1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** LevelX Component                                                      */
17 /**                                                                       */
18 /**   NOR Flash                                                           */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define LX_SOURCE_CODE
24 
25 
26 /* Disable ThreadX error checking.  */
27 
28 #ifndef LX_DISABLE_ERROR_CHECKING
29 #define LX_DISABLE_ERROR_CHECKING
30 #endif
31 
32 
33 /* Include necessary system files.  */
34 
35 #include "lx_api.h"
36 
37 
38 /**************************************************************************/
39 /*                                                                        */
40 /*  FUNCTION                                               RELEASE        */
41 /*                                                                        */
42 /*    _lx_nor_flash_sector_release                        PORTABLE C      */
43 /*                                                           6.1.7        */
44 /*  AUTHOR                                                                */
45 /*                                                                        */
46 /*    William E. Lamie, Microsoft Corporation                             */
47 /*                                                                        */
48 /*  DESCRIPTION                                                           */
49 /*                                                                        */
50 /*    This function releases a logical sector from being managed in the   */
51 /*    NOR flash.                                                          */
52 /*                                                                        */
53 /*  INPUT                                                                 */
54 /*                                                                        */
55 /*    nor_flash                             NOR flash instance            */
56 /*    logical_sector                        Logical sector number         */
57 /*                                                                        */
58 /*  OUTPUT                                                                */
59 /*                                                                        */
60 /*    return status                                                       */
61 /*                                                                        */
62 /*  CALLS                                                                 */
63 /*                                                                        */
64 /*    _lx_nor_flash_driver_write            Driver flash sector write     */
65 /*    _lx_nor_flash_driver_read             Driver flash sector read      */
66 /*    _lx_nor_flash_block_reclaim           Reclaim one flash block       */
67 /*    _lx_nor_flash_sector_mapping_cache_invalidate                       */
68 /*                                          Invalidate cache entry        */
69 /*    _lx_nor_flash_logical_sector_find     Find logical sector           */
70 /*    _lx_nor_flash_system_error            Internal system error handler */
71 /*    tx_mutex_get                          Get thread protection         */
72 /*    tx_mutex_put                          Release thread protection     */
73 /*                                                                        */
74 /*  CALLED BY                                                             */
75 /*                                                                        */
76 /*    Application Code                                                    */
77 /*                                                                        */
78 /*  RELEASE HISTORY                                                       */
79 /*                                                                        */
80 /*    DATE              NAME                      DESCRIPTION             */
81 /*                                                                        */
82 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
83 /*  09-30-2020     William E. Lamie         Modified comment(s),          */
84 /*                                            resulting in version 6.1    */
85 /*  06-02-2021     Bhupendra Naphade        Modified comment(s),          */
86 /*                                            resulting in version 6.1.7  */
87 /*                                                                        */
88 /**************************************************************************/
_lx_nor_flash_sector_release(LX_NOR_FLASH * nor_flash,ULONG logical_sector)89 UINT  _lx_nor_flash_sector_release(LX_NOR_FLASH *nor_flash, ULONG logical_sector)
90 {
91 
92 UINT    status;
93 ULONG   *mapping_address;
94 ULONG   mapping_entry;
95 ULONG   *sector_address;
96 ULONG   i;
97 
98 
99 #ifdef LX_THREAD_SAFE_ENABLE
100 
101     /* Obtain the thread safe mutex.  */
102     tx_mutex_get(&nor_flash -> lx_nor_flash_mutex, TX_WAIT_FOREVER);
103 #endif
104 
105     /* Increment the number of read requests.  */
106     nor_flash -> lx_nor_flash_read_requests++;
107 
108     /* See if we can find the sector in the current mapping.  */
109     _lx_nor_flash_logical_sector_find(nor_flash, logical_sector, LX_FALSE, &mapping_address, &sector_address);
110 
111     /* Determine if the logical sector was found.  */
112     if (mapping_address)
113     {
114 
115         /* Yes, we were able to find the logical sector.  */
116 
117         /* Read in the old sector mapping.  */
118 #ifdef LX_DIRECT_READ
119 
120         /* Read the word directly.  */
121         mapping_entry =  *(mapping_address);
122 #else
123         status =  _lx_nor_flash_driver_read(nor_flash, mapping_address, &mapping_entry, 1);
124 
125         /* Check for an error from flash driver. Drivers should never return an error..  */
126         if (status)
127         {
128 
129             /* Call system error handler.  */
130             _lx_nor_flash_system_error(nor_flash, status);
131 
132 #ifdef LX_THREAD_SAFE_ENABLE
133 
134             /* Release the thread safe mutex.  */
135             tx_mutex_put(&nor_flash -> lx_nor_flash_mutex);
136 #endif
137 
138             /* Return status.  */
139             return(LX_ERROR);
140         }
141 #endif
142 
143         /* Mark this sector as invalid.  */
144         /* Now clear bits 31 and 30, which indicates this sector is now obsoleted.  */
145         mapping_entry =  mapping_entry & ~(((ULONG) LX_NOR_PHYSICAL_SECTOR_VALID) | ((ULONG) LX_NOR_PHYSICAL_SECTOR_SUPERCEDED));
146 
147         /* Write the value back to the flash to clear bits 31 & 30.  */
148         status =  _lx_nor_flash_driver_write(nor_flash, mapping_address, &mapping_entry, 1);
149 
150         /* Check for an error from flash driver. Drivers should never return an error..  */
151         if (status)
152         {
153 
154             /* Call system error handler.  */
155             _lx_nor_flash_system_error(nor_flash, status);
156 
157 #ifdef LX_THREAD_SAFE_ENABLE
158 
159             /* Release the thread safe mutex.  */
160             tx_mutex_put(&nor_flash -> lx_nor_flash_mutex);
161 #endif
162 
163             /* Return status.  */
164             return(LX_ERROR);
165         }
166 
167         /* Increment the number of obsolete physical sectors.  */
168         nor_flash -> lx_nor_flash_obsolete_physical_sectors++;
169 
170         /* Decrement the number of mapped physical sectors.  */
171         nor_flash -> lx_nor_flash_mapped_physical_sectors--;
172 
173         /* Ensure the sector mapping cache no longer has this sector.  */
174         _lx_nor_flash_sector_mapping_cache_invalidate(nor_flash, logical_sector);
175 
176         /* Determine if there are less than two block's worth of free sectors.  */
177         i =  0;
178         while (nor_flash -> lx_nor_flash_free_physical_sectors <= nor_flash -> lx_nor_flash_physical_sectors_per_block)
179         {
180 
181             /* Attempt to reclaim one physical block.  */
182             _lx_nor_flash_block_reclaim(nor_flash);
183 
184             /* Increment the block count.  */
185             i++;
186 
187             /* Have we exceeded the number of blocks in the system?  */
188             if (i >= nor_flash -> lx_nor_flash_total_blocks)
189             {
190 
191                 /* Yes, break out of the loop.  */
192                 break;
193             }
194         }
195 
196         /* Set the status to success.  */
197         status =  LX_SUCCESS;
198     }
199     else
200     {
201 
202         /* Could not find the logical sector.  */
203         status =  LX_SECTOR_NOT_FOUND;
204     }
205 
206 #ifdef LX_THREAD_SAFE_ENABLE
207 
208     /* Release the thread safe mutex.  */
209     tx_mutex_put(&nor_flash -> lx_nor_flash_mutex);
210 #endif
211 
212     /* Return status.  */
213     return(status);
214 }
215 
216