shdma.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  1. /*
  2. * Renesas SuperH DMA Engine support
  3. *
  4. * base is drivers/dma/flsdma.c
  5. *
  6. * Copyright (C) 2011-2012 Guennadi Liakhovetski <g.liakhovetski@gmx.de>
  7. * Copyright (C) 2009 Nobuhiro Iwamatsu <iwamatsu.nobuhiro@renesas.com>
  8. * Copyright (C) 2009 Renesas Solutions, Inc. All rights reserved.
  9. * Copyright (C) 2007 Freescale Semiconductor, Inc. All rights reserved.
  10. *
  11. * This is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * - DMA of SuperH does not have Hardware DMA chain mode.
  17. * - MAX DMA size is 16MB.
  18. *
  19. */
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/dmaengine.h>
  25. #include <linux/delay.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 "../dmaengine.h"
  34. #include "shdma.h"
  35. #define SH_DMAE_DRV_NAME "sh-dma-engine"
  36. /* Default MEMCPY transfer size = 2^2 = 4 bytes */
  37. #define LOG2_DEFAULT_XFER_SIZE 2
  38. #define SH_DMA_SLAVE_NUMBER 256
  39. #define SH_DMA_TCR_MAX (16 * 1024 * 1024 - 1)
  40. /*
  41. * Used for write-side mutual exclusion for the global device list,
  42. * read-side synchronization by way of RCU, and per-controller data.
  43. */
  44. static DEFINE_SPINLOCK(sh_dmae_lock);
  45. static LIST_HEAD(sh_dmae_devices);
  46. static void chclr_write(struct sh_dmae_chan *sh_dc, u32 data)
  47. {
  48. struct sh_dmae_device *shdev = to_sh_dev(sh_dc);
  49. __raw_writel(data, shdev->chan_reg +
  50. shdev->pdata->channel[sh_dc->shdma_chan.id].chclr_offset);
  51. }
  52. static void sh_dmae_writel(struct sh_dmae_chan *sh_dc, u32 data, u32 reg)
  53. {
  54. __raw_writel(data, sh_dc->base + reg / sizeof(u32));
  55. }
  56. static u32 sh_dmae_readl(struct sh_dmae_chan *sh_dc, u32 reg)
  57. {
  58. return __raw_readl(sh_dc->base + reg / sizeof(u32));
  59. }
  60. static u16 dmaor_read(struct sh_dmae_device *shdev)
  61. {
  62. u32 __iomem *addr = shdev->chan_reg + DMAOR / sizeof(u32);
  63. if (shdev->pdata->dmaor_is_32bit)
  64. return __raw_readl(addr);
  65. else
  66. return __raw_readw(addr);
  67. }
  68. static void dmaor_write(struct sh_dmae_device *shdev, u16 data)
  69. {
  70. u32 __iomem *addr = shdev->chan_reg + DMAOR / sizeof(u32);
  71. if (shdev->pdata->dmaor_is_32bit)
  72. __raw_writel(data, addr);
  73. else
  74. __raw_writew(data, addr);
  75. }
  76. static void chcr_write(struct sh_dmae_chan *sh_dc, u32 data)
  77. {
  78. struct sh_dmae_device *shdev = to_sh_dev(sh_dc);
  79. __raw_writel(data, sh_dc->base + shdev->chcr_offset / sizeof(u32));
  80. }
  81. static u32 chcr_read(struct sh_dmae_chan *sh_dc)
  82. {
  83. struct sh_dmae_device *shdev = to_sh_dev(sh_dc);
  84. return __raw_readl(sh_dc->base + shdev->chcr_offset / sizeof(u32));
  85. }
  86. /*
  87. * Reset DMA controller
  88. *
  89. * SH7780 has two DMAOR register
  90. */
  91. static void sh_dmae_ctl_stop(struct sh_dmae_device *shdev)
  92. {
  93. unsigned short dmaor;
  94. unsigned long flags;
  95. spin_lock_irqsave(&sh_dmae_lock, flags);
  96. dmaor = dmaor_read(shdev);
  97. dmaor_write(shdev, dmaor & ~(DMAOR_NMIF | DMAOR_AE | DMAOR_DME));
  98. spin_unlock_irqrestore(&sh_dmae_lock, flags);
  99. }
  100. static int sh_dmae_rst(struct sh_dmae_device *shdev)
  101. {
  102. unsigned short dmaor;
  103. unsigned long flags;
  104. spin_lock_irqsave(&sh_dmae_lock, flags);
  105. dmaor = dmaor_read(shdev) & ~(DMAOR_NMIF | DMAOR_AE | DMAOR_DME);
  106. if (shdev->pdata->chclr_present) {
  107. int i;
  108. for (i = 0; i < shdev->pdata->channel_num; i++) {
  109. struct sh_dmae_chan *sh_chan = shdev->chan[i];
  110. if (sh_chan)
  111. chclr_write(sh_chan, 0);
  112. }
  113. }
  114. dmaor_write(shdev, dmaor | shdev->pdata->dmaor_init);
  115. dmaor = dmaor_read(shdev);
  116. spin_unlock_irqrestore(&sh_dmae_lock, flags);
  117. if (dmaor & (DMAOR_AE | DMAOR_NMIF)) {
  118. dev_warn(shdev->shdma_dev.dma_dev.dev, "Can't initialize DMAOR.\n");
  119. return -EIO;
  120. }
  121. if (shdev->pdata->dmaor_init & ~dmaor)
  122. dev_warn(shdev->shdma_dev.dma_dev.dev,
  123. "DMAOR=0x%x hasn't latched the initial value 0x%x.\n",
  124. dmaor, shdev->pdata->dmaor_init);
  125. return 0;
  126. }
  127. static bool dmae_is_busy(struct sh_dmae_chan *sh_chan)
  128. {
  129. u32 chcr = chcr_read(sh_chan);
  130. if ((chcr & (CHCR_DE | CHCR_TE)) == CHCR_DE)
  131. return true; /* working */
  132. return false; /* waiting */
  133. }
  134. static unsigned int calc_xmit_shift(struct sh_dmae_chan *sh_chan, u32 chcr)
  135. {
  136. struct sh_dmae_device *shdev = to_sh_dev(sh_chan);
  137. struct sh_dmae_pdata *pdata = shdev->pdata;
  138. int cnt = ((chcr & pdata->ts_low_mask) >> pdata->ts_low_shift) |
  139. ((chcr & pdata->ts_high_mask) >> pdata->ts_high_shift);
  140. if (cnt >= pdata->ts_shift_num)
  141. cnt = 0;
  142. return pdata->ts_shift[cnt];
  143. }
  144. static u32 log2size_to_chcr(struct sh_dmae_chan *sh_chan, int l2size)
  145. {
  146. struct sh_dmae_device *shdev = to_sh_dev(sh_chan);
  147. struct sh_dmae_pdata *pdata = shdev->pdata;
  148. int i;
  149. for (i = 0; i < pdata->ts_shift_num; i++)
  150. if (pdata->ts_shift[i] == l2size)
  151. break;
  152. if (i == pdata->ts_shift_num)
  153. i = 0;
  154. return ((i << pdata->ts_low_shift) & pdata->ts_low_mask) |
  155. ((i << pdata->ts_high_shift) & pdata->ts_high_mask);
  156. }
  157. static void dmae_set_reg(struct sh_dmae_chan *sh_chan, struct sh_dmae_regs *hw)
  158. {
  159. sh_dmae_writel(sh_chan, hw->sar, SAR);
  160. sh_dmae_writel(sh_chan, hw->dar, DAR);
  161. sh_dmae_writel(sh_chan, hw->tcr >> sh_chan->xmit_shift, TCR);
  162. }
  163. static void dmae_start(struct sh_dmae_chan *sh_chan)
  164. {
  165. struct sh_dmae_device *shdev = to_sh_dev(sh_chan);
  166. u32 chcr = chcr_read(sh_chan);
  167. if (shdev->pdata->needs_tend_set)
  168. sh_dmae_writel(sh_chan, 0xFFFFFFFF, TEND);
  169. chcr |= CHCR_DE | shdev->chcr_ie_bit;
  170. chcr_write(sh_chan, chcr & ~CHCR_TE);
  171. }
  172. static void dmae_init(struct sh_dmae_chan *sh_chan)
  173. {
  174. /*
  175. * Default configuration for dual address memory-memory transfer.
  176. * 0x400 represents auto-request.
  177. */
  178. u32 chcr = DM_INC | SM_INC | 0x400 | log2size_to_chcr(sh_chan,
  179. LOG2_DEFAULT_XFER_SIZE);
  180. sh_chan->xmit_shift = calc_xmit_shift(sh_chan, chcr);
  181. chcr_write(sh_chan, chcr);
  182. }
  183. static int dmae_set_chcr(struct sh_dmae_chan *sh_chan, u32 val)
  184. {
  185. /* If DMA is active, cannot set CHCR. TODO: remove this superfluous check */
  186. if (dmae_is_busy(sh_chan))
  187. return -EBUSY;
  188. sh_chan->xmit_shift = calc_xmit_shift(sh_chan, val);
  189. chcr_write(sh_chan, val);
  190. return 0;
  191. }
  192. static int dmae_set_dmars(struct sh_dmae_chan *sh_chan, u16 val)
  193. {
  194. struct sh_dmae_device *shdev = to_sh_dev(sh_chan);
  195. struct sh_dmae_pdata *pdata = shdev->pdata;
  196. const struct sh_dmae_channel *chan_pdata = &pdata->channel[sh_chan->shdma_chan.id];
  197. u16 __iomem *addr = shdev->dmars;
  198. unsigned int shift = chan_pdata->dmars_bit;
  199. if (dmae_is_busy(sh_chan))
  200. return -EBUSY;
  201. if (pdata->no_dmars)
  202. return 0;
  203. /* in the case of a missing DMARS resource use first memory window */
  204. if (!addr)
  205. addr = (u16 __iomem *)shdev->chan_reg;
  206. addr += chan_pdata->dmars / sizeof(u16);
  207. __raw_writew((__raw_readw(addr) & (0xff00 >> shift)) | (val << shift),
  208. addr);
  209. return 0;
  210. }
  211. static void sh_dmae_start_xfer(struct shdma_chan *schan,
  212. struct shdma_desc *sdesc)
  213. {
  214. struct sh_dmae_chan *sh_chan = container_of(schan, struct sh_dmae_chan,
  215. shdma_chan);
  216. struct sh_dmae_desc *sh_desc = container_of(sdesc,
  217. struct sh_dmae_desc, shdma_desc);
  218. dev_dbg(sh_chan->shdma_chan.dev, "Queue #%d to %d: %u@%x -> %x\n",
  219. sdesc->async_tx.cookie, sh_chan->shdma_chan.id,
  220. sh_desc->hw.tcr, sh_desc->hw.sar, sh_desc->hw.dar);
  221. /* Get the ld start address from ld_queue */
  222. dmae_set_reg(sh_chan, &sh_desc->hw);
  223. dmae_start(sh_chan);
  224. }
  225. static bool sh_dmae_channel_busy(struct shdma_chan *schan)
  226. {
  227. struct sh_dmae_chan *sh_chan = container_of(schan, struct sh_dmae_chan,
  228. shdma_chan);
  229. return dmae_is_busy(sh_chan);
  230. }
  231. static void sh_dmae_setup_xfer(struct shdma_chan *schan,
  232. int slave_id)
  233. {
  234. struct sh_dmae_chan *sh_chan = container_of(schan, struct sh_dmae_chan,
  235. shdma_chan);
  236. if (slave_id >= 0) {
  237. const struct sh_dmae_slave_config *cfg =
  238. sh_chan->config;
  239. dmae_set_dmars(sh_chan, cfg->mid_rid);
  240. dmae_set_chcr(sh_chan, cfg->chcr);
  241. } else {
  242. dmae_init(sh_chan);
  243. }
  244. }
  245. static const struct sh_dmae_slave_config *dmae_find_slave(
  246. struct sh_dmae_chan *sh_chan, int slave_id)
  247. {
  248. struct sh_dmae_device *shdev = to_sh_dev(sh_chan);
  249. struct sh_dmae_pdata *pdata = shdev->pdata;
  250. const struct sh_dmae_slave_config *cfg;
  251. int i;
  252. if (slave_id >= SH_DMA_SLAVE_NUMBER)
  253. return NULL;
  254. for (i = 0, cfg = pdata->slave; i < pdata->slave_num; i++, cfg++)
  255. if (cfg->slave_id == slave_id)
  256. return cfg;
  257. return NULL;
  258. }
  259. static int sh_dmae_set_slave(struct shdma_chan *schan,
  260. int slave_id, bool try)
  261. {
  262. struct sh_dmae_chan *sh_chan = container_of(schan, struct sh_dmae_chan,
  263. shdma_chan);
  264. const struct sh_dmae_slave_config *cfg = dmae_find_slave(sh_chan, slave_id);
  265. if (!cfg)
  266. return -ENODEV;
  267. if (!try)
  268. sh_chan->config = cfg;
  269. return 0;
  270. }
  271. static void dmae_halt(struct sh_dmae_chan *sh_chan)
  272. {
  273. struct sh_dmae_device *shdev = to_sh_dev(sh_chan);
  274. u32 chcr = chcr_read(sh_chan);
  275. chcr &= ~(CHCR_DE | CHCR_TE | shdev->chcr_ie_bit);
  276. chcr_write(sh_chan, chcr);
  277. }
  278. static int sh_dmae_desc_setup(struct shdma_chan *schan,
  279. struct shdma_desc *sdesc,
  280. dma_addr_t src, dma_addr_t dst, size_t *len)
  281. {
  282. struct sh_dmae_desc *sh_desc = container_of(sdesc,
  283. struct sh_dmae_desc, shdma_desc);
  284. if (*len > schan->max_xfer_len)
  285. *len = schan->max_xfer_len;
  286. sh_desc->hw.sar = src;
  287. sh_desc->hw.dar = dst;
  288. sh_desc->hw.tcr = *len;
  289. return 0;
  290. }
  291. static void sh_dmae_halt(struct shdma_chan *schan)
  292. {
  293. struct sh_dmae_chan *sh_chan = container_of(schan, struct sh_dmae_chan,
  294. shdma_chan);
  295. dmae_halt(sh_chan);
  296. }
  297. static bool sh_dmae_chan_irq(struct shdma_chan *schan, int irq)
  298. {
  299. struct sh_dmae_chan *sh_chan = container_of(schan, struct sh_dmae_chan,
  300. shdma_chan);
  301. if (!(chcr_read(sh_chan) & CHCR_TE))
  302. return false;
  303. /* DMA stop */
  304. dmae_halt(sh_chan);
  305. return true;
  306. }
  307. /* Called from error IRQ or NMI */
  308. static bool sh_dmae_reset(struct sh_dmae_device *shdev)
  309. {
  310. bool ret;
  311. /* halt the dma controller */
  312. sh_dmae_ctl_stop(shdev);
  313. /* We cannot detect, which channel caused the error, have to reset all */
  314. ret = shdma_reset(&shdev->shdma_dev);
  315. sh_dmae_rst(shdev);
  316. return ret;
  317. }
  318. static irqreturn_t sh_dmae_err(int irq, void *data)
  319. {
  320. struct sh_dmae_device *shdev = data;
  321. if (!(dmaor_read(shdev) & DMAOR_AE))
  322. return IRQ_NONE;
  323. sh_dmae_reset(shdev);
  324. return IRQ_HANDLED;
  325. }
  326. static bool sh_dmae_desc_completed(struct shdma_chan *schan,
  327. struct shdma_desc *sdesc)
  328. {
  329. struct sh_dmae_chan *sh_chan = container_of(schan,
  330. struct sh_dmae_chan, shdma_chan);
  331. struct sh_dmae_desc *sh_desc = container_of(sdesc,
  332. struct sh_dmae_desc, shdma_desc);
  333. u32 sar_buf = sh_dmae_readl(sh_chan, SAR);
  334. u32 dar_buf = sh_dmae_readl(sh_chan, DAR);
  335. return (sdesc->direction == DMA_DEV_TO_MEM &&
  336. (sh_desc->hw.dar + sh_desc->hw.tcr) == dar_buf) ||
  337. (sdesc->direction != DMA_DEV_TO_MEM &&
  338. (sh_desc->hw.sar + sh_desc->hw.tcr) == sar_buf);
  339. }
  340. static bool sh_dmae_nmi_notify(struct sh_dmae_device *shdev)
  341. {
  342. /* Fast path out if NMIF is not asserted for this controller */
  343. if ((dmaor_read(shdev) & DMAOR_NMIF) == 0)
  344. return false;
  345. return sh_dmae_reset(shdev);
  346. }
  347. static int sh_dmae_nmi_handler(struct notifier_block *self,
  348. unsigned long cmd, void *data)
  349. {
  350. struct sh_dmae_device *shdev;
  351. int ret = NOTIFY_DONE;
  352. bool triggered;
  353. /*
  354. * Only concern ourselves with NMI events.
  355. *
  356. * Normally we would check the die chain value, but as this needs
  357. * to be architecture independent, check for NMI context instead.
  358. */
  359. if (!in_nmi())
  360. return NOTIFY_DONE;
  361. rcu_read_lock();
  362. list_for_each_entry_rcu(shdev, &sh_dmae_devices, node) {
  363. /*
  364. * Only stop if one of the controllers has NMIF asserted,
  365. * we do not want to interfere with regular address error
  366. * handling or NMI events that don't concern the DMACs.
  367. */
  368. triggered = sh_dmae_nmi_notify(shdev);
  369. if (triggered == true)
  370. ret = NOTIFY_OK;
  371. }
  372. rcu_read_unlock();
  373. return ret;
  374. }
  375. static struct notifier_block sh_dmae_nmi_notifier __read_mostly = {
  376. .notifier_call = sh_dmae_nmi_handler,
  377. /* Run before NMI debug handler and KGDB */
  378. .priority = 1,
  379. };
  380. static int __devinit sh_dmae_chan_probe(struct sh_dmae_device *shdev, int id,
  381. int irq, unsigned long flags)
  382. {
  383. const struct sh_dmae_channel *chan_pdata = &shdev->pdata->channel[id];
  384. struct shdma_dev *sdev = &shdev->shdma_dev;
  385. struct platform_device *pdev = to_platform_device(sdev->dma_dev.dev);
  386. struct sh_dmae_chan *sh_chan;
  387. struct shdma_chan *schan;
  388. int err;
  389. sh_chan = kzalloc(sizeof(struct sh_dmae_chan), GFP_KERNEL);
  390. if (!sh_chan) {
  391. dev_err(sdev->dma_dev.dev,
  392. "No free memory for allocating dma channels!\n");
  393. return -ENOMEM;
  394. }
  395. schan = &sh_chan->shdma_chan;
  396. schan->max_xfer_len = SH_DMA_TCR_MAX + 1;
  397. shdma_chan_probe(sdev, schan, id);
  398. sh_chan->base = shdev->chan_reg + chan_pdata->offset / sizeof(u32);
  399. /* set up channel irq */
  400. if (pdev->id >= 0)
  401. snprintf(sh_chan->dev_id, sizeof(sh_chan->dev_id),
  402. "sh-dmae%d.%d", pdev->id, id);
  403. else
  404. snprintf(sh_chan->dev_id, sizeof(sh_chan->dev_id),
  405. "sh-dma%d", id);
  406. err = shdma_request_irq(schan, irq, flags, sh_chan->dev_id);
  407. if (err) {
  408. dev_err(sdev->dma_dev.dev,
  409. "DMA channel %d request_irq error %d\n",
  410. id, err);
  411. goto err_no_irq;
  412. }
  413. shdev->chan[id] = sh_chan;
  414. return 0;
  415. err_no_irq:
  416. /* remove from dmaengine device node */
  417. shdma_chan_remove(schan);
  418. kfree(sh_chan);
  419. return err;
  420. }
  421. static void sh_dmae_chan_remove(struct sh_dmae_device *shdev)
  422. {
  423. struct dma_device *dma_dev = &shdev->shdma_dev.dma_dev;
  424. struct shdma_chan *schan;
  425. int i;
  426. shdma_for_each_chan(schan, &shdev->shdma_dev, i) {
  427. struct sh_dmae_chan *sh_chan = container_of(schan,
  428. struct sh_dmae_chan, shdma_chan);
  429. BUG_ON(!schan);
  430. shdma_free_irq(&sh_chan->shdma_chan);
  431. shdma_chan_remove(schan);
  432. kfree(sh_chan);
  433. }
  434. dma_dev->chancnt = 0;
  435. }
  436. static void sh_dmae_shutdown(struct platform_device *pdev)
  437. {
  438. struct sh_dmae_device *shdev = platform_get_drvdata(pdev);
  439. sh_dmae_ctl_stop(shdev);
  440. }
  441. static int sh_dmae_runtime_suspend(struct device *dev)
  442. {
  443. return 0;
  444. }
  445. static int sh_dmae_runtime_resume(struct device *dev)
  446. {
  447. struct sh_dmae_device *shdev = dev_get_drvdata(dev);
  448. return sh_dmae_rst(shdev);
  449. }
  450. #ifdef CONFIG_PM
  451. static int sh_dmae_suspend(struct device *dev)
  452. {
  453. return 0;
  454. }
  455. static int sh_dmae_resume(struct device *dev)
  456. {
  457. struct sh_dmae_device *shdev = dev_get_drvdata(dev);
  458. int i, ret;
  459. ret = sh_dmae_rst(shdev);
  460. if (ret < 0)
  461. dev_err(dev, "Failed to reset!\n");
  462. for (i = 0; i < shdev->pdata->channel_num; i++) {
  463. struct sh_dmae_chan *sh_chan = shdev->chan[i];
  464. if (!sh_chan->shdma_chan.desc_num)
  465. continue;
  466. if (sh_chan->shdma_chan.slave_id >= 0) {
  467. const struct sh_dmae_slave_config *cfg = sh_chan->config;
  468. dmae_set_dmars(sh_chan, cfg->mid_rid);
  469. dmae_set_chcr(sh_chan, cfg->chcr);
  470. } else {
  471. dmae_init(sh_chan);
  472. }
  473. }
  474. return 0;
  475. }
  476. #else
  477. #define sh_dmae_suspend NULL
  478. #define sh_dmae_resume NULL
  479. #endif
  480. const struct dev_pm_ops sh_dmae_pm = {
  481. .suspend = sh_dmae_suspend,
  482. .resume = sh_dmae_resume,
  483. .runtime_suspend = sh_dmae_runtime_suspend,
  484. .runtime_resume = sh_dmae_runtime_resume,
  485. };
  486. static dma_addr_t sh_dmae_slave_addr(struct shdma_chan *schan)
  487. {
  488. struct sh_dmae_chan *sh_chan = container_of(schan,
  489. struct sh_dmae_chan, shdma_chan);
  490. /*
  491. * Implicit BUG_ON(!sh_chan->config)
  492. * This is an exclusive slave DMA operation, may only be called after a
  493. * successful slave configuration.
  494. */
  495. return sh_chan->config->addr;
  496. }
  497. static struct shdma_desc *sh_dmae_embedded_desc(void *buf, int i)
  498. {
  499. return &((struct sh_dmae_desc *)buf)[i].shdma_desc;
  500. }
  501. static const struct shdma_ops sh_dmae_shdma_ops = {
  502. .desc_completed = sh_dmae_desc_completed,
  503. .halt_channel = sh_dmae_halt,
  504. .channel_busy = sh_dmae_channel_busy,
  505. .slave_addr = sh_dmae_slave_addr,
  506. .desc_setup = sh_dmae_desc_setup,
  507. .set_slave = sh_dmae_set_slave,
  508. .setup_xfer = sh_dmae_setup_xfer,
  509. .start_xfer = sh_dmae_start_xfer,
  510. .embedded_desc = sh_dmae_embedded_desc,
  511. .chan_irq = sh_dmae_chan_irq,
  512. };
  513. static int __devinit sh_dmae_probe(struct platform_device *pdev)
  514. {
  515. struct sh_dmae_pdata *pdata = pdev->dev.platform_data;
  516. unsigned long irqflags = IRQF_DISABLED,
  517. chan_flag[SH_DMAE_MAX_CHANNELS] = {};
  518. int errirq, chan_irq[SH_DMAE_MAX_CHANNELS];
  519. int err, i, irq_cnt = 0, irqres = 0, irq_cap = 0;
  520. struct sh_dmae_device *shdev;
  521. struct dma_device *dma_dev;
  522. struct resource *chan, *dmars, *errirq_res, *chanirq_res;
  523. /* get platform data */
  524. if (!pdata || !pdata->channel_num)
  525. return -ENODEV;
  526. chan = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  527. /* DMARS area is optional */
  528. dmars = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  529. /*
  530. * IRQ resources:
  531. * 1. there always must be at least one IRQ IO-resource. On SH4 it is
  532. * the error IRQ, in which case it is the only IRQ in this resource:
  533. * start == end. If it is the only IRQ resource, all channels also
  534. * use the same IRQ.
  535. * 2. DMA channel IRQ resources can be specified one per resource or in
  536. * ranges (start != end)
  537. * 3. iff all events (channels and, optionally, error) on this
  538. * controller use the same IRQ, only one IRQ resource can be
  539. * specified, otherwise there must be one IRQ per channel, even if
  540. * some of them are equal
  541. * 4. if all IRQs on this controller are equal or if some specific IRQs
  542. * specify IORESOURCE_IRQ_SHAREABLE in their resources, they will be
  543. * requested with the IRQF_SHARED flag
  544. */
  545. errirq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  546. if (!chan || !errirq_res)
  547. return -ENODEV;
  548. if (!request_mem_region(chan->start, resource_size(chan), pdev->name)) {
  549. dev_err(&pdev->dev, "DMAC register region already claimed\n");
  550. return -EBUSY;
  551. }
  552. if (dmars && !request_mem_region(dmars->start, resource_size(dmars), pdev->name)) {
  553. dev_err(&pdev->dev, "DMAC DMARS region already claimed\n");
  554. err = -EBUSY;
  555. goto ermrdmars;
  556. }
  557. err = -ENOMEM;
  558. shdev = kzalloc(sizeof(struct sh_dmae_device), GFP_KERNEL);
  559. if (!shdev) {
  560. dev_err(&pdev->dev, "Not enough memory\n");
  561. goto ealloc;
  562. }
  563. dma_dev = &shdev->shdma_dev.dma_dev;
  564. shdev->chan_reg = ioremap(chan->start, resource_size(chan));
  565. if (!shdev->chan_reg)
  566. goto emapchan;
  567. if (dmars) {
  568. shdev->dmars = ioremap(dmars->start, resource_size(dmars));
  569. if (!shdev->dmars)
  570. goto emapdmars;
  571. }
  572. if (!pdata->slave_only)
  573. dma_cap_set(DMA_MEMCPY, dma_dev->cap_mask);
  574. if (pdata->slave && pdata->slave_num)
  575. dma_cap_set(DMA_SLAVE, dma_dev->cap_mask);
  576. /* Default transfer size of 32 bytes requires 32-byte alignment */
  577. dma_dev->copy_align = LOG2_DEFAULT_XFER_SIZE;
  578. shdev->shdma_dev.ops = &sh_dmae_shdma_ops;
  579. shdev->shdma_dev.desc_size = sizeof(struct sh_dmae_desc);
  580. err = shdma_init(&pdev->dev, &shdev->shdma_dev,
  581. pdata->channel_num);
  582. if (err < 0)
  583. goto eshdma;
  584. /* platform data */
  585. shdev->pdata = pdev->dev.platform_data;
  586. if (pdata->chcr_offset)
  587. shdev->chcr_offset = pdata->chcr_offset;
  588. else
  589. shdev->chcr_offset = CHCR;
  590. if (pdata->chcr_ie_bit)
  591. shdev->chcr_ie_bit = pdata->chcr_ie_bit;
  592. else
  593. shdev->chcr_ie_bit = CHCR_IE;
  594. platform_set_drvdata(pdev, shdev);
  595. pm_runtime_enable(&pdev->dev);
  596. err = pm_runtime_get_sync(&pdev->dev);
  597. if (err < 0)
  598. dev_err(&pdev->dev, "%s(): GET = %d\n", __func__, err);
  599. spin_lock_irq(&sh_dmae_lock);
  600. list_add_tail_rcu(&shdev->node, &sh_dmae_devices);
  601. spin_unlock_irq(&sh_dmae_lock);
  602. /* reset dma controller - only needed as a test */
  603. err = sh_dmae_rst(shdev);
  604. if (err)
  605. goto rst_err;
  606. #if defined(CONFIG_CPU_SH4) || defined(CONFIG_ARCH_SHMOBILE)
  607. chanirq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
  608. if (!chanirq_res)
  609. chanirq_res = errirq_res;
  610. else
  611. irqres++;
  612. if (chanirq_res == errirq_res ||
  613. (errirq_res->flags & IORESOURCE_BITS) == IORESOURCE_IRQ_SHAREABLE)
  614. irqflags = IRQF_SHARED;
  615. errirq = errirq_res->start;
  616. err = request_irq(errirq, sh_dmae_err, irqflags,
  617. "DMAC Address Error", shdev);
  618. if (err) {
  619. dev_err(&pdev->dev,
  620. "DMA failed requesting irq #%d, error %d\n",
  621. errirq, err);
  622. goto eirq_err;
  623. }
  624. #else
  625. chanirq_res = errirq_res;
  626. #endif /* CONFIG_CPU_SH4 || CONFIG_ARCH_SHMOBILE */
  627. if (chanirq_res->start == chanirq_res->end &&
  628. !platform_get_resource(pdev, IORESOURCE_IRQ, 1)) {
  629. /* Special case - all multiplexed */
  630. for (; irq_cnt < pdata->channel_num; irq_cnt++) {
  631. if (irq_cnt < SH_DMAE_MAX_CHANNELS) {
  632. chan_irq[irq_cnt] = chanirq_res->start;
  633. chan_flag[irq_cnt] = IRQF_SHARED;
  634. } else {
  635. irq_cap = 1;
  636. break;
  637. }
  638. }
  639. } else {
  640. do {
  641. for (i = chanirq_res->start; i <= chanirq_res->end; i++) {
  642. if (irq_cnt >= SH_DMAE_MAX_CHANNELS) {
  643. irq_cap = 1;
  644. break;
  645. }
  646. if ((errirq_res->flags & IORESOURCE_BITS) ==
  647. IORESOURCE_IRQ_SHAREABLE)
  648. chan_flag[irq_cnt] = IRQF_SHARED;
  649. else
  650. chan_flag[irq_cnt] = IRQF_DISABLED;
  651. dev_dbg(&pdev->dev,
  652. "Found IRQ %d for channel %d\n",
  653. i, irq_cnt);
  654. chan_irq[irq_cnt++] = i;
  655. }
  656. if (irq_cnt >= SH_DMAE_MAX_CHANNELS)
  657. break;
  658. chanirq_res = platform_get_resource(pdev,
  659. IORESOURCE_IRQ, ++irqres);
  660. } while (irq_cnt < pdata->channel_num && chanirq_res);
  661. }
  662. /* Create DMA Channel */
  663. for (i = 0; i < irq_cnt; i++) {
  664. err = sh_dmae_chan_probe(shdev, i, chan_irq[i], chan_flag[i]);
  665. if (err)
  666. goto chan_probe_err;
  667. }
  668. if (irq_cap)
  669. dev_notice(&pdev->dev, "Attempting to register %d DMA "
  670. "channels when a maximum of %d are supported.\n",
  671. pdata->channel_num, SH_DMAE_MAX_CHANNELS);
  672. pm_runtime_put(&pdev->dev);
  673. err = dma_async_device_register(&shdev->shdma_dev.dma_dev);
  674. if (err < 0)
  675. goto edmadevreg;
  676. return err;
  677. edmadevreg:
  678. pm_runtime_get(&pdev->dev);
  679. chan_probe_err:
  680. sh_dmae_chan_remove(shdev);
  681. #if defined(CONFIG_CPU_SH4) || defined(CONFIG_ARCH_SHMOBILE)
  682. free_irq(errirq, shdev);
  683. eirq_err:
  684. #endif
  685. rst_err:
  686. spin_lock_irq(&sh_dmae_lock);
  687. list_del_rcu(&shdev->node);
  688. spin_unlock_irq(&sh_dmae_lock);
  689. pm_runtime_put(&pdev->dev);
  690. pm_runtime_disable(&pdev->dev);
  691. platform_set_drvdata(pdev, NULL);
  692. shdma_cleanup(&shdev->shdma_dev);
  693. eshdma:
  694. if (dmars)
  695. iounmap(shdev->dmars);
  696. emapdmars:
  697. iounmap(shdev->chan_reg);
  698. synchronize_rcu();
  699. emapchan:
  700. kfree(shdev);
  701. ealloc:
  702. if (dmars)
  703. release_mem_region(dmars->start, resource_size(dmars));
  704. ermrdmars:
  705. release_mem_region(chan->start, resource_size(chan));
  706. return err;
  707. }
  708. static int __devexit sh_dmae_remove(struct platform_device *pdev)
  709. {
  710. struct sh_dmae_device *shdev = platform_get_drvdata(pdev);
  711. struct dma_device *dma_dev = &shdev->shdma_dev.dma_dev;
  712. struct resource *res;
  713. int errirq = platform_get_irq(pdev, 0);
  714. dma_async_device_unregister(dma_dev);
  715. if (errirq > 0)
  716. free_irq(errirq, shdev);
  717. spin_lock_irq(&sh_dmae_lock);
  718. list_del_rcu(&shdev->node);
  719. spin_unlock_irq(&sh_dmae_lock);
  720. pm_runtime_disable(&pdev->dev);
  721. sh_dmae_chan_remove(shdev);
  722. shdma_cleanup(&shdev->shdma_dev);
  723. if (shdev->dmars)
  724. iounmap(shdev->dmars);
  725. iounmap(shdev->chan_reg);
  726. platform_set_drvdata(pdev, NULL);
  727. synchronize_rcu();
  728. kfree(shdev);
  729. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  730. if (res)
  731. release_mem_region(res->start, resource_size(res));
  732. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  733. if (res)
  734. release_mem_region(res->start, resource_size(res));
  735. return 0;
  736. }
  737. static struct platform_driver sh_dmae_driver = {
  738. .driver = {
  739. .owner = THIS_MODULE,
  740. .pm = &sh_dmae_pm,
  741. .name = SH_DMAE_DRV_NAME,
  742. },
  743. .remove = __devexit_p(sh_dmae_remove),
  744. .shutdown = sh_dmae_shutdown,
  745. };
  746. static int __init sh_dmae_init(void)
  747. {
  748. /* Wire up NMI handling */
  749. int err = register_die_notifier(&sh_dmae_nmi_notifier);
  750. if (err)
  751. return err;
  752. return platform_driver_probe(&sh_dmae_driver, sh_dmae_probe);
  753. }
  754. module_init(sh_dmae_init);
  755. static void __exit sh_dmae_exit(void)
  756. {
  757. platform_driver_unregister(&sh_dmae_driver);
  758. unregister_die_notifier(&sh_dmae_nmi_notifier);
  759. }
  760. module_exit(sh_dmae_exit);
  761. MODULE_AUTHOR("Nobuhiro Iwamatsu <iwamatsu.nobuhiro@renesas.com>");
  762. MODULE_DESCRIPTION("Renesas SH DMA Engine driver");
  763. MODULE_LICENSE("GPL");
  764. MODULE_ALIAS("platform:" SH_DMAE_DRV_NAME);