shdma.c 36 KB

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