at_hdmac.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  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(0) \
  37. |ATC_DIF(1))
  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_assign_cookie - compute and assign new cookie
  148. * @atchan: channel we work on
  149. * @desc: descriptor to asign cookie for
  150. *
  151. * Called with atchan->lock held and bh disabled
  152. */
  153. static dma_cookie_t
  154. atc_assign_cookie(struct at_dma_chan *atchan, struct at_desc *desc)
  155. {
  156. dma_cookie_t cookie = atchan->chan_common.cookie;
  157. if (++cookie < 0)
  158. cookie = 1;
  159. atchan->chan_common.cookie = cookie;
  160. desc->txd.cookie = cookie;
  161. return cookie;
  162. }
  163. /**
  164. * atc_dostart - starts the DMA engine for real
  165. * @atchan: the channel we want to start
  166. * @first: first descriptor in the list we want to begin with
  167. *
  168. * Called with atchan->lock held and bh disabled
  169. */
  170. static void atc_dostart(struct at_dma_chan *atchan, struct at_desc *first)
  171. {
  172. struct at_dma *atdma = to_at_dma(atchan->chan_common.device);
  173. /* ASSERT: channel is idle */
  174. if (atc_chan_is_enabled(atchan)) {
  175. dev_err(chan2dev(&atchan->chan_common),
  176. "BUG: Attempted to start non-idle channel\n");
  177. dev_err(chan2dev(&atchan->chan_common),
  178. " channel: s0x%x d0x%x ctrl0x%x:0x%x l0x%x\n",
  179. channel_readl(atchan, SADDR),
  180. channel_readl(atchan, DADDR),
  181. channel_readl(atchan, CTRLA),
  182. channel_readl(atchan, CTRLB),
  183. channel_readl(atchan, DSCR));
  184. /* The tasklet will hopefully advance the queue... */
  185. return;
  186. }
  187. vdbg_dump_regs(atchan);
  188. /* clear any pending interrupt */
  189. while (dma_readl(atdma, EBCISR))
  190. cpu_relax();
  191. channel_writel(atchan, SADDR, 0);
  192. channel_writel(atchan, DADDR, 0);
  193. channel_writel(atchan, CTRLA, 0);
  194. channel_writel(atchan, CTRLB, 0);
  195. channel_writel(atchan, DSCR, first->txd.phys);
  196. dma_writel(atdma, CHER, atchan->mask);
  197. vdbg_dump_regs(atchan);
  198. }
  199. /**
  200. * atc_chain_complete - finish work for one transaction chain
  201. * @atchan: channel we work on
  202. * @desc: descriptor at the head of the chain we want do complete
  203. *
  204. * Called with atchan->lock held and bh disabled */
  205. static void
  206. atc_chain_complete(struct at_dma_chan *atchan, struct at_desc *desc)
  207. {
  208. dma_async_tx_callback callback;
  209. void *param;
  210. struct dma_async_tx_descriptor *txd = &desc->txd;
  211. dev_vdbg(chan2dev(&atchan->chan_common),
  212. "descriptor %u complete\n", txd->cookie);
  213. atchan->completed_cookie = txd->cookie;
  214. callback = txd->callback;
  215. param = txd->callback_param;
  216. /* move children to free_list */
  217. list_splice_init(&desc->tx_list, &atchan->free_list);
  218. /* move myself to free_list */
  219. list_move(&desc->desc_node, &atchan->free_list);
  220. /* unmap dma addresses */
  221. if (!atchan->chan_common.private) {
  222. struct device *parent = chan2parent(&atchan->chan_common);
  223. if (!(txd->flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
  224. if (txd->flags & DMA_COMPL_DEST_UNMAP_SINGLE)
  225. dma_unmap_single(parent,
  226. desc->lli.daddr,
  227. desc->len, DMA_FROM_DEVICE);
  228. else
  229. dma_unmap_page(parent,
  230. desc->lli.daddr,
  231. desc->len, DMA_FROM_DEVICE);
  232. }
  233. if (!(txd->flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
  234. if (txd->flags & DMA_COMPL_SRC_UNMAP_SINGLE)
  235. dma_unmap_single(parent,
  236. desc->lli.saddr,
  237. desc->len, DMA_TO_DEVICE);
  238. else
  239. dma_unmap_page(parent,
  240. desc->lli.saddr,
  241. desc->len, DMA_TO_DEVICE);
  242. }
  243. }
  244. /*
  245. * The API requires that no submissions are done from a
  246. * callback, so we don't need to drop the lock here
  247. */
  248. if (callback)
  249. callback(param);
  250. dma_run_dependencies(txd);
  251. }
  252. /**
  253. * atc_complete_all - finish work for all transactions
  254. * @atchan: channel to complete transactions for
  255. *
  256. * Eventually submit queued descriptors if any
  257. *
  258. * Assume channel is idle while calling this function
  259. * Called with atchan->lock held and bh disabled
  260. */
  261. static void atc_complete_all(struct at_dma_chan *atchan)
  262. {
  263. struct at_desc *desc, *_desc;
  264. LIST_HEAD(list);
  265. dev_vdbg(chan2dev(&atchan->chan_common), "complete all\n");
  266. BUG_ON(atc_chan_is_enabled(atchan));
  267. /*
  268. * Submit queued descriptors ASAP, i.e. before we go through
  269. * the completed ones.
  270. */
  271. if (!list_empty(&atchan->queue))
  272. atc_dostart(atchan, atc_first_queued(atchan));
  273. /* empty active_list now it is completed */
  274. list_splice_init(&atchan->active_list, &list);
  275. /* empty queue list by moving descriptors (if any) to active_list */
  276. list_splice_init(&atchan->queue, &atchan->active_list);
  277. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  278. atc_chain_complete(atchan, desc);
  279. }
  280. /**
  281. * atc_cleanup_descriptors - cleanup up finished descriptors in active_list
  282. * @atchan: channel to be cleaned up
  283. *
  284. * Called with atchan->lock held and bh disabled
  285. */
  286. static void atc_cleanup_descriptors(struct at_dma_chan *atchan)
  287. {
  288. struct at_desc *desc, *_desc;
  289. struct at_desc *child;
  290. dev_vdbg(chan2dev(&atchan->chan_common), "cleanup descriptors\n");
  291. list_for_each_entry_safe(desc, _desc, &atchan->active_list, desc_node) {
  292. if (!(desc->lli.ctrla & ATC_DONE))
  293. /* This one is currently in progress */
  294. return;
  295. list_for_each_entry(child, &desc->tx_list, desc_node)
  296. if (!(child->lli.ctrla & ATC_DONE))
  297. /* Currently in progress */
  298. return;
  299. /*
  300. * No descriptors so far seem to be in progress, i.e.
  301. * this chain must be done.
  302. */
  303. atc_chain_complete(atchan, desc);
  304. }
  305. }
  306. /**
  307. * atc_advance_work - at the end of a transaction, move forward
  308. * @atchan: channel where the transaction ended
  309. *
  310. * Called with atchan->lock held and bh disabled
  311. */
  312. static void atc_advance_work(struct at_dma_chan *atchan)
  313. {
  314. dev_vdbg(chan2dev(&atchan->chan_common), "advance_work\n");
  315. if (list_empty(&atchan->active_list) ||
  316. list_is_singular(&atchan->active_list)) {
  317. atc_complete_all(atchan);
  318. } else {
  319. atc_chain_complete(atchan, atc_first_active(atchan));
  320. /* advance work */
  321. atc_dostart(atchan, atc_first_active(atchan));
  322. }
  323. }
  324. /**
  325. * atc_handle_error - handle errors reported by DMA controller
  326. * @atchan: channel where error occurs
  327. *
  328. * Called with atchan->lock held and bh disabled
  329. */
  330. static void atc_handle_error(struct at_dma_chan *atchan)
  331. {
  332. struct at_desc *bad_desc;
  333. struct at_desc *child;
  334. /*
  335. * The descriptor currently at the head of the active list is
  336. * broked. Since we don't have any way to report errors, we'll
  337. * just have to scream loudly and try to carry on.
  338. */
  339. bad_desc = atc_first_active(atchan);
  340. list_del_init(&bad_desc->desc_node);
  341. /* As we are stopped, take advantage to push queued descriptors
  342. * in active_list */
  343. list_splice_init(&atchan->queue, atchan->active_list.prev);
  344. /* Try to restart the controller */
  345. if (!list_empty(&atchan->active_list))
  346. atc_dostart(atchan, atc_first_active(atchan));
  347. /*
  348. * KERN_CRITICAL may seem harsh, but since this only happens
  349. * when someone submits a bad physical address in a
  350. * descriptor, we should consider ourselves lucky that the
  351. * controller flagged an error instead of scribbling over
  352. * random memory locations.
  353. */
  354. dev_crit(chan2dev(&atchan->chan_common),
  355. "Bad descriptor submitted for DMA!\n");
  356. dev_crit(chan2dev(&atchan->chan_common),
  357. " cookie: %d\n", bad_desc->txd.cookie);
  358. atc_dump_lli(atchan, &bad_desc->lli);
  359. list_for_each_entry(child, &bad_desc->tx_list, desc_node)
  360. atc_dump_lli(atchan, &child->lli);
  361. /* Pretend the descriptor completed successfully */
  362. atc_chain_complete(atchan, bad_desc);
  363. }
  364. /*-- IRQ & Tasklet ---------------------------------------------------*/
  365. static void atc_tasklet(unsigned long data)
  366. {
  367. struct at_dma_chan *atchan = (struct at_dma_chan *)data;
  368. /* Channel cannot be enabled here */
  369. if (atc_chan_is_enabled(atchan)) {
  370. dev_err(chan2dev(&atchan->chan_common),
  371. "BUG: channel enabled in tasklet\n");
  372. return;
  373. }
  374. spin_lock(&atchan->lock);
  375. if (test_and_clear_bit(0, &atchan->error_status))
  376. atc_handle_error(atchan);
  377. else
  378. atc_advance_work(atchan);
  379. spin_unlock(&atchan->lock);
  380. }
  381. static irqreturn_t at_dma_interrupt(int irq, void *dev_id)
  382. {
  383. struct at_dma *atdma = (struct at_dma *)dev_id;
  384. struct at_dma_chan *atchan;
  385. int i;
  386. u32 status, pending, imr;
  387. int ret = IRQ_NONE;
  388. do {
  389. imr = dma_readl(atdma, EBCIMR);
  390. status = dma_readl(atdma, EBCISR);
  391. pending = status & imr;
  392. if (!pending)
  393. break;
  394. dev_vdbg(atdma->dma_common.dev,
  395. "interrupt: status = 0x%08x, 0x%08x, 0x%08x\n",
  396. status, imr, pending);
  397. for (i = 0; i < atdma->dma_common.chancnt; i++) {
  398. atchan = &atdma->chan[i];
  399. if (pending & (AT_DMA_CBTC(i) | AT_DMA_ERR(i))) {
  400. if (pending & AT_DMA_ERR(i)) {
  401. /* Disable channel on AHB error */
  402. dma_writel(atdma, CHDR, atchan->mask);
  403. /* Give information to tasklet */
  404. set_bit(0, &atchan->error_status);
  405. }
  406. tasklet_schedule(&atchan->tasklet);
  407. ret = IRQ_HANDLED;
  408. }
  409. }
  410. } while (pending);
  411. return ret;
  412. }
  413. /*-- DMA Engine API --------------------------------------------------*/
  414. /**
  415. * atc_tx_submit - set the prepared descriptor(s) to be executed by the engine
  416. * @desc: descriptor at the head of the transaction chain
  417. *
  418. * Queue chain if DMA engine is working already
  419. *
  420. * Cookie increment and adding to active_list or queue must be atomic
  421. */
  422. static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx)
  423. {
  424. struct at_desc *desc = txd_to_at_desc(tx);
  425. struct at_dma_chan *atchan = to_at_dma_chan(tx->chan);
  426. dma_cookie_t cookie;
  427. spin_lock_bh(&atchan->lock);
  428. cookie = atc_assign_cookie(atchan, desc);
  429. if (list_empty(&atchan->active_list)) {
  430. dev_vdbg(chan2dev(tx->chan), "tx_submit: started %u\n",
  431. desc->txd.cookie);
  432. atc_dostart(atchan, desc);
  433. list_add_tail(&desc->desc_node, &atchan->active_list);
  434. } else {
  435. dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u\n",
  436. desc->txd.cookie);
  437. list_add_tail(&desc->desc_node, &atchan->queue);
  438. }
  439. spin_unlock_bh(&atchan->lock);
  440. return cookie;
  441. }
  442. /**
  443. * atc_prep_dma_memcpy - prepare a memcpy operation
  444. * @chan: the channel to prepare operation on
  445. * @dest: operation virtual destination address
  446. * @src: operation virtual source address
  447. * @len: operation length
  448. * @flags: tx descriptor status flags
  449. */
  450. static struct dma_async_tx_descriptor *
  451. atc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  452. size_t len, unsigned long flags)
  453. {
  454. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  455. struct at_desc *desc = NULL;
  456. struct at_desc *first = NULL;
  457. struct at_desc *prev = NULL;
  458. size_t xfer_count;
  459. size_t offset;
  460. unsigned int src_width;
  461. unsigned int dst_width;
  462. u32 ctrla;
  463. u32 ctrlb;
  464. dev_vdbg(chan2dev(chan), "prep_dma_memcpy: d0x%x s0x%x l0x%zx f0x%lx\n",
  465. dest, src, len, flags);
  466. if (unlikely(!len)) {
  467. dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
  468. return NULL;
  469. }
  470. ctrla = ATC_DEFAULT_CTRLA;
  471. ctrlb = ATC_DEFAULT_CTRLB
  472. | ATC_SRC_ADDR_MODE_INCR
  473. | ATC_DST_ADDR_MODE_INCR
  474. | ATC_FC_MEM2MEM;
  475. /*
  476. * We can be a lot more clever here, but this should take care
  477. * of the most common optimization.
  478. */
  479. if (!((src | dest | len) & 3)) {
  480. ctrla |= ATC_SRC_WIDTH_WORD | ATC_DST_WIDTH_WORD;
  481. src_width = dst_width = 2;
  482. } else if (!((src | dest | len) & 1)) {
  483. ctrla |= ATC_SRC_WIDTH_HALFWORD | ATC_DST_WIDTH_HALFWORD;
  484. src_width = dst_width = 1;
  485. } else {
  486. ctrla |= ATC_SRC_WIDTH_BYTE | ATC_DST_WIDTH_BYTE;
  487. src_width = dst_width = 0;
  488. }
  489. for (offset = 0; offset < len; offset += xfer_count << src_width) {
  490. xfer_count = min_t(size_t, (len - offset) >> src_width,
  491. ATC_BTSIZE_MAX);
  492. desc = atc_desc_get(atchan);
  493. if (!desc)
  494. goto err_desc_get;
  495. desc->lli.saddr = src + offset;
  496. desc->lli.daddr = dest + offset;
  497. desc->lli.ctrla = ctrla | xfer_count;
  498. desc->lli.ctrlb = ctrlb;
  499. desc->txd.cookie = 0;
  500. async_tx_ack(&desc->txd);
  501. if (!first) {
  502. first = desc;
  503. } else {
  504. /* inform the HW lli about chaining */
  505. prev->lli.dscr = desc->txd.phys;
  506. /* insert the link descriptor to the LD ring */
  507. list_add_tail(&desc->desc_node,
  508. &first->tx_list);
  509. }
  510. prev = desc;
  511. }
  512. /* First descriptor of the chain embedds additional information */
  513. first->txd.cookie = -EBUSY;
  514. first->len = len;
  515. /* set end-of-link to the last link descriptor of list*/
  516. set_desc_eol(desc);
  517. desc->txd.flags = flags; /* client is in control of this ack */
  518. return &first->txd;
  519. err_desc_get:
  520. atc_desc_put(atchan, first);
  521. return NULL;
  522. }
  523. /**
  524. * atc_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
  525. * @chan: DMA channel
  526. * @sgl: scatterlist to transfer to/from
  527. * @sg_len: number of entries in @scatterlist
  528. * @direction: DMA direction
  529. * @flags: tx descriptor status flags
  530. */
  531. static struct dma_async_tx_descriptor *
  532. atc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  533. unsigned int sg_len, enum dma_data_direction direction,
  534. unsigned long flags)
  535. {
  536. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  537. struct at_dma_slave *atslave = chan->private;
  538. struct at_desc *first = NULL;
  539. struct at_desc *prev = NULL;
  540. u32 ctrla;
  541. u32 ctrlb;
  542. dma_addr_t reg;
  543. unsigned int reg_width;
  544. unsigned int mem_width;
  545. unsigned int i;
  546. struct scatterlist *sg;
  547. size_t total_len = 0;
  548. dev_vdbg(chan2dev(chan), "prep_slave_sg: %s f0x%lx\n",
  549. direction == DMA_TO_DEVICE ? "TO DEVICE" : "FROM DEVICE",
  550. flags);
  551. if (unlikely(!atslave || !sg_len)) {
  552. dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
  553. return NULL;
  554. }
  555. reg_width = atslave->reg_width;
  556. ctrla = ATC_DEFAULT_CTRLA | atslave->ctrla;
  557. ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN;
  558. switch (direction) {
  559. case DMA_TO_DEVICE:
  560. ctrla |= ATC_DST_WIDTH(reg_width);
  561. ctrlb |= ATC_DST_ADDR_MODE_FIXED
  562. | ATC_SRC_ADDR_MODE_INCR
  563. | ATC_FC_MEM2PER;
  564. reg = atslave->tx_reg;
  565. for_each_sg(sgl, sg, sg_len, i) {
  566. struct at_desc *desc;
  567. u32 len;
  568. u32 mem;
  569. desc = atc_desc_get(atchan);
  570. if (!desc)
  571. goto err_desc_get;
  572. mem = sg_phys(sg);
  573. len = sg_dma_len(sg);
  574. mem_width = 2;
  575. if (unlikely(mem & 3 || len & 3))
  576. mem_width = 0;
  577. desc->lli.saddr = mem;
  578. desc->lli.daddr = reg;
  579. desc->lli.ctrla = ctrla
  580. | ATC_SRC_WIDTH(mem_width)
  581. | len >> mem_width;
  582. desc->lli.ctrlb = ctrlb;
  583. if (!first) {
  584. first = desc;
  585. } else {
  586. /* inform the HW lli about chaining */
  587. prev->lli.dscr = desc->txd.phys;
  588. /* insert the link descriptor to the LD ring */
  589. list_add_tail(&desc->desc_node,
  590. &first->tx_list);
  591. }
  592. prev = desc;
  593. total_len += len;
  594. }
  595. break;
  596. case DMA_FROM_DEVICE:
  597. ctrla |= ATC_SRC_WIDTH(reg_width);
  598. ctrlb |= ATC_DST_ADDR_MODE_INCR
  599. | ATC_SRC_ADDR_MODE_FIXED
  600. | ATC_FC_PER2MEM;
  601. reg = atslave->rx_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_phys(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 = reg;
  615. desc->lli.daddr = mem;
  616. desc->lli.ctrla = ctrla
  617. | ATC_DST_WIDTH(mem_width)
  618. | len >> reg_width;
  619. desc->lli.ctrlb = ctrlb;
  620. if (!first) {
  621. first = desc;
  622. } else {
  623. /* inform the HW lli about chaining */
  624. prev->lli.dscr = desc->txd.phys;
  625. /* insert the link descriptor to the LD ring */
  626. list_add_tail(&desc->desc_node,
  627. &first->tx_list);
  628. }
  629. prev = desc;
  630. total_len += len;
  631. }
  632. break;
  633. default:
  634. return NULL;
  635. }
  636. /* set end-of-link to the last link descriptor of list*/
  637. set_desc_eol(prev);
  638. /* First descriptor of the chain embedds additional information */
  639. first->txd.cookie = -EBUSY;
  640. first->len = total_len;
  641. /* last link descriptor of list is responsible of flags */
  642. prev->txd.flags = flags; /* client is in control of this ack */
  643. return &first->txd;
  644. err_desc_get:
  645. dev_err(chan2dev(chan), "not enough descriptors available\n");
  646. atc_desc_put(atchan, first);
  647. return NULL;
  648. }
  649. static int atc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  650. unsigned long arg)
  651. {
  652. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  653. struct at_dma *atdma = to_at_dma(chan->device);
  654. struct at_desc *desc, *_desc;
  655. LIST_HEAD(list);
  656. /* Only supports DMA_TERMINATE_ALL */
  657. if (cmd != DMA_TERMINATE_ALL)
  658. return -ENXIO;
  659. /*
  660. * This is only called when something went wrong elsewhere, so
  661. * we don't really care about the data. Just disable the
  662. * channel. We still have to poll the channel enable bit due
  663. * to AHB/HSB limitations.
  664. */
  665. spin_lock_bh(&atchan->lock);
  666. dma_writel(atdma, CHDR, atchan->mask);
  667. /* confirm that this channel is disabled */
  668. while (dma_readl(atdma, CHSR) & atchan->mask)
  669. cpu_relax();
  670. /* active_list entries will end up before queued entries */
  671. list_splice_init(&atchan->queue, &list);
  672. list_splice_init(&atchan->active_list, &list);
  673. /* Flush all pending and queued descriptors */
  674. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  675. atc_chain_complete(atchan, desc);
  676. spin_unlock_bh(&atchan->lock);
  677. return 0;
  678. }
  679. /**
  680. * atc_tx_status - poll for transaction completion
  681. * @chan: DMA channel
  682. * @cookie: transaction identifier to check status of
  683. * @txstate: if not %NULL updated with transaction state
  684. *
  685. * If @txstate is passed in, upon return it reflect the driver
  686. * internal state and can be used with dma_async_is_complete() to check
  687. * the status of multiple cookies without re-checking hardware state.
  688. */
  689. static enum dma_status
  690. atc_tx_status(struct dma_chan *chan,
  691. dma_cookie_t cookie,
  692. struct dma_tx_state *txstate)
  693. {
  694. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  695. dma_cookie_t last_used;
  696. dma_cookie_t last_complete;
  697. enum dma_status ret;
  698. spin_lock_bh(&atchan->lock);
  699. last_complete = atchan->completed_cookie;
  700. last_used = chan->cookie;
  701. ret = dma_async_is_complete(cookie, last_complete, last_used);
  702. if (ret != DMA_SUCCESS) {
  703. atc_cleanup_descriptors(atchan);
  704. last_complete = atchan->completed_cookie;
  705. last_used = chan->cookie;
  706. ret = dma_async_is_complete(cookie, last_complete, last_used);
  707. }
  708. spin_unlock_bh(&atchan->lock);
  709. dma_set_tx_state(txstate, last_complete, last_used, 0);
  710. dev_vdbg(chan2dev(chan), "tx_status: %d (d%d, u%d)\n",
  711. cookie, last_complete ? last_complete : 0,
  712. last_used ? last_used : 0);
  713. return ret;
  714. }
  715. /**
  716. * atc_issue_pending - try to finish work
  717. * @chan: target DMA channel
  718. */
  719. static void atc_issue_pending(struct dma_chan *chan)
  720. {
  721. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  722. dev_vdbg(chan2dev(chan), "issue_pending\n");
  723. if (!atc_chan_is_enabled(atchan)) {
  724. spin_lock_bh(&atchan->lock);
  725. atc_advance_work(atchan);
  726. spin_unlock_bh(&atchan->lock);
  727. }
  728. }
  729. /**
  730. * atc_alloc_chan_resources - allocate resources for DMA channel
  731. * @chan: allocate descriptor resources for this channel
  732. * @client: current client requesting the channel be ready for requests
  733. *
  734. * return - the number of allocated descriptors
  735. */
  736. static int atc_alloc_chan_resources(struct dma_chan *chan)
  737. {
  738. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  739. struct at_dma *atdma = to_at_dma(chan->device);
  740. struct at_desc *desc;
  741. struct at_dma_slave *atslave;
  742. int i;
  743. u32 cfg;
  744. LIST_HEAD(tmp_list);
  745. dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
  746. /* ASSERT: channel is idle */
  747. if (atc_chan_is_enabled(atchan)) {
  748. dev_dbg(chan2dev(chan), "DMA channel not idle ?\n");
  749. return -EIO;
  750. }
  751. cfg = ATC_DEFAULT_CFG;
  752. atslave = chan->private;
  753. if (atslave) {
  754. /*
  755. * We need controller-specific data to set up slave
  756. * transfers.
  757. */
  758. BUG_ON(!atslave->dma_dev || atslave->dma_dev != atdma->dma_common.dev);
  759. /* if cfg configuration specified take it instad of default */
  760. if (atslave->cfg)
  761. cfg = atslave->cfg;
  762. }
  763. /* have we already been set up?
  764. * reconfigure channel but no need to reallocate descriptors */
  765. if (!list_empty(&atchan->free_list))
  766. return atchan->descs_allocated;
  767. /* Allocate initial pool of descriptors */
  768. for (i = 0; i < init_nr_desc_per_channel; i++) {
  769. desc = atc_alloc_descriptor(chan, GFP_KERNEL);
  770. if (!desc) {
  771. dev_err(atdma->dma_common.dev,
  772. "Only %d initial descriptors\n", i);
  773. break;
  774. }
  775. list_add_tail(&desc->desc_node, &tmp_list);
  776. }
  777. spin_lock_bh(&atchan->lock);
  778. atchan->descs_allocated = i;
  779. list_splice(&tmp_list, &atchan->free_list);
  780. atchan->completed_cookie = chan->cookie = 1;
  781. spin_unlock_bh(&atchan->lock);
  782. /* channel parameters */
  783. channel_writel(atchan, CFG, cfg);
  784. dev_dbg(chan2dev(chan),
  785. "alloc_chan_resources: allocated %d descriptors\n",
  786. atchan->descs_allocated);
  787. return atchan->descs_allocated;
  788. }
  789. /**
  790. * atc_free_chan_resources - free all channel resources
  791. * @chan: DMA channel
  792. */
  793. static void atc_free_chan_resources(struct dma_chan *chan)
  794. {
  795. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  796. struct at_dma *atdma = to_at_dma(chan->device);
  797. struct at_desc *desc, *_desc;
  798. LIST_HEAD(list);
  799. dev_dbg(chan2dev(chan), "free_chan_resources: (descs allocated=%u)\n",
  800. atchan->descs_allocated);
  801. /* ASSERT: channel is idle */
  802. BUG_ON(!list_empty(&atchan->active_list));
  803. BUG_ON(!list_empty(&atchan->queue));
  804. BUG_ON(atc_chan_is_enabled(atchan));
  805. list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
  806. dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
  807. list_del(&desc->desc_node);
  808. /* free link descriptor */
  809. dma_pool_free(atdma->dma_desc_pool, desc, desc->txd.phys);
  810. }
  811. list_splice_init(&atchan->free_list, &list);
  812. atchan->descs_allocated = 0;
  813. dev_vdbg(chan2dev(chan), "free_chan_resources: done\n");
  814. }
  815. /*-- Module Management -----------------------------------------------*/
  816. /**
  817. * at_dma_off - disable DMA controller
  818. * @atdma: the Atmel HDAMC device
  819. */
  820. static void at_dma_off(struct at_dma *atdma)
  821. {
  822. dma_writel(atdma, EN, 0);
  823. /* disable all interrupts */
  824. dma_writel(atdma, EBCIDR, -1L);
  825. /* confirm that all channels are disabled */
  826. while (dma_readl(atdma, CHSR) & atdma->all_chan_mask)
  827. cpu_relax();
  828. }
  829. static int __init at_dma_probe(struct platform_device *pdev)
  830. {
  831. struct at_dma_platform_data *pdata;
  832. struct resource *io;
  833. struct at_dma *atdma;
  834. size_t size;
  835. int irq;
  836. int err;
  837. int i;
  838. /* get DMA Controller parameters from platform */
  839. pdata = pdev->dev.platform_data;
  840. if (!pdata || pdata->nr_channels > AT_DMA_MAX_NR_CHANNELS)
  841. return -EINVAL;
  842. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  843. if (!io)
  844. return -EINVAL;
  845. irq = platform_get_irq(pdev, 0);
  846. if (irq < 0)
  847. return irq;
  848. size = sizeof(struct at_dma);
  849. size += pdata->nr_channels * sizeof(struct at_dma_chan);
  850. atdma = kzalloc(size, GFP_KERNEL);
  851. if (!atdma)
  852. return -ENOMEM;
  853. /* discover transaction capabilites from the platform data */
  854. atdma->dma_common.cap_mask = pdata->cap_mask;
  855. atdma->all_chan_mask = (1 << pdata->nr_channels) - 1;
  856. size = io->end - io->start + 1;
  857. if (!request_mem_region(io->start, size, pdev->dev.driver->name)) {
  858. err = -EBUSY;
  859. goto err_kfree;
  860. }
  861. atdma->regs = ioremap(io->start, size);
  862. if (!atdma->regs) {
  863. err = -ENOMEM;
  864. goto err_release_r;
  865. }
  866. atdma->clk = clk_get(&pdev->dev, "dma_clk");
  867. if (IS_ERR(atdma->clk)) {
  868. err = PTR_ERR(atdma->clk);
  869. goto err_clk;
  870. }
  871. clk_enable(atdma->clk);
  872. /* force dma off, just in case */
  873. at_dma_off(atdma);
  874. err = request_irq(irq, at_dma_interrupt, 0, "at_hdmac", atdma);
  875. if (err)
  876. goto err_irq;
  877. platform_set_drvdata(pdev, atdma);
  878. /* create a pool of consistent memory blocks for hardware descriptors */
  879. atdma->dma_desc_pool = dma_pool_create("at_hdmac_desc_pool",
  880. &pdev->dev, sizeof(struct at_desc),
  881. 4 /* word alignment */, 0);
  882. if (!atdma->dma_desc_pool) {
  883. dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
  884. err = -ENOMEM;
  885. goto err_pool_create;
  886. }
  887. /* clear any pending interrupt */
  888. while (dma_readl(atdma, EBCISR))
  889. cpu_relax();
  890. /* initialize channels related values */
  891. INIT_LIST_HEAD(&atdma->dma_common.channels);
  892. for (i = 0; i < pdata->nr_channels; i++, atdma->dma_common.chancnt++) {
  893. struct at_dma_chan *atchan = &atdma->chan[i];
  894. atchan->chan_common.device = &atdma->dma_common;
  895. atchan->chan_common.cookie = atchan->completed_cookie = 1;
  896. atchan->chan_common.chan_id = i;
  897. list_add_tail(&atchan->chan_common.device_node,
  898. &atdma->dma_common.channels);
  899. atchan->ch_regs = atdma->regs + ch_regs(i);
  900. spin_lock_init(&atchan->lock);
  901. atchan->mask = 1 << i;
  902. INIT_LIST_HEAD(&atchan->active_list);
  903. INIT_LIST_HEAD(&atchan->queue);
  904. INIT_LIST_HEAD(&atchan->free_list);
  905. tasklet_init(&atchan->tasklet, atc_tasklet,
  906. (unsigned long)atchan);
  907. atc_enable_irq(atchan);
  908. }
  909. /* set base routines */
  910. atdma->dma_common.device_alloc_chan_resources = atc_alloc_chan_resources;
  911. atdma->dma_common.device_free_chan_resources = atc_free_chan_resources;
  912. atdma->dma_common.device_tx_status = atc_tx_status;
  913. atdma->dma_common.device_issue_pending = atc_issue_pending;
  914. atdma->dma_common.dev = &pdev->dev;
  915. /* set prep routines based on capability */
  916. if (dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask))
  917. atdma->dma_common.device_prep_dma_memcpy = atc_prep_dma_memcpy;
  918. if (dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask)) {
  919. atdma->dma_common.device_prep_slave_sg = atc_prep_slave_sg;
  920. atdma->dma_common.device_control = atc_control;
  921. }
  922. dma_writel(atdma, EN, AT_DMA_ENABLE);
  923. dev_info(&pdev->dev, "Atmel AHB DMA Controller ( %s%s), %d channels\n",
  924. dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask) ? "cpy " : "",
  925. dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask) ? "slave " : "",
  926. atdma->dma_common.chancnt);
  927. dma_async_device_register(&atdma->dma_common);
  928. return 0;
  929. err_pool_create:
  930. platform_set_drvdata(pdev, NULL);
  931. free_irq(platform_get_irq(pdev, 0), atdma);
  932. err_irq:
  933. clk_disable(atdma->clk);
  934. clk_put(atdma->clk);
  935. err_clk:
  936. iounmap(atdma->regs);
  937. atdma->regs = NULL;
  938. err_release_r:
  939. release_mem_region(io->start, size);
  940. err_kfree:
  941. kfree(atdma);
  942. return err;
  943. }
  944. static int __exit at_dma_remove(struct platform_device *pdev)
  945. {
  946. struct at_dma *atdma = platform_get_drvdata(pdev);
  947. struct dma_chan *chan, *_chan;
  948. struct resource *io;
  949. at_dma_off(atdma);
  950. dma_async_device_unregister(&atdma->dma_common);
  951. dma_pool_destroy(atdma->dma_desc_pool);
  952. platform_set_drvdata(pdev, NULL);
  953. free_irq(platform_get_irq(pdev, 0), atdma);
  954. list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
  955. device_node) {
  956. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  957. /* Disable interrupts */
  958. atc_disable_irq(atchan);
  959. tasklet_disable(&atchan->tasklet);
  960. tasklet_kill(&atchan->tasklet);
  961. list_del(&chan->device_node);
  962. }
  963. clk_disable(atdma->clk);
  964. clk_put(atdma->clk);
  965. iounmap(atdma->regs);
  966. atdma->regs = NULL;
  967. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  968. release_mem_region(io->start, io->end - io->start + 1);
  969. kfree(atdma);
  970. return 0;
  971. }
  972. static void at_dma_shutdown(struct platform_device *pdev)
  973. {
  974. struct at_dma *atdma = platform_get_drvdata(pdev);
  975. at_dma_off(platform_get_drvdata(pdev));
  976. clk_disable(atdma->clk);
  977. }
  978. static int at_dma_suspend_noirq(struct device *dev)
  979. {
  980. struct platform_device *pdev = to_platform_device(dev);
  981. struct at_dma *atdma = platform_get_drvdata(pdev);
  982. at_dma_off(platform_get_drvdata(pdev));
  983. clk_disable(atdma->clk);
  984. return 0;
  985. }
  986. static int at_dma_resume_noirq(struct device *dev)
  987. {
  988. struct platform_device *pdev = to_platform_device(dev);
  989. struct at_dma *atdma = platform_get_drvdata(pdev);
  990. clk_enable(atdma->clk);
  991. dma_writel(atdma, EN, AT_DMA_ENABLE);
  992. return 0;
  993. }
  994. static const struct dev_pm_ops at_dma_dev_pm_ops = {
  995. .suspend_noirq = at_dma_suspend_noirq,
  996. .resume_noirq = at_dma_resume_noirq,
  997. };
  998. static struct platform_driver at_dma_driver = {
  999. .remove = __exit_p(at_dma_remove),
  1000. .shutdown = at_dma_shutdown,
  1001. .driver = {
  1002. .name = "at_hdmac",
  1003. .pm = &at_dma_dev_pm_ops,
  1004. },
  1005. };
  1006. static int __init at_dma_init(void)
  1007. {
  1008. return platform_driver_probe(&at_dma_driver, at_dma_probe);
  1009. }
  1010. module_init(at_dma_init);
  1011. static void __exit at_dma_exit(void)
  1012. {
  1013. platform_driver_unregister(&at_dma_driver);
  1014. }
  1015. module_exit(at_dma_exit);
  1016. MODULE_DESCRIPTION("Atmel AHB DMA Controller driver");
  1017. MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
  1018. MODULE_LICENSE("GPL");
  1019. MODULE_ALIAS("platform:at_hdmac");