dma-sh.c 6.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7
/*
 * arch/sh/drivers/dma/dma-sh.c
 *
 * SuperH On-chip DMAC Support
 *
 * Copyright (C) 2000 Takashi YOSHII
 * Copyright (C) 2003, 2004 Paul Mundt
P
Paul Mundt 已提交
8
 * Copyright (C) 2005 Andriy Skulysh
L
Linus Torvalds 已提交
9 10 11 12 13 14 15 16
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 */
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/module.h>
P
Paul Mundt 已提交
17
#include <asm/dreamcast/dma.h>
L
Linus Torvalds 已提交
18 19 20 21 22 23
#include <asm/dma.h>
#include <asm/io.h>
#include "dma-sh.h"

static inline unsigned int get_dmte_irq(unsigned int chan)
{
P
Paul Mundt 已提交
24
	unsigned int irq = 0;
L
Linus Torvalds 已提交
25 26 27 28 29 30 31 32 33

	/*
	 * Normally we could just do DMTE0_IRQ + chan outright, though in the
	 * case of the 7751R, the DMTE IRQs for channels > 4 start right above
	 * the SCIF
	 */
	if (chan < 4) {
		irq = DMTE0_IRQ + chan;
	} else {
P
Paul Mundt 已提交
34
#ifdef DMTE4_IRQ
L
Linus Torvalds 已提交
35
		irq = DMTE4_IRQ + chan - 4;
P
Paul Mundt 已提交
36
#endif
L
Linus Torvalds 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
	}

	return irq;
}

/*
 * We determine the correct shift size based off of the CHCR transmit size
 * for the given channel. Since we know that it will take:
 *
 *	info->count >> ts_shift[transmit_size]
 *
 * iterations to complete the transfer.
 */
static inline unsigned int calc_xmit_shift(struct dma_channel *chan)
{
	u32 chcr = ctrl_inl(CHCR[chan->chan]);

P
Paul Mundt 已提交
54
	return ts_shift[(chcr & CHCR_TS_MASK)>>CHCR_TS_SHIFT];
L
Linus Torvalds 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
}

/*
 * The transfer end interrupt must read the chcr register to end the
 * hardware interrupt active condition.
 * Besides that it needs to waken any waiting process, which should handle
 * setting up the next transfer.
 */
static irqreturn_t dma_tei(int irq, void *dev_id, struct pt_regs *regs)
{
	struct dma_channel *chan = (struct dma_channel *)dev_id;
	u32 chcr;

	chcr = ctrl_inl(CHCR[chan->chan]);

	if (!(chcr & CHCR_TE))
		return IRQ_NONE;

	chcr &= ~(CHCR_IE | CHCR_DE);
	ctrl_outl(chcr, CHCR[chan->chan]);

	wake_up(&chan->wait_queue);

	return IRQ_HANDLED;
}

static int sh_dmac_request_dma(struct dma_channel *chan)
{
P
Paul Mundt 已提交
83 84
	char name[32];

85 86 87
	if (unlikely(!chan->flags & DMA_TEI_CAPABLE))
		return 0;

P
Paul Mundt 已提交
88 89 90
	snprintf(name, sizeof(name), "DMAC Transfer End (Channel %d)",
		 chan->chan);

L
Linus Torvalds 已提交
91
	return request_irq(get_dmte_irq(chan->chan), dma_tei,
92
			   IRQF_DISABLED, name, chan);
L
Linus Torvalds 已提交
93 94 95 96 97 98 99
}

static void sh_dmac_free_dma(struct dma_channel *chan)
{
	free_irq(get_dmte_irq(chan->chan), chan);
}

P
Paul Mundt 已提交
100 101
static void
sh_dmac_configure_channel(struct dma_channel *chan, unsigned long chcr)
L
Linus Torvalds 已提交
102 103
{
	if (!chcr)
P
Paul Mundt 已提交
104 105 106 107 108 109 110 111
		chcr = RS_DUAL | CHCR_IE;

	if (chcr & CHCR_IE) {
		chcr &= ~CHCR_IE;
		chan->flags |= DMA_TEI_CAPABLE;
	} else {
		chan->flags &= ~DMA_TEI_CAPABLE;
	}
L
Linus Torvalds 已提交
112 113 114 115 116 117 118 119

	ctrl_outl(chcr, CHCR[chan->chan]);

	chan->flags |= DMA_CONFIGURED;
}

