blob: 4099948b6914b99b28e0465a265415c4179ceb90 [file] [log] [blame]
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001/*
2 * Driver for STM32 DMA controller
3 *
4 * Inspired by dma-jz4740.c and tegra20-apb-dma.c
5 *
6 * Copyright (C) M'boumba Cedric Madianga 2015
7 * Author: M'boumba Cedric Madianga <cedric.madianga@gmail.com>
8 *
9 * License terms: GNU General Public License (GPL), version 2
10 */
11
12#include <linux/clk.h>
13#include <linux/delay.h>
14#include <linux/dmaengine.h>
15#include <linux/dma-mapping.h>
16#include <linux/err.h>
17#include <linux/init.h>
18#include <linux/jiffies.h>
19#include <linux/list.h>
20#include <linux/module.h>
21#include <linux/of.h>
22#include <linux/of_device.h>
23#include <linux/of_dma.h>
24#include <linux/platform_device.h>
25#include <linux/reset.h>
26#include <linux/sched.h>
27#include <linux/slab.h>
28
29#include "virt-dma.h"
30
31#define STM32_DMA_LISR 0x0000 /* DMA Low Int Status Reg */
32#define STM32_DMA_HISR 0x0004 /* DMA High Int Status Reg */
33#define STM32_DMA_LIFCR 0x0008 /* DMA Low Int Flag Clear Reg */
34#define STM32_DMA_HIFCR 0x000c /* DMA High Int Flag Clear Reg */
35#define STM32_DMA_TCI BIT(5) /* Transfer Complete Interrupt */
36#define STM32_DMA_TEI BIT(3) /* Transfer Error Interrupt */
37#define STM32_DMA_DMEI BIT(2) /* Direct Mode Error Interrupt */
38#define STM32_DMA_FEI BIT(0) /* FIFO Error Interrupt */
39
40/* DMA Stream x Configuration Register */
41#define STM32_DMA_SCR(x) (0x0010 + 0x18 * (x)) /* x = 0..7 */
42#define STM32_DMA_SCR_REQ(n) ((n & 0x7) << 25)
43#define STM32_DMA_SCR_MBURST_MASK GENMASK(24, 23)
44#define STM32_DMA_SCR_MBURST(n) ((n & 0x3) << 23)
45#define STM32_DMA_SCR_PBURST_MASK GENMASK(22, 21)
46#define STM32_DMA_SCR_PBURST(n) ((n & 0x3) << 21)
47#define STM32_DMA_SCR_PL_MASK GENMASK(17, 16)
48#define STM32_DMA_SCR_PL(n) ((n & 0x3) << 16)
49#define STM32_DMA_SCR_MSIZE_MASK GENMASK(14, 13)
50#define STM32_DMA_SCR_MSIZE(n) ((n & 0x3) << 13)
51#define STM32_DMA_SCR_PSIZE_MASK GENMASK(12, 11)
52#define STM32_DMA_SCR_PSIZE(n) ((n & 0x3) << 11)
53#define STM32_DMA_SCR_PSIZE_GET(n) ((n & STM32_DMA_SCR_PSIZE_MASK) >> 11)
54#define STM32_DMA_SCR_DIR_MASK GENMASK(7, 6)
55#define STM32_DMA_SCR_DIR(n) ((n & 0x3) << 6)
56#define STM32_DMA_SCR_CT BIT(19) /* Target in double buffer */
57#define STM32_DMA_SCR_DBM BIT(18) /* Double Buffer Mode */
58#define STM32_DMA_SCR_PINCOS BIT(15) /* Peripheral inc offset size */
59#define STM32_DMA_SCR_MINC BIT(10) /* Memory increment mode */
60#define STM32_DMA_SCR_PINC BIT(9) /* Peripheral increment mode */
61#define STM32_DMA_SCR_CIRC BIT(8) /* Circular mode */
62#define STM32_DMA_SCR_PFCTRL BIT(5) /* Peripheral Flow Controller */
63#define STM32_DMA_SCR_TCIE BIT(4) /* Transfer Cplete Int Enable*/
64#define STM32_DMA_SCR_TEIE BIT(2) /* Transfer Error Int Enable */
65#define STM32_DMA_SCR_DMEIE BIT(1) /* Direct Mode Err Int Enable */
66#define STM32_DMA_SCR_EN BIT(0) /* Stream Enable */
67#define STM32_DMA_SCR_CFG_MASK (STM32_DMA_SCR_PINC \
68 | STM32_DMA_SCR_MINC \
69 | STM32_DMA_SCR_PINCOS \
70 | STM32_DMA_SCR_PL_MASK)
71#define STM32_DMA_SCR_IRQ_MASK (STM32_DMA_SCR_TCIE \
72 | STM32_DMA_SCR_TEIE \
73 | STM32_DMA_SCR_DMEIE)
74
75/* DMA Stream x number of data register */
76#define STM32_DMA_SNDTR(x) (0x0014 + 0x18 * (x))
77
78/* DMA stream peripheral address register */
79#define STM32_DMA_SPAR(x) (0x0018 + 0x18 * (x))
80
81/* DMA stream x memory 0 address register */
82#define STM32_DMA_SM0AR(x) (0x001c + 0x18 * (x))
83
84/* DMA stream x memory 1 address register */
85#define STM32_DMA_SM1AR(x) (0x0020 + 0x18 * (x))
86
87/* DMA stream x FIFO control register */
88#define STM32_DMA_SFCR(x) (0x0024 + 0x18 * (x))
89#define STM32_DMA_SFCR_FTH_MASK GENMASK(1, 0)
90#define STM32_DMA_SFCR_FTH(n) (n & STM32_DMA_SFCR_FTH_MASK)
91#define STM32_DMA_SFCR_FEIE BIT(7) /* FIFO error interrupt enable */
92#define STM32_DMA_SFCR_DMDIS BIT(2) /* Direct mode disable */
93#define STM32_DMA_SFCR_MASK (STM32_DMA_SFCR_FEIE \
94 | STM32_DMA_SFCR_DMDIS)
95
96/* DMA direction */
97#define STM32_DMA_DEV_TO_MEM 0x00
98#define STM32_DMA_MEM_TO_DEV 0x01
99#define STM32_DMA_MEM_TO_MEM 0x02
100
101/* DMA priority level */
102#define STM32_DMA_PRIORITY_LOW 0x00
103#define STM32_DMA_PRIORITY_MEDIUM 0x01
104#define STM32_DMA_PRIORITY_HIGH 0x02
105#define STM32_DMA_PRIORITY_VERY_HIGH 0x03
106
107/* DMA FIFO threshold selection */
108#define STM32_DMA_FIFO_THRESHOLD_1QUARTERFULL 0x00
109#define STM32_DMA_FIFO_THRESHOLD_HALFFULL 0x01
110#define STM32_DMA_FIFO_THRESHOLD_3QUARTERSFULL 0x02
111#define STM32_DMA_FIFO_THRESHOLD_FULL 0x03
112
113#define STM32_DMA_MAX_DATA_ITEMS 0xffff
114#define STM32_DMA_MAX_CHANNELS 0x08
115#define STM32_DMA_MAX_REQUEST_ID 0x08
116#define STM32_DMA_MAX_DATA_PARAM 0x03
M'boumba Cedric Madianga276b0042016-12-13 14:40:51 +0100117#define STM32_DMA_MAX_BURST 16
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200118
Pierre Yves MORDRET951f44c2018-03-13 17:42:01 +0100119/* DMA Features */
120#define STM32_DMA_THRESHOLD_FTR_MASK GENMASK(1, 0)
121#define STM32_DMA_THRESHOLD_FTR_GET(n) ((n) & STM32_DMA_THRESHOLD_FTR_MASK)
122
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200123enum stm32_dma_width {
124 STM32_DMA_BYTE,
125 STM32_DMA_HALF_WORD,
126 STM32_DMA_WORD,
127};
128
129enum stm32_dma_burst_size {
130 STM32_DMA_BURST_SINGLE,
131 STM32_DMA_BURST_INCR4,
132 STM32_DMA_BURST_INCR8,
133 STM32_DMA_BURST_INCR16,
134};
135
Pierre Yves MORDRET951f44c2018-03-13 17:42:01 +0100136/**
137 * struct stm32_dma_cfg - STM32 DMA custom configuration
138 * @channel_id: channel ID
139 * @request_line: DMA request
140 * @stream_config: 32bit mask specifying the DMA channel configuration
141 * @features: 32bit mask specifying the DMA Feature list
142 */
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200143struct stm32_dma_cfg {
144 u32 channel_id;
145 u32 request_line;
146 u32 stream_config;
Pierre Yves MORDRET951f44c2018-03-13 17:42:01 +0100147 u32 features;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200148};
149
150struct stm32_dma_chan_reg {
151 u32 dma_lisr;
152 u32 dma_hisr;
153 u32 dma_lifcr;
154 u32 dma_hifcr;
155 u32 dma_scr;
156 u32 dma_sndtr;
157 u32 dma_spar;
158 u32 dma_sm0ar;
159 u32 dma_sm1ar;
160 u32 dma_sfcr;
161};
162
163struct stm32_dma_sg_req {
164 u32 len;
165 struct stm32_dma_chan_reg chan_reg;
166};
167
168struct stm32_dma_desc {
169 struct virt_dma_desc vdesc;
170 bool cyclic;
171 u32 num_sgs;
172 struct stm32_dma_sg_req sg_req[];
173};
174
175struct stm32_dma_chan {
176 struct virt_dma_chan vchan;
177 bool config_init;
178 bool busy;
179 u32 id;
180 u32 irq;
181 struct stm32_dma_desc *desc;
182 u32 next_sg;
183 struct dma_slave_config dma_sconfig;
184 struct stm32_dma_chan_reg chan_reg;
Pierre Yves MORDRET951f44c2018-03-13 17:42:01 +0100185 u32 threshold;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200186};
187
188struct stm32_dma_device {
189 struct dma_device ddev;
190 void __iomem *base;
191 struct clk *clk;
192 struct reset_control *rst;
193 bool mem2mem;
194 struct stm32_dma_chan chan[STM32_DMA_MAX_CHANNELS];
195};
196
197static struct stm32_dma_device *stm32_dma_get_dev(struct stm32_dma_chan *chan)
198{
199 return container_of(chan->vchan.chan.device, struct stm32_dma_device,
200 ddev);
201}
202
203static struct stm32_dma_chan *to_stm32_dma_chan(struct dma_chan *c)
204{
205 return container_of(c, struct stm32_dma_chan, vchan.chan);
206}
207
208static struct stm32_dma_desc *to_stm32_dma_desc(struct virt_dma_desc *vdesc)
209{
210 return container_of(vdesc, struct stm32_dma_desc, vdesc);
211}
212
213static struct device *chan2dev(struct stm32_dma_chan *chan)
214{
215 return &chan->vchan.chan.dev->device;
216}
217
218static u32 stm32_dma_read(struct stm32_dma_device *dmadev, u32 reg)
219{
220 return readl_relaxed(dmadev->base + reg);
221}
222
223static void stm32_dma_write(struct stm32_dma_device *dmadev, u32 reg, u32 val)
224{
225 writel_relaxed(val, dmadev->base + reg);
226}
227
228static struct stm32_dma_desc *stm32_dma_alloc_desc(u32 num_sgs)
229{
230 return kzalloc(sizeof(struct stm32_dma_desc) +
231 sizeof(struct stm32_dma_sg_req) * num_sgs, GFP_NOWAIT);
232}
233
234static int stm32_dma_get_width(struct stm32_dma_chan *chan,
235 enum dma_slave_buswidth width)
236{
237 switch (width) {
238 case DMA_SLAVE_BUSWIDTH_1_BYTE:
239 return STM32_DMA_BYTE;
240 case DMA_SLAVE_BUSWIDTH_2_BYTES:
241 return STM32_DMA_HALF_WORD;
242 case DMA_SLAVE_BUSWIDTH_4_BYTES:
243 return STM32_DMA_WORD;
244 default:
245 dev_err(chan2dev(chan), "Dma bus width not supported\n");
246 return -EINVAL;
247 }
248}
249
250static int stm32_dma_get_burst(struct stm32_dma_chan *chan, u32 maxburst)
251{
252 switch (maxburst) {
253 case 0:
254 case 1:
255 return STM32_DMA_BURST_SINGLE;
256 case 4:
257 return STM32_DMA_BURST_INCR4;
258 case 8:
259 return STM32_DMA_BURST_INCR8;
260 case 16:
261 return STM32_DMA_BURST_INCR16;
262 default:
263 dev_err(chan2dev(chan), "Dma burst size not supported\n");
264 return -EINVAL;
265 }
266}
267
268static void stm32_dma_set_fifo_config(struct stm32_dma_chan *chan,
269 u32 src_maxburst, u32 dst_maxburst)
270{
271 chan->chan_reg.dma_sfcr &= ~STM32_DMA_SFCR_MASK;
272 chan->chan_reg.dma_scr &= ~STM32_DMA_SCR_DMEIE;
273
274 if ((!src_maxburst) && (!dst_maxburst)) {
275 /* Using direct mode */
276 chan->chan_reg.dma_scr |= STM32_DMA_SCR_DMEIE;
277 } else {
278 /* Using FIFO mode */
279 chan->chan_reg.dma_sfcr |= STM32_DMA_SFCR_MASK;
280 }
281}
282
283static int stm32_dma_slave_config(struct dma_chan *c,
284 struct dma_slave_config *config)
285{
286 struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
287
288 memcpy(&chan->dma_sconfig, config, sizeof(*config));
289
290 chan->config_init = true;
291
292 return 0;
293}
294
295static u32 stm32_dma_irq_status(struct stm32_dma_chan *chan)
296{
297 struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
298 u32 flags, dma_isr;
299
300 /*
301 * Read "flags" from DMA_xISR register corresponding to the selected
302 * DMA channel at the correct bit offset inside that register.
303 *
304 * If (ch % 4) is 2 or 3, left shift the mask by 16 bits.
305 * If (ch % 4) is 1 or 3, additionally left shift the mask by 6 bits.
306 */
307
308 if (chan->id & 4)
309 dma_isr = stm32_dma_read(dmadev, STM32_DMA_HISR);
310 else
311 dma_isr = stm32_dma_read(dmadev, STM32_DMA_LISR);
312
313 flags = dma_isr >> (((chan->id & 2) << 3) | ((chan->id & 1) * 6));
314
315 return flags;
316}
317
318static void stm32_dma_irq_clear(struct stm32_dma_chan *chan, u32 flags)
319{
320 struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
321 u32 dma_ifcr;
322
323 /*
324 * Write "flags" to the DMA_xIFCR register corresponding to the selected
325 * DMA channel at the correct bit offset inside that register.
326 *
327 * If (ch % 4) is 2 or 3, left shift the mask by 16 bits.
328 * If (ch % 4) is 1 or 3, additionally left shift the mask by 6 bits.
329 */
330 dma_ifcr = flags << (((chan->id & 2) << 3) | ((chan->id & 1) * 6));
331
332 if (chan->id & 4)
333 stm32_dma_write(dmadev, STM32_DMA_HIFCR, dma_ifcr);
334 else
335 stm32_dma_write(dmadev, STM32_DMA_LIFCR, dma_ifcr);
336}
337
338static int stm32_dma_disable_chan(struct stm32_dma_chan *chan)
339{
340 struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
341 unsigned long timeout = jiffies + msecs_to_jiffies(5000);
342 u32 dma_scr, id;
343
344 id = chan->id;
345 dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(id));
346
347 if (dma_scr & STM32_DMA_SCR_EN) {
348 dma_scr &= ~STM32_DMA_SCR_EN;
349 stm32_dma_write(dmadev, STM32_DMA_SCR(id), dma_scr);
350
351 do {
352 dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(id));
353 dma_scr &= STM32_DMA_SCR_EN;
354 if (!dma_scr)
355 break;
356
357 if (time_after_eq(jiffies, timeout)) {
358 dev_err(chan2dev(chan), "%s: timeout!\n",
359 __func__);
360 return -EBUSY;
361 }
362 cond_resched();
363 } while (1);
364 }
365
366 return 0;
367}
368
369static void stm32_dma_stop(struct stm32_dma_chan *chan)
370{
371 struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
372 u32 dma_scr, dma_sfcr, status;
373 int ret;
374
375 /* Disable interrupts */
376 dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id));
377 dma_scr &= ~STM32_DMA_SCR_IRQ_MASK;
378 stm32_dma_write(dmadev, STM32_DMA_SCR(chan->id), dma_scr);
379 dma_sfcr = stm32_dma_read(dmadev, STM32_DMA_SFCR(chan->id));
380 dma_sfcr &= ~STM32_DMA_SFCR_FEIE;
381 stm32_dma_write(dmadev, STM32_DMA_SFCR(chan->id), dma_sfcr);
382
383 /* Disable DMA */
384 ret = stm32_dma_disable_chan(chan);
385 if (ret < 0)
386 return;
387
388 /* Clear interrupt status if it is there */
389 status = stm32_dma_irq_status(chan);
390 if (status) {
391 dev_dbg(chan2dev(chan), "%s(): clearing interrupt: 0x%08x\n",
392 __func__, status);
393 stm32_dma_irq_clear(chan, status);
394 }
395
396 chan->busy = false;
397}
398
399static int stm32_dma_terminate_all(struct dma_chan *c)
400{
401 struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
402 unsigned long flags;
403 LIST_HEAD(head);
404
405 spin_lock_irqsave(&chan->vchan.lock, flags);
406
407 if (chan->busy) {
408 stm32_dma_stop(chan);
409 chan->desc = NULL;
410 }
411
412 vchan_get_all_descriptors(&chan->vchan, &head);
413 spin_unlock_irqrestore(&chan->vchan.lock, flags);
414 vchan_dma_desc_free_list(&chan->vchan, &head);
415
416 return 0;
417}
418
M'boumba Cedric Madiangadc808672016-12-13 14:40:50 +0100419static void stm32_dma_synchronize(struct dma_chan *c)
420{
421 struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
422
423 vchan_synchronize(&chan->vchan);
424}
425
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200426static void stm32_dma_dump_reg(struct stm32_dma_chan *chan)
427{
428 struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
429 u32 scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id));
430 u32 ndtr = stm32_dma_read(dmadev, STM32_DMA_SNDTR(chan->id));
431 u32 spar = stm32_dma_read(dmadev, STM32_DMA_SPAR(chan->id));
432 u32 sm0ar = stm32_dma_read(dmadev, STM32_DMA_SM0AR(chan->id));
433 u32 sm1ar = stm32_dma_read(dmadev, STM32_DMA_SM1AR(chan->id));
434 u32 sfcr = stm32_dma_read(dmadev, STM32_DMA_SFCR(chan->id));
435
436 dev_dbg(chan2dev(chan), "SCR: 0x%08x\n", scr);
437 dev_dbg(chan2dev(chan), "NDTR: 0x%08x\n", ndtr);
438 dev_dbg(chan2dev(chan), "SPAR: 0x%08x\n", spar);
439 dev_dbg(chan2dev(chan), "SM0AR: 0x%08x\n", sm0ar);
440 dev_dbg(chan2dev(chan), "SM1AR: 0x%08x\n", sm1ar);
441 dev_dbg(chan2dev(chan), "SFCR: 0x%08x\n", sfcr);
442}
443
M'boumba Cedric Madianga8d1b76f2016-12-13 14:40:47 +0100444static void stm32_dma_start_transfer(struct stm32_dma_chan *chan)
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200445{
446 struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
447 struct virt_dma_desc *vdesc;
448 struct stm32_dma_sg_req *sg_req;
449 struct stm32_dma_chan_reg *reg;
450 u32 status;
451 int ret;
452
453 ret = stm32_dma_disable_chan(chan);
454 if (ret < 0)
M'boumba Cedric Madianga8d1b76f2016-12-13 14:40:47 +0100455 return;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200456
457 if (!chan->desc) {
458 vdesc = vchan_next_desc(&chan->vchan);
459 if (!vdesc)
M'boumba Cedric Madianga8d1b76f2016-12-13 14:40:47 +0100460 return;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200461
462 chan->desc = to_stm32_dma_desc(vdesc);
463 chan->next_sg = 0;
464 }
465
466 if (chan->next_sg == chan->desc->num_sgs)
467 chan->next_sg = 0;
468
469 sg_req = &chan->desc->sg_req[chan->next_sg];
470 reg = &sg_req->chan_reg;
471
472 stm32_dma_write(dmadev, STM32_DMA_SCR(chan->id), reg->dma_scr);
473 stm32_dma_write(dmadev, STM32_DMA_SPAR(chan->id), reg->dma_spar);
474 stm32_dma_write(dmadev, STM32_DMA_SM0AR(chan->id), reg->dma_sm0ar);
475 stm32_dma_write(dmadev, STM32_DMA_SFCR(chan->id), reg->dma_sfcr);
476 stm32_dma_write(dmadev, STM32_DMA_SM1AR(chan->id), reg->dma_sm1ar);
477 stm32_dma_write(dmadev, STM32_DMA_SNDTR(chan->id), reg->dma_sndtr);
478
479 chan->next_sg++;
480
481 /* Clear interrupt status if it is there */
482 status = stm32_dma_irq_status(chan);
483 if (status)
484 stm32_dma_irq_clear(chan, status);
485
486 stm32_dma_dump_reg(chan);
487
488 /* Start DMA */
489 reg->dma_scr |= STM32_DMA_SCR_EN;
490 stm32_dma_write(dmadev, STM32_DMA_SCR(chan->id), reg->dma_scr);
491
492 chan->busy = true;
493
M'boumba Cedric Madianga8d1b76f2016-12-13 14:40:47 +0100494 dev_dbg(chan2dev(chan), "vchan %p: started\n", &chan->vchan);
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200495}
496
497static void stm32_dma_configure_next_sg(struct stm32_dma_chan *chan)
498{
499 struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
500 struct stm32_dma_sg_req *sg_req;
501 u32 dma_scr, dma_sm0ar, dma_sm1ar, id;
502
503 id = chan->id;
504 dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(id));
505
506 if (dma_scr & STM32_DMA_SCR_DBM) {
507 if (chan->next_sg == chan->desc->num_sgs)
508 chan->next_sg = 0;
509
510 sg_req = &chan->desc->sg_req[chan->next_sg];
511
512 if (dma_scr & STM32_DMA_SCR_CT) {
513 dma_sm0ar = sg_req->chan_reg.dma_sm0ar;
514 stm32_dma_write(dmadev, STM32_DMA_SM0AR(id), dma_sm0ar);
515 dev_dbg(chan2dev(chan), "CT=1 <=> SM0AR: 0x%08x\n",
516 stm32_dma_read(dmadev, STM32_DMA_SM0AR(id)));
517 } else {
518 dma_sm1ar = sg_req->chan_reg.dma_sm1ar;
519 stm32_dma_write(dmadev, STM32_DMA_SM1AR(id), dma_sm1ar);
520 dev_dbg(chan2dev(chan), "CT=0 <=> SM1AR: 0x%08x\n",
521 stm32_dma_read(dmadev, STM32_DMA_SM1AR(id)));
522 }
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200523 }
524}
525
526static void stm32_dma_handle_chan_done(struct stm32_dma_chan *chan)
527{
528 if (chan->desc) {
529 if (chan->desc->cyclic) {
530 vchan_cyclic_callback(&chan->desc->vdesc);
M'boumba Cedric Madianga2b12c5582016-12-13 14:40:48 +0100531 chan->next_sg++;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200532 stm32_dma_configure_next_sg(chan);
533 } else {
534 chan->busy = false;
535 if (chan->next_sg == chan->desc->num_sgs) {
536 list_del(&chan->desc->vdesc.node);
537 vchan_cookie_complete(&chan->desc->vdesc);
538 chan->desc = NULL;
539 }
540 stm32_dma_start_transfer(chan);
541 }
542 }
543}
544
545static irqreturn_t stm32_dma_chan_irq(int irq, void *devid)
546{
547 struct stm32_dma_chan *chan = devid;
548 struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
Vinod Koul1bc4f062016-12-09 15:24:12 +0530549 u32 status, scr;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200550
551 spin_lock(&chan->vchan.lock);
552
553 status = stm32_dma_irq_status(chan);
554 scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id));
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200555
556 if ((status & STM32_DMA_TCI) && (scr & STM32_DMA_SCR_TCIE)) {
557 stm32_dma_irq_clear(chan, STM32_DMA_TCI);
558 stm32_dma_handle_chan_done(chan);
559
560 } else {
561 stm32_dma_irq_clear(chan, status);
562 dev_err(chan2dev(chan), "DMA error: status=0x%08x\n", status);
563 }
564
565 spin_unlock(&chan->vchan.lock);
566
567 return IRQ_HANDLED;
568}
569
570static void stm32_dma_issue_pending(struct dma_chan *c)
571{
572 struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
573 unsigned long flags;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200574
575 spin_lock_irqsave(&chan->vchan.lock, flags);
M'boumba Cedric Madianga8d1b76f2016-12-13 14:40:47 +0100576 if (vchan_issue_pending(&chan->vchan) && !chan->desc && !chan->busy) {
577 dev_dbg(chan2dev(chan), "vchan %p: issued\n", &chan->vchan);
578 stm32_dma_start_transfer(chan);
579 if (chan->desc->cyclic)
580 stm32_dma_configure_next_sg(chan);
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200581 }
582 spin_unlock_irqrestore(&chan->vchan.lock, flags);
583}
584
585static int stm32_dma_set_xfer_param(struct stm32_dma_chan *chan,
586 enum dma_transfer_direction direction,
587 enum dma_slave_buswidth *buswidth)
588{
589 enum dma_slave_buswidth src_addr_width, dst_addr_width;
590 int src_bus_width, dst_bus_width;
591 int src_burst_size, dst_burst_size;
592 u32 src_maxburst, dst_maxburst;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200593 u32 dma_scr = 0;
594
595 src_addr_width = chan->dma_sconfig.src_addr_width;
596 dst_addr_width = chan->dma_sconfig.dst_addr_width;
597 src_maxburst = chan->dma_sconfig.src_maxburst;
598 dst_maxburst = chan->dma_sconfig.dst_maxburst;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200599
600 switch (direction) {
601 case DMA_MEM_TO_DEV:
602 dst_bus_width = stm32_dma_get_width(chan, dst_addr_width);
603 if (dst_bus_width < 0)
604 return dst_bus_width;
605
606 dst_burst_size = stm32_dma_get_burst(chan, dst_maxburst);
607 if (dst_burst_size < 0)
608 return dst_burst_size;
609
610 if (!src_addr_width)
611 src_addr_width = dst_addr_width;
612
613 src_bus_width = stm32_dma_get_width(chan, src_addr_width);
614 if (src_bus_width < 0)
615 return src_bus_width;
616
617 src_burst_size = stm32_dma_get_burst(chan, src_maxburst);
618 if (src_burst_size < 0)
619 return src_burst_size;
620
621 dma_scr = STM32_DMA_SCR_DIR(STM32_DMA_MEM_TO_DEV) |
622 STM32_DMA_SCR_PSIZE(dst_bus_width) |
623 STM32_DMA_SCR_MSIZE(src_bus_width) |
624 STM32_DMA_SCR_PBURST(dst_burst_size) |
625 STM32_DMA_SCR_MBURST(src_burst_size);
626
627 chan->chan_reg.dma_spar = chan->dma_sconfig.dst_addr;
628 *buswidth = dst_addr_width;
629 break;
630
631 case DMA_DEV_TO_MEM:
632 src_bus_width = stm32_dma_get_width(chan, src_addr_width);
633 if (src_bus_width < 0)
634 return src_bus_width;
635
636 src_burst_size = stm32_dma_get_burst(chan, src_maxburst);
637 if (src_burst_size < 0)
638 return src_burst_size;
639
640 if (!dst_addr_width)
641 dst_addr_width = src_addr_width;
642
643 dst_bus_width = stm32_dma_get_width(chan, dst_addr_width);
644 if (dst_bus_width < 0)
645 return dst_bus_width;
646
647 dst_burst_size = stm32_dma_get_burst(chan, dst_maxburst);
648 if (dst_burst_size < 0)
649 return dst_burst_size;
650
651 dma_scr = STM32_DMA_SCR_DIR(STM32_DMA_DEV_TO_MEM) |
652 STM32_DMA_SCR_PSIZE(src_bus_width) |
653 STM32_DMA_SCR_MSIZE(dst_bus_width) |
654 STM32_DMA_SCR_PBURST(src_burst_size) |
655 STM32_DMA_SCR_MBURST(dst_burst_size);
656
657 chan->chan_reg.dma_spar = chan->dma_sconfig.src_addr;
658 *buswidth = chan->dma_sconfig.src_addr_width;
659 break;
660
661 default:
662 dev_err(chan2dev(chan), "Dma direction is not supported\n");
663 return -EINVAL;
664 }
665
666 stm32_dma_set_fifo_config(chan, src_maxburst, dst_maxburst);
667
668 chan->chan_reg.dma_scr &= ~(STM32_DMA_SCR_DIR_MASK |
669 STM32_DMA_SCR_PSIZE_MASK | STM32_DMA_SCR_MSIZE_MASK |
670 STM32_DMA_SCR_PBURST_MASK | STM32_DMA_SCR_MBURST_MASK);
671 chan->chan_reg.dma_scr |= dma_scr;
672
673 return 0;
674}
675
676static void stm32_dma_clear_reg(struct stm32_dma_chan_reg *regs)
677{
678 memset(regs, 0, sizeof(struct stm32_dma_chan_reg));
679}
680
681static struct dma_async_tx_descriptor *stm32_dma_prep_slave_sg(
682 struct dma_chan *c, struct scatterlist *sgl,
683 u32 sg_len, enum dma_transfer_direction direction,
684 unsigned long flags, void *context)
685{
686 struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
687 struct stm32_dma_desc *desc;
688 struct scatterlist *sg;
689 enum dma_slave_buswidth buswidth;
690 u32 nb_data_items;
691 int i, ret;
692
693 if (!chan->config_init) {
694 dev_err(chan2dev(chan), "dma channel is not configured\n");
695 return NULL;
696 }
697
698 if (sg_len < 1) {
699 dev_err(chan2dev(chan), "Invalid segment length %d\n", sg_len);
700 return NULL;
701 }
702
703 desc = stm32_dma_alloc_desc(sg_len);
704 if (!desc)
705 return NULL;
706
707 ret = stm32_dma_set_xfer_param(chan, direction, &buswidth);
708 if (ret < 0)
709 goto err;
710
711 /* Set peripheral flow controller */
712 if (chan->dma_sconfig.device_fc)
713 chan->chan_reg.dma_scr |= STM32_DMA_SCR_PFCTRL;
714 else
715 chan->chan_reg.dma_scr &= ~STM32_DMA_SCR_PFCTRL;
716
717 for_each_sg(sgl, sg, sg_len, i) {
718 desc->sg_req[i].len = sg_dma_len(sg);
719
720 nb_data_items = desc->sg_req[i].len / buswidth;
721 if (nb_data_items > STM32_DMA_MAX_DATA_ITEMS) {
722 dev_err(chan2dev(chan), "nb items not supported\n");
723 goto err;
724 }
725
726 stm32_dma_clear_reg(&desc->sg_req[i].chan_reg);
727 desc->sg_req[i].chan_reg.dma_scr = chan->chan_reg.dma_scr;
728 desc->sg_req[i].chan_reg.dma_sfcr = chan->chan_reg.dma_sfcr;
729 desc->sg_req[i].chan_reg.dma_spar = chan->chan_reg.dma_spar;
730 desc->sg_req[i].chan_reg.dma_sm0ar = sg_dma_address(sg);
731 desc->sg_req[i].chan_reg.dma_sm1ar = sg_dma_address(sg);
732 desc->sg_req[i].chan_reg.dma_sndtr = nb_data_items;
733 }
734
735 desc->num_sgs = sg_len;
736 desc->cyclic = false;
737
738 return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
739
740err:
741 kfree(desc);
742 return NULL;
743}
744
745static struct dma_async_tx_descriptor *stm32_dma_prep_dma_cyclic(
746 struct dma_chan *c, dma_addr_t buf_addr, size_t buf_len,
747 size_t period_len, enum dma_transfer_direction direction,
748 unsigned long flags)
749{
750 struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
751 struct stm32_dma_desc *desc;
752 enum dma_slave_buswidth buswidth;
753 u32 num_periods, nb_data_items;
754 int i, ret;
755
756 if (!buf_len || !period_len) {
757 dev_err(chan2dev(chan), "Invalid buffer/period len\n");
758 return NULL;
759 }
760
761 if (!chan->config_init) {
762 dev_err(chan2dev(chan), "dma channel is not configured\n");
763 return NULL;
764 }
765
766 if (buf_len % period_len) {
767 dev_err(chan2dev(chan), "buf_len not multiple of period_len\n");
768 return NULL;
769 }
770
771 /*
772 * We allow to take more number of requests till DMA is
773 * not started. The driver will loop over all requests.
774 * Once DMA is started then new requests can be queued only after
775 * terminating the DMA.
776 */
777 if (chan->busy) {
778 dev_err(chan2dev(chan), "Request not allowed when dma busy\n");
779 return NULL;
780 }
781
782 ret = stm32_dma_set_xfer_param(chan, direction, &buswidth);
783 if (ret < 0)
784 return NULL;
785
786 nb_data_items = period_len / buswidth;
787 if (nb_data_items > STM32_DMA_MAX_DATA_ITEMS) {
788 dev_err(chan2dev(chan), "number of items not supported\n");
789 return NULL;
790 }
791
792 /* Enable Circular mode or double buffer mode */
793 if (buf_len == period_len)
794 chan->chan_reg.dma_scr |= STM32_DMA_SCR_CIRC;
795 else
796 chan->chan_reg.dma_scr |= STM32_DMA_SCR_DBM;
797
798 /* Clear periph ctrl if client set it */
799 chan->chan_reg.dma_scr &= ~STM32_DMA_SCR_PFCTRL;
800
801 num_periods = buf_len / period_len;
802
803 desc = stm32_dma_alloc_desc(num_periods);
804 if (!desc)
805 return NULL;
806
807 for (i = 0; i < num_periods; i++) {
808 desc->sg_req[i].len = period_len;
809
810 stm32_dma_clear_reg(&desc->sg_req[i].chan_reg);
811 desc->sg_req[i].chan_reg.dma_scr = chan->chan_reg.dma_scr;
812 desc->sg_req[i].chan_reg.dma_sfcr = chan->chan_reg.dma_sfcr;
813 desc->sg_req[i].chan_reg.dma_spar = chan->chan_reg.dma_spar;
814 desc->sg_req[i].chan_reg.dma_sm0ar = buf_addr;
815 desc->sg_req[i].chan_reg.dma_sm1ar = buf_addr;
816 desc->sg_req[i].chan_reg.dma_sndtr = nb_data_items;
817 buf_addr += period_len;
818 }
819
820 desc->num_sgs = num_periods;
821 desc->cyclic = true;
822
823 return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
824}
825
826static struct dma_async_tx_descriptor *stm32_dma_prep_dma_memcpy(
827 struct dma_chan *c, dma_addr_t dest,
828 dma_addr_t src, size_t len, unsigned long flags)
829{
830 struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
831 u32 num_sgs;
832 struct stm32_dma_desc *desc;
833 size_t xfer_count, offset;
834 int i;
835
836 num_sgs = DIV_ROUND_UP(len, STM32_DMA_MAX_DATA_ITEMS);
837 desc = stm32_dma_alloc_desc(num_sgs);
838 if (!desc)
839 return NULL;
840
841 for (offset = 0, i = 0; offset < len; offset += xfer_count, i++) {
842 xfer_count = min_t(size_t, len - offset,
843 STM32_DMA_MAX_DATA_ITEMS);
844
845 desc->sg_req[i].len = xfer_count;
846
847 stm32_dma_clear_reg(&desc->sg_req[i].chan_reg);
848 desc->sg_req[i].chan_reg.dma_scr =
849 STM32_DMA_SCR_DIR(STM32_DMA_MEM_TO_MEM) |
850 STM32_DMA_SCR_MINC |
851 STM32_DMA_SCR_PINC |
852 STM32_DMA_SCR_TCIE |
853 STM32_DMA_SCR_TEIE;
854 desc->sg_req[i].chan_reg.dma_sfcr = STM32_DMA_SFCR_DMDIS |
855 STM32_DMA_SFCR_FTH(STM32_DMA_FIFO_THRESHOLD_FULL) |
856 STM32_DMA_SFCR_FEIE;
857 desc->sg_req[i].chan_reg.dma_spar = src + offset;
858 desc->sg_req[i].chan_reg.dma_sm0ar = dest + offset;
859 desc->sg_req[i].chan_reg.dma_sndtr = xfer_count;
860 }
861
862 desc->num_sgs = num_sgs;
863 desc->cyclic = false;
864
865 return vchan_tx_prep(&chan->vchan, &desc->vdesc, flags);
866}
867
M'boumba Cedric Madianga2b12c5582016-12-13 14:40:48 +0100868static u32 stm32_dma_get_remaining_bytes(struct stm32_dma_chan *chan)
869{
870 u32 dma_scr, width, ndtr;
871 struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
872
873 dma_scr = stm32_dma_read(dmadev, STM32_DMA_SCR(chan->id));
874 width = STM32_DMA_SCR_PSIZE_GET(dma_scr);
875 ndtr = stm32_dma_read(dmadev, STM32_DMA_SNDTR(chan->id));
876
877 return ndtr << width;
878}
879
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200880static size_t stm32_dma_desc_residue(struct stm32_dma_chan *chan,
881 struct stm32_dma_desc *desc,
882 u32 next_sg)
883{
M'boumba Cedric Madianga2b12c5582016-12-13 14:40:48 +0100884 u32 residue = 0;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200885 int i;
886
M'boumba Cedric Madianga2b12c5582016-12-13 14:40:48 +0100887 /*
888 * In cyclic mode, for the last period, residue = remaining bytes from
889 * NDTR
890 */
891 if (chan->desc->cyclic && next_sg == 0)
892 return stm32_dma_get_remaining_bytes(chan);
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200893
M'boumba Cedric Madianga2b12c5582016-12-13 14:40:48 +0100894 /*
895 * For all other periods in cyclic mode, and in sg mode,
896 * residue = remaining bytes from NDTR + remaining periods/sg to be
897 * transferred
898 */
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200899 for (i = next_sg; i < desc->num_sgs; i++)
900 residue += desc->sg_req[i].len;
M'boumba Cedric Madianga2b12c5582016-12-13 14:40:48 +0100901 residue += stm32_dma_get_remaining_bytes(chan);
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200902
903 return residue;
904}
905
906static enum dma_status stm32_dma_tx_status(struct dma_chan *c,
907 dma_cookie_t cookie,
908 struct dma_tx_state *state)
909{
910 struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
911 struct virt_dma_desc *vdesc;
912 enum dma_status status;
913 unsigned long flags;
M'boumba Cedric Madianga57b5a322016-12-13 14:40:46 +0100914 u32 residue = 0;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200915
916 status = dma_cookie_status(c, cookie, state);
917 if ((status == DMA_COMPLETE) || (!state))
918 return status;
919
920 spin_lock_irqsave(&chan->vchan.lock, flags);
921 vdesc = vchan_find_desc(&chan->vchan, cookie);
M'boumba Cedric Madianga57b5a322016-12-13 14:40:46 +0100922 if (chan->desc && cookie == chan->desc->vdesc.tx.cookie)
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200923 residue = stm32_dma_desc_residue(chan, chan->desc,
924 chan->next_sg);
M'boumba Cedric Madianga57b5a322016-12-13 14:40:46 +0100925 else if (vdesc)
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200926 residue = stm32_dma_desc_residue(chan,
927 to_stm32_dma_desc(vdesc), 0);
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200928 dma_set_residue(state, residue);
929
930 spin_unlock_irqrestore(&chan->vchan.lock, flags);
931
932 return status;
933}
934
935static int stm32_dma_alloc_chan_resources(struct dma_chan *c)
936{
937 struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
938 struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
939 int ret;
940
941 chan->config_init = false;
942 ret = clk_prepare_enable(dmadev->clk);
943 if (ret < 0) {
944 dev_err(chan2dev(chan), "clk_prepare_enable failed: %d\n", ret);
945 return ret;
946 }
947
948 ret = stm32_dma_disable_chan(chan);
949 if (ret < 0)
950 clk_disable_unprepare(dmadev->clk);
951
952 return ret;
953}
954
955static void stm32_dma_free_chan_resources(struct dma_chan *c)
956{
957 struct stm32_dma_chan *chan = to_stm32_dma_chan(c);
958 struct stm32_dma_device *dmadev = stm32_dma_get_dev(chan);
959 unsigned long flags;
960
961 dev_dbg(chan2dev(chan), "Freeing channel %d\n", chan->id);
962
963 if (chan->busy) {
964 spin_lock_irqsave(&chan->vchan.lock, flags);
965 stm32_dma_stop(chan);
966 chan->desc = NULL;
967 spin_unlock_irqrestore(&chan->vchan.lock, flags);
968 }
969
970 clk_disable_unprepare(dmadev->clk);
971
972 vchan_free_chan_resources(to_virt_chan(c));
973}
974
975static void stm32_dma_desc_free(struct virt_dma_desc *vdesc)
976{
977 kfree(container_of(vdesc, struct stm32_dma_desc, vdesc));
978}
979
Vinod Koule97adb42016-09-02 15:59:10 +0530980static void stm32_dma_set_config(struct stm32_dma_chan *chan,
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200981 struct stm32_dma_cfg *cfg)
982{
983 stm32_dma_clear_reg(&chan->chan_reg);
984
985 chan->chan_reg.dma_scr = cfg->stream_config & STM32_DMA_SCR_CFG_MASK;
986 chan->chan_reg.dma_scr |= STM32_DMA_SCR_REQ(cfg->request_line);
987
988 /* Enable Interrupts */
989 chan->chan_reg.dma_scr |= STM32_DMA_SCR_TEIE | STM32_DMA_SCR_TCIE;
990
Pierre Yves MORDRET951f44c2018-03-13 17:42:01 +0100991 chan->threshold = STM32_DMA_THRESHOLD_FTR_GET(cfg->features);
992 chan->chan_reg.dma_sfcr = STM32_DMA_SFCR_FTH(chan->threshold);
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +0200993}
994
995static struct dma_chan *stm32_dma_of_xlate(struct of_phandle_args *dma_spec,
996 struct of_dma *ofdma)
997{
998 struct stm32_dma_device *dmadev = ofdma->of_dma_data;
M'boumba Cedric Madianga5df4eb42017-01-05 09:09:40 +0100999 struct device *dev = dmadev->ddev.dev;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001000 struct stm32_dma_cfg cfg;
1001 struct stm32_dma_chan *chan;
1002 struct dma_chan *c;
1003
M'boumba Cedric Madianga5df4eb42017-01-05 09:09:40 +01001004 if (dma_spec->args_count < 4) {
1005 dev_err(dev, "Bad number of cells\n");
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001006 return NULL;
M'boumba Cedric Madianga5df4eb42017-01-05 09:09:40 +01001007 }
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001008
1009 cfg.channel_id = dma_spec->args[0];
1010 cfg.request_line = dma_spec->args[1];
1011 cfg.stream_config = dma_spec->args[2];
Pierre Yves MORDRET951f44c2018-03-13 17:42:01 +01001012 cfg.features = dma_spec->args[3];
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001013
M'boumba Cedric Madianga5df4eb42017-01-05 09:09:40 +01001014 if ((cfg.channel_id >= STM32_DMA_MAX_CHANNELS) ||
1015 (cfg.request_line >= STM32_DMA_MAX_REQUEST_ID)) {
1016 dev_err(dev, "Bad channel and/or request id\n");
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001017 return NULL;
M'boumba Cedric Madianga5df4eb42017-01-05 09:09:40 +01001018 }
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001019
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001020 chan = &dmadev->chan[cfg.channel_id];
1021
1022 c = dma_get_slave_channel(&chan->vchan.chan);
M'boumba Cedric Madianga5df4eb42017-01-05 09:09:40 +01001023 if (!c) {
Colin Ian King041cf7e2017-02-21 18:30:45 +00001024 dev_err(dev, "No more channels available\n");
M'boumba Cedric Madianga5df4eb42017-01-05 09:09:40 +01001025 return NULL;
1026 }
1027
1028 stm32_dma_set_config(chan, &cfg);
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001029
1030 return c;
1031}
1032
1033static const struct of_device_id stm32_dma_of_match[] = {
1034 { .compatible = "st,stm32-dma", },
1035 { /* sentinel */ },
1036};
1037MODULE_DEVICE_TABLE(of, stm32_dma_of_match);
1038
1039static int stm32_dma_probe(struct platform_device *pdev)
1040{
1041 struct stm32_dma_chan *chan;
1042 struct stm32_dma_device *dmadev;
1043 struct dma_device *dd;
1044 const struct of_device_id *match;
1045 struct resource *res;
1046 int i, ret;
1047
1048 match = of_match_device(stm32_dma_of_match, &pdev->dev);
1049 if (!match) {
1050 dev_err(&pdev->dev, "Error: No device match found\n");
1051 return -ENODEV;
1052 }
1053
1054 dmadev = devm_kzalloc(&pdev->dev, sizeof(*dmadev), GFP_KERNEL);
1055 if (!dmadev)
1056 return -ENOMEM;
1057
1058 dd = &dmadev->ddev;
1059
1060 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1061 dmadev->base = devm_ioremap_resource(&pdev->dev, res);
1062 if (IS_ERR(dmadev->base))
1063 return PTR_ERR(dmadev->base);
1064
1065 dmadev->clk = devm_clk_get(&pdev->dev, NULL);
1066 if (IS_ERR(dmadev->clk)) {
1067 dev_err(&pdev->dev, "Error: Missing controller clock\n");
1068 return PTR_ERR(dmadev->clk);
1069 }
1070
1071 dmadev->mem2mem = of_property_read_bool(pdev->dev.of_node,
1072 "st,mem2mem");
1073
1074 dmadev->rst = devm_reset_control_get(&pdev->dev, NULL);
1075 if (!IS_ERR(dmadev->rst)) {
1076 reset_control_assert(dmadev->rst);
1077 udelay(2);
1078 reset_control_deassert(dmadev->rst);
1079 }
1080
1081 dma_cap_set(DMA_SLAVE, dd->cap_mask);
1082 dma_cap_set(DMA_PRIVATE, dd->cap_mask);
1083 dma_cap_set(DMA_CYCLIC, dd->cap_mask);
1084 dd->device_alloc_chan_resources = stm32_dma_alloc_chan_resources;
1085 dd->device_free_chan_resources = stm32_dma_free_chan_resources;
1086 dd->device_tx_status = stm32_dma_tx_status;
1087 dd->device_issue_pending = stm32_dma_issue_pending;
1088 dd->device_prep_slave_sg = stm32_dma_prep_slave_sg;
1089 dd->device_prep_dma_cyclic = stm32_dma_prep_dma_cyclic;
1090 dd->device_config = stm32_dma_slave_config;
1091 dd->device_terminate_all = stm32_dma_terminate_all;
M'boumba Cedric Madiangadc808672016-12-13 14:40:50 +01001092 dd->device_synchronize = stm32_dma_synchronize;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001093 dd->src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1094 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1095 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
1096 dd->dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1097 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1098 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
1099 dd->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
1100 dd->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
M'boumba Cedric Madianga276b0042016-12-13 14:40:51 +01001101 dd->max_burst = STM32_DMA_MAX_BURST;
M'boumba Cedric Madiangad8b46832015-10-16 15:59:14 +02001102 dd->dev = &pdev->dev;
1103 INIT_LIST_HEAD(&dd->channels);
1104
1105 if (dmadev->mem2mem) {
1106 dma_cap_set(DMA_MEMCPY, dd->cap_mask);
1107 dd->device_prep_dma_memcpy = stm32_dma_prep_dma_memcpy;
1108 dd->directions |= BIT(DMA_MEM_TO_MEM);
1109 }
1110
1111 for (i = 0; i < STM32_DMA_MAX_CHANNELS; i++) {
1112 chan = &dmadev->chan[i];
1113 chan->id = i;
1114 chan->vchan.desc_free = stm32_dma_desc_free;
1115 vchan_init(&chan->vchan, dd);
1116 }
1117
1118 ret = dma_async_device_register(dd);
1119 if (ret)
1120 return ret;
1121
1122 for (i = 0; i < STM32_DMA_MAX_CHANNELS; i++) {
1123 chan = &dmadev->chan[i];
1124 res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
1125 if (!res) {
1126 ret = -EINVAL;
1127 dev_err(&pdev->dev, "No irq resource for chan %d\n", i);
1128 goto err_unregister;
1129 }
1130 chan->irq = res->start;
1131 ret = devm_request_irq(&pdev->dev, chan->irq,
1132 stm32_dma_chan_irq, 0,
1133 dev_name(chan2dev(chan)), chan);
1134 if (ret) {
1135 dev_err(&pdev->dev,
1136 "request_irq failed with err %d channel %d\n",
1137 ret, i);
1138 goto err_unregister;
1139 }
1140 }
1141
1142 ret = of_dma_controller_register(pdev->dev.of_node,
1143 stm32_dma_of_xlate, dmadev);
1144 if (ret < 0) {
1145 dev_err(&pdev->dev,
1146 "STM32 DMA DMA OF registration failed %d\n", ret);
1147 goto err_unregister;
1148 }
1149
1150 platform_set_drvdata(pdev, dmadev);
1151
1152 dev_info(&pdev->dev, "STM32 DMA driver registered\n");
1153
1154 return 0;
1155
1156err_unregister:
1157 dma_async_device_unregister(dd);
1158
1159 return ret;
1160}
1161
1162static struct platform_driver stm32_dma_driver = {
1163 .driver = {
1164 .name = "stm32-dma",
1165 .of_match_table = stm32_dma_of_match,
1166 },
1167};
1168
1169static int __init stm32_dma_init(void)
1170{
1171 return platform_driver_probe(&stm32_dma_driver, stm32_dma_probe);
1172}
1173subsys_initcall(stm32_dma_init);