shdma.c 27 KB

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