fsldma.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  1. /*
  2. * Freescale MPC85xx, MPC83xx DMA Engine support
  3. *
  4. * Copyright (C) 2007-2010 Freescale Semiconductor, Inc. All rights reserved.
  5. *
  6. * Author:
  7. * Zhang Wei <wei.zhang@freescale.com>, Jul 2007
  8. * Ebony Zhu <ebony.zhu@freescale.com>, May 2007
  9. *
  10. * Description:
  11. * DMA engine driver for Freescale MPC8540 DMA controller, which is
  12. * also fit for MPC8560, MPC8555, MPC8548, MPC8641, and etc.
  13. * The support for MPC8349 DMA controller is also added.
  14. *
  15. * This driver instructs the DMA controller to issue the PCI Read Multiple
  16. * command for PCI read operations, instead of using the default PCI Read Line
  17. * command. Please be aware that this setting may result in read pre-fetching
  18. * on some platforms.
  19. *
  20. * This is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation; either version 2 of the License, or
  23. * (at your option) any later version.
  24. *
  25. */
  26. #include <linux/init.h>
  27. #include <linux/module.h>
  28. #include <linux/pci.h>
  29. #include <linux/slab.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/dmaengine.h>
  32. #include <linux/delay.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/dmapool.h>
  35. #include <linux/of_platform.h>
  36. #include "fsldma.h"
  37. 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. */
  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, msg_ld_oom);
  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, msg_ld_oom);
  452. goto fail;
  453. }
  454. #ifdef FSL_DMA_LD_DEBUG
  455. dev_dbg(chan->dev, "new link desc alloc %p\n", new);
  456. #endif
  457. copy = min(len, (size_t)FSL_DMA_BCR_MAX_CNT);
  458. set_desc_cnt(chan, &new->hw, copy);
  459. set_desc_src(chan, &new->hw, dma_src);
  460. set_desc_dst(chan, &new->hw, dma_dst);
  461. if (!first)
  462. first = new;
  463. else
  464. set_desc_next(chan, &prev->hw, new->async_tx.phys);
  465. new->async_tx.cookie = 0;
  466. async_tx_ack(&new->async_tx);
  467. prev = new;
  468. len -= copy;
  469. dma_src += copy;
  470. dma_dst += copy;
  471. /* Insert the link descriptor to the LD ring */
  472. list_add_tail(&new->node, &first->tx_list);
  473. } while (len);
  474. new->async_tx.flags = flags; /* client is in control of this ack */
  475. new->async_tx.cookie = -EBUSY;
  476. /* Set End-of-link to the last link descriptor of new list*/
  477. set_ld_eol(chan, new);
  478. return &first->async_tx;
  479. fail:
  480. if (!first)
  481. return NULL;
  482. fsldma_free_desc_list_reverse(chan, &first->tx_list);
  483. return NULL;
  484. }
  485. static struct dma_async_tx_descriptor *fsl_dma_prep_sg(struct dma_chan *dchan,
  486. struct scatterlist *dst_sg, unsigned int dst_nents,
  487. struct scatterlist *src_sg, unsigned int src_nents,
  488. unsigned long flags)
  489. {
  490. struct fsl_desc_sw *first = NULL, *prev = NULL, *new = NULL;
  491. struct fsldma_chan *chan = to_fsl_chan(dchan);
  492. size_t dst_avail, src_avail;
  493. dma_addr_t dst, src;
  494. size_t len;
  495. /* basic sanity checks */
  496. if (dst_nents == 0 || src_nents == 0)
  497. return NULL;
  498. if (dst_sg == NULL || src_sg == NULL)
  499. return NULL;
  500. /*
  501. * TODO: should we check that both scatterlists have the same
  502. * TODO: number of bytes in total? Is that really an error?
  503. */
  504. /* get prepared for the loop */
  505. dst_avail = sg_dma_len(dst_sg);
  506. src_avail = sg_dma_len(src_sg);
  507. /* run until we are out of scatterlist entries */
  508. while (true) {
  509. /* create the largest transaction possible */
  510. len = min_t(size_t, src_avail, dst_avail);
  511. len = min_t(size_t, len, FSL_DMA_BCR_MAX_CNT);
  512. if (len == 0)
  513. goto fetch;
  514. dst = sg_dma_address(dst_sg) + sg_dma_len(dst_sg) - dst_avail;
  515. src = sg_dma_address(src_sg) + sg_dma_len(src_sg) - src_avail;
  516. /* allocate and populate the descriptor */
  517. new = fsl_dma_alloc_descriptor(chan);
  518. if (!new) {
  519. dev_err(chan->dev, msg_ld_oom);
  520. goto fail;
  521. }
  522. #ifdef FSL_DMA_LD_DEBUG
  523. dev_dbg(chan->dev, "new link desc alloc %p\n", new);
  524. #endif
  525. set_desc_cnt(chan, &new->hw, len);
  526. set_desc_src(chan, &new->hw, src);
  527. set_desc_dst(chan, &new->hw, dst);
  528. if (!first)
  529. first = new;
  530. else
  531. set_desc_next(chan, &prev->hw, new->async_tx.phys);
  532. new->async_tx.cookie = 0;
  533. async_tx_ack(&new->async_tx);
  534. prev = new;
  535. /* Insert the link descriptor to the LD ring */
  536. list_add_tail(&new->node, &first->tx_list);
  537. /* update metadata */
  538. dst_avail -= len;
  539. src_avail -= len;
  540. fetch:
  541. /* fetch the next dst scatterlist entry */
  542. if (dst_avail == 0) {
  543. /* no more entries: we're done */
  544. if (dst_nents == 0)
  545. break;
  546. /* fetch the next entry: if there are no more: done */
  547. dst_sg = sg_next(dst_sg);
  548. if (dst_sg == NULL)
  549. break;
  550. dst_nents--;
  551. dst_avail = sg_dma_len(dst_sg);
  552. }
  553. /* fetch the next src scatterlist entry */
  554. if (src_avail == 0) {
  555. /* no more entries: we're done */
  556. if (src_nents == 0)
  557. break;
  558. /* fetch the next entry: if there are no more: done */
  559. src_sg = sg_next(src_sg);
  560. if (src_sg == NULL)
  561. break;
  562. src_nents--;
  563. src_avail = sg_dma_len(src_sg);
  564. }
  565. }
  566. new->async_tx.flags = flags; /* client is in control of this ack */
  567. new->async_tx.cookie = -EBUSY;
  568. /* Set End-of-link to the last link descriptor of new list */
  569. set_ld_eol(chan, new);
  570. return &first->async_tx;
  571. fail:
  572. if (!first)
  573. return NULL;
  574. fsldma_free_desc_list_reverse(chan, &first->tx_list);
  575. return NULL;
  576. }
  577. /**
  578. * fsl_dma_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
  579. * @chan: DMA channel
  580. * @sgl: scatterlist to transfer to/from
  581. * @sg_len: number of entries in @scatterlist
  582. * @direction: DMA direction
  583. * @flags: DMAEngine flags
  584. *
  585. * Prepare a set of descriptors for a DMA_SLAVE transaction. Following the
  586. * DMA_SLAVE API, this gets the device-specific information from the
  587. * chan->private variable.
  588. */
  589. static struct dma_async_tx_descriptor *fsl_dma_prep_slave_sg(
  590. struct dma_chan *dchan, struct scatterlist *sgl, unsigned int sg_len,
  591. enum dma_data_direction direction, unsigned long flags)
  592. {
  593. /*
  594. * This operation is not supported on the Freescale DMA controller
  595. *
  596. * However, we need to provide the function pointer to allow the
  597. * device_control() method to work.
  598. */
  599. return NULL;
  600. }
  601. static int fsl_dma_device_control(struct dma_chan *dchan,
  602. enum dma_ctrl_cmd cmd, unsigned long arg)
  603. {
  604. struct dma_slave_config *config;
  605. struct fsldma_chan *chan;
  606. unsigned long flags;
  607. int size;
  608. if (!dchan)
  609. return -EINVAL;
  610. chan = to_fsl_chan(dchan);
  611. switch (cmd) {
  612. case DMA_TERMINATE_ALL:
  613. /* Halt the DMA engine */
  614. dma_halt(chan);
  615. spin_lock_irqsave(&chan->desc_lock, flags);
  616. /* Remove and free all of the descriptors in the LD queue */
  617. fsldma_free_desc_list(chan, &chan->ld_pending);
  618. fsldma_free_desc_list(chan, &chan->ld_running);
  619. spin_unlock_irqrestore(&chan->desc_lock, flags);
  620. return 0;
  621. case DMA_SLAVE_CONFIG:
  622. config = (struct dma_slave_config *)arg;
  623. /* make sure the channel supports setting burst size */
  624. if (!chan->set_request_count)
  625. return -ENXIO;
  626. /* we set the controller burst size depending on direction */
  627. if (config->direction == DMA_TO_DEVICE)
  628. size = config->dst_addr_width * config->dst_maxburst;
  629. else
  630. size = config->src_addr_width * config->src_maxburst;
  631. chan->set_request_count(chan, size);
  632. return 0;
  633. case FSLDMA_EXTERNAL_START:
  634. /* make sure the channel supports external start */
  635. if (!chan->toggle_ext_start)
  636. return -ENXIO;
  637. chan->toggle_ext_start(chan, arg);
  638. return 0;
  639. default:
  640. return -ENXIO;
  641. }
  642. return 0;
  643. }
  644. /**
  645. * fsl_dma_update_completed_cookie - Update the completed cookie.
  646. * @chan : Freescale DMA channel
  647. *
  648. * CONTEXT: hardirq
  649. */
  650. static void fsl_dma_update_completed_cookie(struct fsldma_chan *chan)
  651. {
  652. struct fsl_desc_sw *desc;
  653. unsigned long flags;
  654. dma_cookie_t cookie;
  655. spin_lock_irqsave(&chan->desc_lock, flags);
  656. if (list_empty(&chan->ld_running)) {
  657. dev_dbg(chan->dev, "no running descriptors\n");
  658. goto out_unlock;
  659. }
  660. /* Get the last descriptor, update the cookie to that */
  661. desc = to_fsl_desc(chan->ld_running.prev);
  662. if (dma_is_idle(chan))
  663. cookie = desc->async_tx.cookie;
  664. else {
  665. cookie = desc->async_tx.cookie - 1;
  666. if (unlikely(cookie < DMA_MIN_COOKIE))
  667. cookie = DMA_MAX_COOKIE;
  668. }
  669. chan->completed_cookie = cookie;
  670. out_unlock:
  671. spin_unlock_irqrestore(&chan->desc_lock, flags);
  672. }
  673. /**
  674. * fsldma_desc_status - Check the status of a descriptor
  675. * @chan: Freescale DMA channel
  676. * @desc: DMA SW descriptor
  677. *
  678. * This function will return the status of the given descriptor
  679. */
  680. static enum dma_status fsldma_desc_status(struct fsldma_chan *chan,
  681. struct fsl_desc_sw *desc)
  682. {
  683. return dma_async_is_complete(desc->async_tx.cookie,
  684. chan->completed_cookie,
  685. chan->common.cookie);
  686. }
  687. /**
  688. * fsl_chan_ld_cleanup - Clean up link descriptors
  689. * @chan : Freescale DMA channel
  690. *
  691. * This function clean up the ld_queue of DMA channel.
  692. */
  693. static void fsl_chan_ld_cleanup(struct fsldma_chan *chan)
  694. {
  695. struct fsl_desc_sw *desc, *_desc;
  696. unsigned long flags;
  697. spin_lock_irqsave(&chan->desc_lock, flags);
  698. dev_dbg(chan->dev, "chan completed_cookie = %d\n", chan->completed_cookie);
  699. list_for_each_entry_safe(desc, _desc, &chan->ld_running, node) {
  700. dma_async_tx_callback callback;
  701. void *callback_param;
  702. if (fsldma_desc_status(chan, desc) == DMA_IN_PROGRESS)
  703. break;
  704. /* Remove from the list of running transactions */
  705. list_del(&desc->node);
  706. /* Run the link descriptor callback function */
  707. callback = desc->async_tx.callback;
  708. callback_param = desc->async_tx.callback_param;
  709. if (callback) {
  710. spin_unlock_irqrestore(&chan->desc_lock, flags);
  711. dev_dbg(chan->dev, "LD %p callback\n", desc);
  712. callback(callback_param);
  713. spin_lock_irqsave(&chan->desc_lock, flags);
  714. }
  715. /* Run any dependencies, then free the descriptor */
  716. dma_run_dependencies(&desc->async_tx);
  717. dma_pool_free(chan->desc_pool, desc, desc->async_tx.phys);
  718. }
  719. spin_unlock_irqrestore(&chan->desc_lock, flags);
  720. }
  721. /**
  722. * fsl_chan_xfer_ld_queue - transfer any pending transactions
  723. * @chan : Freescale DMA channel
  724. *
  725. * This will make sure that any pending transactions will be run.
  726. * If the DMA controller is idle, it will be started. Otherwise,
  727. * the DMA controller's interrupt handler will start any pending
  728. * transactions when it becomes idle.
  729. */
  730. static void fsl_chan_xfer_ld_queue(struct fsldma_chan *chan)
  731. {
  732. struct fsl_desc_sw *desc;
  733. unsigned long flags;
  734. spin_lock_irqsave(&chan->desc_lock, flags);
  735. /*
  736. * If the list of pending descriptors is empty, then we
  737. * don't need to do any work at all
  738. */
  739. if (list_empty(&chan->ld_pending)) {
  740. dev_dbg(chan->dev, "no pending LDs\n");
  741. goto out_unlock;
  742. }
  743. /*
  744. * The DMA controller is not idle, which means the interrupt
  745. * handler will start any queued transactions when it runs
  746. * at the end of the current transaction
  747. */
  748. if (!dma_is_idle(chan)) {
  749. dev_dbg(chan->dev, "DMA controller still busy\n");
  750. goto out_unlock;
  751. }
  752. /*
  753. * TODO:
  754. * make sure the dma_halt() function really un-wedges the
  755. * controller as much as possible
  756. */
  757. dma_halt(chan);
  758. /*
  759. * If there are some link descriptors which have not been
  760. * transferred, we need to start the controller
  761. */
  762. /*
  763. * Move all elements from the queue of pending transactions
  764. * onto the list of running transactions
  765. */
  766. desc = list_first_entry(&chan->ld_pending, struct fsl_desc_sw, node);
  767. list_splice_tail_init(&chan->ld_pending, &chan->ld_running);
  768. /*
  769. * Program the descriptor's address into the DMA controller,
  770. * then start the DMA transaction
  771. */
  772. set_cdar(chan, desc->async_tx.phys);
  773. dma_start(chan);
  774. out_unlock:
  775. spin_unlock_irqrestore(&chan->desc_lock, flags);
  776. }
  777. /**
  778. * fsl_dma_memcpy_issue_pending - Issue the DMA start command
  779. * @chan : Freescale DMA channel
  780. */
  781. static void fsl_dma_memcpy_issue_pending(struct dma_chan *dchan)
  782. {
  783. struct fsldma_chan *chan = to_fsl_chan(dchan);
  784. fsl_chan_xfer_ld_queue(chan);
  785. }
  786. /**
  787. * fsl_tx_status - Determine the DMA status
  788. * @chan : Freescale DMA channel
  789. */
  790. static enum dma_status fsl_tx_status(struct dma_chan *dchan,
  791. dma_cookie_t cookie,
  792. struct dma_tx_state *txstate)
  793. {
  794. struct fsldma_chan *chan = to_fsl_chan(dchan);
  795. dma_cookie_t last_used;
  796. dma_cookie_t last_complete;
  797. fsl_chan_ld_cleanup(chan);
  798. last_used = dchan->cookie;
  799. last_complete = chan->completed_cookie;
  800. dma_set_tx_state(txstate, last_complete, last_used, 0);
  801. return dma_async_is_complete(cookie, last_complete, last_used);
  802. }
  803. /*----------------------------------------------------------------------------*/
  804. /* Interrupt Handling */
  805. /*----------------------------------------------------------------------------*/
  806. static irqreturn_t fsldma_chan_irq(int irq, void *data)
  807. {
  808. struct fsldma_chan *chan = data;
  809. int update_cookie = 0;
  810. int xfer_ld_q = 0;
  811. u32 stat;
  812. /* save and clear the status register */
  813. stat = get_sr(chan);
  814. set_sr(chan, stat);
  815. dev_dbg(chan->dev, "irq: channel %d, stat = 0x%x\n", chan->id, stat);
  816. stat &= ~(FSL_DMA_SR_CB | FSL_DMA_SR_CH);
  817. if (!stat)
  818. return IRQ_NONE;
  819. if (stat & FSL_DMA_SR_TE)
  820. dev_err(chan->dev, "Transfer Error!\n");
  821. /*
  822. * Programming Error
  823. * The DMA_INTERRUPT async_tx is a NULL transfer, which will
  824. * triger a PE interrupt.
  825. */
  826. if (stat & FSL_DMA_SR_PE) {
  827. dev_dbg(chan->dev, "irq: Programming Error INT\n");
  828. if (get_bcr(chan) == 0) {
  829. /* BCR register is 0, this is a DMA_INTERRUPT async_tx.
  830. * Now, update the completed cookie, and continue the
  831. * next uncompleted transfer.
  832. */
  833. update_cookie = 1;
  834. xfer_ld_q = 1;
  835. }
  836. stat &= ~FSL_DMA_SR_PE;
  837. }
  838. /*
  839. * If the link descriptor segment transfer finishes,
  840. * we will recycle the used descriptor.
  841. */
  842. if (stat & FSL_DMA_SR_EOSI) {
  843. dev_dbg(chan->dev, "irq: End-of-segments INT\n");
  844. dev_dbg(chan->dev, "irq: clndar 0x%llx, nlndar 0x%llx\n",
  845. (unsigned long long)get_cdar(chan),
  846. (unsigned long long)get_ndar(chan));
  847. stat &= ~FSL_DMA_SR_EOSI;
  848. update_cookie = 1;
  849. }
  850. /*
  851. * For MPC8349, EOCDI event need to update cookie
  852. * and start the next transfer if it exist.
  853. */
  854. if (stat & FSL_DMA_SR_EOCDI) {
  855. dev_dbg(chan->dev, "irq: End-of-Chain link INT\n");
  856. stat &= ~FSL_DMA_SR_EOCDI;
  857. update_cookie = 1;
  858. xfer_ld_q = 1;
  859. }
  860. /*
  861. * If it current transfer is the end-of-transfer,
  862. * we should clear the Channel Start bit for
  863. * prepare next transfer.
  864. */
  865. if (stat & FSL_DMA_SR_EOLNI) {
  866. dev_dbg(chan->dev, "irq: End-of-link INT\n");
  867. stat &= ~FSL_DMA_SR_EOLNI;
  868. xfer_ld_q = 1;
  869. }
  870. if (update_cookie)
  871. fsl_dma_update_completed_cookie(chan);
  872. if (xfer_ld_q)
  873. fsl_chan_xfer_ld_queue(chan);
  874. if (stat)
  875. dev_dbg(chan->dev, "irq: unhandled sr 0x%02x\n", stat);
  876. dev_dbg(chan->dev, "irq: Exit\n");
  877. tasklet_schedule(&chan->tasklet);
  878. return IRQ_HANDLED;
  879. }
  880. static void dma_do_tasklet(unsigned long data)
  881. {
  882. struct fsldma_chan *chan = (struct fsldma_chan *)data;
  883. fsl_chan_ld_cleanup(chan);
  884. }
  885. static irqreturn_t fsldma_ctrl_irq(int irq, void *data)
  886. {
  887. struct fsldma_device *fdev = data;
  888. struct fsldma_chan *chan;
  889. unsigned int handled = 0;
  890. u32 gsr, mask;
  891. int i;
  892. gsr = (fdev->feature & FSL_DMA_BIG_ENDIAN) ? in_be32(fdev->regs)
  893. : in_le32(fdev->regs);
  894. mask = 0xff000000;
  895. dev_dbg(fdev->dev, "IRQ: gsr 0x%.8x\n", gsr);
  896. for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
  897. chan = fdev->chan[i];
  898. if (!chan)
  899. continue;
  900. if (gsr & mask) {
  901. dev_dbg(fdev->dev, "IRQ: chan %d\n", chan->id);
  902. fsldma_chan_irq(irq, chan);
  903. handled++;
  904. }
  905. gsr &= ~mask;
  906. mask >>= 8;
  907. }
  908. return IRQ_RETVAL(handled);
  909. }
  910. static void fsldma_free_irqs(struct fsldma_device *fdev)
  911. {
  912. struct fsldma_chan *chan;
  913. int i;
  914. if (fdev->irq != NO_IRQ) {
  915. dev_dbg(fdev->dev, "free per-controller IRQ\n");
  916. free_irq(fdev->irq, fdev);
  917. return;
  918. }
  919. for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
  920. chan = fdev->chan[i];
  921. if (chan && chan->irq != NO_IRQ) {
  922. dev_dbg(fdev->dev, "free channel %d IRQ\n", chan->id);
  923. free_irq(chan->irq, chan);
  924. }
  925. }
  926. }
  927. static int fsldma_request_irqs(struct fsldma_device *fdev)
  928. {
  929. struct fsldma_chan *chan;
  930. int ret;
  931. int i;
  932. /* if we have a per-controller IRQ, use that */
  933. if (fdev->irq != NO_IRQ) {
  934. dev_dbg(fdev->dev, "request per-controller IRQ\n");
  935. ret = request_irq(fdev->irq, fsldma_ctrl_irq, IRQF_SHARED,
  936. "fsldma-controller", fdev);
  937. return ret;
  938. }
  939. /* no per-controller IRQ, use the per-channel IRQs */
  940. for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
  941. chan = fdev->chan[i];
  942. if (!chan)
  943. continue;
  944. if (chan->irq == NO_IRQ) {
  945. dev_err(fdev->dev, "no interrupts property defined for "
  946. "DMA channel %d. Please fix your "
  947. "device tree\n", chan->id);
  948. ret = -ENODEV;
  949. goto out_unwind;
  950. }
  951. dev_dbg(fdev->dev, "request channel %d IRQ\n", chan->id);
  952. ret = request_irq(chan->irq, fsldma_chan_irq, IRQF_SHARED,
  953. "fsldma-chan", chan);
  954. if (ret) {
  955. dev_err(fdev->dev, "unable to request IRQ for DMA "
  956. "channel %d\n", chan->id);
  957. goto out_unwind;
  958. }
  959. }
  960. return 0;
  961. out_unwind:
  962. for (/* none */; i >= 0; i--) {
  963. chan = fdev->chan[i];
  964. if (!chan)
  965. continue;
  966. if (chan->irq == NO_IRQ)
  967. continue;
  968. free_irq(chan->irq, chan);
  969. }
  970. return ret;
  971. }
  972. /*----------------------------------------------------------------------------*/
  973. /* OpenFirmware Subsystem */
  974. /*----------------------------------------------------------------------------*/
  975. static int __devinit fsl_dma_chan_probe(struct fsldma_device *fdev,
  976. struct device_node *node, u32 feature, const char *compatible)
  977. {
  978. struct fsldma_chan *chan;
  979. struct resource res;
  980. int err;
  981. /* alloc channel */
  982. chan = kzalloc(sizeof(*chan), GFP_KERNEL);
  983. if (!chan) {
  984. dev_err(fdev->dev, "no free memory for DMA channels!\n");
  985. err = -ENOMEM;
  986. goto out_return;
  987. }
  988. /* ioremap registers for use */
  989. chan->regs = of_iomap(node, 0);
  990. if (!chan->regs) {
  991. dev_err(fdev->dev, "unable to ioremap registers\n");
  992. err = -ENOMEM;
  993. goto out_free_chan;
  994. }
  995. err = of_address_to_resource(node, 0, &res);
  996. if (err) {
  997. dev_err(fdev->dev, "unable to find 'reg' property\n");
  998. goto out_iounmap_regs;
  999. }
  1000. chan->feature = feature;
  1001. if (!fdev->feature)
  1002. fdev->feature = chan->feature;
  1003. /*
  1004. * If the DMA device's feature is different than the feature
  1005. * of its channels, report the bug
  1006. */
  1007. WARN_ON(fdev->feature != chan->feature);
  1008. chan->dev = fdev->dev;
  1009. chan->id = ((res.start - 0x100) & 0xfff) >> 7;
  1010. if (chan->id >= FSL_DMA_MAX_CHANS_PER_DEVICE) {
  1011. dev_err(fdev->dev, "too many channels for device\n");
  1012. err = -EINVAL;
  1013. goto out_iounmap_regs;
  1014. }
  1015. fdev->chan[chan->id] = chan;
  1016. tasklet_init(&chan->tasklet, dma_do_tasklet, (unsigned long)chan);
  1017. /* Initialize the channel */
  1018. dma_init(chan);
  1019. /* Clear cdar registers */
  1020. set_cdar(chan, 0);
  1021. switch (chan->feature & FSL_DMA_IP_MASK) {
  1022. case FSL_DMA_IP_85XX:
  1023. chan->toggle_ext_pause = fsl_chan_toggle_ext_pause;
  1024. case FSL_DMA_IP_83XX:
  1025. chan->toggle_ext_start = fsl_chan_toggle_ext_start;
  1026. chan->set_src_loop_size = fsl_chan_set_src_loop_size;
  1027. chan->set_dst_loop_size = fsl_chan_set_dst_loop_size;
  1028. chan->set_request_count = fsl_chan_set_request_count;
  1029. }
  1030. spin_lock_init(&chan->desc_lock);
  1031. INIT_LIST_HEAD(&chan->ld_pending);
  1032. INIT_LIST_HEAD(&chan->ld_running);
  1033. chan->common.device = &fdev->common;
  1034. /* find the IRQ line, if it exists in the device tree */
  1035. chan->irq = irq_of_parse_and_map(node, 0);
  1036. /* Add the channel to DMA device channel list */
  1037. list_add_tail(&chan->common.device_node, &fdev->common.channels);
  1038. fdev->common.chancnt++;
  1039. dev_info(fdev->dev, "#%d (%s), irq %d\n", chan->id, compatible,
  1040. chan->irq != NO_IRQ ? chan->irq : fdev->irq);
  1041. return 0;
  1042. out_iounmap_regs:
  1043. iounmap(chan->regs);
  1044. out_free_chan:
  1045. kfree(chan);
  1046. out_return:
  1047. return err;
  1048. }
  1049. static void fsl_dma_chan_remove(struct fsldma_chan *chan)
  1050. {
  1051. irq_dispose_mapping(chan->irq);
  1052. list_del(&chan->common.device_node);
  1053. iounmap(chan->regs);
  1054. kfree(chan);
  1055. }
  1056. static int __devinit fsldma_of_probe(struct platform_device *op,
  1057. const struct of_device_id *match)
  1058. {
  1059. struct fsldma_device *fdev;
  1060. struct device_node *child;
  1061. int err;
  1062. fdev = kzalloc(sizeof(*fdev), GFP_KERNEL);
  1063. if (!fdev) {
  1064. dev_err(&op->dev, "No enough memory for 'priv'\n");
  1065. err = -ENOMEM;
  1066. goto out_return;
  1067. }
  1068. fdev->dev = &op->dev;
  1069. INIT_LIST_HEAD(&fdev->common.channels);
  1070. /* ioremap the registers for use */
  1071. fdev->regs = of_iomap(op->dev.of_node, 0);
  1072. if (!fdev->regs) {
  1073. dev_err(&op->dev, "unable to ioremap registers\n");
  1074. err = -ENOMEM;
  1075. goto out_free_fdev;
  1076. }
  1077. /* map the channel IRQ if it exists, but don't hookup the handler yet */
  1078. fdev->irq = irq_of_parse_and_map(op->dev.of_node, 0);
  1079. dma_cap_set(DMA_MEMCPY, fdev->common.cap_mask);
  1080. dma_cap_set(DMA_INTERRUPT, fdev->common.cap_mask);
  1081. dma_cap_set(DMA_SG, fdev->common.cap_mask);
  1082. dma_cap_set(DMA_SLAVE, fdev->common.cap_mask);
  1083. fdev->common.device_alloc_chan_resources = fsl_dma_alloc_chan_resources;
  1084. fdev->common.device_free_chan_resources = fsl_dma_free_chan_resources;
  1085. fdev->common.device_prep_dma_interrupt = fsl_dma_prep_interrupt;
  1086. fdev->common.device_prep_dma_memcpy = fsl_dma_prep_memcpy;
  1087. fdev->common.device_prep_dma_sg = fsl_dma_prep_sg;
  1088. fdev->common.device_tx_status = fsl_tx_status;
  1089. fdev->common.device_issue_pending = fsl_dma_memcpy_issue_pending;
  1090. fdev->common.device_prep_slave_sg = fsl_dma_prep_slave_sg;
  1091. fdev->common.device_control = fsl_dma_device_control;
  1092. fdev->common.dev = &op->dev;
  1093. dma_set_mask(&(op->dev), DMA_BIT_MASK(36));
  1094. dev_set_drvdata(&op->dev, fdev);
  1095. /*
  1096. * We cannot use of_platform_bus_probe() because there is no
  1097. * of_platform_bus_remove(). Instead, we manually instantiate every DMA
  1098. * channel object.
  1099. */
  1100. for_each_child_of_node(op->dev.of_node, child) {
  1101. if (of_device_is_compatible(child, "fsl,eloplus-dma-channel")) {
  1102. fsl_dma_chan_probe(fdev, child,
  1103. FSL_DMA_IP_85XX | FSL_DMA_BIG_ENDIAN,
  1104. "fsl,eloplus-dma-channel");
  1105. }
  1106. if (of_device_is_compatible(child, "fsl,elo-dma-channel")) {
  1107. fsl_dma_chan_probe(fdev, child,
  1108. FSL_DMA_IP_83XX | FSL_DMA_LITTLE_ENDIAN,
  1109. "fsl,elo-dma-channel");
  1110. }
  1111. }
  1112. /*
  1113. * Hookup the IRQ handler(s)
  1114. *
  1115. * If we have a per-controller interrupt, we prefer that to the
  1116. * per-channel interrupts to reduce the number of shared interrupt
  1117. * handlers on the same IRQ line
  1118. */
  1119. err = fsldma_request_irqs(fdev);
  1120. if (err) {
  1121. dev_err(fdev->dev, "unable to request IRQs\n");
  1122. goto out_free_fdev;
  1123. }
  1124. dma_async_device_register(&fdev->common);
  1125. return 0;
  1126. out_free_fdev:
  1127. irq_dispose_mapping(fdev->irq);
  1128. kfree(fdev);
  1129. out_return:
  1130. return err;
  1131. }
  1132. static int fsldma_of_remove(struct platform_device *op)
  1133. {
  1134. struct fsldma_device *fdev;
  1135. unsigned int i;
  1136. fdev = dev_get_drvdata(&op->dev);
  1137. dma_async_device_unregister(&fdev->common);
  1138. fsldma_free_irqs(fdev);
  1139. for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++) {
  1140. if (fdev->chan[i])
  1141. fsl_dma_chan_remove(fdev->chan[i]);
  1142. }
  1143. iounmap(fdev->regs);
  1144. dev_set_drvdata(&op->dev, NULL);
  1145. kfree(fdev);
  1146. return 0;
  1147. }
  1148. static const struct of_device_id fsldma_of_ids[] = {
  1149. { .compatible = "fsl,eloplus-dma", },
  1150. { .compatible = "fsl,elo-dma", },
  1151. {}
  1152. };
  1153. static struct of_platform_driver fsldma_of_driver = {
  1154. .driver = {
  1155. .name = "fsl-elo-dma",
  1156. .owner = THIS_MODULE,
  1157. .of_match_table = fsldma_of_ids,
  1158. },
  1159. .probe = fsldma_of_probe,
  1160. .remove = fsldma_of_remove,
  1161. };
  1162. /*----------------------------------------------------------------------------*/
  1163. /* Module Init / Exit */
  1164. /*----------------------------------------------------------------------------*/
  1165. static __init int fsldma_init(void)
  1166. {
  1167. int ret;
  1168. pr_info("Freescale Elo / Elo Plus DMA driver\n");
  1169. ret = of_register_platform_driver(&fsldma_of_driver);
  1170. if (ret)
  1171. pr_err("fsldma: failed to register platform driver\n");
  1172. return ret;
  1173. }
  1174. static void __exit fsldma_exit(void)
  1175. {
  1176. of_unregister_platform_driver(&fsldma_of_driver);
  1177. }
  1178. subsys_initcall(fsldma_init);
  1179. module_exit(fsldma_exit);
  1180. MODULE_DESCRIPTION("Freescale Elo / Elo Plus DMA driver");
  1181. MODULE_LICENSE("GPL");