fsldma.c 37 KB

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