fsldma.c 37 KB

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