drivers/dma: Add export.h to ste_dma40.c
[linux-2.6.git] / drivers / dma / ste_dma40.c
index 3d4cea3..13259ca 100644 (file)
@@ -6,13 +6,16 @@
  * License terms: GNU General Public License (GPL) version 2
  */
 
+#include <linux/dma-mapping.h>
 #include <linux/kernel.h>
 #include <linux/slab.h>
+#include <linux/export.h>
 #include <linux/dmaengine.h>
 #include <linux/platform_device.h>
 #include <linux/clk.h>
 #include <linux/delay.h>
 #include <linux/err.h>
+#include <linux/amba/bus.h>
 
 #include <plat/ste_dma40.h>
 
@@ -44,9 +47,6 @@
 #define D40_ALLOC_PHY          (1 << 30)
 #define D40_ALLOC_LOG_FREE     0
 
-/* Hardware designer of the block */
-#define D40_HW_DESIGNER 0x8
-
 /**
  * enum 40_command - The different commands and/or statuses.
  *
@@ -68,6 +68,7 @@ enum d40_command {
  * @base: Pointer to memory area when the pre_alloc_lli's are not large
  * enough, IE bigger than the most common case, 1 dst and 1 src. NULL if
  * pre_alloc_lli is used.
+ * @dma_addr: DMA address, if mapped
  * @size: The size in bytes of the memory at base or the size of pre_alloc_lli.
  * @pre_alloc_lli: Pre allocated area for the most common case of transfers,
  * one buffer to one buffer.
@@ -75,6 +76,7 @@ enum d40_command {
 struct d40_lli_pool {
        void    *base;
        int      size;
+       dma_addr_t      dma_addr;
        /* Space for dst and src, plus an extra for padding */
        u8       pre_alloc_lli[3 * sizeof(struct d40_phy_lli)];
 };
