shdma.c 38 KB

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