shdma.c 37 KB

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