]> Pileus Git - ~andy/linux/blobdiff - drivers/dma/dw_dmac.c
dmaengine: add context parameter to prep_slave_sg and prep_dma_cyclic
[~andy/linux] / drivers / dma / dw_dmac.c
index 9f4310f013f5020029b89f72867ffcbfcc00d85c..7439079f5eed9c58bc9d39e0fc92795e29adc7dd 100644 (file)
@@ -9,6 +9,7 @@
  * it under the terms of the GNU General Public License version 2 as
  * published by the Free Software Foundation.
  */
+#include <linux/bitops.h>
 #include <linux/clk.h>
 #include <linux/delay.h>
 #include <linux/dmaengine.h>
@@ -22,6 +23,7 @@
 #include <linux/slab.h>
 
 #include "dw_dmac_regs.h"
+#include "dmaengine.h"
 
 /*
  * This supports the Synopsys "DesignWare AHB Central DMA Controller",
  * which does not support descriptor writeback.
  */
 
-#define DWC_DEFAULT_CTLLO(private) ({                          \
-               struct dw_dma_slave *__slave = (private);       \
-               int dms = __slave ? __slave->dst_master : 0;    \
-               int sms = __slave ? __slave->src_master : 1;    \
-               u8 smsize = __slave ? __slave->src_msize : DW_DMA_MSIZE_16; \
-               u8 dmsize = __slave ? __slave->dst_msize : DW_DMA_MSIZE_16; \
+#define DWC_DEFAULT_CTLLO(_chan) ({                            \
+               struct dw_dma_slave *__slave = (_chan->private);        \
+               struct dw_dma_chan *_dwc = to_dw_dma_chan(_chan);       \
+               struct dma_slave_config *_sconfig = &_dwc->dma_sconfig; \
+               int _dms = __slave ? __slave->dst_master : 0;   \
+               int _sms = __slave ? __slave->src_master : 1;   \
+               u8 _smsize = __slave ? _sconfig->src_maxburst : \
+                       DW_DMA_MSIZE_16;                        \
+               u8 _dmsize = __slave ? _sconfig->dst_maxburst : \
+                       DW_DMA_MSIZE_16;                        \
                                                                \
