include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / drivers / dma / shdma.c
index ab12fa5..7cc31b3 100644 (file)
 
 #include <linux/init.h>
 #include <linux/module.h>
+#include <linux/slab.h>
 #include <linux/interrupt.h>
 #include <linux/dmaengine.h>
 #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 +41,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 +86,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 +106,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 +163,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 +179,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 +291,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 +311,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 +334,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 +348,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 +369,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);
 }
@@ -556,6 +588,19 @@ static void sh_dmae_terminate_all(struct dma_chan *chan)
        if (!chan)
                return;
 
+       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);
 }
 
@@ -670,6 +715,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);
@@ -757,7 +805,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 +870,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 +891,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 +944,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 +1007,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)
@@ -983,7 +1034,7 @@ static int __init sh_dmae_probe(struct platform_device *pdev)
 
        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 +1098,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 +1113,7 @@ eirqres:
 eirq_err:
 #endif
 rst_err:
+       pm_runtime_put(&pdev->dev);
        if (dmars)
                iounmap(shdev->dmars);
 emapdmars:
@@ -1089,6 +1143,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);