shdma.c 31 KB

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