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