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
26 struct stm32_copro_privdata {
27 struct reset_ctl reset_ctl;
28 struct regmap *hold_boot_regmap;
29 uint hold_boot_offset;
34 * stm32_copro_probe() - Basic probe
35 * @dev: corresponding STM32 remote processor device
36 * @return 0 if all went ok, else corresponding -ve error
38 static int stm32_copro_probe(struct udevice *dev)
40 struct stm32_copro_privdata *priv;
41 struct regmap *regmap;
45 priv = dev_get_priv(dev);
47 regmap = syscon_regmap_lookup_by_phandle(dev, "st,syscfg-holdboot");
49 dev_err(dev, "unable to find holdboot regmap (%ld)\n",
51 return PTR_ERR(regmap);
54 cell = dev_read_prop(dev, "st,syscfg-holdboot", &len);
55 if (len < 3 * sizeof(fdt32_t)) {
56 dev_err(dev, "holdboot offset and mask not available\n");
60 priv->hold_boot_regmap = regmap;
61 priv->hold_boot_offset = fdtdec_get_number(cell + 1, 1);
62 priv->hold_boot_mask = fdtdec_get_number(cell + 2, 1);
64 ret = reset_get_by_index(dev, 0, &priv->reset_ctl);
66 dev_err(dev, "failed to get reset (%d)\n", ret);
70 dev_dbg(dev, "probed\n");
76 * stm32_copro_set_hold_boot() - Hold boot bit management
77 * @dev: corresponding STM32 remote processor device
78 * @hold: hold boot value
79 * @return 0 if all went ok, else corresponding -ve error
81 static int stm32_copro_set_hold_boot(struct udevice *dev, bool hold)
83 struct stm32_copro_privdata *priv;
87 priv = dev_get_priv(dev);
89 val = hold ? RCC_GCR_HOLD_BOOT : RCC_GCR_RELEASE_BOOT;
92 * Note: shall run an SMC call (STM32_SMC_RCC) if platform is secured.
93 * To be updated when the code for this SMC service is available which
94 * is not the case for the time being.
96 ret = regmap_update_bits(priv->hold_boot_regmap, priv->hold_boot_offset,
97 priv->hold_boot_mask, val);
99 dev_err(dev, "failed to set hold boot\n");
105 * stm32_copro_device_to_virt() - Convert device address to virtual address
106 * @dev: corresponding STM32 remote processor device
107 * @da: device address
108 * @size: Size of the memory region @da is pointing to
109 * @return converted virtual address
111 static void *stm32_copro_device_to_virt(struct udevice *dev, ulong da,
114 fdt32_t in_addr = cpu_to_be32(da), end_addr;
117 paddr = dev_translate_dma_address(dev, &in_addr);
118 if (paddr == OF_BAD_ADDR) {
119 dev_err(dev, "Unable to convert address %ld\n", da);
123 end_addr = cpu_to_be32(da + size - 1);
124 if (dev_translate_dma_address(dev, &end_addr) == OF_BAD_ADDR) {
125 dev_err(dev, "Unable to convert address %ld\n", da + size - 1);
129 return phys_to_virt(paddr);
133 * stm32_copro_load() - Loadup the STM32 remote processor
134 * @dev: corresponding STM32 remote processor device
135 * @addr: Address in memory where image is stored
136 * @size: Size in bytes of the image
137 * @return 0 if all went ok, else corresponding -ve error
139 static int stm32_copro_load(struct udevice *dev, ulong addr, ulong size)
141 struct stm32_copro_privdata *priv;
144 priv = dev_get_priv(dev);
146 ret = stm32_copro_set_hold_boot(dev, true);
150 ret = reset_assert(&priv->reset_ctl);
152 dev_err(dev, "Unable to assert reset line (ret=%d)\n", ret);
156 return rproc_elf32_load_image(dev, addr, size);
160 * stm32_copro_start() - Start the STM32 remote processor
161 * @dev: corresponding STM32 remote processor device
162 * @return 0 if all went ok, else corresponding -ve error
164 static int stm32_copro_start(struct udevice *dev)
168 /* move hold boot from true to false start the copro */
169 ret = stm32_copro_set_hold_boot(dev, false);
174 * Once copro running, reset hold boot flag to avoid copro
175 * rebooting autonomously
177 ret = stm32_copro_set_hold_boot(dev, true);
178 writel(ret ? TAMP_COPRO_STATE_OFF : TAMP_COPRO_STATE_CRUN,
184 * stm32_copro_reset() - Reset the STM32 remote processor
185 * @dev: corresponding STM32 remote processor device
186 * @return 0 if all went ok, else corresponding -ve error
188 static int stm32_copro_reset(struct udevice *dev)
190 struct stm32_copro_privdata *priv;
193 priv = dev_get_priv(dev);
195 ret = stm32_copro_set_hold_boot(dev, true);
199 ret = reset_assert(&priv->reset_ctl);
201 dev_err(dev, "Unable to assert reset line (ret=%d)\n", ret);
205 writel(TAMP_COPRO_STATE_OFF, TAMP_COPRO_STATE);
211 * stm32_copro_stop() - Stop the STM32 remote processor
212 * @dev: corresponding STM32 remote processor device
213 * @return 0 if all went ok, else corresponding -ve error
215 static int stm32_copro_stop(struct udevice *dev)
217 return stm32_copro_reset(dev);
221 * stm32_copro_is_running() - Is the STM32 remote processor running
222 * @dev: corresponding STM32 remote processor device
223 * @return 0 if the remote processor is running, 1 otherwise
225 static int stm32_copro_is_running(struct udevice *dev)
227 return (readl(TAMP_COPRO_STATE) == TAMP_COPRO_STATE_OFF);
230 static const struct dm_rproc_ops stm32_copro_ops = {
231 .load = stm32_copro_load,
232 .start = stm32_copro_start,
233 .stop = stm32_copro_stop,
234 .reset = stm32_copro_reset,
235 .is_running = stm32_copro_is_running,
236 .device_to_virt = stm32_copro_device_to_virt,
239 static const struct udevice_id stm32_copro_ids[] = {
240 {.compatible = "st,stm32mp1-m4"},
244 U_BOOT_DRIVER(stm32_copro) = {
245 .name = "stm32_m4_proc",
246 .of_match = stm32_copro_ids,
247 .id = UCLASS_REMOTEPROC,
248 .ops = &stm32_copro_ops,
249 .probe = stm32_copro_probe,
250 .priv_auto_alloc_size = sizeof(struct stm32_copro_privdata),