1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
3 * Copyright (C) 2019, STMicroelectronics - All Rights Reserved
5 #define LOG_CATEGORY UCLASS_REMOTEPROC
12 #include <remoteproc.h>
15 #include <dm/device_compat.h>
16 #include <linux/err.h>
19 * struct stm32_copro_privdata - power processor private data
20 * @reset_ctl: reset controller handle
21 * @hold_boot: hold boot controller handle
22 * @rsc_table_addr: resource table address
24 struct stm32_copro_privdata {
25 struct reset_ctl reset_ctl;
26 struct reset_ctl hold_boot;
31 * stm32_copro_probe() - Basic probe
32 * @dev: corresponding STM32 remote processor device
33 * Return: 0 if all went ok, else corresponding -ve error
35 static int stm32_copro_probe(struct udevice *dev)
37 struct stm32_copro_privdata *priv;
40 priv = dev_get_priv(dev);
42 ret = reset_get_by_name(dev, "mcu_rst", &priv->reset_ctl);
44 dev_err(dev, "failed to get reset (%d)\n", ret);
48 ret = reset_get_by_name(dev, "hold_boot", &priv->hold_boot);
50 dev_err(dev, "failed to get hold boot (%d)\n", ret);
54 dev_dbg(dev, "probed\n");
60 * stm32_copro_device_to_virt() - Convert device address to virtual address
61 * @dev: corresponding STM32 remote processor device
63 * @size: Size of the memory region @da is pointing to
64 * Return: converted virtual address
66 static void *stm32_copro_device_to_virt(struct udevice *dev, ulong da,
69 fdt32_t in_addr = cpu_to_be32(da), end_addr;
72 paddr = dev_translate_dma_address(dev, &in_addr);
73 if (paddr == OF_BAD_ADDR) {
74 dev_err(dev, "Unable to convert address %ld\n", da);
78 end_addr = cpu_to_be32(da + size - 1);
79 if (dev_translate_dma_address(dev, &end_addr) == OF_BAD_ADDR) {
80 dev_err(dev, "Unable to convert address %ld\n", da + size - 1);
84 return phys_to_virt(paddr);
88 * stm32_copro_load() - Loadup the STM32 remote processor
89 * @dev: corresponding STM32 remote processor device
90 * @addr: Address in memory where image is stored
91 * @size: Size in bytes of the image
92 * Return: 0 if all went ok, else corresponding -ve error
94 static int stm32_copro_load(struct udevice *dev, ulong addr, ulong size)
96 struct stm32_copro_privdata *priv;
100 priv = dev_get_priv(dev);
102 ret = reset_assert(&priv->hold_boot);
104 dev_err(dev, "Unable to assert hold boot (ret=%d)\n", ret);
108 ret = reset_assert(&priv->reset_ctl);
110 dev_err(dev, "Unable to assert reset line (ret=%d)\n", ret);
114 if (rproc_elf32_load_rsc_table(dev, addr, size, &priv->rsc_table_addr,
116 priv->rsc_table_addr = 0;
117 dev_warn(dev, "No valid resource table for this firmware\n");
120 return rproc_elf32_load_image(dev, addr, size);
124 * stm32_copro_start() - Start the STM32 remote processor
125 * @dev: corresponding STM32 remote processor device
126 * Return: 0 if all went ok, else corresponding -ve error
128 static int stm32_copro_start(struct udevice *dev)
130 struct stm32_copro_privdata *priv;
133 priv = dev_get_priv(dev);
135 ret = reset_deassert(&priv->hold_boot);
137 dev_err(dev, "Unable to deassert hold boot (ret=%d)\n", ret);
142 * Once copro running, reset hold boot flag to avoid copro
143 * rebooting autonomously (error should never occur)
145 ret = reset_assert(&priv->hold_boot);
147 dev_err(dev, "Unable to assert hold boot (ret=%d)\n", ret);
149 /* indicates that copro is running */
150 writel(TAMP_COPRO_STATE_CRUN, TAMP_COPRO_STATE);
151 /* Store rsc_address in bkp register */
152 writel(priv->rsc_table_addr, TAMP_COPRO_RSC_TBL_ADDRESS);
158 * stm32_copro_reset() - Reset the STM32 remote processor
159 * @dev: corresponding STM32 remote processor device
160 * Return: 0 if all went ok, else corresponding -ve error
162 static int stm32_copro_reset(struct udevice *dev)
164 struct stm32_copro_privdata *priv;
167 priv = dev_get_priv(dev);
169 ret = reset_assert(&priv->hold_boot);
171 dev_err(dev, "Unable to assert hold boot (ret=%d)\n", ret);
175 ret = reset_assert(&priv->reset_ctl);
177 dev_err(dev, "Unable to assert reset line (ret=%d)\n", ret);
181 writel(TAMP_COPRO_STATE_OFF, TAMP_COPRO_STATE);
187 * stm32_copro_stop() - Stop the STM32 remote processor
188 * @dev: corresponding STM32 remote processor device
189 * Return: 0 if all went ok, else corresponding -ve error
191 static int stm32_copro_stop(struct udevice *dev)
193 return stm32_copro_reset(dev);
197 * stm32_copro_is_running() - Is the STM32 remote processor running
198 * @dev: corresponding STM32 remote processor device
199 * Return: 0 if the remote processor is running, 1 otherwise
201 static int stm32_copro_is_running(struct udevice *dev)
203 return (readl(TAMP_COPRO_STATE) == TAMP_COPRO_STATE_OFF);
206 static const struct dm_rproc_ops stm32_copro_ops = {
207 .load = stm32_copro_load,
208 .start = stm32_copro_start,
209 .stop = stm32_copro_stop,
210 .reset = stm32_copro_reset,
211 .is_running = stm32_copro_is_running,
212 .device_to_virt = stm32_copro_device_to_virt,
215 static const struct udevice_id stm32_copro_ids[] = {
216 {.compatible = "st,stm32mp1-m4"},
220 U_BOOT_DRIVER(stm32_copro) = {
221 .name = "stm32_m4_proc",
222 .of_match = stm32_copro_ids,
223 .id = UCLASS_REMOTEPROC,
224 .ops = &stm32_copro_ops,
225 .probe = stm32_copro_probe,
226 .priv_auto = sizeof(struct stm32_copro_privdata),