Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 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. |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 10 | */ |
| 11 | |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 12 | #include <linux/module.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/clk.h> |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 15 | #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 Dureghello | 9d83152 | 2018-08-19 19:27:13 +0200 | [diff] [blame] | 21 | #include "fsl-edma-common.h" |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 22 | |
| 23 | static 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 Dureghello | 377eaf3 | 2018-08-19 19:27:14 +0200 | [diff] [blame] | 27 | struct edma_regs *regs = &fsl_edma->regs; |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 28 | struct fsl_edma_chan *fsl_chan; |
| 29 | |
Angelo Dureghello | 377eaf3 | 2018-08-19 19:27:14 +0200 | [diff] [blame] | 30 | intr = edma_readl(fsl_edma, regs->intl); |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 31 | if (!intr) |
| 32 | return IRQ_NONE; |
| 33 | |
| 34 | for (ch = 0; ch < fsl_edma->n_chans; ch++) { |
| 35 | if (intr & (0x1 << ch)) { |
Angelo Dureghello | 377eaf3 | 2018-08-19 19:27:14 +0200 | [diff] [blame] | 36 | edma_writeb(fsl_edma, EDMA_CINT_CINT(ch), regs->cint); |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 37 | |
| 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 Yao | 82d149b | 2015-10-30 19:03:58 +0800 | [diff] [blame] | 46 | fsl_chan->idle = true; |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 47 | } 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 | |
| 60 | static 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 Dureghello | 377eaf3 | 2018-08-19 19:27:14 +0200 | [diff] [blame] | 64 | struct edma_regs *regs = &fsl_edma->regs; |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 65 | |
Angelo Dureghello | 377eaf3 | 2018-08-19 19:27:14 +0200 | [diff] [blame] | 66 | err = edma_readl(fsl_edma, regs->errl); |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 67 | 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 Dureghello | 377eaf3 | 2018-08-19 19:27:14 +0200 | [diff] [blame] | 73 | edma_writeb(fsl_edma, EDMA_CERR_CERR(ch), regs->cerr); |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 74 | fsl_edma->chans[ch].status = DMA_ERROR; |
Yuan Yao | 82d149b | 2015-10-30 19:03:58 +0800 | [diff] [blame] | 75 | fsl_edma->chans[ch].idle = true; |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | return IRQ_HANDLED; |
| 79 | } |
| 80 | |
| 81 | static 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 Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 89 | static 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 Lu | 178c81e | 2014-02-21 14:50:06 +0800 | [diff] [blame] | 93 | struct dma_chan *chan, *_chan; |
Yuan Yao | 82d149b | 2015-10-30 19:03:58 +0800 | [diff] [blame] | 94 | struct fsl_edma_chan *fsl_chan; |
Robin Gong | af80272 | 2019-06-25 17:43:19 +0800 | [diff] [blame] | 95 | u32 dmamux_nr = fsl_edma->drvdata->dmamuxs; |
| 96 | unsigned long chans_per_mux = fsl_edma->n_chans / dmamux_nr; |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 97 | |
| 98 | if (dma_spec->args_count != 2) |
| 99 | return NULL; |
| 100 | |
| 101 | mutex_lock(&fsl_edma->fsl_edma_mutex); |
Jingchang Lu | 178c81e | 2014-02-21 14:50:06 +0800 | [diff] [blame] | 102 | list_for_each_entry_safe(chan, _chan, &fsl_edma->dma_dev.channels, device_node) { |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 103 | if (chan->client_count) |
| 104 | continue; |
Jingchang Lu | 211bfef | 2014-07-01 16:41:03 +0800 | [diff] [blame] | 105 | if ((chan->chan_id / chans_per_mux) == dma_spec->args[0]) { |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 106 | chan = dma_get_slave_channel(chan); |
| 107 | if (chan) { |
| 108 | chan->device->privatecnt++; |
Yuan Yao | 82d149b | 2015-10-30 19:03:58 +0800 | [diff] [blame] | 109 | 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 Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 113 | 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 Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 122 | static int |
| 123 | fsl_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 Boyd | e17be6e | 2019-07-30 11:15:10 -0700 | [diff] [blame] | 128 | if (fsl_edma->txirq < 0) |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 129 | return fsl_edma->txirq; |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 130 | |
| 131 | fsl_edma->errirq = platform_get_irq_byname(pdev, "edma-err"); |
Stephen Boyd | e17be6e | 2019-07-30 11:15:10 -0700 | [diff] [blame] | 132 | if (fsl_edma->errirq < 0) |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 133 | return fsl_edma->errirq; |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 134 | |
| 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 Kozlowski | e095189 | 2019-05-04 11:52:25 +0200 | [diff] [blame] | 140 | return ret; |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 141 | } |
| 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 Kozlowski | e095189 | 2019-05-04 11:52:25 +0200 | [diff] [blame] | 147 | return ret; |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 148 | } |
| 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 Kozlowski | e095189 | 2019-05-04 11:52:25 +0200 | [diff] [blame] | 154 | return ret; |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
Robin Gong | 232a7f1 | 2019-07-24 15:20:34 +0800 | [diff] [blame^] | 161 | static int |
| 162 | fsl_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 Koul | 476c7c8 | 2016-07-01 17:34:14 +0530 | [diff] [blame] | 204 | static 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 Platschek | 2610acf | 2017-12-14 12:50:51 +0100 | [diff] [blame] | 215 | static void fsl_disable_clocks(struct fsl_edma_engine *fsl_edma, int nr_clocks) |
Peter Griffin | 5e2fe1e | 2016-06-07 18:38:34 +0100 | [diff] [blame] | 216 | { |
| 217 | int i; |
| 218 | |
Andreas Platschek | 2610acf | 2017-12-14 12:50:51 +0100 | [diff] [blame] | 219 | for (i = 0; i < nr_clocks; i++) |
Peter Griffin | 5e2fe1e | 2016-06-07 18:38:34 +0100 | [diff] [blame] | 220 | clk_disable_unprepare(fsl_edma->muxclk[i]); |
| 221 | } |
| 222 | |
Robin Gong | af80272 | 2019-06-25 17:43:19 +0800 | [diff] [blame] | 223 | static struct fsl_edma_drvdata vf610_data = { |
| 224 | .version = v1, |
| 225 | .dmamuxs = DMAMUX_NR, |
| 226 | .setup_irq = fsl_edma_irq_init, |
| 227 | }; |
| 228 | |
Robin Gong | 232a7f1 | 2019-07-24 15:20:34 +0800 | [diff] [blame^] | 229 | static 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 Gong | af80272 | 2019-06-25 17:43:19 +0800 | [diff] [blame] | 236 | static const struct of_device_id fsl_edma_dt_ids[] = { |
| 237 | { .compatible = "fsl,vf610-edma", .data = &vf610_data}, |
Robin Gong | 232a7f1 | 2019-07-24 15:20:34 +0800 | [diff] [blame^] | 238 | { .compatible = "fsl,imx7ulp-edma", .data = &imx7ulp_data}, |
Robin Gong | af80272 | 2019-06-25 17:43:19 +0800 | [diff] [blame] | 239 | { /* sentinel */ } |
| 240 | }; |
| 241 | MODULE_DEVICE_TABLE(of, fsl_edma_dt_ids); |
| 242 | |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 243 | static int fsl_edma_probe(struct platform_device *pdev) |
| 244 | { |
Robin Gong | af80272 | 2019-06-25 17:43:19 +0800 | [diff] [blame] | 245 | const struct of_device_id *of_id = |
| 246 | of_match_device(fsl_edma_dt_ids, &pdev->dev); |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 247 | struct device_node *np = pdev->dev.of_node; |
| 248 | struct fsl_edma_engine *fsl_edma; |
Robin Gong | af80272 | 2019-06-25 17:43:19 +0800 | [diff] [blame] | 249 | const struct fsl_edma_drvdata *drvdata = NULL; |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 250 | struct fsl_edma_chan *fsl_chan; |
Angelo Dureghello | 377eaf3 | 2018-08-19 19:27:14 +0200 | [diff] [blame] | 251 | struct edma_regs *regs; |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 252 | struct resource *res; |
| 253 | int len, chans; |
| 254 | int ret, i; |
| 255 | |
Robin Gong | af80272 | 2019-06-25 17:43:19 +0800 | [diff] [blame] | 256 | 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 Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 263 | 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 Gong | af80272 | 2019-06-25 17:43:19 +0800 | [diff] [blame] | 274 | fsl_edma->drvdata = drvdata; |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 275 | 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 Dureghello | 377eaf3 | 2018-08-19 19:27:14 +0200 | [diff] [blame] | 283 | fsl_edma_setup_regs(fsl_edma); |
| 284 | regs = &fsl_edma->regs; |
| 285 | |
Robin Gong | 232a7f1 | 2019-07-24 15:20:34 +0800 | [diff] [blame^] | 286 | 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 Gong | af80272 | 2019-06-25 17:43:19 +0800 | [diff] [blame] | 300 | for (i = 0; i < fsl_edma->drvdata->dmamuxs; i++) { |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 301 | 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 Platschek | 2610acf | 2017-12-14 12:50:51 +0100 | [diff] [blame] | 305 | if (IS_ERR(fsl_edma->muxbase[i])) { |
| 306 | /* on error: disable all previously enabled clks */ |
| 307 | fsl_disable_clocks(fsl_edma, i); |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 308 | return PTR_ERR(fsl_edma->muxbase[i]); |
Andreas Platschek | 2610acf | 2017-12-14 12:50:51 +0100 | [diff] [blame] | 309 | } |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 310 | |
| 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 Platschek | 2610acf | 2017-12-14 12:50:51 +0100 | [diff] [blame] | 315 | /* on error: disable all previously enabled clks */ |
| 316 | fsl_disable_clocks(fsl_edma, i); |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 317 | return PTR_ERR(fsl_edma->muxclk[i]); |
| 318 | } |
| 319 | |
| 320 | ret = clk_prepare_enable(fsl_edma->muxclk[i]); |
Andreas Platschek | 2610acf | 2017-12-14 12:50:51 +0100 | [diff] [blame] | 321 | if (ret) |
| 322 | /* on error: disable all previously enabled clks */ |
| 323 | fsl_disable_clocks(fsl_edma, i); |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 324 | |
| 325 | } |
| 326 | |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 327 | 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 Yao | 82d149b | 2015-10-30 19:03:58 +0800 | [diff] [blame] | 334 | fsl_chan->pm_state = RUNNING; |
| 335 | fsl_chan->slave_id = 0; |
| 336 | fsl_chan->idle = true; |
Laurentiu Tudor | 0fa89f9 | 2019-01-18 12:06:23 +0200 | [diff] [blame] | 337 | fsl_chan->dma_dir = DMA_NONE; |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 338 | fsl_chan->vchan.desc_free = fsl_edma_free_desc; |
| 339 | vchan_init(&fsl_chan->vchan, &fsl_edma->dma_dev); |
| 340 | |
Angelo Dureghello | 377eaf3 | 2018-08-19 19:27:14 +0200 | [diff] [blame] | 341 | edma_writew(fsl_edma, 0x0, ®s->tcd[i].csr); |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 342 | fsl_edma_chan_mux(fsl_chan, 0, false); |
| 343 | } |
| 344 | |
Angelo Dureghello | 377eaf3 | 2018-08-19 19:27:14 +0200 | [diff] [blame] | 345 | edma_writel(fsl_edma, ~0, regs->intl); |
Robin Gong | af80272 | 2019-06-25 17:43:19 +0800 | [diff] [blame] | 346 | ret = fsl_edma->drvdata->setup_irq(pdev, fsl_edma); |
Stefan Agner | 0fe25d6 | 2015-06-07 21:46:10 +0200 | [diff] [blame] | 347 | if (ret) |
| 348 | return ret; |
| 349 | |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 350 | 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 Ripard | d80f381 | 2014-11-17 14:42:15 +0100 | [diff] [blame] | 362 | 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 Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 366 | fsl_edma->dma_dev.device_issue_pending = fsl_edma_issue_pending; |
Maxime Ripard | f45c431 | 2014-11-17 14:42:46 +0100 | [diff] [blame] | 367 | |
| 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 Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 371 | |
| 372 | platform_set_drvdata(pdev, fsl_edma); |
| 373 | |
| 374 | ret = dma_async_device_register(&fsl_edma->dma_dev); |
| 375 | if (ret) { |
Peter Griffin | a86144d | 2016-06-07 18:38:35 +0100 | [diff] [blame] | 376 | dev_err(&pdev->dev, |
| 377 | "Can't register Freescale eDMA engine. (%d)\n", ret); |
Robin Gong | af80272 | 2019-06-25 17:43:19 +0800 | [diff] [blame] | 378 | fsl_disable_clocks(fsl_edma, fsl_edma->drvdata->dmamuxs); |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 379 | return ret; |
| 380 | } |
| 381 | |
| 382 | ret = of_dma_controller_register(np, fsl_edma_xlate, fsl_edma); |
| 383 | if (ret) { |
Peter Griffin | a86144d | 2016-06-07 18:38:35 +0100 | [diff] [blame] | 384 | dev_err(&pdev->dev, |
| 385 | "Can't register Freescale eDMA of_dma. (%d)\n", ret); |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 386 | dma_async_device_unregister(&fsl_edma->dma_dev); |
Robin Gong | af80272 | 2019-06-25 17:43:19 +0800 | [diff] [blame] | 387 | fsl_disable_clocks(fsl_edma, fsl_edma->drvdata->dmamuxs); |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 388 | return ret; |
| 389 | } |
| 390 | |
| 391 | /* enable round robin arbitration */ |
Angelo Dureghello | 377eaf3 | 2018-08-19 19:27:14 +0200 | [diff] [blame] | 392 | edma_writel(fsl_edma, EDMA_CR_ERGA | EDMA_CR_ERCA, regs->cr); |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | static 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 Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 401 | |
Vinod Koul | 476c7c8 | 2016-07-01 17:34:14 +0530 | [diff] [blame] | 402 | fsl_edma_irq_exit(pdev, fsl_edma); |
Vinod Koul | 6f93b93 | 2016-07-02 14:58:30 +0530 | [diff] [blame] | 403 | fsl_edma_cleanup_vchan(&fsl_edma->dma_dev); |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 404 | of_dma_controller_free(np); |
| 405 | dma_async_device_unregister(&fsl_edma->dma_dev); |
Robin Gong | af80272 | 2019-06-25 17:43:19 +0800 | [diff] [blame] | 406 | fsl_disable_clocks(fsl_edma, fsl_edma->drvdata->dmamuxs); |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 407 | |
| 408 | return 0; |
| 409 | } |
| 410 | |
Yuan Yao | 82d149b | 2015-10-30 19:03:58 +0800 | [diff] [blame] | 411 | static 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 | |
| 435 | static 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 Dureghello | 377eaf3 | 2018-08-19 19:27:14 +0200 | [diff] [blame] | 439 | struct edma_regs *regs = &fsl_edma->regs; |
Yuan Yao | 82d149b | 2015-10-30 19:03:58 +0800 | [diff] [blame] | 440 | 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 Dureghello | 377eaf3 | 2018-08-19 19:27:14 +0200 | [diff] [blame] | 445 | edma_writew(fsl_edma, 0x0, ®s->tcd[i].csr); |
Yuan Yao | 82d149b | 2015-10-30 19:03:58 +0800 | [diff] [blame] | 446 | if (fsl_chan->slave_id != 0) |
| 447 | fsl_edma_chan_mux(fsl_chan, fsl_chan->slave_id, true); |
| 448 | } |
| 449 | |
Angelo Dureghello | 377eaf3 | 2018-08-19 19:27:14 +0200 | [diff] [blame] | 450 | edma_writel(fsl_edma, EDMA_CR_ERGA | EDMA_CR_ERCA, regs->cr); |
Yuan Yao | 82d149b | 2015-10-30 19:03:58 +0800 | [diff] [blame] | 451 | |
| 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 | */ |
| 460 | static 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 Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 465 | static struct platform_driver fsl_edma_driver = { |
| 466 | .driver = { |
| 467 | .name = "fsl-edma", |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 468 | .of_match_table = fsl_edma_dt_ids, |
Yuan Yao | 82d149b | 2015-10-30 19:03:58 +0800 | [diff] [blame] | 469 | .pm = &fsl_edma_pm_ops, |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 470 | }, |
| 471 | .probe = fsl_edma_probe, |
| 472 | .remove = fsl_edma_remove, |
| 473 | }; |
| 474 | |
Yuan Yao | 8edc51c | 2014-04-04 12:27:55 +0800 | [diff] [blame] | 475 | static int __init fsl_edma_init(void) |
| 476 | { |
| 477 | return platform_driver_register(&fsl_edma_driver); |
| 478 | } |
| 479 | subsys_initcall(fsl_edma_init); |
| 480 | |
| 481 | static void __exit fsl_edma_exit(void) |
| 482 | { |
| 483 | platform_driver_unregister(&fsl_edma_driver); |
| 484 | } |
| 485 | module_exit(fsl_edma_exit); |
Jingchang Lu | d6be34f | 2014-02-18 10:17:12 +0800 | [diff] [blame] | 486 | |
| 487 | MODULE_ALIAS("platform:fsl-edma"); |
| 488 | MODULE_DESCRIPTION("Freescale eDMA engine driver"); |
| 489 | MODULE_LICENSE("GPL v2"); |