fsldma.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Freescale MPC85xx, MPC83xx DMA Engine support
  4. *
  5. * Copyright (C) 2007-2010 Freescale Semiconductor, Inc. All rights reserved.
  6. *
  7. * Author:
  8. * Zhang Wei <wei.zhang@freescale.com>, Jul 2007
  9. * Ebony Zhu <ebony.zhu@freescale.com>, May 2007
  10. *
  11. * Description:
  12. * DMA engine driver for Freescale MPC8540 DMA controller, which is
  13. * also fit for MPC8560, MPC8555, MPC8548, MPC8641, and etc.
  14. * The support for MPC8349 DMA controller is also added.
  15. *
  16. * This driver instructs the DMA controller to issue the PCI Read Multiple
  17. * command for PCI read operations, instead of using the default PCI Read Line
  18. * command. Please be aware that this setting may result in read pre-fetching
  19. * on some platforms.
  20. */
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/pci.h>
  24. #include <linux/slab.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/dmaengine.h>
  27. #include <linux/delay.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/dmapool.h>
  30. #include <linux/of.h>
  31. #include <linux/of_address.h>
  32. #include <linux/of_irq.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/fsldma.h>
  35. #include "dmaengine.h"
  36. #include "fsldma.h"
  37. #define chan_dbg(chan, fmt, arg...) \
  38. dev_dbg(chan->dev, "%s: " fmt, chan->name, ##arg)
  39. #define chan_err(chan, fmt, arg...) \
  40. dev_err(chan->dev, "%s: " fmt, chan->name, ##arg)
  41. static const char msg_ld_oom[] = "No free memory for link descriptor";
  42. /*
  43. * Register Helpers
  44. */
  45. static void set_sr(struct fsldma_chan *chan, u32 val)
  46. {
  47. FSL_DMA_OUT(chan, &chan->regs->sr, val, 32);
  48. }
  49. static u32 get_sr(struct fsldma_chan *chan)
  50. {
  51. return FSL_DMA_IN(chan, &chan->regs->sr, 32);
  52. }
  53. static void set_mr(struct fsldma_chan *chan, u32 val)
  54. {
  55. FSL_DMA_OUT(chan, &chan->regs->mr, val, 32);
  56. }
  57. static u32 get_mr(struct fsldma_chan *chan)
  58. {
  59. return FSL_DMA_IN(chan, &chan->regs->mr, 32);
  60. }
  61. static void set_cdar(struct fsldma_chan *chan, dma_addr_t addr)
  62. {
  63. FSL_DMA_OUT(chan, &chan->regs->cdar, addr | FSL_DMA_SNEN, 64);
  64. }
  65. static dma_addr_t get_cdar(struct fsldma_chan *chan)
  66. {
  67. return FSL_DMA_IN(chan, &chan->regs->cdar, 64) & ~FSL_DMA_SNEN;
  68. }
  69. static void set_bcr(struct fsldma_chan *chan, u32 val)
  70. {
  71. FSL_DMA_OUT(chan, &chan->regs->bcr, val, 32);
  72. }
  73. static u32 get_bcr(struct fsldma_chan *chan)
  74. {
  75. return FSL_DMA_IN(chan, &chan->regs->bcr, 32);
  76. }
  77. /*
  78. * Descriptor Helpers
  79. */
  80. static void set_desc_cnt(struct fsldma_chan *chan,
  81. struct fsl_dma_ld_hw *hw, u32 count)
  82. {
  83. hw->count = CPU_TO_DMA(chan, count, 32);
  84. }
  85. static void set_desc_src(struct fsldma_chan *chan,
  86. struct fsl_dma_ld_hw *hw, dma_addr_t src)
  87. {
  88. u64 snoop_bits;
  89. snoop_bits = ((chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_85XX)
  90. ? ((u64)FSL_DMA_SATR_SREADTYPE_SNOOP_READ << 32) : 0;
  91. hw->src_addr = CPU_TO_DMA(chan, snoop_bits | src, 64);
  92. }
  93. static void set_desc_dst(struct fsldma_chan *chan,
  94. struct fsl_dma_ld_hw *hw, dma_addr_t dst)
  95. {
  96. u64 snoop_bits;
  97. snoop_bits = ((chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_85XX)
  98. ? ((u64)FSL_DMA_DATR_DWRITETYPE_SNOOP_WRITE << 32) : 0;
  99. hw->dst_addr = CPU_TO_DMA(chan, snoop_bits | dst, 64);
  100. }
  101. static void set_desc_next(struct fsldma_chan *chan,
  102. struct fsl_dma_ld_hw *hw, dma_addr_t next)
  103. {
  104. u64 snoop_bits;
  105. snoop_bits = ((chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_83XX)
  106. ? FSL_DMA_SNEN : 0;
  107. hw->next_ln_addr = CPU_TO_DMA(chan, snoop_bits | next, 64);
  108. }
  109. static void set_ld_eol(struct fsldma_chan *chan, struct fsl_desc_sw *desc)
  110. {
  111. u64 snoop_bits;
  112. snoop_bits = ((chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_83XX)
  113. ? FSL_DMA_SNEN : 0;
  114. desc->hw.next_ln_addr = CPU_TO_DMA(chan,
  115. DMA_TO_CPU(chan, desc->hw.next_ln_addr, 64) | FSL_DMA_EOL
  116. | snoop_bits, 64);
  117. }
  118. /*
  119. * DMA Engine Hardware Control Helpers
  120. */
  121. static void dma_init(struct fsldma_chan *chan)
  122. {
  123. /* Reset the channel */
  124. set_mr(chan, 0);
  125. switch (chan->feature & FSL_DMA_IP_MASK) {
  126. case FSL_DMA_IP_85XX:
  127. /* Set the channel to below modes:
  128. * EIE - Error interrupt enable
  129. * EOLNIE - End of links interrupt enable
  130. * BWC - Bandwidth sharing among channels
  131. */
  132. set_mr(chan, FSL_DMA_MR_BWC | FSL_DMA_MR_EIE
  133. | FSL_DMA_MR_EOLNIE);
  134. break;
  135. case FSL_DMA_IP_83XX:
  136. /* Set the channel to below modes:
  137. * EOTIE - End-of-transfer interrupt enable
  138. * PRC_RM - PCI read multiple
  139. */
  140. set_mr(chan, FSL_DMA_MR_EOTIE | FSL_DMA_MR_PRC_RM);
  141. break;
  142. }
  143. }
  144. static int dma_is_idle(struct fsldma_chan *chan)
  145. {
  146. u32 sr = get_sr(chan);
  147. return (!(sr & FSL_DMA_SR_CB)) || (sr & FSL_DMA_SR_CH);
  148. }
  149. /*
  150. * Start the DMA controller
  151. *
  152. * Preconditions:
  153. * - the CDAR register must point to the start descriptor
  154. * - the MRn[CS] bit must be cleared
  155. */
  156. static void dma_start(struct fsldma_chan *chan)
  157. {
  158. u32 mode;
  159. mode = get_mr(chan);
  160. if (chan->feature & FSL_DMA_CHAN_PAUSE_EXT) {
  161. set_bcr(chan, 0);
  162. mode |= FSL_DMA_MR_EMP_EN;
  163. } else {
  164. mode &= ~FSL_DMA_MR_EMP_EN;
  165. }
  166. if (chan->feature & FSL_DMA_CHAN_START_EXT) {
  167. mode |= FSL_DMA_MR_EMS_EN;
  168. } else {
  169. mode &= ~FSL_DMA_MR_EMS_EN;
  170. mode |= FSL_DMA_MR_CS;
  171. }
  172. set_mr(chan, mode);
  173. }
  174. static void dma_halt(struct fsldma_chan *chan)
  175. {
  176. u32 mode;
  177. int i;
  178. /* read the mode register */
  179. mode = get_mr(chan);
  180. /*
  181. * The 85xx controller supports channel abort, which will stop
  182. * the current transfer. On 83xx, this bit is the transfer error
  183. * mask bit, which should not be changed.
  184. */
  185. if ((chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_85XX) {
  186. mode |= FSL_DMA_MR_CA;
  187. set_mr(chan, mode);
  188. mode &= ~FSL_DMA_MR_CA;
  189. }
  190. /* stop the DMA controller */
  191. mode &= ~(FSL_DMA_MR_CS | FSL_DMA_MR_EMS_EN);
  192. set_mr(chan, mode);
  193. /* wait for the DMA controller to become idle */
  194. for (i = 0; i < 100; i++) {
  195. if (dma_is_idle(chan))
  196. return;
  197. udelay(10);
  198. }
  199. if (!dma_is_idle(chan))
  200. chan_err(chan, "DMA halt timeout!\n");
  201. }
  202. /**
  203. * fsl_chan_set_src_loop_size - Set source address hold transfer size
  204. * @chan : Freescale DMA channel
  205. * @size : Address loop size, 0 for disable loop
  206. *
  207. * The set source address hold transfer size. The source
  208. * address hold or loop transfer size is when the DMA transfer
  209. * data from source address (SA), if the loop size is 4, the DMA will
  210. * read data from SA, SA + 1, SA + 2, SA + 3, then loop back to SA,
  211. * SA + 1 ... and so on.
  212. */
  213. static void fsl_chan_set_src_loop_size(struct fsldma_chan *chan, int size)
  214. {
  215. u32 mode;
  216. mode = get_mr(chan);
  217. switch (size) {
  218. case 0:
  219. mode &= ~FSL_DMA_MR_SAHE;
  220. break;
  221. case 1:
  222. case 2:
  223. case 4:
  224. case 8:
  225. mode &= ~FSL_DMA_MR_SAHTS_MASK;
  226. mode |= FSL_DMA_MR_SAHE | (__ilog2(size) << 14);
  227. break;
  228. }
  229. set_mr(chan, mode);
  230. }
  231. /**
  232. * fsl_chan_set_dst_loop_size - Set destination address hold transfer size
  233. * @chan : Freescale DMA channel
  234. * @size : Address loop size, 0 for disable loop
  235. *
  236. * The set destination address hold transfer size. The destination
  237. * address hold or loop transfer size is when the DMA transfer
  238. * data to destination address (TA), if the loop size is 4, the DMA will
  239. * write data to TA, TA + 1, TA + 2, TA + 3, then loop back to TA,
  240. * TA + 1 ... and so on.
  241. */
  242. static void fsl_chan_set_dst_loop_size(struct fsldma_chan *chan, int size)
  243. {
  244. u32 mode;
  245. mode = get_mr(chan);
  246. switch (size) {
  247. case 0:
  248. mode &= ~FSL_DMA_MR_DAHE;
  249. break;
  250. case 1:
  251. case 2:
  252. case 4:
  253. case 8:
  254. mode &= ~FSL_DMA_MR_DAHTS_MASK;
  255. mode |= FSL_DMA_MR_DAHE | (__ilog2(size) << 16);
  256. break;
  257. }
  258. set_mr(chan, mode);
  259. }
  260. /**
  261. * fsl_chan_set_request_count - Set DMA Request Count for external control
  262. * @chan : Freescale DMA channel
  263. * @size : Number of bytes to transfer in a single request
  264. *
  265. * The Freescale DMA channel can be controlled by the external signal DREQ#.
  266. * The DMA request count is how many bytes are allowed to transfer before
  267. * pausing the channel, after which a new assertion of DREQ# resumes channel
  268. * operation.
  269. *
  270. * A size of 0 disables external pause control. The maximum size is 1024.
  271. */
  272. static void fsl_chan_set_request_count(struct fsldma_chan *chan, int size)
  273. {
  274. u32 mode;
  275. BUG_ON(size > 1024);
  276. mode = get_mr(chan);
  277. mode &= ~FSL_DMA_MR_BWC_MASK;
  278. mode |= (__ilog2(size) << 24) & FSL_DMA_MR_BWC_MASK;
  279. set_mr(chan, mode);
  280. }
  281. /**
  282. * fsl_chan_toggle_ext_pause - Toggle channel external pause status
  283. * @chan : Freescale DMA channel
  284. * @enable : 0 is disabled, 1 is enabled.
  285. *
  286. * The Freescale DMA channel can be controlled by the external signal DREQ#.
  287. * The DMA Request Count feature should be used in addition to this feature
  288. * to set the number of bytes to transfer before pausing the channel.
  289. */
  290. static void fsl_chan_toggle_ext_pause(struct fsldma_chan *chan, int enable)
  291. {
  292. if (enable)
  293. chan->feature |= FSL_DMA_CHAN_PAUSE_EXT;
  294. else
  295. chan->feature &= ~FSL_DMA_CHAN_PAUSE_EXT;
  296. }
  297. /**
  298. * fsl_chan_toggle_ext_start - Toggle channel external start status
  299. * @chan : Freescale DMA channel
  300. * @enable : 0 is disabled, 1 is enabled.
  301. *
  302. * If enable the external start, the channel can be started by an
  303. * external DMA start pin. So the dma_start() does not start the
  304. * transfer immediately. The DMA channel will wait for the
  305. * control pin asserted.
  306. */
  307. static void fsl_chan_toggle_ext_start(struct fsldma_chan *chan, int enable)
  308. {
  309. if (enable)
  310. chan->feature |= FSL_DMA_CHAN_START_EXT;
  311. else
  312. chan->feature &= ~FSL_DMA_CHAN_START_EXT;
  313. }
  314. int fsl_dma_external_start(struct dma_chan *dchan, int enable)
  315. {
  316. struct fsldma_chan *chan;
  317. if (!dchan)
  318. return -EINVAL;
  319. chan = to_fsl_chan(dchan);
  320. fsl_chan_toggle_ext_start(chan, enable);
  321. return 0;
  322. }
  323. EXPORT_SYMBOL_GPL(fsl_dma_external_start);
  324. static void append_ld_queue(struct fsldma_chan *chan, struct fsl_desc_sw *desc)
  325. {
  326. struct fsl_desc_sw *tail = to_fsl_desc(chan->ld_pending.prev);
  327. if (list_empty(&chan->ld_pending))
  328. goto out_splice;
  329. /*
  330. * Add the hardware descriptor to the chain of hardware descriptors
  331. * that already exists in memory.
  332. *
  333. * This will un-set the EOL bit of the existing transaction, and the
  334. * last link in this transaction will become the EOL descriptor.
  335. */
  336. set_desc_next(chan, &tail->hw, desc->async_tx.phys);
  337. /*
  338. * Add the software descriptor and all children to the list
  339. * of pending transactions
  340. */
  341. out_splice:
  342. list_splice_tail_init(&desc->tx_list, &chan->ld_pending);
  343. }
  344. static dma_cookie_t fsl_dma_tx_submit(struct dma_async_tx_descriptor *tx)
  345. {
  346. struct fsldma_chan *chan = to_fsl_chan(tx->chan);
  347. struct fsl_desc_sw *desc = tx_to_fsl_desc(tx);
  348. struct fsl_desc_sw *child;
  349. dma_cookie_t cookie = -EINVAL;
  350. spin_lock_bh(&chan->desc_lock);
  351. #ifdef CONFIG_PM
  352. if (unlikely(chan->pm_state != RUNNING)) {
  353. chan_dbg(chan, "cannot submit due to suspend\n");
  354. spin_unlock_bh(&chan->desc_lock);
  355. return -1;
  356. }
  357. #endif
  358. /*
  359. * assign cookies to all of the software descriptors
  360. * that make up this transaction
  361. */
  362. list_for_each_entry(child, &desc->tx_list, node) {
  363. cookie = dma_cookie_assign(&child->async_tx);
  364. }
  365. /* put this transaction onto the tail of the pending queue */
  366. append_ld_queue(chan, desc);
  367. spin_unlock_bh(&chan->desc_lock);
  368. return cookie;
  369. }
  370. /**
  371. * fsl_dma_free_descriptor - Free descriptor from channel's DMA pool.
  372. * @chan : Freescale DMA channel
  373. * @desc: descriptor to be freed
  374. */
  375. static void fsl_dma_free_descriptor(struct fsldma_chan *chan,
  376. struct fsl_desc_sw *desc)
  377. {
  378. list_del(&desc->node);
  379. chan_dbg(chan, "LD %p free\n", desc);
  380. dma_pool_free(chan->desc_pool, desc, desc->async_tx.phys);
  381. }
  382. /**
  383. * fsl_dma_alloc_descriptor - Allocate descriptor from channel's DMA pool.
  384. * @chan : Freescale DMA channel
  385. *
  386. * Return - The descriptor allocated. NULL for failed.
  387. */
  388. static struct fsl_desc_sw *fsl_dma_alloc_descriptor(struct fsldma_chan *chan)
  389. {
  390. struct fsl_desc_sw *desc;
  391. dma_addr_t pdesc;
  392. desc = dma_pool_zalloc(chan->desc_pool, GFP_ATOMIC, &pdesc);
  393. if (!desc) {
  394. chan_dbg(chan, "out of memory for link descriptor\n");
  395. return NULL;
  396. }
  397. INIT_LIST_HEAD(&desc->tx_list);
  398. dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
  399. desc->async_tx.tx_submit = fsl_dma_tx_submit;
  400. desc->async_tx.phys = pdesc;
  401. chan_dbg(chan, "LD %p allocated\n", desc);
  402. return desc;
  403. }
  404. /**
  405. * fsldma_clean_completed_descriptor - free all descriptors which
  406. * has been completed and acked
  407. * @chan: Freescale DMA channel
  408. *
  409. * This function is used on all completed and acked descriptors.
  410. * All descriptors should only be freed in this function.
  411. */
  412. static void fsldma_clean_completed_descriptor(struct fsldma_chan *chan)
  413. {
  414. struct fsl_desc_sw *desc, *_desc;
  415. /* Run the callback for each descriptor, in order */
  416. list_for_each_entry_safe(desc, _desc, &chan->ld_completed, node)
  417. if (async_tx_test_ack(&desc->async_tx))
  418. fsl_dma_free_descriptor(chan, desc);
  419. }
  420. /**
  421. * fsldma_run_tx_complete_actions - cleanup a single link descriptor
  422. * @chan: Freescale DMA channel
  423. * @desc: descriptor to cleanup and free
  424. * @cookie: Freescale DMA transaction identifier
  425. *
  426. * This function is used on a descriptor which has been executed by the DMA
  427. * controller. It will run any callbacks, submit any dependencies.
  428. */
  429. static dma_cookie_t fsldma_run_tx_complete_actions(struct fsldma_chan *chan,
  430. struct fsl_desc_sw *desc, dma_cookie_t cookie)
  431. {
  432. struct dma_async_tx_descriptor *txd = &desc->async_tx;
  433. dma_cookie_t ret = cookie;
  434. BUG_ON(txd->cookie < 0);
  435. if (txd->cookie > 0) {
  436. ret = txd->cookie;
  437. dma_descriptor_unmap(txd);
  438. /* Run the link descriptor callback function */
  439. dmaengine_desc_get_callback_invoke(txd, NULL);
  440. }
  441. /* Run any dependencies */
  442. dma_run_dependencies(txd);
  443. return ret;
  444. }
  445. /**
  446. * fsldma_clean_running_descriptor - move the completed descriptor from
  447. * ld_running to ld_completed
  448. * @chan: Freescale DMA channel
  449. * @desc: the descriptor which is completed
  450. *
  451. * Free the descriptor directly if acked by async_tx api, or move it to
  452. * queue ld_completed.
  453. */
  454. static void fsldma_clean_running_descriptor(struct fsldma_chan *chan,
  455. struct fsl_desc_sw *desc)
  456. {
  457. /* Remove from the list of transactions */
  458. list_del(&desc->node);
  459. /*
  460. * the client is allowed to attach dependent operations
  461. * until 'ack' is set
  462. */
  463. if (!async_tx_test_ack(&desc->async_tx)) {
  464. /*
  465. * Move this descriptor to the list of descriptors which is
  466. * completed, but still awaiting the 'ack' bit to be set.
  467. */
  468. list_add_tail(&desc->node, &chan->ld_completed);
  469. return;
  470. }
  471. dma_pool_free(chan->desc_pool, desc, desc->async_tx.phys);
  472. }
  473. /**
  474. * fsl_chan_xfer_ld_queue - transfer any pending transactions
  475. * @chan : Freescale DMA channel
  476. *
  477. * HARDWARE STATE: idle
  478. * LOCKING: must hold chan->desc_lock
  479. */
  480. static void fsl_chan_xfer_ld_queue(struct fsldma_chan *chan)
  481. {
  482. struct fsl_desc_sw *desc;
  483. /*
  484. * If the list of pending descriptors is empty, then we
  485. * don't need to do any work at all
  486. */
  487. if (list_empty(&chan->ld_pending)) {
  488. chan_dbg(chan, "no pending LDs\n");
  489. return;
  490. }
  491. /*
  492. * The DMA controller is not idle, which means that the interrupt
  493. * handler will start any queued transactions when it runs after
  494. * this transaction finishes
  495. */
  496. if (!chan->idle) {
  497. chan_dbg(chan, "DMA controller still busy\n");
  498. return;
  499. }
  500. /*
  501. * If there are some link descriptors which have not been
  502. * transferred, we need to start the controller
  503. */
  504. /*
  505. * Move all elements from the queue of pending transactions
  506. * onto the list of running transactions
  507. */
  508. chan_dbg(chan, "idle, starting controller\n");
  509. desc = list_first_entry(&chan->ld_pending, struct fsl_desc_sw, node);
  510. list_splice_tail_init(&chan->ld_pending, &chan->ld_running);
  511. /*
  512. * The 85xx DMA controller doesn't clear the channel start bit
  513. * automatically at the end of a transfer. Therefore we must clear
  514. * it in software before starting the transfer.
  515. */
  516. if ((chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_85XX) {
  517. u32 mode;
  518. mode = get_mr(chan);
  519. mode &= ~FSL_DMA_MR_CS;
  520. set_mr(chan, mode);
  521. }
  522. /*
  523. * Program the descriptor's address into the DMA controller,
  524. * then start the DMA transaction
  525. */
  526. set_cdar(chan, desc->async_tx.phys);
  527. get_cdar(chan);
  528. dma_start(chan);
  529. chan->idle = false;
  530. }
  531. /**
  532. * fsldma_cleanup_descriptors - cleanup link descriptors which are completed
  533. * and move them to ld_completed to free until flag 'ack' is set
  534. * @chan: Freescale DMA channel
  535. *
  536. * This function is used on descriptors which have been executed by the DMA
  537. * controller. It will run any callbacks, submit any dependencies, then
  538. * free these descriptors if flag 'ack' is set.
  539. */
  540. static void fsldma_cleanup_descriptors(struct fsldma_chan *chan)
  541. {
  542. struct fsl_desc_sw *desc, *_desc;
  543. dma_cookie_t cookie = 0;
  544. dma_addr_t curr_phys = get_cdar(chan);
  545. int seen_current = 0;
  546. fsldma_clean_completed_descriptor(chan);
  547. /* Run the callback for each descriptor, in order */
  548. list_for_each_entry_safe(desc, _desc, &chan->ld_running, node) {
  549. /*
  550. * do not advance past the current descriptor loaded into the
  551. * hardware channel, subsequent descriptors are either in
  552. * process or have not been submitted
  553. */
  554. if (seen_current)
  555. break;
  556. /*
  557. * stop the search if we reach the current descriptor and the
  558. * channel is busy
  559. */
  560. if (desc->async_tx.phys == curr_phys) {
  561. seen_current = 1;
  562. if (!dma_is_idle(chan))
  563. break;
  564. }
  565. cookie = fsldma_run_tx_complete_actions(chan, desc, cookie);
  566. fsldma_clean_running_descriptor(chan, desc);
  567. }
  568. /*
  569. * Start any pending transactions automatically
  570. *
  571. * In the ideal case, we keep the DMA controller busy while we go
  572. * ahead and free the descriptors below.
  573. */
  574. fsl_chan_xfer_ld_queue(chan);
  575. if (cookie > 0)
  576. chan->common.completed_cookie = cookie;
  577. }
  578. /**
  579. * fsl_dma_alloc_chan_resources - Allocate resources for DMA channel.
  580. * @chan : Freescale DMA channel
  581. *
  582. * This function will create a dma pool for descriptor allocation.
  583. *
  584. * Return - The number of descriptors allocated.
  585. */
  586. static int fsl_dma_alloc_chan_resources(struct dma_chan *dchan)
  587. {
  588. struct fsldma_chan *chan = to_fsl_chan(dchan);
  589. /* Has this channel already been allocated? */
  590. if (chan->desc_pool)
  591. return 1;
  592. /*
  593. * We need the descriptor to be aligned to 32bytes
  594. * for meeting FSL DMA specification requirement.
  595. */
  596. chan->desc_pool = dma_pool_create(chan->name, chan->dev,
  597. sizeof(struct fsl_desc_sw),
  598. __alignof__(struct fsl_desc_sw), 0);
  599. if (!chan->desc_pool) {
  600. chan_err(chan, "unable to allocate descriptor pool\n");
  601. return -ENOMEM;
  602. }
  603. /* there is at least one descriptor free to be allocated */
  604. return 1;
  605. }
  606. /**
  607. * fsldma_free_desc_list - Free all descriptors in a queue
  608. * @chan: Freescae DMA channel
  609. * @list: the list to free
  610. *
  611. * LOCKING: must hold chan->desc_lock
  612. */
  613. static void fsldma_free_desc_list(struct fsldma_chan *chan,
  614. struct list_head *list)
  615. {
  616. struct fsl_desc_sw *desc, *_desc;
  617. list_for_each_entry_safe(desc, _desc, list, node)
  618. fsl_dma_free_descriptor(chan, desc);
  619. }
  620. static void fsldma_free_desc_list_reverse(struct fsldma_chan *chan,
  621. struct list_head *list)
  622. {
  623. struct fsl_desc_sw *desc, *_desc;
  624. list_for_each_entry_safe_reverse(desc, _desc, list, node)
  625. fsl_dma_free_descriptor(chan, desc);
  626. }
  627. /**
  628. * fsl_dma_free_chan_resources - Free all resources of the channel.
  629. * @chan : Freescale DMA channel
  630. */
  631. static void fsl_dma_free_chan_resources(struct dma_chan *dchan)
  632. {
  633. struct fsldma_chan *chan = to_fsl_chan(dchan);
  634. chan_dbg(chan, "free all channel resources\n");
  635. spin_lock_bh(&chan->desc_lock);
  636. fsldma_cleanup_descriptors(chan);
  637. fsldma_free_desc_list(chan, &chan->ld_pending);
  638. fsldma_free_desc_list(chan, &chan->ld_running);
  639. fsldma_free_desc_list(chan, &chan->ld_completed);
  640. spin_unlock_bh(&chan->desc_lock);
  641. dma_pool_destroy(chan->desc_pool);
  642. chan->desc_pool = NULL;
  643. }
  644. static struct dma_async_tx_descriptor *
  645. fsl_dma_prep_memcpy(struct dma_chan *dchan,
  646. dma_addr_t dma_dst, dma_addr_t dma_src,
  647. size_t len, unsigned long flags)
  648. {
  649. struct fsldma_chan *chan;
  650. struct fsl_desc_sw *first = NULL, *prev = NULL, *new;
  651. size_t copy;
  652. if (!dchan)
  653. return NULL;
  654. if (!len)
  655. return NULL;
  656. chan = to_fsl_chan(dchan);
  657. do {
  658. /* Allocate the link descriptor from DMA pool */
  659. new = fsl_dma_alloc_descriptor(chan);
  660. if (!new) {
  661. chan_err(chan, "%s\n", msg_ld_oom);
  662. goto fail;
  663. }
  664. copy = min(len, (size_t)FSL_DMA_BCR_MAX_CNT);
  665. set_desc_cnt(chan, &new->hw, copy);
  666. set_desc_src(chan, &new->hw, dma_src);
  667. set_desc_dst(chan, &new->hw, dma_dst);
  668. if (!first)
  669. first = new;
  670. else
  671. set_desc_next(chan, &prev->hw, new->async_tx.phys);
  672. new->async_tx.cookie = 0;
  673. async_tx_ack(&new->async_tx);
  674. prev = new;
  675. len -= copy;
  676. dma_src += copy;
  677. dma_dst += copy;
  678. /* Insert the link descriptor to the LD ring */
  679. list_add_tail(&new->node, &first->tx_list);
  680. } while (len);
  681. new->async_tx.flags = flags; /* client is in control of this ack */
  682. new->async_tx.cookie = -EBUSY;
  683. /* Set End-of-link to the last link descriptor of new list */
  684. set_ld_eol(chan, new);
  685. return &first->async_tx;
  686. fail:
  687. if (!first)
  688. return NULL;
  689. fsldma_free_desc_list_reverse(chan, &first->tx_list);
  690. return NULL;
  691. }
  692. static int fsl_dma_device_terminate_all(struct dma_chan *dchan)
  693. {
  694. struct fsldma_chan *chan;
  695. if (!dchan)
  696. return -EINVAL;
  697. chan = to_fsl_chan(dchan);
  698. spin_lock_bh(&chan->desc_lock);
  699. /* Halt the DMA engine */
  700. dma_halt(chan);
  701. /* Remove and free all of the descriptors in the LD queue */
  702. fsldma_free_desc_list(chan, &chan->ld_pending);
  703. fsldma_free_desc_list(chan, &chan->ld_running);
  704. fsldma_free_desc_list(chan, &chan->ld_completed);
  705. chan->idle = true;
  706. spin_unlock_bh(&chan->desc_lock);
  707. return 0;
  708. }
  709. static int fsl_dma_device_config(struct dma_chan *dchan,
  710. struct dma_slave_config *config)
  711. {
  712. struct fsldma_chan *chan;
  713. int size;
  714. if (!dchan)
  715. return -EINVAL;
  716. chan = to_fsl_chan(dchan);
  717. /* make sure the channel supports setting burst size */
  718. if (!chan->set_request_count)
  719. return -ENXIO;
  720. /* we set the controller burst size depending on direction */
  721. if (config->direction == DMA_MEM_TO_DEV)
  722. size = config->dst_addr_width * config->dst_maxburst;
  723. else
  724. size = config->src_addr_width * config->src_maxburst;
  725. chan->set_request_count(chan, size);
  726. return 0;
  727. }
  728. /**
  729. * fsl_dma_memcpy_issue_pending - Issue the DMA start command
  730. * @chan : Freescale DMA channel
  731. */
  732. static void fsl_dma_memcpy_issue_pending(struct dma_chan *dchan)
  733. {
  734. struct fsldma_chan *chan = to_fsl_chan(dchan);
  735. spin_lock_bh(&chan->desc_lock);
  736. fsl_chan_xfer_ld_queue(chan);
  737. spin_unlock_bh(&chan->desc_lock);
  738. }
  739. /**
  740. * fsl_tx_status - Determine the DMA status
  741. * @chan : Freescale DMA channel
  742. */
  743. static enum dma_status fsl_tx_status(struct dma_chan *dchan,
  744. dma_cookie_t cookie,
  745. struct dma_tx_state *txstate)
  746. {
  747. struct fsldma_chan *chan = to_fsl_chan(dchan);
  748. enum dma_status ret;
  749. ret = dma_cookie_status(dchan, cookie, txstate);
  750. if (ret == DMA_COMPLETE)
  751. return ret;
  752. spin_lock_bh(&chan->desc_lock);
  753. fsldma_cleanup_descriptors(chan);
  754. spin_unlock_bh(&chan->desc_lock);
  755. return dma_cookie_status(dchan, cookie, txstate);
  756. }
  757. /*----------------------------------------------------------------------------*/
  758. /* Interrupt Handling */
  759. /*----------------------------------------------------------------------------*/
  760. static irqreturn_t fsldma_chan_irq(int irq, void *data)
  761. {
  762. struct fsldma_chan *chan = data;
  763. u32 stat;
  764. /* save and clear the status register */
  765. stat = get_sr(chan);
  766. set_sr(chan, stat);
  767. chan_dbg(chan, "irq: stat = 0x%x\n", stat);
  768. /* check that this was really our device */
  769. stat &= ~(FSL_DMA_SR_CB | FSL_DMA_SR_CH);
  770. if (!stat)
  771. return IRQ_NONE;
  772. if (stat & FSL_DMA_SR_TE)
  773. chan_err(chan, "Transfer Error!\n");
  774. /*
  775. * Programming Error
  776. * The DMA_INTERRUPT async_tx is a NULL transfer, which will
  777. * trigger a PE interrupt.
  778. */
  779. if (stat & FSL_DMA_SR_PE) {
  780. chan_dbg(chan, "irq: Programming Error INT\n");
  781. stat &= ~FSL_DMA_SR_PE;
  782. if (get_bcr(chan) != 0)
  783. chan_err(chan, "Programming Error!\n");
  784. }
  785. /*
  786. * For MPC8349, EOCDI event need to update cookie
  787. * and start the next transfer if it exist.
  788. */
  789. if (stat & FSL_DMA_SR_EOCDI) {
  790. chan_dbg(chan, "irq: End-of-Chain link INT\n");
  791. stat &= ~FSL_DMA_SR_EOCDI;
  792. }
  793. /*
  794. * If it current transfer is the end-of-transfer,
  795. * we should clear the Channel Start bit for
  796. * prepare next transfer.
  797. */
  798. if (stat & FSL_DMA_SR_EOLNI) {
  799. chan_dbg(chan, "irq: End-of-link INT\n");
  800. stat &= ~FSL_DMA_SR_EOLNI;
  801. }
  802. /* check that the DMA controller is really idle */
  803. if (!dma_is_idle(chan))
  804. chan_err(chan, "irq: controller not idle!\n");
  805. /* check that we handled all of the bits */
  806. if (stat)
  807. chan_err(chan, "irq: unhandled sr 0x%08x\n", stat);
  808. /*
  809. * Schedule the tasklet to handle all cleanup of the current
  810. * transaction. It will start a new transaction if there is
  811. * one pending.
  812. */
  813. tasklet_schedule(&chan->tasklet);
  814. chan_dbg(chan, "irq: Exit\n");
  815. return IRQ_HANDLED;
  816. }
  817. static void dma_do_tasklet(struct tasklet_struct *t)
  818. {
  819. struct fsldma_chan *chan = from_tasklet(chan, t, tasklet);
  820. chan_dbg(chan, "tasklet entry\n");
  821. spin_lock(&chan->desc_lock);
  822. /* the hardware is now idle and ready for more */
  823. chan->idle = true;
  824. /* Run all cleanup for descriptors which have been completed */
  825. fsldma_cleanup_descriptors(chan);
  826. spin_unlock(&chan->desc_lock);
  827. chan_dbg(chan, "tasklet exit\n");
  828. }
  829. static irqreturn_t fsldma_ctrl_irq(int irq, void *data)
  830. {
  831. struct fsldma_device *fdev = data;
  832. struct fsldma_chan *chan;
  833. unsigned int handled = 0;
  834. u32 gsr, mask;
  835. int i;
  836. gsr = (fdev->feature & FSL_DMA_BIG_ENDIAN) ? in_be32(fdev->regs)
  837. : in_le32(fdev->regs);
  838. mask = 0xff000000;
  839. dev_dbg(fdev->dev, "IRQ: gsr 0x%.8x\n", gsr);
  840. for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
  841. chan = fdev->chan[i];
  842. if (!chan)
  843. continue;
  844. if (gsr & mask) {
  845. dev_dbg(fdev->dev, "IRQ: chan %d\n", chan->id);
  846. fsldma_chan_irq(irq, chan);
  847. handled++;
  848. }
  849. gsr &= ~mask;
  850. mask >>= 8;
  851. }
  852. return IRQ_RETVAL(handled);
  853. }
  854. static void fsldma_free_irqs(struct fsldma_device *fdev)
  855. {
  856. struct fsldma_chan *chan;
  857. int i;
  858. if (fdev->irq) {
  859. dev_dbg(fdev->dev, "free per-controller IRQ\n");
  860. free_irq(fdev->irq, fdev);
  861. return;
  862. }
  863. for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
  864. chan = fdev->chan[i];
  865. if (chan && chan->irq) {
  866. chan_dbg(chan, "free per-channel IRQ\n");
  867. free_irq(chan->irq, chan);
  868. }
  869. }
  870. }
  871. static int fsldma_request_irqs(struct fsldma_device *fdev)
  872. {
  873. struct fsldma_chan *chan;
  874. int ret;
  875. int i;
  876. /* if we have a per-controller IRQ, use that */
  877. if (fdev->irq) {
  878. dev_dbg(fdev->dev, "request per-controller IRQ\n");
  879. ret = request_irq(fdev->irq, fsldma_ctrl_irq, IRQF_SHARED,
  880. "fsldma-controller", fdev);
  881. return ret;
  882. }
  883. /* no per-controller IRQ, use the per-channel IRQs */
  884. for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
  885. chan = fdev->chan[i];
  886. if (!chan)
  887. continue;
  888. if (!chan->irq) {
  889. chan_err(chan, "interrupts property missing in device tree\n");
  890. ret = -ENODEV;
  891. goto out_unwind;
  892. }
  893. chan_dbg(chan, "request per-channel IRQ\n");
  894. ret = request_irq(chan->irq, fsldma_chan_irq, IRQF_SHARED,
  895. "fsldma-chan", chan);
  896. if (ret) {
  897. chan_err(chan, "unable to request per-channel IRQ\n");
  898. goto out_unwind;
  899. }
  900. }
  901. return 0;
  902. out_unwind:
  903. for (/* none */; i >= 0; i--) {
  904. chan = fdev->chan[i];
  905. if (!chan)
  906. continue;
  907. if (!chan->irq)
  908. continue;
  909. free_irq(chan->irq, chan);
  910. }
  911. return ret;
  912. }
  913. /*----------------------------------------------------------------------------*/
  914. /* OpenFirmware Subsystem */
  915. /*----------------------------------------------------------------------------*/
  916. static int fsl_dma_chan_probe(struct fsldma_device *fdev,
  917. struct device_node *node, u32 feature, const char *compatible)
  918. {
  919. struct fsldma_chan *chan;
  920. struct resource res;
  921. int err;
  922. /* alloc channel */
  923. chan = kzalloc(sizeof(*chan), GFP_KERNEL);
  924. if (!chan) {
  925. err = -ENOMEM;
  926. goto out_return;
  927. }
  928. /* ioremap registers for use */
  929. chan->regs = of_iomap(node, 0);
  930. if (!chan->regs) {
  931. dev_err(fdev->dev, "unable to ioremap registers\n");
  932. err = -ENOMEM;
  933. goto out_free_chan;
  934. }
  935. err = of_address_to_resource(node, 0, &res);
  936. if (err) {
  937. dev_err(fdev->dev, "unable to find 'reg' property\n");
  938. goto out_iounmap_regs;
  939. }
  940. chan->feature = feature;
  941. if (!fdev->feature)
  942. fdev->feature = chan->feature;
  943. /*
  944. * If the DMA device's feature is different than the feature
  945. * of its channels, report the bug
  946. */
  947. WARN_ON(fdev->feature != chan->feature);
  948. chan->dev = fdev->dev;
  949. chan->id = (res.start & 0xfff) < 0x300 ?
  950. ((res.start - 0x100) & 0xfff) >> 7 :
  951. ((res.start - 0x200) & 0xfff) >> 7;
  952. if (chan->id >= FSL_DMA_MAX_CHANS_PER_DEVICE) {
  953. dev_err(fdev->dev, "too many channels for device\n");
  954. err = -EINVAL;
  955. goto out_iounmap_regs;
  956. }
  957. fdev->chan[chan->id] = chan;
  958. tasklet_setup(&chan->tasklet, dma_do_tasklet);
  959. snprintf(chan->name, sizeof(chan->name), "chan%d", chan->id);
  960. /* Initialize the channel */
  961. dma_init(chan);
  962. /* Clear cdar registers */
  963. set_cdar(chan, 0);
  964. switch (chan->feature & FSL_DMA_IP_MASK) {
  965. case FSL_DMA_IP_85XX:
  966. chan->toggle_ext_pause = fsl_chan_toggle_ext_pause;
  967. fallthrough;
  968. case FSL_DMA_IP_83XX:
  969. chan->toggle_ext_start = fsl_chan_toggle_ext_start;
  970. chan->set_src_loop_size = fsl_chan_set_src_loop_size;
  971. chan->set_dst_loop_size = fsl_chan_set_dst_loop_size;
  972. chan->set_request_count = fsl_chan_set_request_count;
  973. }
  974. spin_lock_init(&chan->desc_lock);
  975. INIT_LIST_HEAD(&chan->ld_pending);
  976. INIT_LIST_HEAD(&chan->ld_running);
  977. INIT_LIST_HEAD(&chan->ld_completed);
  978. chan->idle = true;
  979. #ifdef CONFIG_PM
  980. chan->pm_state = RUNNING;
  981. #endif
  982. chan->common.device = &fdev->common;
  983. dma_cookie_init(&chan->common);
  984. /* find the IRQ line, if it exists in the device tree */
  985. chan->irq = irq_of_parse_and_map(node, 0);
  986. /* Add the channel to DMA device channel list */
  987. list_add_tail(&chan->common.device_node, &fdev->common.channels);
  988. dev_info(fdev->dev, "#%d (%s), irq %d\n", chan->id, compatible,
  989. chan->irq ? chan->irq : fdev->irq);
  990. return 0;
  991. out_iounmap_regs:
  992. iounmap(chan->regs);
  993. out_free_chan:
  994. kfree(chan);
  995. out_return:
  996. return err;
  997. }
  998. static void fsl_dma_chan_remove(struct fsldma_chan *chan)
  999. {
  1000. irq_dispose_mapping(chan->irq);
  1001. list_del(&chan->common.device_node);
  1002. iounmap(chan->regs);
  1003. kfree(chan);
  1004. }
  1005. static int fsldma_of_probe(struct platform_device *op)
  1006. {
  1007. struct fsldma_device *fdev;
  1008. struct device_node *child;
  1009. unsigned int i;
  1010. int err;
  1011. fdev = kzalloc(sizeof(*fdev), GFP_KERNEL);
  1012. if (!fdev) {
  1013. err = -ENOMEM;
  1014. goto out_return;
  1015. }
  1016. fdev->dev = &op->dev;
  1017. INIT_LIST_HEAD(&fdev->common.channels);
  1018. /* ioremap the registers for use */
  1019. fdev->regs = of_iomap(op->dev.of_node, 0);
  1020. if (!fdev->regs) {
  1021. dev_err(&op->dev, "unable to ioremap registers\n");
  1022. err = -ENOMEM;
  1023. goto out_free;
  1024. }
  1025. /* map the channel IRQ if it exists, but don't hookup the handler yet */
  1026. fdev->irq = irq_of_parse_and_map(op->dev.of_node, 0);
  1027. dma_cap_set(DMA_MEMCPY, fdev->common.cap_mask);
  1028. dma_cap_set(DMA_SLAVE, fdev->common.cap_mask);
  1029. fdev->common.device_alloc_chan_resources = fsl_dma_alloc_chan_resources;
  1030. fdev->common.device_free_chan_resources = fsl_dma_free_chan_resources;
  1031. fdev->common.device_prep_dma_memcpy = fsl_dma_prep_memcpy;
  1032. fdev->common.device_tx_status = fsl_tx_status;
  1033. fdev->common.device_issue_pending = fsl_dma_memcpy_issue_pending;
  1034. fdev->common.device_config = fsl_dma_device_config;
  1035. fdev->common.device_terminate_all = fsl_dma_device_terminate_all;
  1036. fdev->common.dev = &op->dev;
  1037. fdev->common.src_addr_widths = FSL_DMA_BUSWIDTHS;
  1038. fdev->common.dst_addr_widths = FSL_DMA_BUSWIDTHS;
  1039. fdev->common.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  1040. fdev->common.residue_granularity = DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
  1041. dma_set_mask(&(op->dev), DMA_BIT_MASK(36));
  1042. platform_set_drvdata(op, fdev);
  1043. /*
  1044. * We cannot use of_platform_bus_probe() because there is no
  1045. * of_platform_bus_remove(). Instead, we manually instantiate every DMA
  1046. * channel object.
  1047. */
  1048. for_each_child_of_node(op->dev.of_node, child) {
  1049. if (of_device_is_compatible(child, "fsl,eloplus-dma-channel")) {
  1050. fsl_dma_chan_probe(fdev, child,
  1051. FSL_DMA_IP_85XX | FSL_DMA_BIG_ENDIAN,
  1052. "fsl,eloplus-dma-channel");
  1053. }
  1054. if (of_device_is_compatible(child, "fsl,elo-dma-channel")) {
  1055. fsl_dma_chan_probe(fdev, child,
  1056. FSL_DMA_IP_83XX | FSL_DMA_LITTLE_ENDIAN,
  1057. "fsl,elo-dma-channel");
  1058. }
  1059. }
  1060. /*
  1061. * Hookup the IRQ handler(s)
  1062. *
  1063. * If we have a per-controller interrupt, we prefer that to the
  1064. * per-channel interrupts to reduce the number of shared interrupt
  1065. * handlers on the same IRQ line
  1066. */
  1067. err = fsldma_request_irqs(fdev);
  1068. if (err) {
  1069. dev_err(fdev->dev, "unable to request IRQs\n");
  1070. goto out_free_fdev;
  1071. }
  1072. dma_async_device_register(&fdev->common);
  1073. return 0;
  1074. out_free_fdev:
  1075. for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
  1076. if (fdev->chan[i])
  1077. fsl_dma_chan_remove(fdev->chan[i]);
  1078. }
  1079. irq_dispose_mapping(fdev->irq);
  1080. iounmap(fdev->regs);
  1081. out_free:
  1082. kfree(fdev);
  1083. out_return:
  1084. return err;
  1085. }
  1086. static void fsldma_of_remove(struct platform_device *op)
  1087. {
  1088. struct fsldma_device *fdev;
  1089. unsigned int i;
  1090. fdev = platform_get_drvdata(op);
  1091. dma_async_device_unregister(&fdev->common);
  1092. fsldma_free_irqs(fdev);
  1093. for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
  1094. if (fdev->chan[i])
  1095. fsl_dma_chan_remove(fdev->chan[i]);
  1096. }
  1097. irq_dispose_mapping(fdev->irq);
  1098. iounmap(fdev->regs);
  1099. kfree(fdev);
  1100. }
  1101. #ifdef CONFIG_PM
  1102. static int fsldma_suspend_late(struct device *dev)
  1103. {
  1104. struct fsldma_device *fdev = dev_get_drvdata(dev);
  1105. struct fsldma_chan *chan;
  1106. int i;
  1107. for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
  1108. chan = fdev->chan[i];
  1109. if (!chan)
  1110. continue;
  1111. spin_lock_bh(&chan->desc_lock);
  1112. if (unlikely(!chan->idle))
  1113. goto out;
  1114. chan->regs_save.mr = get_mr(chan);
  1115. chan->pm_state = SUSPENDED;
  1116. spin_unlock_bh(&chan->desc_lock);
  1117. }
  1118. return 0;
  1119. out:
  1120. for (; i >= 0; i--) {
  1121. chan = fdev->chan[i];
  1122. if (!chan)
  1123. continue;
  1124. chan->pm_state = RUNNING;
  1125. spin_unlock_bh(&chan->desc_lock);
  1126. }
  1127. return -EBUSY;
  1128. }
  1129. static int fsldma_resume_early(struct device *dev)
  1130. {
  1131. struct fsldma_device *fdev = dev_get_drvdata(dev);
  1132. struct fsldma_chan *chan;
  1133. u32 mode;
  1134. int i;
  1135. for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
  1136. chan = fdev->chan[i];
  1137. if (!chan)
  1138. continue;
  1139. spin_lock_bh(&chan->desc_lock);
  1140. mode = chan->regs_save.mr
  1141. & ~FSL_DMA_MR_CS & ~FSL_DMA_MR_CC & ~FSL_DMA_MR_CA;
  1142. set_mr(chan, mode);
  1143. chan->pm_state = RUNNING;
  1144. spin_unlock_bh(&chan->desc_lock);
  1145. }
  1146. return 0;
  1147. }
  1148. static const struct dev_pm_ops fsldma_pm_ops = {
  1149. .suspend_late = fsldma_suspend_late,
  1150. .resume_early = fsldma_resume_early,
  1151. };
  1152. #endif
  1153. static const struct of_device_id fsldma_of_ids[] = {
  1154. { .compatible = "fsl,elo3-dma", },
  1155. { .compatible = "fsl,eloplus-dma", },
  1156. { .compatible = "fsl,elo-dma", },
  1157. {}
  1158. };
  1159. MODULE_DEVICE_TABLE(of, fsldma_of_ids);
  1160. static struct platform_driver fsldma_of_driver = {
  1161. .driver = {
  1162. .name = "fsl-elo-dma",
  1163. .of_match_table = fsldma_of_ids,
  1164. #ifdef CONFIG_PM
  1165. .pm = &fsldma_pm_ops,
  1166. #endif
  1167. },
  1168. .probe = fsldma_of_probe,
  1169. .remove_new = fsldma_of_remove,
  1170. };
  1171. /*----------------------------------------------------------------------------*/
  1172. /* Module Init / Exit */
  1173. /*----------------------------------------------------------------------------*/
  1174. static __init int fsldma_init(void)
  1175. {
  1176. pr_info("Freescale Elo series DMA driver\n");
  1177. return platform_driver_register(&fsldma_of_driver);
  1178. }
  1179. static void __exit fsldma_exit(void)
  1180. {
  1181. platform_driver_unregister(&fsldma_of_driver);
  1182. }
  1183. subsys_initcall(fsldma_init);
  1184. module_exit(fsldma_exit);
  1185. MODULE_DESCRIPTION("Freescale Elo series DMA driver");
  1186. MODULE_LICENSE("GPL");