Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause |
| 2 | /* |
| 3 | * Copyright (C) 2019, STMicroelectronics - All Rights Reserved |
| 4 | */ |
Patrick Delaunay | c886cd6 | 2020-11-06 19:01:34 +0100 | [diff] [blame] | 5 | #define LOG_CATEGORY UCLASS_REMOTEPROC |
| 6 | |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <errno.h> |
| 10 | #include <fdtdec.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 12 | #include <remoteproc.h> |
| 13 | #include <reset.h> |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 14 | #include <asm/io.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 15 | #include <dm/device_compat.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 16 | #include <linux/err.h> |
Simon Glass | 1e94b46 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 17 | #include <linux/printk.h> |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 18 | |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 19 | /** |
| 20 | * struct stm32_copro_privdata - power processor private data |
| 21 | * @reset_ctl: reset controller handle |
Patrick Delaunay | 5a536df | 2020-10-15 15:01:12 +0200 | [diff] [blame] | 22 | * @hold_boot: hold boot controller handle |
Fabien Dessenne | 33fd419 | 2019-10-30 14:38:33 +0100 | [diff] [blame] | 23 | * @rsc_table_addr: resource table address |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 24 | */ |
| 25 | struct stm32_copro_privdata { |
| 26 | struct reset_ctl reset_ctl; |
Patrick Delaunay | 5a536df | 2020-10-15 15:01:12 +0200 | [diff] [blame] | 27 | struct reset_ctl hold_boot; |
Fabien Dessenne | 33fd419 | 2019-10-30 14:38:33 +0100 | [diff] [blame] | 28 | ulong rsc_table_addr; |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | /** |
| 32 | * stm32_copro_probe() - Basic probe |
| 33 | * @dev: corresponding STM32 remote processor device |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 34 | * Return: 0 if all went ok, else corresponding -ve error |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 35 | */ |
| 36 | static int stm32_copro_probe(struct udevice *dev) |
| 37 | { |
| 38 | struct stm32_copro_privdata *priv; |
Patrick Delaunay | 5a536df | 2020-10-15 15:01:12 +0200 | [diff] [blame] | 39 | int ret; |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 40 | |
| 41 | priv = dev_get_priv(dev); |
| 42 | |
Patrick Delaunay | 5a536df | 2020-10-15 15:01:12 +0200 | [diff] [blame] | 43 | ret = reset_get_by_name(dev, "mcu_rst", &priv->reset_ctl); |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 44 | if (ret) { |
| 45 | dev_err(dev, "failed to get reset (%d)\n", ret); |
| 46 | return ret; |
| 47 | } |
| 48 | |
Patrick Delaunay | 5a536df | 2020-10-15 15:01:12 +0200 | [diff] [blame] | 49 | ret = reset_get_by_name(dev, "hold_boot", &priv->hold_boot); |
| 50 | if (ret) { |
| 51 | dev_err(dev, "failed to get hold boot (%d)\n", ret); |
| 52 | return ret; |
| 53 | } |
| 54 | |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 55 | dev_dbg(dev, "probed\n"); |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | /** |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 61 | * stm32_copro_device_to_virt() - Convert device address to virtual address |
| 62 | * @dev: corresponding STM32 remote processor device |
| 63 | * @da: device address |
Lokesh Vutla | c08eb93 | 2019-09-04 16:01:27 +0530 | [diff] [blame] | 64 | * @size: Size of the memory region @da is pointing to |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 65 | * Return: converted virtual address |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 66 | */ |
Lokesh Vutla | c08eb93 | 2019-09-04 16:01:27 +0530 | [diff] [blame] | 67 | static void *stm32_copro_device_to_virt(struct udevice *dev, ulong da, |
| 68 | ulong size) |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 69 | { |
Lokesh Vutla | c08eb93 | 2019-09-04 16:01:27 +0530 | [diff] [blame] | 70 | fdt32_t in_addr = cpu_to_be32(da), end_addr; |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 71 | u64 paddr; |
| 72 | |
| 73 | paddr = dev_translate_dma_address(dev, &in_addr); |
| 74 | if (paddr == OF_BAD_ADDR) { |
| 75 | dev_err(dev, "Unable to convert address %ld\n", da); |
| 76 | return NULL; |
| 77 | } |
| 78 | |
Lokesh Vutla | c08eb93 | 2019-09-04 16:01:27 +0530 | [diff] [blame] | 79 | end_addr = cpu_to_be32(da + size - 1); |
| 80 | if (dev_translate_dma_address(dev, &end_addr) == OF_BAD_ADDR) { |
| 81 | dev_err(dev, "Unable to convert address %ld\n", da + size - 1); |
| 82 | return NULL; |
| 83 | } |
| 84 | |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 85 | return phys_to_virt(paddr); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * stm32_copro_load() - Loadup the STM32 remote processor |
| 90 | * @dev: corresponding STM32 remote processor device |
| 91 | * @addr: Address in memory where image is stored |
| 92 | * @size: Size in bytes of the image |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 93 | * Return: 0 if all went ok, else corresponding -ve error |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 94 | */ |
| 95 | static int stm32_copro_load(struct udevice *dev, ulong addr, ulong size) |
| 96 | { |
| 97 | struct stm32_copro_privdata *priv; |
Fabien Dessenne | 33fd419 | 2019-10-30 14:38:33 +0100 | [diff] [blame] | 98 | ulong rsc_table_size; |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 99 | int ret; |
| 100 | |
| 101 | priv = dev_get_priv(dev); |
| 102 | |
Patrick Delaunay | 5a536df | 2020-10-15 15:01:12 +0200 | [diff] [blame] | 103 | ret = reset_assert(&priv->hold_boot); |
| 104 | if (ret) { |
| 105 | dev_err(dev, "Unable to assert hold boot (ret=%d)\n", ret); |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 106 | return ret; |
Patrick Delaunay | 5a536df | 2020-10-15 15:01:12 +0200 | [diff] [blame] | 107 | } |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 108 | |
| 109 | ret = reset_assert(&priv->reset_ctl); |
| 110 | if (ret) { |
| 111 | dev_err(dev, "Unable to assert reset line (ret=%d)\n", ret); |
| 112 | return ret; |
| 113 | } |
| 114 | |
Fabien Dessenne | 33fd419 | 2019-10-30 14:38:33 +0100 | [diff] [blame] | 115 | if (rproc_elf32_load_rsc_table(dev, addr, size, &priv->rsc_table_addr, |
| 116 | &rsc_table_size)) { |
| 117 | priv->rsc_table_addr = 0; |
| 118 | dev_warn(dev, "No valid resource table for this firmware\n"); |
| 119 | } |
| 120 | |
Lokesh Vutla | 14d963d | 2019-09-04 16:01:28 +0530 | [diff] [blame] | 121 | return rproc_elf32_load_image(dev, addr, size); |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /** |
| 125 | * stm32_copro_start() - Start the STM32 remote processor |
| 126 | * @dev: corresponding STM32 remote processor device |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 127 | * Return: 0 if all went ok, else corresponding -ve error |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 128 | */ |
| 129 | static int stm32_copro_start(struct udevice *dev) |
| 130 | { |
Fabien Dessenne | 33fd419 | 2019-10-30 14:38:33 +0100 | [diff] [blame] | 131 | struct stm32_copro_privdata *priv; |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 132 | int ret; |
| 133 | |
Fabien Dessenne | 33fd419 | 2019-10-30 14:38:33 +0100 | [diff] [blame] | 134 | priv = dev_get_priv(dev); |
| 135 | |
Patrick Delaunay | 5a536df | 2020-10-15 15:01:12 +0200 | [diff] [blame] | 136 | ret = reset_deassert(&priv->hold_boot); |
| 137 | if (ret) { |
| 138 | dev_err(dev, "Unable to deassert hold boot (ret=%d)\n", ret); |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 139 | return ret; |
Patrick Delaunay | 5a536df | 2020-10-15 15:01:12 +0200 | [diff] [blame] | 140 | } |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 141 | |
| 142 | /* |
| 143 | * Once copro running, reset hold boot flag to avoid copro |
Patrick Delaunay | 9ed6f92 | 2020-10-15 15:01:13 +0200 | [diff] [blame] | 144 | * rebooting autonomously (error should never occur) |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 145 | */ |
Patrick Delaunay | 5a536df | 2020-10-15 15:01:12 +0200 | [diff] [blame] | 146 | ret = reset_assert(&priv->hold_boot); |
| 147 | if (ret) |
| 148 | dev_err(dev, "Unable to assert hold boot (ret=%d)\n", ret); |
| 149 | |
Patrick Delaunay | 9ed6f92 | 2020-10-15 15:01:13 +0200 | [diff] [blame] | 150 | /* indicates that copro is running */ |
| 151 | writel(TAMP_COPRO_STATE_CRUN, TAMP_COPRO_STATE); |
| 152 | /* Store rsc_address in bkp register */ |
| 153 | writel(priv->rsc_table_addr, TAMP_COPRO_RSC_TBL_ADDRESS); |
Fabien Dessenne | 33fd419 | 2019-10-30 14:38:33 +0100 | [diff] [blame] | 154 | |
Patrick Delaunay | 9ed6f92 | 2020-10-15 15:01:13 +0200 | [diff] [blame] | 155 | return 0; |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /** |
| 159 | * stm32_copro_reset() - Reset the STM32 remote processor |
| 160 | * @dev: corresponding STM32 remote processor device |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 161 | * Return: 0 if all went ok, else corresponding -ve error |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 162 | */ |
| 163 | static int stm32_copro_reset(struct udevice *dev) |
| 164 | { |
| 165 | struct stm32_copro_privdata *priv; |
| 166 | int ret; |
| 167 | |
| 168 | priv = dev_get_priv(dev); |
| 169 | |
Patrick Delaunay | 5a536df | 2020-10-15 15:01:12 +0200 | [diff] [blame] | 170 | ret = reset_assert(&priv->hold_boot); |
| 171 | if (ret) { |
| 172 | dev_err(dev, "Unable to assert hold boot (ret=%d)\n", ret); |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 173 | return ret; |
Patrick Delaunay | 5a536df | 2020-10-15 15:01:12 +0200 | [diff] [blame] | 174 | } |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 175 | |
| 176 | ret = reset_assert(&priv->reset_ctl); |
| 177 | if (ret) { |
| 178 | dev_err(dev, "Unable to assert reset line (ret=%d)\n", ret); |
| 179 | return ret; |
| 180 | } |
| 181 | |
Fabien Dessenne | 4a4deb8 | 2019-10-30 14:38:31 +0100 | [diff] [blame] | 182 | writel(TAMP_COPRO_STATE_OFF, TAMP_COPRO_STATE); |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * stm32_copro_stop() - Stop the STM32 remote processor |
| 189 | * @dev: corresponding STM32 remote processor device |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 190 | * Return: 0 if all went ok, else corresponding -ve error |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 191 | */ |
| 192 | static int stm32_copro_stop(struct udevice *dev) |
| 193 | { |
| 194 | return stm32_copro_reset(dev); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * stm32_copro_is_running() - Is the STM32 remote processor running |
| 199 | * @dev: corresponding STM32 remote processor device |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 200 | * Return: 0 if the remote processor is running, 1 otherwise |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 201 | */ |
| 202 | static int stm32_copro_is_running(struct udevice *dev) |
| 203 | { |
Fabien Dessenne | 4a4deb8 | 2019-10-30 14:38:31 +0100 | [diff] [blame] | 204 | return (readl(TAMP_COPRO_STATE) == TAMP_COPRO_STATE_OFF); |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static const struct dm_rproc_ops stm32_copro_ops = { |
| 208 | .load = stm32_copro_load, |
| 209 | .start = stm32_copro_start, |
| 210 | .stop = stm32_copro_stop, |
| 211 | .reset = stm32_copro_reset, |
| 212 | .is_running = stm32_copro_is_running, |
| 213 | .device_to_virt = stm32_copro_device_to_virt, |
| 214 | }; |
| 215 | |
| 216 | static const struct udevice_id stm32_copro_ids[] = { |
Patrick Delaunay | 5d2901a | 2019-08-02 15:07:18 +0200 | [diff] [blame] | 217 | {.compatible = "st,stm32mp1-m4"}, |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 218 | {} |
| 219 | }; |
| 220 | |
| 221 | U_BOOT_DRIVER(stm32_copro) = { |
| 222 | .name = "stm32_m4_proc", |
| 223 | .of_match = stm32_copro_ids, |
| 224 | .id = UCLASS_REMOTEPROC, |
| 225 | .ops = &stm32_copro_ops, |
| 226 | .probe = stm32_copro_probe, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 227 | .priv_auto = sizeof(struct stm32_copro_privdata), |
Fabien Dessenne | 6bed04f | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 228 | }; |