blob: 70f327289a8d49af612a938982c39dee818e30e9 [file] [log] [blame]
Lionel Debieveb1e0b112019-08-26 15:14:51 +02001/*
Nicolas Toromanoff68039f22020-12-22 13:54:51 +01002 * Copyright (c) 2019-2022, 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>
Yann Gautier33667d22021-08-30 15:06:54 +020013#include <drivers/clk.h>
Lionel Debieveb1e0b112019-08-26 15:14:51 +020014#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>
Nicolas Toromanoff68039f22020-12-22 13:54:51 +010019#include <libfdt.h>
Lionel Debieveb1e0b112019-08-26 15:14:51 +020020#include <plat/common/platform.h>
21
Nicolas Toromanoff68039f22020-12-22 13:54:51 +010022#include <platform_def.h>
23
24#if STM32_HASH_VER == 2
Lionel Debieveb1e0b112019-08-26 15:14:51 +020025#define DT_HASH_COMPAT "st,stm32f756-hash"
Nicolas Toromanoff68039f22020-12-22 13:54:51 +010026#endif
27#if STM32_HASH_VER == 4
28#define DT_HASH_COMPAT "st,stm32mp13-hash"
29#endif
Lionel Debieveb1e0b112019-08-26 15:14:51 +020030
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)
Nicolas Toromanoff68039f22020-12-22 13:54:51 +010040#if STM32_HASH_VER == 2
Lionel Debieveb1e0b112019-08-26 15:14:51 +020041#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))
Nicolas Toromanoff68039f22020-12-22 13:54:51 +010045#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
Lionel Debieveb1e0b112019-08-26 15:14:51 +020056
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
Nicolas Toromanoff68039f22020-12-22 13:54:51 +010069#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
Lionel Debieveb1e0b112019-08-26 15:14:51 +020073
Etienne Carriere45c70e62019-12-08 08:14:40 +010074#define RESET_TIMEOUT_US_1MS 1000U
Lionel Debieveb1e0b112019-08-26 15:14:51 +020075#define HASH_TIMEOUT_US 10000U
76
77enum 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
84struct stm32_hash_instance {
85 uintptr_t base;
86 unsigned int clock;
87 size_t digest_size;
88};
89
90struct stm32_hash_remain {
91 uint32_t buffer;
92 size_t length;
93};
94
95/* Expect a single HASH peripheral */
96static struct stm32_hash_instance stm32_hash;
97static struct stm32_hash_remain stm32_remain;
98
99static uintptr_t hash_base(void)
100{
101 return stm32_hash.base;
102}
103
104static 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
118static 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
132static 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
146static 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) {
Nicolas Toromanoff68039f22020-12-22 13:54:51 +0100153#if STM32_HASH_VER == 2
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200154 case HASH_MD5SUM:
155 reg |= HASH_CR_ALGO_MD5;
156 stm32_hash.digest_size = MD5_DIGEST_SIZE;
157 break;
Nicolas Toromanoff68039f22020-12-22 13:54:51 +0100158#endif
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200159 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;
Nicolas Toromanoff68039f22020-12-22 13:54:51 +0100167#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
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200177 /* 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
188static 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
215int 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
Yann Gautier33667d22021-08-30 15:06:54 +0200224 clk_enable(stm32_hash.clock);
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200225
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
265exit:
Yann Gautier33667d22021-08-30 15:06:54 +0200266 clk_disable(stm32_hash.clock);
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200267
268 return ret;
269}
270
271int stm32_hash_final(uint8_t *digest)
272{
273 int ret;
274
Yann Gautier33667d22021-08-30 15:06:54 +0200275 clk_enable(stm32_hash.clock);
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200276
277 if (stm32_remain.length != 0U) {
278 ret = hash_write_data(stm32_remain.buffer);
279 if (ret != 0) {
Yann Gautier33667d22021-08-30 15:06:54 +0200280 clk_disable(stm32_hash.clock);
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200281 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));
Lionel Debieve662c1f52020-01-31 16:17:37 +0100287 } else {
288 mmio_clrbits_32(hash_base() + HASH_STR, HASH_STR_NBLW_MASK);
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200289 }
290
291 mmio_setbits_32(hash_base() + HASH_STR, HASH_STR_DCAL);
292
293 ret = hash_get_digest(digest);
294
Yann Gautier33667d22021-08-30 15:06:54 +0200295 clk_disable(stm32_hash.clock);
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200296
297 return ret;
298}
299
300int 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
313void stm32_hash_init(enum stm32_hash_algo_mode mode)
314{
Yann Gautier33667d22021-08-30 15:06:54 +0200315 clk_enable(stm32_hash.clock);
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200316
317 hash_hw_init(mode);
318
Yann Gautier33667d22021-08-30 15:06:54 +0200319 clk_disable(stm32_hash.clock);
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200320
321 zeromem(&stm32_remain, sizeof(stm32_remain));
322}
323
324int 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
Etienne Carriere3d0d0a12019-12-02 10:13:12 +0100337 /* BL32 uses hash if it is assigned only to secure world */
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200338 if (hash_info.status == DT_SECURE) {
Etienne Carriere3d0d0a12019-12-02 10:13:12 +0100339 stm32mp_register_secure_periph_iomem(hash_info.base);
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200340 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
Yann Gautier33667d22021-08-30 15:06:54 +0200356 clk_enable(stm32_hash.clock);
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200357
358 if (hash_info.reset >= 0) {
Etienne Carriere45c70e62019-12-08 08:14:40 +0100359 uint32_t id = (uint32_t)hash_info.reset;
360
361 if (stm32mp_reset_assert(id, RESET_TIMEOUT_US_1MS) != 0) {
362 panic();
363 }
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200364 udelay(20);
Etienne Carriere45c70e62019-12-08 08:14:40 +0100365 if (stm32mp_reset_deassert(id, RESET_TIMEOUT_US_1MS) != 0) {
366 panic();
367 }
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200368 }
369
Yann Gautier33667d22021-08-30 15:06:54 +0200370 clk_disable(stm32_hash.clock);
Lionel Debieveb1e0b112019-08-26 15:14:51 +0200371
372 return 0;
373}