@@ -88,7 +90,7 @@ struct d40_lli_pool {
  * @lli_log: Same as above but for logical channels.
  * @lli_pool: The pool with two entries pre-allocated.
  * @lli_len: Number of llis of current descriptor.
- * @lli_current: Number of transfered llis.
+ * @lli_current: Number of transferred llis.
  * @lcla_alloc: Number of LCLA entries allocated.
  * @txd: DMA engine struct. Used for among other things for communication
  * during a transfer.
@@ -113,6 +115,7 @@ struct d40_desc {
        struct list_head                 node;
 
        bool                             is_in_client_list;
+       bool                             cyclic;
 };
 
 /**
@@ -128,6 +131,7 @@ struct d40_desc {
  */
 struct d40_lcla_pool {
        void            *base;
+       dma_addr_t      dma_addr;
        void            *base_unaligned;
        int              pages;
        spinlock_t       lock;
@@ -171,8 +175,10 @@ struct d40_base;
  * @tasklet: Tasklet that gets scheduled from interrupt context to complete a
  * transfer and call client callback.
  * @client: Cliented owned descriptor list.
+ * @pending_queue: Submitted jobs, to be issued by issue_pending()
  * @active: Active descriptor.
  * @queue: Queued jobs.
+ * @prepare_queue: Prepared jobs.
  * @dma_cfg: The client configuration of this dma channel.
  * @configured: whether the dma_cfg configuration is valid
  * @base: Pointer to the device instance struct.
@@ -181,6 +187,8 @@ struct d40_base;
  * @log_def: Default logical channel settings.
  * @lcla: Space for one dst src pair for logical channel transfers.
  * @lcpa: Pointer to dst and src lcpa settings.
+ * @runtime_addr: runtime configured address.
+ * @runtime_direction: runtime configured direction.
  *
  * This struct can either "be" a logical or a physical channel.
  */
@@ -195,8 +203,10 @@ struct d40_chan {
        struct dma_chan                  chan;
        struct tasklet_struct            tasklet;
        struct list_head                 client;
+       struct list_head                 pending_queue;
        struct list_head                 active;
        struct list_head                 queue;
+       struct list_head                 prepare_queue;
        struct stedma40_chan_cfg         dma_cfg;
        bool                             configured;
        struct d40_base                 *base;
@@ -306,15 +316,32 @@ static struct device *chan2dev(struct d40_chan *d40c)
        return &d40c->chan.dev->device;
 }
 
+static bool chan_is_physical(struct d40_chan *chan)
+{
+       return chan->log_num == D40_PHY_CHAN;
+}
+
+static bool chan_is_logical(struct d40_chan *chan)
+{
+       return !chan_is_physical(chan);
+}
+
 static void __iomem *chan_base(struct d40_chan *chan)
 {
        return chan->base->virtbase + D40_DREG_PCBASE +
               chan->phy_chan->num * D40_DREG_PCDELTA;
 }
 
-static int d40_pool_lli_alloc(struct d40_desc *d40d,
-                             int lli_len, bool is_log)
+#define d40_err(dev, format, arg...)           \
+       dev_err(dev, "[%s] " format, __func__, ## arg)
+
+#define chan_err(d40c, format, arg...)         \
+       d40_err(chan2dev(d40c), format, ## arg)
+
+static int d40_pool_lli_alloc(struct d40_chan *d40c, struct d40_desc *d40d,
+                             int lli_len)
 {
+       bool is_log = chan_is_logical(d40c);
        u32 align;
        void *base;
 
@@ -328,7 +355,7 @@ static int d40_pool_lli_alloc(struct d40_desc *d40d,
                d40d->lli_pool.size = sizeof(d40d->lli_pool.pre_alloc_lli);
                d40d->lli_pool.base = NULL;
        } else {
-               d40d->lli_pool.size = ALIGN(lli_len * 2 * align, align);
+               d40d->lli_pool.size = lli_len * 2 * align;
 
                base = kmalloc(d40d->lli_pool.size + align, GFP_NOWAIT);
                d40d->lli_pool.base = base;
@@ -338,22 +365,37 @@ static int d40_pool_lli_alloc(struct d40_desc *d40d,
        }
 
        if (is_log) {
-               d40d->lli_log.src = PTR_ALIGN((struct d40_log_lli *) base,
-                                             align);
-               d40d->lli_log.dst = PTR_ALIGN(d40d->lli_log.src + lli_len,
-                                             align);
+               d40d->lli_log.src = PTR_ALIGN(base, align);
+               d40d->lli_log.dst = d40d->lli_log.src + lli_len;
+
+               d40d->lli_pool.dma_addr = 0;
        } else {
-               d40d->lli_phy.src = PTR_ALIGN((struct d40_phy_lli *)base,
-                                             align);
-               d40d->lli_phy.dst = PTR_ALIGN(d40d->lli_phy.src + lli_len,
-                                             align);
+               d40d->lli_phy.src = PTR_ALIGN(base, align);
+               d40d->lli_phy.dst = d40d->lli_phy.src + lli_len;
+
+               d40d->lli_pool.dma_addr = dma_map_single(d40c->base->dev,
+                                                        d40d->lli_phy.src,
+                                                        d40d->lli_pool.size,
+                                                        DMA_TO_DEVICE);
+
+               if (dma_mapping_error(d40c->base->dev,
+                                     d40d->lli_pool.dma_addr)) {
+                       kfree(d40d->lli_pool.base);
+                       d40d->lli_pool.base = NULL;
+                       d40d->lli_pool.dma_addr = 0;
+                       return -ENOMEM;
+               }
        }
 
        return 0;
 }
 
-static void d40_pool_lli_free(struct d40_desc *d40d)
+static void d40_pool_lli_free(struct d40_chan *d40c, struct d40_desc *d40d)
 {
+       if (d40d->lli_pool.dma_addr)
+               dma_unmap_single(d40c->base->dev, d40d->lli_pool.dma_addr,
+                                d40d->lli_pool.size, DMA_TO_DEVICE);
+
        kfree(d40d->lli_pool.base);
        d40d->lli_pool.base = NULL;
        d40d->lli_pool.size = 0;
@@ -400,7 +442,7 @@ static int d40_lcla_free_all(struct d40_chan *d40c,
        int i;
        int ret = -EINVAL;
 
-       if (d40c->log_num == D40_PHY_CHAN)
+       if (chan_is_physical(d40c))
                return 0;
 
        spin_lock_irqsave(&d40c->base->lcla_pool.lock, flags);
@@ -439,7 +481,6 @@ static struct d40_desc *d40_desc_get(struct d40_chan *d40c)
 
                list_for_each_entry_safe(d, _d, &d40c->client, node)
                        if (async_tx_test_ack(&d->txd)) {
-                               d40_pool_lli_free(d);
                                d40_desc_remove(d);
                                desc = d;
                                memset(desc, 0, sizeof(*desc));
@@ -459,6 +500,7 @@ static struct d40_desc *d40_desc_get(struct d40_chan *d40c)
 static void d40_desc_free(struct d40_chan *d40c, struct d40_desc *d40d)
 {
 
+       d40_pool_lli_free(d40c, d40d);
        d40_lcla_free_all(d40c, d40d);
        kmem_cache_free(d40c->base->desc_slab, d40d);
 }
@@ -468,57 +510,128 @@ static void d40_desc_submit(struct d40_chan *d40c, struct d40_desc *desc)
        list_add_tail(&desc->node, &d40c->active);
 }
 
-static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d)
+static void d40_phy_lli_load(struct d40_chan *chan, struct d40_desc *desc)
 {
-       int curr_lcla = -EINVAL, next_lcla;
+       struct d40_phy_lli *lli_dst = desc->lli_phy.dst;
+       struct d40_phy_lli *lli_src = desc->lli_phy.src;
+       void __iomem *base = chan_base(chan);
+
+       writel(lli_src->reg_cfg, base + D40_CHAN_REG_SSCFG);
+       writel(lli_src->reg_elt, base + D40_CHAN_REG_SSELT);
+       writel(lli_src->reg_ptr, base + D40_CHAN_REG_SSPTR);
+       writel(lli_src->reg_lnk, base + D40_CHAN_REG_SSLNK);
+
+       writel(lli_dst->reg_cfg, base + D40_CHAN_REG_SDCFG);
+       writel(lli_dst->reg_elt, base + D40_CHAN_REG_SDELT);
+       writel(lli_dst->reg_ptr, base + D40_CHAN_REG_SDPTR);
+       writel(lli_dst->reg_lnk, base + D40_CHAN_REG_SDLNK);
+}
 
-       if (d40c->log_num == D40_PHY_CHAN) {
-               d40_phy_lli_write(d40c->base->virtbase,
-                                 d40c->phy_chan->num,
-                                 d40d->lli_phy.dst,
-                                 d40d->lli_phy.src);
-               d40d->lli_current = d40d->lli_len;
-       } else {
+static void d40_log_lli_to_lcxa(struct d40_chan *chan, struct d40_desc *desc)
+{
+       struct d40_lcla_pool *pool = &chan->base->lcla_pool;
+       struct d40_log_lli_bidir *lli = &desc->lli_log;
+       int lli_current = desc->lli_current;
+       int lli_len = desc->lli_len;
+       bool cyclic = desc->cyclic;
+       int curr_lcla = -EINVAL;
+       int first_lcla = 0;
+       bool linkback;
 
-               if ((d40d->lli_len - d40d->lli_current) > 1)
-                       curr_lcla = d40_lcla_alloc_one(d40c, d40d);
+       /*
+        * We may have partially running cyclic transfers, in case we did't get
+        * enough LCLA entries.
+        */
+       linkback = cyclic && lli_current == 0;
 
-               d40_log_lli_lcpa_write(d40c->lcpa,
-                                      &d40d->lli_log.dst[d40d->lli_current],
-                                      &d40d->lli_log.src[d40d->lli_current],
-                                      curr_lcla);
+       /*
+        * For linkback, we need one LCLA even with only one link, because we
+        * can't link back to the one in LCPA space
+        */
+       if (linkback || (lli_len - lli_current > 1)) {
+               curr_lcla = d40_lcla_alloc_one(chan, desc);
+               first_lcla = curr_lcla;
+       }
 
-               d40d->lli_current++;
-               for (; d40d->lli_current < d40d->lli_len; d40d->lli_current++) {
-                       struct d40_log_lli *lcla;
+       /*
+        * For linkback, we normally load the LCPA in the loop since we need to
+        * link it to the second LCLA and not the first.  However, if we
+        * couldn't even get a first LCLA, then we have to run in LCPA and
+        * reload manually.
+        */
+       if (!linkback || curr_lcla == -EINVAL) {
+               unsigned int flags = 0;
 
-                       if (d40d->lli_current + 1 < d40d->lli_len)
-                               next_lcla = d40_lcla_alloc_one(d40c, d40d);
-                       else
-                               next_lcla = -EINVAL;
+               if (curr_lcla == -EINVAL)
+                       flags |= LLI_TERM_INT;
 
-                       lcla = d40c->base->lcla_pool.base +
-                               d40c->phy_chan->num * 1024 +
-                               8 * curr_lcla * 2;
+               d40_log_lli_lcpa_write(chan->lcpa,
+                                      &lli->dst[lli_current],
+                                      &lli->src[lli_current],
+                                      curr_lcla,
+                                      flags);
+               lli_current++;
+       }
 
-                       d40_log_lli_lcla_write(lcla,
-                                              &d40d->lli_log.dst[d40d->lli_current],
-                                              &d40d->lli_log.src[d40d->lli_current],
-                                              next_lcla);
+       if (curr_lcla < 0)
+               goto out;
 
-                       (void) dma_map_single(d40c->base->dev, lcla,
-                                             2 * sizeof(struct d40_log_lli),
-                                             DMA_TO_DEVICE);
+       for (; lli_current < lli_len; lli_current++) {
+               unsigned int lcla_offset = chan->phy_chan->num * 1024 +
+                                          8 * curr_lcla * 2;
+               struct d40_log_lli *lcla = pool->base + lcla_offset;
+               unsigned int flags = 0;
+               int next_lcla;
 
-                       curr_lcla = next_lcla;
+               if (lli_current + 1 < lli_len)
+                       next_lcla = d40_lcla_alloc_one(chan, desc);
+               else
+                       next_lcla = linkback ? first_lcla : -EINVAL;
 
-                       if (curr_lcla == -EINVAL) {
-                               d40d->lli_current++;
-                               break;
-                       }
+               if (cyclic || next_lcla == -EINVAL)
+                       flags |= LLI_TERM_INT;
 
+               if (linkback && curr_lcla == first_lcla) {
+                       /* First link goes in both LCPA and LCLA */
+                       d40_log_lli_lcpa_write(chan->lcpa,
+                                              &lli->dst[lli_current],
+                                              &lli->src[lli_current],
+                                              next_lcla, flags);
+               }
+
+               /*
+                * One unused LCLA in the cyclic case if the very first
+                * next_lcla fails...
+                */
+               d40_log_lli_lcla_write(lcla,
+                                      &lli->dst[lli_current],
+                                      &lli->src[lli_current],
+                                      next_lcla, flags);
+
+               dma_sync_single_range_for_device(chan->base->dev,
+                                       pool->dma_addr, lcla_offset,
+                                       2 * sizeof(struct d40_log_lli),
+                                       DMA_TO_DEVICE);
+
+               curr_lcla = next_lcla;
+
+               if (curr_lcla == -EINVAL || curr_lcla == first_lcla) {
+                       lli_current++;
+                       break;
                }
        }
+
+out:
+       desc->lli_current = lli_current;
+}
+
+static void d40_desc_load(struct d40_chan *d40c, struct d40_desc *d40d)
+{
+       if (chan_is_physical(d40c)) {
+               d40_phy_lli_load(d40c, d40d);
+               d40d->lli_current = d40d->lli_len;
+       } else
+               d40_log_lli_to_lcxa(d40c, d40d);
 }
 
 static struct d40_desc *d40_first_active_get(struct d40_chan *d40c)
@@ -534,9 +647,25 @@ static struct d40_desc *d40_first_active_get(struct d40_chan *d40c)
        return d;
 }
 
+/* remove desc from current queue and add it to the pending_queue */
 static void d40_desc_queue(struct d40_chan *d40c, struct d40_desc *desc)
 {
-       list_add_tail(&desc->node, &d40c->queue);
+       d40_desc_remove(desc);
+       desc->is_in_client_list = false;
+       list_add_tail(&desc->node, &d40c->pending_queue);
+}
+
+static struct d40_desc *d40_first_pending(struct d40_chan *d40c)
+{
+       struct d40_desc *d;
+
+       if (list_empty(&d40c->pending_queue))
+               return NULL;
+
+       d = list_first_entry(&d40c->pending_queue,
+                            struct d40_desc,
+                            node);
+       return d;
 }
 
 static struct d40_desc *d40_first_queued(struct d40_chan *d40c)
@@ -663,9 +792,9 @@ static int d40_channel_execute_command(struct d40_chan *d40c,
                }
 
                if (i == D40_SUSPEND_MAX_IT) {
-                       dev_err(&d40c->chan.dev->device,
-                               "[%s]: unable to suspend the chl %d (log: %d) status %x\n",
-                               __func__, d40c->phy_chan->num, d40c->log_num,
+                       chan_err(d40c,
+                               "unable to suspend the chl %d (log: %d) status %x\n",
+                               d40c->phy_chan->num, d40c->log_num,
                                status);
                        dump_stack();
                        ret = -EBUSY;
@@ -680,6 +809,7 @@ done:
 static void d40_term_all(struct d40_chan *d40c)
 {
        struct d40_desc *d40d;
+       struct d40_desc *_d;
 
        /* Release active descriptors */
        while ((d40d = d40_first_active_get(d40c))) {
@@ -693,6 +823,26 @@ static void d40_term_all(struct d40_chan *d40c)
                d40_desc_free(d40c, d40d);
        }
 
+       /* Release pending descriptors */
+       while ((d40d = d40_first_pending(d40c))) {
+               d40_desc_remove(d40d);
+               d40_desc_free(d40c, d40d);
+       }
+
+       /* Release client owned descriptors */
+       if (!list_empty(&d40c->client))
+               list_for_each_entry_safe(d40d, _d, &d40c->client, node) {
+                       d40_desc_remove(d40d);
+                       d40_desc_free(d40c, d40d);
+               }
+
+       /* Release descriptors in prepare queue */
+       if (!list_empty(&d40c->prepare_queue))
+               list_for_each_entry_safe(d40d, _d,
+                                        &d40c->prepare_queue, node) {
+                       d40_desc_remove(d40d);
+                       d40_desc_free(d40c, d40d);
+               }
 
        d40c->pending_tx = 0;
        d40c->busy = false;
@@ -788,7 +938,7 @@ static u32 d40_get_prmo(struct d40_chan *d40c)
                        = D40_DREG_PRMO_LCHAN_SRC_LOG_DST_LOG,
        };
 
-       if (d40c->log_num == D40_PHY_CHAN)
+       if (chan_is_physical(d40c))
                return phy_map[d40c->dma_cfg.mode_opt];
        else
                return log_map[d40c->dma_cfg.mode_opt];
@@ -802,7 +952,7 @@ static void d40_config_write(struct d40_chan *d40c)
        /* Odd addresses are even addresses + 4 */
        addr_base = (d40c->phy_chan->num % 2) * 4;
        /* Setup channel mode to logical or physical */
-       var = ((u32)(d40c->log_num != D40_PHY_CHAN) + 1) <<
+       var = ((u32)(chan_is_logical(d40c)) + 1) <<
                D40_CHAN_POS(d40c->phy_chan->num);
        writel(var, d40c->base->virtbase + D40_DREG_PRMSE + addr_base);
 
@@ -811,7 +961,7 @@ static void d40_config_write(struct d40_chan *d40c)
 
        writel(var, d40c->base->virtbase + D40_DREG_PRMOE + addr_base);
 
-       if (d40c->log_num != D40_PHY_CHAN) {
+       if (chan_is_logical(d40c)) {
                int lidx = (d40c->phy_chan->num << D40_SREG_ELEM_LOG_LIDX_POS)
                           & D40_SREG_ELEM_LOG_LIDX_MASK;
                void __iomem *chanbase = chan_base(d40c);
@@ -830,7 +980,7 @@ static u32 d40_residue(struct d40_chan *d40c)
 {
        u32 num_elt;
 
-       if (d40c->log_num != D40_PHY_CHAN)
+       if (chan_is_logical(d40c))
                num_elt = (readl(&d40c->lcpa->lcsp2) & D40_MEM_LCSP2_ECNT_MASK)
                        >> D40_MEM_LCSP2_ECNT_POS;
        else {
@@ -846,7 +996,7 @@ static bool d40_tx_is_linked(struct d40_chan *d40c)
 {
        bool is_link;
 
-       if (d40c->log_num != D40_PHY_CHAN)
+       if (chan_is_logical(d40c))
                is_link = readl(&d40c->lcpa->lcsp3) &  D40_MEM_LCSP3_DLOS_MASK;
        else
                is_link = readl(chan_base(d40c) + D40_CHAN_REG_SDLNK)
@@ -855,10 +1005,8 @@ static bool d40_tx_is_linked(struct d40_chan *d40c)
        return is_link;
 }
 
-static int d40_pause(struct dma_chan *chan)
+static int d40_pause(struct d40_chan *d40c)
 {
-       struct d40_chan *d40c =
-               container_of(chan, struct d40_chan, chan);
        int res = 0;
        unsigned long flags;
 
@@ -869,7 +1017,7 @@ static int d40_pause(struct dma_chan *chan)
 
        res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
        if (res == 0) {
-               if (d40c->log_num != D40_PHY_CHAN) {
+               if (chan_is_logical(d40c)) {
                        d40_config_set_event(d40c, false);
                        /* Resume the other logical channels if any */
                        if (d40_chan_has_events(d40c))
@@ -882,10 +1030,8 @@ static int d40_pause(struct dma_chan *chan)
        return res;
 }
 
-static int d40_resume(struct dma_chan *chan)
+static int d40_resume(struct d40_chan *d40c)
 {
-       struct d40_chan *d40c =
-               container_of(chan, struct d40_chan, chan);
        int res = 0;
        unsigned long flags;
 
@@ -895,7 +1041,7 @@ static int d40_resume(struct dma_chan *chan)
        spin_lock_irqsave(&d40c->lock, flags);
 
        if (d40c->base->rev == 0)
-               if (d40c->log_num != D40_PHY_CHAN) {
+               if (chan_is_logical(d40c)) {
                        res = d40_channel_execute_command(d40c,
                                                          D40_DMA_SUSPEND_REQ);
                        goto no_suspend;
@@ -904,7 +1050,7 @@ static int d40_resume(struct dma_chan *chan)
        /* If bytes left to transfer or linked tx resume job */
        if (d40_residue(d40c) || d40_tx_is_linked(d40c)) {
 
-               if (d40c->log_num != D40_PHY_CHAN)
+               if (chan_is_logical(d40c))
                        d40_config_set_event(d40c, true);
 
                res = d40_channel_execute_command(d40c, D40_DMA_RUN);
@@ -915,6 +1061,22 @@ no_suspend:
        return res;
 }
 
+static int d40_terminate_all(struct d40_chan *chan)
+{
+       unsigned long flags;
+       int ret = 0;
+
+       ret = d40_pause(chan);
+       if (!ret && chan_is_physical(chan))
+               ret = d40_channel_execute_command(chan, D40_DMA_STOP);
+
+       spin_lock_irqsave(&chan->lock, flags);
+       d40_term_all(chan);
+       spin_unlock_irqrestore(&chan->lock, flags);
+
+       return ret;
+}
+
 static dma_cookie_t d40_tx_submit(struct dma_async_tx_descriptor *tx)
 {
        struct d40_chan *d40c = container_of(tx->chan,
@@ -944,7 +1106,7 @@ static int d40_start(struct d40_chan *d40c)
        if (d40c->base->rev == 0) {
                int err;
 
-               if (d40c->log_num != D40_PHY_CHAN) {
+               if (chan_is_logical(d40c)) {
                        err = d40_channel_execute_command(d40c,
                                                          D40_DMA_SUSPEND_REQ);
                        if (err)
@@ -952,7 +1114,7 @@ static int d40_start(struct d40_chan *d40c)
                }
        }
 
-       if (d40c->log_num != D40_PHY_CHAN)
+       if (chan_is_logical(d40c))
                d40_config_set_event(d40c, true);
 
        return d40_channel_execute_command(d40c, D40_DMA_RUN);
@@ -999,17 +1161,36 @@ static void dma_tc_handle(struct d40_chan *d40c)
        if (d40d == NULL)
                return;
 
-       d40_lcla_free_all(d40c, d40d);
+       if (d40d->cyclic) {
+               /*
+                * If this was a paritially loaded list, we need to reloaded
+                * it, and only when the list is completed.  We need to check
+                * for done because the interrupt will hit for every link, and
+                * not just the last one.
+                */
+               if (d40d->lli_current < d40d->lli_len
+                   && !d40_tx_is_linked(d40c)
+                   && !d40_residue(d40c)) {
+                       d40_lcla_free_all(d40c, d40d);
+                       d40_desc_load(d40c, d40d);
+                       (void) d40_start(d40c);
 
-       if (d40d->lli_current < d40d->lli_len) {
-               d40_desc_load(d40c, d40d);
-               /* Start dma job */
-               (void) d40_start(d40c);
-               return;
-       }
+                       if (d40d->lli_current == d40d->lli_len)
+                               d40d->lli_current = 0;
+               }
+       } else {
+               d40_lcla_free_all(d40c, d40d);
+
+               if (d40d->lli_current < d40d->lli_len) {
+                       d40_desc_load(d40c, d40d);
+                       /* Start dma job */
+                       (void) d40_start(d40c);
+                       return;
+               }
 
-       if (d40_queue_start(d40c) == NULL)
-               d40c->busy = false;
+               if (d40_queue_start(d40c) == NULL)
+                       d40c->busy = false;
+       }
 
        d40c->pending_tx++;
        tasklet_schedule(&d40c->tasklet);
@@ -1028,11 +1209,11 @@ static void dma_tasklet(unsigned long data)
 
        /* Get first active entry from list */
        d40d = d40_first_active_get(d40c);
-
        if (d40d == NULL)
                goto err;
 
-       d40c->completed = d40d->txd.cookie;
+       if (!d40d->cyclic)
+               d40c->completed = d40d->txd.cookie;
 
        /*
         * If terminating a channel pending_tx is set to zero.
@@ -1047,16 +1228,17 @@ static void dma_tasklet(unsigned long data)
        callback = d40d->txd.callback;
        callback_param = d40d->txd.callback_param;
 
-       if (async_tx_test_ack(&d40d->txd)) {
-               d40_pool_lli_free(d40d);
-               d40_desc_remove(d40d);
-               d40_desc_free(d40c, d40d);
-       } else {
-               if (!d40d->is_in_client_list) {
+       if (!d40d->cyclic) {
+               if (async_tx_test_ack(&d40d->txd)) {
                        d40_desc_remove(d40d);
-                       d40_lcla_free_all(d40c, d40d);
-                       list_add_tail(&d40d->node, &d40c->client);
-                       d40d->is_in_client_list = true;
+                       d40_desc_free(d40c, d40d);
+               } else {
+                       if (!d40d->is_in_client_list) {
+                               d40_desc_remove(d40d);
+                               d40_lcla_free_all(d40c, d40d);
+                               list_add_tail(&d40d->node, &d40c->client);
+                               d40d->is_in_client_list = true;
+                       }
                }
        }
 
@@ -1073,7 +1255,7 @@ static void dma_tasklet(unsigned long data)
        return;
 
  err:
-       /* Rescue manouver if receiving double interrupts */
+       /* Rescue manoeuvre if receiving double interrupts */
        if (d40c->pending_tx > 0)
                d40c->pending_tx--;
        spin_unlock_irqrestore(&d40c->lock, flags);
@@ -1133,9 +1315,8 @@ static irqreturn_t d40_handle_interrupt(int irq, void *data)
                if (!il[row].is_error)
                        dma_tc_handle(d40c);
                else
-                       dev_err(base->dev,
-                               "[%s] IRQ chan: %ld offset %d idx %d\n",
-                               __func__, chan, il[row].offset, idx);
+                       d40_err(base->dev, "IRQ chan: %ld offset %d idx %d\n",
+                               chan, il[row].offset, idx);
 
                spin_unlock(&d40c->lock);
        }
@@ -1154,8 +1335,7 @@ static int d40_validate_conf(struct d40_chan *d40c,
        bool is_log = conf->mode == STEDMA40_MODE_LOGICAL;
 
        if (!conf->dir) {
-               dev_err(&d40c->chan.dev->device, "[%s] Invalid direction.\n",
-                       __func__);
+               chan_err(d40c, "Invalid direction.\n");
                res = -EINVAL;
        }
 
@@ -1163,46 +1343,40 @@ static int d40_validate_conf(struct d40_chan *d40c,
            d40c->base->plat_data->dev_tx[conf->dst_dev_type] == 0 &&
            d40c->runtime_addr == 0) {
 
-               dev_err(&d40c->chan.dev->device,
-                       "[%s] Invalid TX channel address (%d)\n",
-                       __func__, conf->dst_dev_type);
+               chan_err(d40c, "Invalid TX channel address (%d)\n",
+                        conf->dst_dev_type);
                res = -EINVAL;
        }
 
        if (conf->src_dev_type != STEDMA40_DEV_SRC_MEMORY &&
            d40c->base->plat_data->dev_rx[conf->src_dev_type] == 0 &&
            d40c->runtime_addr == 0) {
-               dev_err(&d40c->chan.dev->device,
-                       "[%s] Invalid RX channel address (%d)\n",
-                       __func__, conf->src_dev_type);
+               chan_err(d40c, "Invalid RX channel address (%d)\n",
+                       conf->src_dev_type);
                res = -EINVAL;
        }
 
        if (conf->dir == STEDMA40_MEM_TO_PERIPH &&
            dst_event_group == STEDMA40_DEV_DST_MEMORY) {
-               dev_err(&d40c->chan.dev->device, "[%s] Invalid dst\n",
-                       __func__);
+               chan_err(d40c, "Invalid dst\n");
                res = -EINVAL;
        }
 
        if (conf->dir == STEDMA40_PERIPH_TO_MEM &&
            src_event_group == STEDMA40_DEV_SRC_MEMORY) {
-               dev_err(&d40c->chan.dev->device, "[%s] Invalid src\n",
-                       __func__);
+               chan_err(d40c, "Invalid src\n");
                res = -EINVAL;
        }
 
        if (src_event_group == STEDMA40_DEV_SRC_MEMORY &&
            dst_event_group == STEDMA40_DEV_DST_MEMORY && is_log) {
-               dev_err(&d40c->chan.dev->device,
-                       "[%s] No event line\n", __func__);
+               chan_err(d40c, "No event line\n");
                res = -EINVAL;
        }
 
        if (conf->dir == STEDMA40_PERIPH_TO_PERIPH &&
            (src_event_group != dst_event_group)) {
-               dev_err(&d40c->chan.dev->device,
-                       "[%s] Invalid event group\n", __func__);
+               chan_err(d40c, "Invalid event group\n");
                res = -EINVAL;
        }
 
@@ -1211,9 +1385,7 @@ static int d40_validate_conf(struct d40_chan *d40c,
                 * DMAC HW supports it. Will be added to this driver,
                 * in case any dma client requires it.
                 */
-               dev_err(&d40c->chan.dev->device,
-                       "[%s] periph to periph not supported\n",
-                       __func__);
+               chan_err(d40c, "periph to periph not supported\n");
                res = -EINVAL;
        }
 
@@ -1226,9 +1398,7 @@ static int d40_validate_conf(struct d40_chan *d40c,
                 * src (burst x width) == dst (burst x width)
                 */
 
-               dev_err(&d40c->chan.dev->device,
-                       "[%s] src (burst x width) != dst (burst x width)\n",
-                       __func__);
+               chan_err(d40c, "src (burst x width) != dst (burst x width)\n");
                res = -EINVAL;
        }
 
@@ -1431,8 +1601,7 @@ static int d40_config_memcpy(struct d40_chan *d40c)
                   dma_has_cap(DMA_SLAVE, cap)) {
                d40c->dma_cfg = *d40c->base->plat_data->memcpy_conf_phy;
        } else {
-               dev_err(&d40c->chan.dev->device, "[%s] No memcpy\n",
-                       __func__);
+               chan_err(d40c, "No memcpy\n");
                return -EINVAL;
        }
 
@@ -1447,31 +1616,18 @@ static int d40_free_dma(struct d40_chan *d40c)
        u32 event;
        struct d40_phy_res *phy = d40c->phy_chan;
        bool is_src;
-       struct d40_desc *d;
-       struct d40_desc *_d;
-
 
        /* Terminate all queued and active transfers */
        d40_term_all(d40c);
 
-       /* Release client owned descriptors */
-       if (!list_empty(&d40c->client))
-               list_for_each_entry_safe(d, _d, &d40c->client, node) {
-                       d40_pool_lli_free(d);
-                       d40_desc_remove(d);
-                       d40_desc_free(d40c, d);
-               }
-
        if (phy == NULL) {
-               dev_err(&d40c->chan.dev->device, "[%s] phy == null\n",
-                       __func__);
+               chan_err(d40c, "phy == null\n");
                return -EINVAL;
        }
 
        if (phy->allocated_src == D40_ALLOC_FREE &&
            phy->allocated_dst == D40_ALLOC_FREE) {
-               dev_err(&d40c->chan.dev->device, "[%s] channel already free\n",
-                       __func__);
+               chan_err(d40c, "channel already free\n");
                return -EINVAL;
        }
 
@@ -1483,19 +1639,17 @@ static int d40_free_dma(struct d40_chan *d40c)
                event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
                is_src = true;
        } else {
-               dev_err(&d40c->chan.dev->device,
-                       "[%s] Unknown direction\n", __func__);
+               chan_err(d40c, "Unknown direction\n");
                return -EINVAL;
        }
 
        res = d40_channel_execute_command(d40c, D40_DMA_SUSPEND_REQ);
        if (res) {
-               dev_err(&d40c->chan.dev->device, "[%s] suspend failed\n",
-                       __func__);
+               chan_err(d40c, "suspend failed\n");
                return res;
        }
 
-       if (d40c->log_num != D40_PHY_CHAN) {
+       if (chan_is_logical(d40c)) {
                /* Release logical channel, deactivate the event line */
 
                d40_config_set_event(d40c, false);
@@ -1511,9 +1665,8 @@ static int d40_free_dma(struct d40_chan *d40c)
                                res = d40_channel_execute_command(d40c,
                                                                  D40_DMA_RUN);
                                if (res) {
-                                       dev_err(&d40c->chan.dev->device,
-                                               "[%s] Executing RUN command\n",
-                                               __func__);
+                                       chan_err(d40c,
+                                               "Executing RUN command\n");
                                        return res;
                                }
                        }
@@ -1526,8 +1679,7 @@ static int d40_free_dma(struct d40_chan *d40c)
        /* Release physical channel */
        res = d40_channel_execute_command(d40c, D40_DMA_STOP);
        if (res) {
-               dev_err(&d40c->chan.dev->device,
-                       "[%s] Failed to stop channel\n", __func__);
+               chan_err(d40c, "Failed to stop channel\n");
                return res;
        }
        d40c->phy_chan = NULL;
@@ -1548,7 +1700,7 @@ static bool d40_is_paused(struct d40_chan *d40c)
 
        spin_lock_irqsave(&d40c->lock, flags);
 
-       if (d40c->log_num == D40_PHY_CHAN) {
+       if (chan_is_physical(d40c)) {
                if (d40c->phy_chan->num % 2 == 0)
                        active_reg = d40c->base->virtbase + D40_DREG_ACTIVE;
                else
@@ -1571,8 +1723,7 @@ static bool d40_is_paused(struct d40_chan *d40c)
                event = D40_TYPE_TO_EVENT(d40c->dma_cfg.src_dev_type);
                status = readl(chanbase + D40_CHAN_REG_SSLNK);
        } else {
-               dev_err(&d40c->chan.dev->device,
-                       "[%s] Unknown direction\n", __func__);
+               chan_err(d40c, "Unknown direction\n");
                goto _exit;
        }
 
@@ -1602,114 +1753,190 @@ static u32 stedma40_residue(struct dma_chan *chan)
        return bytes_left;
 }
 
-struct dma_async_tx_descriptor *stedma40_memcpy_sg(struct dma_chan *chan,
-                                                  struct scatterlist *sgl_dst,
-                                                  struct scatterlist *sgl_src,
-                                                  unsigned int sgl_len,
-                                                  unsigned long dma_flags)
+static int
+d40_prep_sg_log(struct d40_chan *chan, struct d40_desc *desc,
+               struct scatterlist *sg_src, struct scatterlist *sg_dst,
+               unsigned int sg_len, dma_addr_t src_dev_addr,
+               dma_addr_t dst_dev_addr)
 {
-       int res;
-       struct d40_desc *d40d;
-       struct d40_chan *d40c = container_of(chan, struct d40_chan,
-                                            chan);
-       unsigned long flags;
+       struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
+       struct stedma40_half_channel_info *src_info = &cfg->src_info;
+       struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
+       int ret;
 
-       if (d40c->phy_chan == NULL) {
-               dev_err(&d40c->chan.dev->device,
-                       "[%s] Unallocated channel.\n", __func__);
-               return ERR_PTR(-EINVAL);
-       }
+       ret = d40_log_sg_to_lli(sg_src, sg_len,
+                               src_dev_addr,
+                               desc->lli_log.src,
+                               chan->log_def.lcsp1,
+                               src_info->data_width,
+                               dst_info->data_width);
 
-       spin_lock_irqsave(&d40c->lock, flags);
-       d40d = d40_desc_get(d40c);
+       ret = d40_log_sg_to_lli(sg_dst, sg_len,
+                               dst_dev_addr,
+                               desc->lli_log.dst,
+                               chan->log_def.lcsp3,
+                               dst_info->data_width,
+                               src_info->data_width);
 
-       if (d40d == NULL)
+       return ret < 0 ? ret : 0;
+}
+
+static int
+d40_prep_sg_phy(struct d40_chan *chan, struct d40_desc *desc,
+               struct scatterlist *sg_src, struct scatterlist *sg_dst,
+               unsigned int sg_len, dma_addr_t src_dev_addr,
+               dma_addr_t dst_dev_addr)
+{
+       struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
+       struct stedma40_half_channel_info *src_info = &cfg->src_info;
+       struct stedma40_half_channel_info *dst_info = &cfg->dst_info;
+       unsigned long flags = 0;
+       int ret;
+
+       if (desc->cyclic)
+               flags |= LLI_CYCLIC | LLI_TERM_INT;
+
+       ret = d40_phy_sg_to_lli(sg_src, sg_len, src_dev_addr,
+                               desc->lli_phy.src,
+                               virt_to_phys(desc->lli_phy.src),
+                               chan->src_def_cfg,
+                               src_info, dst_info, flags);
+
+       ret = d40_phy_sg_to_lli(sg_dst, sg_len, dst_dev_addr,
+                               desc->lli_phy.dst,
+                               virt_to_phys(desc->lli_phy.dst),
+                               chan->dst_def_cfg,
+                               dst_info, src_info, flags);
+
+       dma_sync_single_for_device(chan->base->dev, desc->lli_pool.dma_addr,
+                                  desc->lli_pool.size, DMA_TO_DEVICE);
+
+       return ret < 0 ? ret : 0;
+}
+
+
+static struct d40_desc *
+d40_prep_desc(struct d40_chan *chan, struct scatterlist *sg,
+             unsigned int sg_len, unsigned long dma_flags)
+{
+       struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
+       struct d40_desc *desc;
+       int ret;
+
+       desc = d40_desc_get(chan);
+       if (!desc)
+               return NULL;
+
+       desc->lli_len = d40_sg_2_dmalen(sg, sg_len, cfg->src_info.data_width,
+                                       cfg->dst_info.data_width);
+       if (desc->lli_len < 0) {
+               chan_err(chan, "Unaligned size\n");
                goto err;
+       }
 
-       d40d->lli_len = d40_sg_2_dmalen(sgl_dst, sgl_len,
-                                       d40c->dma_cfg.src_info.data_width,
-                                       d40c->dma_cfg.dst_info.data_width);
-       if (d40d->lli_len < 0) {
-               dev_err(&d40c->chan.dev->device,
-                       "[%s] Unaligned size\n", __func__);
+       ret = d40_pool_lli_alloc(chan, desc, desc->lli_len);
+       if (ret < 0) {
+               chan_err(chan, "Could not allocate lli\n");
                goto err;
        }
 
-       d40d->lli_current = 0;
-       d40d->txd.flags = dma_flags;
 
-       if (d40c->log_num != D40_PHY_CHAN) {
+       desc->lli_current = 0;
+       desc->txd.flags = dma_flags;
+       desc->txd.tx_submit = d40_tx_submit;
 
-               if (d40_pool_lli_alloc(d40d, d40d->lli_len, true) < 0) {
-                       dev_err(&d40c->chan.dev->device,
-                               "[%s] Out of memory\n", __func__);
-                       goto err;
-               }
+       dma_async_tx_descriptor_init(&desc->txd, &chan->chan);
 
-               (void) d40_log_sg_to_lli(sgl_src,
-                                        sgl_len,
-                                        d40d->lli_log.src,
-                                        d40c->log_def.lcsp1,
-                                        d40c->dma_cfg.src_info.data_width,
-                                        d40c->dma_cfg.dst_info.data_width);
-
-               (void) d40_log_sg_to_lli(sgl_dst,
-                                        sgl_len,
-                                        d40d->lli_log.dst,
-                                        d40c->log_def.lcsp3,
-                                        d40c->dma_cfg.dst_info.data_width,
-                                        d40c->dma_cfg.src_info.data_width);
-       } else {
-               if (d40_pool_lli_alloc(d40d, d40d->lli_len, false) < 0) {
-                       dev_err(&d40c->chan.dev->device,
-                               "[%s] Out of memory\n", __func__);
-                       goto err;
-               }
+       return desc;
+
+err:
+       d40_desc_free(chan, desc);
+       return NULL;
+}
 
-               res = d40_phy_sg_to_lli(sgl_src,
-                                       sgl_len,
-                                       0,
-                                       d40d->lli_phy.src,
-                                       virt_to_phys(d40d->lli_phy.src),
-                                       d40c->src_def_cfg,
-                                       d40c->dma_cfg.src_info.data_width,
-                                       d40c->dma_cfg.dst_info.data_width,
-                                       d40c->dma_cfg.src_info.psize);
+static dma_addr_t
+d40_get_dev_addr(struct d40_chan *chan, enum dma_data_direction direction)
+{
+       struct stedma40_platform_data *plat = chan->base->plat_data;
+       struct stedma40_chan_cfg *cfg = &chan->dma_cfg;
+       dma_addr_t addr = 0;
 
-               if (res < 0)
-                       goto err;
+       if (chan->runtime_addr)
+               return chan->runtime_addr;
 
-               res = d40_phy_sg_to_lli(sgl_dst,
-                                       sgl_len,
-                                       0,
-                                       d40d->lli_phy.dst,
-                                       virt_to_phys(d40d->lli_phy.dst),
-                                       d40c->dst_def_cfg,
-                                       d40c->dma_cfg.dst_info.data_width,
-                                       d40c->dma_cfg.src_info.data_width,
-                                       d40c->dma_cfg.dst_info.psize);
+       if (direction == DMA_FROM_DEVICE)
+               addr = plat->dev_rx[cfg->src_dev_type];
+       else if (direction == DMA_TO_DEVICE)
+               addr = plat->dev_tx[cfg->dst_dev_type];
 
-               if (res < 0)
-                       goto err;
+       return addr;
+}
 
-               (void) dma_map_single(d40c->base->dev, d40d->lli_phy.src,
-                                     d40d->lli_pool.size, DMA_TO_DEVICE);
+static struct dma_async_tx_descriptor *
+d40_prep_sg(struct dma_chan *dchan, struct scatterlist *sg_src,
+           struct scatterlist *sg_dst, unsigned int sg_len,
+           enum dma_data_direction direction, unsigned long dma_flags)
+{
+       struct d40_chan *chan = container_of(dchan, struct d40_chan, chan);
+       dma_addr_t src_dev_addr = 0;
+       dma_addr_t dst_dev_addr = 0;
+       struct d40_desc *desc;
+       unsigned long flags;
+       int ret;
+
+       if (!chan->phy_chan) {
+               chan_err(chan, "Cannot prepare unallocated channel\n");
+               return NULL;
        }
 
-       dma_async_tx_descriptor_init(&d40d->txd, chan);
 
-       d40d->txd.tx_submit = d40_tx_submit;
+       spin_lock_irqsave(&chan->lock, flags);
 
-       spin_unlock_irqrestore(&d40c->lock, flags);
+       desc = d40_prep_desc(chan, sg_src, sg_len, dma_flags);
+       if (desc == NULL)
+               goto err;
+
+       if (sg_next(&sg_src[sg_len - 1]) == sg_src)
+               desc->cyclic = true;
+
+       if (direction != DMA_NONE) {
+               dma_addr_t dev_addr = d40_get_dev_addr(chan, direction);
+
+               if (direction == DMA_FROM_DEVICE)
+                       src_dev_addr = dev_addr;
+               else if (direction == DMA_TO_DEVICE)
+                       dst_dev_addr = dev_addr;
+       }
+
+       if (chan_is_logical(chan))
+               ret = d40_prep_sg_log(chan, desc, sg_src, sg_dst,
+                                     sg_len, src_dev_addr, dst_dev_addr);
+       else
+               ret = d40_prep_sg_phy(chan, desc, sg_src, sg_dst,
+                                     sg_len, src_dev_addr, dst_dev_addr);
+
+       if (ret) {
+               chan_err(chan, "Failed to prepare %s sg job: %d\n",
+                        chan_is_logical(chan) ? "log" : "phy", ret);
+               goto err;
+       }
+
+       /*
+        * add descriptor to the prepare queue in order to be able
+        * to free them later in terminate_all
+        */
+       list_add_tail(&desc->node, &chan->prepare_queue);
+
+       spin_unlock_irqrestore(&chan->lock, flags);
+
+       return &desc->txd;
 
-       return &d40d->txd;
 err:
-       if (d40d)
-               d40_desc_free(d40c, d40d);
-       spin_unlock_irqrestore(&d40c->lock, flags);
+       if (desc)
+               d40_desc_free(chan, desc);
+       spin_unlock_irqrestore(&chan->lock, flags);
        return NULL;
 }
-EXPORT_SYMBOL(stedma40_memcpy_sg);
 
 bool stedma40_filter(struct dma_chan *chan, void *data)
 {
@@ -1732,6 +1959,38 @@ bool stedma40_filter(struct dma_chan *chan, void *data)
 }
 EXPORT_SYMBOL(stedma40_filter);
 
+static void __d40_set_prio_rt(struct d40_chan *d40c, int dev_type, bool src)
+{
+       bool realtime = d40c->dma_cfg.realtime;
+       bool highprio = d40c->dma_cfg.high_priority;
+       u32 prioreg = highprio ? D40_DREG_PSEG1 : D40_DREG_PCEG1;
+       u32 rtreg = realtime ? D40_DREG_RSEG1 : D40_DREG_RCEG1;
+       u32 event = D40_TYPE_TO_EVENT(dev_type);
+       u32 group = D40_TYPE_TO_GROUP(dev_type);
+       u32 bit = 1 << event;
+
+       /* Destination event lines are stored in the upper halfword */
+       if (!src)
+               bit <<= 16;
+
+       writel(bit, d40c->base->virtbase + prioreg + group * 4);
+       writel(bit, d40c->base->virtbase + rtreg + group * 4);
+}
+
+static void d40_set_prio_realtime(struct d40_chan *d40c)
+{
+       if (d40c->base->rev < 3)
+               return;
+
+       if ((d40c->dma_cfg.dir ==  STEDMA40_PERIPH_TO_MEM) ||
+           (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
+               __d40_set_prio_rt(d40c, d40c->dma_cfg.src_dev_type, true);
+
+       if ((d40c->dma_cfg.dir ==  STEDMA40_MEM_TO_PERIPH) ||
+           (d40c->dma_cfg.dir == STEDMA40_PERIPH_TO_PERIPH))
+               __d40_set_prio_rt(d40c, d40c->dma_cfg.dst_dev_type, false);
+}
+
 /* DMA ENGINE functions */
 static int d40_alloc_chan_resources(struct dma_chan *chan)
 {
@@ -1748,9 +2007,7 @@ static int d40_alloc_chan_resources(struct dma_chan *chan)
        if (!d40c->configured) {
                err = d40_config_memcpy(d40c);
                if (err) {
-                       dev_err(&d40c->chan.dev->device,
-                               "[%s] Failed to configure memcpy channel\n",
-                               __func__);
+                       chan_err(d40c, "Failed to configure memcpy channel\n");
                        goto fail;
                }
        }
@@ -1758,16 +2015,17 @@ static int d40_alloc_chan_resources(struct dma_chan *chan)
 
        err = d40_allocate_channel(d40c);
        if (err) {
-               dev_err(&d40c->chan.dev->device,
-                       "[%s] Failed to allocate channel\n", __func__);
+               chan_err(d40c, "Failed to allocate channel\n");
                goto fail;
        }
 
        /* Fill in basic CFG register values */
        d40_phy_cfg(&d40c->dma_cfg, &d40c->src_def_cfg,
-                   &d40c->dst_def_cfg, d40c->log_num != D40_PHY_CHAN);
+                   &d40c->dst_def_cfg, chan_is_logical(d40c));
+
+       d40_set_prio_realtime(d40c);
 
-       if (d40c->log_num != D40_PHY_CHAN) {
+       if (chan_is_logical(d40c)) {
                d40_log_cfg(&d40c->dma_cfg,
                            &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
 
@@ -1800,8 +2058,7 @@ static void d40_free_chan_resources(struct dma_chan *chan)
        unsigned long flags;
 
        if (d40c->phy_chan == NULL) {
-               dev_err(&d40c->chan.dev->device,
-                       "[%s] Cannot free unallocated channel\n", __func__);
+               chan_err(d40c, "Cannot free unallocated channel\n");
                return;
        }
 
@@ -1811,8 +2068,7 @@ static void d40_free_chan_resources(struct dma_chan *chan)
        err = d40_free_dma(d40c);
 
        if (err)
-               dev_err(&d40c->chan.dev->device,
-                       "[%s] Failed to free channel\n", __func__);
+               chan_err(d40c, "Failed to free channel\n");
        spin_unlock_irqrestore(&d40c->lock, flags);
 }
 
@@ -1822,251 +2078,31 @@ static struct dma_async_tx_descriptor *d40_prep_memcpy(struct dma_chan *chan,
                                                       size_t size,
                                                       unsigned long dma_flags)
 {
-       struct d40_desc *d40d;
-       struct d40_chan *d40c = container_of(chan, struct d40_chan,
-                                            chan);
-       unsigned long flags;
+       struct scatterlist dst_sg;
+       struct scatterlist src_sg;
 
-       if (d40c->phy_chan == NULL) {
-               dev_err(&d40c->chan.dev->device,
-                       "[%s] Channel is not allocated.\n", __func__);
-               return ERR_PTR(-EINVAL);
-       }
-
-       spin_lock_irqsave(&d40c->lock, flags);
-       d40d = d40_desc_get(d40c);
-
-       if (d40d == NULL) {
-               dev_err(&d40c->chan.dev->device,
-                       "[%s] Descriptor is NULL\n", __func__);
-               goto err;
-       }
-
-       d40d->txd.flags = dma_flags;
-       d40d->lli_len = d40_size_2_dmalen(size,
-                                         d40c->dma_cfg.src_info.data_width,
-                                         d40c->dma_cfg.dst_info.data_width);
-       if (d40d->lli_len < 0) {
-               dev_err(&d40c->chan.dev->device,
-                       "[%s] Unaligned size\n", __func__);
-               goto err;
-       }
+       sg_init_table(&dst_sg, 1);
+       sg_init_table(&src_sg, 1);
 
+       sg_dma_address(&dst_sg) = dst;
+       sg_dma_address(&src_sg) = src;
 
-       dma_async_tx_descriptor_init(&d40d->txd, chan);
+       sg_dma_len(&dst_sg) = size;
+       sg_dma_len(&src_sg) = size;
 
-       d40d->txd.tx_submit = d40_tx_submit;
-
-       if (d40c->log_num != D40_PHY_CHAN) {
-
-               if (d40_pool_lli_alloc(d40d, d40d->lli_len, true) < 0) {
-                       dev_err(&d40c->chan.dev->device,
-                               "[%s] Out of memory\n", __func__);
-                       goto err;
-               }
-               d40d->lli_current = 0;
-
-               if (d40_log_buf_to_lli(d40d->lli_log.src,
-                                      src,
-                                      size,
-                                      d40c->log_def.lcsp1,
-                                      d40c->dma_cfg.src_info.data_width,
-                                      d40c->dma_cfg.dst_info.data_width,
-                                      true) == NULL)
-                       goto err;
-
-               if (d40_log_buf_to_lli(d40d->lli_log.dst,
-                                      dst,
-                                      size,
-                                      d40c->log_def.lcsp3,
-                                      d40c->dma_cfg.dst_info.data_width,
-                                      d40c->dma_cfg.src_info.data_width,
-                                      true) == NULL)
-                       goto err;
-
-       } else {
-
-               if (d40_pool_lli_alloc(d40d, d40d->lli_len, false) < 0) {
-                       dev_err(&d40c->chan.dev->device,
-                               "[%s] Out of memory\n", __func__);
-                       goto err;
-               }
-
-               if (d40_phy_buf_to_lli(d40d->lli_phy.src,
-                                      src,
-                                      size,
-                                      d40c->dma_cfg.src_info.psize,
-                                      0,
-                                      d40c->src_def_cfg,
-                                      true,
-                                      d40c->dma_cfg.src_info.data_width,
-                                      d40c->dma_cfg.dst_info.data_width,
-                                      false) == NULL)
-                       goto err;
-
-               if (d40_phy_buf_to_lli(d40d->lli_phy.dst,
-                                      dst,
-                                      size,
-                                      d40c->dma_cfg.dst_info.psize,
-                                      0,
-                                      d40c->dst_def_cfg,
-                                      true,
-                                      d40c->dma_cfg.dst_info.data_width,
-                                      d40c->dma_cfg.src_info.data_width,
-                                      false) == NULL)
-                       goto err;
-
-               (void) dma_map_single(d40c->base->dev, d40d->lli_phy.src,
-                                     d40d->lli_pool.size, DMA_TO_DEVICE);
-       }
-
-       spin_unlock_irqrestore(&d40c->lock, flags);
-       return &d40d->txd;
-
-err:
-       if (d40d)
-               d40_desc_free(d40c, d40d);
-       spin_unlock_irqrestore(&d40c->lock, flags);
-       return NULL;
+       return d40_prep_sg(chan, &src_sg, &dst_sg, 1, DMA_NONE, dma_flags);
 }
 
 static struct dma_async_tx_descriptor *
-d40_prep_sg(struct dma_chan *chan,
-           struct scatterlist *dst_sg, unsigned int dst_nents,
-           struct scatterlist *src_sg, unsigned int src_nents,
-           unsigned long dma_flags)
+d40_prep_memcpy_sg(struct dma_chan *chan,
+                  struct scatterlist *dst_sg, unsigned int dst_nents,
+                  struct scatterlist *src_sg, unsigned int src_nents,
+                  unsigned long dma_flags)
 {
        if (dst_nents != src_nents)
                return NULL;
 
-       return stedma40_memcpy_sg(chan, dst_sg, src_sg, dst_nents, dma_flags);
-}
-
-static int d40_prep_slave_sg_log(struct d40_desc *d40d,
-                                struct d40_chan *d40c,
-                                struct scatterlist *sgl,
-                                unsigned int sg_len,
-                                enum dma_data_direction direction,
-                                unsigned long dma_flags)
-{
-       dma_addr_t dev_addr = 0;
-       int total_size;
-
-       d40d->lli_len = d40_sg_2_dmalen(sgl, sg_len,
-                                       d40c->dma_cfg.src_info.data_width,
-                                       d40c->dma_cfg.dst_info.data_width);
-       if (d40d->lli_len < 0) {
-               dev_err(&d40c->chan.dev->device,
-                       "[%s] Unaligned size\n", __func__);
-               return -EINVAL;
-       }
-
-       if (d40_pool_lli_alloc(d40d, d40d->lli_len, true) < 0) {
-               dev_err(&d40c->chan.dev->device,
-                       "[%s] Out of memory\n", __func__);
-               return -ENOMEM;
-       }
-
-       d40d->lli_current = 0;
-
-       if (direction == DMA_FROM_DEVICE)
-               if (d40c->runtime_addr)
-                       dev_addr = d40c->runtime_addr;
-               else
-                       dev_addr = d40c->base->plat_data->dev_rx[d40c->dma_cfg.src_dev_type];
-       else if (direction == DMA_TO_DEVICE)
-               if (d40c->runtime_addr)
-                       dev_addr = d40c->runtime_addr;
-               else
-                       dev_addr = d40c->base->plat_data->dev_tx[d40c->dma_cfg.dst_dev_type];
-
-       else
-               return -EINVAL;
-
-       total_size = d40_log_sg_to_dev(sgl, sg_len,
-                                      &d40d->lli_log,
-                                      &d40c->log_def,
-                                      d40c->dma_cfg.src_info.data_width,
-                                      d40c->dma_cfg.dst_info.data_width,
-                                      direction,
-                                      dev_addr);
-
-       if (total_size < 0)
-               return -EINVAL;
-
-       return 0;
-}
-
-static int d40_prep_slave_sg_phy(struct d40_desc *d40d,
-                                struct d40_chan *d40c,
-                                struct scatterlist *sgl,
-                                unsigned int sgl_len,
-                                enum dma_data_direction direction,
-                                unsigned long dma_flags)
-{
-       dma_addr_t src_dev_addr;
-       dma_addr_t dst_dev_addr;
-       int res;
-
-       d40d->lli_len = d40_sg_2_dmalen(sgl, sgl_len,
-                                       d40c->dma_cfg.src_info.data_width,
-                                       d40c->dma_cfg.dst_info.data_width);
-       if (d40d->lli_len < 0) {
-               dev_err(&d40c->chan.dev->device,
-                       "[%s] Unaligned size\n", __func__);
-               return -EINVAL;
-       }
-
-       if (d40_pool_lli_alloc(d40d, d40d->lli_len, false) < 0) {
-               dev_err(&d40c->chan.dev->device,
-                       "[%s] Out of memory\n", __func__);
-               return -ENOMEM;
-       }
-
-       d40d->lli_current = 0;
-
-       if (direction == DMA_FROM_DEVICE) {
-               dst_dev_addr = 0;
-               if (d40c->runtime_addr)
-                       src_dev_addr = d40c->runtime_addr;
-               else
-                       src_dev_addr = d40c->base->plat_data->dev_rx[d40c->dma_cfg.src_dev_type];
-       } else if (direction == DMA_TO_DEVICE) {
-               if (d40c->runtime_addr)
-                       dst_dev_addr = d40c->runtime_addr;
-               else
-                       dst_dev_addr = d40c->base->plat_data->dev_tx[d40c->dma_cfg.dst_dev_type];
-               src_dev_addr = 0;
-       } else
-               return -EINVAL;
-
-       res = d40_phy_sg_to_lli(sgl,
-                               sgl_len,
-                               src_dev_addr,
-                               d40d->lli_phy.src,
-                               virt_to_phys(d40d->lli_phy.src),
-                               d40c->src_def_cfg,
-                               d40c->dma_cfg.src_info.data_width,
-                               d40c->dma_cfg.dst_info.data_width,
-                               d40c->dma_cfg.src_info.psize);
-       if (res < 0)
-               return res;
-
-       res = d40_phy_sg_to_lli(sgl,
-                               sgl_len,
-                               dst_dev_addr,
-                               d40d->lli_phy.dst,
-                               virt_to_phys(d40d->lli_phy.dst),
-                               d40c->dst_def_cfg,
-                               d40c->dma_cfg.dst_info.data_width,
-                               d40c->dma_cfg.src_info.data_width,
-                               d40c->dma_cfg.dst_info.psize);
-       if (res < 0)
-               return res;
-
-       (void) dma_map_single(d40c->base->dev, d40d->lli_phy.src,
-                             d40d->lli_pool.size, DMA_TO_DEVICE);
-       return 0;
+       return d40_prep_sg(chan, src_sg, dst_sg, src_nents, DMA_NONE, dma_flags);
 }
 
 static struct dma_async_tx_descriptor *d40_prep_slave_sg(struct dma_chan *chan,
@@ -2075,52 +2111,40 @@ static struct dma_async_tx_descriptor *d40_prep_slave_sg(struct dma_chan *chan,
                                                         enum dma_data_direction direction,
                                                         unsigned long dma_flags)
 {
-       struct d40_desc *d40d;
-       struct d40_chan *d40c = container_of(chan, struct d40_chan,
-                                            chan);
-       unsigned long flags;
-       int err;
-
-       if (d40c->phy_chan == NULL) {
-               dev_err(&d40c->chan.dev->device,
-                       "[%s] Cannot prepare unallocated channel\n", __func__);
-               return ERR_PTR(-EINVAL);
-       }
+       if (direction != DMA_FROM_DEVICE && direction != DMA_TO_DEVICE)
+               return NULL;
 
-       spin_lock_irqsave(&d40c->lock, flags);
-       d40d = d40_desc_get(d40c);
+       return d40_prep_sg(chan, sgl, sgl, sg_len, direction, dma_flags);
+}
 
-       if (d40d == NULL)
-               goto err;
+static struct dma_async_tx_descriptor *
+dma40_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr,
+                    size_t buf_len, size_t period_len,
+                    enum dma_data_direction direction)
+{
+       unsigned int periods = buf_len / period_len;
+       struct dma_async_tx_descriptor *txd;
+       struct scatterlist *sg;
+       int i;
 
-       if (d40c->log_num != D40_PHY_CHAN)
-               err = d40_prep_slave_sg_log(d40d, d40c, sgl, sg_len,
-                                           direction, dma_flags);
-       else
-               err = d40_prep_slave_sg_phy(d40d, d40c, sgl, sg_len,
-                                           direction, dma_flags);
-       if (err) {
-               dev_err(&d40c->chan.dev->device,
-                       "[%s] Failed to prepare %s slave sg job: %d\n",
-                       __func__,
-                       d40c->log_num != D40_PHY_CHAN ? "log" : "phy", err);
-               goto err;
+       sg = kcalloc(periods + 1, sizeof(struct scatterlist), GFP_NOWAIT);
+       for (i = 0; i < periods; i++) {
+               sg_dma_address(&sg[i]) = dma_addr;
+               sg_dma_len(&sg[i]) = period_len;
+               dma_addr += period_len;
        }
 
-       d40d->txd.flags = dma_flags;
+       sg[periods].offset = 0;
+       sg[periods].length = 0;
+       sg[periods].page_link =
+               ((unsigned long)sg | 0x01) & ~0x02;
 
-       dma_async_tx_descriptor_init(&d40d->txd, chan);
+       txd = d40_prep_sg(chan, sg, sg, periods, direction,
+                         DMA_PREP_INTERRUPT);
 
-       d40d->txd.tx_submit = d40_tx_submit;
+       kfree(sg);
 
-       spin_unlock_irqrestore(&d40c->lock, flags);
-       return &d40d->txd;
-
-err:
-       if (d40d)
-               d40_desc_free(d40c, d40d);
-       spin_unlock_irqrestore(&d40c->lock, flags);
-       return NULL;
+       return txd;
 }
 
 static enum dma_status d40_tx_status(struct dma_chan *chan,
@@ -2133,9 +2157,7 @@ static enum dma_status d40_tx_status(struct dma_chan *chan,
        int ret;
 
        if (d40c->phy_chan == NULL) {
-               dev_err(&d40c->chan.dev->device,
-                       "[%s] Cannot read status of unallocated channel\n",
-                       __func__);
+               chan_err(d40c, "Cannot read status of unallocated channel\n");
                return -EINVAL;
        }
 
@@ -2159,31 +2181,93 @@ static void d40_issue_pending(struct dma_chan *chan)
        unsigned long flags;
 
        if (d40c->phy_chan == NULL) {
-               dev_err(&d40c->chan.dev->device,
-                       "[%s] Channel is not allocated!\n", __func__);
+               chan_err(d40c, "Channel is not allocated!\n");
                return;
        }
 
        spin_lock_irqsave(&d40c->lock, flags);
 
-       /* Busy means that pending jobs are already being processed */
+       list_splice_tail_init(&d40c->pending_queue, &d40c->queue);
+
+       /* Busy means that queued jobs are already being processed */
        if (!d40c->busy)
                (void) d40_queue_start(d40c);
 
        spin_unlock_irqrestore(&d40c->lock, flags);
 }
 
+static int
+dma40_config_to_halfchannel(struct d40_chan *d40c,
+                           struct stedma40_half_channel_info *info,
+                           enum dma_slave_buswidth width,
+                           u32 maxburst)
+{
+       enum stedma40_periph_data_width addr_width;
+       int psize;
+
+       switch (width) {
+       case DMA_SLAVE_BUSWIDTH_1_BYTE:
+               addr_width = STEDMA40_BYTE_WIDTH;
+               break;
+       case DMA_SLAVE_BUSWIDTH_2_BYTES:
+               addr_width = STEDMA40_HALFWORD_WIDTH;
+               break;
+       case DMA_SLAVE_BUSWIDTH_4_BYTES:
+               addr_width = STEDMA40_WORD_WIDTH;
+               break;
+       case DMA_SLAVE_BUSWIDTH_8_BYTES:
+               addr_width = STEDMA40_DOUBLEWORD_WIDTH;
+               break;
+       default:
+               dev_err(d40c->base->dev,
+                       "illegal peripheral address width "
+                       "requested (%d)\n",
+                       width);
+               return -EINVAL;
+       }
+
+       if (chan_is_logical(d40c)) {
+               if (maxburst >= 16)
+                       psize = STEDMA40_PSIZE_LOG_16;
+               else if (maxburst >= 8)
+                       psize = STEDMA40_PSIZE_LOG_8;
+               else if (maxburst >= 4)
+                       psize = STEDMA40_PSIZE_LOG_4;
+               else
+                       psize = STEDMA40_PSIZE_LOG_1;
+       } else {
+               if (maxburst >= 16)
+                       psize = STEDMA40_PSIZE_PHY_16;
+               else if (maxburst >= 8)
+                       psize = STEDMA40_PSIZE_PHY_8;
+               else if (maxburst >= 4)
+                       psize = STEDMA40_PSIZE_PHY_4;
+               else
+                       psize = STEDMA40_PSIZE_PHY_1;
+       }
+
+       info->data_width = addr_width;
+       info->psize = psize;
+       info->flow_ctrl = STEDMA40_NO_FLOW_CTRL;
+
+       return 0;
+}
+
 /* Runtime reconfiguration extension */
-static void d40_set_runtime_config(struct dma_chan *chan,
-                              struct dma_slave_config *config)
+static int d40_set_runtime_config(struct dma_chan *chan,
+                                 struct dma_slave_config *config)
 {
        struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
        struct stedma40_chan_cfg *cfg = &d40c->dma_cfg;
-       enum dma_slave_buswidth config_addr_width;
+       enum dma_slave_buswidth src_addr_width, dst_addr_width;
        dma_addr_t config_addr;
-       u32 config_maxburst;
-       enum stedma40_periph_data_width addr_width;
-       int psize;
+       u32 src_maxburst, dst_maxburst;
+       int ret;
+
+       src_addr_width = config->src_addr_width;
+       src_maxburst = config->src_maxburst;
+       dst_addr_width = config->dst_addr_width;
+       dst_maxburst = config->dst_maxburst;
 
        if (config->direction == DMA_FROM_DEVICE) {
                dma_addr_t dev_addr_rx =
@@ -2202,8 +2286,11 @@ static void d40_set_runtime_config(struct dma_chan *chan,
                                cfg->dir);
                cfg->dir = STEDMA40_PERIPH_TO_MEM;
 
-               config_addr_width = config->src_addr_width;
-               config_maxburst = config->src_maxburst;
+               /* Configure the memory side */
+               if (dst_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
+                       dst_addr_width = src_addr_width;
+               if (dst_maxburst == 0)
+                       dst_maxburst = src_maxburst;
 
        } else if (config->direction == DMA_TO_DEVICE) {
                dma_addr_t dev_addr_tx =
@@ -2222,71 +2309,42 @@ static void d40_set_runtime_config(struct dma_chan *chan,
                                cfg->dir);
                cfg->dir = STEDMA40_MEM_TO_PERIPH;
 
-               config_addr_width = config->dst_addr_width;
-               config_maxburst = config->dst_maxburst;
-
+               /* Configure the memory side */
+               if (src_addr_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
+                       src_addr_width = dst_addr_width;
+               if (src_maxburst == 0)
+                       src_maxburst = dst_maxburst;
        } else {
                dev_err(d40c->base->dev,
                        "unrecognized channel direction %d\n",
                        config->direction);
-               return;
+               return -EINVAL;
        }
 
-       switch (config_addr_width) {
-       case DMA_SLAVE_BUSWIDTH_1_BYTE:
-               addr_width = STEDMA40_BYTE_WIDTH;
-               break;
-       case DMA_SLAVE_BUSWIDTH_2_BYTES:
-               addr_width = STEDMA40_HALFWORD_WIDTH;
-               break;
-       case DMA_SLAVE_BUSWIDTH_4_BYTES:
-               addr_width = STEDMA40_WORD_WIDTH;
-               break;
-       case DMA_SLAVE_BUSWIDTH_8_BYTES:
-               addr_width = STEDMA40_DOUBLEWORD_WIDTH;
-               break;
-       default:
+       if (src_maxburst * src_addr_width != dst_maxburst * dst_addr_width) {
                dev_err(d40c->base->dev,
-                       "illegal peripheral address width "
-                       "requested (%d)\n",
-                       config->src_addr_width);
-               return;
+                       "src/dst width/maxburst mismatch: %d*%d != %d*%d\n",
+                       src_maxburst,
+                       src_addr_width,
+                       dst_maxburst,
+                       dst_addr_width);
+               return -EINVAL;
        }
 
-       if (d40c->log_num != D40_PHY_CHAN) {
-               if (config_maxburst >= 16)
-                       psize = STEDMA40_PSIZE_LOG_16;
-               else if (config_maxburst >= 8)
-                       psize = STEDMA40_PSIZE_LOG_8;
-               else if (config_maxburst >= 4)
-                       psize = STEDMA40_PSIZE_LOG_4;
-               else
-                       psize = STEDMA40_PSIZE_LOG_1;
-       } else {
-               if (config_maxburst >= 16)
-                       psize = STEDMA40_PSIZE_PHY_16;
-               else if (config_maxburst >= 8)
-                       psize = STEDMA40_PSIZE_PHY_8;
-               else if (config_maxburst >= 4)
-                       psize = STEDMA40_PSIZE_PHY_4;
-               else if (config_maxburst >= 2)
-                       psize = STEDMA40_PSIZE_PHY_2;
-               else
-                       psize = STEDMA40_PSIZE_PHY_1;
-       }
+       ret = dma40_config_to_halfchannel(d40c, &cfg->src_info,
+                                         src_addr_width,
+                                         src_maxburst);
+       if (ret)
+               return ret;
 
-       /* Set up all the endpoint configs */
-       cfg->src_info.data_width = addr_width;
-       cfg->src_info.psize = psize;
-       cfg->src_info.big_endian = false;
-       cfg->src_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL;
-       cfg->dst_info.data_width = addr_width;
-       cfg->dst_info.psize = psize;
-       cfg->dst_info.big_endian = false;
-       cfg->dst_info.flow_ctrl = STEDMA40_NO_FLOW_CTRL;
+       ret = dma40_config_to_halfchannel(d40c, &cfg->dst_info,
+                                         dst_addr_width,
+                                         dst_maxburst);
+       if (ret)
+               return ret;
 
        /* Fill in register values */
-       if (d40c->log_num != D40_PHY_CHAN)
+       if (chan_is_logical(d40c))
                d40_log_cfg(cfg, &d40c->log_def.lcsp1, &d40c->log_def.lcsp3);
        else
                d40_phy_cfg(cfg, &d40c->src_def_cfg,
@@ -2296,40 +2354,36 @@ static void d40_set_runtime_config(struct dma_chan *chan,
        d40c->runtime_addr = config_addr;
        d40c->runtime_direction = config->direction;
        dev_dbg(d40c->base->dev,
-               "configured channel %s for %s, data width %d, "
-               "maxburst %d bytes, LE, no flow control\n",
+               "configured channel %s for %s, data width %d/%d, "
+               "maxburst %d/%d elements, LE, no flow control\n",
                dma_chan_name(chan),
                (config->direction == DMA_FROM_DEVICE) ? "RX" : "TX",
-               config_addr_width,
-               config_maxburst);
+               src_addr_width, dst_addr_width,
+               src_maxburst, dst_maxburst);
+
+       return 0;
 }
 
 static int d40_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
                       unsigned long arg)
 {
-       unsigned long flags;
        struct d40_chan *d40c = container_of(chan, struct d40_chan, chan);
 
        if (d40c->phy_chan == NULL) {
-               dev_err(&d40c->chan.dev->device,
-                       "[%s] Channel is not allocated!\n", __func__);
+               chan_err(d40c, "Channel is not allocated!\n");
                return -EINVAL;
        }
 
        switch (cmd) {
        case DMA_TERMINATE_ALL:
-               spin_lock_irqsave(&d40c->lock, flags);
-               d40_term_all(d40c);
-               spin_unlock_irqrestore(&d40c->lock, flags);
-               return 0;
+               return d40_terminate_all(d40c);
        case DMA_PAUSE:
-               return d40_pause(chan);
+               return d40_pause(d40c);
        case DMA_RESUME:
-               return d40_resume(chan);
+               return d40_resume(d40c);
        case DMA_SLAVE_CONFIG:
-               d40_set_runtime_config(chan,
+               return d40_set_runtime_config(chan,
                        (struct dma_slave_config *) arg);
-               return 0;
        default:
                break;
        }
@@ -2360,7 +2414,9 @@ static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
 
                INIT_LIST_HEAD(&d40c->active);
                INIT_LIST_HEAD(&d40c->queue);
+               INIT_LIST_HEAD(&d40c->pending_queue);
                INIT_LIST_HEAD(&d40c->client);
+               INIT_LIST_HEAD(&d40c->prepare_queue);
 
                tasklet_init(&d40c->tasklet, dma_tasklet,
                             (unsigned long) d40c);
@@ -2370,6 +2426,35 @@ static void __init d40_chan_init(struct d40_base *base, struct dma_device *dma,
        }
 }
 
+static void d40_ops_init(struct d40_base *base, struct dma_device *dev)
+{
+       if (dma_has_cap(DMA_SLAVE, dev->cap_mask))
+               dev->device_prep_slave_sg = d40_prep_slave_sg;
+
+       if (dma_has_cap(DMA_MEMCPY, dev->cap_mask)) {
+               dev->device_prep_dma_memcpy = d40_prep_memcpy;
+
+               /*
+                * This controller can only access address at even
+                * 32bit boundaries, i.e. 2^2
+                */
+               dev->copy_align = 2;
+       }
+
+       if (dma_has_cap(DMA_SG, dev->cap_mask))
+               dev->device_prep_dma_sg = d40_prep_memcpy_sg;
+
+       if (dma_has_cap(DMA_CYCLIC, dev->cap_mask))
+               dev->device_prep_dma_cyclic = dma40_prep_dma_cyclic;
+
+       dev->device_alloc_chan_resources = d40_alloc_chan_resources;
+       dev->device_free_chan_resources = d40_free_chan_resources;
+       dev->device_issue_pending = d40_issue_pending;
+       dev->device_tx_status = d40_tx_status;
+       dev->device_control = d40_control;
+       dev->dev = base->dev;
+}
+
 static int __init d40_dmaengine_init(struct d40_base *base,
                                     int num_reserved_chans)
 {
@@ -2380,23 +2465,14 @@ static int __init d40_dmaengine_init(struct d40_base *base,
 
        dma_cap_zero(base->dma_slave.cap_mask);
        dma_cap_set(DMA_SLAVE, base->dma_slave.cap_mask);
+       dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
 
-       base->dma_slave.device_alloc_chan_resources = d40_alloc_chan_resources;
-       base->dma_slave.device_free_chan_resources = d40_free_chan_resources;
-       base->dma_slave.device_prep_dma_memcpy = d40_prep_memcpy;
-       base->dma_slave.device_prep_dma_sg = d40_prep_sg;
-       base->dma_slave.device_prep_slave_sg = d40_prep_slave_sg;
-       base->dma_slave.device_tx_status = d40_tx_status;
-       base->dma_slave.device_issue_pending = d40_issue_pending;
-       base->dma_slave.device_control = d40_control;
-       base->dma_slave.dev = base->dev;
+       d40_ops_init(base, &base->dma_slave);
 
        err = dma_async_device_register(&base->dma_slave);
 
        if (err) {
-               dev_err(base->dev,
-                       "[%s] Failed to register slave channels\n",
-                       __func__);
+               d40_err(base->dev, "Failed to register slave channels\n");
                goto failure1;
        }
 
@@ -2405,29 +2481,15 @@ static int __init d40_dmaengine_init(struct d40_base *base,
 
        dma_cap_zero(base->dma_memcpy.cap_mask);
        dma_cap_set(DMA_MEMCPY, base->dma_memcpy.cap_mask);
-       dma_cap_set(DMA_SG, base->dma_slave.cap_mask);
-
-       base->dma_memcpy.device_alloc_chan_resources = d40_alloc_chan_resources;
-       base->dma_memcpy.device_free_chan_resources = d40_free_chan_resources;
-       base->dma_memcpy.device_prep_dma_memcpy = d40_prep_memcpy;
-       base->dma_slave.device_prep_dma_sg = d40_prep_sg;
-       base->dma_memcpy.device_prep_slave_sg = d40_prep_slave_sg;
-       base->dma_memcpy.device_tx_status = d40_tx_status;
-       base->dma_memcpy.device_issue_pending = d40_issue_pending;
-       base->dma_memcpy.device_control = d40_control;
-       base->dma_memcpy.dev = base->dev;
-       /*
-        * This controller can only access address at even
-        * 32bit boundaries, i.e. 2^2
-        */
-       base->dma_memcpy.copy_align = 2;
+       dma_cap_set(DMA_SG, base->dma_memcpy.cap_mask);
+
+       d40_ops_init(base, &base->dma_memcpy);
 
        err = dma_async_device_register(&base->dma_memcpy);
 
        if (err) {
-               dev_err(base->dev,
-                       "[%s] Failed to regsiter memcpy only channels\n",
-                       __func__);
+               d40_err(base->dev,
+                       "Failed to regsiter memcpy only channels\n");
                goto failure2;
        }
 
@@ -2437,24 +2499,15 @@ static int __init d40_dmaengine_init(struct d40_base *base,
        dma_cap_zero(base->dma_both.cap_mask);
        dma_cap_set(DMA_SLAVE, base->dma_both.cap_mask);
        dma_cap_set(DMA_MEMCPY, base->dma_both.cap_mask);
-       dma_cap_set(DMA_SG, base->dma_slave.cap_mask);
-
-       base->dma_both.device_alloc_chan_resources = d40_alloc_chan_resources;
-       base->dma_both.device_free_chan_resources = d40_free_chan_resources;
-       base->dma_both.device_prep_dma_memcpy = d40_prep_memcpy;
-       base->dma_slave.device_prep_dma_sg = d40_prep_sg;
-       base->dma_both.device_prep_slave_sg = d40_prep_slave_sg;
-       base->dma_both.device_tx_status = d40_tx_status;
-       base->dma_both.device_issue_pending = d40_issue_pending;
-       base->dma_both.device_control = d40_control;
-       base->dma_both.dev = base->dev;
-       base->dma_both.copy_align = 2;
+       dma_cap_set(DMA_SG, base->dma_both.cap_mask);
+       dma_cap_set(DMA_CYCLIC, base->dma_slave.cap_mask);
+
+       d40_ops_init(base, &base->dma_both);
        err = dma_async_device_register(&base->dma_both);
 
        if (err) {
-               dev_err(base->dev,
-                       "[%s] Failed to register logical and physical capable channels\n",
-                       __func__);
+               d40_err(base->dev,
+                       "Failed to register logical and physical capable channels\n");
                goto failure3;
        }
        return 0;
@@ -2524,24 +2577,6 @@ static int __init d40_phy_res_init(struct d40_base *base)
 
 static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev)
 {
-       static const struct d40_reg_val dma_id_regs[] = {
-               /* Peripheral Id */
-               { .reg = D40_DREG_PERIPHID0, .val = 0x0040},
-               { .reg = D40_DREG_PERIPHID1, .val = 0x0000},
-               /*
-                * D40_DREG_PERIPHID2 Depends on HW revision:
-                *  MOP500/HREF ED has 0x0008,
-                *  ? has 0x0018,
-                *  HREF V1 has 0x0028
-                */
-               { .reg = D40_DREG_PERIPHID3, .val = 0x0000},
-
-               /* PCell Id */
-               { .reg = D40_DREG_CELLID0, .val = 0x000d},
-               { .reg = D40_DREG_CELLID1, .val = 0x00f0},
-               { .reg = D40_DREG_CELLID2, .val = 0x0005},
-               { .reg = D40_DREG_CELLID3, .val = 0x00b1}
-       };
        struct stedma40_platform_data *plat_data;
        struct clk *clk = NULL;
        void __iomem *virtbase = NULL;
@@ -2550,14 +2585,14 @@ static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev)
        int num_log_chans = 0;
        int num_phy_chans;
        int i;
-       u32 val;
-       u32 rev;
+       u32 pid;
+       u32 cid;
+       u8 rev;
 
        clk = clk_get(&pdev->dev, NULL);
 
        if (IS_ERR(clk)) {
-               dev_err(&pdev->dev, "[%s] No matching clock found\n",
-                       __func__);
+               d40_err(&pdev->dev, "No matching clock found\n");
                goto failure;
        }
 
@@ -2576,34 +2611,32 @@ static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev)
        if (!virtbase)
                goto failure;
 
-       /* HW version check */
-       for (i = 0; i < ARRAY_SIZE(dma_id_regs); i++) {
-               if (dma_id_regs[i].val !=
-                   readl(virtbase + dma_id_regs[i].reg)) {
-                       dev_err(&pdev->dev,
-                               "[%s] Unknown hardware! Expected 0x%x at 0x%x but got 0x%x\n",
-                               __func__,
-                               dma_id_regs[i].val,
-                               dma_id_regs[i].reg,
-                               readl(virtbase + dma_id_regs[i].reg));
-                       goto failure;
-               }
-       }
-
-       /* Get silicon revision and designer */
-       val = readl(virtbase + D40_DREG_PERIPHID2);
+       /* This is just a regular AMBA PrimeCell ID actually */
+       for (pid = 0, i = 0; i < 4; i++)
+               pid |= (readl(virtbase + resource_size(res) - 0x20 + 4 * i)
+                       & 255) << (i * 8);
+       for (cid = 0, i = 0; i < 4; i++)
+               cid |= (readl(virtbase + resource_size(res) - 0x10 + 4 * i)
+                       & 255) << (i * 8);
 
-       if ((val & D40_DREG_PERIPHID2_DESIGNER_MASK) !=
-           D40_HW_DESIGNER) {
-               dev_err(&pdev->dev,
-                       "[%s] Unknown designer! Got %x wanted %x\n",
-                       __func__, val & D40_DREG_PERIPHID2_DESIGNER_MASK,
-                       D40_HW_DESIGNER);
+       if (cid != AMBA_CID) {
+               d40_err(&pdev->dev, "Unknown hardware! No PrimeCell ID\n");
                goto failure;
        }
-
-       rev = (val & D40_DREG_PERIPHID2_REV_MASK) >>
-               D40_DREG_PERIPHID2_REV_POS;
+       if (AMBA_MANF_BITS(pid) != AMBA_VENDOR_ST) {
+               d40_err(&pdev->dev, "Unknown designer! Got %x wanted %x\n",
+                       AMBA_MANF_BITS(pid),
+                       AMBA_VENDOR_ST);
+               goto failure;
+       }
+       /*
+        * HW revision:
+        * DB8500ed has revision 0
+        * ? has revision 1
+        * DB8500v1 has revision 2
+        * DB8500v2 has revision 3
+        */
+       rev = AMBA_REV_BITS(pid);
 
        /* The number of physical channels on this HW */
        num_phy_chans = 4 * (readl(virtbase + D40_DREG_ICFG) & 0x7) + 4;
@@ -2627,7 +2660,7 @@ static struct d40_base * __init d40_hw_detect_init(struct platform_device *pdev)
                       sizeof(struct d40_chan), GFP_KERNEL);
 
        if (base == NULL) {
-               dev_err(&pdev->dev, "[%s] Out of memory\n", __func__);
+               d40_err(&pdev->dev, "Out of memory\n");
                goto failure;
        }
 
@@ -2774,6 +2807,7 @@ static void __init d40_hw_init(struct d40_base *base)
 
 static int __init d40_lcla_allocate(struct d40_base *base)
 {
+       struct d40_lcla_pool *pool = &base->lcla_pool;
        unsigned long *page_list;
        int i, j;
        int ret = 0;
@@ -2799,9 +2833,8 @@ static int __init d40_lcla_allocate(struct d40_base *base)
                                                base->lcla_pool.pages);
                if (!page_list[i]) {
 
-                       dev_err(base->dev,
-                               "[%s] Failed to allocate %d pages.\n",
-                               __func__, base->lcla_pool.pages);
+                       d40_err(base->dev, "Failed to allocate %d pages.\n",
+                               base->lcla_pool.pages);
 
                        for (j = 0; j < i; j++)
                                free_pages(page_list[j], base->lcla_pool.pages);
@@ -2839,6 +2872,15 @@ static int __init d40_lcla_allocate(struct d40_base *base)
                                                 LCLA_ALIGNMENT);
        }
 
+       pool->dma_addr = dma_map_single(base->dev, pool->base,
+                                       SZ_1K * base->num_phy_chans,
+                                       DMA_TO_DEVICE);
+       if (dma_mapping_error(base->dev, pool->dma_addr)) {
+               pool->dma_addr = 0;
+               ret = -ENOMEM;
+               goto failure;
+       }
+
        writel(virt_to_phys(base->lcla_pool.base),
               base->virtbase + D40_DREG_LCLA);
 failure:
@@ -2871,9 +2913,7 @@ static int __init d40_probe(struct platform_device *pdev)
        res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "lcpa");
        if (!res) {
                ret = -ENOENT;
-               dev_err(&pdev->dev,
-                       "[%s] No \"lcpa\" memory resource\n",
-                       __func__);
+               d40_err(&pdev->dev, "No \"lcpa\" memory resource\n");
                goto failure;
        }
        base->lcpa_size = resource_size(res);
@@ -2882,9 +2922,9 @@ static int __init d40_probe(struct platform_device *pdev)
        if (request_mem_region(res->start, resource_size(res),
                               D40_NAME " I/O lcpa") == NULL) {
                ret = -EBUSY;
-               dev_err(&pdev->dev,
-                       "[%s] Failed to request LCPA region 0x%x-0x%x\n",
-                       __func__, res->start, res->end);
+               d40_err(&pdev->dev,
+                       "Failed to request LCPA region 0x%x-0x%x\n",
+                       res->start, res->end);
                goto failure;
        }
 
@@ -2900,16 +2940,13 @@ static int __init d40_probe(struct platform_device *pdev)
        base->lcpa_base = ioremap(res->start, resource_size(res));
        if (!base->lcpa_base) {
                ret = -ENOMEM;
-               dev_err(&pdev->dev,
-                       "[%s] Failed to ioremap LCPA region\n",
-                       __func__);
+               d40_err(&pdev->dev, "Failed to ioremap LCPA region\n");
                goto failure;
        }
 
        ret = d40_lcla_allocate(base);
        if (ret) {
-               dev_err(&pdev->dev, "[%s] Failed to allocate LCLA area\n",
-                       __func__);
+               d40_err(&pdev->dev, "Failed to allocate LCLA area\n");
                goto failure;
        }
 
@@ -2918,9 +2955,8 @@ static int __init d40_probe(struct platform_device *pdev)
        base->irq = platform_get_irq(pdev, 0);
 
        ret = request_irq(base->irq, d40_handle_interrupt, 0, D40_NAME, base);
-
        if (ret) {
-               dev_err(&pdev->dev, "[%s] No IRQ defined\n", __func__);
+               d40_err(&pdev->dev, "No IRQ defined\n");
                goto failure;
        }
 
@@ -2939,6 +2975,12 @@ failure:
                        kmem_cache_destroy(base->desc_slab);
                if (base->virtbase)
                        iounmap(base->virtbase);
+
+               if (base->lcla_pool.dma_addr)
+                       dma_unmap_single(base->dev, base->lcla_pool.dma_addr,
+                                        SZ_1K * base->num_phy_chans,
+                                        DMA_TO_DEVICE);
+
                if (!base->lcla_pool.base_unaligned && base->lcla_pool.base)
                        free_pages((unsigned long)base->lcla_pool.base,
                                   base->lcla_pool.pages);
@@ -2963,7 +3005,7 @@ failure:
                kfree(base);
        }
 
-       dev_err(&pdev->dev, "[%s] probe failed\n", __func__);
+       d40_err(&pdev->dev, "probe failed\n");
        return ret;
 }
 
@@ -2978,4 +3020,4 @@ static int __init stedma40_init(void)
 {
        return platform_driver_probe(&d40_driver, d40_probe);
 }
-arch_initcall(stedma40_init);
+subsys_initcall(stedma40_init);