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> |
Patrick Delaunay | d538829 | 2023-01-06 13:20:15 +0100 | [diff] [blame] | 13 | #include <tee.h> |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 14 | #include <asm/io.h> |
Patrick Delaunay | 6332c04 | 2020-06-16 18:27:44 +0200 | [diff] [blame] | 15 | #include <asm/arch/bsec.h> |
Patrick Delaunay | 7858d7e | 2019-02-12 11:44:40 +0100 | [diff] [blame] | 16 | #include <asm/arch/stm32mp1_smc.h> |
Patrick Delaunay | d538829 | 2023-01-06 13:20:15 +0100 | [diff] [blame] | 17 | #include <dm/device.h> |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 18 | #include <dm/device_compat.h> |
Patrick Delaunay | 7858d7e | 2019-02-12 11:44:40 +0100 | [diff] [blame] | 19 | #include <linux/arm-smccc.h> |
Patrick Delaunay | 2fa55eb | 2019-04-18 17:32:39 +0200 | [diff] [blame] | 20 | #include <linux/iopoll.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 21 | #include <linux/printk.h> |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 22 | |
| 23 | #define BSEC_OTP_MAX_VALUE 95 |
Patrick Delaunay | b9201a7 | 2022-02-15 16:08:50 +0100 | [diff] [blame] | 24 | #define BSEC_OTP_UPPER_START 32 |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 25 | #define BSEC_TIMEOUT_US 10000 |
| 26 | |
| 27 | /* BSEC REGISTER OFFSET (base relative) */ |
| 28 | #define BSEC_OTP_CONF_OFF 0x000 |
| 29 | #define BSEC_OTP_CTRL_OFF 0x004 |
| 30 | #define BSEC_OTP_WRDATA_OFF 0x008 |
| 31 | #define BSEC_OTP_STATUS_OFF 0x00C |
| 32 | #define BSEC_OTP_LOCK_OFF 0x010 |
Patrick Delaunay | 6332c04 | 2020-06-16 18:27:44 +0200 | [diff] [blame] | 33 | #define BSEC_DENABLE_OFF 0x014 |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 34 | #define BSEC_DISTURBED_OFF 0x01C |
| 35 | #define BSEC_ERROR_OFF 0x034 |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 36 | #define BSEC_WRLOCK_OFF 0x04C /* OTP write permananet lock */ |
| 37 | #define BSEC_SPLOCK_OFF 0x064 /* OTP write sticky lock */ |
| 38 | #define BSEC_SWLOCK_OFF 0x07C /* shadow write sticky lock */ |
| 39 | #define BSEC_SRLOCK_OFF 0x094 /* shadow read sticky lock */ |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 40 | #define BSEC_OTP_DATA_OFF 0x200 |
| 41 | |
| 42 | /* BSEC_CONFIGURATION Register MASK */ |
| 43 | #define BSEC_CONF_POWER_UP 0x001 |
| 44 | |
| 45 | /* BSEC_CONTROL Register */ |
| 46 | #define BSEC_READ 0x000 |
| 47 | #define BSEC_WRITE 0x100 |
Patrick Delaunay | b9201a7 | 2022-02-15 16:08:50 +0100 | [diff] [blame] | 48 | #define BSEC_LOCK 0x200 |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 49 | |
| 50 | /* LOCK Register */ |
| 51 | #define OTP_LOCK_MASK 0x1F |
| 52 | #define OTP_LOCK_BANK_SHIFT 0x05 |
| 53 | #define OTP_LOCK_BIT_MASK 0x01 |
| 54 | |
| 55 | /* STATUS Register */ |
| 56 | #define BSEC_MODE_BUSY_MASK 0x08 |
| 57 | #define BSEC_MODE_PROGFAIL_MASK 0x10 |
| 58 | #define BSEC_MODE_PWR_MASK 0x20 |
| 59 | |
Patrick Delaunay | 6332c04 | 2020-06-16 18:27:44 +0200 | [diff] [blame] | 60 | /* DENABLE Register */ |
| 61 | #define BSEC_DENABLE_DBGSWENABLE BIT(10) |
| 62 | |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 63 | /* |
| 64 | * OTP Lock services definition |
| 65 | * Value must corresponding to the bit number in the register |
| 66 | */ |
| 67 | #define BSEC_LOCK_PROGRAM 0x04 |
| 68 | |
Patrick Delaunay | d538829 | 2023-01-06 13:20:15 +0100 | [diff] [blame] | 69 | #define PTA_BSEC_UUID { 0x94cf71ad, 0x80e6, 0x40b5, \ |
| 70 | { 0xa7, 0xc6, 0x3d, 0xc5, 0x01, 0xeb, 0x28, 0x03 } } |
| 71 | |
| 72 | /* |
| 73 | * Read OTP memory |
| 74 | * |
| 75 | * [in] value[0].a OTP start offset in byte |
| 76 | * [in] value[0].b Access type (0:shadow, 1:fuse, 2:lock) |
| 77 | * [out] memref[1].buffer Output buffer to store read values |
| 78 | * [out] memref[1].size Size of OTP to be read |
| 79 | * |
| 80 | * Return codes: |
| 81 | * TEE_SUCCESS - Invoke command success |
| 82 | * TEE_ERROR_BAD_PARAMETERS - Incorrect input param |
| 83 | * TEE_ERROR_ACCESS_DENIED - OTP not accessible by caller |
| 84 | */ |
| 85 | #define PTA_BSEC_READ_MEM 0x0 |
| 86 | |
Patrick Delaunay | b9201a7 | 2022-02-15 16:08:50 +0100 | [diff] [blame] | 87 | /* |
Patrick Delaunay | d538829 | 2023-01-06 13:20:15 +0100 | [diff] [blame] | 88 | * Write OTP memory |
| 89 | * |
| 90 | * [in] value[0].a OTP start offset in byte |
| 91 | * [in] value[0].b Access type (0:shadow, 1:fuse, 2:lock) |
| 92 | * [in] memref[1].buffer Input buffer to read values |
| 93 | * [in] memref[1].size Size of OTP to be written |
| 94 | * |
| 95 | * Return codes: |
| 96 | * TEE_SUCCESS - Invoke command success |
| 97 | * TEE_ERROR_BAD_PARAMETERS - Incorrect input param |
| 98 | * TEE_ERROR_ACCESS_DENIED - OTP not accessible by caller |
Patrick Delaunay | b9201a7 | 2022-02-15 16:08:50 +0100 | [diff] [blame] | 99 | */ |
Patrick Delaunay | d538829 | 2023-01-06 13:20:15 +0100 | [diff] [blame] | 100 | #define PTA_BSEC_WRITE_MEM 0x1 |
| 101 | |
| 102 | /* value of PTA_BSEC access type = value[in] b */ |
| 103 | #define SHADOW_ACCESS 0 |
| 104 | #define FUSE_ACCESS 1 |
| 105 | #define LOCK_ACCESS 2 |
Patrick Delaunay | b9201a7 | 2022-02-15 16:08:50 +0100 | [diff] [blame] | 106 | |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 107 | /** |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 108 | * bsec_lock() - manage lock for each type SR/SP/SW |
| 109 | * @address: address of bsec IP register |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 110 | * @otp: otp number (0 - BSEC_OTP_MAX_VALUE) |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 111 | * Return: true if locked else false |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 112 | */ |
Patrice Chotard | 1115c30 | 2023-10-27 16:43:00 +0200 | [diff] [blame] | 113 | static bool bsec_read_lock(void __iomem *address, u32 otp) |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 114 | { |
| 115 | u32 bit; |
| 116 | u32 bank; |
| 117 | |
| 118 | bit = 1 << (otp & OTP_LOCK_MASK); |
| 119 | bank = ((otp >> OTP_LOCK_BANK_SHIFT) & OTP_LOCK_MASK) * sizeof(u32); |
| 120 | |
Patrice Chotard | 1115c30 | 2023-10-27 16:43:00 +0200 | [diff] [blame] | 121 | return !!(readl((address + bank)) & bit); |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /** |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 125 | * bsec_check_error() - Check status of one otp |
| 126 | * @base: base address of bsec IP |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 127 | * @otp: otp number (0 - BSEC_OTP_MAX_VALUE) |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 128 | * Return: 0 if no error, -EAGAIN or -ENOTSUPP |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 129 | */ |
Patrice Chotard | 1115c30 | 2023-10-27 16:43:00 +0200 | [diff] [blame] | 130 | static u32 bsec_check_error(void __iomem *base, u32 otp) |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 131 | { |
| 132 | u32 bit; |
| 133 | u32 bank; |
| 134 | |
| 135 | bit = 1 << (otp & OTP_LOCK_MASK); |
| 136 | bank = ((otp >> OTP_LOCK_BANK_SHIFT) & OTP_LOCK_MASK) * sizeof(u32); |
| 137 | |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 138 | if (readl(base + BSEC_DISTURBED_OFF + bank) & bit) |
| 139 | return -EAGAIN; |
| 140 | else if (readl(base + BSEC_ERROR_OFF + bank) & bit) |
| 141 | return -ENOTSUPP; |
| 142 | |
| 143 | return 0; |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /** |
| 147 | * bsec_read_SR_lock() - read SR lock (Shadowing) |
| 148 | * @base: base address of bsec IP |
| 149 | * @otp: otp number (0 - BSEC_OTP_MAX_VALUE) |
| 150 | * Return: true if locked else false |
| 151 | */ |
Patrice Chotard | 1115c30 | 2023-10-27 16:43:00 +0200 | [diff] [blame] | 152 | static bool bsec_read_SR_lock(void __iomem *base, u32 otp) |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 153 | { |
| 154 | return bsec_read_lock(base + BSEC_SRLOCK_OFF, otp); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * bsec_read_SP_lock() - read SP lock (program Lock) |
| 159 | * @base: base address of bsec IP |
| 160 | * @otp: otp number (0 - BSEC_OTP_MAX_VALUE) |
| 161 | * Return: true if locked else false |
| 162 | */ |
Patrice Chotard | 1115c30 | 2023-10-27 16:43:00 +0200 | [diff] [blame] | 163 | static bool bsec_read_SP_lock(void __iomem *base, u32 otp) |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 164 | { |
| 165 | return bsec_read_lock(base + BSEC_SPLOCK_OFF, otp); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * bsec_SW_lock() - manage SW lock (Write in Shadow) |
| 170 | * @base: base address of bsec IP |
| 171 | * @otp: otp number (0 - BSEC_OTP_MAX_VALUE) |
| 172 | * Return: true if locked else false |
| 173 | */ |
Patrice Chotard | 1115c30 | 2023-10-27 16:43:00 +0200 | [diff] [blame] | 174 | static bool bsec_read_SW_lock(void __iomem *base, u32 otp) |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 175 | { |
| 176 | return bsec_read_lock(base + BSEC_SWLOCK_OFF, otp); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * bsec_power_safmem() - Activate or deactivate safmem power |
| 181 | * @base: base address of bsec IP |
| 182 | * @power: true to power up , false to power down |
| 183 | * Return: 0 if succeed |
| 184 | */ |
Patrice Chotard | 1115c30 | 2023-10-27 16:43:00 +0200 | [diff] [blame] | 185 | static int bsec_power_safmem(void __iomem *base, bool power) |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 186 | { |
| 187 | u32 val; |
| 188 | u32 mask; |
| 189 | |
| 190 | if (power) { |
| 191 | setbits_le32(base + BSEC_OTP_CONF_OFF, BSEC_CONF_POWER_UP); |
| 192 | mask = BSEC_MODE_PWR_MASK; |
| 193 | } else { |
| 194 | clrbits_le32(base + BSEC_OTP_CONF_OFF, BSEC_CONF_POWER_UP); |
| 195 | mask = 0; |
| 196 | } |
| 197 | |
| 198 | /* waiting loop */ |
| 199 | return readl_poll_timeout(base + BSEC_OTP_STATUS_OFF, |
| 200 | val, (val & BSEC_MODE_PWR_MASK) == mask, |
| 201 | BSEC_TIMEOUT_US); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * bsec_shadow_register() - copy safmen otp to bsec data |
Patrick Delaunay | 6292db1 | 2022-02-15 16:08:51 +0100 | [diff] [blame] | 206 | * @dev: bsec IP device |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 207 | * @base: base address of bsec IP |
| 208 | * @otp: otp number (0 - BSEC_OTP_MAX_VALUE) |
| 209 | * Return: 0 if no error |
| 210 | */ |
Patrice Chotard | 1115c30 | 2023-10-27 16:43:00 +0200 | [diff] [blame] | 211 | static int bsec_shadow_register(struct udevice *dev, void __iomem *base, u32 otp) |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 212 | { |
| 213 | u32 val; |
| 214 | int ret; |
| 215 | bool power_up = false; |
| 216 | |
| 217 | /* check if shadowing of otp is locked */ |
| 218 | if (bsec_read_SR_lock(base, otp)) |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 219 | dev_dbg(dev, "OTP %d is locked and refreshed with 0\n", |
| 220 | otp); |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 221 | |
| 222 | /* check if safemem is power up */ |
| 223 | val = readl(base + BSEC_OTP_STATUS_OFF); |
| 224 | if (!(val & BSEC_MODE_PWR_MASK)) { |
| 225 | ret = bsec_power_safmem(base, true); |
| 226 | if (ret) |
| 227 | return ret; |
Patrick Delaunay | f95686b | 2019-02-27 17:01:28 +0100 | [diff] [blame] | 228 | power_up = true; |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 229 | } |
| 230 | /* set BSEC_OTP_CTRL_OFF with the otp value*/ |
| 231 | writel(otp | BSEC_READ, base + BSEC_OTP_CTRL_OFF); |
| 232 | |
| 233 | /* check otp status*/ |
| 234 | ret = readl_poll_timeout(base + BSEC_OTP_STATUS_OFF, |
| 235 | val, (val & BSEC_MODE_BUSY_MASK) == 0, |
| 236 | BSEC_TIMEOUT_US); |
| 237 | if (ret) |
| 238 | return ret; |
| 239 | |
| 240 | ret = bsec_check_error(base, otp); |
| 241 | |
| 242 | if (power_up) |
| 243 | bsec_power_safmem(base, false); |
| 244 | |
| 245 | return ret; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * bsec_read_shadow() - read an otp data value from shadow |
Patrick Delaunay | 6292db1 | 2022-02-15 16:08:51 +0100 | [diff] [blame] | 250 | * @dev: bsec IP device |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 251 | * @base: base address of bsec IP |
| 252 | * @val: read value |
| 253 | * @otp: otp number (0 - BSEC_OTP_MAX_VALUE) |
| 254 | * Return: 0 if no error |
| 255 | */ |
Patrice Chotard | 1115c30 | 2023-10-27 16:43:00 +0200 | [diff] [blame] | 256 | static int bsec_read_shadow(struct udevice *dev, void __iomem *base, u32 *val, |
| 257 | u32 otp) |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 258 | { |
| 259 | *val = readl(base + BSEC_OTP_DATA_OFF + otp * sizeof(u32)); |
| 260 | |
| 261 | return bsec_check_error(base, otp); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * bsec_write_shadow() - write value in BSEC data register in shadow |
Patrick Delaunay | 6292db1 | 2022-02-15 16:08:51 +0100 | [diff] [blame] | 266 | * @dev: bsec IP device |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 267 | * @base: base address of bsec IP |
| 268 | * @val: value to write |
| 269 | * @otp: otp number (0 - BSEC_OTP_MAX_VALUE) |
| 270 | * Return: 0 if no error |
| 271 | */ |
Patrice Chotard | 1115c30 | 2023-10-27 16:43:00 +0200 | [diff] [blame] | 272 | static int bsec_write_shadow(struct udevice *dev, void __iomem *base, u32 val, u32 otp) |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 273 | { |
| 274 | /* check if programming of otp is locked */ |
| 275 | if (bsec_read_SW_lock(base, otp)) |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 276 | 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] | 277 | |
| 278 | writel(val, base + BSEC_OTP_DATA_OFF + otp * sizeof(u32)); |
| 279 | |
| 280 | return bsec_check_error(base, otp); |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * bsec_program_otp() - program a bit in SAFMEM |
Patrick Delaunay | 6292db1 | 2022-02-15 16:08:51 +0100 | [diff] [blame] | 285 | * @dev: bsec IP device |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 286 | * @base: base address of bsec IP |
| 287 | * @val: value to program |
| 288 | * @otp: otp number (0 - BSEC_OTP_MAX_VALUE) |
| 289 | * after the function the otp data is not refreshed in shadow |
| 290 | * Return: 0 if no error |
| 291 | */ |
Patrice Chotard | 1115c30 | 2023-10-27 16:43:00 +0200 | [diff] [blame] | 292 | static int bsec_program_otp(struct udevice *dev, void __iomem *base, u32 val, u32 otp) |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 293 | { |
| 294 | u32 ret; |
| 295 | bool power_up = false; |
| 296 | |
| 297 | if (bsec_read_SP_lock(base, otp)) |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 298 | dev_dbg(dev, "OTP %d locked, prog will be ignore\n", otp); |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 299 | |
| 300 | if (readl(base + BSEC_OTP_LOCK_OFF) & (1 << BSEC_LOCK_PROGRAM)) |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 301 | dev_dbg(dev, "Global lock, prog will be ignore\n"); |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 302 | |
| 303 | /* check if safemem is power up */ |
| 304 | if (!(readl(base + BSEC_OTP_STATUS_OFF) & BSEC_MODE_PWR_MASK)) { |
| 305 | ret = bsec_power_safmem(base, true); |
| 306 | if (ret) |
| 307 | return ret; |
| 308 | |
| 309 | power_up = true; |
| 310 | } |
| 311 | /* set value in write register*/ |
| 312 | writel(val, base + BSEC_OTP_WRDATA_OFF); |
| 313 | |
| 314 | /* set BSEC_OTP_CTRL_OFF with the otp value */ |
| 315 | writel(otp | BSEC_WRITE, base + BSEC_OTP_CTRL_OFF); |
| 316 | |
| 317 | /* check otp status*/ |
| 318 | ret = readl_poll_timeout(base + BSEC_OTP_STATUS_OFF, |
| 319 | val, (val & BSEC_MODE_BUSY_MASK) == 0, |
| 320 | BSEC_TIMEOUT_US); |
| 321 | if (ret) |
| 322 | return ret; |
| 323 | |
| 324 | if (val & BSEC_MODE_PROGFAIL_MASK) |
| 325 | ret = -EACCES; |
| 326 | else |
| 327 | ret = bsec_check_error(base, otp); |
| 328 | |
| 329 | if (power_up) |
| 330 | bsec_power_safmem(base, false); |
| 331 | |
| 332 | return ret; |
| 333 | } |
| 334 | |
Patrick Delaunay | b9201a7 | 2022-02-15 16:08:50 +0100 | [diff] [blame] | 335 | /** |
| 336 | * bsec_permanent_lock_otp() - permanent lock of OTP in SAFMEM |
| 337 | * @dev: bsec IP device |
| 338 | * @base: base address of bsec IP |
| 339 | * @otp: otp number (0 - BSEC_OTP_MAX_VALUE) |
| 340 | * Return: 0 if no error |
| 341 | */ |
Patrice Chotard | 1115c30 | 2023-10-27 16:43:00 +0200 | [diff] [blame] | 342 | static int bsec_permanent_lock_otp(struct udevice *dev, void __iomem *base, uint32_t otp) |
Patrick Delaunay | b9201a7 | 2022-02-15 16:08:50 +0100 | [diff] [blame] | 343 | { |
| 344 | int ret; |
| 345 | bool power_up = false; |
| 346 | u32 val, addr; |
| 347 | |
| 348 | /* check if safemem is power up */ |
| 349 | if (!(readl(base + BSEC_OTP_STATUS_OFF) & BSEC_MODE_PWR_MASK)) { |
| 350 | ret = bsec_power_safmem(base, true); |
| 351 | if (ret) |
| 352 | return ret; |
| 353 | |
| 354 | power_up = true; |
| 355 | } |
| 356 | |
| 357 | /* |
| 358 | * low OTPs = 2 bits word for low OTPs, 1 bits per word for upper OTP |
| 359 | * and only 16 bits used in WRDATA |
| 360 | */ |
| 361 | if (otp < BSEC_OTP_UPPER_START) { |
| 362 | addr = otp / 8; |
| 363 | val = 0x03 << ((otp * 2) & 0xF); |
| 364 | } else { |
| 365 | addr = BSEC_OTP_UPPER_START / 8 + |
| 366 | ((otp - BSEC_OTP_UPPER_START) / 16); |
| 367 | val = 0x01 << (otp & 0xF); |
| 368 | } |
| 369 | |
| 370 | /* set value in write register*/ |
| 371 | writel(val, base + BSEC_OTP_WRDATA_OFF); |
| 372 | |
| 373 | /* set BSEC_OTP_CTRL_OFF with the otp addr and lock request*/ |
| 374 | writel(addr | BSEC_WRITE | BSEC_LOCK, base + BSEC_OTP_CTRL_OFF); |
| 375 | |
| 376 | /* check otp status*/ |
| 377 | ret = readl_poll_timeout(base + BSEC_OTP_STATUS_OFF, |
| 378 | val, (val & BSEC_MODE_BUSY_MASK) == 0, |
| 379 | BSEC_TIMEOUT_US); |
| 380 | if (ret) |
| 381 | return ret; |
| 382 | |
| 383 | if (val & BSEC_MODE_PROGFAIL_MASK) |
| 384 | ret = -EACCES; |
| 385 | else |
| 386 | ret = bsec_check_error(base, otp); |
| 387 | |
| 388 | if (power_up) |
| 389 | bsec_power_safmem(base, false); |
| 390 | |
| 391 | return ret; |
| 392 | } |
| 393 | |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 394 | /* BSEC MISC driver *******************************************************/ |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 395 | struct stm32mp_bsec_plat { |
Patrice Chotard | 1115c30 | 2023-10-27 16:43:00 +0200 | [diff] [blame] | 396 | void __iomem *base; |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 397 | }; |
| 398 | |
Patrick Delaunay | d538829 | 2023-01-06 13:20:15 +0100 | [diff] [blame] | 399 | struct stm32mp_bsec_priv { |
| 400 | struct udevice *tee; |
| 401 | }; |
| 402 | |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 403 | static int stm32mp_bsec_read_otp(struct udevice *dev, u32 *val, u32 otp) |
| 404 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 405 | struct stm32mp_bsec_plat *plat; |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 406 | u32 tmp_data = 0; |
| 407 | int ret; |
| 408 | |
Patrick Delaunay | 72a5762 | 2021-10-11 09:52:50 +0200 | [diff] [blame] | 409 | if (IS_ENABLED(CONFIG_ARM_SMCCC) && !IS_ENABLED(CONFIG_SPL_BUILD)) |
Patrick Delaunay | 1dffeaf | 2020-07-31 16:31:51 +0200 | [diff] [blame] | 410 | return stm32_smc(STM32_SMC_BSEC, |
| 411 | STM32_SMC_READ_OTP, |
| 412 | otp, 0, val); |
| 413 | |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 414 | plat = dev_get_plat(dev); |
Patrick Delaunay | 1dffeaf | 2020-07-31 16:31:51 +0200 | [diff] [blame] | 415 | |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 416 | /* read current shadow value */ |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 417 | ret = bsec_read_shadow(dev, plat->base, &tmp_data, otp); |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 418 | if (ret) |
| 419 | return ret; |
| 420 | |
| 421 | /* copy otp in shadow */ |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 422 | ret = bsec_shadow_register(dev, plat->base, otp); |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 423 | if (ret) |
| 424 | return ret; |
| 425 | |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 426 | ret = bsec_read_shadow(dev, plat->base, val, otp); |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 427 | if (ret) |
| 428 | return ret; |
| 429 | |
| 430 | /* restore shadow value */ |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 431 | ret = bsec_write_shadow(dev, plat->base, tmp_data, otp); |
Patrick Delaunay | 1dffeaf | 2020-07-31 16:31:51 +0200 | [diff] [blame] | 432 | |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 433 | return ret; |
| 434 | } |
| 435 | |
| 436 | static int stm32mp_bsec_read_shadow(struct udevice *dev, u32 *val, u32 otp) |
| 437 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 438 | struct stm32mp_bsec_plat *plat; |
Patrick Delaunay | 1dffeaf | 2020-07-31 16:31:51 +0200 | [diff] [blame] | 439 | |
Patrick Delaunay | 72a5762 | 2021-10-11 09:52:50 +0200 | [diff] [blame] | 440 | if (IS_ENABLED(CONFIG_ARM_SMCCC) && !IS_ENABLED(CONFIG_SPL_BUILD)) |
Patrick Delaunay | 1dffeaf | 2020-07-31 16:31:51 +0200 | [diff] [blame] | 441 | return stm32_smc(STM32_SMC_BSEC, |
| 442 | STM32_SMC_READ_SHADOW, |
| 443 | otp, 0, val); |
| 444 | |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 445 | plat = dev_get_plat(dev); |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 446 | |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 447 | return bsec_read_shadow(dev, plat->base, val, otp); |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 448 | } |
| 449 | |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 450 | static int stm32mp_bsec_read_lock(struct udevice *dev, u32 *val, u32 otp) |
| 451 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 452 | struct stm32mp_bsec_plat *plat = dev_get_plat(dev); |
Patrick Delaunay | b9201a7 | 2022-02-15 16:08:50 +0100 | [diff] [blame] | 453 | u32 wrlock; |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 454 | |
| 455 | /* return OTP permanent write lock status */ |
Patrick Delaunay | b9201a7 | 2022-02-15 16:08:50 +0100 | [diff] [blame] | 456 | wrlock = bsec_read_lock(plat->base + BSEC_WRLOCK_OFF, otp); |
| 457 | |
| 458 | *val = 0; |
| 459 | if (wrlock) |
| 460 | *val = BSEC_LOCK_PERM; |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 461 | |
| 462 | return 0; |
| 463 | } |
| 464 | |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 465 | static int stm32mp_bsec_write_otp(struct udevice *dev, u32 val, u32 otp) |
| 466 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 467 | struct stm32mp_bsec_plat *plat; |
Patrick Delaunay | 1dffeaf | 2020-07-31 16:31:51 +0200 | [diff] [blame] | 468 | |
Patrick Delaunay | 72a5762 | 2021-10-11 09:52:50 +0200 | [diff] [blame] | 469 | if (IS_ENABLED(CONFIG_ARM_SMCCC) && !IS_ENABLED(CONFIG_SPL_BUILD)) |
Patrick Delaunay | 1dffeaf | 2020-07-31 16:31:51 +0200 | [diff] [blame] | 470 | return stm32_smc_exec(STM32_SMC_BSEC, |
| 471 | STM32_SMC_PROG_OTP, |
| 472 | otp, val); |
| 473 | |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 474 | plat = dev_get_plat(dev); |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 475 | |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 476 | return bsec_program_otp(dev, plat->base, val, otp); |
Patrick Delaunay | 1dffeaf | 2020-07-31 16:31:51 +0200 | [diff] [blame] | 477 | |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | static int stm32mp_bsec_write_shadow(struct udevice *dev, u32 val, u32 otp) |
| 481 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 482 | struct stm32mp_bsec_plat *plat; |
Patrick Delaunay | 1dffeaf | 2020-07-31 16:31:51 +0200 | [diff] [blame] | 483 | |
Patrick Delaunay | 72a5762 | 2021-10-11 09:52:50 +0200 | [diff] [blame] | 484 | if (IS_ENABLED(CONFIG_ARM_SMCCC) && !IS_ENABLED(CONFIG_SPL_BUILD)) |
Patrick Delaunay | 1dffeaf | 2020-07-31 16:31:51 +0200 | [diff] [blame] | 485 | return stm32_smc_exec(STM32_SMC_BSEC, |
| 486 | STM32_SMC_WRITE_SHADOW, |
| 487 | otp, val); |
| 488 | |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 489 | plat = dev_get_plat(dev); |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 490 | |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 491 | return bsec_write_shadow(dev, plat->base, val, otp); |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 492 | } |
| 493 | |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 494 | static int stm32mp_bsec_write_lock(struct udevice *dev, u32 val, u32 otp) |
| 495 | { |
Patrick Delaunay | b9201a7 | 2022-02-15 16:08:50 +0100 | [diff] [blame] | 496 | struct stm32mp_bsec_plat *plat; |
| 497 | |
| 498 | /* only permanent write lock is supported in U-Boot */ |
| 499 | if (!(val & BSEC_LOCK_PERM)) { |
| 500 | dev_dbg(dev, "lock option without BSEC_LOCK_PERM: %x\n", val); |
| 501 | return 0; /* nothing to do */ |
| 502 | } |
Patrick Delaunay | 1dffeaf | 2020-07-31 16:31:51 +0200 | [diff] [blame] | 503 | |
Patrick Delaunay | b9201a7 | 2022-02-15 16:08:50 +0100 | [diff] [blame] | 504 | if (IS_ENABLED(CONFIG_ARM_SMCCC) && !IS_ENABLED(CONFIG_SPL_BUILD)) |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 505 | return stm32_smc_exec(STM32_SMC_BSEC, |
| 506 | STM32_SMC_WRLOCK_OTP, |
| 507 | otp, 0); |
Patrick Delaunay | b9201a7 | 2022-02-15 16:08:50 +0100 | [diff] [blame] | 508 | |
| 509 | plat = dev_get_plat(dev); |
| 510 | |
| 511 | return bsec_permanent_lock_otp(dev, plat->base, otp); |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 512 | } |
| 513 | |
Patrick Delaunay | d538829 | 2023-01-06 13:20:15 +0100 | [diff] [blame] | 514 | static int bsec_pta_open_session(struct udevice *tee, u32 *tee_session) |
| 515 | { |
| 516 | const struct tee_optee_ta_uuid uuid = PTA_BSEC_UUID; |
| 517 | struct tee_open_session_arg arg; |
| 518 | int rc; |
| 519 | |
| 520 | memset(&arg, 0, sizeof(arg)); |
| 521 | tee_optee_ta_uuid_to_octets(arg.uuid, &uuid); |
| 522 | arg.clnt_login = TEE_LOGIN_REE_KERNEL; |
| 523 | rc = tee_open_session(tee, &arg, 0, NULL); |
| 524 | if (rc < 0) |
| 525 | return -ENODEV; |
| 526 | |
| 527 | *tee_session = arg.session; |
| 528 | |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | static int bsec_optee_open(struct udevice *dev) |
| 533 | { |
| 534 | struct stm32mp_bsec_priv *priv = dev_get_priv(dev); |
| 535 | struct udevice *tee; |
| 536 | u32 tee_session; |
| 537 | int rc; |
| 538 | |
| 539 | tee = tee_find_device(NULL, NULL, NULL, NULL); |
| 540 | if (!tee) |
| 541 | return -ENODEV; |
| 542 | |
| 543 | /* try to open the STM32 BSEC TA */ |
| 544 | rc = bsec_pta_open_session(tee, &tee_session); |
| 545 | if (rc) |
| 546 | return rc; |
| 547 | |
| 548 | tee_close_session(tee, tee_session); |
| 549 | |
| 550 | priv->tee = tee; |
| 551 | |
| 552 | return 0; |
| 553 | } |
| 554 | |
| 555 | static int bsec_optee_pta(struct udevice *dev, int cmd, int type, int offset, |
| 556 | void *buff, ulong size) |
| 557 | { |
| 558 | struct stm32mp_bsec_priv *priv = dev_get_priv(dev); |
| 559 | u32 tee_session; |
| 560 | struct tee_invoke_arg arg; |
| 561 | struct tee_param param[2]; |
| 562 | struct tee_shm *fw_shm; |
| 563 | int rc; |
| 564 | |
| 565 | rc = bsec_pta_open_session(priv->tee, &tee_session); |
| 566 | if (rc) |
| 567 | return rc; |
| 568 | |
| 569 | rc = tee_shm_register(priv->tee, buff, size, 0, &fw_shm); |
| 570 | if (rc) |
| 571 | goto close_session; |
| 572 | |
| 573 | memset(&arg, 0, sizeof(arg)); |
| 574 | arg.func = cmd; |
| 575 | arg.session = tee_session; |
| 576 | |
| 577 | memset(param, 0, sizeof(param)); |
| 578 | |
| 579 | param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT; |
| 580 | param[0].u.value.a = offset; |
| 581 | param[0].u.value.b = type; |
| 582 | |
| 583 | if (cmd == PTA_BSEC_WRITE_MEM) |
| 584 | param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INPUT; |
| 585 | else |
| 586 | param[1].attr = TEE_PARAM_ATTR_TYPE_MEMREF_OUTPUT; |
| 587 | |
| 588 | param[1].u.memref.shm = fw_shm; |
| 589 | param[1].u.memref.size = size; |
| 590 | |
| 591 | rc = tee_invoke_func(priv->tee, &arg, 2, param); |
| 592 | if (rc < 0 || arg.ret != 0) { |
| 593 | dev_err(priv->tee, |
| 594 | "PTA_BSEC invoke failed TEE err: %x, err:%x\n", |
| 595 | arg.ret, rc); |
| 596 | if (!rc) |
| 597 | rc = -EIO; |
| 598 | } |
| 599 | |
| 600 | tee_shm_free(fw_shm); |
| 601 | |
| 602 | close_session: |
| 603 | tee_close_session(priv->tee, tee_session); |
| 604 | |
| 605 | return rc; |
| 606 | } |
| 607 | |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 608 | static int stm32mp_bsec_read(struct udevice *dev, int offset, |
| 609 | void *buf, int size) |
| 610 | { |
Patrick Delaunay | d538829 | 2023-01-06 13:20:15 +0100 | [diff] [blame] | 611 | struct stm32mp_bsec_priv *priv = dev_get_priv(dev); |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 612 | int ret; |
| 613 | int i; |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 614 | bool shadow = true, lock = false; |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 615 | int nb_otp = size / sizeof(u32); |
Patrick Delaunay | d538829 | 2023-01-06 13:20:15 +0100 | [diff] [blame] | 616 | int otp, cmd; |
Patrick Delaunay | 4c7c074 | 2019-06-21 15:26:43 +0200 | [diff] [blame] | 617 | unsigned int offs = offset; |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 618 | |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 619 | if (offs >= STM32_BSEC_LOCK_OFFSET) { |
| 620 | offs -= STM32_BSEC_LOCK_OFFSET; |
| 621 | lock = true; |
| 622 | } else if (offs >= STM32_BSEC_OTP_OFFSET) { |
Patrick Delaunay | 4c7c074 | 2019-06-21 15:26:43 +0200 | [diff] [blame] | 623 | offs -= STM32_BSEC_OTP_OFFSET; |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 624 | shadow = false; |
| 625 | } |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 626 | |
Patrick Delaunay | 8275ff3 | 2023-04-27 15:36:34 +0200 | [diff] [blame] | 627 | if ((offs % 4) || (size % 4) || !size) |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 628 | return -EINVAL; |
Patrick Delaunay | 7e5f8e3 | 2019-08-02 13:08:02 +0200 | [diff] [blame] | 629 | |
Patrick Delaunay | d538829 | 2023-01-06 13:20:15 +0100 | [diff] [blame] | 630 | if (IS_ENABLED(CONFIG_OPTEE) && priv->tee) { |
| 631 | cmd = FUSE_ACCESS; |
| 632 | if (shadow) |
| 633 | cmd = SHADOW_ACCESS; |
| 634 | if (lock) |
| 635 | cmd = LOCK_ACCESS; |
| 636 | ret = bsec_optee_pta(dev, PTA_BSEC_READ_MEM, cmd, offs, buf, size); |
| 637 | if (ret) |
| 638 | return ret; |
| 639 | |
| 640 | return size; |
| 641 | } |
| 642 | |
Patrick Delaunay | 7e5f8e3 | 2019-08-02 13:08:02 +0200 | [diff] [blame] | 643 | otp = offs / sizeof(u32); |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 644 | |
Patrick Delaunay | 7e5f8e3 | 2019-08-02 13:08:02 +0200 | [diff] [blame] | 645 | 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] | 646 | u32 *addr = &((u32 *)buf)[i - otp]; |
| 647 | |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 648 | if (lock) |
| 649 | ret = stm32mp_bsec_read_lock(dev, addr, i); |
| 650 | else if (shadow) |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 651 | ret = stm32mp_bsec_read_shadow(dev, addr, i); |
| 652 | else |
| 653 | ret = stm32mp_bsec_read_otp(dev, addr, i); |
| 654 | |
| 655 | if (ret) |
| 656 | break; |
| 657 | } |
Patrick Delaunay | 7e5f8e3 | 2019-08-02 13:08:02 +0200 | [diff] [blame] | 658 | if (ret) |
| 659 | return ret; |
| 660 | else |
| 661 | return (i - otp) * 4; |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | static int stm32mp_bsec_write(struct udevice *dev, int offset, |
| 665 | const void *buf, int size) |
| 666 | { |
Patrick Delaunay | d538829 | 2023-01-06 13:20:15 +0100 | [diff] [blame] | 667 | struct stm32mp_bsec_priv *priv = dev_get_priv(dev); |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 668 | int ret = 0; |
| 669 | int i; |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 670 | bool shadow = true, lock = false; |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 671 | int nb_otp = size / sizeof(u32); |
Patrick Delaunay | d538829 | 2023-01-06 13:20:15 +0100 | [diff] [blame] | 672 | int otp, cmd; |
Patrick Delaunay | 4c7c074 | 2019-06-21 15:26:43 +0200 | [diff] [blame] | 673 | unsigned int offs = offset; |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 674 | |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 675 | if (offs >= STM32_BSEC_LOCK_OFFSET) { |
| 676 | offs -= STM32_BSEC_LOCK_OFFSET; |
| 677 | lock = true; |
| 678 | } else if (offs >= STM32_BSEC_OTP_OFFSET) { |
Patrick Delaunay | 4c7c074 | 2019-06-21 15:26:43 +0200 | [diff] [blame] | 679 | offs -= STM32_BSEC_OTP_OFFSET; |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 680 | shadow = false; |
| 681 | } |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 682 | |
Patrick Delaunay | 8275ff3 | 2023-04-27 15:36:34 +0200 | [diff] [blame] | 683 | if ((offs % 4) || (size % 4) || !size) |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 684 | return -EINVAL; |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 685 | |
Patrick Delaunay | d538829 | 2023-01-06 13:20:15 +0100 | [diff] [blame] | 686 | if (IS_ENABLED(CONFIG_OPTEE) && priv->tee) { |
| 687 | cmd = FUSE_ACCESS; |
| 688 | if (shadow) |
| 689 | cmd = SHADOW_ACCESS; |
| 690 | if (lock) |
| 691 | cmd = LOCK_ACCESS; |
| 692 | ret = bsec_optee_pta(dev, PTA_BSEC_WRITE_MEM, cmd, offs, (void *)buf, size); |
| 693 | if (ret) |
| 694 | return ret; |
| 695 | |
| 696 | return size; |
| 697 | } |
| 698 | |
Patrick Delaunay | 7e5f8e3 | 2019-08-02 13:08:02 +0200 | [diff] [blame] | 699 | otp = offs / sizeof(u32); |
| 700 | |
| 701 | 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] | 702 | u32 *val = &((u32 *)buf)[i - otp]; |
| 703 | |
Patrick Delaunay | b10cddf | 2020-02-12 19:37:38 +0100 | [diff] [blame] | 704 | if (lock) |
| 705 | ret = stm32mp_bsec_write_lock(dev, *val, i); |
| 706 | else if (shadow) |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 707 | ret = stm32mp_bsec_write_shadow(dev, *val, i); |
| 708 | else |
| 709 | ret = stm32mp_bsec_write_otp(dev, *val, i); |
| 710 | if (ret) |
| 711 | break; |
| 712 | } |
Patrick Delaunay | 7e5f8e3 | 2019-08-02 13:08:02 +0200 | [diff] [blame] | 713 | if (ret) |
| 714 | return ret; |
| 715 | else |
| 716 | return (i - otp) * 4; |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | static const struct misc_ops stm32mp_bsec_ops = { |
| 720 | .read = stm32mp_bsec_read, |
| 721 | .write = stm32mp_bsec_write, |
| 722 | }; |
| 723 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 724 | static int stm32mp_bsec_of_to_plat(struct udevice *dev) |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 725 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 726 | struct stm32mp_bsec_plat *plat = dev_get_plat(dev); |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 727 | |
Patrice Chotard | 1115c30 | 2023-10-27 16:43:00 +0200 | [diff] [blame] | 728 | plat->base = dev_read_addr_ptr(dev); |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 729 | |
| 730 | return 0; |
| 731 | } |
| 732 | |
Patrick Delaunay | f95686b | 2019-02-27 17:01:28 +0100 | [diff] [blame] | 733 | static int stm32mp_bsec_probe(struct udevice *dev) |
| 734 | { |
| 735 | int otp; |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 736 | struct stm32mp_bsec_plat *plat; |
Patrick Delaunay | aaf1f96 | 2021-02-25 13:43:07 +0100 | [diff] [blame] | 737 | struct clk_bulk clk_bulk; |
| 738 | int ret; |
| 739 | |
| 740 | ret = clk_get_bulk(dev, &clk_bulk); |
| 741 | if (!ret) { |
| 742 | ret = clk_enable_bulk(&clk_bulk); |
| 743 | if (ret) |
| 744 | return ret; |
| 745 | } |
Patrick Delaunay | f95686b | 2019-02-27 17:01:28 +0100 | [diff] [blame] | 746 | |
Patrick Delaunay | d538829 | 2023-01-06 13:20:15 +0100 | [diff] [blame] | 747 | if (IS_ENABLED(CONFIG_OPTEE)) |
| 748 | bsec_optee_open(dev); |
| 749 | |
Patrick Delaunay | b6cc505 | 2020-05-25 12:19:41 +0200 | [diff] [blame] | 750 | /* |
| 751 | * update unlocked shadow for OTP cleared by the rom code |
Patrick Delaunay | dd2ca25 | 2021-10-11 09:52:48 +0200 | [diff] [blame] | 752 | * only executed in SPL, it is done in TF-A for TFABOOT |
Patrick Delaunay | b6cc505 | 2020-05-25 12:19:41 +0200 | [diff] [blame] | 753 | */ |
Patrick Delaunay | dd2ca25 | 2021-10-11 09:52:48 +0200 | [diff] [blame] | 754 | if (IS_ENABLED(CONFIG_SPL_BUILD)) { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 755 | plat = dev_get_plat(dev); |
Patrick Delaunay | 1dffeaf | 2020-07-31 16:31:51 +0200 | [diff] [blame] | 756 | |
| 757 | for (otp = 57; otp <= BSEC_OTP_MAX_VALUE; otp++) |
| 758 | if (!bsec_read_SR_lock(plat->base, otp)) |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 759 | bsec_shadow_register(dev, plat->base, otp); |
Patrick Delaunay | 1dffeaf | 2020-07-31 16:31:51 +0200 | [diff] [blame] | 760 | } |
Patrick Delaunay | f95686b | 2019-02-27 17:01:28 +0100 | [diff] [blame] | 761 | |
| 762 | return 0; |
| 763 | } |
Patrick Delaunay | f95686b | 2019-02-27 17:01:28 +0100 | [diff] [blame] | 764 | |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 765 | static const struct udevice_id stm32mp_bsec_ids[] = { |
Patrick Delaunay | 986ec7b | 2023-01-06 13:20:16 +0100 | [diff] [blame] | 766 | { .compatible = "st,stm32mp13-bsec" }, |
Patrick Delaunay | bdd7136 | 2019-02-27 17:01:27 +0100 | [diff] [blame] | 767 | { .compatible = "st,stm32mp15-bsec" }, |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 768 | {} |
| 769 | }; |
| 770 | |
| 771 | U_BOOT_DRIVER(stm32mp_bsec) = { |
| 772 | .name = "stm32mp_bsec", |
| 773 | .id = UCLASS_MISC, |
| 774 | .of_match = stm32mp_bsec_ids, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 775 | .of_to_plat = stm32mp_bsec_of_to_plat, |
Patrick Delaunay | 5fa74b8 | 2023-01-06 13:20:14 +0100 | [diff] [blame] | 776 | .plat_auto = sizeof(struct stm32mp_bsec_plat), |
Patrick Delaunay | d538829 | 2023-01-06 13:20:15 +0100 | [diff] [blame] | 777 | .priv_auto = sizeof(struct stm32mp_bsec_priv), |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 778 | .ops = &stm32mp_bsec_ops, |
Patrick Delaunay | f95686b | 2019-02-27 17:01:28 +0100 | [diff] [blame] | 779 | .probe = stm32mp_bsec_probe, |
Patrick Delaunay | 14d6a24 | 2018-05-17 15:24:05 +0200 | [diff] [blame] | 780 | }; |
Patrick Delaunay | 6332c04 | 2020-06-16 18:27:44 +0200 | [diff] [blame] | 781 | |
| 782 | bool bsec_dbgswenable(void) |
| 783 | { |
| 784 | struct udevice *dev; |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 785 | struct stm32mp_bsec_plat *plat; |
Patrick Delaunay | 6332c04 | 2020-06-16 18:27:44 +0200 | [diff] [blame] | 786 | int ret; |
| 787 | |
| 788 | ret = uclass_get_device_by_driver(UCLASS_MISC, |
Simon Glass | 65130cd | 2020-12-28 20:34:56 -0700 | [diff] [blame] | 789 | DM_DRIVER_GET(stm32mp_bsec), &dev); |
Patrick Delaunay | 6332c04 | 2020-06-16 18:27:44 +0200 | [diff] [blame] | 790 | if (ret || !dev) { |
Patrick Delaunay | fbefc10 | 2020-11-06 19:01:31 +0100 | [diff] [blame] | 791 | log_debug("bsec driver not available\n"); |
Patrick Delaunay | 6332c04 | 2020-06-16 18:27:44 +0200 | [diff] [blame] | 792 | return false; |
| 793 | } |
| 794 | |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 795 | plat = dev_get_plat(dev); |
Patrick Delaunay | 6332c04 | 2020-06-16 18:27:44 +0200 | [diff] [blame] | 796 | if (readl(plat->base + BSEC_DENABLE_OFF) & BSEC_DENABLE_DBGSWENABLE) |
| 797 | return true; |
| 798 | |
| 799 | return false; |
| 800 | } |
Patrick Delaunay | 9fa24a5 | 2022-05-20 18:24:41 +0200 | [diff] [blame] | 801 | |
| 802 | u32 get_otp(int index, int shift, int mask) |
| 803 | { |
| 804 | int ret; |
| 805 | struct udevice *dev; |
| 806 | u32 otp = 0; |
| 807 | |
| 808 | ret = uclass_get_device_by_driver(UCLASS_MISC, |
| 809 | DM_DRIVER_GET(stm32mp_bsec), |
| 810 | &dev); |
| 811 | |
| 812 | if (!ret) |
| 813 | ret = misc_read(dev, STM32_BSEC_SHADOW(index), |
| 814 | &otp, sizeof(otp)); |
| 815 | |
| 816 | return (otp >> shift) & mask; |
| 817 | } |