fsldma.c 34 KB

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