fsldma.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  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_alloc(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. memset(desc, 0, sizeof(*desc));
  399. INIT_LIST_HEAD(&desc->tx_list);
  400. dma_async_tx_descriptor_init(&desc->async_tx, &chan->common);
  401. desc->async_tx.tx_submit = fsl_dma_tx_submit;
  402. desc->async_tx.phys = pdesc;
  403. chan_dbg(chan, "LD %p allocated\n", desc);
  404. return desc;
  405. }
  406. /**
  407. * fsldma_clean_completed_descriptor - free all descriptors which
  408. * has been completed and acked
  409. * @chan: Freescale DMA channel
  410. *
  411. * This function is used on all completed and acked descriptors.
  412. * All descriptors should only be freed in this function.
  413. */
  414. static void fsldma_clean_completed_descriptor(struct fsldma_chan *chan)
  415. {
  416. struct fsl_desc_sw *desc, *_desc;
  417. /* Run the callback for each descriptor, in order */
  418. list_for_each_entry_safe(desc, _desc, &chan->ld_completed, node)
  419. if (async_tx_test_ack(&desc->async_tx))
  420. fsl_dma_free_descriptor(chan, desc);
  421. }
  422. /**
  423. * fsldma_run_tx_complete_actions - cleanup a single link descriptor
  424. * @chan: Freescale DMA channel
  425. * @desc: descriptor to cleanup and free
  426. * @cookie: Freescale DMA transaction identifier
  427. *
  428. * This function is used on a descriptor which has been executed by the DMA
  429. * controller. It will run any callbacks, submit any dependencies.
  430. */
  431. static dma_cookie_t fsldma_run_tx_complete_actions(struct fsldma_chan *chan,
  432. struct fsl_desc_sw *desc, dma_cookie_t cookie)
  433. {
  434. struct dma_async_tx_descriptor *txd = &desc->async_tx;
  435. dma_cookie_t ret = cookie;
  436. BUG_ON(txd->cookie < 0);
  437. if (txd->cookie > 0) {
  438. ret = txd->cookie;
  439. /* Run the link descriptor callback function */
  440. if (txd->callback) {
  441. chan_dbg(chan, "LD %p callback\n", desc);
  442. txd->callback(txd->callback_param);
  443. }
  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 struct dma_async_tx_descriptor *fsl_dma_prep_sg(struct dma_chan *dchan,
  697. struct scatterlist *dst_sg, unsigned int dst_nents,
  698. struct scatterlist *src_sg, unsigned int src_nents,
  699. unsigned long flags)
  700. {
  701. struct fsl_desc_sw *first = NULL, *prev = NULL, *new = NULL;
  702. struct fsldma_chan *chan = to_fsl_chan(dchan);
  703. size_t dst_avail, src_avail;
  704. dma_addr_t dst, src;
  705. size_t len;
  706. /* basic sanity checks */
  707. if (dst_nents == 0 || src_nents == 0)
  708. return NULL;
  709. if (dst_sg == NULL || src_sg == NULL)
  710. return NULL;
  711. /*
  712. * TODO: should we check that both scatterlists have the same
  713. * TODO: number of bytes in total? Is that really an error?
  714. */
  715. /* get prepared for the loop */
  716. dst_avail = sg_dma_len(dst_sg);
  717. src_avail = sg_dma_len(src_sg);
  718. /* run until we are out of scatterlist entries */
  719. while (true) {
  720. /* create the largest transaction possible */
  721. len = min_t(size_t, src_avail, dst_avail);
  722. len = min_t(size_t, len, FSL_DMA_BCR_MAX_CNT);
  723. if (len == 0)
  724. goto fetch;
  725. dst = sg_dma_address(dst_sg) + sg_dma_len(dst_sg) - dst_avail;
  726. src = sg_dma_address(src_sg) + sg_dma_len(src_sg) - src_avail;
  727. /* allocate and populate the descriptor */
  728. new = fsl_dma_alloc_descriptor(chan);
  729. if (!new) {
  730. chan_err(chan, "%s\n", msg_ld_oom);
  731. goto fail;
  732. }
  733. set_desc_cnt(chan, &new->hw, len);
  734. set_desc_src(chan, &new->hw, src);
  735. set_desc_dst(chan, &new->hw, dst);
  736. if (!first)
  737. first = new;
  738. else
  739. set_desc_next(chan, &prev->hw, new->async_tx.phys);
  740. new->async_tx.cookie = 0;
  741. async_tx_ack(&new->async_tx);
  742. prev = new;
  743. /* Insert the link descriptor to the LD ring */
  744. list_add_tail(&new->node, &first->tx_list);
  745. /* update metadata */
  746. dst_avail -= len;
  747. src_avail -= len;
  748. fetch:
  749. /* fetch the next dst scatterlist entry */
  750. if (dst_avail == 0) {
  751. /* no more entries: we're done */
  752. if (dst_nents == 0)
  753. break;
  754. /* fetch the next entry: if there are no more: done */
  755. dst_sg = sg_next(dst_sg);
  756. if (dst_sg == NULL)
  757. break;
  758. dst_nents--;
  759. dst_avail = sg_dma_len(dst_sg);
  760. }
  761. /* fetch the next src scatterlist entry */
  762. if (src_avail == 0) {
  763. /* no more entries: we're done */
  764. if (src_nents == 0)
  765. break;
  766. /* fetch the next entry: if there are no more: done */
  767. src_sg = sg_next(src_sg);
  768. if (src_sg == NULL)
  769. break;
  770. src_nents--;
  771. src_avail = sg_dma_len(src_sg);
  772. }
  773. }
  774. new->async_tx.flags = flags; /* client is in control of this ack */
  775. new->async_tx.cookie = -EBUSY;
  776. /* Set End-of-link to the last link descriptor of new list */
  777. set_ld_eol(chan, new);
  778. return &first->async_tx;
  779. fail:
  780. if (!first)
  781. return NULL;
  782. fsldma_free_desc_list_reverse(chan, &first->tx_list);
  783. return NULL;
  784. }
  785. /**
  786. * fsl_dma_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
  787. * @chan: DMA channel
  788. * @sgl: scatterlist to transfer to/from
  789. * @sg_len: number of entries in @scatterlist
  790. * @direction: DMA direction
  791. * @flags: DMAEngine flags
  792. * @context: transaction context (ignored)
  793. *
  794. * Prepare a set of descriptors for a DMA_SLAVE transaction. Following the
  795. * DMA_SLAVE API, this gets the device-specific information from the
  796. * chan->private variable.
  797. */
  798. static struct dma_async_tx_descriptor *fsl_dma_prep_slave_sg(
  799. struct dma_chan *dchan, struct scatterlist *sgl, unsigned int sg_len,
  800. enum dma_transfer_direction direction, unsigned long flags,
  801. void *context)
  802. {
  803. /*
  804. * This operation is not supported on the Freescale DMA controller
  805. *
  806. * However, we need to provide the function pointer to allow the
  807. * device_control() method to work.
  808. */
  809. return NULL;
  810. }
  811. static int fsl_dma_device_control(struct dma_chan *dchan,
  812. enum dma_ctrl_cmd cmd, unsigned long arg)
  813. {
  814. struct dma_slave_config *config;
  815. struct fsldma_chan *chan;
  816. int size;
  817. if (!dchan)
  818. return -EINVAL;
  819. chan = to_fsl_chan(dchan);
  820. switch (cmd) {
  821. case DMA_TERMINATE_ALL:
  822. spin_lock_bh(&chan->desc_lock);
  823. /* Halt the DMA engine */
  824. dma_halt(chan);
  825. /* Remove and free all of the descriptors in the LD queue */
  826. fsldma_free_desc_list(chan, &chan->ld_pending);
  827. fsldma_free_desc_list(chan, &chan->ld_running);
  828. fsldma_free_desc_list(chan, &chan->ld_completed);
  829. chan->idle = true;
  830. spin_unlock_bh(&chan->desc_lock);
  831. return 0;
  832. case DMA_SLAVE_CONFIG:
  833. config = (struct dma_slave_config *)arg;
  834. /* make sure the channel supports setting burst size */
  835. if (!chan->set_request_count)
  836. return -ENXIO;
  837. /* we set the controller burst size depending on direction */
  838. if (config->direction == DMA_MEM_TO_DEV)
  839. size = config->dst_addr_width * config->dst_maxburst;
  840. else
  841. size = config->src_addr_width * config->src_maxburst;
  842. chan->set_request_count(chan, size);
  843. return 0;
  844. default:
  845. return -ENXIO;
  846. }
  847. return 0;
  848. }
  849. /**
  850. * fsl_dma_memcpy_issue_pending - Issue the DMA start command
  851. * @chan : Freescale DMA channel
  852. */
  853. static void fsl_dma_memcpy_issue_pending(struct dma_chan *dchan)
  854. {
  855. struct fsldma_chan *chan = to_fsl_chan(dchan);
  856. spin_lock_bh(&chan->desc_lock);
  857. fsl_chan_xfer_ld_queue(chan);
  858. spin_unlock_bh(&chan->desc_lock);
  859. }
  860. /**
  861. * fsl_tx_status - Determine the DMA status
  862. * @chan : Freescale DMA channel
  863. */
  864. static enum dma_status fsl_tx_status(struct dma_chan *dchan,
  865. dma_cookie_t cookie,
  866. struct dma_tx_state *txstate)
  867. {
  868. struct fsldma_chan *chan = to_fsl_chan(dchan);
  869. enum dma_status ret;
  870. ret = dma_cookie_status(dchan, cookie, txstate);
  871. if (ret == DMA_COMPLETE)
  872. return ret;
  873. spin_lock_bh(&chan->desc_lock);
  874. fsldma_cleanup_descriptors(chan);
  875. spin_unlock_bh(&chan->desc_lock);
  876. return dma_cookie_status(dchan, cookie, txstate);
  877. }
  878. /*----------------------------------------------------------------------------*/
  879. /* Interrupt Handling */
  880. /*----------------------------------------------------------------------------*/
  881. static irqreturn_t fsldma_chan_irq(int irq, void *data)
  882. {
  883. struct fsldma_chan *chan = data;
  884. u32 stat;
  885. /* save and clear the status register */
  886. stat = get_sr(chan);
  887. set_sr(chan, stat);
  888. chan_dbg(chan, "irq: stat = 0x%x\n", stat);
  889. /* check that this was really our device */
  890. stat &= ~(FSL_DMA_SR_CB | FSL_DMA_SR_CH);
  891. if (!stat)
  892. return IRQ_NONE;
  893. if (stat & FSL_DMA_SR_TE)
  894. chan_err(chan, "Transfer Error!\n");
  895. /*
  896. * Programming Error
  897. * The DMA_INTERRUPT async_tx is a NULL transfer, which will
  898. * trigger a PE interrupt.
  899. */
  900. if (stat & FSL_DMA_SR_PE) {
  901. chan_dbg(chan, "irq: Programming Error INT\n");
  902. stat &= ~FSL_DMA_SR_PE;
  903. if (get_bcr(chan) != 0)
  904. chan_err(chan, "Programming Error!\n");
  905. }
  906. /*
  907. * For MPC8349, EOCDI event need to update cookie
  908. * and start the next transfer if it exist.
  909. */
  910. if (stat & FSL_DMA_SR_EOCDI) {
  911. chan_dbg(chan, "irq: End-of-Chain link INT\n");
  912. stat &= ~FSL_DMA_SR_EOCDI;
  913. }
  914. /*
  915. * If it current transfer is the end-of-transfer,
  916. * we should clear the Channel Start bit for
  917. * prepare next transfer.
  918. */
  919. if (stat & FSL_DMA_SR_EOLNI) {
  920. chan_dbg(chan, "irq: End-of-link INT\n");
  921. stat &= ~FSL_DMA_SR_EOLNI;
  922. }
  923. /* check that the DMA controller is really idle */
  924. if (!dma_is_idle(chan))
  925. chan_err(chan, "irq: controller not idle!\n");
  926. /* check that we handled all of the bits */
  927. if (stat)
  928. chan_err(chan, "irq: unhandled sr 0x%08x\n", stat);
  929. /*
  930. * Schedule the tasklet to handle all cleanup of the current
  931. * transaction. It will start a new transaction if there is
  932. * one pending.
  933. */
  934. tasklet_schedule(&chan->tasklet);
  935. chan_dbg(chan, "irq: Exit\n");
  936. return IRQ_HANDLED;
  937. }
  938. static void dma_do_tasklet(unsigned long data)
  939. {
  940. struct fsldma_chan *chan = (struct fsldma_chan *)data;
  941. chan_dbg(chan, "tasklet entry\n");
  942. spin_lock_bh(&chan->desc_lock);
  943. /* the hardware is now idle and ready for more */
  944. chan->idle = true;
  945. /* Run all cleanup for descriptors which have been completed */
  946. fsldma_cleanup_descriptors(chan);
  947. spin_unlock_bh(&chan->desc_lock);
  948. chan_dbg(chan, "tasklet exit\n");
  949. }
  950. static irqreturn_t fsldma_ctrl_irq(int irq, void *data)
  951. {
  952. struct fsldma_device *fdev = data;
  953. struct fsldma_chan *chan;
  954. unsigned int handled = 0;
  955. u32 gsr, mask;
  956. int i;
  957. gsr = (fdev->feature & FSL_DMA_BIG_ENDIAN) ? in_be32(fdev->regs)
  958. : in_le32(fdev->regs);
  959. mask = 0xff000000;
  960. dev_dbg(fdev->dev, "IRQ: gsr 0x%.8x\n", gsr);
  961. for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
  962. chan = fdev->chan[i];
  963. if (!chan)
  964. continue;
  965. if (gsr & mask) {
  966. dev_dbg(fdev->dev, "IRQ: chan %d\n", chan->id);
  967. fsldma_chan_irq(irq, chan);
  968. handled++;
  969. }
  970. gsr &= ~mask;
  971. mask >>= 8;
  972. }
  973. return IRQ_RETVAL(handled);
  974. }
  975. static void fsldma_free_irqs(struct fsldma_device *fdev)
  976. {
  977. struct fsldma_chan *chan;
  978. int i;
  979. if (fdev->irq != NO_IRQ) {
  980. dev_dbg(fdev->dev, "free per-controller IRQ\n");
  981. free_irq(fdev->irq, fdev);
  982. return;
  983. }
  984. for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
  985. chan = fdev->chan[i];
  986. if (chan && chan->irq != NO_IRQ) {
  987. chan_dbg(chan, "free per-channel IRQ\n");
  988. free_irq(chan->irq, chan);
  989. }
  990. }
  991. }
  992. static int fsldma_request_irqs(struct fsldma_device *fdev)
  993. {
  994. struct fsldma_chan *chan;
  995. int ret;
  996. int i;
  997. /* if we have a per-controller IRQ, use that */
  998. if (fdev->irq != NO_IRQ) {
  999. dev_dbg(fdev->dev, "request per-controller IRQ\n");
  1000. ret = request_irq(fdev->irq, fsldma_ctrl_irq, IRQF_SHARED,
  1001. "fsldma-controller", fdev);
  1002. return ret;
  1003. }
  1004. /* no per-controller IRQ, use the per-channel IRQs */
  1005. for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
  1006. chan = fdev->chan[i];
  1007. if (!chan)
  1008. continue;
  1009. if (chan->irq == NO_IRQ) {
  1010. chan_err(chan, "interrupts property missing in device tree\n");
  1011. ret = -ENODEV;
  1012. goto out_unwind;
  1013. }
  1014. chan_dbg(chan, "request per-channel IRQ\n");
  1015. ret = request_irq(chan->irq, fsldma_chan_irq, IRQF_SHARED,
  1016. "fsldma-chan", chan);
  1017. if (ret) {
  1018. chan_err(chan, "unable to request per-channel IRQ\n");
  1019. goto out_unwind;
  1020. }
  1021. }
  1022. return 0;
  1023. out_unwind:
  1024. for (/* none */; i >= 0; i--) {
  1025. chan = fdev->chan[i];
  1026. if (!chan)
  1027. continue;
  1028. if (chan->irq == NO_IRQ)
  1029. continue;
  1030. free_irq(chan->irq, chan);
  1031. }
  1032. return ret;
  1033. }
  1034. /*----------------------------------------------------------------------------*/
  1035. /* OpenFirmware Subsystem */
  1036. /*----------------------------------------------------------------------------*/
  1037. static int fsl_dma_chan_probe(struct fsldma_device *fdev,
  1038. struct device_node *node, u32 feature, const char *compatible)
  1039. {
  1040. struct fsldma_chan *chan;
  1041. struct resource res;
  1042. int err;
  1043. /* alloc channel */
  1044. chan = kzalloc(sizeof(*chan), GFP_KERNEL);
  1045. if (!chan) {
  1046. dev_err(fdev->dev, "no free memory for DMA channels!\n");
  1047. err = -ENOMEM;
  1048. goto out_return;
  1049. }
  1050. /* ioremap registers for use */
  1051. chan->regs = of_iomap(node, 0);
  1052. if (!chan->regs) {
  1053. dev_err(fdev->dev, "unable to ioremap registers\n");
  1054. err = -ENOMEM;
  1055. goto out_free_chan;
  1056. }
  1057. err = of_address_to_resource(node, 0, &res);
  1058. if (err) {
  1059. dev_err(fdev->dev, "unable to find 'reg' property\n");
  1060. goto out_iounmap_regs;
  1061. }
  1062. chan->feature = feature;
  1063. if (!fdev->feature)
  1064. fdev->feature = chan->feature;
  1065. /*
  1066. * If the DMA device's feature is different than the feature
  1067. * of its channels, report the bug
  1068. */
  1069. WARN_ON(fdev->feature != chan->feature);
  1070. chan->dev = fdev->dev;
  1071. chan->id = (res.start & 0xfff) < 0x300 ?
  1072. ((res.start - 0x100) & 0xfff) >> 7 :
  1073. ((res.start - 0x200) & 0xfff) >> 7;
  1074. if (chan->id >= FSL_DMA_MAX_CHANS_PER_DEVICE) {
  1075. dev_err(fdev->dev, "too many channels for device\n");
  1076. err = -EINVAL;
  1077. goto out_iounmap_regs;
  1078. }
  1079. fdev->chan[chan->id] = chan;
  1080. tasklet_init(&chan->tasklet, dma_do_tasklet, (unsigned long)chan);
  1081. snprintf(chan->name, sizeof(chan->name), "chan%d", chan->id);
  1082. /* Initialize the channel */
  1083. dma_init(chan);
  1084. /* Clear cdar registers */
  1085. set_cdar(chan, 0);
  1086. switch (chan->feature & FSL_DMA_IP_MASK) {
  1087. case FSL_DMA_IP_85XX:
  1088. chan->toggle_ext_pause = fsl_chan_toggle_ext_pause;
  1089. case FSL_DMA_IP_83XX:
  1090. chan->toggle_ext_start = fsl_chan_toggle_ext_start;
  1091. chan->set_src_loop_size = fsl_chan_set_src_loop_size;
  1092. chan->set_dst_loop_size = fsl_chan_set_dst_loop_size;
  1093. chan->set_request_count = fsl_chan_set_request_count;
  1094. }
  1095. spin_lock_init(&chan->desc_lock);
  1096. INIT_LIST_HEAD(&chan->ld_pending);
  1097. INIT_LIST_HEAD(&chan->ld_running);
  1098. INIT_LIST_HEAD(&chan->ld_completed);
  1099. chan->idle = true;
  1100. #ifdef CONFIG_PM
  1101. chan->pm_state = RUNNING;
  1102. #endif
  1103. chan->common.device = &fdev->common;
  1104. dma_cookie_init(&chan->common);
  1105. /* find the IRQ line, if it exists in the device tree */
  1106. chan->irq = irq_of_parse_and_map(node, 0);
  1107. /* Add the channel to DMA device channel list */
  1108. list_add_tail(&chan->common.device_node, &fdev->common.channels);
  1109. dev_info(fdev->dev, "#%d (%s), irq %d\n", chan->id, compatible,
  1110. chan->irq != NO_IRQ ? chan->irq : fdev->irq);
  1111. return 0;
  1112. out_iounmap_regs:
  1113. iounmap(chan->regs);
  1114. out_free_chan:
  1115. kfree(chan);
  1116. out_return:
  1117. return err;
  1118. }
  1119. static void fsl_dma_chan_remove(struct fsldma_chan *chan)
  1120. {
  1121. irq_dispose_mapping(chan->irq);
  1122. list_del(&chan->common.device_node);
  1123. iounmap(chan->regs);
  1124. kfree(chan);
  1125. }
  1126. static int fsldma_of_probe(struct platform_device *op)
  1127. {
  1128. struct fsldma_device *fdev;
  1129. struct device_node *child;
  1130. int err;
  1131. fdev = kzalloc(sizeof(*fdev), GFP_KERNEL);
  1132. if (!fdev) {
  1133. dev_err(&op->dev, "No enough memory for 'priv'\n");
  1134. err = -ENOMEM;
  1135. goto out_return;
  1136. }
  1137. fdev->dev = &op->dev;
  1138. INIT_LIST_HEAD(&fdev->common.channels);
  1139. /* ioremap the registers for use */
  1140. fdev->regs = of_iomap(op->dev.of_node, 0);
  1141. if (!fdev->regs) {
  1142. dev_err(&op->dev, "unable to ioremap registers\n");
  1143. err = -ENOMEM;
  1144. goto out_free_fdev;
  1145. }
  1146. /* map the channel IRQ if it exists, but don't hookup the handler yet */
  1147. fdev->irq = irq_of_parse_and_map(op->dev.of_node, 0);
  1148. dma_cap_set(DMA_MEMCPY, fdev->common.cap_mask);
  1149. dma_cap_set(DMA_SG, fdev->common.cap_mask);
  1150. dma_cap_set(DMA_SLAVE, fdev->common.cap_mask);
  1151. fdev->common.device_alloc_chan_resources = fsl_dma_alloc_chan_resources;
  1152. fdev->common.device_free_chan_resources = fsl_dma_free_chan_resources;
  1153. fdev->common.device_prep_dma_memcpy = fsl_dma_prep_memcpy;
  1154. fdev->common.device_prep_dma_sg = fsl_dma_prep_sg;
  1155. fdev->common.device_tx_status = fsl_tx_status;
  1156. fdev->common.device_issue_pending = fsl_dma_memcpy_issue_pending;
  1157. fdev->common.device_prep_slave_sg = fsl_dma_prep_slave_sg;
  1158. fdev->common.device_control = fsl_dma_device_control;
  1159. fdev->common.dev = &op->dev;
  1160. dma_set_mask(&(op->dev), DMA_BIT_MASK(36));
  1161. platform_set_drvdata(op, fdev);
  1162. /*
  1163. * We cannot use of_platform_bus_probe() because there is no
  1164. * of_platform_bus_remove(). Instead, we manually instantiate every DMA
  1165. * channel object.
  1166. */
  1167. for_each_child_of_node(op->dev.of_node, child) {
  1168. if (of_device_is_compatible(child, "fsl,eloplus-dma-channel")) {
  1169. fsl_dma_chan_probe(fdev, child,
  1170. FSL_DMA_IP_85XX | FSL_DMA_BIG_ENDIAN,
  1171. "fsl,eloplus-dma-channel");
  1172. }
  1173. if (of_device_is_compatible(child, "fsl,elo-dma-channel")) {
  1174. fsl_dma_chan_probe(fdev, child,
  1175. FSL_DMA_IP_83XX | FSL_DMA_LITTLE_ENDIAN,
  1176. "fsl,elo-dma-channel");
  1177. }
  1178. }
  1179. /*
  1180. * Hookup the IRQ handler(s)
  1181. *
  1182. * If we have a per-controller interrupt, we prefer that to the
  1183. * per-channel interrupts to reduce the number of shared interrupt
  1184. * handlers on the same IRQ line
  1185. */
  1186. err = fsldma_request_irqs(fdev);
  1187. if (err) {
  1188. dev_err(fdev->dev, "unable to request IRQs\n");
  1189. goto out_free_fdev;
  1190. }
  1191. dma_async_device_register(&fdev->common);
  1192. return 0;
  1193. out_free_fdev:
  1194. irq_dispose_mapping(fdev->irq);
  1195. kfree(fdev);
  1196. out_return:
  1197. return err;
  1198. }
  1199. static int fsldma_of_remove(struct platform_device *op)
  1200. {
  1201. struct fsldma_device *fdev;
  1202. unsigned int i;
  1203. fdev = platform_get_drvdata(op);
  1204. dma_async_device_unregister(&fdev->common);
  1205. fsldma_free_irqs(fdev);
  1206. for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
  1207. if (fdev->chan[i])
  1208. fsl_dma_chan_remove(fdev->chan[i]);
  1209. }
  1210. iounmap(fdev->regs);
  1211. kfree(fdev);
  1212. return 0;
  1213. }
  1214. #ifdef CONFIG_PM
  1215. static int fsldma_suspend_late(struct device *dev)
  1216. {
  1217. struct platform_device *pdev = to_platform_device(dev);
  1218. struct fsldma_device *fdev = platform_get_drvdata(pdev);
  1219. struct fsldma_chan *chan;
  1220. int i;
  1221. for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
  1222. chan = fdev->chan[i];
  1223. if (!chan)
  1224. continue;
  1225. spin_lock_bh(&chan->desc_lock);
  1226. if (unlikely(!chan->idle))
  1227. goto out;
  1228. chan->regs_save.mr = get_mr(chan);
  1229. chan->pm_state = SUSPENDED;
  1230. spin_unlock_bh(&chan->desc_lock);
  1231. }
  1232. return 0;
  1233. out:
  1234. for (; i >= 0; i--) {
  1235. chan = fdev->chan[i];
  1236. if (!chan)
  1237. continue;
  1238. chan->pm_state = RUNNING;
  1239. spin_unlock_bh(&chan->desc_lock);
  1240. }
  1241. return -EBUSY;
  1242. }
  1243. static int fsldma_resume_early(struct device *dev)
  1244. {
  1245. struct platform_device *pdev = to_platform_device(dev);
  1246. struct fsldma_device *fdev = platform_get_drvdata(pdev);
  1247. struct fsldma_chan *chan;
  1248. u32 mode;
  1249. int i;
  1250. for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
  1251. chan = fdev->chan[i];
  1252. if (!chan)
  1253. continue;
  1254. spin_lock_bh(&chan->desc_lock);
  1255. mode = chan->regs_save.mr
  1256. & ~FSL_DMA_MR_CS & ~FSL_DMA_MR_CC & ~FSL_DMA_MR_CA;
  1257. set_mr(chan, mode);
  1258. chan->pm_state = RUNNING;
  1259. spin_unlock_bh(&chan->desc_lock);
  1260. }
  1261. return 0;
  1262. }
  1263. static const struct dev_pm_ops fsldma_pm_ops = {
  1264. .suspend_late = fsldma_suspend_late,
  1265. .resume_early = fsldma_resume_early,
  1266. };
  1267. #endif
  1268. static const struct of_device_id fsldma_of_ids[] = {
  1269. { .compatible = "fsl,elo3-dma", },
  1270. { .compatible = "fsl,eloplus-dma", },
  1271. { .compatible = "fsl,elo-dma", },
  1272. {}
  1273. };
  1274. static struct platform_driver fsldma_of_driver = {
  1275. .driver = {
  1276. .name = "fsl-elo-dma",
  1277. .of_match_table = fsldma_of_ids,
  1278. #ifdef CONFIG_PM
  1279. .pm = &fsldma_pm_ops,
  1280. #endif
  1281. },
  1282. .probe = fsldma_of_probe,
  1283. .remove = fsldma_of_remove,
  1284. };
  1285. /*----------------------------------------------------------------------------*/
  1286. /* Module Init / Exit */
  1287. /*----------------------------------------------------------------------------*/
  1288. static __init int fsldma_init(void)
  1289. {
  1290. pr_info("Freescale Elo series DMA driver\n");
  1291. return platform_driver_register(&fsldma_of_driver);
  1292. }
  1293. static void __exit fsldma_exit(void)
  1294. {
  1295. platform_driver_unregister(&fsldma_of_driver);
  1296. }
  1297. subsys_initcall(fsldma_init);
  1298. module_exit(fsldma_exit);
  1299. MODULE_DESCRIPTION("Freescale Elo series DMA driver");
  1300. MODULE_LICENSE("GPL");