1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
3 * Copyright (C) 2019, STMicroelectronics - All Rights Reserved
5 #define pr_fmt(fmt) "%s: " fmt, __func__
11 #include <remoteproc.h>
15 #include <dm/device_compat.h>
16 #include <linux/err.h>
18 #define RCC_GCR_HOLD_BOOT 0
19 #define RCC_GCR_RELEASE_BOOT 1
22 * struct stm32_copro_privdata - power processor private data
23 * @reset_ctl: reset controller handle
24 * @hold_boot_regmap: regmap for remote processor reset hold boot
25 * @hold_boot_offset: offset of the register controlling the hold boot setting
26 * @hold_boot_mask: bitmask of the register for the hold boot field
27 * @rsc_table_addr: resource table address
29 struct stm32_copro_privdata {
30 struct reset_ctl reset_ctl;
31 struct regmap *hold_boot_regmap;
32 uint hold_boot_offset;
38 * stm32_copro_probe() - Basic probe
39 * @dev: corresponding STM32 remote processor device
40 * @return 0 if all went ok, else corresponding -ve error
42 static int stm32_copro_probe(struct udevice *dev)
44 struct stm32_copro_privdata *priv;
45 struct regmap *regmap;
49 priv = dev_get_priv(dev);
51 regmap = syscon_regmap_lookup_by_phandle(dev, "st,syscfg-holdboot");
53 dev_err(dev, "unable to find holdboot regmap (%ld)\n",
55 return PTR_ERR(regmap);
58 cell = dev_read_prop(dev, "st,syscfg-holdboot", &len);
59 if (len < 3 * sizeof(fdt32_t)) {
60 dev_err(dev, "holdboot offset and mask not available\n");
64 priv->hold_boot_regmap = regmap;
65 priv->hold_boot_offset = fdtdec_get_number(cell + 1, 1);
66 priv->hold_boot_mask = fdtdec_get_number(cell + 2, 1);
68 ret = reset_get_by_index(dev, 0, &priv->reset_ctl);
70 dev_err(dev, "failed to get reset (%d)\n", ret);
74 dev_dbg(dev, "probed\n");
80 * stm32_copro_set_hold_boot() - Hold boot bit management
81 * @dev: corresponding STM32 remote processor device
82 * @hold: hold boot value
83 * @return 0 if all went ok, else corresponding -ve error
85 static int stm32_copro_set_hold_boot(struct udevice *dev, bool hold)
87 struct stm32_copro_privdata *priv;
91 priv = dev_get_priv(dev);
93 val = hold ? RCC_GCR_HOLD_BOOT : RCC_GCR_RELEASE_BOOT;
96 * Note: shall run an SMC call (STM32_SMC_RCC) if platform is secured.
97 * To be updated when the code for this SMC service is available which
98 * is not the case for the time being.
100 ret = regmap_update_bits(priv->hold_boot_regmap, priv->hold_boot_offset,
101 priv->hold_boot_mask, val);
103 dev_err(dev, "failed to set hold boot\n");
109 * stm32_copro_device_to_virt() - Convert device address to virtual address
110 * @dev: corresponding STM32 remote processor device
111 * @da: device address
112 * @size: Size of the memory region @da is pointing to
113 * @return converted virtual address
115 static void *stm32_copro_device_to_virt(struct udevice *dev, ulong da,
118 fdt32_t in_addr = cpu_to_be32(da), end_addr;
121 paddr = dev_translate_dma_address(dev, &in_addr);
122 if (paddr == OF_BAD_ADDR) {
123 dev_err(dev, "Unable to convert address %ld\n", da);
127 end_addr = cpu_to_be32(da + size - 1);
128 if (dev_translate_dma_address(dev, &end_addr) == OF_BAD_ADDR) {
129 dev_err(dev, "Unable to convert address %ld\n", da + size - 1);
133 return phys_to_virt(paddr);
137 * stm32_copro_load() - Loadup the STM32 remote processor
138 * @dev: corresponding STM32 remote processor device
139 * @addr: Address in memory where image is stored
140 * @size: Size in bytes of the image
141 * @return 0 if all went ok, else corresponding -ve error
143 static int stm32_copro_load(struct udevice *dev, ulong addr, ulong size)
145 struct stm32_copro_privdata *priv;
146 ulong rsc_table_size;
149 priv = dev_get_priv(dev);
151 ret = stm32_copro_set_hold_boot(dev, true);
155 ret = reset_assert(&priv->reset_ctl);
157 dev_err(dev, "Unable to assert reset line (ret=%d)\n", ret);
161 if (rproc_elf32_load_rsc_table(dev, addr, size, &priv->rsc_table_addr,
163 priv->rsc_table_addr = 0;
164 dev_warn(dev, "No valid resource table for this firmware\n");
167 return rproc_elf32_load_image(dev, addr, size);
171 * stm32_copro_start() - Start the STM32 remote processor
172 * @dev: corresponding STM32 remote processor device
173 * @return 0 if all went ok, else corresponding -ve error
175 static int stm32_copro_start(struct udevice *dev)
177 struct stm32_copro_privdata *priv;
180 priv = dev_get_priv(dev);
182 /* move hold boot from true to false start the copro */
183 ret = stm32_copro_set_hold_boot(dev, false);
188 * Once copro running, reset hold boot flag to avoid copro
189 * rebooting autonomously
191 ret = stm32_copro_set_hold_boot(dev, true);
192 writel(ret ? TAMP_COPRO_STATE_OFF : TAMP_COPRO_STATE_CRUN,
195 /* Store rsc_address in bkp register */
196 writel(priv->rsc_table_addr, TAMP_COPRO_RSC_TBL_ADDRESS);
202 * stm32_copro_reset() - Reset the STM32 remote processor
203 * @dev: corresponding STM32 remote processor device
204 * @return 0 if all went ok, else corresponding -ve error
206 static int stm32_copro_reset(struct udevice *dev)
208 struct stm32_copro_privdata *priv;
211 priv = dev_get_priv(dev);
213 ret = stm32_copro_set_hold_boot(dev, true);
217 ret = reset_assert(&priv->reset_ctl);
219 dev_err(dev, "Unable to assert reset line (ret=%d)\n", ret);
223 writel(TAMP_COPRO_STATE_OFF, TAMP_COPRO_STATE);
229 * stm32_copro_stop() - Stop the STM32 remote processor
230 * @dev: corresponding STM32 remote processor device
231 * @return 0 if all went ok, else corresponding -ve error
233 static int stm32_copro_stop(struct udevice *dev)
235 return stm32_copro_reset(dev);
239 * stm32_copro_is_running() - Is the STM32 remote processor running
240 * @dev: corresponding STM32 remote processor device
241 * @return 0 if the remote processor is running, 1 otherwise
243 static int stm32_copro_is_running(struct udevice *dev)
245 return (readl(TAMP_COPRO_STATE) == TAMP_COPRO_STATE_OFF);
248 static const struct dm_rproc_ops stm32_copro_ops = {
249 .load = stm32_copro_load,
250 .start = stm32_copro_start,
251 .stop = stm32_copro_stop,
252 .reset = stm32_copro_reset,
253 .is_running = stm32_copro_is_running,
254 .device_to_virt = stm32_copro_device_to_virt,
257 static const struct udevice_id stm32_copro_ids[] = {
258 {.compatible = "st,stm32mp1-m4"},
262 U_BOOT_DRIVER(stm32_copro) = {
263 .name = "stm32_m4_proc",
264 .of_match = stm32_copro_ids,
265 .id = UCLASS_REMOTEPROC,
266 .ops = &stm32_copro_ops,
267 .probe = stm32_copro_probe,
268 .priv_auto_alloc_size = sizeof(struct stm32_copro_privdata),