1 // SPDX-License-Identifier: GPL-2.0-only
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>
8 * Driver for STM32 MDMA controller
10 * Inspired by stm32-dma.c and dma-jz4780.c
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>
27 #include <linux/of_device.h>
28 #include <linux/of_dma.h>
29 #include <linux/platform_device.h>
30 #include <linux/pm_runtime.h>
31 #include <linux/reset.h>
32 #include <linux/slab.h>
36 #define STM32_MDMA_GISR0 0x0000 /* MDMA Int Status Reg 1 */
38 /* MDMA Channel x interrupt/status register */
39 #define STM32_MDMA_CISR(x) (0x40 + 0x40 * (x)) /* x = 0..62 */
40 #define STM32_MDMA_CISR_CRQA BIT(16)
41 #define STM32_MDMA_CISR_TCIF BIT(4)
42 #define STM32_MDMA_CISR_BTIF BIT(3)
43 #define STM32_MDMA_CISR_BRTIF BIT(2)
44 #define STM32_MDMA_CISR_CTCIF BIT(1)
45 #define STM32_MDMA_CISR_TEIF BIT(0)
47 /* MDMA Channel x interrupt flag clear register */
48 #define STM32_MDMA_CIFCR(x) (0x44 + 0x40 * (x))
49 #define STM32_MDMA_CIFCR_CLTCIF BIT(4)
50 #define STM32_MDMA_CIFCR_CBTIF BIT(3)
51 #define STM32_MDMA_CIFCR_CBRTIF BIT(2)
52 #define STM32_MDMA_CIFCR_CCTCIF BIT(1)
53 #define STM32_MDMA_CIFCR_CTEIF BIT(0)
54 #define STM32_MDMA_CIFCR_CLEAR_ALL (STM32_MDMA_CIFCR_CLTCIF \
55 | STM32_MDMA_CIFCR_CBTIF \
56 | STM32_MDMA_CIFCR_CBRTIF \
57 | STM32_MDMA_CIFCR_CCTCIF \
58 | STM32_MDMA_CIFCR_CTEIF)
60 /* MDMA Channel x error status register */
61 #define STM32_MDMA_CESR(x) (0x48 + 0x40 * (x))
62 #define STM32_MDMA_CESR_BSE BIT(11)
63 #define STM32_MDMA_CESR_ASR BIT(10)
64 #define STM32_MDMA_CESR_TEMD BIT(9)
65 #define STM32_MDMA_CESR_TELD BIT(8)
66 #define STM32_MDMA_CESR_TED BIT(7)
67 #define STM32_MDMA_CESR_TEA_MASK GENMASK(6, 0)
69 /* MDMA Channel x control register */
70 #define STM32_MDMA_CCR(x) (0x4C + 0x40 * (x))
71 #define STM32_MDMA_CCR_SWRQ BIT(16)
72 #define STM32_MDMA_CCR_WEX BIT(14)
73 #define STM32_MDMA_CCR_HEX BIT(13)
74 #define STM32_MDMA_CCR_BEX BIT(12)
75 #define STM32_MDMA_CCR_SM BIT(8)
76 #define STM32_MDMA_CCR_PL_MASK GENMASK(7, 6)
77 #define STM32_MDMA_CCR_PL(n) FIELD_PREP(STM32_MDMA_CCR_PL_MASK, (n))
78 #define STM32_MDMA_CCR_TCIE BIT(5)
79 #define STM32_MDMA_CCR_BTIE BIT(4)
80 #define STM32_MDMA_CCR_BRTIE BIT(3)
81 #define STM32_MDMA_CCR_CTCIE BIT(2)
82 #define STM32_MDMA_CCR_TEIE BIT(1)
83 #define STM32_MDMA_CCR_EN BIT(0)
84 #define STM32_MDMA_CCR_IRQ_MASK (STM32_MDMA_CCR_TCIE \
85 | STM32_MDMA_CCR_BTIE \
86 | STM32_MDMA_CCR_BRTIE \
87 | STM32_MDMA_CCR_CTCIE \
88 | STM32_MDMA_CCR_TEIE)
90 /* MDMA Channel x transfer configuration register */
91 #define STM32_MDMA_CTCR(x) (0x50 + 0x40 * (x))
92 #define STM32_MDMA_CTCR_BWM BIT(31)
93 #define STM32_MDMA_CTCR_SWRM BIT(30)
94 #define STM32_MDMA_CTCR_TRGM_MSK GENMASK(29, 28)
95 #define STM32_MDMA_CTCR_TRGM(n) FIELD_PREP(STM32_MDMA_CTCR_TRGM_MSK, (n))
96 #define STM32_MDMA_CTCR_TRGM_GET(n) FIELD_GET(STM32_MDMA_CTCR_TRGM_MSK, (n))
97 #define STM32_MDMA_CTCR_PAM_MASK GENMASK(27, 26)
98 #define STM32_MDMA_CTCR_PAM(n) FIELD_PREP(STM32_MDMA_CTCR_PAM_MASK, (n))
99 #define STM32_MDMA_CTCR_PKE BIT(25)
100 #define STM32_MDMA_CTCR_TLEN_MSK GENMASK(24, 18)
101 #define STM32_MDMA_CTCR_TLEN(n) FIELD_PREP(STM32_MDMA_CTCR_TLEN_MSK, (n))
102 #define STM32_MDMA_CTCR_TLEN_GET(n) FIELD_GET(STM32_MDMA_CTCR_TLEN_MSK, (n))
103 #define STM32_MDMA_CTCR_LEN2_MSK GENMASK(25, 18)
104 #define STM32_MDMA_CTCR_LEN2(n) FIELD_PREP(STM32_MDMA_CTCR_LEN2_MSK, (n))
105 #define STM32_MDMA_CTCR_LEN2_GET(n) FIELD_GET(STM32_MDMA_CTCR_LEN2_MSK, (n))
106 #define STM32_MDMA_CTCR_DBURST_MASK GENMASK(17, 15)
107 #define STM32_MDMA_CTCR_DBURST(n) FIELD_PREP(STM32_MDMA_CTCR_DBURST_MASK, (n))
108 #define STM32_MDMA_CTCR_SBURST_MASK GENMASK(14, 12)
109 #define STM32_MDMA_CTCR_SBURST(n) FIELD_PREP(STM32_MDMA_CTCR_SBURST_MASK, (n))
110 #define STM32_MDMA_CTCR_DINCOS_MASK GENMASK(11, 10)
111 #define STM32_MDMA_CTCR_DINCOS(n) FIELD_PREP(STM32_MDMA_CTCR_DINCOS_MASK, (n))
112 #define STM32_MDMA_CTCR_SINCOS_MASK GENMASK(9, 8)
113 #define STM32_MDMA_CTCR_SINCOS(n) FIELD_PREP(STM32_MDMA_CTCR_SINCOS_MASK, (n))
114 #define STM32_MDMA_CTCR_DSIZE_MASK GENMASK(7, 6)
115 #define STM32_MDMA_CTCR_DSIZE(n) FIELD_PREP(STM32_MDMA_CTCR_DSIZE_MASK, (n))
116 #define STM32_MDMA_CTCR_SSIZE_MASK GENMASK(5, 4)
117 #define STM32_MDMA_CTCR_SSIZE(n) FIELD_PREP(STM32_MDMA_CTCR_SSIZE_MASK, (n))
118 #define STM32_MDMA_CTCR_DINC_MASK GENMASK(3, 2)
119 #define STM32_MDMA_CTCR_DINC(n) FIELD_PREP(STM32_MDMA_CTCR_DINC_MASK, (n))
120 #define STM32_MDMA_CTCR_SINC_MASK GENMASK(1, 0)
121 #define STM32_MDMA_CTCR_SINC(n) FIELD_PREP(STM32_MDMA_CTCR_SINC_MASK, (n))
122 #define STM32_MDMA_CTCR_CFG_MASK (STM32_MDMA_CTCR_SINC_MASK \
123 | STM32_MDMA_CTCR_DINC_MASK \
124 | STM32_MDMA_CTCR_SINCOS_MASK \
125 | STM32_MDMA_CTCR_DINCOS_MASK \
126 | STM32_MDMA_CTCR_LEN2_MSK \
127 | STM32_MDMA_CTCR_TRGM_MSK)
129 /* MDMA Channel x block number of data register */
130 #define STM32_MDMA_CBNDTR(x) (0x54 + 0x40 * (x))
131 #define STM32_MDMA_CBNDTR_BRC_MK GENMASK(31, 20)
132 #define STM32_MDMA_CBNDTR_BRC(n) FIELD_PREP(STM32_MDMA_CBNDTR_BRC_MK, (n))
133 #define STM32_MDMA_CBNDTR_BRC_GET(n) FIELD_GET(STM32_MDMA_CBNDTR_BRC_MK, (n))
135 #define STM32_MDMA_CBNDTR_BRDUM BIT(19)
136 #define STM32_MDMA_CBNDTR_BRSUM BIT(18)
137 #define STM32_MDMA_CBNDTR_BNDT_MASK GENMASK(16, 0)
138 #define STM32_MDMA_CBNDTR_BNDT(n) FIELD_PREP(STM32_MDMA_CBNDTR_BNDT_MASK, (n))
140 /* MDMA Channel x source address register */
141 #define STM32_MDMA_CSAR(x) (0x58 + 0x40 * (x))
143 /* MDMA Channel x destination address register */
144 #define STM32_MDMA_CDAR(x) (0x5C + 0x40 * (x))
146 /* MDMA Channel x block repeat address update register */
147 #define STM32_MDMA_CBRUR(x) (0x60 + 0x40 * (x))
148 #define STM32_MDMA_CBRUR_DUV_MASK GENMASK(31, 16)
149 #define STM32_MDMA_CBRUR_DUV(n) FIELD_PREP(STM32_MDMA_CBRUR_DUV_MASK, (n))
150 #define STM32_MDMA_CBRUR_SUV_MASK GENMASK(15, 0)
151 #define STM32_MDMA_CBRUR_SUV(n) FIELD_PREP(STM32_MDMA_CBRUR_SUV_MASK, (n))
153 /* MDMA Channel x link address register */
154 #define STM32_MDMA_CLAR(x) (0x64 + 0x40 * (x))
156 /* MDMA Channel x trigger and bus selection register */
157 #define STM32_MDMA_CTBR(x) (0x68 + 0x40 * (x))
158 #define STM32_MDMA_CTBR_DBUS BIT(17)
159 #define STM32_MDMA_CTBR_SBUS BIT(16)
160 #define STM32_MDMA_CTBR_TSEL_MASK GENMASK(5, 0)
161 #define STM32_MDMA_CTBR_TSEL(n) FIELD_PREP(STM32_MDMA_CTBR_TSEL_MASK, (n))
163 /* MDMA Channel x mask address register */
164 #define STM32_MDMA_CMAR(x) (0x70 + 0x40 * (x))
166 /* MDMA Channel x mask data register */
167 #define STM32_MDMA_CMDR(x) (0x74 + 0x40 * (x))
169 #define STM32_MDMA_MAX_BUF_LEN 128
170 #define STM32_MDMA_MAX_BLOCK_LEN 65536
171 #define STM32_MDMA_MAX_CHANNELS 32
172 #define STM32_MDMA_MAX_REQUESTS 256
173 #define STM32_MDMA_MAX_BURST 128
174 #define STM32_MDMA_VERY_HIGH_PRIORITY 0x3
176 enum stm32_mdma_trigger_mode {
179 STM32_MDMA_BLOCK_REP,
180 STM32_MDMA_LINKED_LIST,
183 enum stm32_mdma_width {
185 STM32_MDMA_HALF_WORD,
187 STM32_MDMA_DOUBLE_WORD,
190 enum stm32_mdma_inc_mode {
191 STM32_MDMA_FIXED = 0,
196 struct stm32_mdma_chan_config {
204 struct stm32_mdma_hwdesc {
217 struct stm32_mdma_desc_node {
218 struct stm32_mdma_hwdesc *hwdesc;
219 dma_addr_t hwdesc_phys;
222 struct stm32_mdma_desc {
223 struct virt_dma_desc vdesc;
227 struct stm32_mdma_desc_node node[];
230 struct stm32_mdma_chan {
231 struct virt_dma_chan vchan;
232 struct dma_pool *desc_pool;
234 struct stm32_mdma_desc *desc;
236 struct dma_slave_config dma_config;
237 struct stm32_mdma_chan_config chan_config;
243 struct stm32_mdma_device {
244 struct dma_device ddev;
250 u32 nr_ahb_addr_masks;
252 struct stm32_mdma_chan chan[STM32_MDMA_MAX_CHANNELS];
253 u32 ahb_addr_masks[];
256 static struct stm32_mdma_device *stm32_mdma_get_dev(
257 struct stm32_mdma_chan *chan)
259 return container_of(chan->vchan.chan.device, struct stm32_mdma_device,
263 static struct stm32_mdma_chan *to_stm32_mdma_chan(struct dma_chan *c)
265 return container_of(c, struct stm32_mdma_chan, vchan.chan);
268 static struct stm32_mdma_desc *to_stm32_mdma_desc(struct virt_dma_desc *vdesc)
270 return container_of(vdesc, struct stm32_mdma_desc, vdesc);
273 static struct device *chan2dev(struct stm32_mdma_chan *chan)
275 return &chan->vchan.chan.dev->device;
278 static struct device *mdma2dev(struct stm32_mdma_device *mdma_dev)
280 return mdma_dev->ddev.dev;
283 static u32 stm32_mdma_read(struct stm32_mdma_device *dmadev, u32 reg)
285 return readl_relaxed(dmadev->base + reg);
288 static void stm32_mdma_write(struct stm32_mdma_device *dmadev, u32 reg, u32 val)
290 writel_relaxed(val, dmadev->base + reg);
293 static void stm32_mdma_set_bits(struct stm32_mdma_device *dmadev, u32 reg,
296 void __iomem *addr = dmadev->base + reg;
298 writel_relaxed(readl_relaxed(addr) | mask, addr);
301 static void stm32_mdma_clr_bits(struct stm32_mdma_device *dmadev, u32 reg,
304 void __iomem *addr = dmadev->base + reg;
306 writel_relaxed(readl_relaxed(addr) & ~mask, addr);
309 static struct stm32_mdma_desc *stm32_mdma_alloc_desc(
310 struct stm32_mdma_chan *chan, u32 count)
312 struct stm32_mdma_desc *desc;
315 desc = kzalloc(struct_size(desc, node, count), GFP_NOWAIT);
319 for (i = 0; i < count; i++) {
320 desc->node[i].hwdesc =
321 dma_pool_alloc(chan->desc_pool, GFP_NOWAIT,
322 &desc->node[i].hwdesc_phys);
323 if (!desc->node[i].hwdesc)
332 dev_err(chan2dev(chan), "Failed to allocate descriptor\n");
334 dma_pool_free(chan->desc_pool, desc->node[i].hwdesc,
335 desc->node[i].hwdesc_phys);
340 static void stm32_mdma_desc_free(struct virt_dma_desc *vdesc)
342 struct stm32_mdma_desc *desc = to_stm32_mdma_desc(vdesc);
343 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(vdesc->tx.chan);
346 for (i = 0; i < desc->count; i++)
347 dma_pool_free(chan->desc_pool, desc->node[i].hwdesc,
348 desc->node[i].hwdesc_phys);
352 static int stm32_mdma_get_width(struct stm32_mdma_chan *chan,
353 enum dma_slave_buswidth width)
356 case DMA_SLAVE_BUSWIDTH_1_BYTE:
357 case DMA_SLAVE_BUSWIDTH_2_BYTES:
358 case DMA_SLAVE_BUSWIDTH_4_BYTES:
359 case DMA_SLAVE_BUSWIDTH_8_BYTES:
360 return ffs(width) - 1;
362 dev_err(chan2dev(chan), "Dma bus width %i not supported\n",
368 static enum dma_slave_buswidth stm32_mdma_get_max_width(dma_addr_t addr,
369 u32 buf_len, u32 tlen)
371 enum dma_slave_buswidth max_width = DMA_SLAVE_BUSWIDTH_8_BYTES;
373 for (max_width = DMA_SLAVE_BUSWIDTH_8_BYTES;
374 max_width > DMA_SLAVE_BUSWIDTH_1_BYTE;
377 * Address and buffer length both have to be aligned on
380 if ((((buf_len | addr) & (max_width - 1)) == 0) &&
388 static u32 stm32_mdma_get_best_burst(u32 buf_len, u32 tlen, u32 max_burst,
389 enum dma_slave_buswidth width)
393 best_burst = min((u32)1 << __ffs(tlen | buf_len),
394 max_burst * width) / width;
396 return (best_burst > 0) ? best_burst : 1;
399 static int stm32_mdma_disable_chan(struct stm32_mdma_chan *chan)
401 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
402 u32 ccr, cisr, id, reg;
406 reg = STM32_MDMA_CCR(id);
408 /* Disable interrupts */
409 stm32_mdma_clr_bits(dmadev, reg, STM32_MDMA_CCR_IRQ_MASK);
411 ccr = stm32_mdma_read(dmadev, reg);
412 if (ccr & STM32_MDMA_CCR_EN) {
413 stm32_mdma_clr_bits(dmadev, reg, STM32_MDMA_CCR_EN);
415 /* Ensure that any ongoing transfer has been completed */
416 ret = readl_relaxed_poll_timeout_atomic(
417 dmadev->base + STM32_MDMA_CISR(id), cisr,
418 (cisr & STM32_MDMA_CISR_CTCIF), 10, 1000);
420 dev_err(chan2dev(chan), "%s: timeout!\n", __func__);
428 static void stm32_mdma_stop(struct stm32_mdma_chan *chan)
430 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
435 ret = stm32_mdma_disable_chan(chan);
439 /* Clear interrupt status if it is there */
440 status = stm32_mdma_read(dmadev, STM32_MDMA_CISR(chan->id));
442 dev_dbg(chan2dev(chan), "%s(): clearing interrupt: 0x%08x\n",
444 stm32_mdma_set_bits(dmadev, STM32_MDMA_CIFCR(chan->id), status);
450 static void stm32_mdma_set_bus(struct stm32_mdma_device *dmadev, u32 *ctbr,
451 u32 ctbr_mask, u32 src_addr)
456 /* Check if memory device is on AHB or AXI */
458 mask = src_addr & 0xF0000000;
459 for (i = 0; i < dmadev->nr_ahb_addr_masks; i++) {
460 if (mask == dmadev->ahb_addr_masks[i]) {
467 static int stm32_mdma_set_xfer_param(struct stm32_mdma_chan *chan,
468 enum dma_transfer_direction direction,
469 u32 *mdma_ccr, u32 *mdma_ctcr,
470 u32 *mdma_ctbr, dma_addr_t addr,
473 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
474 struct stm32_mdma_chan_config *chan_config = &chan->chan_config;
475 enum dma_slave_buswidth src_addr_width, dst_addr_width;
476 phys_addr_t src_addr, dst_addr;
477 int src_bus_width, dst_bus_width;
478 u32 src_maxburst, dst_maxburst, src_best_burst, dst_best_burst;
479 u32 ccr, ctcr, ctbr, tlen;
481 src_addr_width = chan->dma_config.src_addr_width;
482 dst_addr_width = chan->dma_config.dst_addr_width;
483 src_maxburst = chan->dma_config.src_maxburst;
484 dst_maxburst = chan->dma_config.dst_maxburst;
486 ccr = stm32_mdma_read(dmadev, STM32_MDMA_CCR(chan->id));
487 ctcr = stm32_mdma_read(dmadev, STM32_MDMA_CTCR(chan->id));
488 ctbr = stm32_mdma_read(dmadev, STM32_MDMA_CTBR(chan->id));
490 /* Enable HW request mode */
491 ctcr &= ~STM32_MDMA_CTCR_SWRM;
493 /* Set DINC, SINC, DINCOS, SINCOS, TRGM and TLEN retrieve from DT */
494 ctcr &= ~STM32_MDMA_CTCR_CFG_MASK;
495 ctcr |= chan_config->transfer_config & STM32_MDMA_CTCR_CFG_MASK;
498 * For buffer transfer length (TLEN) we have to set
499 * the number of bytes - 1 in CTCR register
501 tlen = STM32_MDMA_CTCR_LEN2_GET(ctcr);
502 ctcr &= ~STM32_MDMA_CTCR_LEN2_MSK;
503 ctcr |= STM32_MDMA_CTCR_TLEN((tlen - 1));
505 /* Disable Pack Enable */
506 ctcr &= ~STM32_MDMA_CTCR_PKE;
508 /* Check burst size constraints */
509 if (src_maxburst * src_addr_width > STM32_MDMA_MAX_BURST ||
510 dst_maxburst * dst_addr_width > STM32_MDMA_MAX_BURST) {
511 dev_err(chan2dev(chan),
512 "burst size * bus width higher than %d bytes\n",
513 STM32_MDMA_MAX_BURST);
517 if ((!is_power_of_2(src_maxburst) && src_maxburst > 0) ||
518 (!is_power_of_2(dst_maxburst) && dst_maxburst > 0)) {
519 dev_err(chan2dev(chan), "burst size must be a power of 2\n");
524 * Configure channel control:
525 * - Clear SW request as in this case this is a HW one
526 * - Clear WEX, HEX and BEX bits
527 * - Set priority level
529 ccr &= ~(STM32_MDMA_CCR_SWRQ | STM32_MDMA_CCR_WEX | STM32_MDMA_CCR_HEX |
530 STM32_MDMA_CCR_BEX | STM32_MDMA_CCR_PL_MASK);
531 ccr |= STM32_MDMA_CCR_PL(chan_config->priority_level);
533 /* Configure Trigger selection */
534 ctbr &= ~STM32_MDMA_CTBR_TSEL_MASK;
535 ctbr |= STM32_MDMA_CTBR_TSEL(chan_config->request);
539 dst_addr = chan->dma_config.dst_addr;
541 /* Set device data size */
542 dst_bus_width = stm32_mdma_get_width(chan, dst_addr_width);
543 if (dst_bus_width < 0)
544 return dst_bus_width;
545 ctcr &= ~STM32_MDMA_CTCR_DSIZE_MASK;
546 ctcr |= STM32_MDMA_CTCR_DSIZE(dst_bus_width);
548 /* Set device burst value */
549 dst_best_burst = stm32_mdma_get_best_burst(buf_len, tlen,
552 chan->mem_burst = dst_best_burst;
553 ctcr &= ~STM32_MDMA_CTCR_DBURST_MASK;
554 ctcr |= STM32_MDMA_CTCR_DBURST((ilog2(dst_best_burst)));
556 /* Set memory data size */
557 src_addr_width = stm32_mdma_get_max_width(addr, buf_len, tlen);
558 chan->mem_width = src_addr_width;
559 src_bus_width = stm32_mdma_get_width(chan, src_addr_width);
560 if (src_bus_width < 0)
561 return src_bus_width;
562 ctcr &= ~STM32_MDMA_CTCR_SSIZE_MASK |
563 STM32_MDMA_CTCR_SINCOS_MASK;
564 ctcr |= STM32_MDMA_CTCR_SSIZE(src_bus_width) |
565 STM32_MDMA_CTCR_SINCOS(src_bus_width);
567 /* Set memory burst value */
568 src_maxburst = STM32_MDMA_MAX_BUF_LEN / src_addr_width;
569 src_best_burst = stm32_mdma_get_best_burst(buf_len, tlen,
572 chan->mem_burst = src_best_burst;
573 ctcr &= ~STM32_MDMA_CTCR_SBURST_MASK;
574 ctcr |= STM32_MDMA_CTCR_SBURST((ilog2(src_best_burst)));
577 stm32_mdma_set_bus(dmadev, &ctbr, STM32_MDMA_CTBR_DBUS,
580 if (dst_bus_width != src_bus_width)
581 ctcr |= STM32_MDMA_CTCR_PKE;
583 /* Set destination address */
584 stm32_mdma_write(dmadev, STM32_MDMA_CDAR(chan->id), dst_addr);
588 src_addr = chan->dma_config.src_addr;
590 /* Set device data size */
591 src_bus_width = stm32_mdma_get_width(chan, src_addr_width);
592 if (src_bus_width < 0)
593 return src_bus_width;
594 ctcr &= ~STM32_MDMA_CTCR_SSIZE_MASK;
595 ctcr |= STM32_MDMA_CTCR_SSIZE(src_bus_width);
597 /* Set device burst value */
598 src_best_burst = stm32_mdma_get_best_burst(buf_len, tlen,
601 ctcr &= ~STM32_MDMA_CTCR_SBURST_MASK;
602 ctcr |= STM32_MDMA_CTCR_SBURST((ilog2(src_best_burst)));
604 /* Set memory data size */
605 dst_addr_width = stm32_mdma_get_max_width(addr, buf_len, tlen);
606 chan->mem_width = dst_addr_width;
607 dst_bus_width = stm32_mdma_get_width(chan, dst_addr_width);
608 if (dst_bus_width < 0)
609 return dst_bus_width;
610 ctcr &= ~(STM32_MDMA_CTCR_DSIZE_MASK |
611 STM32_MDMA_CTCR_DINCOS_MASK);
612 ctcr |= STM32_MDMA_CTCR_DSIZE(dst_bus_width) |
613 STM32_MDMA_CTCR_DINCOS(dst_bus_width);
615 /* Set memory burst value */
616 dst_maxburst = STM32_MDMA_MAX_BUF_LEN / dst_addr_width;
617 dst_best_burst = stm32_mdma_get_best_burst(buf_len, tlen,
620 ctcr &= ~STM32_MDMA_CTCR_DBURST_MASK;
621 ctcr |= STM32_MDMA_CTCR_DBURST((ilog2(dst_best_burst)));
624 stm32_mdma_set_bus(dmadev, &ctbr, STM32_MDMA_CTBR_SBUS,
627 if (dst_bus_width != src_bus_width)
628 ctcr |= STM32_MDMA_CTCR_PKE;
630 /* Set source address */
631 stm32_mdma_write(dmadev, STM32_MDMA_CSAR(chan->id), src_addr);
635 dev_err(chan2dev(chan), "Dma direction is not supported\n");
646 static void stm32_mdma_dump_hwdesc(struct stm32_mdma_chan *chan,
647 struct stm32_mdma_desc_node *node)
649 dev_dbg(chan2dev(chan), "hwdesc: %pad\n", &node->hwdesc_phys);
650 dev_dbg(chan2dev(chan), "CTCR: 0x%08x\n", node->hwdesc->ctcr);
651 dev_dbg(chan2dev(chan), "CBNDTR: 0x%08x\n", node->hwdesc->cbndtr);
652 dev_dbg(chan2dev(chan), "CSAR: 0x%08x\n", node->hwdesc->csar);
653 dev_dbg(chan2dev(chan), "CDAR: 0x%08x\n", node->hwdesc->cdar);
654 dev_dbg(chan2dev(chan), "CBRUR: 0x%08x\n", node->hwdesc->cbrur);
655 dev_dbg(chan2dev(chan), "CLAR: 0x%08x\n", node->hwdesc->clar);
656 dev_dbg(chan2dev(chan), "CTBR: 0x%08x\n", node->hwdesc->ctbr);
657 dev_dbg(chan2dev(chan), "CMAR: 0x%08x\n", node->hwdesc->cmar);
658 dev_dbg(chan2dev(chan), "CMDR: 0x%08x\n\n", node->hwdesc->cmdr);
661 static void stm32_mdma_setup_hwdesc(struct stm32_mdma_chan *chan,
662 struct stm32_mdma_desc *desc,
663 enum dma_transfer_direction dir, u32 count,
664 dma_addr_t src_addr, dma_addr_t dst_addr,
665 u32 len, u32 ctcr, u32 ctbr, bool is_last,
666 bool is_first, bool is_cyclic)
668 struct stm32_mdma_chan_config *config = &chan->chan_config;
669 struct stm32_mdma_hwdesc *hwdesc;
670 u32 next = count + 1;
672 hwdesc = desc->node[count].hwdesc;
674 hwdesc->cbndtr &= ~(STM32_MDMA_CBNDTR_BRC_MK |
675 STM32_MDMA_CBNDTR_BRDUM |
676 STM32_MDMA_CBNDTR_BRSUM |
677 STM32_MDMA_CBNDTR_BNDT_MASK);
678 hwdesc->cbndtr |= STM32_MDMA_CBNDTR_BNDT(len);
679 hwdesc->csar = src_addr;
680 hwdesc->cdar = dst_addr;
683 hwdesc->cmar = config->mask_addr;
684 hwdesc->cmdr = config->mask_data;
688 hwdesc->clar = desc->node[0].hwdesc_phys;
692 hwdesc->clar = desc->node[next].hwdesc_phys;
695 stm32_mdma_dump_hwdesc(chan, &desc->node[count]);
698 static int stm32_mdma_setup_xfer(struct stm32_mdma_chan *chan,
699 struct stm32_mdma_desc *desc,
700 struct scatterlist *sgl, u32 sg_len,
701 enum dma_transfer_direction direction)
703 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
704 struct dma_slave_config *dma_config = &chan->dma_config;
705 struct scatterlist *sg;
706 dma_addr_t src_addr, dst_addr;
710 for_each_sg(sgl, sg, sg_len, i) {
711 if (sg_dma_len(sg) > STM32_MDMA_MAX_BLOCK_LEN) {
712 dev_err(chan2dev(chan), "Invalid block len\n");
716 if (direction == DMA_MEM_TO_DEV) {
717 src_addr = sg_dma_address(sg);
718 dst_addr = dma_config->dst_addr;
719 ret = stm32_mdma_set_xfer_param(chan, direction, &ccr,
720 &ctcr, &ctbr, src_addr,
722 stm32_mdma_set_bus(dmadev, &ctbr, STM32_MDMA_CTBR_SBUS,
725 src_addr = dma_config->src_addr;
726 dst_addr = sg_dma_address(sg);
727 ret = stm32_mdma_set_xfer_param(chan, direction, &ccr,
728 &ctcr, &ctbr, dst_addr,
730 stm32_mdma_set_bus(dmadev, &ctbr, STM32_MDMA_CTBR_DBUS,
737 stm32_mdma_setup_hwdesc(chan, desc, direction, i, src_addr,
738 dst_addr, sg_dma_len(sg), ctcr, ctbr,
739 i == sg_len - 1, i == 0, false);
742 /* Enable interrupts */
743 ccr &= ~STM32_MDMA_CCR_IRQ_MASK;
744 ccr |= STM32_MDMA_CCR_TEIE | STM32_MDMA_CCR_CTCIE;
746 ccr |= STM32_MDMA_CCR_BTIE;
752 static struct dma_async_tx_descriptor *
753 stm32_mdma_prep_slave_sg(struct dma_chan *c, struct scatterlist *sgl,
754 u32 sg_len, enum dma_transfer_direction direction,
755 unsigned long flags, void *context)
757 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
758 struct stm32_mdma_desc *desc;
762 * Once DMA is in setup cyclic mode the channel we cannot assign this
763 * channel anymore. The DMA channel needs to be aborted or terminated
764 * for allowing another request.
766 if (chan->desc && chan->desc->cyclic) {
767 dev_err(chan2dev(chan),
768 "Request not allowed when dma in cyclic mode\n");
772 desc = stm32_mdma_alloc_desc(chan, sg_len);
776 ret = stm32_mdma_setup_xfer(chan, desc, sgl, sg_len, direction);
780 desc->cyclic = false;
782 return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
785 for (i = 0; i < desc->count; i++)
786 dma_pool_free(chan->desc_pool, desc->node[i].hwdesc,
787 desc->node[i].hwdesc_phys);
792 static struct dma_async_tx_descriptor *
793 stm32_mdma_prep_dma_cyclic(struct dma_chan *c, dma_addr_t buf_addr,
794 size_t buf_len, size_t period_len,
795 enum dma_transfer_direction direction,
798 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
799 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
800 struct dma_slave_config *dma_config = &chan->dma_config;
801 struct stm32_mdma_desc *desc;
802 dma_addr_t src_addr, dst_addr;
803 u32 ccr, ctcr, ctbr, count;
807 * Once DMA is in setup cyclic mode the channel we cannot assign this
808 * channel anymore. The DMA channel needs to be aborted or terminated
809 * for allowing another request.
811 if (chan->desc && chan->desc->cyclic) {
812 dev_err(chan2dev(chan),
813 "Request not allowed when dma in cyclic mode\n");
817 if (!buf_len || !period_len || period_len > STM32_MDMA_MAX_BLOCK_LEN) {
818 dev_err(chan2dev(chan), "Invalid buffer/period len\n");
822 if (buf_len % period_len) {
823 dev_err(chan2dev(chan), "buf_len not multiple of period_len\n");
827 count = buf_len / period_len;
829 desc = stm32_mdma_alloc_desc(chan, count);
834 if (direction == DMA_MEM_TO_DEV) {
836 ret = stm32_mdma_set_xfer_param(chan, direction, &ccr, &ctcr,
837 &ctbr, src_addr, period_len);
838 stm32_mdma_set_bus(dmadev, &ctbr, STM32_MDMA_CTBR_SBUS,
842 ret = stm32_mdma_set_xfer_param(chan, direction, &ccr, &ctcr,
843 &ctbr, dst_addr, period_len);
844 stm32_mdma_set_bus(dmadev, &ctbr, STM32_MDMA_CTBR_DBUS,
851 /* Enable interrupts */
852 ccr &= ~STM32_MDMA_CCR_IRQ_MASK;
853 ccr |= STM32_MDMA_CCR_TEIE | STM32_MDMA_CCR_CTCIE | STM32_MDMA_CCR_BTIE;
856 /* Configure hwdesc list */
857 for (i = 0; i < count; i++) {
858 if (direction == DMA_MEM_TO_DEV) {
859 src_addr = buf_addr + i * period_len;
860 dst_addr = dma_config->dst_addr;
862 src_addr = dma_config->src_addr;
863 dst_addr = buf_addr + i * period_len;
866 stm32_mdma_setup_hwdesc(chan, desc, direction, i, src_addr,
867 dst_addr, period_len, ctcr, ctbr,
868 i == count - 1, i == 0, true);
873 return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
876 for (i = 0; i < desc->count; i++)
877 dma_pool_free(chan->desc_pool, desc->node[i].hwdesc,
878 desc->node[i].hwdesc_phys);
883 static struct dma_async_tx_descriptor *
884 stm32_mdma_prep_dma_memcpy(struct dma_chan *c, dma_addr_t dest, dma_addr_t src,
885 size_t len, unsigned long flags)
887 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
888 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
889 enum dma_slave_buswidth max_width;
890 struct stm32_mdma_desc *desc;
891 struct stm32_mdma_hwdesc *hwdesc;
892 u32 ccr, ctcr, ctbr, cbndtr, count, max_burst, mdma_burst;
893 u32 best_burst, tlen;
894 size_t xfer_count, offset;
895 int src_bus_width, dst_bus_width;
899 * Once DMA is in setup cyclic mode the channel we cannot assign this
900 * channel anymore. The DMA channel needs to be aborted or terminated
901 * to allow another request
903 if (chan->desc && chan->desc->cyclic) {
904 dev_err(chan2dev(chan),
905 "Request not allowed when dma in cyclic mode\n");
909 count = DIV_ROUND_UP(len, STM32_MDMA_MAX_BLOCK_LEN);
910 desc = stm32_mdma_alloc_desc(chan, count);
914 ccr = stm32_mdma_read(dmadev, STM32_MDMA_CCR(chan->id));
915 ctcr = stm32_mdma_read(dmadev, STM32_MDMA_CTCR(chan->id));
916 ctbr = stm32_mdma_read(dmadev, STM32_MDMA_CTBR(chan->id));
917 cbndtr = stm32_mdma_read(dmadev, STM32_MDMA_CBNDTR(chan->id));
919 /* Enable sw req, some interrupts and clear other bits */
920 ccr &= ~(STM32_MDMA_CCR_WEX | STM32_MDMA_CCR_HEX |
921 STM32_MDMA_CCR_BEX | STM32_MDMA_CCR_PL_MASK |
922 STM32_MDMA_CCR_IRQ_MASK);
923 ccr |= STM32_MDMA_CCR_TEIE;
925 /* Enable SW request mode, dest/src inc and clear other bits */
926 ctcr &= ~(STM32_MDMA_CTCR_BWM | STM32_MDMA_CTCR_TRGM_MSK |
927 STM32_MDMA_CTCR_PAM_MASK | STM32_MDMA_CTCR_PKE |
928 STM32_MDMA_CTCR_TLEN_MSK | STM32_MDMA_CTCR_DBURST_MASK |
929 STM32_MDMA_CTCR_SBURST_MASK | STM32_MDMA_CTCR_DINCOS_MASK |
930 STM32_MDMA_CTCR_SINCOS_MASK | STM32_MDMA_CTCR_DSIZE_MASK |
931 STM32_MDMA_CTCR_SSIZE_MASK | STM32_MDMA_CTCR_DINC_MASK |
932 STM32_MDMA_CTCR_SINC_MASK);
933 ctcr |= STM32_MDMA_CTCR_SWRM | STM32_MDMA_CTCR_SINC(STM32_MDMA_INC) |
934 STM32_MDMA_CTCR_DINC(STM32_MDMA_INC);
936 /* Reset HW request */
937 ctbr &= ~STM32_MDMA_CTBR_TSEL_MASK;
940 stm32_mdma_set_bus(dmadev, &ctbr, STM32_MDMA_CTBR_SBUS, src);
941 stm32_mdma_set_bus(dmadev, &ctbr, STM32_MDMA_CTBR_DBUS, dest);
943 /* Clear CBNDTR registers */
944 cbndtr &= ~(STM32_MDMA_CBNDTR_BRC_MK | STM32_MDMA_CBNDTR_BRDUM |
945 STM32_MDMA_CBNDTR_BRSUM | STM32_MDMA_CBNDTR_BNDT_MASK);
947 if (len <= STM32_MDMA_MAX_BLOCK_LEN) {
948 cbndtr |= STM32_MDMA_CBNDTR_BNDT(len);
949 if (len <= STM32_MDMA_MAX_BUF_LEN) {
950 /* Setup a buffer transfer */
951 ccr |= STM32_MDMA_CCR_TCIE | STM32_MDMA_CCR_CTCIE;
952 ctcr |= STM32_MDMA_CTCR_TRGM(STM32_MDMA_BUFFER);
954 /* Setup a block transfer */
955 ccr |= STM32_MDMA_CCR_BTIE | STM32_MDMA_CCR_CTCIE;
956 ctcr |= STM32_MDMA_CTCR_TRGM(STM32_MDMA_BLOCK);
959 tlen = STM32_MDMA_MAX_BUF_LEN;
960 ctcr |= STM32_MDMA_CTCR_TLEN((tlen - 1));
962 /* Set source best burst size */
963 max_width = stm32_mdma_get_max_width(src, len, tlen);
964 src_bus_width = stm32_mdma_get_width(chan, max_width);
966 max_burst = tlen / max_width;
967 best_burst = stm32_mdma_get_best_burst(len, tlen, max_burst,
969 mdma_burst = ilog2(best_burst);
971 ctcr |= STM32_MDMA_CTCR_SBURST(mdma_burst) |
972 STM32_MDMA_CTCR_SSIZE(src_bus_width) |
973 STM32_MDMA_CTCR_SINCOS(src_bus_width);
975 /* Set destination best burst size */
976 max_width = stm32_mdma_get_max_width(dest, len, tlen);
977 dst_bus_width = stm32_mdma_get_width(chan, max_width);
979 max_burst = tlen / max_width;
980 best_burst = stm32_mdma_get_best_burst(len, tlen, max_burst,
982 mdma_burst = ilog2(best_burst);
984 ctcr |= STM32_MDMA_CTCR_DBURST(mdma_burst) |
985 STM32_MDMA_CTCR_DSIZE(dst_bus_width) |
986 STM32_MDMA_CTCR_DINCOS(dst_bus_width);
988 if (dst_bus_width != src_bus_width)
989 ctcr |= STM32_MDMA_CTCR_PKE;
991 /* Prepare hardware descriptor */
992 hwdesc = desc->node[0].hwdesc;
994 hwdesc->cbndtr = cbndtr;
1003 stm32_mdma_dump_hwdesc(chan, &desc->node[0]);
1005 /* Setup a LLI transfer */
1006 ctcr |= STM32_MDMA_CTCR_TRGM(STM32_MDMA_LINKED_LIST) |
1007 STM32_MDMA_CTCR_TLEN((STM32_MDMA_MAX_BUF_LEN - 1));
1008 ccr |= STM32_MDMA_CCR_BTIE | STM32_MDMA_CCR_CTCIE;
1009 tlen = STM32_MDMA_MAX_BUF_LEN;
1011 for (i = 0, offset = 0; offset < len;
1012 i++, offset += xfer_count) {
1013 xfer_count = min_t(size_t, len - offset,
1014 STM32_MDMA_MAX_BLOCK_LEN);
1016 /* Set source best burst size */
1017 max_width = stm32_mdma_get_max_width(src, len, tlen);
1018 src_bus_width = stm32_mdma_get_width(chan, max_width);
1020 max_burst = tlen / max_width;
1021 best_burst = stm32_mdma_get_best_burst(len, tlen,
1024 mdma_burst = ilog2(best_burst);
1026 ctcr |= STM32_MDMA_CTCR_SBURST(mdma_burst) |
1027 STM32_MDMA_CTCR_SSIZE(src_bus_width) |
1028 STM32_MDMA_CTCR_SINCOS(src_bus_width);
1030 /* Set destination best burst size */
1031 max_width = stm32_mdma_get_max_width(dest, len, tlen);
1032 dst_bus_width = stm32_mdma_get_width(chan, max_width);
1034 max_burst = tlen / max_width;
1035 best_burst = stm32_mdma_get_best_burst(len, tlen,
1038 mdma_burst = ilog2(best_burst);
1040 ctcr |= STM32_MDMA_CTCR_DBURST(mdma_burst) |
1041 STM32_MDMA_CTCR_DSIZE(dst_bus_width) |
1042 STM32_MDMA_CTCR_DINCOS(dst_bus_width);
1044 if (dst_bus_width != src_bus_width)
1045 ctcr |= STM32_MDMA_CTCR_PKE;
1047 /* Prepare hardware descriptor */
1048 stm32_mdma_setup_hwdesc(chan, desc, DMA_MEM_TO_MEM, i,
1049 src + offset, dest + offset,
1050 xfer_count, ctcr, ctbr,
1051 i == count - 1, i == 0, false);
1057 desc->cyclic = false;
1059 return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
1062 static void stm32_mdma_dump_reg(struct stm32_mdma_chan *chan)
1064 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
1066 dev_dbg(chan2dev(chan), "CCR: 0x%08x\n",
1067 stm32_mdma_read(dmadev, STM32_MDMA_CCR(chan->id)));
1068 dev_dbg(chan2dev(chan), "CTCR: 0x%08x\n",
1069 stm32_mdma_read(dmadev, STM32_MDMA_CTCR(chan->id)));
1070 dev_dbg(chan2dev(chan), "CBNDTR: 0x%08x\n",
1071 stm32_mdma_read(dmadev, STM32_MDMA_CBNDTR(chan->id)));
1072 dev_dbg(chan2dev(chan), "CSAR: 0x%08x\n",
1073 stm32_mdma_read(dmadev, STM32_MDMA_CSAR(chan->id)));
1074 dev_dbg(chan2dev(chan), "CDAR: 0x%08x\n",
1075 stm32_mdma_read(dmadev, STM32_MDMA_CDAR(chan->id)));
1076 dev_dbg(chan2dev(chan), "CBRUR: 0x%08x\n",
1077 stm32_mdma_read(dmadev, STM32_MDMA_CBRUR(chan->id)));
1078 dev_dbg(chan2dev(chan), "CLAR: 0x%08x\n",
1079 stm32_mdma_read(dmadev, STM32_MDMA_CLAR(chan->id)));
1080 dev_dbg(chan2dev(chan), "CTBR: 0x%08x\n",
1081 stm32_mdma_read(dmadev, STM32_MDMA_CTBR(chan->id)));
1082 dev_dbg(chan2dev(chan), "CMAR: 0x%08x\n",
1083 stm32_mdma_read(dmadev, STM32_MDMA_CMAR(chan->id)));
1084 dev_dbg(chan2dev(chan), "CMDR: 0x%08x\n",
1085 stm32_mdma_read(dmadev, STM32_MDMA_CMDR(chan->id)));
1088 static void stm32_mdma_start_transfer(struct stm32_mdma_chan *chan)
1090 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
1091 struct virt_dma_desc *vdesc;
1092 struct stm32_mdma_hwdesc *hwdesc;
1096 vdesc = vchan_next_desc(&chan->vchan);
1102 list_del(&vdesc->node);
1104 chan->desc = to_stm32_mdma_desc(vdesc);
1105 hwdesc = chan->desc->node[0].hwdesc;
1106 chan->curr_hwdesc = 0;
1108 stm32_mdma_write(dmadev, STM32_MDMA_CCR(id), chan->desc->ccr);
1109 stm32_mdma_write(dmadev, STM32_MDMA_CTCR(id), hwdesc->ctcr);
1110 stm32_mdma_write(dmadev, STM32_MDMA_CBNDTR(id), hwdesc->cbndtr);
1111 stm32_mdma_write(dmadev, STM32_MDMA_CSAR(id), hwdesc->csar);
1112 stm32_mdma_write(dmadev, STM32_MDMA_CDAR(id), hwdesc->cdar);
1113 stm32_mdma_write(dmadev, STM32_MDMA_CBRUR(id), hwdesc->cbrur);
1114 stm32_mdma_write(dmadev, STM32_MDMA_CLAR(id), hwdesc->clar);
1115 stm32_mdma_write(dmadev, STM32_MDMA_CTBR(id), hwdesc->ctbr);
1116 stm32_mdma_write(dmadev, STM32_MDMA_CMAR(id), hwdesc->cmar);
1117 stm32_mdma_write(dmadev, STM32_MDMA_CMDR(id), hwdesc->cmdr);
1119 /* Clear interrupt status if it is there */
1120 status = stm32_mdma_read(dmadev, STM32_MDMA_CISR(id));
1122 stm32_mdma_set_bits(dmadev, STM32_MDMA_CIFCR(id), status);
1124 stm32_mdma_dump_reg(chan);
1127 stm32_mdma_set_bits(dmadev, STM32_MDMA_CCR(id), STM32_MDMA_CCR_EN);
1129 /* Set SW request in case of MEM2MEM transfer */
1130 if (hwdesc->ctcr & STM32_MDMA_CTCR_SWRM) {
1131 reg = STM32_MDMA_CCR(id);
1132 stm32_mdma_set_bits(dmadev, reg, STM32_MDMA_CCR_SWRQ);
1137 dev_dbg(chan2dev(chan), "vchan %pK: started\n", &chan->vchan);
1140 static void stm32_mdma_issue_pending(struct dma_chan *c)
1142 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
1143 unsigned long flags;
1145 spin_lock_irqsave(&chan->vchan.lock, flags);
1147 if (!vchan_issue_pending(&chan->vchan))
1150 dev_dbg(chan2dev(chan), "vchan %pK: issued\n", &chan->vchan);
1152 if (!chan->desc && !chan->busy)
1153 stm32_mdma_start_transfer(chan);
1156 spin_unlock_irqrestore(&chan->vchan.lock, flags);
1159 static int stm32_mdma_pause(struct dma_chan *c)
1161 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
1162 unsigned long flags;
1165 spin_lock_irqsave(&chan->vchan.lock, flags);
1166 ret = stm32_mdma_disable_chan(chan);
1167 spin_unlock_irqrestore(&chan->vchan.lock, flags);
1170 dev_dbg(chan2dev(chan), "vchan %pK: pause\n", &chan->vchan);
1175 static int stm32_mdma_resume(struct dma_chan *c)
1177 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
1178 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
1179 struct stm32_mdma_hwdesc *hwdesc;
1180 unsigned long flags;
1183 hwdesc = chan->desc->node[chan->curr_hwdesc].hwdesc;
1185 spin_lock_irqsave(&chan->vchan.lock, flags);
1187 /* Re-configure control register */
1188 stm32_mdma_write(dmadev, STM32_MDMA_CCR(chan->id), chan->desc->ccr);
1190 /* Clear interrupt status if it is there */
1191 status = stm32_mdma_read(dmadev, STM32_MDMA_CISR(chan->id));
1193 stm32_mdma_set_bits(dmadev, STM32_MDMA_CIFCR(chan->id), status);
1195 stm32_mdma_dump_reg(chan);
1198 reg = STM32_MDMA_CCR(chan->id);
1199 stm32_mdma_set_bits(dmadev, reg, STM32_MDMA_CCR_EN);
1201 /* Set SW request in case of MEM2MEM transfer */
1202 if (hwdesc->ctcr & STM32_MDMA_CTCR_SWRM)
1203 stm32_mdma_set_bits(dmadev, reg, STM32_MDMA_CCR_SWRQ);
1205 spin_unlock_irqrestore(&chan->vchan.lock, flags);
1207 dev_dbg(chan2dev(chan), "vchan %pK: resume\n", &chan->vchan);
1212 static int stm32_mdma_terminate_all(struct dma_chan *c)
1214 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
1215 unsigned long flags;
1218 spin_lock_irqsave(&chan->vchan.lock, flags);
1220 vchan_terminate_vdesc(&chan->desc->vdesc);
1222 stm32_mdma_stop(chan);
1225 vchan_get_all_descriptors(&chan->vchan, &head);
1226 spin_unlock_irqrestore(&chan->vchan.lock, flags);
1228 vchan_dma_desc_free_list(&chan->vchan, &head);
1233 static void stm32_mdma_synchronize(struct dma_chan *c)
1235 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
1237 vchan_synchronize(&chan->vchan);
1240 static int stm32_mdma_slave_config(struct dma_chan *c,
1241 struct dma_slave_config *config)
1243 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
1245 memcpy(&chan->dma_config, config, sizeof(*config));
1250 static size_t stm32_mdma_desc_residue(struct stm32_mdma_chan *chan,
1251 struct stm32_mdma_desc *desc,
1254 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
1255 struct stm32_mdma_hwdesc *hwdesc;
1256 u32 cbndtr, residue, modulo, burst_size;
1260 for (i = curr_hwdesc + 1; i < desc->count; i++) {
1261 hwdesc = desc->node[i].hwdesc;
1262 residue += STM32_MDMA_CBNDTR_BNDT(hwdesc->cbndtr);
1264 cbndtr = stm32_mdma_read(dmadev, STM32_MDMA_CBNDTR(chan->id));
1265 residue += cbndtr & STM32_MDMA_CBNDTR_BNDT_MASK;
1267 if (!chan->mem_burst)
1270 burst_size = chan->mem_burst * chan->mem_width;
1271 modulo = residue % burst_size;
1273 residue = residue - modulo + burst_size;
1278 static enum dma_status stm32_mdma_tx_status(struct dma_chan *c,
1279 dma_cookie_t cookie,
1280 struct dma_tx_state *state)
1282 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
1283 struct virt_dma_desc *vdesc;
1284 enum dma_status status;
1285 unsigned long flags;
1288 status = dma_cookie_status(c, cookie, state);
1289 if ((status == DMA_COMPLETE) || (!state))
1292 spin_lock_irqsave(&chan->vchan.lock, flags);
1294 vdesc = vchan_find_desc(&chan->vchan, cookie);
1295 if (chan->desc && cookie == chan->desc->vdesc.tx.cookie)
1296 residue = stm32_mdma_desc_residue(chan, chan->desc,
1299 residue = stm32_mdma_desc_residue(chan,
1300 to_stm32_mdma_desc(vdesc), 0);
1301 dma_set_residue(state, residue);
1303 spin_unlock_irqrestore(&chan->vchan.lock, flags);
1308 static void stm32_mdma_xfer_end(struct stm32_mdma_chan *chan)
1310 vchan_cookie_complete(&chan->desc->vdesc);
1314 /* Start the next transfer if this driver has a next desc */
1315 stm32_mdma_start_transfer(chan);
1318 static irqreturn_t stm32_mdma_irq_handler(int irq, void *devid)
1320 struct stm32_mdma_device *dmadev = devid;
1321 struct stm32_mdma_chan *chan;
1322 u32 reg, id, ccr, ien, status;
1324 /* Find out which channel generates the interrupt */
1325 status = readl_relaxed(dmadev->base + STM32_MDMA_GISR0);
1327 dev_dbg(mdma2dev(dmadev), "spurious it\n");
1331 chan = &dmadev->chan[id];
1333 /* Handle interrupt for the channel */
1334 spin_lock(&chan->vchan.lock);
1335 status = stm32_mdma_read(dmadev, STM32_MDMA_CISR(id));
1336 /* Mask Channel ReQuest Active bit which can be set in case of MEM2MEM */
1337 status &= ~STM32_MDMA_CISR_CRQA;
1338 ccr = stm32_mdma_read(dmadev, STM32_MDMA_CCR(id));
1339 ien = (ccr & STM32_MDMA_CCR_IRQ_MASK) >> 1;
1341 if (!(status & ien)) {
1342 spin_unlock(&chan->vchan.lock);
1344 dev_warn(chan2dev(chan),
1345 "spurious it (status=0x%04x, ien=0x%04x)\n", status, ien);
1347 dev_dbg(chan2dev(chan),
1348 "spurious it (status=0x%04x, ien=0x%04x)\n", status, ien);
1352 reg = STM32_MDMA_CIFCR(id);
1354 if (status & STM32_MDMA_CISR_TEIF) {
1355 dev_err(chan2dev(chan), "Transfer Err: stat=0x%08x\n",
1356 readl_relaxed(dmadev->base + STM32_MDMA_CESR(id)));
1357 stm32_mdma_set_bits(dmadev, reg, STM32_MDMA_CIFCR_CTEIF);
1358 status &= ~STM32_MDMA_CISR_TEIF;
1361 if (status & STM32_MDMA_CISR_CTCIF) {
1362 stm32_mdma_set_bits(dmadev, reg, STM32_MDMA_CIFCR_CCTCIF);
1363 status &= ~STM32_MDMA_CISR_CTCIF;
1364 stm32_mdma_xfer_end(chan);
1367 if (status & STM32_MDMA_CISR_BRTIF) {
1368 stm32_mdma_set_bits(dmadev, reg, STM32_MDMA_CIFCR_CBRTIF);
1369 status &= ~STM32_MDMA_CISR_BRTIF;
1372 if (status & STM32_MDMA_CISR_BTIF) {
1373 stm32_mdma_set_bits(dmadev, reg, STM32_MDMA_CIFCR_CBTIF);
1374 status &= ~STM32_MDMA_CISR_BTIF;
1375 chan->curr_hwdesc++;
1376 if (chan->desc && chan->desc->cyclic) {
1377 if (chan->curr_hwdesc == chan->desc->count)
1378 chan->curr_hwdesc = 0;
1379 vchan_cyclic_callback(&chan->desc->vdesc);
1383 if (status & STM32_MDMA_CISR_TCIF) {
1384 stm32_mdma_set_bits(dmadev, reg, STM32_MDMA_CIFCR_CLTCIF);
1385 status &= ~STM32_MDMA_CISR_TCIF;
1389 stm32_mdma_set_bits(dmadev, reg, status);
1390 dev_err(chan2dev(chan), "DMA error: status=0x%08x\n", status);
1391 if (!(ccr & STM32_MDMA_CCR_EN))
1392 dev_err(chan2dev(chan), "chan disabled by HW\n");
1395 spin_unlock(&chan->vchan.lock);
1400 static int stm32_mdma_alloc_chan_resources(struct dma_chan *c)
1402 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
1403 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
1406 chan->desc_pool = dmam_pool_create(dev_name(&c->dev->device),
1408 sizeof(struct stm32_mdma_hwdesc),
1409 __alignof__(struct stm32_mdma_hwdesc),
1411 if (!chan->desc_pool) {
1412 dev_err(chan2dev(chan), "failed to allocate descriptor pool\n");
1416 ret = pm_runtime_resume_and_get(dmadev->ddev.dev);
1420 ret = stm32_mdma_disable_chan(chan);
1422 pm_runtime_put(dmadev->ddev.dev);
1427 static void stm32_mdma_free_chan_resources(struct dma_chan *c)
1429 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
1430 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
1431 unsigned long flags;
1433 dev_dbg(chan2dev(chan), "Freeing channel %d\n", chan->id);
1436 spin_lock_irqsave(&chan->vchan.lock, flags);
1437 stm32_mdma_stop(chan);
1439 spin_unlock_irqrestore(&chan->vchan.lock, flags);
1442 pm_runtime_put(dmadev->ddev.dev);
1443 vchan_free_chan_resources(to_virt_chan(c));
1444 dmam_pool_destroy(chan->desc_pool);
1445 chan->desc_pool = NULL;
1448 static bool stm32_mdma_filter_fn(struct dma_chan *c, void *fn_param)
1450 struct stm32_mdma_chan *chan = to_stm32_mdma_chan(c);
1451 struct stm32_mdma_device *dmadev = stm32_mdma_get_dev(chan);
1453 /* Check if chan is marked Secure */
1454 if (dmadev->chan_reserved & BIT(chan->id))
1460 static struct dma_chan *stm32_mdma_of_xlate(struct of_phandle_args *dma_spec,
1461 struct of_dma *ofdma)
1463 struct stm32_mdma_device *dmadev = ofdma->of_dma_data;
1464 dma_cap_mask_t mask = dmadev->ddev.cap_mask;
1465 struct stm32_mdma_chan *chan;
1467 struct stm32_mdma_chan_config config;
1469 if (dma_spec->args_count < 5) {
1470 dev_err(mdma2dev(dmadev), "Bad number of args\n");
1474 config.request = dma_spec->args[0];
1475 config.priority_level = dma_spec->args[1];
1476 config.transfer_config = dma_spec->args[2];
1477 config.mask_addr = dma_spec->args[3];
1478 config.mask_data = dma_spec->args[4];
1480 if (config.request >= dmadev->nr_requests) {
1481 dev_err(mdma2dev(dmadev), "Bad request line\n");
1485 if (config.priority_level > STM32_MDMA_VERY_HIGH_PRIORITY) {
1486 dev_err(mdma2dev(dmadev), "Priority level not supported\n");
1490 c = __dma_request_channel(&mask, stm32_mdma_filter_fn, &config, ofdma->of_node);
1492 dev_err(mdma2dev(dmadev), "No more channels available\n");
1496 chan = to_stm32_mdma_chan(c);
1497 chan->chan_config = config;
1502 static const struct of_device_id stm32_mdma_of_match[] = {
1503 { .compatible = "st,stm32h7-mdma", },
1506 MODULE_DEVICE_TABLE(of, stm32_mdma_of_match);
1508 static int stm32_mdma_probe(struct platform_device *pdev)
1510 struct stm32_mdma_chan *chan;
1511 struct stm32_mdma_device *dmadev;
1512 struct dma_device *dd;
1513 struct device_node *of_node;
1514 struct resource *res;
1515 struct reset_control *rst;
1516 u32 nr_channels, nr_requests;
1519 of_node = pdev->dev.of_node;
1523 ret = device_property_read_u32(&pdev->dev, "dma-channels",
1526 nr_channels = STM32_MDMA_MAX_CHANNELS;
1527 dev_warn(&pdev->dev, "MDMA defaulting on %i channels\n",
1531 ret = device_property_read_u32(&pdev->dev, "dma-requests",
1534 nr_requests = STM32_MDMA_MAX_REQUESTS;
1535 dev_warn(&pdev->dev, "MDMA defaulting on %i request lines\n",
1539 count = device_property_count_u32(&pdev->dev, "st,ahb-addr-masks");
1543 dmadev = devm_kzalloc(&pdev->dev,
1544 struct_size(dmadev, ahb_addr_masks, count),
1549 dmadev->nr_channels = nr_channels;
1550 dmadev->nr_requests = nr_requests;
1551 device_property_read_u32_array(&pdev->dev, "st,ahb-addr-masks",
1552 dmadev->ahb_addr_masks,
1554 dmadev->nr_ahb_addr_masks = count;
1556 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1557 dmadev->base = devm_ioremap_resource(&pdev->dev, res);
1558 if (IS_ERR(dmadev->base))
1559 return PTR_ERR(dmadev->base);
1561 dmadev->clk = devm_clk_get(&pdev->dev, NULL);
1562 if (IS_ERR(dmadev->clk))
1563 return dev_err_probe(&pdev->dev, PTR_ERR(dmadev->clk),
1564 "Missing clock controller\n");
1566 ret = clk_prepare_enable(dmadev->clk);
1568 dev_err(&pdev->dev, "clk_prep_enable error: %d\n", ret);
1572 rst = devm_reset_control_get(&pdev->dev, NULL);
1575 if (ret == -EPROBE_DEFER)
1578 reset_control_assert(rst);
1580 reset_control_deassert(rst);
1584 dma_cap_set(DMA_SLAVE, dd->cap_mask);
1585 dma_cap_set(DMA_PRIVATE, dd->cap_mask);
1586 dma_cap_set(DMA_CYCLIC, dd->cap_mask);
1587 dma_cap_set(DMA_MEMCPY, dd->cap_mask);
1588 dd->device_alloc_chan_resources = stm32_mdma_alloc_chan_resources;
1589 dd->device_free_chan_resources = stm32_mdma_free_chan_resources;
1590 dd->device_tx_status = stm32_mdma_tx_status;
1591 dd->device_issue_pending = stm32_mdma_issue_pending;
1592 dd->device_prep_slave_sg = stm32_mdma_prep_slave_sg;
1593 dd->device_prep_dma_cyclic = stm32_mdma_prep_dma_cyclic;
1594 dd->device_prep_dma_memcpy = stm32_mdma_prep_dma_memcpy;
1595 dd->device_config = stm32_mdma_slave_config;
1596 dd->device_pause = stm32_mdma_pause;
1597 dd->device_resume = stm32_mdma_resume;
1598 dd->device_terminate_all = stm32_mdma_terminate_all;
1599 dd->device_synchronize = stm32_mdma_synchronize;
1600 dd->descriptor_reuse = true;
1602 dd->src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1603 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1604 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |
1605 BIT(DMA_SLAVE_BUSWIDTH_8_BYTES);
1606 dd->dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1607 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1608 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |
1609 BIT(DMA_SLAVE_BUSWIDTH_8_BYTES);
1610 dd->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV) |
1611 BIT(DMA_MEM_TO_MEM);
1612 dd->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
1613 dd->max_burst = STM32_MDMA_MAX_BURST;
1614 dd->dev = &pdev->dev;
1615 INIT_LIST_HEAD(&dd->channels);
1617 for (i = 0; i < dmadev->nr_channels; i++) {
1618 chan = &dmadev->chan[i];
1621 if (stm32_mdma_read(dmadev, STM32_MDMA_CCR(i)) & STM32_MDMA_CCR_SM)
1622 dmadev->chan_reserved |= BIT(i);
1624 chan->vchan.desc_free = stm32_mdma_desc_free;
1625 vchan_init(&chan->vchan, dd);
1628 dmadev->irq = platform_get_irq(pdev, 0);
1629 if (dmadev->irq < 0) {
1634 ret = devm_request_irq(&pdev->dev, dmadev->irq, stm32_mdma_irq_handler,
1635 0, dev_name(&pdev->dev), dmadev);
1637 dev_err(&pdev->dev, "failed to request IRQ\n");
1641 ret = dmaenginem_async_device_register(dd);
1645 ret = of_dma_controller_register(of_node, stm32_mdma_of_xlate, dmadev);
1648 "STM32 MDMA DMA OF registration failed %d\n", ret);
1652 platform_set_drvdata(pdev, dmadev);
1653 pm_runtime_set_active(&pdev->dev);
1654 pm_runtime_enable(&pdev->dev);
1655 pm_runtime_get_noresume(&pdev->dev);
1656 pm_runtime_put(&pdev->dev);
1658 dev_info(&pdev->dev, "STM32 MDMA driver registered\n");
1663 clk_disable_unprepare(dmadev->clk);
1669 static int stm32_mdma_runtime_suspend(struct device *dev)
1671 struct stm32_mdma_device *dmadev = dev_get_drvdata(dev);
1673 clk_disable_unprepare(dmadev->clk);
1678 static int stm32_mdma_runtime_resume(struct device *dev)
1680 struct stm32_mdma_device *dmadev = dev_get_drvdata(dev);
1683 ret = clk_prepare_enable(dmadev->clk);
1685 dev_err(dev, "failed to prepare_enable clock\n");
1693 #ifdef CONFIG_PM_SLEEP
1694 static int stm32_mdma_pm_suspend(struct device *dev)
1696 struct stm32_mdma_device *dmadev = dev_get_drvdata(dev);
1700 ret = pm_runtime_resume_and_get(dev);
1704 for (id = 0; id < dmadev->nr_channels; id++) {
1705 ccr = stm32_mdma_read(dmadev, STM32_MDMA_CCR(id));
1706 if (ccr & STM32_MDMA_CCR_EN) {
1707 dev_warn(dev, "Suspend is prevented by Chan %i\n", id);
1712 pm_runtime_put_sync(dev);
1714 pm_runtime_force_suspend(dev);
1719 static int stm32_mdma_pm_resume(struct device *dev)
1721 return pm_runtime_force_resume(dev);
1725 static const struct dev_pm_ops stm32_mdma_pm_ops = {
1726 SET_SYSTEM_SLEEP_PM_OPS(stm32_mdma_pm_suspend, stm32_mdma_pm_resume)
1727 SET_RUNTIME_PM_OPS(stm32_mdma_runtime_suspend,
1728 stm32_mdma_runtime_resume, NULL)
1731 static struct platform_driver stm32_mdma_driver = {
1732 .probe = stm32_mdma_probe,
1734 .name = "stm32-mdma",
1735 .of_match_table = stm32_mdma_of_match,
1736 .pm = &stm32_mdma_pm_ops,
1740 static int __init stm32_mdma_init(void)
1742 return platform_driver_register(&stm32_mdma_driver);
1745 subsys_initcall(stm32_mdma_init);
1747 MODULE_DESCRIPTION("Driver for STM32 MDMA controller");
1748 MODULE_AUTHOR("M'boumba Cedric Madianga <cedric.madianga@gmail.com>");
1749 MODULE_AUTHOR("Pierre-Yves Mordret <pierre-yves.mordret@st.com>");
1750 MODULE_LICENSE("GPL v2");