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_sector_mapping_cache_invalidate       PORTABLE C      */
42 /*                                                           6.1.7        */
43 /*  AUTHOR                                                                */
44 /*                                                                        */
45 /*    William E. Lamie, Microsoft Corporation                             */
46 /*                                                                        */
47 /*  DESCRIPTION                                                           */
48 /*                                                                        */
49 /*    This function invalidates the sector's entry in the NOR flash       */
50 /*    cache.                                                              */
51 /*                                                                        */
52 /*  INPUT                                                                 */
53 /*                                                                        */
54 /*    nor_flash                             NOR flash instance            */
55 /*    logical_sector                        Logical sector                */
56 /*                                                                        */
57 /*  OUTPUT                                                                */
58 /*                                                                        */
59 /*    None                                                                */
60 /*                                                                        */
61 /*  CALLS                                                                 */
62 /*                                                                        */
63 /*    None                                                                */
64 /*                                                                        */
65 /*  CALLED BY                                                             */
66 /*                                                                        */
67 /*    Internal LevelX                                                     */
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 /*                                                                        */
79 /**************************************************************************/
_lx_nor_flash_sector_mapping_cache_invalidate(LX_NOR_FLASH * nor_flash,ULONG logical_sector)80 VOID  _lx_nor_flash_sector_mapping_cache_invalidate(LX_NOR_FLASH *nor_flash, ULONG logical_sector)
81 {
82 
83 ULONG                           i;
84 LX_NOR_SECTOR_MAPPING_CACHE_ENTRY  *sector_mapping_cache_entry_ptr;
85 
86 
87     /* Determine if the sector mapping cache is enabled.  */
88     if (nor_flash -> lx_nor_flash_sector_mapping_cache_enabled)
89     {
90 
91         /* Calculate the starting index of the sector mapping cache for this sector entry.  */
92         i =  (logical_sector & LX_NOR_SECTOR_MAPPING_CACHE_HASH_MASK) * LX_NOR_SECTOR_MAPPING_CACHE_DEPTH;
93 
94         /* Build a pointer to the cache entry.  */
95         sector_mapping_cache_entry_ptr =  &nor_flash -> lx_nor_flash_sector_mapping_cache[i];
96 
97         /* Determine if the sector is in the sector mapping cache - assuming the depth of the sector
98            mapping cache is LX_NOR_SECTOR_MAPPING_CACHE_DEPTH entries.  */
99         if ((sector_mapping_cache_entry_ptr -> lx_nor_sector_mapping_cache_logical_sector) == (logical_sector | LX_NOR_SECTOR_MAPPING_CACHE_ENTRY_VALID))
100         {
101 
102             /* Move all cache entries up and invalidate the last entry.  */
103             *(sector_mapping_cache_entry_ptr) =      *(sector_mapping_cache_entry_ptr + 1);
104             *(sector_mapping_cache_entry_ptr + 1) =  *(sector_mapping_cache_entry_ptr + 2);
105             *(sector_mapping_cache_entry_ptr + 2) =  *(sector_mapping_cache_entry_ptr + 3);
106 
107             /* Invalidate the last entry.  */
108             (sector_mapping_cache_entry_ptr + 3) -> lx_nor_sector_mapping_cache_logical_sector =   0;
109         }
110         else if (((sector_mapping_cache_entry_ptr + 1) -> lx_nor_sector_mapping_cache_logical_sector) == (logical_sector | LX_NOR_SECTOR_MAPPING_CACHE_ENTRY_VALID))
111         {
112 
113             /* Move all subsequent cache entries up and invalidate the last entry.  */
114             *(sector_mapping_cache_entry_ptr + 1) =  *(sector_mapping_cache_entry_ptr + 2);
115             *(sector_mapping_cache_entry_ptr + 2) =  *(sector_mapping_cache_entry_ptr + 3);
116 
117             /* Invalidate the last entry.  */
118             (sector_mapping_cache_entry_ptr + 3) -> lx_nor_sector_mapping_cache_logical_sector =   0;
119         }
120         else if (((sector_mapping_cache_entry_ptr + 2) -> lx_nor_sector_mapping_cache_logical_sector) == (logical_sector | LX_NOR_SECTOR_MAPPING_CACHE_ENTRY_VALID))
121         {
122 
123             /* Move all subsequent cache entries up and invalidate the last entry.  */
124             *(sector_mapping_cache_entry_ptr + 2) =  *(sector_mapping_cache_entry_ptr + 3);
125 
126             /* Invalidate the last entry.  */
127             (sector_mapping_cache_entry_ptr + 3) -> lx_nor_sector_mapping_cache_logical_sector =   0;
128         }
129         else if (((sector_mapping_cache_entry_ptr + 3) -> lx_nor_sector_mapping_cache_logical_sector) == (logical_sector | LX_NOR_SECTOR_MAPPING_CACHE_ENTRY_VALID))
130         {
131 
132             /* Simply invalidate the last entry.  */
133             (sector_mapping_cache_entry_ptr + 3) -> lx_nor_sector_mapping_cache_logical_sector =   0;
134         }
135     }
136 }
137 
138