1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * SLIM core rproc driver
5 * Copyright (C) 2016 STMicroelectronics
7 * Author: Peter Griffin <peter.griffin@linaro.org>
10 #include <linux/clk.h>
11 #include <linux/err.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
15 #include <linux/of_device.h>
16 #include <linux/platform_device.h>
17 #include <linux/remoteproc.h>
18 #include <linux/remoteproc/st_slim_rproc.h>
19 #include "remoteproc_internal.h"
21 /* SLIM core registers */
22 #define SLIM_ID_OFST 0x0
23 #define SLIM_VER_OFST 0x4
25 #define SLIM_EN_OFST 0x8
26 #define SLIM_EN_RUN BIT(0)
28 #define SLIM_CLK_GATE_OFST 0xC
29 #define SLIM_CLK_GATE_DIS BIT(0)
30 #define SLIM_CLK_GATE_RESET BIT(2)
32 #define SLIM_SLIM_PC_OFST 0x20
35 #define SLIM_REV_ID_OFST 0x0
36 #define SLIM_REV_ID_MIN_MASK GENMASK(15, 8)
37 #define SLIM_REV_ID_MIN(id) ((id & SLIM_REV_ID_MIN_MASK) >> 8)
38 #define SLIM_REV_ID_MAJ_MASK GENMASK(23, 16)
39 #define SLIM_REV_ID_MAJ(id) ((id & SLIM_REV_ID_MAJ_MASK) >> 16)
42 /* peripherals registers */
43 #define SLIM_STBUS_SYNC_OFST 0xF88
44 #define SLIM_STBUS_SYNC_DIS BIT(0)
46 #define SLIM_INT_SET_OFST 0xFD4
47 #define SLIM_INT_CLR_OFST 0xFD8
48 #define SLIM_INT_MASK_OFST 0xFDC
50 #define SLIM_CMD_CLR_OFST 0xFC8
51 #define SLIM_CMD_MASK_OFST 0xFCC
53 static const char *mem_names[ST_SLIM_MEM_MAX] = {
54 [ST_SLIM_DMEM] = "dmem",
55 [ST_SLIM_IMEM] = "imem",
58 static int slim_clk_get(struct st_slim_rproc *slim_rproc, struct device *dev)
62 for (clk = 0; clk < ST_SLIM_MAX_CLK; clk++) {
63 slim_rproc->clks[clk] = of_clk_get(dev->of_node, clk);
64 if (IS_ERR(slim_rproc->clks[clk])) {
65 err = PTR_ERR(slim_rproc->clks[clk]);
66 if (err == -EPROBE_DEFER)
68 slim_rproc->clks[clk] = NULL;
77 clk_put(slim_rproc->clks[clk]);
82 static void slim_clk_disable(struct st_slim_rproc *slim_rproc)
86 for (clk = 0; clk < ST_SLIM_MAX_CLK && slim_rproc->clks[clk]; clk++)
87 clk_disable_unprepare(slim_rproc->clks[clk]);
90 static int slim_clk_enable(struct st_slim_rproc *slim_rproc)
94 for (clk = 0; clk < ST_SLIM_MAX_CLK && slim_rproc->clks[clk]; clk++) {
95 ret = clk_prepare_enable(slim_rproc->clks[clk]);
97 goto err_disable_clks;
104 clk_disable_unprepare(slim_rproc->clks[clk]);
110 * Remoteproc slim specific device handlers
112 static int slim_rproc_start(struct rproc *rproc)
114 struct device *dev = &rproc->dev;
115 struct st_slim_rproc *slim_rproc = rproc->priv;
116 unsigned long hw_id, hw_ver, fw_rev;
119 /* disable CPU pipeline clock & reset CPU pipeline */
120 val = SLIM_CLK_GATE_DIS | SLIM_CLK_GATE_RESET;
121 writel(val, slim_rproc->slimcore + SLIM_CLK_GATE_OFST);
123 /* disable SLIM core STBus sync */
124 writel(SLIM_STBUS_SYNC_DIS, slim_rproc->peri + SLIM_STBUS_SYNC_OFST);
126 /* enable cpu pipeline clock */
127 writel(!SLIM_CLK_GATE_DIS,
128 slim_rproc->slimcore + SLIM_CLK_GATE_OFST);
130 /* clear int & cmd mailbox */
131 writel(~0U, slim_rproc->peri + SLIM_INT_CLR_OFST);
132 writel(~0U, slim_rproc->peri + SLIM_CMD_CLR_OFST);
134 /* enable all channels cmd & int */
135 writel(~0U, slim_rproc->peri + SLIM_INT_MASK_OFST);
136 writel(~0U, slim_rproc->peri + SLIM_CMD_MASK_OFST);
139 writel(SLIM_EN_RUN, slim_rproc->slimcore + SLIM_EN_OFST);
141 hw_id = readl_relaxed(slim_rproc->slimcore + SLIM_ID_OFST);
142 hw_ver = readl_relaxed(slim_rproc->slimcore + SLIM_VER_OFST);
144 fw_rev = readl(slim_rproc->mem[ST_SLIM_DMEM].cpu_addr +
147 dev_info(dev, "fw rev:%ld.%ld on SLIM %ld.%ld\n",
148 SLIM_REV_ID_MAJ(fw_rev), SLIM_REV_ID_MIN(fw_rev),
154 static int slim_rproc_stop(struct rproc *rproc)
156 struct st_slim_rproc *slim_rproc = rproc->priv;
159 /* mask all (cmd & int) channels */
160 writel(0UL, slim_rproc->peri + SLIM_INT_MASK_OFST);
161 writel(0UL, slim_rproc->peri + SLIM_CMD_MASK_OFST);
163 /* disable cpu pipeline clock */
164 writel(SLIM_CLK_GATE_DIS, slim_rproc->slimcore + SLIM_CLK_GATE_OFST);
166 writel(!SLIM_EN_RUN, slim_rproc->slimcore + SLIM_EN_OFST);
168 val = readl(slim_rproc->slimcore + SLIM_EN_OFST);
169 if (val & SLIM_EN_RUN)
170 dev_warn(&rproc->dev, "Failed to disable SLIM");
172 dev_dbg(&rproc->dev, "slim stopped\n");
177 static void *slim_rproc_da_to_va(struct rproc *rproc, u64 da, size_t len)
179 struct st_slim_rproc *slim_rproc = rproc->priv;
183 for (i = 0; i < ST_SLIM_MEM_MAX; i++) {
184 if (da != slim_rproc->mem[i].bus_addr)
187 if (len <= slim_rproc->mem[i].size) {
188 /* __force to make sparse happy with type conversion */
189 va = (__force void *)slim_rproc->mem[i].cpu_addr;
194 dev_dbg(&rproc->dev, "da = 0x%llx len = 0x%zx va = 0x%pK\n",
200 static const struct rproc_ops slim_rproc_ops = {
201 .start = slim_rproc_start,
202 .stop = slim_rproc_stop,
203 .da_to_va = slim_rproc_da_to_va,
204 .get_boot_addr = rproc_elf_get_boot_addr,
205 .load = rproc_elf_load_segments,
206 .sanity_check = rproc_elf_sanity_check,
210 * st_slim_rproc_alloc() - allocate and initialise slim rproc
211 * @pdev: Pointer to the platform_device struct
212 * @fw_name: Name of firmware for rproc to use
214 * Function for allocating and initialising a slim rproc for use by
215 * device drivers whose IP is based around the SLIM core. It
216 * obtains and enables any clocks required by the SLIM core and also
217 * ioremaps the various IO.
219 * Returns st_slim_rproc pointer or PTR_ERR() on error.
222 struct st_slim_rproc *st_slim_rproc_alloc(struct platform_device *pdev,
225 struct device *dev = &pdev->dev;
226 struct st_slim_rproc *slim_rproc;
227 struct device_node *np = dev->of_node;
229 struct resource *res;
233 return ERR_PTR(-EINVAL);
235 if (!of_device_is_compatible(np, "st,slim-rproc"))
236 return ERR_PTR(-EINVAL);
238 rproc = rproc_alloc(dev, np->name, &slim_rproc_ops,
239 fw_name, sizeof(*slim_rproc));
241 return ERR_PTR(-ENOMEM);
243 rproc->has_iommu = false;
245 slim_rproc = rproc->priv;
246 slim_rproc->rproc = rproc;
248 /* get imem and dmem */
249 for (i = 0; i < ARRAY_SIZE(mem_names); i++) {
250 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
253 slim_rproc->mem[i].cpu_addr = devm_ioremap_resource(dev, res);
254 if (IS_ERR(slim_rproc->mem[i].cpu_addr)) {
255 dev_err(&pdev->dev, "devm_ioremap_resource failed\n");
256 err = PTR_ERR(slim_rproc->mem[i].cpu_addr);
259 slim_rproc->mem[i].bus_addr = res->start;
260 slim_rproc->mem[i].size = resource_size(res);
263 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "slimcore");
264 slim_rproc->slimcore = devm_ioremap_resource(dev, res);
265 if (IS_ERR(slim_rproc->slimcore)) {
266 dev_err(&pdev->dev, "failed to ioremap slimcore IO\n");
267 err = PTR_ERR(slim_rproc->slimcore);
271 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "peripherals");
272 slim_rproc->peri = devm_ioremap_resource(dev, res);
273 if (IS_ERR(slim_rproc->peri)) {
274 dev_err(&pdev->dev, "failed to ioremap peripherals IO\n");
275 err = PTR_ERR(slim_rproc->peri);
279 err = slim_clk_get(slim_rproc, dev);
283 err = slim_clk_enable(slim_rproc);
285 dev_err(dev, "Failed to enable clocks\n");
289 /* Register as a remoteproc device */
290 err = rproc_add(rproc);
292 dev_err(dev, "registration of slim remoteproc failed\n");
299 slim_clk_disable(slim_rproc);
301 for (i = 0; i < ST_SLIM_MAX_CLK && slim_rproc->clks[i]; i++)
302 clk_put(slim_rproc->clks[i]);
307 EXPORT_SYMBOL(st_slim_rproc_alloc);
310 * st_slim_rproc_put() - put slim rproc resources
311 * @slim_rproc: Pointer to the st_slim_rproc struct
313 * Function for calling respective _put() functions on slim_rproc resources.
316 void st_slim_rproc_put(struct st_slim_rproc *slim_rproc)
323 slim_clk_disable(slim_rproc);
325 for (clk = 0; clk < ST_SLIM_MAX_CLK && slim_rproc->clks[clk]; clk++)
326 clk_put(slim_rproc->clks[clk]);
328 rproc_del(slim_rproc->rproc);
329 rproc_free(slim_rproc->rproc);
331 EXPORT_SYMBOL(st_slim_rproc_put);
333 MODULE_AUTHOR("Peter Griffin <peter.griffin@linaro.org>");
334 MODULE_DESCRIPTION("STMicroelectronics SLIM core rproc driver");
335 MODULE_LICENSE("GPL v2");