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_block_mapping_set                    PORTABLE C      */
43 /*                                                           6.2.1       */
44 /*  AUTHOR                                                                */
45 /*                                                                        */
46 /*    Xiuwen Cai, Microsoft Corporation                                   */
47 /*                                                                        */
48 /*  DESCRIPTION                                                           */
49 /*                                                                        */
50 /*    This function sets block mappings.                                  */
51 /*                                                                        */
52 /*  INPUT                                                                 */
53 /*                                                                        */
54 /*    nand_flash                            NAND flash instance           */
55 /*    logical_sector                        Logical sector number         */
56 /*    block                                 Block number                  */
57 /*                                                                        */
58 /*  OUTPUT                                                                */
59 /*                                                                        */
60 /*    return status                                                       */
61 /*                                                                        */
62 /*  CALLS                                                                 */
63 /*                                                                        */
64 /*    _lx_nand_flash_metadata_write         Write metadata                */
65 /*                                                                        */
66 /*  CALLED BY                                                             */
67 /*                                                                        */
68 /*    Internal LevelX                                                     */
69 /*                                                                        */
70 /*  RELEASE HISTORY                                                       */
71 /*                                                                        */
72 /*    DATE              NAME                      DESCRIPTION             */
73 /*                                                                        */
74 /*  03-08-2023     Xiuwen Cai               Initial Version 6.2.1        */
75 /*                                                                        */
76 /**************************************************************************/
_lx_nand_flash_block_mapping_set(LX_NAND_FLASH * nand_flash,ULONG logical_sector,ULONG block)77 UINT  _lx_nand_flash_block_mapping_set(LX_NAND_FLASH *nand_flash, ULONG logical_sector, ULONG block)
78 {
79 
80 UINT    block_mapping_index;
81 UCHAR   page_number;
82 UINT    status;
83 
84 
85     /* Get the mapping index from logic sector address.  */
86     block_mapping_index = logical_sector / nand_flash -> lx_nand_flash_pages_per_block;
87 
88     /* Check the address range.  */
89     if (block_mapping_index > nand_flash -> lx_nand_flash_block_mapping_table_size / sizeof(*nand_flash -> lx_nand_flash_block_mapping_table))
90     {
91 
92         /* Out of range, return an error. */
93         return(LX_ERROR);
94     }
95 
96     /* Save the block number to mapping table.  */
97     nand_flash -> lx_nand_flash_block_mapping_table[block_mapping_index] = (USHORT)block;
98 
99     /* Get the page number to write.  */
100     page_number = (UCHAR)(block_mapping_index * sizeof(*nand_flash -> lx_nand_flash_block_mapping_table) / nand_flash -> lx_nand_flash_bytes_per_page);
101 
102     /* Save the mapping table.  */
103     status = _lx_nand_flash_metadata_write(nand_flash, ((UCHAR*)nand_flash -> lx_nand_flash_block_mapping_table) +
104                                                 page_number * nand_flash -> lx_nand_flash_bytes_per_page,
105                                                 LX_NAND_PAGE_TYPE_BLOCK_MAPPING_TABLE | page_number);
106 
107     /* Return status.  */
108     return(status);
109 }
110 
111