shdma.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  1. /*
  2. * Renesas SuperH DMA Engine support
  3. *
  4. * base is drivers/dma/flsdma.c
  5. *
  6. * Copyright (C) 2009 Nobuhiro Iwamatsu <iwamatsu.nobuhiro@renesas.com>
  7. * Copyright (C) 2009 Renesas Solutions, Inc. All rights reserved.
  8. * Copyright (C) 2007 Freescale Semiconductor, Inc. All rights reserved.
  9. *
  10. * This is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * - DMA of SuperH does not have Hardware DMA chain mode.
  16. * - MAX DMA size is 16MB.
  17. *
  18. */
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/slab.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/dmaengine.h>
  24. #include <linux/delay.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/pm_runtime.h>
  28. #include <asm/dmaengine.h>
  29. #include "shdma.h"
  30. /* DMA descriptor control */
  31. enum sh_dmae_desc_status {
  32. DESC_IDLE,
  33. DESC_PREPARED,
  34. DESC_SUBMITTED,
  35. DESC_COMPLETED, /* completed, have to call callback */
  36. DESC_WAITING, /* callback called, waiting for ack / re-submit */
  37. };
  38. #define NR_DESCS_PER_CHANNEL 32
  39. /* Default MEMCPY transfer size = 2^2 = 4 bytes */
  40. #define LOG2_DEFAULT_XFER_SIZE 2
  41. /* A bitmask with bits enough for enum sh_dmae_slave_chan_id */
  42. static unsigned long sh_dmae_slave_used[BITS_TO_LONGS(SHDMA_SLAVE_NUMBER)];
  43. static void sh_dmae_chan_ld_cleanup(struct sh_dmae_chan *sh_chan, bool all);
  44. static void sh_dmae_writel(struct sh_dmae_chan *sh_dc, u32 data, u32 reg)
  45. {
  46. __raw_writel(data, sh_dc->base + reg / sizeof(u32));
  47. }
  48. static u32 sh_dmae_readl(struct sh_dmae_chan *sh_dc, u32 reg)
  49. {
  50. return __raw_readl(sh_dc->base + reg / sizeof(u32));
  51. }
  52. static u16 dmaor_read(struct sh_dmae_device *shdev)
  53. {
  54. return __raw_readw(shdev->chan_reg + DMAOR / sizeof(u32));
  55. }
  56. static void dmaor_write(struct sh_dmae_device *shdev, u16 data)
  57. {
  58. __raw_writew(data, shdev->chan_reg + DMAOR / sizeof(u32));
  59. }
  60. /*
  61. * Reset DMA controller
  62. *
  63. * SH7780 has two DMAOR register
  64. */
  65. static void sh_dmae_ctl_stop(struct sh_dmae_device *shdev)
  66. {
  67. unsigned short dmaor = dmaor_read(shdev);
  68. dmaor_write(shdev, dmaor & ~(DMAOR_NMIF | DMAOR_AE | DMAOR_DME));
  69. }
  70. static int sh_dmae_rst(struct sh_dmae_device *shdev)
  71. {
  72. unsigned short dmaor;
  73. sh_dmae_ctl_stop(shdev);
  74. dmaor = dmaor_read(shdev) | shdev->pdata->dmaor_init;
  75. dmaor_write(shdev, dmaor);
  76. if (dmaor_read(shdev) & (DMAOR_AE | DMAOR_NMIF)) {
  77. pr_warning("dma-sh: Can't initialize DMAOR.\n");
  78. return -EINVAL;
  79. }
  80. return 0;
  81. }
  82. static bool dmae_is_busy(struct sh_dmae_chan *sh_chan)
  83. {
  84. u32 chcr = sh_dmae_readl(sh_chan, CHCR);
  85. if ((chcr & (CHCR_DE | CHCR_TE)) == CHCR_DE)
  86. return true; /* working */
  87. return false; /* waiting */
  88. }
  89. static unsigned int calc_xmit_shift(struct sh_dmae_chan *sh_chan, u32 chcr)
  90. {
  91. struct sh_dmae_device *shdev = container_of(sh_chan->common.device,
  92. struct sh_dmae_device, common);
  93. struct sh_dmae_pdata *pdata = shdev->pdata;
  94. int cnt = ((chcr & pdata->ts_low_mask) >> pdata->ts_low_shift) |
  95. ((chcr & pdata->ts_high_mask) >> pdata->ts_high_shift);
  96. if (cnt >= pdata->ts_shift_num)
  97. cnt = 0;
  98. return pdata->ts_shift[cnt];
  99. }
  100. static u32 log2size_to_chcr(struct sh_dmae_chan *sh_chan, int l2size)
  101. {
  102. struct sh_dmae_device *shdev = container_of(sh_chan->common.device,
  103. struct sh_dmae_device, common);
  104. struct sh_dmae_pdata *pdata = shdev->pdata;
  105. int i;
  106. for (i = 0; i < pdata->ts_shift_num; i++)
  107. if (pdata->ts_shift[i] == l2size)
  108. break;
  109. if (i == pdata->ts_shift_num)
  110. i = 0;
  111. return ((i << pdata->ts_low_shift) & pdata->ts_low_mask) |
  112. ((i << pdata->ts_high_shift) & pdata->ts_high_mask);
  113. }
  114. static void dmae_set_reg(struct sh_dmae_chan *sh_chan, struct sh_dmae_regs *hw)
  115. {
  116. sh_dmae_writel(sh_chan, hw->sar, SAR);
  117. sh_dmae_writel(sh_chan, hw->dar, DAR);
  118. sh_dmae_writel(sh_chan, hw->tcr >> sh_chan->xmit_shift, TCR);
  119. }
  120. static void dmae_start(struct sh_dmae_chan *sh_chan)
  121. {
  122. u32 chcr = sh_dmae_readl(sh_chan, CHCR);
  123. chcr |= CHCR_DE | CHCR_IE;
  124. sh_dmae_writel(sh_chan, chcr & ~CHCR_TE, CHCR);
  125. }
  126. static void dmae_halt(struct sh_dmae_chan *sh_chan)
  127. {
  128. u32 chcr = sh_dmae_readl(sh_chan, CHCR);
  129. chcr &= ~(CHCR_DE | CHCR_TE | CHCR_IE);
  130. sh_dmae_writel(sh_chan, chcr, CHCR);
  131. }
  132. static void dmae_init(struct sh_dmae_chan *sh_chan)
  133. {
  134. /*
  135. * Default configuration for dual address memory-memory transfer.
  136. * 0x400 represents auto-request.
  137. */
  138. u32 chcr = DM_INC | SM_INC | 0x400 | log2size_to_chcr(sh_chan,
  139. LOG2_DEFAULT_XFER_SIZE);
  140. sh_chan->xmit_shift = calc_xmit_shift(sh_chan, chcr);
  141. sh_dmae_writel(sh_chan, chcr, CHCR);
  142. }
  143. static int dmae_set_chcr(struct sh_dmae_chan *sh_chan, u32 val)
  144. {
  145. /* When DMA was working, can not set data to CHCR */
  146. if (dmae_is_busy(sh_chan))
  147. return -EBUSY;
  148. sh_chan->xmit_shift = calc_xmit_shift(sh_chan, val);
  149. sh_dmae_writel(sh_chan, val, CHCR);
  150. return 0;
  151. }
  152. static int dmae_set_dmars(struct sh_dmae_chan *sh_chan, u16 val)
  153. {
  154. struct sh_dmae_device *shdev = container_of(sh_chan->common.device,
  155. struct sh_dmae_device, common);
  156. struct sh_dmae_pdata *pdata = shdev->pdata;
  157. struct sh_dmae_channel *chan_pdata = &pdata->channel[sh_chan->id];
  158. u16 __iomem *addr = shdev->dmars + chan_pdata->dmars / sizeof(u16);
  159. int shift = chan_pdata->dmars_bit;
  160. if (dmae_is_busy(sh_chan))
  161. return -EBUSY;
  162. __raw_writew((__raw_readw(addr) & (0xff00 >> shift)) | (val << shift),
  163. addr);
  164. return 0;
  165. }
  166. static dma_cookie_t sh_dmae_tx_submit(struct dma_async_tx_descriptor *tx)
  167. {
  168. struct sh_desc *desc = tx_to_sh_desc(tx), *chunk, *last = desc, *c;
  169. struct sh_dmae_chan *sh_chan = to_sh_chan(tx->chan);
  170. dma_async_tx_callback callback = tx->callback;
  171. dma_cookie_t cookie;
  172. spin_lock_bh(&sh_chan->desc_lock);
  173. cookie = sh_chan->common.cookie;
  174. cookie++;
  175. if (cookie < 0)
  176. cookie = 1;
  177. sh_chan->common.cookie = cookie;
  178. tx->cookie = cookie;
  179. /* Mark all chunks of this descriptor as submitted, move to the queue */
  180. list_for_each_entry_safe(chunk, c, desc->node.prev, node) {
  181. /*
  182. * All chunks are on the global ld_free, so, we have to find
  183. * the end of the chain ourselves
  184. */
  185. if (chunk != desc && (chunk->mark == DESC_IDLE ||
  186. chunk->async_tx.cookie > 0 ||
  187. chunk->async_tx.cookie == -EBUSY ||
  188. &chunk->node == &sh_chan->ld_free))
  189. break;
  190. chunk->mark = DESC_SUBMITTED;
  191. /* Callback goes to the last chunk */
  192. chunk->async_tx.callback = NULL;
  193. chunk->cookie = cookie;
  194. list_move_tail(&chunk->node, &sh_chan->ld_queue);
  195. last = chunk;
  196. }
  197. last->async_tx.callback = callback;
  198. last->async_tx.callback_param = tx->callback_param;
  199. dev_dbg(sh_chan->dev, "submit #%d@%p on %d: %x[%d] -> %x\n",
  200. tx->cookie, &last->async_tx, sh_chan->id,
  201. desc->hw.sar, desc->hw.tcr, desc->hw.dar);
  202. spin_unlock_bh(&sh_chan->desc_lock);
  203. return cookie;
  204. }
  205. /* Called with desc_lock held */
  206. static struct sh_desc *sh_dmae_get_desc(struct sh_dmae_chan *sh_chan)
  207. {
  208. struct sh_desc *desc;
  209. list_for_each_entry(desc, &sh_chan->ld_free, node)
  210. if (desc->mark != DESC_PREPARED) {
  211. BUG_ON(desc->mark != DESC_IDLE);
  212. list_del(&desc->node);
  213. return desc;
  214. }
  215. return NULL;
  216. }
  217. static struct sh_dmae_slave_config *sh_dmae_find_slave(
  218. struct sh_dmae_chan *sh_chan, enum sh_dmae_slave_chan_id slave_id)
  219. {
  220. struct dma_device *dma_dev = sh_chan->common.device;
  221. struct sh_dmae_device *shdev = container_of(dma_dev,
  222. struct sh_dmae_device, common);
  223. struct sh_dmae_pdata *pdata = shdev->pdata;
  224. int i;
  225. if ((unsigned)slave_id >= SHDMA_SLAVE_NUMBER)
  226. return NULL;
  227. for (i = 0; i < pdata->slave_num; i++)
  228. if (pdata->slave[i].slave_id == slave_id)
  229. return pdata->slave + i;
  230. return NULL;
  231. }
  232. static int sh_dmae_alloc_chan_resources(struct dma_chan *chan)
  233. {
  234. struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
  235. struct sh_desc *desc;
  236. struct sh_dmae_slave *param = chan->private;
  237. pm_runtime_get_sync(sh_chan->dev);
  238. /*
  239. * This relies on the guarantee from dmaengine that alloc_chan_resources
  240. * never runs concurrently with itself or free_chan_resources.
  241. */
  242. if (param) {
  243. struct sh_dmae_slave_config *cfg;
  244. cfg = sh_dmae_find_slave(sh_chan, param->slave_id);
  245. if (!cfg)
  246. return -EINVAL;
  247. if (test_and_set_bit(param->slave_id, sh_dmae_slave_used))
  248. return -EBUSY;
  249. param->config = cfg;
  250. dmae_set_dmars(sh_chan, cfg->mid_rid);
  251. dmae_set_chcr(sh_chan, cfg->chcr);
  252. } else if ((sh_dmae_readl(sh_chan, CHCR) & 0xf00) != 0x400) {
  253. dmae_init(sh_chan);
  254. }
  255. spin_lock_bh(&sh_chan->desc_lock);
  256. while (sh_chan->descs_allocated < NR_DESCS_PER_CHANNEL) {
  257. spin_unlock_bh(&sh_chan->desc_lock);
  258. desc = kzalloc(sizeof(struct sh_desc), GFP_KERNEL);
  259. if (!desc) {
  260. spin_lock_bh(&sh_chan->desc_lock);
  261. break;
  262. }
  263. dma_async_tx_descriptor_init(&desc->async_tx,
  264. &sh_chan->common);
  265. desc->async_tx.tx_submit = sh_dmae_tx_submit;
  266. desc->mark = DESC_IDLE;
  267. spin_lock_bh(&sh_chan->desc_lock);
  268. list_add(&desc->node, &sh_chan->ld_free);
  269. sh_chan->descs_allocated++;
  270. }
  271. spin_unlock_bh(&sh_chan->desc_lock);
  272. if (!sh_chan->descs_allocated)
  273. pm_runtime_put(sh_chan->dev);
  274. return sh_chan->descs_allocated;
  275. }
  276. /*
  277. * sh_dma_free_chan_resources - Free all resources of the channel.
  278. */
  279. static void sh_dmae_free_chan_resources(struct dma_chan *chan)
  280. {
  281. struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
  282. struct sh_desc *desc, *_desc;
  283. LIST_HEAD(list);
  284. int descs = sh_chan->descs_allocated;
  285. dmae_halt(sh_chan);
  286. /* Prepared and not submitted descriptors can still be on the queue */
  287. if (!list_empty(&sh_chan->ld_queue))
  288. sh_dmae_chan_ld_cleanup(sh_chan, true);
  289. if (chan->private) {
  290. /* The caller is holding dma_list_mutex */
  291. struct sh_dmae_slave *param = chan->private;
  292. clear_bit(param->slave_id, sh_dmae_slave_used);
  293. }
  294. spin_lock_bh(&sh_chan->desc_lock);
  295. list_splice_init(&sh_chan->ld_free, &list);
  296. sh_chan->descs_allocated = 0;
  297. spin_unlock_bh(&sh_chan->desc_lock);
  298. if (descs > 0)
  299. pm_runtime_put(sh_chan->dev);
  300. list_for_each_entry_safe(desc, _desc, &list, node)
  301. kfree(desc);
  302. }
  303. /**
  304. * sh_dmae_add_desc - get, set up and return one transfer descriptor
  305. * @sh_chan: DMA channel
  306. * @flags: DMA transfer flags
  307. * @dest: destination DMA address, incremented when direction equals
  308. * DMA_FROM_DEVICE or DMA_BIDIRECTIONAL
  309. * @src: source DMA address, incremented when direction equals
  310. * DMA_TO_DEVICE or DMA_BIDIRECTIONAL
  311. * @len: DMA transfer length
  312. * @first: if NULL, set to the current descriptor and cookie set to -EBUSY
  313. * @direction: needed for slave DMA to decide which address to keep constant,
  314. * equals DMA_BIDIRECTIONAL for MEMCPY
  315. * Returns 0 or an error
  316. * Locks: called with desc_lock held
  317. */
  318. static struct sh_desc *sh_dmae_add_desc(struct sh_dmae_chan *sh_chan,
  319. unsigned long flags, dma_addr_t *dest, dma_addr_t *src, size_t *len,
  320. struct sh_desc **first, enum dma_data_direction direction)
  321. {
  322. struct sh_desc *new;
  323. size_t copy_size;
  324. if (!*len)
  325. return NULL;
  326. /* Allocate the link descriptor from the free list */
  327. new = sh_dmae_get_desc(sh_chan);
  328. if (!new) {
  329. dev_err(sh_chan->dev, "No free link descriptor available\n");
  330. return NULL;
  331. }
  332. copy_size = min(*len, (size_t)SH_DMA_TCR_MAX + 1);
  333. new->hw.sar = *src;
  334. new->hw.dar = *dest;
  335. new->hw.tcr = copy_size;
  336. if (!*first) {
  337. /* First desc */
  338. new->async_tx.cookie = -EBUSY;
  339. *first = new;
  340. } else {
  341. /* Other desc - invisible to the user */
  342. new->async_tx.cookie = -EINVAL;
  343. }
  344. dev_dbg(sh_chan->dev,
  345. "chaining (%u/%u)@%x -> %x with %p, cookie %d, shift %d\n",
  346. copy_size, *len, *src, *dest, &new->async_tx,
  347. new->async_tx.cookie, sh_chan->xmit_shift);
  348. new->mark = DESC_PREPARED;
  349. new->async_tx.flags = flags;
  350. new->direction = direction;
  351. *len -= copy_size;
  352. if (direction == DMA_BIDIRECTIONAL || direction == DMA_TO_DEVICE)
  353. *src += copy_size;
  354. if (direction == DMA_BIDIRECTIONAL || direction == DMA_FROM_DEVICE)
  355. *dest += copy_size;
  356. return new;
  357. }
  358. /*
  359. * sh_dmae_prep_sg - prepare transfer descriptors from an SG list
  360. *
  361. * Common routine for public (MEMCPY) and slave DMA. The MEMCPY case is also
  362. * converted to scatter-gather to guarantee consistent locking and a correct
  363. * list manipulation. For slave DMA direction carries the usual meaning, and,
  364. * logically, the SG list is RAM and the addr variable contains slave address,
  365. * e.g., the FIFO I/O register. For MEMCPY direction equals DMA_BIDIRECTIONAL
  366. * and the SG list contains only one element and points at the source buffer.
  367. */
  368. static struct dma_async_tx_descriptor *sh_dmae_prep_sg(struct sh_dmae_chan *sh_chan,
  369. struct scatterlist *sgl, unsigned int sg_len, dma_addr_t *addr,
  370. enum dma_data_direction direction, unsigned long flags)
  371. {
  372. struct scatterlist *sg;
  373. struct sh_desc *first = NULL, *new = NULL /* compiler... */;
  374. LIST_HEAD(tx_list);
  375. int chunks = 0;
  376. int i;
  377. if (!sg_len)
  378. return NULL;
  379. for_each_sg(sgl, sg, sg_len, i)
  380. chunks += (sg_dma_len(sg) + SH_DMA_TCR_MAX) /
  381. (SH_DMA_TCR_MAX + 1);
  382. /* Have to lock the whole loop to protect against concurrent release */
  383. spin_lock_bh(&sh_chan->desc_lock);
  384. /*
  385. * Chaining:
  386. * first descriptor is what user is dealing with in all API calls, its
  387. * cookie is at first set to -EBUSY, at tx-submit to a positive
  388. * number
  389. * if more than one chunk is needed further chunks have cookie = -EINVAL
  390. * the last chunk, if not equal to the first, has cookie = -ENOSPC
  391. * all chunks are linked onto the tx_list head with their .node heads
  392. * only during this function, then they are immediately spliced
  393. * back onto the free list in form of a chain
  394. */
  395. for_each_sg(sgl, sg, sg_len, i) {
  396. dma_addr_t sg_addr = sg_dma_address(sg);
  397. size_t len = sg_dma_len(sg);
  398. if (!len)
  399. goto err_get_desc;
  400. do {
  401. dev_dbg(sh_chan->dev, "Add SG #%d@%p[%d], dma %llx\n",
  402. i, sg, len, (unsigned long long)sg_addr);
  403. if (direction == DMA_FROM_DEVICE)
  404. new = sh_dmae_add_desc(sh_chan, flags,
  405. &sg_addr, addr, &len, &first,
  406. direction);
  407. else
  408. new = sh_dmae_add_desc(sh_chan, flags,
  409. addr, &sg_addr, &len, &first,
  410. direction);
  411. if (!new)
  412. goto err_get_desc;
  413. new->chunks = chunks--;
  414. list_add_tail(&new->node, &tx_list);
  415. } while (len);
  416. }
  417. if (new != first)
  418. new->async_tx.cookie = -ENOSPC;
  419. /* Put them back on the free list, so, they don't get lost */
  420. list_splice_tail(&tx_list, &sh_chan->ld_free);
  421. spin_unlock_bh(&sh_chan->desc_lock);
  422. return &first->async_tx;
  423. err_get_desc:
  424. list_for_each_entry(new, &tx_list, node)
  425. new->mark = DESC_IDLE;
  426. list_splice(&tx_list, &sh_chan->ld_free);
  427. spin_unlock_bh(&sh_chan->desc_lock);
  428. return NULL;
  429. }
  430. static struct dma_async_tx_descriptor *sh_dmae_prep_memcpy(
  431. struct dma_chan *chan, dma_addr_t dma_dest, dma_addr_t dma_src,
  432. size_t len, unsigned long flags)
  433. {
  434. struct sh_dmae_chan *sh_chan;
  435. struct scatterlist sg;
  436. if (!chan || !len)
  437. return NULL;
  438. chan->private = NULL;
  439. sh_chan = to_sh_chan(chan);
  440. sg_init_table(&sg, 1);
  441. sg_set_page(&sg, pfn_to_page(PFN_DOWN(dma_src)), len,
  442. offset_in_page(dma_src));
  443. sg_dma_address(&sg) = dma_src;
  444. sg_dma_len(&sg) = len;
  445. return sh_dmae_prep_sg(sh_chan, &sg, 1, &dma_dest, DMA_BIDIRECTIONAL,
  446. flags);
  447. }
  448. static struct dma_async_tx_descriptor *sh_dmae_prep_slave_sg(
  449. struct dma_chan *chan, struct scatterlist *sgl, unsigned int sg_len,
  450. enum dma_data_direction direction, unsigned long flags)
  451. {
  452. struct sh_dmae_slave *param;
  453. struct sh_dmae_chan *sh_chan;
  454. if (!chan)
  455. return NULL;
  456. sh_chan = to_sh_chan(chan);
  457. param = chan->private;
  458. /* Someone calling slave DMA on a public channel? */
  459. if (!param || !sg_len) {
  460. dev_warn(sh_chan->dev, "%s: bad parameter: %p, %d, %d\n",
  461. __func__, param, sg_len, param ? param->slave_id : -1);
  462. return NULL;
  463. }
  464. /*
  465. * if (param != NULL), this is a successfully requested slave channel,
  466. * therefore param->config != NULL too.
  467. */
  468. return sh_dmae_prep_sg(sh_chan, sgl, sg_len, &param->config->addr,
  469. direction, flags);
  470. }
  471. static void sh_dmae_terminate_all(struct dma_chan *chan)
  472. {
  473. struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
  474. if (!chan)
  475. return;
  476. dmae_halt(sh_chan);
  477. spin_lock_bh(&sh_chan->desc_lock);
  478. if (!list_empty(&sh_chan->ld_queue)) {
  479. /* Record partial transfer */
  480. struct sh_desc *desc = list_entry(sh_chan->ld_queue.next,
  481. struct sh_desc, node);
  482. desc->partial = (desc->hw.tcr - sh_dmae_readl(sh_chan, TCR)) <<
  483. sh_chan->xmit_shift;
  484. }
  485. spin_unlock_bh(&sh_chan->desc_lock);
  486. sh_dmae_chan_ld_cleanup(sh_chan, true);
  487. }
  488. static dma_async_tx_callback __ld_cleanup(struct sh_dmae_chan *sh_chan, bool all)
  489. {
  490. struct sh_desc *desc, *_desc;
  491. /* Is the "exposed" head of a chain acked? */
  492. bool head_acked = false;
  493. dma_cookie_t cookie = 0;
  494. dma_async_tx_callback callback = NULL;
  495. void *param = NULL;
  496. spin_lock_bh(&sh_chan->desc_lock);
  497. list_for_each_entry_safe(desc, _desc, &sh_chan->ld_queue, node) {
  498. struct dma_async_tx_descriptor *tx = &desc->async_tx;
  499. BUG_ON(tx->cookie > 0 && tx->cookie != desc->cookie);
  500. BUG_ON(desc->mark != DESC_SUBMITTED &&
  501. desc->mark != DESC_COMPLETED &&
  502. desc->mark != DESC_WAITING);
  503. /*
  504. * queue is ordered, and we use this loop to (1) clean up all
  505. * completed descriptors, and to (2) update descriptor flags of
  506. * any chunks in a (partially) completed chain
  507. */
  508. if (!all && desc->mark == DESC_SUBMITTED &&
  509. desc->cookie != cookie)
  510. break;
  511. if (tx->cookie > 0)
  512. cookie = tx->cookie;
  513. if (desc->mark == DESC_COMPLETED && desc->chunks == 1) {
  514. if (sh_chan->completed_cookie != desc->cookie - 1)
  515. dev_dbg(sh_chan->dev,
  516. "Completing cookie %d, expected %d\n",
  517. desc->cookie,
  518. sh_chan->completed_cookie + 1);
  519. sh_chan->completed_cookie = desc->cookie;
  520. }
  521. /* Call callback on the last chunk */
  522. if (desc->mark == DESC_COMPLETED && tx->callback) {
  523. desc->mark = DESC_WAITING;
  524. callback = tx->callback;
  525. param = tx->callback_param;
  526. dev_dbg(sh_chan->dev, "descriptor #%d@%p on %d callback\n",
  527. tx->cookie, tx, sh_chan->id);
  528. BUG_ON(desc->chunks != 1);
  529. break;
  530. }
  531. if (tx->cookie > 0 || tx->cookie == -EBUSY) {
  532. if (desc->mark == DESC_COMPLETED) {
  533. BUG_ON(tx->cookie < 0);
  534. desc->mark = DESC_WAITING;
  535. }
  536. head_acked = async_tx_test_ack(tx);
  537. } else {
  538. switch (desc->mark) {
  539. case DESC_COMPLETED:
  540. desc->mark = DESC_WAITING;
  541. /* Fall through */
  542. case DESC_WAITING:
  543. if (head_acked)
  544. async_tx_ack(&desc->async_tx);
  545. }
  546. }
  547. dev_dbg(sh_chan->dev, "descriptor %p #%d completed.\n",
  548. tx, tx->cookie);
  549. if (((desc->mark == DESC_COMPLETED ||
  550. desc->mark == DESC_WAITING) &&
  551. async_tx_test_ack(&desc->async_tx)) || all) {
  552. /* Remove from ld_queue list */
  553. desc->mark = DESC_IDLE;
  554. list_move(&desc->node, &sh_chan->ld_free);
  555. }
  556. }
  557. spin_unlock_bh(&sh_chan->desc_lock);
  558. if (callback)
  559. callback(param);
  560. return callback;
  561. }
  562. /*
  563. * sh_chan_ld_cleanup - Clean up link descriptors
  564. *
  565. * This function cleans up the ld_queue of DMA channel.
  566. */
  567. static void sh_dmae_chan_ld_cleanup(struct sh_dmae_chan *sh_chan, bool all)
  568. {
  569. while (__ld_cleanup(sh_chan, all))
  570. ;
  571. }
  572. static void sh_chan_xfer_ld_queue(struct sh_dmae_chan *sh_chan)
  573. {
  574. struct sh_desc *desc;
  575. spin_lock_bh(&sh_chan->desc_lock);
  576. /* DMA work check */
  577. if (dmae_is_busy(sh_chan)) {
  578. spin_unlock_bh(&sh_chan->desc_lock);
  579. return;
  580. }
  581. /* Find the first not transferred desciptor */
  582. list_for_each_entry(desc, &sh_chan->ld_queue, node)
  583. if (desc->mark == DESC_SUBMITTED) {
  584. dev_dbg(sh_chan->dev, "Queue #%d to %d: %u@%x -> %x\n",
  585. desc->async_tx.cookie, sh_chan->id,
  586. desc->hw.tcr, desc->hw.sar, desc->hw.dar);
  587. /* Get the ld start address from ld_queue */
  588. dmae_set_reg(sh_chan, &desc->hw);
  589. dmae_start(sh_chan);
  590. break;
  591. }
  592. spin_unlock_bh(&sh_chan->desc_lock);
  593. }
  594. static void sh_dmae_memcpy_issue_pending(struct dma_chan *chan)
  595. {
  596. struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
  597. sh_chan_xfer_ld_queue(sh_chan);
  598. }
  599. static enum dma_status sh_dmae_is_complete(struct dma_chan *chan,
  600. dma_cookie_t cookie,
  601. dma_cookie_t *done,
  602. dma_cookie_t *used)
  603. {
  604. struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
  605. dma_cookie_t last_used;
  606. dma_cookie_t last_complete;
  607. enum dma_status status;
  608. sh_dmae_chan_ld_cleanup(sh_chan, false);
  609. last_used = chan->cookie;
  610. last_complete = sh_chan->completed_cookie;
  611. BUG_ON(last_complete < 0);
  612. if (done)
  613. *done = last_complete;
  614. if (used)
  615. *used = last_used;
  616. spin_lock_bh(&sh_chan->desc_lock);
  617. status = dma_async_is_complete(cookie, last_complete, last_used);
  618. /*
  619. * If we don't find cookie on the queue, it has been aborted and we have
  620. * to report error
  621. */
  622. if (status != DMA_SUCCESS) {
  623. struct sh_desc *desc;
  624. status = DMA_ERROR;
  625. list_for_each_entry(desc, &sh_chan->ld_queue, node)
  626. if (desc->cookie == cookie) {
  627. status = DMA_IN_PROGRESS;
  628. break;
  629. }
  630. }
  631. spin_unlock_bh(&sh_chan->desc_lock);
  632. return status;
  633. }
  634. static irqreturn_t sh_dmae_interrupt(int irq, void *data)
  635. {
  636. irqreturn_t ret = IRQ_NONE;
  637. struct sh_dmae_chan *sh_chan = (struct sh_dmae_chan *)data;
  638. u32 chcr = sh_dmae_readl(sh_chan, CHCR);
  639. if (chcr & CHCR_TE) {
  640. /* DMA stop */
  641. dmae_halt(sh_chan);
  642. ret = IRQ_HANDLED;
  643. tasklet_schedule(&sh_chan->tasklet);
  644. }
  645. return ret;
  646. }
  647. #if defined(CONFIG_CPU_SH4)
  648. static irqreturn_t sh_dmae_err(int irq, void *data)
  649. {
  650. struct sh_dmae_device *shdev = (struct sh_dmae_device *)data;
  651. int i;
  652. /* halt the dma controller */
  653. sh_dmae_ctl_stop(shdev);
  654. /* We cannot detect, which channel caused the error, have to reset all */
  655. for (i = 0; i < SH_DMAC_MAX_CHANNELS; i++) {
  656. struct sh_dmae_chan *sh_chan = shdev->chan[i];
  657. if (sh_chan) {
  658. struct sh_desc *desc;
  659. /* Stop the channel */
  660. dmae_halt(sh_chan);
  661. /* Complete all */
  662. list_for_each_entry(desc, &sh_chan->ld_queue, node) {
  663. struct dma_async_tx_descriptor *tx = &desc->async_tx;
  664. desc->mark = DESC_IDLE;
  665. if (tx->callback)
  666. tx->callback(tx->callback_param);
  667. }
  668. list_splice_init(&sh_chan->ld_queue, &sh_chan->ld_free);
  669. }
  670. }
  671. sh_dmae_rst(shdev);
  672. return IRQ_HANDLED;
  673. }
  674. #endif
  675. static void dmae_do_tasklet(unsigned long data)
  676. {
  677. struct sh_dmae_chan *sh_chan = (struct sh_dmae_chan *)data;
  678. struct sh_desc *desc;
  679. u32 sar_buf = sh_dmae_readl(sh_chan, SAR);
  680. u32 dar_buf = sh_dmae_readl(sh_chan, DAR);
  681. spin_lock(&sh_chan->desc_lock);
  682. list_for_each_entry(desc, &sh_chan->ld_queue, node) {
  683. if (desc->mark == DESC_SUBMITTED &&
  684. ((desc->direction == DMA_FROM_DEVICE &&
  685. (desc->hw.dar + desc->hw.tcr) == dar_buf) ||
  686. (desc->hw.sar + desc->hw.tcr) == sar_buf)) {
  687. dev_dbg(sh_chan->dev, "done #%d@%p dst %u\n",
  688. desc->async_tx.cookie, &desc->async_tx,
  689. desc->hw.dar);
  690. desc->mark = DESC_COMPLETED;
  691. break;
  692. }
  693. }
  694. spin_unlock(&sh_chan->desc_lock);
  695. /* Next desc */
  696. sh_chan_xfer_ld_queue(sh_chan);
  697. sh_dmae_chan_ld_cleanup(sh_chan, false);
  698. }
  699. static int __devinit sh_dmae_chan_probe(struct sh_dmae_device *shdev, int id,
  700. int irq, unsigned long flags)
  701. {
  702. int err;
  703. struct sh_dmae_channel *chan_pdata = &shdev->pdata->channel[id];
  704. struct platform_device *pdev = to_platform_device(shdev->common.dev);
  705. struct sh_dmae_chan *new_sh_chan;
  706. /* alloc channel */
  707. new_sh_chan = kzalloc(sizeof(struct sh_dmae_chan), GFP_KERNEL);
  708. if (!new_sh_chan) {
  709. dev_err(shdev->common.dev,
  710. "No free memory for allocating dma channels!\n");
  711. return -ENOMEM;
  712. }
  713. /* copy struct dma_device */
  714. new_sh_chan->common.device = &shdev->common;
  715. new_sh_chan->dev = shdev->common.dev;
  716. new_sh_chan->id = id;
  717. new_sh_chan->irq = irq;
  718. new_sh_chan->base = shdev->chan_reg + chan_pdata->offset / sizeof(u32);
  719. /* Init DMA tasklet */
  720. tasklet_init(&new_sh_chan->tasklet, dmae_do_tasklet,
  721. (unsigned long)new_sh_chan);
  722. /* Init the channel */
  723. dmae_init(new_sh_chan);
  724. spin_lock_init(&new_sh_chan->desc_lock);
  725. /* Init descripter manage list */
  726. INIT_LIST_HEAD(&new_sh_chan->ld_queue);
  727. INIT_LIST_HEAD(&new_sh_chan->ld_free);
  728. /* Add the channel to DMA device channel list */
  729. list_add_tail(&new_sh_chan->common.device_node,
  730. &shdev->common.channels);
  731. shdev->common.chancnt++;
  732. if (pdev->id >= 0)
  733. snprintf(new_sh_chan->dev_id, sizeof(new_sh_chan->dev_id),
  734. "sh-dmae%d.%d", pdev->id, new_sh_chan->id);
  735. else
  736. snprintf(new_sh_chan->dev_id, sizeof(new_sh_chan->dev_id),
  737. "sh-dma%d", new_sh_chan->id);
  738. /* set up channel irq */
  739. err = request_irq(irq, &sh_dmae_interrupt, flags,
  740. new_sh_chan->dev_id, new_sh_chan);
  741. if (err) {
  742. dev_err(shdev->common.dev, "DMA channel %d request_irq error "
  743. "with return %d\n", id, err);
  744. goto err_no_irq;
  745. }
  746. shdev->chan[id] = new_sh_chan;
  747. return 0;
  748. err_no_irq:
  749. /* remove from dmaengine device node */
  750. list_del(&new_sh_chan->common.device_node);
  751. kfree(new_sh_chan);
  752. return err;
  753. }
  754. static void sh_dmae_chan_remove(struct sh_dmae_device *shdev)
  755. {
  756. int i;
  757. for (i = shdev->common.chancnt - 1 ; i >= 0 ; i--) {
  758. if (shdev->chan[i]) {
  759. struct sh_dmae_chan *sh_chan = shdev->chan[i];
  760. free_irq(sh_chan->irq, sh_chan);
  761. list_del(&sh_chan->common.device_node);
  762. kfree(sh_chan);
  763. shdev->chan[i] = NULL;
  764. }
  765. }
  766. shdev->common.chancnt = 0;
  767. }
  768. static int __init sh_dmae_probe(struct platform_device *pdev)
  769. {
  770. struct sh_dmae_pdata *pdata = pdev->dev.platform_data;
  771. unsigned long irqflags = IRQF_DISABLED,
  772. chan_flag[SH_DMAC_MAX_CHANNELS] = {};
  773. int errirq, chan_irq[SH_DMAC_MAX_CHANNELS];
  774. int err, i, irq_cnt = 0, irqres = 0;
  775. struct sh_dmae_device *shdev;
  776. struct resource *chan, *dmars, *errirq_res, *chanirq_res;
  777. /* get platform data */
  778. if (!pdata || !pdata->channel_num)
  779. return -ENODEV;
  780. chan = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  781. /* DMARS area is optional, if absent, this controller cannot do slave DMA */
  782. dmars = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  783. /*
  784. * IRQ resources:
  785. * 1. there always must be at least one IRQ IO-resource. On SH4 it is
  786. * the error IRQ, in which case it is the only IRQ in this resource:
  787. * start == end. If it is the only IRQ resource, all channels also
  788. * use the same IRQ.
  789. * 2. DMA channel IRQ resources can be specified one per resource or in
  790. * ranges (start != end)
  791. * 3. iff all events (channels and, optionally, error) on this
  792. * controller use the same IRQ, only one IRQ resource can be
  793. * specified, otherwise there must be one IRQ per channel, even if
  794. * some of them are equal
  795. * 4. if all IRQs on this controller are equal or if some specific IRQs
  796. * specify IORESOURCE_IRQ_SHAREABLE in their resources, they will be
  797. * requested with the IRQF_SHARED flag
  798. */
  799. errirq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  800. if (!chan || !errirq_res)
  801. return -ENODEV;
  802. if (!request_mem_region(chan->start, resource_size(chan), pdev->name)) {
  803. dev_err(&pdev->dev, "DMAC register region already claimed\n");
  804. return -EBUSY;
  805. }
  806. if (dmars && !request_mem_region(dmars->start, resource_size(dmars), pdev->name)) {
  807. dev_err(&pdev->dev, "DMAC DMARS region already claimed\n");
  808. err = -EBUSY;
  809. goto ermrdmars;
  810. }
  811. err = -ENOMEM;
  812. shdev = kzalloc(sizeof(struct sh_dmae_device), GFP_KERNEL);
  813. if (!shdev) {
  814. dev_err(&pdev->dev, "Not enough memory\n");
  815. goto ealloc;
  816. }
  817. shdev->chan_reg = ioremap(chan->start, resource_size(chan));
  818. if (!shdev->chan_reg)
  819. goto emapchan;
  820. if (dmars) {
  821. shdev->dmars = ioremap(dmars->start, resource_size(dmars));
  822. if (!shdev->dmars)
  823. goto emapdmars;
  824. }
  825. /* platform data */
  826. shdev->pdata = pdata;
  827. pm_runtime_enable(&pdev->dev);
  828. pm_runtime_get_sync(&pdev->dev);
  829. /* reset dma controller */
  830. err = sh_dmae_rst(shdev);
  831. if (err)
  832. goto rst_err;
  833. INIT_LIST_HEAD(&shdev->common.channels);
  834. dma_cap_set(DMA_MEMCPY, shdev->common.cap_mask);
  835. if (dmars)
  836. dma_cap_set(DMA_SLAVE, shdev->common.cap_mask);
  837. shdev->common.device_alloc_chan_resources
  838. = sh_dmae_alloc_chan_resources;
  839. shdev->common.device_free_chan_resources = sh_dmae_free_chan_resources;
  840. shdev->common.device_prep_dma_memcpy = sh_dmae_prep_memcpy;
  841. shdev->common.device_is_tx_complete = sh_dmae_is_complete;
  842. shdev->common.device_issue_pending = sh_dmae_memcpy_issue_pending;
  843. /* Compulsory for DMA_SLAVE fields */
  844. shdev->common.device_prep_slave_sg = sh_dmae_prep_slave_sg;
  845. shdev->common.device_terminate_all = sh_dmae_terminate_all;
  846. shdev->common.dev = &pdev->dev;
  847. /* Default transfer size of 32 bytes requires 32-byte alignment */
  848. shdev->common.copy_align = LOG2_DEFAULT_XFER_SIZE;
  849. #if defined(CONFIG_CPU_SH4)
  850. chanirq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
  851. if (!chanirq_res)
  852. chanirq_res = errirq_res;
  853. else
  854. irqres++;
  855. if (chanirq_res == errirq_res ||
  856. (errirq_res->flags & IORESOURCE_BITS) == IORESOURCE_IRQ_SHAREABLE)
  857. irqflags = IRQF_SHARED;
  858. errirq = errirq_res->start;
  859. err = request_irq(errirq, sh_dmae_err, irqflags,
  860. "DMAC Address Error", shdev);
  861. if (err) {
  862. dev_err(&pdev->dev,
  863. "DMA failed requesting irq #%d, error %d\n",
  864. errirq, err);
  865. goto eirq_err;
  866. }
  867. #else
  868. chanirq_res = errirq_res;
  869. #endif /* CONFIG_CPU_SH4 */
  870. if (chanirq_res->start == chanirq_res->end &&
  871. !platform_get_resource(pdev, IORESOURCE_IRQ, 1)) {
  872. /* Special case - all multiplexed */
  873. for (; irq_cnt < pdata->channel_num; irq_cnt++) {
  874. chan_irq[irq_cnt] = chanirq_res->start;
  875. chan_flag[irq_cnt] = IRQF_SHARED;
  876. }
  877. } else {
  878. do {
  879. for (i = chanirq_res->start; i <= chanirq_res->end; i++) {
  880. if ((errirq_res->flags & IORESOURCE_BITS) ==
  881. IORESOURCE_IRQ_SHAREABLE)
  882. chan_flag[irq_cnt] = IRQF_SHARED;
  883. else
  884. chan_flag[irq_cnt] = IRQF_DISABLED;
  885. dev_dbg(&pdev->dev,
  886. "Found IRQ %d for channel %d\n",
  887. i, irq_cnt);
  888. chan_irq[irq_cnt++] = i;
  889. }
  890. chanirq_res = platform_get_resource(pdev,
  891. IORESOURCE_IRQ, ++irqres);
  892. } while (irq_cnt < pdata->channel_num && chanirq_res);
  893. }
  894. if (irq_cnt < pdata->channel_num)
  895. goto eirqres;
  896. /* Create DMA Channel */
  897. for (i = 0; i < pdata->channel_num; i++) {
  898. err = sh_dmae_chan_probe(shdev, i, chan_irq[i], chan_flag[i]);
  899. if (err)
  900. goto chan_probe_err;
  901. }
  902. pm_runtime_put(&pdev->dev);
  903. platform_set_drvdata(pdev, shdev);
  904. dma_async_device_register(&shdev->common);
  905. return err;
  906. chan_probe_err:
  907. sh_dmae_chan_remove(shdev);
  908. eirqres:
  909. #if defined(CONFIG_CPU_SH4)
  910. free_irq(errirq, shdev);
  911. eirq_err:
  912. #endif
  913. rst_err:
  914. pm_runtime_put(&pdev->dev);
  915. if (dmars)
  916. iounmap(shdev->dmars);
  917. emapdmars:
  918. iounmap(shdev->chan_reg);
  919. emapchan:
  920. kfree(shdev);
  921. ealloc:
  922. if (dmars)
  923. release_mem_region(dmars->start, resource_size(dmars));
  924. ermrdmars:
  925. release_mem_region(chan->start, resource_size(chan));
  926. return err;
  927. }
  928. static int __exit sh_dmae_remove(struct platform_device *pdev)
  929. {
  930. struct sh_dmae_device *shdev = platform_get_drvdata(pdev);
  931. struct resource *res;
  932. int errirq = platform_get_irq(pdev, 0);
  933. dma_async_device_unregister(&shdev->common);
  934. if (errirq > 0)
  935. free_irq(errirq, shdev);
  936. /* channel data remove */
  937. sh_dmae_chan_remove(shdev);
  938. pm_runtime_disable(&pdev->dev);
  939. if (shdev->dmars)
  940. iounmap(shdev->dmars);
  941. iounmap(shdev->chan_reg);
  942. kfree(shdev);
  943. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  944. if (res)
  945. release_mem_region(res->start, resource_size(res));
  946. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  947. if (res)
  948. release_mem_region(res->start, resource_size(res));
  949. return 0;
  950. }
  951. static void sh_dmae_shutdown(struct platform_device *pdev)
  952. {
  953. struct sh_dmae_device *shdev = platform_get_drvdata(pdev);
  954. sh_dmae_ctl_stop(shdev);
  955. }
  956. static struct platform_driver sh_dmae_driver = {
  957. .remove = __exit_p(sh_dmae_remove),
  958. .shutdown = sh_dmae_shutdown,
  959. .driver = {
  960. .name = "sh-dma-engine",
  961. },
  962. };
  963. static int __init sh_dmae_init(void)
  964. {
  965. return platform_driver_probe(&sh_dmae_driver, sh_dmae_probe);
  966. }
  967. module_init(sh_dmae_init);
  968. static void __exit sh_dmae_exit(void)
  969. {
  970. platform_driver_unregister(&sh_dmae_driver);
  971. }
  972. module_exit(sh_dmae_exit);
  973. MODULE_AUTHOR("Nobuhiro Iwamatsu <iwamatsu.nobuhiro@renesas.com>");
  974. MODULE_DESCRIPTION("Renesas SH DMA Engine driver");
  975. MODULE_LICENSE("GPL");