dw_dmac.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  1. /*
  2. * Driver for the Synopsys DesignWare DMA Controller (aka DMACA on
  3. * AVR32 systems.)
  4. *
  5. * Copyright (C) 2007-2008 Atmel Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/dmaengine.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/io.h>
  18. #include <linux/mm.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/slab.h>
  22. #include "dw_dmac_regs.h"
  23. /*
  24. * This supports the Synopsys "DesignWare AHB Central DMA Controller",
  25. * (DW_ahb_dmac) which is used with various AMBA 2.0 systems (not all
  26. * of which use ARM any more). See the "Databook" from Synopsys for
  27. * information beyond what licensees probably provide.
  28. *
  29. * The driver has currently been tested only with the Atmel AT32AP7000,
  30. * which does not support descriptor writeback.
  31. */
  32. #define DWC_DEFAULT_CTLLO(private) ({ \
  33. struct dw_dma_slave *__slave = (private); \
  34. int dms = __slave ? __slave->dst_master : 0; \
  35. int sms = __slave ? __slave->src_master : 1; \
  36. u8 smsize = __slave ? __slave->src_msize : DW_DMA_MSIZE_16; \
  37. u8 dmsize = __slave ? __slave->dst_msize : DW_DMA_MSIZE_16; \
  38. \
  39. (DWC_CTLL_DST_MSIZE(dmsize) \
  40. | DWC_CTLL_SRC_MSIZE(smsize) \
  41. | DWC_CTLL_LLP_D_EN \
  42. | DWC_CTLL_LLP_S_EN \
  43. | DWC_CTLL_DMS(dms) \
  44. | DWC_CTLL_SMS(sms)); \
  45. })
  46. /*
  47. * This is configuration-dependent and usually a funny size like 4095.
  48. *
  49. * Note that this is a transfer count, i.e. if we transfer 32-bit
  50. * words, we can do 16380 bytes per descriptor.
  51. *
  52. * This parameter is also system-specific.
  53. */
  54. #define DWC_MAX_COUNT 4095U
  55. /*
  56. * Number of descriptors to allocate for each channel. This should be
  57. * made configurable somehow; preferably, the clients (at least the
  58. * ones using slave transfers) should be able to give us a hint.
  59. */
  60. #define NR_DESCS_PER_CHANNEL 64
  61. /*----------------------------------------------------------------------*/
  62. /*
  63. * Because we're not relying on writeback from the controller (it may not
  64. * even be configured into the core!) we don't need to use dma_pool. These
  65. * descriptors -- and associated data -- are cacheable. We do need to make
  66. * sure their dcache entries are written back before handing them off to
  67. * the controller, though.
  68. */
  69. static struct device *chan2dev(struct dma_chan *chan)
  70. {
  71. return &chan->dev->device;
  72. }
  73. static struct device *chan2parent(struct dma_chan *chan)
  74. {
  75. return chan->dev->device.parent;
  76. }
  77. static struct dw_desc *dwc_first_active(struct dw_dma_chan *dwc)
  78. {
  79. return list_entry(dwc->active_list.next, struct dw_desc, desc_node);
  80. }
  81. static struct dw_desc *dwc_desc_get(struct dw_dma_chan *dwc)
  82. {
  83. struct dw_desc *desc, *_desc;
  84. struct dw_desc *ret = NULL;
  85. unsigned int i = 0;
  86. spin_lock_bh(&dwc->lock);
  87. list_for_each_entry_safe(desc, _desc, &dwc->free_list, desc_node) {
  88. if (async_tx_test_ack(&desc->txd)) {
  89. list_del(&desc->desc_node);
  90. ret = desc;
  91. break;
  92. }
  93. dev_dbg(chan2dev(&dwc->chan), "desc %p not ACKed\n", desc);
  94. i++;
  95. }
  96. spin_unlock_bh(&dwc->lock);
  97. dev_vdbg(chan2dev(&dwc->chan), "scanned %u descriptors on freelist\n", i);
  98. return ret;
  99. }
  100. static void dwc_sync_desc_for_cpu(struct dw_dma_chan *dwc, struct dw_desc *desc)
  101. {
  102. struct dw_desc *child;
  103. list_for_each_entry(child, &desc->tx_list, desc_node)
  104. dma_sync_single_for_cpu(chan2parent(&dwc->chan),
  105. child->txd.phys, sizeof(child->lli),
  106. DMA_TO_DEVICE);
  107. dma_sync_single_for_cpu(chan2parent(&dwc->chan),
  108. desc->txd.phys, sizeof(desc->lli),
  109. DMA_TO_DEVICE);
  110. }
  111. /*
  112. * Move a descriptor, including any children, to the free list.
  113. * `desc' must not be on any lists.
  114. */
  115. static void dwc_desc_put(struct dw_dma_chan *dwc, struct dw_desc *desc)
  116. {
  117. if (desc) {
  118. struct dw_desc *child;
  119. dwc_sync_desc_for_cpu(dwc, desc);
  120. spin_lock_bh(&dwc->lock);
  121. list_for_each_entry(child, &desc->tx_list, desc_node)
  122. dev_vdbg(chan2dev(&dwc->chan),
  123. "moving child desc %p to freelist\n",
  124. child);
  125. list_splice_init(&desc->tx_list, &dwc->free_list);
  126. dev_vdbg(chan2dev(&dwc->chan), "moving desc %p to freelist\n", desc);
  127. list_add(&desc->desc_node, &dwc->free_list);
  128. spin_unlock_bh(&dwc->lock);
  129. }
  130. }
  131. /* Called with dwc->lock held and bh disabled */
  132. static dma_cookie_t
  133. dwc_assign_cookie(struct dw_dma_chan *dwc, struct dw_desc *desc)
  134. {
  135. dma_cookie_t cookie = dwc->chan.cookie;
  136. if (++cookie < 0)
  137. cookie = 1;
  138. dwc->chan.cookie = cookie;
  139. desc->txd.cookie = cookie;
  140. return cookie;
  141. }
  142. /*----------------------------------------------------------------------*/
  143. /* Called with dwc->lock held and bh disabled */
  144. static void dwc_dostart(struct dw_dma_chan *dwc, struct dw_desc *first)
  145. {
  146. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  147. /* ASSERT: channel is idle */
  148. if (dma_readl(dw, CH_EN) & dwc->mask) {
  149. dev_err(chan2dev(&dwc->chan),
  150. "BUG: Attempted to start non-idle channel\n");
  151. dev_err(chan2dev(&dwc->chan),
  152. " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
  153. channel_readl(dwc, SAR),
  154. channel_readl(dwc, DAR),
  155. channel_readl(dwc, LLP),
  156. channel_readl(dwc, CTL_HI),
  157. channel_readl(dwc, CTL_LO));
  158. /* The tasklet will hopefully advance the queue... */
  159. return;
  160. }
  161. channel_writel(dwc, LLP, first->txd.phys);
  162. channel_writel(dwc, CTL_LO,
  163. DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
  164. channel_writel(dwc, CTL_HI, 0);
  165. channel_set_bit(dw, CH_EN, dwc->mask);
  166. }
  167. /*----------------------------------------------------------------------*/
  168. static void
  169. dwc_descriptor_complete(struct dw_dma_chan *dwc, struct dw_desc *desc,
  170. bool callback_required)
  171. {
  172. dma_async_tx_callback callback = NULL;
  173. void *param = NULL;
  174. struct dma_async_tx_descriptor *txd = &desc->txd;
  175. struct dw_desc *child;
  176. dev_vdbg(chan2dev(&dwc->chan), "descriptor %u complete\n", txd->cookie);
  177. dwc->completed = txd->cookie;
  178. if (callback_required) {
  179. callback = txd->callback;
  180. param = txd->callback_param;
  181. }
  182. dwc_sync_desc_for_cpu(dwc, desc);
  183. /* async_tx_ack */
  184. list_for_each_entry(child, &desc->tx_list, desc_node)
  185. async_tx_ack(&child->txd);
  186. async_tx_ack(&desc->txd);
  187. list_splice_init(&desc->tx_list, &dwc->free_list);
  188. list_move(&desc->desc_node, &dwc->free_list);
  189. if (!dwc->chan.private) {
  190. struct device *parent = chan2parent(&dwc->chan);
  191. if (!(txd->flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
  192. if (txd->flags & DMA_COMPL_DEST_UNMAP_SINGLE)
  193. dma_unmap_single(parent, desc->lli.dar,
  194. desc->len, DMA_FROM_DEVICE);
  195. else
  196. dma_unmap_page(parent, desc->lli.dar,
  197. desc->len, DMA_FROM_DEVICE);
  198. }
  199. if (!(txd->flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
  200. if (txd->flags & DMA_COMPL_SRC_UNMAP_SINGLE)
  201. dma_unmap_single(parent, desc->lli.sar,
  202. desc->len, DMA_TO_DEVICE);
  203. else
  204. dma_unmap_page(parent, desc->lli.sar,
  205. desc->len, DMA_TO_DEVICE);
  206. }
  207. }
  208. if (callback_required && callback)
  209. callback(param);
  210. }
  211. static void dwc_complete_all(struct dw_dma *dw, struct dw_dma_chan *dwc)
  212. {
  213. struct dw_desc *desc, *_desc;
  214. LIST_HEAD(list);
  215. if (dma_readl(dw, CH_EN) & dwc->mask) {
  216. dev_err(chan2dev(&dwc->chan),
  217. "BUG: XFER bit set, but channel not idle!\n");
  218. /* Try to continue after resetting the channel... */
  219. channel_clear_bit(dw, CH_EN, dwc->mask);
  220. while (dma_readl(dw, CH_EN) & dwc->mask)
  221. cpu_relax();
  222. }
  223. /*
  224. * Submit queued descriptors ASAP, i.e. before we go through
  225. * the completed ones.
  226. */
  227. list_splice_init(&dwc->active_list, &list);
  228. if (!list_empty(&dwc->queue)) {
  229. list_move(dwc->queue.next, &dwc->active_list);
  230. dwc_dostart(dwc, dwc_first_active(dwc));
  231. }
  232. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  233. dwc_descriptor_complete(dwc, desc, true);
  234. }
  235. static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc)
  236. {
  237. dma_addr_t llp;
  238. struct dw_desc *desc, *_desc;
  239. struct dw_desc *child;
  240. u32 status_xfer;
  241. /*
  242. * Clear block interrupt flag before scanning so that we don't
  243. * miss any, and read LLP before RAW_XFER to ensure it is
  244. * valid if we decide to scan the list.
  245. */
  246. dma_writel(dw, CLEAR.BLOCK, dwc->mask);
  247. llp = channel_readl(dwc, LLP);
  248. status_xfer = dma_readl(dw, RAW.XFER);
  249. if (status_xfer & dwc->mask) {
  250. /* Everything we've submitted is done */
  251. dma_writel(dw, CLEAR.XFER, dwc->mask);
  252. dwc_complete_all(dw, dwc);
  253. return;
  254. }
  255. if (list_empty(&dwc->active_list))
  256. return;
  257. dev_vdbg(chan2dev(&dwc->chan), "scan_descriptors: llp=0x%x\n", llp);
  258. list_for_each_entry_safe(desc, _desc, &dwc->active_list, desc_node) {
  259. /* check first descriptors addr */
  260. if (desc->txd.phys == llp)
  261. return;
  262. /* check first descriptors llp */
  263. if (desc->lli.llp == llp)
  264. /* This one is currently in progress */
  265. return;
  266. list_for_each_entry(child, &desc->tx_list, desc_node)
  267. if (child->lli.llp == llp)
  268. /* Currently in progress */
  269. return;
  270. /*
  271. * No descriptors so far seem to be in progress, i.e.
  272. * this one must be done.
  273. */
  274. dwc_descriptor_complete(dwc, desc, true);
  275. }
  276. dev_err(chan2dev(&dwc->chan),
  277. "BUG: All descriptors done, but channel not idle!\n");
  278. /* Try to continue after resetting the channel... */
  279. channel_clear_bit(dw, CH_EN, dwc->mask);
  280. while (dma_readl(dw, CH_EN) & dwc->mask)
  281. cpu_relax();
  282. if (!list_empty(&dwc->queue)) {
  283. list_move(dwc->queue.next, &dwc->active_list);
  284. dwc_dostart(dwc, dwc_first_active(dwc));
  285. }
  286. }
  287. static void dwc_dump_lli(struct dw_dma_chan *dwc, struct dw_lli *lli)
  288. {
  289. dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
  290. " desc: s0x%x d0x%x l0x%x c0x%x:%x\n",
  291. lli->sar, lli->dar, lli->llp,
  292. lli->ctlhi, lli->ctllo);
  293. }
  294. static void dwc_handle_error(struct dw_dma *dw, struct dw_dma_chan *dwc)
  295. {
  296. struct dw_desc *bad_desc;
  297. struct dw_desc *child;
  298. dwc_scan_descriptors(dw, dwc);
  299. /*
  300. * The descriptor currently at the head of the active list is
  301. * borked. Since we don't have any way to report errors, we'll
  302. * just have to scream loudly and try to carry on.
  303. */
  304. bad_desc = dwc_first_active(dwc);
  305. list_del_init(&bad_desc->desc_node);
  306. list_move(dwc->queue.next, dwc->active_list.prev);
  307. /* Clear the error flag and try to restart the controller */
  308. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  309. if (!list_empty(&dwc->active_list))
  310. dwc_dostart(dwc, dwc_first_active(dwc));
  311. /*
  312. * KERN_CRITICAL may seem harsh, but since this only happens
  313. * when someone submits a bad physical address in a
  314. * descriptor, we should consider ourselves lucky that the
  315. * controller flagged an error instead of scribbling over
  316. * random memory locations.
  317. */
  318. dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
  319. "Bad descriptor submitted for DMA!\n");
  320. dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
  321. " cookie: %d\n", bad_desc->txd.cookie);
  322. dwc_dump_lli(dwc, &bad_desc->lli);
  323. list_for_each_entry(child, &bad_desc->tx_list, desc_node)
  324. dwc_dump_lli(dwc, &child->lli);
  325. /* Pretend the descriptor completed successfully */
  326. dwc_descriptor_complete(dwc, bad_desc, true);
  327. }
  328. /* --------------------- Cyclic DMA API extensions -------------------- */
  329. inline dma_addr_t dw_dma_get_src_addr(struct dma_chan *chan)
  330. {
  331. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  332. return channel_readl(dwc, SAR);
  333. }
  334. EXPORT_SYMBOL(dw_dma_get_src_addr);
  335. inline dma_addr_t dw_dma_get_dst_addr(struct dma_chan *chan)
  336. {
  337. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  338. return channel_readl(dwc, DAR);
  339. }
  340. EXPORT_SYMBOL(dw_dma_get_dst_addr);
  341. /* called with dwc->lock held and all DMAC interrupts disabled */
  342. static void dwc_handle_cyclic(struct dw_dma *dw, struct dw_dma_chan *dwc,
  343. u32 status_block, u32 status_err, u32 status_xfer)
  344. {
  345. if (status_block & dwc->mask) {
  346. void (*callback)(void *param);
  347. void *callback_param;
  348. dev_vdbg(chan2dev(&dwc->chan), "new cyclic period llp 0x%08x\n",
  349. channel_readl(dwc, LLP));
  350. dma_writel(dw, CLEAR.BLOCK, dwc->mask);
  351. callback = dwc->cdesc->period_callback;
  352. callback_param = dwc->cdesc->period_callback_param;
  353. if (callback) {
  354. spin_unlock(&dwc->lock);
  355. callback(callback_param);
  356. spin_lock(&dwc->lock);
  357. }
  358. }
  359. /*
  360. * Error and transfer complete are highly unlikely, and will most
  361. * likely be due to a configuration error by the user.
  362. */
  363. if (unlikely(status_err & dwc->mask) ||
  364. unlikely(status_xfer & dwc->mask)) {
  365. int i;
  366. dev_err(chan2dev(&dwc->chan), "cyclic DMA unexpected %s "
  367. "interrupt, stopping DMA transfer\n",
  368. status_xfer ? "xfer" : "error");
  369. dev_err(chan2dev(&dwc->chan),
  370. " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
  371. channel_readl(dwc, SAR),
  372. channel_readl(dwc, DAR),
  373. channel_readl(dwc, LLP),
  374. channel_readl(dwc, CTL_HI),
  375. channel_readl(dwc, CTL_LO));
  376. channel_clear_bit(dw, CH_EN, dwc->mask);
  377. while (dma_readl(dw, CH_EN) & dwc->mask)
  378. cpu_relax();
  379. /* make sure DMA does not restart by loading a new list */
  380. channel_writel(dwc, LLP, 0);
  381. channel_writel(dwc, CTL_LO, 0);
  382. channel_writel(dwc, CTL_HI, 0);
  383. dma_writel(dw, CLEAR.BLOCK, dwc->mask);
  384. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  385. dma_writel(dw, CLEAR.XFER, dwc->mask);
  386. for (i = 0; i < dwc->cdesc->periods; i++)
  387. dwc_dump_lli(dwc, &dwc->cdesc->desc[i]->lli);
  388. }
  389. }
  390. /* ------------------------------------------------------------------------- */
  391. static void dw_dma_tasklet(unsigned long data)
  392. {
  393. struct dw_dma *dw = (struct dw_dma *)data;
  394. struct dw_dma_chan *dwc;
  395. u32 status_block;
  396. u32 status_xfer;
  397. u32 status_err;
  398. int i;
  399. status_block = dma_readl(dw, RAW.BLOCK);
  400. status_xfer = dma_readl(dw, RAW.XFER);
  401. status_err = dma_readl(dw, RAW.ERROR);
  402. dev_vdbg(dw->dma.dev, "tasklet: status_block=%x status_err=%x\n",
  403. status_block, status_err);
  404. for (i = 0; i < dw->dma.chancnt; i++) {
  405. dwc = &dw->chan[i];
  406. spin_lock(&dwc->lock);
  407. if (test_bit(DW_DMA_IS_CYCLIC, &dwc->flags))
  408. dwc_handle_cyclic(dw, dwc, status_block, status_err,
  409. status_xfer);
  410. else if (status_err & (1 << i))
  411. dwc_handle_error(dw, dwc);
  412. else if ((status_block | status_xfer) & (1 << i))
  413. dwc_scan_descriptors(dw, dwc);
  414. spin_unlock(&dwc->lock);
  415. }
  416. /*
  417. * Re-enable interrupts. Block Complete interrupts are only
  418. * enabled if the INT_EN bit in the descriptor is set. This
  419. * will trigger a scan before the whole list is done.
  420. */
  421. channel_set_bit(dw, MASK.XFER, dw->all_chan_mask);
  422. channel_set_bit(dw, MASK.BLOCK, dw->all_chan_mask);
  423. channel_set_bit(dw, MASK.ERROR, dw->all_chan_mask);
  424. }
  425. static irqreturn_t dw_dma_interrupt(int irq, void *dev_id)
  426. {
  427. struct dw_dma *dw = dev_id;
  428. u32 status;
  429. dev_vdbg(dw->dma.dev, "interrupt: status=0x%x\n",
  430. dma_readl(dw, STATUS_INT));
  431. /*
  432. * Just disable the interrupts. We'll turn them back on in the
  433. * softirq handler.
  434. */
  435. channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
  436. channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
  437. channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
  438. status = dma_readl(dw, STATUS_INT);
  439. if (status) {
  440. dev_err(dw->dma.dev,
  441. "BUG: Unexpected interrupts pending: 0x%x\n",
  442. status);
  443. /* Try to recover */
  444. channel_clear_bit(dw, MASK.XFER, (1 << 8) - 1);
  445. channel_clear_bit(dw, MASK.BLOCK, (1 << 8) - 1);
  446. channel_clear_bit(dw, MASK.SRC_TRAN, (1 << 8) - 1);
  447. channel_clear_bit(dw, MASK.DST_TRAN, (1 << 8) - 1);
  448. channel_clear_bit(dw, MASK.ERROR, (1 << 8) - 1);
  449. }
  450. tasklet_schedule(&dw->tasklet);
  451. return IRQ_HANDLED;
  452. }
  453. /*----------------------------------------------------------------------*/
  454. static dma_cookie_t dwc_tx_submit(struct dma_async_tx_descriptor *tx)
  455. {
  456. struct dw_desc *desc = txd_to_dw_desc(tx);
  457. struct dw_dma_chan *dwc = to_dw_dma_chan(tx->chan);
  458. dma_cookie_t cookie;
  459. spin_lock_bh(&dwc->lock);
  460. cookie = dwc_assign_cookie(dwc, desc);
  461. /*
  462. * REVISIT: We should attempt to chain as many descriptors as
  463. * possible, perhaps even appending to those already submitted
  464. * for DMA. But this is hard to do in a race-free manner.
  465. */
  466. if (list_empty(&dwc->active_list)) {
  467. dev_vdbg(chan2dev(tx->chan), "tx_submit: started %u\n",
  468. desc->txd.cookie);
  469. list_add_tail(&desc->desc_node, &dwc->active_list);
  470. dwc_dostart(dwc, dwc_first_active(dwc));
  471. } else {
  472. dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u\n",
  473. desc->txd.cookie);
  474. list_add_tail(&desc->desc_node, &dwc->queue);
  475. }
  476. spin_unlock_bh(&dwc->lock);
  477. return cookie;
  478. }
  479. static struct dma_async_tx_descriptor *
  480. dwc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  481. size_t len, unsigned long flags)
  482. {
  483. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  484. struct dw_desc *desc;
  485. struct dw_desc *first;
  486. struct dw_desc *prev;
  487. size_t xfer_count;
  488. size_t offset;
  489. unsigned int src_width;
  490. unsigned int dst_width;
  491. u32 ctllo;
  492. dev_vdbg(chan2dev(chan), "prep_dma_memcpy d0x%x s0x%x l0x%zx f0x%lx\n",
  493. dest, src, len, flags);
  494. if (unlikely(!len)) {
  495. dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
  496. return NULL;
  497. }
  498. /*
  499. * We can be a lot more clever here, but this should take care
  500. * of the most common optimization.
  501. */
  502. if (!((src | dest | len) & 7))
  503. src_width = dst_width = 3;
  504. else if (!((src | dest | len) & 3))
  505. src_width = dst_width = 2;
  506. else if (!((src | dest | len) & 1))
  507. src_width = dst_width = 1;
  508. else
  509. src_width = dst_width = 0;
  510. ctllo = DWC_DEFAULT_CTLLO(chan->private)
  511. | DWC_CTLL_DST_WIDTH(dst_width)
  512. | DWC_CTLL_SRC_WIDTH(src_width)
  513. | DWC_CTLL_DST_INC
  514. | DWC_CTLL_SRC_INC
  515. | DWC_CTLL_FC_M2M;
  516. prev = first = NULL;
  517. for (offset = 0; offset < len; offset += xfer_count << src_width) {
  518. xfer_count = min_t(size_t, (len - offset) >> src_width,
  519. DWC_MAX_COUNT);
  520. desc = dwc_desc_get(dwc);
  521. if (!desc)
  522. goto err_desc_get;
  523. desc->lli.sar = src + offset;
  524. desc->lli.dar = dest + offset;
  525. desc->lli.ctllo = ctllo;
  526. desc->lli.ctlhi = xfer_count;
  527. if (!first) {
  528. first = desc;
  529. } else {
  530. prev->lli.llp = desc->txd.phys;
  531. dma_sync_single_for_device(chan2parent(chan),
  532. prev->txd.phys, sizeof(prev->lli),
  533. DMA_TO_DEVICE);
  534. list_add_tail(&desc->desc_node,
  535. &first->tx_list);
  536. }
  537. prev = desc;
  538. }
  539. if (flags & DMA_PREP_INTERRUPT)
  540. /* Trigger interrupt after last block */
  541. prev->lli.ctllo |= DWC_CTLL_INT_EN;
  542. prev->lli.llp = 0;
  543. dma_sync_single_for_device(chan2parent(chan),
  544. prev->txd.phys, sizeof(prev->lli),
  545. DMA_TO_DEVICE);
  546. first->txd.flags = flags;
  547. first->len = len;
  548. return &first->txd;
  549. err_desc_get:
  550. dwc_desc_put(dwc, first);
  551. return NULL;
  552. }
  553. static struct dma_async_tx_descriptor *
  554. dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  555. unsigned int sg_len, enum dma_data_direction direction,
  556. unsigned long flags)
  557. {
  558. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  559. struct dw_dma_slave *dws = chan->private;
  560. struct dw_desc *prev;
  561. struct dw_desc *first;
  562. u32 ctllo;
  563. dma_addr_t reg;
  564. unsigned int reg_width;
  565. unsigned int mem_width;
  566. unsigned int i;
  567. struct scatterlist *sg;
  568. size_t total_len = 0;
  569. dev_vdbg(chan2dev(chan), "prep_dma_slave\n");
  570. if (unlikely(!dws || !sg_len))
  571. return NULL;
  572. reg_width = dws->reg_width;
  573. prev = first = NULL;
  574. switch (direction) {
  575. case DMA_TO_DEVICE:
  576. ctllo = (DWC_DEFAULT_CTLLO(chan->private)
  577. | DWC_CTLL_DST_WIDTH(reg_width)
  578. | DWC_CTLL_DST_FIX
  579. | DWC_CTLL_SRC_INC
  580. | DWC_CTLL_FC(dws->fc));
  581. reg = dws->tx_reg;
  582. for_each_sg(sgl, sg, sg_len, i) {
  583. struct dw_desc *desc;
  584. u32 len;
  585. u32 mem;
  586. desc = dwc_desc_get(dwc);
  587. if (!desc) {
  588. dev_err(chan2dev(chan),
  589. "not enough descriptors available\n");
  590. goto err_desc_get;
  591. }
  592. mem = sg_phys(sg);
  593. len = sg_dma_len(sg);
  594. mem_width = 2;
  595. if (unlikely(mem & 3 || len & 3))
  596. mem_width = 0;
  597. desc->lli.sar = mem;
  598. desc->lli.dar = reg;
  599. desc->lli.ctllo = ctllo | DWC_CTLL_SRC_WIDTH(mem_width);
  600. desc->lli.ctlhi = len >> mem_width;
  601. if (!first) {
  602. first = desc;
  603. } else {
  604. prev->lli.llp = desc->txd.phys;
  605. dma_sync_single_for_device(chan2parent(chan),
  606. prev->txd.phys,
  607. sizeof(prev->lli),
  608. DMA_TO_DEVICE);
  609. list_add_tail(&desc->desc_node,
  610. &first->tx_list);
  611. }
  612. prev = desc;
  613. total_len += len;
  614. }
  615. break;
  616. case DMA_FROM_DEVICE:
  617. ctllo = (DWC_DEFAULT_CTLLO(chan->private)
  618. | DWC_CTLL_SRC_WIDTH(reg_width)
  619. | DWC_CTLL_DST_INC
  620. | DWC_CTLL_SRC_FIX
  621. | DWC_CTLL_FC(dws->fc));
  622. reg = dws->rx_reg;
  623. for_each_sg(sgl, sg, sg_len, i) {
  624. struct dw_desc *desc;
  625. u32 len;
  626. u32 mem;
  627. desc = dwc_desc_get(dwc);
  628. if (!desc) {
  629. dev_err(chan2dev(chan),
  630. "not enough descriptors available\n");
  631. goto err_desc_get;
  632. }
  633. mem = sg_phys(sg);
  634. len = sg_dma_len(sg);
  635. mem_width = 2;
  636. if (unlikely(mem & 3 || len & 3))
  637. mem_width = 0;
  638. desc->lli.sar = reg;
  639. desc->lli.dar = mem;
  640. desc->lli.ctllo = ctllo | DWC_CTLL_DST_WIDTH(mem_width);
  641. desc->lli.ctlhi = len >> reg_width;
  642. if (!first) {
  643. first = desc;
  644. } else {
  645. prev->lli.llp = desc->txd.phys;
  646. dma_sync_single_for_device(chan2parent(chan),
  647. prev->txd.phys,
  648. sizeof(prev->lli),
  649. DMA_TO_DEVICE);
  650. list_add_tail(&desc->desc_node,
  651. &first->tx_list);
  652. }
  653. prev = desc;
  654. total_len += len;
  655. }
  656. break;
  657. default:
  658. return NULL;
  659. }
  660. if (flags & DMA_PREP_INTERRUPT)
  661. /* Trigger interrupt after last block */
  662. prev->lli.ctllo |= DWC_CTLL_INT_EN;
  663. prev->lli.llp = 0;
  664. dma_sync_single_for_device(chan2parent(chan),
  665. prev->txd.phys, sizeof(prev->lli),
  666. DMA_TO_DEVICE);
  667. first->len = total_len;
  668. return &first->txd;
  669. err_desc_get:
  670. dwc_desc_put(dwc, first);
  671. return NULL;
  672. }
  673. static int dwc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  674. unsigned long arg)
  675. {
  676. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  677. struct dw_dma *dw = to_dw_dma(chan->device);
  678. struct dw_desc *desc, *_desc;
  679. LIST_HEAD(list);
  680. /* Only supports DMA_TERMINATE_ALL */
  681. if (cmd != DMA_TERMINATE_ALL)
  682. return -ENXIO;
  683. /*
  684. * This is only called when something went wrong elsewhere, so
  685. * we don't really care about the data. Just disable the
  686. * channel. We still have to poll the channel enable bit due
  687. * to AHB/HSB limitations.
  688. */
  689. spin_lock_bh(&dwc->lock);
  690. channel_clear_bit(dw, CH_EN, dwc->mask);
  691. while (dma_readl(dw, CH_EN) & dwc->mask)
  692. cpu_relax();
  693. /* active_list entries will end up before queued entries */
  694. list_splice_init(&dwc->queue, &list);
  695. list_splice_init(&dwc->active_list, &list);
  696. spin_unlock_bh(&dwc->lock);
  697. /* Flush all pending and queued descriptors */
  698. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  699. dwc_descriptor_complete(dwc, desc, false);
  700. return 0;
  701. }
  702. static enum dma_status
  703. dwc_tx_status(struct dma_chan *chan,
  704. dma_cookie_t cookie,
  705. struct dma_tx_state *txstate)
  706. {
  707. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  708. dma_cookie_t last_used;
  709. dma_cookie_t last_complete;
  710. int ret;
  711. last_complete = dwc->completed;
  712. last_used = chan->cookie;
  713. ret = dma_async_is_complete(cookie, last_complete, last_used);
  714. if (ret != DMA_SUCCESS) {
  715. spin_lock_bh(&dwc->lock);
  716. dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
  717. spin_unlock_bh(&dwc->lock);
  718. last_complete = dwc->completed;
  719. last_used = chan->cookie;
  720. ret = dma_async_is_complete(cookie, last_complete, last_used);
  721. }
  722. if (ret != DMA_SUCCESS)
  723. dma_set_tx_state(txstate, last_complete, last_used,
  724. dwc_first_active(dwc)->len);
  725. else
  726. dma_set_tx_state(txstate, last_complete, last_used, 0);
  727. return ret;
  728. }
  729. static void dwc_issue_pending(struct dma_chan *chan)
  730. {
  731. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  732. spin_lock_bh(&dwc->lock);
  733. if (!list_empty(&dwc->queue))
  734. dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
  735. spin_unlock_bh(&dwc->lock);
  736. }
  737. static int dwc_alloc_chan_resources(struct dma_chan *chan)
  738. {
  739. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  740. struct dw_dma *dw = to_dw_dma(chan->device);
  741. struct dw_desc *desc;
  742. struct dw_dma_slave *dws;
  743. int i;
  744. u32 cfghi;
  745. u32 cfglo;
  746. dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
  747. /* ASSERT: channel is idle */
  748. if (dma_readl(dw, CH_EN) & dwc->mask) {
  749. dev_dbg(chan2dev(chan), "DMA channel not idle?\n");
  750. return -EIO;
  751. }
  752. dwc->completed = chan->cookie = 1;
  753. cfghi = DWC_CFGH_FIFO_MODE;
  754. cfglo = 0;
  755. dws = chan->private;
  756. if (dws) {
  757. /*
  758. * We need controller-specific data to set up slave
  759. * transfers.
  760. */
  761. BUG_ON(!dws->dma_dev || dws->dma_dev != dw->dma.dev);
  762. cfghi = dws->cfg_hi;
  763. cfglo = dws->cfg_lo & ~DWC_CFGL_CH_PRIOR_MASK;
  764. }
  765. cfglo |= DWC_CFGL_CH_PRIOR(dwc->priority);
  766. channel_writel(dwc, CFG_LO, cfglo);
  767. channel_writel(dwc, CFG_HI, cfghi);
  768. /*
  769. * NOTE: some controllers may have additional features that we
  770. * need to initialize here, like "scatter-gather" (which
  771. * doesn't mean what you think it means), and status writeback.
  772. */
  773. spin_lock_bh(&dwc->lock);
  774. i = dwc->descs_allocated;
  775. while (dwc->descs_allocated < NR_DESCS_PER_CHANNEL) {
  776. spin_unlock_bh(&dwc->lock);
  777. desc = kzalloc(sizeof(struct dw_desc), GFP_KERNEL);
  778. if (!desc) {
  779. dev_info(chan2dev(chan),
  780. "only allocated %d descriptors\n", i);
  781. spin_lock_bh(&dwc->lock);
  782. break;
  783. }
  784. INIT_LIST_HEAD(&desc->tx_list);
  785. dma_async_tx_descriptor_init(&desc->txd, chan);
  786. desc->txd.tx_submit = dwc_tx_submit;
  787. desc->txd.flags = DMA_CTRL_ACK;
  788. desc->txd.phys = dma_map_single(chan2parent(chan), &desc->lli,
  789. sizeof(desc->lli), DMA_TO_DEVICE);
  790. dwc_desc_put(dwc, desc);
  791. spin_lock_bh(&dwc->lock);
  792. i = ++dwc->descs_allocated;
  793. }
  794. /* Enable interrupts */
  795. channel_set_bit(dw, MASK.XFER, dwc->mask);
  796. channel_set_bit(dw, MASK.BLOCK, dwc->mask);
  797. channel_set_bit(dw, MASK.ERROR, dwc->mask);
  798. spin_unlock_bh(&dwc->lock);
  799. dev_dbg(chan2dev(chan),
  800. "alloc_chan_resources allocated %d descriptors\n", i);
  801. return i;
  802. }
  803. static void dwc_free_chan_resources(struct dma_chan *chan)
  804. {
  805. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  806. struct dw_dma *dw = to_dw_dma(chan->device);
  807. struct dw_desc *desc, *_desc;
  808. LIST_HEAD(list);
  809. dev_dbg(chan2dev(chan), "free_chan_resources (descs allocated=%u)\n",
  810. dwc->descs_allocated);
  811. /* ASSERT: channel is idle */
  812. BUG_ON(!list_empty(&dwc->active_list));
  813. BUG_ON(!list_empty(&dwc->queue));
  814. BUG_ON(dma_readl(to_dw_dma(chan->device), CH_EN) & dwc->mask);
  815. spin_lock_bh(&dwc->lock);
  816. list_splice_init(&dwc->free_list, &list);
  817. dwc->descs_allocated = 0;
  818. /* Disable interrupts */
  819. channel_clear_bit(dw, MASK.XFER, dwc->mask);
  820. channel_clear_bit(dw, MASK.BLOCK, dwc->mask);
  821. channel_clear_bit(dw, MASK.ERROR, dwc->mask);
  822. spin_unlock_bh(&dwc->lock);
  823. list_for_each_entry_safe(desc, _desc, &list, desc_node) {
  824. dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
  825. dma_unmap_single(chan2parent(chan), desc->txd.phys,
  826. sizeof(desc->lli), DMA_TO_DEVICE);
  827. kfree(desc);
  828. }
  829. dev_vdbg(chan2dev(chan), "free_chan_resources done\n");
  830. }
  831. /* --------------------- Cyclic DMA API extensions -------------------- */
  832. /**
  833. * dw_dma_cyclic_start - start the cyclic DMA transfer
  834. * @chan: the DMA channel to start
  835. *
  836. * Must be called with soft interrupts disabled. Returns zero on success or
  837. * -errno on failure.
  838. */
  839. int dw_dma_cyclic_start(struct dma_chan *chan)
  840. {
  841. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  842. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  843. if (!test_bit(DW_DMA_IS_CYCLIC, &dwc->flags)) {
  844. dev_err(chan2dev(&dwc->chan), "missing prep for cyclic DMA\n");
  845. return -ENODEV;
  846. }
  847. spin_lock(&dwc->lock);
  848. /* assert channel is idle */
  849. if (dma_readl(dw, CH_EN) & dwc->mask) {
  850. dev_err(chan2dev(&dwc->chan),
  851. "BUG: Attempted to start non-idle channel\n");
  852. dev_err(chan2dev(&dwc->chan),
  853. " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
  854. channel_readl(dwc, SAR),
  855. channel_readl(dwc, DAR),
  856. channel_readl(dwc, LLP),
  857. channel_readl(dwc, CTL_HI),
  858. channel_readl(dwc, CTL_LO));
  859. spin_unlock(&dwc->lock);
  860. return -EBUSY;
  861. }
  862. dma_writel(dw, CLEAR.BLOCK, dwc->mask);
  863. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  864. dma_writel(dw, CLEAR.XFER, dwc->mask);
  865. /* setup DMAC channel registers */
  866. channel_writel(dwc, LLP, dwc->cdesc->desc[0]->txd.phys);
  867. channel_writel(dwc, CTL_LO, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
  868. channel_writel(dwc, CTL_HI, 0);
  869. channel_set_bit(dw, CH_EN, dwc->mask);
  870. spin_unlock(&dwc->lock);
  871. return 0;
  872. }
  873. EXPORT_SYMBOL(dw_dma_cyclic_start);
  874. /**
  875. * dw_dma_cyclic_stop - stop the cyclic DMA transfer
  876. * @chan: the DMA channel to stop
  877. *
  878. * Must be called with soft interrupts disabled.
  879. */
  880. void dw_dma_cyclic_stop(struct dma_chan *chan)
  881. {
  882. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  883. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  884. spin_lock(&dwc->lock);
  885. channel_clear_bit(dw, CH_EN, dwc->mask);
  886. while (dma_readl(dw, CH_EN) & dwc->mask)
  887. cpu_relax();
  888. spin_unlock(&dwc->lock);
  889. }
  890. EXPORT_SYMBOL(dw_dma_cyclic_stop);
  891. /**
  892. * dw_dma_cyclic_prep - prepare the cyclic DMA transfer
  893. * @chan: the DMA channel to prepare
  894. * @buf_addr: physical DMA address where the buffer starts
  895. * @buf_len: total number of bytes for the entire buffer
  896. * @period_len: number of bytes for each period
  897. * @direction: transfer direction, to or from device
  898. *
  899. * Must be called before trying to start the transfer. Returns a valid struct
  900. * dw_cyclic_desc if successful or an ERR_PTR(-errno) if not successful.
  901. */
  902. struct dw_cyclic_desc *dw_dma_cyclic_prep(struct dma_chan *chan,
  903. dma_addr_t buf_addr, size_t buf_len, size_t period_len,
  904. enum dma_data_direction direction)
  905. {
  906. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  907. struct dw_cyclic_desc *cdesc;
  908. struct dw_cyclic_desc *retval = NULL;
  909. struct dw_desc *desc;
  910. struct dw_desc *last = NULL;
  911. struct dw_dma_slave *dws = chan->private;
  912. unsigned long was_cyclic;
  913. unsigned int reg_width;
  914. unsigned int periods;
  915. unsigned int i;
  916. spin_lock_bh(&dwc->lock);
  917. if (!list_empty(&dwc->queue) || !list_empty(&dwc->active_list)) {
  918. spin_unlock_bh(&dwc->lock);
  919. dev_dbg(chan2dev(&dwc->chan),
  920. "queue and/or active list are not empty\n");
  921. return ERR_PTR(-EBUSY);
  922. }
  923. was_cyclic = test_and_set_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
  924. spin_unlock_bh(&dwc->lock);
  925. if (was_cyclic) {
  926. dev_dbg(chan2dev(&dwc->chan),
  927. "channel already prepared for cyclic DMA\n");
  928. return ERR_PTR(-EBUSY);
  929. }
  930. retval = ERR_PTR(-EINVAL);
  931. reg_width = dws->reg_width;
  932. periods = buf_len / period_len;
  933. /* Check for too big/unaligned periods and unaligned DMA buffer. */
  934. if (period_len > (DWC_MAX_COUNT << reg_width))
  935. goto out_err;
  936. if (unlikely(period_len & ((1 << reg_width) - 1)))
  937. goto out_err;
  938. if (unlikely(buf_addr & ((1 << reg_width) - 1)))
  939. goto out_err;
  940. if (unlikely(!(direction & (DMA_TO_DEVICE | DMA_FROM_DEVICE))))
  941. goto out_err;
  942. retval = ERR_PTR(-ENOMEM);
  943. if (periods > NR_DESCS_PER_CHANNEL)
  944. goto out_err;
  945. cdesc = kzalloc(sizeof(struct dw_cyclic_desc), GFP_KERNEL);
  946. if (!cdesc)
  947. goto out_err;
  948. cdesc->desc = kzalloc(sizeof(struct dw_desc *) * periods, GFP_KERNEL);
  949. if (!cdesc->desc)
  950. goto out_err_alloc;
  951. for (i = 0; i < periods; i++) {
  952. desc = dwc_desc_get(dwc);
  953. if (!desc)
  954. goto out_err_desc_get;
  955. switch (direction) {
  956. case DMA_TO_DEVICE:
  957. desc->lli.dar = dws->tx_reg;
  958. desc->lli.sar = buf_addr + (period_len * i);
  959. desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan->private)
  960. | DWC_CTLL_DST_WIDTH(reg_width)
  961. | DWC_CTLL_SRC_WIDTH(reg_width)
  962. | DWC_CTLL_DST_FIX
  963. | DWC_CTLL_SRC_INC
  964. | DWC_CTLL_FC(dws->fc)
  965. | DWC_CTLL_INT_EN);
  966. break;
  967. case DMA_FROM_DEVICE:
  968. desc->lli.dar = buf_addr + (period_len * i);
  969. desc->lli.sar = dws->rx_reg;
  970. desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan->private)
  971. | DWC_CTLL_SRC_WIDTH(reg_width)
  972. | DWC_CTLL_DST_WIDTH(reg_width)
  973. | DWC_CTLL_DST_INC
  974. | DWC_CTLL_SRC_FIX
  975. | DWC_CTLL_FC(dws->fc)
  976. | DWC_CTLL_INT_EN);
  977. break;
  978. default:
  979. break;
  980. }
  981. desc->lli.ctlhi = (period_len >> reg_width);
  982. cdesc->desc[i] = desc;
  983. if (last) {
  984. last->lli.llp = desc->txd.phys;
  985. dma_sync_single_for_device(chan2parent(chan),
  986. last->txd.phys, sizeof(last->lli),
  987. DMA_TO_DEVICE);
  988. }
  989. last = desc;
  990. }
  991. /* lets make a cyclic list */
  992. last->lli.llp = cdesc->desc[0]->txd.phys;
  993. dma_sync_single_for_device(chan2parent(chan), last->txd.phys,
  994. sizeof(last->lli), DMA_TO_DEVICE);
  995. dev_dbg(chan2dev(&dwc->chan), "cyclic prepared buf 0x%08x len %zu "
  996. "period %zu periods %d\n", buf_addr, buf_len,
  997. period_len, periods);
  998. cdesc->periods = periods;
  999. dwc->cdesc = cdesc;
  1000. return cdesc;
  1001. out_err_desc_get:
  1002. while (i--)
  1003. dwc_desc_put(dwc, cdesc->desc[i]);
  1004. out_err_alloc:
  1005. kfree(cdesc);
  1006. out_err:
  1007. clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
  1008. return (struct dw_cyclic_desc *)retval;
  1009. }
  1010. EXPORT_SYMBOL(dw_dma_cyclic_prep);
  1011. /**
  1012. * dw_dma_cyclic_free - free a prepared cyclic DMA transfer
  1013. * @chan: the DMA channel to free
  1014. */
  1015. void dw_dma_cyclic_free(struct dma_chan *chan)
  1016. {
  1017. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  1018. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  1019. struct dw_cyclic_desc *cdesc = dwc->cdesc;
  1020. int i;
  1021. dev_dbg(chan2dev(&dwc->chan), "cyclic free\n");
  1022. if (!cdesc)
  1023. return;
  1024. spin_lock_bh(&dwc->lock);
  1025. channel_clear_bit(dw, CH_EN, dwc->mask);
  1026. while (dma_readl(dw, CH_EN) & dwc->mask)
  1027. cpu_relax();
  1028. dma_writel(dw, CLEAR.BLOCK, dwc->mask);
  1029. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  1030. dma_writel(dw, CLEAR.XFER, dwc->mask);
  1031. spin_unlock_bh(&dwc->lock);
  1032. for (i = 0; i < cdesc->periods; i++)
  1033. dwc_desc_put(dwc, cdesc->desc[i]);
  1034. kfree(cdesc->desc);
  1035. kfree(cdesc);
  1036. clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
  1037. }
  1038. EXPORT_SYMBOL(dw_dma_cyclic_free);
  1039. /*----------------------------------------------------------------------*/
  1040. static void dw_dma_off(struct dw_dma *dw)
  1041. {
  1042. dma_writel(dw, CFG, 0);
  1043. channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
  1044. channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
  1045. channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
  1046. channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
  1047. channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
  1048. while (dma_readl(dw, CFG) & DW_CFG_DMA_EN)
  1049. cpu_relax();
  1050. }
  1051. static int __init dw_probe(struct platform_device *pdev)
  1052. {
  1053. struct dw_dma_platform_data *pdata;
  1054. struct resource *io;
  1055. struct dw_dma *dw;
  1056. size_t size;
  1057. int irq;
  1058. int err;
  1059. int i;
  1060. pdata = pdev->dev.platform_data;
  1061. if (!pdata || pdata->nr_channels > DW_DMA_MAX_NR_CHANNELS)
  1062. return -EINVAL;
  1063. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1064. if (!io)
  1065. return -EINVAL;
  1066. irq = platform_get_irq(pdev, 0);
  1067. if (irq < 0)
  1068. return irq;
  1069. size = sizeof(struct dw_dma);
  1070. size += pdata->nr_channels * sizeof(struct dw_dma_chan);
  1071. dw = kzalloc(size, GFP_KERNEL);
  1072. if (!dw)
  1073. return -ENOMEM;
  1074. if (!request_mem_region(io->start, DW_REGLEN, pdev->dev.driver->name)) {
  1075. err = -EBUSY;
  1076. goto err_kfree;
  1077. }
  1078. dw->regs = ioremap(io->start, DW_REGLEN);
  1079. if (!dw->regs) {
  1080. err = -ENOMEM;
  1081. goto err_release_r;
  1082. }
  1083. dw->clk = clk_get(&pdev->dev, "hclk");
  1084. if (IS_ERR(dw->clk)) {
  1085. err = PTR_ERR(dw->clk);
  1086. goto err_clk;
  1087. }
  1088. clk_enable(dw->clk);
  1089. /* force dma off, just in case */
  1090. dw_dma_off(dw);
  1091. err = request_irq(irq, dw_dma_interrupt, 0, "dw_dmac", dw);
  1092. if (err)
  1093. goto err_irq;
  1094. platform_set_drvdata(pdev, dw);
  1095. tasklet_init(&dw->tasklet, dw_dma_tasklet, (unsigned long)dw);
  1096. dw->all_chan_mask = (1 << pdata->nr_channels) - 1;
  1097. INIT_LIST_HEAD(&dw->dma.channels);
  1098. for (i = 0; i < pdata->nr_channels; i++, dw->dma.chancnt++) {
  1099. struct dw_dma_chan *dwc = &dw->chan[i];
  1100. dwc->chan.device = &dw->dma;
  1101. dwc->chan.cookie = dwc->completed = 1;
  1102. dwc->chan.chan_id = i;
  1103. if (pdata->chan_allocation_order == CHAN_ALLOCATION_ASCENDING)
  1104. list_add_tail(&dwc->chan.device_node,
  1105. &dw->dma.channels);
  1106. else
  1107. list_add(&dwc->chan.device_node, &dw->dma.channels);
  1108. /* 7 is highest priority & 0 is lowest. */
  1109. if (pdata->chan_priority == CHAN_PRIORITY_ASCENDING)
  1110. dwc->priority = 7 - i;
  1111. else
  1112. dwc->priority = i;
  1113. dwc->ch_regs = &__dw_regs(dw)->CHAN[i];
  1114. spin_lock_init(&dwc->lock);
  1115. dwc->mask = 1 << i;
  1116. INIT_LIST_HEAD(&dwc->active_list);
  1117. INIT_LIST_HEAD(&dwc->queue);
  1118. INIT_LIST_HEAD(&dwc->free_list);
  1119. channel_clear_bit(dw, CH_EN, dwc->mask);
  1120. }
  1121. /* Clear/disable all interrupts on all channels. */
  1122. dma_writel(dw, CLEAR.XFER, dw->all_chan_mask);
  1123. dma_writel(dw, CLEAR.BLOCK, dw->all_chan_mask);
  1124. dma_writel(dw, CLEAR.SRC_TRAN, dw->all_chan_mask);
  1125. dma_writel(dw, CLEAR.DST_TRAN, dw->all_chan_mask);
  1126. dma_writel(dw, CLEAR.ERROR, dw->all_chan_mask);
  1127. channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
  1128. channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
  1129. channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
  1130. channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
  1131. channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
  1132. dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
  1133. dma_cap_set(DMA_SLAVE, dw->dma.cap_mask);
  1134. if (pdata->is_private)
  1135. dma_cap_set(DMA_PRIVATE, dw->dma.cap_mask);
  1136. dw->dma.dev = &pdev->dev;
  1137. dw->dma.device_alloc_chan_resources = dwc_alloc_chan_resources;
  1138. dw->dma.device_free_chan_resources = dwc_free_chan_resources;
  1139. dw->dma.device_prep_dma_memcpy = dwc_prep_dma_memcpy;
  1140. dw->dma.device_prep_slave_sg = dwc_prep_slave_sg;
  1141. dw->dma.device_control = dwc_control;
  1142. dw->dma.device_tx_status = dwc_tx_status;
  1143. dw->dma.device_issue_pending = dwc_issue_pending;
  1144. dma_writel(dw, CFG, DW_CFG_DMA_EN);
  1145. printk(KERN_INFO "%s: DesignWare DMA Controller, %d channels\n",
  1146. dev_name(&pdev->dev), dw->dma.chancnt);
  1147. dma_async_device_register(&dw->dma);
  1148. return 0;
  1149. err_irq:
  1150. clk_disable(dw->clk);
  1151. clk_put(dw->clk);
  1152. err_clk:
  1153. iounmap(dw->regs);
  1154. dw->regs = NULL;
  1155. err_release_r:
  1156. release_resource(io);
  1157. err_kfree:
  1158. kfree(dw);
  1159. return err;
  1160. }
  1161. static int __exit dw_remove(struct platform_device *pdev)
  1162. {
  1163. struct dw_dma *dw = platform_get_drvdata(pdev);
  1164. struct dw_dma_chan *dwc, *_dwc;
  1165. struct resource *io;
  1166. dw_dma_off(dw);
  1167. dma_async_device_unregister(&dw->dma);
  1168. free_irq(platform_get_irq(pdev, 0), dw);
  1169. tasklet_kill(&dw->tasklet);
  1170. list_for_each_entry_safe(dwc, _dwc, &dw->dma.channels,
  1171. chan.device_node) {
  1172. list_del(&dwc->chan.device_node);
  1173. channel_clear_bit(dw, CH_EN, dwc->mask);
  1174. }
  1175. clk_disable(dw->clk);
  1176. clk_put(dw->clk);
  1177. iounmap(dw->regs);
  1178. dw->regs = NULL;
  1179. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1180. release_mem_region(io->start, DW_REGLEN);
  1181. kfree(dw);
  1182. return 0;
  1183. }
  1184. static void dw_shutdown(struct platform_device *pdev)
  1185. {
  1186. struct dw_dma *dw = platform_get_drvdata(pdev);
  1187. dw_dma_off(platform_get_drvdata(pdev));
  1188. clk_disable(dw->clk);
  1189. }
  1190. static int dw_suspend_noirq(struct device *dev)
  1191. {
  1192. struct platform_device *pdev = to_platform_device(dev);
  1193. struct dw_dma *dw = platform_get_drvdata(pdev);
  1194. dw_dma_off(platform_get_drvdata(pdev));
  1195. clk_disable(dw->clk);
  1196. return 0;
  1197. }
  1198. static int dw_resume_noirq(struct device *dev)
  1199. {
  1200. struct platform_device *pdev = to_platform_device(dev);
  1201. struct dw_dma *dw = platform_get_drvdata(pdev);
  1202. clk_enable(dw->clk);
  1203. dma_writel(dw, CFG, DW_CFG_DMA_EN);
  1204. return 0;
  1205. }
  1206. static const struct dev_pm_ops dw_dev_pm_ops = {
  1207. .suspend_noirq = dw_suspend_noirq,
  1208. .resume_noirq = dw_resume_noirq,
  1209. };
  1210. static struct platform_driver dw_driver = {
  1211. .remove = __exit_p(dw_remove),
  1212. .shutdown = dw_shutdown,
  1213. .driver = {
  1214. .name = "dw_dmac",
  1215. .pm = &dw_dev_pm_ops,
  1216. },
  1217. };
  1218. static int __init dw_init(void)
  1219. {
  1220. return platform_driver_probe(&dw_driver, dw_probe);
  1221. }
  1222. subsys_initcall(dw_init);
  1223. static void __exit dw_exit(void)
  1224. {
  1225. platform_driver_unregister(&dw_driver);
  1226. }
  1227. module_exit(dw_exit);
  1228. MODULE_LICENSE("GPL v2");
  1229. MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller driver");
  1230. MODULE_AUTHOR("Haavard Skinnemoen <haavard.skinnemoen@atmel.com>");