fsldma.c 38 KB

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