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