blob: 50e0f97afd75ebef90153f5f61a565e87f0bf175 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/arch/arm/mach-rpc/dma.c
4 *
5 * Copyright (C) 1998 Russell King
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * DMA functions specific to RiscPC architecture
8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/mman.h>
10#include <linux/init.h>
11#include <linux/interrupt.h>
Russell King7cdad482006-01-04 15:08:30 +000012#include <linux/dma-mapping.h>
Russell Kingfced80c2008-09-06 12:10:45 +010013#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include <asm/page.h>
16#include <asm/dma.h>
17#include <asm/fiq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/irq.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010019#include <mach/hardware.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080020#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include <asm/mach/dma.h>
23#include <asm/hardware/iomd.h>
24
Russell King308d3332009-02-21 21:36:22 +000025struct iomd_dma {
26 struct dma_struct dma;
Russell King090a37c2019-05-02 17:56:49 +010027 void __iomem *base; /* Controller base address */
Russell King308d3332009-02-21 21:36:22 +000028 int irq; /* Controller IRQ */
Russell King090a37c2019-05-02 17:56:49 +010029 unsigned int state;
Russell King81944682019-05-02 17:37:51 +010030 dma_addr_t cur_addr;
31 unsigned int cur_len;
Russell Kingfa4e9982009-02-21 21:38:56 +000032 dma_addr_t dma_addr;
33 unsigned int dma_len;
Russell King308d3332009-02-21 21:36:22 +000034};
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#if 0
37typedef enum {
38 dma_size_8 = 1,
39 dma_size_16 = 2,
40 dma_size_32 = 4,
41 dma_size_128 = 16
42} dma_size_t;
43#endif
44
45#define TRANSFER_SIZE 2
46
47#define CURA (0)
48#define ENDA (IOMD_IO0ENDA - IOMD_IO0CURA)
49#define CURB (IOMD_IO0CURB - IOMD_IO0CURA)
50#define ENDB (IOMD_IO0ENDB - IOMD_IO0CURA)
51#define CR (IOMD_IO0CR - IOMD_IO0CURA)
52#define ST (IOMD_IO0ST - IOMD_IO0CURA)
53
Russell King81944682019-05-02 17:37:51 +010054static void iomd_get_next_sg(struct iomd_dma *idma)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
56 unsigned long end, offset, flags = 0;
57
Russell Kingad9dd942008-12-08 17:35:48 +000058 if (idma->dma.sg) {
Russell King81944682019-05-02 17:37:51 +010059 idma->cur_addr = idma->dma_addr;
60 offset = idma->cur_addr & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Russell Kingfa4e9982009-02-21 21:38:56 +000062 end = offset + idma->dma_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 if (end > PAGE_SIZE)
65 end = PAGE_SIZE;
66
67 if (offset + TRANSFER_SIZE >= end)
68 flags |= DMA_END_L;
69
Russell King81944682019-05-02 17:37:51 +010070 idma->cur_len = end - TRANSFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Russell Kingfa4e9982009-02-21 21:38:56 +000072 idma->dma_len -= end - offset;
73 idma->dma_addr += end - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Russell Kingfa4e9982009-02-21 21:38:56 +000075 if (idma->dma_len == 0) {
Russell Kingad9dd942008-12-08 17:35:48 +000076 if (idma->dma.sgcount > 1) {
Russell King9e28d7e2008-12-08 19:03:58 +000077 idma->dma.sg = sg_next(idma->dma.sg);
Russell Kingfa4e9982009-02-21 21:38:56 +000078 idma->dma_addr = idma->dma.sg->dma_address;
79 idma->dma_len = idma->dma.sg->length;
Russell Kingad9dd942008-12-08 17:35:48 +000080 idma->dma.sgcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 } else {
Russell Kingad9dd942008-12-08 17:35:48 +000082 idma->dma.sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 flags |= DMA_END_S;
84 }
85 }
86 } else {
87 flags = DMA_END_S | DMA_END_L;
Russell King81944682019-05-02 17:37:51 +010088 idma->cur_addr = 0;
89 idma->cur_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
91
Russell King81944682019-05-02 17:37:51 +010092 idma->cur_len |= flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
Linus Torvalds0cd61b62006-10-06 10:53:39 -070095static irqreturn_t iomd_dma_handle(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Russell Kingad9dd942008-12-08 17:35:48 +000097 struct iomd_dma *idma = dev_id;
Russell King090a37c2019-05-02 17:56:49 +010098 void __iomem *base = idma->base;
Russell King39694ed2019-05-02 17:43:36 +010099 unsigned int state = idma->state;
Russell Kinge6595872019-05-02 17:50:55 +0100100 unsigned int status, cur, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 do {
Russell King090a37c2019-05-02 17:56:49 +0100103 status = readb(base + ST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 if (!(status & DMA_ST_INT))
Russell King39694ed2019-05-02 17:43:36 +0100105 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Russell King39694ed2019-05-02 17:43:36 +0100107 if ((state ^ status) & DMA_ST_AB)
Russell King81944682019-05-02 17:37:51 +0100108 iomd_get_next_sg(idma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Russell Kinge6595872019-05-02 17:50:55 +0100110 // This efficiently implements state = OFL != AB ? AB : 0
111 state = ((status >> 2) ^ status) & DMA_ST_AB;
112 if (state) {
113 cur = CURA;
114 end = ENDA;
115 } else {
116 cur = CURB;
117 end = ENDB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
Russell King090a37c2019-05-02 17:56:49 +0100119 writel(idma->cur_addr, base + cur);
120 writel(idma->cur_len, base + end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 if (status & DMA_ST_OFL &&
Russell King81944682019-05-02 17:37:51 +0100123 idma->cur_len == (DMA_END_S|DMA_END_L))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 break;
125 } while (1);
126
Russell King39694ed2019-05-02 17:43:36 +0100127 state = ~DMA_ST_AB;
Russell Kingffd9a1b2019-05-02 17:19:18 +0100128 disable_irq_nosync(irq);
Russell King39694ed2019-05-02 17:43:36 +0100129out:
130 idma->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return IRQ_HANDLED;
132}
133
Russell King1df81302008-12-08 15:58:50 +0000134static int iomd_request_dma(unsigned int chan, dma_t *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Russell Kingad9dd942008-12-08 17:35:48 +0000136 struct iomd_dma *idma = container_of(dma, struct iomd_dma, dma);
137
138 return request_irq(idma->irq, iomd_dma_handle,
Michael Opdenacker78f6db92014-03-04 22:04:50 +0100139 0, idma->dma.device_id, idma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Russell King1df81302008-12-08 15:58:50 +0000142static void iomd_free_dma(unsigned int chan, dma_t *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Russell Kingad9dd942008-12-08 17:35:48 +0000144 struct iomd_dma *idma = container_of(dma, struct iomd_dma, dma);
145
146 free_irq(idma->irq, idma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
Christoph Hellwig5ab6a912018-12-21 14:56:38 +0100149static struct device isa_dma_dev = {
150 .init_name = "fallback device",
151 .coherent_dma_mask = ~(dma_addr_t)0,
152 .dma_mask = &isa_dma_dev.coherent_dma_mask,
153};
154
Russell King1df81302008-12-08 15:58:50 +0000155static void iomd_enable_dma(unsigned int chan, dma_t *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Russell Kingad9dd942008-12-08 17:35:48 +0000157 struct iomd_dma *idma = container_of(dma, struct iomd_dma, dma);
Russell King090a37c2019-05-02 17:56:49 +0100158 void __iomem *base = idma->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 unsigned int ctrl = TRANSFER_SIZE | DMA_CR_E;
160
Russell Kingad9dd942008-12-08 17:35:48 +0000161 if (idma->dma.invalid) {
162 idma->dma.invalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 /*
165 * Cope with ISA-style drivers which expect cache
166 * coherence.
167 */
Russell Kingad9dd942008-12-08 17:35:48 +0000168 if (!idma->dma.sg) {
169 idma->dma.sg = &idma->dma.buf;
170 idma->dma.sgcount = 1;
171 idma->dma.buf.length = idma->dma.count;
Christoph Hellwig5ab6a912018-12-21 14:56:38 +0100172 idma->dma.buf.dma_address = dma_map_single(&isa_dma_dev,
Russell Kingad9dd942008-12-08 17:35:48 +0000173 idma->dma.addr, idma->dma.count,
174 idma->dma.dma_mode == DMA_MODE_READ ?
Russell King7cdad482006-01-04 15:08:30 +0000175 DMA_FROM_DEVICE : DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
177
Russell Kingffd9a1b2019-05-02 17:19:18 +0100178 idma->dma_addr = idma->dma.sg->dma_address;
179 idma->dma_len = idma->dma.sg->length;
180
Russell King090a37c2019-05-02 17:56:49 +0100181 writeb(DMA_CR_C, base + CR);
Russell Kingad9dd942008-12-08 17:35:48 +0000182 idma->state = DMA_ST_AB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
Russell Kingad9dd942008-12-08 17:35:48 +0000184
185 if (idma->dma.dma_mode == DMA_MODE_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 ctrl |= DMA_CR_D;
187
Russell King090a37c2019-05-02 17:56:49 +0100188 writeb(ctrl, base + CR);
Russell Kingad9dd942008-12-08 17:35:48 +0000189 enable_irq(idma->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Russell King1df81302008-12-08 15:58:50 +0000192static void iomd_disable_dma(unsigned int chan, dma_t *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Russell Kingad9dd942008-12-08 17:35:48 +0000194 struct iomd_dma *idma = container_of(dma, struct iomd_dma, dma);
Russell King090a37c2019-05-02 17:56:49 +0100195 void __iomem *base = idma->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 unsigned long flags;
197
198 local_irq_save(flags);
Russell Kingad9dd942008-12-08 17:35:48 +0000199 if (idma->state != ~DMA_ST_AB)
200 disable_irq(idma->irq);
Russell King090a37c2019-05-02 17:56:49 +0100201 writeb(0, base + CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 local_irq_restore(flags);
203}
204
Russell King1df81302008-12-08 15:58:50 +0000205static int iomd_set_dma_speed(unsigned int chan, dma_t *dma, int cycle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
207 int tcr, speed;
208
209 if (cycle < 188)
210 speed = 3;
211 else if (cycle <= 250)
212 speed = 2;
213 else if (cycle < 438)
214 speed = 1;
215 else
216 speed = 0;
217
218 tcr = iomd_readb(IOMD_DMATCR);
219 speed &= 3;
220
Russell King1df81302008-12-08 15:58:50 +0000221 switch (chan) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 case DMA_0:
223 tcr = (tcr & ~0x03) | speed;
224 break;
225
226 case DMA_1:
227 tcr = (tcr & ~0x0c) | (speed << 2);
228 break;
229
230 case DMA_2:
231 tcr = (tcr & ~0x30) | (speed << 4);
232 break;
233
234 case DMA_3:
235 tcr = (tcr & ~0xc0) | (speed << 6);
236 break;
237
238 default:
239 break;
240 }
241
242 iomd_writeb(tcr, IOMD_DMATCR);
243
244 return speed;
245}
246
247static struct dma_ops iomd_dma_ops = {
248 .type = "IOMD",
249 .request = iomd_request_dma,
250 .free = iomd_free_dma,
251 .enable = iomd_enable_dma,
252 .disable = iomd_disable_dma,
253 .setspeed = iomd_set_dma_speed,
254};
255
256static struct fiq_handler fh = {
257 .name = "floppydma"
258};
259
Russell King308d3332009-02-21 21:36:22 +0000260struct floppy_dma {
261 struct dma_struct dma;
262 unsigned int fiq;
263};
264
Russell King1df81302008-12-08 15:58:50 +0000265static void floppy_enable_dma(unsigned int chan, dma_t *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Russell Kingad9dd942008-12-08 17:35:48 +0000267 struct floppy_dma *fdma = container_of(dma, struct floppy_dma, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 void *fiqhandler_start;
269 unsigned int fiqhandler_length;
270 struct pt_regs regs;
271
Russell Kingad9dd942008-12-08 17:35:48 +0000272 if (fdma->dma.sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 BUG();
274
Russell Kingad9dd942008-12-08 17:35:48 +0000275 if (fdma->dma.dma_mode == DMA_MODE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 extern unsigned char floppy_fiqin_start, floppy_fiqin_end;
277 fiqhandler_start = &floppy_fiqin_start;
278 fiqhandler_length = &floppy_fiqin_end - &floppy_fiqin_start;
279 } else {
280 extern unsigned char floppy_fiqout_start, floppy_fiqout_end;
281 fiqhandler_start = &floppy_fiqout_start;
282 fiqhandler_length = &floppy_fiqout_end - &floppy_fiqout_start;
283 }
284
Russell Kingad9dd942008-12-08 17:35:48 +0000285 regs.ARM_r9 = fdma->dma.count;
286 regs.ARM_r10 = (unsigned long)fdma->dma.addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 regs.ARM_fp = (unsigned long)FLOPPYDMA_BASE;
288
289 if (claim_fiq(&fh)) {
290 printk("floppydma: couldn't claim FIQ.\n");
291 return;
292 }
293
294 set_fiq_handler(fiqhandler_start, fiqhandler_length);
295 set_fiq_regs(&regs);
Russell Kingad9dd942008-12-08 17:35:48 +0000296 enable_fiq(fdma->fiq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Russell King1df81302008-12-08 15:58:50 +0000299static void floppy_disable_dma(unsigned int chan, dma_t *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Russell Kingad9dd942008-12-08 17:35:48 +0000301 struct floppy_dma *fdma = container_of(dma, struct floppy_dma, dma);
302 disable_fiq(fdma->fiq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 release_fiq(&fh);
304}
305
Russell King1df81302008-12-08 15:58:50 +0000306static int floppy_get_residue(unsigned int chan, dma_t *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 struct pt_regs regs;
309 get_fiq_regs(&regs);
310 return regs.ARM_r9;
311}
312
313static struct dma_ops floppy_dma_ops = {
314 .type = "FIQDMA",
315 .enable = floppy_enable_dma,
316 .disable = floppy_disable_dma,
317 .residue = floppy_get_residue,
318};
319
320/*
321 * This is virtual DMA - we don't need anything here.
322 */
Russell King1df81302008-12-08 15:58:50 +0000323static void sound_enable_disable_dma(unsigned int chan, dma_t *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325}
326
327static struct dma_ops sound_dma_ops = {
328 .type = "VIRTUAL",
329 .enable = sound_enable_disable_dma,
330 .disable = sound_enable_disable_dma,
331};
332
Russell Kingad9dd942008-12-08 17:35:48 +0000333static struct iomd_dma iomd_dma[6];
Russell King2f757f22008-12-08 16:33:30 +0000334
Russell Kingad9dd942008-12-08 17:35:48 +0000335static struct floppy_dma floppy_dma = {
336 .dma = {
337 .d_ops = &floppy_dma_ops,
338 },
339 .fiq = FIQ_FLOPPYDATA,
Russell King2f757f22008-12-08 16:33:30 +0000340};
341
342static dma_t sound_dma = {
343 .d_ops = &sound_dma_ops,
344};
345
346static int __init rpc_dma_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Russell King2f757f22008-12-08 16:33:30 +0000348 unsigned int i;
349 int ret;
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 iomd_writeb(0, IOMD_IO0CR);
352 iomd_writeb(0, IOMD_IO1CR);
353 iomd_writeb(0, IOMD_IO2CR);
354 iomd_writeb(0, IOMD_IO3CR);
355
356 iomd_writeb(0xa0, IOMD_DMATCR);
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 /*
359 * Setup DMA channels 2,3 to be for podules
360 * and channels 0,1 for internal devices
361 */
362 iomd_writeb(DMA_EXT_IO3|DMA_EXT_IO2, IOMD_DMAEXT);
Russell King2f757f22008-12-08 16:33:30 +0000363
Russell King090a37c2019-05-02 17:56:49 +0100364 iomd_dma[DMA_0].base = IOMD_BASE + IOMD_IO0CURA;
Russell Kingad9dd942008-12-08 17:35:48 +0000365 iomd_dma[DMA_0].irq = IRQ_DMA0;
Russell King090a37c2019-05-02 17:56:49 +0100366 iomd_dma[DMA_1].base = IOMD_BASE + IOMD_IO1CURA;
Russell Kingad9dd942008-12-08 17:35:48 +0000367 iomd_dma[DMA_1].irq = IRQ_DMA1;
Russell King090a37c2019-05-02 17:56:49 +0100368 iomd_dma[DMA_2].base = IOMD_BASE + IOMD_IO2CURA;
Russell Kingad9dd942008-12-08 17:35:48 +0000369 iomd_dma[DMA_2].irq = IRQ_DMA2;
Russell King090a37c2019-05-02 17:56:49 +0100370 iomd_dma[DMA_3].base = IOMD_BASE + IOMD_IO3CURA;
Russell Kingad9dd942008-12-08 17:35:48 +0000371 iomd_dma[DMA_3].irq = IRQ_DMA3;
Russell King090a37c2019-05-02 17:56:49 +0100372 iomd_dma[DMA_S0].base = IOMD_BASE + IOMD_SD0CURA;
Russell Kingad9dd942008-12-08 17:35:48 +0000373 iomd_dma[DMA_S0].irq = IRQ_DMAS0;
Russell King090a37c2019-05-02 17:56:49 +0100374 iomd_dma[DMA_S1].base = IOMD_BASE + IOMD_SD1CURA;
Russell Kingad9dd942008-12-08 17:35:48 +0000375 iomd_dma[DMA_S1].irq = IRQ_DMAS1;
Russell King2f757f22008-12-08 16:33:30 +0000376
377 for (i = DMA_0; i <= DMA_S1; i++) {
Russell Kingad9dd942008-12-08 17:35:48 +0000378 iomd_dma[i].dma.d_ops = &iomd_dma_ops;
Russell King2f757f22008-12-08 16:33:30 +0000379
Russell Kingad9dd942008-12-08 17:35:48 +0000380 ret = isa_dma_add(i, &iomd_dma[i].dma);
Russell King2f757f22008-12-08 16:33:30 +0000381 if (ret)
382 printk("IOMDDMA%u: unable to register: %d\n", i, ret);
383 }
384
Russell Kingad9dd942008-12-08 17:35:48 +0000385 ret = isa_dma_add(DMA_VIRTUAL_FLOPPY, &floppy_dma.dma);
Russell King2f757f22008-12-08 16:33:30 +0000386 if (ret)
387 printk("IOMDFLOPPY: unable to register: %d\n", ret);
388 ret = isa_dma_add(DMA_VIRTUAL_SOUND, &sound_dma);
389 if (ret)
390 printk("IOMDSOUND: unable to register: %d\n", ret);
391 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
Russell King2f757f22008-12-08 16:33:30 +0000393core_initcall(rpc_dma_init);