shdma.c 35 KB

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