fsldma.c 36 KB

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