shdma.c 38 KB

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