fsldma.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  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 driver instructs the DMA controller to issue the PCI Read Multiple
  16. * command for PCI read operations, instead of using the default PCI Read Line
  17. * command. Please be aware that this setting may result in read pre-fetching
  18. * on some platforms.
  19. *
  20. * This is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation; either version 2 of the License, or
  23. * (at your option) any later version.
  24. *
  25. */
  26. #include <linux/init.h>
  27. #include <linux/module.h>
  28. #include <linux/pci.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/dmaengine.h>
  31. #include <linux/delay.h>
  32. #include <linux/dma-mapping.h>
  33. #include <linux/dmapool.h>
  34. #include <linux/of_platform.h>
  35. #include <asm/fsldma.h>
  36. #include "fsldma.h"
  37. static void dma_init(struct fsl_dma_chan *fsl_chan)
  38. {
  39. /* Reset the channel */
  40. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr, 0, 32);
  41. switch (fsl_chan->feature & FSL_DMA_IP_MASK) {
  42. case FSL_DMA_IP_85XX:
  43. /* Set the channel to below modes:
  44. * EIE - Error interrupt enable
  45. * EOSIE - End of segments interrupt enable (basic mode)
  46. * EOLNIE - End of links interrupt enable
  47. */
  48. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr, FSL_DMA_MR_EIE
  49. | FSL_DMA_MR_EOLNIE | FSL_DMA_MR_EOSIE, 32);
  50. break;
  51. case FSL_DMA_IP_83XX:
  52. /* Set the channel to below modes:
  53. * EOTIE - End-of-transfer interrupt enable
  54. * PRC_RM - PCI read multiple
  55. */
  56. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr, FSL_DMA_MR_EOTIE
  57. | FSL_DMA_MR_PRC_RM, 32);
  58. break;
  59. }
  60. }
  61. static void set_sr(struct fsl_dma_chan *fsl_chan, u32 val)
  62. {
  63. DMA_OUT(fsl_chan, &fsl_chan->reg_base->sr, val, 32);
  64. }
  65. static u32 get_sr(struct fsl_dma_chan *fsl_chan)
  66. {
  67. return DMA_IN(fsl_chan, &fsl_chan->reg_base->sr, 32);
  68. }
  69. static void set_desc_cnt(struct fsl_dma_chan *fsl_chan,
  70. struct fsl_dma_ld_hw *hw, u32 count)
  71. {
  72. hw->count = CPU_TO_DMA(fsl_chan, count, 32);
  73. }
  74. static void set_desc_src(struct fsl_dma_chan *fsl_chan,
  75. struct fsl_dma_ld_hw *hw, dma_addr_t src)
  76. {
  77. u64 snoop_bits;
  78. snoop_bits = ((fsl_chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_85XX)
  79. ? ((u64)FSL_DMA_SATR_SREADTYPE_SNOOP_READ << 32) : 0;
  80. hw->src_addr = CPU_TO_DMA(fsl_chan, snoop_bits | src, 64);
  81. }
  82. static void set_desc_dest(struct fsl_dma_chan *fsl_chan,
  83. struct fsl_dma_ld_hw *hw, dma_addr_t dest)
  84. {
  85. u64 snoop_bits;
  86. snoop_bits = ((fsl_chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_85XX)
  87. ? ((u64)FSL_DMA_DATR_DWRITETYPE_SNOOP_WRITE << 32) : 0;
  88. hw->dst_addr = CPU_TO_DMA(fsl_chan, snoop_bits | dest, 64);
  89. }
  90. static void set_desc_next(struct fsl_dma_chan *fsl_chan,
  91. struct fsl_dma_ld_hw *hw, dma_addr_t next)
  92. {
  93. u64 snoop_bits;
  94. snoop_bits = ((fsl_chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_83XX)
  95. ? FSL_DMA_SNEN : 0;
  96. hw->next_ln_addr = CPU_TO_DMA(fsl_chan, snoop_bits | next, 64);
  97. }
  98. static void set_cdar(struct fsl_dma_chan *fsl_chan, dma_addr_t addr)
  99. {
  100. DMA_OUT(fsl_chan, &fsl_chan->reg_base->cdar, addr | FSL_DMA_SNEN, 64);
  101. }
  102. static dma_addr_t get_cdar(struct fsl_dma_chan *fsl_chan)
  103. {
  104. return DMA_IN(fsl_chan, &fsl_chan->reg_base->cdar, 64) & ~FSL_DMA_SNEN;
  105. }
  106. static void set_ndar(struct fsl_dma_chan *fsl_chan, dma_addr_t addr)
  107. {
  108. DMA_OUT(fsl_chan, &fsl_chan->reg_base->ndar, addr, 64);
  109. }
  110. static dma_addr_t get_ndar(struct fsl_dma_chan *fsl_chan)
  111. {
  112. return DMA_IN(fsl_chan, &fsl_chan->reg_base->ndar, 64);
  113. }
  114. static u32 get_bcr(struct fsl_dma_chan *fsl_chan)
  115. {
  116. return DMA_IN(fsl_chan, &fsl_chan->reg_base->bcr, 32);
  117. }
  118. static int dma_is_idle(struct fsl_dma_chan *fsl_chan)
  119. {
  120. u32 sr = get_sr(fsl_chan);
  121. return (!(sr & FSL_DMA_SR_CB)) || (sr & FSL_DMA_SR_CH);
  122. }
  123. static void dma_start(struct fsl_dma_chan *fsl_chan)
  124. {
  125. u32 mr_set = 0;
  126. if (fsl_chan->feature & FSL_DMA_CHAN_PAUSE_EXT) {
  127. DMA_OUT(fsl_chan, &fsl_chan->reg_base->bcr, 0, 32);
  128. mr_set |= FSL_DMA_MR_EMP_EN;
  129. } else if ((fsl_chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_85XX) {
  130. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr,
  131. DMA_IN(fsl_chan, &fsl_chan->reg_base->mr, 32)
  132. & ~FSL_DMA_MR_EMP_EN, 32);
  133. }
  134. if (fsl_chan->feature & FSL_DMA_CHAN_START_EXT)
  135. mr_set |= FSL_DMA_MR_EMS_EN;
  136. else
  137. mr_set |= FSL_DMA_MR_CS;
  138. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr,
  139. DMA_IN(fsl_chan, &fsl_chan->reg_base->mr, 32)
  140. | mr_set, 32);
  141. }
  142. static void dma_halt(struct fsl_dma_chan *fsl_chan)
  143. {
  144. int i;
  145. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr,
  146. DMA_IN(fsl_chan, &fsl_chan->reg_base->mr, 32) | FSL_DMA_MR_CA,
  147. 32);
  148. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr,
  149. DMA_IN(fsl_chan, &fsl_chan->reg_base->mr, 32) & ~(FSL_DMA_MR_CS
  150. | FSL_DMA_MR_EMS_EN | FSL_DMA_MR_CA), 32);
  151. for (i = 0; i < 100; i++) {
  152. if (dma_is_idle(fsl_chan))
  153. break;
  154. udelay(10);
  155. }
  156. if (i >= 100 && !dma_is_idle(fsl_chan))
  157. dev_err(fsl_chan->dev, "DMA halt timeout!\n");
  158. }
  159. static void set_ld_eol(struct fsl_dma_chan *fsl_chan,
  160. struct fsl_desc_sw *desc)
  161. {
  162. u64 snoop_bits;
  163. snoop_bits = ((fsl_chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_83XX)
  164. ? FSL_DMA_SNEN : 0;
  165. desc->hw.next_ln_addr = CPU_TO_DMA(fsl_chan,
  166. DMA_TO_CPU(fsl_chan, desc->hw.next_ln_addr, 64) | FSL_DMA_EOL
  167. | snoop_bits, 64);
  168. }
  169. static void append_ld_queue(struct fsl_dma_chan *fsl_chan,
  170. struct fsl_desc_sw *new_desc)
  171. {
  172. struct fsl_desc_sw *queue_tail = to_fsl_desc(fsl_chan->ld_queue.prev);
  173. if (list_empty(&fsl_chan->ld_queue))
  174. return;
  175. /* Link to the new descriptor physical address and
  176. * Enable End-of-segment interrupt for
  177. * the last link descriptor.
  178. * (the previous node's next link descriptor)
  179. *
  180. * For FSL_DMA_IP_83xx, the snoop enable bit need be set.
  181. */
  182. queue_tail->hw.next_ln_addr = CPU_TO_DMA(fsl_chan,
  183. new_desc->async_tx.phys | FSL_DMA_EOSIE |
  184. (((fsl_chan->feature & FSL_DMA_IP_MASK)
  185. == FSL_DMA_IP_83XX) ? FSL_DMA_SNEN : 0), 64);
  186. }
  187. /**
  188. * fsl_chan_set_src_loop_size - Set source address hold transfer size
  189. * @fsl_chan : Freescale DMA channel
  190. * @size : Address loop size, 0 for disable loop
  191. *
  192. * The set source address hold transfer size. The source
  193. * address hold or loop transfer size is when the DMA transfer
  194. * data from source address (SA), if the loop size is 4, the DMA will
  195. * read data from SA, SA + 1, SA + 2, SA + 3, then loop back to SA,
  196. * SA + 1 ... and so on.
  197. */
  198. static void fsl_chan_set_src_loop_size(struct fsl_dma_chan *fsl_chan, int size)
  199. {
  200. switch (size) {
  201. case 0:
  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), 32);
  205. break;
  206. case 1:
  207. case 2:
  208. case 4:
  209. case 8:
  210. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr,
  211. DMA_IN(fsl_chan, &fsl_chan->reg_base->mr, 32) |
  212. FSL_DMA_MR_SAHE | (__ilog2(size) << 14),
  213. 32);
  214. break;
  215. }
  216. }
  217. /**
  218. * fsl_chan_set_dest_loop_size - Set destination address hold transfer size
  219. * @fsl_chan : Freescale DMA channel
  220. * @size : Address loop size, 0 for disable loop
  221. *
  222. * The set destination address hold transfer size. The destination
  223. * address hold or loop transfer size is when the DMA transfer
  224. * data to destination address (TA), if the loop size is 4, the DMA will
  225. * write data to TA, TA + 1, TA + 2, TA + 3, then loop back to TA,
  226. * TA + 1 ... and so on.
  227. */
  228. static void fsl_chan_set_dest_loop_size(struct fsl_dma_chan *fsl_chan, int size)
  229. {
  230. switch (size) {
  231. case 0:
  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), 32);
  235. break;
  236. case 1:
  237. case 2:
  238. case 4:
  239. case 8:
  240. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr,
  241. DMA_IN(fsl_chan, &fsl_chan->reg_base->mr, 32) |
  242. FSL_DMA_MR_DAHE | (__ilog2(size) << 16),
  243. 32);
  244. break;
  245. }
  246. }
  247. /**
  248. * fsl_chan_set_request_count - Set DMA Request Count for external control
  249. * @fsl_chan : Freescale DMA channel
  250. * @size : Number of bytes to transfer in a single request
  251. *
  252. * The Freescale DMA channel can be controlled by the external signal DREQ#.
  253. * The DMA request count is how many bytes are allowed to transfer before
  254. * pausing the channel, after which a new assertion of DREQ# resumes channel
  255. * operation.
  256. *
  257. * A size of 0 disables external pause control. The maximum size is 1024.
  258. */
  259. static void fsl_chan_set_request_count(struct fsl_dma_chan *fsl_chan, int size)
  260. {
  261. BUG_ON(size > 1024);
  262. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr,
  263. DMA_IN(fsl_chan, &fsl_chan->reg_base->mr, 32)
  264. | ((__ilog2(size) << 24) & 0x0f000000),
  265. 32);
  266. }
  267. /**
  268. * fsl_chan_toggle_ext_pause - Toggle channel external pause status
  269. * @fsl_chan : Freescale DMA channel
  270. * @enable : 0 is disabled, 1 is enabled.
  271. *
  272. * The Freescale DMA channel can be controlled by the external signal DREQ#.
  273. * The DMA Request Count feature should be used in addition to this feature
  274. * to set the number of bytes to transfer before pausing the channel.
  275. */
  276. static void fsl_chan_toggle_ext_pause(struct fsl_dma_chan *fsl_chan, int enable)
  277. {
  278. if (enable)
  279. fsl_chan->feature |= FSL_DMA_CHAN_PAUSE_EXT;
  280. else
  281. fsl_chan->feature &= ~FSL_DMA_CHAN_PAUSE_EXT;
  282. }
  283. /**
  284. * fsl_chan_toggle_ext_start - Toggle channel external start status
  285. * @fsl_chan : Freescale DMA channel
  286. * @enable : 0 is disabled, 1 is enabled.
  287. *
  288. * If enable the external start, the channel can be started by an
  289. * external DMA start pin. So the dma_start() does not start the
  290. * transfer immediately. The DMA channel will wait for the
  291. * control pin asserted.
  292. */
  293. static void fsl_chan_toggle_ext_start(struct fsl_dma_chan *fsl_chan, int enable)
  294. {
  295. if (enable)
  296. fsl_chan->feature |= FSL_DMA_CHAN_START_EXT;
  297. else
  298. fsl_chan->feature &= ~FSL_DMA_CHAN_START_EXT;
  299. }
  300. static dma_cookie_t fsl_dma_tx_submit(struct dma_async_tx_descriptor *tx)
  301. {
  302. struct fsl_dma_chan *fsl_chan = to_fsl_chan(tx->chan);
  303. struct fsl_desc_sw *desc = tx_to_fsl_desc(tx);
  304. struct fsl_desc_sw *child;
  305. unsigned long flags;
  306. dma_cookie_t cookie;
  307. /* cookie increment and adding to ld_queue must be atomic */
  308. spin_lock_irqsave(&fsl_chan->desc_lock, flags);
  309. cookie = fsl_chan->common.cookie;
  310. list_for_each_entry(child, &desc->tx_list, node) {
  311. cookie++;
  312. if (cookie < 0)
  313. cookie = 1;
  314. desc->async_tx.cookie = cookie;
  315. }
  316. fsl_chan->common.cookie = cookie;
  317. append_ld_queue(fsl_chan, desc);
  318. list_splice_init(&desc->tx_list, fsl_chan->ld_queue.prev);
  319. spin_unlock_irqrestore(&fsl_chan->desc_lock, flags);
  320. return cookie;
  321. }
  322. /**
  323. * fsl_dma_alloc_descriptor - Allocate descriptor from channel's DMA pool.
  324. * @fsl_chan : Freescale DMA channel
  325. *
  326. * Return - The descriptor allocated. NULL for failed.
  327. */
  328. static struct fsl_desc_sw *fsl_dma_alloc_descriptor(
  329. struct fsl_dma_chan *fsl_chan)
  330. {
  331. dma_addr_t pdesc;
  332. struct fsl_desc_sw *desc_sw;
  333. desc_sw = dma_pool_alloc(fsl_chan->desc_pool, GFP_ATOMIC, &pdesc);
  334. if (desc_sw) {
  335. memset(desc_sw, 0, sizeof(struct fsl_desc_sw));
  336. INIT_LIST_HEAD(&desc_sw->tx_list);
  337. dma_async_tx_descriptor_init(&desc_sw->async_tx,
  338. &fsl_chan->common);
  339. desc_sw->async_tx.tx_submit = fsl_dma_tx_submit;
  340. desc_sw->async_tx.phys = pdesc;
  341. }
  342. return desc_sw;
  343. }
  344. /**
  345. * fsl_dma_alloc_chan_resources - Allocate resources for DMA channel.
  346. * @fsl_chan : Freescale DMA channel
  347. *
  348. * This function will create a dma pool for descriptor allocation.
  349. *
  350. * Return - The number of descriptors allocated.
  351. */
  352. static int fsl_dma_alloc_chan_resources(struct dma_chan *chan)
  353. {
  354. struct fsl_dma_chan *fsl_chan = to_fsl_chan(chan);
  355. /* Has this channel already been allocated? */
  356. if (fsl_chan->desc_pool)
  357. return 1;
  358. /* We need the descriptor to be aligned to 32bytes
  359. * for meeting FSL DMA specification requirement.
  360. */
  361. fsl_chan->desc_pool = dma_pool_create("fsl_dma_engine_desc_pool",
  362. fsl_chan->dev, sizeof(struct fsl_desc_sw),
  363. 32, 0);
  364. if (!fsl_chan->desc_pool) {
  365. dev_err(fsl_chan->dev, "No memory for channel %d "
  366. "descriptor dma pool.\n", fsl_chan->id);
  367. return 0;
  368. }
  369. return 1;
  370. }
  371. /**
  372. * fsl_dma_free_chan_resources - Free all resources of the channel.
  373. * @fsl_chan : Freescale DMA channel
  374. */
  375. static void fsl_dma_free_chan_resources(struct dma_chan *chan)
  376. {
  377. struct fsl_dma_chan *fsl_chan = to_fsl_chan(chan);
  378. struct fsl_desc_sw *desc, *_desc;
  379. unsigned long flags;
  380. dev_dbg(fsl_chan->dev, "Free all channel resources.\n");
  381. spin_lock_irqsave(&fsl_chan->desc_lock, flags);
  382. list_for_each_entry_safe(desc, _desc, &fsl_chan->ld_queue, node) {
  383. #ifdef FSL_DMA_LD_DEBUG
  384. dev_dbg(fsl_chan->dev,
  385. "LD %p will be released.\n", desc);
  386. #endif
  387. list_del(&desc->node);
  388. /* free link descriptor */
  389. dma_pool_free(fsl_chan->desc_pool, desc, desc->async_tx.phys);
  390. }
  391. spin_unlock_irqrestore(&fsl_chan->desc_lock, flags);
  392. dma_pool_destroy(fsl_chan->desc_pool);
  393. fsl_chan->desc_pool = NULL;
  394. }
  395. static struct dma_async_tx_descriptor *
  396. fsl_dma_prep_interrupt(struct dma_chan *chan, unsigned long flags)
  397. {
  398. struct fsl_dma_chan *fsl_chan;
  399. struct fsl_desc_sw *new;
  400. if (!chan)
  401. return NULL;
  402. fsl_chan = to_fsl_chan(chan);
  403. new = fsl_dma_alloc_descriptor(fsl_chan);
  404. if (!new) {
  405. dev_err(fsl_chan->dev, "No free memory for link descriptor\n");
  406. return NULL;
  407. }
  408. new->async_tx.cookie = -EBUSY;
  409. new->async_tx.flags = flags;
  410. /* Insert the link descriptor to the LD ring */
  411. list_add_tail(&new->node, &new->tx_list);
  412. /* Set End-of-link to the last link descriptor of new list*/
  413. set_ld_eol(fsl_chan, new);
  414. return &new->async_tx;
  415. }
  416. static struct dma_async_tx_descriptor *fsl_dma_prep_memcpy(
  417. struct dma_chan *chan, dma_addr_t dma_dest, dma_addr_t dma_src,
  418. size_t len, unsigned long flags)
  419. {
  420. struct fsl_dma_chan *fsl_chan;
  421. struct fsl_desc_sw *first = NULL, *prev = NULL, *new;
  422. struct list_head *list;
  423. size_t copy;
  424. if (!chan)
  425. return NULL;
  426. if (!len)
  427. return NULL;
  428. fsl_chan = to_fsl_chan(chan);
  429. do {
  430. /* Allocate the link descriptor from DMA pool */
  431. new = fsl_dma_alloc_descriptor(fsl_chan);
  432. if (!new) {
  433. dev_err(fsl_chan->dev,
  434. "No free memory for link descriptor\n");
  435. goto fail;
  436. }
  437. #ifdef FSL_DMA_LD_DEBUG
  438. dev_dbg(fsl_chan->dev, "new link desc alloc %p\n", new);
  439. #endif
  440. copy = min(len, (size_t)FSL_DMA_BCR_MAX_CNT);
  441. set_desc_cnt(fsl_chan, &new->hw, copy);
  442. set_desc_src(fsl_chan, &new->hw, dma_src);
  443. set_desc_dest(fsl_chan, &new->hw, dma_dest);
  444. if (!first)
  445. first = new;
  446. else
  447. set_desc_next(fsl_chan, &prev->hw, new->async_tx.phys);
  448. new->async_tx.cookie = 0;
  449. async_tx_ack(&new->async_tx);
  450. prev = new;
  451. len -= copy;
  452. dma_src += copy;
  453. dma_dest += copy;
  454. /* Insert the link descriptor to the LD ring */
  455. list_add_tail(&new->node, &first->tx_list);
  456. } while (len);
  457. new->async_tx.flags = flags; /* client is in control of this ack */
  458. new->async_tx.cookie = -EBUSY;
  459. /* Set End-of-link to the last link descriptor of new list*/
  460. set_ld_eol(fsl_chan, new);
  461. return &first->async_tx;
  462. fail:
  463. if (!first)
  464. return NULL;
  465. list = &first->tx_list;
  466. list_for_each_entry_safe_reverse(new, prev, list, node) {
  467. list_del(&new->node);
  468. dma_pool_free(fsl_chan->desc_pool, new, new->async_tx.phys);
  469. }
  470. return NULL;
  471. }
  472. /**
  473. * fsl_dma_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
  474. * @chan: DMA channel
  475. * @sgl: scatterlist to transfer to/from
  476. * @sg_len: number of entries in @scatterlist
  477. * @direction: DMA direction
  478. * @flags: DMAEngine flags
  479. *
  480. * Prepare a set of descriptors for a DMA_SLAVE transaction. Following the
  481. * DMA_SLAVE API, this gets the device-specific information from the
  482. * chan->private variable.
  483. */
  484. static struct dma_async_tx_descriptor *fsl_dma_prep_slave_sg(
  485. struct dma_chan *chan, struct scatterlist *sgl, unsigned int sg_len,
  486. enum dma_data_direction direction, unsigned long flags)
  487. {
  488. struct fsl_dma_chan *fsl_chan;
  489. struct fsl_desc_sw *first = NULL, *prev = NULL, *new = NULL;
  490. struct fsl_dma_slave *slave;
  491. struct list_head *tx_list;
  492. size_t copy;
  493. int i;
  494. struct scatterlist *sg;
  495. size_t sg_used;
  496. size_t hw_used;
  497. struct fsl_dma_hw_addr *hw;
  498. dma_addr_t dma_dst, dma_src;
  499. if (!chan)
  500. return NULL;
  501. if (!chan->private)
  502. return NULL;
  503. fsl_chan = to_fsl_chan(chan);
  504. slave = chan->private;
  505. if (list_empty(&slave->addresses))
  506. return NULL;
  507. hw = list_first_entry(&slave->addresses, struct fsl_dma_hw_addr, entry);
  508. hw_used = 0;
  509. /*
  510. * Build the hardware transaction to copy from the scatterlist to
  511. * the hardware, or from the hardware to the scatterlist
  512. *
  513. * If you are copying from the hardware to the scatterlist and it
  514. * takes two hardware entries to fill an entire page, then both
  515. * hardware entries will be coalesced into the same page
  516. *
  517. * If you are copying from the scatterlist to the hardware and a
  518. * single page can fill two hardware entries, then the data will
  519. * be read out of the page into the first hardware entry, and so on
  520. */
  521. for_each_sg(sgl, sg, sg_len, i) {
  522. sg_used = 0;
  523. /* Loop until the entire scatterlist entry is used */
  524. while (sg_used < sg_dma_len(sg)) {
  525. /*
  526. * If we've used up the current hardware address/length
  527. * pair, we need to load a new one
  528. *
  529. * This is done in a while loop so that descriptors with
  530. * length == 0 will be skipped
  531. */
  532. while (hw_used >= hw->length) {
  533. /*
  534. * If the current hardware entry is the last
  535. * entry in the list, we're finished
  536. */
  537. if (list_is_last(&hw->entry, &slave->addresses))
  538. goto finished;
  539. /* Get the next hardware address/length pair */
  540. hw = list_entry(hw->entry.next,
  541. struct fsl_dma_hw_addr, entry);
  542. hw_used = 0;
  543. }
  544. /* Allocate the link descriptor from DMA pool */
  545. new = fsl_dma_alloc_descriptor(fsl_chan);
  546. if (!new) {
  547. dev_err(fsl_chan->dev, "No free memory for "
  548. "link descriptor\n");
  549. goto fail;
  550. }
  551. #ifdef FSL_DMA_LD_DEBUG
  552. dev_dbg(fsl_chan->dev, "new link desc alloc %p\n", new);
  553. #endif
  554. /*
  555. * Calculate the maximum number of bytes to transfer,
  556. * making sure it is less than the DMA controller limit
  557. */
  558. copy = min_t(size_t, sg_dma_len(sg) - sg_used,
  559. hw->length - hw_used);
  560. copy = min_t(size_t, copy, FSL_DMA_BCR_MAX_CNT);
  561. /*
  562. * DMA_FROM_DEVICE
  563. * from the hardware to the scatterlist
  564. *
  565. * DMA_TO_DEVICE
  566. * from the scatterlist to the hardware
  567. */
  568. if (direction == DMA_FROM_DEVICE) {
  569. dma_src = hw->address + hw_used;
  570. dma_dst = sg_dma_address(sg) + sg_used;
  571. } else {
  572. dma_src = sg_dma_address(sg) + sg_used;
  573. dma_dst = hw->address + hw_used;
  574. }
  575. /* Fill in the descriptor */
  576. set_desc_cnt(fsl_chan, &new->hw, copy);
  577. set_desc_src(fsl_chan, &new->hw, dma_src);
  578. set_desc_dest(fsl_chan, &new->hw, dma_dst);
  579. /*
  580. * If this is not the first descriptor, chain the
  581. * current descriptor after the previous descriptor
  582. */
  583. if (!first) {
  584. first = new;
  585. } else {
  586. set_desc_next(fsl_chan, &prev->hw,
  587. new->async_tx.phys);
  588. }
  589. new->async_tx.cookie = 0;
  590. async_tx_ack(&new->async_tx);
  591. prev = new;
  592. sg_used += copy;
  593. hw_used += copy;
  594. /* Insert the link descriptor into the LD ring */
  595. list_add_tail(&new->node, &first->tx_list);
  596. }
  597. }
  598. finished:
  599. /* All of the hardware address/length pairs had length == 0 */
  600. if (!first || !new)
  601. return NULL;
  602. new->async_tx.flags = flags;
  603. new->async_tx.cookie = -EBUSY;
  604. /* Set End-of-link to the last link descriptor of new list */
  605. set_ld_eol(fsl_chan, new);
  606. /* Enable extra controller features */
  607. if (fsl_chan->set_src_loop_size)
  608. fsl_chan->set_src_loop_size(fsl_chan, slave->src_loop_size);
  609. if (fsl_chan->set_dest_loop_size)
  610. fsl_chan->set_dest_loop_size(fsl_chan, slave->dst_loop_size);
  611. if (fsl_chan->toggle_ext_start)
  612. fsl_chan->toggle_ext_start(fsl_chan, slave->external_start);
  613. if (fsl_chan->toggle_ext_pause)
  614. fsl_chan->toggle_ext_pause(fsl_chan, slave->external_pause);
  615. if (fsl_chan->set_request_count)
  616. fsl_chan->set_request_count(fsl_chan, slave->request_count);
  617. return &first->async_tx;
  618. fail:
  619. /* If first was not set, then we failed to allocate the very first
  620. * descriptor, and we're done */
  621. if (!first)
  622. return NULL;
  623. /*
  624. * First is set, so all of the descriptors we allocated have been added
  625. * to first->tx_list, INCLUDING "first" itself. Therefore we
  626. * must traverse the list backwards freeing each descriptor in turn
  627. *
  628. * We're re-using variables for the loop, oh well
  629. */
  630. tx_list = &first->tx_list;
  631. list_for_each_entry_safe_reverse(new, prev, tx_list, node) {
  632. list_del_init(&new->node);
  633. dma_pool_free(fsl_chan->desc_pool, new, new->async_tx.phys);
  634. }
  635. return NULL;
  636. }
  637. static void fsl_dma_device_terminate_all(struct dma_chan *chan)
  638. {
  639. struct fsl_dma_chan *fsl_chan;
  640. struct fsl_desc_sw *desc, *tmp;
  641. unsigned long flags;
  642. if (!chan)
  643. return;
  644. fsl_chan = to_fsl_chan(chan);
  645. /* Halt the DMA engine */
  646. dma_halt(fsl_chan);
  647. spin_lock_irqsave(&fsl_chan->desc_lock, flags);
  648. /* Remove and free all of the descriptors in the LD queue */
  649. list_for_each_entry_safe(desc, tmp, &fsl_chan->ld_queue, node) {
  650. list_del(&desc->node);
  651. dma_pool_free(fsl_chan->desc_pool, desc, desc->async_tx.phys);
  652. }
  653. spin_unlock_irqrestore(&fsl_chan->desc_lock, flags);
  654. }
  655. /**
  656. * fsl_dma_update_completed_cookie - Update the completed cookie.
  657. * @fsl_chan : Freescale DMA channel
  658. */
  659. static void fsl_dma_update_completed_cookie(struct fsl_dma_chan *fsl_chan)
  660. {
  661. struct fsl_desc_sw *cur_desc, *desc;
  662. dma_addr_t ld_phy;
  663. ld_phy = get_cdar(fsl_chan) & FSL_DMA_NLDA_MASK;
  664. if (ld_phy) {
  665. cur_desc = NULL;
  666. list_for_each_entry(desc, &fsl_chan->ld_queue, node)
  667. if (desc->async_tx.phys == ld_phy) {
  668. cur_desc = desc;
  669. break;
  670. }
  671. if (cur_desc && cur_desc->async_tx.cookie) {
  672. if (dma_is_idle(fsl_chan))
  673. fsl_chan->completed_cookie =
  674. cur_desc->async_tx.cookie;
  675. else
  676. fsl_chan->completed_cookie =
  677. cur_desc->async_tx.cookie - 1;
  678. }
  679. }
  680. }
  681. /**
  682. * fsl_chan_ld_cleanup - Clean up link descriptors
  683. * @fsl_chan : Freescale DMA channel
  684. *
  685. * This function clean up the ld_queue of DMA channel.
  686. * If 'in_intr' is set, the function will move the link descriptor to
  687. * the recycle list. Otherwise, free it directly.
  688. */
  689. static void fsl_chan_ld_cleanup(struct fsl_dma_chan *fsl_chan)
  690. {
  691. struct fsl_desc_sw *desc, *_desc;
  692. unsigned long flags;
  693. spin_lock_irqsave(&fsl_chan->desc_lock, flags);
  694. dev_dbg(fsl_chan->dev, "chan completed_cookie = %d\n",
  695. fsl_chan->completed_cookie);
  696. list_for_each_entry_safe(desc, _desc, &fsl_chan->ld_queue, node) {
  697. dma_async_tx_callback callback;
  698. void *callback_param;
  699. if (dma_async_is_complete(desc->async_tx.cookie,
  700. fsl_chan->completed_cookie, fsl_chan->common.cookie)
  701. == DMA_IN_PROGRESS)
  702. break;
  703. callback = desc->async_tx.callback;
  704. callback_param = desc->async_tx.callback_param;
  705. /* Remove from ld_queue list */
  706. list_del(&desc->node);
  707. dev_dbg(fsl_chan->dev, "link descriptor %p will be recycle.\n",
  708. desc);
  709. dma_pool_free(fsl_chan->desc_pool, desc, desc->async_tx.phys);
  710. /* Run the link descriptor callback function */
  711. if (callback) {
  712. spin_unlock_irqrestore(&fsl_chan->desc_lock, flags);
  713. dev_dbg(fsl_chan->dev, "link descriptor %p callback\n",
  714. desc);
  715. callback(callback_param);
  716. spin_lock_irqsave(&fsl_chan->desc_lock, flags);
  717. }
  718. }
  719. spin_unlock_irqrestore(&fsl_chan->desc_lock, flags);
  720. }
  721. /**
  722. * fsl_chan_xfer_ld_queue - Transfer link descriptors in channel ld_queue.
  723. * @fsl_chan : Freescale DMA channel
  724. */
  725. static void fsl_chan_xfer_ld_queue(struct fsl_dma_chan *fsl_chan)
  726. {
  727. struct list_head *ld_node;
  728. dma_addr_t next_dest_addr;
  729. unsigned long flags;
  730. spin_lock_irqsave(&fsl_chan->desc_lock, flags);
  731. if (!dma_is_idle(fsl_chan))
  732. goto out_unlock;
  733. dma_halt(fsl_chan);
  734. /* If there are some link descriptors
  735. * not transfered in queue. We need to start it.
  736. */
  737. /* Find the first un-transfer desciptor */
  738. for (ld_node = fsl_chan->ld_queue.next;
  739. (ld_node != &fsl_chan->ld_queue)
  740. && (dma_async_is_complete(
  741. to_fsl_desc(ld_node)->async_tx.cookie,
  742. fsl_chan->completed_cookie,
  743. fsl_chan->common.cookie) == DMA_SUCCESS);
  744. ld_node = ld_node->next);
  745. if (ld_node != &fsl_chan->ld_queue) {
  746. /* Get the ld start address from ld_queue */
  747. next_dest_addr = to_fsl_desc(ld_node)->async_tx.phys;
  748. dev_dbg(fsl_chan->dev, "xfer LDs staring from 0x%llx\n",
  749. (unsigned long long)next_dest_addr);
  750. set_cdar(fsl_chan, next_dest_addr);
  751. dma_start(fsl_chan);
  752. } else {
  753. set_cdar(fsl_chan, 0);
  754. set_ndar(fsl_chan, 0);
  755. }
  756. out_unlock:
  757. spin_unlock_irqrestore(&fsl_chan->desc_lock, flags);
  758. }
  759. /**
  760. * fsl_dma_memcpy_issue_pending - Issue the DMA start command
  761. * @fsl_chan : Freescale DMA channel
  762. */
  763. static void fsl_dma_memcpy_issue_pending(struct dma_chan *chan)
  764. {
  765. struct fsl_dma_chan *fsl_chan = to_fsl_chan(chan);
  766. #ifdef FSL_DMA_LD_DEBUG
  767. struct fsl_desc_sw *ld;
  768. unsigned long flags;
  769. spin_lock_irqsave(&fsl_chan->desc_lock, flags);
  770. if (list_empty(&fsl_chan->ld_queue)) {
  771. spin_unlock_irqrestore(&fsl_chan->desc_lock, flags);
  772. return;
  773. }
  774. dev_dbg(fsl_chan->dev, "--memcpy issue--\n");
  775. list_for_each_entry(ld, &fsl_chan->ld_queue, node) {
  776. int i;
  777. dev_dbg(fsl_chan->dev, "Ch %d, LD %08x\n",
  778. fsl_chan->id, ld->async_tx.phys);
  779. for (i = 0; i < 8; i++)
  780. dev_dbg(fsl_chan->dev, "LD offset %d: %08x\n",
  781. i, *(((u32 *)&ld->hw) + i));
  782. }
  783. dev_dbg(fsl_chan->dev, "----------------\n");
  784. spin_unlock_irqrestore(&fsl_chan->desc_lock, flags);
  785. #endif
  786. fsl_chan_xfer_ld_queue(fsl_chan);
  787. }
  788. /**
  789. * fsl_dma_is_complete - Determine the DMA status
  790. * @fsl_chan : Freescale DMA channel
  791. */
  792. static enum dma_status fsl_dma_is_complete(struct dma_chan *chan,
  793. dma_cookie_t cookie,
  794. dma_cookie_t *done,
  795. dma_cookie_t *used)
  796. {
  797. struct fsl_dma_chan *fsl_chan = to_fsl_chan(chan);
  798. dma_cookie_t last_used;
  799. dma_cookie_t last_complete;
  800. fsl_chan_ld_cleanup(fsl_chan);
  801. last_used = chan->cookie;
  802. last_complete = fsl_chan->completed_cookie;
  803. if (done)
  804. *done = last_complete;
  805. if (used)
  806. *used = last_used;
  807. return dma_async_is_complete(cookie, last_complete, last_used);
  808. }
  809. static irqreturn_t fsl_dma_chan_do_interrupt(int irq, void *data)
  810. {
  811. struct fsl_dma_chan *fsl_chan = (struct fsl_dma_chan *)data;
  812. u32 stat;
  813. int update_cookie = 0;
  814. int xfer_ld_q = 0;
  815. stat = get_sr(fsl_chan);
  816. dev_dbg(fsl_chan->dev, "event: channel %d, stat = 0x%x\n",
  817. fsl_chan->id, stat);
  818. set_sr(fsl_chan, stat); /* Clear the event register */
  819. stat &= ~(FSL_DMA_SR_CB | FSL_DMA_SR_CH);
  820. if (!stat)
  821. return IRQ_NONE;
  822. if (stat & FSL_DMA_SR_TE)
  823. dev_err(fsl_chan->dev, "Transfer Error!\n");
  824. /* Programming Error
  825. * The DMA_INTERRUPT async_tx is a NULL transfer, which will
  826. * triger a PE interrupt.
  827. */
  828. if (stat & FSL_DMA_SR_PE) {
  829. dev_dbg(fsl_chan->dev, "event: Programming Error INT\n");
  830. if (get_bcr(fsl_chan) == 0) {
  831. /* BCR register is 0, this is a DMA_INTERRUPT async_tx.
  832. * Now, update the completed cookie, and continue the
  833. * next uncompleted transfer.
  834. */
  835. update_cookie = 1;
  836. xfer_ld_q = 1;
  837. }
  838. stat &= ~FSL_DMA_SR_PE;
  839. }
  840. /* If the link descriptor segment transfer finishes,
  841. * we will recycle the used descriptor.
  842. */
  843. if (stat & FSL_DMA_SR_EOSI) {
  844. dev_dbg(fsl_chan->dev, "event: End-of-segments INT\n");
  845. dev_dbg(fsl_chan->dev, "event: clndar 0x%llx, nlndar 0x%llx\n",
  846. (unsigned long long)get_cdar(fsl_chan),
  847. (unsigned long long)get_ndar(fsl_chan));
  848. stat &= ~FSL_DMA_SR_EOSI;
  849. update_cookie = 1;
  850. }
  851. /* For MPC8349, EOCDI event need to update cookie
  852. * and start the next transfer if it exist.
  853. */
  854. if (stat & FSL_DMA_SR_EOCDI) {
  855. dev_dbg(fsl_chan->dev, "event: End-of-Chain link INT\n");
  856. stat &= ~FSL_DMA_SR_EOCDI;
  857. update_cookie = 1;
  858. xfer_ld_q = 1;
  859. }
  860. /* If it current transfer is the end-of-transfer,
  861. * we should clear the Channel Start bit for
  862. * prepare next transfer.
  863. */
  864. if (stat & FSL_DMA_SR_EOLNI) {
  865. dev_dbg(fsl_chan->dev, "event: End-of-link INT\n");
  866. stat &= ~FSL_DMA_SR_EOLNI;
  867. xfer_ld_q = 1;
  868. }
  869. if (update_cookie)
  870. fsl_dma_update_completed_cookie(fsl_chan);
  871. if (xfer_ld_q)
  872. fsl_chan_xfer_ld_queue(fsl_chan);
  873. if (stat)
  874. dev_dbg(fsl_chan->dev, "event: unhandled sr 0x%02x\n",
  875. stat);
  876. dev_dbg(fsl_chan->dev, "event: Exit\n");
  877. tasklet_schedule(&fsl_chan->tasklet);
  878. return IRQ_HANDLED;
  879. }
  880. static irqreturn_t fsl_dma_do_interrupt(int irq, void *data)
  881. {
  882. struct fsl_dma_device *fdev = (struct fsl_dma_device *)data;
  883. u32 gsr;
  884. int ch_nr;
  885. gsr = (fdev->feature & FSL_DMA_BIG_ENDIAN) ? in_be32(fdev->reg_base)
  886. : in_le32(fdev->reg_base);
  887. ch_nr = (32 - ffs(gsr)) / 8;
  888. return fdev->chan[ch_nr] ? fsl_dma_chan_do_interrupt(irq,
  889. fdev->chan[ch_nr]) : IRQ_NONE;
  890. }
  891. static void dma_do_tasklet(unsigned long data)
  892. {
  893. struct fsl_dma_chan *fsl_chan = (struct fsl_dma_chan *)data;
  894. fsl_chan_ld_cleanup(fsl_chan);
  895. }
  896. static int __devinit fsl_dma_chan_probe(struct fsl_dma_device *fdev,
  897. struct device_node *node, u32 feature, const char *compatible)
  898. {
  899. struct fsl_dma_chan *new_fsl_chan;
  900. int err;
  901. /* alloc channel */
  902. new_fsl_chan = kzalloc(sizeof(struct fsl_dma_chan), GFP_KERNEL);
  903. if (!new_fsl_chan) {
  904. dev_err(fdev->dev, "No free memory for allocating "
  905. "dma channels!\n");
  906. return -ENOMEM;
  907. }
  908. /* get dma channel register base */
  909. err = of_address_to_resource(node, 0, &new_fsl_chan->reg);
  910. if (err) {
  911. dev_err(fdev->dev, "Can't get %s property 'reg'\n",
  912. node->full_name);
  913. goto err_no_reg;
  914. }
  915. new_fsl_chan->feature = feature;
  916. if (!fdev->feature)
  917. fdev->feature = new_fsl_chan->feature;
  918. /* If the DMA device's feature is different than its channels',
  919. * report the bug.
  920. */
  921. WARN_ON(fdev->feature != new_fsl_chan->feature);
  922. new_fsl_chan->dev = fdev->dev;
  923. new_fsl_chan->reg_base = ioremap(new_fsl_chan->reg.start,
  924. new_fsl_chan->reg.end - new_fsl_chan->reg.start + 1);
  925. new_fsl_chan->id = ((new_fsl_chan->reg.start - 0x100) & 0xfff) >> 7;
  926. if (new_fsl_chan->id >= FSL_DMA_MAX_CHANS_PER_DEVICE) {
  927. dev_err(fdev->dev, "There is no %d channel!\n",
  928. new_fsl_chan->id);
  929. err = -EINVAL;
  930. goto err_no_chan;
  931. }
  932. fdev->chan[new_fsl_chan->id] = new_fsl_chan;
  933. tasklet_init(&new_fsl_chan->tasklet, dma_do_tasklet,
  934. (unsigned long)new_fsl_chan);
  935. /* Init the channel */
  936. dma_init(new_fsl_chan);
  937. /* Clear cdar registers */
  938. set_cdar(new_fsl_chan, 0);
  939. switch (new_fsl_chan->feature & FSL_DMA_IP_MASK) {
  940. case FSL_DMA_IP_85XX:
  941. new_fsl_chan->toggle_ext_pause = fsl_chan_toggle_ext_pause;
  942. case FSL_DMA_IP_83XX:
  943. new_fsl_chan->toggle_ext_start = fsl_chan_toggle_ext_start;
  944. new_fsl_chan->set_src_loop_size = fsl_chan_set_src_loop_size;
  945. new_fsl_chan->set_dest_loop_size = fsl_chan_set_dest_loop_size;
  946. new_fsl_chan->set_request_count = fsl_chan_set_request_count;
  947. }
  948. spin_lock_init(&new_fsl_chan->desc_lock);
  949. INIT_LIST_HEAD(&new_fsl_chan->ld_queue);
  950. new_fsl_chan->common.device = &fdev->common;
  951. /* Add the channel to DMA device channel list */
  952. list_add_tail(&new_fsl_chan->common.device_node,
  953. &fdev->common.channels);
  954. fdev->common.chancnt++;
  955. new_fsl_chan->irq = irq_of_parse_and_map(node, 0);
  956. if (new_fsl_chan->irq != NO_IRQ) {
  957. err = request_irq(new_fsl_chan->irq,
  958. &fsl_dma_chan_do_interrupt, IRQF_SHARED,
  959. "fsldma-channel", new_fsl_chan);
  960. if (err) {
  961. dev_err(fdev->dev, "DMA channel %s request_irq error "
  962. "with return %d\n", node->full_name, err);
  963. goto err_no_irq;
  964. }
  965. }
  966. dev_info(fdev->dev, "#%d (%s), irq %d\n", new_fsl_chan->id,
  967. compatible,
  968. new_fsl_chan->irq != NO_IRQ ? new_fsl_chan->irq : fdev->irq);
  969. return 0;
  970. err_no_irq:
  971. list_del(&new_fsl_chan->common.device_node);
  972. err_no_chan:
  973. iounmap(new_fsl_chan->reg_base);
  974. err_no_reg:
  975. kfree(new_fsl_chan);
  976. return err;
  977. }
  978. static void fsl_dma_chan_remove(struct fsl_dma_chan *fchan)
  979. {
  980. if (fchan->irq != NO_IRQ)
  981. free_irq(fchan->irq, fchan);
  982. list_del(&fchan->common.device_node);
  983. iounmap(fchan->reg_base);
  984. kfree(fchan);
  985. }
  986. static int __devinit of_fsl_dma_probe(struct of_device *dev,
  987. const struct of_device_id *match)
  988. {
  989. int err;
  990. struct fsl_dma_device *fdev;
  991. struct device_node *child;
  992. fdev = kzalloc(sizeof(struct fsl_dma_device), GFP_KERNEL);
  993. if (!fdev) {
  994. dev_err(&dev->dev, "No enough memory for 'priv'\n");
  995. return -ENOMEM;
  996. }
  997. fdev->dev = &dev->dev;
  998. INIT_LIST_HEAD(&fdev->common.channels);
  999. /* get DMA controller register base */
  1000. err = of_address_to_resource(dev->node, 0, &fdev->reg);
  1001. if (err) {
  1002. dev_err(&dev->dev, "Can't get %s property 'reg'\n",
  1003. dev->node->full_name);
  1004. goto err_no_reg;
  1005. }
  1006. dev_info(&dev->dev, "Probe the Freescale DMA driver for %s "
  1007. "controller at 0x%llx...\n",
  1008. match->compatible, (unsigned long long)fdev->reg.start);
  1009. fdev->reg_base = ioremap(fdev->reg.start, fdev->reg.end
  1010. - fdev->reg.start + 1);
  1011. dma_cap_set(DMA_MEMCPY, fdev->common.cap_mask);
  1012. dma_cap_set(DMA_INTERRUPT, fdev->common.cap_mask);
  1013. dma_cap_set(DMA_SLAVE, fdev->common.cap_mask);
  1014. fdev->common.device_alloc_chan_resources = fsl_dma_alloc_chan_resources;
  1015. fdev->common.device_free_chan_resources = fsl_dma_free_chan_resources;
  1016. fdev->common.device_prep_dma_interrupt = fsl_dma_prep_interrupt;
  1017. fdev->common.device_prep_dma_memcpy = fsl_dma_prep_memcpy;
  1018. fdev->common.device_is_tx_complete = fsl_dma_is_complete;
  1019. fdev->common.device_issue_pending = fsl_dma_memcpy_issue_pending;
  1020. fdev->common.device_prep_slave_sg = fsl_dma_prep_slave_sg;
  1021. fdev->common.device_terminate_all = fsl_dma_device_terminate_all;
  1022. fdev->common.dev = &dev->dev;
  1023. fdev->irq = irq_of_parse_and_map(dev->node, 0);
  1024. if (fdev->irq != NO_IRQ) {
  1025. err = request_irq(fdev->irq, &fsl_dma_do_interrupt, IRQF_SHARED,
  1026. "fsldma-device", fdev);
  1027. if (err) {
  1028. dev_err(&dev->dev, "DMA device request_irq error "
  1029. "with return %d\n", err);
  1030. goto err;
  1031. }
  1032. }
  1033. dev_set_drvdata(&(dev->dev), fdev);
  1034. /* We cannot use of_platform_bus_probe() because there is no
  1035. * of_platform_bus_remove. Instead, we manually instantiate every DMA
  1036. * channel object.
  1037. */
  1038. for_each_child_of_node(dev->node, child) {
  1039. if (of_device_is_compatible(child, "fsl,eloplus-dma-channel"))
  1040. fsl_dma_chan_probe(fdev, child,
  1041. FSL_DMA_IP_85XX | FSL_DMA_BIG_ENDIAN,
  1042. "fsl,eloplus-dma-channel");
  1043. if (of_device_is_compatible(child, "fsl,elo-dma-channel"))
  1044. fsl_dma_chan_probe(fdev, child,
  1045. FSL_DMA_IP_83XX | FSL_DMA_LITTLE_ENDIAN,
  1046. "fsl,elo-dma-channel");
  1047. }
  1048. dma_async_device_register(&fdev->common);
  1049. return 0;
  1050. err:
  1051. iounmap(fdev->reg_base);
  1052. err_no_reg:
  1053. kfree(fdev);
  1054. return err;
  1055. }
  1056. static int of_fsl_dma_remove(struct of_device *of_dev)
  1057. {
  1058. struct fsl_dma_device *fdev;
  1059. unsigned int i;
  1060. fdev = dev_get_drvdata(&of_dev->dev);
  1061. dma_async_device_unregister(&fdev->common);
  1062. for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++)
  1063. if (fdev->chan[i])
  1064. fsl_dma_chan_remove(fdev->chan[i]);
  1065. if (fdev->irq != NO_IRQ)
  1066. free_irq(fdev->irq, fdev);
  1067. iounmap(fdev->reg_base);
  1068. kfree(fdev);
  1069. dev_set_drvdata(&of_dev->dev, NULL);
  1070. return 0;
  1071. }
  1072. static struct of_device_id of_fsl_dma_ids[] = {
  1073. { .compatible = "fsl,eloplus-dma", },
  1074. { .compatible = "fsl,elo-dma", },
  1075. {}
  1076. };
  1077. static struct of_platform_driver of_fsl_dma_driver = {
  1078. .name = "fsl-elo-dma",
  1079. .match_table = of_fsl_dma_ids,
  1080. .probe = of_fsl_dma_probe,
  1081. .remove = of_fsl_dma_remove,
  1082. };
  1083. static __init int of_fsl_dma_init(void)
  1084. {
  1085. int ret;
  1086. pr_info("Freescale Elo / Elo Plus DMA driver\n");
  1087. ret = of_register_platform_driver(&of_fsl_dma_driver);
  1088. if (ret)
  1089. pr_err("fsldma: failed to register platform driver\n");
  1090. return ret;
  1091. }
  1092. static void __exit of_fsl_dma_exit(void)
  1093. {
  1094. of_unregister_platform_driver(&of_fsl_dma_driver);
  1095. }
  1096. subsys_initcall(of_fsl_dma_init);
  1097. module_exit(of_fsl_dma_exit);
  1098. MODULE_DESCRIPTION("Freescale Elo / Elo Plus DMA driver");
  1099. MODULE_LICENSE("GPL");