Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * SLIM core rproc driver
0004  *
0005  * Copyright (C) 2016 STMicroelectronics
0006  *
0007  * Author: Peter Griffin <peter.griffin@linaro.org>
0008  */
0009 
0010 #include <linux/clk.h>
0011 #include <linux/err.h>
0012 #include <linux/kernel.h>
0013 #include <linux/module.h>
0014 #include <linux/of.h>
0015 #include <linux/of_device.h>
0016 #include <linux/platform_device.h>
0017 #include <linux/remoteproc.h>
0018 #include <linux/remoteproc/st_slim_rproc.h>
0019 #include "remoteproc_internal.h"
0020 
0021 /* SLIM core registers */
0022 #define SLIM_ID_OFST        0x0
0023 #define SLIM_VER_OFST       0x4
0024 
0025 #define SLIM_EN_OFST        0x8
0026 #define SLIM_EN_RUN         BIT(0)
0027 
0028 #define SLIM_CLK_GATE_OFST  0xC
0029 #define SLIM_CLK_GATE_DIS       BIT(0)
0030 #define SLIM_CLK_GATE_RESET     BIT(2)
0031 
0032 #define SLIM_SLIM_PC_OFST   0x20
0033 
0034 /* DMEM registers */
0035 #define SLIM_REV_ID_OFST    0x0
0036 #define SLIM_REV_ID_MIN_MASK        GENMASK(15, 8)
0037 #define SLIM_REV_ID_MIN(id)     ((id & SLIM_REV_ID_MIN_MASK) >> 8)
0038 #define SLIM_REV_ID_MAJ_MASK        GENMASK(23, 16)
0039 #define SLIM_REV_ID_MAJ(id)     ((id & SLIM_REV_ID_MAJ_MASK) >> 16)
0040 
0041 
0042 /* peripherals registers */
0043 #define SLIM_STBUS_SYNC_OFST    0xF88
0044 #define SLIM_STBUS_SYNC_DIS     BIT(0)
0045 
0046 #define SLIM_INT_SET_OFST   0xFD4
0047 #define SLIM_INT_CLR_OFST   0xFD8
0048 #define SLIM_INT_MASK_OFST  0xFDC
0049 
0050 #define SLIM_CMD_CLR_OFST   0xFC8
0051 #define SLIM_CMD_MASK_OFST  0xFCC
0052 
0053 static const char *mem_names[ST_SLIM_MEM_MAX] = {
0054     [ST_SLIM_DMEM]  = "dmem",
0055     [ST_SLIM_IMEM]  = "imem",
0056 };
0057 
0058 static int slim_clk_get(struct st_slim_rproc *slim_rproc, struct device *dev)
0059 {
0060     int clk, err;
0061 
0062     for (clk = 0; clk < ST_SLIM_MAX_CLK; clk++) {
0063         slim_rproc->clks[clk] = of_clk_get(dev->of_node, clk);
0064         if (IS_ERR(slim_rproc->clks[clk])) {
0065             err = PTR_ERR(slim_rproc->clks[clk]);
0066             if (err == -EPROBE_DEFER)
0067                 goto err_put_clks;
0068             slim_rproc->clks[clk] = NULL;
0069             break;
0070         }
0071     }
0072 
0073     return 0;
0074 
0075 err_put_clks:
0076     while (--clk >= 0)
0077         clk_put(slim_rproc->clks[clk]);
0078 
0079     return err;
0080 }
0081 
0082 static void slim_clk_disable(struct st_slim_rproc *slim_rproc)
0083 {
0084     int clk;
0085 
0086     for (clk = 0; clk < ST_SLIM_MAX_CLK && slim_rproc->clks[clk]; clk++)
0087         clk_disable_unprepare(slim_rproc->clks[clk]);
0088 }
0089 
0090 static int slim_clk_enable(struct st_slim_rproc *slim_rproc)
0091 {
0092     int clk, ret;
0093 
0094     for (clk = 0; clk < ST_SLIM_MAX_CLK && slim_rproc->clks[clk]; clk++) {
0095         ret = clk_prepare_enable(slim_rproc->clks[clk]);
0096         if (ret)
0097             goto err_disable_clks;
0098     }
0099 
0100     return 0;
0101 
0102 err_disable_clks:
0103     while (--clk >= 0)
0104         clk_disable_unprepare(slim_rproc->clks[clk]);
0105 
0106     return ret;
0107 }
0108 
0109 /*
0110  * Remoteproc slim specific device handlers
0111  */
0112 static int slim_rproc_start(struct rproc *rproc)
0113 {
0114     struct device *dev = &rproc->dev;
0115     struct st_slim_rproc *slim_rproc = rproc->priv;
0116     unsigned long hw_id, hw_ver, fw_rev;
0117     u32 val;
0118 
0119     /* disable CPU pipeline clock & reset CPU pipeline */
0120     val = SLIM_CLK_GATE_DIS | SLIM_CLK_GATE_RESET;
0121     writel(val, slim_rproc->slimcore + SLIM_CLK_GATE_OFST);
0122 
0123     /* disable SLIM core STBus sync */
0124     writel(SLIM_STBUS_SYNC_DIS, slim_rproc->peri + SLIM_STBUS_SYNC_OFST);
0125 
0126     /* enable cpu pipeline clock */
0127     writel(!SLIM_CLK_GATE_DIS,
0128         slim_rproc->slimcore + SLIM_CLK_GATE_OFST);
0129 
0130     /* clear int & cmd mailbox */
0131     writel(~0U, slim_rproc->peri + SLIM_INT_CLR_OFST);
0132     writel(~0U, slim_rproc->peri + SLIM_CMD_CLR_OFST);
0133 
0134     /* enable all channels cmd & int */
0135     writel(~0U, slim_rproc->peri + SLIM_INT_MASK_OFST);
0136     writel(~0U, slim_rproc->peri + SLIM_CMD_MASK_OFST);
0137 
0138     /* enable cpu */
0139     writel(SLIM_EN_RUN, slim_rproc->slimcore + SLIM_EN_OFST);
0140 
0141     hw_id = readl_relaxed(slim_rproc->slimcore + SLIM_ID_OFST);
0142     hw_ver = readl_relaxed(slim_rproc->slimcore + SLIM_VER_OFST);
0143 
0144     fw_rev = readl(slim_rproc->mem[ST_SLIM_DMEM].cpu_addr +
0145             SLIM_REV_ID_OFST);
0146 
0147     dev_info(dev, "fw rev:%ld.%ld on SLIM %ld.%ld\n",
0148          SLIM_REV_ID_MAJ(fw_rev), SLIM_REV_ID_MIN(fw_rev),
0149          hw_id, hw_ver);
0150 
0151     return 0;
0152 }
0153 
0154 static int slim_rproc_stop(struct rproc *rproc)
0155 {
0156     struct st_slim_rproc *slim_rproc = rproc->priv;
0157     u32 val;
0158 
0159     /* mask all (cmd & int) channels */
0160     writel(0UL, slim_rproc->peri + SLIM_INT_MASK_OFST);
0161     writel(0UL, slim_rproc->peri + SLIM_CMD_MASK_OFST);
0162 
0163     /* disable cpu pipeline clock */
0164     writel(SLIM_CLK_GATE_DIS, slim_rproc->slimcore + SLIM_CLK_GATE_OFST);
0165 
0166     writel(!SLIM_EN_RUN, slim_rproc->slimcore + SLIM_EN_OFST);
0167 
0168     val = readl(slim_rproc->slimcore + SLIM_EN_OFST);
0169     if (val & SLIM_EN_RUN)
0170         dev_warn(&rproc->dev, "Failed to disable SLIM");
0171 
0172     dev_dbg(&rproc->dev, "slim stopped\n");
0173 
0174     return 0;
0175 }
0176 
0177 static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem)
0178 {
0179     struct st_slim_rproc *slim_rproc = rproc->priv;
0180     void *va = NULL;
0181     int i;
0182 
0183     for (i = 0; i < ST_SLIM_MEM_MAX; i++) {
0184         if (da != slim_rproc->mem[i].bus_addr)
0185             continue;
0186 
0187         if (len <= slim_rproc->mem[i].size) {
0188             /* __force to make sparse happy with type conversion */
0189             va = (__force void *)slim_rproc->mem[i].cpu_addr;
0190             break;
0191         }
0192     }
0193 
0194     dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%zx va = 0x%pK\n",
0195         da, len, va);
0196 
0197     return va;
0198 }
0199 
0200 static const struct rproc_ops slim_rproc_ops = {
0201     .start      = slim_rproc_start,
0202     .stop       = slim_rproc_stop,
0203     .da_to_va       = slim_rproc_da_to_va,
0204     .get_boot_addr  = rproc_elf_get_boot_addr,
0205     .load       = rproc_elf_load_segments,
0206     .sanity_check   = rproc_elf_sanity_check,
0207 };
0208 
0209 /**
0210  * st_slim_rproc_alloc() - allocate and initialise slim rproc
0211  * @pdev: Pointer to the platform_device struct
0212  * @fw_name: Name of firmware for rproc to use
0213  *
0214  * Function for allocating and initialising a slim rproc for use by
0215  * device drivers whose IP is based around the SLIM core. It
0216  * obtains and enables any clocks required by the SLIM core and also
0217  * ioremaps the various IO.
0218  *
0219  * Return: st_slim_rproc pointer or PTR_ERR() on error.
0220  */
0221 
0222 struct st_slim_rproc *st_slim_rproc_alloc(struct platform_device *pdev,
0223                 char *fw_name)
0224 {
0225     struct device *dev = &pdev->dev;
0226     struct st_slim_rproc *slim_rproc;
0227     struct device_node *np = dev->of_node;
0228     struct rproc *rproc;
0229     struct resource *res;
0230     int err, i;
0231 
0232     if (!fw_name)
0233         return ERR_PTR(-EINVAL);
0234 
0235     if (!of_device_is_compatible(np, "st,slim-rproc"))
0236         return ERR_PTR(-EINVAL);
0237 
0238     rproc = rproc_alloc(dev, np->name, &slim_rproc_ops,
0239             fw_name, sizeof(*slim_rproc));
0240     if (!rproc)
0241         return ERR_PTR(-ENOMEM);
0242 
0243     rproc->has_iommu = false;
0244 
0245     slim_rproc = rproc->priv;
0246     slim_rproc->rproc = rproc;
0247 
0248     /* get imem and dmem */
0249     for (i = 0; i < ARRAY_SIZE(mem_names); i++) {
0250         res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
0251                         mem_names[i]);
0252 
0253         slim_rproc->mem[i].cpu_addr = devm_ioremap_resource(dev, res);
0254         if (IS_ERR(slim_rproc->mem[i].cpu_addr)) {
0255             dev_err(&pdev->dev, "devm_ioremap_resource failed\n");
0256             err = PTR_ERR(slim_rproc->mem[i].cpu_addr);
0257             goto err;
0258         }
0259         slim_rproc->mem[i].bus_addr = res->start;
0260         slim_rproc->mem[i].size = resource_size(res);
0261     }
0262 
0263     res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "slimcore");
0264     slim_rproc->slimcore = devm_ioremap_resource(dev, res);
0265     if (IS_ERR(slim_rproc->slimcore)) {
0266         dev_err(&pdev->dev, "failed to ioremap slimcore IO\n");
0267         err = PTR_ERR(slim_rproc->slimcore);
0268         goto err;
0269     }
0270 
0271     res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "peripherals");
0272     slim_rproc->peri = devm_ioremap_resource(dev, res);
0273     if (IS_ERR(slim_rproc->peri)) {
0274         dev_err(&pdev->dev, "failed to ioremap peripherals IO\n");
0275         err = PTR_ERR(slim_rproc->peri);
0276         goto err;
0277     }
0278 
0279     err = slim_clk_get(slim_rproc, dev);
0280     if (err)
0281         goto err;
0282 
0283     err = slim_clk_enable(slim_rproc);
0284     if (err) {
0285         dev_err(dev, "Failed to enable clocks\n");
0286         goto err_clk_put;
0287     }
0288 
0289     /* Register as a remoteproc device */
0290     err = rproc_add(rproc);
0291     if (err) {
0292         dev_err(dev, "registration of slim remoteproc failed\n");
0293         goto err_clk_dis;
0294     }
0295 
0296     return slim_rproc;
0297 
0298 err_clk_dis:
0299     slim_clk_disable(slim_rproc);
0300 err_clk_put:
0301     for (i = 0; i < ST_SLIM_MAX_CLK && slim_rproc->clks[i]; i++)
0302         clk_put(slim_rproc->clks[i]);
0303 err:
0304     rproc_free(rproc);
0305     return ERR_PTR(err);
0306 }
0307 EXPORT_SYMBOL(st_slim_rproc_alloc);
0308 
0309 /**
0310   * st_slim_rproc_put() - put slim rproc resources
0311   * @slim_rproc: Pointer to the st_slim_rproc struct
0312   *
0313   * Function for calling respective _put() functions on slim_rproc resources.
0314   *
0315   */
0316 void st_slim_rproc_put(struct st_slim_rproc *slim_rproc)
0317 {
0318     int clk;
0319 
0320     if (!slim_rproc)
0321         return;
0322 
0323     slim_clk_disable(slim_rproc);
0324 
0325     for (clk = 0; clk < ST_SLIM_MAX_CLK && slim_rproc->clks[clk]; clk++)
0326         clk_put(slim_rproc->clks[clk]);
0327 
0328     rproc_del(slim_rproc->rproc);
0329     rproc_free(slim_rproc->rproc);
0330 }
0331 EXPORT_SYMBOL(st_slim_rproc_put);
0332 
0333 MODULE_AUTHOR("Peter Griffin <peter.griffin@linaro.org>");
0334 MODULE_DESCRIPTION("STMicroelectronics SLIM core rproc driver");
0335 MODULE_LICENSE("GPL v2");