Skip to content

Commit d92d75e

Browse files
committed
M467: Improve Crypto H/W wait helper routine
Add crypto_xxx_wait2 helper routine to replace crypto_xxx_wait for Crypto H/W control
1 parent 3fe95b2 commit d92d75e

File tree

2 files changed

+51
-8
lines changed

2 files changed

+51
-8
lines changed

targets/TARGET_NUVOTON/TARGET_M460/crypto/crypto-misc.cpp

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <limits.h>
2525
#include "nu_modutil.h"
2626
#include "nu_bitutil.h"
27+
#include "nu_timer.h"
2728
#include "crypto-misc.h"
2829
#include "platform/SingletonPtr.h"
2930
#include "platform/PlatformMutex.h"
@@ -72,7 +73,7 @@ static volatile uint16_t crypto_ecc_done;
7273
static volatile uint16_t crypto_rsa_done;
7374

7475
static void crypto_submodule_prestart(volatile uint16_t *submodule_done);
75-
static bool crypto_submodule_wait(volatile uint16_t *submodule_done);
76+
static bool crypto_submodule_wait(volatile uint16_t *submodule_done, int32_t timeout_us);
7677

7778
/* As crypto init counter changes from 0 to 1:
7879
*
@@ -206,7 +207,12 @@ void crypto_prng_prestart(void)
206207

207208
bool crypto_prng_wait(void)
208209
{
209-
return crypto_submodule_wait(&crypto_prng_done);
210+
return crypto_prng_wait2(-1);
211+
}
212+
213+
bool crypto_prng_wait2(int32_t timeout_us)
214+
{
215+
return crypto_submodule_wait(&crypto_prng_done, timeout_us);
210216
}
211217

212218
void crypto_aes_prestart(void)
@@ -216,7 +222,12 @@ void crypto_aes_prestart(void)
216222

217223
bool crypto_aes_wait(void)
218224
{
219-
return crypto_submodule_wait(&crypto_aes_done);
225+
return crypto_aes_wait2(-1);
226+
}
227+
228+
bool crypto_aes_wait2(int32_t timeout_us)
229+
{
230+
return crypto_submodule_wait(&crypto_aes_done, timeout_us);
220231
}
221232

222233
void crypto_sha_prestart(void)
@@ -226,7 +237,12 @@ void crypto_sha_prestart(void)
226237

227238
bool crypto_sha_wait(void)
228239
{
229-
return crypto_submodule_wait(&crypto_sha_done);
240+
return crypto_sha_wait2(-1);
241+
}
242+
243+
bool crypto_sha_wait2(int32_t timeout_us)
244+
{
245+
return crypto_submodule_wait(&crypto_sha_done, timeout_us);
230246
}
231247

232248
void crypto_ecc_prestart(void)
@@ -236,7 +252,12 @@ void crypto_ecc_prestart(void)
236252

237253
bool crypto_ecc_wait(void)
238254
{
239-
return crypto_submodule_wait(&crypto_ecc_done);
255+
return crypto_ecc_wait2(-1);
256+
}
257+
258+
bool crypto_ecc_wait2(int32_t timeout_us)
259+
{
260+
return crypto_submodule_wait(&crypto_ecc_done, timeout_us);
240261
}
241262

242263
void crypto_rsa_prestart(void)
@@ -246,7 +267,12 @@ void crypto_rsa_prestart(void)
246267

247268
bool crypto_rsa_wait(void)
248269
{
249-
return crypto_submodule_wait(&crypto_rsa_done);
270+
return crypto_rsa_wait2(-1);
271+
}
272+
273+
bool crypto_rsa_wait2(int32_t timeout_us)
274+
{
275+
return crypto_submodule_wait(&crypto_rsa_done, timeout_us);
250276
}
251277

252278
bool crypto_dma_buff_compat(const void *buff, size_t buff_size, size_t size_aligned_to)
@@ -304,9 +330,21 @@ static void crypto_submodule_prestart(volatile uint16_t *submodule_done)
304330
__DSB();
305331
}
306332

307-
static bool crypto_submodule_wait(volatile uint16_t *submodule_done)
333+
static bool crypto_submodule_wait(volatile uint16_t *submodule_done, int32_t timeout_us)
308334
{
309-
while (! *submodule_done);
335+
/* Translate indefinite to extremely large */
336+
if (timeout_us < 0) {
337+
timeout_us = 0x7FFFFFFF;
338+
}
339+
340+
struct nu_countdown_ctx_s ctx;
341+
nu_countdown_init(&ctx, timeout_us);
342+
while (! *submodule_done) {
343+
if (nu_countdown_expired(&ctx)) {
344+
break;
345+
}
346+
}
347+
nu_countdown_free(&ctx);
310348

311349
/* Ensure while loop above and subsequent code are not reordered */
312350
__DSB();

targets/TARGET_NUVOTON/TARGET_M460/crypto/crypto-misc.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,19 @@ void crypto_rsa_release(void);
7171
*/
7272
void crypto_prng_prestart(void);
7373
bool crypto_prng_wait(void);
74+
bool crypto_prng_wait2(int32_t timeout_us);
7475
void crypto_aes_prestart(void);
7576
bool crypto_aes_wait(void);
77+
bool crypto_aes_wait2(int32_t timeout_us);
7678
void crypto_sha_prestart(void);
7779
bool crypto_sha_wait(void);
80+
bool crypto_sha_wait2(int32_t timeout_us);
7881
void crypto_ecc_prestart(void);
7982
bool crypto_ecc_wait(void);
83+
bool crypto_ecc_wait2(int32_t timeout_us);
8084
void crypto_rsa_prestart(void);
8185
bool crypto_rsa_wait(void);
86+
bool crypto_rsa_wait2(int32_t timeout_us);
8287

8388

8489
/* Check if buffer can be used for crypto DMA. It has the following requirements:

0 commit comments

Comments
 (0)