xref: /linux/drivers/dma/stm32/stm32-dma.c (revision 4dd10fbbeb9c33709edc89916e19c03c780a0751)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Driver for STM32 DMA controller
4  *
5  * Inspired by dma-jz4740.c and tegra20-apb-dma.c
6  *
7  * Copyright (C) M'boumba Cedric Madianga 2015
8  * Author: M'boumba Cedric Madianga <cedric.madianga@gmail.com>
9  *         Pierre-Yves Mordret <pierre-yves.mordret@st.com>
10  */
11 
12 #include <linux/bitfield.h>
13 #include <linux/clk.h>
14 #include <linux/delay.h>
15 #include <linux/dmaengine.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/err.h>
18 #include <linux/init.h>
19 #include <linux/iopoll.h>
20 #include <linux/jiffies.h>
21 #include <linux/list.h>
22 #include <linux/module.h>
23 #include <linux/of.h>
24 #include <linux/of_dma.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/reset.h>
28 #include <linux/sched.h>
29 #include <linux/slab.h>
30 
31 #include "../virt-dma.h"
32 
33 #define STM32_DMA_LISR			0x0000 /* DMA Low Int Status Reg */
34 #define STM32_DMA_HISR			0x0004 /* DMA High Int Status Reg */
35 #define STM32_DMA_ISR(n)		(((n) & 4) ? STM32_DMA_HISR : STM32_DMA_LISR)
36 #define STM32_DMA_LIFCR			0x0008 /* DMA Low Int Flag Clear Reg */
37 #define STM32_DMA_HIFCR			0x000c /* DMA High Int Flag Clear Reg */
38 #define STM32_DMA_IFCR(n)		(((n) & 4) ? STM32_DMA_HIFCR : STM32_DMA_LIFCR)
39 #define STM32_DMA_TCI			BIT(5) /* Transfer Complete Interrupt */
40 #define STM32_DMA_HTI			BIT(4) /* Half Transfer Interrupt */
41 #define STM32_DMA_TEI			BIT(3) /* Transfer Error Interrupt */
42 #define STM32_DMA_DMEI			BIT(2) /* Direct Mode Error Interrupt */
43 #define STM32_DMA_FEI			BIT(0) /* FIFO Error Interrupt */
44 #define STM32_DMA_MASKI			(STM32_DMA_TCI \
45 					 | STM32_DMA_TEI \
46 					 | STM32_DMA_DMEI \
47 					 | STM32_DMA_FEI)
48 /*
49  * If (chan->id % 4) is 2 or 3, left shift the mask by 16 bits;
50  * if (ch % 4) is 1 or 3, additionally left shift the mask by 6 bits.
51  */
52 #define STM32_DMA_FLAGS_SHIFT(n)	({ typeof(n) (_n) = (n); \
53 					   (((_n) & 2) << 3) | (((_n) & 1) * 6); })
54 
55 /* DMA Stream x Configuration Register */
56 #define STM32_DMA_SCR(x)		(0x0010 + 0x18 * (x)) /* x = 0..7 */
57 #define STM32_DMA_SCR_REQ_MASK		GENMASK(27, 25)
58 #define STM32_DMA_SCR_MBURST_MASK	GENMASK(24, 23)
59 #define STM32_DMA_SCR_PBURST_MASK	GENMASK(22, 21)
60 #define STM32_DMA_SCR_PL_MASK		GENMASK(17, 16)
61 #define STM32_DMA_SCR_MSIZE_MASK	GENMASK(14, 13)
62 #define STM32_DMA_SCR_PSIZE_MASK	GENMASK(12, 11)
63 #define STM32_DMA_SCR_DIR_MASK		GENMASK(7, 6)
64 #define STM32_DMA_SCR_TRBUFF		BIT(20) /* Bufferable transfer for USART/UART */
65 #define STM32_DMA_SCR_CT		BIT(19) /* Target in double buffer */
66 #define STM32_DMA_SCR_DBM		BIT(18) /* Double Buffer Mode */
67 #define STM32_DMA_SCR_PINCOS		BIT(15) /* Peripheral inc offset size */
68 #define STM32_DMA_SCR_MINC		BIT(10) /* Memory increment mode */
69 #define STM32_DMA_SCR_PINC		BIT(9) /* Peripheral increment mode */
70 #define STM32_DMA_SCR_CIRC		BIT(8) /* Circular mode */
71 #define STM32_DMA_SCR_PFCTRL		BIT(5) /* Peripheral Flow Controller */
72 #define STM32_DMA_SCR_TCIE		BIT(4) /* Transfer Complete Int Enable
73 						*/
74 #define STM32_DMA_SCR_TEIE		BIT(2) /* Transfer Error Int Enable */
75 #define STM32_DMA_SCR_DMEIE		BIT(1) /* Direct Mode Err Int Enable */
76 #define STM32_DMA_SCR_EN		BIT(0) /* Stream Enable */
77 #define STM32_DMA_SCR_CFG_MASK		(STM32_DMA_SCR_PINC \
78 					| STM32_DMA_SCR_MINC \
79 					| STM32_DMA_SCR_PINCOS \
80 					| STM32_DMA_SCR_PL_MASK)
81 #define STM32_DMA_SCR_IRQ_MASK		(STM32_DMA_SCR_TCIE \
82 					| STM32_DMA_SCR_TEIE \
83 					| STM32_DMA_SCR_DMEIE)
84 
85 /* DMA Stream x number of data register */
86 #define STM32_DMA_SNDTR(x)		(0x0014 + 0x18 * (x))
87 
88 /* DMA stream peripheral address register */
89 #define STM32_DMA_SPAR(x)		(0x0018 + 0x18 * (x))
90 
91 /* DMA stream x memory 0 address register */
92 #define STM32_DMA_SM0AR(x)		(0x001c + 0x18 * (x))
93 
94 /* DMA stream x memory 1 address register */
95 #define STM32_DMA_SM1AR(x)		(0x0020 + 0x18 * (x))
96 
97 /* DMA stream x FIFO control register */
98 #define STM32_DMA_SFCR(x)		(0x0024 + 0x18 * (x))
99 #define STM32_DMA_SFCR_FTH_MASK		GENMASK(1, 0)
100 #define STM32_DMA_SFCR_FEIE		BIT(7) /* FIFO error interrupt enable */
101 #define STM32_DMA_SFCR_DMDIS		BIT(2) /* Direct mode disable */
102 #define STM32_DMA_SFCR_MASK		(STM32_DMA_SFCR_FEIE \
103 					| STM32_DMA_SFCR_DMDIS)
104 
105 /* DMA direction */
106 #define STM32_DMA_DEV_TO_MEM		0x00
107 #define	STM32_DMA_MEM_TO_DEV		0x01
108 #define	STM32_DMA_MEM_TO_MEM		0x02
109 
110 /* DMA priority level */
111 #define STM32_DMA_PRIORITY_LOW		0x00
112 #define STM32_DMA_PRIORITY_MEDIUM	0x01
113 #define STM32_DMA_PRIORITY_HIGH		0x02
114 #define STM32_DMA_PRIORITY_VERY_HIGH	0x03
115 
116 /* DMA FIFO threshold selection */
117 #define STM32_DMA_FIFO_THRESHOLD_1QUARTERFULL		0x00
118 #define STM32_DMA_FIFO_THRESHOLD_HALFFULL		0x01
119 #define STM32_DMA_FIFO_THRESHOLD_3QUARTERSFULL		0x02
120 #define STM32_DMA_FIFO_THRESHOLD_FULL			0x03
121 #define STM32_DMA_FIFO_THRESHOLD_NONE			0x04
122 
123 #define STM32_DMA_MAX_DATA_ITEMS	0xffff
124 /*
125  * Valid transfer starts from @0 to @0xFFFE leading to unaligned scatter
126  * gather at boundary. Thus it's safer to round down this value on FIFO
127  * size (16 Bytes)
128  */
129 #define STM32_DMA_ALIGNED_MAX_DATA_ITEMS	\
130 	ALIGN_DOWN(STM32_DMA_MAX_DATA_ITEMS, 16)
131 #define STM32_DMA_MAX_CHANNELS		0x08
132 #define STM32_DMA_MAX_REQUEST_ID	0x08
133 #define STM32_DMA_MAX_DATA_PARAM	0x03
134 #define STM32_DMA_FIFO_SIZE		16	/* FIFO is 16 bytes */
135 #define STM32_DMA_MIN_BURST		4
136 #define STM32_DMA_MAX_BURST		16
137 
138 /* DMA Features */
139 #define STM32_DMA_THRESHOLD_FTR_MASK	GENMASK(1, 0)
140 #define STM32_DMA_DIRECT_MODE_MASK	BIT(2)
141 #define STM32_DMA_ALT_ACK_MODE_MASK	BIT(4)
142 #define STM32_DMA_MDMA_STREAM_ID_MASK	GENMASK(19, 16)
143 
144 enum stm32_dma_width {
145 	STM32_DMA_BYTE,
146 	STM32_DMA_HALF_WORD,
147 	STM32_DMA_WORD,
148 };
149 
150 enum stm32_dma_burst_size {
151 	STM32_DMA_BURST_SINGLE,
152 	STM32_DMA_BURST_INCR4,
153 	STM32_DMA_BURST_INCR8,
154 	STM32_DMA_BURST_INCR16,
155 };
156 
157 /**
158  * struct stm32_dma_cfg - STM32 DMA custom configuration
159  * @channel_id: channel ID
160  * @request_line: DMA request
161  * @stream_config: 32bit mask specifying the DMA channel configuration
162  * @features: 32bit mask specifying the DMA Feature list
163  */
164 struct stm32_dma_cfg {
165 	u32 channel_id;
166 	u32 request_line;
167 	u32 stream_config;
168 	u32 features;
169 };
170 
171 struct stm32_dma_chan_reg {
172 	u32 dma_lisr;
173 	u32 dma_hisr;
174 	u32 dma_lifcr;
175 	u32 dma_hifcr;
176 	u32 dma_scr;
177 	u32 dma_sndtr;
178 	u32 dma_spar;
179 	u32 dma_sm0ar;
180 	u32 dma_sm1ar;
181 	u32 dma_sfcr;
182 };
183 
184 struct stm32_dma_sg_req {
185 	u32 len;
186 	struct stm32_dma_chan_reg chan_reg;
187 };
188 
189 struct stm32_dma_desc {
190 	struct virt_dma_desc vdesc;
191 	bool cyclic;
192 	u32 num_sgs;
193 	struct stm32_dma_sg_req sg_req[] __counted_by(num_sgs);
194 };
195 
196 /**
197  * struct stm32_dma_mdma_config - STM32 DMA MDMA configuration
198  * @stream_id: DMA request to trigger STM32 MDMA transfer
199  * @ifcr: DMA interrupt flag clear register address,
200  *        used by STM32 MDMA to clear DMA Transfer Complete flag
201  * @tcf: DMA Transfer Complete flag
202  */
203 struct stm32_dma_mdma_config {
204 	u32 stream_id;
205 	u32 ifcr;
206 	u32 tcf;
207 };
208 
209 struct stm32_dma_chan {
210 	struct virt_dma_chan vchan;
211 	bool config_init;
212 	bool busy;
213 	u32 id;
214 	u32 irq;
215 	struct stm32_dma_desc *desc;
216 	u32 next_sg;
217 	struct dma_slave_config	dma_sconfig;
218 	struct stm32_dma_chan_reg chan_reg;
219 	u32 threshold;
220 	u32 mem_burst;
221 	u32 mem_width;
222 	enum dma_status status;
223 	bool trig_mdma;
224 	struct stm32_dma_mdma_config mdma_config;
225 };
226 
227 struct stm32_dma_device {
228 	struct dma_device ddev;
229 	void __iomem *base;
230 	struct clk *clk;
231 	bool mem2mem;
232 	struct stm32_dma_chan chan[STM32_DMA_MAX_CHANNELS];
233 };
234 
235 static struct stm32_dma_device *stm32_dma_get_dev(struct stm32_dma_chan *chan)
236 {
237 	return container_of(chan->vchan.chan.device, struct stm32_dma_device,
238 			    ddev);
239 }
240 
241 static struct stm32_dma_chan *to_stm32_dma_chan(struct dma_chan *c)
242 {
243 	return container_of(c, struct stm32_dma_chan, vchan.chan);
244 }
245 
246 static struct stm32_dma_desc *to_stm32_dma_desc(struct virt_dma_desc *vdesc)
247 {
248 	return container_of(vdesc, struct stm32_dma_desc, vdesc);
249 }
250 
251 static u32 stm32_dma_read(struct stm32_dma_device *dmadev, u32 reg)
252 {
253 	return readl_relaxed(dmadev->base + reg);
254 }
255 
256 static void stm32_dma_write(struct stm32_dma_device *dmadev, u32 reg, u32 val)
257 {
258 	writel_relaxed(val, dmadev->base + reg);
259 }
260 
261 static int stm32_dma_get_width(struct stm32_dma_chan *chan,
262 			       enum dma_slave_buswidth width)
263 {
264 	switch (width) {
265 	case DMA_SLAVE_BUSWIDTH_1_BYTE:
266 		return STM32_DMA_BYTE;
267 	case DMA_SLAVE_BUSWIDTH_2_BYTES:
268 		return STM32_DMA_HALF_WORD;
269 	case DMA_SLAVE_BUSWIDTH_4_BYTES:
270 		return STM32_DMA_WORD;
271 	default:
272 		dev_err(vchan_chan_dev(&chan->vchan), "Dma bus width not supported\n");
273 		return -EINVAL;
274 	}
275 }
276 
277 static enum dma_slave_buswidth stm32_dma_get_max_width(u32 buf_len,
278 						       dma_addr_t buf_addr,
279 						       u32 threshold)
280 {
281 	enum dma_slave_buswidth max_width;
282 
283 	if (threshold == STM32_DMA_FIFO_THRESHOLD_FULL)
284 		max_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
285 	else
286 		max_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
287 
288 	while ((buf_len < max_width  || buf_len % max_width) &&
289 	       max_width > DMA_SLAVE_BUSWIDTH_1_BYTE)
290 		max_width = max_width >> 1;
291 
292 	if (buf_addr & (max_width - 1))
293 		max_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
294 
295 	return max_width;
296 }
297 
298 static bool stm32_dma_fifo_threshold_is_allowed(u32 burst, u32 threshold,
299 						enum dma_slave_buswidth width)
300 {
301 	u32 remaining;
302 
303 	if (threshold == STM32_DMA_FIFO_THRESHOLD_NONE)
304 		return false;
305 
306 	if (width != DMA_SLAVE_BUSWIDTH_UNDEFINED) {
307 		if (burst != 0) {
308 			/*
309 			 * If number of beats fit in several whole bursts
310 			 * this configuration is allowed.
311 			 */
312 			remaining = ((STM32_DMA_FIFO_SIZE / width) *
313 				     (threshold + 1) / 4) % burst;
314 
315 			if (remaining == 0)
316 				return true;
317 		} else {
318 			return true;
319 		}
320 	}
321 
322 	return false;
323 }
324 
325 static bool stm32_dma_is_burst_possible(u32 buf_len, u32 threshold)
326 {
327 	/* If FIFO direct mode, burst is not possible */
328 	if (threshold == STM32_DMA_FIFO_THRESHOLD_NONE)
329 		return false;
330 
331 	/*
332 	 * Buffer or period length has to be aligned on FIFO depth.
333 	 * Otherwise bytes may be stuck within FIFO at buffer or period
334 	 * length.
335 	 */
336 	return ((buf_len % ((threshold + 1) * 4)) == 0);
337 }
338 
339 static u32 stm32_dma_get_best_burst(u32 buf_len, u32 max_burst, u32 threshold,
340 				    enum dma_slave_buswidth width)
341 {
342 	u32 best_burst = max_burst;
343 
344 	if (best_burst == 1 || !stm32_dma_is_burst_possible(buf_len, threshold))
345 		return 0;
346 
347 	while ((buf_len < best_burst * width && best_burst > 1) ||
348 	       !stm32_dma_fifo_threshold_is_allowed(best_burst, threshold,
349 						    width)) {
350 		if (best_burst > STM32_DMA_MIN_BURST)
351 			best_burst = best_burst >> 1;
352 		else
353 			best_burst = 0;
354 	}
355 
356 	return best_burst;
357 }
358 
359 static int stm32_dma_get_burst(struct stm32_dma_chan *chan, u32 maxburst)
360 {
361 	switch (maxburst) {
362 	case 0:
363 	case 1:
364 		return STM32_DMA_BURST_SINGLE;
365 	case 4:
366 		return STM32_DMA_BURST_INCR4;
367 	case 8:
368 		return STM32_DMA_BURST_INCR8;
369 	case 16:
370 		return STM32_DMA_BURST_INCR16;
371 	default:
372 		dev_err(vchan_chan_dev(&chan->vchan), "Dma burst size not supported\n");
373 		return -EINVAL;
374 	}
375 }
376 
377 static void stm32_dma_set_fifo_config(struct stm32_dma_chan *chan,
378 				      u32 src_burst, u32 dst_burst)
379 {
380 	chan->chan_reg.dma_sfcr &= ~STM32_DMA_SFCR_MASK;
381 	chan->chan_reg.dma_scr &= ~STM32_DMA_SCR_DMEIE;
382 
383 	if (!src_burst && !dst_burst) {
384 		/* Using direct mode */
385 		chan->chan_reg.dma_scr |= STM32_DMA_SCR_DMEIE;
386 	} else {
387 		/* Using FIFO mode */
388 		chan->chan_reg.dma_sfcr |= STM32_DMA_SFCR_MASK;
389 	}
390 }
391 
392 static int stm32_dma_slave_config(struct dma_chan *c,
393 				  struct dma_slave_config *config)
394 {
395 	struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
396 
397 	memcpy(&chan->dma_sconfig, config, sizeof(*config));
398 
399 	/* Check if user is requesting DMA to trigger STM32 MDMA */
400 	if (config->peripheral_size) {
401 		config->peripheral_config = &chan->mdma_config;
402 		config->peripheral_size = sizeof(chan->mdma_config);
403 		chan->trig_mdma = true;
404 	}
405 
406 	chan->config_init = true;
407 
408 	return 0;
409 }
410 
411 static u32 stm32_dma_irq_status(struct stm32_dma_chan *chan)
412 {
413 	struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
414 	u32 flags, dma_isr;
415 
416 	/*
417 	 * Read "flags" from DMA_xISR register corresponding to the selected
418 	 * DMA channel at the correct bit offset inside that register.
419 	 */
420 
421 	dma_isr = stm32_dma_read(dmadev, STM32_DMA_ISR(chan->id));
422 	flags = dma_isr >> STM32_DMA_FLAGS_SHIFT(chan->id);
423 
424 	return flags & STM32_DMA_MASKI;
425 }
426 
427 static void stm32_dma_irq_clear(struct stm32_dma_chan *chan, u32 flags)
428 {
429 	struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
430 	u32 dma_ifcr;
431 
432 	/*
433 	 * Write "flags" to the DMA_xIFCR register corresponding to the selected
434 	 * DMA channel at the correct bit offset inside that register.
435 	 */
436 	flags &= STM32_DMA_MASKI;
437 	dma_ifcr = flags << STM32_DMA_FLAGS_SHIFT(chan->id);
438 
439 	stm32_dma_write(dmadev, STM32_DMA_IFCR(chan->id), dma_ifcr);
440 }
441 
442 static int stm32_dma_disable_chan(struct stm32_dma_chan *chan)
443 {
444 	struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
445 	u32 dma_scr, id, reg;
446 
447 	id = chan->id;
448 	reg = STM32_DMA_SCR(id);
449 	dma_scr = stm32_dma_read(dmadev, reg);
450 
451 	if (dma_scr & STM32_DMA_SCR_EN) {
452 		dma_scr &= ~STM32_DMA_SCR_EN;
453 		stm32_dma_write(dmadev, reg, dma_scr);
454 
455 		return readl_relaxed_poll_timeout_atomic(dmadev->base + reg,
456 					dma_scr, !(dma_scr & STM32_DMA_SCR_EN),
457 					10, 1000000);
458 	}
459 
460 	return 0;
461 }
462 
463 static void stm32_dma_stop(struct stm32_dma_chan *chan)
464 {
465 	struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
466 	u32 dma_scr, dma_sfcr, status;
467 	int ret;
468 
469 	/* Disable interrupts */
470 	dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id));
471 	dma_scr &= ~STM32_DMA_SCR_IRQ_MASK;
472 	stm32_dma_write(dmadev, STM32_DMA_SCR(chan->id), dma_scr);
473 	dma_sfcr = stm32_dma_read(dmadev, STM32_DMA_SFCR(chan->id));
474 	dma_sfcr &= ~STM32_DMA_SFCR_FEIE;
475 	stm32_dma_write(dmadev, STM32_DMA_SFCR(chan->id), dma_sfcr);
476 
477 	/* Disable DMA */
478 	ret = stm32_dma_disable_chan(chan);
479 	if (ret < 0)
480 		return;
481 
482 	/* Clear interrupt status if it is there */
483 	status = stm32_dma_irq_status(chan);
484 	if (status) {
485 		dev_dbg(vchan_chan_dev(&chan->vchan), "%s(): clearing interrupt: 0x%08x\n",
486 			__func__, status);
487 		stm32_dma_irq_clear(chan, status);
488 	}
489 
490 	chan->busy = false;
491 	chan->status = DMA_COMPLETE;
492 }
493 
494 static int stm32_dma_terminate_all(struct dma_chan *c)
495 {
496 	struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
497 	unsigned long flags;
498 	LIST_HEAD(head);
499 
500 	spin_lock_irqsave(&chan->vchan.lock, flags);
501 
502 	if (chan->desc) {
503 		dma_cookie_complete(&chan->desc->vdesc.tx);
504 		vchan_terminate_vdesc(&chan->desc->vdesc);
505 		if (chan->busy)
506 			stm32_dma_stop(chan);
507 		chan->desc = NULL;
508 	}
509 
510 	vchan_get_all_descriptors(&chan->vchan, &head);
511 	spin_unlock_irqrestore(&chan->vchan.lock, flags);
512 	vchan_dma_desc_free_list(&chan->vchan, &head);
513 
514 	return 0;
515 }
516 
517 static void stm32_dma_synchronize(struct dma_chan *c)
518 {
519 	struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
520 
521 	vchan_synchronize(&chan->vchan);
522 }
523 
524 static void stm32_dma_dump_reg(struct stm32_dma_chan *chan)
525 {
526 	struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
527 	u32 scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id));
528 	u32 ndtr = stm32_dma_read(dmadev, STM32_DMA_SNDTR(chan->id));
529 	u32 spar = stm32_dma_read(dmadev, STM32_DMA_SPAR(chan->id));
530 	u32 sm0ar = stm32_dma_read(dmadev, STM32_DMA_SM0AR(chan->id));
531 	u32 sm1ar = stm32_dma_read(dmadev, STM32_DMA_SM1AR(chan->id));
532 	u32 sfcr = stm32_dma_read(dmadev, STM32_DMA_SFCR(chan->id));
533 
534 	dev_dbg(vchan_chan_dev(&chan->vchan), "SCR:   0x%08x\n", scr);
535 	dev_dbg(vchan_chan_dev(&chan->vchan), "NDTR:  0x%08x\n", ndtr);
536 	dev_dbg(vchan_chan_dev(&chan->vchan), "SPAR:  0x%08x\n", spar);
537 	dev_dbg(vchan_chan_dev(&chan->vchan), "SM0AR: 0x%08x\n", sm0ar);
538 	dev_dbg(vchan_chan_dev(&chan->vchan), "SM1AR: 0x%08x\n", sm1ar);
539 	dev_dbg(vchan_chan_dev(&chan->vchan), "SFCR:  0x%08x\n", sfcr);
540 }
541 
542 static void stm32_dma_sg_inc(struct stm32_dma_chan *chan)
543 {
544 	chan->next_sg++;
545 	if (chan->desc->cyclic && (chan->next_sg == chan->desc->num_sgs))
546 		chan->next_sg = 0;
547 }
548 
549 static void stm32_dma_configure_next_sg(struct stm32_dma_chan *chan);
550 
551 static void stm32_dma_start_transfer(struct stm32_dma_chan *chan)
552 {
553 	struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
554 	struct virt_dma_desc *vdesc;
555 	struct stm32_dma_sg_req *sg_req;
556 	struct stm32_dma_chan_reg *reg;
557 	u32 status;
558 	int ret;
559 
560 	ret = stm32_dma_disable_chan(chan);
561 	if (ret < 0)
562 		return;
563 
564 	if (!chan->desc) {
565 		vdesc = vchan_next_desc(&chan->vchan);
566 		if (!vdesc)
567 			return;
568 
569 		list_del(&vdesc->node);
570 
571 		chan->desc = to_stm32_dma_desc(vdesc);
572 		chan->next_sg = 0;
573 	}
574 
575 	if (chan->next_sg == chan->desc->num_sgs)
576 		chan->next_sg = 0;
577 
578 	sg_req = &chan->desc->sg_req[chan->next_sg];
579 	reg = &sg_req->chan_reg;
580 
581 	/* When DMA triggers STM32 MDMA, DMA Transfer Complete is managed by STM32 MDMA */
582 	if (chan->trig_mdma && chan->dma_sconfig.direction != DMA_MEM_TO_DEV)
583 		reg->dma_scr &= ~STM32_DMA_SCR_TCIE;
584 
585 	reg->dma_scr &= ~STM32_DMA_SCR_EN;
586 	stm32_dma_write(dmadev, STM32_DMA_SCR(chan->id), reg->dma_scr);
587 	stm32_dma_write(dmadev, STM32_DMA_SPAR(chan->id), reg->dma_spar);
588 	stm32_dma_write(dmadev, STM32_DMA_SM0AR(chan->id), reg->dma_sm0ar);
589 	stm32_dma_write(dmadev, STM32_DMA_SFCR(chan->id), reg->dma_sfcr);
590 	stm32_dma_write(dmadev, STM32_DMA_SM1AR(chan->id), reg->dma_sm1ar);
591 	stm32_dma_write(dmadev, STM32_DMA_SNDTR(chan->id), reg->dma_sndtr);
592 
593 	stm32_dma_sg_inc(chan);
594 
595 	/* Clear interrupt status if it is there */
596 	status = stm32_dma_irq_status(chan);
597 	if (status)
598 		stm32_dma_irq_clear(chan, status);
599 
600 	if (chan->desc->cyclic)
601 		stm32_dma_configure_next_sg(chan);
602 
603 	stm32_dma_dump_reg(chan);
604 
605 	/* Start DMA */
606 	chan->busy = true;
607 	chan->status = DMA_IN_PROGRESS;
608 	reg->dma_scr |= STM32_DMA_SCR_EN;
609 	stm32_dma_write(dmadev, STM32_DMA_SCR(chan->id), reg->dma_scr);
610 
611 	dev_dbg(vchan_chan_dev(&chan->vchan), "vchan %p: started\n", &chan->vchan);
612 }
613 
614 static void stm32_dma_configure_next_sg(struct stm32_dma_chan *chan)
615 {
616 	struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
617 	struct stm32_dma_sg_req *sg_req;
618 	u32 dma_scr, dma_sm0ar, dma_sm1ar, id;
619 
620 	id = chan->id;
621 	dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(id));
622 
623 	sg_req = &chan->desc->sg_req[chan->next_sg];
624 
625 	if (dma_scr & STM32_DMA_SCR_CT) {
626 		dma_sm0ar = sg_req->chan_reg.dma_sm0ar;
627 		stm32_dma_write(dmadev, STM32_DMA_SM0AR(id), dma_sm0ar);
628 		dev_dbg(vchan_chan_dev(&chan->vchan), "CT=1 <=> SM0AR: 0x%08x\n",
629 			stm32_dma_read(dmadev, STM32_DMA_SM0AR(id)));
630 	} else {
631 		dma_sm1ar = sg_req->chan_reg.dma_sm1ar;
632 		stm32_dma_write(dmadev, STM32_DMA_SM1AR(id), dma_sm1ar);
633 		dev_dbg(vchan_chan_dev(&chan->vchan), "CT=0 <=> SM1AR: 0x%08x\n",
634 			stm32_dma_read(dmadev, STM32_DMA_SM1AR(id)));
635 	}
636 }
637 
638 static void stm32_dma_handle_chan_paused(struct stm32_dma_chan *chan)
639 {
640 	struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
641 	u32 dma_scr;
642 
643 	/*
644 	 * Read and store current remaining data items and peripheral/memory addresses to be
645 	 * updated on resume
646 	 */
647 	dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id));
648 	/*
649 	 * Transfer can be paused while between a previous resume and reconfiguration on transfer
650 	 * complete. If transfer is cyclic and CIRC and DBM have been deactivated for resume, need
651 	 * to set it here in SCR backup to ensure a good reconfiguration on transfer complete.
652 	 */
653 	if (chan->desc && chan->desc->cyclic) {
654 		if (chan->desc->num_sgs == 1)
655 			dma_scr |= STM32_DMA_SCR_CIRC;
656 		else
657 			dma_scr |= STM32_DMA_SCR_DBM;
658 	}
659 	chan->chan_reg.dma_scr = dma_scr;
660 
661 	/*
662 	 * Need to temporarily deactivate CIRC/DBM until next Transfer Complete interrupt, otherwise
663 	 * on resume NDTR autoreload value will be wrong (lower than the initial period length)
664 	 */
665 	if (chan->desc && chan->desc->cyclic) {
666 		dma_scr &= ~(STM32_DMA_SCR_DBM | STM32_DMA_SCR_CIRC);
667 		stm32_dma_write(dmadev, STM32_DMA_SCR(chan->id), dma_scr);
668 	}
669 
670 	chan->chan_reg.dma_sndtr = stm32_dma_read(dmadev, STM32_DMA_SNDTR(chan->id));
671 
672 	chan->status = DMA_PAUSED;
673 
674 	dev_dbg(vchan_chan_dev(&chan->vchan), "vchan %p: paused\n", &chan->vchan);
675 }
676 
677 static void stm32_dma_post_resume_reconfigure(struct stm32_dma_chan *chan)
678 {
679 	struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
680 	struct stm32_dma_sg_req *sg_req;
681 	u32 dma_scr, status, id;
682 
683 	id = chan->id;
684 	dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(id));
685 
686 	/* Clear interrupt status if it is there */
687 	status = stm32_dma_irq_status(chan);
688 	if (status)
689 		stm32_dma_irq_clear(chan, status);
690 
691 	if (!chan->next_sg)
692 		sg_req = &chan->desc->sg_req[chan->desc->num_sgs - 1];
693 	else
694 		sg_req = &chan->desc->sg_req[chan->next_sg - 1];
695 
696 	/* Reconfigure NDTR with the initial value */
697 	stm32_dma_write(dmadev, STM32_DMA_SNDTR(chan->id), sg_req->chan_reg.dma_sndtr);
698 
699 	/* Restore SPAR */
700 	stm32_dma_write(dmadev, STM32_DMA_SPAR(id), sg_req->chan_reg.dma_spar);
701 
702 	/* Restore SM0AR/SM1AR whatever DBM/CT as they may have been modified */
703 	stm32_dma_write(dmadev, STM32_DMA_SM0AR(id), sg_req->chan_reg.dma_sm0ar);
704 	stm32_dma_write(dmadev, STM32_DMA_SM1AR(id), sg_req->chan_reg.dma_sm1ar);
705 
706 	/* Reactivate CIRC/DBM if needed */
707 	if (chan->chan_reg.dma_scr & STM32_DMA_SCR_DBM) {
708 		dma_scr |= STM32_DMA_SCR_DBM;
709 		/* Restore CT */
710 		if (chan->chan_reg.dma_scr & STM32_DMA_SCR_CT)
711 			dma_scr &= ~STM32_DMA_SCR_CT;
712 		else
713 			dma_scr |= STM32_DMA_SCR_CT;
714 	} else if (chan->chan_reg.dma_scr & STM32_DMA_SCR_CIRC) {
715 		dma_scr |= STM32_DMA_SCR_CIRC;
716 	}
717 	stm32_dma_write(dmadev, STM32_DMA_SCR(chan->id), dma_scr);
718 
719 	stm32_dma_configure_next_sg(chan);
720 
721 	stm32_dma_dump_reg(chan);
722 
723 	dma_scr |= STM32_DMA_SCR_EN;
724 	stm32_dma_write(dmadev, STM32_DMA_SCR(chan->id), dma_scr);
725 
726 	dev_dbg(vchan_chan_dev(&chan->vchan), "vchan %p: reconfigured after pause/resume\n", &chan->vchan);
727 }
728 
729 static void stm32_dma_handle_chan_done(struct stm32_dma_chan *chan, u32 scr)
730 {
731 	if (!chan->desc)
732 		return;
733 
734 	if (chan->desc->cyclic) {
735 		vchan_cyclic_callback(&chan->desc->vdesc);
736 		if (chan->trig_mdma)
737 			return;
738 		stm32_dma_sg_inc(chan);
739 		/* cyclic while CIRC/DBM disable => post resume reconfiguration needed */
740 		if (!(scr & (STM32_DMA_SCR_CIRC | STM32_DMA_SCR_DBM)))
741 			stm32_dma_post_resume_reconfigure(chan);
742 		else if (scr & STM32_DMA_SCR_DBM && chan->desc->num_sgs > 2)
743 			stm32_dma_configure_next_sg(chan);
744 	} else {
745 		chan->busy = false;
746 		chan->status = DMA_COMPLETE;
747 		if (chan->next_sg == chan->desc->num_sgs) {
748 			vchan_cookie_complete(&chan->desc->vdesc);
749 			chan->desc = NULL;
750 		}
751 		stm32_dma_start_transfer(chan);
752 	}
753 }
754 
755 static irqreturn_t stm32_dma_chan_irq(int irq, void *devid)
756 {
757 	struct stm32_dma_chan *chan = devid;
758 	struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
759 	u32 status, scr, sfcr;
760 
761 	spin_lock(&chan->vchan.lock);
762 
763 	status = stm32_dma_irq_status(chan);
764 	scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id));
765 	sfcr = stm32_dma_read(dmadev, STM32_DMA_SFCR(chan->id));
766 
767 	if (status & STM32_DMA_FEI) {
768 		stm32_dma_irq_clear(chan, STM32_DMA_FEI);
769 		status &= ~STM32_DMA_FEI;
770 		if (sfcr & STM32_DMA_SFCR_FEIE) {
771 			if (!(scr & STM32_DMA_SCR_EN) &&
772 			    !(status & STM32_DMA_TCI))
773 				dev_err(vchan_chan_dev(&chan->vchan), "FIFO Error\n");
774 			else
775 				dev_dbg(vchan_chan_dev(&chan->vchan), "FIFO over/underrun\n");
776 		}
777 	}
778 	if (status & STM32_DMA_DMEI) {
779 		stm32_dma_irq_clear(chan, STM32_DMA_DMEI);
780 		status &= ~STM32_DMA_DMEI;
781 		if (sfcr & STM32_DMA_SCR_DMEIE)
782 			dev_dbg(vchan_chan_dev(&chan->vchan), "Direct mode overrun\n");
783 	}
784 
785 	if (status & STM32_DMA_TCI) {
786 		stm32_dma_irq_clear(chan, STM32_DMA_TCI);
787 		if (scr & STM32_DMA_SCR_TCIE) {
788 			if (chan->status != DMA_PAUSED)
789 				stm32_dma_handle_chan_done(chan, scr);
790 		}
791 		status &= ~STM32_DMA_TCI;
792 	}
793 
794 	if (status & STM32_DMA_HTI) {
795 		stm32_dma_irq_clear(chan, STM32_DMA_HTI);
796 		status &= ~STM32_DMA_HTI;
797 	}
798 
799 	if (status) {
800 		stm32_dma_irq_clear(chan, status);
801 		dev_err(vchan_chan_dev(&chan->vchan), "DMA error: status=0x%08x\n", status);
802 		if (!(scr & STM32_DMA_SCR_EN))
803 			dev_err(vchan_chan_dev(&chan->vchan), "chan disabled by HW\n");
804 	}
805 
806 	spin_unlock(&chan->vchan.lock);
807 
808 	return IRQ_HANDLED;
809 }
810 
811 static void stm32_dma_issue_pending(struct dma_chan *c)
812 {
813 	struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
814 	unsigned long flags;
815 
816 	spin_lock_irqsave(&chan->vchan.lock, flags);
817 	if (vchan_issue_pending(&chan->vchan) && !chan->desc && !chan->busy) {
818 		dev_dbg(vchan_chan_dev(&chan->vchan), "vchan %p: issued\n", &chan->vchan);
819 		stm32_dma_start_transfer(chan);
820 
821 	}
822 	spin_unlock_irqrestore(&chan->vchan.lock, flags);
823 }
824 
825 static int stm32_dma_pause(struct dma_chan *c)
826 {
827 	struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
828 	unsigned long flags;
829 	int ret;
830 
831 	if (chan->status != DMA_IN_PROGRESS)
832 		return -EPERM;
833 
834 	spin_lock_irqsave(&chan->vchan.lock, flags);
835 
836 	ret = stm32_dma_disable_chan(chan);
837 	if (!ret)
838 		stm32_dma_handle_chan_paused(chan);
839 
840 	spin_unlock_irqrestore(&chan->vchan.lock, flags);
841 
842 	return ret;
843 }
844 
845 static int stm32_dma_resume(struct dma_chan *c)
846 {
847 	struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
848 	struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
849 	struct stm32_dma_chan_reg chan_reg = chan->chan_reg;
850 	u32 id = chan->id, scr, ndtr, offset, spar, sm0ar, sm1ar;
851 	struct stm32_dma_sg_req *sg_req;
852 	unsigned long flags;
853 
854 	if (chan->status != DMA_PAUSED)
855 		return -EPERM;
856 
857 	scr = stm32_dma_read(dmadev, STM32_DMA_SCR(id));
858 	if (WARN_ON(scr & STM32_DMA_SCR_EN))
859 		return -EPERM;
860 
861 	spin_lock_irqsave(&chan->vchan.lock, flags);
862 
863 	/* sg_reg[prev_sg] contains original ndtr, sm0ar and sm1ar before pausing the transfer */
864 	if (!chan->next_sg)
865 		sg_req = &chan->desc->sg_req[chan->desc->num_sgs - 1];
866 	else
867 		sg_req = &chan->desc->sg_req[chan->next_sg - 1];
868 
869 	ndtr = sg_req->chan_reg.dma_sndtr;
870 	offset = (ndtr - chan_reg.dma_sndtr);
871 	offset <<= FIELD_GET(STM32_DMA_SCR_PSIZE_MASK, chan_reg.dma_scr);
872 	spar = sg_req->chan_reg.dma_spar;
873 	sm0ar = sg_req->chan_reg.dma_sm0ar;
874 	sm1ar = sg_req->chan_reg.dma_sm1ar;
875 
876 	/*
877 	 * The peripheral and/or memory addresses have to be updated in order to adjust the
878 	 * address pointers. Need to check increment.
879 	 */
880 	if (chan_reg.dma_scr & STM32_DMA_SCR_PINC)
881 		stm32_dma_write(dmadev, STM32_DMA_SPAR(id), spar + offset);
882 	else
883 		stm32_dma_write(dmadev, STM32_DMA_SPAR(id), spar);
884 
885 	if (!(chan_reg.dma_scr & STM32_DMA_SCR_MINC))
886 		offset = 0;
887 
888 	/*
889 	 * In case of DBM, the current target could be SM1AR.
890 	 * Need to temporarily deactivate CIRC/DBM to finish the current transfer, so
891 	 * SM0AR becomes the current target and must be updated with SM1AR + offset if CT=1.
892 	 */
893 	if ((chan_reg.dma_scr & STM32_DMA_SCR_DBM) && (chan_reg.dma_scr & STM32_DMA_SCR_CT))
894 		stm32_dma_write(dmadev, STM32_DMA_SM1AR(id), sm1ar + offset);
895 	else
896 		stm32_dma_write(dmadev, STM32_DMA_SM0AR(id), sm0ar + offset);
897 
898 	/* NDTR must be restored otherwise internal HW counter won't be correctly reset */
899 	stm32_dma_write(dmadev, STM32_DMA_SNDTR(id), chan_reg.dma_sndtr);
900 
901 	/*
902 	 * Need to temporarily deactivate CIRC/DBM until next Transfer Complete interrupt,
903 	 * otherwise NDTR autoreload value will be wrong (lower than the initial period length)
904 	 */
905 	if (chan_reg.dma_scr & (STM32_DMA_SCR_CIRC | STM32_DMA_SCR_DBM))
906 		chan_reg.dma_scr &= ~(STM32_DMA_SCR_CIRC | STM32_DMA_SCR_DBM);
907 
908 	if (chan_reg.dma_scr & STM32_DMA_SCR_DBM)
909 		stm32_dma_configure_next_sg(chan);
910 
911 	stm32_dma_dump_reg(chan);
912 
913 	/* The stream may then be re-enabled to restart transfer from the point it was stopped */
914 	chan->status = DMA_IN_PROGRESS;
915 	chan_reg.dma_scr |= STM32_DMA_SCR_EN;
916 	stm32_dma_write(dmadev, STM32_DMA_SCR(id), chan_reg.dma_scr);
917 
918 	spin_unlock_irqrestore(&chan->vchan.lock, flags);
919 
920 	dev_dbg(vchan_chan_dev(&chan->vchan), "vchan %p: resumed\n", &chan->vchan);
921 
922 	return 0;
923 }
924 
925 static int stm32_dma_set_xfer_param(struct stm32_dma_chan *chan,
926 				    enum dma_transfer_direction direction,
927 				    enum dma_slave_buswidth *buswidth,
928 				    u32 buf_len, dma_addr_t buf_addr)
929 {
930 	enum dma_slave_buswidth src_addr_width, dst_addr_width;
931 	int src_bus_width, dst_bus_width;
932 	int src_burst_size, dst_burst_size;
933 	u32 src_maxburst, dst_maxburst, src_best_burst, dst_best_burst;
934 	u32 dma_scr, fifoth;
935 
936 	src_addr_width = chan->dma_sconfig.src_addr_width;
937 	dst_addr_width = chan->dma_sconfig.dst_addr_width;
938 	src_maxburst = chan->dma_sconfig.src_maxburst;
939 	dst_maxburst = chan->dma_sconfig.dst_maxburst;
940 	fifoth = chan->threshold;
941 
942 	switch (direction) {
943 	case DMA_MEM_TO_DEV:
944 		/* Set device data size */
945 		dst_bus_width = stm32_dma_get_width(chan, dst_addr_width);
946 		if (dst_bus_width < 0)
947 			return dst_bus_width;
948 
949 		/* Set device burst size */
950 		dst_best_burst = stm32_dma_get_best_burst(buf_len,
951 							  dst_maxburst,
952 							  fifoth,
953 							  dst_addr_width);
954 
955 		dst_burst_size = stm32_dma_get_burst(chan, dst_best_burst);
956 		if (dst_burst_size < 0)
957 			return dst_burst_size;
958 
959 		/* Set memory data size */
960 		src_addr_width = stm32_dma_get_max_width(buf_len, buf_addr,
961 							 fifoth);
962 		chan->mem_width = src_addr_width;
963 		src_bus_width = stm32_dma_get_width(chan, src_addr_width);
964 		if (src_bus_width < 0)
965 			return src_bus_width;
966 
967 		/*
968 		 * Set memory burst size - burst not possible if address is not aligned on
969 		 * the address boundary equal to the size of the transfer
970 		 */
971 		if (buf_addr & (buf_len - 1))
972 			src_maxburst = 1;
973 		else
974 			src_maxburst = STM32_DMA_MAX_BURST;
975 		src_best_burst = stm32_dma_get_best_burst(buf_len,
976 							  src_maxburst,
977 							  fifoth,
978 							  src_addr_width);
979 		src_burst_size = stm32_dma_get_burst(chan, src_best_burst);
980 		if (src_burst_size < 0)
981 			return src_burst_size;
982 
983 		dma_scr = FIELD_PREP(STM32_DMA_SCR_DIR_MASK, STM32_DMA_MEM_TO_DEV) |
984 			FIELD_PREP(STM32_DMA_SCR_PSIZE_MASK, dst_bus_width) |
985 			FIELD_PREP(STM32_DMA_SCR_MSIZE_MASK, src_bus_width) |
986 			FIELD_PREP(STM32_DMA_SCR_PBURST_MASK, dst_burst_size) |
987 			FIELD_PREP(STM32_DMA_SCR_MBURST_MASK, src_burst_size);
988 
989 		/* Set FIFO threshold */
990 		chan->chan_reg.dma_sfcr &= ~STM32_DMA_SFCR_FTH_MASK;
991 		if (fifoth != STM32_DMA_FIFO_THRESHOLD_NONE)
992 			chan->chan_reg.dma_sfcr |= FIELD_PREP(STM32_DMA_SFCR_FTH_MASK, fifoth);
993 
994 		/* Set peripheral address */
995 		chan->chan_reg.dma_spar = chan->dma_sconfig.dst_addr;
996 		*buswidth = dst_addr_width;
997 		break;
998 
999 	case DMA_DEV_TO_MEM:
1000 		/* Set device data size */
1001 		src_bus_width = stm32_dma_get_width(chan, src_addr_width);
1002 		if (src_bus_width < 0)
1003 			return src_bus_width;
1004 
1005 		/* Set device burst size */
1006 		src_best_burst = stm32_dma_get_best_burst(buf_len,
1007 							  src_maxburst,
1008 							  fifoth,
1009 							  src_addr_width);
1010 		chan->mem_burst = src_best_burst;
1011 		src_burst_size = stm32_dma_get_burst(chan, src_best_burst);
1012 		if (src_burst_size < 0)
1013 			return src_burst_size;
1014 
1015 		/* Set memory data size */
1016 		dst_addr_width = stm32_dma_get_max_width(buf_len, buf_addr,
1017 							 fifoth);
1018 		chan->mem_width = dst_addr_width;
1019 		dst_bus_width = stm32_dma_get_width(chan, dst_addr_width);
1020 		if (dst_bus_width < 0)
1021 			return dst_bus_width;
1022 
1023 		/*
1024 		 * Set memory burst size - burst not possible if address is not aligned on
1025 		 * the address boundary equal to the size of the transfer
1026 		 */
1027 		if (buf_addr & (buf_len - 1))
1028 			dst_maxburst = 1;
1029 		else
1030 			dst_maxburst = STM32_DMA_MAX_BURST;
1031 		dst_best_burst = stm32_dma_get_best_burst(buf_len,
1032 							  dst_maxburst,
1033 							  fifoth,
1034 							  dst_addr_width);
1035 		chan->mem_burst = dst_best_burst;
1036 		dst_burst_size = stm32_dma_get_burst(chan, dst_best_burst);
1037 		if (dst_burst_size < 0)
1038 			return dst_burst_size;
1039 
1040 		dma_scr = FIELD_PREP(STM32_DMA_SCR_DIR_MASK, STM32_DMA_DEV_TO_MEM) |
1041 			FIELD_PREP(STM32_DMA_SCR_PSIZE_MASK, src_bus_width) |
1042 			FIELD_PREP(STM32_DMA_SCR_MSIZE_MASK, dst_bus_width) |
1043 			FIELD_PREP(STM32_DMA_SCR_PBURST_MASK, src_burst_size) |
1044 			FIELD_PREP(STM32_DMA_SCR_MBURST_MASK, dst_burst_size);
1045 
1046 		/* Set FIFO threshold */
1047 		chan->chan_reg.dma_sfcr &= ~STM32_DMA_SFCR_FTH_MASK;
1048 		if (fifoth != STM32_DMA_FIFO_THRESHOLD_NONE)
1049 			chan->chan_reg.dma_sfcr |= FIELD_PREP(STM32_DMA_SFCR_FTH_MASK, fifoth);
1050 
1051 		/* Set peripheral address */
1052 		chan->chan_reg.dma_spar = chan->dma_sconfig.src_addr;
1053 		*buswidth = chan->dma_sconfig.src_addr_width;
1054 		break;
1055 
1056 	default:
1057 		dev_err(vchan_chan_dev(&chan->vchan), "Dma direction is not supported\n");
1058 		return -EINVAL;
1059 	}
1060 
1061 	stm32_dma_set_fifo_config(chan, src_best_burst, dst_best_burst);
1062 
1063 	/* Set DMA control register */
1064 	chan->chan_reg.dma_scr &= ~(STM32_DMA_SCR_DIR_MASK |
1065 			STM32_DMA_SCR_PSIZE_MASK | STM32_DMA_SCR_MSIZE_MASK |
1066 			STM32_DMA_SCR_PBURST_MASK | STM32_DMA_SCR_MBURST_MASK);
1067 	chan->chan_reg.dma_scr |= dma_scr;
1068 
1069 	return 0;
1070 }
1071 
1072 static void stm32_dma_clear_reg(struct stm32_dma_chan_reg *regs)
1073 {
1074 	memset(regs, 0, sizeof(struct stm32_dma_chan_reg));
1075 }
1076 
1077 static struct dma_async_tx_descriptor *stm32_dma_prep_slave_sg(
1078 	struct dma_chan *c, struct scatterlist *sgl,
1079 	u32 sg_len, enum dma_transfer_direction direction,
1080 	unsigned long flags, void *context)
1081 {
1082 	struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
1083 	struct stm32_dma_desc *desc;
1084 	struct scatterlist *sg;
1085 	enum dma_slave_buswidth buswidth;
1086 	u32 nb_data_items;
1087 	int i, ret;
1088 
1089 	if (!chan->config_init) {
1090 		dev_err(vchan_chan_dev(&chan->vchan), "dma channel is not configured\n");
1091 		return NULL;
1092 	}
1093 
1094 	if (sg_len < 1) {
1095 		dev_err(vchan_chan_dev(&chan->vchan), "Invalid segment length %d\n", sg_len);
1096 		return NULL;
1097 	}
1098 
1099 	desc = kzalloc_flex(*desc, sg_req, sg_len, GFP_NOWAIT);
1100 	if (!desc)
1101 		return NULL;
1102 	desc->num_sgs = sg_len;
1103 
1104 	/* Set peripheral flow controller */
1105 	if (chan->dma_sconfig.device_fc)
1106 		chan->chan_reg.dma_scr |= STM32_DMA_SCR_PFCTRL;
1107 	else
1108 		chan->chan_reg.dma_scr &= ~STM32_DMA_SCR_PFCTRL;
1109 
1110 	/* Activate Double Buffer Mode if DMA triggers STM32 MDMA and more than 1 sg */
1111 	if (chan->trig_mdma && sg_len > 1) {
1112 		chan->chan_reg.dma_scr |= STM32_DMA_SCR_DBM;
1113 		chan->chan_reg.dma_scr &= ~STM32_DMA_SCR_CT;
1114 	}
1115 
1116 	for_each_sg(sgl, sg, sg_len, i) {
1117 		ret = stm32_dma_set_xfer_param(chan, direction, &buswidth,
1118 					       sg_dma_len(sg),
1119 					       sg_dma_address(sg));
1120 		if (ret < 0)
1121 			goto err;
1122 
1123 		desc->sg_req[i].len = sg_dma_len(sg);
1124 
1125 		nb_data_items = desc->sg_req[i].len / buswidth;
1126 		if (nb_data_items > STM32_DMA_ALIGNED_MAX_DATA_ITEMS) {
1127 			dev_err(vchan_chan_dev(&chan->vchan), "nb items not supported\n");
1128 			goto err;
1129 		}
1130 
1131 		stm32_dma_clear_reg(&desc->sg_req[i].chan_reg);
1132 		desc->sg_req[i].chan_reg.dma_scr = chan->chan_reg.dma_scr;
1133 		desc->sg_req[i].chan_reg.dma_sfcr = chan->chan_reg.dma_sfcr;
1134 		desc->sg_req[i].chan_reg.dma_spar = chan->chan_reg.dma_spar;
1135 		desc->sg_req[i].chan_reg.dma_sm0ar = sg_dma_address(sg);
1136 		desc->sg_req[i].chan_reg.dma_sm1ar = sg_dma_address(sg);
1137 		if (chan->trig_mdma)
1138 			desc->sg_req[i].chan_reg.dma_sm1ar += sg_dma_len(sg);
1139 		desc->sg_req[i].chan_reg.dma_sndtr = nb_data_items;
1140 	}
1141 	desc->cyclic = false;
1142 
1143 	return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
1144 
1145 err:
1146 	kfree(desc);
1147 	return NULL;
1148 }
1149 
1150 static struct dma_async_tx_descriptor *stm32_dma_prep_dma_cyclic(
1151 	struct dma_chan *c, dma_addr_t buf_addr, size_t buf_len,
1152 	size_t period_len, enum dma_transfer_direction direction,
1153 	unsigned long flags)
1154 {
1155 	struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
1156 	struct stm32_dma_desc *desc;
1157 	enum dma_slave_buswidth buswidth;
1158 	u32 num_periods, nb_data_items;
1159 	int i, ret;
1160 
1161 	if (!buf_len || !period_len) {
1162 		dev_err(vchan_chan_dev(&chan->vchan), "Invalid buffer/period len\n");
1163 		return NULL;
1164 	}
1165 
1166 	if (!chan->config_init) {
1167 		dev_err(vchan_chan_dev(&chan->vchan), "dma channel is not configured\n");
1168 		return NULL;
1169 	}
1170 
1171 	if (buf_len % period_len) {
1172 		dev_err(vchan_chan_dev(&chan->vchan), "buf_len not multiple of period_len\n");
1173 		return NULL;
1174 	}
1175 
1176 	/*
1177 	 * We allow to take more number of requests till DMA is
1178 	 * not started. The driver will loop over all requests.
1179 	 * Once DMA is started then new requests can be queued only after
1180 	 * terminating the DMA.
1181 	 */
1182 	if (chan->busy) {
1183 		dev_err(vchan_chan_dev(&chan->vchan), "Request not allowed when dma busy\n");
1184 		return NULL;
1185 	}
1186 
1187 	ret = stm32_dma_set_xfer_param(chan, direction, &buswidth, period_len,
1188 				       buf_addr);
1189 	if (ret < 0)
1190 		return NULL;
1191 
1192 	nb_data_items = period_len / buswidth;
1193 	if (nb_data_items > STM32_DMA_ALIGNED_MAX_DATA_ITEMS) {
1194 		dev_err(vchan_chan_dev(&chan->vchan), "number of items not supported\n");
1195 		return NULL;
1196 	}
1197 
1198 	/*  Enable Circular mode or double buffer mode */
1199 	if (buf_len == period_len) {
1200 		chan->chan_reg.dma_scr |= STM32_DMA_SCR_CIRC;
1201 	} else {
1202 		chan->chan_reg.dma_scr |= STM32_DMA_SCR_DBM;
1203 		chan->chan_reg.dma_scr &= ~STM32_DMA_SCR_CT;
1204 	}
1205 
1206 	/* Clear periph ctrl if client set it */
1207 	chan->chan_reg.dma_scr &= ~STM32_DMA_SCR_PFCTRL;
1208 
1209 	num_periods = buf_len / period_len;
1210 
1211 	desc = kzalloc_flex(*desc, sg_req, num_periods, GFP_NOWAIT);
1212 	if (!desc)
1213 		return NULL;
1214 	desc->num_sgs = num_periods;
1215 
1216 	for (i = 0; i < num_periods; i++) {
1217 		desc->sg_req[i].len = period_len;
1218 
1219 		stm32_dma_clear_reg(&desc->sg_req[i].chan_reg);
1220 		desc->sg_req[i].chan_reg.dma_scr = chan->chan_reg.dma_scr;
1221 		desc->sg_req[i].chan_reg.dma_sfcr = chan->chan_reg.dma_sfcr;
1222 		desc->sg_req[i].chan_reg.dma_spar = chan->chan_reg.dma_spar;
1223 		desc->sg_req[i].chan_reg.dma_sm0ar = buf_addr;
1224 		desc->sg_req[i].chan_reg.dma_sm1ar = buf_addr;
1225 		if (chan->trig_mdma)
1226 			desc->sg_req[i].chan_reg.dma_sm1ar += period_len;
1227 		desc->sg_req[i].chan_reg.dma_sndtr = nb_data_items;
1228 		if (!chan->trig_mdma)
1229 			buf_addr += period_len;
1230 	}
1231 	desc->cyclic = true;
1232 
1233 	return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
1234 }
1235 
1236 static struct dma_async_tx_descriptor *stm32_dma_prep_dma_memcpy(
1237 	struct dma_chan *c, dma_addr_t dest,
1238 	dma_addr_t src, size_t len, unsigned long flags)
1239 {
1240 	struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
1241 	enum dma_slave_buswidth max_width;
1242 	struct stm32_dma_desc *desc;
1243 	size_t xfer_count, offset;
1244 	u32 num_sgs, best_burst, threshold;
1245 	int dma_burst, i;
1246 
1247 	num_sgs = DIV_ROUND_UP(len, STM32_DMA_ALIGNED_MAX_DATA_ITEMS);
1248 	desc = kzalloc_flex(*desc, sg_req, num_sgs, GFP_NOWAIT);
1249 	if (!desc)
1250 		return NULL;
1251 	desc->num_sgs = num_sgs;
1252 
1253 	threshold = chan->threshold;
1254 
1255 	for (offset = 0, i = 0; offset < len; offset += xfer_count, i++) {
1256 		xfer_count = min_t(size_t, len - offset,
1257 				   STM32_DMA_ALIGNED_MAX_DATA_ITEMS);
1258 
1259 		/* Compute best burst size */
1260 		max_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1261 		best_burst = stm32_dma_get_best_burst(len, STM32_DMA_MAX_BURST,
1262 						      threshold, max_width);
1263 		dma_burst = stm32_dma_get_burst(chan, best_burst);
1264 		if (dma_burst < 0) {
1265 			kfree(desc);
1266 			return NULL;
1267 		}
1268 
1269 		stm32_dma_clear_reg(&desc->sg_req[i].chan_reg);
1270 		desc->sg_req[i].chan_reg.dma_scr =
1271 			FIELD_PREP(STM32_DMA_SCR_DIR_MASK, STM32_DMA_MEM_TO_MEM) |
1272 			FIELD_PREP(STM32_DMA_SCR_PBURST_MASK, dma_burst) |
1273 			FIELD_PREP(STM32_DMA_SCR_MBURST_MASK, dma_burst) |
1274 			STM32_DMA_SCR_MINC |
1275 			STM32_DMA_SCR_PINC |
1276 			STM32_DMA_SCR_TCIE |
1277 			STM32_DMA_SCR_TEIE;
1278 		desc->sg_req[i].chan_reg.dma_sfcr |= STM32_DMA_SFCR_MASK;
1279 		desc->sg_req[i].chan_reg.dma_sfcr |= FIELD_PREP(STM32_DMA_SFCR_FTH_MASK, threshold);
1280 		desc->sg_req[i].chan_reg.dma_spar = src + offset;
1281 		desc->sg_req[i].chan_reg.dma_sm0ar = dest + offset;
1282 		desc->sg_req[i].chan_reg.dma_sndtr = xfer_count;
1283 		desc->sg_req[i].len = xfer_count;
1284 	}
1285 	desc->cyclic = false;
1286 
1287 	return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
1288 }
1289 
1290 static u32 stm32_dma_get_remaining_bytes(struct stm32_dma_chan *chan)
1291 {
1292 	u32 dma_scr, width, ndtr;
1293 	struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
1294 
1295 	dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id));
1296 	width = FIELD_GET(STM32_DMA_SCR_PSIZE_MASK, dma_scr);
1297 	ndtr = stm32_dma_read(dmadev, STM32_DMA_SNDTR(chan->id));
1298 
1299 	return ndtr << width;
1300 }
1301 
1302 /**
1303  * stm32_dma_is_current_sg - check that expected sg_req is currently transferred
1304  * @chan: dma channel
1305  *
1306  * This function called when IRQ are disable, checks that the hardware has not
1307  * switched on the next transfer in double buffer mode. The test is done by
1308  * comparing the next_sg memory address with the hardware related register
1309  * (based on CT bit value).
1310  *
1311  * Returns true if expected current transfer is still running or double
1312  * buffer mode is not activated.
1313  */
1314 static bool stm32_dma_is_current_sg(struct stm32_dma_chan *chan)
1315 {
1316 	struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
1317 	struct stm32_dma_sg_req *sg_req;
1318 	u32 dma_scr, dma_smar, id, period_len;
1319 
1320 	id = chan->id;
1321 	dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(id));
1322 
1323 	/* In cyclic CIRC but not DBM, CT is not used */
1324 	if (!(dma_scr & STM32_DMA_SCR_DBM))
1325 		return true;
1326 
1327 	sg_req = &chan->desc->sg_req[chan->next_sg];
1328 	period_len = sg_req->len;
1329 
1330 	/* DBM - take care of a previous pause/resume not yet post reconfigured */
1331 	if (dma_scr & STM32_DMA_SCR_CT) {
1332 		dma_smar = stm32_dma_read(dmadev, STM32_DMA_SM0AR(id));
1333 		/*
1334 		 * If transfer has been pause/resumed,
1335 		 * SM0AR is in the range of [SM0AR:SM0AR+period_len]
1336 		 */
1337 		return (dma_smar >= sg_req->chan_reg.dma_sm0ar &&
1338 			dma_smar < sg_req->chan_reg.dma_sm0ar + period_len);
1339 	}
1340 
1341 	dma_smar = stm32_dma_read(dmadev, STM32_DMA_SM1AR(id));
1342 	/*
1343 	 * If transfer has been pause/resumed,
1344 	 * SM1AR is in the range of [SM1AR:SM1AR+period_len]
1345 	 */
1346 	return (dma_smar >= sg_req->chan_reg.dma_sm1ar &&
1347 		dma_smar < sg_req->chan_reg.dma_sm1ar + period_len);
1348 }
1349 
1350 static size_t stm32_dma_desc_residue(struct stm32_dma_chan *chan,
1351 				     struct stm32_dma_desc *desc,
1352 				     u32 next_sg)
1353 {
1354 	u32 modulo, burst_size;
1355 	u32 residue;
1356 	u32 n_sg = next_sg;
1357 	struct stm32_dma_sg_req *sg_req = &chan->desc->sg_req[chan->next_sg];
1358 	int i;
1359 
1360 	/*
1361 	 * Calculate the residue means compute the descriptors
1362 	 * information:
1363 	 * - the sg_req currently transferred
1364 	 * - the Hardware remaining position in this sg (NDTR bits field).
1365 	 *
1366 	 * A race condition may occur if DMA is running in cyclic or double
1367 	 * buffer mode, since the DMA register are automatically reloaded at end
1368 	 * of period transfer. The hardware may have switched to the next
1369 	 * transfer (CT bit updated) just before the position (SxNDTR reg) is
1370 	 * read.
1371 	 * In this case the SxNDTR reg could (or not) correspond to the new
1372 	 * transfer position, and not the expected one.
1373 	 * The strategy implemented in the stm32 driver is to:
1374 	 *  - read the SxNDTR register
1375 	 *  - crosscheck that hardware is still in current transfer.
1376 	 * In case of switch, we can assume that the DMA is at the beginning of
1377 	 * the next transfer. So we approximate the residue in consequence, by
1378 	 * pointing on the beginning of next transfer.
1379 	 *
1380 	 * This race condition doesn't apply for none cyclic mode, as double
1381 	 * buffer is not used. In such situation registers are updated by the
1382 	 * software.
1383 	 */
1384 
1385 	residue = stm32_dma_get_remaining_bytes(chan);
1386 
1387 	if ((chan->desc->cyclic || chan->trig_mdma) && !stm32_dma_is_current_sg(chan)) {
1388 		n_sg++;
1389 		if (n_sg == chan->desc->num_sgs)
1390 			n_sg = 0;
1391 		if (!chan->trig_mdma)
1392 			residue = sg_req->len;
1393 	}
1394 
1395 	/*
1396 	 * In cyclic mode, for the last period, residue = remaining bytes
1397 	 * from NDTR,
1398 	 * else for all other periods in cyclic mode, and in sg mode,
1399 	 * residue = remaining bytes from NDTR + remaining
1400 	 * periods/sg to be transferred
1401 	 */
1402 	if ((!chan->desc->cyclic && !chan->trig_mdma) || n_sg != 0)
1403 		for (i = n_sg; i < desc->num_sgs; i++)
1404 			residue += desc->sg_req[i].len;
1405 
1406 	if (!chan->mem_burst)
1407 		return residue;
1408 
1409 	burst_size = chan->mem_burst * chan->mem_width;
1410 	modulo = residue % burst_size;
1411 	if (modulo)
1412 		residue = residue - modulo + burst_size;
1413 
1414 	return residue;
1415 }
1416 
1417 static enum dma_status stm32_dma_tx_status(struct dma_chan *c,
1418 					   dma_cookie_t cookie,
1419 					   struct dma_tx_state *state)
1420 {
1421 	struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
1422 	struct virt_dma_desc *vdesc;
1423 	enum dma_status status;
1424 	unsigned long flags;
1425 	u32 residue = 0;
1426 
1427 	status = dma_cookie_status(c, cookie, state);
1428 	if (status == DMA_COMPLETE)
1429 		return status;
1430 
1431 	status = chan->status;
1432 
1433 	if (!state)
1434 		return status;
1435 
1436 	spin_lock_irqsave(&chan->vchan.lock, flags);
1437 	vdesc = vchan_find_desc(&chan->vchan, cookie);
1438 	if (chan->desc && cookie == chan->desc->vdesc.tx.cookie)
1439 		residue = stm32_dma_desc_residue(chan, chan->desc,
1440 						 chan->next_sg);
1441 	else if (vdesc)
1442 		residue = stm32_dma_desc_residue(chan,
1443 						 to_stm32_dma_desc(vdesc), 0);
1444 	dma_set_residue(state, residue);
1445 
1446 	spin_unlock_irqrestore(&chan->vchan.lock, flags);
1447 
1448 	return status;
1449 }
1450 
1451 static int stm32_dma_alloc_chan_resources(struct dma_chan *c)
1452 {
1453 	struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
1454 	struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
1455 	int ret;
1456 
1457 	chan->config_init = false;
1458 
1459 	ret = pm_runtime_resume_and_get(dmadev->ddev.dev);
1460 	if (ret < 0)
1461 		return ret;
1462 
1463 	ret = stm32_dma_disable_chan(chan);
1464 	if (ret < 0)
1465 		pm_runtime_put(dmadev->ddev.dev);
1466 
1467 	return ret;
1468 }
1469 
1470 static void stm32_dma_free_chan_resources(struct dma_chan *c)
1471 {
1472 	struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
1473 	struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
1474 	unsigned long flags;
1475 
1476 	dev_dbg(vchan_chan_dev(&chan->vchan), "Freeing channel %d\n", chan->id);
1477 
1478 	if (chan->busy) {
1479 		spin_lock_irqsave(&chan->vchan.lock, flags);
1480 		stm32_dma_stop(chan);
1481 		chan->desc = NULL;
1482 		spin_unlock_irqrestore(&chan->vchan.lock, flags);
1483 	}
1484 
1485 	pm_runtime_put(dmadev->ddev.dev);
1486 
1487 	vchan_free_chan_resources(to_virt_chan(c));
1488 	stm32_dma_clear_reg(&chan->chan_reg);
1489 	chan->threshold = 0;
1490 }
1491 
1492 static void stm32_dma_desc_free(struct virt_dma_desc *vdesc)
1493 {
1494 	kfree(container_of(vdesc, struct stm32_dma_desc, vdesc));
1495 }
1496 
1497 static void stm32_dma_set_config(struct stm32_dma_chan *chan,
1498 				 struct stm32_dma_cfg *cfg)
1499 {
1500 	stm32_dma_clear_reg(&chan->chan_reg);
1501 
1502 	chan->chan_reg.dma_scr = cfg->stream_config & STM32_DMA_SCR_CFG_MASK;
1503 	chan->chan_reg.dma_scr |= FIELD_PREP(STM32_DMA_SCR_REQ_MASK, cfg->request_line);
1504 
1505 	/* Enable Interrupts  */
1506 	chan->chan_reg.dma_scr |= STM32_DMA_SCR_TEIE | STM32_DMA_SCR_TCIE;
1507 
1508 	chan->threshold = FIELD_GET(STM32_DMA_THRESHOLD_FTR_MASK, cfg->features);
1509 	if (FIELD_GET(STM32_DMA_DIRECT_MODE_MASK, cfg->features))
1510 		chan->threshold = STM32_DMA_FIFO_THRESHOLD_NONE;
1511 	if (FIELD_GET(STM32_DMA_ALT_ACK_MODE_MASK, cfg->features))
1512 		chan->chan_reg.dma_scr |= STM32_DMA_SCR_TRBUFF;
1513 	chan->mdma_config.stream_id = FIELD_GET(STM32_DMA_MDMA_STREAM_ID_MASK, cfg->features);
1514 }
1515 
1516 static struct dma_chan *stm32_dma_of_xlate(struct of_phandle_args *dma_spec,
1517 					   struct of_dma *ofdma)
1518 {
1519 	struct stm32_dma_device *dmadev = ofdma->of_dma_data;
1520 	struct device *dev = dmadev->ddev.dev;
1521 	struct stm32_dma_cfg cfg;
1522 	struct stm32_dma_chan *chan;
1523 	struct dma_chan *c;
1524 
1525 	if (dma_spec->args_count < 4) {
1526 		dev_err(dev, "Bad number of cells\n");
1527 		return NULL;
1528 	}
1529 
1530 	cfg.channel_id = dma_spec->args[0];
1531 	cfg.request_line = dma_spec->args[1];
1532 	cfg.stream_config = dma_spec->args[2];
1533 	cfg.features = dma_spec->args[3];
1534 
1535 	if (cfg.channel_id >= STM32_DMA_MAX_CHANNELS ||
1536 	    cfg.request_line >= STM32_DMA_MAX_REQUEST_ID) {
1537 		dev_err(dev, "Bad channel and/or request id\n");
1538 		return NULL;
1539 	}
1540 
1541 	chan = &dmadev->chan[cfg.channel_id];
1542 
1543 	c = dma_get_slave_channel(&chan->vchan.chan);
1544 	if (!c) {
1545 		dev_err(dev, "No more channels available\n");
1546 		return NULL;
1547 	}
1548 
1549 	stm32_dma_set_config(chan, &cfg);
1550 
1551 	return c;
1552 }
1553 
1554 static const struct of_device_id stm32_dma_of_match[] = {
1555 	{ .compatible = "st,stm32-dma", },
1556 	{ /* sentinel */ },
1557 };
1558 MODULE_DEVICE_TABLE(of, stm32_dma_of_match);
1559 
1560 static int stm32_dma_probe(struct platform_device *pdev)
1561 {
1562 	struct stm32_dma_chan *chan;
1563 	struct stm32_dma_device *dmadev;
1564 	struct dma_device *dd;
1565 	struct resource *res;
1566 	struct reset_control *rst;
1567 	int i, ret;
1568 
1569 	dmadev = devm_kzalloc_obj(&pdev->dev, *dmadev);
1570 	if (!dmadev)
1571 		return -ENOMEM;
1572 
1573 	dd = &dmadev->ddev;
1574 
1575 	dmadev->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
1576 	if (IS_ERR(dmadev->base))
1577 		return PTR_ERR(dmadev->base);
1578 
1579 	dmadev->clk = devm_clk_get(&pdev->dev, NULL);
1580 	if (IS_ERR(dmadev->clk))
1581 		return dev_err_probe(&pdev->dev, PTR_ERR(dmadev->clk), "Can't get clock\n");
1582 
1583 	ret = clk_prepare_enable(dmadev->clk);
1584 	if (ret < 0) {
1585 		dev_err(&pdev->dev, "clk_prep_enable error: %d\n", ret);
1586 		return ret;
1587 	}
1588 
1589 	dmadev->mem2mem = of_property_read_bool(pdev->dev.of_node,
1590 						"st,mem2mem");
1591 
1592 	rst = devm_reset_control_get(&pdev->dev, NULL);
1593 	if (IS_ERR(rst)) {
1594 		ret = PTR_ERR(rst);
1595 		if (ret == -EPROBE_DEFER)
1596 			goto clk_free;
1597 	} else {
1598 		reset_control_assert(rst);
1599 		udelay(2);
1600 		reset_control_deassert(rst);
1601 	}
1602 
1603 	dma_set_max_seg_size(&pdev->dev, STM32_DMA_ALIGNED_MAX_DATA_ITEMS);
1604 
1605 	dma_cap_set(DMA_SLAVE, dd->cap_mask);
1606 	dma_cap_set(DMA_PRIVATE, dd->cap_mask);
1607 	dma_cap_set(DMA_CYCLIC, dd->cap_mask);
1608 	dd->device_alloc_chan_resources = stm32_dma_alloc_chan_resources;
1609 	dd->device_free_chan_resources = stm32_dma_free_chan_resources;
1610 	dd->device_tx_status = stm32_dma_tx_status;
1611 	dd->device_issue_pending = stm32_dma_issue_pending;
1612 	dd->device_prep_slave_sg = stm32_dma_prep_slave_sg;
1613 	dd->device_prep_dma_cyclic = stm32_dma_prep_dma_cyclic;
1614 	dd->device_config = stm32_dma_slave_config;
1615 	dd->device_pause = stm32_dma_pause;
1616 	dd->device_resume = stm32_dma_resume;
1617 	dd->device_terminate_all = stm32_dma_terminate_all;
1618 	dd->device_synchronize = stm32_dma_synchronize;
1619 	dd->src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1620 		BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1621 		BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
1622 	dd->dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1623 		BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1624 		BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
1625 	dd->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
1626 	dd->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
1627 	dd->copy_align = DMAENGINE_ALIGN_32_BYTES;
1628 	dd->max_burst = STM32_DMA_MAX_BURST;
1629 	dd->max_sg_burst = STM32_DMA_ALIGNED_MAX_DATA_ITEMS;
1630 	dd->descriptor_reuse = true;
1631 	dd->dev = &pdev->dev;
1632 	INIT_LIST_HEAD(&dd->channels);
1633 
1634 	if (dmadev->mem2mem) {
1635 		dma_cap_set(DMA_MEMCPY, dd->cap_mask);
1636 		dd->device_prep_dma_memcpy = stm32_dma_prep_dma_memcpy;
1637 		dd->directions |= BIT(DMA_MEM_TO_MEM);
1638 	}
1639 
1640 	for (i = 0; i < STM32_DMA_MAX_CHANNELS; i++) {
1641 		chan = &dmadev->chan[i];
1642 		chan->id = i;
1643 		chan->vchan.desc_free = stm32_dma_desc_free;
1644 		vchan_init(&chan->vchan, dd);
1645 
1646 		chan->mdma_config.ifcr = res->start;
1647 		chan->mdma_config.ifcr += STM32_DMA_IFCR(chan->id);
1648 
1649 		chan->mdma_config.tcf = STM32_DMA_TCI;
1650 		chan->mdma_config.tcf <<= STM32_DMA_FLAGS_SHIFT(chan->id);
1651 	}
1652 
1653 	ret = dma_async_device_register(dd);
1654 	if (ret)
1655 		goto clk_free;
1656 
1657 	for (i = 0; i < STM32_DMA_MAX_CHANNELS; i++) {
1658 		chan = &dmadev->chan[i];
1659 		ret = platform_get_irq(pdev, i);
1660 		if (ret < 0)
1661 			goto err_unregister;
1662 		chan->irq = ret;
1663 
1664 		ret = devm_request_irq(&pdev->dev, chan->irq,
1665 				       stm32_dma_chan_irq, 0,
1666 				       vchan_chan_name(&chan->vchan), chan);
1667 		if (ret) {
1668 			dev_err(&pdev->dev,
1669 				"request_irq failed with err %d channel %d\n",
1670 				ret, i);
1671 			goto err_unregister;
1672 		}
1673 	}
1674 
1675 	ret = of_dma_controller_register(pdev->dev.of_node,
1676 					 stm32_dma_of_xlate, dmadev);
1677 	if (ret < 0) {
1678 		dev_err(&pdev->dev,
1679 			"STM32 DMA DMA OF registration failed %d\n", ret);
1680 		goto err_unregister;
1681 	}
1682 
1683 	platform_set_drvdata(pdev, dmadev);
1684 
1685 	pm_runtime_set_active(&pdev->dev);
1686 	pm_runtime_enable(&pdev->dev);
1687 	pm_runtime_get_noresume(&pdev->dev);
1688 	pm_runtime_put(&pdev->dev);
1689 
1690 	dev_info(&pdev->dev, "STM32 DMA driver registered\n");
1691 
1692 	return 0;
1693 
1694 err_unregister:
1695 	dma_async_device_unregister(dd);
1696 clk_free:
1697 	clk_disable_unprepare(dmadev->clk);
1698 
1699 	return ret;
1700 }
1701 
1702 #ifdef CONFIG_PM
1703 static int stm32_dma_runtime_suspend(struct device *dev)
1704 {
1705 	struct stm32_dma_device *dmadev = dev_get_drvdata(dev);
1706 
1707 	clk_disable_unprepare(dmadev->clk);
1708 
1709 	return 0;
1710 }
1711 
1712 static int stm32_dma_runtime_resume(struct device *dev)
1713 {
1714 	struct stm32_dma_device *dmadev = dev_get_drvdata(dev);
1715 	int ret;
1716 
1717 	ret = clk_prepare_enable(dmadev->clk);
1718 	if (ret) {
1719 		dev_err(dev, "failed to prepare_enable clock\n");
1720 		return ret;
1721 	}
1722 
1723 	return 0;
1724 }
1725 #endif
1726 
1727 #ifdef CONFIG_PM_SLEEP
1728 static int stm32_dma_pm_suspend(struct device *dev)
1729 {
1730 	struct stm32_dma_device *dmadev = dev_get_drvdata(dev);
1731 	int id, ret, scr;
1732 
1733 	ret = pm_runtime_resume_and_get(dev);
1734 	if (ret < 0)
1735 		return ret;
1736 
1737 	for (id = 0; id < STM32_DMA_MAX_CHANNELS; id++) {
1738 		scr = stm32_dma_read(dmadev, STM32_DMA_SCR(id));
1739 		if (scr & STM32_DMA_SCR_EN) {
1740 			dev_warn(dev, "Suspend is prevented by Chan %i\n", id);
1741 			return -EBUSY;
1742 		}
1743 	}
1744 
1745 	pm_runtime_put_sync(dev);
1746 
1747 	pm_runtime_force_suspend(dev);
1748 
1749 	return 0;
1750 }
1751 
1752 static int stm32_dma_pm_resume(struct device *dev)
1753 {
1754 	return pm_runtime_force_resume(dev);
1755 }
1756 #endif
1757 
1758 static const struct dev_pm_ops stm32_dma_pm_ops = {
1759 	SET_SYSTEM_SLEEP_PM_OPS(stm32_dma_pm_suspend, stm32_dma_pm_resume)
1760 	SET_RUNTIME_PM_OPS(stm32_dma_runtime_suspend,
1761 			   stm32_dma_runtime_resume, NULL)
1762 };
1763 
1764 static struct platform_driver stm32_dma_driver = {
1765 	.driver = {
1766 		.name = "stm32-dma",
1767 		.of_match_table = stm32_dma_of_match,
1768 		.pm = &stm32_dma_pm_ops,
1769 	},
1770 	.probe = stm32_dma_probe,
1771 };
1772 
1773 static int __init stm32_dma_init(void)
1774 {
1775 	return platform_driver_register(&stm32_dma_driver);
1776 }
1777 subsys_initcall(stm32_dma_init);
1778