blob: eeda808a732036a26b3ef2140d1d4750c72f36da [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;
Russell King090a37c2019-05-02 17:56:49 +010030 void __iomem *base; /* Controller base address */
Russell King308d3332009-02-21 21:36:22 +000031 int irq; /* Controller IRQ */
Russell King090a37c2019-05-02 17:56:49 +010032 unsigned int state;
Russell King81944682019-05-02 17:37:51 +010033 dma_addr_t cur_addr;
34 unsigned int cur_len;
Russell Kingfa4e9982009-02-21 21:38:56 +000035 dma_addr_t dma_addr;
36 unsigned int dma_len;
Russell King308d3332009-02-21 21:36:22 +000037};
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#if 0
40typedef enum {
41 dma_size_8 = 1,
42 dma_size_16 = 2,
43 dma_size_32 = 4,
44 dma_size_128 = 16
45} dma_size_t;
46#endif
47
48#define TRANSFER_SIZE 2
49
50#define CURA (0)
51#define ENDA (IOMD_IO0ENDA - IOMD_IO0CURA)
52#define CURB (IOMD_IO0CURB - IOMD_IO0CURA)
53#define ENDB (IOMD_IO0ENDB - IOMD_IO0CURA)
54#define CR (IOMD_IO0CR - IOMD_IO0CURA)
55#define ST (IOMD_IO0ST - IOMD_IO0CURA)
56
Russell King81944682019-05-02 17:37:51 +010057static void iomd_get_next_sg(struct iomd_dma *idma)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 unsigned long end, offset, flags = 0;
60
Russell Kingad9dd942008-12-08 17:35:48 +000061 if (idma->dma.sg) {
Russell King81944682019-05-02 17:37:51 +010062 idma->cur_addr = idma->dma_addr;
63 offset = idma->cur_addr & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Russell Kingfa4e9982009-02-21 21:38:56 +000065 end = offset + idma->dma_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 if (end > PAGE_SIZE)
68 end = PAGE_SIZE;
69
70 if (offset + TRANSFER_SIZE >= end)
71 flags |= DMA_END_L;
72
Russell King81944682019-05-02 17:37:51 +010073 idma->cur_len = end - TRANSFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Russell Kingfa4e9982009-02-21 21:38:56 +000075 idma->dma_len -= end - offset;
76 idma->dma_addr += end - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Russell Kingfa4e9982009-02-21 21:38:56 +000078 if (idma->dma_len == 0) {
Russell Kingad9dd942008-12-08 17:35:48 +000079 if (idma->dma.sgcount > 1) {
Russell King9e28d7e2008-12-08 19:03:58 +000080 idma->dma.sg = sg_next(idma->dma.sg);
Russell Kingfa4e9982009-02-21 21:38:56 +000081 idma->dma_addr = idma->dma.sg->dma_address;
82 idma->dma_len = idma->dma.sg->length;
Russell Kingad9dd942008-12-08 17:35:48 +000083 idma->dma.sgcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 } else {
Russell Kingad9dd942008-12-08 17:35:48 +000085 idma->dma.sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 flags |= DMA_END_S;
87 }
88 }
89 } else {
90 flags = DMA_END_S | DMA_END_L;
Russell King81944682019-05-02 17:37:51 +010091 idma->cur_addr = 0;
92 idma->cur_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
94
Russell King81944682019-05-02 17:37:51 +010095 idma->cur_len |= flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Linus Torvalds0cd61b62006-10-06 10:53:39 -070098static irqreturn_t iomd_dma_handle(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Russell Kingad9dd942008-12-08 17:35:48 +0000100 struct iomd_dma *idma = dev_id;
Russell King090a37c2019-05-02 17:56:49 +0100101 void __iomem *base = idma->base;
Russell King39694ed2019-05-02 17:43:36 +0100102 unsigned int state = idma->state;
Russell Kinge6595872019-05-02 17:50:55 +0100103 unsigned int status, cur, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 do {
Russell King090a37c2019-05-02 17:56:49 +0100106 status = readb(base + ST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 if (!(status & DMA_ST_INT))
Russell King39694ed2019-05-02 17:43:36 +0100108 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Russell King39694ed2019-05-02 17:43:36 +0100110 if ((state ^ status) & DMA_ST_AB)
Russell King81944682019-05-02 17:37:51 +0100111 iomd_get_next_sg(idma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Russell Kinge6595872019-05-02 17:50:55 +0100113 // This efficiently implements state = OFL != AB ? AB : 0
114 state = ((status >> 2) ^ status) & DMA_ST_AB;
115 if (state) {
116 cur = CURA;
117 end = ENDA;
118 } else {
119 cur = CURB;
120 end = ENDB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
Russell King090a37c2019-05-02 17:56:49 +0100122 writel(idma->cur_addr, base + cur);
123 writel(idma->cur_len, base + end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 if (status & DMA_ST_OFL &&
Russell King81944682019-05-02 17:37:51 +0100126 idma->cur_len == (DMA_END_S|DMA_END_L))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 break;
128 } while (1);
129
Russell King39694ed2019-05-02 17:43:36 +0100130 state = ~DMA_ST_AB;
Russell Kingffd9a1b2019-05-02 17:19:18 +0100131 disable_irq_nosync(irq);
Russell King39694ed2019-05-02 17:43:36 +0100132out:
133 idma->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return IRQ_HANDLED;
135}
136
Russell King1df81302008-12-08 15:58:50 +0000137static int iomd_request_dma(unsigned int chan, dma_t *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Russell Kingad9dd942008-12-08 17:35:48 +0000139 struct iomd_dma *idma = container_of(dma, struct iomd_dma, dma);
140
141 return request_irq(idma->irq, iomd_dma_handle,
Michael Opdenacker78f6db92014-03-04 22:04:50 +0100142 0, idma->dma.device_id, idma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
Russell King1df81302008-12-08 15:58:50 +0000145static void iomd_free_dma(unsigned int chan, dma_t *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Russell Kingad9dd942008-12-08 17:35:48 +0000147 struct iomd_dma *idma = container_of(dma, struct iomd_dma, dma);
148
149 free_irq(idma->irq, idma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
Russell King1df81302008-12-08 15:58:50 +0000152static void iomd_enable_dma(unsigned int chan, dma_t *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Russell Kingad9dd942008-12-08 17:35:48 +0000154 struct iomd_dma *idma = container_of(dma, struct iomd_dma, dma);
Russell King090a37c2019-05-02 17:56:49 +0100155 void __iomem *base = idma->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 unsigned int ctrl = TRANSFER_SIZE | DMA_CR_E;
157
Russell Kingad9dd942008-12-08 17:35:48 +0000158 if (idma->dma.invalid) {
159 idma->dma.invalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 /*
162 * Cope with ISA-style drivers which expect cache
163 * coherence.
164 */
Russell Kingad9dd942008-12-08 17:35:48 +0000165 if (!idma->dma.sg) {
166 idma->dma.sg = &idma->dma.buf;
167 idma->dma.sgcount = 1;
168 idma->dma.buf.length = idma->dma.count;
169 idma->dma.buf.dma_address = dma_map_single(NULL,
170 idma->dma.addr, idma->dma.count,
171 idma->dma.dma_mode == DMA_MODE_READ ?
Russell King7cdad482006-01-04 15:08:30 +0000172 DMA_FROM_DEVICE : DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
174
Russell Kingffd9a1b2019-05-02 17:19:18 +0100175 idma->dma_addr = idma->dma.sg->dma_address;
176 idma->dma_len = idma->dma.sg->length;
177
Russell King090a37c2019-05-02 17:56:49 +0100178 writeb(DMA_CR_C, base + CR);
Russell Kingad9dd942008-12-08 17:35:48 +0000179 idma->state = DMA_ST_AB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
Russell Kingad9dd942008-12-08 17:35:48 +0000181
182 if (idma->dma.dma_mode == DMA_MODE_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 ctrl |= DMA_CR_D;
184
Russell King090a37c2019-05-02 17:56:49 +0100185 writeb(ctrl, base + CR);
Russell Kingad9dd942008-12-08 17:35:48 +0000186 enable_irq(idma->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Russell King1df81302008-12-08 15:58:50 +0000189static void iomd_disable_dma(unsigned int chan, dma_t *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Russell Kingad9dd942008-12-08 17:35:48 +0000191 struct iomd_dma *idma = container_of(dma, struct iomd_dma, dma);
Russell King090a37c2019-05-02 17:56:49 +0100192 void __iomem *base = idma->base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 unsigned long flags;
194
195 local_irq_save(flags);
Russell Kingad9dd942008-12-08 17:35:48 +0000196 if (idma->state != ~DMA_ST_AB)
197 disable_irq(idma->irq);
Russell King090a37c2019-05-02 17:56:49 +0100198 writeb(0, base + CR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 local_irq_restore(flags);
200}
201
Russell King1df81302008-12-08 15:58:50 +0000202static int iomd_set_dma_speed(unsigned int chan, dma_t *dma, int cycle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 int tcr, speed;
205
206 if (cycle < 188)
207 speed = 3;
208 else if (cycle <= 250)
209 speed = 2;
210 else if (cycle < 438)
211 speed = 1;
212 else
213 speed = 0;
214
215 tcr = iomd_readb(IOMD_DMATCR);
216 speed &= 3;
217
Russell King1df81302008-12-08 15:58:50 +0000218 switch (chan) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 case DMA_0:
220 tcr = (tcr & ~0x03) | speed;
221 break;
222
223 case DMA_1:
224 tcr = (tcr & ~0x0c) | (speed << 2);
225 break;
226
227 case DMA_2:
228 tcr = (tcr & ~0x30) | (speed << 4);
229 break;
230
231 case DMA_3:
232 tcr = (tcr & ~0xc0) | (speed << 6);
233 break;
234
235 default:
236 break;
237 }
238
239 iomd_writeb(tcr, IOMD_DMATCR);
240
241 return speed;
242}
243
244static struct dma_ops iomd_dma_ops = {
245 .type = "IOMD",
246 .request = iomd_request_dma,
247 .free = iomd_free_dma,
248 .enable = iomd_enable_dma,
249 .disable = iomd_disable_dma,
250 .setspeed = iomd_set_dma_speed,
251};
252
253static struct fiq_handler fh = {
254 .name = "floppydma"
255};
256
Russell King308d3332009-02-21 21:36:22 +0000257struct floppy_dma {
258 struct dma_struct dma;
259 unsigned int fiq;
260};
261
Russell King1df81302008-12-08 15:58:50 +0000262static void floppy_enable_dma(unsigned int chan, dma_t *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Russell Kingad9dd942008-12-08 17:35:48 +0000264 struct floppy_dma *fdma = container_of(dma, struct floppy_dma, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 void *fiqhandler_start;
266 unsigned int fiqhandler_length;
267 struct pt_regs regs;
268
Russell Kingad9dd942008-12-08 17:35:48 +0000269 if (fdma->dma.sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 BUG();
271
Russell Kingad9dd942008-12-08 17:35:48 +0000272 if (fdma->dma.dma_mode == DMA_MODE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 extern unsigned char floppy_fiqin_start, floppy_fiqin_end;
274 fiqhandler_start = &floppy_fiqin_start;
275 fiqhandler_length = &floppy_fiqin_end - &floppy_fiqin_start;
276 } else {
277 extern unsigned char floppy_fiqout_start, floppy_fiqout_end;
278 fiqhandler_start = &floppy_fiqout_start;
279 fiqhandler_length = &floppy_fiqout_end - &floppy_fiqout_start;
280 }
281
Russell Kingad9dd942008-12-08 17:35:48 +0000282 regs.ARM_r9 = fdma->dma.count;
283 regs.ARM_r10 = (unsigned long)fdma->dma.addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 regs.ARM_fp = (unsigned long)FLOPPYDMA_BASE;
285
286 if (claim_fiq(&fh)) {
287 printk("floppydma: couldn't claim FIQ.\n");
288 return;
289 }
290
291 set_fiq_handler(fiqhandler_start, fiqhandler_length);
292 set_fiq_regs(&regs);
Russell Kingad9dd942008-12-08 17:35:48 +0000293 enable_fiq(fdma->fiq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Russell King1df81302008-12-08 15:58:50 +0000296static void floppy_disable_dma(unsigned int chan, dma_t *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Russell Kingad9dd942008-12-08 17:35:48 +0000298 struct floppy_dma *fdma = container_of(dma, struct floppy_dma, dma);
299 disable_fiq(fdma->fiq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 release_fiq(&fh);
301}
302
Russell King1df81302008-12-08 15:58:50 +0000303static int floppy_get_residue(unsigned int chan, dma_t *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 struct pt_regs regs;
306 get_fiq_regs(&regs);
307 return regs.ARM_r9;
308}
309
310static struct dma_ops floppy_dma_ops = {
311 .type = "FIQDMA",
312 .enable = floppy_enable_dma,
313 .disable = floppy_disable_dma,
314 .residue = floppy_get_residue,
315};
316
317/*
318 * This is virtual DMA - we don't need anything here.
319 */
Russell King1df81302008-12-08 15:58:50 +0000320static void sound_enable_disable_dma(unsigned int chan, dma_t *dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322}
323
324static struct dma_ops sound_dma_ops = {
325 .type = "VIRTUAL",
326 .enable = sound_enable_disable_dma,
327 .disable = sound_enable_disable_dma,
328};
329
Russell Kingad9dd942008-12-08 17:35:48 +0000330static struct iomd_dma iomd_dma[6];
Russell King2f757f22008-12-08 16:33:30 +0000331
Russell Kingad9dd942008-12-08 17:35:48 +0000332static struct floppy_dma floppy_dma = {
333 .dma = {
334 .d_ops = &floppy_dma_ops,
335 },
336 .fiq = FIQ_FLOPPYDATA,
Russell King2f757f22008-12-08 16:33:30 +0000337};
338
339static dma_t sound_dma = {
340 .d_ops = &sound_dma_ops,
341};
342
343static int __init rpc_dma_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Russell King2f757f22008-12-08 16:33:30 +0000345 unsigned int i;
346 int ret;
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 iomd_writeb(0, IOMD_IO0CR);
349 iomd_writeb(0, IOMD_IO1CR);
350 iomd_writeb(0, IOMD_IO2CR);
351 iomd_writeb(0, IOMD_IO3CR);
352
353 iomd_writeb(0xa0, IOMD_DMATCR);
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 /*
356 * Setup DMA channels 2,3 to be for podules
357 * and channels 0,1 for internal devices
358 */
359 iomd_writeb(DMA_EXT_IO3|DMA_EXT_IO2, IOMD_DMAEXT);
Russell King2f757f22008-12-08 16:33:30 +0000360
Russell King090a37c2019-05-02 17:56:49 +0100361 iomd_dma[DMA_0].base = IOMD_BASE + IOMD_IO0CURA;
Russell Kingad9dd942008-12-08 17:35:48 +0000362 iomd_dma[DMA_0].irq = IRQ_DMA0;
Russell King090a37c2019-05-02 17:56:49 +0100363 iomd_dma[DMA_1].base = IOMD_BASE + IOMD_IO1CURA;
Russell Kingad9dd942008-12-08 17:35:48 +0000364 iomd_dma[DMA_1].irq = IRQ_DMA1;
Russell King090a37c2019-05-02 17:56:49 +0100365 iomd_dma[DMA_2].base = IOMD_BASE + IOMD_IO2CURA;
Russell Kingad9dd942008-12-08 17:35:48 +0000366 iomd_dma[DMA_2].irq = IRQ_DMA2;
Russell King090a37c2019-05-02 17:56:49 +0100367 iomd_dma[DMA_3].base = IOMD_BASE + IOMD_IO3CURA;
Russell Kingad9dd942008-12-08 17:35:48 +0000368 iomd_dma[DMA_3].irq = IRQ_DMA3;
Russell King090a37c2019-05-02 17:56:49 +0100369 iomd_dma[DMA_S0].base = IOMD_BASE + IOMD_SD0CURA;
Russell Kingad9dd942008-12-08 17:35:48 +0000370 iomd_dma[DMA_S0].irq = IRQ_DMAS0;
Russell King090a37c2019-05-02 17:56:49 +0100371 iomd_dma[DMA_S1].base = IOMD_BASE + IOMD_SD1CURA;
Russell Kingad9dd942008-12-08 17:35:48 +0000372 iomd_dma[DMA_S1].irq = IRQ_DMAS1;
Russell King2f757f22008-12-08 16:33:30 +0000373
374 for (i = DMA_0; i <= DMA_S1; i++) {
Russell Kingad9dd942008-12-08 17:35:48 +0000375 iomd_dma[i].dma.d_ops = &iomd_dma_ops;
Russell King2f757f22008-12-08 16:33:30 +0000376
Russell Kingad9dd942008-12-08 17:35:48 +0000377 ret = isa_dma_add(i, &iomd_dma[i].dma);
Russell King2f757f22008-12-08 16:33:30 +0000378 if (ret)
379 printk("IOMDDMA%u: unable to register: %d\n", i, ret);
380 }
381
Russell Kingad9dd942008-12-08 17:35:48 +0000382 ret = isa_dma_add(DMA_VIRTUAL_FLOPPY, &floppy_dma.dma);
Russell King2f757f22008-12-08 16:33:30 +0000383 if (ret)
384 printk("IOMDFLOPPY: unable to register: %d\n", ret);
385 ret = isa_dma_add(DMA_VIRTUAL_SOUND, &sound_dma);
386 if (ret)
387 printk("IOMDSOUND: unable to register: %d\n", ret);
388 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
Russell King2f757f22008-12-08 16:33:30 +0000390core_initcall(rpc_dma_init);