shdmac.c 24 KB

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