at_hdmac.c 44 KB

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