dma.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /*
  2. * linux/arch/arm/mach-imx/dma.c
  3. *
  4. * imx DMA registration and IRQ dispatching
  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. * 2004-03-03 Sascha Hauer <sascha@saschahauer.de>
  11. * initial version heavily inspired by
  12. * linux/arch/arm/mach-pxa/dma.c
  13. *
  14. * 2005-04-17 Pavel Pisa <pisa@cmp.felk.cvut.cz>
  15. * Changed to support scatter gather DMA
  16. * by taking Russell's code from RiscPC
  17. *
  18. */
  19. #undef DEBUG
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/kernel.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/errno.h>
  25. #include <asm/system.h>
  26. #include <asm/irq.h>
  27. #include <asm/hardware.h>
  28. #include <asm/dma.h>
  29. #include <asm/arch/imx-dma.h>
  30. struct imx_dma_channel imx_dma_channels[IMX_DMA_CHANNELS];
  31. /*
  32. * imx_dma_sg_next - prepare next chunk for scatter-gather DMA emulation
  33. * @dma_ch: i.MX DMA channel number
  34. * @lastcount: number of bytes transferred during last transfer
  35. *
  36. * Functions prepares DMA controller for next sg data chunk transfer.
  37. * The @lastcount argument informs function about number of bytes transferred
  38. * during last block. Zero value can be used for @lastcount to setup DMA
  39. * for the first chunk.
  40. */
  41. static inline int imx_dma_sg_next(imx_dmach_t dma_ch, unsigned int lastcount)
  42. {
  43. struct imx_dma_channel *imxdma = &imx_dma_channels[dma_ch];
  44. unsigned int nextcount;
  45. unsigned int nextaddr;
  46. if (!imxdma->name) {
  47. printk(KERN_CRIT "%s: called for not allocated channel %d\n",
  48. __FUNCTION__, dma_ch);
  49. return 0;
  50. }
  51. imxdma->resbytes -= lastcount;
  52. if (!imxdma->sg) {
  53. pr_debug("imxdma%d: no sg data\n", dma_ch);
  54. return 0;
  55. }
  56. imxdma->sgbc += lastcount;
  57. if ((imxdma->sgbc >= imxdma->sg->length) || !imxdma->resbytes) {
  58. if ((imxdma->sgcount <= 1) || !imxdma->resbytes) {
  59. pr_debug("imxdma%d: sg transfer limit reached\n",
  60. dma_ch);
  61. imxdma->sgcount=0;
  62. imxdma->sg = NULL;
  63. return 0;
  64. } else {
  65. imxdma->sgcount--;
  66. imxdma->sg++;
  67. imxdma->sgbc = 0;
  68. }
  69. }
  70. nextcount = imxdma->sg->length - imxdma->sgbc;
  71. nextaddr = imxdma->sg->dma_address + imxdma->sgbc;
  72. if(imxdma->resbytes < nextcount)
  73. nextcount = imxdma->resbytes;
  74. if ((imxdma->dma_mode & DMA_MODE_MASK) == DMA_MODE_READ)
  75. DAR(dma_ch) = nextaddr;
  76. else
  77. SAR(dma_ch) = nextaddr;
  78. CNTR(dma_ch) = nextcount;
  79. pr_debug("imxdma%d: next sg chunk dst 0x%08x, src 0x%08x, size 0x%08x\n",
  80. dma_ch, DAR(dma_ch), SAR(dma_ch), CNTR(dma_ch));
  81. return nextcount;
  82. }
  83. /*
  84. * imx_dma_setup_sg_base - scatter-gather DMA emulation
  85. * @dma_ch: i.MX DMA channel number
  86. * @sg: pointer to the scatter-gather list/vector
  87. * @sgcount: scatter-gather list hungs count
  88. *
  89. * Functions sets up i.MX DMA state for emulated scatter-gather transfer
  90. * and sets up channel registers to be ready for the first chunk
  91. */
  92. static int
  93. imx_dma_setup_sg_base(imx_dmach_t dma_ch,
  94. struct scatterlist *sg, unsigned int sgcount)
  95. {
  96. struct imx_dma_channel *imxdma = &imx_dma_channels[dma_ch];
  97. imxdma->sg = sg;
  98. imxdma->sgcount = sgcount;
  99. imxdma->sgbc = 0;
  100. return imx_dma_sg_next(dma_ch, 0);
  101. }
  102. /**
  103. * imx_dma_setup_single - setup i.MX DMA channel for linear memory to/from device transfer
  104. * @dma_ch: i.MX DMA channel number
  105. * @dma_address: the DMA/physical memory address of the linear data block
  106. * to transfer
  107. * @dma_length: length of the data block in bytes
  108. * @dev_addr: physical device port address
  109. * @dmamode: DMA transfer mode, %DMA_MODE_READ from the device to the memory
  110. * or %DMA_MODE_WRITE from memory to the device
  111. *
  112. * The function setups DMA channel source and destination addresses for transfer
  113. * specified by provided parameters. The scatter-gather emulation is disabled,
  114. * because linear data block
  115. * form the physical address range is transfered.
  116. * Return value: if incorrect parameters are provided -%EINVAL.
  117. * Zero indicates success.
  118. */
  119. int
  120. imx_dma_setup_single(imx_dmach_t dma_ch, dma_addr_t dma_address,
  121. unsigned int dma_length, unsigned int dev_addr,
  122. dmamode_t dmamode)
  123. {
  124. struct imx_dma_channel *imxdma = &imx_dma_channels[dma_ch];
  125. imxdma->sg = NULL;
  126. imxdma->sgcount = 0;
  127. imxdma->dma_mode = dmamode;
  128. imxdma->resbytes = dma_length;
  129. if (!dma_address) {
  130. printk(KERN_ERR "imxdma%d: imx_dma_setup_single null address\n",
  131. dma_ch);
  132. return -EINVAL;
  133. }
  134. if (!dma_length) {
  135. printk(KERN_ERR "imxdma%d: imx_dma_setup_single zero length\n",
  136. dma_ch);
  137. return -EINVAL;
  138. }
  139. if ((dmamode & DMA_MODE_MASK) == DMA_MODE_READ) {
  140. pr_debug("imxdma%d: mx_dma_setup_single2dev dma_addressg=0x%08x dma_length=%d dev_addr=0x%08x for read\n",
  141. dma_ch, (unsigned int)dma_address, dma_length,
  142. dev_addr);
  143. SAR(dma_ch) = dev_addr;
  144. DAR(dma_ch) = (unsigned int)dma_address;
  145. } else if ((dmamode & DMA_MODE_MASK) == DMA_MODE_WRITE) {
  146. pr_debug("imxdma%d: mx_dma_setup_single2dev dma_addressg=0x%08x dma_length=%d dev_addr=0x%08x for write\n",
  147. dma_ch, (unsigned int)dma_address, dma_length,
  148. dev_addr);
  149. SAR(dma_ch) = (unsigned int)dma_address;
  150. DAR(dma_ch) = dev_addr;
  151. } else {
  152. printk(KERN_ERR "imxdma%d: imx_dma_setup_single bad dmamode\n",
  153. dma_ch);
  154. return -EINVAL;
  155. }
  156. CNTR(dma_ch) = dma_length;
  157. return 0;
  158. }
  159. /**
  160. * imx_dma_setup_sg - setup i.MX DMA channel SG list to/from device transfer
  161. * @dma_ch: i.MX DMA channel number
  162. * @sg: pointer to the scatter-gather list/vector
  163. * @sgcount: scatter-gather list hungs count
  164. * @dma_length: total length of the transfer request in bytes
  165. * @dev_addr: physical device port address
  166. * @dmamode: DMA transfer mode, %DMA_MODE_READ from the device to the memory
  167. * or %DMA_MODE_WRITE from memory to the device
  168. *
  169. * The function setups DMA channel state and registers to be ready for transfer
  170. * specified by provided parameters. The scatter-gather emulation is set up
  171. * according to the parameters.
  172. *
  173. * The full preparation of the transfer requires setup of more register
  174. * by the caller before imx_dma_enable() can be called.
  175. *
  176. * %BLR(dma_ch) holds transfer burst length in bytes, 0 means 64 bytes
  177. *
  178. * %RSSR(dma_ch) has to be set to the DMA request line source %DMA_REQ_xxx
  179. *
  180. * %CCR(dma_ch) has to specify transfer parameters, the next settings is typical
  181. * for linear or simple scatter-gather transfers if %DMA_MODE_READ is specified
  182. *
  183. * %CCR_DMOD_LINEAR | %CCR_DSIZ_32 | %CCR_SMOD_FIFO | %CCR_SSIZ_x
  184. *
  185. * The typical setup for %DMA_MODE_WRITE is specified by next options combination
  186. *
  187. * %CCR_SMOD_LINEAR | %CCR_SSIZ_32 | %CCR_DMOD_FIFO | %CCR_DSIZ_x
  188. *
  189. * Be carefull there and do not mistakenly mix source and target device
  190. * port sizes constants, they are really different:
  191. * %CCR_SSIZ_8, %CCR_SSIZ_16, %CCR_SSIZ_32,
  192. * %CCR_DSIZ_8, %CCR_DSIZ_16, %CCR_DSIZ_32
  193. *
  194. * Return value: if incorrect parameters are provided -%EINVAL.
  195. * Zero indicates success.
  196. */
  197. int
  198. imx_dma_setup_sg(imx_dmach_t dma_ch,
  199. struct scatterlist *sg, unsigned int sgcount, unsigned int dma_length,
  200. unsigned int dev_addr, dmamode_t dmamode)
  201. {
  202. int res;
  203. struct imx_dma_channel *imxdma = &imx_dma_channels[dma_ch];
  204. imxdma->sg = NULL;
  205. imxdma->sgcount = 0;
  206. imxdma->dma_mode = dmamode;
  207. imxdma->resbytes = dma_length;
  208. if (!sg || !sgcount) {
  209. printk(KERN_ERR "imxdma%d: imx_dma_setup_sg epty sg list\n",
  210. dma_ch);
  211. return -EINVAL;
  212. }
  213. if (!sg->length) {
  214. printk(KERN_ERR "imxdma%d: imx_dma_setup_sg zero length\n",
  215. dma_ch);
  216. return -EINVAL;
  217. }
  218. if ((dmamode & DMA_MODE_MASK) == DMA_MODE_READ) {
  219. pr_debug("imxdma%d: mx_dma_setup_sg2dev sg=%p sgcount=%d total length=%d dev_addr=0x%08x for read\n",
  220. dma_ch, sg, sgcount, dma_length, dev_addr);
  221. SAR(dma_ch) = dev_addr;
  222. } else if ((dmamode & DMA_MODE_MASK) == DMA_MODE_WRITE) {
  223. pr_debug("imxdma%d: mx_dma_setup_sg2dev sg=%p sgcount=%d total length=%d dev_addr=0x%08x for write\n",
  224. dma_ch, sg, sgcount, dma_length, dev_addr);
  225. DAR(dma_ch) = dev_addr;
  226. } else {
  227. printk(KERN_ERR "imxdma%d: imx_dma_setup_sg bad dmamode\n",
  228. dma_ch);
  229. return -EINVAL;
  230. }
  231. res = imx_dma_setup_sg_base(dma_ch, sg, sgcount);
  232. if (res <= 0) {
  233. printk(KERN_ERR "imxdma%d: no sg chunk ready\n", dma_ch);
  234. return -EINVAL;
  235. }
  236. return 0;
  237. }
  238. /**
  239. * imx_dma_setup_handlers - setup i.MX DMA channel end and error notification handlers
  240. * @dma_ch: i.MX DMA channel number
  241. * @irq_handler: the pointer to the function called if the transfer
  242. * ends successfully
  243. * @err_handler: the pointer to the function called if the premature
  244. * end caused by error occurs
  245. * @data: user specified value to be passed to the handlers
  246. */
  247. int
  248. imx_dma_setup_handlers(imx_dmach_t dma_ch,
  249. void (*irq_handler) (int, void *, struct pt_regs *),
  250. void (*err_handler) (int, void *, struct pt_regs *),
  251. void *data)
  252. {
  253. struct imx_dma_channel *imxdma = &imx_dma_channels[dma_ch];
  254. unsigned long flags;
  255. if (!imxdma->name) {
  256. printk(KERN_CRIT "%s: called for not allocated channel %d\n",
  257. __FUNCTION__, dma_ch);
  258. return -ENODEV;
  259. }
  260. local_irq_save(flags);
  261. DISR = (1 << dma_ch);
  262. imxdma->irq_handler = irq_handler;
  263. imxdma->err_handler = err_handler;
  264. imxdma->data = data;
  265. local_irq_restore(flags);
  266. return 0;
  267. }
  268. /**
  269. * imx_dma_enable - function to start i.MX DMA channel operation
  270. * @dma_ch: i.MX DMA channel number
  271. *
  272. * The channel has to be allocated by driver through imx_dma_request()
  273. * or imx_dma_request_by_prio() function.
  274. * The transfer parameters has to be set to the channel registers through
  275. * call of the imx_dma_setup_single() or imx_dma_setup_sg() function
  276. * and registers %BLR(dma_ch), %RSSR(dma_ch) and %CCR(dma_ch) has to
  277. * be set prior this function call by the channel user.
  278. */
  279. void imx_dma_enable(imx_dmach_t dma_ch)
  280. {
  281. struct imx_dma_channel *imxdma = &imx_dma_channels[dma_ch];
  282. unsigned long flags;
  283. pr_debug("imxdma%d: imx_dma_enable\n", dma_ch);
  284. if (!imxdma->name) {
  285. printk(KERN_CRIT "%s: called for not allocated channel %d\n",
  286. __FUNCTION__, dma_ch);
  287. return;
  288. }
  289. local_irq_save(flags);
  290. DISR = (1 << dma_ch);
  291. DIMR &= ~(1 << dma_ch);
  292. CCR(dma_ch) |= CCR_CEN;
  293. local_irq_restore(flags);
  294. }
  295. /**
  296. * imx_dma_disable - stop, finish i.MX DMA channel operatin
  297. * @dma_ch: i.MX DMA channel number
  298. */
  299. void imx_dma_disable(imx_dmach_t dma_ch)
  300. {
  301. unsigned long flags;
  302. pr_debug("imxdma%d: imx_dma_disable\n", dma_ch);
  303. local_irq_save(flags);
  304. DIMR |= (1 << dma_ch);
  305. CCR(dma_ch) &= ~CCR_CEN;
  306. DISR = (1 << dma_ch);
  307. local_irq_restore(flags);
  308. }
  309. /**
  310. * imx_dma_request - request/allocate specified channel number
  311. * @dma_ch: i.MX DMA channel number
  312. * @name: the driver/caller own non-%NULL identification
  313. */
  314. int imx_dma_request(imx_dmach_t dma_ch, const char *name)
  315. {
  316. struct imx_dma_channel *imxdma = &imx_dma_channels[dma_ch];
  317. unsigned long flags;
  318. /* basic sanity checks */
  319. if (!name)
  320. return -EINVAL;
  321. if (dma_ch >= IMX_DMA_CHANNELS) {
  322. printk(KERN_CRIT "%s: called for non-existed channel %d\n",
  323. __FUNCTION__, dma_ch);
  324. return -EINVAL;
  325. }
  326. local_irq_save(flags);
  327. if (imxdma->name) {
  328. local_irq_restore(flags);
  329. return -ENODEV;
  330. }
  331. imxdma->name = name;
  332. imxdma->irq_handler = NULL;
  333. imxdma->err_handler = NULL;
  334. imxdma->data = NULL;
  335. imxdma->sg = NULL;
  336. local_irq_restore(flags);
  337. return 0;
  338. }
  339. /**
  340. * imx_dma_free - release previously acquired channel
  341. * @dma_ch: i.MX DMA channel number
  342. */
  343. void imx_dma_free(imx_dmach_t dma_ch)
  344. {
  345. unsigned long flags;
  346. struct imx_dma_channel *imxdma = &imx_dma_channels[dma_ch];
  347. if (!imxdma->name) {
  348. printk(KERN_CRIT
  349. "%s: trying to free channel %d which is already freed\n",
  350. __FUNCTION__, dma_ch);
  351. return;
  352. }
  353. local_irq_save(flags);
  354. /* Disable interrupts */
  355. DIMR |= (1 << dma_ch);
  356. CCR(dma_ch) &= ~CCR_CEN;
  357. imxdma->name = NULL;
  358. local_irq_restore(flags);
  359. }
  360. /**
  361. * imx_dma_request_by_prio - find and request some of free channels best suiting requested priority
  362. * @dma_ch: i.MX DMA channel number
  363. * @name: the driver/caller own non-%NULL identification
  364. * @prio: one of the hardware distinguished priority level:
  365. * %DMA_PRIO_HIGH, %DMA_PRIO_MEDIUM, %DMA_PRIO_LOW
  366. *
  367. * This function tries to find free channel in the specified priority group
  368. * if the priority cannot be achieved it tries to look for free channel
  369. * in the higher and then even lower priority groups.
  370. *
  371. * Return value: If there is no free channel to allocate, -%ENODEV is returned.
  372. * Zero value indicates successful channel allocation.
  373. */
  374. int
  375. imx_dma_request_by_prio(imx_dmach_t * pdma_ch, const char *name,
  376. imx_dma_prio prio)
  377. {
  378. int i;
  379. int best;
  380. switch (prio) {
  381. case (DMA_PRIO_HIGH):
  382. best = 8;
  383. break;
  384. case (DMA_PRIO_MEDIUM):
  385. best = 4;
  386. break;
  387. case (DMA_PRIO_LOW):
  388. default:
  389. best = 0;
  390. break;
  391. }
  392. for (i = best; i < IMX_DMA_CHANNELS; i++) {
  393. if (!imx_dma_request(i, name)) {
  394. *pdma_ch = i;
  395. return 0;
  396. }
  397. }
  398. for (i = best - 1; i >= 0; i--) {
  399. if (!imx_dma_request(i, name)) {
  400. *pdma_ch = i;
  401. return 0;
  402. }
  403. }
  404. printk(KERN_ERR "%s: no free DMA channel found\n", __FUNCTION__);
  405. return -ENODEV;
  406. }
  407. static irqreturn_t dma_err_handler(int irq, void *dev_id, struct pt_regs *regs)
  408. {
  409. int i, disr = DISR;
  410. struct imx_dma_channel *channel;
  411. unsigned int err_mask = DBTOSR | DRTOSR | DSESR | DBOSR;
  412. DISR = disr;
  413. for (i = 0; i < IMX_DMA_CHANNELS; i++) {
  414. channel = &imx_dma_channels[i];
  415. if ((err_mask & 1 << i) && channel->name
  416. && channel->err_handler) {
  417. channel->err_handler(i, channel->data, regs);
  418. continue;
  419. }
  420. imx_dma_channels[i].sg = NULL;
  421. if (DBTOSR & (1 << i)) {
  422. printk(KERN_WARNING
  423. "Burst timeout on channel %d (%s)\n",
  424. i, channel->name);
  425. DBTOSR |= (1 << i);
  426. }
  427. if (DRTOSR & (1 << i)) {
  428. printk(KERN_WARNING
  429. "Request timeout on channel %d (%s)\n",
  430. i, channel->name);
  431. DRTOSR |= (1 << i);
  432. }
  433. if (DSESR & (1 << i)) {
  434. printk(KERN_WARNING
  435. "Transfer timeout on channel %d (%s)\n",
  436. i, channel->name);
  437. DSESR |= (1 << i);
  438. }
  439. if (DBOSR & (1 << i)) {
  440. printk(KERN_WARNING
  441. "Buffer overflow timeout on channel %d (%s)\n",
  442. i, channel->name);
  443. DBOSR |= (1 << i);
  444. }
  445. }
  446. return IRQ_HANDLED;
  447. }
  448. static irqreturn_t dma_irq_handler(int irq, void *dev_id, struct pt_regs *regs)
  449. {
  450. int i, disr = DISR;
  451. pr_debug("imxdma: dma_irq_handler called, disr=0x%08x\n",
  452. disr);
  453. DISR = disr;
  454. for (i = 0; i < IMX_DMA_CHANNELS; i++) {
  455. if (disr & (1 << i)) {
  456. struct imx_dma_channel *channel = &imx_dma_channels[i];
  457. if (channel->name) {
  458. if (imx_dma_sg_next(i, CNTR(i))) {
  459. CCR(i) &= ~CCR_CEN;
  460. mb();
  461. CCR(i) |= CCR_CEN;
  462. } else {
  463. if (channel->irq_handler)
  464. channel->irq_handler(i,
  465. channel->data, regs);
  466. }
  467. } else {
  468. /*
  469. * IRQ for an unregistered DMA channel:
  470. * let's clear the interrupts and disable it.
  471. */
  472. printk(KERN_WARNING
  473. "spurious IRQ for DMA channel %d\n", i);
  474. }
  475. }
  476. }
  477. return IRQ_HANDLED;
  478. }
  479. static int __init imx_dma_init(void)
  480. {
  481. int ret;
  482. int i;
  483. /* reset DMA module */
  484. DCR = DCR_DRST;
  485. ret = request_irq(DMA_INT, dma_irq_handler, 0, "DMA", NULL);
  486. if (ret) {
  487. printk(KERN_CRIT "Wow! Can't register IRQ for DMA\n");
  488. return ret;
  489. }
  490. ret = request_irq(DMA_ERR, dma_err_handler, 0, "DMA", NULL);
  491. if (ret) {
  492. printk(KERN_CRIT "Wow! Can't register ERRIRQ for DMA\n");
  493. free_irq(DMA_INT, NULL);
  494. }
  495. /* enable DMA module */
  496. DCR = DCR_DEN;
  497. /* clear all interrupts */
  498. DISR = (1 << IMX_DMA_CHANNELS) - 1;
  499. /* enable interrupts */
  500. DIMR = (1 << IMX_DMA_CHANNELS) - 1;
  501. for (i = 0; i < IMX_DMA_CHANNELS; i++) {
  502. imx_dma_channels[i].sg = NULL;
  503. imx_dma_channels[i].dma_num = i;
  504. }
  505. return ret;
  506. }
  507. arch_initcall(imx_dma_init);
  508. EXPORT_SYMBOL(imx_dma_setup_single);
  509. EXPORT_SYMBOL(imx_dma_setup_sg);
  510. EXPORT_SYMBOL(imx_dma_setup_handlers);
  511. EXPORT_SYMBOL(imx_dma_enable);
  512. EXPORT_SYMBOL(imx_dma_disable);
  513. EXPORT_SYMBOL(imx_dma_request);
  514. EXPORT_SYMBOL(imx_dma_free);
  515. EXPORT_SYMBOL(imx_dma_request_by_prio);
  516. EXPORT_SYMBOL(imx_dma_channels);