shdma.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  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 descriptor */
  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. struct sh_dmae_device *shdev = data;
  707. if (dmaor_read(shdev) & DMAOR_AE)
  708. return IRQ_RETVAL(sh_dmae_reset(data));
  709. else
  710. return IRQ_NONE;
  711. }
  712. static void dmae_do_tasklet(unsigned long data)
  713. {
  714. struct sh_dmae_chan *sh_chan = (struct sh_dmae_chan *)data;
  715. struct sh_desc *desc;
  716. u32 sar_buf = sh_dmae_readl(sh_chan, SAR);
  717. u32 dar_buf = sh_dmae_readl(sh_chan, DAR);
  718. spin_lock(&sh_chan->desc_lock);
  719. list_for_each_entry(desc, &sh_chan->ld_queue, node) {
  720. if (desc->mark == DESC_SUBMITTED &&
  721. ((desc->direction == DMA_FROM_DEVICE &&
  722. (desc->hw.dar + desc->hw.tcr) == dar_buf) ||
  723. (desc->hw.sar + desc->hw.tcr) == sar_buf)) {
  724. dev_dbg(sh_chan->dev, "done #%d@%p dst %u\n",
  725. desc->async_tx.cookie, &desc->async_tx,
  726. desc->hw.dar);
  727. desc->mark = DESC_COMPLETED;
  728. break;
  729. }
  730. }
  731. spin_unlock(&sh_chan->desc_lock);
  732. /* Next desc */
  733. sh_chan_xfer_ld_queue(sh_chan);
  734. sh_dmae_chan_ld_cleanup(sh_chan, false);
  735. }
  736. static bool sh_dmae_nmi_notify(struct sh_dmae_device *shdev)
  737. {
  738. unsigned int handled;
  739. /* Fast path out if NMIF is not asserted for this controller */
  740. if ((dmaor_read(shdev) & DMAOR_NMIF) == 0)
  741. return false;
  742. handled = sh_dmae_reset(shdev);
  743. if (handled)
  744. return true;
  745. return false;
  746. }
  747. static int sh_dmae_nmi_handler(struct notifier_block *self,
  748. unsigned long cmd, void *data)
  749. {
  750. struct sh_dmae_device *shdev;
  751. int ret = NOTIFY_DONE;
  752. bool triggered;
  753. /*
  754. * Only concern ourselves with NMI events.
  755. *
  756. * Normally we would check the die chain value, but as this needs
  757. * to be architecture independent, check for NMI context instead.
  758. */
  759. if (!in_nmi())
  760. return NOTIFY_DONE;
  761. rcu_read_lock();
  762. list_for_each_entry_rcu(shdev, &sh_dmae_devices, node) {
  763. /*
  764. * Only stop if one of the controllers has NMIF asserted,
  765. * we do not want to interfere with regular address error
  766. * handling or NMI events that don't concern the DMACs.
  767. */
  768. triggered = sh_dmae_nmi_notify(shdev);
  769. if (triggered == true)
  770. ret = NOTIFY_OK;
  771. }
  772. rcu_read_unlock();
  773. return ret;
  774. }
  775. static struct notifier_block sh_dmae_nmi_notifier __read_mostly = {
  776. .notifier_call = sh_dmae_nmi_handler,
  777. /* Run before NMI debug handler and KGDB */
  778. .priority = 1,
  779. };
  780. static int __devinit sh_dmae_chan_probe(struct sh_dmae_device *shdev, int id,
  781. int irq, unsigned long flags)
  782. {
  783. int err;
  784. const struct sh_dmae_channel *chan_pdata = &shdev->pdata->channel[id];
  785. struct platform_device *pdev = to_platform_device(shdev->common.dev);
  786. struct sh_dmae_chan *new_sh_chan;
  787. /* alloc channel */
  788. new_sh_chan = kzalloc(sizeof(struct sh_dmae_chan), GFP_KERNEL);
  789. if (!new_sh_chan) {
  790. dev_err(shdev->common.dev,
  791. "No free memory for allocating dma channels!\n");
  792. return -ENOMEM;
  793. }
  794. /* copy struct dma_device */
  795. new_sh_chan->common.device = &shdev->common;
  796. new_sh_chan->dev = shdev->common.dev;
  797. new_sh_chan->id = id;
  798. new_sh_chan->irq = irq;
  799. new_sh_chan->base = shdev->chan_reg + chan_pdata->offset / sizeof(u32);
  800. /* Init DMA tasklet */
  801. tasklet_init(&new_sh_chan->tasklet, dmae_do_tasklet,
  802. (unsigned long)new_sh_chan);
  803. /* Init the channel */
  804. dmae_init(new_sh_chan);
  805. spin_lock_init(&new_sh_chan->desc_lock);
  806. /* Init descripter manage list */
  807. INIT_LIST_HEAD(&new_sh_chan->ld_queue);
  808. INIT_LIST_HEAD(&new_sh_chan->ld_free);
  809. /* Add the channel to DMA device channel list */
  810. list_add_tail(&new_sh_chan->common.device_node,
  811. &shdev->common.channels);
  812. shdev->common.chancnt++;
  813. if (pdev->id >= 0)
  814. snprintf(new_sh_chan->dev_id, sizeof(new_sh_chan->dev_id),
  815. "sh-dmae%d.%d", pdev->id, new_sh_chan->id);
  816. else
  817. snprintf(new_sh_chan->dev_id, sizeof(new_sh_chan->dev_id),
  818. "sh-dma%d", new_sh_chan->id);
  819. /* set up channel irq */
  820. err = request_irq(irq, &sh_dmae_interrupt, flags,
  821. new_sh_chan->dev_id, new_sh_chan);
  822. if (err) {
  823. dev_err(shdev->common.dev, "DMA channel %d request_irq error "
  824. "with return %d\n", id, err);
  825. goto err_no_irq;
  826. }
  827. shdev->chan[id] = new_sh_chan;
  828. return 0;
  829. err_no_irq:
  830. /* remove from dmaengine device node */
  831. list_del(&new_sh_chan->common.device_node);
  832. kfree(new_sh_chan);
  833. return err;
  834. }
  835. static void sh_dmae_chan_remove(struct sh_dmae_device *shdev)
  836. {
  837. int i;
  838. for (i = shdev->common.chancnt - 1 ; i >= 0 ; i--) {
  839. if (shdev->chan[i]) {
  840. struct sh_dmae_chan *sh_chan = shdev->chan[i];
  841. free_irq(sh_chan->irq, sh_chan);
  842. list_del(&sh_chan->common.device_node);
  843. kfree(sh_chan);
  844. shdev->chan[i] = NULL;
  845. }
  846. }
  847. shdev->common.chancnt = 0;
  848. }
  849. static int __init sh_dmae_probe(struct platform_device *pdev)
  850. {
  851. struct sh_dmae_pdata *pdata = pdev->dev.platform_data;
  852. unsigned long irqflags = IRQF_DISABLED,
  853. chan_flag[SH_DMAC_MAX_CHANNELS] = {};
  854. unsigned long flags;
  855. int errirq, chan_irq[SH_DMAC_MAX_CHANNELS];
  856. int err, i, irq_cnt = 0, irqres = 0;
  857. struct sh_dmae_device *shdev;
  858. struct resource *chan, *dmars, *errirq_res, *chanirq_res;
  859. /* get platform data */
  860. if (!pdata || !pdata->channel_num)
  861. return -ENODEV;
  862. chan = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  863. /* DMARS area is optional, if absent, this controller cannot do slave DMA */
  864. dmars = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  865. /*
  866. * IRQ resources:
  867. * 1. there always must be at least one IRQ IO-resource. On SH4 it is
  868. * the error IRQ, in which case it is the only IRQ in this resource:
  869. * start == end. If it is the only IRQ resource, all channels also
  870. * use the same IRQ.
  871. * 2. DMA channel IRQ resources can be specified one per resource or in
  872. * ranges (start != end)
  873. * 3. iff all events (channels and, optionally, error) on this
  874. * controller use the same IRQ, only one IRQ resource can be
  875. * specified, otherwise there must be one IRQ per channel, even if
  876. * some of them are equal
  877. * 4. if all IRQs on this controller are equal or if some specific IRQs
  878. * specify IORESOURCE_IRQ_SHAREABLE in their resources, they will be
  879. * requested with the IRQF_SHARED flag
  880. */
  881. errirq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  882. if (!chan || !errirq_res)
  883. return -ENODEV;
  884. if (!request_mem_region(chan->start, resource_size(chan), pdev->name)) {
  885. dev_err(&pdev->dev, "DMAC register region already claimed\n");
  886. return -EBUSY;
  887. }
  888. if (dmars && !request_mem_region(dmars->start, resource_size(dmars), pdev->name)) {
  889. dev_err(&pdev->dev, "DMAC DMARS region already claimed\n");
  890. err = -EBUSY;
  891. goto ermrdmars;
  892. }
  893. err = -ENOMEM;
  894. shdev = kzalloc(sizeof(struct sh_dmae_device), GFP_KERNEL);
  895. if (!shdev) {
  896. dev_err(&pdev->dev, "Not enough memory\n");
  897. goto ealloc;
  898. }
  899. shdev->chan_reg = ioremap(chan->start, resource_size(chan));
  900. if (!shdev->chan_reg)
  901. goto emapchan;
  902. if (dmars) {
  903. shdev->dmars = ioremap(dmars->start, resource_size(dmars));
  904. if (!shdev->dmars)
  905. goto emapdmars;
  906. }
  907. /* platform data */
  908. shdev->pdata = pdata;
  909. pm_runtime_enable(&pdev->dev);
  910. pm_runtime_get_sync(&pdev->dev);
  911. spin_lock_irqsave(&sh_dmae_lock, flags);
  912. list_add_tail_rcu(&shdev->node, &sh_dmae_devices);
  913. spin_unlock_irqrestore(&sh_dmae_lock, flags);
  914. /* reset dma controller */
  915. err = sh_dmae_rst(shdev);
  916. if (err)
  917. goto rst_err;
  918. INIT_LIST_HEAD(&shdev->common.channels);
  919. dma_cap_set(DMA_MEMCPY, shdev->common.cap_mask);
  920. if (dmars)
  921. dma_cap_set(DMA_SLAVE, shdev->common.cap_mask);
  922. shdev->common.device_alloc_chan_resources
  923. = sh_dmae_alloc_chan_resources;
  924. shdev->common.device_free_chan_resources = sh_dmae_free_chan_resources;
  925. shdev->common.device_prep_dma_memcpy = sh_dmae_prep_memcpy;
  926. shdev->common.device_tx_status = sh_dmae_tx_status;
  927. shdev->common.device_issue_pending = sh_dmae_memcpy_issue_pending;
  928. /* Compulsory for DMA_SLAVE fields */
  929. shdev->common.device_prep_slave_sg = sh_dmae_prep_slave_sg;
  930. shdev->common.device_control = sh_dmae_control;
  931. shdev->common.dev = &pdev->dev;
  932. /* Default transfer size of 32 bytes requires 32-byte alignment */
  933. shdev->common.copy_align = LOG2_DEFAULT_XFER_SIZE;
  934. #if defined(CONFIG_CPU_SH4) || defined(CONFIG_ARCH_SHMOBILE)
  935. chanirq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
  936. if (!chanirq_res)
  937. chanirq_res = errirq_res;
  938. else
  939. irqres++;
  940. if (chanirq_res == errirq_res ||
  941. (errirq_res->flags & IORESOURCE_BITS) == IORESOURCE_IRQ_SHAREABLE)
  942. irqflags = IRQF_SHARED;
  943. errirq = errirq_res->start;
  944. err = request_irq(errirq, sh_dmae_err, irqflags,
  945. "DMAC Address Error", shdev);
  946. if (err) {
  947. dev_err(&pdev->dev,
  948. "DMA failed requesting irq #%d, error %d\n",
  949. errirq, err);
  950. goto eirq_err;
  951. }
  952. #else
  953. chanirq_res = errirq_res;
  954. #endif /* CONFIG_CPU_SH4 || CONFIG_ARCH_SHMOBILE */
  955. if (chanirq_res->start == chanirq_res->end &&
  956. !platform_get_resource(pdev, IORESOURCE_IRQ, 1)) {
  957. /* Special case - all multiplexed */
  958. for (; irq_cnt < pdata->channel_num; irq_cnt++) {
  959. chan_irq[irq_cnt] = chanirq_res->start;
  960. chan_flag[irq_cnt] = IRQF_SHARED;
  961. }
  962. } else {
  963. do {
  964. for (i = chanirq_res->start; i <= chanirq_res->end; i++) {
  965. if ((errirq_res->flags & IORESOURCE_BITS) ==
  966. IORESOURCE_IRQ_SHAREABLE)
  967. chan_flag[irq_cnt] = IRQF_SHARED;
  968. else
  969. chan_flag[irq_cnt] = IRQF_DISABLED;
  970. dev_dbg(&pdev->dev,
  971. "Found IRQ %d for channel %d\n",
  972. i, irq_cnt);
  973. chan_irq[irq_cnt++] = i;
  974. }
  975. chanirq_res = platform_get_resource(pdev,
  976. IORESOURCE_IRQ, ++irqres);
  977. } while (irq_cnt < pdata->channel_num && chanirq_res);
  978. }
  979. if (irq_cnt < pdata->channel_num)
  980. goto eirqres;
  981. /* Create DMA Channel */
  982. for (i = 0; i < pdata->channel_num; i++) {
  983. err = sh_dmae_chan_probe(shdev, i, chan_irq[i], chan_flag[i]);
  984. if (err)
  985. goto chan_probe_err;
  986. }
  987. pm_runtime_put(&pdev->dev);
  988. platform_set_drvdata(pdev, shdev);
  989. dma_async_device_register(&shdev->common);
  990. return err;
  991. chan_probe_err:
  992. sh_dmae_chan_remove(shdev);
  993. eirqres:
  994. #if defined(CONFIG_CPU_SH4) || defined(CONFIG_ARCH_SHMOBILE)
  995. free_irq(errirq, shdev);
  996. eirq_err:
  997. #endif
  998. rst_err:
  999. spin_lock_irqsave(&sh_dmae_lock, flags);
  1000. list_del_rcu(&shdev->node);
  1001. spin_unlock_irqrestore(&sh_dmae_lock, flags);
  1002. pm_runtime_put(&pdev->dev);
  1003. if (dmars)
  1004. iounmap(shdev->dmars);
  1005. emapdmars:
  1006. iounmap(shdev->chan_reg);
  1007. emapchan:
  1008. kfree(shdev);
  1009. ealloc:
  1010. if (dmars)
  1011. release_mem_region(dmars->start, resource_size(dmars));
  1012. ermrdmars:
  1013. release_mem_region(chan->start, resource_size(chan));
  1014. return err;
  1015. }
  1016. static int __exit sh_dmae_remove(struct platform_device *pdev)
  1017. {
  1018. struct sh_dmae_device *shdev = platform_get_drvdata(pdev);
  1019. struct resource *res;
  1020. unsigned long flags;
  1021. int errirq = platform_get_irq(pdev, 0);
  1022. dma_async_device_unregister(&shdev->common);
  1023. if (errirq > 0)
  1024. free_irq(errirq, shdev);
  1025. spin_lock_irqsave(&sh_dmae_lock, flags);
  1026. list_del_rcu(&shdev->node);
  1027. spin_unlock_irqrestore(&sh_dmae_lock, flags);
  1028. /* channel data remove */
  1029. sh_dmae_chan_remove(shdev);
  1030. pm_runtime_disable(&pdev->dev);
  1031. if (shdev->dmars)
  1032. iounmap(shdev->dmars);
  1033. iounmap(shdev->chan_reg);
  1034. kfree(shdev);
  1035. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1036. if (res)
  1037. release_mem_region(res->start, resource_size(res));
  1038. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1039. if (res)
  1040. release_mem_region(res->start, resource_size(res));
  1041. return 0;
  1042. }
  1043. static void sh_dmae_shutdown(struct platform_device *pdev)
  1044. {
  1045. struct sh_dmae_device *shdev = platform_get_drvdata(pdev);
  1046. sh_dmae_ctl_stop(shdev);
  1047. }
  1048. static struct platform_driver sh_dmae_driver = {
  1049. .remove = __exit_p(sh_dmae_remove),
  1050. .shutdown = sh_dmae_shutdown,
  1051. .driver = {
  1052. .owner = THIS_MODULE,
  1053. .name = "sh-dma-engine",
  1054. },
  1055. };
  1056. static int __init sh_dmae_init(void)
  1057. {
  1058. /* Wire up NMI handling */
  1059. int err = register_die_notifier(&sh_dmae_nmi_notifier);
  1060. if (err)
  1061. return err;
  1062. return platform_driver_probe(&sh_dmae_driver, sh_dmae_probe);
  1063. }
  1064. module_init(sh_dmae_init);
  1065. static void __exit sh_dmae_exit(void)
  1066. {
  1067. platform_driver_unregister(&sh_dmae_driver);
  1068. unregister_die_notifier(&sh_dmae_nmi_notifier);
  1069. }
  1070. module_exit(sh_dmae_exit);
  1071. MODULE_AUTHOR("Nobuhiro Iwamatsu <iwamatsu.nobuhiro@renesas.com>");
  1072. MODULE_DESCRIPTION("Renesas SH DMA Engine driver");
  1073. MODULE_LICENSE("GPL");
  1074. MODULE_ALIAS("platform:sh-dma-engine");