• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Driver for the Atmel Extensible DMA Controller (aka XDMAC on AT91 systems)
4  *
5  * Copyright (C) 2014 Atmel Corporation
6  *
7  * Author: Ludovic Desroches <ludovic.desroches@atmel.com>
8  */
9 
10 #include <asm/barrier.h>
11 #include <dt-bindings/dma/at91.h>
12 #include <linux/clk.h>
13 #include <linux/dmaengine.h>
14 #include <linux/dmapool.h>
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <linux/kernel.h>
18 #include <linux/list.h>
19 #include <linux/module.h>
20 #include <linux/of_dma.h>
21 #include <linux/of_platform.h>
22 #include <linux/platform_device.h>
23 #include <linux/pm.h>
24 
25 #include "dmaengine.h"
26 
27 /* Global registers */
28 #define AT_XDMAC_GTYPE		0x00	/* Global Type Register */
29 #define		AT_XDMAC_NB_CH(i)	(((i) & 0x1F) + 1)		/* Number of Channels Minus One */
30 #define		AT_XDMAC_FIFO_SZ(i)	(((i) >> 5) & 0x7FF)		/* Number of Bytes */
31 #define		AT_XDMAC_NB_REQ(i)	((((i) >> 16) & 0x3F) + 1)	/* Number of Peripheral Requests Minus One */
32 #define AT_XDMAC_GCFG		0x04	/* Global Configuration Register */
33 #define		AT_XDMAC_WRHP(i)		(((i) & 0xF) << 4)
34 #define		AT_XDMAC_WRMP(i)		(((i) & 0xF) << 8)
35 #define		AT_XDMAC_WRLP(i)		(((i) & 0xF) << 12)
36 #define		AT_XDMAC_RDHP(i)		(((i) & 0xF) << 16)
37 #define		AT_XDMAC_RDMP(i)		(((i) & 0xF) << 20)
38 #define		AT_XDMAC_RDLP(i)		(((i) & 0xF) << 24)
39 #define		AT_XDMAC_RDSG(i)		(((i) & 0xF) << 28)
40 #define AT_XDMAC_GCFG_M2M	(AT_XDMAC_RDLP(0xF) | AT_XDMAC_WRLP(0xF))
41 #define AT_XDMAC_GCFG_P2M	(AT_XDMAC_RDSG(0x1) | AT_XDMAC_RDHP(0x3) | \
42 				AT_XDMAC_WRHP(0x5))
43 #define AT_XDMAC_GWAC		0x08	/* Global Weighted Arbiter Configuration Register */
44 #define		AT_XDMAC_PW0(i)		(((i) & 0xF) << 0)
45 #define		AT_XDMAC_PW1(i)		(((i) & 0xF) << 4)
46 #define		AT_XDMAC_PW2(i)		(((i) & 0xF) << 8)
47 #define		AT_XDMAC_PW3(i)		(((i) & 0xF) << 12)
48 #define AT_XDMAC_GWAC_M2M	0
49 #define AT_XDMAC_GWAC_P2M	(AT_XDMAC_PW0(0xF) | AT_XDMAC_PW2(0xF))
50 
51 #define AT_XDMAC_GIE		0x0C	/* Global Interrupt Enable Register */
52 #define AT_XDMAC_GID		0x10	/* Global Interrupt Disable Register */
53 #define AT_XDMAC_GIM		0x14	/* Global Interrupt Mask Register */
54 #define AT_XDMAC_GIS		0x18	/* Global Interrupt Status Register */
55 #define AT_XDMAC_GE		0x1C	/* Global Channel Enable Register */
56 #define AT_XDMAC_GD		0x20	/* Global Channel Disable Register */
57 #define AT_XDMAC_GS		0x24	/* Global Channel Status Register */
58 #define AT_XDMAC_VERSION	0xFFC	/* XDMAC Version Register */
59 
60 /* Channel relative registers offsets */
61 #define AT_XDMAC_CIE		0x00	/* Channel Interrupt Enable Register */
62 #define		AT_XDMAC_CIE_BIE	BIT(0)	/* End of Block Interrupt Enable Bit */
63 #define		AT_XDMAC_CIE_LIE	BIT(1)	/* End of Linked List Interrupt Enable Bit */
64 #define		AT_XDMAC_CIE_DIE	BIT(2)	/* End of Disable Interrupt Enable Bit */
65 #define		AT_XDMAC_CIE_FIE	BIT(3)	/* End of Flush Interrupt Enable Bit */
66 #define		AT_XDMAC_CIE_RBEIE	BIT(4)	/* Read Bus Error Interrupt Enable Bit */
67 #define		AT_XDMAC_CIE_WBEIE	BIT(5)	/* Write Bus Error Interrupt Enable Bit */
68 #define		AT_XDMAC_CIE_ROIE	BIT(6)	/* Request Overflow Interrupt Enable Bit */
69 #define AT_XDMAC_CID		0x04	/* Channel Interrupt Disable Register */
70 #define		AT_XDMAC_CID_BID	BIT(0)	/* End of Block Interrupt Disable Bit */
71 #define		AT_XDMAC_CID_LID	BIT(1)	/* End of Linked List Interrupt Disable Bit */
72 #define		AT_XDMAC_CID_DID	BIT(2)	/* End of Disable Interrupt Disable Bit */
73 #define		AT_XDMAC_CID_FID	BIT(3)	/* End of Flush Interrupt Disable Bit */
74 #define		AT_XDMAC_CID_RBEID	BIT(4)	/* Read Bus Error Interrupt Disable Bit */
75 #define		AT_XDMAC_CID_WBEID	BIT(5)	/* Write Bus Error Interrupt Disable Bit */
76 #define		AT_XDMAC_CID_ROID	BIT(6)	/* Request Overflow Interrupt Disable Bit */
77 #define AT_XDMAC_CIM		0x08	/* Channel Interrupt Mask Register */
78 #define		AT_XDMAC_CIM_BIM	BIT(0)	/* End of Block Interrupt Mask Bit */
79 #define		AT_XDMAC_CIM_LIM	BIT(1)	/* End of Linked List Interrupt Mask Bit */
80 #define		AT_XDMAC_CIM_DIM	BIT(2)	/* End of Disable Interrupt Mask Bit */
81 #define		AT_XDMAC_CIM_FIM	BIT(3)	/* End of Flush Interrupt Mask Bit */
82 #define		AT_XDMAC_CIM_RBEIM	BIT(4)	/* Read Bus Error Interrupt Mask Bit */
83 #define		AT_XDMAC_CIM_WBEIM	BIT(5)	/* Write Bus Error Interrupt Mask Bit */
84 #define		AT_XDMAC_CIM_ROIM	BIT(6)	/* Request Overflow Interrupt Mask Bit */
85 #define AT_XDMAC_CIS		0x0C	/* Channel Interrupt Status Register */
86 #define		AT_XDMAC_CIS_BIS	BIT(0)	/* End of Block Interrupt Status Bit */
87 #define		AT_XDMAC_CIS_LIS	BIT(1)	/* End of Linked List Interrupt Status Bit */
88 #define		AT_XDMAC_CIS_DIS	BIT(2)	/* End of Disable Interrupt Status Bit */
89 #define		AT_XDMAC_CIS_FIS	BIT(3)	/* End of Flush Interrupt Status Bit */
90 #define		AT_XDMAC_CIS_RBEIS	BIT(4)	/* Read Bus Error Interrupt Status Bit */
91 #define		AT_XDMAC_CIS_WBEIS	BIT(5)	/* Write Bus Error Interrupt Status Bit */
92 #define		AT_XDMAC_CIS_ROIS	BIT(6)	/* Request Overflow Interrupt Status Bit */
93 #define AT_XDMAC_CSA		0x10	/* Channel Source Address Register */
94 #define AT_XDMAC_CDA		0x14	/* Channel Destination Address Register */
95 #define AT_XDMAC_CNDA		0x18	/* Channel Next Descriptor Address Register */
96 #define		AT_XDMAC_CNDA_NDAIF(i)	((i) & 0x1)			/* Channel x Next Descriptor Interface */
97 #define		AT_XDMAC_CNDA_NDA(i)	((i) & 0xfffffffc)		/* Channel x Next Descriptor Address */
98 #define AT_XDMAC_CNDC		0x1C	/* Channel Next Descriptor Control Register */
99 #define		AT_XDMAC_CNDC_NDE		(0x1 << 0)		/* Channel x Next Descriptor Enable */
100 #define		AT_XDMAC_CNDC_NDSUP		(0x1 << 1)		/* Channel x Next Descriptor Source Update */
101 #define		AT_XDMAC_CNDC_NDDUP		(0x1 << 2)		/* Channel x Next Descriptor Destination Update */
102 #define		AT_XDMAC_CNDC_NDVIEW_MASK	GENMASK(28, 27)
103 #define		AT_XDMAC_CNDC_NDVIEW_NDV0	(0x0 << 3)		/* Channel x Next Descriptor View 0 */
104 #define		AT_XDMAC_CNDC_NDVIEW_NDV1	(0x1 << 3)		/* Channel x Next Descriptor View 1 */
105 #define		AT_XDMAC_CNDC_NDVIEW_NDV2	(0x2 << 3)		/* Channel x Next Descriptor View 2 */
106 #define		AT_XDMAC_CNDC_NDVIEW_NDV3	(0x3 << 3)		/* Channel x Next Descriptor View 3 */
107 #define AT_XDMAC_CUBC		0x20	/* Channel Microblock Control Register */
108 #define AT_XDMAC_CBC		0x24	/* Channel Block Control Register */
109 #define AT_XDMAC_CC		0x28	/* Channel Configuration Register */
110 #define		AT_XDMAC_CC_TYPE	(0x1 << 0)	/* Channel Transfer Type */
111 #define			AT_XDMAC_CC_TYPE_MEM_TRAN	(0x0 << 0)	/* Memory to Memory Transfer */
112 #define			AT_XDMAC_CC_TYPE_PER_TRAN	(0x1 << 0)	/* Peripheral to Memory or Memory to Peripheral Transfer */
113 #define		AT_XDMAC_CC_MBSIZE_MASK	(0x3 << 1)
114 #define			AT_XDMAC_CC_MBSIZE_SINGLE	(0x0 << 1)
115 #define			AT_XDMAC_CC_MBSIZE_FOUR		(0x1 << 1)
116 #define			AT_XDMAC_CC_MBSIZE_EIGHT	(0x2 << 1)
117 #define			AT_XDMAC_CC_MBSIZE_SIXTEEN	(0x3 << 1)
118 #define		AT_XDMAC_CC_DSYNC	(0x1 << 4)	/* Channel Synchronization */
119 #define			AT_XDMAC_CC_DSYNC_PER2MEM	(0x0 << 4)
120 #define			AT_XDMAC_CC_DSYNC_MEM2PER	(0x1 << 4)
121 #define		AT_XDMAC_CC_PROT	(0x1 << 5)	/* Channel Protection */
122 #define			AT_XDMAC_CC_PROT_SEC		(0x0 << 5)
123 #define			AT_XDMAC_CC_PROT_UNSEC		(0x1 << 5)
124 #define		AT_XDMAC_CC_SWREQ	(0x1 << 6)	/* Channel Software Request Trigger */
125 #define			AT_XDMAC_CC_SWREQ_HWR_CONNECTED	(0x0 << 6)
126 #define			AT_XDMAC_CC_SWREQ_SWR_CONNECTED	(0x1 << 6)
127 #define		AT_XDMAC_CC_MEMSET	(0x1 << 7)	/* Channel Fill Block of memory */
128 #define			AT_XDMAC_CC_MEMSET_NORMAL_MODE	(0x0 << 7)
129 #define			AT_XDMAC_CC_MEMSET_HW_MODE	(0x1 << 7)
130 #define		AT_XDMAC_CC_CSIZE(i)	((0x7 & (i)) << 8)	/* Channel Chunk Size */
131 #define		AT_XDMAC_CC_DWIDTH_OFFSET	11
132 #define		AT_XDMAC_CC_DWIDTH_MASK	(0x3 << AT_XDMAC_CC_DWIDTH_OFFSET)
133 #define		AT_XDMAC_CC_DWIDTH(i)	((0x3 & (i)) << AT_XDMAC_CC_DWIDTH_OFFSET)	/* Channel Data Width */
134 #define			AT_XDMAC_CC_DWIDTH_BYTE		0x0
135 #define			AT_XDMAC_CC_DWIDTH_HALFWORD	0x1
136 #define			AT_XDMAC_CC_DWIDTH_WORD		0x2
137 #define			AT_XDMAC_CC_DWIDTH_DWORD	0x3
138 #define		AT_XDMAC_CC_SIF(i)	((0x1 & (i)) << 13)	/* Channel Source Interface Identifier */
139 #define		AT_XDMAC_CC_DIF(i)	((0x1 & (i)) << 14)	/* Channel Destination Interface Identifier */
140 #define		AT_XDMAC_CC_SAM_MASK	(0x3 << 16)	/* Channel Source Addressing Mode */
141 #define			AT_XDMAC_CC_SAM_FIXED_AM	(0x0 << 16)
142 #define			AT_XDMAC_CC_SAM_INCREMENTED_AM	(0x1 << 16)
143 #define			AT_XDMAC_CC_SAM_UBS_AM		(0x2 << 16)
144 #define			AT_XDMAC_CC_SAM_UBS_DS_AM	(0x3 << 16)
145 #define		AT_XDMAC_CC_DAM_MASK	(0x3 << 18)	/* Channel Source Addressing Mode */
146 #define			AT_XDMAC_CC_DAM_FIXED_AM	(0x0 << 18)
147 #define			AT_XDMAC_CC_DAM_INCREMENTED_AM	(0x1 << 18)
148 #define			AT_XDMAC_CC_DAM_UBS_AM		(0x2 << 18)
149 #define			AT_XDMAC_CC_DAM_UBS_DS_AM	(0x3 << 18)
150 #define		AT_XDMAC_CC_INITD	(0x1 << 21)	/* Channel Initialization Terminated (read only) */
151 #define			AT_XDMAC_CC_INITD_TERMINATED	(0x0 << 21)
152 #define			AT_XDMAC_CC_INITD_IN_PROGRESS	(0x1 << 21)
153 #define		AT_XDMAC_CC_RDIP	(0x1 << 22)	/* Read in Progress (read only) */
154 #define			AT_XDMAC_CC_RDIP_DONE		(0x0 << 22)
155 #define			AT_XDMAC_CC_RDIP_IN_PROGRESS	(0x1 << 22)
156 #define		AT_XDMAC_CC_WRIP	(0x1 << 23)	/* Write in Progress (read only) */
157 #define			AT_XDMAC_CC_WRIP_DONE		(0x0 << 23)
158 #define			AT_XDMAC_CC_WRIP_IN_PROGRESS	(0x1 << 23)
159 #define		AT_XDMAC_CC_PERID(i)	((0x7f & (i)) << 24)	/* Channel Peripheral Identifier */
160 #define AT_XDMAC_CDS_MSP	0x2C	/* Channel Data Stride Memory Set Pattern */
161 #define AT_XDMAC_CSUS		0x30	/* Channel Source Microblock Stride */
162 #define AT_XDMAC_CDUS		0x34	/* Channel Destination Microblock Stride */
163 
164 /* Microblock control members */
165 #define AT_XDMAC_MBR_UBC_UBLEN_MAX	0xFFFFFFUL	/* Maximum Microblock Length */
166 #define AT_XDMAC_MBR_UBC_NDE		(0x1 << 24)	/* Next Descriptor Enable */
167 #define AT_XDMAC_MBR_UBC_NSEN		(0x1 << 25)	/* Next Descriptor Source Update */
168 #define AT_XDMAC_MBR_UBC_NDEN		(0x1 << 26)	/* Next Descriptor Destination Update */
169 #define AT_XDMAC_MBR_UBC_NDV0		(0x0 << 27)	/* Next Descriptor View 0 */
170 #define AT_XDMAC_MBR_UBC_NDV1		(0x1 << 27)	/* Next Descriptor View 1 */
171 #define AT_XDMAC_MBR_UBC_NDV2		(0x2 << 27)	/* Next Descriptor View 2 */
172 #define AT_XDMAC_MBR_UBC_NDV3		(0x3 << 27)	/* Next Descriptor View 3 */
173 
174 #define AT_XDMAC_MAX_CHAN	0x20
175 #define AT_XDMAC_MAX_CSIZE	16	/* 16 data */
176 #define AT_XDMAC_MAX_DWIDTH	8	/* 64 bits */
177 #define AT_XDMAC_RESIDUE_MAX_RETRIES	5
178 
179 #define AT_XDMAC_DMA_BUSWIDTHS\
180 	(BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) |\
181 	BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |\
182 	BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |\
183 	BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |\
184 	BIT(DMA_SLAVE_BUSWIDTH_8_BYTES))
185 
186 enum atc_status {
187 	AT_XDMAC_CHAN_IS_CYCLIC = 0,
188 	AT_XDMAC_CHAN_IS_PAUSED,
189 };
190 
191 struct at_xdmac_layout {
192 	/* Global Channel Read Suspend Register */
193 	u8				grs;
194 	/* Global Write Suspend Register */
195 	u8				gws;
196 	/* Global Channel Read Write Suspend Register */
197 	u8				grws;
198 	/* Global Channel Read Write Resume Register */
199 	u8				grwr;
200 	/* Global Channel Software Request Register */
201 	u8				gswr;
202 	/* Global channel Software Request Status Register */
203 	u8				gsws;
204 	/* Global Channel Software Flush Request Register */
205 	u8				gswf;
206 	/* Channel reg base */
207 	u8				chan_cc_reg_base;
208 	/* Source/Destination Interface must be specified or not */
209 	bool				sdif;
210 	/* AXI queue priority configuration supported */
211 	bool				axi_config;
212 };
213 
214 /* ----- Channels ----- */
215 struct at_xdmac_chan {
216 	struct dma_chan			chan;
217 	void __iomem			*ch_regs;
218 	u32				mask;		/* Channel Mask */
219 	u32				cfg;		/* Channel Configuration Register */
220 	u8				perid;		/* Peripheral ID */
221 	u8				perif;		/* Peripheral Interface */
222 	u8				memif;		/* Memory Interface */
223 	u32				save_cc;
224 	u32				save_cim;
225 	u32				save_cnda;
226 	u32				save_cndc;
227 	u32				irq_status;
228 	unsigned long			status;
229 	struct tasklet_struct		tasklet;
230 	struct dma_slave_config		sconfig;
231 
232 	spinlock_t			lock;
233 
234 	struct list_head		xfers_list;
235 	struct list_head		free_descs_list;
236 };
237 
238 
239 /* ----- Controller ----- */
240 struct at_xdmac {
241 	struct dma_device	dma;
242 	void __iomem		*regs;
243 	int			irq;
244 	struct clk		*clk;
245 	u32			save_gim;
246 	u32			save_gs;
247 	struct dma_pool		*at_xdmac_desc_pool;
248 	const struct at_xdmac_layout	*layout;
249 	struct at_xdmac_chan	chan[];
250 };
251 
252 
253 /* ----- Descriptors ----- */
254 
255 /* Linked List Descriptor */
256 struct at_xdmac_lld {
257 	u32 mbr_nda;	/* Next Descriptor Member */
258 	u32 mbr_ubc;	/* Microblock Control Member */
259 	u32 mbr_sa;	/* Source Address Member */
260 	u32 mbr_da;	/* Destination Address Member */
261 	u32 mbr_cfg;	/* Configuration Register */
262 	u32 mbr_bc;	/* Block Control Register */
263 	u32 mbr_ds;	/* Data Stride Register */
264 	u32 mbr_sus;	/* Source Microblock Stride Register */
265 	u32 mbr_dus;	/* Destination Microblock Stride Register */
266 };
267 
268 /* 64-bit alignment needed to update CNDA and CUBC registers in an atomic way. */
269 struct at_xdmac_desc {
270 	struct at_xdmac_lld		lld;
271 	enum dma_transfer_direction	direction;
272 	struct dma_async_tx_descriptor	tx_dma_desc;
273 	struct list_head		desc_node;
274 	/* Following members are only used by the first descriptor */
275 	bool				active_xfer;
276 	unsigned int			xfer_size;
277 	struct list_head		descs_list;
278 	struct list_head		xfer_node;
279 } __aligned(sizeof(u64));
280 
281 static const struct at_xdmac_layout at_xdmac_sama5d4_layout = {
282 	.grs = 0x28,
283 	.gws = 0x2C,
284 	.grws = 0x30,
285 	.grwr = 0x34,
286 	.gswr = 0x38,
287 	.gsws = 0x3C,
288 	.gswf = 0x40,
289 	.chan_cc_reg_base = 0x50,
290 	.sdif = true,
291 	.axi_config = false,
292 };
293 
294 static const struct at_xdmac_layout at_xdmac_sama7g5_layout = {
295 	.grs = 0x30,
296 	.gws = 0x38,
297 	.grws = 0x40,
298 	.grwr = 0x44,
299 	.gswr = 0x48,
300 	.gsws = 0x4C,
301 	.gswf = 0x50,
302 	.chan_cc_reg_base = 0x60,
303 	.sdif = false,
304 	.axi_config = true,
305 };
306 
at_xdmac_chan_reg_base(struct at_xdmac * atxdmac,unsigned int chan_nb)307 static inline void __iomem *at_xdmac_chan_reg_base(struct at_xdmac *atxdmac, unsigned int chan_nb)
308 {
309 	return atxdmac->regs + (atxdmac->layout->chan_cc_reg_base + chan_nb * 0x40);
310 }
311 
312 #define at_xdmac_read(atxdmac, reg) readl_relaxed((atxdmac)->regs + (reg))
313 #define at_xdmac_write(atxdmac, reg, value) \
314 	writel_relaxed((value), (atxdmac)->regs + (reg))
315 
316 #define at_xdmac_chan_read(atchan, reg) readl_relaxed((atchan)->ch_regs + (reg))
317 #define at_xdmac_chan_write(atchan, reg, value) writel_relaxed((value), (atchan)->ch_regs + (reg))
318 
to_at_xdmac_chan(struct dma_chan * dchan)319 static inline struct at_xdmac_chan *to_at_xdmac_chan(struct dma_chan *dchan)
320 {
321 	return container_of(dchan, struct at_xdmac_chan, chan);
322 }
323 
chan2dev(struct dma_chan * chan)324 static struct device *chan2dev(struct dma_chan *chan)
325 {
326 	return &chan->dev->device;
327 }
328 
to_at_xdmac(struct dma_device * ddev)329 static inline struct at_xdmac *to_at_xdmac(struct dma_device *ddev)
330 {
331 	return container_of(ddev, struct at_xdmac, dma);
332 }
333 
txd_to_at_desc(struct dma_async_tx_descriptor * txd)334 static inline struct at_xdmac_desc *txd_to_at_desc(struct dma_async_tx_descriptor *txd)
335 {
336 	return container_of(txd, struct at_xdmac_desc, tx_dma_desc);
337 }
338 
at_xdmac_chan_is_cyclic(struct at_xdmac_chan * atchan)339 static inline int at_xdmac_chan_is_cyclic(struct at_xdmac_chan *atchan)
340 {
341 	return test_bit(AT_XDMAC_CHAN_IS_CYCLIC, &atchan->status);
342 }
343 
at_xdmac_chan_is_paused(struct at_xdmac_chan * atchan)344 static inline int at_xdmac_chan_is_paused(struct at_xdmac_chan *atchan)
345 {
346 	return test_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status);
347 }
348 
at_xdmac_chan_is_peripheral_xfer(u32 cfg)349 static inline bool at_xdmac_chan_is_peripheral_xfer(u32 cfg)
350 {
351 	return cfg & AT_XDMAC_CC_TYPE_PER_TRAN;
352 }
353 
at_xdmac_get_dwidth(u32 cfg)354 static inline u8 at_xdmac_get_dwidth(u32 cfg)
355 {
356 	return (cfg & AT_XDMAC_CC_DWIDTH_MASK) >> AT_XDMAC_CC_DWIDTH_OFFSET;
357 };
358 
359 static unsigned int init_nr_desc_per_channel = 64;
360 module_param(init_nr_desc_per_channel, uint, 0644);
361 MODULE_PARM_DESC(init_nr_desc_per_channel,
362 		 "initial descriptors per channel (default: 64)");
363 
364 
at_xdmac_chan_is_enabled(struct at_xdmac_chan * atchan)365 static bool at_xdmac_chan_is_enabled(struct at_xdmac_chan *atchan)
366 {
367 	return at_xdmac_chan_read(atchan, AT_XDMAC_GS) & atchan->mask;
368 }
369 
at_xdmac_off(struct at_xdmac * atxdmac)370 static void at_xdmac_off(struct at_xdmac *atxdmac)
371 {
372 	at_xdmac_write(atxdmac, AT_XDMAC_GD, -1L);
373 
374 	/* Wait that all chans are disabled. */
375 	while (at_xdmac_read(atxdmac, AT_XDMAC_GS))
376 		cpu_relax();
377 
378 	at_xdmac_write(atxdmac, AT_XDMAC_GID, -1L);
379 }
380 
381 /* Call with lock hold. */
at_xdmac_start_xfer(struct at_xdmac_chan * atchan,struct at_xdmac_desc * first)382 static void at_xdmac_start_xfer(struct at_xdmac_chan *atchan,
383 				struct at_xdmac_desc *first)
384 {
385 	struct at_xdmac	*atxdmac = to_at_xdmac(atchan->chan.device);
386 	u32		reg;
387 
388 	dev_vdbg(chan2dev(&atchan->chan), "%s: desc 0x%p\n", __func__, first);
389 
390 	/* Set transfer as active to not try to start it again. */
391 	first->active_xfer = true;
392 
393 	/* Tell xdmac where to get the first descriptor. */
394 	reg = AT_XDMAC_CNDA_NDA(first->tx_dma_desc.phys);
395 	if (atxdmac->layout->sdif)
396 		reg |= AT_XDMAC_CNDA_NDAIF(atchan->memif);
397 
398 	at_xdmac_chan_write(atchan, AT_XDMAC_CNDA, reg);
399 
400 	/*
401 	 * When doing non cyclic transfer we need to use the next
402 	 * descriptor view 2 since some fields of the configuration register
403 	 * depend on transfer size and src/dest addresses.
404 	 */
405 	if (at_xdmac_chan_is_cyclic(atchan))
406 		reg = AT_XDMAC_CNDC_NDVIEW_NDV1;
407 	else if ((first->lld.mbr_ubc &
408 		  AT_XDMAC_CNDC_NDVIEW_MASK) == AT_XDMAC_MBR_UBC_NDV3)
409 		reg = AT_XDMAC_CNDC_NDVIEW_NDV3;
410 	else
411 		reg = AT_XDMAC_CNDC_NDVIEW_NDV2;
412 	/*
413 	 * Even if the register will be updated from the configuration in the
414 	 * descriptor when using view 2 or higher, the PROT bit won't be set
415 	 * properly. This bit can be modified only by using the channel
416 	 * configuration register.
417 	 */
418 	at_xdmac_chan_write(atchan, AT_XDMAC_CC, first->lld.mbr_cfg);
419 
420 	reg |= AT_XDMAC_CNDC_NDDUP
421 	       | AT_XDMAC_CNDC_NDSUP
422 	       | AT_XDMAC_CNDC_NDE;
423 	at_xdmac_chan_write(atchan, AT_XDMAC_CNDC, reg);
424 
425 	dev_vdbg(chan2dev(&atchan->chan),
426 		 "%s: CC=0x%08x CNDA=0x%08x, CNDC=0x%08x, CSA=0x%08x, CDA=0x%08x, CUBC=0x%08x\n",
427 		 __func__, at_xdmac_chan_read(atchan, AT_XDMAC_CC),
428 		 at_xdmac_chan_read(atchan, AT_XDMAC_CNDA),
429 		 at_xdmac_chan_read(atchan, AT_XDMAC_CNDC),
430 		 at_xdmac_chan_read(atchan, AT_XDMAC_CSA),
431 		 at_xdmac_chan_read(atchan, AT_XDMAC_CDA),
432 		 at_xdmac_chan_read(atchan, AT_XDMAC_CUBC));
433 
434 	at_xdmac_chan_write(atchan, AT_XDMAC_CID, 0xffffffff);
435 	reg = AT_XDMAC_CIE_RBEIE | AT_XDMAC_CIE_WBEIE;
436 	/*
437 	 * Request Overflow Error is only for peripheral synchronized transfers
438 	 */
439 	if (at_xdmac_chan_is_peripheral_xfer(first->lld.mbr_cfg))
440 		reg |= AT_XDMAC_CIE_ROIE;
441 
442 	/*
443 	 * There is no end of list when doing cyclic dma, we need to get
444 	 * an interrupt after each periods.
445 	 */
446 	if (at_xdmac_chan_is_cyclic(atchan))
447 		at_xdmac_chan_write(atchan, AT_XDMAC_CIE,
448 				    reg | AT_XDMAC_CIE_BIE);
449 	else
450 		at_xdmac_chan_write(atchan, AT_XDMAC_CIE,
451 				    reg | AT_XDMAC_CIE_LIE);
452 	at_xdmac_write(atxdmac, AT_XDMAC_GIE, atchan->mask);
453 	dev_vdbg(chan2dev(&atchan->chan),
454 		 "%s: enable channel (0x%08x)\n", __func__, atchan->mask);
455 	wmb();
456 	at_xdmac_write(atxdmac, AT_XDMAC_GE, atchan->mask);
457 
458 	dev_vdbg(chan2dev(&atchan->chan),
459 		 "%s: CC=0x%08x CNDA=0x%08x, CNDC=0x%08x, CSA=0x%08x, CDA=0x%08x, CUBC=0x%08x\n",
460 		 __func__, at_xdmac_chan_read(atchan, AT_XDMAC_CC),
461 		 at_xdmac_chan_read(atchan, AT_XDMAC_CNDA),
462 		 at_xdmac_chan_read(atchan, AT_XDMAC_CNDC),
463 		 at_xdmac_chan_read(atchan, AT_XDMAC_CSA),
464 		 at_xdmac_chan_read(atchan, AT_XDMAC_CDA),
465 		 at_xdmac_chan_read(atchan, AT_XDMAC_CUBC));
466 
467 }
468 
at_xdmac_tx_submit(struct dma_async_tx_descriptor * tx)469 static dma_cookie_t at_xdmac_tx_submit(struct dma_async_tx_descriptor *tx)
470 {
471 	struct at_xdmac_desc	*desc = txd_to_at_desc(tx);
472 	struct at_xdmac_chan	*atchan = to_at_xdmac_chan(tx->chan);
473 	dma_cookie_t		cookie;
474 	unsigned long		irqflags;
475 
476 	spin_lock_irqsave(&atchan->lock, irqflags);
477 	cookie = dma_cookie_assign(tx);
478 
479 	list_add_tail(&desc->xfer_node, &atchan->xfers_list);
480 	spin_unlock_irqrestore(&atchan->lock, irqflags);
481 
482 	dev_vdbg(chan2dev(tx->chan), "%s: atchan 0x%p, add desc 0x%p to xfers_list\n",
483 		 __func__, atchan, desc);
484 
485 	return cookie;
486 }
487 
at_xdmac_alloc_desc(struct dma_chan * chan,gfp_t gfp_flags)488 static struct at_xdmac_desc *at_xdmac_alloc_desc(struct dma_chan *chan,
489 						 gfp_t gfp_flags)
490 {
491 	struct at_xdmac_desc	*desc;
492 	struct at_xdmac		*atxdmac = to_at_xdmac(chan->device);
493 	dma_addr_t		phys;
494 
495 	desc = dma_pool_zalloc(atxdmac->at_xdmac_desc_pool, gfp_flags, &phys);
496 	if (desc) {
497 		INIT_LIST_HEAD(&desc->descs_list);
498 		dma_async_tx_descriptor_init(&desc->tx_dma_desc, chan);
499 		desc->tx_dma_desc.tx_submit = at_xdmac_tx_submit;
500 		desc->tx_dma_desc.phys = phys;
501 	}
502 
503 	return desc;
504 }
505 
at_xdmac_init_used_desc(struct at_xdmac_desc * desc)506 static void at_xdmac_init_used_desc(struct at_xdmac_desc *desc)
507 {
508 	memset(&desc->lld, 0, sizeof(desc->lld));
509 	INIT_LIST_HEAD(&desc->descs_list);
510 	desc->direction = DMA_TRANS_NONE;
511 	desc->xfer_size = 0;
512 	desc->active_xfer = false;
513 }
514 
515 /* Call must be protected by lock. */
at_xdmac_get_desc(struct at_xdmac_chan * atchan)516 static struct at_xdmac_desc *at_xdmac_get_desc(struct at_xdmac_chan *atchan)
517 {
518 	struct at_xdmac_desc *desc;
519 
520 	if (list_empty(&atchan->free_descs_list)) {
521 		desc = at_xdmac_alloc_desc(&atchan->chan, GFP_NOWAIT);
522 	} else {
523 		desc = list_first_entry(&atchan->free_descs_list,
524 					struct at_xdmac_desc, desc_node);
525 		list_del(&desc->desc_node);
526 		at_xdmac_init_used_desc(desc);
527 	}
528 
529 	return desc;
530 }
531 
at_xdmac_queue_desc(struct dma_chan * chan,struct at_xdmac_desc * prev,struct at_xdmac_desc * desc)532 static void at_xdmac_queue_desc(struct dma_chan *chan,
533 				struct at_xdmac_desc *prev,
534 				struct at_xdmac_desc *desc)
535 {
536 	if (!prev || !desc)
537 		return;
538 
539 	prev->lld.mbr_nda = desc->tx_dma_desc.phys;
540 	prev->lld.mbr_ubc |= AT_XDMAC_MBR_UBC_NDE;
541 
542 	dev_dbg(chan2dev(chan),	"%s: chain lld: prev=0x%p, mbr_nda=%pad\n",
543 		__func__, prev, &prev->lld.mbr_nda);
544 }
545 
at_xdmac_increment_block_count(struct dma_chan * chan,struct at_xdmac_desc * desc)546 static inline void at_xdmac_increment_block_count(struct dma_chan *chan,
547 						  struct at_xdmac_desc *desc)
548 {
549 	if (!desc)
550 		return;
551 
552 	desc->lld.mbr_bc++;
553 
554 	dev_dbg(chan2dev(chan),
555 		"%s: incrementing the block count of the desc 0x%p\n",
556 		__func__, desc);
557 }
558 
at_xdmac_xlate(struct of_phandle_args * dma_spec,struct of_dma * of_dma)559 static struct dma_chan *at_xdmac_xlate(struct of_phandle_args *dma_spec,
560 				       struct of_dma *of_dma)
561 {
562 	struct at_xdmac		*atxdmac = of_dma->of_dma_data;
563 	struct at_xdmac_chan	*atchan;
564 	struct dma_chan		*chan;
565 	struct device		*dev = atxdmac->dma.dev;
566 
567 	if (dma_spec->args_count != 1) {
568 		dev_err(dev, "dma phandler args: bad number of args\n");
569 		return NULL;
570 	}
571 
572 	chan = dma_get_any_slave_channel(&atxdmac->dma);
573 	if (!chan) {
574 		dev_err(dev, "can't get a dma channel\n");
575 		return NULL;
576 	}
577 
578 	atchan = to_at_xdmac_chan(chan);
579 	atchan->memif = AT91_XDMAC_DT_GET_MEM_IF(dma_spec->args[0]);
580 	atchan->perif = AT91_XDMAC_DT_GET_PER_IF(dma_spec->args[0]);
581 	atchan->perid = AT91_XDMAC_DT_GET_PERID(dma_spec->args[0]);
582 	dev_dbg(dev, "chan dt cfg: memif=%u perif=%u perid=%u\n",
583 		 atchan->memif, atchan->perif, atchan->perid);
584 
585 	return chan;
586 }
587 
at_xdmac_compute_chan_conf(struct dma_chan * chan,enum dma_transfer_direction direction)588 static int at_xdmac_compute_chan_conf(struct dma_chan *chan,
589 				      enum dma_transfer_direction direction)
590 {
591 	struct at_xdmac_chan	*atchan = to_at_xdmac_chan(chan);
592 	struct at_xdmac		*atxdmac = to_at_xdmac(atchan->chan.device);
593 	int			csize, dwidth;
594 
595 	if (direction == DMA_DEV_TO_MEM) {
596 		atchan->cfg =
597 			AT91_XDMAC_DT_PERID(atchan->perid)
598 			| AT_XDMAC_CC_DAM_INCREMENTED_AM
599 			| AT_XDMAC_CC_SAM_FIXED_AM
600 			| AT_XDMAC_CC_SWREQ_HWR_CONNECTED
601 			| AT_XDMAC_CC_DSYNC_PER2MEM
602 			| AT_XDMAC_CC_MBSIZE_SIXTEEN
603 			| AT_XDMAC_CC_TYPE_PER_TRAN;
604 		if (atxdmac->layout->sdif)
605 			atchan->cfg |= AT_XDMAC_CC_DIF(atchan->memif) |
606 				       AT_XDMAC_CC_SIF(atchan->perif);
607 
608 		csize = ffs(atchan->sconfig.src_maxburst) - 1;
609 		if (csize < 0) {
610 			dev_err(chan2dev(chan), "invalid src maxburst value\n");
611 			return -EINVAL;
612 		}
613 		atchan->cfg |= AT_XDMAC_CC_CSIZE(csize);
614 		dwidth = ffs(atchan->sconfig.src_addr_width) - 1;
615 		if (dwidth < 0) {
616 			dev_err(chan2dev(chan), "invalid src addr width value\n");
617 			return -EINVAL;
618 		}
619 		atchan->cfg |= AT_XDMAC_CC_DWIDTH(dwidth);
620 	} else if (direction == DMA_MEM_TO_DEV) {
621 		atchan->cfg =
622 			AT91_XDMAC_DT_PERID(atchan->perid)
623 			| AT_XDMAC_CC_DAM_FIXED_AM
624 			| AT_XDMAC_CC_SAM_INCREMENTED_AM
625 			| AT_XDMAC_CC_SWREQ_HWR_CONNECTED
626 			| AT_XDMAC_CC_DSYNC_MEM2PER
627 			| AT_XDMAC_CC_MBSIZE_SIXTEEN
628 			| AT_XDMAC_CC_TYPE_PER_TRAN;
629 		if (atxdmac->layout->sdif)
630 			atchan->cfg |= AT_XDMAC_CC_DIF(atchan->perif) |
631 				       AT_XDMAC_CC_SIF(atchan->memif);
632 
633 		csize = ffs(atchan->sconfig.dst_maxburst) - 1;
634 		if (csize < 0) {
635 			dev_err(chan2dev(chan), "invalid src maxburst value\n");
636 			return -EINVAL;
637 		}
638 		atchan->cfg |= AT_XDMAC_CC_CSIZE(csize);
639 		dwidth = ffs(atchan->sconfig.dst_addr_width) - 1;
640 		if (dwidth < 0) {
641 			dev_err(chan2dev(chan), "invalid dst addr width value\n");
642 			return -EINVAL;
643 		}
644 		atchan->cfg |= AT_XDMAC_CC_DWIDTH(dwidth);
645 	}
646 
647 	dev_dbg(chan2dev(chan),	"%s: cfg=0x%08x\n", __func__, atchan->cfg);
648 
649 	return 0;
650 }
651 
652 /*
653  * Only check that maxburst and addr width values are supported by the
654  * the controller but not that the configuration is good to perform the
655  * transfer since we don't know the direction at this stage.
656  */
at_xdmac_check_slave_config(struct dma_slave_config * sconfig)657 static int at_xdmac_check_slave_config(struct dma_slave_config *sconfig)
658 {
659 	if ((sconfig->src_maxburst > AT_XDMAC_MAX_CSIZE)
660 	    || (sconfig->dst_maxburst > AT_XDMAC_MAX_CSIZE))
661 		return -EINVAL;
662 
663 	if ((sconfig->src_addr_width > AT_XDMAC_MAX_DWIDTH)
664 	    || (sconfig->dst_addr_width > AT_XDMAC_MAX_DWIDTH))
665 		return -EINVAL;
666 
667 	return 0;
668 }
669 
at_xdmac_set_slave_config(struct dma_chan * chan,struct dma_slave_config * sconfig)670 static int at_xdmac_set_slave_config(struct dma_chan *chan,
671 				      struct dma_slave_config *sconfig)
672 {
673 	struct at_xdmac_chan	*atchan = to_at_xdmac_chan(chan);
674 
675 	if (at_xdmac_check_slave_config(sconfig)) {
676 		dev_err(chan2dev(chan), "invalid slave configuration\n");
677 		return -EINVAL;
678 	}
679 
680 	memcpy(&atchan->sconfig, sconfig, sizeof(atchan->sconfig));
681 
682 	return 0;
683 }
684 
685 static struct dma_async_tx_descriptor *
at_xdmac_prep_slave_sg(struct dma_chan * chan,struct scatterlist * sgl,unsigned int sg_len,enum dma_transfer_direction direction,unsigned long flags,void * context)686 at_xdmac_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
687 		       unsigned int sg_len, enum dma_transfer_direction direction,
688 		       unsigned long flags, void *context)
689 {
690 	struct at_xdmac_chan		*atchan = to_at_xdmac_chan(chan);
691 	struct at_xdmac_desc		*first = NULL, *prev = NULL;
692 	struct scatterlist		*sg;
693 	int				i;
694 	unsigned int			xfer_size = 0;
695 	unsigned long			irqflags;
696 	struct dma_async_tx_descriptor	*ret = NULL;
697 
698 	if (!sgl)
699 		return NULL;
700 
701 	if (!is_slave_direction(direction)) {
702 		dev_err(chan2dev(chan), "invalid DMA direction\n");
703 		return NULL;
704 	}
705 
706 	dev_dbg(chan2dev(chan), "%s: sg_len=%d, dir=%s, flags=0x%lx\n",
707 		 __func__, sg_len,
708 		 direction == DMA_MEM_TO_DEV ? "to device" : "from device",
709 		 flags);
710 
711 	/* Protect dma_sconfig field that can be modified by set_slave_conf. */
712 	spin_lock_irqsave(&atchan->lock, irqflags);
713 
714 	if (at_xdmac_compute_chan_conf(chan, direction))
715 		goto spin_unlock;
716 
717 	/* Prepare descriptors. */
718 	for_each_sg(sgl, sg, sg_len, i) {
719 		struct at_xdmac_desc	*desc = NULL;
720 		u32			len, mem, dwidth, fixed_dwidth;
721 
722 		len = sg_dma_len(sg);
723 		mem = sg_dma_address(sg);
724 		if (unlikely(!len)) {
725 			dev_err(chan2dev(chan), "sg data length is zero\n");
726 			goto spin_unlock;
727 		}
728 		dev_dbg(chan2dev(chan), "%s: * sg%d len=%u, mem=0x%08x\n",
729 			 __func__, i, len, mem);
730 
731 		desc = at_xdmac_get_desc(atchan);
732 		if (!desc) {
733 			dev_err(chan2dev(chan), "can't get descriptor\n");
734 			if (first)
735 				list_splice_tail_init(&first->descs_list,
736 						      &atchan->free_descs_list);
737 			goto spin_unlock;
738 		}
739 
740 		/* Linked list descriptor setup. */
741 		if (direction == DMA_DEV_TO_MEM) {
742 			desc->lld.mbr_sa = atchan->sconfig.src_addr;
743 			desc->lld.mbr_da = mem;
744 		} else {
745 			desc->lld.mbr_sa = mem;
746 			desc->lld.mbr_da = atchan->sconfig.dst_addr;
747 		}
748 		dwidth = at_xdmac_get_dwidth(atchan->cfg);
749 		fixed_dwidth = IS_ALIGNED(len, 1 << dwidth)
750 			       ? dwidth
751 			       : AT_XDMAC_CC_DWIDTH_BYTE;
752 		desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV2			/* next descriptor view */
753 			| AT_XDMAC_MBR_UBC_NDEN					/* next descriptor dst parameter update */
754 			| AT_XDMAC_MBR_UBC_NSEN					/* next descriptor src parameter update */
755 			| (len >> fixed_dwidth);				/* microblock length */
756 		desc->lld.mbr_cfg = (atchan->cfg & ~AT_XDMAC_CC_DWIDTH_MASK) |
757 				    AT_XDMAC_CC_DWIDTH(fixed_dwidth);
758 		dev_dbg(chan2dev(chan),
759 			 "%s: lld: mbr_sa=%pad, mbr_da=%pad, mbr_ubc=0x%08x\n",
760 			 __func__, &desc->lld.mbr_sa, &desc->lld.mbr_da, desc->lld.mbr_ubc);
761 
762 		/* Chain lld. */
763 		if (prev)
764 			at_xdmac_queue_desc(chan, prev, desc);
765 
766 		prev = desc;
767 		if (!first)
768 			first = desc;
769 
770 		dev_dbg(chan2dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n",
771 			 __func__, desc, first);
772 		list_add_tail(&desc->desc_node, &first->descs_list);
773 		xfer_size += len;
774 	}
775 
776 
777 	first->tx_dma_desc.flags = flags;
778 	first->xfer_size = xfer_size;
779 	first->direction = direction;
780 	ret = &first->tx_dma_desc;
781 
782 spin_unlock:
783 	spin_unlock_irqrestore(&atchan->lock, irqflags);
784 	return ret;
785 }
786 
787 static struct dma_async_tx_descriptor *
at_xdmac_prep_dma_cyclic(struct dma_chan * chan,dma_addr_t buf_addr,size_t buf_len,size_t period_len,enum dma_transfer_direction direction,unsigned long flags)788 at_xdmac_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr,
789 			 size_t buf_len, size_t period_len,
790 			 enum dma_transfer_direction direction,
791 			 unsigned long flags)
792 {
793 	struct at_xdmac_chan	*atchan = to_at_xdmac_chan(chan);
794 	struct at_xdmac_desc	*first = NULL, *prev = NULL;
795 	unsigned int		periods = buf_len / period_len;
796 	int			i;
797 	unsigned long		irqflags;
798 
799 	dev_dbg(chan2dev(chan), "%s: buf_addr=%pad, buf_len=%zd, period_len=%zd, dir=%s, flags=0x%lx\n",
800 		__func__, &buf_addr, buf_len, period_len,
801 		direction == DMA_MEM_TO_DEV ? "mem2per" : "per2mem", flags);
802 
803 	if (!is_slave_direction(direction)) {
804 		dev_err(chan2dev(chan), "invalid DMA direction\n");
805 		return NULL;
806 	}
807 
808 	if (test_and_set_bit(AT_XDMAC_CHAN_IS_CYCLIC, &atchan->status)) {
809 		dev_err(chan2dev(chan), "channel currently used\n");
810 		return NULL;
811 	}
812 
813 	if (at_xdmac_compute_chan_conf(chan, direction))
814 		return NULL;
815 
816 	for (i = 0; i < periods; i++) {
817 		struct at_xdmac_desc	*desc = NULL;
818 
819 		spin_lock_irqsave(&atchan->lock, irqflags);
820 		desc = at_xdmac_get_desc(atchan);
821 		if (!desc) {
822 			dev_err(chan2dev(chan), "can't get descriptor\n");
823 			if (first)
824 				list_splice_tail_init(&first->descs_list,
825 						      &atchan->free_descs_list);
826 			spin_unlock_irqrestore(&atchan->lock, irqflags);
827 			return NULL;
828 		}
829 		spin_unlock_irqrestore(&atchan->lock, irqflags);
830 		dev_dbg(chan2dev(chan),
831 			"%s: desc=0x%p, tx_dma_desc.phys=%pad\n",
832 			__func__, desc, &desc->tx_dma_desc.phys);
833 
834 		if (direction == DMA_DEV_TO_MEM) {
835 			desc->lld.mbr_sa = atchan->sconfig.src_addr;
836 			desc->lld.mbr_da = buf_addr + i * period_len;
837 		} else {
838 			desc->lld.mbr_sa = buf_addr + i * period_len;
839 			desc->lld.mbr_da = atchan->sconfig.dst_addr;
840 		}
841 		desc->lld.mbr_cfg = atchan->cfg;
842 		desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV1
843 			| AT_XDMAC_MBR_UBC_NDEN
844 			| AT_XDMAC_MBR_UBC_NSEN
845 			| period_len >> at_xdmac_get_dwidth(desc->lld.mbr_cfg);
846 
847 		dev_dbg(chan2dev(chan),
848 			 "%s: lld: mbr_sa=%pad, mbr_da=%pad, mbr_ubc=0x%08x\n",
849 			 __func__, &desc->lld.mbr_sa, &desc->lld.mbr_da, desc->lld.mbr_ubc);
850 
851 		/* Chain lld. */
852 		if (prev)
853 			at_xdmac_queue_desc(chan, prev, desc);
854 
855 		prev = desc;
856 		if (!first)
857 			first = desc;
858 
859 		dev_dbg(chan2dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n",
860 			 __func__, desc, first);
861 		list_add_tail(&desc->desc_node, &first->descs_list);
862 	}
863 
864 	at_xdmac_queue_desc(chan, prev, first);
865 	first->tx_dma_desc.flags = flags;
866 	first->xfer_size = buf_len;
867 	first->direction = direction;
868 
869 	return &first->tx_dma_desc;
870 }
871 
at_xdmac_align_width(struct dma_chan * chan,dma_addr_t addr)872 static inline u32 at_xdmac_align_width(struct dma_chan *chan, dma_addr_t addr)
873 {
874 	u32 width;
875 
876 	/*
877 	 * Check address alignment to select the greater data width we
878 	 * can use.
879 	 *
880 	 * Some XDMAC implementations don't provide dword transfer, in
881 	 * this case selecting dword has the same behavior as
882 	 * selecting word transfers.
883 	 */
884 	if (!(addr & 7)) {
885 		width = AT_XDMAC_CC_DWIDTH_DWORD;
886 		dev_dbg(chan2dev(chan), "%s: dwidth: double word\n", __func__);
887 	} else if (!(addr & 3)) {
888 		width = AT_XDMAC_CC_DWIDTH_WORD;
889 		dev_dbg(chan2dev(chan), "%s: dwidth: word\n", __func__);
890 	} else if (!(addr & 1)) {
891 		width = AT_XDMAC_CC_DWIDTH_HALFWORD;
892 		dev_dbg(chan2dev(chan), "%s: dwidth: half word\n", __func__);
893 	} else {
894 		width = AT_XDMAC_CC_DWIDTH_BYTE;
895 		dev_dbg(chan2dev(chan), "%s: dwidth: byte\n", __func__);
896 	}
897 
898 	return width;
899 }
900 
901 static struct at_xdmac_desc *
at_xdmac_interleaved_queue_desc(struct dma_chan * chan,struct at_xdmac_chan * atchan,struct at_xdmac_desc * prev,dma_addr_t src,dma_addr_t dst,struct dma_interleaved_template * xt,struct data_chunk * chunk)902 at_xdmac_interleaved_queue_desc(struct dma_chan *chan,
903 				struct at_xdmac_chan *atchan,
904 				struct at_xdmac_desc *prev,
905 				dma_addr_t src, dma_addr_t dst,
906 				struct dma_interleaved_template *xt,
907 				struct data_chunk *chunk)
908 {
909 	struct at_xdmac_desc	*desc;
910 	u32			dwidth;
911 	unsigned long		flags;
912 	size_t			ublen;
913 	/*
914 	 * WARNING: The channel configuration is set here since there is no
915 	 * dmaengine_slave_config call in this case. Moreover we don't know the
916 	 * direction, it involves we can't dynamically set the source and dest
917 	 * interface so we have to use the same one. Only interface 0 allows EBI
918 	 * access. Hopefully we can access DDR through both ports (at least on
919 	 * SAMA5D4x), so we can use the same interface for source and dest,
920 	 * that solves the fact we don't know the direction.
921 	 * ERRATA: Even if useless for memory transfers, the PERID has to not
922 	 * match the one of another channel. If not, it could lead to spurious
923 	 * flag status.
924 	 * For SAMA7G5x case, the SIF and DIF fields are no longer used.
925 	 * Thus, no need to have the SIF/DIF interfaces here.
926 	 * For SAMA5D4x and SAMA5D2x the SIF and DIF are already configured as
927 	 * zero.
928 	 */
929 	u32			chan_cc = AT_XDMAC_CC_PERID(0x7f)
930 					| AT_XDMAC_CC_MBSIZE_SIXTEEN
931 					| AT_XDMAC_CC_TYPE_MEM_TRAN;
932 
933 	dwidth = at_xdmac_align_width(chan, src | dst | chunk->size);
934 	if (chunk->size >= (AT_XDMAC_MBR_UBC_UBLEN_MAX << dwidth)) {
935 		dev_dbg(chan2dev(chan),
936 			"%s: chunk too big (%zu, max size %lu)...\n",
937 			__func__, chunk->size,
938 			AT_XDMAC_MBR_UBC_UBLEN_MAX << dwidth);
939 		return NULL;
940 	}
941 
942 	if (prev)
943 		dev_dbg(chan2dev(chan),
944 			"Adding items at the end of desc 0x%p\n", prev);
945 
946 	if (xt->src_inc) {
947 		if (xt->src_sgl)
948 			chan_cc |=  AT_XDMAC_CC_SAM_UBS_AM;
949 		else
950 			chan_cc |=  AT_XDMAC_CC_SAM_INCREMENTED_AM;
951 	}
952 
953 	if (xt->dst_inc) {
954 		if (xt->dst_sgl)
955 			chan_cc |=  AT_XDMAC_CC_DAM_UBS_AM;
956 		else
957 			chan_cc |=  AT_XDMAC_CC_DAM_INCREMENTED_AM;
958 	}
959 
960 	spin_lock_irqsave(&atchan->lock, flags);
961 	desc = at_xdmac_get_desc(atchan);
962 	spin_unlock_irqrestore(&atchan->lock, flags);
963 	if (!desc) {
964 		dev_err(chan2dev(chan), "can't get descriptor\n");
965 		return NULL;
966 	}
967 
968 	chan_cc |= AT_XDMAC_CC_DWIDTH(dwidth);
969 
970 	ublen = chunk->size >> dwidth;
971 
972 	desc->lld.mbr_sa = src;
973 	desc->lld.mbr_da = dst;
974 	desc->lld.mbr_sus = dmaengine_get_src_icg(xt, chunk);
975 	desc->lld.mbr_dus = dmaengine_get_dst_icg(xt, chunk);
976 
977 	desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV3
978 		| AT_XDMAC_MBR_UBC_NDEN
979 		| AT_XDMAC_MBR_UBC_NSEN
980 		| ublen;
981 	desc->lld.mbr_cfg = chan_cc;
982 
983 	dev_dbg(chan2dev(chan),
984 		"%s: lld: mbr_sa=%pad, mbr_da=%pad, mbr_ubc=0x%08x, mbr_cfg=0x%08x\n",
985 		__func__, &desc->lld.mbr_sa, &desc->lld.mbr_da,
986 		desc->lld.mbr_ubc, desc->lld.mbr_cfg);
987 
988 	/* Chain lld. */
989 	if (prev)
990 		at_xdmac_queue_desc(chan, prev, desc);
991 
992 	return desc;
993 }
994 
995 static struct dma_async_tx_descriptor *
at_xdmac_prep_interleaved(struct dma_chan * chan,struct dma_interleaved_template * xt,unsigned long flags)996 at_xdmac_prep_interleaved(struct dma_chan *chan,
997 			  struct dma_interleaved_template *xt,
998 			  unsigned long flags)
999 {
1000 	struct at_xdmac_chan	*atchan = to_at_xdmac_chan(chan);
1001 	struct at_xdmac_desc	*prev = NULL, *first = NULL;
1002 	dma_addr_t		dst_addr, src_addr;
1003 	size_t			src_skip = 0, dst_skip = 0, len = 0;
1004 	struct data_chunk	*chunk;
1005 	int			i;
1006 
1007 	if (!xt || !xt->numf || (xt->dir != DMA_MEM_TO_MEM))
1008 		return NULL;
1009 
1010 	/*
1011 	 * TODO: Handle the case where we have to repeat a chain of
1012 	 * descriptors...
1013 	 */
1014 	if ((xt->numf > 1) && (xt->frame_size > 1))
1015 		return NULL;
1016 
1017 	dev_dbg(chan2dev(chan), "%s: src=%pad, dest=%pad, numf=%zu, frame_size=%zu, flags=0x%lx\n",
1018 		__func__, &xt->src_start, &xt->dst_start,	xt->numf,
1019 		xt->frame_size, flags);
1020 
1021 	src_addr = xt->src_start;
1022 	dst_addr = xt->dst_start;
1023 
1024 	if (xt->numf > 1) {
1025 		first = at_xdmac_interleaved_queue_desc(chan, atchan,
1026 							NULL,
1027 							src_addr, dst_addr,
1028 							xt, xt->sgl);
1029 		if (!first)
1030 			return NULL;
1031 
1032 		/* Length of the block is (BLEN+1) microblocks. */
1033 		for (i = 0; i < xt->numf - 1; i++)
1034 			at_xdmac_increment_block_count(chan, first);
1035 
1036 		dev_dbg(chan2dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n",
1037 			__func__, first, first);
1038 		list_add_tail(&first->desc_node, &first->descs_list);
1039 	} else {
1040 		for (i = 0; i < xt->frame_size; i++) {
1041 			size_t src_icg = 0, dst_icg = 0;
1042 			struct at_xdmac_desc *desc;
1043 
1044 			chunk = xt->sgl + i;
1045 
1046 			dst_icg = dmaengine_get_dst_icg(xt, chunk);
1047 			src_icg = dmaengine_get_src_icg(xt, chunk);
1048 
1049 			src_skip = chunk->size + src_icg;
1050 			dst_skip = chunk->size + dst_icg;
1051 
1052 			dev_dbg(chan2dev(chan),
1053 				"%s: chunk size=%zu, src icg=%zu, dst icg=%zu\n",
1054 				__func__, chunk->size, src_icg, dst_icg);
1055 
1056 			desc = at_xdmac_interleaved_queue_desc(chan, atchan,
1057 							       prev,
1058 							       src_addr, dst_addr,
1059 							       xt, chunk);
1060 			if (!desc) {
1061 				if (first)
1062 					list_splice_tail_init(&first->descs_list,
1063 							      &atchan->free_descs_list);
1064 				return NULL;
1065 			}
1066 
1067 			if (!first)
1068 				first = desc;
1069 
1070 			dev_dbg(chan2dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n",
1071 				__func__, desc, first);
1072 			list_add_tail(&desc->desc_node, &first->descs_list);
1073 
1074 			if (xt->src_sgl)
1075 				src_addr += src_skip;
1076 
1077 			if (xt->dst_sgl)
1078 				dst_addr += dst_skip;
1079 
1080 			len += chunk->size;
1081 			prev = desc;
1082 		}
1083 	}
1084 
1085 	first->tx_dma_desc.cookie = -EBUSY;
1086 	first->tx_dma_desc.flags = flags;
1087 	first->xfer_size = len;
1088 
1089 	return &first->tx_dma_desc;
1090 }
1091 
1092 static struct dma_async_tx_descriptor *
at_xdmac_prep_dma_memcpy(struct dma_chan * chan,dma_addr_t dest,dma_addr_t src,size_t len,unsigned long flags)1093 at_xdmac_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
1094 			 size_t len, unsigned long flags)
1095 {
1096 	struct at_xdmac_chan	*atchan = to_at_xdmac_chan(chan);
1097 	struct at_xdmac_desc	*first = NULL, *prev = NULL;
1098 	size_t			remaining_size = len, xfer_size = 0, ublen;
1099 	dma_addr_t		src_addr = src, dst_addr = dest;
1100 	u32			dwidth;
1101 	/*
1102 	 * WARNING: We don't know the direction, it involves we can't
1103 	 * dynamically set the source and dest interface so we have to use the
1104 	 * same one. Only interface 0 allows EBI access. Hopefully we can
1105 	 * access DDR through both ports (at least on SAMA5D4x), so we can use
1106 	 * the same interface for source and dest, that solves the fact we
1107 	 * don't know the direction.
1108 	 * ERRATA: Even if useless for memory transfers, the PERID has to not
1109 	 * match the one of another channel. If not, it could lead to spurious
1110 	 * flag status.
1111 	 * For SAMA7G5x case, the SIF and DIF fields are no longer used.
1112 	 * Thus, no need to have the SIF/DIF interfaces here.
1113 	 * For SAMA5D4x and SAMA5D2x the SIF and DIF are already configured as
1114 	 * zero.
1115 	 */
1116 	u32			chan_cc = AT_XDMAC_CC_PERID(0x7f)
1117 					| AT_XDMAC_CC_DAM_INCREMENTED_AM
1118 					| AT_XDMAC_CC_SAM_INCREMENTED_AM
1119 					| AT_XDMAC_CC_MBSIZE_SIXTEEN
1120 					| AT_XDMAC_CC_TYPE_MEM_TRAN;
1121 	unsigned long		irqflags;
1122 
1123 	dev_dbg(chan2dev(chan), "%s: src=%pad, dest=%pad, len=%zd, flags=0x%lx\n",
1124 		__func__, &src, &dest, len, flags);
1125 
1126 	if (unlikely(!len))
1127 		return NULL;
1128 
1129 	dwidth = at_xdmac_align_width(chan, src_addr | dst_addr);
1130 
1131 	/* Prepare descriptors. */
1132 	while (remaining_size) {
1133 		struct at_xdmac_desc	*desc = NULL;
1134 
1135 		dev_dbg(chan2dev(chan), "%s: remaining_size=%zu\n", __func__, remaining_size);
1136 
1137 		spin_lock_irqsave(&atchan->lock, irqflags);
1138 		desc = at_xdmac_get_desc(atchan);
1139 		spin_unlock_irqrestore(&atchan->lock, irqflags);
1140 		if (!desc) {
1141 			dev_err(chan2dev(chan), "can't get descriptor\n");
1142 			if (first)
1143 				list_splice_tail_init(&first->descs_list,
1144 						      &atchan->free_descs_list);
1145 			return NULL;
1146 		}
1147 
1148 		/* Update src and dest addresses. */
1149 		src_addr += xfer_size;
1150 		dst_addr += xfer_size;
1151 
1152 		if (remaining_size >= AT_XDMAC_MBR_UBC_UBLEN_MAX << dwidth)
1153 			xfer_size = AT_XDMAC_MBR_UBC_UBLEN_MAX << dwidth;
1154 		else
1155 			xfer_size = remaining_size;
1156 
1157 		dev_dbg(chan2dev(chan), "%s: xfer_size=%zu\n", __func__, xfer_size);
1158 
1159 		/* Check remaining length and change data width if needed. */
1160 		dwidth = at_xdmac_align_width(chan,
1161 					      src_addr | dst_addr | xfer_size);
1162 		chan_cc &= ~AT_XDMAC_CC_DWIDTH_MASK;
1163 		chan_cc |= AT_XDMAC_CC_DWIDTH(dwidth);
1164 
1165 		ublen = xfer_size >> dwidth;
1166 		remaining_size -= xfer_size;
1167 
1168 		desc->lld.mbr_sa = src_addr;
1169 		desc->lld.mbr_da = dst_addr;
1170 		desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV2
1171 			| AT_XDMAC_MBR_UBC_NDEN
1172 			| AT_XDMAC_MBR_UBC_NSEN
1173 			| ublen;
1174 		desc->lld.mbr_cfg = chan_cc;
1175 
1176 		dev_dbg(chan2dev(chan),
1177 			 "%s: lld: mbr_sa=%pad, mbr_da=%pad, mbr_ubc=0x%08x, mbr_cfg=0x%08x\n",
1178 			 __func__, &desc->lld.mbr_sa, &desc->lld.mbr_da, desc->lld.mbr_ubc, desc->lld.mbr_cfg);
1179 
1180 		/* Chain lld. */
1181 		if (prev)
1182 			at_xdmac_queue_desc(chan, prev, desc);
1183 
1184 		prev = desc;
1185 		if (!first)
1186 			first = desc;
1187 
1188 		dev_dbg(chan2dev(chan), "%s: add desc 0x%p to descs_list 0x%p\n",
1189 			 __func__, desc, first);
1190 		list_add_tail(&desc->desc_node, &first->descs_list);
1191 	}
1192 
1193 	first->tx_dma_desc.flags = flags;
1194 	first->xfer_size = len;
1195 
1196 	return &first->tx_dma_desc;
1197 }
1198 
at_xdmac_memset_create_desc(struct dma_chan * chan,struct at_xdmac_chan * atchan,dma_addr_t dst_addr,size_t len,int value)1199 static struct at_xdmac_desc *at_xdmac_memset_create_desc(struct dma_chan *chan,
1200 							 struct at_xdmac_chan *atchan,
1201 							 dma_addr_t dst_addr,
1202 							 size_t len,
1203 							 int value)
1204 {
1205 	struct at_xdmac_desc	*desc;
1206 	unsigned long		flags;
1207 	size_t			ublen;
1208 	u32			dwidth;
1209 	/*
1210 	 * WARNING: The channel configuration is set here since there is no
1211 	 * dmaengine_slave_config call in this case. Moreover we don't know the
1212 	 * direction, it involves we can't dynamically set the source and dest
1213 	 * interface so we have to use the same one. Only interface 0 allows EBI
1214 	 * access. Hopefully we can access DDR through both ports (at least on
1215 	 * SAMA5D4x), so we can use the same interface for source and dest,
1216 	 * that solves the fact we don't know the direction.
1217 	 * ERRATA: Even if useless for memory transfers, the PERID has to not
1218 	 * match the one of another channel. If not, it could lead to spurious
1219 	 * flag status.
1220 	 * For SAMA7G5x case, the SIF and DIF fields are no longer used.
1221 	 * Thus, no need to have the SIF/DIF interfaces here.
1222 	 * For SAMA5D4x and SAMA5D2x the SIF and DIF are already configured as
1223 	 * zero.
1224 	 */
1225 	u32			chan_cc = AT_XDMAC_CC_PERID(0x7f)
1226 					| AT_XDMAC_CC_DAM_UBS_AM
1227 					| AT_XDMAC_CC_SAM_INCREMENTED_AM
1228 					| AT_XDMAC_CC_MBSIZE_SIXTEEN
1229 					| AT_XDMAC_CC_MEMSET_HW_MODE
1230 					| AT_XDMAC_CC_TYPE_MEM_TRAN;
1231 
1232 	dwidth = at_xdmac_align_width(chan, dst_addr);
1233 
1234 	if (len >= (AT_XDMAC_MBR_UBC_UBLEN_MAX << dwidth)) {
1235 		dev_err(chan2dev(chan),
1236 			"%s: Transfer too large, aborting...\n",
1237 			__func__);
1238 		return NULL;
1239 	}
1240 
1241 	spin_lock_irqsave(&atchan->lock, flags);
1242 	desc = at_xdmac_get_desc(atchan);
1243 	spin_unlock_irqrestore(&atchan->lock, flags);
1244 	if (!desc) {
1245 		dev_err(chan2dev(chan), "can't get descriptor\n");
1246 		return NULL;
1247 	}
1248 
1249 	chan_cc |= AT_XDMAC_CC_DWIDTH(dwidth);
1250 
1251 	ublen = len >> dwidth;
1252 
1253 	desc->lld.mbr_da = dst_addr;
1254 	desc->lld.mbr_ds = value;
1255 	desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV3
1256 		| AT_XDMAC_MBR_UBC_NDEN
1257 		| AT_XDMAC_MBR_UBC_NSEN
1258 		| ublen;
1259 	desc->lld.mbr_cfg = chan_cc;
1260 
1261 	dev_dbg(chan2dev(chan),
1262 		"%s: lld: mbr_da=%pad, mbr_ds=0x%08x, mbr_ubc=0x%08x, mbr_cfg=0x%08x\n",
1263 		__func__, &desc->lld.mbr_da, desc->lld.mbr_ds, desc->lld.mbr_ubc,
1264 		desc->lld.mbr_cfg);
1265 
1266 	return desc;
1267 }
1268 
1269 static struct dma_async_tx_descriptor *
at_xdmac_prep_dma_memset(struct dma_chan * chan,dma_addr_t dest,int value,size_t len,unsigned long flags)1270 at_xdmac_prep_dma_memset(struct dma_chan *chan, dma_addr_t dest, int value,
1271 			 size_t len, unsigned long flags)
1272 {
1273 	struct at_xdmac_chan	*atchan = to_at_xdmac_chan(chan);
1274 	struct at_xdmac_desc	*desc;
1275 
1276 	dev_dbg(chan2dev(chan), "%s: dest=%pad, len=%zu, pattern=0x%x, flags=0x%lx\n",
1277 		__func__, &dest, len, value, flags);
1278 
1279 	if (unlikely(!len))
1280 		return NULL;
1281 
1282 	desc = at_xdmac_memset_create_desc(chan, atchan, dest, len, value);
1283 	list_add_tail(&desc->desc_node, &desc->descs_list);
1284 
1285 	desc->tx_dma_desc.cookie = -EBUSY;
1286 	desc->tx_dma_desc.flags = flags;
1287 	desc->xfer_size = len;
1288 
1289 	return &desc->tx_dma_desc;
1290 }
1291 
1292 static struct dma_async_tx_descriptor *
at_xdmac_prep_dma_memset_sg(struct dma_chan * chan,struct scatterlist * sgl,unsigned int sg_len,int value,unsigned long flags)1293 at_xdmac_prep_dma_memset_sg(struct dma_chan *chan, struct scatterlist *sgl,
1294 			    unsigned int sg_len, int value,
1295 			    unsigned long flags)
1296 {
1297 	struct at_xdmac_chan	*atchan = to_at_xdmac_chan(chan);
1298 	struct at_xdmac_desc	*desc, *pdesc = NULL,
1299 				*ppdesc = NULL, *first = NULL;
1300 	struct scatterlist	*sg, *psg = NULL, *ppsg = NULL;
1301 	size_t			stride = 0, pstride = 0, len = 0;
1302 	int			i;
1303 
1304 	if (!sgl)
1305 		return NULL;
1306 
1307 	dev_dbg(chan2dev(chan), "%s: sg_len=%d, value=0x%x, flags=0x%lx\n",
1308 		__func__, sg_len, value, flags);
1309 
1310 	/* Prepare descriptors. */
1311 	for_each_sg(sgl, sg, sg_len, i) {
1312 		dev_dbg(chan2dev(chan), "%s: dest=%pad, len=%d, pattern=0x%x, flags=0x%lx\n",
1313 			__func__, &sg_dma_address(sg), sg_dma_len(sg),
1314 			value, flags);
1315 		desc = at_xdmac_memset_create_desc(chan, atchan,
1316 						   sg_dma_address(sg),
1317 						   sg_dma_len(sg),
1318 						   value);
1319 		if (!desc && first)
1320 			list_splice_tail_init(&first->descs_list,
1321 					      &atchan->free_descs_list);
1322 
1323 		if (!first)
1324 			first = desc;
1325 
1326 		/* Update our strides */
1327 		pstride = stride;
1328 		if (psg)
1329 			stride = sg_dma_address(sg) -
1330 				(sg_dma_address(psg) + sg_dma_len(psg));
1331 
1332 		/*
1333 		 * The scatterlist API gives us only the address and
1334 		 * length of each elements.
1335 		 *
1336 		 * Unfortunately, we don't have the stride, which we
1337 		 * will need to compute.
1338 		 *
1339 		 * That make us end up in a situation like this one:
1340 		 *    len    stride    len    stride    len
1341 		 * +-------+        +-------+        +-------+
1342 		 * |  N-2  |        |  N-1  |        |   N   |
1343 		 * +-------+        +-------+        +-------+
1344 		 *
1345 		 * We need all these three elements (N-2, N-1 and N)
1346 		 * to actually take the decision on whether we need to
1347 		 * queue N-1 or reuse N-2.
1348 		 *
1349 		 * We will only consider N if it is the last element.
1350 		 */
1351 		if (ppdesc && pdesc) {
1352 			if ((stride == pstride) &&
1353 			    (sg_dma_len(ppsg) == sg_dma_len(psg))) {
1354 				dev_dbg(chan2dev(chan),
1355 					"%s: desc 0x%p can be merged with desc 0x%p\n",
1356 					__func__, pdesc, ppdesc);
1357 
1358 				/*
1359 				 * Increment the block count of the
1360 				 * N-2 descriptor
1361 				 */
1362 				at_xdmac_increment_block_count(chan, ppdesc);
1363 				ppdesc->lld.mbr_dus = stride;
1364 
1365 				/*
1366 				 * Put back the N-1 descriptor in the
1367 				 * free descriptor list
1368 				 */
1369 				list_add_tail(&pdesc->desc_node,
1370 					      &atchan->free_descs_list);
1371 
1372 				/*
1373 				 * Make our N-1 descriptor pointer
1374 				 * point to the N-2 since they were
1375 				 * actually merged.
1376 				 */
1377 				pdesc = ppdesc;
1378 
1379 			/*
1380 			 * Rule out the case where we don't have
1381 			 * pstride computed yet (our second sg
1382 			 * element)
1383 			 *
1384 			 * We also want to catch the case where there
1385 			 * would be a negative stride,
1386 			 */
1387 			} else if (pstride ||
1388 				   sg_dma_address(sg) < sg_dma_address(psg)) {
1389 				/*
1390 				 * Queue the N-1 descriptor after the
1391 				 * N-2
1392 				 */
1393 				at_xdmac_queue_desc(chan, ppdesc, pdesc);
1394 
1395 				/*
1396 				 * Add the N-1 descriptor to the list
1397 				 * of the descriptors used for this
1398 				 * transfer
1399 				 */
1400 				list_add_tail(&desc->desc_node,
1401 					      &first->descs_list);
1402 				dev_dbg(chan2dev(chan),
1403 					"%s: add desc 0x%p to descs_list 0x%p\n",
1404 					__func__, desc, first);
1405 			}
1406 		}
1407 
1408 		/*
1409 		 * If we are the last element, just see if we have the
1410 		 * same size than the previous element.
1411 		 *
1412 		 * If so, we can merge it with the previous descriptor
1413 		 * since we don't care about the stride anymore.
1414 		 */
1415 		if ((i == (sg_len - 1)) &&
1416 		    sg_dma_len(psg) == sg_dma_len(sg)) {
1417 			dev_dbg(chan2dev(chan),
1418 				"%s: desc 0x%p can be merged with desc 0x%p\n",
1419 				__func__, desc, pdesc);
1420 
1421 			/*
1422 			 * Increment the block count of the N-1
1423 			 * descriptor
1424 			 */
1425 			at_xdmac_increment_block_count(chan, pdesc);
1426 			pdesc->lld.mbr_dus = stride;
1427 
1428 			/*
1429 			 * Put back the N descriptor in the free
1430 			 * descriptor list
1431 			 */
1432 			list_add_tail(&desc->desc_node,
1433 				      &atchan->free_descs_list);
1434 		}
1435 
1436 		/* Update our descriptors */
1437 		ppdesc = pdesc;
1438 		pdesc = desc;
1439 
1440 		/* Update our scatter pointers */
1441 		ppsg = psg;
1442 		psg = sg;
1443 
1444 		len += sg_dma_len(sg);
1445 	}
1446 
1447 	first->tx_dma_desc.cookie = -EBUSY;
1448 	first->tx_dma_desc.flags = flags;
1449 	first->xfer_size = len;
1450 
1451 	return &first->tx_dma_desc;
1452 }
1453 
1454 static enum dma_status
at_xdmac_tx_status(struct dma_chan * chan,dma_cookie_t cookie,struct dma_tx_state * txstate)1455 at_xdmac_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
1456 		struct dma_tx_state *txstate)
1457 {
1458 	struct at_xdmac_chan	*atchan = to_at_xdmac_chan(chan);
1459 	struct at_xdmac		*atxdmac = to_at_xdmac(atchan->chan.device);
1460 	struct at_xdmac_desc	*desc, *_desc, *iter;
1461 	struct list_head	*descs_list;
1462 	enum dma_status		ret;
1463 	int			residue, retry;
1464 	u32			cur_nda, check_nda, cur_ubc, mask, value;
1465 	u8			dwidth = 0;
1466 	unsigned long		flags;
1467 	bool			initd;
1468 
1469 	ret = dma_cookie_status(chan, cookie, txstate);
1470 	if (ret == DMA_COMPLETE)
1471 		return ret;
1472 
1473 	if (!txstate)
1474 		return ret;
1475 
1476 	spin_lock_irqsave(&atchan->lock, flags);
1477 
1478 	desc = list_first_entry(&atchan->xfers_list, struct at_xdmac_desc, xfer_node);
1479 
1480 	/*
1481 	 * If the transfer has not been started yet, don't need to compute the
1482 	 * residue, it's the transfer length.
1483 	 */
1484 	if (!desc->active_xfer) {
1485 		dma_set_residue(txstate, desc->xfer_size);
1486 		goto spin_unlock;
1487 	}
1488 
1489 	residue = desc->xfer_size;
1490 	/*
1491 	 * Flush FIFO: only relevant when the transfer is source peripheral
1492 	 * synchronized. Flush is needed before reading CUBC because data in
1493 	 * the FIFO are not reported by CUBC. Reporting a residue of the
1494 	 * transfer length while we have data in FIFO can cause issue.
1495 	 * Usecase: atmel USART has a timeout which means I have received
1496 	 * characters but there is no more character received for a while. On
1497 	 * timeout, it requests the residue. If the data are in the DMA FIFO,
1498 	 * we will return a residue of the transfer length. It means no data
1499 	 * received. If an application is waiting for these data, it will hang
1500 	 * since we won't have another USART timeout without receiving new
1501 	 * data.
1502 	 */
1503 	mask = AT_XDMAC_CC_TYPE | AT_XDMAC_CC_DSYNC;
1504 	value = AT_XDMAC_CC_TYPE_PER_TRAN | AT_XDMAC_CC_DSYNC_PER2MEM;
1505 	if ((desc->lld.mbr_cfg & mask) == value) {
1506 		at_xdmac_write(atxdmac, atxdmac->layout->gswf, atchan->mask);
1507 		while (!(at_xdmac_chan_read(atchan, AT_XDMAC_CIS) & AT_XDMAC_CIS_FIS))
1508 			cpu_relax();
1509 	}
1510 
1511 	/*
1512 	 * The easiest way to compute the residue should be to pause the DMA
1513 	 * but doing this can lead to miss some data as some devices don't
1514 	 * have FIFO.
1515 	 * We need to read several registers because:
1516 	 * - DMA is running therefore a descriptor change is possible while
1517 	 * reading these registers
1518 	 * - When the block transfer is done, the value of the CUBC register
1519 	 * is set to its initial value until the fetch of the next descriptor.
1520 	 * This value will corrupt the residue calculation so we have to skip
1521 	 * it.
1522 	 *
1523 	 * INITD --------                    ------------
1524 	 *              |____________________|
1525 	 *       _______________________  _______________
1526 	 * NDA       @desc2             \/   @desc3
1527 	 *       _______________________/\_______________
1528 	 *       __________  ___________  _______________
1529 	 * CUBC       0    \/ MAX desc1 \/  MAX desc2
1530 	 *       __________/\___________/\_______________
1531 	 *
1532 	 * Since descriptors are aligned on 64 bits, we can assume that
1533 	 * the update of NDA and CUBC is atomic.
1534 	 * Memory barriers are used to ensure the read order of the registers.
1535 	 * A max number of retries is set because unlikely it could never ends.
1536 	 */
1537 	for (retry = 0; retry < AT_XDMAC_RESIDUE_MAX_RETRIES; retry++) {
1538 		check_nda = at_xdmac_chan_read(atchan, AT_XDMAC_CNDA) & 0xfffffffc;
1539 		rmb();
1540 		cur_ubc = at_xdmac_chan_read(atchan, AT_XDMAC_CUBC);
1541 		rmb();
1542 		initd = !!(at_xdmac_chan_read(atchan, AT_XDMAC_CC) & AT_XDMAC_CC_INITD);
1543 		rmb();
1544 		cur_nda = at_xdmac_chan_read(atchan, AT_XDMAC_CNDA) & 0xfffffffc;
1545 		rmb();
1546 
1547 		if ((check_nda == cur_nda) && initd)
1548 			break;
1549 	}
1550 
1551 	if (unlikely(retry >= AT_XDMAC_RESIDUE_MAX_RETRIES)) {
1552 		ret = DMA_ERROR;
1553 		goto spin_unlock;
1554 	}
1555 
1556 	/*
1557 	 * Flush FIFO: only relevant when the transfer is source peripheral
1558 	 * synchronized. Another flush is needed here because CUBC is updated
1559 	 * when the controller sends the data write command. It can lead to
1560 	 * report data that are not written in the memory or the device. The
1561 	 * FIFO flush ensures that data are really written.
1562 	 */
1563 	if ((desc->lld.mbr_cfg & mask) == value) {
1564 		at_xdmac_write(atxdmac, atxdmac->layout->gswf, atchan->mask);
1565 		while (!(at_xdmac_chan_read(atchan, AT_XDMAC_CIS) & AT_XDMAC_CIS_FIS))
1566 			cpu_relax();
1567 	}
1568 
1569 	/*
1570 	 * Remove size of all microblocks already transferred and the current
1571 	 * one. Then add the remaining size to transfer of the current
1572 	 * microblock.
1573 	 */
1574 	descs_list = &desc->descs_list;
1575 	list_for_each_entry_safe(iter, _desc, descs_list, desc_node) {
1576 		dwidth = at_xdmac_get_dwidth(iter->lld.mbr_cfg);
1577 		residue -= (iter->lld.mbr_ubc & 0xffffff) << dwidth;
1578 		if ((iter->lld.mbr_nda & 0xfffffffc) == cur_nda) {
1579 			desc = iter;
1580 			break;
1581 		}
1582 	}
1583 	residue += cur_ubc << dwidth;
1584 
1585 	dma_set_residue(txstate, residue);
1586 
1587 	dev_dbg(chan2dev(chan),
1588 		 "%s: desc=0x%p, tx_dma_desc.phys=%pad, tx_status=%d, cookie=%d, residue=%d\n",
1589 		 __func__, desc, &desc->tx_dma_desc.phys, ret, cookie, residue);
1590 
1591 spin_unlock:
1592 	spin_unlock_irqrestore(&atchan->lock, flags);
1593 	return ret;
1594 }
1595 
at_xdmac_advance_work(struct at_xdmac_chan * atchan)1596 static void at_xdmac_advance_work(struct at_xdmac_chan *atchan)
1597 {
1598 	struct at_xdmac_desc	*desc;
1599 
1600 	/*
1601 	 * If channel is enabled, do nothing, advance_work will be triggered
1602 	 * after the interruption.
1603 	 */
1604 	if (!at_xdmac_chan_is_enabled(atchan) && !list_empty(&atchan->xfers_list)) {
1605 		desc = list_first_entry(&atchan->xfers_list,
1606 					struct at_xdmac_desc,
1607 					xfer_node);
1608 		dev_vdbg(chan2dev(&atchan->chan), "%s: desc 0x%p\n", __func__, desc);
1609 		if (!desc->active_xfer)
1610 			at_xdmac_start_xfer(atchan, desc);
1611 	}
1612 }
1613 
at_xdmac_handle_cyclic(struct at_xdmac_chan * atchan)1614 static void at_xdmac_handle_cyclic(struct at_xdmac_chan *atchan)
1615 {
1616 	struct at_xdmac_desc		*desc;
1617 	struct dma_async_tx_descriptor	*txd;
1618 
1619 	spin_lock_irq(&atchan->lock);
1620 	if (list_empty(&atchan->xfers_list)) {
1621 		spin_unlock_irq(&atchan->lock);
1622 		return;
1623 	}
1624 	desc = list_first_entry(&atchan->xfers_list, struct at_xdmac_desc,
1625 				xfer_node);
1626 	spin_unlock_irq(&atchan->lock);
1627 	txd = &desc->tx_dma_desc;
1628 	if (txd->flags & DMA_PREP_INTERRUPT)
1629 		dmaengine_desc_get_callback_invoke(txd, NULL);
1630 }
1631 
at_xdmac_handle_error(struct at_xdmac_chan * atchan)1632 static void at_xdmac_handle_error(struct at_xdmac_chan *atchan)
1633 {
1634 	struct at_xdmac		*atxdmac = to_at_xdmac(atchan->chan.device);
1635 	struct at_xdmac_desc	*bad_desc;
1636 
1637 	/*
1638 	 * The descriptor currently at the head of the active list is
1639 	 * broken. Since we don't have any way to report errors, we'll
1640 	 * just have to scream loudly and try to continue with other
1641 	 * descriptors queued (if any).
1642 	 */
1643 	if (atchan->irq_status & AT_XDMAC_CIS_RBEIS)
1644 		dev_err(chan2dev(&atchan->chan), "read bus error!!!");
1645 	if (atchan->irq_status & AT_XDMAC_CIS_WBEIS)
1646 		dev_err(chan2dev(&atchan->chan), "write bus error!!!");
1647 	if (atchan->irq_status & AT_XDMAC_CIS_ROIS)
1648 		dev_err(chan2dev(&atchan->chan), "request overflow error!!!");
1649 
1650 	spin_lock_irq(&atchan->lock);
1651 
1652 	/* Channel must be disabled first as it's not done automatically */
1653 	at_xdmac_write(atxdmac, AT_XDMAC_GD, atchan->mask);
1654 	while (at_xdmac_read(atxdmac, AT_XDMAC_GS) & atchan->mask)
1655 		cpu_relax();
1656 
1657 	bad_desc = list_first_entry(&atchan->xfers_list,
1658 				    struct at_xdmac_desc,
1659 				    xfer_node);
1660 
1661 	spin_unlock_irq(&atchan->lock);
1662 
1663 	/* Print bad descriptor's details if needed */
1664 	dev_dbg(chan2dev(&atchan->chan),
1665 		"%s: lld: mbr_sa=%pad, mbr_da=%pad, mbr_ubc=0x%08x\n",
1666 		__func__, &bad_desc->lld.mbr_sa, &bad_desc->lld.mbr_da,
1667 		bad_desc->lld.mbr_ubc);
1668 
1669 	/* Then continue with usual descriptor management */
1670 }
1671 
at_xdmac_tasklet(struct tasklet_struct * t)1672 static void at_xdmac_tasklet(struct tasklet_struct *t)
1673 {
1674 	struct at_xdmac_chan	*atchan = from_tasklet(atchan, t, tasklet);
1675 	struct at_xdmac_desc	*desc;
1676 	u32			error_mask;
1677 
1678 	dev_dbg(chan2dev(&atchan->chan), "%s: status=0x%08x\n",
1679 		__func__, atchan->irq_status);
1680 
1681 	error_mask = AT_XDMAC_CIS_RBEIS
1682 		     | AT_XDMAC_CIS_WBEIS
1683 		     | AT_XDMAC_CIS_ROIS;
1684 
1685 	if (at_xdmac_chan_is_cyclic(atchan)) {
1686 		at_xdmac_handle_cyclic(atchan);
1687 	} else if ((atchan->irq_status & AT_XDMAC_CIS_LIS)
1688 		   || (atchan->irq_status & error_mask)) {
1689 		struct dma_async_tx_descriptor  *txd;
1690 
1691 		if (atchan->irq_status & error_mask)
1692 			at_xdmac_handle_error(atchan);
1693 
1694 		spin_lock_irq(&atchan->lock);
1695 		desc = list_first_entry(&atchan->xfers_list,
1696 					struct at_xdmac_desc,
1697 					xfer_node);
1698 		dev_vdbg(chan2dev(&atchan->chan), "%s: desc 0x%p\n", __func__, desc);
1699 		if (!desc->active_xfer) {
1700 			dev_err(chan2dev(&atchan->chan), "Xfer not active: exiting");
1701 			spin_unlock_irq(&atchan->lock);
1702 			return;
1703 		}
1704 
1705 		txd = &desc->tx_dma_desc;
1706 		dma_cookie_complete(txd);
1707 		/* Remove the transfer from the transfer list. */
1708 		list_del(&desc->xfer_node);
1709 		spin_unlock_irq(&atchan->lock);
1710 
1711 		if (txd->flags & DMA_PREP_INTERRUPT)
1712 			dmaengine_desc_get_callback_invoke(txd, NULL);
1713 
1714 		dma_run_dependencies(txd);
1715 
1716 		spin_lock_irq(&atchan->lock);
1717 		/* Move the xfer descriptors into the free descriptors list. */
1718 		list_splice_tail_init(&desc->descs_list,
1719 				      &atchan->free_descs_list);
1720 		at_xdmac_advance_work(atchan);
1721 		spin_unlock_irq(&atchan->lock);
1722 	}
1723 }
1724 
at_xdmac_interrupt(int irq,void * dev_id)1725 static irqreturn_t at_xdmac_interrupt(int irq, void *dev_id)
1726 {
1727 	struct at_xdmac		*atxdmac = (struct at_xdmac *)dev_id;
1728 	struct at_xdmac_chan	*atchan;
1729 	u32			imr, status, pending;
1730 	u32			chan_imr, chan_status;
1731 	int			i, ret = IRQ_NONE;
1732 
1733 	do {
1734 		imr = at_xdmac_read(atxdmac, AT_XDMAC_GIM);
1735 		status = at_xdmac_read(atxdmac, AT_XDMAC_GIS);
1736 		pending = status & imr;
1737 
1738 		dev_vdbg(atxdmac->dma.dev,
1739 			 "%s: status=0x%08x, imr=0x%08x, pending=0x%08x\n",
1740 			 __func__, status, imr, pending);
1741 
1742 		if (!pending)
1743 			break;
1744 
1745 		/* We have to find which channel has generated the interrupt. */
1746 		for (i = 0; i < atxdmac->dma.chancnt; i++) {
1747 			if (!((1 << i) & pending))
1748 				continue;
1749 
1750 			atchan = &atxdmac->chan[i];
1751 			chan_imr = at_xdmac_chan_read(atchan, AT_XDMAC_CIM);
1752 			chan_status = at_xdmac_chan_read(atchan, AT_XDMAC_CIS);
1753 			atchan->irq_status = chan_status & chan_imr;
1754 			dev_vdbg(atxdmac->dma.dev,
1755 				 "%s: chan%d: imr=0x%x, status=0x%x\n",
1756 				 __func__, i, chan_imr, chan_status);
1757 			dev_vdbg(chan2dev(&atchan->chan),
1758 				 "%s: CC=0x%08x CNDA=0x%08x, CNDC=0x%08x, CSA=0x%08x, CDA=0x%08x, CUBC=0x%08x\n",
1759 				 __func__,
1760 				 at_xdmac_chan_read(atchan, AT_XDMAC_CC),
1761 				 at_xdmac_chan_read(atchan, AT_XDMAC_CNDA),
1762 				 at_xdmac_chan_read(atchan, AT_XDMAC_CNDC),
1763 				 at_xdmac_chan_read(atchan, AT_XDMAC_CSA),
1764 				 at_xdmac_chan_read(atchan, AT_XDMAC_CDA),
1765 				 at_xdmac_chan_read(atchan, AT_XDMAC_CUBC));
1766 
1767 			if (atchan->irq_status & (AT_XDMAC_CIS_RBEIS | AT_XDMAC_CIS_WBEIS))
1768 				at_xdmac_write(atxdmac, AT_XDMAC_GD, atchan->mask);
1769 
1770 			tasklet_schedule(&atchan->tasklet);
1771 			ret = IRQ_HANDLED;
1772 		}
1773 
1774 	} while (pending);
1775 
1776 	return ret;
1777 }
1778 
at_xdmac_issue_pending(struct dma_chan * chan)1779 static void at_xdmac_issue_pending(struct dma_chan *chan)
1780 {
1781 	struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
1782 	unsigned long flags;
1783 
1784 	dev_dbg(chan2dev(&atchan->chan), "%s\n", __func__);
1785 
1786 	spin_lock_irqsave(&atchan->lock, flags);
1787 	at_xdmac_advance_work(atchan);
1788 	spin_unlock_irqrestore(&atchan->lock, flags);
1789 
1790 	return;
1791 }
1792 
at_xdmac_device_config(struct dma_chan * chan,struct dma_slave_config * config)1793 static int at_xdmac_device_config(struct dma_chan *chan,
1794 				  struct dma_slave_config *config)
1795 {
1796 	struct at_xdmac_chan	*atchan = to_at_xdmac_chan(chan);
1797 	int ret;
1798 	unsigned long		flags;
1799 
1800 	dev_dbg(chan2dev(chan), "%s\n", __func__);
1801 
1802 	spin_lock_irqsave(&atchan->lock, flags);
1803 	ret = at_xdmac_set_slave_config(chan, config);
1804 	spin_unlock_irqrestore(&atchan->lock, flags);
1805 
1806 	return ret;
1807 }
1808 
at_xdmac_device_pause(struct dma_chan * chan)1809 static int at_xdmac_device_pause(struct dma_chan *chan)
1810 {
1811 	struct at_xdmac_chan	*atchan = to_at_xdmac_chan(chan);
1812 	struct at_xdmac		*atxdmac = to_at_xdmac(atchan->chan.device);
1813 	unsigned long		flags;
1814 
1815 	dev_dbg(chan2dev(chan), "%s\n", __func__);
1816 
1817 	if (test_and_set_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status))
1818 		return 0;
1819 
1820 	spin_lock_irqsave(&atchan->lock, flags);
1821 	at_xdmac_write(atxdmac, atxdmac->layout->grws, atchan->mask);
1822 	while (at_xdmac_chan_read(atchan, AT_XDMAC_CC)
1823 	       & (AT_XDMAC_CC_WRIP | AT_XDMAC_CC_RDIP))
1824 		cpu_relax();
1825 	spin_unlock_irqrestore(&atchan->lock, flags);
1826 
1827 	return 0;
1828 }
1829 
at_xdmac_device_resume(struct dma_chan * chan)1830 static int at_xdmac_device_resume(struct dma_chan *chan)
1831 {
1832 	struct at_xdmac_chan	*atchan = to_at_xdmac_chan(chan);
1833 	struct at_xdmac		*atxdmac = to_at_xdmac(atchan->chan.device);
1834 	unsigned long		flags;
1835 
1836 	dev_dbg(chan2dev(chan), "%s\n", __func__);
1837 
1838 	spin_lock_irqsave(&atchan->lock, flags);
1839 	if (!at_xdmac_chan_is_paused(atchan)) {
1840 		spin_unlock_irqrestore(&atchan->lock, flags);
1841 		return 0;
1842 	}
1843 
1844 	at_xdmac_write(atxdmac, atxdmac->layout->grwr, atchan->mask);
1845 	clear_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status);
1846 	spin_unlock_irqrestore(&atchan->lock, flags);
1847 
1848 	return 0;
1849 }
1850 
at_xdmac_device_terminate_all(struct dma_chan * chan)1851 static int at_xdmac_device_terminate_all(struct dma_chan *chan)
1852 {
1853 	struct at_xdmac_desc	*desc, *_desc;
1854 	struct at_xdmac_chan	*atchan = to_at_xdmac_chan(chan);
1855 	struct at_xdmac		*atxdmac = to_at_xdmac(atchan->chan.device);
1856 	unsigned long		flags;
1857 
1858 	dev_dbg(chan2dev(chan), "%s\n", __func__);
1859 
1860 	spin_lock_irqsave(&atchan->lock, flags);
1861 	at_xdmac_write(atxdmac, AT_XDMAC_GD, atchan->mask);
1862 	while (at_xdmac_read(atxdmac, AT_XDMAC_GS) & atchan->mask)
1863 		cpu_relax();
1864 
1865 	/* Cancel all pending transfers. */
1866 	list_for_each_entry_safe(desc, _desc, &atchan->xfers_list, xfer_node) {
1867 		list_del(&desc->xfer_node);
1868 		list_splice_tail_init(&desc->descs_list,
1869 				      &atchan->free_descs_list);
1870 	}
1871 
1872 	clear_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status);
1873 	clear_bit(AT_XDMAC_CHAN_IS_CYCLIC, &atchan->status);
1874 	spin_unlock_irqrestore(&atchan->lock, flags);
1875 
1876 	return 0;
1877 }
1878 
at_xdmac_alloc_chan_resources(struct dma_chan * chan)1879 static int at_xdmac_alloc_chan_resources(struct dma_chan *chan)
1880 {
1881 	struct at_xdmac_chan	*atchan = to_at_xdmac_chan(chan);
1882 	struct at_xdmac_desc	*desc;
1883 	int			i;
1884 
1885 	if (at_xdmac_chan_is_enabled(atchan)) {
1886 		dev_err(chan2dev(chan),
1887 			"can't allocate channel resources (channel enabled)\n");
1888 		return -EIO;
1889 	}
1890 
1891 	if (!list_empty(&atchan->free_descs_list)) {
1892 		dev_err(chan2dev(chan),
1893 			"can't allocate channel resources (channel not free from a previous use)\n");
1894 		return -EIO;
1895 	}
1896 
1897 	for (i = 0; i < init_nr_desc_per_channel; i++) {
1898 		desc = at_xdmac_alloc_desc(chan, GFP_KERNEL);
1899 		if (!desc) {
1900 			if (i == 0) {
1901 				dev_warn(chan2dev(chan),
1902 					 "can't allocate any descriptors\n");
1903 				return -EIO;
1904 			}
1905 			dev_warn(chan2dev(chan),
1906 				"only %d descriptors have been allocated\n", i);
1907 			break;
1908 		}
1909 		list_add_tail(&desc->desc_node, &atchan->free_descs_list);
1910 	}
1911 
1912 	dma_cookie_init(chan);
1913 
1914 	dev_dbg(chan2dev(chan), "%s: allocated %d descriptors\n", __func__, i);
1915 
1916 	return i;
1917 }
1918 
at_xdmac_free_chan_resources(struct dma_chan * chan)1919 static void at_xdmac_free_chan_resources(struct dma_chan *chan)
1920 {
1921 	struct at_xdmac_chan	*atchan = to_at_xdmac_chan(chan);
1922 	struct at_xdmac		*atxdmac = to_at_xdmac(chan->device);
1923 	struct at_xdmac_desc	*desc, *_desc;
1924 
1925 	list_for_each_entry_safe(desc, _desc, &atchan->free_descs_list, desc_node) {
1926 		dev_dbg(chan2dev(chan), "%s: freeing descriptor %p\n", __func__, desc);
1927 		list_del(&desc->desc_node);
1928 		dma_pool_free(atxdmac->at_xdmac_desc_pool, desc, desc->tx_dma_desc.phys);
1929 	}
1930 
1931 	return;
1932 }
1933 
at_xdmac_axi_config(struct platform_device * pdev)1934 static void at_xdmac_axi_config(struct platform_device *pdev)
1935 {
1936 	struct at_xdmac	*atxdmac = (struct at_xdmac *)platform_get_drvdata(pdev);
1937 	bool dev_m2m = false;
1938 	u32 dma_requests;
1939 
1940 	if (!atxdmac->layout->axi_config)
1941 		return; /* Not supported */
1942 
1943 	if (!of_property_read_u32(pdev->dev.of_node, "dma-requests",
1944 				  &dma_requests)) {
1945 		dev_info(&pdev->dev, "controller in mem2mem mode.\n");
1946 		dev_m2m = true;
1947 	}
1948 
1949 	if (dev_m2m) {
1950 		at_xdmac_write(atxdmac, AT_XDMAC_GCFG, AT_XDMAC_GCFG_M2M);
1951 		at_xdmac_write(atxdmac, AT_XDMAC_GWAC, AT_XDMAC_GWAC_M2M);
1952 	} else {
1953 		at_xdmac_write(atxdmac, AT_XDMAC_GCFG, AT_XDMAC_GCFG_P2M);
1954 		at_xdmac_write(atxdmac, AT_XDMAC_GWAC, AT_XDMAC_GWAC_P2M);
1955 	}
1956 }
1957 
1958 #ifdef CONFIG_PM
atmel_xdmac_prepare(struct device * dev)1959 static int atmel_xdmac_prepare(struct device *dev)
1960 {
1961 	struct at_xdmac		*atxdmac = dev_get_drvdata(dev);
1962 	struct dma_chan		*chan, *_chan;
1963 
1964 	list_for_each_entry_safe(chan, _chan, &atxdmac->dma.channels, device_node) {
1965 		struct at_xdmac_chan	*atchan = to_at_xdmac_chan(chan);
1966 
1967 		/* Wait for transfer completion, except in cyclic case. */
1968 		if (at_xdmac_chan_is_enabled(atchan) && !at_xdmac_chan_is_cyclic(atchan))
1969 			return -EAGAIN;
1970 	}
1971 	return 0;
1972 }
1973 #else
1974 #	define atmel_xdmac_prepare NULL
1975 #endif
1976 
1977 #ifdef CONFIG_PM_SLEEP
atmel_xdmac_suspend(struct device * dev)1978 static int atmel_xdmac_suspend(struct device *dev)
1979 {
1980 	struct at_xdmac		*atxdmac = dev_get_drvdata(dev);
1981 	struct dma_chan		*chan, *_chan;
1982 
1983 	list_for_each_entry_safe(chan, _chan, &atxdmac->dma.channels, device_node) {
1984 		struct at_xdmac_chan	*atchan = to_at_xdmac_chan(chan);
1985 
1986 		atchan->save_cc = at_xdmac_chan_read(atchan, AT_XDMAC_CC);
1987 		if (at_xdmac_chan_is_cyclic(atchan)) {
1988 			if (!at_xdmac_chan_is_paused(atchan))
1989 				at_xdmac_device_pause(chan);
1990 			atchan->save_cim = at_xdmac_chan_read(atchan, AT_XDMAC_CIM);
1991 			atchan->save_cnda = at_xdmac_chan_read(atchan, AT_XDMAC_CNDA);
1992 			atchan->save_cndc = at_xdmac_chan_read(atchan, AT_XDMAC_CNDC);
1993 		}
1994 	}
1995 	atxdmac->save_gim = at_xdmac_read(atxdmac, AT_XDMAC_GIM);
1996 	atxdmac->save_gs = at_xdmac_read(atxdmac, AT_XDMAC_GS);
1997 
1998 	at_xdmac_off(atxdmac);
1999 	clk_disable_unprepare(atxdmac->clk);
2000 	return 0;
2001 }
2002 
atmel_xdmac_resume(struct device * dev)2003 static int atmel_xdmac_resume(struct device *dev)
2004 {
2005 	struct at_xdmac		*atxdmac = dev_get_drvdata(dev);
2006 	struct at_xdmac_chan	*atchan;
2007 	struct dma_chan		*chan, *_chan;
2008 	struct platform_device	*pdev = container_of(dev, struct platform_device, dev);
2009 	int			i;
2010 	int ret;
2011 
2012 	ret = clk_prepare_enable(atxdmac->clk);
2013 	if (ret)
2014 		return ret;
2015 
2016 	at_xdmac_axi_config(pdev);
2017 
2018 	/* Clear pending interrupts. */
2019 	for (i = 0; i < atxdmac->dma.chancnt; i++) {
2020 		atchan = &atxdmac->chan[i];
2021 		while (at_xdmac_chan_read(atchan, AT_XDMAC_CIS))
2022 			cpu_relax();
2023 	}
2024 
2025 	at_xdmac_write(atxdmac, AT_XDMAC_GIE, atxdmac->save_gim);
2026 	list_for_each_entry_safe(chan, _chan, &atxdmac->dma.channels, device_node) {
2027 		atchan = to_at_xdmac_chan(chan);
2028 		at_xdmac_chan_write(atchan, AT_XDMAC_CC, atchan->save_cc);
2029 		if (at_xdmac_chan_is_cyclic(atchan)) {
2030 			if (at_xdmac_chan_is_paused(atchan))
2031 				at_xdmac_device_resume(chan);
2032 			at_xdmac_chan_write(atchan, AT_XDMAC_CNDA, atchan->save_cnda);
2033 			at_xdmac_chan_write(atchan, AT_XDMAC_CNDC, atchan->save_cndc);
2034 			at_xdmac_chan_write(atchan, AT_XDMAC_CIE, atchan->save_cim);
2035 			wmb();
2036 			if (atxdmac->save_gs & atchan->mask)
2037 				at_xdmac_write(atxdmac, AT_XDMAC_GE, atchan->mask);
2038 		}
2039 	}
2040 	return 0;
2041 }
2042 #endif /* CONFIG_PM_SLEEP */
2043 
at_xdmac_probe(struct platform_device * pdev)2044 static int at_xdmac_probe(struct platform_device *pdev)
2045 {
2046 	struct at_xdmac	*atxdmac;
2047 	int		irq, size, nr_channels, i, ret;
2048 	void __iomem	*base;
2049 	u32		reg;
2050 
2051 	irq = platform_get_irq(pdev, 0);
2052 	if (irq < 0)
2053 		return irq;
2054 
2055 	base = devm_platform_ioremap_resource(pdev, 0);
2056 	if (IS_ERR(base))
2057 		return PTR_ERR(base);
2058 
2059 	/*
2060 	 * Read number of xdmac channels, read helper function can't be used
2061 	 * since atxdmac is not yet allocated and we need to know the number
2062 	 * of channels to do the allocation.
2063 	 */
2064 	reg = readl_relaxed(base + AT_XDMAC_GTYPE);
2065 	nr_channels = AT_XDMAC_NB_CH(reg);
2066 	if (nr_channels > AT_XDMAC_MAX_CHAN) {
2067 		dev_err(&pdev->dev, "invalid number of channels (%u)\n",
2068 			nr_channels);
2069 		return -EINVAL;
2070 	}
2071 
2072 	size = sizeof(*atxdmac);
2073 	size += nr_channels * sizeof(struct at_xdmac_chan);
2074 	atxdmac = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
2075 	if (!atxdmac) {
2076 		dev_err(&pdev->dev, "can't allocate at_xdmac structure\n");
2077 		return -ENOMEM;
2078 	}
2079 
2080 	atxdmac->regs = base;
2081 	atxdmac->irq = irq;
2082 
2083 	atxdmac->layout = of_device_get_match_data(&pdev->dev);
2084 	if (!atxdmac->layout)
2085 		return -ENODEV;
2086 
2087 	atxdmac->clk = devm_clk_get(&pdev->dev, "dma_clk");
2088 	if (IS_ERR(atxdmac->clk)) {
2089 		dev_err(&pdev->dev, "can't get dma_clk\n");
2090 		return PTR_ERR(atxdmac->clk);
2091 	}
2092 
2093 	/* Do not use dev res to prevent races with tasklet */
2094 	ret = request_irq(atxdmac->irq, at_xdmac_interrupt, 0, "at_xdmac", atxdmac);
2095 	if (ret) {
2096 		dev_err(&pdev->dev, "can't request irq\n");
2097 		return ret;
2098 	}
2099 
2100 	ret = clk_prepare_enable(atxdmac->clk);
2101 	if (ret) {
2102 		dev_err(&pdev->dev, "can't prepare or enable clock\n");
2103 		goto err_free_irq;
2104 	}
2105 
2106 	atxdmac->at_xdmac_desc_pool =
2107 		dmam_pool_create(dev_name(&pdev->dev), &pdev->dev,
2108 				sizeof(struct at_xdmac_desc), 4, 0);
2109 	if (!atxdmac->at_xdmac_desc_pool) {
2110 		dev_err(&pdev->dev, "no memory for descriptors dma pool\n");
2111 		ret = -ENOMEM;
2112 		goto err_clk_disable;
2113 	}
2114 
2115 	dma_cap_set(DMA_CYCLIC, atxdmac->dma.cap_mask);
2116 	dma_cap_set(DMA_INTERLEAVE, atxdmac->dma.cap_mask);
2117 	dma_cap_set(DMA_MEMCPY, atxdmac->dma.cap_mask);
2118 	dma_cap_set(DMA_MEMSET, atxdmac->dma.cap_mask);
2119 	dma_cap_set(DMA_MEMSET_SG, atxdmac->dma.cap_mask);
2120 	dma_cap_set(DMA_SLAVE, atxdmac->dma.cap_mask);
2121 	/*
2122 	 * Without DMA_PRIVATE the driver is not able to allocate more than
2123 	 * one channel, second allocation fails in private_candidate.
2124 	 */
2125 	dma_cap_set(DMA_PRIVATE, atxdmac->dma.cap_mask);
2126 	atxdmac->dma.dev				= &pdev->dev;
2127 	atxdmac->dma.device_alloc_chan_resources	= at_xdmac_alloc_chan_resources;
2128 	atxdmac->dma.device_free_chan_resources		= at_xdmac_free_chan_resources;
2129 	atxdmac->dma.device_tx_status			= at_xdmac_tx_status;
2130 	atxdmac->dma.device_issue_pending		= at_xdmac_issue_pending;
2131 	atxdmac->dma.device_prep_dma_cyclic		= at_xdmac_prep_dma_cyclic;
2132 	atxdmac->dma.device_prep_interleaved_dma	= at_xdmac_prep_interleaved;
2133 	atxdmac->dma.device_prep_dma_memcpy		= at_xdmac_prep_dma_memcpy;
2134 	atxdmac->dma.device_prep_dma_memset		= at_xdmac_prep_dma_memset;
2135 	atxdmac->dma.device_prep_dma_memset_sg		= at_xdmac_prep_dma_memset_sg;
2136 	atxdmac->dma.device_prep_slave_sg		= at_xdmac_prep_slave_sg;
2137 	atxdmac->dma.device_config			= at_xdmac_device_config;
2138 	atxdmac->dma.device_pause			= at_xdmac_device_pause;
2139 	atxdmac->dma.device_resume			= at_xdmac_device_resume;
2140 	atxdmac->dma.device_terminate_all		= at_xdmac_device_terminate_all;
2141 	atxdmac->dma.src_addr_widths = AT_XDMAC_DMA_BUSWIDTHS;
2142 	atxdmac->dma.dst_addr_widths = AT_XDMAC_DMA_BUSWIDTHS;
2143 	atxdmac->dma.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
2144 	atxdmac->dma.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
2145 
2146 	/* Disable all chans and interrupts. */
2147 	at_xdmac_off(atxdmac);
2148 
2149 	/* Init channels. */
2150 	INIT_LIST_HEAD(&atxdmac->dma.channels);
2151 	for (i = 0; i < nr_channels; i++) {
2152 		struct at_xdmac_chan *atchan = &atxdmac->chan[i];
2153 
2154 		atchan->chan.device = &atxdmac->dma;
2155 		list_add_tail(&atchan->chan.device_node,
2156 			      &atxdmac->dma.channels);
2157 
2158 		atchan->ch_regs = at_xdmac_chan_reg_base(atxdmac, i);
2159 		atchan->mask = 1 << i;
2160 
2161 		spin_lock_init(&atchan->lock);
2162 		INIT_LIST_HEAD(&atchan->xfers_list);
2163 		INIT_LIST_HEAD(&atchan->free_descs_list);
2164 		tasklet_setup(&atchan->tasklet, at_xdmac_tasklet);
2165 
2166 		/* Clear pending interrupts. */
2167 		while (at_xdmac_chan_read(atchan, AT_XDMAC_CIS))
2168 			cpu_relax();
2169 	}
2170 	platform_set_drvdata(pdev, atxdmac);
2171 
2172 	ret = dma_async_device_register(&atxdmac->dma);
2173 	if (ret) {
2174 		dev_err(&pdev->dev, "fail to register DMA engine device\n");
2175 		goto err_clk_disable;
2176 	}
2177 
2178 	ret = of_dma_controller_register(pdev->dev.of_node,
2179 					 at_xdmac_xlate, atxdmac);
2180 	if (ret) {
2181 		dev_err(&pdev->dev, "could not register of dma controller\n");
2182 		goto err_dma_unregister;
2183 	}
2184 
2185 	dev_info(&pdev->dev, "%d channels, mapped at 0x%p\n",
2186 		 nr_channels, atxdmac->regs);
2187 
2188 	at_xdmac_axi_config(pdev);
2189 
2190 	return 0;
2191 
2192 err_dma_unregister:
2193 	dma_async_device_unregister(&atxdmac->dma);
2194 err_clk_disable:
2195 	clk_disable_unprepare(atxdmac->clk);
2196 err_free_irq:
2197 	free_irq(atxdmac->irq, atxdmac);
2198 	return ret;
2199 }
2200 
at_xdmac_remove(struct platform_device * pdev)2201 static int at_xdmac_remove(struct platform_device *pdev)
2202 {
2203 	struct at_xdmac	*atxdmac = (struct at_xdmac *)platform_get_drvdata(pdev);
2204 	int		i;
2205 
2206 	at_xdmac_off(atxdmac);
2207 	of_dma_controller_free(pdev->dev.of_node);
2208 	dma_async_device_unregister(&atxdmac->dma);
2209 	clk_disable_unprepare(atxdmac->clk);
2210 
2211 	free_irq(atxdmac->irq, atxdmac);
2212 
2213 	for (i = 0; i < atxdmac->dma.chancnt; i++) {
2214 		struct at_xdmac_chan *atchan = &atxdmac->chan[i];
2215 
2216 		tasklet_kill(&atchan->tasklet);
2217 		at_xdmac_free_chan_resources(&atchan->chan);
2218 	}
2219 
2220 	return 0;
2221 }
2222 
2223 static const struct dev_pm_ops atmel_xdmac_dev_pm_ops = {
2224 	.prepare	= atmel_xdmac_prepare,
2225 	SET_LATE_SYSTEM_SLEEP_PM_OPS(atmel_xdmac_suspend, atmel_xdmac_resume)
2226 };
2227 
2228 static const struct of_device_id atmel_xdmac_dt_ids[] = {
2229 	{
2230 		.compatible = "atmel,sama5d4-dma",
2231 		.data = &at_xdmac_sama5d4_layout,
2232 	}, {
2233 		.compatible = "microchip,sama7g5-dma",
2234 		.data = &at_xdmac_sama7g5_layout,
2235 	}, {
2236 		/* sentinel */
2237 	}
2238 };
2239 MODULE_DEVICE_TABLE(of, atmel_xdmac_dt_ids);
2240 
2241 static struct platform_driver at_xdmac_driver = {
2242 	.probe		= at_xdmac_probe,
2243 	.remove		= at_xdmac_remove,
2244 	.driver = {
2245 		.name		= "at_xdmac",
2246 		.of_match_table	= of_match_ptr(atmel_xdmac_dt_ids),
2247 		.pm		= &atmel_xdmac_dev_pm_ops,
2248 	}
2249 };
2250 
at_xdmac_init(void)2251 static int __init at_xdmac_init(void)
2252 {
2253 	return platform_driver_register(&at_xdmac_driver);
2254 }
2255 subsys_initcall(at_xdmac_init);
2256 
at_xdmac_exit(void)2257 static void __exit at_xdmac_exit(void)
2258 {
2259 	platform_driver_unregister(&at_xdmac_driver);
2260 }
2261 module_exit(at_xdmac_exit);
2262 
2263 MODULE_DESCRIPTION("Atmel Extended DMA Controller driver");
2264 MODULE_AUTHOR("Ludovic Desroches <ludovic.desroches@atmel.com>");
2265 MODULE_LICENSE("GPL");
2266