1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** LevelX Component                                                      */
17 /**                                                                       */
18 /**   NAND Flash                                                          */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define LX_SOURCE_CODE
24 
25 
26 /* Disable ThreadX error checking.  */
27 
28 #ifndef LX_DISABLE_ERROR_CHECKING
29 #define LX_DISABLE_ERROR_CHECKING
30 #endif
31 
32 
33 /* Include necessary system files.  */
34 
35 #include "lx_api.h"
36 
37 
38 /**************************************************************************/
39 /*                                                                        */
40 /*  FUNCTION                                               RELEASE        */
41 /*                                                                        */
42 /*    _lx_nand_flash_metadata_build                       PORTABLE C      */
43 /*                                                           6.2.1       */
44 /*  AUTHOR                                                                */
45 /*                                                                        */
46 /*    Xiuwen Cai, Microsoft Corporation                                   */
47 /*                                                                        */
48 /*  DESCRIPTION                                                           */
49 /*                                                                        */
50 /*    This function rewrites all metadata pages.                          */
51 /*                                                                        */
52 /*  INPUT                                                                 */
53 /*                                                                        */
54 /*    nand_flash                            NAND flash instance           */
55 /*                                                                        */
56 /*  OUTPUT                                                                */
57 /*                                                                        */
58 /*    return status                                                       */
59 /*                                                                        */
60 /*  CALLS                                                                 */
61 /*                                                                        */
62 /*    _lx_nand_flash_metadata_write         Write metadata                */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    Internal LevelX                                                     */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  03-08-2023     Xiuwen Cai               Initial Version 6.2.1        */
73 /*                                                                        */
74 /**************************************************************************/
_lx_nand_flash_metadata_build(LX_NAND_FLASH * nand_flash)75 UINT  _lx_nand_flash_metadata_build(LX_NAND_FLASH *nand_flash)
76 {
77 
78 UINT                status;
79 LX_NAND_DEVICE_INFO *nand_device_info_page;
80 UINT                page_count;
81 UINT                i;
82 
83     /* Build device info page.  */
84     nand_device_info_page = (LX_NAND_DEVICE_INFO*)nand_flash -> lx_nand_flash_page_buffer;
85     nand_device_info_page -> lx_nand_device_info_signature1 = LX_NAND_DEVICE_INFO_SIGNATURE1;
86     nand_device_info_page -> lx_nand_device_info_signature2 = LX_NAND_DEVICE_INFO_SIGNATURE2;
87     nand_device_info_page -> lx_nand_device_info_major_version = LEVELX_MAJOR_VERSION;
88     nand_device_info_page -> lx_nand_device_info_minor_version = LEVELX_MINOR_VERSION;
89     nand_device_info_page -> lx_nand_device_info_patch_version = LEVELX_PATCH_VERSION;
90     nand_device_info_page -> lx_nand_device_info_metadata_block_number = nand_flash -> lx_nand_flash_metadata_block_number;
91     nand_device_info_page -> lx_nand_device_info_backup_metadata_block_number = nand_flash -> lx_nand_flash_backup_metadata_block_number;
92     nand_device_info_page -> lx_nand_device_info_base_erase_count = nand_flash -> lx_nand_flash_base_erase_count;
93 
94     /* Write metadata.  */
95     status = _lx_nand_flash_metadata_write(nand_flash, (UCHAR*)nand_device_info_page, LX_NAND_PAGE_TYPE_DEVICE_INFO);
96 
97     /* Check return status.  */
98     if (status != LX_SUCCESS)
99     {
100 
101         /* Return error status.  */
102         return(status);
103     }
104 
105     /* Calculate page count for erase count table.  */
106     page_count = (nand_flash -> lx_nand_flash_erase_count_table_size + (nand_flash -> lx_nand_flash_bytes_per_page - 1)) / nand_flash -> lx_nand_flash_bytes_per_page;
107 
108     /* Loop to write all the pages.  */
109     for (i = 0; i < page_count; i++)
110     {
111 
112         /* Write erase count table.  */
113         status = _lx_nand_flash_metadata_write(nand_flash, (UCHAR*)(nand_flash -> lx_nand_flash_erase_count_table +
114                                                 i * nand_flash -> lx_nand_flash_bytes_per_page),
115                                                 LX_NAND_PAGE_TYPE_ERASE_COUNT_TABLE | i);
116 
117         /* Check return status.  */
118         if (status != LX_SUCCESS)
119         {
120 
121             /* Return error status.  */
122             return(status);
123         }
124     }
125 
126     /* Calculate page count for block mapping table.  */
127     page_count = (nand_flash -> lx_nand_flash_block_mapping_table_size + (nand_flash -> lx_nand_flash_bytes_per_page - 1)) / nand_flash -> lx_nand_flash_bytes_per_page;
128 
129     /* Loop to write all the pages.  */
130     for (i = 0; i < page_count; i++)
131     {
132 
133         /* Write block mapping table.  */
134         status = _lx_nand_flash_metadata_write(nand_flash, (UCHAR*)(nand_flash -> lx_nand_flash_block_mapping_table +
135                                                 i * nand_flash -> lx_nand_flash_bytes_per_page / sizeof(*nand_flash -> lx_nand_flash_block_mapping_table)),
136                                                 LX_NAND_PAGE_TYPE_BLOCK_MAPPING_TABLE | i);
137         /* Check return status.  */
138         if (status != LX_SUCCESS)
139         {
140 
141             /* Return error status.  */
142             return(status);
143         }
144     }
145 
146     /* Calculate page count for block status table.  */
147     page_count = (nand_flash -> lx_nand_flash_block_status_table_size + (nand_flash -> lx_nand_flash_bytes_per_page - 1)) / nand_flash -> lx_nand_flash_bytes_per_page;
148 
149     /* Loop to write all the pages.  */
150     for (i = 0; i < page_count; i++)
151     {
152 
153         /* Write block status table.  */
154         status = _lx_nand_flash_metadata_write(nand_flash, (UCHAR*)(nand_flash -> lx_nand_flash_block_status_table +
155                                             i * nand_flash -> lx_nand_flash_bytes_per_page / sizeof(*nand_flash -> lx_nand_flash_block_status_table)),
156                                             LX_NAND_PAGE_TYPE_BLOCK_STATUS_TABLE | i);
157 
158         /* Check return status.  */
159         if (status != LX_SUCCESS)
160         {
161 
162             /* Return error status.  */
163             return(status);
164         }
165     }
166 
167     /* Return status.  */
168     return(status);
169 }
170 
171