at_hdmac.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770
  1. /*
  2. * Driver for the Atmel AHB DMA Controller (aka HDMA or DMAC on AT91 systems)
  3. *
  4. * Copyright (C) 2008 Atmel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. *
  12. * This supports the Atmel AHB DMA Controller found in several Atmel SoCs.
  13. * The only Atmel DMA Controller that is not covered by this driver is the one
  14. * found on AT91SAM9263.
  15. */
  16. #include <dt-bindings/dma/at91.h>
  17. #include <linux/clk.h>
  18. #include <linux/dmaengine.h>
  19. #include <linux/dma-mapping.h>
  20. #include <linux/dmapool.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/slab.h>
  25. #include <linux/of.h>
  26. #include <linux/of_device.h>
  27. #include <linux/of_dma.h>
  28. #include "at_hdmac_regs.h"
  29. #include "dmaengine.h"
  30. /*
  31. * Glossary
  32. * --------
  33. *
  34. * at_hdmac : Name of the ATmel AHB DMA Controller
  35. * at_dma_ / atdma : ATmel DMA controller entity related
  36. * atc_ / atchan : ATmel DMA Channel entity related
  37. */
  38. #define ATC_DEFAULT_CFG (ATC_FIFOCFG_HALFFIFO)
  39. #define ATC_DEFAULT_CTRLB (ATC_SIF(AT_DMA_MEM_IF) \
  40. |ATC_DIF(AT_DMA_MEM_IF))
  41. #define ATC_DMA_BUSWIDTHS\
  42. (BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) |\
  43. BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |\
  44. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |\
  45. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
  46. /*
  47. * Initial number of descriptors to allocate for each channel. This could
  48. * be increased during dma usage.
  49. */
  50. static unsigned int init_nr_desc_per_channel = 64;
  51. module_param(init_nr_desc_per_channel, uint, 0644);
  52. MODULE_PARM_DESC(init_nr_desc_per_channel,
  53. "initial descriptors per channel (default: 64)");
  54. /* prototypes */
  55. static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx);
  56. static void atc_issue_pending(struct dma_chan *chan);
  57. /*----------------------------------------------------------------------*/
  58. static struct at_desc *atc_first_active(struct at_dma_chan *atchan)
  59. {
  60. return list_first_entry(&atchan->active_list,
  61. struct at_desc, desc_node);
  62. }
  63. static struct at_desc *atc_first_queued(struct at_dma_chan *atchan)
  64. {
  65. return list_first_entry(&atchan->queue,
  66. struct at_desc, desc_node);
  67. }
  68. /**
  69. * atc_alloc_descriptor - allocate and return an initialized descriptor
  70. * @chan: the channel to allocate descriptors for
  71. * @gfp_flags: GFP allocation flags
  72. *
  73. * Note: The ack-bit is positioned in the descriptor flag at creation time
  74. * to make initial allocation more convenient. This bit will be cleared
  75. * and control will be given to client at usage time (during
  76. * preparation functions).
  77. */
  78. static struct at_desc *atc_alloc_descriptor(struct dma_chan *chan,
  79. gfp_t gfp_flags)
  80. {
  81. struct at_desc *desc = NULL;
  82. struct at_dma *atdma = to_at_dma(chan->device);
  83. dma_addr_t phys;
  84. desc = dma_pool_alloc(atdma->dma_desc_pool, gfp_flags, &phys);
  85. if (desc) {
  86. memset(desc, 0, sizeof(struct at_desc));
  87. INIT_LIST_HEAD(&desc->tx_list);
  88. dma_async_tx_descriptor_init(&desc->txd, chan);
  89. /* txd.flags will be overwritten in prep functions */
  90. desc->txd.flags = DMA_CTRL_ACK;
  91. desc->txd.tx_submit = atc_tx_submit;
  92. desc->txd.phys = phys;
  93. }
  94. return desc;
  95. }
  96. /**
  97. * atc_desc_get - get an unused descriptor from free_list
  98. * @atchan: channel we want a new descriptor for
  99. */
  100. static struct at_desc *atc_desc_get(struct at_dma_chan *atchan)
  101. {
  102. struct at_desc *desc, *_desc;
  103. struct at_desc *ret = NULL;
  104. unsigned long flags;
  105. unsigned int i = 0;
  106. LIST_HEAD(tmp_list);
  107. spin_lock_irqsave(&atchan->lock, flags);
  108. list_for_each_entry_safe(desc, _desc, &atchan->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(&atchan->chan_common),
  116. "desc %p not ACKed\n", desc);
  117. }
  118. spin_unlock_irqrestore(&atchan->lock, flags);
  119. dev_vdbg(chan2dev(&atchan->chan_common),
  120. "scanned %u descriptors on freelist\n", i);
  121. /* no more descriptor available in initial pool: create one more */
  122. if (!ret) {
  123. ret = atc_alloc_descriptor(&atchan->chan_common, GFP_ATOMIC);
  124. if (ret) {
  125. spin_lock_irqsave(&atchan->lock, flags);
  126. atchan->descs_allocated++;
  127. spin_unlock_irqrestore(&atchan->lock, flags);
  128. } else {
  129. dev_err(chan2dev(&atchan->chan_common),
  130. "not enough descriptors available\n");
  131. }
  132. }
  133. return ret;
  134. }
  135. /**
  136. * atc_desc_put - move a descriptor, including any children, to the free list
  137. * @atchan: channel we work on
  138. * @desc: descriptor, at the head of a chain, to move to free list
  139. */
  140. static void atc_desc_put(struct at_dma_chan *atchan, struct at_desc *desc)
  141. {
  142. if (desc) {
  143. struct at_desc *child;
  144. unsigned long flags;
  145. spin_lock_irqsave(&atchan->lock, flags);
  146. list_for_each_entry(child, &desc->tx_list, desc_node)
  147. dev_vdbg(chan2dev(&atchan->chan_common),
  148. "moving child desc %p to freelist\n",
  149. child);
  150. list_splice_init(&desc->tx_list, &atchan->free_list);
  151. dev_vdbg(chan2dev(&atchan->chan_common),
  152. "moving desc %p to freelist\n", desc);
  153. list_add(&desc->desc_node, &atchan->free_list);
  154. spin_unlock_irqrestore(&atchan->lock, flags);
  155. }
  156. }
  157. /**
  158. * atc_desc_chain - build chain adding a descriptor
  159. * @first: address of first descriptor of the chain
  160. * @prev: address of previous descriptor of the chain
  161. * @desc: descriptor to queue
  162. *
  163. * Called from prep_* functions
  164. */
  165. static void atc_desc_chain(struct at_desc **first, struct at_desc **prev,
  166. struct at_desc *desc)
  167. {
  168. if (!(*first)) {
  169. *first = desc;
  170. } else {
  171. /* inform the HW lli about chaining */
  172. (*prev)->lli.dscr = desc->txd.phys;
  173. /* insert the link descriptor to the LD ring */
  174. list_add_tail(&desc->desc_node,
  175. &(*first)->tx_list);
  176. }
  177. *prev = desc;
  178. }
  179. /**
  180. * atc_dostart - starts the DMA engine for real
  181. * @atchan: the channel we want to start
  182. * @first: first descriptor in the list we want to begin with
  183. *
  184. * Called with atchan->lock held and bh disabled
  185. */
  186. static void atc_dostart(struct at_dma_chan *atchan, struct at_desc *first)
  187. {
  188. struct at_dma *atdma = to_at_dma(atchan->chan_common.device);
  189. /* ASSERT: channel is idle */
  190. if (atc_chan_is_enabled(atchan)) {
  191. dev_err(chan2dev(&atchan->chan_common),
  192. "BUG: Attempted to start non-idle channel\n");
  193. dev_err(chan2dev(&atchan->chan_common),
  194. " channel: s0x%x d0x%x ctrl0x%x:0x%x l0x%x\n",
  195. channel_readl(atchan, SADDR),
  196. channel_readl(atchan, DADDR),
  197. channel_readl(atchan, CTRLA),
  198. channel_readl(atchan, CTRLB),
  199. channel_readl(atchan, DSCR));
  200. /* The tasklet will hopefully advance the queue... */
  201. return;
  202. }
  203. vdbg_dump_regs(atchan);
  204. channel_writel(atchan, SADDR, 0);
  205. channel_writel(atchan, DADDR, 0);
  206. channel_writel(atchan, CTRLA, 0);
  207. channel_writel(atchan, CTRLB, 0);
  208. channel_writel(atchan, DSCR, first->txd.phys);
  209. dma_writel(atdma, CHER, atchan->mask);
  210. vdbg_dump_regs(atchan);
  211. }
  212. /*
  213. * atc_get_current_descriptors -
  214. * locate the descriptor which equal to physical address in DSCR
  215. * @atchan: the channel we want to start
  216. * @dscr_addr: physical descriptor address in DSCR
  217. */
  218. static struct at_desc *atc_get_current_descriptors(struct at_dma_chan *atchan,
  219. u32 dscr_addr)
  220. {
  221. struct at_desc *desc, *_desc, *child, *desc_cur = NULL;
  222. list_for_each_entry_safe(desc, _desc, &atchan->active_list, desc_node) {
  223. if (desc->lli.dscr == dscr_addr) {
  224. desc_cur = desc;
  225. break;
  226. }
  227. list_for_each_entry(child, &desc->tx_list, desc_node) {
  228. if (child->lli.dscr == dscr_addr) {
  229. desc_cur = child;
  230. break;
  231. }
  232. }
  233. }
  234. return desc_cur;
  235. }
  236. /*
  237. * atc_get_bytes_left -
  238. * Get the number of bytes residue in dma buffer,
  239. * @chan: the channel we want to start
  240. */
  241. static int atc_get_bytes_left(struct dma_chan *chan)
  242. {
  243. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  244. struct at_dma *atdma = to_at_dma(chan->device);
  245. int chan_id = atchan->chan_common.chan_id;
  246. struct at_desc *desc_first = atc_first_active(atchan);
  247. struct at_desc *desc_cur;
  248. int ret = 0, count = 0;
  249. /*
  250. * Initialize necessary values in the first time.
  251. * remain_desc record remain desc length.
  252. */
  253. if (atchan->remain_desc == 0)
  254. /* First descriptor embedds the transaction length */
  255. atchan->remain_desc = desc_first->len;
  256. /*
  257. * This happens when current descriptor transfer complete.
  258. * The residual buffer size should reduce current descriptor length.
  259. */
  260. if (unlikely(test_bit(ATC_IS_BTC, &atchan->status))) {
  261. clear_bit(ATC_IS_BTC, &atchan->status);
  262. desc_cur = atc_get_current_descriptors(atchan,
  263. channel_readl(atchan, DSCR));
  264. if (!desc_cur) {
  265. ret = -EINVAL;
  266. goto out;
  267. }
  268. count = (desc_cur->lli.ctrla & ATC_BTSIZE_MAX)
  269. << desc_first->tx_width;
  270. if (atchan->remain_desc < count) {
  271. ret = -EINVAL;
  272. goto out;
  273. }
  274. atchan->remain_desc -= count;
  275. ret = atchan->remain_desc;
  276. } else {
  277. /*
  278. * Get residual bytes when current
  279. * descriptor transfer in progress.
  280. */
  281. count = (channel_readl(atchan, CTRLA) & ATC_BTSIZE_MAX)
  282. << (desc_first->tx_width);
  283. ret = atchan->remain_desc - count;
  284. }
  285. /*
  286. * Check fifo empty.
  287. */
  288. if (!(dma_readl(atdma, CHSR) & AT_DMA_EMPT(chan_id)))
  289. atc_issue_pending(chan);
  290. out:
  291. return ret;
  292. }
  293. /**
  294. * atc_chain_complete - finish work for one transaction chain
  295. * @atchan: channel we work on
  296. * @desc: descriptor at the head of the chain we want do complete
  297. *
  298. * Called with atchan->lock held and bh disabled */
  299. static void
  300. atc_chain_complete(struct at_dma_chan *atchan, struct at_desc *desc)
  301. {
  302. struct dma_async_tx_descriptor *txd = &desc->txd;
  303. dev_vdbg(chan2dev(&atchan->chan_common),
  304. "descriptor %u complete\n", txd->cookie);
  305. /* mark the descriptor as complete for non cyclic cases only */
  306. if (!atc_chan_is_cyclic(atchan))
  307. dma_cookie_complete(txd);
  308. /* move children to free_list */
  309. list_splice_init(&desc->tx_list, &atchan->free_list);
  310. /* move myself to free_list */
  311. list_move(&desc->desc_node, &atchan->free_list);
  312. dma_descriptor_unmap(txd);
  313. /* for cyclic transfers,
  314. * no need to replay callback function while stopping */
  315. if (!atc_chan_is_cyclic(atchan)) {
  316. dma_async_tx_callback callback = txd->callback;
  317. void *param = txd->callback_param;
  318. /*
  319. * The API requires that no submissions are done from a
  320. * callback, so we don't need to drop the lock here
  321. */
  322. if (callback)
  323. callback(param);
  324. }
  325. dma_run_dependencies(txd);
  326. }
  327. /**
  328. * atc_complete_all - finish work for all transactions
  329. * @atchan: channel to complete transactions for
  330. *
  331. * Eventually submit queued descriptors if any
  332. *
  333. * Assume channel is idle while calling this function
  334. * Called with atchan->lock held and bh disabled
  335. */
  336. static void atc_complete_all(struct at_dma_chan *atchan)
  337. {
  338. struct at_desc *desc, *_desc;
  339. LIST_HEAD(list);
  340. dev_vdbg(chan2dev(&atchan->chan_common), "complete all\n");
  341. /*
  342. * Submit queued descriptors ASAP, i.e. before we go through
  343. * the completed ones.
  344. */
  345. if (!list_empty(&atchan->queue))
  346. atc_dostart(atchan, atc_first_queued(atchan));
  347. /* empty active_list now it is completed */
  348. list_splice_init(&atchan->active_list, &list);
  349. /* empty queue list by moving descriptors (if any) to active_list */
  350. list_splice_init(&atchan->queue, &atchan->active_list);
  351. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  352. atc_chain_complete(atchan, desc);
  353. }
  354. /**
  355. * atc_advance_work - at the end of a transaction, move forward
  356. * @atchan: channel where the transaction ended
  357. *
  358. * Called with atchan->lock held and bh disabled
  359. */
  360. static void atc_advance_work(struct at_dma_chan *atchan)
  361. {
  362. dev_vdbg(chan2dev(&atchan->chan_common), "advance_work\n");
  363. if (atc_chan_is_enabled(atchan))
  364. return;
  365. if (list_empty(&atchan->active_list) ||
  366. list_is_singular(&atchan->active_list)) {
  367. atc_complete_all(atchan);
  368. } else {
  369. atc_chain_complete(atchan, atc_first_active(atchan));
  370. /* advance work */
  371. atc_dostart(atchan, atc_first_active(atchan));
  372. }
  373. }
  374. /**
  375. * atc_handle_error - handle errors reported by DMA controller
  376. * @atchan: channel where error occurs
  377. *
  378. * Called with atchan->lock held and bh disabled
  379. */
  380. static void atc_handle_error(struct at_dma_chan *atchan)
  381. {
  382. struct at_desc *bad_desc;
  383. struct at_desc *child;
  384. /*
  385. * The descriptor currently at the head of the active list is
  386. * broked. Since we don't have any way to report errors, we'll
  387. * just have to scream loudly and try to carry on.
  388. */
  389. bad_desc = atc_first_active(atchan);
  390. list_del_init(&bad_desc->desc_node);
  391. /* As we are stopped, take advantage to push queued descriptors
  392. * in active_list */
  393. list_splice_init(&atchan->queue, atchan->active_list.prev);
  394. /* Try to restart the controller */
  395. if (!list_empty(&atchan->active_list))
  396. atc_dostart(atchan, atc_first_active(atchan));
  397. /*
  398. * KERN_CRITICAL may seem harsh, but since this only happens
  399. * when someone submits a bad physical address in a
  400. * descriptor, we should consider ourselves lucky that the
  401. * controller flagged an error instead of scribbling over
  402. * random memory locations.
  403. */
  404. dev_crit(chan2dev(&atchan->chan_common),
  405. "Bad descriptor submitted for DMA!\n");
  406. dev_crit(chan2dev(&atchan->chan_common),
  407. " cookie: %d\n", bad_desc->txd.cookie);
  408. atc_dump_lli(atchan, &bad_desc->lli);
  409. list_for_each_entry(child, &bad_desc->tx_list, desc_node)
  410. atc_dump_lli(atchan, &child->lli);
  411. /* Pretend the descriptor completed successfully */
  412. atc_chain_complete(atchan, bad_desc);
  413. }
  414. /**
  415. * atc_handle_cyclic - at the end of a period, run callback function
  416. * @atchan: channel used for cyclic operations
  417. *
  418. * Called with atchan->lock held and bh disabled
  419. */
  420. static void atc_handle_cyclic(struct at_dma_chan *atchan)
  421. {
  422. struct at_desc *first = atc_first_active(atchan);
  423. struct dma_async_tx_descriptor *txd = &first->txd;
  424. dma_async_tx_callback callback = txd->callback;
  425. void *param = txd->callback_param;
  426. dev_vdbg(chan2dev(&atchan->chan_common),
  427. "new cyclic period llp 0x%08x\n",
  428. channel_readl(atchan, DSCR));
  429. if (callback)
  430. callback(param);
  431. }
  432. /*-- IRQ & Tasklet ---------------------------------------------------*/
  433. static void atc_tasklet(unsigned long data)
  434. {
  435. struct at_dma_chan *atchan = (struct at_dma_chan *)data;
  436. unsigned long flags;
  437. spin_lock_irqsave(&atchan->lock, flags);
  438. if (test_and_clear_bit(ATC_IS_ERROR, &atchan->status))
  439. atc_handle_error(atchan);
  440. else if (atc_chan_is_cyclic(atchan))
  441. atc_handle_cyclic(atchan);
  442. else
  443. atc_advance_work(atchan);
  444. spin_unlock_irqrestore(&atchan->lock, flags);
  445. }
  446. static irqreturn_t at_dma_interrupt(int irq, void *dev_id)
  447. {
  448. struct at_dma *atdma = (struct at_dma *)dev_id;
  449. struct at_dma_chan *atchan;
  450. int i;
  451. u32 status, pending, imr;
  452. int ret = IRQ_NONE;
  453. do {
  454. imr = dma_readl(atdma, EBCIMR);
  455. status = dma_readl(atdma, EBCISR);
  456. pending = status & imr;
  457. if (!pending)
  458. break;
  459. dev_vdbg(atdma->dma_common.dev,
  460. "interrupt: status = 0x%08x, 0x%08x, 0x%08x\n",
  461. status, imr, pending);
  462. for (i = 0; i < atdma->dma_common.chancnt; i++) {
  463. atchan = &atdma->chan[i];
  464. if (pending & (AT_DMA_BTC(i) | AT_DMA_ERR(i))) {
  465. if (pending & AT_DMA_ERR(i)) {
  466. /* Disable channel on AHB error */
  467. dma_writel(atdma, CHDR,
  468. AT_DMA_RES(i) | atchan->mask);
  469. /* Give information to tasklet */
  470. set_bit(ATC_IS_ERROR, &atchan->status);
  471. }
  472. if (pending & AT_DMA_BTC(i))
  473. set_bit(ATC_IS_BTC, &atchan->status);
  474. tasklet_schedule(&atchan->tasklet);
  475. ret = IRQ_HANDLED;
  476. }
  477. }
  478. } while (pending);
  479. return ret;
  480. }
  481. /*-- DMA Engine API --------------------------------------------------*/
  482. /**
  483. * atc_tx_submit - set the prepared descriptor(s) to be executed by the engine
  484. * @desc: descriptor at the head of the transaction chain
  485. *
  486. * Queue chain if DMA engine is working already
  487. *
  488. * Cookie increment and adding to active_list or queue must be atomic
  489. */
  490. static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx)
  491. {
  492. struct at_desc *desc = txd_to_at_desc(tx);
  493. struct at_dma_chan *atchan = to_at_dma_chan(tx->chan);
  494. dma_cookie_t cookie;
  495. unsigned long flags;
  496. spin_lock_irqsave(&atchan->lock, flags);
  497. cookie = dma_cookie_assign(tx);
  498. if (list_empty(&atchan->active_list)) {
  499. dev_vdbg(chan2dev(tx->chan), "tx_submit: started %u\n",
  500. desc->txd.cookie);
  501. atc_dostart(atchan, desc);
  502. list_add_tail(&desc->desc_node, &atchan->active_list);
  503. } else {
  504. dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u\n",
  505. desc->txd.cookie);
  506. list_add_tail(&desc->desc_node, &atchan->queue);
  507. }
  508. spin_unlock_irqrestore(&atchan->lock, flags);
  509. return cookie;
  510. }
  511. /**
  512. * atc_prep_dma_memcpy - prepare a memcpy operation
  513. * @chan: the channel to prepare operation on
  514. * @dest: operation virtual destination address
  515. * @src: operation virtual source address
  516. * @len: operation length
  517. * @flags: tx descriptor status flags
  518. */
  519. static struct dma_async_tx_descriptor *
  520. atc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  521. size_t len, unsigned long flags)
  522. {
  523. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  524. struct at_desc *desc = NULL;
  525. struct at_desc *first = NULL;
  526. struct at_desc *prev = NULL;
  527. size_t xfer_count;
  528. size_t offset;
  529. unsigned int src_width;
  530. unsigned int dst_width;
  531. u32 ctrla;
  532. u32 ctrlb;
  533. dev_vdbg(chan2dev(chan), "prep_dma_memcpy: d0x%x s0x%x l0x%zx f0x%lx\n",
  534. dest, src, len, flags);
  535. if (unlikely(!len)) {
  536. dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
  537. return NULL;
  538. }
  539. ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN
  540. | ATC_SRC_ADDR_MODE_INCR
  541. | ATC_DST_ADDR_MODE_INCR
  542. | ATC_FC_MEM2MEM;
  543. /*
  544. * We can be a lot more clever here, but this should take care
  545. * of the most common optimization.
  546. */
  547. if (!((src | dest | len) & 3)) {
  548. ctrla = ATC_SRC_WIDTH_WORD | ATC_DST_WIDTH_WORD;
  549. src_width = dst_width = 2;
  550. } else if (!((src | dest | len) & 1)) {
  551. ctrla = ATC_SRC_WIDTH_HALFWORD | ATC_DST_WIDTH_HALFWORD;
  552. src_width = dst_width = 1;
  553. } else {
  554. ctrla = ATC_SRC_WIDTH_BYTE | ATC_DST_WIDTH_BYTE;
  555. src_width = dst_width = 0;
  556. }
  557. for (offset = 0; offset < len; offset += xfer_count << src_width) {
  558. xfer_count = min_t(size_t, (len - offset) >> src_width,
  559. ATC_BTSIZE_MAX);
  560. desc = atc_desc_get(atchan);
  561. if (!desc)
  562. goto err_desc_get;
  563. desc->lli.saddr = src + offset;
  564. desc->lli.daddr = dest + offset;
  565. desc->lli.ctrla = ctrla | xfer_count;
  566. desc->lli.ctrlb = ctrlb;
  567. desc->txd.cookie = 0;
  568. atc_desc_chain(&first, &prev, desc);
  569. }
  570. /* First descriptor of the chain embedds additional information */
  571. first->txd.cookie = -EBUSY;
  572. first->len = len;
  573. first->tx_width = src_width;
  574. /* set end-of-link to the last link descriptor of list*/
  575. set_desc_eol(desc);
  576. first->txd.flags = flags; /* client is in control of this ack */
  577. return &first->txd;
  578. err_desc_get:
  579. atc_desc_put(atchan, first);
  580. return NULL;
  581. }
  582. /**
  583. * atc_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
  584. * @chan: DMA channel
  585. * @sgl: scatterlist to transfer to/from
  586. * @sg_len: number of entries in @scatterlist
  587. * @direction: DMA direction
  588. * @flags: tx descriptor status flags
  589. * @context: transaction context (ignored)
  590. */
  591. static struct dma_async_tx_descriptor *
  592. atc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  593. unsigned int sg_len, enum dma_transfer_direction direction,
  594. unsigned long flags, void *context)
  595. {
  596. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  597. struct at_dma_slave *atslave = chan->private;
  598. struct dma_slave_config *sconfig = &atchan->dma_sconfig;
  599. struct at_desc *first = NULL;
  600. struct at_desc *prev = NULL;
  601. u32 ctrla;
  602. u32 ctrlb;
  603. dma_addr_t reg;
  604. unsigned int reg_width;
  605. unsigned int mem_width;
  606. unsigned int i;
  607. struct scatterlist *sg;
  608. size_t total_len = 0;
  609. dev_vdbg(chan2dev(chan), "prep_slave_sg (%d): %s f0x%lx\n",
  610. sg_len,
  611. direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE",
  612. flags);
  613. if (unlikely(!atslave || !sg_len)) {
  614. dev_dbg(chan2dev(chan), "prep_slave_sg: sg length is zero!\n");
  615. return NULL;
  616. }
  617. ctrla = ATC_SCSIZE(sconfig->src_maxburst)
  618. | ATC_DCSIZE(sconfig->dst_maxburst);
  619. ctrlb = ATC_IEN;
  620. switch (direction) {
  621. case DMA_MEM_TO_DEV:
  622. reg_width = convert_buswidth(sconfig->dst_addr_width);
  623. ctrla |= ATC_DST_WIDTH(reg_width);
  624. ctrlb |= ATC_DST_ADDR_MODE_FIXED
  625. | ATC_SRC_ADDR_MODE_INCR
  626. | ATC_FC_MEM2PER
  627. | ATC_SIF(atchan->mem_if) | ATC_DIF(atchan->per_if);
  628. reg = sconfig->dst_addr;
  629. for_each_sg(sgl, sg, sg_len, i) {
  630. struct at_desc *desc;
  631. u32 len;
  632. u32 mem;
  633. desc = atc_desc_get(atchan);
  634. if (!desc)
  635. goto err_desc_get;
  636. mem = sg_dma_address(sg);
  637. len = sg_dma_len(sg);
  638. if (unlikely(!len)) {
  639. dev_dbg(chan2dev(chan),
  640. "prep_slave_sg: sg(%d) data length is zero\n", i);
  641. goto err;
  642. }
  643. mem_width = 2;
  644. if (unlikely(mem & 3 || len & 3))
  645. mem_width = 0;
  646. desc->lli.saddr = mem;
  647. desc->lli.daddr = reg;
  648. desc->lli.ctrla = ctrla
  649. | ATC_SRC_WIDTH(mem_width)
  650. | len >> mem_width;
  651. desc->lli.ctrlb = ctrlb;
  652. atc_desc_chain(&first, &prev, desc);
  653. total_len += len;
  654. }
  655. break;
  656. case DMA_DEV_TO_MEM:
  657. reg_width = convert_buswidth(sconfig->src_addr_width);
  658. ctrla |= ATC_SRC_WIDTH(reg_width);
  659. ctrlb |= ATC_DST_ADDR_MODE_INCR
  660. | ATC_SRC_ADDR_MODE_FIXED
  661. | ATC_FC_PER2MEM
  662. | ATC_SIF(atchan->per_if) | ATC_DIF(atchan->mem_if);
  663. reg = sconfig->src_addr;
  664. for_each_sg(sgl, sg, sg_len, i) {
  665. struct at_desc *desc;
  666. u32 len;
  667. u32 mem;
  668. desc = atc_desc_get(atchan);
  669. if (!desc)
  670. goto err_desc_get;
  671. mem = sg_dma_address(sg);
  672. len = sg_dma_len(sg);
  673. if (unlikely(!len)) {
  674. dev_dbg(chan2dev(chan),
  675. "prep_slave_sg: sg(%d) data length is zero\n", i);
  676. goto err;
  677. }
  678. mem_width = 2;
  679. if (unlikely(mem & 3 || len & 3))
  680. mem_width = 0;
  681. desc->lli.saddr = reg;
  682. desc->lli.daddr = mem;
  683. desc->lli.ctrla = ctrla
  684. | ATC_DST_WIDTH(mem_width)
  685. | len >> reg_width;
  686. desc->lli.ctrlb = ctrlb;
  687. atc_desc_chain(&first, &prev, desc);
  688. total_len += len;
  689. }
  690. break;
  691. default:
  692. return NULL;
  693. }
  694. /* set end-of-link to the last link descriptor of list*/
  695. set_desc_eol(prev);
  696. /* First descriptor of the chain embedds additional information */
  697. first->txd.cookie = -EBUSY;
  698. first->len = total_len;
  699. first->tx_width = reg_width;
  700. /* first link descriptor of list is responsible of flags */
  701. first->txd.flags = flags; /* client is in control of this ack */
  702. return &first->txd;
  703. err_desc_get:
  704. dev_err(chan2dev(chan), "not enough descriptors available\n");
  705. err:
  706. atc_desc_put(atchan, first);
  707. return NULL;
  708. }
  709. /**
  710. * atc_dma_cyclic_check_values
  711. * Check for too big/unaligned periods and unaligned DMA buffer
  712. */
  713. static int
  714. atc_dma_cyclic_check_values(unsigned int reg_width, dma_addr_t buf_addr,
  715. size_t period_len)
  716. {
  717. if (period_len > (ATC_BTSIZE_MAX << reg_width))
  718. goto err_out;
  719. if (unlikely(period_len & ((1 << reg_width) - 1)))
  720. goto err_out;
  721. if (unlikely(buf_addr & ((1 << reg_width) - 1)))
  722. goto err_out;
  723. return 0;
  724. err_out:
  725. return -EINVAL;
  726. }
  727. /**
  728. * atc_dma_cyclic_fill_desc - Fill one period descriptor
  729. */
  730. static int
  731. atc_dma_cyclic_fill_desc(struct dma_chan *chan, struct at_desc *desc,
  732. unsigned int period_index, dma_addr_t buf_addr,
  733. unsigned int reg_width, size_t period_len,
  734. enum dma_transfer_direction direction)
  735. {
  736. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  737. struct dma_slave_config *sconfig = &atchan->dma_sconfig;
  738. u32 ctrla;
  739. /* prepare common CRTLA value */
  740. ctrla = ATC_SCSIZE(sconfig->src_maxburst)
  741. | ATC_DCSIZE(sconfig->dst_maxburst)
  742. | ATC_DST_WIDTH(reg_width)
  743. | ATC_SRC_WIDTH(reg_width)
  744. | period_len >> reg_width;
  745. switch (direction) {
  746. case DMA_MEM_TO_DEV:
  747. desc->lli.saddr = buf_addr + (period_len * period_index);
  748. desc->lli.daddr = sconfig->dst_addr;
  749. desc->lli.ctrla = ctrla;
  750. desc->lli.ctrlb = ATC_DST_ADDR_MODE_FIXED
  751. | ATC_SRC_ADDR_MODE_INCR
  752. | ATC_FC_MEM2PER
  753. | ATC_SIF(atchan->mem_if)
  754. | ATC_DIF(atchan->per_if);
  755. break;
  756. case DMA_DEV_TO_MEM:
  757. desc->lli.saddr = sconfig->src_addr;
  758. desc->lli.daddr = buf_addr + (period_len * period_index);
  759. desc->lli.ctrla = ctrla;
  760. desc->lli.ctrlb = ATC_DST_ADDR_MODE_INCR
  761. | ATC_SRC_ADDR_MODE_FIXED
  762. | ATC_FC_PER2MEM
  763. | ATC_SIF(atchan->per_if)
  764. | ATC_DIF(atchan->mem_if);
  765. break;
  766. default:
  767. return -EINVAL;
  768. }
  769. return 0;
  770. }
  771. /**
  772. * atc_prep_dma_cyclic - prepare the cyclic DMA transfer
  773. * @chan: the DMA channel to prepare
  774. * @buf_addr: physical DMA address where the buffer starts
  775. * @buf_len: total number of bytes for the entire buffer
  776. * @period_len: number of bytes for each period
  777. * @direction: transfer direction, to or from device
  778. * @flags: tx descriptor status flags
  779. */
  780. static struct dma_async_tx_descriptor *
  781. atc_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  782. size_t period_len, enum dma_transfer_direction direction,
  783. unsigned long flags)
  784. {
  785. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  786. struct at_dma_slave *atslave = chan->private;
  787. struct dma_slave_config *sconfig = &atchan->dma_sconfig;
  788. struct at_desc *first = NULL;
  789. struct at_desc *prev = NULL;
  790. unsigned long was_cyclic;
  791. unsigned int reg_width;
  792. unsigned int periods = buf_len / period_len;
  793. unsigned int i;
  794. dev_vdbg(chan2dev(chan), "prep_dma_cyclic: %s buf@0x%08x - %d (%d/%d)\n",
  795. direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE",
  796. buf_addr,
  797. periods, buf_len, period_len);
  798. if (unlikely(!atslave || !buf_len || !period_len)) {
  799. dev_dbg(chan2dev(chan), "prep_dma_cyclic: length is zero!\n");
  800. return NULL;
  801. }
  802. was_cyclic = test_and_set_bit(ATC_IS_CYCLIC, &atchan->status);
  803. if (was_cyclic) {
  804. dev_dbg(chan2dev(chan), "prep_dma_cyclic: channel in use!\n");
  805. return NULL;
  806. }
  807. if (unlikely(!is_slave_direction(direction)))
  808. goto err_out;
  809. if (sconfig->direction == DMA_MEM_TO_DEV)
  810. reg_width = convert_buswidth(sconfig->dst_addr_width);
  811. else
  812. reg_width = convert_buswidth(sconfig->src_addr_width);
  813. /* Check for too big/unaligned periods and unaligned DMA buffer */
  814. if (atc_dma_cyclic_check_values(reg_width, buf_addr, period_len))
  815. goto err_out;
  816. /* build cyclic linked list */
  817. for (i = 0; i < periods; i++) {
  818. struct at_desc *desc;
  819. desc = atc_desc_get(atchan);
  820. if (!desc)
  821. goto err_desc_get;
  822. if (atc_dma_cyclic_fill_desc(chan, desc, i, buf_addr,
  823. reg_width, period_len, direction))
  824. goto err_desc_get;
  825. atc_desc_chain(&first, &prev, desc);
  826. }
  827. /* lets make a cyclic list */
  828. prev->lli.dscr = first->txd.phys;
  829. /* First descriptor of the chain embedds additional information */
  830. first->txd.cookie = -EBUSY;
  831. first->len = buf_len;
  832. first->tx_width = reg_width;
  833. return &first->txd;
  834. err_desc_get:
  835. dev_err(chan2dev(chan), "not enough descriptors available\n");
  836. atc_desc_put(atchan, first);
  837. err_out:
  838. clear_bit(ATC_IS_CYCLIC, &atchan->status);
  839. return NULL;
  840. }
  841. static int atc_config(struct dma_chan *chan,
  842. struct dma_slave_config *sconfig)
  843. {
  844. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  845. dev_vdbg(chan2dev(chan), "%s\n", __func__);
  846. /* Check if it is chan is configured for slave transfers */
  847. if (!chan->private)
  848. return -EINVAL;
  849. memcpy(&atchan->dma_sconfig, sconfig, sizeof(*sconfig));
  850. convert_burst(&atchan->dma_sconfig.src_maxburst);
  851. convert_burst(&atchan->dma_sconfig.dst_maxburst);
  852. return 0;
  853. }
  854. static int atc_pause(struct dma_chan *chan)
  855. {
  856. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  857. struct at_dma *atdma = to_at_dma(chan->device);
  858. int chan_id = atchan->chan_common.chan_id;
  859. unsigned long flags;
  860. LIST_HEAD(list);
  861. dev_vdbg(chan2dev(chan), "%s\n", __func__);
  862. spin_lock_irqsave(&atchan->lock, flags);
  863. dma_writel(atdma, CHER, AT_DMA_SUSP(chan_id));
  864. set_bit(ATC_IS_PAUSED, &atchan->status);
  865. spin_unlock_irqrestore(&atchan->lock, flags);
  866. return 0;
  867. }
  868. static int atc_resume(struct dma_chan *chan)
  869. {
  870. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  871. struct at_dma *atdma = to_at_dma(chan->device);
  872. int chan_id = atchan->chan_common.chan_id;
  873. unsigned long flags;
  874. LIST_HEAD(list);
  875. dev_vdbg(chan2dev(chan), "%s\n", __func__);
  876. if (!atc_chan_is_paused(atchan))
  877. return 0;
  878. spin_lock_irqsave(&atchan->lock, flags);
  879. dma_writel(atdma, CHDR, AT_DMA_RES(chan_id));
  880. clear_bit(ATC_IS_PAUSED, &atchan->status);
  881. spin_unlock_irqrestore(&atchan->lock, flags);
  882. return 0;
  883. }
  884. static int atc_terminate_all(struct dma_chan *chan)
  885. {
  886. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  887. struct at_dma *atdma = to_at_dma(chan->device);
  888. int chan_id = atchan->chan_common.chan_id;
  889. struct at_desc *desc, *_desc;
  890. unsigned long flags;
  891. LIST_HEAD(list);
  892. dev_vdbg(chan2dev(chan), "%s\n", __func__);
  893. /*
  894. * This is only called when something went wrong elsewhere, so
  895. * we don't really care about the data. Just disable the
  896. * channel. We still have to poll the channel enable bit due
  897. * to AHB/HSB limitations.
  898. */
  899. spin_lock_irqsave(&atchan->lock, flags);
  900. /* disabling channel: must also remove suspend state */
  901. dma_writel(atdma, CHDR, AT_DMA_RES(chan_id) | atchan->mask);
  902. /* confirm that this channel is disabled */
  903. while (dma_readl(atdma, CHSR) & atchan->mask)
  904. cpu_relax();
  905. /* active_list entries will end up before queued entries */
  906. list_splice_init(&atchan->queue, &list);
  907. list_splice_init(&atchan->active_list, &list);
  908. /* Flush all pending and queued descriptors */
  909. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  910. atc_chain_complete(atchan, desc);
  911. clear_bit(ATC_IS_PAUSED, &atchan->status);
  912. /* if channel dedicated to cyclic operations, free it */
  913. clear_bit(ATC_IS_CYCLIC, &atchan->status);
  914. spin_unlock_irqrestore(&atchan->lock, flags);
  915. return 0;
  916. }
  917. /**
  918. * atc_tx_status - poll for transaction completion
  919. * @chan: DMA channel
  920. * @cookie: transaction identifier to check status of
  921. * @txstate: if not %NULL updated with transaction state
  922. *
  923. * If @txstate is passed in, upon return it reflect the driver
  924. * internal state and can be used with dma_async_is_complete() to check
  925. * the status of multiple cookies without re-checking hardware state.
  926. */
  927. static enum dma_status
  928. atc_tx_status(struct dma_chan *chan,
  929. dma_cookie_t cookie,
  930. struct dma_tx_state *txstate)
  931. {
  932. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  933. unsigned long flags;
  934. enum dma_status ret;
  935. int bytes = 0;
  936. ret = dma_cookie_status(chan, cookie, txstate);
  937. if (ret == DMA_COMPLETE)
  938. return ret;
  939. /*
  940. * There's no point calculating the residue if there's
  941. * no txstate to store the value.
  942. */
  943. if (!txstate)
  944. return DMA_ERROR;
  945. spin_lock_irqsave(&atchan->lock, flags);
  946. /* Get number of bytes left in the active transactions */
  947. bytes = atc_get_bytes_left(chan);
  948. spin_unlock_irqrestore(&atchan->lock, flags);
  949. if (unlikely(bytes < 0)) {
  950. dev_vdbg(chan2dev(chan), "get residual bytes error\n");
  951. return DMA_ERROR;
  952. } else {
  953. dma_set_residue(txstate, bytes);
  954. }
  955. dev_vdbg(chan2dev(chan), "tx_status %d: cookie = %d residue = %d\n",
  956. ret, cookie, bytes);
  957. return ret;
  958. }
  959. /**
  960. * atc_issue_pending - try to finish work
  961. * @chan: target DMA channel
  962. */
  963. static void atc_issue_pending(struct dma_chan *chan)
  964. {
  965. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  966. unsigned long flags;
  967. dev_vdbg(chan2dev(chan), "issue_pending\n");
  968. /* Not needed for cyclic transfers */
  969. if (atc_chan_is_cyclic(atchan))
  970. return;
  971. spin_lock_irqsave(&atchan->lock, flags);
  972. atc_advance_work(atchan);
  973. spin_unlock_irqrestore(&atchan->lock, flags);
  974. }
  975. /**
  976. * atc_alloc_chan_resources - allocate resources for DMA channel
  977. * @chan: allocate descriptor resources for this channel
  978. * @client: current client requesting the channel be ready for requests
  979. *
  980. * return - the number of allocated descriptors
  981. */
  982. static int atc_alloc_chan_resources(struct dma_chan *chan)
  983. {
  984. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  985. struct at_dma *atdma = to_at_dma(chan->device);
  986. struct at_desc *desc;
  987. struct at_dma_slave *atslave;
  988. unsigned long flags;
  989. int i;
  990. u32 cfg;
  991. LIST_HEAD(tmp_list);
  992. dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
  993. /* ASSERT: channel is idle */
  994. if (atc_chan_is_enabled(atchan)) {
  995. dev_dbg(chan2dev(chan), "DMA channel not idle ?\n");
  996. return -EIO;
  997. }
  998. cfg = ATC_DEFAULT_CFG;
  999. atslave = chan->private;
  1000. if (atslave) {
  1001. /*
  1002. * We need controller-specific data to set up slave
  1003. * transfers.
  1004. */
  1005. BUG_ON(!atslave->dma_dev || atslave->dma_dev != atdma->dma_common.dev);
  1006. /* if cfg configuration specified take it instead of default */
  1007. if (atslave->cfg)
  1008. cfg = atslave->cfg;
  1009. }
  1010. /* have we already been set up?
  1011. * reconfigure channel but no need to reallocate descriptors */
  1012. if (!list_empty(&atchan->free_list))
  1013. return atchan->descs_allocated;
  1014. /* Allocate initial pool of descriptors */
  1015. for (i = 0; i < init_nr_desc_per_channel; i++) {
  1016. desc = atc_alloc_descriptor(chan, GFP_KERNEL);
  1017. if (!desc) {
  1018. dev_err(atdma->dma_common.dev,
  1019. "Only %d initial descriptors\n", i);
  1020. break;
  1021. }
  1022. list_add_tail(&desc->desc_node, &tmp_list);
  1023. }
  1024. spin_lock_irqsave(&atchan->lock, flags);
  1025. atchan->descs_allocated = i;
  1026. atchan->remain_desc = 0;
  1027. list_splice(&tmp_list, &atchan->free_list);
  1028. dma_cookie_init(chan);
  1029. spin_unlock_irqrestore(&atchan->lock, flags);
  1030. /* channel parameters */
  1031. channel_writel(atchan, CFG, cfg);
  1032. dev_dbg(chan2dev(chan),
  1033. "alloc_chan_resources: allocated %d descriptors\n",
  1034. atchan->descs_allocated);
  1035. return atchan->descs_allocated;
  1036. }
  1037. /**
  1038. * atc_free_chan_resources - free all channel resources
  1039. * @chan: DMA channel
  1040. */
  1041. static void atc_free_chan_resources(struct dma_chan *chan)
  1042. {
  1043. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1044. struct at_dma *atdma = to_at_dma(chan->device);
  1045. struct at_desc *desc, *_desc;
  1046. LIST_HEAD(list);
  1047. dev_dbg(chan2dev(chan), "free_chan_resources: (descs allocated=%u)\n",
  1048. atchan->descs_allocated);
  1049. /* ASSERT: channel is idle */
  1050. BUG_ON(!list_empty(&atchan->active_list));
  1051. BUG_ON(!list_empty(&atchan->queue));
  1052. BUG_ON(atc_chan_is_enabled(atchan));
  1053. list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
  1054. dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
  1055. list_del(&desc->desc_node);
  1056. /* free link descriptor */
  1057. dma_pool_free(atdma->dma_desc_pool, desc, desc->txd.phys);
  1058. }
  1059. list_splice_init(&atchan->free_list, &list);
  1060. atchan->descs_allocated = 0;
  1061. atchan->status = 0;
  1062. atchan->remain_desc = 0;
  1063. dev_vdbg(chan2dev(chan), "free_chan_resources: done\n");
  1064. }
  1065. #ifdef CONFIG_OF
  1066. static bool at_dma_filter(struct dma_chan *chan, void *slave)
  1067. {
  1068. struct at_dma_slave *atslave = slave;
  1069. if (atslave->dma_dev == chan->device->dev) {
  1070. chan->private = atslave;
  1071. return true;
  1072. } else {
  1073. return false;
  1074. }
  1075. }
  1076. static struct dma_chan *at_dma_xlate(struct of_phandle_args *dma_spec,
  1077. struct of_dma *of_dma)
  1078. {
  1079. struct dma_chan *chan;
  1080. struct at_dma_chan *atchan;
  1081. struct at_dma_slave *atslave;
  1082. dma_cap_mask_t mask;
  1083. unsigned int per_id;
  1084. struct platform_device *dmac_pdev;
  1085. if (dma_spec->args_count != 2)
  1086. return NULL;
  1087. dmac_pdev = of_find_device_by_node(dma_spec->np);
  1088. dma_cap_zero(mask);
  1089. dma_cap_set(DMA_SLAVE, mask);
  1090. atslave = devm_kzalloc(&dmac_pdev->dev, sizeof(*atslave), GFP_KERNEL);
  1091. if (!atslave)
  1092. return NULL;
  1093. atslave->cfg = ATC_DST_H2SEL_HW | ATC_SRC_H2SEL_HW;
  1094. /*
  1095. * We can fill both SRC_PER and DST_PER, one of these fields will be
  1096. * ignored depending on DMA transfer direction.
  1097. */
  1098. per_id = dma_spec->args[1] & AT91_DMA_CFG_PER_ID_MASK;
  1099. atslave->cfg |= ATC_DST_PER_MSB(per_id) | ATC_DST_PER(per_id)
  1100. | ATC_SRC_PER_MSB(per_id) | ATC_SRC_PER(per_id);
  1101. /*
  1102. * We have to translate the value we get from the device tree since
  1103. * the half FIFO configuration value had to be 0 to keep backward
  1104. * compatibility.
  1105. */
  1106. switch (dma_spec->args[1] & AT91_DMA_CFG_FIFOCFG_MASK) {
  1107. case AT91_DMA_CFG_FIFOCFG_ALAP:
  1108. atslave->cfg |= ATC_FIFOCFG_LARGESTBURST;
  1109. break;
  1110. case AT91_DMA_CFG_FIFOCFG_ASAP:
  1111. atslave->cfg |= ATC_FIFOCFG_ENOUGHSPACE;
  1112. break;
  1113. case AT91_DMA_CFG_FIFOCFG_HALF:
  1114. default:
  1115. atslave->cfg |= ATC_FIFOCFG_HALFFIFO;
  1116. }
  1117. atslave->dma_dev = &dmac_pdev->dev;
  1118. chan = dma_request_channel(mask, at_dma_filter, atslave);
  1119. if (!chan)
  1120. return NULL;
  1121. atchan = to_at_dma_chan(chan);
  1122. atchan->per_if = dma_spec->args[0] & 0xff;
  1123. atchan->mem_if = (dma_spec->args[0] >> 16) & 0xff;
  1124. return chan;
  1125. }
  1126. #else
  1127. static struct dma_chan *at_dma_xlate(struct of_phandle_args *dma_spec,
  1128. struct of_dma *of_dma)
  1129. {
  1130. return NULL;
  1131. }
  1132. #endif
  1133. /*-- Module Management -----------------------------------------------*/
  1134. /* cap_mask is a multi-u32 bitfield, fill it with proper C code. */
  1135. static struct at_dma_platform_data at91sam9rl_config = {
  1136. .nr_channels = 2,
  1137. };
  1138. static struct at_dma_platform_data at91sam9g45_config = {
  1139. .nr_channels = 8,
  1140. };
  1141. #if defined(CONFIG_OF)
  1142. static const struct of_device_id atmel_dma_dt_ids[] = {
  1143. {
  1144. .compatible = "atmel,at91sam9rl-dma",
  1145. .data = &at91sam9rl_config,
  1146. }, {
  1147. .compatible = "atmel,at91sam9g45-dma",
  1148. .data = &at91sam9g45_config,
  1149. }, {
  1150. /* sentinel */
  1151. }
  1152. };
  1153. MODULE_DEVICE_TABLE(of, atmel_dma_dt_ids);
  1154. #endif
  1155. static const struct platform_device_id atdma_devtypes[] = {
  1156. {
  1157. .name = "at91sam9rl_dma",
  1158. .driver_data = (unsigned long) &at91sam9rl_config,
  1159. }, {
  1160. .name = "at91sam9g45_dma",
  1161. .driver_data = (unsigned long) &at91sam9g45_config,
  1162. }, {
  1163. /* sentinel */
  1164. }
  1165. };
  1166. static inline const struct at_dma_platform_data * __init at_dma_get_driver_data(
  1167. struct platform_device *pdev)
  1168. {
  1169. if (pdev->dev.of_node) {
  1170. const struct of_device_id *match;
  1171. match = of_match_node(atmel_dma_dt_ids, pdev->dev.of_node);
  1172. if (match == NULL)
  1173. return NULL;
  1174. return match->data;
  1175. }
  1176. return (struct at_dma_platform_data *)
  1177. platform_get_device_id(pdev)->driver_data;
  1178. }
  1179. /**
  1180. * at_dma_off - disable DMA controller
  1181. * @atdma: the Atmel HDAMC device
  1182. */
  1183. static void at_dma_off(struct at_dma *atdma)
  1184. {
  1185. dma_writel(atdma, EN, 0);
  1186. /* disable all interrupts */
  1187. dma_writel(atdma, EBCIDR, -1L);
  1188. /* confirm that all channels are disabled */
  1189. while (dma_readl(atdma, CHSR) & atdma->all_chan_mask)
  1190. cpu_relax();
  1191. }
  1192. static int __init at_dma_probe(struct platform_device *pdev)
  1193. {
  1194. struct resource *io;
  1195. struct at_dma *atdma;
  1196. size_t size;
  1197. int irq;
  1198. int err;
  1199. int i;
  1200. const struct at_dma_platform_data *plat_dat;
  1201. /* setup platform data for each SoC */
  1202. dma_cap_set(DMA_MEMCPY, at91sam9rl_config.cap_mask);
  1203. dma_cap_set(DMA_MEMCPY, at91sam9g45_config.cap_mask);
  1204. dma_cap_set(DMA_SLAVE, at91sam9g45_config.cap_mask);
  1205. /* get DMA parameters from controller type */
  1206. plat_dat = at_dma_get_driver_data(pdev);
  1207. if (!plat_dat)
  1208. return -ENODEV;
  1209. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1210. if (!io)
  1211. return -EINVAL;
  1212. irq = platform_get_irq(pdev, 0);
  1213. if (irq < 0)
  1214. return irq;
  1215. size = sizeof(struct at_dma);
  1216. size += plat_dat->nr_channels * sizeof(struct at_dma_chan);
  1217. atdma = kzalloc(size, GFP_KERNEL);
  1218. if (!atdma)
  1219. return -ENOMEM;
  1220. /* discover transaction capabilities */
  1221. atdma->dma_common.cap_mask = plat_dat->cap_mask;
  1222. atdma->all_chan_mask = (1 << plat_dat->nr_channels) - 1;
  1223. size = resource_size(io);
  1224. if (!request_mem_region(io->start, size, pdev->dev.driver->name)) {
  1225. err = -EBUSY;
  1226. goto err_kfree;
  1227. }
  1228. atdma->regs = ioremap(io->start, size);
  1229. if (!atdma->regs) {
  1230. err = -ENOMEM;
  1231. goto err_release_r;
  1232. }
  1233. atdma->clk = clk_get(&pdev->dev, "dma_clk");
  1234. if (IS_ERR(atdma->clk)) {
  1235. err = PTR_ERR(atdma->clk);
  1236. goto err_clk;
  1237. }
  1238. err = clk_prepare_enable(atdma->clk);
  1239. if (err)
  1240. goto err_clk_prepare;
  1241. /* force dma off, just in case */
  1242. at_dma_off(atdma);
  1243. err = request_irq(irq, at_dma_interrupt, 0, "at_hdmac", atdma);
  1244. if (err)
  1245. goto err_irq;
  1246. platform_set_drvdata(pdev, atdma);
  1247. /* create a pool of consistent memory blocks for hardware descriptors */
  1248. atdma->dma_desc_pool = dma_pool_create("at_hdmac_desc_pool",
  1249. &pdev->dev, sizeof(struct at_desc),
  1250. 4 /* word alignment */, 0);
  1251. if (!atdma->dma_desc_pool) {
  1252. dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
  1253. err = -ENOMEM;
  1254. goto err_pool_create;
  1255. }
  1256. /* clear any pending interrupt */
  1257. while (dma_readl(atdma, EBCISR))
  1258. cpu_relax();
  1259. /* initialize channels related values */
  1260. INIT_LIST_HEAD(&atdma->dma_common.channels);
  1261. for (i = 0; i < plat_dat->nr_channels; i++) {
  1262. struct at_dma_chan *atchan = &atdma->chan[i];
  1263. atchan->mem_if = AT_DMA_MEM_IF;
  1264. atchan->per_if = AT_DMA_PER_IF;
  1265. atchan->chan_common.device = &atdma->dma_common;
  1266. dma_cookie_init(&atchan->chan_common);
  1267. list_add_tail(&atchan->chan_common.device_node,
  1268. &atdma->dma_common.channels);
  1269. atchan->ch_regs = atdma->regs + ch_regs(i);
  1270. spin_lock_init(&atchan->lock);
  1271. atchan->mask = 1 << i;
  1272. INIT_LIST_HEAD(&atchan->active_list);
  1273. INIT_LIST_HEAD(&atchan->queue);
  1274. INIT_LIST_HEAD(&atchan->free_list);
  1275. tasklet_init(&atchan->tasklet, atc_tasklet,
  1276. (unsigned long)atchan);
  1277. atc_enable_chan_irq(atdma, i);
  1278. }
  1279. /* set base routines */
  1280. atdma->dma_common.device_alloc_chan_resources = atc_alloc_chan_resources;
  1281. atdma->dma_common.device_free_chan_resources = atc_free_chan_resources;
  1282. atdma->dma_common.device_tx_status = atc_tx_status;
  1283. atdma->dma_common.device_issue_pending = atc_issue_pending;
  1284. atdma->dma_common.dev = &pdev->dev;
  1285. /* set prep routines based on capability */
  1286. if (dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask))
  1287. atdma->dma_common.device_prep_dma_memcpy = atc_prep_dma_memcpy;
  1288. if (dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask)) {
  1289. atdma->dma_common.device_prep_slave_sg = atc_prep_slave_sg;
  1290. /* controller can do slave DMA: can trigger cyclic transfers */
  1291. dma_cap_set(DMA_CYCLIC, atdma->dma_common.cap_mask);
  1292. atdma->dma_common.device_prep_dma_cyclic = atc_prep_dma_cyclic;
  1293. atdma->dma_common.device_config = atc_config;
  1294. atdma->dma_common.device_pause = atc_pause;
  1295. atdma->dma_common.device_resume = atc_resume;
  1296. atdma->dma_common.device_terminate_all = atc_terminate_all;
  1297. atdma->dma_common.src_addr_widths = ATC_DMA_BUSWIDTHS;
  1298. atdma->dma_common.dst_addr_widths = ATC_DMA_BUSWIDTHS;
  1299. atdma->dma_common.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  1300. atdma->dma_common.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  1301. }
  1302. dma_writel(atdma, EN, AT_DMA_ENABLE);
  1303. dev_info(&pdev->dev, "Atmel AHB DMA Controller ( %s%s), %d channels\n",
  1304. dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask) ? "cpy " : "",
  1305. dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask) ? "slave " : "",
  1306. plat_dat->nr_channels);
  1307. dma_async_device_register(&atdma->dma_common);
  1308. /*
  1309. * Do not return an error if the dmac node is not present in order to
  1310. * not break the existing way of requesting channel with
  1311. * dma_request_channel().
  1312. */
  1313. if (pdev->dev.of_node) {
  1314. err = of_dma_controller_register(pdev->dev.of_node,
  1315. at_dma_xlate, atdma);
  1316. if (err) {
  1317. dev_err(&pdev->dev, "could not register of_dma_controller\n");
  1318. goto err_of_dma_controller_register;
  1319. }
  1320. }
  1321. return 0;
  1322. err_of_dma_controller_register:
  1323. dma_async_device_unregister(&atdma->dma_common);
  1324. dma_pool_destroy(atdma->dma_desc_pool);
  1325. err_pool_create:
  1326. free_irq(platform_get_irq(pdev, 0), atdma);
  1327. err_irq:
  1328. clk_disable_unprepare(atdma->clk);
  1329. err_clk_prepare:
  1330. clk_put(atdma->clk);
  1331. err_clk:
  1332. iounmap(atdma->regs);
  1333. atdma->regs = NULL;
  1334. err_release_r:
  1335. release_mem_region(io->start, size);
  1336. err_kfree:
  1337. kfree(atdma);
  1338. return err;
  1339. }
  1340. static int at_dma_remove(struct platform_device *pdev)
  1341. {
  1342. struct at_dma *atdma = platform_get_drvdata(pdev);
  1343. struct dma_chan *chan, *_chan;
  1344. struct resource *io;
  1345. at_dma_off(atdma);
  1346. dma_async_device_unregister(&atdma->dma_common);
  1347. dma_pool_destroy(atdma->dma_desc_pool);
  1348. free_irq(platform_get_irq(pdev, 0), atdma);
  1349. list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
  1350. device_node) {
  1351. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1352. /* Disable interrupts */
  1353. atc_disable_chan_irq(atdma, chan->chan_id);
  1354. tasklet_kill(&atchan->tasklet);
  1355. list_del(&chan->device_node);
  1356. }
  1357. clk_disable_unprepare(atdma->clk);
  1358. clk_put(atdma->clk);
  1359. iounmap(atdma->regs);
  1360. atdma->regs = NULL;
  1361. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1362. release_mem_region(io->start, resource_size(io));
  1363. kfree(atdma);
  1364. return 0;
  1365. }
  1366. static void at_dma_shutdown(struct platform_device *pdev)
  1367. {
  1368. struct at_dma *atdma = platform_get_drvdata(pdev);
  1369. at_dma_off(platform_get_drvdata(pdev));
  1370. clk_disable_unprepare(atdma->clk);
  1371. }
  1372. static int at_dma_prepare(struct device *dev)
  1373. {
  1374. struct platform_device *pdev = to_platform_device(dev);
  1375. struct at_dma *atdma = platform_get_drvdata(pdev);
  1376. struct dma_chan *chan, *_chan;
  1377. list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
  1378. device_node) {
  1379. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1380. /* wait for transaction completion (except in cyclic case) */
  1381. if (atc_chan_is_enabled(atchan) && !atc_chan_is_cyclic(atchan))
  1382. return -EAGAIN;
  1383. }
  1384. return 0;
  1385. }
  1386. static void atc_suspend_cyclic(struct at_dma_chan *atchan)
  1387. {
  1388. struct dma_chan *chan = &atchan->chan_common;
  1389. /* Channel should be paused by user
  1390. * do it anyway even if it is not done already */
  1391. if (!atc_chan_is_paused(atchan)) {
  1392. dev_warn(chan2dev(chan),
  1393. "cyclic channel not paused, should be done by channel user\n");
  1394. atc_pause(chan);
  1395. }
  1396. /* now preserve additional data for cyclic operations */
  1397. /* next descriptor address in the cyclic list */
  1398. atchan->save_dscr = channel_readl(atchan, DSCR);
  1399. vdbg_dump_regs(atchan);
  1400. }
  1401. static int at_dma_suspend_noirq(struct device *dev)
  1402. {
  1403. struct platform_device *pdev = to_platform_device(dev);
  1404. struct at_dma *atdma = platform_get_drvdata(pdev);
  1405. struct dma_chan *chan, *_chan;
  1406. /* preserve data */
  1407. list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
  1408. device_node) {
  1409. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1410. if (atc_chan_is_cyclic(atchan))
  1411. atc_suspend_cyclic(atchan);
  1412. atchan->save_cfg = channel_readl(atchan, CFG);
  1413. }
  1414. atdma->save_imr = dma_readl(atdma, EBCIMR);
  1415. /* disable DMA controller */
  1416. at_dma_off(atdma);
  1417. clk_disable_unprepare(atdma->clk);
  1418. return 0;
  1419. }
  1420. static void atc_resume_cyclic(struct at_dma_chan *atchan)
  1421. {
  1422. struct at_dma *atdma = to_at_dma(atchan->chan_common.device);
  1423. /* restore channel status for cyclic descriptors list:
  1424. * next descriptor in the cyclic list at the time of suspend */
  1425. channel_writel(atchan, SADDR, 0);
  1426. channel_writel(atchan, DADDR, 0);
  1427. channel_writel(atchan, CTRLA, 0);
  1428. channel_writel(atchan, CTRLB, 0);
  1429. channel_writel(atchan, DSCR, atchan->save_dscr);
  1430. dma_writel(atdma, CHER, atchan->mask);
  1431. /* channel pause status should be removed by channel user
  1432. * We cannot take the initiative to do it here */
  1433. vdbg_dump_regs(atchan);
  1434. }
  1435. static int at_dma_resume_noirq(struct device *dev)
  1436. {
  1437. struct platform_device *pdev = to_platform_device(dev);
  1438. struct at_dma *atdma = platform_get_drvdata(pdev);
  1439. struct dma_chan *chan, *_chan;
  1440. /* bring back DMA controller */
  1441. clk_prepare_enable(atdma->clk);
  1442. dma_writel(atdma, EN, AT_DMA_ENABLE);
  1443. /* clear any pending interrupt */
  1444. while (dma_readl(atdma, EBCISR))
  1445. cpu_relax();
  1446. /* restore saved data */
  1447. dma_writel(atdma, EBCIER, atdma->save_imr);
  1448. list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
  1449. device_node) {
  1450. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1451. channel_writel(atchan, CFG, atchan->save_cfg);
  1452. if (atc_chan_is_cyclic(atchan))
  1453. atc_resume_cyclic(atchan);
  1454. }
  1455. return 0;
  1456. }
  1457. static const struct dev_pm_ops at_dma_dev_pm_ops = {
  1458. .prepare = at_dma_prepare,
  1459. .suspend_noirq = at_dma_suspend_noirq,
  1460. .resume_noirq = at_dma_resume_noirq,
  1461. };
  1462. static struct platform_driver at_dma_driver = {
  1463. .remove = at_dma_remove,
  1464. .shutdown = at_dma_shutdown,
  1465. .id_table = atdma_devtypes,
  1466. .driver = {
  1467. .name = "at_hdmac",
  1468. .pm = &at_dma_dev_pm_ops,
  1469. .of_match_table = of_match_ptr(atmel_dma_dt_ids),
  1470. },
  1471. };
  1472. static int __init at_dma_init(void)
  1473. {
  1474. return platform_driver_probe(&at_dma_driver, at_dma_probe);
  1475. }
  1476. subsys_initcall(at_dma_init);
  1477. static void __exit at_dma_exit(void)
  1478. {
  1479. platform_driver_unregister(&at_dma_driver);
  1480. }
  1481. module_exit(at_dma_exit);
  1482. MODULE_DESCRIPTION("Atmel AHB DMA Controller driver");
  1483. MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
  1484. MODULE_LICENSE("GPL");
  1485. MODULE_ALIAS("platform:at_hdmac");