shdma.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  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 void sh_dmae_terminate_all(struct dma_chan *chan)
  471. {
  472. struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
  473. if (!chan)
  474. return;
  475. sh_dmae_chan_ld_cleanup(sh_chan, true);
  476. }
  477. static dma_async_tx_callback __ld_cleanup(struct sh_dmae_chan *sh_chan, bool all)
  478. {
  479. struct sh_desc *desc, *_desc;
  480. /* Is the "exposed" head of a chain acked? */
  481. bool head_acked = false;
  482. dma_cookie_t cookie = 0;
  483. dma_async_tx_callback callback = NULL;
  484. void *param = NULL;
  485. spin_lock_bh(&sh_chan->desc_lock);
  486. list_for_each_entry_safe(desc, _desc, &sh_chan->ld_queue, node) {
  487. struct dma_async_tx_descriptor *tx = &desc->async_tx;
  488. BUG_ON(tx->cookie > 0 && tx->cookie != desc->cookie);
  489. BUG_ON(desc->mark != DESC_SUBMITTED &&
  490. desc->mark != DESC_COMPLETED &&
  491. desc->mark != DESC_WAITING);
  492. /*
  493. * queue is ordered, and we use this loop to (1) clean up all
  494. * completed descriptors, and to (2) update descriptor flags of
  495. * any chunks in a (partially) completed chain
  496. */
  497. if (!all && desc->mark == DESC_SUBMITTED &&
  498. desc->cookie != cookie)
  499. break;
  500. if (tx->cookie > 0)
  501. cookie = tx->cookie;
  502. if (desc->mark == DESC_COMPLETED && desc->chunks == 1) {
  503. if (sh_chan->completed_cookie != desc->cookie - 1)
  504. dev_dbg(sh_chan->dev,
  505. "Completing cookie %d, expected %d\n",
  506. desc->cookie,
  507. sh_chan->completed_cookie + 1);
  508. sh_chan->completed_cookie = desc->cookie;
  509. }
  510. /* Call callback on the last chunk */
  511. if (desc->mark == DESC_COMPLETED && tx->callback) {
  512. desc->mark = DESC_WAITING;
  513. callback = tx->callback;
  514. param = tx->callback_param;
  515. dev_dbg(sh_chan->dev, "descriptor #%d@%p on %d callback\n",
  516. tx->cookie, tx, sh_chan->id);
  517. BUG_ON(desc->chunks != 1);
  518. break;
  519. }
  520. if (tx->cookie > 0 || tx->cookie == -EBUSY) {
  521. if (desc->mark == DESC_COMPLETED) {
  522. BUG_ON(tx->cookie < 0);
  523. desc->mark = DESC_WAITING;
  524. }
  525. head_acked = async_tx_test_ack(tx);
  526. } else {
  527. switch (desc->mark) {
  528. case DESC_COMPLETED:
  529. desc->mark = DESC_WAITING;
  530. /* Fall through */
  531. case DESC_WAITING:
  532. if (head_acked)
  533. async_tx_ack(&desc->async_tx);
  534. }
  535. }
  536. dev_dbg(sh_chan->dev, "descriptor %p #%d completed.\n",
  537. tx, tx->cookie);
  538. if (((desc->mark == DESC_COMPLETED ||
  539. desc->mark == DESC_WAITING) &&
  540. async_tx_test_ack(&desc->async_tx)) || all) {
  541. /* Remove from ld_queue list */
  542. desc->mark = DESC_IDLE;
  543. list_move(&desc->node, &sh_chan->ld_free);
  544. }
  545. }
  546. spin_unlock_bh(&sh_chan->desc_lock);
  547. if (callback)
  548. callback(param);
  549. return callback;
  550. }
  551. /*
  552. * sh_chan_ld_cleanup - Clean up link descriptors
  553. *
  554. * This function cleans up the ld_queue of DMA channel.
  555. */
  556. static void sh_dmae_chan_ld_cleanup(struct sh_dmae_chan *sh_chan, bool all)
  557. {
  558. while (__ld_cleanup(sh_chan, all))
  559. ;
  560. }
  561. static void sh_chan_xfer_ld_queue(struct sh_dmae_chan *sh_chan)
  562. {
  563. struct sh_desc *desc;
  564. spin_lock_bh(&sh_chan->desc_lock);
  565. /* DMA work check */
  566. if (dmae_is_busy(sh_chan)) {
  567. spin_unlock_bh(&sh_chan->desc_lock);
  568. return;
  569. }
  570. /* Find the first not transferred desciptor */
  571. list_for_each_entry(desc, &sh_chan->ld_queue, node)
  572. if (desc->mark == DESC_SUBMITTED) {
  573. /* Get the ld start address from ld_queue */
  574. dmae_set_reg(sh_chan, &desc->hw);
  575. dmae_start(sh_chan);
  576. break;
  577. }
  578. spin_unlock_bh(&sh_chan->desc_lock);
  579. }
  580. static void sh_dmae_memcpy_issue_pending(struct dma_chan *chan)
  581. {
  582. struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
  583. sh_chan_xfer_ld_queue(sh_chan);
  584. }
  585. static enum dma_status sh_dmae_is_complete(struct dma_chan *chan,
  586. dma_cookie_t cookie,
  587. dma_cookie_t *done,
  588. dma_cookie_t *used)
  589. {
  590. struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
  591. dma_cookie_t last_used;
  592. dma_cookie_t last_complete;
  593. enum dma_status status;
  594. sh_dmae_chan_ld_cleanup(sh_chan, false);
  595. last_used = chan->cookie;
  596. last_complete = sh_chan->completed_cookie;
  597. BUG_ON(last_complete < 0);
  598. if (done)
  599. *done = last_complete;
  600. if (used)
  601. *used = last_used;
  602. spin_lock_bh(&sh_chan->desc_lock);
  603. status = dma_async_is_complete(cookie, last_complete, last_used);
  604. /*
  605. * If we don't find cookie on the queue, it has been aborted and we have
  606. * to report error
  607. */
  608. if (status != DMA_SUCCESS) {
  609. struct sh_desc *desc;
  610. status = DMA_ERROR;
  611. list_for_each_entry(desc, &sh_chan->ld_queue, node)
  612. if (desc->cookie == cookie) {
  613. status = DMA_IN_PROGRESS;
  614. break;
  615. }
  616. }
  617. spin_unlock_bh(&sh_chan->desc_lock);
  618. return status;
  619. }
  620. static irqreturn_t sh_dmae_interrupt(int irq, void *data)
  621. {
  622. irqreturn_t ret = IRQ_NONE;
  623. struct sh_dmae_chan *sh_chan = (struct sh_dmae_chan *)data;
  624. u32 chcr = sh_dmae_readl(sh_chan, CHCR);
  625. if (chcr & CHCR_TE) {
  626. /* DMA stop */
  627. dmae_halt(sh_chan);
  628. ret = IRQ_HANDLED;
  629. tasklet_schedule(&sh_chan->tasklet);
  630. }
  631. return ret;
  632. }
  633. #if defined(CONFIG_CPU_SH4)
  634. static irqreturn_t sh_dmae_err(int irq, void *data)
  635. {
  636. struct sh_dmae_device *shdev = (struct sh_dmae_device *)data;
  637. int i;
  638. /* halt the dma controller */
  639. sh_dmae_ctl_stop(shdev);
  640. /* We cannot detect, which channel caused the error, have to reset all */
  641. for (i = 0; i < SH_DMAC_MAX_CHANNELS; i++) {
  642. struct sh_dmae_chan *sh_chan = shdev->chan[i];
  643. if (sh_chan) {
  644. struct sh_desc *desc;
  645. /* Stop the channel */
  646. dmae_halt(sh_chan);
  647. /* Complete all */
  648. list_for_each_entry(desc, &sh_chan->ld_queue, node) {
  649. struct dma_async_tx_descriptor *tx = &desc->async_tx;
  650. desc->mark = DESC_IDLE;
  651. if (tx->callback)
  652. tx->callback(tx->callback_param);
  653. }
  654. list_splice_init(&sh_chan->ld_queue, &sh_chan->ld_free);
  655. }
  656. }
  657. sh_dmae_rst(shdev);
  658. return IRQ_HANDLED;
  659. }
  660. #endif
  661. static void dmae_do_tasklet(unsigned long data)
  662. {
  663. struct sh_dmae_chan *sh_chan = (struct sh_dmae_chan *)data;
  664. struct sh_desc *desc;
  665. u32 sar_buf = sh_dmae_readl(sh_chan, SAR);
  666. u32 dar_buf = sh_dmae_readl(sh_chan, DAR);
  667. spin_lock(&sh_chan->desc_lock);
  668. list_for_each_entry(desc, &sh_chan->ld_queue, node) {
  669. if (desc->mark == DESC_SUBMITTED &&
  670. ((desc->direction == DMA_FROM_DEVICE &&
  671. (desc->hw.dar + desc->hw.tcr) == dar_buf) ||
  672. (desc->hw.sar + desc->hw.tcr) == sar_buf)) {
  673. dev_dbg(sh_chan->dev, "done #%d@%p dst %u\n",
  674. desc->async_tx.cookie, &desc->async_tx,
  675. desc->hw.dar);
  676. desc->mark = DESC_COMPLETED;
  677. break;
  678. }
  679. }
  680. spin_unlock(&sh_chan->desc_lock);
  681. /* Next desc */
  682. sh_chan_xfer_ld_queue(sh_chan);
  683. sh_dmae_chan_ld_cleanup(sh_chan, false);
  684. }
  685. static int __devinit sh_dmae_chan_probe(struct sh_dmae_device *shdev, int id,
  686. int irq, unsigned long flags)
  687. {
  688. int err;
  689. struct sh_dmae_channel *chan_pdata = &shdev->pdata->channel[id];
  690. struct platform_device *pdev = to_platform_device(shdev->common.dev);
  691. struct sh_dmae_chan *new_sh_chan;
  692. /* alloc channel */
  693. new_sh_chan = kzalloc(sizeof(struct sh_dmae_chan), GFP_KERNEL);
  694. if (!new_sh_chan) {
  695. dev_err(shdev->common.dev,
  696. "No free memory for allocating dma channels!\n");
  697. return -ENOMEM;
  698. }
  699. /* copy struct dma_device */
  700. new_sh_chan->common.device = &shdev->common;
  701. new_sh_chan->dev = shdev->common.dev;
  702. new_sh_chan->id = id;
  703. new_sh_chan->irq = irq;
  704. new_sh_chan->base = shdev->chan_reg + chan_pdata->offset / sizeof(u32);
  705. /* Init DMA tasklet */
  706. tasklet_init(&new_sh_chan->tasklet, dmae_do_tasklet,
  707. (unsigned long)new_sh_chan);
  708. /* Init the channel */
  709. dmae_init(new_sh_chan);
  710. spin_lock_init(&new_sh_chan->desc_lock);
  711. /* Init descripter manage list */
  712. INIT_LIST_HEAD(&new_sh_chan->ld_queue);
  713. INIT_LIST_HEAD(&new_sh_chan->ld_free);
  714. /* Add the channel to DMA device channel list */
  715. list_add_tail(&new_sh_chan->common.device_node,
  716. &shdev->common.channels);
  717. shdev->common.chancnt++;
  718. if (pdev->id >= 0)
  719. snprintf(new_sh_chan->dev_id, sizeof(new_sh_chan->dev_id),
  720. "sh-dmae%d.%d", pdev->id, new_sh_chan->id);
  721. else
  722. snprintf(new_sh_chan->dev_id, sizeof(new_sh_chan->dev_id),
  723. "sh-dma%d", new_sh_chan->id);
  724. /* set up channel irq */
  725. err = request_irq(irq, &sh_dmae_interrupt, flags,
  726. new_sh_chan->dev_id, new_sh_chan);
  727. if (err) {
  728. dev_err(shdev->common.dev, "DMA channel %d request_irq error "
  729. "with return %d\n", id, err);
  730. goto err_no_irq;
  731. }
  732. shdev->chan[id] = new_sh_chan;
  733. return 0;
  734. err_no_irq:
  735. /* remove from dmaengine device node */
  736. list_del(&new_sh_chan->common.device_node);
  737. kfree(new_sh_chan);
  738. return err;
  739. }
  740. static void sh_dmae_chan_remove(struct sh_dmae_device *shdev)
  741. {
  742. int i;
  743. for (i = shdev->common.chancnt - 1 ; i >= 0 ; i--) {
  744. if (shdev->chan[i]) {
  745. struct sh_dmae_chan *sh_chan = shdev->chan[i];
  746. free_irq(sh_chan->irq, sh_chan);
  747. list_del(&sh_chan->common.device_node);
  748. kfree(sh_chan);
  749. shdev->chan[i] = NULL;
  750. }
  751. }
  752. shdev->common.chancnt = 0;
  753. }
  754. static int __init sh_dmae_probe(struct platform_device *pdev)
  755. {
  756. struct sh_dmae_pdata *pdata = pdev->dev.platform_data;
  757. unsigned long irqflags = IRQF_DISABLED,
  758. chan_flag[SH_DMAC_MAX_CHANNELS] = {};
  759. int errirq, chan_irq[SH_DMAC_MAX_CHANNELS];
  760. int err, i, irq_cnt = 0, irqres = 0;
  761. struct sh_dmae_device *shdev;
  762. struct resource *chan, *dmars, *errirq_res, *chanirq_res;
  763. /* get platform data */
  764. if (!pdata || !pdata->channel_num)
  765. return -ENODEV;
  766. chan = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  767. /* DMARS area is optional, if absent, this controller cannot do slave DMA */
  768. dmars = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  769. /*
  770. * IRQ resources:
  771. * 1. there always must be at least one IRQ IO-resource. On SH4 it is
  772. * the error IRQ, in which case it is the only IRQ in this resource:
  773. * start == end. If it is the only IRQ resource, all channels also
  774. * use the same IRQ.
  775. * 2. DMA channel IRQ resources can be specified one per resource or in
  776. * ranges (start != end)
  777. * 3. iff all events (channels and, optionally, error) on this
  778. * controller use the same IRQ, only one IRQ resource can be
  779. * specified, otherwise there must be one IRQ per channel, even if
  780. * some of them are equal
  781. * 4. if all IRQs on this controller are equal or if some specific IRQs
  782. * specify IORESOURCE_IRQ_SHAREABLE in their resources, they will be
  783. * requested with the IRQF_SHARED flag
  784. */
  785. errirq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  786. if (!chan || !errirq_res)
  787. return -ENODEV;
  788. if (!request_mem_region(chan->start, resource_size(chan), pdev->name)) {
  789. dev_err(&pdev->dev, "DMAC register region already claimed\n");
  790. return -EBUSY;
  791. }
  792. if (dmars && !request_mem_region(dmars->start, resource_size(dmars), pdev->name)) {
  793. dev_err(&pdev->dev, "DMAC DMARS region already claimed\n");
  794. err = -EBUSY;
  795. goto ermrdmars;
  796. }
  797. err = -ENOMEM;
  798. shdev = kzalloc(sizeof(struct sh_dmae_device), GFP_KERNEL);
  799. if (!shdev) {
  800. dev_err(&pdev->dev, "Not enough memory\n");
  801. goto ealloc;
  802. }
  803. shdev->chan_reg = ioremap(chan->start, resource_size(chan));
  804. if (!shdev->chan_reg)
  805. goto emapchan;
  806. if (dmars) {
  807. shdev->dmars = ioremap(dmars->start, resource_size(dmars));
  808. if (!shdev->dmars)
  809. goto emapdmars;
  810. }
  811. /* platform data */
  812. shdev->pdata = pdata;
  813. pm_runtime_enable(&pdev->dev);
  814. pm_runtime_get_sync(&pdev->dev);
  815. /* reset dma controller */
  816. err = sh_dmae_rst(shdev);
  817. if (err)
  818. goto rst_err;
  819. INIT_LIST_HEAD(&shdev->common.channels);
  820. dma_cap_set(DMA_MEMCPY, shdev->common.cap_mask);
  821. if (dmars)
  822. dma_cap_set(DMA_SLAVE, shdev->common.cap_mask);
  823. shdev->common.device_alloc_chan_resources
  824. = sh_dmae_alloc_chan_resources;
  825. shdev->common.device_free_chan_resources = sh_dmae_free_chan_resources;
  826. shdev->common.device_prep_dma_memcpy = sh_dmae_prep_memcpy;
  827. shdev->common.device_is_tx_complete = sh_dmae_is_complete;
  828. shdev->common.device_issue_pending = sh_dmae_memcpy_issue_pending;
  829. /* Compulsory for DMA_SLAVE fields */
  830. shdev->common.device_prep_slave_sg = sh_dmae_prep_slave_sg;
  831. shdev->common.device_terminate_all = sh_dmae_terminate_all;
  832. shdev->common.dev = &pdev->dev;
  833. /* Default transfer size of 32 bytes requires 32-byte alignment */
  834. shdev->common.copy_align = LOG2_DEFAULT_XFER_SIZE;
  835. #if defined(CONFIG_CPU_SH4)
  836. chanirq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
  837. if (!chanirq_res)
  838. chanirq_res = errirq_res;
  839. else
  840. irqres++;
  841. if (chanirq_res == errirq_res ||
  842. (errirq_res->flags & IORESOURCE_BITS) == IORESOURCE_IRQ_SHAREABLE)
  843. irqflags = IRQF_SHARED;
  844. errirq = errirq_res->start;
  845. err = request_irq(errirq, sh_dmae_err, irqflags,
  846. "DMAC Address Error", shdev);
  847. if (err) {
  848. dev_err(&pdev->dev,
  849. "DMA failed requesting irq #%d, error %d\n",
  850. errirq, err);
  851. goto eirq_err;
  852. }
  853. #else
  854. chanirq_res = errirq_res;
  855. #endif /* CONFIG_CPU_SH4 */
  856. if (chanirq_res->start == chanirq_res->end &&
  857. !platform_get_resource(pdev, IORESOURCE_IRQ, 1)) {
  858. /* Special case - all multiplexed */
  859. for (; irq_cnt < pdata->channel_num; irq_cnt++) {
  860. chan_irq[irq_cnt] = chanirq_res->start;
  861. chan_flag[irq_cnt] = IRQF_SHARED;
  862. }
  863. } else {
  864. do {
  865. for (i = chanirq_res->start; i <= chanirq_res->end; i++) {
  866. if ((errirq_res->flags & IORESOURCE_BITS) ==
  867. IORESOURCE_IRQ_SHAREABLE)
  868. chan_flag[irq_cnt] = IRQF_SHARED;
  869. else
  870. chan_flag[irq_cnt] = IRQF_DISABLED;
  871. dev_dbg(&pdev->dev,
  872. "Found IRQ %d for channel %d\n",
  873. i, irq_cnt);
  874. chan_irq[irq_cnt++] = i;
  875. }
  876. chanirq_res = platform_get_resource(pdev,
  877. IORESOURCE_IRQ, ++irqres);
  878. } while (irq_cnt < pdata->channel_num && chanirq_res);
  879. }
  880. if (irq_cnt < pdata->channel_num)
  881. goto eirqres;
  882. /* Create DMA Channel */
  883. for (i = 0; i < pdata->channel_num; i++) {
  884. err = sh_dmae_chan_probe(shdev, i, chan_irq[i], chan_flag[i]);
  885. if (err)
  886. goto chan_probe_err;
  887. }
  888. pm_runtime_put(&pdev->dev);
  889. platform_set_drvdata(pdev, shdev);
  890. dma_async_device_register(&shdev->common);
  891. return err;
  892. chan_probe_err:
  893. sh_dmae_chan_remove(shdev);
  894. eirqres:
  895. #if defined(CONFIG_CPU_SH4)
  896. free_irq(errirq, shdev);
  897. eirq_err:
  898. #endif
  899. rst_err:
  900. pm_runtime_put(&pdev->dev);
  901. if (dmars)
  902. iounmap(shdev->dmars);
  903. emapdmars:
  904. iounmap(shdev->chan_reg);
  905. emapchan:
  906. kfree(shdev);
  907. ealloc:
  908. if (dmars)
  909. release_mem_region(dmars->start, resource_size(dmars));
  910. ermrdmars:
  911. release_mem_region(chan->start, resource_size(chan));
  912. return err;
  913. }
  914. static int __exit sh_dmae_remove(struct platform_device *pdev)
  915. {
  916. struct sh_dmae_device *shdev = platform_get_drvdata(pdev);
  917. struct resource *res;
  918. int errirq = platform_get_irq(pdev, 0);
  919. dma_async_device_unregister(&shdev->common);
  920. if (errirq > 0)
  921. free_irq(errirq, shdev);
  922. /* channel data remove */
  923. sh_dmae_chan_remove(shdev);
  924. pm_runtime_disable(&pdev->dev);
  925. if (shdev->dmars)
  926. iounmap(shdev->dmars);
  927. iounmap(shdev->chan_reg);
  928. kfree(shdev);
  929. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  930. if (res)
  931. release_mem_region(res->start, resource_size(res));
  932. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  933. if (res)
  934. release_mem_region(res->start, resource_size(res));
  935. return 0;
  936. }
  937. static void sh_dmae_shutdown(struct platform_device *pdev)
  938. {
  939. struct sh_dmae_device *shdev = platform_get_drvdata(pdev);
  940. sh_dmae_ctl_stop(shdev);
  941. }
  942. static struct platform_driver sh_dmae_driver = {
  943. .remove = __exit_p(sh_dmae_remove),
  944. .shutdown = sh_dmae_shutdown,
  945. .driver = {
  946. .name = "sh-dma-engine",
  947. },
  948. };
  949. static int __init sh_dmae_init(void)
  950. {
  951. return platform_driver_probe(&sh_dmae_driver, sh_dmae_probe);
  952. }
  953. module_init(sh_dmae_init);
  954. static void __exit sh_dmae_exit(void)
  955. {
  956. platform_driver_unregister(&sh_dmae_driver);
  957. }
  958. module_exit(sh_dmae_exit);
  959. MODULE_AUTHOR("Nobuhiro Iwamatsu <iwamatsu.nobuhiro@renesas.com>");
  960. MODULE_DESCRIPTION("Renesas SH DMA Engine driver");
  961. MODULE_LICENSE("GPL");