static void sh_dmac_enable_dma(struct dma_channel *chan)
{
P
Paul Mundt 已提交
120
	int irq;
L
Linus Torvalds 已提交
121 122 123
	u32 chcr;

	chcr = ctrl_inl(CHCR[chan->chan]);
P
Paul Mundt 已提交
124 125 126 127 128
	chcr |= CHCR_DE;

	if (chan->flags & DMA_TEI_CAPABLE)
		chcr |= CHCR_IE;

L
Linus Torvalds 已提交
129 130
	ctrl_outl(chcr, CHCR[chan->chan]);

P
Paul Mundt 已提交
131 132 133 134
	if (chan->flags & DMA_TEI_CAPABLE) {
		irq = get_dmte_irq(chan->chan);
		enable_irq(irq);
	}
L
Linus Torvalds 已提交
135 136 137 138
}

static void sh_dmac_disable_dma(struct dma_channel *chan)
{
P
Paul Mundt 已提交
139
	int irq;
L
Linus Torvalds 已提交
140 141
	u32 chcr;

P
Paul Mundt 已提交
142 143 144 145
	if (chan->flags & DMA_TEI_CAPABLE) {
		irq = get_dmte_irq(chan->chan);
		disable_irq(irq);
	}
L
Linus Torvalds 已提交
146 147 148 149 150 151 152 153 154 155 156 157

	chcr = ctrl_inl(CHCR[chan->chan]);
	chcr &= ~(CHCR_DE | CHCR_TE | CHCR_IE);
	ctrl_outl(chcr, CHCR[chan->chan]);
}

static int sh_dmac_xfer_dma(struct dma_channel *chan)
{
	/*
	 * If we haven't pre-configured the channel with special flags, use
	 * the defaults.
	 */
P
Paul Mundt 已提交
158
	if (unlikely(!(chan->flags & DMA_CONFIGURED)))
L
Linus Torvalds 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
		sh_dmac_configure_channel(chan, 0);

	sh_dmac_disable_dma(chan);

	/*
	 * Single-address mode usage note!
	 *
	 * It's important that we don't accidentally write any value to SAR/DAR
	 * (this includes 0) that hasn't been directly specified by the user if
	 * we're in single-address mode.
	 *
	 * In this case, only one address can be defined, anything else will
	 * result in a DMA address error interrupt (at least on the SH-4),
	 * which will subsequently halt the transfer.
	 *
	 * Channel 2 on the Dreamcast is a special case, as this is used for
	 * cascading to the PVR2 DMAC. In this case, we still need to write
	 * SAR and DAR, regardless of value, in order for cascading to work.
	 */
P
Paul Mundt 已提交
178 179
	if (chan->sar || (mach_is_dreamcast() &&
			  chan->chan == PVR2_CASCADE_CHAN))
L
Linus Torvalds 已提交
180
		ctrl_outl(chan->sar, SAR[chan->chan]);
P
Paul Mundt 已提交
181 182
	if (chan->dar || (mach_is_dreamcast() &&
			  chan->chan == PVR2_CASCADE_CHAN))
L
Linus Torvalds 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
		ctrl_outl(chan->dar, DAR[chan->chan]);

	ctrl_outl(chan->count >> calc_xmit_shift(chan), DMATCR[chan->chan]);

	sh_dmac_enable_dma(chan);

	return 0;
}

static int sh_dmac_get_dma_residue(struct dma_channel *chan)
{
	if (!(ctrl_inl(CHCR[chan->chan]) & CHCR_DE))
		return 0;

	return ctrl_inl(DMATCR[chan->chan]) << calc_xmit_shift(chan);
}

P
Paul Mundt 已提交
200 201 202 203 204 205 206 207 208
#ifdef CONFIG_CPU_SUBTYPE_SH7780
#define dmaor_read_reg()	ctrl_inw(DMAOR)
#define dmaor_write_reg(data)	ctrl_outw(data, DMAOR)
#else
#define dmaor_read_reg()	ctrl_inl(DMAOR)
#define dmaor_write_reg(data)	ctrl_outl(data, DMAOR)
#endif

