1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * drivers/dma/fsl-edma.c
4 *
5 * Copyright 2013-2014 Freescale Semiconductor, Inc.
6 *
7 * Driver for the Freescale eDMA engine with flexible channel multiplexing
8 * capability for DMA request sources. The eDMA block can be found on some
9 * Vybrid and Layerscape SoCs.
10 */
11
12 #include <dt-bindings/dma/fsl-edma.h>
13 #include <linux/bitfield.h>
14 #include <linux/module.h>
15 #include <linux/interrupt.h>
16 #include <linux/clk.h>
17 #include <linux/of.h>
18 #include <linux/of_device.h>
19 #include <linux/of_address.h>
20 #include <linux/of_irq.h>
21 #include <linux/of_dma.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/pm_domain.h>
25
26 #include "fsl-edma-common.h"
27
fsl_edma_synchronize(struct dma_chan * chan)28 static void fsl_edma_synchronize(struct dma_chan *chan)
29 {
30 struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
31
32 vchan_synchronize(&fsl_chan->vchan);
33 }
34
fsl_edma_tx_handler(int irq,void * dev_id)35 static irqreturn_t fsl_edma_tx_handler(int irq, void *dev_id)
36 {
37 struct fsl_edma_engine *fsl_edma = dev_id;
38 unsigned int intr, ch;
39 struct edma_regs *regs = &fsl_edma->regs;
40
41 intr = edma_readl(fsl_edma, regs->intl);
42 if (!intr)
43 return IRQ_NONE;
44
45 for (ch = 0; ch < fsl_edma->n_chans; ch++) {
46 if (intr & (0x1 << ch)) {
47 edma_writeb(fsl_edma, EDMA_CINT_CINT(ch), regs->cint);
48 fsl_edma_tx_chan_handler(&fsl_edma->chans[ch]);
49 }
50 }
51 return IRQ_HANDLED;
52 }
53
fsl_edma3_tx_handler(int irq,void * dev_id)54 static irqreturn_t fsl_edma3_tx_handler(int irq, void *dev_id)
55 {
56 struct fsl_edma_chan *fsl_chan = dev_id;
57 unsigned int intr;
58
59 intr = edma_readl_chreg(fsl_chan, ch_int);
60 if (!intr)
61 return IRQ_HANDLED;
62
63 edma_writel_chreg(fsl_chan, 1, ch_int);
64
65 fsl_edma_tx_chan_handler(fsl_chan);
66
67 return IRQ_HANDLED;
68 }
69
fsl_edma_err_handler(int irq,void * dev_id)70 static irqreturn_t fsl_edma_err_handler(int irq, void *dev_id)
71 {
72 struct fsl_edma_engine *fsl_edma = dev_id;
73 unsigned int err, ch;
74 struct edma_regs *regs = &fsl_edma->regs;
75
76 err = edma_readl(fsl_edma, regs->errl);
77 if (!err)
78 return IRQ_NONE;
79
80 for (ch = 0; ch < fsl_edma->n_chans; ch++) {
81 if (err & (0x1 << ch)) {
82 fsl_edma_disable_request(&fsl_edma->chans[ch]);
83 edma_writeb(fsl_edma, EDMA_CERR_CERR(ch), regs->cerr);
84 fsl_edma_err_chan_handler(&fsl_edma->chans[ch]);
85 }
86 }
87 return IRQ_HANDLED;
88 }
89
fsl_edma_irq_handler(int irq,void * dev_id)90 static irqreturn_t fsl_edma_irq_handler(int irq, void *dev_id)
91 {
92 if (fsl_edma_tx_handler(irq, dev_id) == IRQ_HANDLED)
93 return IRQ_HANDLED;
94
95 return fsl_edma_err_handler(irq, dev_id);
96 }
97
fsl_edma_xlate(struct of_phandle_args * dma_spec,struct of_dma * ofdma)98 static struct dma_chan *fsl_edma_xlate(struct of_phandle_args *dma_spec,
99 struct of_dma *ofdma)
100 {
101 struct fsl_edma_engine *fsl_edma = ofdma->of_dma_data;
102 struct dma_chan *chan, *_chan;
103 struct fsl_edma_chan *fsl_chan;
104 u32 dmamux_nr = fsl_edma->drvdata->dmamuxs;
105 unsigned long chans_per_mux = fsl_edma->n_chans / dmamux_nr;
106
107 if (dma_spec->args_count != 2)
108 return NULL;
109
110 mutex_lock(&fsl_edma->fsl_edma_mutex);
111 list_for_each_entry_safe(chan, _chan, &fsl_edma->dma_dev.channels, device_node) {
112 if (chan->client_count)
113 continue;
114 if ((chan->chan_id / chans_per_mux) == dma_spec->args[0]) {
115 chan = dma_get_slave_channel(chan);
116 if (chan) {
117 chan->device->privatecnt++;
118 fsl_chan = to_fsl_edma_chan(chan);
119 fsl_chan->slave_id = dma_spec->args[1];
120 fsl_edma_chan_mux(fsl_chan, fsl_chan->slave_id,
121 true);
122 mutex_unlock(&fsl_edma->fsl_edma_mutex);
123 return chan;
124 }
125 }
126 }
127 mutex_unlock(&fsl_edma->fsl_edma_mutex);
128 return NULL;
129 }
130
fsl_edma3_xlate(struct of_phandle_args * dma_spec,struct of_dma * ofdma)131 static struct dma_chan *fsl_edma3_xlate(struct of_phandle_args *dma_spec,
132 struct of_dma *ofdma)
133 {
134 struct fsl_edma_engine *fsl_edma = ofdma->of_dma_data;
135 struct dma_chan *chan, *_chan;
136 struct fsl_edma_chan *fsl_chan;
137 bool b_chmux;
138 int i;
139
140 if (dma_spec->args_count != 3)
141 return NULL;
142
143 b_chmux = !!(fsl_edma->drvdata->flags & FSL_EDMA_DRV_HAS_CHMUX);
144
145 mutex_lock(&fsl_edma->fsl_edma_mutex);
146 list_for_each_entry_safe(chan, _chan, &fsl_edma->dma_dev.channels,
147 device_node) {
148
149 if (chan->client_count)
150 continue;
151
152 fsl_chan = to_fsl_edma_chan(chan);
153 i = fsl_chan - fsl_edma->chans;
154
155 fsl_chan->priority = dma_spec->args[1];
156 fsl_chan->is_rxchan = dma_spec->args[2] & FSL_EDMA_RX;
157 fsl_chan->is_remote = dma_spec->args[2] & FSL_EDMA_REMOTE;
158 fsl_chan->is_multi_fifo = dma_spec->args[2] & FSL_EDMA_MULTI_FIFO;
159
160 if ((dma_spec->args[2] & FSL_EDMA_EVEN_CH) && (i & 0x1))
161 continue;
162
163 if ((dma_spec->args[2] & FSL_EDMA_ODD_CH) && !(i & 0x1))
164 continue;
165
166 if (!b_chmux && i == dma_spec->args[0]) {
167 chan = dma_get_slave_channel(chan);
168 chan->device->privatecnt++;
169 mutex_unlock(&fsl_edma->fsl_edma_mutex);
170 return chan;
171 } else if (b_chmux && !fsl_chan->srcid) {
172 /* if controller support channel mux, choose a free channel */
173 chan = dma_get_slave_channel(chan);
174 chan->device->privatecnt++;
175 fsl_chan->srcid = dma_spec->args[0];
176 mutex_unlock(&fsl_edma->fsl_edma_mutex);
177 return chan;
178 }
179 }
180 mutex_unlock(&fsl_edma->fsl_edma_mutex);
181 return NULL;
182 }
183
184 static int
fsl_edma_irq_init(struct platform_device * pdev,struct fsl_edma_engine * fsl_edma)185 fsl_edma_irq_init(struct platform_device *pdev, struct fsl_edma_engine *fsl_edma)
186 {
187 int ret;
188
189 edma_writel(fsl_edma, ~0, fsl_edma->regs.intl);
190
191 fsl_edma->txirq = platform_get_irq_byname(pdev, "edma-tx");
192 if (fsl_edma->txirq < 0)
193 return fsl_edma->txirq;
194
195 fsl_edma->errirq = platform_get_irq_byname(pdev, "edma-err");
196 if (fsl_edma->errirq < 0)
197 return fsl_edma->errirq;
198
199 if (fsl_edma->txirq == fsl_edma->errirq) {
200 ret = devm_request_irq(&pdev->dev, fsl_edma->txirq,
201 fsl_edma_irq_handler, 0, "eDMA", fsl_edma);
202 if (ret) {
203 dev_err(&pdev->dev, "Can't register eDMA IRQ.\n");
204 return ret;
205 }
206 } else {
207 ret = devm_request_irq(&pdev->dev, fsl_edma->txirq,
208 fsl_edma_tx_handler, 0, "eDMA tx", fsl_edma);
209 if (ret) {
210 dev_err(&pdev->dev, "Can't register eDMA tx IRQ.\n");
211 return ret;
212 }
213
214 ret = devm_request_irq(&pdev->dev, fsl_edma->errirq,
215 fsl_edma_err_handler, 0, "eDMA err", fsl_edma);
216 if (ret) {
217 dev_err(&pdev->dev, "Can't register eDMA err IRQ.\n");
218 return ret;
219 }
220 }
221
222 return 0;
223 }
224
fsl_edma3_irq_init(struct platform_device * pdev,struct fsl_edma_engine * fsl_edma)225 static int fsl_edma3_irq_init(struct platform_device *pdev, struct fsl_edma_engine *fsl_edma)
226 {
227 int ret;
228 int i;
229
230 for (i = 0; i < fsl_edma->n_chans; i++) {
231
232 struct fsl_edma_chan *fsl_chan = &fsl_edma->chans[i];
233
234 if (fsl_edma->chan_masked & BIT(i))
235 continue;
236
237 /* request channel irq */
238 fsl_chan->txirq = platform_get_irq(pdev, i);
239 if (fsl_chan->txirq < 0) {
240 dev_err(&pdev->dev, "Can't get chan %d's irq.\n", i);
241 return -EINVAL;
242 }
243
244 ret = devm_request_irq(&pdev->dev, fsl_chan->txirq,
245 fsl_edma3_tx_handler, IRQF_SHARED,
246 fsl_chan->chan_name, fsl_chan);
247 if (ret) {
248 dev_err(&pdev->dev, "Can't register chan%d's IRQ.\n", i);
249 return -EINVAL;
250 }
251 }
252
253 return 0;
254 }
255
256 static int
fsl_edma2_irq_init(struct platform_device * pdev,struct fsl_edma_engine * fsl_edma)257 fsl_edma2_irq_init(struct platform_device *pdev,
258 struct fsl_edma_engine *fsl_edma)
259 {
260 int i, ret, irq;
261 int count;
262
263 edma_writel(fsl_edma, ~0, fsl_edma->regs.intl);
264
265 count = platform_irq_count(pdev);
266 dev_dbg(&pdev->dev, "%s Found %d interrupts\r\n", __func__, count);
267 if (count <= 2) {
268 dev_err(&pdev->dev, "Interrupts in DTS not correct.\n");
269 return -EINVAL;
270 }
271 /*
272 * 16 channel independent interrupts + 1 error interrupt on i.mx7ulp.
273 * 2 channel share one interrupt, for example, ch0/ch16, ch1/ch17...
274 * For now, just simply request irq without IRQF_SHARED flag, since 16
275 * channels are enough on i.mx7ulp whose M4 domain own some peripherals.
276 */
277 for (i = 0; i < count; i++) {
278 irq = platform_get_irq(pdev, i);
279 if (irq < 0)
280 return -ENXIO;
281
282 /* The last IRQ is for eDMA err */
283 if (i == count - 1)
284 ret = devm_request_irq(&pdev->dev, irq,
285 fsl_edma_err_handler,
286 0, "eDMA2-ERR", fsl_edma);
287 else
288 ret = devm_request_irq(&pdev->dev, irq,
289 fsl_edma_tx_handler, 0,
290 fsl_edma->chans[i].chan_name,
291 fsl_edma);
292 if (ret)
293 return ret;
294 }
295
296 return 0;
297 }
298
fsl_edma_irq_exit(struct platform_device * pdev,struct fsl_edma_engine * fsl_edma)299 static void fsl_edma_irq_exit(
300 struct platform_device *pdev, struct fsl_edma_engine *fsl_edma)
301 {
302 if (fsl_edma->txirq == fsl_edma->errirq) {
303 devm_free_irq(&pdev->dev, fsl_edma->txirq, fsl_edma);
304 } else {
305 devm_free_irq(&pdev->dev, fsl_edma->txirq, fsl_edma);
306 devm_free_irq(&pdev->dev, fsl_edma->errirq, fsl_edma);
307 }
308 }
309
fsl_disable_clocks(struct fsl_edma_engine * fsl_edma,int nr_clocks)310 static void fsl_disable_clocks(struct fsl_edma_engine *fsl_edma, int nr_clocks)
311 {
312 int i;
313
314 for (i = 0; i < nr_clocks; i++)
315 clk_disable_unprepare(fsl_edma->muxclk[i]);
316 }
317
318 static struct fsl_edma_drvdata vf610_data = {
319 .dmamuxs = DMAMUX_NR,
320 .flags = FSL_EDMA_DRV_WRAP_IO,
321 .chreg_off = EDMA_TCD,
322 .chreg_space_sz = sizeof(struct fsl_edma_hw_tcd),
323 .setup_irq = fsl_edma_irq_init,
324 };
325
326 static struct fsl_edma_drvdata ls1028a_data = {
327 .dmamuxs = DMAMUX_NR,
328 .flags = FSL_EDMA_DRV_MUX_SWAP | FSL_EDMA_DRV_WRAP_IO,
329 .chreg_off = EDMA_TCD,
330 .chreg_space_sz = sizeof(struct fsl_edma_hw_tcd),
331 .setup_irq = fsl_edma_irq_init,
332 };
333
334 static struct fsl_edma_drvdata imx7ulp_data = {
335 .dmamuxs = 1,
336 .chreg_off = EDMA_TCD,
337 .chreg_space_sz = sizeof(struct fsl_edma_hw_tcd),
338 .flags = FSL_EDMA_DRV_HAS_DMACLK | FSL_EDMA_DRV_CONFIG32,
339 .setup_irq = fsl_edma2_irq_init,
340 };
341
342 static struct fsl_edma_drvdata imx8qm_data = {
343 .flags = FSL_EDMA_DRV_HAS_PD | FSL_EDMA_DRV_EDMA3 | FSL_EDMA_DRV_MEM_REMOTE,
344 .chreg_space_sz = 0x10000,
345 .chreg_off = 0x10000,
346 .setup_irq = fsl_edma3_irq_init,
347 };
348
349 static struct fsl_edma_drvdata imx8ulp_data = {
350 .flags = FSL_EDMA_DRV_HAS_CHMUX | FSL_EDMA_DRV_HAS_CHCLK | FSL_EDMA_DRV_HAS_DMACLK |
351 FSL_EDMA_DRV_EDMA3,
352 .chreg_space_sz = 0x10000,
353 .chreg_off = 0x10000,
354 .mux_off = 0x10000 + offsetof(struct fsl_edma3_ch_reg, ch_mux),
355 .mux_skip = 0x10000,
356 .setup_irq = fsl_edma3_irq_init,
357 };
358
359 static struct fsl_edma_drvdata imx93_data3 = {
360 .flags = FSL_EDMA_DRV_HAS_DMACLK | FSL_EDMA_DRV_EDMA3,
361 .chreg_space_sz = 0x10000,
362 .chreg_off = 0x10000,
363 .setup_irq = fsl_edma3_irq_init,
364 };
365
366 static struct fsl_edma_drvdata imx93_data4 = {
367 .flags = FSL_EDMA_DRV_HAS_CHMUX | FSL_EDMA_DRV_HAS_DMACLK | FSL_EDMA_DRV_EDMA4,
368 .chreg_space_sz = 0x8000,
369 .chreg_off = 0x10000,
370 .mux_off = 0x10000 + offsetof(struct fsl_edma3_ch_reg, ch_mux),
371 .mux_skip = 0x8000,
372 .setup_irq = fsl_edma3_irq_init,
373 };
374
375 static const struct of_device_id fsl_edma_dt_ids[] = {
376 { .compatible = "fsl,vf610-edma", .data = &vf610_data},
377 { .compatible = "fsl,ls1028a-edma", .data = &ls1028a_data},
378 { .compatible = "fsl,imx7ulp-edma", .data = &imx7ulp_data},
379 { .compatible = "fsl,imx8qm-edma", .data = &imx8qm_data},
380 { .compatible = "fsl,imx8ulp-edma", .data = &imx8ulp_data},
381 { .compatible = "fsl,imx93-edma3", .data = &imx93_data3},
382 { .compatible = "fsl,imx93-edma4", .data = &imx93_data4},
383 { /* sentinel */ }
384 };
385 MODULE_DEVICE_TABLE(of, fsl_edma_dt_ids);
386
fsl_edma3_detach_pd(struct fsl_edma_engine * fsl_edma)387 static void fsl_edma3_detach_pd(struct fsl_edma_engine *fsl_edma)
388 {
389 struct fsl_edma_chan *fsl_chan;
390 int i;
391
392 for (i = 0; i < fsl_edma->n_chans; i++) {
393 if (fsl_edma->chan_masked & BIT(i))
394 continue;
395 fsl_chan = &fsl_edma->chans[i];
396 if (fsl_chan->pd_dev_link)
397 device_link_del(fsl_chan->pd_dev_link);
398 if (fsl_chan->pd_dev) {
399 dev_pm_domain_detach(fsl_chan->pd_dev, false);
400 pm_runtime_dont_use_autosuspend(fsl_chan->pd_dev);
401 pm_runtime_set_suspended(fsl_chan->pd_dev);
402 }
403 }
404 }
405
devm_fsl_edma3_detach_pd(void * data)406 static void devm_fsl_edma3_detach_pd(void *data)
407 {
408 fsl_edma3_detach_pd(data);
409 }
410
fsl_edma3_attach_pd(struct platform_device * pdev,struct fsl_edma_engine * fsl_edma)411 static int fsl_edma3_attach_pd(struct platform_device *pdev, struct fsl_edma_engine *fsl_edma)
412 {
413 struct fsl_edma_chan *fsl_chan;
414 struct device *pd_chan;
415 struct device *dev;
416 int i;
417
418 dev = &pdev->dev;
419
420 for (i = 0; i < fsl_edma->n_chans; i++) {
421 if (fsl_edma->chan_masked & BIT(i))
422 continue;
423
424 fsl_chan = &fsl_edma->chans[i];
425
426 pd_chan = dev_pm_domain_attach_by_id(dev, i);
427 if (IS_ERR_OR_NULL(pd_chan)) {
428 dev_err(dev, "Failed attach pd %d\n", i);
429 goto detach;
430 }
431
432 fsl_chan->pd_dev_link = device_link_add(dev, pd_chan, DL_FLAG_STATELESS |
433 DL_FLAG_PM_RUNTIME |
434 DL_FLAG_RPM_ACTIVE);
435 if (!fsl_chan->pd_dev_link) {
436 dev_err(dev, "Failed to add device_link to %d\n", i);
437 dev_pm_domain_detach(pd_chan, false);
438 goto detach;
439 }
440
441 fsl_chan->pd_dev = pd_chan;
442
443 pm_runtime_use_autosuspend(fsl_chan->pd_dev);
444 pm_runtime_set_autosuspend_delay(fsl_chan->pd_dev, 200);
445 pm_runtime_set_active(fsl_chan->pd_dev);
446 }
447
448 return 0;
449
450 detach:
451 fsl_edma3_detach_pd(fsl_edma);
452 return -EINVAL;
453 }
454
fsl_edma_probe(struct platform_device * pdev)455 static int fsl_edma_probe(struct platform_device *pdev)
456 {
457 const struct of_device_id *of_id =
458 of_match_device(fsl_edma_dt_ids, &pdev->dev);
459 struct device_node *np = pdev->dev.of_node;
460 struct fsl_edma_engine *fsl_edma;
461 const struct fsl_edma_drvdata *drvdata = NULL;
462 u32 chan_mask[2] = {0, 0};
463 char clk_name[36];
464 struct edma_regs *regs;
465 int chans;
466 int ret, i;
467
468 if (of_id)
469 drvdata = of_id->data;
470 if (!drvdata) {
471 dev_err(&pdev->dev, "unable to find driver data\n");
472 return -EINVAL;
473 }
474
475 ret = of_property_read_u32(np, "dma-channels", &chans);
476 if (ret) {
477 dev_err(&pdev->dev, "Can't get dma-channels.\n");
478 return ret;
479 }
480
481 fsl_edma = devm_kzalloc(&pdev->dev, struct_size(fsl_edma, chans, chans),
482 GFP_KERNEL);
483 if (!fsl_edma)
484 return -ENOMEM;
485
486 fsl_edma->drvdata = drvdata;
487 fsl_edma->n_chans = chans;
488 mutex_init(&fsl_edma->fsl_edma_mutex);
489
490 fsl_edma->membase = devm_platform_ioremap_resource(pdev, 0);
491 if (IS_ERR(fsl_edma->membase))
492 return PTR_ERR(fsl_edma->membase);
493
494 if (!(drvdata->flags & FSL_EDMA_DRV_SPLIT_REG)) {
495 fsl_edma_setup_regs(fsl_edma);
496 regs = &fsl_edma->regs;
497 }
498
499 if (drvdata->flags & FSL_EDMA_DRV_HAS_DMACLK) {
500 fsl_edma->dmaclk = devm_clk_get_enabled(&pdev->dev, "dma");
501 if (IS_ERR(fsl_edma->dmaclk)) {
502 dev_err(&pdev->dev, "Missing DMA block clock.\n");
503 return PTR_ERR(fsl_edma->dmaclk);
504 }
505 }
506
507 if (drvdata->flags & FSL_EDMA_DRV_HAS_CHCLK) {
508 fsl_edma->chclk = devm_clk_get_enabled(&pdev->dev, "mp");
509 if (IS_ERR(fsl_edma->chclk)) {
510 dev_err(&pdev->dev, "Missing MP block clock.\n");
511 return PTR_ERR(fsl_edma->chclk);
512 }
513 }
514
515 ret = of_property_read_variable_u32_array(np, "dma-channel-mask", chan_mask, 1, 2);
516
517 if (ret > 0) {
518 fsl_edma->chan_masked = chan_mask[1];
519 fsl_edma->chan_masked <<= 32;
520 fsl_edma->chan_masked |= chan_mask[0];
521 }
522
523 for (i = 0; i < fsl_edma->drvdata->dmamuxs; i++) {
524 char clkname[32];
525
526 /* eDMAv3 mux register move to TCD area if ch_mux exist */
527 if (drvdata->flags & FSL_EDMA_DRV_SPLIT_REG)
528 break;
529
530 fsl_edma->muxbase[i] = devm_platform_ioremap_resource(pdev,
531 1 + i);
532 if (IS_ERR(fsl_edma->muxbase[i])) {
533 /* on error: disable all previously enabled clks */
534 fsl_disable_clocks(fsl_edma, i);
535 return PTR_ERR(fsl_edma->muxbase[i]);
536 }
537
538 sprintf(clkname, "dmamux%d", i);
539 fsl_edma->muxclk[i] = devm_clk_get_enabled(&pdev->dev, clkname);
540 if (IS_ERR(fsl_edma->muxclk[i])) {
541 dev_err(&pdev->dev, "Missing DMAMUX block clock.\n");
542 /* on error: disable all previously enabled clks */
543 return PTR_ERR(fsl_edma->muxclk[i]);
544 }
545 }
546
547 fsl_edma->big_endian = of_property_read_bool(np, "big-endian");
548
549 if (drvdata->flags & FSL_EDMA_DRV_HAS_PD) {
550 ret = fsl_edma3_attach_pd(pdev, fsl_edma);
551 if (ret)
552 return ret;
553 ret = devm_add_action_or_reset(&pdev->dev, devm_fsl_edma3_detach_pd, fsl_edma);
554 if (ret)
555 return ret;
556 }
557
558 INIT_LIST_HEAD(&fsl_edma->dma_dev.channels);
559 for (i = 0; i < fsl_edma->n_chans; i++) {
560 struct fsl_edma_chan *fsl_chan = &fsl_edma->chans[i];
561 int len;
562
563 if (fsl_edma->chan_masked & BIT(i))
564 continue;
565
566 snprintf(fsl_chan->chan_name, sizeof(fsl_chan->chan_name), "%s-CH%02d",
567 dev_name(&pdev->dev), i);
568
569 fsl_chan->edma = fsl_edma;
570 fsl_chan->pm_state = RUNNING;
571 fsl_chan->slave_id = 0;
572 fsl_chan->idle = true;
573 fsl_chan->dma_dir = DMA_NONE;
574 fsl_chan->vchan.desc_free = fsl_edma_free_desc;
575
576 len = (drvdata->flags & FSL_EDMA_DRV_SPLIT_REG) ?
577 offsetof(struct fsl_edma3_ch_reg, tcd) : 0;
578 fsl_chan->tcd = fsl_edma->membase
579 + i * drvdata->chreg_space_sz + drvdata->chreg_off + len;
580 fsl_chan->mux_addr = fsl_edma->membase + drvdata->mux_off + i * drvdata->mux_skip;
581
582 if (drvdata->flags & FSL_EDMA_DRV_HAS_CHCLK) {
583 snprintf(clk_name, sizeof(clk_name), "ch%02d", i);
584 fsl_chan->clk = devm_clk_get_enabled(&pdev->dev,
585 (const char *)clk_name);
586
587 if (IS_ERR(fsl_chan->clk))
588 return PTR_ERR(fsl_chan->clk);
589 }
590 fsl_chan->pdev = pdev;
591 vchan_init(&fsl_chan->vchan, &fsl_edma->dma_dev);
592
593 edma_write_tcdreg(fsl_chan, 0, csr);
594 fsl_edma_chan_mux(fsl_chan, 0, false);
595 if (fsl_chan->edma->drvdata->flags & FSL_EDMA_DRV_HAS_CHCLK)
596 clk_disable_unprepare(fsl_chan->clk);
597 }
598
599 ret = fsl_edma->drvdata->setup_irq(pdev, fsl_edma);
600 if (ret)
601 return ret;
602
603 dma_cap_set(DMA_PRIVATE, fsl_edma->dma_dev.cap_mask);
604 dma_cap_set(DMA_SLAVE, fsl_edma->dma_dev.cap_mask);
605 dma_cap_set(DMA_CYCLIC, fsl_edma->dma_dev.cap_mask);
606 dma_cap_set(DMA_MEMCPY, fsl_edma->dma_dev.cap_mask);
607
608 fsl_edma->dma_dev.dev = &pdev->dev;
609 fsl_edma->dma_dev.device_alloc_chan_resources
610 = fsl_edma_alloc_chan_resources;
611 fsl_edma->dma_dev.device_free_chan_resources
612 = fsl_edma_free_chan_resources;
613 fsl_edma->dma_dev.device_tx_status = fsl_edma_tx_status;
614 fsl_edma->dma_dev.device_prep_slave_sg = fsl_edma_prep_slave_sg;
615 fsl_edma->dma_dev.device_prep_dma_cyclic = fsl_edma_prep_dma_cyclic;
616 fsl_edma->dma_dev.device_prep_dma_memcpy = fsl_edma_prep_memcpy;
617 fsl_edma->dma_dev.device_config = fsl_edma_slave_config;
618 fsl_edma->dma_dev.device_pause = fsl_edma_pause;
619 fsl_edma->dma_dev.device_resume = fsl_edma_resume;
620 fsl_edma->dma_dev.device_terminate_all = fsl_edma_terminate_all;
621 fsl_edma->dma_dev.device_synchronize = fsl_edma_synchronize;
622 fsl_edma->dma_dev.device_issue_pending = fsl_edma_issue_pending;
623
624 fsl_edma->dma_dev.src_addr_widths = FSL_EDMA_BUSWIDTHS;
625 fsl_edma->dma_dev.dst_addr_widths = FSL_EDMA_BUSWIDTHS;
626
627 if (drvdata->flags & FSL_EDMA_DRV_BUS_8BYTE) {
628 fsl_edma->dma_dev.src_addr_widths |= BIT(DMA_SLAVE_BUSWIDTH_8_BYTES);
629 fsl_edma->dma_dev.dst_addr_widths |= BIT(DMA_SLAVE_BUSWIDTH_8_BYTES);
630 }
631
632 fsl_edma->dma_dev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
633 if (drvdata->flags & FSL_EDMA_DRV_DEV_TO_DEV)
634 fsl_edma->dma_dev.directions |= BIT(DMA_DEV_TO_DEV);
635
636 fsl_edma->dma_dev.copy_align = drvdata->flags & FSL_EDMA_DRV_ALIGN_64BYTE ?
637 DMAENGINE_ALIGN_64_BYTES :
638 DMAENGINE_ALIGN_32_BYTES;
639
640 /* Per worst case 'nbytes = 1' take CITER as the max_seg_size */
641 dma_set_max_seg_size(fsl_edma->dma_dev.dev,
642 FIELD_GET(EDMA_TCD_ITER_MASK, EDMA_TCD_ITER_MASK));
643
644 fsl_edma->dma_dev.residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
645
646 platform_set_drvdata(pdev, fsl_edma);
647
648 ret = dma_async_device_register(&fsl_edma->dma_dev);
649 if (ret) {
650 dev_err(&pdev->dev,
651 "Can't register Freescale eDMA engine. (%d)\n", ret);
652 return ret;
653 }
654
655 ret = of_dma_controller_register(np,
656 drvdata->flags & FSL_EDMA_DRV_SPLIT_REG ? fsl_edma3_xlate : fsl_edma_xlate,
657 fsl_edma);
658 if (ret) {
659 dev_err(&pdev->dev,
660 "Can't register Freescale eDMA of_dma. (%d)\n", ret);
661 dma_async_device_unregister(&fsl_edma->dma_dev);
662 return ret;
663 }
664
665 /* enable round robin arbitration */
666 if (!(drvdata->flags & FSL_EDMA_DRV_SPLIT_REG))
667 edma_writel(fsl_edma, EDMA_CR_ERGA | EDMA_CR_ERCA, regs->cr);
668
669 return 0;
670 }
671
fsl_edma_remove(struct platform_device * pdev)672 static int fsl_edma_remove(struct platform_device *pdev)
673 {
674 struct device_node *np = pdev->dev.of_node;
675 struct fsl_edma_engine *fsl_edma = platform_get_drvdata(pdev);
676
677 fsl_edma_irq_exit(pdev, fsl_edma);
678 fsl_edma_cleanup_vchan(&fsl_edma->dma_dev);
679 of_dma_controller_free(np);
680 dma_async_device_unregister(&fsl_edma->dma_dev);
681 fsl_disable_clocks(fsl_edma, fsl_edma->drvdata->dmamuxs);
682
683 return 0;
684 }
685
fsl_edma_suspend_late(struct device * dev)686 static int fsl_edma_suspend_late(struct device *dev)
687 {
688 struct fsl_edma_engine *fsl_edma = dev_get_drvdata(dev);
689 struct fsl_edma_chan *fsl_chan;
690 unsigned long flags;
691 int i;
692
693 for (i = 0; i < fsl_edma->n_chans; i++) {
694 fsl_chan = &fsl_edma->chans[i];
695 if (fsl_edma->chan_masked & BIT(i))
696 continue;
697 spin_lock_irqsave(&fsl_chan->vchan.lock, flags);
698 /* Make sure chan is idle or will force disable. */
699 if (unlikely(!fsl_chan->idle)) {
700 dev_warn(dev, "WARN: There is non-idle channel.");
701 fsl_edma_disable_request(fsl_chan);
702 fsl_edma_chan_mux(fsl_chan, 0, false);
703 }
704
705 fsl_chan->pm_state = SUSPENDED;
706 spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags);
707 }
708
709 return 0;
710 }
711
fsl_edma_resume_early(struct device * dev)712 static int fsl_edma_resume_early(struct device *dev)
713 {
714 struct fsl_edma_engine *fsl_edma = dev_get_drvdata(dev);
715 struct fsl_edma_chan *fsl_chan;
716 struct edma_regs *regs = &fsl_edma->regs;
717 int i;
718
719 for (i = 0; i < fsl_edma->n_chans; i++) {
720 fsl_chan = &fsl_edma->chans[i];
721 if (fsl_edma->chan_masked & BIT(i))
722 continue;
723 fsl_chan->pm_state = RUNNING;
724 edma_write_tcdreg(fsl_chan, 0, csr);
725 if (fsl_chan->slave_id != 0)
726 fsl_edma_chan_mux(fsl_chan, fsl_chan->slave_id, true);
727 }
728
729 if (!(fsl_edma->drvdata->flags & FSL_EDMA_DRV_SPLIT_REG))
730 edma_writel(fsl_edma, EDMA_CR_ERGA | EDMA_CR_ERCA, regs->cr);
731
732 return 0;
733 }
734
735 /*
736 * eDMA provides the service to others, so it should be suspend late
737 * and resume early. When eDMA suspend, all of the clients should stop
738 * the DMA data transmission and let the channel idle.
739 */
740 static const struct dev_pm_ops fsl_edma_pm_ops = {
741 .suspend_late = fsl_edma_suspend_late,
742 .resume_early = fsl_edma_resume_early,
743 };
744
745 static struct platform_driver fsl_edma_driver = {
746 .driver = {
747 .name = "fsl-edma",
748 .of_match_table = fsl_edma_dt_ids,
749 .pm = &fsl_edma_pm_ops,
750 },
751 .probe = fsl_edma_probe,
752 .remove = fsl_edma_remove,
753 };
754
fsl_edma_init(void)755 static int __init fsl_edma_init(void)
756 {
757 return platform_driver_register(&fsl_edma_driver);
758 }
759 subsys_initcall(fsl_edma_init);
760
fsl_edma_exit(void)761 static void __exit fsl_edma_exit(void)
762 {
763 platform_driver_unregister(&fsl_edma_driver);
764 }
765 module_exit(fsl_edma_exit);
766
767 MODULE_ALIAS("platform:fsl-edma");
768 MODULE_DESCRIPTION("Freescale eDMA engine driver");
769 MODULE_LICENSE("GPL v2");
770