shdma.c 24 KB

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