Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * DMA Router driver for LPC18xx/43xx DMA MUX
0004  *
0005  * Copyright (C) 2015 Joachim Eastwood <manabian@gmail.com>
0006  *
0007  * Based on TI DMA Crossbar driver by:
0008  *   Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com
0009  *   Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
0010  */
0011 
0012 #include <linux/err.h>
0013 #include <linux/init.h>
0014 #include <linux/mfd/syscon.h>
0015 #include <linux/of_device.h>
0016 #include <linux/of_dma.h>
0017 #include <linux/regmap.h>
0018 #include <linux/spinlock.h>
0019 
0020 /* CREG register offset and macros for mux manipulation */
0021 #define LPC18XX_CREG_DMAMUX     0x11c
0022 #define LPC18XX_DMAMUX_VAL(v, n)    ((v) << (n * 2))
0023 #define LPC18XX_DMAMUX_MASK(n)      (0x3 << (n * 2))
0024 #define LPC18XX_DMAMUX_MAX_VAL      0x3
0025 
0026 struct lpc18xx_dmamux {
0027     u32 value;
0028     bool busy;
0029 };
0030 
0031 struct lpc18xx_dmamux_data {
0032     struct dma_router dmarouter;
0033     struct lpc18xx_dmamux *muxes;
0034     u32 dma_master_requests;
0035     u32 dma_mux_requests;
0036     struct regmap *reg;
0037     spinlock_t lock;
0038 };
0039 
0040 static void lpc18xx_dmamux_free(struct device *dev, void *route_data)
0041 {
0042     struct lpc18xx_dmamux_data *dmamux = dev_get_drvdata(dev);
0043     struct lpc18xx_dmamux *mux = route_data;
0044     unsigned long flags;
0045 
0046     spin_lock_irqsave(&dmamux->lock, flags);
0047     mux->busy = false;
0048     spin_unlock_irqrestore(&dmamux->lock, flags);
0049 }
0050 
0051 static void *lpc18xx_dmamux_reserve(struct of_phandle_args *dma_spec,
0052                     struct of_dma *ofdma)
0053 {
0054     struct platform_device *pdev = of_find_device_by_node(ofdma->of_node);
0055     struct lpc18xx_dmamux_data *dmamux = platform_get_drvdata(pdev);
0056     unsigned long flags;
0057     unsigned mux;
0058 
0059     if (dma_spec->args_count != 3) {
0060         dev_err(&pdev->dev, "invalid number of dma mux args\n");
0061         return ERR_PTR(-EINVAL);
0062     }
0063 
0064     mux = dma_spec->args[0];
0065     if (mux >= dmamux->dma_master_requests) {
0066         dev_err(&pdev->dev, "invalid mux number: %d\n",
0067             dma_spec->args[0]);
0068         return ERR_PTR(-EINVAL);
0069     }
0070 
0071     if (dma_spec->args[1] > LPC18XX_DMAMUX_MAX_VAL) {
0072         dev_err(&pdev->dev, "invalid dma mux value: %d\n",
0073             dma_spec->args[1]);
0074         return ERR_PTR(-EINVAL);
0075     }
0076 
0077     /* The of_node_put() will be done in the core for the node */
0078     dma_spec->np = of_parse_phandle(ofdma->of_node, "dma-masters", 0);
0079     if (!dma_spec->np) {
0080         dev_err(&pdev->dev, "can't get dma master\n");
0081         return ERR_PTR(-EINVAL);
0082     }
0083 
0084     spin_lock_irqsave(&dmamux->lock, flags);
0085     if (dmamux->muxes[mux].busy) {
0086         spin_unlock_irqrestore(&dmamux->lock, flags);
0087         dev_err(&pdev->dev, "dma request %u busy with %u.%u\n",
0088             mux, mux, dmamux->muxes[mux].value);
0089         of_node_put(dma_spec->np);
0090         return ERR_PTR(-EBUSY);
0091     }
0092 
0093     dmamux->muxes[mux].busy = true;
0094     dmamux->muxes[mux].value = dma_spec->args[1];
0095 
0096     regmap_update_bits(dmamux->reg, LPC18XX_CREG_DMAMUX,
0097                LPC18XX_DMAMUX_MASK(mux),
0098                LPC18XX_DMAMUX_VAL(dmamux->muxes[mux].value, mux));
0099     spin_unlock_irqrestore(&dmamux->lock, flags);
0100 
0101     dma_spec->args[1] = dma_spec->args[2];
0102     dma_spec->args_count = 2;
0103 
0104     dev_dbg(&pdev->dev, "mapping dmamux %u.%u to dma request %u\n", mux,
0105         dmamux->muxes[mux].value, mux);
0106 
0107     return &dmamux->muxes[mux];
0108 }
0109 
0110 static int lpc18xx_dmamux_probe(struct platform_device *pdev)
0111 {
0112     struct device_node *dma_np, *np = pdev->dev.of_node;
0113     struct lpc18xx_dmamux_data *dmamux;
0114     int ret;
0115 
0116     dmamux = devm_kzalloc(&pdev->dev, sizeof(*dmamux), GFP_KERNEL);
0117     if (!dmamux)
0118         return -ENOMEM;
0119 
0120     dmamux->reg = syscon_regmap_lookup_by_compatible("nxp,lpc1850-creg");
0121     if (IS_ERR(dmamux->reg)) {
0122         dev_err(&pdev->dev, "syscon lookup failed\n");
0123         return PTR_ERR(dmamux->reg);
0124     }
0125 
0126     ret = of_property_read_u32(np, "dma-requests",
0127                    &dmamux->dma_mux_requests);
0128     if (ret) {
0129         dev_err(&pdev->dev, "missing dma-requests property\n");
0130         return ret;
0131     }
0132 
0133     dma_np = of_parse_phandle(np, "dma-masters", 0);
0134     if (!dma_np) {
0135         dev_err(&pdev->dev, "can't get dma master\n");
0136         return -ENODEV;
0137     }
0138 
0139     ret = of_property_read_u32(dma_np, "dma-requests",
0140                    &dmamux->dma_master_requests);
0141     of_node_put(dma_np);
0142     if (ret) {
0143         dev_err(&pdev->dev, "missing master dma-requests property\n");
0144         return ret;
0145     }
0146 
0147     dmamux->muxes = devm_kcalloc(&pdev->dev, dmamux->dma_master_requests,
0148                      sizeof(struct lpc18xx_dmamux),
0149                      GFP_KERNEL);
0150     if (!dmamux->muxes)
0151         return -ENOMEM;
0152 
0153     spin_lock_init(&dmamux->lock);
0154     platform_set_drvdata(pdev, dmamux);
0155     dmamux->dmarouter.dev = &pdev->dev;
0156     dmamux->dmarouter.route_free = lpc18xx_dmamux_free;
0157 
0158     return of_dma_router_register(np, lpc18xx_dmamux_reserve,
0159                       &dmamux->dmarouter);
0160 }
0161 
0162 static const struct of_device_id lpc18xx_dmamux_match[] = {
0163     { .compatible = "nxp,lpc1850-dmamux" },
0164     {},
0165 };
0166 
0167 static struct platform_driver lpc18xx_dmamux_driver = {
0168     .probe  = lpc18xx_dmamux_probe,
0169     .driver = {
0170         .name = "lpc18xx-dmamux",
0171         .of_match_table = lpc18xx_dmamux_match,
0172     },
0173 };
0174 
0175 static int __init lpc18xx_dmamux_init(void)
0176 {
0177     return platform_driver_register(&lpc18xx_dmamux_driver);
0178 }
0179 arch_initcall(lpc18xx_dmamux_init);