shdma.c 38 KB

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