shdma.c 36 KB

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