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>
14 #include <dm/device_compat.h>
15 #include <linux/err.h>
18 * struct stm32_copro_privdata - power processor private data
19 * @reset_ctl: reset controller handle
20 * @hold_boot: hold boot controller handle
21 * @rsc_table_addr: resource table address
23 struct stm32_copro_privdata {
24 struct reset_ctl reset_ctl;
25 struct reset_ctl hold_boot;
30 * stm32_copro_probe() - Basic probe
31 * @dev: corresponding STM32 remote processor device
32 * @return 0 if all went ok, else corresponding -ve error
34 static int stm32_copro_probe(struct udevice *dev)
36 struct stm32_copro_privdata *priv;
39 priv = dev_get_priv(dev);
41 ret = reset_get_by_name(dev, "mcu_rst", &priv->reset_ctl);
43 dev_err(dev, "failed to get reset (%d)\n", ret);
47 ret = reset_get_by_name(dev, "hold_boot", &priv->hold_boot);
49 dev_err(dev, "failed to get hold boot (%d)\n", ret);
53 dev_dbg(dev, "probed\n");
59 * stm32_copro_device_to_virt() - Convert device address to virtual address
60 * @dev: corresponding STM32 remote processor device
62 * @size: Size of the memory region @da is pointing to
63 * @return converted virtual address
65 static void *stm32_copro_device_to_virt(struct udevice *dev, ulong da,
68 fdt32_t in_addr = cpu_to_be32(da), end_addr;
71 paddr = dev_translate_dma_address(dev, &in_addr);
72 if (paddr == OF_BAD_ADDR) {
73 dev_err(dev, "Unable to convert address %ld\n", da);
77 end_addr = cpu_to_be32(da + size - 1);
78 if (dev_translate_dma_address(dev, &end_addr) == OF_BAD_ADDR) {
79 dev_err(dev, "Unable to convert address %ld\n", da + size - 1);
83 return phys_to_virt(paddr);
87 * stm32_copro_load() - Loadup the STM32 remote processor
88 * @dev: corresponding STM32 remote processor device
89 * @addr: Address in memory where image is stored
90 * @size: Size in bytes of the image
91 * @return 0 if all went ok, else corresponding -ve error
93 static int stm32_copro_load(struct udevice *dev, ulong addr, ulong size)
95 struct stm32_copro_privdata *priv;
99 priv = dev_get_priv(dev);
101 ret = reset_assert(&priv->hold_boot);
103 dev_err(dev, "Unable to assert hold boot (ret=%d)\n", ret);
107 ret = reset_assert(&priv->reset_ctl);
109 dev_err(dev, "Unable to assert reset line (ret=%d)\n", ret);
113 if (rproc_elf32_load_rsc_table(dev, addr, size, &priv->rsc_table_addr,
115 priv->rsc_table_addr = 0;
116 dev_warn(dev, "No valid resource table for this firmware\n");
119 return rproc_elf32_load_image(dev, addr, size);
123 * stm32_copro_start() - Start the STM32 remote processor
124 * @dev: corresponding STM32 remote processor device
125 * @return 0 if all went ok, else corresponding -ve error
127 static int stm32_copro_start(struct udevice *dev)
129 struct stm32_copro_privdata *priv;
132 priv = dev_get_priv(dev);
134 ret = reset_deassert(&priv->hold_boot);
136 dev_err(dev, "Unable to deassert hold boot (ret=%d)\n", ret);
141 * Once copro running, reset hold boot flag to avoid copro
142 * rebooting autonomously (error should never occur)
144 ret = reset_assert(&priv->hold_boot);
146 dev_err(dev, "Unable to assert hold boot (ret=%d)\n", ret);
148 /* indicates that copro is running */
149 writel(TAMP_COPRO_STATE_CRUN, TAMP_COPRO_STATE);
150 /* Store rsc_address in bkp register */
151 writel(priv->rsc_table_addr, TAMP_COPRO_RSC_TBL_ADDRESS);
157 * stm32_copro_reset() - Reset the STM32 remote processor
158 * @dev: corresponding STM32 remote processor device
159 * @return 0 if all went ok, else corresponding -ve error
161 static int stm32_copro_reset(struct udevice *dev)
163 struct stm32_copro_privdata *priv;
166 priv = dev_get_priv(dev);
168 ret = reset_assert(&priv->hold_boot);
170 dev_err(dev, "Unable to assert hold boot (ret=%d)\n", ret);
174 ret = reset_assert(&priv->reset_ctl);
176 dev_err(dev, "Unable to assert reset line (ret=%d)\n", ret);
180 writel(TAMP_COPRO_STATE_OFF, TAMP_COPRO_STATE);
186 * stm32_copro_stop() - Stop the STM32 remote processor
187 * @dev: corresponding STM32 remote processor device
188 * @return 0 if all went ok, else corresponding -ve error
190 static int stm32_copro_stop(struct udevice *dev)
192 return stm32_copro_reset(dev);
196 * stm32_copro_is_running() - Is the STM32 remote processor running
197 * @dev: corresponding STM32 remote processor device
198 * @return 0 if the remote processor is running, 1 otherwise
200 static int stm32_copro_is_running(struct udevice *dev)
202 return (readl(TAMP_COPRO_STATE) == TAMP_COPRO_STATE_OFF);
205 static const struct dm_rproc_ops stm32_copro_ops = {
206 .load = stm32_copro_load,
207 .start = stm32_copro_start,
208 .stop = stm32_copro_stop,
209 .reset = stm32_copro_reset,
210 .is_running = stm32_copro_is_running,
211 .device_to_virt = stm32_copro_device_to_virt,
214 static const struct udevice_id stm32_copro_ids[] = {
215 {.compatible = "st,stm32mp1-m4"},
219 U_BOOT_DRIVER(stm32_copro) = {
220 .name = "stm32_m4_proc",
221 .of_match = stm32_copro_ids,
222 .id = UCLASS_REMOTEPROC,
223 .ops = &stm32_copro_ops,
224 .probe = stm32_copro_probe,
225 .priv_auto_alloc_size = sizeof(struct stm32_copro_privdata),