DMAENGINE: Support for ST-Ericssons DMA40 block v3
[safe/jmp/linux-2.6] / drivers / dma / shdma.c
index ab12fa5..98f82cd 100644 (file)
 #include <linux/delay.h>
 #include <linux/dma-mapping.h>
 #include <linux/platform_device.h>
-#include <cpu/dma.h>
-#include <asm/dma-sh.h>
+#include <linux/pm_runtime.h>
+
+#include <asm/dmaengine.h>
+
 #include "shdma.h"
 
 /* DMA descriptor control */
@@ -38,15 +40,8 @@ enum sh_dmae_desc_status {
 };
 
 #define NR_DESCS_PER_CHANNEL 32
-/*
- * Define the default configuration for dual address memory-memory transfer.
- * The 0x400 value represents auto-request, external->external.
- *
- * And this driver set 4byte burst mode.
- * If you want to change mode, you need to change RS_DEFAULT of value.
- * (ex 1byte burst mode -> (RS_DUAL & ~TS_32)
- */
-#define RS_DEFAULT  (RS_DUAL)
+/* Default MEMCPY transfer size = 2^2 = 4 bytes */
+#define LOG2_DEFAULT_XFER_SIZE 2
 
 /* A bitmask with bits enough for enum sh_dmae_slave_chan_id */
 static unsigned long sh_dmae_slave_used[BITS_TO_LONGS(SHDMA_SLAVE_NUMBER)];
@@ -90,7 +85,7 @@ static int sh_dmae_rst(struct sh_dmae_device *shdev)
        unsigned short dmaor;
 
        sh_dmae_ctl_stop(shdev);
-       dmaor = dmaor_read(shdev) | DMAOR_INIT;
+       dmaor = dmaor_read(shdev) | shdev->pdata->dmaor_init;
 
        dmaor_write(shdev, dmaor);
        if (dmaor_read(shdev) & (DMAOR_AE | DMAOR_NMIF)) {
@@ -110,13 +105,36 @@ static bool dmae_is_busy(struct sh_dmae_chan *sh_chan)
        return false; /* waiting */
 }
 
-static unsigned int ts_shift[] = TS_SHIFT;
-static inline unsigned int calc_xmit_shift(u32 chcr)
+static unsigned int calc_xmit_shift(struct sh_dmae_chan *sh_chan, u32 chcr)
 {
-       int cnt = ((chcr & CHCR_TS_LOW_MASK) >> CHCR_TS_LOW_SHIFT) |
-               ((chcr & CHCR_TS_HIGH_MASK) >> CHCR_TS_HIGH_SHIFT);
+       struct sh_dmae_device *shdev = container_of(sh_chan->common.device,
+                                               struct sh_dmae_device, common);
+       struct sh_dmae_pdata *pdata = shdev->pdata;
+       int cnt = ((chcr & pdata->ts_low_mask) >> pdata->ts_low_shift) |
+               ((chcr & pdata->ts_high_mask) >> pdata->ts_high_shift);
+
+       if (cnt >= pdata->ts_shift_num)
+               cnt = 0;
 
-       return ts_shift[cnt];
+       return pdata->ts_shift[cnt];
+}
+
+static u32 log2size_to_chcr(struct sh_dmae_chan *sh_chan, int l2size)
+{
+       struct sh_dmae_device *shdev = container_of(sh_chan->common.device,
+                                               struct sh_dmae_device, common);
+       struct sh_dmae_pdata *pdata = shdev->pdata;
+       int i;
+
+       for (i = 0; i < pdata->ts_shift_num; i++)
+               if (pdata->ts_shift[i] == l2size)
+                       break;
+
+       if (i == pdata->ts_shift_num)
+               i = 0;
+
+       return ((i << pdata->ts_low_shift) & pdata->ts_low_mask) |
+               ((i << pdata->ts_high_shift) & pdata->ts_high_mask);
 }
 
 static void dmae_set_reg(struct sh_dmae_chan *sh_chan, struct sh_dmae_regs *hw)
@@ -144,8 +162,13 @@ static void dmae_halt(struct sh_dmae_chan *sh_chan)
 
 static void dmae_init(struct sh_dmae_chan *sh_chan)
 {
-       u32 chcr = RS_DEFAULT; /* default is DUAL mode */
-       sh_chan->xmit_shift = calc_xmit_shift(chcr);
+       /*
+        * Default configuration for dual address memory-memory transfer.
+        * 0x400 represents auto-request.
+        */
+       u32 chcr = DM_INC | SM_INC | 0x400 | log2size_to_chcr(sh_chan,
+                                                  LOG2_DEFAULT_XFER_SIZE);
+       sh_chan->xmit_shift = calc_xmit_shift(sh_chan, chcr);
        sh_dmae_writel(sh_chan, chcr, CHCR);
 }
 
@@ -155,7 +178,7 @@ static int dmae_set_chcr(struct sh_dmae_chan *sh_chan, u32 val)
        if (dmae_is_busy(sh_chan))
                return -EBUSY;
 
