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_write                          PORTABLE C      */
42 /*                                                           6.2.1       */
43 /*  AUTHOR                                                                */
44 /*                                                                        */
45 /*    William E. Lamie, Microsoft Corporation                             */
46 /*                                                                        */
47 /*  DESCRIPTION                                                           */
48 /*                                                                        */
49 /*    This function performs a write of the NOR flash memory.             */
50 /*                                                                        */
51 /*  INPUT                                                                 */
52 /*                                                                        */
53 /*    nor_flash                             NOR flash instance            */
54 /*    flash_address                         Address of NOR flash to write */
55 /*    source                                Source for the write          */
56 /*    words                                 Number of words to write      */
57 /*                                                                        */
58 /*  OUTPUT                                                                */
59 /*                                                                        */
60 /*    return status                                                       */
61 /*                                                                        */
62 /*  CALLS                                                                 */
63 /*                                                                        */
64 /*    (lx_nor_flash_driver_write)           Actual driver write           */
65 /*                                                                        */
66 /*  CALLED BY                                                             */
67 /*                                                                        */
68 /*    Application Code                                                    */
69 /*                                                                        */
70 /*  RELEASE HISTORY                                                       */
71 /*                                                                        */
72 /*    DATE              NAME                      DESCRIPTION             */
73 /*                                                                        */
74 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
75 /*  09-30-2020     William E. Lamie         Modified comment(s),          */
76 /*                                            resulting in version 6.1    */
77 /*  06-02-2021     Bhupendra Naphade        Modified comment(s),          */
78 /*                                            resulting in version 6.1.7  */
79 /*  03-08-2023     Xiuwen Cai               Modified comment(s),          */
80 /*                                            added new driver interface, */
81 /*                                            resulting in version 6.2.1 */
82 /*                                                                        */
83 /**************************************************************************/
_lx_nor_flash_driver_write(LX_NOR_FLASH * nor_flash,ULONG * flash_address,ULONG * source,ULONG words)84 UINT  _lx_nor_flash_driver_write(LX_NOR_FLASH *nor_flash, ULONG *flash_address, ULONG *source, ULONG words)
85 {
86 
87 #ifndef LX_NOR_DISABLE_EXTENDED_CACHE
88 
89 UINT    status;
90 UINT    i;
91 ULONG   *cache_entry_start;
92 ULONG   *cache_entry_end;
93 ULONG   cache_offset;
94 
95 
96     /* Is the request a whole sector or a partial sector.  */
97     if ((words == 1) && (nor_flash -> lx_nor_flash_extended_cache_entries))
98     {
99 
100         /* One word request, which implies that it is a NOR flash metadata write.  */
101 
102         /* Loop through the cache entries to see if there is a sector in cache.  */
103         for (i = 0; i < nor_flash -> lx_nor_flash_extended_cache_entries; i++)
104         {
105 
106             /* Search through the cache to see if there is a cache entry.  */
107 
108             /* Determine the cache entry addresses.  */
109             cache_entry_start =  nor_flash -> lx_nor_flash_extended_cache[i].lx_nor_flash_extended_cache_entry_sector_address;
110             cache_entry_end =    cache_entry_start + LX_NOR_SECTOR_SIZE;
111 
112             /* Determine if the flash address in in the cache entry.  */
113             if ((cache_entry_start) && (flash_address >= cache_entry_start) && (flash_address < cache_entry_end))
114             {
115 
116                 /* Yes, we found the entry.  */
117 
118                 /* Calculate the offset into the cache entry.  */
119                 cache_offset =  (ULONG)(flash_address - cache_entry_start);
120 
121                 /* Copy the word into the cache.  */
122                 *(nor_flash -> lx_nor_flash_extended_cache[i].lx_nor_flash_extended_cache_entry_sector_memory + cache_offset) =  *source;
123 
124                 /* Get out of the loop.  */
125                 break;
126             }
127         }
128     }
129 
130     /* In any case, call the actual driver write function.  */
131 #ifdef LX_NOR_ENABLE_CONTROL_BLOCK_FOR_DRIVER_INTERFACE
132     status =  (nor_flash -> lx_nor_flash_driver_write)(nor_flash, flash_address, source, words);
133 #else
134     status =  (nor_flash -> lx_nor_flash_driver_write)(flash_address, source, words);
135 #endif
136 
137     /* Return completion status.  */
138     return(status);
139 
140 #else
141 UINT    status;
142 
143 
144     /* Call the actual driver write function.  */
145 #ifdef LX_NOR_ENABLE_CONTROL_BLOCK_FOR_DRIVER_INTERFACE
146     status =  (nor_flash -> lx_nor_flash_driver_write)(nor_flash, flash_address, source, words);
147 #else
148     status =  (nor_flash -> lx_nor_flash_driver_write)(flash_address, source, words);
149 #endif
150 
151     /* Return completion status.  */
152     return(status);
153 #endif
154 }
155 
156