2 * drivers/dma/fsl-edma.c
4 * Copyright 2013-2014 Freescale Semiconductor, Inc.
6 * Driver for the Freescale eDMA engine with flexible channel multiplexing
7 * capability for DMA request sources. The eDMA block can be found on some
8 * Vybrid and Layerscape SoCs.
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
16 #include <linux/module.h>
17 #include <linux/interrupt.h>
18 #include <linux/clk.h>
20 #include <linux/of_device.h>
21 #include <linux/of_address.h>
22 #include <linux/of_irq.h>
23 #include <linux/of_dma.h>
25 #include "fsl-edma-common.h"
27 static irqreturn_t
fsl_edma_tx_handler(int irq
, void *dev_id
)
29 struct fsl_edma_engine
*fsl_edma
= dev_id
;
30 unsigned int intr
, ch
;
31 struct edma_regs
*regs
= &fsl_edma
->regs
;
32 struct fsl_edma_chan
*fsl_chan
;
34 intr
= edma_readl(fsl_edma
, regs
->intl
);
38 for (ch
= 0; ch
< fsl_edma
->n_chans
; ch
++) {
39 if (intr
& (0x1 << ch
)) {
40 edma_writeb(fsl_edma
, EDMA_CINT_CINT(ch
), regs
->cint
);
42 fsl_chan
= &fsl_edma
->chans
[ch
];
44 spin_lock(&fsl_chan
->vchan
.lock
);
45 if (!fsl_chan
->edesc
->iscyclic
) {
46 list_del(&fsl_chan
->edesc
->vdesc
.node
);
47 vchan_cookie_complete(&fsl_chan
->edesc
->vdesc
);
48 fsl_chan
->edesc
= NULL
;
49 fsl_chan
->status
= DMA_COMPLETE
;
50 fsl_chan
->idle
= true;
52 vchan_cyclic_callback(&fsl_chan
->edesc
->vdesc
);
56 fsl_edma_xfer_desc(fsl_chan
);
58 spin_unlock(&fsl_chan
->vchan
.lock
);
64 static irqreturn_t
fsl_edma_err_handler(int irq
, void *dev_id
)
66 struct fsl_edma_engine
*fsl_edma
= dev_id
;
68 struct edma_regs
*regs
= &fsl_edma
->regs
;
70 err
= edma_readl(fsl_edma
, regs
->errl
);
74 for (ch
= 0; ch
< fsl_edma
->n_chans
; ch
++) {
75 if (err
& (0x1 << ch
)) {
76 fsl_edma_disable_request(&fsl_edma
->chans
[ch
]);
77 edma_writeb(fsl_edma
, EDMA_CERR_CERR(ch
), regs
->cerr
);
78 fsl_edma
->chans
[ch
].status
= DMA_ERROR
;
79 fsl_edma
->chans
[ch
].idle
= true;
85 static irqreturn_t
fsl_edma_irq_handler(int irq
, void *dev_id
)
87 if (fsl_edma_tx_handler(irq
, dev_id
) == IRQ_HANDLED
)
90 return fsl_edma_err_handler(irq
, dev_id
);
93 static struct dma_chan
*fsl_edma_xlate(struct of_phandle_args
*dma_spec
,
96 struct fsl_edma_engine
*fsl_edma
= ofdma
->of_dma_data
;
97 struct dma_chan
*chan
, *_chan
;
98 struct fsl_edma_chan
*fsl_chan
;
99 unsigned long chans_per_mux
= fsl_edma
->n_chans
/ DMAMUX_NR
;
101 if (dma_spec
->args_count
!= 2)
104 mutex_lock(&fsl_edma
->fsl_edma_mutex
);
105 list_for_each_entry_safe(chan
, _chan
, &fsl_edma
->dma_dev
.channels
, device_node
) {
106 if (chan
->client_count
)
108 if ((chan
->chan_id
/ chans_per_mux
) == dma_spec
->args
[0]) {
109 chan
= dma_get_slave_channel(chan
);
111 chan
->device
->privatecnt
++;
112 fsl_chan
= to_fsl_edma_chan(chan
);
113 fsl_chan
->slave_id
= dma_spec
->args
[1];
114 fsl_edma_chan_mux(fsl_chan
, fsl_chan
->slave_id
,
116 mutex_unlock(&fsl_edma
->fsl_edma_mutex
);
121 mutex_unlock(&fsl_edma
->fsl_edma_mutex
);
126 fsl_edma_irq_init(struct platform_device
*pdev
, struct fsl_edma_engine
*fsl_edma
)
130 fsl_edma
->txirq
= platform_get_irq_byname(pdev
, "edma-tx");
131 if (fsl_edma
->txirq
< 0) {
132 dev_err(&pdev
->dev
, "Can't get edma-tx irq.\n");
133 return fsl_edma
->txirq
;
136 fsl_edma
->errirq
= platform_get_irq_byname(pdev
, "edma-err");
137 if (fsl_edma
->errirq
< 0) {
138 dev_err(&pdev
->dev
, "Can't get edma-err irq.\n");
139 return fsl_edma
->errirq
;
142 if (fsl_edma
->txirq
== fsl_edma
->errirq
) {
143 ret
= devm_request_irq(&pdev
->dev
, fsl_edma
->txirq
,
144 fsl_edma_irq_handler
, 0, "eDMA", fsl_edma
);
146 dev_err(&pdev
->dev
, "Can't register eDMA IRQ.\n");
150 ret
= devm_request_irq(&pdev
->dev
, fsl_edma
->txirq
,
151 fsl_edma_tx_handler
, 0, "eDMA tx", fsl_edma
);
153 dev_err(&pdev
->dev
, "Can't register eDMA tx IRQ.\n");
157 ret
= devm_request_irq(&pdev
->dev
, fsl_edma
->errirq
,
158 fsl_edma_err_handler
, 0, "eDMA err", fsl_edma
);
160 dev_err(&pdev
->dev
, "Can't register eDMA err IRQ.\n");
168 static void fsl_edma_irq_exit(
169 struct platform_device
*pdev
, struct fsl_edma_engine
*fsl_edma
)
171 if (fsl_edma
->txirq
== fsl_edma
->errirq
) {
172 devm_free_irq(&pdev
->dev
, fsl_edma
->txirq
, fsl_edma
);
174 devm_free_irq(&pdev
->dev
, fsl_edma
->txirq
, fsl_edma
);
175 devm_free_irq(&pdev
->dev
, fsl_edma
->errirq
, fsl_edma
);
179 static void fsl_disable_clocks(struct fsl_edma_engine
*fsl_edma
, int nr_clocks
)
183 for (i
= 0; i
< nr_clocks
; i
++)
184 clk_disable_unprepare(fsl_edma
->muxclk
[i
]);
187 static int fsl_edma_probe(struct platform_device
*pdev
)
189 struct device_node
*np
= pdev
->dev
.of_node
;
190 struct fsl_edma_engine
*fsl_edma
;
191 struct fsl_edma_chan
*fsl_chan
;
192 struct edma_regs
*regs
;
193 struct resource
*res
;
197 ret
= of_property_read_u32(np
, "dma-channels", &chans
);
199 dev_err(&pdev
->dev
, "Can't get dma-channels.\n");
203 len
= sizeof(*fsl_edma
) + sizeof(*fsl_chan
) * chans
;
204 fsl_edma
= devm_kzalloc(&pdev
->dev
, len
, GFP_KERNEL
);
208 fsl_edma
->version
= v1
;
209 fsl_edma
->n_chans
= chans
;
210 mutex_init(&fsl_edma
->fsl_edma_mutex
);
212 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
213 fsl_edma
->membase
= devm_ioremap_resource(&pdev
->dev
, res
);
214 if (IS_ERR(fsl_edma
->membase
))
215 return PTR_ERR(fsl_edma
->membase
);
217 fsl_edma_setup_regs(fsl_edma
);
218 regs
= &fsl_edma
->regs
;
220 for (i
= 0; i
< DMAMUX_NR
; i
++) {
223 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1 + i
);
224 fsl_edma
->muxbase
[i
] = devm_ioremap_resource(&pdev
->dev
, res
);
225 if (IS_ERR(fsl_edma
->muxbase
[i
])) {
226 /* on error: disable all previously enabled clks */
227 fsl_disable_clocks(fsl_edma
, i
);
228 return PTR_ERR(fsl_edma
->muxbase
[i
]);
231 sprintf(clkname
, "dmamux%d", i
);
232 fsl_edma
->muxclk
[i
] = devm_clk_get(&pdev
->dev
, clkname
);
233 if (IS_ERR(fsl_edma
->muxclk
[i
])) {
234 dev_err(&pdev
->dev
, "Missing DMAMUX block clock.\n");
235 /* on error: disable all previously enabled clks */
236 fsl_disable_clocks(fsl_edma
, i
);
237 return PTR_ERR(fsl_edma
->muxclk
[i
]);
240 ret
= clk_prepare_enable(fsl_edma
->muxclk
[i
]);
242 /* on error: disable all previously enabled clks */
243 fsl_disable_clocks(fsl_edma
, i
);
247 fsl_edma
->big_endian
= of_property_read_bool(np
, "big-endian");
249 INIT_LIST_HEAD(&fsl_edma
->dma_dev
.channels
);
250 for (i
= 0; i
< fsl_edma
->n_chans
; i
++) {
251 struct fsl_edma_chan
*fsl_chan
= &fsl_edma
->chans
[i
];
253 fsl_chan
->edma
= fsl_edma
;
254 fsl_chan
->pm_state
= RUNNING
;
255 fsl_chan
->slave_id
= 0;
256 fsl_chan
->idle
= true;
257 fsl_chan
->vchan
.desc_free
= fsl_edma_free_desc
;
258 vchan_init(&fsl_chan
->vchan
, &fsl_edma
->dma_dev
);
260 edma_writew(fsl_edma
, 0x0, ®s
->tcd
[i
].csr
);
261 fsl_edma_chan_mux(fsl_chan
, 0, false);
264 edma_writel(fsl_edma
, ~0, regs
->intl
);
265 ret
= fsl_edma_irq_init(pdev
, fsl_edma
);
269 dma_cap_set(DMA_PRIVATE
, fsl_edma
->dma_dev
.cap_mask
);
270 dma_cap_set(DMA_SLAVE
, fsl_edma
->dma_dev
.cap_mask
);
271 dma_cap_set(DMA_CYCLIC
, fsl_edma
->dma_dev
.cap_mask
);
273 fsl_edma
->dma_dev
.dev
= &pdev
->dev
;
274 fsl_edma
->dma_dev
.device_alloc_chan_resources
275 = fsl_edma_alloc_chan_resources
;
276 fsl_edma
->dma_dev
.device_free_chan_resources
277 = fsl_edma_free_chan_resources
;
278 fsl_edma
->dma_dev
.device_tx_status
= fsl_edma_tx_status
;
279 fsl_edma
->dma_dev
.device_prep_slave_sg
= fsl_edma_prep_slave_sg
;
280 fsl_edma
->dma_dev
.device_prep_dma_cyclic
= fsl_edma_prep_dma_cyclic
;
281 fsl_edma
->dma_dev
.device_config
= fsl_edma_slave_config
;
282 fsl_edma
->dma_dev
.device_pause
= fsl_edma_pause
;
283 fsl_edma
->dma_dev
.device_resume
= fsl_edma_resume
;
284 fsl_edma
->dma_dev
.device_terminate_all
= fsl_edma_terminate_all
;
285 fsl_edma
->dma_dev
.device_issue_pending
= fsl_edma_issue_pending
;
287 fsl_edma
->dma_dev
.src_addr_widths
= FSL_EDMA_BUSWIDTHS
;
288 fsl_edma
->dma_dev
.dst_addr_widths
= FSL_EDMA_BUSWIDTHS
;
289 fsl_edma
->dma_dev
.directions
= BIT(DMA_DEV_TO_MEM
) | BIT(DMA_MEM_TO_DEV
);
291 platform_set_drvdata(pdev
, fsl_edma
);
293 ret
= dma_async_device_register(&fsl_edma
->dma_dev
);
296 "Can't register Freescale eDMA engine. (%d)\n", ret
);
297 fsl_disable_clocks(fsl_edma
, DMAMUX_NR
);
301 ret
= of_dma_controller_register(np
, fsl_edma_xlate
, fsl_edma
);
304 "Can't register Freescale eDMA of_dma. (%d)\n", ret
);
305 dma_async_device_unregister(&fsl_edma
->dma_dev
);
306 fsl_disable_clocks(fsl_edma
, DMAMUX_NR
);
310 /* enable round robin arbitration */
311 edma_writel(fsl_edma
, EDMA_CR_ERGA
| EDMA_CR_ERCA
, regs
->cr
);
316 static int fsl_edma_remove(struct platform_device
*pdev
)
318 struct device_node
*np
= pdev
->dev
.of_node
;
319 struct fsl_edma_engine
*fsl_edma
= platform_get_drvdata(pdev
);
321 fsl_edma_irq_exit(pdev
, fsl_edma
);
322 fsl_edma_cleanup_vchan(&fsl_edma
->dma_dev
);
323 of_dma_controller_free(np
);
324 dma_async_device_unregister(&fsl_edma
->dma_dev
);
325 fsl_disable_clocks(fsl_edma
, DMAMUX_NR
);
330 static int fsl_edma_suspend_late(struct device
*dev
)
332 struct fsl_edma_engine
*fsl_edma
= dev_get_drvdata(dev
);
333 struct fsl_edma_chan
*fsl_chan
;
337 for (i
= 0; i
< fsl_edma
->n_chans
; i
++) {
338 fsl_chan
= &fsl_edma
->chans
[i
];
339 spin_lock_irqsave(&fsl_chan
->vchan
.lock
, flags
);
340 /* Make sure chan is idle or will force disable. */
341 if (unlikely(!fsl_chan
->idle
)) {
342 dev_warn(dev
, "WARN: There is non-idle channel.");
343 fsl_edma_disable_request(fsl_chan
);
344 fsl_edma_chan_mux(fsl_chan
, 0, false);
347 fsl_chan
->pm_state
= SUSPENDED
;
348 spin_unlock_irqrestore(&fsl_chan
->vchan
.lock
, flags
);
354 static int fsl_edma_resume_early(struct device
*dev
)
356 struct fsl_edma_engine
*fsl_edma
= dev_get_drvdata(dev
);
357 struct fsl_edma_chan
*fsl_chan
;
358 struct edma_regs
*regs
= &fsl_edma
->regs
;
361 for (i
= 0; i
< fsl_edma
->n_chans
; i
++) {
362 fsl_chan
= &fsl_edma
->chans
[i
];
363 fsl_chan
->pm_state
= RUNNING
;
364 edma_writew(fsl_edma
, 0x0, ®s
->tcd
[i
].csr
);
365 if (fsl_chan
->slave_id
!= 0)
366 fsl_edma_chan_mux(fsl_chan
, fsl_chan
->slave_id
, true);
369 edma_writel(fsl_edma
, EDMA_CR_ERGA
| EDMA_CR_ERCA
, regs
->cr
);
375 * eDMA provides the service to others, so it should be suspend late
376 * and resume early. When eDMA suspend, all of the clients should stop
377 * the DMA data transmission and let the channel idle.
379 static const struct dev_pm_ops fsl_edma_pm_ops
= {
380 .suspend_late
= fsl_edma_suspend_late
,
381 .resume_early
= fsl_edma_resume_early
,
384 static const struct of_device_id fsl_edma_dt_ids
[] = {
385 { .compatible
= "fsl,vf610-edma", },
388 MODULE_DEVICE_TABLE(of
, fsl_edma_dt_ids
);
390 static struct platform_driver fsl_edma_driver
= {
393 .of_match_table
= fsl_edma_dt_ids
,
394 .pm
= &fsl_edma_pm_ops
,
396 .probe
= fsl_edma_probe
,
397 .remove
= fsl_edma_remove
,
400 static int __init
fsl_edma_init(void)
402 return platform_driver_register(&fsl_edma_driver
);
404 subsys_initcall(fsl_edma_init
);
406 static void __exit
fsl_edma_exit(void)
408 platform_driver_unregister(&fsl_edma_driver
);
410 module_exit(fsl_edma_exit
);
412 MODULE_ALIAS("platform:fsl-edma");
413 MODULE_DESCRIPTION("Freescale eDMA engine driver");
414 MODULE_LICENSE("GPL v2");