fsldma.c 35 KB

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