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