1 /*
2  * Copyright 2010-2024 NXP
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef _PHNXPNCIHAL_H_
17 #define _PHNXPNCIHAL_H_
18 
19 #include <hardware/nfc.h>
20 #include <phNxpNciHal_utils.h>
21 
22 #include <vector>
23 
24 #include "NxpMfcReader.h"
25 #include "NxpNfcCapability.h"
26 #ifdef NXP_BOOTTIME_UPDATE
27 #include "eSEClientIntf.h"
28 #endif
29 
30 #include "eSEClientExtns.h"
31 #include "phNxpNciHal_IoctlOperations.h"
32 
33 /********************* Definitions and structures *****************************/
34 #define MAX_RETRY_COUNT 5
35 #define NCI_MAX_DATA_LEN 300
36 #define NCI_POLL_DURATION 500
37 #define HAL_NFC_ENABLE_I2C_FRAGMENTATION_EVT 0x07
38 #define NCI_VERSION_2_0 0x20
39 #define NCI_VERSION_1_1 0x11
40 #define NCI_VERSION_1_0 0x10
41 #define NCI_VERSION_UNKNOWN 0x00
42 #define SNXXX_NXP_AUTH_TIMEOUT_BUF_LEN 0x05
43 #define PN557_NXP_AUTH_TIMEOUT_BUF_LEN 0x0C
44 
45 /*Mem alloc with 8 byte alignment*/
46 #define size_align(sz) ((((sz)-1) | 7) + 1)
47 #define nxp_malloc(size) malloc(size_align((size)))
48 
49 typedef void(phNxpNciHal_control_granted_callback_t)();
50 
51 #define FW_DBG_REASON_AVAILABLE (0xA3)
52 
53 #define HOST_ID 0x00
54 #define ESE_ID 0x01
55 #define UICC1_ID 0x02
56 #define UICC2_ID 0x04
57 #define UICC3_ID 0x08
58 #define ENABLE_T4T_CE 0x03
59 /* NCI Data */
60 
61 //#define NCI_MT_CMD 0x20
62 //#define NCI_MT_RSP 0x40
63 //#define NCI_MT_NTF 0x60
64 #define NCI_OID_SYSTEM_TERMPERATURE_INFO_NTF 0x42
65 #define CORE_RESET_TRIGGER_TYPE_CORE_RESET_CMD_RECEIVED 0x02
66 #define CORE_RESET_TRIGGER_TYPE_POWERED_ON 0x01
67 #define NCI2_0_CORE_RESET_TRIGGER_TYPE_OVER_TEMPERATURE ((uint8_t)0xA1)
68 #define CORE_RESET_TRIGGER_TYPE_UNRECOVERABLE_ERROR 0x00
69 #define CORE_RESET_TRIGGER_TYPE_FW_ASSERT ((uint8_t)0xA0)
70 #define CORE_RESET_TRIGGER_TYPE_WATCHDOG_RESET ((uint8_t)0xA3)
71 #define CORE_RESET_TRIGGER_TYPE_INPUT_CLOCK_LOST ((uint8_t)0xA4)
72 //#define NCI_MSG_CORE_RESET           0x00
73 //#define NCI_MSG_CORE_INIT            0x01
74 #define NCI_MT_MASK 0xE0
75 #define NCI_OID_MASK 0x3F
76 /* GID: Group Identifier (byte 0) */
77 #define NCI_GID_MASK 0x0F
78 #define ORIG_NXPHAL 0x01
79 #define ORIG_LIBNFC 0x02
80 #define NXP_PROPCMD_GID 0x2F
81 #define NXP_FLUSH_SRAM_AO_TO_FLASH 0x21
82 #define NXP_CORE_GET_CONFIG_CMD 0x03
83 #define NXP_CORE_SET_CONFIG_CMD 0x02
84 #define NXP_MAX_CONFIG_STRING_LEN 260
85 #define NCI_HEADER_SIZE 3
86 
87 #define NXP_PHONE_OFF_NFC_OFF_CE_NOT_SUPPORTED 0x00
88 #define NXP_PHONE_OFF_NFC_OFF_T4T_CE_SUPPORTED 0x03
89 
90 #define CORE_RESET_NTF_RECOVERY_REQ_COUNT 0x03
91 
92 typedef struct nci_data {
93   uint16_t len;
94   uint8_t p_data[NCI_MAX_DATA_LEN];
95 } nci_data_t;
96 
97 typedef enum {
98   HAL_STATUS_CLOSE = 0,
99   HAL_STATUS_OPEN,
100   HAL_STATUS_MIN_OPEN
101 } phNxpNci_HalStatus;
102 
103 typedef enum {
104   HAL_CLOSED, /* Either hal_close() done or hal_open() is on going */
105   HAL_OPENED, /* hal_open() is done */
106   HAL_OPEN_CORE_INITIALIZING /* core_initialized() ongoing. will be set back to
107                                 HAL_OPENED once done. */
108 } phNxpNci_HalOpenStatus;
109 
110 typedef enum {
111   HAL_NFC_FW_UPDATE_INVALID = 0x00,
112   HAL_NFC_FW_UPDATE_START,
113   HAL_NFC_FW_UPDATE_SCUCCESS,
114   HAL_NFC_FW_UPDATE_FAILED,
115 } HalNfcFwUpdateStatus;
116 
117 typedef enum {
118   GPIO_UNKNOWN = 0x00,
119   GPIO_STORE = 0x01,
120   GPIO_STORE_DONE = 0x02,
121   GPIO_RESTORE = 0x10,
122   GPIO_RESTORE_DONE = 0x20,
123   GPIO_CLEAR = 0xFF
124 } phNxpNciHal_GpioInfoState;
125 #ifdef NXP_BOOTTIME_UPDATE
126 extern ese_update_state_t ese_update;
127 #endif
128 typedef struct phNxpNciGpioInfo {
129   phNxpNciHal_GpioInfoState state;
130   uint8_t values[2];
131 } phNxpNciGpioInfo_t;
132 
133 /* Macros to enable and disable extensions */
134 #define HAL_ENABLE_EXT() (nxpncihal_ctrl.hal_ext_enabled = 1)
135 #define HAL_DISABLE_EXT() (nxpncihal_ctrl.hal_ext_enabled = 0)
136 typedef struct phNxpNciInfo {
137   uint8_t nci_version;
138   bool_t wait_for_ntf;
139   uint8_t lastResetNtfReason;
140 } phNxpNciInfo_t;
141 /* NCI Control structure */
142 typedef struct phNxpNciHal_Control {
143   phNxpNci_HalStatus halStatus; /* Indicate if hal is open or closed */
144   pthread_t client_thread;      /* Integration thread handle */
145   uint8_t thread_running;       /* Thread running if set to 1, else set to 0 */
146   phLibNfc_sConfig_t gDrvCfg;   /* Driver config data */
147 
148   /* Rx data */
149   uint8_t* p_rx_data;
150   uint16_t rx_data_len;
151 
152   /* Rx data */
153   uint8_t* p_rx_ese_data;
154   uint16_t rx_ese_data_len;
155 
156   /* libnfc-nci callbacks */
157   nfc_stack_callback_t* p_nfc_stack_cback;
158   nfc_stack_data_callback_t* p_nfc_stack_data_cback;
159 
160   /* control granted callback */
161   phNxpNciHal_control_granted_callback_t* p_control_granted_cback;
162 
163   /* HAL open status */
164   phNxpNci_HalOpenStatus hal_open_status;
165 
166   /* HAL extensions */
167   uint8_t hal_ext_enabled;
168 
169   /* Waiting semaphore */
170   phNxpNciHal_Sem_t ext_cb_data;
171   sem_t syncSpiNfc;
172 
173   uint16_t cmd_len;
174   uint8_t p_cmd_data[NCI_MAX_DATA_LEN];
175   uint16_t rsp_len;
176   uint8_t p_rsp_data[NCI_MAX_DATA_LEN];
177 
178   uint16_t vendor_msg_len;
179   uint8_t vendor_msg[NCI_MAX_DATA_LEN];
180 
181   /* retry count used to force download */
182   uint16_t retry_cnt;
183   uint8_t read_retry_cnt;
184   phNxpNciInfo_t nci_info;
185   uint8_t hal_boot_mode;
186   bool_t isCoreRstForFwDnld;
187   /* to store and restore gpio values */
188   phNxpNciGpioInfo_t phNxpNciGpioInfo;
189   tNFC_chipType chipType;
190   bool_t power_reset_triggered;
191   bool_t isUlpdetModeEnabled;
192 } phNxpNciHal_Control_t;
193 
194 typedef struct {
195   uint8_t fw_update_reqd;
196   uint8_t rf_update_reqd;
197 } phNxpNciHal_FwRfupdateInfo_t;
198 
199 typedef struct phNxpNciClock {
200   bool_t isClockSet;
201   uint8_t p_rx_data[20];
202   bool_t issetConfig;
203 } phNxpNciClock_t;
204 
205 typedef struct phNxpNciRfSetting {
206   bool_t isGetRfSetting;
207   vector<uint8_t> p_rx_data;
208 } phNxpNciRfSetting_t;
209 
210 typedef struct phNxpNciMwEepromArea {
211   bool_t isGetEepromArea;
212   uint8_t p_rx_data[32];
213 } phNxpNciMwEepromArea_t;
214 
215 struct phRfMiscSettings {
216   const char* configName;
217   int configPosition;
218   uint8_t configBitMask;
219 };
220 
221 enum { SE_TYPE_ESE, SE_TYPE_EUICC, SE_TYPE_UICC, SE_TYPE_UICC2, NUM_SE_TYPES };
222 
223 typedef enum {
224   ANTENNA_CHECK_STATUS,
225   ANTENNA_SET_VDDPA
226 } phNxpNci_Antenaa_Actions_type_t;
227 
228 typedef void (*fpVerInfoStoreInEeprom_t)();
229 typedef int (*fpVerifyCscEfsTest_t)(char* nfcc_csc, char* rffilepath,
230                                     char* fwfilepath);
231 typedef int (*fpRegRfFwDndl_t)(uint8_t* fw_update_req, uint8_t* rf_update_req,
232                                uint8_t skipEEPROMRead);
233 typedef int (*fpPropConfCover_t)(bool attached, int type);
234 typedef int (*fpDoAntennaActivity_t)(phNxpNci_Antenaa_Actions_type_t action);
235 void phNxpNciHal_initializeRegRfFwDnld();
236 void phNxpNciHal_deinitializeRegRfFwDnld();
237 /*set config management*/
238 
239 #define TOTAL_DURATION 0x00
240 #define ATR_REQ_GEN_BYTES_POLL 0x29
241 #define ATR_REQ_GEN_BYTES_LIS 0x61
242 #define LEN_WT 0x60
243 
244 /*PN557 Ven Pin Configuration*/
245 
246 #define PN557_VEN_CFG_VALUE 0x01
247 #define PN557_VEN_CFG_PULLDOWN 0x02
248 #define PN557_VEN_CFG_DEFAULT 0x03
249 
250 /*Whenever a new get cfg need to be sent,
251  * array must be updated with defined config type*/
252 static const uint8_t get_cfg_arr[] = {TOTAL_DURATION, ATR_REQ_GEN_BYTES_POLL,
253                                       ATR_REQ_GEN_BYTES_LIS, LEN_WT};
254 
255 //#define NXP_NFC_SET_CONFIG_PARAM_EXT 0xA0
256 //#define NXP_NFC_PARAM_ID_SWP2        0xD4
257 //#define NXP_NFC_PARAM_ID_SWPUICC3    0xDC
258 typedef enum {
259   EEPROM_RF_CFG,
260   EEPROM_FW_DWNLD,
261   EEPROM_WIREDMODE_RESUME_TIMEOUT,
262   EEPROM_ESE_SVDD_POWER,
263   EEPROM_ESE_POWER_EXT_PMU,
264   EEPROM_PROP_ROUTING,
265   EEPROM_ESE_SESSION_ID,
266   EEPROM_SWP1_INTF,
267   EEPROM_SWP1A_INTF,
268   EEPROM_SWP2_INTF,
269   EEPROM_FLASH_UPDATE,
270   EEPROM_AUTH_CMD_TIMEOUT,
271   EEPROM_GUARD_TIMER,
272   EEPROM_T4T_NFCEE_ENABLE,
273   EEPROM_AUTONOMOUS_MODE,
274   EEPROM_CE_PHONE_OFF_CFG,
275   EEPROM_ENABLE_VEN_CFG,
276   EEPROM_ISODEP_MERGE_SAK,
277   EEPROM_SRD_TIMEOUT,
278   EEPROM_UICC1_SESSION_ID,
279   EEPROM_UICC2_SESSION_ID,
280   EEPROM_CE_ACT_NTF,
281   EEPROM_UICC_HCI_CE_STATE,
282   EEPROM_EXT_FIELD_DETECT_MODE,
283   EEPROM_CONF_GPIO_CTRL,
284   EEPROM_SET_GPIO_VALUE,
285   EEPROM_POWER_TRACKER_ENABLE,
286   EEPROM_VDDPA,
287 } phNxpNci_EEPROM_request_type_t;
288 
289 typedef struct phNxpNci_EEPROM_info {
290   uint8_t request_mode;
291   phNxpNci_EEPROM_request_type_t request_type;
292   uint8_t update_mode;
293   uint8_t* buffer;
294   uint8_t bufflen;
295 } phNxpNci_EEPROM_info_t;
296 
297 typedef struct phNxpNci_getCfg_info {
298   bool_t isGetcfg;
299   uint8_t total_duration[4];
300   uint8_t total_duration_len;
301   uint8_t atr_req_gen_bytes[48];
302   uint8_t atr_req_gen_bytes_len;
303   uint8_t atr_res_gen_bytes[48];
304   uint8_t atr_res_gen_bytes_len;
305   uint8_t pmid_wt[3];
306   uint8_t pmid_wt_len;
307   uint8_t auth_cmd_timeout[PN557_NXP_AUTH_TIMEOUT_BUF_LEN];
308   uint8_t auth_cmd_timeoutlen;
309 } phNxpNci_getCfg_info_t;
310 typedef enum {
311   NFC_FORUM_PROFILE,
312   EMV_CO_PROFILE,
313   SRD_PROFILE,
314   INVALID_PROFILe
315 } phNxpNciProfile_t;
316 /* NXP Poll Profile control structure */
317 typedef struct phNxpNciProfile_Control {
318   phNxpNciProfile_t profile_type;
319   uint8_t bClkSrcVal; /* Holds the System clock source read from config file */
320   uint8_t
321       bClkFreqVal;  /* Holds the System clock frequency read from config file */
322   uint8_t bTimeout; /* Holds the Timeout Value */
323   uint8_t clkReqDelay; /* Holds default delay time before start clock request*/
324 } phNxpNciProfile_Control_t;
325 
326 /* Internal messages to handle callbacks */
327 #define NCI_HAL_OPEN_CPLT_MSG 0x411
328 #define NCI_HAL_CLOSE_CPLT_MSG 0x412
329 #define NCI_HAL_POST_INIT_CPLT_MSG 0x413
330 #define NCI_HAL_PRE_DISCOVER_CPLT_MSG 0x414
331 #define NCI_HAL_ERROR_MSG 0x415
332 #define NCI_HAL_HCI_NETWORK_RESET_MSG 0x416
333 #define NCI_HAL_RX_MSG 0xF01
334 #define NCI_HAL_VENDOR_MSG 0xF02
335 #define HAL_NFC_FW_UPDATE_STATUS_EVT 0x0A
336 
337 #define NCIHAL_CMD_CODE_LEN_BYTE_OFFSET (2U)
338 #define NCIHAL_CMD_CODE_BYTE_LEN (3U)
339 
340 /******************** NCI HAL exposed functions *******************************/
341 int phNxpNciHal_check_ncicmd_write_window(uint16_t cmd_len, uint8_t* p_cmd);
342 void phNxpNciHal_request_control(void);
343 void phNxpNciHal_release_control(void);
344 NFCSTATUS phNxpNciHal_send_get_cfgs();
345 int phNxpNciHal_write_unlocked(uint16_t data_len, const uint8_t* p_data,
346                                int origin);
347 NFCSTATUS request_EEPROM(phNxpNci_EEPROM_info_t* mEEPROM_info);
348 NFCSTATUS phNxpNciHal_fw_download(uint8_t seq_handler_offset = 0,
349                                   bool bIsNfccDlState = false);
350 NFCSTATUS phNxpNciHal_nfcc_core_reset_init(bool keep_config = false);
351 int phNxpNciHal_fw_mw_ver_check();
352 NFCSTATUS phNxpNciHal_check_clock_config(void);
353 NFCSTATUS phNxpNciHal_china_tianjin_rf_setting(void);
354 NFCSTATUS phNxpNciHal_CheckValidFwVersion(void);
355 
356 NFCSTATUS phNxpNciHal_send_nfcee_pwr_cntl_cmd(uint8_t type);
357 NFCSTATUS phNxpNciHal_nfccClockCfgApply(void);
358 /*******************************************************************************
359 **
360 ** Function         phNxpNciHal_configFeatureList
361 **
362 ** Description      Configures the featureList based on chip type
363 **                  HW Version information number will provide chipType.
364 **                  HW Version can be obtained from CORE_INIT_RESPONSE(NCI 1.0)
365 **                  or CORE_RST_NTF(NCI 2.0)
366 **
367 ** Parameters       CORE_INIT_RESPONSE/CORE_RST_NTF, len
368 **
369 ** Returns          none
370 *******************************************************************************/
371 void phNxpNciHal_configFeatureList(uint8_t* init_rsp, uint16_t rsp_len);
372 
373 /******************************************************************************
374  * Function         phNxpNciHal_read_and_update_se_state
375  *
376  * Description      This will read NFCEE status from system properties
377  *                  and update to NFCC to enable/disable.
378  *
379  * Returns          none
380  *
381  ******************************************************************************/
382 void phNxpNciHal_read_and_update_se_state();
383 
384 /******************************************************************************
385  * Function         phNxpNciHal_Abort
386  *
387  * Description      This will post the message to the upper layer
388  *                  using the callback p_nfc_stack_cback_backup.
389  *
390  * Returns          none
391  *
392  ******************************************************************************/
393 extern bool phNxpNciHal_Abort();
394 /******************************************************************************
395  * Function         phNxpNciHal_read_fw_dw_status
396  *
397  * Description      This will read the value of fw download status flag
398  *                  from eeprom
399  *
400  * Parameters       value - this parameter will be updated with the flag
401  *                  value from eeprom.
402  *
403  * Returns          status of the read
404  *
405  ******************************************************************************/
406 NFCSTATUS phNxpNciHal_read_fw_dw_status(uint8_t& value);
407 
408 /******************************************************************************
409  * Function         phNxpNciHal_write_fw_dw_status
410  *
411  * Description      This will update value of fw download status flag
412  *                  to eeprom
413  *
414  * Parameters       value - this value will be updated to eeprom flag.
415  *
416  * Returns          status of the write
417  *
418  ******************************************************************************/
419 NFCSTATUS phNxpNciHal_write_fw_dw_status(uint8_t value);
420 
421 /******************************************************************************
422  * Function         phNxpNciHal_save_uicc_params
423  *
424  * Description      This will read the UICC HCI param values
425  *                  from eeprom and store in global variable
426  *
427  * Returns          status of the read
428  *
429  ******************************************************************************/
430 NFCSTATUS phNxpNciHal_save_uicc_params();
431 
432 /******************************************************************************
433  * Function         phNxpNciHal_restore_uicc_params
434  *
435  * Description      This will set the UICC HCI param values
436  *                  back to eeprom from global variable
437  *
438  * Returns          status of the read
439  *
440  ******************************************************************************/
441 NFCSTATUS phNxpNciHal_restore_uicc_params();
442 
443 /******************************************************************************
444  * Function         phNxpNciHal_client_data_callback
445  *
446  * Description      This will process the data and sends message to lib-nfc
447  *                  client via callback
448  *
449  * Returns          void
450  *
451  ******************************************************************************/
452 void phNxpNciHal_client_data_callback();
453 
454 /******************************************************************************
455  * Function         phNxpNciHal_UpdateRfMiscSettings
456  *
457  * Description      This will look the configuration properties and
458  *                  update the RF misc settings
459  *
460  * Returns          bool - true if the RF Misc settings update required
461  *                      otherwise false
462  *
463  ******************************************************************************/
464 bool phNxpNciHal_UpdateRfMiscSettings();
465 
466 /******************************************************************************
467  * Function         phNxpNciHal_notifyPollingFrame
468  *
469  * Description      Send polling info notification to send to upper layer
470  *
471  * Parameters       p_data - Polling loop info notification
472  *
473  * Returns          void
474  *
475  ******************************************************************************/
476 void phNxpNciHal_notifyPollingFrame(uint16_t data_len, uint8_t* p_data);
477 
478 #endif /* _PHNXPNCIHAL_H_ */
479