xref: /OK3568_Linux_fs/kernel/drivers/dma/ep93xx_dma.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Driver for the Cirrus Logic EP93xx DMA Controller
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2011 Mika Westerberg
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * DMA M2P implementation is based on the original
8*4882a593Smuzhiyun  * arch/arm/mach-ep93xx/dma-m2p.c which has following copyrights:
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  *   Copyright (C) 2006 Lennert Buytenhek <buytenh@wantstofly.org>
11*4882a593Smuzhiyun  *   Copyright (C) 2006 Applied Data Systems
12*4882a593Smuzhiyun  *   Copyright (C) 2009 Ryan Mallon <rmallon@gmail.com>
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * This driver is based on dw_dmac and amba-pl08x drivers.
15*4882a593Smuzhiyun  */
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include <linux/clk.h>
18*4882a593Smuzhiyun #include <linux/init.h>
19*4882a593Smuzhiyun #include <linux/interrupt.h>
20*4882a593Smuzhiyun #include <linux/dmaengine.h>
21*4882a593Smuzhiyun #include <linux/module.h>
22*4882a593Smuzhiyun #include <linux/mod_devicetable.h>
23*4882a593Smuzhiyun #include <linux/platform_device.h>
24*4882a593Smuzhiyun #include <linux/slab.h>
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun #include <linux/platform_data/dma-ep93xx.h>
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun #include "dmaengine.h"
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun /* M2P registers */
31*4882a593Smuzhiyun #define M2P_CONTROL			0x0000
32*4882a593Smuzhiyun #define M2P_CONTROL_STALLINT		BIT(0)
33*4882a593Smuzhiyun #define M2P_CONTROL_NFBINT		BIT(1)
34*4882a593Smuzhiyun #define M2P_CONTROL_CH_ERROR_INT	BIT(3)
35*4882a593Smuzhiyun #define M2P_CONTROL_ENABLE		BIT(4)
36*4882a593Smuzhiyun #define M2P_CONTROL_ICE			BIT(6)
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun #define M2P_INTERRUPT			0x0004
39*4882a593Smuzhiyun #define M2P_INTERRUPT_STALL		BIT(0)
40*4882a593Smuzhiyun #define M2P_INTERRUPT_NFB		BIT(1)
41*4882a593Smuzhiyun #define M2P_INTERRUPT_ERROR		BIT(3)
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun #define M2P_PPALLOC			0x0008
44*4882a593Smuzhiyun #define M2P_STATUS			0x000c
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun #define M2P_MAXCNT0			0x0020
47*4882a593Smuzhiyun #define M2P_BASE0			0x0024
48*4882a593Smuzhiyun #define M2P_MAXCNT1			0x0030
49*4882a593Smuzhiyun #define M2P_BASE1			0x0034
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun #define M2P_STATE_IDLE			0
52*4882a593Smuzhiyun #define M2P_STATE_STALL			1
53*4882a593Smuzhiyun #define M2P_STATE_ON			2
54*4882a593Smuzhiyun #define M2P_STATE_NEXT			3
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun /* M2M registers */
57*4882a593Smuzhiyun #define M2M_CONTROL			0x0000
58*4882a593Smuzhiyun #define M2M_CONTROL_DONEINT		BIT(2)
59*4882a593Smuzhiyun #define M2M_CONTROL_ENABLE		BIT(3)
60*4882a593Smuzhiyun #define M2M_CONTROL_START		BIT(4)
61*4882a593Smuzhiyun #define M2M_CONTROL_DAH			BIT(11)
62*4882a593Smuzhiyun #define M2M_CONTROL_SAH			BIT(12)
63*4882a593Smuzhiyun #define M2M_CONTROL_PW_SHIFT		9
64*4882a593Smuzhiyun #define M2M_CONTROL_PW_8		(0 << M2M_CONTROL_PW_SHIFT)
65*4882a593Smuzhiyun #define M2M_CONTROL_PW_16		(1 << M2M_CONTROL_PW_SHIFT)
66*4882a593Smuzhiyun #define M2M_CONTROL_PW_32		(2 << M2M_CONTROL_PW_SHIFT)
67*4882a593Smuzhiyun #define M2M_CONTROL_PW_MASK		(3 << M2M_CONTROL_PW_SHIFT)
68*4882a593Smuzhiyun #define M2M_CONTROL_TM_SHIFT		13
69*4882a593Smuzhiyun #define M2M_CONTROL_TM_TX		(1 << M2M_CONTROL_TM_SHIFT)
70*4882a593Smuzhiyun #define M2M_CONTROL_TM_RX		(2 << M2M_CONTROL_TM_SHIFT)
71*4882a593Smuzhiyun #define M2M_CONTROL_NFBINT		BIT(21)
72*4882a593Smuzhiyun #define M2M_CONTROL_RSS_SHIFT		22
73*4882a593Smuzhiyun #define M2M_CONTROL_RSS_SSPRX		(1 << M2M_CONTROL_RSS_SHIFT)
74*4882a593Smuzhiyun #define M2M_CONTROL_RSS_SSPTX		(2 << M2M_CONTROL_RSS_SHIFT)
75*4882a593Smuzhiyun #define M2M_CONTROL_RSS_IDE		(3 << M2M_CONTROL_RSS_SHIFT)
76*4882a593Smuzhiyun #define M2M_CONTROL_NO_HDSK		BIT(24)
77*4882a593Smuzhiyun #define M2M_CONTROL_PWSC_SHIFT		25
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun #define M2M_INTERRUPT			0x0004
80*4882a593Smuzhiyun #define M2M_INTERRUPT_MASK		6
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun #define M2M_STATUS			0x000c
83*4882a593Smuzhiyun #define M2M_STATUS_CTL_SHIFT		1
84*4882a593Smuzhiyun #define M2M_STATUS_CTL_IDLE		(0 << M2M_STATUS_CTL_SHIFT)
85*4882a593Smuzhiyun #define M2M_STATUS_CTL_STALL		(1 << M2M_STATUS_CTL_SHIFT)
86*4882a593Smuzhiyun #define M2M_STATUS_CTL_MEMRD		(2 << M2M_STATUS_CTL_SHIFT)
87*4882a593Smuzhiyun #define M2M_STATUS_CTL_MEMWR		(3 << M2M_STATUS_CTL_SHIFT)
88*4882a593Smuzhiyun #define M2M_STATUS_CTL_BWCWAIT		(4 << M2M_STATUS_CTL_SHIFT)
89*4882a593Smuzhiyun #define M2M_STATUS_CTL_MASK		(7 << M2M_STATUS_CTL_SHIFT)
90*4882a593Smuzhiyun #define M2M_STATUS_BUF_SHIFT		4
91*4882a593Smuzhiyun #define M2M_STATUS_BUF_NO		(0 << M2M_STATUS_BUF_SHIFT)
92*4882a593Smuzhiyun #define M2M_STATUS_BUF_ON		(1 << M2M_STATUS_BUF_SHIFT)
93*4882a593Smuzhiyun #define M2M_STATUS_BUF_NEXT		(2 << M2M_STATUS_BUF_SHIFT)
94*4882a593Smuzhiyun #define M2M_STATUS_BUF_MASK		(3 << M2M_STATUS_BUF_SHIFT)
95*4882a593Smuzhiyun #define M2M_STATUS_DONE			BIT(6)
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun #define M2M_BCR0			0x0010
98*4882a593Smuzhiyun #define M2M_BCR1			0x0014
99*4882a593Smuzhiyun #define M2M_SAR_BASE0			0x0018
100*4882a593Smuzhiyun #define M2M_SAR_BASE1			0x001c
101*4882a593Smuzhiyun #define M2M_DAR_BASE0			0x002c
102*4882a593Smuzhiyun #define M2M_DAR_BASE1			0x0030
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun #define DMA_MAX_CHAN_BYTES		0xffff
105*4882a593Smuzhiyun #define DMA_MAX_CHAN_DESCRIPTORS	32
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun struct ep93xx_dma_engine;
108*4882a593Smuzhiyun static int ep93xx_dma_slave_config_write(struct dma_chan *chan,
109*4882a593Smuzhiyun 					 enum dma_transfer_direction dir,
110*4882a593Smuzhiyun 					 struct dma_slave_config *config);
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun /**
113*4882a593Smuzhiyun  * struct ep93xx_dma_desc - EP93xx specific transaction descriptor
114*4882a593Smuzhiyun  * @src_addr: source address of the transaction
115*4882a593Smuzhiyun  * @dst_addr: destination address of the transaction
116*4882a593Smuzhiyun  * @size: size of the transaction (in bytes)
117*4882a593Smuzhiyun  * @complete: this descriptor is completed
118*4882a593Smuzhiyun  * @txd: dmaengine API descriptor
119*4882a593Smuzhiyun  * @tx_list: list of linked descriptors
120*4882a593Smuzhiyun  * @node: link used for putting this into a channel queue
121*4882a593Smuzhiyun  */
122*4882a593Smuzhiyun struct ep93xx_dma_desc {
123*4882a593Smuzhiyun 	u32				src_addr;
124*4882a593Smuzhiyun 	u32				dst_addr;
125*4882a593Smuzhiyun 	size_t				size;
126*4882a593Smuzhiyun 	bool				complete;
127*4882a593Smuzhiyun 	struct dma_async_tx_descriptor	txd;
128*4882a593Smuzhiyun 	struct list_head		tx_list;
129*4882a593Smuzhiyun 	struct list_head		node;
130*4882a593Smuzhiyun };
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun /**
133*4882a593Smuzhiyun  * struct ep93xx_dma_chan - an EP93xx DMA M2P/M2M channel
134*4882a593Smuzhiyun  * @chan: dmaengine API channel
135*4882a593Smuzhiyun  * @edma: pointer to to the engine device
136*4882a593Smuzhiyun  * @regs: memory mapped registers
137*4882a593Smuzhiyun  * @irq: interrupt number of the channel
138*4882a593Smuzhiyun  * @clk: clock used by this channel
139*4882a593Smuzhiyun  * @tasklet: channel specific tasklet used for callbacks
140*4882a593Smuzhiyun  * @lock: lock protecting the fields following
141*4882a593Smuzhiyun  * @flags: flags for the channel
142*4882a593Smuzhiyun  * @buffer: which buffer to use next (0/1)
143*4882a593Smuzhiyun  * @active: flattened chain of descriptors currently being processed
144*4882a593Smuzhiyun  * @queue: pending descriptors which are handled next
145*4882a593Smuzhiyun  * @free_list: list of free descriptors which can be used
146*4882a593Smuzhiyun  * @runtime_addr: physical address currently used as dest/src (M2M only). This
147*4882a593Smuzhiyun  *                is set via .device_config before slave operation is
148*4882a593Smuzhiyun  *                prepared
149*4882a593Smuzhiyun  * @runtime_ctrl: M2M runtime values for the control register.
150*4882a593Smuzhiyun  * @slave_config: slave configuration
151*4882a593Smuzhiyun  *
152*4882a593Smuzhiyun  * As EP93xx DMA controller doesn't support real chained DMA descriptors we
153*4882a593Smuzhiyun  * will have slightly different scheme here: @active points to a head of
154*4882a593Smuzhiyun  * flattened DMA descriptor chain.
155*4882a593Smuzhiyun  *
156*4882a593Smuzhiyun  * @queue holds pending transactions. These are linked through the first
157*4882a593Smuzhiyun  * descriptor in the chain. When a descriptor is moved to the @active queue,
158*4882a593Smuzhiyun  * the first and chained descriptors are flattened into a single list.
159*4882a593Smuzhiyun  *
160*4882a593Smuzhiyun  * @chan.private holds pointer to &struct ep93xx_dma_data which contains
161*4882a593Smuzhiyun  * necessary channel configuration information. For memcpy channels this must
162*4882a593Smuzhiyun  * be %NULL.
163*4882a593Smuzhiyun  */
164*4882a593Smuzhiyun struct ep93xx_dma_chan {
165*4882a593Smuzhiyun 	struct dma_chan			chan;
166*4882a593Smuzhiyun 	const struct ep93xx_dma_engine	*edma;
167*4882a593Smuzhiyun 	void __iomem			*regs;
168*4882a593Smuzhiyun 	int				irq;
169*4882a593Smuzhiyun 	struct clk			*clk;
170*4882a593Smuzhiyun 	struct tasklet_struct		tasklet;
171*4882a593Smuzhiyun 	/* protects the fields following */
172*4882a593Smuzhiyun 	spinlock_t			lock;
173*4882a593Smuzhiyun 	unsigned long			flags;
174*4882a593Smuzhiyun /* Channel is configured for cyclic transfers */
175*4882a593Smuzhiyun #define EP93XX_DMA_IS_CYCLIC		0
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	int				buffer;
178*4882a593Smuzhiyun 	struct list_head		active;
179*4882a593Smuzhiyun 	struct list_head		queue;
180*4882a593Smuzhiyun 	struct list_head		free_list;
181*4882a593Smuzhiyun 	u32				runtime_addr;
182*4882a593Smuzhiyun 	u32				runtime_ctrl;
183*4882a593Smuzhiyun 	struct dma_slave_config		slave_config;
184*4882a593Smuzhiyun };
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun /**
187*4882a593Smuzhiyun  * struct ep93xx_dma_engine - the EP93xx DMA engine instance
188*4882a593Smuzhiyun  * @dma_dev: holds the dmaengine device
189*4882a593Smuzhiyun  * @m2m: is this an M2M or M2P device
190*4882a593Smuzhiyun  * @hw_setup: method which sets the channel up for operation
191*4882a593Smuzhiyun  * @hw_synchronize: synchronizes DMA channel termination to current context
192*4882a593Smuzhiyun  * @hw_shutdown: shuts the channel down and flushes whatever is left
193*4882a593Smuzhiyun  * @hw_submit: pushes active descriptor(s) to the hardware
194*4882a593Smuzhiyun  * @hw_interrupt: handle the interrupt
195*4882a593Smuzhiyun  * @num_channels: number of channels for this instance
196*4882a593Smuzhiyun  * @channels: array of channels
197*4882a593Smuzhiyun  *
198*4882a593Smuzhiyun  * There is one instance of this struct for the M2P channels and one for the
199*4882a593Smuzhiyun  * M2M channels. hw_xxx() methods are used to perform operations which are
200*4882a593Smuzhiyun  * different on M2M and M2P channels. These methods are called with channel
201*4882a593Smuzhiyun  * lock held and interrupts disabled so they cannot sleep.
202*4882a593Smuzhiyun  */
203*4882a593Smuzhiyun struct ep93xx_dma_engine {
204*4882a593Smuzhiyun 	struct dma_device	dma_dev;
205*4882a593Smuzhiyun 	bool			m2m;
206*4882a593Smuzhiyun 	int			(*hw_setup)(struct ep93xx_dma_chan *);
207*4882a593Smuzhiyun 	void			(*hw_synchronize)(struct ep93xx_dma_chan *);
208*4882a593Smuzhiyun 	void			(*hw_shutdown)(struct ep93xx_dma_chan *);
209*4882a593Smuzhiyun 	void			(*hw_submit)(struct ep93xx_dma_chan *);
210*4882a593Smuzhiyun 	int			(*hw_interrupt)(struct ep93xx_dma_chan *);
211*4882a593Smuzhiyun #define INTERRUPT_UNKNOWN	0
212*4882a593Smuzhiyun #define INTERRUPT_DONE		1
213*4882a593Smuzhiyun #define INTERRUPT_NEXT_BUFFER	2
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	size_t			num_channels;
216*4882a593Smuzhiyun 	struct ep93xx_dma_chan	channels[];
217*4882a593Smuzhiyun };
218*4882a593Smuzhiyun 
chan2dev(struct ep93xx_dma_chan * edmac)219*4882a593Smuzhiyun static inline struct device *chan2dev(struct ep93xx_dma_chan *edmac)
220*4882a593Smuzhiyun {
221*4882a593Smuzhiyun 	return &edmac->chan.dev->device;
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun 
to_ep93xx_dma_chan(struct dma_chan * chan)224*4882a593Smuzhiyun static struct ep93xx_dma_chan *to_ep93xx_dma_chan(struct dma_chan *chan)
225*4882a593Smuzhiyun {
226*4882a593Smuzhiyun 	return container_of(chan, struct ep93xx_dma_chan, chan);
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun /**
230*4882a593Smuzhiyun  * ep93xx_dma_set_active - set new active descriptor chain
231*4882a593Smuzhiyun  * @edmac: channel
232*4882a593Smuzhiyun  * @desc: head of the new active descriptor chain
233*4882a593Smuzhiyun  *
234*4882a593Smuzhiyun  * Sets @desc to be the head of the new active descriptor chain. This is the
235*4882a593Smuzhiyun  * chain which is processed next. The active list must be empty before calling
236*4882a593Smuzhiyun  * this function.
237*4882a593Smuzhiyun  *
238*4882a593Smuzhiyun  * Called with @edmac->lock held and interrupts disabled.
239*4882a593Smuzhiyun  */
ep93xx_dma_set_active(struct ep93xx_dma_chan * edmac,struct ep93xx_dma_desc * desc)240*4882a593Smuzhiyun static void ep93xx_dma_set_active(struct ep93xx_dma_chan *edmac,
241*4882a593Smuzhiyun 				  struct ep93xx_dma_desc *desc)
242*4882a593Smuzhiyun {
243*4882a593Smuzhiyun 	BUG_ON(!list_empty(&edmac->active));
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	list_add_tail(&desc->node, &edmac->active);
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	/* Flatten the @desc->tx_list chain into @edmac->active list */
248*4882a593Smuzhiyun 	while (!list_empty(&desc->tx_list)) {
249*4882a593Smuzhiyun 		struct ep93xx_dma_desc *d = list_first_entry(&desc->tx_list,
250*4882a593Smuzhiyun 			struct ep93xx_dma_desc, node);
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 		/*
253*4882a593Smuzhiyun 		 * We copy the callback parameters from the first descriptor
254*4882a593Smuzhiyun 		 * to all the chained descriptors. This way we can call the
255*4882a593Smuzhiyun 		 * callback without having to find out the first descriptor in
256*4882a593Smuzhiyun 		 * the chain. Useful for cyclic transfers.
257*4882a593Smuzhiyun 		 */
258*4882a593Smuzhiyun 		d->txd.callback = desc->txd.callback;
259*4882a593Smuzhiyun 		d->txd.callback_param = desc->txd.callback_param;
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 		list_move_tail(&d->node, &edmac->active);
262*4882a593Smuzhiyun 	}
263*4882a593Smuzhiyun }
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun /* Called with @edmac->lock held and interrupts disabled */
266*4882a593Smuzhiyun static struct ep93xx_dma_desc *
ep93xx_dma_get_active(struct ep93xx_dma_chan * edmac)267*4882a593Smuzhiyun ep93xx_dma_get_active(struct ep93xx_dma_chan *edmac)
268*4882a593Smuzhiyun {
269*4882a593Smuzhiyun 	return list_first_entry_or_null(&edmac->active,
270*4882a593Smuzhiyun 					struct ep93xx_dma_desc, node);
271*4882a593Smuzhiyun }
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun /**
274*4882a593Smuzhiyun  * ep93xx_dma_advance_active - advances to the next active descriptor
275*4882a593Smuzhiyun  * @edmac: channel
276*4882a593Smuzhiyun  *
277*4882a593Smuzhiyun  * Function advances active descriptor to the next in the @edmac->active and
278*4882a593Smuzhiyun  * returns %true if we still have descriptors in the chain to process.
279*4882a593Smuzhiyun  * Otherwise returns %false.
280*4882a593Smuzhiyun  *
281*4882a593Smuzhiyun  * When the channel is in cyclic mode always returns %true.
282*4882a593Smuzhiyun  *
283*4882a593Smuzhiyun  * Called with @edmac->lock held and interrupts disabled.
284*4882a593Smuzhiyun  */
ep93xx_dma_advance_active(struct ep93xx_dma_chan * edmac)285*4882a593Smuzhiyun static bool ep93xx_dma_advance_active(struct ep93xx_dma_chan *edmac)
286*4882a593Smuzhiyun {
287*4882a593Smuzhiyun 	struct ep93xx_dma_desc *desc;
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 	list_rotate_left(&edmac->active);
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun 	if (test_bit(EP93XX_DMA_IS_CYCLIC, &edmac->flags))
292*4882a593Smuzhiyun 		return true;
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	desc = ep93xx_dma_get_active(edmac);
295*4882a593Smuzhiyun 	if (!desc)
296*4882a593Smuzhiyun 		return false;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	/*
299*4882a593Smuzhiyun 	 * If txd.cookie is set it means that we are back in the first
300*4882a593Smuzhiyun 	 * descriptor in the chain and hence done with it.
301*4882a593Smuzhiyun 	 */
302*4882a593Smuzhiyun 	return !desc->txd.cookie;
303*4882a593Smuzhiyun }
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun /*
306*4882a593Smuzhiyun  * M2P DMA implementation
307*4882a593Smuzhiyun  */
308*4882a593Smuzhiyun 
m2p_set_control(struct ep93xx_dma_chan * edmac,u32 control)309*4882a593Smuzhiyun static void m2p_set_control(struct ep93xx_dma_chan *edmac, u32 control)
310*4882a593Smuzhiyun {
311*4882a593Smuzhiyun 	writel(control, edmac->regs + M2P_CONTROL);
312*4882a593Smuzhiyun 	/*
313*4882a593Smuzhiyun 	 * EP93xx User's Guide states that we must perform a dummy read after
314*4882a593Smuzhiyun 	 * write to the control register.
315*4882a593Smuzhiyun 	 */
316*4882a593Smuzhiyun 	readl(edmac->regs + M2P_CONTROL);
317*4882a593Smuzhiyun }
318*4882a593Smuzhiyun 
m2p_hw_setup(struct ep93xx_dma_chan * edmac)319*4882a593Smuzhiyun static int m2p_hw_setup(struct ep93xx_dma_chan *edmac)
320*4882a593Smuzhiyun {
321*4882a593Smuzhiyun 	struct ep93xx_dma_data *data = edmac->chan.private;
322*4882a593Smuzhiyun 	u32 control;
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	writel(data->port & 0xf, edmac->regs + M2P_PPALLOC);
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	control = M2P_CONTROL_CH_ERROR_INT | M2P_CONTROL_ICE
327*4882a593Smuzhiyun 		| M2P_CONTROL_ENABLE;
328*4882a593Smuzhiyun 	m2p_set_control(edmac, control);
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	edmac->buffer = 0;
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun 	return 0;
333*4882a593Smuzhiyun }
334*4882a593Smuzhiyun 
m2p_channel_state(struct ep93xx_dma_chan * edmac)335*4882a593Smuzhiyun static inline u32 m2p_channel_state(struct ep93xx_dma_chan *edmac)
336*4882a593Smuzhiyun {
337*4882a593Smuzhiyun 	return (readl(edmac->regs + M2P_STATUS) >> 4) & 0x3;
338*4882a593Smuzhiyun }
339*4882a593Smuzhiyun 
m2p_hw_synchronize(struct ep93xx_dma_chan * edmac)340*4882a593Smuzhiyun static void m2p_hw_synchronize(struct ep93xx_dma_chan *edmac)
341*4882a593Smuzhiyun {
342*4882a593Smuzhiyun 	unsigned long flags;
343*4882a593Smuzhiyun 	u32 control;
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 	spin_lock_irqsave(&edmac->lock, flags);
346*4882a593Smuzhiyun 	control = readl(edmac->regs + M2P_CONTROL);
347*4882a593Smuzhiyun 	control &= ~(M2P_CONTROL_STALLINT | M2P_CONTROL_NFBINT);
348*4882a593Smuzhiyun 	m2p_set_control(edmac, control);
349*4882a593Smuzhiyun 	spin_unlock_irqrestore(&edmac->lock, flags);
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 	while (m2p_channel_state(edmac) >= M2P_STATE_ON)
352*4882a593Smuzhiyun 		schedule();
353*4882a593Smuzhiyun }
354*4882a593Smuzhiyun 
m2p_hw_shutdown(struct ep93xx_dma_chan * edmac)355*4882a593Smuzhiyun static void m2p_hw_shutdown(struct ep93xx_dma_chan *edmac)
356*4882a593Smuzhiyun {
357*4882a593Smuzhiyun 	m2p_set_control(edmac, 0);
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	while (m2p_channel_state(edmac) != M2P_STATE_IDLE)
360*4882a593Smuzhiyun 		dev_warn(chan2dev(edmac), "M2P: Not yet IDLE\n");
361*4882a593Smuzhiyun }
362*4882a593Smuzhiyun 
m2p_fill_desc(struct ep93xx_dma_chan * edmac)363*4882a593Smuzhiyun static void m2p_fill_desc(struct ep93xx_dma_chan *edmac)
364*4882a593Smuzhiyun {
365*4882a593Smuzhiyun 	struct ep93xx_dma_desc *desc;
366*4882a593Smuzhiyun 	u32 bus_addr;
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun 	desc = ep93xx_dma_get_active(edmac);
369*4882a593Smuzhiyun 	if (!desc) {
370*4882a593Smuzhiyun 		dev_warn(chan2dev(edmac), "M2P: empty descriptor list\n");
371*4882a593Smuzhiyun 		return;
372*4882a593Smuzhiyun 	}
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun 	if (ep93xx_dma_chan_direction(&edmac->chan) == DMA_MEM_TO_DEV)
375*4882a593Smuzhiyun 		bus_addr = desc->src_addr;
376*4882a593Smuzhiyun 	else
377*4882a593Smuzhiyun 		bus_addr = desc->dst_addr;
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 	if (edmac->buffer == 0) {
380*4882a593Smuzhiyun 		writel(desc->size, edmac->regs + M2P_MAXCNT0);
381*4882a593Smuzhiyun 		writel(bus_addr, edmac->regs + M2P_BASE0);
382*4882a593Smuzhiyun 	} else {
383*4882a593Smuzhiyun 		writel(desc->size, edmac->regs + M2P_MAXCNT1);
384*4882a593Smuzhiyun 		writel(bus_addr, edmac->regs + M2P_BASE1);
385*4882a593Smuzhiyun 	}
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 	edmac->buffer ^= 1;
388*4882a593Smuzhiyun }
389*4882a593Smuzhiyun 
m2p_hw_submit(struct ep93xx_dma_chan * edmac)390*4882a593Smuzhiyun static void m2p_hw_submit(struct ep93xx_dma_chan *edmac)
391*4882a593Smuzhiyun {
392*4882a593Smuzhiyun 	u32 control = readl(edmac->regs + M2P_CONTROL);
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 	m2p_fill_desc(edmac);
395*4882a593Smuzhiyun 	control |= M2P_CONTROL_STALLINT;
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	if (ep93xx_dma_advance_active(edmac)) {
398*4882a593Smuzhiyun 		m2p_fill_desc(edmac);
399*4882a593Smuzhiyun 		control |= M2P_CONTROL_NFBINT;
400*4882a593Smuzhiyun 	}
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun 	m2p_set_control(edmac, control);
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun 
m2p_hw_interrupt(struct ep93xx_dma_chan * edmac)405*4882a593Smuzhiyun static int m2p_hw_interrupt(struct ep93xx_dma_chan *edmac)
406*4882a593Smuzhiyun {
407*4882a593Smuzhiyun 	u32 irq_status = readl(edmac->regs + M2P_INTERRUPT);
408*4882a593Smuzhiyun 	u32 control;
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun 	if (irq_status & M2P_INTERRUPT_ERROR) {
411*4882a593Smuzhiyun 		struct ep93xx_dma_desc *desc = ep93xx_dma_get_active(edmac);
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun 		/* Clear the error interrupt */
414*4882a593Smuzhiyun 		writel(1, edmac->regs + M2P_INTERRUPT);
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun 		/*
417*4882a593Smuzhiyun 		 * It seems that there is no easy way of reporting errors back
418*4882a593Smuzhiyun 		 * to client so we just report the error here and continue as
419*4882a593Smuzhiyun 		 * usual.
420*4882a593Smuzhiyun 		 *
421*4882a593Smuzhiyun 		 * Revisit this when there is a mechanism to report back the
422*4882a593Smuzhiyun 		 * errors.
423*4882a593Smuzhiyun 		 */
424*4882a593Smuzhiyun 		dev_err(chan2dev(edmac),
425*4882a593Smuzhiyun 			"DMA transfer failed! Details:\n"
426*4882a593Smuzhiyun 			"\tcookie	: %d\n"
427*4882a593Smuzhiyun 			"\tsrc_addr	: 0x%08x\n"
428*4882a593Smuzhiyun 			"\tdst_addr	: 0x%08x\n"
429*4882a593Smuzhiyun 			"\tsize		: %zu\n",
430*4882a593Smuzhiyun 			desc->txd.cookie, desc->src_addr, desc->dst_addr,
431*4882a593Smuzhiyun 			desc->size);
432*4882a593Smuzhiyun 	}
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 	/*
435*4882a593Smuzhiyun 	 * Even latest E2 silicon revision sometimes assert STALL interrupt
436*4882a593Smuzhiyun 	 * instead of NFB. Therefore we treat them equally, basing on the
437*4882a593Smuzhiyun 	 * amount of data we still have to transfer.
438*4882a593Smuzhiyun 	 */
439*4882a593Smuzhiyun 	if (!(irq_status & (M2P_INTERRUPT_STALL | M2P_INTERRUPT_NFB)))
440*4882a593Smuzhiyun 		return INTERRUPT_UNKNOWN;
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 	if (ep93xx_dma_advance_active(edmac)) {
443*4882a593Smuzhiyun 		m2p_fill_desc(edmac);
444*4882a593Smuzhiyun 		return INTERRUPT_NEXT_BUFFER;
445*4882a593Smuzhiyun 	}
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun 	/* Disable interrupts */
448*4882a593Smuzhiyun 	control = readl(edmac->regs + M2P_CONTROL);
449*4882a593Smuzhiyun 	control &= ~(M2P_CONTROL_STALLINT | M2P_CONTROL_NFBINT);
450*4882a593Smuzhiyun 	m2p_set_control(edmac, control);
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun 	return INTERRUPT_DONE;
453*4882a593Smuzhiyun }
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun /*
456*4882a593Smuzhiyun  * M2M DMA implementation
457*4882a593Smuzhiyun  */
458*4882a593Smuzhiyun 
m2m_hw_setup(struct ep93xx_dma_chan * edmac)459*4882a593Smuzhiyun static int m2m_hw_setup(struct ep93xx_dma_chan *edmac)
460*4882a593Smuzhiyun {
461*4882a593Smuzhiyun 	const struct ep93xx_dma_data *data = edmac->chan.private;
462*4882a593Smuzhiyun 	u32 control = 0;
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun 	if (!data) {
465*4882a593Smuzhiyun 		/* This is memcpy channel, nothing to configure */
466*4882a593Smuzhiyun 		writel(control, edmac->regs + M2M_CONTROL);
467*4882a593Smuzhiyun 		return 0;
468*4882a593Smuzhiyun 	}
469*4882a593Smuzhiyun 
470*4882a593Smuzhiyun 	switch (data->port) {
471*4882a593Smuzhiyun 	case EP93XX_DMA_SSP:
472*4882a593Smuzhiyun 		/*
473*4882a593Smuzhiyun 		 * This was found via experimenting - anything less than 5
474*4882a593Smuzhiyun 		 * causes the channel to perform only a partial transfer which
475*4882a593Smuzhiyun 		 * leads to problems since we don't get DONE interrupt then.
476*4882a593Smuzhiyun 		 */
477*4882a593Smuzhiyun 		control = (5 << M2M_CONTROL_PWSC_SHIFT);
478*4882a593Smuzhiyun 		control |= M2M_CONTROL_NO_HDSK;
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun 		if (data->direction == DMA_MEM_TO_DEV) {
481*4882a593Smuzhiyun 			control |= M2M_CONTROL_DAH;
482*4882a593Smuzhiyun 			control |= M2M_CONTROL_TM_TX;
483*4882a593Smuzhiyun 			control |= M2M_CONTROL_RSS_SSPTX;
484*4882a593Smuzhiyun 		} else {
485*4882a593Smuzhiyun 			control |= M2M_CONTROL_SAH;
486*4882a593Smuzhiyun 			control |= M2M_CONTROL_TM_RX;
487*4882a593Smuzhiyun 			control |= M2M_CONTROL_RSS_SSPRX;
488*4882a593Smuzhiyun 		}
489*4882a593Smuzhiyun 		break;
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 	case EP93XX_DMA_IDE:
492*4882a593Smuzhiyun 		/*
493*4882a593Smuzhiyun 		 * This IDE part is totally untested. Values below are taken
494*4882a593Smuzhiyun 		 * from the EP93xx Users's Guide and might not be correct.
495*4882a593Smuzhiyun 		 */
496*4882a593Smuzhiyun 		if (data->direction == DMA_MEM_TO_DEV) {
497*4882a593Smuzhiyun 			/* Worst case from the UG */
498*4882a593Smuzhiyun 			control = (3 << M2M_CONTROL_PWSC_SHIFT);
499*4882a593Smuzhiyun 			control |= M2M_CONTROL_DAH;
500*4882a593Smuzhiyun 			control |= M2M_CONTROL_TM_TX;
501*4882a593Smuzhiyun 		} else {
502*4882a593Smuzhiyun 			control = (2 << M2M_CONTROL_PWSC_SHIFT);
503*4882a593Smuzhiyun 			control |= M2M_CONTROL_SAH;
504*4882a593Smuzhiyun 			control |= M2M_CONTROL_TM_RX;
505*4882a593Smuzhiyun 		}
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 		control |= M2M_CONTROL_NO_HDSK;
508*4882a593Smuzhiyun 		control |= M2M_CONTROL_RSS_IDE;
509*4882a593Smuzhiyun 		control |= M2M_CONTROL_PW_16;
510*4882a593Smuzhiyun 		break;
511*4882a593Smuzhiyun 
512*4882a593Smuzhiyun 	default:
513*4882a593Smuzhiyun 		return -EINVAL;
514*4882a593Smuzhiyun 	}
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 	writel(control, edmac->regs + M2M_CONTROL);
517*4882a593Smuzhiyun 	return 0;
518*4882a593Smuzhiyun }
519*4882a593Smuzhiyun 
m2m_hw_shutdown(struct ep93xx_dma_chan * edmac)520*4882a593Smuzhiyun static void m2m_hw_shutdown(struct ep93xx_dma_chan *edmac)
521*4882a593Smuzhiyun {
522*4882a593Smuzhiyun 	/* Just disable the channel */
523*4882a593Smuzhiyun 	writel(0, edmac->regs + M2M_CONTROL);
524*4882a593Smuzhiyun }
525*4882a593Smuzhiyun 
m2m_fill_desc(struct ep93xx_dma_chan * edmac)526*4882a593Smuzhiyun static void m2m_fill_desc(struct ep93xx_dma_chan *edmac)
527*4882a593Smuzhiyun {
528*4882a593Smuzhiyun 	struct ep93xx_dma_desc *desc;
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 	desc = ep93xx_dma_get_active(edmac);
531*4882a593Smuzhiyun 	if (!desc) {
532*4882a593Smuzhiyun 		dev_warn(chan2dev(edmac), "M2M: empty descriptor list\n");
533*4882a593Smuzhiyun 		return;
534*4882a593Smuzhiyun 	}
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun 	if (edmac->buffer == 0) {
537*4882a593Smuzhiyun 		writel(desc->src_addr, edmac->regs + M2M_SAR_BASE0);
538*4882a593Smuzhiyun 		writel(desc->dst_addr, edmac->regs + M2M_DAR_BASE0);
539*4882a593Smuzhiyun 		writel(desc->size, edmac->regs + M2M_BCR0);
540*4882a593Smuzhiyun 	} else {
541*4882a593Smuzhiyun 		writel(desc->src_addr, edmac->regs + M2M_SAR_BASE1);
542*4882a593Smuzhiyun 		writel(desc->dst_addr, edmac->regs + M2M_DAR_BASE1);
543*4882a593Smuzhiyun 		writel(desc->size, edmac->regs + M2M_BCR1);
544*4882a593Smuzhiyun 	}
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun 	edmac->buffer ^= 1;
547*4882a593Smuzhiyun }
548*4882a593Smuzhiyun 
m2m_hw_submit(struct ep93xx_dma_chan * edmac)549*4882a593Smuzhiyun static void m2m_hw_submit(struct ep93xx_dma_chan *edmac)
550*4882a593Smuzhiyun {
551*4882a593Smuzhiyun 	struct ep93xx_dma_data *data = edmac->chan.private;
552*4882a593Smuzhiyun 	u32 control = readl(edmac->regs + M2M_CONTROL);
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun 	/*
555*4882a593Smuzhiyun 	 * Since we allow clients to configure PW (peripheral width) we always
556*4882a593Smuzhiyun 	 * clear PW bits here and then set them according what is given in
557*4882a593Smuzhiyun 	 * the runtime configuration.
558*4882a593Smuzhiyun 	 */
559*4882a593Smuzhiyun 	control &= ~M2M_CONTROL_PW_MASK;
560*4882a593Smuzhiyun 	control |= edmac->runtime_ctrl;
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun 	m2m_fill_desc(edmac);
563*4882a593Smuzhiyun 	control |= M2M_CONTROL_DONEINT;
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 	if (ep93xx_dma_advance_active(edmac)) {
566*4882a593Smuzhiyun 		m2m_fill_desc(edmac);
567*4882a593Smuzhiyun 		control |= M2M_CONTROL_NFBINT;
568*4882a593Smuzhiyun 	}
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun 	/*
571*4882a593Smuzhiyun 	 * Now we can finally enable the channel. For M2M channel this must be
572*4882a593Smuzhiyun 	 * done _after_ the BCRx registers are programmed.
573*4882a593Smuzhiyun 	 */
574*4882a593Smuzhiyun 	control |= M2M_CONTROL_ENABLE;
575*4882a593Smuzhiyun 	writel(control, edmac->regs + M2M_CONTROL);
576*4882a593Smuzhiyun 
577*4882a593Smuzhiyun 	if (!data) {
578*4882a593Smuzhiyun 		/*
579*4882a593Smuzhiyun 		 * For memcpy channels the software trigger must be asserted
580*4882a593Smuzhiyun 		 * in order to start the memcpy operation.
581*4882a593Smuzhiyun 		 */
582*4882a593Smuzhiyun 		control |= M2M_CONTROL_START;
583*4882a593Smuzhiyun 		writel(control, edmac->regs + M2M_CONTROL);
584*4882a593Smuzhiyun 	}
585*4882a593Smuzhiyun }
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun /*
588*4882a593Smuzhiyun  * According to EP93xx User's Guide, we should receive DONE interrupt when all
589*4882a593Smuzhiyun  * M2M DMA controller transactions complete normally. This is not always the
590*4882a593Smuzhiyun  * case - sometimes EP93xx M2M DMA asserts DONE interrupt when the DMA channel
591*4882a593Smuzhiyun  * is still running (channel Buffer FSM in DMA_BUF_ON state, and channel
592*4882a593Smuzhiyun  * Control FSM in DMA_MEM_RD state, observed at least in IDE-DMA operation).
593*4882a593Smuzhiyun  * In effect, disabling the channel when only DONE bit is set could stop
594*4882a593Smuzhiyun  * currently running DMA transfer. To avoid this, we use Buffer FSM and
595*4882a593Smuzhiyun  * Control FSM to check current state of DMA channel.
596*4882a593Smuzhiyun  */
m2m_hw_interrupt(struct ep93xx_dma_chan * edmac)597*4882a593Smuzhiyun static int m2m_hw_interrupt(struct ep93xx_dma_chan *edmac)
598*4882a593Smuzhiyun {
599*4882a593Smuzhiyun 	u32 status = readl(edmac->regs + M2M_STATUS);
600*4882a593Smuzhiyun 	u32 ctl_fsm = status & M2M_STATUS_CTL_MASK;
601*4882a593Smuzhiyun 	u32 buf_fsm = status & M2M_STATUS_BUF_MASK;
602*4882a593Smuzhiyun 	bool done = status & M2M_STATUS_DONE;
603*4882a593Smuzhiyun 	bool last_done;
604*4882a593Smuzhiyun 	u32 control;
605*4882a593Smuzhiyun 	struct ep93xx_dma_desc *desc;
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun 	/* Accept only DONE and NFB interrupts */
608*4882a593Smuzhiyun 	if (!(readl(edmac->regs + M2M_INTERRUPT) & M2M_INTERRUPT_MASK))
609*4882a593Smuzhiyun 		return INTERRUPT_UNKNOWN;
610*4882a593Smuzhiyun 
611*4882a593Smuzhiyun 	if (done) {
612*4882a593Smuzhiyun 		/* Clear the DONE bit */
613*4882a593Smuzhiyun 		writel(0, edmac->regs + M2M_INTERRUPT);
614*4882a593Smuzhiyun 	}
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun 	/*
617*4882a593Smuzhiyun 	 * Check whether we are done with descriptors or not. This, together
618*4882a593Smuzhiyun 	 * with DMA channel state, determines action to take in interrupt.
619*4882a593Smuzhiyun 	 */
620*4882a593Smuzhiyun 	desc = ep93xx_dma_get_active(edmac);
621*4882a593Smuzhiyun 	last_done = !desc || desc->txd.cookie;
622*4882a593Smuzhiyun 
623*4882a593Smuzhiyun 	/*
624*4882a593Smuzhiyun 	 * Use M2M DMA Buffer FSM and Control FSM to check current state of
625*4882a593Smuzhiyun 	 * DMA channel. Using DONE and NFB bits from channel status register
626*4882a593Smuzhiyun 	 * or bits from channel interrupt register is not reliable.
627*4882a593Smuzhiyun 	 */
628*4882a593Smuzhiyun 	if (!last_done &&
629*4882a593Smuzhiyun 	    (buf_fsm == M2M_STATUS_BUF_NO ||
630*4882a593Smuzhiyun 	     buf_fsm == M2M_STATUS_BUF_ON)) {
631*4882a593Smuzhiyun 		/*
632*4882a593Smuzhiyun 		 * Two buffers are ready for update when Buffer FSM is in
633*4882a593Smuzhiyun 		 * DMA_NO_BUF state. Only one buffer can be prepared without
634*4882a593Smuzhiyun 		 * disabling the channel or polling the DONE bit.
635*4882a593Smuzhiyun 		 * To simplify things, always prepare only one buffer.
636*4882a593Smuzhiyun 		 */
637*4882a593Smuzhiyun 		if (ep93xx_dma_advance_active(edmac)) {
638*4882a593Smuzhiyun 			m2m_fill_desc(edmac);
639*4882a593Smuzhiyun 			if (done && !edmac->chan.private) {
640*4882a593Smuzhiyun 				/* Software trigger for memcpy channel */
641*4882a593Smuzhiyun 				control = readl(edmac->regs + M2M_CONTROL);
642*4882a593Smuzhiyun 				control |= M2M_CONTROL_START;
643*4882a593Smuzhiyun 				writel(control, edmac->regs + M2M_CONTROL);
644*4882a593Smuzhiyun 			}
645*4882a593Smuzhiyun 			return INTERRUPT_NEXT_BUFFER;
646*4882a593Smuzhiyun 		} else {
647*4882a593Smuzhiyun 			last_done = true;
648*4882a593Smuzhiyun 		}
649*4882a593Smuzhiyun 	}
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 	/*
652*4882a593Smuzhiyun 	 * Disable the channel only when Buffer FSM is in DMA_NO_BUF state
653*4882a593Smuzhiyun 	 * and Control FSM is in DMA_STALL state.
654*4882a593Smuzhiyun 	 */
655*4882a593Smuzhiyun 	if (last_done &&
656*4882a593Smuzhiyun 	    buf_fsm == M2M_STATUS_BUF_NO &&
657*4882a593Smuzhiyun 	    ctl_fsm == M2M_STATUS_CTL_STALL) {
658*4882a593Smuzhiyun 		/* Disable interrupts and the channel */
659*4882a593Smuzhiyun 		control = readl(edmac->regs + M2M_CONTROL);
660*4882a593Smuzhiyun 		control &= ~(M2M_CONTROL_DONEINT | M2M_CONTROL_NFBINT
661*4882a593Smuzhiyun 			    | M2M_CONTROL_ENABLE);
662*4882a593Smuzhiyun 		writel(control, edmac->regs + M2M_CONTROL);
663*4882a593Smuzhiyun 		return INTERRUPT_DONE;
664*4882a593Smuzhiyun 	}
665*4882a593Smuzhiyun 
666*4882a593Smuzhiyun 	/*
667*4882a593Smuzhiyun 	 * Nothing to do this time.
668*4882a593Smuzhiyun 	 */
669*4882a593Smuzhiyun 	return INTERRUPT_NEXT_BUFFER;
670*4882a593Smuzhiyun }
671*4882a593Smuzhiyun 
672*4882a593Smuzhiyun /*
673*4882a593Smuzhiyun  * DMA engine API implementation
674*4882a593Smuzhiyun  */
675*4882a593Smuzhiyun 
676*4882a593Smuzhiyun static struct ep93xx_dma_desc *
ep93xx_dma_desc_get(struct ep93xx_dma_chan * edmac)677*4882a593Smuzhiyun ep93xx_dma_desc_get(struct ep93xx_dma_chan *edmac)
678*4882a593Smuzhiyun {
679*4882a593Smuzhiyun 	struct ep93xx_dma_desc *desc, *_desc;
680*4882a593Smuzhiyun 	struct ep93xx_dma_desc *ret = NULL;
681*4882a593Smuzhiyun 	unsigned long flags;
682*4882a593Smuzhiyun 
683*4882a593Smuzhiyun 	spin_lock_irqsave(&edmac->lock, flags);
684*4882a593Smuzhiyun 	list_for_each_entry_safe(desc, _desc, &edmac->free_list, node) {
685*4882a593Smuzhiyun 		if (async_tx_test_ack(&desc->txd)) {
686*4882a593Smuzhiyun 			list_del_init(&desc->node);
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun 			/* Re-initialize the descriptor */
689*4882a593Smuzhiyun 			desc->src_addr = 0;
690*4882a593Smuzhiyun 			desc->dst_addr = 0;
691*4882a593Smuzhiyun 			desc->size = 0;
692*4882a593Smuzhiyun 			desc->complete = false;
693*4882a593Smuzhiyun 			desc->txd.cookie = 0;
694*4882a593Smuzhiyun 			desc->txd.callback = NULL;
695*4882a593Smuzhiyun 			desc->txd.callback_param = NULL;
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun 			ret = desc;
698*4882a593Smuzhiyun 			break;
699*4882a593Smuzhiyun 		}
700*4882a593Smuzhiyun 	}
701*4882a593Smuzhiyun 	spin_unlock_irqrestore(&edmac->lock, flags);
702*4882a593Smuzhiyun 	return ret;
703*4882a593Smuzhiyun }
704*4882a593Smuzhiyun 
ep93xx_dma_desc_put(struct ep93xx_dma_chan * edmac,struct ep93xx_dma_desc * desc)705*4882a593Smuzhiyun static void ep93xx_dma_desc_put(struct ep93xx_dma_chan *edmac,
706*4882a593Smuzhiyun 				struct ep93xx_dma_desc *desc)
707*4882a593Smuzhiyun {
708*4882a593Smuzhiyun 	if (desc) {
709*4882a593Smuzhiyun 		unsigned long flags;
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun 		spin_lock_irqsave(&edmac->lock, flags);
712*4882a593Smuzhiyun 		list_splice_init(&desc->tx_list, &edmac->free_list);
713*4882a593Smuzhiyun 		list_add(&desc->node, &edmac->free_list);
714*4882a593Smuzhiyun 		spin_unlock_irqrestore(&edmac->lock, flags);
715*4882a593Smuzhiyun 	}
716*4882a593Smuzhiyun }
717*4882a593Smuzhiyun 
718*4882a593Smuzhiyun /**
719*4882a593Smuzhiyun  * ep93xx_dma_advance_work - start processing the next pending transaction
720*4882a593Smuzhiyun  * @edmac: channel
721*4882a593Smuzhiyun  *
722*4882a593Smuzhiyun  * If we have pending transactions queued and we are currently idling, this
723*4882a593Smuzhiyun  * function takes the next queued transaction from the @edmac->queue and
724*4882a593Smuzhiyun  * pushes it to the hardware for execution.
725*4882a593Smuzhiyun  */
ep93xx_dma_advance_work(struct ep93xx_dma_chan * edmac)726*4882a593Smuzhiyun static void ep93xx_dma_advance_work(struct ep93xx_dma_chan *edmac)
727*4882a593Smuzhiyun {
728*4882a593Smuzhiyun 	struct ep93xx_dma_desc *new;
729*4882a593Smuzhiyun 	unsigned long flags;
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 	spin_lock_irqsave(&edmac->lock, flags);
732*4882a593Smuzhiyun 	if (!list_empty(&edmac->active) || list_empty(&edmac->queue)) {
733*4882a593Smuzhiyun 		spin_unlock_irqrestore(&edmac->lock, flags);
734*4882a593Smuzhiyun 		return;
735*4882a593Smuzhiyun 	}
736*4882a593Smuzhiyun 
737*4882a593Smuzhiyun 	/* Take the next descriptor from the pending queue */
738*4882a593Smuzhiyun 	new = list_first_entry(&edmac->queue, struct ep93xx_dma_desc, node);
739*4882a593Smuzhiyun 	list_del_init(&new->node);
740*4882a593Smuzhiyun 
741*4882a593Smuzhiyun 	ep93xx_dma_set_active(edmac, new);
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 	/* Push it to the hardware */
744*4882a593Smuzhiyun 	edmac->edma->hw_submit(edmac);
745*4882a593Smuzhiyun 	spin_unlock_irqrestore(&edmac->lock, flags);
746*4882a593Smuzhiyun }
747*4882a593Smuzhiyun 
ep93xx_dma_tasklet(struct tasklet_struct * t)748*4882a593Smuzhiyun static void ep93xx_dma_tasklet(struct tasklet_struct *t)
749*4882a593Smuzhiyun {
750*4882a593Smuzhiyun 	struct ep93xx_dma_chan *edmac = from_tasklet(edmac, t, tasklet);
751*4882a593Smuzhiyun 	struct ep93xx_dma_desc *desc, *d;
752*4882a593Smuzhiyun 	struct dmaengine_desc_callback cb;
753*4882a593Smuzhiyun 	LIST_HEAD(list);
754*4882a593Smuzhiyun 
755*4882a593Smuzhiyun 	memset(&cb, 0, sizeof(cb));
756*4882a593Smuzhiyun 	spin_lock_irq(&edmac->lock);
757*4882a593Smuzhiyun 	/*
758*4882a593Smuzhiyun 	 * If dma_terminate_all() was called before we get to run, the active
759*4882a593Smuzhiyun 	 * list has become empty. If that happens we aren't supposed to do
760*4882a593Smuzhiyun 	 * anything more than call ep93xx_dma_advance_work().
761*4882a593Smuzhiyun 	 */
762*4882a593Smuzhiyun 	desc = ep93xx_dma_get_active(edmac);
763*4882a593Smuzhiyun 	if (desc) {
764*4882a593Smuzhiyun 		if (desc->complete) {
765*4882a593Smuzhiyun 			/* mark descriptor complete for non cyclic case only */
766*4882a593Smuzhiyun 			if (!test_bit(EP93XX_DMA_IS_CYCLIC, &edmac->flags))
767*4882a593Smuzhiyun 				dma_cookie_complete(&desc->txd);
768*4882a593Smuzhiyun 			list_splice_init(&edmac->active, &list);
769*4882a593Smuzhiyun 		}
770*4882a593Smuzhiyun 		dmaengine_desc_get_callback(&desc->txd, &cb);
771*4882a593Smuzhiyun 	}
772*4882a593Smuzhiyun 	spin_unlock_irq(&edmac->lock);
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun 	/* Pick up the next descriptor from the queue */
775*4882a593Smuzhiyun 	ep93xx_dma_advance_work(edmac);
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun 	/* Now we can release all the chained descriptors */
778*4882a593Smuzhiyun 	list_for_each_entry_safe(desc, d, &list, node) {
779*4882a593Smuzhiyun 		dma_descriptor_unmap(&desc->txd);
780*4882a593Smuzhiyun 		ep93xx_dma_desc_put(edmac, desc);
781*4882a593Smuzhiyun 	}
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun 	dmaengine_desc_callback_invoke(&cb, NULL);
784*4882a593Smuzhiyun }
785*4882a593Smuzhiyun 
ep93xx_dma_interrupt(int irq,void * dev_id)786*4882a593Smuzhiyun static irqreturn_t ep93xx_dma_interrupt(int irq, void *dev_id)
787*4882a593Smuzhiyun {
788*4882a593Smuzhiyun 	struct ep93xx_dma_chan *edmac = dev_id;
789*4882a593Smuzhiyun 	struct ep93xx_dma_desc *desc;
790*4882a593Smuzhiyun 	irqreturn_t ret = IRQ_HANDLED;
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun 	spin_lock(&edmac->lock);
793*4882a593Smuzhiyun 
794*4882a593Smuzhiyun 	desc = ep93xx_dma_get_active(edmac);
795*4882a593Smuzhiyun 	if (!desc) {
796*4882a593Smuzhiyun 		dev_warn(chan2dev(edmac),
797*4882a593Smuzhiyun 			 "got interrupt while active list is empty\n");
798*4882a593Smuzhiyun 		spin_unlock(&edmac->lock);
799*4882a593Smuzhiyun 		return IRQ_NONE;
800*4882a593Smuzhiyun 	}
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 	switch (edmac->edma->hw_interrupt(edmac)) {
803*4882a593Smuzhiyun 	case INTERRUPT_DONE:
804*4882a593Smuzhiyun 		desc->complete = true;
805*4882a593Smuzhiyun 		tasklet_schedule(&edmac->tasklet);
806*4882a593Smuzhiyun 		break;
807*4882a593Smuzhiyun 
808*4882a593Smuzhiyun 	case INTERRUPT_NEXT_BUFFER:
809*4882a593Smuzhiyun 		if (test_bit(EP93XX_DMA_IS_CYCLIC, &edmac->flags))
810*4882a593Smuzhiyun 			tasklet_schedule(&edmac->tasklet);
811*4882a593Smuzhiyun 		break;
812*4882a593Smuzhiyun 
813*4882a593Smuzhiyun 	default:
814*4882a593Smuzhiyun 		dev_warn(chan2dev(edmac), "unknown interrupt!\n");
815*4882a593Smuzhiyun 		ret = IRQ_NONE;
816*4882a593Smuzhiyun 		break;
817*4882a593Smuzhiyun 	}
818*4882a593Smuzhiyun 
819*4882a593Smuzhiyun 	spin_unlock(&edmac->lock);
820*4882a593Smuzhiyun 	return ret;
821*4882a593Smuzhiyun }
822*4882a593Smuzhiyun 
823*4882a593Smuzhiyun /**
824*4882a593Smuzhiyun  * ep93xx_dma_tx_submit - set the prepared descriptor(s) to be executed
825*4882a593Smuzhiyun  * @tx: descriptor to be executed
826*4882a593Smuzhiyun  *
827*4882a593Smuzhiyun  * Function will execute given descriptor on the hardware or if the hardware
828*4882a593Smuzhiyun  * is busy, queue the descriptor to be executed later on. Returns cookie which
829*4882a593Smuzhiyun  * can be used to poll the status of the descriptor.
830*4882a593Smuzhiyun  */
ep93xx_dma_tx_submit(struct dma_async_tx_descriptor * tx)831*4882a593Smuzhiyun static dma_cookie_t ep93xx_dma_tx_submit(struct dma_async_tx_descriptor *tx)
832*4882a593Smuzhiyun {
833*4882a593Smuzhiyun 	struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(tx->chan);
834*4882a593Smuzhiyun 	struct ep93xx_dma_desc *desc;
835*4882a593Smuzhiyun 	dma_cookie_t cookie;
836*4882a593Smuzhiyun 	unsigned long flags;
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun 	spin_lock_irqsave(&edmac->lock, flags);
839*4882a593Smuzhiyun 	cookie = dma_cookie_assign(tx);
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 	desc = container_of(tx, struct ep93xx_dma_desc, txd);
842*4882a593Smuzhiyun 
843*4882a593Smuzhiyun 	/*
844*4882a593Smuzhiyun 	 * If nothing is currently prosessed, we push this descriptor
845*4882a593Smuzhiyun 	 * directly to the hardware. Otherwise we put the descriptor
846*4882a593Smuzhiyun 	 * to the pending queue.
847*4882a593Smuzhiyun 	 */
848*4882a593Smuzhiyun 	if (list_empty(&edmac->active)) {
849*4882a593Smuzhiyun 		ep93xx_dma_set_active(edmac, desc);
850*4882a593Smuzhiyun 		edmac->edma->hw_submit(edmac);
851*4882a593Smuzhiyun 	} else {
852*4882a593Smuzhiyun 		list_add_tail(&desc->node, &edmac->queue);
853*4882a593Smuzhiyun 	}
854*4882a593Smuzhiyun 
855*4882a593Smuzhiyun 	spin_unlock_irqrestore(&edmac->lock, flags);
856*4882a593Smuzhiyun 	return cookie;
857*4882a593Smuzhiyun }
858*4882a593Smuzhiyun 
859*4882a593Smuzhiyun /**
860*4882a593Smuzhiyun  * ep93xx_dma_alloc_chan_resources - allocate resources for the channel
861*4882a593Smuzhiyun  * @chan: channel to allocate resources
862*4882a593Smuzhiyun  *
863*4882a593Smuzhiyun  * Function allocates necessary resources for the given DMA channel and
864*4882a593Smuzhiyun  * returns number of allocated descriptors for the channel. Negative errno
865*4882a593Smuzhiyun  * is returned in case of failure.
866*4882a593Smuzhiyun  */
ep93xx_dma_alloc_chan_resources(struct dma_chan * chan)867*4882a593Smuzhiyun static int ep93xx_dma_alloc_chan_resources(struct dma_chan *chan)
868*4882a593Smuzhiyun {
869*4882a593Smuzhiyun 	struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan);
870*4882a593Smuzhiyun 	struct ep93xx_dma_data *data = chan->private;
871*4882a593Smuzhiyun 	const char *name = dma_chan_name(chan);
872*4882a593Smuzhiyun 	int ret, i;
873*4882a593Smuzhiyun 
874*4882a593Smuzhiyun 	/* Sanity check the channel parameters */
875*4882a593Smuzhiyun 	if (!edmac->edma->m2m) {
876*4882a593Smuzhiyun 		if (!data)
877*4882a593Smuzhiyun 			return -EINVAL;
878*4882a593Smuzhiyun 		if (data->port < EP93XX_DMA_I2S1 ||
879*4882a593Smuzhiyun 		    data->port > EP93XX_DMA_IRDA)
880*4882a593Smuzhiyun 			return -EINVAL;
881*4882a593Smuzhiyun 		if (data->direction != ep93xx_dma_chan_direction(chan))
882*4882a593Smuzhiyun 			return -EINVAL;
883*4882a593Smuzhiyun 	} else {
884*4882a593Smuzhiyun 		if (data) {
885*4882a593Smuzhiyun 			switch (data->port) {
886*4882a593Smuzhiyun 			case EP93XX_DMA_SSP:
887*4882a593Smuzhiyun 			case EP93XX_DMA_IDE:
888*4882a593Smuzhiyun 				if (!is_slave_direction(data->direction))
889*4882a593Smuzhiyun 					return -EINVAL;
890*4882a593Smuzhiyun 				break;
891*4882a593Smuzhiyun 			default:
892*4882a593Smuzhiyun 				return -EINVAL;
893*4882a593Smuzhiyun 			}
894*4882a593Smuzhiyun 		}
895*4882a593Smuzhiyun 	}
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun 	if (data && data->name)
898*4882a593Smuzhiyun 		name = data->name;
899*4882a593Smuzhiyun 
900*4882a593Smuzhiyun 	ret = clk_enable(edmac->clk);
901*4882a593Smuzhiyun 	if (ret)
902*4882a593Smuzhiyun 		return ret;
903*4882a593Smuzhiyun 
904*4882a593Smuzhiyun 	ret = request_irq(edmac->irq, ep93xx_dma_interrupt, 0, name, edmac);
905*4882a593Smuzhiyun 	if (ret)
906*4882a593Smuzhiyun 		goto fail_clk_disable;
907*4882a593Smuzhiyun 
908*4882a593Smuzhiyun 	spin_lock_irq(&edmac->lock);
909*4882a593Smuzhiyun 	dma_cookie_init(&edmac->chan);
910*4882a593Smuzhiyun 	ret = edmac->edma->hw_setup(edmac);
911*4882a593Smuzhiyun 	spin_unlock_irq(&edmac->lock);
912*4882a593Smuzhiyun 
913*4882a593Smuzhiyun 	if (ret)
914*4882a593Smuzhiyun 		goto fail_free_irq;
915*4882a593Smuzhiyun 
916*4882a593Smuzhiyun 	for (i = 0; i < DMA_MAX_CHAN_DESCRIPTORS; i++) {
917*4882a593Smuzhiyun 		struct ep93xx_dma_desc *desc;
918*4882a593Smuzhiyun 
919*4882a593Smuzhiyun 		desc = kzalloc(sizeof(*desc), GFP_KERNEL);
920*4882a593Smuzhiyun 		if (!desc) {
921*4882a593Smuzhiyun 			dev_warn(chan2dev(edmac), "not enough descriptors\n");
922*4882a593Smuzhiyun 			break;
923*4882a593Smuzhiyun 		}
924*4882a593Smuzhiyun 
925*4882a593Smuzhiyun 		INIT_LIST_HEAD(&desc->tx_list);
926*4882a593Smuzhiyun 
927*4882a593Smuzhiyun 		dma_async_tx_descriptor_init(&desc->txd, chan);
928*4882a593Smuzhiyun 		desc->txd.flags = DMA_CTRL_ACK;
929*4882a593Smuzhiyun 		desc->txd.tx_submit = ep93xx_dma_tx_submit;
930*4882a593Smuzhiyun 
931*4882a593Smuzhiyun 		ep93xx_dma_desc_put(edmac, desc);
932*4882a593Smuzhiyun 	}
933*4882a593Smuzhiyun 
934*4882a593Smuzhiyun 	return i;
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun fail_free_irq:
937*4882a593Smuzhiyun 	free_irq(edmac->irq, edmac);
938*4882a593Smuzhiyun fail_clk_disable:
939*4882a593Smuzhiyun 	clk_disable(edmac->clk);
940*4882a593Smuzhiyun 
941*4882a593Smuzhiyun 	return ret;
942*4882a593Smuzhiyun }
943*4882a593Smuzhiyun 
944*4882a593Smuzhiyun /**
945*4882a593Smuzhiyun  * ep93xx_dma_free_chan_resources - release resources for the channel
946*4882a593Smuzhiyun  * @chan: channel
947*4882a593Smuzhiyun  *
948*4882a593Smuzhiyun  * Function releases all the resources allocated for the given channel.
949*4882a593Smuzhiyun  * The channel must be idle when this is called.
950*4882a593Smuzhiyun  */
ep93xx_dma_free_chan_resources(struct dma_chan * chan)951*4882a593Smuzhiyun static void ep93xx_dma_free_chan_resources(struct dma_chan *chan)
952*4882a593Smuzhiyun {
953*4882a593Smuzhiyun 	struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan);
954*4882a593Smuzhiyun 	struct ep93xx_dma_desc *desc, *d;
955*4882a593Smuzhiyun 	unsigned long flags;
956*4882a593Smuzhiyun 	LIST_HEAD(list);
957*4882a593Smuzhiyun 
958*4882a593Smuzhiyun 	BUG_ON(!list_empty(&edmac->active));
959*4882a593Smuzhiyun 	BUG_ON(!list_empty(&edmac->queue));
960*4882a593Smuzhiyun 
961*4882a593Smuzhiyun 	spin_lock_irqsave(&edmac->lock, flags);
962*4882a593Smuzhiyun 	edmac->edma->hw_shutdown(edmac);
963*4882a593Smuzhiyun 	edmac->runtime_addr = 0;
964*4882a593Smuzhiyun 	edmac->runtime_ctrl = 0;
965*4882a593Smuzhiyun 	edmac->buffer = 0;
966*4882a593Smuzhiyun 	list_splice_init(&edmac->free_list, &list);
967*4882a593Smuzhiyun 	spin_unlock_irqrestore(&edmac->lock, flags);
968*4882a593Smuzhiyun 
969*4882a593Smuzhiyun 	list_for_each_entry_safe(desc, d, &list, node)
970*4882a593Smuzhiyun 		kfree(desc);
971*4882a593Smuzhiyun 
972*4882a593Smuzhiyun 	clk_disable(edmac->clk);
973*4882a593Smuzhiyun 	free_irq(edmac->irq, edmac);
974*4882a593Smuzhiyun }
975*4882a593Smuzhiyun 
976*4882a593Smuzhiyun /**
977*4882a593Smuzhiyun  * ep93xx_dma_prep_dma_memcpy - prepare a memcpy DMA operation
978*4882a593Smuzhiyun  * @chan: channel
979*4882a593Smuzhiyun  * @dest: destination bus address
980*4882a593Smuzhiyun  * @src: source bus address
981*4882a593Smuzhiyun  * @len: size of the transaction
982*4882a593Smuzhiyun  * @flags: flags for the descriptor
983*4882a593Smuzhiyun  *
984*4882a593Smuzhiyun  * Returns a valid DMA descriptor or %NULL in case of failure.
985*4882a593Smuzhiyun  */
986*4882a593Smuzhiyun static struct dma_async_tx_descriptor *
ep93xx_dma_prep_dma_memcpy(struct dma_chan * chan,dma_addr_t dest,dma_addr_t src,size_t len,unsigned long flags)987*4882a593Smuzhiyun ep93xx_dma_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest,
988*4882a593Smuzhiyun 			   dma_addr_t src, size_t len, unsigned long flags)
989*4882a593Smuzhiyun {
990*4882a593Smuzhiyun 	struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan);
991*4882a593Smuzhiyun 	struct ep93xx_dma_desc *desc, *first;
992*4882a593Smuzhiyun 	size_t bytes, offset;
993*4882a593Smuzhiyun 
994*4882a593Smuzhiyun 	first = NULL;
995*4882a593Smuzhiyun 	for (offset = 0; offset < len; offset += bytes) {
996*4882a593Smuzhiyun 		desc = ep93xx_dma_desc_get(edmac);
997*4882a593Smuzhiyun 		if (!desc) {
998*4882a593Smuzhiyun 			dev_warn(chan2dev(edmac), "couldn't get descriptor\n");
999*4882a593Smuzhiyun 			goto fail;
1000*4882a593Smuzhiyun 		}
1001*4882a593Smuzhiyun 
1002*4882a593Smuzhiyun 		bytes = min_t(size_t, len - offset, DMA_MAX_CHAN_BYTES);
1003*4882a593Smuzhiyun 
1004*4882a593Smuzhiyun 		desc->src_addr = src + offset;
1005*4882a593Smuzhiyun 		desc->dst_addr = dest + offset;
1006*4882a593Smuzhiyun 		desc->size = bytes;
1007*4882a593Smuzhiyun 
1008*4882a593Smuzhiyun 		if (!first)
1009*4882a593Smuzhiyun 			first = desc;
1010*4882a593Smuzhiyun 		else
1011*4882a593Smuzhiyun 			list_add_tail(&desc->node, &first->tx_list);
1012*4882a593Smuzhiyun 	}
1013*4882a593Smuzhiyun 
1014*4882a593Smuzhiyun 	first->txd.cookie = -EBUSY;
1015*4882a593Smuzhiyun 	first->txd.flags = flags;
1016*4882a593Smuzhiyun 
1017*4882a593Smuzhiyun 	return &first->txd;
1018*4882a593Smuzhiyun fail:
1019*4882a593Smuzhiyun 	ep93xx_dma_desc_put(edmac, first);
1020*4882a593Smuzhiyun 	return NULL;
1021*4882a593Smuzhiyun }
1022*4882a593Smuzhiyun 
1023*4882a593Smuzhiyun /**
1024*4882a593Smuzhiyun  * ep93xx_dma_prep_slave_sg - prepare a slave DMA operation
1025*4882a593Smuzhiyun  * @chan: channel
1026*4882a593Smuzhiyun  * @sgl: list of buffers to transfer
1027*4882a593Smuzhiyun  * @sg_len: number of entries in @sgl
1028*4882a593Smuzhiyun  * @dir: direction of tha DMA transfer
1029*4882a593Smuzhiyun  * @flags: flags for the descriptor
1030*4882a593Smuzhiyun  * @context: operation context (ignored)
1031*4882a593Smuzhiyun  *
1032*4882a593Smuzhiyun  * Returns a valid DMA descriptor or %NULL in case of failure.
1033*4882a593Smuzhiyun  */
1034*4882a593Smuzhiyun static struct dma_async_tx_descriptor *
ep93xx_dma_prep_slave_sg(struct dma_chan * chan,struct scatterlist * sgl,unsigned int sg_len,enum dma_transfer_direction dir,unsigned long flags,void * context)1035*4882a593Smuzhiyun ep93xx_dma_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
1036*4882a593Smuzhiyun 			 unsigned int sg_len, enum dma_transfer_direction dir,
1037*4882a593Smuzhiyun 			 unsigned long flags, void *context)
1038*4882a593Smuzhiyun {
1039*4882a593Smuzhiyun 	struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan);
1040*4882a593Smuzhiyun 	struct ep93xx_dma_desc *desc, *first;
1041*4882a593Smuzhiyun 	struct scatterlist *sg;
1042*4882a593Smuzhiyun 	int i;
1043*4882a593Smuzhiyun 
1044*4882a593Smuzhiyun 	if (!edmac->edma->m2m && dir != ep93xx_dma_chan_direction(chan)) {
1045*4882a593Smuzhiyun 		dev_warn(chan2dev(edmac),
1046*4882a593Smuzhiyun 			 "channel was configured with different direction\n");
1047*4882a593Smuzhiyun 		return NULL;
1048*4882a593Smuzhiyun 	}
1049*4882a593Smuzhiyun 
1050*4882a593Smuzhiyun 	if (test_bit(EP93XX_DMA_IS_CYCLIC, &edmac->flags)) {
1051*4882a593Smuzhiyun 		dev_warn(chan2dev(edmac),
1052*4882a593Smuzhiyun 			 "channel is already used for cyclic transfers\n");
1053*4882a593Smuzhiyun 		return NULL;
1054*4882a593Smuzhiyun 	}
1055*4882a593Smuzhiyun 
1056*4882a593Smuzhiyun 	ep93xx_dma_slave_config_write(chan, dir, &edmac->slave_config);
1057*4882a593Smuzhiyun 
1058*4882a593Smuzhiyun 	first = NULL;
1059*4882a593Smuzhiyun 	for_each_sg(sgl, sg, sg_len, i) {
1060*4882a593Smuzhiyun 		size_t len = sg_dma_len(sg);
1061*4882a593Smuzhiyun 
1062*4882a593Smuzhiyun 		if (len > DMA_MAX_CHAN_BYTES) {
1063*4882a593Smuzhiyun 			dev_warn(chan2dev(edmac), "too big transfer size %zu\n",
1064*4882a593Smuzhiyun 				 len);
1065*4882a593Smuzhiyun 			goto fail;
1066*4882a593Smuzhiyun 		}
1067*4882a593Smuzhiyun 
1068*4882a593Smuzhiyun 		desc = ep93xx_dma_desc_get(edmac);
1069*4882a593Smuzhiyun 		if (!desc) {
1070*4882a593Smuzhiyun 			dev_warn(chan2dev(edmac), "couldn't get descriptor\n");
1071*4882a593Smuzhiyun 			goto fail;
1072*4882a593Smuzhiyun 		}
1073*4882a593Smuzhiyun 
1074*4882a593Smuzhiyun 		if (dir == DMA_MEM_TO_DEV) {
1075*4882a593Smuzhiyun 			desc->src_addr = sg_dma_address(sg);
1076*4882a593Smuzhiyun 			desc->dst_addr = edmac->runtime_addr;
1077*4882a593Smuzhiyun 		} else {
1078*4882a593Smuzhiyun 			desc->src_addr = edmac->runtime_addr;
1079*4882a593Smuzhiyun 			desc->dst_addr = sg_dma_address(sg);
1080*4882a593Smuzhiyun 		}
1081*4882a593Smuzhiyun 		desc->size = len;
1082*4882a593Smuzhiyun 
1083*4882a593Smuzhiyun 		if (!first)
1084*4882a593Smuzhiyun 			first = desc;
1085*4882a593Smuzhiyun 		else
1086*4882a593Smuzhiyun 			list_add_tail(&desc->node, &first->tx_list);
1087*4882a593Smuzhiyun 	}
1088*4882a593Smuzhiyun 
1089*4882a593Smuzhiyun 	first->txd.cookie = -EBUSY;
1090*4882a593Smuzhiyun 	first->txd.flags = flags;
1091*4882a593Smuzhiyun 
1092*4882a593Smuzhiyun 	return &first->txd;
1093*4882a593Smuzhiyun 
1094*4882a593Smuzhiyun fail:
1095*4882a593Smuzhiyun 	ep93xx_dma_desc_put(edmac, first);
1096*4882a593Smuzhiyun 	return NULL;
1097*4882a593Smuzhiyun }
1098*4882a593Smuzhiyun 
1099*4882a593Smuzhiyun /**
1100*4882a593Smuzhiyun  * ep93xx_dma_prep_dma_cyclic - prepare a cyclic DMA operation
1101*4882a593Smuzhiyun  * @chan: channel
1102*4882a593Smuzhiyun  * @dma_addr: DMA mapped address of the buffer
1103*4882a593Smuzhiyun  * @buf_len: length of the buffer (in bytes)
1104*4882a593Smuzhiyun  * @period_len: length of a single period
1105*4882a593Smuzhiyun  * @dir: direction of the operation
1106*4882a593Smuzhiyun  * @flags: tx descriptor status flags
1107*4882a593Smuzhiyun  *
1108*4882a593Smuzhiyun  * Prepares a descriptor for cyclic DMA operation. This means that once the
1109*4882a593Smuzhiyun  * descriptor is submitted, we will be submitting in a @period_len sized
1110*4882a593Smuzhiyun  * buffers and calling callback once the period has been elapsed. Transfer
1111*4882a593Smuzhiyun  * terminates only when client calls dmaengine_terminate_all() for this
1112*4882a593Smuzhiyun  * channel.
1113*4882a593Smuzhiyun  *
1114*4882a593Smuzhiyun  * Returns a valid DMA descriptor or %NULL in case of failure.
1115*4882a593Smuzhiyun  */
1116*4882a593Smuzhiyun static struct dma_async_tx_descriptor *
ep93xx_dma_prep_dma_cyclic(struct dma_chan * chan,dma_addr_t dma_addr,size_t buf_len,size_t period_len,enum dma_transfer_direction dir,unsigned long flags)1117*4882a593Smuzhiyun ep93xx_dma_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr,
1118*4882a593Smuzhiyun 			   size_t buf_len, size_t period_len,
1119*4882a593Smuzhiyun 			   enum dma_transfer_direction dir, unsigned long flags)
1120*4882a593Smuzhiyun {
1121*4882a593Smuzhiyun 	struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan);
1122*4882a593Smuzhiyun 	struct ep93xx_dma_desc *desc, *first;
1123*4882a593Smuzhiyun 	size_t offset = 0;
1124*4882a593Smuzhiyun 
1125*4882a593Smuzhiyun 	if (!edmac->edma->m2m && dir != ep93xx_dma_chan_direction(chan)) {
1126*4882a593Smuzhiyun 		dev_warn(chan2dev(edmac),
1127*4882a593Smuzhiyun 			 "channel was configured with different direction\n");
1128*4882a593Smuzhiyun 		return NULL;
1129*4882a593Smuzhiyun 	}
1130*4882a593Smuzhiyun 
1131*4882a593Smuzhiyun 	if (test_and_set_bit(EP93XX_DMA_IS_CYCLIC, &edmac->flags)) {
1132*4882a593Smuzhiyun 		dev_warn(chan2dev(edmac),
1133*4882a593Smuzhiyun 			 "channel is already used for cyclic transfers\n");
1134*4882a593Smuzhiyun 		return NULL;
1135*4882a593Smuzhiyun 	}
1136*4882a593Smuzhiyun 
1137*4882a593Smuzhiyun 	if (period_len > DMA_MAX_CHAN_BYTES) {
1138*4882a593Smuzhiyun 		dev_warn(chan2dev(edmac), "too big period length %zu\n",
1139*4882a593Smuzhiyun 			 period_len);
1140*4882a593Smuzhiyun 		return NULL;
1141*4882a593Smuzhiyun 	}
1142*4882a593Smuzhiyun 
1143*4882a593Smuzhiyun 	ep93xx_dma_slave_config_write(chan, dir, &edmac->slave_config);
1144*4882a593Smuzhiyun 
1145*4882a593Smuzhiyun 	/* Split the buffer into period size chunks */
1146*4882a593Smuzhiyun 	first = NULL;
1147*4882a593Smuzhiyun 	for (offset = 0; offset < buf_len; offset += period_len) {
1148*4882a593Smuzhiyun 		desc = ep93xx_dma_desc_get(edmac);
1149*4882a593Smuzhiyun 		if (!desc) {
1150*4882a593Smuzhiyun 			dev_warn(chan2dev(edmac), "couldn't get descriptor\n");
1151*4882a593Smuzhiyun 			goto fail;
1152*4882a593Smuzhiyun 		}
1153*4882a593Smuzhiyun 
1154*4882a593Smuzhiyun 		if (dir == DMA_MEM_TO_DEV) {
1155*4882a593Smuzhiyun 			desc->src_addr = dma_addr + offset;
1156*4882a593Smuzhiyun 			desc->dst_addr = edmac->runtime_addr;
1157*4882a593Smuzhiyun 		} else {
1158*4882a593Smuzhiyun 			desc->src_addr = edmac->runtime_addr;
1159*4882a593Smuzhiyun 			desc->dst_addr = dma_addr + offset;
1160*4882a593Smuzhiyun 		}
1161*4882a593Smuzhiyun 
1162*4882a593Smuzhiyun 		desc->size = period_len;
1163*4882a593Smuzhiyun 
1164*4882a593Smuzhiyun 		if (!first)
1165*4882a593Smuzhiyun 			first = desc;
1166*4882a593Smuzhiyun 		else
1167*4882a593Smuzhiyun 			list_add_tail(&desc->node, &first->tx_list);
1168*4882a593Smuzhiyun 	}
1169*4882a593Smuzhiyun 
1170*4882a593Smuzhiyun 	first->txd.cookie = -EBUSY;
1171*4882a593Smuzhiyun 
1172*4882a593Smuzhiyun 	return &first->txd;
1173*4882a593Smuzhiyun 
1174*4882a593Smuzhiyun fail:
1175*4882a593Smuzhiyun 	ep93xx_dma_desc_put(edmac, first);
1176*4882a593Smuzhiyun 	return NULL;
1177*4882a593Smuzhiyun }
1178*4882a593Smuzhiyun 
1179*4882a593Smuzhiyun /**
1180*4882a593Smuzhiyun  * ep93xx_dma_synchronize - Synchronizes the termination of transfers to the
1181*4882a593Smuzhiyun  * current context.
1182*4882a593Smuzhiyun  * @chan: channel
1183*4882a593Smuzhiyun  *
1184*4882a593Smuzhiyun  * Synchronizes the DMA channel termination to the current context. When this
1185*4882a593Smuzhiyun  * function returns it is guaranteed that all transfers for previously issued
1186*4882a593Smuzhiyun  * descriptors have stopped and and it is safe to free the memory associated
1187*4882a593Smuzhiyun  * with them. Furthermore it is guaranteed that all complete callback functions
1188*4882a593Smuzhiyun  * for a previously submitted descriptor have finished running and it is safe to
1189*4882a593Smuzhiyun  * free resources accessed from within the complete callbacks.
1190*4882a593Smuzhiyun  */
ep93xx_dma_synchronize(struct dma_chan * chan)1191*4882a593Smuzhiyun static void ep93xx_dma_synchronize(struct dma_chan *chan)
1192*4882a593Smuzhiyun {
1193*4882a593Smuzhiyun 	struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan);
1194*4882a593Smuzhiyun 
1195*4882a593Smuzhiyun 	if (edmac->edma->hw_synchronize)
1196*4882a593Smuzhiyun 		edmac->edma->hw_synchronize(edmac);
1197*4882a593Smuzhiyun }
1198*4882a593Smuzhiyun 
1199*4882a593Smuzhiyun /**
1200*4882a593Smuzhiyun  * ep93xx_dma_terminate_all - terminate all transactions
1201*4882a593Smuzhiyun  * @chan: channel
1202*4882a593Smuzhiyun  *
1203*4882a593Smuzhiyun  * Stops all DMA transactions. All descriptors are put back to the
1204*4882a593Smuzhiyun  * @edmac->free_list and callbacks are _not_ called.
1205*4882a593Smuzhiyun  */
ep93xx_dma_terminate_all(struct dma_chan * chan)1206*4882a593Smuzhiyun static int ep93xx_dma_terminate_all(struct dma_chan *chan)
1207*4882a593Smuzhiyun {
1208*4882a593Smuzhiyun 	struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan);
1209*4882a593Smuzhiyun 	struct ep93xx_dma_desc *desc, *_d;
1210*4882a593Smuzhiyun 	unsigned long flags;
1211*4882a593Smuzhiyun 	LIST_HEAD(list);
1212*4882a593Smuzhiyun 
1213*4882a593Smuzhiyun 	spin_lock_irqsave(&edmac->lock, flags);
1214*4882a593Smuzhiyun 	/* First we disable and flush the DMA channel */
1215*4882a593Smuzhiyun 	edmac->edma->hw_shutdown(edmac);
1216*4882a593Smuzhiyun 	clear_bit(EP93XX_DMA_IS_CYCLIC, &edmac->flags);
1217*4882a593Smuzhiyun 	list_splice_init(&edmac->active, &list);
1218*4882a593Smuzhiyun 	list_splice_init(&edmac->queue, &list);
1219*4882a593Smuzhiyun 	/*
1220*4882a593Smuzhiyun 	 * We then re-enable the channel. This way we can continue submitting
1221*4882a593Smuzhiyun 	 * the descriptors by just calling ->hw_submit() again.
1222*4882a593Smuzhiyun 	 */
1223*4882a593Smuzhiyun 	edmac->edma->hw_setup(edmac);
1224*4882a593Smuzhiyun 	spin_unlock_irqrestore(&edmac->lock, flags);
1225*4882a593Smuzhiyun 
1226*4882a593Smuzhiyun 	list_for_each_entry_safe(desc, _d, &list, node)
1227*4882a593Smuzhiyun 		ep93xx_dma_desc_put(edmac, desc);
1228*4882a593Smuzhiyun 
1229*4882a593Smuzhiyun 	return 0;
1230*4882a593Smuzhiyun }
1231*4882a593Smuzhiyun 
ep93xx_dma_slave_config(struct dma_chan * chan,struct dma_slave_config * config)1232*4882a593Smuzhiyun static int ep93xx_dma_slave_config(struct dma_chan *chan,
1233*4882a593Smuzhiyun 				   struct dma_slave_config *config)
1234*4882a593Smuzhiyun {
1235*4882a593Smuzhiyun 	struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan);
1236*4882a593Smuzhiyun 
1237*4882a593Smuzhiyun 	memcpy(&edmac->slave_config, config, sizeof(*config));
1238*4882a593Smuzhiyun 
1239*4882a593Smuzhiyun 	return 0;
1240*4882a593Smuzhiyun }
1241*4882a593Smuzhiyun 
ep93xx_dma_slave_config_write(struct dma_chan * chan,enum dma_transfer_direction dir,struct dma_slave_config * config)1242*4882a593Smuzhiyun static int ep93xx_dma_slave_config_write(struct dma_chan *chan,
1243*4882a593Smuzhiyun 					 enum dma_transfer_direction dir,
1244*4882a593Smuzhiyun 					 struct dma_slave_config *config)
1245*4882a593Smuzhiyun {
1246*4882a593Smuzhiyun 	struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan);
1247*4882a593Smuzhiyun 	enum dma_slave_buswidth width;
1248*4882a593Smuzhiyun 	unsigned long flags;
1249*4882a593Smuzhiyun 	u32 addr, ctrl;
1250*4882a593Smuzhiyun 
1251*4882a593Smuzhiyun 	if (!edmac->edma->m2m)
1252*4882a593Smuzhiyun 		return -EINVAL;
1253*4882a593Smuzhiyun 
1254*4882a593Smuzhiyun 	switch (dir) {
1255*4882a593Smuzhiyun 	case DMA_DEV_TO_MEM:
1256*4882a593Smuzhiyun 		width = config->src_addr_width;
1257*4882a593Smuzhiyun 		addr = config->src_addr;
1258*4882a593Smuzhiyun 		break;
1259*4882a593Smuzhiyun 
1260*4882a593Smuzhiyun 	case DMA_MEM_TO_DEV:
1261*4882a593Smuzhiyun 		width = config->dst_addr_width;
1262*4882a593Smuzhiyun 		addr = config->dst_addr;
1263*4882a593Smuzhiyun 		break;
1264*4882a593Smuzhiyun 
1265*4882a593Smuzhiyun 	default:
1266*4882a593Smuzhiyun 		return -EINVAL;
1267*4882a593Smuzhiyun 	}
1268*4882a593Smuzhiyun 
1269*4882a593Smuzhiyun 	switch (width) {
1270*4882a593Smuzhiyun 	case DMA_SLAVE_BUSWIDTH_1_BYTE:
1271*4882a593Smuzhiyun 		ctrl = 0;
1272*4882a593Smuzhiyun 		break;
1273*4882a593Smuzhiyun 	case DMA_SLAVE_BUSWIDTH_2_BYTES:
1274*4882a593Smuzhiyun 		ctrl = M2M_CONTROL_PW_16;
1275*4882a593Smuzhiyun 		break;
1276*4882a593Smuzhiyun 	case DMA_SLAVE_BUSWIDTH_4_BYTES:
1277*4882a593Smuzhiyun 		ctrl = M2M_CONTROL_PW_32;
1278*4882a593Smuzhiyun 		break;
1279*4882a593Smuzhiyun 	default:
1280*4882a593Smuzhiyun 		return -EINVAL;
1281*4882a593Smuzhiyun 	}
1282*4882a593Smuzhiyun 
1283*4882a593Smuzhiyun 	spin_lock_irqsave(&edmac->lock, flags);
1284*4882a593Smuzhiyun 	edmac->runtime_addr = addr;
1285*4882a593Smuzhiyun 	edmac->runtime_ctrl = ctrl;
1286*4882a593Smuzhiyun 	spin_unlock_irqrestore(&edmac->lock, flags);
1287*4882a593Smuzhiyun 
1288*4882a593Smuzhiyun 	return 0;
1289*4882a593Smuzhiyun }
1290*4882a593Smuzhiyun 
1291*4882a593Smuzhiyun /**
1292*4882a593Smuzhiyun  * ep93xx_dma_tx_status - check if a transaction is completed
1293*4882a593Smuzhiyun  * @chan: channel
1294*4882a593Smuzhiyun  * @cookie: transaction specific cookie
1295*4882a593Smuzhiyun  * @state: state of the transaction is stored here if given
1296*4882a593Smuzhiyun  *
1297*4882a593Smuzhiyun  * This function can be used to query state of a given transaction.
1298*4882a593Smuzhiyun  */
ep93xx_dma_tx_status(struct dma_chan * chan,dma_cookie_t cookie,struct dma_tx_state * state)1299*4882a593Smuzhiyun static enum dma_status ep93xx_dma_tx_status(struct dma_chan *chan,
1300*4882a593Smuzhiyun 					    dma_cookie_t cookie,
1301*4882a593Smuzhiyun 					    struct dma_tx_state *state)
1302*4882a593Smuzhiyun {
1303*4882a593Smuzhiyun 	return dma_cookie_status(chan, cookie, state);
1304*4882a593Smuzhiyun }
1305*4882a593Smuzhiyun 
1306*4882a593Smuzhiyun /**
1307*4882a593Smuzhiyun  * ep93xx_dma_issue_pending - push pending transactions to the hardware
1308*4882a593Smuzhiyun  * @chan: channel
1309*4882a593Smuzhiyun  *
1310*4882a593Smuzhiyun  * When this function is called, all pending transactions are pushed to the
1311*4882a593Smuzhiyun  * hardware and executed.
1312*4882a593Smuzhiyun  */
ep93xx_dma_issue_pending(struct dma_chan * chan)1313*4882a593Smuzhiyun static void ep93xx_dma_issue_pending(struct dma_chan *chan)
1314*4882a593Smuzhiyun {
1315*4882a593Smuzhiyun 	ep93xx_dma_advance_work(to_ep93xx_dma_chan(chan));
1316*4882a593Smuzhiyun }
1317*4882a593Smuzhiyun 
ep93xx_dma_probe(struct platform_device * pdev)1318*4882a593Smuzhiyun static int __init ep93xx_dma_probe(struct platform_device *pdev)
1319*4882a593Smuzhiyun {
1320*4882a593Smuzhiyun 	struct ep93xx_dma_platform_data *pdata = dev_get_platdata(&pdev->dev);
1321*4882a593Smuzhiyun 	struct ep93xx_dma_engine *edma;
1322*4882a593Smuzhiyun 	struct dma_device *dma_dev;
1323*4882a593Smuzhiyun 	size_t edma_size;
1324*4882a593Smuzhiyun 	int ret, i;
1325*4882a593Smuzhiyun 
1326*4882a593Smuzhiyun 	edma_size = pdata->num_channels * sizeof(struct ep93xx_dma_chan);
1327*4882a593Smuzhiyun 	edma = kzalloc(sizeof(*edma) + edma_size, GFP_KERNEL);
1328*4882a593Smuzhiyun 	if (!edma)
1329*4882a593Smuzhiyun 		return -ENOMEM;
1330*4882a593Smuzhiyun 
1331*4882a593Smuzhiyun 	dma_dev = &edma->dma_dev;
1332*4882a593Smuzhiyun 	edma->m2m = platform_get_device_id(pdev)->driver_data;
1333*4882a593Smuzhiyun 	edma->num_channels = pdata->num_channels;
1334*4882a593Smuzhiyun 
1335*4882a593Smuzhiyun 	INIT_LIST_HEAD(&dma_dev->channels);
1336*4882a593Smuzhiyun 	for (i = 0; i < pdata->num_channels; i++) {
1337*4882a593Smuzhiyun 		const struct ep93xx_dma_chan_data *cdata = &pdata->channels[i];
1338*4882a593Smuzhiyun 		struct ep93xx_dma_chan *edmac = &edma->channels[i];
1339*4882a593Smuzhiyun 
1340*4882a593Smuzhiyun 		edmac->chan.device = dma_dev;
1341*4882a593Smuzhiyun 		edmac->regs = cdata->base;
1342*4882a593Smuzhiyun 		edmac->irq = cdata->irq;
1343*4882a593Smuzhiyun 		edmac->edma = edma;
1344*4882a593Smuzhiyun 
1345*4882a593Smuzhiyun 		edmac->clk = clk_get(NULL, cdata->name);
1346*4882a593Smuzhiyun 		if (IS_ERR(edmac->clk)) {
1347*4882a593Smuzhiyun 			dev_warn(&pdev->dev, "failed to get clock for %s\n",
1348*4882a593Smuzhiyun 				 cdata->name);
1349*4882a593Smuzhiyun 			continue;
1350*4882a593Smuzhiyun 		}
1351*4882a593Smuzhiyun 
1352*4882a593Smuzhiyun 		spin_lock_init(&edmac->lock);
1353*4882a593Smuzhiyun 		INIT_LIST_HEAD(&edmac->active);
1354*4882a593Smuzhiyun 		INIT_LIST_HEAD(&edmac->queue);
1355*4882a593Smuzhiyun 		INIT_LIST_HEAD(&edmac->free_list);
1356*4882a593Smuzhiyun 		tasklet_setup(&edmac->tasklet, ep93xx_dma_tasklet);
1357*4882a593Smuzhiyun 
1358*4882a593Smuzhiyun 		list_add_tail(&edmac->chan.device_node,
1359*4882a593Smuzhiyun 			      &dma_dev->channels);
1360*4882a593Smuzhiyun 	}
1361*4882a593Smuzhiyun 
1362*4882a593Smuzhiyun 	dma_cap_zero(dma_dev->cap_mask);
1363*4882a593Smuzhiyun 	dma_cap_set(DMA_SLAVE, dma_dev->cap_mask);
1364*4882a593Smuzhiyun 	dma_cap_set(DMA_CYCLIC, dma_dev->cap_mask);
1365*4882a593Smuzhiyun 
1366*4882a593Smuzhiyun 	dma_dev->dev = &pdev->dev;
1367*4882a593Smuzhiyun 	dma_dev->device_alloc_chan_resources = ep93xx_dma_alloc_chan_resources;
1368*4882a593Smuzhiyun 	dma_dev->device_free_chan_resources = ep93xx_dma_free_chan_resources;
1369*4882a593Smuzhiyun 	dma_dev->device_prep_slave_sg = ep93xx_dma_prep_slave_sg;
1370*4882a593Smuzhiyun 	dma_dev->device_prep_dma_cyclic = ep93xx_dma_prep_dma_cyclic;
1371*4882a593Smuzhiyun 	dma_dev->device_config = ep93xx_dma_slave_config;
1372*4882a593Smuzhiyun 	dma_dev->device_synchronize = ep93xx_dma_synchronize;
1373*4882a593Smuzhiyun 	dma_dev->device_terminate_all = ep93xx_dma_terminate_all;
1374*4882a593Smuzhiyun 	dma_dev->device_issue_pending = ep93xx_dma_issue_pending;
1375*4882a593Smuzhiyun 	dma_dev->device_tx_status = ep93xx_dma_tx_status;
1376*4882a593Smuzhiyun 
1377*4882a593Smuzhiyun 	dma_set_max_seg_size(dma_dev->dev, DMA_MAX_CHAN_BYTES);
1378*4882a593Smuzhiyun 
1379*4882a593Smuzhiyun 	if (edma->m2m) {
1380*4882a593Smuzhiyun 		dma_cap_set(DMA_MEMCPY, dma_dev->cap_mask);
1381*4882a593Smuzhiyun 		dma_dev->device_prep_dma_memcpy = ep93xx_dma_prep_dma_memcpy;
1382*4882a593Smuzhiyun 
1383*4882a593Smuzhiyun 		edma->hw_setup = m2m_hw_setup;
1384*4882a593Smuzhiyun 		edma->hw_shutdown = m2m_hw_shutdown;
1385*4882a593Smuzhiyun 		edma->hw_submit = m2m_hw_submit;
1386*4882a593Smuzhiyun 		edma->hw_interrupt = m2m_hw_interrupt;
1387*4882a593Smuzhiyun 	} else {
1388*4882a593Smuzhiyun 		dma_cap_set(DMA_PRIVATE, dma_dev->cap_mask);
1389*4882a593Smuzhiyun 
1390*4882a593Smuzhiyun 		edma->hw_synchronize = m2p_hw_synchronize;
1391*4882a593Smuzhiyun 		edma->hw_setup = m2p_hw_setup;
1392*4882a593Smuzhiyun 		edma->hw_shutdown = m2p_hw_shutdown;
1393*4882a593Smuzhiyun 		edma->hw_submit = m2p_hw_submit;
1394*4882a593Smuzhiyun 		edma->hw_interrupt = m2p_hw_interrupt;
1395*4882a593Smuzhiyun 	}
1396*4882a593Smuzhiyun 
1397*4882a593Smuzhiyun 	ret = dma_async_device_register(dma_dev);
1398*4882a593Smuzhiyun 	if (unlikely(ret)) {
1399*4882a593Smuzhiyun 		for (i = 0; i < edma->num_channels; i++) {
1400*4882a593Smuzhiyun 			struct ep93xx_dma_chan *edmac = &edma->channels[i];
1401*4882a593Smuzhiyun 			if (!IS_ERR_OR_NULL(edmac->clk))
1402*4882a593Smuzhiyun 				clk_put(edmac->clk);
1403*4882a593Smuzhiyun 		}
1404*4882a593Smuzhiyun 		kfree(edma);
1405*4882a593Smuzhiyun 	} else {
1406*4882a593Smuzhiyun 		dev_info(dma_dev->dev, "EP93xx M2%s DMA ready\n",
1407*4882a593Smuzhiyun 			 edma->m2m ? "M" : "P");
1408*4882a593Smuzhiyun 	}
1409*4882a593Smuzhiyun 
1410*4882a593Smuzhiyun 	return ret;
1411*4882a593Smuzhiyun }
1412*4882a593Smuzhiyun 
1413*4882a593Smuzhiyun static const struct platform_device_id ep93xx_dma_driver_ids[] = {
1414*4882a593Smuzhiyun 	{ "ep93xx-dma-m2p", 0 },
1415*4882a593Smuzhiyun 	{ "ep93xx-dma-m2m", 1 },
1416*4882a593Smuzhiyun 	{ },
1417*4882a593Smuzhiyun };
1418*4882a593Smuzhiyun 
1419*4882a593Smuzhiyun static struct platform_driver ep93xx_dma_driver = {
1420*4882a593Smuzhiyun 	.driver		= {
1421*4882a593Smuzhiyun 		.name	= "ep93xx-dma",
1422*4882a593Smuzhiyun 	},
1423*4882a593Smuzhiyun 	.id_table	= ep93xx_dma_driver_ids,
1424*4882a593Smuzhiyun };
1425*4882a593Smuzhiyun 
ep93xx_dma_module_init(void)1426*4882a593Smuzhiyun static int __init ep93xx_dma_module_init(void)
1427*4882a593Smuzhiyun {
1428*4882a593Smuzhiyun 	return platform_driver_probe(&ep93xx_dma_driver, ep93xx_dma_probe);
1429*4882a593Smuzhiyun }
1430*4882a593Smuzhiyun subsys_initcall(ep93xx_dma_module_init);
1431*4882a593Smuzhiyun 
1432*4882a593Smuzhiyun MODULE_AUTHOR("Mika Westerberg <mika.westerberg@iki.fi>");
1433*4882a593Smuzhiyun MODULE_DESCRIPTION("EP93xx DMA driver");
1434*4882a593Smuzhiyun MODULE_LICENSE("GPL");
1435