1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** LevelX Component                                                      */
16 /**                                                                       */
17 /**   NAND Flash                                                          */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define LX_SOURCE_CODE
23 
24 
25 /* Disable ThreadX error checking.  */
26 
27 #ifndef LX_DISABLE_ERROR_CHECKING
28 #define LX_DISABLE_ERROR_CHECKING
29 #endif
30 
31 
32 /* Include necessary system files.  */
33 
34 #include "lx_api.h"
35 
36 
37 /**************************************************************************/
38 /*                                                                        */
39 /*  FUNCTION                                               RELEASE        */
40 /*                                                                        */
41 /*    _lx_nand_flash_block_mapping_set                    PORTABLE C      */
42 /*                                                           6.2.1       */
43 /*  AUTHOR                                                                */
44 /*                                                                        */
45 /*    Xiuwen Cai, Microsoft Corporation                                   */
46 /*                                                                        */
47 /*  DESCRIPTION                                                           */
48 /*                                                                        */
49 /*    This function sets block mappings.                                  */
50 /*                                                                        */
51 /*  INPUT                                                                 */
52 /*                                                                        */
53 /*    nand_flash                            NAND flash instance           */
54 /*    logical_sector                        Logical sector number         */
55 /*    block                                 Block number                  */
56 /*                                                                        */
57 /*  OUTPUT                                                                */
58 /*                                                                        */
59 /*    return status                                                       */
60 /*                                                                        */
61 /*  CALLS                                                                 */
62 /*                                                                        */
63 /*    _lx_nand_flash_metadata_write         Write metadata                */
64 /*                                                                        */
65 /*  CALLED BY                                                             */
66 /*                                                                        */
67 /*    Internal LevelX                                                     */
68 /*                                                                        */
69 /*  RELEASE HISTORY                                                       */
70 /*                                                                        */
71 /*    DATE              NAME                      DESCRIPTION             */
72 /*                                                                        */
73 /*  03-08-2023     Xiuwen Cai               Initial Version 6.2.1        */
74 /*                                                                        */
75 /**************************************************************************/
_lx_nand_flash_block_mapping_set(LX_NAND_FLASH * nand_flash,ULONG logical_sector,ULONG block)76 UINT  _lx_nand_flash_block_mapping_set(LX_NAND_FLASH *nand_flash, ULONG logical_sector, ULONG block)
77 {
78 
79 UINT    block_mapping_index;
80 UCHAR   page_number;
81 UINT    status;
82 
83 
84     /* Get the mapping index from logic sector address.  */
85     block_mapping_index = logical_sector / nand_flash -> lx_nand_flash_pages_per_block;
86 
87     /* Check the address range.  */
88     if (block_mapping_index > nand_flash -> lx_nand_flash_block_mapping_table_size / sizeof(*nand_flash -> lx_nand_flash_block_mapping_table))
89     {
90 
91         /* Out of range, return an error. */
92         return(LX_ERROR);
93     }
94 
95     /* Save the block number to mapping table.  */
96     nand_flash -> lx_nand_flash_block_mapping_table[block_mapping_index] = (USHORT)block;
97 
98     /* Get the page number to write.  */
99     page_number = (UCHAR)(block_mapping_index * sizeof(*nand_flash -> lx_nand_flash_block_mapping_table) / nand_flash -> lx_nand_flash_bytes_per_page);
100 
101     /* Save the mapping table.  */
102     status = _lx_nand_flash_metadata_write(nand_flash, ((UCHAR*)nand_flash -> lx_nand_flash_block_mapping_table) +
103                                                 page_number * nand_flash -> lx_nand_flash_bytes_per_page,
104                                                 LX_NAND_PAGE_TYPE_BLOCK_MAPPING_TABLE | page_number);
105 
106     /* Return status.  */
107     return(status);
108 }
109 
110