blob: e1a519e796d7a25af04cbf88d493665fe537c837 [file] [log] [blame]
Lionel Debieveb1e0b112019-08-26 15:14:51 +02001/*
Thomas BOURGOIN84ebe2a2023-03-27 16:42:23 +02002 * Copyright (c) 2019-2025, STMicroelectronics - All Rights Reserved
Lionel Debieveb1e0b112019-08-26 15:14:51 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <assert.h>
8#include <errno.h>
9#include <stdint.h>
10
Lionel Debieveb1e0b112019-08-26 15:14:51 +020011#include <arch_helpers.h>
12#include <common/debug.h>
Raghu Krishnamurthye9529e42024-09-24 07:11:29 -070013#include <common/sha_common_macros.h>
Yann Gautier33667d22021-08-30 15:06:54 +020014#include <drivers/clk.h>
Lionel Debieveb1e0b112019-08-26 15:14:51 +020015#include <drivers/delay_timer.h>
16#include <drivers/st/stm32_hash.h>
17#include <drivers/st/stm32mp_reset.h>
18#include <lib/mmio.h>
19#include <lib/utils.h>
Nicolas Toromanoff68039f22020-12-22 13:54:51 +010020#include <libfdt.h>
Lionel Debieveb1e0b112019-08-26 15:14:51 +020021#include <plat/common/platform.h>
22
Nicolas Toromanoff68039f22020-12-22 13:54:51 +010023#include <platform_def.h>
24
25#if STM32_HASH_VER == 2
Lionel Debieveb1e0b112019-08-26 15:14:51 +020026#define DT_HASH_COMPAT "st,stm32f756-hash"
Nicolas Toromanoff68039f22020-12-22 13:54:51 +010027#endif
28#if STM32_HASH_VER == 4
29#define DT_HASH_COMPAT "st,stm32mp13-hash"
30#endif
Lionel Debieveb1e0b112019-08-26 15:14:51 +020031
32#define HASH_CR 0x00U
33#define HASH_DIN 0x04U
34#define HASH_STR 0x08U
35#define HASH_SR 0x24U
36#define HASH_HREG(x) (0x310U + ((x) * 0x04U))
37
38/* Control Register */
39#define HASH_CR_INIT BIT(2)
40#define HASH_CR_DATATYPE_SHIFT U(4)
Nicolas Toromanoff68039f22020-12-22 13:54:51 +010041#if STM32_HASH_VER == 2
Lionel Debieveb1e0b112019-08-26 15:14:51 +020042#define HASH_CR_ALGO_SHA1 0x0U
43#define HASH_CR_ALGO_MD5 BIT(7)
44#define HASH_CR_ALGO_SHA224 BIT(18)
45#define HASH_CR_ALGO_SHA256 (BIT(18) | BIT(7))
Nicolas Toromanoff68039f22020-12-22 13:54:51 +010046#endif
47#if STM32_HASH_VER == 4
48#define HASH_CR_ALGO_SHIFT U(17)
Thomas BOURGOIN84ebe2a2023-03-27 16:42:23 +020049#define HASH_CR_ALGO_SHA1 ((uint32_t)(0x0U) << HASH_CR_ALGO_SHIFT)
50#define HASH_CR_ALGO_SHA224 ((uint32_t)(0x2U) << HASH_CR_ALGO_SHIFT)
51#define HASH_CR_ALGO_SHA256 ((uint32_t)(0x3U) << HASH_CR_ALGO_SHIFT)
52#define HASH_CR_ALGO_SHA384 ((uint32_t)(0xCU) << HASH_CR_ALGO_SHIFT)
53#define HASH_CR_ALGO_SHA512_224 ((uint32_t)(0xDU) << HASH_CR_ALGO_SHIFT)
54#define HASH_CR_ALGO_SHA512_256 ((uint32_t)(0xEU) << HASH_CR_ALGO_SHIFT)
55#define HASH_CR_ALGO_SHA512 ((uint32_t)(0xFU) << HASH_CR_ALGO_SHIFT)
Nicolas Toromanoff68039f22020-12-22 13:54:51 +010056#endif
Lionel Debieveb1e0b112019-08-26 15:14:51 +020057
58/* Status Flags */
59#define HASH_SR_DCIS BIT(1)
60#define HASH_SR_BUSY BIT(3)
61
62/* STR Register */
63#define HASH_STR_NBLW_MASK GENMASK(4, 0)
64#define HASH_STR_DCAL BIT(8)
65
Etienne Carriere45c70e62019-12-08 08:14:40 +010066#define RESET_TIMEOUT_US_1MS 1000U
Lionel Debieveb1e0b112019-08-26 15:14:51 +020067#define HASH_TIMEOUT_US 10000U
68
69enum stm32_hash_data_format {
70 HASH_DATA_32_BITS,
71 HASH_DATA_16_BITS,
72 HASH_DATA_8_BITS,
73 HASH_DATA_1_BIT
74};
75
76struct stm32_hash_instance {
77 uintptr_t base;
78 unsigned int clock;
79 size_t digest_size;
80};
81
82struct stm32_hash_remain {
83 uint32_t buffer;
84 size_t length;
85};
86
87/* Expect a single HASH peripheral */
88static struct stm32_hash_instance stm32_hash;
89static struct stm32_hash_remain stm32_remain;
90
91static uintptr_t hash_base(void)
92{
93 return stm32_hash.base;
94}
95
96static int hash_wait_busy(void)
97{
98 uint64_t timeout = timeout_init_us(HASH_TIMEOUT_US);
99
100 while ((mmio_read_32(hash_base() + HASH_SR) & HASH_SR_BUSY) != 0U) {
101 if (timeout_elapsed(timeout)) {
102 ERROR("%s: busy timeout\n", __func__);
103 return -ETIMEDOUT;
104 }
105 }
106
107 return 0;
108}
109
110static int hash_wait_computation(void)
111{
112 uint64_t timeout = timeout_init_us(HASH_TIMEOUT_US);
113
114 while ((mmio_read_32(hash_base() + HASH_SR) & HASH_SR_DCIS) == 0U) {
115 if (timeout_elapsed(timeout)) {
116 ERROR("%s: busy timeout\n", __func__);
117 return -ETIMEDOUT;
118 }
119 }
120
121 return 0;
122}
123
124static int hash_write_data(uint32_t data)
125{
126 int ret;
127
128 ret = hash_wait_busy();
129 if (ret != 0) {
130 return ret;
131 }
132
133 mmio_write_32(hash_base() + HASH_DIN, data);
134
135 return 0;
136}
137
138static void hash_hw_init(enum stm32_hash_algo_mode mode)
139{
140 uint32_t reg;
141
Thomas BOURGOIN84ebe2a2023-03-27 16:42:23 +0200142 reg = HASH_CR_INIT | ((uint32_t)HASH_DATA_8_BITS << HASH_CR_DATATYPE_SHIFT);
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200143
144 switch (mode) {
Nicolas Toromanoff68039f22020-12-22 13:54:51 +0100145#if STM32_HASH_VER == 2
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200146 case HASH_MD5SUM:
147 reg |= HASH_CR_ALGO_MD5;
148 stm32_hash.digest_size = MD5_DIGEST_SIZE;
149 break;
Nicolas Toromanoff68039f22020-12-22 13:54:51 +0100150#endif
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200151 case HASH_SHA1:
152 reg |= HASH_CR_ALGO_SHA1;
153 stm32_hash.digest_size = SHA1_DIGEST_SIZE;
154 break;
155 case HASH_SHA224:
156 reg |= HASH_CR_ALGO_SHA224;
157 stm32_hash.digest_size = SHA224_DIGEST_SIZE;
158 break;
Nicolas Toromanoff68039f22020-12-22 13:54:51 +0100159#if STM32_HASH_VER == 4
160 case HASH_SHA384:
161 reg |= HASH_CR_ALGO_SHA384;
162 stm32_hash.digest_size = SHA384_DIGEST_SIZE;
163 break;
164 case HASH_SHA512:
165 reg |= HASH_CR_ALGO_SHA512;
166 stm32_hash.digest_size = SHA512_DIGEST_SIZE;
167 break;
168#endif
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200169 /* Default selected algo is SHA256 */
170 case HASH_SHA256:
171 default:
172 reg |= HASH_CR_ALGO_SHA256;
173 stm32_hash.digest_size = SHA256_DIGEST_SIZE;
174 break;
175 }
176
177 mmio_write_32(hash_base() + HASH_CR, reg);
178}
179
180static int hash_get_digest(uint8_t *digest)
181{
182 int ret;
183 uint32_t i;
184 uint32_t dsg;
185
186 ret = hash_wait_computation();
187 if (ret != 0) {
188 return ret;
189 }
190
191 for (i = 0U; i < (stm32_hash.digest_size / sizeof(uint32_t)); i++) {
192 dsg = __builtin_bswap32(mmio_read_32(hash_base() +
193 HASH_HREG(i)));
Thomas BOURGOIN84ebe2a2023-03-27 16:42:23 +0200194 (void)(memcpy(&digest[i * sizeof(uint32_t)], (uint8_t *)&dsg, sizeof(uint32_t)));
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200195 }
196
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200197 /*
198 * Clean hardware context as HASH could be used later
199 * by non-secure software
200 */
201 hash_hw_init(HASH_SHA256);
Lionel Debieve6b5fc192022-10-04 14:28:57 +0200202
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200203 return 0;
204}
205
206int stm32_hash_update(const uint8_t *buffer, size_t length)
207{
208 size_t remain_length = length;
Thomas BOURGOIN84ebe2a2023-03-27 16:42:23 +0200209 uint8_t *remain_buf = (uint8_t *)&stm32_remain.buffer;
210 const uint8_t *buf = buffer;
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200211 int ret = 0;
212
213 if ((length == 0U) || (buffer == NULL)) {
214 return 0;
215 }
216
Thomas BOURGOIN84ebe2a2023-03-27 16:42:23 +0200217 ret = clk_enable(stm32_hash.clock);
218 if (ret != 0) {
219 return ret;
220 }
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200221
222 if (stm32_remain.length != 0U) {
223 uint32_t copysize;
224
225 copysize = MIN((sizeof(uint32_t) - stm32_remain.length),
226 length);
Thomas BOURGOIN84ebe2a2023-03-27 16:42:23 +0200227 (void)(memcpy(&remain_buf[stm32_remain.length], buf, copysize));
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200228 remain_length -= copysize;
Thomas BOURGOIN84ebe2a2023-03-27 16:42:23 +0200229 buf = &buf[copysize];
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200230 if (stm32_remain.length == sizeof(uint32_t)) {
231 ret = hash_write_data(stm32_remain.buffer);
232 if (ret != 0) {
233 goto exit;
234 }
235
236 zeromem(&stm32_remain, sizeof(stm32_remain));
237 }
238 }
239
240 while (remain_length / sizeof(uint32_t) != 0U) {
241 uint32_t tmp_buf;
242
Thomas BOURGOIN84ebe2a2023-03-27 16:42:23 +0200243 (void)(memcpy((void *)&tmp_buf, buf, sizeof(uint32_t)));
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200244 ret = hash_write_data(tmp_buf);
245 if (ret != 0) {
246 goto exit;
247 }
248
Thomas BOURGOIN84ebe2a2023-03-27 16:42:23 +0200249 buf = &buf[sizeof(uint32_t)];
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200250 remain_length -= sizeof(uint32_t);
251 }
252
253 if (remain_length != 0U) {
254 assert(stm32_remain.length == 0U);
255
Thomas BOURGOIN84ebe2a2023-03-27 16:42:23 +0200256 (void)(memcpy((uint8_t *)&stm32_remain.buffer, buf, remain_length));
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200257 stm32_remain.length = remain_length;
258 }
259
260exit:
Yann Gautier33667d22021-08-30 15:06:54 +0200261 clk_disable(stm32_hash.clock);
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200262
263 return ret;
264}
265
266int stm32_hash_final(uint8_t *digest)
267{
268 int ret;
269
Thomas BOURGOIN84ebe2a2023-03-27 16:42:23 +0200270 ret = clk_enable(stm32_hash.clock);
271 if (ret != 0) {
272 return ret;
273 }
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200274
275 if (stm32_remain.length != 0U) {
276 ret = hash_write_data(stm32_remain.buffer);
277 if (ret != 0) {
Yann Gautier33667d22021-08-30 15:06:54 +0200278 clk_disable(stm32_hash.clock);
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200279 return ret;
280 }
281
282 mmio_clrsetbits_32(hash_base() + HASH_STR, HASH_STR_NBLW_MASK,
283 8U * stm32_remain.length);
284 zeromem(&stm32_remain, sizeof(stm32_remain));
Lionel Debieve662c1f52020-01-31 16:17:37 +0100285 } else {
286 mmio_clrbits_32(hash_base() + HASH_STR, HASH_STR_NBLW_MASK);
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200287 }
288
289 mmio_setbits_32(hash_base() + HASH_STR, HASH_STR_DCAL);
290
291 ret = hash_get_digest(digest);
292
Yann Gautier33667d22021-08-30 15:06:54 +0200293 clk_disable(stm32_hash.clock);
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200294
295 return ret;
296}
297
298int stm32_hash_final_update(const uint8_t *buffer, uint32_t length,
299 uint8_t *digest)
300{
301 int ret;
302
303 ret = stm32_hash_update(buffer, length);
304 if (ret != 0) {
305 return ret;
306 }
307
308 return stm32_hash_final(digest);
309}
310
311void stm32_hash_init(enum stm32_hash_algo_mode mode)
312{
Thomas BOURGOIN84ebe2a2023-03-27 16:42:23 +0200313 if (clk_enable(stm32_hash.clock) != 0) {
314 ERROR("%s: fail to enable clock\n", __func__);
315 panic();
316 }
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200317
318 hash_hw_init(mode);
319
Yann Gautier33667d22021-08-30 15:06:54 +0200320 clk_disable(stm32_hash.clock);
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200321
322 zeromem(&stm32_remain, sizeof(stm32_remain));
323}
324
325int stm32_hash_register(void)
326{
327 struct dt_node_info hash_info;
328 int node;
Thomas BOURGOIN84ebe2a2023-03-27 16:42:23 +0200329 int ret;
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200330
331 for (node = dt_get_node(&hash_info, -1, DT_HASH_COMPAT);
332 node != -FDT_ERR_NOTFOUND;
333 node = dt_get_node(&hash_info, node, DT_HASH_COMPAT)) {
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200334 if (hash_info.status != DT_DISABLED) {
335 break;
336 }
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200337 }
338
339 if (node == -FDT_ERR_NOTFOUND) {
340 return -ENODEV;
341 }
342
343 if (hash_info.clock < 0) {
344 return -EINVAL;
345 }
346
347 stm32_hash.base = hash_info.base;
348 stm32_hash.clock = hash_info.clock;
349
Thomas BOURGOIN84ebe2a2023-03-27 16:42:23 +0200350 ret = clk_enable(stm32_hash.clock);
351 if (ret != 0) {
352 return ret;
353 }
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200354
355 if (hash_info.reset >= 0) {
Etienne Carriere45c70e62019-12-08 08:14:40 +0100356 uint32_t id = (uint32_t)hash_info.reset;
357
358 if (stm32mp_reset_assert(id, RESET_TIMEOUT_US_1MS) != 0) {
359 panic();
360 }
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200361 udelay(20);
Etienne Carriere45c70e62019-12-08 08:14:40 +0100362 if (stm32mp_reset_deassert(id, RESET_TIMEOUT_US_1MS) != 0) {
363 panic();
364 }
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200365 }
366
Yann Gautier33667d22021-08-30 15:06:54 +0200367 clk_disable(stm32_hash.clock);
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200368
369 return 0;
370}