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__
12 #include <remoteproc.h>
16 #include <dm/device_compat.h>
17 #include <linux/err.h>
19 #define RCC_GCR_HOLD_BOOT 0
20 #define RCC_GCR_RELEASE_BOOT 1
23 * struct stm32_copro_privdata - power processor private data
24 * @reset_ctl: reset controller handle
25 * @hold_boot_regmap: regmap for remote processor reset hold boot
26 * @hold_boot_offset: offset of the register controlling the hold boot setting
27 * @hold_boot_mask: bitmask of the register for the hold boot field
28 * @rsc_table_addr: resource table address
30 struct stm32_copro_privdata {
31 struct reset_ctl reset_ctl;
32 struct regmap *hold_boot_regmap;
33 uint hold_boot_offset;
39 * stm32_copro_probe() - Basic probe
40 * @dev: corresponding STM32 remote processor device
41 * @return 0 if all went ok, else corresponding -ve error
43 static int stm32_copro_probe(struct udevice *dev)
45 struct stm32_copro_privdata *priv;
46 struct regmap *regmap;
50 priv = dev_get_priv(dev);
52 regmap = syscon_regmap_lookup_by_phandle(dev, "st,syscfg-holdboot");
54 dev_err(dev, "unable to find holdboot regmap (%ld)\n",
56 return PTR_ERR(regmap);
59 cell = dev_read_prop(dev, "st,syscfg-holdboot", &len);
60 if (len < 3 * sizeof(fdt32_t)) {
61 dev_err(dev, "holdboot offset and mask not available\n");
65 priv->hold_boot_regmap = regmap;
66 priv->hold_boot_offset = fdtdec_get_number(cell + 1, 1);
67 priv->hold_boot_mask = fdtdec_get_number(cell + 2, 1);
69 ret = reset_get_by_index(dev, 0, &priv->reset_ctl);
71 dev_err(dev, "failed to get reset (%d)\n", ret);
75 dev_dbg(dev, "probed\n");
81 * stm32_copro_set_hold_boot() - Hold boot bit management
82 * @dev: corresponding STM32 remote processor device
83 * @hold: hold boot value
84 * @return 0 if all went ok, else corresponding -ve error
86 static int stm32_copro_set_hold_boot(struct udevice *dev, bool hold)
88 struct stm32_copro_privdata *priv;
92 priv = dev_get_priv(dev);
94 val = hold ? RCC_GCR_HOLD_BOOT : RCC_GCR_RELEASE_BOOT;
97 * Note: shall run an SMC call (STM32_SMC_RCC) if platform is secured.
98 * To be updated when the code for this SMC service is available which
99 * is not the case for the time being.
101 ret = regmap_update_bits(priv->hold_boot_regmap, priv->hold_boot_offset,
102 priv->hold_boot_mask, val);
104 dev_err(dev, "failed to set hold boot\n");
110 * stm32_copro_device_to_virt() - Convert device address to virtual address
111 * @dev: corresponding STM32 remote processor device
112 * @da: device address
113 * @size: Size of the memory region @da is pointing to
114 * @return converted virtual address
116 static void *stm32_copro_device_to_virt(struct udevice *dev, ulong da,
119 fdt32_t in_addr = cpu_to_be32(da), end_addr;
122 paddr = dev_translate_dma_address(dev, &in_addr);
123 if (paddr == OF_BAD_ADDR) {
124 dev_err(dev, "Unable to convert address %ld\n", da);
128 end_addr = cpu_to_be32(da + size - 1);
129 if (dev_translate_dma_address(dev, &end_addr) == OF_BAD_ADDR) {
130 dev_err(dev, "Unable to convert address %ld\n", da + size - 1);
134 return phys_to_virt(paddr);
138 * stm32_copro_load() - Loadup the STM32 remote processor
139 * @dev: corresponding STM32 remote processor device
140 * @addr: Address in memory where image is stored
141 * @size: Size in bytes of the image
142 * @return 0 if all went ok, else corresponding -ve error
144 static int stm32_copro_load(struct udevice *dev, ulong addr, ulong size)
146 struct stm32_copro_privdata *priv;
147 ulong rsc_table_size;
150 priv = dev_get_priv(dev);
152 ret = stm32_copro_set_hold_boot(dev, true);
156 ret = reset_assert(&priv->reset_ctl);
158 dev_err(dev, "Unable to assert reset line (ret=%d)\n", ret);
162 if (rproc_elf32_load_rsc_table(dev, addr, size, &priv->rsc_table_addr,
164 priv->rsc_table_addr = 0;
165 dev_warn(dev, "No valid resource table for this firmware\n");
168 return rproc_elf32_load_image(dev, addr, size);
172 * stm32_copro_start() - Start the STM32 remote processor
173 * @dev: corresponding STM32 remote processor device
174 * @return 0 if all went ok, else corresponding -ve error
176 static int stm32_copro_start(struct udevice *dev)
178 struct stm32_copro_privdata *priv;
181 priv = dev_get_priv(dev);
183 /* move hold boot from true to false start the copro */
184 ret = stm32_copro_set_hold_boot(dev, false);
189 * Once copro running, reset hold boot flag to avoid copro
190 * rebooting autonomously
192 ret = stm32_copro_set_hold_boot(dev, true);
193 writel(ret ? TAMP_COPRO_STATE_OFF : TAMP_COPRO_STATE_CRUN,
196 /* Store rsc_address in bkp register */
197 writel(priv->rsc_table_addr, TAMP_COPRO_RSC_TBL_ADDRESS);
203 * stm32_copro_reset() - Reset the STM32 remote processor
204 * @dev: corresponding STM32 remote processor device
205 * @return 0 if all went ok, else corresponding -ve error
207 static int stm32_copro_reset(struct udevice *dev)
209 struct stm32_copro_privdata *priv;
212 priv = dev_get_priv(dev);
214 ret = stm32_copro_set_hold_boot(dev, true);
218 ret = reset_assert(&priv->reset_ctl);
220 dev_err(dev, "Unable to assert reset line (ret=%d)\n", ret);
224 writel(TAMP_COPRO_STATE_OFF, TAMP_COPRO_STATE);
230 * stm32_copro_stop() - Stop the STM32 remote processor
231 * @dev: corresponding STM32 remote processor device
232 * @return 0 if all went ok, else corresponding -ve error
234 static int stm32_copro_stop(struct udevice *dev)
236 return stm32_copro_reset(dev);
240 * stm32_copro_is_running() - Is the STM32 remote processor running
241 * @dev: corresponding STM32 remote processor device
242 * @return 0 if the remote processor is running, 1 otherwise
244 static int stm32_copro_is_running(struct udevice *dev)
246 return (readl(TAMP_COPRO_STATE) == TAMP_COPRO_STATE_OFF);
249 static const struct dm_rproc_ops stm32_copro_ops = {
250 .load = stm32_copro_load,
251 .start = stm32_copro_start,
252 .stop = stm32_copro_stop,
253 .reset = stm32_copro_reset,
254 .is_running = stm32_copro_is_running,
255 .device_to_virt = stm32_copro_device_to_virt,
258 static const struct udevice_id stm32_copro_ids[] = {
259 {.compatible = "st,stm32mp1-m4"},
263 U_BOOT_DRIVER(stm32_copro) = {
264 .name = "stm32_m4_proc",
265 .of_match = stm32_copro_ids,
266 .id = UCLASS_REMOTEPROC,
267 .ops = &stm32_copro_ops,
268 .probe = stm32_copro_probe,
269 .priv_auto_alloc_size = sizeof(struct stm32_copro_privdata),