blob: b585e11c21687e8e42542a5dfb02cb12636f543a [file] [log] [blame]
Thomas Gleixneraf873fc2019-05-28 09:57:21 -07001// SPDX-License-Identifier: GPL-2.0-only
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02002/*
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>
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +01009 * Pierre-Yves Mordret <pierre-yves.mordret@st.com>
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +020010 */
11
12#include <linux/clk.h>
13#include <linux/delay.h>
14#include <linux/dmaengine.h>
15#include <linux/dma-mapping.h>
16#include <linux/err.h>
17#include <linux/init.h>
Amelie Delaunay409ffc42020-01-29 16:36:27 +010018#include <linux/iopoll.h>
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +020019#include <linux/jiffies.h>
20#include <linux/list.h>
21#include <linux/module.h>
22#include <linux/of.h>
23#include <linux/of_device.h>
24#include <linux/of_dma.h>
25#include <linux/platform_device.h>
Pierre-Yves MORDRET48bc73b2019-01-03 11:17:08 +010026#include <linux/pm_runtime.h>
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +020027#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_LIFCR 0x0008 /* DMA Low Int Flag Clear Reg */
36#define STM32_DMA_HIFCR 0x000c /* DMA High Int Flag Clear Reg */
37#define STM32_DMA_TCI BIT(5) /* Transfer Complete Interrupt */
Pierre Yves MORDRETc2d86b12018-03-13 17:42:03 +010038#define STM32_DMA_HTI BIT(4) /* Half Transfer Interrupt */
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +020039#define STM32_DMA_TEI BIT(3) /* Transfer Error Interrupt */
40#define STM32_DMA_DMEI BIT(2) /* Direct Mode Error Interrupt */
41#define STM32_DMA_FEI BIT(0) /* FIFO Error Interrupt */
Pierre Yves MORDRET9df3bd52018-03-13 17:42:05 +010042#define STM32_DMA_MASKI (STM32_DMA_TCI \
43 | STM32_DMA_TEI \
44 | STM32_DMA_DMEI \
45 | STM32_DMA_FEI)
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +020046
47/* DMA Stream x Configuration Register */
48#define STM32_DMA_SCR(x) (0x0010 + 0x18 * (x)) /* x = 0..7 */
49#define STM32_DMA_SCR_REQ(n) ((n & 0x7) << 25)
50#define STM32_DMA_SCR_MBURST_MASK GENMASK(24, 23)
51#define STM32_DMA_SCR_MBURST(n) ((n & 0x3) << 23)
52#define STM32_DMA_SCR_PBURST_MASK GENMASK(22, 21)
53#define STM32_DMA_SCR_PBURST(n) ((n & 0x3) << 21)
54#define STM32_DMA_SCR_PL_MASK GENMASK(17, 16)
55#define STM32_DMA_SCR_PL(n) ((n & 0x3) << 16)
56#define STM32_DMA_SCR_MSIZE_MASK GENMASK(14, 13)
57#define STM32_DMA_SCR_MSIZE(n) ((n & 0x3) << 13)
58#define STM32_DMA_SCR_PSIZE_MASK GENMASK(12, 11)
59#define STM32_DMA_SCR_PSIZE(n) ((n & 0x3) << 11)
60#define STM32_DMA_SCR_PSIZE_GET(n) ((n & STM32_DMA_SCR_PSIZE_MASK) >> 11)
61#define STM32_DMA_SCR_DIR_MASK GENMASK(7, 6)
62#define STM32_DMA_SCR_DIR(n) ((n & 0x3) << 6)
63#define STM32_DMA_SCR_CT BIT(19) /* Target in double buffer */
64#define STM32_DMA_SCR_DBM BIT(18) /* Double Buffer Mode */
65#define STM32_DMA_SCR_PINCOS BIT(15) /* Peripheral inc offset size */
66#define STM32_DMA_SCR_MINC BIT(10) /* Memory increment mode */
67#define STM32_DMA_SCR_PINC BIT(9) /* Peripheral increment mode */
68#define STM32_DMA_SCR_CIRC BIT(8) /* Circular mode */
69#define STM32_DMA_SCR_PFCTRL BIT(5) /* Peripheral Flow Controller */
Pierre Yves MORDRET249d5532018-03-13 17:42:07 +010070#define STM32_DMA_SCR_TCIE BIT(4) /* Transfer Complete Int Enable
71 */
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +020072#define STM32_DMA_SCR_TEIE BIT(2) /* Transfer Error Int Enable */
73#define STM32_DMA_SCR_DMEIE BIT(1) /* Direct Mode Err Int Enable */
74#define STM32_DMA_SCR_EN BIT(0) /* Stream Enable */
75#define STM32_DMA_SCR_CFG_MASK (STM32_DMA_SCR_PINC \
76 | STM32_DMA_SCR_MINC \
77 | STM32_DMA_SCR_PINCOS \
78 | STM32_DMA_SCR_PL_MASK)
79#define STM32_DMA_SCR_IRQ_MASK (STM32_DMA_SCR_TCIE \
80 | STM32_DMA_SCR_TEIE \
81 | STM32_DMA_SCR_DMEIE)
82
83/* DMA Stream x number of data register */
84#define STM32_DMA_SNDTR(x) (0x0014 + 0x18 * (x))
85
86/* DMA stream peripheral address register */
87#define STM32_DMA_SPAR(x) (0x0018 + 0x18 * (x))
88
89/* DMA stream x memory 0 address register */
90#define STM32_DMA_SM0AR(x) (0x001c + 0x18 * (x))
91
92/* DMA stream x memory 1 address register */
93#define STM32_DMA_SM1AR(x) (0x0020 + 0x18 * (x))
94
95/* DMA stream x FIFO control register */
96#define STM32_DMA_SFCR(x) (0x0024 + 0x18 * (x))
97#define STM32_DMA_SFCR_FTH_MASK GENMASK(1, 0)
98#define STM32_DMA_SFCR_FTH(n) (n & STM32_DMA_SFCR_FTH_MASK)
99#define STM32_DMA_SFCR_FEIE BIT(7) /* FIFO error interrupt enable */
100#define STM32_DMA_SFCR_DMDIS BIT(2) /* Direct mode disable */
101#define STM32_DMA_SFCR_MASK (STM32_DMA_SFCR_FEIE \
102 | STM32_DMA_SFCR_DMDIS)
103
104/* DMA direction */
105#define STM32_DMA_DEV_TO_MEM 0x00
106#define STM32_DMA_MEM_TO_DEV 0x01
107#define STM32_DMA_MEM_TO_MEM 0x02
108
109/* DMA priority level */
110#define STM32_DMA_PRIORITY_LOW 0x00
111#define STM32_DMA_PRIORITY_MEDIUM 0x01
112#define STM32_DMA_PRIORITY_HIGH 0x02
113#define STM32_DMA_PRIORITY_VERY_HIGH 0x03
114
115/* DMA FIFO threshold selection */
116#define STM32_DMA_FIFO_THRESHOLD_1QUARTERFULL 0x00
117#define STM32_DMA_FIFO_THRESHOLD_HALFFULL 0x01
118#define STM32_DMA_FIFO_THRESHOLD_3QUARTERSFULL 0x02
119#define STM32_DMA_FIFO_THRESHOLD_FULL 0x03
120
121#define STM32_DMA_MAX_DATA_ITEMS 0xffff
Pierre Yves MORDRET80a76952018-03-13 17:42:04 +0100122/*
123 * Valid transfer starts from @0 to @0xFFFE leading to unaligned scatter
124 * gather at boundary. Thus it's safer to round down this value on FIFO
125 * size (16 Bytes)
126 */
127#define STM32_DMA_ALIGNED_MAX_DATA_ITEMS \
128 ALIGN_DOWN(STM32_DMA_MAX_DATA_ITEMS, 16)
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200129#define STM32_DMA_MAX_CHANNELS 0x08
130#define STM32_DMA_MAX_REQUEST_ID 0x08
131#define STM32_DMA_MAX_DATA_PARAM 0x03
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +0100132#define STM32_DMA_FIFO_SIZE 16 /* FIFO is 16 bytes */
133#define STM32_DMA_MIN_BURST 4
M'boumba Cedric Madianga276b0042016-12-13 14:40:51 +0100134#define STM32_DMA_MAX_BURST 16
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200135
Pierre Yves MORDRET951f44c2018-03-13 17:42:01 +0100136/* DMA Features */
137#define STM32_DMA_THRESHOLD_FTR_MASK GENMASK(1, 0)
138#define STM32_DMA_THRESHOLD_FTR_GET(n) ((n) & STM32_DMA_THRESHOLD_FTR_MASK)
139
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200140enum stm32_dma_width {
141 STM32_DMA_BYTE,
142 STM32_DMA_HALF_WORD,
143 STM32_DMA_WORD,
144};
145
146enum stm32_dma_burst_size {
147 STM32_DMA_BURST_SINGLE,
148 STM32_DMA_BURST_INCR4,
149 STM32_DMA_BURST_INCR8,
150 STM32_DMA_BURST_INCR16,
151};
152
Pierre Yves MORDRET951f44c2018-03-13 17:42:01 +0100153/**
154 * struct stm32_dma_cfg - STM32 DMA custom configuration
155 * @channel_id: channel ID
156 * @request_line: DMA request
157 * @stream_config: 32bit mask specifying the DMA channel configuration
158 * @features: 32bit mask specifying the DMA Feature list
159 */
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200160struct stm32_dma_cfg {
161 u32 channel_id;
162 u32 request_line;
163 u32 stream_config;
Pierre Yves MORDRET951f44c2018-03-13 17:42:01 +0100164 u32 features;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200165};
166
167struct stm32_dma_chan_reg {
168 u32 dma_lisr;
169 u32 dma_hisr;
170 u32 dma_lifcr;
171 u32 dma_hifcr;
172 u32 dma_scr;
173 u32 dma_sndtr;
174 u32 dma_spar;
175 u32 dma_sm0ar;
176 u32 dma_sm1ar;
177 u32 dma_sfcr;
178};
179
180struct stm32_dma_sg_req {
181 u32 len;
182 struct stm32_dma_chan_reg chan_reg;
183};
184
185struct stm32_dma_desc {
186 struct virt_dma_desc vdesc;
187 bool cyclic;
188 u32 num_sgs;
189 struct stm32_dma_sg_req sg_req[];
190};
191
192struct stm32_dma_chan {
193 struct virt_dma_chan vchan;
194 bool config_init;
195 bool busy;
196 u32 id;
197 u32 irq;
198 struct stm32_dma_desc *desc;
199 u32 next_sg;
200 struct dma_slave_config dma_sconfig;
201 struct stm32_dma_chan_reg chan_reg;
Pierre Yves MORDRET951f44c2018-03-13 17:42:01 +0100202 u32 threshold;
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +0100203 u32 mem_burst;
204 u32 mem_width;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200205};
206
207struct stm32_dma_device {
208 struct dma_device ddev;
209 void __iomem *base;
210 struct clk *clk;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200211 bool mem2mem;
212 struct stm32_dma_chan chan[STM32_DMA_MAX_CHANNELS];
213};
214
215static struct stm32_dma_device *stm32_dma_get_dev(struct stm32_dma_chan *chan)
216{
217 return container_of(chan->vchan.chan.device, struct stm32_dma_device,
218 ddev);
219}
220
221static struct stm32_dma_chan *to_stm32_dma_chan(struct dma_chan *c)
222{
223 return container_of(c, struct stm32_dma_chan, vchan.chan);
224}
225
226static struct stm32_dma_desc *to_stm32_dma_desc(struct virt_dma_desc *vdesc)
227{
228 return container_of(vdesc, struct stm32_dma_desc, vdesc);
229}
230
231static struct device *chan2dev(struct stm32_dma_chan *chan)
232{
233 return &chan->vchan.chan.dev->device;
234}
235
236static u32 stm32_dma_read(struct stm32_dma_device *dmadev, u32 reg)
237{
238 return readl_relaxed(dmadev->base + reg);
239}
240
241static void stm32_dma_write(struct stm32_dma_device *dmadev, u32 reg, u32 val)
242{
243 writel_relaxed(val, dmadev->base + reg);
244}
245
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200246static int stm32_dma_get_width(struct stm32_dma_chan *chan,
247 enum dma_slave_buswidth width)
248{
249 switch (width) {
250 case DMA_SLAVE_BUSWIDTH_1_BYTE:
251 return STM32_DMA_BYTE;
252 case DMA_SLAVE_BUSWIDTH_2_BYTES:
253 return STM32_DMA_HALF_WORD;
254 case DMA_SLAVE_BUSWIDTH_4_BYTES:
255 return STM32_DMA_WORD;
256 default:
257 dev_err(chan2dev(chan), "Dma bus width not supported\n");
258 return -EINVAL;
259 }
260}
261
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +0100262static enum dma_slave_buswidth stm32_dma_get_max_width(u32 buf_len,
263 u32 threshold)
264{
265 enum dma_slave_buswidth max_width;
266
267 if (threshold == STM32_DMA_FIFO_THRESHOLD_FULL)
268 max_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
269 else
270 max_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
271
272 while ((buf_len < max_width || buf_len % max_width) &&
273 max_width > DMA_SLAVE_BUSWIDTH_1_BYTE)
274 max_width = max_width >> 1;
275
276 return max_width;
277}
278
279static bool stm32_dma_fifo_threshold_is_allowed(u32 burst, u32 threshold,
280 enum dma_slave_buswidth width)
281{
282 u32 remaining;
283
284 if (width != DMA_SLAVE_BUSWIDTH_UNDEFINED) {
285 if (burst != 0) {
286 /*
287 * If number of beats fit in several whole bursts
288 * this configuration is allowed.
289 */
290 remaining = ((STM32_DMA_FIFO_SIZE / width) *
291 (threshold + 1) / 4) % burst;
292
293 if (remaining == 0)
294 return true;
295 } else {
296 return true;
297 }
298 }
299
300 return false;
301}
302
303static bool stm32_dma_is_burst_possible(u32 buf_len, u32 threshold)
304{
Pierre-Yves MORDRETcc832dc2018-09-11 09:31:16 +0200305 /*
306 * Buffer or period length has to be aligned on FIFO depth.
307 * Otherwise bytes may be stuck within FIFO at buffer or period
308 * length.
309 */
310 return ((buf_len % ((threshold + 1) * 4)) == 0);
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +0100311}
312
313static u32 stm32_dma_get_best_burst(u32 buf_len, u32 max_burst, u32 threshold,
314 enum dma_slave_buswidth width)
315{
316 u32 best_burst = max_burst;
317
318 if (best_burst == 1 || !stm32_dma_is_burst_possible(buf_len, threshold))
319 return 0;
320
321 while ((buf_len < best_burst * width && best_burst > 1) ||
322 !stm32_dma_fifo_threshold_is_allowed(best_burst, threshold,
323 width)) {
324 if (best_burst > STM32_DMA_MIN_BURST)
325 best_burst = best_burst >> 1;
326 else
327 best_burst = 0;
328 }
329
330 return best_burst;
331}
332
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200333static int stm32_dma_get_burst(struct stm32_dma_chan *chan, u32 maxburst)
334{
335 switch (maxburst) {
336 case 0:
337 case 1:
338 return STM32_DMA_BURST_SINGLE;
339 case 4:
340 return STM32_DMA_BURST_INCR4;
341 case 8:
342 return STM32_DMA_BURST_INCR8;
343 case 16:
344 return STM32_DMA_BURST_INCR16;
345 default:
346 dev_err(chan2dev(chan), "Dma burst size not supported\n");
347 return -EINVAL;
348 }
349}
350
351static void stm32_dma_set_fifo_config(struct stm32_dma_chan *chan,
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +0100352 u32 src_burst, u32 dst_burst)
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200353{
354 chan->chan_reg.dma_sfcr &= ~STM32_DMA_SFCR_MASK;
355 chan->chan_reg.dma_scr &= ~STM32_DMA_SCR_DMEIE;
356
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +0100357 if (!src_burst && !dst_burst) {
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200358 /* Using direct mode */
359 chan->chan_reg.dma_scr |= STM32_DMA_SCR_DMEIE;
360 } else {
361 /* Using FIFO mode */
362 chan->chan_reg.dma_sfcr |= STM32_DMA_SFCR_MASK;
363 }
364}
365
366static int stm32_dma_slave_config(struct dma_chan *c,
367 struct dma_slave_config *config)
368{
369 struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
370
371 memcpy(&chan->dma_sconfig, config, sizeof(*config));
372
373 chan->config_init = true;
374
375 return 0;
376}
377
378static u32 stm32_dma_irq_status(struct stm32_dma_chan *chan)
379{
380 struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
381 u32 flags, dma_isr;
382
383 /*
384 * Read "flags" from DMA_xISR register corresponding to the selected
385 * DMA channel at the correct bit offset inside that register.
386 *
387 * If (ch % 4) is 2 or 3, left shift the mask by 16 bits.
388 * If (ch % 4) is 1 or 3, additionally left shift the mask by 6 bits.
389 */
390
391 if (chan->id & 4)
392 dma_isr = stm32_dma_read(dmadev, STM32_DMA_HISR);
393 else
394 dma_isr = stm32_dma_read(dmadev, STM32_DMA_LISR);
395
396 flags = dma_isr >> (((chan->id & 2) << 3) | ((chan->id & 1) * 6));
397
Pierre Yves MORDRET9df3bd52018-03-13 17:42:05 +0100398 return flags & STM32_DMA_MASKI;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200399}
400
401static void stm32_dma_irq_clear(struct stm32_dma_chan *chan, u32 flags)
402{
403 struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
404 u32 dma_ifcr;
405
406 /*
407 * Write "flags" to the DMA_xIFCR register corresponding to the selected
408 * DMA channel at the correct bit offset inside that register.
409 *
410 * If (ch % 4) is 2 or 3, left shift the mask by 16 bits.
411 * If (ch % 4) is 1 or 3, additionally left shift the mask by 6 bits.
412 */
Pierre Yves MORDRET9df3bd52018-03-13 17:42:05 +0100413 flags &= STM32_DMA_MASKI;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200414 dma_ifcr = flags << (((chan->id & 2) << 3) | ((chan->id & 1) * 6));
415
416 if (chan->id & 4)
417 stm32_dma_write(dmadev, STM32_DMA_HIFCR, dma_ifcr);
418 else
419 stm32_dma_write(dmadev, STM32_DMA_LIFCR, dma_ifcr);
420}
421
422static int stm32_dma_disable_chan(struct stm32_dma_chan *chan)
423{
424 struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
Amelie Delaunay409ffc42020-01-29 16:36:27 +0100425 u32 dma_scr, id, reg;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200426
427 id = chan->id;
Amelie Delaunay409ffc42020-01-29 16:36:27 +0100428 reg = STM32_DMA_SCR(id);
429 dma_scr = stm32_dma_read(dmadev, reg);
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200430
431 if (dma_scr & STM32_DMA_SCR_EN) {
432 dma_scr &= ~STM32_DMA_SCR_EN;
Amelie Delaunay409ffc42020-01-29 16:36:27 +0100433 stm32_dma_write(dmadev, reg, dma_scr);
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200434
Amelie Delaunay409ffc42020-01-29 16:36:27 +0100435 return readl_relaxed_poll_timeout_atomic(dmadev->base + reg,
436 dma_scr, !(dma_scr & STM32_DMA_SCR_EN),
437 10, 1000000);
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200438 }
439
440 return 0;
441}
442
443static void stm32_dma_stop(struct stm32_dma_chan *chan)
444{
445 struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
446 u32 dma_scr, dma_sfcr, status;
447 int ret;
448
449 /* Disable interrupts */
450 dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id));
451 dma_scr &= ~STM32_DMA_SCR_IRQ_MASK;
452 stm32_dma_write(dmadev, STM32_DMA_SCR(chan->id), dma_scr);
453 dma_sfcr = stm32_dma_read(dmadev, STM32_DMA_SFCR(chan->id));
454 dma_sfcr &= ~STM32_DMA_SFCR_FEIE;
455 stm32_dma_write(dmadev, STM32_DMA_SFCR(chan->id), dma_sfcr);
456
457 /* Disable DMA */
458 ret = stm32_dma_disable_chan(chan);
459 if (ret < 0)
460 return;
461
462 /* Clear interrupt status if it is there */
463 status = stm32_dma_irq_status(chan);
464 if (status) {
465 dev_dbg(chan2dev(chan), "%s(): clearing interrupt: 0x%08x\n",
466 __func__, status);
467 stm32_dma_irq_clear(chan, status);
468 }
469
470 chan->busy = false;
471}
472
473static int stm32_dma_terminate_all(struct dma_chan *c)
474{
475 struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
476 unsigned long flags;
477 LIST_HEAD(head);
478
479 spin_lock_irqsave(&chan->vchan.lock, flags);
480
481 if (chan->busy) {
482 stm32_dma_stop(chan);
483 chan->desc = NULL;
484 }
485
486 vchan_get_all_descriptors(&chan->vchan, &head);
487 spin_unlock_irqrestore(&chan->vchan.lock, flags);
488 vchan_dma_desc_free_list(&chan->vchan, &head);
489
490 return 0;
491}
492
M'boumba Cedric Madiangadc808672016-12-13 14:40:50 +0100493static void stm32_dma_synchronize(struct dma_chan *c)
494{
495 struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
496
497 vchan_synchronize(&chan->vchan);
498}
499
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200500static void stm32_dma_dump_reg(struct stm32_dma_chan *chan)
501{
502 struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
503 u32 scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id));
504 u32 ndtr = stm32_dma_read(dmadev, STM32_DMA_SNDTR(chan->id));
505 u32 spar = stm32_dma_read(dmadev, STM32_DMA_SPAR(chan->id));
506 u32 sm0ar = stm32_dma_read(dmadev, STM32_DMA_SM0AR(chan->id));
507 u32 sm1ar = stm32_dma_read(dmadev, STM32_DMA_SM1AR(chan->id));
508 u32 sfcr = stm32_dma_read(dmadev, STM32_DMA_SFCR(chan->id));
509
510 dev_dbg(chan2dev(chan), "SCR: 0x%08x\n", scr);
511 dev_dbg(chan2dev(chan), "NDTR: 0x%08x\n", ndtr);
512 dev_dbg(chan2dev(chan), "SPAR: 0x%08x\n", spar);
513 dev_dbg(chan2dev(chan), "SM0AR: 0x%08x\n", sm0ar);
514 dev_dbg(chan2dev(chan), "SM1AR: 0x%08x\n", sm1ar);
515 dev_dbg(chan2dev(chan), "SFCR: 0x%08x\n", sfcr);
516}
517
Pierre Yves MORDRETe57cb3b2018-03-13 17:42:06 +0100518static void stm32_dma_configure_next_sg(struct stm32_dma_chan *chan);
519
M'boumba Cedric Madianga8d1b76f2016-12-13 14:40:47 +0100520static void stm32_dma_start_transfer(struct stm32_dma_chan *chan)
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200521{
522 struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
523 struct virt_dma_desc *vdesc;
524 struct stm32_dma_sg_req *sg_req;
525 struct stm32_dma_chan_reg *reg;
526 u32 status;
527 int ret;
528
529 ret = stm32_dma_disable_chan(chan);
530 if (ret < 0)
M'boumba Cedric Madianga8d1b76f2016-12-13 14:40:47 +0100531 return;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200532
533 if (!chan->desc) {
534 vdesc = vchan_next_desc(&chan->vchan);
535 if (!vdesc)
M'boumba Cedric Madianga8d1b76f2016-12-13 14:40:47 +0100536 return;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200537
538 chan->desc = to_stm32_dma_desc(vdesc);
539 chan->next_sg = 0;
540 }
541
542 if (chan->next_sg == chan->desc->num_sgs)
543 chan->next_sg = 0;
544
545 sg_req = &chan->desc->sg_req[chan->next_sg];
546 reg = &sg_req->chan_reg;
547
Pierre-Yves MORDRET22a0bb22020-01-29 16:36:24 +0100548 reg->dma_scr &= ~STM32_DMA_SCR_EN;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200549 stm32_dma_write(dmadev, STM32_DMA_SCR(chan->id), reg->dma_scr);
550 stm32_dma_write(dmadev, STM32_DMA_SPAR(chan->id), reg->dma_spar);
551 stm32_dma_write(dmadev, STM32_DMA_SM0AR(chan->id), reg->dma_sm0ar);
552 stm32_dma_write(dmadev, STM32_DMA_SFCR(chan->id), reg->dma_sfcr);
553 stm32_dma_write(dmadev, STM32_DMA_SM1AR(chan->id), reg->dma_sm1ar);
554 stm32_dma_write(dmadev, STM32_DMA_SNDTR(chan->id), reg->dma_sndtr);
555
556 chan->next_sg++;
557
558 /* Clear interrupt status if it is there */
559 status = stm32_dma_irq_status(chan);
560 if (status)
561 stm32_dma_irq_clear(chan, status);
562
Pierre Yves MORDRETe57cb3b2018-03-13 17:42:06 +0100563 if (chan->desc->cyclic)
564 stm32_dma_configure_next_sg(chan);
565
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200566 stm32_dma_dump_reg(chan);
567
568 /* Start DMA */
569 reg->dma_scr |= STM32_DMA_SCR_EN;
570 stm32_dma_write(dmadev, STM32_DMA_SCR(chan->id), reg->dma_scr);
571
572 chan->busy = true;
573
Benjamin Gaignard90ec93c2018-07-06 15:02:20 +0200574 dev_dbg(chan2dev(chan), "vchan %pK: started\n", &chan->vchan);
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200575}
576
577static void stm32_dma_configure_next_sg(struct stm32_dma_chan *chan)
578{
579 struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
580 struct stm32_dma_sg_req *sg_req;
581 u32 dma_scr, dma_sm0ar, dma_sm1ar, id;
582
583 id = chan->id;
584 dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(id));
585
586 if (dma_scr & STM32_DMA_SCR_DBM) {
587 if (chan->next_sg == chan->desc->num_sgs)
588 chan->next_sg = 0;
589
590 sg_req = &chan->desc->sg_req[chan->next_sg];
591
592 if (dma_scr & STM32_DMA_SCR_CT) {
593 dma_sm0ar = sg_req->chan_reg.dma_sm0ar;
594 stm32_dma_write(dmadev, STM32_DMA_SM0AR(id), dma_sm0ar);
595 dev_dbg(chan2dev(chan), "CT=1 <=> SM0AR: 0x%08x\n",
596 stm32_dma_read(dmadev, STM32_DMA_SM0AR(id)));
597 } else {
598 dma_sm1ar = sg_req->chan_reg.dma_sm1ar;
599 stm32_dma_write(dmadev, STM32_DMA_SM1AR(id), dma_sm1ar);
600 dev_dbg(chan2dev(chan), "CT=0 <=> SM1AR: 0x%08x\n",
601 stm32_dma_read(dmadev, STM32_DMA_SM1AR(id)));
602 }
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200603 }
604}
605
606static void stm32_dma_handle_chan_done(struct stm32_dma_chan *chan)
607{
608 if (chan->desc) {
609 if (chan->desc->cyclic) {
610 vchan_cyclic_callback(&chan->desc->vdesc);
M'boumba Cedric Madianga2b12c5582016-12-13 14:40:48 +0100611 chan->next_sg++;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200612 stm32_dma_configure_next_sg(chan);
613 } else {
614 chan->busy = false;
615 if (chan->next_sg == chan->desc->num_sgs) {
616 list_del(&chan->desc->vdesc.node);
617 vchan_cookie_complete(&chan->desc->vdesc);
618 chan->desc = NULL;
619 }
620 stm32_dma_start_transfer(chan);
621 }
622 }
623}
624
625static irqreturn_t stm32_dma_chan_irq(int irq, void *devid)
626{
627 struct stm32_dma_chan *chan = devid;
628 struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
Pierre-Yves MORDRETca4c72c2019-01-03 11:17:29 +0100629 u32 status, scr, sfcr;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200630
631 spin_lock(&chan->vchan.lock);
632
633 status = stm32_dma_irq_status(chan);
634 scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id));
Pierre-Yves MORDRETca4c72c2019-01-03 11:17:29 +0100635 sfcr = stm32_dma_read(dmadev, STM32_DMA_SFCR(chan->id));
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200636
Pierre Yves MORDRETc2d86b12018-03-13 17:42:03 +0100637 if (status & STM32_DMA_TCI) {
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200638 stm32_dma_irq_clear(chan, STM32_DMA_TCI);
Pierre Yves MORDRETc2d86b12018-03-13 17:42:03 +0100639 if (scr & STM32_DMA_SCR_TCIE)
640 stm32_dma_handle_chan_done(chan);
641 status &= ~STM32_DMA_TCI;
642 }
643 if (status & STM32_DMA_HTI) {
644 stm32_dma_irq_clear(chan, STM32_DMA_HTI);
645 status &= ~STM32_DMA_HTI;
646 }
647 if (status & STM32_DMA_FEI) {
648 stm32_dma_irq_clear(chan, STM32_DMA_FEI);
649 status &= ~STM32_DMA_FEI;
Pierre-Yves MORDRETca4c72c2019-01-03 11:17:29 +0100650 if (sfcr & STM32_DMA_SFCR_FEIE) {
651 if (!(scr & STM32_DMA_SCR_EN))
652 dev_err(chan2dev(chan), "FIFO Error\n");
653 else
654 dev_dbg(chan2dev(chan), "FIFO over/underrun\n");
655 }
Pierre Yves MORDRETc2d86b12018-03-13 17:42:03 +0100656 }
657 if (status) {
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200658 stm32_dma_irq_clear(chan, status);
659 dev_err(chan2dev(chan), "DMA error: status=0x%08x\n", status);
Pierre Yves MORDRETc2d86b12018-03-13 17:42:03 +0100660 if (!(scr & STM32_DMA_SCR_EN))
661 dev_err(chan2dev(chan), "chan disabled by HW\n");
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200662 }
663
664 spin_unlock(&chan->vchan.lock);
665
666 return IRQ_HANDLED;
667}
668
669static void stm32_dma_issue_pending(struct dma_chan *c)
670{
671 struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
672 unsigned long flags;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200673
674 spin_lock_irqsave(&chan->vchan.lock, flags);
M'boumba Cedric Madianga8d1b76f2016-12-13 14:40:47 +0100675 if (vchan_issue_pending(&chan->vchan) && !chan->desc && !chan->busy) {
Benjamin Gaignard90ec93c2018-07-06 15:02:20 +0200676 dev_dbg(chan2dev(chan), "vchan %pK: issued\n", &chan->vchan);
M'boumba Cedric Madianga8d1b76f2016-12-13 14:40:47 +0100677 stm32_dma_start_transfer(chan);
Pierre Yves MORDRETe57cb3b2018-03-13 17:42:06 +0100678
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200679 }
680 spin_unlock_irqrestore(&chan->vchan.lock, flags);
681}
682
683static int stm32_dma_set_xfer_param(struct stm32_dma_chan *chan,
684 enum dma_transfer_direction direction,
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +0100685 enum dma_slave_buswidth *buswidth,
686 u32 buf_len)
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200687{
688 enum dma_slave_buswidth src_addr_width, dst_addr_width;
689 int src_bus_width, dst_bus_width;
690 int src_burst_size, dst_burst_size;
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +0100691 u32 src_maxburst, dst_maxburst, src_best_burst, dst_best_burst;
692 u32 dma_scr, threshold;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200693
694 src_addr_width = chan->dma_sconfig.src_addr_width;
695 dst_addr_width = chan->dma_sconfig.dst_addr_width;
696 src_maxburst = chan->dma_sconfig.src_maxburst;
697 dst_maxburst = chan->dma_sconfig.dst_maxburst;
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +0100698 threshold = chan->threshold;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200699
700 switch (direction) {
701 case DMA_MEM_TO_DEV:
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +0100702 /* Set device data size */
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200703 dst_bus_width = stm32_dma_get_width(chan, dst_addr_width);
704 if (dst_bus_width < 0)
705 return dst_bus_width;
706
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +0100707 /* Set device burst size */
708 dst_best_burst = stm32_dma_get_best_burst(buf_len,
709 dst_maxburst,
710 threshold,
711 dst_addr_width);
712
713 dst_burst_size = stm32_dma_get_burst(chan, dst_best_burst);
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200714 if (dst_burst_size < 0)
715 return dst_burst_size;
716
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +0100717 /* Set memory data size */
718 src_addr_width = stm32_dma_get_max_width(buf_len, threshold);
719 chan->mem_width = src_addr_width;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200720 src_bus_width = stm32_dma_get_width(chan, src_addr_width);
721 if (src_bus_width < 0)
722 return src_bus_width;
723
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +0100724 /* Set memory burst size */
725 src_maxburst = STM32_DMA_MAX_BURST;
726 src_best_burst = stm32_dma_get_best_burst(buf_len,
727 src_maxburst,
728 threshold,
729 src_addr_width);
730 src_burst_size = stm32_dma_get_burst(chan, src_best_burst);
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200731 if (src_burst_size < 0)
732 return src_burst_size;
733
734 dma_scr = STM32_DMA_SCR_DIR(STM32_DMA_MEM_TO_DEV) |
735 STM32_DMA_SCR_PSIZE(dst_bus_width) |
736 STM32_DMA_SCR_MSIZE(src_bus_width) |
737 STM32_DMA_SCR_PBURST(dst_burst_size) |
738 STM32_DMA_SCR_MBURST(src_burst_size);
739
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +0100740 /* Set FIFO threshold */
741 chan->chan_reg.dma_sfcr &= ~STM32_DMA_SFCR_FTH_MASK;
742 chan->chan_reg.dma_sfcr |= STM32_DMA_SFCR_FTH(threshold);
743
744 /* Set peripheral address */
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200745 chan->chan_reg.dma_spar = chan->dma_sconfig.dst_addr;
746 *buswidth = dst_addr_width;
747 break;
748
749 case DMA_DEV_TO_MEM:
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +0100750 /* Set device data size */
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200751 src_bus_width = stm32_dma_get_width(chan, src_addr_width);
752 if (src_bus_width < 0)
753 return src_bus_width;
754
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +0100755 /* Set device burst size */
756 src_best_burst = stm32_dma_get_best_burst(buf_len,
757 src_maxburst,
758 threshold,
759 src_addr_width);
760 chan->mem_burst = src_best_burst;
761 src_burst_size = stm32_dma_get_burst(chan, src_best_burst);
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200762 if (src_burst_size < 0)
763 return src_burst_size;
764
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +0100765 /* Set memory data size */
766 dst_addr_width = stm32_dma_get_max_width(buf_len, threshold);
767 chan->mem_width = dst_addr_width;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200768 dst_bus_width = stm32_dma_get_width(chan, dst_addr_width);
769 if (dst_bus_width < 0)
770 return dst_bus_width;
771
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +0100772 /* Set memory burst size */
773 dst_maxburst = STM32_DMA_MAX_BURST;
774 dst_best_burst = stm32_dma_get_best_burst(buf_len,
775 dst_maxburst,
776 threshold,
777 dst_addr_width);
778 chan->mem_burst = dst_best_burst;
779 dst_burst_size = stm32_dma_get_burst(chan, dst_best_burst);
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200780 if (dst_burst_size < 0)
781 return dst_burst_size;
782
783 dma_scr = STM32_DMA_SCR_DIR(STM32_DMA_DEV_TO_MEM) |
784 STM32_DMA_SCR_PSIZE(src_bus_width) |
785 STM32_DMA_SCR_MSIZE(dst_bus_width) |
786 STM32_DMA_SCR_PBURST(src_burst_size) |
787 STM32_DMA_SCR_MBURST(dst_burst_size);
788
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +0100789 /* Set FIFO threshold */
790 chan->chan_reg.dma_sfcr &= ~STM32_DMA_SFCR_FTH_MASK;
791 chan->chan_reg.dma_sfcr |= STM32_DMA_SFCR_FTH(threshold);
792
793 /* Set peripheral address */
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200794 chan->chan_reg.dma_spar = chan->dma_sconfig.src_addr;
795 *buswidth = chan->dma_sconfig.src_addr_width;
796 break;
797
798 default:
799 dev_err(chan2dev(chan), "Dma direction is not supported\n");
800 return -EINVAL;
801 }
802
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +0100803 stm32_dma_set_fifo_config(chan, src_best_burst, dst_best_burst);
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200804
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +0100805 /* Set DMA control register */
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200806 chan->chan_reg.dma_scr &= ~(STM32_DMA_SCR_DIR_MASK |
807 STM32_DMA_SCR_PSIZE_MASK | STM32_DMA_SCR_MSIZE_MASK |
808 STM32_DMA_SCR_PBURST_MASK | STM32_DMA_SCR_MBURST_MASK);
809 chan->chan_reg.dma_scr |= dma_scr;
810
811 return 0;
812}
813
814static void stm32_dma_clear_reg(struct stm32_dma_chan_reg *regs)
815{
816 memset(regs, 0, sizeof(struct stm32_dma_chan_reg));
817}
818
819static struct dma_async_tx_descriptor *stm32_dma_prep_slave_sg(
820 struct dma_chan *c, struct scatterlist *sgl,
821 u32 sg_len, enum dma_transfer_direction direction,
822 unsigned long flags, void *context)
823{
824 struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
825 struct stm32_dma_desc *desc;
826 struct scatterlist *sg;
827 enum dma_slave_buswidth buswidth;
828 u32 nb_data_items;
829 int i, ret;
830
831 if (!chan->config_init) {
832 dev_err(chan2dev(chan), "dma channel is not configured\n");
833 return NULL;
834 }
835
836 if (sg_len < 1) {
837 dev_err(chan2dev(chan), "Invalid segment length %d\n", sg_len);
838 return NULL;
839 }
840
Gustavo A. R. Silva402096c2019-08-30 11:14:23 -0500841 desc = kzalloc(struct_size(desc, sg_req, sg_len), GFP_NOWAIT);
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200842 if (!desc)
843 return NULL;
844
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200845 /* Set peripheral flow controller */
846 if (chan->dma_sconfig.device_fc)
847 chan->chan_reg.dma_scr |= STM32_DMA_SCR_PFCTRL;
848 else
849 chan->chan_reg.dma_scr &= ~STM32_DMA_SCR_PFCTRL;
850
851 for_each_sg(sgl, sg, sg_len, i) {
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +0100852 ret = stm32_dma_set_xfer_param(chan, direction, &buswidth,
853 sg_dma_len(sg));
854 if (ret < 0)
855 goto err;
856
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200857 desc->sg_req[i].len = sg_dma_len(sg);
858
859 nb_data_items = desc->sg_req[i].len / buswidth;
Pierre Yves MORDRET80a76952018-03-13 17:42:04 +0100860 if (nb_data_items > STM32_DMA_ALIGNED_MAX_DATA_ITEMS) {
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200861 dev_err(chan2dev(chan), "nb items not supported\n");
862 goto err;
863 }
864
865 stm32_dma_clear_reg(&desc->sg_req[i].chan_reg);
866 desc->sg_req[i].chan_reg.dma_scr = chan->chan_reg.dma_scr;
867 desc->sg_req[i].chan_reg.dma_sfcr = chan->chan_reg.dma_sfcr;
868 desc->sg_req[i].chan_reg.dma_spar = chan->chan_reg.dma_spar;
869 desc->sg_req[i].chan_reg.dma_sm0ar = sg_dma_address(sg);
870 desc->sg_req[i].chan_reg.dma_sm1ar = sg_dma_address(sg);
871 desc->sg_req[i].chan_reg.dma_sndtr = nb_data_items;
872 }
873
874 desc->num_sgs = sg_len;
875 desc->cyclic = false;
876
877 return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
878
879err:
880 kfree(desc);
881 return NULL;
882}
883
884static struct dma_async_tx_descriptor *stm32_dma_prep_dma_cyclic(
885 struct dma_chan *c, dma_addr_t buf_addr, size_t buf_len,
886 size_t period_len, enum dma_transfer_direction direction,
887 unsigned long flags)
888{
889 struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
890 struct stm32_dma_desc *desc;
891 enum dma_slave_buswidth buswidth;
892 u32 num_periods, nb_data_items;
893 int i, ret;
894
895 if (!buf_len || !period_len) {
896 dev_err(chan2dev(chan), "Invalid buffer/period len\n");
897 return NULL;
898 }
899
900 if (!chan->config_init) {
901 dev_err(chan2dev(chan), "dma channel is not configured\n");
902 return NULL;
903 }
904
905 if (buf_len % period_len) {
906 dev_err(chan2dev(chan), "buf_len not multiple of period_len\n");
907 return NULL;
908 }
909
910 /*
911 * We allow to take more number of requests till DMA is
912 * not started. The driver will loop over all requests.
913 * Once DMA is started then new requests can be queued only after
914 * terminating the DMA.
915 */
916 if (chan->busy) {
917 dev_err(chan2dev(chan), "Request not allowed when dma busy\n");
918 return NULL;
919 }
920
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +0100921 ret = stm32_dma_set_xfer_param(chan, direction, &buswidth, period_len);
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200922 if (ret < 0)
923 return NULL;
924
925 nb_data_items = period_len / buswidth;
Pierre Yves MORDRET80a76952018-03-13 17:42:04 +0100926 if (nb_data_items > STM32_DMA_ALIGNED_MAX_DATA_ITEMS) {
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200927 dev_err(chan2dev(chan), "number of items not supported\n");
928 return NULL;
929 }
930
931 /* Enable Circular mode or double buffer mode */
932 if (buf_len == period_len)
933 chan->chan_reg.dma_scr |= STM32_DMA_SCR_CIRC;
934 else
935 chan->chan_reg.dma_scr |= STM32_DMA_SCR_DBM;
936
937 /* Clear periph ctrl if client set it */
938 chan->chan_reg.dma_scr &= ~STM32_DMA_SCR_PFCTRL;
939
940 num_periods = buf_len / period_len;
941
Gustavo A. R. Silva402096c2019-08-30 11:14:23 -0500942 desc = kzalloc(struct_size(desc, sg_req, num_periods), GFP_NOWAIT);
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200943 if (!desc)
944 return NULL;
945
946 for (i = 0; i < num_periods; i++) {
947 desc->sg_req[i].len = period_len;
948
949 stm32_dma_clear_reg(&desc->sg_req[i].chan_reg);
950 desc->sg_req[i].chan_reg.dma_scr = chan->chan_reg.dma_scr;
951 desc->sg_req[i].chan_reg.dma_sfcr = chan->chan_reg.dma_sfcr;
952 desc->sg_req[i].chan_reg.dma_spar = chan->chan_reg.dma_spar;
953 desc->sg_req[i].chan_reg.dma_sm0ar = buf_addr;
954 desc->sg_req[i].chan_reg.dma_sm1ar = buf_addr;
955 desc->sg_req[i].chan_reg.dma_sndtr = nb_data_items;
956 buf_addr += period_len;
957 }
958
959 desc->num_sgs = num_periods;
960 desc->cyclic = true;
961
962 return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
963}
964
965static struct dma_async_tx_descriptor *stm32_dma_prep_dma_memcpy(
966 struct dma_chan *c, dma_addr_t dest,
967 dma_addr_t src, size_t len, unsigned long flags)
968{
969 struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +0100970 enum dma_slave_buswidth max_width;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200971 struct stm32_dma_desc *desc;
972 size_t xfer_count, offset;
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +0100973 u32 num_sgs, best_burst, dma_burst, threshold;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200974 int i;
975
Pierre Yves MORDRET80a76952018-03-13 17:42:04 +0100976 num_sgs = DIV_ROUND_UP(len, STM32_DMA_ALIGNED_MAX_DATA_ITEMS);
Gustavo A. R. Silva402096c2019-08-30 11:14:23 -0500977 desc = kzalloc(struct_size(desc, sg_req, num_sgs), GFP_NOWAIT);
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200978 if (!desc)
979 return NULL;
980
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +0100981 threshold = chan->threshold;
982
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200983 for (offset = 0, i = 0; offset < len; offset += xfer_count, i++) {
984 xfer_count = min_t(size_t, len - offset,
Pierre Yves MORDRET80a76952018-03-13 17:42:04 +0100985 STM32_DMA_ALIGNED_MAX_DATA_ITEMS);
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200986
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +0100987 /* Compute best burst size */
988 max_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
989 best_burst = stm32_dma_get_best_burst(len, STM32_DMA_MAX_BURST,
990 threshold, max_width);
991 dma_burst = stm32_dma_get_burst(chan, best_burst);
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200992
993 stm32_dma_clear_reg(&desc->sg_req[i].chan_reg);
994 desc->sg_req[i].chan_reg.dma_scr =
995 STM32_DMA_SCR_DIR(STM32_DMA_MEM_TO_MEM) |
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +0100996 STM32_DMA_SCR_PBURST(dma_burst) |
997 STM32_DMA_SCR_MBURST(dma_burst) |
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200998 STM32_DMA_SCR_MINC |
999 STM32_DMA_SCR_PINC |
1000 STM32_DMA_SCR_TCIE |
1001 STM32_DMA_SCR_TEIE;
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +01001002 desc->sg_req[i].chan_reg.dma_sfcr |= STM32_DMA_SFCR_MASK;
1003 desc->sg_req[i].chan_reg.dma_sfcr |=
1004 STM32_DMA_SFCR_FTH(threshold);
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001005 desc->sg_req[i].chan_reg.dma_spar = src + offset;
1006 desc->sg_req[i].chan_reg.dma_sm0ar = dest + offset;
1007 desc->sg_req[i].chan_reg.dma_sndtr = xfer_count;
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +01001008 desc->sg_req[i].len = xfer_count;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001009 }
1010
1011 desc->num_sgs = num_sgs;
1012 desc->cyclic = false;
1013
1014 return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
1015}
1016
M'boumba Cedric Madianga2b12c5582016-12-13 14:40:48 +01001017static u32 stm32_dma_get_remaining_bytes(struct stm32_dma_chan *chan)
1018{
1019 u32 dma_scr, width, ndtr;
1020 struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
1021
1022 dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id));
1023 width = STM32_DMA_SCR_PSIZE_GET(dma_scr);
1024 ndtr = stm32_dma_read(dmadev, STM32_DMA_SNDTR(chan->id));
1025
1026 return ndtr << width;
1027}
1028
Arnaud Pouliquen2a4885a2019-05-02 11:28:42 +02001029/**
1030 * stm32_dma_is_current_sg - check that expected sg_req is currently transferred
1031 * @chan: dma channel
1032 *
1033 * This function called when IRQ are disable, checks that the hardware has not
1034 * switched on the next transfer in double buffer mode. The test is done by
1035 * comparing the next_sg memory address with the hardware related register
1036 * (based on CT bit value).
1037 *
1038 * Returns true if expected current transfer is still running or double
1039 * buffer mode is not activated.
1040 */
1041static bool stm32_dma_is_current_sg(struct stm32_dma_chan *chan)
1042{
1043 struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
1044 struct stm32_dma_sg_req *sg_req;
1045 u32 dma_scr, dma_smar, id;
1046
1047 id = chan->id;
1048 dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(id));
1049
1050 if (!(dma_scr & STM32_DMA_SCR_DBM))
1051 return true;
1052
1053 sg_req = &chan->desc->sg_req[chan->next_sg];
1054
1055 if (dma_scr & STM32_DMA_SCR_CT) {
1056 dma_smar = stm32_dma_read(dmadev, STM32_DMA_SM0AR(id));
1057 return (dma_smar == sg_req->chan_reg.dma_sm0ar);
1058 }
1059
1060 dma_smar = stm32_dma_read(dmadev, STM32_DMA_SM1AR(id));
1061
1062 return (dma_smar == sg_req->chan_reg.dma_sm1ar);
1063}
1064
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001065static size_t stm32_dma_desc_residue(struct stm32_dma_chan *chan,
1066 struct stm32_dma_desc *desc,
1067 u32 next_sg)
1068{
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +01001069 u32 modulo, burst_size;
Arnaud Pouliquen2a4885a2019-05-02 11:28:42 +02001070 u32 residue;
1071 u32 n_sg = next_sg;
1072 struct stm32_dma_sg_req *sg_req = &chan->desc->sg_req[chan->next_sg];
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001073 int i;
1074
M'boumba Cedric Madianga2b12c5582016-12-13 14:40:48 +01001075 /*
Arnaud Pouliquen2a4885a2019-05-02 11:28:42 +02001076 * Calculate the residue means compute the descriptors
1077 * information:
1078 * - the sg_req currently transferred
1079 * - the Hardware remaining position in this sg (NDTR bits field).
1080 *
1081 * A race condition may occur if DMA is running in cyclic or double
1082 * buffer mode, since the DMA register are automatically reloaded at end
1083 * of period transfer. The hardware may have switched to the next
1084 * transfer (CT bit updated) just before the position (SxNDTR reg) is
1085 * read.
1086 * In this case the SxNDTR reg could (or not) correspond to the new
1087 * transfer position, and not the expected one.
1088 * The strategy implemented in the stm32 driver is to:
1089 * - read the SxNDTR register
1090 * - crosscheck that hardware is still in current transfer.
1091 * In case of switch, we can assume that the DMA is at the beginning of
1092 * the next transfer. So we approximate the residue in consequence, by
1093 * pointing on the beginning of next transfer.
1094 *
1095 * This race condition doesn't apply for none cyclic mode, as double
1096 * buffer is not used. In such situation registers are updated by the
1097 * software.
M'boumba Cedric Madianga2b12c5582016-12-13 14:40:48 +01001098 */
Arnaud Pouliquen2a4885a2019-05-02 11:28:42 +02001099
1100 residue = stm32_dma_get_remaining_bytes(chan);
1101
1102 if (!stm32_dma_is_current_sg(chan)) {
1103 n_sg++;
1104 if (n_sg == chan->desc->num_sgs)
1105 n_sg = 0;
1106 residue = sg_req->len;
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +01001107 }
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001108
M'boumba Cedric Madianga2b12c5582016-12-13 14:40:48 +01001109 /*
Arnaud Pouliquen2a4885a2019-05-02 11:28:42 +02001110 * In cyclic mode, for the last period, residue = remaining bytes
1111 * from NDTR,
1112 * else for all other periods in cyclic mode, and in sg mode,
1113 * residue = remaining bytes from NDTR + remaining
1114 * periods/sg to be transferred
M'boumba Cedric Madianga2b12c5582016-12-13 14:40:48 +01001115 */
Arnaud Pouliquen2a4885a2019-05-02 11:28:42 +02001116 if (!chan->desc->cyclic || n_sg != 0)
1117 for (i = n_sg; i < desc->num_sgs; i++)
1118 residue += desc->sg_req[i].len;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001119
Pierre Yves MORDRETa2b61032018-03-13 17:42:02 +01001120 if (!chan->mem_burst)
1121 return residue;
1122
1123 burst_size = chan->mem_burst * chan->mem_width;
1124 modulo = residue % burst_size;
1125 if (modulo)
1126 residue = residue - modulo + burst_size;
1127
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001128 return residue;
1129}
1130
1131static enum dma_status stm32_dma_tx_status(struct dma_chan *c,
1132 dma_cookie_t cookie,
1133 struct dma_tx_state *state)
1134{
1135 struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
1136 struct virt_dma_desc *vdesc;
1137 enum dma_status status;
1138 unsigned long flags;
M'boumba Cedric Madianga57b5a322016-12-13 14:40:46 +01001139 u32 residue = 0;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001140
1141 status = dma_cookie_status(c, cookie, state);
Pierre Yves MORDRET249d5532018-03-13 17:42:07 +01001142 if (status == DMA_COMPLETE || !state)
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001143 return status;
1144
1145 spin_lock_irqsave(&chan->vchan.lock, flags);
1146 vdesc = vchan_find_desc(&chan->vchan, cookie);
M'boumba Cedric Madianga57b5a322016-12-13 14:40:46 +01001147 if (chan->desc && cookie == chan->desc->vdesc.tx.cookie)
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001148 residue = stm32_dma_desc_residue(chan, chan->desc,
1149 chan->next_sg);
M'boumba Cedric Madianga57b5a322016-12-13 14:40:46 +01001150 else if (vdesc)
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001151 residue = stm32_dma_desc_residue(chan,
1152 to_stm32_dma_desc(vdesc), 0);
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001153 dma_set_residue(state, residue);
1154
1155 spin_unlock_irqrestore(&chan->vchan.lock, flags);
1156
1157 return status;
1158}
1159
1160static int stm32_dma_alloc_chan_resources(struct dma_chan *c)
1161{
1162 struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
1163 struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
1164 int ret;
1165
1166 chan->config_init = false;
Pierre-Yves MORDRET48bc73b2019-01-03 11:17:08 +01001167
1168 ret = pm_runtime_get_sync(dmadev->ddev.dev);
1169 if (ret < 0)
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001170 return ret;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001171
1172 ret = stm32_dma_disable_chan(chan);
1173 if (ret < 0)
Pierre-Yves MORDRET48bc73b2019-01-03 11:17:08 +01001174 pm_runtime_put(dmadev->ddev.dev);
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001175
1176 return ret;
1177}
1178
1179static void stm32_dma_free_chan_resources(struct dma_chan *c)
1180{
1181 struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
1182 struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
1183 unsigned long flags;
1184
1185 dev_dbg(chan2dev(chan), "Freeing channel %d\n", chan->id);
1186
1187 if (chan->busy) {
1188 spin_lock_irqsave(&chan->vchan.lock, flags);
1189 stm32_dma_stop(chan);
1190 chan->desc = NULL;
1191 spin_unlock_irqrestore(&chan->vchan.lock, flags);
1192 }
1193
Pierre-Yves MORDRET48bc73b2019-01-03 11:17:08 +01001194 pm_runtime_put(dmadev->ddev.dev);
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001195
1196 vchan_free_chan_resources(to_virt_chan(c));
1197}
1198
1199static void stm32_dma_desc_free(struct virt_dma_desc *vdesc)
1200{
1201 kfree(container_of(vdesc, struct stm32_dma_desc, vdesc));
1202}
1203
Vinod Koule97adb42016-09-02 15:59:10 +05301204static void stm32_dma_set_config(struct stm32_dma_chan *chan,
Pierre Yves MORDRET249d5532018-03-13 17:42:07 +01001205 struct stm32_dma_cfg *cfg)
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001206{
1207 stm32_dma_clear_reg(&chan->chan_reg);
1208
1209 chan->chan_reg.dma_scr = cfg->stream_config & STM32_DMA_SCR_CFG_MASK;
1210 chan->chan_reg.dma_scr |= STM32_DMA_SCR_REQ(cfg->request_line);
1211
1212 /* Enable Interrupts */
1213 chan->chan_reg.dma_scr |= STM32_DMA_SCR_TEIE | STM32_DMA_SCR_TCIE;
1214
Pierre Yves MORDRET951f44c2018-03-13 17:42:01 +01001215 chan->threshold = STM32_DMA_THRESHOLD_FTR_GET(cfg->features);
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001216}
1217
1218static struct dma_chan *stm32_dma_of_xlate(struct of_phandle_args *dma_spec,
1219 struct of_dma *ofdma)
1220{
1221 struct stm32_dma_device *dmadev = ofdma->of_dma_data;
M'boumba Cedric Madianga5df4eb42017-01-05 09:09:40 +01001222 struct device *dev = dmadev->ddev.dev;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001223 struct stm32_dma_cfg cfg;
1224 struct stm32_dma_chan *chan;
1225 struct dma_chan *c;
1226
M'boumba Cedric Madianga5df4eb42017-01-05 09:09:40 +01001227 if (dma_spec->args_count < 4) {
1228 dev_err(dev, "Bad number of cells\n");
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001229 return NULL;
M'boumba Cedric Madianga5df4eb42017-01-05 09:09:40 +01001230 }
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001231
1232 cfg.channel_id = dma_spec->args[0];
1233 cfg.request_line = dma_spec->args[1];
1234 cfg.stream_config = dma_spec->args[2];
Pierre Yves MORDRET951f44c2018-03-13 17:42:01 +01001235 cfg.features = dma_spec->args[3];
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001236
Pierre Yves MORDRET249d5532018-03-13 17:42:07 +01001237 if (cfg.channel_id >= STM32_DMA_MAX_CHANNELS ||
1238 cfg.request_line >= STM32_DMA_MAX_REQUEST_ID) {
M'boumba Cedric Madianga5df4eb42017-01-05 09:09:40 +01001239 dev_err(dev, "Bad channel and/or request id\n");
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001240 return NULL;
M'boumba Cedric Madianga5df4eb42017-01-05 09:09:40 +01001241 }
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001242
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001243 chan = &dmadev->chan[cfg.channel_id];
1244
1245 c = dma_get_slave_channel(&chan->vchan.chan);
M'boumba Cedric Madianga5df4eb42017-01-05 09:09:40 +01001246 if (!c) {
Colin Ian King041cf7e2017-02-21 18:30:45 +00001247 dev_err(dev, "No more channels available\n");
M'boumba Cedric Madianga5df4eb42017-01-05 09:09:40 +01001248 return NULL;
1249 }
1250
1251 stm32_dma_set_config(chan, &cfg);
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001252
1253 return c;
1254}
1255
1256static const struct of_device_id stm32_dma_of_match[] = {
1257 { .compatible = "st,stm32-dma", },
1258 { /* sentinel */ },
1259};
1260MODULE_DEVICE_TABLE(of, stm32_dma_of_match);
1261
1262static int stm32_dma_probe(struct platform_device *pdev)
1263{
1264 struct stm32_dma_chan *chan;
1265 struct stm32_dma_device *dmadev;
1266 struct dma_device *dd;
1267 const struct of_device_id *match;
1268 struct resource *res;
Etienne Carriere8cf1e0f2020-01-29 16:36:22 +01001269 struct reset_control *rst;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001270 int i, ret;
1271
1272 match = of_match_device(stm32_dma_of_match, &pdev->dev);
1273 if (!match) {
1274 dev_err(&pdev->dev, "Error: No device match found\n");
1275 return -ENODEV;
1276 }
1277
1278 dmadev = devm_kzalloc(&pdev->dev, sizeof(*dmadev), GFP_KERNEL);
1279 if (!dmadev)
1280 return -ENOMEM;
1281
1282 dd = &dmadev->ddev;
1283
1284 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1285 dmadev->base = devm_ioremap_resource(&pdev->dev, res);
1286 if (IS_ERR(dmadev->base))
1287 return PTR_ERR(dmadev->base);
1288
1289 dmadev->clk = devm_clk_get(&pdev->dev, NULL);
1290 if (IS_ERR(dmadev->clk)) {
Etienne Carriere615eee22020-01-29 16:36:23 +01001291 ret = PTR_ERR(dmadev->clk);
1292 if (ret != -EPROBE_DEFER)
1293 dev_err(&pdev->dev, "Can't get clock\n");
1294 return ret;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001295 }
1296
Pierre-Yves MORDRET48bc73b2019-01-03 11:17:08 +01001297 ret = clk_prepare_enable(dmadev->clk);
1298 if (ret < 0) {
1299 dev_err(&pdev->dev, "clk_prep_enable error: %d\n", ret);
1300 return ret;
1301 }
1302
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001303 dmadev->mem2mem = of_property_read_bool(pdev->dev.of_node,
1304 "st,mem2mem");
1305
Etienne Carriere8cf1e0f2020-01-29 16:36:22 +01001306 rst = devm_reset_control_get(&pdev->dev, NULL);
Etienne Carriere615eee22020-01-29 16:36:23 +01001307 if (IS_ERR(rst)) {
1308 ret = PTR_ERR(rst);
1309 if (ret == -EPROBE_DEFER)
1310 goto clk_free;
1311 } else {
Etienne Carriere8cf1e0f2020-01-29 16:36:22 +01001312 reset_control_assert(rst);
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001313 udelay(2);
Etienne Carriere8cf1e0f2020-01-29 16:36:22 +01001314 reset_control_deassert(rst);
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001315 }
1316
Amelie Delaunayd7a9e422020-01-29 16:36:25 +01001317 dma_set_max_seg_size(&pdev->dev, STM32_DMA_ALIGNED_MAX_DATA_ITEMS);
1318
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001319 dma_cap_set(DMA_SLAVE, dd->cap_mask);
1320 dma_cap_set(DMA_PRIVATE, dd->cap_mask);
1321 dma_cap_set(DMA_CYCLIC, dd->cap_mask);
1322 dd->device_alloc_chan_resources = stm32_dma_alloc_chan_resources;
1323 dd->device_free_chan_resources = stm32_dma_free_chan_resources;
1324 dd->device_tx_status = stm32_dma_tx_status;
1325 dd->device_issue_pending = stm32_dma_issue_pending;
1326 dd->device_prep_slave_sg = stm32_dma_prep_slave_sg;
1327 dd->device_prep_dma_cyclic = stm32_dma_prep_dma_cyclic;
1328 dd->device_config = stm32_dma_slave_config;
1329 dd->device_terminate_all = stm32_dma_terminate_all;
M'boumba Cedric Madiangadc808672016-12-13 14:40:50 +01001330 dd->device_synchronize = stm32_dma_synchronize;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001331 dd->src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1332 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1333 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
1334 dd->dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1335 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1336 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
1337 dd->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
1338 dd->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
Amelie Delaunay32ce1082020-01-29 16:36:26 +01001339 dd->copy_align = DMAENGINE_ALIGN_32_BYTES;
M'boumba Cedric Madianga276b0042016-12-13 14:40:51 +01001340 dd->max_burst = STM32_DMA_MAX_BURST;
Pierre-Yves MORDRET22a0bb22020-01-29 16:36:24 +01001341 dd->descriptor_reuse = true;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001342 dd->dev = &pdev->dev;
1343 INIT_LIST_HEAD(&dd->channels);
1344
1345 if (dmadev->mem2mem) {
1346 dma_cap_set(DMA_MEMCPY, dd->cap_mask);
1347 dd->device_prep_dma_memcpy = stm32_dma_prep_dma_memcpy;
1348 dd->directions |= BIT(DMA_MEM_TO_MEM);
1349 }
1350
1351 for (i = 0; i < STM32_DMA_MAX_CHANNELS; i++) {
1352 chan = &dmadev->chan[i];
1353 chan->id = i;
1354 chan->vchan.desc_free = stm32_dma_desc_free;
1355 vchan_init(&chan->vchan, dd);
1356 }
1357
1358 ret = dma_async_device_register(dd);
1359 if (ret)
Pierre-Yves MORDRET48bc73b2019-01-03 11:17:08 +01001360 goto clk_free;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001361
1362 for (i = 0; i < STM32_DMA_MAX_CHANNELS; i++) {
1363 chan = &dmadev->chan[i];
Vinod Koulc6504be2019-04-26 22:30:27 +05301364 ret = platform_get_irq(pdev, i);
Stephen Boyde17be6e2019-07-30 11:15:10 -07001365 if (ret < 0)
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001366 goto err_unregister;
Vinod Koulc6504be2019-04-26 22:30:27 +05301367 chan->irq = ret;
1368
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001369 ret = devm_request_irq(&pdev->dev, chan->irq,
1370 stm32_dma_chan_irq, 0,
1371 dev_name(chan2dev(chan)), chan);
1372 if (ret) {
1373 dev_err(&pdev->dev,
1374 "request_irq failed with err %d channel %d\n",
1375 ret, i);
1376 goto err_unregister;
1377 }
1378 }
1379
1380 ret = of_dma_controller_register(pdev->dev.of_node,
1381 stm32_dma_of_xlate, dmadev);
1382 if (ret < 0) {
1383 dev_err(&pdev->dev,
1384 "STM32 DMA DMA OF registration failed %d\n", ret);
1385 goto err_unregister;
1386 }
1387
1388 platform_set_drvdata(pdev, dmadev);
1389
Pierre-Yves MORDRET48bc73b2019-01-03 11:17:08 +01001390 pm_runtime_set_active(&pdev->dev);
1391 pm_runtime_enable(&pdev->dev);
1392 pm_runtime_get_noresume(&pdev->dev);
1393 pm_runtime_put(&pdev->dev);
1394
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001395 dev_info(&pdev->dev, "STM32 DMA driver registered\n");
1396
1397 return 0;
1398
1399err_unregister:
1400 dma_async_device_unregister(dd);
Pierre-Yves MORDRET48bc73b2019-01-03 11:17:08 +01001401clk_free:
1402 clk_disable_unprepare(dmadev->clk);
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001403
1404 return ret;
1405}
1406
Pierre-Yves MORDRET48bc73b2019-01-03 11:17:08 +01001407#ifdef CONFIG_PM
1408static int stm32_dma_runtime_suspend(struct device *dev)
1409{
1410 struct stm32_dma_device *dmadev = dev_get_drvdata(dev);
1411
1412 clk_disable_unprepare(dmadev->clk);
1413
1414 return 0;
1415}
1416
1417static int stm32_dma_runtime_resume(struct device *dev)
1418{
1419 struct stm32_dma_device *dmadev = dev_get_drvdata(dev);
1420 int ret;
1421
1422 ret = clk_prepare_enable(dmadev->clk);
1423 if (ret) {
1424 dev_err(dev, "failed to prepare_enable clock\n");
1425 return ret;
1426 }
1427
1428 return 0;
1429}
1430#endif
1431
Pierre-Yves MORDRET05f87402020-01-29 16:36:21 +01001432#ifdef CONFIG_PM_SLEEP
1433static int stm32_dma_suspend(struct device *dev)
1434{
1435 struct stm32_dma_device *dmadev = dev_get_drvdata(dev);
1436 int id, ret, scr;
1437
1438 ret = pm_runtime_get_sync(dev);
1439 if (ret < 0)
1440 return ret;
1441
1442 for (id = 0; id < STM32_DMA_MAX_CHANNELS; id++) {
1443 scr = stm32_dma_read(dmadev, STM32_DMA_SCR(id));
1444 if (scr & STM32_DMA_SCR_EN) {
1445 dev_warn(dev, "Suspend is prevented by Chan %i\n", id);
1446 return -EBUSY;
1447 }
1448 }
1449
1450 pm_runtime_put_sync(dev);
1451
1452 pm_runtime_force_suspend(dev);
1453
1454 return 0;
1455}
1456
1457static int stm32_dma_resume(struct device *dev)
1458{
1459 return pm_runtime_force_resume(dev);
1460}
1461#endif
1462
Pierre-Yves MORDRET48bc73b2019-01-03 11:17:08 +01001463static const struct dev_pm_ops stm32_dma_pm_ops = {
Pierre-Yves MORDRET05f87402020-01-29 16:36:21 +01001464 SET_SYSTEM_SLEEP_PM_OPS(stm32_dma_suspend, stm32_dma_resume)
Pierre-Yves MORDRET48bc73b2019-01-03 11:17:08 +01001465 SET_RUNTIME_PM_OPS(stm32_dma_runtime_suspend,
1466 stm32_dma_runtime_resume, NULL)
1467};
1468
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001469static struct platform_driver stm32_dma_driver = {
1470 .driver = {
1471 .name = "stm32-dma",
1472 .of_match_table = stm32_dma_of_match,
Pierre-Yves MORDRET48bc73b2019-01-03 11:17:08 +01001473 .pm = &stm32_dma_pm_ops,
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001474 },
Etienne Carriere615eee22020-01-29 16:36:23 +01001475 .probe = stm32_dma_probe,
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001476};
1477
1478static int __init stm32_dma_init(void)
1479{
Etienne Carriere615eee22020-01-29 16:36:23 +01001480 return platform_driver_register(&stm32_dma_driver);
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001481}
1482subsys_initcall(stm32_dma_init);