fsldma.c 35 KB

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