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
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 char pattern;
1210 /*
1211 * WARNING: The channel configuration is set here since there is no
1212 * dmaengine_slave_config call in this case. Moreover we don't know the
1213 * direction, it involves we can't dynamically set the source and dest
1214 * interface so we have to use the same one. Only interface 0 allows EBI
1215 * access. Hopefully we can access DDR through both ports (at least on
1216 * SAMA5D4x), so we can use the same interface for source and dest,
1217 * that solves the fact we don't know the direction.
1218 * ERRATA: Even if useless for memory transfers, the PERID has to not
1219 * match the one of another channel. If not, it could lead to spurious
1220 * flag status.
1221 * For SAMA7G5x case, the SIF and DIF fields are no longer used.
1222 * Thus, no need to have the SIF/DIF interfaces here.
1223 * For SAMA5D4x and SAMA5D2x the SIF and DIF are already configured as
1224 * zero.
1225 */
1226 u32 chan_cc = AT_XDMAC_CC_PERID(0x7f)
1227 | AT_XDMAC_CC_DAM_UBS_AM
1228 | AT_XDMAC_CC_SAM_INCREMENTED_AM
1229 | AT_XDMAC_CC_MBSIZE_SIXTEEN
1230 | AT_XDMAC_CC_MEMSET_HW_MODE
1231 | AT_XDMAC_CC_TYPE_MEM_TRAN;
1232
1233 dwidth = at_xdmac_align_width(chan, dst_addr);
1234
1235 if (len >= (AT_XDMAC_MBR_UBC_UBLEN_MAX << dwidth)) {
1236 dev_err(chan2dev(chan),
1237 "%s: Transfer too large, aborting...\n",
1238 __func__);
1239 return NULL;
1240 }
1241
1242 spin_lock_irqsave(&atchan->lock, flags);
1243 desc = at_xdmac_get_desc(atchan);
1244 spin_unlock_irqrestore(&atchan->lock, flags);
1245 if (!desc) {
1246 dev_err(chan2dev(chan), "can't get descriptor\n");
1247 return NULL;
1248 }
1249
1250 chan_cc |= AT_XDMAC_CC_DWIDTH(dwidth);
1251
1252 /* Only the first byte of value is to be used according to dmaengine */
1253 pattern = (char)value;
1254
1255 ublen = len >> dwidth;
1256
1257 desc->lld.mbr_da = dst_addr;
1258 desc->lld.mbr_ds = (pattern << 24) |
1259 (pattern << 16) |
1260 (pattern << 8) |
1261 pattern;
1262 desc->lld.mbr_ubc = AT_XDMAC_MBR_UBC_NDV3
1263 | AT_XDMAC_MBR_UBC_NDEN
1264 | AT_XDMAC_MBR_UBC_NSEN
1265 | ublen;
1266 desc->lld.mbr_cfg = chan_cc;
1267
1268 dev_dbg(chan2dev(chan),
1269 "%s: lld: mbr_da=%pad, mbr_ds=0x%08x, mbr_ubc=0x%08x, mbr_cfg=0x%08x\n",
1270 __func__, &desc->lld.mbr_da, desc->lld.mbr_ds, desc->lld.mbr_ubc,
1271 desc->lld.mbr_cfg);
1272
1273 return desc;
1274 }
1275
1276 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)1277 at_xdmac_prep_dma_memset(struct dma_chan *chan, dma_addr_t dest, int value,
1278 size_t len, unsigned long flags)
1279 {
1280 struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
1281 struct at_xdmac_desc *desc;
1282
1283 dev_dbg(chan2dev(chan), "%s: dest=%pad, len=%zu, pattern=0x%x, flags=0x%lx\n",
1284 __func__, &dest, len, value, flags);
1285
1286 if (unlikely(!len))
1287 return NULL;
1288
1289 desc = at_xdmac_memset_create_desc(chan, atchan, dest, len, value);
1290 list_add_tail(&desc->desc_node, &desc->descs_list);
1291
1292 desc->tx_dma_desc.cookie = -EBUSY;
1293 desc->tx_dma_desc.flags = flags;
1294 desc->xfer_size = len;
1295
1296 return &desc->tx_dma_desc;
1297 }
1298
1299 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)1300 at_xdmac_prep_dma_memset_sg(struct dma_chan *chan, struct scatterlist *sgl,
1301 unsigned int sg_len, int value,
1302 unsigned long flags)
1303 {
1304 struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
1305 struct at_xdmac_desc *desc, *pdesc = NULL,
1306 *ppdesc = NULL, *first = NULL;
1307 struct scatterlist *sg, *psg = NULL, *ppsg = NULL;
1308 size_t stride = 0, pstride = 0, len = 0;
1309 int i;
1310
1311 if (!sgl)
1312 return NULL;
1313
1314 dev_dbg(chan2dev(chan), "%s: sg_len=%d, value=0x%x, flags=0x%lx\n",
1315 __func__, sg_len, value, flags);
1316
1317 /* Prepare descriptors. */
1318 for_each_sg(sgl, sg, sg_len, i) {
1319 dev_dbg(chan2dev(chan), "%s: dest=%pad, len=%d, pattern=0x%x, flags=0x%lx\n",
1320 __func__, &sg_dma_address(sg), sg_dma_len(sg),
1321 value, flags);
1322 desc = at_xdmac_memset_create_desc(chan, atchan,
1323 sg_dma_address(sg),
1324 sg_dma_len(sg),
1325 value);
1326 if (!desc && first)
1327 list_splice_tail_init(&first->descs_list,
1328 &atchan->free_descs_list);
1329
1330 if (!first)
1331 first = desc;
1332
1333 /* Update our strides */
1334 pstride = stride;
1335 if (psg)
1336 stride = sg_dma_address(sg) -
1337 (sg_dma_address(psg) + sg_dma_len(psg));
1338
1339 /*
1340 * The scatterlist API gives us only the address and
1341 * length of each elements.
1342 *
1343 * Unfortunately, we don't have the stride, which we
1344 * will need to compute.
1345 *
1346 * That make us end up in a situation like this one:
1347 * len stride len stride len
1348 * +-------+ +-------+ +-------+
1349 * | N-2 | | N-1 | | N |
1350 * +-------+ +-------+ +-------+
1351 *
1352 * We need all these three elements (N-2, N-1 and N)
1353 * to actually take the decision on whether we need to
1354 * queue N-1 or reuse N-2.
1355 *
1356 * We will only consider N if it is the last element.
1357 */
1358 if (ppdesc && pdesc) {
1359 if ((stride == pstride) &&
1360 (sg_dma_len(ppsg) == sg_dma_len(psg))) {
1361 dev_dbg(chan2dev(chan),
1362 "%s: desc 0x%p can be merged with desc 0x%p\n",
1363 __func__, pdesc, ppdesc);
1364
1365 /*
1366 * Increment the block count of the
1367 * N-2 descriptor
1368 */
1369 at_xdmac_increment_block_count(chan, ppdesc);
1370 ppdesc->lld.mbr_dus = stride;
1371
1372 /*
1373 * Put back the N-1 descriptor in the
1374 * free descriptor list
1375 */
1376 list_add_tail(&pdesc->desc_node,
1377 &atchan->free_descs_list);
1378
1379 /*
1380 * Make our N-1 descriptor pointer
1381 * point to the N-2 since they were
1382 * actually merged.
1383 */
1384 pdesc = ppdesc;
1385
1386 /*
1387 * Rule out the case where we don't have
1388 * pstride computed yet (our second sg
1389 * element)
1390 *
1391 * We also want to catch the case where there
1392 * would be a negative stride,
1393 */
1394 } else if (pstride ||
1395 sg_dma_address(sg) < sg_dma_address(psg)) {
1396 /*
1397 * Queue the N-1 descriptor after the
1398 * N-2
1399 */
1400 at_xdmac_queue_desc(chan, ppdesc, pdesc);
1401
1402 /*
1403 * Add the N-1 descriptor to the list
1404 * of the descriptors used for this
1405 * transfer
1406 */
1407 list_add_tail(&desc->desc_node,
1408 &first->descs_list);
1409 dev_dbg(chan2dev(chan),
1410 "%s: add desc 0x%p to descs_list 0x%p\n",
1411 __func__, desc, first);
1412 }
1413 }
1414
1415 /*
1416 * If we are the last element, just see if we have the
1417 * same size than the previous element.
1418 *
1419 * If so, we can merge it with the previous descriptor
1420 * since we don't care about the stride anymore.
1421 */
1422 if ((i == (sg_len - 1)) &&
1423 sg_dma_len(psg) == sg_dma_len(sg)) {
1424 dev_dbg(chan2dev(chan),
1425 "%s: desc 0x%p can be merged with desc 0x%p\n",
1426 __func__, desc, pdesc);
1427
1428 /*
1429 * Increment the block count of the N-1
1430 * descriptor
1431 */
1432 at_xdmac_increment_block_count(chan, pdesc);
1433 pdesc->lld.mbr_dus = stride;
1434
1435 /*
1436 * Put back the N descriptor in the free
1437 * descriptor list
1438 */
1439 list_add_tail(&desc->desc_node,
1440 &atchan->free_descs_list);
1441 }
1442
1443 /* Update our descriptors */
1444 ppdesc = pdesc;
1445 pdesc = desc;
1446
1447 /* Update our scatter pointers */
1448 ppsg = psg;
1449 psg = sg;
1450
1451 len += sg_dma_len(sg);
1452 }
1453
1454 first->tx_dma_desc.cookie = -EBUSY;
1455 first->tx_dma_desc.flags = flags;
1456 first->xfer_size = len;
1457
1458 return &first->tx_dma_desc;
1459 }
1460
1461 static enum dma_status
at_xdmac_tx_status(struct dma_chan * chan,dma_cookie_t cookie,struct dma_tx_state * txstate)1462 at_xdmac_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
1463 struct dma_tx_state *txstate)
1464 {
1465 struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
1466 struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
1467 struct at_xdmac_desc *desc, *_desc, *iter;
1468 struct list_head *descs_list;
1469 enum dma_status ret;
1470 int residue, retry;
1471 u32 cur_nda, check_nda, cur_ubc, mask, value;
1472 u8 dwidth = 0;
1473 unsigned long flags;
1474 bool initd;
1475
1476 ret = dma_cookie_status(chan, cookie, txstate);
1477 if (ret == DMA_COMPLETE || !txstate)
1478 return ret;
1479
1480 spin_lock_irqsave(&atchan->lock, flags);
1481
1482 desc = list_first_entry(&atchan->xfers_list, struct at_xdmac_desc, xfer_node);
1483
1484 /*
1485 * If the transfer has not been started yet, don't need to compute the
1486 * residue, it's the transfer length.
1487 */
1488 if (!desc->active_xfer) {
1489 dma_set_residue(txstate, desc->xfer_size);
1490 goto spin_unlock;
1491 }
1492
1493 residue = desc->xfer_size;
1494 /*
1495 * Flush FIFO: only relevant when the transfer is source peripheral
1496 * synchronized. Flush is needed before reading CUBC because data in
1497 * the FIFO are not reported by CUBC. Reporting a residue of the
1498 * transfer length while we have data in FIFO can cause issue.
1499 * Usecase: atmel USART has a timeout which means I have received
1500 * characters but there is no more character received for a while. On
1501 * timeout, it requests the residue. If the data are in the DMA FIFO,
1502 * we will return a residue of the transfer length. It means no data
1503 * received. If an application is waiting for these data, it will hang
1504 * since we won't have another USART timeout without receiving new
1505 * data.
1506 */
1507 mask = AT_XDMAC_CC_TYPE | AT_XDMAC_CC_DSYNC;
1508 value = AT_XDMAC_CC_TYPE_PER_TRAN | AT_XDMAC_CC_DSYNC_PER2MEM;
1509 if ((desc->lld.mbr_cfg & mask) == value) {
1510 at_xdmac_write(atxdmac, atxdmac->layout->gswf, atchan->mask);
1511 while (!(at_xdmac_chan_read(atchan, AT_XDMAC_CIS) & AT_XDMAC_CIS_FIS))
1512 cpu_relax();
1513 }
1514
1515 /*
1516 * The easiest way to compute the residue should be to pause the DMA
1517 * but doing this can lead to miss some data as some devices don't
1518 * have FIFO.
1519 * We need to read several registers because:
1520 * - DMA is running therefore a descriptor change is possible while
1521 * reading these registers
1522 * - When the block transfer is done, the value of the CUBC register
1523 * is set to its initial value until the fetch of the next descriptor.
1524 * This value will corrupt the residue calculation so we have to skip
1525 * it.
1526 *
1527 * INITD -------- ------------
1528 * |____________________|
1529 * _______________________ _______________
1530 * NDA @desc2 \/ @desc3
1531 * _______________________/\_______________
1532 * __________ ___________ _______________
1533 * CUBC 0 \/ MAX desc1 \/ MAX desc2
1534 * __________/\___________/\_______________
1535 *
1536 * Since descriptors are aligned on 64 bits, we can assume that
1537 * the update of NDA and CUBC is atomic.
1538 * Memory barriers are used to ensure the read order of the registers.
1539 * A max number of retries is set because unlikely it could never ends.
1540 */
1541 for (retry = 0; retry < AT_XDMAC_RESIDUE_MAX_RETRIES; retry++) {
1542 check_nda = at_xdmac_chan_read(atchan, AT_XDMAC_CNDA) & 0xfffffffc;
1543 rmb();
1544 cur_ubc = at_xdmac_chan_read(atchan, AT_XDMAC_CUBC);
1545 rmb();
1546 initd = !!(at_xdmac_chan_read(atchan, AT_XDMAC_CC) & AT_XDMAC_CC_INITD);
1547 rmb();
1548 cur_nda = at_xdmac_chan_read(atchan, AT_XDMAC_CNDA) & 0xfffffffc;
1549 rmb();
1550
1551 if ((check_nda == cur_nda) && initd)
1552 break;
1553 }
1554
1555 if (unlikely(retry >= AT_XDMAC_RESIDUE_MAX_RETRIES)) {
1556 ret = DMA_ERROR;
1557 goto spin_unlock;
1558 }
1559
1560 /*
1561 * Flush FIFO: only relevant when the transfer is source peripheral
1562 * synchronized. Another flush is needed here because CUBC is updated
1563 * when the controller sends the data write command. It can lead to
1564 * report data that are not written in the memory or the device. The
1565 * FIFO flush ensures that data are really written.
1566 */
1567 if ((desc->lld.mbr_cfg & mask) == value) {
1568 at_xdmac_write(atxdmac, atxdmac->layout->gswf, atchan->mask);
1569 while (!(at_xdmac_chan_read(atchan, AT_XDMAC_CIS) & AT_XDMAC_CIS_FIS))
1570 cpu_relax();
1571 }
1572
1573 /*
1574 * Remove size of all microblocks already transferred and the current
1575 * one. Then add the remaining size to transfer of the current
1576 * microblock.
1577 */
1578 descs_list = &desc->descs_list;
1579 list_for_each_entry_safe(iter, _desc, descs_list, desc_node) {
1580 dwidth = at_xdmac_get_dwidth(iter->lld.mbr_cfg);
1581 residue -= (iter->lld.mbr_ubc & 0xffffff) << dwidth;
1582 if ((iter->lld.mbr_nda & 0xfffffffc) == cur_nda) {
1583 desc = iter;
1584 break;
1585 }
1586 }
1587 residue += cur_ubc << dwidth;
1588
1589 dma_set_residue(txstate, residue);
1590
1591 dev_dbg(chan2dev(chan),
1592 "%s: desc=0x%p, tx_dma_desc.phys=%pad, tx_status=%d, cookie=%d, residue=%d\n",
1593 __func__, desc, &desc->tx_dma_desc.phys, ret, cookie, residue);
1594
1595 spin_unlock:
1596 spin_unlock_irqrestore(&atchan->lock, flags);
1597 return ret;
1598 }
1599
at_xdmac_advance_work(struct at_xdmac_chan * atchan)1600 static void at_xdmac_advance_work(struct at_xdmac_chan *atchan)
1601 {
1602 struct at_xdmac_desc *desc;
1603
1604 /*
1605 * If channel is enabled, do nothing, advance_work will be triggered
1606 * after the interruption.
1607 */
1608 if (at_xdmac_chan_is_enabled(atchan) || list_empty(&atchan->xfers_list))
1609 return;
1610
1611 desc = list_first_entry(&atchan->xfers_list, struct at_xdmac_desc,
1612 xfer_node);
1613 dev_vdbg(chan2dev(&atchan->chan), "%s: desc 0x%p\n", __func__, desc);
1614 if (!desc->active_xfer)
1615 at_xdmac_start_xfer(atchan, desc);
1616 }
1617
at_xdmac_handle_cyclic(struct at_xdmac_chan * atchan)1618 static void at_xdmac_handle_cyclic(struct at_xdmac_chan *atchan)
1619 {
1620 struct at_xdmac_desc *desc;
1621 struct dma_async_tx_descriptor *txd;
1622
1623 spin_lock_irq(&atchan->lock);
1624 dev_dbg(chan2dev(&atchan->chan), "%s: status=0x%08x\n",
1625 __func__, atchan->irq_status);
1626 if (list_empty(&atchan->xfers_list)) {
1627 spin_unlock_irq(&atchan->lock);
1628 return;
1629 }
1630 desc = list_first_entry(&atchan->xfers_list, struct at_xdmac_desc,
1631 xfer_node);
1632 spin_unlock_irq(&atchan->lock);
1633 txd = &desc->tx_dma_desc;
1634 if (txd->flags & DMA_PREP_INTERRUPT)
1635 dmaengine_desc_get_callback_invoke(txd, NULL);
1636 }
1637
1638 /* Called with atchan->lock held. */
at_xdmac_handle_error(struct at_xdmac_chan * atchan)1639 static void at_xdmac_handle_error(struct at_xdmac_chan *atchan)
1640 {
1641 struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
1642 struct at_xdmac_desc *bad_desc;
1643
1644 /*
1645 * The descriptor currently at the head of the active list is
1646 * broken. Since we don't have any way to report errors, we'll
1647 * just have to scream loudly and try to continue with other
1648 * descriptors queued (if any).
1649 */
1650 if (atchan->irq_status & AT_XDMAC_CIS_RBEIS)
1651 dev_err(chan2dev(&atchan->chan), "read bus error!!!");
1652 if (atchan->irq_status & AT_XDMAC_CIS_WBEIS)
1653 dev_err(chan2dev(&atchan->chan), "write bus error!!!");
1654 if (atchan->irq_status & AT_XDMAC_CIS_ROIS)
1655 dev_err(chan2dev(&atchan->chan), "request overflow error!!!");
1656
1657 /* Channel must be disabled first as it's not done automatically */
1658 at_xdmac_write(atxdmac, AT_XDMAC_GD, atchan->mask);
1659 while (at_xdmac_read(atxdmac, AT_XDMAC_GS) & atchan->mask)
1660 cpu_relax();
1661
1662 bad_desc = list_first_entry(&atchan->xfers_list,
1663 struct at_xdmac_desc,
1664 xfer_node);
1665
1666 /* Print bad descriptor's details if needed */
1667 dev_dbg(chan2dev(&atchan->chan),
1668 "%s: lld: mbr_sa=%pad, mbr_da=%pad, mbr_ubc=0x%08x\n",
1669 __func__, &bad_desc->lld.mbr_sa, &bad_desc->lld.mbr_da,
1670 bad_desc->lld.mbr_ubc);
1671
1672 /* Then continue with usual descriptor management */
1673 }
1674
at_xdmac_tasklet(struct tasklet_struct * t)1675 static void at_xdmac_tasklet(struct tasklet_struct *t)
1676 {
1677 struct at_xdmac_chan *atchan = from_tasklet(atchan, t, tasklet);
1678 struct at_xdmac_desc *desc;
1679 struct dma_async_tx_descriptor *txd;
1680 u32 error_mask;
1681
1682 if (at_xdmac_chan_is_cyclic(atchan))
1683 return at_xdmac_handle_cyclic(atchan);
1684
1685 error_mask = AT_XDMAC_CIS_RBEIS | AT_XDMAC_CIS_WBEIS |
1686 AT_XDMAC_CIS_ROIS;
1687
1688 spin_lock_irq(&atchan->lock);
1689
1690 dev_dbg(chan2dev(&atchan->chan), "%s: status=0x%08x\n",
1691 __func__, atchan->irq_status);
1692
1693 if (!(atchan->irq_status & AT_XDMAC_CIS_LIS) &&
1694 !(atchan->irq_status & error_mask)) {
1695 spin_unlock_irq(&atchan->lock);
1696 return;
1697 }
1698
1699 if (atchan->irq_status & error_mask)
1700 at_xdmac_handle_error(atchan);
1701
1702 desc = list_first_entry(&atchan->xfers_list, struct at_xdmac_desc,
1703 xfer_node);
1704 dev_vdbg(chan2dev(&atchan->chan), "%s: desc 0x%p\n", __func__, desc);
1705 if (!desc->active_xfer) {
1706 dev_err(chan2dev(&atchan->chan), "Xfer not active: exiting");
1707 spin_unlock_irq(&atchan->lock);
1708 return;
1709 }
1710
1711 txd = &desc->tx_dma_desc;
1712 dma_cookie_complete(txd);
1713 /* Remove the transfer from the transfer list. */
1714 list_del(&desc->xfer_node);
1715 spin_unlock_irq(&atchan->lock);
1716
1717 if (txd->flags & DMA_PREP_INTERRUPT)
1718 dmaengine_desc_get_callback_invoke(txd, NULL);
1719
1720 dma_run_dependencies(txd);
1721
1722 spin_lock_irq(&atchan->lock);
1723 /* Move the xfer descriptors into the free descriptors list. */
1724 list_splice_tail_init(&desc->descs_list, &atchan->free_descs_list);
1725 at_xdmac_advance_work(atchan);
1726 spin_unlock_irq(&atchan->lock);
1727 }
1728
at_xdmac_interrupt(int irq,void * dev_id)1729 static irqreturn_t at_xdmac_interrupt(int irq, void *dev_id)
1730 {
1731 struct at_xdmac *atxdmac = (struct at_xdmac *)dev_id;
1732 struct at_xdmac_chan *atchan;
1733 u32 imr, status, pending;
1734 u32 chan_imr, chan_status;
1735 int i, ret = IRQ_NONE;
1736
1737 do {
1738 imr = at_xdmac_read(atxdmac, AT_XDMAC_GIM);
1739 status = at_xdmac_read(atxdmac, AT_XDMAC_GIS);
1740 pending = status & imr;
1741
1742 dev_vdbg(atxdmac->dma.dev,
1743 "%s: status=0x%08x, imr=0x%08x, pending=0x%08x\n",
1744 __func__, status, imr, pending);
1745
1746 if (!pending)
1747 break;
1748
1749 /* We have to find which channel has generated the interrupt. */
1750 for (i = 0; i < atxdmac->dma.chancnt; i++) {
1751 if (!((1 << i) & pending))
1752 continue;
1753
1754 atchan = &atxdmac->chan[i];
1755 chan_imr = at_xdmac_chan_read(atchan, AT_XDMAC_CIM);
1756 chan_status = at_xdmac_chan_read(atchan, AT_XDMAC_CIS);
1757 atchan->irq_status = chan_status & chan_imr;
1758 dev_vdbg(atxdmac->dma.dev,
1759 "%s: chan%d: imr=0x%x, status=0x%x\n",
1760 __func__, i, chan_imr, chan_status);
1761 dev_vdbg(chan2dev(&atchan->chan),
1762 "%s: CC=0x%08x CNDA=0x%08x, CNDC=0x%08x, CSA=0x%08x, CDA=0x%08x, CUBC=0x%08x\n",
1763 __func__,
1764 at_xdmac_chan_read(atchan, AT_XDMAC_CC),
1765 at_xdmac_chan_read(atchan, AT_XDMAC_CNDA),
1766 at_xdmac_chan_read(atchan, AT_XDMAC_CNDC),
1767 at_xdmac_chan_read(atchan, AT_XDMAC_CSA),
1768 at_xdmac_chan_read(atchan, AT_XDMAC_CDA),
1769 at_xdmac_chan_read(atchan, AT_XDMAC_CUBC));
1770
1771 if (atchan->irq_status & (AT_XDMAC_CIS_RBEIS | AT_XDMAC_CIS_WBEIS))
1772 at_xdmac_write(atxdmac, AT_XDMAC_GD, atchan->mask);
1773
1774 tasklet_schedule(&atchan->tasklet);
1775 ret = IRQ_HANDLED;
1776 }
1777
1778 } while (pending);
1779
1780 return ret;
1781 }
1782
at_xdmac_issue_pending(struct dma_chan * chan)1783 static void at_xdmac_issue_pending(struct dma_chan *chan)
1784 {
1785 struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
1786 unsigned long flags;
1787
1788 dev_dbg(chan2dev(&atchan->chan), "%s\n", __func__);
1789
1790 spin_lock_irqsave(&atchan->lock, flags);
1791 at_xdmac_advance_work(atchan);
1792 spin_unlock_irqrestore(&atchan->lock, flags);
1793
1794 return;
1795 }
1796
at_xdmac_device_config(struct dma_chan * chan,struct dma_slave_config * config)1797 static int at_xdmac_device_config(struct dma_chan *chan,
1798 struct dma_slave_config *config)
1799 {
1800 struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
1801 int ret;
1802 unsigned long flags;
1803
1804 dev_dbg(chan2dev(chan), "%s\n", __func__);
1805
1806 spin_lock_irqsave(&atchan->lock, flags);
1807 ret = at_xdmac_set_slave_config(chan, config);
1808 spin_unlock_irqrestore(&atchan->lock, flags);
1809
1810 return ret;
1811 }
1812
at_xdmac_device_pause(struct dma_chan * chan)1813 static int at_xdmac_device_pause(struct dma_chan *chan)
1814 {
1815 struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
1816 struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
1817 unsigned long flags;
1818
1819 dev_dbg(chan2dev(chan), "%s\n", __func__);
1820
1821 if (test_and_set_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status))
1822 return 0;
1823
1824 spin_lock_irqsave(&atchan->lock, flags);
1825 at_xdmac_write(atxdmac, atxdmac->layout->grws, atchan->mask);
1826 while (at_xdmac_chan_read(atchan, AT_XDMAC_CC)
1827 & (AT_XDMAC_CC_WRIP | AT_XDMAC_CC_RDIP))
1828 cpu_relax();
1829 spin_unlock_irqrestore(&atchan->lock, flags);
1830
1831 return 0;
1832 }
1833
at_xdmac_device_resume(struct dma_chan * chan)1834 static int at_xdmac_device_resume(struct dma_chan *chan)
1835 {
1836 struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
1837 struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
1838 unsigned long flags;
1839
1840 dev_dbg(chan2dev(chan), "%s\n", __func__);
1841
1842 spin_lock_irqsave(&atchan->lock, flags);
1843 if (!at_xdmac_chan_is_paused(atchan)) {
1844 spin_unlock_irqrestore(&atchan->lock, flags);
1845 return 0;
1846 }
1847
1848 at_xdmac_write(atxdmac, atxdmac->layout->grwr, atchan->mask);
1849 clear_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status);
1850 spin_unlock_irqrestore(&atchan->lock, flags);
1851
1852 return 0;
1853 }
1854
at_xdmac_device_terminate_all(struct dma_chan * chan)1855 static int at_xdmac_device_terminate_all(struct dma_chan *chan)
1856 {
1857 struct at_xdmac_desc *desc, *_desc;
1858 struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
1859 struct at_xdmac *atxdmac = to_at_xdmac(atchan->chan.device);
1860 unsigned long flags;
1861
1862 dev_dbg(chan2dev(chan), "%s\n", __func__);
1863
1864 spin_lock_irqsave(&atchan->lock, flags);
1865 at_xdmac_write(atxdmac, AT_XDMAC_GD, atchan->mask);
1866 while (at_xdmac_read(atxdmac, AT_XDMAC_GS) & atchan->mask)
1867 cpu_relax();
1868
1869 /* Cancel all pending transfers. */
1870 list_for_each_entry_safe(desc, _desc, &atchan->xfers_list, xfer_node) {
1871 list_del(&desc->xfer_node);
1872 list_splice_tail_init(&desc->descs_list,
1873 &atchan->free_descs_list);
1874 }
1875
1876 clear_bit(AT_XDMAC_CHAN_IS_PAUSED, &atchan->status);
1877 clear_bit(AT_XDMAC_CHAN_IS_CYCLIC, &atchan->status);
1878 spin_unlock_irqrestore(&atchan->lock, flags);
1879
1880 return 0;
1881 }
1882
at_xdmac_alloc_chan_resources(struct dma_chan * chan)1883 static int at_xdmac_alloc_chan_resources(struct dma_chan *chan)
1884 {
1885 struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
1886 struct at_xdmac_desc *desc;
1887 int i;
1888
1889 if (at_xdmac_chan_is_enabled(atchan)) {
1890 dev_err(chan2dev(chan),
1891 "can't allocate channel resources (channel enabled)\n");
1892 return -EIO;
1893 }
1894
1895 if (!list_empty(&atchan->free_descs_list)) {
1896 dev_err(chan2dev(chan),
1897 "can't allocate channel resources (channel not free from a previous use)\n");
1898 return -EIO;
1899 }
1900
1901 for (i = 0; i < init_nr_desc_per_channel; i++) {
1902 desc = at_xdmac_alloc_desc(chan, GFP_KERNEL);
1903 if (!desc) {
1904 if (i == 0) {
1905 dev_warn(chan2dev(chan),
1906 "can't allocate any descriptors\n");
1907 return -EIO;
1908 }
1909 dev_warn(chan2dev(chan),
1910 "only %d descriptors have been allocated\n", i);
1911 break;
1912 }
1913 list_add_tail(&desc->desc_node, &atchan->free_descs_list);
1914 }
1915
1916 dma_cookie_init(chan);
1917
1918 dev_dbg(chan2dev(chan), "%s: allocated %d descriptors\n", __func__, i);
1919
1920 return i;
1921 }
1922
at_xdmac_free_chan_resources(struct dma_chan * chan)1923 static void at_xdmac_free_chan_resources(struct dma_chan *chan)
1924 {
1925 struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
1926 struct at_xdmac *atxdmac = to_at_xdmac(chan->device);
1927 struct at_xdmac_desc *desc, *_desc;
1928
1929 list_for_each_entry_safe(desc, _desc, &atchan->free_descs_list, desc_node) {
1930 dev_dbg(chan2dev(chan), "%s: freeing descriptor %p\n", __func__, desc);
1931 list_del(&desc->desc_node);
1932 dma_pool_free(atxdmac->at_xdmac_desc_pool, desc, desc->tx_dma_desc.phys);
1933 }
1934
1935 return;
1936 }
1937
at_xdmac_axi_config(struct platform_device * pdev)1938 static void at_xdmac_axi_config(struct platform_device *pdev)
1939 {
1940 struct at_xdmac *atxdmac = (struct at_xdmac *)platform_get_drvdata(pdev);
1941 bool dev_m2m = false;
1942 u32 dma_requests;
1943
1944 if (!atxdmac->layout->axi_config)
1945 return; /* Not supported */
1946
1947 if (!of_property_read_u32(pdev->dev.of_node, "dma-requests",
1948 &dma_requests)) {
1949 dev_info(&pdev->dev, "controller in mem2mem mode.\n");
1950 dev_m2m = true;
1951 }
1952
1953 if (dev_m2m) {
1954 at_xdmac_write(atxdmac, AT_XDMAC_GCFG, AT_XDMAC_GCFG_M2M);
1955 at_xdmac_write(atxdmac, AT_XDMAC_GWAC, AT_XDMAC_GWAC_M2M);
1956 } else {
1957 at_xdmac_write(atxdmac, AT_XDMAC_GCFG, AT_XDMAC_GCFG_P2M);
1958 at_xdmac_write(atxdmac, AT_XDMAC_GWAC, AT_XDMAC_GWAC_P2M);
1959 }
1960 }
1961
atmel_xdmac_prepare(struct device * dev)1962 static int __maybe_unused atmel_xdmac_prepare(struct device *dev)
1963 {
1964 struct at_xdmac *atxdmac = dev_get_drvdata(dev);
1965 struct dma_chan *chan, *_chan;
1966
1967 list_for_each_entry_safe(chan, _chan, &atxdmac->dma.channels, device_node) {
1968 struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
1969
1970 /* Wait for transfer completion, except in cyclic case. */
1971 if (at_xdmac_chan_is_enabled(atchan) && !at_xdmac_chan_is_cyclic(atchan))
1972 return -EAGAIN;
1973 }
1974 return 0;
1975 }
1976
atmel_xdmac_suspend(struct device * dev)1977 static int __maybe_unused atmel_xdmac_suspend(struct device *dev)
1978 {
1979 struct at_xdmac *atxdmac = dev_get_drvdata(dev);
1980 struct dma_chan *chan, *_chan;
1981
1982 list_for_each_entry_safe(chan, _chan, &atxdmac->dma.channels, device_node) {
1983 struct at_xdmac_chan *atchan = to_at_xdmac_chan(chan);
1984
1985 atchan->save_cc = at_xdmac_chan_read(atchan, AT_XDMAC_CC);
1986 if (at_xdmac_chan_is_cyclic(atchan)) {
1987 if (!at_xdmac_chan_is_paused(atchan))
1988 at_xdmac_device_pause(chan);
1989 atchan->save_cim = at_xdmac_chan_read(atchan, AT_XDMAC_CIM);
1990 atchan->save_cnda = at_xdmac_chan_read(atchan, AT_XDMAC_CNDA);
1991 atchan->save_cndc = at_xdmac_chan_read(atchan, AT_XDMAC_CNDC);
1992 }
1993 }
1994 atxdmac->save_gim = at_xdmac_read(atxdmac, AT_XDMAC_GIM);
1995 atxdmac->save_gs = at_xdmac_read(atxdmac, AT_XDMAC_GS);
1996
1997 at_xdmac_off(atxdmac);
1998 clk_disable_unprepare(atxdmac->clk);
1999 return 0;
2000 }
2001
atmel_xdmac_resume(struct device * dev)2002 static int __maybe_unused atmel_xdmac_resume(struct device *dev)
2003 {
2004 struct at_xdmac *atxdmac = dev_get_drvdata(dev);
2005 struct at_xdmac_chan *atchan;
2006 struct dma_chan *chan, *_chan;
2007 struct platform_device *pdev = container_of(dev, struct platform_device, dev);
2008 int i;
2009 int ret;
2010
2011 ret = clk_prepare_enable(atxdmac->clk);
2012 if (ret)
2013 return ret;
2014
2015 at_xdmac_axi_config(pdev);
2016
2017 /* Clear pending interrupts. */
2018 for (i = 0; i < atxdmac->dma.chancnt; i++) {
2019 atchan = &atxdmac->chan[i];
2020 while (at_xdmac_chan_read(atchan, AT_XDMAC_CIS))
2021 cpu_relax();
2022 }
2023
2024 at_xdmac_write(atxdmac, AT_XDMAC_GIE, atxdmac->save_gim);
2025 list_for_each_entry_safe(chan, _chan, &atxdmac->dma.channels, device_node) {
2026 atchan = to_at_xdmac_chan(chan);
2027 at_xdmac_chan_write(atchan, AT_XDMAC_CC, atchan->save_cc);
2028 if (at_xdmac_chan_is_cyclic(atchan)) {
2029 if (at_xdmac_chan_is_paused(atchan))
2030 at_xdmac_device_resume(chan);
2031 at_xdmac_chan_write(atchan, AT_XDMAC_CNDA, atchan->save_cnda);
2032 at_xdmac_chan_write(atchan, AT_XDMAC_CNDC, atchan->save_cndc);
2033 at_xdmac_chan_write(atchan, AT_XDMAC_CIE, atchan->save_cim);
2034 wmb();
2035 if (atxdmac->save_gs & atchan->mask)
2036 at_xdmac_write(atxdmac, AT_XDMAC_GE, atchan->mask);
2037 }
2038 }
2039 return 0;
2040 }
2041
at_xdmac_probe(struct platform_device * pdev)2042 static int at_xdmac_probe(struct platform_device *pdev)
2043 {
2044 struct at_xdmac *atxdmac;
2045 int irq, nr_channels, i, ret;
2046 void __iomem *base;
2047 u32 reg;
2048
2049 irq = platform_get_irq(pdev, 0);
2050 if (irq < 0)
2051 return irq;
2052
2053 base = devm_platform_ioremap_resource(pdev, 0);
2054 if (IS_ERR(base))
2055 return PTR_ERR(base);
2056
2057 /*
2058 * Read number of xdmac channels, read helper function can't be used
2059 * since atxdmac is not yet allocated and we need to know the number
2060 * of channels to do the allocation.
2061 */
2062 reg = readl_relaxed(base + AT_XDMAC_GTYPE);
2063 nr_channels = AT_XDMAC_NB_CH(reg);
2064 if (nr_channels > AT_XDMAC_MAX_CHAN) {
2065 dev_err(&pdev->dev, "invalid number of channels (%u)\n",
2066 nr_channels);
2067 return -EINVAL;
2068 }
2069
2070 atxdmac = devm_kzalloc(&pdev->dev,
2071 struct_size(atxdmac, chan, nr_channels),
2072 GFP_KERNEL);
2073 if (!atxdmac) {
2074 dev_err(&pdev->dev, "can't allocate at_xdmac structure\n");
2075 return -ENOMEM;
2076 }
2077
2078 atxdmac->regs = base;
2079 atxdmac->irq = irq;
2080
2081 atxdmac->layout = of_device_get_match_data(&pdev->dev);
2082 if (!atxdmac->layout)
2083 return -ENODEV;
2084
2085 atxdmac->clk = devm_clk_get(&pdev->dev, "dma_clk");
2086 if (IS_ERR(atxdmac->clk)) {
2087 dev_err(&pdev->dev, "can't get dma_clk\n");
2088 return PTR_ERR(atxdmac->clk);
2089 }
2090
2091 /* Do not use dev res to prevent races with tasklet */
2092 ret = request_irq(atxdmac->irq, at_xdmac_interrupt, 0, "at_xdmac", atxdmac);
2093 if (ret) {
2094 dev_err(&pdev->dev, "can't request irq\n");
2095 return ret;
2096 }
2097
2098 ret = clk_prepare_enable(atxdmac->clk);
2099 if (ret) {
2100 dev_err(&pdev->dev, "can't prepare or enable clock\n");
2101 goto err_free_irq;
2102 }
2103
2104 atxdmac->at_xdmac_desc_pool =
2105 dmam_pool_create(dev_name(&pdev->dev), &pdev->dev,
2106 sizeof(struct at_xdmac_desc), 4, 0);
2107 if (!atxdmac->at_xdmac_desc_pool) {
2108 dev_err(&pdev->dev, "no memory for descriptors dma pool\n");
2109 ret = -ENOMEM;
2110 goto err_clk_disable;
2111 }
2112
2113 dma_cap_set(DMA_CYCLIC, atxdmac->dma.cap_mask);
2114 dma_cap_set(DMA_INTERLEAVE, atxdmac->dma.cap_mask);
2115 dma_cap_set(DMA_MEMCPY, atxdmac->dma.cap_mask);
2116 dma_cap_set(DMA_MEMSET, atxdmac->dma.cap_mask);
2117 dma_cap_set(DMA_MEMSET_SG, atxdmac->dma.cap_mask);
2118 dma_cap_set(DMA_SLAVE, atxdmac->dma.cap_mask);
2119 /*
2120 * Without DMA_PRIVATE the driver is not able to allocate more than
2121 * one channel, second allocation fails in private_candidate.
2122 */
2123 dma_cap_set(DMA_PRIVATE, atxdmac->dma.cap_mask);
2124 atxdmac->dma.dev = &pdev->dev;
2125 atxdmac->dma.device_alloc_chan_resources = at_xdmac_alloc_chan_resources;
2126 atxdmac->dma.device_free_chan_resources = at_xdmac_free_chan_resources;
2127 atxdmac->dma.device_tx_status = at_xdmac_tx_status;
2128 atxdmac->dma.device_issue_pending = at_xdmac_issue_pending;
2129 atxdmac->dma.device_prep_dma_cyclic = at_xdmac_prep_dma_cyclic;
2130 atxdmac->dma.device_prep_interleaved_dma = at_xdmac_prep_interleaved;
2131 atxdmac->dma.device_prep_dma_memcpy = at_xdmac_prep_dma_memcpy;
2132 atxdmac->dma.device_prep_dma_memset = at_xdmac_prep_dma_memset;
2133 atxdmac->dma.device_prep_dma_memset_sg = at_xdmac_prep_dma_memset_sg;
2134 atxdmac->dma.device_prep_slave_sg = at_xdmac_prep_slave_sg;
2135 atxdmac->dma.device_config = at_xdmac_device_config;
2136 atxdmac->dma.device_pause = at_xdmac_device_pause;
2137 atxdmac->dma.device_resume = at_xdmac_device_resume;
2138 atxdmac->dma.device_terminate_all = at_xdmac_device_terminate_all;
2139 atxdmac->dma.src_addr_widths = AT_XDMAC_DMA_BUSWIDTHS;
2140 atxdmac->dma.dst_addr_widths = AT_XDMAC_DMA_BUSWIDTHS;
2141 atxdmac->dma.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
2142 atxdmac->dma.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
2143
2144 /* Disable all chans and interrupts. */
2145 at_xdmac_off(atxdmac);
2146
2147 /* Init channels. */
2148 INIT_LIST_HEAD(&atxdmac->dma.channels);
2149 for (i = 0; i < nr_channels; i++) {
2150 struct at_xdmac_chan *atchan = &atxdmac->chan[i];
2151
2152 atchan->chan.device = &atxdmac->dma;
2153 list_add_tail(&atchan->chan.device_node,
2154 &atxdmac->dma.channels);
2155
2156 atchan->ch_regs = at_xdmac_chan_reg_base(atxdmac, i);
2157 atchan->mask = 1 << i;
2158
2159 spin_lock_init(&atchan->lock);
2160 INIT_LIST_HEAD(&atchan->xfers_list);
2161 INIT_LIST_HEAD(&atchan->free_descs_list);
2162 tasklet_setup(&atchan->tasklet, at_xdmac_tasklet);
2163
2164 /* Clear pending interrupts. */
2165 while (at_xdmac_chan_read(atchan, AT_XDMAC_CIS))
2166 cpu_relax();
2167 }
2168 platform_set_drvdata(pdev, atxdmac);
2169
2170 ret = dma_async_device_register(&atxdmac->dma);
2171 if (ret) {
2172 dev_err(&pdev->dev, "fail to register DMA engine device\n");
2173 goto err_clk_disable;
2174 }
2175
2176 ret = of_dma_controller_register(pdev->dev.of_node,
2177 at_xdmac_xlate, atxdmac);
2178 if (ret) {
2179 dev_err(&pdev->dev, "could not register of dma controller\n");
2180 goto err_dma_unregister;
2181 }
2182
2183 dev_info(&pdev->dev, "%d channels, mapped at 0x%p\n",
2184 nr_channels, atxdmac->regs);
2185
2186 at_xdmac_axi_config(pdev);
2187
2188 return 0;
2189
2190 err_dma_unregister:
2191 dma_async_device_unregister(&atxdmac->dma);
2192 err_clk_disable:
2193 clk_disable_unprepare(atxdmac->clk);
2194 err_free_irq:
2195 free_irq(atxdmac->irq, atxdmac);
2196 return ret;
2197 }
2198
at_xdmac_remove(struct platform_device * pdev)2199 static int at_xdmac_remove(struct platform_device *pdev)
2200 {
2201 struct at_xdmac *atxdmac = (struct at_xdmac *)platform_get_drvdata(pdev);
2202 int i;
2203
2204 at_xdmac_off(atxdmac);
2205 of_dma_controller_free(pdev->dev.of_node);
2206 dma_async_device_unregister(&atxdmac->dma);
2207 clk_disable_unprepare(atxdmac->clk);
2208
2209 free_irq(atxdmac->irq, atxdmac);
2210
2211 for (i = 0; i < atxdmac->dma.chancnt; i++) {
2212 struct at_xdmac_chan *atchan = &atxdmac->chan[i];
2213
2214 tasklet_kill(&atchan->tasklet);
2215 at_xdmac_free_chan_resources(&atchan->chan);
2216 }
2217
2218 return 0;
2219 }
2220
2221 static const struct dev_pm_ops __maybe_unused atmel_xdmac_dev_pm_ops = {
2222 .prepare = atmel_xdmac_prepare,
2223 SET_LATE_SYSTEM_SLEEP_PM_OPS(atmel_xdmac_suspend, atmel_xdmac_resume)
2224 };
2225
2226 static const struct of_device_id atmel_xdmac_dt_ids[] = {
2227 {
2228 .compatible = "atmel,sama5d4-dma",
2229 .data = &at_xdmac_sama5d4_layout,
2230 }, {
2231 .compatible = "microchip,sama7g5-dma",
2232 .data = &at_xdmac_sama7g5_layout,
2233 }, {
2234 /* sentinel */
2235 }
2236 };
2237 MODULE_DEVICE_TABLE(of, atmel_xdmac_dt_ids);
2238
2239 static struct platform_driver at_xdmac_driver = {
2240 .probe = at_xdmac_probe,
2241 .remove = at_xdmac_remove,
2242 .driver = {
2243 .name = "at_xdmac",
2244 .of_match_table = of_match_ptr(atmel_xdmac_dt_ids),
2245 .pm = pm_ptr(&atmel_xdmac_dev_pm_ops),
2246 }
2247 };
2248
at_xdmac_init(void)2249 static int __init at_xdmac_init(void)
2250 {
2251 return platform_driver_register(&at_xdmac_driver);
2252 }
2253 subsys_initcall(at_xdmac_init);
2254
at_xdmac_exit(void)2255 static void __exit at_xdmac_exit(void)
2256 {
2257 platform_driver_unregister(&at_xdmac_driver);
2258 }
2259 module_exit(at_xdmac_exit);
2260
2261 MODULE_DESCRIPTION("Atmel Extended DMA Controller driver");
2262 MODULE_AUTHOR("Ludovic Desroches <ludovic.desroches@atmel.com>");
2263 MODULE_LICENSE("GPL");
2264