fsldma.c 27 KB

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