blob: f2703ca17954636b059d651a1ebf78c03bfde2dc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/arm/mach-rpc/dma.c
3 *
4 * Copyright (C) 1998 Russell King
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * DMA functions specific to RiscPC architecture
11 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/mman.h>
13#include <linux/init.h>
14#include <linux/interrupt.h>
Russell King7cdad482006-01-04 15:08:30 +000015#include <linux/dma-mapping.h>
Russell Kingfced80c2008-09-06 12:10:45 +010016#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include <asm/page.h>
19#include <asm/dma.h>
20#include <asm/fiq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/irq.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010022#include <mach/hardware.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080023#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/mach/dma.h>
26#include <asm/hardware/iomd.h>
27
Russell King308d3332009-02-21 21:36:22 +000028struct iomd_dma {
29 struct dma_struct dma;
30 unsigned int state;
31 unsigned long base; /* Controller base address */
32 int irq; /* Controller IRQ */
33 struct scatterlist cur_sg; /* Current controller buffer */
Russell Kingfa4e9982009-02-21 21:38:56 +000034 dma_addr_t dma_addr;
35 unsigned int dma_len;
Russell King308d3332009-02-21 21:36:22 +000036};
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#if 0
39typedef enum {
40 dma_size_8 = 1,
41 dma_size_16 = 2,
42 dma_size_32 = 4,
43 dma_size_128 = 16
44} dma_size_t;
45#endif
46
47#define TRANSFER_SIZE 2
48
49#define CURA (0)
50#define ENDA (IOMD_IO0ENDA - IOMD_IO0CURA)
51#define CURB (IOMD_IO0CURB - IOMD_IO0CURA)
52#define ENDB (IOMD_IO0ENDB - IOMD_IO0CURA)
53#define CR (IOMD_IO0CR - IOMD_IO0CURA)
54#define ST (IOMD_IO0ST - IOMD_IO0CURA)
55
Russell Kingad9dd942008-12-08 17:35:48 +000056static void iomd_get_next_sg(struct scatterlist *sg, struct iomd_dma *idma)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 unsigned long end, offset, flags = 0;
59
Russell Kingad9dd942008-12-08 17:35:48 +000060 if (idma->dma.sg) {
Russell Kingfa4e9982009-02-21 21:38:56 +000061 sg->dma_address = idma->dma_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 offset = sg->dma_address & ~PAGE_MASK;
63
Russell Kingfa4e9982009-02-21 21:38:56 +000064 end = offset + idma->dma_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 if (end > PAGE_SIZE)
67 end = PAGE_SIZE;
68
69 if (offset + TRANSFER_SIZE >= end)
70 flags |= DMA_END_L;
71
72 sg->length = end - TRANSFER_SIZE;
73
Russell Kingfa4e9982009-02-21 21:38:56 +000074 idma->dma_len -= end - offset;
75 idma->dma_addr += end - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Russell Kingfa4e9982009-02-21 21:38:56 +000077 if (idma->dma_len == 0) {
Russell Kingad9dd942008-12-08 17:35:48 +000078 if (idma->dma.sgcount > 1) {
Russell King9e28d7e2008-12-08 19:03:58 +000079 idma->dma.sg = sg_next(idma->dma.sg);
Russell Kingfa4e9982009-02-21 21:38:56 +000080 idma->dma_addr = idma->dma.sg->dma_address;
81 idma->dma_len = idma->dma.sg->length;
Russell Kingad9dd942008-12-08 17:35:48 +000082 idma->dma.sgcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 } else {
Russell Kingad9dd942008-12-08 17:35:48 +000084 idma->dma.sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 flags |= DMA_END_S;
86 }
87 }
88 } else {
89 flags = DMA_END_S | DMA_END_L;
90 sg->dma_address = 0;
91 sg->length = 0;
92 }
93
94 sg->length |= flags;
95}
96
Linus Torvalds0cd61b62006-10-06 10:53:39 -070097static irqreturn_t iomd_dma_handle(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Russell Kingad9dd942008-12-08 17:35:48 +000099 struct iomd_dma *idma = dev_id;
100 unsigned long base = idma->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 do {
103 unsigned int status;
104
105 status = iomd_readb(base + ST);
106 if (!(status & DMA_ST_INT))
107 return IRQ_HANDLED;
108
Russell Kingad9dd942008-12-08 17:35:48 +0000109 if ((idma->state ^ status) & DMA_ST_AB)
110 iomd_get_next_sg(&idma->cur_sg, idma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 switch (status & (DMA_ST_OFL | DMA_ST_AB)) {
113 case DMA_ST_OFL: /* OIA */
114 case DMA_ST_AB: /* .IB */
Russell Kingad9dd942008-12-08 17:35:48 +0000115 iomd_writel(idma->cur_sg.dma_address, base + CURA);
116 iomd_writel(idma->cur_sg.length, base + ENDA);
117 idma->state = DMA_ST_AB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 break;
119
120 case DMA_ST_OFL | DMA_ST_AB: /* OIB */
121 case 0: /* .IA */
Russell Kingad9dd942008-12-08 17:35:48 +0000122 iomd_writel(idma->cur_sg.dma_address, base + CURB);
123 iomd_writel(idma->cur_sg.length, base + ENDB);
124 idma->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 break;
126 }
127
128 if (status & DMA_ST_OFL &&
Russell Kingad9dd942008-12-08 17:35:48 +0000129 idma->cur_sg.length == (DMA_END_S|DMA_END_L))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 break;
131 } while (1);
132
Russell Kingad9dd942008-12-08 17:35:48 +0000133 idma->state = ~DMA_ST_AB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 disable_irq(irq);
135
136 return IRQ_HANDLED;
137}
138
Russell King1df81302008-12-08 15:58:50 +0000139static int iomd_request_dma(unsigned int chan, dma_t *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Russell Kingad9dd942008-12-08 17:35:48 +0000141 struct iomd_dma *idma = container_of(dma, struct iomd_dma, dma);
142
143 return request_irq(idma->irq, iomd_dma_handle,
Michael Opdenacker78f6db92014-03-04 22:04:50 +0100144 0, idma->dma.device_id, idma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
Russell King1df81302008-12-08 15:58:50 +0000147static void iomd_free_dma(unsigned int chan, dma_t *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Russell Kingad9dd942008-12-08 17:35:48 +0000149 struct iomd_dma *idma = container_of(dma, struct iomd_dma, dma);
150
151 free_irq(idma->irq, idma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
Christoph Hellwig5ab6a912018-12-21 14:56:38 +0100154static struct device isa_dma_dev = {
155 .init_name = "fallback device",
156 .coherent_dma_mask = ~(dma_addr_t)0,
157 .dma_mask = &isa_dma_dev.coherent_dma_mask,
158};
159
Russell King1df81302008-12-08 15:58:50 +0000160static void iomd_enable_dma(unsigned int chan, dma_t *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Russell Kingad9dd942008-12-08 17:35:48 +0000162 struct iomd_dma *idma = container_of(dma, struct iomd_dma, dma);
163 unsigned long dma_base = idma->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 unsigned int ctrl = TRANSFER_SIZE | DMA_CR_E;
165
Russell Kingad9dd942008-12-08 17:35:48 +0000166 if (idma->dma.invalid) {
167 idma->dma.invalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 /*
170 * Cope with ISA-style drivers which expect cache
171 * coherence.
172 */
Russell Kingad9dd942008-12-08 17:35:48 +0000173 if (!idma->dma.sg) {
174 idma->dma.sg = &idma->dma.buf;
175 idma->dma.sgcount = 1;
176 idma->dma.buf.length = idma->dma.count;
Christoph Hellwig5ab6a912018-12-21 14:56:38 +0100177 idma->dma.buf.dma_address = dma_map_single(&isa_dma_dev,
Russell Kingad9dd942008-12-08 17:35:48 +0000178 idma->dma.addr, idma->dma.count,
179 idma->dma.dma_mode == DMA_MODE_READ ?
Russell King7cdad482006-01-04 15:08:30 +0000180 DMA_FROM_DEVICE : DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
182
183 iomd_writeb(DMA_CR_C, dma_base + CR);
Russell Kingad9dd942008-12-08 17:35:48 +0000184 idma->state = DMA_ST_AB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
Russell Kingad9dd942008-12-08 17:35:48 +0000186
187 if (idma->dma.dma_mode == DMA_MODE_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 ctrl |= DMA_CR_D;
189
190 iomd_writeb(ctrl, dma_base + CR);
Russell Kingad9dd942008-12-08 17:35:48 +0000191 enable_irq(idma->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
Russell King1df81302008-12-08 15:58:50 +0000194static void iomd_disable_dma(unsigned int chan, dma_t *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Russell Kingad9dd942008-12-08 17:35:48 +0000196 struct iomd_dma *idma = container_of(dma, struct iomd_dma, dma);
197 unsigned long dma_base = idma->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 unsigned long flags;
199
200 local_irq_save(flags);
Russell Kingad9dd942008-12-08 17:35:48 +0000201 if (idma->state != ~DMA_ST_AB)
202 disable_irq(idma->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 iomd_writeb(0, dma_base + CR);
204 local_irq_restore(flags);
205}
206
Russell King1df81302008-12-08 15:58:50 +0000207static int iomd_set_dma_speed(unsigned int chan, dma_t *dma, int cycle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
209 int tcr, speed;
210
211 if (cycle < 188)
212 speed = 3;
213 else if (cycle <= 250)
214 speed = 2;
215 else if (cycle < 438)
216 speed = 1;
217 else
218 speed = 0;
219
220 tcr = iomd_readb(IOMD_DMATCR);
221 speed &= 3;
222
Russell King1df81302008-12-08 15:58:50 +0000223 switch (chan) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 case DMA_0:
225 tcr = (tcr & ~0x03) | speed;
226 break;
227
228 case DMA_1:
229 tcr = (tcr & ~0x0c) | (speed << 2);
230 break;
231
232 case DMA_2:
233 tcr = (tcr & ~0x30) | (speed << 4);
234 break;
235
236 case DMA_3:
237 tcr = (tcr & ~0xc0) | (speed << 6);
238 break;
239
240 default:
241 break;
242 }
243
244 iomd_writeb(tcr, IOMD_DMATCR);
245
246 return speed;
247}
248
249static struct dma_ops iomd_dma_ops = {
250 .type = "IOMD",
251 .request = iomd_request_dma,
252 .free = iomd_free_dma,
253 .enable = iomd_enable_dma,
254 .disable = iomd_disable_dma,
255 .setspeed = iomd_set_dma_speed,
256};
257
258static struct fiq_handler fh = {
259 .name = "floppydma"
260};
261
Russell King308d3332009-02-21 21:36:22 +0000262struct floppy_dma {
263 struct dma_struct dma;
264 unsigned int fiq;
265};
266
Russell King1df81302008-12-08 15:58:50 +0000267static void floppy_enable_dma(unsigned int chan, dma_t *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Russell Kingad9dd942008-12-08 17:35:48 +0000269 struct floppy_dma *fdma = container_of(dma, struct floppy_dma, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 void *fiqhandler_start;
271 unsigned int fiqhandler_length;
272 struct pt_regs regs;
273
Russell Kingad9dd942008-12-08 17:35:48 +0000274 if (fdma->dma.sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 BUG();
276
Russell Kingad9dd942008-12-08 17:35:48 +0000277 if (fdma->dma.dma_mode == DMA_MODE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 extern unsigned char floppy_fiqin_start, floppy_fiqin_end;
279 fiqhandler_start = &floppy_fiqin_start;
280 fiqhandler_length = &floppy_fiqin_end - &floppy_fiqin_start;
281 } else {
282 extern unsigned char floppy_fiqout_start, floppy_fiqout_end;
283 fiqhandler_start = &floppy_fiqout_start;
284 fiqhandler_length = &floppy_fiqout_end - &floppy_fiqout_start;
285 }
286
Russell Kingad9dd942008-12-08 17:35:48 +0000287 regs.ARM_r9 = fdma->dma.count;
288 regs.ARM_r10 = (unsigned long)fdma->dma.addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 regs.ARM_fp = (unsigned long)FLOPPYDMA_BASE;
290
291 if (claim_fiq(&fh)) {
292 printk("floppydma: couldn't claim FIQ.\n");
293 return;
294 }
295
296 set_fiq_handler(fiqhandler_start, fiqhandler_length);
297 set_fiq_regs(&regs);
Russell Kingad9dd942008-12-08 17:35:48 +0000298 enable_fiq(fdma->fiq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
Russell King1df81302008-12-08 15:58:50 +0000301static void floppy_disable_dma(unsigned int chan, dma_t *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Russell Kingad9dd942008-12-08 17:35:48 +0000303 struct floppy_dma *fdma = container_of(dma, struct floppy_dma, dma);
304 disable_fiq(fdma->fiq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 release_fiq(&fh);
306}
307
Russell King1df81302008-12-08 15:58:50 +0000308static int floppy_get_residue(unsigned int chan, dma_t *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 struct pt_regs regs;
311 get_fiq_regs(&regs);
312 return regs.ARM_r9;
313}
314
315static struct dma_ops floppy_dma_ops = {
316 .type = "FIQDMA",
317 .enable = floppy_enable_dma,
318 .disable = floppy_disable_dma,
319 .residue = floppy_get_residue,
320};
321
322/*
323 * This is virtual DMA - we don't need anything here.
324 */
Russell King1df81302008-12-08 15:58:50 +0000325static void sound_enable_disable_dma(unsigned int chan, dma_t *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327}
328
329static struct dma_ops sound_dma_ops = {
330 .type = "VIRTUAL",
331 .enable = sound_enable_disable_dma,
332 .disable = sound_enable_disable_dma,
333};
334
Russell Kingad9dd942008-12-08 17:35:48 +0000335static struct iomd_dma iomd_dma[6];
Russell King2f757f22008-12-08 16:33:30 +0000336
Russell Kingad9dd942008-12-08 17:35:48 +0000337static struct floppy_dma floppy_dma = {
338 .dma = {
339 .d_ops = &floppy_dma_ops,
340 },
341 .fiq = FIQ_FLOPPYDATA,
Russell King2f757f22008-12-08 16:33:30 +0000342};
343
344static dma_t sound_dma = {
345 .d_ops = &sound_dma_ops,
346};
347
348static int __init rpc_dma_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Russell King2f757f22008-12-08 16:33:30 +0000350 unsigned int i;
351 int ret;
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 iomd_writeb(0, IOMD_IO0CR);
354 iomd_writeb(0, IOMD_IO1CR);
355 iomd_writeb(0, IOMD_IO2CR);
356 iomd_writeb(0, IOMD_IO3CR);
357
358 iomd_writeb(0xa0, IOMD_DMATCR);
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 /*
361 * Setup DMA channels 2,3 to be for podules
362 * and channels 0,1 for internal devices
363 */
364 iomd_writeb(DMA_EXT_IO3|DMA_EXT_IO2, IOMD_DMAEXT);
Russell King2f757f22008-12-08 16:33:30 +0000365
Russell Kingad9dd942008-12-08 17:35:48 +0000366 iomd_dma[DMA_0].base = IOMD_IO0CURA;
367 iomd_dma[DMA_0].irq = IRQ_DMA0;
368 iomd_dma[DMA_1].base = IOMD_IO1CURA;
369 iomd_dma[DMA_1].irq = IRQ_DMA1;
370 iomd_dma[DMA_2].base = IOMD_IO2CURA;
371 iomd_dma[DMA_2].irq = IRQ_DMA2;
372 iomd_dma[DMA_3].base = IOMD_IO3CURA;
373 iomd_dma[DMA_3].irq = IRQ_DMA3;
374 iomd_dma[DMA_S0].base = IOMD_SD0CURA;
375 iomd_dma[DMA_S0].irq = IRQ_DMAS0;
376 iomd_dma[DMA_S1].base = IOMD_SD1CURA;
377 iomd_dma[DMA_S1].irq = IRQ_DMAS1;
Russell King2f757f22008-12-08 16:33:30 +0000378
379 for (i = DMA_0; i <= DMA_S1; i++) {
Russell Kingad9dd942008-12-08 17:35:48 +0000380 iomd_dma[i].dma.d_ops = &iomd_dma_ops;
Russell King2f757f22008-12-08 16:33:30 +0000381
Russell Kingad9dd942008-12-08 17:35:48 +0000382 ret = isa_dma_add(i, &iomd_dma[i].dma);
Russell King2f757f22008-12-08 16:33:30 +0000383 if (ret)
384 printk("IOMDDMA%u: unable to register: %d\n", i, ret);
385 }
386
Russell Kingad9dd942008-12-08 17:35:48 +0000387 ret = isa_dma_add(DMA_VIRTUAL_FLOPPY, &floppy_dma.dma);
Russell King2f757f22008-12-08 16:33:30 +0000388 if (ret)
389 printk("IOMDFLOPPY: unable to register: %d\n", ret);
390 ret = isa_dma_add(DMA_VIRTUAL_SOUND, &sound_dma);
391 if (ret)
392 printk("IOMDSOUND: unable to register: %d\n", ret);
393 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
Russell King2f757f22008-12-08 16:33:30 +0000395core_initcall(rpc_dma_init);