3636#include <nimble/nimble_npl_os.h>
3737#endif
3838#include "os/os_trace_api.h"
39+ #ifndef NRF54L_SERIES
3940#include <hal/nrf_rng.h>
41+ #endif
4042#include "hal/nrf_ecb.h"
4143
44+ #ifdef NRF54L_SERIES
45+ #include <hal/nrf_cracen.h>
46+
47+ #define NRF_ECB NRF_ECB00
48+ #define NRF_AAR NRF_AAR00
49+
50+ /* ECB data structure */
51+ struct ecb_job_entry {
52+ uint8_t * ptr ;
53+ uint32_t attr_and_length ;
54+ };
55+
56+ static struct ecb_job_entry ecb_input_job_list [2 ];
57+ static struct ecb_job_entry ecb_output_job_list [2 ];
58+ #endif
59+
4260/* Total number of resolving list elements */
4361#define BLE_HW_RESOLV_LIST_SIZE (16)
4462
@@ -276,6 +294,43 @@ ble_hw_encrypt_block(struct ble_encryption_block *ecb)
276294 uint32_t end ;
277295 uint32_t err ;
278296
297+ #ifdef NRF54L_SERIES
298+ /* Stop ECB */
299+ nrf_ecb_task_trigger (NRF_ECB , NRF_ECB_TASK_STOP );
300+
301+ ecb_input_job_list [0 ].ptr = ecb -> plain_text ;
302+ ecb_input_job_list [0 ].attr_and_length = (11 << 24 ) | (16 & 0x00ffffff );
303+ ecb_output_job_list [0 ].ptr = ecb -> cipher_text ;
304+ ecb_output_job_list [0 ].attr_and_length = (11 << 24 ) | (16 & 0x00ffffff );
305+
306+ /* The end of a job list shall be 0 */
307+ ecb_input_job_list [1 ].ptr = 0 ;
308+ ecb_input_job_list [1 ].attr_and_length = 0 ;
309+ ecb_output_job_list [1 ].ptr = 0 ;
310+ ecb_output_job_list [1 ].attr_and_length = 0 ;
311+
312+ NRF_ECB -> EVENTS_END = 0 ;
313+ NRF_ECB -> EVENTS_ERROR = 0 ;
314+ NRF_ECB -> IN .PTR = (uint32_t )ecb_input_job_list ;
315+ NRF_ECB -> OUT .PTR = (uint32_t )ecb_output_job_list ;
316+ memcpy ((void * )NRF_ECB -> KEY .VALUE , ecb -> key , sizeof (uint32_t ) * 4 );
317+
318+ /* Start ECB */
319+ nrf_ecb_task_trigger (NRF_ECB , NRF_ECB_TASK_START );
320+
321+ /* Wait till error or done */
322+ rc = 0 ;
323+ while (1 ) {
324+ end = NRF_ECB -> EVENTS_END ;
325+ err = NRF_ECB -> EVENTS_ERROR ;
326+ if (end || err ) {
327+ if (err ) {
328+ rc = -1 ;
329+ }
330+ break ;
331+ }
332+ }
333+ #else
279334 /* Stop ECB */
280335 nrf_ecb_task_trigger (NRF_ECB , NRF_ECB_TASK_STOPECB );
281336 /* XXX: does task stop clear these counters? Anyway to do this quicker? */
@@ -301,7 +356,7 @@ ble_hw_encrypt_block(struct ble_encryption_block *ecb)
301356 tm_tick ();
302357#endif
303358 }
304-
359+ #endif
305360 return rc ;
306361}
307362
@@ -314,7 +369,25 @@ ble_rng_isr(void)
314369 uint8_t rnum ;
315370
316371 os_trace_isr_enter ();
372+ #ifdef NRF54L_SERIES
373+ /* No callback? Clear and disable interrupts */
374+ if (g_ble_rng_isr_cb == NULL ) {
375+ nrf_cracen_int_disable (NRF_CRACEN , NRF_CRACEN_INT_RNG_MASK );
376+ NRF_CRACENCORE -> RNGCONTROL .CONTROL &= ~CRACENCORE_RNGCONTROL_CONTROL_INTENFULL_Msk ;
377+ NRF_CRACEN -> EVENTS_RNG = 0 ;
378+ os_trace_isr_exit ();
379+ return ;
380+ }
317381
382+ if (g_ble_rng_isr_cb ) {
383+ /* If there is a value ready in the queue grab it */
384+ while ((NRF_CRACENCORE -> RNGCONTROL .CONTROL &
385+ CRACENCORE_RNGCONTROL_CONTROL_INTENFULL_Msk ) &&
386+ (NRF_CRACENCORE -> RNGCONTROL .FIFOLEVEL > 0 )) {
387+ g_ble_rng_isr_cb (ble_hw_rng_read ());
388+ }
389+ }
390+ #else
318391 /* No callback? Clear and disable interrupts */
319392 if (g_ble_rng_isr_cb == NULL ) {
320393 nrf_rng_int_disable (NRF_RNG , NRF_RNG_INT_VALRDY_MASK );
@@ -330,7 +403,7 @@ ble_rng_isr(void)
330403 rnum = (uint8_t )NRF_RNG -> VALUE ;
331404 (* g_ble_rng_isr_cb )(rnum );
332405 }
333-
406+ #endif
334407 os_trace_isr_exit ();
335408}
336409
@@ -345,6 +418,24 @@ ble_rng_isr(void)
345418int
346419ble_hw_rng_init (ble_rng_isr_cb_t cb , int bias )
347420{
421+ #ifdef NRF54L_SERIES
422+ NRF_CRACEN -> ENABLE = CRACEN_ENABLE_CRYPTOMASTER_Msk |
423+ CRACEN_ENABLE_RNG_Msk |
424+ CRACEN_ENABLE_PKEIKG_Msk ;
425+
426+ while (NRF_CRACENCORE -> PK .STATUS & CRACENCORE_PK_STATUS_PKBUSY_Msk );
427+ NRF_CRACENCORE -> PK .CONTROL &= ~CRACENCORE_IKG_PKECONTROL_CLEARIRQ_Msk ;
428+
429+ NRF_CRACENCORE -> RNGCONTROL .CONTROL = CRACENCORE_RNGCONTROL_CONTROL_ResetValue |
430+ CRACENCORE_RNGCONTROL_CONTROL_ENABLE_Msk ;
431+
432+ /* If we were passed a function pointer we need to enable the interrupt */
433+ if (cb != NULL ) {
434+ NVIC_SetVector (CRACEN_IRQn , (uint32_t )ble_rng_isr );
435+ NVIC_EnableIRQ (CRACEN_IRQn );
436+ g_ble_rng_isr_cb = cb ;
437+ }
438+ #else
348439 /* Set bias */
349440 if (bias ) {
350441 NRF_RNG -> CONFIG = 1 ;
@@ -365,6 +456,7 @@ ble_hw_rng_init(ble_rng_isr_cb_t cb, int bias)
365456 NVIC_EnableIRQ (RNG_IRQn );
366457 g_ble_rng_isr_cb = cb ;
367458 }
459+ #endif
368460
369461 return 0 ;
370462}
@@ -381,12 +473,23 @@ ble_hw_rng_start(void)
381473
382474 /* No need for interrupt if there is no callback */
383475 OS_ENTER_CRITICAL (sr );
476+ #ifdef NRF54L_SERIES
477+ NRF_CRACEN -> EVENTS_RNG = 0 ;
478+
479+ if (g_ble_rng_isr_cb ) {
480+ nrf_cracen_int_enable (NRF_CRACEN , NRF_CRACEN_INT_RNG_MASK );
481+ NRF_CRACENCORE -> RNGCONTROL .CONTROL |= CRACENCORE_RNGCONTROL_CONTROL_INTENFULL_Msk ;
482+ /* Force regeneration of the samples */
483+ NRF_CRACENCORE -> RNGCONTROL .FIFOLEVEL = 0 ;
484+ }
485+ #else
384486 NRF_RNG -> EVENTS_VALRDY = 0 ;
385487
386488 if (g_ble_rng_isr_cb ) {
387489 nrf_rng_int_enable (NRF_RNG , NRF_RNG_INT_VALRDY_MASK );
388490 }
389491 nrf_rng_task_trigger (NRF_RNG , NRF_RNG_TASK_START );
492+ #endif
390493 OS_EXIT_CRITICAL (sr );
391494
392495 return 0 ;
@@ -404,9 +507,15 @@ ble_hw_rng_stop(void)
404507
405508 /* No need for interrupt if there is no callback */
406509 OS_ENTER_CRITICAL (sr );
510+ #ifdef NRF54L_SERIES
511+ nrf_cracen_int_disable (NRF_CRACEN , NRF_CRACEN_INT_RNG_MASK );
512+ NRF_CRACENCORE -> RNGCONTROL .CONTROL &= ~CRACENCORE_RNGCONTROL_CONTROL_INTENFULL_Msk ;
513+ NRF_CRACEN -> EVENTS_RNG = 0 ;
514+ #else
407515 nrf_rng_int_disable (NRF_RNG , NRF_RNG_INT_VALRDY_MASK );
408516 nrf_rng_task_trigger (NRF_RNG , NRF_RNG_TASK_STOP );
409517 NRF_RNG -> EVENTS_VALRDY = 0 ;
518+ #endif
410519 OS_EXIT_CRITICAL (sr );
411520
412521 return 0 ;
@@ -421,14 +530,28 @@ uint8_t
421530ble_hw_rng_read (void )
422531{
423532 uint8_t rnum ;
533+ #ifdef NRF54L_SERIES
534+ uint8_t slot_id ;
424535
536+ /* Wait for a sample */
537+ while (NRF_CRACENCORE -> RNGCONTROL .FIFOLEVEL == 0 ) {
538+ assert ((NRF_CRACENCORE -> RNGCONTROL .STATUS &
539+ CRACENCORE_RNGCONTROL_STATUS_STATE_Msk ) !=
540+ (CRACENCORE_RNGCONTROL_STATUS_STATE_ERROR <<
541+ CRACENCORE_RNGCONTROL_STATUS_STATE_Pos ));
542+ }
543+
544+ NRF_CRACEN -> EVENTS_RNG = 0 ;
545+ slot_id = NRF_CRACENCORE -> RNGCONTROL .FIFODEPTH - NRF_CRACENCORE -> RNGCONTROL .FIFOLEVEL ;
546+ rnum = (uint8_t )NRF_CRACENCORE -> RNGCONTROL .FIFO [slot_id ];
547+ #else
425548 /* Wait for a sample */
426549 while (NRF_RNG -> EVENTS_VALRDY == 0 ) {
427550 }
428551
429552 NRF_RNG -> EVENTS_VALRDY = 0 ;
430553 rnum = (uint8_t )NRF_RNG -> VALUE ;
431-
554+ #endif
432555 return rnum ;
433556}
434557
511634ble_hw_resolv_list_match (void )
512635{
513636 if (NRF_AAR -> ENABLE && NRF_AAR -> EVENTS_END && NRF_AAR -> EVENTS_RESOLVED ) {
637+ #ifdef NRF54L_SERIES
638+ return (int )NRF_AAR -> ERRORSTATUS ;
639+ #else
514640 return (int )NRF_AAR -> STATUS ;
641+ #endif
515642 }
516643
517644 return -1 ;
0 commit comments