at_hdmac.c 38 KB

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