static inline int dmaor_reset(void)
L
Linus Torvalds 已提交
209
{
P
Paul Mundt 已提交
210 211 212 213 214
	unsigned long dmaor = dmaor_read_reg();

	/* Try to clear the error flags first, incase they are set */
	dmaor &= ~(DMAOR_NMIF | DMAOR_AE);
	dmaor_write_reg(dmaor);
L
Linus Torvalds 已提交
215

P
Paul Mundt 已提交
216 217
	dmaor |= DMAOR_INIT;
	dmaor_write_reg(dmaor);
L
Linus Torvalds 已提交
218

P
Paul Mundt 已提交
219 220 221 222 223
	/* See if we got an error again */
	if ((dmaor_read_reg() & (DMAOR_AE | DMAOR_NMIF))) {
		printk(KERN_ERR "dma-sh: Can't initialize DMAOR.\n");
		return -EINVAL;
	}
L
Linus Torvalds 已提交
224

P
Paul Mundt 已提交
225 226 227 228 229 230 231
	return 0;
}

#if defined(CONFIG_CPU_SH4)
static irqreturn_t dma_err(int irq, void *dev_id, struct pt_regs *regs)
{
	dmaor_reset();
L
Linus Torvalds 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
	disable_irq(irq);

	return IRQ_HANDLED;
}
#endif

static struct dma_ops sh_dmac_ops = {
	.request	= sh_dmac_request_dma,
	.free		= sh_dmac_free_dma,
	.get_residue	= sh_dmac_get_dma_residue,
	.xfer		= sh_dmac_xfer_dma,
	.configure	= sh_dmac_configure_channel,
};

static struct dma_info sh_dmac_info = {
P
Paul Mundt 已提交
247 248
	.name		= "sh_dmac",
	.nr_channels	= CONFIG_NR_ONCHIP_DMA_CHANNELS,
L
Linus Torvalds 已提交
249 250 251 252 253 254 255 256 257 258 259
	.ops		= &sh_dmac_ops,
	.flags		= DMAC_CHANNELS_TEI_CAPABLE,
};

static int __init sh_dmac_init(void)
{
	struct dma_info *info = &sh_dmac_info;
	int i;

#ifdef CONFIG_CPU_SH4
	make_ipr_irq(DMAE_IRQ, DMA_IPR_ADDR, DMA_IPR_POS, DMA_PRIORITY);
260
	i = request_irq(DMAE_IRQ, dma_err, IRQF_DISABLED, "DMAC Address Error", 0);
261
	if (unlikely(i < 0))
L
Linus Torvalds 已提交
262 263 264 265 266 267 268 269 270
		return i;
#endif

	for (i = 0; i < info->nr_channels; i++) {
		int irq = get_dmte_irq(i);

		make_ipr_irq(irq, DMA_IPR_ADDR, DMA_IPR_POS, DMA_PRIORITY);
	}

P
Paul Mundt 已提交
271 272 273 274 275
	/*
	 * Initialize DMAOR, and clean up any error flags that may have
	 * been set.
	 */
	i = dmaor_reset();
276
	if (unlikely(i != 0))
P
Paul Mundt 已提交
277
		return i;
L
Linus Torvalds 已提交
278 279 280 281 282 283 284 285 286

	return register_dmac(info);
}

static void __exit sh_dmac_exit(void)
{
#ifdef CONFIG_CPU_SH4
	free_irq(DMAE_IRQ, 0);
#endif
P
Paul Mundt 已提交
287
	unregister_dmac(&sh_dmac_info);
L
Linus Torvalds 已提交
288 289 290 291 292
}

subsys_initcall(sh_dmac_init);
module_exit(sh_dmac_exit);

P
Paul Mundt 已提交
293 294
MODULE_AUTHOR("Takashi YOSHII, Paul Mundt, Andriy Skulysh");
MODULE_DESCRIPTION("SuperH On-Chip DMAC Support");
L
Linus Torvalds 已提交
295
MODULE_LICENSE("GPL");