1 /*
2  * Copyright 2024 NXP
3  *
4  * All rights reserved.
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #include "fsl_sfdp_parser.h"
9 
10 /*******************************************************************************
11  * Definitions
12  ******************************************************************************/
13 const uint8_t g_sfdpXspiProfile1OpiCmdShift[SFDP_XSPI_PROFILE_OPI_CMD_COUNT] = {31U, 28U, 26U, 20U, 25U, 0xFFU, 0xFFU};
14 const uint8_t g_sfdpXspiProfile2OpiCmdShift[SFDP_XSPI_PROFILE_OPI_CMD_COUNT] = {0xFFU, 13U, 12U,  0xFFU,
15                                                                                 17U,   19U, 0xFFU};
16 /*******************************************************************************
17  * Prototypes
18  ******************************************************************************/
19 
20 /*******************************************************************************
21  * Variables
22  ******************************************************************************/
23 typedef struct _sfdp_func_table_info
24 {
25     uint32_t indexOfArray : 7U;
26     uint32_t supported : 1U;
27     uint32_t paramTableRevision : 16U;
28     uint32_t reserved : 8U;
29 } sfdp_func_table_info_t;
30 
31 struct
32 {
33     sfdp_func_table_info_t sectorMapTable;
34     sfdp_func_table_info_t rpmcTable;
35     sfdp_func_table_info_t fourByteAddrInstTable;
36     sfdp_func_table_info_t xSPIProfile1Table;
37     sfdp_func_table_info_t xSPIProfile2Table;
38     sfdp_func_table_info_t SCCRegMapTable;
39     sfdp_func_table_info_t SCCRegMapxSPIProfile2Table;
40     sfdp_func_table_info_t SCCRegMapMultiChipTable;
41     sfdp_func_table_info_t cmdSeq2OctalDDR;
42     sfdp_func_table_info_t x4QualIOWithDS;
43     sfdp_func_table_info_t cmdSeq2QuadDDR;
44     sfdp_func_table_info_t securePacketRWTable;
45 } g_sfdpFuncTables;
46 
47 /*******************************************************************************
48  * Code
49  ******************************************************************************/
50 
51 /************************** Static Functions Start ****************************/
52 
SFDP_GetParamId(sfdp_param_header_t * ptrParamHeader)53 static inline uint16_t SFDP_GetParamId(sfdp_param_header_t *ptrParamHeader)
54 {
55     return (ptrParamHeader->idMsb << 8U) | (ptrParamHeader->idLsb);
56 }
57 
SFDP_GetParamRevision(sfdp_param_header_t * ptrParamHeader)58 static inline uint16_t SFDP_GetParamRevision(sfdp_param_header_t *ptrParamHeader)
59 {
60     return (ptrParamHeader->majorRev << 8U) | (ptrParamHeader->minorRev);
61 }
62 
SFDP_InterpretSFDPParamHeaders(sfdp_param_header_t * ptrSfdpParamHeaders,sfdp_handle_t * handle)63 static void SFDP_InterpretSFDPParamHeaders(sfdp_param_header_t *ptrSfdpParamHeaders, sfdp_handle_t *handle)
64 {
65     uint8_t paramTableCount                = handle->nph + 1U;
66     sfdp_param_header_t *ptrCurParamHeader = NULL;
67     SFDP_SpiReadFunc spiReadTable          = handle->spiRead;
68 
69     (void)memset(&g_sfdpFuncTables, 0UL, sizeof(g_sfdpFuncTables));
70 
71     for (uint8_t i = 0U; i < paramTableCount; i++)
72     {
73         ptrCurParamHeader = &(ptrSfdpParamHeaders[i]);
74         switch (SFDP_GetParamId(ptrCurParamHeader))
75         {
76             case 0xFF00U:
77             {
78                 (void)spiReadTable(ptrCurParamHeader->ptp, (uint32_t *)((uint32_t)(&(handle->bfp))),
79                                    (4UL * ptrCurParamHeader->len));
80                 break;
81             }
82             case 0xFF81U:
83             {
84                 g_sfdpFuncTables.sectorMapTable.supported          = 1U;
85                 g_sfdpFuncTables.sectorMapTable.indexOfArray       = i;
86                 g_sfdpFuncTables.sectorMapTable.paramTableRevision = SFDP_GetParamRevision(&(ptrSfdpParamHeaders[i]));
87                 (void)spiReadTable(ptrCurParamHeader->ptp, (uint32_t *)((uint32_t)(&(handle->sectorMapTable))),
88                                    (4UL * ptrCurParamHeader->len));
89                 break;
90             }
91             case 0xFF03U:
92             {
93                 g_sfdpFuncTables.rpmcTable.supported    = 1U;
94                 g_sfdpFuncTables.rpmcTable.indexOfArray = i;
95                 g_sfdpFuncTables.fourByteAddrInstTable.paramTableRevision =
96                     SFDP_GetParamRevision(&(ptrSfdpParamHeaders[i]));
97                 (void)spiReadTable(ptrCurParamHeader->ptp, (uint32_t *)((uint32_t)(&(handle->rpmcTable))),
98                                    (4UL * ptrCurParamHeader->len));
99                 break;
100             }
101             case 0xFF84U:
102             {
103                 g_sfdpFuncTables.fourByteAddrInstTable.supported    = 1U;
104                 g_sfdpFuncTables.fourByteAddrInstTable.indexOfArray = i;
105                 g_sfdpFuncTables.fourByteAddrInstTable.paramTableRevision =
106                     SFDP_GetParamRevision(&(ptrSfdpParamHeaders[i]));
107                 (void)spiReadTable(ptrCurParamHeader->ptp, (uint32_t *)((uint32_t)(&(handle->fourByteAddrInstTable))),
108                                    (4UL * ptrCurParamHeader->len));
109                 break;
110             }
111             case 0xFF05U:
112             {
113                 g_sfdpFuncTables.xSPIProfile1Table.supported    = 1U;
114                 g_sfdpFuncTables.xSPIProfile1Table.indexOfArray = i;
115                 g_sfdpFuncTables.xSPIProfile1Table.paramTableRevision =
116                     SFDP_GetParamRevision(&(ptrSfdpParamHeaders[i]));
117                 (void)spiReadTable(ptrCurParamHeader->ptp, (uint32_t *)((uint32_t)(&(handle->xSPIProfile1Table))),
118                                    (4UL * ptrCurParamHeader->len));
119                 break;
120             }
121             case 0xFF06U:
122             {
123                 g_sfdpFuncTables.xSPIProfile2Table.supported    = 1U;
124                 g_sfdpFuncTables.xSPIProfile2Table.indexOfArray = i;
125                 g_sfdpFuncTables.xSPIProfile2Table.paramTableRevision =
126                     SFDP_GetParamRevision(&(ptrSfdpParamHeaders[i]));
127                 (void)spiReadTable(ptrCurParamHeader->ptp, (uint32_t *)((uint32_t)(&(handle->xSPIProfile2Table))),
128                                    (4UL * ptrCurParamHeader->len));
129                 break;
130             }
131             case 0xFF87U:
132             {
133                 g_sfdpFuncTables.SCCRegMapTable.supported          = 1U;
134                 g_sfdpFuncTables.SCCRegMapTable.indexOfArray       = i;
135                 g_sfdpFuncTables.SCCRegMapTable.paramTableRevision = SFDP_GetParamRevision(&(ptrSfdpParamHeaders[i]));
136                 (void)spiReadTable(ptrCurParamHeader->ptp, (uint32_t *)((uint32_t)(&(handle->SCCRegMapTable))),
137                                    (4UL * ptrCurParamHeader->len));
138                 break;
139             }
140             case 0xFF88U:
141             {
142                 g_sfdpFuncTables.SCCRegMapMultiChipTable.supported    = 1U;
143                 g_sfdpFuncTables.SCCRegMapMultiChipTable.indexOfArray = i;
144                 g_sfdpFuncTables.SCCRegMapMultiChipTable.paramTableRevision =
145                     SFDP_GetParamRevision(&(ptrSfdpParamHeaders[i]));
146                 (void)spiReadTable(ptrCurParamHeader->ptp, (uint32_t *)((uint32_t)(&(handle->SCCRegMapMultiChipTable))),
147                                    (4UL * ptrCurParamHeader->len));
148                 break;
149             }
150             case 0xFF09U:
151             {
152                 g_sfdpFuncTables.SCCRegMapxSPIProfile2Table.supported    = 1U;
153                 g_sfdpFuncTables.SCCRegMapxSPIProfile2Table.indexOfArray = i;
154                 g_sfdpFuncTables.SCCRegMapxSPIProfile2Table.paramTableRevision =
155                     SFDP_GetParamRevision(&(ptrSfdpParamHeaders[i]));
156                 (void)spiReadTable(ptrCurParamHeader->ptp,
157                                    (uint32_t *)((uint32_t)(&(handle->SCCRegMapxSPIProfile2Table))),
158                                    (4UL * ptrCurParamHeader->len));
159                 break;
160             }
161             case 0xFF0AU:
162             {
163                 g_sfdpFuncTables.cmdSeq2OctalDDR.supported          = 1U;
164                 g_sfdpFuncTables.cmdSeq2OctalDDR.indexOfArray       = i;
165                 g_sfdpFuncTables.cmdSeq2OctalDDR.paramTableRevision = SFDP_GetParamRevision(&(ptrSfdpParamHeaders[i]));
166                 (void)spiReadTable(ptrCurParamHeader->ptp, (uint32_t *)((uint32_t)(&(handle->cmdSeq2OctalDDR))),
167                                    (4UL * ptrCurParamHeader->len));
168                 break;
169             }
170             case 0xFF0C:
171             {
172                 g_sfdpFuncTables.x4QualIOWithDS.supported          = 1U;
173                 g_sfdpFuncTables.x4QualIOWithDS.indexOfArray       = i;
174                 g_sfdpFuncTables.x4QualIOWithDS.paramTableRevision = SFDP_GetParamRevision(&(ptrSfdpParamHeaders[i]));
175                 (void)spiReadTable(ptrCurParamHeader->ptp, (uint32_t *)((uint32_t)(&(handle->x4QualIOWithDS))),
176                                    (4UL * ptrCurParamHeader->len));
177                 break;
178             }
179             case 0xFF8D:
180             {
181                 g_sfdpFuncTables.cmdSeq2QuadDDR.supported          = 1U;
182                 g_sfdpFuncTables.cmdSeq2QuadDDR.indexOfArray       = i;
183                 g_sfdpFuncTables.cmdSeq2QuadDDR.paramTableRevision = SFDP_GetParamRevision(&(ptrSfdpParamHeaders[i]));
184                 (void)spiReadTable(ptrCurParamHeader->ptp, (uint32_t *)((uint32_t)(&(handle->cmdSeq2QuadDDR))),
185                                    (4UL * ptrCurParamHeader->len));
186                 break;
187             }
188             case 0xFF8E:
189             {
190                 g_sfdpFuncTables.securePacketRWTable.supported    = 1U;
191                 g_sfdpFuncTables.securePacketRWTable.indexOfArray = i;
192                 g_sfdpFuncTables.securePacketRWTable.paramTableRevision =
193                     SFDP_GetParamRevision(&(ptrSfdpParamHeaders[i]));
194                 (void)spiReadTable(ptrCurParamHeader->ptp, (uint32_t *)((uint32_t)(&(handle->securePacketRWTable))),
195                                    (4UL * ptrCurParamHeader->len));
196                 break;
197             }
198             default:
199             {
200                 break;
201             }
202         }
203     }
204 }
205 
SFDP_CheckSignature(sfdp_header_t * ptrSfdpHeader)206 static inline bool SFDP_CheckSignature(sfdp_header_t *ptrSfdpHeader)
207 {
208     return (bool)(ptrSfdpHeader->signature == 0x50444653UL);
209 }
210 
SFDP_Get1PadReadCmd(sfdp_handle_t * handle,sfdp_protocol_type_t protocolType,sfdp_read_cmd_info_t * ptr1PadReadCmdInfo)211 static void SFDP_Get1PadReadCmd(sfdp_handle_t *handle,
212                                 sfdp_protocol_type_t protocolType,
213                                 sfdp_read_cmd_info_t *ptr1PadReadCmdInfo)
214 {
215     if (protocolType == kSFDP_Protocol_1s1s1s)
216     {
217         ptr1PadReadCmdInfo->dummyCycle  = 8U;
218         ptr1PadReadCmdInfo->instruction = 0x0BU;
219         if ((handle->addrMode != kSFDP_AddrMode_3ByteOnly) && (g_sfdpFuncTables.fourByteAddrInstTable.supported != 0U))
220         {
221             /* In case of 4-byte instruction supported. */
222             if ((handle->fourByteAddrInstTable.dw1 & 0x2UL) != 0UL)
223             {
224                 ptr1PadReadCmdInfo->instruction = 0x0CU;
225             }
226         }
227     }
228     else
229     {
230         ptr1PadReadCmdInfo->dummyCycle  = handle->bfp.dw22 & 0x1FUL;
231         ptr1PadReadCmdInfo->instruction = (handle->bfp.dw22 & 0xFF00UL) >> 8UL;
232         if ((handle->addrMode != kSFDP_AddrMode_3ByteOnly) && (g_sfdpFuncTables.fourByteAddrInstTable.supported != 0U))
233         {
234             /* In case of 4-byte instruction supported. */
235             if ((handle->fourByteAddrInstTable.dw1 & 0x2000UL) != 0UL)
236             {
237                 ptr1PadReadCmdInfo->instruction = 0x0EU;
238             }
239         }
240         ptr1PadReadCmdInfo->modeClocks = (handle->bfp.dw22 & 0xE0UL) >> 5UL;
241     }
242 }
243 
SFDP_Get2PadReadCmd(sfdp_handle_t * handle,sfdp_protocol_type_t protocolType,sfdp_read_cmd_info_t * ptr2PadReadCmdInfo)244 static void SFDP_Get2PadReadCmd(sfdp_handle_t *handle,
245                                 sfdp_protocol_type_t protocolType,
246                                 sfdp_read_cmd_info_t *ptr2PadReadCmdInfo)
247 {
248     if (protocolType == kSFDP_Protocol_1s1s2s)
249     {
250         ptr2PadReadCmdInfo->dummyCycle  = (uint8_t)(handle->bfp.dw4 & 0x1FUL);
251         ptr2PadReadCmdInfo->modeClocks  = (uint8_t)((handle->bfp.dw4 & 0xE0UL) >> 5UL);
252         ptr2PadReadCmdInfo->instruction = (uint8_t)((handle->bfp.dw4 & 0xFF00UL) >> 8UL);
253     }
254     else if (protocolType == kSFDP_Protocol_1s2s2s)
255     {
256         ptr2PadReadCmdInfo->dummyCycle  = (uint8_t)((handle->bfp.dw4 & 0x1F0000UL) >> 16UL);
257         ptr2PadReadCmdInfo->modeClocks  = (uint8_t)((handle->bfp.dw4 & 0xE00000UL) >> 21UL);
258         ptr2PadReadCmdInfo->instruction = (uint8_t)((handle->bfp.dw4 & 0xFF000000UL) >> 24UL);
259     }
260     else if (protocolType == kSFDP_Protocol_1s2d2d)
261     {
262         ptr2PadReadCmdInfo->dummyCycle  = (uint8_t)((handle->bfp.dw22 & 0x1F0000UL) >> 16UL);
263         ptr2PadReadCmdInfo->modeClocks  = (uint8_t)((handle->bfp.dw22 & 0xE00000UL) >> 21UL);
264         ptr2PadReadCmdInfo->instruction = (uint8_t)((handle->bfp.dw22 & 0xFF000000UL) >> 24UL);
265         if ((handle->addrMode != kSFDP_AddrMode_3ByteOnly) && (g_sfdpFuncTables.fourByteAddrInstTable.supported != 0U))
266         {
267             /* In case of 4 byte address mode. */
268             if ((handle->fourByteAddrInstTable.dw1 & 0x4000UL) != 0UL)
269             {
270                 ptr2PadReadCmdInfo->instruction = 0xBEU;
271             }
272         }
273     }
274     else if (protocolType == kSFDP_Protocol_2s2s2s)
275     {
276         ptr2PadReadCmdInfo->dummyCycle  = (uint8_t)((handle->bfp.dw6 & 0x1F0000UL) >> 16UL);
277         ptr2PadReadCmdInfo->modeClocks  = (uint8_t)((handle->bfp.dw6 & 0xE00000UL) >> 21UL);
278         ptr2PadReadCmdInfo->instruction = (uint8_t)((handle->bfp.dw6 & 0xFF000000UL) >> 24UL);
279     }
280     else
281     {
282     }
283 }
284 
SFDP_Get4PadReadCmd(sfdp_handle_t * handle,sfdp_protocol_type_t protocolType,uint32_t clkFreq,sfdp_read_cmd_info_t * ptr4PadReadCmdInfo)285 static void SFDP_Get4PadReadCmd(sfdp_handle_t *handle,
286                                 sfdp_protocol_type_t protocolType,
287                                 uint32_t clkFreq,
288                                 sfdp_read_cmd_info_t *ptr4PadReadCmdInfo)
289 {
290     if (protocolType == kSFDP_Protocol_1s1s4s)
291     {
292         ptr4PadReadCmdInfo->dummyCycle  = (uint8_t)((handle->bfp.dw3 & 0x1F0000UL) >> 16UL);
293         ptr4PadReadCmdInfo->modeClocks  = (uint8_t)((handle->bfp.dw3 & 0xE00000UL) >> 21UL);
294         ptr4PadReadCmdInfo->instruction = (uint8_t)((handle->bfp.dw3 & 0xFF000000UL) >> 24UL);
295 
296         if ((handle->addrMode != kSFDP_AddrMode_3ByteOnly) && (g_sfdpFuncTables.fourByteAddrInstTable.supported != 0U))
297         {
298             if ((handle->fourByteAddrInstTable.dw1 & 0x80UL) != 0UL)
299             {
300                 /* In case of 4byte address mode support for 1s-1s-4s fast read command. */
301                 ptr4PadReadCmdInfo->instruction = 0x6CU;
302             }
303         }
304     }
305     else if (protocolType == kSFDP_Protocol_1s4s4s)
306     {
307         ptr4PadReadCmdInfo->dummyCycle  = (uint8_t)(handle->bfp.dw3 & 0x1FUL);
308         ptr4PadReadCmdInfo->modeClocks  = (uint8_t)((handle->bfp.dw3 & 0xE0UL) >> 5UL);
309         ptr4PadReadCmdInfo->instruction = (uint8_t)((handle->bfp.dw3 & 0xFF00UL) >> 8UL);
310 
311         if ((handle->addrMode != kSFDP_AddrMode_3ByteOnly) && (g_sfdpFuncTables.fourByteAddrInstTable.supported != 0U))
312         {
313             if ((handle->fourByteAddrInstTable.dw1 & 0x100UL) != 0UL)
314             {
315                 /* In case of 4byte address mode support for 1s-4s-4s fast read command. */
316                 ptr4PadReadCmdInfo->instruction = 0xECU;
317             }
318         }
319     }
320     else if (protocolType == kSFDP_Protocol_1s4d4d)
321     {
322         ptr4PadReadCmdInfo->dummyCycle  = handle->bfp.dw23 & 0x1FUL;
323         ptr4PadReadCmdInfo->modeClocks  = (handle->bfp.dw23 & 0xE0UL) >> 5UL;
324         ptr4PadReadCmdInfo->instruction = (handle->bfp.dw23 & 0xFF00UL) >> 8UL;
325         if ((handle->addrMode != kSFDP_AddrMode_3ByteOnly) && (g_sfdpFuncTables.fourByteAddrInstTable.supported != 0U))
326         {
327             if ((handle->fourByteAddrInstTable.dw1 & 0x8000UL) != 0UL)
328             {
329                 ptr4PadReadCmdInfo->instruction = 0xEEU;
330             }
331         }
332     }
333     else if (protocolType == kSFDP_Protocol_4s4s4s)
334     {
335         ptr4PadReadCmdInfo->dummyCycle  = (uint8_t)((handle->bfp.dw7 & 0x1F0000UL) >> 16UL);
336         ptr4PadReadCmdInfo->modeClocks  = (uint8_t)((handle->bfp.dw7 & 0xE00000UL) >> 21UL);
337         ptr4PadReadCmdInfo->instruction = (uint8_t)((handle->bfp.dw7 & 0xFF000000UL) >> 24UL);
338     }
339     else if (protocolType == kSFDP_Protocol_4s4d4d)
340     {
341         ptr4PadReadCmdInfo->dummyCycle  = (uint8_t)((handle->bfp.dw23 & 0x1F0000UL) >> 16UL);
342         ptr4PadReadCmdInfo->modeClocks  = (uint8_t)((handle->bfp.dw23 & 0xE00000UL) >> 21UL);
343         ptr4PadReadCmdInfo->instruction = (uint8_t)((handle->bfp.dw23 & 0xFF000000UL) >> 24UL);
344         if ((g_sfdpFuncTables.x4QualIOWithDS.supported != 0U) && SFDP_CheckDSSupportedForQpiDtrMode(handle))
345         {
346             if (clkFreq == 100000000UL)
347             {
348                 ptr4PadReadCmdInfo->dummyCycle = (uint8_t)((handle->x4QualIOWithDS.dw5 & 0xF80UL) >> 7UL);
349             }
350             else if (clkFreq == 133000000UL)
351             {
352                 ptr4PadReadCmdInfo->dummyCycle = (uint8_t)((handle->x4QualIOWithDS.dw5 & 0x3E0000UL) >> 17UL);
353             }
354             else if (clkFreq == 166000000UL)
355             {
356                 ptr4PadReadCmdInfo->dummyCycle = (uint8_t)((handle->x4QualIOWithDS.dw5 & 0xF8000000UL) >> 27UL);
357             }
358             else if (clkFreq == 200000000UL)
359             {
360                 ptr4PadReadCmdInfo->dummyCycle = (uint8_t)((handle->x4QualIOWithDS.dw4 & 0xF80UL) >> 7UL);
361             }
362             else
363             {
364             }
365         }
366     }
367     else
368     {
369     }
370 }
371 
SFDP_Get8PadReadCmd(sfdp_handle_t * handle,sfdp_protocol_type_t protocolType,uint32_t clkFreq,sfdp_read_cmd_info_t * ptr8PadReadCmdInfo)372 static void SFDP_Get8PadReadCmd(sfdp_handle_t *handle,
373                                 sfdp_protocol_type_t protocolType,
374                                 uint32_t clkFreq,
375                                 sfdp_read_cmd_info_t *ptr8PadReadCmdInfo)
376 {
377     if (protocolType == kSFDP_Protocol_1s1s8s)
378     {
379         ptr8PadReadCmdInfo->dummyCycle  = (uint8_t)((handle->bfp.dw17 & 0x1F0000UL) >> 16UL);
380         ptr8PadReadCmdInfo->modeClocks  = (uint8_t)((handle->bfp.dw17 & 0xE00000UL) >> 21UL);
381         ptr8PadReadCmdInfo->instruction = (uint8_t)((handle->bfp.dw17 & 0xFF000000UL) >> 24UL);
382         if ((handle->addrMode != kSFDP_AddrMode_3ByteOnly) && (g_sfdpFuncTables.fourByteAddrInstTable.supported != 0U))
383         {
384             /* In case of 4 byte address mode. */
385             if ((handle->fourByteAddrInstTable.dw1 & 0x800000UL) != 0UL)
386             {
387                 ptr8PadReadCmdInfo->instruction = 0x7CU;
388             }
389         }
390     }
391     else if (protocolType == kSFDP_Protocol_1s8s8s)
392     {
393         ptr8PadReadCmdInfo->dummyCycle  = (uint8_t)(handle->bfp.dw17 & 0x1FUL);
394         ptr8PadReadCmdInfo->modeClocks  = (uint8_t)((handle->bfp.dw17 & 0xE0UL) >> 5UL);
395         ptr8PadReadCmdInfo->instruction = (uint8_t)((handle->bfp.dw17 & 0xFF00UL) >> 8UL);
396         if ((handle->addrMode != kSFDP_AddrMode_3ByteOnly) && (g_sfdpFuncTables.fourByteAddrInstTable.supported != 0U))
397         {
398             /* In case of 4 byte address mode. */
399             if ((handle->fourByteAddrInstTable.dw1 & 0x1000000UL) != 0UL)
400             {
401                 ptr8PadReadCmdInfo->instruction = 0xCCU;
402             }
403         }
404     }
405     else if (protocolType == kSFDP_Protocol_1s8d8d)
406     {
407         if (g_sfdpFuncTables.fourByteAddrInstTable.supported != 0U)
408         {
409             if ((handle->fourByteAddrInstTable.dw1 & 0x400000UL) != 0UL)
410             {
411                 ptr8PadReadCmdInfo->instruction = 0xFDU;
412                 ptr8PadReadCmdInfo->dummyCycle  = 16U;
413                 ptr8PadReadCmdInfo->modeClocks  = 0U;
414             }
415         }
416     }
417     else if (protocolType == kSFDP_Protocol_8s8s8s)
418     {
419         ptr8PadReadCmdInfo->instruction = 0xECU;
420         ptr8PadReadCmdInfo->dummyCycle  = 16U;
421         ptr8PadReadCmdInfo->modeClocks  = 0U;
422 
423         if (g_sfdpFuncTables.xSPIProfile1Table.supported != 0U)
424         {
425             if (clkFreq == 100000000UL)
426             {
427                 ptr8PadReadCmdInfo->dummyCycle = (uint8_t)((handle->xSPIProfile1Table.dw5 & 0xF80UL) >> 7UL);
428             }
429             else if (clkFreq == 133000000UL)
430             {
431                 ptr8PadReadCmdInfo->dummyCycle = (uint8_t)((handle->xSPIProfile1Table.dw5 & 0x3E0000UL) >> 12UL);
432             }
433             else if (clkFreq == 166000000UL)
434             {
435                 ptr8PadReadCmdInfo->dummyCycle = (uint8_t)((handle->xSPIProfile1Table.dw5 & 0xF8000000UL) >> 27UL);
436             }
437             else if (clkFreq == 200000000UL)
438             {
439                 ptr8PadReadCmdInfo->dummyCycle = (uint8_t)((handle->xSPIProfile1Table.dw4 & 0xF80UL) >> 7UL);
440             }
441             else
442             {
443             }
444         }
445         else if (g_sfdpFuncTables.xSPIProfile2Table.supported != 0U)
446         {
447             if (clkFreq == 100000000UL)
448             {
449                 ptr8PadReadCmdInfo->dummyCycle = (uint8_t)((handle->xSPIProfile2Table.dw3 & 0xF80UL) >> 7UL);
450             }
451             else if (clkFreq == 133000000UL)
452             {
453                 ptr8PadReadCmdInfo->dummyCycle = (uint8_t)((handle->xSPIProfile2Table.dw3 & 0x3E0000UL) >> 12UL);
454             }
455             else if (clkFreq == 166000000UL)
456             {
457                 ptr8PadReadCmdInfo->dummyCycle = (uint8_t)((handle->xSPIProfile2Table.dw3 & 0xF8000000UL) >> 27UL);
458             }
459             else if (clkFreq == 200000000UL)
460             {
461                 ptr8PadReadCmdInfo->dummyCycle = (uint8_t)((handle->xSPIProfile2Table.dw2 & 0xF80UL) >> 7UL);
462             }
463             else
464             {
465             }
466         }
467     }
468     else if (protocolType == kSFDP_Protocol_8d8d8d)
469     {
470         ptr8PadReadCmdInfo->instruction = 0xCCU;
471         ptr8PadReadCmdInfo->dummyCycle  = 16U;
472         ptr8PadReadCmdInfo->modeClocks  = 0U;
473         if (g_sfdpFuncTables.xSPIProfile1Table.supported != 0U)
474         {
475             ptr8PadReadCmdInfo->instruction = (uint8_t)((handle->xSPIProfile1Table.dw1 & 0xFF00UL) >> 8UL);
476             if (clkFreq == 100000000UL)
477             {
478                 ptr8PadReadCmdInfo->dummyCycle = (uint8_t)((handle->xSPIProfile1Table.dw5 & 0xF80UL) >> 7UL);
479             }
480             else if (clkFreq == 133000000UL)
481             {
482                 ptr8PadReadCmdInfo->dummyCycle = (uint8_t)((handle->xSPIProfile1Table.dw5 & 0x3E0000UL) >> 12UL);
483             }
484             else if (clkFreq == 166000000UL)
485             {
486                 ptr8PadReadCmdInfo->dummyCycle = (uint8_t)((handle->xSPIProfile1Table.dw5 & 0xF8000000UL) >> 27UL);
487             }
488             else if (clkFreq == 200000000UL)
489             {
490                 ptr8PadReadCmdInfo->dummyCycle = (uint8_t)((handle->xSPIProfile1Table.dw4 & 0xF80UL) >> 7UL);
491             }
492             else
493             {
494             }
495         }
496         else if (g_sfdpFuncTables.xSPIProfile2Table.supported != 0U)
497         {
498             if (clkFreq == 100000000UL)
499             {
500                 ptr8PadReadCmdInfo->dummyCycle = (uint8_t)((handle->xSPIProfile2Table.dw3 & 0xF80UL) >> 7UL);
501             }
502             else if (clkFreq == 133000000UL)
503             {
504                 ptr8PadReadCmdInfo->dummyCycle = (uint8_t)((handle->xSPIProfile2Table.dw3 & 0x3E0000UL) >> 12UL);
505             }
506             else if (clkFreq == 166000000UL)
507             {
508                 ptr8PadReadCmdInfo->dummyCycle = (uint8_t)((handle->xSPIProfile2Table.dw3 & 0xF8000000UL) >> 27UL);
509             }
510             else if (clkFreq == 200000000UL)
511             {
512                 ptr8PadReadCmdInfo->dummyCycle = (uint8_t)((handle->xSPIProfile2Table.dw2 & 0xF80UL) >> 7UL);
513             }
514             else
515             {
516             }
517         }
518     }
519     else
520     {
521     }
522 }
523 
524 /************************** Static Functions End ****************************/
525 
526 /************************ Read SFDP Data Sets Start *************************/
527 
528 /*!
529  * @brief Read SFDP header, and check if SFDP signature is correct.
530  *
531  * @param[in] handle The parameter in type of @ref sfdp_handle_t.
532  * @param[in] spiRead The function to read sfdp header.
533  *
534  * @retval kSFDP_RET_HardwareIssue Fail to read SFDP header due to some hardware issue.
535  * @retval kSFDP_RET_Invalid Fail to read SFDP header due to invalid SFDP signature.
536  * @retval kSFDP_RET_Success Successfully to read sfdp header.
537  */
SFDP_ReadSFDPHeader(sfdp_handle_t * handle,SFDP_SpiReadFunc spiRead)538 sfdp_ret_type_t SFDP_ReadSFDPHeader(sfdp_handle_t *handle, SFDP_SpiReadFunc spiRead)
539 {
540     assert(handle != NULL);
541     assert(spiRead != NULL);
542 
543     status_t status = kStatus_Success;
544 
545     /* Read SFDP header: address is 0, size is 8 bytes. */
546     sfdp_header_t tmpSfdpHeader;
547 
548     status = spiRead(0UL, (uint32_t *)((uint32_t)&tmpSfdpHeader), 8UL);
549 
550     if (status != kStatus_Success)
551     {
552         return kSFDP_RET_HardwareIssue;
553     }
554 
555     if (SFDP_CheckSignature(&tmpSfdpHeader) == false)
556     {
557         return kSFDP_RET_Invalid;
558     }
559 
560     handle->spiRead         = spiRead;
561     handle->curJEDECVersion = (sfdp_revsion_t)SFDP_REVISION(tmpSfdpHeader.majorRev, tmpSfdpHeader.minorRev);
562     handle->nph             = tmpSfdpHeader.nph;
563     handle->access          = tmpSfdpHeader.access;
564     return kSFDP_RET_Success;
565 }
566 
567 /*!
568  * @brief Read SFDP parameter header.
569  *
570  * @param[out] ptrSfdpParamHeader Pointer to the variable in type of @ref sfdp_param_header_t
571                                 to store sfdp parameter header.
572  * @param[in] nph Number of parameter header.
573  * @param[in] spiRead Function to read sfdp parameter header.
574  *
575  * @retval kStatus_Success Successfully to read SFDP parameter header.
576  * @retval kSFDP_RET_HardwareIssue Fail to read SFDP parameter header due to some hardware issues.
577  */
SFDP_ReadSFDPParameterHeader(sfdp_param_header_t * ptrSfdpParamHeader,uint8_t nph,SFDP_SpiReadFunc spiRead)578 sfdp_ret_type_t SFDP_ReadSFDPParameterHeader(sfdp_param_header_t *ptrSfdpParamHeader,
579                                              uint8_t nph,
580                                              SFDP_SpiReadFunc spiRead)
581 {
582     status_t status;
583 
584     status = spiRead(0x8UL, (uint32_t *)((uint32_t)ptrSfdpParamHeader), 8UL * (nph + 1UL));
585 
586     if (status != kStatus_Success)
587     {
588         return kSFDP_RET_HardwareIssue;
589     }
590 
591     return kSFDP_RET_Success;
592 }
593 
594 /*!
595  * @brief Read all current serial flash device supported parameter tables.
596  *
597  * @param[in] handle The parameter in type of @ref sfdp_handle_t.
598  *
599  * @retval kSFDP_RET_HardwareIssue Fail to read SFDP parameter header due to some hardware issues.
600  * @retval kSFDP_RET_Success Successfully to read all supported parameter tables.
601  */
SFDP_ReadAllSupportedTable(sfdp_handle_t * handle)602 sfdp_ret_type_t SFDP_ReadAllSupportedTable(sfdp_handle_t *handle)
603 {
604     sfdp_ret_type_t ret;
605 
606     sfdp_param_header_t *ptrSfdpParamHeader = NULL;
607 
608     ptrSfdpParamHeader = (sfdp_param_header_t *)(uint32_t)malloc((handle->nph + 1U) * 8U);
609     ret                = SFDP_ReadSFDPParameterHeader(ptrSfdpParamHeader, handle->nph, handle->spiRead);
610 
611     if (ret != kSFDP_RET_Success)
612     {
613         free((void *)(uint32_t)ptrSfdpParamHeader);
614         return kSFDP_RET_HardwareIssue;
615     }
616 
617     SFDP_InterpretSFDPParamHeaders(ptrSfdpParamHeader, handle);
618     free((void *)(uint32_t)ptrSfdpParamHeader);
619 
620     handle->addrMode = SFDP_GetAddressBytes(handle);
621 
622     return kSFDP_RET_Success;
623 }
624 /************************ Read SFDP Data Sets End *************************/
625 
626 /******************* Get Flash Basic Attributes Start *********************/
627 /*!
628  * @brief Get Flash Density, the result is in unit of KB.
629 
630  * @param[in] ptrBfp Pointer to the variable in type of @ref sfdp_basic_flash_param_table_t.
631  *
632  * @return In unit of KB, 64 means 64KB(512 Kb)
633  */
SFDP_GetFlashDensity(sfdp_handle_t * handle)634 uint32_t SFDP_GetFlashDensity(sfdp_handle_t *handle)
635 {
636     assert(handle != NULL);
637 
638     uint32_t dw        = handle->bfp.dw2;
639     uint64_t bitsCount = 0ULL;
640 
641     if ((dw & (1UL << 31UL)) == 0UL)
642     {
643         /* For densities 2 gigabits or less, bit 31 is set to 0b. The field 30:0 defines the size in bits.  */
644         bitsCount = (uint64_t)dw + 1ULL;
645     }
646     else
647     {
648         dw &= ((1UL << 31UL) - 1UL);
649         /* For densities 4 gigabits and above, bit 31 is set to 1. The field 30:0 defines "N" when density is computed
650             2^N bits. */
651         bitsCount = (1ULL << dw);
652     }
653 
654     return (uint32_t)(bitsCount >> 13UL);
655 }
656 
657 /*!
658  * @brief Get page size, the result in unit of Byte.
659  *
660  * @param[in] handle The parameter in type of @ref sfdp_handle_t.
661  *
662  * @return The size of page.
663  */
SFDP_GetPageSize(sfdp_handle_t * handle)664 uint32_t SFDP_GetPageSize(sfdp_handle_t *handle)
665 {
666     assert(handle != NULL);
667 
668     uint8_t n = 0U;
669 
670     n = (handle->bfp.dw11 & 0xF0) >> 4U;
671 
672     if (n == 0U)
673     {
674         return 256;
675     }
676     else
677     {
678         return 1UL << (uint32_t)n;
679     }
680 }
681 
682 /*!
683  * @brief Get flash interface mode based on input policy, and number of data pad.
684  *
685  * @param[in] handle The parameter in type of @ref sfdp_handle_t.
686  * @param[in] cmdPolicy The policy used to select interface mode, in type of @ref sfdp_cmd_policy_t.
687  * @param[in] flashPadNum The number of data pad, in type of @ref sfdp_flash_pad_num_t
688  *
689  * @return Calculated protocol type, in type of @ref sfdp_protocol_type_t.
690  */
SFDP_GetFlashProtocolType(sfdp_handle_t * handle,sfdp_cmd_policy_t cmdPolicy,sfdp_flash_pad_num_t flashPadNum)691 sfdp_protocol_type_t SFDP_GetFlashProtocolType(sfdp_handle_t *handle,
692                                                sfdp_cmd_policy_t cmdPolicy,
693                                                sfdp_flash_pad_num_t flashPadNum)
694 {
695     sfdp_protocol_type_t protocolTypeSelected = kSFDP_Protocol_1s1s1s;
696 
697     switch (flashPadNum)
698     {
699         case kSFDP_Flash1Pad:
700         {
701             if (cmdPolicy >= kSFDP_CmdPolicy_LowestThroughput)
702             {
703                 protocolTypeSelected = kSFDP_Protocol_1s1s1s;
704             }
705             else
706             {
707                 if ((handle->bfp.dw21 & 0x1UL) != 0UL)
708                 {
709                     protocolTypeSelected = kSFDP_Protocol_1s1d1d;
710                 }
711             }
712             break;
713         }
714         case kSFDP_Flash2Pad:
715         {
716             if ((cmdPolicy == kSFDP_CmdPolicy_DdrIO) || (cmdPolicy == kSFDP_CmdPolicy_HighestThroughput))
717             {
718                 if ((handle->bfp.dw21 & 0x2UL) != 0UL)
719                 {
720                     protocolTypeSelected = kSFDP_Protocol_1s2d2d;
721                 }
722             }
723             else if (cmdPolicy == kSFDP_CmdPolicy_SdrIO)
724             {
725                 if ((handle->bfp.dw1 & 0x100000UL) != 0UL)
726                 {
727                     protocolTypeSelected = kSFDP_Protocol_1s2s2s;
728                 }
729                 if ((handle->bfp.dw5 & 0x1UL) != 0UL)
730                 {
731                     protocolTypeSelected = kSFDP_Protocol_2s2s2s;
732                 }
733             }
734             else if ((cmdPolicy == kSFDP_CmdPolicy_SdrOutput) || (cmdPolicy == kSFDP_CmdPolicy_LowestThroughput))
735             {
736                 if ((handle->bfp.dw1 & 0x10000UL) != 0UL)
737                 {
738                     protocolTypeSelected = kSFDP_Protocol_1s1s2s;
739                 }
740             }
741             else
742             {
743             }
744             break;
745         }
746         case kSFDP_Flash4Pad:
747         {
748             if (cmdPolicy == kSFDP_CmdPolicy_DdrIO)
749             {
750                 if ((handle->curJEDECVersion > kSFDP_JESD216E) && ((handle->bfp.dw21 & 0x4UL) != 0UL))
751                 {
752                     protocolTypeSelected = kSFDP_Protocol_1s4d4d;
753                 }
754             }
755             else if (cmdPolicy == kSFDP_CmdPolicy_HighestThroughput)
756             {
757                 if ((handle->curJEDECVersion > kSFDP_JESD216E) && ((handle->bfp.dw21 & 0x8UL) != 0UL))
758                 {
759                     protocolTypeSelected = kSFDP_Protocol_4s4d4d;
760                 }
761             }
762             else if (cmdPolicy == kSFDP_CmdPolicy_SdrIO)
763             {
764                 if ((handle->bfp.dw1 & 0x200000UL) != 0UL)
765                 {
766                     protocolTypeSelected = kSFDP_Protocol_1s4s4s;
767                 }
768 
769                 if ((handle->bfp.dw5 & 0x10UL) != 0UL)
770                 {
771                     protocolTypeSelected = kSFDP_Protocol_4s4s4s;
772                 }
773             }
774             else if ((cmdPolicy == kSFDP_CmdPolicy_SdrOutput) || (cmdPolicy == kSFDP_CmdPolicy_LowestThroughput))
775             {
776                 if ((handle->bfp.dw1 & 0x400000UL) != 0UL)
777                 {
778                     protocolTypeSelected = kSFDP_Protocol_1s1s4s;
779                 }
780             }
781             else
782             {
783             }
784 
785             break;
786         }
787         case kSFDP_Flash8Pad:
788         {
789             if (cmdPolicy == kSFDP_CmdPolicy_SdrIO)
790             {
791                 if ((handle->curJEDECVersion >= kSFDP_JESD216A) && ((handle->bfp.dw17 & 0xFF00UL) != 0UL))
792                 {
793                     protocolTypeSelected = kSFDP_Protocol_1s8s8s;
794                 }
795 
796                 protocolTypeSelected = kSFDP_Protocol_8s8s8s;
797             }
798             else if ((cmdPolicy == kSFDP_CmdPolicy_SdrOutput) || (cmdPolicy == kSFDP_CmdPolicy_LowestThroughput))
799             {
800                 if ((handle->curJEDECVersion >= kSFDP_JESD216A) && ((handle->bfp.dw17 & 0xFF000000UL) != 0UL))
801                 {
802                     protocolTypeSelected = kSFDP_Protocol_1s1s8s;
803                 }
804             }
805             else if (cmdPolicy == kSFDP_CmdPolicy_DdrOutput)
806             {
807                 protocolTypeSelected = kSFDP_Protocol_1s8d8d;
808             }
809             else if (cmdPolicy == kSFDP_CmdPolicy_HighestThroughput)
810             {
811                 protocolTypeSelected = kSFDP_Protocol_8d8d8d;
812             }
813 
814             break;
815         }
816         default:
817         {
818             assert(false);
819         }
820     }
821 
822     return protocolTypeSelected;
823 }
824 
825 /******************* Get Flash Basic Attributes End *********************/
826 
827 /*********************** Get QPI Attributes Start ***********************/
828 /*!
829  * @brief Get sequence to entry 4s-4d-4d mode.
830  *
831  * @param[in] handle The parameter in type of @ref sfdp_handle_t.
832  * @param[out] ptrEntrySe The parameter to store sequences to enter 4s-4d-4d mode,
833                         in type of @ref sfdp_4s4d4d_entry_seq_t.
834  *
835  * @retval kSFDP_RET_Success Successfully to get 4s-4d-4d entry sequence.
836  * @retval kSFDP_RET_NotImplemented Current serial flash device does not support 4s-4d-4d entry sequence.
837  */
SFDP_Get4s4d4dEntrySeq(sfdp_handle_t * handle,sfdp_4s4d4d_entry_seq_t * ptrEntrySeq)838 sfdp_ret_type_t SFDP_Get4s4d4dEntrySeq(sfdp_handle_t *handle, sfdp_4s4d4d_entry_seq_t *ptrEntrySeq)
839 {
840     if (g_sfdpFuncTables.cmdSeq2QuadDDR.supported != 0UL)
841     {
842         (void)memcpy((void *)ptrEntrySeq, (void *)(&(handle->cmdSeq2QuadDDR)), sizeof(sfdp_4s4d4d_entry_seq_t));
843         return kSFDP_RET_Success;
844     }
845     else
846     {
847         return kSFDP_RET_NotImplemented;
848     }
849 }
850 
851 /*********************** Get QPI Attributes End ***********************/
852 
853 /*********************** Get OPI Attributes Start ***********************/
854 
855 /*!
856  * @brief Get command extension of input command.
857  *
858  * @param[in] handle The parameter in type of @ref sfdp_handle_t.
859  * @param[in] cmd The octal command used to calculate command extension.
860  *
861  * @return The command extension based on current serial flash device's command policy.
862  */
SFDP_GetOctalDTRCmdExtension(sfdp_handle_t * handle,uint8_t cmd)863 uint8_t SFDP_GetOctalDTRCmdExtension(sfdp_handle_t *handle, uint8_t cmd)
864 {
865     sfdp_octal_dtr_cmd_type_t octalCmdType = SFDP_GetOctalDTRCmdType(handle);
866     uint8_t cmdExtension                   = 0U;
867 
868     switch (octalCmdType)
869     {
870         case kSFDP_OctalDTRCmdExtensionSameAsCmd:
871         {
872             cmdExtension = cmd;
873             break;
874         }
875         case kSFDP_OctalDTRCmdExtensionInvertCmd:
876         {
877             cmdExtension = (uint8_t)(~cmd);
878             break;
879         }
880         case kSFDP_OctalDTRCmdExtensionCmdForm16Bit:
881         {
882             cmdExtension = (uint8_t)(0xFFFFU - cmd);
883             break;
884         }
885         default:
886         {
887             assert(false);
888         }
889     }
890 
891     return cmdExtension;
892 }
893 
894 /*!
895  * @brief Check if current serial flash device support input OPI(octal spi) command.
896  *
897  * @param[in] handle The parameter in type of @ref sfdp_handle_t.
898  * @param opiCmd The input octal spi command to check.
899  *
900  * @retval kSFDP_RET_OPICMDNotSupported The input opi command not supported.
901  * @retval kSFDP_RET_Success The input opi command supported.
902  */
SFDP_CheckOPICmdSupported(sfdp_handle_t * handle,sfdp_xspi_profile_opi_cmd_t opiCmd)903 sfdp_ret_type_t SFDP_CheckOPICmdSupported(sfdp_handle_t *handle, sfdp_xspi_profile_opi_cmd_t opiCmd)
904 {
905     uint32_t mask = 0UL;
906     uint8_t shift = 0U;
907 
908     if (g_sfdpFuncTables.xSPIProfile1Table.supported != 0UL)
909     {
910         shift = g_sfdpXspiProfile1OpiCmdShift[(uint8_t)opiCmd];
911         if (shift != 0xFFU)
912         {
913             mask = 1UL << shift;
914             if ((handle->xSPIProfile1Table.dw3 & mask) == 0UL)
915             {
916                 return kSFDP_RET_OPICMDNotSupported;
917             }
918         }
919     }
920 
921     if (g_sfdpFuncTables.xSPIProfile2Table.supported != 0UL)
922     {
923         shift = g_sfdpXspiProfile2OpiCmdShift[(uint8_t)opiCmd];
924         if (shift != 0xFFU)
925         {
926             mask = 1UL << shift;
927             if ((handle->xSPIProfile2Table.dw1 & mask) == 0UL)
928             {
929                 return kSFDP_RET_OPICMDNotSupported;
930             }
931         }
932     }
933 
934     if (g_sfdpFuncTables.SCCRegMapTable.supported != 0UL)
935     {
936         if ((handle->SCCRegMapTable.dw3 & 0x3C0UL) == 0x3C0UL)
937         {
938             return kSFDP_RET_OPICMDNotSupported;
939         }
940     }
941 
942     return kSFDP_RET_Success;
943 }
944 
945 /*!
946  * @brief Get 8d-8d-8d(octal DTR) mode entry sequences.
947  *
948  * @param[in] handle The parameter in type of @ref sfdp_handle_t.
949  * @param[out] ptrEntrySeq The variable to store sequences to enter 8d-8d-8d mode.
950  *
951  * @retval kSFDP_RET_Success Successfully to get sequences to enter 8d-8d-8d mode.
952  * @retval kSFDP_RET_NotImplemented Current serial flash device do not support sequences to enter 8d-8d-8d mode.
953  */
SFDP_Get8d8d8dEntrySeq(sfdp_handle_t * handle,sfdp_8d8d8d_entry_seq_t * ptrEntrySeq)954 sfdp_ret_type_t SFDP_Get8d8d8dEntrySeq(sfdp_handle_t *handle, sfdp_8d8d8d_entry_seq_t *ptrEntrySeq)
955 {
956     if (g_sfdpFuncTables.cmdSeq2OctalDDR.supported != 0UL)
957     {
958         (void)memcpy((void *)ptrEntrySeq, (void *)(&(handle->cmdSeq2OctalDDR)), sizeof(sfdp_8d8d8d_entry_seq_t));
959         return kSFDP_RET_Success;
960     }
961     else
962     {
963         return kSFDP_RET_NotImplemented;
964     }
965 }
966 
967 /*********************** Get OPI Attributes End ***********************/
968 
969 /********************* Get Device's Commands Start ********************/
970 /*!
971  * @brief Get read command information.
972  *
973  * @param[in] handle The parameter in type of @ref sfdp_handle_t.
974  * @param[in] protocolType The protocol type for current serial flash device.
975  * @param[in] clkFreq The frequency of serial clock.
976  * @param[out] ptrReadCmdInfo The variable of store read command information,
977  *                          please refer to @ref sfdp_read_cmd_info_t for details.
978  */
SFDP_GetReadCmdInfo(sfdp_handle_t * handle,sfdp_protocol_type_t protocolType,uint32_t clkFreq,sfdp_read_cmd_info_t * ptrReadCmdInfo)979 void SFDP_GetReadCmdInfo(sfdp_handle_t *handle,
980                          sfdp_protocol_type_t protocolType,
981                          uint32_t clkFreq,
982                          sfdp_read_cmd_info_t *ptrReadCmdInfo)
983 {
984     assert(handle != NULL);
985 
986     if (protocolType >= kSFDP_Protocol_1s1s8s)
987     {
988         SFDP_Get8PadReadCmd(handle, protocolType, clkFreq, ptrReadCmdInfo);
989     }
990     else if (protocolType >= kSFDP_Protocol_1s1s4s)
991     {
992         SFDP_Get4PadReadCmd(handle, protocolType, clkFreq, ptrReadCmdInfo);
993     }
994     else if (protocolType >= kSFDP_Protocol_1s1s2s)
995     {
996         SFDP_Get2PadReadCmd(handle, protocolType, ptrReadCmdInfo);
997     }
998     else
999     {
1000         SFDP_Get1PadReadCmd(handle, protocolType, ptrReadCmdInfo);
1001     }
1002 }
1003 
1004 /*!
1005  * @brief Get chip erase information.
1006  *
1007  * @param[in] handle The parameter in type of @ref sfdp_handle_t.
1008  * @param[out] ptrChipEraseCmdInfo The variable to store chip erase command information,
1009  *                                  please refer to @ref sfdp_chip_erase_cmd_info_t for details.
1010  */
SFDP_GetChipEraseCmdInfo(sfdp_handle_t * handle,sfdp_chip_erase_cmd_info_t * ptrChipEraseCmdInfo)1011 void SFDP_GetChipEraseCmdInfo(sfdp_handle_t *handle, sfdp_chip_erase_cmd_info_t *ptrChipEraseCmdInfo)
1012 {
1013     assert(handle != NULL);
1014 
1015     ptrChipEraseCmdInfo->typicalTime = 0UL;
1016     if (handle->curJEDECVersion >= kSFDP_JESD216A)
1017     {
1018         uint8_t tmp8 = (uint8_t)((handle->bfp.dw11 & 0x7F0000000UL) >> 24UL);
1019 
1020         switch ((tmp8 & 0x60U) >> 5U)
1021         {
1022             case 0x0U:
1023             {
1024                 ptrChipEraseCmdInfo->typicalTime = 16UL * (tmp8 & 0x1FU);
1025                 break;
1026             }
1027             case 0x01U:
1028             {
1029                 ptrChipEraseCmdInfo->typicalTime = 256UL * (tmp8 & 0x1FU);
1030                 break;
1031             }
1032             case 0x02U:
1033             {
1034                 ptrChipEraseCmdInfo->typicalTime = 4000UL * (tmp8 & 0x1FU);
1035                 break;
1036             }
1037             case 0x03U:
1038             {
1039                 ptrChipEraseCmdInfo->typicalTime = 64000UL * (tmp8 & 0x1FU);
1040                 break;
1041             }
1042             default:
1043             {
1044                 assert(false);
1045             }
1046         }
1047     }
1048 
1049     ptrChipEraseCmdInfo->instruction = 0x60U;
1050 }
1051 
1052 /*!
1053  * @brief Get sector erase command information.
1054  *
1055  * @param[in] handle The parameter in type of @ref sfdp_handle_t.
1056  * @param[in] type Used to select erase command type, please refer to @ref sfdp_erase_cmd_type_t.
1057  * @param[out] ptrEraseCmdInfo The variable to store erase command information,
1058                                 please refer to @ref sfdp_erase_cmd_info_t for details.
1059  *
1060  * @retval kSFDP_RET_EraseCMDNotSupported Selected type of erase command not supported.
1061  * @retval kSFDP_RET_Success Successfully to get selected erase command information.
1062  */
SFDP_GetSectorEraseCmdInfo(sfdp_handle_t * handle,sfdp_erase_cmd_type_t type,sfdp_erase_cmd_info_t * ptrEraseCmdInfo)1063 sfdp_ret_type_t SFDP_GetSectorEraseCmdInfo(sfdp_handle_t *handle,
1064                                            sfdp_erase_cmd_type_t type,
1065                                            sfdp_erase_cmd_info_t *ptrEraseCmdInfo)
1066 {
1067     uint32_t sizeFactor = 0UL;
1068     uint32_t tmp32      = 0UL;
1069 
1070     if (type <= kSFDP_EraseCmdType2)
1071     {
1072         tmp32 = handle->bfp.dw8;
1073     }
1074     else
1075     {
1076         tmp32 = handle->bfp.dw9;
1077     }
1078 
1079     tmp32 = (tmp32 >> (16UL * ((uint32_t)type % 2UL))) & 0xFFFFUL;
1080 
1081     sizeFactor = tmp32 & 0xFFUL;
1082     if (sizeFactor == 0UL)
1083     {
1084         return kSFDP_RET_EraseCMDNotSupported;
1085     }
1086 
1087     ptrEraseCmdInfo->eraseSize   = 1UL << sizeFactor;
1088     ptrEraseCmdInfo->instruction = (tmp32 & 0xFF00UL) >> 8UL;
1089 
1090     if ((handle->addrMode != kSFDP_AddrMode_3ByteOnly) && (g_sfdpFuncTables.fourByteAddrInstTable.supported != 0U))
1091     {
1092         ptrEraseCmdInfo->instruction = ((handle->fourByteAddrInstTable.dw2 >> (8 * (uint8_t)(type))) & 0xFFUL);
1093     }
1094 
1095     tmp32 = (handle->bfp.dw10) >> 4UL;
1096     tmp32 = (tmp32 >> (7UL * ((uint32_t)type % 2UL))) & 0x7FUL;
1097 
1098     switch ((tmp32 & 0x60U) >> 5U)
1099     {
1100         case 0x0UL:
1101         {
1102             ptrEraseCmdInfo->typicalTime = tmp32 & 0x1FUL;
1103             break;
1104         }
1105         case 0x1UL:
1106         {
1107             ptrEraseCmdInfo->typicalTime = (tmp32 & 0x1FUL) * 16UL;
1108             break;
1109         }
1110         case 0x2UL:
1111         {
1112             ptrEraseCmdInfo->typicalTime = (tmp32 & 0x1FUL) * 128UL;
1113             break;
1114         }
1115         case 0x3UL:
1116         {
1117             ptrEraseCmdInfo->typicalTime = (tmp32 & 0x1FUL) * 1000UL;
1118             break;
1119         }
1120         default:
1121         {
1122             assert(false);
1123         }
1124     }
1125 
1126     return kSFDP_RET_Success;
1127 }
1128 
1129 /*!
1130  * @brief Get page program command information.
1131  *
1132  * @param[in] handle The parameter in type of @ref sfdp_handle_t.
1133  * @param[in] protocolType The protocol type for current serial flash device.
1134  * @param[out] ptrPageProgramCmdInfo The variable to store page program command information,
1135  *                                  please refer to @ref sfdp_page_program_cmd_info_t.
1136  */
SFDP_GetPageProgramCmdInfo(sfdp_handle_t * handle,sfdp_protocol_type_t protocolType,sfdp_page_program_cmd_info_t * ptrPageProgramCmdInfo)1137 void SFDP_GetPageProgramCmdInfo(sfdp_handle_t *handle,
1138                                 sfdp_protocol_type_t protocolType,
1139                                 sfdp_page_program_cmd_info_t *ptrPageProgramCmdInfo)
1140 {
1141     uint32_t tmp32 = 0UL;
1142     if (handle->curJEDECVersion >= kSFDP_JESD216A)
1143     {
1144         tmp32 = ((handle->bfp.dw11 & 0x3F00UL) >> 8UL);
1145         if ((tmp32 & 0x20U) == 0U)
1146         {
1147             ptrPageProgramCmdInfo->typicalTime = ((tmp32 & 0x1FUL) + 1UL) * 8UL;
1148         }
1149         else
1150         {
1151             ptrPageProgramCmdInfo->typicalTime = ((tmp32 & 0x1FUL) + 1UL) * 64UL;
1152         }
1153     }
1154 
1155     ptrPageProgramCmdInfo->instruction = 0x02U;
1156 
1157     if ((handle->addrMode != kSFDP_AddrMode_3ByteOnly) && (g_sfdpFuncTables.fourByteAddrInstTable.supported != 0U))
1158     {
1159         /* In case of 4 byte address mode. */
1160         if (protocolType == kSFDP_Protocol_1s1s1s)
1161         {
1162             ptrPageProgramCmdInfo->instruction = 0x12U;
1163         }
1164         else if (protocolType == kSFDP_Protocol_1s1s4s)
1165         {
1166             ptrPageProgramCmdInfo->instruction = 0x34U;
1167         }
1168         else if (protocolType == kSFDP_Protocol_1s4s4s)
1169         {
1170             ptrPageProgramCmdInfo->instruction = 0x3EU;
1171         }
1172         else if (protocolType == kSFDP_Protocol_1s1s8s)
1173         {
1174             ptrPageProgramCmdInfo->instruction = 0x84U;
1175         }
1176         else if (protocolType == kSFDP_Protocol_1s8s8s)
1177         {
1178             ptrPageProgramCmdInfo->instruction = 0x8EU;
1179         }
1180         else if (protocolType == kSFDP_Protocol_8d8d8d)
1181         {
1182             ptrPageProgramCmdInfo->instruction = 0x12U;
1183         }
1184         else
1185         {
1186             /* Avoid violation of MISRA C-2012 rules. */
1187         }
1188     }
1189 }
1190 
1191 /*!
1192  * @brief Get read status command information.
1193  *
1194  * @param[in] handle The parameter in type of @ref sfdp_handle_t.
1195  * @param[in] protocolType The protocol type for current serial flash device.
1196  * @param[out] ptrReadStatusCmdInfo The variable to store read status command information,
1197  *                              please refer to @ref sfdp_read_status_cmd_info_t.
1198  */
SFDP_GetReadStatusCmdInfo(sfdp_handle_t * handle,sfdp_protocol_type_t protocolType,sfdp_read_status_cmd_info_t * ptrReadStatusCmdInfo)1199 void SFDP_GetReadStatusCmdInfo(sfdp_handle_t *handle,
1200                                sfdp_protocol_type_t protocolType,
1201                                sfdp_read_status_cmd_info_t *ptrReadStatusCmdInfo)
1202 {
1203     ptrReadStatusCmdInfo->instruction = 0x05U;
1204     uint32_t tmp32                    = 0UL;
1205     if ((g_sfdpFuncTables.SCCRegMapTable.supported != 0U))
1206     {
1207         tmp32                          = handle->SCCRegMapTable.dw3;
1208         ptrReadStatusCmdInfo->addrSize = 8U * (uint8_t)(((tmp32 & 0x30000000UL) >> 28UL) + 1UL);
1209 
1210         if (protocolType == kSFDP_Protocol_1s1s1s)
1211         {
1212             if ((tmp32 & 0xC000000UL) == 0x8000000UL)
1213             {
1214                 ptrReadStatusCmdInfo->dummyCycle = (uint8_t)(tmp32 & 0xFUL);
1215             }
1216             else if ((tmp32 & 0xC000000UL) == 0UL)
1217             {
1218                 ptrReadStatusCmdInfo->dummyCycle = 0U;
1219             }
1220             else if ((tmp32 & 0xC000000UL) == 0x4000000UL)
1221             {
1222                 ptrReadStatusCmdInfo->dummyCycle = 8U;
1223             }
1224             else
1225             {
1226                 assert(false);
1227             }
1228         }
1229         else if (protocolType == kSFDP_Protocol_2s2s2s)
1230         {
1231             ptrReadStatusCmdInfo->dummyCycle = (uint8_t)((tmp32 & 0x3C00000UL) >> 22UL);
1232         }
1233         else if (protocolType == kSFDP_Protocol_4s4s4s)
1234         {
1235             ptrReadStatusCmdInfo->dummyCycle = (uint8_t)((tmp32 & 0x3C0000UL) >> 18UL);
1236         }
1237         else if (protocolType == kSFDP_Protocol_4s4d4d)
1238         {
1239             ptrReadStatusCmdInfo->dummyCycle = (uint8_t)((tmp32 & 0x3C000UL) >> 14UL);
1240         }
1241         else if (protocolType == kSFDP_Protocol_8s8s8s)
1242         {
1243             ptrReadStatusCmdInfo->dummyCycle = (uint8_t)((tmp32 & 0x3C00UL) >> 10UL);
1244         }
1245         else if (protocolType == kSFDP_Protocol_8d8d8d)
1246         {
1247             ptrReadStatusCmdInfo->dummyCycle = (uint8_t)((tmp32 & 0x3C0UL) >> 6UL);
1248         }
1249         else
1250         {
1251             ptrReadStatusCmdInfo->dummyCycle = 0U;
1252         }
1253     }
1254     else if ((g_sfdpFuncTables.xSPIProfile2Table.supported != 0U) &&
1255              (g_sfdpFuncTables.SCCRegMapxSPIProfile2Table.supported != 0U))
1256     {
1257         ptrReadStatusCmdInfo->addrSize   = 48U;
1258         ptrReadStatusCmdInfo->dummyCycle = (uint8_t)((handle->SCCRegMapxSPIProfile2Table.dw3 & 0xF800000UL) >> 23UL);
1259     }
1260 }
1261 
1262 /*!
1263  * @brief Get write status command information.
1264  *
1265  * @param[in] handle The parameter in type of @ref sfdp_handle_t.
1266  * @param[out] ptrWriteStatusCmdInfo The variable to stor write status command information,
1267  *                                  please refer to @ref sfdp_write_status_cmd_info_t.
1268  */
SFDP_GetWriteStatusCmdInfo(sfdp_handle_t * handle,sfdp_write_status_cmd_info_t * ptrWriteStatusCmdInfo)1269 void SFDP_GetWriteStatusCmdInfo(sfdp_handle_t *handle, sfdp_write_status_cmd_info_t *ptrWriteStatusCmdInfo)
1270 {
1271     ptrWriteStatusCmdInfo->instruction = 0x01U;
1272 }
1273 
1274 /*!
1275  * @brief Get erase suspend/resume command information.
1276  *
1277  * @param[in] handle The parameter in type of @ref sfdp_handle_t.
1278  * @param[out] ptrEraseSuspendResumeCmdInfo The variable to store erase suspend/resume command information, please
1279  *                                          refer to @ref sfdp_erase_suspend_resume_cmd_info_t for details.
1280  */
SFDP_GetEraseSuspendResumeCmdInfo(sfdp_handle_t * handle,sfdp_erase_suspend_resume_cmd_info_t * ptrEraseSuspendResumeCmdInfo)1281 void SFDP_GetEraseSuspendResumeCmdInfo(sfdp_handle_t *handle,
1282                                        sfdp_erase_suspend_resume_cmd_info_t *ptrEraseSuspendResumeCmdInfo)
1283 {
1284     uint32_t tmp32                             = 0UL;
1285     ptrEraseSuspendResumeCmdInfo->suspendInstr = (uint8_t)((handle->bfp.dw13 & 0xFF000000UL) >> 24UL);
1286     ptrEraseSuspendResumeCmdInfo->resumeInstr  = (uint8_t)((handle->bfp.dw13 & 0xFF0000UL) >> 16UL);
1287 
1288     tmp32 = handle->bfp.dw12;
1289 
1290     if ((tmp32 & 0x60000000UL) == 0x0UL)
1291     {
1292         ptrEraseSuspendResumeCmdInfo->eraseMaxLatency = ((((tmp32 & 0x1F000000UL) >> 24UL) + 1UL) * 128UL);
1293     }
1294     else if (((tmp32 & 0x60000000UL) >> 29UL) == 0x1UL)
1295     {
1296         ptrEraseSuspendResumeCmdInfo->eraseMaxLatency = ((((tmp32 & 0x1F000000UL) >> 24UL) + 1UL) * 1000UL);
1297     }
1298     else if (((tmp32 & 0x60000000UL) >> 29UL) == 0x2UL)
1299     {
1300         ptrEraseSuspendResumeCmdInfo->eraseMaxLatency = ((((tmp32 & 0x1F000000UL) >> 24UL) + 1UL) * 8000UL);
1301     }
1302     else
1303     {
1304         ptrEraseSuspendResumeCmdInfo->eraseMaxLatency = ((((tmp32 & 0x1F000000UL) >> 24UL) + 1UL) * 64000UL);
1305     }
1306 
1307     ptrEraseSuspendResumeCmdInfo->resumeToSuspendInterval = (((tmp32 & 0xF00000UL) >> 20UL) + 1UL) * 64UL;
1308 }
1309 
1310 /*!
1311  * @brief Get program suspend/resume command information.
1312  *
1313  * @param[in] handle The parameter in type of @ref sfdp_handle_t.
1314  * @param[out] ptrProgramSuspendResumeCmdInfo The variable to store program suspend/resume command information,
1315  * please refer to @ref sfdp_program_suspend_cmd_info_t for details.
1316  */
SFDP_GetProgramSuspendResumeCmdInfo(sfdp_handle_t * handle,sfdp_program_suspend_cmd_info_t * ptrProgramSuspendResumeCmdInfo)1317 void SFDP_GetProgramSuspendResumeCmdInfo(sfdp_handle_t *handle,
1318                                          sfdp_program_suspend_cmd_info_t *ptrProgramSuspendResumeCmdInfo)
1319 {
1320     uint32_t tmp32                               = 0UL;
1321     ptrProgramSuspendResumeCmdInfo->suspendInstr = (uint8_t)((handle->bfp.dw13 & 0xFF00UL) >> 8UL);
1322     ptrProgramSuspendResumeCmdInfo->resumeInstr  = (uint8_t)(handle->bfp.dw13 & 0xFFUL);
1323 
1324     tmp32 = handle->bfp.dw12;
1325 
1326     if ((tmp32 & 0xC0000UL) == 0UL)
1327     {
1328         ptrProgramSuspendResumeCmdInfo->eraseMaxLatency = ((((tmp32 & 0x3E000UL) >> 13UL) + 1UL) * 128UL);
1329     }
1330     else if ((tmp32 & 0xC0000UL) == (1UL << 18UL))
1331     {
1332         ptrProgramSuspendResumeCmdInfo->eraseMaxLatency = ((((tmp32 & 0x3E000UL) >> 13UL) + 1UL) * 1000UL);
1333     }
1334     else if ((tmp32 & 0xC0000UL) == (2UL << 18UL))
1335     {
1336         ptrProgramSuspendResumeCmdInfo->eraseMaxLatency = ((((tmp32 & 0x3E000UL) >> 13UL) + 1UL) * 8000UL);
1337     }
1338     else
1339     {
1340         ptrProgramSuspendResumeCmdInfo->eraseMaxLatency = ((((tmp32 & 0x3E000UL) >> 13UL) + 1UL) * 64000UL);
1341     }
1342 
1343     ptrProgramSuspendResumeCmdInfo->resumeToSuspendInterval = (((tmp32 & 0x1E00UL) >> 9UL) + 1UL) * 64UL;
1344 }
1345 
1346 /*!
1347  * @brief Get deep power down command information.
1348  *
1349  * @param[in] handle The parameter in type of @ref sfdp_handle_t.
1350  * @param[out] ptrDeepPowerDownCmdInfo The variable to store deep power down command information, please refer to
1351  * @ref sfdp_deep_power_down_cmd_info_t for details.
1352  */
SFDP_GetDeepPowerDownCmdInfo(sfdp_handle_t * handle,sfdp_deep_power_down_cmd_info_t * ptrDeepPowerDownCmdInfo)1353 void SFDP_GetDeepPowerDownCmdInfo(sfdp_handle_t *handle, sfdp_deep_power_down_cmd_info_t *ptrDeepPowerDownCmdInfo)
1354 {
1355     uint32_t tmp32                                   = handle->bfp.dw14;
1356     ptrDeepPowerDownCmdInfo->enterDeepPowerDownInstr = (uint8_t)((tmp32 & 0x7F800000UL) >> 23UL);
1357     ptrDeepPowerDownCmdInfo->exitDeepPowerDownDelay  = (uint8_t)((tmp32 & 0x7F8000UL) >> 15UL);
1358 
1359     if ((tmp32 & 0x6000) == 0UL)
1360     {
1361         ptrDeepPowerDownCmdInfo->exitDeepPowerDownDelay = (((tmp32 & 0x1F00UL) >> 8UL) + 1UL) * 128UL;
1362     }
1363     else if ((tmp32 & 0x6000) == (1UL << 13UL))
1364     {
1365         ptrDeepPowerDownCmdInfo->exitDeepPowerDownDelay = (((tmp32 & 0x1F00UL) >> 8UL) + 1UL) * 1000UL;
1366     }
1367     else if ((tmp32 & 0x6000) == (2UL << 13UL))
1368     {
1369         ptrDeepPowerDownCmdInfo->exitDeepPowerDownDelay = (((tmp32 & 0x1F00UL) >> 8UL) + 1UL) * 8000UL;
1370     }
1371     else
1372     {
1373         ptrDeepPowerDownCmdInfo->exitDeepPowerDownDelay = (((tmp32 & 0x1F00UL) >> 8UL) + 1UL) * 64000UL;
1374     }
1375 }
1376 
1377 /********************* Get Device's Commands End ********************/
1378 
1379 /************** Get Device register bit field information Start ************/
1380 /*!
1381  * @brief Get busy bit field information.
1382  *
1383  * @param[in] handle The parameter in type of @ref sfdp_handle_t.
1384  * @param[in] padNum Number of data pad.
1385  * @param[out] ptrBusyBitInfo The variable to store busy bit information.
1386  *
1387  * @retval kSFDP_RET_Success Successfully to get busy bit field information.
1388  */
SFDP_GetBusyBitInfo(sfdp_handle_t * handle,sfdp_flash_pad_num_t padNum,sfdp_wip_bit_info_t * ptrBusyBitInfo)1389 sfdp_ret_type_t SFDP_GetBusyBitInfo(sfdp_handle_t *handle,
1390                                     sfdp_flash_pad_num_t padNum,
1391                                     sfdp_wip_bit_info_t *ptrBusyBitInfo)
1392 {
1393     assert(handle != NULL);
1394 
1395     uint32_t bfpDw14 = handle->bfp.dw14;
1396     uint32_t sccrDw5 = 0UL;
1397 
1398     ptrBusyBitInfo->upperHalfWordSelected = false;
1399     if ((bfpDw14 & 0x2UL) != 0UL)
1400     {
1401         ptrBusyBitInfo->readInstr  = 0x05U;
1402         ptrBusyBitInfo->writeInstr = 0x01U;
1403         ptrBusyBitInfo->polarity   = false;
1404         ptrBusyBitInfo->location   = 0U;
1405     }
1406     else if ((bfpDw14 & 0x4UL) != 0UL)
1407     {
1408         ptrBusyBitInfo->readInstr  = 0x70U;
1409         ptrBusyBitInfo->writeInstr = 0x50U;
1410         ptrBusyBitInfo->polarity   = true;
1411         ptrBusyBitInfo->location   = 7U;
1412     }
1413     else
1414     {
1415         /* Reserved. */
1416     }
1417 
1418     if ((padNum == kSFDP_Flash1Pad))
1419     {
1420         ptrBusyBitInfo->dummyCycles   = 0U;
1421         ptrBusyBitInfo->addrFollowCmd = false;
1422     }
1423 
1424     if (g_sfdpFuncTables.SCCRegMapTable.supported)
1425     {
1426         sccrDw5 = handle->SCCRegMapTable.dw5;
1427         if ((sccrDw5 & 0x80000000UL) != 0UL)
1428         {
1429             if ((sccrDw5 & 0x10000000UL) != 0UL)
1430             {
1431                 ptrBusyBitInfo->addrFollowCmd = true;
1432             }
1433             else
1434             {
1435                 ptrBusyBitInfo->addrFollowCmd = false;
1436             }
1437 
1438             ptrBusyBitInfo->polarity              = ((sccrDw5 & 0x40000000UL) != 0x0UL) ? true : false;
1439             ptrBusyBitInfo->readInstr             = (uint8_t)((sccrDw5 & 0xFF00UL) >> 8UL);
1440             ptrBusyBitInfo->upperHalfWordSelected = ((sccrDw5 & 0x10000000UL) != 0UL) ? true : false;
1441             ptrBusyBitInfo->location              = (uint8_t)((sccrDw5 & 0x7000000UL) >> 24UL);
1442             ptrBusyBitInfo->dummyCycles           = (uint8_t)((sccrDw5 & 0xF0000UL) >> 16UL);
1443         }
1444     }
1445 
1446     return kSFDP_RET_Success;
1447 }
1448 
1449 /*!
1450  * @brief Get WEL(write enable) bit field information.
1451  *
1452  * @param[in] handle The parameter in type of @ref sfdp_handle_t.
1453  * @param[in] protocolType Current selected protocol type.
1454  * @param[out] ptrWelBitInfo The variable to store WEL bit information.
1455  *
1456  * @retval kSFDP_RET_Success Successfully to get WEL bit field information.
1457  */
SFDP_GetWELBitInfo(sfdp_handle_t * handle,sfdp_protocol_type_t protocolType,sfdp_wel_bit_info_t * ptrWelBitInfo)1458 sfdp_ret_type_t SFDP_GetWELBitInfo(sfdp_handle_t *handle,
1459                                    sfdp_protocol_type_t protocolType,
1460                                    sfdp_wel_bit_info_t *ptrWelBitInfo)
1461 {
1462     assert(handle != NULL);
1463 
1464     uint32_t tmp32 = handle->SCCRegMapTable.dw6;
1465 
1466     if ((tmp32 & (1UL << 31UL)) == 0UL)
1467     {
1468         return kSFDP_RET_NotImplemented;
1469     }
1470 
1471     if ((tmp32 & (1UL << 29UL)) != 0UL)
1472     {
1473         return kSFDP_RET_RegBitNotSupported;
1474     }
1475 
1476     ptrWelBitInfo->dummyCycles = 0U;
1477     if ((tmp32 & (1UL << 28UL)) == 0UL)
1478     {
1479         ptrWelBitInfo->addrFollowCmd = false;
1480 
1481         if ((protocolType == kSFDP_Protocol_4s4s4s) && ((tmp32 & (1UL << 20UL)) != 0UL))
1482         {
1483             ptrWelBitInfo->dummyCycles = (uint8_t)((tmp32 & 0xF0000UL) >> 16UL);
1484         }
1485 
1486         if ((protocolType == kSFDP_Protocol_4s4d4d) && ((tmp32 & (1UL << 21UL)) != 0UL))
1487         {
1488             ptrWelBitInfo->dummyCycles = (uint8_t)((tmp32 & 0xF0000UL) >> 16UL);
1489         }
1490 
1491         if ((protocolType == kSFDP_Protocol_8s8s8s) && ((tmp32 & (1UL << 22UL)) != 0UL))
1492         {
1493             ptrWelBitInfo->dummyCycles = (uint8_t)((tmp32 & 0xF0000UL) >> 16UL);
1494         }
1495 
1496         if ((protocolType == kSFDP_Protocol_8d8d8d) && ((tmp32 & (1UL << 23UL)) != 0UL))
1497         {
1498             ptrWelBitInfo->dummyCycles = (uint8_t)((tmp32 & 0xF0000UL) >> 16UL);
1499         }
1500     }
1501     else
1502     {
1503         ptrWelBitInfo->addrFollowCmd = true;
1504     }
1505 
1506     ptrWelBitInfo->readInstr             = (uint8_t)((tmp32 & 0xFF00UL) >> 8UL);
1507     ptrWelBitInfo->writeInstr            = (uint8_t)(tmp32 & 0xFFUL);
1508     ptrWelBitInfo->polarity              = ((tmp32 & (1UL << 30UL)) != 0UL);
1509     ptrWelBitInfo->location              = (uint8_t)((tmp32 & 0x7000000UL) >> 24UL);
1510     ptrWelBitInfo->upperHalfWordSelected = (bool)((tmp32 & (1UL << 27UL)) != 0UL);
1511     return kSFDP_RET_Success;
1512 }
1513 
1514 /*!
1515  * @brief Get program error bit field information.
1516  *
1517  * @param[in] handle The parameter in type of @ref sfdp_handle_t.
1518  * @param[in] protocolType Specify the protocol type to use.
1519  * @param[out] ptrProgramErrorBitInfo The variable to store program error bit information.
1520  *
1521  * @retval kSFDP_RET_NotImplemented Current serial flash do not implement the parameter table.
1522  * @retval kSFDP_RET_RegBitNotSupported Current serial flash do not support program error bit.
1523  * @retval kSFDP_RET_Success Successfully to get program error bit field information.
1524  */
SFDP_GetProgramErrorBitInfo(sfdp_handle_t * handle,sfdp_protocol_type_t protocolType,sfdp_program_error_bit_info_t * ptrProgramErrorBitInfo)1525 sfdp_ret_type_t SFDP_GetProgramErrorBitInfo(sfdp_handle_t *handle,
1526                                             sfdp_protocol_type_t protocolType,
1527                                             sfdp_program_error_bit_info_t *ptrProgramErrorBitInfo)
1528 {
1529     assert(handle != NULL);
1530 
1531     uint32_t tmp32 = handle->SCCRegMapTable.dw7;
1532 
1533     if ((tmp32 & (1UL << 31UL)) == 0UL)
1534     {
1535         return kSFDP_RET_NotImplemented;
1536     }
1537 
1538     if ((tmp32 & (1UL << 29UL)) != 0UL)
1539     {
1540         return kSFDP_RET_RegBitNotSupported;
1541     }
1542 
1543     ptrProgramErrorBitInfo->dummyCycles = 0U;
1544     if ((tmp32 & (1UL << 28UL)) == 0UL)
1545     {
1546         ptrProgramErrorBitInfo->addrFollowCmd = false;
1547 
1548         if ((protocolType == kSFDP_Protocol_4s4s4s) && ((tmp32 & (1UL << 20UL)) != 0UL))
1549         {
1550             ptrProgramErrorBitInfo->dummyCycles = (uint8_t)((tmp32 & 0xF0000UL) >> 16UL);
1551         }
1552 
1553         if ((protocolType == kSFDP_Protocol_4s4d4d) && ((tmp32 & (1UL << 21UL)) != 0UL))
1554         {
1555             ptrProgramErrorBitInfo->dummyCycles = (uint8_t)((tmp32 & 0xF0000UL) >> 16UL);
1556         }
1557 
1558         if ((protocolType == kSFDP_Protocol_8s8s8s) && ((tmp32 & (1UL << 22UL)) != 0UL))
1559         {
1560             ptrProgramErrorBitInfo->dummyCycles = (uint8_t)((tmp32 & 0xF0000UL) >> 16UL);
1561         }
1562 
1563         if ((protocolType == kSFDP_Protocol_8d8d8d) && ((tmp32 & (1UL << 23UL)) != 0UL))
1564         {
1565             ptrProgramErrorBitInfo->dummyCycles = (uint8_t)((tmp32 & 0xF0000UL) >> 16UL);
1566         }
1567     }
1568     else
1569     {
1570         ptrProgramErrorBitInfo->addrFollowCmd = true;
1571     }
1572 
1573     ptrProgramErrorBitInfo->readInstr             = (uint8_t)((tmp32 & 0xFF00UL) >> 8UL);
1574     ptrProgramErrorBitInfo->writeInstr            = (uint8_t)(tmp32 & 0xFFUL);
1575     ptrProgramErrorBitInfo->polarity              = ((tmp32 & (1UL << 30UL)) != 0UL);
1576     ptrProgramErrorBitInfo->location              = (uint8_t)((tmp32 & 0x7000000UL) >> 24UL);
1577     ptrProgramErrorBitInfo->upperHalfWordSelected = (bool)((tmp32 & (1UL << 27UL)) != 0UL);
1578     return kSFDP_RET_Success;
1579 }
1580 
1581 /*!
1582  * @brief Get erase error bit field information.
1583  *
1584  * @param[in] handle The parameter in type of @ref sfdp_handle_t.
1585  * @param[in] protocolType Specify the protocol type to use.
1586  * @param[out] ptrEraseErrorBitInfo The variable to store erase error bit information.
1587  *
1588  * @retval kSFDP_RET_NotImplemented Current serial flash do not implement the parameter table.
1589  * @retval kSFDP_RET_RegBitNotSupported Current serial flash do not support erase error bit.
1590  * @retval kSFDP_RET_Success Successfully to get erase error bit field information.
1591  */
SFDP_GetEraseErrorBitInfo(sfdp_handle_t * handle,sfdp_protocol_type_t protocolType,sfdp_erase_error_bit_info_t * ptrEraseErrorBitInfo)1592 sfdp_ret_type_t SFDP_GetEraseErrorBitInfo(sfdp_handle_t *handle,
1593                                           sfdp_protocol_type_t protocolType,
1594                                           sfdp_erase_error_bit_info_t *ptrEraseErrorBitInfo)
1595 {
1596     assert(handle != NULL);
1597 
1598     uint32_t tmp32 = handle->SCCRegMapTable.dw8;
1599 
1600     if ((tmp32 & (1UL << 31UL)) == 0UL)
1601     {
1602         return kSFDP_RET_NotImplemented;
1603     }
1604 
1605     if ((tmp32 & (1UL << 29UL)) != 0UL)
1606     {
1607         return kSFDP_RET_RegBitNotSupported;
1608     }
1609 
1610     ptrEraseErrorBitInfo->dummyCycles = 0U;
1611     if ((tmp32 & (1UL << 28UL)) == 0UL)
1612     {
1613         ptrEraseErrorBitInfo->addrFollowCmd = false;
1614 
1615         if ((protocolType == kSFDP_Protocol_4s4s4s) && ((tmp32 & (1UL << 20UL)) != 0UL))
1616         {
1617             ptrEraseErrorBitInfo->dummyCycles = (uint8_t)((tmp32 & 0xF0000UL) >> 16UL);
1618         }
1619 
1620         if ((protocolType == kSFDP_Protocol_4s4d4d) && ((tmp32 & (1UL << 21UL)) != 0UL))
1621         {
1622             ptrEraseErrorBitInfo->dummyCycles = (uint8_t)((tmp32 & 0xF0000UL) >> 16UL);
1623         }
1624 
1625         if ((protocolType == kSFDP_Protocol_8s8s8s) && ((tmp32 & (1UL << 22UL)) != 0UL))
1626         {
1627             ptrEraseErrorBitInfo->dummyCycles = (uint8_t)((tmp32 & 0xF0000UL) >> 16UL);
1628         }
1629 
1630         if ((protocolType == kSFDP_Protocol_8d8d8d) && ((tmp32 & (1UL << 23UL)) != 0UL))
1631         {
1632             ptrEraseErrorBitInfo->dummyCycles = (uint8_t)((tmp32 & 0xF0000UL) >> 16UL);
1633         }
1634     }
1635     else
1636     {
1637         ptrEraseErrorBitInfo->addrFollowCmd = true;
1638     }
1639 
1640     ptrEraseErrorBitInfo->readInstr             = (uint8_t)((tmp32 & 0xFF00UL) >> 8UL);
1641     ptrEraseErrorBitInfo->writeInstr            = (uint8_t)(tmp32 & 0xFFUL);
1642     ptrEraseErrorBitInfo->polarity              = ((tmp32 & (1UL << 30UL)) != 0UL);
1643     ptrEraseErrorBitInfo->location              = (uint8_t)((tmp32 & 0x7000000UL) >> 24UL);
1644     ptrEraseErrorBitInfo->upperHalfWordSelected = (bool)((tmp32 & (1UL << 27UL)) != 0UL);
1645     return kSFDP_RET_Success;
1646 }
1647 
1648 /*!
1649  * @brief Get variable dummy cycle information.
1650  *
1651  * @param[in] handle The parameter in type of @ref sfdp_handle_t.
1652  * @param[in] protocolType Specify the protocol type to use.
1653  * @param[out] ptrVariableDCInfo The variable to updated dummy cycle value..
1654  *
1655  * @retval kSFDP_RET_NotImplemented Current serial flash do not implement the parameter table.
1656  * @retval kSFDP_RET_RegBitNotSupported Current serial flash do not support variable dummy cycle bit.
1657  * @retval kSFDP_RET_Success Successfully to get variable dummy cycle bit field information.
1658  */
SFDP_GetVolatileVariableDummyCycleInfo(sfdp_handle_t * handle,sfdp_protocol_type_t protocolType,sfdp_variable_dummy_cycle_t * ptrVariableDCInfo)1659 sfdp_ret_type_t SFDP_GetVolatileVariableDummyCycleInfo(sfdp_handle_t *handle,
1660                                                        sfdp_protocol_type_t protocolType,
1661                                                        sfdp_variable_dummy_cycle_t *ptrVariableDCInfo)
1662 {
1663     assert(handle != NULL);
1664 
1665     uint32_t tmp32 = handle->SCCRegMapTable.dw9;
1666 
1667     if ((tmp32 & (1UL << 31UL)) == 0UL)
1668     {
1669         return kSFDP_RET_NotImplemented;
1670     }
1671 
1672     if ((tmp32 & (1UL << 29UL)) != 0UL)
1673     {
1674         return kSFDP_RET_RegBitNotSupported;
1675     }
1676 
1677     ptrVariableDCInfo->dummyCycles = 0U;
1678     if ((tmp32 & (1UL << 28UL)) == 0UL)
1679     {
1680         ptrVariableDCInfo->addrFollowCmd = false;
1681 
1682         if ((protocolType == kSFDP_Protocol_4s4s4s) && ((tmp32 & (1UL << 20UL)) != 0UL))
1683         {
1684             ptrVariableDCInfo->dummyCycles = (uint8_t)((tmp32 & 0xF0000UL) >> 16UL);
1685         }
1686 
1687         if ((protocolType == kSFDP_Protocol_4s4d4d) && ((tmp32 & (1UL << 21UL)) != 0UL))
1688         {
1689             ptrVariableDCInfo->dummyCycles = (uint8_t)((tmp32 & 0xF0000UL) >> 16UL);
1690         }
1691 
1692         if ((protocolType == kSFDP_Protocol_8s8s8s) && ((tmp32 & (1UL << 22UL)) != 0UL))
1693         {
1694             ptrVariableDCInfo->dummyCycles = (uint8_t)((tmp32 & 0xF0000UL) >> 16UL);
1695         }
1696 
1697         if ((protocolType == kSFDP_Protocol_8d8d8d) && ((tmp32 & (1UL << 23UL)) != 0UL))
1698         {
1699             ptrVariableDCInfo->dummyCycles = (uint8_t)((tmp32 & 0xF0000UL) >> 16UL);
1700         }
1701     }
1702     else
1703     {
1704         ptrVariableDCInfo->addrFollowCmd = true;
1705     }
1706 
1707     ptrVariableDCInfo->readInstr             = (uint8_t)((tmp32 & 0xFF00UL) >> 8UL);
1708     ptrVariableDCInfo->writeInstr            = (uint8_t)(tmp32 & 0xFFUL);
1709     ptrVariableDCInfo->bitFieldWidth         = ((tmp32 & 0x60000000UL) >> 29UL);
1710     ptrVariableDCInfo->location              = (uint8_t)((tmp32 & 0x7000000UL) >> 24UL);
1711     ptrVariableDCInfo->upperHalfWordSelected = (bool)((tmp32 & (1UL << 27UL)) != 0UL);
1712 
1713     return kSFDP_RET_Success;
1714 }
1715 /************** Get Device register bit field information End ************/
1716