1 /* 2 * Copyright (c) 2019-2022, STMicroelectronics - All Rights Reserved 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #include <assert.h> 8 #include <errno.h> 9 #include <stdint.h> 10 11 #include <arch_helpers.h> 12 #include <common/debug.h> 13 #include <drivers/clk.h> 14 #include <drivers/delay_timer.h> 15 #include <drivers/st/stm32_hash.h> 16 #include <drivers/st/stm32mp_reset.h> 17 #include <lib/mmio.h> 18 #include <lib/utils.h> 19 #include <libfdt.h> 20 #include <plat/common/platform.h> 21 22 #include <platform_def.h> 23 24 #if STM32_HASH_VER == 2 25 #define DT_HASH_COMPAT "st,stm32f756-hash" 26 #endif 27 #if STM32_HASH_VER == 4 28 #define DT_HASH_COMPAT "st,stm32mp13-hash" 29 #endif 30 31 #define HASH_CR 0x00U 32 #define HASH_DIN 0x04U 33 #define HASH_STR 0x08U 34 #define HASH_SR 0x24U 35 #define HASH_HREG(x) (0x310U + ((x) * 0x04U)) 36 37 /* Control Register */ 38 #define HASH_CR_INIT BIT(2) 39 #define HASH_CR_DATATYPE_SHIFT U(4) 40 #if STM32_HASH_VER == 2 41 #define HASH_CR_ALGO_SHA1 0x0U 42 #define HASH_CR_ALGO_MD5 BIT(7) 43 #define HASH_CR_ALGO_SHA224 BIT(18) 44 #define HASH_CR_ALGO_SHA256 (BIT(18) | BIT(7)) 45 #endif 46 #if STM32_HASH_VER == 4 47 #define HASH_CR_ALGO_SHIFT U(17) 48 #define HASH_CR_ALGO_SHA1 (0x0U << HASH_CR_ALGO_SHIFT) 49 #define HASH_CR_ALGO_SHA224 (0x2U << HASH_CR_ALGO_SHIFT) 50 #define HASH_CR_ALGO_SHA256 (0x3U << HASH_CR_ALGO_SHIFT) 51 #define HASH_CR_ALGO_SHA384 (0xCU << HASH_CR_ALGO_SHIFT) 52 #define HASH_CR_ALGO_SHA512_224 (0xDU << HASH_CR_ALGO_SHIFT) 53 #define HASH_CR_ALGO_SHA512_256 (0xEU << HASH_CR_ALGO_SHIFT) 54 #define HASH_CR_ALGO_SHA512 (0xFU << HASH_CR_ALGO_SHIFT) 55 #endif 56 57 /* Status Flags */ 58 #define HASH_SR_DCIS BIT(1) 59 #define HASH_SR_BUSY BIT(3) 60 61 /* STR Register */ 62 #define HASH_STR_NBLW_MASK GENMASK(4, 0) 63 #define HASH_STR_DCAL BIT(8) 64 65 #define MD5_DIGEST_SIZE 16U 66 #define SHA1_DIGEST_SIZE 20U 67 #define SHA224_DIGEST_SIZE 28U 68 #define SHA256_DIGEST_SIZE 32U 69 #define SHA384_DIGEST_SIZE 48U 70 #define SHA512_224_DIGEST_SIZE 28U 71 #define SHA512_256_DIGEST_SIZE 32U 72 #define SHA512_DIGEST_SIZE 64U 73 74 #define RESET_TIMEOUT_US_1MS 1000U 75 #define HASH_TIMEOUT_US 10000U 76 77 enum stm32_hash_data_format { 78 HASH_DATA_32_BITS, 79 HASH_DATA_16_BITS, 80 HASH_DATA_8_BITS, 81 HASH_DATA_1_BIT 82 }; 83 84 struct stm32_hash_instance { 85 uintptr_t base; 86 unsigned int clock; 87 size_t digest_size; 88 }; 89 90 struct stm32_hash_remain { 91 uint32_t buffer; 92 size_t length; 93 }; 94 95 /* Expect a single HASH peripheral */ 96 static struct stm32_hash_instance stm32_hash; 97 static struct stm32_hash_remain stm32_remain; 98 99 static uintptr_t hash_base(void) 100 { 101 return stm32_hash.base; 102 } 103 104 static int hash_wait_busy(void) 105 { 106 uint64_t timeout = timeout_init_us(HASH_TIMEOUT_US); 107 108 while ((mmio_read_32(hash_base() + HASH_SR) & HASH_SR_BUSY) != 0U) { 109 if (timeout_elapsed(timeout)) { 110 ERROR("%s: busy timeout\n", __func__); 111 return -ETIMEDOUT; 112 } 113 } 114 115 return 0; 116 } 117 118 static int hash_wait_computation(void) 119 { 120 uint64_t timeout = timeout_init_us(HASH_TIMEOUT_US); 121 122 while ((mmio_read_32(hash_base() + HASH_SR) & HASH_SR_DCIS) == 0U) { 123 if (timeout_elapsed(timeout)) { 124 ERROR("%s: busy timeout\n", __func__); 125 return -ETIMEDOUT; 126 } 127 } 128 129 return 0; 130 } 131 132 static int hash_write_data(uint32_t data) 133 { 134 int ret; 135 136 ret = hash_wait_busy(); 137 if (ret != 0) { 138 return ret; 139 } 140 141 mmio_write_32(hash_base() + HASH_DIN, data); 142 143 return 0; 144 } 145 146 static void hash_hw_init(enum stm32_hash_algo_mode mode) 147 { 148 uint32_t reg; 149 150 reg = HASH_CR_INIT | (HASH_DATA_8_BITS << HASH_CR_DATATYPE_SHIFT); 151 152 switch (mode) { 153 #if STM32_HASH_VER == 2 154 case HASH_MD5SUM: 155 reg |= HASH_CR_ALGO_MD5; 156 stm32_hash.digest_size = MD5_DIGEST_SIZE; 157 break; 158 #endif 159 case HASH_SHA1: 160 reg |= HASH_CR_ALGO_SHA1; 161 stm32_hash.digest_size = SHA1_DIGEST_SIZE; 162 break; 163 case HASH_SHA224: 164 reg |= HASH_CR_ALGO_SHA224; 165 stm32_hash.digest_size = SHA224_DIGEST_SIZE; 166 break; 167 #if STM32_HASH_VER == 4 168 case HASH_SHA384: 169 reg |= HASH_CR_ALGO_SHA384; 170 stm32_hash.digest_size = SHA384_DIGEST_SIZE; 171 break; 172 case HASH_SHA512: 173 reg |= HASH_CR_ALGO_SHA512; 174 stm32_hash.digest_size = SHA512_DIGEST_SIZE; 175 break; 176 #endif 177 /* Default selected algo is SHA256 */ 178 case HASH_SHA256: 179 default: 180 reg |= HASH_CR_ALGO_SHA256; 181 stm32_hash.digest_size = SHA256_DIGEST_SIZE; 182 break; 183 } 184 185 mmio_write_32(hash_base() + HASH_CR, reg); 186 } 187 188 static int hash_get_digest(uint8_t *digest) 189 { 190 int ret; 191 uint32_t i; 192 uint32_t dsg; 193 194 ret = hash_wait_computation(); 195 if (ret != 0) { 196 return ret; 197 } 198 199 for (i = 0U; i < (stm32_hash.digest_size / sizeof(uint32_t)); i++) { 200 dsg = __builtin_bswap32(mmio_read_32(hash_base() + 201 HASH_HREG(i))); 202 memcpy(digest + (i * sizeof(uint32_t)), &dsg, sizeof(uint32_t)); 203 } 204 205 #if defined(IMAGE_BL2) 206 /* 207 * Clean hardware context as HASH could be used later 208 * by non-secure software 209 */ 210 hash_hw_init(HASH_SHA256); 211 #endif 212 return 0; 213 } 214 215 int stm32_hash_update(const uint8_t *buffer, size_t length) 216 { 217 size_t remain_length = length; 218 int ret = 0; 219 220 if ((length == 0U) || (buffer == NULL)) { 221 return 0; 222 } 223 224 clk_enable(stm32_hash.clock); 225 226 if (stm32_remain.length != 0U) { 227 uint32_t copysize; 228 229 copysize = MIN((sizeof(uint32_t) - stm32_remain.length), 230 length); 231 memcpy(((uint8_t *)&stm32_remain.buffer) + stm32_remain.length, 232 buffer, copysize); 233 remain_length -= copysize; 234 buffer += copysize; 235 if (stm32_remain.length == sizeof(uint32_t)) { 236 ret = hash_write_data(stm32_remain.buffer); 237 if (ret != 0) { 238 goto exit; 239 } 240 241 zeromem(&stm32_remain, sizeof(stm32_remain)); 242 } 243 } 244 245 while (remain_length / sizeof(uint32_t) != 0U) { 246 uint32_t tmp_buf; 247 248 memcpy(&tmp_buf, buffer, sizeof(uint32_t)); 249 ret = hash_write_data(tmp_buf); 250 if (ret != 0) { 251 goto exit; 252 } 253 254 buffer += sizeof(uint32_t); 255 remain_length -= sizeof(uint32_t); 256 } 257 258 if (remain_length != 0U) { 259 assert(stm32_remain.length == 0U); 260 261 memcpy((uint8_t *)&stm32_remain.buffer, buffer, remain_length); 262 stm32_remain.length = remain_length; 263 } 264 265 exit: 266 clk_disable(stm32_hash.clock); 267 268 return ret; 269 } 270 271 int stm32_hash_final(uint8_t *digest) 272 { 273 int ret; 274 275 clk_enable(stm32_hash.clock); 276 277 if (stm32_remain.length != 0U) { 278 ret = hash_write_data(stm32_remain.buffer); 279 if (ret != 0) { 280 clk_disable(stm32_hash.clock); 281 return ret; 282 } 283 284 mmio_clrsetbits_32(hash_base() + HASH_STR, HASH_STR_NBLW_MASK, 285 8U * stm32_remain.length); 286 zeromem(&stm32_remain, sizeof(stm32_remain)); 287 } else { 288 mmio_clrbits_32(hash_base() + HASH_STR, HASH_STR_NBLW_MASK); 289 } 290 291 mmio_setbits_32(hash_base() + HASH_STR, HASH_STR_DCAL); 292 293 ret = hash_get_digest(digest); 294 295 clk_disable(stm32_hash.clock); 296 297 return ret; 298 } 299 300 int stm32_hash_final_update(const uint8_t *buffer, uint32_t length, 301 uint8_t *digest) 302 { 303 int ret; 304 305 ret = stm32_hash_update(buffer, length); 306 if (ret != 0) { 307 return ret; 308 } 309 310 return stm32_hash_final(digest); 311 } 312 313 void stm32_hash_init(enum stm32_hash_algo_mode mode) 314 { 315 clk_enable(stm32_hash.clock); 316 317 hash_hw_init(mode); 318 319 clk_disable(stm32_hash.clock); 320 321 zeromem(&stm32_remain, sizeof(stm32_remain)); 322 } 323 324 int stm32_hash_register(void) 325 { 326 struct dt_node_info hash_info; 327 int node; 328 329 for (node = dt_get_node(&hash_info, -1, DT_HASH_COMPAT); 330 node != -FDT_ERR_NOTFOUND; 331 node = dt_get_node(&hash_info, node, DT_HASH_COMPAT)) { 332 #if defined(IMAGE_BL2) 333 if (hash_info.status != DT_DISABLED) { 334 break; 335 } 336 #else 337 /* BL32 uses hash if it is assigned only to secure world */ 338 if (hash_info.status == DT_SECURE) { 339 stm32mp_register_secure_periph_iomem(hash_info.base); 340 break; 341 } 342 #endif 343 } 344 345 if (node == -FDT_ERR_NOTFOUND) { 346 return -ENODEV; 347 } 348 349 if (hash_info.clock < 0) { 350 return -EINVAL; 351 } 352 353 stm32_hash.base = hash_info.base; 354 stm32_hash.clock = hash_info.clock; 355 356 clk_enable(stm32_hash.clock); 357 358 if (hash_info.reset >= 0) { 359 uint32_t id = (uint32_t)hash_info.reset; 360 361 if (stm32mp_reset_assert(id, RESET_TIMEOUT_US_1MS) != 0) { 362 panic(); 363 } 364 udelay(20); 365 if (stm32mp_reset_deassert(id, RESET_TIMEOUT_US_1MS) != 0) { 366 panic(); 367 } 368 } 369 370 clk_disable(stm32_hash.clock); 371 372 return 0; 373 } 374