shdma.c 37 KB

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