1 /*
2  * Copyright (c) 2019 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include <stdint.h>
7 #include <nrf.h>
8 #include <hal/nrf_gpio.h>
9 #include <hal/nrf_ficr.h>
10 
11 #if defined(CONFIG_TRUSTED_EXECUTION_NONSECURE)
12 #if NRF_GPIO_HAS_SEL
13 void soc_secure_gpio_pin_mcu_select(uint32_t pin_number, nrf_gpio_pin_sel_t mcu);
14 #endif
15 
16 int soc_secure_mem_read(void *dst, void *src, size_t len);
17 
18 #else /* defined(CONFIG_TRUSTED_EXECUTION_NONSECURE) */
19 #if NRF_GPIO_HAS_SEL
soc_secure_gpio_pin_mcu_select(uint32_t pin_number,nrf_gpio_pin_sel_t mcu)20 static inline void soc_secure_gpio_pin_mcu_select(uint32_t pin_number, nrf_gpio_pin_sel_t mcu)
21 {
22 	nrf_gpio_pin_control_select(pin_number, mcu);
23 }
24 #endif /* NRF_GPIO_HAS_SEL */
25 
soc_secure_mem_read(void * dst,void * src,size_t len)26 static inline int soc_secure_mem_read(void *dst, void *src, size_t len)
27 {
28 	(void)memcpy(dst, src, len);
29 	return 0;
30 }
31 
32 #endif /* defined CONFIG_TRUSTED_EXECUTION_NONSECURE */
33 
34 /* Include these soc_secure_* functions only when the FICR is mapped as secure only */
35 #if defined(NRF_FICR_S)
36 #if defined(CONFIG_TRUSTED_EXECUTION_NONSECURE)
37 #if defined(CONFIG_SOC_HFXO_CAP_INTERNAL) || \
38 	DT_ENUM_HAS_VALUE(DT_NODELABEL(hfxo), load_capacitors, internal)
soc_secure_read_xosc32mtrim(void)39 static inline uint32_t soc_secure_read_xosc32mtrim(void)
40 {
41 	uint32_t xosc32mtrim;
42 	int err;
43 
44 	err = soc_secure_mem_read(&xosc32mtrim,
45 				  (void *)&NRF_FICR_S->XOSC32MTRIM,
46 				  sizeof(xosc32mtrim));
47 	__ASSERT(err == 0, "Secure read error (%d)", err);
48 
49 	return xosc32mtrim;
50 }
51 #endif /* defined(CONFIG_SOC_HFXO_CAP_INTERNAL) */
52 
soc_secure_read_deviceid(uint32_t deviceid[2])53 static inline void soc_secure_read_deviceid(uint32_t deviceid[2])
54 {
55 	int err;
56 
57 	err = soc_secure_mem_read(deviceid,
58 				 (void *)&NRF_FICR_S->INFO.DEVICEID,
59 				 2 * sizeof(uint32_t));
60 	__ASSERT(err == 0, "Secure read error (%d)", err);
61 }
62 
63 #else /* defined(CONFIG_TRUSTED_EXECUTION_NONSECURE) */
64 #if defined(CONFIG_SOC_HFXO_CAP_INTERNAL) || \
65 	DT_ENUM_HAS_VALUE(DT_NODELABEL(hfxo), load_capacitors, internal)
soc_secure_read_xosc32mtrim(void)66 static inline uint32_t soc_secure_read_xosc32mtrim(void)
67 {
68 	return NRF_FICR_S->XOSC32MTRIM;
69 }
70 #endif /* defined(CONFIG_SOC_HFXO_CAP_INTERNAL) */
71 
soc_secure_read_deviceid(uint32_t deviceid[2])72 static inline void soc_secure_read_deviceid(uint32_t deviceid[2])
73 {
74 	deviceid[0] = nrf_ficr_deviceid_get(NRF_FICR_S, 0);
75 	deviceid[1] = nrf_ficr_deviceid_get(NRF_FICR_S, 1);
76 }
77 
78 #endif /* defined CONFIG_TRUSTED_EXECUTION_NONSECURE */
79 #endif /* defined(NRF_FICR_S) */
80