blob: e40769666e3933f649692ddb29a8e4968817eacb [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jingchang Lud6be34f2014-02-18 10:17:12 +08002/*
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.
Jingchang Lud6be34f2014-02-18 10:17:12 +080010 */
11
Jingchang Lud6be34f2014-02-18 10:17:12 +080012#include <linux/module.h>
13#include <linux/interrupt.h>
14#include <linux/clk.h>
Jingchang Lud6be34f2014-02-18 10:17:12 +080015#include <linux/of.h>
16#include <linux/of_device.h>
17#include <linux/of_address.h>
18#include <linux/of_irq.h>
19#include <linux/of_dma.h>
Joy Zoue0674852021-10-26 17:00:25 +080020#include <linux/dma-mapping.h>
Jingchang Lud6be34f2014-02-18 10:17:12 +080021
Angelo Dureghello9d831522018-08-19 19:27:13 +020022#include "fsl-edma-common.h"
Jingchang Lud6be34f2014-02-18 10:17:12 +080023
Andrey Smirnovba1cab72019-07-31 10:36:59 -070024static void fsl_edma_synchronize(struct dma_chan *chan)
25{
26 struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
27
28 vchan_synchronize(&fsl_chan->vchan);
29}
30
Jingchang Lud6be34f2014-02-18 10:17:12 +080031static irqreturn_t fsl_edma_tx_handler(int irq, void *dev_id)
32{
33 struct fsl_edma_engine *fsl_edma = dev_id;
34 unsigned int intr, ch;
Angelo Dureghello377eaf32018-08-19 19:27:14 +020035 struct edma_regs *regs = &fsl_edma->regs;
Jingchang Lud6be34f2014-02-18 10:17:12 +080036 struct fsl_edma_chan *fsl_chan;
37
Angelo Dureghello377eaf32018-08-19 19:27:14 +020038 intr = edma_readl(fsl_edma, regs->intl);
Jingchang Lud6be34f2014-02-18 10:17:12 +080039 if (!intr)
40 return IRQ_NONE;
41
42 for (ch = 0; ch < fsl_edma->n_chans; ch++) {
43 if (intr & (0x1 << ch)) {
Angelo Dureghello377eaf32018-08-19 19:27:14 +020044 edma_writeb(fsl_edma, EDMA_CINT_CINT(ch), regs->cint);
Jingchang Lud6be34f2014-02-18 10:17:12 +080045
46 fsl_chan = &fsl_edma->chans[ch];
47
48 spin_lock(&fsl_chan->vchan.lock);
Krzysztof Kozlowskif5e56772020-06-11 14:17:41 +020049
50 if (!fsl_chan->edesc) {
51 /* terminate_all called before */
52 spin_unlock(&fsl_chan->vchan.lock);
53 continue;
54 }
55
Jingchang Lud6be34f2014-02-18 10:17:12 +080056 if (!fsl_chan->edesc->iscyclic) {
57 list_del(&fsl_chan->edesc->vdesc.node);
58 vchan_cookie_complete(&fsl_chan->edesc->vdesc);
59 fsl_chan->edesc = NULL;
60 fsl_chan->status = DMA_COMPLETE;
Yuan Yao82d149b2015-10-30 19:03:58 +080061 fsl_chan->idle = true;
Jingchang Lud6be34f2014-02-18 10:17:12 +080062 } else {
63 vchan_cyclic_callback(&fsl_chan->edesc->vdesc);
64 }
65
66 if (!fsl_chan->edesc)
67 fsl_edma_xfer_desc(fsl_chan);
68
69 spin_unlock(&fsl_chan->vchan.lock);
70 }
71 }
72 return IRQ_HANDLED;
73}
74
75static irqreturn_t fsl_edma_err_handler(int irq, void *dev_id)
76{
77 struct fsl_edma_engine *fsl_edma = dev_id;
78 unsigned int err, ch;
Angelo Dureghello377eaf32018-08-19 19:27:14 +020079 struct edma_regs *regs = &fsl_edma->regs;
Jingchang Lud6be34f2014-02-18 10:17:12 +080080
Angelo Dureghello377eaf32018-08-19 19:27:14 +020081 err = edma_readl(fsl_edma, regs->errl);
Jingchang Lud6be34f2014-02-18 10:17:12 +080082 if (!err)
83 return IRQ_NONE;
84
85 for (ch = 0; ch < fsl_edma->n_chans; ch++) {
86 if (err & (0x1 << ch)) {
87 fsl_edma_disable_request(&fsl_edma->chans[ch]);
Angelo Dureghello377eaf32018-08-19 19:27:14 +020088 edma_writeb(fsl_edma, EDMA_CERR_CERR(ch), regs->cerr);
Jingchang Lud6be34f2014-02-18 10:17:12 +080089 fsl_edma->chans[ch].status = DMA_ERROR;
Yuan Yao82d149b2015-10-30 19:03:58 +080090 fsl_edma->chans[ch].idle = true;
Jingchang Lud6be34f2014-02-18 10:17:12 +080091 }
92 }
93 return IRQ_HANDLED;
94}
95
96static irqreturn_t fsl_edma_irq_handler(int irq, void *dev_id)
97{
98 if (fsl_edma_tx_handler(irq, dev_id) == IRQ_HANDLED)
99 return IRQ_HANDLED;
100
101 return fsl_edma_err_handler(irq, dev_id);
102}
103
Jingchang Lud6be34f2014-02-18 10:17:12 +0800104static struct dma_chan *fsl_edma_xlate(struct of_phandle_args *dma_spec,
105 struct of_dma *ofdma)
106{
107 struct fsl_edma_engine *fsl_edma = ofdma->of_dma_data;
Jingchang Lu178c81e2014-02-21 14:50:06 +0800108 struct dma_chan *chan, *_chan;
Yuan Yao82d149b2015-10-30 19:03:58 +0800109 struct fsl_edma_chan *fsl_chan;
Robin Gongaf802722019-06-25 17:43:19 +0800110 u32 dmamux_nr = fsl_edma->drvdata->dmamuxs;
111 unsigned long chans_per_mux = fsl_edma->n_chans / dmamux_nr;
Jingchang Lud6be34f2014-02-18 10:17:12 +0800112
113 if (dma_spec->args_count != 2)
114 return NULL;
115
116 mutex_lock(&fsl_edma->fsl_edma_mutex);
Jingchang Lu178c81e2014-02-21 14:50:06 +0800117 list_for_each_entry_safe(chan, _chan, &fsl_edma->dma_dev.channels, device_node) {
Jingchang Lud6be34f2014-02-18 10:17:12 +0800118 if (chan->client_count)
119 continue;
Jingchang Lu211bfef2014-07-01 16:41:03 +0800120 if ((chan->chan_id / chans_per_mux) == dma_spec->args[0]) {
Jingchang Lud6be34f2014-02-18 10:17:12 +0800121 chan = dma_get_slave_channel(chan);
122 if (chan) {
123 chan->device->privatecnt++;
Yuan Yao82d149b2015-10-30 19:03:58 +0800124 fsl_chan = to_fsl_edma_chan(chan);
125 fsl_chan->slave_id = dma_spec->args[1];
126 fsl_edma_chan_mux(fsl_chan, fsl_chan->slave_id,
127 true);
Jingchang Lud6be34f2014-02-18 10:17:12 +0800128 mutex_unlock(&fsl_edma->fsl_edma_mutex);
129 return chan;
130 }
131 }
132 }
133 mutex_unlock(&fsl_edma->fsl_edma_mutex);
134 return NULL;
135}
136
Jingchang Lud6be34f2014-02-18 10:17:12 +0800137static int
138fsl_edma_irq_init(struct platform_device *pdev, struct fsl_edma_engine *fsl_edma)
139{
140 int ret;
141
142 fsl_edma->txirq = platform_get_irq_byname(pdev, "edma-tx");
Stephen Boyde17be6e2019-07-30 11:15:10 -0700143 if (fsl_edma->txirq < 0)
Jingchang Lud6be34f2014-02-18 10:17:12 +0800144 return fsl_edma->txirq;
Jingchang Lud6be34f2014-02-18 10:17:12 +0800145
146 fsl_edma->errirq = platform_get_irq_byname(pdev, "edma-err");
Stephen Boyde17be6e2019-07-30 11:15:10 -0700147 if (fsl_edma->errirq < 0)
Jingchang Lud6be34f2014-02-18 10:17:12 +0800148 return fsl_edma->errirq;
Jingchang Lud6be34f2014-02-18 10:17:12 +0800149
150 if (fsl_edma->txirq == fsl_edma->errirq) {
151 ret = devm_request_irq(&pdev->dev, fsl_edma->txirq,
152 fsl_edma_irq_handler, 0, "eDMA", fsl_edma);
153 if (ret) {
154 dev_err(&pdev->dev, "Can't register eDMA IRQ.\n");
Krzysztof Kozlowskie0951892019-05-04 11:52:25 +0200155 return ret;
Jingchang Lud6be34f2014-02-18 10:17:12 +0800156 }
157 } else {
158 ret = devm_request_irq(&pdev->dev, fsl_edma->txirq,
159 fsl_edma_tx_handler, 0, "eDMA tx", fsl_edma);
160 if (ret) {
161 dev_err(&pdev->dev, "Can't register eDMA tx IRQ.\n");
Krzysztof Kozlowskie0951892019-05-04 11:52:25 +0200162 return ret;
Jingchang Lud6be34f2014-02-18 10:17:12 +0800163 }
164
165 ret = devm_request_irq(&pdev->dev, fsl_edma->errirq,
166 fsl_edma_err_handler, 0, "eDMA err", fsl_edma);
167 if (ret) {
168 dev_err(&pdev->dev, "Can't register eDMA err IRQ.\n");
Krzysztof Kozlowskie0951892019-05-04 11:52:25 +0200169 return ret;
Jingchang Lud6be34f2014-02-18 10:17:12 +0800170 }
171 }
172
173 return 0;
174}
175
Robin Gong232a7f12019-07-24 15:20:34 +0800176static int
177fsl_edma2_irq_init(struct platform_device *pdev,
178 struct fsl_edma_engine *fsl_edma)
179{
180 int i, ret, irq;
181 int count;
182
183 count = platform_irq_count(pdev);
184 dev_dbg(&pdev->dev, "%s Found %d interrupts\r\n", __func__, count);
185 if (count <= 2) {
186 dev_err(&pdev->dev, "Interrupts in DTS not correct.\n");
187 return -EINVAL;
188 }
189 /*
190 * 16 channel independent interrupts + 1 error interrupt on i.mx7ulp.
191 * 2 channel share one interrupt, for example, ch0/ch16, ch1/ch17...
192 * For now, just simply request irq without IRQF_SHARED flag, since 16
193 * channels are enough on i.mx7ulp whose M4 domain own some peripherals.
194 */
195 for (i = 0; i < count; i++) {
196 irq = platform_get_irq(pdev, i);
197 if (irq < 0)
198 return -ENXIO;
199
200 sprintf(fsl_edma->chans[i].chan_name, "eDMA2-CH%02d", i);
201
202 /* The last IRQ is for eDMA err */
203 if (i == count - 1)
204 ret = devm_request_irq(&pdev->dev, irq,
205 fsl_edma_err_handler,
206 0, "eDMA2-ERR", fsl_edma);
207 else
208 ret = devm_request_irq(&pdev->dev, irq,
209 fsl_edma_tx_handler, 0,
210 fsl_edma->chans[i].chan_name,
211 fsl_edma);
212 if (ret)
213 return ret;
214 }
215
216 return 0;
217}
218
Vinod Koul476c7c82016-07-01 17:34:14 +0530219static void fsl_edma_irq_exit(
220 struct platform_device *pdev, struct fsl_edma_engine *fsl_edma)
221{
222 if (fsl_edma->txirq == fsl_edma->errirq) {
223 devm_free_irq(&pdev->dev, fsl_edma->txirq, fsl_edma);
224 } else {
225 devm_free_irq(&pdev->dev, fsl_edma->txirq, fsl_edma);
226 devm_free_irq(&pdev->dev, fsl_edma->errirq, fsl_edma);
227 }
228}
229
Andreas Platschek2610acf2017-12-14 12:50:51 +0100230static void fsl_disable_clocks(struct fsl_edma_engine *fsl_edma, int nr_clocks)
Peter Griffin5e2fe1e2016-06-07 18:38:34 +0100231{
232 int i;
233
Andreas Platschek2610acf2017-12-14 12:50:51 +0100234 for (i = 0; i < nr_clocks; i++)
Peter Griffin5e2fe1e2016-06-07 18:38:34 +0100235 clk_disable_unprepare(fsl_edma->muxclk[i]);
236}
237
Robin Gongaf802722019-06-25 17:43:19 +0800238static struct fsl_edma_drvdata vf610_data = {
239 .version = v1,
240 .dmamuxs = DMAMUX_NR,
241 .setup_irq = fsl_edma_irq_init,
242};
243
Peng Maed5a0ab2019-12-12 03:38:10 +0000244static struct fsl_edma_drvdata ls1028a_data = {
245 .version = v1,
246 .dmamuxs = DMAMUX_NR,
247 .mux_swap = true,
248 .setup_irq = fsl_edma_irq_init,
249};
250
Robin Gong232a7f12019-07-24 15:20:34 +0800251static struct fsl_edma_drvdata imx7ulp_data = {
252 .version = v3,
253 .dmamuxs = 1,
254 .has_dmaclk = true,
255 .setup_irq = fsl_edma2_irq_init,
256};
257
Robin Gongaf802722019-06-25 17:43:19 +0800258static const struct of_device_id fsl_edma_dt_ids[] = {
259 { .compatible = "fsl,vf610-edma", .data = &vf610_data},
Peng Maed5a0ab2019-12-12 03:38:10 +0000260 { .compatible = "fsl,ls1028a-edma", .data = &ls1028a_data},
Robin Gong232a7f12019-07-24 15:20:34 +0800261 { .compatible = "fsl,imx7ulp-edma", .data = &imx7ulp_data},
Robin Gongaf802722019-06-25 17:43:19 +0800262 { /* sentinel */ }
263};
264MODULE_DEVICE_TABLE(of, fsl_edma_dt_ids);
265
Jingchang Lud6be34f2014-02-18 10:17:12 +0800266static int fsl_edma_probe(struct platform_device *pdev)
267{
Robin Gongaf802722019-06-25 17:43:19 +0800268 const struct of_device_id *of_id =
269 of_match_device(fsl_edma_dt_ids, &pdev->dev);
Jingchang Lud6be34f2014-02-18 10:17:12 +0800270 struct device_node *np = pdev->dev.of_node;
271 struct fsl_edma_engine *fsl_edma;
Robin Gongaf802722019-06-25 17:43:19 +0800272 const struct fsl_edma_drvdata *drvdata = NULL;
Jingchang Lud6be34f2014-02-18 10:17:12 +0800273 struct fsl_edma_chan *fsl_chan;
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200274 struct edma_regs *regs;
Jingchang Lud6be34f2014-02-18 10:17:12 +0800275 int len, chans;
276 int ret, i;
277
Robin Gongaf802722019-06-25 17:43:19 +0800278 if (of_id)
279 drvdata = of_id->data;
280 if (!drvdata) {
281 dev_err(&pdev->dev, "unable to find driver data\n");
282 return -EINVAL;
283 }
284
Jingchang Lud6be34f2014-02-18 10:17:12 +0800285 ret = of_property_read_u32(np, "dma-channels", &chans);
286 if (ret) {
287 dev_err(&pdev->dev, "Can't get dma-channels.\n");
288 return ret;
289 }
290
291 len = sizeof(*fsl_edma) + sizeof(*fsl_chan) * chans;
292 fsl_edma = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
293 if (!fsl_edma)
294 return -ENOMEM;
295
Robin Gongaf802722019-06-25 17:43:19 +0800296 fsl_edma->drvdata = drvdata;
Jingchang Lud6be34f2014-02-18 10:17:12 +0800297 fsl_edma->n_chans = chans;
298 mutex_init(&fsl_edma->fsl_edma_mutex);
299
Tudor Ambarus4b236032022-11-10 17:25:28 +0200300 fsl_edma->membase = devm_platform_ioremap_resource(pdev, 0);
Jingchang Lud6be34f2014-02-18 10:17:12 +0800301 if (IS_ERR(fsl_edma->membase))
302 return PTR_ERR(fsl_edma->membase);
303
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200304 fsl_edma_setup_regs(fsl_edma);
305 regs = &fsl_edma->regs;
306
Robin Gong232a7f12019-07-24 15:20:34 +0800307 if (drvdata->has_dmaclk) {
308 fsl_edma->dmaclk = devm_clk_get(&pdev->dev, "dma");
309 if (IS_ERR(fsl_edma->dmaclk)) {
310 dev_err(&pdev->dev, "Missing DMA block clock.\n");
311 return PTR_ERR(fsl_edma->dmaclk);
312 }
313
314 ret = clk_prepare_enable(fsl_edma->dmaclk);
315 if (ret) {
316 dev_err(&pdev->dev, "DMA clk block failed.\n");
317 return ret;
318 }
319 }
320
Robin Gongaf802722019-06-25 17:43:19 +0800321 for (i = 0; i < fsl_edma->drvdata->dmamuxs; i++) {
Jingchang Lud6be34f2014-02-18 10:17:12 +0800322 char clkname[32];
323
Tudor Ambarus4b236032022-11-10 17:25:28 +0200324 fsl_edma->muxbase[i] = devm_platform_ioremap_resource(pdev,
325 1 + i);
Andreas Platschek2610acf2017-12-14 12:50:51 +0100326 if (IS_ERR(fsl_edma->muxbase[i])) {
327 /* on error: disable all previously enabled clks */
328 fsl_disable_clocks(fsl_edma, i);
Jingchang Lud6be34f2014-02-18 10:17:12 +0800329 return PTR_ERR(fsl_edma->muxbase[i]);
Andreas Platschek2610acf2017-12-14 12:50:51 +0100330 }
Jingchang Lud6be34f2014-02-18 10:17:12 +0800331
332 sprintf(clkname, "dmamux%d", i);
333 fsl_edma->muxclk[i] = devm_clk_get(&pdev->dev, clkname);
334 if (IS_ERR(fsl_edma->muxclk[i])) {
335 dev_err(&pdev->dev, "Missing DMAMUX block clock.\n");
Andreas Platschek2610acf2017-12-14 12:50:51 +0100336 /* on error: disable all previously enabled clks */
337 fsl_disable_clocks(fsl_edma, i);
Jingchang Lud6be34f2014-02-18 10:17:12 +0800338 return PTR_ERR(fsl_edma->muxclk[i]);
339 }
340
341 ret = clk_prepare_enable(fsl_edma->muxclk[i]);
Andreas Platschek2610acf2017-12-14 12:50:51 +0100342 if (ret)
343 /* on error: disable all previously enabled clks */
344 fsl_disable_clocks(fsl_edma, i);
Jingchang Lud6be34f2014-02-18 10:17:12 +0800345
346 }
347
Jingchang Lud6be34f2014-02-18 10:17:12 +0800348 fsl_edma->big_endian = of_property_read_bool(np, "big-endian");
349
350 INIT_LIST_HEAD(&fsl_edma->dma_dev.channels);
351 for (i = 0; i < fsl_edma->n_chans; i++) {
352 struct fsl_edma_chan *fsl_chan = &fsl_edma->chans[i];
353
354 fsl_chan->edma = fsl_edma;
Yuan Yao82d149b2015-10-30 19:03:58 +0800355 fsl_chan->pm_state = RUNNING;
356 fsl_chan->slave_id = 0;
357 fsl_chan->idle = true;
Laurentiu Tudor0fa89f92019-01-18 12:06:23 +0200358 fsl_chan->dma_dir = DMA_NONE;
Jingchang Lud6be34f2014-02-18 10:17:12 +0800359 fsl_chan->vchan.desc_free = fsl_edma_free_desc;
360 vchan_init(&fsl_chan->vchan, &fsl_edma->dma_dev);
361
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200362 edma_writew(fsl_edma, 0x0, &regs->tcd[i].csr);
Jingchang Lud6be34f2014-02-18 10:17:12 +0800363 fsl_edma_chan_mux(fsl_chan, 0, false);
364 }
365
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200366 edma_writel(fsl_edma, ~0, regs->intl);
Robin Gongaf802722019-06-25 17:43:19 +0800367 ret = fsl_edma->drvdata->setup_irq(pdev, fsl_edma);
Stefan Agner0fe25d62015-06-07 21:46:10 +0200368 if (ret)
369 return ret;
370
Jingchang Lud6be34f2014-02-18 10:17:12 +0800371 dma_cap_set(DMA_PRIVATE, fsl_edma->dma_dev.cap_mask);
372 dma_cap_set(DMA_SLAVE, fsl_edma->dma_dev.cap_mask);
373 dma_cap_set(DMA_CYCLIC, fsl_edma->dma_dev.cap_mask);
Joy Zoue0674852021-10-26 17:00:25 +0800374 dma_cap_set(DMA_MEMCPY, fsl_edma->dma_dev.cap_mask);
Jingchang Lud6be34f2014-02-18 10:17:12 +0800375
376 fsl_edma->dma_dev.dev = &pdev->dev;
377 fsl_edma->dma_dev.device_alloc_chan_resources
378 = fsl_edma_alloc_chan_resources;
379 fsl_edma->dma_dev.device_free_chan_resources
380 = fsl_edma_free_chan_resources;
381 fsl_edma->dma_dev.device_tx_status = fsl_edma_tx_status;
382 fsl_edma->dma_dev.device_prep_slave_sg = fsl_edma_prep_slave_sg;
383 fsl_edma->dma_dev.device_prep_dma_cyclic = fsl_edma_prep_dma_cyclic;
Joy Zoue0674852021-10-26 17:00:25 +0800384 fsl_edma->dma_dev.device_prep_dma_memcpy = fsl_edma_prep_memcpy;
Maxime Ripardd80f3812014-11-17 14:42:15 +0100385 fsl_edma->dma_dev.device_config = fsl_edma_slave_config;
386 fsl_edma->dma_dev.device_pause = fsl_edma_pause;
387 fsl_edma->dma_dev.device_resume = fsl_edma_resume;
388 fsl_edma->dma_dev.device_terminate_all = fsl_edma_terminate_all;
Andrey Smirnovba1cab72019-07-31 10:36:59 -0700389 fsl_edma->dma_dev.device_synchronize = fsl_edma_synchronize;
Jingchang Lud6be34f2014-02-18 10:17:12 +0800390 fsl_edma->dma_dev.device_issue_pending = fsl_edma_issue_pending;
Maxime Ripardf45c4312014-11-17 14:42:46 +0100391
392 fsl_edma->dma_dev.src_addr_widths = FSL_EDMA_BUSWIDTHS;
393 fsl_edma->dma_dev.dst_addr_widths = FSL_EDMA_BUSWIDTHS;
394 fsl_edma->dma_dev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
Jingchang Lud6be34f2014-02-18 10:17:12 +0800395
Joy Zoue0674852021-10-26 17:00:25 +0800396 fsl_edma->dma_dev.copy_align = DMAENGINE_ALIGN_32_BYTES;
397 /* Per worst case 'nbytes = 1' take CITER as the max_seg_size */
398 dma_set_max_seg_size(fsl_edma->dma_dev.dev, 0x3fff);
399
Jingchang Lud6be34f2014-02-18 10:17:12 +0800400 platform_set_drvdata(pdev, fsl_edma);
401
402 ret = dma_async_device_register(&fsl_edma->dma_dev);
403 if (ret) {
Peter Griffina86144d2016-06-07 18:38:35 +0100404 dev_err(&pdev->dev,
405 "Can't register Freescale eDMA engine. (%d)\n", ret);
Robin Gongaf802722019-06-25 17:43:19 +0800406 fsl_disable_clocks(fsl_edma, fsl_edma->drvdata->dmamuxs);
Jingchang Lud6be34f2014-02-18 10:17:12 +0800407 return ret;
408 }
409
410 ret = of_dma_controller_register(np, fsl_edma_xlate, fsl_edma);
411 if (ret) {
Peter Griffina86144d2016-06-07 18:38:35 +0100412 dev_err(&pdev->dev,
413 "Can't register Freescale eDMA of_dma. (%d)\n", ret);
Jingchang Lud6be34f2014-02-18 10:17:12 +0800414 dma_async_device_unregister(&fsl_edma->dma_dev);
Robin Gongaf802722019-06-25 17:43:19 +0800415 fsl_disable_clocks(fsl_edma, fsl_edma->drvdata->dmamuxs);
Jingchang Lud6be34f2014-02-18 10:17:12 +0800416 return ret;
417 }
418
419 /* enable round robin arbitration */
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200420 edma_writel(fsl_edma, EDMA_CR_ERGA | EDMA_CR_ERCA, regs->cr);
Jingchang Lud6be34f2014-02-18 10:17:12 +0800421
422 return 0;
423}
424
425static int fsl_edma_remove(struct platform_device *pdev)
426{
427 struct device_node *np = pdev->dev.of_node;
428 struct fsl_edma_engine *fsl_edma = platform_get_drvdata(pdev);
Jingchang Lud6be34f2014-02-18 10:17:12 +0800429
Vinod Koul476c7c82016-07-01 17:34:14 +0530430 fsl_edma_irq_exit(pdev, fsl_edma);
Vinod Koul6f93b932016-07-02 14:58:30 +0530431 fsl_edma_cleanup_vchan(&fsl_edma->dma_dev);
Jingchang Lud6be34f2014-02-18 10:17:12 +0800432 of_dma_controller_free(np);
433 dma_async_device_unregister(&fsl_edma->dma_dev);
Robin Gongaf802722019-06-25 17:43:19 +0800434 fsl_disable_clocks(fsl_edma, fsl_edma->drvdata->dmamuxs);
Jingchang Lud6be34f2014-02-18 10:17:12 +0800435
436 return 0;
437}
438
Yuan Yao82d149b2015-10-30 19:03:58 +0800439static int fsl_edma_suspend_late(struct device *dev)
440{
441 struct fsl_edma_engine *fsl_edma = dev_get_drvdata(dev);
442 struct fsl_edma_chan *fsl_chan;
443 unsigned long flags;
444 int i;
445
446 for (i = 0; i < fsl_edma->n_chans; i++) {
447 fsl_chan = &fsl_edma->chans[i];
448 spin_lock_irqsave(&fsl_chan->vchan.lock, flags);
449 /* Make sure chan is idle or will force disable. */
450 if (unlikely(!fsl_chan->idle)) {
451 dev_warn(dev, "WARN: There is non-idle channel.");
452 fsl_edma_disable_request(fsl_chan);
453 fsl_edma_chan_mux(fsl_chan, 0, false);
454 }
455
456 fsl_chan->pm_state = SUSPENDED;
457 spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags);
458 }
459
460 return 0;
461}
462
463static int fsl_edma_resume_early(struct device *dev)
464{
465 struct fsl_edma_engine *fsl_edma = dev_get_drvdata(dev);
466 struct fsl_edma_chan *fsl_chan;
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200467 struct edma_regs *regs = &fsl_edma->regs;
Yuan Yao82d149b2015-10-30 19:03:58 +0800468 int i;
469
470 for (i = 0; i < fsl_edma->n_chans; i++) {
471 fsl_chan = &fsl_edma->chans[i];
472 fsl_chan->pm_state = RUNNING;
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200473 edma_writew(fsl_edma, 0x0, &regs->tcd[i].csr);
Yuan Yao82d149b2015-10-30 19:03:58 +0800474 if (fsl_chan->slave_id != 0)
475 fsl_edma_chan_mux(fsl_chan, fsl_chan->slave_id, true);
476 }
477
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200478 edma_writel(fsl_edma, EDMA_CR_ERGA | EDMA_CR_ERCA, regs->cr);
Yuan Yao82d149b2015-10-30 19:03:58 +0800479
480 return 0;
481}
482
483/*
484 * eDMA provides the service to others, so it should be suspend late
485 * and resume early. When eDMA suspend, all of the clients should stop
486 * the DMA data transmission and let the channel idle.
487 */
488static const struct dev_pm_ops fsl_edma_pm_ops = {
489 .suspend_late = fsl_edma_suspend_late,
490 .resume_early = fsl_edma_resume_early,
491};
492
Jingchang Lud6be34f2014-02-18 10:17:12 +0800493static struct platform_driver fsl_edma_driver = {
494 .driver = {
495 .name = "fsl-edma",
Jingchang Lud6be34f2014-02-18 10:17:12 +0800496 .of_match_table = fsl_edma_dt_ids,
Yuan Yao82d149b2015-10-30 19:03:58 +0800497 .pm = &fsl_edma_pm_ops,
Jingchang Lud6be34f2014-02-18 10:17:12 +0800498 },
499 .probe = fsl_edma_probe,
500 .remove = fsl_edma_remove,
501};
502
Yuan Yao8edc51c2014-04-04 12:27:55 +0800503static int __init fsl_edma_init(void)
504{
505 return platform_driver_register(&fsl_edma_driver);
506}
507subsys_initcall(fsl_edma_init);
508
509static void __exit fsl_edma_exit(void)
510{
511 platform_driver_unregister(&fsl_edma_driver);
512}
513module_exit(fsl_edma_exit);
Jingchang Lud6be34f2014-02-18 10:17:12 +0800514
515MODULE_ALIAS("platform:fsl-edma");
516MODULE_DESCRIPTION("Freescale eDMA engine driver");
517MODULE_LICENSE("GPL v2");