Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Remote processor machine-specific module for DA8XX
0004  *
0005  * Copyright (C) 2013 Texas Instruments, Inc.
0006  */
0007 
0008 #include <linux/bitops.h>
0009 #include <linux/clk.h>
0010 #include <linux/reset.h>
0011 #include <linux/err.h>
0012 #include <linux/interrupt.h>
0013 #include <linux/io.h>
0014 #include <linux/irq.h>
0015 #include <linux/kernel.h>
0016 #include <linux/module.h>
0017 #include <linux/of_reserved_mem.h>
0018 #include <linux/platform_device.h>
0019 #include <linux/remoteproc.h>
0020 
0021 #include "remoteproc_internal.h"
0022 
0023 static char *da8xx_fw_name;
0024 module_param(da8xx_fw_name, charp, 0444);
0025 MODULE_PARM_DESC(da8xx_fw_name,
0026          "Name of DSP firmware file in /lib/firmware (if not specified defaults to 'rproc-dsp-fw')");
0027 
0028 /*
0029  * OMAP-L138 Technical References:
0030  * http://www.ti.com/product/omap-l138
0031  */
0032 #define SYSCFG_CHIPSIG0 BIT(0)
0033 #define SYSCFG_CHIPSIG1 BIT(1)
0034 #define SYSCFG_CHIPSIG2 BIT(2)
0035 #define SYSCFG_CHIPSIG3 BIT(3)
0036 #define SYSCFG_CHIPSIG4 BIT(4)
0037 
0038 #define DA8XX_RPROC_LOCAL_ADDRESS_MASK  (SZ_16M - 1)
0039 
0040 /**
0041  * struct da8xx_rproc_mem - internal memory structure
0042  * @cpu_addr: MPU virtual address of the memory region
0043  * @bus_addr: Bus address used to access the memory region
0044  * @dev_addr: Device address of the memory region from DSP view
0045  * @size: Size of the memory region
0046  */
0047 struct da8xx_rproc_mem {
0048     void __iomem *cpu_addr;
0049     phys_addr_t bus_addr;
0050     u32 dev_addr;
0051     size_t size;
0052 };
0053 
0054 /**
0055  * struct da8xx_rproc - da8xx remote processor instance state
0056  * @rproc: rproc handle
0057  * @mem: internal memory regions data
0058  * @num_mems: number of internal memory regions
0059  * @dsp_clk: placeholder for platform's DSP clk
0060  * @ack_fxn: chip-specific ack function for ack'ing irq
0061  * @irq_data: ack_fxn function parameter
0062  * @chipsig: virt ptr to DSP interrupt registers (CHIPSIG & CHIPSIG_CLR)
0063  * @bootreg: virt ptr to DSP boot address register (HOST1CFG)
0064  * @irq: irq # used by this instance
0065  */
0066 struct da8xx_rproc {
0067     struct rproc *rproc;
0068     struct da8xx_rproc_mem *mem;
0069     int num_mems;
0070     struct clk *dsp_clk;
0071     struct reset_control *dsp_reset;
0072     void (*ack_fxn)(struct irq_data *data);
0073     struct irq_data *irq_data;
0074     void __iomem *chipsig;
0075     void __iomem *bootreg;
0076     int irq;
0077 };
0078 
0079 /**
0080  * handle_event() - inbound virtqueue message workqueue function
0081  *
0082  * This function is registered as a kernel thread and is scheduled by the
0083  * kernel handler.
0084  */
0085 static irqreturn_t handle_event(int irq, void *p)
0086 {
0087     struct rproc *rproc = (struct rproc *)p;
0088 
0089     /* Process incoming buffers on all our vrings */
0090     rproc_vq_interrupt(rproc, 0);
0091     rproc_vq_interrupt(rproc, 1);
0092 
0093     return IRQ_HANDLED;
0094 }
0095 
0096 /**
0097  * da8xx_rproc_callback() - inbound virtqueue message handler
0098  *
0099  * This handler is invoked directly by the kernel whenever the remote
0100  * core (DSP) has modified the state of a virtqueue.  There is no
0101  * "payload" message indicating the virtqueue index as is the case with
0102  * mailbox-based implementations on OMAP4.  As such, this handler "polls"
0103  * each known virtqueue index for every invocation.
0104  */
0105 static irqreturn_t da8xx_rproc_callback(int irq, void *p)
0106 {
0107     struct rproc *rproc = (struct rproc *)p;
0108     struct da8xx_rproc *drproc = (struct da8xx_rproc *)rproc->priv;
0109     u32 chipsig;
0110 
0111     chipsig = readl(drproc->chipsig);
0112     if (chipsig & SYSCFG_CHIPSIG0) {
0113         /* Clear interrupt level source */
0114         writel(SYSCFG_CHIPSIG0, drproc->chipsig + 4);
0115 
0116         /*
0117          * ACK intr to AINTC.
0118          *
0119          * It has already been ack'ed by the kernel before calling
0120          * this function, but since the ARM<->DSP interrupts in the
0121          * CHIPSIG register are "level" instead of "pulse" variety,
0122          * we need to ack it after taking down the level else we'll
0123          * be called again immediately after returning.
0124          */
0125         drproc->ack_fxn(drproc->irq_data);
0126 
0127         return IRQ_WAKE_THREAD;
0128     }
0129 
0130     return IRQ_HANDLED;
0131 }
0132 
0133 static int da8xx_rproc_start(struct rproc *rproc)
0134 {
0135     struct device *dev = rproc->dev.parent;
0136     struct da8xx_rproc *drproc = (struct da8xx_rproc *)rproc->priv;
0137     struct clk *dsp_clk = drproc->dsp_clk;
0138     struct reset_control *dsp_reset = drproc->dsp_reset;
0139     int ret;
0140 
0141     /* hw requires the start (boot) address be on 1KB boundary */
0142     if (rproc->bootaddr & 0x3ff) {
0143         dev_err(dev, "invalid boot address: must be aligned to 1KB\n");
0144 
0145         return -EINVAL;
0146     }
0147 
0148     writel(rproc->bootaddr, drproc->bootreg);
0149 
0150     ret = clk_prepare_enable(dsp_clk);
0151     if (ret) {
0152         dev_err(dev, "clk_prepare_enable() failed: %d\n", ret);
0153         return ret;
0154     }
0155 
0156     ret = reset_control_deassert(dsp_reset);
0157     if (ret) {
0158         dev_err(dev, "reset_control_deassert() failed: %d\n", ret);
0159         clk_disable_unprepare(dsp_clk);
0160         return ret;
0161     }
0162 
0163     return 0;
0164 }
0165 
0166 static int da8xx_rproc_stop(struct rproc *rproc)
0167 {
0168     struct da8xx_rproc *drproc = rproc->priv;
0169     struct device *dev = rproc->dev.parent;
0170     int ret;
0171 
0172     ret = reset_control_assert(drproc->dsp_reset);
0173     if (ret) {
0174         dev_err(dev, "reset_control_assert() failed: %d\n", ret);
0175         return ret;
0176     }
0177 
0178     clk_disable_unprepare(drproc->dsp_clk);
0179 
0180     return 0;
0181 }
0182 
0183 /* kick a virtqueue */
0184 static void da8xx_rproc_kick(struct rproc *rproc, int vqid)
0185 {
0186     struct da8xx_rproc *drproc = (struct da8xx_rproc *)rproc->priv;
0187 
0188     /* Interrupt remote proc */
0189     writel(SYSCFG_CHIPSIG2, drproc->chipsig);
0190 }
0191 
0192 static const struct rproc_ops da8xx_rproc_ops = {
0193     .start = da8xx_rproc_start,
0194     .stop = da8xx_rproc_stop,
0195     .kick = da8xx_rproc_kick,
0196 };
0197 
0198 static int da8xx_rproc_get_internal_memories(struct platform_device *pdev,
0199                          struct da8xx_rproc *drproc)
0200 {
0201     static const char * const mem_names[] = {"l2sram", "l1pram", "l1dram"};
0202     int num_mems = ARRAY_SIZE(mem_names);
0203     struct device *dev = &pdev->dev;
0204     struct resource *res;
0205     int i;
0206 
0207     drproc->mem = devm_kcalloc(dev, num_mems, sizeof(*drproc->mem),
0208                    GFP_KERNEL);
0209     if (!drproc->mem)
0210         return -ENOMEM;
0211 
0212     for (i = 0; i < num_mems; i++) {
0213         res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
0214                            mem_names[i]);
0215         drproc->mem[i].cpu_addr = devm_ioremap_resource(dev, res);
0216         if (IS_ERR(drproc->mem[i].cpu_addr)) {
0217             dev_err(dev, "failed to parse and map %s memory\n",
0218                 mem_names[i]);
0219             return PTR_ERR(drproc->mem[i].cpu_addr);
0220         }
0221         drproc->mem[i].bus_addr = res->start;
0222         drproc->mem[i].dev_addr =
0223                 res->start & DA8XX_RPROC_LOCAL_ADDRESS_MASK;
0224         drproc->mem[i].size = resource_size(res);
0225 
0226         dev_dbg(dev, "memory %8s: bus addr %pa size 0x%zx va %p da 0x%x\n",
0227             mem_names[i], &drproc->mem[i].bus_addr,
0228             drproc->mem[i].size, drproc->mem[i].cpu_addr,
0229             drproc->mem[i].dev_addr);
0230     }
0231     drproc->num_mems = num_mems;
0232 
0233     return 0;
0234 }
0235 
0236 static int da8xx_rproc_probe(struct platform_device *pdev)
0237 {
0238     struct device *dev = &pdev->dev;
0239     struct da8xx_rproc *drproc;
0240     struct rproc *rproc;
0241     struct irq_data *irq_data;
0242     struct resource *bootreg_res;
0243     struct resource *chipsig_res;
0244     struct clk *dsp_clk;
0245     struct reset_control *dsp_reset;
0246     void __iomem *chipsig;
0247     void __iomem *bootreg;
0248     int irq;
0249     int ret;
0250 
0251     irq = platform_get_irq(pdev, 0);
0252     if (irq < 0)
0253         return irq;
0254 
0255     irq_data = irq_get_irq_data(irq);
0256     if (!irq_data) {
0257         dev_err(dev, "irq_get_irq_data(%d): NULL\n", irq);
0258         return -EINVAL;
0259     }
0260 
0261     bootreg_res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
0262                            "host1cfg");
0263     bootreg = devm_ioremap_resource(dev, bootreg_res);
0264     if (IS_ERR(bootreg))
0265         return PTR_ERR(bootreg);
0266 
0267     chipsig_res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
0268                            "chipsig");
0269     chipsig = devm_ioremap_resource(dev, chipsig_res);
0270     if (IS_ERR(chipsig))
0271         return PTR_ERR(chipsig);
0272 
0273     dsp_clk = devm_clk_get(dev, NULL);
0274     if (IS_ERR(dsp_clk)) {
0275         dev_err(dev, "clk_get error: %ld\n", PTR_ERR(dsp_clk));
0276 
0277         return PTR_ERR(dsp_clk);
0278     }
0279 
0280     dsp_reset = devm_reset_control_get_exclusive(dev, NULL);
0281     if (IS_ERR(dsp_reset)) {
0282         if (PTR_ERR(dsp_reset) != -EPROBE_DEFER)
0283             dev_err(dev, "unable to get reset control: %ld\n",
0284                 PTR_ERR(dsp_reset));
0285 
0286         return PTR_ERR(dsp_reset);
0287     }
0288 
0289     if (dev->of_node) {
0290         ret = of_reserved_mem_device_init(dev);
0291         if (ret) {
0292             dev_err(dev, "device does not have specific CMA pool: %d\n",
0293                 ret);
0294             return ret;
0295         }
0296     }
0297 
0298     rproc = rproc_alloc(dev, "dsp", &da8xx_rproc_ops, da8xx_fw_name,
0299         sizeof(*drproc));
0300     if (!rproc) {
0301         ret = -ENOMEM;
0302         goto free_mem;
0303     }
0304 
0305     /* error recovery is not supported at present */
0306     rproc->recovery_disabled = true;
0307 
0308     drproc = rproc->priv;
0309     drproc->rproc = rproc;
0310     drproc->dsp_clk = dsp_clk;
0311     drproc->dsp_reset = dsp_reset;
0312     rproc->has_iommu = false;
0313 
0314     ret = da8xx_rproc_get_internal_memories(pdev, drproc);
0315     if (ret)
0316         goto free_rproc;
0317 
0318     platform_set_drvdata(pdev, rproc);
0319 
0320     /* everything the ISR needs is now setup, so hook it up */
0321     ret = devm_request_threaded_irq(dev, irq, da8xx_rproc_callback,
0322                     handle_event, 0, "da8xx-remoteproc",
0323                     rproc);
0324     if (ret) {
0325         dev_err(dev, "devm_request_threaded_irq error: %d\n", ret);
0326         goto free_rproc;
0327     }
0328 
0329     /*
0330      * rproc_add() can end up enabling the DSP's clk with the DSP
0331      * *not* in reset, but da8xx_rproc_start() needs the DSP to be
0332      * held in reset at the time it is called.
0333      */
0334     ret = reset_control_assert(dsp_reset);
0335     if (ret)
0336         goto free_rproc;
0337 
0338     drproc->chipsig = chipsig;
0339     drproc->bootreg = bootreg;
0340     drproc->ack_fxn = irq_data->chip->irq_ack;
0341     drproc->irq_data = irq_data;
0342     drproc->irq = irq;
0343 
0344     ret = rproc_add(rproc);
0345     if (ret) {
0346         dev_err(dev, "rproc_add failed: %d\n", ret);
0347         goto free_rproc;
0348     }
0349 
0350     return 0;
0351 
0352 free_rproc:
0353     rproc_free(rproc);
0354 free_mem:
0355     if (dev->of_node)
0356         of_reserved_mem_device_release(dev);
0357     return ret;
0358 }
0359 
0360 static int da8xx_rproc_remove(struct platform_device *pdev)
0361 {
0362     struct rproc *rproc = platform_get_drvdata(pdev);
0363     struct da8xx_rproc *drproc = (struct da8xx_rproc *)rproc->priv;
0364     struct device *dev = &pdev->dev;
0365 
0366     /*
0367      * The devm subsystem might end up releasing things before
0368      * freeing the irq, thus allowing an interrupt to sneak in while
0369      * the device is being removed.  This should prevent that.
0370      */
0371     disable_irq(drproc->irq);
0372 
0373     rproc_del(rproc);
0374     rproc_free(rproc);
0375     if (dev->of_node)
0376         of_reserved_mem_device_release(dev);
0377 
0378     return 0;
0379 }
0380 
0381 static const struct of_device_id davinci_rproc_of_match[] __maybe_unused = {
0382     { .compatible = "ti,da850-dsp", },
0383     { /* sentinel */ },
0384 };
0385 MODULE_DEVICE_TABLE(of, davinci_rproc_of_match);
0386 
0387 static struct platform_driver da8xx_rproc_driver = {
0388     .probe = da8xx_rproc_probe,
0389     .remove = da8xx_rproc_remove,
0390     .driver = {
0391         .name = "davinci-rproc",
0392         .of_match_table = of_match_ptr(davinci_rproc_of_match),
0393     },
0394 };
0395 
0396 module_platform_driver(da8xx_rproc_driver);
0397 
0398 MODULE_LICENSE("GPL v2");
0399 MODULE_DESCRIPTION("DA8XX Remote Processor control driver");