xref: /OK3568_Linux_fs/kernel/drivers/dma/s3c24xx-dma.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * S3C24XX DMA handling
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 2013 Heiko Stuebner <heiko@sntech.de>
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * based on amba-pl08x.c
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * Copyright (c) 2006 ARM Ltd.
10*4882a593Smuzhiyun  * Copyright (c) 2010 ST-Ericsson SA
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * Author: Peter Pearse <peter.pearse@arm.com>
13*4882a593Smuzhiyun  * Author: Linus Walleij <linus.walleij@stericsson.com>
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * The DMA controllers in S3C24XX SoCs have a varying number of DMA signals
16*4882a593Smuzhiyun  * that can be routed to any of the 4 to 8 hardware-channels.
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  * Therefore on these DMA controllers the number of channels
19*4882a593Smuzhiyun  * and the number of incoming DMA signals are two totally different things.
20*4882a593Smuzhiyun  * It is usually not possible to theoretically handle all physical signals,
21*4882a593Smuzhiyun  * so a multiplexing scheme with possible denial of use is necessary.
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * Open items:
24*4882a593Smuzhiyun  * - bursts
25*4882a593Smuzhiyun  */
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #include <linux/platform_device.h>
28*4882a593Smuzhiyun #include <linux/types.h>
29*4882a593Smuzhiyun #include <linux/dmaengine.h>
30*4882a593Smuzhiyun #include <linux/dma-mapping.h>
31*4882a593Smuzhiyun #include <linux/interrupt.h>
32*4882a593Smuzhiyun #include <linux/clk.h>
33*4882a593Smuzhiyun #include <linux/module.h>
34*4882a593Smuzhiyun #include <linux/mod_devicetable.h>
35*4882a593Smuzhiyun #include <linux/slab.h>
36*4882a593Smuzhiyun #include <linux/platform_data/dma-s3c24xx.h>
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun #include "dmaengine.h"
39*4882a593Smuzhiyun #include "virt-dma.h"
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun #define MAX_DMA_CHANNELS	8
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun #define S3C24XX_DISRC			0x00
44*4882a593Smuzhiyun #define S3C24XX_DISRCC			0x04
45*4882a593Smuzhiyun #define S3C24XX_DISRCC_INC_INCREMENT	0
46*4882a593Smuzhiyun #define S3C24XX_DISRCC_INC_FIXED	BIT(0)
47*4882a593Smuzhiyun #define S3C24XX_DISRCC_LOC_AHB		0
48*4882a593Smuzhiyun #define S3C24XX_DISRCC_LOC_APB		BIT(1)
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun #define S3C24XX_DIDST			0x08
51*4882a593Smuzhiyun #define S3C24XX_DIDSTC			0x0c
52*4882a593Smuzhiyun #define S3C24XX_DIDSTC_INC_INCREMENT	0
53*4882a593Smuzhiyun #define S3C24XX_DIDSTC_INC_FIXED	BIT(0)
54*4882a593Smuzhiyun #define S3C24XX_DIDSTC_LOC_AHB		0
55*4882a593Smuzhiyun #define S3C24XX_DIDSTC_LOC_APB		BIT(1)
56*4882a593Smuzhiyun #define S3C24XX_DIDSTC_INT_TC0		0
57*4882a593Smuzhiyun #define S3C24XX_DIDSTC_INT_RELOAD	BIT(2)
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun #define S3C24XX_DCON			0x10
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun #define S3C24XX_DCON_TC_MASK		0xfffff
62*4882a593Smuzhiyun #define S3C24XX_DCON_DSZ_BYTE		(0 << 20)
63*4882a593Smuzhiyun #define S3C24XX_DCON_DSZ_HALFWORD	(1 << 20)
64*4882a593Smuzhiyun #define S3C24XX_DCON_DSZ_WORD		(2 << 20)
65*4882a593Smuzhiyun #define S3C24XX_DCON_DSZ_MASK		(3 << 20)
66*4882a593Smuzhiyun #define S3C24XX_DCON_DSZ_SHIFT		20
67*4882a593Smuzhiyun #define S3C24XX_DCON_AUTORELOAD		0
68*4882a593Smuzhiyun #define S3C24XX_DCON_NORELOAD		BIT(22)
69*4882a593Smuzhiyun #define S3C24XX_DCON_HWTRIG		BIT(23)
70*4882a593Smuzhiyun #define S3C24XX_DCON_HWSRC_SHIFT	24
71*4882a593Smuzhiyun #define S3C24XX_DCON_SERV_SINGLE	0
72*4882a593Smuzhiyun #define S3C24XX_DCON_SERV_WHOLE		BIT(27)
73*4882a593Smuzhiyun #define S3C24XX_DCON_TSZ_UNIT		0
74*4882a593Smuzhiyun #define S3C24XX_DCON_TSZ_BURST4		BIT(28)
75*4882a593Smuzhiyun #define S3C24XX_DCON_INT		BIT(29)
76*4882a593Smuzhiyun #define S3C24XX_DCON_SYNC_PCLK		0
77*4882a593Smuzhiyun #define S3C24XX_DCON_SYNC_HCLK		BIT(30)
78*4882a593Smuzhiyun #define S3C24XX_DCON_DEMAND		0
79*4882a593Smuzhiyun #define S3C24XX_DCON_HANDSHAKE		BIT(31)
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun #define S3C24XX_DSTAT			0x14
82*4882a593Smuzhiyun #define S3C24XX_DSTAT_STAT_BUSY		BIT(20)
83*4882a593Smuzhiyun #define S3C24XX_DSTAT_CURRTC_MASK	0xfffff
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun #define S3C24XX_DMASKTRIG		0x20
86*4882a593Smuzhiyun #define S3C24XX_DMASKTRIG_SWTRIG	BIT(0)
87*4882a593Smuzhiyun #define S3C24XX_DMASKTRIG_ON		BIT(1)
88*4882a593Smuzhiyun #define S3C24XX_DMASKTRIG_STOP		BIT(2)
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun #define S3C24XX_DMAREQSEL		0x24
91*4882a593Smuzhiyun #define S3C24XX_DMAREQSEL_HW		BIT(0)
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun /*
94*4882a593Smuzhiyun  * S3C2410, S3C2440 and S3C2442 SoCs cannot select any physical channel
95*4882a593Smuzhiyun  * for a DMA source. Instead only specific channels are valid.
96*4882a593Smuzhiyun  * All of these SoCs have 4 physical channels and the number of request
97*4882a593Smuzhiyun  * source bits is 3. Additionally we also need 1 bit to mark the channel
98*4882a593Smuzhiyun  * as valid.
99*4882a593Smuzhiyun  * Therefore we separate the chansel element of the channel data into 4
100*4882a593Smuzhiyun  * parts of 4 bits each, to hold the information if the channel is valid
101*4882a593Smuzhiyun  * and the hw request source to use.
102*4882a593Smuzhiyun  *
103*4882a593Smuzhiyun  * Example:
104*4882a593Smuzhiyun  * SDI is valid on channels 0, 2 and 3 - with varying hw request sources.
105*4882a593Smuzhiyun  * For it the chansel field would look like
106*4882a593Smuzhiyun  *
107*4882a593Smuzhiyun  * ((BIT(3) | 1) << 3 * 4) | // channel 3, with request source 1
108*4882a593Smuzhiyun  * ((BIT(3) | 2) << 2 * 4) | // channel 2, with request source 2
109*4882a593Smuzhiyun  * ((BIT(3) | 2) << 0 * 4)   // channel 0, with request source 2
110*4882a593Smuzhiyun  */
111*4882a593Smuzhiyun #define S3C24XX_CHANSEL_WIDTH		4
112*4882a593Smuzhiyun #define S3C24XX_CHANSEL_VALID		BIT(3)
113*4882a593Smuzhiyun #define S3C24XX_CHANSEL_REQ_MASK	7
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun /*
116*4882a593Smuzhiyun  * struct soc_data - vendor-specific config parameters for individual SoCs
117*4882a593Smuzhiyun  * @stride: spacing between the registers of each channel
118*4882a593Smuzhiyun  * @has_reqsel: does the controller use the newer requestselection mechanism
119*4882a593Smuzhiyun  * @has_clocks: are controllable dma-clocks present
120*4882a593Smuzhiyun  */
121*4882a593Smuzhiyun struct soc_data {
122*4882a593Smuzhiyun 	int stride;
123*4882a593Smuzhiyun 	bool has_reqsel;
124*4882a593Smuzhiyun 	bool has_clocks;
125*4882a593Smuzhiyun };
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun /*
128*4882a593Smuzhiyun  * enum s3c24xx_dma_chan_state - holds the virtual channel states
129*4882a593Smuzhiyun  * @S3C24XX_DMA_CHAN_IDLE: the channel is idle
130*4882a593Smuzhiyun  * @S3C24XX_DMA_CHAN_RUNNING: the channel has allocated a physical transport
131*4882a593Smuzhiyun  * channel and is running a transfer on it
132*4882a593Smuzhiyun  * @S3C24XX_DMA_CHAN_WAITING: the channel is waiting for a physical transport
133*4882a593Smuzhiyun  * channel to become available (only pertains to memcpy channels)
134*4882a593Smuzhiyun  */
135*4882a593Smuzhiyun enum s3c24xx_dma_chan_state {
136*4882a593Smuzhiyun 	S3C24XX_DMA_CHAN_IDLE,
137*4882a593Smuzhiyun 	S3C24XX_DMA_CHAN_RUNNING,
138*4882a593Smuzhiyun 	S3C24XX_DMA_CHAN_WAITING,
139*4882a593Smuzhiyun };
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun /*
142*4882a593Smuzhiyun  * struct s3c24xx_sg - structure containing data per sg
143*4882a593Smuzhiyun  * @src_addr: src address of sg
144*4882a593Smuzhiyun  * @dst_addr: dst address of sg
145*4882a593Smuzhiyun  * @len: transfer len in bytes
146*4882a593Smuzhiyun  * @node: node for txd's dsg_list
147*4882a593Smuzhiyun  */
148*4882a593Smuzhiyun struct s3c24xx_sg {
149*4882a593Smuzhiyun 	dma_addr_t src_addr;
150*4882a593Smuzhiyun 	dma_addr_t dst_addr;
151*4882a593Smuzhiyun 	size_t len;
152*4882a593Smuzhiyun 	struct list_head node;
153*4882a593Smuzhiyun };
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun /*
156*4882a593Smuzhiyun  * struct s3c24xx_txd - wrapper for struct dma_async_tx_descriptor
157*4882a593Smuzhiyun  * @vd: virtual DMA descriptor
158*4882a593Smuzhiyun  * @dsg_list: list of children sg's
159*4882a593Smuzhiyun  * @at: sg currently being transfered
160*4882a593Smuzhiyun  * @width: transfer width
161*4882a593Smuzhiyun  * @disrcc: value for source control register
162*4882a593Smuzhiyun  * @didstc: value for destination control register
163*4882a593Smuzhiyun  * @dcon: base value for dcon register
164*4882a593Smuzhiyun  * @cyclic: indicate cyclic transfer
165*4882a593Smuzhiyun  */
166*4882a593Smuzhiyun struct s3c24xx_txd {
167*4882a593Smuzhiyun 	struct virt_dma_desc vd;
168*4882a593Smuzhiyun 	struct list_head dsg_list;
169*4882a593Smuzhiyun 	struct list_head *at;
170*4882a593Smuzhiyun 	u8 width;
171*4882a593Smuzhiyun 	u32 disrcc;
172*4882a593Smuzhiyun 	u32 didstc;
173*4882a593Smuzhiyun 	u32 dcon;
174*4882a593Smuzhiyun 	bool cyclic;
175*4882a593Smuzhiyun };
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun struct s3c24xx_dma_chan;
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun /*
180*4882a593Smuzhiyun  * struct s3c24xx_dma_phy - holder for the physical channels
181*4882a593Smuzhiyun  * @id: physical index to this channel
182*4882a593Smuzhiyun  * @valid: does the channel have all required elements
183*4882a593Smuzhiyun  * @base: virtual memory base (remapped) for the this channel
184*4882a593Smuzhiyun  * @irq: interrupt for this channel
185*4882a593Smuzhiyun  * @clk: clock for this channel
186*4882a593Smuzhiyun  * @lock: a lock to use when altering an instance of this struct
187*4882a593Smuzhiyun  * @serving: virtual channel currently being served by this physicalchannel
188*4882a593Smuzhiyun  * @host: a pointer to the host (internal use)
189*4882a593Smuzhiyun  */
190*4882a593Smuzhiyun struct s3c24xx_dma_phy {
191*4882a593Smuzhiyun 	unsigned int			id;
192*4882a593Smuzhiyun 	bool				valid;
193*4882a593Smuzhiyun 	void __iomem			*base;
194*4882a593Smuzhiyun 	int				irq;
195*4882a593Smuzhiyun 	struct clk			*clk;
196*4882a593Smuzhiyun 	spinlock_t			lock;
197*4882a593Smuzhiyun 	struct s3c24xx_dma_chan		*serving;
198*4882a593Smuzhiyun 	struct s3c24xx_dma_engine	*host;
199*4882a593Smuzhiyun };
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun /*
202*4882a593Smuzhiyun  * struct s3c24xx_dma_chan - this structure wraps a DMA ENGINE channel
203*4882a593Smuzhiyun  * @id: the id of the channel
204*4882a593Smuzhiyun  * @name: name of the channel
205*4882a593Smuzhiyun  * @vc: wrappped virtual channel
206*4882a593Smuzhiyun  * @phy: the physical channel utilized by this channel, if there is one
207*4882a593Smuzhiyun  * @runtime_addr: address for RX/TX according to the runtime config
208*4882a593Smuzhiyun  * @at: active transaction on this channel
209*4882a593Smuzhiyun  * @lock: a lock for this channel data
210*4882a593Smuzhiyun  * @host: a pointer to the host (internal use)
211*4882a593Smuzhiyun  * @state: whether the channel is idle, running etc
212*4882a593Smuzhiyun  * @slave: whether this channel is a device (slave) or for memcpy
213*4882a593Smuzhiyun  */
214*4882a593Smuzhiyun struct s3c24xx_dma_chan {
215*4882a593Smuzhiyun 	int id;
216*4882a593Smuzhiyun 	const char *name;
217*4882a593Smuzhiyun 	struct virt_dma_chan vc;
218*4882a593Smuzhiyun 	struct s3c24xx_dma_phy *phy;
219*4882a593Smuzhiyun 	struct dma_slave_config cfg;
220*4882a593Smuzhiyun 	struct s3c24xx_txd *at;
221*4882a593Smuzhiyun 	struct s3c24xx_dma_engine *host;
222*4882a593Smuzhiyun 	enum s3c24xx_dma_chan_state state;
223*4882a593Smuzhiyun 	bool slave;
224*4882a593Smuzhiyun };
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun /*
227*4882a593Smuzhiyun  * struct s3c24xx_dma_engine - the local state holder for the S3C24XX
228*4882a593Smuzhiyun  * @pdev: the corresponding platform device
229*4882a593Smuzhiyun  * @pdata: platform data passed in from the platform/machine
230*4882a593Smuzhiyun  * @base: virtual memory base (remapped)
231*4882a593Smuzhiyun  * @slave: slave engine for this instance
232*4882a593Smuzhiyun  * @memcpy: memcpy engine for this instance
233*4882a593Smuzhiyun  * @phy_chans: array of data for the physical channels
234*4882a593Smuzhiyun  */
235*4882a593Smuzhiyun struct s3c24xx_dma_engine {
236*4882a593Smuzhiyun 	struct platform_device			*pdev;
237*4882a593Smuzhiyun 	const struct s3c24xx_dma_platdata	*pdata;
238*4882a593Smuzhiyun 	struct soc_data				*sdata;
239*4882a593Smuzhiyun 	void __iomem				*base;
240*4882a593Smuzhiyun 	struct dma_device			slave;
241*4882a593Smuzhiyun 	struct dma_device			memcpy;
242*4882a593Smuzhiyun 	struct s3c24xx_dma_phy			*phy_chans;
243*4882a593Smuzhiyun };
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun /*
246*4882a593Smuzhiyun  * Physical channel handling
247*4882a593Smuzhiyun  */
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun /*
250*4882a593Smuzhiyun  * Check whether a certain channel is busy or not.
251*4882a593Smuzhiyun  */
s3c24xx_dma_phy_busy(struct s3c24xx_dma_phy * phy)252*4882a593Smuzhiyun static int s3c24xx_dma_phy_busy(struct s3c24xx_dma_phy *phy)
253*4882a593Smuzhiyun {
254*4882a593Smuzhiyun 	unsigned int val = readl(phy->base + S3C24XX_DSTAT);
255*4882a593Smuzhiyun 	return val & S3C24XX_DSTAT_STAT_BUSY;
256*4882a593Smuzhiyun }
257*4882a593Smuzhiyun 
s3c24xx_dma_phy_valid(struct s3c24xx_dma_chan * s3cchan,struct s3c24xx_dma_phy * phy)258*4882a593Smuzhiyun static bool s3c24xx_dma_phy_valid(struct s3c24xx_dma_chan *s3cchan,
259*4882a593Smuzhiyun 				  struct s3c24xx_dma_phy *phy)
260*4882a593Smuzhiyun {
261*4882a593Smuzhiyun 	struct s3c24xx_dma_engine *s3cdma = s3cchan->host;
262*4882a593Smuzhiyun 	const struct s3c24xx_dma_platdata *pdata = s3cdma->pdata;
263*4882a593Smuzhiyun 	struct s3c24xx_dma_channel *cdata = &pdata->channels[s3cchan->id];
264*4882a593Smuzhiyun 	int phyvalid;
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	/* every phy is valid for memcopy channels */
267*4882a593Smuzhiyun 	if (!s3cchan->slave)
268*4882a593Smuzhiyun 		return true;
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	/* On newer variants all phys can be used for all virtual channels */
271*4882a593Smuzhiyun 	if (s3cdma->sdata->has_reqsel)
272*4882a593Smuzhiyun 		return true;
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	phyvalid = (cdata->chansel >> (phy->id * S3C24XX_CHANSEL_WIDTH));
275*4882a593Smuzhiyun 	return (phyvalid & S3C24XX_CHANSEL_VALID) ? true : false;
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun /*
279*4882a593Smuzhiyun  * Allocate a physical channel for a virtual channel
280*4882a593Smuzhiyun  *
281*4882a593Smuzhiyun  * Try to locate a physical channel to be used for this transfer. If all
282*4882a593Smuzhiyun  * are taken return NULL and the requester will have to cope by using
283*4882a593Smuzhiyun  * some fallback PIO mode or retrying later.
284*4882a593Smuzhiyun  */
285*4882a593Smuzhiyun static
s3c24xx_dma_get_phy(struct s3c24xx_dma_chan * s3cchan)286*4882a593Smuzhiyun struct s3c24xx_dma_phy *s3c24xx_dma_get_phy(struct s3c24xx_dma_chan *s3cchan)
287*4882a593Smuzhiyun {
288*4882a593Smuzhiyun 	struct s3c24xx_dma_engine *s3cdma = s3cchan->host;
289*4882a593Smuzhiyun 	struct s3c24xx_dma_phy *phy = NULL;
290*4882a593Smuzhiyun 	unsigned long flags;
291*4882a593Smuzhiyun 	int i;
292*4882a593Smuzhiyun 	int ret;
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	for (i = 0; i < s3cdma->pdata->num_phy_channels; i++) {
295*4882a593Smuzhiyun 		phy = &s3cdma->phy_chans[i];
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 		if (!phy->valid)
298*4882a593Smuzhiyun 			continue;
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 		if (!s3c24xx_dma_phy_valid(s3cchan, phy))
301*4882a593Smuzhiyun 			continue;
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 		spin_lock_irqsave(&phy->lock, flags);
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun 		if (!phy->serving) {
306*4882a593Smuzhiyun 			phy->serving = s3cchan;
307*4882a593Smuzhiyun 			spin_unlock_irqrestore(&phy->lock, flags);
308*4882a593Smuzhiyun 			break;
309*4882a593Smuzhiyun 		}
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 		spin_unlock_irqrestore(&phy->lock, flags);
312*4882a593Smuzhiyun 	}
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	/* No physical channel available, cope with it */
315*4882a593Smuzhiyun 	if (i == s3cdma->pdata->num_phy_channels) {
316*4882a593Smuzhiyun 		dev_warn(&s3cdma->pdev->dev, "no phy channel available\n");
317*4882a593Smuzhiyun 		return NULL;
318*4882a593Smuzhiyun 	}
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 	/* start the phy clock */
321*4882a593Smuzhiyun 	if (s3cdma->sdata->has_clocks) {
322*4882a593Smuzhiyun 		ret = clk_enable(phy->clk);
323*4882a593Smuzhiyun 		if (ret) {
324*4882a593Smuzhiyun 			dev_err(&s3cdma->pdev->dev, "could not enable clock for channel %d, err %d\n",
325*4882a593Smuzhiyun 				phy->id, ret);
326*4882a593Smuzhiyun 			phy->serving = NULL;
327*4882a593Smuzhiyun 			return NULL;
328*4882a593Smuzhiyun 		}
329*4882a593Smuzhiyun 	}
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	return phy;
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun /*
335*4882a593Smuzhiyun  * Mark the physical channel as free.
336*4882a593Smuzhiyun  *
337*4882a593Smuzhiyun  * This drops the link between the physical and virtual channel.
338*4882a593Smuzhiyun  */
s3c24xx_dma_put_phy(struct s3c24xx_dma_phy * phy)339*4882a593Smuzhiyun static inline void s3c24xx_dma_put_phy(struct s3c24xx_dma_phy *phy)
340*4882a593Smuzhiyun {
341*4882a593Smuzhiyun 	struct s3c24xx_dma_engine *s3cdma = phy->host;
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 	if (s3cdma->sdata->has_clocks)
344*4882a593Smuzhiyun 		clk_disable(phy->clk);
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 	phy->serving = NULL;
347*4882a593Smuzhiyun }
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun /*
350*4882a593Smuzhiyun  * Stops the channel by writing the stop bit.
351*4882a593Smuzhiyun  * This should not be used for an on-going transfer, but as a method of
352*4882a593Smuzhiyun  * shutting down a channel (eg, when it's no longer used) or terminating a
353*4882a593Smuzhiyun  * transfer.
354*4882a593Smuzhiyun  */
s3c24xx_dma_terminate_phy(struct s3c24xx_dma_phy * phy)355*4882a593Smuzhiyun static void s3c24xx_dma_terminate_phy(struct s3c24xx_dma_phy *phy)
356*4882a593Smuzhiyun {
357*4882a593Smuzhiyun 	writel(S3C24XX_DMASKTRIG_STOP, phy->base + S3C24XX_DMASKTRIG);
358*4882a593Smuzhiyun }
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun /*
361*4882a593Smuzhiyun  * Virtual channel handling
362*4882a593Smuzhiyun  */
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun static inline
to_s3c24xx_dma_chan(struct dma_chan * chan)365*4882a593Smuzhiyun struct s3c24xx_dma_chan *to_s3c24xx_dma_chan(struct dma_chan *chan)
366*4882a593Smuzhiyun {
367*4882a593Smuzhiyun 	return container_of(chan, struct s3c24xx_dma_chan, vc.chan);
368*4882a593Smuzhiyun }
369*4882a593Smuzhiyun 
s3c24xx_dma_getbytes_chan(struct s3c24xx_dma_chan * s3cchan)370*4882a593Smuzhiyun static u32 s3c24xx_dma_getbytes_chan(struct s3c24xx_dma_chan *s3cchan)
371*4882a593Smuzhiyun {
372*4882a593Smuzhiyun 	struct s3c24xx_dma_phy *phy = s3cchan->phy;
373*4882a593Smuzhiyun 	struct s3c24xx_txd *txd = s3cchan->at;
374*4882a593Smuzhiyun 	u32 tc = readl(phy->base + S3C24XX_DSTAT) & S3C24XX_DSTAT_CURRTC_MASK;
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	return tc * txd->width;
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun 
s3c24xx_dma_set_runtime_config(struct dma_chan * chan,struct dma_slave_config * config)379*4882a593Smuzhiyun static int s3c24xx_dma_set_runtime_config(struct dma_chan *chan,
380*4882a593Smuzhiyun 				  struct dma_slave_config *config)
381*4882a593Smuzhiyun {
382*4882a593Smuzhiyun 	struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(chan);
383*4882a593Smuzhiyun 	unsigned long flags;
384*4882a593Smuzhiyun 	int ret = 0;
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 	/* Reject definitely invalid configurations */
387*4882a593Smuzhiyun 	if (config->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
388*4882a593Smuzhiyun 	    config->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
389*4882a593Smuzhiyun 		return -EINVAL;
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 	spin_lock_irqsave(&s3cchan->vc.lock, flags);
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun 	if (!s3cchan->slave) {
394*4882a593Smuzhiyun 		ret = -EINVAL;
395*4882a593Smuzhiyun 		goto out;
396*4882a593Smuzhiyun 	}
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun 	s3cchan->cfg = *config;
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun out:
401*4882a593Smuzhiyun 	spin_unlock_irqrestore(&s3cchan->vc.lock, flags);
402*4882a593Smuzhiyun 	return ret;
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun /*
406*4882a593Smuzhiyun  * Transfer handling
407*4882a593Smuzhiyun  */
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun static inline
to_s3c24xx_txd(struct dma_async_tx_descriptor * tx)410*4882a593Smuzhiyun struct s3c24xx_txd *to_s3c24xx_txd(struct dma_async_tx_descriptor *tx)
411*4882a593Smuzhiyun {
412*4882a593Smuzhiyun 	return container_of(tx, struct s3c24xx_txd, vd.tx);
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun 
s3c24xx_dma_get_txd(void)415*4882a593Smuzhiyun static struct s3c24xx_txd *s3c24xx_dma_get_txd(void)
416*4882a593Smuzhiyun {
417*4882a593Smuzhiyun 	struct s3c24xx_txd *txd = kzalloc(sizeof(*txd), GFP_NOWAIT);
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun 	if (txd) {
420*4882a593Smuzhiyun 		INIT_LIST_HEAD(&txd->dsg_list);
421*4882a593Smuzhiyun 		txd->dcon = S3C24XX_DCON_INT | S3C24XX_DCON_NORELOAD;
422*4882a593Smuzhiyun 	}
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun 	return txd;
425*4882a593Smuzhiyun }
426*4882a593Smuzhiyun 
s3c24xx_dma_free_txd(struct s3c24xx_txd * txd)427*4882a593Smuzhiyun static void s3c24xx_dma_free_txd(struct s3c24xx_txd *txd)
428*4882a593Smuzhiyun {
429*4882a593Smuzhiyun 	struct s3c24xx_sg *dsg, *_dsg;
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 	list_for_each_entry_safe(dsg, _dsg, &txd->dsg_list, node) {
432*4882a593Smuzhiyun 		list_del(&dsg->node);
433*4882a593Smuzhiyun 		kfree(dsg);
434*4882a593Smuzhiyun 	}
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 	kfree(txd);
437*4882a593Smuzhiyun }
438*4882a593Smuzhiyun 
s3c24xx_dma_start_next_sg(struct s3c24xx_dma_chan * s3cchan,struct s3c24xx_txd * txd)439*4882a593Smuzhiyun static void s3c24xx_dma_start_next_sg(struct s3c24xx_dma_chan *s3cchan,
440*4882a593Smuzhiyun 				       struct s3c24xx_txd *txd)
441*4882a593Smuzhiyun {
442*4882a593Smuzhiyun 	struct s3c24xx_dma_engine *s3cdma = s3cchan->host;
443*4882a593Smuzhiyun 	struct s3c24xx_dma_phy *phy = s3cchan->phy;
444*4882a593Smuzhiyun 	const struct s3c24xx_dma_platdata *pdata = s3cdma->pdata;
445*4882a593Smuzhiyun 	struct s3c24xx_sg *dsg = list_entry(txd->at, struct s3c24xx_sg, node);
446*4882a593Smuzhiyun 	u32 dcon = txd->dcon;
447*4882a593Smuzhiyun 	u32 val;
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun 	/* transfer-size and -count from len and width */
450*4882a593Smuzhiyun 	switch (txd->width) {
451*4882a593Smuzhiyun 	case 1:
452*4882a593Smuzhiyun 		dcon |= S3C24XX_DCON_DSZ_BYTE | dsg->len;
453*4882a593Smuzhiyun 		break;
454*4882a593Smuzhiyun 	case 2:
455*4882a593Smuzhiyun 		dcon |= S3C24XX_DCON_DSZ_HALFWORD | (dsg->len / 2);
456*4882a593Smuzhiyun 		break;
457*4882a593Smuzhiyun 	case 4:
458*4882a593Smuzhiyun 		dcon |= S3C24XX_DCON_DSZ_WORD | (dsg->len / 4);
459*4882a593Smuzhiyun 		break;
460*4882a593Smuzhiyun 	}
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun 	if (s3cchan->slave) {
463*4882a593Smuzhiyun 		struct s3c24xx_dma_channel *cdata =
464*4882a593Smuzhiyun 					&pdata->channels[s3cchan->id];
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 		if (s3cdma->sdata->has_reqsel) {
467*4882a593Smuzhiyun 			writel_relaxed((cdata->chansel << 1) |
468*4882a593Smuzhiyun 							S3C24XX_DMAREQSEL_HW,
469*4882a593Smuzhiyun 					phy->base + S3C24XX_DMAREQSEL);
470*4882a593Smuzhiyun 		} else {
471*4882a593Smuzhiyun 			int csel = cdata->chansel >> (phy->id *
472*4882a593Smuzhiyun 							S3C24XX_CHANSEL_WIDTH);
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun 			csel &= S3C24XX_CHANSEL_REQ_MASK;
475*4882a593Smuzhiyun 			dcon |= csel << S3C24XX_DCON_HWSRC_SHIFT;
476*4882a593Smuzhiyun 			dcon |= S3C24XX_DCON_HWTRIG;
477*4882a593Smuzhiyun 		}
478*4882a593Smuzhiyun 	} else {
479*4882a593Smuzhiyun 		if (s3cdma->sdata->has_reqsel)
480*4882a593Smuzhiyun 			writel_relaxed(0, phy->base + S3C24XX_DMAREQSEL);
481*4882a593Smuzhiyun 	}
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun 	writel_relaxed(dsg->src_addr, phy->base + S3C24XX_DISRC);
484*4882a593Smuzhiyun 	writel_relaxed(txd->disrcc, phy->base + S3C24XX_DISRCC);
485*4882a593Smuzhiyun 	writel_relaxed(dsg->dst_addr, phy->base + S3C24XX_DIDST);
486*4882a593Smuzhiyun 	writel_relaxed(txd->didstc, phy->base + S3C24XX_DIDSTC);
487*4882a593Smuzhiyun 	writel_relaxed(dcon, phy->base + S3C24XX_DCON);
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 	val = readl_relaxed(phy->base + S3C24XX_DMASKTRIG);
490*4882a593Smuzhiyun 	val &= ~S3C24XX_DMASKTRIG_STOP;
491*4882a593Smuzhiyun 	val |= S3C24XX_DMASKTRIG_ON;
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun 	/* trigger the dma operation for memcpy transfers */
494*4882a593Smuzhiyun 	if (!s3cchan->slave)
495*4882a593Smuzhiyun 		val |= S3C24XX_DMASKTRIG_SWTRIG;
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun 	writel(val, phy->base + S3C24XX_DMASKTRIG);
498*4882a593Smuzhiyun }
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun /*
501*4882a593Smuzhiyun  * Set the initial DMA register values and start first sg.
502*4882a593Smuzhiyun  */
s3c24xx_dma_start_next_txd(struct s3c24xx_dma_chan * s3cchan)503*4882a593Smuzhiyun static void s3c24xx_dma_start_next_txd(struct s3c24xx_dma_chan *s3cchan)
504*4882a593Smuzhiyun {
505*4882a593Smuzhiyun 	struct s3c24xx_dma_phy *phy = s3cchan->phy;
506*4882a593Smuzhiyun 	struct virt_dma_desc *vd = vchan_next_desc(&s3cchan->vc);
507*4882a593Smuzhiyun 	struct s3c24xx_txd *txd = to_s3c24xx_txd(&vd->tx);
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 	list_del(&txd->vd.node);
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun 	s3cchan->at = txd;
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 	/* Wait for channel inactive */
514*4882a593Smuzhiyun 	while (s3c24xx_dma_phy_busy(phy))
515*4882a593Smuzhiyun 		cpu_relax();
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun 	/* point to the first element of the sg list */
518*4882a593Smuzhiyun 	txd->at = txd->dsg_list.next;
519*4882a593Smuzhiyun 	s3c24xx_dma_start_next_sg(s3cchan, txd);
520*4882a593Smuzhiyun }
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun /*
523*4882a593Smuzhiyun  * Try to allocate a physical channel.  When successful, assign it to
524*4882a593Smuzhiyun  * this virtual channel, and initiate the next descriptor.  The
525*4882a593Smuzhiyun  * virtual channel lock must be held at this point.
526*4882a593Smuzhiyun  */
s3c24xx_dma_phy_alloc_and_start(struct s3c24xx_dma_chan * s3cchan)527*4882a593Smuzhiyun static void s3c24xx_dma_phy_alloc_and_start(struct s3c24xx_dma_chan *s3cchan)
528*4882a593Smuzhiyun {
529*4882a593Smuzhiyun 	struct s3c24xx_dma_engine *s3cdma = s3cchan->host;
530*4882a593Smuzhiyun 	struct s3c24xx_dma_phy *phy;
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun 	phy = s3c24xx_dma_get_phy(s3cchan);
533*4882a593Smuzhiyun 	if (!phy) {
534*4882a593Smuzhiyun 		dev_dbg(&s3cdma->pdev->dev, "no physical channel available for xfer on %s\n",
535*4882a593Smuzhiyun 			s3cchan->name);
536*4882a593Smuzhiyun 		s3cchan->state = S3C24XX_DMA_CHAN_WAITING;
537*4882a593Smuzhiyun 		return;
538*4882a593Smuzhiyun 	}
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun 	dev_dbg(&s3cdma->pdev->dev, "allocated physical channel %d for xfer on %s\n",
541*4882a593Smuzhiyun 		phy->id, s3cchan->name);
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 	s3cchan->phy = phy;
544*4882a593Smuzhiyun 	s3cchan->state = S3C24XX_DMA_CHAN_RUNNING;
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun 	s3c24xx_dma_start_next_txd(s3cchan);
547*4882a593Smuzhiyun }
548*4882a593Smuzhiyun 
s3c24xx_dma_phy_reassign_start(struct s3c24xx_dma_phy * phy,struct s3c24xx_dma_chan * s3cchan)549*4882a593Smuzhiyun static void s3c24xx_dma_phy_reassign_start(struct s3c24xx_dma_phy *phy,
550*4882a593Smuzhiyun 	struct s3c24xx_dma_chan *s3cchan)
551*4882a593Smuzhiyun {
552*4882a593Smuzhiyun 	struct s3c24xx_dma_engine *s3cdma = s3cchan->host;
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun 	dev_dbg(&s3cdma->pdev->dev, "reassigned physical channel %d for xfer on %s\n",
555*4882a593Smuzhiyun 		phy->id, s3cchan->name);
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 	/*
558*4882a593Smuzhiyun 	 * We do this without taking the lock; we're really only concerned
559*4882a593Smuzhiyun 	 * about whether this pointer is NULL or not, and we're guaranteed
560*4882a593Smuzhiyun 	 * that this will only be called when it _already_ is non-NULL.
561*4882a593Smuzhiyun 	 */
562*4882a593Smuzhiyun 	phy->serving = s3cchan;
563*4882a593Smuzhiyun 	s3cchan->phy = phy;
564*4882a593Smuzhiyun 	s3cchan->state = S3C24XX_DMA_CHAN_RUNNING;
565*4882a593Smuzhiyun 	s3c24xx_dma_start_next_txd(s3cchan);
566*4882a593Smuzhiyun }
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun /*
569*4882a593Smuzhiyun  * Free a physical DMA channel, potentially reallocating it to another
570*4882a593Smuzhiyun  * virtual channel if we have any pending.
571*4882a593Smuzhiyun  */
s3c24xx_dma_phy_free(struct s3c24xx_dma_chan * s3cchan)572*4882a593Smuzhiyun static void s3c24xx_dma_phy_free(struct s3c24xx_dma_chan *s3cchan)
573*4882a593Smuzhiyun {
574*4882a593Smuzhiyun 	struct s3c24xx_dma_engine *s3cdma = s3cchan->host;
575*4882a593Smuzhiyun 	struct s3c24xx_dma_chan *p, *next;
576*4882a593Smuzhiyun 
577*4882a593Smuzhiyun retry:
578*4882a593Smuzhiyun 	next = NULL;
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun 	/* Find a waiting virtual channel for the next transfer. */
581*4882a593Smuzhiyun 	list_for_each_entry(p, &s3cdma->memcpy.channels, vc.chan.device_node)
582*4882a593Smuzhiyun 		if (p->state == S3C24XX_DMA_CHAN_WAITING) {
583*4882a593Smuzhiyun 			next = p;
584*4882a593Smuzhiyun 			break;
585*4882a593Smuzhiyun 		}
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun 	if (!next) {
588*4882a593Smuzhiyun 		list_for_each_entry(p, &s3cdma->slave.channels,
589*4882a593Smuzhiyun 				    vc.chan.device_node)
590*4882a593Smuzhiyun 			if (p->state == S3C24XX_DMA_CHAN_WAITING &&
591*4882a593Smuzhiyun 				      s3c24xx_dma_phy_valid(p, s3cchan->phy)) {
592*4882a593Smuzhiyun 				next = p;
593*4882a593Smuzhiyun 				break;
594*4882a593Smuzhiyun 			}
595*4882a593Smuzhiyun 	}
596*4882a593Smuzhiyun 
597*4882a593Smuzhiyun 	/* Ensure that the physical channel is stopped */
598*4882a593Smuzhiyun 	s3c24xx_dma_terminate_phy(s3cchan->phy);
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun 	if (next) {
601*4882a593Smuzhiyun 		bool success;
602*4882a593Smuzhiyun 
603*4882a593Smuzhiyun 		/*
604*4882a593Smuzhiyun 		 * Eww.  We know this isn't going to deadlock
605*4882a593Smuzhiyun 		 * but lockdep probably doesn't.
606*4882a593Smuzhiyun 		 */
607*4882a593Smuzhiyun 		spin_lock(&next->vc.lock);
608*4882a593Smuzhiyun 		/* Re-check the state now that we have the lock */
609*4882a593Smuzhiyun 		success = next->state == S3C24XX_DMA_CHAN_WAITING;
610*4882a593Smuzhiyun 		if (success)
611*4882a593Smuzhiyun 			s3c24xx_dma_phy_reassign_start(s3cchan->phy, next);
612*4882a593Smuzhiyun 		spin_unlock(&next->vc.lock);
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun 		/* If the state changed, try to find another channel */
615*4882a593Smuzhiyun 		if (!success)
616*4882a593Smuzhiyun 			goto retry;
617*4882a593Smuzhiyun 	} else {
618*4882a593Smuzhiyun 		/* No more jobs, so free up the physical channel */
619*4882a593Smuzhiyun 		s3c24xx_dma_put_phy(s3cchan->phy);
620*4882a593Smuzhiyun 	}
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun 	s3cchan->phy = NULL;
623*4882a593Smuzhiyun 	s3cchan->state = S3C24XX_DMA_CHAN_IDLE;
624*4882a593Smuzhiyun }
625*4882a593Smuzhiyun 
s3c24xx_dma_desc_free(struct virt_dma_desc * vd)626*4882a593Smuzhiyun static void s3c24xx_dma_desc_free(struct virt_dma_desc *vd)
627*4882a593Smuzhiyun {
628*4882a593Smuzhiyun 	struct s3c24xx_txd *txd = to_s3c24xx_txd(&vd->tx);
629*4882a593Smuzhiyun 	struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(vd->tx.chan);
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun 	if (!s3cchan->slave)
632*4882a593Smuzhiyun 		dma_descriptor_unmap(&vd->tx);
633*4882a593Smuzhiyun 
634*4882a593Smuzhiyun 	s3c24xx_dma_free_txd(txd);
635*4882a593Smuzhiyun }
636*4882a593Smuzhiyun 
s3c24xx_dma_irq(int irq,void * data)637*4882a593Smuzhiyun static irqreturn_t s3c24xx_dma_irq(int irq, void *data)
638*4882a593Smuzhiyun {
639*4882a593Smuzhiyun 	struct s3c24xx_dma_phy *phy = data;
640*4882a593Smuzhiyun 	struct s3c24xx_dma_chan *s3cchan = phy->serving;
641*4882a593Smuzhiyun 	struct s3c24xx_txd *txd;
642*4882a593Smuzhiyun 
643*4882a593Smuzhiyun 	dev_dbg(&phy->host->pdev->dev, "interrupt on channel %d\n", phy->id);
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun 	/*
646*4882a593Smuzhiyun 	 * Interrupts happen to notify the completion of a transfer and the
647*4882a593Smuzhiyun 	 * channel should have moved into its stop state already on its own.
648*4882a593Smuzhiyun 	 * Therefore interrupts on channels not bound to a virtual channel
649*4882a593Smuzhiyun 	 * should never happen. Nevertheless send a terminate command to the
650*4882a593Smuzhiyun 	 * channel if the unlikely case happens.
651*4882a593Smuzhiyun 	 */
652*4882a593Smuzhiyun 	if (unlikely(!s3cchan)) {
653*4882a593Smuzhiyun 		dev_err(&phy->host->pdev->dev, "interrupt on unused channel %d\n",
654*4882a593Smuzhiyun 			phy->id);
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun 		s3c24xx_dma_terminate_phy(phy);
657*4882a593Smuzhiyun 
658*4882a593Smuzhiyun 		return IRQ_HANDLED;
659*4882a593Smuzhiyun 	}
660*4882a593Smuzhiyun 
661*4882a593Smuzhiyun 	spin_lock(&s3cchan->vc.lock);
662*4882a593Smuzhiyun 	txd = s3cchan->at;
663*4882a593Smuzhiyun 	if (txd) {
664*4882a593Smuzhiyun 		/* when more sg's are in this txd, start the next one */
665*4882a593Smuzhiyun 		if (!list_is_last(txd->at, &txd->dsg_list)) {
666*4882a593Smuzhiyun 			txd->at = txd->at->next;
667*4882a593Smuzhiyun 			if (txd->cyclic)
668*4882a593Smuzhiyun 				vchan_cyclic_callback(&txd->vd);
669*4882a593Smuzhiyun 			s3c24xx_dma_start_next_sg(s3cchan, txd);
670*4882a593Smuzhiyun 		} else if (!txd->cyclic) {
671*4882a593Smuzhiyun 			s3cchan->at = NULL;
672*4882a593Smuzhiyun 			vchan_cookie_complete(&txd->vd);
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun 			/*
675*4882a593Smuzhiyun 			 * And start the next descriptor (if any),
676*4882a593Smuzhiyun 			 * otherwise free this channel.
677*4882a593Smuzhiyun 			 */
678*4882a593Smuzhiyun 			if (vchan_next_desc(&s3cchan->vc))
679*4882a593Smuzhiyun 				s3c24xx_dma_start_next_txd(s3cchan);
680*4882a593Smuzhiyun 			else
681*4882a593Smuzhiyun 				s3c24xx_dma_phy_free(s3cchan);
682*4882a593Smuzhiyun 		} else {
683*4882a593Smuzhiyun 			vchan_cyclic_callback(&txd->vd);
684*4882a593Smuzhiyun 
685*4882a593Smuzhiyun 			/* Cyclic: reset at beginning */
686*4882a593Smuzhiyun 			txd->at = txd->dsg_list.next;
687*4882a593Smuzhiyun 			s3c24xx_dma_start_next_sg(s3cchan, txd);
688*4882a593Smuzhiyun 		}
689*4882a593Smuzhiyun 	}
690*4882a593Smuzhiyun 	spin_unlock(&s3cchan->vc.lock);
691*4882a593Smuzhiyun 
692*4882a593Smuzhiyun 	return IRQ_HANDLED;
693*4882a593Smuzhiyun }
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun /*
696*4882a593Smuzhiyun  * The DMA ENGINE API
697*4882a593Smuzhiyun  */
698*4882a593Smuzhiyun 
s3c24xx_dma_terminate_all(struct dma_chan * chan)699*4882a593Smuzhiyun static int s3c24xx_dma_terminate_all(struct dma_chan *chan)
700*4882a593Smuzhiyun {
701*4882a593Smuzhiyun 	struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(chan);
702*4882a593Smuzhiyun 	struct s3c24xx_dma_engine *s3cdma = s3cchan->host;
703*4882a593Smuzhiyun 	LIST_HEAD(head);
704*4882a593Smuzhiyun 	unsigned long flags;
705*4882a593Smuzhiyun 	int ret;
706*4882a593Smuzhiyun 
707*4882a593Smuzhiyun 	spin_lock_irqsave(&s3cchan->vc.lock, flags);
708*4882a593Smuzhiyun 
709*4882a593Smuzhiyun 	if (!s3cchan->phy && !s3cchan->at) {
710*4882a593Smuzhiyun 		dev_err(&s3cdma->pdev->dev, "trying to terminate already stopped channel %d\n",
711*4882a593Smuzhiyun 			s3cchan->id);
712*4882a593Smuzhiyun 		ret = -EINVAL;
713*4882a593Smuzhiyun 		goto unlock;
714*4882a593Smuzhiyun 	}
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun 	s3cchan->state = S3C24XX_DMA_CHAN_IDLE;
717*4882a593Smuzhiyun 
718*4882a593Smuzhiyun 	/* Mark physical channel as free */
719*4882a593Smuzhiyun 	if (s3cchan->phy)
720*4882a593Smuzhiyun 		s3c24xx_dma_phy_free(s3cchan);
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun 	/* Dequeue current job */
723*4882a593Smuzhiyun 	if (s3cchan->at) {
724*4882a593Smuzhiyun 		vchan_terminate_vdesc(&s3cchan->at->vd);
725*4882a593Smuzhiyun 		s3cchan->at = NULL;
726*4882a593Smuzhiyun 	}
727*4882a593Smuzhiyun 
728*4882a593Smuzhiyun 	/* Dequeue jobs not yet fired as well */
729*4882a593Smuzhiyun 
730*4882a593Smuzhiyun 	vchan_get_all_descriptors(&s3cchan->vc, &head);
731*4882a593Smuzhiyun 
732*4882a593Smuzhiyun 	spin_unlock_irqrestore(&s3cchan->vc.lock, flags);
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun 	vchan_dma_desc_free_list(&s3cchan->vc, &head);
735*4882a593Smuzhiyun 
736*4882a593Smuzhiyun 	return 0;
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun unlock:
739*4882a593Smuzhiyun 	spin_unlock_irqrestore(&s3cchan->vc.lock, flags);
740*4882a593Smuzhiyun 
741*4882a593Smuzhiyun 	return ret;
742*4882a593Smuzhiyun }
743*4882a593Smuzhiyun 
s3c24xx_dma_synchronize(struct dma_chan * chan)744*4882a593Smuzhiyun static void s3c24xx_dma_synchronize(struct dma_chan *chan)
745*4882a593Smuzhiyun {
746*4882a593Smuzhiyun 	struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(chan);
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun 	vchan_synchronize(&s3cchan->vc);
749*4882a593Smuzhiyun }
750*4882a593Smuzhiyun 
s3c24xx_dma_free_chan_resources(struct dma_chan * chan)751*4882a593Smuzhiyun static void s3c24xx_dma_free_chan_resources(struct dma_chan *chan)
752*4882a593Smuzhiyun {
753*4882a593Smuzhiyun 	/* Ensure all queued descriptors are freed */
754*4882a593Smuzhiyun 	vchan_free_chan_resources(to_virt_chan(chan));
755*4882a593Smuzhiyun }
756*4882a593Smuzhiyun 
s3c24xx_dma_tx_status(struct dma_chan * chan,dma_cookie_t cookie,struct dma_tx_state * txstate)757*4882a593Smuzhiyun static enum dma_status s3c24xx_dma_tx_status(struct dma_chan *chan,
758*4882a593Smuzhiyun 		dma_cookie_t cookie, struct dma_tx_state *txstate)
759*4882a593Smuzhiyun {
760*4882a593Smuzhiyun 	struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(chan);
761*4882a593Smuzhiyun 	struct s3c24xx_txd *txd;
762*4882a593Smuzhiyun 	struct s3c24xx_sg *dsg;
763*4882a593Smuzhiyun 	struct virt_dma_desc *vd;
764*4882a593Smuzhiyun 	unsigned long flags;
765*4882a593Smuzhiyun 	enum dma_status ret;
766*4882a593Smuzhiyun 	size_t bytes = 0;
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun 	spin_lock_irqsave(&s3cchan->vc.lock, flags);
769*4882a593Smuzhiyun 	ret = dma_cookie_status(chan, cookie, txstate);
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun 	/*
772*4882a593Smuzhiyun 	 * There's no point calculating the residue if there's
773*4882a593Smuzhiyun 	 * no txstate to store the value.
774*4882a593Smuzhiyun 	 */
775*4882a593Smuzhiyun 	if (ret == DMA_COMPLETE || !txstate) {
776*4882a593Smuzhiyun 		spin_unlock_irqrestore(&s3cchan->vc.lock, flags);
777*4882a593Smuzhiyun 		return ret;
778*4882a593Smuzhiyun 	}
779*4882a593Smuzhiyun 
780*4882a593Smuzhiyun 	vd = vchan_find_desc(&s3cchan->vc, cookie);
781*4882a593Smuzhiyun 	if (vd) {
782*4882a593Smuzhiyun 		/* On the issued list, so hasn't been processed yet */
783*4882a593Smuzhiyun 		txd = to_s3c24xx_txd(&vd->tx);
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun 		list_for_each_entry(dsg, &txd->dsg_list, node)
786*4882a593Smuzhiyun 			bytes += dsg->len;
787*4882a593Smuzhiyun 	} else {
788*4882a593Smuzhiyun 		/*
789*4882a593Smuzhiyun 		 * Currently running, so sum over the pending sg's and
790*4882a593Smuzhiyun 		 * the currently active one.
791*4882a593Smuzhiyun 		 */
792*4882a593Smuzhiyun 		txd = s3cchan->at;
793*4882a593Smuzhiyun 
794*4882a593Smuzhiyun 		dsg = list_entry(txd->at, struct s3c24xx_sg, node);
795*4882a593Smuzhiyun 		list_for_each_entry_from(dsg, &txd->dsg_list, node)
796*4882a593Smuzhiyun 			bytes += dsg->len;
797*4882a593Smuzhiyun 
798*4882a593Smuzhiyun 		bytes += s3c24xx_dma_getbytes_chan(s3cchan);
799*4882a593Smuzhiyun 	}
800*4882a593Smuzhiyun 	spin_unlock_irqrestore(&s3cchan->vc.lock, flags);
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 	/*
803*4882a593Smuzhiyun 	 * This cookie not complete yet
804*4882a593Smuzhiyun 	 * Get number of bytes left in the active transactions and queue
805*4882a593Smuzhiyun 	 */
806*4882a593Smuzhiyun 	dma_set_residue(txstate, bytes);
807*4882a593Smuzhiyun 
808*4882a593Smuzhiyun 	/* Whether waiting or running, we're in progress */
809*4882a593Smuzhiyun 	return ret;
810*4882a593Smuzhiyun }
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun /*
813*4882a593Smuzhiyun  * Initialize a descriptor to be used by memcpy submit
814*4882a593Smuzhiyun  */
s3c24xx_dma_prep_memcpy(struct dma_chan * chan,dma_addr_t dest,dma_addr_t src,size_t len,unsigned long flags)815*4882a593Smuzhiyun static struct dma_async_tx_descriptor *s3c24xx_dma_prep_memcpy(
816*4882a593Smuzhiyun 		struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
817*4882a593Smuzhiyun 		size_t len, unsigned long flags)
818*4882a593Smuzhiyun {
819*4882a593Smuzhiyun 	struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(chan);
820*4882a593Smuzhiyun 	struct s3c24xx_dma_engine *s3cdma = s3cchan->host;
821*4882a593Smuzhiyun 	struct s3c24xx_txd *txd;
822*4882a593Smuzhiyun 	struct s3c24xx_sg *dsg;
823*4882a593Smuzhiyun 	int src_mod, dest_mod;
824*4882a593Smuzhiyun 
825*4882a593Smuzhiyun 	dev_dbg(&s3cdma->pdev->dev, "prepare memcpy of %zu bytes from %s\n",
826*4882a593Smuzhiyun 			len, s3cchan->name);
827*4882a593Smuzhiyun 
828*4882a593Smuzhiyun 	if ((len & S3C24XX_DCON_TC_MASK) != len) {
829*4882a593Smuzhiyun 		dev_err(&s3cdma->pdev->dev, "memcpy size %zu to large\n", len);
830*4882a593Smuzhiyun 		return NULL;
831*4882a593Smuzhiyun 	}
832*4882a593Smuzhiyun 
833*4882a593Smuzhiyun 	txd = s3c24xx_dma_get_txd();
834*4882a593Smuzhiyun 	if (!txd)
835*4882a593Smuzhiyun 		return NULL;
836*4882a593Smuzhiyun 
837*4882a593Smuzhiyun 	dsg = kzalloc(sizeof(*dsg), GFP_NOWAIT);
838*4882a593Smuzhiyun 	if (!dsg) {
839*4882a593Smuzhiyun 		s3c24xx_dma_free_txd(txd);
840*4882a593Smuzhiyun 		return NULL;
841*4882a593Smuzhiyun 	}
842*4882a593Smuzhiyun 	list_add_tail(&dsg->node, &txd->dsg_list);
843*4882a593Smuzhiyun 
844*4882a593Smuzhiyun 	dsg->src_addr = src;
845*4882a593Smuzhiyun 	dsg->dst_addr = dest;
846*4882a593Smuzhiyun 	dsg->len = len;
847*4882a593Smuzhiyun 
848*4882a593Smuzhiyun 	/*
849*4882a593Smuzhiyun 	 * Determine a suitable transfer width.
850*4882a593Smuzhiyun 	 * The DMA controller cannot fetch/store information which is not
851*4882a593Smuzhiyun 	 * naturally aligned on the bus, i.e., a 4 byte fetch must start at
852*4882a593Smuzhiyun 	 * an address divisible by 4 - more generally addr % width must be 0.
853*4882a593Smuzhiyun 	 */
854*4882a593Smuzhiyun 	src_mod = src % 4;
855*4882a593Smuzhiyun 	dest_mod = dest % 4;
856*4882a593Smuzhiyun 	switch (len % 4) {
857*4882a593Smuzhiyun 	case 0:
858*4882a593Smuzhiyun 		txd->width = (src_mod == 0 && dest_mod == 0) ? 4 : 1;
859*4882a593Smuzhiyun 		break;
860*4882a593Smuzhiyun 	case 2:
861*4882a593Smuzhiyun 		txd->width = ((src_mod == 2 || src_mod == 0) &&
862*4882a593Smuzhiyun 			      (dest_mod == 2 || dest_mod == 0)) ? 2 : 1;
863*4882a593Smuzhiyun 		break;
864*4882a593Smuzhiyun 	default:
865*4882a593Smuzhiyun 		txd->width = 1;
866*4882a593Smuzhiyun 		break;
867*4882a593Smuzhiyun 	}
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun 	txd->disrcc = S3C24XX_DISRCC_LOC_AHB | S3C24XX_DISRCC_INC_INCREMENT;
870*4882a593Smuzhiyun 	txd->didstc = S3C24XX_DIDSTC_LOC_AHB | S3C24XX_DIDSTC_INC_INCREMENT;
871*4882a593Smuzhiyun 	txd->dcon |= S3C24XX_DCON_DEMAND | S3C24XX_DCON_SYNC_HCLK |
872*4882a593Smuzhiyun 		     S3C24XX_DCON_SERV_WHOLE;
873*4882a593Smuzhiyun 
874*4882a593Smuzhiyun 	return vchan_tx_prep(&s3cchan->vc, &txd->vd, flags);
875*4882a593Smuzhiyun }
876*4882a593Smuzhiyun 
s3c24xx_dma_prep_dma_cyclic(struct dma_chan * chan,dma_addr_t addr,size_t size,size_t period,enum dma_transfer_direction direction,unsigned long flags)877*4882a593Smuzhiyun static struct dma_async_tx_descriptor *s3c24xx_dma_prep_dma_cyclic(
878*4882a593Smuzhiyun 	struct dma_chan *chan, dma_addr_t addr, size_t size, size_t period,
879*4882a593Smuzhiyun 	enum dma_transfer_direction direction, unsigned long flags)
880*4882a593Smuzhiyun {
881*4882a593Smuzhiyun 	struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(chan);
882*4882a593Smuzhiyun 	struct s3c24xx_dma_engine *s3cdma = s3cchan->host;
883*4882a593Smuzhiyun 	const struct s3c24xx_dma_platdata *pdata = s3cdma->pdata;
884*4882a593Smuzhiyun 	struct s3c24xx_dma_channel *cdata = &pdata->channels[s3cchan->id];
885*4882a593Smuzhiyun 	struct s3c24xx_txd *txd;
886*4882a593Smuzhiyun 	struct s3c24xx_sg *dsg;
887*4882a593Smuzhiyun 	unsigned sg_len;
888*4882a593Smuzhiyun 	dma_addr_t slave_addr;
889*4882a593Smuzhiyun 	u32 hwcfg = 0;
890*4882a593Smuzhiyun 	int i;
891*4882a593Smuzhiyun 
892*4882a593Smuzhiyun 	dev_dbg(&s3cdma->pdev->dev,
893*4882a593Smuzhiyun 		"prepare cyclic transaction of %zu bytes with period %zu from %s\n",
894*4882a593Smuzhiyun 		size, period, s3cchan->name);
895*4882a593Smuzhiyun 
896*4882a593Smuzhiyun 	if (!is_slave_direction(direction)) {
897*4882a593Smuzhiyun 		dev_err(&s3cdma->pdev->dev,
898*4882a593Smuzhiyun 			"direction %d unsupported\n", direction);
899*4882a593Smuzhiyun 		return NULL;
900*4882a593Smuzhiyun 	}
901*4882a593Smuzhiyun 
902*4882a593Smuzhiyun 	txd = s3c24xx_dma_get_txd();
903*4882a593Smuzhiyun 	if (!txd)
904*4882a593Smuzhiyun 		return NULL;
905*4882a593Smuzhiyun 
906*4882a593Smuzhiyun 	txd->cyclic = 1;
907*4882a593Smuzhiyun 
908*4882a593Smuzhiyun 	if (cdata->handshake)
909*4882a593Smuzhiyun 		txd->dcon |= S3C24XX_DCON_HANDSHAKE;
910*4882a593Smuzhiyun 
911*4882a593Smuzhiyun 	switch (cdata->bus) {
912*4882a593Smuzhiyun 	case S3C24XX_DMA_APB:
913*4882a593Smuzhiyun 		txd->dcon |= S3C24XX_DCON_SYNC_PCLK;
914*4882a593Smuzhiyun 		hwcfg |= S3C24XX_DISRCC_LOC_APB;
915*4882a593Smuzhiyun 		break;
916*4882a593Smuzhiyun 	case S3C24XX_DMA_AHB:
917*4882a593Smuzhiyun 		txd->dcon |= S3C24XX_DCON_SYNC_HCLK;
918*4882a593Smuzhiyun 		hwcfg |= S3C24XX_DISRCC_LOC_AHB;
919*4882a593Smuzhiyun 		break;
920*4882a593Smuzhiyun 	}
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun 	/*
923*4882a593Smuzhiyun 	 * Always assume our peripheral desintation is a fixed
924*4882a593Smuzhiyun 	 * address in memory.
925*4882a593Smuzhiyun 	 */
926*4882a593Smuzhiyun 	hwcfg |= S3C24XX_DISRCC_INC_FIXED;
927*4882a593Smuzhiyun 
928*4882a593Smuzhiyun 	/*
929*4882a593Smuzhiyun 	 * Individual dma operations are requested by the slave,
930*4882a593Smuzhiyun 	 * so serve only single atomic operations (S3C24XX_DCON_SERV_SINGLE).
931*4882a593Smuzhiyun 	 */
932*4882a593Smuzhiyun 	txd->dcon |= S3C24XX_DCON_SERV_SINGLE;
933*4882a593Smuzhiyun 
934*4882a593Smuzhiyun 	if (direction == DMA_MEM_TO_DEV) {
935*4882a593Smuzhiyun 		txd->disrcc = S3C24XX_DISRCC_LOC_AHB |
936*4882a593Smuzhiyun 			      S3C24XX_DISRCC_INC_INCREMENT;
937*4882a593Smuzhiyun 		txd->didstc = hwcfg;
938*4882a593Smuzhiyun 		slave_addr = s3cchan->cfg.dst_addr;
939*4882a593Smuzhiyun 		txd->width = s3cchan->cfg.dst_addr_width;
940*4882a593Smuzhiyun 	} else {
941*4882a593Smuzhiyun 		txd->disrcc = hwcfg;
942*4882a593Smuzhiyun 		txd->didstc = S3C24XX_DIDSTC_LOC_AHB |
943*4882a593Smuzhiyun 			      S3C24XX_DIDSTC_INC_INCREMENT;
944*4882a593Smuzhiyun 		slave_addr = s3cchan->cfg.src_addr;
945*4882a593Smuzhiyun 		txd->width = s3cchan->cfg.src_addr_width;
946*4882a593Smuzhiyun 	}
947*4882a593Smuzhiyun 
948*4882a593Smuzhiyun 	sg_len = size / period;
949*4882a593Smuzhiyun 
950*4882a593Smuzhiyun 	for (i = 0; i < sg_len; i++) {
951*4882a593Smuzhiyun 		dsg = kzalloc(sizeof(*dsg), GFP_NOWAIT);
952*4882a593Smuzhiyun 		if (!dsg) {
953*4882a593Smuzhiyun 			s3c24xx_dma_free_txd(txd);
954*4882a593Smuzhiyun 			return NULL;
955*4882a593Smuzhiyun 		}
956*4882a593Smuzhiyun 		list_add_tail(&dsg->node, &txd->dsg_list);
957*4882a593Smuzhiyun 
958*4882a593Smuzhiyun 		dsg->len = period;
959*4882a593Smuzhiyun 		/* Check last period length */
960*4882a593Smuzhiyun 		if (i == sg_len - 1)
961*4882a593Smuzhiyun 			dsg->len = size - period * i;
962*4882a593Smuzhiyun 		if (direction == DMA_MEM_TO_DEV) {
963*4882a593Smuzhiyun 			dsg->src_addr = addr + period * i;
964*4882a593Smuzhiyun 			dsg->dst_addr = slave_addr;
965*4882a593Smuzhiyun 		} else { /* DMA_DEV_TO_MEM */
966*4882a593Smuzhiyun 			dsg->src_addr = slave_addr;
967*4882a593Smuzhiyun 			dsg->dst_addr = addr + period * i;
968*4882a593Smuzhiyun 		}
969*4882a593Smuzhiyun 	}
970*4882a593Smuzhiyun 
971*4882a593Smuzhiyun 	return vchan_tx_prep(&s3cchan->vc, &txd->vd, flags);
972*4882a593Smuzhiyun }
973*4882a593Smuzhiyun 
s3c24xx_dma_prep_slave_sg(struct dma_chan * chan,struct scatterlist * sgl,unsigned int sg_len,enum dma_transfer_direction direction,unsigned long flags,void * context)974*4882a593Smuzhiyun static struct dma_async_tx_descriptor *s3c24xx_dma_prep_slave_sg(
975*4882a593Smuzhiyun 		struct dma_chan *chan, struct scatterlist *sgl,
976*4882a593Smuzhiyun 		unsigned int sg_len, enum dma_transfer_direction direction,
977*4882a593Smuzhiyun 		unsigned long flags, void *context)
978*4882a593Smuzhiyun {
979*4882a593Smuzhiyun 	struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(chan);
980*4882a593Smuzhiyun 	struct s3c24xx_dma_engine *s3cdma = s3cchan->host;
981*4882a593Smuzhiyun 	const struct s3c24xx_dma_platdata *pdata = s3cdma->pdata;
982*4882a593Smuzhiyun 	struct s3c24xx_dma_channel *cdata = &pdata->channels[s3cchan->id];
983*4882a593Smuzhiyun 	struct s3c24xx_txd *txd;
984*4882a593Smuzhiyun 	struct s3c24xx_sg *dsg;
985*4882a593Smuzhiyun 	struct scatterlist *sg;
986*4882a593Smuzhiyun 	dma_addr_t slave_addr;
987*4882a593Smuzhiyun 	u32 hwcfg = 0;
988*4882a593Smuzhiyun 	int tmp;
989*4882a593Smuzhiyun 
990*4882a593Smuzhiyun 	dev_dbg(&s3cdma->pdev->dev, "prepare transaction of %d bytes from %s\n",
991*4882a593Smuzhiyun 			sg_dma_len(sgl), s3cchan->name);
992*4882a593Smuzhiyun 
993*4882a593Smuzhiyun 	txd = s3c24xx_dma_get_txd();
994*4882a593Smuzhiyun 	if (!txd)
995*4882a593Smuzhiyun 		return NULL;
996*4882a593Smuzhiyun 
997*4882a593Smuzhiyun 	if (cdata->handshake)
998*4882a593Smuzhiyun 		txd->dcon |= S3C24XX_DCON_HANDSHAKE;
999*4882a593Smuzhiyun 
1000*4882a593Smuzhiyun 	switch (cdata->bus) {
1001*4882a593Smuzhiyun 	case S3C24XX_DMA_APB:
1002*4882a593Smuzhiyun 		txd->dcon |= S3C24XX_DCON_SYNC_PCLK;
1003*4882a593Smuzhiyun 		hwcfg |= S3C24XX_DISRCC_LOC_APB;
1004*4882a593Smuzhiyun 		break;
1005*4882a593Smuzhiyun 	case S3C24XX_DMA_AHB:
1006*4882a593Smuzhiyun 		txd->dcon |= S3C24XX_DCON_SYNC_HCLK;
1007*4882a593Smuzhiyun 		hwcfg |= S3C24XX_DISRCC_LOC_AHB;
1008*4882a593Smuzhiyun 		break;
1009*4882a593Smuzhiyun 	}
1010*4882a593Smuzhiyun 
1011*4882a593Smuzhiyun 	/*
1012*4882a593Smuzhiyun 	 * Always assume our peripheral desintation is a fixed
1013*4882a593Smuzhiyun 	 * address in memory.
1014*4882a593Smuzhiyun 	 */
1015*4882a593Smuzhiyun 	hwcfg |= S3C24XX_DISRCC_INC_FIXED;
1016*4882a593Smuzhiyun 
1017*4882a593Smuzhiyun 	/*
1018*4882a593Smuzhiyun 	 * Individual dma operations are requested by the slave,
1019*4882a593Smuzhiyun 	 * so serve only single atomic operations (S3C24XX_DCON_SERV_SINGLE).
1020*4882a593Smuzhiyun 	 */
1021*4882a593Smuzhiyun 	txd->dcon |= S3C24XX_DCON_SERV_SINGLE;
1022*4882a593Smuzhiyun 
1023*4882a593Smuzhiyun 	if (direction == DMA_MEM_TO_DEV) {
1024*4882a593Smuzhiyun 		txd->disrcc = S3C24XX_DISRCC_LOC_AHB |
1025*4882a593Smuzhiyun 			      S3C24XX_DISRCC_INC_INCREMENT;
1026*4882a593Smuzhiyun 		txd->didstc = hwcfg;
1027*4882a593Smuzhiyun 		slave_addr = s3cchan->cfg.dst_addr;
1028*4882a593Smuzhiyun 		txd->width = s3cchan->cfg.dst_addr_width;
1029*4882a593Smuzhiyun 	} else if (direction == DMA_DEV_TO_MEM) {
1030*4882a593Smuzhiyun 		txd->disrcc = hwcfg;
1031*4882a593Smuzhiyun 		txd->didstc = S3C24XX_DIDSTC_LOC_AHB |
1032*4882a593Smuzhiyun 			      S3C24XX_DIDSTC_INC_INCREMENT;
1033*4882a593Smuzhiyun 		slave_addr = s3cchan->cfg.src_addr;
1034*4882a593Smuzhiyun 		txd->width = s3cchan->cfg.src_addr_width;
1035*4882a593Smuzhiyun 	} else {
1036*4882a593Smuzhiyun 		s3c24xx_dma_free_txd(txd);
1037*4882a593Smuzhiyun 		dev_err(&s3cdma->pdev->dev,
1038*4882a593Smuzhiyun 			"direction %d unsupported\n", direction);
1039*4882a593Smuzhiyun 		return NULL;
1040*4882a593Smuzhiyun 	}
1041*4882a593Smuzhiyun 
1042*4882a593Smuzhiyun 	for_each_sg(sgl, sg, sg_len, tmp) {
1043*4882a593Smuzhiyun 		dsg = kzalloc(sizeof(*dsg), GFP_NOWAIT);
1044*4882a593Smuzhiyun 		if (!dsg) {
1045*4882a593Smuzhiyun 			s3c24xx_dma_free_txd(txd);
1046*4882a593Smuzhiyun 			return NULL;
1047*4882a593Smuzhiyun 		}
1048*4882a593Smuzhiyun 		list_add_tail(&dsg->node, &txd->dsg_list);
1049*4882a593Smuzhiyun 
1050*4882a593Smuzhiyun 		dsg->len = sg_dma_len(sg);
1051*4882a593Smuzhiyun 		if (direction == DMA_MEM_TO_DEV) {
1052*4882a593Smuzhiyun 			dsg->src_addr = sg_dma_address(sg);
1053*4882a593Smuzhiyun 			dsg->dst_addr = slave_addr;
1054*4882a593Smuzhiyun 		} else { /* DMA_DEV_TO_MEM */
1055*4882a593Smuzhiyun 			dsg->src_addr = slave_addr;
1056*4882a593Smuzhiyun 			dsg->dst_addr = sg_dma_address(sg);
1057*4882a593Smuzhiyun 		}
1058*4882a593Smuzhiyun 	}
1059*4882a593Smuzhiyun 
1060*4882a593Smuzhiyun 	return vchan_tx_prep(&s3cchan->vc, &txd->vd, flags);
1061*4882a593Smuzhiyun }
1062*4882a593Smuzhiyun 
1063*4882a593Smuzhiyun /*
1064*4882a593Smuzhiyun  * Slave transactions callback to the slave device to allow
1065*4882a593Smuzhiyun  * synchronization of slave DMA signals with the DMAC enable
1066*4882a593Smuzhiyun  */
s3c24xx_dma_issue_pending(struct dma_chan * chan)1067*4882a593Smuzhiyun static void s3c24xx_dma_issue_pending(struct dma_chan *chan)
1068*4882a593Smuzhiyun {
1069*4882a593Smuzhiyun 	struct s3c24xx_dma_chan *s3cchan = to_s3c24xx_dma_chan(chan);
1070*4882a593Smuzhiyun 	unsigned long flags;
1071*4882a593Smuzhiyun 
1072*4882a593Smuzhiyun 	spin_lock_irqsave(&s3cchan->vc.lock, flags);
1073*4882a593Smuzhiyun 	if (vchan_issue_pending(&s3cchan->vc)) {
1074*4882a593Smuzhiyun 		if (!s3cchan->phy && s3cchan->state != S3C24XX_DMA_CHAN_WAITING)
1075*4882a593Smuzhiyun 			s3c24xx_dma_phy_alloc_and_start(s3cchan);
1076*4882a593Smuzhiyun 	}
1077*4882a593Smuzhiyun 	spin_unlock_irqrestore(&s3cchan->vc.lock, flags);
1078*4882a593Smuzhiyun }
1079*4882a593Smuzhiyun 
1080*4882a593Smuzhiyun /*
1081*4882a593Smuzhiyun  * Bringup and teardown
1082*4882a593Smuzhiyun  */
1083*4882a593Smuzhiyun 
1084*4882a593Smuzhiyun /*
1085*4882a593Smuzhiyun  * Initialise the DMAC memcpy/slave channels.
1086*4882a593Smuzhiyun  * Make a local wrapper to hold required data
1087*4882a593Smuzhiyun  */
s3c24xx_dma_init_virtual_channels(struct s3c24xx_dma_engine * s3cdma,struct dma_device * dmadev,unsigned int channels,bool slave)1088*4882a593Smuzhiyun static int s3c24xx_dma_init_virtual_channels(struct s3c24xx_dma_engine *s3cdma,
1089*4882a593Smuzhiyun 		struct dma_device *dmadev, unsigned int channels, bool slave)
1090*4882a593Smuzhiyun {
1091*4882a593Smuzhiyun 	struct s3c24xx_dma_chan *chan;
1092*4882a593Smuzhiyun 	int i;
1093*4882a593Smuzhiyun 
1094*4882a593Smuzhiyun 	INIT_LIST_HEAD(&dmadev->channels);
1095*4882a593Smuzhiyun 
1096*4882a593Smuzhiyun 	/*
1097*4882a593Smuzhiyun 	 * Register as many many memcpy as we have physical channels,
1098*4882a593Smuzhiyun 	 * we won't always be able to use all but the code will have
1099*4882a593Smuzhiyun 	 * to cope with that situation.
1100*4882a593Smuzhiyun 	 */
1101*4882a593Smuzhiyun 	for (i = 0; i < channels; i++) {
1102*4882a593Smuzhiyun 		chan = devm_kzalloc(dmadev->dev, sizeof(*chan), GFP_KERNEL);
1103*4882a593Smuzhiyun 		if (!chan)
1104*4882a593Smuzhiyun 			return -ENOMEM;
1105*4882a593Smuzhiyun 
1106*4882a593Smuzhiyun 		chan->id = i;
1107*4882a593Smuzhiyun 		chan->host = s3cdma;
1108*4882a593Smuzhiyun 		chan->state = S3C24XX_DMA_CHAN_IDLE;
1109*4882a593Smuzhiyun 
1110*4882a593Smuzhiyun 		if (slave) {
1111*4882a593Smuzhiyun 			chan->slave = true;
1112*4882a593Smuzhiyun 			chan->name = kasprintf(GFP_KERNEL, "slave%d", i);
1113*4882a593Smuzhiyun 			if (!chan->name)
1114*4882a593Smuzhiyun 				return -ENOMEM;
1115*4882a593Smuzhiyun 		} else {
1116*4882a593Smuzhiyun 			chan->name = kasprintf(GFP_KERNEL, "memcpy%d", i);
1117*4882a593Smuzhiyun 			if (!chan->name)
1118*4882a593Smuzhiyun 				return -ENOMEM;
1119*4882a593Smuzhiyun 		}
1120*4882a593Smuzhiyun 		dev_dbg(dmadev->dev,
1121*4882a593Smuzhiyun 			 "initialize virtual channel \"%s\"\n",
1122*4882a593Smuzhiyun 			 chan->name);
1123*4882a593Smuzhiyun 
1124*4882a593Smuzhiyun 		chan->vc.desc_free = s3c24xx_dma_desc_free;
1125*4882a593Smuzhiyun 		vchan_init(&chan->vc, dmadev);
1126*4882a593Smuzhiyun 	}
1127*4882a593Smuzhiyun 	dev_info(dmadev->dev, "initialized %d virtual %s channels\n",
1128*4882a593Smuzhiyun 		 i, slave ? "slave" : "memcpy");
1129*4882a593Smuzhiyun 	return i;
1130*4882a593Smuzhiyun }
1131*4882a593Smuzhiyun 
s3c24xx_dma_free_virtual_channels(struct dma_device * dmadev)1132*4882a593Smuzhiyun static void s3c24xx_dma_free_virtual_channels(struct dma_device *dmadev)
1133*4882a593Smuzhiyun {
1134*4882a593Smuzhiyun 	struct s3c24xx_dma_chan *chan = NULL;
1135*4882a593Smuzhiyun 	struct s3c24xx_dma_chan *next;
1136*4882a593Smuzhiyun 
1137*4882a593Smuzhiyun 	list_for_each_entry_safe(chan,
1138*4882a593Smuzhiyun 				 next, &dmadev->channels, vc.chan.device_node) {
1139*4882a593Smuzhiyun 		list_del(&chan->vc.chan.device_node);
1140*4882a593Smuzhiyun 		tasklet_kill(&chan->vc.task);
1141*4882a593Smuzhiyun 	}
1142*4882a593Smuzhiyun }
1143*4882a593Smuzhiyun 
1144*4882a593Smuzhiyun /* s3c2410, s3c2440 and s3c2442 have a 0x40 stride without separate clocks */
1145*4882a593Smuzhiyun static struct soc_data soc_s3c2410 = {
1146*4882a593Smuzhiyun 	.stride = 0x40,
1147*4882a593Smuzhiyun 	.has_reqsel = false,
1148*4882a593Smuzhiyun 	.has_clocks = false,
1149*4882a593Smuzhiyun };
1150*4882a593Smuzhiyun 
1151*4882a593Smuzhiyun /* s3c2412 and s3c2413 have a 0x40 stride and dmareqsel mechanism */
1152*4882a593Smuzhiyun static struct soc_data soc_s3c2412 = {
1153*4882a593Smuzhiyun 	.stride = 0x40,
1154*4882a593Smuzhiyun 	.has_reqsel = true,
1155*4882a593Smuzhiyun 	.has_clocks = true,
1156*4882a593Smuzhiyun };
1157*4882a593Smuzhiyun 
1158*4882a593Smuzhiyun /* s3c2443 and following have a 0x100 stride and dmareqsel mechanism */
1159*4882a593Smuzhiyun static struct soc_data soc_s3c2443 = {
1160*4882a593Smuzhiyun 	.stride = 0x100,
1161*4882a593Smuzhiyun 	.has_reqsel = true,
1162*4882a593Smuzhiyun 	.has_clocks = true,
1163*4882a593Smuzhiyun };
1164*4882a593Smuzhiyun 
1165*4882a593Smuzhiyun static const struct platform_device_id s3c24xx_dma_driver_ids[] = {
1166*4882a593Smuzhiyun 	{
1167*4882a593Smuzhiyun 		.name		= "s3c2410-dma",
1168*4882a593Smuzhiyun 		.driver_data	= (kernel_ulong_t)&soc_s3c2410,
1169*4882a593Smuzhiyun 	}, {
1170*4882a593Smuzhiyun 		.name		= "s3c2412-dma",
1171*4882a593Smuzhiyun 		.driver_data	= (kernel_ulong_t)&soc_s3c2412,
1172*4882a593Smuzhiyun 	}, {
1173*4882a593Smuzhiyun 		.name		= "s3c2443-dma",
1174*4882a593Smuzhiyun 		.driver_data	= (kernel_ulong_t)&soc_s3c2443,
1175*4882a593Smuzhiyun 	},
1176*4882a593Smuzhiyun 	{ },
1177*4882a593Smuzhiyun };
1178*4882a593Smuzhiyun 
s3c24xx_dma_get_soc_data(struct platform_device * pdev)1179*4882a593Smuzhiyun static struct soc_data *s3c24xx_dma_get_soc_data(struct platform_device *pdev)
1180*4882a593Smuzhiyun {
1181*4882a593Smuzhiyun 	return (struct soc_data *)
1182*4882a593Smuzhiyun 			 platform_get_device_id(pdev)->driver_data;
1183*4882a593Smuzhiyun }
1184*4882a593Smuzhiyun 
s3c24xx_dma_probe(struct platform_device * pdev)1185*4882a593Smuzhiyun static int s3c24xx_dma_probe(struct platform_device *pdev)
1186*4882a593Smuzhiyun {
1187*4882a593Smuzhiyun 	const struct s3c24xx_dma_platdata *pdata = dev_get_platdata(&pdev->dev);
1188*4882a593Smuzhiyun 	struct s3c24xx_dma_engine *s3cdma;
1189*4882a593Smuzhiyun 	struct soc_data *sdata;
1190*4882a593Smuzhiyun 	struct resource *res;
1191*4882a593Smuzhiyun 	int ret;
1192*4882a593Smuzhiyun 	int i;
1193*4882a593Smuzhiyun 
1194*4882a593Smuzhiyun 	if (!pdata) {
1195*4882a593Smuzhiyun 		dev_err(&pdev->dev, "platform data missing\n");
1196*4882a593Smuzhiyun 		return -ENODEV;
1197*4882a593Smuzhiyun 	}
1198*4882a593Smuzhiyun 
1199*4882a593Smuzhiyun 	/* Basic sanity check */
1200*4882a593Smuzhiyun 	if (pdata->num_phy_channels > MAX_DMA_CHANNELS) {
1201*4882a593Smuzhiyun 		dev_err(&pdev->dev, "too many dma channels %d, max %d\n",
1202*4882a593Smuzhiyun 			pdata->num_phy_channels, MAX_DMA_CHANNELS);
1203*4882a593Smuzhiyun 		return -EINVAL;
1204*4882a593Smuzhiyun 	}
1205*4882a593Smuzhiyun 
1206*4882a593Smuzhiyun 	sdata = s3c24xx_dma_get_soc_data(pdev);
1207*4882a593Smuzhiyun 	if (!sdata)
1208*4882a593Smuzhiyun 		return -EINVAL;
1209*4882a593Smuzhiyun 
1210*4882a593Smuzhiyun 	s3cdma = devm_kzalloc(&pdev->dev, sizeof(*s3cdma), GFP_KERNEL);
1211*4882a593Smuzhiyun 	if (!s3cdma)
1212*4882a593Smuzhiyun 		return -ENOMEM;
1213*4882a593Smuzhiyun 
1214*4882a593Smuzhiyun 	s3cdma->pdev = pdev;
1215*4882a593Smuzhiyun 	s3cdma->pdata = pdata;
1216*4882a593Smuzhiyun 	s3cdma->sdata = sdata;
1217*4882a593Smuzhiyun 
1218*4882a593Smuzhiyun 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1219*4882a593Smuzhiyun 	s3cdma->base = devm_ioremap_resource(&pdev->dev, res);
1220*4882a593Smuzhiyun 	if (IS_ERR(s3cdma->base))
1221*4882a593Smuzhiyun 		return PTR_ERR(s3cdma->base);
1222*4882a593Smuzhiyun 
1223*4882a593Smuzhiyun 	s3cdma->phy_chans = devm_kcalloc(&pdev->dev,
1224*4882a593Smuzhiyun 					      pdata->num_phy_channels,
1225*4882a593Smuzhiyun 					      sizeof(struct s3c24xx_dma_phy),
1226*4882a593Smuzhiyun 					      GFP_KERNEL);
1227*4882a593Smuzhiyun 	if (!s3cdma->phy_chans)
1228*4882a593Smuzhiyun 		return -ENOMEM;
1229*4882a593Smuzhiyun 
1230*4882a593Smuzhiyun 	/* acquire irqs and clocks for all physical channels */
1231*4882a593Smuzhiyun 	for (i = 0; i < pdata->num_phy_channels; i++) {
1232*4882a593Smuzhiyun 		struct s3c24xx_dma_phy *phy = &s3cdma->phy_chans[i];
1233*4882a593Smuzhiyun 		char clk_name[6];
1234*4882a593Smuzhiyun 
1235*4882a593Smuzhiyun 		phy->id = i;
1236*4882a593Smuzhiyun 		phy->base = s3cdma->base + (i * sdata->stride);
1237*4882a593Smuzhiyun 		phy->host = s3cdma;
1238*4882a593Smuzhiyun 
1239*4882a593Smuzhiyun 		phy->irq = platform_get_irq(pdev, i);
1240*4882a593Smuzhiyun 		if (phy->irq < 0)
1241*4882a593Smuzhiyun 			continue;
1242*4882a593Smuzhiyun 
1243*4882a593Smuzhiyun 		ret = devm_request_irq(&pdev->dev, phy->irq, s3c24xx_dma_irq,
1244*4882a593Smuzhiyun 				       0, pdev->name, phy);
1245*4882a593Smuzhiyun 		if (ret) {
1246*4882a593Smuzhiyun 			dev_err(&pdev->dev, "Unable to request irq for channel %d, error %d\n",
1247*4882a593Smuzhiyun 				i, ret);
1248*4882a593Smuzhiyun 			continue;
1249*4882a593Smuzhiyun 		}
1250*4882a593Smuzhiyun 
1251*4882a593Smuzhiyun 		if (sdata->has_clocks) {
1252*4882a593Smuzhiyun 			sprintf(clk_name, "dma.%d", i);
1253*4882a593Smuzhiyun 			phy->clk = devm_clk_get(&pdev->dev, clk_name);
1254*4882a593Smuzhiyun 			if (IS_ERR(phy->clk) && sdata->has_clocks) {
1255*4882a593Smuzhiyun 				dev_err(&pdev->dev, "unable to acquire clock for channel %d, error %lu\n",
1256*4882a593Smuzhiyun 					i, PTR_ERR(phy->clk));
1257*4882a593Smuzhiyun 				continue;
1258*4882a593Smuzhiyun 			}
1259*4882a593Smuzhiyun 
1260*4882a593Smuzhiyun 			ret = clk_prepare(phy->clk);
1261*4882a593Smuzhiyun 			if (ret) {
1262*4882a593Smuzhiyun 				dev_err(&pdev->dev, "clock for phy %d failed, error %d\n",
1263*4882a593Smuzhiyun 					i, ret);
1264*4882a593Smuzhiyun 				continue;
1265*4882a593Smuzhiyun 			}
1266*4882a593Smuzhiyun 		}
1267*4882a593Smuzhiyun 
1268*4882a593Smuzhiyun 		spin_lock_init(&phy->lock);
1269*4882a593Smuzhiyun 		phy->valid = true;
1270*4882a593Smuzhiyun 
1271*4882a593Smuzhiyun 		dev_dbg(&pdev->dev, "physical channel %d is %s\n",
1272*4882a593Smuzhiyun 			i, s3c24xx_dma_phy_busy(phy) ? "BUSY" : "FREE");
1273*4882a593Smuzhiyun 	}
1274*4882a593Smuzhiyun 
1275*4882a593Smuzhiyun 	/* Initialize memcpy engine */
1276*4882a593Smuzhiyun 	dma_cap_set(DMA_MEMCPY, s3cdma->memcpy.cap_mask);
1277*4882a593Smuzhiyun 	dma_cap_set(DMA_PRIVATE, s3cdma->memcpy.cap_mask);
1278*4882a593Smuzhiyun 	s3cdma->memcpy.dev = &pdev->dev;
1279*4882a593Smuzhiyun 	s3cdma->memcpy.device_free_chan_resources =
1280*4882a593Smuzhiyun 					s3c24xx_dma_free_chan_resources;
1281*4882a593Smuzhiyun 	s3cdma->memcpy.device_prep_dma_memcpy = s3c24xx_dma_prep_memcpy;
1282*4882a593Smuzhiyun 	s3cdma->memcpy.device_tx_status = s3c24xx_dma_tx_status;
1283*4882a593Smuzhiyun 	s3cdma->memcpy.device_issue_pending = s3c24xx_dma_issue_pending;
1284*4882a593Smuzhiyun 	s3cdma->memcpy.device_config = s3c24xx_dma_set_runtime_config;
1285*4882a593Smuzhiyun 	s3cdma->memcpy.device_terminate_all = s3c24xx_dma_terminate_all;
1286*4882a593Smuzhiyun 	s3cdma->memcpy.device_synchronize = s3c24xx_dma_synchronize;
1287*4882a593Smuzhiyun 
1288*4882a593Smuzhiyun 	/* Initialize slave engine for SoC internal dedicated peripherals */
1289*4882a593Smuzhiyun 	dma_cap_set(DMA_SLAVE, s3cdma->slave.cap_mask);
1290*4882a593Smuzhiyun 	dma_cap_set(DMA_CYCLIC, s3cdma->slave.cap_mask);
1291*4882a593Smuzhiyun 	dma_cap_set(DMA_PRIVATE, s3cdma->slave.cap_mask);
1292*4882a593Smuzhiyun 	s3cdma->slave.dev = &pdev->dev;
1293*4882a593Smuzhiyun 	s3cdma->slave.device_free_chan_resources =
1294*4882a593Smuzhiyun 					s3c24xx_dma_free_chan_resources;
1295*4882a593Smuzhiyun 	s3cdma->slave.device_tx_status = s3c24xx_dma_tx_status;
1296*4882a593Smuzhiyun 	s3cdma->slave.device_issue_pending = s3c24xx_dma_issue_pending;
1297*4882a593Smuzhiyun 	s3cdma->slave.device_prep_slave_sg = s3c24xx_dma_prep_slave_sg;
1298*4882a593Smuzhiyun 	s3cdma->slave.device_prep_dma_cyclic = s3c24xx_dma_prep_dma_cyclic;
1299*4882a593Smuzhiyun 	s3cdma->slave.device_config = s3c24xx_dma_set_runtime_config;
1300*4882a593Smuzhiyun 	s3cdma->slave.device_terminate_all = s3c24xx_dma_terminate_all;
1301*4882a593Smuzhiyun 	s3cdma->slave.device_synchronize = s3c24xx_dma_synchronize;
1302*4882a593Smuzhiyun 	s3cdma->slave.filter.map = pdata->slave_map;
1303*4882a593Smuzhiyun 	s3cdma->slave.filter.mapcnt = pdata->slavecnt;
1304*4882a593Smuzhiyun 	s3cdma->slave.filter.fn = s3c24xx_dma_filter;
1305*4882a593Smuzhiyun 
1306*4882a593Smuzhiyun 	/* Register as many memcpy channels as there are physical channels */
1307*4882a593Smuzhiyun 	ret = s3c24xx_dma_init_virtual_channels(s3cdma, &s3cdma->memcpy,
1308*4882a593Smuzhiyun 						pdata->num_phy_channels, false);
1309*4882a593Smuzhiyun 	if (ret <= 0) {
1310*4882a593Smuzhiyun 		dev_warn(&pdev->dev,
1311*4882a593Smuzhiyun 			 "%s failed to enumerate memcpy channels - %d\n",
1312*4882a593Smuzhiyun 			 __func__, ret);
1313*4882a593Smuzhiyun 		goto err_memcpy;
1314*4882a593Smuzhiyun 	}
1315*4882a593Smuzhiyun 
1316*4882a593Smuzhiyun 	/* Register slave channels */
1317*4882a593Smuzhiyun 	ret = s3c24xx_dma_init_virtual_channels(s3cdma, &s3cdma->slave,
1318*4882a593Smuzhiyun 				pdata->num_channels, true);
1319*4882a593Smuzhiyun 	if (ret <= 0) {
1320*4882a593Smuzhiyun 		dev_warn(&pdev->dev,
1321*4882a593Smuzhiyun 			"%s failed to enumerate slave channels - %d\n",
1322*4882a593Smuzhiyun 				__func__, ret);
1323*4882a593Smuzhiyun 		goto err_slave;
1324*4882a593Smuzhiyun 	}
1325*4882a593Smuzhiyun 
1326*4882a593Smuzhiyun 	ret = dma_async_device_register(&s3cdma->memcpy);
1327*4882a593Smuzhiyun 	if (ret) {
1328*4882a593Smuzhiyun 		dev_warn(&pdev->dev,
1329*4882a593Smuzhiyun 			"%s failed to register memcpy as an async device - %d\n",
1330*4882a593Smuzhiyun 			__func__, ret);
1331*4882a593Smuzhiyun 		goto err_memcpy_reg;
1332*4882a593Smuzhiyun 	}
1333*4882a593Smuzhiyun 
1334*4882a593Smuzhiyun 	ret = dma_async_device_register(&s3cdma->slave);
1335*4882a593Smuzhiyun 	if (ret) {
1336*4882a593Smuzhiyun 		dev_warn(&pdev->dev,
1337*4882a593Smuzhiyun 			"%s failed to register slave as an async device - %d\n",
1338*4882a593Smuzhiyun 			__func__, ret);
1339*4882a593Smuzhiyun 		goto err_slave_reg;
1340*4882a593Smuzhiyun 	}
1341*4882a593Smuzhiyun 
1342*4882a593Smuzhiyun 	platform_set_drvdata(pdev, s3cdma);
1343*4882a593Smuzhiyun 	dev_info(&pdev->dev, "Loaded dma driver with %d physical channels\n",
1344*4882a593Smuzhiyun 		 pdata->num_phy_channels);
1345*4882a593Smuzhiyun 
1346*4882a593Smuzhiyun 	return 0;
1347*4882a593Smuzhiyun 
1348*4882a593Smuzhiyun err_slave_reg:
1349*4882a593Smuzhiyun 	dma_async_device_unregister(&s3cdma->memcpy);
1350*4882a593Smuzhiyun err_memcpy_reg:
1351*4882a593Smuzhiyun 	s3c24xx_dma_free_virtual_channels(&s3cdma->slave);
1352*4882a593Smuzhiyun err_slave:
1353*4882a593Smuzhiyun 	s3c24xx_dma_free_virtual_channels(&s3cdma->memcpy);
1354*4882a593Smuzhiyun err_memcpy:
1355*4882a593Smuzhiyun 	if (sdata->has_clocks)
1356*4882a593Smuzhiyun 		for (i = 0; i < pdata->num_phy_channels; i++) {
1357*4882a593Smuzhiyun 			struct s3c24xx_dma_phy *phy = &s3cdma->phy_chans[i];
1358*4882a593Smuzhiyun 			if (phy->valid)
1359*4882a593Smuzhiyun 				clk_unprepare(phy->clk);
1360*4882a593Smuzhiyun 		}
1361*4882a593Smuzhiyun 
1362*4882a593Smuzhiyun 	return ret;
1363*4882a593Smuzhiyun }
1364*4882a593Smuzhiyun 
s3c24xx_dma_free_irq(struct platform_device * pdev,struct s3c24xx_dma_engine * s3cdma)1365*4882a593Smuzhiyun static void s3c24xx_dma_free_irq(struct platform_device *pdev,
1366*4882a593Smuzhiyun 				struct s3c24xx_dma_engine *s3cdma)
1367*4882a593Smuzhiyun {
1368*4882a593Smuzhiyun 	int i;
1369*4882a593Smuzhiyun 
1370*4882a593Smuzhiyun 	for (i = 0; i < s3cdma->pdata->num_phy_channels; i++) {
1371*4882a593Smuzhiyun 		struct s3c24xx_dma_phy *phy = &s3cdma->phy_chans[i];
1372*4882a593Smuzhiyun 
1373*4882a593Smuzhiyun 		devm_free_irq(&pdev->dev, phy->irq, phy);
1374*4882a593Smuzhiyun 	}
1375*4882a593Smuzhiyun }
1376*4882a593Smuzhiyun 
s3c24xx_dma_remove(struct platform_device * pdev)1377*4882a593Smuzhiyun static int s3c24xx_dma_remove(struct platform_device *pdev)
1378*4882a593Smuzhiyun {
1379*4882a593Smuzhiyun 	const struct s3c24xx_dma_platdata *pdata = dev_get_platdata(&pdev->dev);
1380*4882a593Smuzhiyun 	struct s3c24xx_dma_engine *s3cdma = platform_get_drvdata(pdev);
1381*4882a593Smuzhiyun 	struct soc_data *sdata = s3c24xx_dma_get_soc_data(pdev);
1382*4882a593Smuzhiyun 	int i;
1383*4882a593Smuzhiyun 
1384*4882a593Smuzhiyun 	dma_async_device_unregister(&s3cdma->slave);
1385*4882a593Smuzhiyun 	dma_async_device_unregister(&s3cdma->memcpy);
1386*4882a593Smuzhiyun 
1387*4882a593Smuzhiyun 	s3c24xx_dma_free_irq(pdev, s3cdma);
1388*4882a593Smuzhiyun 
1389*4882a593Smuzhiyun 	s3c24xx_dma_free_virtual_channels(&s3cdma->slave);
1390*4882a593Smuzhiyun 	s3c24xx_dma_free_virtual_channels(&s3cdma->memcpy);
1391*4882a593Smuzhiyun 
1392*4882a593Smuzhiyun 	if (sdata->has_clocks)
1393*4882a593Smuzhiyun 		for (i = 0; i < pdata->num_phy_channels; i++) {
1394*4882a593Smuzhiyun 			struct s3c24xx_dma_phy *phy = &s3cdma->phy_chans[i];
1395*4882a593Smuzhiyun 			if (phy->valid)
1396*4882a593Smuzhiyun 				clk_unprepare(phy->clk);
1397*4882a593Smuzhiyun 		}
1398*4882a593Smuzhiyun 
1399*4882a593Smuzhiyun 	return 0;
1400*4882a593Smuzhiyun }
1401*4882a593Smuzhiyun 
1402*4882a593Smuzhiyun static struct platform_driver s3c24xx_dma_driver = {
1403*4882a593Smuzhiyun 	.driver		= {
1404*4882a593Smuzhiyun 		.name	= "s3c24xx-dma",
1405*4882a593Smuzhiyun 	},
1406*4882a593Smuzhiyun 	.id_table	= s3c24xx_dma_driver_ids,
1407*4882a593Smuzhiyun 	.probe		= s3c24xx_dma_probe,
1408*4882a593Smuzhiyun 	.remove		= s3c24xx_dma_remove,
1409*4882a593Smuzhiyun };
1410*4882a593Smuzhiyun 
1411*4882a593Smuzhiyun module_platform_driver(s3c24xx_dma_driver);
1412*4882a593Smuzhiyun 
s3c24xx_dma_filter(struct dma_chan * chan,void * param)1413*4882a593Smuzhiyun bool s3c24xx_dma_filter(struct dma_chan *chan, void *param)
1414*4882a593Smuzhiyun {
1415*4882a593Smuzhiyun 	struct s3c24xx_dma_chan *s3cchan;
1416*4882a593Smuzhiyun 
1417*4882a593Smuzhiyun 	if (chan->device->dev->driver != &s3c24xx_dma_driver.driver)
1418*4882a593Smuzhiyun 		return false;
1419*4882a593Smuzhiyun 
1420*4882a593Smuzhiyun 	s3cchan = to_s3c24xx_dma_chan(chan);
1421*4882a593Smuzhiyun 
1422*4882a593Smuzhiyun 	return s3cchan->id == (uintptr_t)param;
1423*4882a593Smuzhiyun }
1424*4882a593Smuzhiyun EXPORT_SYMBOL(s3c24xx_dma_filter);
1425*4882a593Smuzhiyun 
1426*4882a593Smuzhiyun MODULE_DESCRIPTION("S3C24XX DMA Driver");
1427*4882a593Smuzhiyun MODULE_AUTHOR("Heiko Stuebner");
1428*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
1429