fsldma.c 35 KB

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