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