fsldma.c 27 KB

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