dma.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  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. */
  12. #include <linux/slab.h>
  13. #include <linux/mman.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/io.h>
  18. #include <asm/page.h>
  19. #include <asm/dma.h>
  20. #include <asm/fiq.h>
  21. #include <asm/irq.h>
  22. #include <mach/hardware.h>
  23. #include <asm/uaccess.h>
  24. #include <asm/mach/dma.h>
  25. #include <asm/hardware/iomd.h>
  26. struct iomd_dma {
  27. struct dma_struct dma;
  28. unsigned int state;
  29. unsigned long base; /* Controller base address */
  30. int irq; /* Controller IRQ */
  31. struct scatterlist cur_sg; /* Current controller buffer */
  32. dma_addr_t dma_addr;
  33. unsigned int dma_len;
  34. };
  35. #if 0
  36. typedef enum {
  37. dma_size_8 = 1,
  38. dma_size_16 = 2,
  39. dma_size_32 = 4,
  40. dma_size_128 = 16
  41. } dma_size_t;
  42. #endif
  43. #define TRANSFER_SIZE 2
  44. #define CURA (0)
  45. #define ENDA (IOMD_IO0ENDA - IOMD_IO0CURA)
  46. #define CURB (IOMD_IO0CURB - IOMD_IO0CURA)
  47. #define ENDB (IOMD_IO0ENDB - IOMD_IO0CURA)
  48. #define CR (IOMD_IO0CR - IOMD_IO0CURA)
  49. #define ST (IOMD_IO0ST - IOMD_IO0CURA)
  50. static void iomd_get_next_sg(struct scatterlist *sg, struct iomd_dma *idma)
  51. {
  52. unsigned long end, offset, flags = 0;
  53. if (idma->dma.sg) {
  54. sg->dma_address = idma->dma_addr;
  55. offset = sg->dma_address & ~PAGE_MASK;
  56. end = offset + idma->dma_len;
  57. if (end > PAGE_SIZE)
  58. end = PAGE_SIZE;
  59. if (offset + TRANSFER_SIZE >= end)
  60. flags |= DMA_END_L;
  61. sg->length = end - TRANSFER_SIZE;
  62. idma->dma_len -= end - offset;
  63. idma->dma_addr += end - offset;
  64. if (idma->dma_len == 0) {
  65. if (idma->dma.sgcount > 1) {
  66. idma->dma.sg = sg_next(idma->dma.sg);
  67. idma->dma_addr = idma->dma.sg->dma_address;
  68. idma->dma_len = idma->dma.sg->length;
  69. idma->dma.sgcount--;
  70. } else {
  71. idma->dma.sg = NULL;
  72. flags |= DMA_END_S;
  73. }
  74. }
  75. } else {
  76. flags = DMA_END_S | DMA_END_L;
  77. sg->dma_address = 0;
  78. sg->length = 0;
  79. }
  80. sg->length |= flags;
  81. }
  82. static irqreturn_t iomd_dma_handle(int irq, void *dev_id)
  83. {
  84. struct iomd_dma *idma = dev_id;
  85. unsigned long base = idma->base;
  86. do {
  87. unsigned int status;
  88. status = iomd_readb(base + ST);
  89. if (!(status & DMA_ST_INT))
  90. return IRQ_HANDLED;
  91. if ((idma->state ^ status) & DMA_ST_AB)
  92. iomd_get_next_sg(&idma->cur_sg, idma);
  93. switch (status & (DMA_ST_OFL | DMA_ST_AB)) {
  94. case DMA_ST_OFL: /* OIA */
  95. case DMA_ST_AB: /* .IB */
  96. iomd_writel(idma->cur_sg.dma_address, base + CURA);
  97. iomd_writel(idma->cur_sg.length, base + ENDA);
  98. idma->state = DMA_ST_AB;
  99. break;
  100. case DMA_ST_OFL | DMA_ST_AB: /* OIB */
  101. case 0: /* .IA */
  102. iomd_writel(idma->cur_sg.dma_address, base + CURB);
  103. iomd_writel(idma->cur_sg.length, base + ENDB);
  104. idma->state = 0;
  105. break;
  106. }
  107. if (status & DMA_ST_OFL &&
  108. idma->cur_sg.length == (DMA_END_S|DMA_END_L))
  109. break;
  110. } while (1);
  111. idma->state = ~DMA_ST_AB;
  112. disable_irq(irq);
  113. return IRQ_HANDLED;
  114. }
  115. static int iomd_request_dma(unsigned int chan, dma_t *dma)
  116. {
  117. struct iomd_dma *idma = container_of(dma, struct iomd_dma, dma);
  118. return request_irq(idma->irq, iomd_dma_handle,
  119. IRQF_DISABLED, idma->dma.device_id, idma);
  120. }
  121. static void iomd_free_dma(unsigned int chan, dma_t *dma)
  122. {
  123. struct iomd_dma *idma = container_of(dma, struct iomd_dma, dma);
  124. free_irq(idma->irq, idma);
  125. }
  126. static void iomd_enable_dma(unsigned int chan, dma_t *dma)
  127. {
  128. struct iomd_dma *idma = container_of(dma, struct iomd_dma, dma);
  129. unsigned long dma_base = idma->base;
  130. unsigned int ctrl = TRANSFER_SIZE | DMA_CR_E;
  131. if (idma->dma.invalid) {
  132. idma->dma.invalid = 0;
  133. /*
  134. * Cope with ISA-style drivers which expect cache
  135. * coherence.
  136. */
  137. if (!idma->dma.sg) {
  138. idma->dma.sg = &idma->dma.buf;
  139. idma->dma.sgcount = 1;
  140. idma->dma.buf.length = idma->dma.count;
  141. idma->dma.buf.dma_address = dma_map_single(NULL,
  142. idma->dma.addr, idma->dma.count,
  143. idma->dma.dma_mode == DMA_MODE_READ ?
  144. DMA_FROM_DEVICE : DMA_TO_DEVICE);
  145. }
  146. iomd_writeb(DMA_CR_C, dma_base + CR);
  147. idma->state = DMA_ST_AB;
  148. }
  149. if (idma->dma.dma_mode == DMA_MODE_READ)
  150. ctrl |= DMA_CR_D;
  151. iomd_writeb(ctrl, dma_base + CR);
  152. enable_irq(idma->irq);
  153. }
  154. static void iomd_disable_dma(unsigned int chan, dma_t *dma)
  155. {
  156. struct iomd_dma *idma = container_of(dma, struct iomd_dma, dma);
  157. unsigned long dma_base = idma->base;
  158. unsigned long flags;
  159. local_irq_save(flags);
  160. if (idma->state != ~DMA_ST_AB)
  161. disable_irq(idma->irq);
  162. iomd_writeb(0, dma_base + CR);
  163. local_irq_restore(flags);
  164. }
  165. static int iomd_set_dma_speed(unsigned int chan, dma_t *dma, int cycle)
  166. {
  167. int tcr, speed;
  168. if (cycle < 188)
  169. speed = 3;
  170. else if (cycle <= 250)
  171. speed = 2;
  172. else if (cycle < 438)
  173. speed = 1;
  174. else
  175. speed = 0;
  176. tcr = iomd_readb(IOMD_DMATCR);
  177. speed &= 3;
  178. switch (chan) {
  179. case DMA_0:
  180. tcr = (tcr & ~0x03) | speed;
  181. break;
  182. case DMA_1:
  183. tcr = (tcr & ~0x0c) | (speed << 2);
  184. break;
  185. case DMA_2:
  186. tcr = (tcr & ~0x30) | (speed << 4);
  187. break;
  188. case DMA_3:
  189. tcr = (tcr & ~0xc0) | (speed << 6);
  190. break;
  191. default:
  192. break;
  193. }
  194. iomd_writeb(tcr, IOMD_DMATCR);
  195. return speed;
  196. }
  197. static struct dma_ops iomd_dma_ops = {
  198. .type = "IOMD",
  199. .request = iomd_request_dma,
  200. .free = iomd_free_dma,
  201. .enable = iomd_enable_dma,
  202. .disable = iomd_disable_dma,
  203. .setspeed = iomd_set_dma_speed,
  204. };
  205. static struct fiq_handler fh = {
  206. .name = "floppydma"
  207. };
  208. struct floppy_dma {
  209. struct dma_struct dma;
  210. unsigned int fiq;
  211. };
  212. static void floppy_enable_dma(unsigned int chan, dma_t *dma)
  213. {
  214. struct floppy_dma *fdma = container_of(dma, struct floppy_dma, dma);
  215. void *fiqhandler_start;
  216. unsigned int fiqhandler_length;
  217. struct pt_regs regs;
  218. if (fdma->dma.sg)
  219. BUG();
  220. if (fdma->dma.dma_mode == DMA_MODE_READ) {
  221. extern unsigned char floppy_fiqin_start, floppy_fiqin_end;
  222. fiqhandler_start = &floppy_fiqin_start;
  223. fiqhandler_length = &floppy_fiqin_end - &floppy_fiqin_start;
  224. } else {
  225. extern unsigned char floppy_fiqout_start, floppy_fiqout_end;
  226. fiqhandler_start = &floppy_fiqout_start;
  227. fiqhandler_length = &floppy_fiqout_end - &floppy_fiqout_start;
  228. }
  229. regs.ARM_r9 = fdma->dma.count;
  230. regs.ARM_r10 = (unsigned long)fdma->dma.addr;
  231. regs.ARM_fp = (unsigned long)FLOPPYDMA_BASE;
  232. if (claim_fiq(&fh)) {
  233. printk("floppydma: couldn't claim FIQ.\n");
  234. return;
  235. }
  236. set_fiq_handler(fiqhandler_start, fiqhandler_length);
  237. set_fiq_regs(&regs);
  238. enable_fiq(fdma->fiq);
  239. }
  240. static void floppy_disable_dma(unsigned int chan, dma_t *dma)
  241. {
  242. struct floppy_dma *fdma = container_of(dma, struct floppy_dma, dma);
  243. disable_fiq(fdma->fiq);
  244. release_fiq(&fh);
  245. }
  246. static int floppy_get_residue(unsigned int chan, dma_t *dma)
  247. {
  248. struct pt_regs regs;
  249. get_fiq_regs(&regs);
  250. return regs.ARM_r9;
  251. }
  252. static struct dma_ops floppy_dma_ops = {
  253. .type = "FIQDMA",
  254. .enable = floppy_enable_dma,
  255. .disable = floppy_disable_dma,
  256. .residue = floppy_get_residue,
  257. };
  258. /*
  259. * This is virtual DMA - we don't need anything here.
  260. */
  261. static void sound_enable_disable_dma(unsigned int chan, dma_t *dma)
  262. {
  263. }
  264. static struct dma_ops sound_dma_ops = {
  265. .type = "VIRTUAL",
  266. .enable = sound_enable_disable_dma,
  267. .disable = sound_enable_disable_dma,
  268. };
  269. static struct iomd_dma iomd_dma[6];
  270. static struct floppy_dma floppy_dma = {
  271. .dma = {
  272. .d_ops = &floppy_dma_ops,
  273. },
  274. .fiq = FIQ_FLOPPYDATA,
  275. };
  276. static dma_t sound_dma = {
  277. .d_ops = &sound_dma_ops,
  278. };
  279. static int __init rpc_dma_init(void)
  280. {
  281. unsigned int i;
  282. int ret;
  283. iomd_writeb(0, IOMD_IO0CR);
  284. iomd_writeb(0, IOMD_IO1CR);
  285. iomd_writeb(0, IOMD_IO2CR);
  286. iomd_writeb(0, IOMD_IO3CR);
  287. iomd_writeb(0xa0, IOMD_DMATCR);
  288. /*
  289. * Setup DMA channels 2,3 to be for podules
  290. * and channels 0,1 for internal devices
  291. */
  292. iomd_writeb(DMA_EXT_IO3|DMA_EXT_IO2, IOMD_DMAEXT);
  293. iomd_dma[DMA_0].base = IOMD_IO0CURA;
  294. iomd_dma[DMA_0].irq = IRQ_DMA0;
  295. iomd_dma[DMA_1].base = IOMD_IO1CURA;
  296. iomd_dma[DMA_1].irq = IRQ_DMA1;
  297. iomd_dma[DMA_2].base = IOMD_IO2CURA;
  298. iomd_dma[DMA_2].irq = IRQ_DMA2;
  299. iomd_dma[DMA_3].base = IOMD_IO3CURA;
  300. iomd_dma[DMA_3].irq = IRQ_DMA3;
  301. iomd_dma[DMA_S0].base = IOMD_SD0CURA;
  302. iomd_dma[DMA_S0].irq = IRQ_DMAS0;
  303. iomd_dma[DMA_S1].base = IOMD_SD1CURA;
  304. iomd_dma[DMA_S1].irq = IRQ_DMAS1;
  305. for (i = DMA_0; i <= DMA_S1; i++) {
  306. iomd_dma[i].dma.d_ops = &iomd_dma_ops;
  307. ret = isa_dma_add(i, &iomd_dma[i].dma);
  308. if (ret)
  309. printk("IOMDDMA%u: unable to register: %d\n", i, ret);
  310. }
  311. ret = isa_dma_add(DMA_VIRTUAL_FLOPPY, &floppy_dma.dma);
  312. if (ret)
  313. printk("IOMDFLOPPY: unable to register: %d\n", ret);
  314. ret = isa_dma_add(DMA_VIRTUAL_SOUND, &sound_dma);
  315. if (ret)
  316. printk("IOMDSOUND: unable to register: %d\n", ret);
  317. return 0;
  318. }
  319. core_initcall(rpc_dma_init);