at_hdmac.c 42 KB

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