Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 *
4 * Copyright (C) STMicroelectronics SA 2017
5 * Author(s): M'boumba Cedric Madianga <cedric.madianga@gmail.com>
6 * Pierre-Yves Mordret <pierre-yves.mordret@st.com>
7 *
8 * Driver for STM32 MDMA controller
9 *
10 * Inspired by stm32-dma.c and dma-jz4780.c
11 */
12
13#include <linux/clk.h>
14#include <linux/delay.h>
15#include <linux/dmaengine.h>
16#include <linux/dma-mapping.h>
17#include <linux/dmapool.h>
18#include <linux/err.h>
19#include <linux/init.h>
20#include <linux/iopoll.h>
21#include <linux/jiffies.h>
22#include <linux/list.h>
23#include <linux/log2.h>
24#include <linux/module.h>
25#include <linux/of.h>
26#include <linux/of_device.h>
27#include <linux/of_dma.h>
28#include <linux/platform_device.h>
29#include <linux/pm_runtime.h>
30#include <linux/reset.h>
31#include <linux/slab.h>
32
33#include "virt-dma.h"
34
35/* MDMA Generic getter/setter */
36#define STM32_MDMA_SHIFT(n) (ffs(n) - 1)
37#define STM32_MDMA_SET(n, mask) (((n) << STM32_MDMA_SHIFT(mask)) & \
38 (mask))
39#define STM32_MDMA_GET(n, mask) (((n) & (mask)) >> \
40 STM32_MDMA_SHIFT(mask))
41
42#define STM32_MDMA_GISR0 0x0000 /* MDMA Int Status Reg 1 */
43#define STM32_MDMA_GISR1 0x0004 /* MDMA Int Status Reg 2 */
44
45/* MDMA Channel x interrupt/status register */
46#define STM32_MDMA_CISR(x) (0x40 + 0x40 * (x)) /* x = 0..62 */
47#define STM32_MDMA_CISR_CRQA BIT(16)
48#define STM32_MDMA_CISR_TCIF BIT(4)
49#define STM32_MDMA_CISR_BTIF BIT(3)
50#define STM32_MDMA_CISR_BRTIF BIT(2)
51#define STM32_MDMA_CISR_CTCIF BIT(1)
52#define STM32_MDMA_CISR_TEIF BIT(0)
53
54/* MDMA Channel x interrupt flag clear register */
55#define STM32_MDMA_CIFCR(x) (0x44 + 0x40 * (x))
56#define STM32_MDMA_CIFCR_CLTCIF BIT(4)
57#define STM32_MDMA_CIFCR_CBTIF BIT(3)
58#define STM32_MDMA_CIFCR_CBRTIF BIT(2)
59#define STM32_MDMA_CIFCR_CCTCIF BIT(1)
60#define STM32_MDMA_CIFCR_CTEIF BIT(0)
61#define STM32_MDMA_CIFCR_CLEAR_ALL (STM32_MDMA_CIFCR_CLTCIF \
62 | STM32_MDMA_CIFCR_CBTIF \
63 | STM32_MDMA_CIFCR_CBRTIF \
64 | STM32_MDMA_CIFCR_CCTCIF \
65 | STM32_MDMA_CIFCR_CTEIF)
66
67/* MDMA Channel x error status register */
68#define STM32_MDMA_CESR(x) (0x48 + 0x40 * (x))
69#define STM32_MDMA_CESR_BSE BIT(11)
70#define STM32_MDMA_CESR_ASR BIT(10)
71#define STM32_MDMA_CESR_TEMD BIT(9)
72#define STM32_MDMA_CESR_TELD BIT(8)
73#define STM32_MDMA_CESR_TED BIT(7)
74#define STM32_MDMA_CESR_TEA_MASK GENMASK(6, 0)
75
76/* MDMA Channel x control register */
77#define STM32_MDMA_CCR(x) (0x4C + 0x40 * (x))
78#define STM32_MDMA_CCR_SWRQ BIT(16)
79#define STM32_MDMA_CCR_WEX BIT(14)
80#define STM32_MDMA_CCR_HEX BIT(13)
81#define STM32_MDMA_CCR_BEX BIT(12)
82#define STM32_MDMA_CCR_PL_MASK GENMASK(7, 6)
83#define STM32_MDMA_CCR_PL(n) STM32_MDMA_SET(n, \
84 STM32_MDMA_CCR_PL_MASK)
85#define STM32_MDMA_CCR_TCIE BIT(5)
86#define STM32_MDMA_CCR_BTIE BIT(4)
87#define STM32_MDMA_CCR_BRTIE BIT(3)
88#define STM32_MDMA_CCR_CTCIE BIT(2)
89#define STM32_MDMA_CCR_TEIE BIT(1)
90#define STM32_MDMA_CCR_EN BIT(0)
91#define STM32_MDMA_CCR_IRQ_MASK (STM32_MDMA_CCR_TCIE \
92 | STM32_MDMA_CCR_BTIE \
93 | STM32_MDMA_CCR_BRTIE \
94 | STM32_MDMA_CCR_CTCIE \
95 | STM32_MDMA_CCR_TEIE)
96
97/* MDMA Channel x transfer configuration register */
98#define STM32_MDMA_CTCR(x) (0x50 + 0x40 * (x))
99#define STM32_MDMA_CTCR_BWM BIT(31)
100#define STM32_MDMA_CTCR_SWRM BIT(30)
101#define STM32_MDMA_CTCR_TRGM_MSK GENMASK(29, 28)
102#define STM32_MDMA_CTCR_TRGM(n) STM32_MDMA_SET((n), \
103 STM32_MDMA_CTCR_TRGM_MSK)
104#define STM32_MDMA_CTCR_TRGM_GET(n) STM32_MDMA_GET((n), \
105 STM32_MDMA_CTCR_TRGM_MSK)
106#define STM32_MDMA_CTCR_PAM_MASK GENMASK(27, 26)
107#define STM32_MDMA_CTCR_PAM(n) STM32_MDMA_SET(n, \
108 STM32_MDMA_CTCR_PAM_MASK)
109#define STM32_MDMA_CTCR_PKE BIT(25)
110#define STM32_MDMA_CTCR_TLEN_MSK GENMASK(24, 18)
111#define STM32_MDMA_CTCR_TLEN(n) STM32_MDMA_SET((n), \
112 STM32_MDMA_CTCR_TLEN_MSK)
113#define STM32_MDMA_CTCR_TLEN_GET(n) STM32_MDMA_GET((n), \
114 STM32_MDMA_CTCR_TLEN_MSK)
115#define STM32_MDMA_CTCR_LEN2_MSK GENMASK(25, 18)
116#define STM32_MDMA_CTCR_LEN2(n) STM32_MDMA_SET((n), \
117 STM32_MDMA_CTCR_LEN2_MSK)
118#define STM32_MDMA_CTCR_LEN2_GET(n) STM32_MDMA_GET((n), \
119 STM32_MDMA_CTCR_LEN2_MSK)
120#define STM32_MDMA_CTCR_DBURST_MASK GENMASK(17, 15)
121#define STM32_MDMA_CTCR_DBURST(n) STM32_MDMA_SET(n, \
122 STM32_MDMA_CTCR_DBURST_MASK)
123#define STM32_MDMA_CTCR_SBURST_MASK GENMASK(14, 12)
124#define STM32_MDMA_CTCR_SBURST(n) STM32_MDMA_SET(n, \
125 STM32_MDMA_CTCR_SBURST_MASK)
126#define STM32_MDMA_CTCR_DINCOS_MASK GENMASK(11, 10)
127#define STM32_MDMA_CTCR_DINCOS(n) STM32_MDMA_SET((n), \
128 STM32_MDMA_CTCR_DINCOS_MASK)
129#define STM32_MDMA_CTCR_SINCOS_MASK GENMASK(9, 8)
130#define STM32_MDMA_CTCR_SINCOS(n) STM32_MDMA_SET((n), \
131 STM32_MDMA_CTCR_SINCOS_MASK)
132#define STM32_MDMA_CTCR_DSIZE_MASK GENMASK(7, 6)
133#define STM32_MDMA_CTCR_DSIZE(n) STM32_MDMA_SET(n, \
134 STM32_MDMA_CTCR_DSIZE_MASK)
135#define STM32_MDMA_CTCR_SSIZE_MASK GENMASK(5, 4)
136#define STM32_MDMA_CTCR_SSIZE(n) STM32_MDMA_SET(n, \
137 STM32_MDMA_CTCR_SSIZE_MASK)
138#define STM32_MDMA_CTCR_DINC_MASK GENMASK(3, 2)
139#define STM32_MDMA_CTCR_DINC(n) STM32_MDMA_SET((n), \
140 STM32_MDMA_CTCR_DINC_MASK)
141#define STM32_MDMA_CTCR_SINC_MASK GENMASK(1, 0)
142#define STM32_MDMA_CTCR_SINC(n) STM32_MDMA_SET((n), \
143 STM32_MDMA_CTCR_SINC_MASK)
144#define STM32_MDMA_CTCR_CFG_MASK (STM32_MDMA_CTCR_SINC_MASK \
145 | STM32_MDMA_CTCR_DINC_MASK \
146 | STM32_MDMA_CTCR_SINCOS_MASK \
147 | STM32_MDMA_CTCR_DINCOS_MASK \
148 | STM32_MDMA_CTCR_LEN2_MSK \
149 | STM32_MDMA_CTCR_TRGM_MSK)
150
151/* MDMA Channel x block number of data register */
152#define STM32_MDMA_CBNDTR(x) (0x54 + 0x40 * (x))
153#define STM32_MDMA_CBNDTR_BRC_MK GENMASK(31, 20)
154#define STM32_MDMA_CBNDTR_BRC(n) STM32_MDMA_SET(n, \
155 STM32_MDMA_CBNDTR_BRC_MK)
156#define STM32_MDMA_CBNDTR_BRC_GET(n) STM32_MDMA_GET((n), \
157 STM32_MDMA_CBNDTR_BRC_MK)
158
159#define STM32_MDMA_CBNDTR_BRDUM BIT(19)
160#define STM32_MDMA_CBNDTR_BRSUM BIT(18)
161#define STM32_MDMA_CBNDTR_BNDT_MASK GENMASK(16, 0)
162#define STM32_MDMA_CBNDTR_BNDT(n) STM32_MDMA_SET(n, \
163 STM32_MDMA_CBNDTR_BNDT_MASK)
164
165/* MDMA Channel x source address register */
166#define STM32_MDMA_CSAR(x) (0x58 + 0x40 * (x))
167
168/* MDMA Channel x destination address register */
169#define STM32_MDMA_CDAR(x) (0x5C + 0x40 * (x))
170
171/* MDMA Channel x block repeat address update register */
172#define STM32_MDMA_CBRUR(x) (0x60 + 0x40 * (x))
173#define STM32_MDMA_CBRUR_DUV_MASK GENMASK(31, 16)
174#define STM32_MDMA_CBRUR_DUV(n) STM32_MDMA_SET(n, \
175 STM32_MDMA_CBRUR_DUV_MASK)
176#define STM32_MDMA_CBRUR_SUV_MASK GENMASK(15, 0)
177#define STM32_MDMA_CBRUR_SUV(n) STM32_MDMA_SET(n, \
178 STM32_MDMA_CBRUR_SUV_MASK)
179
180/* MDMA Channel x link address register */
181#define STM32_MDMA_CLAR(x) (0x64 + 0x40 * (x))
182
183/* MDMA Channel x trigger and bus selection register */
184#define STM32_MDMA_CTBR(x) (0x68 + 0x40 * (x))
185#define STM32_MDMA_CTBR_DBUS BIT(17)
186#define STM32_MDMA_CTBR_SBUS BIT(16)
187#define STM32_MDMA_CTBR_TSEL_MASK GENMASK(7, 0)
188#define STM32_MDMA_CTBR_TSEL(n) STM32_MDMA_SET(n, \
189 STM32_MDMA_CTBR_TSEL_MASK)
190
191/* MDMA Channel x mask address register */
192#define STM32_MDMA_CMAR(x) (0x70 + 0x40 * (x))
193
194/* MDMA Channel x mask data register */
195#define STM32_MDMA_CMDR(x) (0x74 + 0x40 * (x))
196
197#define STM32_MDMA_MAX_BUF_LEN 128
198#define STM32_MDMA_MAX_BLOCK_LEN 65536
199#define STM32_MDMA_MAX_CHANNELS 63
200#define STM32_MDMA_MAX_REQUESTS 256
201#define STM32_MDMA_MAX_BURST 128
202#define STM32_MDMA_VERY_HIGH_PRIORITY 0x11
203
204enum stm32_mdma_trigger_mode {
205 STM32_MDMA_BUFFER,
206 STM32_MDMA_BLOCK,
207 STM32_MDMA_BLOCK_REP,
208 STM32_MDMA_LINKED_LIST,
209};
210
211enum stm32_mdma_width {
212 STM32_MDMA_BYTE,
213 STM32_MDMA_HALF_WORD,
214 STM32_MDMA_WORD,
215 STM32_MDMA_DOUBLE_WORD,
216};
217
218enum stm32_mdma_inc_mode {
219 STM32_MDMA_FIXED = 0,
220 STM32_MDMA_INC = 2,
221 STM32_MDMA_DEC = 3,
222};
223
224struct stm32_mdma_chan_config {
225 u32 request;
226 u32 priority_level;
227 u32 transfer_config;
228 u32 mask_addr;
229 u32 mask_data;
230};
231
232struct stm32_mdma_hwdesc {
233 u32 ctcr;
234 u32 cbndtr;
235 u32 csar;
236 u32 cdar;
237 u32 cbrur;
238 u32 clar;
239 u32 ctbr;
240 u32 dummy;
241 u32 cmar;
242 u32 cmdr;
243} __aligned(64);
244
245struct stm32_mdma_desc_node {
246 struct stm32_mdma_hwdesc *hwdesc;
247 dma_addr_t hwdesc_phys;
248};
249
250struct stm32_mdma_desc {
251 struct virt_dma_desc vdesc;
252 u32 ccr;
253 bool cyclic;
254 u32 count;
255 struct stm32_mdma_desc_node node[];
256};
257
258struct stm32_mdma_chan {
259 struct virt_dma_chan vchan;
260 struct dma_pool *desc_pool;
261 u32 id;
262 struct stm32_mdma_desc *desc;
263 u32 curr_hwdesc;
264 struct dma_slave_config dma_config;
265 struct stm32_mdma_chan_config chan_config;
266 bool busy;
267 u32 mem_burst;
268 u32 mem_width;
269};
270
271struct stm32_mdma_device {
272 struct dma_device ddev;
273 void __iomem *base;
274 struct clk *clk;
275 int irq;
276 u32 nr_channels;
277 u32 nr_requests;
278 u32 nr_ahb_addr_masks;
279 struct stm32_mdma_chan chan[STM32_MDMA_MAX_CHANNELS];
280 u32 ahb_addr_masks[];
281};
282
283static struct stm32_mdma_device *stm32_mdma_get_dev(
284 struct stm32_mdma_chan *chan)
285{
286 return container_of(chan->vchan.chan.device, struct stm32_mdma_device,
287 ddev);
288}
289
290static struct stm32_mdma_chan *to_stm32_mdma_chan(struct dma_chan *c)
291{
292 return container_of(c, struct stm32_mdma_chan, vchan.chan);
293}
294
295static struct stm32_mdma_desc *to_stm32_mdma_desc(struct virt_dma_desc *vdesc)
296{
297 return container_of(vdesc, struct stm32_mdma_desc, vdesc);
298}
299
300static struct device *chan2dev(struct stm32_mdma_chan *chan)
301{
302 return &chan->vchan.chan.dev->device;
303}
304
305static struct device *mdma2dev(struct stm32_mdma_device *mdma_dev)
306{
307 return mdma_dev->ddev.dev;
308}
309
310static u32 stm32_mdma_read(struct stm32_mdma_device *dmadev, u32 reg)
311{
312 return readl_relaxed(dmadev->base + reg);
313}
314
315static void stm32_mdma_write(struct stm32_mdma_device *dmadev, u32 reg, u32 val)
316{
317 writel_relaxed(val, dmadev->base + reg);
318}
319
320static void stm32_mdma_set_bits(struct stm32_mdma_device *dmadev, u32 reg,
321 u32 mask)
322{
323 void __iomem *addr = dmadev->base + reg;
324
325 writel_relaxed(readl_relaxed(addr) | mask, addr);
326}
327
328static void stm32_mdma_clr_bits(struct stm32_mdma_device *dmadev, u32 reg,
329 u32 mask)
330{
331 void __iomem *addr = dmadev->base + reg;
332
333 writel_relaxed(readl_relaxed(addr) & ~mask, addr);
334}
335
336static struct stm32_mdma_desc *stm32_mdma_alloc_desc(
337 struct stm32_mdma_chan *chan, u32 count)
338{
339 struct stm32_mdma_desc *desc;
340 int i;
341
342 desc = kzalloc(offsetof(typeof(*desc), node[count]), GFP_NOWAIT);
343 if (!desc)
344 return NULL;
345
346 for (i = 0; i < count; i++) {
347 desc->node[i].hwdesc =
348 dma_pool_alloc(chan->desc_pool, GFP_NOWAIT,
349 &desc->node[i].hwdesc_phys);
350 if (!desc->node[i].hwdesc)
351 goto err;
352 }
353
354 desc->count = count;
355
356 return desc;
357
358err:
359 dev_err(chan2dev(chan), "Failed to allocate descriptor\n");
360 while (--i >= 0)
361 dma_pool_free(chan->desc_pool, desc->node[i].hwdesc,
362 desc->node[i].hwdesc_phys);
363 kfree(desc);
364 return NULL;
365}
366
367static void stm32_mdma_desc_free(struct virt_dma_desc *vdesc)
368{
369 struct stm32_mdma_desc *desc = to_stm32_mdma_desc(vdesc);
370 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(vdesc->tx.chan);
371 int i;
372
373 for (i = 0; i < desc->count; i++)
374 dma_pool_free(chan->desc_pool, desc->node[i].hwdesc,
375 desc->node[i].hwdesc_phys);
376 kfree(desc);
377}
378
379static int stm32_mdma_get_width(struct stm32_mdma_chan *chan,
380 enum dma_slave_buswidth width)
381{
382 switch (width) {
383 case DMA_SLAVE_BUSWIDTH_1_BYTE:
384 case DMA_SLAVE_BUSWIDTH_2_BYTES:
385 case DMA_SLAVE_BUSWIDTH_4_BYTES:
386 case DMA_SLAVE_BUSWIDTH_8_BYTES:
387 return ffs(width) - 1;
388 default:
389 dev_err(chan2dev(chan), "Dma bus width %i not supported\n",
390 width);
391 return -EINVAL;
392 }
393}
394
395static enum dma_slave_buswidth stm32_mdma_get_max_width(dma_addr_t addr,
396 u32 buf_len, u32 tlen)
397{
398 enum dma_slave_buswidth max_width = DMA_SLAVE_BUSWIDTH_8_BYTES;
399
400 for (max_width = DMA_SLAVE_BUSWIDTH_8_BYTES;
401 max_width > DMA_SLAVE_BUSWIDTH_1_BYTE;
402 max_width >>= 1) {
403 /*
404 * Address and buffer length both have to be aligned on
405 * bus width
406 */
407 if ((((buf_len | addr) & (max_width - 1)) == 0) &&
408 tlen >= max_width)
409 break;
410 }
411
412 return max_width;
413}
414
415static u32 stm32_mdma_get_best_burst(u32 buf_len, u32 tlen, u32 max_burst,
416 enum dma_slave_buswidth width)
417{
418 u32 best_burst;
419
420 best_burst = min((u32)1 << __ffs(tlen | buf_len),
421 max_burst * width) / width;
422
423 return (best_burst > 0) ? best_burst : 1;
424}
425
426static int stm32_mdma_disable_chan(struct stm32_mdma_chan *chan)
427{
428 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
429 u32 ccr, cisr, id, reg;
430 int ret;
431
432 id = chan->id;
433 reg = STM32_MDMA_CCR(id);
434
435 /* Disable interrupts */
436 stm32_mdma_clr_bits(dmadev, reg, STM32_MDMA_CCR_IRQ_MASK);
437
438 ccr = stm32_mdma_read(dmadev, reg);
439 if (ccr & STM32_MDMA_CCR_EN) {
440 stm32_mdma_clr_bits(dmadev, reg, STM32_MDMA_CCR_EN);
441
442 /* Ensure that any ongoing transfer has been completed */
443 ret = readl_relaxed_poll_timeout_atomic(
444 dmadev->base + STM32_MDMA_CISR(id), cisr,
445 (cisr & STM32_MDMA_CISR_CTCIF), 10, 1000);
446 if (ret) {
447 dev_err(chan2dev(chan), "%s: timeout!\n", __func__);
448 return -EBUSY;
449 }
450 }
451
452 return 0;
453}
454
455static void stm32_mdma_stop(struct stm32_mdma_chan *chan)
456{
457 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
458 u32 status;
459 int ret;
460
461 /* Disable DMA */
462 ret = stm32_mdma_disable_chan(chan);
463 if (ret < 0)
464 return;
465
466 /* Clear interrupt status if it is there */
467 status = stm32_mdma_read(dmadev, STM32_MDMA_CISR(chan->id));
468 if (status) {
469 dev_dbg(chan2dev(chan), "%s(): clearing interrupt: 0x%08x\n",
470 __func__, status);
471 stm32_mdma_set_bits(dmadev, STM32_MDMA_CIFCR(chan->id), status);
472 }
473
474 chan->busy = false;
475}
476
477static void stm32_mdma_set_bus(struct stm32_mdma_device *dmadev, u32 *ctbr,
478 u32 ctbr_mask, u32 src_addr)
479{
480 u32 mask;
481 int i;
482
483 /* Check if memory device is on AHB or AXI */
484 *ctbr &= ~ctbr_mask;
485 mask = src_addr & 0xF0000000;
486 for (i = 0; i < dmadev->nr_ahb_addr_masks; i++) {
487 if (mask == dmadev->ahb_addr_masks[i]) {
488 *ctbr |= ctbr_mask;
489 break;
490 }
491 }
492}
493
494static int stm32_mdma_set_xfer_param(struct stm32_mdma_chan *chan,
495 enum dma_transfer_direction direction,
496 u32 *mdma_ccr, u32 *mdma_ctcr,
497 u32 *mdma_ctbr, dma_addr_t addr,
498 u32 buf_len)
499{
500 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
501 struct stm32_mdma_chan_config *chan_config = &chan->chan_config;
502 enum dma_slave_buswidth src_addr_width, dst_addr_width;
503 phys_addr_t src_addr, dst_addr;
504 int src_bus_width, dst_bus_width;
505 u32 src_maxburst, dst_maxburst, src_best_burst, dst_best_burst;
506 u32 ccr, ctcr, ctbr, tlen;
507
508 src_addr_width = chan->dma_config.src_addr_width;
509 dst_addr_width = chan->dma_config.dst_addr_width;
510 src_maxburst = chan->dma_config.src_maxburst;
511 dst_maxburst = chan->dma_config.dst_maxburst;
512
513 ccr = stm32_mdma_read(dmadev, STM32_MDMA_CCR(chan->id));
514 ctcr = stm32_mdma_read(dmadev, STM32_MDMA_CTCR(chan->id));
515 ctbr = stm32_mdma_read(dmadev, STM32_MDMA_CTBR(chan->id));
516
517 /* Enable HW request mode */
518 ctcr &= ~STM32_MDMA_CTCR_SWRM;
519
520 /* Set DINC, SINC, DINCOS, SINCOS, TRGM and TLEN retrieve from DT */
521 ctcr &= ~STM32_MDMA_CTCR_CFG_MASK;
522 ctcr |= chan_config->transfer_config & STM32_MDMA_CTCR_CFG_MASK;
523
524 /*
525 * For buffer transfer length (TLEN) we have to set
526 * the number of bytes - 1 in CTCR register
527 */
528 tlen = STM32_MDMA_CTCR_LEN2_GET(ctcr);
529 ctcr &= ~STM32_MDMA_CTCR_LEN2_MSK;
530 ctcr |= STM32_MDMA_CTCR_TLEN((tlen - 1));
531
532 /* Disable Pack Enable */
533 ctcr &= ~STM32_MDMA_CTCR_PKE;
534
535 /* Check burst size constraints */
536 if (src_maxburst * src_addr_width > STM32_MDMA_MAX_BURST ||
537 dst_maxburst * dst_addr_width > STM32_MDMA_MAX_BURST) {
538 dev_err(chan2dev(chan),
539 "burst size * bus width higher than %d bytes\n",
540 STM32_MDMA_MAX_BURST);
541 return -EINVAL;
542 }
543
544 if ((!is_power_of_2(src_maxburst) && src_maxburst > 0) ||
545 (!is_power_of_2(dst_maxburst) && dst_maxburst > 0)) {
546 dev_err(chan2dev(chan), "burst size must be a power of 2\n");
547 return -EINVAL;
548 }
549
550 /*
551 * Configure channel control:
552 * - Clear SW request as in this case this is a HW one
553 * - Clear WEX, HEX and BEX bits
554 * - Set priority level
555 */
556 ccr &= ~(STM32_MDMA_CCR_SWRQ | STM32_MDMA_CCR_WEX | STM32_MDMA_CCR_HEX |
557 STM32_MDMA_CCR_BEX | STM32_MDMA_CCR_PL_MASK);
558 ccr |= STM32_MDMA_CCR_PL(chan_config->priority_level);
559
560 /* Configure Trigger selection */
561 ctbr &= ~STM32_MDMA_CTBR_TSEL_MASK;
562 ctbr |= STM32_MDMA_CTBR_TSEL(chan_config->request);
563
564 switch (direction) {
565 case DMA_MEM_TO_DEV:
566 dst_addr = chan->dma_config.dst_addr;
567
568 /* Set device data size */
569 dst_bus_width = stm32_mdma_get_width(chan, dst_addr_width);
570 if (dst_bus_width < 0)
571 return dst_bus_width;
572 ctcr &= ~STM32_MDMA_CTCR_DSIZE_MASK;
573 ctcr |= STM32_MDMA_CTCR_DSIZE(dst_bus_width);
574
575 /* Set device burst value */
576 dst_best_burst = stm32_mdma_get_best_burst(buf_len, tlen,
577 dst_maxburst,
578 dst_addr_width);
579 chan->mem_burst = dst_best_burst;
580 ctcr &= ~STM32_MDMA_CTCR_DBURST_MASK;
581 ctcr |= STM32_MDMA_CTCR_DBURST((ilog2(dst_best_burst)));
582
583 /* Set memory data size */
584 src_addr_width = stm32_mdma_get_max_width(addr, buf_len, tlen);
585 chan->mem_width = src_addr_width;
586 src_bus_width = stm32_mdma_get_width(chan, src_addr_width);
587 if (src_bus_width < 0)
588 return src_bus_width;
589 ctcr &= ~STM32_MDMA_CTCR_SSIZE_MASK |
590 STM32_MDMA_CTCR_SINCOS_MASK;
591 ctcr |= STM32_MDMA_CTCR_SSIZE(src_bus_width) |
592 STM32_MDMA_CTCR_SINCOS(src_bus_width);
593
594 /* Set memory burst value */
595 src_maxburst = STM32_MDMA_MAX_BUF_LEN / src_addr_width;
596 src_best_burst = stm32_mdma_get_best_burst(buf_len, tlen,
597 src_maxburst,
598 src_addr_width);
599 chan->mem_burst = src_best_burst;
600 ctcr &= ~STM32_MDMA_CTCR_SBURST_MASK;
601 ctcr |= STM32_MDMA_CTCR_SBURST((ilog2(src_best_burst)));
602
603 /* Select bus */
604 stm32_mdma_set_bus(dmadev, &ctbr, STM32_MDMA_CTBR_DBUS,
605 dst_addr);
606
607 if (dst_bus_width != src_bus_width)
608 ctcr |= STM32_MDMA_CTCR_PKE;
609
610 /* Set destination address */
611 stm32_mdma_write(dmadev, STM32_MDMA_CDAR(chan->id), dst_addr);
612 break;
613
614 case DMA_DEV_TO_MEM:
615 src_addr = chan->dma_config.src_addr;
616
617 /* Set device data size */
618 src_bus_width = stm32_mdma_get_width(chan, src_addr_width);
619 if (src_bus_width < 0)
620 return src_bus_width;
621 ctcr &= ~STM32_MDMA_CTCR_SSIZE_MASK;
622 ctcr |= STM32_MDMA_CTCR_SSIZE(src_bus_width);
623
624 /* Set device burst value */
625 src_best_burst = stm32_mdma_get_best_burst(buf_len, tlen,
626 src_maxburst,
627 src_addr_width);
628 ctcr &= ~STM32_MDMA_CTCR_SBURST_MASK;
629 ctcr |= STM32_MDMA_CTCR_SBURST((ilog2(src_best_burst)));
630
631 /* Set memory data size */
632 dst_addr_width = stm32_mdma_get_max_width(addr, buf_len, tlen);
633 chan->mem_width = dst_addr_width;
634 dst_bus_width = stm32_mdma_get_width(chan, dst_addr_width);
635 if (dst_bus_width < 0)
636 return dst_bus_width;
637 ctcr &= ~(STM32_MDMA_CTCR_DSIZE_MASK |
638 STM32_MDMA_CTCR_DINCOS_MASK);
639 ctcr |= STM32_MDMA_CTCR_DSIZE(dst_bus_width) |
640 STM32_MDMA_CTCR_DINCOS(dst_bus_width);
641
642 /* Set memory burst value */
643 dst_maxburst = STM32_MDMA_MAX_BUF_LEN / dst_addr_width;
644 dst_best_burst = stm32_mdma_get_best_burst(buf_len, tlen,
645 dst_maxburst,
646 dst_addr_width);
647 ctcr &= ~STM32_MDMA_CTCR_DBURST_MASK;
648 ctcr |= STM32_MDMA_CTCR_DBURST((ilog2(dst_best_burst)));
649
650 /* Select bus */
651 stm32_mdma_set_bus(dmadev, &ctbr, STM32_MDMA_CTBR_SBUS,
652 src_addr);
653
654 if (dst_bus_width != src_bus_width)
655 ctcr |= STM32_MDMA_CTCR_PKE;
656
657 /* Set source address */
658 stm32_mdma_write(dmadev, STM32_MDMA_CSAR(chan->id), src_addr);
659 break;
660
661 default:
662 dev_err(chan2dev(chan), "Dma direction is not supported\n");
663 return -EINVAL;
664 }
665
666 *mdma_ccr = ccr;
667 *mdma_ctcr = ctcr;
668 *mdma_ctbr = ctbr;
669
670 return 0;
671}
672
673static void stm32_mdma_dump_hwdesc(struct stm32_mdma_chan *chan,
674 struct stm32_mdma_desc_node *node)
675{
676 dev_dbg(chan2dev(chan), "hwdesc: %pad\n", &node->hwdesc_phys);
677 dev_dbg(chan2dev(chan), "CTCR: 0x%08x\n", node->hwdesc->ctcr);
678 dev_dbg(chan2dev(chan), "CBNDTR: 0x%08x\n", node->hwdesc->cbndtr);
679 dev_dbg(chan2dev(chan), "CSAR: 0x%08x\n", node->hwdesc->csar);
680 dev_dbg(chan2dev(chan), "CDAR: 0x%08x\n", node->hwdesc->cdar);
681 dev_dbg(chan2dev(chan), "CBRUR: 0x%08x\n", node->hwdesc->cbrur);
682 dev_dbg(chan2dev(chan), "CLAR: 0x%08x\n", node->hwdesc->clar);
683 dev_dbg(chan2dev(chan), "CTBR: 0x%08x\n", node->hwdesc->ctbr);
684 dev_dbg(chan2dev(chan), "CMAR: 0x%08x\n", node->hwdesc->cmar);
685 dev_dbg(chan2dev(chan), "CMDR: 0x%08x\n\n", node->hwdesc->cmdr);
686}
687
688static void stm32_mdma_setup_hwdesc(struct stm32_mdma_chan *chan,
689 struct stm32_mdma_desc *desc,
690 enum dma_transfer_direction dir, u32 count,
691 dma_addr_t src_addr, dma_addr_t dst_addr,
692 u32 len, u32 ctcr, u32 ctbr, bool is_last,
693 bool is_first, bool is_cyclic)
694{
695 struct stm32_mdma_chan_config *config = &chan->chan_config;
696 struct stm32_mdma_hwdesc *hwdesc;
697 u32 next = count + 1;
698
699 hwdesc = desc->node[count].hwdesc;
700 hwdesc->ctcr = ctcr;
701 hwdesc->cbndtr &= ~(STM32_MDMA_CBNDTR_BRC_MK |
702 STM32_MDMA_CBNDTR_BRDUM |
703 STM32_MDMA_CBNDTR_BRSUM |
704 STM32_MDMA_CBNDTR_BNDT_MASK);
705 hwdesc->cbndtr |= STM32_MDMA_CBNDTR_BNDT(len);
706 hwdesc->csar = src_addr;
707 hwdesc->cdar = dst_addr;
708 hwdesc->cbrur = 0;
709 hwdesc->ctbr = ctbr;
710 hwdesc->cmar = config->mask_addr;
711 hwdesc->cmdr = config->mask_data;
712
713 if (is_last) {
714 if (is_cyclic)
715 hwdesc->clar = desc->node[0].hwdesc_phys;
716 else
717 hwdesc->clar = 0;
718 } else {
719 hwdesc->clar = desc->node[next].hwdesc_phys;
720 }
721
722 stm32_mdma_dump_hwdesc(chan, &desc->node[count]);
723}
724
725static int stm32_mdma_setup_xfer(struct stm32_mdma_chan *chan,
726 struct stm32_mdma_desc *desc,
727 struct scatterlist *sgl, u32 sg_len,
728 enum dma_transfer_direction direction)
729{
730 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
731 struct dma_slave_config *dma_config = &chan->dma_config;
732 struct scatterlist *sg;
733 dma_addr_t src_addr, dst_addr;
734 u32 ccr, ctcr, ctbr;
735 int i, ret = 0;
736
737 for_each_sg(sgl, sg, sg_len, i) {
738 if (sg_dma_len(sg) > STM32_MDMA_MAX_BLOCK_LEN) {
739 dev_err(chan2dev(chan), "Invalid block len\n");
740 return -EINVAL;
741 }
742
743 if (direction == DMA_MEM_TO_DEV) {
744 src_addr = sg_dma_address(sg);
745 dst_addr = dma_config->dst_addr;
746 ret = stm32_mdma_set_xfer_param(chan, direction, &ccr,
747 &ctcr, &ctbr, src_addr,
748 sg_dma_len(sg));
749 stm32_mdma_set_bus(dmadev, &ctbr, STM32_MDMA_CTBR_SBUS,
750 src_addr);
751 } else {
752 src_addr = dma_config->src_addr;
753 dst_addr = sg_dma_address(sg);
754 ret = stm32_mdma_set_xfer_param(chan, direction, &ccr,
755 &ctcr, &ctbr, dst_addr,
756 sg_dma_len(sg));
757 stm32_mdma_set_bus(dmadev, &ctbr, STM32_MDMA_CTBR_DBUS,
758 dst_addr);
759 }
760
761 if (ret < 0)
762 return ret;
763
764 stm32_mdma_setup_hwdesc(chan, desc, direction, i, src_addr,
765 dst_addr, sg_dma_len(sg), ctcr, ctbr,
766 i == sg_len - 1, i == 0, false);
767 }
768
769 /* Enable interrupts */
770 ccr &= ~STM32_MDMA_CCR_IRQ_MASK;
771 ccr |= STM32_MDMA_CCR_TEIE | STM32_MDMA_CCR_CTCIE;
772 if (sg_len > 1)
773 ccr |= STM32_MDMA_CCR_BTIE;
774 desc->ccr = ccr;
775
776 return 0;
777}
778
779static struct dma_async_tx_descriptor *
780stm32_mdma_prep_slave_sg(struct dma_chan *c, struct scatterlist *sgl,
781 u32 sg_len, enum dma_transfer_direction direction,
782 unsigned long flags, void *context)
783{
784 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
785 struct stm32_mdma_desc *desc;
786 int i, ret;
787
788 /*
789 * Once DMA is in setup cyclic mode the channel we cannot assign this
790 * channel anymore. The DMA channel needs to be aborted or terminated
791 * for allowing another request.
792 */
793 if (chan->desc && chan->desc->cyclic) {
794 dev_err(chan2dev(chan),
795 "Request not allowed when dma in cyclic mode\n");
796 return NULL;
797 }
798
799 desc = stm32_mdma_alloc_desc(chan, sg_len);
800 if (!desc)
801 return NULL;
802
803 ret = stm32_mdma_setup_xfer(chan, desc, sgl, sg_len, direction);
804 if (ret < 0)
805 goto xfer_setup_err;
806
807 desc->cyclic = false;
808
809 return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
810
811xfer_setup_err:
812 for (i = 0; i < desc->count; i++)
813 dma_pool_free(chan->desc_pool, desc->node[i].hwdesc,
814 desc->node[i].hwdesc_phys);
815 kfree(desc);
816 return NULL;
817}
818
819static struct dma_async_tx_descriptor *
820stm32_mdma_prep_dma_cyclic(struct dma_chan *c, dma_addr_t buf_addr,
821 size_t buf_len, size_t period_len,
822 enum dma_transfer_direction direction,
823 unsigned long flags)
824{
825 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
826 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
827 struct dma_slave_config *dma_config = &chan->dma_config;
828 struct stm32_mdma_desc *desc;
829 dma_addr_t src_addr, dst_addr;
830 u32 ccr, ctcr, ctbr, count;
831 int i, ret;
832
833 /*
834 * Once DMA is in setup cyclic mode the channel we cannot assign this
835 * channel anymore. The DMA channel needs to be aborted or terminated
836 * for allowing another request.
837 */
838 if (chan->desc && chan->desc->cyclic) {
839 dev_err(chan2dev(chan),
840 "Request not allowed when dma in cyclic mode\n");
841 return NULL;
842 }
843
844 if (!buf_len || !period_len || period_len > STM32_MDMA_MAX_BLOCK_LEN) {
845 dev_err(chan2dev(chan), "Invalid buffer/period len\n");
846 return NULL;
847 }
848
849 if (buf_len % period_len) {
850 dev_err(chan2dev(chan), "buf_len not multiple of period_len\n");
851 return NULL;
852 }
853
854 count = buf_len / period_len;
855
856 desc = stm32_mdma_alloc_desc(chan, count);
857 if (!desc)
858 return NULL;
859
860 /* Select bus */
861 if (direction == DMA_MEM_TO_DEV) {
862 src_addr = buf_addr;
863 ret = stm32_mdma_set_xfer_param(chan, direction, &ccr, &ctcr,
864 &ctbr, src_addr, period_len);
865 stm32_mdma_set_bus(dmadev, &ctbr, STM32_MDMA_CTBR_SBUS,
866 src_addr);
867 } else {
868 dst_addr = buf_addr;
869 ret = stm32_mdma_set_xfer_param(chan, direction, &ccr, &ctcr,
870 &ctbr, dst_addr, period_len);
871 stm32_mdma_set_bus(dmadev, &ctbr, STM32_MDMA_CTBR_DBUS,
872 dst_addr);
873 }
874
875 if (ret < 0)
876 goto xfer_setup_err;
877
878 /* Enable interrupts */
879 ccr &= ~STM32_MDMA_CCR_IRQ_MASK;
880 ccr |= STM32_MDMA_CCR_TEIE | STM32_MDMA_CCR_CTCIE | STM32_MDMA_CCR_BTIE;
881 desc->ccr = ccr;
882
883 /* Configure hwdesc list */
884 for (i = 0; i < count; i++) {
885 if (direction == DMA_MEM_TO_DEV) {
886 src_addr = buf_addr + i * period_len;
887 dst_addr = dma_config->dst_addr;
888 } else {
889 src_addr = dma_config->src_addr;
890 dst_addr = buf_addr + i * period_len;
891 }
892
893 stm32_mdma_setup_hwdesc(chan, desc, direction, i, src_addr,
894 dst_addr, period_len, ctcr, ctbr,
895 i == count - 1, i == 0, true);
896 }
897
898 desc->cyclic = true;
899
900 return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
901
902xfer_setup_err:
903 for (i = 0; i < desc->count; i++)
904 dma_pool_free(chan->desc_pool, desc->node[i].hwdesc,
905 desc->node[i].hwdesc_phys);
906 kfree(desc);
907 return NULL;
908}
909
910static struct dma_async_tx_descriptor *
911stm32_mdma_prep_dma_memcpy(struct dma_chan *c, dma_addr_t dest, dma_addr_t src,
912 size_t len, unsigned long flags)
913{
914 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
915 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
916 enum dma_slave_buswidth max_width;
917 struct stm32_mdma_desc *desc;
918 struct stm32_mdma_hwdesc *hwdesc;
919 u32 ccr, ctcr, ctbr, cbndtr, count, max_burst, mdma_burst;
920 u32 best_burst, tlen;
921 size_t xfer_count, offset;
922 int src_bus_width, dst_bus_width;
923 int i;
924
925 /*
926 * Once DMA is in setup cyclic mode the channel we cannot assign this
927 * channel anymore. The DMA channel needs to be aborted or terminated
928 * to allow another request
929 */
930 if (chan->desc && chan->desc->cyclic) {
931 dev_err(chan2dev(chan),
932 "Request not allowed when dma in cyclic mode\n");
933 return NULL;
934 }
935
936 count = DIV_ROUND_UP(len, STM32_MDMA_MAX_BLOCK_LEN);
937 desc = stm32_mdma_alloc_desc(chan, count);
938 if (!desc)
939 return NULL;
940
941 ccr = stm32_mdma_read(dmadev, STM32_MDMA_CCR(chan->id));
942 ctcr = stm32_mdma_read(dmadev, STM32_MDMA_CTCR(chan->id));
943 ctbr = stm32_mdma_read(dmadev, STM32_MDMA_CTBR(chan->id));
944 cbndtr = stm32_mdma_read(dmadev, STM32_MDMA_CBNDTR(chan->id));
945
946 /* Enable sw req, some interrupts and clear other bits */
947 ccr &= ~(STM32_MDMA_CCR_WEX | STM32_MDMA_CCR_HEX |
948 STM32_MDMA_CCR_BEX | STM32_MDMA_CCR_PL_MASK |
949 STM32_MDMA_CCR_IRQ_MASK);
950 ccr |= STM32_MDMA_CCR_TEIE;
951
952 /* Enable SW request mode, dest/src inc and clear other bits */
953 ctcr &= ~(STM32_MDMA_CTCR_BWM | STM32_MDMA_CTCR_TRGM_MSK |
954 STM32_MDMA_CTCR_PAM_MASK | STM32_MDMA_CTCR_PKE |
955 STM32_MDMA_CTCR_TLEN_MSK | STM32_MDMA_CTCR_DBURST_MASK |
956 STM32_MDMA_CTCR_SBURST_MASK | STM32_MDMA_CTCR_DINCOS_MASK |
957 STM32_MDMA_CTCR_SINCOS_MASK | STM32_MDMA_CTCR_DSIZE_MASK |
958 STM32_MDMA_CTCR_SSIZE_MASK | STM32_MDMA_CTCR_DINC_MASK |
959 STM32_MDMA_CTCR_SINC_MASK);
960 ctcr |= STM32_MDMA_CTCR_SWRM | STM32_MDMA_CTCR_SINC(STM32_MDMA_INC) |
961 STM32_MDMA_CTCR_DINC(STM32_MDMA_INC);
962
963 /* Reset HW request */
964 ctbr &= ~STM32_MDMA_CTBR_TSEL_MASK;
965
966 /* Select bus */
967 stm32_mdma_set_bus(dmadev, &ctbr, STM32_MDMA_CTBR_SBUS, src);
968 stm32_mdma_set_bus(dmadev, &ctbr, STM32_MDMA_CTBR_DBUS, dest);
969
970 /* Clear CBNDTR registers */
971 cbndtr &= ~(STM32_MDMA_CBNDTR_BRC_MK | STM32_MDMA_CBNDTR_BRDUM |
972 STM32_MDMA_CBNDTR_BRSUM | STM32_MDMA_CBNDTR_BNDT_MASK);
973
974 if (len <= STM32_MDMA_MAX_BLOCK_LEN) {
975 cbndtr |= STM32_MDMA_CBNDTR_BNDT(len);
976 if (len <= STM32_MDMA_MAX_BUF_LEN) {
977 /* Setup a buffer transfer */
978 ccr |= STM32_MDMA_CCR_TCIE | STM32_MDMA_CCR_CTCIE;
979 ctcr |= STM32_MDMA_CTCR_TRGM(STM32_MDMA_BUFFER);
980 } else {
981 /* Setup a block transfer */
982 ccr |= STM32_MDMA_CCR_BTIE | STM32_MDMA_CCR_CTCIE;
983 ctcr |= STM32_MDMA_CTCR_TRGM(STM32_MDMA_BLOCK);
984 }
985
986 tlen = STM32_MDMA_MAX_BUF_LEN;
987 ctcr |= STM32_MDMA_CTCR_TLEN((tlen - 1));
988
989 /* Set source best burst size */
990 max_width = stm32_mdma_get_max_width(src, len, tlen);
991 src_bus_width = stm32_mdma_get_width(chan, max_width);
992
993 max_burst = tlen / max_width;
994 best_burst = stm32_mdma_get_best_burst(len, tlen, max_burst,
995 max_width);
996 mdma_burst = ilog2(best_burst);
997
998 ctcr |= STM32_MDMA_CTCR_SBURST(mdma_burst) |
999 STM32_MDMA_CTCR_SSIZE(src_bus_width) |
1000 STM32_MDMA_CTCR_SINCOS(src_bus_width);
1001
1002 /* Set destination best burst size */
1003 max_width = stm32_mdma_get_max_width(dest, len, tlen);
1004 dst_bus_width = stm32_mdma_get_width(chan, max_width);
1005
1006 max_burst = tlen / max_width;
1007 best_burst = stm32_mdma_get_best_burst(len, tlen, max_burst,
1008 max_width);
1009 mdma_burst = ilog2(best_burst);
1010
1011 ctcr |= STM32_MDMA_CTCR_DBURST(mdma_burst) |
1012 STM32_MDMA_CTCR_DSIZE(dst_bus_width) |
1013 STM32_MDMA_CTCR_DINCOS(dst_bus_width);
1014
1015 if (dst_bus_width != src_bus_width)
1016 ctcr |= STM32_MDMA_CTCR_PKE;
1017
1018 /* Prepare hardware descriptor */
1019 hwdesc = desc->node[0].hwdesc;
1020 hwdesc->ctcr = ctcr;
1021 hwdesc->cbndtr = cbndtr;
1022 hwdesc->csar = src;
1023 hwdesc->cdar = dest;
1024 hwdesc->cbrur = 0;
1025 hwdesc->clar = 0;
1026 hwdesc->ctbr = ctbr;
1027 hwdesc->cmar = 0;
1028 hwdesc->cmdr = 0;
1029
1030 stm32_mdma_dump_hwdesc(chan, &desc->node[0]);
1031 } else {
1032 /* Setup a LLI transfer */
1033 ctcr |= STM32_MDMA_CTCR_TRGM(STM32_MDMA_LINKED_LIST) |
1034 STM32_MDMA_CTCR_TLEN((STM32_MDMA_MAX_BUF_LEN - 1));
1035 ccr |= STM32_MDMA_CCR_BTIE | STM32_MDMA_CCR_CTCIE;
1036 tlen = STM32_MDMA_MAX_BUF_LEN;
1037
1038 for (i = 0, offset = 0; offset < len;
1039 i++, offset += xfer_count) {
1040 xfer_count = min_t(size_t, len - offset,
1041 STM32_MDMA_MAX_BLOCK_LEN);
1042
1043 /* Set source best burst size */
1044 max_width = stm32_mdma_get_max_width(src, len, tlen);
1045 src_bus_width = stm32_mdma_get_width(chan, max_width);
1046
1047 max_burst = tlen / max_width;
1048 best_burst = stm32_mdma_get_best_burst(len, tlen,
1049 max_burst,
1050 max_width);
1051 mdma_burst = ilog2(best_burst);
1052
1053 ctcr |= STM32_MDMA_CTCR_SBURST(mdma_burst) |
1054 STM32_MDMA_CTCR_SSIZE(src_bus_width) |
1055 STM32_MDMA_CTCR_SINCOS(src_bus_width);
1056
1057 /* Set destination best burst size */
1058 max_width = stm32_mdma_get_max_width(dest, len, tlen);
1059 dst_bus_width = stm32_mdma_get_width(chan, max_width);
1060
1061 max_burst = tlen / max_width;
1062 best_burst = stm32_mdma_get_best_burst(len, tlen,
1063 max_burst,
1064 max_width);
1065 mdma_burst = ilog2(best_burst);
1066
1067 ctcr |= STM32_MDMA_CTCR_DBURST(mdma_burst) |
1068 STM32_MDMA_CTCR_DSIZE(dst_bus_width) |
1069 STM32_MDMA_CTCR_DINCOS(dst_bus_width);
1070
1071 if (dst_bus_width != src_bus_width)
1072 ctcr |= STM32_MDMA_CTCR_PKE;
1073
1074 /* Prepare hardware descriptor */
1075 stm32_mdma_setup_hwdesc(chan, desc, DMA_MEM_TO_MEM, i,
1076 src + offset, dest + offset,
1077 xfer_count, ctcr, ctbr,
1078 i == count - 1, i == 0, false);
1079 }
1080 }
1081
1082 desc->ccr = ccr;
1083
1084 desc->cyclic = false;
1085
1086 return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
1087}
1088
1089static void stm32_mdma_dump_reg(struct stm32_mdma_chan *chan)
1090{
1091 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
1092
1093 dev_dbg(chan2dev(chan), "CCR: 0x%08x\n",
1094 stm32_mdma_read(dmadev, STM32_MDMA_CCR(chan->id)));
1095 dev_dbg(chan2dev(chan), "CTCR: 0x%08x\n",
1096 stm32_mdma_read(dmadev, STM32_MDMA_CTCR(chan->id)));
1097 dev_dbg(chan2dev(chan), "CBNDTR: 0x%08x\n",
1098 stm32_mdma_read(dmadev, STM32_MDMA_CBNDTR(chan->id)));
1099 dev_dbg(chan2dev(chan), "CSAR: 0x%08x\n",
1100 stm32_mdma_read(dmadev, STM32_MDMA_CSAR(chan->id)));
1101 dev_dbg(chan2dev(chan), "CDAR: 0x%08x\n",
1102 stm32_mdma_read(dmadev, STM32_MDMA_CDAR(chan->id)));
1103 dev_dbg(chan2dev(chan), "CBRUR: 0x%08x\n",
1104 stm32_mdma_read(dmadev, STM32_MDMA_CBRUR(chan->id)));
1105 dev_dbg(chan2dev(chan), "CLAR: 0x%08x\n",
1106 stm32_mdma_read(dmadev, STM32_MDMA_CLAR(chan->id)));
1107 dev_dbg(chan2dev(chan), "CTBR: 0x%08x\n",
1108 stm32_mdma_read(dmadev, STM32_MDMA_CTBR(chan->id)));
1109 dev_dbg(chan2dev(chan), "CMAR: 0x%08x\n",
1110 stm32_mdma_read(dmadev, STM32_MDMA_CMAR(chan->id)));
1111 dev_dbg(chan2dev(chan), "CMDR: 0x%08x\n",
1112 stm32_mdma_read(dmadev, STM32_MDMA_CMDR(chan->id)));
1113}
1114
1115static void stm32_mdma_start_transfer(struct stm32_mdma_chan *chan)
1116{
1117 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
1118 struct virt_dma_desc *vdesc;
1119 struct stm32_mdma_hwdesc *hwdesc;
1120 u32 id = chan->id;
1121 u32 status, reg;
1122
1123 vdesc = vchan_next_desc(&chan->vchan);
1124 if (!vdesc) {
1125 chan->desc = NULL;
1126 return;
1127 }
1128
1129 list_del(&vdesc->node);
1130
1131 chan->desc = to_stm32_mdma_desc(vdesc);
1132 hwdesc = chan->desc->node[0].hwdesc;
1133 chan->curr_hwdesc = 0;
1134
1135 stm32_mdma_write(dmadev, STM32_MDMA_CCR(id), chan->desc->ccr);
1136 stm32_mdma_write(dmadev, STM32_MDMA_CTCR(id), hwdesc->ctcr);
1137 stm32_mdma_write(dmadev, STM32_MDMA_CBNDTR(id), hwdesc->cbndtr);
1138 stm32_mdma_write(dmadev, STM32_MDMA_CSAR(id), hwdesc->csar);
1139 stm32_mdma_write(dmadev, STM32_MDMA_CDAR(id), hwdesc->cdar);
1140 stm32_mdma_write(dmadev, STM32_MDMA_CBRUR(id), hwdesc->cbrur);
1141 stm32_mdma_write(dmadev, STM32_MDMA_CLAR(id), hwdesc->clar);
1142 stm32_mdma_write(dmadev, STM32_MDMA_CTBR(id), hwdesc->ctbr);
1143 stm32_mdma_write(dmadev, STM32_MDMA_CMAR(id), hwdesc->cmar);
1144 stm32_mdma_write(dmadev, STM32_MDMA_CMDR(id), hwdesc->cmdr);
1145
1146 /* Clear interrupt status if it is there */
1147 status = stm32_mdma_read(dmadev, STM32_MDMA_CISR(id));
1148 if (status)
1149 stm32_mdma_set_bits(dmadev, STM32_MDMA_CIFCR(id), status);
1150
1151 stm32_mdma_dump_reg(chan);
1152
1153 /* Start DMA */
1154 stm32_mdma_set_bits(dmadev, STM32_MDMA_CCR(id), STM32_MDMA_CCR_EN);
1155
1156 /* Set SW request in case of MEM2MEM transfer */
1157 if (hwdesc->ctcr & STM32_MDMA_CTCR_SWRM) {
1158 reg = STM32_MDMA_CCR(id);
1159 stm32_mdma_set_bits(dmadev, reg, STM32_MDMA_CCR_SWRQ);
1160 }
1161
1162 chan->busy = true;
1163
1164 dev_dbg(chan2dev(chan), "vchan %pK: started\n", &chan->vchan);
1165}
1166
1167static void stm32_mdma_issue_pending(struct dma_chan *c)
1168{
1169 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
1170 unsigned long flags;
1171
1172 spin_lock_irqsave(&chan->vchan.lock, flags);
1173
1174 if (!vchan_issue_pending(&chan->vchan))
1175 goto end;
1176
1177 dev_dbg(chan2dev(chan), "vchan %pK: issued\n", &chan->vchan);
1178
1179 if (!chan->desc && !chan->busy)
1180 stm32_mdma_start_transfer(chan);
1181
1182end:
1183 spin_unlock_irqrestore(&chan->vchan.lock, flags);
1184}
1185
1186static int stm32_mdma_pause(struct dma_chan *c)
1187{
1188 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
1189 unsigned long flags;
1190 int ret;
1191
1192 spin_lock_irqsave(&chan->vchan.lock, flags);
1193 ret = stm32_mdma_disable_chan(chan);
1194 spin_unlock_irqrestore(&chan->vchan.lock, flags);
1195
1196 if (!ret)
1197 dev_dbg(chan2dev(chan), "vchan %pK: pause\n", &chan->vchan);
1198
1199 return ret;
1200}
1201
1202static int stm32_mdma_resume(struct dma_chan *c)
1203{
1204 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
1205 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
1206 struct stm32_mdma_hwdesc *hwdesc;
1207 unsigned long flags;
1208 u32 status, reg;
1209
1210 hwdesc = chan->desc->node[chan->curr_hwdesc].hwdesc;
1211
1212 spin_lock_irqsave(&chan->vchan.lock, flags);
1213
1214 /* Re-configure control register */
1215 stm32_mdma_write(dmadev, STM32_MDMA_CCR(chan->id), chan->desc->ccr);
1216
1217 /* Clear interrupt status if it is there */
1218 status = stm32_mdma_read(dmadev, STM32_MDMA_CISR(chan->id));
1219 if (status)
1220 stm32_mdma_set_bits(dmadev, STM32_MDMA_CIFCR(chan->id), status);
1221
1222 stm32_mdma_dump_reg(chan);
1223
1224 /* Re-start DMA */
1225 reg = STM32_MDMA_CCR(chan->id);
1226 stm32_mdma_set_bits(dmadev, reg, STM32_MDMA_CCR_EN);
1227
1228 /* Set SW request in case of MEM2MEM transfer */
1229 if (hwdesc->ctcr & STM32_MDMA_CTCR_SWRM)
1230 stm32_mdma_set_bits(dmadev, reg, STM32_MDMA_CCR_SWRQ);
1231
1232 spin_unlock_irqrestore(&chan->vchan.lock, flags);
1233
1234 dev_dbg(chan2dev(chan), "vchan %pK: resume\n", &chan->vchan);
1235
1236 return 0;
1237}
1238
1239static int stm32_mdma_terminate_all(struct dma_chan *c)
1240{
1241 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
1242 unsigned long flags;
1243 LIST_HEAD(head);
1244
1245 spin_lock_irqsave(&chan->vchan.lock, flags);
1246 if (chan->desc) {
1247 vchan_terminate_vdesc(&chan->desc->vdesc);
1248 if (chan->busy)
1249 stm32_mdma_stop(chan);
1250 chan->desc = NULL;
1251 }
1252 vchan_get_all_descriptors(&chan->vchan, &head);
1253 spin_unlock_irqrestore(&chan->vchan.lock, flags);
1254
1255 vchan_dma_desc_free_list(&chan->vchan, &head);
1256
1257 return 0;
1258}
1259
1260static void stm32_mdma_synchronize(struct dma_chan *c)
1261{
1262 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
1263
1264 vchan_synchronize(&chan->vchan);
1265}
1266
1267static int stm32_mdma_slave_config(struct dma_chan *c,
1268 struct dma_slave_config *config)
1269{
1270 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
1271
1272 memcpy(&chan->dma_config, config, sizeof(*config));
1273
1274 return 0;
1275}
1276
1277static size_t stm32_mdma_desc_residue(struct stm32_mdma_chan *chan,
1278 struct stm32_mdma_desc *desc,
1279 u32 curr_hwdesc)
1280{
1281 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
1282 struct stm32_mdma_hwdesc *hwdesc = desc->node[0].hwdesc;
1283 u32 cbndtr, residue, modulo, burst_size;
1284 int i;
1285
1286 residue = 0;
1287 for (i = curr_hwdesc + 1; i < desc->count; i++) {
1288 hwdesc = desc->node[i].hwdesc;
1289 residue += STM32_MDMA_CBNDTR_BNDT(hwdesc->cbndtr);
1290 }
1291 cbndtr = stm32_mdma_read(dmadev, STM32_MDMA_CBNDTR(chan->id));
1292 residue += cbndtr & STM32_MDMA_CBNDTR_BNDT_MASK;
1293
1294 if (!chan->mem_burst)
1295 return residue;
1296
1297 burst_size = chan->mem_burst * chan->mem_width;
1298 modulo = residue % burst_size;
1299 if (modulo)
1300 residue = residue - modulo + burst_size;
1301
1302 return residue;
1303}
1304
1305static enum dma_status stm32_mdma_tx_status(struct dma_chan *c,
1306 dma_cookie_t cookie,
1307 struct dma_tx_state *state)
1308{
1309 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
1310 struct virt_dma_desc *vdesc;
1311 enum dma_status status;
1312 unsigned long flags;
1313 u32 residue = 0;
1314
1315 status = dma_cookie_status(c, cookie, state);
1316 if ((status == DMA_COMPLETE) || (!state))
1317 return status;
1318
1319 spin_lock_irqsave(&chan->vchan.lock, flags);
1320
1321 vdesc = vchan_find_desc(&chan->vchan, cookie);
1322 if (chan->desc && cookie == chan->desc->vdesc.tx.cookie)
1323 residue = stm32_mdma_desc_residue(chan, chan->desc,
1324 chan->curr_hwdesc);
1325 else if (vdesc)
1326 residue = stm32_mdma_desc_residue(chan,
1327 to_stm32_mdma_desc(vdesc), 0);
1328 dma_set_residue(state, residue);
1329
1330 spin_unlock_irqrestore(&chan->vchan.lock, flags);
1331
1332 return status;
1333}
1334
1335static void stm32_mdma_xfer_end(struct stm32_mdma_chan *chan)
1336{
1337 vchan_cookie_complete(&chan->desc->vdesc);
1338 chan->desc = NULL;
1339 chan->busy = false;
1340
1341 /* Start the next transfer if this driver has a next desc */
1342 stm32_mdma_start_transfer(chan);
1343}
1344
1345static irqreturn_t stm32_mdma_irq_handler(int irq, void *devid)
1346{
1347 struct stm32_mdma_device *dmadev = devid;
1348 struct stm32_mdma_chan *chan = devid;
1349 u32 reg, id, ien, status, flag;
1350
1351 /* Find out which channel generates the interrupt */
1352 status = readl_relaxed(dmadev->base + STM32_MDMA_GISR0);
1353 if (status) {
1354 id = __ffs(status);
1355 } else {
1356 status = readl_relaxed(dmadev->base + STM32_MDMA_GISR1);
1357 if (!status) {
1358 dev_dbg(mdma2dev(dmadev), "spurious it\n");
1359 return IRQ_NONE;
1360 }
1361 id = __ffs(status);
1362 /*
1363 * As GISR0 provides status for channel id from 0 to 31,
1364 * so GISR1 provides status for channel id from 32 to 62
1365 */
1366 id += 32;
1367 }
1368
1369 chan = &dmadev->chan[id];
1370 if (!chan) {
1371 dev_dbg(mdma2dev(dmadev), "MDMA channel not initialized\n");
1372 goto exit;
1373 }
1374
1375 /* Handle interrupt for the channel */
1376 spin_lock(&chan->vchan.lock);
1377 status = stm32_mdma_read(dmadev, STM32_MDMA_CISR(chan->id));
1378 ien = stm32_mdma_read(dmadev, STM32_MDMA_CCR(chan->id));
1379 ien &= STM32_MDMA_CCR_IRQ_MASK;
1380 ien >>= 1;
1381
1382 if (!(status & ien)) {
1383 spin_unlock(&chan->vchan.lock);
1384 dev_dbg(chan2dev(chan),
1385 "spurious it (status=0x%04x, ien=0x%04x)\n",
1386 status, ien);
1387 return IRQ_NONE;
1388 }
1389
1390 flag = __ffs(status & ien);
1391 reg = STM32_MDMA_CIFCR(chan->id);
1392
1393 switch (1 << flag) {
1394 case STM32_MDMA_CISR_TEIF:
1395 id = chan->id;
1396 status = readl_relaxed(dmadev->base + STM32_MDMA_CESR(id));
1397 dev_err(chan2dev(chan), "Transfer Err: stat=0x%08x\n", status);
1398 stm32_mdma_set_bits(dmadev, reg, STM32_MDMA_CIFCR_CTEIF);
1399 break;
1400
1401 case STM32_MDMA_CISR_CTCIF:
1402 stm32_mdma_set_bits(dmadev, reg, STM32_MDMA_CIFCR_CCTCIF);
1403 stm32_mdma_xfer_end(chan);
1404 break;
1405
1406 case STM32_MDMA_CISR_BRTIF:
1407 stm32_mdma_set_bits(dmadev, reg, STM32_MDMA_CIFCR_CBRTIF);
1408 break;
1409
1410 case STM32_MDMA_CISR_BTIF:
1411 stm32_mdma_set_bits(dmadev, reg, STM32_MDMA_CIFCR_CBTIF);
1412 chan->curr_hwdesc++;
1413 if (chan->desc && chan->desc->cyclic) {
1414 if (chan->curr_hwdesc == chan->desc->count)
1415 chan->curr_hwdesc = 0;
1416 vchan_cyclic_callback(&chan->desc->vdesc);
1417 }
1418 break;
1419
1420 case STM32_MDMA_CISR_TCIF:
1421 stm32_mdma_set_bits(dmadev, reg, STM32_MDMA_CIFCR_CLTCIF);
1422 break;
1423
1424 default:
1425 dev_err(chan2dev(chan), "it %d unhandled (status=0x%04x)\n",
1426 1 << flag, status);
1427 }
1428
1429 spin_unlock(&chan->vchan.lock);
1430
1431exit:
1432 return IRQ_HANDLED;
1433}
1434
1435static int stm32_mdma_alloc_chan_resources(struct dma_chan *c)
1436{
1437 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
1438 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
1439 int ret;
1440
1441 chan->desc_pool = dmam_pool_create(dev_name(&c->dev->device),
1442 c->device->dev,
1443 sizeof(struct stm32_mdma_hwdesc),
1444 __alignof__(struct stm32_mdma_hwdesc),
1445 0);
1446 if (!chan->desc_pool) {
1447 dev_err(chan2dev(chan), "failed to allocate descriptor pool\n");
1448 return -ENOMEM;
1449 }
1450
1451 ret = pm_runtime_get_sync(dmadev->ddev.dev);
1452 if (ret < 0)
1453 return ret;
1454
1455 ret = stm32_mdma_disable_chan(chan);
1456 if (ret < 0)
1457 pm_runtime_put(dmadev->ddev.dev);
1458
1459 return ret;
1460}
1461
1462static void stm32_mdma_free_chan_resources(struct dma_chan *c)
1463{
1464 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
1465 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
1466 unsigned long flags;
1467
1468 dev_dbg(chan2dev(chan), "Freeing channel %d\n", chan->id);
1469
1470 if (chan->busy) {
1471 spin_lock_irqsave(&chan->vchan.lock, flags);
1472 stm32_mdma_stop(chan);
1473 chan->desc = NULL;
1474 spin_unlock_irqrestore(&chan->vchan.lock, flags);
1475 }
1476
1477 pm_runtime_put(dmadev->ddev.dev);
1478 vchan_free_chan_resources(to_virt_chan(c));
1479 dmam_pool_destroy(chan->desc_pool);
1480 chan->desc_pool = NULL;
1481}
1482
1483static struct dma_chan *stm32_mdma_of_xlate(struct of_phandle_args *dma_spec,
1484 struct of_dma *ofdma)
1485{
1486 struct stm32_mdma_device *dmadev = ofdma->of_dma_data;
1487 struct stm32_mdma_chan *chan;
1488 struct dma_chan *c;
1489 struct stm32_mdma_chan_config config;
1490
1491 if (dma_spec->args_count < 5) {
1492 dev_err(mdma2dev(dmadev), "Bad number of args\n");
1493 return NULL;
1494 }
1495
1496 config.request = dma_spec->args[0];
1497 config.priority_level = dma_spec->args[1];
1498 config.transfer_config = dma_spec->args[2];
1499 config.mask_addr = dma_spec->args[3];
1500 config.mask_data = dma_spec->args[4];
1501
1502 if (config.request >= dmadev->nr_requests) {
1503 dev_err(mdma2dev(dmadev), "Bad request line\n");
1504 return NULL;
1505 }
1506
1507 if (config.priority_level > STM32_MDMA_VERY_HIGH_PRIORITY) {
1508 dev_err(mdma2dev(dmadev), "Priority level not supported\n");
1509 return NULL;
1510 }
1511
1512 c = dma_get_any_slave_channel(&dmadev->ddev);
1513 if (!c) {
1514 dev_err(mdma2dev(dmadev), "No more channels available\n");
1515 return NULL;
1516 }
1517
1518 chan = to_stm32_mdma_chan(c);
1519 chan->chan_config = config;
1520
1521 return c;
1522}
1523
1524static const struct of_device_id stm32_mdma_of_match[] = {
1525 { .compatible = "st,stm32h7-mdma", },
1526 { /* sentinel */ },
1527};
1528MODULE_DEVICE_TABLE(of, stm32_mdma_of_match);
1529
1530static int stm32_mdma_probe(struct platform_device *pdev)
1531{
1532 struct stm32_mdma_chan *chan;
1533 struct stm32_mdma_device *dmadev;
1534 struct dma_device *dd;
1535 struct device_node *of_node;
1536 struct resource *res;
1537 struct reset_control *rst;
1538 u32 nr_channels, nr_requests;
1539 int i, count, ret;
1540
1541 of_node = pdev->dev.of_node;
1542 if (!of_node)
1543 return -ENODEV;
1544
1545 ret = device_property_read_u32(&pdev->dev, "dma-channels",
1546 &nr_channels);
1547 if (ret) {
1548 nr_channels = STM32_MDMA_MAX_CHANNELS;
1549 dev_warn(&pdev->dev, "MDMA defaulting on %i channels\n",
1550 nr_channels);
1551 }
1552
1553 ret = device_property_read_u32(&pdev->dev, "dma-requests",
1554 &nr_requests);
1555 if (ret) {
1556 nr_requests = STM32_MDMA_MAX_REQUESTS;
1557 dev_warn(&pdev->dev, "MDMA defaulting on %i request lines\n",
1558 nr_requests);
1559 }
1560
1561 count = device_property_count_u32(&pdev->dev, "st,ahb-addr-masks");
1562 if (count < 0)
1563 count = 0;
1564
1565 dmadev = devm_kzalloc(&pdev->dev, sizeof(*dmadev) + sizeof(u32) * count,
1566 GFP_KERNEL);
1567 if (!dmadev)
1568 return -ENOMEM;
1569
1570 dmadev->nr_channels = nr_channels;
1571 dmadev->nr_requests = nr_requests;
1572 device_property_read_u32_array(&pdev->dev, "st,ahb-addr-masks",
1573 dmadev->ahb_addr_masks,
1574 count);
1575 dmadev->nr_ahb_addr_masks = count;
1576
1577 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1578 dmadev->base = devm_ioremap_resource(&pdev->dev, res);
1579 if (IS_ERR(dmadev->base))
1580 return PTR_ERR(dmadev->base);
1581
1582 dmadev->clk = devm_clk_get(&pdev->dev, NULL);
1583 if (IS_ERR(dmadev->clk)) {
1584 ret = PTR_ERR(dmadev->clk);
1585 if (ret != -EPROBE_DEFER)
1586 dev_err(&pdev->dev, "Missing clock controller\n");
1587 return ret;
1588 }
1589
1590 ret = clk_prepare_enable(dmadev->clk);
1591 if (ret < 0) {
1592 dev_err(&pdev->dev, "clk_prep_enable error: %d\n", ret);
1593 return ret;
1594 }
1595
1596 rst = devm_reset_control_get(&pdev->dev, NULL);
1597 if (IS_ERR(rst)) {
1598 ret = PTR_ERR(rst);
1599 if (ret == -EPROBE_DEFER)
1600 goto err_clk;
1601 } else {
1602 reset_control_assert(rst);
1603 udelay(2);
1604 reset_control_deassert(rst);
1605 }
1606
1607 dd = &dmadev->ddev;
1608 dma_cap_set(DMA_SLAVE, dd->cap_mask);
1609 dma_cap_set(DMA_PRIVATE, dd->cap_mask);
1610 dma_cap_set(DMA_CYCLIC, dd->cap_mask);
1611 dma_cap_set(DMA_MEMCPY, dd->cap_mask);
1612 dd->device_alloc_chan_resources = stm32_mdma_alloc_chan_resources;
1613 dd->device_free_chan_resources = stm32_mdma_free_chan_resources;
1614 dd->device_tx_status = stm32_mdma_tx_status;
1615 dd->device_issue_pending = stm32_mdma_issue_pending;
1616 dd->device_prep_slave_sg = stm32_mdma_prep_slave_sg;
1617 dd->device_prep_dma_cyclic = stm32_mdma_prep_dma_cyclic;
1618 dd->device_prep_dma_memcpy = stm32_mdma_prep_dma_memcpy;
1619 dd->device_config = stm32_mdma_slave_config;
1620 dd->device_pause = stm32_mdma_pause;
1621 dd->device_resume = stm32_mdma_resume;
1622 dd->device_terminate_all = stm32_mdma_terminate_all;
1623 dd->device_synchronize = stm32_mdma_synchronize;
1624 dd->descriptor_reuse = true;
1625
1626 dd->src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1627 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1628 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |
1629 BIT(DMA_SLAVE_BUSWIDTH_8_BYTES);
1630 dd->dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1631 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1632 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |
1633 BIT(DMA_SLAVE_BUSWIDTH_8_BYTES);
1634 dd->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV) |
1635 BIT(DMA_MEM_TO_MEM);
1636 dd->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
1637 dd->max_burst = STM32_MDMA_MAX_BURST;
1638 dd->dev = &pdev->dev;
1639 INIT_LIST_HEAD(&dd->channels);
1640
1641 for (i = 0; i < dmadev->nr_channels; i++) {
1642 chan = &dmadev->chan[i];
1643 chan->id = i;
1644 chan->vchan.desc_free = stm32_mdma_desc_free;
1645 vchan_init(&chan->vchan, dd);
1646 }
1647
1648 dmadev->irq = platform_get_irq(pdev, 0);
1649 if (dmadev->irq < 0) {
1650 ret = dmadev->irq;
1651 goto err_clk;
1652 }
1653
1654 ret = devm_request_irq(&pdev->dev, dmadev->irq, stm32_mdma_irq_handler,
1655 0, dev_name(&pdev->dev), dmadev);
1656 if (ret) {
1657 dev_err(&pdev->dev, "failed to request IRQ\n");
1658 goto err_clk;
1659 }
1660
1661 ret = dmaenginem_async_device_register(dd);
1662 if (ret)
1663 goto err_clk;
1664
1665 ret = of_dma_controller_register(of_node, stm32_mdma_of_xlate, dmadev);
1666 if (ret < 0) {
1667 dev_err(&pdev->dev,
1668 "STM32 MDMA DMA OF registration failed %d\n", ret);
1669 goto err_clk;
1670 }
1671
1672 platform_set_drvdata(pdev, dmadev);
1673 pm_runtime_set_active(&pdev->dev);
1674 pm_runtime_enable(&pdev->dev);
1675 pm_runtime_get_noresume(&pdev->dev);
1676 pm_runtime_put(&pdev->dev);
1677
1678 dev_info(&pdev->dev, "STM32 MDMA driver registered\n");
1679
1680 return 0;
1681
1682err_clk:
1683 clk_disable_unprepare(dmadev->clk);
1684
1685 return ret;
1686}
1687
1688#ifdef CONFIG_PM
1689static int stm32_mdma_runtime_suspend(struct device *dev)
1690{
1691 struct stm32_mdma_device *dmadev = dev_get_drvdata(dev);
1692
1693 clk_disable_unprepare(dmadev->clk);
1694
1695 return 0;
1696}
1697
1698static int stm32_mdma_runtime_resume(struct device *dev)
1699{
1700 struct stm32_mdma_device *dmadev = dev_get_drvdata(dev);
1701 int ret;
1702
1703 ret = clk_prepare_enable(dmadev->clk);
1704 if (ret) {
1705 dev_err(dev, "failed to prepare_enable clock\n");
1706 return ret;
1707 }
1708
1709 return 0;
1710}
1711#endif
1712
1713#ifdef CONFIG_PM_SLEEP
1714static int stm32_mdma_pm_suspend(struct device *dev)
1715{
1716 struct stm32_mdma_device *dmadev = dev_get_drvdata(dev);
1717 u32 ccr, id;
1718 int ret;
1719
1720 ret = pm_runtime_get_sync(dev);
1721 if (ret < 0)
1722 return ret;
1723
1724 for (id = 0; id < dmadev->nr_channels; id++) {
1725 ccr = stm32_mdma_read(dmadev, STM32_MDMA_CCR(id));
1726 if (ccr & STM32_MDMA_CCR_EN) {
1727 dev_warn(dev, "Suspend is prevented by Chan %i\n", id);
1728 return -EBUSY;
1729 }
1730 }
1731
1732 pm_runtime_put_sync(dev);
1733
1734 pm_runtime_force_suspend(dev);
1735
1736 return 0;
1737}
1738
1739static int stm32_mdma_pm_resume(struct device *dev)
1740{
1741 return pm_runtime_force_resume(dev);
1742}
1743#endif
1744
1745static const struct dev_pm_ops stm32_mdma_pm_ops = {
1746 SET_SYSTEM_SLEEP_PM_OPS(stm32_mdma_pm_suspend, stm32_mdma_pm_resume)
1747 SET_RUNTIME_PM_OPS(stm32_mdma_runtime_suspend,
1748 stm32_mdma_runtime_resume, NULL)
1749};
1750
1751static struct platform_driver stm32_mdma_driver = {
1752 .probe = stm32_mdma_probe,
1753 .driver = {
1754 .name = "stm32-mdma",
1755 .of_match_table = stm32_mdma_of_match,
1756 .pm = &stm32_mdma_pm_ops,
1757 },
1758};
1759
1760static int __init stm32_mdma_init(void)
1761{
1762 return platform_driver_register(&stm32_mdma_driver);
1763}
1764
1765subsys_initcall(stm32_mdma_init);
1766
1767MODULE_DESCRIPTION("Driver for STM32 MDMA controller");
1768MODULE_AUTHOR("M'boumba Cedric Madianga <cedric.madianga@gmail.com>");
1769MODULE_AUTHOR("Pierre-Yves Mordret <pierre-yves.mordret@st.com>");
1770MODULE_LICENSE("GPL v2");
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 *
4 * Copyright (C) STMicroelectronics SA 2017
5 * Author(s): M'boumba Cedric Madianga <cedric.madianga@gmail.com>
6 * Pierre-Yves Mordret <pierre-yves.mordret@st.com>
7 *
8 * Driver for STM32 MDMA controller
9 *
10 * Inspired by stm32-dma.c and dma-jz4780.c
11 */
12
13#include <linux/bitfield.h>
14#include <linux/clk.h>
15#include <linux/delay.h>
16#include <linux/dmaengine.h>
17#include <linux/dma-mapping.h>
18#include <linux/dmapool.h>
19#include <linux/err.h>
20#include <linux/init.h>
21#include <linux/iopoll.h>
22#include <linux/jiffies.h>
23#include <linux/list.h>
24#include <linux/log2.h>
25#include <linux/module.h>
26#include <linux/of.h>
27#include <linux/of_dma.h>
28#include <linux/platform_device.h>
29#include <linux/pm_runtime.h>
30#include <linux/reset.h>
31#include <linux/slab.h>
32
33#include "virt-dma.h"
34
35#define STM32_MDMA_GISR0 0x0000 /* MDMA Int Status Reg 1 */
36
37/* MDMA Channel x interrupt/status register */
38#define STM32_MDMA_CISR(x) (0x40 + 0x40 * (x)) /* x = 0..62 */
39#define STM32_MDMA_CISR_CRQA BIT(16)
40#define STM32_MDMA_CISR_TCIF BIT(4)
41#define STM32_MDMA_CISR_BTIF BIT(3)
42#define STM32_MDMA_CISR_BRTIF BIT(2)
43#define STM32_MDMA_CISR_CTCIF BIT(1)
44#define STM32_MDMA_CISR_TEIF BIT(0)
45
46/* MDMA Channel x interrupt flag clear register */
47#define STM32_MDMA_CIFCR(x) (0x44 + 0x40 * (x))
48#define STM32_MDMA_CIFCR_CLTCIF BIT(4)
49#define STM32_MDMA_CIFCR_CBTIF BIT(3)
50#define STM32_MDMA_CIFCR_CBRTIF BIT(2)
51#define STM32_MDMA_CIFCR_CCTCIF BIT(1)
52#define STM32_MDMA_CIFCR_CTEIF BIT(0)
53#define STM32_MDMA_CIFCR_CLEAR_ALL (STM32_MDMA_CIFCR_CLTCIF \
54 | STM32_MDMA_CIFCR_CBTIF \
55 | STM32_MDMA_CIFCR_CBRTIF \
56 | STM32_MDMA_CIFCR_CCTCIF \
57 | STM32_MDMA_CIFCR_CTEIF)
58
59/* MDMA Channel x error status register */
60#define STM32_MDMA_CESR(x) (0x48 + 0x40 * (x))
61#define STM32_MDMA_CESR_BSE BIT(11)
62#define STM32_MDMA_CESR_ASR BIT(10)
63#define STM32_MDMA_CESR_TEMD BIT(9)
64#define STM32_MDMA_CESR_TELD BIT(8)
65#define STM32_MDMA_CESR_TED BIT(7)
66#define STM32_MDMA_CESR_TEA_MASK GENMASK(6, 0)
67
68/* MDMA Channel x control register */
69#define STM32_MDMA_CCR(x) (0x4C + 0x40 * (x))
70#define STM32_MDMA_CCR_SWRQ BIT(16)
71#define STM32_MDMA_CCR_WEX BIT(14)
72#define STM32_MDMA_CCR_HEX BIT(13)
73#define STM32_MDMA_CCR_BEX BIT(12)
74#define STM32_MDMA_CCR_SM BIT(8)
75#define STM32_MDMA_CCR_PL_MASK GENMASK(7, 6)
76#define STM32_MDMA_CCR_PL(n) FIELD_PREP(STM32_MDMA_CCR_PL_MASK, (n))
77#define STM32_MDMA_CCR_TCIE BIT(5)
78#define STM32_MDMA_CCR_BTIE BIT(4)
79#define STM32_MDMA_CCR_BRTIE BIT(3)
80#define STM32_MDMA_CCR_CTCIE BIT(2)
81#define STM32_MDMA_CCR_TEIE BIT(1)
82#define STM32_MDMA_CCR_EN BIT(0)
83#define STM32_MDMA_CCR_IRQ_MASK (STM32_MDMA_CCR_TCIE \
84 | STM32_MDMA_CCR_BTIE \
85 | STM32_MDMA_CCR_BRTIE \
86 | STM32_MDMA_CCR_CTCIE \
87 | STM32_MDMA_CCR_TEIE)
88
89/* MDMA Channel x transfer configuration register */
90#define STM32_MDMA_CTCR(x) (0x50 + 0x40 * (x))
91#define STM32_MDMA_CTCR_BWM BIT(31)
92#define STM32_MDMA_CTCR_SWRM BIT(30)
93#define STM32_MDMA_CTCR_TRGM_MSK GENMASK(29, 28)
94#define STM32_MDMA_CTCR_TRGM(n) FIELD_PREP(STM32_MDMA_CTCR_TRGM_MSK, (n))
95#define STM32_MDMA_CTCR_TRGM_GET(n) FIELD_GET(STM32_MDMA_CTCR_TRGM_MSK, (n))
96#define STM32_MDMA_CTCR_PAM_MASK GENMASK(27, 26)
97#define STM32_MDMA_CTCR_PAM(n) FIELD_PREP(STM32_MDMA_CTCR_PAM_MASK, (n))
98#define STM32_MDMA_CTCR_PKE BIT(25)
99#define STM32_MDMA_CTCR_TLEN_MSK GENMASK(24, 18)
100#define STM32_MDMA_CTCR_TLEN(n) FIELD_PREP(STM32_MDMA_CTCR_TLEN_MSK, (n))
101#define STM32_MDMA_CTCR_TLEN_GET(n) FIELD_GET(STM32_MDMA_CTCR_TLEN_MSK, (n))
102#define STM32_MDMA_CTCR_LEN2_MSK GENMASK(25, 18)
103#define STM32_MDMA_CTCR_LEN2(n) FIELD_PREP(STM32_MDMA_CTCR_LEN2_MSK, (n))
104#define STM32_MDMA_CTCR_LEN2_GET(n) FIELD_GET(STM32_MDMA_CTCR_LEN2_MSK, (n))
105#define STM32_MDMA_CTCR_DBURST_MASK GENMASK(17, 15)
106#define STM32_MDMA_CTCR_DBURST(n) FIELD_PREP(STM32_MDMA_CTCR_DBURST_MASK, (n))
107#define STM32_MDMA_CTCR_SBURST_MASK GENMASK(14, 12)
108#define STM32_MDMA_CTCR_SBURST(n) FIELD_PREP(STM32_MDMA_CTCR_SBURST_MASK, (n))
109#define STM32_MDMA_CTCR_DINCOS_MASK GENMASK(11, 10)
110#define STM32_MDMA_CTCR_DINCOS(n) FIELD_PREP(STM32_MDMA_CTCR_DINCOS_MASK, (n))
111#define STM32_MDMA_CTCR_SINCOS_MASK GENMASK(9, 8)
112#define STM32_MDMA_CTCR_SINCOS(n) FIELD_PREP(STM32_MDMA_CTCR_SINCOS_MASK, (n))
113#define STM32_MDMA_CTCR_DSIZE_MASK GENMASK(7, 6)
114#define STM32_MDMA_CTCR_DSIZE(n) FIELD_PREP(STM32_MDMA_CTCR_DSIZE_MASK, (n))
115#define STM32_MDMA_CTCR_SSIZE_MASK GENMASK(5, 4)
116#define STM32_MDMA_CTCR_SSIZE(n) FIELD_PREP(STM32_MDMA_CTCR_SSIZE_MASK, (n))
117#define STM32_MDMA_CTCR_DINC_MASK GENMASK(3, 2)
118#define STM32_MDMA_CTCR_DINC(n) FIELD_PREP(STM32_MDMA_CTCR_DINC_MASK, (n))
119#define STM32_MDMA_CTCR_SINC_MASK GENMASK(1, 0)
120#define STM32_MDMA_CTCR_SINC(n) FIELD_PREP(STM32_MDMA_CTCR_SINC_MASK, (n))
121#define STM32_MDMA_CTCR_CFG_MASK (STM32_MDMA_CTCR_SINC_MASK \
122 | STM32_MDMA_CTCR_DINC_MASK \
123 | STM32_MDMA_CTCR_SINCOS_MASK \
124 | STM32_MDMA_CTCR_DINCOS_MASK \
125 | STM32_MDMA_CTCR_LEN2_MSK \
126 | STM32_MDMA_CTCR_TRGM_MSK)
127
128/* MDMA Channel x block number of data register */
129#define STM32_MDMA_CBNDTR(x) (0x54 + 0x40 * (x))
130#define STM32_MDMA_CBNDTR_BRC_MK GENMASK(31, 20)
131#define STM32_MDMA_CBNDTR_BRC(n) FIELD_PREP(STM32_MDMA_CBNDTR_BRC_MK, (n))
132#define STM32_MDMA_CBNDTR_BRC_GET(n) FIELD_GET(STM32_MDMA_CBNDTR_BRC_MK, (n))
133
134#define STM32_MDMA_CBNDTR_BRDUM BIT(19)
135#define STM32_MDMA_CBNDTR_BRSUM BIT(18)
136#define STM32_MDMA_CBNDTR_BNDT_MASK GENMASK(16, 0)
137#define STM32_MDMA_CBNDTR_BNDT(n) FIELD_PREP(STM32_MDMA_CBNDTR_BNDT_MASK, (n))
138
139/* MDMA Channel x source address register */
140#define STM32_MDMA_CSAR(x) (0x58 + 0x40 * (x))
141
142/* MDMA Channel x destination address register */
143#define STM32_MDMA_CDAR(x) (0x5C + 0x40 * (x))
144
145/* MDMA Channel x block repeat address update register */
146#define STM32_MDMA_CBRUR(x) (0x60 + 0x40 * (x))
147#define STM32_MDMA_CBRUR_DUV_MASK GENMASK(31, 16)
148#define STM32_MDMA_CBRUR_DUV(n) FIELD_PREP(STM32_MDMA_CBRUR_DUV_MASK, (n))
149#define STM32_MDMA_CBRUR_SUV_MASK GENMASK(15, 0)
150#define STM32_MDMA_CBRUR_SUV(n) FIELD_PREP(STM32_MDMA_CBRUR_SUV_MASK, (n))
151
152/* MDMA Channel x link address register */
153#define STM32_MDMA_CLAR(x) (0x64 + 0x40 * (x))
154
155/* MDMA Channel x trigger and bus selection register */
156#define STM32_MDMA_CTBR(x) (0x68 + 0x40 * (x))
157#define STM32_MDMA_CTBR_DBUS BIT(17)
158#define STM32_MDMA_CTBR_SBUS BIT(16)
159#define STM32_MDMA_CTBR_TSEL_MASK GENMASK(5, 0)
160#define STM32_MDMA_CTBR_TSEL(n) FIELD_PREP(STM32_MDMA_CTBR_TSEL_MASK, (n))
161
162/* MDMA Channel x mask address register */
163#define STM32_MDMA_CMAR(x) (0x70 + 0x40 * (x))
164
165/* MDMA Channel x mask data register */
166#define STM32_MDMA_CMDR(x) (0x74 + 0x40 * (x))
167
168#define STM32_MDMA_MAX_BUF_LEN 128
169#define STM32_MDMA_MAX_BLOCK_LEN 65536
170#define STM32_MDMA_MAX_CHANNELS 32
171#define STM32_MDMA_MAX_REQUESTS 256
172#define STM32_MDMA_MAX_BURST 128
173#define STM32_MDMA_VERY_HIGH_PRIORITY 0x3
174
175enum stm32_mdma_trigger_mode {
176 STM32_MDMA_BUFFER,
177 STM32_MDMA_BLOCK,
178 STM32_MDMA_BLOCK_REP,
179 STM32_MDMA_LINKED_LIST,
180};
181
182enum stm32_mdma_width {
183 STM32_MDMA_BYTE,
184 STM32_MDMA_HALF_WORD,
185 STM32_MDMA_WORD,
186 STM32_MDMA_DOUBLE_WORD,
187};
188
189enum stm32_mdma_inc_mode {
190 STM32_MDMA_FIXED = 0,
191 STM32_MDMA_INC = 2,
192 STM32_MDMA_DEC = 3,
193};
194
195struct stm32_mdma_chan_config {
196 u32 request;
197 u32 priority_level;
198 u32 transfer_config;
199 u32 mask_addr;
200 u32 mask_data;
201 bool m2m_hw; /* True when MDMA is triggered by STM32 DMA */
202};
203
204struct stm32_mdma_hwdesc {
205 u32 ctcr;
206 u32 cbndtr;
207 u32 csar;
208 u32 cdar;
209 u32 cbrur;
210 u32 clar;
211 u32 ctbr;
212 u32 dummy;
213 u32 cmar;
214 u32 cmdr;
215} __aligned(64);
216
217struct stm32_mdma_desc_node {
218 struct stm32_mdma_hwdesc *hwdesc;
219 dma_addr_t hwdesc_phys;
220};
221
222struct stm32_mdma_desc {
223 struct virt_dma_desc vdesc;
224 u32 ccr;
225 bool cyclic;
226 u32 count;
227 struct stm32_mdma_desc_node node[] __counted_by(count);
228};
229
230struct stm32_mdma_dma_config {
231 u32 request; /* STM32 DMA channel stream id, triggering MDMA */
232 u32 cmar; /* STM32 DMA interrupt flag clear register address */
233 u32 cmdr; /* STM32 DMA Transfer Complete flag */
234};
235
236struct stm32_mdma_chan {
237 struct virt_dma_chan vchan;
238 struct dma_pool *desc_pool;
239 u32 id;
240 struct stm32_mdma_desc *desc;
241 u32 curr_hwdesc;
242 struct dma_slave_config dma_config;
243 struct stm32_mdma_chan_config chan_config;
244 bool busy;
245 u32 mem_burst;
246 u32 mem_width;
247};
248
249struct stm32_mdma_device {
250 struct dma_device ddev;
251 void __iomem *base;
252 struct clk *clk;
253 int irq;
254 u32 nr_channels;
255 u32 nr_requests;
256 u32 nr_ahb_addr_masks;
257 u32 chan_reserved;
258 struct stm32_mdma_chan chan[STM32_MDMA_MAX_CHANNELS];
259 u32 ahb_addr_masks[] __counted_by(nr_ahb_addr_masks);
260};
261
262static struct stm32_mdma_device *stm32_mdma_get_dev(
263 struct stm32_mdma_chan *chan)
264{
265 return container_of(chan->vchan.chan.device, struct stm32_mdma_device,
266 ddev);
267}
268
269static struct stm32_mdma_chan *to_stm32_mdma_chan(struct dma_chan *c)
270{
271 return container_of(c, struct stm32_mdma_chan, vchan.chan);
272}
273
274static struct stm32_mdma_desc *to_stm32_mdma_desc(struct virt_dma_desc *vdesc)
275{
276 return container_of(vdesc, struct stm32_mdma_desc, vdesc);
277}
278
279static struct device *chan2dev(struct stm32_mdma_chan *chan)
280{
281 return &chan->vchan.chan.dev->device;
282}
283
284static struct device *mdma2dev(struct stm32_mdma_device *mdma_dev)
285{
286 return mdma_dev->ddev.dev;
287}
288
289static u32 stm32_mdma_read(struct stm32_mdma_device *dmadev, u32 reg)
290{
291 return readl_relaxed(dmadev->base + reg);
292}
293
294static void stm32_mdma_write(struct stm32_mdma_device *dmadev, u32 reg, u32 val)
295{
296 writel_relaxed(val, dmadev->base + reg);
297}
298
299static void stm32_mdma_set_bits(struct stm32_mdma_device *dmadev, u32 reg,
300 u32 mask)
301{
302 void __iomem *addr = dmadev->base + reg;
303
304 writel_relaxed(readl_relaxed(addr) | mask, addr);
305}
306
307static void stm32_mdma_clr_bits(struct stm32_mdma_device *dmadev, u32 reg,
308 u32 mask)
309{
310 void __iomem *addr = dmadev->base + reg;
311
312 writel_relaxed(readl_relaxed(addr) & ~mask, addr);
313}
314
315static struct stm32_mdma_desc *stm32_mdma_alloc_desc(
316 struct stm32_mdma_chan *chan, u32 count)
317{
318 struct stm32_mdma_desc *desc;
319 int i;
320
321 desc = kzalloc(struct_size(desc, node, count), GFP_NOWAIT);
322 if (!desc)
323 return NULL;
324 desc->count = count;
325
326 for (i = 0; i < count; i++) {
327 desc->node[i].hwdesc =
328 dma_pool_alloc(chan->desc_pool, GFP_NOWAIT,
329 &desc->node[i].hwdesc_phys);
330 if (!desc->node[i].hwdesc)
331 goto err;
332 }
333
334 return desc;
335
336err:
337 dev_err(chan2dev(chan), "Failed to allocate descriptor\n");
338 while (--i >= 0)
339 dma_pool_free(chan->desc_pool, desc->node[i].hwdesc,
340 desc->node[i].hwdesc_phys);
341 kfree(desc);
342 return NULL;
343}
344
345static void stm32_mdma_desc_free(struct virt_dma_desc *vdesc)
346{
347 struct stm32_mdma_desc *desc = to_stm32_mdma_desc(vdesc);
348 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(vdesc->tx.chan);
349 int i;
350
351 for (i = 0; i < desc->count; i++)
352 dma_pool_free(chan->desc_pool, desc->node[i].hwdesc,
353 desc->node[i].hwdesc_phys);
354 kfree(desc);
355}
356
357static int stm32_mdma_get_width(struct stm32_mdma_chan *chan,
358 enum dma_slave_buswidth width)
359{
360 switch (width) {
361 case DMA_SLAVE_BUSWIDTH_1_BYTE:
362 case DMA_SLAVE_BUSWIDTH_2_BYTES:
363 case DMA_SLAVE_BUSWIDTH_4_BYTES:
364 case DMA_SLAVE_BUSWIDTH_8_BYTES:
365 return ffs(width) - 1;
366 default:
367 dev_err(chan2dev(chan), "Dma bus width %i not supported\n",
368 width);
369 return -EINVAL;
370 }
371}
372
373static enum dma_slave_buswidth stm32_mdma_get_max_width(dma_addr_t addr,
374 u32 buf_len, u32 tlen)
375{
376 enum dma_slave_buswidth max_width = DMA_SLAVE_BUSWIDTH_8_BYTES;
377
378 for (max_width = DMA_SLAVE_BUSWIDTH_8_BYTES;
379 max_width > DMA_SLAVE_BUSWIDTH_1_BYTE;
380 max_width >>= 1) {
381 /*
382 * Address and buffer length both have to be aligned on
383 * bus width
384 */
385 if ((((buf_len | addr) & (max_width - 1)) == 0) &&
386 tlen >= max_width)
387 break;
388 }
389
390 return max_width;
391}
392
393static u32 stm32_mdma_get_best_burst(u32 buf_len, u32 tlen, u32 max_burst,
394 enum dma_slave_buswidth width)
395{
396 u32 best_burst;
397
398 best_burst = min((u32)1 << __ffs(tlen | buf_len),
399 max_burst * width) / width;
400
401 return (best_burst > 0) ? best_burst : 1;
402}
403
404static int stm32_mdma_disable_chan(struct stm32_mdma_chan *chan)
405{
406 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
407 u32 ccr, cisr, id, reg;
408 int ret;
409
410 id = chan->id;
411 reg = STM32_MDMA_CCR(id);
412
413 /* Disable interrupts */
414 stm32_mdma_clr_bits(dmadev, reg, STM32_MDMA_CCR_IRQ_MASK);
415
416 ccr = stm32_mdma_read(dmadev, reg);
417 if (ccr & STM32_MDMA_CCR_EN) {
418 stm32_mdma_clr_bits(dmadev, reg, STM32_MDMA_CCR_EN);
419
420 /* Ensure that any ongoing transfer has been completed */
421 ret = readl_relaxed_poll_timeout_atomic(
422 dmadev->base + STM32_MDMA_CISR(id), cisr,
423 (cisr & STM32_MDMA_CISR_CTCIF), 10, 1000);
424 if (ret) {
425 dev_err(chan2dev(chan), "%s: timeout!\n", __func__);
426 return -EBUSY;
427 }
428 }
429
430 return 0;
431}
432
433static void stm32_mdma_stop(struct stm32_mdma_chan *chan)
434{
435 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
436 u32 status;
437 int ret;
438
439 /* Disable DMA */
440 ret = stm32_mdma_disable_chan(chan);
441 if (ret < 0)
442 return;
443
444 /* Clear interrupt status if it is there */
445 status = stm32_mdma_read(dmadev, STM32_MDMA_CISR(chan->id));
446 if (status) {
447 dev_dbg(chan2dev(chan), "%s(): clearing interrupt: 0x%08x\n",
448 __func__, status);
449 stm32_mdma_set_bits(dmadev, STM32_MDMA_CIFCR(chan->id), status);
450 }
451
452 chan->busy = false;
453}
454
455static void stm32_mdma_set_bus(struct stm32_mdma_device *dmadev, u32 *ctbr,
456 u32 ctbr_mask, u32 src_addr)
457{
458 u32 mask;
459 int i;
460
461 /* Check if memory device is on AHB or AXI */
462 *ctbr &= ~ctbr_mask;
463 mask = src_addr & 0xF0000000;
464 for (i = 0; i < dmadev->nr_ahb_addr_masks; i++) {
465 if (mask == dmadev->ahb_addr_masks[i]) {
466 *ctbr |= ctbr_mask;
467 break;
468 }
469 }
470}
471
472static int stm32_mdma_set_xfer_param(struct stm32_mdma_chan *chan,
473 enum dma_transfer_direction direction,
474 u32 *mdma_ccr, u32 *mdma_ctcr,
475 u32 *mdma_ctbr, dma_addr_t addr,
476 u32 buf_len)
477{
478 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
479 struct stm32_mdma_chan_config *chan_config = &chan->chan_config;
480 enum dma_slave_buswidth src_addr_width, dst_addr_width;
481 phys_addr_t src_addr, dst_addr;
482 int src_bus_width, dst_bus_width;
483 u32 src_maxburst, dst_maxburst, src_best_burst, dst_best_burst;
484 u32 ccr, ctcr, ctbr, tlen;
485
486 src_addr_width = chan->dma_config.src_addr_width;
487 dst_addr_width = chan->dma_config.dst_addr_width;
488 src_maxburst = chan->dma_config.src_maxburst;
489 dst_maxburst = chan->dma_config.dst_maxburst;
490
491 ccr = stm32_mdma_read(dmadev, STM32_MDMA_CCR(chan->id)) & ~STM32_MDMA_CCR_EN;
492 ctcr = stm32_mdma_read(dmadev, STM32_MDMA_CTCR(chan->id));
493 ctbr = stm32_mdma_read(dmadev, STM32_MDMA_CTBR(chan->id));
494
495 /* Enable HW request mode */
496 ctcr &= ~STM32_MDMA_CTCR_SWRM;
497
498 /* Set DINC, SINC, DINCOS, SINCOS, TRGM and TLEN retrieve from DT */
499 ctcr &= ~STM32_MDMA_CTCR_CFG_MASK;
500 ctcr |= chan_config->transfer_config & STM32_MDMA_CTCR_CFG_MASK;
501
502 /*
503 * For buffer transfer length (TLEN) we have to set
504 * the number of bytes - 1 in CTCR register
505 */
506 tlen = STM32_MDMA_CTCR_LEN2_GET(ctcr);
507 ctcr &= ~STM32_MDMA_CTCR_LEN2_MSK;
508 ctcr |= STM32_MDMA_CTCR_TLEN((tlen - 1));
509
510 /* Disable Pack Enable */
511 ctcr &= ~STM32_MDMA_CTCR_PKE;
512
513 /* Check burst size constraints */
514 if (src_maxburst * src_addr_width > STM32_MDMA_MAX_BURST ||
515 dst_maxburst * dst_addr_width > STM32_MDMA_MAX_BURST) {
516 dev_err(chan2dev(chan),
517 "burst size * bus width higher than %d bytes\n",
518 STM32_MDMA_MAX_BURST);
519 return -EINVAL;
520 }
521
522 if ((!is_power_of_2(src_maxburst) && src_maxburst > 0) ||
523 (!is_power_of_2(dst_maxburst) && dst_maxburst > 0)) {
524 dev_err(chan2dev(chan), "burst size must be a power of 2\n");
525 return -EINVAL;
526 }
527
528 /*
529 * Configure channel control:
530 * - Clear SW request as in this case this is a HW one
531 * - Clear WEX, HEX and BEX bits
532 * - Set priority level
533 */
534 ccr &= ~(STM32_MDMA_CCR_SWRQ | STM32_MDMA_CCR_WEX | STM32_MDMA_CCR_HEX |
535 STM32_MDMA_CCR_BEX | STM32_MDMA_CCR_PL_MASK);
536 ccr |= STM32_MDMA_CCR_PL(chan_config->priority_level);
537
538 /* Configure Trigger selection */
539 ctbr &= ~STM32_MDMA_CTBR_TSEL_MASK;
540 ctbr |= STM32_MDMA_CTBR_TSEL(chan_config->request);
541
542 switch (direction) {
543 case DMA_MEM_TO_DEV:
544 dst_addr = chan->dma_config.dst_addr;
545
546 /* Set device data size */
547 if (chan_config->m2m_hw)
548 dst_addr_width = stm32_mdma_get_max_width(dst_addr, buf_len,
549 STM32_MDMA_MAX_BUF_LEN);
550 dst_bus_width = stm32_mdma_get_width(chan, dst_addr_width);
551 if (dst_bus_width < 0)
552 return dst_bus_width;
553 ctcr &= ~STM32_MDMA_CTCR_DSIZE_MASK;
554 ctcr |= STM32_MDMA_CTCR_DSIZE(dst_bus_width);
555 if (chan_config->m2m_hw) {
556 ctcr &= ~STM32_MDMA_CTCR_DINCOS_MASK;
557 ctcr |= STM32_MDMA_CTCR_DINCOS(dst_bus_width);
558 }
559
560 /* Set device burst value */
561 if (chan_config->m2m_hw)
562 dst_maxburst = STM32_MDMA_MAX_BUF_LEN / dst_addr_width;
563
564 dst_best_burst = stm32_mdma_get_best_burst(buf_len, tlen,
565 dst_maxburst,
566 dst_addr_width);
567 chan->mem_burst = dst_best_burst;
568 ctcr &= ~STM32_MDMA_CTCR_DBURST_MASK;
569 ctcr |= STM32_MDMA_CTCR_DBURST((ilog2(dst_best_burst)));
570
571 /* Set memory data size */
572 src_addr_width = stm32_mdma_get_max_width(addr, buf_len, tlen);
573 chan->mem_width = src_addr_width;
574 src_bus_width = stm32_mdma_get_width(chan, src_addr_width);
575 if (src_bus_width < 0)
576 return src_bus_width;
577 ctcr &= ~STM32_MDMA_CTCR_SSIZE_MASK |
578 STM32_MDMA_CTCR_SINCOS_MASK;
579 ctcr |= STM32_MDMA_CTCR_SSIZE(src_bus_width) |
580 STM32_MDMA_CTCR_SINCOS(src_bus_width);
581
582 /* Set memory burst value */
583 src_maxburst = STM32_MDMA_MAX_BUF_LEN / src_addr_width;
584 src_best_burst = stm32_mdma_get_best_burst(buf_len, tlen,
585 src_maxburst,
586 src_addr_width);
587 chan->mem_burst = src_best_burst;
588 ctcr &= ~STM32_MDMA_CTCR_SBURST_MASK;
589 ctcr |= STM32_MDMA_CTCR_SBURST((ilog2(src_best_burst)));
590
591 /* Select bus */
592 stm32_mdma_set_bus(dmadev, &ctbr, STM32_MDMA_CTBR_DBUS,
593 dst_addr);
594
595 if (dst_bus_width != src_bus_width)
596 ctcr |= STM32_MDMA_CTCR_PKE;
597
598 /* Set destination address */
599 stm32_mdma_write(dmadev, STM32_MDMA_CDAR(chan->id), dst_addr);
600 break;
601
602 case DMA_DEV_TO_MEM:
603 src_addr = chan->dma_config.src_addr;
604
605 /* Set device data size */
606 if (chan_config->m2m_hw)
607 src_addr_width = stm32_mdma_get_max_width(src_addr, buf_len,
608 STM32_MDMA_MAX_BUF_LEN);
609
610 src_bus_width = stm32_mdma_get_width(chan, src_addr_width);
611 if (src_bus_width < 0)
612 return src_bus_width;
613 ctcr &= ~STM32_MDMA_CTCR_SSIZE_MASK;
614 ctcr |= STM32_MDMA_CTCR_SSIZE(src_bus_width);
615 if (chan_config->m2m_hw) {
616 ctcr &= ~STM32_MDMA_CTCR_SINCOS_MASK;
617 ctcr |= STM32_MDMA_CTCR_SINCOS(src_bus_width);
618 }
619
620 /* Set device burst value */
621 if (chan_config->m2m_hw)
622 src_maxburst = STM32_MDMA_MAX_BUF_LEN / src_addr_width;
623
624 src_best_burst = stm32_mdma_get_best_burst(buf_len, tlen,
625 src_maxburst,
626 src_addr_width);
627 ctcr &= ~STM32_MDMA_CTCR_SBURST_MASK;
628 ctcr |= STM32_MDMA_CTCR_SBURST((ilog2(src_best_burst)));
629
630 /* Set memory data size */
631 dst_addr_width = stm32_mdma_get_max_width(addr, buf_len, tlen);
632 chan->mem_width = dst_addr_width;
633 dst_bus_width = stm32_mdma_get_width(chan, dst_addr_width);
634 if (dst_bus_width < 0)
635 return dst_bus_width;
636 ctcr &= ~(STM32_MDMA_CTCR_DSIZE_MASK |
637 STM32_MDMA_CTCR_DINCOS_MASK);
638 ctcr |= STM32_MDMA_CTCR_DSIZE(dst_bus_width) |
639 STM32_MDMA_CTCR_DINCOS(dst_bus_width);
640
641 /* Set memory burst value */
642 dst_maxburst = STM32_MDMA_MAX_BUF_LEN / dst_addr_width;
643 dst_best_burst = stm32_mdma_get_best_burst(buf_len, tlen,
644 dst_maxburst,
645 dst_addr_width);
646 ctcr &= ~STM32_MDMA_CTCR_DBURST_MASK;
647 ctcr |= STM32_MDMA_CTCR_DBURST((ilog2(dst_best_burst)));
648
649 /* Select bus */
650 stm32_mdma_set_bus(dmadev, &ctbr, STM32_MDMA_CTBR_SBUS,
651 src_addr);
652
653 if (dst_bus_width != src_bus_width)
654 ctcr |= STM32_MDMA_CTCR_PKE;
655
656 /* Set source address */
657 stm32_mdma_write(dmadev, STM32_MDMA_CSAR(chan->id), src_addr);
658 break;
659
660 default:
661 dev_err(chan2dev(chan), "Dma direction is not supported\n");
662 return -EINVAL;
663 }
664
665 *mdma_ccr = ccr;
666 *mdma_ctcr = ctcr;
667 *mdma_ctbr = ctbr;
668
669 return 0;
670}
671
672static void stm32_mdma_dump_hwdesc(struct stm32_mdma_chan *chan,
673 struct stm32_mdma_desc_node *node)
674{
675 dev_dbg(chan2dev(chan), "hwdesc: %pad\n", &node->hwdesc_phys);
676 dev_dbg(chan2dev(chan), "CTCR: 0x%08x\n", node->hwdesc->ctcr);
677 dev_dbg(chan2dev(chan), "CBNDTR: 0x%08x\n", node->hwdesc->cbndtr);
678 dev_dbg(chan2dev(chan), "CSAR: 0x%08x\n", node->hwdesc->csar);
679 dev_dbg(chan2dev(chan), "CDAR: 0x%08x\n", node->hwdesc->cdar);
680 dev_dbg(chan2dev(chan), "CBRUR: 0x%08x\n", node->hwdesc->cbrur);
681 dev_dbg(chan2dev(chan), "CLAR: 0x%08x\n", node->hwdesc->clar);
682 dev_dbg(chan2dev(chan), "CTBR: 0x%08x\n", node->hwdesc->ctbr);
683 dev_dbg(chan2dev(chan), "CMAR: 0x%08x\n", node->hwdesc->cmar);
684 dev_dbg(chan2dev(chan), "CMDR: 0x%08x\n\n", node->hwdesc->cmdr);
685}
686
687static void stm32_mdma_setup_hwdesc(struct stm32_mdma_chan *chan,
688 struct stm32_mdma_desc *desc,
689 enum dma_transfer_direction dir, u32 count,
690 dma_addr_t src_addr, dma_addr_t dst_addr,
691 u32 len, u32 ctcr, u32 ctbr, bool is_last,
692 bool is_first, bool is_cyclic)
693{
694 struct stm32_mdma_chan_config *config = &chan->chan_config;
695 struct stm32_mdma_hwdesc *hwdesc;
696 u32 next = count + 1;
697
698 hwdesc = desc->node[count].hwdesc;
699 hwdesc->ctcr = ctcr;
700 hwdesc->cbndtr &= ~(STM32_MDMA_CBNDTR_BRC_MK |
701 STM32_MDMA_CBNDTR_BRDUM |
702 STM32_MDMA_CBNDTR_BRSUM |
703 STM32_MDMA_CBNDTR_BNDT_MASK);
704 hwdesc->cbndtr |= STM32_MDMA_CBNDTR_BNDT(len);
705 hwdesc->csar = src_addr;
706 hwdesc->cdar = dst_addr;
707 hwdesc->cbrur = 0;
708 hwdesc->ctbr = ctbr;
709 hwdesc->cmar = config->mask_addr;
710 hwdesc->cmdr = config->mask_data;
711
712 if (is_last) {
713 if (is_cyclic)
714 hwdesc->clar = desc->node[0].hwdesc_phys;
715 else
716 hwdesc->clar = 0;
717 } else {
718 hwdesc->clar = desc->node[next].hwdesc_phys;
719 }
720
721 stm32_mdma_dump_hwdesc(chan, &desc->node[count]);
722}
723
724static int stm32_mdma_setup_xfer(struct stm32_mdma_chan *chan,
725 struct stm32_mdma_desc *desc,
726 struct scatterlist *sgl, u32 sg_len,
727 enum dma_transfer_direction direction)
728{
729 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
730 struct dma_slave_config *dma_config = &chan->dma_config;
731 struct stm32_mdma_chan_config *chan_config = &chan->chan_config;
732 struct scatterlist *sg;
733 dma_addr_t src_addr, dst_addr;
734 u32 m2m_hw_period, ccr, ctcr, ctbr;
735 int i, ret = 0;
736
737 if (chan_config->m2m_hw)
738 m2m_hw_period = sg_dma_len(sgl);
739
740 for_each_sg(sgl, sg, sg_len, i) {
741 if (sg_dma_len(sg) > STM32_MDMA_MAX_BLOCK_LEN) {
742 dev_err(chan2dev(chan), "Invalid block len\n");
743 return -EINVAL;
744 }
745
746 if (direction == DMA_MEM_TO_DEV) {
747 src_addr = sg_dma_address(sg);
748 dst_addr = dma_config->dst_addr;
749 if (chan_config->m2m_hw && (i & 1))
750 dst_addr += m2m_hw_period;
751 ret = stm32_mdma_set_xfer_param(chan, direction, &ccr,
752 &ctcr, &ctbr, src_addr,
753 sg_dma_len(sg));
754 stm32_mdma_set_bus(dmadev, &ctbr, STM32_MDMA_CTBR_SBUS,
755 src_addr);
756 } else {
757 src_addr = dma_config->src_addr;
758 if (chan_config->m2m_hw && (i & 1))
759 src_addr += m2m_hw_period;
760 dst_addr = sg_dma_address(sg);
761 ret = stm32_mdma_set_xfer_param(chan, direction, &ccr,
762 &ctcr, &ctbr, dst_addr,
763 sg_dma_len(sg));
764 stm32_mdma_set_bus(dmadev, &ctbr, STM32_MDMA_CTBR_DBUS,
765 dst_addr);
766 }
767
768 if (ret < 0)
769 return ret;
770
771 stm32_mdma_setup_hwdesc(chan, desc, direction, i, src_addr,
772 dst_addr, sg_dma_len(sg), ctcr, ctbr,
773 i == sg_len - 1, i == 0, false);
774 }
775
776 /* Enable interrupts */
777 ccr &= ~STM32_MDMA_CCR_IRQ_MASK;
778 ccr |= STM32_MDMA_CCR_TEIE | STM32_MDMA_CCR_CTCIE;
779 desc->ccr = ccr;
780
781 return 0;
782}
783
784static struct dma_async_tx_descriptor *
785stm32_mdma_prep_slave_sg(struct dma_chan *c, struct scatterlist *sgl,
786 u32 sg_len, enum dma_transfer_direction direction,
787 unsigned long flags, void *context)
788{
789 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
790 struct stm32_mdma_chan_config *chan_config = &chan->chan_config;
791 struct stm32_mdma_desc *desc;
792 int i, ret;
793
794 /*
795 * Once DMA is in setup cyclic mode the channel we cannot assign this
796 * channel anymore. The DMA channel needs to be aborted or terminated
797 * for allowing another request.
798 */
799 if (chan->desc && chan->desc->cyclic) {
800 dev_err(chan2dev(chan),
801 "Request not allowed when dma in cyclic mode\n");
802 return NULL;
803 }
804
805 desc = stm32_mdma_alloc_desc(chan, sg_len);
806 if (!desc)
807 return NULL;
808
809 ret = stm32_mdma_setup_xfer(chan, desc, sgl, sg_len, direction);
810 if (ret < 0)
811 goto xfer_setup_err;
812
813 /*
814 * In case of M2M HW transfer triggered by STM32 DMA, we do not have to clear the
815 * transfer complete flag by hardware in order to let the CPU rearm the STM32 DMA
816 * with the next sg element and update some data in dmaengine framework.
817 */
818 if (chan_config->m2m_hw && direction == DMA_MEM_TO_DEV) {
819 struct stm32_mdma_hwdesc *hwdesc;
820
821 for (i = 0; i < sg_len; i++) {
822 hwdesc = desc->node[i].hwdesc;
823 hwdesc->cmar = 0;
824 hwdesc->cmdr = 0;
825 }
826 }
827
828 desc->cyclic = false;
829
830 return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
831
832xfer_setup_err:
833 for (i = 0; i < desc->count; i++)
834 dma_pool_free(chan->desc_pool, desc->node[i].hwdesc,
835 desc->node[i].hwdesc_phys);
836 kfree(desc);
837 return NULL;
838}
839
840static struct dma_async_tx_descriptor *
841stm32_mdma_prep_dma_cyclic(struct dma_chan *c, dma_addr_t buf_addr,
842 size_t buf_len, size_t period_len,
843 enum dma_transfer_direction direction,
844 unsigned long flags)
845{
846 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
847 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
848 struct dma_slave_config *dma_config = &chan->dma_config;
849 struct stm32_mdma_chan_config *chan_config = &chan->chan_config;
850 struct stm32_mdma_desc *desc;
851 dma_addr_t src_addr, dst_addr;
852 u32 ccr, ctcr, ctbr, count;
853 int i, ret;
854
855 /*
856 * Once DMA is in setup cyclic mode the channel we cannot assign this
857 * channel anymore. The DMA channel needs to be aborted or terminated
858 * for allowing another request.
859 */
860 if (chan->desc && chan->desc->cyclic) {
861 dev_err(chan2dev(chan),
862 "Request not allowed when dma in cyclic mode\n");
863 return NULL;
864 }
865
866 if (!buf_len || !period_len || period_len > STM32_MDMA_MAX_BLOCK_LEN) {
867 dev_err(chan2dev(chan), "Invalid buffer/period len\n");
868 return NULL;
869 }
870
871 if (buf_len % period_len) {
872 dev_err(chan2dev(chan), "buf_len not multiple of period_len\n");
873 return NULL;
874 }
875
876 count = buf_len / period_len;
877
878 desc = stm32_mdma_alloc_desc(chan, count);
879 if (!desc)
880 return NULL;
881
882 /* Select bus */
883 if (direction == DMA_MEM_TO_DEV) {
884 src_addr = buf_addr;
885 ret = stm32_mdma_set_xfer_param(chan, direction, &ccr, &ctcr,
886 &ctbr, src_addr, period_len);
887 stm32_mdma_set_bus(dmadev, &ctbr, STM32_MDMA_CTBR_SBUS,
888 src_addr);
889 } else {
890 dst_addr = buf_addr;
891 ret = stm32_mdma_set_xfer_param(chan, direction, &ccr, &ctcr,
892 &ctbr, dst_addr, period_len);
893 stm32_mdma_set_bus(dmadev, &ctbr, STM32_MDMA_CTBR_DBUS,
894 dst_addr);
895 }
896
897 if (ret < 0)
898 goto xfer_setup_err;
899
900 /* Enable interrupts */
901 ccr &= ~STM32_MDMA_CCR_IRQ_MASK;
902 ccr |= STM32_MDMA_CCR_TEIE | STM32_MDMA_CCR_CTCIE | STM32_MDMA_CCR_BTIE;
903 desc->ccr = ccr;
904
905 /* Configure hwdesc list */
906 for (i = 0; i < count; i++) {
907 if (direction == DMA_MEM_TO_DEV) {
908 src_addr = buf_addr + i * period_len;
909 dst_addr = dma_config->dst_addr;
910 if (chan_config->m2m_hw && (i & 1))
911 dst_addr += period_len;
912 } else {
913 src_addr = dma_config->src_addr;
914 if (chan_config->m2m_hw && (i & 1))
915 src_addr += period_len;
916 dst_addr = buf_addr + i * period_len;
917 }
918
919 stm32_mdma_setup_hwdesc(chan, desc, direction, i, src_addr,
920 dst_addr, period_len, ctcr, ctbr,
921 i == count - 1, i == 0, true);
922 }
923
924 desc->cyclic = true;
925
926 return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
927
928xfer_setup_err:
929 for (i = 0; i < desc->count; i++)
930 dma_pool_free(chan->desc_pool, desc->node[i].hwdesc,
931 desc->node[i].hwdesc_phys);
932 kfree(desc);
933 return NULL;
934}
935
936static struct dma_async_tx_descriptor *
937stm32_mdma_prep_dma_memcpy(struct dma_chan *c, dma_addr_t dest, dma_addr_t src,
938 size_t len, unsigned long flags)
939{
940 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
941 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
942 enum dma_slave_buswidth max_width;
943 struct stm32_mdma_desc *desc;
944 struct stm32_mdma_hwdesc *hwdesc;
945 u32 ccr, ctcr, ctbr, cbndtr, count, max_burst, mdma_burst;
946 u32 best_burst, tlen;
947 size_t xfer_count, offset;
948 int src_bus_width, dst_bus_width;
949 int i;
950
951 /*
952 * Once DMA is in setup cyclic mode the channel we cannot assign this
953 * channel anymore. The DMA channel needs to be aborted or terminated
954 * to allow another request
955 */
956 if (chan->desc && chan->desc->cyclic) {
957 dev_err(chan2dev(chan),
958 "Request not allowed when dma in cyclic mode\n");
959 return NULL;
960 }
961
962 count = DIV_ROUND_UP(len, STM32_MDMA_MAX_BLOCK_LEN);
963 desc = stm32_mdma_alloc_desc(chan, count);
964 if (!desc)
965 return NULL;
966
967 ccr = stm32_mdma_read(dmadev, STM32_MDMA_CCR(chan->id)) & ~STM32_MDMA_CCR_EN;
968 ctcr = stm32_mdma_read(dmadev, STM32_MDMA_CTCR(chan->id));
969 ctbr = stm32_mdma_read(dmadev, STM32_MDMA_CTBR(chan->id));
970 cbndtr = stm32_mdma_read(dmadev, STM32_MDMA_CBNDTR(chan->id));
971
972 /* Enable sw req, some interrupts and clear other bits */
973 ccr &= ~(STM32_MDMA_CCR_WEX | STM32_MDMA_CCR_HEX |
974 STM32_MDMA_CCR_BEX | STM32_MDMA_CCR_PL_MASK |
975 STM32_MDMA_CCR_IRQ_MASK);
976 ccr |= STM32_MDMA_CCR_TEIE;
977
978 /* Enable SW request mode, dest/src inc and clear other bits */
979 ctcr &= ~(STM32_MDMA_CTCR_BWM | STM32_MDMA_CTCR_TRGM_MSK |
980 STM32_MDMA_CTCR_PAM_MASK | STM32_MDMA_CTCR_PKE |
981 STM32_MDMA_CTCR_TLEN_MSK | STM32_MDMA_CTCR_DBURST_MASK |
982 STM32_MDMA_CTCR_SBURST_MASK | STM32_MDMA_CTCR_DINCOS_MASK |
983 STM32_MDMA_CTCR_SINCOS_MASK | STM32_MDMA_CTCR_DSIZE_MASK |
984 STM32_MDMA_CTCR_SSIZE_MASK | STM32_MDMA_CTCR_DINC_MASK |
985 STM32_MDMA_CTCR_SINC_MASK);
986 ctcr |= STM32_MDMA_CTCR_SWRM | STM32_MDMA_CTCR_SINC(STM32_MDMA_INC) |
987 STM32_MDMA_CTCR_DINC(STM32_MDMA_INC);
988
989 /* Reset HW request */
990 ctbr &= ~STM32_MDMA_CTBR_TSEL_MASK;
991
992 /* Select bus */
993 stm32_mdma_set_bus(dmadev, &ctbr, STM32_MDMA_CTBR_SBUS, src);
994 stm32_mdma_set_bus(dmadev, &ctbr, STM32_MDMA_CTBR_DBUS, dest);
995
996 /* Clear CBNDTR registers */
997 cbndtr &= ~(STM32_MDMA_CBNDTR_BRC_MK | STM32_MDMA_CBNDTR_BRDUM |
998 STM32_MDMA_CBNDTR_BRSUM | STM32_MDMA_CBNDTR_BNDT_MASK);
999
1000 if (len <= STM32_MDMA_MAX_BLOCK_LEN) {
1001 cbndtr |= STM32_MDMA_CBNDTR_BNDT(len);
1002 if (len <= STM32_MDMA_MAX_BUF_LEN) {
1003 /* Setup a buffer transfer */
1004 ccr |= STM32_MDMA_CCR_TCIE | STM32_MDMA_CCR_CTCIE;
1005 ctcr |= STM32_MDMA_CTCR_TRGM(STM32_MDMA_BUFFER);
1006 } else {
1007 /* Setup a block transfer */
1008 ccr |= STM32_MDMA_CCR_BTIE | STM32_MDMA_CCR_CTCIE;
1009 ctcr |= STM32_MDMA_CTCR_TRGM(STM32_MDMA_BLOCK);
1010 }
1011
1012 tlen = STM32_MDMA_MAX_BUF_LEN;
1013 ctcr |= STM32_MDMA_CTCR_TLEN((tlen - 1));
1014
1015 /* Set source best burst size */
1016 max_width = stm32_mdma_get_max_width(src, len, tlen);
1017 src_bus_width = stm32_mdma_get_width(chan, max_width);
1018
1019 max_burst = tlen / max_width;
1020 best_burst = stm32_mdma_get_best_burst(len, tlen, max_burst,
1021 max_width);
1022 mdma_burst = ilog2(best_burst);
1023
1024 ctcr |= STM32_MDMA_CTCR_SBURST(mdma_burst) |
1025 STM32_MDMA_CTCR_SSIZE(src_bus_width) |
1026 STM32_MDMA_CTCR_SINCOS(src_bus_width);
1027
1028 /* Set destination best burst size */
1029 max_width = stm32_mdma_get_max_width(dest, len, tlen);
1030 dst_bus_width = stm32_mdma_get_width(chan, max_width);
1031
1032 max_burst = tlen / max_width;
1033 best_burst = stm32_mdma_get_best_burst(len, tlen, max_burst,
1034 max_width);
1035 mdma_burst = ilog2(best_burst);
1036
1037 ctcr |= STM32_MDMA_CTCR_DBURST(mdma_burst) |
1038 STM32_MDMA_CTCR_DSIZE(dst_bus_width) |
1039 STM32_MDMA_CTCR_DINCOS(dst_bus_width);
1040
1041 if (dst_bus_width != src_bus_width)
1042 ctcr |= STM32_MDMA_CTCR_PKE;
1043
1044 /* Prepare hardware descriptor */
1045 hwdesc = desc->node[0].hwdesc;
1046 hwdesc->ctcr = ctcr;
1047 hwdesc->cbndtr = cbndtr;
1048 hwdesc->csar = src;
1049 hwdesc->cdar = dest;
1050 hwdesc->cbrur = 0;
1051 hwdesc->clar = 0;
1052 hwdesc->ctbr = ctbr;
1053 hwdesc->cmar = 0;
1054 hwdesc->cmdr = 0;
1055
1056 stm32_mdma_dump_hwdesc(chan, &desc->node[0]);
1057 } else {
1058 /* Setup a LLI transfer */
1059 ctcr |= STM32_MDMA_CTCR_TRGM(STM32_MDMA_LINKED_LIST) |
1060 STM32_MDMA_CTCR_TLEN((STM32_MDMA_MAX_BUF_LEN - 1));
1061 ccr |= STM32_MDMA_CCR_BTIE | STM32_MDMA_CCR_CTCIE;
1062 tlen = STM32_MDMA_MAX_BUF_LEN;
1063
1064 for (i = 0, offset = 0; offset < len;
1065 i++, offset += xfer_count) {
1066 xfer_count = min_t(size_t, len - offset,
1067 STM32_MDMA_MAX_BLOCK_LEN);
1068
1069 /* Set source best burst size */
1070 max_width = stm32_mdma_get_max_width(src, len, tlen);
1071 src_bus_width = stm32_mdma_get_width(chan, max_width);
1072
1073 max_burst = tlen / max_width;
1074 best_burst = stm32_mdma_get_best_burst(len, tlen,
1075 max_burst,
1076 max_width);
1077 mdma_burst = ilog2(best_burst);
1078
1079 ctcr |= STM32_MDMA_CTCR_SBURST(mdma_burst) |
1080 STM32_MDMA_CTCR_SSIZE(src_bus_width) |
1081 STM32_MDMA_CTCR_SINCOS(src_bus_width);
1082
1083 /* Set destination best burst size */
1084 max_width = stm32_mdma_get_max_width(dest, len, tlen);
1085 dst_bus_width = stm32_mdma_get_width(chan, max_width);
1086
1087 max_burst = tlen / max_width;
1088 best_burst = stm32_mdma_get_best_burst(len, tlen,
1089 max_burst,
1090 max_width);
1091 mdma_burst = ilog2(best_burst);
1092
1093 ctcr |= STM32_MDMA_CTCR_DBURST(mdma_burst) |
1094 STM32_MDMA_CTCR_DSIZE(dst_bus_width) |
1095 STM32_MDMA_CTCR_DINCOS(dst_bus_width);
1096
1097 if (dst_bus_width != src_bus_width)
1098 ctcr |= STM32_MDMA_CTCR_PKE;
1099
1100 /* Prepare hardware descriptor */
1101 stm32_mdma_setup_hwdesc(chan, desc, DMA_MEM_TO_MEM, i,
1102 src + offset, dest + offset,
1103 xfer_count, ctcr, ctbr,
1104 i == count - 1, i == 0, false);
1105 }
1106 }
1107
1108 desc->ccr = ccr;
1109
1110 desc->cyclic = false;
1111
1112 return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
1113}
1114
1115static void stm32_mdma_dump_reg(struct stm32_mdma_chan *chan)
1116{
1117 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
1118
1119 dev_dbg(chan2dev(chan), "CCR: 0x%08x\n",
1120 stm32_mdma_read(dmadev, STM32_MDMA_CCR(chan->id)));
1121 dev_dbg(chan2dev(chan), "CTCR: 0x%08x\n",
1122 stm32_mdma_read(dmadev, STM32_MDMA_CTCR(chan->id)));
1123 dev_dbg(chan2dev(chan), "CBNDTR: 0x%08x\n",
1124 stm32_mdma_read(dmadev, STM32_MDMA_CBNDTR(chan->id)));
1125 dev_dbg(chan2dev(chan), "CSAR: 0x%08x\n",
1126 stm32_mdma_read(dmadev, STM32_MDMA_CSAR(chan->id)));
1127 dev_dbg(chan2dev(chan), "CDAR: 0x%08x\n",
1128 stm32_mdma_read(dmadev, STM32_MDMA_CDAR(chan->id)));
1129 dev_dbg(chan2dev(chan), "CBRUR: 0x%08x\n",
1130 stm32_mdma_read(dmadev, STM32_MDMA_CBRUR(chan->id)));
1131 dev_dbg(chan2dev(chan), "CLAR: 0x%08x\n",
1132 stm32_mdma_read(dmadev, STM32_MDMA_CLAR(chan->id)));
1133 dev_dbg(chan2dev(chan), "CTBR: 0x%08x\n",
1134 stm32_mdma_read(dmadev, STM32_MDMA_CTBR(chan->id)));
1135 dev_dbg(chan2dev(chan), "CMAR: 0x%08x\n",
1136 stm32_mdma_read(dmadev, STM32_MDMA_CMAR(chan->id)));
1137 dev_dbg(chan2dev(chan), "CMDR: 0x%08x\n",
1138 stm32_mdma_read(dmadev, STM32_MDMA_CMDR(chan->id)));
1139}
1140
1141static void stm32_mdma_start_transfer(struct stm32_mdma_chan *chan)
1142{
1143 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
1144 struct virt_dma_desc *vdesc;
1145 struct stm32_mdma_hwdesc *hwdesc;
1146 u32 id = chan->id;
1147 u32 status, reg;
1148
1149 vdesc = vchan_next_desc(&chan->vchan);
1150 if (!vdesc) {
1151 chan->desc = NULL;
1152 return;
1153 }
1154
1155 list_del(&vdesc->node);
1156
1157 chan->desc = to_stm32_mdma_desc(vdesc);
1158 hwdesc = chan->desc->node[0].hwdesc;
1159 chan->curr_hwdesc = 0;
1160
1161 stm32_mdma_write(dmadev, STM32_MDMA_CCR(id), chan->desc->ccr);
1162 stm32_mdma_write(dmadev, STM32_MDMA_CTCR(id), hwdesc->ctcr);
1163 stm32_mdma_write(dmadev, STM32_MDMA_CBNDTR(id), hwdesc->cbndtr);
1164 stm32_mdma_write(dmadev, STM32_MDMA_CSAR(id), hwdesc->csar);
1165 stm32_mdma_write(dmadev, STM32_MDMA_CDAR(id), hwdesc->cdar);
1166 stm32_mdma_write(dmadev, STM32_MDMA_CBRUR(id), hwdesc->cbrur);
1167 stm32_mdma_write(dmadev, STM32_MDMA_CLAR(id), hwdesc->clar);
1168 stm32_mdma_write(dmadev, STM32_MDMA_CTBR(id), hwdesc->ctbr);
1169 stm32_mdma_write(dmadev, STM32_MDMA_CMAR(id), hwdesc->cmar);
1170 stm32_mdma_write(dmadev, STM32_MDMA_CMDR(id), hwdesc->cmdr);
1171
1172 /* Clear interrupt status if it is there */
1173 status = stm32_mdma_read(dmadev, STM32_MDMA_CISR(id));
1174 if (status)
1175 stm32_mdma_set_bits(dmadev, STM32_MDMA_CIFCR(id), status);
1176
1177 stm32_mdma_dump_reg(chan);
1178
1179 /* Start DMA */
1180 stm32_mdma_set_bits(dmadev, STM32_MDMA_CCR(id), STM32_MDMA_CCR_EN);
1181
1182 /* Set SW request in case of MEM2MEM transfer */
1183 if (hwdesc->ctcr & STM32_MDMA_CTCR_SWRM) {
1184 reg = STM32_MDMA_CCR(id);
1185 stm32_mdma_set_bits(dmadev, reg, STM32_MDMA_CCR_SWRQ);
1186 }
1187
1188 chan->busy = true;
1189
1190 dev_dbg(chan2dev(chan), "vchan %pK: started\n", &chan->vchan);
1191}
1192
1193static void stm32_mdma_issue_pending(struct dma_chan *c)
1194{
1195 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
1196 unsigned long flags;
1197
1198 spin_lock_irqsave(&chan->vchan.lock, flags);
1199
1200 if (!vchan_issue_pending(&chan->vchan))
1201 goto end;
1202
1203 dev_dbg(chan2dev(chan), "vchan %pK: issued\n", &chan->vchan);
1204
1205 if (!chan->desc && !chan->busy)
1206 stm32_mdma_start_transfer(chan);
1207
1208end:
1209 spin_unlock_irqrestore(&chan->vchan.lock, flags);
1210}
1211
1212static int stm32_mdma_pause(struct dma_chan *c)
1213{
1214 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
1215 unsigned long flags;
1216 int ret;
1217
1218 spin_lock_irqsave(&chan->vchan.lock, flags);
1219 ret = stm32_mdma_disable_chan(chan);
1220 spin_unlock_irqrestore(&chan->vchan.lock, flags);
1221
1222 if (!ret)
1223 dev_dbg(chan2dev(chan), "vchan %pK: pause\n", &chan->vchan);
1224
1225 return ret;
1226}
1227
1228static int stm32_mdma_resume(struct dma_chan *c)
1229{
1230 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
1231 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
1232 struct stm32_mdma_hwdesc *hwdesc;
1233 unsigned long flags;
1234 u32 status, reg;
1235
1236 /* Transfer can be terminated */
1237 if (!chan->desc || (stm32_mdma_read(dmadev, STM32_MDMA_CCR(chan->id)) & STM32_MDMA_CCR_EN))
1238 return -EPERM;
1239
1240 hwdesc = chan->desc->node[chan->curr_hwdesc].hwdesc;
1241
1242 spin_lock_irqsave(&chan->vchan.lock, flags);
1243
1244 /* Re-configure control register */
1245 stm32_mdma_write(dmadev, STM32_MDMA_CCR(chan->id), chan->desc->ccr);
1246
1247 /* Clear interrupt status if it is there */
1248 status = stm32_mdma_read(dmadev, STM32_MDMA_CISR(chan->id));
1249 if (status)
1250 stm32_mdma_set_bits(dmadev, STM32_MDMA_CIFCR(chan->id), status);
1251
1252 stm32_mdma_dump_reg(chan);
1253
1254 /* Re-start DMA */
1255 reg = STM32_MDMA_CCR(chan->id);
1256 stm32_mdma_set_bits(dmadev, reg, STM32_MDMA_CCR_EN);
1257
1258 /* Set SW request in case of MEM2MEM transfer */
1259 if (hwdesc->ctcr & STM32_MDMA_CTCR_SWRM)
1260 stm32_mdma_set_bits(dmadev, reg, STM32_MDMA_CCR_SWRQ);
1261
1262 spin_unlock_irqrestore(&chan->vchan.lock, flags);
1263
1264 dev_dbg(chan2dev(chan), "vchan %pK: resume\n", &chan->vchan);
1265
1266 return 0;
1267}
1268
1269static int stm32_mdma_terminate_all(struct dma_chan *c)
1270{
1271 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
1272 unsigned long flags;
1273 LIST_HEAD(head);
1274
1275 spin_lock_irqsave(&chan->vchan.lock, flags);
1276 if (chan->desc) {
1277 vchan_terminate_vdesc(&chan->desc->vdesc);
1278 if (chan->busy)
1279 stm32_mdma_stop(chan);
1280 chan->desc = NULL;
1281 }
1282 vchan_get_all_descriptors(&chan->vchan, &head);
1283 spin_unlock_irqrestore(&chan->vchan.lock, flags);
1284
1285 vchan_dma_desc_free_list(&chan->vchan, &head);
1286
1287 return 0;
1288}
1289
1290static void stm32_mdma_synchronize(struct dma_chan *c)
1291{
1292 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
1293
1294 vchan_synchronize(&chan->vchan);
1295}
1296
1297static int stm32_mdma_slave_config(struct dma_chan *c,
1298 struct dma_slave_config *config)
1299{
1300 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
1301
1302 memcpy(&chan->dma_config, config, sizeof(*config));
1303
1304 /* Check if user is requesting STM32 DMA to trigger MDMA */
1305 if (config->peripheral_size) {
1306 struct stm32_mdma_dma_config *mdma_config;
1307
1308 mdma_config = (struct stm32_mdma_dma_config *)chan->dma_config.peripheral_config;
1309 chan->chan_config.request = mdma_config->request;
1310 chan->chan_config.mask_addr = mdma_config->cmar;
1311 chan->chan_config.mask_data = mdma_config->cmdr;
1312 chan->chan_config.m2m_hw = true;
1313 }
1314
1315 return 0;
1316}
1317
1318static size_t stm32_mdma_desc_residue(struct stm32_mdma_chan *chan,
1319 struct stm32_mdma_desc *desc,
1320 u32 curr_hwdesc,
1321 struct dma_tx_state *state)
1322{
1323 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
1324 struct stm32_mdma_hwdesc *hwdesc;
1325 u32 cisr, clar, cbndtr, residue, modulo, burst_size;
1326 int i;
1327
1328 cisr = stm32_mdma_read(dmadev, STM32_MDMA_CISR(chan->id));
1329
1330 residue = 0;
1331 /* Get the next hw descriptor to process from current transfer */
1332 clar = stm32_mdma_read(dmadev, STM32_MDMA_CLAR(chan->id));
1333 for (i = desc->count - 1; i >= 0; i--) {
1334 hwdesc = desc->node[i].hwdesc;
1335
1336 if (hwdesc->clar == clar)
1337 break;/* Current transfer found, stop cumulating */
1338
1339 /* Cumulate residue of unprocessed hw descriptors */
1340 residue += STM32_MDMA_CBNDTR_BNDT(hwdesc->cbndtr);
1341 }
1342 cbndtr = stm32_mdma_read(dmadev, STM32_MDMA_CBNDTR(chan->id));
1343 residue += cbndtr & STM32_MDMA_CBNDTR_BNDT_MASK;
1344
1345 state->in_flight_bytes = 0;
1346 if (chan->chan_config.m2m_hw && (cisr & STM32_MDMA_CISR_CRQA))
1347 state->in_flight_bytes = cbndtr & STM32_MDMA_CBNDTR_BNDT_MASK;
1348
1349 if (!chan->mem_burst)
1350 return residue;
1351
1352 burst_size = chan->mem_burst * chan->mem_width;
1353 modulo = residue % burst_size;
1354 if (modulo)
1355 residue = residue - modulo + burst_size;
1356
1357 return residue;
1358}
1359
1360static enum dma_status stm32_mdma_tx_status(struct dma_chan *c,
1361 dma_cookie_t cookie,
1362 struct dma_tx_state *state)
1363{
1364 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
1365 struct virt_dma_desc *vdesc;
1366 enum dma_status status;
1367 unsigned long flags;
1368 u32 residue = 0;
1369
1370 status = dma_cookie_status(c, cookie, state);
1371 if ((status == DMA_COMPLETE) || (!state))
1372 return status;
1373
1374 spin_lock_irqsave(&chan->vchan.lock, flags);
1375
1376 vdesc = vchan_find_desc(&chan->vchan, cookie);
1377 if (chan->desc && cookie == chan->desc->vdesc.tx.cookie)
1378 residue = stm32_mdma_desc_residue(chan, chan->desc, chan->curr_hwdesc, state);
1379 else if (vdesc)
1380 residue = stm32_mdma_desc_residue(chan, to_stm32_mdma_desc(vdesc), 0, state);
1381
1382 dma_set_residue(state, residue);
1383
1384 spin_unlock_irqrestore(&chan->vchan.lock, flags);
1385
1386 return status;
1387}
1388
1389static void stm32_mdma_xfer_end(struct stm32_mdma_chan *chan)
1390{
1391 vchan_cookie_complete(&chan->desc->vdesc);
1392 chan->desc = NULL;
1393 chan->busy = false;
1394
1395 /* Start the next transfer if this driver has a next desc */
1396 stm32_mdma_start_transfer(chan);
1397}
1398
1399static irqreturn_t stm32_mdma_irq_handler(int irq, void *devid)
1400{
1401 struct stm32_mdma_device *dmadev = devid;
1402 struct stm32_mdma_chan *chan;
1403 u32 reg, id, ccr, ien, status;
1404
1405 /* Find out which channel generates the interrupt */
1406 status = readl_relaxed(dmadev->base + STM32_MDMA_GISR0);
1407 if (!status) {
1408 dev_dbg(mdma2dev(dmadev), "spurious it\n");
1409 return IRQ_NONE;
1410 }
1411 id = __ffs(status);
1412 chan = &dmadev->chan[id];
1413
1414 /* Handle interrupt for the channel */
1415 spin_lock(&chan->vchan.lock);
1416 status = stm32_mdma_read(dmadev, STM32_MDMA_CISR(id));
1417 /* Mask Channel ReQuest Active bit which can be set in case of MEM2MEM */
1418 status &= ~STM32_MDMA_CISR_CRQA;
1419 ccr = stm32_mdma_read(dmadev, STM32_MDMA_CCR(id));
1420 ien = (ccr & STM32_MDMA_CCR_IRQ_MASK) >> 1;
1421
1422 if (!(status & ien)) {
1423 spin_unlock(&chan->vchan.lock);
1424 if (chan->busy)
1425 dev_warn(chan2dev(chan),
1426 "spurious it (status=0x%04x, ien=0x%04x)\n", status, ien);
1427 else
1428 dev_dbg(chan2dev(chan),
1429 "spurious it (status=0x%04x, ien=0x%04x)\n", status, ien);
1430 return IRQ_NONE;
1431 }
1432
1433 reg = STM32_MDMA_CIFCR(id);
1434
1435 if (status & STM32_MDMA_CISR_TEIF) {
1436 dev_err(chan2dev(chan), "Transfer Err: stat=0x%08x\n",
1437 readl_relaxed(dmadev->base + STM32_MDMA_CESR(id)));
1438 stm32_mdma_set_bits(dmadev, reg, STM32_MDMA_CIFCR_CTEIF);
1439 status &= ~STM32_MDMA_CISR_TEIF;
1440 }
1441
1442 if (status & STM32_MDMA_CISR_CTCIF) {
1443 stm32_mdma_set_bits(dmadev, reg, STM32_MDMA_CIFCR_CCTCIF);
1444 status &= ~STM32_MDMA_CISR_CTCIF;
1445 stm32_mdma_xfer_end(chan);
1446 }
1447
1448 if (status & STM32_MDMA_CISR_BRTIF) {
1449 stm32_mdma_set_bits(dmadev, reg, STM32_MDMA_CIFCR_CBRTIF);
1450 status &= ~STM32_MDMA_CISR_BRTIF;
1451 }
1452
1453 if (status & STM32_MDMA_CISR_BTIF) {
1454 stm32_mdma_set_bits(dmadev, reg, STM32_MDMA_CIFCR_CBTIF);
1455 status &= ~STM32_MDMA_CISR_BTIF;
1456 chan->curr_hwdesc++;
1457 if (chan->desc && chan->desc->cyclic) {
1458 if (chan->curr_hwdesc == chan->desc->count)
1459 chan->curr_hwdesc = 0;
1460 vchan_cyclic_callback(&chan->desc->vdesc);
1461 }
1462 }
1463
1464 if (status & STM32_MDMA_CISR_TCIF) {
1465 stm32_mdma_set_bits(dmadev, reg, STM32_MDMA_CIFCR_CLTCIF);
1466 status &= ~STM32_MDMA_CISR_TCIF;
1467 }
1468
1469 if (status) {
1470 stm32_mdma_set_bits(dmadev, reg, status);
1471 dev_err(chan2dev(chan), "DMA error: status=0x%08x\n", status);
1472 if (!(ccr & STM32_MDMA_CCR_EN))
1473 dev_err(chan2dev(chan), "chan disabled by HW\n");
1474 }
1475
1476 spin_unlock(&chan->vchan.lock);
1477
1478 return IRQ_HANDLED;
1479}
1480
1481static int stm32_mdma_alloc_chan_resources(struct dma_chan *c)
1482{
1483 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
1484 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
1485 int ret;
1486
1487 chan->desc_pool = dmam_pool_create(dev_name(&c->dev->device),
1488 c->device->dev,
1489 sizeof(struct stm32_mdma_hwdesc),
1490 __alignof__(struct stm32_mdma_hwdesc),
1491 0);
1492 if (!chan->desc_pool) {
1493 dev_err(chan2dev(chan), "failed to allocate descriptor pool\n");
1494 return -ENOMEM;
1495 }
1496
1497 ret = pm_runtime_resume_and_get(dmadev->ddev.dev);
1498 if (ret < 0)
1499 return ret;
1500
1501 ret = stm32_mdma_disable_chan(chan);
1502 if (ret < 0)
1503 pm_runtime_put(dmadev->ddev.dev);
1504
1505 return ret;
1506}
1507
1508static void stm32_mdma_free_chan_resources(struct dma_chan *c)
1509{
1510 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
1511 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
1512 unsigned long flags;
1513
1514 dev_dbg(chan2dev(chan), "Freeing channel %d\n", chan->id);
1515
1516 if (chan->busy) {
1517 spin_lock_irqsave(&chan->vchan.lock, flags);
1518 stm32_mdma_stop(chan);
1519 chan->desc = NULL;
1520 spin_unlock_irqrestore(&chan->vchan.lock, flags);
1521 }
1522
1523 pm_runtime_put(dmadev->ddev.dev);
1524 vchan_free_chan_resources(to_virt_chan(c));
1525 dmam_pool_destroy(chan->desc_pool);
1526 chan->desc_pool = NULL;
1527}
1528
1529static bool stm32_mdma_filter_fn(struct dma_chan *c, void *fn_param)
1530{
1531 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
1532 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
1533
1534 /* Check if chan is marked Secure */
1535 if (dmadev->chan_reserved & BIT(chan->id))
1536 return false;
1537
1538 return true;
1539}
1540
1541static struct dma_chan *stm32_mdma_of_xlate(struct of_phandle_args *dma_spec,
1542 struct of_dma *ofdma)
1543{
1544 struct stm32_mdma_device *dmadev = ofdma->of_dma_data;
1545 dma_cap_mask_t mask = dmadev->ddev.cap_mask;
1546 struct stm32_mdma_chan *chan;
1547 struct dma_chan *c;
1548 struct stm32_mdma_chan_config config;
1549
1550 if (dma_spec->args_count < 5) {
1551 dev_err(mdma2dev(dmadev), "Bad number of args\n");
1552 return NULL;
1553 }
1554
1555 memset(&config, 0, sizeof(config));
1556 config.request = dma_spec->args[0];
1557 config.priority_level = dma_spec->args[1];
1558 config.transfer_config = dma_spec->args[2];
1559 config.mask_addr = dma_spec->args[3];
1560 config.mask_data = dma_spec->args[4];
1561
1562 if (config.request >= dmadev->nr_requests) {
1563 dev_err(mdma2dev(dmadev), "Bad request line\n");
1564 return NULL;
1565 }
1566
1567 if (config.priority_level > STM32_MDMA_VERY_HIGH_PRIORITY) {
1568 dev_err(mdma2dev(dmadev), "Priority level not supported\n");
1569 return NULL;
1570 }
1571
1572 c = __dma_request_channel(&mask, stm32_mdma_filter_fn, &config, ofdma->of_node);
1573 if (!c) {
1574 dev_err(mdma2dev(dmadev), "No more channels available\n");
1575 return NULL;
1576 }
1577
1578 chan = to_stm32_mdma_chan(c);
1579 chan->chan_config = config;
1580
1581 return c;
1582}
1583
1584static const struct of_device_id stm32_mdma_of_match[] = {
1585 { .compatible = "st,stm32h7-mdma", },
1586 { /* sentinel */ },
1587};
1588MODULE_DEVICE_TABLE(of, stm32_mdma_of_match);
1589
1590static int stm32_mdma_probe(struct platform_device *pdev)
1591{
1592 struct stm32_mdma_chan *chan;
1593 struct stm32_mdma_device *dmadev;
1594 struct dma_device *dd;
1595 struct device_node *of_node;
1596 struct reset_control *rst;
1597 u32 nr_channels, nr_requests;
1598 int i, count, ret;
1599
1600 of_node = pdev->dev.of_node;
1601 if (!of_node)
1602 return -ENODEV;
1603
1604 ret = device_property_read_u32(&pdev->dev, "dma-channels",
1605 &nr_channels);
1606 if (ret) {
1607 nr_channels = STM32_MDMA_MAX_CHANNELS;
1608 dev_warn(&pdev->dev, "MDMA defaulting on %i channels\n",
1609 nr_channels);
1610 }
1611
1612 ret = device_property_read_u32(&pdev->dev, "dma-requests",
1613 &nr_requests);
1614 if (ret) {
1615 nr_requests = STM32_MDMA_MAX_REQUESTS;
1616 dev_warn(&pdev->dev, "MDMA defaulting on %i request lines\n",
1617 nr_requests);
1618 }
1619
1620 count = device_property_count_u32(&pdev->dev, "st,ahb-addr-masks");
1621 if (count < 0)
1622 count = 0;
1623
1624 dmadev = devm_kzalloc(&pdev->dev,
1625 struct_size(dmadev, ahb_addr_masks, count),
1626 GFP_KERNEL);
1627 if (!dmadev)
1628 return -ENOMEM;
1629 dmadev->nr_ahb_addr_masks = count;
1630
1631 dmadev->nr_channels = nr_channels;
1632 dmadev->nr_requests = nr_requests;
1633 device_property_read_u32_array(&pdev->dev, "st,ahb-addr-masks",
1634 dmadev->ahb_addr_masks,
1635 count);
1636
1637 dmadev->base = devm_platform_ioremap_resource(pdev, 0);
1638 if (IS_ERR(dmadev->base))
1639 return PTR_ERR(dmadev->base);
1640
1641 dmadev->clk = devm_clk_get(&pdev->dev, NULL);
1642 if (IS_ERR(dmadev->clk))
1643 return dev_err_probe(&pdev->dev, PTR_ERR(dmadev->clk),
1644 "Missing clock controller\n");
1645
1646 ret = clk_prepare_enable(dmadev->clk);
1647 if (ret < 0) {
1648 dev_err(&pdev->dev, "clk_prep_enable error: %d\n", ret);
1649 return ret;
1650 }
1651
1652 rst = devm_reset_control_get(&pdev->dev, NULL);
1653 if (IS_ERR(rst)) {
1654 ret = PTR_ERR(rst);
1655 if (ret == -EPROBE_DEFER)
1656 goto err_clk;
1657 } else {
1658 reset_control_assert(rst);
1659 udelay(2);
1660 reset_control_deassert(rst);
1661 }
1662
1663 dd = &dmadev->ddev;
1664 dma_cap_set(DMA_SLAVE, dd->cap_mask);
1665 dma_cap_set(DMA_PRIVATE, dd->cap_mask);
1666 dma_cap_set(DMA_CYCLIC, dd->cap_mask);
1667 dma_cap_set(DMA_MEMCPY, dd->cap_mask);
1668 dd->device_alloc_chan_resources = stm32_mdma_alloc_chan_resources;
1669 dd->device_free_chan_resources = stm32_mdma_free_chan_resources;
1670 dd->device_tx_status = stm32_mdma_tx_status;
1671 dd->device_issue_pending = stm32_mdma_issue_pending;
1672 dd->device_prep_slave_sg = stm32_mdma_prep_slave_sg;
1673 dd->device_prep_dma_cyclic = stm32_mdma_prep_dma_cyclic;
1674 dd->device_prep_dma_memcpy = stm32_mdma_prep_dma_memcpy;
1675 dd->device_config = stm32_mdma_slave_config;
1676 dd->device_pause = stm32_mdma_pause;
1677 dd->device_resume = stm32_mdma_resume;
1678 dd->device_terminate_all = stm32_mdma_terminate_all;
1679 dd->device_synchronize = stm32_mdma_synchronize;
1680 dd->descriptor_reuse = true;
1681
1682 dd->src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1683 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1684 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |
1685 BIT(DMA_SLAVE_BUSWIDTH_8_BYTES);
1686 dd->dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1687 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1688 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |
1689 BIT(DMA_SLAVE_BUSWIDTH_8_BYTES);
1690 dd->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV) |
1691 BIT(DMA_MEM_TO_MEM);
1692 dd->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
1693 dd->max_burst = STM32_MDMA_MAX_BURST;
1694 dd->dev = &pdev->dev;
1695 INIT_LIST_HEAD(&dd->channels);
1696
1697 for (i = 0; i < dmadev->nr_channels; i++) {
1698 chan = &dmadev->chan[i];
1699 chan->id = i;
1700
1701 if (stm32_mdma_read(dmadev, STM32_MDMA_CCR(i)) & STM32_MDMA_CCR_SM)
1702 dmadev->chan_reserved |= BIT(i);
1703
1704 chan->vchan.desc_free = stm32_mdma_desc_free;
1705 vchan_init(&chan->vchan, dd);
1706 }
1707
1708 dmadev->irq = platform_get_irq(pdev, 0);
1709 if (dmadev->irq < 0) {
1710 ret = dmadev->irq;
1711 goto err_clk;
1712 }
1713
1714 ret = devm_request_irq(&pdev->dev, dmadev->irq, stm32_mdma_irq_handler,
1715 0, dev_name(&pdev->dev), dmadev);
1716 if (ret) {
1717 dev_err(&pdev->dev, "failed to request IRQ\n");
1718 goto err_clk;
1719 }
1720
1721 ret = dmaenginem_async_device_register(dd);
1722 if (ret)
1723 goto err_clk;
1724
1725 ret = of_dma_controller_register(of_node, stm32_mdma_of_xlate, dmadev);
1726 if (ret < 0) {
1727 dev_err(&pdev->dev,
1728 "STM32 MDMA DMA OF registration failed %d\n", ret);
1729 goto err_clk;
1730 }
1731
1732 platform_set_drvdata(pdev, dmadev);
1733 pm_runtime_set_active(&pdev->dev);
1734 pm_runtime_enable(&pdev->dev);
1735 pm_runtime_get_noresume(&pdev->dev);
1736 pm_runtime_put(&pdev->dev);
1737
1738 dev_info(&pdev->dev, "STM32 MDMA driver registered\n");
1739
1740 return 0;
1741
1742err_clk:
1743 clk_disable_unprepare(dmadev->clk);
1744
1745 return ret;
1746}
1747
1748#ifdef CONFIG_PM
1749static int stm32_mdma_runtime_suspend(struct device *dev)
1750{
1751 struct stm32_mdma_device *dmadev = dev_get_drvdata(dev);
1752
1753 clk_disable_unprepare(dmadev->clk);
1754
1755 return 0;
1756}
1757
1758static int stm32_mdma_runtime_resume(struct device *dev)
1759{
1760 struct stm32_mdma_device *dmadev = dev_get_drvdata(dev);
1761 int ret;
1762
1763 ret = clk_prepare_enable(dmadev->clk);
1764 if (ret) {
1765 dev_err(dev, "failed to prepare_enable clock\n");
1766 return ret;
1767 }
1768
1769 return 0;
1770}
1771#endif
1772
1773#ifdef CONFIG_PM_SLEEP
1774static int stm32_mdma_pm_suspend(struct device *dev)
1775{
1776 struct stm32_mdma_device *dmadev = dev_get_drvdata(dev);
1777 u32 ccr, id;
1778 int ret;
1779
1780 ret = pm_runtime_resume_and_get(dev);
1781 if (ret < 0)
1782 return ret;
1783
1784 for (id = 0; id < dmadev->nr_channels; id++) {
1785 ccr = stm32_mdma_read(dmadev, STM32_MDMA_CCR(id));
1786 if (ccr & STM32_MDMA_CCR_EN) {
1787 dev_warn(dev, "Suspend is prevented by Chan %i\n", id);
1788 return -EBUSY;
1789 }
1790 }
1791
1792 pm_runtime_put_sync(dev);
1793
1794 pm_runtime_force_suspend(dev);
1795
1796 return 0;
1797}
1798
1799static int stm32_mdma_pm_resume(struct device *dev)
1800{
1801 return pm_runtime_force_resume(dev);
1802}
1803#endif
1804
1805static const struct dev_pm_ops stm32_mdma_pm_ops = {
1806 SET_SYSTEM_SLEEP_PM_OPS(stm32_mdma_pm_suspend, stm32_mdma_pm_resume)
1807 SET_RUNTIME_PM_OPS(stm32_mdma_runtime_suspend,
1808 stm32_mdma_runtime_resume, NULL)
1809};
1810
1811static struct platform_driver stm32_mdma_driver = {
1812 .probe = stm32_mdma_probe,
1813 .driver = {
1814 .name = "stm32-mdma",
1815 .of_match_table = stm32_mdma_of_match,
1816 .pm = &stm32_mdma_pm_ops,
1817 },
1818};
1819
1820static int __init stm32_mdma_init(void)
1821{
1822 return platform_driver_register(&stm32_mdma_driver);
1823}
1824
1825subsys_initcall(stm32_mdma_init);
1826
1827MODULE_DESCRIPTION("Driver for STM32 MDMA controller");
1828MODULE_AUTHOR("M'boumba Cedric Madianga <cedric.madianga@gmail.com>");
1829MODULE_AUTHOR("Pierre-Yves Mordret <pierre-yves.mordret@st.com>");