dma.c 16 KB

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