shdma.c 30 KB

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