dma.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 <linux/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_nosync(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. idma->dma_addr = idma->dma.sg->dma_address;
  146. idma->dma_len = idma->dma.sg->length;
  147. iomd_writeb(DMA_CR_C, dma_base + CR);
  148. idma->state = DMA_ST_AB;
  149. }
  150. if (idma->dma.dma_mode == DMA_MODE_READ)
  151. ctrl |= DMA_CR_D;
  152. iomd_writeb(ctrl, dma_base + CR);
  153. enable_irq(idma->irq);
  154. }
  155. static void iomd_disable_dma(unsigned int chan, dma_t *dma)
  156. {
  157. struct iomd_dma *idma = container_of(dma, struct iomd_dma, dma);
  158. unsigned long dma_base = idma->base;
  159. unsigned long flags;
  160. local_irq_save(flags);
  161. if (idma->state != ~DMA_ST_AB)
  162. disable_irq(idma->irq);
  163. iomd_writeb(0, dma_base + CR);
  164. local_irq_restore(flags);
  165. }
  166. static int iomd_set_dma_speed(unsigned int chan, dma_t *dma, int cycle)
  167. {
  168. int tcr, speed;
  169. if (cycle < 188)
  170. speed = 3;
  171. else if (cycle <= 250)
  172. speed = 2;
  173. else if (cycle < 438)
  174. speed = 1;
  175. else
  176. speed = 0;
  177. tcr = iomd_readb(IOMD_DMATCR);
  178. speed &= 3;
  179. switch (chan) {
  180. case DMA_0:
  181. tcr = (tcr & ~0x03) | speed;
  182. break;
  183. case DMA_1:
  184. tcr = (tcr & ~0x0c) | (speed << 2);
  185. break;
  186. case DMA_2:
  187. tcr = (tcr & ~0x30) | (speed << 4);
  188. break;
  189. case DMA_3:
  190. tcr = (tcr & ~0xc0) | (speed << 6);
  191. break;
  192. default:
  193. break;
  194. }
  195. iomd_writeb(tcr, IOMD_DMATCR);
  196. return speed;
  197. }
  198. static struct dma_ops iomd_dma_ops = {
  199. .type = "IOMD",
  200. .request = iomd_request_dma,
  201. .free = iomd_free_dma,
  202. .enable = iomd_enable_dma,
  203. .disable = iomd_disable_dma,
  204. .setspeed = iomd_set_dma_speed,
  205. };
  206. static struct fiq_handler fh = {
  207. .name = "floppydma"
  208. };
  209. struct floppy_dma {
  210. struct dma_struct dma;
  211. unsigned int fiq;
  212. };
  213. static void floppy_enable_dma(unsigned int chan, dma_t *dma)
  214. {
  215. struct floppy_dma *fdma = container_of(dma, struct floppy_dma, dma);
  216. void *fiqhandler_start;
  217. unsigned int fiqhandler_length;
  218. struct pt_regs regs;
  219. if (fdma->dma.sg)
  220. BUG();
  221. if (fdma->dma.dma_mode == DMA_MODE_READ) {
  222. extern unsigned char floppy_fiqin_start, floppy_fiqin_end;
  223. fiqhandler_start = &floppy_fiqin_start;
  224. fiqhandler_length = &floppy_fiqin_end - &floppy_fiqin_start;
  225. } else {
  226. extern unsigned char floppy_fiqout_start, floppy_fiqout_end;
  227. fiqhandler_start = &floppy_fiqout_start;
  228. fiqhandler_length = &floppy_fiqout_end - &floppy_fiqout_start;
  229. }
  230. regs.ARM_r9 = fdma->dma.count;
  231. regs.ARM_r10 = (unsigned long)fdma->dma.addr;
  232. regs.ARM_fp = (unsigned long)FLOPPYDMA_BASE;
  233. if (claim_fiq(&fh)) {
  234. printk("floppydma: couldn't claim FIQ.\n");
  235. return;
  236. }
  237. set_fiq_handler(fiqhandler_start, fiqhandler_length);
  238. set_fiq_regs(&regs);
  239. enable_fiq(fdma->fiq);
  240. }
  241. static void floppy_disable_dma(unsigned int chan, dma_t *dma)
  242. {
  243. struct floppy_dma *fdma = container_of(dma, struct floppy_dma, dma);
  244. disable_fiq(fdma->fiq);
  245. release_fiq(&fh);
  246. }
  247. static int floppy_get_residue(unsigned int chan, dma_t *dma)
  248. {
  249. struct pt_regs regs;
  250. get_fiq_regs(&regs);
  251. return regs.ARM_r9;
  252. }
  253. static struct dma_ops floppy_dma_ops = {
  254. .type = "FIQDMA",
  255. .enable = floppy_enable_dma,
  256. .disable = floppy_disable_dma,
  257. .residue = floppy_get_residue,
  258. };
  259. /*
  260. * This is virtual DMA - we don't need anything here.
  261. */
  262. static void sound_enable_disable_dma(unsigned int chan, dma_t *dma)
  263. {
  264. }
  265. static struct dma_ops sound_dma_ops = {
  266. .type = "VIRTUAL",
  267. .enable = sound_enable_disable_dma,
  268. .disable = sound_enable_disable_dma,
  269. };
  270. static struct iomd_dma iomd_dma[6];
  271. static struct floppy_dma floppy_dma = {
  272. .dma = {
  273. .d_ops = &floppy_dma_ops,
  274. },
  275. .fiq = FIQ_FLOPPYDATA,
  276. };
  277. static dma_t sound_dma = {
  278. .d_ops = &sound_dma_ops,
  279. };
  280. static int __init rpc_dma_init(void)
  281. {
  282. unsigned int i;
  283. int ret;
  284. iomd_writeb(0, IOMD_IO0CR);
  285. iomd_writeb(0, IOMD_IO1CR);
  286. iomd_writeb(0, IOMD_IO2CR);
  287. iomd_writeb(0, IOMD_IO3CR);
  288. iomd_writeb(0xa0, IOMD_DMATCR);
  289. /*
  290. * Setup DMA channels 2,3 to be for podules
  291. * and channels 0,1 for internal devices
  292. */
  293. iomd_writeb(DMA_EXT_IO3|DMA_EXT_IO2, IOMD_DMAEXT);
  294. iomd_dma[DMA_0].base = IOMD_IO0CURA;
  295. iomd_dma[DMA_0].irq = IRQ_DMA0;
  296. iomd_dma[DMA_1].base = IOMD_IO1CURA;
  297. iomd_dma[DMA_1].irq = IRQ_DMA1;
  298. iomd_dma[DMA_2].base = IOMD_IO2CURA;
  299. iomd_dma[DMA_2].irq = IRQ_DMA2;
  300. iomd_dma[DMA_3].base = IOMD_IO3CURA;
  301. iomd_dma[DMA_3].irq = IRQ_DMA3;
  302. iomd_dma[DMA_S0].base = IOMD_SD0CURA;
  303. iomd_dma[DMA_S0].irq = IRQ_DMAS0;
  304. iomd_dma[DMA_S1].base = IOMD_SD1CURA;
  305. iomd_dma[DMA_S1].irq = IRQ_DMAS1;
  306. for (i = DMA_0; i <= DMA_S1; i++) {
  307. iomd_dma[i].dma.d_ops = &iomd_dma_ops;
  308. ret = isa_dma_add(i, &iomd_dma[i].dma);
  309. if (ret)
  310. printk("IOMDDMA%u: unable to register: %d\n", i, ret);
  311. }
  312. ret = isa_dma_add(DMA_VIRTUAL_FLOPPY, &floppy_dma.dma);
  313. if (ret)
  314. printk("IOMDFLOPPY: unable to register: %d\n", ret);
  315. ret = isa_dma_add(DMA_VIRTUAL_SOUND, &sound_dma);
  316. if (ret)
  317. printk("IOMDSOUND: unable to register: %d\n", ret);
  318. return 0;
  319. }
  320. core_initcall(rpc_dma_init);