shdma.c 39 KB

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