fsldma.c 38 KB

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