at_hdmac.c 32 KB

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