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>
16 #define RCC_GCR_HOLD_BOOT 0
17 #define RCC_GCR_RELEASE_BOOT 1
20 * struct stm32_copro_privdata - power processor private data
21 * @reset_ctl: reset controller handle
22 * @hold_boot_regmap: regmap for remote processor reset hold boot
23 * @hold_boot_offset: offset of the register controlling the hold boot setting
24 * @hold_boot_mask: bitmask of the register for the hold boot field
25 * @is_running: is the remote processor running
27 struct stm32_copro_privdata {
28 struct reset_ctl reset_ctl;
29 struct regmap *hold_boot_regmap;
30 uint hold_boot_offset;
36 * stm32_copro_probe() - Basic probe
37 * @dev: corresponding STM32 remote processor device
38 * @return 0 if all went ok, else corresponding -ve error
40 static int stm32_copro_probe(struct udevice *dev)
42 struct stm32_copro_privdata *priv;
43 struct regmap *regmap;
47 priv = dev_get_priv(dev);
49 regmap = syscon_regmap_lookup_by_phandle(dev, "st,syscfg-holdboot");
51 dev_err(dev, "unable to find holdboot regmap (%ld)\n",
53 return PTR_ERR(regmap);
56 cell = dev_read_prop(dev, "st,syscfg-holdboot", &len);
57 if (len < 3 * sizeof(fdt32_t)) {
58 dev_err(dev, "holdboot offset and mask not available\n");
62 priv->hold_boot_regmap = regmap;
63 priv->hold_boot_offset = fdtdec_get_number(cell + 1, 1);
64 priv->hold_boot_mask = fdtdec_get_number(cell + 2, 1);
66 ret = reset_get_by_index(dev, 0, &priv->reset_ctl);
68 dev_err(dev, "failed to get reset (%d)\n", ret);
72 dev_dbg(dev, "probed\n");
78 * stm32_copro_set_hold_boot() - Hold boot bit management
79 * @dev: corresponding STM32 remote processor device
80 * @hold: hold boot value
81 * @return 0 if all went ok, else corresponding -ve error
83 static int stm32_copro_set_hold_boot(struct udevice *dev, bool hold)
85 struct stm32_copro_privdata *priv;
89 priv = dev_get_priv(dev);
91 val = hold ? RCC_GCR_HOLD_BOOT : RCC_GCR_RELEASE_BOOT;
94 * Note: shall run an SMC call (STM32_SMC_RCC) if platform is secured.
95 * To be updated when the code for this SMC service is available which
96 * is not the case for the time being.
98 ret = regmap_update_bits(priv->hold_boot_regmap, priv->hold_boot_offset,
99 priv->hold_boot_mask, val);
101 dev_err(dev, "failed to set hold boot\n");
107 * stm32_copro_device_to_virt() - Convert device address to virtual address
108 * @dev: corresponding STM32 remote processor device
109 * @da: device address
110 * @size: Size of the memory region @da is pointing to
111 * @return converted virtual address
113 static void *stm32_copro_device_to_virt(struct udevice *dev, ulong da,
116 fdt32_t in_addr = cpu_to_be32(da), end_addr;
119 paddr = dev_translate_dma_address(dev, &in_addr);
120 if (paddr == OF_BAD_ADDR) {
121 dev_err(dev, "Unable to convert address %ld\n", da);
125 end_addr = cpu_to_be32(da + size - 1);
126 if (dev_translate_dma_address(dev, &end_addr) == OF_BAD_ADDR) {
127 dev_err(dev, "Unable to convert address %ld\n", da + size - 1);
131 return phys_to_virt(paddr);
135 * stm32_copro_load() - Loadup the STM32 remote processor
136 * @dev: corresponding STM32 remote processor device
137 * @addr: Address in memory where image is stored
138 * @size: Size in bytes of the image
139 * @return 0 if all went ok, else corresponding -ve error
141 static int stm32_copro_load(struct udevice *dev, ulong addr, ulong size)
143 struct stm32_copro_privdata *priv;
146 priv = dev_get_priv(dev);
148 ret = stm32_copro_set_hold_boot(dev, true);
152 ret = reset_assert(&priv->reset_ctl);
154 dev_err(dev, "Unable to assert reset line (ret=%d)\n", ret);
158 /* Support only ELF32 image */
159 ret = rproc_elf32_sanity_check(addr, size);
161 dev_err(dev, "Invalid ELF32 image (%d)\n", ret);
165 return rproc_elf32_load_image(dev, addr);
169 * stm32_copro_start() - Start the STM32 remote processor
170 * @dev: corresponding STM32 remote processor device
171 * @return 0 if all went ok, else corresponding -ve error
173 static int stm32_copro_start(struct udevice *dev)
175 struct stm32_copro_privdata *priv;
178 priv = dev_get_priv(dev);
180 /* move hold boot from true to false start the copro */
181 ret = stm32_copro_set_hold_boot(dev, false);
186 * Once copro running, reset hold boot flag to avoid copro
187 * rebooting autonomously
189 ret = stm32_copro_set_hold_boot(dev, true);
190 priv->is_running = !ret;
195 * stm32_copro_reset() - Reset the STM32 remote processor
196 * @dev: corresponding STM32 remote processor device
197 * @return 0 if all went ok, else corresponding -ve error
199 static int stm32_copro_reset(struct udevice *dev)
201 struct stm32_copro_privdata *priv;
204 priv = dev_get_priv(dev);
206 ret = stm32_copro_set_hold_boot(dev, true);
210 ret = reset_assert(&priv->reset_ctl);
212 dev_err(dev, "Unable to assert reset line (ret=%d)\n", ret);
216 priv->is_running = false;
222 * stm32_copro_stop() - Stop the STM32 remote processor
223 * @dev: corresponding STM32 remote processor device
224 * @return 0 if all went ok, else corresponding -ve error
226 static int stm32_copro_stop(struct udevice *dev)
228 return stm32_copro_reset(dev);
232 * stm32_copro_is_running() - Is the STM32 remote processor running
233 * @dev: corresponding STM32 remote processor device
234 * @return 1 if the remote processor is running, 0 otherwise
236 static int stm32_copro_is_running(struct udevice *dev)
238 struct stm32_copro_privdata *priv;
240 priv = dev_get_priv(dev);
241 return priv->is_running;
244 static const struct dm_rproc_ops stm32_copro_ops = {
245 .load = stm32_copro_load,
246 .start = stm32_copro_start,
247 .stop = stm32_copro_stop,
248 .reset = stm32_copro_reset,
249 .is_running = stm32_copro_is_running,
250 .device_to_virt = stm32_copro_device_to_virt,
253 static const struct udevice_id stm32_copro_ids[] = {
254 {.compatible = "st,stm32mp1-m4"},
258 U_BOOT_DRIVER(stm32_copro) = {
259 .name = "stm32_m4_proc",
260 .of_match = stm32_copro_ids,
261 .id = UCLASS_REMOTEPROC,
262 .ops = &stm32_copro_ops,
263 .probe = stm32_copro_probe,
264 .priv_auto_alloc_size = sizeof(struct stm32_copro_privdata),