at_hdmac.c 47 KB

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