fsldma.c 35 KB

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