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 /**   NOR 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_nor_flash_driver_block_erase                    PORTABLE C      */
42 /*                                                           6.2.1       */
43 /*  AUTHOR                                                                */
44 /*                                                                        */
45 /*    William E. Lamie, Microsoft Corporation                             */
46 /*                                                                        */
47 /*  DESCRIPTION                                                           */
48 /*                                                                        */
49 /*    This function performs a NOR flash block erase.                     */
50 /*                                                                        */
51 /*  INPUT                                                                 */
52 /*                                                                        */
53 /*    nor_flash                             NOR flash instance            */
54 /*    block                                 Block number to erase         */
55 /*    erase_count                           Erase count for this block    */
56 /*                                                                        */
57 /*  OUTPUT                                                                */
58 /*                                                                        */
59 /*    return status                                                       */
60 /*                                                                        */
61 /*  CALLS                                                                 */
62 /*                                                                        */
63 /*    (lx_nor_flash_driver_block_erase)     Actual driver block erase     */
64 /*                                                                        */
65 /*  CALLED BY                                                             */
66 /*                                                                        */
67 /*    Application Code                                                    */
68 /*                                                                        */
69 /*  RELEASE HISTORY                                                       */
70 /*                                                                        */
71 /*    DATE              NAME                      DESCRIPTION             */
72 /*                                                                        */
73 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
74 /*  09-30-2020     William E. Lamie         Modified comment(s),          */
75 /*                                            resulting in version 6.1    */
76 /*  06-02-2021     Bhupendra Naphade        Modified comment(s),          */
77 /*                                            resulting in version 6.1.7  */
78 /*  03-08-2023     Xiuwen Cai               Modified comment(s),          */
79 /*                                            added new driver interface, */
80 /*                                            resulting in version 6.2.1 */
81 /*                                                                        */
82 /**************************************************************************/
_lx_nor_flash_driver_block_erase(LX_NOR_FLASH * nor_flash,ULONG block,ULONG erase_count)83 UINT  _lx_nor_flash_driver_block_erase(LX_NOR_FLASH *nor_flash, ULONG block, ULONG erase_count)
84 {
85 
86 UINT    status;
87 
88 #ifndef LX_NOR_DISABLE_EXTENDED_CACHE
89 
90 UINT    i;
91 ULONG   *block_start_address;
92 ULONG   *block_end_address;
93 ULONG   *cache_entry_start;
94 ULONG   *cache_entry_end;
95 
96 
97     /* Calculate the block starting address.  */
98     block_start_address =  nor_flash -> lx_nor_flash_base_address + (block * nor_flash -> lx_nor_flash_words_per_block);
99     block_end_address =    block_start_address + nor_flash -> lx_nor_flash_words_per_block;
100 
101     /* Loop through the cache entries to see if there is a sector in cache.  */
102     for (i = 0; i < nor_flash -> lx_nor_flash_extended_cache_entries; i++)
103     {
104 
105         /* Search through the cache to see if this cache entry needs to be invalidated.  */
106 
107         /* Determine the cache entry addresses.  */
108         cache_entry_start =  nor_flash -> lx_nor_flash_extended_cache[i].lx_nor_flash_extended_cache_entry_sector_address;
109         cache_entry_end =    cache_entry_start + LX_NOR_SECTOR_SIZE;
110 
111         /* Determine if the flash address in in the cache entry.  */
112         if ((cache_entry_start) && (block_start_address <= cache_entry_start) && (block_end_address > cache_entry_end))
113         {
114 
115             /* Yes, this cache entry is in the block to be erased so invalidate it.  */
116             nor_flash -> lx_nor_flash_extended_cache[i].lx_nor_flash_extended_cache_entry_sector_address =  LX_NULL;
117             nor_flash -> lx_nor_flash_extended_cache[i].lx_nor_flash_extended_cache_entry_access_count =    0;
118         }
119     }
120 #endif
121 
122     /* Call the actual driver block erase function.  */
123 #ifdef LX_NOR_ENABLE_CONTROL_BLOCK_FOR_DRIVER_INTERFACE
124     status =  (nor_flash -> lx_nor_flash_driver_block_erase)(nor_flash, block, erase_count);
125 #else
126     status =  (nor_flash -> lx_nor_flash_driver_block_erase)(block, erase_count);
127 #endif
128 
129     /* Return completion status.  */
130     return(status);
131 }
132 
133 
134