-               (DWC_CTLL_DST_MSIZE(dmsize)                     \
-                | DWC_CTLL_SRC_MSIZE(smsize)                   \
+               (DWC_CTLL_DST_MSIZE(_dmsize)                    \
+                | DWC_CTLL_SRC_MSIZE(_smsize)                  \
                 | DWC_CTLL_LLP_D_EN                            \
                 | DWC_CTLL_LLP_S_EN                            \
-                | DWC_CTLL_DMS(dms)                            \
-                | DWC_CTLL_SMS(sms));                          \
+                | DWC_CTLL_DMS(_dms)                           \
+                | DWC_CTLL_SMS(_sms));                         \
        })
 
 /*
@@ -151,21 +157,6 @@ static void dwc_desc_put(struct dw_dma_chan *dwc, struct dw_desc *desc)
        }
 }
 
-/* Called with dwc->lock held and bh disabled */
-static dma_cookie_t
-dwc_assign_cookie(struct dw_dma_chan *dwc, struct dw_desc *desc)
-{
-       dma_cookie_t cookie = dwc->chan.cookie;
-
-       if (++cookie < 0)
-               cookie = 1;
-
-       dwc->chan.cookie = cookie;
-       desc->txd.cookie = cookie;
-
-       return cookie;
-}
-
 static void dwc_initialize(struct dw_dma_chan *dwc)
 {
        struct dw_dma *dw = to_dw_dma(dwc->chan.device);
@@ -244,7 +235,7 @@ dwc_descriptor_complete(struct dw_dma_chan *dwc, struct dw_desc *desc,
        dev_vdbg(chan2dev(&dwc->chan), "descriptor %u complete\n", txd->cookie);
 
        spin_lock_irqsave(&dwc->lock, flags);
-       dwc->completed = txd->cookie;
+       dma_cookie_complete(txd);
        if (callback_required) {
                callback = txd->callback;
                param = txd->callback_param;
@@ -597,7 +588,7 @@ static dma_cookie_t dwc_tx_submit(struct dma_async_tx_descriptor *tx)
        unsigned long           flags;
 
        spin_lock_irqsave(&dwc->lock, flags);
-       cookie = dwc_assign_cookie(dwc, desc);
+       cookie = dma_cookie_assign(tx);
 
        /*
         * REVISIT: We should attempt to chain as many descriptors as
@@ -656,7 +647,7 @@ dwc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
        else
                src_width = dst_width = 0;
 
-       ctllo = DWC_DEFAULT_CTLLO(chan->private)
+       ctllo = DWC_DEFAULT_CTLLO(chan)
                        | DWC_CTLL_DST_WIDTH(dst_width)
                        | DWC_CTLL_SRC_WIDTH(src_width)
                        | DWC_CTLL_DST_INC
@@ -713,10 +704,11 @@ err_desc_get:
 static struct dma_async_tx_descriptor *
 dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
                unsigned int sg_len, enum dma_transfer_direction direction,
-               unsigned long flags)
+               unsigned long flags, void *context)
 {
        struct dw_dma_chan      *dwc = to_dw_dma_chan(chan);
        struct dw_dma_slave     *dws = chan->private;
+       struct dma_slave_config *sconfig = &dwc->dma_sconfig;
        struct dw_desc          *prev;
        struct dw_desc          *first;
        u32                     ctllo;
@@ -732,17 +724,20 @@ dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
        if (unlikely(!dws || !sg_len))
                return NULL;
 
-       reg_width = dws->reg_width;
        prev = first = NULL;
 
        switch (direction) {
        case DMA_MEM_TO_DEV:
-               ctllo = (DWC_DEFAULT_CTLLO(chan->private)
+               reg_width = __fls(sconfig->dst_addr_width);
+               reg = sconfig->dst_addr;
+               ctllo = (DWC_DEFAULT_CTLLO(chan)
                                | DWC_CTLL_DST_WIDTH(reg_width)
                                | DWC_CTLL_DST_FIX
-                               | DWC_CTLL_SRC_INC
-                               | DWC_CTLL_FC(dws->fc));
-               reg = dws->tx_reg;
+                               | DWC_CTLL_SRC_INC);
+
+               ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_M2P) :
+                       DWC_CTLL_FC(DW_DMA_FC_D_M2P);
+
                for_each_sg(sgl, sg, sg_len, i) {
                        struct dw_desc  *desc;
                        u32             len, dlen, mem;
@@ -800,13 +795,16 @@ slave_sg_todev_fill_desc:
                }
                break;
        case DMA_DEV_TO_MEM:
-               ctllo = (DWC_DEFAULT_CTLLO(chan->private)
+               reg_width = __fls(sconfig->src_addr_width);
+               reg = sconfig->src_addr;
+               ctllo = (DWC_DEFAULT_CTLLO(chan)
                                | DWC_CTLL_SRC_WIDTH(reg_width)
                                | DWC_CTLL_DST_INC
-                               | DWC_CTLL_SRC_FIX
-                               | DWC_CTLL_FC(dws->fc));
+                               | DWC_CTLL_SRC_FIX);
+
+               ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_P2M) :
+                       DWC_CTLL_FC(DW_DMA_FC_D_P2M);
 
-               reg = dws->rx_reg;
                for_each_sg(sgl, sg, sg_len, i) {
                        struct dw_desc  *desc;
                        u32             len, dlen, mem;
@@ -884,6 +882,39 @@ err_desc_get:
        return NULL;
 }
 
+/*
+ * Fix sconfig's burst size according to dw_dmac. We need to convert them as:
+ * 1 -> 0, 4 -> 1, 8 -> 2, 16 -> 3.
+ *
+ * NOTE: burst size 2 is not supported by controller.
+ *
+ * This can be done by finding least significant bit set: n & (n - 1)
+ */
+static inline void convert_burst(u32 *maxburst)
+{
+       if (*maxburst > 1)
+               *maxburst = fls(*maxburst) - 2;
+       else
+               *maxburst = 0;
+}
+
+static int
+set_runtime_config(struct dma_chan *chan, struct dma_slave_config *sconfig)
+{
+       struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
+
+       /* Check if it is chan is configured for slave transfers */
+       if (!chan->private)
+               return -EINVAL;
+
+       memcpy(&dwc->dma_sconfig, sconfig, sizeof(*sconfig));
+
+       convert_burst(&dwc->dma_sconfig.src_maxburst);
+       convert_burst(&dwc->dma_sconfig.dst_maxburst);
+
+       return 0;
+}
+
 static int dwc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
                       unsigned long arg)
 {
@@ -933,8 +964,11 @@ static int dwc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
                /* Flush all pending and queued descriptors */
                list_for_each_entry_safe(desc, _desc, &list, desc_node)
                        dwc_descriptor_complete(dwc, desc, false);
-       } else
+       } else if (cmd == DMA_SLAVE_CONFIG) {
+               return set_runtime_config(chan, (struct dma_slave_config *)arg);
+       } else {
                return -ENXIO;
+       }
 
        return 0;
 }
