1 /*
2  * Copyright 2010-2023 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 
17 /*
18  * Internal Primitives (Functions + Variables) used for Firmware Download
19  */
20 #ifndef PHDNLDNFC_INTERNAL_H
21 #define PHDNLDNFC_INTERNAL_H
22 
23 #include <phDnldNfc.h>
24 #include <phDnldNfc_Cmd.h>
25 #include <phDnldNfc_Status.h>
26 
27 /* DL Host Short Frame Buffer Size for pipelined WRITE RSP */
28 #define PHDNLDNFC_WRITERSP_BUFF_SIZE (0x08U)
29 
30 /* DL Host Frame Buffer Header Length */
31 #define PHDNLDNFC_FRAME_HDR_LEN (0x02U)
32 /* DL Host Frame Buffer CRC Length */
33 #define PHDNLDNFC_FRAME_CRC_LEN (PHDNLDNFC_FRAME_HDR_LEN)
34 #define PHDNLDNFC_FRAME_ID_LEN (0x01U) /* Length of Cmd Id */
35 
36 /* size of EEPROM/Flash address */
37 #define PHDNLDNFC_EEFL_ADDR_SIZE (0x03U)
38 /* 2 Byte size of data */
39 #define PHDNLDNFC_DATA_SIZE (PHDNLDNFC_FRAME_HDR_LEN)
40 
41 /* Start of EEPROM address for log */
42 #define PHDNLDNFC_EEPROM_LOG_START_ADDR (0x201F80U)
43 /* End of EEPROM address for log */
44 #define PHDNLDNFC_EEPROM_LOG_END_ADDR (0x201FBFU)
45 
46 #define PHDNLDNFC_MAX_LOG_SIZE \
47   ((PHDNLDNFC_EEPROM_LOG_END_ADDR - PHDNLDNFC_EEPROM_LOG_START_ADDR) + 1)
48 
49 /*
50  * Enum definition contains Download Event Types
51  */
52 typedef enum phDnldNfc_Event {
53   phDnldNfc_EventInvalid = 0x00, /*Invalid Event Value*/
54   phDnldNfc_EventReset,          /* Reset event */
55   phDnldNfc_EventGetVer,         /* Get Version event*/
56   phDnldNfc_EventWrite,          /* Write event*/
57   phDnldNfc_EventRead,           /* Read event*/
58   phDnldNfc_EventIntegChk,       /* Integrity Check event*/
59   phDnldNfc_EventGetSesnSt,      /* Get Session State event*/
60   phDnldNfc_EventLog,            /* Log event*/
61   phDnldNfc_EventForce,          /* Force event*/
62   phDnldNfc_EventRaw, /* Raw Req/Rsp event,used currently for sending NCI RESET
63                          cmd */
64   phDnldNfc_EVENT_INT_MAX /* Max Event Count*/
65 } phDnldNfc_Event_t;
66 
67 /*
68  * Enum definition contains Download Handler states for each event requested
69  */
70 typedef enum phDnldNfc_State {
71   phDnldNfc_StateInit = 0x00, /* Handler init state */
72   phDnldNfc_StateSend,        /* Send frame to NFCC state */
73   phDnldNfc_StateRecv,        /* Recv Send complete State */
74   phDnldNfc_StateTimer, /* State to stop prev set timer on Recv or handle timed
75                            out scenario */
76   phDnldNfc_StateResponse,  /* Process response from NFCC state */
77   phDnldNfc_StatePipelined, /* Write requests to be pipelined state */
78   phDnldNfc_StateInvalid    /* Invalid Handler state */
79 } phDnldNfc_State_t;
80 
81 /*
82  * Enum definition contains Download Handler Transition
83  */
84 typedef enum phDnldNfc_Transition {
85   phDnldNfc_TransitionIdle =
86       0x00, /* Handler in Idle state - No Download in progress */
87   phDnldNfc_TransitionBusy,   /* Handler is busy processing download request */
88   phDnldNfc_TransitionInvalid /* Invalid Handler Transition */
89 } phDnldNfc_Transition_t;
90 
91 /*
92  * Enum definition contains the Frame input type for CmdId in process
93  */
94 typedef enum {
95   phDnldNfc_FTNone = 0, /* input type None */
96   phDnldNfc_ChkIntg, /* user eeprom offset & len to be added for Check Integrity
97                         Request */
98   phDnldNfc_FTWrite, /* Frame inputs for Write request */
99   phDnldNfc_FTLog,   /* Frame inputs for Log request */
100   phDnldNfc_FTForce, /* Frame input for Force cmd request */
101   phDnldNfc_FTRead,  /* Addr input required for read request */
102   phDnldNfc_FTRaw    /* Raw Req/Rsp type */
103 } phDnldNfc_FrameInputType_t;
104 
105 /*
106  * Enum definition contains Firmware file format
107  */
108 typedef enum phDnldNfc_FwFormat {
109   FW_FORMAT_UNKNOWN = 0x00,
110   FW_FORMAT_SO = 0x01,
111   FW_FORMAT_BIN = 0x02,
112   FW_FORMAT_ARRAY = 0x03,
113 } phDnldNfc_FwFormat_t;
114 
115 /*
116  * Contains Host Frame Buffer information.
117  */
118 typedef struct phDnldNfc_FrameInfo {
119   uint16_t dwSendlength; /* length of the payload  */
120   uint8_t* aFrameBuff;   /* Buffer to store command/frame to be sent */
121 } phDnldNfc_FrameInfo_t,
122     *pphDnldNfc_FrameInfo_t; /* pointer to #phDnldNfc_FrameInfo_t */
123 
124 /*
125  * Frame Input Type & Value for CmdId in Process
126  */
127 typedef struct phDnldNfc_FrameInput {
128   phDnldNfc_FrameInputType_t
129       Type;        /* Type of frame input required for current cmd in process */
130   uint32_t dwAddr; /* Address value required for Read/Write Cmd*/
131 } phDnldNfc_FrameInput_t,
132     *pphDnldNfc_FrameInput_t; /* pointer to #phDnldNfc_FrameInput_t */
133 
134 /*
135  * Context for the response timeout
136  */
137 typedef struct phDnldNfc_RspTimerInfo {
138   uint32_t dwRspTimerId;     /* Timer for Core to handle response */
139   uint8_t TimerStatus;       /* 0 = Timer not running 1 = timer running*/
140   NFCSTATUS wTimerExpStatus; /* Holds the status code on timer expiry */
141   uint16_t rspTimeout;       /*FW download rsp timeout value*/
142 } phDnldNfc_RspTimerInfo_t;
143 
144 /*
145  * Read/Write Processing Info
146  */
147 typedef struct phDnldNfc_RWInfo {
148   uint32_t dwAddr;    /* current Addr updated for read/write */
149   uint32_t wOffset;   /* current offset within the user buffer to read/write */
150   uint32_t wRemBytes; /* Remaining bytes to read/write */
151   uint16_t wRemChunkBytes; /* Remaining bytes within the chunked frame */
152   uint16_t wRWPldSize;     /* Size of the read/write payload per transaction */
153   uint16_t wBytesToSendRecv; /* Num of Bytes being written/read currently */
154   uint16_t wBytesRead;       /* Bytes read from read cmd currently */
155   bool_t bFramesSegmented;   /* Flag to indicate if Read/Write frames are
156                                 segmented */
157   bool_t bFirstWrReq; /* Flag to indicate if this is the first write frame being
158                          sent */
159   bool_t
160       bFirstChunkResp; /* Flag to indicate if we got the first chunk response */
161 } phDnldNfc_RWInfo_t, *pphDnldNfc_RWInfo_t; /* pointer to #phDnldNfc_RWInfo_t */
162 
163 /*
164  * Download context structure
165  */
166 typedef struct phDnldNfc_DlContext {
167   const uint8_t* nxp_nfc_fw; /* Pointer to firmware version from image */
168   const uint8_t*
169       nxp_nfc_fwp; /* Pointer to firmware version from get_version cmd */
170   uint32_t nxp_nfc_fwp_len; /* Length of firmware image length */
171   uint32_t nxp_nfc_fw_len;  /* Firmware image length */
172   uint16_t nxp_i2c_fragment_len;
173   bool_t bResendLastFrame; /* Flag to resend the last write frame after MEM_BSY
174                               status */
175   phDnldNfc_Transition_t
176       tDnldInProgress; /* Flag to indicate if download request is ongoing */
177   phDnldNfc_Event_t tCurrEvent; /* Current event being processed */
178   phDnldNfc_State_t tCurrState; /* Current state being processed */
179   pphDnldNfc_RspCb_t UserCb;    /* Upper layer call back function */
180   void* UserCtxt;               /* Pointer to upper layer context */
181   phDnldNfc_Buff_t tUserData;   /* Data buffer provided by caller */
182   phDnldNfc_Buff_t
183       tRspBuffInfo; /* Buffer to store payload field of the received response*/
184   phDnldNfc_FrameInfo_t tCmdRspFrameInfo; /* Buffer to hold the cmd/resp frame
185                                              except pipeline write */
186   phDnldNfc_FrameInfo_t
187       tPipeLineWrFrameInfo; /* Buffer to hold the pipelined write frame */
188   NFCSTATUS
189   wCmdSendStatus; /* Holds the status of cmd request made to cmd handler */
190   phDnldNfc_CmdId_t tCmdId; /* Cmd Id of the currently processed cmd */
191   phDnldNfc_FrameInput_t
192       FrameInp; /* input value required for current cmd in process */
193   phDnldNfc_RspTimerInfo_t
194       TimerInfo;              /* Timer context handled into download context*/
195   phDnldNfc_Buff_t tTKey;     /* Default Transport Key provided by caller */
196   phDnldNfc_RWInfo_t tRWInfo; /* Read/Write segmented frame info */
197   phDnldNfc_Status_t tLastStatus; /* saved status to distinguish signature or
198                                      pltform recovery */
199   phDnldNfc_FwFormat_t FwFormat;  /*FW file format either lib or bin*/
200 } phDnldNfc_DlContext_t,
201     *pphDnldNfc_DlContext_t; /* pointer to #phDnldNfc_DlContext_t structure */
202 
203 /* The phDnldNfc_CmdHandler function declaration */
204 extern NFCSTATUS phDnldNfc_CmdHandler(void* pContext,
205                                       phDnldNfc_Event_t TrigEvent);
206 
207 #endif /* PHDNLDNFC_INTERNAL_H */
208