Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v5.7-rc6 505 lines 14 kB view raw
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 <linux/module.h> 13#include <linux/interrupt.h> 14#include <linux/clk.h> 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 21#include "fsl-edma-common.h" 22 23static void fsl_edma_synchronize(struct dma_chan *chan) 24{ 25 struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan); 26 27 vchan_synchronize(&fsl_chan->vchan); 28} 29 30static irqreturn_t fsl_edma_tx_handler(int irq, void *dev_id) 31{ 32 struct fsl_edma_engine *fsl_edma = dev_id; 33 unsigned int intr, ch; 34 struct edma_regs *regs = &fsl_edma->regs; 35 struct fsl_edma_chan *fsl_chan; 36 37 intr = edma_readl(fsl_edma, regs->intl); 38 if (!intr) 39 return IRQ_NONE; 40 41 for (ch = 0; ch < fsl_edma->n_chans; ch++) { 42 if (intr & (0x1 << ch)) { 43 edma_writeb(fsl_edma, EDMA_CINT_CINT(ch), regs->cint); 44 45 fsl_chan = &fsl_edma->chans[ch]; 46 47 spin_lock(&fsl_chan->vchan.lock); 48 if (!fsl_chan->edesc->iscyclic) { 49 list_del(&fsl_chan->edesc->vdesc.node); 50 vchan_cookie_complete(&fsl_chan->edesc->vdesc); 51 fsl_chan->edesc = NULL; 52 fsl_chan->status = DMA_COMPLETE; 53 fsl_chan->idle = true; 54 } else { 55 vchan_cyclic_callback(&fsl_chan->edesc->vdesc); 56 } 57 58 if (!fsl_chan->edesc) 59 fsl_edma_xfer_desc(fsl_chan); 60 61 spin_unlock(&fsl_chan->vchan.lock); 62 } 63 } 64 return IRQ_HANDLED; 65} 66 67static irqreturn_t fsl_edma_err_handler(int irq, void *dev_id) 68{ 69 struct fsl_edma_engine *fsl_edma = dev_id; 70 unsigned int err, ch; 71 struct edma_regs *regs = &fsl_edma->regs; 72 73 err = edma_readl(fsl_edma, regs->errl); 74 if (!err) 75 return IRQ_NONE; 76 77 for (ch = 0; ch < fsl_edma->n_chans; ch++) { 78 if (err & (0x1 << ch)) { 79 fsl_edma_disable_request(&fsl_edma->chans[ch]); 80 edma_writeb(fsl_edma, EDMA_CERR_CERR(ch), regs->cerr); 81 fsl_edma->chans[ch].status = DMA_ERROR; 82 fsl_edma->chans[ch].idle = true; 83 } 84 } 85 return IRQ_HANDLED; 86} 87 88static irqreturn_t fsl_edma_irq_handler(int irq, void *dev_id) 89{ 90 if (fsl_edma_tx_handler(irq, dev_id) == IRQ_HANDLED) 91 return IRQ_HANDLED; 92 93 return fsl_edma_err_handler(irq, dev_id); 94} 95 96static struct dma_chan *fsl_edma_xlate(struct of_phandle_args *dma_spec, 97 struct of_dma *ofdma) 98{ 99 struct fsl_edma_engine *fsl_edma = ofdma->of_dma_data; 100 struct dma_chan *chan, *_chan; 101 struct fsl_edma_chan *fsl_chan; 102 u32 dmamux_nr = fsl_edma->drvdata->dmamuxs; 103 unsigned long chans_per_mux = fsl_edma->n_chans / dmamux_nr; 104 105 if (dma_spec->args_count != 2) 106 return NULL; 107 108 mutex_lock(&fsl_edma->fsl_edma_mutex); 109 list_for_each_entry_safe(chan, _chan, &fsl_edma->dma_dev.channels, device_node) { 110 if (chan->client_count) 111 continue; 112 if ((chan->chan_id / chans_per_mux) == dma_spec->args[0]) { 113 chan = dma_get_slave_channel(chan); 114 if (chan) { 115 chan->device->privatecnt++; 116 fsl_chan = to_fsl_edma_chan(chan); 117 fsl_chan->slave_id = dma_spec->args[1]; 118 fsl_edma_chan_mux(fsl_chan, fsl_chan->slave_id, 119 true); 120 mutex_unlock(&fsl_edma->fsl_edma_mutex); 121 return chan; 122 } 123 } 124 } 125 mutex_unlock(&fsl_edma->fsl_edma_mutex); 126 return NULL; 127} 128 129static int 130fsl_edma_irq_init(struct platform_device *pdev, struct fsl_edma_engine *fsl_edma) 131{ 132 int ret; 133 134 fsl_edma->txirq = platform_get_irq_byname(pdev, "edma-tx"); 135 if (fsl_edma->txirq < 0) 136 return fsl_edma->txirq; 137 138 fsl_edma->errirq = platform_get_irq_byname(pdev, "edma-err"); 139 if (fsl_edma->errirq < 0) 140 return fsl_edma->errirq; 141 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); 145 if (ret) { 146 dev_err(&pdev->dev, "Can't register eDMA IRQ.\n"); 147 return ret; 148 } 149 } else { 150 ret = devm_request_irq(&pdev->dev, fsl_edma->txirq, 151 fsl_edma_tx_handler, 0, "eDMA tx", fsl_edma); 152 if (ret) { 153 dev_err(&pdev->dev, "Can't register eDMA tx IRQ.\n"); 154 return ret; 155 } 156 157 ret = devm_request_irq(&pdev->dev, fsl_edma->errirq, 158 fsl_edma_err_handler, 0, "eDMA err", fsl_edma); 159 if (ret) { 160 dev_err(&pdev->dev, "Can't register eDMA err IRQ.\n"); 161 return ret; 162 } 163 } 164 165 return 0; 166} 167 168static int 169fsl_edma2_irq_init(struct platform_device *pdev, 170 struct fsl_edma_engine *fsl_edma) 171{ 172 int i, ret, irq; 173 int count; 174 175 count = platform_irq_count(pdev); 176 dev_dbg(&pdev->dev, "%s Found %d interrupts\r\n", __func__, count); 177 if (count <= 2) { 178 dev_err(&pdev->dev, "Interrupts in DTS not correct.\n"); 179 return -EINVAL; 180 } 181 /* 182 * 16 channel independent interrupts + 1 error interrupt on i.mx7ulp. 183 * 2 channel share one interrupt, for example, ch0/ch16, ch1/ch17... 184 * For now, just simply request irq without IRQF_SHARED flag, since 16 185 * channels are enough on i.mx7ulp whose M4 domain own some peripherals. 186 */ 187 for (i = 0; i < count; i++) { 188 irq = platform_get_irq(pdev, i); 189 if (irq < 0) 190 return -ENXIO; 191 192 sprintf(fsl_edma->chans[i].chan_name, "eDMA2-CH%02d", i); 193 194 /* The last IRQ is for eDMA err */ 195 if (i == count - 1) 196 ret = devm_request_irq(&pdev->dev, irq, 197 fsl_edma_err_handler, 198 0, "eDMA2-ERR", fsl_edma); 199 else 200 ret = devm_request_irq(&pdev->dev, irq, 201 fsl_edma_tx_handler, 0, 202 fsl_edma->chans[i].chan_name, 203 fsl_edma); 204 if (ret) 205 return ret; 206 } 207 208 return 0; 209} 210 211static void fsl_edma_irq_exit( 212 struct platform_device *pdev, struct fsl_edma_engine *fsl_edma) 213{ 214 if (fsl_edma->txirq == fsl_edma->errirq) { 215 devm_free_irq(&pdev->dev, fsl_edma->txirq, fsl_edma); 216 } else { 217 devm_free_irq(&pdev->dev, fsl_edma->txirq, fsl_edma); 218 devm_free_irq(&pdev->dev, fsl_edma->errirq, fsl_edma); 219 } 220} 221 222static void fsl_disable_clocks(struct fsl_edma_engine *fsl_edma, int nr_clocks) 223{ 224 int i; 225 226 for (i = 0; i < nr_clocks; i++) 227 clk_disable_unprepare(fsl_edma->muxclk[i]); 228} 229 230static struct fsl_edma_drvdata vf610_data = { 231 .version = v1, 232 .dmamuxs = DMAMUX_NR, 233 .setup_irq = fsl_edma_irq_init, 234}; 235 236static struct fsl_edma_drvdata ls1028a_data = { 237 .version = v1, 238 .dmamuxs = DMAMUX_NR, 239 .mux_swap = true, 240 .setup_irq = fsl_edma_irq_init, 241}; 242 243static struct fsl_edma_drvdata imx7ulp_data = { 244 .version = v3, 245 .dmamuxs = 1, 246 .has_dmaclk = true, 247 .setup_irq = fsl_edma2_irq_init, 248}; 249 250static const struct of_device_id fsl_edma_dt_ids[] = { 251 { .compatible = "fsl,vf610-edma", .data = &vf610_data}, 252 { .compatible = "fsl,ls1028a-edma", .data = &ls1028a_data}, 253 { .compatible = "fsl,imx7ulp-edma", .data = &imx7ulp_data}, 254 { /* sentinel */ } 255}; 256MODULE_DEVICE_TABLE(of, fsl_edma_dt_ids); 257 258static int fsl_edma_probe(struct platform_device *pdev) 259{ 260 const struct of_device_id *of_id = 261 of_match_device(fsl_edma_dt_ids, &pdev->dev); 262 struct device_node *np = pdev->dev.of_node; 263 struct fsl_edma_engine *fsl_edma; 264 const struct fsl_edma_drvdata *drvdata = NULL; 265 struct fsl_edma_chan *fsl_chan; 266 struct edma_regs *regs; 267 struct resource *res; 268 int len, chans; 269 int ret, i; 270 271 if (of_id) 272 drvdata = of_id->data; 273 if (!drvdata) { 274 dev_err(&pdev->dev, "unable to find driver data\n"); 275 return -EINVAL; 276 } 277 278 ret = of_property_read_u32(np, "dma-channels", &chans); 279 if (ret) { 280 dev_err(&pdev->dev, "Can't get dma-channels.\n"); 281 return ret; 282 } 283 284 len = sizeof(*fsl_edma) + sizeof(*fsl_chan) * chans; 285 fsl_edma = devm_kzalloc(&pdev->dev, len, GFP_KERNEL); 286 if (!fsl_edma) 287 return -ENOMEM; 288 289 fsl_edma->drvdata = drvdata; 290 fsl_edma->n_chans = chans; 291 mutex_init(&fsl_edma->fsl_edma_mutex); 292 293 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 294 fsl_edma->membase = devm_ioremap_resource(&pdev->dev, res); 295 if (IS_ERR(fsl_edma->membase)) 296 return PTR_ERR(fsl_edma->membase); 297 298 fsl_edma_setup_regs(fsl_edma); 299 regs = &fsl_edma->regs; 300 301 if (drvdata->has_dmaclk) { 302 fsl_edma->dmaclk = devm_clk_get(&pdev->dev, "dma"); 303 if (IS_ERR(fsl_edma->dmaclk)) { 304 dev_err(&pdev->dev, "Missing DMA block clock.\n"); 305 return PTR_ERR(fsl_edma->dmaclk); 306 } 307 308 ret = clk_prepare_enable(fsl_edma->dmaclk); 309 if (ret) { 310 dev_err(&pdev->dev, "DMA clk block failed.\n"); 311 return ret; 312 } 313 } 314 315 for (i = 0; i < fsl_edma->drvdata->dmamuxs; i++) { 316 char clkname[32]; 317 318 res = platform_get_resource(pdev, IORESOURCE_MEM, 1 + i); 319 fsl_edma->muxbase[i] = devm_ioremap_resource(&pdev->dev, res); 320 if (IS_ERR(fsl_edma->muxbase[i])) { 321 /* on error: disable all previously enabled clks */ 322 fsl_disable_clocks(fsl_edma, i); 323 return PTR_ERR(fsl_edma->muxbase[i]); 324 } 325 326 sprintf(clkname, "dmamux%d", i); 327 fsl_edma->muxclk[i] = devm_clk_get(&pdev->dev, clkname); 328 if (IS_ERR(fsl_edma->muxclk[i])) { 329 dev_err(&pdev->dev, "Missing DMAMUX block clock.\n"); 330 /* on error: disable all previously enabled clks */ 331 fsl_disable_clocks(fsl_edma, i); 332 return PTR_ERR(fsl_edma->muxclk[i]); 333 } 334 335 ret = clk_prepare_enable(fsl_edma->muxclk[i]); 336 if (ret) 337 /* on error: disable all previously enabled clks */ 338 fsl_disable_clocks(fsl_edma, i); 339 340 } 341 342 fsl_edma->big_endian = of_property_read_bool(np, "big-endian"); 343 344 INIT_LIST_HEAD(&fsl_edma->dma_dev.channels); 345 for (i = 0; i < fsl_edma->n_chans; i++) { 346 struct fsl_edma_chan *fsl_chan = &fsl_edma->chans[i]; 347 348 fsl_chan->edma = fsl_edma; 349 fsl_chan->pm_state = RUNNING; 350 fsl_chan->slave_id = 0; 351 fsl_chan->idle = true; 352 fsl_chan->dma_dir = DMA_NONE; 353 fsl_chan->vchan.desc_free = fsl_edma_free_desc; 354 vchan_init(&fsl_chan->vchan, &fsl_edma->dma_dev); 355 356 edma_writew(fsl_edma, 0x0, &regs->tcd[i].csr); 357 fsl_edma_chan_mux(fsl_chan, 0, false); 358 } 359 360 edma_writel(fsl_edma, ~0, regs->intl); 361 ret = fsl_edma->drvdata->setup_irq(pdev, fsl_edma); 362 if (ret) 363 return ret; 364 365 dma_cap_set(DMA_PRIVATE, fsl_edma->dma_dev.cap_mask); 366 dma_cap_set(DMA_SLAVE, fsl_edma->dma_dev.cap_mask); 367 dma_cap_set(DMA_CYCLIC, fsl_edma->dma_dev.cap_mask); 368 369 fsl_edma->dma_dev.dev = &pdev->dev; 370 fsl_edma->dma_dev.device_alloc_chan_resources 371 = fsl_edma_alloc_chan_resources; 372 fsl_edma->dma_dev.device_free_chan_resources 373 = fsl_edma_free_chan_resources; 374 fsl_edma->dma_dev.device_tx_status = fsl_edma_tx_status; 375 fsl_edma->dma_dev.device_prep_slave_sg = fsl_edma_prep_slave_sg; 376 fsl_edma->dma_dev.device_prep_dma_cyclic = fsl_edma_prep_dma_cyclic; 377 fsl_edma->dma_dev.device_config = fsl_edma_slave_config; 378 fsl_edma->dma_dev.device_pause = fsl_edma_pause; 379 fsl_edma->dma_dev.device_resume = fsl_edma_resume; 380 fsl_edma->dma_dev.device_terminate_all = fsl_edma_terminate_all; 381 fsl_edma->dma_dev.device_synchronize = fsl_edma_synchronize; 382 fsl_edma->dma_dev.device_issue_pending = fsl_edma_issue_pending; 383 384 fsl_edma->dma_dev.src_addr_widths = FSL_EDMA_BUSWIDTHS; 385 fsl_edma->dma_dev.dst_addr_widths = FSL_EDMA_BUSWIDTHS; 386 fsl_edma->dma_dev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV); 387 388 platform_set_drvdata(pdev, fsl_edma); 389 390 ret = dma_async_device_register(&fsl_edma->dma_dev); 391 if (ret) { 392 dev_err(&pdev->dev, 393 "Can't register Freescale eDMA engine. (%d)\n", ret); 394 fsl_disable_clocks(fsl_edma, fsl_edma->drvdata->dmamuxs); 395 return ret; 396 } 397 398 ret = of_dma_controller_register(np, fsl_edma_xlate, fsl_edma); 399 if (ret) { 400 dev_err(&pdev->dev, 401 "Can't register Freescale eDMA of_dma. (%d)\n", ret); 402 dma_async_device_unregister(&fsl_edma->dma_dev); 403 fsl_disable_clocks(fsl_edma, fsl_edma->drvdata->dmamuxs); 404 return ret; 405 } 406 407 /* enable round robin arbitration */ 408 edma_writel(fsl_edma, EDMA_CR_ERGA | EDMA_CR_ERCA, regs->cr); 409 410 return 0; 411} 412 413static int fsl_edma_remove(struct platform_device *pdev) 414{ 415 struct device_node *np = pdev->dev.of_node; 416 struct fsl_edma_engine *fsl_edma = platform_get_drvdata(pdev); 417 418 fsl_edma_irq_exit(pdev, fsl_edma); 419 fsl_edma_cleanup_vchan(&fsl_edma->dma_dev); 420 of_dma_controller_free(np); 421 dma_async_device_unregister(&fsl_edma->dma_dev); 422 fsl_disable_clocks(fsl_edma, fsl_edma->drvdata->dmamuxs); 423 424 return 0; 425} 426 427static int fsl_edma_suspend_late(struct device *dev) 428{ 429 struct fsl_edma_engine *fsl_edma = dev_get_drvdata(dev); 430 struct fsl_edma_chan *fsl_chan; 431 unsigned long flags; 432 int i; 433 434 for (i = 0; i < fsl_edma->n_chans; i++) { 435 fsl_chan = &fsl_edma->chans[i]; 436 spin_lock_irqsave(&fsl_chan->vchan.lock, flags); 437 /* Make sure chan is idle or will force disable. */ 438 if (unlikely(!fsl_chan->idle)) { 439 dev_warn(dev, "WARN: There is non-idle channel."); 440 fsl_edma_disable_request(fsl_chan); 441 fsl_edma_chan_mux(fsl_chan, 0, false); 442 } 443 444 fsl_chan->pm_state = SUSPENDED; 445 spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags); 446 } 447 448 return 0; 449} 450 451static int fsl_edma_resume_early(struct device *dev) 452{ 453 struct fsl_edma_engine *fsl_edma = dev_get_drvdata(dev); 454 struct fsl_edma_chan *fsl_chan; 455 struct edma_regs *regs = &fsl_edma->regs; 456 int i; 457 458 for (i = 0; i < fsl_edma->n_chans; i++) { 459 fsl_chan = &fsl_edma->chans[i]; 460 fsl_chan->pm_state = RUNNING; 461 edma_writew(fsl_edma, 0x0, &regs->tcd[i].csr); 462 if (fsl_chan->slave_id != 0) 463 fsl_edma_chan_mux(fsl_chan, fsl_chan->slave_id, true); 464 } 465 466 edma_writel(fsl_edma, EDMA_CR_ERGA | EDMA_CR_ERCA, regs->cr); 467 468 return 0; 469} 470 471/* 472 * eDMA provides the service to others, so it should be suspend late 473 * and resume early. When eDMA suspend, all of the clients should stop 474 * the DMA data transmission and let the channel idle. 475 */ 476static const struct dev_pm_ops fsl_edma_pm_ops = { 477 .suspend_late = fsl_edma_suspend_late, 478 .resume_early = fsl_edma_resume_early, 479}; 480 481static struct platform_driver fsl_edma_driver = { 482 .driver = { 483 .name = "fsl-edma", 484 .of_match_table = fsl_edma_dt_ids, 485 .pm = &fsl_edma_pm_ops, 486 }, 487 .probe = fsl_edma_probe, 488 .remove = fsl_edma_remove, 489}; 490 491static int __init fsl_edma_init(void) 492{ 493 return platform_driver_register(&fsl_edma_driver); 494} 495subsys_initcall(fsl_edma_init); 496 497static void __exit fsl_edma_exit(void) 498{ 499 platform_driver_unregister(&fsl_edma_driver); 500} 501module_exit(fsl_edma_exit); 502 503MODULE_ALIAS("platform:fsl-edma"); 504MODULE_DESCRIPTION("Freescale eDMA engine driver"); 505MODULE_LICENSE("GPL v2");