fsldma.c 37 KB

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