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