shdma.c 24 KB

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