1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2006 ARM Ltd.
4 * Copyright (c) 2010 ST-Ericsson SA
5 * Copyirght (c) 2017 Linaro Ltd.
7 * Author: Peter Pearse <peter.pearse@arm.com>
8 * Author: Linus Walleij <linus.walleij@linaro.org>
10 * Documentation: ARM DDI 0196G == PL080
11 * Documentation: ARM DDI 0218E == PL081
12 * Documentation: S3C6410 User's Manual == PL080S
14 * PL080 & PL081 both have 16 sets of DMA signals that can be routed to any
17 * The PL080 has 8 channels available for simultaneous use, and the PL081
18 * has only two channels. So on these DMA controllers the number of channels
19 * and the number of incoming DMA signals are two totally different things.
20 * It is usually not possible to theoretically handle all physical signals,
21 * so a multiplexing scheme with possible denial of use is necessary.
23 * The PL080 has a dual bus master, PL081 has a single master.
25 * PL080S is a version modified by Samsung and used in S3C64xx SoCs.
26 * It differs in following aspects:
27 * - CH_CONFIG register at different offset,
28 * - separate CH_CONTROL2 register for transfer size,
29 * - bigger maximum transfer size,
30 * - 8-word aligned LLI, instead of 4-word, due to extra CCTL2 word,
31 * - no support for peripheral flow control.
33 * Memory to peripheral transfer may be visualized as
34 * Get data from memory to DMAC
36 * On burst request from peripheral
37 * Destination burst from DMAC to peripheral
39 * Raise terminal count interrupt
41 * For peripherals with a FIFO:
42 * Source burst size == half the depth of the peripheral FIFO
43 * Destination burst size == the depth of the peripheral FIFO
45 * (Bursts are irrelevant for mem to mem transfers - there are no burst
46 * signals, the DMA controller will simply facilitate its AHB master.)
48 * ASSUMES default (little) endianness for DMA transfers
50 * The PL08x has two flow control settings:
51 * - DMAC flow control: the transfer size defines the number of transfers
52 * which occur for the current LLI entry, and the DMAC raises TC at the
53 * end of every LLI entry. Observed behaviour shows the DMAC listening
54 * to both the BREQ and SREQ signals (contrary to documented),
55 * transferring data if either is active. The LBREQ and LSREQ signals
58 * - Peripheral flow control: the transfer size is ignored (and should be
59 * zero). The data is transferred from the current LLI entry, until
60 * after the final transfer signalled by LBREQ or LSREQ. The DMAC
61 * will then move to the next LLI entry. Unsupported by PL080S.
63 #ifndef CONFIG_SOC_STARFIVE_JH7110
64 #include <linux/amba/bus.h>
66 #include <linux/amba/pl08x.h>
67 #include <linux/debugfs.h>
68 #include <linux/delay.h>
69 #include <linux/device.h>
70 #include <linux/dmaengine.h>
71 #include <linux/dmapool.h>
72 #include <linux/dma-mapping.h>
73 #include <linux/export.h>
74 #include <linux/init.h>
75 #include <linux/interrupt.h>
76 #include <linux/module.h>
78 #include <linux/of_dma.h>
79 #ifdef CONFIG_SOC_STARFIVE_JH7110
80 #include <linux/of_device.h>
81 #include <linux/platform_device.h>
83 #include <linux/pm_runtime.h>
84 #include <linux/seq_file.h>
85 #include <linux/slab.h>
86 #include <linux/amba/pl080.h>
88 #include "dmaengine.h"
91 #define DRIVER_NAME "pl08xdmac"
93 #define PL80X_DMA_BUSWIDTHS \
94 BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
95 BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
96 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
97 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES)
99 #ifndef CONFIG_SOC_STARFIVE_JH7110
100 static struct amba_driver pl08x_amba_driver;
102 struct pl08x_driver_data;
105 * struct vendor_data - vendor-specific config parameters for PL08x derivatives
106 * @config_offset: offset to the configuration register
107 * @channels: the number of channels available in this variant
108 * @signals: the number of request signals available from the hardware
109 * @dualmaster: whether this version supports dual AHB masters or not.
110 * @nomadik: whether this variant is a ST Microelectronics Nomadik, where the
111 * channels have Nomadik security extension bits that need to be checked
112 * for permission before use and some registers are missing
113 * @pl080s: whether this variant is a Samsung PL080S, which has separate
114 * register and LLI word for transfer size.
115 * @ftdmac020: whether this variant is a Faraday Technology FTDMAC020
116 * @max_transfer_size: the maximum single element transfer size for this
127 u32 max_transfer_size;
131 * struct pl08x_bus_data - information of source or destination
132 * busses for a transfer
133 * @addr: current address
134 * @maxwidth: the maximum width of a transfer on this bus
135 * @buswidth: the width of this bus in bytes: 1, 2 or 4
137 struct pl08x_bus_data {
143 #define IS_BUS_ALIGNED(bus) IS_ALIGNED((bus)->addr, (bus)->buswidth)
146 * struct pl08x_phy_chan - holder for the physical channels
147 * @id: physical index to this channel
148 * @base: memory base address for this physical channel
149 * @reg_config: configuration address for this physical channel
150 * @reg_control: control address for this physical channel
151 * @reg_src: transfer source address register
152 * @reg_dst: transfer destination address register
153 * @reg_lli: transfer LLI address register
154 * @reg_busy: if the variant has a special per-channel busy register,
155 * this contains a pointer to it
156 * @lock: a lock to use when altering an instance of this struct
157 * @serving: the virtual channel currently being served by this physical
159 * @locked: channel unavailable for the system, e.g. dedicated to secure
161 * @ftdmac020: channel is on a FTDMAC020
162 * @pl080s: channel is on a PL08s
164 struct pl08x_phy_chan {
167 void __iomem *reg_config;
168 void __iomem *reg_control;
169 void __iomem *reg_src;
170 void __iomem *reg_dst;
171 void __iomem *reg_lli;
172 void __iomem *reg_busy;
174 struct pl08x_dma_chan *serving;
181 * struct pl08x_sg - structure containing data per sg
182 * @src_addr: src address of sg
183 * @dst_addr: dst address of sg
184 * @len: transfer len in bytes
185 * @node: node for txd's dsg_list
191 struct list_head node;
195 * struct pl08x_txd - wrapper for struct dma_async_tx_descriptor
196 * @vd: virtual DMA descriptor
197 * @dsg_list: list of children sg's
198 * @llis_bus: DMA memory address (physical) start for the LLIs
199 * @llis_va: virtual memory address start for the LLIs
200 * @cctl: control reg values for current txd
201 * @ccfg: config reg values for current txd
202 * @done: this marks completed descriptors, which should not have their
204 * @cyclic: indicate cyclic transfers
207 struct virt_dma_desc vd;
208 struct list_head dsg_list;
211 /* Default cctl value for LLIs */
214 * Settings to be put into the physical channel when we
215 * trigger this txd. Other registers are in llis_va[0].
223 * enum pl08x_dma_chan_state - holds the PL08x specific virtual channel
225 * @PL08X_CHAN_IDLE: the channel is idle
226 * @PL08X_CHAN_RUNNING: the channel has allocated a physical transport
227 * channel and is running a transfer on it
228 * @PL08X_CHAN_PAUSED: the channel has allocated a physical transport
229 * channel, but the transfer is currently paused
230 * @PL08X_CHAN_WAITING: the channel is waiting for a physical transport
231 * channel to become available (only pertains to memcpy channels)
233 enum pl08x_dma_chan_state {
241 * struct pl08x_dma_chan - this structure wraps a DMA ENGINE channel
242 * @vc: wrappped virtual channel
243 * @phychan: the physical channel utilized by this channel, if there is one
244 * @name: name of channel
245 * @cd: channel platform data
246 * @cfg: slave configuration
247 * @at: active transaction on this channel
248 * @host: a pointer to the host (internal use)
249 * @state: whether the channel is idle, paused, running etc
250 * @slave: whether this channel is a device (slave) or for memcpy
251 * @signal: the physical DMA request signal which this channel is using
252 * @mux_use: count of descriptors using this DMA request signal setting
253 * @waiting_at: time in jiffies when this channel moved to waiting state
255 struct pl08x_dma_chan {
256 struct virt_dma_chan vc;
257 struct pl08x_phy_chan *phychan;
259 struct pl08x_channel_data *cd;
260 struct dma_slave_config cfg;
261 struct pl08x_txd *at;
262 struct pl08x_driver_data *host;
263 enum pl08x_dma_chan_state state;
264 #ifdef CONFIG_SOC_STARFIVE_JH7110
270 unsigned long waiting_at;
274 * struct pl08x_driver_data - the local state holder for the PL08x
275 * @slave: optional slave engine for this instance
276 * @memcpy: memcpy engine for this instance
277 * @has_slave: the PL08x has a slave engine (routed signals)
278 * @base: virtual memory base (remapped) for the PL08x
279 * @adev: the corresponding AMBA (PrimeCell) bus entry
280 * @vd: vendor data for this PL08x variant
281 * @pd: platform data passed in from the platform/machine
282 * @phy_chans: array of data for the physical channels
283 * @pool: a pool for the LLI descriptors
284 * @lli_buses: bitmask to or in to LLI pointer selecting AHB port for LLI
286 * @mem_buses: set to indicate memory transfers on AHB2.
287 * @lli_words: how many words are used in each LLI item for this variant
289 struct pl08x_driver_data {
290 struct dma_device slave;
291 struct dma_device memcpy;
294 #ifdef CONFIG_SOC_STARFIVE_JH7110
295 struct platform_device *adev;
297 struct amba_device *adev;
299 const struct vendor_data *vd;
300 struct pl08x_platform_data *pd;
301 struct pl08x_phy_chan *phy_chans;
302 struct dma_pool *pool;
309 * PL08X specific defines
312 /* The order of words in an LLI. */
313 #define PL080_LLI_SRC 0
314 #define PL080_LLI_DST 1
315 #define PL080_LLI_LLI 2
316 #define PL080_LLI_CCTL 3
317 #define PL080S_LLI_CCTL2 4
319 /* Total words in an LLI. */
320 #define PL080_LLI_WORDS 4
321 #define PL080S_LLI_WORDS 8
324 * Number of LLIs in each LLI buffer allocated for one transfer
325 * (maximum times we call dma_pool_alloc on this pool without freeing)
327 #define MAX_NUM_TSFR_LLIS 512
328 #define PL08X_ALIGN 8
330 static inline struct pl08x_dma_chan *to_pl08x_chan(struct dma_chan *chan)
332 return container_of(chan, struct pl08x_dma_chan, vc.chan);
335 static inline struct pl08x_txd *to_pl08x_txd(struct dma_async_tx_descriptor *tx)
337 return container_of(tx, struct pl08x_txd, vd.tx);
343 * This gives us the DMA request input to the PL08x primecell which the
344 * peripheral described by the channel data will be routed to, possibly
345 * via a board/SoC specific external MUX. One important point to note
346 * here is that this does not depend on the physical channel.
348 static int pl08x_request_mux(struct pl08x_dma_chan *plchan)
350 const struct pl08x_platform_data *pd = plchan->host->pd;
353 if (plchan->mux_use++ == 0 && pd->get_xfer_signal) {
354 ret = pd->get_xfer_signal(plchan->cd);
360 plchan->signal = ret;
365 static void pl08x_release_mux(struct pl08x_dma_chan *plchan)
367 const struct pl08x_platform_data *pd = plchan->host->pd;
369 if (plchan->signal >= 0) {
370 WARN_ON(plchan->mux_use == 0);
372 if (--plchan->mux_use == 0 && pd->put_xfer_signal) {
373 pd->put_xfer_signal(plchan->cd, plchan->signal);
380 * Physical channel handling
383 /* Whether a certain channel is busy or not */
384 static int pl08x_phy_channel_busy(struct pl08x_phy_chan *ch)
388 /* If we have a special busy register, take a shortcut */
390 val = readl(ch->reg_busy);
391 return !!(val & BIT(ch->id));
393 val = readl(ch->reg_config);
394 return val & PL080_CONFIG_ACTIVE;
398 * pl08x_write_lli() - Write an LLI into the DMA controller.
400 * The PL08x derivatives support linked lists, but the first item of the
401 * list containing the source, destination, control word and next LLI is
402 * ignored. Instead the driver has to write those values directly into the
403 * SRC, DST, LLI and control registers. On FTDMAC020 also the SIZE
404 * register need to be set up for the first transfer.
406 static void pl08x_write_lli(struct pl08x_driver_data *pl08x,
407 struct pl08x_phy_chan *phychan, const u32 *lli, u32 ccfg)
409 if (pl08x->vd->pl080s)
410 dev_vdbg(&pl08x->adev->dev,
411 "WRITE channel %d: csrc=0x%08x, cdst=0x%08x, "
412 "clli=0x%08x, cctl=0x%08x, cctl2=0x%08x, ccfg=0x%08x\n",
413 phychan->id, lli[PL080_LLI_SRC], lli[PL080_LLI_DST],
414 lli[PL080_LLI_LLI], lli[PL080_LLI_CCTL],
415 lli[PL080S_LLI_CCTL2], ccfg);
417 dev_vdbg(&pl08x->adev->dev,
418 "WRITE channel %d: csrc=0x%08x, cdst=0x%08x, "
419 "clli=0x%08x, cctl=0x%08x, ccfg=0x%08x\n",
420 phychan->id, lli[PL080_LLI_SRC], lli[PL080_LLI_DST],
421 lli[PL080_LLI_LLI], lli[PL080_LLI_CCTL], ccfg);
423 writel_relaxed(lli[PL080_LLI_SRC], phychan->reg_src);
424 writel_relaxed(lli[PL080_LLI_DST], phychan->reg_dst);
425 writel_relaxed(lli[PL080_LLI_LLI], phychan->reg_lli);
428 * The FTMAC020 has a different layout in the CCTL word of the LLI
429 * and the CCTL register which is split in CSR and SIZE registers.
430 * Convert the LLI item CCTL into the proper values to write into
431 * the CSR and SIZE registers.
433 if (phychan->ftdmac020) {
434 u32 llictl = lli[PL080_LLI_CCTL];
437 /* Write the transfer size (12 bits) to the size register */
438 writel_relaxed(llictl & FTDMAC020_LLI_TRANSFER_SIZE_MASK,
439 phychan->base + FTDMAC020_CH_SIZE);
441 * Then write the control bits 28..16 to the control register
442 * by shuffleing the bits around to where they are in the
443 * main register. The mapping is as follows:
444 * Bit 28: TC_MSK - mask on all except last LLI
445 * Bit 27..25: SRC_WIDTH
446 * Bit 24..22: DST_WIDTH
447 * Bit 21..20: SRCAD_CTRL
448 * Bit 19..17: DSTAD_CTRL
452 if (llictl & FTDMAC020_LLI_TC_MSK)
453 val |= FTDMAC020_CH_CSR_TC_MSK;
454 val |= ((llictl & FTDMAC020_LLI_SRC_WIDTH_MSK) >>
455 (FTDMAC020_LLI_SRC_WIDTH_SHIFT -
456 FTDMAC020_CH_CSR_SRC_WIDTH_SHIFT));
457 val |= ((llictl & FTDMAC020_LLI_DST_WIDTH_MSK) >>
458 (FTDMAC020_LLI_DST_WIDTH_SHIFT -
459 FTDMAC020_CH_CSR_DST_WIDTH_SHIFT));
460 val |= ((llictl & FTDMAC020_LLI_SRCAD_CTL_MSK) >>
461 (FTDMAC020_LLI_SRCAD_CTL_SHIFT -
462 FTDMAC020_CH_CSR_SRCAD_CTL_SHIFT));
463 val |= ((llictl & FTDMAC020_LLI_DSTAD_CTL_MSK) >>
464 (FTDMAC020_LLI_DSTAD_CTL_SHIFT -
465 FTDMAC020_CH_CSR_DSTAD_CTL_SHIFT));
466 if (llictl & FTDMAC020_LLI_SRC_SEL)
467 val |= FTDMAC020_CH_CSR_SRC_SEL;
468 if (llictl & FTDMAC020_LLI_DST_SEL)
469 val |= FTDMAC020_CH_CSR_DST_SEL;
472 * Set up the bits that exist in the CSR but are not
473 * part the LLI, i.e. only gets written to the control
474 * register right here.
476 * FIXME: do not just handle memcpy, also handle slave DMA.
478 switch (pl08x->pd->memcpy_burst_size) {
480 case PL08X_BURST_SZ_1:
481 val |= PL080_BSIZE_1 <<
482 FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
484 case PL08X_BURST_SZ_4:
485 val |= PL080_BSIZE_4 <<
486 FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
488 case PL08X_BURST_SZ_8:
489 val |= PL080_BSIZE_8 <<
490 FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
492 case PL08X_BURST_SZ_16:
493 val |= PL080_BSIZE_16 <<
494 FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
496 case PL08X_BURST_SZ_32:
497 val |= PL080_BSIZE_32 <<
498 FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
500 case PL08X_BURST_SZ_64:
501 val |= PL080_BSIZE_64 <<
502 FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
504 case PL08X_BURST_SZ_128:
505 val |= PL080_BSIZE_128 <<
506 FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
508 case PL08X_BURST_SZ_256:
509 val |= PL080_BSIZE_256 <<
510 FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
514 /* Protection flags */
515 if (pl08x->pd->memcpy_prot_buff)
516 val |= FTDMAC020_CH_CSR_PROT2;
517 if (pl08x->pd->memcpy_prot_cache)
518 val |= FTDMAC020_CH_CSR_PROT3;
519 /* We are the kernel, so we are in privileged mode */
520 val |= FTDMAC020_CH_CSR_PROT1;
522 writel_relaxed(val, phychan->reg_control);
524 /* Bits are just identical */
525 writel_relaxed(lli[PL080_LLI_CCTL], phychan->reg_control);
528 /* Second control word on the PL080s */
529 if (pl08x->vd->pl080s)
530 writel_relaxed(lli[PL080S_LLI_CCTL2],
531 phychan->base + PL080S_CH_CONTROL2);
533 writel(ccfg, phychan->reg_config);
537 * Set the initial DMA register values i.e. those for the first LLI
538 * The next LLI pointer and the configuration interrupt bit have
539 * been set when the LLIs were constructed. Poke them into the hardware
540 * and start the transfer.
542 static void pl08x_start_next_txd(struct pl08x_dma_chan *plchan)
544 struct pl08x_driver_data *pl08x = plchan->host;
545 struct pl08x_phy_chan *phychan = plchan->phychan;
546 struct virt_dma_desc *vd = vchan_next_desc(&plchan->vc);
547 struct pl08x_txd *txd = to_pl08x_txd(&vd->tx);
550 list_del(&txd->vd.node);
554 /* Wait for channel inactive */
555 while (pl08x_phy_channel_busy(phychan))
558 pl08x_write_lli(pl08x, phychan, &txd->llis_va[0], txd->ccfg);
560 /* Enable the DMA channel */
561 /* Do not access config register until channel shows as disabled */
562 while (readl(pl08x->base + PL080_EN_CHAN) & BIT(phychan->id))
565 /* Do not access config register until channel shows as inactive */
566 if (phychan->ftdmac020) {
567 val = readl(phychan->reg_config);
568 while (val & FTDMAC020_CH_CFG_BUSY)
569 val = readl(phychan->reg_config);
571 val = readl(phychan->reg_control);
572 while (val & FTDMAC020_CH_CSR_EN)
573 val = readl(phychan->reg_control);
575 writel(val | FTDMAC020_CH_CSR_EN,
576 phychan->reg_control);
578 val = readl(phychan->reg_config);
579 while ((val & PL080_CONFIG_ACTIVE) ||
580 (val & PL080_CONFIG_ENABLE))
581 val = readl(phychan->reg_config);
583 writel(val | PL080_CONFIG_ENABLE, phychan->reg_config);
588 * Pause the channel by setting the HALT bit.
590 * For M->P transfers, pause the DMAC first and then stop the peripheral -
591 * the FIFO can only drain if the peripheral is still requesting data.
592 * (note: this can still timeout if the DMAC FIFO never drains of data.)
594 * For P->M transfers, disable the peripheral first to stop it filling
595 * the DMAC FIFO, and then pause the DMAC.
597 static void pl08x_pause_phy_chan(struct pl08x_phy_chan *ch)
603 /* Use the enable bit on the FTDMAC020 */
604 val = readl(ch->reg_control);
605 val &= ~FTDMAC020_CH_CSR_EN;
606 writel(val, ch->reg_control);
610 /* Set the HALT bit and wait for the FIFO to drain */
611 val = readl(ch->reg_config);
612 val |= PL080_CONFIG_HALT;
613 writel(val, ch->reg_config);
615 /* Wait for channel inactive */
616 for (timeout = 1000; timeout; timeout--) {
617 if (!pl08x_phy_channel_busy(ch))
621 if (pl08x_phy_channel_busy(ch))
622 pr_err("pl08x: channel%u timeout waiting for pause\n", ch->id);
625 static void pl08x_resume_phy_chan(struct pl08x_phy_chan *ch)
629 /* Use the enable bit on the FTDMAC020 */
631 val = readl(ch->reg_control);
632 val |= FTDMAC020_CH_CSR_EN;
633 writel(val, ch->reg_control);
637 /* Clear the HALT bit */
638 val = readl(ch->reg_config);
639 val &= ~PL080_CONFIG_HALT;
640 writel(val, ch->reg_config);
644 * pl08x_terminate_phy_chan() stops the channel, clears the FIFO and
645 * clears any pending interrupt status. This should not be used for
646 * an on-going transfer, but as a method of shutting down a channel
647 * (eg, when it's no longer used) or terminating a transfer.
649 static void pl08x_terminate_phy_chan(struct pl08x_driver_data *pl08x,
650 struct pl08x_phy_chan *ch)
654 /* The layout for the FTDMAC020 is different */
656 /* Disable all interrupts */
657 val = readl(ch->reg_config);
658 val |= (FTDMAC020_CH_CFG_INT_ABT_MASK |
659 FTDMAC020_CH_CFG_INT_ERR_MASK |
660 FTDMAC020_CH_CFG_INT_TC_MASK);
661 writel(val, ch->reg_config);
663 /* Abort and disable channel */
664 val = readl(ch->reg_control);
665 val &= ~FTDMAC020_CH_CSR_EN;
666 val |= FTDMAC020_CH_CSR_ABT;
667 writel(val, ch->reg_control);
669 /* Clear ABT and ERR interrupt flags */
670 writel(BIT(ch->id) | BIT(ch->id + 16),
671 pl08x->base + PL080_ERR_CLEAR);
672 writel(BIT(ch->id), pl08x->base + PL080_TC_CLEAR);
677 val = readl(ch->reg_config);
678 val &= ~(PL080_CONFIG_ENABLE | PL080_CONFIG_ERR_IRQ_MASK |
679 PL080_CONFIG_TC_IRQ_MASK);
680 writel(val, ch->reg_config);
682 writel(BIT(ch->id), pl08x->base + PL080_ERR_CLEAR);
683 writel(BIT(ch->id), pl08x->base + PL080_TC_CLEAR);
686 static u32 get_bytes_in_phy_channel(struct pl08x_phy_chan *ch)
692 bytes = readl(ch->base + FTDMAC020_CH_SIZE);
694 val = readl(ch->reg_control);
695 val &= FTDMAC020_CH_CSR_SRC_WIDTH_MSK;
696 val >>= FTDMAC020_CH_CSR_SRC_WIDTH_SHIFT;
697 } else if (ch->pl080s) {
698 val = readl(ch->base + PL080S_CH_CONTROL2);
699 bytes = val & PL080S_CONTROL_TRANSFER_SIZE_MASK;
701 val = readl(ch->reg_control);
702 val &= PL080_CONTROL_SWIDTH_MASK;
703 val >>= PL080_CONTROL_SWIDTH_SHIFT;
706 val = readl(ch->reg_control);
707 bytes = val & PL080_CONTROL_TRANSFER_SIZE_MASK;
709 val &= PL080_CONTROL_SWIDTH_MASK;
710 val >>= PL080_CONTROL_SWIDTH_SHIFT;
714 case PL080_WIDTH_8BIT:
716 case PL080_WIDTH_16BIT:
719 case PL080_WIDTH_32BIT:
726 static u32 get_bytes_in_lli(struct pl08x_phy_chan *ch, const u32 *llis_va)
732 val = llis_va[PL080_LLI_CCTL];
733 bytes = val & FTDMAC020_LLI_TRANSFER_SIZE_MASK;
735 val = llis_va[PL080_LLI_CCTL];
736 val &= FTDMAC020_LLI_SRC_WIDTH_MSK;
737 val >>= FTDMAC020_LLI_SRC_WIDTH_SHIFT;
738 } else if (ch->pl080s) {
739 val = llis_va[PL080S_LLI_CCTL2];
740 bytes = val & PL080S_CONTROL_TRANSFER_SIZE_MASK;
742 val = llis_va[PL080_LLI_CCTL];
743 val &= PL080_CONTROL_SWIDTH_MASK;
744 val >>= PL080_CONTROL_SWIDTH_SHIFT;
747 val = llis_va[PL080_LLI_CCTL];
748 bytes = val & PL080_CONTROL_TRANSFER_SIZE_MASK;
750 val &= PL080_CONTROL_SWIDTH_MASK;
751 val >>= PL080_CONTROL_SWIDTH_SHIFT;
755 case PL080_WIDTH_8BIT:
757 case PL080_WIDTH_16BIT:
760 case PL080_WIDTH_32BIT:
767 /* The channel should be paused when calling this */
768 static u32 pl08x_getbytes_chan(struct pl08x_dma_chan *plchan)
770 struct pl08x_driver_data *pl08x = plchan->host;
771 const u32 *llis_va, *llis_va_limit;
772 struct pl08x_phy_chan *ch;
774 struct pl08x_txd *txd;
779 ch = plchan->phychan;
786 * Follow the LLIs to get the number of remaining
787 * bytes in the currently active transaction.
789 clli = readl(ch->reg_lli) & ~PL080_LLI_LM_AHB2;
791 /* First get the remaining bytes in the active transfer */
792 bytes = get_bytes_in_phy_channel(ch);
797 llis_va = txd->llis_va;
798 llis_bus = txd->llis_bus;
800 llis_max_words = pl08x->lli_words * MAX_NUM_TSFR_LLIS;
801 BUG_ON(clli < llis_bus || clli >= llis_bus +
802 sizeof(u32) * llis_max_words);
805 * Locate the next LLI - as this is an array,
806 * it's simple maths to find.
808 llis_va += (clli - llis_bus) / sizeof(u32);
810 llis_va_limit = llis_va + llis_max_words;
812 for (; llis_va < llis_va_limit; llis_va += pl08x->lli_words) {
813 bytes += get_bytes_in_lli(ch, llis_va);
816 * A LLI pointer going backward terminates the LLI list
818 if (llis_va[PL080_LLI_LLI] <= clli)
826 * Allocate a physical channel for a virtual channel
828 * Try to locate a physical channel to be used for this transfer. If all
829 * are taken return NULL and the requester will have to cope by using
830 * some fallback PIO mode or retrying later.
832 static struct pl08x_phy_chan *
833 pl08x_get_phy_channel(struct pl08x_driver_data *pl08x,
834 struct pl08x_dma_chan *virt_chan)
836 struct pl08x_phy_chan *ch = NULL;
840 #ifdef CONFIG_SOC_STARFIVE_JH7110
841 ch = &pl08x->phy_chans[virt_chan->chan_id];
843 spin_lock_irqsave(&ch->lock, flags);
845 if (!ch->locked && !ch->serving) {
846 ch->serving = virt_chan;
847 spin_unlock_irqrestore(&ch->lock, flags);
851 spin_unlock_irqrestore(&ch->lock, flags);
853 for (i = 0; i < pl08x->vd->channels; i++) {
854 ch = &pl08x->phy_chans[i];
856 spin_lock_irqsave(&ch->lock, flags);
858 if (!ch->locked && !ch->serving) {
859 ch->serving = virt_chan;
860 spin_unlock_irqrestore(&ch->lock, flags);
864 spin_unlock_irqrestore(&ch->lock, flags);
867 if (i == pl08x->vd->channels) {
868 /* No physical channel available, cope with it */
875 /* Mark the physical channel as free. Note, this write is atomic. */
876 static inline void pl08x_put_phy_channel(struct pl08x_driver_data *pl08x,
877 struct pl08x_phy_chan *ch)
883 * Try to allocate a physical channel. When successful, assign it to
884 * this virtual channel, and initiate the next descriptor. The
885 * virtual channel lock must be held at this point.
887 static void pl08x_phy_alloc_and_start(struct pl08x_dma_chan *plchan)
889 struct pl08x_driver_data *pl08x = plchan->host;
890 struct pl08x_phy_chan *ch;
892 ch = pl08x_get_phy_channel(pl08x, plchan);
894 dev_dbg(&pl08x->adev->dev, "no physical channel available for xfer on %s\n", plchan->name);
895 plchan->state = PL08X_CHAN_WAITING;
896 plchan->waiting_at = jiffies;
900 dev_dbg(&pl08x->adev->dev, "allocated physical channel %d for xfer on %s\n",
901 ch->id, plchan->name);
903 plchan->phychan = ch;
904 plchan->state = PL08X_CHAN_RUNNING;
905 pl08x_start_next_txd(plchan);
908 static void pl08x_phy_reassign_start(struct pl08x_phy_chan *ch,
909 struct pl08x_dma_chan *plchan)
911 struct pl08x_driver_data *pl08x = plchan->host;
913 dev_dbg(&pl08x->adev->dev, "reassigned physical channel %d for xfer on %s\n",
914 ch->id, plchan->name);
917 * We do this without taking the lock; we're really only concerned
918 * about whether this pointer is NULL or not, and we're guaranteed
919 * that this will only be called when it _already_ is non-NULL.
921 ch->serving = plchan;
922 plchan->phychan = ch;
923 plchan->state = PL08X_CHAN_RUNNING;
924 pl08x_start_next_txd(plchan);
928 * Free a physical DMA channel, potentially reallocating it to another
929 * virtual channel if we have any pending.
931 static void pl08x_phy_free(struct pl08x_dma_chan *plchan)
933 struct pl08x_driver_data *pl08x = plchan->host;
934 struct pl08x_dma_chan *p, *next;
935 unsigned long waiting_at;
938 waiting_at = jiffies;
941 * Find a waiting virtual channel for the next transfer.
942 * To be fair, time when each channel reached waiting state is compared
943 * to select channel that is waiting for the longest time.
945 list_for_each_entry(p, &pl08x->memcpy.channels, vc.chan.device_node)
946 if (p->state == PL08X_CHAN_WAITING &&
947 p->waiting_at <= waiting_at) {
949 waiting_at = p->waiting_at;
952 if (!next && pl08x->has_slave) {
953 list_for_each_entry(p, &pl08x->slave.channels, vc.chan.device_node)
954 if (p->state == PL08X_CHAN_WAITING &&
955 p->waiting_at <= waiting_at) {
957 waiting_at = p->waiting_at;
961 /* Ensure that the physical channel is stopped */
962 pl08x_terminate_phy_chan(pl08x, plchan->phychan);
968 * Eww. We know this isn't going to deadlock
969 * but lockdep probably doesn't.
971 spin_lock(&next->vc.lock);
972 /* Re-check the state now that we have the lock */
973 success = next->state == PL08X_CHAN_WAITING;
975 pl08x_phy_reassign_start(plchan->phychan, next);
976 spin_unlock(&next->vc.lock);
978 /* If the state changed, try to find another channel */
982 /* No more jobs, so free up the physical channel */
983 pl08x_put_phy_channel(pl08x, plchan->phychan);
986 plchan->phychan = NULL;
987 plchan->state = PL08X_CHAN_IDLE;
994 static inline unsigned int
995 pl08x_get_bytes_for_lli(struct pl08x_driver_data *pl08x,
1001 if (pl08x->vd->ftdmac020) {
1003 val = (cctl & FTDMAC020_LLI_SRC_WIDTH_MSK) >>
1004 FTDMAC020_LLI_SRC_WIDTH_SHIFT;
1006 val = (cctl & FTDMAC020_LLI_DST_WIDTH_MSK) >>
1007 FTDMAC020_LLI_DST_WIDTH_SHIFT;
1010 val = (cctl & PL080_CONTROL_SWIDTH_MASK) >>
1011 PL080_CONTROL_SWIDTH_SHIFT;
1013 val = (cctl & PL080_CONTROL_DWIDTH_MASK) >>
1014 PL080_CONTROL_DWIDTH_SHIFT;
1018 case PL080_WIDTH_8BIT:
1020 case PL080_WIDTH_16BIT:
1022 case PL080_WIDTH_32BIT:
1031 static inline u32 pl08x_lli_control_bits(struct pl08x_driver_data *pl08x,
1033 u8 srcwidth, u8 dstwidth,
1039 * Remove all src, dst and transfer size bits, then set the
1040 * width and size according to the parameters. The bit offsets
1041 * are different in the FTDMAC020 so we need to accound for this.
1043 if (pl08x->vd->ftdmac020) {
1044 retbits &= ~FTDMAC020_LLI_DST_WIDTH_MSK;
1045 retbits &= ~FTDMAC020_LLI_SRC_WIDTH_MSK;
1046 retbits &= ~FTDMAC020_LLI_TRANSFER_SIZE_MASK;
1050 retbits |= PL080_WIDTH_8BIT <<
1051 FTDMAC020_LLI_SRC_WIDTH_SHIFT;
1054 retbits |= PL080_WIDTH_16BIT <<
1055 FTDMAC020_LLI_SRC_WIDTH_SHIFT;
1058 retbits |= PL080_WIDTH_32BIT <<
1059 FTDMAC020_LLI_SRC_WIDTH_SHIFT;
1068 retbits |= PL080_WIDTH_8BIT <<
1069 FTDMAC020_LLI_DST_WIDTH_SHIFT;
1072 retbits |= PL080_WIDTH_16BIT <<
1073 FTDMAC020_LLI_DST_WIDTH_SHIFT;
1076 retbits |= PL080_WIDTH_32BIT <<
1077 FTDMAC020_LLI_DST_WIDTH_SHIFT;
1084 tsize &= FTDMAC020_LLI_TRANSFER_SIZE_MASK;
1085 retbits |= tsize << FTDMAC020_LLI_TRANSFER_SIZE_SHIFT;
1087 retbits &= ~PL080_CONTROL_DWIDTH_MASK;
1088 retbits &= ~PL080_CONTROL_SWIDTH_MASK;
1089 retbits &= ~PL080_CONTROL_TRANSFER_SIZE_MASK;
1093 retbits |= PL080_WIDTH_8BIT <<
1094 PL080_CONTROL_SWIDTH_SHIFT;
1097 retbits |= PL080_WIDTH_16BIT <<
1098 PL080_CONTROL_SWIDTH_SHIFT;
1101 retbits |= PL080_WIDTH_32BIT <<
1102 PL080_CONTROL_SWIDTH_SHIFT;
1111 retbits |= PL080_WIDTH_8BIT <<
1112 PL080_CONTROL_DWIDTH_SHIFT;
1115 retbits |= PL080_WIDTH_16BIT <<
1116 PL080_CONTROL_DWIDTH_SHIFT;
1119 retbits |= PL080_WIDTH_32BIT <<
1120 PL080_CONTROL_DWIDTH_SHIFT;
1127 tsize &= PL080_CONTROL_TRANSFER_SIZE_MASK;
1128 retbits |= tsize << PL080_CONTROL_TRANSFER_SIZE_SHIFT;
1134 struct pl08x_lli_build_data {
1135 struct pl08x_txd *txd;
1136 struct pl08x_bus_data srcbus;
1137 struct pl08x_bus_data dstbus;
1143 * Autoselect a master bus to use for the transfer. Slave will be the chosen as
1144 * victim in case src & dest are not similarly aligned. i.e. If after aligning
1145 * masters address with width requirements of transfer (by sending few byte by
1146 * byte data), slave is still not aligned, then its width will be reduced to
1148 * - prefers the destination bus if both available
1149 * - prefers bus with fixed address (i.e. peripheral)
1151 static void pl08x_choose_master_bus(struct pl08x_driver_data *pl08x,
1152 struct pl08x_lli_build_data *bd,
1153 struct pl08x_bus_data **mbus,
1154 struct pl08x_bus_data **sbus,
1161 * The FTDMAC020 only supports memory-to-memory transfer, so
1162 * source and destination always increase.
1164 if (pl08x->vd->ftdmac020) {
1168 dst_incr = !!(cctl & PL080_CONTROL_DST_INCR);
1169 src_incr = !!(cctl & PL080_CONTROL_SRC_INCR);
1173 * If either bus is not advancing, i.e. it is a peripheral, that
1174 * one becomes master
1177 *mbus = &bd->dstbus;
1178 *sbus = &bd->srcbus;
1179 } else if (!src_incr) {
1180 *mbus = &bd->srcbus;
1181 *sbus = &bd->dstbus;
1183 if (bd->dstbus.buswidth >= bd->srcbus.buswidth) {
1184 *mbus = &bd->dstbus;
1185 *sbus = &bd->srcbus;
1187 *mbus = &bd->srcbus;
1188 *sbus = &bd->dstbus;
1194 * Fills in one LLI for a certain transfer descriptor and advance the counter
1196 static void pl08x_fill_lli_for_desc(struct pl08x_driver_data *pl08x,
1197 struct pl08x_lli_build_data *bd,
1198 int num_llis, int len, u32 cctl, u32 cctl2)
1200 u32 offset = num_llis * pl08x->lli_words;
1201 u32 *llis_va = bd->txd->llis_va + offset;
1202 dma_addr_t llis_bus = bd->txd->llis_bus;
1204 BUG_ON(num_llis >= MAX_NUM_TSFR_LLIS);
1206 /* Advance the offset to next LLI. */
1207 offset += pl08x->lli_words;
1209 llis_va[PL080_LLI_SRC] = bd->srcbus.addr;
1210 llis_va[PL080_LLI_DST] = bd->dstbus.addr;
1211 llis_va[PL080_LLI_LLI] = (llis_bus + sizeof(u32) * offset);
1212 llis_va[PL080_LLI_LLI] |= bd->lli_bus;
1213 llis_va[PL080_LLI_CCTL] = cctl;
1214 if (pl08x->vd->pl080s)
1215 llis_va[PL080S_LLI_CCTL2] = cctl2;
1217 if (pl08x->vd->ftdmac020) {
1218 /* FIXME: only memcpy so far so both increase */
1219 bd->srcbus.addr += len;
1220 bd->dstbus.addr += len;
1222 if (cctl & PL080_CONTROL_SRC_INCR)
1223 bd->srcbus.addr += len;
1224 if (cctl & PL080_CONTROL_DST_INCR)
1225 bd->dstbus.addr += len;
1228 BUG_ON(bd->remainder < len);
1230 bd->remainder -= len;
1233 static inline void prep_byte_width_lli(struct pl08x_driver_data *pl08x,
1234 struct pl08x_lli_build_data *bd, u32 *cctl, u32 len,
1235 int num_llis, size_t *total_bytes)
1237 *cctl = pl08x_lli_control_bits(pl08x, *cctl, 1, 1, len);
1238 pl08x_fill_lli_for_desc(pl08x, bd, num_llis, len, *cctl, len);
1239 (*total_bytes) += len;
1243 static void pl08x_dump_lli(struct pl08x_driver_data *pl08x,
1244 const u32 *llis_va, int num_llis)
1248 if (pl08x->vd->pl080s) {
1249 dev_vdbg(&pl08x->adev->dev,
1250 "%-3s %-9s %-10s %-10s %-10s %-10s %s\n",
1251 "lli", "", "csrc", "cdst", "clli", "cctl", "cctl2");
1252 for (i = 0; i < num_llis; i++) {
1253 dev_vdbg(&pl08x->adev->dev,
1254 "%3d @%p: 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
1255 i, llis_va, llis_va[PL080_LLI_SRC],
1256 llis_va[PL080_LLI_DST], llis_va[PL080_LLI_LLI],
1257 llis_va[PL080_LLI_CCTL],
1258 llis_va[PL080S_LLI_CCTL2]);
1259 llis_va += pl08x->lli_words;
1262 dev_vdbg(&pl08x->adev->dev,
1263 "%-3s %-9s %-10s %-10s %-10s %s\n",
1264 "lli", "", "csrc", "cdst", "clli", "cctl");
1265 for (i = 0; i < num_llis; i++) {
1266 dev_vdbg(&pl08x->adev->dev,
1267 "%3d @%p: 0x%08x 0x%08x 0x%08x 0x%08x\n",
1268 i, llis_va, llis_va[PL080_LLI_SRC],
1269 llis_va[PL080_LLI_DST], llis_va[PL080_LLI_LLI],
1270 llis_va[PL080_LLI_CCTL]);
1271 llis_va += pl08x->lli_words;
1276 static inline void pl08x_dump_lli(struct pl08x_driver_data *pl08x,
1277 const u32 *llis_va, int num_llis) {}
1281 * This fills in the table of LLIs for the transfer descriptor
1282 * Note that we assume we never have to change the burst sizes
1283 * Return 0 for error
1285 static int pl08x_fill_llis_for_desc(struct pl08x_driver_data *pl08x,
1286 struct pl08x_txd *txd)
1288 struct pl08x_bus_data *mbus, *sbus;
1289 struct pl08x_lli_build_data bd;
1291 u32 cctl, early_bytes = 0;
1292 size_t max_bytes_per_lli, total_bytes;
1293 u32 *llis_va, *last_lli;
1294 struct pl08x_sg *dsg;
1296 txd->llis_va = dma_pool_alloc(pl08x->pool, GFP_NOWAIT, &txd->llis_bus);
1297 if (!txd->llis_va) {
1298 dev_err(&pl08x->adev->dev, "%s no memory for llis\n", __func__);
1303 bd.lli_bus = (pl08x->lli_buses & PL08X_AHB2) ? PL080_LLI_LM_AHB2 : 0;
1306 /* Find maximum width of the source bus */
1307 bd.srcbus.maxwidth = pl08x_get_bytes_for_lli(pl08x, cctl, true);
1309 /* Find maximum width of the destination bus */
1310 bd.dstbus.maxwidth = pl08x_get_bytes_for_lli(pl08x, cctl, false);
1312 list_for_each_entry(dsg, &txd->dsg_list, node) {
1316 bd.srcbus.addr = dsg->src_addr;
1317 bd.dstbus.addr = dsg->dst_addr;
1318 bd.remainder = dsg->len;
1319 bd.srcbus.buswidth = bd.srcbus.maxwidth;
1320 bd.dstbus.buswidth = bd.dstbus.maxwidth;
1322 pl08x_choose_master_bus(pl08x, &bd, &mbus, &sbus, cctl);
1324 dev_vdbg(&pl08x->adev->dev,
1325 "src=0x%08llx%s/%u dst=0x%08llx%s/%u len=%zu\n",
1326 (u64)bd.srcbus.addr,
1327 cctl & PL080_CONTROL_SRC_INCR ? "+" : "",
1329 (u64)bd.dstbus.addr,
1330 cctl & PL080_CONTROL_DST_INCR ? "+" : "",
1333 dev_vdbg(&pl08x->adev->dev, "mbus=%s sbus=%s\n",
1334 mbus == &bd.srcbus ? "src" : "dst",
1335 sbus == &bd.srcbus ? "src" : "dst");
1338 * Zero length is only allowed if all these requirements are
1340 * - flow controller is peripheral.
1341 * - src.addr is aligned to src.width
1342 * - dst.addr is aligned to dst.width
1344 * sg_len == 1 should be true, as there can be two cases here:
1346 * - Memory addresses are contiguous and are not scattered.
1347 * Here, Only one sg will be passed by user driver, with
1348 * memory address and zero length. We pass this to controller
1349 * and after the transfer it will receive the last burst
1350 * request from peripheral and so transfer finishes.
1352 * - Memory addresses are scattered and are not contiguous.
1353 * Here, Obviously as DMA controller doesn't know when a lli's
1354 * transfer gets over, it can't load next lli. So in this
1355 * case, there has to be an assumption that only one lli is
1356 * supported. Thus, we can't have scattered addresses.
1358 if (!bd.remainder) {
1361 /* FTDMAC020 only does memory-to-memory */
1362 if (pl08x->vd->ftdmac020)
1363 fc = PL080_FLOW_MEM2MEM;
1365 fc = (txd->ccfg & PL080_CONFIG_FLOW_CONTROL_MASK) >>
1366 PL080_CONFIG_FLOW_CONTROL_SHIFT;
1367 if (!((fc >= PL080_FLOW_SRC2DST_DST) &&
1368 (fc <= PL080_FLOW_SRC2DST_SRC))) {
1369 dev_err(&pl08x->adev->dev, "%s sg len can't be zero",
1374 if (!IS_BUS_ALIGNED(&bd.srcbus) ||
1375 !IS_BUS_ALIGNED(&bd.dstbus)) {
1376 dev_err(&pl08x->adev->dev,
1377 "%s src & dst address must be aligned to src"
1378 " & dst width if peripheral is flow controller",
1383 cctl = pl08x_lli_control_bits(pl08x, cctl,
1384 bd.srcbus.buswidth, bd.dstbus.buswidth,
1386 pl08x_fill_lli_for_desc(pl08x, &bd, num_llis++,
1392 * Send byte by byte for following cases
1393 * - Less than a bus width available
1394 * - until master bus is aligned
1396 if (bd.remainder < mbus->buswidth)
1397 early_bytes = bd.remainder;
1398 else if (!IS_BUS_ALIGNED(mbus)) {
1399 early_bytes = mbus->buswidth -
1400 (mbus->addr & (mbus->buswidth - 1));
1401 if ((bd.remainder - early_bytes) < mbus->buswidth)
1402 early_bytes = bd.remainder;
1406 dev_vdbg(&pl08x->adev->dev,
1407 "%s byte width LLIs (remain 0x%08zx)\n",
1408 __func__, bd.remainder);
1409 prep_byte_width_lli(pl08x, &bd, &cctl, early_bytes,
1410 num_llis++, &total_bytes);
1415 * Master now aligned
1416 * - if slave is not then we must set its width down
1418 if (!IS_BUS_ALIGNED(sbus)) {
1419 dev_dbg(&pl08x->adev->dev,
1420 "%s set down bus width to one byte\n",
1427 * Bytes transferred = tsize * src width, not
1430 max_bytes_per_lli = bd.srcbus.buswidth *
1431 pl08x->vd->max_transfer_size;
1432 dev_vdbg(&pl08x->adev->dev,
1433 "%s max bytes per lli = %zu\n",
1434 __func__, max_bytes_per_lli);
1437 * Make largest possible LLIs until less than one bus
1440 while (bd.remainder > (mbus->buswidth - 1)) {
1441 size_t lli_len, tsize, width;
1444 * If enough left try to send max possible,
1445 * otherwise try to send the remainder
1447 lli_len = min(bd.remainder, max_bytes_per_lli);
1450 * Check against maximum bus alignment:
1451 * Calculate actual transfer size in relation to
1452 * bus width an get a maximum remainder of the
1453 * highest bus width - 1
1455 width = max(mbus->buswidth, sbus->buswidth);
1456 lli_len = (lli_len / width) * width;
1457 tsize = lli_len / bd.srcbus.buswidth;
1459 dev_vdbg(&pl08x->adev->dev,
1460 "%s fill lli with single lli chunk of "
1461 "size 0x%08zx (remainder 0x%08zx)\n",
1462 __func__, lli_len, bd.remainder);
1464 cctl = pl08x_lli_control_bits(pl08x, cctl,
1465 bd.srcbus.buswidth, bd.dstbus.buswidth,
1467 pl08x_fill_lli_for_desc(pl08x, &bd, num_llis++,
1468 lli_len, cctl, tsize);
1469 total_bytes += lli_len;
1473 * Send any odd bytes
1476 dev_vdbg(&pl08x->adev->dev,
1477 "%s align with boundary, send odd bytes (remain %zu)\n",
1478 __func__, bd.remainder);
1479 prep_byte_width_lli(pl08x, &bd, &cctl,
1480 bd.remainder, num_llis++, &total_bytes);
1484 if (total_bytes != dsg->len) {
1485 dev_err(&pl08x->adev->dev,
1486 "%s size of encoded lli:s don't match total txd, transferred 0x%08zx from size 0x%08zx\n",
1487 __func__, total_bytes, dsg->len);
1491 if (num_llis >= MAX_NUM_TSFR_LLIS) {
1492 dev_err(&pl08x->adev->dev,
1493 "%s need to increase MAX_NUM_TSFR_LLIS from 0x%08x\n",
1494 __func__, MAX_NUM_TSFR_LLIS);
1499 llis_va = txd->llis_va;
1500 last_lli = llis_va + (num_llis - 1) * pl08x->lli_words;
1503 /* Link back to the first LLI. */
1504 last_lli[PL080_LLI_LLI] = txd->llis_bus | bd.lli_bus;
1506 /* The final LLI terminates the LLI. */
1507 last_lli[PL080_LLI_LLI] = 0;
1508 /* The final LLI element shall also fire an interrupt. */
1509 if (pl08x->vd->ftdmac020)
1510 last_lli[PL080_LLI_CCTL] &= ~FTDMAC020_LLI_TC_MSK;
1512 last_lli[PL080_LLI_CCTL] |= PL080_CONTROL_TC_IRQ_EN;
1515 pl08x_dump_lli(pl08x, llis_va, num_llis);
1520 static void pl08x_free_txd(struct pl08x_driver_data *pl08x,
1521 struct pl08x_txd *txd)
1523 struct pl08x_sg *dsg, *_dsg;
1526 dma_pool_free(pl08x->pool, txd->llis_va, txd->llis_bus);
1528 list_for_each_entry_safe(dsg, _dsg, &txd->dsg_list, node) {
1529 list_del(&dsg->node);
1536 static void pl08x_desc_free(struct virt_dma_desc *vd)
1538 struct pl08x_txd *txd = to_pl08x_txd(&vd->tx);
1539 struct pl08x_dma_chan *plchan = to_pl08x_chan(vd->tx.chan);
1541 dma_descriptor_unmap(&vd->tx);
1543 pl08x_release_mux(plchan);
1545 pl08x_free_txd(plchan->host, txd);
1548 static void pl08x_free_txd_list(struct pl08x_driver_data *pl08x,
1549 struct pl08x_dma_chan *plchan)
1553 vchan_get_all_descriptors(&plchan->vc, &head);
1554 vchan_dma_desc_free_list(&plchan->vc, &head);
1558 * The DMA ENGINE API
1560 static void pl08x_free_chan_resources(struct dma_chan *chan)
1562 /* Ensure all queued descriptors are freed */
1563 vchan_free_chan_resources(to_virt_chan(chan));
1566 static struct dma_async_tx_descriptor *pl08x_prep_dma_interrupt(
1567 struct dma_chan *chan, unsigned long flags)
1569 struct dma_async_tx_descriptor *retval = NULL;
1575 * Code accessing dma_async_is_complete() in a tight loop may give problems.
1576 * If slaves are relying on interrupts to signal completion this function
1577 * must not be called with interrupts disabled.
1579 static enum dma_status pl08x_dma_tx_status(struct dma_chan *chan,
1580 dma_cookie_t cookie, struct dma_tx_state *txstate)
1582 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1583 struct virt_dma_desc *vd;
1584 unsigned long flags;
1585 enum dma_status ret;
1588 ret = dma_cookie_status(chan, cookie, txstate);
1589 if (ret == DMA_COMPLETE)
1593 * There's no point calculating the residue if there's
1594 * no txstate to store the value.
1597 if (plchan->state == PL08X_CHAN_PAUSED)
1602 spin_lock_irqsave(&plchan->vc.lock, flags);
1603 ret = dma_cookie_status(chan, cookie, txstate);
1604 if (ret != DMA_COMPLETE) {
1605 vd = vchan_find_desc(&plchan->vc, cookie);
1607 /* On the issued list, so hasn't been processed yet */
1608 struct pl08x_txd *txd = to_pl08x_txd(&vd->tx);
1609 struct pl08x_sg *dsg;
1611 list_for_each_entry(dsg, &txd->dsg_list, node)
1614 bytes = pl08x_getbytes_chan(plchan);
1617 spin_unlock_irqrestore(&plchan->vc.lock, flags);
1620 * This cookie not complete yet
1621 * Get number of bytes left in the active transactions and queue
1623 dma_set_residue(txstate, bytes);
1625 if (plchan->state == PL08X_CHAN_PAUSED && ret == DMA_IN_PROGRESS)
1628 /* Whether waiting or running, we're in progress */
1632 /* PrimeCell DMA extension */
1633 struct burst_table {
1638 static const struct burst_table burst_sizes[] = {
1641 .reg = PL080_BSIZE_256,
1645 .reg = PL080_BSIZE_128,
1649 .reg = PL080_BSIZE_64,
1653 .reg = PL080_BSIZE_32,
1657 .reg = PL080_BSIZE_16,
1661 .reg = PL080_BSIZE_8,
1665 .reg = PL080_BSIZE_4,
1669 .reg = PL080_BSIZE_1,
1674 * Given the source and destination available bus masks, select which
1675 * will be routed to each port. We try to have source and destination
1676 * on separate ports, but always respect the allowable settings.
1678 static u32 pl08x_select_bus(bool ftdmac020, u8 src, u8 dst)
1684 /* The FTDMAC020 use different bits to indicate src/dst bus */
1686 dst_ahb2 = FTDMAC020_LLI_DST_SEL;
1687 src_ahb2 = FTDMAC020_LLI_SRC_SEL;
1689 dst_ahb2 = PL080_CONTROL_DST_AHB2;
1690 src_ahb2 = PL080_CONTROL_SRC_AHB2;
1693 if (!(dst & PL08X_AHB1) || ((dst & PL08X_AHB2) && (src & PL08X_AHB1)))
1695 if (!(src & PL08X_AHB1) || ((src & PL08X_AHB2) && !(dst & PL08X_AHB2)))
1701 static u32 pl08x_cctl(u32 cctl)
1703 cctl &= ~(PL080_CONTROL_SRC_AHB2 | PL080_CONTROL_DST_AHB2 |
1704 PL080_CONTROL_SRC_INCR | PL080_CONTROL_DST_INCR |
1705 PL080_CONTROL_PROT_MASK);
1707 /* Access the cell in privileged mode, non-bufferable, non-cacheable */
1708 return cctl | PL080_CONTROL_PROT_SYS;
1711 static u32 pl08x_width(enum dma_slave_buswidth width)
1714 case DMA_SLAVE_BUSWIDTH_1_BYTE:
1715 return PL080_WIDTH_8BIT;
1716 case DMA_SLAVE_BUSWIDTH_2_BYTES:
1717 return PL080_WIDTH_16BIT;
1718 case DMA_SLAVE_BUSWIDTH_4_BYTES:
1719 return PL080_WIDTH_32BIT;
1725 static u32 pl08x_burst(u32 maxburst)
1729 for (i = 0; i < ARRAY_SIZE(burst_sizes); i++)
1730 if (burst_sizes[i].burstwords <= maxburst)
1733 return burst_sizes[i].reg;
1736 static u32 pl08x_get_cctl(struct pl08x_dma_chan *plchan,
1737 enum dma_slave_buswidth addr_width, u32 maxburst)
1739 u32 width, burst, cctl = 0;
1741 width = pl08x_width(addr_width);
1745 cctl |= width << PL080_CONTROL_SWIDTH_SHIFT;
1746 cctl |= width << PL080_CONTROL_DWIDTH_SHIFT;
1749 * If this channel will only request single transfers, set this
1750 * down to ONE element. Also select one element if no maxburst
1753 if (plchan->cd->single)
1756 burst = pl08x_burst(maxburst);
1757 cctl |= burst << PL080_CONTROL_SB_SIZE_SHIFT;
1758 cctl |= burst << PL080_CONTROL_DB_SIZE_SHIFT;
1760 return pl08x_cctl(cctl);
1764 * Slave transactions callback to the slave device to allow
1765 * synchronization of slave DMA signals with the DMAC enable
1767 static void pl08x_issue_pending(struct dma_chan *chan)
1769 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1770 unsigned long flags;
1772 #ifdef CONFIG_SOC_STARFIVE_JH7110
1773 plchan->chan_id = chan->chan_id;
1775 spin_lock_irqsave(&plchan->vc.lock, flags);
1776 if (vchan_issue_pending(&plchan->vc)) {
1777 if (!plchan->phychan && plchan->state != PL08X_CHAN_WAITING)
1778 pl08x_phy_alloc_and_start(plchan);
1780 spin_unlock_irqrestore(&plchan->vc.lock, flags);
1783 static struct pl08x_txd *pl08x_get_txd(struct pl08x_dma_chan *plchan)
1785 struct pl08x_txd *txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
1788 INIT_LIST_HEAD(&txd->dsg_list);
1792 static u32 pl08x_memcpy_cctl(struct pl08x_driver_data *pl08x)
1797 switch (pl08x->pd->memcpy_burst_size) {
1799 dev_err(&pl08x->adev->dev,
1800 "illegal burst size for memcpy, set to 1\n");
1802 case PL08X_BURST_SZ_1:
1803 cctl |= PL080_BSIZE_1 << PL080_CONTROL_SB_SIZE_SHIFT |
1804 PL080_BSIZE_1 << PL080_CONTROL_DB_SIZE_SHIFT;
1806 case PL08X_BURST_SZ_4:
1807 cctl |= PL080_BSIZE_4 << PL080_CONTROL_SB_SIZE_SHIFT |
1808 PL080_BSIZE_4 << PL080_CONTROL_DB_SIZE_SHIFT;
1810 case PL08X_BURST_SZ_8:
1811 cctl |= PL080_BSIZE_8 << PL080_CONTROL_SB_SIZE_SHIFT |
1812 PL080_BSIZE_8 << PL080_CONTROL_DB_SIZE_SHIFT;
1814 case PL08X_BURST_SZ_16:
1815 cctl |= PL080_BSIZE_16 << PL080_CONTROL_SB_SIZE_SHIFT |
1816 PL080_BSIZE_16 << PL080_CONTROL_DB_SIZE_SHIFT;
1818 case PL08X_BURST_SZ_32:
1819 cctl |= PL080_BSIZE_32 << PL080_CONTROL_SB_SIZE_SHIFT |
1820 PL080_BSIZE_32 << PL080_CONTROL_DB_SIZE_SHIFT;
1822 case PL08X_BURST_SZ_64:
1823 cctl |= PL080_BSIZE_64 << PL080_CONTROL_SB_SIZE_SHIFT |
1824 PL080_BSIZE_64 << PL080_CONTROL_DB_SIZE_SHIFT;
1826 case PL08X_BURST_SZ_128:
1827 cctl |= PL080_BSIZE_128 << PL080_CONTROL_SB_SIZE_SHIFT |
1828 PL080_BSIZE_128 << PL080_CONTROL_DB_SIZE_SHIFT;
1830 case PL08X_BURST_SZ_256:
1831 cctl |= PL080_BSIZE_256 << PL080_CONTROL_SB_SIZE_SHIFT |
1832 PL080_BSIZE_256 << PL080_CONTROL_DB_SIZE_SHIFT;
1836 switch (pl08x->pd->memcpy_bus_width) {
1838 dev_err(&pl08x->adev->dev,
1839 "illegal bus width for memcpy, set to 8 bits\n");
1841 case PL08X_BUS_WIDTH_8_BITS:
1842 cctl |= PL080_WIDTH_8BIT << PL080_CONTROL_SWIDTH_SHIFT |
1843 PL080_WIDTH_8BIT << PL080_CONTROL_DWIDTH_SHIFT;
1845 case PL08X_BUS_WIDTH_16_BITS:
1846 cctl |= PL080_WIDTH_16BIT << PL080_CONTROL_SWIDTH_SHIFT |
1847 PL080_WIDTH_16BIT << PL080_CONTROL_DWIDTH_SHIFT;
1849 case PL08X_BUS_WIDTH_32_BITS:
1850 cctl |= PL080_WIDTH_32BIT << PL080_CONTROL_SWIDTH_SHIFT |
1851 PL080_WIDTH_32BIT << PL080_CONTROL_DWIDTH_SHIFT;
1855 /* Protection flags */
1856 if (pl08x->pd->memcpy_prot_buff)
1857 cctl |= PL080_CONTROL_PROT_BUFF;
1858 if (pl08x->pd->memcpy_prot_cache)
1859 cctl |= PL080_CONTROL_PROT_CACHE;
1861 /* We are the kernel, so we are in privileged mode */
1862 cctl |= PL080_CONTROL_PROT_SYS;
1864 /* Both to be incremented or the code will break */
1865 cctl |= PL080_CONTROL_SRC_INCR | PL080_CONTROL_DST_INCR;
1867 if (pl08x->vd->dualmaster)
1868 cctl |= pl08x_select_bus(false,
1875 static u32 pl08x_ftdmac020_memcpy_cctl(struct pl08x_driver_data *pl08x)
1880 switch (pl08x->pd->memcpy_bus_width) {
1882 dev_err(&pl08x->adev->dev,
1883 "illegal bus width for memcpy, set to 8 bits\n");
1885 case PL08X_BUS_WIDTH_8_BITS:
1886 cctl |= PL080_WIDTH_8BIT << FTDMAC020_LLI_SRC_WIDTH_SHIFT |
1887 PL080_WIDTH_8BIT << FTDMAC020_LLI_DST_WIDTH_SHIFT;
1889 case PL08X_BUS_WIDTH_16_BITS:
1890 cctl |= PL080_WIDTH_16BIT << FTDMAC020_LLI_SRC_WIDTH_SHIFT |
1891 PL080_WIDTH_16BIT << FTDMAC020_LLI_DST_WIDTH_SHIFT;
1893 case PL08X_BUS_WIDTH_32_BITS:
1894 cctl |= PL080_WIDTH_32BIT << FTDMAC020_LLI_SRC_WIDTH_SHIFT |
1895 PL080_WIDTH_32BIT << FTDMAC020_LLI_DST_WIDTH_SHIFT;
1900 * By default mask the TC IRQ on all LLIs, it will be unmasked on
1901 * the last LLI item by other code.
1903 cctl |= FTDMAC020_LLI_TC_MSK;
1906 * Both to be incremented so leave bits FTDMAC020_LLI_SRCAD_CTL
1907 * and FTDMAC020_LLI_DSTAD_CTL as zero
1909 if (pl08x->vd->dualmaster)
1910 cctl |= pl08x_select_bus(true,
1918 * Initialize a descriptor to be used by memcpy submit
1920 static struct dma_async_tx_descriptor *pl08x_prep_dma_memcpy(
1921 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
1922 size_t len, unsigned long flags)
1924 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1925 struct pl08x_driver_data *pl08x = plchan->host;
1926 struct pl08x_txd *txd;
1927 struct pl08x_sg *dsg;
1930 txd = pl08x_get_txd(plchan);
1932 dev_err(&pl08x->adev->dev,
1933 "%s no memory for descriptor\n", __func__);
1937 dsg = kzalloc(sizeof(struct pl08x_sg), GFP_NOWAIT);
1939 pl08x_free_txd(pl08x, txd);
1940 dev_err(&pl08x->adev->dev,
1941 "%s no memory for kzalloc\n", __func__);
1944 list_add_tail(&dsg->node, &txd->dsg_list);
1946 dsg->src_addr = src;
1947 dsg->dst_addr = dest;
1949 if (pl08x->vd->ftdmac020) {
1950 /* Writing CCFG zero ENABLES all interrupts */
1952 txd->cctl = pl08x_ftdmac020_memcpy_cctl(pl08x);
1954 txd->ccfg = PL080_CONFIG_ERR_IRQ_MASK |
1955 PL080_CONFIG_TC_IRQ_MASK |
1956 PL080_FLOW_MEM2MEM << PL080_CONFIG_FLOW_CONTROL_SHIFT;
1957 txd->cctl = pl08x_memcpy_cctl(pl08x);
1960 ret = pl08x_fill_llis_for_desc(plchan->host, txd);
1962 pl08x_free_txd(pl08x, txd);
1963 dev_err(&pl08x->adev->dev,
1964 "%s pl08x_fill_llis_for_desc error\n", __func__);
1968 return vchan_tx_prep(&plchan->vc, &txd->vd, flags);
1971 static struct pl08x_txd *pl08x_init_txd(
1972 struct dma_chan *chan,
1973 enum dma_transfer_direction direction,
1974 dma_addr_t *slave_addr)
1976 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1977 struct pl08x_driver_data *pl08x = plchan->host;
1978 struct pl08x_txd *txd;
1979 enum dma_slave_buswidth addr_width;
1981 u8 src_buses, dst_buses;
1984 txd = pl08x_get_txd(plchan);
1986 dev_err(&pl08x->adev->dev, "%s no txd\n", __func__);
1991 * Set up addresses, the PrimeCell configured address
1992 * will take precedence since this may configure the
1993 * channel target address dynamically at runtime.
1995 if (direction == DMA_MEM_TO_DEV) {
1996 cctl = PL080_CONTROL_SRC_INCR;
1997 *slave_addr = plchan->cfg.dst_addr;
1998 addr_width = plchan->cfg.dst_addr_width;
1999 maxburst = plchan->cfg.dst_maxburst;
2000 src_buses = pl08x->mem_buses;
2001 dst_buses = plchan->cd->periph_buses;
2002 } else if (direction == DMA_DEV_TO_MEM) {
2003 cctl = PL080_CONTROL_DST_INCR;
2004 *slave_addr = plchan->cfg.src_addr;
2005 addr_width = plchan->cfg.src_addr_width;
2006 maxburst = plchan->cfg.src_maxburst;
2007 src_buses = plchan->cd->periph_buses;
2008 dst_buses = pl08x->mem_buses;
2010 pl08x_free_txd(pl08x, txd);
2011 dev_err(&pl08x->adev->dev,
2012 "%s direction unsupported\n", __func__);
2016 cctl |= pl08x_get_cctl(plchan, addr_width, maxburst);
2018 pl08x_free_txd(pl08x, txd);
2019 dev_err(&pl08x->adev->dev,
2020 "DMA slave configuration botched?\n");
2024 txd->cctl = cctl | pl08x_select_bus(false, src_buses, dst_buses);
2026 if (plchan->cfg.device_fc)
2027 tmp = (direction == DMA_MEM_TO_DEV) ? PL080_FLOW_MEM2PER_PER :
2028 PL080_FLOW_PER2MEM_PER;
2030 tmp = (direction == DMA_MEM_TO_DEV) ? PL080_FLOW_MEM2PER :
2033 txd->ccfg = PL080_CONFIG_ERR_IRQ_MASK |
2034 PL080_CONFIG_TC_IRQ_MASK |
2035 tmp << PL080_CONFIG_FLOW_CONTROL_SHIFT;
2037 ret = pl08x_request_mux(plchan);
2039 pl08x_free_txd(pl08x, txd);
2040 dev_dbg(&pl08x->adev->dev,
2041 "unable to mux for transfer on %s due to platform restrictions\n",
2046 dev_dbg(&pl08x->adev->dev, "allocated DMA request signal %d for xfer on %s\n",
2047 plchan->signal, plchan->name);
2049 /* Assign the flow control signal to this channel */
2050 if (direction == DMA_MEM_TO_DEV)
2051 txd->ccfg |= plchan->signal << PL080_CONFIG_DST_SEL_SHIFT;
2053 txd->ccfg |= plchan->signal << PL080_CONFIG_SRC_SEL_SHIFT;
2058 static int pl08x_tx_add_sg(struct pl08x_txd *txd,
2059 enum dma_transfer_direction direction,
2060 dma_addr_t slave_addr,
2061 dma_addr_t buf_addr,
2064 struct pl08x_sg *dsg;
2066 dsg = kzalloc(sizeof(struct pl08x_sg), GFP_NOWAIT);
2070 list_add_tail(&dsg->node, &txd->dsg_list);
2073 if (direction == DMA_MEM_TO_DEV) {
2074 dsg->src_addr = buf_addr;
2075 dsg->dst_addr = slave_addr;
2077 dsg->src_addr = slave_addr;
2078 dsg->dst_addr = buf_addr;
2084 static struct dma_async_tx_descriptor *pl08x_prep_slave_sg(
2085 struct dma_chan *chan, struct scatterlist *sgl,
2086 unsigned int sg_len, enum dma_transfer_direction direction,
2087 unsigned long flags, void *context)
2089 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
2090 struct pl08x_driver_data *pl08x = plchan->host;
2091 struct pl08x_txd *txd;
2092 struct scatterlist *sg;
2094 dma_addr_t slave_addr;
2096 dev_dbg(&pl08x->adev->dev, "%s prepare transaction of %d bytes from %s\n",
2097 __func__, sg_dma_len(sgl), plchan->name);
2099 txd = pl08x_init_txd(chan, direction, &slave_addr);
2103 for_each_sg(sgl, sg, sg_len, tmp) {
2104 ret = pl08x_tx_add_sg(txd, direction, slave_addr,
2108 pl08x_release_mux(plchan);
2109 pl08x_free_txd(pl08x, txd);
2110 dev_err(&pl08x->adev->dev, "%s no mem for pl080 sg\n",
2116 ret = pl08x_fill_llis_for_desc(plchan->host, txd);
2118 pl08x_release_mux(plchan);
2119 pl08x_free_txd(pl08x, txd);
2123 return vchan_tx_prep(&plchan->vc, &txd->vd, flags);
2126 static struct dma_async_tx_descriptor *pl08x_prep_dma_cyclic(
2127 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
2128 size_t period_len, enum dma_transfer_direction direction,
2129 unsigned long flags)
2131 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
2132 struct pl08x_driver_data *pl08x = plchan->host;
2133 struct pl08x_txd *txd;
2135 dma_addr_t slave_addr;
2137 dev_dbg(&pl08x->adev->dev,
2138 "%s prepare cyclic transaction of %zd/%zd bytes %s %s\n",
2139 __func__, period_len, buf_len,
2140 direction == DMA_MEM_TO_DEV ? "to" : "from",
2143 txd = pl08x_init_txd(chan, direction, &slave_addr);
2148 txd->cctl |= PL080_CONTROL_TC_IRQ_EN;
2149 for (tmp = 0; tmp < buf_len; tmp += period_len) {
2150 ret = pl08x_tx_add_sg(txd, direction, slave_addr,
2151 buf_addr + tmp, period_len);
2153 pl08x_release_mux(plchan);
2154 pl08x_free_txd(pl08x, txd);
2159 ret = pl08x_fill_llis_for_desc(plchan->host, txd);
2161 pl08x_release_mux(plchan);
2162 pl08x_free_txd(pl08x, txd);
2166 return vchan_tx_prep(&plchan->vc, &txd->vd, flags);
2169 static int pl08x_config(struct dma_chan *chan,
2170 struct dma_slave_config *config)
2172 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
2173 struct pl08x_driver_data *pl08x = plchan->host;
2178 /* Reject definitely invalid configurations */
2179 if (config->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
2180 config->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
2183 if (config->device_fc && pl08x->vd->pl080s) {
2184 dev_err(&pl08x->adev->dev,
2185 "%s: PL080S does not support peripheral flow control\n",
2190 plchan->cfg = *config;
2195 static int pl08x_terminate_all(struct dma_chan *chan)
2197 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
2198 struct pl08x_driver_data *pl08x = plchan->host;
2199 unsigned long flags;
2201 spin_lock_irqsave(&plchan->vc.lock, flags);
2202 if (!plchan->phychan && !plchan->at) {
2203 spin_unlock_irqrestore(&plchan->vc.lock, flags);
2207 plchan->state = PL08X_CHAN_IDLE;
2209 if (plchan->phychan) {
2211 * Mark physical channel as free and free any slave
2214 pl08x_phy_free(plchan);
2216 /* Dequeue jobs and free LLIs */
2218 vchan_terminate_vdesc(&plchan->at->vd);
2221 /* Dequeue jobs not yet fired as well */
2222 pl08x_free_txd_list(pl08x, plchan);
2224 spin_unlock_irqrestore(&plchan->vc.lock, flags);
2229 static void pl08x_synchronize(struct dma_chan *chan)
2231 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
2233 vchan_synchronize(&plchan->vc);
2236 static int pl08x_pause(struct dma_chan *chan)
2238 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
2239 unsigned long flags;
2242 * Anything succeeds on channels with no physical allocation and
2243 * no queued transfers.
2245 spin_lock_irqsave(&plchan->vc.lock, flags);
2246 if (!plchan->phychan && !plchan->at) {
2247 spin_unlock_irqrestore(&plchan->vc.lock, flags);
2251 pl08x_pause_phy_chan(plchan->phychan);
2252 plchan->state = PL08X_CHAN_PAUSED;
2254 spin_unlock_irqrestore(&plchan->vc.lock, flags);
2259 static int pl08x_resume(struct dma_chan *chan)
2261 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
2262 unsigned long flags;
2265 * Anything succeeds on channels with no physical allocation and
2266 * no queued transfers.
2268 spin_lock_irqsave(&plchan->vc.lock, flags);
2269 if (!plchan->phychan && !plchan->at) {
2270 spin_unlock_irqrestore(&plchan->vc.lock, flags);
2274 pl08x_resume_phy_chan(plchan->phychan);
2275 plchan->state = PL08X_CHAN_RUNNING;
2277 spin_unlock_irqrestore(&plchan->vc.lock, flags);
2282 #ifndef CONFIG_SOC_STARFIVE_JH7110
2283 bool pl08x_filter_id(struct dma_chan *chan, void *chan_id)
2285 struct pl08x_dma_chan *plchan;
2286 char *name = chan_id;
2288 /* Reject channels for devices not bound to this driver */
2289 if (chan->device->dev->driver != &pl08x_amba_driver.drv)
2292 plchan = to_pl08x_chan(chan);
2294 /* Check that the channel is not taken! */
2295 if (!strcmp(plchan->name, name))
2300 EXPORT_SYMBOL_GPL(pl08x_filter_id);
2303 static bool pl08x_filter_fn(struct dma_chan *chan, void *chan_id)
2305 struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
2307 return plchan->cd == chan_id;
2311 * Just check that the device is there and active
2312 * TODO: turn this bit on/off depending on the number of physical channels
2313 * actually used, if it is zero... well shut it off. That will save some
2314 * power. Cut the clock at the same time.
2316 static void pl08x_ensure_on(struct pl08x_driver_data *pl08x)
2318 /* The Nomadik variant does not have the config register */
2319 if (pl08x->vd->nomadik)
2321 /* The FTDMAC020 variant does this in another register */
2322 if (pl08x->vd->ftdmac020) {
2323 writel(PL080_CONFIG_ENABLE, pl08x->base + FTDMAC020_CSR);
2326 writel(PL080_CONFIG_ENABLE, pl08x->base + PL080_CONFIG);
2329 static irqreturn_t pl08x_irq(int irq, void *dev)
2331 struct pl08x_driver_data *pl08x = dev;
2332 u32 mask = 0, err, tc, i;
2334 /* check & clear - ERR & TC interrupts */
2335 err = readl(pl08x->base + PL080_ERR_STATUS);
2337 dev_err(&pl08x->adev->dev, "%s error interrupt, register value 0x%08x\n",
2339 writel(err, pl08x->base + PL080_ERR_CLEAR);
2341 tc = readl(pl08x->base + PL080_TC_STATUS);
2343 writel(tc, pl08x->base + PL080_TC_CLEAR);
2348 for (i = 0; i < pl08x->vd->channels; i++) {
2349 if ((BIT(i) & err) || (BIT(i) & tc)) {
2350 /* Locate physical channel */
2351 struct pl08x_phy_chan *phychan = &pl08x->phy_chans[i];
2352 struct pl08x_dma_chan *plchan = phychan->serving;
2353 struct pl08x_txd *tx;
2356 dev_err(&pl08x->adev->dev,
2357 "%s Error TC interrupt on unused channel: 0x%08x\n",
2362 spin_lock(&plchan->vc.lock);
2364 if (tx && tx->cyclic) {
2365 vchan_cyclic_callback(&tx->vd);
2369 * This descriptor is done, release its mux
2372 pl08x_release_mux(plchan);
2374 vchan_cookie_complete(&tx->vd);
2377 * And start the next descriptor (if any),
2378 * otherwise free this channel.
2380 if (vchan_next_desc(&plchan->vc))
2381 pl08x_start_next_txd(plchan);
2383 pl08x_phy_free(plchan);
2385 spin_unlock(&plchan->vc.lock);
2391 return mask ? IRQ_HANDLED : IRQ_NONE;
2394 static void pl08x_dma_slave_init(struct pl08x_dma_chan *chan)
2397 chan->name = chan->cd->bus_id;
2398 chan->cfg.src_addr = chan->cd->addr;
2399 chan->cfg.dst_addr = chan->cd->addr;
2403 * Initialise the DMAC memcpy/slave channels.
2404 * Make a local wrapper to hold required data
2406 static int pl08x_dma_init_virtual_channels(struct pl08x_driver_data *pl08x,
2407 struct dma_device *dmadev, unsigned int channels, bool slave)
2409 struct pl08x_dma_chan *chan;
2412 INIT_LIST_HEAD(&dmadev->channels);
2415 * Register as many many memcpy as we have physical channels,
2416 * we won't always be able to use all but the code will have
2417 * to cope with that situation.
2419 for (i = 0; i < channels; i++) {
2420 chan = kzalloc(sizeof(*chan), GFP_KERNEL);
2425 chan->state = PL08X_CHAN_IDLE;
2429 chan->cd = &pl08x->pd->slave_channels[i];
2431 * Some implementations have muxed signals, whereas some
2432 * use a mux in front of the signals and need dynamic
2433 * assignment of signals.
2436 pl08x_dma_slave_init(chan);
2438 chan->cd = kzalloc(sizeof(*chan->cd), GFP_KERNEL);
2443 chan->cd->bus_id = "memcpy";
2444 chan->cd->periph_buses = pl08x->pd->mem_buses;
2445 chan->name = kasprintf(GFP_KERNEL, "memcpy%d", i);
2452 dev_dbg(&pl08x->adev->dev,
2453 "initialize virtual channel \"%s\"\n",
2456 chan->vc.desc_free = pl08x_desc_free;
2457 vchan_init(&chan->vc, dmadev);
2459 dev_info(&pl08x->adev->dev, "initialized %d virtual %s channels\n",
2460 i, slave ? "slave" : "memcpy");
2464 static void pl08x_free_virtual_channels(struct dma_device *dmadev)
2466 struct pl08x_dma_chan *chan = NULL;
2467 struct pl08x_dma_chan *next;
2469 list_for_each_entry_safe(chan,
2470 next, &dmadev->channels, vc.chan.device_node) {
2471 list_del(&chan->vc.chan.device_node);
2476 #ifdef CONFIG_DEBUG_FS
2477 static const char *pl08x_state_str(enum pl08x_dma_chan_state state)
2480 case PL08X_CHAN_IDLE:
2482 case PL08X_CHAN_RUNNING:
2484 case PL08X_CHAN_PAUSED:
2486 case PL08X_CHAN_WAITING:
2491 return "UNKNOWN STATE";
2494 static int pl08x_debugfs_show(struct seq_file *s, void *data)
2496 struct pl08x_driver_data *pl08x = s->private;
2497 struct pl08x_dma_chan *chan;
2498 struct pl08x_phy_chan *ch;
2499 unsigned long flags;
2502 seq_printf(s, "PL08x physical channels:\n");
2503 seq_printf(s, "CHANNEL:\tUSER:\n");
2504 seq_printf(s, "--------\t-----\n");
2505 for (i = 0; i < pl08x->vd->channels; i++) {
2506 struct pl08x_dma_chan *virt_chan;
2508 ch = &pl08x->phy_chans[i];
2510 spin_lock_irqsave(&ch->lock, flags);
2511 virt_chan = ch->serving;
2513 seq_printf(s, "%d\t\t%s%s\n",
2515 virt_chan ? virt_chan->name : "(none)",
2516 ch->locked ? " LOCKED" : "");
2518 spin_unlock_irqrestore(&ch->lock, flags);
2521 seq_printf(s, "\nPL08x virtual memcpy channels:\n");
2522 seq_printf(s, "CHANNEL:\tSTATE:\n");
2523 seq_printf(s, "--------\t------\n");
2524 list_for_each_entry(chan, &pl08x->memcpy.channels, vc.chan.device_node) {
2525 seq_printf(s, "%s\t\t%s\n", chan->name,
2526 pl08x_state_str(chan->state));
2529 if (pl08x->has_slave) {
2530 seq_printf(s, "\nPL08x virtual slave channels:\n");
2531 seq_printf(s, "CHANNEL:\tSTATE:\n");
2532 seq_printf(s, "--------\t------\n");
2533 list_for_each_entry(chan, &pl08x->slave.channels,
2534 vc.chan.device_node) {
2535 seq_printf(s, "%s\t\t%s\n", chan->name,
2536 pl08x_state_str(chan->state));
2543 DEFINE_SHOW_ATTRIBUTE(pl08x_debugfs);
2545 static void init_pl08x_debugfs(struct pl08x_driver_data *pl08x)
2547 /* Expose a simple debugfs interface to view all clocks */
2548 debugfs_create_file(dev_name(&pl08x->adev->dev), S_IFREG | S_IRUGO,
2549 NULL, pl08x, &pl08x_debugfs_fops);
2553 static inline void init_pl08x_debugfs(struct pl08x_driver_data *pl08x)
2559 static struct dma_chan *pl08x_find_chan_id(struct pl08x_driver_data *pl08x,
2562 struct pl08x_dma_chan *chan;
2564 /* Trying to get a slave channel from something with no slave support */
2565 if (!pl08x->has_slave)
2568 list_for_each_entry(chan, &pl08x->slave.channels, vc.chan.device_node) {
2569 if (chan->signal == id)
2570 return &chan->vc.chan;
2576 static struct dma_chan *pl08x_of_xlate(struct of_phandle_args *dma_spec,
2577 struct of_dma *ofdma)
2579 struct pl08x_driver_data *pl08x = ofdma->of_dma_data;
2580 struct dma_chan *dma_chan;
2581 struct pl08x_dma_chan *plchan;
2586 if (dma_spec->args_count != 2) {
2587 dev_err(&pl08x->adev->dev,
2588 "DMA channel translation requires two cells\n");
2592 dma_chan = pl08x_find_chan_id(pl08x, dma_spec->args[0]);
2594 dev_err(&pl08x->adev->dev,
2595 "DMA slave channel not found\n");
2599 plchan = to_pl08x_chan(dma_chan);
2600 dev_dbg(&pl08x->adev->dev,
2601 "translated channel for signal %d\n",
2604 /* Augment channel data for applicable AHB buses */
2605 plchan->cd->periph_buses = dma_spec->args[1];
2606 return dma_get_slave_channel(dma_chan);
2609 #ifdef CONFIG_SOC_STARFIVE_JH7110
2610 static int pl08x_of_probe(struct platform_device *adev,
2612 static int pl08x_of_probe(struct amba_device *adev,
2614 struct pl08x_driver_data *pl08x,
2615 struct device_node *np)
2617 struct pl08x_platform_data *pd;
2618 struct pl08x_channel_data *chanp = NULL;
2623 pd = devm_kzalloc(&adev->dev, sizeof(*pd), GFP_KERNEL);
2627 /* Eligible bus masters for fetching LLIs */
2628 if (of_property_read_bool(np, "lli-bus-interface-ahb1"))
2629 pd->lli_buses |= PL08X_AHB1;
2630 if (of_property_read_bool(np, "lli-bus-interface-ahb2"))
2631 pd->lli_buses |= PL08X_AHB2;
2632 if (!pd->lli_buses) {
2633 dev_info(&adev->dev, "no bus masters for LLIs stated, assume all\n");
2634 pd->lli_buses |= PL08X_AHB1 | PL08X_AHB2;
2637 /* Eligible bus masters for memory access */
2638 if (of_property_read_bool(np, "mem-bus-interface-ahb1"))
2639 pd->mem_buses |= PL08X_AHB1;
2640 if (of_property_read_bool(np, "mem-bus-interface-ahb2"))
2641 pd->mem_buses |= PL08X_AHB2;
2642 if (!pd->mem_buses) {
2643 dev_info(&adev->dev, "no bus masters for memory stated, assume all\n");
2644 pd->mem_buses |= PL08X_AHB1 | PL08X_AHB2;
2647 /* Parse the memcpy channel properties */
2648 ret = of_property_read_u32(np, "memcpy-burst-size", &val);
2650 dev_info(&adev->dev, "no memcpy burst size specified, using 1 byte\n");
2655 dev_err(&adev->dev, "illegal burst size for memcpy, set to 1\n");
2658 pd->memcpy_burst_size = PL08X_BURST_SZ_1;
2661 pd->memcpy_burst_size = PL08X_BURST_SZ_4;
2664 pd->memcpy_burst_size = PL08X_BURST_SZ_8;
2667 pd->memcpy_burst_size = PL08X_BURST_SZ_16;
2670 pd->memcpy_burst_size = PL08X_BURST_SZ_32;
2673 pd->memcpy_burst_size = PL08X_BURST_SZ_64;
2676 pd->memcpy_burst_size = PL08X_BURST_SZ_128;
2679 pd->memcpy_burst_size = PL08X_BURST_SZ_256;
2683 ret = of_property_read_u32(np, "memcpy-bus-width", &val);
2685 dev_info(&adev->dev, "no memcpy bus width specified, using 8 bits\n");
2690 dev_err(&adev->dev, "illegal bus width for memcpy, set to 8 bits\n");
2693 pd->memcpy_bus_width = PL08X_BUS_WIDTH_8_BITS;
2696 pd->memcpy_bus_width = PL08X_BUS_WIDTH_16_BITS;
2699 pd->memcpy_bus_width = PL08X_BUS_WIDTH_32_BITS;
2704 * Allocate channel data for all possible slave channels (one
2705 * for each possible signal), channels will then be allocated
2706 * for a device and have it's AHB interfaces set up at
2709 if (pl08x->vd->signals) {
2710 chanp = devm_kcalloc(&adev->dev,
2712 sizeof(struct pl08x_channel_data),
2717 pd->slave_channels = chanp;
2718 for (i = 0; i < pl08x->vd->signals; i++) {
2720 * chanp->periph_buses will be assigned at translation
2722 chanp->bus_id = kasprintf(GFP_KERNEL, "slave%d", i);
2725 pd->num_slave_channels = pl08x->vd->signals;
2730 return of_dma_controller_register(adev->dev.of_node, pl08x_of_xlate,
2734 #ifdef CONFIG_SOC_STARFIVE_JH7110
2735 static inline int pl08x_of_probe(struct platform_device *adev,
2737 static inline int pl08x_of_probe(struct amba_device *adev,
2739 struct pl08x_driver_data *pl08x,
2740 struct device_node *np)
2746 #ifdef CONFIG_SOC_STARFIVE_JH7110
2747 static int pl08x_probe(struct platform_device *adev) //, const struct amba_id *id)
2749 static int pl08x_probe(struct amba_device *adev, const struct amba_id *id)
2752 struct pl08x_driver_data *pl08x;
2753 struct device_node *np = adev->dev.of_node;
2755 #ifdef CONFIG_SOC_STARFIVE_JH7110
2756 struct vendor_data *vd;
2757 struct resource *res;
2760 struct vendor_data *vd = id->data;
2765 #ifndef CONFIG_SOC_STARFIVE_JH7110
2766 ret = amba_request_regions(adev, NULL);
2771 /* Ensure that we can do DMA */
2772 ret = dma_set_mask_and_coherent(&adev->dev, DMA_BIT_MASK(32));
2773 #ifdef CONFIG_SOC_STARFIVE_JH7110
2781 /* Create the driver state holder */
2782 pl08x = kzalloc(sizeof(*pl08x), GFP_KERNEL);
2785 #ifdef CONFIG_SOC_STARFIVE_JH7110
2792 /* Assign useful pointers to the driver state */
2794 #ifdef CONFIG_SOC_STARFIVE_JH7110
2795 vd = (struct vendor_data *)of_device_get_match_data(&adev->dev);
2801 #ifdef CONFIG_SOC_STARFIVE_JH7110
2802 res = platform_get_resource_byname(adev, IORESOURCE_MEM, "sec_dma");
2803 pl08x->base = devm_ioremap_resource(&adev->dev, res);
2805 pl08x->base = ioremap(adev->res.start, resource_size(&adev->res));
2809 goto out_no_ioremap;
2812 if (vd->ftdmac020) {
2815 val = readl(pl08x->base + FTDMAC020_REVISION);
2816 dev_info(&pl08x->adev->dev, "FTDMAC020 %d.%d rel %d\n",
2817 (val >> 16) & 0xff, (val >> 8) & 0xff, val & 0xff);
2818 val = readl(pl08x->base + FTDMAC020_FEATURE);
2819 dev_info(&pl08x->adev->dev, "FTDMAC020 %d channels, "
2820 "%s built-in bridge, %s, %s linked lists\n",
2822 (val & BIT(10)) ? "no" : "has",
2823 (val & BIT(9)) ? "AHB0 and AHB1" : "AHB0",
2824 (val & BIT(8)) ? "supports" : "does not support");
2826 /* Vendor data from feature register */
2827 if (!(val & BIT(8)))
2828 dev_warn(&pl08x->adev->dev,
2829 "linked lists not supported, required\n");
2830 vd->channels = (val >> 12) & 0x0f;
2831 vd->dualmaster = !!(val & BIT(9));
2834 /* Initialize memcpy engine */
2835 dma_cap_set(DMA_MEMCPY, pl08x->memcpy.cap_mask);
2836 pl08x->memcpy.dev = &adev->dev;
2837 pl08x->memcpy.device_free_chan_resources = pl08x_free_chan_resources;
2838 pl08x->memcpy.device_prep_dma_memcpy = pl08x_prep_dma_memcpy;
2839 pl08x->memcpy.device_prep_dma_interrupt = pl08x_prep_dma_interrupt;
2840 pl08x->memcpy.device_tx_status = pl08x_dma_tx_status;
2841 pl08x->memcpy.device_issue_pending = pl08x_issue_pending;
2842 pl08x->memcpy.device_config = pl08x_config;
2843 pl08x->memcpy.device_pause = pl08x_pause;
2844 pl08x->memcpy.device_resume = pl08x_resume;
2845 pl08x->memcpy.device_terminate_all = pl08x_terminate_all;
2846 pl08x->memcpy.device_synchronize = pl08x_synchronize;
2847 pl08x->memcpy.src_addr_widths = PL80X_DMA_BUSWIDTHS;
2848 pl08x->memcpy.dst_addr_widths = PL80X_DMA_BUSWIDTHS;
2849 pl08x->memcpy.directions = BIT(DMA_MEM_TO_MEM);
2850 pl08x->memcpy.residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
2852 pl08x->memcpy.copy_align = DMAENGINE_ALIGN_4_BYTES;
2856 * Initialize slave engine, if the block has no signals, that means
2857 * we have no slave support.
2860 pl08x->has_slave = true;
2861 dma_cap_set(DMA_SLAVE, pl08x->slave.cap_mask);
2862 dma_cap_set(DMA_CYCLIC, pl08x->slave.cap_mask);
2863 pl08x->slave.dev = &adev->dev;
2864 pl08x->slave.device_free_chan_resources =
2865 pl08x_free_chan_resources;
2866 pl08x->slave.device_prep_dma_interrupt =
2867 pl08x_prep_dma_interrupt;
2868 pl08x->slave.device_tx_status = pl08x_dma_tx_status;
2869 pl08x->slave.device_issue_pending = pl08x_issue_pending;
2870 pl08x->slave.device_prep_slave_sg = pl08x_prep_slave_sg;
2871 pl08x->slave.device_prep_dma_cyclic = pl08x_prep_dma_cyclic;
2872 pl08x->slave.device_config = pl08x_config;
2873 pl08x->slave.device_pause = pl08x_pause;
2874 pl08x->slave.device_resume = pl08x_resume;
2875 pl08x->slave.device_terminate_all = pl08x_terminate_all;
2876 pl08x->slave.device_synchronize = pl08x_synchronize;
2877 pl08x->slave.src_addr_widths = PL80X_DMA_BUSWIDTHS;
2878 pl08x->slave.dst_addr_widths = PL80X_DMA_BUSWIDTHS;
2879 pl08x->slave.directions =
2880 BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
2881 pl08x->slave.residue_granularity =
2882 DMA_RESIDUE_GRANULARITY_SEGMENT;
2885 /* Get the platform data */
2886 pl08x->pd = dev_get_platdata(&adev->dev);
2889 ret = pl08x_of_probe(adev, pl08x, np);
2891 goto out_no_platdata;
2893 dev_err(&adev->dev, "no platform data supplied\n");
2895 goto out_no_platdata;
2898 pl08x->slave.filter.map = pl08x->pd->slave_map;
2899 pl08x->slave.filter.mapcnt = pl08x->pd->slave_map_len;
2900 pl08x->slave.filter.fn = pl08x_filter_fn;
2903 /* By default, AHB1 only. If dualmaster, from platform */
2904 pl08x->lli_buses = PL08X_AHB1;
2905 pl08x->mem_buses = PL08X_AHB1;
2906 if (pl08x->vd->dualmaster) {
2907 pl08x->lli_buses = pl08x->pd->lli_buses;
2908 pl08x->mem_buses = pl08x->pd->mem_buses;
2912 pl08x->lli_words = PL080S_LLI_WORDS;
2914 pl08x->lli_words = PL080_LLI_WORDS;
2915 tsfr_size = MAX_NUM_TSFR_LLIS * pl08x->lli_words * sizeof(u32);
2917 /* A DMA memory pool for LLIs, align on 1-byte boundary */
2918 pl08x->pool = dma_pool_create(DRIVER_NAME, &pl08x->adev->dev,
2919 tsfr_size, PL08X_ALIGN, 0);
2922 goto out_no_lli_pool;
2925 /* Turn on the PL08x */
2926 pl08x_ensure_on(pl08x);
2928 /* Clear any pending interrupts */
2930 /* This variant has error IRQs in bits 16-19 */
2931 writel(0x0000FFFF, pl08x->base + PL080_ERR_CLEAR);
2933 writel(0x000000FF, pl08x->base + PL080_ERR_CLEAR);
2934 writel(0x000000FF, pl08x->base + PL080_TC_CLEAR);
2936 /* Attach the interrupt handler */
2937 #ifdef CONFIG_SOC_STARFIVE_JH7110
2938 irq = platform_get_irq(adev, 0);
2940 dev_err(&adev->dev, "Cannot get IRQ resource\n");
2944 ret = request_irq(irq, pl08x_irq, 0, DRIVER_NAME, pl08x);
2946 ret = request_irq(adev->irq[0], pl08x_irq, 0, DRIVER_NAME, pl08x);
2949 dev_err(&adev->dev, "%s failed to request interrupt %d\n",
2950 #ifdef CONFIG_SOC_STARFIVE_JH7110
2953 __func__, adev->irq[0]);
2958 /* Initialize physical channels */
2959 pl08x->phy_chans = kzalloc((vd->channels * sizeof(*pl08x->phy_chans)),
2961 if (!pl08x->phy_chans) {
2963 goto out_no_phychans;
2966 for (i = 0; i < vd->channels; i++) {
2967 struct pl08x_phy_chan *ch = &pl08x->phy_chans[i];
2970 ch->base = pl08x->base + PL080_Cx_BASE(i);
2971 if (vd->ftdmac020) {
2972 /* FTDMA020 has a special channel busy register */
2973 ch->reg_busy = ch->base + FTDMAC020_CH_BUSY;
2974 ch->reg_config = ch->base + FTDMAC020_CH_CFG;
2975 ch->reg_control = ch->base + FTDMAC020_CH_CSR;
2976 ch->reg_src = ch->base + FTDMAC020_CH_SRC_ADDR;
2977 ch->reg_dst = ch->base + FTDMAC020_CH_DST_ADDR;
2978 ch->reg_lli = ch->base + FTDMAC020_CH_LLP;
2979 ch->ftdmac020 = true;
2981 ch->reg_config = ch->base + vd->config_offset;
2982 ch->reg_control = ch->base + PL080_CH_CONTROL;
2983 ch->reg_src = ch->base + PL080_CH_SRC_ADDR;
2984 ch->reg_dst = ch->base + PL080_CH_DST_ADDR;
2985 ch->reg_lli = ch->base + PL080_CH_LLI;
2990 spin_lock_init(&ch->lock);
2993 * Nomadik variants can have channels that are locked
2994 * down for the secure world only. Lock up these channels
2995 * by perpetually serving a dummy virtual channel.
3000 val = readl(ch->reg_config);
3001 if (val & (PL080N_CONFIG_ITPROT | PL080N_CONFIG_SECPROT)) {
3002 dev_info(&adev->dev, "physical channel %d reserved for secure access only\n", i);
3007 dev_dbg(&adev->dev, "physical channel %d is %s\n",
3008 i, pl08x_phy_channel_busy(ch) ? "BUSY" : "FREE");
3011 /* Register as many memcpy channels as there are physical channels */
3012 ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->memcpy,
3013 pl08x->vd->channels, false);
3015 dev_warn(&pl08x->adev->dev,
3016 "%s failed to enumerate memcpy channels - %d\n",
3021 /* Register slave channels */
3022 if (pl08x->has_slave) {
3023 ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->slave,
3024 pl08x->pd->num_slave_channels, true);
3026 dev_warn(&pl08x->adev->dev,
3027 "%s failed to enumerate slave channels - %d\n",
3033 ret = dma_async_device_register(&pl08x->memcpy);
3035 dev_warn(&pl08x->adev->dev,
3036 "%s failed to register memcpy as an async device - %d\n",
3038 goto out_no_memcpy_reg;
3041 if (pl08x->has_slave) {
3042 ret = dma_async_device_register(&pl08x->slave);
3044 dev_warn(&pl08x->adev->dev,
3045 "%s failed to register slave as an async device - %d\n",
3047 goto out_no_slave_reg;
3051 #ifdef CONFIG_SOC_STARFIVE_JH7110
3052 platform_set_drvdata(adev, pl08x);
3054 amba_set_drvdata(adev, pl08x);
3056 init_pl08x_debugfs(pl08x);
3057 #ifdef CONFIG_SOC_STARFIVE_JH7110
3058 dev_dbg(&pl08x->adev->dev, "DMA: PL080 at 0x%08llx irq %d\n",
3059 (unsigned long long)res->start, irq);
3061 dev_info(&pl08x->adev->dev, "DMA: PL%03x%s rev%u at 0x%08llx irq %d\n",
3062 amba_part(adev), pl08x->vd->pl080s ? "s" : "", amba_rev(adev),
3063 (unsigned long long)adev->res.start, adev->irq[0]);
3069 dma_async_device_unregister(&pl08x->memcpy);
3071 if (pl08x->has_slave)
3072 pl08x_free_virtual_channels(&pl08x->slave);
3074 pl08x_free_virtual_channels(&pl08x->memcpy);
3076 kfree(pl08x->phy_chans);
3078 #ifdef CONFIG_SOC_STARFIVE_JH7110
3079 free_irq(irq, pl08x);
3081 free_irq(adev->irq[0], pl08x);
3084 dma_pool_destroy(pl08x->pool);
3087 iounmap(pl08x->base);
3091 #ifndef CONFIG_SOC_STARFIVE_JH7110
3093 amba_release_regions(adev);
3098 /* PL080 has 8 channels and the PL080 have just 2 */
3099 static struct vendor_data vendor_pl080 = {
3100 .config_offset = PL080_CH_CONFIG,
3104 .max_transfer_size = PL080_CONTROL_TRANSFER_SIZE_MASK,
3106 #ifdef CONFIG_SOC_STARFIVE_JH7110
3107 static const struct of_device_id jh7110_dma_ids[] = {
3108 { .compatible = "starfive,jh7110-pl080", .data = &vendor_pl080},
3111 MODULE_DEVICE_TABLE(of, jh7110_dma_ids);
3113 static struct platform_driver jh7110_pl08x_driver = {
3114 .probe = pl08x_probe,
3116 .name = DRIVER_NAME,
3117 .of_match_table = jh7110_dma_ids,
3121 module_platform_driver(jh7110_pl08x_driver);
3123 MODULE_LICENSE("GPL");
3124 MODULE_AUTHOR("Huan Feng <huan.feng@starfivetech.com>");
3126 static struct vendor_data vendor_nomadik = {
3127 .config_offset = PL080_CH_CONFIG,
3132 .max_transfer_size = PL080_CONTROL_TRANSFER_SIZE_MASK,
3135 static struct vendor_data vendor_pl080s = {
3136 .config_offset = PL080S_CH_CONFIG,
3140 .max_transfer_size = PL080S_CONTROL_TRANSFER_SIZE_MASK,
3143 static struct vendor_data vendor_pl081 = {
3144 .config_offset = PL080_CH_CONFIG,
3147 .dualmaster = false,
3148 .max_transfer_size = PL080_CONTROL_TRANSFER_SIZE_MASK,
3151 static struct vendor_data vendor_ftdmac020 = {
3152 .config_offset = PL080_CH_CONFIG,
3154 .max_transfer_size = PL080_CONTROL_TRANSFER_SIZE_MASK,
3157 static const struct amba_id pl08x_ids[] = {
3158 /* Samsung PL080S variant */
3162 .data = &vendor_pl080s,
3168 .data = &vendor_pl080,
3174 .data = &vendor_pl081,
3176 /* Nomadik 8815 PL080 variant */
3180 .data = &vendor_nomadik,
3182 /* Faraday Technology FTDMAC020 */
3186 .data = &vendor_ftdmac020,
3191 MODULE_DEVICE_TABLE(amba, pl08x_ids);
3193 static struct amba_driver pl08x_amba_driver = {
3194 .drv.name = DRIVER_NAME,
3195 .id_table = pl08x_ids,
3196 .probe = pl08x_probe,
3199 static int __init pl08x_init(void)
3202 retval = amba_driver_register(&pl08x_amba_driver);
3204 printk(KERN_WARNING DRIVER_NAME
3205 "failed to register as an AMBA device (%d)\n",
3209 subsys_initcall(pl08x_init);