xref: /openbmc/linux/drivers/dma/amba-pl08x.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
14ffda636SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2e8689e63SLinus Walleij /*
3e8689e63SLinus Walleij  * Copyright (c) 2006 ARM Ltd.
4e8689e63SLinus Walleij  * Copyright (c) 2010 ST-Ericsson SA
51e1cfc72SLinus Walleij  * Copyirght (c) 2017 Linaro Ltd.
6e8689e63SLinus Walleij  *
7e8689e63SLinus Walleij  * Author: Peter Pearse <peter.pearse@arm.com>
81e1cfc72SLinus Walleij  * Author: Linus Walleij <linus.walleij@linaro.org>
9e8689e63SLinus Walleij  *
10e8689e63SLinus Walleij  * Documentation: ARM DDI 0196G == PL080
11e8689e63SLinus Walleij  * Documentation: ARM DDI 0218E == PL081
12da1b6c05STomasz Figa  * Documentation: S3C6410 User's Manual == PL080S
13e8689e63SLinus Walleij  *
1494ae8522SRussell King - ARM Linux  * PL080 & PL081 both have 16 sets of DMA signals that can be routed to any
1594ae8522SRussell King - ARM Linux  * channel.
16e8689e63SLinus Walleij  *
17e8689e63SLinus Walleij  * The PL080 has 8 channels available for simultaneous use, and the PL081
18e8689e63SLinus Walleij  * has only two channels. So on these DMA controllers the number of channels
19e8689e63SLinus Walleij  * and the number of incoming DMA signals are two totally different things.
20e8689e63SLinus Walleij  * It is usually not possible to theoretically handle all physical signals,
21e8689e63SLinus Walleij  * so a multiplexing scheme with possible denial of use is necessary.
22e8689e63SLinus Walleij  *
23e8689e63SLinus Walleij  * The PL080 has a dual bus master, PL081 has a single master.
24e8689e63SLinus Walleij  *
25da1b6c05STomasz Figa  * PL080S is a version modified by Samsung and used in S3C64xx SoCs.
26da1b6c05STomasz Figa  * It differs in following aspects:
27da1b6c05STomasz Figa  * - CH_CONFIG register at different offset,
28da1b6c05STomasz Figa  * - separate CH_CONTROL2 register for transfer size,
29da1b6c05STomasz Figa  * - bigger maximum transfer size,
30da1b6c05STomasz Figa  * - 8-word aligned LLI, instead of 4-word, due to extra CCTL2 word,
31da1b6c05STomasz Figa  * - no support for peripheral flow control.
32da1b6c05STomasz Figa  *
33e8689e63SLinus Walleij  * Memory to peripheral transfer may be visualized as
34e8689e63SLinus Walleij  *	Get data from memory to DMAC
35e8689e63SLinus Walleij  *	Until no data left
36e8689e63SLinus Walleij  *		On burst request from peripheral
37e8689e63SLinus Walleij  *			Destination burst from DMAC to peripheral
38e8689e63SLinus Walleij  *			Clear burst request
39e8689e63SLinus Walleij  *	Raise terminal count interrupt
40e8689e63SLinus Walleij  *
41e8689e63SLinus Walleij  * For peripherals with a FIFO:
42e8689e63SLinus Walleij  * Source      burst size == half the depth of the peripheral FIFO
43e8689e63SLinus Walleij  * Destination burst size == the depth of the peripheral FIFO
44e8689e63SLinus Walleij  *
45e8689e63SLinus Walleij  * (Bursts are irrelevant for mem to mem transfers - there are no burst
46e8689e63SLinus Walleij  * signals, the DMA controller will simply facilitate its AHB master.)
47e8689e63SLinus Walleij  *
48e8689e63SLinus Walleij  * ASSUMES default (little) endianness for DMA transfers
49e8689e63SLinus Walleij  *
509dc2c200SRussell King - ARM Linux  * The PL08x has two flow control settings:
519dc2c200SRussell King - ARM Linux  *  - DMAC flow control: the transfer size defines the number of transfers
529dc2c200SRussell King - ARM Linux  *    which occur for the current LLI entry, and the DMAC raises TC at the
539dc2c200SRussell King - ARM Linux  *    end of every LLI entry.  Observed behaviour shows the DMAC listening
549dc2c200SRussell King - ARM Linux  *    to both the BREQ and SREQ signals (contrary to documented),
559dc2c200SRussell King - ARM Linux  *    transferring data if either is active.  The LBREQ and LSREQ signals
569dc2c200SRussell King - ARM Linux  *    are ignored.
579dc2c200SRussell King - ARM Linux  *
589dc2c200SRussell King - ARM Linux  *  - Peripheral flow control: the transfer size is ignored (and should be
599dc2c200SRussell King - ARM Linux  *    zero).  The data is transferred from the current LLI entry, until
609dc2c200SRussell King - ARM Linux  *    after the final transfer signalled by LBREQ or LSREQ.  The DMAC
61da1b6c05STomasz Figa  *    will then move to the next LLI entry. Unsupported by PL080S.
62e8689e63SLinus Walleij  */
63730404acSRussell King - ARM Linux #include <linux/amba/bus.h>
64e8689e63SLinus Walleij #include <linux/amba/pl08x.h>
65e8689e63SLinus Walleij #include <linux/debugfs.h>
660c38d701SViresh Kumar #include <linux/delay.h>
670c38d701SViresh Kumar #include <linux/device.h>
680c38d701SViresh Kumar #include <linux/dmaengine.h>
690c38d701SViresh Kumar #include <linux/dmapool.h>
708516f52fSVinod Koul #include <linux/dma-mapping.h>
716d05c9faSSachin Kamat #include <linux/export.h>
720c38d701SViresh Kumar #include <linux/init.h>
730c38d701SViresh Kumar #include <linux/interrupt.h>
740c38d701SViresh Kumar #include <linux/module.h>
75aa4734daSLinus Walleij #include <linux/of.h>
76aa4734daSLinus Walleij #include <linux/of_dma.h>
77b7b6018bSViresh Kumar #include <linux/pm_runtime.h>
78e8689e63SLinus Walleij #include <linux/seq_file.h>
790c38d701SViresh Kumar #include <linux/slab.h>
803a95b9fbSAlessandro Rubini #include <linux/amba/pl080.h>
81e8689e63SLinus Walleij 
82d2ebfb33SRussell King - ARM Linux #include "dmaengine.h"
8301d8dc64SRussell King #include "virt-dma.h"
84d2ebfb33SRussell King - ARM Linux 
85e8689e63SLinus Walleij #define DRIVER_NAME	"pl08xdmac"
86e8689e63SLinus Walleij 
87ea524c7eSMark Brown #define PL80X_DMA_BUSWIDTHS \
88ea524c7eSMark Brown 	BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
89ea524c7eSMark Brown 	BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
90ea524c7eSMark Brown 	BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
91ea524c7eSMark Brown 	BIT(DMA_SLAVE_BUSWIDTH_4_BYTES)
92ea524c7eSMark Brown 
937703eac9SRussell King - ARM Linux static struct amba_driver pl08x_amba_driver;
94b23f204cSRussell King struct pl08x_driver_data;
957703eac9SRussell King - ARM Linux 
96e8689e63SLinus Walleij /**
9794ae8522SRussell King - ARM Linux  * struct vendor_data - vendor-specific config parameters for PL08x derivatives
98da7cbd20SLinus Walleij  * @config_offset: offset to the configuration register
99e8689e63SLinus Walleij  * @channels: the number of channels available in this variant
100f9cd4761SLinus Walleij  * @signals: the number of request signals available from the hardware
10194ae8522SRussell King - ARM Linux  * @dualmaster: whether this version supports dual AHB masters or not.
1021e1cfc72SLinus Walleij  * @nomadik: whether this variant is a ST Microelectronics Nomadik, where the
1031e1cfc72SLinus Walleij  *	channels have Nomadik security extension bits that need to be checked
1041e1cfc72SLinus Walleij  *	for permission before use and some registers are missing
1051e1cfc72SLinus Walleij  * @pl080s: whether this variant is a Samsung PL080S, which has separate
1061e1cfc72SLinus Walleij  *	register and LLI word for transfer size.
1071e1cfc72SLinus Walleij  * @ftdmac020: whether this variant is a Faraday Technology FTDMAC020
108f9cd4761SLinus Walleij  * @max_transfer_size: the maximum single element transfer size for this
109f9cd4761SLinus Walleij  *	PL08x variant.
110e8689e63SLinus Walleij  */
111e8689e63SLinus Walleij struct vendor_data {
112d86ccea7STomasz Figa 	u8 config_offset;
113e8689e63SLinus Walleij 	u8 channels;
114f9cd4761SLinus Walleij 	u8 signals;
115e8689e63SLinus Walleij 	bool dualmaster;
116affa115eSLinus Walleij 	bool nomadik;
117da1b6c05STomasz Figa 	bool pl080s;
1181e1cfc72SLinus Walleij 	bool ftdmac020;
1195110e51dSTomasz Figa 	u32 max_transfer_size;
120e8689e63SLinus Walleij };
121e8689e63SLinus Walleij 
122e8689e63SLinus Walleij /**
123b23f204cSRussell King  * struct pl08x_bus_data - information of source or destination
124b23f204cSRussell King  * busses for a transfer
125b23f204cSRussell King  * @addr: current address
126b23f204cSRussell King  * @maxwidth: the maximum width of a transfer on this bus
127b23f204cSRussell King  * @buswidth: the width of this bus in bytes: 1, 2 or 4
128b23f204cSRussell King  */
129b23f204cSRussell King struct pl08x_bus_data {
130b23f204cSRussell King 	dma_addr_t addr;
131b23f204cSRussell King 	u8 maxwidth;
132b23f204cSRussell King 	u8 buswidth;
133b23f204cSRussell King };
134b23f204cSRussell King 
1351c38b289SAndre Przywara #define IS_BUS_ALIGNED(bus) IS_ALIGNED((bus)->addr, (bus)->buswidth)
1361c38b289SAndre Przywara 
137b23f204cSRussell King /**
138b23f204cSRussell King  * struct pl08x_phy_chan - holder for the physical channels
139b23f204cSRussell King  * @id: physical index to this channel
140da7cbd20SLinus Walleij  * @base: memory base address for this physical channel
141da7cbd20SLinus Walleij  * @reg_config: configuration address for this physical channel
1421e1cfc72SLinus Walleij  * @reg_control: control address for this physical channel
1431e1cfc72SLinus Walleij  * @reg_src: transfer source address register
1441e1cfc72SLinus Walleij  * @reg_dst: transfer destination address register
1451e1cfc72SLinus Walleij  * @reg_lli: transfer LLI address register
1461e1cfc72SLinus Walleij  * @reg_busy: if the variant has a special per-channel busy register,
1471e1cfc72SLinus Walleij  * this contains a pointer to it
148b23f204cSRussell King  * @lock: a lock to use when altering an instance of this struct
149b23f204cSRussell King  * @serving: the virtual channel currently being served by this physical
150b23f204cSRussell King  * channel
151ad0de2acSRussell King  * @locked: channel unavailable for the system, e.g. dedicated to secure
152ad0de2acSRussell King  * world
1531e1cfc72SLinus Walleij  * @ftdmac020: channel is on a FTDMAC020
1541e1cfc72SLinus Walleij  * @pl080s: channel is on a PL08s
155b23f204cSRussell King  */
156b23f204cSRussell King struct pl08x_phy_chan {
157b23f204cSRussell King 	unsigned int id;
158b23f204cSRussell King 	void __iomem *base;
159d86ccea7STomasz Figa 	void __iomem *reg_config;
1601e1cfc72SLinus Walleij 	void __iomem *reg_control;
1611e1cfc72SLinus Walleij 	void __iomem *reg_src;
1621e1cfc72SLinus Walleij 	void __iomem *reg_dst;
1631e1cfc72SLinus Walleij 	void __iomem *reg_lli;
1641e1cfc72SLinus Walleij 	void __iomem *reg_busy;
165b23f204cSRussell King 	spinlock_t lock;
166b23f204cSRussell King 	struct pl08x_dma_chan *serving;
167ad0de2acSRussell King 	bool locked;
1681e1cfc72SLinus Walleij 	bool ftdmac020;
1691e1cfc72SLinus Walleij 	bool pl080s;
170b23f204cSRussell King };
171b23f204cSRussell King 
172b23f204cSRussell King /**
173b23f204cSRussell King  * struct pl08x_sg - structure containing data per sg
174b23f204cSRussell King  * @src_addr: src address of sg
175b23f204cSRussell King  * @dst_addr: dst address of sg
176b23f204cSRussell King  * @len: transfer len in bytes
177b23f204cSRussell King  * @node: node for txd's dsg_list
178b23f204cSRussell King  */
179b23f204cSRussell King struct pl08x_sg {
180b23f204cSRussell King 	dma_addr_t src_addr;
181b23f204cSRussell King 	dma_addr_t dst_addr;
182b23f204cSRussell King 	size_t len;
183b23f204cSRussell King 	struct list_head node;
184b23f204cSRussell King };
185b23f204cSRussell King 
186b23f204cSRussell King /**
187b23f204cSRussell King  * struct pl08x_txd - wrapper for struct dma_async_tx_descriptor
18801d8dc64SRussell King  * @vd: virtual DMA descriptor
189b23f204cSRussell King  * @dsg_list: list of children sg's
190b23f204cSRussell King  * @llis_bus: DMA memory address (physical) start for the LLIs
191b23f204cSRussell King  * @llis_va: virtual memory address start for the LLIs
192b23f204cSRussell King  * @cctl: control reg values for current txd
193b23f204cSRussell King  * @ccfg: config reg values for current txd
19418536134SRussell King  * @done: this marks completed descriptors, which should not have their
19518536134SRussell King  *   mux released.
1963b24c20bSAlban Bedel  * @cyclic: indicate cyclic transfers
197b23f204cSRussell King  */
198b23f204cSRussell King struct pl08x_txd {
19901d8dc64SRussell King 	struct virt_dma_desc vd;
200b23f204cSRussell King 	struct list_head dsg_list;
201b23f204cSRussell King 	dma_addr_t llis_bus;
202ba6785ffSTomasz Figa 	u32 *llis_va;
203b23f204cSRussell King 	/* Default cctl value for LLIs */
204b23f204cSRussell King 	u32 cctl;
205b23f204cSRussell King 	/*
206b23f204cSRussell King 	 * Settings to be put into the physical channel when we
207b23f204cSRussell King 	 * trigger this txd.  Other registers are in llis_va[0].
208b23f204cSRussell King 	 */
209b23f204cSRussell King 	u32 ccfg;
21018536134SRussell King 	bool done;
2113b24c20bSAlban Bedel 	bool cyclic;
212b23f204cSRussell King };
213b23f204cSRussell King 
214b23f204cSRussell King /**
2158ee1bdc5SVinod Koul  * enum pl08x_dma_chan_state - holds the PL08x specific virtual channel
216b23f204cSRussell King  * states
217b23f204cSRussell King  * @PL08X_CHAN_IDLE: the channel is idle
218b23f204cSRussell King  * @PL08X_CHAN_RUNNING: the channel has allocated a physical transport
219b23f204cSRussell King  * channel and is running a transfer on it
220b23f204cSRussell King  * @PL08X_CHAN_PAUSED: the channel has allocated a physical transport
221b23f204cSRussell King  * channel, but the transfer is currently paused
222b23f204cSRussell King  * @PL08X_CHAN_WAITING: the channel is waiting for a physical transport
223b23f204cSRussell King  * channel to become available (only pertains to memcpy channels)
224b23f204cSRussell King  */
225b23f204cSRussell King enum pl08x_dma_chan_state {
226b23f204cSRussell King 	PL08X_CHAN_IDLE,
227b23f204cSRussell King 	PL08X_CHAN_RUNNING,
228b23f204cSRussell King 	PL08X_CHAN_PAUSED,
229b23f204cSRussell King 	PL08X_CHAN_WAITING,
230b23f204cSRussell King };
231b23f204cSRussell King 
232b23f204cSRussell King /**
233b23f204cSRussell King  * struct pl08x_dma_chan - this structure wraps a DMA ENGINE channel
234e4c4182fSJulia Lawall  * @vc: wrapped virtual channel
235b23f204cSRussell King  * @phychan: the physical channel utilized by this channel, if there is one
236b23f204cSRussell King  * @name: name of channel
237b23f204cSRussell King  * @cd: channel platform data
238da7cbd20SLinus Walleij  * @cfg: slave configuration
239b23f204cSRussell King  * @at: active transaction on this channel
240b23f204cSRussell King  * @host: a pointer to the host (internal use)
241b23f204cSRussell King  * @state: whether the channel is idle, paused, running etc
242b23f204cSRussell King  * @slave: whether this channel is a device (slave) or for memcpy
243ad0de2acSRussell King  * @signal: the physical DMA request signal which this channel is using
2445e2479bdSRussell King  * @mux_use: count of descriptors using this DMA request signal setting
2452ff25c1cSJean-Nicolas Graux  * @waiting_at: time in jiffies when this channel moved to waiting state
246b23f204cSRussell King  */
247b23f204cSRussell King struct pl08x_dma_chan {
24801d8dc64SRussell King 	struct virt_dma_chan vc;
249b23f204cSRussell King 	struct pl08x_phy_chan *phychan;
250550ec36fSRussell King 	const char *name;
251f9cd4761SLinus Walleij 	struct pl08x_channel_data *cd;
252ed91c13dSRussell King 	struct dma_slave_config cfg;
253b23f204cSRussell King 	struct pl08x_txd *at;
254b23f204cSRussell King 	struct pl08x_driver_data *host;
255b23f204cSRussell King 	enum pl08x_dma_chan_state state;
256b23f204cSRussell King 	bool slave;
257ad0de2acSRussell King 	int signal;
2585e2479bdSRussell King 	unsigned mux_use;
2592ff25c1cSJean-Nicolas Graux 	unsigned long waiting_at;
260b23f204cSRussell King };
261b23f204cSRussell King 
262b23f204cSRussell King /**
263e8689e63SLinus Walleij  * struct pl08x_driver_data - the local state holder for the PL08x
264ebe9b300SLinus Walleij  * @slave: optional slave engine for this instance
265e8689e63SLinus Walleij  * @memcpy: memcpy engine for this instance
266ebe9b300SLinus Walleij  * @has_slave: the PL08x has a slave engine (routed signals)
267e8689e63SLinus Walleij  * @base: virtual memory base (remapped) for the PL08x
268e8689e63SLinus Walleij  * @adev: the corresponding AMBA (PrimeCell) bus entry
269e8689e63SLinus Walleij  * @vd: vendor data for this PL08x variant
270e8689e63SLinus Walleij  * @pd: platform data passed in from the platform/machine
271e8689e63SLinus Walleij  * @phy_chans: array of data for the physical channels
272e8689e63SLinus Walleij  * @pool: a pool for the LLI descriptors
2733e27ee84SViresh Kumar  * @lli_buses: bitmask to or in to LLI pointer selecting AHB port for LLI
2743e27ee84SViresh Kumar  * fetches
27530749cb4SRussell King - ARM Linux  * @mem_buses: set to indicate memory transfers on AHB2.
276da7cbd20SLinus Walleij  * @lli_words: how many words are used in each LLI item for this variant
277e8689e63SLinus Walleij  */
278e8689e63SLinus Walleij struct pl08x_driver_data {
279e8689e63SLinus Walleij 	struct dma_device slave;
280e8689e63SLinus Walleij 	struct dma_device memcpy;
281ebe9b300SLinus Walleij 	bool has_slave;
282e8689e63SLinus Walleij 	void __iomem *base;
283e8689e63SLinus Walleij 	struct amba_device *adev;
284f96ca9ecSRussell King - ARM Linux 	const struct vendor_data *vd;
285e8689e63SLinus Walleij 	struct pl08x_platform_data *pd;
286e8689e63SLinus Walleij 	struct pl08x_phy_chan *phy_chans;
287e8689e63SLinus Walleij 	struct dma_pool *pool;
28830749cb4SRussell King - ARM Linux 	u8 lli_buses;
28930749cb4SRussell King - ARM Linux 	u8 mem_buses;
290ba6785ffSTomasz Figa 	u8 lli_words;
291e8689e63SLinus Walleij };
292e8689e63SLinus Walleij 
293e8689e63SLinus Walleij /*
294e8689e63SLinus Walleij  * PL08X specific defines
295e8689e63SLinus Walleij  */
296e8689e63SLinus Walleij 
297ba6785ffSTomasz Figa /* The order of words in an LLI. */
298ba6785ffSTomasz Figa #define PL080_LLI_SRC		0
299ba6785ffSTomasz Figa #define PL080_LLI_DST		1
300ba6785ffSTomasz Figa #define PL080_LLI_LLI		2
301ba6785ffSTomasz Figa #define PL080_LLI_CCTL		3
302da1b6c05STomasz Figa #define PL080S_LLI_CCTL2	4
303e8689e63SLinus Walleij 
304ba6785ffSTomasz Figa /* Total words in an LLI. */
305ba6785ffSTomasz Figa #define PL080_LLI_WORDS		4
306da1b6c05STomasz Figa #define PL080S_LLI_WORDS	8
307ba6785ffSTomasz Figa 
308ba6785ffSTomasz Figa /*
309ba6785ffSTomasz Figa  * Number of LLIs in each LLI buffer allocated for one transfer
310ba6785ffSTomasz Figa  * (maximum times we call dma_pool_alloc on this pool without freeing)
311ba6785ffSTomasz Figa  */
312ba6785ffSTomasz Figa #define MAX_NUM_TSFR_LLIS	512
313e8689e63SLinus Walleij #define PL08X_ALIGN		8
314e8689e63SLinus Walleij 
to_pl08x_chan(struct dma_chan * chan)315e8689e63SLinus Walleij static inline struct pl08x_dma_chan *to_pl08x_chan(struct dma_chan *chan)
316e8689e63SLinus Walleij {
31701d8dc64SRussell King 	return container_of(chan, struct pl08x_dma_chan, vc.chan);
318e8689e63SLinus Walleij }
319e8689e63SLinus Walleij 
to_pl08x_txd(struct dma_async_tx_descriptor * tx)320501e67e8SRussell King - ARM Linux static inline struct pl08x_txd *to_pl08x_txd(struct dma_async_tx_descriptor *tx)
321501e67e8SRussell King - ARM Linux {
32201d8dc64SRussell King 	return container_of(tx, struct pl08x_txd, vd.tx);
323501e67e8SRussell King - ARM Linux }
324501e67e8SRussell King - ARM Linux 
325e8689e63SLinus Walleij /*
3266b16c8b1SRussell King  * Mux handling.
3276b16c8b1SRussell King  *
3286b16c8b1SRussell King  * This gives us the DMA request input to the PL08x primecell which the
3296b16c8b1SRussell King  * peripheral described by the channel data will be routed to, possibly
3306b16c8b1SRussell King  * via a board/SoC specific external MUX.  One important point to note
3316b16c8b1SRussell King  * here is that this does not depend on the physical channel.
3326b16c8b1SRussell King  */
pl08x_request_mux(struct pl08x_dma_chan * plchan)333ad0de2acSRussell King static int pl08x_request_mux(struct pl08x_dma_chan *plchan)
3346b16c8b1SRussell King {
3356b16c8b1SRussell King 	const struct pl08x_platform_data *pd = plchan->host->pd;
3366b16c8b1SRussell King 	int ret;
3376b16c8b1SRussell King 
338d7cabeedSMark Brown 	if (plchan->mux_use++ == 0 && pd->get_xfer_signal) {
339d7cabeedSMark Brown 		ret = pd->get_xfer_signal(plchan->cd);
3405e2479bdSRussell King 		if (ret < 0) {
3415e2479bdSRussell King 			plchan->mux_use = 0;
3426b16c8b1SRussell King 			return ret;
3435e2479bdSRussell King 		}
3446b16c8b1SRussell King 
345ad0de2acSRussell King 		plchan->signal = ret;
3466b16c8b1SRussell King 	}
3476b16c8b1SRussell King 	return 0;
3486b16c8b1SRussell King }
3496b16c8b1SRussell King 
pl08x_release_mux(struct pl08x_dma_chan * plchan)3506b16c8b1SRussell King static void pl08x_release_mux(struct pl08x_dma_chan *plchan)
3516b16c8b1SRussell King {
3526b16c8b1SRussell King 	const struct pl08x_platform_data *pd = plchan->host->pd;
3536b16c8b1SRussell King 
3545e2479bdSRussell King 	if (plchan->signal >= 0) {
3555e2479bdSRussell King 		WARN_ON(plchan->mux_use == 0);
3565e2479bdSRussell King 
357d7cabeedSMark Brown 		if (--plchan->mux_use == 0 && pd->put_xfer_signal) {
358d7cabeedSMark Brown 			pd->put_xfer_signal(plchan->cd, plchan->signal);
359ad0de2acSRussell King 			plchan->signal = -1;
3606b16c8b1SRussell King 		}
3616b16c8b1SRussell King 	}
3625e2479bdSRussell King }
3636b16c8b1SRussell King 
3646b16c8b1SRussell King /*
365e8689e63SLinus Walleij  * Physical channel handling
366e8689e63SLinus Walleij  */
367e8689e63SLinus Walleij 
368e8689e63SLinus Walleij /* Whether a certain channel is busy or not */
pl08x_phy_channel_busy(struct pl08x_phy_chan * ch)369e8689e63SLinus Walleij static int pl08x_phy_channel_busy(struct pl08x_phy_chan *ch)
370e8689e63SLinus Walleij {
371e8689e63SLinus Walleij 	unsigned int val;
372e8689e63SLinus Walleij 
3731e1cfc72SLinus Walleij 	/* If we have a special busy register, take a shortcut */
3741e1cfc72SLinus Walleij 	if (ch->reg_busy) {
3751e1cfc72SLinus Walleij 		val = readl(ch->reg_busy);
3761e1cfc72SLinus Walleij 		return !!(val & BIT(ch->id));
3771e1cfc72SLinus Walleij 	}
378d86ccea7STomasz Figa 	val = readl(ch->reg_config);
379e8689e63SLinus Walleij 	return val & PL080_CONFIG_ACTIVE;
380e8689e63SLinus Walleij }
381e8689e63SLinus Walleij 
3821e1cfc72SLinus Walleij /*
3831e1cfc72SLinus Walleij  * pl08x_write_lli() - Write an LLI into the DMA controller.
3841e1cfc72SLinus Walleij  *
3851e1cfc72SLinus Walleij  * The PL08x derivatives support linked lists, but the first item of the
3861e1cfc72SLinus Walleij  * list containing the source, destination, control word and next LLI is
3871e1cfc72SLinus Walleij  * ignored. Instead the driver has to write those values directly into the
3881e1cfc72SLinus Walleij  * SRC, DST, LLI and control registers. On FTDMAC020 also the SIZE
3891e1cfc72SLinus Walleij  * register need to be set up for the first transfer.
3901e1cfc72SLinus Walleij  */
pl08x_write_lli(struct pl08x_driver_data * pl08x,struct pl08x_phy_chan * phychan,const u32 * lli,u32 ccfg)391ba6785ffSTomasz Figa static void pl08x_write_lli(struct pl08x_driver_data *pl08x,
392ba6785ffSTomasz Figa 		struct pl08x_phy_chan *phychan, const u32 *lli, u32 ccfg)
393ba6785ffSTomasz Figa {
394da1b6c05STomasz Figa 	if (pl08x->vd->pl080s)
395da1b6c05STomasz Figa 		dev_vdbg(&pl08x->adev->dev,
396da1b6c05STomasz Figa 			"WRITE channel %d: csrc=0x%08x, cdst=0x%08x, "
397da1b6c05STomasz Figa 			"clli=0x%08x, cctl=0x%08x, cctl2=0x%08x, ccfg=0x%08x\n",
398da1b6c05STomasz Figa 			phychan->id, lli[PL080_LLI_SRC], lli[PL080_LLI_DST],
399da1b6c05STomasz Figa 			lli[PL080_LLI_LLI], lli[PL080_LLI_CCTL],
400da1b6c05STomasz Figa 			lli[PL080S_LLI_CCTL2], ccfg);
401da1b6c05STomasz Figa 	else
402ba6785ffSTomasz Figa 		dev_vdbg(&pl08x->adev->dev,
403ba6785ffSTomasz Figa 			"WRITE channel %d: csrc=0x%08x, cdst=0x%08x, "
404ba6785ffSTomasz Figa 			"clli=0x%08x, cctl=0x%08x, ccfg=0x%08x\n",
405ba6785ffSTomasz Figa 			phychan->id, lli[PL080_LLI_SRC], lli[PL080_LLI_DST],
406ba6785ffSTomasz Figa 			lli[PL080_LLI_LLI], lli[PL080_LLI_CCTL], ccfg);
407ba6785ffSTomasz Figa 
4081e1cfc72SLinus Walleij 	writel_relaxed(lli[PL080_LLI_SRC], phychan->reg_src);
4091e1cfc72SLinus Walleij 	writel_relaxed(lli[PL080_LLI_DST], phychan->reg_dst);
4101e1cfc72SLinus Walleij 	writel_relaxed(lli[PL080_LLI_LLI], phychan->reg_lli);
411ba6785ffSTomasz Figa 
4121e1cfc72SLinus Walleij 	/*
4131e1cfc72SLinus Walleij 	 * The FTMAC020 has a different layout in the CCTL word of the LLI
4141e1cfc72SLinus Walleij 	 * and the CCTL register which is split in CSR and SIZE registers.
4151e1cfc72SLinus Walleij 	 * Convert the LLI item CCTL into the proper values to write into
4161e1cfc72SLinus Walleij 	 * the CSR and SIZE registers.
4171e1cfc72SLinus Walleij 	 */
4181e1cfc72SLinus Walleij 	if (phychan->ftdmac020) {
4191e1cfc72SLinus Walleij 		u32 llictl = lli[PL080_LLI_CCTL];
4201e1cfc72SLinus Walleij 		u32 val = 0;
4211e1cfc72SLinus Walleij 
4221e1cfc72SLinus Walleij 		/* Write the transfer size (12 bits) to the size register */
4231e1cfc72SLinus Walleij 		writel_relaxed(llictl & FTDMAC020_LLI_TRANSFER_SIZE_MASK,
4241e1cfc72SLinus Walleij 			       phychan->base + FTDMAC020_CH_SIZE);
4251e1cfc72SLinus Walleij 		/*
4261e1cfc72SLinus Walleij 		 * Then write the control bits 28..16 to the control register
4271e1cfc72SLinus Walleij 		 * by shuffleing the bits around to where they are in the
4281e1cfc72SLinus Walleij 		 * main register. The mapping is as follows:
4291e1cfc72SLinus Walleij 		 * Bit 28: TC_MSK - mask on all except last LLI
4301e1cfc72SLinus Walleij 		 * Bit 27..25: SRC_WIDTH
4311e1cfc72SLinus Walleij 		 * Bit 24..22: DST_WIDTH
4321e1cfc72SLinus Walleij 		 * Bit 21..20: SRCAD_CTRL
4331e1cfc72SLinus Walleij 		 * Bit 19..17: DSTAD_CTRL
4341e1cfc72SLinus Walleij 		 * Bit 17: SRC_SEL
4351e1cfc72SLinus Walleij 		 * Bit 16: DST_SEL
4361e1cfc72SLinus Walleij 		 */
4371e1cfc72SLinus Walleij 		if (llictl & FTDMAC020_LLI_TC_MSK)
4381e1cfc72SLinus Walleij 			val |= FTDMAC020_CH_CSR_TC_MSK;
4391e1cfc72SLinus Walleij 		val |= ((llictl  & FTDMAC020_LLI_SRC_WIDTH_MSK) >>
4401e1cfc72SLinus Walleij 			(FTDMAC020_LLI_SRC_WIDTH_SHIFT -
4411e1cfc72SLinus Walleij 			 FTDMAC020_CH_CSR_SRC_WIDTH_SHIFT));
4421e1cfc72SLinus Walleij 		val |= ((llictl  & FTDMAC020_LLI_DST_WIDTH_MSK) >>
4431e1cfc72SLinus Walleij 			(FTDMAC020_LLI_DST_WIDTH_SHIFT -
4441e1cfc72SLinus Walleij 			 FTDMAC020_CH_CSR_DST_WIDTH_SHIFT));
4451e1cfc72SLinus Walleij 		val |= ((llictl  & FTDMAC020_LLI_SRCAD_CTL_MSK) >>
4461e1cfc72SLinus Walleij 			(FTDMAC020_LLI_SRCAD_CTL_SHIFT -
4471e1cfc72SLinus Walleij 			 FTDMAC020_CH_CSR_SRCAD_CTL_SHIFT));
4481e1cfc72SLinus Walleij 		val |= ((llictl  & FTDMAC020_LLI_DSTAD_CTL_MSK) >>
4491e1cfc72SLinus Walleij 			(FTDMAC020_LLI_DSTAD_CTL_SHIFT -
4501e1cfc72SLinus Walleij 			 FTDMAC020_CH_CSR_DSTAD_CTL_SHIFT));
4511e1cfc72SLinus Walleij 		if (llictl & FTDMAC020_LLI_SRC_SEL)
4521e1cfc72SLinus Walleij 			val |= FTDMAC020_CH_CSR_SRC_SEL;
4531e1cfc72SLinus Walleij 		if (llictl & FTDMAC020_LLI_DST_SEL)
4541e1cfc72SLinus Walleij 			val |= FTDMAC020_CH_CSR_DST_SEL;
4551e1cfc72SLinus Walleij 
4561e1cfc72SLinus Walleij 		/*
4571e1cfc72SLinus Walleij 		 * Set up the bits that exist in the CSR but are not
4581e1cfc72SLinus Walleij 		 * part the LLI, i.e. only gets written to the control
4591e1cfc72SLinus Walleij 		 * register right here.
4601e1cfc72SLinus Walleij 		 *
4611e1cfc72SLinus Walleij 		 * FIXME: do not just handle memcpy, also handle slave DMA.
4621e1cfc72SLinus Walleij 		 */
4631e1cfc72SLinus Walleij 		switch (pl08x->pd->memcpy_burst_size) {
4641e1cfc72SLinus Walleij 		default:
4651e1cfc72SLinus Walleij 		case PL08X_BURST_SZ_1:
4661e1cfc72SLinus Walleij 			val |= PL080_BSIZE_1 <<
4671e1cfc72SLinus Walleij 				FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
4681e1cfc72SLinus Walleij 			break;
4691e1cfc72SLinus Walleij 		case PL08X_BURST_SZ_4:
4701e1cfc72SLinus Walleij 			val |= PL080_BSIZE_4 <<
4711e1cfc72SLinus Walleij 				FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
4721e1cfc72SLinus Walleij 			break;
4731e1cfc72SLinus Walleij 		case PL08X_BURST_SZ_8:
4741e1cfc72SLinus Walleij 			val |= PL080_BSIZE_8 <<
4751e1cfc72SLinus Walleij 				FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
4761e1cfc72SLinus Walleij 			break;
4771e1cfc72SLinus Walleij 		case PL08X_BURST_SZ_16:
4781e1cfc72SLinus Walleij 			val |= PL080_BSIZE_16 <<
4791e1cfc72SLinus Walleij 				FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
4801e1cfc72SLinus Walleij 			break;
4811e1cfc72SLinus Walleij 		case PL08X_BURST_SZ_32:
4821e1cfc72SLinus Walleij 			val |= PL080_BSIZE_32 <<
4831e1cfc72SLinus Walleij 				FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
4841e1cfc72SLinus Walleij 			break;
4851e1cfc72SLinus Walleij 		case PL08X_BURST_SZ_64:
4861e1cfc72SLinus Walleij 			val |= PL080_BSIZE_64 <<
4871e1cfc72SLinus Walleij 				FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
4881e1cfc72SLinus Walleij 			break;
4891e1cfc72SLinus Walleij 		case PL08X_BURST_SZ_128:
4901e1cfc72SLinus Walleij 			val |= PL080_BSIZE_128 <<
4911e1cfc72SLinus Walleij 				FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
4921e1cfc72SLinus Walleij 			break;
4931e1cfc72SLinus Walleij 		case PL08X_BURST_SZ_256:
4941e1cfc72SLinus Walleij 			val |= PL080_BSIZE_256 <<
4951e1cfc72SLinus Walleij 				FTDMAC020_CH_CSR_SRC_SIZE_SHIFT;
4961e1cfc72SLinus Walleij 			break;
4971e1cfc72SLinus Walleij 		}
4981e1cfc72SLinus Walleij 
4991e1cfc72SLinus Walleij 		/* Protection flags */
5001e1cfc72SLinus Walleij 		if (pl08x->pd->memcpy_prot_buff)
5011e1cfc72SLinus Walleij 			val |= FTDMAC020_CH_CSR_PROT2;
5021e1cfc72SLinus Walleij 		if (pl08x->pd->memcpy_prot_cache)
5031e1cfc72SLinus Walleij 			val |= FTDMAC020_CH_CSR_PROT3;
5041e1cfc72SLinus Walleij 		/* We are the kernel, so we are in privileged mode */
5051e1cfc72SLinus Walleij 		val |= FTDMAC020_CH_CSR_PROT1;
5061e1cfc72SLinus Walleij 
5071e1cfc72SLinus Walleij 		writel_relaxed(val, phychan->reg_control);
5081e1cfc72SLinus Walleij 	} else {
5091e1cfc72SLinus Walleij 		/* Bits are just identical */
5101e1cfc72SLinus Walleij 		writel_relaxed(lli[PL080_LLI_CCTL], phychan->reg_control);
5111e1cfc72SLinus Walleij 	}
5121e1cfc72SLinus Walleij 
5131e1cfc72SLinus Walleij 	/* Second control word on the PL080s */
514da1b6c05STomasz Figa 	if (pl08x->vd->pl080s)
515da1b6c05STomasz Figa 		writel_relaxed(lli[PL080S_LLI_CCTL2],
516da1b6c05STomasz Figa 				phychan->base + PL080S_CH_CONTROL2);
517da1b6c05STomasz Figa 
518ba6785ffSTomasz Figa 	writel(ccfg, phychan->reg_config);
519ba6785ffSTomasz Figa }
520ba6785ffSTomasz Figa 
521e8689e63SLinus Walleij /*
522e8689e63SLinus Walleij  * Set the initial DMA register values i.e. those for the first LLI
523e8b5e11dSRussell King - ARM Linux  * The next LLI pointer and the configuration interrupt bit have
524c885bee4SRussell King - ARM Linux  * been set when the LLIs were constructed.  Poke them into the hardware
525c885bee4SRussell King - ARM Linux  * and start the transfer.
526e8689e63SLinus Walleij  */
pl08x_start_next_txd(struct pl08x_dma_chan * plchan)527eab82533SRussell King static void pl08x_start_next_txd(struct pl08x_dma_chan *plchan)
528e8689e63SLinus Walleij {
529c885bee4SRussell King - ARM Linux 	struct pl08x_driver_data *pl08x = plchan->host;
530e8689e63SLinus Walleij 	struct pl08x_phy_chan *phychan = plchan->phychan;
531879f127bSRussell King 	struct virt_dma_desc *vd = vchan_next_desc(&plchan->vc);
532879f127bSRussell King 	struct pl08x_txd *txd = to_pl08x_txd(&vd->tx);
53309b3c323SRussell King - ARM Linux 	u32 val;
534c885bee4SRussell King - ARM Linux 
535879f127bSRussell King 	list_del(&txd->vd.node);
536eab82533SRussell King 
537c885bee4SRussell King - ARM Linux 	plchan->at = txd;
538e8689e63SLinus Walleij 
539c885bee4SRussell King - ARM Linux 	/* Wait for channel inactive */
540c885bee4SRussell King - ARM Linux 	while (pl08x_phy_channel_busy(phychan))
54119386b32SRussell King - ARM Linux 		cpu_relax();
542e8689e63SLinus Walleij 
543ba6785ffSTomasz Figa 	pl08x_write_lli(pl08x, phychan, &txd->llis_va[0], txd->ccfg);
544c885bee4SRussell King - ARM Linux 
545c885bee4SRussell King - ARM Linux 	/* Enable the DMA channel */
546c885bee4SRussell King - ARM Linux 	/* Do not access config register until channel shows as disabled */
547ded091feSLinus Walleij 	while (readl(pl08x->base + PL080_EN_CHAN) & BIT(phychan->id))
548c885bee4SRussell King - ARM Linux 		cpu_relax();
549c885bee4SRussell King - ARM Linux 
550c885bee4SRussell King - ARM Linux 	/* Do not access config register until channel shows as inactive */
5511e1cfc72SLinus Walleij 	if (phychan->ftdmac020) {
552d86ccea7STomasz Figa 		val = readl(phychan->reg_config);
5531e1cfc72SLinus Walleij 		while (val & FTDMAC020_CH_CFG_BUSY)
5541e1cfc72SLinus Walleij 			val = readl(phychan->reg_config);
5551e1cfc72SLinus Walleij 
5561e1cfc72SLinus Walleij 		val = readl(phychan->reg_control);
5571e1cfc72SLinus Walleij 		while (val & FTDMAC020_CH_CSR_EN)
5581e1cfc72SLinus Walleij 			val = readl(phychan->reg_control);
5591e1cfc72SLinus Walleij 
5601e1cfc72SLinus Walleij 		writel(val | FTDMAC020_CH_CSR_EN,
5611e1cfc72SLinus Walleij 		       phychan->reg_control);
5621e1cfc72SLinus Walleij 	} else {
5631e1cfc72SLinus Walleij 		val = readl(phychan->reg_config);
5641e1cfc72SLinus Walleij 		while ((val & PL080_CONFIG_ACTIVE) ||
5651e1cfc72SLinus Walleij 		       (val & PL080_CONFIG_ENABLE))
566d86ccea7STomasz Figa 			val = readl(phychan->reg_config);
567c885bee4SRussell King - ARM Linux 
568d86ccea7STomasz Figa 		writel(val | PL080_CONFIG_ENABLE, phychan->reg_config);
569e8689e63SLinus Walleij 	}
5701e1cfc72SLinus Walleij }
571e8689e63SLinus Walleij 
572e8689e63SLinus Walleij /*
57381796616SRussell King - ARM Linux  * Pause the channel by setting the HALT bit.
574e8689e63SLinus Walleij  *
57581796616SRussell King - ARM Linux  * For M->P transfers, pause the DMAC first and then stop the peripheral -
57681796616SRussell King - ARM Linux  * the FIFO can only drain if the peripheral is still requesting data.
57781796616SRussell King - ARM Linux  * (note: this can still timeout if the DMAC FIFO never drains of data.)
578e8689e63SLinus Walleij  *
57981796616SRussell King - ARM Linux  * For P->M transfers, disable the peripheral first to stop it filling
58081796616SRussell King - ARM Linux  * the DMAC FIFO, and then pause the DMAC.
581e8689e63SLinus Walleij  */
pl08x_pause_phy_chan(struct pl08x_phy_chan * ch)582e8689e63SLinus Walleij static void pl08x_pause_phy_chan(struct pl08x_phy_chan *ch)
583e8689e63SLinus Walleij {
584e8689e63SLinus Walleij 	u32 val;
58581796616SRussell King - ARM Linux 	int timeout;
586e8689e63SLinus Walleij 
5871e1cfc72SLinus Walleij 	if (ch->ftdmac020) {
5881e1cfc72SLinus Walleij 		/* Use the enable bit on the FTDMAC020 */
5891e1cfc72SLinus Walleij 		val = readl(ch->reg_control);
5901e1cfc72SLinus Walleij 		val &= ~FTDMAC020_CH_CSR_EN;
5911e1cfc72SLinus Walleij 		writel(val, ch->reg_control);
5921e1cfc72SLinus Walleij 		return;
5931e1cfc72SLinus Walleij 	}
5941e1cfc72SLinus Walleij 
595e8689e63SLinus Walleij 	/* Set the HALT bit and wait for the FIFO to drain */
596d86ccea7STomasz Figa 	val = readl(ch->reg_config);
597e8689e63SLinus Walleij 	val |= PL080_CONFIG_HALT;
598d86ccea7STomasz Figa 	writel(val, ch->reg_config);
599e8689e63SLinus Walleij 
600e8689e63SLinus Walleij 	/* Wait for channel inactive */
60181796616SRussell King - ARM Linux 	for (timeout = 1000; timeout; timeout--) {
60281796616SRussell King - ARM Linux 		if (!pl08x_phy_channel_busy(ch))
60381796616SRussell King - ARM Linux 			break;
60481796616SRussell King - ARM Linux 		udelay(1);
60581796616SRussell King - ARM Linux 	}
60681796616SRussell King - ARM Linux 	if (pl08x_phy_channel_busy(ch))
60781796616SRussell King - ARM Linux 		pr_err("pl08x: channel%u timeout waiting for pause\n", ch->id);
608e8689e63SLinus Walleij }
609e8689e63SLinus Walleij 
pl08x_resume_phy_chan(struct pl08x_phy_chan * ch)610e8689e63SLinus Walleij static void pl08x_resume_phy_chan(struct pl08x_phy_chan *ch)
611e8689e63SLinus Walleij {
612e8689e63SLinus Walleij 	u32 val;
613e8689e63SLinus Walleij 
6141e1cfc72SLinus Walleij 	/* Use the enable bit on the FTDMAC020 */
6151e1cfc72SLinus Walleij 	if (ch->ftdmac020) {
6161e1cfc72SLinus Walleij 		val = readl(ch->reg_control);
6171e1cfc72SLinus Walleij 		val |= FTDMAC020_CH_CSR_EN;
6181e1cfc72SLinus Walleij 		writel(val, ch->reg_control);
6191e1cfc72SLinus Walleij 		return;
6201e1cfc72SLinus Walleij 	}
6211e1cfc72SLinus Walleij 
622e8689e63SLinus Walleij 	/* Clear the HALT bit */
623d86ccea7STomasz Figa 	val = readl(ch->reg_config);
624e8689e63SLinus Walleij 	val &= ~PL080_CONFIG_HALT;
625d86ccea7STomasz Figa 	writel(val, ch->reg_config);
626e8689e63SLinus Walleij }
627e8689e63SLinus Walleij 
628fb526210SRussell King - ARM Linux /*
629fb526210SRussell King - ARM Linux  * pl08x_terminate_phy_chan() stops the channel, clears the FIFO and
630fb526210SRussell King - ARM Linux  * clears any pending interrupt status.  This should not be used for
631fb526210SRussell King - ARM Linux  * an on-going transfer, but as a method of shutting down a channel
632fb526210SRussell King - ARM Linux  * (eg, when it's no longer used) or terminating a transfer.
633fb526210SRussell King - ARM Linux  */
pl08x_terminate_phy_chan(struct pl08x_driver_data * pl08x,struct pl08x_phy_chan * ch)634fb526210SRussell King - ARM Linux static void pl08x_terminate_phy_chan(struct pl08x_driver_data *pl08x,
635fb526210SRussell King - ARM Linux 	struct pl08x_phy_chan *ch)
636e8689e63SLinus Walleij {
6371e1cfc72SLinus Walleij 	u32 val;
638e8689e63SLinus Walleij 
6391e1cfc72SLinus Walleij 	/* The layout for the FTDMAC020 is different */
6401e1cfc72SLinus Walleij 	if (ch->ftdmac020) {
6411e1cfc72SLinus Walleij 		/* Disable all interrupts */
6421e1cfc72SLinus Walleij 		val = readl(ch->reg_config);
6431e1cfc72SLinus Walleij 		val |= (FTDMAC020_CH_CFG_INT_ABT_MASK |
6441e1cfc72SLinus Walleij 			FTDMAC020_CH_CFG_INT_ERR_MASK |
6451e1cfc72SLinus Walleij 			FTDMAC020_CH_CFG_INT_TC_MASK);
6461e1cfc72SLinus Walleij 		writel(val, ch->reg_config);
6471e1cfc72SLinus Walleij 
6481e1cfc72SLinus Walleij 		/* Abort and disable channel */
6491e1cfc72SLinus Walleij 		val = readl(ch->reg_control);
6501e1cfc72SLinus Walleij 		val &= ~FTDMAC020_CH_CSR_EN;
6511e1cfc72SLinus Walleij 		val |= FTDMAC020_CH_CSR_ABT;
6521e1cfc72SLinus Walleij 		writel(val, ch->reg_control);
6531e1cfc72SLinus Walleij 
6541e1cfc72SLinus Walleij 		/* Clear ABT and ERR interrupt flags */
6551e1cfc72SLinus Walleij 		writel(BIT(ch->id) | BIT(ch->id + 16),
6561e1cfc72SLinus Walleij 		       pl08x->base + PL080_ERR_CLEAR);
6571e1cfc72SLinus Walleij 		writel(BIT(ch->id), pl08x->base + PL080_TC_CLEAR);
6581e1cfc72SLinus Walleij 
6591e1cfc72SLinus Walleij 		return;
6601e1cfc72SLinus Walleij 	}
6611e1cfc72SLinus Walleij 
6621e1cfc72SLinus Walleij 	val = readl(ch->reg_config);
663fb526210SRussell King - ARM Linux 	val &= ~(PL080_CONFIG_ENABLE | PL080_CONFIG_ERR_IRQ_MASK |
664fb526210SRussell King - ARM Linux 		 PL080_CONFIG_TC_IRQ_MASK);
665d86ccea7STomasz Figa 	writel(val, ch->reg_config);
666fb526210SRussell King - ARM Linux 
667ded091feSLinus Walleij 	writel(BIT(ch->id), pl08x->base + PL080_ERR_CLEAR);
668ded091feSLinus Walleij 	writel(BIT(ch->id), pl08x->base + PL080_TC_CLEAR);
669e8689e63SLinus Walleij }
670e8689e63SLinus Walleij 
get_bytes_in_phy_channel(struct pl08x_phy_chan * ch)6711e1cfc72SLinus Walleij static u32 get_bytes_in_phy_channel(struct pl08x_phy_chan *ch)
672e8689e63SLinus Walleij {
6731e1cfc72SLinus Walleij 	u32 val;
6741e1cfc72SLinus Walleij 	u32 bytes;
675e8689e63SLinus Walleij 
6761e1cfc72SLinus Walleij 	if (ch->ftdmac020) {
6771e1cfc72SLinus Walleij 		bytes = readl(ch->base + FTDMAC020_CH_SIZE);
678f3287a52SAlban Bedel 
6791e1cfc72SLinus Walleij 		val = readl(ch->reg_control);
6801e1cfc72SLinus Walleij 		val &= FTDMAC020_CH_CSR_SRC_WIDTH_MSK;
6811e1cfc72SLinus Walleij 		val >>= FTDMAC020_CH_CSR_SRC_WIDTH_SHIFT;
6821e1cfc72SLinus Walleij 	} else if (ch->pl080s) {
6831e1cfc72SLinus Walleij 		val = readl(ch->base + PL080S_CH_CONTROL2);
6841e1cfc72SLinus Walleij 		bytes = val & PL080S_CONTROL_TRANSFER_SIZE_MASK;
6851e1cfc72SLinus Walleij 
6861e1cfc72SLinus Walleij 		val = readl(ch->reg_control);
6871e1cfc72SLinus Walleij 		val &= PL080_CONTROL_SWIDTH_MASK;
6881e1cfc72SLinus Walleij 		val >>= PL080_CONTROL_SWIDTH_SHIFT;
6891e1cfc72SLinus Walleij 	} else {
6901e1cfc72SLinus Walleij 		/* Plain PL08x */
6911e1cfc72SLinus Walleij 		val = readl(ch->reg_control);
6921e1cfc72SLinus Walleij 		bytes = val & PL080_CONTROL_TRANSFER_SIZE_MASK;
6931e1cfc72SLinus Walleij 
6941e1cfc72SLinus Walleij 		val &= PL080_CONTROL_SWIDTH_MASK;
6951e1cfc72SLinus Walleij 		val >>= PL080_CONTROL_SWIDTH_SHIFT;
6961e1cfc72SLinus Walleij 	}
6971e1cfc72SLinus Walleij 
6981e1cfc72SLinus Walleij 	switch (val) {
699e8689e63SLinus Walleij 	case PL080_WIDTH_8BIT:
700e8689e63SLinus Walleij 		break;
701e8689e63SLinus Walleij 	case PL080_WIDTH_16BIT:
702e8689e63SLinus Walleij 		bytes *= 2;
703e8689e63SLinus Walleij 		break;
704e8689e63SLinus Walleij 	case PL080_WIDTH_32BIT:
705e8689e63SLinus Walleij 		bytes *= 4;
706e8689e63SLinus Walleij 		break;
707e8689e63SLinus Walleij 	}
708e8689e63SLinus Walleij 	return bytes;
709e8689e63SLinus Walleij }
710e8689e63SLinus Walleij 
get_bytes_in_lli(struct pl08x_phy_chan * ch,const u32 * llis_va)7111e1cfc72SLinus Walleij static u32 get_bytes_in_lli(struct pl08x_phy_chan *ch, const u32 *llis_va)
712da1b6c05STomasz Figa {
7131e1cfc72SLinus Walleij 	u32 val;
7141e1cfc72SLinus Walleij 	u32 bytes;
715da1b6c05STomasz Figa 
7161e1cfc72SLinus Walleij 	if (ch->ftdmac020) {
7171e1cfc72SLinus Walleij 		val = llis_va[PL080_LLI_CCTL];
7181e1cfc72SLinus Walleij 		bytes = val & FTDMAC020_LLI_TRANSFER_SIZE_MASK;
719f3287a52SAlban Bedel 
7201e1cfc72SLinus Walleij 		val = llis_va[PL080_LLI_CCTL];
7211e1cfc72SLinus Walleij 		val &= FTDMAC020_LLI_SRC_WIDTH_MSK;
7221e1cfc72SLinus Walleij 		val >>= FTDMAC020_LLI_SRC_WIDTH_SHIFT;
7231e1cfc72SLinus Walleij 	} else if (ch->pl080s) {
7241e1cfc72SLinus Walleij 		val = llis_va[PL080S_LLI_CCTL2];
7251e1cfc72SLinus Walleij 		bytes = val & PL080S_CONTROL_TRANSFER_SIZE_MASK;
7261e1cfc72SLinus Walleij 
7271e1cfc72SLinus Walleij 		val = llis_va[PL080_LLI_CCTL];
7281e1cfc72SLinus Walleij 		val &= PL080_CONTROL_SWIDTH_MASK;
7291e1cfc72SLinus Walleij 		val >>= PL080_CONTROL_SWIDTH_SHIFT;
7301e1cfc72SLinus Walleij 	} else {
7311e1cfc72SLinus Walleij 		/* Plain PL08x */
7321e1cfc72SLinus Walleij 		val = llis_va[PL080_LLI_CCTL];
7331e1cfc72SLinus Walleij 		bytes = val & PL080_CONTROL_TRANSFER_SIZE_MASK;
7341e1cfc72SLinus Walleij 
7351e1cfc72SLinus Walleij 		val &= PL080_CONTROL_SWIDTH_MASK;
7361e1cfc72SLinus Walleij 		val >>= PL080_CONTROL_SWIDTH_SHIFT;
7371e1cfc72SLinus Walleij 	}
7381e1cfc72SLinus Walleij 
7391e1cfc72SLinus Walleij 	switch (val) {
740e8689e63SLinus Walleij 	case PL080_WIDTH_8BIT:
741e8689e63SLinus Walleij 		break;
742e8689e63SLinus Walleij 	case PL080_WIDTH_16BIT:
743e8689e63SLinus Walleij 		bytes *= 2;
744e8689e63SLinus Walleij 		break;
745e8689e63SLinus Walleij 	case PL080_WIDTH_32BIT:
746e8689e63SLinus Walleij 		bytes *= 4;
747e8689e63SLinus Walleij 		break;
748e8689e63SLinus Walleij 	}
749e8689e63SLinus Walleij 	return bytes;
750e8689e63SLinus Walleij }
751e8689e63SLinus Walleij 
752e8689e63SLinus Walleij /* The channel should be paused when calling this */
pl08x_getbytes_chan(struct pl08x_dma_chan * plchan)753e8689e63SLinus Walleij static u32 pl08x_getbytes_chan(struct pl08x_dma_chan *plchan)
754e8689e63SLinus Walleij {
755ba6785ffSTomasz Figa 	struct pl08x_driver_data *pl08x = plchan->host;
756ba6785ffSTomasz Figa 	const u32 *llis_va, *llis_va_limit;
757e8689e63SLinus Walleij 	struct pl08x_phy_chan *ch;
75868a7faa2STomasz Figa 	dma_addr_t llis_bus;
759e8689e63SLinus Walleij 	struct pl08x_txd *txd;
760ba6785ffSTomasz Figa 	u32 llis_max_words;
76168a7faa2STomasz Figa 	size_t bytes;
76268a7faa2STomasz Figa 	u32 clli;
763e8689e63SLinus Walleij 
764e8689e63SLinus Walleij 	ch = plchan->phychan;
765e8689e63SLinus Walleij 	txd = plchan->at;
766e8689e63SLinus Walleij 
76768a7faa2STomasz Figa 	if (!ch || !txd)
76868a7faa2STomasz Figa 		return 0;
76968a7faa2STomasz Figa 
770e8689e63SLinus Walleij 	/*
771db9f136aSRussell King - ARM Linux 	 * Follow the LLIs to get the number of remaining
772db9f136aSRussell King - ARM Linux 	 * bytes in the currently active transaction.
773e8689e63SLinus Walleij 	 */
7741e1cfc72SLinus Walleij 	clli = readl(ch->reg_lli) & ~PL080_LLI_LM_AHB2;
775e8689e63SLinus Walleij 
776db9f136aSRussell King - ARM Linux 	/* First get the remaining bytes in the active transfer */
7771e1cfc72SLinus Walleij 	bytes = get_bytes_in_phy_channel(ch);
778e8689e63SLinus Walleij 
77968a7faa2STomasz Figa 	if (!clli)
78068a7faa2STomasz Figa 		return bytes;
781e8689e63SLinus Walleij 
78268a7faa2STomasz Figa 	llis_va = txd->llis_va;
78368a7faa2STomasz Figa 	llis_bus = txd->llis_bus;
784e8689e63SLinus Walleij 
785ba6785ffSTomasz Figa 	llis_max_words = pl08x->lli_words * MAX_NUM_TSFR_LLIS;
786db9f136aSRussell King - ARM Linux 	BUG_ON(clli < llis_bus || clli >= llis_bus +
787ba6785ffSTomasz Figa 						sizeof(u32) * llis_max_words);
788e8689e63SLinus Walleij 
789db9f136aSRussell King - ARM Linux 	/*
790db9f136aSRussell King - ARM Linux 	 * Locate the next LLI - as this is an array,
791db9f136aSRussell King - ARM Linux 	 * it's simple maths to find.
792db9f136aSRussell King - ARM Linux 	 */
793ba6785ffSTomasz Figa 	llis_va += (clli - llis_bus) / sizeof(u32);
794db9f136aSRussell King - ARM Linux 
795ba6785ffSTomasz Figa 	llis_va_limit = llis_va + llis_max_words;
796ba6785ffSTomasz Figa 
797ba6785ffSTomasz Figa 	for (; llis_va < llis_va_limit; llis_va += pl08x->lli_words) {
7981e1cfc72SLinus Walleij 		bytes += get_bytes_in_lli(ch, llis_va);
799db9f136aSRussell King - ARM Linux 
800e8689e63SLinus Walleij 		/*
8013b24c20bSAlban Bedel 		 * A LLI pointer going backward terminates the LLI list
802e8689e63SLinus Walleij 		 */
8033b24c20bSAlban Bedel 		if (llis_va[PL080_LLI_LLI] <= clli)
804db9f136aSRussell King - ARM Linux 			break;
805e8689e63SLinus Walleij 	}
806e8689e63SLinus Walleij 
807e8689e63SLinus Walleij 	return bytes;
808e8689e63SLinus Walleij }
809e8689e63SLinus Walleij 
810e8689e63SLinus Walleij /*
811e8689e63SLinus Walleij  * Allocate a physical channel for a virtual channel
81294ae8522SRussell King - ARM Linux  *
81394ae8522SRussell King - ARM Linux  * Try to locate a physical channel to be used for this transfer. If all
81494ae8522SRussell King - ARM Linux  * are taken return NULL and the requester will have to cope by using
81594ae8522SRussell King - ARM Linux  * some fallback PIO mode or retrying later.
816e8689e63SLinus Walleij  */
817e8689e63SLinus Walleij static struct pl08x_phy_chan *
pl08x_get_phy_channel(struct pl08x_driver_data * pl08x,struct pl08x_dma_chan * virt_chan)818e8689e63SLinus Walleij pl08x_get_phy_channel(struct pl08x_driver_data *pl08x,
819e8689e63SLinus Walleij 		      struct pl08x_dma_chan *virt_chan)
820e8689e63SLinus Walleij {
821e8689e63SLinus Walleij 	struct pl08x_phy_chan *ch = NULL;
822e8689e63SLinus Walleij 	unsigned long flags;
823e8689e63SLinus Walleij 	int i;
824e8689e63SLinus Walleij 
825e8689e63SLinus Walleij 	for (i = 0; i < pl08x->vd->channels; i++) {
826e8689e63SLinus Walleij 		ch = &pl08x->phy_chans[i];
827e8689e63SLinus Walleij 
828e8689e63SLinus Walleij 		spin_lock_irqsave(&ch->lock, flags);
829e8689e63SLinus Walleij 
830affa115eSLinus Walleij 		if (!ch->locked && !ch->serving) {
831e8689e63SLinus Walleij 			ch->serving = virt_chan;
832e8689e63SLinus Walleij 			spin_unlock_irqrestore(&ch->lock, flags);
833e8689e63SLinus Walleij 			break;
834e8689e63SLinus Walleij 		}
835e8689e63SLinus Walleij 
836e8689e63SLinus Walleij 		spin_unlock_irqrestore(&ch->lock, flags);
837e8689e63SLinus Walleij 	}
838e8689e63SLinus Walleij 
839e8689e63SLinus Walleij 	if (i == pl08x->vd->channels) {
840e8689e63SLinus Walleij 		/* No physical channel available, cope with it */
841e8689e63SLinus Walleij 		return NULL;
842e8689e63SLinus Walleij 	}
843e8689e63SLinus Walleij 
844e8689e63SLinus Walleij 	return ch;
845e8689e63SLinus Walleij }
846e8689e63SLinus Walleij 
847a5a488dbSRussell King /* 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)848e8689e63SLinus Walleij static inline void pl08x_put_phy_channel(struct pl08x_driver_data *pl08x,
849e8689e63SLinus Walleij 					 struct pl08x_phy_chan *ch)
850e8689e63SLinus Walleij {
851e8689e63SLinus Walleij 	ch->serving = NULL;
852a5a488dbSRussell King }
853a5a488dbSRussell King 
854a5a488dbSRussell King /*
855a5a488dbSRussell King  * Try to allocate a physical channel.  When successful, assign it to
856a5a488dbSRussell King  * this virtual channel, and initiate the next descriptor.  The
857a5a488dbSRussell King  * virtual channel lock must be held at this point.
858a5a488dbSRussell King  */
pl08x_phy_alloc_and_start(struct pl08x_dma_chan * plchan)859a5a488dbSRussell King static void pl08x_phy_alloc_and_start(struct pl08x_dma_chan *plchan)
860a5a488dbSRussell King {
861a5a488dbSRussell King 	struct pl08x_driver_data *pl08x = plchan->host;
862a5a488dbSRussell King 	struct pl08x_phy_chan *ch;
863a5a488dbSRussell King 
864a5a488dbSRussell King 	ch = pl08x_get_phy_channel(pl08x, plchan);
865a5a488dbSRussell King 	if (!ch) {
866a5a488dbSRussell King 		dev_dbg(&pl08x->adev->dev, "no physical channel available for xfer on %s\n", plchan->name);
867a5a488dbSRussell King 		plchan->state = PL08X_CHAN_WAITING;
8682ff25c1cSJean-Nicolas Graux 		plchan->waiting_at = jiffies;
869a5a488dbSRussell King 		return;
870a5a488dbSRussell King 	}
871a5a488dbSRussell King 
872a5a488dbSRussell King 	dev_dbg(&pl08x->adev->dev, "allocated physical channel %d for xfer on %s\n",
873a5a488dbSRussell King 		ch->id, plchan->name);
874a5a488dbSRussell King 
875a5a488dbSRussell King 	plchan->phychan = ch;
876a5a488dbSRussell King 	plchan->state = PL08X_CHAN_RUNNING;
877a5a488dbSRussell King 	pl08x_start_next_txd(plchan);
878a5a488dbSRussell King }
879a5a488dbSRussell King 
pl08x_phy_reassign_start(struct pl08x_phy_chan * ch,struct pl08x_dma_chan * plchan)880a5a488dbSRussell King static void pl08x_phy_reassign_start(struct pl08x_phy_chan *ch,
881a5a488dbSRussell King 	struct pl08x_dma_chan *plchan)
882a5a488dbSRussell King {
883a5a488dbSRussell King 	struct pl08x_driver_data *pl08x = plchan->host;
884a5a488dbSRussell King 
885a5a488dbSRussell King 	dev_dbg(&pl08x->adev->dev, "reassigned physical channel %d for xfer on %s\n",
886a5a488dbSRussell King 		ch->id, plchan->name);
887a5a488dbSRussell King 
888a5a488dbSRussell King 	/*
889a5a488dbSRussell King 	 * We do this without taking the lock; we're really only concerned
890a5a488dbSRussell King 	 * about whether this pointer is NULL or not, and we're guaranteed
891a5a488dbSRussell King 	 * that this will only be called when it _already_ is non-NULL.
892a5a488dbSRussell King 	 */
893a5a488dbSRussell King 	ch->serving = plchan;
894a5a488dbSRussell King 	plchan->phychan = ch;
895a5a488dbSRussell King 	plchan->state = PL08X_CHAN_RUNNING;
896a5a488dbSRussell King 	pl08x_start_next_txd(plchan);
897a5a488dbSRussell King }
898a5a488dbSRussell King 
899a5a488dbSRussell King /*
900a5a488dbSRussell King  * Free a physical DMA channel, potentially reallocating it to another
901a5a488dbSRussell King  * virtual channel if we have any pending.
902a5a488dbSRussell King  */
pl08x_phy_free(struct pl08x_dma_chan * plchan)903a5a488dbSRussell King static void pl08x_phy_free(struct pl08x_dma_chan *plchan)
904a5a488dbSRussell King {
905a5a488dbSRussell King 	struct pl08x_driver_data *pl08x = plchan->host;
906a5a488dbSRussell King 	struct pl08x_dma_chan *p, *next;
9072ff25c1cSJean-Nicolas Graux 	unsigned long waiting_at;
908a5a488dbSRussell King  retry:
909a5a488dbSRussell King 	next = NULL;
9102ff25c1cSJean-Nicolas Graux 	waiting_at = jiffies;
911a5a488dbSRussell King 
9122ff25c1cSJean-Nicolas Graux 	/*
9132ff25c1cSJean-Nicolas Graux 	 * Find a waiting virtual channel for the next transfer.
9142ff25c1cSJean-Nicolas Graux 	 * To be fair, time when each channel reached waiting state is compared
9152ff25c1cSJean-Nicolas Graux 	 * to select channel that is waiting for the longest time.
9162ff25c1cSJean-Nicolas Graux 	 */
91701d8dc64SRussell King 	list_for_each_entry(p, &pl08x->memcpy.channels, vc.chan.device_node)
9182ff25c1cSJean-Nicolas Graux 		if (p->state == PL08X_CHAN_WAITING &&
9192ff25c1cSJean-Nicolas Graux 		    p->waiting_at <= waiting_at) {
920a5a488dbSRussell King 			next = p;
9212ff25c1cSJean-Nicolas Graux 			waiting_at = p->waiting_at;
922a5a488dbSRussell King 		}
923a5a488dbSRussell King 
924ebe9b300SLinus Walleij 	if (!next && pl08x->has_slave) {
92501d8dc64SRussell King 		list_for_each_entry(p, &pl08x->slave.channels, vc.chan.device_node)
9262ff25c1cSJean-Nicolas Graux 			if (p->state == PL08X_CHAN_WAITING &&
9272ff25c1cSJean-Nicolas Graux 			    p->waiting_at <= waiting_at) {
928a5a488dbSRussell King 				next = p;
9292ff25c1cSJean-Nicolas Graux 				waiting_at = p->waiting_at;
930a5a488dbSRussell King 			}
931a5a488dbSRussell King 	}
932a5a488dbSRussell King 
933a5a488dbSRussell King 	/* Ensure that the physical channel is stopped */
934a5a488dbSRussell King 	pl08x_terminate_phy_chan(pl08x, plchan->phychan);
935a5a488dbSRussell King 
936a5a488dbSRussell King 	if (next) {
937a5a488dbSRussell King 		bool success;
938a5a488dbSRussell King 
939a5a488dbSRussell King 		/*
940a5a488dbSRussell King 		 * Eww.  We know this isn't going to deadlock
941a5a488dbSRussell King 		 * but lockdep probably doesn't.
942a5a488dbSRussell King 		 */
943083be28aSRussell King 		spin_lock(&next->vc.lock);
944a5a488dbSRussell King 		/* Re-check the state now that we have the lock */
945a5a488dbSRussell King 		success = next->state == PL08X_CHAN_WAITING;
946a5a488dbSRussell King 		if (success)
947a5a488dbSRussell King 			pl08x_phy_reassign_start(plchan->phychan, next);
948083be28aSRussell King 		spin_unlock(&next->vc.lock);
949a5a488dbSRussell King 
950a5a488dbSRussell King 		/* If the state changed, try to find another channel */
951a5a488dbSRussell King 		if (!success)
952a5a488dbSRussell King 			goto retry;
953a5a488dbSRussell King 	} else {
954a5a488dbSRussell King 		/* No more jobs, so free up the physical channel */
955a5a488dbSRussell King 		pl08x_put_phy_channel(pl08x, plchan->phychan);
956a5a488dbSRussell King 	}
957a5a488dbSRussell King 
958a5a488dbSRussell King 	plchan->phychan = NULL;
959a5a488dbSRussell King 	plchan->state = PL08X_CHAN_IDLE;
960e8689e63SLinus Walleij }
961e8689e63SLinus Walleij 
962e8689e63SLinus Walleij /*
963e8689e63SLinus Walleij  * LLI handling
964e8689e63SLinus Walleij  */
965e8689e63SLinus Walleij 
9661e1cfc72SLinus Walleij static inline unsigned int
pl08x_get_bytes_for_lli(struct pl08x_driver_data * pl08x,u32 cctl,bool source)9671e1cfc72SLinus Walleij pl08x_get_bytes_for_lli(struct pl08x_driver_data *pl08x,
9681e1cfc72SLinus Walleij 			u32 cctl,
9691e1cfc72SLinus Walleij 			bool source)
970e8689e63SLinus Walleij {
9711e1cfc72SLinus Walleij 	u32 val;
9721e1cfc72SLinus Walleij 
9731e1cfc72SLinus Walleij 	if (pl08x->vd->ftdmac020) {
9741e1cfc72SLinus Walleij 		if (source)
9751e1cfc72SLinus Walleij 			val = (cctl & FTDMAC020_LLI_SRC_WIDTH_MSK) >>
9761e1cfc72SLinus Walleij 				FTDMAC020_LLI_SRC_WIDTH_SHIFT;
9771e1cfc72SLinus Walleij 		else
9781e1cfc72SLinus Walleij 			val = (cctl & FTDMAC020_LLI_DST_WIDTH_MSK) >>
9791e1cfc72SLinus Walleij 				FTDMAC020_LLI_DST_WIDTH_SHIFT;
9801e1cfc72SLinus Walleij 	} else {
9811e1cfc72SLinus Walleij 		if (source)
9821e1cfc72SLinus Walleij 			val = (cctl & PL080_CONTROL_SWIDTH_MASK) >>
9831e1cfc72SLinus Walleij 				PL080_CONTROL_SWIDTH_SHIFT;
9841e1cfc72SLinus Walleij 		else
9851e1cfc72SLinus Walleij 			val = (cctl & PL080_CONTROL_DWIDTH_MASK) >>
9861e1cfc72SLinus Walleij 				PL080_CONTROL_DWIDTH_SHIFT;
9871e1cfc72SLinus Walleij 	}
9881e1cfc72SLinus Walleij 
9891e1cfc72SLinus Walleij 	switch (val) {
990e8689e63SLinus Walleij 	case PL080_WIDTH_8BIT:
991e8689e63SLinus Walleij 		return 1;
992e8689e63SLinus Walleij 	case PL080_WIDTH_16BIT:
993e8689e63SLinus Walleij 		return 2;
994e8689e63SLinus Walleij 	case PL080_WIDTH_32BIT:
995e8689e63SLinus Walleij 		return 4;
996e8689e63SLinus Walleij 	default:
997e8689e63SLinus Walleij 		break;
998e8689e63SLinus Walleij 	}
999e8689e63SLinus Walleij 	BUG();
1000e8689e63SLinus Walleij 	return 0;
1001e8689e63SLinus Walleij }
1002e8689e63SLinus Walleij 
pl08x_lli_control_bits(struct pl08x_driver_data * pl08x,u32 cctl,u8 srcwidth,u8 dstwidth,size_t tsize)10031e1cfc72SLinus Walleij static inline u32 pl08x_lli_control_bits(struct pl08x_driver_data *pl08x,
10041e1cfc72SLinus Walleij 					 u32 cctl,
10051e1cfc72SLinus Walleij 					 u8 srcwidth, u8 dstwidth,
1006cace6585SRussell King - ARM Linux 					 size_t tsize)
1007e8689e63SLinus Walleij {
1008e8689e63SLinus Walleij 	u32 retbits = cctl;
1009e8689e63SLinus Walleij 
10101e1cfc72SLinus Walleij 	/*
10111e1cfc72SLinus Walleij 	 * Remove all src, dst and transfer size bits, then set the
10121e1cfc72SLinus Walleij 	 * width and size according to the parameters. The bit offsets
10131e1cfc72SLinus Walleij 	 * are different in the FTDMAC020 so we need to accound for this.
10141e1cfc72SLinus Walleij 	 */
10151e1cfc72SLinus Walleij 	if (pl08x->vd->ftdmac020) {
10161e1cfc72SLinus Walleij 		retbits &= ~FTDMAC020_LLI_DST_WIDTH_MSK;
10171e1cfc72SLinus Walleij 		retbits &= ~FTDMAC020_LLI_SRC_WIDTH_MSK;
10181e1cfc72SLinus Walleij 		retbits &= ~FTDMAC020_LLI_TRANSFER_SIZE_MASK;
1019e8689e63SLinus Walleij 
1020e8689e63SLinus Walleij 		switch (srcwidth) {
1021e8689e63SLinus Walleij 		case 1:
10221e1cfc72SLinus Walleij 			retbits |= PL080_WIDTH_8BIT <<
10231e1cfc72SLinus Walleij 				FTDMAC020_LLI_SRC_WIDTH_SHIFT;
1024e8689e63SLinus Walleij 			break;
1025e8689e63SLinus Walleij 		case 2:
10261e1cfc72SLinus Walleij 			retbits |= PL080_WIDTH_16BIT <<
10271e1cfc72SLinus Walleij 				FTDMAC020_LLI_SRC_WIDTH_SHIFT;
1028e8689e63SLinus Walleij 			break;
1029e8689e63SLinus Walleij 		case 4:
10301e1cfc72SLinus Walleij 			retbits |= PL080_WIDTH_32BIT <<
10311e1cfc72SLinus Walleij 				FTDMAC020_LLI_SRC_WIDTH_SHIFT;
1032e8689e63SLinus Walleij 			break;
1033e8689e63SLinus Walleij 		default:
1034e8689e63SLinus Walleij 			BUG();
1035e8689e63SLinus Walleij 			break;
1036e8689e63SLinus Walleij 		}
1037e8689e63SLinus Walleij 
1038e8689e63SLinus Walleij 		switch (dstwidth) {
1039e8689e63SLinus Walleij 		case 1:
10401e1cfc72SLinus Walleij 			retbits |= PL080_WIDTH_8BIT <<
10411e1cfc72SLinus Walleij 				FTDMAC020_LLI_DST_WIDTH_SHIFT;
1042e8689e63SLinus Walleij 			break;
1043e8689e63SLinus Walleij 		case 2:
10441e1cfc72SLinus Walleij 			retbits |= PL080_WIDTH_16BIT <<
10451e1cfc72SLinus Walleij 				FTDMAC020_LLI_DST_WIDTH_SHIFT;
1046e8689e63SLinus Walleij 			break;
1047e8689e63SLinus Walleij 		case 4:
10481e1cfc72SLinus Walleij 			retbits |= PL080_WIDTH_32BIT <<
10491e1cfc72SLinus Walleij 				FTDMAC020_LLI_DST_WIDTH_SHIFT;
10501e1cfc72SLinus Walleij 			break;
10511e1cfc72SLinus Walleij 		default:
10521e1cfc72SLinus Walleij 			BUG();
10531e1cfc72SLinus Walleij 			break;
10541e1cfc72SLinus Walleij 		}
10551e1cfc72SLinus Walleij 
10561e1cfc72SLinus Walleij 		tsize &= FTDMAC020_LLI_TRANSFER_SIZE_MASK;
10571e1cfc72SLinus Walleij 		retbits |= tsize << FTDMAC020_LLI_TRANSFER_SIZE_SHIFT;
10581e1cfc72SLinus Walleij 	} else {
10591e1cfc72SLinus Walleij 		retbits &= ~PL080_CONTROL_DWIDTH_MASK;
10601e1cfc72SLinus Walleij 		retbits &= ~PL080_CONTROL_SWIDTH_MASK;
10611e1cfc72SLinus Walleij 		retbits &= ~PL080_CONTROL_TRANSFER_SIZE_MASK;
10621e1cfc72SLinus Walleij 
10631e1cfc72SLinus Walleij 		switch (srcwidth) {
10641e1cfc72SLinus Walleij 		case 1:
10651e1cfc72SLinus Walleij 			retbits |= PL080_WIDTH_8BIT <<
10661e1cfc72SLinus Walleij 				PL080_CONTROL_SWIDTH_SHIFT;
10671e1cfc72SLinus Walleij 			break;
10681e1cfc72SLinus Walleij 		case 2:
10691e1cfc72SLinus Walleij 			retbits |= PL080_WIDTH_16BIT <<
10701e1cfc72SLinus Walleij 				PL080_CONTROL_SWIDTH_SHIFT;
10711e1cfc72SLinus Walleij 			break;
10721e1cfc72SLinus Walleij 		case 4:
10731e1cfc72SLinus Walleij 			retbits |= PL080_WIDTH_32BIT <<
10741e1cfc72SLinus Walleij 				PL080_CONTROL_SWIDTH_SHIFT;
10751e1cfc72SLinus Walleij 			break;
10761e1cfc72SLinus Walleij 		default:
10771e1cfc72SLinus Walleij 			BUG();
10781e1cfc72SLinus Walleij 			break;
10791e1cfc72SLinus Walleij 		}
10801e1cfc72SLinus Walleij 
10811e1cfc72SLinus Walleij 		switch (dstwidth) {
10821e1cfc72SLinus Walleij 		case 1:
10831e1cfc72SLinus Walleij 			retbits |= PL080_WIDTH_8BIT <<
10841e1cfc72SLinus Walleij 				PL080_CONTROL_DWIDTH_SHIFT;
10851e1cfc72SLinus Walleij 			break;
10861e1cfc72SLinus Walleij 		case 2:
10871e1cfc72SLinus Walleij 			retbits |= PL080_WIDTH_16BIT <<
10881e1cfc72SLinus Walleij 				PL080_CONTROL_DWIDTH_SHIFT;
10891e1cfc72SLinus Walleij 			break;
10901e1cfc72SLinus Walleij 		case 4:
10911e1cfc72SLinus Walleij 			retbits |= PL080_WIDTH_32BIT <<
10921e1cfc72SLinus Walleij 				PL080_CONTROL_DWIDTH_SHIFT;
1093e8689e63SLinus Walleij 			break;
1094e8689e63SLinus Walleij 		default:
1095e8689e63SLinus Walleij 			BUG();
1096e8689e63SLinus Walleij 			break;
1097e8689e63SLinus Walleij 		}
1098e8689e63SLinus Walleij 
10995110e51dSTomasz Figa 		tsize &= PL080_CONTROL_TRANSFER_SIZE_MASK;
1100e8689e63SLinus Walleij 		retbits |= tsize << PL080_CONTROL_TRANSFER_SIZE_SHIFT;
11011e1cfc72SLinus Walleij 	}
11021e1cfc72SLinus Walleij 
1103e8689e63SLinus Walleij 	return retbits;
1104e8689e63SLinus Walleij }
1105e8689e63SLinus Walleij 
1106542361f8SRussell King - ARM Linux struct pl08x_lli_build_data {
1107542361f8SRussell King - ARM Linux 	struct pl08x_txd *txd;
1108542361f8SRussell King - ARM Linux 	struct pl08x_bus_data srcbus;
1109542361f8SRussell King - ARM Linux 	struct pl08x_bus_data dstbus;
1110542361f8SRussell King - ARM Linux 	size_t remainder;
111125c94f7fSRussell King - ARM Linux 	u32 lli_bus;
1112542361f8SRussell King - ARM Linux };
1113542361f8SRussell King - ARM Linux 
1114e8689e63SLinus Walleij /*
11150532e6fcSViresh Kumar  * Autoselect a master bus to use for the transfer. Slave will be the chosen as
11160532e6fcSViresh Kumar  * victim in case src & dest are not similarly aligned. i.e. If after aligning
11170532e6fcSViresh Kumar  * masters address with width requirements of transfer (by sending few byte by
11180532e6fcSViresh Kumar  * byte data), slave is still not aligned, then its width will be reduced to
11190532e6fcSViresh Kumar  * BYTE.
11200532e6fcSViresh Kumar  * - prefers the destination bus if both available
1121036f05fdSViresh Kumar  * - prefers bus with fixed address (i.e. peripheral)
1122e8689e63SLinus Walleij  */
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)11231e1cfc72SLinus Walleij static void pl08x_choose_master_bus(struct pl08x_driver_data *pl08x,
11241e1cfc72SLinus Walleij 				    struct pl08x_lli_build_data *bd,
11251e1cfc72SLinus Walleij 				    struct pl08x_bus_data **mbus,
11261e1cfc72SLinus Walleij 				    struct pl08x_bus_data **sbus,
11271e1cfc72SLinus Walleij 				    u32 cctl)
1128e8689e63SLinus Walleij {
11291e1cfc72SLinus Walleij 	bool dst_incr;
11301e1cfc72SLinus Walleij 	bool src_incr;
11311e1cfc72SLinus Walleij 
11321e1cfc72SLinus Walleij 	/*
11331e1cfc72SLinus Walleij 	 * The FTDMAC020 only supports memory-to-memory transfer, so
11341e1cfc72SLinus Walleij 	 * source and destination always increase.
11351e1cfc72SLinus Walleij 	 */
11361e1cfc72SLinus Walleij 	if (pl08x->vd->ftdmac020) {
11371e1cfc72SLinus Walleij 		dst_incr = true;
11381e1cfc72SLinus Walleij 		src_incr = true;
11391e1cfc72SLinus Walleij 	} else {
11401e1cfc72SLinus Walleij 		dst_incr = !!(cctl & PL080_CONTROL_DST_INCR);
11411e1cfc72SLinus Walleij 		src_incr = !!(cctl & PL080_CONTROL_SRC_INCR);
11421e1cfc72SLinus Walleij 	}
11431e1cfc72SLinus Walleij 
11441e1cfc72SLinus Walleij 	/*
11451e1cfc72SLinus Walleij 	 * If either bus is not advancing, i.e. it is a peripheral, that
11461e1cfc72SLinus Walleij 	 * one becomes master
11471e1cfc72SLinus Walleij 	 */
11481e1cfc72SLinus Walleij 	if (!dst_incr) {
1149036f05fdSViresh Kumar 		*mbus = &bd->dstbus;
1150036f05fdSViresh Kumar 		*sbus = &bd->srcbus;
11511e1cfc72SLinus Walleij 	} else if (!src_incr) {
1152542361f8SRussell King - ARM Linux 		*mbus = &bd->srcbus;
1153542361f8SRussell King - ARM Linux 		*sbus = &bd->dstbus;
1154e8689e63SLinus Walleij 	} else {
1155036f05fdSViresh Kumar 		if (bd->dstbus.buswidth >= bd->srcbus.buswidth) {
1156542361f8SRussell King - ARM Linux 			*mbus = &bd->dstbus;
1157542361f8SRussell King - ARM Linux 			*sbus = &bd->srcbus;
1158036f05fdSViresh Kumar 		} else {
1159036f05fdSViresh Kumar 			*mbus = &bd->srcbus;
1160036f05fdSViresh Kumar 			*sbus = &bd->dstbus;
1161e8689e63SLinus Walleij 		}
1162e8689e63SLinus Walleij 	}
1163e8689e63SLinus Walleij }
1164e8689e63SLinus Walleij 
1165e8689e63SLinus Walleij /*
116694ae8522SRussell King - ARM Linux  * Fills in one LLI for a certain transfer descriptor and advance the counter
1167e8689e63SLinus Walleij  */
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)1168ba6785ffSTomasz Figa static void pl08x_fill_lli_for_desc(struct pl08x_driver_data *pl08x,
1169ba6785ffSTomasz Figa 				    struct pl08x_lli_build_data *bd,
1170da1b6c05STomasz Figa 				    int num_llis, int len, u32 cctl, u32 cctl2)
1171e8689e63SLinus Walleij {
1172ba6785ffSTomasz Figa 	u32 offset = num_llis * pl08x->lli_words;
1173ba6785ffSTomasz Figa 	u32 *llis_va = bd->txd->llis_va + offset;
1174542361f8SRussell King - ARM Linux 	dma_addr_t llis_bus = bd->txd->llis_bus;
1175e8689e63SLinus Walleij 
1176e8689e63SLinus Walleij 	BUG_ON(num_llis >= MAX_NUM_TSFR_LLIS);
1177e8689e63SLinus Walleij 
1178ba6785ffSTomasz Figa 	/* Advance the offset to next LLI. */
1179ba6785ffSTomasz Figa 	offset += pl08x->lli_words;
1180ba6785ffSTomasz Figa 
1181ba6785ffSTomasz Figa 	llis_va[PL080_LLI_SRC] = bd->srcbus.addr;
1182ba6785ffSTomasz Figa 	llis_va[PL080_LLI_DST] = bd->dstbus.addr;
1183ba6785ffSTomasz Figa 	llis_va[PL080_LLI_LLI] = (llis_bus + sizeof(u32) * offset);
1184ba6785ffSTomasz Figa 	llis_va[PL080_LLI_LLI] |= bd->lli_bus;
1185ba6785ffSTomasz Figa 	llis_va[PL080_LLI_CCTL] = cctl;
1186da1b6c05STomasz Figa 	if (pl08x->vd->pl080s)
1187da1b6c05STomasz Figa 		llis_va[PL080S_LLI_CCTL2] = cctl2;
1188e8689e63SLinus Walleij 
11891e1cfc72SLinus Walleij 	if (pl08x->vd->ftdmac020) {
11901e1cfc72SLinus Walleij 		/* FIXME: only memcpy so far so both increase */
11911e1cfc72SLinus Walleij 		bd->srcbus.addr += len;
11921e1cfc72SLinus Walleij 		bd->dstbus.addr += len;
11931e1cfc72SLinus Walleij 	} else {
1194e8689e63SLinus Walleij 		if (cctl & PL080_CONTROL_SRC_INCR)
1195542361f8SRussell King - ARM Linux 			bd->srcbus.addr += len;
1196e8689e63SLinus Walleij 		if (cctl & PL080_CONTROL_DST_INCR)
1197542361f8SRussell King - ARM Linux 			bd->dstbus.addr += len;
11981e1cfc72SLinus Walleij 	}
1199e8689e63SLinus Walleij 
1200542361f8SRussell King - ARM Linux 	BUG_ON(bd->remainder < len);
1201cace6585SRussell King - ARM Linux 
1202542361f8SRussell King - ARM Linux 	bd->remainder -= len;
1203e8689e63SLinus Walleij }
1204e8689e63SLinus Walleij 
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)1205ba6785ffSTomasz Figa static inline void prep_byte_width_lli(struct pl08x_driver_data *pl08x,
1206ba6785ffSTomasz Figa 			struct pl08x_lli_build_data *bd, u32 *cctl, u32 len,
1207ba6785ffSTomasz Figa 			int num_llis, size_t *total_bytes)
1208e8689e63SLinus Walleij {
12091e1cfc72SLinus Walleij 	*cctl = pl08x_lli_control_bits(pl08x, *cctl, 1, 1, len);
1210da1b6c05STomasz Figa 	pl08x_fill_lli_for_desc(pl08x, bd, num_llis, len, *cctl, len);
121103af500fSViresh Kumar 	(*total_bytes) += len;
1212e8689e63SLinus Walleij }
1213e8689e63SLinus Walleij 
12141e1cfc72SLinus Walleij #if 1
pl08x_dump_lli(struct pl08x_driver_data * pl08x,const u32 * llis_va,int num_llis)121548924e42STomasz Figa static void pl08x_dump_lli(struct pl08x_driver_data *pl08x,
121648924e42STomasz Figa 			   const u32 *llis_va, int num_llis)
121748924e42STomasz Figa {
121848924e42STomasz Figa 	int i;
121948924e42STomasz Figa 
1220da1b6c05STomasz Figa 	if (pl08x->vd->pl080s) {
1221da1b6c05STomasz Figa 		dev_vdbg(&pl08x->adev->dev,
1222da1b6c05STomasz Figa 			"%-3s %-9s  %-10s %-10s %-10s %-10s %s\n",
1223da1b6c05STomasz Figa 			"lli", "", "csrc", "cdst", "clli", "cctl", "cctl2");
1224da1b6c05STomasz Figa 		for (i = 0; i < num_llis; i++) {
1225da1b6c05STomasz Figa 			dev_vdbg(&pl08x->adev->dev,
1226da1b6c05STomasz Figa 				"%3d @%p: 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
1227da1b6c05STomasz Figa 				i, llis_va, llis_va[PL080_LLI_SRC],
1228da1b6c05STomasz Figa 				llis_va[PL080_LLI_DST], llis_va[PL080_LLI_LLI],
1229da1b6c05STomasz Figa 				llis_va[PL080_LLI_CCTL],
1230da1b6c05STomasz Figa 				llis_va[PL080S_LLI_CCTL2]);
1231da1b6c05STomasz Figa 			llis_va += pl08x->lli_words;
1232da1b6c05STomasz Figa 		}
1233da1b6c05STomasz Figa 	} else {
123448924e42STomasz Figa 		dev_vdbg(&pl08x->adev->dev,
123548924e42STomasz Figa 			"%-3s %-9s  %-10s %-10s %-10s %s\n",
123648924e42STomasz Figa 			"lli", "", "csrc", "cdst", "clli", "cctl");
123748924e42STomasz Figa 		for (i = 0; i < num_llis; i++) {
123848924e42STomasz Figa 			dev_vdbg(&pl08x->adev->dev,
123948924e42STomasz Figa 				"%3d @%p: 0x%08x 0x%08x 0x%08x 0x%08x\n",
124048924e42STomasz Figa 				i, llis_va, llis_va[PL080_LLI_SRC],
124148924e42STomasz Figa 				llis_va[PL080_LLI_DST], llis_va[PL080_LLI_LLI],
124248924e42STomasz Figa 				llis_va[PL080_LLI_CCTL]);
124348924e42STomasz Figa 			llis_va += pl08x->lli_words;
124448924e42STomasz Figa 		}
124548924e42STomasz Figa 	}
1246da1b6c05STomasz Figa }
124748924e42STomasz Figa #else
pl08x_dump_lli(struct pl08x_driver_data * pl08x,const u32 * llis_va,int num_llis)124848924e42STomasz Figa static inline void pl08x_dump_lli(struct pl08x_driver_data *pl08x,
124948924e42STomasz Figa 				  const u32 *llis_va, int num_llis) {}
125048924e42STomasz Figa #endif
125148924e42STomasz Figa 
1252e8689e63SLinus Walleij /*
1253e8689e63SLinus Walleij  * This fills in the table of LLIs for the transfer descriptor
1254e8689e63SLinus Walleij  * Note that we assume we never have to change the burst sizes
1255e8689e63SLinus Walleij  * Return 0 for error
1256e8689e63SLinus Walleij  */
pl08x_fill_llis_for_desc(struct pl08x_driver_data * pl08x,struct pl08x_txd * txd)1257e8689e63SLinus Walleij static int pl08x_fill_llis_for_desc(struct pl08x_driver_data *pl08x,
1258e8689e63SLinus Walleij 			      struct pl08x_txd *txd)
1259e8689e63SLinus Walleij {
1260e8689e63SLinus Walleij 	struct pl08x_bus_data *mbus, *sbus;
1261542361f8SRussell King - ARM Linux 	struct pl08x_lli_build_data bd;
1262e8689e63SLinus Walleij 	int num_llis = 0;
126303af500fSViresh Kumar 	u32 cctl, early_bytes = 0;
1264b7f69d9dSViresh Kumar 	size_t max_bytes_per_lli, total_bytes;
1265ba6785ffSTomasz Figa 	u32 *llis_va, *last_lli;
1266b7f69d9dSViresh Kumar 	struct pl08x_sg *dsg;
1267e8689e63SLinus Walleij 
12683e27ee84SViresh Kumar 	txd->llis_va = dma_pool_alloc(pl08x->pool, GFP_NOWAIT, &txd->llis_bus);
1269e8689e63SLinus Walleij 	if (!txd->llis_va) {
1270e8689e63SLinus Walleij 		dev_err(&pl08x->adev->dev, "%s no memory for llis\n", __func__);
1271e8689e63SLinus Walleij 		return 0;
1272e8689e63SLinus Walleij 	}
1273e8689e63SLinus Walleij 
1274542361f8SRussell King - ARM Linux 	bd.txd = txd;
127525c94f7fSRussell King - ARM Linux 	bd.lli_bus = (pl08x->lli_buses & PL08X_AHB2) ? PL080_LLI_LM_AHB2 : 0;
1276b7f69d9dSViresh Kumar 	cctl = txd->cctl;
1277542361f8SRussell King - ARM Linux 
1278e8689e63SLinus Walleij 	/* Find maximum width of the source bus */
12791e1cfc72SLinus Walleij 	bd.srcbus.maxwidth = pl08x_get_bytes_for_lli(pl08x, cctl, true);
1280e8689e63SLinus Walleij 
1281e8689e63SLinus Walleij 	/* Find maximum width of the destination bus */
12821e1cfc72SLinus Walleij 	bd.dstbus.maxwidth = pl08x_get_bytes_for_lli(pl08x, cctl, false);
1283e8689e63SLinus Walleij 
1284b7f69d9dSViresh Kumar 	list_for_each_entry(dsg, &txd->dsg_list, node) {
1285b7f69d9dSViresh Kumar 		total_bytes = 0;
1286b7f69d9dSViresh Kumar 		cctl = txd->cctl;
1287b7f69d9dSViresh Kumar 
1288b7f69d9dSViresh Kumar 		bd.srcbus.addr = dsg->src_addr;
1289b7f69d9dSViresh Kumar 		bd.dstbus.addr = dsg->dst_addr;
1290b7f69d9dSViresh Kumar 		bd.remainder = dsg->len;
1291542361f8SRussell King - ARM Linux 		bd.srcbus.buswidth = bd.srcbus.maxwidth;
1292542361f8SRussell King - ARM Linux 		bd.dstbus.buswidth = bd.dstbus.maxwidth;
1293e8689e63SLinus Walleij 
12941e1cfc72SLinus Walleij 		pl08x_choose_master_bus(pl08x, &bd, &mbus, &sbus, cctl);
1295e8689e63SLinus Walleij 
1296b90ca063SAndre Przywara 		dev_vdbg(&pl08x->adev->dev,
1297b90ca063SAndre Przywara 			"src=0x%08llx%s/%u dst=0x%08llx%s/%u len=%zu\n",
1298b90ca063SAndre Przywara 			(u64)bd.srcbus.addr,
1299b90ca063SAndre Przywara 			cctl & PL080_CONTROL_SRC_INCR ? "+" : "",
1300fc74eb79SRussell King - ARM Linux 			bd.srcbus.buswidth,
1301b90ca063SAndre Przywara 			(u64)bd.dstbus.addr,
1302b90ca063SAndre Przywara 			cctl & PL080_CONTROL_DST_INCR ? "+" : "",
1303fc74eb79SRussell King - ARM Linux 			bd.dstbus.buswidth,
1304fa6a940bSViresh Kumar 			bd.remainder);
1305fc74eb79SRussell King - ARM Linux 		dev_vdbg(&pl08x->adev->dev, "mbus=%s sbus=%s\n",
1306fc74eb79SRussell King - ARM Linux 			mbus == &bd.srcbus ? "src" : "dst",
1307fc74eb79SRussell King - ARM Linux 			sbus == &bd.srcbus ? "src" : "dst");
1308fc74eb79SRussell King - ARM Linux 
130903af500fSViresh Kumar 		/*
1310b7f69d9dSViresh Kumar 		 * Zero length is only allowed if all these requirements are
1311b7f69d9dSViresh Kumar 		 * met:
13120a235657SViresh Kumar 		 * - flow controller is peripheral.
13130a235657SViresh Kumar 		 * - src.addr is aligned to src.width
13140a235657SViresh Kumar 		 * - dst.addr is aligned to dst.width
13150a235657SViresh Kumar 		 *
13160a235657SViresh Kumar 		 * sg_len == 1 should be true, as there can be two cases here:
13170a235657SViresh Kumar 		 *
1318b7f69d9dSViresh Kumar 		 * - Memory addresses are contiguous and are not scattered.
1319b7f69d9dSViresh Kumar 		 *   Here, Only one sg will be passed by user driver, with
1320b7f69d9dSViresh Kumar 		 *   memory address and zero length. We pass this to controller
1321b7f69d9dSViresh Kumar 		 *   and after the transfer it will receive the last burst
1322b7f69d9dSViresh Kumar 		 *   request from peripheral and so transfer finishes.
1323b7f69d9dSViresh Kumar 		 *
1324b7f69d9dSViresh Kumar 		 * - Memory addresses are scattered and are not contiguous.
1325b7f69d9dSViresh Kumar 		 *   Here, Obviously as DMA controller doesn't know when a lli's
1326b7f69d9dSViresh Kumar 		 *   transfer gets over, it can't load next lli. So in this
1327b7f69d9dSViresh Kumar 		 *   case, there has to be an assumption that only one lli is
1328b7f69d9dSViresh Kumar 		 *   supported. Thus, we can't have scattered addresses.
13290a235657SViresh Kumar 		 */
13300a235657SViresh Kumar 		if (!bd.remainder) {
13311e1cfc72SLinus Walleij 			u32 fc;
13321e1cfc72SLinus Walleij 
13331e1cfc72SLinus Walleij 			/* FTDMAC020 only does memory-to-memory */
13341e1cfc72SLinus Walleij 			if (pl08x->vd->ftdmac020)
13351e1cfc72SLinus Walleij 				fc = PL080_FLOW_MEM2MEM;
13361e1cfc72SLinus Walleij 			else
13371e1cfc72SLinus Walleij 				fc = (txd->ccfg & PL080_CONFIG_FLOW_CONTROL_MASK) >>
13380a235657SViresh Kumar 					PL080_CONFIG_FLOW_CONTROL_SHIFT;
13390a235657SViresh Kumar 			if (!((fc >= PL080_FLOW_SRC2DST_DST) &&
13400a235657SViresh Kumar 					(fc <= PL080_FLOW_SRC2DST_SRC))) {
13410a235657SViresh Kumar 				dev_err(&pl08x->adev->dev, "%s sg len can't be zero",
13420a235657SViresh Kumar 					__func__);
13430a235657SViresh Kumar 				return 0;
1344e8689e63SLinus Walleij 			}
1345e8689e63SLinus Walleij 
13461c38b289SAndre Przywara 			if (!IS_BUS_ALIGNED(&bd.srcbus) ||
13471c38b289SAndre Przywara 				!IS_BUS_ALIGNED(&bd.dstbus)) {
13480a235657SViresh Kumar 				dev_err(&pl08x->adev->dev,
13490a235657SViresh Kumar 					"%s src & dst address must be aligned to src"
13500a235657SViresh Kumar 					" & dst width if peripheral is flow controller",
13510a235657SViresh Kumar 					__func__);
13520a235657SViresh Kumar 				return 0;
13530a235657SViresh Kumar 			}
13540a235657SViresh Kumar 
13551e1cfc72SLinus Walleij 			cctl = pl08x_lli_control_bits(pl08x, cctl,
13561e1cfc72SLinus Walleij 					bd.srcbus.buswidth, bd.dstbus.buswidth,
13571e1cfc72SLinus Walleij 					0);
1358ba6785ffSTomasz Figa 			pl08x_fill_lli_for_desc(pl08x, &bd, num_llis++,
1359da1b6c05STomasz Figa 					0, cctl, 0);
1360b7f69d9dSViresh Kumar 			break;
13610a235657SViresh Kumar 		}
13620a235657SViresh Kumar 
13630a235657SViresh Kumar 		/*
136403af500fSViresh Kumar 		 * Send byte by byte for following cases
136503af500fSViresh Kumar 		 * - Less than a bus width available
136603af500fSViresh Kumar 		 * - until master bus is aligned
136703af500fSViresh Kumar 		 */
136803af500fSViresh Kumar 		if (bd.remainder < mbus->buswidth)
136903af500fSViresh Kumar 			early_bytes = bd.remainder;
13701c38b289SAndre Przywara 		else if (!IS_BUS_ALIGNED(mbus)) {
13711c38b289SAndre Przywara 			early_bytes = mbus->buswidth -
13721c38b289SAndre Przywara 				(mbus->addr & (mbus->buswidth - 1));
137303af500fSViresh Kumar 			if ((bd.remainder - early_bytes) < mbus->buswidth)
137403af500fSViresh Kumar 				early_bytes = bd.remainder;
1375e8689e63SLinus Walleij 		}
1376e8689e63SLinus Walleij 
137703af500fSViresh Kumar 		if (early_bytes) {
1378b7f69d9dSViresh Kumar 			dev_vdbg(&pl08x->adev->dev,
13796fc8ae78SMark Brown 				"%s byte width LLIs (remain 0x%08zx)\n",
1380b7f69d9dSViresh Kumar 				__func__, bd.remainder);
1381ba6785ffSTomasz Figa 			prep_byte_width_lli(pl08x, &bd, &cctl, early_bytes,
1382ba6785ffSTomasz Figa 				num_llis++, &total_bytes);
138303af500fSViresh Kumar 		}
138403af500fSViresh Kumar 
138503af500fSViresh Kumar 		if (bd.remainder) {
1386e8689e63SLinus Walleij 			/*
1387e8689e63SLinus Walleij 			 * Master now aligned
1388e8689e63SLinus Walleij 			 * - if slave is not then we must set its width down
1389e8689e63SLinus Walleij 			 */
13901c38b289SAndre Przywara 			if (!IS_BUS_ALIGNED(sbus)) {
1391e8689e63SLinus Walleij 				dev_dbg(&pl08x->adev->dev,
1392e8689e63SLinus Walleij 					"%s set down bus width to one byte\n",
1393e8689e63SLinus Walleij 					__func__);
1394e8689e63SLinus Walleij 
1395e8689e63SLinus Walleij 				sbus->buswidth = 1;
1396e8689e63SLinus Walleij 			}
1397e8689e63SLinus Walleij 
1398b7f69d9dSViresh Kumar 			/*
1399b7f69d9dSViresh Kumar 			 * Bytes transferred = tsize * src width, not
1400b7f69d9dSViresh Kumar 			 * MIN(buswidths)
1401b7f69d9dSViresh Kumar 			 */
1402fa6a940bSViresh Kumar 			max_bytes_per_lli = bd.srcbus.buswidth *
14035110e51dSTomasz Figa 						pl08x->vd->max_transfer_size;
1404b7f69d9dSViresh Kumar 			dev_vdbg(&pl08x->adev->dev,
1405b7f69d9dSViresh Kumar 				"%s max bytes per lli = %zu\n",
1406b7f69d9dSViresh Kumar 				__func__, max_bytes_per_lli);
1407fa6a940bSViresh Kumar 
1408e8689e63SLinus Walleij 			/*
1409e8689e63SLinus Walleij 			 * Make largest possible LLIs until less than one bus
1410e8689e63SLinus Walleij 			 * width left
1411e8689e63SLinus Walleij 			 */
1412542361f8SRussell King - ARM Linux 			while (bd.remainder > (mbus->buswidth - 1)) {
1413e0719165SViresh Kumar 				size_t lli_len, tsize, width;
1414e8689e63SLinus Walleij 
1415e8689e63SLinus Walleij 				/*
1416e8689e63SLinus Walleij 				 * If enough left try to send max possible,
1417e8689e63SLinus Walleij 				 * otherwise try to send the remainder
1418e8689e63SLinus Walleij 				 */
141916a2e7d3SViresh Kumar 				lli_len = min(bd.remainder, max_bytes_per_lli);
1420e8689e63SLinus Walleij 
1421e8689e63SLinus Walleij 				/*
1422b7f69d9dSViresh Kumar 				 * Check against maximum bus alignment:
1423b7f69d9dSViresh Kumar 				 * Calculate actual transfer size in relation to
1424b7f69d9dSViresh Kumar 				 * bus width an get a maximum remainder of the
1425b7f69d9dSViresh Kumar 				 * highest bus width - 1
1426e8689e63SLinus Walleij 				 */
1427e0719165SViresh Kumar 				width = max(mbus->buswidth, sbus->buswidth);
1428e0719165SViresh Kumar 				lli_len = (lli_len / width) * width;
1429e0719165SViresh Kumar 				tsize = lli_len / bd.srcbus.buswidth;
1430e8689e63SLinus Walleij 
1431e8689e63SLinus Walleij 				dev_vdbg(&pl08x->adev->dev,
143216a2e7d3SViresh Kumar 					"%s fill lli with single lli chunk of "
143316a2e7d3SViresh Kumar 					"size 0x%08zx (remainder 0x%08zx)\n",
1434542361f8SRussell King - ARM Linux 					__func__, lli_len, bd.remainder);
1435e8689e63SLinus Walleij 
14361e1cfc72SLinus Walleij 				cctl = pl08x_lli_control_bits(pl08x, cctl,
14371e1cfc72SLinus Walleij 					bd.srcbus.buswidth, bd.dstbus.buswidth,
14381e1cfc72SLinus Walleij 					tsize);
1439ba6785ffSTomasz Figa 				pl08x_fill_lli_for_desc(pl08x, &bd, num_llis++,
1440da1b6c05STomasz Figa 						lli_len, cctl, tsize);
1441e8689e63SLinus Walleij 				total_bytes += lli_len;
1442e8689e63SLinus Walleij 			}
1443e8689e63SLinus Walleij 
1444e8689e63SLinus Walleij 			/*
1445e8689e63SLinus Walleij 			 * Send any odd bytes
1446e8689e63SLinus Walleij 			 */
144703af500fSViresh Kumar 			if (bd.remainder) {
1448e8689e63SLinus Walleij 				dev_vdbg(&pl08x->adev->dev,
144903af500fSViresh Kumar 					"%s align with boundary, send odd bytes (remain %zu)\n",
1450542361f8SRussell King - ARM Linux 					__func__, bd.remainder);
1451ba6785ffSTomasz Figa 				prep_byte_width_lli(pl08x, &bd, &cctl,
1452ba6785ffSTomasz Figa 					bd.remainder, num_llis++, &total_bytes);
1453e8689e63SLinus Walleij 			}
1454e8689e63SLinus Walleij 		}
145516a2e7d3SViresh Kumar 
1456b7f69d9dSViresh Kumar 		if (total_bytes != dsg->len) {
1457e8689e63SLinus Walleij 			dev_err(&pl08x->adev->dev,
1458cace6585SRussell King - ARM Linux 				"%s size of encoded lli:s don't match total txd, transferred 0x%08zx from size 0x%08zx\n",
1459b7f69d9dSViresh Kumar 				__func__, total_bytes, dsg->len);
1460e8689e63SLinus Walleij 			return 0;
1461e8689e63SLinus Walleij 		}
1462e8689e63SLinus Walleij 
1463e8689e63SLinus Walleij 		if (num_llis >= MAX_NUM_TSFR_LLIS) {
1464e8689e63SLinus Walleij 			dev_err(&pl08x->adev->dev,
1465e8689e63SLinus Walleij 				"%s need to increase MAX_NUM_TSFR_LLIS from 0x%08x\n",
1466ba6785ffSTomasz Figa 				__func__, MAX_NUM_TSFR_LLIS);
1467e8689e63SLinus Walleij 			return 0;
1468e8689e63SLinus Walleij 		}
1469b7f69d9dSViresh Kumar 	}
1470e8689e63SLinus Walleij 
1471b58b6b5bSRussell King - ARM Linux 	llis_va = txd->llis_va;
1472ba6785ffSTomasz Figa 	last_lli = llis_va + (num_llis - 1) * pl08x->lli_words;
14733b24c20bSAlban Bedel 
14743b24c20bSAlban Bedel 	if (txd->cyclic) {
14753b24c20bSAlban Bedel 		/* Link back to the first LLI. */
14763b24c20bSAlban Bedel 		last_lli[PL080_LLI_LLI] = txd->llis_bus | bd.lli_bus;
14773b24c20bSAlban Bedel 	} else {
147894ae8522SRussell King - ARM Linux 		/* The final LLI terminates the LLI. */
1479ba6785ffSTomasz Figa 		last_lli[PL080_LLI_LLI] = 0;
148094ae8522SRussell King - ARM Linux 		/* The final LLI element shall also fire an interrupt. */
14811e1cfc72SLinus Walleij 		if (pl08x->vd->ftdmac020)
14821e1cfc72SLinus Walleij 			last_lli[PL080_LLI_CCTL] &= ~FTDMAC020_LLI_TC_MSK;
14831e1cfc72SLinus Walleij 		else
1484ba6785ffSTomasz Figa 			last_lli[PL080_LLI_CCTL] |= PL080_CONTROL_TC_IRQ_EN;
1485e8689e63SLinus Walleij 	}
1486e8689e63SLinus Walleij 
148748924e42STomasz Figa 	pl08x_dump_lli(pl08x, llis_va, num_llis);
1488e8689e63SLinus Walleij 
1489e8689e63SLinus Walleij 	return num_llis;
1490e8689e63SLinus Walleij }
1491e8689e63SLinus Walleij 
pl08x_free_txd(struct pl08x_driver_data * pl08x,struct pl08x_txd * txd)1492e8689e63SLinus Walleij static void pl08x_free_txd(struct pl08x_driver_data *pl08x,
1493e8689e63SLinus Walleij 			   struct pl08x_txd *txd)
1494e8689e63SLinus Walleij {
1495b7f69d9dSViresh Kumar 	struct pl08x_sg *dsg, *_dsg;
1496b7f69d9dSViresh Kumar 
1497c1205646SViresh Kumar 	if (txd->llis_va)
149856b61882SRussell King - ARM Linux 		dma_pool_free(pl08x->pool, txd->llis_va, txd->llis_bus);
1499e8689e63SLinus Walleij 
1500b7f69d9dSViresh Kumar 	list_for_each_entry_safe(dsg, _dsg, &txd->dsg_list, node) {
1501b7f69d9dSViresh Kumar 		list_del(&dsg->node);
1502b7f69d9dSViresh Kumar 		kfree(dsg);
1503b7f69d9dSViresh Kumar 	}
1504b7f69d9dSViresh Kumar 
1505e8689e63SLinus Walleij 	kfree(txd);
1506e8689e63SLinus Walleij }
1507e8689e63SLinus Walleij 
pl08x_desc_free(struct virt_dma_desc * vd)150818536134SRussell King static void pl08x_desc_free(struct virt_dma_desc *vd)
150918536134SRussell King {
151018536134SRussell King 	struct pl08x_txd *txd = to_pl08x_txd(&vd->tx);
151118536134SRussell King 	struct pl08x_dma_chan *plchan = to_pl08x_chan(vd->tx.chan);
151218536134SRussell King 
151389116bf9SRussell King - ARM Linux 	dma_descriptor_unmap(&vd->tx);
151418536134SRussell King 	if (!txd->done)
151518536134SRussell King 		pl08x_release_mux(plchan);
151618536134SRussell King 
151718536134SRussell King 	pl08x_free_txd(plchan->host, txd);
151818536134SRussell King }
151918536134SRussell King 
pl08x_free_txd_list(struct pl08x_driver_data * pl08x,struct pl08x_dma_chan * plchan)1520e8689e63SLinus Walleij static void pl08x_free_txd_list(struct pl08x_driver_data *pl08x,
1521e8689e63SLinus Walleij 				struct pl08x_dma_chan *plchan)
1522e8689e63SLinus Walleij {
1523ea160561SRussell King 	LIST_HEAD(head);
1524e8689e63SLinus Walleij 
1525879f127bSRussell King 	vchan_get_all_descriptors(&plchan->vc, &head);
152691998261SAkinobu Mita 	vchan_dma_desc_free_list(&plchan->vc, &head);
1527e8689e63SLinus Walleij }
1528e8689e63SLinus Walleij 
1529e8689e63SLinus Walleij /*
1530e8689e63SLinus Walleij  * The DMA ENGINE API
1531e8689e63SLinus Walleij  */
pl08x_free_chan_resources(struct dma_chan * chan)1532e8689e63SLinus Walleij static void pl08x_free_chan_resources(struct dma_chan *chan)
1533e8689e63SLinus Walleij {
1534a068682cSRussell King 	/* Ensure all queued descriptors are freed */
1535a068682cSRussell King 	vchan_free_chan_resources(to_virt_chan(chan));
1536e8689e63SLinus Walleij }
1537e8689e63SLinus Walleij 
1538e8689e63SLinus Walleij /*
153994ae8522SRussell King - ARM Linux  * Code accessing dma_async_is_complete() in a tight loop may give problems.
154094ae8522SRussell King - ARM Linux  * If slaves are relying on interrupts to signal completion this function
154194ae8522SRussell King - ARM Linux  * must not be called with interrupts disabled.
1542e8689e63SLinus Walleij  */
pl08x_dma_tx_status(struct dma_chan * chan,dma_cookie_t cookie,struct dma_tx_state * txstate)15433e27ee84SViresh Kumar static enum dma_status pl08x_dma_tx_status(struct dma_chan *chan,
15443e27ee84SViresh Kumar 		dma_cookie_t cookie, struct dma_tx_state *txstate)
1545e8689e63SLinus Walleij {
1546e8689e63SLinus Walleij 	struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
154706e885b7SRussell King 	struct virt_dma_desc *vd;
154806e885b7SRussell King 	unsigned long flags;
1549e8689e63SLinus Walleij 	enum dma_status ret;
155006e885b7SRussell King 	size_t bytes = 0;
1551e8689e63SLinus Walleij 
155296a2af41SRussell King - ARM Linux 	ret = dma_cookie_status(chan, cookie, txstate);
15530996e895SVinod Koul 	if (ret == DMA_COMPLETE)
1554e8689e63SLinus Walleij 		return ret;
1555e8689e63SLinus Walleij 
1556e8689e63SLinus Walleij 	/*
155706e885b7SRussell King 	 * There's no point calculating the residue if there's
155806e885b7SRussell King 	 * no txstate to store the value.
155906e885b7SRussell King 	 */
156006e885b7SRussell King 	if (!txstate) {
156106e885b7SRussell King 		if (plchan->state == PL08X_CHAN_PAUSED)
156206e885b7SRussell King 			ret = DMA_PAUSED;
156306e885b7SRussell King 		return ret;
156406e885b7SRussell King 	}
156506e885b7SRussell King 
156606e885b7SRussell King 	spin_lock_irqsave(&plchan->vc.lock, flags);
156706e885b7SRussell King 	ret = dma_cookie_status(chan, cookie, txstate);
15680996e895SVinod Koul 	if (ret != DMA_COMPLETE) {
156906e885b7SRussell King 		vd = vchan_find_desc(&plchan->vc, cookie);
157006e885b7SRussell King 		if (vd) {
157106e885b7SRussell King 			/* On the issued list, so hasn't been processed yet */
157206e885b7SRussell King 			struct pl08x_txd *txd = to_pl08x_txd(&vd->tx);
157306e885b7SRussell King 			struct pl08x_sg *dsg;
157406e885b7SRussell King 
157506e885b7SRussell King 			list_for_each_entry(dsg, &txd->dsg_list, node)
157606e885b7SRussell King 				bytes += dsg->len;
157706e885b7SRussell King 		} else {
157806e885b7SRussell King 			bytes = pl08x_getbytes_chan(plchan);
157906e885b7SRussell King 		}
158006e885b7SRussell King 	}
158106e885b7SRussell King 	spin_unlock_irqrestore(&plchan->vc.lock, flags);
158206e885b7SRussell King 
158306e885b7SRussell King 	/*
1584e8689e63SLinus Walleij 	 * This cookie not complete yet
158596a2af41SRussell King - ARM Linux 	 * Get number of bytes left in the active transactions and queue
1586e8689e63SLinus Walleij 	 */
158706e885b7SRussell King 	dma_set_residue(txstate, bytes);
1588e8689e63SLinus Walleij 
158906e885b7SRussell King 	if (plchan->state == PL08X_CHAN_PAUSED && ret == DMA_IN_PROGRESS)
159006e885b7SRussell King 		ret = DMA_PAUSED;
1591e8689e63SLinus Walleij 
1592e8689e63SLinus Walleij 	/* Whether waiting or running, we're in progress */
159306e885b7SRussell King 	return ret;
1594e8689e63SLinus Walleij }
1595e8689e63SLinus Walleij 
1596e8689e63SLinus Walleij /* PrimeCell DMA extension */
1597e8689e63SLinus Walleij struct burst_table {
1598760596c6SRussell King - ARM Linux 	u32 burstwords;
1599e8689e63SLinus Walleij 	u32 reg;
1600e8689e63SLinus Walleij };
1601e8689e63SLinus Walleij 
1602e8689e63SLinus Walleij static const struct burst_table burst_sizes[] = {
1603e8689e63SLinus Walleij 	{
1604e8689e63SLinus Walleij 		.burstwords = 256,
1605760596c6SRussell King - ARM Linux 		.reg = PL080_BSIZE_256,
1606e8689e63SLinus Walleij 	},
1607e8689e63SLinus Walleij 	{
1608e8689e63SLinus Walleij 		.burstwords = 128,
1609760596c6SRussell King - ARM Linux 		.reg = PL080_BSIZE_128,
1610e8689e63SLinus Walleij 	},
1611e8689e63SLinus Walleij 	{
1612e8689e63SLinus Walleij 		.burstwords = 64,
1613760596c6SRussell King - ARM Linux 		.reg = PL080_BSIZE_64,
1614e8689e63SLinus Walleij 	},
1615e8689e63SLinus Walleij 	{
1616e8689e63SLinus Walleij 		.burstwords = 32,
1617760596c6SRussell King - ARM Linux 		.reg = PL080_BSIZE_32,
1618e8689e63SLinus Walleij 	},
1619e8689e63SLinus Walleij 	{
1620e8689e63SLinus Walleij 		.burstwords = 16,
1621760596c6SRussell King - ARM Linux 		.reg = PL080_BSIZE_16,
1622e8689e63SLinus Walleij 	},
1623e8689e63SLinus Walleij 	{
1624e8689e63SLinus Walleij 		.burstwords = 8,
1625760596c6SRussell King - ARM Linux 		.reg = PL080_BSIZE_8,
1626e8689e63SLinus Walleij 	},
1627e8689e63SLinus Walleij 	{
1628e8689e63SLinus Walleij 		.burstwords = 4,
1629760596c6SRussell King - ARM Linux 		.reg = PL080_BSIZE_4,
1630e8689e63SLinus Walleij 	},
1631e8689e63SLinus Walleij 	{
1632760596c6SRussell King - ARM Linux 		.burstwords = 0,
1633760596c6SRussell King - ARM Linux 		.reg = PL080_BSIZE_1,
1634e8689e63SLinus Walleij 	},
1635e8689e63SLinus Walleij };
1636e8689e63SLinus Walleij 
1637121c8476SRussell King - ARM Linux /*
1638121c8476SRussell King - ARM Linux  * Given the source and destination available bus masks, select which
1639121c8476SRussell King - ARM Linux  * will be routed to each port.  We try to have source and destination
1640121c8476SRussell King - ARM Linux  * on separate ports, but always respect the allowable settings.
1641121c8476SRussell King - ARM Linux  */
pl08x_select_bus(bool ftdmac020,u8 src,u8 dst)16421e1cfc72SLinus Walleij static u32 pl08x_select_bus(bool ftdmac020, u8 src, u8 dst)
1643121c8476SRussell King - ARM Linux {
1644121c8476SRussell King - ARM Linux 	u32 cctl = 0;
16451e1cfc72SLinus Walleij 	u32 dst_ahb2;
16461e1cfc72SLinus Walleij 	u32 src_ahb2;
16471e1cfc72SLinus Walleij 
16481e1cfc72SLinus Walleij 	/* The FTDMAC020 use different bits to indicate src/dst bus */
16491e1cfc72SLinus Walleij 	if (ftdmac020) {
16501e1cfc72SLinus Walleij 		dst_ahb2 = FTDMAC020_LLI_DST_SEL;
16511e1cfc72SLinus Walleij 		src_ahb2 = FTDMAC020_LLI_SRC_SEL;
16521e1cfc72SLinus Walleij 	} else {
16531e1cfc72SLinus Walleij 		dst_ahb2 = PL080_CONTROL_DST_AHB2;
16541e1cfc72SLinus Walleij 		src_ahb2 = PL080_CONTROL_SRC_AHB2;
16551e1cfc72SLinus Walleij 	}
1656121c8476SRussell King - ARM Linux 
1657121c8476SRussell King - ARM Linux 	if (!(dst & PL08X_AHB1) || ((dst & PL08X_AHB2) && (src & PL08X_AHB1)))
16581e1cfc72SLinus Walleij 		cctl |= dst_ahb2;
1659121c8476SRussell King - ARM Linux 	if (!(src & PL08X_AHB1) || ((src & PL08X_AHB2) && !(dst & PL08X_AHB2)))
16601e1cfc72SLinus Walleij 		cctl |= src_ahb2;
1661121c8476SRussell King - ARM Linux 
1662121c8476SRussell King - ARM Linux 	return cctl;
1663121c8476SRussell King - ARM Linux }
1664121c8476SRussell King - ARM Linux 
pl08x_cctl(u32 cctl)1665f14c426cSRussell King - ARM Linux static u32 pl08x_cctl(u32 cctl)
1666f14c426cSRussell King - ARM Linux {
1667f14c426cSRussell King - ARM Linux 	cctl &= ~(PL080_CONTROL_SRC_AHB2 | PL080_CONTROL_DST_AHB2 |
1668f14c426cSRussell King - ARM Linux 		  PL080_CONTROL_SRC_INCR | PL080_CONTROL_DST_INCR |
1669f14c426cSRussell King - ARM Linux 		  PL080_CONTROL_PROT_MASK);
1670f14c426cSRussell King - ARM Linux 
1671f14c426cSRussell King - ARM Linux 	/* Access the cell in privileged mode, non-bufferable, non-cacheable */
1672f14c426cSRussell King - ARM Linux 	return cctl | PL080_CONTROL_PROT_SYS;
1673f14c426cSRussell King - ARM Linux }
1674f14c426cSRussell King - ARM Linux 
pl08x_width(enum dma_slave_buswidth width)1675aa88cdaaSRussell King - ARM Linux static u32 pl08x_width(enum dma_slave_buswidth width)
1676aa88cdaaSRussell King - ARM Linux {
1677aa88cdaaSRussell King - ARM Linux 	switch (width) {
1678aa88cdaaSRussell King - ARM Linux 	case DMA_SLAVE_BUSWIDTH_1_BYTE:
1679aa88cdaaSRussell King - ARM Linux 		return PL080_WIDTH_8BIT;
1680aa88cdaaSRussell King - ARM Linux 	case DMA_SLAVE_BUSWIDTH_2_BYTES:
1681aa88cdaaSRussell King - ARM Linux 		return PL080_WIDTH_16BIT;
1682aa88cdaaSRussell King - ARM Linux 	case DMA_SLAVE_BUSWIDTH_4_BYTES:
1683aa88cdaaSRussell King - ARM Linux 		return PL080_WIDTH_32BIT;
1684f32807f1SVinod Koul 	default:
1685aa88cdaaSRussell King - ARM Linux 		return ~0;
1686aa88cdaaSRussell King - ARM Linux 	}
1687f32807f1SVinod Koul }
1688aa88cdaaSRussell King - ARM Linux 
pl08x_burst(u32 maxburst)1689760596c6SRussell King - ARM Linux static u32 pl08x_burst(u32 maxburst)
1690760596c6SRussell King - ARM Linux {
1691760596c6SRussell King - ARM Linux 	int i;
1692760596c6SRussell King - ARM Linux 
1693760596c6SRussell King - ARM Linux 	for (i = 0; i < ARRAY_SIZE(burst_sizes); i++)
1694760596c6SRussell King - ARM Linux 		if (burst_sizes[i].burstwords <= maxburst)
1695760596c6SRussell King - ARM Linux 			break;
1696760596c6SRussell King - ARM Linux 
1697760596c6SRussell King - ARM Linux 	return burst_sizes[i].reg;
1698760596c6SRussell King - ARM Linux }
1699760596c6SRussell King - ARM Linux 
pl08x_get_cctl(struct pl08x_dma_chan * plchan,enum dma_slave_buswidth addr_width,u32 maxburst)17009862ba17SRussell King static u32 pl08x_get_cctl(struct pl08x_dma_chan *plchan,
17019862ba17SRussell King 	enum dma_slave_buswidth addr_width, u32 maxburst)
17029862ba17SRussell King {
17039862ba17SRussell King 	u32 width, burst, cctl = 0;
17049862ba17SRussell King 
17059862ba17SRussell King 	width = pl08x_width(addr_width);
17069862ba17SRussell King 	if (width == ~0)
17079862ba17SRussell King 		return ~0;
17089862ba17SRussell King 
17099862ba17SRussell King 	cctl |= width << PL080_CONTROL_SWIDTH_SHIFT;
17109862ba17SRussell King 	cctl |= width << PL080_CONTROL_DWIDTH_SHIFT;
17119862ba17SRussell King 
17129862ba17SRussell King 	/*
17139862ba17SRussell King 	 * If this channel will only request single transfers, set this
17149862ba17SRussell King 	 * down to ONE element.  Also select one element if no maxburst
17159862ba17SRussell King 	 * is specified.
17169862ba17SRussell King 	 */
17179862ba17SRussell King 	if (plchan->cd->single)
17189862ba17SRussell King 		maxburst = 1;
17199862ba17SRussell King 
17209862ba17SRussell King 	burst = pl08x_burst(maxburst);
17219862ba17SRussell King 	cctl |= burst << PL080_CONTROL_SB_SIZE_SHIFT;
17229862ba17SRussell King 	cctl |= burst << PL080_CONTROL_DB_SIZE_SHIFT;
17239862ba17SRussell King 
17249862ba17SRussell King 	return pl08x_cctl(cctl);
17259862ba17SRussell King }
17269862ba17SRussell King 
1727e8689e63SLinus Walleij /*
1728e8689e63SLinus Walleij  * Slave transactions callback to the slave device to allow
1729e8689e63SLinus Walleij  * synchronization of slave DMA signals with the DMAC enable
1730e8689e63SLinus Walleij  */
pl08x_issue_pending(struct dma_chan * chan)1731e8689e63SLinus Walleij static void pl08x_issue_pending(struct dma_chan *chan)
1732e8689e63SLinus Walleij {
1733e8689e63SLinus Walleij 	struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1734e8689e63SLinus Walleij 	unsigned long flags;
1735e8689e63SLinus Walleij 
1736083be28aSRussell King 	spin_lock_irqsave(&plchan->vc.lock, flags);
1737879f127bSRussell King 	if (vchan_issue_pending(&plchan->vc)) {
1738a5a488dbSRussell King 		if (!plchan->phychan && plchan->state != PL08X_CHAN_WAITING)
1739a5a488dbSRussell King 			pl08x_phy_alloc_and_start(plchan);
1740e8689e63SLinus Walleij 	}
1741083be28aSRussell King 	spin_unlock_irqrestore(&plchan->vc.lock, flags);
1742e8689e63SLinus Walleij }
1743e8689e63SLinus Walleij 
pl08x_get_txd(struct pl08x_dma_chan * plchan)1744879f127bSRussell King static struct pl08x_txd *pl08x_get_txd(struct pl08x_dma_chan *plchan)
1745ac3cd20dSRussell King - ARM Linux {
1746b201c111SViresh Kumar 	struct pl08x_txd *txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
1747ac3cd20dSRussell King - ARM Linux 
17481e1cfc72SLinus Walleij 	if (txd)
1749b7f69d9dSViresh Kumar 		INIT_LIST_HEAD(&txd->dsg_list);
1750ac3cd20dSRussell King - ARM Linux 	return txd;
1751ac3cd20dSRussell King - ARM Linux }
1752ac3cd20dSRussell King - ARM Linux 
pl08x_memcpy_cctl(struct pl08x_driver_data * pl08x)17531e1cfc72SLinus Walleij static u32 pl08x_memcpy_cctl(struct pl08x_driver_data *pl08x)
1754e8689e63SLinus Walleij {
17554166a56aSLinus Walleij 	u32 cctl = 0;
17564166a56aSLinus Walleij 
17574166a56aSLinus Walleij 	/* Conjure cctl */
17584166a56aSLinus Walleij 	switch (pl08x->pd->memcpy_burst_size) {
17594166a56aSLinus Walleij 	default:
17604166a56aSLinus Walleij 		dev_err(&pl08x->adev->dev,
17614166a56aSLinus Walleij 			"illegal burst size for memcpy, set to 1\n");
1762df561f66SGustavo A. R. Silva 		fallthrough;
17634166a56aSLinus Walleij 	case PL08X_BURST_SZ_1:
17644166a56aSLinus Walleij 		cctl |= PL080_BSIZE_1 << PL080_CONTROL_SB_SIZE_SHIFT |
17654166a56aSLinus Walleij 			PL080_BSIZE_1 << PL080_CONTROL_DB_SIZE_SHIFT;
17664166a56aSLinus Walleij 		break;
17674166a56aSLinus Walleij 	case PL08X_BURST_SZ_4:
17684166a56aSLinus Walleij 		cctl |= PL080_BSIZE_4 << PL080_CONTROL_SB_SIZE_SHIFT |
17694166a56aSLinus Walleij 			PL080_BSIZE_4 << PL080_CONTROL_DB_SIZE_SHIFT;
17704166a56aSLinus Walleij 		break;
17714166a56aSLinus Walleij 	case PL08X_BURST_SZ_8:
17724166a56aSLinus Walleij 		cctl |= PL080_BSIZE_8 << PL080_CONTROL_SB_SIZE_SHIFT |
17734166a56aSLinus Walleij 			PL080_BSIZE_8 << PL080_CONTROL_DB_SIZE_SHIFT;
17744166a56aSLinus Walleij 		break;
17754166a56aSLinus Walleij 	case PL08X_BURST_SZ_16:
17764166a56aSLinus Walleij 		cctl |= PL080_BSIZE_16 << PL080_CONTROL_SB_SIZE_SHIFT |
17774166a56aSLinus Walleij 			PL080_BSIZE_16 << PL080_CONTROL_DB_SIZE_SHIFT;
17784166a56aSLinus Walleij 		break;
17794166a56aSLinus Walleij 	case PL08X_BURST_SZ_32:
17804166a56aSLinus Walleij 		cctl |= PL080_BSIZE_32 << PL080_CONTROL_SB_SIZE_SHIFT |
17814166a56aSLinus Walleij 			PL080_BSIZE_32 << PL080_CONTROL_DB_SIZE_SHIFT;
17824166a56aSLinus Walleij 		break;
17834166a56aSLinus Walleij 	case PL08X_BURST_SZ_64:
17844166a56aSLinus Walleij 		cctl |= PL080_BSIZE_64 << PL080_CONTROL_SB_SIZE_SHIFT |
17854166a56aSLinus Walleij 			PL080_BSIZE_64 << PL080_CONTROL_DB_SIZE_SHIFT;
17864166a56aSLinus Walleij 		break;
17874166a56aSLinus Walleij 	case PL08X_BURST_SZ_128:
17884166a56aSLinus Walleij 		cctl |= PL080_BSIZE_128 << PL080_CONTROL_SB_SIZE_SHIFT |
17894166a56aSLinus Walleij 			PL080_BSIZE_128 << PL080_CONTROL_DB_SIZE_SHIFT;
17904166a56aSLinus Walleij 		break;
17914166a56aSLinus Walleij 	case PL08X_BURST_SZ_256:
17924166a56aSLinus Walleij 		cctl |= PL080_BSIZE_256 << PL080_CONTROL_SB_SIZE_SHIFT |
17934166a56aSLinus Walleij 			PL080_BSIZE_256 << PL080_CONTROL_DB_SIZE_SHIFT;
17944166a56aSLinus Walleij 		break;
17954166a56aSLinus Walleij 	}
17964166a56aSLinus Walleij 
17974166a56aSLinus Walleij 	switch (pl08x->pd->memcpy_bus_width) {
17984166a56aSLinus Walleij 	default:
17994166a56aSLinus Walleij 		dev_err(&pl08x->adev->dev,
18004166a56aSLinus Walleij 			"illegal bus width for memcpy, set to 8 bits\n");
1801df561f66SGustavo A. R. Silva 		fallthrough;
18024166a56aSLinus Walleij 	case PL08X_BUS_WIDTH_8_BITS:
18034166a56aSLinus Walleij 		cctl |= PL080_WIDTH_8BIT << PL080_CONTROL_SWIDTH_SHIFT |
18044166a56aSLinus Walleij 			PL080_WIDTH_8BIT << PL080_CONTROL_DWIDTH_SHIFT;
18054166a56aSLinus Walleij 		break;
18064166a56aSLinus Walleij 	case PL08X_BUS_WIDTH_16_BITS:
18074166a56aSLinus Walleij 		cctl |= PL080_WIDTH_16BIT << PL080_CONTROL_SWIDTH_SHIFT |
18084166a56aSLinus Walleij 			PL080_WIDTH_16BIT << PL080_CONTROL_DWIDTH_SHIFT;
18094166a56aSLinus Walleij 		break;
18104166a56aSLinus Walleij 	case PL08X_BUS_WIDTH_32_BITS:
18114166a56aSLinus Walleij 		cctl |= PL080_WIDTH_32BIT << PL080_CONTROL_SWIDTH_SHIFT |
18124166a56aSLinus Walleij 			PL080_WIDTH_32BIT << PL080_CONTROL_DWIDTH_SHIFT;
18134166a56aSLinus Walleij 		break;
18144166a56aSLinus Walleij 	}
18154166a56aSLinus Walleij 
18164166a56aSLinus Walleij 	/* Protection flags */
18174166a56aSLinus Walleij 	if (pl08x->pd->memcpy_prot_buff)
18184166a56aSLinus Walleij 		cctl |= PL080_CONTROL_PROT_BUFF;
18194166a56aSLinus Walleij 	if (pl08x->pd->memcpy_prot_cache)
18204166a56aSLinus Walleij 		cctl |= PL080_CONTROL_PROT_CACHE;
18214166a56aSLinus Walleij 
18224166a56aSLinus Walleij 	/* We are the kernel, so we are in privileged mode */
18234166a56aSLinus Walleij 	cctl |= PL080_CONTROL_PROT_SYS;
18244983a04fSRussell King - ARM Linux 
1825e8689e63SLinus Walleij 	/* Both to be incremented or the code will break */
18264166a56aSLinus Walleij 	cctl |= PL080_CONTROL_SRC_INCR | PL080_CONTROL_DST_INCR;
1827c7da9a56SRussell King - ARM Linux 
1828c7da9a56SRussell King - ARM Linux 	if (pl08x->vd->dualmaster)
18291e1cfc72SLinus Walleij 		cctl |= pl08x_select_bus(false,
18301e1cfc72SLinus Walleij 					 pl08x->mem_buses,
1831121c8476SRussell King - ARM Linux 					 pl08x->mem_buses);
1832e8689e63SLinus Walleij 
18331e1cfc72SLinus Walleij 	return cctl;
18341e1cfc72SLinus Walleij }
18351e1cfc72SLinus Walleij 
pl08x_ftdmac020_memcpy_cctl(struct pl08x_driver_data * pl08x)18361e1cfc72SLinus Walleij static u32 pl08x_ftdmac020_memcpy_cctl(struct pl08x_driver_data *pl08x)
18371e1cfc72SLinus Walleij {
18381e1cfc72SLinus Walleij 	u32 cctl = 0;
18391e1cfc72SLinus Walleij 
18401e1cfc72SLinus Walleij 	/* Conjure cctl */
18411e1cfc72SLinus Walleij 	switch (pl08x->pd->memcpy_bus_width) {
18421e1cfc72SLinus Walleij 	default:
18431e1cfc72SLinus Walleij 		dev_err(&pl08x->adev->dev,
18441e1cfc72SLinus Walleij 			"illegal bus width for memcpy, set to 8 bits\n");
1845df561f66SGustavo A. R. Silva 		fallthrough;
18461e1cfc72SLinus Walleij 	case PL08X_BUS_WIDTH_8_BITS:
18471e1cfc72SLinus Walleij 		cctl |= PL080_WIDTH_8BIT << FTDMAC020_LLI_SRC_WIDTH_SHIFT |
18481e1cfc72SLinus Walleij 			PL080_WIDTH_8BIT << FTDMAC020_LLI_DST_WIDTH_SHIFT;
18491e1cfc72SLinus Walleij 		break;
18501e1cfc72SLinus Walleij 	case PL08X_BUS_WIDTH_16_BITS:
18511e1cfc72SLinus Walleij 		cctl |= PL080_WIDTH_16BIT << FTDMAC020_LLI_SRC_WIDTH_SHIFT |
18521e1cfc72SLinus Walleij 			PL080_WIDTH_16BIT << FTDMAC020_LLI_DST_WIDTH_SHIFT;
18531e1cfc72SLinus Walleij 		break;
18541e1cfc72SLinus Walleij 	case PL08X_BUS_WIDTH_32_BITS:
18551e1cfc72SLinus Walleij 		cctl |= PL080_WIDTH_32BIT << FTDMAC020_LLI_SRC_WIDTH_SHIFT |
18561e1cfc72SLinus Walleij 			PL080_WIDTH_32BIT << FTDMAC020_LLI_DST_WIDTH_SHIFT;
18571e1cfc72SLinus Walleij 		break;
18581e1cfc72SLinus Walleij 	}
18591e1cfc72SLinus Walleij 
18601e1cfc72SLinus Walleij 	/*
18611e1cfc72SLinus Walleij 	 * By default mask the TC IRQ on all LLIs, it will be unmasked on
18621e1cfc72SLinus Walleij 	 * the last LLI item by other code.
18631e1cfc72SLinus Walleij 	 */
18641e1cfc72SLinus Walleij 	cctl |= FTDMAC020_LLI_TC_MSK;
18651e1cfc72SLinus Walleij 
18661e1cfc72SLinus Walleij 	/*
18671e1cfc72SLinus Walleij 	 * Both to be incremented so leave bits FTDMAC020_LLI_SRCAD_CTL
18681e1cfc72SLinus Walleij 	 * and FTDMAC020_LLI_DSTAD_CTL as zero
18691e1cfc72SLinus Walleij 	 */
18701e1cfc72SLinus Walleij 	if (pl08x->vd->dualmaster)
18711e1cfc72SLinus Walleij 		cctl |= pl08x_select_bus(true,
18721e1cfc72SLinus Walleij 					 pl08x->mem_buses,
18731e1cfc72SLinus Walleij 					 pl08x->mem_buses);
18741e1cfc72SLinus Walleij 
18751e1cfc72SLinus Walleij 	return cctl;
18761e1cfc72SLinus Walleij }
18771e1cfc72SLinus Walleij 
18781e1cfc72SLinus Walleij /*
18791e1cfc72SLinus Walleij  * Initialize a descriptor to be used by memcpy submit
18801e1cfc72SLinus Walleij  */
pl08x_prep_dma_memcpy(struct dma_chan * chan,dma_addr_t dest,dma_addr_t src,size_t len,unsigned long flags)18811e1cfc72SLinus Walleij static struct dma_async_tx_descriptor *pl08x_prep_dma_memcpy(
18821e1cfc72SLinus Walleij 		struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
18831e1cfc72SLinus Walleij 		size_t len, unsigned long flags)
18841e1cfc72SLinus Walleij {
18851e1cfc72SLinus Walleij 	struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
18861e1cfc72SLinus Walleij 	struct pl08x_driver_data *pl08x = plchan->host;
18871e1cfc72SLinus Walleij 	struct pl08x_txd *txd;
18881e1cfc72SLinus Walleij 	struct pl08x_sg *dsg;
18891e1cfc72SLinus Walleij 	int ret;
18901e1cfc72SLinus Walleij 
18911e1cfc72SLinus Walleij 	txd = pl08x_get_txd(plchan);
18921e1cfc72SLinus Walleij 	if (!txd) {
18931e1cfc72SLinus Walleij 		dev_err(&pl08x->adev->dev,
18941e1cfc72SLinus Walleij 			"%s no memory for descriptor\n", __func__);
18951e1cfc72SLinus Walleij 		return NULL;
18961e1cfc72SLinus Walleij 	}
18971e1cfc72SLinus Walleij 
18981e1cfc72SLinus Walleij 	dsg = kzalloc(sizeof(struct pl08x_sg), GFP_NOWAIT);
18991e1cfc72SLinus Walleij 	if (!dsg) {
19001e1cfc72SLinus Walleij 		pl08x_free_txd(pl08x, txd);
19011e1cfc72SLinus Walleij 		return NULL;
19021e1cfc72SLinus Walleij 	}
19031e1cfc72SLinus Walleij 	list_add_tail(&dsg->node, &txd->dsg_list);
19041e1cfc72SLinus Walleij 
19051e1cfc72SLinus Walleij 	dsg->src_addr = src;
19061e1cfc72SLinus Walleij 	dsg->dst_addr = dest;
19071e1cfc72SLinus Walleij 	dsg->len = len;
19081e1cfc72SLinus Walleij 	if (pl08x->vd->ftdmac020) {
19091e1cfc72SLinus Walleij 		/* Writing CCFG zero ENABLES all interrupts */
19101e1cfc72SLinus Walleij 		txd->ccfg = 0;
19111e1cfc72SLinus Walleij 		txd->cctl = pl08x_ftdmac020_memcpy_cctl(pl08x);
19121e1cfc72SLinus Walleij 	} else {
19131e1cfc72SLinus Walleij 		txd->ccfg = PL080_CONFIG_ERR_IRQ_MASK |
19141e1cfc72SLinus Walleij 			PL080_CONFIG_TC_IRQ_MASK |
19151e1cfc72SLinus Walleij 			PL080_FLOW_MEM2MEM << PL080_CONFIG_FLOW_CONTROL_SHIFT;
19161e1cfc72SLinus Walleij 		txd->cctl = pl08x_memcpy_cctl(pl08x);
19171e1cfc72SLinus Walleij 	}
19184166a56aSLinus Walleij 
1919aa4afb75SRussell King 	ret = pl08x_fill_llis_for_desc(plchan->host, txd);
1920aa4afb75SRussell King 	if (!ret) {
1921aa4afb75SRussell King 		pl08x_free_txd(pl08x, txd);
1922e8689e63SLinus Walleij 		return NULL;
1923aa4afb75SRussell King 	}
1924e8689e63SLinus Walleij 
1925879f127bSRussell King 	return vchan_tx_prep(&plchan->vc, &txd->vd, flags);
1926e8689e63SLinus Walleij }
1927e8689e63SLinus Walleij 
pl08x_init_txd(struct dma_chan * chan,enum dma_transfer_direction direction,dma_addr_t * slave_addr)19283b24c20bSAlban Bedel static struct pl08x_txd *pl08x_init_txd(
19293b24c20bSAlban Bedel 		struct dma_chan *chan,
19303b24c20bSAlban Bedel 		enum dma_transfer_direction direction,
19313b24c20bSAlban Bedel 		dma_addr_t *slave_addr)
1932e8689e63SLinus Walleij {
1933e8689e63SLinus Walleij 	struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
1934e8689e63SLinus Walleij 	struct pl08x_driver_data *pl08x = plchan->host;
1935e8689e63SLinus Walleij 	struct pl08x_txd *txd;
1936dc8d5f8dSRussell King 	enum dma_slave_buswidth addr_width;
19370a235657SViresh Kumar 	int ret, tmp;
1938409ec8dbSRussell King 	u8 src_buses, dst_buses;
1939dc8d5f8dSRussell King 	u32 maxburst, cctl;
1940e8689e63SLinus Walleij 
1941879f127bSRussell King 	txd = pl08x_get_txd(plchan);
1942e8689e63SLinus Walleij 	if (!txd) {
1943e8689e63SLinus Walleij 		dev_err(&pl08x->adev->dev, "%s no txd\n", __func__);
1944e8689e63SLinus Walleij 		return NULL;
1945e8689e63SLinus Walleij 	}
1946e8689e63SLinus Walleij 
1947e8689e63SLinus Walleij 	/*
1948e8689e63SLinus Walleij 	 * Set up addresses, the PrimeCell configured address
1949e8689e63SLinus Walleij 	 * will take precedence since this may configure the
1950e8689e63SLinus Walleij 	 * channel target address dynamically at runtime.
1951e8689e63SLinus Walleij 	 */
1952db8196dfSVinod Koul 	if (direction == DMA_MEM_TO_DEV) {
1953dc8d5f8dSRussell King 		cctl = PL080_CONTROL_SRC_INCR;
19543b24c20bSAlban Bedel 		*slave_addr = plchan->cfg.dst_addr;
1955dc8d5f8dSRussell King 		addr_width = plchan->cfg.dst_addr_width;
1956dc8d5f8dSRussell King 		maxburst = plchan->cfg.dst_maxburst;
1957409ec8dbSRussell King 		src_buses = pl08x->mem_buses;
1958409ec8dbSRussell King 		dst_buses = plchan->cd->periph_buses;
1959db8196dfSVinod Koul 	} else if (direction == DMA_DEV_TO_MEM) {
1960dc8d5f8dSRussell King 		cctl = PL080_CONTROL_DST_INCR;
19613b24c20bSAlban Bedel 		*slave_addr = plchan->cfg.src_addr;
1962dc8d5f8dSRussell King 		addr_width = plchan->cfg.src_addr_width;
1963dc8d5f8dSRussell King 		maxburst = plchan->cfg.src_maxburst;
1964409ec8dbSRussell King 		src_buses = plchan->cd->periph_buses;
1965409ec8dbSRussell King 		dst_buses = pl08x->mem_buses;
1966e8689e63SLinus Walleij 	} else {
1967b7f69d9dSViresh Kumar 		pl08x_free_txd(pl08x, txd);
1968e8689e63SLinus Walleij 		dev_err(&pl08x->adev->dev,
1969e8689e63SLinus Walleij 			"%s direction unsupported\n", __func__);
1970e8689e63SLinus Walleij 		return NULL;
1971e8689e63SLinus Walleij 	}
1972e8689e63SLinus Walleij 
1973dc8d5f8dSRussell King 	cctl |= pl08x_get_cctl(plchan, addr_width, maxburst);
1974800d683eSRussell King 	if (cctl == ~0) {
1975800d683eSRussell King 		pl08x_free_txd(pl08x, txd);
1976800d683eSRussell King 		dev_err(&pl08x->adev->dev,
1977800d683eSRussell King 			"DMA slave configuration botched?\n");
1978800d683eSRussell King 		return NULL;
1979800d683eSRussell King 	}
1980800d683eSRussell King 
19811e1cfc72SLinus Walleij 	txd->cctl = cctl | pl08x_select_bus(false, src_buses, dst_buses);
1982409ec8dbSRussell King 
198395442b22SRussell King 	if (plchan->cfg.device_fc)
1984db8196dfSVinod Koul 		tmp = (direction == DMA_MEM_TO_DEV) ? PL080_FLOW_MEM2PER_PER :
19850a235657SViresh Kumar 			PL080_FLOW_PER2MEM_PER;
19860a235657SViresh Kumar 	else
1987db8196dfSVinod Koul 		tmp = (direction == DMA_MEM_TO_DEV) ? PL080_FLOW_MEM2PER :
19880a235657SViresh Kumar 			PL080_FLOW_PER2MEM;
19890a235657SViresh Kumar 
19901e1cfc72SLinus Walleij 	txd->ccfg = PL080_CONFIG_ERR_IRQ_MASK |
19911e1cfc72SLinus Walleij 		PL080_CONFIG_TC_IRQ_MASK |
19921e1cfc72SLinus Walleij 		tmp << PL080_CONFIG_FLOW_CONTROL_SHIFT;
19930a235657SViresh Kumar 
1994c48d4963SRussell King 	ret = pl08x_request_mux(plchan);
1995c48d4963SRussell King 	if (ret < 0) {
1996c48d4963SRussell King 		pl08x_free_txd(pl08x, txd);
1997c48d4963SRussell King 		dev_dbg(&pl08x->adev->dev,
1998c48d4963SRussell King 			"unable to mux for transfer on %s due to platform restrictions\n",
1999c48d4963SRussell King 			plchan->name);
2000c48d4963SRussell King 		return NULL;
2001c48d4963SRussell King 	}
2002c48d4963SRussell King 
2003c48d4963SRussell King 	dev_dbg(&pl08x->adev->dev, "allocated DMA request signal %d for xfer on %s\n",
2004c48d4963SRussell King 		 plchan->signal, plchan->name);
2005c48d4963SRussell King 
2006c48d4963SRussell King 	/* Assign the flow control signal to this channel */
2007c48d4963SRussell King 	if (direction == DMA_MEM_TO_DEV)
2008c48d4963SRussell King 		txd->ccfg |= plchan->signal << PL080_CONFIG_DST_SEL_SHIFT;
2009c48d4963SRussell King 	else
2010c48d4963SRussell King 		txd->ccfg |= plchan->signal << PL080_CONFIG_SRC_SEL_SHIFT;
2011c48d4963SRussell King 
20123b24c20bSAlban Bedel 	return txd;
20133b24c20bSAlban Bedel }
20143b24c20bSAlban Bedel 
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)20153b24c20bSAlban Bedel static int pl08x_tx_add_sg(struct pl08x_txd *txd,
20163b24c20bSAlban Bedel 			   enum dma_transfer_direction direction,
20173b24c20bSAlban Bedel 			   dma_addr_t slave_addr,
20183b24c20bSAlban Bedel 			   dma_addr_t buf_addr,
20193b24c20bSAlban Bedel 			   unsigned int len)
20203b24c20bSAlban Bedel {
20213b24c20bSAlban Bedel 	struct pl08x_sg *dsg;
20223b24c20bSAlban Bedel 
2023b7f69d9dSViresh Kumar 	dsg = kzalloc(sizeof(struct pl08x_sg), GFP_NOWAIT);
20243b24c20bSAlban Bedel 	if (!dsg)
20253b24c20bSAlban Bedel 		return -ENOMEM;
20263b24c20bSAlban Bedel 
20273b24c20bSAlban Bedel 	list_add_tail(&dsg->node, &txd->dsg_list);
20283b24c20bSAlban Bedel 
20293b24c20bSAlban Bedel 	dsg->len = len;
20303b24c20bSAlban Bedel 	if (direction == DMA_MEM_TO_DEV) {
20313b24c20bSAlban Bedel 		dsg->src_addr = buf_addr;
20323b24c20bSAlban Bedel 		dsg->dst_addr = slave_addr;
20333b24c20bSAlban Bedel 	} else {
20343b24c20bSAlban Bedel 		dsg->src_addr = slave_addr;
20353b24c20bSAlban Bedel 		dsg->dst_addr = buf_addr;
20363b24c20bSAlban Bedel 	}
20373b24c20bSAlban Bedel 
20383b24c20bSAlban Bedel 	return 0;
20393b24c20bSAlban Bedel }
20403b24c20bSAlban Bedel 
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)20413b24c20bSAlban Bedel static struct dma_async_tx_descriptor *pl08x_prep_slave_sg(
20423b24c20bSAlban Bedel 		struct dma_chan *chan, struct scatterlist *sgl,
20433b24c20bSAlban Bedel 		unsigned int sg_len, enum dma_transfer_direction direction,
20443b24c20bSAlban Bedel 		unsigned long flags, void *context)
20453b24c20bSAlban Bedel {
20463b24c20bSAlban Bedel 	struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
20473b24c20bSAlban Bedel 	struct pl08x_driver_data *pl08x = plchan->host;
20483b24c20bSAlban Bedel 	struct pl08x_txd *txd;
20493b24c20bSAlban Bedel 	struct scatterlist *sg;
20503b24c20bSAlban Bedel 	int ret, tmp;
20513b24c20bSAlban Bedel 	dma_addr_t slave_addr;
20523b24c20bSAlban Bedel 
20533b24c20bSAlban Bedel 	dev_dbg(&pl08x->adev->dev, "%s prepare transaction of %d bytes from %s\n",
20543b24c20bSAlban Bedel 			__func__, sg_dma_len(sgl), plchan->name);
20553b24c20bSAlban Bedel 
20563b24c20bSAlban Bedel 	txd = pl08x_init_txd(chan, direction, &slave_addr);
20573b24c20bSAlban Bedel 	if (!txd)
20583b24c20bSAlban Bedel 		return NULL;
20593b24c20bSAlban Bedel 
20603b24c20bSAlban Bedel 	for_each_sg(sgl, sg, sg_len, tmp) {
20613b24c20bSAlban Bedel 		ret = pl08x_tx_add_sg(txd, direction, slave_addr,
20623b24c20bSAlban Bedel 				      sg_dma_address(sg),
20633b24c20bSAlban Bedel 				      sg_dma_len(sg));
20643b24c20bSAlban Bedel 		if (ret) {
2065c48d4963SRussell King 			pl08x_release_mux(plchan);
2066b7f69d9dSViresh Kumar 			pl08x_free_txd(pl08x, txd);
2067b7f69d9dSViresh Kumar 			dev_err(&pl08x->adev->dev, "%s no mem for pl080 sg\n",
2068b7f69d9dSViresh Kumar 					__func__);
2069b7f69d9dSViresh Kumar 			return NULL;
2070b7f69d9dSViresh Kumar 		}
20713b24c20bSAlban Bedel 	}
2072b7f69d9dSViresh Kumar 
20733b24c20bSAlban Bedel 	ret = pl08x_fill_llis_for_desc(plchan->host, txd);
20743b24c20bSAlban Bedel 	if (!ret) {
20753b24c20bSAlban Bedel 		pl08x_release_mux(plchan);
20763b24c20bSAlban Bedel 		pl08x_free_txd(pl08x, txd);
20773b24c20bSAlban Bedel 		return NULL;
20783b24c20bSAlban Bedel 	}
20793b24c20bSAlban Bedel 
20803b24c20bSAlban Bedel 	return vchan_tx_prep(&plchan->vc, &txd->vd, flags);
20813b24c20bSAlban Bedel }
20823b24c20bSAlban Bedel 
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)20833b24c20bSAlban Bedel static struct dma_async_tx_descriptor *pl08x_prep_dma_cyclic(
20843b24c20bSAlban Bedel 		struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
20853b24c20bSAlban Bedel 		size_t period_len, enum dma_transfer_direction direction,
208631c1e5a1SLaurent Pinchart 		unsigned long flags)
20873b24c20bSAlban Bedel {
20883b24c20bSAlban Bedel 	struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
20893b24c20bSAlban Bedel 	struct pl08x_driver_data *pl08x = plchan->host;
20903b24c20bSAlban Bedel 	struct pl08x_txd *txd;
20913b24c20bSAlban Bedel 	int ret, tmp;
20923b24c20bSAlban Bedel 	dma_addr_t slave_addr;
20933b24c20bSAlban Bedel 
20943b24c20bSAlban Bedel 	dev_dbg(&pl08x->adev->dev,
20956fc8ae78SMark Brown 		"%s prepare cyclic transaction of %zd/%zd bytes %s %s\n",
20963b24c20bSAlban Bedel 		__func__, period_len, buf_len,
20973b24c20bSAlban Bedel 		direction == DMA_MEM_TO_DEV ? "to" : "from",
20983b24c20bSAlban Bedel 		plchan->name);
20993b24c20bSAlban Bedel 
21003b24c20bSAlban Bedel 	txd = pl08x_init_txd(chan, direction, &slave_addr);
21013b24c20bSAlban Bedel 	if (!txd)
21023b24c20bSAlban Bedel 		return NULL;
21033b24c20bSAlban Bedel 
21043b24c20bSAlban Bedel 	txd->cyclic = true;
21053b24c20bSAlban Bedel 	txd->cctl |= PL080_CONTROL_TC_IRQ_EN;
21063b24c20bSAlban Bedel 	for (tmp = 0; tmp < buf_len; tmp += period_len) {
21073b24c20bSAlban Bedel 		ret = pl08x_tx_add_sg(txd, direction, slave_addr,
21083b24c20bSAlban Bedel 				      buf_addr + tmp, period_len);
21093b24c20bSAlban Bedel 		if (ret) {
21103b24c20bSAlban Bedel 			pl08x_release_mux(plchan);
21113b24c20bSAlban Bedel 			pl08x_free_txd(pl08x, txd);
21123b24c20bSAlban Bedel 			return NULL;
2113b7f69d9dSViresh Kumar 		}
2114b7f69d9dSViresh Kumar 	}
2115b7f69d9dSViresh Kumar 
2116aa4afb75SRussell King 	ret = pl08x_fill_llis_for_desc(plchan->host, txd);
2117aa4afb75SRussell King 	if (!ret) {
2118aa4afb75SRussell King 		pl08x_release_mux(plchan);
2119aa4afb75SRussell King 		pl08x_free_txd(pl08x, txd);
2120e8689e63SLinus Walleij 		return NULL;
2121aa4afb75SRussell King 	}
2122e8689e63SLinus Walleij 
2123879f127bSRussell King 	return vchan_tx_prep(&plchan->vc, &txd->vd, flags);
2124e8689e63SLinus Walleij }
2125e8689e63SLinus Walleij 
pl08x_config(struct dma_chan * chan,struct dma_slave_config * config)2126bcd1b0b9SMaxime Ripard static int pl08x_config(struct dma_chan *chan,
2127bcd1b0b9SMaxime Ripard 			struct dma_slave_config *config)
2128bcd1b0b9SMaxime Ripard {
2129bcd1b0b9SMaxime Ripard 	struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
2130bcd1b0b9SMaxime Ripard 	struct pl08x_driver_data *pl08x = plchan->host;
2131bcd1b0b9SMaxime Ripard 
2132bcd1b0b9SMaxime Ripard 	if (!plchan->slave)
2133bcd1b0b9SMaxime Ripard 		return -EINVAL;
2134bcd1b0b9SMaxime Ripard 
2135bcd1b0b9SMaxime Ripard 	/* Reject definitely invalid configurations */
2136bcd1b0b9SMaxime Ripard 	if (config->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
2137bcd1b0b9SMaxime Ripard 	    config->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
2138bcd1b0b9SMaxime Ripard 		return -EINVAL;
2139bcd1b0b9SMaxime Ripard 
2140bcd1b0b9SMaxime Ripard 	if (config->device_fc && pl08x->vd->pl080s) {
2141bcd1b0b9SMaxime Ripard 		dev_err(&pl08x->adev->dev,
2142bcd1b0b9SMaxime Ripard 			"%s: PL080S does not support peripheral flow control\n",
2143bcd1b0b9SMaxime Ripard 			__func__);
2144bcd1b0b9SMaxime Ripard 		return -EINVAL;
2145bcd1b0b9SMaxime Ripard 	}
2146bcd1b0b9SMaxime Ripard 
2147bcd1b0b9SMaxime Ripard 	plchan->cfg = *config;
2148bcd1b0b9SMaxime Ripard 
2149bcd1b0b9SMaxime Ripard 	return 0;
2150bcd1b0b9SMaxime Ripard }
2151bcd1b0b9SMaxime Ripard 
pl08x_terminate_all(struct dma_chan * chan)2152bcd1b0b9SMaxime Ripard static int pl08x_terminate_all(struct dma_chan *chan)
2153e8689e63SLinus Walleij {
2154e8689e63SLinus Walleij 	struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
2155e8689e63SLinus Walleij 	struct pl08x_driver_data *pl08x = plchan->host;
2156e8689e63SLinus Walleij 	unsigned long flags;
2157e8689e63SLinus Walleij 
2158083be28aSRussell King 	spin_lock_irqsave(&plchan->vc.lock, flags);
2159e8689e63SLinus Walleij 	if (!plchan->phychan && !plchan->at) {
2160083be28aSRussell King 		spin_unlock_irqrestore(&plchan->vc.lock, flags);
2161e8689e63SLinus Walleij 		return 0;
2162e8689e63SLinus Walleij 	}
2163e8689e63SLinus Walleij 
2164e8689e63SLinus Walleij 	plchan->state = PL08X_CHAN_IDLE;
2165e8689e63SLinus Walleij 
2166e8689e63SLinus Walleij 	if (plchan->phychan) {
2167e8689e63SLinus Walleij 		/*
2168e8689e63SLinus Walleij 		 * Mark physical channel as free and free any slave
2169e8689e63SLinus Walleij 		 * signal
2170e8689e63SLinus Walleij 		 */
2171a5a488dbSRussell King 		pl08x_phy_free(plchan);
2172e8689e63SLinus Walleij 	}
2173e8689e63SLinus Walleij 	/* Dequeue jobs and free LLIs */
2174e8689e63SLinus Walleij 	if (plchan->at) {
217547d71bc7SPeter Ujfalusi 		vchan_terminate_vdesc(&plchan->at->vd);
2176e8689e63SLinus Walleij 		plchan->at = NULL;
2177e8689e63SLinus Walleij 	}
2178e8689e63SLinus Walleij 	/* Dequeue jobs not yet fired as well */
2179e8689e63SLinus Walleij 	pl08x_free_txd_list(pl08x, plchan);
2180e8689e63SLinus Walleij 
2181083be28aSRussell King 	spin_unlock_irqrestore(&plchan->vc.lock, flags);
2182e8689e63SLinus Walleij 
2183bcd1b0b9SMaxime Ripard 	return 0;
2184bcd1b0b9SMaxime Ripard }
2185bcd1b0b9SMaxime Ripard 
pl08x_synchronize(struct dma_chan * chan)218647d71bc7SPeter Ujfalusi static void pl08x_synchronize(struct dma_chan *chan)
218747d71bc7SPeter Ujfalusi {
218847d71bc7SPeter Ujfalusi 	struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
218947d71bc7SPeter Ujfalusi 
219047d71bc7SPeter Ujfalusi 	vchan_synchronize(&plchan->vc);
219147d71bc7SPeter Ujfalusi }
219247d71bc7SPeter Ujfalusi 
pl08x_pause(struct dma_chan * chan)2193bcd1b0b9SMaxime Ripard static int pl08x_pause(struct dma_chan *chan)
2194bcd1b0b9SMaxime Ripard {
2195bcd1b0b9SMaxime Ripard 	struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
2196bcd1b0b9SMaxime Ripard 	unsigned long flags;
2197bcd1b0b9SMaxime Ripard 
2198bcd1b0b9SMaxime Ripard 	/*
2199bcd1b0b9SMaxime Ripard 	 * Anything succeeds on channels with no physical allocation and
2200bcd1b0b9SMaxime Ripard 	 * no queued transfers.
2201bcd1b0b9SMaxime Ripard 	 */
2202bcd1b0b9SMaxime Ripard 	spin_lock_irqsave(&plchan->vc.lock, flags);
2203bcd1b0b9SMaxime Ripard 	if (!plchan->phychan && !plchan->at) {
2204bcd1b0b9SMaxime Ripard 		spin_unlock_irqrestore(&plchan->vc.lock, flags);
2205bcd1b0b9SMaxime Ripard 		return 0;
2206bcd1b0b9SMaxime Ripard 	}
2207bcd1b0b9SMaxime Ripard 
2208bcd1b0b9SMaxime Ripard 	pl08x_pause_phy_chan(plchan->phychan);
2209bcd1b0b9SMaxime Ripard 	plchan->state = PL08X_CHAN_PAUSED;
2210bcd1b0b9SMaxime Ripard 
2211bcd1b0b9SMaxime Ripard 	spin_unlock_irqrestore(&plchan->vc.lock, flags);
2212bcd1b0b9SMaxime Ripard 
2213bcd1b0b9SMaxime Ripard 	return 0;
2214bcd1b0b9SMaxime Ripard }
2215bcd1b0b9SMaxime Ripard 
pl08x_resume(struct dma_chan * chan)2216bcd1b0b9SMaxime Ripard static int pl08x_resume(struct dma_chan *chan)
2217bcd1b0b9SMaxime Ripard {
2218bcd1b0b9SMaxime Ripard 	struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
2219bcd1b0b9SMaxime Ripard 	unsigned long flags;
2220bcd1b0b9SMaxime Ripard 
2221bcd1b0b9SMaxime Ripard 	/*
2222bcd1b0b9SMaxime Ripard 	 * Anything succeeds on channels with no physical allocation and
2223bcd1b0b9SMaxime Ripard 	 * no queued transfers.
2224bcd1b0b9SMaxime Ripard 	 */
2225bcd1b0b9SMaxime Ripard 	spin_lock_irqsave(&plchan->vc.lock, flags);
2226bcd1b0b9SMaxime Ripard 	if (!plchan->phychan && !plchan->at) {
2227bcd1b0b9SMaxime Ripard 		spin_unlock_irqrestore(&plchan->vc.lock, flags);
2228bcd1b0b9SMaxime Ripard 		return 0;
2229bcd1b0b9SMaxime Ripard 	}
2230bcd1b0b9SMaxime Ripard 
2231bcd1b0b9SMaxime Ripard 	pl08x_resume_phy_chan(plchan->phychan);
2232bcd1b0b9SMaxime Ripard 	plchan->state = PL08X_CHAN_RUNNING;
2233bcd1b0b9SMaxime Ripard 
2234bcd1b0b9SMaxime Ripard 	spin_unlock_irqrestore(&plchan->vc.lock, flags);
2235bcd1b0b9SMaxime Ripard 
2236bcd1b0b9SMaxime Ripard 	return 0;
2237e8689e63SLinus Walleij }
2238e8689e63SLinus Walleij 
pl08x_filter_id(struct dma_chan * chan,void * chan_id)2239e8689e63SLinus Walleij bool pl08x_filter_id(struct dma_chan *chan, void *chan_id)
2240e8689e63SLinus Walleij {
22417703eac9SRussell King - ARM Linux 	struct pl08x_dma_chan *plchan;
2242e8689e63SLinus Walleij 	char *name = chan_id;
2243e8689e63SLinus Walleij 
22447703eac9SRussell King - ARM Linux 	/* Reject channels for devices not bound to this driver */
22457703eac9SRussell King - ARM Linux 	if (chan->device->dev->driver != &pl08x_amba_driver.drv)
22467703eac9SRussell King - ARM Linux 		return false;
22477703eac9SRussell King - ARM Linux 
22487703eac9SRussell King - ARM Linux 	plchan = to_pl08x_chan(chan);
22497703eac9SRussell King - ARM Linux 
2250e8689e63SLinus Walleij 	/* Check that the channel is not taken! */
2251e8689e63SLinus Walleij 	if (!strcmp(plchan->name, name))
2252e8689e63SLinus Walleij 		return true;
2253e8689e63SLinus Walleij 
2254e8689e63SLinus Walleij 	return false;
2255e8689e63SLinus Walleij }
22566d05c9faSSachin Kamat EXPORT_SYMBOL_GPL(pl08x_filter_id);
2257e8689e63SLinus Walleij 
pl08x_filter_fn(struct dma_chan * chan,void * chan_id)2258da6f8ca1SSylwester Nawrocki static bool pl08x_filter_fn(struct dma_chan *chan, void *chan_id)
2259da6f8ca1SSylwester Nawrocki {
2260da6f8ca1SSylwester Nawrocki 	struct pl08x_dma_chan *plchan = to_pl08x_chan(chan);
2261da6f8ca1SSylwester Nawrocki 
2262da6f8ca1SSylwester Nawrocki 	return plchan->cd == chan_id;
2263da6f8ca1SSylwester Nawrocki }
2264da6f8ca1SSylwester Nawrocki 
2265e8689e63SLinus Walleij /*
2266e8689e63SLinus Walleij  * Just check that the device is there and active
226794ae8522SRussell King - ARM Linux  * TODO: turn this bit on/off depending on the number of physical channels
226894ae8522SRussell King - ARM Linux  * actually used, if it is zero... well shut it off. That will save some
226994ae8522SRussell King - ARM Linux  * power. Cut the clock at the same time.
2270e8689e63SLinus Walleij  */
pl08x_ensure_on(struct pl08x_driver_data * pl08x)2271e8689e63SLinus Walleij static void pl08x_ensure_on(struct pl08x_driver_data *pl08x)
2272e8689e63SLinus Walleij {
2273affa115eSLinus Walleij 	/* The Nomadik variant does not have the config register */
2274affa115eSLinus Walleij 	if (pl08x->vd->nomadik)
2275affa115eSLinus Walleij 		return;
22761e1cfc72SLinus Walleij 	/* The FTDMAC020 variant does this in another register */
22771e1cfc72SLinus Walleij 	if (pl08x->vd->ftdmac020) {
22781e1cfc72SLinus Walleij 		writel(PL080_CONFIG_ENABLE, pl08x->base + FTDMAC020_CSR);
22791e1cfc72SLinus Walleij 		return;
22801e1cfc72SLinus Walleij 	}
228148a59ef3SViresh Kumar 	writel(PL080_CONFIG_ENABLE, pl08x->base + PL080_CONFIG);
2282e8689e63SLinus Walleij }
2283e8689e63SLinus Walleij 
pl08x_irq(int irq,void * dev)2284e8689e63SLinus Walleij static irqreturn_t pl08x_irq(int irq, void *dev)
2285e8689e63SLinus Walleij {
2286e8689e63SLinus Walleij 	struct pl08x_driver_data *pl08x = dev;
228728da2836SViresh Kumar 	u32 mask = 0, err, tc, i;
2288e8689e63SLinus Walleij 
228928da2836SViresh Kumar 	/* check & clear - ERR & TC interrupts */
229028da2836SViresh Kumar 	err = readl(pl08x->base + PL080_ERR_STATUS);
229128da2836SViresh Kumar 	if (err) {
229228da2836SViresh Kumar 		dev_err(&pl08x->adev->dev, "%s error interrupt, register value 0x%08x\n",
229328da2836SViresh Kumar 			__func__, err);
229428da2836SViresh Kumar 		writel(err, pl08x->base + PL080_ERR_CLEAR);
2295e8689e63SLinus Walleij 	}
2296d29bf019SLinus Walleij 	tc = readl(pl08x->base + PL080_TC_STATUS);
229728da2836SViresh Kumar 	if (tc)
229828da2836SViresh Kumar 		writel(tc, pl08x->base + PL080_TC_CLEAR);
229928da2836SViresh Kumar 
230028da2836SViresh Kumar 	if (!err && !tc)
230128da2836SViresh Kumar 		return IRQ_NONE;
230228da2836SViresh Kumar 
2303e8689e63SLinus Walleij 	for (i = 0; i < pl08x->vd->channels; i++) {
2304ded091feSLinus Walleij 		if ((BIT(i) & err) || (BIT(i) & tc)) {
2305e8689e63SLinus Walleij 			/* Locate physical channel */
2306e8689e63SLinus Walleij 			struct pl08x_phy_chan *phychan = &pl08x->phy_chans[i];
2307e8689e63SLinus Walleij 			struct pl08x_dma_chan *plchan = phychan->serving;
2308a936e793SRussell King 			struct pl08x_txd *tx;
2309e8689e63SLinus Walleij 
231028da2836SViresh Kumar 			if (!plchan) {
231128da2836SViresh Kumar 				dev_err(&pl08x->adev->dev,
231228da2836SViresh Kumar 					"%s Error TC interrupt on unused channel: 0x%08x\n",
231328da2836SViresh Kumar 					__func__, i);
231428da2836SViresh Kumar 				continue;
231528da2836SViresh Kumar 			}
231628da2836SViresh Kumar 
2317083be28aSRussell King 			spin_lock(&plchan->vc.lock);
2318a936e793SRussell King 			tx = plchan->at;
23193b24c20bSAlban Bedel 			if (tx && tx->cyclic) {
23203b24c20bSAlban Bedel 				vchan_cyclic_callback(&tx->vd);
23213b24c20bSAlban Bedel 			} else if (tx) {
2322a936e793SRussell King 				plchan->at = NULL;
2323c48d4963SRussell King 				/*
2324c48d4963SRussell King 				 * This descriptor is done, release its mux
2325c48d4963SRussell King 				 * reservation.
2326c48d4963SRussell King 				 */
2327c48d4963SRussell King 				pl08x_release_mux(plchan);
232818536134SRussell King 				tx->done = true;
232918536134SRussell King 				vchan_cookie_complete(&tx->vd);
2330c33b644cSRussell King 
2331a5a488dbSRussell King 				/*
2332a5a488dbSRussell King 				 * And start the next descriptor (if any),
2333a5a488dbSRussell King 				 * otherwise free this channel.
2334a5a488dbSRussell King 				 */
2335879f127bSRussell King 				if (vchan_next_desc(&plchan->vc))
2336c33b644cSRussell King 					pl08x_start_next_txd(plchan);
2337a5a488dbSRussell King 				else
2338a5a488dbSRussell King 					pl08x_phy_free(plchan);
2339a936e793SRussell King 			}
2340083be28aSRussell King 			spin_unlock(&plchan->vc.lock);
2341a936e793SRussell King 
2342ded091feSLinus Walleij 			mask |= BIT(i);
2343e8689e63SLinus Walleij 		}
2344e8689e63SLinus Walleij 	}
2345e8689e63SLinus Walleij 
2346e8689e63SLinus Walleij 	return mask ? IRQ_HANDLED : IRQ_NONE;
2347e8689e63SLinus Walleij }
2348e8689e63SLinus Walleij 
pl08x_dma_slave_init(struct pl08x_dma_chan * chan)2349121c8476SRussell King - ARM Linux static void pl08x_dma_slave_init(struct pl08x_dma_chan *chan)
2350121c8476SRussell King - ARM Linux {
2351121c8476SRussell King - ARM Linux 	chan->slave = true;
2352121c8476SRussell King - ARM Linux 	chan->name = chan->cd->bus_id;
2353ed91c13dSRussell King 	chan->cfg.src_addr = chan->cd->addr;
2354ed91c13dSRussell King 	chan->cfg.dst_addr = chan->cd->addr;
2355121c8476SRussell King - ARM Linux }
2356121c8476SRussell King - ARM Linux 
2357e8689e63SLinus Walleij /*
2358e8689e63SLinus Walleij  * Initialise the DMAC memcpy/slave channels.
2359e8689e63SLinus Walleij  * Make a local wrapper to hold required data
2360e8689e63SLinus Walleij  */
pl08x_dma_init_virtual_channels(struct pl08x_driver_data * pl08x,struct dma_device * dmadev,unsigned int channels,bool slave)2361e8689e63SLinus Walleij static int pl08x_dma_init_virtual_channels(struct pl08x_driver_data *pl08x,
23623e27ee84SViresh Kumar 		struct dma_device *dmadev, unsigned int channels, bool slave)
2363e8689e63SLinus Walleij {
2364e8689e63SLinus Walleij 	struct pl08x_dma_chan *chan;
2365e8689e63SLinus Walleij 	int i;
2366e8689e63SLinus Walleij 
2367e8689e63SLinus Walleij 	INIT_LIST_HEAD(&dmadev->channels);
236894ae8522SRussell King - ARM Linux 
2369e8689e63SLinus Walleij 	/*
2370*5c43442fSShaomin Deng 	 * Register as many memcpy as we have physical channels,
2371e8689e63SLinus Walleij 	 * we won't always be able to use all but the code will have
2372e8689e63SLinus Walleij 	 * to cope with that situation.
2373e8689e63SLinus Walleij 	 */
2374e8689e63SLinus Walleij 	for (i = 0; i < channels; i++) {
2375b201c111SViresh Kumar 		chan = kzalloc(sizeof(*chan), GFP_KERNEL);
2376aef94feaSPeter Griffin 		if (!chan)
2377e8689e63SLinus Walleij 			return -ENOMEM;
2378e8689e63SLinus Walleij 
2379e8689e63SLinus Walleij 		chan->host = pl08x;
2380e8689e63SLinus Walleij 		chan->state = PL08X_CHAN_IDLE;
2381ad0de2acSRussell King 		chan->signal = -1;
2382e8689e63SLinus Walleij 
2383e8689e63SLinus Walleij 		if (slave) {
2384e8689e63SLinus Walleij 			chan->cd = &pl08x->pd->slave_channels[i];
2385f9cd4761SLinus Walleij 			/*
2386f9cd4761SLinus Walleij 			 * Some implementations have muxed signals, whereas some
2387f9cd4761SLinus Walleij 			 * use a mux in front of the signals and need dynamic
2388f9cd4761SLinus Walleij 			 * assignment of signals.
2389f9cd4761SLinus Walleij 			 */
2390f9cd4761SLinus Walleij 			chan->signal = i;
2391121c8476SRussell King - ARM Linux 			pl08x_dma_slave_init(chan);
2392e8689e63SLinus Walleij 		} else {
23934166a56aSLinus Walleij 			chan->cd = kzalloc(sizeof(*chan->cd), GFP_KERNEL);
23944166a56aSLinus Walleij 			if (!chan->cd) {
23954166a56aSLinus Walleij 				kfree(chan);
23964166a56aSLinus Walleij 				return -ENOMEM;
23974166a56aSLinus Walleij 			}
23984166a56aSLinus Walleij 			chan->cd->bus_id = "memcpy";
23994166a56aSLinus Walleij 			chan->cd->periph_buses = pl08x->pd->mem_buses;
2400e8689e63SLinus Walleij 			chan->name = kasprintf(GFP_KERNEL, "memcpy%d", i);
2401e8689e63SLinus Walleij 			if (!chan->name) {
24024166a56aSLinus Walleij 				kfree(chan->cd);
2403e8689e63SLinus Walleij 				kfree(chan);
2404e8689e63SLinus Walleij 				return -ENOMEM;
2405e8689e63SLinus Walleij 			}
2406e8689e63SLinus Walleij 		}
2407175a5e61SViresh Kumar 		dev_dbg(&pl08x->adev->dev,
2408e8689e63SLinus Walleij 			 "initialize virtual channel \"%s\"\n",
2409e8689e63SLinus Walleij 			 chan->name);
2410e8689e63SLinus Walleij 
241118536134SRussell King 		chan->vc.desc_free = pl08x_desc_free;
2412083be28aSRussell King 		vchan_init(&chan->vc, dmadev);
2413e8689e63SLinus Walleij 	}
2414e8689e63SLinus Walleij 	dev_info(&pl08x->adev->dev, "initialized %d virtual %s channels\n",
2415e8689e63SLinus Walleij 		 i, slave ? "slave" : "memcpy");
2416e8689e63SLinus Walleij 	return i;
2417e8689e63SLinus Walleij }
2418e8689e63SLinus Walleij 
pl08x_free_virtual_channels(struct dma_device * dmadev)2419e8689e63SLinus Walleij static void pl08x_free_virtual_channels(struct dma_device *dmadev)
2420e8689e63SLinus Walleij {
2421e8689e63SLinus Walleij 	struct pl08x_dma_chan *chan = NULL;
2422e8689e63SLinus Walleij 	struct pl08x_dma_chan *next;
2423e8689e63SLinus Walleij 
2424e8689e63SLinus Walleij 	list_for_each_entry_safe(chan,
242501d8dc64SRussell King 				 next, &dmadev->channels, vc.chan.device_node) {
242601d8dc64SRussell King 		list_del(&chan->vc.chan.device_node);
2427e8689e63SLinus Walleij 		kfree(chan);
2428e8689e63SLinus Walleij 	}
2429e8689e63SLinus Walleij }
2430e8689e63SLinus Walleij 
2431e8689e63SLinus Walleij #ifdef CONFIG_DEBUG_FS
pl08x_state_str(enum pl08x_dma_chan_state state)2432e8689e63SLinus Walleij static const char *pl08x_state_str(enum pl08x_dma_chan_state state)
2433e8689e63SLinus Walleij {
2434e8689e63SLinus Walleij 	switch (state) {
2435e8689e63SLinus Walleij 	case PL08X_CHAN_IDLE:
2436e8689e63SLinus Walleij 		return "idle";
2437e8689e63SLinus Walleij 	case PL08X_CHAN_RUNNING:
2438e8689e63SLinus Walleij 		return "running";
2439e8689e63SLinus Walleij 	case PL08X_CHAN_PAUSED:
2440e8689e63SLinus Walleij 		return "paused";
2441e8689e63SLinus Walleij 	case PL08X_CHAN_WAITING:
2442e8689e63SLinus Walleij 		return "waiting";
2443e8689e63SLinus Walleij 	default:
2444e8689e63SLinus Walleij 		break;
2445e8689e63SLinus Walleij 	}
2446e8689e63SLinus Walleij 	return "UNKNOWN STATE";
2447e8689e63SLinus Walleij }
2448e8689e63SLinus Walleij 
pl08x_debugfs_show(struct seq_file * s,void * data)2449e8689e63SLinus Walleij static int pl08x_debugfs_show(struct seq_file *s, void *data)
2450e8689e63SLinus Walleij {
2451e8689e63SLinus Walleij 	struct pl08x_driver_data *pl08x = s->private;
2452e8689e63SLinus Walleij 	struct pl08x_dma_chan *chan;
2453e8689e63SLinus Walleij 	struct pl08x_phy_chan *ch;
2454e8689e63SLinus Walleij 	unsigned long flags;
2455e8689e63SLinus Walleij 	int i;
2456e8689e63SLinus Walleij 
2457e8689e63SLinus Walleij 	seq_printf(s, "PL08x physical channels:\n");
2458e8689e63SLinus Walleij 	seq_printf(s, "CHANNEL:\tUSER:\n");
2459e8689e63SLinus Walleij 	seq_printf(s, "--------\t-----\n");
2460e8689e63SLinus Walleij 	for (i = 0; i < pl08x->vd->channels; i++) {
2461e8689e63SLinus Walleij 		struct pl08x_dma_chan *virt_chan;
2462e8689e63SLinus Walleij 
2463e8689e63SLinus Walleij 		ch = &pl08x->phy_chans[i];
2464e8689e63SLinus Walleij 
2465e8689e63SLinus Walleij 		spin_lock_irqsave(&ch->lock, flags);
2466e8689e63SLinus Walleij 		virt_chan = ch->serving;
2467e8689e63SLinus Walleij 
2468affa115eSLinus Walleij 		seq_printf(s, "%d\t\t%s%s\n",
2469affa115eSLinus Walleij 			   ch->id,
2470affa115eSLinus Walleij 			   virt_chan ? virt_chan->name : "(none)",
2471affa115eSLinus Walleij 			   ch->locked ? " LOCKED" : "");
2472e8689e63SLinus Walleij 
2473e8689e63SLinus Walleij 		spin_unlock_irqrestore(&ch->lock, flags);
2474e8689e63SLinus Walleij 	}
2475e8689e63SLinus Walleij 
2476e8689e63SLinus Walleij 	seq_printf(s, "\nPL08x virtual memcpy channels:\n");
2477e8689e63SLinus Walleij 	seq_printf(s, "CHANNEL:\tSTATE:\n");
2478e8689e63SLinus Walleij 	seq_printf(s, "--------\t------\n");
247901d8dc64SRussell King 	list_for_each_entry(chan, &pl08x->memcpy.channels, vc.chan.device_node) {
24803e2a037cSRussell King - ARM Linux 		seq_printf(s, "%s\t\t%s\n", chan->name,
2481e8689e63SLinus Walleij 			   pl08x_state_str(chan->state));
2482e8689e63SLinus Walleij 	}
2483e8689e63SLinus Walleij 
2484ebe9b300SLinus Walleij 	if (pl08x->has_slave) {
2485e8689e63SLinus Walleij 		seq_printf(s, "\nPL08x virtual slave channels:\n");
2486e8689e63SLinus Walleij 		seq_printf(s, "CHANNEL:\tSTATE:\n");
2487e8689e63SLinus Walleij 		seq_printf(s, "--------\t------\n");
2488ebe9b300SLinus Walleij 		list_for_each_entry(chan, &pl08x->slave.channels,
2489ebe9b300SLinus Walleij 				    vc.chan.device_node) {
24903e2a037cSRussell King - ARM Linux 			seq_printf(s, "%s\t\t%s\n", chan->name,
2491e8689e63SLinus Walleij 				   pl08x_state_str(chan->state));
2492e8689e63SLinus Walleij 		}
2493ebe9b300SLinus Walleij 	}
2494e8689e63SLinus Walleij 
2495e8689e63SLinus Walleij 	return 0;
2496e8689e63SLinus Walleij }
2497e8689e63SLinus Walleij 
24988e1897bcSYangtao Li DEFINE_SHOW_ATTRIBUTE(pl08x_debugfs);
2499e8689e63SLinus Walleij 
init_pl08x_debugfs(struct pl08x_driver_data * pl08x)2500e8689e63SLinus Walleij static void init_pl08x_debugfs(struct pl08x_driver_data *pl08x)
2501e8689e63SLinus Walleij {
2502e8689e63SLinus Walleij 	/* Expose a simple debugfs interface to view all clocks */
2503718745f8SGreg Kroah-Hartman 	debugfs_create_file(dev_name(&pl08x->adev->dev), S_IFREG | S_IRUGO,
2504718745f8SGreg Kroah-Hartman 			    NULL, pl08x, &pl08x_debugfs_fops);
2505e8689e63SLinus Walleij }
2506e8689e63SLinus Walleij 
2507e8689e63SLinus Walleij #else
init_pl08x_debugfs(struct pl08x_driver_data * pl08x)2508e8689e63SLinus Walleij static inline void init_pl08x_debugfs(struct pl08x_driver_data *pl08x)
2509e8689e63SLinus Walleij {
2510e8689e63SLinus Walleij }
2511e8689e63SLinus Walleij #endif
2512e8689e63SLinus Walleij 
2513aa4734daSLinus Walleij #ifdef CONFIG_OF
pl08x_find_chan_id(struct pl08x_driver_data * pl08x,u32 id)2514aa4734daSLinus Walleij static struct dma_chan *pl08x_find_chan_id(struct pl08x_driver_data *pl08x,
2515aa4734daSLinus Walleij 					 u32 id)
2516aa4734daSLinus Walleij {
2517aa4734daSLinus Walleij 	struct pl08x_dma_chan *chan;
2518aa4734daSLinus Walleij 
2519ebe9b300SLinus Walleij 	/* Trying to get a slave channel from something with no slave support */
2520ebe9b300SLinus Walleij 	if (!pl08x->has_slave)
2521ebe9b300SLinus Walleij 		return NULL;
2522ebe9b300SLinus Walleij 
2523aa4734daSLinus Walleij 	list_for_each_entry(chan, &pl08x->slave.channels, vc.chan.device_node) {
2524aa4734daSLinus Walleij 		if (chan->signal == id)
2525aa4734daSLinus Walleij 			return &chan->vc.chan;
2526aa4734daSLinus Walleij 	}
2527aa4734daSLinus Walleij 
2528aa4734daSLinus Walleij 	return NULL;
2529aa4734daSLinus Walleij }
2530aa4734daSLinus Walleij 
pl08x_of_xlate(struct of_phandle_args * dma_spec,struct of_dma * ofdma)2531aa4734daSLinus Walleij static struct dma_chan *pl08x_of_xlate(struct of_phandle_args *dma_spec,
2532aa4734daSLinus Walleij 				       struct of_dma *ofdma)
2533aa4734daSLinus Walleij {
2534aa4734daSLinus Walleij 	struct pl08x_driver_data *pl08x = ofdma->of_dma_data;
2535aa4734daSLinus Walleij 	struct dma_chan *dma_chan;
2536f9cd4761SLinus Walleij 	struct pl08x_dma_chan *plchan;
2537aa4734daSLinus Walleij 
2538aa4734daSLinus Walleij 	if (!pl08x)
2539aa4734daSLinus Walleij 		return NULL;
2540aa4734daSLinus Walleij 
2541f9cd4761SLinus Walleij 	if (dma_spec->args_count != 2) {
2542f9cd4761SLinus Walleij 		dev_err(&pl08x->adev->dev,
2543f9cd4761SLinus Walleij 			"DMA channel translation requires two cells\n");
2544aa4734daSLinus Walleij 		return NULL;
2545f9cd4761SLinus Walleij 	}
2546aa4734daSLinus Walleij 
2547aa4734daSLinus Walleij 	dma_chan = pl08x_find_chan_id(pl08x, dma_spec->args[0]);
2548f9cd4761SLinus Walleij 	if (!dma_chan) {
2549f9cd4761SLinus Walleij 		dev_err(&pl08x->adev->dev,
2550f9cd4761SLinus Walleij 			"DMA slave channel not found\n");
2551aa4734daSLinus Walleij 		return NULL;
2552f9cd4761SLinus Walleij 	}
2553aa4734daSLinus Walleij 
2554f9cd4761SLinus Walleij 	plchan = to_pl08x_chan(dma_chan);
2555f9cd4761SLinus Walleij 	dev_dbg(&pl08x->adev->dev,
2556f9cd4761SLinus Walleij 		"translated channel for signal %d\n",
2557f9cd4761SLinus Walleij 		dma_spec->args[0]);
2558aa4734daSLinus Walleij 
2559f9cd4761SLinus Walleij 	/* Augment channel data for applicable AHB buses */
2560f9cd4761SLinus Walleij 	plchan->cd->periph_buses = dma_spec->args[1];
2561f9cd4761SLinus Walleij 	return dma_get_slave_channel(dma_chan);
2562aa4734daSLinus Walleij }
2563aa4734daSLinus Walleij 
pl08x_of_probe(struct amba_device * adev,struct pl08x_driver_data * pl08x,struct device_node * np)2564aa4734daSLinus Walleij static int pl08x_of_probe(struct amba_device *adev,
2565aa4734daSLinus Walleij 			  struct pl08x_driver_data *pl08x,
2566aa4734daSLinus Walleij 			  struct device_node *np)
2567aa4734daSLinus Walleij {
2568aa4734daSLinus Walleij 	struct pl08x_platform_data *pd;
2569f9cd4761SLinus Walleij 	struct pl08x_channel_data *chanp = NULL;
2570aa4734daSLinus Walleij 	u32 val;
2571aa4734daSLinus Walleij 	int ret;
2572f9cd4761SLinus Walleij 	int i;
2573aa4734daSLinus Walleij 
2574aa4734daSLinus Walleij 	pd = devm_kzalloc(&adev->dev, sizeof(*pd), GFP_KERNEL);
2575aa4734daSLinus Walleij 	if (!pd)
2576aa4734daSLinus Walleij 		return -ENOMEM;
2577aa4734daSLinus Walleij 
2578aa4734daSLinus Walleij 	/* Eligible bus masters for fetching LLIs */
2579aa4734daSLinus Walleij 	if (of_property_read_bool(np, "lli-bus-interface-ahb1"))
2580aa4734daSLinus Walleij 		pd->lli_buses |= PL08X_AHB1;
2581aa4734daSLinus Walleij 	if (of_property_read_bool(np, "lli-bus-interface-ahb2"))
2582aa4734daSLinus Walleij 		pd->lli_buses |= PL08X_AHB2;
2583aa4734daSLinus Walleij 	if (!pd->lli_buses) {
2584aa4734daSLinus Walleij 		dev_info(&adev->dev, "no bus masters for LLIs stated, assume all\n");
2585aa4734daSLinus Walleij 		pd->lli_buses |= PL08X_AHB1 | PL08X_AHB2;
2586aa4734daSLinus Walleij 	}
2587aa4734daSLinus Walleij 
2588aa4734daSLinus Walleij 	/* Eligible bus masters for memory access */
2589aa4734daSLinus Walleij 	if (of_property_read_bool(np, "mem-bus-interface-ahb1"))
2590aa4734daSLinus Walleij 		pd->mem_buses |= PL08X_AHB1;
2591aa4734daSLinus Walleij 	if (of_property_read_bool(np, "mem-bus-interface-ahb2"))
2592aa4734daSLinus Walleij 		pd->mem_buses |= PL08X_AHB2;
2593aa4734daSLinus Walleij 	if (!pd->mem_buses) {
2594aa4734daSLinus Walleij 		dev_info(&adev->dev, "no bus masters for memory stated, assume all\n");
2595aa4734daSLinus Walleij 		pd->mem_buses |= PL08X_AHB1 | PL08X_AHB2;
2596aa4734daSLinus Walleij 	}
2597aa4734daSLinus Walleij 
2598aa4734daSLinus Walleij 	/* Parse the memcpy channel properties */
2599aa4734daSLinus Walleij 	ret = of_property_read_u32(np, "memcpy-burst-size", &val);
2600aa4734daSLinus Walleij 	if (ret) {
2601aa4734daSLinus Walleij 		dev_info(&adev->dev, "no memcpy burst size specified, using 1 byte\n");
2602aa4734daSLinus Walleij 		val = 1;
2603aa4734daSLinus Walleij 	}
2604aa4734daSLinus Walleij 	switch (val) {
2605aa4734daSLinus Walleij 	default:
2606aa4734daSLinus Walleij 		dev_err(&adev->dev, "illegal burst size for memcpy, set to 1\n");
2607df561f66SGustavo A. R. Silva 		fallthrough;
2608aa4734daSLinus Walleij 	case 1:
26094166a56aSLinus Walleij 		pd->memcpy_burst_size = PL08X_BURST_SZ_1;
2610aa4734daSLinus Walleij 		break;
2611aa4734daSLinus Walleij 	case 4:
26124166a56aSLinus Walleij 		pd->memcpy_burst_size = PL08X_BURST_SZ_4;
2613aa4734daSLinus Walleij 		break;
2614aa4734daSLinus Walleij 	case 8:
26154166a56aSLinus Walleij 		pd->memcpy_burst_size = PL08X_BURST_SZ_8;
2616aa4734daSLinus Walleij 		break;
2617aa4734daSLinus Walleij 	case 16:
26184166a56aSLinus Walleij 		pd->memcpy_burst_size = PL08X_BURST_SZ_16;
2619aa4734daSLinus Walleij 		break;
2620aa4734daSLinus Walleij 	case 32:
26214166a56aSLinus Walleij 		pd->memcpy_burst_size = PL08X_BURST_SZ_32;
2622aa4734daSLinus Walleij 		break;
2623aa4734daSLinus Walleij 	case 64:
26244166a56aSLinus Walleij 		pd->memcpy_burst_size = PL08X_BURST_SZ_64;
2625aa4734daSLinus Walleij 		break;
2626aa4734daSLinus Walleij 	case 128:
26274166a56aSLinus Walleij 		pd->memcpy_burst_size = PL08X_BURST_SZ_128;
2628aa4734daSLinus Walleij 		break;
2629aa4734daSLinus Walleij 	case 256:
26304166a56aSLinus Walleij 		pd->memcpy_burst_size = PL08X_BURST_SZ_256;
2631aa4734daSLinus Walleij 		break;
2632aa4734daSLinus Walleij 	}
2633aa4734daSLinus Walleij 
2634aa4734daSLinus Walleij 	ret = of_property_read_u32(np, "memcpy-bus-width", &val);
2635aa4734daSLinus Walleij 	if (ret) {
2636aa4734daSLinus Walleij 		dev_info(&adev->dev, "no memcpy bus width specified, using 8 bits\n");
2637aa4734daSLinus Walleij 		val = 8;
2638aa4734daSLinus Walleij 	}
2639aa4734daSLinus Walleij 	switch (val) {
2640aa4734daSLinus Walleij 	default:
2641aa4734daSLinus Walleij 		dev_err(&adev->dev, "illegal bus width for memcpy, set to 8 bits\n");
2642df561f66SGustavo A. R. Silva 		fallthrough;
2643aa4734daSLinus Walleij 	case 8:
26444166a56aSLinus Walleij 		pd->memcpy_bus_width = PL08X_BUS_WIDTH_8_BITS;
2645aa4734daSLinus Walleij 		break;
2646aa4734daSLinus Walleij 	case 16:
26474166a56aSLinus Walleij 		pd->memcpy_bus_width = PL08X_BUS_WIDTH_16_BITS;
2648aa4734daSLinus Walleij 		break;
2649aa4734daSLinus Walleij 	case 32:
26504166a56aSLinus Walleij 		pd->memcpy_bus_width = PL08X_BUS_WIDTH_32_BITS;
2651aa4734daSLinus Walleij 		break;
2652aa4734daSLinus Walleij 	}
2653aa4734daSLinus Walleij 
2654f9cd4761SLinus Walleij 	/*
2655f9cd4761SLinus Walleij 	 * Allocate channel data for all possible slave channels (one
2656f9cd4761SLinus Walleij 	 * for each possible signal), channels will then be allocated
2657f9cd4761SLinus Walleij 	 * for a device and have it's AHB interfaces set up at
2658f9cd4761SLinus Walleij 	 * translation time.
2659f9cd4761SLinus Walleij 	 */
2660ebe9b300SLinus Walleij 	if (pl08x->vd->signals) {
2661f9cd4761SLinus Walleij 		chanp = devm_kcalloc(&adev->dev,
2662f9cd4761SLinus Walleij 				     pl08x->vd->signals,
2663f9cd4761SLinus Walleij 				     sizeof(struct pl08x_channel_data),
2664f9cd4761SLinus Walleij 				     GFP_KERNEL);
2665f9cd4761SLinus Walleij 		if (!chanp)
2666f9cd4761SLinus Walleij 			return -ENOMEM;
2667f9cd4761SLinus Walleij 
2668f9cd4761SLinus Walleij 		pd->slave_channels = chanp;
2669f9cd4761SLinus Walleij 		for (i = 0; i < pl08x->vd->signals; i++) {
2670ebe9b300SLinus Walleij 			/*
2671ebe9b300SLinus Walleij 			 * chanp->periph_buses will be assigned at translation
2672ebe9b300SLinus Walleij 			 */
2673f9cd4761SLinus Walleij 			chanp->bus_id = kasprintf(GFP_KERNEL, "slave%d", i);
2674f9cd4761SLinus Walleij 			chanp++;
2675f9cd4761SLinus Walleij 		}
2676f9cd4761SLinus Walleij 		pd->num_slave_channels = pl08x->vd->signals;
2677ebe9b300SLinus Walleij 	}
2678f9cd4761SLinus Walleij 
2679aa4734daSLinus Walleij 	pl08x->pd = pd;
2680aa4734daSLinus Walleij 
2681aa4734daSLinus Walleij 	return of_dma_controller_register(adev->dev.of_node, pl08x_of_xlate,
2682aa4734daSLinus Walleij 					  pl08x);
2683aa4734daSLinus Walleij }
2684aa4734daSLinus Walleij #else
pl08x_of_probe(struct amba_device * adev,struct pl08x_driver_data * pl08x,struct device_node * np)2685aa4734daSLinus Walleij static inline int pl08x_of_probe(struct amba_device *adev,
2686aa4734daSLinus Walleij 				 struct pl08x_driver_data *pl08x,
2687aa4734daSLinus Walleij 				 struct device_node *np)
2688aa4734daSLinus Walleij {
2689aa4734daSLinus Walleij 	return -EINVAL;
2690aa4734daSLinus Walleij }
2691aa4734daSLinus Walleij #endif
2692aa4734daSLinus Walleij 
pl08x_probe(struct amba_device * adev,const struct amba_id * id)2693aa25afadSRussell King static int pl08x_probe(struct amba_device *adev, const struct amba_id *id)
2694e8689e63SLinus Walleij {
2695e8689e63SLinus Walleij 	struct pl08x_driver_data *pl08x;
26961e1cfc72SLinus Walleij 	struct vendor_data *vd = id->data;
2697aa4734daSLinus Walleij 	struct device_node *np = adev->dev.of_node;
2698ba6785ffSTomasz Figa 	u32 tsfr_size;
2699e8689e63SLinus Walleij 	int ret = 0;
2700e8689e63SLinus Walleij 	int i;
2701e8689e63SLinus Walleij 
2702e8689e63SLinus Walleij 	ret = amba_request_regions(adev, NULL);
2703e8689e63SLinus Walleij 	if (ret)
2704e8689e63SLinus Walleij 		return ret;
2705e8689e63SLinus Walleij 
2706de1a2419SRussell King 	/* Ensure that we can do DMA */
2707de1a2419SRussell King 	ret = dma_set_mask_and_coherent(&adev->dev, DMA_BIT_MASK(32));
2708de1a2419SRussell King 	if (ret)
2709de1a2419SRussell King 		goto out_no_pl08x;
2710de1a2419SRussell King 
2711e8689e63SLinus Walleij 	/* Create the driver state holder */
2712b201c111SViresh Kumar 	pl08x = kzalloc(sizeof(*pl08x), GFP_KERNEL);
2713e8689e63SLinus Walleij 	if (!pl08x) {
2714e8689e63SLinus Walleij 		ret = -ENOMEM;
2715e8689e63SLinus Walleij 		goto out_no_pl08x;
2716e8689e63SLinus Walleij 	}
2717e8689e63SLinus Walleij 
2718f9cd4761SLinus Walleij 	/* Assign useful pointers to the driver state */
2719f9cd4761SLinus Walleij 	pl08x->adev = adev;
2720f9cd4761SLinus Walleij 	pl08x->vd = vd;
2721f9cd4761SLinus Walleij 
27221e1cfc72SLinus Walleij 	pl08x->base = ioremap(adev->res.start, resource_size(&adev->res));
27231e1cfc72SLinus Walleij 	if (!pl08x->base) {
27241e1cfc72SLinus Walleij 		ret = -ENOMEM;
27251e1cfc72SLinus Walleij 		goto out_no_ioremap;
27261e1cfc72SLinus Walleij 	}
27271e1cfc72SLinus Walleij 
27281e1cfc72SLinus Walleij 	if (vd->ftdmac020) {
27291e1cfc72SLinus Walleij 		u32 val;
27301e1cfc72SLinus Walleij 
27311e1cfc72SLinus Walleij 		val = readl(pl08x->base + FTDMAC020_REVISION);
27321e1cfc72SLinus Walleij 		dev_info(&pl08x->adev->dev, "FTDMAC020 %d.%d rel %d\n",
27331e1cfc72SLinus Walleij 			 (val >> 16) & 0xff, (val >> 8) & 0xff, val & 0xff);
27341e1cfc72SLinus Walleij 		val = readl(pl08x->base + FTDMAC020_FEATURE);
27351e1cfc72SLinus Walleij 		dev_info(&pl08x->adev->dev, "FTDMAC020 %d channels, "
27361e1cfc72SLinus Walleij 			 "%s built-in bridge, %s, %s linked lists\n",
27371e1cfc72SLinus Walleij 			 (val >> 12) & 0x0f,
27381e1cfc72SLinus Walleij 			 (val & BIT(10)) ? "no" : "has",
27391e1cfc72SLinus Walleij 			 (val & BIT(9)) ? "AHB0 and AHB1" : "AHB0",
27401e1cfc72SLinus Walleij 			 (val & BIT(8)) ? "supports" : "does not support");
27411e1cfc72SLinus Walleij 
27421e1cfc72SLinus Walleij 		/* Vendor data from feature register */
27431e1cfc72SLinus Walleij 		if (!(val & BIT(8)))
27441e1cfc72SLinus Walleij 			dev_warn(&pl08x->adev->dev,
27451e1cfc72SLinus Walleij 				 "linked lists not supported, required\n");
27461e1cfc72SLinus Walleij 		vd->channels = (val >> 12) & 0x0f;
27471e1cfc72SLinus Walleij 		vd->dualmaster = !!(val & BIT(9));
27481e1cfc72SLinus Walleij 	}
27491e1cfc72SLinus Walleij 
2750e8689e63SLinus Walleij 	/* Initialize memcpy engine */
2751e8689e63SLinus Walleij 	dma_cap_set(DMA_MEMCPY, pl08x->memcpy.cap_mask);
2752e8689e63SLinus Walleij 	pl08x->memcpy.dev = &adev->dev;
2753e8689e63SLinus Walleij 	pl08x->memcpy.device_free_chan_resources = pl08x_free_chan_resources;
2754e8689e63SLinus Walleij 	pl08x->memcpy.device_prep_dma_memcpy = pl08x_prep_dma_memcpy;
2755e8689e63SLinus Walleij 	pl08x->memcpy.device_tx_status = pl08x_dma_tx_status;
2756e8689e63SLinus Walleij 	pl08x->memcpy.device_issue_pending = pl08x_issue_pending;
2757bcd1b0b9SMaxime Ripard 	pl08x->memcpy.device_config = pl08x_config;
2758bcd1b0b9SMaxime Ripard 	pl08x->memcpy.device_pause = pl08x_pause;
2759bcd1b0b9SMaxime Ripard 	pl08x->memcpy.device_resume = pl08x_resume;
2760bcd1b0b9SMaxime Ripard 	pl08x->memcpy.device_terminate_all = pl08x_terminate_all;
276147d71bc7SPeter Ujfalusi 	pl08x->memcpy.device_synchronize = pl08x_synchronize;
2762ea524c7eSMark Brown 	pl08x->memcpy.src_addr_widths = PL80X_DMA_BUSWIDTHS;
2763ea524c7eSMark Brown 	pl08x->memcpy.dst_addr_widths = PL80X_DMA_BUSWIDTHS;
2764ea524c7eSMark Brown 	pl08x->memcpy.directions = BIT(DMA_MEM_TO_MEM);
2765ea524c7eSMark Brown 	pl08x->memcpy.residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
27661e1cfc72SLinus Walleij 	if (vd->ftdmac020)
27671e1cfc72SLinus Walleij 		pl08x->memcpy.copy_align = DMAENGINE_ALIGN_4_BYTES;
27681e1cfc72SLinus Walleij 
2769e8689e63SLinus Walleij 
2770ebe9b300SLinus Walleij 	/*
2771ebe9b300SLinus Walleij 	 * Initialize slave engine, if the block has no signals, that means
2772ebe9b300SLinus Walleij 	 * we have no slave support.
2773ebe9b300SLinus Walleij 	 */
2774ebe9b300SLinus Walleij 	if (vd->signals) {
2775ebe9b300SLinus Walleij 		pl08x->has_slave = true;
2776e8689e63SLinus Walleij 		dma_cap_set(DMA_SLAVE, pl08x->slave.cap_mask);
27773b24c20bSAlban Bedel 		dma_cap_set(DMA_CYCLIC, pl08x->slave.cap_mask);
2778e8689e63SLinus Walleij 		pl08x->slave.dev = &adev->dev;
2779ebe9b300SLinus Walleij 		pl08x->slave.device_free_chan_resources =
2780ebe9b300SLinus Walleij 			pl08x_free_chan_resources;
2781e8689e63SLinus Walleij 		pl08x->slave.device_tx_status = pl08x_dma_tx_status;
2782e8689e63SLinus Walleij 		pl08x->slave.device_issue_pending = pl08x_issue_pending;
2783e8689e63SLinus Walleij 		pl08x->slave.device_prep_slave_sg = pl08x_prep_slave_sg;
27843b24c20bSAlban Bedel 		pl08x->slave.device_prep_dma_cyclic = pl08x_prep_dma_cyclic;
2785bcd1b0b9SMaxime Ripard 		pl08x->slave.device_config = pl08x_config;
2786bcd1b0b9SMaxime Ripard 		pl08x->slave.device_pause = pl08x_pause;
2787bcd1b0b9SMaxime Ripard 		pl08x->slave.device_resume = pl08x_resume;
2788bcd1b0b9SMaxime Ripard 		pl08x->slave.device_terminate_all = pl08x_terminate_all;
278947d71bc7SPeter Ujfalusi 		pl08x->slave.device_synchronize = pl08x_synchronize;
2790ea524c7eSMark Brown 		pl08x->slave.src_addr_widths = PL80X_DMA_BUSWIDTHS;
2791ea524c7eSMark Brown 		pl08x->slave.dst_addr_widths = PL80X_DMA_BUSWIDTHS;
2792ebe9b300SLinus Walleij 		pl08x->slave.directions =
2793ebe9b300SLinus Walleij 			BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
2794ebe9b300SLinus Walleij 		pl08x->slave.residue_granularity =
2795ebe9b300SLinus Walleij 			DMA_RESIDUE_GRANULARITY_SEGMENT;
2796ebe9b300SLinus Walleij 	}
2797e8689e63SLinus Walleij 
2798e8689e63SLinus Walleij 	/* Get the platform data */
2799e8689e63SLinus Walleij 	pl08x->pd = dev_get_platdata(&adev->dev);
2800e8689e63SLinus Walleij 	if (!pl08x->pd) {
2801aa4734daSLinus Walleij 		if (np) {
2802aa4734daSLinus Walleij 			ret = pl08x_of_probe(adev, pl08x, np);
2803aa4734daSLinus Walleij 			if (ret)
2804aa4734daSLinus Walleij 				goto out_no_platdata;
2805aa4734daSLinus Walleij 		} else {
2806e8689e63SLinus Walleij 			dev_err(&adev->dev, "no platform data supplied\n");
2807983d7bebSJulia Lawall 			ret = -EINVAL;
2808e8689e63SLinus Walleij 			goto out_no_platdata;
2809e8689e63SLinus Walleij 		}
2810da6f8ca1SSylwester Nawrocki 	} else {
2811da6f8ca1SSylwester Nawrocki 		pl08x->slave.filter.map = pl08x->pd->slave_map;
2812da6f8ca1SSylwester Nawrocki 		pl08x->slave.filter.mapcnt = pl08x->pd->slave_map_len;
2813da6f8ca1SSylwester Nawrocki 		pl08x->slave.filter.fn = pl08x_filter_fn;
2814aa4734daSLinus Walleij 	}
2815e8689e63SLinus Walleij 
281630749cb4SRussell King - ARM Linux 	/* By default, AHB1 only.  If dualmaster, from platform */
281730749cb4SRussell King - ARM Linux 	pl08x->lli_buses = PL08X_AHB1;
281830749cb4SRussell King - ARM Linux 	pl08x->mem_buses = PL08X_AHB1;
281930749cb4SRussell King - ARM Linux 	if (pl08x->vd->dualmaster) {
282030749cb4SRussell King - ARM Linux 		pl08x->lli_buses = pl08x->pd->lli_buses;
282130749cb4SRussell King - ARM Linux 		pl08x->mem_buses = pl08x->pd->mem_buses;
282230749cb4SRussell King - ARM Linux 	}
282330749cb4SRussell King - ARM Linux 
2824da1b6c05STomasz Figa 	if (vd->pl080s)
2825da1b6c05STomasz Figa 		pl08x->lli_words = PL080S_LLI_WORDS;
2826da1b6c05STomasz Figa 	else
2827ba6785ffSTomasz Figa 		pl08x->lli_words = PL080_LLI_WORDS;
2828ba6785ffSTomasz Figa 	tsfr_size = MAX_NUM_TSFR_LLIS * pl08x->lli_words * sizeof(u32);
2829ba6785ffSTomasz Figa 
2830e8689e63SLinus Walleij 	/* A DMA memory pool for LLIs, align on 1-byte boundary */
2831e8689e63SLinus Walleij 	pl08x->pool = dma_pool_create(DRIVER_NAME, &pl08x->adev->dev,
2832ba6785ffSTomasz Figa 						tsfr_size, PL08X_ALIGN, 0);
2833e8689e63SLinus Walleij 	if (!pl08x->pool) {
2834e8689e63SLinus Walleij 		ret = -ENOMEM;
2835e8689e63SLinus Walleij 		goto out_no_lli_pool;
2836e8689e63SLinus Walleij 	}
2837e8689e63SLinus Walleij 
2838e8689e63SLinus Walleij 	/* Turn on the PL08x */
2839e8689e63SLinus Walleij 	pl08x_ensure_on(pl08x);
2840e8689e63SLinus Walleij 
28411e1cfc72SLinus Walleij 	/* Clear any pending interrupts */
28421e1cfc72SLinus Walleij 	if (vd->ftdmac020)
28431e1cfc72SLinus Walleij 		/* This variant has error IRQs in bits 16-19 */
28441e1cfc72SLinus Walleij 		writel(0x0000FFFF, pl08x->base + PL080_ERR_CLEAR);
28451e1cfc72SLinus Walleij 	else
2846e8689e63SLinus Walleij 		writel(0x000000FF, pl08x->base + PL080_ERR_CLEAR);
2847e8689e63SLinus Walleij 	writel(0x000000FF, pl08x->base + PL080_TC_CLEAR);
2848e8689e63SLinus Walleij 
28491e1cfc72SLinus Walleij 	/* Attach the interrupt handler */
2850174b537aSMichael Opdenacker 	ret = request_irq(adev->irq[0], pl08x_irq, 0, DRIVER_NAME, pl08x);
2851e8689e63SLinus Walleij 	if (ret) {
2852e8689e63SLinus Walleij 		dev_err(&adev->dev, "%s failed to request interrupt %d\n",
2853e8689e63SLinus Walleij 			__func__, adev->irq[0]);
2854e8689e63SLinus Walleij 		goto out_no_irq;
2855e8689e63SLinus Walleij 	}
2856e8689e63SLinus Walleij 
2857e8689e63SLinus Walleij 	/* Initialize physical channels */
2858affa115eSLinus Walleij 	pl08x->phy_chans = kzalloc((vd->channels * sizeof(*pl08x->phy_chans)),
2859e8689e63SLinus Walleij 			GFP_KERNEL);
2860e8689e63SLinus Walleij 	if (!pl08x->phy_chans) {
2861983d7bebSJulia Lawall 		ret = -ENOMEM;
2862e8689e63SLinus Walleij 		goto out_no_phychans;
2863e8689e63SLinus Walleij 	}
2864e8689e63SLinus Walleij 
2865e8689e63SLinus Walleij 	for (i = 0; i < vd->channels; i++) {
2866e8689e63SLinus Walleij 		struct pl08x_phy_chan *ch = &pl08x->phy_chans[i];
2867e8689e63SLinus Walleij 
2868e8689e63SLinus Walleij 		ch->id = i;
2869e8689e63SLinus Walleij 		ch->base = pl08x->base + PL080_Cx_BASE(i);
28701e1cfc72SLinus Walleij 		if (vd->ftdmac020) {
28711e1cfc72SLinus Walleij 			/* FTDMA020 has a special channel busy register */
28721e1cfc72SLinus Walleij 			ch->reg_busy = ch->base + FTDMAC020_CH_BUSY;
28731e1cfc72SLinus Walleij 			ch->reg_config = ch->base + FTDMAC020_CH_CFG;
28741e1cfc72SLinus Walleij 			ch->reg_control = ch->base + FTDMAC020_CH_CSR;
28751e1cfc72SLinus Walleij 			ch->reg_src = ch->base + FTDMAC020_CH_SRC_ADDR;
28761e1cfc72SLinus Walleij 			ch->reg_dst = ch->base + FTDMAC020_CH_DST_ADDR;
28771e1cfc72SLinus Walleij 			ch->reg_lli = ch->base + FTDMAC020_CH_LLP;
28781e1cfc72SLinus Walleij 			ch->ftdmac020 = true;
28791e1cfc72SLinus Walleij 		} else {
2880d86ccea7STomasz Figa 			ch->reg_config = ch->base + vd->config_offset;
28811e1cfc72SLinus Walleij 			ch->reg_control = ch->base + PL080_CH_CONTROL;
28821e1cfc72SLinus Walleij 			ch->reg_src = ch->base + PL080_CH_SRC_ADDR;
28831e1cfc72SLinus Walleij 			ch->reg_dst = ch->base + PL080_CH_DST_ADDR;
28841e1cfc72SLinus Walleij 			ch->reg_lli = ch->base + PL080_CH_LLI;
28851e1cfc72SLinus Walleij 		}
28861e1cfc72SLinus Walleij 		if (vd->pl080s)
28871e1cfc72SLinus Walleij 			ch->pl080s = true;
28881e1cfc72SLinus Walleij 
2889e8689e63SLinus Walleij 		spin_lock_init(&ch->lock);
2890affa115eSLinus Walleij 
2891affa115eSLinus Walleij 		/*
2892affa115eSLinus Walleij 		 * Nomadik variants can have channels that are locked
2893affa115eSLinus Walleij 		 * down for the secure world only. Lock up these channels
2894affa115eSLinus Walleij 		 * by perpetually serving a dummy virtual channel.
2895affa115eSLinus Walleij 		 */
2896affa115eSLinus Walleij 		if (vd->nomadik) {
2897affa115eSLinus Walleij 			u32 val;
2898affa115eSLinus Walleij 
2899d86ccea7STomasz Figa 			val = readl(ch->reg_config);
2900affa115eSLinus Walleij 			if (val & (PL080N_CONFIG_ITPROT | PL080N_CONFIG_SECPROT)) {
2901affa115eSLinus Walleij 				dev_info(&adev->dev, "physical channel %d reserved for secure access only\n", i);
2902affa115eSLinus Walleij 				ch->locked = true;
2903affa115eSLinus Walleij 			}
2904affa115eSLinus Walleij 		}
2905affa115eSLinus Walleij 
2906175a5e61SViresh Kumar 		dev_dbg(&adev->dev, "physical channel %d is %s\n",
2907175a5e61SViresh Kumar 			i, pl08x_phy_channel_busy(ch) ? "BUSY" : "FREE");
2908e8689e63SLinus Walleij 	}
2909e8689e63SLinus Walleij 
2910e8689e63SLinus Walleij 	/* Register as many memcpy channels as there are physical channels */
2911e8689e63SLinus Walleij 	ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->memcpy,
2912e8689e63SLinus Walleij 					      pl08x->vd->channels, false);
2913e8689e63SLinus Walleij 	if (ret <= 0) {
2914e8689e63SLinus Walleij 		dev_warn(&pl08x->adev->dev,
2915e8689e63SLinus Walleij 			 "%s failed to enumerate memcpy channels - %d\n",
2916e8689e63SLinus Walleij 			 __func__, ret);
2917e8689e63SLinus Walleij 		goto out_no_memcpy;
2918e8689e63SLinus Walleij 	}
2919e8689e63SLinus Walleij 
2920e8689e63SLinus Walleij 	/* Register slave channels */
2921ebe9b300SLinus Walleij 	if (pl08x->has_slave) {
2922e8689e63SLinus Walleij 		ret = pl08x_dma_init_virtual_channels(pl08x, &pl08x->slave,
29233e27ee84SViresh Kumar 					pl08x->pd->num_slave_channels, true);
29241080411cSLinus Walleij 		if (ret < 0) {
2925e8689e63SLinus Walleij 			dev_warn(&pl08x->adev->dev,
2926e8689e63SLinus Walleij 				 "%s failed to enumerate slave channels - %d\n",
2927e8689e63SLinus Walleij 				 __func__, ret);
2928e8689e63SLinus Walleij 			goto out_no_slave;
2929e8689e63SLinus Walleij 		}
2930ebe9b300SLinus Walleij 	}
2931e8689e63SLinus Walleij 
2932e8689e63SLinus Walleij 	ret = dma_async_device_register(&pl08x->memcpy);
2933e8689e63SLinus Walleij 	if (ret) {
2934e8689e63SLinus Walleij 		dev_warn(&pl08x->adev->dev,
2935e8689e63SLinus Walleij 			"%s failed to register memcpy as an async device - %d\n",
2936e8689e63SLinus Walleij 			__func__, ret);
2937e8689e63SLinus Walleij 		goto out_no_memcpy_reg;
2938e8689e63SLinus Walleij 	}
2939e8689e63SLinus Walleij 
2940ebe9b300SLinus Walleij 	if (pl08x->has_slave) {
2941e8689e63SLinus Walleij 		ret = dma_async_device_register(&pl08x->slave);
2942e8689e63SLinus Walleij 		if (ret) {
2943e8689e63SLinus Walleij 			dev_warn(&pl08x->adev->dev,
2944e8689e63SLinus Walleij 			"%s failed to register slave as an async device - %d\n",
2945e8689e63SLinus Walleij 			__func__, ret);
2946e8689e63SLinus Walleij 			goto out_no_slave_reg;
2947e8689e63SLinus Walleij 		}
2948ebe9b300SLinus Walleij 	}
2949e8689e63SLinus Walleij 
2950e8689e63SLinus Walleij 	amba_set_drvdata(adev, pl08x);
2951e8689e63SLinus Walleij 	init_pl08x_debugfs(pl08x);
2952da1b6c05STomasz Figa 	dev_info(&pl08x->adev->dev, "DMA: PL%03x%s rev%u at 0x%08llx irq %d\n",
2953da1b6c05STomasz Figa 		 amba_part(adev), pl08x->vd->pl080s ? "s" : "", amba_rev(adev),
2954b05cd8f4SRussell King - ARM Linux 		 (unsigned long long)adev->res.start, adev->irq[0]);
2955b7b6018bSViresh Kumar 
2956e8689e63SLinus Walleij 	return 0;
2957e8689e63SLinus Walleij 
2958e8689e63SLinus Walleij out_no_slave_reg:
2959e8689e63SLinus Walleij 	dma_async_device_unregister(&pl08x->memcpy);
2960e8689e63SLinus Walleij out_no_memcpy_reg:
2961ebe9b300SLinus Walleij 	if (pl08x->has_slave)
2962e8689e63SLinus Walleij 		pl08x_free_virtual_channels(&pl08x->slave);
2963e8689e63SLinus Walleij out_no_slave:
2964e8689e63SLinus Walleij 	pl08x_free_virtual_channels(&pl08x->memcpy);
2965e8689e63SLinus Walleij out_no_memcpy:
2966e8689e63SLinus Walleij 	kfree(pl08x->phy_chans);
2967e8689e63SLinus Walleij out_no_phychans:
2968e8689e63SLinus Walleij 	free_irq(adev->irq[0], pl08x);
2969e8689e63SLinus Walleij out_no_irq:
2970e8689e63SLinus Walleij 	dma_pool_destroy(pl08x->pool);
2971e8689e63SLinus Walleij out_no_lli_pool:
2972e8689e63SLinus Walleij out_no_platdata:
29731e1cfc72SLinus Walleij 	iounmap(pl08x->base);
29741e1cfc72SLinus Walleij out_no_ioremap:
2975e8689e63SLinus Walleij 	kfree(pl08x);
2976e8689e63SLinus Walleij out_no_pl08x:
2977e8689e63SLinus Walleij 	amba_release_regions(adev);
2978e8689e63SLinus Walleij 	return ret;
2979e8689e63SLinus Walleij }
2980e8689e63SLinus Walleij 
2981e8689e63SLinus Walleij /* PL080 has 8 channels and the PL080 have just 2 */
2982e8689e63SLinus Walleij static struct vendor_data vendor_pl080 = {
2983d86ccea7STomasz Figa 	.config_offset = PL080_CH_CONFIG,
2984e8689e63SLinus Walleij 	.channels = 8,
2985f9cd4761SLinus Walleij 	.signals = 16,
2986e8689e63SLinus Walleij 	.dualmaster = true,
29875110e51dSTomasz Figa 	.max_transfer_size = PL080_CONTROL_TRANSFER_SIZE_MASK,
2988e8689e63SLinus Walleij };
2989e8689e63SLinus Walleij 
2990affa115eSLinus Walleij static struct vendor_data vendor_nomadik = {
2991d86ccea7STomasz Figa 	.config_offset = PL080_CH_CONFIG,
2992affa115eSLinus Walleij 	.channels = 8,
2993f9cd4761SLinus Walleij 	.signals = 32,
2994affa115eSLinus Walleij 	.dualmaster = true,
2995affa115eSLinus Walleij 	.nomadik = true,
29965110e51dSTomasz Figa 	.max_transfer_size = PL080_CONTROL_TRANSFER_SIZE_MASK,
2997affa115eSLinus Walleij };
2998affa115eSLinus Walleij 
2999da1b6c05STomasz Figa static struct vendor_data vendor_pl080s = {
3000da1b6c05STomasz Figa 	.config_offset = PL080S_CH_CONFIG,
3001da1b6c05STomasz Figa 	.channels = 8,
3002f9cd4761SLinus Walleij 	.signals = 32,
3003da1b6c05STomasz Figa 	.pl080s = true,
30045110e51dSTomasz Figa 	.max_transfer_size = PL080S_CONTROL_TRANSFER_SIZE_MASK,
3005e8689e63SLinus Walleij };
3006e8689e63SLinus Walleij 
3007e8689e63SLinus Walleij static struct vendor_data vendor_pl081 = {
3008d86ccea7STomasz Figa 	.config_offset = PL080_CH_CONFIG,
3009e8689e63SLinus Walleij 	.channels = 2,
3010f9cd4761SLinus Walleij 	.signals = 16,
3011e8689e63SLinus Walleij 	.dualmaster = false,
30125110e51dSTomasz Figa 	.max_transfer_size = PL080_CONTROL_TRANSFER_SIZE_MASK,
3013e8689e63SLinus Walleij };
3014e8689e63SLinus Walleij 
30151e1cfc72SLinus Walleij static struct vendor_data vendor_ftdmac020 = {
30161e1cfc72SLinus Walleij 	.config_offset = PL080_CH_CONFIG,
30171e1cfc72SLinus Walleij 	.ftdmac020 = true,
30181e1cfc72SLinus Walleij 	.max_transfer_size = PL080_CONTROL_TRANSFER_SIZE_MASK,
30191e1cfc72SLinus Walleij };
30201e1cfc72SLinus Walleij 
3021b80fa121SArvind Yadav static const struct amba_id pl08x_ids[] = {
3022da1b6c05STomasz Figa 	/* Samsung PL080S variant */
3023da1b6c05STomasz Figa 	{
3024da1b6c05STomasz Figa 		.id	= 0x0a141080,
3025da1b6c05STomasz Figa 		.mask	= 0xffffffff,
3026da1b6c05STomasz Figa 		.data	= &vendor_pl080s,
3027da1b6c05STomasz Figa 	},
3028e8689e63SLinus Walleij 	/* PL080 */
3029e8689e63SLinus Walleij 	{
3030e8689e63SLinus Walleij 		.id	= 0x00041080,
3031e8689e63SLinus Walleij 		.mask	= 0x000fffff,
3032e8689e63SLinus Walleij 		.data	= &vendor_pl080,
3033e8689e63SLinus Walleij 	},
3034e8689e63SLinus Walleij 	/* PL081 */
3035e8689e63SLinus Walleij 	{
3036e8689e63SLinus Walleij 		.id	= 0x00041081,
3037e8689e63SLinus Walleij 		.mask	= 0x000fffff,
3038e8689e63SLinus Walleij 		.data	= &vendor_pl081,
3039e8689e63SLinus Walleij 	},
3040e8689e63SLinus Walleij 	/* Nomadik 8815 PL080 variant */
3041e8689e63SLinus Walleij 	{
3042affa115eSLinus Walleij 		.id	= 0x00280080,
3043e8689e63SLinus Walleij 		.mask	= 0x00ffffff,
3044affa115eSLinus Walleij 		.data	= &vendor_nomadik,
3045e8689e63SLinus Walleij 	},
30461e1cfc72SLinus Walleij 	/* Faraday Technology FTDMAC020 */
30471e1cfc72SLinus Walleij 	{
30481e1cfc72SLinus Walleij 		.id	= 0x0003b080,
30491e1cfc72SLinus Walleij 		.mask	= 0x000fffff,
30501e1cfc72SLinus Walleij 		.data	= &vendor_ftdmac020,
30511e1cfc72SLinus Walleij 	},
3052e8689e63SLinus Walleij 	{ 0, 0 },
3053e8689e63SLinus Walleij };
3054e8689e63SLinus Walleij 
3055037566dfSDave Martin MODULE_DEVICE_TABLE(amba, pl08x_ids);
3056037566dfSDave Martin 
3057e8689e63SLinus Walleij static struct amba_driver pl08x_amba_driver = {
3058e8689e63SLinus Walleij 	.drv.name	= DRIVER_NAME,
3059e8689e63SLinus Walleij 	.id_table	= pl08x_ids,
3060e8689e63SLinus Walleij 	.probe		= pl08x_probe,
3061e8689e63SLinus Walleij };
3062e8689e63SLinus Walleij 
pl08x_init(void)3063e8689e63SLinus Walleij static int __init pl08x_init(void)
3064e8689e63SLinus Walleij {
3065e8689e63SLinus Walleij 	int retval;
3066e8689e63SLinus Walleij 	retval = amba_driver_register(&pl08x_amba_driver);
3067e8689e63SLinus Walleij 	if (retval)
3068e8689e63SLinus Walleij 		printk(KERN_WARNING DRIVER_NAME
3069e8b5e11dSRussell King - ARM Linux 		       "failed to register as an AMBA device (%d)\n",
3070e8689e63SLinus Walleij 		       retval);
3071e8689e63SLinus Walleij 	return retval;
3072e8689e63SLinus Walleij }
3073e8689e63SLinus Walleij subsys_initcall(pl08x_init);
3074