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_free_block_list_add                  PORTABLE C      */
42 /*                                                           6.2.1       */
43 /*  AUTHOR                                                                */
44 /*                                                                        */
45 /*    Xiuwen Cai, Microsoft Corporation                                   */
46 /*                                                                        */
47 /*  DESCRIPTION                                                           */
48 /*                                                                        */
49 /*    This function adds a block to free block list.                      */
50 /*                                                                        */
51 /*  INPUT                                                                 */
52 /*                                                                        */
53 /*    nand_flash                            NAND flash instance           */
54 /*    block                                 Block number                  */
55 /*                                                                        */
56 /*  OUTPUT                                                                */
57 /*                                                                        */
58 /*    return status                                                       */
59 /*                                                                        */
60 /*  CALLS                                                                 */
61 /*                                                                        */
62 /*    None                                                                */
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_free_block_list_add(LX_NAND_FLASH * nand_flash,ULONG block)75 UINT  _lx_nand_flash_free_block_list_add(LX_NAND_FLASH* nand_flash, ULONG block)
76 {
77 
78 ULONG insert_position;
79 INT search_position;
80 UCHAR new_block_erase_count;
81 
82 
83     /* Get insert position for the free block list.  */
84     insert_position = nand_flash -> lx_nand_flash_free_block_list_tail;
85 
86     /* Check if the list if full.  */
87     if (insert_position > nand_flash -> lx_nand_flash_mapped_block_list_head)
88     {
89 
90         /* Return an error.  */
91         return(LX_ERROR);
92     }
93 
94     /* Get the erase count.  */
95     new_block_erase_count = nand_flash -> lx_nand_flash_erase_count_table[block];
96 
97     /* Add one block to the free list.  */
98     nand_flash -> lx_nand_flash_free_block_list_tail++;
99 
100     /* Initialize the search pointer.  */
101     search_position = (INT)insert_position - 1;
102 
103     /* Loop to search the insert position by block erase count.  */
104     while ((search_position >= 0) &&
105            (nand_flash -> lx_nand_flash_erase_count_table[nand_flash -> lx_nand_flash_block_list[search_position]] < new_block_erase_count))
106     {
107 
108         /* Move the item in the list.  */
109         nand_flash -> lx_nand_flash_block_list[insert_position] = nand_flash -> lx_nand_flash_block_list[search_position];
110         search_position--;
111         insert_position--;
112     }
113 
114     /* Insert the new block to the list.  */
115     nand_flash -> lx_nand_flash_block_list[insert_position] = (USHORT)block;
116 
117     /* Return successful completion.  */
118     return(LX_SUCCESS);
119 }
120 
121