fsldma.c 36 KB

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