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