blob: dfce75c2991b97b0791369e5724599ef10f78249 [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>
20
Angelo Dureghello9d831522018-08-19 19:27:13 +020021#include "fsl-edma-common.h"
Jingchang Lud6be34f2014-02-18 10:17:12 +080022
23static irqreturn_t fsl_edma_tx_handler(int irq, void *dev_id)
24{
25 struct fsl_edma_engine *fsl_edma = dev_id;
26 unsigned int intr, ch;
Angelo Dureghello377eaf32018-08-19 19:27:14 +020027 struct edma_regs *regs = &fsl_edma->regs;
Jingchang Lud6be34f2014-02-18 10:17:12 +080028 struct fsl_edma_chan *fsl_chan;
29
Angelo Dureghello377eaf32018-08-19 19:27:14 +020030 intr = edma_readl(fsl_edma, regs->intl);
Jingchang Lud6be34f2014-02-18 10:17:12 +080031 if (!intr)
32 return IRQ_NONE;
33
34 for (ch = 0; ch < fsl_edma->n_chans; ch++) {
35 if (intr & (0x1 << ch)) {
Angelo Dureghello377eaf32018-08-19 19:27:14 +020036 edma_writeb(fsl_edma, EDMA_CINT_CINT(ch), regs->cint);
Jingchang Lud6be34f2014-02-18 10:17:12 +080037
38 fsl_chan = &fsl_edma->chans[ch];
39
40 spin_lock(&fsl_chan->vchan.lock);
41 if (!fsl_chan->edesc->iscyclic) {
42 list_del(&fsl_chan->edesc->vdesc.node);
43 vchan_cookie_complete(&fsl_chan->edesc->vdesc);
44 fsl_chan->edesc = NULL;
45 fsl_chan->status = DMA_COMPLETE;
Yuan Yao82d149b2015-10-30 19:03:58 +080046 fsl_chan->idle = true;
Jingchang Lud6be34f2014-02-18 10:17:12 +080047 } else {
48 vchan_cyclic_callback(&fsl_chan->edesc->vdesc);
49 }
50
51 if (!fsl_chan->edesc)
52 fsl_edma_xfer_desc(fsl_chan);
53
54 spin_unlock(&fsl_chan->vchan.lock);
55 }
56 }
57 return IRQ_HANDLED;
58}
59
60static irqreturn_t fsl_edma_err_handler(int irq, void *dev_id)
61{
62 struct fsl_edma_engine *fsl_edma = dev_id;
63 unsigned int err, ch;
Angelo Dureghello377eaf32018-08-19 19:27:14 +020064 struct edma_regs *regs = &fsl_edma->regs;
Jingchang Lud6be34f2014-02-18 10:17:12 +080065
Angelo Dureghello377eaf32018-08-19 19:27:14 +020066 err = edma_readl(fsl_edma, regs->errl);
Jingchang Lud6be34f2014-02-18 10:17:12 +080067 if (!err)
68 return IRQ_NONE;
69
70 for (ch = 0; ch < fsl_edma->n_chans; ch++) {
71 if (err & (0x1 << ch)) {
72 fsl_edma_disable_request(&fsl_edma->chans[ch]);
Angelo Dureghello377eaf32018-08-19 19:27:14 +020073 edma_writeb(fsl_edma, EDMA_CERR_CERR(ch), regs->cerr);
Jingchang Lud6be34f2014-02-18 10:17:12 +080074 fsl_edma->chans[ch].status = DMA_ERROR;
Yuan Yao82d149b2015-10-30 19:03:58 +080075 fsl_edma->chans[ch].idle = true;
Jingchang Lud6be34f2014-02-18 10:17:12 +080076 }
77 }
78 return IRQ_HANDLED;
79}
80
81static irqreturn_t fsl_edma_irq_handler(int irq, void *dev_id)
82{
83 if (fsl_edma_tx_handler(irq, dev_id) == IRQ_HANDLED)
84 return IRQ_HANDLED;
85
86 return fsl_edma_err_handler(irq, dev_id);
87}
88
Jingchang Lud6be34f2014-02-18 10:17:12 +080089static struct dma_chan *fsl_edma_xlate(struct of_phandle_args *dma_spec,
90 struct of_dma *ofdma)
91{
92 struct fsl_edma_engine *fsl_edma = ofdma->of_dma_data;
Jingchang Lu178c81e2014-02-21 14:50:06 +080093 struct dma_chan *chan, *_chan;
Yuan Yao82d149b2015-10-30 19:03:58 +080094 struct fsl_edma_chan *fsl_chan;
Robin Gongaf802722019-06-25 17:43:19 +080095 u32 dmamux_nr = fsl_edma->drvdata->dmamuxs;
96 unsigned long chans_per_mux = fsl_edma->n_chans / dmamux_nr;
Jingchang Lud6be34f2014-02-18 10:17:12 +080097
98 if (dma_spec->args_count != 2)
99 return NULL;
100
101 mutex_lock(&fsl_edma->fsl_edma_mutex);
Jingchang Lu178c81e2014-02-21 14:50:06 +0800102 list_for_each_entry_safe(chan, _chan, &fsl_edma->dma_dev.channels, device_node) {
Jingchang Lud6be34f2014-02-18 10:17:12 +0800103 if (chan->client_count)
104 continue;
Jingchang Lu211bfef2014-07-01 16:41:03 +0800105 if ((chan->chan_id / chans_per_mux) == dma_spec->args[0]) {
Jingchang Lud6be34f2014-02-18 10:17:12 +0800106 chan = dma_get_slave_channel(chan);
107 if (chan) {
108 chan->device->privatecnt++;
Yuan Yao82d149b2015-10-30 19:03:58 +0800109 fsl_chan = to_fsl_edma_chan(chan);
110 fsl_chan->slave_id = dma_spec->args[1];
111 fsl_edma_chan_mux(fsl_chan, fsl_chan->slave_id,
112 true);
Jingchang Lud6be34f2014-02-18 10:17:12 +0800113 mutex_unlock(&fsl_edma->fsl_edma_mutex);
114 return chan;
115 }
116 }
117 }
118 mutex_unlock(&fsl_edma->fsl_edma_mutex);
119 return NULL;
120}
121
Jingchang Lud6be34f2014-02-18 10:17:12 +0800122static int
123fsl_edma_irq_init(struct platform_device *pdev, struct fsl_edma_engine *fsl_edma)
124{
125 int ret;
126
127 fsl_edma->txirq = platform_get_irq_byname(pdev, "edma-tx");
Stephen Boyde17be6e2019-07-30 11:15:10 -0700128 if (fsl_edma->txirq < 0)
Jingchang Lud6be34f2014-02-18 10:17:12 +0800129 return fsl_edma->txirq;
Jingchang Lud6be34f2014-02-18 10:17:12 +0800130
131 fsl_edma->errirq = platform_get_irq_byname(pdev, "edma-err");
Stephen Boyde17be6e2019-07-30 11:15:10 -0700132 if (fsl_edma->errirq < 0)
Jingchang Lud6be34f2014-02-18 10:17:12 +0800133 return fsl_edma->errirq;
Jingchang Lud6be34f2014-02-18 10:17:12 +0800134
135 if (fsl_edma->txirq == fsl_edma->errirq) {
136 ret = devm_request_irq(&pdev->dev, fsl_edma->txirq,
137 fsl_edma_irq_handler, 0, "eDMA", fsl_edma);
138 if (ret) {
139 dev_err(&pdev->dev, "Can't register eDMA IRQ.\n");
Krzysztof Kozlowskie0951892019-05-04 11:52:25 +0200140 return ret;
Jingchang Lud6be34f2014-02-18 10:17:12 +0800141 }
142 } else {
143 ret = devm_request_irq(&pdev->dev, fsl_edma->txirq,
144 fsl_edma_tx_handler, 0, "eDMA tx", fsl_edma);
145 if (ret) {
146 dev_err(&pdev->dev, "Can't register eDMA tx IRQ.\n");
Krzysztof Kozlowskie0951892019-05-04 11:52:25 +0200147 return ret;
Jingchang Lud6be34f2014-02-18 10:17:12 +0800148 }
149
150 ret = devm_request_irq(&pdev->dev, fsl_edma->errirq,
151 fsl_edma_err_handler, 0, "eDMA err", fsl_edma);
152 if (ret) {
153 dev_err(&pdev->dev, "Can't register eDMA err IRQ.\n");
Krzysztof Kozlowskie0951892019-05-04 11:52:25 +0200154 return ret;
Jingchang Lud6be34f2014-02-18 10:17:12 +0800155 }
156 }
157
158 return 0;
159}
160
Robin Gong232a7f12019-07-24 15:20:34 +0800161static int
162fsl_edma2_irq_init(struct platform_device *pdev,
163 struct fsl_edma_engine *fsl_edma)
164{
165 int i, ret, irq;
166 int count;
167
168 count = platform_irq_count(pdev);
169 dev_dbg(&pdev->dev, "%s Found %d interrupts\r\n", __func__, count);
170 if (count <= 2) {
171 dev_err(&pdev->dev, "Interrupts in DTS not correct.\n");
172 return -EINVAL;
173 }
174 /*
175 * 16 channel independent interrupts + 1 error interrupt on i.mx7ulp.
176 * 2 channel share one interrupt, for example, ch0/ch16, ch1/ch17...
177 * For now, just simply request irq without IRQF_SHARED flag, since 16
178 * channels are enough on i.mx7ulp whose M4 domain own some peripherals.
179 */
180 for (i = 0; i < count; i++) {
181 irq = platform_get_irq(pdev, i);
182 if (irq < 0)
183 return -ENXIO;
184
185 sprintf(fsl_edma->chans[i].chan_name, "eDMA2-CH%02d", i);
186
187 /* The last IRQ is for eDMA err */
188 if (i == count - 1)
189 ret = devm_request_irq(&pdev->dev, irq,
190 fsl_edma_err_handler,
191 0, "eDMA2-ERR", fsl_edma);
192 else
193 ret = devm_request_irq(&pdev->dev, irq,
194 fsl_edma_tx_handler, 0,
195 fsl_edma->chans[i].chan_name,
196 fsl_edma);
197 if (ret)
198 return ret;
199 }
200
201 return 0;
202}
203
Vinod Koul476c7c82016-07-01 17:34:14 +0530204static void fsl_edma_irq_exit(
205 struct platform_device *pdev, struct fsl_edma_engine *fsl_edma)
206{
207 if (fsl_edma->txirq == fsl_edma->errirq) {
208 devm_free_irq(&pdev->dev, fsl_edma->txirq, fsl_edma);
209 } else {
210 devm_free_irq(&pdev->dev, fsl_edma->txirq, fsl_edma);
211 devm_free_irq(&pdev->dev, fsl_edma->errirq, fsl_edma);
212 }
213}
214
Andreas Platschek2610acf2017-12-14 12:50:51 +0100215static void fsl_disable_clocks(struct fsl_edma_engine *fsl_edma, int nr_clocks)
Peter Griffin5e2fe1e2016-06-07 18:38:34 +0100216{
217 int i;
218
Andreas Platschek2610acf2017-12-14 12:50:51 +0100219 for (i = 0; i < nr_clocks; i++)
Peter Griffin5e2fe1e2016-06-07 18:38:34 +0100220 clk_disable_unprepare(fsl_edma->muxclk[i]);
221}
222
Robin Gongaf802722019-06-25 17:43:19 +0800223static struct fsl_edma_drvdata vf610_data = {
224 .version = v1,
225 .dmamuxs = DMAMUX_NR,
226 .setup_irq = fsl_edma_irq_init,
227};
228
Robin Gong232a7f12019-07-24 15:20:34 +0800229static struct fsl_edma_drvdata imx7ulp_data = {
230 .version = v3,
231 .dmamuxs = 1,
232 .has_dmaclk = true,
233 .setup_irq = fsl_edma2_irq_init,
234};
235
Robin Gongaf802722019-06-25 17:43:19 +0800236static const struct of_device_id fsl_edma_dt_ids[] = {
237 { .compatible = "fsl,vf610-edma", .data = &vf610_data},
Robin Gong232a7f12019-07-24 15:20:34 +0800238 { .compatible = "fsl,imx7ulp-edma", .data = &imx7ulp_data},
Robin Gongaf802722019-06-25 17:43:19 +0800239 { /* sentinel */ }
240};
241MODULE_DEVICE_TABLE(of, fsl_edma_dt_ids);
242
Jingchang Lud6be34f2014-02-18 10:17:12 +0800243static int fsl_edma_probe(struct platform_device *pdev)
244{
Robin Gongaf802722019-06-25 17:43:19 +0800245 const struct of_device_id *of_id =
246 of_match_device(fsl_edma_dt_ids, &pdev->dev);
Jingchang Lud6be34f2014-02-18 10:17:12 +0800247 struct device_node *np = pdev->dev.of_node;
248 struct fsl_edma_engine *fsl_edma;
Robin Gongaf802722019-06-25 17:43:19 +0800249 const struct fsl_edma_drvdata *drvdata = NULL;
Jingchang Lud6be34f2014-02-18 10:17:12 +0800250 struct fsl_edma_chan *fsl_chan;
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200251 struct edma_regs *regs;
Jingchang Lud6be34f2014-02-18 10:17:12 +0800252 struct resource *res;
253 int len, chans;
254 int ret, i;
255
Robin Gongaf802722019-06-25 17:43:19 +0800256 if (of_id)
257 drvdata = of_id->data;
258 if (!drvdata) {
259 dev_err(&pdev->dev, "unable to find driver data\n");
260 return -EINVAL;
261 }
262
Jingchang Lud6be34f2014-02-18 10:17:12 +0800263 ret = of_property_read_u32(np, "dma-channels", &chans);
264 if (ret) {
265 dev_err(&pdev->dev, "Can't get dma-channels.\n");
266 return ret;
267 }
268
269 len = sizeof(*fsl_edma) + sizeof(*fsl_chan) * chans;
270 fsl_edma = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
271 if (!fsl_edma)
272 return -ENOMEM;
273
Robin Gongaf802722019-06-25 17:43:19 +0800274 fsl_edma->drvdata = drvdata;
Jingchang Lud6be34f2014-02-18 10:17:12 +0800275 fsl_edma->n_chans = chans;
276 mutex_init(&fsl_edma->fsl_edma_mutex);
277
278 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
279 fsl_edma->membase = devm_ioremap_resource(&pdev->dev, res);
280 if (IS_ERR(fsl_edma->membase))
281 return PTR_ERR(fsl_edma->membase);
282
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200283 fsl_edma_setup_regs(fsl_edma);
284 regs = &fsl_edma->regs;
285
Robin Gong232a7f12019-07-24 15:20:34 +0800286 if (drvdata->has_dmaclk) {
287 fsl_edma->dmaclk = devm_clk_get(&pdev->dev, "dma");
288 if (IS_ERR(fsl_edma->dmaclk)) {
289 dev_err(&pdev->dev, "Missing DMA block clock.\n");
290 return PTR_ERR(fsl_edma->dmaclk);
291 }
292
293 ret = clk_prepare_enable(fsl_edma->dmaclk);
294 if (ret) {
295 dev_err(&pdev->dev, "DMA clk block failed.\n");
296 return ret;
297 }
298 }
299
Robin Gongaf802722019-06-25 17:43:19 +0800300 for (i = 0; i < fsl_edma->drvdata->dmamuxs; i++) {
Jingchang Lud6be34f2014-02-18 10:17:12 +0800301 char clkname[32];
302
303 res = platform_get_resource(pdev, IORESOURCE_MEM, 1 + i);
304 fsl_edma->muxbase[i] = devm_ioremap_resource(&pdev->dev, res);
Andreas Platschek2610acf2017-12-14 12:50:51 +0100305 if (IS_ERR(fsl_edma->muxbase[i])) {
306 /* on error: disable all previously enabled clks */
307 fsl_disable_clocks(fsl_edma, i);
Jingchang Lud6be34f2014-02-18 10:17:12 +0800308 return PTR_ERR(fsl_edma->muxbase[i]);
Andreas Platschek2610acf2017-12-14 12:50:51 +0100309 }
Jingchang Lud6be34f2014-02-18 10:17:12 +0800310
311 sprintf(clkname, "dmamux%d", i);
312 fsl_edma->muxclk[i] = devm_clk_get(&pdev->dev, clkname);
313 if (IS_ERR(fsl_edma->muxclk[i])) {
314 dev_err(&pdev->dev, "Missing DMAMUX block clock.\n");
Andreas Platschek2610acf2017-12-14 12:50:51 +0100315 /* on error: disable all previously enabled clks */
316 fsl_disable_clocks(fsl_edma, i);
Jingchang Lud6be34f2014-02-18 10:17:12 +0800317 return PTR_ERR(fsl_edma->muxclk[i]);
318 }
319
320 ret = clk_prepare_enable(fsl_edma->muxclk[i]);
Andreas Platschek2610acf2017-12-14 12:50:51 +0100321 if (ret)
322 /* on error: disable all previously enabled clks */
323 fsl_disable_clocks(fsl_edma, i);
Jingchang Lud6be34f2014-02-18 10:17:12 +0800324
325 }
326
Jingchang Lud6be34f2014-02-18 10:17:12 +0800327 fsl_edma->big_endian = of_property_read_bool(np, "big-endian");
328
329 INIT_LIST_HEAD(&fsl_edma->dma_dev.channels);
330 for (i = 0; i < fsl_edma->n_chans; i++) {
331 struct fsl_edma_chan *fsl_chan = &fsl_edma->chans[i];
332
333 fsl_chan->edma = fsl_edma;
Yuan Yao82d149b2015-10-30 19:03:58 +0800334 fsl_chan->pm_state = RUNNING;
335 fsl_chan->slave_id = 0;
336 fsl_chan->idle = true;
Laurentiu Tudor0fa89f92019-01-18 12:06:23 +0200337 fsl_chan->dma_dir = DMA_NONE;
Jingchang Lud6be34f2014-02-18 10:17:12 +0800338 fsl_chan->vchan.desc_free = fsl_edma_free_desc;
339 vchan_init(&fsl_chan->vchan, &fsl_edma->dma_dev);
340
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200341 edma_writew(fsl_edma, 0x0, &regs->tcd[i].csr);
Jingchang Lud6be34f2014-02-18 10:17:12 +0800342 fsl_edma_chan_mux(fsl_chan, 0, false);
343 }
344
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200345 edma_writel(fsl_edma, ~0, regs->intl);
Robin Gongaf802722019-06-25 17:43:19 +0800346 ret = fsl_edma->drvdata->setup_irq(pdev, fsl_edma);
Stefan Agner0fe25d62015-06-07 21:46:10 +0200347 if (ret)
348 return ret;
349
Jingchang Lud6be34f2014-02-18 10:17:12 +0800350 dma_cap_set(DMA_PRIVATE, fsl_edma->dma_dev.cap_mask);
351 dma_cap_set(DMA_SLAVE, fsl_edma->dma_dev.cap_mask);
352 dma_cap_set(DMA_CYCLIC, fsl_edma->dma_dev.cap_mask);
353
354 fsl_edma->dma_dev.dev = &pdev->dev;
355 fsl_edma->dma_dev.device_alloc_chan_resources
356 = fsl_edma_alloc_chan_resources;
357 fsl_edma->dma_dev.device_free_chan_resources
358 = fsl_edma_free_chan_resources;
359 fsl_edma->dma_dev.device_tx_status = fsl_edma_tx_status;
360 fsl_edma->dma_dev.device_prep_slave_sg = fsl_edma_prep_slave_sg;
361 fsl_edma->dma_dev.device_prep_dma_cyclic = fsl_edma_prep_dma_cyclic;
Maxime Ripardd80f3812014-11-17 14:42:15 +0100362 fsl_edma->dma_dev.device_config = fsl_edma_slave_config;
363 fsl_edma->dma_dev.device_pause = fsl_edma_pause;
364 fsl_edma->dma_dev.device_resume = fsl_edma_resume;
365 fsl_edma->dma_dev.device_terminate_all = fsl_edma_terminate_all;
Jingchang Lud6be34f2014-02-18 10:17:12 +0800366 fsl_edma->dma_dev.device_issue_pending = fsl_edma_issue_pending;
Maxime Ripardf45c4312014-11-17 14:42:46 +0100367
368 fsl_edma->dma_dev.src_addr_widths = FSL_EDMA_BUSWIDTHS;
369 fsl_edma->dma_dev.dst_addr_widths = FSL_EDMA_BUSWIDTHS;
370 fsl_edma->dma_dev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
Jingchang Lud6be34f2014-02-18 10:17:12 +0800371
372 platform_set_drvdata(pdev, fsl_edma);
373
374 ret = dma_async_device_register(&fsl_edma->dma_dev);
375 if (ret) {
Peter Griffina86144d2016-06-07 18:38:35 +0100376 dev_err(&pdev->dev,
377 "Can't register Freescale eDMA engine. (%d)\n", ret);
Robin Gongaf802722019-06-25 17:43:19 +0800378 fsl_disable_clocks(fsl_edma, fsl_edma->drvdata->dmamuxs);
Jingchang Lud6be34f2014-02-18 10:17:12 +0800379 return ret;
380 }
381
382 ret = of_dma_controller_register(np, fsl_edma_xlate, fsl_edma);
383 if (ret) {
Peter Griffina86144d2016-06-07 18:38:35 +0100384 dev_err(&pdev->dev,
385 "Can't register Freescale eDMA of_dma. (%d)\n", ret);
Jingchang Lud6be34f2014-02-18 10:17:12 +0800386 dma_async_device_unregister(&fsl_edma->dma_dev);
Robin Gongaf802722019-06-25 17:43:19 +0800387 fsl_disable_clocks(fsl_edma, fsl_edma->drvdata->dmamuxs);
Jingchang Lud6be34f2014-02-18 10:17:12 +0800388 return ret;
389 }
390
391 /* enable round robin arbitration */
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200392 edma_writel(fsl_edma, EDMA_CR_ERGA | EDMA_CR_ERCA, regs->cr);
Jingchang Lud6be34f2014-02-18 10:17:12 +0800393
394 return 0;
395}
396
397static int fsl_edma_remove(struct platform_device *pdev)
398{
399 struct device_node *np = pdev->dev.of_node;
400 struct fsl_edma_engine *fsl_edma = platform_get_drvdata(pdev);
Jingchang Lud6be34f2014-02-18 10:17:12 +0800401
Vinod Koul476c7c82016-07-01 17:34:14 +0530402 fsl_edma_irq_exit(pdev, fsl_edma);
Vinod Koul6f93b932016-07-02 14:58:30 +0530403 fsl_edma_cleanup_vchan(&fsl_edma->dma_dev);
Jingchang Lud6be34f2014-02-18 10:17:12 +0800404 of_dma_controller_free(np);
405 dma_async_device_unregister(&fsl_edma->dma_dev);
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
408 return 0;
409}
410
Yuan Yao82d149b2015-10-30 19:03:58 +0800411static int fsl_edma_suspend_late(struct device *dev)
412{
413 struct fsl_edma_engine *fsl_edma = dev_get_drvdata(dev);
414 struct fsl_edma_chan *fsl_chan;
415 unsigned long flags;
416 int i;
417
418 for (i = 0; i < fsl_edma->n_chans; i++) {
419 fsl_chan = &fsl_edma->chans[i];
420 spin_lock_irqsave(&fsl_chan->vchan.lock, flags);
421 /* Make sure chan is idle or will force disable. */
422 if (unlikely(!fsl_chan->idle)) {
423 dev_warn(dev, "WARN: There is non-idle channel.");
424 fsl_edma_disable_request(fsl_chan);
425 fsl_edma_chan_mux(fsl_chan, 0, false);
426 }
427
428 fsl_chan->pm_state = SUSPENDED;
429 spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags);
430 }
431
432 return 0;
433}
434
435static int fsl_edma_resume_early(struct device *dev)
436{
437 struct fsl_edma_engine *fsl_edma = dev_get_drvdata(dev);
438 struct fsl_edma_chan *fsl_chan;
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200439 struct edma_regs *regs = &fsl_edma->regs;
Yuan Yao82d149b2015-10-30 19:03:58 +0800440 int i;
441
442 for (i = 0; i < fsl_edma->n_chans; i++) {
443 fsl_chan = &fsl_edma->chans[i];
444 fsl_chan->pm_state = RUNNING;
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200445 edma_writew(fsl_edma, 0x0, &regs->tcd[i].csr);
Yuan Yao82d149b2015-10-30 19:03:58 +0800446 if (fsl_chan->slave_id != 0)
447 fsl_edma_chan_mux(fsl_chan, fsl_chan->slave_id, true);
448 }
449
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200450 edma_writel(fsl_edma, EDMA_CR_ERGA | EDMA_CR_ERCA, regs->cr);
Yuan Yao82d149b2015-10-30 19:03:58 +0800451
452 return 0;
453}
454
455/*
456 * eDMA provides the service to others, so it should be suspend late
457 * and resume early. When eDMA suspend, all of the clients should stop
458 * the DMA data transmission and let the channel idle.
459 */
460static const struct dev_pm_ops fsl_edma_pm_ops = {
461 .suspend_late = fsl_edma_suspend_late,
462 .resume_early = fsl_edma_resume_early,
463};
464
Jingchang Lud6be34f2014-02-18 10:17:12 +0800465static struct platform_driver fsl_edma_driver = {
466 .driver = {
467 .name = "fsl-edma",
Jingchang Lud6be34f2014-02-18 10:17:12 +0800468 .of_match_table = fsl_edma_dt_ids,
Yuan Yao82d149b2015-10-30 19:03:58 +0800469 .pm = &fsl_edma_pm_ops,
Jingchang Lud6be34f2014-02-18 10:17:12 +0800470 },
471 .probe = fsl_edma_probe,
472 .remove = fsl_edma_remove,
473};
474
Yuan Yao8edc51c2014-04-04 12:27:55 +0800475static int __init fsl_edma_init(void)
476{
477 return platform_driver_register(&fsl_edma_driver);
478}
479subsys_initcall(fsl_edma_init);
480
481static void __exit fsl_edma_exit(void)
482{
483 platform_driver_unregister(&fsl_edma_driver);
484}
485module_exit(fsl_edma_exit);
Jingchang Lud6be34f2014-02-18 10:17:12 +0800486
487MODULE_ALIAS("platform:fsl-edma");
488MODULE_DESCRIPTION("Freescale eDMA engine driver");
489MODULE_LICENSE("GPL v2");