shdmac.c 24 KB

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