fsldma.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. /*
  2. * Freescale MPC85xx, MPC83xx DMA Engine support
  3. *
  4. * Copyright (C) 2007 Freescale Semiconductor, Inc. All rights reserved.
  5. *
  6. * Author:
  7. * Zhang Wei <wei.zhang@freescale.com>, Jul 2007
  8. * Ebony Zhu <ebony.zhu@freescale.com>, May 2007
  9. *
  10. * Description:
  11. * DMA engine driver for Freescale MPC8540 DMA controller, which is
  12. * also fit for MPC8560, MPC8555, MPC8548, MPC8641, and etc.
  13. * The support for MPC8349 DMA contorller is also added.
  14. *
  15. * This driver instructs the DMA controller to issue the PCI Read Multiple
  16. * command for PCI read operations, instead of using the default PCI Read Line
  17. * command. Please be aware that this setting may result in read pre-fetching
  18. * on some platforms.
  19. *
  20. * This is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation; either version 2 of the License, or
  23. * (at your option) any later version.
  24. *
  25. */
  26. #include <linux/init.h>
  27. #include <linux/module.h>
  28. #include <linux/pci.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/dmaengine.h>
  31. #include <linux/delay.h>
  32. #include <linux/dma-mapping.h>
  33. #include <linux/dmapool.h>
  34. #include <linux/of_platform.h>
  35. #include "fsldma.h"
  36. static void dma_init(struct fsl_dma_chan *fsl_chan)
  37. {
  38. /* Reset the channel */
  39. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr, 0, 32);
  40. switch (fsl_chan->feature & FSL_DMA_IP_MASK) {
  41. case FSL_DMA_IP_85XX:
  42. /* Set the channel to below modes:
  43. * EIE - Error interrupt enable
  44. * EOSIE - End of segments interrupt enable (basic mode)
  45. * EOLNIE - End of links interrupt enable
  46. */
  47. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr, FSL_DMA_MR_EIE
  48. | FSL_DMA_MR_EOLNIE | FSL_DMA_MR_EOSIE, 32);
  49. break;
  50. case FSL_DMA_IP_83XX:
  51. /* Set the channel to below modes:
  52. * EOTIE - End-of-transfer interrupt enable
  53. * PRC_RM - PCI read multiple
  54. */
  55. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr, FSL_DMA_MR_EOTIE
  56. | FSL_DMA_MR_PRC_RM, 32);
  57. break;
  58. }
  59. }
  60. static void set_sr(struct fsl_dma_chan *fsl_chan, u32 val)
  61. {
  62. DMA_OUT(fsl_chan, &fsl_chan->reg_base->sr, val, 32);
  63. }
  64. static u32 get_sr(struct fsl_dma_chan *fsl_chan)
  65. {
  66. return DMA_IN(fsl_chan, &fsl_chan->reg_base->sr, 32);
  67. }
  68. static void set_desc_cnt(struct fsl_dma_chan *fsl_chan,
  69. struct fsl_dma_ld_hw *hw, u32 count)
  70. {
  71. hw->count = CPU_TO_DMA(fsl_chan, count, 32);
  72. }
  73. static void set_desc_src(struct fsl_dma_chan *fsl_chan,
  74. struct fsl_dma_ld_hw *hw, dma_addr_t src)
  75. {
  76. u64 snoop_bits;
  77. snoop_bits = ((fsl_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(fsl_chan, snoop_bits | src, 64);
  80. }
  81. static void set_desc_dest(struct fsl_dma_chan *fsl_chan,
  82. struct fsl_dma_ld_hw *hw, dma_addr_t dest)
  83. {
  84. u64 snoop_bits;
  85. snoop_bits = ((fsl_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(fsl_chan, snoop_bits | dest, 64);
  88. }
  89. static void set_desc_next(struct fsl_dma_chan *fsl_chan,
  90. struct fsl_dma_ld_hw *hw, dma_addr_t next)
  91. {
  92. u64 snoop_bits;
  93. snoop_bits = ((fsl_chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_83XX)
  94. ? FSL_DMA_SNEN : 0;
  95. hw->next_ln_addr = CPU_TO_DMA(fsl_chan, snoop_bits | next, 64);
  96. }
  97. static void set_cdar(struct fsl_dma_chan *fsl_chan, dma_addr_t addr)
  98. {
  99. DMA_OUT(fsl_chan, &fsl_chan->reg_base->cdar, addr | FSL_DMA_SNEN, 64);
  100. }
  101. static dma_addr_t get_cdar(struct fsl_dma_chan *fsl_chan)
  102. {
  103. return DMA_IN(fsl_chan, &fsl_chan->reg_base->cdar, 64) & ~FSL_DMA_SNEN;
  104. }
  105. static void set_ndar(struct fsl_dma_chan *fsl_chan, dma_addr_t addr)
  106. {
  107. DMA_OUT(fsl_chan, &fsl_chan->reg_base->ndar, addr, 64);
  108. }
  109. static dma_addr_t get_ndar(struct fsl_dma_chan *fsl_chan)
  110. {
  111. return DMA_IN(fsl_chan, &fsl_chan->reg_base->ndar, 64);
  112. }
  113. static u32 get_bcr(struct fsl_dma_chan *fsl_chan)
  114. {
  115. return DMA_IN(fsl_chan, &fsl_chan->reg_base->bcr, 32);
  116. }
  117. static int dma_is_idle(struct fsl_dma_chan *fsl_chan)
  118. {
  119. u32 sr = get_sr(fsl_chan);
  120. return (!(sr & FSL_DMA_SR_CB)) || (sr & FSL_DMA_SR_CH);
  121. }
  122. static void dma_start(struct fsl_dma_chan *fsl_chan)
  123. {
  124. u32 mr_set = 0;;
  125. if (fsl_chan->feature & FSL_DMA_CHAN_PAUSE_EXT) {
  126. DMA_OUT(fsl_chan, &fsl_chan->reg_base->bcr, 0, 32);
  127. mr_set |= FSL_DMA_MR_EMP_EN;
  128. } else
  129. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr,
  130. DMA_IN(fsl_chan, &fsl_chan->reg_base->mr, 32)
  131. & ~FSL_DMA_MR_EMP_EN, 32);
  132. if (fsl_chan->feature & FSL_DMA_CHAN_START_EXT)
  133. mr_set |= FSL_DMA_MR_EMS_EN;
  134. else
  135. mr_set |= FSL_DMA_MR_CS;
  136. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr,
  137. DMA_IN(fsl_chan, &fsl_chan->reg_base->mr, 32)
  138. | mr_set, 32);
  139. }
  140. static void dma_halt(struct fsl_dma_chan *fsl_chan)
  141. {
  142. int i;
  143. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr,
  144. DMA_IN(fsl_chan, &fsl_chan->reg_base->mr, 32) | FSL_DMA_MR_CA,
  145. 32);
  146. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr,
  147. DMA_IN(fsl_chan, &fsl_chan->reg_base->mr, 32) & ~(FSL_DMA_MR_CS
  148. | FSL_DMA_MR_EMS_EN | FSL_DMA_MR_CA), 32);
  149. for (i = 0; i < 100; i++) {
  150. if (dma_is_idle(fsl_chan))
  151. break;
  152. udelay(10);
  153. }
  154. if (i >= 100 && !dma_is_idle(fsl_chan))
  155. dev_err(fsl_chan->dev, "DMA halt timeout!\n");
  156. }
  157. static void set_ld_eol(struct fsl_dma_chan *fsl_chan,
  158. struct fsl_desc_sw *desc)
  159. {
  160. u64 snoop_bits;
  161. snoop_bits = ((fsl_chan->feature & FSL_DMA_IP_MASK) == FSL_DMA_IP_83XX)
  162. ? FSL_DMA_SNEN : 0;
  163. desc->hw.next_ln_addr = CPU_TO_DMA(fsl_chan,
  164. DMA_TO_CPU(fsl_chan, desc->hw.next_ln_addr, 64) | FSL_DMA_EOL
  165. | snoop_bits, 64);
  166. }
  167. static void append_ld_queue(struct fsl_dma_chan *fsl_chan,
  168. struct fsl_desc_sw *new_desc)
  169. {
  170. struct fsl_desc_sw *queue_tail = to_fsl_desc(fsl_chan->ld_queue.prev);
  171. if (list_empty(&fsl_chan->ld_queue))
  172. return;
  173. /* Link to the new descriptor physical address and
  174. * Enable End-of-segment interrupt for
  175. * the last link descriptor.
  176. * (the previous node's next link descriptor)
  177. *
  178. * For FSL_DMA_IP_83xx, the snoop enable bit need be set.
  179. */
  180. queue_tail->hw.next_ln_addr = CPU_TO_DMA(fsl_chan,
  181. new_desc->async_tx.phys | FSL_DMA_EOSIE |
  182. (((fsl_chan->feature & FSL_DMA_IP_MASK)
  183. == FSL_DMA_IP_83XX) ? FSL_DMA_SNEN : 0), 64);
  184. }
  185. /**
  186. * fsl_chan_set_src_loop_size - Set source address hold transfer size
  187. * @fsl_chan : Freescale DMA channel
  188. * @size : Address loop size, 0 for disable loop
  189. *
  190. * The set source address hold transfer size. The source
  191. * address hold or loop transfer size is when the DMA transfer
  192. * data from source address (SA), if the loop size is 4, the DMA will
  193. * read data from SA, SA + 1, SA + 2, SA + 3, then loop back to SA,
  194. * SA + 1 ... and so on.
  195. */
  196. static void fsl_chan_set_src_loop_size(struct fsl_dma_chan *fsl_chan, int size)
  197. {
  198. switch (size) {
  199. case 0:
  200. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr,
  201. DMA_IN(fsl_chan, &fsl_chan->reg_base->mr, 32) &
  202. (~FSL_DMA_MR_SAHE), 32);
  203. break;
  204. case 1:
  205. case 2:
  206. case 4:
  207. case 8:
  208. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr,
  209. DMA_IN(fsl_chan, &fsl_chan->reg_base->mr, 32) |
  210. FSL_DMA_MR_SAHE | (__ilog2(size) << 14),
  211. 32);
  212. break;
  213. }
  214. }
  215. /**
  216. * fsl_chan_set_dest_loop_size - Set destination address hold transfer size
  217. * @fsl_chan : Freescale DMA channel
  218. * @size : Address loop size, 0 for disable loop
  219. *
  220. * The set destination address hold transfer size. The destination
  221. * address hold or loop transfer size is when the DMA transfer
  222. * data to destination address (TA), if the loop size is 4, the DMA will
  223. * write data to TA, TA + 1, TA + 2, TA + 3, then loop back to TA,
  224. * TA + 1 ... and so on.
  225. */
  226. static void fsl_chan_set_dest_loop_size(struct fsl_dma_chan *fsl_chan, int size)
  227. {
  228. switch (size) {
  229. case 0:
  230. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr,
  231. DMA_IN(fsl_chan, &fsl_chan->reg_base->mr, 32) &
  232. (~FSL_DMA_MR_DAHE), 32);
  233. break;
  234. case 1:
  235. case 2:
  236. case 4:
  237. case 8:
  238. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr,
  239. DMA_IN(fsl_chan, &fsl_chan->reg_base->mr, 32) |
  240. FSL_DMA_MR_DAHE | (__ilog2(size) << 16),
  241. 32);
  242. break;
  243. }
  244. }
  245. /**
  246. * fsl_chan_toggle_ext_pause - Toggle channel external pause status
  247. * @fsl_chan : Freescale DMA channel
  248. * @size : Pause control size, 0 for disable external pause control.
  249. * The maximum is 1024.
  250. *
  251. * The Freescale DMA channel can be controlled by the external
  252. * signal DREQ#. The pause control size is how many bytes are allowed
  253. * to transfer before pausing the channel, after which a new assertion
  254. * of DREQ# resumes channel operation.
  255. */
  256. static void fsl_chan_toggle_ext_pause(struct fsl_dma_chan *fsl_chan, int size)
  257. {
  258. if (size > 1024)
  259. return;
  260. if (size) {
  261. DMA_OUT(fsl_chan, &fsl_chan->reg_base->mr,
  262. DMA_IN(fsl_chan, &fsl_chan->reg_base->mr, 32)
  263. | ((__ilog2(size) << 24) & 0x0f000000),
  264. 32);
  265. fsl_chan->feature |= FSL_DMA_CHAN_PAUSE_EXT;
  266. } else
  267. fsl_chan->feature &= ~FSL_DMA_CHAN_PAUSE_EXT;
  268. }
  269. /**
  270. * fsl_chan_toggle_ext_start - Toggle channel external start status
  271. * @fsl_chan : Freescale DMA channel
  272. * @enable : 0 is disabled, 1 is enabled.
  273. *
  274. * If enable the external start, the channel can be started by an
  275. * external DMA start pin. So the dma_start() does not start the
  276. * transfer immediately. The DMA channel will wait for the
  277. * control pin asserted.
  278. */
  279. static void fsl_chan_toggle_ext_start(struct fsl_dma_chan *fsl_chan, int enable)
  280. {
  281. if (enable)
  282. fsl_chan->feature |= FSL_DMA_CHAN_START_EXT;
  283. else
  284. fsl_chan->feature &= ~FSL_DMA_CHAN_START_EXT;
  285. }
  286. static dma_cookie_t fsl_dma_tx_submit(struct dma_async_tx_descriptor *tx)
  287. {
  288. struct fsl_dma_chan *fsl_chan = to_fsl_chan(tx->chan);
  289. struct fsl_desc_sw *desc;
  290. unsigned long flags;
  291. dma_cookie_t cookie;
  292. /* cookie increment and adding to ld_queue must be atomic */
  293. spin_lock_irqsave(&fsl_chan->desc_lock, flags);
  294. cookie = fsl_chan->common.cookie;
  295. list_for_each_entry(desc, &tx->tx_list, node) {
  296. cookie++;
  297. if (cookie < 0)
  298. cookie = 1;
  299. desc->async_tx.cookie = cookie;
  300. }
  301. fsl_chan->common.cookie = cookie;
  302. append_ld_queue(fsl_chan, tx_to_fsl_desc(tx));
  303. list_splice_init(&tx->tx_list, fsl_chan->ld_queue.prev);
  304. spin_unlock_irqrestore(&fsl_chan->desc_lock, flags);
  305. return cookie;
  306. }
  307. /**
  308. * fsl_dma_alloc_descriptor - Allocate descriptor from channel's DMA pool.
  309. * @fsl_chan : Freescale DMA channel
  310. *
  311. * Return - The descriptor allocated. NULL for failed.
  312. */
  313. static struct fsl_desc_sw *fsl_dma_alloc_descriptor(
  314. struct fsl_dma_chan *fsl_chan)
  315. {
  316. dma_addr_t pdesc;
  317. struct fsl_desc_sw *desc_sw;
  318. desc_sw = dma_pool_alloc(fsl_chan->desc_pool, GFP_ATOMIC, &pdesc);
  319. if (desc_sw) {
  320. memset(desc_sw, 0, sizeof(struct fsl_desc_sw));
  321. dma_async_tx_descriptor_init(&desc_sw->async_tx,
  322. &fsl_chan->common);
  323. desc_sw->async_tx.tx_submit = fsl_dma_tx_submit;
  324. desc_sw->async_tx.phys = pdesc;
  325. }
  326. return desc_sw;
  327. }
  328. /**
  329. * fsl_dma_alloc_chan_resources - Allocate resources for DMA channel.
  330. * @fsl_chan : Freescale DMA channel
  331. *
  332. * This function will create a dma pool for descriptor allocation.
  333. *
  334. * Return - The number of descriptors allocated.
  335. */
  336. static int fsl_dma_alloc_chan_resources(struct dma_chan *chan)
  337. {
  338. struct fsl_dma_chan *fsl_chan = to_fsl_chan(chan);
  339. /* Has this channel already been allocated? */
  340. if (fsl_chan->desc_pool)
  341. return 1;
  342. /* We need the descriptor to be aligned to 32bytes
  343. * for meeting FSL DMA specification requirement.
  344. */
  345. fsl_chan->desc_pool = dma_pool_create("fsl_dma_engine_desc_pool",
  346. fsl_chan->dev, sizeof(struct fsl_desc_sw),
  347. 32, 0);
  348. if (!fsl_chan->desc_pool) {
  349. dev_err(fsl_chan->dev, "No memory for channel %d "
  350. "descriptor dma pool.\n", fsl_chan->id);
  351. return 0;
  352. }
  353. return 1;
  354. }
  355. /**
  356. * fsl_dma_free_chan_resources - Free all resources of the channel.
  357. * @fsl_chan : Freescale DMA channel
  358. */
  359. static void fsl_dma_free_chan_resources(struct dma_chan *chan)
  360. {
  361. struct fsl_dma_chan *fsl_chan = to_fsl_chan(chan);
  362. struct fsl_desc_sw *desc, *_desc;
  363. unsigned long flags;
  364. dev_dbg(fsl_chan->dev, "Free all channel resources.\n");
  365. spin_lock_irqsave(&fsl_chan->desc_lock, flags);
  366. list_for_each_entry_safe(desc, _desc, &fsl_chan->ld_queue, node) {
  367. #ifdef FSL_DMA_LD_DEBUG
  368. dev_dbg(fsl_chan->dev,
  369. "LD %p will be released.\n", desc);
  370. #endif
  371. list_del(&desc->node);
  372. /* free link descriptor */
  373. dma_pool_free(fsl_chan->desc_pool, desc, desc->async_tx.phys);
  374. }
  375. spin_unlock_irqrestore(&fsl_chan->desc_lock, flags);
  376. dma_pool_destroy(fsl_chan->desc_pool);
  377. fsl_chan->desc_pool = NULL;
  378. }
  379. static struct dma_async_tx_descriptor *
  380. fsl_dma_prep_interrupt(struct dma_chan *chan, unsigned long flags)
  381. {
  382. struct fsl_dma_chan *fsl_chan;
  383. struct fsl_desc_sw *new;
  384. if (!chan)
  385. return NULL;
  386. fsl_chan = to_fsl_chan(chan);
  387. new = fsl_dma_alloc_descriptor(fsl_chan);
  388. if (!new) {
  389. dev_err(fsl_chan->dev, "No free memory for link descriptor\n");
  390. return NULL;
  391. }
  392. new->async_tx.cookie = -EBUSY;
  393. new->async_tx.flags = flags;
  394. /* Insert the link descriptor to the LD ring */
  395. list_add_tail(&new->node, &new->async_tx.tx_list);
  396. /* Set End-of-link to the last link descriptor of new list*/
  397. set_ld_eol(fsl_chan, new);
  398. return &new->async_tx;
  399. }
  400. static struct dma_async_tx_descriptor *fsl_dma_prep_memcpy(
  401. struct dma_chan *chan, dma_addr_t dma_dest, dma_addr_t dma_src,
  402. size_t len, unsigned long flags)
  403. {
  404. struct fsl_dma_chan *fsl_chan;
  405. struct fsl_desc_sw *first = NULL, *prev = NULL, *new;
  406. struct list_head *list;
  407. size_t copy;
  408. if (!chan)
  409. return NULL;
  410. if (!len)
  411. return NULL;
  412. fsl_chan = to_fsl_chan(chan);
  413. do {
  414. /* Allocate the link descriptor from DMA pool */
  415. new = fsl_dma_alloc_descriptor(fsl_chan);
  416. if (!new) {
  417. dev_err(fsl_chan->dev,
  418. "No free memory for link descriptor\n");
  419. goto fail;
  420. }
  421. #ifdef FSL_DMA_LD_DEBUG
  422. dev_dbg(fsl_chan->dev, "new link desc alloc %p\n", new);
  423. #endif
  424. copy = min(len, (size_t)FSL_DMA_BCR_MAX_CNT);
  425. set_desc_cnt(fsl_chan, &new->hw, copy);
  426. set_desc_src(fsl_chan, &new->hw, dma_src);
  427. set_desc_dest(fsl_chan, &new->hw, dma_dest);
  428. if (!first)
  429. first = new;
  430. else
  431. set_desc_next(fsl_chan, &prev->hw, new->async_tx.phys);
  432. new->async_tx.cookie = 0;
  433. async_tx_ack(&new->async_tx);
  434. prev = new;
  435. len -= copy;
  436. dma_src += copy;
  437. dma_dest += copy;
  438. /* Insert the link descriptor to the LD ring */
  439. list_add_tail(&new->node, &first->async_tx.tx_list);
  440. } while (len);
  441. new->async_tx.flags = flags; /* client is in control of this ack */
  442. new->async_tx.cookie = -EBUSY;
  443. /* Set End-of-link to the last link descriptor of new list*/
  444. set_ld_eol(fsl_chan, new);
  445. return &first->async_tx;
  446. fail:
  447. if (!first)
  448. return NULL;
  449. list = &first->async_tx.tx_list;
  450. list_for_each_entry_safe_reverse(new, prev, list, node) {
  451. list_del(&new->node);
  452. dma_pool_free(fsl_chan->desc_pool, new, new->async_tx.phys);
  453. }
  454. return NULL;
  455. }
  456. /**
  457. * fsl_dma_update_completed_cookie - Update the completed cookie.
  458. * @fsl_chan : Freescale DMA channel
  459. */
  460. static void fsl_dma_update_completed_cookie(struct fsl_dma_chan *fsl_chan)
  461. {
  462. struct fsl_desc_sw *cur_desc, *desc;
  463. dma_addr_t ld_phy;
  464. ld_phy = get_cdar(fsl_chan) & FSL_DMA_NLDA_MASK;
  465. if (ld_phy) {
  466. cur_desc = NULL;
  467. list_for_each_entry(desc, &fsl_chan->ld_queue, node)
  468. if (desc->async_tx.phys == ld_phy) {
  469. cur_desc = desc;
  470. break;
  471. }
  472. if (cur_desc && cur_desc->async_tx.cookie) {
  473. if (dma_is_idle(fsl_chan))
  474. fsl_chan->completed_cookie =
  475. cur_desc->async_tx.cookie;
  476. else
  477. fsl_chan->completed_cookie =
  478. cur_desc->async_tx.cookie - 1;
  479. }
  480. }
  481. }
  482. /**
  483. * fsl_chan_ld_cleanup - Clean up link descriptors
  484. * @fsl_chan : Freescale DMA channel
  485. *
  486. * This function clean up the ld_queue of DMA channel.
  487. * If 'in_intr' is set, the function will move the link descriptor to
  488. * the recycle list. Otherwise, free it directly.
  489. */
  490. static void fsl_chan_ld_cleanup(struct fsl_dma_chan *fsl_chan)
  491. {
  492. struct fsl_desc_sw *desc, *_desc;
  493. unsigned long flags;
  494. spin_lock_irqsave(&fsl_chan->desc_lock, flags);
  495. dev_dbg(fsl_chan->dev, "chan completed_cookie = %d\n",
  496. fsl_chan->completed_cookie);
  497. list_for_each_entry_safe(desc, _desc, &fsl_chan->ld_queue, node) {
  498. dma_async_tx_callback callback;
  499. void *callback_param;
  500. if (dma_async_is_complete(desc->async_tx.cookie,
  501. fsl_chan->completed_cookie, fsl_chan->common.cookie)
  502. == DMA_IN_PROGRESS)
  503. break;
  504. callback = desc->async_tx.callback;
  505. callback_param = desc->async_tx.callback_param;
  506. /* Remove from ld_queue list */
  507. list_del(&desc->node);
  508. dev_dbg(fsl_chan->dev, "link descriptor %p will be recycle.\n",
  509. desc);
  510. dma_pool_free(fsl_chan->desc_pool, desc, desc->async_tx.phys);
  511. /* Run the link descriptor callback function */
  512. if (callback) {
  513. spin_unlock_irqrestore(&fsl_chan->desc_lock, flags);
  514. dev_dbg(fsl_chan->dev, "link descriptor %p callback\n",
  515. desc);
  516. callback(callback_param);
  517. spin_lock_irqsave(&fsl_chan->desc_lock, flags);
  518. }
  519. }
  520. spin_unlock_irqrestore(&fsl_chan->desc_lock, flags);
  521. }
  522. /**
  523. * fsl_chan_xfer_ld_queue - Transfer link descriptors in channel ld_queue.
  524. * @fsl_chan : Freescale DMA channel
  525. */
  526. static void fsl_chan_xfer_ld_queue(struct fsl_dma_chan *fsl_chan)
  527. {
  528. struct list_head *ld_node;
  529. dma_addr_t next_dest_addr;
  530. unsigned long flags;
  531. spin_lock_irqsave(&fsl_chan->desc_lock, flags);
  532. if (!dma_is_idle(fsl_chan))
  533. goto out_unlock;
  534. dma_halt(fsl_chan);
  535. /* If there are some link descriptors
  536. * not transfered in queue. We need to start it.
  537. */
  538. /* Find the first un-transfer desciptor */
  539. for (ld_node = fsl_chan->ld_queue.next;
  540. (ld_node != &fsl_chan->ld_queue)
  541. && (dma_async_is_complete(
  542. to_fsl_desc(ld_node)->async_tx.cookie,
  543. fsl_chan->completed_cookie,
  544. fsl_chan->common.cookie) == DMA_SUCCESS);
  545. ld_node = ld_node->next);
  546. if (ld_node != &fsl_chan->ld_queue) {
  547. /* Get the ld start address from ld_queue */
  548. next_dest_addr = to_fsl_desc(ld_node)->async_tx.phys;
  549. dev_dbg(fsl_chan->dev, "xfer LDs staring from 0x%llx\n",
  550. (unsigned long long)next_dest_addr);
  551. set_cdar(fsl_chan, next_dest_addr);
  552. dma_start(fsl_chan);
  553. } else {
  554. set_cdar(fsl_chan, 0);
  555. set_ndar(fsl_chan, 0);
  556. }
  557. out_unlock:
  558. spin_unlock_irqrestore(&fsl_chan->desc_lock, flags);
  559. }
  560. /**
  561. * fsl_dma_memcpy_issue_pending - Issue the DMA start command
  562. * @fsl_chan : Freescale DMA channel
  563. */
  564. static void fsl_dma_memcpy_issue_pending(struct dma_chan *chan)
  565. {
  566. struct fsl_dma_chan *fsl_chan = to_fsl_chan(chan);
  567. #ifdef FSL_DMA_LD_DEBUG
  568. struct fsl_desc_sw *ld;
  569. unsigned long flags;
  570. spin_lock_irqsave(&fsl_chan->desc_lock, flags);
  571. if (list_empty(&fsl_chan->ld_queue)) {
  572. spin_unlock_irqrestore(&fsl_chan->desc_lock, flags);
  573. return;
  574. }
  575. dev_dbg(fsl_chan->dev, "--memcpy issue--\n");
  576. list_for_each_entry(ld, &fsl_chan->ld_queue, node) {
  577. int i;
  578. dev_dbg(fsl_chan->dev, "Ch %d, LD %08x\n",
  579. fsl_chan->id, ld->async_tx.phys);
  580. for (i = 0; i < 8; i++)
  581. dev_dbg(fsl_chan->dev, "LD offset %d: %08x\n",
  582. i, *(((u32 *)&ld->hw) + i));
  583. }
  584. dev_dbg(fsl_chan->dev, "----------------\n");
  585. spin_unlock_irqrestore(&fsl_chan->desc_lock, flags);
  586. #endif
  587. fsl_chan_xfer_ld_queue(fsl_chan);
  588. }
  589. /**
  590. * fsl_dma_is_complete - Determine the DMA status
  591. * @fsl_chan : Freescale DMA channel
  592. */
  593. static enum dma_status fsl_dma_is_complete(struct dma_chan *chan,
  594. dma_cookie_t cookie,
  595. dma_cookie_t *done,
  596. dma_cookie_t *used)
  597. {
  598. struct fsl_dma_chan *fsl_chan = to_fsl_chan(chan);
  599. dma_cookie_t last_used;
  600. dma_cookie_t last_complete;
  601. fsl_chan_ld_cleanup(fsl_chan);
  602. last_used = chan->cookie;
  603. last_complete = fsl_chan->completed_cookie;
  604. if (done)
  605. *done = last_complete;
  606. if (used)
  607. *used = last_used;
  608. return dma_async_is_complete(cookie, last_complete, last_used);
  609. }
  610. static irqreturn_t fsl_dma_chan_do_interrupt(int irq, void *data)
  611. {
  612. struct fsl_dma_chan *fsl_chan = (struct fsl_dma_chan *)data;
  613. u32 stat;
  614. int update_cookie = 0;
  615. int xfer_ld_q = 0;
  616. stat = get_sr(fsl_chan);
  617. dev_dbg(fsl_chan->dev, "event: channel %d, stat = 0x%x\n",
  618. fsl_chan->id, stat);
  619. set_sr(fsl_chan, stat); /* Clear the event register */
  620. stat &= ~(FSL_DMA_SR_CB | FSL_DMA_SR_CH);
  621. if (!stat)
  622. return IRQ_NONE;
  623. if (stat & FSL_DMA_SR_TE)
  624. dev_err(fsl_chan->dev, "Transfer Error!\n");
  625. /* Programming Error
  626. * The DMA_INTERRUPT async_tx is a NULL transfer, which will
  627. * triger a PE interrupt.
  628. */
  629. if (stat & FSL_DMA_SR_PE) {
  630. dev_dbg(fsl_chan->dev, "event: Programming Error INT\n");
  631. if (get_bcr(fsl_chan) == 0) {
  632. /* BCR register is 0, this is a DMA_INTERRUPT async_tx.
  633. * Now, update the completed cookie, and continue the
  634. * next uncompleted transfer.
  635. */
  636. update_cookie = 1;
  637. xfer_ld_q = 1;
  638. }
  639. stat &= ~FSL_DMA_SR_PE;
  640. }
  641. /* If the link descriptor segment transfer finishes,
  642. * we will recycle the used descriptor.
  643. */
  644. if (stat & FSL_DMA_SR_EOSI) {
  645. dev_dbg(fsl_chan->dev, "event: End-of-segments INT\n");
  646. dev_dbg(fsl_chan->dev, "event: clndar 0x%llx, nlndar 0x%llx\n",
  647. (unsigned long long)get_cdar(fsl_chan),
  648. (unsigned long long)get_ndar(fsl_chan));
  649. stat &= ~FSL_DMA_SR_EOSI;
  650. update_cookie = 1;
  651. }
  652. /* For MPC8349, EOCDI event need to update cookie
  653. * and start the next transfer if it exist.
  654. */
  655. if (stat & FSL_DMA_SR_EOCDI) {
  656. dev_dbg(fsl_chan->dev, "event: End-of-Chain link INT\n");
  657. stat &= ~FSL_DMA_SR_EOCDI;
  658. update_cookie = 1;
  659. xfer_ld_q = 1;
  660. }
  661. /* If it current transfer is the end-of-transfer,
  662. * we should clear the Channel Start bit for
  663. * prepare next transfer.
  664. */
  665. if (stat & FSL_DMA_SR_EOLNI) {
  666. dev_dbg(fsl_chan->dev, "event: End-of-link INT\n");
  667. stat &= ~FSL_DMA_SR_EOLNI;
  668. xfer_ld_q = 1;
  669. }
  670. if (update_cookie)
  671. fsl_dma_update_completed_cookie(fsl_chan);
  672. if (xfer_ld_q)
  673. fsl_chan_xfer_ld_queue(fsl_chan);
  674. if (stat)
  675. dev_dbg(fsl_chan->dev, "event: unhandled sr 0x%02x\n",
  676. stat);
  677. dev_dbg(fsl_chan->dev, "event: Exit\n");
  678. tasklet_schedule(&fsl_chan->tasklet);
  679. return IRQ_HANDLED;
  680. }
  681. static irqreturn_t fsl_dma_do_interrupt(int irq, void *data)
  682. {
  683. struct fsl_dma_device *fdev = (struct fsl_dma_device *)data;
  684. u32 gsr;
  685. int ch_nr;
  686. gsr = (fdev->feature & FSL_DMA_BIG_ENDIAN) ? in_be32(fdev->reg_base)
  687. : in_le32(fdev->reg_base);
  688. ch_nr = (32 - ffs(gsr)) / 8;
  689. return fdev->chan[ch_nr] ? fsl_dma_chan_do_interrupt(irq,
  690. fdev->chan[ch_nr]) : IRQ_NONE;
  691. }
  692. static void dma_do_tasklet(unsigned long data)
  693. {
  694. struct fsl_dma_chan *fsl_chan = (struct fsl_dma_chan *)data;
  695. fsl_chan_ld_cleanup(fsl_chan);
  696. }
  697. static int __devinit fsl_dma_chan_probe(struct fsl_dma_device *fdev,
  698. struct device_node *node, u32 feature, const char *compatible)
  699. {
  700. struct fsl_dma_chan *new_fsl_chan;
  701. int err;
  702. /* alloc channel */
  703. new_fsl_chan = kzalloc(sizeof(struct fsl_dma_chan), GFP_KERNEL);
  704. if (!new_fsl_chan) {
  705. dev_err(fdev->dev, "No free memory for allocating "
  706. "dma channels!\n");
  707. return -ENOMEM;
  708. }
  709. /* get dma channel register base */
  710. err = of_address_to_resource(node, 0, &new_fsl_chan->reg);
  711. if (err) {
  712. dev_err(fdev->dev, "Can't get %s property 'reg'\n",
  713. node->full_name);
  714. goto err_no_reg;
  715. }
  716. new_fsl_chan->feature = feature;
  717. if (!fdev->feature)
  718. fdev->feature = new_fsl_chan->feature;
  719. /* If the DMA device's feature is different than its channels',
  720. * report the bug.
  721. */
  722. WARN_ON(fdev->feature != new_fsl_chan->feature);
  723. new_fsl_chan->dev = fdev->dev;
  724. new_fsl_chan->reg_base = ioremap(new_fsl_chan->reg.start,
  725. new_fsl_chan->reg.end - new_fsl_chan->reg.start + 1);
  726. new_fsl_chan->id = ((new_fsl_chan->reg.start - 0x100) & 0xfff) >> 7;
  727. if (new_fsl_chan->id >= FSL_DMA_MAX_CHANS_PER_DEVICE) {
  728. dev_err(fdev->dev, "There is no %d channel!\n",
  729. new_fsl_chan->id);
  730. err = -EINVAL;
  731. goto err_no_chan;
  732. }
  733. fdev->chan[new_fsl_chan->id] = new_fsl_chan;
  734. tasklet_init(&new_fsl_chan->tasklet, dma_do_tasklet,
  735. (unsigned long)new_fsl_chan);
  736. /* Init the channel */
  737. dma_init(new_fsl_chan);
  738. /* Clear cdar registers */
  739. set_cdar(new_fsl_chan, 0);
  740. switch (new_fsl_chan->feature & FSL_DMA_IP_MASK) {
  741. case FSL_DMA_IP_85XX:
  742. new_fsl_chan->toggle_ext_pause = fsl_chan_toggle_ext_pause;
  743. case FSL_DMA_IP_83XX:
  744. new_fsl_chan->toggle_ext_start = fsl_chan_toggle_ext_start;
  745. new_fsl_chan->set_src_loop_size = fsl_chan_set_src_loop_size;
  746. new_fsl_chan->set_dest_loop_size = fsl_chan_set_dest_loop_size;
  747. }
  748. spin_lock_init(&new_fsl_chan->desc_lock);
  749. INIT_LIST_HEAD(&new_fsl_chan->ld_queue);
  750. new_fsl_chan->common.device = &fdev->common;
  751. /* Add the channel to DMA device channel list */
  752. list_add_tail(&new_fsl_chan->common.device_node,
  753. &fdev->common.channels);
  754. fdev->common.chancnt++;
  755. new_fsl_chan->irq = irq_of_parse_and_map(node, 0);
  756. if (new_fsl_chan->irq != NO_IRQ) {
  757. err = request_irq(new_fsl_chan->irq,
  758. &fsl_dma_chan_do_interrupt, IRQF_SHARED,
  759. "fsldma-channel", new_fsl_chan);
  760. if (err) {
  761. dev_err(fdev->dev, "DMA channel %s request_irq error "
  762. "with return %d\n", node->full_name, err);
  763. goto err_no_irq;
  764. }
  765. }
  766. dev_info(fdev->dev, "#%d (%s), irq %d\n", new_fsl_chan->id,
  767. compatible,
  768. new_fsl_chan->irq != NO_IRQ ? new_fsl_chan->irq : fdev->irq);
  769. return 0;
  770. err_no_irq:
  771. list_del(&new_fsl_chan->common.device_node);
  772. err_no_chan:
  773. iounmap(new_fsl_chan->reg_base);
  774. err_no_reg:
  775. kfree(new_fsl_chan);
  776. return err;
  777. }
  778. static void fsl_dma_chan_remove(struct fsl_dma_chan *fchan)
  779. {
  780. if (fchan->irq != NO_IRQ)
  781. free_irq(fchan->irq, fchan);
  782. list_del(&fchan->common.device_node);
  783. iounmap(fchan->reg_base);
  784. kfree(fchan);
  785. }
  786. static int __devinit of_fsl_dma_probe(struct of_device *dev,
  787. const struct of_device_id *match)
  788. {
  789. int err;
  790. struct fsl_dma_device *fdev;
  791. struct device_node *child;
  792. fdev = kzalloc(sizeof(struct fsl_dma_device), GFP_KERNEL);
  793. if (!fdev) {
  794. dev_err(&dev->dev, "No enough memory for 'priv'\n");
  795. return -ENOMEM;
  796. }
  797. fdev->dev = &dev->dev;
  798. INIT_LIST_HEAD(&fdev->common.channels);
  799. /* get DMA controller register base */
  800. err = of_address_to_resource(dev->node, 0, &fdev->reg);
  801. if (err) {
  802. dev_err(&dev->dev, "Can't get %s property 'reg'\n",
  803. dev->node->full_name);
  804. goto err_no_reg;
  805. }
  806. dev_info(&dev->dev, "Probe the Freescale DMA driver for %s "
  807. "controller at 0x%llx...\n",
  808. match->compatible, (unsigned long long)fdev->reg.start);
  809. fdev->reg_base = ioremap(fdev->reg.start, fdev->reg.end
  810. - fdev->reg.start + 1);
  811. dma_cap_set(DMA_MEMCPY, fdev->common.cap_mask);
  812. dma_cap_set(DMA_INTERRUPT, fdev->common.cap_mask);
  813. fdev->common.device_alloc_chan_resources = fsl_dma_alloc_chan_resources;
  814. fdev->common.device_free_chan_resources = fsl_dma_free_chan_resources;
  815. fdev->common.device_prep_dma_interrupt = fsl_dma_prep_interrupt;
  816. fdev->common.device_prep_dma_memcpy = fsl_dma_prep_memcpy;
  817. fdev->common.device_is_tx_complete = fsl_dma_is_complete;
  818. fdev->common.device_issue_pending = fsl_dma_memcpy_issue_pending;
  819. fdev->common.dev = &dev->dev;
  820. fdev->irq = irq_of_parse_and_map(dev->node, 0);
  821. if (fdev->irq != NO_IRQ) {
  822. err = request_irq(fdev->irq, &fsl_dma_do_interrupt, IRQF_SHARED,
  823. "fsldma-device", fdev);
  824. if (err) {
  825. dev_err(&dev->dev, "DMA device request_irq error "
  826. "with return %d\n", err);
  827. goto err;
  828. }
  829. }
  830. dev_set_drvdata(&(dev->dev), fdev);
  831. /* We cannot use of_platform_bus_probe() because there is no
  832. * of_platform_bus_remove. Instead, we manually instantiate every DMA
  833. * channel object.
  834. */
  835. for_each_child_of_node(dev->node, child) {
  836. if (of_device_is_compatible(child, "fsl,eloplus-dma-channel"))
  837. fsl_dma_chan_probe(fdev, child,
  838. FSL_DMA_IP_85XX | FSL_DMA_BIG_ENDIAN,
  839. "fsl,eloplus-dma-channel");
  840. if (of_device_is_compatible(child, "fsl,elo-dma-channel"))
  841. fsl_dma_chan_probe(fdev, child,
  842. FSL_DMA_IP_83XX | FSL_DMA_LITTLE_ENDIAN,
  843. "fsl,elo-dma-channel");
  844. }
  845. dma_async_device_register(&fdev->common);
  846. return 0;
  847. err:
  848. iounmap(fdev->reg_base);
  849. err_no_reg:
  850. kfree(fdev);
  851. return err;
  852. }
  853. static int of_fsl_dma_remove(struct of_device *of_dev)
  854. {
  855. struct fsl_dma_device *fdev;
  856. unsigned int i;
  857. fdev = dev_get_drvdata(&of_dev->dev);
  858. dma_async_device_unregister(&fdev->common);
  859. for (i = 0; i < FSL_DMA_MAX_CHANS_PER_DEVICE; i++)
  860. if (fdev->chan[i])
  861. fsl_dma_chan_remove(fdev->chan[i]);
  862. if (fdev->irq != NO_IRQ)
  863. free_irq(fdev->irq, fdev);
  864. iounmap(fdev->reg_base);
  865. kfree(fdev);
  866. dev_set_drvdata(&of_dev->dev, NULL);
  867. return 0;
  868. }
  869. static struct of_device_id of_fsl_dma_ids[] = {
  870. { .compatible = "fsl,eloplus-dma", },
  871. { .compatible = "fsl,elo-dma", },
  872. {}
  873. };
  874. static struct of_platform_driver of_fsl_dma_driver = {
  875. .name = "fsl-elo-dma",
  876. .match_table = of_fsl_dma_ids,
  877. .probe = of_fsl_dma_probe,
  878. .remove = of_fsl_dma_remove,
  879. };
  880. static __init int of_fsl_dma_init(void)
  881. {
  882. int ret;
  883. pr_info("Freescale Elo / Elo Plus DMA driver\n");
  884. ret = of_register_platform_driver(&of_fsl_dma_driver);
  885. if (ret)
  886. pr_err("fsldma: failed to register platform driver\n");
  887. return ret;
  888. }
  889. static void __exit of_fsl_dma_exit(void)
  890. {
  891. of_unregister_platform_driver(&of_fsl_dma_driver);
  892. }
  893. subsys_initcall(of_fsl_dma_init);
  894. module_exit(of_fsl_dma_exit);
  895. MODULE_DESCRIPTION("Freescale Elo / Elo Plus DMA driver");
  896. MODULE_LICENSE("GPL");