2 * Renesas SuperH DMA Engine support
4 * base is drivers/dma/flsdma.c
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.
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.
15 * - DMA of SuperH does not have Hardware DMA chain mode.
16 * - MAX DMA size is 16MB.
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/interrupt.h>
23 #include <linux/dmaengine.h>
24 #include <linux/delay.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/platform_device.h>
28 #include <asm/dma-sh.h>
31 /* DMA descriptor control */
32 enum sh_dmae_desc_status {
36 DESC_COMPLETED, /* completed, have to call callback */
37 DESC_WAITING, /* callback called, waiting for ack / re-submit */
40 #define NR_DESCS_PER_CHANNEL 32
42 * Define the default configuration for dual address memory-memory transfer.
43 * The 0x400 value represents auto-request, external->external.
45 * And this driver set 4byte burst mode.
46 * If you want to change mode, you need to change RS_DEFAULT of value.
47 * (ex 1byte burst mode -> (RS_DUAL & ~TS_32)
49 #define RS_DEFAULT (RS_DUAL)
51 static void sh_dmae_chan_ld_cleanup(struct sh_dmae_chan *sh_chan, bool all);
53 #define SH_DMAC_CHAN_BASE(id) (dma_base_addr[id])
54 static void sh_dmae_writel(struct sh_dmae_chan *sh_dc, u32 data, u32 reg)
56 ctrl_outl(data, SH_DMAC_CHAN_BASE(sh_dc->id) + reg);
59 static u32 sh_dmae_readl(struct sh_dmae_chan *sh_dc, u32 reg)
61 return ctrl_inl(SH_DMAC_CHAN_BASE(sh_dc->id) + reg);
64 static void dmae_init(struct sh_dmae_chan *sh_chan)
66 u32 chcr = RS_DEFAULT; /* default is DUAL mode */
67 sh_dmae_writel(sh_chan, chcr, CHCR);
71 * Reset DMA controller
73 * SH7780 has two DMAOR register
75 static void sh_dmae_ctl_stop(int id)
77 unsigned short dmaor = dmaor_read_reg(id);
79 dmaor &= ~(DMAOR_NMIF | DMAOR_AE);
80 dmaor_write_reg(id, dmaor);
83 static int sh_dmae_rst(int id)
88 dmaor = dmaor_read_reg(id) | DMAOR_INIT;
90 dmaor_write_reg(id, dmaor);
91 if (dmaor_read_reg(id) & (DMAOR_AE | DMAOR_NMIF)) {
92 pr_warning(KERN_ERR "dma-sh: Can't initialize DMAOR.\n");
98 static bool dmae_is_busy(struct sh_dmae_chan *sh_chan)
100 u32 chcr = sh_dmae_readl(sh_chan, CHCR);
102 if ((chcr & (CHCR_DE | CHCR_TE)) == CHCR_DE)
103 return true; /* working */
105 return false; /* waiting */
108 static unsigned int ts_shift[] = TS_SHIFT;
109 static inline unsigned int calc_xmit_shift(struct sh_dmae_chan *sh_chan)
111 u32 chcr = sh_dmae_readl(sh_chan, CHCR);
112 int cnt = ((chcr & CHCR_TS_LOW_MASK) >> CHCR_TS_LOW_SHIFT) |
113 ((chcr & CHCR_TS_HIGH_MASK) >> CHCR_TS_HIGH_SHIFT);
115 return ts_shift[cnt];
118 static void dmae_set_reg(struct sh_dmae_chan *sh_chan, struct sh_dmae_regs *hw)
120 sh_dmae_writel(sh_chan, hw->sar, SAR);
121 sh_dmae_writel(sh_chan, hw->dar, DAR);
122 sh_dmae_writel(sh_chan, hw->tcr >> calc_xmit_shift(sh_chan), TCR);
125 static void dmae_start(struct sh_dmae_chan *sh_chan)
127 u32 chcr = sh_dmae_readl(sh_chan, CHCR);
129 chcr |= CHCR_DE | CHCR_IE;
130 sh_dmae_writel(sh_chan, chcr, CHCR);
133 static void dmae_halt(struct sh_dmae_chan *sh_chan)
135 u32 chcr = sh_dmae_readl(sh_chan, CHCR);
137 chcr &= ~(CHCR_DE | CHCR_TE | CHCR_IE);
138 sh_dmae_writel(sh_chan, chcr, CHCR);
141 static int dmae_set_chcr(struct sh_dmae_chan *sh_chan, u32 val)
143 /* When DMA was working, can not set data to CHCR */
144 if (dmae_is_busy(sh_chan))
147 sh_dmae_writel(sh_chan, val, CHCR);
151 #define DMARS1_ADDR 0x04
152 #define DMARS2_ADDR 0x08
153 #define DMARS_SHIFT 8
154 #define DMARS_CHAN_MSK 0x01
155 static int dmae_set_dmars(struct sh_dmae_chan *sh_chan, u16 val)
160 if (dmae_is_busy(sh_chan))
163 if (sh_chan->id & DMARS_CHAN_MSK)
166 switch (sh_chan->id) {
170 addr = SH_DMARS_BASE;
175 addr = (SH_DMARS_BASE + DMARS1_ADDR);
180 addr = (SH_DMARS_BASE + DMARS2_ADDR);
186 ctrl_outw((val << shift) |
187 (ctrl_inw(addr) & (shift ? 0xFF00 : 0x00FF)),
193 static dma_cookie_t sh_dmae_tx_submit(struct dma_async_tx_descriptor *tx)
195 struct sh_desc *desc = tx_to_sh_desc(tx), *chunk, *last = desc, *c;
196 struct sh_dmae_chan *sh_chan = to_sh_chan(tx->chan);
197 dma_async_tx_callback callback = tx->callback;
200 spin_lock_bh(&sh_chan->desc_lock);
202 cookie = sh_chan->common.cookie;
207 sh_chan->common.cookie = cookie;
210 /* Mark all chunks of this descriptor as submitted, move to the queue */
211 list_for_each_entry_safe(chunk, c, desc->node.prev, node) {
213 * All chunks are on the global ld_free, so, we have to find
214 * the end of the chain ourselves
216 if (chunk != desc && (chunk->mark == DESC_IDLE ||
217 chunk->async_tx.cookie > 0 ||
218 chunk->async_tx.cookie == -EBUSY ||
219 &chunk->node == &sh_chan->ld_free))
221 chunk->mark = DESC_SUBMITTED;
222 /* Callback goes to the last chunk */
223 chunk->async_tx.callback = NULL;
224 chunk->cookie = cookie;
225 list_move_tail(&chunk->node, &sh_chan->ld_queue);
229 last->async_tx.callback = callback;
230 last->async_tx.callback_param = tx->callback_param;
232 dev_dbg(sh_chan->dev, "submit #%d@%p on %d: %x[%d] -> %x\n",
233 tx->cookie, &last->async_tx, sh_chan->id,
234 desc->hw.sar, desc->hw.tcr, desc->hw.dar);
236 spin_unlock_bh(&sh_chan->desc_lock);
241 /* Called with desc_lock held */
242 static struct sh_desc *sh_dmae_get_desc(struct sh_dmae_chan *sh_chan)
244 struct sh_desc *desc;
246 list_for_each_entry(desc, &sh_chan->ld_free, node)
247 if (desc->mark != DESC_PREPARED) {
248 BUG_ON(desc->mark != DESC_IDLE);
249 list_del(&desc->node);
256 static int sh_dmae_alloc_chan_resources(struct dma_chan *chan)
258 struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
259 struct sh_desc *desc;
261 spin_lock_bh(&sh_chan->desc_lock);
262 while (sh_chan->descs_allocated < NR_DESCS_PER_CHANNEL) {
263 spin_unlock_bh(&sh_chan->desc_lock);
264 desc = kzalloc(sizeof(struct sh_desc), GFP_KERNEL);
266 spin_lock_bh(&sh_chan->desc_lock);
269 dma_async_tx_descriptor_init(&desc->async_tx,
271 desc->async_tx.tx_submit = sh_dmae_tx_submit;
272 desc->mark = DESC_IDLE;
274 spin_lock_bh(&sh_chan->desc_lock);
275 list_add(&desc->node, &sh_chan->ld_free);
276 sh_chan->descs_allocated++;
278 spin_unlock_bh(&sh_chan->desc_lock);
280 return sh_chan->descs_allocated;
284 * sh_dma_free_chan_resources - Free all resources of the channel.
286 static void sh_dmae_free_chan_resources(struct dma_chan *chan)
288 struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
289 struct sh_desc *desc, *_desc;
292 /* Prepared and not submitted descriptors can still be on the queue */
293 if (!list_empty(&sh_chan->ld_queue))
294 sh_dmae_chan_ld_cleanup(sh_chan, true);
296 spin_lock_bh(&sh_chan->desc_lock);
298 list_splice_init(&sh_chan->ld_free, &list);
299 sh_chan->descs_allocated = 0;
301 spin_unlock_bh(&sh_chan->desc_lock);
303 list_for_each_entry_safe(desc, _desc, &list, node)
308 * sh_dmae_add_desc - get, set up and return one transfer descriptor
309 * @sh_chan: DMA channel
310 * @flags: DMA transfer flags
311 * @dest: destination DMA address, incremented when direction equals
312 * DMA_FROM_DEVICE or DMA_BIDIRECTIONAL
313 * @src: source DMA address, incremented when direction equals
314 * DMA_TO_DEVICE or DMA_BIDIRECTIONAL
315 * @len: DMA transfer length
316 * @first: if NULL, set to the current descriptor and cookie set to -EBUSY
317 * @direction: needed for slave DMA to decide which address to keep constant,
318 * equals DMA_BIDIRECTIONAL for MEMCPY
319 * Returns 0 or an error
320 * Locks: called with desc_lock held
322 static struct sh_desc *sh_dmae_add_desc(struct sh_dmae_chan *sh_chan,
323 unsigned long flags, dma_addr_t *dest, dma_addr_t *src, size_t *len,
324 struct sh_desc **first, enum dma_data_direction direction)
332 /* Allocate the link descriptor from the free list */
333 new = sh_dmae_get_desc(sh_chan);
335 dev_err(sh_chan->dev, "No free link descriptor available\n");
339 copy_size = min(*len, (size_t)SH_DMA_TCR_MAX + 1);
343 new->hw.tcr = copy_size;
347 new->async_tx.cookie = -EBUSY;
350 /* Other desc - invisible to the user */
351 new->async_tx.cookie = -EINVAL;
354 dev_dbg(sh_chan->dev, "chaining (%u/%u)@%x -> %x with %p, cookie %d\n",
355 copy_size, *len, *src, *dest, &new->async_tx,
356 new->async_tx.cookie);
358 new->mark = DESC_PREPARED;
359 new->async_tx.flags = flags;
362 if (direction == DMA_BIDIRECTIONAL || direction == DMA_TO_DEVICE)
364 if (direction == DMA_BIDIRECTIONAL || direction == DMA_FROM_DEVICE)
371 * sh_dmae_prep_sg - prepare transfer descriptors from an SG list
373 * Common routine for public (MEMCPY) and slave DMA. The MEMCPY case is also
374 * converted to scatter-gather to guarantee consistent locking and a correct
375 * list manipulation. For slave DMA direction carries the usual meaning, and,
376 * logically, the SG list is RAM and the addr variable contains slave address,
377 * e.g., the FIFO I/O register. For MEMCPY direction equals DMA_BIDIRECTIONAL
378 * and the SG list contains only one element and points at the source buffer.
380 static struct dma_async_tx_descriptor *sh_dmae_prep_sg(struct sh_dmae_chan *sh_chan,
381 struct scatterlist *sgl, unsigned int sg_len, dma_addr_t *addr,
382 enum dma_data_direction direction, unsigned long flags)
384 struct scatterlist *sg;
385 struct sh_desc *first = NULL, *new = NULL /* compiler... */;
393 for_each_sg(sgl, sg, sg_len, i)
394 chunks += (sg_dma_len(sg) + SH_DMA_TCR_MAX) /
395 (SH_DMA_TCR_MAX + 1);
397 /* Have to lock the whole loop to protect against concurrent release */
398 spin_lock_bh(&sh_chan->desc_lock);
402 * first descriptor is what user is dealing with in all API calls, its
403 * cookie is at first set to -EBUSY, at tx-submit to a positive
405 * if more than one chunk is needed further chunks have cookie = -EINVAL
406 * the last chunk, if not equal to the first, has cookie = -ENOSPC
407 * all chunks are linked onto the tx_list head with their .node heads
408 * only during this function, then they are immediately spliced
409 * back onto the free list in form of a chain
411 for_each_sg(sgl, sg, sg_len, i) {
412 dma_addr_t sg_addr = sg_dma_address(sg);
413 size_t len = sg_dma_len(sg);
419 dev_dbg(sh_chan->dev, "Add SG #%d@%p[%d], dma %llx\n",
420 i, sg, len, (unsigned long long)sg_addr);
422 if (direction == DMA_FROM_DEVICE)
423 new = sh_dmae_add_desc(sh_chan, flags,
424 &sg_addr, addr, &len, &first,
427 new = sh_dmae_add_desc(sh_chan, flags,
428 addr, &sg_addr, &len, &first,
433 new->chunks = chunks--;
434 list_add_tail(&new->node, &tx_list);
439 new->async_tx.cookie = -ENOSPC;
441 /* Put them back on the free list, so, they don't get lost */
442 list_splice_tail(&tx_list, &sh_chan->ld_free);
444 spin_unlock_bh(&sh_chan->desc_lock);
446 return &first->async_tx;
449 list_for_each_entry(new, &tx_list, node)
450 new->mark = DESC_IDLE;
451 list_splice(&tx_list, &sh_chan->ld_free);
453 spin_unlock_bh(&sh_chan->desc_lock);
458 static struct dma_async_tx_descriptor *sh_dmae_prep_memcpy(
459 struct dma_chan *chan, dma_addr_t dma_dest, dma_addr_t dma_src,
460 size_t len, unsigned long flags)
462 struct sh_dmae_chan *sh_chan;
463 struct scatterlist sg;
468 sh_chan = to_sh_chan(chan);
470 sg_init_table(&sg, 1);
471 sg_set_page(&sg, pfn_to_page(PFN_DOWN(dma_src)), len,
472 offset_in_page(dma_src));
473 sg_dma_address(&sg) = dma_src;
474 sg_dma_len(&sg) = len;
476 return sh_dmae_prep_sg(sh_chan, &sg, 1, &dma_dest, DMA_BIDIRECTIONAL,
480 static dma_async_tx_callback __ld_cleanup(struct sh_dmae_chan *sh_chan, bool all)
482 struct sh_desc *desc, *_desc;
483 /* Is the "exposed" head of a chain acked? */
484 bool head_acked = false;
485 dma_cookie_t cookie = 0;
486 dma_async_tx_callback callback = NULL;
489 spin_lock_bh(&sh_chan->desc_lock);
490 list_for_each_entry_safe(desc, _desc, &sh_chan->ld_queue, node) {
491 struct dma_async_tx_descriptor *tx = &desc->async_tx;
493 BUG_ON(tx->cookie > 0 && tx->cookie != desc->cookie);
494 BUG_ON(desc->mark != DESC_SUBMITTED &&
495 desc->mark != DESC_COMPLETED &&
496 desc->mark != DESC_WAITING);
499 * queue is ordered, and we use this loop to (1) clean up all
500 * completed descriptors, and to (2) update descriptor flags of
501 * any chunks in a (partially) completed chain
503 if (!all && desc->mark == DESC_SUBMITTED &&
504 desc->cookie != cookie)
510 if (desc->mark == DESC_COMPLETED && desc->chunks == 1) {
511 BUG_ON(sh_chan->completed_cookie != desc->cookie - 1);
512 sh_chan->completed_cookie = desc->cookie;
515 /* Call callback on the last chunk */
516 if (desc->mark == DESC_COMPLETED && tx->callback) {
517 desc->mark = DESC_WAITING;
518 callback = tx->callback;
519 param = tx->callback_param;
520 dev_dbg(sh_chan->dev, "descriptor #%d@%p on %d callback\n",
521 tx->cookie, tx, sh_chan->id);
522 BUG_ON(desc->chunks != 1);
526 if (tx->cookie > 0 || tx->cookie == -EBUSY) {
527 if (desc->mark == DESC_COMPLETED) {
528 BUG_ON(tx->cookie < 0);
529 desc->mark = DESC_WAITING;
531 head_acked = async_tx_test_ack(tx);
533 switch (desc->mark) {
535 desc->mark = DESC_WAITING;
539 async_tx_ack(&desc->async_tx);
543 dev_dbg(sh_chan->dev, "descriptor %p #%d completed.\n",
546 if (((desc->mark == DESC_COMPLETED ||
547 desc->mark == DESC_WAITING) &&
548 async_tx_test_ack(&desc->async_tx)) || all) {
549 /* Remove from ld_queue list */
550 desc->mark = DESC_IDLE;
551 list_move(&desc->node, &sh_chan->ld_free);
554 spin_unlock_bh(&sh_chan->desc_lock);
563 * sh_chan_ld_cleanup - Clean up link descriptors
565 * This function cleans up the ld_queue of DMA channel.
567 static void sh_dmae_chan_ld_cleanup(struct sh_dmae_chan *sh_chan, bool all)
569 while (__ld_cleanup(sh_chan, all))
573 static void sh_chan_xfer_ld_queue(struct sh_dmae_chan *sh_chan)
577 spin_lock_bh(&sh_chan->desc_lock);
579 if (dmae_is_busy(sh_chan)) {
580 spin_unlock_bh(&sh_chan->desc_lock);
584 /* Find the first un-transfer desciptor */
585 list_for_each_entry(sd, &sh_chan->ld_queue, node)
586 if (sd->mark == DESC_SUBMITTED) {
587 /* Get the ld start address from ld_queue */
588 dmae_set_reg(sh_chan, &sd->hw);
593 spin_unlock_bh(&sh_chan->desc_lock);
596 static void sh_dmae_memcpy_issue_pending(struct dma_chan *chan)
598 struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
599 sh_chan_xfer_ld_queue(sh_chan);
602 static enum dma_status sh_dmae_is_complete(struct dma_chan *chan,
607 struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
608 dma_cookie_t last_used;
609 dma_cookie_t last_complete;
611 sh_dmae_chan_ld_cleanup(sh_chan, false);
613 last_used = chan->cookie;
614 last_complete = sh_chan->completed_cookie;
615 BUG_ON(last_complete < 0);
618 *done = last_complete;
623 return dma_async_is_complete(cookie, last_complete, last_used);
626 static irqreturn_t sh_dmae_interrupt(int irq, void *data)
628 irqreturn_t ret = IRQ_NONE;
629 struct sh_dmae_chan *sh_chan = (struct sh_dmae_chan *)data;
630 u32 chcr = sh_dmae_readl(sh_chan, CHCR);
632 if (chcr & CHCR_TE) {
637 tasklet_schedule(&sh_chan->tasklet);
643 #if defined(CONFIG_CPU_SH4)
644 static irqreturn_t sh_dmae_err(int irq, void *data)
647 struct sh_dmae_device *shdev = (struct sh_dmae_device *)data;
650 if (shdev->pdata.mode & SHDMA_MIX_IRQ) {
651 int __maybe_unused cnt = 0;
653 #if defined(DMTE6_IRQ) && defined(DMAE1_IRQ)
658 if (dmaor_read_reg(cnt) & (DMAOR_NMIF | DMAOR_AE)) {
666 /* reset dma controller */
667 err = sh_dmae_rst(0);
671 if (shdev->pdata.mode & SHDMA_DMAOR1) {
672 err = sh_dmae_rst(1);
683 static void dmae_do_tasklet(unsigned long data)
685 struct sh_dmae_chan *sh_chan = (struct sh_dmae_chan *)data;
686 struct sh_desc *desc;
687 u32 sar_buf = sh_dmae_readl(sh_chan, SAR);
689 spin_lock(&sh_chan->desc_lock);
690 list_for_each_entry(desc, &sh_chan->ld_queue, node) {
691 if ((desc->hw.sar + desc->hw.tcr) == sar_buf &&
692 desc->mark == DESC_SUBMITTED) {
693 dev_dbg(sh_chan->dev, "done #%d@%p dst %u\n",
694 desc->async_tx.cookie, &desc->async_tx,
696 desc->mark = DESC_COMPLETED;
700 spin_unlock(&sh_chan->desc_lock);
703 sh_chan_xfer_ld_queue(sh_chan);
704 sh_dmae_chan_ld_cleanup(sh_chan, false);
707 static unsigned int get_dmae_irq(unsigned int id)
709 unsigned int irq = 0;
710 if (id < ARRAY_SIZE(dmte_irq_map))
711 irq = dmte_irq_map[id];
715 static int __devinit sh_dmae_chan_probe(struct sh_dmae_device *shdev, int id)
718 unsigned int irq = get_dmae_irq(id);
719 unsigned long irqflags = IRQF_DISABLED;
720 struct sh_dmae_chan *new_sh_chan;
723 new_sh_chan = kzalloc(sizeof(struct sh_dmae_chan), GFP_KERNEL);
725 dev_err(shdev->common.dev,
726 "No free memory for allocating dma channels!\n");
730 new_sh_chan->dev = shdev->common.dev;
731 new_sh_chan->id = id;
733 /* Init DMA tasklet */
734 tasklet_init(&new_sh_chan->tasklet, dmae_do_tasklet,
735 (unsigned long)new_sh_chan);
737 /* Init the channel */
738 dmae_init(new_sh_chan);
740 spin_lock_init(&new_sh_chan->desc_lock);
742 /* Init descripter manage list */
743 INIT_LIST_HEAD(&new_sh_chan->ld_queue);
744 INIT_LIST_HEAD(&new_sh_chan->ld_free);
746 /* copy struct dma_device */
747 new_sh_chan->common.device = &shdev->common;
749 /* Add the channel to DMA device channel list */
750 list_add_tail(&new_sh_chan->common.device_node,
751 &shdev->common.channels);
752 shdev->common.chancnt++;
754 if (shdev->pdata.mode & SHDMA_MIX_IRQ) {
755 irqflags = IRQF_SHARED;
756 #if defined(DMTE6_IRQ)
757 if (irq >= DMTE6_IRQ)
764 snprintf(new_sh_chan->dev_id, sizeof(new_sh_chan->dev_id),
765 "sh-dmae%d", new_sh_chan->id);
767 /* set up channel irq */
768 err = request_irq(irq, &sh_dmae_interrupt, irqflags,
769 new_sh_chan->dev_id, new_sh_chan);
771 dev_err(shdev->common.dev, "DMA channel %d request_irq error "
772 "with return %d\n", id, err);
776 /* CHCR register control function */
777 new_sh_chan->set_chcr = dmae_set_chcr;
778 /* DMARS register control function */
779 new_sh_chan->set_dmars = dmae_set_dmars;
781 shdev->chan[id] = new_sh_chan;
785 /* remove from dmaengine device node */
786 list_del(&new_sh_chan->common.device_node);
791 static void sh_dmae_chan_remove(struct sh_dmae_device *shdev)
795 for (i = shdev->common.chancnt - 1 ; i >= 0 ; i--) {
796 if (shdev->chan[i]) {
797 struct sh_dmae_chan *shchan = shdev->chan[i];
798 if (!(shdev->pdata.mode & SHDMA_MIX_IRQ))
799 free_irq(dmte_irq_map[i], shchan);
801 list_del(&shchan->common.device_node);
803 shdev->chan[i] = NULL;
806 shdev->common.chancnt = 0;
809 static int __init sh_dmae_probe(struct platform_device *pdev)
811 int err = 0, cnt, ecnt;
812 unsigned long irqflags = IRQF_DISABLED;
813 #if defined(CONFIG_CPU_SH4)
814 int eirq[] = { DMAE0_IRQ,
815 #if defined(DMAE1_IRQ)
820 struct sh_dmae_device *shdev;
822 /* get platform data */
823 if (!pdev->dev.platform_data)
826 shdev = kzalloc(sizeof(struct sh_dmae_device), GFP_KERNEL);
828 dev_err(&pdev->dev, "No enough memory\n");
833 memcpy(&shdev->pdata, pdev->dev.platform_data,
834 sizeof(struct sh_dmae_pdata));
836 /* reset dma controller */
837 err = sh_dmae_rst(0);
841 /* SH7780/85/23 has DMAOR1 */
842 if (shdev->pdata.mode & SHDMA_DMAOR1) {
843 err = sh_dmae_rst(1);
848 INIT_LIST_HEAD(&shdev->common.channels);
850 dma_cap_set(DMA_MEMCPY, shdev->common.cap_mask);
851 shdev->common.device_alloc_chan_resources
852 = sh_dmae_alloc_chan_resources;
853 shdev->common.device_free_chan_resources = sh_dmae_free_chan_resources;
854 shdev->common.device_prep_dma_memcpy = sh_dmae_prep_memcpy;
855 shdev->common.device_is_tx_complete = sh_dmae_is_complete;
856 shdev->common.device_issue_pending = sh_dmae_memcpy_issue_pending;
857 shdev->common.dev = &pdev->dev;
858 /* Default transfer size of 32 bytes requires 32-byte alignment */
859 shdev->common.copy_align = 5;
861 #if defined(CONFIG_CPU_SH4)
862 /* Non Mix IRQ mode SH7722/SH7730 etc... */
863 if (shdev->pdata.mode & SHDMA_MIX_IRQ) {
864 irqflags = IRQF_SHARED;
866 #if defined(DMTE6_IRQ) && defined(DMAE1_IRQ)
871 for (ecnt = 0 ; ecnt < ARRAY_SIZE(eirq); ecnt++) {
872 err = request_irq(eirq[ecnt], sh_dmae_err, irqflags,
873 "DMAC Address Error", shdev);
875 dev_err(&pdev->dev, "DMA device request_irq"
876 "error (irq %d) with return %d\n",
881 #endif /* CONFIG_CPU_SH4 */
883 /* Create DMA Channel */
884 for (cnt = 0 ; cnt < MAX_DMA_CHANNELS ; cnt++) {
885 err = sh_dmae_chan_probe(shdev, cnt);
890 platform_set_drvdata(pdev, shdev);
891 dma_async_device_register(&shdev->common);
896 sh_dmae_chan_remove(shdev);
899 for (ecnt-- ; ecnt >= 0; ecnt--)
900 free_irq(eirq[ecnt], shdev);
908 static int __exit sh_dmae_remove(struct platform_device *pdev)
910 struct sh_dmae_device *shdev = platform_get_drvdata(pdev);
912 dma_async_device_unregister(&shdev->common);
914 if (shdev->pdata.mode & SHDMA_MIX_IRQ) {
915 free_irq(DMTE0_IRQ, shdev);
916 #if defined(DMTE6_IRQ)
917 free_irq(DMTE6_IRQ, shdev);
921 /* channel data remove */
922 sh_dmae_chan_remove(shdev);
924 if (!(shdev->pdata.mode & SHDMA_MIX_IRQ)) {
925 free_irq(DMAE0_IRQ, shdev);
926 #if defined(DMAE1_IRQ)
927 free_irq(DMAE1_IRQ, shdev);
935 static void sh_dmae_shutdown(struct platform_device *pdev)
937 struct sh_dmae_device *shdev = platform_get_drvdata(pdev);
939 if (shdev->pdata.mode & SHDMA_DMAOR1)
943 static struct platform_driver sh_dmae_driver = {
944 .remove = __exit_p(sh_dmae_remove),
945 .shutdown = sh_dmae_shutdown,
947 .name = "sh-dma-engine",
951 static int __init sh_dmae_init(void)
953 return platform_driver_probe(&sh_dmae_driver, sh_dmae_probe);
955 module_init(sh_dmae_init);
957 static void __exit sh_dmae_exit(void)
959 platform_driver_unregister(&sh_dmae_driver);
961 module_exit(sh_dmae_exit);
963 MODULE_AUTHOR("Nobuhiro Iwamatsu <iwamatsu.nobuhiro@renesas.com>");
964 MODULE_DESCRIPTION("Renesas SH DMA Engine driver");
965 MODULE_LICENSE("GPL");