xref: /OK3568_Linux_fs/kernel/drivers/dma/amba-pl08x.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (c) 2006 ARM Ltd.
4*4882a593Smuzhiyun  * Copyright (c) 2010 ST-Ericsson SA
5*4882a593Smuzhiyun  * Copyirght (c) 2017 Linaro Ltd.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Author: Peter Pearse <peter.pearse@arm.com>
8*4882a593Smuzhiyun  * Author: Linus Walleij <linus.walleij@linaro.org>
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * Documentation: ARM DDI 0196G == PL080
11*4882a593Smuzhiyun  * Documentation: ARM DDI 0218E == PL081
12*4882a593Smuzhiyun  * Documentation: S3C6410 User's Manual == PL080S
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * PL080 & PL081 both have 16 sets of DMA signals that can be routed to any
15*4882a593Smuzhiyun  * channel.
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  * The PL080 has 8 channels available for simultaneous use, and the PL081
18*4882a593Smuzhiyun  * has only two channels. So on these DMA controllers the number of channels
19*4882a593Smuzhiyun  * and the number of incoming DMA signals are two totally different things.
20*4882a593Smuzhiyun  * It is usually not possible to theoretically handle all physical signals,
21*4882a593Smuzhiyun  * so a multiplexing scheme with possible denial of use is necessary.
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * The PL080 has a dual bus master, PL081 has a single master.
24*4882a593Smuzhiyun  *
25*4882a593Smuzhiyun  * PL080S is a version modified by Samsung and used in S3C64xx SoCs.
26*4882a593Smuzhiyun  * It differs in following aspects:
27*4882a593Smuzhiyun  * - CH_CONFIG register at different offset,
28*4882a593Smuzhiyun  * - separate CH_CONTROL2 register for transfer size,
29*4882a593Smuzhiyun  * - bigger maximum transfer size,
30*4882a593Smuzhiyun  * - 8-word aligned LLI, instead of 4-word, due to extra CCTL2 word,
31*4882a593Smuzhiyun  * - no support for peripheral flow control.
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  * Memory to peripheral transfer may be visualized as
34*4882a593Smuzhiyun  *	Get data from memory to DMAC
35*4882a593Smuzhiyun  *	Until no data left
36*4882a593Smuzhiyun  *		On burst request from peripheral
37*4882a593Smuzhiyun  *			Destination burst from DMAC to peripheral
38*4882a593Smuzhiyun  *			Clear burst request
39*4882a593Smuzhiyun  *	Raise terminal count interrupt
40*4882a593Smuzhiyun  *
41*4882a593Smuzhiyun  * For peripherals with a FIFO:
42*4882a593Smuzhiyun  * Source      burst size == half the depth of the peripheral FIFO
43*4882a593Smuzhiyun  * Destination burst size == the depth of the peripheral FIFO
44*4882a593Smuzhiyun  *
45*4882a593Smuzhiyun  * (Bursts are irrelevant for mem to mem transfers - there are no burst
46*4882a593Smuzhiyun  * signals, the DMA controller will simply facilitate its AHB master.)
47*4882a593Smuzhiyun  *
48*4882a593Smuzhiyun  * ASSUMES default (little) endianness for DMA transfers
49*4882a593Smuzhiyun  *
50*4882a593Smuzhiyun  * The PL08x has two flow control settings:
51*4882a593Smuzhiyun  *  - DMAC flow control: the transfer size defines the number of transfers
52*4882a593Smuzhiyun  *    which occur for the current LLI entry, and the DMAC raises TC at the
53*4882a593Smuzhiyun  *    end of every LLI entry.  Observed behaviour shows the DMAC listening
54*4882a593Smuzhiyun  *    to both the BREQ and SREQ signals (contrary to documented),
55*4882a593Smuzhiyun  *    transferring data if either is active.  The LBREQ and LSREQ signals
56*4882a593Smuzhiyun  *    are ignored.
57*4882a593Smuzhiyun  *
58*4882a593Smuzhiyun  *  - Peripheral flow control: the transfer size is ignored (and should be
59*4882a593Smuzhiyun  *    zero).  The data is transferred from the current LLI entry, until
60*4882a593Smuzhiyun  *    after the final transfer signalled by LBREQ or LSREQ.  The DMAC
61*4882a593Smuzhiyun  *    will then move to the next LLI entry. Unsupported by PL080S.
62*4882a593Smuzhiyun  */
63*4882a593Smuzhiyun #include <linux/amba/bus.h>
64*4882a593Smuzhiyun #include <linux/amba/pl08x.h>
65*4882a593Smuzhiyun #include <linux/debugfs.h>
66*4882a593Smuzhiyun #include <linux/delay.h>
67*4882a593Smuzhiyun #include <linux/device.h>
68*4882a593Smuzhiyun #include <linux/dmaengine.h>
69*4882a593Smuzhiyun #include <linux/dmapool.h>
70*4882a593Smuzhiyun #include <linux/dma-mapping.h>
71*4882a593Smuzhiyun #include <linux/export.h>
72*4882a593Smuzhiyun #include <linux/init.h>
73*4882a593Smuzhiyun #include <linux/interrupt.h>
74*4882a593Smuzhiyun #include <linux/module.h>
75*4882a593Smuzhiyun #include <linux/of.h>
76*4882a593Smuzhiyun #include <linux/of_dma.h>
77*4882a593Smuzhiyun #include <linux/pm_runtime.h>
78*4882a593Smuzhiyun #include <linux/seq_file.h>
79*4882a593Smuzhiyun #include <linux/slab.h>
80*4882a593Smuzhiyun #include <linux/amba/pl080.h>
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun #include "dmaengine.h"
83*4882a593Smuzhiyun #include "virt-dma.h"
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun #define DRIVER_NAME	"pl08xdmac"
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun #define PL80X_DMA_BUSWIDTHS \
88*4882a593Smuzhiyun 	BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
89*4882a593Smuzhiyun 	BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
90*4882a593Smuzhiyun 	BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
91*4882a593Smuzhiyun 	BIT(DMA_SLAVE_BUSWIDTH_4_BYTES)
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun static struct amba_driver pl08x_amba_driver;
94*4882a593Smuzhiyun struct pl08x_driver_data;
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun /**
97*4882a593Smuzhiyun  * struct vendor_data - vendor-specific config parameters for PL08x derivatives
98*4882a593Smuzhiyun  * @config_offset: offset to the configuration register
99*4882a593Smuzhiyun  * @channels: the number of channels available in this variant
100*4882a593Smuzhiyun  * @signals: the number of request signals available from the hardware
101*4882a593Smuzhiyun  * @dualmaster: whether this version supports dual AHB masters or not.
102*4882a593Smuzhiyun  * @nomadik: whether this variant is a ST Microelectronics Nomadik, where the
103*4882a593Smuzhiyun  *	channels have Nomadik security extension bits that need to be checked
104*4882a593Smuzhiyun  *	for permission before use and some registers are missing
105*4882a593Smuzhiyun  * @pl080s: whether this variant is a Samsung PL080S, which has separate
106*4882a593Smuzhiyun  *	register and LLI word for transfer size.
107*4882a593Smuzhiyun  * @ftdmac020: whether this variant is a Faraday Technology FTDMAC020
108*4882a593Smuzhiyun  * @max_transfer_size: the maximum single element transfer size for this
109*4882a593Smuzhiyun  *	PL08x variant.
110*4882a593Smuzhiyun  */
111*4882a593Smuzhiyun struct vendor_data {
112*4882a593Smuzhiyun 	u8 config_offset;
113*4882a593Smuzhiyun 	u8 channels;
114*4882a593Smuzhiyun 	u8 signals;
115*4882a593Smuzhiyun 	bool dualmaster;
116*4882a593Smuzhiyun 	bool nomadik;
117*4882a593Smuzhiyun 	bool pl080s;
118*4882a593Smuzhiyun 	bool ftdmac020;
119*4882a593Smuzhiyun 	u32 max_transfer_size;
120*4882a593Smuzhiyun };
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun /**
123*4882a593Smuzhiyun  * struct pl08x_bus_data - information of source or destination
124*4882a593Smuzhiyun  * busses for a transfer
125*4882a593Smuzhiyun  * @addr: current address
126*4882a593Smuzhiyun  * @maxwidth: the maximum width of a transfer on this bus
127*4882a593Smuzhiyun  * @buswidth: the width of this bus in bytes: 1, 2 or 4
128*4882a593Smuzhiyun  */
129*4882a593Smuzhiyun struct pl08x_bus_data {
130*4882a593Smuzhiyun 	dma_addr_t addr;
131*4882a593Smuzhiyun 	u8 maxwidth;
132*4882a593Smuzhiyun 	u8 buswidth;
133*4882a593Smuzhiyun };
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun #define IS_BUS_ALIGNED(bus) IS_ALIGNED((bus)->addr, (bus)->buswidth)
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun /**
138*4882a593Smuzhiyun  * struct pl08x_phy_chan - holder for the physical channels
139*4882a593Smuzhiyun  * @id: physical index to this channel
140*4882a593Smuzhiyun  * @base: memory base address for this physical channel
141*4882a593Smuzhiyun  * @reg_config: configuration address for this physical channel
142*4882a593Smuzhiyun  * @reg_control: control address for this physical channel
143*4882a593Smuzhiyun  * @reg_src: transfer source address register
144*4882a593Smuzhiyun  * @reg_dst: transfer destination address register
145*4882a593Smuzhiyun  * @reg_lli: transfer LLI address register
146*4882a593Smuzhiyun  * @reg_busy: if the variant has a special per-channel busy register,
147*4882a593Smuzhiyun  * this contains a pointer to it
148*4882a593Smuzhiyun  * @lock: a lock to use when altering an instance of this struct
149*4882a593Smuzhiyun  * @serving: the virtual channel currently being served by this physical
150*4882a593Smuzhiyun  * channel
151*4882a593Smuzhiyun  * @locked: channel unavailable for the system, e.g. dedicated to secure
152*4882a593Smuzhiyun  * world
153*4882a593Smuzhiyun  * @ftdmac020: channel is on a FTDMAC020
154*4882a593Smuzhiyun  * @pl080s: channel is on a PL08s
155*4882a593Smuzhiyun  */
156*4882a593Smuzhiyun struct pl08x_phy_chan {
157*4882a593Smuzhiyun 	unsigned int id;
158*4882a593Smuzhiyun 	void __iomem *base;
159*4882a593Smuzhiyun 	void __iomem *reg_config;
160*4882a593Smuzhiyun 	void __iomem *reg_control;
161*4882a593Smuzhiyun 	void __iomem *reg_src;
162*4882a593Smuzhiyun 	void __iomem *reg_dst;
163*4882a593Smuzhiyun 	void __iomem *reg_lli;
164*4882a593Smuzhiyun 	void __iomem *reg_busy;
165*4882a593Smuzhiyun 	spinlock_t lock;
166*4882a593Smuzhiyun 	struct pl08x_dma_chan *serving;
167*4882a593Smuzhiyun 	bool locked;
168*4882a593Smuzhiyun 	bool ftdmac020;
169*4882a593Smuzhiyun 	bool pl080s;
170*4882a593Smuzhiyun };
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun /**
173*4882a593Smuzhiyun  * struct pl08x_sg - structure containing data per sg
174*4882a593Smuzhiyun  * @src_addr: src address of sg
175*4882a593Smuzhiyun  * @dst_addr: dst address of sg
176*4882a593Smuzhiyun  * @len: transfer len in bytes
177*4882a593Smuzhiyun  * @node: node for txd's dsg_list
178*4882a593Smuzhiyun  */
179*4882a593Smuzhiyun struct pl08x_sg {
180*4882a593Smuzhiyun 	dma_addr_t src_addr;
181*4882a593Smuzhiyun 	dma_addr_t dst_addr;
182*4882a593Smuzhiyun 	size_t len;
183*4882a593Smuzhiyun 	struct list_head node;
184*4882a593Smuzhiyun };
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun /**
187*4882a593Smuzhiyun  * struct pl08x_txd - wrapper for struct dma_async_tx_descriptor
188*4882a593Smuzhiyun  * @vd: virtual DMA descriptor
189*4882a593Smuzhiyun  * @dsg_list: list of children sg's
190*4882a593Smuzhiyun  * @llis_bus: DMA memory address (physical) start for the LLIs
191*4882a593Smuzhiyun  * @llis_va: virtual memory address start for the LLIs
192*4882a593Smuzhiyun  * @cctl: control reg values for current txd
193*4882a593Smuzhiyun  * @ccfg: config reg values for current txd
194*4882a593Smuzhiyun  * @done: this marks completed descriptors, which should not have their
195*4882a593Smuzhiyun  *   mux released.
196*4882a593Smuzhiyun  * @cyclic: indicate cyclic transfers
197*4882a593Smuzhiyun  */
198*4882a593Smuzhiyun struct pl08x_txd {
199*4882a593Smuzhiyun 	struct virt_dma_desc vd;
200*4882a593Smuzhiyun 	struct list_head dsg_list;
201*4882a593Smuzhiyun 	dma_addr_t llis_bus;
202*4882a593Smuzhiyun 	u32 *llis_va;
203*4882a593Smuzhiyun 	/* Default cctl value for LLIs */
204*4882a593Smuzhiyun 	u32 cctl;
205*4882a593Smuzhiyun 	/*
206*4882a593Smuzhiyun 	 * Settings to be put into the physical channel when we
207*4882a593Smuzhiyun 	 * trigger this txd.  Other registers are in llis_va[0].
208*4882a593Smuzhiyun 	 */
209*4882a593Smuzhiyun 	u32 ccfg;
210*4882a593Smuzhiyun 	bool done;
211*4882a593Smuzhiyun 	bool cyclic;
212*4882a593Smuzhiyun };
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun /**
215*4882a593Smuzhiyun  * enum pl08x_dma_chan_state - holds the PL08x specific virtual channel
216*4882a593Smuzhiyun  * states
217*4882a593Smuzhiyun  * @PL08X_CHAN_IDLE: the channel is idle
218*4882a593Smuzhiyun  * @PL08X_CHAN_RUNNING: the channel has allocated a physical transport
219*4882a593Smuzhiyun  * channel and is running a transfer on it
220*4882a593Smuzhiyun  * @PL08X_CHAN_PAUSED: the channel has allocated a physical transport
221*4882a593Smuzhiyun  * channel, but the transfer is currently paused
222*4882a593Smuzhiyun  * @PL08X_CHAN_WAITING: the channel is waiting for a physical transport
223*4882a593Smuzhiyun  * channel to become available (only pertains to memcpy channels)
224*4882a593Smuzhiyun  */
225*4882a593Smuzhiyun enum pl08x_dma_chan_state {
226*4882a593Smuzhiyun 	PL08X_CHAN_IDLE,
227*4882a593Smuzhiyun 	PL08X_CHAN_RUNNING,
228*4882a593Smuzhiyun 	PL08X_CHAN_PAUSED,
229*4882a593Smuzhiyun 	PL08X_CHAN_WAITING,
230*4882a593Smuzhiyun };
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun /**
233*4882a593Smuzhiyun  * struct pl08x_dma_chan - this structure wraps a DMA ENGINE channel
234*4882a593Smuzhiyun  * @vc: wrappped virtual channel
235*4882a593Smuzhiyun  * @phychan: the physical channel utilized by this channel, if there is one
236*4882a593Smuzhiyun  * @name: name of channel
237*4882a593Smuzhiyun  * @cd: channel platform data
238*4882a593Smuzhiyun  * @cfg: slave configuration
239*4882a593Smuzhiyun  * @at: active transaction on this channel
240*4882a593Smuzhiyun  * @host: a pointer to the host (internal use)
241*4882a593Smuzhiyun  * @state: whether the channel is idle, paused, running etc
242*4882a593Smuzhiyun  * @slave: whether this channel is a device (slave) or for memcpy
243*4882a593Smuzhiyun  * @signal: the physical DMA request signal which this channel is using
244*4882a593Smuzhiyun  * @mux_use: count of descriptors using this DMA request signal setting
245*4882a593Smuzhiyun  * @waiting_at: time in jiffies when this channel moved to waiting state
246*4882a593Smuzhiyun  */
247*4882a593Smuzhiyun struct pl08x_dma_chan {
248*4882a593Smuzhiyun 	struct virt_dma_chan vc;
249*4882a593Smuzhiyun 	struct pl08x_phy_chan *phychan;
250*4882a593Smuzhiyun 	const char *name;
251*4882a593Smuzhiyun 	struct pl08x_channel_data *cd;
252*4882a593Smuzhiyun 	struct dma_slave_config cfg;
253*4882a593Smuzhiyun 	struct pl08x_txd *at;
254*4882a593Smuzhiyun 	struct pl08x_driver_data *host;
255*4882a593Smuzhiyun 	enum pl08x_dma_chan_state state;
256*4882a593Smuzhiyun 	bool slave;
257*4882a593Smuzhiyun 	int signal;
258*4882a593Smuzhiyun 	unsigned mux_use;
259*4882a593Smuzhiyun 	unsigned long waiting_at;
260*4882a593Smuzhiyun };
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun /**
263*4882a593Smuzhiyun  * struct pl08x_driver_data - the local state holder for the PL08x
264*4882a593Smuzhiyun  * @slave: optional slave engine for this instance
265*4882a593Smuzhiyun  * @memcpy: memcpy engine for this instance
266*4882a593Smuzhiyun  * @has_slave: the PL08x has a slave engine (routed signals)
267*4882a593Smuzhiyun  * @base: virtual memory base (remapped) for the PL08x
268*4882a593Smuzhiyun  * @adev: the corresponding AMBA (PrimeCell) bus entry
269*4882a593Smuzhiyun  * @vd: vendor data for this PL08x variant
270*4882a593Smuzhiyun  * @pd: platform data passed in from the platform/machine
271*4882a593Smuzhiyun  * @phy_chans: array of data for the physical channels
272*4882a593Smuzhiyun  * @pool: a pool for the LLI descriptors
273*4882a593Smuzhiyun  * @lli_buses: bitmask to or in to LLI pointer selecting AHB port for LLI
274*4882a593Smuzhiyun  * fetches
275*4882a593Smuzhiyun  * @mem_buses: set to indicate memory transfers on AHB2.
276*4882a593Smuzhiyun  * @lli_words: how many words are used in each LLI item for this variant
277*4882a593Smuzhiyun  */
278*4882a593Smuzhiyun struct pl08x_driver_data {
279*4882a593Smuzhiyun 	struct dma_device slave;
280*4882a593Smuzhiyun 	struct dma_device memcpy;
281*4882a593Smuzhiyun 	bool has_slave;
282*4882a593Smuzhiyun 	void __iomem *base;
283*4882a593Smuzhiyun 	struct amba_device *adev;
284*4882a593Smuzhiyun 	const struct vendor_data *vd;
285*4882a593Smuzhiyun 	struct pl08x_platform_data *pd;
286*4882a593Smuzhiyun 	struct pl08x_phy_chan *phy_chans;
287*4882a593Smuzhiyun 	struct dma_pool *pool;
288*4882a593Smuzhiyun 	u8 lli_buses;
289*4882a593Smuzhiyun 	u8 mem_buses;
290*4882a593Smuzhiyun 	u8 lli_words;
291*4882a593Smuzhiyun };
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun /*
294*4882a593Smuzhiyun  * PL08X specific defines
295*4882a593Smuzhiyun  */
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun /* The order of words in an LLI. */
298*4882a593Smuzhiyun #define PL080_LLI_SRC		0
299*4882a593Smuzhiyun #define PL080_LLI_DST		1
300*4882a593Smuzhiyun #define PL080_LLI_LLI		2
301*4882a593Smuzhiyun #define PL080_LLI_CCTL		3
302*4882a593Smuzhiyun #define PL080S_LLI_CCTL2	4
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun /* Total words in an LLI. */
305*4882a593Smuzhiyun #define PL080_LLI_WORDS		4
306*4882a593Smuzhiyun #define PL080S_LLI_WORDS	8
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun /*
309*4882a593Smuzhiyun  * Number of LLIs in each LLI buffer allocated for one transfer
310*4882a593Smuzhiyun  * (maximum times we call dma_pool_alloc on this pool without freeing)
311*4882a593Smuzhiyun  */
312*4882a593Smuzhiyun #define MAX_NUM_TSFR_LLIS	512
313*4882a593Smuzhiyun #define PL08X_ALIGN		8
314*4882a593Smuzhiyun 
to_pl08x_chan(struct dma_chan * chan)315*4882a593Smuzhiyun static inline struct pl08x_dma_chan *to_pl08x_chan(struct dma_chan *chan)
316*4882a593Smuzhiyun {
317*4882a593Smuzhiyun 	return container_of(chan, struct pl08x_dma_chan, vc.chan);
318*4882a593Smuzhiyun }
319*4882a593Smuzhiyun 
to_pl08x_txd(struct dma_async_tx_descriptor * tx)320*4882a593Smuzhiyun static inline struct pl08x_txd *to_pl08x_txd(struct dma_async_tx_descriptor *tx)
321*4882a593Smuzhiyun {
322*4882a593Smuzhiyun 	return container_of(tx, struct pl08x_txd, vd.tx);
323*4882a593Smuzhiyun }
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun /*
326*4882a593Smuzhiyun  * Mux handling.
327*4882a593Smuzhiyun  *
328*4882a593Smuzhiyun  * This gives us the DMA request input to the PL08x primecell which the
329*4882a593Smuzhiyun  * peripheral described by the channel data will be routed to, possibly
330*4882a593Smuzhiyun  * via a board/SoC specific external MUX.  One important point to note
331*4882a593Smuzhiyun  * here is that this does not depend on the physical channel.
332*4882a593Smuzhiyun  */
pl08x_request_mux(struct pl08x_dma_chan * plchan)333*4882a593Smuzhiyun static int pl08x_request_mux(struct pl08x_dma_chan *plchan)
334*4882a593Smuzhiyun {
335*4882a593Smuzhiyun 	const struct pl08x_platform_data *pd = plchan->host->pd;
336*4882a593Smuzhiyun 	int ret;
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 	if (plchan->mux_use++ == 0 && pd->get_xfer_signal) {
339*4882a593Smuzhiyun 		ret = pd->get_xfer_signal(plchan->cd);
340*4882a593Smuzhiyun 		if (ret < 0) {
341*4882a593Smuzhiyun 			plchan->mux_use = 0;
342*4882a593Smuzhiyun 			return ret;
343*4882a593Smuzhiyun 		}
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 		plchan->signal = ret;
346*4882a593Smuzhiyun 	}
347*4882a593Smuzhiyun 	return 0;
348*4882a593Smuzhiyun }
349*4882a593Smuzhiyun 
pl08x_release_mux(struct pl08x_dma_chan * plchan)350*4882a593Smuzhiyun static void pl08x_release_mux(struct pl08x_dma_chan *plchan)
351*4882a593Smuzhiyun {
352*4882a593Smuzhiyun 	const struct pl08x_platform_data *pd = plchan->host->pd;
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 	if (plchan->signal >= 0) {
355*4882a593Smuzhiyun 		WARN_ON(plchan->mux_use == 0);
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 		if (--plchan->mux_use == 0 && pd->put_xfer_signal) {
358*4882a593Smuzhiyun 			pd->put_xfer_signal(plchan->cd, plchan->signal);
359*4882a593Smuzhiyun 			plchan->signal = -1;
360*4882a593Smuzhiyun 		}
361*4882a593Smuzhiyun 	}
362*4882a593Smuzhiyun }
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun /*
365*4882a593Smuzhiyun  * Physical channel handling
366*4882a593Smuzhiyun  */
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun /* Whether a certain channel is busy or not */
pl08x_phy_channel_busy(struct pl08x_phy_chan * ch)369*4882a593Smuzhiyun static int pl08x_phy_channel_busy(struct pl08x_phy_chan *ch)
370*4882a593Smuzhiyun {
371*4882a593Smuzhiyun 	unsigned int val;
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 	/* If we have a special busy register, take a shortcut */
374*4882a593Smuzhiyun 	if (ch->reg_busy) {
375*4882a593Smuzhiyun 		val = readl(ch->reg_busy);
376*4882a593Smuzhiyun 		return !!(val & BIT(ch->id));
377*4882a593Smuzhiyun 	}
378*4882a593Smuzhiyun 	val = readl(ch->reg_config);
379*4882a593Smuzhiyun 	return val & PL080_CONFIG_ACTIVE;
380*4882a593Smuzhiyun }
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun /*
383*4882a593Smuzhiyun  * pl08x_write_lli() - Write an LLI into the DMA controller.
384*4882a593Smuzhiyun  *
385*4882a593Smuzhiyun  * The PL08x derivatives support linked lists, but the first item of the
386*4882a593Smuzhiyun  * list containing the source, destination, control word and next LLI is
387*4882a593Smuzhiyun  * ignored. Instead the driver has to write those values directly into the
388*4882a593Smuzhiyun  * SRC, DST, LLI and control registers. On FTDMAC020 also the SIZE
389*4882a593Smuzhiyun  * register need to be set up for the first transfer.
390*4882a593Smuzhiyun  */
pl08x_write_lli(struct pl08x_driver_data * pl08x,struct pl08x_phy_chan * phychan,const u32 * lli,u32 ccfg)391*4882a593Smuzhiyun static void pl08x_write_lli(struct pl08x_driver_data *pl08x,
392*4882a593Smuzhiyun 		struct pl08x_phy_chan *phychan, const u32 *lli, u32 ccfg)
393*4882a593Smuzhiyun {
394*4882a593Smuzhiyun 	if (pl08x->vd->pl080s)
395*4882a593Smuzhiyun 		dev_vdbg(&pl08x->adev->dev,
396*4882a593Smuzhiyun 			"WRITE channel %d: csrc=0x%08x, cdst=0x%08x, "
397*4882a593Smuzhiyun 			"clli=0x%08x, cctl=0x%08x, cctl2=0x%08x, ccfg=0x%08x\n",
398*4882a593Smuzhiyun 			phychan->id, lli[PL080_LLI_SRC], lli[PL080_LLI_DST],
399*4882a593Smuzhiyun 			lli[PL080_LLI_LLI], lli[PL080_LLI_CCTL],
400*4882a593Smuzhiyun 			lli[PL080S_LLI_CCTL2], ccfg);
401*4882a593Smuzhiyun 	else
402*4882a593Smuzhiyun 		dev_vdbg(&pl08x->adev->dev,
403*4882a593Smuzhiyun 			"WRITE channel %d: csrc=0x%08x, cdst=0x%08x, "
404*4882a593Smuzhiyun 			"clli=0x%08x, cctl=0x%08x, ccfg=0x%08x\n",
405*4882a593Smuzhiyun 			phychan->id, lli[PL080_LLI_SRC], lli[PL080_LLI_DST],
406*4882a593Smuzhiyun 			lli[PL080_LLI_LLI], lli[PL080_LLI_CCTL], ccfg);
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 	writel_relaxed(lli[PL080_LLI_SRC], phychan->reg_src);
409*4882a593Smuzhiyun 	writel_relaxed(lli[PL080_LLI_DST], phychan->reg_dst);
410*4882a593Smuzhiyun 	writel_relaxed(lli[PL080_LLI_LLI], phychan->reg_lli);
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 	/*
413*4882a593Smuzhiyun 	 * The FTMAC020 has a different layout in the CCTL word of the LLI
414*4882a593Smuzhiyun 	 * and the CCTL register which is split in CSR and SIZE registers.
415*4882a593Smuzhiyun 	 * Convert the LLI item CCTL into the proper values to write into
416*4882a593Smuzhiyun 	 * the CSR and SIZE registers.
417*4882a593Smuzhiyun 	 */
418*4882a593Smuzhiyun 	if (phychan->ftdmac020) {
419*4882a593Smuzhiyun 		u32 llictl = lli[PL080_LLI_CCTL];
420*4882a593Smuzhiyun 		u32 val = 0;
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 		/* Write the transfer size (12 bits) to the size register */
423*4882a593Smuzhiyun 		writel_relaxed(llictl & FTDMAC020_LLI_TRANSFER_SIZE_MASK,
424*4882a593Smuzhiyun 			       phychan->base + FTDMAC020_CH_SIZE);
425*4882a593Smuzhiyun 		/*
426*4882a593Smuzhiyun 		 * Then write the control bits 28..16 to the control register
427*4882a593Smuzhiyun 		 * by shuffleing the bits around to where they are in the
428*4882a593Smuzhiyun 		 * main register. The mapping is as follows:
429*4882a593Smuzhiyun 		 * Bit 28: TC_MSK - mask on all except last LLI
430*4882a593Smuzhiyun 		 * Bit 27..25: SRC_WIDTH
431*4882a593Smuzhiyun 		 * Bit 24..22: DST_WIDTH
432*4882a593Smuzhiyun 		 * Bit 21..20: SRCAD_CTRL
433*4882a593Smuzhiyun 		 * Bit 19..17: DSTAD_CTRL
434*4882a593Smuzhiyun 		 * Bit 17: SRC_SEL
435*4882a593Smuzhiyun 		 * Bit 16: DST_SEL
436*4882a593Smuzhiyun 		 */
437*4882a593Smuzhiyun 		if (llictl & FTDMAC020_LLI_TC_MSK)
438*4882a593Smuzhiyun 			val |= FTDMAC020_CH_CSR_TC_MSK;
439*4882a593Smuzhiyun 		val |= ((llictl  & FTDMAC020_LLI_SRC_WIDTH_MSK) >>
440*4882a593Smuzhiyun 			(FTDMAC020_LLI_SRC_WIDTH_SHIFT -
441*4882a593Smuzhiyun 			 FTDMAC020_CH_CSR_SRC_WIDTH_SHIFT));
442*4882a593Smuzhiyun 		val |= ((llictl  & FTDMAC020_LLI_DST_WIDTH_MSK) >>
443*4882a593Smuzhiyun 			(FTDMAC020_LLI_DST_WIDTH_SHIFT -
444*4882a593Smuzhiyun 			 FTDMAC020_CH_CSR_DST_WIDTH_SHIFT));
445*4882a593Smuzhiyun 		val |= ((llictl  & FTDMAC020_LLI_SRCAD_CTL_MSK) >>
446*4882a593Smuzhiyun 			(FTDMAC020_LLI_SRCAD_CTL_SHIFT -
447*4882a593Smuzhiyun 			 FTDMAC020_CH_CSR_SRCAD_CTL_SHIFT));
448*4882a593Smuzhiyun 		val |= ((llictl  & FTDMAC020_LLI_DSTAD_CTL_MSK) >>
449*4882a593Smuzhiyun 			(FTDMAC020_LLI_DSTAD_CTL_SHIFT -
450*4882a593Smuzhiyun 			 FTDMAC020_CH_CSR_DSTAD_CTL_SHIFT));
451*4882a593Smuzhiyun 		if (llictl & FTDMAC020_LLI_SRC_SEL)
452*4882a593Smuzhiyun 			val |= FTDMAC020_CH_CSR_SRC_SEL;
453*4882a593Smuzhiyun 		if (llictl & FTDMAC020_LLI_DST_SEL)
454*4882a593Smuzhiyun 			val |= FTDMAC020_CH_CSR_DST_SEL;
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun 		/*
457*4882a593Smuzhiyun 		 * Set up the bits that exist in the CSR but are not
458*4882a593Smuzhiyun 		 * part the LLI, i.e. only gets written to the control
459*4882a593Smuzhiyun 		 * register right here.
460*4882a593Smuzhiyun 		 *
461*4882a593Smuzhiyun 		 * FIXME: do not just handle memcpy, also handle slave DMA.
462*4882a593Smuzhiyun 		 */
463*4882a593Smuzhiyun 		switch (pl08x->pd->memcpy_burst_size) {
464*4882a593Smuzhiyun 		default:
465*4882a593Smuzhiyun 		case PL08X_BURST_SZ_1:
466*4882a593Smuzhiyun 			val |= PL080_BSIZE_1 <<
467*4882a593Smuzhiyun 				FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
468*4882a593Smuzhiyun 			break;
469*4882a593Smuzhiyun 		case PL08X_BURST_SZ_4:
470*4882a593Smuzhiyun 			val |= PL080_BSIZE_4 <<
471*4882a593Smuzhiyun 				FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
472*4882a593Smuzhiyun 			break;
473*4882a593Smuzhiyun 		case PL08X_BURST_SZ_8:
474*4882a593Smuzhiyun 			val |= PL080_BSIZE_8 <<
475*4882a593Smuzhiyun 				FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
476*4882a593Smuzhiyun 			break;
477*4882a593Smuzhiyun 		case PL08X_BURST_SZ_16:
478*4882a593Smuzhiyun 			val |= PL080_BSIZE_16 <<
479*4882a593Smuzhiyun 				FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
480*4882a593Smuzhiyun 			break;
481*4882a593Smuzhiyun 		case PL08X_BURST_SZ_32:
482*4882a593Smuzhiyun 			val |= PL080_BSIZE_32 <<
483*4882a593Smuzhiyun 				FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
484*4882a593Smuzhiyun 			break;
485*4882a593Smuzhiyun 		case PL08X_BURST_SZ_64:
486*4882a593Smuzhiyun 			val |= PL080_BSIZE_64 <<
487*4882a593Smuzhiyun 				FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
488*4882a593Smuzhiyun 			break;
489*4882a593Smuzhiyun 		case PL08X_BURST_SZ_128:
490*4882a593Smuzhiyun 			val |= PL080_BSIZE_128 <<
491*4882a593Smuzhiyun 				FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
492*4882a593Smuzhiyun 			break;
493*4882a593Smuzhiyun 		case PL08X_BURST_SZ_256:
494*4882a593Smuzhiyun 			val |= PL080_BSIZE_256 <<
495*4882a593Smuzhiyun 				FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
496*4882a593Smuzhiyun 			break;
497*4882a593Smuzhiyun 		}
498*4882a593Smuzhiyun 
499*4882a593Smuzhiyun 		/* Protection flags */
500*4882a593Smuzhiyun 		if (pl08x->pd->memcpy_prot_buff)
501*4882a593Smuzhiyun 			val |= FTDMAC020_CH_CSR_PROT2;
502*4882a593Smuzhiyun 		if (pl08x->pd->memcpy_prot_cache)
503*4882a593Smuzhiyun 			val |= FTDMAC020_CH_CSR_PROT3;
504*4882a593Smuzhiyun 		/* We are the kernel, so we are in privileged mode */
505*4882a593Smuzhiyun 		val |= FTDMAC020_CH_CSR_PROT1;
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 		writel_relaxed(val, phychan->reg_control);
508*4882a593Smuzhiyun 	} else {
509*4882a593Smuzhiyun 		/* Bits are just identical */
510*4882a593Smuzhiyun 		writel_relaxed(lli[PL080_LLI_CCTL], phychan->reg_control);
511*4882a593Smuzhiyun 	}
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 	/* Second control word on the PL080s */
514*4882a593Smuzhiyun 	if (pl08x->vd->pl080s)
515*4882a593Smuzhiyun 		writel_relaxed(lli[PL080S_LLI_CCTL2],
516*4882a593Smuzhiyun 				phychan->base + PL080S_CH_CONTROL2);
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun 	writel(ccfg, phychan->reg_config);
519*4882a593Smuzhiyun }
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun /*
522*4882a593Smuzhiyun  * Set the initial DMA register values i.e. those for the first LLI
523*4882a593Smuzhiyun  * The next LLI pointer and the configuration interrupt bit have
524*4882a593Smuzhiyun  * been set when the LLIs were constructed.  Poke them into the hardware
525*4882a593Smuzhiyun  * and start the transfer.
526*4882a593Smuzhiyun  */
pl08x_start_next_txd(struct pl08x_dma_chan * plchan)527*4882a593Smuzhiyun static void pl08x_start_next_txd(struct pl08x_dma_chan *plchan)
528*4882a593Smuzhiyun {
529*4882a593Smuzhiyun 	struct pl08x_driver_data *pl08x = plchan->host;
530*4882a593Smuzhiyun 	struct pl08x_phy_chan *phychan = plchan->phychan;
531*4882a593Smuzhiyun 	struct virt_dma_desc *vd = vchan_next_desc(&plchan->vc);
532*4882a593Smuzhiyun 	struct pl08x_txd *txd = to_pl08x_txd(&vd->tx);
533*4882a593Smuzhiyun 	u32 val;
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun 	list_del(&txd->vd.node);
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 	plchan->at = txd;
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun 	/* Wait for channel inactive */
540*4882a593Smuzhiyun 	while (pl08x_phy_channel_busy(phychan))
541*4882a593Smuzhiyun 		cpu_relax();
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 	pl08x_write_lli(pl08x, phychan, &txd->llis_va[0], txd->ccfg);
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 	/* Enable the DMA channel */
546*4882a593Smuzhiyun 	/* Do not access config register until channel shows as disabled */
547*4882a593Smuzhiyun 	while (readl(pl08x->base + PL080_EN_CHAN) & BIT(phychan->id))
548*4882a593Smuzhiyun 		cpu_relax();
549*4882a593Smuzhiyun 
550*4882a593Smuzhiyun 	/* Do not access config register until channel shows as inactive */
551*4882a593Smuzhiyun 	if (phychan->ftdmac020) {
552*4882a593Smuzhiyun 		val = readl(phychan->reg_config);
553*4882a593Smuzhiyun 		while (val & FTDMAC020_CH_CFG_BUSY)
554*4882a593Smuzhiyun 			val = readl(phychan->reg_config);
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun 		val = readl(phychan->reg_control);
557*4882a593Smuzhiyun 		while (val & FTDMAC020_CH_CSR_EN)
558*4882a593Smuzhiyun 			val = readl(phychan->reg_control);
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun 		writel(val | FTDMAC020_CH_CSR_EN,
561*4882a593Smuzhiyun 		       phychan->reg_control);
562*4882a593Smuzhiyun 	} else {
563*4882a593Smuzhiyun 		val = readl(phychan->reg_config);
564*4882a593Smuzhiyun 		while ((val & PL080_CONFIG_ACTIVE) ||
565*4882a593Smuzhiyun 		       (val & PL080_CONFIG_ENABLE))
566*4882a593Smuzhiyun 			val = readl(phychan->reg_config);
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun 		writel(val | PL080_CONFIG_ENABLE, phychan->reg_config);
569*4882a593Smuzhiyun 	}
570*4882a593Smuzhiyun }
571*4882a593Smuzhiyun 
572*4882a593Smuzhiyun /*
573*4882a593Smuzhiyun  * Pause the channel by setting the HALT bit.
574*4882a593Smuzhiyun  *
575*4882a593Smuzhiyun  * For M->P transfers, pause the DMAC first and then stop the peripheral -
576*4882a593Smuzhiyun  * the FIFO can only drain if the peripheral is still requesting data.
577*4882a593Smuzhiyun  * (note: this can still timeout if the DMAC FIFO never drains of data.)
578*4882a593Smuzhiyun  *
579*4882a593Smuzhiyun  * For P->M transfers, disable the peripheral first to stop it filling
580*4882a593Smuzhiyun  * the DMAC FIFO, and then pause the DMAC.
581*4882a593Smuzhiyun  */
pl08x_pause_phy_chan(struct pl08x_phy_chan * ch)582*4882a593Smuzhiyun static void pl08x_pause_phy_chan(struct pl08x_phy_chan *ch)
583*4882a593Smuzhiyun {
584*4882a593Smuzhiyun 	u32 val;
585*4882a593Smuzhiyun 	int timeout;
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun 	if (ch->ftdmac020) {
588*4882a593Smuzhiyun 		/* Use the enable bit on the FTDMAC020 */
589*4882a593Smuzhiyun 		val = readl(ch->reg_control);
590*4882a593Smuzhiyun 		val &= ~FTDMAC020_CH_CSR_EN;
591*4882a593Smuzhiyun 		writel(val, ch->reg_control);
592*4882a593Smuzhiyun 		return;
593*4882a593Smuzhiyun 	}
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun 	/* Set the HALT bit and wait for the FIFO to drain */
596*4882a593Smuzhiyun 	val = readl(ch->reg_config);
597*4882a593Smuzhiyun 	val |= PL080_CONFIG_HALT;
598*4882a593Smuzhiyun 	writel(val, ch->reg_config);
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun 	/* Wait for channel inactive */
601*4882a593Smuzhiyun 	for (timeout = 1000; timeout; timeout--) {
602*4882a593Smuzhiyun 		if (!pl08x_phy_channel_busy(ch))
603*4882a593Smuzhiyun 			break;
604*4882a593Smuzhiyun 		udelay(1);
605*4882a593Smuzhiyun 	}
606*4882a593Smuzhiyun 	if (pl08x_phy_channel_busy(ch))
607*4882a593Smuzhiyun 		pr_err("pl08x: channel%u timeout waiting for pause\n", ch->id);
608*4882a593Smuzhiyun }
609*4882a593Smuzhiyun 
pl08x_resume_phy_chan(struct pl08x_phy_chan * ch)610*4882a593Smuzhiyun static void pl08x_resume_phy_chan(struct pl08x_phy_chan *ch)
611*4882a593Smuzhiyun {
612*4882a593Smuzhiyun 	u32 val;
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun 	/* Use the enable bit on the FTDMAC020 */
615*4882a593Smuzhiyun 	if (ch->ftdmac020) {
616*4882a593Smuzhiyun 		val = readl(ch->reg_control);
617*4882a593Smuzhiyun 		val |= FTDMAC020_CH_CSR_EN;
618*4882a593Smuzhiyun 		writel(val, ch->reg_control);
619*4882a593Smuzhiyun 		return;
620*4882a593Smuzhiyun 	}
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun 	/* Clear the HALT bit */
623*4882a593Smuzhiyun 	val = readl(ch->reg_config);
624*4882a593Smuzhiyun 	val &= ~PL080_CONFIG_HALT;
625*4882a593Smuzhiyun 	writel(val, ch->reg_config);
626*4882a593Smuzhiyun }
627*4882a593Smuzhiyun 
628*4882a593Smuzhiyun /*
629*4882a593Smuzhiyun  * pl08x_terminate_phy_chan() stops the channel, clears the FIFO and
630*4882a593Smuzhiyun  * clears any pending interrupt status.  This should not be used for
631*4882a593Smuzhiyun  * an on-going transfer, but as a method of shutting down a channel
632*4882a593Smuzhiyun  * (eg, when it's no longer used) or terminating a transfer.
633*4882a593Smuzhiyun  */
pl08x_terminate_phy_chan(struct pl08x_driver_data * pl08x,struct pl08x_phy_chan * ch)634*4882a593Smuzhiyun static void pl08x_terminate_phy_chan(struct pl08x_driver_data *pl08x,
635*4882a593Smuzhiyun 	struct pl08x_phy_chan *ch)
636*4882a593Smuzhiyun {
637*4882a593Smuzhiyun 	u32 val;
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun 	/* The layout for the FTDMAC020 is different */
640*4882a593Smuzhiyun 	if (ch->ftdmac020) {
641*4882a593Smuzhiyun 		/* Disable all interrupts */
642*4882a593Smuzhiyun 		val = readl(ch->reg_config);
643*4882a593Smuzhiyun 		val |= (FTDMAC020_CH_CFG_INT_ABT_MASK |
644*4882a593Smuzhiyun 			FTDMAC020_CH_CFG_INT_ERR_MASK |
645*4882a593Smuzhiyun 			FTDMAC020_CH_CFG_INT_TC_MASK);
646*4882a593Smuzhiyun 		writel(val, ch->reg_config);
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 		/* Abort and disable channel */
649*4882a593Smuzhiyun 		val = readl(ch->reg_control);
650*4882a593Smuzhiyun 		val &= ~FTDMAC020_CH_CSR_EN;
651*4882a593Smuzhiyun 		val |= FTDMAC020_CH_CSR_ABT;
652*4882a593Smuzhiyun 		writel(val, ch->reg_control);
653*4882a593Smuzhiyun 
654*4882a593Smuzhiyun 		/* Clear ABT and ERR interrupt flags */
655*4882a593Smuzhiyun 		writel(BIT(ch->id) | BIT(ch->id + 16),
656*4882a593Smuzhiyun 		       pl08x->base + PL080_ERR_CLEAR);
657*4882a593Smuzhiyun 		writel(BIT(ch->id), pl08x->base + PL080_TC_CLEAR);
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun 		return;
660*4882a593Smuzhiyun 	}
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun 	val = readl(ch->reg_config);
663*4882a593Smuzhiyun 	val &= ~(PL080_CONFIG_ENABLE | PL080_CONFIG_ERR_IRQ_MASK |
664*4882a593Smuzhiyun 		 PL080_CONFIG_TC_IRQ_MASK);
665*4882a593Smuzhiyun 	writel(val, ch->reg_config);
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun 	writel(BIT(ch->id), pl08x->base + PL080_ERR_CLEAR);
668*4882a593Smuzhiyun 	writel(BIT(ch->id), pl08x->base + PL080_TC_CLEAR);
669*4882a593Smuzhiyun }
670*4882a593Smuzhiyun 
get_bytes_in_phy_channel(struct pl08x_phy_chan * ch)671*4882a593Smuzhiyun static u32 get_bytes_in_phy_channel(struct pl08x_phy_chan *ch)
672*4882a593Smuzhiyun {
673*4882a593Smuzhiyun 	u32 val;
674*4882a593Smuzhiyun 	u32 bytes;
675*4882a593Smuzhiyun 
676*4882a593Smuzhiyun 	if (ch->ftdmac020) {
677*4882a593Smuzhiyun 		bytes = readl(ch->base + FTDMAC020_CH_SIZE);
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun 		val = readl(ch->reg_control);
680*4882a593Smuzhiyun 		val &= FTDMAC020_CH_CSR_SRC_WIDTH_MSK;
681*4882a593Smuzhiyun 		val >>= FTDMAC020_CH_CSR_SRC_WIDTH_SHIFT;
682*4882a593Smuzhiyun 	} else if (ch->pl080s) {
683*4882a593Smuzhiyun 		val = readl(ch->base + PL080S_CH_CONTROL2);
684*4882a593Smuzhiyun 		bytes = val & PL080S_CONTROL_TRANSFER_SIZE_MASK;
685*4882a593Smuzhiyun 
686*4882a593Smuzhiyun 		val = readl(ch->reg_control);
687*4882a593Smuzhiyun 		val &= PL080_CONTROL_SWIDTH_MASK;
688*4882a593Smuzhiyun 		val >>= PL080_CONTROL_SWIDTH_SHIFT;
689*4882a593Smuzhiyun 	} else {
690*4882a593Smuzhiyun 		/* Plain PL08x */
691*4882a593Smuzhiyun 		val = readl(ch->reg_control);
692*4882a593Smuzhiyun 		bytes = val & PL080_CONTROL_TRANSFER_SIZE_MASK;
693*4882a593Smuzhiyun 
694*4882a593Smuzhiyun 		val &= PL080_CONTROL_SWIDTH_MASK;
695*4882a593Smuzhiyun 		val >>= PL080_CONTROL_SWIDTH_SHIFT;
696*4882a593Smuzhiyun 	}
697*4882a593Smuzhiyun 
698*4882a593Smuzhiyun 	switch (val) {
699*4882a593Smuzhiyun 	case PL080_WIDTH_8BIT:
700*4882a593Smuzhiyun 		break;
701*4882a593Smuzhiyun 	case PL080_WIDTH_16BIT:
702*4882a593Smuzhiyun 		bytes *= 2;
703*4882a593Smuzhiyun 		break;
704*4882a593Smuzhiyun 	case PL080_WIDTH_32BIT:
705*4882a593Smuzhiyun 		bytes *= 4;
706*4882a593Smuzhiyun 		break;
707*4882a593Smuzhiyun 	}
708*4882a593Smuzhiyun 	return bytes;
709*4882a593Smuzhiyun }
710*4882a593Smuzhiyun 
get_bytes_in_lli(struct pl08x_phy_chan * ch,const u32 * llis_va)711*4882a593Smuzhiyun static u32 get_bytes_in_lli(struct pl08x_phy_chan *ch, const u32 *llis_va)
712*4882a593Smuzhiyun {
713*4882a593Smuzhiyun 	u32 val;
714*4882a593Smuzhiyun 	u32 bytes;
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun 	if (ch->ftdmac020) {
717*4882a593Smuzhiyun 		val = llis_va[PL080_LLI_CCTL];
718*4882a593Smuzhiyun 		bytes = val & FTDMAC020_LLI_TRANSFER_SIZE_MASK;
719*4882a593Smuzhiyun 
720*4882a593Smuzhiyun 		val = llis_va[PL080_LLI_CCTL];
721*4882a593Smuzhiyun 		val &= FTDMAC020_LLI_SRC_WIDTH_MSK;
722*4882a593Smuzhiyun 		val >>= FTDMAC020_LLI_SRC_WIDTH_SHIFT;
723*4882a593Smuzhiyun 	} else if (ch->pl080s) {
724*4882a593Smuzhiyun 		val = llis_va[PL080S_LLI_CCTL2];
725*4882a593Smuzhiyun 		bytes = val & PL080S_CONTROL_TRANSFER_SIZE_MASK;
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun 		val = llis_va[PL080_LLI_CCTL];
728*4882a593Smuzhiyun 		val &= PL080_CONTROL_SWIDTH_MASK;
729*4882a593Smuzhiyun 		val >>= PL080_CONTROL_SWIDTH_SHIFT;
730*4882a593Smuzhiyun 	} else {
731*4882a593Smuzhiyun 		/* Plain PL08x */
732*4882a593Smuzhiyun 		val = llis_va[PL080_LLI_CCTL];
733*4882a593Smuzhiyun 		bytes = val & PL080_CONTROL_TRANSFER_SIZE_MASK;
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 		val &= PL080_CONTROL_SWIDTH_MASK;
736*4882a593Smuzhiyun 		val >>= PL080_CONTROL_SWIDTH_SHIFT;
737*4882a593Smuzhiyun 	}
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 	switch (val) {
740*4882a593Smuzhiyun 	case PL080_WIDTH_8BIT:
741*4882a593Smuzhiyun 		break;
742*4882a593Smuzhiyun 	case PL080_WIDTH_16BIT:
743*4882a593Smuzhiyun 		bytes *= 2;
744*4882a593Smuzhiyun 		break;
745*4882a593Smuzhiyun 	case PL080_WIDTH_32BIT:
746*4882a593Smuzhiyun 		bytes *= 4;
747*4882a593Smuzhiyun 		break;
748*4882a593Smuzhiyun 	}
749*4882a593Smuzhiyun 	return bytes;
750*4882a593Smuzhiyun }
751*4882a593Smuzhiyun 
752*4882a593Smuzhiyun /* The channel should be paused when calling this */
pl08x_getbytes_chan(struct pl08x_dma_chan * plchan)753*4882a593Smuzhiyun static u32 pl08x_getbytes_chan(struct pl08x_dma_chan *plchan)
754*4882a593Smuzhiyun {
755*4882a593Smuzhiyun 	struct pl08x_driver_data *pl08x = plchan->host;
756*4882a593Smuzhiyun 	const u32 *llis_va, *llis_va_limit;
757*4882a593Smuzhiyun 	struct pl08x_phy_chan *ch;
758*4882a593Smuzhiyun 	dma_addr_t llis_bus;
759*4882a593Smuzhiyun 	struct pl08x_txd *txd;
760*4882a593Smuzhiyun 	u32 llis_max_words;
761*4882a593Smuzhiyun 	size_t bytes;
762*4882a593Smuzhiyun 	u32 clli;
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun 	ch = plchan->phychan;
765*4882a593Smuzhiyun 	txd = plchan->at;
766*4882a593Smuzhiyun 
767*4882a593Smuzhiyun 	if (!ch || !txd)
768*4882a593Smuzhiyun 		return 0;
769*4882a593Smuzhiyun 
770*4882a593Smuzhiyun 	/*
771*4882a593Smuzhiyun 	 * Follow the LLIs to get the number of remaining
772*4882a593Smuzhiyun 	 * bytes in the currently active transaction.
773*4882a593Smuzhiyun 	 */
774*4882a593Smuzhiyun 	clli = readl(ch->reg_lli) & ~PL080_LLI_LM_AHB2;
775*4882a593Smuzhiyun 
776*4882a593Smuzhiyun 	/* First get the remaining bytes in the active transfer */
777*4882a593Smuzhiyun 	bytes = get_bytes_in_phy_channel(ch);
778*4882a593Smuzhiyun 
779*4882a593Smuzhiyun 	if (!clli)
780*4882a593Smuzhiyun 		return bytes;
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 	llis_va = txd->llis_va;
783*4882a593Smuzhiyun 	llis_bus = txd->llis_bus;
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun 	llis_max_words = pl08x->lli_words * MAX_NUM_TSFR_LLIS;
786*4882a593Smuzhiyun 	BUG_ON(clli < llis_bus || clli >= llis_bus +
787*4882a593Smuzhiyun 						sizeof(u32) * llis_max_words);
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun 	/*
790*4882a593Smuzhiyun 	 * Locate the next LLI - as this is an array,
791*4882a593Smuzhiyun 	 * it's simple maths to find.
792*4882a593Smuzhiyun 	 */
793*4882a593Smuzhiyun 	llis_va += (clli - llis_bus) / sizeof(u32);
794*4882a593Smuzhiyun 
795*4882a593Smuzhiyun 	llis_va_limit = llis_va + llis_max_words;
796*4882a593Smuzhiyun 
797*4882a593Smuzhiyun 	for (; llis_va < llis_va_limit; llis_va += pl08x->lli_words) {
798*4882a593Smuzhiyun 		bytes += get_bytes_in_lli(ch, llis_va);
799*4882a593Smuzhiyun 
800*4882a593Smuzhiyun 		/*
801*4882a593Smuzhiyun 		 * A LLI pointer going backward terminates the LLI list
802*4882a593Smuzhiyun 		 */
803*4882a593Smuzhiyun 		if (llis_va[PL080_LLI_LLI] <= clli)
804*4882a593Smuzhiyun 			break;
805*4882a593Smuzhiyun 	}
806*4882a593Smuzhiyun 
807*4882a593Smuzhiyun 	return bytes;
808*4882a593Smuzhiyun }
809*4882a593Smuzhiyun 
810*4882a593Smuzhiyun /*
811*4882a593Smuzhiyun  * Allocate a physical channel for a virtual channel
812*4882a593Smuzhiyun  *
813*4882a593Smuzhiyun  * Try to locate a physical channel to be used for this transfer. If all
814*4882a593Smuzhiyun  * are taken return NULL and the requester will have to cope by using
815*4882a593Smuzhiyun  * some fallback PIO mode or retrying later.
816*4882a593Smuzhiyun  */
817*4882a593Smuzhiyun static struct pl08x_phy_chan *
pl08x_get_phy_channel(struct pl08x_driver_data * pl08x,struct pl08x_dma_chan * virt_chan)818*4882a593Smuzhiyun pl08x_get_phy_channel(struct pl08x_driver_data *pl08x,
819*4882a593Smuzhiyun 		      struct pl08x_dma_chan *virt_chan)
820*4882a593Smuzhiyun {
821*4882a593Smuzhiyun 	struct pl08x_phy_chan *ch = NULL;
822*4882a593Smuzhiyun 	unsigned long flags;
823*4882a593Smuzhiyun 	int i;
824*4882a593Smuzhiyun 
825*4882a593Smuzhiyun 	for (i = 0; i < pl08x->vd->channels; i++) {
826*4882a593Smuzhiyun 		ch = &pl08x->phy_chans[i];
827*4882a593Smuzhiyun 
828*4882a593Smuzhiyun 		spin_lock_irqsave(&ch->lock, flags);
829*4882a593Smuzhiyun 
830*4882a593Smuzhiyun 		if (!ch->locked && !ch->serving) {
831*4882a593Smuzhiyun 			ch->serving = virt_chan;
832*4882a593Smuzhiyun 			spin_unlock_irqrestore(&ch->lock, flags);
833*4882a593Smuzhiyun 			break;
834*4882a593Smuzhiyun 		}
835*4882a593Smuzhiyun 
836*4882a593Smuzhiyun 		spin_unlock_irqrestore(&ch->lock, flags);
837*4882a593Smuzhiyun 	}
838*4882a593Smuzhiyun 
839*4882a593Smuzhiyun 	if (i == pl08x->vd->channels) {
840*4882a593Smuzhiyun 		/* No physical channel available, cope with it */
841*4882a593Smuzhiyun 		return NULL;
842*4882a593Smuzhiyun 	}
843*4882a593Smuzhiyun 
844*4882a593Smuzhiyun 	return ch;
845*4882a593Smuzhiyun }
846*4882a593Smuzhiyun 
847*4882a593Smuzhiyun /* Mark the physical channel as free.  Note, this write is atomic. */
pl08x_put_phy_channel(struct pl08x_driver_data * pl08x,struct pl08x_phy_chan * ch)848*4882a593Smuzhiyun static inline void pl08x_put_phy_channel(struct pl08x_driver_data *pl08x,
849*4882a593Smuzhiyun 					 struct pl08x_phy_chan *ch)
850*4882a593Smuzhiyun {
851*4882a593Smuzhiyun 	ch->serving = NULL;
852*4882a593Smuzhiyun }
853*4882a593Smuzhiyun 
854*4882a593Smuzhiyun /*
855*4882a593Smuzhiyun  * Try to allocate a physical channel.  When successful, assign it to
856*4882a593Smuzhiyun  * this virtual channel, and initiate the next descriptor.  The
857*4882a593Smuzhiyun  * virtual channel lock must be held at this point.
858*4882a593Smuzhiyun  */
pl08x_phy_alloc_and_start(struct pl08x_dma_chan * plchan)859*4882a593Smuzhiyun static void pl08x_phy_alloc_and_start(struct pl08x_dma_chan *plchan)
860*4882a593Smuzhiyun {
861*4882a593Smuzhiyun 	struct pl08x_driver_data *pl08x = plchan->host;
862*4882a593Smuzhiyun 	struct pl08x_phy_chan *ch;
863*4882a593Smuzhiyun 
864*4882a593Smuzhiyun 	ch = pl08x_get_phy_channel(pl08x, plchan);
865*4882a593Smuzhiyun 	if (!ch) {
866*4882a593Smuzhiyun 		dev_dbg(&pl08x->adev->dev, "no physical channel available for xfer on %s\n", plchan->name);
867*4882a593Smuzhiyun 		plchan->state = PL08X_CHAN_WAITING;
868*4882a593Smuzhiyun 		plchan->waiting_at = jiffies;
869*4882a593Smuzhiyun 		return;
870*4882a593Smuzhiyun 	}
871*4882a593Smuzhiyun 
872*4882a593Smuzhiyun 	dev_dbg(&pl08x->adev->dev, "allocated physical channel %d for xfer on %s\n",
873*4882a593Smuzhiyun 		ch->id, plchan->name);
874*4882a593Smuzhiyun 
875*4882a593Smuzhiyun 	plchan->phychan = ch;
876*4882a593Smuzhiyun 	plchan->state = PL08X_CHAN_RUNNING;
877*4882a593Smuzhiyun 	pl08x_start_next_txd(plchan);
878*4882a593Smuzhiyun }
879*4882a593Smuzhiyun 
pl08x_phy_reassign_start(struct pl08x_phy_chan * ch,struct pl08x_dma_chan * plchan)880*4882a593Smuzhiyun static void pl08x_phy_reassign_start(struct pl08x_phy_chan *ch,
881*4882a593Smuzhiyun 	struct pl08x_dma_chan *plchan)
882*4882a593Smuzhiyun {
883*4882a593Smuzhiyun 	struct pl08x_driver_data *pl08x = plchan->host;
884*4882a593Smuzhiyun 
885*4882a593Smuzhiyun 	dev_dbg(&pl08x->adev->dev, "reassigned physical channel %d for xfer on %s\n",
886*4882a593Smuzhiyun 		ch->id, plchan->name);
887*4882a593Smuzhiyun 
888*4882a593Smuzhiyun 	/*
889*4882a593Smuzhiyun 	 * We do this without taking the lock; we're really only concerned
890*4882a593Smuzhiyun 	 * about whether this pointer is NULL or not, and we're guaranteed
891*4882a593Smuzhiyun 	 * that this will only be called when it _already_ is non-NULL.
892*4882a593Smuzhiyun 	 */
893*4882a593Smuzhiyun 	ch->serving = plchan;
894*4882a593Smuzhiyun 	plchan->phychan = ch;
895*4882a593Smuzhiyun 	plchan->state = PL08X_CHAN_RUNNING;
896*4882a593Smuzhiyun 	pl08x_start_next_txd(plchan);
897*4882a593Smuzhiyun }
898*4882a593Smuzhiyun 
899*4882a593Smuzhiyun /*
900*4882a593Smuzhiyun  * Free a physical DMA channel, potentially reallocating it to another
901*4882a593Smuzhiyun  * virtual channel if we have any pending.
902*4882a593Smuzhiyun  */
pl08x_phy_free(struct pl08x_dma_chan * plchan)903*4882a593Smuzhiyun static void pl08x_phy_free(struct pl08x_dma_chan *plchan)
904*4882a593Smuzhiyun {
905*4882a593Smuzhiyun 	struct pl08x_driver_data *pl08x = plchan->host;
906*4882a593Smuzhiyun 	struct pl08x_dma_chan *p, *next;
907*4882a593Smuzhiyun 	unsigned long waiting_at;
908*4882a593Smuzhiyun  retry:
909*4882a593Smuzhiyun 	next = NULL;
910*4882a593Smuzhiyun 	waiting_at = jiffies;
911*4882a593Smuzhiyun 
912*4882a593Smuzhiyun 	/*
913*4882a593Smuzhiyun 	 * Find a waiting virtual channel for the next transfer.
914*4882a593Smuzhiyun 	 * To be fair, time when each channel reached waiting state is compared
915*4882a593Smuzhiyun 	 * to select channel that is waiting for the longest time.
916*4882a593Smuzhiyun 	 */
917*4882a593Smuzhiyun 	list_for_each_entry(p, &pl08x->memcpy.channels, vc.chan.device_node)
918*4882a593Smuzhiyun 		if (p->state == PL08X_CHAN_WAITING &&
919*4882a593Smuzhiyun 		    p->waiting_at <= waiting_at) {
920*4882a593Smuzhiyun 			next = p;
921*4882a593Smuzhiyun 			waiting_at = p->waiting_at;
922*4882a593Smuzhiyun 		}
923*4882a593Smuzhiyun 
924*4882a593Smuzhiyun 	if (!next && pl08x->has_slave) {
925*4882a593Smuzhiyun 		list_for_each_entry(p, &pl08x->slave.channels, vc.chan.device_node)
926*4882a593Smuzhiyun 			if (p->state == PL08X_CHAN_WAITING &&
927*4882a593Smuzhiyun 			    p->waiting_at <= waiting_at) {
928*4882a593Smuzhiyun 				next = p;
929*4882a593Smuzhiyun 				waiting_at = p->waiting_at;
930*4882a593Smuzhiyun 			}
931*4882a593Smuzhiyun 	}
932*4882a593Smuzhiyun 
933*4882a593Smuzhiyun 	/* Ensure that the physical channel is stopped */
934*4882a593Smuzhiyun 	pl08x_terminate_phy_chan(pl08x, plchan->phychan);
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun 	if (next) {
937*4882a593Smuzhiyun 		bool success;
938*4882a593Smuzhiyun 
939*4882a593Smuzhiyun 		/*
940*4882a593Smuzhiyun 		 * Eww.  We know this isn't going to deadlock
941*4882a593Smuzhiyun 		 * but lockdep probably doesn't.
942*4882a593Smuzhiyun 		 */
943*4882a593Smuzhiyun 		spin_lock(&next->vc.lock);
944*4882a593Smuzhiyun 		/* Re-check the state now that we have the lock */
945*4882a593Smuzhiyun 		success = next->state == PL08X_CHAN_WAITING;
946*4882a593Smuzhiyun 		if (success)
947*4882a593Smuzhiyun 			pl08x_phy_reassign_start(plchan->phychan, next);
948*4882a593Smuzhiyun 		spin_unlock(&next->vc.lock);
949*4882a593Smuzhiyun 
950*4882a593Smuzhiyun 		/* If the state changed, try to find another channel */
951*4882a593Smuzhiyun 		if (!success)
952*4882a593Smuzhiyun 			goto retry;
953*4882a593Smuzhiyun 	} else {
954*4882a593Smuzhiyun 		/* No more jobs, so free up the physical channel */
955*4882a593Smuzhiyun 		pl08x_put_phy_channel(pl08x, plchan->phychan);
956*4882a593Smuzhiyun 	}
957*4882a593Smuzhiyun 
958*4882a593Smuzhiyun 	plchan->phychan = NULL;
959*4882a593Smuzhiyun 	plchan->state = PL08X_CHAN_IDLE;
960*4882a593Smuzhiyun }
961*4882a593Smuzhiyun 
962*4882a593Smuzhiyun /*
963*4882a593Smuzhiyun  * LLI handling
964*4882a593Smuzhiyun  */
965*4882a593Smuzhiyun 
966*4882a593Smuzhiyun static inline unsigned int
pl08x_get_bytes_for_lli(struct pl08x_driver_data * pl08x,u32 cctl,bool source)967*4882a593Smuzhiyun pl08x_get_bytes_for_lli(struct pl08x_driver_data *pl08x,
968*4882a593Smuzhiyun 			u32 cctl,
969*4882a593Smuzhiyun 			bool source)
970*4882a593Smuzhiyun {
971*4882a593Smuzhiyun 	u32 val;
972*4882a593Smuzhiyun 
973*4882a593Smuzhiyun 	if (pl08x->vd->ftdmac020) {
974*4882a593Smuzhiyun 		if (source)
975*4882a593Smuzhiyun 			val = (cctl & FTDMAC020_LLI_SRC_WIDTH_MSK) >>
976*4882a593Smuzhiyun 				FTDMAC020_LLI_SRC_WIDTH_SHIFT;
977*4882a593Smuzhiyun 		else
978*4882a593Smuzhiyun 			val = (cctl & FTDMAC020_LLI_DST_WIDTH_MSK) >>
979*4882a593Smuzhiyun 				FTDMAC020_LLI_DST_WIDTH_SHIFT;
980*4882a593Smuzhiyun 	} else {
981*4882a593Smuzhiyun 		if (source)
982*4882a593Smuzhiyun 			val = (cctl & PL080_CONTROL_SWIDTH_MASK) >>
983*4882a593Smuzhiyun 				PL080_CONTROL_SWIDTH_SHIFT;
984*4882a593Smuzhiyun 		else
985*4882a593Smuzhiyun 			val = (cctl & PL080_CONTROL_DWIDTH_MASK) >>
986*4882a593Smuzhiyun 				PL080_CONTROL_DWIDTH_SHIFT;
987*4882a593Smuzhiyun 	}
988*4882a593Smuzhiyun 
989*4882a593Smuzhiyun 	switch (val) {
990*4882a593Smuzhiyun 	case PL080_WIDTH_8BIT:
991*4882a593Smuzhiyun 		return 1;
992*4882a593Smuzhiyun 	case PL080_WIDTH_16BIT:
993*4882a593Smuzhiyun 		return 2;
994*4882a593Smuzhiyun 	case PL080_WIDTH_32BIT:
995*4882a593Smuzhiyun 		return 4;
996*4882a593Smuzhiyun 	default:
997*4882a593Smuzhiyun 		break;
998*4882a593Smuzhiyun 	}
999*4882a593Smuzhiyun 	BUG();
1000*4882a593Smuzhiyun 	return 0;
1001*4882a593Smuzhiyun }
1002*4882a593Smuzhiyun 
pl08x_lli_control_bits(struct pl08x_driver_data * pl08x,u32 cctl,u8 srcwidth,u8 dstwidth,size_t tsize)1003*4882a593Smuzhiyun static inline u32 pl08x_lli_control_bits(struct pl08x_driver_data *pl08x,
1004*4882a593Smuzhiyun 					 u32 cctl,
1005*4882a593Smuzhiyun 					 u8 srcwidth, u8 dstwidth,
1006*4882a593Smuzhiyun 					 size_t tsize)
1007*4882a593Smuzhiyun {
1008*4882a593Smuzhiyun 	u32 retbits = cctl;
1009*4882a593Smuzhiyun 
1010*4882a593Smuzhiyun 	/*
1011*4882a593Smuzhiyun 	 * Remove all src, dst and transfer size bits, then set the
1012*4882a593Smuzhiyun 	 * width and size according to the parameters. The bit offsets
1013*4882a593Smuzhiyun 	 * are different in the FTDMAC020 so we need to accound for this.
1014*4882a593Smuzhiyun 	 */
1015*4882a593Smuzhiyun 	if (pl08x->vd->ftdmac020) {
1016*4882a593Smuzhiyun 		retbits &= ~FTDMAC020_LLI_DST_WIDTH_MSK;
1017*4882a593Smuzhiyun 		retbits &= ~FTDMAC020_LLI_SRC_WIDTH_MSK;
1018*4882a593Smuzhiyun 		retbits &= ~FTDMAC020_LLI_TRANSFER_SIZE_MASK;
1019*4882a593Smuzhiyun 
1020*4882a593Smuzhiyun 		switch (srcwidth) {
1021*4882a593Smuzhiyun 		case 1:
1022*4882a593Smuzhiyun 			retbits |= PL080_WIDTH_8BIT <<
1023*4882a593Smuzhiyun 				FTDMAC020_LLI_SRC_WIDTH_SHIFT;
1024*4882a593Smuzhiyun 			break;
1025*4882a593Smuzhiyun 		case 2:
1026*4882a593Smuzhiyun 			retbits |= PL080_WIDTH_16BIT <<
1027*4882a593Smuzhiyun 				FTDMAC020_LLI_SRC_WIDTH_SHIFT;
1028*4882a593Smuzhiyun 			break;
1029*4882a593Smuzhiyun 		case 4:
1030*4882a593Smuzhiyun 			retbits |= PL080_WIDTH_32BIT <<
1031*4882a593Smuzhiyun 				FTDMAC020_LLI_SRC_WIDTH_SHIFT;
1032*4882a593Smuzhiyun 			break;
1033*4882a593Smuzhiyun 		default:
1034*4882a593Smuzhiyun 			BUG();
1035*4882a593Smuzhiyun 			break;
1036*4882a593Smuzhiyun 		}
1037*4882a593Smuzhiyun 
1038*4882a593Smuzhiyun 		switch (dstwidth) {
1039*4882a593Smuzhiyun 		case 1:
1040*4882a593Smuzhiyun 			retbits |= PL080_WIDTH_8BIT <<
1041*4882a593Smuzhiyun 				FTDMAC020_LLI_DST_WIDTH_SHIFT;
1042*4882a593Smuzhiyun 			break;
1043*4882a593Smuzhiyun 		case 2:
1044*4882a593Smuzhiyun 			retbits |= PL080_WIDTH_16BIT <<
1045*4882a593Smuzhiyun 				FTDMAC020_LLI_DST_WIDTH_SHIFT;
1046*4882a593Smuzhiyun 			break;
1047*4882a593Smuzhiyun 		case 4:
1048*4882a593Smuzhiyun 			retbits |= PL080_WIDTH_32BIT <<
1049*4882a593Smuzhiyun 				FTDMAC020_LLI_DST_WIDTH_SHIFT;
1050*4882a593Smuzhiyun 			break;
1051*4882a593Smuzhiyun 		default:
1052*4882a593Smuzhiyun 			BUG();
1053*4882a593Smuzhiyun 			break;
1054*4882a593Smuzhiyun 		}
1055*4882a593Smuzhiyun 
1056*4882a593Smuzhiyun 		tsize &= FTDMAC020_LLI_TRANSFER_SIZE_MASK;
1057*4882a593Smuzhiyun 		retbits |= tsize << FTDMAC020_LLI_TRANSFER_SIZE_SHIFT;
1058*4882a593Smuzhiyun 	} else {
1059*4882a593Smuzhiyun 		retbits &= ~PL080_CONTROL_DWIDTH_MASK;
1060*4882a593Smuzhiyun 		retbits &= ~PL080_CONTROL_SWIDTH_MASK;
1061*4882a593Smuzhiyun 		retbits &= ~PL080_CONTROL_TRANSFER_SIZE_MASK;
1062*4882a593Smuzhiyun 
1063*4882a593Smuzhiyun 		switch (srcwidth) {
1064*4882a593Smuzhiyun 		case 1:
1065*4882a593Smuzhiyun 			retbits |= PL080_WIDTH_8BIT <<
1066*4882a593Smuzhiyun 				PL080_CONTROL_SWIDTH_SHIFT;
1067*4882a593Smuzhiyun 			break;
1068*4882a593Smuzhiyun 		case 2:
1069*4882a593Smuzhiyun 			retbits |= PL080_WIDTH_16BIT <<
1070*4882a593Smuzhiyun 				PL080_CONTROL_SWIDTH_SHIFT;
1071*4882a593Smuzhiyun 			break;
1072*4882a593Smuzhiyun 		case 4:
1073*4882a593Smuzhiyun 			retbits |= PL080_WIDTH_32BIT <<
1074*4882a593Smuzhiyun 				PL080_CONTROL_SWIDTH_SHIFT;
1075*4882a593Smuzhiyun 			break;
1076*4882a593Smuzhiyun 		default:
1077*4882a593Smuzhiyun 			BUG();
1078*4882a593Smuzhiyun 			break;
1079*4882a593Smuzhiyun 		}
1080*4882a593Smuzhiyun 
1081*4882a593Smuzhiyun 		switch (dstwidth) {
1082*4882a593Smuzhiyun 		case 1:
1083*4882a593Smuzhiyun 			retbits |= PL080_WIDTH_8BIT <<
1084*4882a593Smuzhiyun 				PL080_CONTROL_DWIDTH_SHIFT;
1085*4882a593Smuzhiyun 			break;
1086*4882a593Smuzhiyun 		case 2:
1087*4882a593Smuzhiyun 			retbits |= PL080_WIDTH_16BIT <<
1088*4882a593Smuzhiyun 				PL080_CONTROL_DWIDTH_SHIFT;
1089*4882a593Smuzhiyun 			break;
1090*4882a593Smuzhiyun 		case 4:
1091*4882a593Smuzhiyun 			retbits |= PL080_WIDTH_32BIT <<
1092*4882a593Smuzhiyun 				PL080_CONTROL_DWIDTH_SHIFT;
1093*4882a593Smuzhiyun 			break;
1094*4882a593Smuzhiyun 		default:
1095*4882a593Smuzhiyun 			BUG();
1096*4882a593Smuzhiyun 			break;
1097*4882a593Smuzhiyun 		}
1098*4882a593Smuzhiyun 
1099*4882a593Smuzhiyun 		tsize &= PL080_CONTROL_TRANSFER_SIZE_MASK;
1100*4882a593Smuzhiyun 		retbits |= tsize << PL080_CONTROL_TRANSFER_SIZE_SHIFT;
1101*4882a593Smuzhiyun 	}
1102*4882a593Smuzhiyun 
1103*4882a593Smuzhiyun 	return retbits;
1104*4882a593Smuzhiyun }
1105*4882a593Smuzhiyun 
1106*4882a593Smuzhiyun struct pl08x_lli_build_data {
1107*4882a593Smuzhiyun 	struct pl08x_txd *txd;
1108*4882a593Smuzhiyun 	struct pl08x_bus_data srcbus;
1109*4882a593Smuzhiyun 	struct pl08x_bus_data dstbus;
1110*4882a593Smuzhiyun 	size_t remainder;
1111*4882a593Smuzhiyun 	u32 lli_bus;
1112*4882a593Smuzhiyun };
1113*4882a593Smuzhiyun 
1114*4882a593Smuzhiyun /*
1115*4882a593Smuzhiyun  * Autoselect a master bus to use for the transfer. Slave will be the chosen as
1116*4882a593Smuzhiyun  * victim in case src & dest are not similarly aligned. i.e. If after aligning
1117*4882a593Smuzhiyun  * masters address with width requirements of transfer (by sending few byte by
1118*4882a593Smuzhiyun  * byte data), slave is still not aligned, then its width will be reduced to
1119*4882a593Smuzhiyun  * BYTE.
1120*4882a593Smuzhiyun  * - prefers the destination bus if both available
1121*4882a593Smuzhiyun  * - prefers bus with fixed address (i.e. peripheral)
1122*4882a593Smuzhiyun  */
pl08x_choose_master_bus(struct pl08x_driver_data * pl08x,struct pl08x_lli_build_data * bd,struct pl08x_bus_data ** mbus,struct pl08x_bus_data ** sbus,u32 cctl)1123*4882a593Smuzhiyun static void pl08x_choose_master_bus(struct pl08x_driver_data *pl08x,
1124*4882a593Smuzhiyun 				    struct pl08x_lli_build_data *bd,
1125*4882a593Smuzhiyun 				    struct pl08x_bus_data **mbus,
1126*4882a593Smuzhiyun 				    struct pl08x_bus_data **sbus,
1127*4882a593Smuzhiyun 				    u32 cctl)
1128*4882a593Smuzhiyun {
1129*4882a593Smuzhiyun 	bool dst_incr;
1130*4882a593Smuzhiyun 	bool src_incr;
1131*4882a593Smuzhiyun 
1132*4882a593Smuzhiyun 	/*
1133*4882a593Smuzhiyun 	 * The FTDMAC020 only supports memory-to-memory transfer, so
1134*4882a593Smuzhiyun 	 * source and destination always increase.
1135*4882a593Smuzhiyun 	 */
1136*4882a593Smuzhiyun 	if (pl08x->vd->ftdmac020) {
1137*4882a593Smuzhiyun 		dst_incr = true;
1138*4882a593Smuzhiyun 		src_incr = true;
1139*4882a593Smuzhiyun 	} else {
1140*4882a593Smuzhiyun 		dst_incr = !!(cctl & PL080_CONTROL_DST_INCR);
1141*4882a593Smuzhiyun 		src_incr = !!(cctl & PL080_CONTROL_SRC_INCR);
1142*4882a593Smuzhiyun 	}
1143*4882a593Smuzhiyun 
1144*4882a593Smuzhiyun 	/*
1145*4882a593Smuzhiyun 	 * If either bus is not advancing, i.e. it is a peripheral, that
1146*4882a593Smuzhiyun 	 * one becomes master
1147*4882a593Smuzhiyun 	 */
1148*4882a593Smuzhiyun 	if (!dst_incr) {
1149*4882a593Smuzhiyun 		*mbus = &bd->dstbus;
1150*4882a593Smuzhiyun 		*sbus = &bd->srcbus;
1151*4882a593Smuzhiyun 	} else if (!src_incr) {
1152*4882a593Smuzhiyun 		*mbus = &bd->srcbus;
1153*4882a593Smuzhiyun 		*sbus = &bd->dstbus;
1154*4882a593Smuzhiyun 	} else {
1155*4882a593Smuzhiyun 		if (bd->dstbus.buswidth >= bd->srcbus.buswidth) {
1156*4882a593Smuzhiyun 			*mbus = &bd->dstbus;
1157*4882a593Smuzhiyun 			*sbus = &bd->srcbus;
1158*4882a593Smuzhiyun 		} else {
1159*4882a593Smuzhiyun 			*mbus = &bd->srcbus;
1160*4882a593Smuzhiyun 			*sbus = &bd->dstbus;
1161*4882a593Smuzhiyun 		}
1162*4882a593Smuzhiyun 	}
1163*4882a593Smuzhiyun }
1164*4882a593Smuzhiyun 
1165*4882a593Smuzhiyun /*
1166*4882a593Smuzhiyun  * Fills in one LLI for a certain transfer descriptor and advance the counter
1167*4882a593Smuzhiyun  */
pl08x_fill_lli_for_desc(struct pl08x_driver_data * pl08x,struct pl08x_lli_build_data * bd,int num_llis,int len,u32 cctl,u32 cctl2)1168*4882a593Smuzhiyun static void pl08x_fill_lli_for_desc(struct pl08x_driver_data *pl08x,
1169*4882a593Smuzhiyun 				    struct pl08x_lli_build_data *bd,
1170*4882a593Smuzhiyun 				    int num_llis, int len, u32 cctl, u32 cctl2)
1171*4882a593Smuzhiyun {
1172*4882a593Smuzhiyun 	u32 offset = num_llis * pl08x->lli_words;
1173*4882a593Smuzhiyun 	u32 *llis_va = bd->txd->llis_va + offset;
1174*4882a593Smuzhiyun 	dma_addr_t llis_bus = bd->txd->llis_bus;
1175*4882a593Smuzhiyun 
1176*4882a593Smuzhiyun 	BUG_ON(num_llis >= MAX_NUM_TSFR_LLIS);
1177*4882a593Smuzhiyun 
1178*4882a593Smuzhiyun 	/* Advance the offset to next LLI. */
1179*4882a593Smuzhiyun 	offset += pl08x->lli_words;
1180*4882a593Smuzhiyun 
1181*4882a593Smuzhiyun 	llis_va[PL080_LLI_SRC] = bd->srcbus.addr;
1182*4882a593Smuzhiyun 	llis_va[PL080_LLI_DST] = bd->dstbus.addr;
1183*4882a593Smuzhiyun 	llis_va[PL080_LLI_LLI] = (llis_bus + sizeof(u32) * offset);
1184*4882a593Smuzhiyun 	llis_va[PL080_LLI_LLI] |= bd->lli_bus;
1185*4882a593Smuzhiyun 	llis_va[PL080_LLI_CCTL] = cctl;
1186*4882a593Smuzhiyun 	if (pl08x->vd->pl080s)
1187*4882a593Smuzhiyun 		llis_va[PL080S_LLI_CCTL2] = cctl2;
1188*4882a593Smuzhiyun 
1189*4882a593Smuzhiyun 	if (pl08x->vd->ftdmac020) {
1190*4882a593Smuzhiyun 		/* FIXME: only memcpy so far so both increase */
1191*4882a593Smuzhiyun 		bd->srcbus.addr += len;
1192*4882a593Smuzhiyun 		bd->dstbus.addr += len;
1193*4882a593Smuzhiyun 	} else {
1194*4882a593Smuzhiyun 		if (cctl & PL080_CONTROL_SRC_INCR)
1195*4882a593Smuzhiyun 			bd->srcbus.addr += len;
1196*4882a593Smuzhiyun 		if (cctl & PL080_CONTROL_DST_INCR)
1197*4882a593Smuzhiyun 			bd->dstbus.addr += len;
1198*4882a593Smuzhiyun 	}
1199*4882a593Smuzhiyun 
1200*4882a593Smuzhiyun 	BUG_ON(bd->remainder < len);
1201*4882a593Smuzhiyun 
1202*4882a593Smuzhiyun 	bd->remainder -= len;
1203*4882a593Smuzhiyun }
1204*4882a593Smuzhiyun 
prep_byte_width_lli(struct pl08x_driver_data * pl08x,struct pl08x_lli_build_data * bd,u32 * cctl,u32 len,int num_llis,size_t * total_bytes)1205*4882a593Smuzhiyun static inline void prep_byte_width_lli(struct pl08x_driver_data *pl08x,
1206*4882a593Smuzhiyun 			struct pl08x_lli_build_data *bd, u32 *cctl, u32 len,
1207*4882a593Smuzhiyun 			int num_llis, size_t *total_bytes)
1208*4882a593Smuzhiyun {
1209*4882a593Smuzhiyun 	*cctl = pl08x_lli_control_bits(pl08x, *cctl, 1, 1, len);
1210*4882a593Smuzhiyun 	pl08x_fill_lli_for_desc(pl08x, bd, num_llis, len, *cctl, len);
1211*4882a593Smuzhiyun 	(*total_bytes) += len;
1212*4882a593Smuzhiyun }
1213*4882a593Smuzhiyun 
1214*4882a593Smuzhiyun #if 1
pl08x_dump_lli(struct pl08x_driver_data * pl08x,const u32 * llis_va,int num_llis)1215*4882a593Smuzhiyun static void pl08x_dump_lli(struct pl08x_driver_data *pl08x,
1216*4882a593Smuzhiyun 			   const u32 *llis_va, int num_llis)
1217*4882a593Smuzhiyun {
1218*4882a593Smuzhiyun 	int i;
1219*4882a593Smuzhiyun 
1220*4882a593Smuzhiyun 	if (pl08x->vd->pl080s) {
1221*4882a593Smuzhiyun 		dev_vdbg(&pl08x->adev->dev,
1222*4882a593Smuzhiyun 			"%-3s %-9s  %-10s %-10s %-10s %-10s %s\n",
1223*4882a593Smuzhiyun 			"lli", "", "csrc", "cdst", "clli", "cctl", "cctl2");
1224*4882a593Smuzhiyun 		for (i = 0; i < num_llis; i++) {
1225*4882a593Smuzhiyun 			dev_vdbg(&pl08x->adev->dev,
1226*4882a593Smuzhiyun 				"%3d @%p: 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
1227*4882a593Smuzhiyun 				i, llis_va, llis_va[PL080_LLI_SRC],
1228*4882a593Smuzhiyun 				llis_va[PL080_LLI_DST], llis_va[PL080_LLI_LLI],
1229*4882a593Smuzhiyun 				llis_va[PL080_LLI_CCTL],
1230*4882a593Smuzhiyun 				llis_va[PL080S_LLI_CCTL2]);
1231*4882a593Smuzhiyun 			llis_va += pl08x->lli_words;
1232*4882a593Smuzhiyun 		}
1233*4882a593Smuzhiyun 	} else {
1234*4882a593Smuzhiyun 		dev_vdbg(&pl08x->adev->dev,
1235*4882a593Smuzhiyun 			"%-3s %-9s  %-10s %-10s %-10s %s\n",
1236*4882a593Smuzhiyun 			"lli", "", "csrc", "cdst", "clli", "cctl");
1237*4882a593Smuzhiyun 		for (i = 0; i < num_llis; i++) {
1238*4882a593Smuzhiyun 			dev_vdbg(&pl08x->adev->dev,
1239*4882a593Smuzhiyun 				"%3d @%p: 0x%08x 0x%08x 0x%08x 0x%08x\n",
1240*4882a593Smuzhiyun 				i, llis_va, llis_va[PL080_LLI_SRC],
1241*4882a593Smuzhiyun 				llis_va[PL080_LLI_DST], llis_va[PL080_LLI_LLI],
1242*4882a593Smuzhiyun 				llis_va[PL080_LLI_CCTL]);
1243*4882a593Smuzhiyun 			llis_va += pl08x->lli_words;
1244*4882a593Smuzhiyun 		}
1245*4882a593Smuzhiyun 	}
1246*4882a593Smuzhiyun }
1247*4882a593Smuzhiyun #else
pl08x_dump_lli(struct pl08x_driver_data * pl08x,const u32 * llis_va,int num_llis)1248*4882a593Smuzhiyun static inline void pl08x_dump_lli(struct pl08x_driver_data *pl08x,
1249*4882a593Smuzhiyun 				  const u32 *llis_va, int num_llis) {}
1250*4882a593Smuzhiyun #endif
1251*4882a593Smuzhiyun 
1252*4882a593Smuzhiyun /*
1253*4882a593Smuzhiyun  * This fills in the table of LLIs for the transfer descriptor
1254*4882a593Smuzhiyun  * Note that we assume we never have to change the burst sizes
1255*4882a593Smuzhiyun  * Return 0 for error
1256*4882a593Smuzhiyun  */
pl08x_fill_llis_for_desc(struct pl08x_driver_data * pl08x,struct pl08x_txd * txd)1257*4882a593Smuzhiyun static int pl08x_fill_llis_for_desc(struct pl08x_driver_data *pl08x,
1258*4882a593Smuzhiyun 			      struct pl08x_txd *txd)
1259*4882a593Smuzhiyun {
1260*4882a593Smuzhiyun 	struct pl08x_bus_data *mbus, *sbus;
1261*4882a593Smuzhiyun 	struct pl08x_lli_build_data bd;
1262*4882a593Smuzhiyun 	int num_llis = 0;
1263*4882a593Smuzhiyun 	u32 cctl, early_bytes = 0;
1264*4882a593Smuzhiyun 	size_t max_bytes_per_lli, total_bytes;
1265*4882a593Smuzhiyun 	u32 *llis_va, *last_lli;
1266*4882a593Smuzhiyun 	struct pl08x_sg *dsg;
1267*4882a593Smuzhiyun 
1268*4882a593Smuzhiyun 	txd->llis_va = dma_pool_alloc(pl08x->pool, GFP_NOWAIT, &txd->llis_bus);
1269*4882a593Smuzhiyun 	if (!txd->llis_va) {
1270*4882a593Smuzhiyun 		dev_err(&pl08x->adev->dev, "%s no memory for llis\n", __func__);
1271*4882a593Smuzhiyun 		return 0;
1272*4882a593Smuzhiyun 	}
1273*4882a593Smuzhiyun 
1274*4882a593Smuzhiyun 	bd.txd = txd;
1275*4882a593Smuzhiyun 	bd.lli_bus = (pl08x->lli_buses & PL08X_AHB2) ? PL080_LLI_LM_AHB2 : 0;
1276*4882a593Smuzhiyun 	cctl = txd->cctl;
1277*4882a593Smuzhiyun 
1278*4882a593Smuzhiyun 	/* Find maximum width of the source bus */
1279*4882a593Smuzhiyun 	bd.srcbus.maxwidth = pl08x_get_bytes_for_lli(pl08x, cctl, true);
1280*4882a593Smuzhiyun 
1281*4882a593Smuzhiyun 	/* Find maximum width of the destination bus */
1282*4882a593Smuzhiyun 	bd.dstbus.maxwidth = pl08x_get_bytes_for_lli(pl08x, cctl, false);
1283*4882a593Smuzhiyun 
1284*4882a593Smuzhiyun 	list_for_each_entry(dsg, &txd->dsg_list, node) {
1285*4882a593Smuzhiyun 		total_bytes = 0;
1286*4882a593Smuzhiyun 		cctl = txd->cctl;
1287*4882a593Smuzhiyun 
1288*4882a593Smuzhiyun 		bd.srcbus.addr = dsg->src_addr;
1289*4882a593Smuzhiyun 		bd.dstbus.addr = dsg->dst_addr;
1290*4882a593Smuzhiyun 		bd.remainder = dsg->len;
1291*4882a593Smuzhiyun 		bd.srcbus.buswidth = bd.srcbus.maxwidth;
1292*4882a593Smuzhiyun 		bd.dstbus.buswidth = bd.dstbus.maxwidth;
1293*4882a593Smuzhiyun 
1294*4882a593Smuzhiyun 		pl08x_choose_master_bus(pl08x, &bd, &mbus, &sbus, cctl);
1295*4882a593Smuzhiyun 
1296*4882a593Smuzhiyun 		dev_vdbg(&pl08x->adev->dev,
1297*4882a593Smuzhiyun 			"src=0x%08llx%s/%u dst=0x%08llx%s/%u len=%zu\n",
1298*4882a593Smuzhiyun 			(u64)bd.srcbus.addr,
1299*4882a593Smuzhiyun 			cctl & PL080_CONTROL_SRC_INCR ? "+" : "",
1300*4882a593Smuzhiyun 			bd.srcbus.buswidth,
1301*4882a593Smuzhiyun 			(u64)bd.dstbus.addr,
1302*4882a593Smuzhiyun 			cctl & PL080_CONTROL_DST_INCR ? "+" : "",
1303*4882a593Smuzhiyun 			bd.dstbus.buswidth,
1304*4882a593Smuzhiyun 			bd.remainder);
1305*4882a593Smuzhiyun 		dev_vdbg(&pl08x->adev->dev, "mbus=%s sbus=%s\n",
1306*4882a593Smuzhiyun 			mbus == &bd.srcbus ? "src" : "dst",
1307*4882a593Smuzhiyun 			sbus == &bd.srcbus ? "src" : "dst");
1308*4882a593Smuzhiyun 
1309*4882a593Smuzhiyun 		/*
1310*4882a593Smuzhiyun 		 * Zero length is only allowed if all these requirements are
1311*4882a593Smuzhiyun 		 * met:
1312*4882a593Smuzhiyun 		 * - flow controller is peripheral.
1313*4882a593Smuzhiyun 		 * - src.addr is aligned to src.width
1314*4882a593Smuzhiyun 		 * - dst.addr is aligned to dst.width
1315*4882a593Smuzhiyun 		 *
1316*4882a593Smuzhiyun 		 * sg_len == 1 should be true, as there can be two cases here:
1317*4882a593Smuzhiyun 		 *
1318*4882a593Smuzhiyun 		 * - Memory addresses are contiguous and are not scattered.
1319*4882a593Smuzhiyun 		 *   Here, Only one sg will be passed by user driver, with
1320*4882a593Smuzhiyun 		 *   memory address and zero length. We pass this to controller
1321*4882a593Smuzhiyun 		 *   and after the transfer it will receive the last burst
1322*4882a593Smuzhiyun 		 *   request from peripheral and so transfer finishes.
1323*4882a593Smuzhiyun 		 *
1324*4882a593Smuzhiyun 		 * - Memory addresses are scattered and are not contiguous.
1325*4882a593Smuzhiyun 		 *   Here, Obviously as DMA controller doesn't know when a lli's
1326*4882a593Smuzhiyun 		 *   transfer gets over, it can't load next lli. So in this
1327*4882a593Smuzhiyun 		 *   case, there has to be an assumption that only one lli is
1328*4882a593Smuzhiyun 		 *   supported. Thus, we can't have scattered addresses.
1329*4882a593Smuzhiyun 		 */
1330*4882a593Smuzhiyun 		if (!bd.remainder) {
1331*4882a593Smuzhiyun 			u32 fc;
1332*4882a593Smuzhiyun 
1333*4882a593Smuzhiyun 			/* FTDMAC020 only does memory-to-memory */
1334*4882a593Smuzhiyun 			if (pl08x->vd->ftdmac020)
1335*4882a593Smuzhiyun 				fc = PL080_FLOW_MEM2MEM;
1336*4882a593Smuzhiyun 			else
1337*4882a593Smuzhiyun 				fc = (txd->ccfg & PL080_CONFIG_FLOW_CONTROL_MASK) >>
1338*4882a593Smuzhiyun 					PL080_CONFIG_FLOW_CONTROL_SHIFT;
1339*4882a593Smuzhiyun 			if (!((fc >= PL080_FLOW_SRC2DST_DST) &&
1340*4882a593Smuzhiyun 					(fc <= PL080_FLOW_SRC2DST_SRC))) {
1341*4882a593Smuzhiyun 				dev_err(&pl08x->adev->dev, "%s sg len can't be zero",
1342*4882a593Smuzhiyun 					__func__);
1343*4882a593Smuzhiyun 				return 0;
1344*4882a593Smuzhiyun 			}
1345*4882a593Smuzhiyun 
1346*4882a593Smuzhiyun 			if (!IS_BUS_ALIGNED(&bd.srcbus) ||
1347*4882a593Smuzhiyun 				!IS_BUS_ALIGNED(&bd.dstbus)) {
1348*4882a593Smuzhiyun 				dev_err(&pl08x->adev->dev,
1349*4882a593Smuzhiyun 					"%s src & dst address must be aligned to src"
1350*4882a593Smuzhiyun 					" & dst width if peripheral is flow controller",
1351*4882a593Smuzhiyun 					__func__);
1352*4882a593Smuzhiyun 				return 0;
1353*4882a593Smuzhiyun 			}
1354*4882a593Smuzhiyun 
1355*4882a593Smuzhiyun 			cctl = pl08x_lli_control_bits(pl08x, cctl,
1356*4882a593Smuzhiyun 					bd.srcbus.buswidth, bd.dstbus.buswidth,
1357*4882a593Smuzhiyun 					0);
1358*4882a593Smuzhiyun 			pl08x_fill_lli_for_desc(pl08x, &bd, num_llis++,
1359*4882a593Smuzhiyun 					0, cctl, 0);
1360*4882a593Smuzhiyun 			break;
1361*4882a593Smuzhiyun 		}
1362*4882a593Smuzhiyun 
1363*4882a593Smuzhiyun 		/*
1364*4882a593Smuzhiyun 		 * Send byte by byte for following cases
1365*4882a593Smuzhiyun 		 * - Less than a bus width available
1366*4882a593Smuzhiyun 		 * - until master bus is aligned
1367*4882a593Smuzhiyun 		 */
1368*4882a593Smuzhiyun 		if (bd.remainder < mbus->buswidth)
1369*4882a593Smuzhiyun 			early_bytes = bd.remainder;
1370*4882a593Smuzhiyun 		else if (!IS_BUS_ALIGNED(mbus)) {
1371*4882a593Smuzhiyun 			early_bytes = mbus->buswidth -
1372*4882a593Smuzhiyun 				(mbus->addr & (mbus->buswidth - 1));
1373*4882a593Smuzhiyun 			if ((bd.remainder - early_bytes) < mbus->buswidth)
1374*4882a593Smuzhiyun 				early_bytes = bd.remainder;
1375*4882a593Smuzhiyun 		}
1376*4882a593Smuzhiyun 
1377*4882a593Smuzhiyun 		if (early_bytes) {
1378*4882a593Smuzhiyun 			dev_vdbg(&pl08x->adev->dev,
1379*4882a593Smuzhiyun 				"%s byte width LLIs (remain 0x%08zx)\n",
1380*4882a593Smuzhiyun 				__func__, bd.remainder);
1381*4882a593Smuzhiyun 			prep_byte_width_lli(pl08x, &bd, &cctl, early_bytes,
1382*4882a593Smuzhiyun 				num_llis++, &total_bytes);
1383*4882a593Smuzhiyun 		}
1384*4882a593Smuzhiyun 
1385*4882a593Smuzhiyun 		if (bd.remainder) {
1386*4882a593Smuzhiyun 			/*
1387*4882a593Smuzhiyun 			 * Master now aligned
1388*4882a593Smuzhiyun 			 * - if slave is not then we must set its width down
1389*4882a593Smuzhiyun 			 */
1390*4882a593Smuzhiyun 			if (!IS_BUS_ALIGNED(sbus)) {
1391*4882a593Smuzhiyun 				dev_dbg(&pl08x->adev->dev,
1392*4882a593Smuzhiyun 					"%s set down bus width to one byte\n",
1393*4882a593Smuzhiyun 					__func__);
1394*4882a593Smuzhiyun 
1395*4882a593Smuzhiyun 				sbus->buswidth = 1;
1396*4882a593Smuzhiyun 			}
1397*4882a593Smuzhiyun 
1398*4882a593Smuzhiyun 			/*
1399*4882a593Smuzhiyun 			 * Bytes transferred = tsize * src width, not
1400*4882a593Smuzhiyun 			 * MIN(buswidths)
1401*4882a593Smuzhiyun 			 */
1402*4882a593Smuzhiyun 			max_bytes_per_lli = bd.srcbus.buswidth *
1403*4882a593Smuzhiyun 						pl08x->vd->max_transfer_size;
1404*4882a593Smuzhiyun 			dev_vdbg(&pl08x->adev->dev,
1405*4882a593Smuzhiyun 				"%s max bytes per lli = %zu\n",
1406*4882a593Smuzhiyun 				__func__, max_bytes_per_lli);
1407*4882a593Smuzhiyun 
1408*4882a593Smuzhiyun 			/*
1409*4882a593Smuzhiyun 			 * Make largest possible LLIs until less than one bus
1410*4882a593Smuzhiyun 			 * width left
1411*4882a593Smuzhiyun 			 */
1412*4882a593Smuzhiyun 			while (bd.remainder > (mbus->buswidth - 1)) {
1413*4882a593Smuzhiyun 				size_t lli_len, tsize, width;
1414*4882a593Smuzhiyun 
1415*4882a593Smuzhiyun 				/*
1416*4882a593Smuzhiyun 				 * If enough left try to send max possible,
1417*4882a593Smuzhiyun 				 * otherwise try to send the remainder
1418*4882a593Smuzhiyun 				 */
1419*4882a593Smuzhiyun 				lli_len = min(bd.remainder, max_bytes_per_lli);
1420*4882a593Smuzhiyun 
1421*4882a593Smuzhiyun 				/*
1422*4882a593Smuzhiyun 				 * Check against maximum bus alignment:
1423*4882a593Smuzhiyun 				 * Calculate actual transfer size in relation to
1424*4882a593Smuzhiyun 				 * bus width an get a maximum remainder of the
1425*4882a593Smuzhiyun 				 * highest bus width - 1
1426*4882a593Smuzhiyun 				 */
1427*4882a593Smuzhiyun 				width = max(mbus->buswidth, sbus->buswidth);
1428*4882a593Smuzhiyun 				lli_len = (lli_len / width) * width;
1429*4882a593Smuzhiyun 				tsize = lli_len / bd.srcbus.buswidth;
1430*4882a593Smuzhiyun 
1431*4882a593Smuzhiyun 				dev_vdbg(&pl08x->adev->dev,
1432*4882a593Smuzhiyun 					"%s fill lli with single lli chunk of "
1433*4882a593Smuzhiyun 					"size 0x%08zx (remainder 0x%08zx)\n",
1434*4882a593Smuzhiyun 					__func__, lli_len, bd.remainder);
1435*4882a593Smuzhiyun 
1436*4882a593Smuzhiyun 				cctl = pl08x_lli_control_bits(pl08x, cctl,
1437*4882a593Smuzhiyun 					bd.srcbus.buswidth, bd.dstbus.buswidth,
1438*4882a593Smuzhiyun 					tsize);
1439*4882a593Smuzhiyun 				pl08x_fill_lli_for_desc(pl08x, &bd, num_llis++,
1440*4882a593Smuzhiyun 						lli_len, cctl, tsize);
1441*4882a593Smuzhiyun 				total_bytes += lli_len;
1442*4882a593Smuzhiyun 			}
1443*4882a593Smuzhiyun 
1444*4882a593Smuzhiyun 			/*
1445*4882a593Smuzhiyun 			 * Send any odd bytes
1446*4882a593Smuzhiyun 			 */
1447*4882a593Smuzhiyun 			if (bd.remainder) {
1448*4882a593Smuzhiyun 				dev_vdbg(&pl08x->adev->dev,
1449*4882a593Smuzhiyun 					"%s align with boundary, send odd bytes (remain %zu)\n",
1450*4882a593Smuzhiyun 					__func__, bd.remainder);
1451*4882a593Smuzhiyun 				prep_byte_width_lli(pl08x, &bd, &cctl,
1452*4882a593Smuzhiyun 					bd.remainder, num_llis++, &total_bytes);
1453*4882a593Smuzhiyun 			}
1454*4882a593Smuzhiyun 		}
1455*4882a593Smuzhiyun 
1456*4882a593Smuzhiyun 		if (total_bytes != dsg->len) {
1457*4882a593Smuzhiyun 			dev_err(&pl08x->adev->dev,
1458*4882a593Smuzhiyun 				"%s size of encoded lli:s don't match total txd, transferred 0x%08zx from size 0x%08zx\n",
1459*4882a593Smuzhiyun 				__func__, total_bytes, dsg->len);
1460*4882a593Smuzhiyun 			return 0;
1461*4882a593Smuzhiyun 		}
1462*4882a593Smuzhiyun 
1463*4882a593Smuzhiyun 		if (num_llis >= MAX_NUM_TSFR_LLIS) {
1464*4882a593Smuzhiyun 			dev_err(&pl08x->adev->dev,
1465*4882a593Smuzhiyun 				"%s need to increase MAX_NUM_TSFR_LLIS from 0x%08x\n",
1466*4882a593Smuzhiyun 				__func__, MAX_NUM_TSFR_LLIS);
1467*4882a593Smuzhiyun 			return 0;
1468*4882a593Smuzhiyun 		}
1469*4882a593Smuzhiyun 	}
1470*4882a593Smuzhiyun 
1471*4882a593Smuzhiyun 	llis_va = txd->llis_va;
1472*4882a593Smuzhiyun 	last_lli = llis_va + (num_llis - 1) * pl08x->lli_words;
1473*4882a593Smuzhiyun 
1474*4882a593Smuzhiyun 	if (txd->cyclic) {
1475*4882a593Smuzhiyun 		/* Link back to the first LLI. */
1476*4882a593Smuzhiyun 		last_lli[PL080_LLI_LLI] = txd->llis_bus | bd.lli_bus;
1477*4882a593Smuzhiyun 	} else {
1478*4882a593Smuzhiyun 		/* The final LLI terminates the LLI. */
1479*4882a593Smuzhiyun 		last_lli[PL080_LLI_LLI] = 0;
1480*4882a593Smuzhiyun 		/* The final LLI element shall also fire an interrupt. */
1481*4882a593Smuzhiyun 		if (pl08x->vd->ftdmac020)
1482*4882a593Smuzhiyun 			last_lli[PL080_LLI_CCTL] &= ~FTDMAC020_LLI_TC_MSK;
1483*4882a593Smuzhiyun 		else
1484*4882a593Smuzhiyun 			last_lli[PL080_LLI_CCTL] |= PL080_CONTROL_TC_IRQ_EN;
1485*4882a593Smuzhiyun 	}
1486*4882a593Smuzhiyun 
1487*4882a593Smuzhiyun 	pl08x_dump_lli(pl08x, llis_va, num_llis);
1488*4882a593Smuzhiyun 
1489*4882a593Smuzhiyun 	return num_llis;
1490*4882a593Smuzhiyun }
1491*4882a593Smuzhiyun 
pl08x_free_txd(struct pl08x_driver_data * pl08x,struct pl08x_txd * txd)1492*4882a593Smuzhiyun static void pl08x_free_txd(struct pl08x_driver_data *pl08x,
1493*4882a593Smuzhiyun 			   struct pl08x_txd *txd)
1494*4882a593Smuzhiyun {
1495*4882a593Smuzhiyun 	struct pl08x_sg *dsg, *_dsg;
1496*4882a593Smuzhiyun 
1497*4882a593Smuzhiyun 	if (txd->llis_va)
1498*4882a593Smuzhiyun 		dma_pool_free(pl08x->pool, txd->llis_va, txd->llis_bus);
1499*4882a593Smuzhiyun 
1500*4882a593Smuzhiyun 	list_for_each_entry_safe(dsg, _dsg, &txd->dsg_list, node) {
1501*4882a593Smuzhiyun 		list_del(&dsg->node);
1502*4882a593Smuzhiyun 		kfree(dsg);
1503*4882a593Smuzhiyun 	}
1504*4882a593Smuzhiyun 
1505*4882a593Smuzhiyun 	kfree(txd);
1506*4882a593Smuzhiyun }
1507*4882a593Smuzhiyun 
pl08x_desc_free(struct virt_dma_desc * vd)1508*4882a593Smuzhiyun static void pl08x_desc_free(struct virt_dma_desc *vd)
1509*4882a593Smuzhiyun {
1510*4882a593Smuzhiyun 	struct pl08x_txd *txd = to_pl08x_txd(&vd->tx);
1511*4882a593Smuzhiyun 	struct pl08x_dma_chan *plchan = to_pl08x_chan(vd->tx.chan);
1512*4882a593Smuzhiyun 
1513*4882a593Smuzhiyun 	dma_descriptor_unmap(&vd->tx);
1514*4882a593Smuzhiyun 	if (!txd->done)
1515*4882a593Smuzhiyun 		pl08x_release_mux(plchan);
1516*4882a593Smuzhiyun 
1517*4882a593Smuzhiyun 	pl08x_free_txd(plchan->host, txd);
1518*4882a593Smuzhiyun }
1519*4882a593Smuzhiyun 
pl08x_free_txd_list(struct pl08x_driver_data * pl08x,struct pl08x_dma_chan * plchan)1520*4882a593Smuzhiyun static void pl08x_free_txd_list(struct pl08x_driver_data *pl08x,
1521*4882a593Smuzhiyun 				struct pl08x_dma_chan *plchan)
1522*4882a593Smuzhiyun {
1523*4882a593Smuzhiyun 	LIST_HEAD(head);
1524*4882a593Smuzhiyun 
1525*4882a593Smuzhiyun 	vchan_get_all_descriptors(&plchan->vc, &head);
1526*4882a593Smuzhiyun 	vchan_dma_desc_free_list(&plchan->vc, &head);
1527*4882a593Smuzhiyun }
1528*4882a593Smuzhiyun 
1529*4882a593Smuzhiyun /*
1530*4882a593Smuzhiyun  * The DMA ENGINE API
1531*4882a593Smuzhiyun  */
pl08x_free_chan_resources(struct dma_chan * chan)1532*4882a593Smuzhiyun static void pl08x_free_chan_resources(struct dma_chan *chan)
1533*4882a593Smuzhiyun {
1534*4882a593Smuzhiyun 	/* Ensure all queued descriptors are freed */
1535*4882a593Smuzhiyun 	vchan_free_chan_resources(to_virt_chan(chan));
1536*4882a593Smuzhiyun }
1537*4882a593Smuzhiyun 
pl08x_prep_dma_interrupt(struct dma_chan * chan,unsigned long flags)1538*4882a593Smuzhiyun static struct dma_async_tx_descriptor *pl08x_prep_dma_interrupt(
1539*4882a593Smuzhiyun 		struct dma_chan *chan, unsigned long flags)
1540*4882a593Smuzhiyun {
1541*4882a593Smuzhiyun 	struct dma_async_tx_descriptor *retval = NULL;
1542*4882a593Smuzhiyun 
1543*4882a593Smuzhiyun 	return retval;
1544*4882a593Smuzhiyun }
1545*4882a593Smuzhiyun 
1546*4882a593Smuzhiyun /*
1547*4882a593Smuzhiyun  * Code accessing dma_async_is_complete() in a tight loop may give problems.
1548*4882a593Smuzhiyun  * If slaves are relying on interrupts to signal completion this function
1549*4882a593Smuzhiyun  * must not be called with interrupts disabled.
1550*4882a593Smuzhiyun  */
pl08x_dma_tx_status(struct dma_chan * chan,dma_cookie_t cookie,struct dma_tx_state * txstate)1551*4882a593Smuzhiyun static enum dma_status pl08x_dma_tx_status(struct dma_chan *chan,
1552*4882a593Smuzhiyun 		dma_cookie_t cookie, struct dma_tx_state *txstate)
1553*4882a593Smuzhiyun {
1554*4882a593Smuzhiyun 	struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1555*4882a593Smuzhiyun 	struct virt_dma_desc *vd;
1556*4882a593Smuzhiyun 	unsigned long flags;
1557*4882a593Smuzhiyun 	enum dma_status ret;
1558*4882a593Smuzhiyun 	size_t bytes = 0;
1559*4882a593Smuzhiyun 
1560*4882a593Smuzhiyun 	ret = dma_cookie_status(chan, cookie, txstate);
1561*4882a593Smuzhiyun 	if (ret == DMA_COMPLETE)
1562*4882a593Smuzhiyun 		return ret;
1563*4882a593Smuzhiyun 
1564*4882a593Smuzhiyun 	/*
1565*4882a593Smuzhiyun 	 * There's no point calculating the residue if there's
1566*4882a593Smuzhiyun 	 * no txstate to store the value.
1567*4882a593Smuzhiyun 	 */
1568*4882a593Smuzhiyun 	if (!txstate) {
1569*4882a593Smuzhiyun 		if (plchan->state == PL08X_CHAN_PAUSED)
1570*4882a593Smuzhiyun 			ret = DMA_PAUSED;
1571*4882a593Smuzhiyun 		return ret;
1572*4882a593Smuzhiyun 	}
1573*4882a593Smuzhiyun 
1574*4882a593Smuzhiyun 	spin_lock_irqsave(&plchan->vc.lock, flags);
1575*4882a593Smuzhiyun 	ret = dma_cookie_status(chan, cookie, txstate);
1576*4882a593Smuzhiyun 	if (ret != DMA_COMPLETE) {
1577*4882a593Smuzhiyun 		vd = vchan_find_desc(&plchan->vc, cookie);
1578*4882a593Smuzhiyun 		if (vd) {
1579*4882a593Smuzhiyun 			/* On the issued list, so hasn't been processed yet */
1580*4882a593Smuzhiyun 			struct pl08x_txd *txd = to_pl08x_txd(&vd->tx);
1581*4882a593Smuzhiyun 			struct pl08x_sg *dsg;
1582*4882a593Smuzhiyun 
1583*4882a593Smuzhiyun 			list_for_each_entry(dsg, &txd->dsg_list, node)
1584*4882a593Smuzhiyun 				bytes += dsg->len;
1585*4882a593Smuzhiyun 		} else {
1586*4882a593Smuzhiyun 			bytes = pl08x_getbytes_chan(plchan);
1587*4882a593Smuzhiyun 		}
1588*4882a593Smuzhiyun 	}
1589*4882a593Smuzhiyun 	spin_unlock_irqrestore(&plchan->vc.lock, flags);
1590*4882a593Smuzhiyun 
1591*4882a593Smuzhiyun 	/*
1592*4882a593Smuzhiyun 	 * This cookie not complete yet
1593*4882a593Smuzhiyun 	 * Get number of bytes left in the active transactions and queue
1594*4882a593Smuzhiyun 	 */
1595*4882a593Smuzhiyun 	dma_set_residue(txstate, bytes);
1596*4882a593Smuzhiyun 
1597*4882a593Smuzhiyun 	if (plchan->state == PL08X_CHAN_PAUSED && ret == DMA_IN_PROGRESS)
1598*4882a593Smuzhiyun 		ret = DMA_PAUSED;
1599*4882a593Smuzhiyun 
1600*4882a593Smuzhiyun 	/* Whether waiting or running, we're in progress */
1601*4882a593Smuzhiyun 	return ret;
1602*4882a593Smuzhiyun }
1603*4882a593Smuzhiyun 
1604*4882a593Smuzhiyun /* PrimeCell DMA extension */
1605*4882a593Smuzhiyun struct burst_table {
1606*4882a593Smuzhiyun 	u32 burstwords;
1607*4882a593Smuzhiyun 	u32 reg;
1608*4882a593Smuzhiyun };
1609*4882a593Smuzhiyun 
1610*4882a593Smuzhiyun static const struct burst_table burst_sizes[] = {
1611*4882a593Smuzhiyun 	{
1612*4882a593Smuzhiyun 		.burstwords = 256,
1613*4882a593Smuzhiyun 		.reg = PL080_BSIZE_256,
1614*4882a593Smuzhiyun 	},
1615*4882a593Smuzhiyun 	{
1616*4882a593Smuzhiyun 		.burstwords = 128,
1617*4882a593Smuzhiyun 		.reg = PL080_BSIZE_128,
1618*4882a593Smuzhiyun 	},
1619*4882a593Smuzhiyun 	{
1620*4882a593Smuzhiyun 		.burstwords = 64,
1621*4882a593Smuzhiyun 		.reg = PL080_BSIZE_64,
1622*4882a593Smuzhiyun 	},
1623*4882a593Smuzhiyun 	{
1624*4882a593Smuzhiyun 		.burstwords = 32,
1625*4882a593Smuzhiyun 		.reg = PL080_BSIZE_32,
1626*4882a593Smuzhiyun 	},
1627*4882a593Smuzhiyun 	{
1628*4882a593Smuzhiyun 		.burstwords = 16,
1629*4882a593Smuzhiyun 		.reg = PL080_BSIZE_16,
1630*4882a593Smuzhiyun 	},
1631*4882a593Smuzhiyun 	{
1632*4882a593Smuzhiyun 		.burstwords = 8,
1633*4882a593Smuzhiyun 		.reg = PL080_BSIZE_8,
1634*4882a593Smuzhiyun 	},
1635*4882a593Smuzhiyun 	{
1636*4882a593Smuzhiyun 		.burstwords = 4,
1637*4882a593Smuzhiyun 		.reg = PL080_BSIZE_4,
1638*4882a593Smuzhiyun 	},
1639*4882a593Smuzhiyun 	{
1640*4882a593Smuzhiyun 		.burstwords = 0,
1641*4882a593Smuzhiyun 		.reg = PL080_BSIZE_1,
1642*4882a593Smuzhiyun 	},
1643*4882a593Smuzhiyun };
1644*4882a593Smuzhiyun 
1645*4882a593Smuzhiyun /*
1646*4882a593Smuzhiyun  * Given the source and destination available bus masks, select which
1647*4882a593Smuzhiyun  * will be routed to each port.  We try to have source and destination
1648*4882a593Smuzhiyun  * on separate ports, but always respect the allowable settings.
1649*4882a593Smuzhiyun  */
pl08x_select_bus(bool ftdmac020,u8 src,u8 dst)1650*4882a593Smuzhiyun static u32 pl08x_select_bus(bool ftdmac020, u8 src, u8 dst)
1651*4882a593Smuzhiyun {
1652*4882a593Smuzhiyun 	u32 cctl = 0;
1653*4882a593Smuzhiyun 	u32 dst_ahb2;
1654*4882a593Smuzhiyun 	u32 src_ahb2;
1655*4882a593Smuzhiyun 
1656*4882a593Smuzhiyun 	/* The FTDMAC020 use different bits to indicate src/dst bus */
1657*4882a593Smuzhiyun 	if (ftdmac020) {
1658*4882a593Smuzhiyun 		dst_ahb2 = FTDMAC020_LLI_DST_SEL;
1659*4882a593Smuzhiyun 		src_ahb2 = FTDMAC020_LLI_SRC_SEL;
1660*4882a593Smuzhiyun 	} else {
1661*4882a593Smuzhiyun 		dst_ahb2 = PL080_CONTROL_DST_AHB2;
1662*4882a593Smuzhiyun 		src_ahb2 = PL080_CONTROL_SRC_AHB2;
1663*4882a593Smuzhiyun 	}
1664*4882a593Smuzhiyun 
1665*4882a593Smuzhiyun 	if (!(dst & PL08X_AHB1) || ((dst & PL08X_AHB2) && (src & PL08X_AHB1)))
1666*4882a593Smuzhiyun 		cctl |= dst_ahb2;
1667*4882a593Smuzhiyun 	if (!(src & PL08X_AHB1) || ((src & PL08X_AHB2) && !(dst & PL08X_AHB2)))
1668*4882a593Smuzhiyun 		cctl |= src_ahb2;
1669*4882a593Smuzhiyun 
1670*4882a593Smuzhiyun 	return cctl;
1671*4882a593Smuzhiyun }
1672*4882a593Smuzhiyun 
pl08x_cctl(u32 cctl)1673*4882a593Smuzhiyun static u32 pl08x_cctl(u32 cctl)
1674*4882a593Smuzhiyun {
1675*4882a593Smuzhiyun 	cctl &= ~(PL080_CONTROL_SRC_AHB2 | PL080_CONTROL_DST_AHB2 |
1676*4882a593Smuzhiyun 		  PL080_CONTROL_SRC_INCR | PL080_CONTROL_DST_INCR |
1677*4882a593Smuzhiyun 		  PL080_CONTROL_PROT_MASK);
1678*4882a593Smuzhiyun 
1679*4882a593Smuzhiyun 	/* Access the cell in privileged mode, non-bufferable, non-cacheable */
1680*4882a593Smuzhiyun 	return cctl | PL080_CONTROL_PROT_SYS;
1681*4882a593Smuzhiyun }
1682*4882a593Smuzhiyun 
pl08x_width(enum dma_slave_buswidth width)1683*4882a593Smuzhiyun static u32 pl08x_width(enum dma_slave_buswidth width)
1684*4882a593Smuzhiyun {
1685*4882a593Smuzhiyun 	switch (width) {
1686*4882a593Smuzhiyun 	case DMA_SLAVE_BUSWIDTH_1_BYTE:
1687*4882a593Smuzhiyun 		return PL080_WIDTH_8BIT;
1688*4882a593Smuzhiyun 	case DMA_SLAVE_BUSWIDTH_2_BYTES:
1689*4882a593Smuzhiyun 		return PL080_WIDTH_16BIT;
1690*4882a593Smuzhiyun 	case DMA_SLAVE_BUSWIDTH_4_BYTES:
1691*4882a593Smuzhiyun 		return PL080_WIDTH_32BIT;
1692*4882a593Smuzhiyun 	default:
1693*4882a593Smuzhiyun 		return ~0;
1694*4882a593Smuzhiyun 	}
1695*4882a593Smuzhiyun }
1696*4882a593Smuzhiyun 
pl08x_burst(u32 maxburst)1697*4882a593Smuzhiyun static u32 pl08x_burst(u32 maxburst)
1698*4882a593Smuzhiyun {
1699*4882a593Smuzhiyun 	int i;
1700*4882a593Smuzhiyun 
1701*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(burst_sizes); i++)
1702*4882a593Smuzhiyun 		if (burst_sizes[i].burstwords <= maxburst)
1703*4882a593Smuzhiyun 			break;
1704*4882a593Smuzhiyun 
1705*4882a593Smuzhiyun 	return burst_sizes[i].reg;
1706*4882a593Smuzhiyun }
1707*4882a593Smuzhiyun 
pl08x_get_cctl(struct pl08x_dma_chan * plchan,enum dma_slave_buswidth addr_width,u32 maxburst)1708*4882a593Smuzhiyun static u32 pl08x_get_cctl(struct pl08x_dma_chan *plchan,
1709*4882a593Smuzhiyun 	enum dma_slave_buswidth addr_width, u32 maxburst)
1710*4882a593Smuzhiyun {
1711*4882a593Smuzhiyun 	u32 width, burst, cctl = 0;
1712*4882a593Smuzhiyun 
1713*4882a593Smuzhiyun 	width = pl08x_width(addr_width);
1714*4882a593Smuzhiyun 	if (width == ~0)
1715*4882a593Smuzhiyun 		return ~0;
1716*4882a593Smuzhiyun 
1717*4882a593Smuzhiyun 	cctl |= width << PL080_CONTROL_SWIDTH_SHIFT;
1718*4882a593Smuzhiyun 	cctl |= width << PL080_CONTROL_DWIDTH_SHIFT;
1719*4882a593Smuzhiyun 
1720*4882a593Smuzhiyun 	/*
1721*4882a593Smuzhiyun 	 * If this channel will only request single transfers, set this
1722*4882a593Smuzhiyun 	 * down to ONE element.  Also select one element if no maxburst
1723*4882a593Smuzhiyun 	 * is specified.
1724*4882a593Smuzhiyun 	 */
1725*4882a593Smuzhiyun 	if (plchan->cd->single)
1726*4882a593Smuzhiyun 		maxburst = 1;
1727*4882a593Smuzhiyun 
1728*4882a593Smuzhiyun 	burst = pl08x_burst(maxburst);
1729*4882a593Smuzhiyun 	cctl |= burst << PL080_CONTROL_SB_SIZE_SHIFT;
1730*4882a593Smuzhiyun 	cctl |= burst << PL080_CONTROL_DB_SIZE_SHIFT;
1731*4882a593Smuzhiyun 
1732*4882a593Smuzhiyun 	return pl08x_cctl(cctl);
1733*4882a593Smuzhiyun }
1734*4882a593Smuzhiyun 
1735*4882a593Smuzhiyun /*
1736*4882a593Smuzhiyun  * Slave transactions callback to the slave device to allow
1737*4882a593Smuzhiyun  * synchronization of slave DMA signals with the DMAC enable
1738*4882a593Smuzhiyun  */
pl08x_issue_pending(struct dma_chan * chan)1739*4882a593Smuzhiyun static void pl08x_issue_pending(struct dma_chan *chan)
1740*4882a593Smuzhiyun {
1741*4882a593Smuzhiyun 	struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1742*4882a593Smuzhiyun 	unsigned long flags;
1743*4882a593Smuzhiyun 
1744*4882a593Smuzhiyun 	spin_lock_irqsave(&plchan->vc.lock, flags);
1745*4882a593Smuzhiyun 	if (vchan_issue_pending(&plchan->vc)) {
1746*4882a593Smuzhiyun 		if (!plchan->phychan && plchan->state != PL08X_CHAN_WAITING)
1747*4882a593Smuzhiyun 			pl08x_phy_alloc_and_start(plchan);
1748*4882a593Smuzhiyun 	}
1749*4882a593Smuzhiyun 	spin_unlock_irqrestore(&plchan->vc.lock, flags);
1750*4882a593Smuzhiyun }
1751*4882a593Smuzhiyun 
pl08x_get_txd(struct pl08x_dma_chan * plchan)1752*4882a593Smuzhiyun static struct pl08x_txd *pl08x_get_txd(struct pl08x_dma_chan *plchan)
1753*4882a593Smuzhiyun {
1754*4882a593Smuzhiyun 	struct pl08x_txd *txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
1755*4882a593Smuzhiyun 
1756*4882a593Smuzhiyun 	if (txd)
1757*4882a593Smuzhiyun 		INIT_LIST_HEAD(&txd->dsg_list);
1758*4882a593Smuzhiyun 	return txd;
1759*4882a593Smuzhiyun }
1760*4882a593Smuzhiyun 
pl08x_memcpy_cctl(struct pl08x_driver_data * pl08x)1761*4882a593Smuzhiyun static u32 pl08x_memcpy_cctl(struct pl08x_driver_data *pl08x)
1762*4882a593Smuzhiyun {
1763*4882a593Smuzhiyun 	u32 cctl = 0;
1764*4882a593Smuzhiyun 
1765*4882a593Smuzhiyun 	/* Conjure cctl */
1766*4882a593Smuzhiyun 	switch (pl08x->pd->memcpy_burst_size) {
1767*4882a593Smuzhiyun 	default:
1768*4882a593Smuzhiyun 		dev_err(&pl08x->adev->dev,
1769*4882a593Smuzhiyun 			"illegal burst size for memcpy, set to 1\n");
1770*4882a593Smuzhiyun 		fallthrough;
1771*4882a593Smuzhiyun 	case PL08X_BURST_SZ_1:
1772*4882a593Smuzhiyun 		cctl |= PL080_BSIZE_1 << PL080_CONTROL_SB_SIZE_SHIFT |
1773*4882a593Smuzhiyun 			PL080_BSIZE_1 << PL080_CONTROL_DB_SIZE_SHIFT;
1774*4882a593Smuzhiyun 		break;
1775*4882a593Smuzhiyun 	case PL08X_BURST_SZ_4:
1776*4882a593Smuzhiyun 		cctl |= PL080_BSIZE_4 << PL080_CONTROL_SB_SIZE_SHIFT |
1777*4882a593Smuzhiyun 			PL080_BSIZE_4 << PL080_CONTROL_DB_SIZE_SHIFT;
1778*4882a593Smuzhiyun 		break;
1779*4882a593Smuzhiyun 	case PL08X_BURST_SZ_8:
1780*4882a593Smuzhiyun 		cctl |= PL080_BSIZE_8 << PL080_CONTROL_SB_SIZE_SHIFT |
1781*4882a593Smuzhiyun 			PL080_BSIZE_8 << PL080_CONTROL_DB_SIZE_SHIFT;
1782*4882a593Smuzhiyun 		break;
1783*4882a593Smuzhiyun 	case PL08X_BURST_SZ_16:
1784*4882a593Smuzhiyun 		cctl |= PL080_BSIZE_16 << PL080_CONTROL_SB_SIZE_SHIFT |
1785*4882a593Smuzhiyun 			PL080_BSIZE_16 << PL080_CONTROL_DB_SIZE_SHIFT;
1786*4882a593Smuzhiyun 		break;
1787*4882a593Smuzhiyun 	case PL08X_BURST_SZ_32:
1788*4882a593Smuzhiyun 		cctl |= PL080_BSIZE_32 << PL080_CONTROL_SB_SIZE_SHIFT |
1789*4882a593Smuzhiyun 			PL080_BSIZE_32 << PL080_CONTROL_DB_SIZE_SHIFT;
1790*4882a593Smuzhiyun 		break;
1791*4882a593Smuzhiyun 	case PL08X_BURST_SZ_64:
1792*4882a593Smuzhiyun 		cctl |= PL080_BSIZE_64 << PL080_CONTROL_SB_SIZE_SHIFT |
1793*4882a593Smuzhiyun 			PL080_BSIZE_64 << PL080_CONTROL_DB_SIZE_SHIFT;
1794*4882a593Smuzhiyun 		break;
1795*4882a593Smuzhiyun 	case PL08X_BURST_SZ_128:
1796*4882a593Smuzhiyun 		cctl |= PL080_BSIZE_128 << PL080_CONTROL_SB_SIZE_SHIFT |
1797*4882a593Smuzhiyun 			PL080_BSIZE_128 << PL080_CONTROL_DB_SIZE_SHIFT;
1798*4882a593Smuzhiyun 		break;
1799*4882a593Smuzhiyun 	case PL08X_BURST_SZ_256:
1800*4882a593Smuzhiyun 		cctl |= PL080_BSIZE_256 << PL080_CONTROL_SB_SIZE_SHIFT |
1801*4882a593Smuzhiyun 			PL080_BSIZE_256 << PL080_CONTROL_DB_SIZE_SHIFT;
1802*4882a593Smuzhiyun 		break;
1803*4882a593Smuzhiyun 	}
1804*4882a593Smuzhiyun 
1805*4882a593Smuzhiyun 	switch (pl08x->pd->memcpy_bus_width) {
1806*4882a593Smuzhiyun 	default:
1807*4882a593Smuzhiyun 		dev_err(&pl08x->adev->dev,
1808*4882a593Smuzhiyun 			"illegal bus width for memcpy, set to 8 bits\n");
1809*4882a593Smuzhiyun 		fallthrough;
1810*4882a593Smuzhiyun 	case PL08X_BUS_WIDTH_8_BITS:
1811*4882a593Smuzhiyun 		cctl |= PL080_WIDTH_8BIT << PL080_CONTROL_SWIDTH_SHIFT |
1812*4882a593Smuzhiyun 			PL080_WIDTH_8BIT << PL080_CONTROL_DWIDTH_SHIFT;
1813*4882a593Smuzhiyun 		break;
1814*4882a593Smuzhiyun 	case PL08X_BUS_WIDTH_16_BITS:
1815*4882a593Smuzhiyun 		cctl |= PL080_WIDTH_16BIT << PL080_CONTROL_SWIDTH_SHIFT |
1816*4882a593Smuzhiyun 			PL080_WIDTH_16BIT << PL080_CONTROL_DWIDTH_SHIFT;
1817*4882a593Smuzhiyun 		break;
1818*4882a593Smuzhiyun 	case PL08X_BUS_WIDTH_32_BITS:
1819*4882a593Smuzhiyun 		cctl |= PL080_WIDTH_32BIT << PL080_CONTROL_SWIDTH_SHIFT |
1820*4882a593Smuzhiyun 			PL080_WIDTH_32BIT << PL080_CONTROL_DWIDTH_SHIFT;
1821*4882a593Smuzhiyun 		break;
1822*4882a593Smuzhiyun 	}
1823*4882a593Smuzhiyun 
1824*4882a593Smuzhiyun 	/* Protection flags */
1825*4882a593Smuzhiyun 	if (pl08x->pd->memcpy_prot_buff)
1826*4882a593Smuzhiyun 		cctl |= PL080_CONTROL_PROT_BUFF;
1827*4882a593Smuzhiyun 	if (pl08x->pd->memcpy_prot_cache)
1828*4882a593Smuzhiyun 		cctl |= PL080_CONTROL_PROT_CACHE;
1829*4882a593Smuzhiyun 
1830*4882a593Smuzhiyun 	/* We are the kernel, so we are in privileged mode */
1831*4882a593Smuzhiyun 	cctl |= PL080_CONTROL_PROT_SYS;
1832*4882a593Smuzhiyun 
1833*4882a593Smuzhiyun 	/* Both to be incremented or the code will break */
1834*4882a593Smuzhiyun 	cctl |= PL080_CONTROL_SRC_INCR | PL080_CONTROL_DST_INCR;
1835*4882a593Smuzhiyun 
1836*4882a593Smuzhiyun 	if (pl08x->vd->dualmaster)
1837*4882a593Smuzhiyun 		cctl |= pl08x_select_bus(false,
1838*4882a593Smuzhiyun 					 pl08x->mem_buses,
1839*4882a593Smuzhiyun 					 pl08x->mem_buses);
1840*4882a593Smuzhiyun 
1841*4882a593Smuzhiyun 	return cctl;
1842*4882a593Smuzhiyun }
1843*4882a593Smuzhiyun 
pl08x_ftdmac020_memcpy_cctl(struct pl08x_driver_data * pl08x)1844*4882a593Smuzhiyun static u32 pl08x_ftdmac020_memcpy_cctl(struct pl08x_driver_data *pl08x)
1845*4882a593Smuzhiyun {
1846*4882a593Smuzhiyun 	u32 cctl = 0;
1847*4882a593Smuzhiyun 
1848*4882a593Smuzhiyun 	/* Conjure cctl */
1849*4882a593Smuzhiyun 	switch (pl08x->pd->memcpy_bus_width) {
1850*4882a593Smuzhiyun 	default:
1851*4882a593Smuzhiyun 		dev_err(&pl08x->adev->dev,
1852*4882a593Smuzhiyun 			"illegal bus width for memcpy, set to 8 bits\n");
1853*4882a593Smuzhiyun 		fallthrough;
1854*4882a593Smuzhiyun 	case PL08X_BUS_WIDTH_8_BITS:
1855*4882a593Smuzhiyun 		cctl |= PL080_WIDTH_8BIT << FTDMAC020_LLI_SRC_WIDTH_SHIFT |
1856*4882a593Smuzhiyun 			PL080_WIDTH_8BIT << FTDMAC020_LLI_DST_WIDTH_SHIFT;
1857*4882a593Smuzhiyun 		break;
1858*4882a593Smuzhiyun 	case PL08X_BUS_WIDTH_16_BITS:
1859*4882a593Smuzhiyun 		cctl |= PL080_WIDTH_16BIT << FTDMAC020_LLI_SRC_WIDTH_SHIFT |
1860*4882a593Smuzhiyun 			PL080_WIDTH_16BIT << FTDMAC020_LLI_DST_WIDTH_SHIFT;
1861*4882a593Smuzhiyun 		break;
1862*4882a593Smuzhiyun 	case PL08X_BUS_WIDTH_32_BITS:
1863*4882a593Smuzhiyun 		cctl |= PL080_WIDTH_32BIT << FTDMAC020_LLI_SRC_WIDTH_SHIFT |
1864*4882a593Smuzhiyun 			PL080_WIDTH_32BIT << FTDMAC020_LLI_DST_WIDTH_SHIFT;
1865*4882a593Smuzhiyun 		break;
1866*4882a593Smuzhiyun 	}
1867*4882a593Smuzhiyun 
1868*4882a593Smuzhiyun 	/*
1869*4882a593Smuzhiyun 	 * By default mask the TC IRQ on all LLIs, it will be unmasked on
1870*4882a593Smuzhiyun 	 * the last LLI item by other code.
1871*4882a593Smuzhiyun 	 */
1872*4882a593Smuzhiyun 	cctl |= FTDMAC020_LLI_TC_MSK;
1873*4882a593Smuzhiyun 
1874*4882a593Smuzhiyun 	/*
1875*4882a593Smuzhiyun 	 * Both to be incremented so leave bits FTDMAC020_LLI_SRCAD_CTL
1876*4882a593Smuzhiyun 	 * and FTDMAC020_LLI_DSTAD_CTL as zero
1877*4882a593Smuzhiyun 	 */
1878*4882a593Smuzhiyun 	if (pl08x->vd->dualmaster)
1879*4882a593Smuzhiyun 		cctl |= pl08x_select_bus(true,
1880*4882a593Smuzhiyun 					 pl08x->mem_buses,
1881*4882a593Smuzhiyun 					 pl08x->mem_buses);
1882*4882a593Smuzhiyun 
1883*4882a593Smuzhiyun 	return cctl;
1884*4882a593Smuzhiyun }
1885*4882a593Smuzhiyun 
1886*4882a593Smuzhiyun /*
1887*4882a593Smuzhiyun  * Initialize a descriptor to be used by memcpy submit
1888*4882a593Smuzhiyun  */
pl08x_prep_dma_memcpy(struct dma_chan * chan,dma_addr_t dest,dma_addr_t src,size_t len,unsigned long flags)1889*4882a593Smuzhiyun static struct dma_async_tx_descriptor *pl08x_prep_dma_memcpy(
1890*4882a593Smuzhiyun 		struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
1891*4882a593Smuzhiyun 		size_t len, unsigned long flags)
1892*4882a593Smuzhiyun {
1893*4882a593Smuzhiyun 	struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1894*4882a593Smuzhiyun 	struct pl08x_driver_data *pl08x = plchan->host;
1895*4882a593Smuzhiyun 	struct pl08x_txd *txd;
1896*4882a593Smuzhiyun 	struct pl08x_sg *dsg;
1897*4882a593Smuzhiyun 	int ret;
1898*4882a593Smuzhiyun 
1899*4882a593Smuzhiyun 	txd = pl08x_get_txd(plchan);
1900*4882a593Smuzhiyun 	if (!txd) {
1901*4882a593Smuzhiyun 		dev_err(&pl08x->adev->dev,
1902*4882a593Smuzhiyun 			"%s no memory for descriptor\n", __func__);
1903*4882a593Smuzhiyun 		return NULL;
1904*4882a593Smuzhiyun 	}
1905*4882a593Smuzhiyun 
1906*4882a593Smuzhiyun 	dsg = kzalloc(sizeof(struct pl08x_sg), GFP_NOWAIT);
1907*4882a593Smuzhiyun 	if (!dsg) {
1908*4882a593Smuzhiyun 		pl08x_free_txd(pl08x, txd);
1909*4882a593Smuzhiyun 		return NULL;
1910*4882a593Smuzhiyun 	}
1911*4882a593Smuzhiyun 	list_add_tail(&dsg->node, &txd->dsg_list);
1912*4882a593Smuzhiyun 
1913*4882a593Smuzhiyun 	dsg->src_addr = src;
1914*4882a593Smuzhiyun 	dsg->dst_addr = dest;
1915*4882a593Smuzhiyun 	dsg->len = len;
1916*4882a593Smuzhiyun 	if (pl08x->vd->ftdmac020) {
1917*4882a593Smuzhiyun 		/* Writing CCFG zero ENABLES all interrupts */
1918*4882a593Smuzhiyun 		txd->ccfg = 0;
1919*4882a593Smuzhiyun 		txd->cctl = pl08x_ftdmac020_memcpy_cctl(pl08x);
1920*4882a593Smuzhiyun 	} else {
1921*4882a593Smuzhiyun 		txd->ccfg = PL080_CONFIG_ERR_IRQ_MASK |
1922*4882a593Smuzhiyun 			PL080_CONFIG_TC_IRQ_MASK |
1923*4882a593Smuzhiyun 			PL080_FLOW_MEM2MEM << PL080_CONFIG_FLOW_CONTROL_SHIFT;
1924*4882a593Smuzhiyun 		txd->cctl = pl08x_memcpy_cctl(pl08x);
1925*4882a593Smuzhiyun 	}
1926*4882a593Smuzhiyun 
1927*4882a593Smuzhiyun 	ret = pl08x_fill_llis_for_desc(plchan->host, txd);
1928*4882a593Smuzhiyun 	if (!ret) {
1929*4882a593Smuzhiyun 		pl08x_free_txd(pl08x, txd);
1930*4882a593Smuzhiyun 		return NULL;
1931*4882a593Smuzhiyun 	}
1932*4882a593Smuzhiyun 
1933*4882a593Smuzhiyun 	return vchan_tx_prep(&plchan->vc, &txd->vd, flags);
1934*4882a593Smuzhiyun }
1935*4882a593Smuzhiyun 
pl08x_init_txd(struct dma_chan * chan,enum dma_transfer_direction direction,dma_addr_t * slave_addr)1936*4882a593Smuzhiyun static struct pl08x_txd *pl08x_init_txd(
1937*4882a593Smuzhiyun 		struct dma_chan *chan,
1938*4882a593Smuzhiyun 		enum dma_transfer_direction direction,
1939*4882a593Smuzhiyun 		dma_addr_t *slave_addr)
1940*4882a593Smuzhiyun {
1941*4882a593Smuzhiyun 	struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1942*4882a593Smuzhiyun 	struct pl08x_driver_data *pl08x = plchan->host;
1943*4882a593Smuzhiyun 	struct pl08x_txd *txd;
1944*4882a593Smuzhiyun 	enum dma_slave_buswidth addr_width;
1945*4882a593Smuzhiyun 	int ret, tmp;
1946*4882a593Smuzhiyun 	u8 src_buses, dst_buses;
1947*4882a593Smuzhiyun 	u32 maxburst, cctl;
1948*4882a593Smuzhiyun 
1949*4882a593Smuzhiyun 	txd = pl08x_get_txd(plchan);
1950*4882a593Smuzhiyun 	if (!txd) {
1951*4882a593Smuzhiyun 		dev_err(&pl08x->adev->dev, "%s no txd\n", __func__);
1952*4882a593Smuzhiyun 		return NULL;
1953*4882a593Smuzhiyun 	}
1954*4882a593Smuzhiyun 
1955*4882a593Smuzhiyun 	/*
1956*4882a593Smuzhiyun 	 * Set up addresses, the PrimeCell configured address
1957*4882a593Smuzhiyun 	 * will take precedence since this may configure the
1958*4882a593Smuzhiyun 	 * channel target address dynamically at runtime.
1959*4882a593Smuzhiyun 	 */
1960*4882a593Smuzhiyun 	if (direction == DMA_MEM_TO_DEV) {
1961*4882a593Smuzhiyun 		cctl = PL080_CONTROL_SRC_INCR;
1962*4882a593Smuzhiyun 		*slave_addr = plchan->cfg.dst_addr;
1963*4882a593Smuzhiyun 		addr_width = plchan->cfg.dst_addr_width;
1964*4882a593Smuzhiyun 		maxburst = plchan->cfg.dst_maxburst;
1965*4882a593Smuzhiyun 		src_buses = pl08x->mem_buses;
1966*4882a593Smuzhiyun 		dst_buses = plchan->cd->periph_buses;
1967*4882a593Smuzhiyun 	} else if (direction == DMA_DEV_TO_MEM) {
1968*4882a593Smuzhiyun 		cctl = PL080_CONTROL_DST_INCR;
1969*4882a593Smuzhiyun 		*slave_addr = plchan->cfg.src_addr;
1970*4882a593Smuzhiyun 		addr_width = plchan->cfg.src_addr_width;
1971*4882a593Smuzhiyun 		maxburst = plchan->cfg.src_maxburst;
1972*4882a593Smuzhiyun 		src_buses = plchan->cd->periph_buses;
1973*4882a593Smuzhiyun 		dst_buses = pl08x->mem_buses;
1974*4882a593Smuzhiyun 	} else {
1975*4882a593Smuzhiyun 		pl08x_free_txd(pl08x, txd);
1976*4882a593Smuzhiyun 		dev_err(&pl08x->adev->dev,
1977*4882a593Smuzhiyun 			"%s direction unsupported\n", __func__);
1978*4882a593Smuzhiyun 		return NULL;
1979*4882a593Smuzhiyun 	}
1980*4882a593Smuzhiyun 
1981*4882a593Smuzhiyun 	cctl |= pl08x_get_cctl(plchan, addr_width, maxburst);
1982*4882a593Smuzhiyun 	if (cctl == ~0) {
1983*4882a593Smuzhiyun 		pl08x_free_txd(pl08x, txd);
1984*4882a593Smuzhiyun 		dev_err(&pl08x->adev->dev,
1985*4882a593Smuzhiyun 			"DMA slave configuration botched?\n");
1986*4882a593Smuzhiyun 		return NULL;
1987*4882a593Smuzhiyun 	}
1988*4882a593Smuzhiyun 
1989*4882a593Smuzhiyun 	txd->cctl = cctl | pl08x_select_bus(false, src_buses, dst_buses);
1990*4882a593Smuzhiyun 
1991*4882a593Smuzhiyun 	if (plchan->cfg.device_fc)
1992*4882a593Smuzhiyun 		tmp = (direction == DMA_MEM_TO_DEV) ? PL080_FLOW_MEM2PER_PER :
1993*4882a593Smuzhiyun 			PL080_FLOW_PER2MEM_PER;
1994*4882a593Smuzhiyun 	else
1995*4882a593Smuzhiyun 		tmp = (direction == DMA_MEM_TO_DEV) ? PL080_FLOW_MEM2PER :
1996*4882a593Smuzhiyun 			PL080_FLOW_PER2MEM;
1997*4882a593Smuzhiyun 
1998*4882a593Smuzhiyun 	txd->ccfg = PL080_CONFIG_ERR_IRQ_MASK |
1999*4882a593Smuzhiyun 		PL080_CONFIG_TC_IRQ_MASK |
2000*4882a593Smuzhiyun 		tmp << PL080_CONFIG_FLOW_CONTROL_SHIFT;
2001*4882a593Smuzhiyun 
2002*4882a593Smuzhiyun 	ret = pl08x_request_mux(plchan);
2003*4882a593Smuzhiyun 	if (ret < 0) {
2004*4882a593Smuzhiyun 		pl08x_free_txd(pl08x, txd);
2005*4882a593Smuzhiyun 		dev_dbg(&pl08x->adev->dev,
2006*4882a593Smuzhiyun 			"unable to mux for transfer on %s due to platform restrictions\n",
2007*4882a593Smuzhiyun 			plchan->name);
2008*4882a593Smuzhiyun 		return NULL;
2009*4882a593Smuzhiyun 	}
2010*4882a593Smuzhiyun 
2011*4882a593Smuzhiyun 	dev_dbg(&pl08x->adev->dev, "allocated DMA request signal %d for xfer on %s\n",
2012*4882a593Smuzhiyun 		 plchan->signal, plchan->name);
2013*4882a593Smuzhiyun 
2014*4882a593Smuzhiyun 	/* Assign the flow control signal to this channel */
2015*4882a593Smuzhiyun 	if (direction == DMA_MEM_TO_DEV)
2016*4882a593Smuzhiyun 		txd->ccfg |= plchan->signal << PL080_CONFIG_DST_SEL_SHIFT;
2017*4882a593Smuzhiyun 	else
2018*4882a593Smuzhiyun 		txd->ccfg |= plchan->signal << PL080_CONFIG_SRC_SEL_SHIFT;
2019*4882a593Smuzhiyun 
2020*4882a593Smuzhiyun 	return txd;
2021*4882a593Smuzhiyun }
2022*4882a593Smuzhiyun 
pl08x_tx_add_sg(struct pl08x_txd * txd,enum dma_transfer_direction direction,dma_addr_t slave_addr,dma_addr_t buf_addr,unsigned int len)2023*4882a593Smuzhiyun static int pl08x_tx_add_sg(struct pl08x_txd *txd,
2024*4882a593Smuzhiyun 			   enum dma_transfer_direction direction,
2025*4882a593Smuzhiyun 			   dma_addr_t slave_addr,
2026*4882a593Smuzhiyun 			   dma_addr_t buf_addr,
2027*4882a593Smuzhiyun 			   unsigned int len)
2028*4882a593Smuzhiyun {
2029*4882a593Smuzhiyun 	struct pl08x_sg *dsg;
2030*4882a593Smuzhiyun 
2031*4882a593Smuzhiyun 	dsg = kzalloc(sizeof(struct pl08x_sg), GFP_NOWAIT);
2032*4882a593Smuzhiyun 	if (!dsg)
2033*4882a593Smuzhiyun 		return -ENOMEM;
2034*4882a593Smuzhiyun 
2035*4882a593Smuzhiyun 	list_add_tail(&dsg->node, &txd->dsg_list);
2036*4882a593Smuzhiyun 
2037*4882a593Smuzhiyun 	dsg->len = len;
2038*4882a593Smuzhiyun 	if (direction == DMA_MEM_TO_DEV) {
2039*4882a593Smuzhiyun 		dsg->src_addr = buf_addr;
2040*4882a593Smuzhiyun 		dsg->dst_addr = slave_addr;
2041*4882a593Smuzhiyun 	} else {
2042*4882a593Smuzhiyun 		dsg->src_addr = slave_addr;
2043*4882a593Smuzhiyun 		dsg->dst_addr = buf_addr;
2044*4882a593Smuzhiyun 	}
2045*4882a593Smuzhiyun 
2046*4882a593Smuzhiyun 	return 0;
2047*4882a593Smuzhiyun }
2048*4882a593Smuzhiyun 
pl08x_prep_slave_sg(struct dma_chan * chan,struct scatterlist * sgl,unsigned int sg_len,enum dma_transfer_direction direction,unsigned long flags,void * context)2049*4882a593Smuzhiyun static struct dma_async_tx_descriptor *pl08x_prep_slave_sg(
2050*4882a593Smuzhiyun 		struct dma_chan *chan, struct scatterlist *sgl,
2051*4882a593Smuzhiyun 		unsigned int sg_len, enum dma_transfer_direction direction,
2052*4882a593Smuzhiyun 		unsigned long flags, void *context)
2053*4882a593Smuzhiyun {
2054*4882a593Smuzhiyun 	struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
2055*4882a593Smuzhiyun 	struct pl08x_driver_data *pl08x = plchan->host;
2056*4882a593Smuzhiyun 	struct pl08x_txd *txd;
2057*4882a593Smuzhiyun 	struct scatterlist *sg;
2058*4882a593Smuzhiyun 	int ret, tmp;
2059*4882a593Smuzhiyun 	dma_addr_t slave_addr;
2060*4882a593Smuzhiyun 
2061*4882a593Smuzhiyun 	dev_dbg(&pl08x->adev->dev, "%s prepare transaction of %d bytes from %s\n",
2062*4882a593Smuzhiyun 			__func__, sg_dma_len(sgl), plchan->name);
2063*4882a593Smuzhiyun 
2064*4882a593Smuzhiyun 	txd = pl08x_init_txd(chan, direction, &slave_addr);
2065*4882a593Smuzhiyun 	if (!txd)
2066*4882a593Smuzhiyun 		return NULL;
2067*4882a593Smuzhiyun 
2068*4882a593Smuzhiyun 	for_each_sg(sgl, sg, sg_len, tmp) {
2069*4882a593Smuzhiyun 		ret = pl08x_tx_add_sg(txd, direction, slave_addr,
2070*4882a593Smuzhiyun 				      sg_dma_address(sg),
2071*4882a593Smuzhiyun 				      sg_dma_len(sg));
2072*4882a593Smuzhiyun 		if (ret) {
2073*4882a593Smuzhiyun 			pl08x_release_mux(plchan);
2074*4882a593Smuzhiyun 			pl08x_free_txd(pl08x, txd);
2075*4882a593Smuzhiyun 			dev_err(&pl08x->adev->dev, "%s no mem for pl080 sg\n",
2076*4882a593Smuzhiyun 					__func__);
2077*4882a593Smuzhiyun 			return NULL;
2078*4882a593Smuzhiyun 		}
2079*4882a593Smuzhiyun 	}
2080*4882a593Smuzhiyun 
2081*4882a593Smuzhiyun 	ret = pl08x_fill_llis_for_desc(plchan->host, txd);
2082*4882a593Smuzhiyun 	if (!ret) {
2083*4882a593Smuzhiyun 		pl08x_release_mux(plchan);
2084*4882a593Smuzhiyun 		pl08x_free_txd(pl08x, txd);
2085*4882a593Smuzhiyun 		return NULL;
2086*4882a593Smuzhiyun 	}
2087*4882a593Smuzhiyun 
2088*4882a593Smuzhiyun 	return vchan_tx_prep(&plchan->vc, &txd->vd, flags);
2089*4882a593Smuzhiyun }
2090*4882a593Smuzhiyun 
pl08x_prep_dma_cyclic(struct dma_chan * chan,dma_addr_t buf_addr,size_t buf_len,size_t period_len,enum dma_transfer_direction direction,unsigned long flags)2091*4882a593Smuzhiyun static struct dma_async_tx_descriptor *pl08x_prep_dma_cyclic(
2092*4882a593Smuzhiyun 		struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
2093*4882a593Smuzhiyun 		size_t period_len, enum dma_transfer_direction direction,
2094*4882a593Smuzhiyun 		unsigned long flags)
2095*4882a593Smuzhiyun {
2096*4882a593Smuzhiyun 	struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
2097*4882a593Smuzhiyun 	struct pl08x_driver_data *pl08x = plchan->host;
2098*4882a593Smuzhiyun 	struct pl08x_txd *txd;
2099*4882a593Smuzhiyun 	int ret, tmp;
2100*4882a593Smuzhiyun 	dma_addr_t slave_addr;
2101*4882a593Smuzhiyun 
2102*4882a593Smuzhiyun 	dev_dbg(&pl08x->adev->dev,
2103*4882a593Smuzhiyun 		"%s prepare cyclic transaction of %zd/%zd bytes %s %s\n",
2104*4882a593Smuzhiyun 		__func__, period_len, buf_len,
2105*4882a593Smuzhiyun 		direction == DMA_MEM_TO_DEV ? "to" : "from",
2106*4882a593Smuzhiyun 		plchan->name);
2107*4882a593Smuzhiyun 
2108*4882a593Smuzhiyun 	txd = pl08x_init_txd(chan, direction, &slave_addr);
2109*4882a593Smuzhiyun 	if (!txd)
2110*4882a593Smuzhiyun 		return NULL;
2111*4882a593Smuzhiyun 
2112*4882a593Smuzhiyun 	txd->cyclic = true;
2113*4882a593Smuzhiyun 	txd->cctl |= PL080_CONTROL_TC_IRQ_EN;
2114*4882a593Smuzhiyun 	for (tmp = 0; tmp < buf_len; tmp += period_len) {
2115*4882a593Smuzhiyun 		ret = pl08x_tx_add_sg(txd, direction, slave_addr,
2116*4882a593Smuzhiyun 				      buf_addr + tmp, period_len);
2117*4882a593Smuzhiyun 		if (ret) {
2118*4882a593Smuzhiyun 			pl08x_release_mux(plchan);
2119*4882a593Smuzhiyun 			pl08x_free_txd(pl08x, txd);
2120*4882a593Smuzhiyun 			return NULL;
2121*4882a593Smuzhiyun 		}
2122*4882a593Smuzhiyun 	}
2123*4882a593Smuzhiyun 
2124*4882a593Smuzhiyun 	ret = pl08x_fill_llis_for_desc(plchan->host, txd);
2125*4882a593Smuzhiyun 	if (!ret) {
2126*4882a593Smuzhiyun 		pl08x_release_mux(plchan);
2127*4882a593Smuzhiyun 		pl08x_free_txd(pl08x, txd);
2128*4882a593Smuzhiyun 		return NULL;
2129*4882a593Smuzhiyun 	}
2130*4882a593Smuzhiyun 
2131*4882a593Smuzhiyun 	return vchan_tx_prep(&plchan->vc, &txd->vd, flags);
2132*4882a593Smuzhiyun }
2133*4882a593Smuzhiyun 
pl08x_config(struct dma_chan * chan,struct dma_slave_config * config)2134*4882a593Smuzhiyun static int pl08x_config(struct dma_chan *chan,
2135*4882a593Smuzhiyun 			struct dma_slave_config *config)
2136*4882a593Smuzhiyun {
2137*4882a593Smuzhiyun 	struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
2138*4882a593Smuzhiyun 	struct pl08x_driver_data *pl08x = plchan->host;
2139*4882a593Smuzhiyun 
2140*4882a593Smuzhiyun 	if (!plchan->slave)
2141*4882a593Smuzhiyun 		return -EINVAL;
2142*4882a593Smuzhiyun 
2143*4882a593Smuzhiyun 	/* Reject definitely invalid configurations */
2144*4882a593Smuzhiyun 	if (config->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
2145*4882a593Smuzhiyun 	    config->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
2146*4882a593Smuzhiyun 		return -EINVAL;
2147*4882a593Smuzhiyun 
2148*4882a593Smuzhiyun 	if (config->device_fc && pl08x->vd->pl080s) {
2149*4882a593Smuzhiyun 		dev_err(&pl08x->adev->dev,
2150*4882a593Smuzhiyun 			"%s: PL080S does not support peripheral flow control\n",
2151*4882a593Smuzhiyun 			__func__);
2152*4882a593Smuzhiyun 		return -EINVAL;
2153*4882a593Smuzhiyun 	}
2154*4882a593Smuzhiyun 
2155*4882a593Smuzhiyun 	plchan->cfg = *config;
2156*4882a593Smuzhiyun 
2157*4882a593Smuzhiyun 	return 0;
2158*4882a593Smuzhiyun }
2159*4882a593Smuzhiyun 
pl08x_terminate_all(struct dma_chan * chan)2160*4882a593Smuzhiyun static int pl08x_terminate_all(struct dma_chan *chan)
2161*4882a593Smuzhiyun {
2162*4882a593Smuzhiyun 	struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
2163*4882a593Smuzhiyun 	struct pl08x_driver_data *pl08x = plchan->host;
2164*4882a593Smuzhiyun 	unsigned long flags;
2165*4882a593Smuzhiyun 
2166*4882a593Smuzhiyun 	spin_lock_irqsave(&plchan->vc.lock, flags);
2167*4882a593Smuzhiyun 	if (!plchan->phychan && !plchan->at) {
2168*4882a593Smuzhiyun 		spin_unlock_irqrestore(&plchan->vc.lock, flags);
2169*4882a593Smuzhiyun 		return 0;
2170*4882a593Smuzhiyun 	}
2171*4882a593Smuzhiyun 
2172*4882a593Smuzhiyun 	plchan->state = PL08X_CHAN_IDLE;
2173*4882a593Smuzhiyun 
2174*4882a593Smuzhiyun 	if (plchan->phychan) {
2175*4882a593Smuzhiyun 		/*
2176*4882a593Smuzhiyun 		 * Mark physical channel as free and free any slave
2177*4882a593Smuzhiyun 		 * signal
2178*4882a593Smuzhiyun 		 */
2179*4882a593Smuzhiyun 		pl08x_phy_free(plchan);
2180*4882a593Smuzhiyun 	}
2181*4882a593Smuzhiyun 	/* Dequeue jobs and free LLIs */
2182*4882a593Smuzhiyun 	if (plchan->at) {
2183*4882a593Smuzhiyun 		vchan_terminate_vdesc(&plchan->at->vd);
2184*4882a593Smuzhiyun 		plchan->at = NULL;
2185*4882a593Smuzhiyun 	}
2186*4882a593Smuzhiyun 	/* Dequeue jobs not yet fired as well */
2187*4882a593Smuzhiyun 	pl08x_free_txd_list(pl08x, plchan);
2188*4882a593Smuzhiyun 
2189*4882a593Smuzhiyun 	spin_unlock_irqrestore(&plchan->vc.lock, flags);
2190*4882a593Smuzhiyun 
2191*4882a593Smuzhiyun 	return 0;
2192*4882a593Smuzhiyun }
2193*4882a593Smuzhiyun 
pl08x_synchronize(struct dma_chan * chan)2194*4882a593Smuzhiyun static void pl08x_synchronize(struct dma_chan *chan)
2195*4882a593Smuzhiyun {
2196*4882a593Smuzhiyun 	struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
2197*4882a593Smuzhiyun 
2198*4882a593Smuzhiyun 	vchan_synchronize(&plchan->vc);
2199*4882a593Smuzhiyun }
2200*4882a593Smuzhiyun 
pl08x_pause(struct dma_chan * chan)2201*4882a593Smuzhiyun static int pl08x_pause(struct dma_chan *chan)
2202*4882a593Smuzhiyun {
2203*4882a593Smuzhiyun 	struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
2204*4882a593Smuzhiyun 	unsigned long flags;
2205*4882a593Smuzhiyun 
2206*4882a593Smuzhiyun 	/*
2207*4882a593Smuzhiyun 	 * Anything succeeds on channels with no physical allocation and
2208*4882a593Smuzhiyun 	 * no queued transfers.
2209*4882a593Smuzhiyun 	 */
2210*4882a593Smuzhiyun 	spin_lock_irqsave(&plchan->vc.lock, flags);
2211*4882a593Smuzhiyun 	if (!plchan->phychan && !plchan->at) {
2212*4882a593Smuzhiyun 		spin_unlock_irqrestore(&plchan->vc.lock, flags);
2213*4882a593Smuzhiyun 		return 0;
2214*4882a593Smuzhiyun 	}
2215*4882a593Smuzhiyun 
2216*4882a593Smuzhiyun 	pl08x_pause_phy_chan(plchan->phychan);
2217*4882a593Smuzhiyun 	plchan->state = PL08X_CHAN_PAUSED;
2218*4882a593Smuzhiyun 
2219*4882a593Smuzhiyun 	spin_unlock_irqrestore(&plchan->vc.lock, flags);
2220*4882a593Smuzhiyun 
2221*4882a593Smuzhiyun 	return 0;
2222*4882a593Smuzhiyun }
2223*4882a593Smuzhiyun 
pl08x_resume(struct dma_chan * chan)2224*4882a593Smuzhiyun static int pl08x_resume(struct dma_chan *chan)
2225*4882a593Smuzhiyun {
2226*4882a593Smuzhiyun 	struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
2227*4882a593Smuzhiyun 	unsigned long flags;
2228*4882a593Smuzhiyun 
2229*4882a593Smuzhiyun 	/*
2230*4882a593Smuzhiyun 	 * Anything succeeds on channels with no physical allocation and
2231*4882a593Smuzhiyun 	 * no queued transfers.
2232*4882a593Smuzhiyun 	 */
2233*4882a593Smuzhiyun 	spin_lock_irqsave(&plchan->vc.lock, flags);
2234*4882a593Smuzhiyun 	if (!plchan->phychan && !plchan->at) {
2235*4882a593Smuzhiyun 		spin_unlock_irqrestore(&plchan->vc.lock, flags);
2236*4882a593Smuzhiyun 		return 0;
2237*4882a593Smuzhiyun 	}
2238*4882a593Smuzhiyun 
2239*4882a593Smuzhiyun 	pl08x_resume_phy_chan(plchan->phychan);
2240*4882a593Smuzhiyun 	plchan->state = PL08X_CHAN_RUNNING;
2241*4882a593Smuzhiyun 
2242*4882a593Smuzhiyun 	spin_unlock_irqrestore(&plchan->vc.lock, flags);
2243*4882a593Smuzhiyun 
2244*4882a593Smuzhiyun 	return 0;
2245*4882a593Smuzhiyun }
2246*4882a593Smuzhiyun 
pl08x_filter_id(struct dma_chan * chan,void * chan_id)2247*4882a593Smuzhiyun bool pl08x_filter_id(struct dma_chan *chan, void *chan_id)
2248*4882a593Smuzhiyun {
2249*4882a593Smuzhiyun 	struct pl08x_dma_chan *plchan;
2250*4882a593Smuzhiyun 	char *name = chan_id;
2251*4882a593Smuzhiyun 
2252*4882a593Smuzhiyun 	/* Reject channels for devices not bound to this driver */
2253*4882a593Smuzhiyun 	if (chan->device->dev->driver != &pl08x_amba_driver.drv)
2254*4882a593Smuzhiyun 		return false;
2255*4882a593Smuzhiyun 
2256*4882a593Smuzhiyun 	plchan = to_pl08x_chan(chan);
2257*4882a593Smuzhiyun 
2258*4882a593Smuzhiyun 	/* Check that the channel is not taken! */
2259*4882a593Smuzhiyun 	if (!strcmp(plchan->name, name))
2260*4882a593Smuzhiyun 		return true;
2261*4882a593Smuzhiyun 
2262*4882a593Smuzhiyun 	return false;
2263*4882a593Smuzhiyun }
2264*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(pl08x_filter_id);
2265*4882a593Smuzhiyun 
pl08x_filter_fn(struct dma_chan * chan,void * chan_id)2266*4882a593Smuzhiyun static bool pl08x_filter_fn(struct dma_chan *chan, void *chan_id)
2267*4882a593Smuzhiyun {
2268*4882a593Smuzhiyun 	struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
2269*4882a593Smuzhiyun 
2270*4882a593Smuzhiyun 	return plchan->cd == chan_id;
2271*4882a593Smuzhiyun }
2272*4882a593Smuzhiyun 
2273*4882a593Smuzhiyun /*
2274*4882a593Smuzhiyun  * Just check that the device is there and active
2275*4882a593Smuzhiyun  * TODO: turn this bit on/off depending on the number of physical channels
2276*4882a593Smuzhiyun  * actually used, if it is zero... well shut it off. That will save some
2277*4882a593Smuzhiyun  * power. Cut the clock at the same time.
2278*4882a593Smuzhiyun  */
pl08x_ensure_on(struct pl08x_driver_data * pl08x)2279*4882a593Smuzhiyun static void pl08x_ensure_on(struct pl08x_driver_data *pl08x)
2280*4882a593Smuzhiyun {
2281*4882a593Smuzhiyun 	/* The Nomadik variant does not have the config register */
2282*4882a593Smuzhiyun 	if (pl08x->vd->nomadik)
2283*4882a593Smuzhiyun 		return;
2284*4882a593Smuzhiyun 	/* The FTDMAC020 variant does this in another register */
2285*4882a593Smuzhiyun 	if (pl08x->vd->ftdmac020) {
2286*4882a593Smuzhiyun 		writel(PL080_CONFIG_ENABLE, pl08x->base + FTDMAC020_CSR);
2287*4882a593Smuzhiyun 		return;
2288*4882a593Smuzhiyun 	}
2289*4882a593Smuzhiyun 	writel(PL080_CONFIG_ENABLE, pl08x->base + PL080_CONFIG);
2290*4882a593Smuzhiyun }
2291*4882a593Smuzhiyun 
pl08x_irq(int irq,void * dev)2292*4882a593Smuzhiyun static irqreturn_t pl08x_irq(int irq, void *dev)
2293*4882a593Smuzhiyun {
2294*4882a593Smuzhiyun 	struct pl08x_driver_data *pl08x = dev;
2295*4882a593Smuzhiyun 	u32 mask = 0, err, tc, i;
2296*4882a593Smuzhiyun 
2297*4882a593Smuzhiyun 	/* check & clear - ERR & TC interrupts */
2298*4882a593Smuzhiyun 	err = readl(pl08x->base + PL080_ERR_STATUS);
2299*4882a593Smuzhiyun 	if (err) {
2300*4882a593Smuzhiyun 		dev_err(&pl08x->adev->dev, "%s error interrupt, register value 0x%08x\n",
2301*4882a593Smuzhiyun 			__func__, err);
2302*4882a593Smuzhiyun 		writel(err, pl08x->base + PL080_ERR_CLEAR);
2303*4882a593Smuzhiyun 	}
2304*4882a593Smuzhiyun 	tc = readl(pl08x->base + PL080_TC_STATUS);
2305*4882a593Smuzhiyun 	if (tc)
2306*4882a593Smuzhiyun 		writel(tc, pl08x->base + PL080_TC_CLEAR);
2307*4882a593Smuzhiyun 
2308*4882a593Smuzhiyun 	if (!err && !tc)
2309*4882a593Smuzhiyun 		return IRQ_NONE;
2310*4882a593Smuzhiyun 
2311*4882a593Smuzhiyun 	for (i = 0; i < pl08x->vd->channels; i++) {
2312*4882a593Smuzhiyun 		if ((BIT(i) & err) || (BIT(i) & tc)) {
2313*4882a593Smuzhiyun 			/* Locate physical channel */
2314*4882a593Smuzhiyun 			struct pl08x_phy_chan *phychan = &pl08x->phy_chans[i];
2315*4882a593Smuzhiyun 			struct pl08x_dma_chan *plchan = phychan->serving;
2316*4882a593Smuzhiyun 			struct pl08x_txd *tx;
2317*4882a593Smuzhiyun 
2318*4882a593Smuzhiyun 			if (!plchan) {
2319*4882a593Smuzhiyun 				dev_err(&pl08x->adev->dev,
2320*4882a593Smuzhiyun 					"%s Error TC interrupt on unused channel: 0x%08x\n",
2321*4882a593Smuzhiyun 					__func__, i);
2322*4882a593Smuzhiyun 				continue;
2323*4882a593Smuzhiyun 			}
2324*4882a593Smuzhiyun 
2325*4882a593Smuzhiyun 			spin_lock(&plchan->vc.lock);
2326*4882a593Smuzhiyun 			tx = plchan->at;
2327*4882a593Smuzhiyun 			if (tx && tx->cyclic) {
2328*4882a593Smuzhiyun 				vchan_cyclic_callback(&tx->vd);
2329*4882a593Smuzhiyun 			} else if (tx) {
2330*4882a593Smuzhiyun 				plchan->at = NULL;
2331*4882a593Smuzhiyun 				/*
2332*4882a593Smuzhiyun 				 * This descriptor is done, release its mux
2333*4882a593Smuzhiyun 				 * reservation.
2334*4882a593Smuzhiyun 				 */
2335*4882a593Smuzhiyun 				pl08x_release_mux(plchan);
2336*4882a593Smuzhiyun 				tx->done = true;
2337*4882a593Smuzhiyun 				vchan_cookie_complete(&tx->vd);
2338*4882a593Smuzhiyun 
2339*4882a593Smuzhiyun 				/*
2340*4882a593Smuzhiyun 				 * And start the next descriptor (if any),
2341*4882a593Smuzhiyun 				 * otherwise free this channel.
2342*4882a593Smuzhiyun 				 */
2343*4882a593Smuzhiyun 				if (vchan_next_desc(&plchan->vc))
2344*4882a593Smuzhiyun 					pl08x_start_next_txd(plchan);
2345*4882a593Smuzhiyun 				else
2346*4882a593Smuzhiyun 					pl08x_phy_free(plchan);
2347*4882a593Smuzhiyun 			}
2348*4882a593Smuzhiyun 			spin_unlock(&plchan->vc.lock);
2349*4882a593Smuzhiyun 
2350*4882a593Smuzhiyun 			mask |= BIT(i);
2351*4882a593Smuzhiyun 		}
2352*4882a593Smuzhiyun 	}
2353*4882a593Smuzhiyun 
2354*4882a593Smuzhiyun 	return mask ? IRQ_HANDLED : IRQ_NONE;
2355*4882a593Smuzhiyun }
2356*4882a593Smuzhiyun 
pl08x_dma_slave_init(struct pl08x_dma_chan * chan)2357*4882a593Smuzhiyun static void pl08x_dma_slave_init(struct pl08x_dma_chan *chan)
2358*4882a593Smuzhiyun {
2359*4882a593Smuzhiyun 	chan->slave = true;
2360*4882a593Smuzhiyun 	chan->name = chan->cd->bus_id;
2361*4882a593Smuzhiyun 	chan->cfg.src_addr = chan->cd->addr;
2362*4882a593Smuzhiyun 	chan->cfg.dst_addr = chan->cd->addr;
2363*4882a593Smuzhiyun }
2364*4882a593Smuzhiyun 
2365*4882a593Smuzhiyun /*
2366*4882a593Smuzhiyun  * Initialise the DMAC memcpy/slave channels.
2367*4882a593Smuzhiyun  * Make a local wrapper to hold required data
2368*4882a593Smuzhiyun  */
pl08x_dma_init_virtual_channels(struct pl08x_driver_data * pl08x,struct dma_device * dmadev,unsigned int channels,bool slave)2369*4882a593Smuzhiyun static int pl08x_dma_init_virtual_channels(struct pl08x_driver_data *pl08x,
2370*4882a593Smuzhiyun 		struct dma_device *dmadev, unsigned int channels, bool slave)
2371*4882a593Smuzhiyun {
2372*4882a593Smuzhiyun 	struct pl08x_dma_chan *chan;
2373*4882a593Smuzhiyun 	int i;
2374*4882a593Smuzhiyun 
2375*4882a593Smuzhiyun 	INIT_LIST_HEAD(&dmadev->channels);
2376*4882a593Smuzhiyun 
2377*4882a593Smuzhiyun 	/*
2378*4882a593Smuzhiyun 	 * Register as many many memcpy as we have physical channels,
2379*4882a593Smuzhiyun 	 * we won't always be able to use all but the code will have
2380*4882a593Smuzhiyun 	 * to cope with that situation.
2381*4882a593Smuzhiyun 	 */
2382*4882a593Smuzhiyun 	for (i = 0; i < channels; i++) {
2383*4882a593Smuzhiyun 		chan = kzalloc(sizeof(*chan), GFP_KERNEL);
2384*4882a593Smuzhiyun 		if (!chan)
2385*4882a593Smuzhiyun 			return -ENOMEM;
2386*4882a593Smuzhiyun 
2387*4882a593Smuzhiyun 		chan->host = pl08x;
2388*4882a593Smuzhiyun 		chan->state = PL08X_CHAN_IDLE;
2389*4882a593Smuzhiyun 		chan->signal = -1;
2390*4882a593Smuzhiyun 
2391*4882a593Smuzhiyun 		if (slave) {
2392*4882a593Smuzhiyun 			chan->cd = &pl08x->pd->slave_channels[i];
2393*4882a593Smuzhiyun 			/*
2394*4882a593Smuzhiyun 			 * Some implementations have muxed signals, whereas some
2395*4882a593Smuzhiyun 			 * use a mux in front of the signals and need dynamic
2396*4882a593Smuzhiyun 			 * assignment of signals.
2397*4882a593Smuzhiyun 			 */
2398*4882a593Smuzhiyun 			chan->signal = i;
2399*4882a593Smuzhiyun 			pl08x_dma_slave_init(chan);
2400*4882a593Smuzhiyun 		} else {
2401*4882a593Smuzhiyun 			chan->cd = kzalloc(sizeof(*chan->cd), GFP_KERNEL);
2402*4882a593Smuzhiyun 			if (!chan->cd) {
2403*4882a593Smuzhiyun 				kfree(chan);
2404*4882a593Smuzhiyun 				return -ENOMEM;
2405*4882a593Smuzhiyun 			}
2406*4882a593Smuzhiyun 			chan->cd->bus_id = "memcpy";
2407*4882a593Smuzhiyun 			chan->cd->periph_buses = pl08x->pd->mem_buses;
2408*4882a593Smuzhiyun 			chan->name = kasprintf(GFP_KERNEL, "memcpy%d", i);
2409*4882a593Smuzhiyun 			if (!chan->name) {
2410*4882a593Smuzhiyun 				kfree(chan->cd);
2411*4882a593Smuzhiyun 				kfree(chan);
2412*4882a593Smuzhiyun 				return -ENOMEM;
2413*4882a593Smuzhiyun 			}
2414*4882a593Smuzhiyun 		}
2415*4882a593Smuzhiyun 		dev_dbg(&pl08x->adev->dev,
2416*4882a593Smuzhiyun 			 "initialize virtual channel \"%s\"\n",
2417*4882a593Smuzhiyun 			 chan->name);
2418*4882a593Smuzhiyun 
2419*4882a593Smuzhiyun 		chan->vc.desc_free = pl08x_desc_free;
2420*4882a593Smuzhiyun 		vchan_init(&chan->vc, dmadev);
2421*4882a593Smuzhiyun 	}
2422*4882a593Smuzhiyun 	dev_info(&pl08x->adev->dev, "initialized %d virtual %s channels\n",
2423*4882a593Smuzhiyun 		 i, slave ? "slave" : "memcpy");
2424*4882a593Smuzhiyun 	return i;
2425*4882a593Smuzhiyun }
2426*4882a593Smuzhiyun 
pl08x_free_virtual_channels(struct dma_device * dmadev)2427*4882a593Smuzhiyun static void pl08x_free_virtual_channels(struct dma_device *dmadev)
2428*4882a593Smuzhiyun {
2429*4882a593Smuzhiyun 	struct pl08x_dma_chan *chan = NULL;
2430*4882a593Smuzhiyun 	struct pl08x_dma_chan *next;
2431*4882a593Smuzhiyun 
2432*4882a593Smuzhiyun 	list_for_each_entry_safe(chan,
2433*4882a593Smuzhiyun 				 next, &dmadev->channels, vc.chan.device_node) {
2434*4882a593Smuzhiyun 		list_del(&chan->vc.chan.device_node);
2435*4882a593Smuzhiyun 		kfree(chan);
2436*4882a593Smuzhiyun 	}
2437*4882a593Smuzhiyun }
2438*4882a593Smuzhiyun 
2439*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_FS
pl08x_state_str(enum pl08x_dma_chan_state state)2440*4882a593Smuzhiyun static const char *pl08x_state_str(enum pl08x_dma_chan_state state)
2441*4882a593Smuzhiyun {
2442*4882a593Smuzhiyun 	switch (state) {
2443*4882a593Smuzhiyun 	case PL08X_CHAN_IDLE:
2444*4882a593Smuzhiyun 		return "idle";
2445*4882a593Smuzhiyun 	case PL08X_CHAN_RUNNING:
2446*4882a593Smuzhiyun 		return "running";
2447*4882a593Smuzhiyun 	case PL08X_CHAN_PAUSED:
2448*4882a593Smuzhiyun 		return "paused";
2449*4882a593Smuzhiyun 	case PL08X_CHAN_WAITING:
2450*4882a593Smuzhiyun 		return "waiting";
2451*4882a593Smuzhiyun 	default:
2452*4882a593Smuzhiyun 		break;
2453*4882a593Smuzhiyun 	}
2454*4882a593Smuzhiyun 	return "UNKNOWN STATE";
2455*4882a593Smuzhiyun }
2456*4882a593Smuzhiyun 
pl08x_debugfs_show(struct seq_file * s,void * data)2457*4882a593Smuzhiyun static int pl08x_debugfs_show(struct seq_file *s, void *data)
2458*4882a593Smuzhiyun {
2459*4882a593Smuzhiyun 	struct pl08x_driver_data *pl08x = s->private;
2460*4882a593Smuzhiyun 	struct pl08x_dma_chan *chan;
2461*4882a593Smuzhiyun 	struct pl08x_phy_chan *ch;
2462*4882a593Smuzhiyun 	unsigned long flags;
2463*4882a593Smuzhiyun 	int i;
2464*4882a593Smuzhiyun 
2465*4882a593Smuzhiyun 	seq_printf(s, "PL08x physical channels:\n");
2466*4882a593Smuzhiyun 	seq_printf(s, "CHANNEL:\tUSER:\n");
2467*4882a593Smuzhiyun 	seq_printf(s, "--------\t-----\n");
2468*4882a593Smuzhiyun 	for (i = 0; i < pl08x->vd->channels; i++) {
2469*4882a593Smuzhiyun 		struct pl08x_dma_chan *virt_chan;
2470*4882a593Smuzhiyun 
2471*4882a593Smuzhiyun 		ch = &pl08x->phy_chans[i];
2472*4882a593Smuzhiyun 
2473*4882a593Smuzhiyun 		spin_lock_irqsave(&ch->lock, flags);
2474*4882a593Smuzhiyun 		virt_chan = ch->serving;
2475*4882a593Smuzhiyun 
2476*4882a593Smuzhiyun 		seq_printf(s, "%d\t\t%s%s\n",
2477*4882a593Smuzhiyun 			   ch->id,
2478*4882a593Smuzhiyun 			   virt_chan ? virt_chan->name : "(none)",
2479*4882a593Smuzhiyun 			   ch->locked ? " LOCKED" : "");
2480*4882a593Smuzhiyun 
2481*4882a593Smuzhiyun 		spin_unlock_irqrestore(&ch->lock, flags);
2482*4882a593Smuzhiyun 	}
2483*4882a593Smuzhiyun 
2484*4882a593Smuzhiyun 	seq_printf(s, "\nPL08x virtual memcpy channels:\n");
2485*4882a593Smuzhiyun 	seq_printf(s, "CHANNEL:\tSTATE:\n");
2486*4882a593Smuzhiyun 	seq_printf(s, "--------\t------\n");
2487*4882a593Smuzhiyun 	list_for_each_entry(chan, &pl08x->memcpy.channels, vc.chan.device_node) {
2488*4882a593Smuzhiyun 		seq_printf(s, "%s\t\t%s\n", chan->name,
2489*4882a593Smuzhiyun 			   pl08x_state_str(chan->state));
2490*4882a593Smuzhiyun 	}
2491*4882a593Smuzhiyun 
2492*4882a593Smuzhiyun 	if (pl08x->has_slave) {
2493*4882a593Smuzhiyun 		seq_printf(s, "\nPL08x virtual slave channels:\n");
2494*4882a593Smuzhiyun 		seq_printf(s, "CHANNEL:\tSTATE:\n");
2495*4882a593Smuzhiyun 		seq_printf(s, "--------\t------\n");
2496*4882a593Smuzhiyun 		list_for_each_entry(chan, &pl08x->slave.channels,
2497*4882a593Smuzhiyun 				    vc.chan.device_node) {
2498*4882a593Smuzhiyun 			seq_printf(s, "%s\t\t%s\n", chan->name,
2499*4882a593Smuzhiyun 				   pl08x_state_str(chan->state));
2500*4882a593Smuzhiyun 		}
2501*4882a593Smuzhiyun 	}
2502*4882a593Smuzhiyun 
2503*4882a593Smuzhiyun 	return 0;
2504*4882a593Smuzhiyun }
2505*4882a593Smuzhiyun 
2506*4882a593Smuzhiyun DEFINE_SHOW_ATTRIBUTE(pl08x_debugfs);
2507*4882a593Smuzhiyun 
init_pl08x_debugfs(struct pl08x_driver_data * pl08x)2508*4882a593Smuzhiyun static void init_pl08x_debugfs(struct pl08x_driver_data *pl08x)
2509*4882a593Smuzhiyun {
2510*4882a593Smuzhiyun 	/* Expose a simple debugfs interface to view all clocks */
2511*4882a593Smuzhiyun 	debugfs_create_file(dev_name(&pl08x->adev->dev), S_IFREG | S_IRUGO,
2512*4882a593Smuzhiyun 			    NULL, pl08x, &pl08x_debugfs_fops);
2513*4882a593Smuzhiyun }
2514*4882a593Smuzhiyun 
2515*4882a593Smuzhiyun #else
init_pl08x_debugfs(struct pl08x_driver_data * pl08x)2516*4882a593Smuzhiyun static inline void init_pl08x_debugfs(struct pl08x_driver_data *pl08x)
2517*4882a593Smuzhiyun {
2518*4882a593Smuzhiyun }
2519*4882a593Smuzhiyun #endif
2520*4882a593Smuzhiyun 
2521*4882a593Smuzhiyun #ifdef CONFIG_OF
pl08x_find_chan_id(struct pl08x_driver_data * pl08x,u32 id)2522*4882a593Smuzhiyun static struct dma_chan *pl08x_find_chan_id(struct pl08x_driver_data *pl08x,
2523*4882a593Smuzhiyun 					 u32 id)
2524*4882a593Smuzhiyun {
2525*4882a593Smuzhiyun 	struct pl08x_dma_chan *chan;
2526*4882a593Smuzhiyun 
2527*4882a593Smuzhiyun 	/* Trying to get a slave channel from something with no slave support */
2528*4882a593Smuzhiyun 	if (!pl08x->has_slave)
2529*4882a593Smuzhiyun 		return NULL;
2530*4882a593Smuzhiyun 
2531*4882a593Smuzhiyun 	list_for_each_entry(chan, &pl08x->slave.channels, vc.chan.device_node) {
2532*4882a593Smuzhiyun 		if (chan->signal == id)
2533*4882a593Smuzhiyun 			return &chan->vc.chan;
2534*4882a593Smuzhiyun 	}
2535*4882a593Smuzhiyun 
2536*4882a593Smuzhiyun 	return NULL;
2537*4882a593Smuzhiyun }
2538*4882a593Smuzhiyun 
pl08x_of_xlate(struct of_phandle_args * dma_spec,struct of_dma * ofdma)2539*4882a593Smuzhiyun static struct dma_chan *pl08x_of_xlate(struct of_phandle_args *dma_spec,
2540*4882a593Smuzhiyun 				       struct of_dma *ofdma)
2541*4882a593Smuzhiyun {
2542*4882a593Smuzhiyun 	struct pl08x_driver_data *pl08x = ofdma->of_dma_data;
2543*4882a593Smuzhiyun 	struct dma_chan *dma_chan;
2544*4882a593Smuzhiyun 	struct pl08x_dma_chan *plchan;
2545*4882a593Smuzhiyun 
2546*4882a593Smuzhiyun 	if (!pl08x)
2547*4882a593Smuzhiyun 		return NULL;
2548*4882a593Smuzhiyun 
2549*4882a593Smuzhiyun 	if (dma_spec->args_count != 2) {
2550*4882a593Smuzhiyun 		dev_err(&pl08x->adev->dev,
2551*4882a593Smuzhiyun 			"DMA channel translation requires two cells\n");
2552*4882a593Smuzhiyun 		return NULL;
2553*4882a593Smuzhiyun 	}
2554*4882a593Smuzhiyun 
2555*4882a593Smuzhiyun 	dma_chan = pl08x_find_chan_id(pl08x, dma_spec->args[0]);
2556*4882a593Smuzhiyun 	if (!dma_chan) {
2557*4882a593Smuzhiyun 		dev_err(&pl08x->adev->dev,
2558*4882a593Smuzhiyun 			"DMA slave channel not found\n");
2559*4882a593Smuzhiyun 		return NULL;
2560*4882a593Smuzhiyun 	}
2561*4882a593Smuzhiyun 
2562*4882a593Smuzhiyun 	plchan = to_pl08x_chan(dma_chan);
2563*4882a593Smuzhiyun 	dev_dbg(&pl08x->adev->dev,
2564*4882a593Smuzhiyun 		"translated channel for signal %d\n",
2565*4882a593Smuzhiyun 		dma_spec->args[0]);
2566*4882a593Smuzhiyun 
2567*4882a593Smuzhiyun 	/* Augment channel data for applicable AHB buses */
2568*4882a593Smuzhiyun 	plchan->cd->periph_buses = dma_spec->args[1];
2569*4882a593Smuzhiyun 	return dma_get_slave_channel(dma_chan);
2570*4882a593Smuzhiyun }
2571*4882a593Smuzhiyun 
pl08x_of_probe(struct amba_device * adev,struct pl08x_driver_data * pl08x,struct device_node * np)2572*4882a593Smuzhiyun static int pl08x_of_probe(struct amba_device *adev,
2573*4882a593Smuzhiyun 			  struct pl08x_driver_data *pl08x,
2574*4882a593Smuzhiyun 			  struct device_node *np)
2575*4882a593Smuzhiyun {
2576*4882a593Smuzhiyun 	struct pl08x_platform_data *pd;
2577*4882a593Smuzhiyun 	struct pl08x_channel_data *chanp = NULL;
2578*4882a593Smuzhiyun 	u32 val;
2579*4882a593Smuzhiyun 	int ret;
2580*4882a593Smuzhiyun 	int i;
2581*4882a593Smuzhiyun 
2582*4882a593Smuzhiyun 	pd = devm_kzalloc(&adev->dev, sizeof(*pd), GFP_KERNEL);
2583*4882a593Smuzhiyun 	if (!pd)
2584*4882a593Smuzhiyun 		return -ENOMEM;
2585*4882a593Smuzhiyun 
2586*4882a593Smuzhiyun 	/* Eligible bus masters for fetching LLIs */
2587*4882a593Smuzhiyun 	if (of_property_read_bool(np, "lli-bus-interface-ahb1"))
2588*4882a593Smuzhiyun 		pd->lli_buses |= PL08X_AHB1;
2589*4882a593Smuzhiyun 	if (of_property_read_bool(np, "lli-bus-interface-ahb2"))
2590*4882a593Smuzhiyun 		pd->lli_buses |= PL08X_AHB2;
2591*4882a593Smuzhiyun 	if (!pd->lli_buses) {
2592*4882a593Smuzhiyun 		dev_info(&adev->dev, "no bus masters for LLIs stated, assume all\n");
2593*4882a593Smuzhiyun 		pd->lli_buses |= PL08X_AHB1 | PL08X_AHB2;
2594*4882a593Smuzhiyun 	}
2595*4882a593Smuzhiyun 
2596*4882a593Smuzhiyun 	/* Eligible bus masters for memory access */
2597*4882a593Smuzhiyun 	if (of_property_read_bool(np, "mem-bus-interface-ahb1"))
2598*4882a593Smuzhiyun 		pd->mem_buses |= PL08X_AHB1;
2599*4882a593Smuzhiyun 	if (of_property_read_bool(np, "mem-bus-interface-ahb2"))
2600*4882a593Smuzhiyun 		pd->mem_buses |= PL08X_AHB2;
2601*4882a593Smuzhiyun 	if (!pd->mem_buses) {
2602*4882a593Smuzhiyun 		dev_info(&adev->dev, "no bus masters for memory stated, assume all\n");
2603*4882a593Smuzhiyun 		pd->mem_buses |= PL08X_AHB1 | PL08X_AHB2;
2604*4882a593Smuzhiyun 	}
2605*4882a593Smuzhiyun 
2606*4882a593Smuzhiyun 	/* Parse the memcpy channel properties */
2607*4882a593Smuzhiyun 	ret = of_property_read_u32(np, "memcpy-burst-size", &val);
2608*4882a593Smuzhiyun 	if (ret) {
2609*4882a593Smuzhiyun 		dev_info(&adev->dev, "no memcpy burst size specified, using 1 byte\n");
2610*4882a593Smuzhiyun 		val = 1;
2611*4882a593Smuzhiyun 	}
2612*4882a593Smuzhiyun 	switch (val) {
2613*4882a593Smuzhiyun 	default:
2614*4882a593Smuzhiyun 		dev_err(&adev->dev, "illegal burst size for memcpy, set to 1\n");
2615*4882a593Smuzhiyun 		fallthrough;
2616*4882a593Smuzhiyun 	case 1:
2617*4882a593Smuzhiyun 		pd->memcpy_burst_size = PL08X_BURST_SZ_1;
2618*4882a593Smuzhiyun 		break;
2619*4882a593Smuzhiyun 	case 4:
2620*4882a593Smuzhiyun 		pd->memcpy_burst_size = PL08X_BURST_SZ_4;
2621*4882a593Smuzhiyun 		break;
2622*4882a593Smuzhiyun 	case 8:
2623*4882a593Smuzhiyun 		pd->memcpy_burst_size = PL08X_BURST_SZ_8;
2624*4882a593Smuzhiyun 		break;
2625*4882a593Smuzhiyun 	case 16:
2626*4882a593Smuzhiyun 		pd->memcpy_burst_size = PL08X_BURST_SZ_16;
2627*4882a593Smuzhiyun 		break;
2628*4882a593Smuzhiyun 	case 32:
2629*4882a593Smuzhiyun 		pd->memcpy_burst_size = PL08X_BURST_SZ_32;
2630*4882a593Smuzhiyun 		break;
2631*4882a593Smuzhiyun 	case 64:
2632*4882a593Smuzhiyun 		pd->memcpy_burst_size = PL08X_BURST_SZ_64;
2633*4882a593Smuzhiyun 		break;
2634*4882a593Smuzhiyun 	case 128:
2635*4882a593Smuzhiyun 		pd->memcpy_burst_size = PL08X_BURST_SZ_128;
2636*4882a593Smuzhiyun 		break;
2637*4882a593Smuzhiyun 	case 256:
2638*4882a593Smuzhiyun 		pd->memcpy_burst_size = PL08X_BURST_SZ_256;
2639*4882a593Smuzhiyun 		break;
2640*4882a593Smuzhiyun 	}
2641*4882a593Smuzhiyun 
2642*4882a593Smuzhiyun 	ret = of_property_read_u32(np, "memcpy-bus-width", &val);
2643*4882a593Smuzhiyun 	if (ret) {
2644*4882a593Smuzhiyun 		dev_info(&adev->dev, "no memcpy bus width specified, using 8 bits\n");
2645*4882a593Smuzhiyun 		val = 8;
2646*4882a593Smuzhiyun 	}
2647*4882a593Smuzhiyun 	switch (val) {
2648*4882a593Smuzhiyun 	default:
2649*4882a593Smuzhiyun 		dev_err(&adev->dev, "illegal bus width for memcpy, set to 8 bits\n");
2650*4882a593Smuzhiyun 		fallthrough;
2651*4882a593Smuzhiyun 	case 8:
2652*4882a593Smuzhiyun 		pd->memcpy_bus_width = PL08X_BUS_WIDTH_8_BITS;
2653*4882a593Smuzhiyun 		break;
2654*4882a593Smuzhiyun 	case 16:
2655*4882a593Smuzhiyun 		pd->memcpy_bus_width = PL08X_BUS_WIDTH_16_BITS;
2656*4882a593Smuzhiyun 		break;
2657*4882a593Smuzhiyun 	case 32:
2658*4882a593Smuzhiyun 		pd->memcpy_bus_width = PL08X_BUS_WIDTH_32_BITS;
2659*4882a593Smuzhiyun 		break;
2660*4882a593Smuzhiyun 	}
2661*4882a593Smuzhiyun 
2662*4882a593Smuzhiyun 	/*
2663*4882a593Smuzhiyun 	 * Allocate channel data for all possible slave channels (one
2664*4882a593Smuzhiyun 	 * for each possible signal), channels will then be allocated
2665*4882a593Smuzhiyun 	 * for a device and have it's AHB interfaces set up at
2666*4882a593Smuzhiyun 	 * translation time.
2667*4882a593Smuzhiyun 	 */
2668*4882a593Smuzhiyun 	if (pl08x->vd->signals) {
2669*4882a593Smuzhiyun 		chanp = devm_kcalloc(&adev->dev,
2670*4882a593Smuzhiyun 				     pl08x->vd->signals,
2671*4882a593Smuzhiyun 				     sizeof(struct pl08x_channel_data),
2672*4882a593Smuzhiyun 				     GFP_KERNEL);
2673*4882a593Smuzhiyun 		if (!chanp)
2674*4882a593Smuzhiyun 			return -ENOMEM;
2675*4882a593Smuzhiyun 
2676*4882a593Smuzhiyun 		pd->slave_channels = chanp;
2677*4882a593Smuzhiyun 		for (i = 0; i < pl08x->vd->signals; i++) {
2678*4882a593Smuzhiyun 			/*
2679*4882a593Smuzhiyun 			 * chanp->periph_buses will be assigned at translation
2680*4882a593Smuzhiyun 			 */
2681*4882a593Smuzhiyun 			chanp->bus_id = kasprintf(GFP_KERNEL, "slave%d", i);
2682*4882a593Smuzhiyun 			chanp++;
2683*4882a593Smuzhiyun 		}
2684*4882a593Smuzhiyun 		pd->num_slave_channels = pl08x->vd->signals;
2685*4882a593Smuzhiyun 	}
2686*4882a593Smuzhiyun 
2687*4882a593Smuzhiyun 	pl08x->pd = pd;
2688*4882a593Smuzhiyun 
2689*4882a593Smuzhiyun 	return of_dma_controller_register(adev->dev.of_node, pl08x_of_xlate,
2690*4882a593Smuzhiyun 					  pl08x);
2691*4882a593Smuzhiyun }
2692*4882a593Smuzhiyun #else
pl08x_of_probe(struct amba_device * adev,struct pl08x_driver_data * pl08x,struct device_node * np)2693*4882a593Smuzhiyun static inline int pl08x_of_probe(struct amba_device *adev,
2694*4882a593Smuzhiyun 				 struct pl08x_driver_data *pl08x,
2695*4882a593Smuzhiyun 				 struct device_node *np)
2696*4882a593Smuzhiyun {
2697*4882a593Smuzhiyun 	return -EINVAL;
2698*4882a593Smuzhiyun }
2699*4882a593Smuzhiyun #endif
2700*4882a593Smuzhiyun 
pl08x_probe(struct amba_device * adev,const struct amba_id * id)2701*4882a593Smuzhiyun static int pl08x_probe(struct amba_device *adev, const struct amba_id *id)
2702*4882a593Smuzhiyun {
2703*4882a593Smuzhiyun 	struct pl08x_driver_data *pl08x;
2704*4882a593Smuzhiyun 	struct vendor_data *vd = id->data;
2705*4882a593Smuzhiyun 	struct device_node *np = adev->dev.of_node;
2706*4882a593Smuzhiyun 	u32 tsfr_size;
2707*4882a593Smuzhiyun 	int ret = 0;
2708*4882a593Smuzhiyun 	int i;
2709*4882a593Smuzhiyun 
2710*4882a593Smuzhiyun 	ret = amba_request_regions(adev, NULL);
2711*4882a593Smuzhiyun 	if (ret)
2712*4882a593Smuzhiyun 		return ret;
2713*4882a593Smuzhiyun 
2714*4882a593Smuzhiyun 	/* Ensure that we can do DMA */
2715*4882a593Smuzhiyun 	ret = dma_set_mask_and_coherent(&adev->dev, DMA_BIT_MASK(32));
2716*4882a593Smuzhiyun 	if (ret)
2717*4882a593Smuzhiyun 		goto out_no_pl08x;
2718*4882a593Smuzhiyun 
2719*4882a593Smuzhiyun 	/* Create the driver state holder */
2720*4882a593Smuzhiyun 	pl08x = kzalloc(sizeof(*pl08x), GFP_KERNEL);
2721*4882a593Smuzhiyun 	if (!pl08x) {
2722*4882a593Smuzhiyun 		ret = -ENOMEM;
2723*4882a593Smuzhiyun 		goto out_no_pl08x;
2724*4882a593Smuzhiyun 	}
2725*4882a593Smuzhiyun 
2726*4882a593Smuzhiyun 	/* Assign useful pointers to the driver state */
2727*4882a593Smuzhiyun 	pl08x->adev = adev;
2728*4882a593Smuzhiyun 	pl08x->vd = vd;
2729*4882a593Smuzhiyun 
2730*4882a593Smuzhiyun 	pl08x->base = ioremap(adev->res.start, resource_size(&adev->res));
2731*4882a593Smuzhiyun 	if (!pl08x->base) {
2732*4882a593Smuzhiyun 		ret = -ENOMEM;
2733*4882a593Smuzhiyun 		goto out_no_ioremap;
2734*4882a593Smuzhiyun 	}
2735*4882a593Smuzhiyun 
2736*4882a593Smuzhiyun 	if (vd->ftdmac020) {
2737*4882a593Smuzhiyun 		u32 val;
2738*4882a593Smuzhiyun 
2739*4882a593Smuzhiyun 		val = readl(pl08x->base + FTDMAC020_REVISION);
2740*4882a593Smuzhiyun 		dev_info(&pl08x->adev->dev, "FTDMAC020 %d.%d rel %d\n",
2741*4882a593Smuzhiyun 			 (val >> 16) & 0xff, (val >> 8) & 0xff, val & 0xff);
2742*4882a593Smuzhiyun 		val = readl(pl08x->base + FTDMAC020_FEATURE);
2743*4882a593Smuzhiyun 		dev_info(&pl08x->adev->dev, "FTDMAC020 %d channels, "
2744*4882a593Smuzhiyun 			 "%s built-in bridge, %s, %s linked lists\n",
2745*4882a593Smuzhiyun 			 (val >> 12) & 0x0f,
2746*4882a593Smuzhiyun 			 (val & BIT(10)) ? "no" : "has",
2747*4882a593Smuzhiyun 			 (val & BIT(9)) ? "AHB0 and AHB1" : "AHB0",
2748*4882a593Smuzhiyun 			 (val & BIT(8)) ? "supports" : "does not support");
2749*4882a593Smuzhiyun 
2750*4882a593Smuzhiyun 		/* Vendor data from feature register */
2751*4882a593Smuzhiyun 		if (!(val & BIT(8)))
2752*4882a593Smuzhiyun 			dev_warn(&pl08x->adev->dev,
2753*4882a593Smuzhiyun 				 "linked lists not supported, required\n");
2754*4882a593Smuzhiyun 		vd->channels = (val >> 12) & 0x0f;
2755*4882a593Smuzhiyun 		vd->dualmaster = !!(val & BIT(9));
2756*4882a593Smuzhiyun 	}
2757*4882a593Smuzhiyun 
2758*4882a593Smuzhiyun 	/* Initialize memcpy engine */
2759*4882a593Smuzhiyun 	dma_cap_set(DMA_MEMCPY, pl08x->memcpy.cap_mask);
2760*4882a593Smuzhiyun 	pl08x->memcpy.dev = &adev->dev;
2761*4882a593Smuzhiyun 	pl08x->memcpy.device_free_chan_resources = pl08x_free_chan_resources;
2762*4882a593Smuzhiyun 	pl08x->memcpy.device_prep_dma_memcpy = pl08x_prep_dma_memcpy;
2763*4882a593Smuzhiyun 	pl08x->memcpy.device_prep_dma_interrupt = pl08x_prep_dma_interrupt;
2764*4882a593Smuzhiyun 	pl08x->memcpy.device_tx_status = pl08x_dma_tx_status;
2765*4882a593Smuzhiyun 	pl08x->memcpy.device_issue_pending = pl08x_issue_pending;
2766*4882a593Smuzhiyun 	pl08x->memcpy.device_config = pl08x_config;
2767*4882a593Smuzhiyun 	pl08x->memcpy.device_pause = pl08x_pause;
2768*4882a593Smuzhiyun 	pl08x->memcpy.device_resume = pl08x_resume;
2769*4882a593Smuzhiyun 	pl08x->memcpy.device_terminate_all = pl08x_terminate_all;
2770*4882a593Smuzhiyun 	pl08x->memcpy.device_synchronize = pl08x_synchronize;
2771*4882a593Smuzhiyun 	pl08x->memcpy.src_addr_widths = PL80X_DMA_BUSWIDTHS;
2772*4882a593Smuzhiyun 	pl08x->memcpy.dst_addr_widths = PL80X_DMA_BUSWIDTHS;
2773*4882a593Smuzhiyun 	pl08x->memcpy.directions = BIT(DMA_MEM_TO_MEM);
2774*4882a593Smuzhiyun 	pl08x->memcpy.residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
2775*4882a593Smuzhiyun 	if (vd->ftdmac020)
2776*4882a593Smuzhiyun 		pl08x->memcpy.copy_align = DMAENGINE_ALIGN_4_BYTES;
2777*4882a593Smuzhiyun 
2778*4882a593Smuzhiyun 
2779*4882a593Smuzhiyun 	/*
2780*4882a593Smuzhiyun 	 * Initialize slave engine, if the block has no signals, that means
2781*4882a593Smuzhiyun 	 * we have no slave support.
2782*4882a593Smuzhiyun 	 */
2783*4882a593Smuzhiyun 	if (vd->signals) {
2784*4882a593Smuzhiyun 		pl08x->has_slave = true;
2785*4882a593Smuzhiyun 		dma_cap_set(DMA_SLAVE, pl08x->slave.cap_mask);
2786*4882a593Smuzhiyun 		dma_cap_set(DMA_CYCLIC, pl08x->slave.cap_mask);
2787*4882a593Smuzhiyun 		pl08x->slave.dev = &adev->dev;
2788*4882a593Smuzhiyun 		pl08x->slave.device_free_chan_resources =
2789*4882a593Smuzhiyun 			pl08x_free_chan_resources;
2790*4882a593Smuzhiyun 		pl08x->slave.device_prep_dma_interrupt =
2791*4882a593Smuzhiyun 			pl08x_prep_dma_interrupt;
2792*4882a593Smuzhiyun 		pl08x->slave.device_tx_status = pl08x_dma_tx_status;
2793*4882a593Smuzhiyun 		pl08x->slave.device_issue_pending = pl08x_issue_pending;
2794*4882a593Smuzhiyun 		pl08x->slave.device_prep_slave_sg = pl08x_prep_slave_sg;
2795*4882a593Smuzhiyun 		pl08x->slave.device_prep_dma_cyclic = pl08x_prep_dma_cyclic;
2796*4882a593Smuzhiyun 		pl08x->slave.device_config = pl08x_config;
2797*4882a593Smuzhiyun 		pl08x->slave.device_pause = pl08x_pause;
2798*4882a593Smuzhiyun 		pl08x->slave.device_resume = pl08x_resume;
2799*4882a593Smuzhiyun 		pl08x->slave.device_terminate_all = pl08x_terminate_all;
2800*4882a593Smuzhiyun 		pl08x->slave.device_synchronize = pl08x_synchronize;
2801*4882a593Smuzhiyun 		pl08x->slave.src_addr_widths = PL80X_DMA_BUSWIDTHS;
2802*4882a593Smuzhiyun 		pl08x->slave.dst_addr_widths = PL80X_DMA_BUSWIDTHS;
2803*4882a593Smuzhiyun 		pl08x->slave.directions =
2804*4882a593Smuzhiyun 			BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
2805*4882a593Smuzhiyun 		pl08x->slave.residue_granularity =
2806*4882a593Smuzhiyun 			DMA_RESIDUE_GRANULARITY_SEGMENT;
2807*4882a593Smuzhiyun 	}
2808*4882a593Smuzhiyun 
2809*4882a593Smuzhiyun 	/* Get the platform data */
2810*4882a593Smuzhiyun 	pl08x->pd = dev_get_platdata(&adev->dev);
2811*4882a593Smuzhiyun 	if (!pl08x->pd) {
2812*4882a593Smuzhiyun 		if (np) {
2813*4882a593Smuzhiyun 			ret = pl08x_of_probe(adev, pl08x, np);
2814*4882a593Smuzhiyun 			if (ret)
2815*4882a593Smuzhiyun 				goto out_no_platdata;
2816*4882a593Smuzhiyun 		} else {
2817*4882a593Smuzhiyun 			dev_err(&adev->dev, "no platform data supplied\n");
2818*4882a593Smuzhiyun 			ret = -EINVAL;
2819*4882a593Smuzhiyun 			goto out_no_platdata;
2820*4882a593Smuzhiyun 		}
2821*4882a593Smuzhiyun 	} else {
2822*4882a593Smuzhiyun 		pl08x->slave.filter.map = pl08x->pd->slave_map;
2823*4882a593Smuzhiyun 		pl08x->slave.filter.mapcnt = pl08x->pd->slave_map_len;
2824*4882a593Smuzhiyun 		pl08x->slave.filter.fn = pl08x_filter_fn;
2825*4882a593Smuzhiyun 	}
2826*4882a593Smuzhiyun 
2827*4882a593Smuzhiyun 	/* By default, AHB1 only.  If dualmaster, from platform */
2828*4882a593Smuzhiyun 	pl08x->lli_buses = PL08X_AHB1;
2829*4882a593Smuzhiyun 	pl08x->mem_buses = PL08X_AHB1;
2830*4882a593Smuzhiyun 	if (pl08x->vd->dualmaster) {
2831*4882a593Smuzhiyun 		pl08x->lli_buses = pl08x->pd->lli_buses;
2832*4882a593Smuzhiyun 		pl08x->mem_buses = pl08x->pd->mem_buses;
2833*4882a593Smuzhiyun 	}
2834*4882a593Smuzhiyun 
2835*4882a593Smuzhiyun 	if (vd->pl080s)
2836*4882a593Smuzhiyun 		pl08x->lli_words = PL080S_LLI_WORDS;
2837*4882a593Smuzhiyun 	else
2838*4882a593Smuzhiyun 		pl08x->lli_words = PL080_LLI_WORDS;
2839*4882a593Smuzhiyun 	tsfr_size = MAX_NUM_TSFR_LLIS * pl08x->lli_words * sizeof(u32);
2840*4882a593Smuzhiyun 
2841*4882a593Smuzhiyun 	/* A DMA memory pool for LLIs, align on 1-byte boundary */
2842*4882a593Smuzhiyun 	pl08x->pool = dma_pool_create(DRIVER_NAME, &pl08x->adev->dev,
2843*4882a593Smuzhiyun 						tsfr_size, PL08X_ALIGN, 0);
2844*4882a593Smuzhiyun 	if (!pl08x->pool) {
2845*4882a593Smuzhiyun 		ret = -ENOMEM;
2846*4882a593Smuzhiyun 		goto out_no_lli_pool;
2847*4882a593Smuzhiyun 	}
2848*4882a593Smuzhiyun 
2849*4882a593Smuzhiyun 	/* Turn on the PL08x */
2850*4882a593Smuzhiyun 	pl08x_ensure_on(pl08x);
2851*4882a593Smuzhiyun 
2852*4882a593Smuzhiyun 	/* Clear any pending interrupts */
2853*4882a593Smuzhiyun 	if (vd->ftdmac020)
2854*4882a593Smuzhiyun 		/* This variant has error IRQs in bits 16-19 */
2855*4882a593Smuzhiyun 		writel(0x0000FFFF, pl08x->base + PL080_ERR_CLEAR);
2856*4882a593Smuzhiyun 	else
2857*4882a593Smuzhiyun 		writel(0x000000FF, pl08x->base + PL080_ERR_CLEAR);
2858*4882a593Smuzhiyun 	writel(0x000000FF, pl08x->base + PL080_TC_CLEAR);
2859*4882a593Smuzhiyun 
2860*4882a593Smuzhiyun 	/* Attach the interrupt handler */
2861*4882a593Smuzhiyun 	ret = request_irq(adev->irq[0], pl08x_irq, 0, DRIVER_NAME, pl08x);
2862*4882a593Smuzhiyun 	if (ret) {
2863*4882a593Smuzhiyun 		dev_err(&adev->dev, "%s failed to request interrupt %d\n",
2864*4882a593Smuzhiyun 			__func__, adev->irq[0]);
2865*4882a593Smuzhiyun 		goto out_no_irq;
2866*4882a593Smuzhiyun 	}
2867*4882a593Smuzhiyun 
2868*4882a593Smuzhiyun 	/* Initialize physical channels */
2869*4882a593Smuzhiyun 	pl08x->phy_chans = kzalloc((vd->channels * sizeof(*pl08x->phy_chans)),
2870*4882a593Smuzhiyun 			GFP_KERNEL);
2871*4882a593Smuzhiyun 	if (!pl08x->phy_chans) {
2872*4882a593Smuzhiyun 		ret = -ENOMEM;
2873*4882a593Smuzhiyun 		goto out_no_phychans;
2874*4882a593Smuzhiyun 	}
2875*4882a593Smuzhiyun 
2876*4882a593Smuzhiyun 	for (i = 0; i < vd->channels; i++) {
2877*4882a593Smuzhiyun 		struct pl08x_phy_chan *ch = &pl08x->phy_chans[i];
2878*4882a593Smuzhiyun 
2879*4882a593Smuzhiyun 		ch->id = i;
2880*4882a593Smuzhiyun 		ch->base = pl08x->base + PL080_Cx_BASE(i);
2881*4882a593Smuzhiyun 		if (vd->ftdmac020) {
2882*4882a593Smuzhiyun 			/* FTDMA020 has a special channel busy register */
2883*4882a593Smuzhiyun 			ch->reg_busy = ch->base + FTDMAC020_CH_BUSY;
2884*4882a593Smuzhiyun 			ch->reg_config = ch->base + FTDMAC020_CH_CFG;
2885*4882a593Smuzhiyun 			ch->reg_control = ch->base + FTDMAC020_CH_CSR;
2886*4882a593Smuzhiyun 			ch->reg_src = ch->base + FTDMAC020_CH_SRC_ADDR;
2887*4882a593Smuzhiyun 			ch->reg_dst = ch->base + FTDMAC020_CH_DST_ADDR;
2888*4882a593Smuzhiyun 			ch->reg_lli = ch->base + FTDMAC020_CH_LLP;
2889*4882a593Smuzhiyun 			ch->ftdmac020 = true;
2890*4882a593Smuzhiyun 		} else {
2891*4882a593Smuzhiyun 			ch->reg_config = ch->base + vd->config_offset;
2892*4882a593Smuzhiyun 			ch->reg_control = ch->base + PL080_CH_CONTROL;
2893*4882a593Smuzhiyun 			ch->reg_src = ch->base + PL080_CH_SRC_ADDR;
2894*4882a593Smuzhiyun 			ch->reg_dst = ch->base + PL080_CH_DST_ADDR;
2895*4882a593Smuzhiyun 			ch->reg_lli = ch->base + PL080_CH_LLI;
2896*4882a593Smuzhiyun 		}
2897*4882a593Smuzhiyun 		if (vd->pl080s)
2898*4882a593Smuzhiyun 			ch->pl080s = true;
2899*4882a593Smuzhiyun 
2900*4882a593Smuzhiyun 		spin_lock_init(&ch->lock);
2901*4882a593Smuzhiyun 
2902*4882a593Smuzhiyun 		/*
2903*4882a593Smuzhiyun 		 * Nomadik variants can have channels that are locked
2904*4882a593Smuzhiyun 		 * down for the secure world only. Lock up these channels
2905*4882a593Smuzhiyun 		 * by perpetually serving a dummy virtual channel.
2906*4882a593Smuzhiyun 		 */
2907*4882a593Smuzhiyun 		if (vd->nomadik) {
2908*4882a593Smuzhiyun 			u32 val;
2909*4882a593Smuzhiyun 
2910*4882a593Smuzhiyun 			val = readl(ch->reg_config);
2911*4882a593Smuzhiyun 			if (val & (PL080N_CONFIG_ITPROT | PL080N_CONFIG_SECPROT)) {
2912*4882a593Smuzhiyun 				dev_info(&adev->dev, "physical channel %d reserved for secure access only\n", i);
2913*4882a593Smuzhiyun 				ch->locked = true;
2914*4882a593Smuzhiyun 			}
2915*4882a593Smuzhiyun 		}
2916*4882a593Smuzhiyun 
2917*4882a593Smuzhiyun 		dev_dbg(&adev->dev, "physical channel %d is %s\n",
2918*4882a593Smuzhiyun 			i, pl08x_phy_channel_busy(ch) ? "BUSY" : "FREE");
2919*4882a593Smuzhiyun 	}
2920*4882a593Smuzhiyun 
2921*4882a593Smuzhiyun 	/* Register as many memcpy channels as there are physical channels */
2922*4882a593Smuzhiyun 	ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->memcpy,
2923*4882a593Smuzhiyun 					      pl08x->vd->channels, false);
2924*4882a593Smuzhiyun 	if (ret <= 0) {
2925*4882a593Smuzhiyun 		dev_warn(&pl08x->adev->dev,
2926*4882a593Smuzhiyun 			 "%s failed to enumerate memcpy channels - %d\n",
2927*4882a593Smuzhiyun 			 __func__, ret);
2928*4882a593Smuzhiyun 		goto out_no_memcpy;
2929*4882a593Smuzhiyun 	}
2930*4882a593Smuzhiyun 
2931*4882a593Smuzhiyun 	/* Register slave channels */
2932*4882a593Smuzhiyun 	if (pl08x->has_slave) {
2933*4882a593Smuzhiyun 		ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->slave,
2934*4882a593Smuzhiyun 					pl08x->pd->num_slave_channels, true);
2935*4882a593Smuzhiyun 		if (ret < 0) {
2936*4882a593Smuzhiyun 			dev_warn(&pl08x->adev->dev,
2937*4882a593Smuzhiyun 				 "%s failed to enumerate slave channels - %d\n",
2938*4882a593Smuzhiyun 				 __func__, ret);
2939*4882a593Smuzhiyun 			goto out_no_slave;
2940*4882a593Smuzhiyun 		}
2941*4882a593Smuzhiyun 	}
2942*4882a593Smuzhiyun 
2943*4882a593Smuzhiyun 	ret = dma_async_device_register(&pl08x->memcpy);
2944*4882a593Smuzhiyun 	if (ret) {
2945*4882a593Smuzhiyun 		dev_warn(&pl08x->adev->dev,
2946*4882a593Smuzhiyun 			"%s failed to register memcpy as an async device - %d\n",
2947*4882a593Smuzhiyun 			__func__, ret);
2948*4882a593Smuzhiyun 		goto out_no_memcpy_reg;
2949*4882a593Smuzhiyun 	}
2950*4882a593Smuzhiyun 
2951*4882a593Smuzhiyun 	if (pl08x->has_slave) {
2952*4882a593Smuzhiyun 		ret = dma_async_device_register(&pl08x->slave);
2953*4882a593Smuzhiyun 		if (ret) {
2954*4882a593Smuzhiyun 			dev_warn(&pl08x->adev->dev,
2955*4882a593Smuzhiyun 			"%s failed to register slave as an async device - %d\n",
2956*4882a593Smuzhiyun 			__func__, ret);
2957*4882a593Smuzhiyun 			goto out_no_slave_reg;
2958*4882a593Smuzhiyun 		}
2959*4882a593Smuzhiyun 	}
2960*4882a593Smuzhiyun 
2961*4882a593Smuzhiyun 	amba_set_drvdata(adev, pl08x);
2962*4882a593Smuzhiyun 	init_pl08x_debugfs(pl08x);
2963*4882a593Smuzhiyun 	dev_info(&pl08x->adev->dev, "DMA: PL%03x%s rev%u at 0x%08llx irq %d\n",
2964*4882a593Smuzhiyun 		 amba_part(adev), pl08x->vd->pl080s ? "s" : "", amba_rev(adev),
2965*4882a593Smuzhiyun 		 (unsigned long long)adev->res.start, adev->irq[0]);
2966*4882a593Smuzhiyun 
2967*4882a593Smuzhiyun 	return 0;
2968*4882a593Smuzhiyun 
2969*4882a593Smuzhiyun out_no_slave_reg:
2970*4882a593Smuzhiyun 	dma_async_device_unregister(&pl08x->memcpy);
2971*4882a593Smuzhiyun out_no_memcpy_reg:
2972*4882a593Smuzhiyun 	if (pl08x->has_slave)
2973*4882a593Smuzhiyun 		pl08x_free_virtual_channels(&pl08x->slave);
2974*4882a593Smuzhiyun out_no_slave:
2975*4882a593Smuzhiyun 	pl08x_free_virtual_channels(&pl08x->memcpy);
2976*4882a593Smuzhiyun out_no_memcpy:
2977*4882a593Smuzhiyun 	kfree(pl08x->phy_chans);
2978*4882a593Smuzhiyun out_no_phychans:
2979*4882a593Smuzhiyun 	free_irq(adev->irq[0], pl08x);
2980*4882a593Smuzhiyun out_no_irq:
2981*4882a593Smuzhiyun 	dma_pool_destroy(pl08x->pool);
2982*4882a593Smuzhiyun out_no_lli_pool:
2983*4882a593Smuzhiyun out_no_platdata:
2984*4882a593Smuzhiyun 	iounmap(pl08x->base);
2985*4882a593Smuzhiyun out_no_ioremap:
2986*4882a593Smuzhiyun 	kfree(pl08x);
2987*4882a593Smuzhiyun out_no_pl08x:
2988*4882a593Smuzhiyun 	amba_release_regions(adev);
2989*4882a593Smuzhiyun 	return ret;
2990*4882a593Smuzhiyun }
2991*4882a593Smuzhiyun 
2992*4882a593Smuzhiyun /* PL080 has 8 channels and the PL080 have just 2 */
2993*4882a593Smuzhiyun static struct vendor_data vendor_pl080 = {
2994*4882a593Smuzhiyun 	.config_offset = PL080_CH_CONFIG,
2995*4882a593Smuzhiyun 	.channels = 8,
2996*4882a593Smuzhiyun 	.signals = 16,
2997*4882a593Smuzhiyun 	.dualmaster = true,
2998*4882a593Smuzhiyun 	.max_transfer_size = PL080_CONTROL_TRANSFER_SIZE_MASK,
2999*4882a593Smuzhiyun };
3000*4882a593Smuzhiyun 
3001*4882a593Smuzhiyun static struct vendor_data vendor_nomadik = {
3002*4882a593Smuzhiyun 	.config_offset = PL080_CH_CONFIG,
3003*4882a593Smuzhiyun 	.channels = 8,
3004*4882a593Smuzhiyun 	.signals = 32,
3005*4882a593Smuzhiyun 	.dualmaster = true,
3006*4882a593Smuzhiyun 	.nomadik = true,
3007*4882a593Smuzhiyun 	.max_transfer_size = PL080_CONTROL_TRANSFER_SIZE_MASK,
3008*4882a593Smuzhiyun };
3009*4882a593Smuzhiyun 
3010*4882a593Smuzhiyun static struct vendor_data vendor_pl080s = {
3011*4882a593Smuzhiyun 	.config_offset = PL080S_CH_CONFIG,
3012*4882a593Smuzhiyun 	.channels = 8,
3013*4882a593Smuzhiyun 	.signals = 32,
3014*4882a593Smuzhiyun 	.pl080s = true,
3015*4882a593Smuzhiyun 	.max_transfer_size = PL080S_CONTROL_TRANSFER_SIZE_MASK,
3016*4882a593Smuzhiyun };
3017*4882a593Smuzhiyun 
3018*4882a593Smuzhiyun static struct vendor_data vendor_pl081 = {
3019*4882a593Smuzhiyun 	.config_offset = PL080_CH_CONFIG,
3020*4882a593Smuzhiyun 	.channels = 2,
3021*4882a593Smuzhiyun 	.signals = 16,
3022*4882a593Smuzhiyun 	.dualmaster = false,
3023*4882a593Smuzhiyun 	.max_transfer_size = PL080_CONTROL_TRANSFER_SIZE_MASK,
3024*4882a593Smuzhiyun };
3025*4882a593Smuzhiyun 
3026*4882a593Smuzhiyun static struct vendor_data vendor_ftdmac020 = {
3027*4882a593Smuzhiyun 	.config_offset = PL080_CH_CONFIG,
3028*4882a593Smuzhiyun 	.ftdmac020 = true,
3029*4882a593Smuzhiyun 	.max_transfer_size = PL080_CONTROL_TRANSFER_SIZE_MASK,
3030*4882a593Smuzhiyun };
3031*4882a593Smuzhiyun 
3032*4882a593Smuzhiyun static const struct amba_id pl08x_ids[] = {
3033*4882a593Smuzhiyun 	/* Samsung PL080S variant */
3034*4882a593Smuzhiyun 	{
3035*4882a593Smuzhiyun 		.id	= 0x0a141080,
3036*4882a593Smuzhiyun 		.mask	= 0xffffffff,
3037*4882a593Smuzhiyun 		.data	= &vendor_pl080s,
3038*4882a593Smuzhiyun 	},
3039*4882a593Smuzhiyun 	/* PL080 */
3040*4882a593Smuzhiyun 	{
3041*4882a593Smuzhiyun 		.id	= 0x00041080,
3042*4882a593Smuzhiyun 		.mask	= 0x000fffff,
3043*4882a593Smuzhiyun 		.data	= &vendor_pl080,
3044*4882a593Smuzhiyun 	},
3045*4882a593Smuzhiyun 	/* PL081 */
3046*4882a593Smuzhiyun 	{
3047*4882a593Smuzhiyun 		.id	= 0x00041081,
3048*4882a593Smuzhiyun 		.mask	= 0x000fffff,
3049*4882a593Smuzhiyun 		.data	= &vendor_pl081,
3050*4882a593Smuzhiyun 	},
3051*4882a593Smuzhiyun 	/* Nomadik 8815 PL080 variant */
3052*4882a593Smuzhiyun 	{
3053*4882a593Smuzhiyun 		.id	= 0x00280080,
3054*4882a593Smuzhiyun 		.mask	= 0x00ffffff,
3055*4882a593Smuzhiyun 		.data	= &vendor_nomadik,
3056*4882a593Smuzhiyun 	},
3057*4882a593Smuzhiyun 	/* Faraday Technology FTDMAC020 */
3058*4882a593Smuzhiyun 	{
3059*4882a593Smuzhiyun 		.id	= 0x0003b080,
3060*4882a593Smuzhiyun 		.mask	= 0x000fffff,
3061*4882a593Smuzhiyun 		.data	= &vendor_ftdmac020,
3062*4882a593Smuzhiyun 	},
3063*4882a593Smuzhiyun 	{ 0, 0 },
3064*4882a593Smuzhiyun };
3065*4882a593Smuzhiyun 
3066*4882a593Smuzhiyun MODULE_DEVICE_TABLE(amba, pl08x_ids);
3067*4882a593Smuzhiyun 
3068*4882a593Smuzhiyun static struct amba_driver pl08x_amba_driver = {
3069*4882a593Smuzhiyun 	.drv.name	= DRIVER_NAME,
3070*4882a593Smuzhiyun 	.id_table	= pl08x_ids,
3071*4882a593Smuzhiyun 	.probe		= pl08x_probe,
3072*4882a593Smuzhiyun };
3073*4882a593Smuzhiyun 
pl08x_init(void)3074*4882a593Smuzhiyun static int __init pl08x_init(void)
3075*4882a593Smuzhiyun {
3076*4882a593Smuzhiyun 	int retval;
3077*4882a593Smuzhiyun 	retval = amba_driver_register(&pl08x_amba_driver);
3078*4882a593Smuzhiyun 	if (retval)
3079*4882a593Smuzhiyun 		printk(KERN_WARNING DRIVER_NAME
3080*4882a593Smuzhiyun 		       "failed to register as an AMBA device (%d)\n",
3081*4882a593Smuzhiyun 		       retval);
3082*4882a593Smuzhiyun 	return retval;
3083*4882a593Smuzhiyun }
3084*4882a593Smuzhiyun subsys_initcall(pl08x_init);
3085