shdma.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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 bool dmae_is_busy(struct sh_dmae_chan *sh_chan)
  86. {
  87. u32 chcr = sh_dmae_readl(sh_chan, CHCR);
  88. if ((chcr & (CHCR_DE | CHCR_TE)) == CHCR_DE)
  89. return true; /* working */
  90. return false; /* waiting */
  91. }
  92. static unsigned int ts_shift[] = TS_SHIFT;
  93. static inline unsigned int calc_xmit_shift(struct sh_dmae_chan *sh_chan)
  94. {
  95. u32 chcr = sh_dmae_readl(sh_chan, CHCR);
  96. int cnt = ((chcr & CHCR_TS_LOW_MASK) >> CHCR_TS_LOW_SHIFT) |
  97. ((chcr & CHCR_TS_HIGH_MASK) >> CHCR_TS_HIGH_SHIFT);
  98. return ts_shift[cnt];
  99. }
  100. static void dmae_set_reg(struct sh_dmae_chan *sh_chan, struct sh_dmae_regs *hw)
  101. {
  102. sh_dmae_writel(sh_chan, hw->sar, SAR);
  103. sh_dmae_writel(sh_chan, hw->dar, DAR);
  104. sh_dmae_writel(sh_chan, hw->tcr >> calc_xmit_shift(sh_chan), TCR);
  105. }
  106. static void dmae_start(struct sh_dmae_chan *sh_chan)
  107. {
  108. u32 chcr = sh_dmae_readl(sh_chan, CHCR);
  109. chcr |= CHCR_DE | CHCR_IE;
  110. sh_dmae_writel(sh_chan, chcr, CHCR);
  111. }
  112. static void dmae_halt(struct sh_dmae_chan *sh_chan)
  113. {
  114. u32 chcr = sh_dmae_readl(sh_chan, CHCR);
  115. chcr &= ~(CHCR_DE | CHCR_TE | CHCR_IE);
  116. sh_dmae_writel(sh_chan, chcr, CHCR);
  117. }
  118. static int dmae_set_chcr(struct sh_dmae_chan *sh_chan, u32 val)
  119. {
  120. /* When DMA was working, can not set data to CHCR */
  121. if (dmae_is_busy(sh_chan))
  122. return -EBUSY;
  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. if (dmae_is_busy(sh_chan))
  135. return -EBUSY;
  136. if (sh_chan->id & DMARS_CHAN_MSK)
  137. shift = DMARS_SHIFT;
  138. switch (sh_chan->id) {
  139. /* DMARS0 */
  140. case 0:
  141. case 1:
  142. addr = SH_DMARS_BASE;
  143. break;
  144. /* DMARS1 */
  145. case 2:
  146. case 3:
  147. addr = (SH_DMARS_BASE + DMARS1_ADDR);
  148. break;
  149. /* DMARS2 */
  150. case 4:
  151. case 5:
  152. addr = (SH_DMARS_BASE + DMARS2_ADDR);
  153. break;
  154. default:
  155. return -EINVAL;
  156. }
  157. ctrl_outw((val << shift) |
  158. (ctrl_inw(addr) & (shift ? 0xFF00 : 0x00FF)),
  159. addr);
  160. return 0;
  161. }
  162. static dma_cookie_t sh_dmae_tx_submit(struct dma_async_tx_descriptor *tx)
  163. {
  164. struct sh_desc *desc = tx_to_sh_desc(tx), *chunk, *last = desc, *c;
  165. struct sh_dmae_chan *sh_chan = to_sh_chan(tx->chan);
  166. dma_async_tx_callback callback = tx->callback;
  167. dma_cookie_t cookie;
  168. spin_lock_bh(&sh_chan->desc_lock);
  169. cookie = sh_chan->common.cookie;
  170. cookie++;
  171. if (cookie < 0)
  172. cookie = 1;
  173. sh_chan->common.cookie = cookie;
  174. tx->cookie = cookie;
  175. /* Mark all chunks of this descriptor as submitted, move to the queue */
  176. list_for_each_entry_safe(chunk, c, desc->node.prev, node) {
  177. /*
  178. * All chunks are on the global ld_free, so, we have to find
  179. * the end of the chain ourselves
  180. */
  181. if (chunk != desc && (chunk->mark == DESC_IDLE ||
  182. chunk->async_tx.cookie > 0 ||
  183. chunk->async_tx.cookie == -EBUSY ||
  184. &chunk->node == &sh_chan->ld_free))
  185. break;
  186. chunk->mark = DESC_SUBMITTED;
  187. /* Callback goes to the last chunk */
  188. chunk->async_tx.callback = NULL;
  189. chunk->cookie = cookie;
  190. list_move_tail(&chunk->node, &sh_chan->ld_queue);
  191. last = chunk;
  192. }
  193. last->async_tx.callback = callback;
  194. last->async_tx.callback_param = tx->callback_param;
  195. dev_dbg(sh_chan->dev, "submit #%d@%p on %d: %x[%d] -> %x\n",
  196. tx->cookie, &last->async_tx, sh_chan->id,
  197. desc->hw.sar, desc->hw.tcr, desc->hw.dar);
  198. spin_unlock_bh(&sh_chan->desc_lock);
  199. return cookie;
  200. }
  201. /* Called with desc_lock held */
  202. static struct sh_desc *sh_dmae_get_desc(struct sh_dmae_chan *sh_chan)
  203. {
  204. struct sh_desc *desc;
  205. list_for_each_entry(desc, &sh_chan->ld_free, node)
  206. if (desc->mark != DESC_PREPARED) {
  207. BUG_ON(desc->mark != DESC_IDLE);
  208. list_del(&desc->node);
  209. return desc;
  210. }
  211. return NULL;
  212. }
  213. static int sh_dmae_alloc_chan_resources(struct dma_chan *chan)
  214. {
  215. struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
  216. struct sh_desc *desc;
  217. spin_lock_bh(&sh_chan->desc_lock);
  218. while (sh_chan->descs_allocated < NR_DESCS_PER_CHANNEL) {
  219. spin_unlock_bh(&sh_chan->desc_lock);
  220. desc = kzalloc(sizeof(struct sh_desc), GFP_KERNEL);
  221. if (!desc) {
  222. spin_lock_bh(&sh_chan->desc_lock);
  223. break;
  224. }
  225. dma_async_tx_descriptor_init(&desc->async_tx,
  226. &sh_chan->common);
  227. desc->async_tx.tx_submit = sh_dmae_tx_submit;
  228. desc->mark = DESC_IDLE;
  229. spin_lock_bh(&sh_chan->desc_lock);
  230. list_add(&desc->node, &sh_chan->ld_free);
  231. sh_chan->descs_allocated++;
  232. }
  233. spin_unlock_bh(&sh_chan->desc_lock);
  234. return sh_chan->descs_allocated;
  235. }
  236. /*
  237. * sh_dma_free_chan_resources - Free all resources of the channel.
  238. */
  239. static void sh_dmae_free_chan_resources(struct dma_chan *chan)
  240. {
  241. struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
  242. struct sh_desc *desc, *_desc;
  243. LIST_HEAD(list);
  244. /* Prepared and not submitted descriptors can still be on the queue */
  245. if (!list_empty(&sh_chan->ld_queue))
  246. sh_dmae_chan_ld_cleanup(sh_chan, true);
  247. spin_lock_bh(&sh_chan->desc_lock);
  248. list_splice_init(&sh_chan->ld_free, &list);
  249. sh_chan->descs_allocated = 0;
  250. spin_unlock_bh(&sh_chan->desc_lock);
  251. list_for_each_entry_safe(desc, _desc, &list, node)
  252. kfree(desc);
  253. }
  254. /*
  255. * sh_dmae_add_desc - get, set up and return one transfer descriptor
  256. * @sh_chan: DMA channel
  257. * @flags: DMA transfer flags
  258. * @dest: destination DMA address, incremented when direction equals
  259. * DMA_FROM_DEVICE or DMA_BIDIRECTIONAL
  260. * @src: source DMA address, incremented when direction equals
  261. * DMA_TO_DEVICE or DMA_BIDIRECTIONAL
  262. * @len: DMA transfer length
  263. * @first: if NULL, set to the current descriptor and cookie set to -EBUSY
  264. * @direction: needed for slave DMA to decide which address to keep constant,
  265. * equals DMA_BIDIRECTIONAL for MEMCPY
  266. * Returns 0 or an error
  267. * Locks: called with desc_lock held
  268. */
  269. static struct sh_desc *sh_dmae_add_desc(struct sh_dmae_chan *sh_chan,
  270. unsigned long flags, dma_addr_t *dest, dma_addr_t *src, size_t *len,
  271. struct sh_desc **first, enum dma_data_direction direction)
  272. {
  273. struct sh_desc *new;
  274. size_t copy_size;
  275. if (!*len)
  276. return NULL;
  277. /* Allocate the link descriptor from the free list */
  278. new = sh_dmae_get_desc(sh_chan);
  279. if (!new) {
  280. dev_err(sh_chan->dev, "No free link descriptor available\n");
  281. return NULL;
  282. }
  283. copy_size = min(*len, (size_t)SH_DMA_TCR_MAX + 1);
  284. new->hw.sar = *src;
  285. new->hw.dar = *dest;
  286. new->hw.tcr = copy_size;
  287. if (!*first) {
  288. /* First desc */
  289. new->async_tx.cookie = -EBUSY;
  290. *first = new;
  291. } else {
  292. /* Other desc - invisible to the user */
  293. new->async_tx.cookie = -EINVAL;
  294. }
  295. dev_dbg(sh_chan->dev, "chaining (%u/%u)@%x -> %x with %p, cookie %d\n",
  296. copy_size, *len, *src, *dest, &new->async_tx,
  297. new->async_tx.cookie);
  298. new->mark = DESC_PREPARED;
  299. new->async_tx.flags = flags;
  300. *len -= copy_size;
  301. if (direction == DMA_BIDIRECTIONAL || direction == DMA_TO_DEVICE)
  302. *src += copy_size;
  303. if (direction == DMA_BIDIRECTIONAL || direction == DMA_FROM_DEVICE)
  304. *dest += copy_size;
  305. return new;
  306. }
  307. /*
  308. * sh_dmae_prep_sg - prepare transfer descriptors from an SG list
  309. *
  310. * Common routine for public (MEMCPY) and slave DMA. The MEMCPY case is also
  311. * converted to scatter-gather to guarantee consistent locking and a correct
  312. * list manipulation. For slave DMA direction carries the usual meaning, and,
  313. * logically, the SG list is RAM and the addr variable contains slave address,
  314. * e.g., the FIFO I/O register. For MEMCPY direction equals DMA_BIDIRECTIONAL
  315. * and the SG list contains only one element and points at the source buffer.
  316. */
  317. static struct dma_async_tx_descriptor *sh_dmae_prep_sg(struct sh_dmae_chan *sh_chan,
  318. struct scatterlist *sgl, unsigned int sg_len, dma_addr_t *addr,
  319. enum dma_data_direction direction, unsigned long flags)
  320. {
  321. struct scatterlist *sg;
  322. struct sh_desc *first = NULL, *new = NULL /* compiler... */;
  323. LIST_HEAD(tx_list);
  324. int chunks = 0;
  325. int i;
  326. if (!sg_len)
  327. return NULL;
  328. for_each_sg(sgl, sg, sg_len, i)
  329. chunks += (sg_dma_len(sg) + SH_DMA_TCR_MAX) /
  330. (SH_DMA_TCR_MAX + 1);
  331. /* Have to lock the whole loop to protect against concurrent release */
  332. spin_lock_bh(&sh_chan->desc_lock);
  333. /*
  334. * Chaining:
  335. * first descriptor is what user is dealing with in all API calls, its
  336. * cookie is at first set to -EBUSY, at tx-submit to a positive
  337. * number
  338. * if more than one chunk is needed further chunks have cookie = -EINVAL
  339. * the last chunk, if not equal to the first, has cookie = -ENOSPC
  340. * all chunks are linked onto the tx_list head with their .node heads
  341. * only during this function, then they are immediately spliced
  342. * back onto the free list in form of a chain
  343. */
  344. for_each_sg(sgl, sg, sg_len, i) {
  345. dma_addr_t sg_addr = sg_dma_address(sg);
  346. size_t len = sg_dma_len(sg);
  347. if (!len)
  348. goto err_get_desc;
  349. do {
  350. dev_dbg(sh_chan->dev, "Add SG #%d@%p[%d], dma %llx\n",
  351. i, sg, len, (unsigned long long)sg_addr);
  352. if (direction == DMA_FROM_DEVICE)
  353. new = sh_dmae_add_desc(sh_chan, flags,
  354. &sg_addr, addr, &len, &first,
  355. direction);
  356. else
  357. new = sh_dmae_add_desc(sh_chan, flags,
  358. addr, &sg_addr, &len, &first,
  359. direction);
  360. if (!new)
  361. goto err_get_desc;
  362. new->chunks = chunks--;
  363. list_add_tail(&new->node, &tx_list);
  364. } while (len);
  365. }
  366. if (new != first)
  367. new->async_tx.cookie = -ENOSPC;
  368. /* Put them back on the free list, so, they don't get lost */
  369. list_splice_tail(&tx_list, &sh_chan->ld_free);
  370. spin_unlock_bh(&sh_chan->desc_lock);
  371. return &first->async_tx;
  372. err_get_desc:
  373. list_for_each_entry(new, &tx_list, node)
  374. new->mark = DESC_IDLE;
  375. list_splice(&tx_list, &sh_chan->ld_free);
  376. spin_unlock_bh(&sh_chan->desc_lock);
  377. return NULL;
  378. }
  379. static struct dma_async_tx_descriptor *sh_dmae_prep_memcpy(
  380. struct dma_chan *chan, dma_addr_t dma_dest, dma_addr_t dma_src,
  381. size_t len, unsigned long flags)
  382. {
  383. struct sh_dmae_chan *sh_chan;
  384. struct scatterlist sg;
  385. if (!chan || !len)
  386. return NULL;
  387. sh_chan = to_sh_chan(chan);
  388. sg_init_table(&sg, 1);
  389. sg_set_page(&sg, pfn_to_page(PFN_DOWN(dma_src)), len,
  390. offset_in_page(dma_src));
  391. sg_dma_address(&sg) = dma_src;
  392. sg_dma_len(&sg) = len;
  393. return sh_dmae_prep_sg(sh_chan, &sg, 1, &dma_dest, DMA_BIDIRECTIONAL,
  394. flags);
  395. }
  396. static dma_async_tx_callback __ld_cleanup(struct sh_dmae_chan *sh_chan, bool all)
  397. {
  398. struct sh_desc *desc, *_desc;
  399. /* Is the "exposed" head of a chain acked? */
  400. bool head_acked = false;
  401. dma_cookie_t cookie = 0;
  402. dma_async_tx_callback callback = NULL;
  403. void *param = NULL;
  404. spin_lock_bh(&sh_chan->desc_lock);
  405. list_for_each_entry_safe(desc, _desc, &sh_chan->ld_queue, node) {
  406. struct dma_async_tx_descriptor *tx = &desc->async_tx;
  407. BUG_ON(tx->cookie > 0 && tx->cookie != desc->cookie);
  408. BUG_ON(desc->mark != DESC_SUBMITTED &&
  409. desc->mark != DESC_COMPLETED &&
  410. desc->mark != DESC_WAITING);
  411. /*
  412. * queue is ordered, and we use this loop to (1) clean up all
  413. * completed descriptors, and to (2) update descriptor flags of
  414. * any chunks in a (partially) completed chain
  415. */
  416. if (!all && desc->mark == DESC_SUBMITTED &&
  417. desc->cookie != cookie)
  418. break;
  419. if (tx->cookie > 0)
  420. cookie = tx->cookie;
  421. if (desc->mark == DESC_COMPLETED && desc->chunks == 1) {
  422. BUG_ON(sh_chan->completed_cookie != desc->cookie - 1);
  423. sh_chan->completed_cookie = desc->cookie;
  424. }
  425. /* Call callback on the last chunk */
  426. if (desc->mark == DESC_COMPLETED && tx->callback) {
  427. desc->mark = DESC_WAITING;
  428. callback = tx->callback;
  429. param = tx->callback_param;
  430. dev_dbg(sh_chan->dev, "descriptor #%d@%p on %d callback\n",
  431. tx->cookie, tx, sh_chan->id);
  432. BUG_ON(desc->chunks != 1);
  433. break;
  434. }
  435. if (tx->cookie > 0 || tx->cookie == -EBUSY) {
  436. if (desc->mark == DESC_COMPLETED) {
  437. BUG_ON(tx->cookie < 0);
  438. desc->mark = DESC_WAITING;
  439. }
  440. head_acked = async_tx_test_ack(tx);
  441. } else {
  442. switch (desc->mark) {
  443. case DESC_COMPLETED:
  444. desc->mark = DESC_WAITING;
  445. /* Fall through */
  446. case DESC_WAITING:
  447. if (head_acked)
  448. async_tx_ack(&desc->async_tx);
  449. }
  450. }
  451. dev_dbg(sh_chan->dev, "descriptor %p #%d completed.\n",
  452. tx, tx->cookie);
  453. if (((desc->mark == DESC_COMPLETED ||
  454. desc->mark == DESC_WAITING) &&
  455. async_tx_test_ack(&desc->async_tx)) || all) {
  456. /* Remove from ld_queue list */
  457. desc->mark = DESC_IDLE;
  458. list_move(&desc->node, &sh_chan->ld_free);
  459. }
  460. }
  461. spin_unlock_bh(&sh_chan->desc_lock);
  462. if (callback)
  463. callback(param);
  464. return callback;
  465. }
  466. /*
  467. * sh_chan_ld_cleanup - Clean up link descriptors
  468. *
  469. * This function cleans up the ld_queue of DMA channel.
  470. */
  471. static void sh_dmae_chan_ld_cleanup(struct sh_dmae_chan *sh_chan, bool all)
  472. {
  473. while (__ld_cleanup(sh_chan, all))
  474. ;
  475. }
  476. static void sh_chan_xfer_ld_queue(struct sh_dmae_chan *sh_chan)
  477. {
  478. struct sh_desc *sd;
  479. spin_lock_bh(&sh_chan->desc_lock);
  480. /* DMA work check */
  481. if (dmae_is_busy(sh_chan)) {
  482. spin_unlock_bh(&sh_chan->desc_lock);
  483. return;
  484. }
  485. /* Find the first un-transfer desciptor */
  486. list_for_each_entry(sd, &sh_chan->ld_queue, node)
  487. if (sd->mark == DESC_SUBMITTED) {
  488. /* Get the ld start address from ld_queue */
  489. dmae_set_reg(sh_chan, &sd->hw);
  490. dmae_start(sh_chan);
  491. break;
  492. }
  493. spin_unlock_bh(&sh_chan->desc_lock);
  494. }
  495. static void sh_dmae_memcpy_issue_pending(struct dma_chan *chan)
  496. {
  497. struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
  498. sh_chan_xfer_ld_queue(sh_chan);
  499. }
  500. static enum dma_status sh_dmae_is_complete(struct dma_chan *chan,
  501. dma_cookie_t cookie,
  502. dma_cookie_t *done,
  503. dma_cookie_t *used)
  504. {
  505. struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
  506. dma_cookie_t last_used;
  507. dma_cookie_t last_complete;
  508. sh_dmae_chan_ld_cleanup(sh_chan, false);
  509. last_used = chan->cookie;
  510. last_complete = sh_chan->completed_cookie;
  511. BUG_ON(last_complete < 0);
  512. if (done)
  513. *done = last_complete;
  514. if (used)
  515. *used = last_used;
  516. return dma_async_is_complete(cookie, last_complete, last_used);
  517. }
  518. static irqreturn_t sh_dmae_interrupt(int irq, void *data)
  519. {
  520. irqreturn_t ret = IRQ_NONE;
  521. struct sh_dmae_chan *sh_chan = (struct sh_dmae_chan *)data;
  522. u32 chcr = sh_dmae_readl(sh_chan, CHCR);
  523. if (chcr & CHCR_TE) {
  524. /* DMA stop */
  525. dmae_halt(sh_chan);
  526. ret = IRQ_HANDLED;
  527. tasklet_schedule(&sh_chan->tasklet);
  528. }
  529. return ret;
  530. }
  531. #if defined(CONFIG_CPU_SH4)
  532. static irqreturn_t sh_dmae_err(int irq, void *data)
  533. {
  534. int err = 0;
  535. struct sh_dmae_device *shdev = (struct sh_dmae_device *)data;
  536. /* IRQ Multi */
  537. if (shdev->pdata.mode & SHDMA_MIX_IRQ) {
  538. int __maybe_unused cnt = 0;
  539. switch (irq) {
  540. #if defined(DMTE6_IRQ) && defined(DMAE1_IRQ)
  541. case DMTE6_IRQ:
  542. cnt++;
  543. #endif
  544. case DMTE0_IRQ:
  545. if (dmaor_read_reg(cnt) & (DMAOR_NMIF | DMAOR_AE)) {
  546. disable_irq(irq);
  547. return IRQ_HANDLED;
  548. }
  549. default:
  550. return IRQ_NONE;
  551. }
  552. } else {
  553. /* reset dma controller */
  554. err = sh_dmae_rst(0);
  555. if (err)
  556. return err;
  557. #ifdef SH_DMAC_BASE1
  558. if (shdev->pdata.mode & SHDMA_DMAOR1) {
  559. err = sh_dmae_rst(1);
  560. if (err)
  561. return err;
  562. }
  563. #endif
  564. disable_irq(irq);
  565. return IRQ_HANDLED;
  566. }
  567. }
  568. #endif
  569. static void dmae_do_tasklet(unsigned long data)
  570. {
  571. struct sh_dmae_chan *sh_chan = (struct sh_dmae_chan *)data;
  572. struct sh_desc *desc;
  573. u32 sar_buf = sh_dmae_readl(sh_chan, SAR);
  574. spin_lock(&sh_chan->desc_lock);
  575. list_for_each_entry(desc, &sh_chan->ld_queue, node) {
  576. if ((desc->hw.sar + desc->hw.tcr) == sar_buf &&
  577. desc->mark == DESC_SUBMITTED) {
  578. dev_dbg(sh_chan->dev, "done #%d@%p dst %u\n",
  579. desc->async_tx.cookie, &desc->async_tx,
  580. desc->hw.dar);
  581. desc->mark = DESC_COMPLETED;
  582. break;
  583. }
  584. }
  585. spin_unlock(&sh_chan->desc_lock);
  586. /* Next desc */
  587. sh_chan_xfer_ld_queue(sh_chan);
  588. sh_dmae_chan_ld_cleanup(sh_chan, false);
  589. }
  590. static unsigned int get_dmae_irq(unsigned int id)
  591. {
  592. unsigned int irq = 0;
  593. if (id < ARRAY_SIZE(dmte_irq_map))
  594. irq = dmte_irq_map[id];
  595. return irq;
  596. }
  597. static int __devinit sh_dmae_chan_probe(struct sh_dmae_device *shdev, int id)
  598. {
  599. int err;
  600. unsigned int irq = get_dmae_irq(id);
  601. unsigned long irqflags = IRQF_DISABLED;
  602. struct sh_dmae_chan *new_sh_chan;
  603. /* alloc channel */
  604. new_sh_chan = kzalloc(sizeof(struct sh_dmae_chan), GFP_KERNEL);
  605. if (!new_sh_chan) {
  606. dev_err(shdev->common.dev,
  607. "No free memory for allocating dma channels!\n");
  608. return -ENOMEM;
  609. }
  610. new_sh_chan->dev = shdev->common.dev;
  611. new_sh_chan->id = id;
  612. /* Init DMA tasklet */
  613. tasklet_init(&new_sh_chan->tasklet, dmae_do_tasklet,
  614. (unsigned long)new_sh_chan);
  615. /* Init the channel */
  616. dmae_init(new_sh_chan);
  617. spin_lock_init(&new_sh_chan->desc_lock);
  618. /* Init descripter manage list */
  619. INIT_LIST_HEAD(&new_sh_chan->ld_queue);
  620. INIT_LIST_HEAD(&new_sh_chan->ld_free);
  621. /* copy struct dma_device */
  622. new_sh_chan->common.device = &shdev->common;
  623. /* Add the channel to DMA device channel list */
  624. list_add_tail(&new_sh_chan->common.device_node,
  625. &shdev->common.channels);
  626. shdev->common.chancnt++;
  627. if (shdev->pdata.mode & SHDMA_MIX_IRQ) {
  628. irqflags = IRQF_SHARED;
  629. #if defined(DMTE6_IRQ)
  630. if (irq >= DMTE6_IRQ)
  631. irq = DMTE6_IRQ;
  632. else
  633. #endif
  634. irq = DMTE0_IRQ;
  635. }
  636. snprintf(new_sh_chan->dev_id, sizeof(new_sh_chan->dev_id),
  637. "sh-dmae%d", new_sh_chan->id);
  638. /* set up channel irq */
  639. err = request_irq(irq, &sh_dmae_interrupt, irqflags,
  640. new_sh_chan->dev_id, new_sh_chan);
  641. if (err) {
  642. dev_err(shdev->common.dev, "DMA channel %d request_irq error "
  643. "with return %d\n", id, err);
  644. goto err_no_irq;
  645. }
  646. /* CHCR register control function */
  647. new_sh_chan->set_chcr = dmae_set_chcr;
  648. /* DMARS register control function */
  649. new_sh_chan->set_dmars = dmae_set_dmars;
  650. shdev->chan[id] = new_sh_chan;
  651. return 0;
  652. err_no_irq:
  653. /* remove from dmaengine device node */
  654. list_del(&new_sh_chan->common.device_node);
  655. kfree(new_sh_chan);
  656. return err;
  657. }
  658. static void sh_dmae_chan_remove(struct sh_dmae_device *shdev)
  659. {
  660. int i;
  661. for (i = shdev->common.chancnt - 1 ; i >= 0 ; i--) {
  662. if (shdev->chan[i]) {
  663. struct sh_dmae_chan *shchan = shdev->chan[i];
  664. if (!(shdev->pdata.mode & SHDMA_MIX_IRQ))
  665. free_irq(dmte_irq_map[i], shchan);
  666. list_del(&shchan->common.device_node);
  667. kfree(shchan);
  668. shdev->chan[i] = NULL;
  669. }
  670. }
  671. shdev->common.chancnt = 0;
  672. }
  673. static int __init sh_dmae_probe(struct platform_device *pdev)
  674. {
  675. int err = 0, cnt, ecnt;
  676. unsigned long irqflags = IRQF_DISABLED;
  677. #if defined(CONFIG_CPU_SH4)
  678. int eirq[] = { DMAE0_IRQ,
  679. #if defined(DMAE1_IRQ)
  680. DMAE1_IRQ
  681. #endif
  682. };
  683. #endif
  684. struct sh_dmae_device *shdev;
  685. /* get platform data */
  686. if (!pdev->dev.platform_data)
  687. return -ENODEV;
  688. shdev = kzalloc(sizeof(struct sh_dmae_device), GFP_KERNEL);
  689. if (!shdev) {
  690. dev_err(&pdev->dev, "No enough memory\n");
  691. return -ENOMEM;
  692. }
  693. /* platform data */
  694. memcpy(&shdev->pdata, pdev->dev.platform_data,
  695. sizeof(struct sh_dmae_pdata));
  696. /* reset dma controller */
  697. err = sh_dmae_rst(0);
  698. if (err)
  699. goto rst_err;
  700. /* SH7780/85/23 has DMAOR1 */
  701. if (shdev->pdata.mode & SHDMA_DMAOR1) {
  702. err = sh_dmae_rst(1);
  703. if (err)
  704. goto rst_err;
  705. }
  706. INIT_LIST_HEAD(&shdev->common.channels);
  707. dma_cap_set(DMA_MEMCPY, shdev->common.cap_mask);
  708. shdev->common.device_alloc_chan_resources
  709. = sh_dmae_alloc_chan_resources;
  710. shdev->common.device_free_chan_resources = sh_dmae_free_chan_resources;
  711. shdev->common.device_prep_dma_memcpy = sh_dmae_prep_memcpy;
  712. shdev->common.device_is_tx_complete = sh_dmae_is_complete;
  713. shdev->common.device_issue_pending = sh_dmae_memcpy_issue_pending;
  714. shdev->common.dev = &pdev->dev;
  715. /* Default transfer size of 32 bytes requires 32-byte alignment */
  716. shdev->common.copy_align = 5;
  717. #if defined(CONFIG_CPU_SH4)
  718. /* Non Mix IRQ mode SH7722/SH7730 etc... */
  719. if (shdev->pdata.mode & SHDMA_MIX_IRQ) {
  720. irqflags = IRQF_SHARED;
  721. eirq[0] = DMTE0_IRQ;
  722. #if defined(DMTE6_IRQ) && defined(DMAE1_IRQ)
  723. eirq[1] = DMTE6_IRQ;
  724. #endif
  725. }
  726. for (ecnt = 0 ; ecnt < ARRAY_SIZE(eirq); ecnt++) {
  727. err = request_irq(eirq[ecnt], sh_dmae_err, irqflags,
  728. "DMAC Address Error", shdev);
  729. if (err) {
  730. dev_err(&pdev->dev, "DMA device request_irq"
  731. "error (irq %d) with return %d\n",
  732. eirq[ecnt], err);
  733. goto eirq_err;
  734. }
  735. }
  736. #endif /* CONFIG_CPU_SH4 */
  737. /* Create DMA Channel */
  738. for (cnt = 0 ; cnt < MAX_DMA_CHANNELS ; cnt++) {
  739. err = sh_dmae_chan_probe(shdev, cnt);
  740. if (err)
  741. goto chan_probe_err;
  742. }
  743. platform_set_drvdata(pdev, shdev);
  744. dma_async_device_register(&shdev->common);
  745. return err;
  746. chan_probe_err:
  747. sh_dmae_chan_remove(shdev);
  748. eirq_err:
  749. for (ecnt-- ; ecnt >= 0; ecnt--)
  750. free_irq(eirq[ecnt], shdev);
  751. rst_err:
  752. kfree(shdev);
  753. return err;
  754. }
  755. static int __exit sh_dmae_remove(struct platform_device *pdev)
  756. {
  757. struct sh_dmae_device *shdev = platform_get_drvdata(pdev);
  758. dma_async_device_unregister(&shdev->common);
  759. if (shdev->pdata.mode & SHDMA_MIX_IRQ) {
  760. free_irq(DMTE0_IRQ, shdev);
  761. #if defined(DMTE6_IRQ)
  762. free_irq(DMTE6_IRQ, shdev);
  763. #endif
  764. }
  765. /* channel data remove */
  766. sh_dmae_chan_remove(shdev);
  767. if (!(shdev->pdata.mode & SHDMA_MIX_IRQ)) {
  768. free_irq(DMAE0_IRQ, shdev);
  769. #if defined(DMAE1_IRQ)
  770. free_irq(DMAE1_IRQ, shdev);
  771. #endif
  772. }
  773. kfree(shdev);
  774. return 0;
  775. }
  776. static void sh_dmae_shutdown(struct platform_device *pdev)
  777. {
  778. struct sh_dmae_device *shdev = platform_get_drvdata(pdev);
  779. sh_dmae_ctl_stop(0);
  780. if (shdev->pdata.mode & SHDMA_DMAOR1)
  781. sh_dmae_ctl_stop(1);
  782. }
  783. static struct platform_driver sh_dmae_driver = {
  784. .remove = __exit_p(sh_dmae_remove),
  785. .shutdown = sh_dmae_shutdown,
  786. .driver = {
  787. .name = "sh-dma-engine",
  788. },
  789. };
  790. static int __init sh_dmae_init(void)
  791. {
  792. return platform_driver_probe(&sh_dmae_driver, sh_dmae_probe);
  793. }
  794. module_init(sh_dmae_init);
  795. static void __exit sh_dmae_exit(void)
  796. {
  797. platform_driver_unregister(&sh_dmae_driver);
  798. }
  799. module_exit(sh_dmae_exit);
  800. MODULE_AUTHOR("Nobuhiro Iwamatsu <iwamatsu.nobuhiro@renesas.com>");
  801. MODULE_DESCRIPTION("Renesas SH DMA Engine driver");
  802. MODULE_LICENSE("GPL");