shdma.c 38 KB

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