1 /*
2  * Copyright 2021-2022, 2024 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef __FWK_PLATFORM_ICS_H__
10 #define __FWK_PLATFORM_ICS_H__
11 
12 /*!
13  * @addtogroup FWK_Platform_module
14  * @{
15  */
16 /*!
17  * @addtogroup FWK_Platform_ICS
18  * The FWK_Platform_ICS module
19  *
20  * Framework platform inter core service module provides services to manage communication between the 2 device cores.
21  * @{
22  */
23 
24 /* -------------------------------------------------------------------------- */
25 /*                                  Includes                                  */
26 /* -------------------------------------------------------------------------- */
27 
28 #include "EmbeddedTypes.h"
29 
30 /* -------------------------------------------------------------------------- */
31 /*                                Public macros                               */
32 /* -------------------------------------------------------------------------- */
33 #define MAX_TAG_SZ        40
34 #define MAX_SHA_SZ        20
35 #define MAX_VARIANT_SZ    25
36 #define MAX_BUILD_TYPE_SZ 10
37 
38 #ifndef NBU_VERSION_DBG
39 #define NBU_VERSION_DBG 0
40 #endif
41 
42 /* maximum size for API input parameters in bytes */
43 #define NBU_API_MAX_PARAM_LENGTH 40U
44 
45 /* maximum size for API output parameters including status in bytes.
46    should be same as the one defined in the NBU core  */
47 #define NBU_API_MAX_RETURN_PARAM_LENGTH 38U
48 
49 /* -------------------------------------------------------------------------- */
50 /*                           Public type definitions                          */
51 /* -------------------------------------------------------------------------- */
52 
53 /*! \brief  NBU core build information.
54  *
55  * Data structure containing NBU build information.
56  * WARNING: this must be in sync with equivalent NBU structure
57  */
58 typedef PACKED_STRUCT NbuInfo_tag
59 {
60     uint8_t versionNumber[3];
61     uint8_t repo_digest[MAX_SHA_SZ];
62     char    repo_tag[MAX_TAG_SZ];
63     char    variant[MAX_VARIANT_SZ];
64     char    build_type[MAX_BUILD_TYPE_SZ];
65     uint8_t versionBuildNo;
66 }
67 NbuInfo_t;
68 
69 typedef PACKED_STRUCT NbuDbgMemInfo_tag
70 {
71     uint8_t  poolId;
72     uint8_t  reserved;
73     uint16_t bufferSize;
74 }
75 NbuDbgMemInfo_t;
76 /*! \brief  FWK ICS message type.
77  *
78  *  \details enumarated values for FWK ICS messages
79  */
80 typedef enum
81 {
82     gFwkSrvNbu2HostFirst_c                      = 0U,
83     gFwkSrvNbuInitDone_c                        = 0x1U,
84     gFwkSrvNbuVersionIndication_c               = 0x2U,
85     gFwkSrvNbuApiIndication_c                   = 0x3U,
86     gFwkSrvNbuMemFullIndication_c               = 0x4U,
87     gFwkSrvHostSetLowPowerConstraint_c          = 0x5U,
88     gFwkSrvHostReleaseLowPowerConstraint_c      = 0x6U,
89     gFwkSrvFroNotification_c                    = 0x7U,
90     gFwkSrvNbuIssueIndication_c                 = 0x8U,
91     gFwkSrvNbuSecurityEventIndication_c         = 0x9U,
92     gFwkSrvNbuRequestRngSeed_c                  = 0xAU,
93     gFwkSrvNbu2HostLast_c                       = 0xBU,
94     gFwkSrvHost2NbuFirst_c                      = 0x80U,
95     gFwkSrvNbuVersionRequest_c                  = 0x81U,
96     gFwkSrvXtal32MTrimIndication_c              = 0x82U,
97     gFwkSrvNbuApiRequest_c                      = 0x83U,
98     gFwkTemperatureIndication_c                 = 0x84U, /*!< Receive Temperature value */
99     gFwkSrvHostChipRevision_c                   = 0x85U, /*!< Receive Chip Revision value */
100     gFwkSrvNbuSecureModeRequest_c               = 0x86U,
101     gFwkSrvNbuWakeupDelayLpoCycle_c             = 0x87U, /*!< BLE timer wakeup delay in number of 3.2kHz period */
102     gFwkSrvNbuUpdateFrequencyConstraintFromHost = 0x88U,
103     gFwkSrvNbuSetRfSfcConfig_c                  = 0x89U,
104     gFwkSrvFroEnableNotification_c              = 0x8AU,
105     gFwkSrvRngReseed_c                          = 0x8BU,
106     gFwkSrvHost2NbuLast_c                       = 0x8CU,
107 } eFwkSrvMsgType;
108 
109 typedef int32_t (*fwksrv_lowpower_constraint_func_t)(int32_t power_mode);
110 
111 typedef struct
112 {
113     fwksrv_lowpower_constraint_func_t fwkSrvSetLowPowerConstraint;
114     fwksrv_lowpower_constraint_func_t fwkSrvReleaseLowPowerConstraint;
115 } FwkSrv_LowPowerConstraintCallbacks_t;
116 
117 typedef void (*nbu_memory_error_callback_t)(NbuDbgMemInfo_t *memInfo);
118 
119 typedef void (*nbu_issue_callback_t)(void);
120 
121 typedef void (*nbu_security_event_callback_t)(uint32_t securityEventBitmask);
122 
123 typedef void (*PLATFORM_FroDebugCallback_t)(uint16_t freq, int16_t ppm_mean, int16_t ppm, uint16_t fro_trim);
124 
125 /* -------------------------------------------------------------------------- */
126 /*                              Public prototypes                             */
127 /* -------------------------------------------------------------------------- */
128 
129 /*!
130  * \brief Initialization of the PLATFORM Service channel
131  *
132  * \return int 0 if success, 1 if already initialized, negative value if error.
133  */
134 int PLATFORM_FwkSrvInit(void);
135 
136 /*!
137  * \brief Sends message to peer CPU
138  *
139  * \param[in] msg_type Message type (from eFwkSrvMsgType enum)
140  * \param[in] msg pointer to message buffer
141  * \param[in] msg_lg size of the message (in bytes)
142  * \return int 0 if success, -1 if out of memory, -2 if sending failed, -4 if NBU not initialized yet
143  */
144 int PLATFORM_FwkSrvSendPacket(eFwkSrvMsgType msg_type, void *msg, uint16_t msg_lg);
145 
146 /*!
147  * \brief Send NbuInfo request to NBU and spin waiting for response.
148  *
149  * \param[in] nbu_info_p pointer to externally allocated NbuInfo_t
150  * \return int 0 if OK, -1 in case of Rpmsg error, -10 if NBU did not respond in time.
151  */
152 int PLATFORM_GetNbuInfo(NbuInfo_t *nbu_info_p);
153 
154 /*!
155  * \brief Send NbuInfo request to NBU and spin waiting for response.
156  *
157  * \return int 0 if OK, < 0 in case of error: -3 if chip revision invalid
158  */
159 int PLATFORM_SendChipRevision(void);
160 
161 /*!
162  * \brief Call API located on the NBU core. This function will wait until the response
163  *        from the NBU is received.
164  * \param[out] api_return Return status and results from remote API execution
165  * \param[in]  api_id API identifier
166  * \param[in]  fmt Argument size specifier ending with 0 (ex. {1U,2U,4U,0U} )
167  * \param[in]  tab Array of arguments
168  * \param[in]  nb_returns Number of bytes returned API status and optional results
169  * \return bool_t 0 if OK otherwise error.\n
170  *         In case of success, g_nbu_api_ind_status contains the returned value of the API.
171  */
172 bool_t PLATFORM_NbuApiReq(uint8_t *api_return, uint16_t api_id, const uint8_t *fmt, uint32_t *tab, uint32_t nb_returns);
173 
174 /*!
175  * \brief Registers callback triggered when NBU is running out of memory
176  *
177  * \param[in] cb function pointer to callback. Can be NULL to unregister
178  *            the callback.
179  */
180 void PLATFORM_RegisterNbuMemErrorCallback(nbu_memory_error_callback_t cb);
181 
182 /*!
183  * \brief Registers callback to set/release low power constraint requested by the NBU
184  *
185  * \param[in] callbacks pointer to the callback structure
186  *                      WARNING: It will be used directly by the module so DO NOT allocate the structure on the stack!
187  */
188 void PLATFORM_FwkSrvRegisterLowPowerCallbacks(const FwkSrv_LowPowerConstraintCallbacks_t *callbacks);
189 
190 /*!
191  * \brief Sends a new config to the RF_SFC module
192  *
193  * \param[in] config pointer to the config to send, see sfc_config_t for details
194  * \param[in] size size of the config structure
195  */
196 void PLATFORM_FwkSrvSetRfSfcConfig(void *config, uint16_t size);
197 
198 /*!
199  * \brief Register a callback for notification purpose of the SFC module
200  *
201  * \details In a first step the notifications needs to be enabled with PLATFORM_EnableFroNotificationInfo()
202  *
203  * \param[in] cb callback that will be used to handle SFC measurement
204  */
205 void PLATFORM_RegisterFroNotificationCallback(PLATFORM_FroDebugCallback_t cb);
206 
207 /*!
208  * \brief Register callback called when nbu has non recoverable Radio Issue
209  *
210  * \param[in] cb error callback for platform module
211  *
212  */
213 void PLATFORM_RegisterNbuIssueCb(nbu_issue_callback_t cb);
214 
215 /*!
216  * \brief Register callback called when nbu has detected a security issue (vulnerabilty attack).
217  *
218  * \param[in] cb error callback for platform module
219  *
220  */
221 void PLATFORM_RegisterSecurityEventCb(nbu_security_event_callback_t cb);
222 
223 /*!
224  * \brief Sends a a message to the RF_SFC module to enable notifications. The RF_SFC module will send a message to the
225  *       host to update some values, see PLATFORM_FroDebugCallback_t to know what are these values.
226  *
227  * \details Needs to register a callback via PLATFORM_RegisterFroNotificationCallback() to use the values send by
228  *          the controller
229  *
230  * \param[in] enable
231  */
232 void PLATFORM_EnableFroNotification(int8_t enable);
233 
234 /*!
235  * @}  end of FWK_Platform_ICS addtogroup
236  */
237 /*!
238  * @}  end of FWK_Platform_module addtogroup
239  */
240 #endif /* __FWK_PLATFORM_ICS_H__ */
241