shdma.c 39 KB

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