fsldma.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  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 = 0;
  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. while (!dma_is_idle(fsl_chan) && (i++ < 100))
  144. udelay(10);
  145. if (i >= 100 && !dma_is_idle(fsl_chan))
  146. dev_err(fsl_chan->dev, "DMA halt timeout!\n");
  147. }
  148. static void set_ld_eol(struct fsl_dma_chan *fsl_chan,
  149. struct fsl_desc_sw *desc)
  150. {
  151. desc->hw.next_ln_addr = CPU_TO_DMA(fsl_chan,
  152. DMA_TO_CPU(fsl_chan, desc->hw.next_ln_addr, 64) | FSL_DMA_EOL,
  153. 64);
  154. }
  155. static void append_ld_queue(struct fsl_dma_chan *fsl_chan,
  156. struct fsl_desc_sw *new_desc)
  157. {
  158. struct fsl_desc_sw *queue_tail = to_fsl_desc(fsl_chan->ld_queue.prev);
  159. if (list_empty(&fsl_chan->ld_queue))
  160. return;
  161. /* Link to the new descriptor physical address and
  162. * Enable End-of-segment interrupt for
  163. * the last link descriptor.
  164. * (the previous node's next link descriptor)
  165. *
  166. * For FSL_DMA_IP_83xx, the snoop enable bit need be set.
  167. */
  168. queue_tail->hw.next_ln_addr = CPU_TO_DMA(fsl_chan,
  169. new_desc->async_tx.phys | FSL_DMA_EOSIE |
  170. (((fsl_chan->feature & FSL_DMA_IP_MASK)
  171. == FSL_DMA_IP_83XX) ? FSL_DMA_SNEN : 0), 64);
  172. }
  173. /**
  174. * fsl_chan_set_src_loop_size - Set source address hold transfer size
  175. * @fsl_chan : Freescale DMA channel
  176. * @size : Address loop size, 0 for disable loop
  177. *
  178. * The set source address hold transfer size. The source
  179. * address hold or loop transfer size is when the DMA transfer
  180. * data from source address (SA), if the loop size is 4, the DMA will
  181. * read data from SA, SA + 1, SA + 2, SA + 3, then loop back to SA,
  182. * SA + 1 ... and so on.
  183. */
  184. static void fsl_chan_set_src_loop_size(struct fsl_dma_chan *fsl_chan, int size)
  185. {
  186. switch (size) {
  187. case 0:
  188. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr,
  189. DMA_IN(fsl_chan, &fsl_chan->reg_base->mr, 32) &
  190. (~FSL_DMA_MR_SAHE), 32);
  191. break;
  192. case 1:
  193. case 2:
  194. case 4:
  195. case 8:
  196. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr,
  197. DMA_IN(fsl_chan, &fsl_chan->reg_base->mr, 32) |
  198. FSL_DMA_MR_SAHE | (__ilog2(size) << 14),
  199. 32);
  200. break;
  201. }
  202. }
  203. /**
  204. * fsl_chan_set_dest_loop_size - Set destination address hold transfer size
  205. * @fsl_chan : Freescale DMA channel
  206. * @size : Address loop size, 0 for disable loop
  207. *
  208. * The set destination address hold transfer size. The destination
  209. * address hold or loop transfer size is when the DMA transfer
  210. * data to destination address (TA), if the loop size is 4, the DMA will
  211. * write data to TA, TA + 1, TA + 2, TA + 3, then loop back to TA,
  212. * TA + 1 ... and so on.
  213. */
  214. static void fsl_chan_set_dest_loop_size(struct fsl_dma_chan *fsl_chan, int size)
  215. {
  216. switch (size) {
  217. case 0:
  218. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr,
  219. DMA_IN(fsl_chan, &fsl_chan->reg_base->mr, 32) &
  220. (~FSL_DMA_MR_DAHE), 32);
  221. break;
  222. case 1:
  223. case 2:
  224. case 4:
  225. case 8:
  226. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr,
  227. DMA_IN(fsl_chan, &fsl_chan->reg_base->mr, 32) |
  228. FSL_DMA_MR_DAHE | (__ilog2(size) << 16),
  229. 32);
  230. break;
  231. }
  232. }
  233. /**
  234. * fsl_chan_toggle_ext_pause - Toggle channel external pause status
  235. * @fsl_chan : Freescale DMA channel
  236. * @size : Pause control size, 0 for disable external pause control.
  237. * The maximum is 1024.
  238. *
  239. * The Freescale DMA channel can be controlled by the external
  240. * signal DREQ#. The pause control size is how many bytes are allowed
  241. * to transfer before pausing the channel, after which a new assertion
  242. * of DREQ# resumes channel operation.
  243. */
  244. static void fsl_chan_toggle_ext_pause(struct fsl_dma_chan *fsl_chan, int size)
  245. {
  246. if (size > 1024)
  247. return;
  248. if (size) {
  249. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr,
  250. DMA_IN(fsl_chan, &fsl_chan->reg_base->mr, 32)
  251. | ((__ilog2(size) << 24) & 0x0f000000),
  252. 32);
  253. fsl_chan->feature |= FSL_DMA_CHAN_PAUSE_EXT;
  254. } else
  255. fsl_chan->feature &= ~FSL_DMA_CHAN_PAUSE_EXT;
  256. }
  257. /**
  258. * fsl_chan_toggle_ext_start - Toggle channel external start status
  259. * @fsl_chan : Freescale DMA channel
  260. * @enable : 0 is disabled, 1 is enabled.
  261. *
  262. * If enable the external start, the channel can be started by an
  263. * external DMA start pin. So the dma_start() does not start the
  264. * transfer immediately. The DMA channel will wait for the
  265. * control pin asserted.
  266. */
  267. static void fsl_chan_toggle_ext_start(struct fsl_dma_chan *fsl_chan, int enable)
  268. {
  269. if (enable)
  270. fsl_chan->feature |= FSL_DMA_CHAN_START_EXT;
  271. else
  272. fsl_chan->feature &= ~FSL_DMA_CHAN_START_EXT;
  273. }
  274. static dma_cookie_t fsl_dma_tx_submit(struct dma_async_tx_descriptor *tx)
  275. {
  276. struct fsl_desc_sw *desc = tx_to_fsl_desc(tx);
  277. struct fsl_dma_chan *fsl_chan = to_fsl_chan(tx->chan);
  278. unsigned long flags;
  279. dma_cookie_t cookie;
  280. /* cookie increment and adding to ld_queue must be atomic */
  281. spin_lock_irqsave(&fsl_chan->desc_lock, flags);
  282. cookie = fsl_chan->common.cookie;
  283. cookie++;
  284. if (cookie < 0)
  285. cookie = 1;
  286. desc->async_tx.cookie = cookie;
  287. fsl_chan->common.cookie = desc->async_tx.cookie;
  288. append_ld_queue(fsl_chan, desc);
  289. list_splice_init(&desc->async_tx.tx_list, fsl_chan->ld_queue.prev);
  290. spin_unlock_irqrestore(&fsl_chan->desc_lock, flags);
  291. return cookie;
  292. }
  293. /**
  294. * fsl_dma_alloc_descriptor - Allocate descriptor from channel's DMA pool.
  295. * @fsl_chan : Freescale DMA channel
  296. *
  297. * Return - The descriptor allocated. NULL for failed.
  298. */
  299. static struct fsl_desc_sw *fsl_dma_alloc_descriptor(
  300. struct fsl_dma_chan *fsl_chan)
  301. {
  302. dma_addr_t pdesc;
  303. struct fsl_desc_sw *desc_sw;
  304. desc_sw = dma_pool_alloc(fsl_chan->desc_pool, GFP_ATOMIC, &pdesc);
  305. if (desc_sw) {
  306. memset(desc_sw, 0, sizeof(struct fsl_desc_sw));
  307. dma_async_tx_descriptor_init(&desc_sw->async_tx,
  308. &fsl_chan->common);
  309. desc_sw->async_tx.tx_submit = fsl_dma_tx_submit;
  310. INIT_LIST_HEAD(&desc_sw->async_tx.tx_list);
  311. desc_sw->async_tx.phys = pdesc;
  312. }
  313. return desc_sw;
  314. }
  315. /**
  316. * fsl_dma_alloc_chan_resources - Allocate resources for DMA channel.
  317. * @fsl_chan : Freescale DMA channel
  318. *
  319. * This function will create a dma pool for descriptor allocation.
  320. *
  321. * Return - The number of descriptors allocated.
  322. */
  323. static int fsl_dma_alloc_chan_resources(struct dma_chan *chan)
  324. {
  325. struct fsl_dma_chan *fsl_chan = to_fsl_chan(chan);
  326. LIST_HEAD(tmp_list);
  327. /* We need the descriptor to be aligned to 32bytes
  328. * for meeting FSL DMA specification requirement.
  329. */
  330. fsl_chan->desc_pool = dma_pool_create("fsl_dma_engine_desc_pool",
  331. fsl_chan->dev, sizeof(struct fsl_desc_sw),
  332. 32, 0);
  333. if (!fsl_chan->desc_pool) {
  334. dev_err(fsl_chan->dev, "No memory for channel %d "
  335. "descriptor dma pool.\n", fsl_chan->id);
  336. return 0;
  337. }
  338. return 1;
  339. }
  340. /**
  341. * fsl_dma_free_chan_resources - Free all resources of the channel.
  342. * @fsl_chan : Freescale DMA channel
  343. */
  344. static void fsl_dma_free_chan_resources(struct dma_chan *chan)
  345. {
  346. struct fsl_dma_chan *fsl_chan = to_fsl_chan(chan);
  347. struct fsl_desc_sw *desc, *_desc;
  348. unsigned long flags;
  349. dev_dbg(fsl_chan->dev, "Free all channel resources.\n");
  350. spin_lock_irqsave(&fsl_chan->desc_lock, flags);
  351. list_for_each_entry_safe(desc, _desc, &fsl_chan->ld_queue, node) {
  352. #ifdef FSL_DMA_LD_DEBUG
  353. dev_dbg(fsl_chan->dev,
  354. "LD %p will be released.\n", desc);
  355. #endif
  356. list_del(&desc->node);
  357. /* free link descriptor */
  358. dma_pool_free(fsl_chan->desc_pool, desc, desc->async_tx.phys);
  359. }
  360. spin_unlock_irqrestore(&fsl_chan->desc_lock, flags);
  361. dma_pool_destroy(fsl_chan->desc_pool);
  362. }
  363. static struct dma_async_tx_descriptor *
  364. fsl_dma_prep_interrupt(struct dma_chan *chan, unsigned long flags)
  365. {
  366. struct fsl_dma_chan *fsl_chan;
  367. struct fsl_desc_sw *new;
  368. if (!chan)
  369. return NULL;
  370. fsl_chan = to_fsl_chan(chan);
  371. new = fsl_dma_alloc_descriptor(fsl_chan);
  372. if (!new) {
  373. dev_err(fsl_chan->dev, "No free memory for link descriptor\n");
  374. return NULL;
  375. }
  376. new->async_tx.cookie = -EBUSY;
  377. new->async_tx.flags = flags;
  378. /* Insert the link descriptor to the LD ring */
  379. list_add_tail(&new->node, &new->async_tx.tx_list);
  380. /* Set End-of-link to the last link descriptor of new list*/
  381. set_ld_eol(fsl_chan, new);
  382. return &new->async_tx;
  383. }
  384. static struct dma_async_tx_descriptor *fsl_dma_prep_memcpy(
  385. struct dma_chan *chan, dma_addr_t dma_dest, dma_addr_t dma_src,
  386. size_t len, unsigned long flags)
  387. {
  388. struct fsl_dma_chan *fsl_chan;
  389. struct fsl_desc_sw *first = NULL, *prev = NULL, *new;
  390. size_t copy;
  391. LIST_HEAD(link_chain);
  392. if (!chan)
  393. return NULL;
  394. if (!len)
  395. return NULL;
  396. fsl_chan = to_fsl_chan(chan);
  397. do {
  398. /* Allocate the link descriptor from DMA pool */
  399. new = fsl_dma_alloc_descriptor(fsl_chan);
  400. if (!new) {
  401. dev_err(fsl_chan->dev,
  402. "No free memory for link descriptor\n");
  403. return NULL;
  404. }
  405. #ifdef FSL_DMA_LD_DEBUG
  406. dev_dbg(fsl_chan->dev, "new link desc alloc %p\n", new);
  407. #endif
  408. copy = min(len, (size_t)FSL_DMA_BCR_MAX_CNT);
  409. set_desc_cnt(fsl_chan, &new->hw, copy);
  410. set_desc_src(fsl_chan, &new->hw, dma_src);
  411. set_desc_dest(fsl_chan, &new->hw, dma_dest);
  412. if (!first)
  413. first = new;
  414. else
  415. set_desc_next(fsl_chan, &prev->hw, new->async_tx.phys);
  416. new->async_tx.cookie = 0;
  417. async_tx_ack(&new->async_tx);
  418. prev = new;
  419. len -= copy;
  420. dma_src += copy;
  421. dma_dest += copy;
  422. /* Insert the link descriptor to the LD ring */
  423. list_add_tail(&new->node, &first->async_tx.tx_list);
  424. } while (len);
  425. new->async_tx.flags = flags; /* client is in control of this ack */
  426. new->async_tx.cookie = -EBUSY;
  427. /* Set End-of-link to the last link descriptor of new list*/
  428. set_ld_eol(fsl_chan, new);
  429. return first ? &first->async_tx : NULL;
  430. }
  431. /**
  432. * fsl_dma_update_completed_cookie - Update the completed cookie.
  433. * @fsl_chan : Freescale DMA channel
  434. */
  435. static void fsl_dma_update_completed_cookie(struct fsl_dma_chan *fsl_chan)
  436. {
  437. struct fsl_desc_sw *cur_desc, *desc;
  438. dma_addr_t ld_phy;
  439. ld_phy = get_cdar(fsl_chan) & FSL_DMA_NLDA_MASK;
  440. if (ld_phy) {
  441. cur_desc = NULL;
  442. list_for_each_entry(desc, &fsl_chan->ld_queue, node)
  443. if (desc->async_tx.phys == ld_phy) {
  444. cur_desc = desc;
  445. break;
  446. }
  447. if (cur_desc && cur_desc->async_tx.cookie) {
  448. if (dma_is_idle(fsl_chan))
  449. fsl_chan->completed_cookie =
  450. cur_desc->async_tx.cookie;
  451. else
  452. fsl_chan->completed_cookie =
  453. cur_desc->async_tx.cookie - 1;
  454. }
  455. }
  456. }
  457. /**
  458. * fsl_chan_ld_cleanup - Clean up link descriptors
  459. * @fsl_chan : Freescale DMA channel
  460. *
  461. * This function clean up the ld_queue of DMA channel.
  462. * If 'in_intr' is set, the function will move the link descriptor to
  463. * the recycle list. Otherwise, free it directly.
  464. */
  465. static void fsl_chan_ld_cleanup(struct fsl_dma_chan *fsl_chan)
  466. {
  467. struct fsl_desc_sw *desc, *_desc;
  468. unsigned long flags;
  469. spin_lock_irqsave(&fsl_chan->desc_lock, flags);
  470. dev_dbg(fsl_chan->dev, "chan completed_cookie = %d\n",
  471. fsl_chan->completed_cookie);
  472. list_for_each_entry_safe(desc, _desc, &fsl_chan->ld_queue, node) {
  473. dma_async_tx_callback callback;
  474. void *callback_param;
  475. if (dma_async_is_complete(desc->async_tx.cookie,
  476. fsl_chan->completed_cookie, fsl_chan->common.cookie)
  477. == DMA_IN_PROGRESS)
  478. break;
  479. callback = desc->async_tx.callback;
  480. callback_param = desc->async_tx.callback_param;
  481. /* Remove from ld_queue list */
  482. list_del(&desc->node);
  483. dev_dbg(fsl_chan->dev, "link descriptor %p will be recycle.\n",
  484. desc);
  485. dma_pool_free(fsl_chan->desc_pool, desc, desc->async_tx.phys);
  486. /* Run the link descriptor callback function */
  487. if (callback) {
  488. spin_unlock_irqrestore(&fsl_chan->desc_lock, flags);
  489. dev_dbg(fsl_chan->dev, "link descriptor %p callback\n",
  490. desc);
  491. callback(callback_param);
  492. spin_lock_irqsave(&fsl_chan->desc_lock, flags);
  493. }
  494. }
  495. spin_unlock_irqrestore(&fsl_chan->desc_lock, flags);
  496. }
  497. /**
  498. * fsl_chan_xfer_ld_queue - Transfer link descriptors in channel ld_queue.
  499. * @fsl_chan : Freescale DMA channel
  500. */
  501. static void fsl_chan_xfer_ld_queue(struct fsl_dma_chan *fsl_chan)
  502. {
  503. struct list_head *ld_node;
  504. dma_addr_t next_dest_addr;
  505. unsigned long flags;
  506. if (!dma_is_idle(fsl_chan))
  507. return;
  508. dma_halt(fsl_chan);
  509. /* If there are some link descriptors
  510. * not transfered in queue. We need to start it.
  511. */
  512. spin_lock_irqsave(&fsl_chan->desc_lock, flags);
  513. /* Find the first un-transfer desciptor */
  514. for (ld_node = fsl_chan->ld_queue.next;
  515. (ld_node != &fsl_chan->ld_queue)
  516. && (dma_async_is_complete(
  517. to_fsl_desc(ld_node)->async_tx.cookie,
  518. fsl_chan->completed_cookie,
  519. fsl_chan->common.cookie) == DMA_SUCCESS);
  520. ld_node = ld_node->next);
  521. spin_unlock_irqrestore(&fsl_chan->desc_lock, flags);
  522. if (ld_node != &fsl_chan->ld_queue) {
  523. /* Get the ld start address from ld_queue */
  524. next_dest_addr = to_fsl_desc(ld_node)->async_tx.phys;
  525. dev_dbg(fsl_chan->dev, "xfer LDs staring from %p\n",
  526. (void *)next_dest_addr);
  527. set_cdar(fsl_chan, next_dest_addr);
  528. dma_start(fsl_chan);
  529. } else {
  530. set_cdar(fsl_chan, 0);
  531. set_ndar(fsl_chan, 0);
  532. }
  533. }
  534. /**
  535. * fsl_dma_memcpy_issue_pending - Issue the DMA start command
  536. * @fsl_chan : Freescale DMA channel
  537. */
  538. static void fsl_dma_memcpy_issue_pending(struct dma_chan *chan)
  539. {
  540. struct fsl_dma_chan *fsl_chan = to_fsl_chan(chan);
  541. #ifdef FSL_DMA_LD_DEBUG
  542. struct fsl_desc_sw *ld;
  543. unsigned long flags;
  544. spin_lock_irqsave(&fsl_chan->desc_lock, flags);
  545. if (list_empty(&fsl_chan->ld_queue)) {
  546. spin_unlock_irqrestore(&fsl_chan->desc_lock, flags);
  547. return;
  548. }
  549. dev_dbg(fsl_chan->dev, "--memcpy issue--\n");
  550. list_for_each_entry(ld, &fsl_chan->ld_queue, node) {
  551. int i;
  552. dev_dbg(fsl_chan->dev, "Ch %d, LD %08x\n",
  553. fsl_chan->id, ld->async_tx.phys);
  554. for (i = 0; i < 8; i++)
  555. dev_dbg(fsl_chan->dev, "LD offset %d: %08x\n",
  556. i, *(((u32 *)&ld->hw) + i));
  557. }
  558. dev_dbg(fsl_chan->dev, "----------------\n");
  559. spin_unlock_irqrestore(&fsl_chan->desc_lock, flags);
  560. #endif
  561. fsl_chan_xfer_ld_queue(fsl_chan);
  562. }
  563. /**
  564. * fsl_dma_is_complete - Determine the DMA status
  565. * @fsl_chan : Freescale DMA channel
  566. */
  567. static enum dma_status fsl_dma_is_complete(struct dma_chan *chan,
  568. dma_cookie_t cookie,
  569. dma_cookie_t *done,
  570. dma_cookie_t *used)
  571. {
  572. struct fsl_dma_chan *fsl_chan = to_fsl_chan(chan);
  573. dma_cookie_t last_used;
  574. dma_cookie_t last_complete;
  575. fsl_chan_ld_cleanup(fsl_chan);
  576. last_used = chan->cookie;
  577. last_complete = fsl_chan->completed_cookie;
  578. if (done)
  579. *done = last_complete;
  580. if (used)
  581. *used = last_used;
  582. return dma_async_is_complete(cookie, last_complete, last_used);
  583. }
  584. static irqreturn_t fsl_dma_chan_do_interrupt(int irq, void *data)
  585. {
  586. struct fsl_dma_chan *fsl_chan = (struct fsl_dma_chan *)data;
  587. u32 stat;
  588. int update_cookie = 0;
  589. int xfer_ld_q = 0;
  590. stat = get_sr(fsl_chan);
  591. dev_dbg(fsl_chan->dev, "event: channel %d, stat = 0x%x\n",
  592. fsl_chan->id, stat);
  593. set_sr(fsl_chan, stat); /* Clear the event register */
  594. stat &= ~(FSL_DMA_SR_CB | FSL_DMA_SR_CH);
  595. if (!stat)
  596. return IRQ_NONE;
  597. if (stat & FSL_DMA_SR_TE)
  598. dev_err(fsl_chan->dev, "Transfer Error!\n");
  599. /* Programming Error
  600. * The DMA_INTERRUPT async_tx is a NULL transfer, which will
  601. * triger a PE interrupt.
  602. */
  603. if (stat & FSL_DMA_SR_PE) {
  604. dev_dbg(fsl_chan->dev, "event: Programming Error INT\n");
  605. if (get_bcr(fsl_chan) == 0) {
  606. /* BCR register is 0, this is a DMA_INTERRUPT async_tx.
  607. * Now, update the completed cookie, and continue the
  608. * next uncompleted transfer.
  609. */
  610. update_cookie = 1;
  611. xfer_ld_q = 1;
  612. }
  613. stat &= ~FSL_DMA_SR_PE;
  614. }
  615. /* If the link descriptor segment transfer finishes,
  616. * we will recycle the used descriptor.
  617. */
  618. if (stat & FSL_DMA_SR_EOSI) {
  619. dev_dbg(fsl_chan->dev, "event: End-of-segments INT\n");
  620. dev_dbg(fsl_chan->dev, "event: clndar %p, nlndar %p\n",
  621. (void *)get_cdar(fsl_chan), (void *)get_ndar(fsl_chan));
  622. stat &= ~FSL_DMA_SR_EOSI;
  623. update_cookie = 1;
  624. }
  625. /* For MPC8349, EOCDI event need to update cookie
  626. * and start the next transfer if it exist.
  627. */
  628. if (stat & FSL_DMA_SR_EOCDI) {
  629. dev_dbg(fsl_chan->dev, "event: End-of-Chain link INT\n");
  630. stat &= ~FSL_DMA_SR_EOCDI;
  631. update_cookie = 1;
  632. xfer_ld_q = 1;
  633. }
  634. /* If it current transfer is the end-of-transfer,
  635. * we should clear the Channel Start bit for
  636. * prepare next transfer.
  637. */
  638. if (stat & FSL_DMA_SR_EOLNI) {
  639. dev_dbg(fsl_chan->dev, "event: End-of-link INT\n");
  640. stat &= ~FSL_DMA_SR_EOLNI;
  641. xfer_ld_q = 1;
  642. }
  643. if (update_cookie)
  644. fsl_dma_update_completed_cookie(fsl_chan);
  645. if (xfer_ld_q)
  646. fsl_chan_xfer_ld_queue(fsl_chan);
  647. if (stat)
  648. dev_dbg(fsl_chan->dev, "event: unhandled sr 0x%02x\n",
  649. stat);
  650. dev_dbg(fsl_chan->dev, "event: Exit\n");
  651. tasklet_schedule(&fsl_chan->tasklet);
  652. return IRQ_HANDLED;
  653. }
  654. static irqreturn_t fsl_dma_do_interrupt(int irq, void *data)
  655. {
  656. struct fsl_dma_device *fdev = (struct fsl_dma_device *)data;
  657. u32 gsr;
  658. int ch_nr;
  659. gsr = (fdev->feature & FSL_DMA_BIG_ENDIAN) ? in_be32(fdev->reg_base)
  660. : in_le32(fdev->reg_base);
  661. ch_nr = (32 - ffs(gsr)) / 8;
  662. return fdev->chan[ch_nr] ? fsl_dma_chan_do_interrupt(irq,
  663. fdev->chan[ch_nr]) : IRQ_NONE;
  664. }
  665. static void dma_do_tasklet(unsigned long data)
  666. {
  667. struct fsl_dma_chan *fsl_chan = (struct fsl_dma_chan *)data;
  668. fsl_chan_ld_cleanup(fsl_chan);
  669. }
  670. static void fsl_dma_callback_test(void *param)
  671. {
  672. struct fsl_dma_chan *fsl_chan = param;
  673. if (fsl_chan)
  674. dev_dbg(fsl_chan->dev, "selftest: callback is ok!\n");
  675. }
  676. static int fsl_dma_self_test(struct fsl_dma_chan *fsl_chan)
  677. {
  678. struct dma_chan *chan;
  679. int err = 0;
  680. dma_addr_t dma_dest, dma_src;
  681. dma_cookie_t cookie;
  682. u8 *src, *dest;
  683. int i;
  684. size_t test_size;
  685. struct dma_async_tx_descriptor *tx1, *tx2, *tx3;
  686. test_size = 4096;
  687. src = kmalloc(test_size * 2, GFP_KERNEL);
  688. if (!src) {
  689. dev_err(fsl_chan->dev,
  690. "selftest: Cannot alloc memory for test!\n");
  691. return -ENOMEM;
  692. }
  693. dest = src + test_size;
  694. for (i = 0; i < test_size; i++)
  695. src[i] = (u8) i;
  696. chan = &fsl_chan->common;
  697. if (fsl_dma_alloc_chan_resources(chan) < 1) {
  698. dev_err(fsl_chan->dev,
  699. "selftest: Cannot alloc resources for DMA\n");
  700. err = -ENODEV;
  701. goto out;
  702. }
  703. /* TX 1 */
  704. dma_src = dma_map_single(fsl_chan->dev, src, test_size / 2,
  705. DMA_TO_DEVICE);
  706. dma_dest = dma_map_single(fsl_chan->dev, dest, test_size / 2,
  707. DMA_FROM_DEVICE);
  708. tx1 = fsl_dma_prep_memcpy(chan, dma_dest, dma_src, test_size / 2, 0);
  709. async_tx_ack(tx1);
  710. cookie = fsl_dma_tx_submit(tx1);
  711. fsl_dma_memcpy_issue_pending(chan);
  712. msleep(2);
  713. if (fsl_dma_is_complete(chan, cookie, NULL, NULL) != DMA_SUCCESS) {
  714. dev_err(fsl_chan->dev, "selftest: Time out!\n");
  715. err = -ENODEV;
  716. goto free_resources;
  717. }
  718. /* Test free and re-alloc channel resources */
  719. fsl_dma_free_chan_resources(chan);
  720. if (fsl_dma_alloc_chan_resources(chan) < 1) {
  721. dev_err(fsl_chan->dev,
  722. "selftest: Cannot alloc resources for DMA\n");
  723. err = -ENODEV;
  724. goto free_resources;
  725. }
  726. /* Continue to test
  727. * TX 2
  728. */
  729. dma_src = dma_map_single(fsl_chan->dev, src + test_size / 2,
  730. test_size / 4, DMA_TO_DEVICE);
  731. dma_dest = dma_map_single(fsl_chan->dev, dest + test_size / 2,
  732. test_size / 4, DMA_FROM_DEVICE);
  733. tx2 = fsl_dma_prep_memcpy(chan, dma_dest, dma_src, test_size / 4, 0);
  734. async_tx_ack(tx2);
  735. /* TX 3 */
  736. dma_src = dma_map_single(fsl_chan->dev, src + test_size * 3 / 4,
  737. test_size / 4, DMA_TO_DEVICE);
  738. dma_dest = dma_map_single(fsl_chan->dev, dest + test_size * 3 / 4,
  739. test_size / 4, DMA_FROM_DEVICE);
  740. tx3 = fsl_dma_prep_memcpy(chan, dma_dest, dma_src, test_size / 4, 0);
  741. async_tx_ack(tx3);
  742. /* Interrupt tx test */
  743. tx1 = fsl_dma_prep_interrupt(chan, 0);
  744. async_tx_ack(tx1);
  745. cookie = fsl_dma_tx_submit(tx1);
  746. /* Test exchanging the prepared tx sort */
  747. cookie = fsl_dma_tx_submit(tx3);
  748. cookie = fsl_dma_tx_submit(tx2);
  749. if (dma_has_cap(DMA_INTERRUPT, ((struct fsl_dma_device *)
  750. dev_get_drvdata(fsl_chan->dev->parent))->common.cap_mask)) {
  751. tx3->callback = fsl_dma_callback_test;
  752. tx3->callback_param = fsl_chan;
  753. }
  754. fsl_dma_memcpy_issue_pending(chan);
  755. msleep(2);
  756. if (fsl_dma_is_complete(chan, cookie, NULL, NULL) != DMA_SUCCESS) {
  757. dev_err(fsl_chan->dev, "selftest: Time out!\n");
  758. err = -ENODEV;
  759. goto free_resources;
  760. }
  761. err = memcmp(src, dest, test_size);
  762. if (err) {
  763. for (i = 0; (*(src + i) == *(dest + i)) && (i < test_size);
  764. i++);
  765. dev_err(fsl_chan->dev, "selftest: Test failed, data %d/%ld is "
  766. "error! src 0x%x, dest 0x%x\n",
  767. i, (long)test_size, *(src + i), *(dest + i));
  768. }
  769. free_resources:
  770. fsl_dma_free_chan_resources(chan);
  771. out:
  772. kfree(src);
  773. return err;
  774. }
  775. static int __devinit of_fsl_dma_chan_probe(struct of_device *dev,
  776. const struct of_device_id *match)
  777. {
  778. struct fsl_dma_device *fdev;
  779. struct fsl_dma_chan *new_fsl_chan;
  780. int err;
  781. fdev = dev_get_drvdata(dev->dev.parent);
  782. BUG_ON(!fdev);
  783. /* alloc channel */
  784. new_fsl_chan = kzalloc(sizeof(struct fsl_dma_chan), GFP_KERNEL);
  785. if (!new_fsl_chan) {
  786. dev_err(&dev->dev, "No free memory for allocating "
  787. "dma channels!\n");
  788. return -ENOMEM;
  789. }
  790. /* get dma channel register base */
  791. err = of_address_to_resource(dev->node, 0, &new_fsl_chan->reg);
  792. if (err) {
  793. dev_err(&dev->dev, "Can't get %s property 'reg'\n",
  794. dev->node->full_name);
  795. goto err_no_reg;
  796. }
  797. new_fsl_chan->feature = *(u32 *)match->data;
  798. if (!fdev->feature)
  799. fdev->feature = new_fsl_chan->feature;
  800. /* If the DMA device's feature is different than its channels',
  801. * report the bug.
  802. */
  803. WARN_ON(fdev->feature != new_fsl_chan->feature);
  804. new_fsl_chan->dev = &dev->dev;
  805. new_fsl_chan->reg_base = ioremap(new_fsl_chan->reg.start,
  806. new_fsl_chan->reg.end - new_fsl_chan->reg.start + 1);
  807. new_fsl_chan->id = ((new_fsl_chan->reg.start - 0x100) & 0xfff) >> 7;
  808. if (new_fsl_chan->id > FSL_DMA_MAX_CHANS_PER_DEVICE) {
  809. dev_err(&dev->dev, "There is no %d channel!\n",
  810. new_fsl_chan->id);
  811. err = -EINVAL;
  812. goto err_no_chan;
  813. }
  814. fdev->chan[new_fsl_chan->id] = new_fsl_chan;
  815. tasklet_init(&new_fsl_chan->tasklet, dma_do_tasklet,
  816. (unsigned long)new_fsl_chan);
  817. /* Init the channel */
  818. dma_init(new_fsl_chan);
  819. /* Clear cdar registers */
  820. set_cdar(new_fsl_chan, 0);
  821. switch (new_fsl_chan->feature & FSL_DMA_IP_MASK) {
  822. case FSL_DMA_IP_85XX:
  823. new_fsl_chan->toggle_ext_start = fsl_chan_toggle_ext_start;
  824. new_fsl_chan->toggle_ext_pause = fsl_chan_toggle_ext_pause;
  825. case FSL_DMA_IP_83XX:
  826. new_fsl_chan->set_src_loop_size = fsl_chan_set_src_loop_size;
  827. new_fsl_chan->set_dest_loop_size = fsl_chan_set_dest_loop_size;
  828. }
  829. spin_lock_init(&new_fsl_chan->desc_lock);
  830. INIT_LIST_HEAD(&new_fsl_chan->ld_queue);
  831. new_fsl_chan->common.device = &fdev->common;
  832. /* Add the channel to DMA device channel list */
  833. list_add_tail(&new_fsl_chan->common.device_node,
  834. &fdev->common.channels);
  835. fdev->common.chancnt++;
  836. new_fsl_chan->irq = irq_of_parse_and_map(dev->node, 0);
  837. if (new_fsl_chan->irq != NO_IRQ) {
  838. err = request_irq(new_fsl_chan->irq,
  839. &fsl_dma_chan_do_interrupt, IRQF_SHARED,
  840. "fsldma-channel", new_fsl_chan);
  841. if (err) {
  842. dev_err(&dev->dev, "DMA channel %s request_irq error "
  843. "with return %d\n", dev->node->full_name, err);
  844. goto err_no_irq;
  845. }
  846. }
  847. err = fsl_dma_self_test(new_fsl_chan);
  848. if (err)
  849. goto err_self_test;
  850. dev_info(&dev->dev, "#%d (%s), irq %d\n", new_fsl_chan->id,
  851. match->compatible, new_fsl_chan->irq);
  852. return 0;
  853. err_self_test:
  854. free_irq(new_fsl_chan->irq, new_fsl_chan);
  855. err_no_irq:
  856. list_del(&new_fsl_chan->common.device_node);
  857. err_no_chan:
  858. iounmap(new_fsl_chan->reg_base);
  859. err_no_reg:
  860. kfree(new_fsl_chan);
  861. return err;
  862. }
  863. const u32 mpc8540_dma_ip_feature = FSL_DMA_IP_85XX | FSL_DMA_BIG_ENDIAN;
  864. const u32 mpc8349_dma_ip_feature = FSL_DMA_IP_83XX | FSL_DMA_LITTLE_ENDIAN;
  865. static struct of_device_id of_fsl_dma_chan_ids[] = {
  866. {
  867. .compatible = "fsl,eloplus-dma-channel",
  868. .data = (void *)&mpc8540_dma_ip_feature,
  869. },
  870. {
  871. .compatible = "fsl,elo-dma-channel",
  872. .data = (void *)&mpc8349_dma_ip_feature,
  873. },
  874. {}
  875. };
  876. static struct of_platform_driver of_fsl_dma_chan_driver = {
  877. .name = "of-fsl-dma-channel",
  878. .match_table = of_fsl_dma_chan_ids,
  879. .probe = of_fsl_dma_chan_probe,
  880. };
  881. static __init int of_fsl_dma_chan_init(void)
  882. {
  883. return of_register_platform_driver(&of_fsl_dma_chan_driver);
  884. }
  885. static int __devinit of_fsl_dma_probe(struct of_device *dev,
  886. const struct of_device_id *match)
  887. {
  888. int err;
  889. unsigned int irq;
  890. struct fsl_dma_device *fdev;
  891. fdev = kzalloc(sizeof(struct fsl_dma_device), GFP_KERNEL);
  892. if (!fdev) {
  893. dev_err(&dev->dev, "No enough memory for 'priv'\n");
  894. return -ENOMEM;
  895. }
  896. fdev->dev = &dev->dev;
  897. INIT_LIST_HEAD(&fdev->common.channels);
  898. /* get DMA controller register base */
  899. err = of_address_to_resource(dev->node, 0, &fdev->reg);
  900. if (err) {
  901. dev_err(&dev->dev, "Can't get %s property 'reg'\n",
  902. dev->node->full_name);
  903. goto err_no_reg;
  904. }
  905. dev_info(&dev->dev, "Probe the Freescale DMA driver for %s "
  906. "controller at %p...\n",
  907. match->compatible, (void *)fdev->reg.start);
  908. fdev->reg_base = ioremap(fdev->reg.start, fdev->reg.end
  909. - fdev->reg.start + 1);
  910. dma_cap_set(DMA_MEMCPY, fdev->common.cap_mask);
  911. dma_cap_set(DMA_INTERRUPT, fdev->common.cap_mask);
  912. fdev->common.device_alloc_chan_resources = fsl_dma_alloc_chan_resources;
  913. fdev->common.device_free_chan_resources = fsl_dma_free_chan_resources;
  914. fdev->common.device_prep_dma_interrupt = fsl_dma_prep_interrupt;
  915. fdev->common.device_prep_dma_memcpy = fsl_dma_prep_memcpy;
  916. fdev->common.device_is_tx_complete = fsl_dma_is_complete;
  917. fdev->common.device_issue_pending = fsl_dma_memcpy_issue_pending;
  918. fdev->common.dev = &dev->dev;
  919. irq = irq_of_parse_and_map(dev->node, 0);
  920. if (irq != NO_IRQ) {
  921. err = request_irq(irq, &fsl_dma_do_interrupt, IRQF_SHARED,
  922. "fsldma-device", fdev);
  923. if (err) {
  924. dev_err(&dev->dev, "DMA device request_irq error "
  925. "with return %d\n", err);
  926. goto err;
  927. }
  928. }
  929. dev_set_drvdata(&(dev->dev), fdev);
  930. of_platform_bus_probe(dev->node, of_fsl_dma_chan_ids, &dev->dev);
  931. dma_async_device_register(&fdev->common);
  932. return 0;
  933. err:
  934. iounmap(fdev->reg_base);
  935. err_no_reg:
  936. kfree(fdev);
  937. return err;
  938. }
  939. static struct of_device_id of_fsl_dma_ids[] = {
  940. { .compatible = "fsl,eloplus-dma", },
  941. { .compatible = "fsl,elo-dma", },
  942. {}
  943. };
  944. static struct of_platform_driver of_fsl_dma_driver = {
  945. .name = "of-fsl-dma",
  946. .match_table = of_fsl_dma_ids,
  947. .probe = of_fsl_dma_probe,
  948. };
  949. static __init int of_fsl_dma_init(void)
  950. {
  951. return of_register_platform_driver(&of_fsl_dma_driver);
  952. }
  953. subsys_initcall(of_fsl_dma_chan_init);
  954. subsys_initcall(of_fsl_dma_init);