fsldma.c 38 KB

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