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 <linux/err.h>
17 #define RCC_GCR_HOLD_BOOT 0
18 #define RCC_GCR_RELEASE_BOOT 1
21 * struct stm32_copro_privdata - power processor private data
22 * @reset_ctl: reset controller handle
23 * @hold_boot_regmap: regmap for remote processor reset hold boot
24 * @hold_boot_offset: offset of the register controlling the hold boot setting
25 * @hold_boot_mask: bitmask of the register for the hold boot field
26 * @rsc_table_addr: resource table address
28 struct stm32_copro_privdata {
29 struct reset_ctl reset_ctl;
30 struct regmap *hold_boot_regmap;
31 uint hold_boot_offset;
37 * stm32_copro_probe() - Basic probe
38 * @dev: corresponding STM32 remote processor device
39 * @return 0 if all went ok, else corresponding -ve error
41 static int stm32_copro_probe(struct udevice *dev)
43 struct stm32_copro_privdata *priv;
44 struct regmap *regmap;
48 priv = dev_get_priv(dev);
50 regmap = syscon_regmap_lookup_by_phandle(dev, "st,syscfg-holdboot");
52 dev_err(dev, "unable to find holdboot regmap (%ld)\n",
54 return PTR_ERR(regmap);
57 cell = dev_read_prop(dev, "st,syscfg-holdboot", &len);
58 if (len < 3 * sizeof(fdt32_t)) {
59 dev_err(dev, "holdboot offset and mask not available\n");
63 priv->hold_boot_regmap = regmap;
64 priv->hold_boot_offset = fdtdec_get_number(cell + 1, 1);
65 priv->hold_boot_mask = fdtdec_get_number(cell + 2, 1);
67 ret = reset_get_by_index(dev, 0, &priv->reset_ctl);
69 dev_err(dev, "failed to get reset (%d)\n", ret);
73 dev_dbg(dev, "probed\n");
79 * stm32_copro_set_hold_boot() - Hold boot bit management
80 * @dev: corresponding STM32 remote processor device
81 * @hold: hold boot value
82 * @return 0 if all went ok, else corresponding -ve error
84 static int stm32_copro_set_hold_boot(struct udevice *dev, bool hold)
86 struct stm32_copro_privdata *priv;
90 priv = dev_get_priv(dev);
92 val = hold ? RCC_GCR_HOLD_BOOT : RCC_GCR_RELEASE_BOOT;
95 * Note: shall run an SMC call (STM32_SMC_RCC) if platform is secured.
96 * To be updated when the code for this SMC service is available which
97 * is not the case for the time being.
99 ret = regmap_update_bits(priv->hold_boot_regmap, priv->hold_boot_offset,
100 priv->hold_boot_mask, val);
102 dev_err(dev, "failed to set hold boot\n");
108 * stm32_copro_device_to_virt() - Convert device address to virtual address
109 * @dev: corresponding STM32 remote processor device
110 * @da: device address
111 * @size: Size of the memory region @da is pointing to
112 * @return converted virtual address
114 static void *stm32_copro_device_to_virt(struct udevice *dev, ulong da,
117 fdt32_t in_addr = cpu_to_be32(da), end_addr;
120 paddr = dev_translate_dma_address(dev, &in_addr);
121 if (paddr == OF_BAD_ADDR) {
122 dev_err(dev, "Unable to convert address %ld\n", da);
126 end_addr = cpu_to_be32(da + size - 1);
127 if (dev_translate_dma_address(dev, &end_addr) == OF_BAD_ADDR) {
128 dev_err(dev, "Unable to convert address %ld\n", da + size - 1);
132 return phys_to_virt(paddr);
136 * stm32_copro_load() - Loadup the STM32 remote processor
137 * @dev: corresponding STM32 remote processor device
138 * @addr: Address in memory where image is stored
139 * @size: Size in bytes of the image
140 * @return 0 if all went ok, else corresponding -ve error
142 static int stm32_copro_load(struct udevice *dev, ulong addr, ulong size)
144 struct stm32_copro_privdata *priv;
145 ulong rsc_table_size;
148 priv = dev_get_priv(dev);
150 ret = stm32_copro_set_hold_boot(dev, true);
154 ret = reset_assert(&priv->reset_ctl);
156 dev_err(dev, "Unable to assert reset line (ret=%d)\n", ret);
160 if (rproc_elf32_load_rsc_table(dev, addr, size, &priv->rsc_table_addr,
162 priv->rsc_table_addr = 0;
163 dev_warn(dev, "No valid resource table for this firmware\n");
166 return rproc_elf32_load_image(dev, addr, size);
170 * stm32_copro_start() - Start the STM32 remote processor
171 * @dev: corresponding STM32 remote processor device
172 * @return 0 if all went ok, else corresponding -ve error
174 static int stm32_copro_start(struct udevice *dev)
176 struct stm32_copro_privdata *priv;
179 priv = dev_get_priv(dev);
181 /* move hold boot from true to false start the copro */
182 ret = stm32_copro_set_hold_boot(dev, false);
187 * Once copro running, reset hold boot flag to avoid copro
188 * rebooting autonomously
190 ret = stm32_copro_set_hold_boot(dev, true);
191 writel(ret ? TAMP_COPRO_STATE_OFF : TAMP_COPRO_STATE_CRUN,
194 /* Store rsc_address in bkp register */
195 writel(priv->rsc_table_addr, TAMP_COPRO_RSC_TBL_ADDRESS);
201 * stm32_copro_reset() - Reset the STM32 remote processor
202 * @dev: corresponding STM32 remote processor device
203 * @return 0 if all went ok, else corresponding -ve error
205 static int stm32_copro_reset(struct udevice *dev)
207 struct stm32_copro_privdata *priv;
210 priv = dev_get_priv(dev);
212 ret = stm32_copro_set_hold_boot(dev, true);
216 ret = reset_assert(&priv->reset_ctl);
218 dev_err(dev, "Unable to assert reset line (ret=%d)\n", ret);
222 writel(TAMP_COPRO_STATE_OFF, TAMP_COPRO_STATE);
228 * stm32_copro_stop() - Stop the STM32 remote processor
229 * @dev: corresponding STM32 remote processor device
230 * @return 0 if all went ok, else corresponding -ve error
232 static int stm32_copro_stop(struct udevice *dev)
234 return stm32_copro_reset(dev);
238 * stm32_copro_is_running() - Is the STM32 remote processor running
239 * @dev: corresponding STM32 remote processor device
240 * @return 0 if the remote processor is running, 1 otherwise
242 static int stm32_copro_is_running(struct udevice *dev)
244 return (readl(TAMP_COPRO_STATE) == TAMP_COPRO_STATE_OFF);
247 static const struct dm_rproc_ops stm32_copro_ops = {
248 .load = stm32_copro_load,
249 .start = stm32_copro_start,
250 .stop = stm32_copro_stop,
251 .reset = stm32_copro_reset,
252 .is_running = stm32_copro_is_running,
253 .device_to_virt = stm32_copro_device_to_virt,
256 static const struct udevice_id stm32_copro_ids[] = {
257 {.compatible = "st,stm32mp1-m4"},
261 U_BOOT_DRIVER(stm32_copro) = {
262 .name = "stm32_m4_proc",
263 .of_match = stm32_copro_ids,
264 .id = UCLASS_REMOTEPROC,
265 .ops = &stm32_copro_ops,
266 .probe = stm32_copro_probe,
267 .priv_auto_alloc_size = sizeof(struct stm32_copro_privdata),