at_hdmac.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577
  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. #if defined(CONFIG_OF)
  991. static const struct of_device_id atmel_dma_dt_ids[] = {
  992. {
  993. .compatible = "atmel,at91sam9rl-dma",
  994. .data = (void *)ATDMA_DEVTYPE_SAM9RL
  995. }, {
  996. .compatible = "atmel,at91sam9g45-dma",
  997. .data = (void *)ATDMA_DEVTYPE_SAM9G45
  998. }, { /* sentinel */ }
  999. };
  1000. MODULE_DEVICE_TABLE(of, atmel_dma_dt_ids);
  1001. #endif
  1002. static const struct platform_device_id atdma_devtypes[] = {
  1003. {
  1004. .name = "at91sam9rl_dma",
  1005. .driver_data = ATDMA_DEVTYPE_SAM9RL,
  1006. }, {
  1007. .name = "at91sam9g45_dma",
  1008. .driver_data = ATDMA_DEVTYPE_SAM9G45,
  1009. }, {
  1010. /* sentinel */
  1011. }
  1012. };
  1013. static inline enum atdma_devtype __init at_dma_get_driver_data(
  1014. struct platform_device *pdev)
  1015. {
  1016. if (pdev->dev.of_node) {
  1017. const struct of_device_id *match;
  1018. match = of_match_node(atmel_dma_dt_ids, pdev->dev.of_node);
  1019. if (match == NULL)
  1020. return ATDMA_DEVTYPE_UNDEFINED;
  1021. return (enum atdma_devtype)match->data;
  1022. }
  1023. return platform_get_device_id(pdev)->driver_data;
  1024. }
  1025. /**
  1026. * at_dma_off - disable DMA controller
  1027. * @atdma: the Atmel HDAMC device
  1028. */
  1029. static void at_dma_off(struct at_dma *atdma)
  1030. {
  1031. dma_writel(atdma, EN, 0);
  1032. /* disable all interrupts */
  1033. dma_writel(atdma, EBCIDR, -1L);
  1034. /* confirm that all channels are disabled */
  1035. while (dma_readl(atdma, CHSR) & atdma->all_chan_mask)
  1036. cpu_relax();
  1037. }
  1038. static int __init at_dma_probe(struct platform_device *pdev)
  1039. {
  1040. struct resource *io;
  1041. struct at_dma *atdma;
  1042. size_t size;
  1043. int irq;
  1044. int err;
  1045. int i;
  1046. u32 nr_channels;
  1047. dma_cap_mask_t cap_mask = {};
  1048. enum atdma_devtype atdmatype;
  1049. dma_cap_set(DMA_MEMCPY, cap_mask);
  1050. /* get DMA parameters from controller type */
  1051. atdmatype = at_dma_get_driver_data(pdev);
  1052. switch (atdmatype) {
  1053. case ATDMA_DEVTYPE_SAM9RL:
  1054. nr_channels = 2;
  1055. break;
  1056. case ATDMA_DEVTYPE_SAM9G45:
  1057. nr_channels = 8;
  1058. dma_cap_set(DMA_SLAVE, cap_mask);
  1059. break;
  1060. default:
  1061. return -EINVAL;
  1062. }
  1063. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1064. if (!io)
  1065. return -EINVAL;
  1066. irq = platform_get_irq(pdev, 0);
  1067. if (irq < 0)
  1068. return irq;
  1069. size = sizeof(struct at_dma);
  1070. size += nr_channels * sizeof(struct at_dma_chan);
  1071. atdma = kzalloc(size, GFP_KERNEL);
  1072. if (!atdma)
  1073. return -ENOMEM;
  1074. /* discover transaction capabilities */
  1075. atdma->dma_common.cap_mask = cap_mask;
  1076. atdma->all_chan_mask = (1 << nr_channels) - 1;
  1077. atdma->devtype = atdmatype;
  1078. size = resource_size(io);
  1079. if (!request_mem_region(io->start, size, pdev->dev.driver->name)) {
  1080. err = -EBUSY;
  1081. goto err_kfree;
  1082. }
  1083. atdma->regs = ioremap(io->start, size);
  1084. if (!atdma->regs) {
  1085. err = -ENOMEM;
  1086. goto err_release_r;
  1087. }
  1088. atdma->clk = clk_get(&pdev->dev, "dma_clk");
  1089. if (IS_ERR(atdma->clk)) {
  1090. err = PTR_ERR(atdma->clk);
  1091. goto err_clk;
  1092. }
  1093. clk_enable(atdma->clk);
  1094. /* force dma off, just in case */
  1095. at_dma_off(atdma);
  1096. err = request_irq(irq, at_dma_interrupt, 0, "at_hdmac", atdma);
  1097. if (err)
  1098. goto err_irq;
  1099. platform_set_drvdata(pdev, atdma);
  1100. /* create a pool of consistent memory blocks for hardware descriptors */
  1101. atdma->dma_desc_pool = dma_pool_create("at_hdmac_desc_pool",
  1102. &pdev->dev, sizeof(struct at_desc),
  1103. 4 /* word alignment */, 0);
  1104. if (!atdma->dma_desc_pool) {
  1105. dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
  1106. err = -ENOMEM;
  1107. goto err_pool_create;
  1108. }
  1109. /* clear any pending interrupt */
  1110. while (dma_readl(atdma, EBCISR))
  1111. cpu_relax();
  1112. /* initialize channels related values */
  1113. INIT_LIST_HEAD(&atdma->dma_common.channels);
  1114. for (i = 0; i < nr_channels; i++) {
  1115. struct at_dma_chan *atchan = &atdma->chan[i];
  1116. atchan->chan_common.device = &atdma->dma_common;
  1117. atchan->chan_common.cookie = atchan->completed_cookie = 1;
  1118. list_add_tail(&atchan->chan_common.device_node,
  1119. &atdma->dma_common.channels);
  1120. atchan->ch_regs = atdma->regs + ch_regs(i);
  1121. spin_lock_init(&atchan->lock);
  1122. atchan->mask = 1 << i;
  1123. INIT_LIST_HEAD(&atchan->active_list);
  1124. INIT_LIST_HEAD(&atchan->queue);
  1125. INIT_LIST_HEAD(&atchan->free_list);
  1126. tasklet_init(&atchan->tasklet, atc_tasklet,
  1127. (unsigned long)atchan);
  1128. atc_enable_irq(atchan);
  1129. }
  1130. /* set base routines */
  1131. atdma->dma_common.device_alloc_chan_resources = atc_alloc_chan_resources;
  1132. atdma->dma_common.device_free_chan_resources = atc_free_chan_resources;
  1133. atdma->dma_common.device_tx_status = atc_tx_status;
  1134. atdma->dma_common.device_issue_pending = atc_issue_pending;
  1135. atdma->dma_common.dev = &pdev->dev;
  1136. /* set prep routines based on capability */
  1137. if (dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask))
  1138. atdma->dma_common.device_prep_dma_memcpy = atc_prep_dma_memcpy;
  1139. if (dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask)) {
  1140. atdma->dma_common.device_prep_slave_sg = atc_prep_slave_sg;
  1141. /* controller can do slave DMA: can trigger cyclic transfers */
  1142. dma_cap_set(DMA_CYCLIC, atdma->dma_common.cap_mask);
  1143. atdma->dma_common.device_prep_dma_cyclic = atc_prep_dma_cyclic;
  1144. atdma->dma_common.device_control = atc_control;
  1145. }
  1146. dma_writel(atdma, EN, AT_DMA_ENABLE);
  1147. dev_info(&pdev->dev, "Atmel AHB DMA Controller ( %s%s), %d channels\n",
  1148. dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask) ? "cpy " : "",
  1149. dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask) ? "slave " : "",
  1150. nr_channels);
  1151. dma_async_device_register(&atdma->dma_common);
  1152. return 0;
  1153. err_pool_create:
  1154. platform_set_drvdata(pdev, NULL);
  1155. free_irq(platform_get_irq(pdev, 0), atdma);
  1156. err_irq:
  1157. clk_disable(atdma->clk);
  1158. clk_put(atdma->clk);
  1159. err_clk:
  1160. iounmap(atdma->regs);
  1161. atdma->regs = NULL;
  1162. err_release_r:
  1163. release_mem_region(io->start, size);
  1164. err_kfree:
  1165. kfree(atdma);
  1166. return err;
  1167. }
  1168. static int __exit at_dma_remove(struct platform_device *pdev)
  1169. {
  1170. struct at_dma *atdma = platform_get_drvdata(pdev);
  1171. struct dma_chan *chan, *_chan;
  1172. struct resource *io;
  1173. at_dma_off(atdma);
  1174. dma_async_device_unregister(&atdma->dma_common);
  1175. dma_pool_destroy(atdma->dma_desc_pool);
  1176. platform_set_drvdata(pdev, NULL);
  1177. free_irq(platform_get_irq(pdev, 0), atdma);
  1178. list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
  1179. device_node) {
  1180. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1181. /* Disable interrupts */
  1182. atc_disable_irq(atchan);
  1183. tasklet_disable(&atchan->tasklet);
  1184. tasklet_kill(&atchan->tasklet);
  1185. list_del(&chan->device_node);
  1186. }
  1187. clk_disable(atdma->clk);
  1188. clk_put(atdma->clk);
  1189. iounmap(atdma->regs);
  1190. atdma->regs = NULL;
  1191. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1192. release_mem_region(io->start, resource_size(io));
  1193. kfree(atdma);
  1194. return 0;
  1195. }
  1196. static void at_dma_shutdown(struct platform_device *pdev)
  1197. {
  1198. struct at_dma *atdma = platform_get_drvdata(pdev);
  1199. at_dma_off(platform_get_drvdata(pdev));
  1200. clk_disable(atdma->clk);
  1201. }
  1202. static int at_dma_prepare(struct device *dev)
  1203. {
  1204. struct platform_device *pdev = to_platform_device(dev);
  1205. struct at_dma *atdma = platform_get_drvdata(pdev);
  1206. struct dma_chan *chan, *_chan;
  1207. list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
  1208. device_node) {
  1209. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1210. /* wait for transaction completion (except in cyclic case) */
  1211. if (atc_chan_is_enabled(atchan) && !atc_chan_is_cyclic(atchan))
  1212. return -EAGAIN;
  1213. }
  1214. return 0;
  1215. }
  1216. static void atc_suspend_cyclic(struct at_dma_chan *atchan)
  1217. {
  1218. struct dma_chan *chan = &atchan->chan_common;
  1219. /* Channel should be paused by user
  1220. * do it anyway even if it is not done already */
  1221. if (!atc_chan_is_paused(atchan)) {
  1222. dev_warn(chan2dev(chan),
  1223. "cyclic channel not paused, should be done by channel user\n");
  1224. atc_control(chan, DMA_PAUSE, 0);
  1225. }
  1226. /* now preserve additional data for cyclic operations */
  1227. /* next descriptor address in the cyclic list */
  1228. atchan->save_dscr = channel_readl(atchan, DSCR);
  1229. vdbg_dump_regs(atchan);
  1230. }
  1231. static int at_dma_suspend_noirq(struct device *dev)
  1232. {
  1233. struct platform_device *pdev = to_platform_device(dev);
  1234. struct at_dma *atdma = platform_get_drvdata(pdev);
  1235. struct dma_chan *chan, *_chan;
  1236. /* preserve data */
  1237. list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
  1238. device_node) {
  1239. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1240. if (atc_chan_is_cyclic(atchan))
  1241. atc_suspend_cyclic(atchan);
  1242. atchan->save_cfg = channel_readl(atchan, CFG);
  1243. }
  1244. atdma->save_imr = dma_readl(atdma, EBCIMR);
  1245. /* disable DMA controller */
  1246. at_dma_off(atdma);
  1247. clk_disable(atdma->clk);
  1248. return 0;
  1249. }
  1250. static void atc_resume_cyclic(struct at_dma_chan *atchan)
  1251. {
  1252. struct at_dma *atdma = to_at_dma(atchan->chan_common.device);
  1253. /* restore channel status for cyclic descriptors list:
  1254. * next descriptor in the cyclic list at the time of suspend */
  1255. channel_writel(atchan, SADDR, 0);
  1256. channel_writel(atchan, DADDR, 0);
  1257. channel_writel(atchan, CTRLA, 0);
  1258. channel_writel(atchan, CTRLB, 0);
  1259. channel_writel(atchan, DSCR, atchan->save_dscr);
  1260. dma_writel(atdma, CHER, atchan->mask);
  1261. /* channel pause status should be removed by channel user
  1262. * We cannot take the initiative to do it here */
  1263. vdbg_dump_regs(atchan);
  1264. }
  1265. static int at_dma_resume_noirq(struct device *dev)
  1266. {
  1267. struct platform_device *pdev = to_platform_device(dev);
  1268. struct at_dma *atdma = platform_get_drvdata(pdev);
  1269. struct dma_chan *chan, *_chan;
  1270. /* bring back DMA controller */
  1271. clk_enable(atdma->clk);
  1272. dma_writel(atdma, EN, AT_DMA_ENABLE);
  1273. /* clear any pending interrupt */
  1274. while (dma_readl(atdma, EBCISR))
  1275. cpu_relax();
  1276. /* restore saved data */
  1277. dma_writel(atdma, EBCIER, atdma->save_imr);
  1278. list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
  1279. device_node) {
  1280. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1281. channel_writel(atchan, CFG, atchan->save_cfg);
  1282. if (atc_chan_is_cyclic(atchan))
  1283. atc_resume_cyclic(atchan);
  1284. }
  1285. return 0;
  1286. }
  1287. static const struct dev_pm_ops at_dma_dev_pm_ops = {
  1288. .prepare = at_dma_prepare,
  1289. .suspend_noirq = at_dma_suspend_noirq,
  1290. .resume_noirq = at_dma_resume_noirq,
  1291. };
  1292. static struct platform_driver at_dma_driver = {
  1293. .remove = __exit_p(at_dma_remove),
  1294. .shutdown = at_dma_shutdown,
  1295. .id_table = atdma_devtypes,
  1296. .driver = {
  1297. .name = "at_hdmac",
  1298. .pm = &at_dma_dev_pm_ops,
  1299. .of_match_table = of_match_ptr(atmel_dma_dt_ids),
  1300. },
  1301. };
  1302. static int __init at_dma_init(void)
  1303. {
  1304. return platform_driver_probe(&at_dma_driver, at_dma_probe);
  1305. }
  1306. subsys_initcall(at_dma_init);
  1307. static void __exit at_dma_exit(void)
  1308. {
  1309. platform_driver_unregister(&at_dma_driver);
  1310. }
  1311. module_exit(at_dma_exit);
  1312. MODULE_DESCRIPTION("Atmel AHB DMA Controller driver");
  1313. MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
  1314. MODULE_LICENSE("GPL");
  1315. MODULE_ALIAS("platform:at_hdmac");