fsldma.c 36 KB

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