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_find                           PORTABLE C      */
42 /*                                                           6.2.1       */
43 /*  AUTHOR                                                                */
44 /*                                                                        */
45 /*    Xiuwen Cai, Microsoft Corporation                                   */
46 /*                                                                        */
47 /*  DESCRIPTION                                                           */
48 /*                                                                        */
49 /*    This function attempts to find the mapped block for the logical     */
50 /*    sector in the mapping table.                                        */
51 /*                                                                        */
52 /*  INPUT                                                                 */
53 /*                                                                        */
54 /*    nand_flash                            NAND flash instance           */
55 /*    logical_sector                        Logical sector number         */
56 /*    superceded_check                      Check for page being          */
57 /*                                            superceded (can happen if   */
58 /*                                            on interruptions of page    */
59 /*                                            write)                      */
60 /*    block                                 Destination for block         */
61 /*    page                                  Destination for page          */
62 /*                                                                        */
63 /*  OUTPUT                                                                */
64 /*                                                                        */
65 /*    return status                                                       */
66 /*                                                                        */
67 /*  CALLS                                                                 */
68 /*                                                                        */
69 /*    None                                                                */
70 /*                                                                        */
71 /*  CALLED BY                                                             */
72 /*                                                                        */
73 /*    Internal LevelX                                                     */
74 /*                                                                        */
75 /*  RELEASE HISTORY                                                       */
76 /*                                                                        */
77 /*    DATE              NAME                      DESCRIPTION             */
78 /*                                                                        */
79 /*  03-08-2023     Xiuwen Cai               Initial Version 6.2.1        */
80 /*                                                                        */
81 /**************************************************************************/
_lx_nand_flash_block_find(LX_NAND_FLASH * nand_flash,ULONG logical_sector,ULONG * block,USHORT * block_status)82 UINT  _lx_nand_flash_block_find(LX_NAND_FLASH *nand_flash, ULONG logical_sector, ULONG *block, USHORT *block_status)
83 {
84 
85  UINT    block_mapping_index;
86  USHORT  block_number;
87 
88 
89     /* Get the mapping index from logic sector address.  */
90     block_mapping_index = logical_sector / nand_flash -> lx_nand_flash_pages_per_block;
91 
92     /* Check the address range.  */
93     if (block_mapping_index > nand_flash -> lx_nand_flash_block_mapping_table_size / sizeof(*nand_flash -> lx_nand_flash_block_mapping_table))
94     {
95 
96         /* Out of range, return an error. */
97         return(LX_ERROR);
98     }
99 
100     /* Get the block number from mapping table.  */
101     block_number = nand_flash -> lx_nand_flash_block_mapping_table[block_mapping_index];
102 
103     /* Check if it is mapped.  */
104     if (block_number != LX_NAND_BLOCK_UNMAPPED)
105     {
106 
107         /* Return the block status.  */
108         *block_status = nand_flash -> lx_nand_flash_block_status_table[block_number];
109     }
110 
111     /* Return the block number.  */
112     *block = (ULONG)block_number;
113 
114     /* Return successful completion.  */
115     return(LX_SUCCESS);
116 }
117 
118