mmc: core: use default generic cmd6 timeout for flushing cache
[firefly-linux-kernel-4.4.55.git] / drivers / dma / pl330.c
index 17ee758b419ffbf6e10722fbcac95a08a96e4892..5e0c2607176fab6bcf19686b06dc57a35a66f2ba 100644 (file)
@@ -33,6 +33,9 @@
 #define PL330_MAX_CHAN         8
 #define PL330_MAX_IRQS         32
 #define PL330_MAX_PERI         32
+#define PL330_MAX_BURST         16
+
+#define PL330_QUIRK_BROKEN_NO_FLUSHP BIT(0)
 
 enum pl330_cachectrl {
        CCTRL0,         /* Noncacheable and nonbufferable */
@@ -237,6 +240,7 @@ enum pl330_byteswap {
 
 #define BYTE_TO_BURST(b, ccr)  ((b) / BRST_SIZE(ccr) / BRST_LEN(ccr))
 #define BURST_TO_BYTE(c, ccr)  ((c) * BRST_SIZE(ccr) * BRST_LEN(ccr))
+#define BYTE_MOD_BURST_LEN(b, ccr)     (((b) / BRST_SIZE(ccr)) % BRST_LEN(ccr))
 
 /*
  * With 256 bytes, we can do more than 2.5MB and 5MB xfers per req
@@ -445,6 +449,9 @@ struct dma_pl330_chan {
 
        /* for cyclic capability */
        bool cyclic;
+
+       /* for runtime pm tracking */
+       bool active;
 };
 
 struct pl330_dmac {
@@ -488,6 +495,19 @@ struct pl330_dmac {
        /* Peripheral channels connected to this DMAC */
        unsigned int num_peripherals;
        struct dma_pl330_chan *peripherals; /* keep at end */
+       /* set peripherals request type according to soc config*/
+       enum pl330_cond peripherals_req_type;
+       int quirks;
+};
+
+static struct pl330_of_quirks {
+       char *quirk;
+       int id;
+} of_quirks[] = {
+       {
+               .quirk = "arm,pl330-broken-no-flushp",
+               .id = PL330_QUIRK_BROKEN_NO_FLUSHP,
+       }
 };
 
 struct dma_pl330_desc {
@@ -1137,47 +1157,57 @@ static inline int _ldst_memtomem(unsigned dry_run, u8 buf[],
        return off;
 }
 
-static inline int _ldst_devtomem(unsigned dry_run, u8 buf[],
-               const struct _xfer_spec *pxs, int cyc)
+static inline int _ldst_devtomem(struct pl330_dmac *pl330, unsigned dry_run,
+                                u8 buf[], const struct _xfer_spec *pxs,
+                                int cyc)
 {
        int off = 0;
+       enum pl330_cond cond = pl330->peripherals_req_type;
 
        while (cyc--) {
-               off += _emit_WFP(dry_run, &buf[off], SINGLE, pxs->desc->peri);
-               off += _emit_LDP(dry_run, &buf[off], SINGLE, pxs->desc->peri);
+               off += _emit_WFP(dry_run, &buf[off], cond, pxs->desc->peri);
+               off += _emit_LDP(dry_run, &buf[off], cond, pxs->desc->peri);
                off += _emit_ST(dry_run, &buf[off], ALWAYS);
-               off += _emit_FLUSHP(dry_run, &buf[off], pxs->desc->peri);
+
+               if (!(pl330->quirks & PL330_QUIRK_BROKEN_NO_FLUSHP))
+                       off += _emit_FLUSHP(dry_run, &buf[off],
+                                           pxs->desc->peri);
        }
 
        return off;
 }
 
-static inline int _ldst_memtodev(unsigned dry_run, u8 buf[],
-               const struct _xfer_spec *pxs, int cyc)
+static inline int _ldst_memtodev(struct pl330_dmac *pl330,
+                                unsigned dry_run, u8 buf[],
+                                const struct _xfer_spec *pxs, int cyc)
 {
        int off = 0;
+       enum pl330_cond cond = pl330->peripherals_req_type;
 
        while (cyc--) {
-               off += _emit_WFP(dry_run, &buf[off], SINGLE, pxs->desc->peri);
+               off += _emit_WFP(dry_run, &buf[off], cond, pxs->desc->peri);
                off += _emit_LD(dry_run, &buf[off], ALWAYS);
-               off += _emit_STP(dry_run, &buf[off], SINGLE, pxs->desc->peri);
-               off += _emit_FLUSHP(dry_run, &buf[off], pxs->desc->peri);
+               off += _emit_STP(dry_run, &buf[off], cond, pxs->desc->peri);
+
+               if (!(pl330->quirks & PL330_QUIRK_BROKEN_NO_FLUSHP))
+                       off += _emit_FLUSHP(dry_run, &buf[off],
+                                           pxs->desc->peri);
        }
 
        return off;
 }
 
-static int _bursts(unsigned dry_run, u8 buf[],
+static int _bursts(struct pl330_dmac *pl330, unsigned dry_run, u8 buf[],
                const struct _xfer_spec *pxs, int cyc)
 {
        int off = 0;
 
        switch (pxs->desc->rqtype) {
        case DMA_MEM_TO_DEV:
-               off += _ldst_memtodev(dry_run, &buf[off], pxs, cyc);
+               off += _ldst_memtodev(pl330, dry_run, &buf[off], pxs, cyc);
                break;
        case DMA_DEV_TO_MEM:
-               off += _ldst_devtomem(dry_run, &buf[off], pxs, cyc);
+               off += _ldst_devtomem(pl330, dry_run, &buf[off], pxs, cyc);
                break;
        case DMA_MEM_TO_MEM:
                off += _ldst_memtomem(dry_run, &buf[off], pxs, cyc);
@@ -1191,7 +1221,7 @@ static int _bursts(unsigned dry_run, u8 buf[],
 }
 
 /* Returns bytes consumed and updates bursts */
-static inline int _loop(unsigned dry_run, u8 buf[],
+static inline int _loop(struct pl330_dmac *pl330, unsigned dry_run, u8 buf[],
                unsigned long *bursts, const struct _xfer_spec *pxs)
 {
        int cyc, cycmax, szlp, szlpend, szbrst, off;
@@ -1199,7 +1229,7 @@ static inline int _loop(unsigned dry_run, u8 buf[],
        struct _arg_LPEND lpend;
 
        if (*bursts == 1)
-               return _bursts(dry_run, buf, pxs, 1);
+               return _bursts(pl330, dry_run, buf, pxs, 1);
 
        /* Max iterations possible in DMALP is 256 */
        if (*bursts >= 256*256) {
@@ -1217,7 +1247,7 @@ static inline int _loop(unsigned dry_run, u8 buf[],
        }
 
        szlp = _emit_LP(1, buf, 0, 0);
-       szbrst = _bursts(1, buf, pxs, 1);
+       szbrst = _bursts(pl330, 1, buf, pxs, 1);
 
        lpend.cond = ALWAYS;
        lpend.forever = false;
@@ -1249,7 +1279,7 @@ static inline int _loop(unsigned dry_run, u8 buf[],
        off += _emit_LP(dry_run, &buf[off], 1, lcnt1);
        ljmp1 = off;
 
-       off += _bursts(dry_run, &buf[off], pxs, cyc);
+       off += _bursts(pl330, dry_run, &buf[off], pxs, cyc);
 
        lpend.cond = ALWAYS;
        lpend.forever = false;
@@ -1272,8 +1302,9 @@ static inline int _loop(unsigned dry_run, u8 buf[],
        return off;
 }
 
-static inline int _setup_loops(unsigned dry_run, u8 buf[],
-               const struct _xfer_spec *pxs)
+static inline int _setup_loops(struct pl330_dmac *pl330,
+                              unsigned dry_run, u8 buf[],
+                              const struct _xfer_spec *pxs)
 {
        struct pl330_xfer *x = &pxs->desc->px;
        u32 ccr = pxs->ccr;
@@ -1282,15 +1313,16 @@ static inline int _setup_loops(unsigned dry_run, u8 buf[],
 
        while (bursts) {
                c = bursts;
-               off += _loop(dry_run, &buf[off], &c, pxs);
+               off += _loop(pl330, dry_run, &buf[off], &c, pxs);
                bursts -= c;
        }
 
        return off;
 }
 
-static inline int _setup_xfer(unsigned dry_run, u8 buf[],
-               const struct _xfer_spec *pxs)
+static inline int _setup_xfer(struct pl330_dmac *pl330,
+                             unsigned dry_run, u8 buf[],
+                             const struct _xfer_spec *pxs)
 {
        struct pl330_xfer *x = &pxs->desc->px;
        int off = 0;
@@ -1301,7 +1333,21 @@ static inline int _setup_xfer(unsigned dry_run, u8 buf[],
        off += _emit_MOV(dry_run, &buf[off], DAR, x->dst_addr);
 
        /* Setup Loop(s) */
-       off += _setup_loops(dry_run, &buf[off], pxs);
+       off += _setup_loops(pl330, dry_run, &buf[off], pxs);
+
+       if (pl330->peripherals_req_type == BURST) {
+               unsigned int ccr = pxs->ccr;
+               unsigned long c = 0;
+
+               c = BYTE_MOD_BURST_LEN(x->bytes, pxs->ccr);
+
+               if (c) {
+                       ccr &= ~(0xf << CC_SRCBRSTLEN_SHFT);
+                       ccr &= ~(0xf << CC_DSTBRSTLEN_SHFT);
+                       off += _emit_MOV(dry_run, &buf[off], CCR, ccr);
+                       off += _loop(pl330, dry_run, &buf[off], &c, pxs);
+               }
+       }
 
        return off;
 }
@@ -1310,8 +1356,9 @@ static inline int _setup_xfer(unsigned dry_run, u8 buf[],
  * A req is a sequence of one or more xfer units.
  * Returns the number of bytes taken to setup the MC for the req.
  */
-static int _setup_req(unsigned dry_run, struct pl330_thread *thrd,
-               unsigned index, struct _xfer_spec *pxs)
+static int _setup_req(struct pl330_dmac *pl330, unsigned dry_run,
+                     struct pl330_thread *thrd, unsigned index,
+                     struct _xfer_spec *pxs)
 {
        struct _pl330_req *req = &thrd->req[index];
        struct pl330_xfer *x;
@@ -1324,11 +1371,13 @@ static int _setup_req(unsigned dry_run, struct pl330_thread *thrd,
        off += _emit_MOV(dry_run, &buf[off], CCR, pxs->ccr);
 
        x = &pxs->desc->px;
-       /* Error if xfer length is not aligned at burst size */
-       if (x->bytes % (BRST_SIZE(pxs->ccr) * BRST_LEN(pxs->ccr)))
-               return -EINVAL;
+       if (pl330->peripherals_req_type != BURST) {
+               /* Error if xfer length is not aligned at burst size */
+               if (x->bytes % (BRST_SIZE(pxs->ccr) * BRST_LEN(pxs->ccr)))
+                       return -EINVAL;
+       }
 
-       off += _setup_xfer(dry_run, &buf[off], pxs);
+       off += _setup_xfer(pl330, dry_run, &buf[off], pxs);
 
        /* DMASEV peripheral/event */
        off += _emit_SEV(dry_run, &buf[off], thrd->ev);
@@ -1422,7 +1471,7 @@ static int pl330_submit_req(struct pl330_thread *thrd,
        xs.desc = desc;
 
        /* First dry run to check if req is acceptable */
-       ret = _setup_req(1, thrd, idx, &xs);
+       ret = _setup_req(pl330, 1, thrd, idx, &xs);
        if (ret < 0)
                goto xfer_exit;
 
@@ -1436,7 +1485,7 @@ static int pl330_submit_req(struct pl330_thread *thrd,
        /* Hook the request */
        thrd->lstenq = idx;
        thrd->req[idx].desc = desc;
-       _setup_req(0, thrd, idx, &xs);
+       _setup_req(pl330, 0, thrd, idx, &xs);
 
        ret = 0;
 
@@ -1994,6 +2043,7 @@ static void pl330_tasklet(unsigned long data)
                _stop(pch->thread);
                spin_unlock(&pch->thread->dmac->lock);
                power_down = true;
+               pch->active = false;
        } else {
                /* Make sure the PL330 Channel thread is active */
                spin_lock(&pch->thread->dmac->lock);
@@ -2015,6 +2065,7 @@ static void pl330_tasklet(unsigned long data)
                        desc->status = PREP;
                        list_move_tail(&desc->node, &pch->work_list);
                        if (power_down) {
+                               pch->active = true;
                                spin_lock(&pch->thread->dmac->lock);
                                _start(pch->thread);
                                spin_unlock(&pch->thread->dmac->lock);
@@ -2129,6 +2180,7 @@ static int pl330_terminate_all(struct dma_chan *chan)
        unsigned long flags;
        struct pl330_dmac *pl330 = pch->dmac;
        LIST_HEAD(list);
+       bool power_down = false;
 
        pm_runtime_get_sync(pl330->ddma.dev);
        spin_lock_irqsave(&pch->lock, flags);
@@ -2139,6 +2191,8 @@ static int pl330_terminate_all(struct dma_chan *chan)
        pch->thread->req[0].desc = NULL;
        pch->thread->req[1].desc = NULL;
        pch->thread->req_running = -1;
+       power_down = pch->active;
+       pch->active = false;
 
        /* Mark all desc done */
        list_for_each_entry(desc, &pch->submitted_list, node) {
@@ -2156,6 +2210,8 @@ static int pl330_terminate_all(struct dma_chan *chan)
        list_splice_tail_init(&pch->completed_list, &pl330->desc_pool);
        spin_unlock_irqrestore(&pch->lock, flags);
        pm_runtime_mark_last_busy(pl330->ddma.dev);
+       if (power_down)
+               pm_runtime_put_autosuspend(pl330->ddma.dev);
        pm_runtime_put_autosuspend(pl330->ddma.dev);
 
        return 0;
@@ -2302,6 +2358,7 @@ static void pl330_issue_pending(struct dma_chan *chan)
                 * updated on work_list emptiness status.
                 */
                WARN_ON(list_empty(&pch->submitted_list));
+               pch->active = true;
                pm_runtime_get_sync(pch->dmac->ddma.dev);
        }
        list_splice_tail_init(&pch->submitted_list, &pch->work_list);
@@ -2560,7 +2617,12 @@ static struct dma_async_tx_descriptor *pl330_prep_dma_cyclic(
 
                desc->rqtype = direction;
                desc->rqcfg.brst_size = pch->burst_sz;
-               desc->rqcfg.brst_len = 1;
+
+               if (pl330->peripherals_req_type == BURST)
+                       desc->rqcfg.brst_len = pch->burst_len;
+               else
+                       desc->rqcfg.brst_len = 1;
+
                desc->bytes_requested = period_len;
                fill_px(&desc->px, dst, src, period_len);
 
@@ -2662,6 +2724,7 @@ pl330_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
 {
        struct dma_pl330_desc *first, *desc = NULL;
        struct dma_pl330_chan *pch = to_pchan(chan);
+       struct pl330_dmac *pl330 = pch->dmac;
        struct scatterlist *sg;
        int i;
        dma_addr_t addr;
@@ -2705,7 +2768,12 @@ pl330_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
                }
 
                desc->rqcfg.brst_size = pch->burst_sz;
-               desc->rqcfg.brst_len = 1;
+
+               if (pl330->peripherals_req_type == BURST)
+                       desc->rqcfg.brst_len = pch->burst_len;
+               else
+                       desc->rqcfg.brst_len = 1;
+
                desc->rqtype = direction;
                desc->bytes_requested = sg_dma_len(sg);
        }
@@ -2781,6 +2849,7 @@ pl330_probe(struct amba_device *adev, const struct amba_id *id)
        struct resource *res;
        int i, ret, irq;
        int num_chan;
+       struct device_node *np = adev->dev.of_node;
 
        pdat = dev_get_platdata(&adev->dev);
 
@@ -2800,6 +2869,16 @@ pl330_probe(struct amba_device *adev, const struct amba_id *id)
 
        pl330->mcbufsz = pdat ? pdat->mcbuf_sz : 0;
 
+       if (of_find_property(np, "peripherals-req-type-burst", NULL))
+               pl330->peripherals_req_type = BURST;
+       else
+               pl330->peripherals_req_type = SINGLE;
+
+       /* get quirk */
+       for (i = 0; i < ARRAY_SIZE(of_quirks); i++)
+               if (of_property_read_bool(np, of_quirks[i].quirk))
+                       pl330->quirks |= of_quirks[i].id;
+
        res = &adev->res;
        pl330->base = devm_ioremap_resource(&adev->dev, res);
        if (IS_ERR(pl330->base))
@@ -2895,6 +2974,8 @@ pl330_probe(struct amba_device *adev, const struct amba_id *id)
        pd->dst_addr_widths = PL330_DMA_BUSWIDTHS;
        pd->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
        pd->residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
+       pd->max_burst = ((pl330->quirks & PL330_QUIRK_BROKEN_NO_FLUSHP) ?
+                        1 : PL330_MAX_BURST);
 
        ret = dma_async_device_register(pd);
        if (ret) {