-       sh_chan->xmit_shift = calc_xmit_shift(val);
+       sh_chan->xmit_shift = calc_xmit_shift(sh_chan, val);
        sh_dmae_writel(sh_chan, val, CHCR);
 
        return 0;
@@ -267,6 +290,8 @@ static int sh_dmae_alloc_chan_resources(struct dma_chan *chan)
        struct sh_desc *desc;
        struct sh_dmae_slave *param = chan->private;
 
+       pm_runtime_get_sync(sh_chan->dev);
+
        /*
         * This relies on the guarantee from dmaengine that alloc_chan_resources
         * never runs concurrently with itself or free_chan_resources.
@@ -285,9 +310,8 @@ static int sh_dmae_alloc_chan_resources(struct dma_chan *chan)
 
                dmae_set_dmars(sh_chan, cfg->mid_rid);
                dmae_set_chcr(sh_chan, cfg->chcr);
-       } else {
-               if ((sh_dmae_readl(sh_chan, CHCR) & 0x700) != 0x400)
-                       dmae_set_chcr(sh_chan, RS_DEFAULT);
+       } else if ((sh_dmae_readl(sh_chan, CHCR) & 0xf00) != 0x400) {
+               dmae_init(sh_chan);
        }
 
        spin_lock_bh(&sh_chan->desc_lock);
@@ -309,6 +333,9 @@ static int sh_dmae_alloc_chan_resources(struct dma_chan *chan)
        }
        spin_unlock_bh(&sh_chan->desc_lock);
 
+       if (!sh_chan->descs_allocated)
+               pm_runtime_put(sh_chan->dev);
+
        return sh_chan->descs_allocated;
 }
 
@@ -320,6 +347,7 @@ static void sh_dmae_free_chan_resources(struct dma_chan *chan)
        struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
        struct sh_desc *desc, *_desc;
        LIST_HEAD(list);
+       int descs = sh_chan->descs_allocated;
 
        dmae_halt(sh_chan);
 
@@ -340,6 +368,9 @@ static void sh_dmae_free_chan_resources(struct dma_chan *chan)
 
        spin_unlock_bh(&sh_chan->desc_lock);
 
+       if (descs > 0)
+               pm_runtime_put(sh_chan->dev);
+
        list_for_each_entry_safe(desc, _desc, &list, node)
                kfree(desc);
 }
@@ -549,14 +580,33 @@ static struct dma_async_tx_descriptor *sh_dmae_prep_slave_sg(
                               direction, flags);
 }
 
-static void sh_dmae_terminate_all(struct dma_chan *chan)
+static int sh_dmae_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd)
 {
        struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
 
+       /* Only supports DMA_TERMINATE_ALL */
+       if (cmd != DMA_TERMINATE_ALL)
+               return -ENXIO;
+
        if (!chan)
-               return;
+               return -EINVAL;
+
+       dmae_halt(sh_chan);
+
+       spin_lock_bh(&sh_chan->desc_lock);
+       if (!list_empty(&sh_chan->ld_queue)) {
+               /* Record partial transfer */
+               struct sh_desc *desc = list_entry(sh_chan->ld_queue.next,
+                                                 struct sh_desc, node);
+               desc->partial = (desc->hw.tcr - sh_dmae_readl(sh_chan, TCR)) <<
+                       sh_chan->xmit_shift;
+
+       }
+       spin_unlock_bh(&sh_chan->desc_lock);
 
        sh_dmae_chan_ld_cleanup(sh_chan, true);
+
+       return 0;
 }
 
 static dma_async_tx_callback __ld_cleanup(struct sh_dmae_chan *sh_chan, bool all)
@@ -670,6 +720,9 @@ static void sh_chan_xfer_ld_queue(struct sh_dmae_chan *sh_chan)
        /* Find the first not transferred desciptor */
        list_for_each_entry(desc, &sh_chan->ld_queue, node)
                if (desc->mark == DESC_SUBMITTED) {
+                       dev_dbg(sh_chan->dev, "Queue #%d to %d: %u@%x -> %x\n",
+                               desc->async_tx.cookie, sh_chan->id,
+                               desc->hw.tcr, desc->hw.sar, desc->hw.dar);
                        /* Get the ld start address from ld_queue */
                        dmae_set_reg(sh_chan, &desc->hw);
                        dmae_start(sh_chan);
@@ -685,10 +738,9 @@ static void sh_dmae_memcpy_issue_pending(struct dma_chan *chan)
        sh_chan_xfer_ld_queue(sh_chan);
 }
 
