at_hdmac.c 42 KB

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