Skip to content

Commit 4e62ade

Browse files
committed
alif/se_services: Add SE services interface.
Includes services to get random numbers, reset SoC, get unique-id, dump SoC info, and CPU control services. Signed-off-by: Damien George <damien@micropython.org> Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
1 parent 53b7c14 commit 4e62ade

5 files changed

Lines changed: 309 additions & 0 deletions

File tree

ports/alif/Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ INC += -I$(BUILD)
4242
INC += -I$(BOARD_DIR)
4343
INC += -I$(CMSIS_DIR)
4444
INC += -I$(ALIF_DFP_REL_HERE)/drivers/include/
45+
INC += -I$(ALIF_DFP_REL_HERE)/se_services/include/
4546
INC += -I$(ALIF_DFP_REL_HERE)/ospi_xip/source/ospi
4647
INC += -I$(ALIF_DFP_REL_HERE)/Device/common/config/
4748
INC += -I$(ALIF_DFP_REL_HERE)/Device/common/include/
@@ -120,6 +121,7 @@ SRC_C = \
120121
ospi_flash.c \
121122
pendsv.c \
122123
system_tick.c \
124+
se_services.c \
123125
usbd.c \
124126
$(wildcard $(BOARD_DIR)/*.c)
125127

@@ -174,13 +176,23 @@ ALIF_SRC_C += $(addprefix $(ALIF_DFP_REL_TOP)/,\
174176
Device/common/source/system_M55.c \
175177
Device/common/source/system_utils.c \
176178
Device/core/$(MCU_CORE)/source/startup_$(MCU_CORE).c \
179+
drivers/source/mhu_driver.c \
180+
drivers/source/mhu_receiver.c \
181+
drivers/source/mhu_sender.c \
177182
drivers/source/pinconf.c \
178183
drivers/source/uart.c \
179184
drivers/source/utimer.c \
180185
ospi_xip/source/ospi/ospi_drv.c \
186+
se_services/source/services_host_application.c \
187+
se_services/source/services_host_boot.c \
188+
se_services/source/services_host_clocks.c \
189+
se_services/source/services_host_cryptocell.c \
190+
se_services/source/services_host_handler.c \
191+
se_services/source/services_host_system.c \
181192
)
182193

183194
$(BUILD)/tinyusb_port/tusb_alif_dcd.o: CFLAGS += -Wno-unused-variable -DTUSB_ALIF_NO_IRQ_CFG=1
195+
$(BUILD)/$(ALIF_DFP_REL_TOP)/se_services/source/services_host_boot.o: CFLAGS += -Wno-stringop-truncation
184196

185197
# List of sources for qstr extraction
186198
SRC_QSTR += $(SRC_C) $(SHARED_SRC_C) $(GEN_PINS_SRC)

ports/alif/irq.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
#define NVIC_PRIORITYGROUP_7 ((uint32_t)0x00000000U)
4242
#define IRQ_PRI_SYSTEM_TICK NVIC_EncodePriority(NVIC_PRIORITYGROUP_7, 0, 0)
43+
#define IRQ_PRI_MHU NVIC_EncodePriority(NVIC_PRIORITYGROUP_7, 0, 0)
4344
#define IRQ_PRI_QUIET_TIMING NVIC_EncodePriority(NVIC_PRIORITYGROUP_7, 1, 0)
4445
#define IRQ_PRI_UART_REPL NVIC_EncodePriority(NVIC_PRIORITYGROUP_7, 1, 0)
4546
#define IRQ_PRI_USB NVIC_EncodePriority(NVIC_PRIORITYGROUP_7, 5, 0)

ports/alif/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "mpuart.h"
4040
#include "ospi_flash.h"
4141
#include "pendsv.h"
42+
#include "se_services.h"
4243
#include "system_tick.h"
4344

4445
extern uint8_t __StackTop, __StackLimit;
@@ -60,6 +61,7 @@ void _start(void) {
6061
MICROPY_BOARD_STARTUP();
6162

6263
pendsv_init();
64+
se_services_init();
6365

6466
MICROPY_BOARD_EARLY_INIT();
6567

ports/alif/se_services.c

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2024 OpenMV LLC.
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <stdio.h>
28+
#include <string.h>
29+
30+
#include "irq.h"
31+
#include "se_services.h"
32+
33+
#include "mhu.h"
34+
#include "services_lib_bare_metal.h"
35+
#include "services_lib_protocol.h"
36+
37+
#include "py/mphal.h"
38+
39+
// MHU indices.
40+
#define MHU_M55_SE_MHU0 0
41+
#define MAX_MHU 1
42+
43+
// The following timeout is implemented in se_services_handle.c as a
44+
// simple loop busy polling on a variable set from an IRQ.
45+
#define TIMEOUT 10000000
46+
47+
typedef struct {
48+
volatile unsigned int RST_CTRL; // 0x1A010318
49+
volatile unsigned int RST_STAT; // 0x1A01031C
50+
} CPU_Type;
51+
52+
// HE CPU register flags
53+
#define RST_CTRL_CPUWAIT_MASK (1 << 0)
54+
#define RST_CTRL_RST_REQ_MASK (1 << 1)
55+
#define RST_STAT_RST_ACK_MASK (3 << 1)
56+
#define HE_CPU ((CPU_Type *)0x1A010318)
57+
58+
static const uint32_t mhu_sender_base_address_list[MAX_MHU] = {
59+
MHU_SESS_S_TX_BASE,
60+
};
61+
62+
static const uint32_t mhu_receiver_base_address_list[MAX_MHU] = {
63+
MHU_SESS_S_RX_BASE,
64+
};
65+
66+
// Must be aligned as a uint32_t.
67+
static uint32_t packet_buffer[SERVICES_MAX_PACKET_BUFFER_SIZE / sizeof(uint32_t)];
68+
69+
static mhu_driver_out_t mhu_driver_out;
70+
static uint32_t se_services_handle;
71+
72+
void MHU_SESS_S_TX_IRQHandler(void) {
73+
mhu_driver_out.sender_irq_handler(MHU_M55_SE_MHU0);
74+
}
75+
76+
void MHU_SESS_S_RX_IRQHandler(void) {
77+
mhu_driver_out.receiver_irq_handler(MHU_M55_SE_MHU0);
78+
}
79+
80+
int dummy_printf(const char *fmt, ...) {
81+
(void)fmt;
82+
return 0;
83+
}
84+
85+
void se_services_init(void) {
86+
// Initialize MHU.
87+
mhu_driver_in_t mhu_driver_in;
88+
mhu_driver_in.sender_base_address_list = (uint32_t *)mhu_sender_base_address_list;
89+
mhu_driver_in.receiver_base_address_list = (uint32_t *)mhu_receiver_base_address_list;
90+
mhu_driver_in.mhu_count = MAX_MHU;
91+
mhu_driver_in.send_msg_acked_callback = SERVICES_send_msg_acked_callback;
92+
mhu_driver_in.rx_msg_callback = SERVICES_rx_msg_callback;
93+
mhu_driver_in.debug_print = NULL; // not currently used by MHU_driver_initialize
94+
MHU_driver_initialize(&mhu_driver_in, &mhu_driver_out);
95+
96+
// Initialize SE services.
97+
services_lib_t services_init_params = {
98+
.packet_buffer_address = (uint32_t)packet_buffer,
99+
.fn_send_mhu_message = mhu_driver_out.send_message,
100+
.fn_wait_ms = NULL, // not currently used by services_host_handler.c
101+
.wait_timeout = TIMEOUT,
102+
.fn_print_msg = dummy_printf,
103+
};
104+
SERVICES_initialize(&services_init_params);
105+
106+
// Create SE services channel for sending requests.
107+
se_services_handle = SERVICES_register_channel(MHU_M55_SE_MHU0, 0);
108+
109+
// Enable MHU interrupts.
110+
NVIC_ClearPendingIRQ(MHU_SESS_S_RX_IRQ_IRQn);
111+
NVIC_SetPriority(MHU_SESS_S_RX_IRQ_IRQn, IRQ_PRI_MHU);
112+
NVIC_EnableIRQ(MHU_SESS_S_RX_IRQ_IRQn);
113+
NVIC_ClearPendingIRQ(MHU_SESS_S_TX_IRQ_IRQn);
114+
NVIC_SetPriority(MHU_SESS_S_TX_IRQ_IRQn, IRQ_PRI_MHU);
115+
NVIC_EnableIRQ(MHU_SESS_S_TX_IRQ_IRQn);
116+
117+
// Send heartbeat services requests until one succeeds.
118+
SERVICES_synchronize_with_se(se_services_handle);
119+
}
120+
121+
void se_services_dump_device_data(void) {
122+
uint32_t error_code;
123+
124+
uint8_t revision[80];
125+
SERVICES_get_se_revision(se_services_handle, revision, &error_code);
126+
127+
SERVICES_version_data_t data;
128+
SERVICES_system_get_device_data(se_services_handle, &data, &error_code);
129+
130+
printf("SE revision: %s\n", revision);
131+
printf("ALIF_PN: %s\n", data.ALIF_PN);
132+
printf("Raw device data:\n");
133+
for (int i = 0; i < sizeof(data); ++i) {
134+
printf(" %02x", ((uint8_t *)&data)[i]);
135+
if (i % 16 == 15) {
136+
printf("\n");
137+
}
138+
}
139+
printf("\n");
140+
}
141+
142+
void se_services_get_unique_id(uint8_t id[5]) {
143+
uint32_t error_code;
144+
SERVICES_version_data_t data;
145+
SERVICES_system_get_device_data(se_services_handle, &data, &error_code);
146+
// The MfgData has 5 bytes of valid data, at least on REV_B2.
147+
memcpy(id, data.MfgData, 5);
148+
}
149+
150+
__attribute__((noreturn)) void se_services_reset_soc(void) {
151+
SERVICES_boot_reset_soc(se_services_handle);
152+
NVIC_SystemReset();
153+
}
154+
155+
uint64_t se_services_rand64(void) {
156+
// If the SE core is not ready then the return value can be
157+
// SERVICES_REQ_NOT_ACKNOWLEDGE. So retry a few times.
158+
for (int retry = 0; retry < 100; ++retry) {
159+
uint64_t value;
160+
int32_t error_code;
161+
uint32_t ret = SERVICES_cryptocell_get_rnd(se_services_handle, sizeof(uint64_t), &value, &error_code);
162+
if (ret == SERVICES_REQ_SUCCESS) {
163+
return value;
164+
}
165+
}
166+
167+
// No random number available.
168+
return 0;
169+
}
170+
171+
uint32_t se_services_enable_clock(clock_enable_t clock, bool enable) {
172+
uint32_t error_code;
173+
SERVICES_clocks_enable_clock(se_services_handle, clock, enable, &error_code);
174+
return error_code;
175+
}
176+
177+
uint32_t se_services_select_pll_source(pll_source_t source, pll_target_t target) {
178+
uint32_t error_code;
179+
SERVICES_clocks_select_pll_source(se_services_handle, source, target, &error_code);
180+
return error_code;
181+
}
182+
183+
uint32_t se_services_get_run_profile(run_profile_t *profile) {
184+
uint32_t error_code;
185+
SERVICES_get_run_cfg(se_services_handle, profile, &error_code);
186+
return error_code;
187+
}
188+
189+
uint32_t se_services_set_run_profile(run_profile_t *profile) {
190+
uint32_t error_code;
191+
SERVICES_set_run_cfg(se_services_handle, profile, &error_code);
192+
return error_code;
193+
}
194+
195+
uint32_t se_services_get_off_profile(off_profile_t *profile) {
196+
uint32_t error_code;
197+
SERVICES_get_off_cfg(se_services_handle, profile, &error_code);
198+
return error_code;
199+
}
200+
201+
uint32_t se_services_set_off_profile(off_profile_t *profile) {
202+
uint32_t error_code;
203+
SERVICES_set_off_cfg(se_services_handle, profile, &error_code);
204+
return error_code;
205+
}
206+
207+
uint32_t se_services_boot_process_toc_entry(const uint8_t *image_id) {
208+
uint32_t error_code;
209+
SERVICES_boot_process_toc_entry(se_services_handle, image_id, &error_code);
210+
return error_code;
211+
}
212+
213+
uint32_t se_services_boot_cpu(uint32_t cpu_id, uint32_t address) {
214+
uint32_t error_code;
215+
SERVICES_boot_cpu(se_services_handle, cpu_id, address, &error_code);
216+
return error_code;
217+
}
218+
219+
uint32_t se_services_boot_reset_cpu(uint32_t cpu_id) {
220+
uint32_t error_code;
221+
if (HE_CPU->RST_CTRL & RST_CTRL_CPUWAIT_MASK) {
222+
// CPU held in reset
223+
return SERVICES_REQ_SUCCESS;
224+
}
225+
226+
for (mp_uint_t start = mp_hal_ticks_ms(); ; mp_hal_delay_ms(1)) {
227+
uint32_t ret = SERVICES_boot_reset_cpu(se_services_handle, cpu_id, &error_code);
228+
if (ret != SERVICES_REQ_SUCCESS) {
229+
return error_code;
230+
}
231+
232+
if ((HE_CPU->RST_STAT & RST_STAT_RST_ACK_MASK) == 0x4) {
233+
return SERVICES_REQ_SUCCESS;
234+
}
235+
236+
if ((mp_hal_ticks_ms() - start) >= 100) {
237+
return SERVICES_REQ_TIMEOUT;
238+
}
239+
}
240+
return error_code;
241+
}
242+
243+
uint32_t se_services_boot_release_cpu(uint32_t cpu_id) {
244+
uint32_t error_code;
245+
SERVICES_boot_release_cpu(se_services_handle, cpu_id, &error_code);
246+
return error_code;
247+
}

ports/alif/se_services.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2024 OpenMV LLC.
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
#ifndef MICROPY_INCLUDED_ALIF_SE_SERVICES_H
27+
#define MICROPY_INCLUDED_ALIF_SE_SERVICES_H
28+
29+
#include "services_lib_api.h"
30+
31+
void se_services_init(void);
32+
void se_services_dump_device_data(void);
33+
void se_services_get_unique_id(uint8_t id[5]);
34+
__attribute__((noreturn)) void se_services_reset_soc(void);
35+
uint64_t se_services_rand64(void);
36+
uint32_t se_services_enable_clock(clock_enable_t clock, bool enable);
37+
uint32_t se_services_select_pll_source(pll_source_t source, pll_target_t target);
38+
uint32_t se_services_get_run_profile(run_profile_t *profile);
39+
uint32_t se_services_set_run_profile(run_profile_t *profile);
40+
uint32_t se_services_get_off_profile(off_profile_t *profile);
41+
uint32_t se_services_set_off_profile(off_profile_t *profile);
42+
43+
uint32_t se_services_boot_process_toc_entry(const uint8_t *image_id);
44+
uint32_t se_services_boot_cpu(uint32_t cpu_id, uint32_t address);
45+
uint32_t se_services_boot_reset_cpu(uint32_t cpu_id);
46+
uint32_t se_services_boot_release_cpu(uint32_t cpu_id);
47+
#endif // MICROPY_INCLUDED_ALIF_SE_SERVICES_H

0 commit comments

Comments
 (0)