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