Fabien Dessenne | e09846a | 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 | */ |
| 5 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
| 8 | #include <errno.h> |
| 9 | #include <fdtdec.h> |
| 10 | #include <regmap.h> |
| 11 | #include <remoteproc.h> |
| 12 | #include <reset.h> |
| 13 | #include <syscon.h> |
| 14 | #include <asm/io.h> |
| 15 | |
| 16 | #define RCC_GCR_HOLD_BOOT 0 |
| 17 | #define RCC_GCR_RELEASE_BOOT 1 |
| 18 | |
| 19 | /** |
| 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 |
Fabien Dessenne | ec452e1 | 2019-10-30 14:38:33 +0100 | [diff] [blame] | 25 | * @rsc_table_addr: resource table address |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 26 | */ |
| 27 | struct stm32_copro_privdata { |
| 28 | struct reset_ctl reset_ctl; |
| 29 | struct regmap *hold_boot_regmap; |
| 30 | uint hold_boot_offset; |
| 31 | uint hold_boot_mask; |
Fabien Dessenne | ec452e1 | 2019-10-30 14:38:33 +0100 | [diff] [blame] | 32 | ulong rsc_table_addr; |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | /** |
| 36 | * stm32_copro_probe() - Basic probe |
| 37 | * @dev: corresponding STM32 remote processor device |
| 38 | * @return 0 if all went ok, else corresponding -ve error |
| 39 | */ |
| 40 | static int stm32_copro_probe(struct udevice *dev) |
| 41 | { |
| 42 | struct stm32_copro_privdata *priv; |
| 43 | struct regmap *regmap; |
| 44 | const fdt32_t *cell; |
| 45 | int len, ret; |
| 46 | |
| 47 | priv = dev_get_priv(dev); |
| 48 | |
| 49 | regmap = syscon_regmap_lookup_by_phandle(dev, "st,syscfg-holdboot"); |
| 50 | if (IS_ERR(regmap)) { |
| 51 | dev_err(dev, "unable to find holdboot regmap (%ld)\n", |
| 52 | PTR_ERR(regmap)); |
| 53 | return PTR_ERR(regmap); |
| 54 | } |
| 55 | |
| 56 | cell = dev_read_prop(dev, "st,syscfg-holdboot", &len); |
| 57 | if (len < 3 * sizeof(fdt32_t)) { |
| 58 | dev_err(dev, "holdboot offset and mask not available\n"); |
| 59 | return -EINVAL; |
| 60 | } |
| 61 | |
| 62 | priv->hold_boot_regmap = regmap; |
| 63 | priv->hold_boot_offset = fdtdec_get_number(cell + 1, 1); |
| 64 | priv->hold_boot_mask = fdtdec_get_number(cell + 2, 1); |
| 65 | |
| 66 | ret = reset_get_by_index(dev, 0, &priv->reset_ctl); |
| 67 | if (ret) { |
| 68 | dev_err(dev, "failed to get reset (%d)\n", ret); |
| 69 | return ret; |
| 70 | } |
| 71 | |
| 72 | dev_dbg(dev, "probed\n"); |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * stm32_copro_set_hold_boot() - Hold boot bit management |
| 79 | * @dev: corresponding STM32 remote processor device |
| 80 | * @hold: hold boot value |
| 81 | * @return 0 if all went ok, else corresponding -ve error |
| 82 | */ |
| 83 | static int stm32_copro_set_hold_boot(struct udevice *dev, bool hold) |
| 84 | { |
| 85 | struct stm32_copro_privdata *priv; |
| 86 | uint val; |
| 87 | int ret; |
| 88 | |
| 89 | priv = dev_get_priv(dev); |
| 90 | |
| 91 | val = hold ? RCC_GCR_HOLD_BOOT : RCC_GCR_RELEASE_BOOT; |
| 92 | |
| 93 | /* |
| 94 | * Note: shall run an SMC call (STM32_SMC_RCC) if platform is secured. |
| 95 | * To be updated when the code for this SMC service is available which |
| 96 | * is not the case for the time being. |
| 97 | */ |
| 98 | ret = regmap_update_bits(priv->hold_boot_regmap, priv->hold_boot_offset, |
| 99 | priv->hold_boot_mask, val); |
| 100 | if (ret) |
| 101 | dev_err(dev, "failed to set hold boot\n"); |
| 102 | |
| 103 | return ret; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * stm32_copro_device_to_virt() - Convert device address to virtual address |
| 108 | * @dev: corresponding STM32 remote processor device |
| 109 | * @da: device address |
Lokesh Vutla | e18166f | 2019-09-04 16:01:27 +0530 | [diff] [blame] | 110 | * @size: Size of the memory region @da is pointing to |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 111 | * @return converted virtual address |
| 112 | */ |
Lokesh Vutla | e18166f | 2019-09-04 16:01:27 +0530 | [diff] [blame] | 113 | static void *stm32_copro_device_to_virt(struct udevice *dev, ulong da, |
| 114 | ulong size) |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 115 | { |
Lokesh Vutla | e18166f | 2019-09-04 16:01:27 +0530 | [diff] [blame] | 116 | fdt32_t in_addr = cpu_to_be32(da), end_addr; |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 117 | u64 paddr; |
| 118 | |
| 119 | paddr = dev_translate_dma_address(dev, &in_addr); |
| 120 | if (paddr == OF_BAD_ADDR) { |
| 121 | dev_err(dev, "Unable to convert address %ld\n", da); |
| 122 | return NULL; |
| 123 | } |
| 124 | |
Lokesh Vutla | e18166f | 2019-09-04 16:01:27 +0530 | [diff] [blame] | 125 | end_addr = cpu_to_be32(da + size - 1); |
| 126 | if (dev_translate_dma_address(dev, &end_addr) == OF_BAD_ADDR) { |
| 127 | dev_err(dev, "Unable to convert address %ld\n", da + size - 1); |
| 128 | return NULL; |
| 129 | } |
| 130 | |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 131 | return phys_to_virt(paddr); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * stm32_copro_load() - Loadup the STM32 remote processor |
| 136 | * @dev: corresponding STM32 remote processor device |
| 137 | * @addr: Address in memory where image is stored |
| 138 | * @size: Size in bytes of the image |
| 139 | * @return 0 if all went ok, else corresponding -ve error |
| 140 | */ |
| 141 | static int stm32_copro_load(struct udevice *dev, ulong addr, ulong size) |
| 142 | { |
| 143 | struct stm32_copro_privdata *priv; |
Fabien Dessenne | ec452e1 | 2019-10-30 14:38:33 +0100 | [diff] [blame] | 144 | ulong rsc_table_size; |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 145 | int ret; |
| 146 | |
| 147 | priv = dev_get_priv(dev); |
| 148 | |
| 149 | ret = stm32_copro_set_hold_boot(dev, true); |
| 150 | if (ret) |
| 151 | return ret; |
| 152 | |
| 153 | ret = reset_assert(&priv->reset_ctl); |
| 154 | if (ret) { |
| 155 | dev_err(dev, "Unable to assert reset line (ret=%d)\n", ret); |
| 156 | return ret; |
| 157 | } |
| 158 | |
Fabien Dessenne | ec452e1 | 2019-10-30 14:38:33 +0100 | [diff] [blame] | 159 | if (rproc_elf32_load_rsc_table(dev, addr, size, &priv->rsc_table_addr, |
| 160 | &rsc_table_size)) { |
| 161 | priv->rsc_table_addr = 0; |
| 162 | dev_warn(dev, "No valid resource table for this firmware\n"); |
| 163 | } |
| 164 | |
Lokesh Vutla | b506cba | 2019-09-04 16:01:28 +0530 | [diff] [blame] | 165 | return rproc_elf32_load_image(dev, addr, size); |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /** |
| 169 | * stm32_copro_start() - Start the STM32 remote processor |
| 170 | * @dev: corresponding STM32 remote processor device |
| 171 | * @return 0 if all went ok, else corresponding -ve error |
| 172 | */ |
| 173 | static int stm32_copro_start(struct udevice *dev) |
| 174 | { |
Fabien Dessenne | ec452e1 | 2019-10-30 14:38:33 +0100 | [diff] [blame] | 175 | struct stm32_copro_privdata *priv; |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 176 | int ret; |
| 177 | |
Fabien Dessenne | ec452e1 | 2019-10-30 14:38:33 +0100 | [diff] [blame] | 178 | priv = dev_get_priv(dev); |
| 179 | |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 180 | /* move hold boot from true to false start the copro */ |
| 181 | ret = stm32_copro_set_hold_boot(dev, false); |
| 182 | if (ret) |
| 183 | return ret; |
| 184 | |
| 185 | /* |
| 186 | * Once copro running, reset hold boot flag to avoid copro |
| 187 | * rebooting autonomously |
| 188 | */ |
| 189 | ret = stm32_copro_set_hold_boot(dev, true); |
Fabien Dessenne | 4446d2f | 2019-10-30 14:38:31 +0100 | [diff] [blame] | 190 | writel(ret ? TAMP_COPRO_STATE_OFF : TAMP_COPRO_STATE_CRUN, |
| 191 | TAMP_COPRO_STATE); |
Fabien Dessenne | ec452e1 | 2019-10-30 14:38:33 +0100 | [diff] [blame] | 192 | if (!ret) |
| 193 | /* Store rsc_address in bkp register */ |
| 194 | writel(priv->rsc_table_addr, TAMP_COPRO_RSC_TBL_ADDRESS); |
| 195 | |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 196 | return ret; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * stm32_copro_reset() - Reset the STM32 remote processor |
| 201 | * @dev: corresponding STM32 remote processor device |
| 202 | * @return 0 if all went ok, else corresponding -ve error |
| 203 | */ |
| 204 | static int stm32_copro_reset(struct udevice *dev) |
| 205 | { |
| 206 | struct stm32_copro_privdata *priv; |
| 207 | int ret; |
| 208 | |
| 209 | priv = dev_get_priv(dev); |
| 210 | |
| 211 | ret = stm32_copro_set_hold_boot(dev, true); |
| 212 | if (ret) |
| 213 | return ret; |
| 214 | |
| 215 | ret = reset_assert(&priv->reset_ctl); |
| 216 | if (ret) { |
| 217 | dev_err(dev, "Unable to assert reset line (ret=%d)\n", ret); |
| 218 | return ret; |
| 219 | } |
| 220 | |
Fabien Dessenne | 4446d2f | 2019-10-30 14:38:31 +0100 | [diff] [blame] | 221 | writel(TAMP_COPRO_STATE_OFF, TAMP_COPRO_STATE); |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * stm32_copro_stop() - Stop the STM32 remote processor |
| 228 | * @dev: corresponding STM32 remote processor device |
| 229 | * @return 0 if all went ok, else corresponding -ve error |
| 230 | */ |
| 231 | static int stm32_copro_stop(struct udevice *dev) |
| 232 | { |
| 233 | return stm32_copro_reset(dev); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * stm32_copro_is_running() - Is the STM32 remote processor running |
| 238 | * @dev: corresponding STM32 remote processor device |
Fabien Dessenne | 4446d2f | 2019-10-30 14:38:31 +0100 | [diff] [blame] | 239 | * @return 0 if the remote processor is running, 1 otherwise |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 240 | */ |
| 241 | static int stm32_copro_is_running(struct udevice *dev) |
| 242 | { |
Fabien Dessenne | 4446d2f | 2019-10-30 14:38:31 +0100 | [diff] [blame] | 243 | return (readl(TAMP_COPRO_STATE) == TAMP_COPRO_STATE_OFF); |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | static const struct dm_rproc_ops stm32_copro_ops = { |
| 247 | .load = stm32_copro_load, |
| 248 | .start = stm32_copro_start, |
| 249 | .stop = stm32_copro_stop, |
| 250 | .reset = stm32_copro_reset, |
| 251 | .is_running = stm32_copro_is_running, |
| 252 | .device_to_virt = stm32_copro_device_to_virt, |
| 253 | }; |
| 254 | |
| 255 | static const struct udevice_id stm32_copro_ids[] = { |
Patrick Delaunay | 26c24b4 | 2019-08-02 15:07:18 +0200 | [diff] [blame] | 256 | {.compatible = "st,stm32mp1-m4"}, |
Fabien Dessenne | e09846a | 2019-05-31 15:11:34 +0200 | [diff] [blame] | 257 | {} |
| 258 | }; |
| 259 | |
| 260 | U_BOOT_DRIVER(stm32_copro) = { |
| 261 | .name = "stm32_m4_proc", |
| 262 | .of_match = stm32_copro_ids, |
| 263 | .id = UCLASS_REMOTEPROC, |
| 264 | .ops = &stm32_copro_ops, |
| 265 | .probe = stm32_copro_probe, |
| 266 | .priv_auto_alloc_size = sizeof(struct stm32_copro_privdata), |
| 267 | }; |