shdma.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. /*
  2. * Renesas SuperH DMA Engine support
  3. *
  4. * base is drivers/dma/flsdma.c
  5. *
  6. * Copyright (C) 2009 Nobuhiro Iwamatsu <iwamatsu.nobuhiro@renesas.com>
  7. * Copyright (C) 2009 Renesas Solutions, Inc. All rights reserved.
  8. * Copyright (C) 2007 Freescale Semiconductor, Inc. All rights reserved.
  9. *
  10. * This is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * - DMA of SuperH does not have Hardware DMA chain mode.
  16. * - MAX DMA size is 16MB.
  17. *
  18. */
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/dmaengine.h>
  23. #include <linux/delay.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/platform_device.h>
  26. #include <cpu/dma.h>
  27. #include <asm/dma-sh.h>
  28. #include "shdma.h"
  29. /* DMA descriptor control */
  30. enum sh_dmae_desc_status {
  31. DESC_IDLE,
  32. DESC_PREPARED,
  33. DESC_SUBMITTED,
  34. DESC_COMPLETED, /* completed, have to call callback */
  35. DESC_WAITING, /* callback called, waiting for ack / re-submit */
  36. };
  37. #define NR_DESCS_PER_CHANNEL 32
  38. /*
  39. * Define the default configuration for dual address memory-memory transfer.
  40. * The 0x400 value represents auto-request, external->external.
  41. *
  42. * And this driver set 4byte burst mode.
  43. * If you want to change mode, you need to change RS_DEFAULT of value.
  44. * (ex 1byte burst mode -> (RS_DUAL & ~TS_32)
  45. */
  46. #define RS_DEFAULT (RS_DUAL)
  47. static void sh_dmae_chan_ld_cleanup(struct sh_dmae_chan *sh_chan, bool all);
  48. #define SH_DMAC_CHAN_BASE(id) (dma_base_addr[id])
  49. static void sh_dmae_writel(struct sh_dmae_chan *sh_dc, u32 data, u32 reg)
  50. {
  51. ctrl_outl(data, (SH_DMAC_CHAN_BASE(sh_dc->id) + reg));
  52. }
  53. static u32 sh_dmae_readl(struct sh_dmae_chan *sh_dc, u32 reg)
  54. {
  55. return ctrl_inl((SH_DMAC_CHAN_BASE(sh_dc->id) + reg));
  56. }
  57. static void dmae_init(struct sh_dmae_chan *sh_chan)
  58. {
  59. u32 chcr = RS_DEFAULT; /* default is DUAL mode */
  60. sh_dmae_writel(sh_chan, chcr, CHCR);
  61. }
  62. /*
  63. * Reset DMA controller
  64. *
  65. * SH7780 has two DMAOR register
  66. */
  67. static void sh_dmae_ctl_stop(int id)
  68. {
  69. unsigned short dmaor = dmaor_read_reg(id);
  70. dmaor &= ~(DMAOR_NMIF | DMAOR_AE);
  71. dmaor_write_reg(id, dmaor);
  72. }
  73. static int sh_dmae_rst(int id)
  74. {
  75. unsigned short dmaor;
  76. sh_dmae_ctl_stop(id);
  77. dmaor = dmaor_read_reg(id) | DMAOR_INIT;
  78. dmaor_write_reg(id, dmaor);
  79. if (dmaor_read_reg(id) & (DMAOR_AE | DMAOR_NMIF)) {
  80. pr_warning(KERN_ERR "dma-sh: Can't initialize DMAOR.\n");
  81. return -EINVAL;
  82. }
  83. return 0;
  84. }
  85. static int dmae_is_busy(struct sh_dmae_chan *sh_chan)
  86. {
  87. u32 chcr = sh_dmae_readl(sh_chan, CHCR);
  88. if (chcr & CHCR_DE) {
  89. if (!(chcr & CHCR_TE))
  90. return -EBUSY; /* working */
  91. }
  92. return 0; /* waiting */
  93. }
  94. static inline unsigned int calc_xmit_shift(struct sh_dmae_chan *sh_chan)
  95. {
  96. u32 chcr = sh_dmae_readl(sh_chan, CHCR);
  97. return ts_shift[(chcr & CHCR_TS_MASK) >> CHCR_TS_SHIFT];
  98. }
  99. static void dmae_set_reg(struct sh_dmae_chan *sh_chan, struct sh_dmae_regs *hw)
  100. {
  101. sh_dmae_writel(sh_chan, hw->sar, SAR);
  102. sh_dmae_writel(sh_chan, hw->dar, DAR);
  103. sh_dmae_writel(sh_chan, hw->tcr >> calc_xmit_shift(sh_chan), TCR);
  104. }
  105. static void dmae_start(struct sh_dmae_chan *sh_chan)
  106. {
  107. u32 chcr = sh_dmae_readl(sh_chan, CHCR);
  108. chcr |= CHCR_DE | CHCR_IE;
  109. sh_dmae_writel(sh_chan, chcr, CHCR);
  110. }
  111. static void dmae_halt(struct sh_dmae_chan *sh_chan)
  112. {
  113. u32 chcr = sh_dmae_readl(sh_chan, CHCR);
  114. chcr &= ~(CHCR_DE | CHCR_TE | CHCR_IE);
  115. sh_dmae_writel(sh_chan, chcr, CHCR);
  116. }
  117. static int dmae_set_chcr(struct sh_dmae_chan *sh_chan, u32 val)
  118. {
  119. int ret = dmae_is_busy(sh_chan);
  120. /* When DMA was working, can not set data to CHCR */
  121. if (ret)
  122. return ret;
  123. sh_dmae_writel(sh_chan, val, CHCR);
  124. return 0;
  125. }
  126. #define DMARS1_ADDR 0x04
  127. #define DMARS2_ADDR 0x08
  128. #define DMARS_SHIFT 8
  129. #define DMARS_CHAN_MSK 0x01
  130. static int dmae_set_dmars(struct sh_dmae_chan *sh_chan, u16 val)
  131. {
  132. u32 addr;
  133. int shift = 0;
  134. int ret = dmae_is_busy(sh_chan);
  135. if (ret)
  136. return ret;
  137. if (sh_chan->id & DMARS_CHAN_MSK)
  138. shift = DMARS_SHIFT;
  139. switch (sh_chan->id) {
  140. /* DMARS0 */
  141. case 0:
  142. case 1:
  143. addr = SH_DMARS_BASE;
  144. break;
  145. /* DMARS1 */
  146. case 2:
  147. case 3:
  148. addr = (SH_DMARS_BASE + DMARS1_ADDR);
  149. break;
  150. /* DMARS2 */
  151. case 4:
  152. case 5:
  153. addr = (SH_DMARS_BASE + DMARS2_ADDR);
  154. break;
  155. default:
  156. return -EINVAL;
  157. }
  158. ctrl_outw((val << shift) |
  159. (ctrl_inw(addr) & (shift ? 0xFF00 : 0x00FF)),
  160. addr);
  161. return 0;
  162. }
  163. static dma_cookie_t sh_dmae_tx_submit(struct dma_async_tx_descriptor *tx)
  164. {
  165. struct sh_desc *desc = tx_to_sh_desc(tx), *chunk, *last = desc, *c;
  166. struct sh_dmae_chan *sh_chan = to_sh_chan(tx->chan);
  167. dma_async_tx_callback callback = tx->callback;
  168. dma_cookie_t cookie;
  169. spin_lock_bh(&sh_chan->desc_lock);
  170. cookie = sh_chan->common.cookie;
  171. cookie++;
  172. if (cookie < 0)
  173. cookie = 1;
  174. sh_chan->common.cookie = cookie;
  175. tx->cookie = cookie;
  176. /* Mark all chunks of this descriptor as submitted, move to the queue */
  177. list_for_each_entry_safe(chunk, c, desc->node.prev, node) {
  178. /*
  179. * All chunks are on the global ld_free, so, we have to find
  180. * the end of the chain ourselves
  181. */
  182. if (chunk != desc && (chunk->mark == DESC_IDLE ||
  183. chunk->async_tx.cookie > 0 ||
  184. chunk->async_tx.cookie == -EBUSY ||
  185. &chunk->node == &sh_chan->ld_free))
  186. break;
  187. chunk->mark = DESC_SUBMITTED;
  188. /* Callback goes to the last chunk */
  189. chunk->async_tx.callback = NULL;
  190. chunk->cookie = cookie;
  191. list_move_tail(&chunk->node, &sh_chan->ld_queue);
  192. last = chunk;
  193. }
  194. last->async_tx.callback = callback;
  195. last->async_tx.callback_param = tx->callback_param;
  196. dev_dbg(sh_chan->dev, "submit #%d@%p on %d: %x[%d] -> %x\n",
  197. tx->cookie, &last->async_tx, sh_chan->id,
  198. desc->hw.sar, desc->hw.tcr, desc->hw.dar);
  199. spin_unlock_bh(&sh_chan->desc_lock);
  200. return cookie;
  201. }
  202. /* Called with desc_lock held */
  203. static struct sh_desc *sh_dmae_get_desc(struct sh_dmae_chan *sh_chan)
  204. {
  205. struct sh_desc *desc;
  206. list_for_each_entry(desc, &sh_chan->ld_free, node)
  207. if (desc->mark != DESC_PREPARED) {
  208. BUG_ON(desc->mark != DESC_IDLE);
  209. list_del(&desc->node);
  210. return desc;
  211. }
  212. return NULL;
  213. }
  214. static int sh_dmae_alloc_chan_resources(struct dma_chan *chan)
  215. {
  216. struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
  217. struct sh_desc *desc;
  218. spin_lock_bh(&sh_chan->desc_lock);
  219. while (sh_chan->descs_allocated < NR_DESCS_PER_CHANNEL) {
  220. spin_unlock_bh(&sh_chan->desc_lock);
  221. desc = kzalloc(sizeof(struct sh_desc), GFP_KERNEL);
  222. if (!desc) {
  223. spin_lock_bh(&sh_chan->desc_lock);
  224. break;
  225. }
  226. dma_async_tx_descriptor_init(&desc->async_tx,
  227. &sh_chan->common);
  228. desc->async_tx.tx_submit = sh_dmae_tx_submit;
  229. desc->mark = DESC_IDLE;
  230. spin_lock_bh(&sh_chan->desc_lock);
  231. list_add(&desc->node, &sh_chan->ld_free);
  232. sh_chan->descs_allocated++;
  233. }
  234. spin_unlock_bh(&sh_chan->desc_lock);
  235. return sh_chan->descs_allocated;
  236. }
  237. /*
  238. * sh_dma_free_chan_resources - Free all resources of the channel.
  239. */
  240. static void sh_dmae_free_chan_resources(struct dma_chan *chan)
  241. {
  242. struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
  243. struct sh_desc *desc, *_desc;
  244. LIST_HEAD(list);
  245. /* Prepared and not submitted descriptors can still be on the queue */
  246. if (!list_empty(&sh_chan->ld_queue))
  247. sh_dmae_chan_ld_cleanup(sh_chan, true);
  248. spin_lock_bh(&sh_chan->desc_lock);
  249. list_splice_init(&sh_chan->ld_free, &list);
  250. sh_chan->descs_allocated = 0;
  251. spin_unlock_bh(&sh_chan->desc_lock);
  252. list_for_each_entry_safe(desc, _desc, &list, node)
  253. kfree(desc);
  254. }
  255. static struct dma_async_tx_descriptor *sh_dmae_prep_memcpy(
  256. struct dma_chan *chan, dma_addr_t dma_dest, dma_addr_t dma_src,
  257. size_t len, unsigned long flags)
  258. {
  259. struct sh_dmae_chan *sh_chan;
  260. struct sh_desc *first = NULL, *prev = NULL, *new;
  261. size_t copy_size;
  262. LIST_HEAD(tx_list);
  263. int chunks = (len + SH_DMA_TCR_MAX) / (SH_DMA_TCR_MAX + 1);
  264. if (!chan)
  265. return NULL;
  266. if (!len)
  267. return NULL;
  268. sh_chan = to_sh_chan(chan);
  269. /* Have to lock the whole loop to protect against concurrent release */
  270. spin_lock_bh(&sh_chan->desc_lock);
  271. /*
  272. * Chaining:
  273. * first descriptor is what user is dealing with in all API calls, its
  274. * cookie is at first set to -EBUSY, at tx-submit to a positive
  275. * number
  276. * if more than one chunk is needed further chunks have cookie = -EINVAL
  277. * the last chunk, if not equal to the first, has cookie = -ENOSPC
  278. * all chunks are linked onto the tx_list head with their .node heads
  279. * only during this function, then they are immediately spliced
  280. * back onto the free list in form of a chain
  281. */
  282. do {
  283. /* Allocate the link descriptor from the free list */
  284. new = sh_dmae_get_desc(sh_chan);
  285. if (!new) {
  286. dev_err(sh_chan->dev,
  287. "No free memory for link descriptor\n");
  288. list_for_each_entry(new, &tx_list, node)
  289. new->mark = DESC_IDLE;
  290. list_splice(&tx_list, &sh_chan->ld_free);
  291. spin_unlock_bh(&sh_chan->desc_lock);
  292. return NULL;
  293. }
  294. copy_size = min(len, (size_t)SH_DMA_TCR_MAX + 1);
  295. new->hw.sar = dma_src;
  296. new->hw.dar = dma_dest;
  297. new->hw.tcr = copy_size;
  298. if (!first) {
  299. /* First desc */
  300. new->async_tx.cookie = -EBUSY;
  301. first = new;
  302. } else {
  303. /* Other desc - invisible to the user */
  304. new->async_tx.cookie = -EINVAL;
  305. }
  306. dev_dbg(sh_chan->dev,
  307. "chaining %u of %u with %p, dst %x, cookie %d\n",
  308. copy_size, len, &new->async_tx, dma_dest,
  309. new->async_tx.cookie);
  310. new->mark = DESC_PREPARED;
  311. new->async_tx.flags = flags;
  312. new->chunks = chunks--;
  313. prev = new;
  314. len -= copy_size;
  315. dma_src += copy_size;
  316. dma_dest += copy_size;
  317. /* Insert the link descriptor to the LD ring */
  318. list_add_tail(&new->node, &tx_list);
  319. } while (len);
  320. if (new != first)
  321. new->async_tx.cookie = -ENOSPC;
  322. /* Put them back on the free list, so, they don't get lost */
  323. list_splice_tail(&tx_list, &sh_chan->ld_free);
  324. spin_unlock_bh(&sh_chan->desc_lock);
  325. return &first->async_tx;
  326. }
  327. static dma_async_tx_callback __ld_cleanup(struct sh_dmae_chan *sh_chan, bool all)
  328. {
  329. struct sh_desc *desc, *_desc;
  330. /* Is the "exposed" head of a chain acked? */
  331. bool head_acked = false;
  332. dma_cookie_t cookie = 0;
  333. dma_async_tx_callback callback = NULL;
  334. void *param = NULL;
  335. spin_lock_bh(&sh_chan->desc_lock);
  336. list_for_each_entry_safe(desc, _desc, &sh_chan->ld_queue, node) {
  337. struct dma_async_tx_descriptor *tx = &desc->async_tx;
  338. BUG_ON(tx->cookie > 0 && tx->cookie != desc->cookie);
  339. BUG_ON(desc->mark != DESC_SUBMITTED &&
  340. desc->mark != DESC_COMPLETED &&
  341. desc->mark != DESC_WAITING);
  342. /*
  343. * queue is ordered, and we use this loop to (1) clean up all
  344. * completed descriptors, and to (2) update descriptor flags of
  345. * any chunks in a (partially) completed chain
  346. */
  347. if (!all && desc->mark == DESC_SUBMITTED &&
  348. desc->cookie != cookie)
  349. break;
  350. if (tx->cookie > 0)
  351. cookie = tx->cookie;
  352. if (desc->mark == DESC_COMPLETED && desc->chunks == 1) {
  353. BUG_ON(sh_chan->completed_cookie != desc->cookie - 1);
  354. sh_chan->completed_cookie = desc->cookie;
  355. }
  356. /* Call callback on the last chunk */
  357. if (desc->mark == DESC_COMPLETED && tx->callback) {
  358. desc->mark = DESC_WAITING;
  359. callback = tx->callback;
  360. param = tx->callback_param;
  361. dev_dbg(sh_chan->dev, "descriptor #%d@%p on %d callback\n",
  362. tx->cookie, tx, sh_chan->id);
  363. BUG_ON(desc->chunks != 1);
  364. break;
  365. }
  366. if (tx->cookie > 0 || tx->cookie == -EBUSY) {
  367. if (desc->mark == DESC_COMPLETED) {
  368. BUG_ON(tx->cookie < 0);
  369. desc->mark = DESC_WAITING;
  370. }
  371. head_acked = async_tx_test_ack(tx);
  372. } else {
  373. switch (desc->mark) {
  374. case DESC_COMPLETED:
  375. desc->mark = DESC_WAITING;
  376. /* Fall through */
  377. case DESC_WAITING:
  378. if (head_acked)
  379. async_tx_ack(&desc->async_tx);
  380. }
  381. }
  382. dev_dbg(sh_chan->dev, "descriptor %p #%d completed.\n",
  383. tx, tx->cookie);
  384. if (((desc->mark == DESC_COMPLETED ||
  385. desc->mark == DESC_WAITING) &&
  386. async_tx_test_ack(&desc->async_tx)) || all) {
  387. /* Remove from ld_queue list */
  388. desc->mark = DESC_IDLE;
  389. list_move(&desc->node, &sh_chan->ld_free);
  390. }
  391. }
  392. spin_unlock_bh(&sh_chan->desc_lock);
  393. if (callback)
  394. callback(param);
  395. return callback;
  396. }
  397. /*
  398. * sh_chan_ld_cleanup - Clean up link descriptors
  399. *
  400. * This function cleans up the ld_queue of DMA channel.
  401. */
  402. static void sh_dmae_chan_ld_cleanup(struct sh_dmae_chan *sh_chan, bool all)
  403. {
  404. while (__ld_cleanup(sh_chan, all))
  405. ;
  406. }
  407. static void sh_chan_xfer_ld_queue(struct sh_dmae_chan *sh_chan)
  408. {
  409. struct sh_desc *sd;
  410. spin_lock_bh(&sh_chan->desc_lock);
  411. /* DMA work check */
  412. if (dmae_is_busy(sh_chan)) {
  413. spin_unlock_bh(&sh_chan->desc_lock);
  414. return;
  415. }
  416. /* Find the first un-transfer desciptor */
  417. list_for_each_entry(sd, &sh_chan->ld_queue, node)
  418. if (sd->mark == DESC_SUBMITTED) {
  419. /* Get the ld start address from ld_queue */
  420. dmae_set_reg(sh_chan, &sd->hw);
  421. dmae_start(sh_chan);
  422. break;
  423. }
  424. spin_unlock_bh(&sh_chan->desc_lock);
  425. }
  426. static void sh_dmae_memcpy_issue_pending(struct dma_chan *chan)
  427. {
  428. struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
  429. sh_chan_xfer_ld_queue(sh_chan);
  430. }
  431. static enum dma_status sh_dmae_is_complete(struct dma_chan *chan,
  432. dma_cookie_t cookie,
  433. dma_cookie_t *done,
  434. dma_cookie_t *used)
  435. {
  436. struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
  437. dma_cookie_t last_used;
  438. dma_cookie_t last_complete;
  439. sh_dmae_chan_ld_cleanup(sh_chan, false);
  440. last_used = chan->cookie;
  441. last_complete = sh_chan->completed_cookie;
  442. BUG_ON(last_complete < 0);
  443. if (done)
  444. *done = last_complete;
  445. if (used)
  446. *used = last_used;
  447. return dma_async_is_complete(cookie, last_complete, last_used);
  448. }
  449. static irqreturn_t sh_dmae_interrupt(int irq, void *data)
  450. {
  451. irqreturn_t ret = IRQ_NONE;
  452. struct sh_dmae_chan *sh_chan = (struct sh_dmae_chan *)data;
  453. u32 chcr = sh_dmae_readl(sh_chan, CHCR);
  454. if (chcr & CHCR_TE) {
  455. /* DMA stop */
  456. dmae_halt(sh_chan);
  457. ret = IRQ_HANDLED;
  458. tasklet_schedule(&sh_chan->tasklet);
  459. }
  460. return ret;
  461. }
  462. #if defined(CONFIG_CPU_SH4)
  463. static irqreturn_t sh_dmae_err(int irq, void *data)
  464. {
  465. int err = 0;
  466. struct sh_dmae_device *shdev = (struct sh_dmae_device *)data;
  467. /* IRQ Multi */
  468. if (shdev->pdata.mode & SHDMA_MIX_IRQ) {
  469. int cnt = 0;
  470. switch (irq) {
  471. #if defined(DMTE6_IRQ) && defined(DMAE1_IRQ)
  472. case DMTE6_IRQ:
  473. cnt++;
  474. #endif
  475. case DMTE0_IRQ:
  476. if (dmaor_read_reg(cnt) & (DMAOR_NMIF | DMAOR_AE)) {
  477. disable_irq(irq);
  478. return IRQ_HANDLED;
  479. }
  480. default:
  481. return IRQ_NONE;
  482. }
  483. } else {
  484. /* reset dma controller */
  485. err = sh_dmae_rst(0);
  486. if (err)
  487. return err;
  488. #ifdef SH_DMAC_BASE1
  489. if (shdev->pdata.mode & SHDMA_DMAOR1) {
  490. err = sh_dmae_rst(1);
  491. if (err)
  492. return err;
  493. }
  494. #endif
  495. disable_irq(irq);
  496. return IRQ_HANDLED;
  497. }
  498. }
  499. #endif
  500. static void dmae_do_tasklet(unsigned long data)
  501. {
  502. struct sh_dmae_chan *sh_chan = (struct sh_dmae_chan *)data;
  503. struct sh_desc *desc;
  504. u32 sar_buf = sh_dmae_readl(sh_chan, SAR);
  505. spin_lock(&sh_chan->desc_lock);
  506. list_for_each_entry(desc, &sh_chan->ld_queue, node) {
  507. if ((desc->hw.sar + desc->hw.tcr) == sar_buf &&
  508. desc->mark == DESC_SUBMITTED) {
  509. dev_dbg(sh_chan->dev, "done #%d@%p dst %u\n",
  510. desc->async_tx.cookie, &desc->async_tx,
  511. desc->hw.dar);
  512. desc->mark = DESC_COMPLETED;
  513. break;
  514. }
  515. }
  516. spin_unlock(&sh_chan->desc_lock);
  517. /* Next desc */
  518. sh_chan_xfer_ld_queue(sh_chan);
  519. sh_dmae_chan_ld_cleanup(sh_chan, false);
  520. }
  521. static unsigned int get_dmae_irq(unsigned int id)
  522. {
  523. unsigned int irq = 0;
  524. if (id < ARRAY_SIZE(dmte_irq_map))
  525. irq = dmte_irq_map[id];
  526. return irq;
  527. }
  528. static int __devinit sh_dmae_chan_probe(struct sh_dmae_device *shdev, int id)
  529. {
  530. int err;
  531. unsigned int irq = get_dmae_irq(id);
  532. unsigned long irqflags = IRQF_DISABLED;
  533. struct sh_dmae_chan *new_sh_chan;
  534. /* alloc channel */
  535. new_sh_chan = kzalloc(sizeof(struct sh_dmae_chan), GFP_KERNEL);
  536. if (!new_sh_chan) {
  537. dev_err(shdev->common.dev,
  538. "No free memory for allocating dma channels!\n");
  539. return -ENOMEM;
  540. }
  541. new_sh_chan->dev = shdev->common.dev;
  542. new_sh_chan->id = id;
  543. /* Init DMA tasklet */
  544. tasklet_init(&new_sh_chan->tasklet, dmae_do_tasklet,
  545. (unsigned long)new_sh_chan);
  546. /* Init the channel */
  547. dmae_init(new_sh_chan);
  548. spin_lock_init(&new_sh_chan->desc_lock);
  549. /* Init descripter manage list */
  550. INIT_LIST_HEAD(&new_sh_chan->ld_queue);
  551. INIT_LIST_HEAD(&new_sh_chan->ld_free);
  552. /* copy struct dma_device */
  553. new_sh_chan->common.device = &shdev->common;
  554. /* Add the channel to DMA device channel list */
  555. list_add_tail(&new_sh_chan->common.device_node,
  556. &shdev->common.channels);
  557. shdev->common.chancnt++;
  558. if (shdev->pdata.mode & SHDMA_MIX_IRQ) {
  559. irqflags = IRQF_SHARED;
  560. #if defined(DMTE6_IRQ)
  561. if (irq >= DMTE6_IRQ)
  562. irq = DMTE6_IRQ;
  563. else
  564. #endif
  565. irq = DMTE0_IRQ;
  566. }
  567. snprintf(new_sh_chan->dev_id, sizeof(new_sh_chan->dev_id),
  568. "sh-dmae%d", new_sh_chan->id);
  569. /* set up channel irq */
  570. err = request_irq(irq, &sh_dmae_interrupt, irqflags,
  571. new_sh_chan->dev_id, new_sh_chan);
  572. if (err) {
  573. dev_err(shdev->common.dev, "DMA channel %d request_irq error "
  574. "with return %d\n", id, err);
  575. goto err_no_irq;
  576. }
  577. /* CHCR register control function */
  578. new_sh_chan->set_chcr = dmae_set_chcr;
  579. /* DMARS register control function */
  580. new_sh_chan->set_dmars = dmae_set_dmars;
  581. shdev->chan[id] = new_sh_chan;
  582. return 0;
  583. err_no_irq:
  584. /* remove from dmaengine device node */
  585. list_del(&new_sh_chan->common.device_node);
  586. kfree(new_sh_chan);
  587. return err;
  588. }
  589. static void sh_dmae_chan_remove(struct sh_dmae_device *shdev)
  590. {
  591. int i;
  592. for (i = shdev->common.chancnt - 1 ; i >= 0 ; i--) {
  593. if (shdev->chan[i]) {
  594. struct sh_dmae_chan *shchan = shdev->chan[i];
  595. if (!(shdev->pdata.mode & SHDMA_MIX_IRQ))
  596. free_irq(dmte_irq_map[i], shchan);
  597. list_del(&shchan->common.device_node);
  598. kfree(shchan);
  599. shdev->chan[i] = NULL;
  600. }
  601. }
  602. shdev->common.chancnt = 0;
  603. }
  604. static int __init sh_dmae_probe(struct platform_device *pdev)
  605. {
  606. int err = 0, cnt, ecnt;
  607. unsigned long irqflags = IRQF_DISABLED;
  608. #if defined(CONFIG_CPU_SH4)
  609. int eirq[] = { DMAE0_IRQ,
  610. #if defined(DMAE1_IRQ)
  611. DMAE1_IRQ
  612. #endif
  613. };
  614. #endif
  615. struct sh_dmae_device *shdev;
  616. /* get platform data */
  617. if (!pdev->dev.platform_data)
  618. return -ENODEV;
  619. shdev = kzalloc(sizeof(struct sh_dmae_device), GFP_KERNEL);
  620. if (!shdev) {
  621. dev_err(&pdev->dev, "No enough memory\n");
  622. return -ENOMEM;
  623. }
  624. /* platform data */
  625. memcpy(&shdev->pdata, pdev->dev.platform_data,
  626. sizeof(struct sh_dmae_pdata));
  627. /* reset dma controller */
  628. err = sh_dmae_rst(0);
  629. if (err)
  630. goto rst_err;
  631. /* SH7780/85/23 has DMAOR1 */
  632. if (shdev->pdata.mode & SHDMA_DMAOR1) {
  633. err = sh_dmae_rst(1);
  634. if (err)
  635. goto rst_err;
  636. }
  637. INIT_LIST_HEAD(&shdev->common.channels);
  638. dma_cap_set(DMA_MEMCPY, shdev->common.cap_mask);
  639. shdev->common.device_alloc_chan_resources
  640. = sh_dmae_alloc_chan_resources;
  641. shdev->common.device_free_chan_resources = sh_dmae_free_chan_resources;
  642. shdev->common.device_prep_dma_memcpy = sh_dmae_prep_memcpy;
  643. shdev->common.device_is_tx_complete = sh_dmae_is_complete;
  644. shdev->common.device_issue_pending = sh_dmae_memcpy_issue_pending;
  645. shdev->common.dev = &pdev->dev;
  646. /* Default transfer size of 32 bytes requires 32-byte alignment */
  647. shdev->common.copy_align = 5;
  648. #if defined(CONFIG_CPU_SH4)
  649. /* Non Mix IRQ mode SH7722/SH7730 etc... */
  650. if (shdev->pdata.mode & SHDMA_MIX_IRQ) {
  651. irqflags = IRQF_SHARED;
  652. eirq[0] = DMTE0_IRQ;
  653. #if defined(DMTE6_IRQ) && defined(DMAE1_IRQ)
  654. eirq[1] = DMTE6_IRQ;
  655. #endif
  656. }
  657. for (ecnt = 0 ; ecnt < ARRAY_SIZE(eirq); ecnt++) {
  658. err = request_irq(eirq[ecnt], sh_dmae_err, irqflags,
  659. "DMAC Address Error", shdev);
  660. if (err) {
  661. dev_err(&pdev->dev, "DMA device request_irq"
  662. "error (irq %d) with return %d\n",
  663. eirq[ecnt], err);
  664. goto eirq_err;
  665. }
  666. }
  667. #endif /* CONFIG_CPU_SH4 */
  668. /* Create DMA Channel */
  669. for (cnt = 0 ; cnt < MAX_DMA_CHANNELS ; cnt++) {
  670. err = sh_dmae_chan_probe(shdev, cnt);
  671. if (err)
  672. goto chan_probe_err;
  673. }
  674. platform_set_drvdata(pdev, shdev);
  675. dma_async_device_register(&shdev->common);
  676. return err;
  677. chan_probe_err:
  678. sh_dmae_chan_remove(shdev);
  679. eirq_err:
  680. for (ecnt-- ; ecnt >= 0; ecnt--)
  681. free_irq(eirq[ecnt], shdev);
  682. rst_err:
  683. kfree(shdev);
  684. return err;
  685. }
  686. static int __exit sh_dmae_remove(struct platform_device *pdev)
  687. {
  688. struct sh_dmae_device *shdev = platform_get_drvdata(pdev);
  689. dma_async_device_unregister(&shdev->common);
  690. if (shdev->pdata.mode & SHDMA_MIX_IRQ) {
  691. free_irq(DMTE0_IRQ, shdev);
  692. #if defined(DMTE6_IRQ)
  693. free_irq(DMTE6_IRQ, shdev);
  694. #endif
  695. }
  696. /* channel data remove */
  697. sh_dmae_chan_remove(shdev);
  698. if (!(shdev->pdata.mode & SHDMA_MIX_IRQ)) {
  699. free_irq(DMAE0_IRQ, shdev);
  700. #if defined(DMAE1_IRQ)
  701. free_irq(DMAE1_IRQ, shdev);
  702. #endif
  703. }
  704. kfree(shdev);
  705. return 0;
  706. }
  707. static void sh_dmae_shutdown(struct platform_device *pdev)
  708. {
  709. struct sh_dmae_device *shdev = platform_get_drvdata(pdev);
  710. sh_dmae_ctl_stop(0);
  711. if (shdev->pdata.mode & SHDMA_DMAOR1)
  712. sh_dmae_ctl_stop(1);
  713. }
  714. static struct platform_driver sh_dmae_driver = {
  715. .remove = __exit_p(sh_dmae_remove),
  716. .shutdown = sh_dmae_shutdown,
  717. .driver = {
  718. .name = "sh-dma-engine",
  719. },
  720. };
  721. static int __init sh_dmae_init(void)
  722. {
  723. return platform_driver_probe(&sh_dmae_driver, sh_dmae_probe);
  724. }
  725. module_init(sh_dmae_init);
  726. static void __exit sh_dmae_exit(void)
  727. {
  728. platform_driver_unregister(&sh_dmae_driver);
  729. }
  730. module_exit(sh_dmae_exit);
  731. MODULE_AUTHOR("Nobuhiro Iwamatsu <iwamatsu.nobuhiro@renesas.com>");
  732. MODULE_DESCRIPTION("Renesas SH DMA Engine driver");
  733. MODULE_LICENSE("GPL");