fsldma.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. /*
  2. * Freescale MPC85xx, MPC83xx DMA Engine support
  3. *
  4. * Copyright (C) 2007 Freescale Semiconductor, Inc. All rights reserved.
  5. *
  6. * Author:
  7. * Zhang Wei <wei.zhang@freescale.com>, Jul 2007
  8. * Ebony Zhu <ebony.zhu@freescale.com>, May 2007
  9. *
  10. * Description:
  11. * DMA engine driver for Freescale MPC8540 DMA controller, which is
  12. * also fit for MPC8560, MPC8555, MPC8548, MPC8641, and etc.
  13. * The support for MPC8349 DMA contorller is also added.
  14. *
  15. * This is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. */
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/pci.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/dmaengine.h>
  26. #include <linux/delay.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/dmapool.h>
  29. #include <linux/of_platform.h>
  30. #include "fsldma.h"
  31. static void dma_init(struct fsl_dma_chan *fsl_chan)
  32. {
  33. /* Reset the channel */
  34. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr, 0, 32);
  35. switch (fsl_chan->feature & FSL_DMA_IP_MASK) {
  36. case FSL_DMA_IP_85XX:
  37. /* Set the channel to below modes:
  38. * EIE - Error interrupt enable
  39. * EOSIE - End of segments interrupt enable (basic mode)
  40. * EOLNIE - End of links interrupt enable
  41. */
  42. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr, FSL_DMA_MR_EIE
  43. | FSL_DMA_MR_EOLNIE | FSL_DMA_MR_EOSIE, 32);
  44. break;
  45. case FSL_DMA_IP_83XX:
  46. /* Set the channel to below modes:
  47. * EOTIE - End-of-transfer interrupt enable
  48. */
  49. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr, FSL_DMA_MR_EOTIE,
  50. 32);
  51. break;
  52. }
  53. }
  54. static void set_sr(struct fsl_dma_chan *fsl_chan, u32 val)
  55. {
  56. DMA_OUT(fsl_chan, &fsl_chan->reg_base->sr, val, 32);
  57. }
  58. static u32 get_sr(struct fsl_dma_chan *fsl_chan)
  59. {
  60. return DMA_IN(fsl_chan, &fsl_chan->reg_base->sr, 32);
  61. }
  62. static void set_desc_cnt(struct fsl_dma_chan *fsl_chan,
  63. struct fsl_dma_ld_hw *hw, u32 count)
  64. {
  65. hw->count = CPU_TO_DMA(fsl_chan, count, 32);
  66. }
  67. static void set_desc_src(struct fsl_dma_chan *fsl_chan,
  68. struct fsl_dma_ld_hw *hw, dma_addr_t src)
  69. {
  70. u64 snoop_bits;
  71. snoop_bits = ((fsl_chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_85XX)
  72. ? ((u64)FSL_DMA_SATR_SREADTYPE_SNOOP_READ << 32) : 0;
  73. hw->src_addr = CPU_TO_DMA(fsl_chan, snoop_bits | src, 64);
  74. }
  75. static void set_desc_dest(struct fsl_dma_chan *fsl_chan,
  76. struct fsl_dma_ld_hw *hw, dma_addr_t dest)
  77. {
  78. u64 snoop_bits;
  79. snoop_bits = ((fsl_chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_85XX)
  80. ? ((u64)FSL_DMA_DATR_DWRITETYPE_SNOOP_WRITE << 32) : 0;
  81. hw->dst_addr = CPU_TO_DMA(fsl_chan, snoop_bits | dest, 64);
  82. }
  83. static void set_desc_next(struct fsl_dma_chan *fsl_chan,
  84. struct fsl_dma_ld_hw *hw, dma_addr_t next)
  85. {
  86. u64 snoop_bits;
  87. snoop_bits = ((fsl_chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_83XX)
  88. ? FSL_DMA_SNEN : 0;
  89. hw->next_ln_addr = CPU_TO_DMA(fsl_chan, snoop_bits | next, 64);
  90. }
  91. static void set_cdar(struct fsl_dma_chan *fsl_chan, dma_addr_t addr)
  92. {
  93. DMA_OUT(fsl_chan, &fsl_chan->reg_base->cdar, addr | FSL_DMA_SNEN, 64);
  94. }
  95. static dma_addr_t get_cdar(struct fsl_dma_chan *fsl_chan)
  96. {
  97. return DMA_IN(fsl_chan, &fsl_chan->reg_base->cdar, 64) & ~FSL_DMA_SNEN;
  98. }
  99. static void set_ndar(struct fsl_dma_chan *fsl_chan, dma_addr_t addr)
  100. {
  101. DMA_OUT(fsl_chan, &fsl_chan->reg_base->ndar, addr, 64);
  102. }
  103. static dma_addr_t get_ndar(struct fsl_dma_chan *fsl_chan)
  104. {
  105. return DMA_IN(fsl_chan, &fsl_chan->reg_base->ndar, 64);
  106. }
  107. static u32 get_bcr(struct fsl_dma_chan *fsl_chan)
  108. {
  109. return DMA_IN(fsl_chan, &fsl_chan->reg_base->bcr, 32);
  110. }
  111. static int dma_is_idle(struct fsl_dma_chan *fsl_chan)
  112. {
  113. u32 sr = get_sr(fsl_chan);
  114. return (!(sr & FSL_DMA_SR_CB)) || (sr & FSL_DMA_SR_CH);
  115. }
  116. static void dma_start(struct fsl_dma_chan *fsl_chan)
  117. {
  118. u32 mr_set = 0;;
  119. if (fsl_chan->feature & FSL_DMA_CHAN_PAUSE_EXT) {
  120. DMA_OUT(fsl_chan, &fsl_chan->reg_base->bcr, 0, 32);
  121. mr_set |= FSL_DMA_MR_EMP_EN;
  122. } else
  123. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr,
  124. DMA_IN(fsl_chan, &fsl_chan->reg_base->mr, 32)
  125. & ~FSL_DMA_MR_EMP_EN, 32);
  126. if (fsl_chan->feature & FSL_DMA_CHAN_START_EXT)
  127. mr_set |= FSL_DMA_MR_EMS_EN;
  128. else
  129. mr_set |= FSL_DMA_MR_CS;
  130. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr,
  131. DMA_IN(fsl_chan, &fsl_chan->reg_base->mr, 32)
  132. | mr_set, 32);
  133. }
  134. static void dma_halt(struct fsl_dma_chan *fsl_chan)
  135. {
  136. int i;
  137. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr,
  138. DMA_IN(fsl_chan, &fsl_chan->reg_base->mr, 32) | FSL_DMA_MR_CA,
  139. 32);
  140. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr,
  141. DMA_IN(fsl_chan, &fsl_chan->reg_base->mr, 32) & ~(FSL_DMA_MR_CS
  142. | FSL_DMA_MR_EMS_EN | FSL_DMA_MR_CA), 32);
  143. for (i = 0; i < 100; i++) {
  144. if (dma_is_idle(fsl_chan))
  145. break;
  146. udelay(10);
  147. }
  148. if (i >= 100 && !dma_is_idle(fsl_chan))
  149. dev_err(fsl_chan->dev, "DMA halt timeout!\n");
  150. }
  151. static void set_ld_eol(struct fsl_dma_chan *fsl_chan,
  152. struct fsl_desc_sw *desc)
  153. {
  154. u64 snoop_bits;
  155. snoop_bits = ((fsl_chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_83XX)
  156. ? FSL_DMA_SNEN : 0;
  157. desc->hw.next_ln_addr = CPU_TO_DMA(fsl_chan,
  158. DMA_TO_CPU(fsl_chan, desc->hw.next_ln_addr, 64) | FSL_DMA_EOL
  159. | snoop_bits, 64);
  160. }
  161. static void append_ld_queue(struct fsl_dma_chan *fsl_chan,
  162. struct fsl_desc_sw *new_desc)
  163. {
  164. struct fsl_desc_sw *queue_tail = to_fsl_desc(fsl_chan->ld_queue.prev);
  165. if (list_empty(&fsl_chan->ld_queue))
  166. return;
  167. /* Link to the new descriptor physical address and
  168. * Enable End-of-segment interrupt for
  169. * the last link descriptor.
  170. * (the previous node's next link descriptor)
  171. *
  172. * For FSL_DMA_IP_83xx, the snoop enable bit need be set.
  173. */
  174. queue_tail->hw.next_ln_addr = CPU_TO_DMA(fsl_chan,
  175. new_desc->async_tx.phys | FSL_DMA_EOSIE |
  176. (((fsl_chan->feature & FSL_DMA_IP_MASK)
  177. == FSL_DMA_IP_83XX) ? FSL_DMA_SNEN : 0), 64);
  178. }
  179. /**
  180. * fsl_chan_set_src_loop_size - Set source address hold transfer size
  181. * @fsl_chan : Freescale DMA channel
  182. * @size : Address loop size, 0 for disable loop
  183. *
  184. * The set source address hold transfer size. The source
  185. * address hold or loop transfer size is when the DMA transfer
  186. * data from source address (SA), if the loop size is 4, the DMA will
  187. * read data from SA, SA + 1, SA + 2, SA + 3, then loop back to SA,
  188. * SA + 1 ... and so on.
  189. */
  190. static void fsl_chan_set_src_loop_size(struct fsl_dma_chan *fsl_chan, int size)
  191. {
  192. switch (size) {
  193. case 0:
  194. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr,
  195. DMA_IN(fsl_chan, &fsl_chan->reg_base->mr, 32) &
  196. (~FSL_DMA_MR_SAHE), 32);
  197. break;
  198. case 1:
  199. case 2:
  200. case 4:
  201. case 8:
  202. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr,
  203. DMA_IN(fsl_chan, &fsl_chan->reg_base->mr, 32) |
  204. FSL_DMA_MR_SAHE | (__ilog2(size) << 14),
  205. 32);
  206. break;
  207. }
  208. }
  209. /**
  210. * fsl_chan_set_dest_loop_size - Set destination address hold transfer size
  211. * @fsl_chan : Freescale DMA channel
  212. * @size : Address loop size, 0 for disable loop
  213. *
  214. * The set destination address hold transfer size. The destination
  215. * address hold or loop transfer size is when the DMA transfer
  216. * data to destination address (TA), if the loop size is 4, the DMA will
  217. * write data to TA, TA + 1, TA + 2, TA + 3, then loop back to TA,
  218. * TA + 1 ... and so on.
  219. */
  220. static void fsl_chan_set_dest_loop_size(struct fsl_dma_chan *fsl_chan, int size)
  221. {
  222. switch (size) {
  223. case 0:
  224. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr,
  225. DMA_IN(fsl_chan, &fsl_chan->reg_base->mr, 32) &
  226. (~FSL_DMA_MR_DAHE), 32);
  227. break;
  228. case 1:
  229. case 2:
  230. case 4:
  231. case 8:
  232. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr,
  233. DMA_IN(fsl_chan, &fsl_chan->reg_base->mr, 32) |
  234. FSL_DMA_MR_DAHE | (__ilog2(size) << 16),
  235. 32);
  236. break;
  237. }
  238. }
  239. /**
  240. * fsl_chan_toggle_ext_pause - Toggle channel external pause status
  241. * @fsl_chan : Freescale DMA channel
  242. * @size : Pause control size, 0 for disable external pause control.
  243. * The maximum is 1024.
  244. *
  245. * The Freescale DMA channel can be controlled by the external
  246. * signal DREQ#. The pause control size is how many bytes are allowed
  247. * to transfer before pausing the channel, after which a new assertion
  248. * of DREQ# resumes channel operation.
  249. */
  250. static void fsl_chan_toggle_ext_pause(struct fsl_dma_chan *fsl_chan, int size)
  251. {
  252. if (size > 1024)
  253. return;
  254. if (size) {
  255. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr,
  256. DMA_IN(fsl_chan, &fsl_chan->reg_base->mr, 32)
  257. | ((__ilog2(size) << 24) & 0x0f000000),
  258. 32);
  259. fsl_chan->feature |= FSL_DMA_CHAN_PAUSE_EXT;
  260. } else
  261. fsl_chan->feature &= ~FSL_DMA_CHAN_PAUSE_EXT;
  262. }
  263. /**
  264. * fsl_chan_toggle_ext_start - Toggle channel external start status
  265. * @fsl_chan : Freescale DMA channel
  266. * @enable : 0 is disabled, 1 is enabled.
  267. *
  268. * If enable the external start, the channel can be started by an
  269. * external DMA start pin. So the dma_start() does not start the
  270. * transfer immediately. The DMA channel will wait for the
  271. * control pin asserted.
  272. */
  273. static void fsl_chan_toggle_ext_start(struct fsl_dma_chan *fsl_chan, int enable)
  274. {
  275. if (enable)
  276. fsl_chan->feature |= FSL_DMA_CHAN_START_EXT;
  277. else
  278. fsl_chan->feature &= ~FSL_DMA_CHAN_START_EXT;
  279. }
  280. static dma_cookie_t fsl_dma_tx_submit(struct dma_async_tx_descriptor *tx)
  281. {
  282. struct fsl_dma_chan *fsl_chan = to_fsl_chan(tx->chan);
  283. struct fsl_desc_sw *desc;
  284. unsigned long flags;
  285. dma_cookie_t cookie;
  286. /* cookie increment and adding to ld_queue must be atomic */
  287. spin_lock_irqsave(&fsl_chan->desc_lock, flags);
  288. cookie = fsl_chan->common.cookie;
  289. list_for_each_entry(desc, &tx->tx_list, node) {
  290. cookie++;
  291. if (cookie < 0)
  292. cookie = 1;
  293. desc->async_tx.cookie = cookie;
  294. }
  295. fsl_chan->common.cookie = cookie;
  296. append_ld_queue(fsl_chan, tx_to_fsl_desc(tx));
  297. list_splice_init(&tx->tx_list, fsl_chan->ld_queue.prev);
  298. spin_unlock_irqrestore(&fsl_chan->desc_lock, flags);
  299. return cookie;
  300. }
  301. /**
  302. * fsl_dma_alloc_descriptor - Allocate descriptor from channel's DMA pool.
  303. * @fsl_chan : Freescale DMA channel
  304. *
  305. * Return - The descriptor allocated. NULL for failed.
  306. */
  307. static struct fsl_desc_sw *fsl_dma_alloc_descriptor(
  308. struct fsl_dma_chan *fsl_chan)
  309. {
  310. dma_addr_t pdesc;
  311. struct fsl_desc_sw *desc_sw;
  312. desc_sw = dma_pool_alloc(fsl_chan->desc_pool, GFP_ATOMIC, &pdesc);
  313. if (desc_sw) {
  314. memset(desc_sw, 0, sizeof(struct fsl_desc_sw));
  315. dma_async_tx_descriptor_init(&desc_sw->async_tx,
  316. &fsl_chan->common);
  317. desc_sw->async_tx.tx_submit = fsl_dma_tx_submit;
  318. desc_sw->async_tx.phys = pdesc;
  319. }
  320. return desc_sw;
  321. }
  322. /**
  323. * fsl_dma_alloc_chan_resources - Allocate resources for DMA channel.
  324. * @fsl_chan : Freescale DMA channel
  325. *
  326. * This function will create a dma pool for descriptor allocation.
  327. *
  328. * Return - The number of descriptors allocated.
  329. */
  330. static int fsl_dma_alloc_chan_resources(struct dma_chan *chan)
  331. {
  332. struct fsl_dma_chan *fsl_chan = to_fsl_chan(chan);
  333. /* Has this channel already been allocated? */
  334. if (fsl_chan->desc_pool)
  335. return 1;
  336. /* We need the descriptor to be aligned to 32bytes
  337. * for meeting FSL DMA specification requirement.
  338. */
  339. fsl_chan->desc_pool = dma_pool_create("fsl_dma_engine_desc_pool",
  340. fsl_chan->dev, sizeof(struct fsl_desc_sw),
  341. 32, 0);
  342. if (!fsl_chan->desc_pool) {
  343. dev_err(fsl_chan->dev, "No memory for channel %d "
  344. "descriptor dma pool.\n", fsl_chan->id);
  345. return 0;
  346. }
  347. return 1;
  348. }
  349. /**
  350. * fsl_dma_free_chan_resources - Free all resources of the channel.
  351. * @fsl_chan : Freescale DMA channel
  352. */
  353. static void fsl_dma_free_chan_resources(struct dma_chan *chan)
  354. {
  355. struct fsl_dma_chan *fsl_chan = to_fsl_chan(chan);
  356. struct fsl_desc_sw *desc, *_desc;
  357. unsigned long flags;
  358. dev_dbg(fsl_chan->dev, "Free all channel resources.\n");
  359. spin_lock_irqsave(&fsl_chan->desc_lock, flags);
  360. list_for_each_entry_safe(desc, _desc, &fsl_chan->ld_queue, node) {
  361. #ifdef FSL_DMA_LD_DEBUG
  362. dev_dbg(fsl_chan->dev,
  363. "LD %p will be released.\n", desc);
  364. #endif
  365. list_del(&desc->node);
  366. /* free link descriptor */
  367. dma_pool_free(fsl_chan->desc_pool, desc, desc->async_tx.phys);
  368. }
  369. spin_unlock_irqrestore(&fsl_chan->desc_lock, flags);
  370. dma_pool_destroy(fsl_chan->desc_pool);
  371. fsl_chan->desc_pool = NULL;
  372. }
  373. static struct dma_async_tx_descriptor *
  374. fsl_dma_prep_interrupt(struct dma_chan *chan, unsigned long flags)
  375. {
  376. struct fsl_dma_chan *fsl_chan;
  377. struct fsl_desc_sw *new;
  378. if (!chan)
  379. return NULL;
  380. fsl_chan = to_fsl_chan(chan);
  381. new = fsl_dma_alloc_descriptor(fsl_chan);
  382. if (!new) {
  383. dev_err(fsl_chan->dev, "No free memory for link descriptor\n");
  384. return NULL;
  385. }
  386. new->async_tx.cookie = -EBUSY;
  387. new->async_tx.flags = flags;
  388. /* Insert the link descriptor to the LD ring */
  389. list_add_tail(&new->node, &new->async_tx.tx_list);
  390. /* Set End-of-link to the last link descriptor of new list*/
  391. set_ld_eol(fsl_chan, new);
  392. return &new->async_tx;
  393. }
  394. static struct dma_async_tx_descriptor *fsl_dma_prep_memcpy(
  395. struct dma_chan *chan, dma_addr_t dma_dest, dma_addr_t dma_src,
  396. size_t len, unsigned long flags)
  397. {
  398. struct fsl_dma_chan *fsl_chan;
  399. struct fsl_desc_sw *first = NULL, *prev = NULL, *new;
  400. struct list_head *list;
  401. size_t copy;
  402. if (!chan)
  403. return NULL;
  404. if (!len)
  405. return NULL;
  406. fsl_chan = to_fsl_chan(chan);
  407. do {
  408. /* Allocate the link descriptor from DMA pool */
  409. new = fsl_dma_alloc_descriptor(fsl_chan);
  410. if (!new) {
  411. dev_err(fsl_chan->dev,
  412. "No free memory for link descriptor\n");
  413. goto fail;
  414. }
  415. #ifdef FSL_DMA_LD_DEBUG
  416. dev_dbg(fsl_chan->dev, "new link desc alloc %p\n", new);
  417. #endif
  418. copy = min(len, (size_t)FSL_DMA_BCR_MAX_CNT);
  419. set_desc_cnt(fsl_chan, &new->hw, copy);
  420. set_desc_src(fsl_chan, &new->hw, dma_src);
  421. set_desc_dest(fsl_chan, &new->hw, dma_dest);
  422. if (!first)
  423. first = new;
  424. else
  425. set_desc_next(fsl_chan, &prev->hw, new->async_tx.phys);
  426. new->async_tx.cookie = 0;
  427. async_tx_ack(&new->async_tx);
  428. prev = new;
  429. len -= copy;
  430. dma_src += copy;
  431. dma_dest += copy;
  432. /* Insert the link descriptor to the LD ring */
  433. list_add_tail(&new->node, &first->async_tx.tx_list);
  434. } while (len);
  435. new->async_tx.flags = flags; /* client is in control of this ack */
  436. new->async_tx.cookie = -EBUSY;
  437. /* Set End-of-link to the last link descriptor of new list*/
  438. set_ld_eol(fsl_chan, new);
  439. return &first->async_tx;
  440. fail:
  441. if (!first)
  442. return NULL;
  443. list = &first->async_tx.tx_list;
  444. list_for_each_entry_safe_reverse(new, prev, list, node) {
  445. list_del(&new->node);
  446. dma_pool_free(fsl_chan->desc_pool, new, new->async_tx.phys);
  447. }
  448. return NULL;
  449. }
  450. /**
  451. * fsl_dma_update_completed_cookie - Update the completed cookie.
  452. * @fsl_chan : Freescale DMA channel
  453. */
  454. static void fsl_dma_update_completed_cookie(struct fsl_dma_chan *fsl_chan)
  455. {
  456. struct fsl_desc_sw *cur_desc, *desc;
  457. dma_addr_t ld_phy;
  458. ld_phy = get_cdar(fsl_chan) & FSL_DMA_NLDA_MASK;
  459. if (ld_phy) {
  460. cur_desc = NULL;
  461. list_for_each_entry(desc, &fsl_chan->ld_queue, node)
  462. if (desc->async_tx.phys == ld_phy) {
  463. cur_desc = desc;
  464. break;
  465. }
  466. if (cur_desc && cur_desc->async_tx.cookie) {
  467. if (dma_is_idle(fsl_chan))
  468. fsl_chan->completed_cookie =
  469. cur_desc->async_tx.cookie;
  470. else
  471. fsl_chan->completed_cookie =
  472. cur_desc->async_tx.cookie - 1;
  473. }
  474. }
  475. }
  476. /**
  477. * fsl_chan_ld_cleanup - Clean up link descriptors
  478. * @fsl_chan : Freescale DMA channel
  479. *
  480. * This function clean up the ld_queue of DMA channel.
  481. * If 'in_intr' is set, the function will move the link descriptor to
  482. * the recycle list. Otherwise, free it directly.
  483. */
  484. static void fsl_chan_ld_cleanup(struct fsl_dma_chan *fsl_chan)
  485. {
  486. struct fsl_desc_sw *desc, *_desc;
  487. unsigned long flags;
  488. spin_lock_irqsave(&fsl_chan->desc_lock, flags);
  489. dev_dbg(fsl_chan->dev, "chan completed_cookie = %d\n",
  490. fsl_chan->completed_cookie);
  491. list_for_each_entry_safe(desc, _desc, &fsl_chan->ld_queue, node) {
  492. dma_async_tx_callback callback;
  493. void *callback_param;
  494. if (dma_async_is_complete(desc->async_tx.cookie,
  495. fsl_chan->completed_cookie, fsl_chan->common.cookie)
  496. == DMA_IN_PROGRESS)
  497. break;
  498. callback = desc->async_tx.callback;
  499. callback_param = desc->async_tx.callback_param;
  500. /* Remove from ld_queue list */
  501. list_del(&desc->node);
  502. dev_dbg(fsl_chan->dev, "link descriptor %p will be recycle.\n",
  503. desc);
  504. dma_pool_free(fsl_chan->desc_pool, desc, desc->async_tx.phys);
  505. /* Run the link descriptor callback function */
  506. if (callback) {
  507. spin_unlock_irqrestore(&fsl_chan->desc_lock, flags);
  508. dev_dbg(fsl_chan->dev, "link descriptor %p callback\n",
  509. desc);
  510. callback(callback_param);
  511. spin_lock_irqsave(&fsl_chan->desc_lock, flags);
  512. }
  513. }
  514. spin_unlock_irqrestore(&fsl_chan->desc_lock, flags);
  515. }
  516. /**
  517. * fsl_chan_xfer_ld_queue - Transfer link descriptors in channel ld_queue.
  518. * @fsl_chan : Freescale DMA channel
  519. */
  520. static void fsl_chan_xfer_ld_queue(struct fsl_dma_chan *fsl_chan)
  521. {
  522. struct list_head *ld_node;
  523. dma_addr_t next_dest_addr;
  524. unsigned long flags;
  525. spin_lock_irqsave(&fsl_chan->desc_lock, flags);
  526. if (!dma_is_idle(fsl_chan))
  527. goto out_unlock;
  528. dma_halt(fsl_chan);
  529. /* If there are some link descriptors
  530. * not transfered in queue. We need to start it.
  531. */
  532. /* Find the first un-transfer desciptor */
  533. for (ld_node = fsl_chan->ld_queue.next;
  534. (ld_node != &fsl_chan->ld_queue)
  535. && (dma_async_is_complete(
  536. to_fsl_desc(ld_node)->async_tx.cookie,
  537. fsl_chan->completed_cookie,
  538. fsl_chan->common.cookie) == DMA_SUCCESS);
  539. ld_node = ld_node->next);
  540. if (ld_node != &fsl_chan->ld_queue) {
  541. /* Get the ld start address from ld_queue */
  542. next_dest_addr = to_fsl_desc(ld_node)->async_tx.phys;
  543. dev_dbg(fsl_chan->dev, "xfer LDs staring from 0x%llx\n",
  544. (unsigned long long)next_dest_addr);
  545. set_cdar(fsl_chan, next_dest_addr);
  546. dma_start(fsl_chan);
  547. } else {
  548. set_cdar(fsl_chan, 0);
  549. set_ndar(fsl_chan, 0);
  550. }
  551. out_unlock:
  552. spin_unlock_irqrestore(&fsl_chan->desc_lock, flags);
  553. }
  554. /**
  555. * fsl_dma_memcpy_issue_pending - Issue the DMA start command
  556. * @fsl_chan : Freescale DMA channel
  557. */
  558. static void fsl_dma_memcpy_issue_pending(struct dma_chan *chan)
  559. {
  560. struct fsl_dma_chan *fsl_chan = to_fsl_chan(chan);
  561. #ifdef FSL_DMA_LD_DEBUG
  562. struct fsl_desc_sw *ld;
  563. unsigned long flags;
  564. spin_lock_irqsave(&fsl_chan->desc_lock, flags);
  565. if (list_empty(&fsl_chan->ld_queue)) {
  566. spin_unlock_irqrestore(&fsl_chan->desc_lock, flags);
  567. return;
  568. }
  569. dev_dbg(fsl_chan->dev, "--memcpy issue--\n");
  570. list_for_each_entry(ld, &fsl_chan->ld_queue, node) {
  571. int i;
  572. dev_dbg(fsl_chan->dev, "Ch %d, LD %08x\n",
  573. fsl_chan->id, ld->async_tx.phys);
  574. for (i = 0; i < 8; i++)
  575. dev_dbg(fsl_chan->dev, "LD offset %d: %08x\n",
  576. i, *(((u32 *)&ld->hw) + i));
  577. }
  578. dev_dbg(fsl_chan->dev, "----------------\n");
  579. spin_unlock_irqrestore(&fsl_chan->desc_lock, flags);
  580. #endif
  581. fsl_chan_xfer_ld_queue(fsl_chan);
  582. }
  583. /**
  584. * fsl_dma_is_complete - Determine the DMA status
  585. * @fsl_chan : Freescale DMA channel
  586. */
  587. static enum dma_status fsl_dma_is_complete(struct dma_chan *chan,
  588. dma_cookie_t cookie,
  589. dma_cookie_t *done,
  590. dma_cookie_t *used)
  591. {
  592. struct fsl_dma_chan *fsl_chan = to_fsl_chan(chan);
  593. dma_cookie_t last_used;
  594. dma_cookie_t last_complete;
  595. fsl_chan_ld_cleanup(fsl_chan);
  596. last_used = chan->cookie;
  597. last_complete = fsl_chan->completed_cookie;
  598. if (done)
  599. *done = last_complete;
  600. if (used)
  601. *used = last_used;
  602. return dma_async_is_complete(cookie, last_complete, last_used);
  603. }
  604. static irqreturn_t fsl_dma_chan_do_interrupt(int irq, void *data)
  605. {
  606. struct fsl_dma_chan *fsl_chan = (struct fsl_dma_chan *)data;
  607. u32 stat;
  608. int update_cookie = 0;
  609. int xfer_ld_q = 0;
  610. stat = get_sr(fsl_chan);
  611. dev_dbg(fsl_chan->dev, "event: channel %d, stat = 0x%x\n",
  612. fsl_chan->id, stat);
  613. set_sr(fsl_chan, stat); /* Clear the event register */
  614. stat &= ~(FSL_DMA_SR_CB | FSL_DMA_SR_CH);
  615. if (!stat)
  616. return IRQ_NONE;
  617. if (stat & FSL_DMA_SR_TE)
  618. dev_err(fsl_chan->dev, "Transfer Error!\n");
  619. /* Programming Error
  620. * The DMA_INTERRUPT async_tx is a NULL transfer, which will
  621. * triger a PE interrupt.
  622. */
  623. if (stat & FSL_DMA_SR_PE) {
  624. dev_dbg(fsl_chan->dev, "event: Programming Error INT\n");
  625. if (get_bcr(fsl_chan) == 0) {
  626. /* BCR register is 0, this is a DMA_INTERRUPT async_tx.
  627. * Now, update the completed cookie, and continue the
  628. * next uncompleted transfer.
  629. */
  630. update_cookie = 1;
  631. xfer_ld_q = 1;
  632. }
  633. stat &= ~FSL_DMA_SR_PE;
  634. }
  635. /* If the link descriptor segment transfer finishes,
  636. * we will recycle the used descriptor.
  637. */
  638. if (stat & FSL_DMA_SR_EOSI) {
  639. dev_dbg(fsl_chan->dev, "event: End-of-segments INT\n");
  640. dev_dbg(fsl_chan->dev, "event: clndar 0x%llx, nlndar 0x%llx\n",
  641. (unsigned long long)get_cdar(fsl_chan),
  642. (unsigned long long)get_ndar(fsl_chan));
  643. stat &= ~FSL_DMA_SR_EOSI;
  644. update_cookie = 1;
  645. }
  646. /* For MPC8349, EOCDI event need to update cookie
  647. * and start the next transfer if it exist.
  648. */
  649. if (stat & FSL_DMA_SR_EOCDI) {
  650. dev_dbg(fsl_chan->dev, "event: End-of-Chain link INT\n");
  651. stat &= ~FSL_DMA_SR_EOCDI;
  652. update_cookie = 1;
  653. xfer_ld_q = 1;
  654. }
  655. /* If it current transfer is the end-of-transfer,
  656. * we should clear the Channel Start bit for
  657. * prepare next transfer.
  658. */
  659. if (stat & FSL_DMA_SR_EOLNI) {
  660. dev_dbg(fsl_chan->dev, "event: End-of-link INT\n");
  661. stat &= ~FSL_DMA_SR_EOLNI;
  662. xfer_ld_q = 1;
  663. }
  664. if (update_cookie)
  665. fsl_dma_update_completed_cookie(fsl_chan);
  666. if (xfer_ld_q)
  667. fsl_chan_xfer_ld_queue(fsl_chan);
  668. if (stat)
  669. dev_dbg(fsl_chan->dev, "event: unhandled sr 0x%02x\n",
  670. stat);
  671. dev_dbg(fsl_chan->dev, "event: Exit\n");
  672. tasklet_schedule(&fsl_chan->tasklet);
  673. return IRQ_HANDLED;
  674. }
  675. static irqreturn_t fsl_dma_do_interrupt(int irq, void *data)
  676. {
  677. struct fsl_dma_device *fdev = (struct fsl_dma_device *)data;
  678. u32 gsr;
  679. int ch_nr;
  680. gsr = (fdev->feature & FSL_DMA_BIG_ENDIAN) ? in_be32(fdev->reg_base)
  681. : in_le32(fdev->reg_base);
  682. ch_nr = (32 - ffs(gsr)) / 8;
  683. return fdev->chan[ch_nr] ? fsl_dma_chan_do_interrupt(irq,
  684. fdev->chan[ch_nr]) : IRQ_NONE;
  685. }
  686. static void dma_do_tasklet(unsigned long data)
  687. {
  688. struct fsl_dma_chan *fsl_chan = (struct fsl_dma_chan *)data;
  689. fsl_chan_ld_cleanup(fsl_chan);
  690. }
  691. static int __devinit fsl_dma_chan_probe(struct fsl_dma_device *fdev,
  692. struct device_node *node, u32 feature, const char *compatible)
  693. {
  694. struct fsl_dma_chan *new_fsl_chan;
  695. int err;
  696. /* alloc channel */
  697. new_fsl_chan = kzalloc(sizeof(struct fsl_dma_chan), GFP_KERNEL);
  698. if (!new_fsl_chan) {
  699. dev_err(fdev->dev, "No free memory for allocating "
  700. "dma channels!\n");
  701. return -ENOMEM;
  702. }
  703. /* get dma channel register base */
  704. err = of_address_to_resource(node, 0, &new_fsl_chan->reg);
  705. if (err) {
  706. dev_err(fdev->dev, "Can't get %s property 'reg'\n",
  707. node->full_name);
  708. goto err_no_reg;
  709. }
  710. new_fsl_chan->feature = feature;
  711. if (!fdev->feature)
  712. fdev->feature = new_fsl_chan->feature;
  713. /* If the DMA device's feature is different than its channels',
  714. * report the bug.
  715. */
  716. WARN_ON(fdev->feature != new_fsl_chan->feature);
  717. new_fsl_chan->dev = fdev->dev;
  718. new_fsl_chan->reg_base = ioremap(new_fsl_chan->reg.start,
  719. new_fsl_chan->reg.end - new_fsl_chan->reg.start + 1);
  720. new_fsl_chan->id = ((new_fsl_chan->reg.start - 0x100) & 0xfff) >> 7;
  721. if (new_fsl_chan->id >= FSL_DMA_MAX_CHANS_PER_DEVICE) {
  722. dev_err(fdev->dev, "There is no %d channel!\n",
  723. new_fsl_chan->id);
  724. err = -EINVAL;
  725. goto err_no_chan;
  726. }
  727. fdev->chan[new_fsl_chan->id] = new_fsl_chan;
  728. tasklet_init(&new_fsl_chan->tasklet, dma_do_tasklet,
  729. (unsigned long)new_fsl_chan);
  730. /* Init the channel */
  731. dma_init(new_fsl_chan);
  732. /* Clear cdar registers */
  733. set_cdar(new_fsl_chan, 0);
  734. switch (new_fsl_chan->feature & FSL_DMA_IP_MASK) {
  735. case FSL_DMA_IP_85XX:
  736. new_fsl_chan->toggle_ext_start = fsl_chan_toggle_ext_start;
  737. new_fsl_chan->toggle_ext_pause = fsl_chan_toggle_ext_pause;
  738. case FSL_DMA_IP_83XX:
  739. new_fsl_chan->set_src_loop_size = fsl_chan_set_src_loop_size;
  740. new_fsl_chan->set_dest_loop_size = fsl_chan_set_dest_loop_size;
  741. }
  742. spin_lock_init(&new_fsl_chan->desc_lock);
  743. INIT_LIST_HEAD(&new_fsl_chan->ld_queue);
  744. new_fsl_chan->common.device = &fdev->common;
  745. /* Add the channel to DMA device channel list */
  746. list_add_tail(&new_fsl_chan->common.device_node,
  747. &fdev->common.channels);
  748. fdev->common.chancnt++;
  749. new_fsl_chan->irq = irq_of_parse_and_map(node, 0);
  750. if (new_fsl_chan->irq != NO_IRQ) {
  751. err = request_irq(new_fsl_chan->irq,
  752. &fsl_dma_chan_do_interrupt, IRQF_SHARED,
  753. "fsldma-channel", new_fsl_chan);
  754. if (err) {
  755. dev_err(fdev->dev, "DMA channel %s request_irq error "
  756. "with return %d\n", node->full_name, err);
  757. goto err_no_irq;
  758. }
  759. }
  760. dev_info(fdev->dev, "#%d (%s), irq %d\n", new_fsl_chan->id,
  761. compatible,
  762. new_fsl_chan->irq != NO_IRQ ? new_fsl_chan->irq : fdev->irq);
  763. return 0;
  764. err_no_irq:
  765. list_del(&new_fsl_chan->common.device_node);
  766. err_no_chan:
  767. iounmap(new_fsl_chan->reg_base);
  768. err_no_reg:
  769. kfree(new_fsl_chan);
  770. return err;
  771. }
  772. static void fsl_dma_chan_remove(struct fsl_dma_chan *fchan)
  773. {
  774. if (fchan->irq != NO_IRQ)
  775. free_irq(fchan->irq, fchan);
  776. list_del(&fchan->common.device_node);
  777. iounmap(fchan->reg_base);
  778. kfree(fchan);
  779. }
  780. static int __devinit of_fsl_dma_probe(struct of_device *dev,
  781. const struct of_device_id *match)
  782. {
  783. int err;
  784. struct fsl_dma_device *fdev;
  785. struct device_node *child;
  786. fdev = kzalloc(sizeof(struct fsl_dma_device), GFP_KERNEL);
  787. if (!fdev) {
  788. dev_err(&dev->dev, "No enough memory for 'priv'\n");
  789. return -ENOMEM;
  790. }
  791. fdev->dev = &dev->dev;
  792. INIT_LIST_HEAD(&fdev->common.channels);
  793. /* get DMA controller register base */
  794. err = of_address_to_resource(dev->node, 0, &fdev->reg);
  795. if (err) {
  796. dev_err(&dev->dev, "Can't get %s property 'reg'\n",
  797. dev->node->full_name);
  798. goto err_no_reg;
  799. }
  800. dev_info(&dev->dev, "Probe the Freescale DMA driver for %s "
  801. "controller at 0x%llx...\n",
  802. match->compatible, (unsigned long long)fdev->reg.start);
  803. fdev->reg_base = ioremap(fdev->reg.start, fdev->reg.end
  804. - fdev->reg.start + 1);
  805. dma_cap_set(DMA_MEMCPY, fdev->common.cap_mask);
  806. dma_cap_set(DMA_INTERRUPT, fdev->common.cap_mask);
  807. fdev->common.device_alloc_chan_resources = fsl_dma_alloc_chan_resources;
  808. fdev->common.device_free_chan_resources = fsl_dma_free_chan_resources;
  809. fdev->common.device_prep_dma_interrupt = fsl_dma_prep_interrupt;
  810. fdev->common.device_prep_dma_memcpy = fsl_dma_prep_memcpy;
  811. fdev->common.device_is_tx_complete = fsl_dma_is_complete;
  812. fdev->common.device_issue_pending = fsl_dma_memcpy_issue_pending;
  813. fdev->common.dev = &dev->dev;
  814. fdev->irq = irq_of_parse_and_map(dev->node, 0);
  815. if (fdev->irq != NO_IRQ) {
  816. err = request_irq(fdev->irq, &fsl_dma_do_interrupt, IRQF_SHARED,
  817. "fsldma-device", fdev);
  818. if (err) {
  819. dev_err(&dev->dev, "DMA device request_irq error "
  820. "with return %d\n", err);
  821. goto err;
  822. }
  823. }
  824. dev_set_drvdata(&(dev->dev), fdev);
  825. /* We cannot use of_platform_bus_probe() because there is no
  826. * of_platform_bus_remove. Instead, we manually instantiate every DMA
  827. * channel object.
  828. */
  829. for_each_child_of_node(dev->node, child) {
  830. if (of_device_is_compatible(child, "fsl,eloplus-dma-channel"))
  831. fsl_dma_chan_probe(fdev, child,
  832. FSL_DMA_IP_85XX | FSL_DMA_BIG_ENDIAN,
  833. "fsl,eloplus-dma-channel");
  834. if (of_device_is_compatible(child, "fsl,elo-dma-channel"))
  835. fsl_dma_chan_probe(fdev, child,
  836. FSL_DMA_IP_83XX | FSL_DMA_LITTLE_ENDIAN,
  837. "fsl,elo-dma-channel");
  838. }
  839. dma_async_device_register(&fdev->common);
  840. return 0;
  841. err:
  842. iounmap(fdev->reg_base);
  843. err_no_reg:
  844. kfree(fdev);
  845. return err;
  846. }
  847. static int of_fsl_dma_remove(struct of_device *of_dev)
  848. {
  849. struct fsl_dma_device *fdev;
  850. unsigned int i;
  851. fdev = dev_get_drvdata(&of_dev->dev);
  852. dma_async_device_unregister(&fdev->common);
  853. for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++)
  854. if (fdev->chan[i])
  855. fsl_dma_chan_remove(fdev->chan[i]);
  856. if (fdev->irq != NO_IRQ)
  857. free_irq(fdev->irq, fdev);
  858. iounmap(fdev->reg_base);
  859. kfree(fdev);
  860. dev_set_drvdata(&of_dev->dev, NULL);
  861. return 0;
  862. }
  863. static struct of_device_id of_fsl_dma_ids[] = {
  864. { .compatible = "fsl,eloplus-dma", },
  865. { .compatible = "fsl,elo-dma", },
  866. {}
  867. };
  868. static struct of_platform_driver of_fsl_dma_driver = {
  869. .name = "fsl-elo-dma",
  870. .match_table = of_fsl_dma_ids,
  871. .probe = of_fsl_dma_probe,
  872. .remove = of_fsl_dma_remove,
  873. };
  874. static __init int of_fsl_dma_init(void)
  875. {
  876. int ret;
  877. pr_info("Freescale Elo / Elo Plus DMA driver\n");
  878. ret = of_register_platform_driver(&of_fsl_dma_driver);
  879. if (ret)
  880. pr_err("fsldma: failed to register platform driver\n");
  881. return ret;
  882. }
  883. static void __exit of_fsl_dma_exit(void)
  884. {
  885. of_unregister_platform_driver(&of_fsl_dma_driver);
  886. }
  887. subsys_initcall(of_fsl_dma_init);
  888. module_exit(of_fsl_dma_exit);
  889. MODULE_DESCRIPTION("Freescale Elo / Elo Plus DMA driver");
  890. MODULE_LICENSE("GPL");