Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2018, STMicroelectronics - All Rights Reserved |
| 4 | */ |
| 5 | |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 6 | #define LOG_CATEGORY UCLASS_MISC |
| 7 | |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 8 | #include <common.h> |
Patrick Delaunay | aaf1f96 | 2021-02-25 13:43:07 +0100 | [diff] [blame] | 9 | #include <clk.h> |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 10 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 12 | #include <misc.h> |
| 13 | #include <asm/io.h> |
Patrick Delaunay | 6332c04 | 2020-06-16 18:27:44 +0200 | [diff] [blame] | 14 | #include <asm/arch/bsec.h> |
Patrick Delaunay | 7858d7e | 2019-02-12 11:44:40 +0100 | [diff] [blame] | 15 | #include <asm/arch/stm32mp1_smc.h> |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 16 | #include <dm/device_compat.h> |
Patrick Delaunay | 7858d7e | 2019-02-12 11:44:40 +0100 | [diff] [blame] | 17 | #include <linux/arm-smccc.h> |
Patrick Delaunay | 2fa55eb | 2019-04-18 17:32:39 +0200 | [diff] [blame] | 18 | #include <linux/iopoll.h> |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 19 | |
| 20 | #define BSEC_OTP_MAX_VALUE 95 |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 21 | #define BSEC_TIMEOUT_US 10000 |
| 22 | |
| 23 | /* BSEC REGISTER OFFSET (base relative) */ |
| 24 | #define BSEC_OTP_CONF_OFF 0x000 |
| 25 | #define BSEC_OTP_CTRL_OFF 0x004 |
| 26 | #define BSEC_OTP_WRDATA_OFF 0x008 |
| 27 | #define BSEC_OTP_STATUS_OFF 0x00C |
| 28 | #define BSEC_OTP_LOCK_OFF 0x010 |
Patrick Delaunay | 6332c04 | 2020-06-16 18:27:44 +0200 | [diff] [blame] | 29 | #define BSEC_DENABLE_OFF 0x014 |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 30 | #define BSEC_DISTURBED_OFF 0x01C |
| 31 | #define BSEC_ERROR_OFF 0x034 |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 32 | #define BSEC_WRLOCK_OFF 0x04C /* OTP write permananet lock */ |
| 33 | #define BSEC_SPLOCK_OFF 0x064 /* OTP write sticky lock */ |
| 34 | #define BSEC_SWLOCK_OFF 0x07C /* shadow write sticky lock */ |
| 35 | #define BSEC_SRLOCK_OFF 0x094 /* shadow read sticky lock */ |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 36 | #define BSEC_OTP_DATA_OFF 0x200 |
| 37 | |
| 38 | /* BSEC_CONFIGURATION Register MASK */ |
| 39 | #define BSEC_CONF_POWER_UP 0x001 |
| 40 | |
| 41 | /* BSEC_CONTROL Register */ |
| 42 | #define BSEC_READ 0x000 |
| 43 | #define BSEC_WRITE 0x100 |
| 44 | |
| 45 | /* LOCK Register */ |
| 46 | #define OTP_LOCK_MASK 0x1F |
| 47 | #define OTP_LOCK_BANK_SHIFT 0x05 |
| 48 | #define OTP_LOCK_BIT_MASK 0x01 |
| 49 | |
| 50 | /* STATUS Register */ |
| 51 | #define BSEC_MODE_BUSY_MASK 0x08 |
| 52 | #define BSEC_MODE_PROGFAIL_MASK 0x10 |
| 53 | #define BSEC_MODE_PWR_MASK 0x20 |
| 54 | |
Patrick Delaunay | 6332c04 | 2020-06-16 18:27:44 +0200 | [diff] [blame] | 55 | /* DENABLE Register */ |
| 56 | #define BSEC_DENABLE_DBGSWENABLE BIT(10) |
| 57 | |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 58 | /* |
| 59 | * OTP Lock services definition |
| 60 | * Value must corresponding to the bit number in the register |
| 61 | */ |
| 62 | #define BSEC_LOCK_PROGRAM 0x04 |
| 63 | |
| 64 | /** |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 65 | * bsec_lock() - manage lock for each type SR/SP/SW |
| 66 | * @address: address of bsec IP register |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 67 | * @otp: otp number (0 - BSEC_OTP_MAX_VALUE) |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 68 | * Return: true if locked else false |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 69 | */ |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 70 | static bool bsec_read_lock(u32 address, u32 otp) |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 71 | { |
| 72 | u32 bit; |
| 73 | u32 bank; |
| 74 | |
| 75 | bit = 1 << (otp & OTP_LOCK_MASK); |
| 76 | bank = ((otp >> OTP_LOCK_BANK_SHIFT) & OTP_LOCK_MASK) * sizeof(u32); |
| 77 | |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 78 | return !!(readl(address + bank) & bit); |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /** |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 82 | * bsec_check_error() - Check status of one otp |
| 83 | * @base: base address of bsec IP |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 84 | * @otp: otp number (0 - BSEC_OTP_MAX_VALUE) |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 85 | * Return: 0 if no error, -EAGAIN or -ENOTSUPP |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 86 | */ |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 87 | static u32 bsec_check_error(u32 base, u32 otp) |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 88 | { |
| 89 | u32 bit; |
| 90 | u32 bank; |
| 91 | |
| 92 | bit = 1 << (otp & OTP_LOCK_MASK); |
| 93 | bank = ((otp >> OTP_LOCK_BANK_SHIFT) & OTP_LOCK_MASK) * sizeof(u32); |
| 94 | |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 95 | if (readl(base + BSEC_DISTURBED_OFF + bank) & bit) |
| 96 | return -EAGAIN; |
| 97 | else if (readl(base + BSEC_ERROR_OFF + bank) & bit) |
| 98 | return -ENOTSUPP; |
| 99 | |
| 100 | return 0; |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
| 104 | * bsec_read_SR_lock() - read SR lock (Shadowing) |
| 105 | * @base: base address of bsec IP |
| 106 | * @otp: otp number (0 - BSEC_OTP_MAX_VALUE) |
| 107 | * Return: true if locked else false |
| 108 | */ |
| 109 | static bool bsec_read_SR_lock(u32 base, u32 otp) |
| 110 | { |
| 111 | return bsec_read_lock(base + BSEC_SRLOCK_OFF, otp); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * bsec_read_SP_lock() - read SP lock (program Lock) |
| 116 | * @base: base address of bsec IP |
| 117 | * @otp: otp number (0 - BSEC_OTP_MAX_VALUE) |
| 118 | * Return: true if locked else false |
| 119 | */ |
| 120 | static bool bsec_read_SP_lock(u32 base, u32 otp) |
| 121 | { |
| 122 | return bsec_read_lock(base + BSEC_SPLOCK_OFF, otp); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * bsec_SW_lock() - manage SW lock (Write in Shadow) |
| 127 | * @base: base address of bsec IP |
| 128 | * @otp: otp number (0 - BSEC_OTP_MAX_VALUE) |
| 129 | * Return: true if locked else false |
| 130 | */ |
| 131 | static bool bsec_read_SW_lock(u32 base, u32 otp) |
| 132 | { |
| 133 | return bsec_read_lock(base + BSEC_SWLOCK_OFF, otp); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * bsec_power_safmem() - Activate or deactivate safmem power |
| 138 | * @base: base address of bsec IP |
| 139 | * @power: true to power up , false to power down |
| 140 | * Return: 0 if succeed |
| 141 | */ |
| 142 | static int bsec_power_safmem(u32 base, bool power) |
| 143 | { |
| 144 | u32 val; |
| 145 | u32 mask; |
| 146 | |
| 147 | if (power) { |
| 148 | setbits_le32(base + BSEC_OTP_CONF_OFF, BSEC_CONF_POWER_UP); |
| 149 | mask = BSEC_MODE_PWR_MASK; |
| 150 | } else { |
| 151 | clrbits_le32(base + BSEC_OTP_CONF_OFF, BSEC_CONF_POWER_UP); |
| 152 | mask = 0; |
| 153 | } |
| 154 | |
| 155 | /* waiting loop */ |
| 156 | return readl_poll_timeout(base + BSEC_OTP_STATUS_OFF, |
| 157 | val, (val & BSEC_MODE_PWR_MASK) == mask, |
| 158 | BSEC_TIMEOUT_US); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * bsec_shadow_register() - copy safmen otp to bsec data |
| 163 | * @base: base address of bsec IP |
| 164 | * @otp: otp number (0 - BSEC_OTP_MAX_VALUE) |
| 165 | * Return: 0 if no error |
| 166 | */ |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 167 | static int bsec_shadow_register(struct udevice *dev, u32 base, u32 otp) |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 168 | { |
| 169 | u32 val; |
| 170 | int ret; |
| 171 | bool power_up = false; |
| 172 | |
| 173 | /* check if shadowing of otp is locked */ |
| 174 | if (bsec_read_SR_lock(base, otp)) |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 175 | dev_dbg(dev, "OTP %d is locked and refreshed with 0\n", |
| 176 | otp); |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 177 | |
| 178 | /* check if safemem is power up */ |
| 179 | val = readl(base + BSEC_OTP_STATUS_OFF); |
| 180 | if (!(val & BSEC_MODE_PWR_MASK)) { |
| 181 | ret = bsec_power_safmem(base, true); |
| 182 | if (ret) |
| 183 | return ret; |
Patrick Delaunay | f95686b | 2019-02-27 17:01:28 +0100 | [diff] [blame] | 184 | power_up = true; |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 185 | } |
| 186 | /* set BSEC_OTP_CTRL_OFF with the otp value*/ |
| 187 | writel(otp | BSEC_READ, base + BSEC_OTP_CTRL_OFF); |
| 188 | |
| 189 | /* check otp status*/ |
| 190 | ret = readl_poll_timeout(base + BSEC_OTP_STATUS_OFF, |
| 191 | val, (val & BSEC_MODE_BUSY_MASK) == 0, |
| 192 | BSEC_TIMEOUT_US); |
| 193 | if (ret) |
| 194 | return ret; |
| 195 | |
| 196 | ret = bsec_check_error(base, otp); |
| 197 | |
| 198 | if (power_up) |
| 199 | bsec_power_safmem(base, false); |
| 200 | |
| 201 | return ret; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * bsec_read_shadow() - read an otp data value from shadow |
| 206 | * @base: base address of bsec IP |
| 207 | * @val: read value |
| 208 | * @otp: otp number (0 - BSEC_OTP_MAX_VALUE) |
| 209 | * Return: 0 if no error |
| 210 | */ |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 211 | static int bsec_read_shadow(struct udevice *dev, u32 base, u32 *val, u32 otp) |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 212 | { |
| 213 | *val = readl(base + BSEC_OTP_DATA_OFF + otp * sizeof(u32)); |
| 214 | |
| 215 | return bsec_check_error(base, otp); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * bsec_write_shadow() - write value in BSEC data register in shadow |
| 220 | * @base: base address of bsec IP |
| 221 | * @val: value to write |
| 222 | * @otp: otp number (0 - BSEC_OTP_MAX_VALUE) |
| 223 | * Return: 0 if no error |
| 224 | */ |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 225 | static int bsec_write_shadow(struct udevice *dev, u32 base, u32 val, u32 otp) |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 226 | { |
| 227 | /* check if programming of otp is locked */ |
| 228 | if (bsec_read_SW_lock(base, otp)) |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 229 | dev_dbg(dev, "OTP %d is lock, write will be ignore\n", otp); |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 230 | |
| 231 | writel(val, base + BSEC_OTP_DATA_OFF + otp * sizeof(u32)); |
| 232 | |
| 233 | return bsec_check_error(base, otp); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * bsec_program_otp() - program a bit in SAFMEM |
| 238 | * @base: base address of bsec IP |
| 239 | * @val: value to program |
| 240 | * @otp: otp number (0 - BSEC_OTP_MAX_VALUE) |
| 241 | * after the function the otp data is not refreshed in shadow |
| 242 | * Return: 0 if no error |
| 243 | */ |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 244 | static int bsec_program_otp(struct udevice *dev, long base, u32 val, u32 otp) |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 245 | { |
| 246 | u32 ret; |
| 247 | bool power_up = false; |
| 248 | |
| 249 | if (bsec_read_SP_lock(base, otp)) |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 250 | dev_dbg(dev, "OTP %d locked, prog will be ignore\n", otp); |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 251 | |
| 252 | if (readl(base + BSEC_OTP_LOCK_OFF) & (1 << BSEC_LOCK_PROGRAM)) |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 253 | dev_dbg(dev, "Global lock, prog will be ignore\n"); |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 254 | |
| 255 | /* check if safemem is power up */ |
| 256 | if (!(readl(base + BSEC_OTP_STATUS_OFF) & BSEC_MODE_PWR_MASK)) { |
| 257 | ret = bsec_power_safmem(base, true); |
| 258 | if (ret) |
| 259 | return ret; |
| 260 | |
| 261 | power_up = true; |
| 262 | } |
| 263 | /* set value in write register*/ |
| 264 | writel(val, base + BSEC_OTP_WRDATA_OFF); |
| 265 | |
| 266 | /* set BSEC_OTP_CTRL_OFF with the otp value */ |
| 267 | writel(otp | BSEC_WRITE, base + BSEC_OTP_CTRL_OFF); |
| 268 | |
| 269 | /* check otp status*/ |
| 270 | ret = readl_poll_timeout(base + BSEC_OTP_STATUS_OFF, |
| 271 | val, (val & BSEC_MODE_BUSY_MASK) == 0, |
| 272 | BSEC_TIMEOUT_US); |
| 273 | if (ret) |
| 274 | return ret; |
| 275 | |
| 276 | if (val & BSEC_MODE_PROGFAIL_MASK) |
| 277 | ret = -EACCES; |
| 278 | else |
| 279 | ret = bsec_check_error(base, otp); |
| 280 | |
| 281 | if (power_up) |
| 282 | bsec_power_safmem(base, false); |
| 283 | |
| 284 | return ret; |
| 285 | } |
| 286 | |
| 287 | /* BSEC MISC driver *******************************************************/ |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 288 | struct stm32mp_bsec_plat { |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 289 | u32 base; |
| 290 | }; |
| 291 | |
| 292 | static int stm32mp_bsec_read_otp(struct udevice *dev, u32 *val, u32 otp) |
| 293 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 294 | struct stm32mp_bsec_plat *plat; |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 295 | u32 tmp_data = 0; |
| 296 | int ret; |
| 297 | |
Patrick Delaunay | 72a5762 | 2021-10-11 09:52:50 +0200 | [diff] [blame] | 298 | if (IS_ENABLED(CONFIG_ARM_SMCCC) && !IS_ENABLED(CONFIG_SPL_BUILD)) |
Patrick Delaunay | 1dffeaf | 2020-07-31 16:31:51 +0200 | [diff] [blame] | 299 | return stm32_smc(STM32_SMC_BSEC, |
| 300 | STM32_SMC_READ_OTP, |
| 301 | otp, 0, val); |
| 302 | |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 303 | plat = dev_get_plat(dev); |
Patrick Delaunay | 1dffeaf | 2020-07-31 16:31:51 +0200 | [diff] [blame] | 304 | |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 305 | /* read current shadow value */ |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 306 | ret = bsec_read_shadow(dev, plat->base, &tmp_data, otp); |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 307 | if (ret) |
| 308 | return ret; |
| 309 | |
| 310 | /* copy otp in shadow */ |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 311 | ret = bsec_shadow_register(dev, plat->base, otp); |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 312 | if (ret) |
| 313 | return ret; |
| 314 | |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 315 | ret = bsec_read_shadow(dev, plat->base, val, otp); |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 316 | if (ret) |
| 317 | return ret; |
| 318 | |
| 319 | /* restore shadow value */ |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 320 | ret = bsec_write_shadow(dev, plat->base, tmp_data, otp); |
Patrick Delaunay | 1dffeaf | 2020-07-31 16:31:51 +0200 | [diff] [blame] | 321 | |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 322 | return ret; |
| 323 | } |
| 324 | |
| 325 | static int stm32mp_bsec_read_shadow(struct udevice *dev, u32 *val, u32 otp) |
| 326 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 327 | struct stm32mp_bsec_plat *plat; |
Patrick Delaunay | 1dffeaf | 2020-07-31 16:31:51 +0200 | [diff] [blame] | 328 | |
Patrick Delaunay | 72a5762 | 2021-10-11 09:52:50 +0200 | [diff] [blame] | 329 | if (IS_ENABLED(CONFIG_ARM_SMCCC) && !IS_ENABLED(CONFIG_SPL_BUILD)) |
Patrick Delaunay | 1dffeaf | 2020-07-31 16:31:51 +0200 | [diff] [blame] | 330 | return stm32_smc(STM32_SMC_BSEC, |
| 331 | STM32_SMC_READ_SHADOW, |
| 332 | otp, 0, val); |
| 333 | |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 334 | plat = dev_get_plat(dev); |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 335 | |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 336 | return bsec_read_shadow(dev, plat->base, val, otp); |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 337 | } |
| 338 | |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 339 | static int stm32mp_bsec_read_lock(struct udevice *dev, u32 *val, u32 otp) |
| 340 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 341 | struct stm32mp_bsec_plat *plat = dev_get_plat(dev); |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 342 | |
| 343 | /* return OTP permanent write lock status */ |
| 344 | *val = bsec_read_lock(plat->base + BSEC_WRLOCK_OFF, otp); |
| 345 | |
| 346 | return 0; |
| 347 | } |
| 348 | |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 349 | static int stm32mp_bsec_write_otp(struct udevice *dev, u32 val, u32 otp) |
| 350 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 351 | struct stm32mp_bsec_plat *plat; |
Patrick Delaunay | 1dffeaf | 2020-07-31 16:31:51 +0200 | [diff] [blame] | 352 | |
Patrick Delaunay | 72a5762 | 2021-10-11 09:52:50 +0200 | [diff] [blame] | 353 | if (IS_ENABLED(CONFIG_ARM_SMCCC) && !IS_ENABLED(CONFIG_SPL_BUILD)) |
Patrick Delaunay | 1dffeaf | 2020-07-31 16:31:51 +0200 | [diff] [blame] | 354 | return stm32_smc_exec(STM32_SMC_BSEC, |
| 355 | STM32_SMC_PROG_OTP, |
| 356 | otp, val); |
| 357 | |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 358 | plat = dev_get_plat(dev); |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 359 | |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 360 | return bsec_program_otp(dev, plat->base, val, otp); |
Patrick Delaunay | 1dffeaf | 2020-07-31 16:31:51 +0200 | [diff] [blame] | 361 | |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | static int stm32mp_bsec_write_shadow(struct udevice *dev, u32 val, u32 otp) |
| 365 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 366 | struct stm32mp_bsec_plat *plat; |
Patrick Delaunay | 1dffeaf | 2020-07-31 16:31:51 +0200 | [diff] [blame] | 367 | |
Patrick Delaunay | 72a5762 | 2021-10-11 09:52:50 +0200 | [diff] [blame] | 368 | if (IS_ENABLED(CONFIG_ARM_SMCCC) && !IS_ENABLED(CONFIG_SPL_BUILD)) |
Patrick Delaunay | 1dffeaf | 2020-07-31 16:31:51 +0200 | [diff] [blame] | 369 | return stm32_smc_exec(STM32_SMC_BSEC, |
| 370 | STM32_SMC_WRITE_SHADOW, |
| 371 | otp, val); |
| 372 | |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 373 | plat = dev_get_plat(dev); |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 374 | |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 375 | return bsec_write_shadow(dev, plat->base, val, otp); |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 376 | } |
| 377 | |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 378 | static int stm32mp_bsec_write_lock(struct udevice *dev, u32 val, u32 otp) |
| 379 | { |
Patrick Delaunay | 72a5762 | 2021-10-11 09:52:50 +0200 | [diff] [blame] | 380 | if (!IS_ENABLED(CONFIG_ARM_SMCCC) || IS_ENABLED(CONFIG_SPL_BUILD)) |
Patrick Delaunay | 1dffeaf | 2020-07-31 16:31:51 +0200 | [diff] [blame] | 381 | return -ENOTSUPP; |
| 382 | |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 383 | if (val == 1) |
| 384 | return stm32_smc_exec(STM32_SMC_BSEC, |
| 385 | STM32_SMC_WRLOCK_OTP, |
| 386 | otp, 0); |
| 387 | if (val == 0) |
| 388 | return 0; /* nothing to do */ |
| 389 | |
| 390 | return -EINVAL; |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 391 | } |
| 392 | |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 393 | static int stm32mp_bsec_read(struct udevice *dev, int offset, |
| 394 | void *buf, int size) |
| 395 | { |
| 396 | int ret; |
| 397 | int i; |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 398 | bool shadow = true, lock = false; |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 399 | int nb_otp = size / sizeof(u32); |
| 400 | int otp; |
Patrick Delaunay | 4c7c074 | 2019-06-21 15:26:43 +0200 | [diff] [blame] | 401 | unsigned int offs = offset; |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 402 | |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 403 | if (offs >= STM32_BSEC_LOCK_OFFSET) { |
| 404 | offs -= STM32_BSEC_LOCK_OFFSET; |
| 405 | lock = true; |
| 406 | } else if (offs >= STM32_BSEC_OTP_OFFSET) { |
Patrick Delaunay | 4c7c074 | 2019-06-21 15:26:43 +0200 | [diff] [blame] | 407 | offs -= STM32_BSEC_OTP_OFFSET; |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 408 | shadow = false; |
| 409 | } |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 410 | |
Patrick Delaunay | 3b7dbd4 | 2020-02-12 19:37:37 +0100 | [diff] [blame] | 411 | if ((offs % 4) || (size % 4)) |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 412 | return -EINVAL; |
Patrick Delaunay | 7e5f8e3 | 2019-08-02 13:08:02 +0200 | [diff] [blame] | 413 | |
| 414 | otp = offs / sizeof(u32); |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 415 | |
Patrick Delaunay | 7e5f8e3 | 2019-08-02 13:08:02 +0200 | [diff] [blame] | 416 | for (i = otp; i < (otp + nb_otp) && i <= BSEC_OTP_MAX_VALUE; i++) { |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 417 | u32 *addr = &((u32 *)buf)[i - otp]; |
| 418 | |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 419 | if (lock) |
| 420 | ret = stm32mp_bsec_read_lock(dev, addr, i); |
| 421 | else if (shadow) |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 422 | ret = stm32mp_bsec_read_shadow(dev, addr, i); |
| 423 | else |
| 424 | ret = stm32mp_bsec_read_otp(dev, addr, i); |
| 425 | |
| 426 | if (ret) |
| 427 | break; |
| 428 | } |
Patrick Delaunay | 7e5f8e3 | 2019-08-02 13:08:02 +0200 | [diff] [blame] | 429 | if (ret) |
| 430 | return ret; |
| 431 | else |
| 432 | return (i - otp) * 4; |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | static int stm32mp_bsec_write(struct udevice *dev, int offset, |
| 436 | const void *buf, int size) |
| 437 | { |
| 438 | int ret = 0; |
| 439 | int i; |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 440 | bool shadow = true, lock = false; |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 441 | int nb_otp = size / sizeof(u32); |
| 442 | int otp; |
Patrick Delaunay | 4c7c074 | 2019-06-21 15:26:43 +0200 | [diff] [blame] | 443 | unsigned int offs = offset; |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 444 | |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 445 | if (offs >= STM32_BSEC_LOCK_OFFSET) { |
| 446 | offs -= STM32_BSEC_LOCK_OFFSET; |
| 447 | lock = true; |
| 448 | } else if (offs >= STM32_BSEC_OTP_OFFSET) { |
Patrick Delaunay | 4c7c074 | 2019-06-21 15:26:43 +0200 | [diff] [blame] | 449 | offs -= STM32_BSEC_OTP_OFFSET; |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 450 | shadow = false; |
| 451 | } |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 452 | |
Patrick Delaunay | 3b7dbd4 | 2020-02-12 19:37:37 +0100 | [diff] [blame] | 453 | if ((offs % 4) || (size % 4)) |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 454 | return -EINVAL; |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 455 | |
Patrick Delaunay | 7e5f8e3 | 2019-08-02 13:08:02 +0200 | [diff] [blame] | 456 | otp = offs / sizeof(u32); |
| 457 | |
| 458 | for (i = otp; i < otp + nb_otp && i <= BSEC_OTP_MAX_VALUE; i++) { |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 459 | u32 *val = &((u32 *)buf)[i - otp]; |
| 460 | |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 461 | if (lock) |
| 462 | ret = stm32mp_bsec_write_lock(dev, *val, i); |
| 463 | else if (shadow) |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 464 | ret = stm32mp_bsec_write_shadow(dev, *val, i); |
| 465 | else |
| 466 | ret = stm32mp_bsec_write_otp(dev, *val, i); |
| 467 | if (ret) |
| 468 | break; |
| 469 | } |
Patrick Delaunay | 7e5f8e3 | 2019-08-02 13:08:02 +0200 | [diff] [blame] | 470 | if (ret) |
| 471 | return ret; |
| 472 | else |
| 473 | return (i - otp) * 4; |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | static const struct misc_ops stm32mp_bsec_ops = { |
| 477 | .read = stm32mp_bsec_read, |
| 478 | .write = stm32mp_bsec_write, |
| 479 | }; |
| 480 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 481 | static int stm32mp_bsec_of_to_plat(struct udevice *dev) |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 482 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 483 | struct stm32mp_bsec_plat *plat = dev_get_plat(dev); |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 484 | |
| 485 | plat->base = (u32)dev_read_addr_ptr(dev); |
| 486 | |
| 487 | return 0; |
| 488 | } |
| 489 | |
Patrick Delaunay | f95686b | 2019-02-27 17:01:28 +0100 | [diff] [blame] | 490 | static int stm32mp_bsec_probe(struct udevice *dev) |
| 491 | { |
| 492 | int otp; |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 493 | struct stm32mp_bsec_plat *plat; |
Patrick Delaunay | aaf1f96 | 2021-02-25 13:43:07 +0100 | [diff] [blame] | 494 | struct clk_bulk clk_bulk; |
| 495 | int ret; |
| 496 | |
| 497 | ret = clk_get_bulk(dev, &clk_bulk); |
| 498 | if (!ret) { |
| 499 | ret = clk_enable_bulk(&clk_bulk); |
| 500 | if (ret) |
| 501 | return ret; |
| 502 | } |
Patrick Delaunay | f95686b | 2019-02-27 17:01:28 +0100 | [diff] [blame] | 503 | |
Patrick Delaunay | b6cc505 | 2020-05-25 12:19:41 +0200 | [diff] [blame] | 504 | /* |
| 505 | * update unlocked shadow for OTP cleared by the rom code |
Patrick Delaunay | dd2ca25 | 2021-10-11 09:52:48 +0200 | [diff] [blame] | 506 | * only executed in SPL, it is done in TF-A for TFABOOT |
Patrick Delaunay | b6cc505 | 2020-05-25 12:19:41 +0200 | [diff] [blame] | 507 | */ |
Patrick Delaunay | dd2ca25 | 2021-10-11 09:52:48 +0200 | [diff] [blame] | 508 | if (IS_ENABLED(CONFIG_SPL_BUILD)) { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 509 | plat = dev_get_plat(dev); |
Patrick Delaunay | 1dffeaf | 2020-07-31 16:31:51 +0200 | [diff] [blame] | 510 | |
| 511 | for (otp = 57; otp <= BSEC_OTP_MAX_VALUE; otp++) |
| 512 | if (!bsec_read_SR_lock(plat->base, otp)) |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 513 | bsec_shadow_register(dev, plat->base, otp); |
Patrick Delaunay | 1dffeaf | 2020-07-31 16:31:51 +0200 | [diff] [blame] | 514 | } |
Patrick Delaunay | f95686b | 2019-02-27 17:01:28 +0100 | [diff] [blame] | 515 | |
| 516 | return 0; |
| 517 | } |
Patrick Delaunay | f95686b | 2019-02-27 17:01:28 +0100 | [diff] [blame] | 518 | |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 519 | static const struct udevice_id stm32mp_bsec_ids[] = { |
Patrick Delaunay | bdd7136 | 2019-02-27 17:01:27 +0100 | [diff] [blame] | 520 | { .compatible = "st,stm32mp15-bsec" }, |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 521 | {} |
| 522 | }; |
| 523 | |
| 524 | U_BOOT_DRIVER(stm32mp_bsec) = { |
| 525 | .name = "stm32mp_bsec", |
| 526 | .id = UCLASS_MISC, |
| 527 | .of_match = stm32mp_bsec_ids, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 528 | .of_to_plat = stm32mp_bsec_of_to_plat, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 529 | .plat_auto = sizeof(struct stm32mp_bsec_plat), |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 530 | .ops = &stm32mp_bsec_ops, |
Patrick Delaunay | f95686b | 2019-02-27 17:01:28 +0100 | [diff] [blame] | 531 | .probe = stm32mp_bsec_probe, |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 532 | }; |
Patrick Delaunay | 6332c04 | 2020-06-16 18:27:44 +0200 | [diff] [blame] | 533 | |
| 534 | bool bsec_dbgswenable(void) |
| 535 | { |
| 536 | struct udevice *dev; |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 537 | struct stm32mp_bsec_plat *plat; |
Patrick Delaunay | 6332c04 | 2020-06-16 18:27:44 +0200 | [diff] [blame] | 538 | int ret; |
| 539 | |
| 540 | ret = uclass_get_device_by_driver(UCLASS_MISC, |
Simon Glass | 65130cd | 2020-12-28 20:34:56 -0700 | [diff] [blame] | 541 | DM_DRIVER_GET(stm32mp_bsec), &dev); |
Patrick Delaunay | 6332c04 | 2020-06-16 18:27:44 +0200 | [diff] [blame] | 542 | if (ret || !dev) { |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 543 | log_debug("bsec driver not available\n"); |
Patrick Delaunay | 6332c04 | 2020-06-16 18:27:44 +0200 | [diff] [blame] | 544 | return false; |
| 545 | } |
| 546 | |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 547 | plat = dev_get_plat(dev); |
Patrick Delaunay | 6332c04 | 2020-06-16 18:27:44 +0200 | [diff] [blame] | 548 | if (readl(plat->base + BSEC_DENABLE_OFF) & BSEC_DENABLE_DBGSWENABLE) |
| 549 | return true; |
| 550 | |
| 551 | return false; |
| 552 | } |