-static enum dma_status sh_dmae_is_complete(struct dma_chan *chan,
+static enum dma_status sh_dmae_tx_status(struct dma_chan *chan,
                                        dma_cookie_t cookie,
-                                       dma_cookie_t *done,
-                                       dma_cookie_t *used)
+                                       struct dma_tx_state *txstate)
 {
        struct sh_dmae_chan *sh_chan = to_sh_chan(chan);
        dma_cookie_t last_used;
@@ -700,12 +752,7 @@ static enum dma_status sh_dmae_is_complete(struct dma_chan *chan,
        last_used = chan->cookie;
        last_complete = sh_chan->completed_cookie;
        BUG_ON(last_complete < 0);
-
-       if (done)
-               *done = last_complete;
-
-       if (used)
-               *used = last_used;
+       dma_set_tx_state(txstate, last_complete, last_used, 0);
 
        spin_lock_bh(&sh_chan->desc_lock);
 
@@ -757,7 +804,7 @@ static irqreturn_t sh_dmae_err(int irq, void *data)
        sh_dmae_ctl_stop(shdev);
 
        /* We cannot detect, which channel caused the error, have to reset all */
-       for (i = 0; i < MAX_DMA_CHANNELS; i++) {
+       for (i = 0; i < SH_DMAC_MAX_CHANNELS; i++) {
                struct sh_dmae_chan *sh_chan = shdev->chan[i];
                if (sh_chan) {
                        struct sh_desc *desc;
@@ -822,6 +869,9 @@ static int __devinit sh_dmae_chan_probe(struct sh_dmae_device *shdev, int id,
                return -ENOMEM;
        }
 
+       /* copy struct dma_device */
+       new_sh_chan->common.device = &shdev->common;
+
        new_sh_chan->dev = shdev->common.dev;
        new_sh_chan->id = id;
        new_sh_chan->irq = irq;
@@ -840,9 +890,6 @@ static int __devinit sh_dmae_chan_probe(struct sh_dmae_device *shdev, int id,
        INIT_LIST_HEAD(&new_sh_chan->ld_queue);
        INIT_LIST_HEAD(&new_sh_chan->ld_free);
 
-       /* copy struct dma_device */
-       new_sh_chan->common.device = &shdev->common;
-
        /* Add the channel to DMA device channel list */
        list_add_tail(&new_sh_chan->common.device_node,
                        &shdev->common.channels);
@@ -896,8 +943,8 @@ static int __init sh_dmae_probe(struct platform_device *pdev)
 {
        struct sh_dmae_pdata *pdata = pdev->dev.platform_data;
        unsigned long irqflags = IRQF_DISABLED,
-               chan_flag[MAX_DMA_CHANNELS] = {};
-       int errirq, chan_irq[MAX_DMA_CHANNELS];
+               chan_flag[SH_DMAC_MAX_CHANNELS] = {};
+       int errirq, chan_irq[SH_DMAC_MAX_CHANNELS];
        int err, i, irq_cnt = 0, irqres = 0;
        struct sh_dmae_device *shdev;
        struct resource *chan, *dmars, *errirq_res, *chanirq_res;
@@ -959,6 +1006,9 @@ static int __init sh_dmae_probe(struct platform_device *pdev)
        /* platform data */
        shdev->pdata = pdata;
 
+       pm_runtime_enable(&pdev->dev);
+       pm_runtime_get_sync(&pdev->dev);
+
        /* reset dma controller */
        err = sh_dmae_rst(shdev);
        if (err)
@@ -974,16 +1024,16 @@ static int __init sh_dmae_probe(struct platform_device *pdev)
                = sh_dmae_alloc_chan_resources;
        shdev->common.device_free_chan_resources = sh_dmae_free_chan_resources;
        shdev->common.device_prep_dma_memcpy = sh_dmae_prep_memcpy;
-       shdev->common.device_is_tx_complete = sh_dmae_is_complete;
+       shdev->common.device_tx_status = sh_dmae_tx_status;
        shdev->common.device_issue_pending = sh_dmae_memcpy_issue_pending;
 
        /* Compulsory for DMA_SLAVE fields */
        shdev->common.device_prep_slave_sg = sh_dmae_prep_slave_sg;
-       shdev->common.device_terminate_all = sh_dmae_terminate_all;
+       shdev->common.device_control = sh_dmae_control;
 
        shdev->common.dev = &pdev->dev;
        /* Default transfer size of 32 bytes requires 32-byte alignment */
-       shdev->common.copy_align = 5;
+       shdev->common.copy_align = LOG2_DEFAULT_XFER_SIZE;
 
 #if defined(CONFIG_CPU_SH4)
        chanirq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
@@ -1047,6 +1097,8 @@ static int __init sh_dmae_probe(struct platform_device *pdev)
                        goto chan_probe_err;
        }
 
+       pm_runtime_put(&pdev->dev);
+
        platform_set_drvdata(pdev, shdev);
        dma_async_device_register(&shdev->common);
 
@@ -1060,6 +1112,7 @@ eirqres:
 eirq_err:
 #endif
 rst_err:
+       pm_runtime_put(&pdev->dev);
        if (dmars)
                iounmap(shdev->dmars);
 emapdmars:
@@ -1089,6 +1142,8 @@ static int __exit sh_dmae_remove(struct platform_device *pdev)
        /* channel data remove */
        sh_dmae_chan_remove(shdev);
 
+       pm_runtime_disable(&pdev->dev);
+
        if (shdev->dmars)
                iounmap(shdev->dmars);
        iounmap(shdev->chan_reg);