shdma.c 24 KB

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