dw_dmac.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956
  1. /*
  2. * Core driver for the Synopsys DesignWare DMA Controller
  3. *
  4. * Copyright (C) 2007-2008 Atmel Corporation
  5. * Copyright (C) 2010-2011 ST Microelectronics
  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/bitops.h>
  12. #include <linux/clk.h>
  13. #include <linux/delay.h>
  14. #include <linux/dmaengine.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/dmapool.h>
  17. #include <linux/err.h>
  18. #include <linux/init.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/io.h>
  21. #include <linux/of.h>
  22. #include <linux/of_dma.h>
  23. #include <linux/mm.h>
  24. #include <linux/module.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/slab.h>
  27. #include "dw_dmac_regs.h"
  28. #include "dmaengine.h"
  29. /*
  30. * This supports the Synopsys "DesignWare AHB Central DMA Controller",
  31. * (DW_ahb_dmac) which is used with various AMBA 2.0 systems (not all
  32. * of which use ARM any more). See the "Databook" from Synopsys for
  33. * information beyond what licensees probably provide.
  34. *
  35. * The driver has currently been tested only with the Atmel AT32AP7000,
  36. * which does not support descriptor writeback.
  37. */
  38. static inline unsigned int dwc_get_dms(struct dw_dma_slave *slave)
  39. {
  40. return slave ? slave->dst_master : 0;
  41. }
  42. static inline unsigned int dwc_get_sms(struct dw_dma_slave *slave)
  43. {
  44. return slave ? slave->src_master : 1;
  45. }
  46. #define SRC_MASTER 0
  47. #define DST_MASTER 1
  48. static inline unsigned int dwc_get_master(struct dma_chan *chan, int master)
  49. {
  50. struct dw_dma *dw = to_dw_dma(chan->device);
  51. struct dw_dma_slave *dws = chan->private;
  52. unsigned int m;
  53. if (master == SRC_MASTER)
  54. m = dwc_get_sms(dws);
  55. else
  56. m = dwc_get_dms(dws);
  57. return min_t(unsigned int, dw->nr_masters - 1, m);
  58. }
  59. #define DWC_DEFAULT_CTLLO(_chan) ({ \
  60. struct dw_dma_chan *_dwc = to_dw_dma_chan(_chan); \
  61. struct dma_slave_config *_sconfig = &_dwc->dma_sconfig; \
  62. bool _is_slave = is_slave_direction(_dwc->direction); \
  63. int _dms = dwc_get_master(_chan, DST_MASTER); \
  64. int _sms = dwc_get_master(_chan, SRC_MASTER); \
  65. u8 _smsize = _is_slave ? _sconfig->src_maxburst : \
  66. DW_DMA_MSIZE_16; \
  67. u8 _dmsize = _is_slave ? _sconfig->dst_maxburst : \
  68. DW_DMA_MSIZE_16; \
  69. \
  70. (DWC_CTLL_DST_MSIZE(_dmsize) \
  71. | DWC_CTLL_SRC_MSIZE(_smsize) \
  72. | DWC_CTLL_LLP_D_EN \
  73. | DWC_CTLL_LLP_S_EN \
  74. | DWC_CTLL_DMS(_dms) \
  75. | DWC_CTLL_SMS(_sms)); \
  76. })
  77. /*
  78. * Number of descriptors to allocate for each channel. This should be
  79. * made configurable somehow; preferably, the clients (at least the
  80. * ones using slave transfers) should be able to give us a hint.
  81. */
  82. #define NR_DESCS_PER_CHANNEL 64
  83. static inline unsigned int dwc_get_data_width(struct dma_chan *chan, int master)
  84. {
  85. struct dw_dma *dw = to_dw_dma(chan->device);
  86. return dw->data_width[dwc_get_master(chan, master)];
  87. }
  88. /*----------------------------------------------------------------------*/
  89. static struct device *chan2dev(struct dma_chan *chan)
  90. {
  91. return &chan->dev->device;
  92. }
  93. static struct device *chan2parent(struct dma_chan *chan)
  94. {
  95. return chan->dev->device.parent;
  96. }
  97. static struct dw_desc *dwc_first_active(struct dw_dma_chan *dwc)
  98. {
  99. return to_dw_desc(dwc->active_list.next);
  100. }
  101. static struct dw_desc *dwc_desc_get(struct dw_dma_chan *dwc)
  102. {
  103. struct dw_desc *desc, *_desc;
  104. struct dw_desc *ret = NULL;
  105. unsigned int i = 0;
  106. unsigned long flags;
  107. spin_lock_irqsave(&dwc->lock, flags);
  108. list_for_each_entry_safe(desc, _desc, &dwc->free_list, desc_node) {
  109. i++;
  110. if (async_tx_test_ack(&desc->txd)) {
  111. list_del(&desc->desc_node);
  112. ret = desc;
  113. break;
  114. }
  115. dev_dbg(chan2dev(&dwc->chan), "desc %p not ACKed\n", desc);
  116. }
  117. spin_unlock_irqrestore(&dwc->lock, flags);
  118. dev_vdbg(chan2dev(&dwc->chan), "scanned %u descriptors on freelist\n", i);
  119. return ret;
  120. }
  121. /*
  122. * Move a descriptor, including any children, to the free list.
  123. * `desc' must not be on any lists.
  124. */
  125. static void dwc_desc_put(struct dw_dma_chan *dwc, struct dw_desc *desc)
  126. {
  127. unsigned long flags;
  128. if (desc) {
  129. struct dw_desc *child;
  130. spin_lock_irqsave(&dwc->lock, flags);
  131. list_for_each_entry(child, &desc->tx_list, desc_node)
  132. dev_vdbg(chan2dev(&dwc->chan),
  133. "moving child desc %p to freelist\n",
  134. child);
  135. list_splice_init(&desc->tx_list, &dwc->free_list);
  136. dev_vdbg(chan2dev(&dwc->chan), "moving desc %p to freelist\n", desc);
  137. list_add(&desc->desc_node, &dwc->free_list);
  138. spin_unlock_irqrestore(&dwc->lock, flags);
  139. }
  140. }
  141. static void dwc_initialize(struct dw_dma_chan *dwc)
  142. {
  143. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  144. struct dw_dma_slave *dws = dwc->chan.private;
  145. u32 cfghi = DWC_CFGH_FIFO_MODE;
  146. u32 cfglo = DWC_CFGL_CH_PRIOR(dwc->priority);
  147. if (dwc->initialized == true)
  148. return;
  149. if (dws && dws->cfg_hi == ~0 && dws->cfg_lo == ~0) {
  150. /* autoconfigure based on request line from DT */
  151. if (dwc->direction == DMA_MEM_TO_DEV)
  152. cfghi = DWC_CFGH_DST_PER(dwc->request_line);
  153. else if (dwc->direction == DMA_DEV_TO_MEM)
  154. cfghi = DWC_CFGH_SRC_PER(dwc->request_line);
  155. } else if (dws) {
  156. /*
  157. * We need controller-specific data to set up slave
  158. * transfers.
  159. */
  160. BUG_ON(!dws->dma_dev || dws->dma_dev != dw->dma.dev);
  161. cfghi = dws->cfg_hi;
  162. cfglo |= dws->cfg_lo & ~DWC_CFGL_CH_PRIOR_MASK;
  163. } else {
  164. if (dwc->direction == DMA_MEM_TO_DEV)
  165. cfghi = DWC_CFGH_DST_PER(dwc->dma_sconfig.slave_id);
  166. else if (dwc->direction == DMA_DEV_TO_MEM)
  167. cfghi = DWC_CFGH_SRC_PER(dwc->dma_sconfig.slave_id);
  168. }
  169. channel_writel(dwc, CFG_LO, cfglo);
  170. channel_writel(dwc, CFG_HI, cfghi);
  171. /* Enable interrupts */
  172. channel_set_bit(dw, MASK.XFER, dwc->mask);
  173. channel_set_bit(dw, MASK.ERROR, dwc->mask);
  174. dwc->initialized = true;
  175. }
  176. /*----------------------------------------------------------------------*/
  177. static inline unsigned int dwc_fast_fls(unsigned long long v)
  178. {
  179. /*
  180. * We can be a lot more clever here, but this should take care
  181. * of the most common optimization.
  182. */
  183. if (!(v & 7))
  184. return 3;
  185. else if (!(v & 3))
  186. return 2;
  187. else if (!(v & 1))
  188. return 1;
  189. return 0;
  190. }
  191. static inline void dwc_dump_chan_regs(struct dw_dma_chan *dwc)
  192. {
  193. dev_err(chan2dev(&dwc->chan),
  194. " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
  195. channel_readl(dwc, SAR),
  196. channel_readl(dwc, DAR),
  197. channel_readl(dwc, LLP),
  198. channel_readl(dwc, CTL_HI),
  199. channel_readl(dwc, CTL_LO));
  200. }
  201. static inline void dwc_chan_disable(struct dw_dma *dw, struct dw_dma_chan *dwc)
  202. {
  203. channel_clear_bit(dw, CH_EN, dwc->mask);
  204. while (dma_readl(dw, CH_EN) & dwc->mask)
  205. cpu_relax();
  206. }
  207. /*----------------------------------------------------------------------*/
  208. /* Perform single block transfer */
  209. static inline void dwc_do_single_block(struct dw_dma_chan *dwc,
  210. struct dw_desc *desc)
  211. {
  212. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  213. u32 ctllo;
  214. /* Software emulation of LLP mode relies on interrupts to continue
  215. * multi block transfer. */
  216. ctllo = desc->lli.ctllo | DWC_CTLL_INT_EN;
  217. channel_writel(dwc, SAR, desc->lli.sar);
  218. channel_writel(dwc, DAR, desc->lli.dar);
  219. channel_writel(dwc, CTL_LO, ctllo);
  220. channel_writel(dwc, CTL_HI, desc->lli.ctlhi);
  221. channel_set_bit(dw, CH_EN, dwc->mask);
  222. /* Move pointer to next descriptor */
  223. dwc->tx_node_active = dwc->tx_node_active->next;
  224. }
  225. /* Called with dwc->lock held and bh disabled */
  226. static void dwc_dostart(struct dw_dma_chan *dwc, struct dw_desc *first)
  227. {
  228. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  229. unsigned long was_soft_llp;
  230. /* ASSERT: channel is idle */
  231. if (dma_readl(dw, CH_EN) & dwc->mask) {
  232. dev_err(chan2dev(&dwc->chan),
  233. "BUG: Attempted to start non-idle channel\n");
  234. dwc_dump_chan_regs(dwc);
  235. /* The tasklet will hopefully advance the queue... */
  236. return;
  237. }
  238. if (dwc->nollp) {
  239. was_soft_llp = test_and_set_bit(DW_DMA_IS_SOFT_LLP,
  240. &dwc->flags);
  241. if (was_soft_llp) {
  242. dev_err(chan2dev(&dwc->chan),
  243. "BUG: Attempted to start new LLP transfer "
  244. "inside ongoing one\n");
  245. return;
  246. }
  247. dwc_initialize(dwc);
  248. dwc->residue = first->total_len;
  249. dwc->tx_node_active = &first->tx_list;
  250. /* Submit first block */
  251. dwc_do_single_block(dwc, first);
  252. return;
  253. }
  254. dwc_initialize(dwc);
  255. channel_writel(dwc, LLP, first->txd.phys);
  256. channel_writel(dwc, CTL_LO,
  257. DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
  258. channel_writel(dwc, CTL_HI, 0);
  259. channel_set_bit(dw, CH_EN, dwc->mask);
  260. }
  261. /*----------------------------------------------------------------------*/
  262. static void
  263. dwc_descriptor_complete(struct dw_dma_chan *dwc, struct dw_desc *desc,
  264. bool callback_required)
  265. {
  266. dma_async_tx_callback callback = NULL;
  267. void *param = NULL;
  268. struct dma_async_tx_descriptor *txd = &desc->txd;
  269. struct dw_desc *child;
  270. unsigned long flags;
  271. dev_vdbg(chan2dev(&dwc->chan), "descriptor %u complete\n", txd->cookie);
  272. spin_lock_irqsave(&dwc->lock, flags);
  273. dma_cookie_complete(txd);
  274. if (callback_required) {
  275. callback = txd->callback;
  276. param = txd->callback_param;
  277. }
  278. /* async_tx_ack */
  279. list_for_each_entry(child, &desc->tx_list, desc_node)
  280. async_tx_ack(&child->txd);
  281. async_tx_ack(&desc->txd);
  282. list_splice_init(&desc->tx_list, &dwc->free_list);
  283. list_move(&desc->desc_node, &dwc->free_list);
  284. if (!is_slave_direction(dwc->direction)) {
  285. struct device *parent = chan2parent(&dwc->chan);
  286. if (!(txd->flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
  287. if (txd->flags & DMA_COMPL_DEST_UNMAP_SINGLE)
  288. dma_unmap_single(parent, desc->lli.dar,
  289. desc->total_len, DMA_FROM_DEVICE);
  290. else
  291. dma_unmap_page(parent, desc->lli.dar,
  292. desc->total_len, DMA_FROM_DEVICE);
  293. }
  294. if (!(txd->flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
  295. if (txd->flags & DMA_COMPL_SRC_UNMAP_SINGLE)
  296. dma_unmap_single(parent, desc->lli.sar,
  297. desc->total_len, DMA_TO_DEVICE);
  298. else
  299. dma_unmap_page(parent, desc->lli.sar,
  300. desc->total_len, DMA_TO_DEVICE);
  301. }
  302. }
  303. spin_unlock_irqrestore(&dwc->lock, flags);
  304. if (callback)
  305. callback(param);
  306. }
  307. static void dwc_complete_all(struct dw_dma *dw, struct dw_dma_chan *dwc)
  308. {
  309. struct dw_desc *desc, *_desc;
  310. LIST_HEAD(list);
  311. unsigned long flags;
  312. spin_lock_irqsave(&dwc->lock, flags);
  313. if (dma_readl(dw, CH_EN) & dwc->mask) {
  314. dev_err(chan2dev(&dwc->chan),
  315. "BUG: XFER bit set, but channel not idle!\n");
  316. /* Try to continue after resetting the channel... */
  317. dwc_chan_disable(dw, dwc);
  318. }
  319. /*
  320. * Submit queued descriptors ASAP, i.e. before we go through
  321. * the completed ones.
  322. */
  323. list_splice_init(&dwc->active_list, &list);
  324. if (!list_empty(&dwc->queue)) {
  325. list_move(dwc->queue.next, &dwc->active_list);
  326. dwc_dostart(dwc, dwc_first_active(dwc));
  327. }
  328. spin_unlock_irqrestore(&dwc->lock, flags);
  329. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  330. dwc_descriptor_complete(dwc, desc, true);
  331. }
  332. /* Returns how many bytes were already received from source */
  333. static inline u32 dwc_get_sent(struct dw_dma_chan *dwc)
  334. {
  335. u32 ctlhi = channel_readl(dwc, CTL_HI);
  336. u32 ctllo = channel_readl(dwc, CTL_LO);
  337. return (ctlhi & DWC_CTLH_BLOCK_TS_MASK) * (1 << (ctllo >> 4 & 7));
  338. }
  339. static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc)
  340. {
  341. dma_addr_t llp;
  342. struct dw_desc *desc, *_desc;
  343. struct dw_desc *child;
  344. u32 status_xfer;
  345. unsigned long flags;
  346. spin_lock_irqsave(&dwc->lock, flags);
  347. llp = channel_readl(dwc, LLP);
  348. status_xfer = dma_readl(dw, RAW.XFER);
  349. if (status_xfer & dwc->mask) {
  350. /* Everything we've submitted is done */
  351. dma_writel(dw, CLEAR.XFER, dwc->mask);
  352. if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags)) {
  353. struct list_head *head, *active = dwc->tx_node_active;
  354. /*
  355. * We are inside first active descriptor.
  356. * Otherwise something is really wrong.
  357. */
  358. desc = dwc_first_active(dwc);
  359. head = &desc->tx_list;
  360. if (active != head) {
  361. /* Update desc to reflect last sent one */
  362. if (active != head->next)
  363. desc = to_dw_desc(active->prev);
  364. dwc->residue -= desc->len;
  365. child = to_dw_desc(active);
  366. /* Submit next block */
  367. dwc_do_single_block(dwc, child);
  368. spin_unlock_irqrestore(&dwc->lock, flags);
  369. return;
  370. }
  371. /* We are done here */
  372. clear_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags);
  373. }
  374. dwc->residue = 0;
  375. spin_unlock_irqrestore(&dwc->lock, flags);
  376. dwc_complete_all(dw, dwc);
  377. return;
  378. }
  379. if (list_empty(&dwc->active_list)) {
  380. dwc->residue = 0;
  381. spin_unlock_irqrestore(&dwc->lock, flags);
  382. return;
  383. }
  384. if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags)) {
  385. dev_vdbg(chan2dev(&dwc->chan), "%s: soft LLP mode\n", __func__);
  386. spin_unlock_irqrestore(&dwc->lock, flags);
  387. return;
  388. }
  389. dev_vdbg(chan2dev(&dwc->chan), "%s: llp=0x%llx\n", __func__,
  390. (unsigned long long)llp);
  391. list_for_each_entry_safe(desc, _desc, &dwc->active_list, desc_node) {
  392. /* initial residue value */
  393. dwc->residue = desc->total_len;
  394. /* check first descriptors addr */
  395. if (desc->txd.phys == llp) {
  396. spin_unlock_irqrestore(&dwc->lock, flags);
  397. return;
  398. }
  399. /* check first descriptors llp */
  400. if (desc->lli.llp == llp) {
  401. /* This one is currently in progress */
  402. dwc->residue -= dwc_get_sent(dwc);
  403. spin_unlock_irqrestore(&dwc->lock, flags);
  404. return;
  405. }
  406. dwc->residue -= desc->len;
  407. list_for_each_entry(child, &desc->tx_list, desc_node) {
  408. if (child->lli.llp == llp) {
  409. /* Currently in progress */
  410. dwc->residue -= dwc_get_sent(dwc);
  411. spin_unlock_irqrestore(&dwc->lock, flags);
  412. return;
  413. }
  414. dwc->residue -= child->len;
  415. }
  416. /*
  417. * No descriptors so far seem to be in progress, i.e.
  418. * this one must be done.
  419. */
  420. spin_unlock_irqrestore(&dwc->lock, flags);
  421. dwc_descriptor_complete(dwc, desc, true);
  422. spin_lock_irqsave(&dwc->lock, flags);
  423. }
  424. dev_err(chan2dev(&dwc->chan),
  425. "BUG: All descriptors done, but channel not idle!\n");
  426. /* Try to continue after resetting the channel... */
  427. dwc_chan_disable(dw, dwc);
  428. if (!list_empty(&dwc->queue)) {
  429. list_move(dwc->queue.next, &dwc->active_list);
  430. dwc_dostart(dwc, dwc_first_active(dwc));
  431. }
  432. spin_unlock_irqrestore(&dwc->lock, flags);
  433. }
  434. static inline void dwc_dump_lli(struct dw_dma_chan *dwc, struct dw_lli *lli)
  435. {
  436. dev_crit(chan2dev(&dwc->chan), " desc: s0x%x d0x%x l0x%x c0x%x:%x\n",
  437. lli->sar, lli->dar, lli->llp, lli->ctlhi, lli->ctllo);
  438. }
  439. static void dwc_handle_error(struct dw_dma *dw, struct dw_dma_chan *dwc)
  440. {
  441. struct dw_desc *bad_desc;
  442. struct dw_desc *child;
  443. unsigned long flags;
  444. dwc_scan_descriptors(dw, dwc);
  445. spin_lock_irqsave(&dwc->lock, flags);
  446. /*
  447. * The descriptor currently at the head of the active list is
  448. * borked. Since we don't have any way to report errors, we'll
  449. * just have to scream loudly and try to carry on.
  450. */
  451. bad_desc = dwc_first_active(dwc);
  452. list_del_init(&bad_desc->desc_node);
  453. list_move(dwc->queue.next, dwc->active_list.prev);
  454. /* Clear the error flag and try to restart the controller */
  455. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  456. if (!list_empty(&dwc->active_list))
  457. dwc_dostart(dwc, dwc_first_active(dwc));
  458. /*
  459. * WARN may seem harsh, but since this only happens
  460. * when someone submits a bad physical address in a
  461. * descriptor, we should consider ourselves lucky that the
  462. * controller flagged an error instead of scribbling over
  463. * random memory locations.
  464. */
  465. dev_WARN(chan2dev(&dwc->chan), "Bad descriptor submitted for DMA!\n"
  466. " cookie: %d\n", bad_desc->txd.cookie);
  467. dwc_dump_lli(dwc, &bad_desc->lli);
  468. list_for_each_entry(child, &bad_desc->tx_list, desc_node)
  469. dwc_dump_lli(dwc, &child->lli);
  470. spin_unlock_irqrestore(&dwc->lock, flags);
  471. /* Pretend the descriptor completed successfully */
  472. dwc_descriptor_complete(dwc, bad_desc, true);
  473. }
  474. /* --------------------- Cyclic DMA API extensions -------------------- */
  475. inline dma_addr_t dw_dma_get_src_addr(struct dma_chan *chan)
  476. {
  477. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  478. return channel_readl(dwc, SAR);
  479. }
  480. EXPORT_SYMBOL(dw_dma_get_src_addr);
  481. inline dma_addr_t dw_dma_get_dst_addr(struct dma_chan *chan)
  482. {
  483. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  484. return channel_readl(dwc, DAR);
  485. }
  486. EXPORT_SYMBOL(dw_dma_get_dst_addr);
  487. /* called with dwc->lock held and all DMAC interrupts disabled */
  488. static void dwc_handle_cyclic(struct dw_dma *dw, struct dw_dma_chan *dwc,
  489. u32 status_err, u32 status_xfer)
  490. {
  491. unsigned long flags;
  492. if (dwc->mask) {
  493. void (*callback)(void *param);
  494. void *callback_param;
  495. dev_vdbg(chan2dev(&dwc->chan), "new cyclic period llp 0x%08x\n",
  496. channel_readl(dwc, LLP));
  497. callback = dwc->cdesc->period_callback;
  498. callback_param = dwc->cdesc->period_callback_param;
  499. if (callback)
  500. callback(callback_param);
  501. }
  502. /*
  503. * Error and transfer complete are highly unlikely, and will most
  504. * likely be due to a configuration error by the user.
  505. */
  506. if (unlikely(status_err & dwc->mask) ||
  507. unlikely(status_xfer & dwc->mask)) {
  508. int i;
  509. dev_err(chan2dev(&dwc->chan), "cyclic DMA unexpected %s "
  510. "interrupt, stopping DMA transfer\n",
  511. status_xfer ? "xfer" : "error");
  512. spin_lock_irqsave(&dwc->lock, flags);
  513. dwc_dump_chan_regs(dwc);
  514. dwc_chan_disable(dw, dwc);
  515. /* make sure DMA does not restart by loading a new list */
  516. channel_writel(dwc, LLP, 0);
  517. channel_writel(dwc, CTL_LO, 0);
  518. channel_writel(dwc, CTL_HI, 0);
  519. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  520. dma_writel(dw, CLEAR.XFER, dwc->mask);
  521. for (i = 0; i < dwc->cdesc->periods; i++)
  522. dwc_dump_lli(dwc, &dwc->cdesc->desc[i]->lli);
  523. spin_unlock_irqrestore(&dwc->lock, flags);
  524. }
  525. }
  526. /* ------------------------------------------------------------------------- */
  527. static void dw_dma_tasklet(unsigned long data)
  528. {
  529. struct dw_dma *dw = (struct dw_dma *)data;
  530. struct dw_dma_chan *dwc;
  531. u32 status_xfer;
  532. u32 status_err;
  533. int i;
  534. status_xfer = dma_readl(dw, RAW.XFER);
  535. status_err = dma_readl(dw, RAW.ERROR);
  536. dev_vdbg(dw->dma.dev, "%s: status_err=%x\n", __func__, status_err);
  537. for (i = 0; i < dw->dma.chancnt; i++) {
  538. dwc = &dw->chan[i];
  539. if (test_bit(DW_DMA_IS_CYCLIC, &dwc->flags))
  540. dwc_handle_cyclic(dw, dwc, status_err, status_xfer);
  541. else if (status_err & (1 << i))
  542. dwc_handle_error(dw, dwc);
  543. else if (status_xfer & (1 << i))
  544. dwc_scan_descriptors(dw, dwc);
  545. }
  546. /*
  547. * Re-enable interrupts.
  548. */
  549. channel_set_bit(dw, MASK.XFER, dw->all_chan_mask);
  550. channel_set_bit(dw, MASK.ERROR, dw->all_chan_mask);
  551. }
  552. static irqreturn_t dw_dma_interrupt(int irq, void *dev_id)
  553. {
  554. struct dw_dma *dw = dev_id;
  555. u32 status;
  556. dev_vdbg(dw->dma.dev, "%s: status=0x%x\n", __func__,
  557. dma_readl(dw, STATUS_INT));
  558. /*
  559. * Just disable the interrupts. We'll turn them back on in the
  560. * softirq handler.
  561. */
  562. channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
  563. channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
  564. status = dma_readl(dw, STATUS_INT);
  565. if (status) {
  566. dev_err(dw->dma.dev,
  567. "BUG: Unexpected interrupts pending: 0x%x\n",
  568. status);
  569. /* Try to recover */
  570. channel_clear_bit(dw, MASK.XFER, (1 << 8) - 1);
  571. channel_clear_bit(dw, MASK.SRC_TRAN, (1 << 8) - 1);
  572. channel_clear_bit(dw, MASK.DST_TRAN, (1 << 8) - 1);
  573. channel_clear_bit(dw, MASK.ERROR, (1 << 8) - 1);
  574. }
  575. tasklet_schedule(&dw->tasklet);
  576. return IRQ_HANDLED;
  577. }
  578. /*----------------------------------------------------------------------*/
  579. static dma_cookie_t dwc_tx_submit(struct dma_async_tx_descriptor *tx)
  580. {
  581. struct dw_desc *desc = txd_to_dw_desc(tx);
  582. struct dw_dma_chan *dwc = to_dw_dma_chan(tx->chan);
  583. dma_cookie_t cookie;
  584. unsigned long flags;
  585. spin_lock_irqsave(&dwc->lock, flags);
  586. cookie = dma_cookie_assign(tx);
  587. /*
  588. * REVISIT: We should attempt to chain as many descriptors as
  589. * possible, perhaps even appending to those already submitted
  590. * for DMA. But this is hard to do in a race-free manner.
  591. */
  592. if (list_empty(&dwc->active_list)) {
  593. dev_vdbg(chan2dev(tx->chan), "%s: started %u\n", __func__,
  594. desc->txd.cookie);
  595. list_add_tail(&desc->desc_node, &dwc->active_list);
  596. dwc_dostart(dwc, dwc_first_active(dwc));
  597. } else {
  598. dev_vdbg(chan2dev(tx->chan), "%s: queued %u\n", __func__,
  599. desc->txd.cookie);
  600. list_add_tail(&desc->desc_node, &dwc->queue);
  601. }
  602. spin_unlock_irqrestore(&dwc->lock, flags);
  603. return cookie;
  604. }
  605. static struct dma_async_tx_descriptor *
  606. dwc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  607. size_t len, unsigned long flags)
  608. {
  609. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  610. struct dw_desc *desc;
  611. struct dw_desc *first;
  612. struct dw_desc *prev;
  613. size_t xfer_count;
  614. size_t offset;
  615. unsigned int src_width;
  616. unsigned int dst_width;
  617. unsigned int data_width;
  618. u32 ctllo;
  619. dev_vdbg(chan2dev(chan),
  620. "%s: d0x%llx s0x%llx l0x%zx f0x%lx\n", __func__,
  621. (unsigned long long)dest, (unsigned long long)src,
  622. len, flags);
  623. if (unlikely(!len)) {
  624. dev_dbg(chan2dev(chan), "%s: length is zero!\n", __func__);
  625. return NULL;
  626. }
  627. dwc->direction = DMA_MEM_TO_MEM;
  628. data_width = min_t(unsigned int, dwc_get_data_width(chan, SRC_MASTER),
  629. dwc_get_data_width(chan, DST_MASTER));
  630. src_width = dst_width = min_t(unsigned int, data_width,
  631. dwc_fast_fls(src | dest | len));
  632. ctllo = DWC_DEFAULT_CTLLO(chan)
  633. | DWC_CTLL_DST_WIDTH(dst_width)
  634. | DWC_CTLL_SRC_WIDTH(src_width)
  635. | DWC_CTLL_DST_INC
  636. | DWC_CTLL_SRC_INC
  637. | DWC_CTLL_FC_M2M;
  638. prev = first = NULL;
  639. for (offset = 0; offset < len; offset += xfer_count << src_width) {
  640. xfer_count = min_t(size_t, (len - offset) >> src_width,
  641. dwc->block_size);
  642. desc = dwc_desc_get(dwc);
  643. if (!desc)
  644. goto err_desc_get;
  645. desc->lli.sar = src + offset;
  646. desc->lli.dar = dest + offset;
  647. desc->lli.ctllo = ctllo;
  648. desc->lli.ctlhi = xfer_count;
  649. desc->len = xfer_count << src_width;
  650. if (!first) {
  651. first = desc;
  652. } else {
  653. prev->lli.llp = desc->txd.phys;
  654. list_add_tail(&desc->desc_node,
  655. &first->tx_list);
  656. }
  657. prev = desc;
  658. }
  659. if (flags & DMA_PREP_INTERRUPT)
  660. /* Trigger interrupt after last block */
  661. prev->lli.ctllo |= DWC_CTLL_INT_EN;
  662. prev->lli.llp = 0;
  663. first->txd.flags = flags;
  664. first->total_len = len;
  665. return &first->txd;
  666. err_desc_get:
  667. dwc_desc_put(dwc, first);
  668. return NULL;
  669. }
  670. static struct dma_async_tx_descriptor *
  671. dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  672. unsigned int sg_len, enum dma_transfer_direction direction,
  673. unsigned long flags, void *context)
  674. {
  675. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  676. struct dma_slave_config *sconfig = &dwc->dma_sconfig;
  677. struct dw_desc *prev;
  678. struct dw_desc *first;
  679. u32 ctllo;
  680. dma_addr_t reg;
  681. unsigned int reg_width;
  682. unsigned int mem_width;
  683. unsigned int data_width;
  684. unsigned int i;
  685. struct scatterlist *sg;
  686. size_t total_len = 0;
  687. dev_vdbg(chan2dev(chan), "%s\n", __func__);
  688. if (unlikely(!is_slave_direction(direction) || !sg_len))
  689. return NULL;
  690. dwc->direction = direction;
  691. prev = first = NULL;
  692. switch (direction) {
  693. case DMA_MEM_TO_DEV:
  694. reg_width = __fls(sconfig->dst_addr_width);
  695. reg = sconfig->dst_addr;
  696. ctllo = (DWC_DEFAULT_CTLLO(chan)
  697. | DWC_CTLL_DST_WIDTH(reg_width)
  698. | DWC_CTLL_DST_FIX
  699. | DWC_CTLL_SRC_INC);
  700. ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_M2P) :
  701. DWC_CTLL_FC(DW_DMA_FC_D_M2P);
  702. data_width = dwc_get_data_width(chan, SRC_MASTER);
  703. for_each_sg(sgl, sg, sg_len, i) {
  704. struct dw_desc *desc;
  705. u32 len, dlen, mem;
  706. mem = sg_dma_address(sg);
  707. len = sg_dma_len(sg);
  708. mem_width = min_t(unsigned int,
  709. data_width, dwc_fast_fls(mem | len));
  710. slave_sg_todev_fill_desc:
  711. desc = dwc_desc_get(dwc);
  712. if (!desc) {
  713. dev_err(chan2dev(chan),
  714. "not enough descriptors available\n");
  715. goto err_desc_get;
  716. }
  717. desc->lli.sar = mem;
  718. desc->lli.dar = reg;
  719. desc->lli.ctllo = ctllo | DWC_CTLL_SRC_WIDTH(mem_width);
  720. if ((len >> mem_width) > dwc->block_size) {
  721. dlen = dwc->block_size << mem_width;
  722. mem += dlen;
  723. len -= dlen;
  724. } else {
  725. dlen = len;
  726. len = 0;
  727. }
  728. desc->lli.ctlhi = dlen >> mem_width;
  729. desc->len = dlen;
  730. if (!first) {
  731. first = desc;
  732. } else {
  733. prev->lli.llp = desc->txd.phys;
  734. list_add_tail(&desc->desc_node,
  735. &first->tx_list);
  736. }
  737. prev = desc;
  738. total_len += dlen;
  739. if (len)
  740. goto slave_sg_todev_fill_desc;
  741. }
  742. break;
  743. case DMA_DEV_TO_MEM:
  744. reg_width = __fls(sconfig->src_addr_width);
  745. reg = sconfig->src_addr;
  746. ctllo = (DWC_DEFAULT_CTLLO(chan)
  747. | DWC_CTLL_SRC_WIDTH(reg_width)
  748. | DWC_CTLL_DST_INC
  749. | DWC_CTLL_SRC_FIX);
  750. ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_P2M) :
  751. DWC_CTLL_FC(DW_DMA_FC_D_P2M);
  752. data_width = dwc_get_data_width(chan, DST_MASTER);
  753. for_each_sg(sgl, sg, sg_len, i) {
  754. struct dw_desc *desc;
  755. u32 len, dlen, mem;
  756. mem = sg_dma_address(sg);
  757. len = sg_dma_len(sg);
  758. mem_width = min_t(unsigned int,
  759. data_width, dwc_fast_fls(mem | len));
  760. slave_sg_fromdev_fill_desc:
  761. desc = dwc_desc_get(dwc);
  762. if (!desc) {
  763. dev_err(chan2dev(chan),
  764. "not enough descriptors available\n");
  765. goto err_desc_get;
  766. }
  767. desc->lli.sar = reg;
  768. desc->lli.dar = mem;
  769. desc->lli.ctllo = ctllo | DWC_CTLL_DST_WIDTH(mem_width);
  770. if ((len >> reg_width) > dwc->block_size) {
  771. dlen = dwc->block_size << reg_width;
  772. mem += dlen;
  773. len -= dlen;
  774. } else {
  775. dlen = len;
  776. len = 0;
  777. }
  778. desc->lli.ctlhi = dlen >> reg_width;
  779. desc->len = dlen;
  780. if (!first) {
  781. first = desc;
  782. } else {
  783. prev->lli.llp = desc->txd.phys;
  784. list_add_tail(&desc->desc_node,
  785. &first->tx_list);
  786. }
  787. prev = desc;
  788. total_len += dlen;
  789. if (len)
  790. goto slave_sg_fromdev_fill_desc;
  791. }
  792. break;
  793. default:
  794. return NULL;
  795. }
  796. if (flags & DMA_PREP_INTERRUPT)
  797. /* Trigger interrupt after last block */
  798. prev->lli.ctllo |= DWC_CTLL_INT_EN;
  799. prev->lli.llp = 0;
  800. first->total_len = total_len;
  801. return &first->txd;
  802. err_desc_get:
  803. dwc_desc_put(dwc, first);
  804. return NULL;
  805. }
  806. /*
  807. * Fix sconfig's burst size according to dw_dmac. We need to convert them as:
  808. * 1 -> 0, 4 -> 1, 8 -> 2, 16 -> 3.
  809. *
  810. * NOTE: burst size 2 is not supported by controller.
  811. *
  812. * This can be done by finding least significant bit set: n & (n - 1)
  813. */
  814. static inline void convert_burst(u32 *maxburst)
  815. {
  816. if (*maxburst > 1)
  817. *maxburst = fls(*maxburst) - 2;
  818. else
  819. *maxburst = 0;
  820. }
  821. static inline void convert_slave_id(struct dw_dma_chan *dwc)
  822. {
  823. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  824. dwc->dma_sconfig.slave_id -= dw->request_line_base;
  825. }
  826. static int
  827. set_runtime_config(struct dma_chan *chan, struct dma_slave_config *sconfig)
  828. {
  829. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  830. /* Check if chan will be configured for slave transfers */
  831. if (!is_slave_direction(sconfig->direction))
  832. return -EINVAL;
  833. memcpy(&dwc->dma_sconfig, sconfig, sizeof(*sconfig));
  834. dwc->direction = sconfig->direction;
  835. convert_burst(&dwc->dma_sconfig.src_maxburst);
  836. convert_burst(&dwc->dma_sconfig.dst_maxburst);
  837. convert_slave_id(dwc);
  838. return 0;
  839. }
  840. static inline void dwc_chan_pause(struct dw_dma_chan *dwc)
  841. {
  842. u32 cfglo = channel_readl(dwc, CFG_LO);
  843. unsigned int count = 20; /* timeout iterations */
  844. channel_writel(dwc, CFG_LO, cfglo | DWC_CFGL_CH_SUSP);
  845. while (!(channel_readl(dwc, CFG_LO) & DWC_CFGL_FIFO_EMPTY) && count--)
  846. udelay(2);
  847. dwc->paused = true;
  848. }
  849. static inline void dwc_chan_resume(struct dw_dma_chan *dwc)
  850. {
  851. u32 cfglo = channel_readl(dwc, CFG_LO);
  852. channel_writel(dwc, CFG_LO, cfglo & ~DWC_CFGL_CH_SUSP);
  853. dwc->paused = false;
  854. }
  855. static int dwc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  856. unsigned long arg)
  857. {
  858. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  859. struct dw_dma *dw = to_dw_dma(chan->device);
  860. struct dw_desc *desc, *_desc;
  861. unsigned long flags;
  862. LIST_HEAD(list);
  863. if (cmd == DMA_PAUSE) {
  864. spin_lock_irqsave(&dwc->lock, flags);
  865. dwc_chan_pause(dwc);
  866. spin_unlock_irqrestore(&dwc->lock, flags);
  867. } else if (cmd == DMA_RESUME) {
  868. if (!dwc->paused)
  869. return 0;
  870. spin_lock_irqsave(&dwc->lock, flags);
  871. dwc_chan_resume(dwc);
  872. spin_unlock_irqrestore(&dwc->lock, flags);
  873. } else if (cmd == DMA_TERMINATE_ALL) {
  874. spin_lock_irqsave(&dwc->lock, flags);
  875. clear_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags);
  876. dwc_chan_disable(dw, dwc);
  877. dwc_chan_resume(dwc);
  878. /* active_list entries will end up before queued entries */
  879. list_splice_init(&dwc->queue, &list);
  880. list_splice_init(&dwc->active_list, &list);
  881. spin_unlock_irqrestore(&dwc->lock, flags);
  882. /* Flush all pending and queued descriptors */
  883. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  884. dwc_descriptor_complete(dwc, desc, false);
  885. } else if (cmd == DMA_SLAVE_CONFIG) {
  886. return set_runtime_config(chan, (struct dma_slave_config *)arg);
  887. } else {
  888. return -ENXIO;
  889. }
  890. return 0;
  891. }
  892. static inline u32 dwc_get_residue(struct dw_dma_chan *dwc)
  893. {
  894. unsigned long flags;
  895. u32 residue;
  896. spin_lock_irqsave(&dwc->lock, flags);
  897. residue = dwc->residue;
  898. if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags) && residue)
  899. residue -= dwc_get_sent(dwc);
  900. spin_unlock_irqrestore(&dwc->lock, flags);
  901. return residue;
  902. }
  903. static enum dma_status
  904. dwc_tx_status(struct dma_chan *chan,
  905. dma_cookie_t cookie,
  906. struct dma_tx_state *txstate)
  907. {
  908. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  909. enum dma_status ret;
  910. ret = dma_cookie_status(chan, cookie, txstate);
  911. if (ret != DMA_SUCCESS) {
  912. dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
  913. ret = dma_cookie_status(chan, cookie, txstate);
  914. }
  915. if (ret != DMA_SUCCESS)
  916. dma_set_residue(txstate, dwc_get_residue(dwc));
  917. if (dwc->paused)
  918. return DMA_PAUSED;
  919. return ret;
  920. }
  921. static void dwc_issue_pending(struct dma_chan *chan)
  922. {
  923. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  924. if (!list_empty(&dwc->queue))
  925. dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
  926. }
  927. static int dwc_alloc_chan_resources(struct dma_chan *chan)
  928. {
  929. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  930. struct dw_dma *dw = to_dw_dma(chan->device);
  931. struct dw_desc *desc;
  932. int i;
  933. unsigned long flags;
  934. dev_vdbg(chan2dev(chan), "%s\n", __func__);
  935. /* ASSERT: channel is idle */
  936. if (dma_readl(dw, CH_EN) & dwc->mask) {
  937. dev_dbg(chan2dev(chan), "DMA channel not idle?\n");
  938. return -EIO;
  939. }
  940. dma_cookie_init(chan);
  941. /*
  942. * NOTE: some controllers may have additional features that we
  943. * need to initialize here, like "scatter-gather" (which
  944. * doesn't mean what you think it means), and status writeback.
  945. */
  946. spin_lock_irqsave(&dwc->lock, flags);
  947. i = dwc->descs_allocated;
  948. while (dwc->descs_allocated < NR_DESCS_PER_CHANNEL) {
  949. dma_addr_t phys;
  950. spin_unlock_irqrestore(&dwc->lock, flags);
  951. desc = dma_pool_alloc(dw->desc_pool, GFP_ATOMIC, &phys);
  952. if (!desc)
  953. goto err_desc_alloc;
  954. memset(desc, 0, sizeof(struct dw_desc));
  955. INIT_LIST_HEAD(&desc->tx_list);
  956. dma_async_tx_descriptor_init(&desc->txd, chan);
  957. desc->txd.tx_submit = dwc_tx_submit;
  958. desc->txd.flags = DMA_CTRL_ACK;
  959. desc->txd.phys = phys;
  960. dwc_desc_put(dwc, desc);
  961. spin_lock_irqsave(&dwc->lock, flags);
  962. i = ++dwc->descs_allocated;
  963. }
  964. spin_unlock_irqrestore(&dwc->lock, flags);
  965. dev_dbg(chan2dev(chan), "%s: allocated %d descriptors\n", __func__, i);
  966. return i;
  967. err_desc_alloc:
  968. dev_info(chan2dev(chan), "only allocated %d descriptors\n", i);
  969. return i;
  970. }
  971. static void dwc_free_chan_resources(struct dma_chan *chan)
  972. {
  973. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  974. struct dw_dma *dw = to_dw_dma(chan->device);
  975. struct dw_desc *desc, *_desc;
  976. unsigned long flags;
  977. LIST_HEAD(list);
  978. dev_dbg(chan2dev(chan), "%s: descs allocated=%u\n", __func__,
  979. dwc->descs_allocated);
  980. /* ASSERT: channel is idle */
  981. BUG_ON(!list_empty(&dwc->active_list));
  982. BUG_ON(!list_empty(&dwc->queue));
  983. BUG_ON(dma_readl(to_dw_dma(chan->device), CH_EN) & dwc->mask);
  984. spin_lock_irqsave(&dwc->lock, flags);
  985. list_splice_init(&dwc->free_list, &list);
  986. dwc->descs_allocated = 0;
  987. dwc->initialized = false;
  988. /* Disable interrupts */
  989. channel_clear_bit(dw, MASK.XFER, dwc->mask);
  990. channel_clear_bit(dw, MASK.ERROR, dwc->mask);
  991. spin_unlock_irqrestore(&dwc->lock, flags);
  992. list_for_each_entry_safe(desc, _desc, &list, desc_node) {
  993. dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
  994. dma_pool_free(dw->desc_pool, desc, desc->txd.phys);
  995. }
  996. dev_vdbg(chan2dev(chan), "%s: done\n", __func__);
  997. }
  998. struct dw_dma_filter_args {
  999. struct dw_dma *dw;
  1000. unsigned int req;
  1001. unsigned int src;
  1002. unsigned int dst;
  1003. };
  1004. static bool dw_dma_generic_filter(struct dma_chan *chan, void *param)
  1005. {
  1006. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  1007. struct dw_dma *dw = to_dw_dma(chan->device);
  1008. struct dw_dma_filter_args *fargs = param;
  1009. struct dw_dma_slave *dws = &dwc->slave;
  1010. /* ensure the device matches our channel */
  1011. if (chan->device != &fargs->dw->dma)
  1012. return false;
  1013. dws->dma_dev = dw->dma.dev;
  1014. dws->cfg_hi = ~0;
  1015. dws->cfg_lo = ~0;
  1016. dws->src_master = fargs->src;
  1017. dws->dst_master = fargs->dst;
  1018. dwc->request_line = fargs->req;
  1019. chan->private = dws;
  1020. return true;
  1021. }
  1022. static struct dma_chan *dw_dma_xlate(struct of_phandle_args *dma_spec,
  1023. struct of_dma *ofdma)
  1024. {
  1025. struct dw_dma *dw = ofdma->of_dma_data;
  1026. struct dw_dma_filter_args fargs = {
  1027. .dw = dw,
  1028. };
  1029. dma_cap_mask_t cap;
  1030. if (dma_spec->args_count != 3)
  1031. return NULL;
  1032. fargs.req = dma_spec->args[0];
  1033. fargs.src = dma_spec->args[1];
  1034. fargs.dst = dma_spec->args[2];
  1035. if (WARN_ON(fargs.req >= DW_DMA_MAX_NR_REQUESTS ||
  1036. fargs.src >= dw->nr_masters ||
  1037. fargs.dst >= dw->nr_masters))
  1038. return NULL;
  1039. dma_cap_zero(cap);
  1040. dma_cap_set(DMA_SLAVE, cap);
  1041. /* TODO: there should be a simpler way to do this */
  1042. return dma_request_channel(cap, dw_dma_generic_filter, &fargs);
  1043. }
  1044. /* --------------------- Cyclic DMA API extensions -------------------- */
  1045. /**
  1046. * dw_dma_cyclic_start - start the cyclic DMA transfer
  1047. * @chan: the DMA channel to start
  1048. *
  1049. * Must be called with soft interrupts disabled. Returns zero on success or
  1050. * -errno on failure.
  1051. */
  1052. int dw_dma_cyclic_start(struct dma_chan *chan)
  1053. {
  1054. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  1055. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  1056. unsigned long flags;
  1057. if (!test_bit(DW_DMA_IS_CYCLIC, &dwc->flags)) {
  1058. dev_err(chan2dev(&dwc->chan), "missing prep for cyclic DMA\n");
  1059. return -ENODEV;
  1060. }
  1061. spin_lock_irqsave(&dwc->lock, flags);
  1062. /* assert channel is idle */
  1063. if (dma_readl(dw, CH_EN) & dwc->mask) {
  1064. dev_err(chan2dev(&dwc->chan),
  1065. "BUG: Attempted to start non-idle channel\n");
  1066. dwc_dump_chan_regs(dwc);
  1067. spin_unlock_irqrestore(&dwc->lock, flags);
  1068. return -EBUSY;
  1069. }
  1070. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  1071. dma_writel(dw, CLEAR.XFER, dwc->mask);
  1072. /* setup DMAC channel registers */
  1073. channel_writel(dwc, LLP, dwc->cdesc->desc[0]->txd.phys);
  1074. channel_writel(dwc, CTL_LO, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
  1075. channel_writel(dwc, CTL_HI, 0);
  1076. channel_set_bit(dw, CH_EN, dwc->mask);
  1077. spin_unlock_irqrestore(&dwc->lock, flags);
  1078. return 0;
  1079. }
  1080. EXPORT_SYMBOL(dw_dma_cyclic_start);
  1081. /**
  1082. * dw_dma_cyclic_stop - stop the cyclic DMA transfer
  1083. * @chan: the DMA channel to stop
  1084. *
  1085. * Must be called with soft interrupts disabled.
  1086. */
  1087. void dw_dma_cyclic_stop(struct dma_chan *chan)
  1088. {
  1089. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  1090. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  1091. unsigned long flags;
  1092. spin_lock_irqsave(&dwc->lock, flags);
  1093. dwc_chan_disable(dw, dwc);
  1094. spin_unlock_irqrestore(&dwc->lock, flags);
  1095. }
  1096. EXPORT_SYMBOL(dw_dma_cyclic_stop);
  1097. /**
  1098. * dw_dma_cyclic_prep - prepare the cyclic DMA transfer
  1099. * @chan: the DMA channel to prepare
  1100. * @buf_addr: physical DMA address where the buffer starts
  1101. * @buf_len: total number of bytes for the entire buffer
  1102. * @period_len: number of bytes for each period
  1103. * @direction: transfer direction, to or from device
  1104. *
  1105. * Must be called before trying to start the transfer. Returns a valid struct
  1106. * dw_cyclic_desc if successful or an ERR_PTR(-errno) if not successful.
  1107. */
  1108. struct dw_cyclic_desc *dw_dma_cyclic_prep(struct dma_chan *chan,
  1109. dma_addr_t buf_addr, size_t buf_len, size_t period_len,
  1110. enum dma_transfer_direction direction)
  1111. {
  1112. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  1113. struct dma_slave_config *sconfig = &dwc->dma_sconfig;
  1114. struct dw_cyclic_desc *cdesc;
  1115. struct dw_cyclic_desc *retval = NULL;
  1116. struct dw_desc *desc;
  1117. struct dw_desc *last = NULL;
  1118. unsigned long was_cyclic;
  1119. unsigned int reg_width;
  1120. unsigned int periods;
  1121. unsigned int i;
  1122. unsigned long flags;
  1123. spin_lock_irqsave(&dwc->lock, flags);
  1124. if (dwc->nollp) {
  1125. spin_unlock_irqrestore(&dwc->lock, flags);
  1126. dev_dbg(chan2dev(&dwc->chan),
  1127. "channel doesn't support LLP transfers\n");
  1128. return ERR_PTR(-EINVAL);
  1129. }
  1130. if (!list_empty(&dwc->queue) || !list_empty(&dwc->active_list)) {
  1131. spin_unlock_irqrestore(&dwc->lock, flags);
  1132. dev_dbg(chan2dev(&dwc->chan),
  1133. "queue and/or active list are not empty\n");
  1134. return ERR_PTR(-EBUSY);
  1135. }
  1136. was_cyclic = test_and_set_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
  1137. spin_unlock_irqrestore(&dwc->lock, flags);
  1138. if (was_cyclic) {
  1139. dev_dbg(chan2dev(&dwc->chan),
  1140. "channel already prepared for cyclic DMA\n");
  1141. return ERR_PTR(-EBUSY);
  1142. }
  1143. retval = ERR_PTR(-EINVAL);
  1144. if (unlikely(!is_slave_direction(direction)))
  1145. goto out_err;
  1146. dwc->direction = direction;
  1147. if (direction == DMA_MEM_TO_DEV)
  1148. reg_width = __ffs(sconfig->dst_addr_width);
  1149. else
  1150. reg_width = __ffs(sconfig->src_addr_width);
  1151. periods = buf_len / period_len;
  1152. /* Check for too big/unaligned periods and unaligned DMA buffer. */
  1153. if (period_len > (dwc->block_size << reg_width))
  1154. goto out_err;
  1155. if (unlikely(period_len & ((1 << reg_width) - 1)))
  1156. goto out_err;
  1157. if (unlikely(buf_addr & ((1 << reg_width) - 1)))
  1158. goto out_err;
  1159. retval = ERR_PTR(-ENOMEM);
  1160. if (periods > NR_DESCS_PER_CHANNEL)
  1161. goto out_err;
  1162. cdesc = kzalloc(sizeof(struct dw_cyclic_desc), GFP_KERNEL);
  1163. if (!cdesc)
  1164. goto out_err;
  1165. cdesc->desc = kzalloc(sizeof(struct dw_desc *) * periods, GFP_KERNEL);
  1166. if (!cdesc->desc)
  1167. goto out_err_alloc;
  1168. for (i = 0; i < periods; i++) {
  1169. desc = dwc_desc_get(dwc);
  1170. if (!desc)
  1171. goto out_err_desc_get;
  1172. switch (direction) {
  1173. case DMA_MEM_TO_DEV:
  1174. desc->lli.dar = sconfig->dst_addr;
  1175. desc->lli.sar = buf_addr + (period_len * i);
  1176. desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan)
  1177. | DWC_CTLL_DST_WIDTH(reg_width)
  1178. | DWC_CTLL_SRC_WIDTH(reg_width)
  1179. | DWC_CTLL_DST_FIX
  1180. | DWC_CTLL_SRC_INC
  1181. | DWC_CTLL_INT_EN);
  1182. desc->lli.ctllo |= sconfig->device_fc ?
  1183. DWC_CTLL_FC(DW_DMA_FC_P_M2P) :
  1184. DWC_CTLL_FC(DW_DMA_FC_D_M2P);
  1185. break;
  1186. case DMA_DEV_TO_MEM:
  1187. desc->lli.dar = buf_addr + (period_len * i);
  1188. desc->lli.sar = sconfig->src_addr;
  1189. desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan)
  1190. | DWC_CTLL_SRC_WIDTH(reg_width)
  1191. | DWC_CTLL_DST_WIDTH(reg_width)
  1192. | DWC_CTLL_DST_INC
  1193. | DWC_CTLL_SRC_FIX
  1194. | DWC_CTLL_INT_EN);
  1195. desc->lli.ctllo |= sconfig->device_fc ?
  1196. DWC_CTLL_FC(DW_DMA_FC_P_P2M) :
  1197. DWC_CTLL_FC(DW_DMA_FC_D_P2M);
  1198. break;
  1199. default:
  1200. break;
  1201. }
  1202. desc->lli.ctlhi = (period_len >> reg_width);
  1203. cdesc->desc[i] = desc;
  1204. if (last)
  1205. last->lli.llp = desc->txd.phys;
  1206. last = desc;
  1207. }
  1208. /* lets make a cyclic list */
  1209. last->lli.llp = cdesc->desc[0]->txd.phys;
  1210. dev_dbg(chan2dev(&dwc->chan), "cyclic prepared buf 0x%llx len %zu "
  1211. "period %zu periods %d\n", (unsigned long long)buf_addr,
  1212. buf_len, period_len, periods);
  1213. cdesc->periods = periods;
  1214. dwc->cdesc = cdesc;
  1215. return cdesc;
  1216. out_err_desc_get:
  1217. while (i--)
  1218. dwc_desc_put(dwc, cdesc->desc[i]);
  1219. out_err_alloc:
  1220. kfree(cdesc);
  1221. out_err:
  1222. clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
  1223. return (struct dw_cyclic_desc *)retval;
  1224. }
  1225. EXPORT_SYMBOL(dw_dma_cyclic_prep);
  1226. /**
  1227. * dw_dma_cyclic_free - free a prepared cyclic DMA transfer
  1228. * @chan: the DMA channel to free
  1229. */
  1230. void dw_dma_cyclic_free(struct dma_chan *chan)
  1231. {
  1232. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  1233. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  1234. struct dw_cyclic_desc *cdesc = dwc->cdesc;
  1235. int i;
  1236. unsigned long flags;
  1237. dev_dbg(chan2dev(&dwc->chan), "%s\n", __func__);
  1238. if (!cdesc)
  1239. return;
  1240. spin_lock_irqsave(&dwc->lock, flags);
  1241. dwc_chan_disable(dw, dwc);
  1242. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  1243. dma_writel(dw, CLEAR.XFER, dwc->mask);
  1244. spin_unlock_irqrestore(&dwc->lock, flags);
  1245. for (i = 0; i < cdesc->periods; i++)
  1246. dwc_desc_put(dwc, cdesc->desc[i]);
  1247. kfree(cdesc->desc);
  1248. kfree(cdesc);
  1249. clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
  1250. }
  1251. EXPORT_SYMBOL(dw_dma_cyclic_free);
  1252. /*----------------------------------------------------------------------*/
  1253. static void dw_dma_off(struct dw_dma *dw)
  1254. {
  1255. int i;
  1256. dma_writel(dw, CFG, 0);
  1257. channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
  1258. channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
  1259. channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
  1260. channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
  1261. while (dma_readl(dw, CFG) & DW_CFG_DMA_EN)
  1262. cpu_relax();
  1263. for (i = 0; i < dw->dma.chancnt; i++)
  1264. dw->chan[i].initialized = false;
  1265. }
  1266. #ifdef CONFIG_OF
  1267. static struct dw_dma_platform_data *
  1268. dw_dma_parse_dt(struct platform_device *pdev)
  1269. {
  1270. struct device_node *np = pdev->dev.of_node;
  1271. struct dw_dma_platform_data *pdata;
  1272. u32 tmp, arr[4];
  1273. if (!np) {
  1274. dev_err(&pdev->dev, "Missing DT data\n");
  1275. return NULL;
  1276. }
  1277. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1278. if (!pdata)
  1279. return NULL;
  1280. if (of_property_read_u32(np, "dma-channels", &pdata->nr_channels))
  1281. return NULL;
  1282. if (of_property_read_bool(np, "is_private"))
  1283. pdata->is_private = true;
  1284. if (!of_property_read_u32(np, "chan_allocation_order", &tmp))
  1285. pdata->chan_allocation_order = (unsigned char)tmp;
  1286. if (!of_property_read_u32(np, "chan_priority", &tmp))
  1287. pdata->chan_priority = tmp;
  1288. if (!of_property_read_u32(np, "block_size", &tmp))
  1289. pdata->block_size = tmp;
  1290. if (!of_property_read_u32(np, "dma-masters", &tmp)) {
  1291. if (tmp > 4)
  1292. return NULL;
  1293. pdata->nr_masters = tmp;
  1294. }
  1295. if (!of_property_read_u32_array(np, "data_width", arr,
  1296. pdata->nr_masters))
  1297. for (tmp = 0; tmp < pdata->nr_masters; tmp++)
  1298. pdata->data_width[tmp] = arr[tmp];
  1299. return pdata;
  1300. }
  1301. #else
  1302. static inline struct dw_dma_platform_data *
  1303. dw_dma_parse_dt(struct platform_device *pdev)
  1304. {
  1305. return NULL;
  1306. }
  1307. #endif
  1308. static int dw_probe(struct platform_device *pdev)
  1309. {
  1310. const struct platform_device_id *match;
  1311. struct dw_dma_platform_data *pdata;
  1312. struct resource *io;
  1313. struct dw_dma *dw;
  1314. size_t size;
  1315. void __iomem *regs;
  1316. bool autocfg;
  1317. unsigned int dw_params;
  1318. unsigned int nr_channels;
  1319. unsigned int max_blk_size = 0;
  1320. int irq;
  1321. int err;
  1322. int i;
  1323. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1324. if (!io)
  1325. return -EINVAL;
  1326. irq = platform_get_irq(pdev, 0);
  1327. if (irq < 0)
  1328. return irq;
  1329. regs = devm_ioremap_resource(&pdev->dev, io);
  1330. if (IS_ERR(regs))
  1331. return PTR_ERR(regs);
  1332. /* Apply default dma_mask if needed */
  1333. if (!pdev->dev.dma_mask) {
  1334. pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  1335. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  1336. }
  1337. dw_params = dma_read_byaddr(regs, DW_PARAMS);
  1338. autocfg = dw_params >> DW_PARAMS_EN & 0x1;
  1339. dev_dbg(&pdev->dev, "DW_PARAMS: 0x%08x\n", dw_params);
  1340. pdata = dev_get_platdata(&pdev->dev);
  1341. if (!pdata)
  1342. pdata = dw_dma_parse_dt(pdev);
  1343. if (!pdata && autocfg) {
  1344. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1345. if (!pdata)
  1346. return -ENOMEM;
  1347. /* Fill platform data with the default values */
  1348. pdata->is_private = true;
  1349. pdata->chan_allocation_order = CHAN_ALLOCATION_ASCENDING;
  1350. pdata->chan_priority = CHAN_PRIORITY_ASCENDING;
  1351. } else if (!pdata || pdata->nr_channels > DW_DMA_MAX_NR_CHANNELS)
  1352. return -EINVAL;
  1353. if (autocfg)
  1354. nr_channels = (dw_params >> DW_PARAMS_NR_CHAN & 0x7) + 1;
  1355. else
  1356. nr_channels = pdata->nr_channels;
  1357. size = sizeof(struct dw_dma) + nr_channels * sizeof(struct dw_dma_chan);
  1358. dw = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
  1359. if (!dw)
  1360. return -ENOMEM;
  1361. dw->clk = devm_clk_get(&pdev->dev, "hclk");
  1362. if (IS_ERR(dw->clk))
  1363. return PTR_ERR(dw->clk);
  1364. clk_prepare_enable(dw->clk);
  1365. dw->regs = regs;
  1366. /* get hardware configuration parameters */
  1367. if (autocfg) {
  1368. max_blk_size = dma_readl(dw, MAX_BLK_SIZE);
  1369. dw->nr_masters = (dw_params >> DW_PARAMS_NR_MASTER & 3) + 1;
  1370. for (i = 0; i < dw->nr_masters; i++) {
  1371. dw->data_width[i] =
  1372. (dw_params >> DW_PARAMS_DATA_WIDTH(i) & 3) + 2;
  1373. }
  1374. } else {
  1375. dw->nr_masters = pdata->nr_masters;
  1376. memcpy(dw->data_width, pdata->data_width, 4);
  1377. }
  1378. /* Get the base request line if set */
  1379. match = platform_get_device_id(pdev);
  1380. if (match)
  1381. dw->request_line_base = (unsigned int)match->driver_data;
  1382. /* Calculate all channel mask before DMA setup */
  1383. dw->all_chan_mask = (1 << nr_channels) - 1;
  1384. /* force dma off, just in case */
  1385. dw_dma_off(dw);
  1386. /* disable BLOCK interrupts as well */
  1387. channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
  1388. err = devm_request_irq(&pdev->dev, irq, dw_dma_interrupt, 0,
  1389. "dw_dmac", dw);
  1390. if (err)
  1391. return err;
  1392. platform_set_drvdata(pdev, dw);
  1393. /* create a pool of consistent memory blocks for hardware descriptors */
  1394. dw->desc_pool = dmam_pool_create("dw_dmac_desc_pool", &pdev->dev,
  1395. sizeof(struct dw_desc), 4, 0);
  1396. if (!dw->desc_pool) {
  1397. dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
  1398. return -ENOMEM;
  1399. }
  1400. tasklet_init(&dw->tasklet, dw_dma_tasklet, (unsigned long)dw);
  1401. INIT_LIST_HEAD(&dw->dma.channels);
  1402. for (i = 0; i < nr_channels; i++) {
  1403. struct dw_dma_chan *dwc = &dw->chan[i];
  1404. int r = nr_channels - i - 1;
  1405. dwc->chan.device = &dw->dma;
  1406. dma_cookie_init(&dwc->chan);
  1407. if (pdata->chan_allocation_order == CHAN_ALLOCATION_ASCENDING)
  1408. list_add_tail(&dwc->chan.device_node,
  1409. &dw->dma.channels);
  1410. else
  1411. list_add(&dwc->chan.device_node, &dw->dma.channels);
  1412. /* 7 is highest priority & 0 is lowest. */
  1413. if (pdata->chan_priority == CHAN_PRIORITY_ASCENDING)
  1414. dwc->priority = r;
  1415. else
  1416. dwc->priority = i;
  1417. dwc->ch_regs = &__dw_regs(dw)->CHAN[i];
  1418. spin_lock_init(&dwc->lock);
  1419. dwc->mask = 1 << i;
  1420. INIT_LIST_HEAD(&dwc->active_list);
  1421. INIT_LIST_HEAD(&dwc->queue);
  1422. INIT_LIST_HEAD(&dwc->free_list);
  1423. channel_clear_bit(dw, CH_EN, dwc->mask);
  1424. dwc->direction = DMA_TRANS_NONE;
  1425. /* hardware configuration */
  1426. if (autocfg) {
  1427. unsigned int dwc_params;
  1428. dwc_params = dma_read_byaddr(regs + r * sizeof(u32),
  1429. DWC_PARAMS);
  1430. dev_dbg(&pdev->dev, "DWC_PARAMS[%d]: 0x%08x\n", i,
  1431. dwc_params);
  1432. /* Decode maximum block size for given channel. The
  1433. * stored 4 bit value represents blocks from 0x00 for 3
  1434. * up to 0x0a for 4095. */
  1435. dwc->block_size =
  1436. (4 << ((max_blk_size >> 4 * i) & 0xf)) - 1;
  1437. dwc->nollp =
  1438. (dwc_params >> DWC_PARAMS_MBLK_EN & 0x1) == 0;
  1439. } else {
  1440. dwc->block_size = pdata->block_size;
  1441. /* Check if channel supports multi block transfer */
  1442. channel_writel(dwc, LLP, 0xfffffffc);
  1443. dwc->nollp =
  1444. (channel_readl(dwc, LLP) & 0xfffffffc) == 0;
  1445. channel_writel(dwc, LLP, 0);
  1446. }
  1447. }
  1448. /* Clear all interrupts on all channels. */
  1449. dma_writel(dw, CLEAR.XFER, dw->all_chan_mask);
  1450. dma_writel(dw, CLEAR.BLOCK, dw->all_chan_mask);
  1451. dma_writel(dw, CLEAR.SRC_TRAN, dw->all_chan_mask);
  1452. dma_writel(dw, CLEAR.DST_TRAN, dw->all_chan_mask);
  1453. dma_writel(dw, CLEAR.ERROR, dw->all_chan_mask);
  1454. dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
  1455. dma_cap_set(DMA_SLAVE, dw->dma.cap_mask);
  1456. if (pdata->is_private)
  1457. dma_cap_set(DMA_PRIVATE, dw->dma.cap_mask);
  1458. dw->dma.dev = &pdev->dev;
  1459. dw->dma.device_alloc_chan_resources = dwc_alloc_chan_resources;
  1460. dw->dma.device_free_chan_resources = dwc_free_chan_resources;
  1461. dw->dma.device_prep_dma_memcpy = dwc_prep_dma_memcpy;
  1462. dw->dma.device_prep_slave_sg = dwc_prep_slave_sg;
  1463. dw->dma.device_control = dwc_control;
  1464. dw->dma.device_tx_status = dwc_tx_status;
  1465. dw->dma.device_issue_pending = dwc_issue_pending;
  1466. dma_writel(dw, CFG, DW_CFG_DMA_EN);
  1467. dev_info(&pdev->dev, "DesignWare DMA Controller, %d channels\n",
  1468. nr_channels);
  1469. dma_async_device_register(&dw->dma);
  1470. if (pdev->dev.of_node) {
  1471. err = of_dma_controller_register(pdev->dev.of_node,
  1472. dw_dma_xlate, dw);
  1473. if (err && err != -ENODEV)
  1474. dev_err(&pdev->dev,
  1475. "could not register of_dma_controller\n");
  1476. }
  1477. return 0;
  1478. }
  1479. static int dw_remove(struct platform_device *pdev)
  1480. {
  1481. struct dw_dma *dw = platform_get_drvdata(pdev);
  1482. struct dw_dma_chan *dwc, *_dwc;
  1483. if (pdev->dev.of_node)
  1484. of_dma_controller_free(pdev->dev.of_node);
  1485. dw_dma_off(dw);
  1486. dma_async_device_unregister(&dw->dma);
  1487. tasklet_kill(&dw->tasklet);
  1488. list_for_each_entry_safe(dwc, _dwc, &dw->dma.channels,
  1489. chan.device_node) {
  1490. list_del(&dwc->chan.device_node);
  1491. channel_clear_bit(dw, CH_EN, dwc->mask);
  1492. }
  1493. return 0;
  1494. }
  1495. static void dw_shutdown(struct platform_device *pdev)
  1496. {
  1497. struct dw_dma *dw = platform_get_drvdata(pdev);
  1498. dw_dma_off(dw);
  1499. clk_disable_unprepare(dw->clk);
  1500. }
  1501. static int dw_suspend_noirq(struct device *dev)
  1502. {
  1503. struct platform_device *pdev = to_platform_device(dev);
  1504. struct dw_dma *dw = platform_get_drvdata(pdev);
  1505. dw_dma_off(dw);
  1506. clk_disable_unprepare(dw->clk);
  1507. return 0;
  1508. }
  1509. static int dw_resume_noirq(struct device *dev)
  1510. {
  1511. struct platform_device *pdev = to_platform_device(dev);
  1512. struct dw_dma *dw = platform_get_drvdata(pdev);
  1513. clk_prepare_enable(dw->clk);
  1514. dma_writel(dw, CFG, DW_CFG_DMA_EN);
  1515. return 0;
  1516. }
  1517. static const struct dev_pm_ops dw_dev_pm_ops = {
  1518. .suspend_noirq = dw_suspend_noirq,
  1519. .resume_noirq = dw_resume_noirq,
  1520. .freeze_noirq = dw_suspend_noirq,
  1521. .thaw_noirq = dw_resume_noirq,
  1522. .restore_noirq = dw_resume_noirq,
  1523. .poweroff_noirq = dw_suspend_noirq,
  1524. };
  1525. #ifdef CONFIG_OF
  1526. static const struct of_device_id dw_dma_id_table[] = {
  1527. { .compatible = "snps,dma-spear1340" },
  1528. {}
  1529. };
  1530. MODULE_DEVICE_TABLE(of, dw_dma_id_table);
  1531. #endif
  1532. static const struct platform_device_id dw_dma_ids[] = {
  1533. /* Name, Request Line Base */
  1534. { "INTL9C60", (kernel_ulong_t)16 },
  1535. { }
  1536. };
  1537. static struct platform_driver dw_driver = {
  1538. .probe = dw_probe,
  1539. .remove = dw_remove,
  1540. .shutdown = dw_shutdown,
  1541. .driver = {
  1542. .name = "dw_dmac",
  1543. .pm = &dw_dev_pm_ops,
  1544. .of_match_table = of_match_ptr(dw_dma_id_table),
  1545. },
  1546. .id_table = dw_dma_ids,
  1547. };
  1548. static int __init dw_init(void)
  1549. {
  1550. return platform_driver_register(&dw_driver);
  1551. }
  1552. subsys_initcall(dw_init);
  1553. static void __exit dw_exit(void)
  1554. {
  1555. platform_driver_unregister(&dw_driver);
  1556. }
  1557. module_exit(dw_exit);
  1558. MODULE_LICENSE("GPL v2");
  1559. MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller driver");
  1560. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  1561. MODULE_AUTHOR("Viresh Kumar <viresh.linux@gmail.com>");