@@ -945,28 +979,17 @@ dwc_tx_status(struct dma_chan *chan,
              struct dma_tx_state *txstate)
 {
        struct dw_dma_chan      *dwc = to_dw_dma_chan(chan);
-       dma_cookie_t            last_used;
-       dma_cookie_t            last_complete;
-       int                     ret;
+       enum dma_status         ret;
 
-       last_complete = dwc->completed;
-       last_used = chan->cookie;
-
-       ret = dma_async_is_complete(cookie, last_complete, last_used);
+       ret = dma_cookie_status(chan, cookie, txstate);
        if (ret != DMA_SUCCESS) {
                dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
 
-               last_complete = dwc->completed;
-               last_used = chan->cookie;
-
-               ret = dma_async_is_complete(cookie, last_complete, last_used);
+               ret = dma_cookie_status(chan, cookie, txstate);
        }
 
        if (ret != DMA_SUCCESS)
-               dma_set_tx_state(txstate, last_complete, last_used,
-                               dwc_first_active(dwc)->len);
-       else
-               dma_set_tx_state(txstate, last_complete, last_used, 0);
+               dma_set_residue(txstate, dwc_first_active(dwc)->len);
 
        if (dwc->paused)
                return DMA_PAUSED;
@@ -998,7 +1021,7 @@ static int dwc_alloc_chan_resources(struct dma_chan *chan)
                return -EIO;
        }
 
-       dwc->completed = chan->cookie = 1;
+       dma_cookie_init(chan);
 
        /*
         * NOTE: some controllers may have additional features that we
@@ -1167,11 +1190,11 @@ struct dw_cyclic_desc *dw_dma_cyclic_prep(struct dma_chan *chan,
                enum dma_transfer_direction direction)
 {
        struct dw_dma_chan              *dwc = to_dw_dma_chan(chan);
+       struct dma_slave_config         *sconfig = &dwc->dma_sconfig;
        struct dw_cyclic_desc           *cdesc;
        struct dw_cyclic_desc           *retval = NULL;
        struct dw_desc                  *desc;
        struct dw_desc                  *last = NULL;
-       struct dw_dma_slave             *dws = chan->private;
        unsigned long                   was_cyclic;
        unsigned int                    reg_width;
        unsigned int                    periods;
@@ -1195,7 +1218,12 @@ struct dw_cyclic_desc *dw_dma_cyclic_prep(struct dma_chan *chan,
        }
 
        retval = ERR_PTR(-EINVAL);
-       reg_width = dws->reg_width;
+
+       if (direction == DMA_MEM_TO_DEV)
+               reg_width = __ffs(sconfig->dst_addr_width);
+       else
+               reg_width = __ffs(sconfig->src_addr_width);
+
        periods = buf_len / period_len;
 
        /* Check for too big/unaligned periods and unaligned DMA buffer. */
@@ -1228,26 +1256,34 @@ struct dw_cyclic_desc *dw_dma_cyclic_prep(struct dma_chan *chan,
 
                switch (direction) {
                case DMA_MEM_TO_DEV:
-                       desc->lli.dar = dws->tx_reg;
+                       desc->lli.dar = sconfig->dst_addr;
                        desc->lli.sar = buf_addr + (period_len * i);
-                       desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan->private)
+                       desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan)
                                        | DWC_CTLL_DST_WIDTH(reg_width)
                                        | DWC_CTLL_SRC_WIDTH(reg_width)
                                        | DWC_CTLL_DST_FIX
                                        | DWC_CTLL_SRC_INC
-                                       | DWC_CTLL_FC(dws->fc)
                                        | DWC_CTLL_INT_EN);
+
+                       desc->lli.ctllo |= sconfig->device_fc ?
+                               DWC_CTLL_FC(DW_DMA_FC_P_M2P) :
+                               DWC_CTLL_FC(DW_DMA_FC_D_M2P);
+
                        break;
                case DMA_DEV_TO_MEM:
                        desc->lli.dar = buf_addr + (period_len * i);
-                       desc->lli.sar = dws->rx_reg;
-                       desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan->private)
+                       desc->lli.sar = sconfig->src_addr;
+                       desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan)
                                        | DWC_CTLL_SRC_WIDTH(reg_width)
                                        | DWC_CTLL_DST_WIDTH(reg_width)
                                        | DWC_CTLL_DST_INC
                                        | DWC_CTLL_SRC_FIX
-                                       | DWC_CTLL_FC(dws->fc)
                                        | DWC_CTLL_INT_EN);
+
+                       desc->lli.ctllo |= sconfig->device_fc ?
+                               DWC_CTLL_FC(DW_DMA_FC_P_P2M) :
+                               DWC_CTLL_FC(DW_DMA_FC_D_P2M);
+
                        break;
                default:
                        break;
@@ -1413,7 +1449,7 @@ static int __init dw_probe(struct platform_device *pdev)
                struct dw_dma_chan      *dwc = &dw->chan[i];
 
                dwc->chan.device = &dw->dma;
-               dwc->chan.cookie = dwc->completed = 1;
+               dma_cookie_init(&dwc->chan);
                if (pdata->chan_allocation_order == CHAN_ALLOCATION_ASCENDING)
                        list_add_tail(&dwc->chan.device_node,
                                        &dw->dma.channels);