fsldma.c 36 KB

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