fsldma.c 36 KB

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