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 #if defined(CONFIG_SOC_HFXO_CAP_INTERNAL)
soc_secure_read_xosc32mtrim(void)19 static inline uint32_t soc_secure_read_xosc32mtrim(void)
20 {
21 	uint32_t xosc32mtrim;
22 	int err;
23 
24 	err = soc_secure_mem_read(&xosc32mtrim,
25 				  (void *)&NRF_FICR_S->XOSC32MTRIM,
26 				  sizeof(xosc32mtrim));
27 	__ASSERT(err == 0, "Secure read error (%d)", err);
28 
29 	return xosc32mtrim;
30 }
31 #endif /* defined(CONFIG_SOC_HFXO_CAP_INTERNAL) */
32 
soc_secure_read_deviceid(uint32_t deviceid[2])33 static inline void soc_secure_read_deviceid(uint32_t deviceid[2])
34 {
35 	int err;
36 
37 	err = soc_secure_mem_read(deviceid,
38 				 (void *)&NRF_FICR_S->INFO.DEVICEID,
39 				 2 * sizeof(uint32_t));
40 	__ASSERT(err == 0, "Secure read error (%d)", err);
41 }
42 
43 #else /* defined(CONFIG_TRUSTED_EXECUTION_NONSECURE) */
44 
soc_secure_mem_read(void * dst,void * src,size_t len)45 static inline int soc_secure_mem_read(void *dst, void *src, size_t len)
46 {
47 	(void)memcpy(dst, src, len);
48 	return 0;
49 }
50 
51 #if NRF_GPIO_HAS_SEL
soc_secure_gpio_pin_mcu_select(uint32_t pin_number,nrf_gpio_pin_sel_t mcu)52 static inline void soc_secure_gpio_pin_mcu_select(uint32_t pin_number,
53 						  nrf_gpio_pin_sel_t mcu)
54 {
55 	nrf_gpio_pin_control_select(pin_number, mcu);
56 }
57 #endif /* NRF_GPIO_HAS_SEL */
58 
59 #if defined(CONFIG_SOC_HFXO_CAP_INTERNAL)
soc_secure_read_xosc32mtrim(void)60 static inline uint32_t soc_secure_read_xosc32mtrim(void)
61 {
62 	return NRF_FICR_S->XOSC32MTRIM;
63 }
64 #endif /* defined(CONFIG_SOC_HFXO_CAP_INTERNAL) */
65 
soc_secure_read_deviceid(uint32_t deviceid[2])66 static inline void soc_secure_read_deviceid(uint32_t deviceid[2])
67 {
68 	deviceid[0] = nrf_ficr_deviceid_get(NRF_FICR, 0);
69 	deviceid[1] = nrf_ficr_deviceid_get(NRF_FICR, 1);
70 }
71 #endif /* defined CONFIG_TRUSTED_EXECUTION_NONSECURE */
72