shdma.c 34 KB

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