166aac8eaSFrank Li // SPDX-License-Identifier: GPL-2.0+ 266aac8eaSFrank Li // 366aac8eaSFrank Li // Copyright (c) 2013-2014 Freescale Semiconductor, Inc 466aac8eaSFrank Li // Copyright (c) 2017 Sysam, Angelo Dureghello <angelo@sysam.it> 566aac8eaSFrank Li 666aac8eaSFrank Li #include <linux/module.h> 766aac8eaSFrank Li #include <linux/interrupt.h> 866aac8eaSFrank Li #include <linux/dmaengine.h> 966aac8eaSFrank Li #include <linux/platform_device.h> 1066aac8eaSFrank Li #include <linux/platform_data/dma-mcf-edma.h> 1166aac8eaSFrank Li 1266aac8eaSFrank Li #include "fsl-edma-common.h" 1366aac8eaSFrank Li 1466aac8eaSFrank Li #define EDMA_CHANNELS 64 1566aac8eaSFrank Li #define EDMA_MASK_CH(x) ((x) & GENMASK(5, 0)) 1666aac8eaSFrank Li 1766aac8eaSFrank Li static irqreturn_t mcf_edma_tx_handler(int irq, void *dev_id) 1866aac8eaSFrank Li { 1966aac8eaSFrank Li struct fsl_edma_engine *mcf_edma = dev_id; 2066aac8eaSFrank Li struct edma_regs *regs = &mcf_edma->regs; 2166aac8eaSFrank Li unsigned int ch; 2266aac8eaSFrank Li u64 intmap; 2366aac8eaSFrank Li 2466aac8eaSFrank Li intmap = ioread32(regs->inth); 2566aac8eaSFrank Li intmap <<= 32; 2666aac8eaSFrank Li intmap |= ioread32(regs->intl); 2766aac8eaSFrank Li if (!intmap) 2866aac8eaSFrank Li return IRQ_NONE; 2966aac8eaSFrank Li 3066aac8eaSFrank Li for (ch = 0; ch < mcf_edma->n_chans; ch++) { 3166aac8eaSFrank Li if (intmap & BIT(ch)) { 3266aac8eaSFrank Li iowrite8(EDMA_MASK_CH(ch), regs->cint); 33*79434f9bSFrank Li fsl_edma_tx_chan_handler(&mcf_edma->chans[ch]); 3466aac8eaSFrank Li } 3566aac8eaSFrank Li } 3666aac8eaSFrank Li 3766aac8eaSFrank Li return IRQ_HANDLED; 3866aac8eaSFrank Li } 3966aac8eaSFrank Li 4066aac8eaSFrank Li static irqreturn_t mcf_edma_err_handler(int irq, void *dev_id) 4166aac8eaSFrank Li { 4266aac8eaSFrank Li struct fsl_edma_engine *mcf_edma = dev_id; 4366aac8eaSFrank Li struct edma_regs *regs = &mcf_edma->regs; 4466aac8eaSFrank Li unsigned int err, ch; 4566aac8eaSFrank Li 4666aac8eaSFrank Li err = ioread32(regs->errl); 4766aac8eaSFrank Li if (!err) 4866aac8eaSFrank Li return IRQ_NONE; 4966aac8eaSFrank Li 5066aac8eaSFrank Li for (ch = 0; ch < (EDMA_CHANNELS / 2); ch++) { 5166aac8eaSFrank Li if (err & BIT(ch)) { 5266aac8eaSFrank Li fsl_edma_disable_request(&mcf_edma->chans[ch]); 5366aac8eaSFrank Li iowrite8(EDMA_CERR_CERR(ch), regs->cerr); 54*79434f9bSFrank Li fsl_edma_err_chan_handler(&mcf_edma->chans[ch]); 5566aac8eaSFrank Li } 5666aac8eaSFrank Li } 5766aac8eaSFrank Li 5866aac8eaSFrank Li err = ioread32(regs->errh); 5966aac8eaSFrank Li if (!err) 6066aac8eaSFrank Li return IRQ_NONE; 6166aac8eaSFrank Li 6266aac8eaSFrank Li for (ch = (EDMA_CHANNELS / 2); ch < EDMA_CHANNELS; ch++) { 6366aac8eaSFrank Li if (err & (BIT(ch - (EDMA_CHANNELS / 2)))) { 6466aac8eaSFrank Li fsl_edma_disable_request(&mcf_edma->chans[ch]); 6566aac8eaSFrank Li iowrite8(EDMA_CERR_CERR(ch), regs->cerr); 6666aac8eaSFrank Li mcf_edma->chans[ch].status = DMA_ERROR; 6766aac8eaSFrank Li mcf_edma->chans[ch].idle = true; 6866aac8eaSFrank Li } 6966aac8eaSFrank Li } 7066aac8eaSFrank Li 7166aac8eaSFrank Li return IRQ_HANDLED; 7266aac8eaSFrank Li } 7366aac8eaSFrank Li 7466aac8eaSFrank Li static int mcf_edma_irq_init(struct platform_device *pdev, 7566aac8eaSFrank Li struct fsl_edma_engine *mcf_edma) 7666aac8eaSFrank Li { 7766aac8eaSFrank Li int ret = 0, i; 7866aac8eaSFrank Li struct resource *res; 7966aac8eaSFrank Li 8066aac8eaSFrank Li res = platform_get_resource_byname(pdev, 8166aac8eaSFrank Li IORESOURCE_IRQ, "edma-tx-00-15"); 8266aac8eaSFrank Li if (!res) 8366aac8eaSFrank Li return -1; 8466aac8eaSFrank Li 8566aac8eaSFrank Li for (ret = 0, i = res->start; i <= res->end; ++i) 8666aac8eaSFrank Li ret |= request_irq(i, mcf_edma_tx_handler, 0, "eDMA", mcf_edma); 8766aac8eaSFrank Li if (ret) 8866aac8eaSFrank Li return ret; 8966aac8eaSFrank Li 9066aac8eaSFrank Li res = platform_get_resource_byname(pdev, 9166aac8eaSFrank Li IORESOURCE_IRQ, "edma-tx-16-55"); 9266aac8eaSFrank Li if (!res) 9366aac8eaSFrank Li return -1; 9466aac8eaSFrank Li 9566aac8eaSFrank Li for (ret = 0, i = res->start; i <= res->end; ++i) 9666aac8eaSFrank Li ret |= request_irq(i, mcf_edma_tx_handler, 0, "eDMA", mcf_edma); 9766aac8eaSFrank Li if (ret) 9866aac8eaSFrank Li return ret; 9966aac8eaSFrank Li 10066aac8eaSFrank Li ret = platform_get_irq_byname(pdev, "edma-tx-56-63"); 10166aac8eaSFrank Li if (ret != -ENXIO) { 10266aac8eaSFrank Li ret = request_irq(ret, mcf_edma_tx_handler, 10366aac8eaSFrank Li 0, "eDMA", mcf_edma); 10466aac8eaSFrank Li if (ret) 10566aac8eaSFrank Li return ret; 10666aac8eaSFrank Li } 10766aac8eaSFrank Li 10866aac8eaSFrank Li ret = platform_get_irq_byname(pdev, "edma-err"); 10966aac8eaSFrank Li if (ret != -ENXIO) { 11066aac8eaSFrank Li ret = request_irq(ret, mcf_edma_err_handler, 11166aac8eaSFrank Li 0, "eDMA", mcf_edma); 11266aac8eaSFrank Li if (ret) 11366aac8eaSFrank Li return ret; 11466aac8eaSFrank Li } 11566aac8eaSFrank Li 11666aac8eaSFrank Li return 0; 11766aac8eaSFrank Li } 11866aac8eaSFrank Li 11966aac8eaSFrank Li static void mcf_edma_irq_free(struct platform_device *pdev, 12066aac8eaSFrank Li struct fsl_edma_engine *mcf_edma) 12166aac8eaSFrank Li { 12266aac8eaSFrank Li int irq; 12366aac8eaSFrank Li struct resource *res; 12466aac8eaSFrank Li 12566aac8eaSFrank Li res = platform_get_resource_byname(pdev, 12666aac8eaSFrank Li IORESOURCE_IRQ, "edma-tx-00-15"); 12766aac8eaSFrank Li if (res) { 12866aac8eaSFrank Li for (irq = res->start; irq <= res->end; irq++) 12966aac8eaSFrank Li free_irq(irq, mcf_edma); 13066aac8eaSFrank Li } 13166aac8eaSFrank Li 13266aac8eaSFrank Li res = platform_get_resource_byname(pdev, 13366aac8eaSFrank Li IORESOURCE_IRQ, "edma-tx-16-55"); 13466aac8eaSFrank Li if (res) { 13566aac8eaSFrank Li for (irq = res->start; irq <= res->end; irq++) 13666aac8eaSFrank Li free_irq(irq, mcf_edma); 13766aac8eaSFrank Li } 13866aac8eaSFrank Li 13966aac8eaSFrank Li irq = platform_get_irq_byname(pdev, "edma-tx-56-63"); 14066aac8eaSFrank Li if (irq != -ENXIO) 14166aac8eaSFrank Li free_irq(irq, mcf_edma); 14266aac8eaSFrank Li 14366aac8eaSFrank Li irq = platform_get_irq_byname(pdev, "edma-err"); 14466aac8eaSFrank Li if (irq != -ENXIO) 14566aac8eaSFrank Li free_irq(irq, mcf_edma); 14666aac8eaSFrank Li } 14766aac8eaSFrank Li 14866aac8eaSFrank Li static struct fsl_edma_drvdata mcf_data = { 149c26e6114SFrank Li .flags = FSL_EDMA_DRV_EDMA64, 15066aac8eaSFrank Li .setup_irq = mcf_edma_irq_init, 15166aac8eaSFrank Li }; 15266aac8eaSFrank Li 15366aac8eaSFrank Li static int mcf_edma_probe(struct platform_device *pdev) 15466aac8eaSFrank Li { 15566aac8eaSFrank Li struct mcf_edma_platform_data *pdata; 15666aac8eaSFrank Li struct fsl_edma_engine *mcf_edma; 15766aac8eaSFrank Li struct edma_regs *regs; 15866aac8eaSFrank Li int ret, i, chans; 15966aac8eaSFrank Li 16066aac8eaSFrank Li pdata = dev_get_platdata(&pdev->dev); 16166aac8eaSFrank Li if (!pdata) { 16266aac8eaSFrank Li dev_err(&pdev->dev, "no platform data supplied\n"); 16366aac8eaSFrank Li return -EINVAL; 16466aac8eaSFrank Li } 16566aac8eaSFrank Li 16666aac8eaSFrank Li chans = pdata->dma_channels; 16766aac8eaSFrank Li mcf_edma = devm_kzalloc(&pdev->dev, struct_size(mcf_edma, chans, chans), 16866aac8eaSFrank Li GFP_KERNEL); 16966aac8eaSFrank Li if (!mcf_edma) 17066aac8eaSFrank Li return -ENOMEM; 17166aac8eaSFrank Li 17266aac8eaSFrank Li mcf_edma->n_chans = chans; 17366aac8eaSFrank Li 17466aac8eaSFrank Li /* Set up drvdata for ColdFire edma */ 17566aac8eaSFrank Li mcf_edma->drvdata = &mcf_data; 17666aac8eaSFrank Li mcf_edma->big_endian = 1; 17766aac8eaSFrank Li 17866aac8eaSFrank Li if (!mcf_edma->n_chans) { 17966aac8eaSFrank Li dev_info(&pdev->dev, "setting default channel number to 64"); 18066aac8eaSFrank Li mcf_edma->n_chans = 64; 18166aac8eaSFrank Li } 18266aac8eaSFrank Li 18366aac8eaSFrank Li mutex_init(&mcf_edma->fsl_edma_mutex); 18466aac8eaSFrank Li 18566aac8eaSFrank Li mcf_edma->membase = devm_platform_ioremap_resource(pdev, 0); 18666aac8eaSFrank Li if (IS_ERR(mcf_edma->membase)) 18766aac8eaSFrank Li return PTR_ERR(mcf_edma->membase); 18866aac8eaSFrank Li 18966aac8eaSFrank Li fsl_edma_setup_regs(mcf_edma); 19066aac8eaSFrank Li regs = &mcf_edma->regs; 19166aac8eaSFrank Li 19266aac8eaSFrank Li INIT_LIST_HEAD(&mcf_edma->dma_dev.channels); 19366aac8eaSFrank Li for (i = 0; i < mcf_edma->n_chans; i++) { 19466aac8eaSFrank Li struct fsl_edma_chan *mcf_chan = &mcf_edma->chans[i]; 19566aac8eaSFrank Li 19666aac8eaSFrank Li mcf_chan->edma = mcf_edma; 19766aac8eaSFrank Li mcf_chan->slave_id = i; 19866aac8eaSFrank Li mcf_chan->idle = true; 19966aac8eaSFrank Li mcf_chan->dma_dir = DMA_NONE; 20066aac8eaSFrank Li mcf_chan->vchan.desc_free = fsl_edma_free_desc; 20166aac8eaSFrank Li vchan_init(&mcf_chan->vchan, &mcf_edma->dma_dev); 20266aac8eaSFrank Li iowrite32(0x0, ®s->tcd[i].csr); 20366aac8eaSFrank Li } 20466aac8eaSFrank Li 20566aac8eaSFrank Li iowrite32(~0, regs->inth); 20666aac8eaSFrank Li iowrite32(~0, regs->intl); 20766aac8eaSFrank Li 20866aac8eaSFrank Li ret = mcf_edma->drvdata->setup_irq(pdev, mcf_edma); 20966aac8eaSFrank Li if (ret) 21066aac8eaSFrank Li return ret; 21166aac8eaSFrank Li 21266aac8eaSFrank Li dma_cap_set(DMA_PRIVATE, mcf_edma->dma_dev.cap_mask); 21366aac8eaSFrank Li dma_cap_set(DMA_SLAVE, mcf_edma->dma_dev.cap_mask); 21466aac8eaSFrank Li dma_cap_set(DMA_CYCLIC, mcf_edma->dma_dev.cap_mask); 21566aac8eaSFrank Li 21666aac8eaSFrank Li mcf_edma->dma_dev.dev = &pdev->dev; 21766aac8eaSFrank Li mcf_edma->dma_dev.device_alloc_chan_resources = 21866aac8eaSFrank Li fsl_edma_alloc_chan_resources; 21966aac8eaSFrank Li mcf_edma->dma_dev.device_free_chan_resources = 22066aac8eaSFrank Li fsl_edma_free_chan_resources; 22166aac8eaSFrank Li mcf_edma->dma_dev.device_config = fsl_edma_slave_config; 22266aac8eaSFrank Li mcf_edma->dma_dev.device_prep_dma_cyclic = 22366aac8eaSFrank Li fsl_edma_prep_dma_cyclic; 22466aac8eaSFrank Li mcf_edma->dma_dev.device_prep_slave_sg = fsl_edma_prep_slave_sg; 22566aac8eaSFrank Li mcf_edma->dma_dev.device_tx_status = fsl_edma_tx_status; 22666aac8eaSFrank Li mcf_edma->dma_dev.device_pause = fsl_edma_pause; 22766aac8eaSFrank Li mcf_edma->dma_dev.device_resume = fsl_edma_resume; 22866aac8eaSFrank Li mcf_edma->dma_dev.device_terminate_all = fsl_edma_terminate_all; 22966aac8eaSFrank Li mcf_edma->dma_dev.device_issue_pending = fsl_edma_issue_pending; 23066aac8eaSFrank Li 23166aac8eaSFrank Li mcf_edma->dma_dev.src_addr_widths = FSL_EDMA_BUSWIDTHS; 23266aac8eaSFrank Li mcf_edma->dma_dev.dst_addr_widths = FSL_EDMA_BUSWIDTHS; 23366aac8eaSFrank Li mcf_edma->dma_dev.directions = 23466aac8eaSFrank Li BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV); 23566aac8eaSFrank Li 23666aac8eaSFrank Li mcf_edma->dma_dev.filter.fn = mcf_edma_filter_fn; 23766aac8eaSFrank Li mcf_edma->dma_dev.filter.map = pdata->slave_map; 23866aac8eaSFrank Li mcf_edma->dma_dev.filter.mapcnt = pdata->slavecnt; 23966aac8eaSFrank Li 24066aac8eaSFrank Li platform_set_drvdata(pdev, mcf_edma); 24166aac8eaSFrank Li 24266aac8eaSFrank Li ret = dma_async_device_register(&mcf_edma->dma_dev); 24366aac8eaSFrank Li if (ret) { 24466aac8eaSFrank Li dev_err(&pdev->dev, 24566aac8eaSFrank Li "Can't register Freescale eDMA engine. (%d)\n", ret); 24666aac8eaSFrank Li return ret; 24766aac8eaSFrank Li } 24866aac8eaSFrank Li 24966aac8eaSFrank Li /* Enable round robin arbitration */ 25066aac8eaSFrank Li iowrite32(EDMA_CR_ERGA | EDMA_CR_ERCA, regs->cr); 25166aac8eaSFrank Li 25266aac8eaSFrank Li return 0; 25366aac8eaSFrank Li } 25466aac8eaSFrank Li 25566aac8eaSFrank Li static int mcf_edma_remove(struct platform_device *pdev) 25666aac8eaSFrank Li { 25766aac8eaSFrank Li struct fsl_edma_engine *mcf_edma = platform_get_drvdata(pdev); 25866aac8eaSFrank Li 25966aac8eaSFrank Li mcf_edma_irq_free(pdev, mcf_edma); 26066aac8eaSFrank Li fsl_edma_cleanup_vchan(&mcf_edma->dma_dev); 26166aac8eaSFrank Li dma_async_device_unregister(&mcf_edma->dma_dev); 26266aac8eaSFrank Li 26366aac8eaSFrank Li return 0; 26466aac8eaSFrank Li } 26566aac8eaSFrank Li 26666aac8eaSFrank Li static struct platform_driver mcf_edma_driver = { 26766aac8eaSFrank Li .driver = { 26866aac8eaSFrank Li .name = "mcf-edma", 26966aac8eaSFrank Li }, 27066aac8eaSFrank Li .probe = mcf_edma_probe, 27166aac8eaSFrank Li .remove = mcf_edma_remove, 27266aac8eaSFrank Li }; 27366aac8eaSFrank Li 27466aac8eaSFrank Li bool mcf_edma_filter_fn(struct dma_chan *chan, void *param) 27566aac8eaSFrank Li { 27666aac8eaSFrank Li if (chan->device->dev->driver == &mcf_edma_driver.driver) { 27766aac8eaSFrank Li struct fsl_edma_chan *mcf_chan = to_fsl_edma_chan(chan); 27866aac8eaSFrank Li 27966aac8eaSFrank Li return (mcf_chan->slave_id == (uintptr_t)param); 28066aac8eaSFrank Li } 28166aac8eaSFrank Li 28266aac8eaSFrank Li return false; 28366aac8eaSFrank Li } 28466aac8eaSFrank Li EXPORT_SYMBOL(mcf_edma_filter_fn); 28566aac8eaSFrank Li 28666aac8eaSFrank Li static int __init mcf_edma_init(void) 28766aac8eaSFrank Li { 28866aac8eaSFrank Li return platform_driver_register(&mcf_edma_driver); 28966aac8eaSFrank Li } 29066aac8eaSFrank Li subsys_initcall(mcf_edma_init); 29166aac8eaSFrank Li 29266aac8eaSFrank Li static void __exit mcf_edma_exit(void) 29366aac8eaSFrank Li { 29466aac8eaSFrank Li platform_driver_unregister(&mcf_edma_driver); 29566aac8eaSFrank Li } 29666aac8eaSFrank Li module_exit(mcf_edma_exit); 29766aac8eaSFrank Li 29866aac8eaSFrank Li MODULE_ALIAS("platform:mcf-edma"); 29966aac8eaSFrank Li MODULE_DESCRIPTION("Freescale eDMA engine driver, ColdFire family"); 30066aac8eaSFrank Li MODULE_LICENSE("GPL v2"); 301