at_hdmac.c 42 KB

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