|
1706
|
1 //========================================================================== |
|
|
2 // |
|
|
3 // flash.h |
|
|
4 // |
|
|
5 // Flash programming - external interfaces |
|
|
6 // |
|
|
7 //========================================================================== |
|
|
8 //####ECOSGPLCOPYRIGHTBEGIN#### |
|
|
9 // ------------------------------------------- |
|
|
10 // This file is part of eCos, the Embedded Configurable Operating System. |
|
|
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc. |
|
|
12 // Copyright (C) 2003 Gary Thomas |
|
|
13 // copyright (C) 2004 Andrew Lunn |
|
|
14 // |
|
|
15 // eCos is free software; you can redistribute it and/or modify it under |
|
|
16 // the terms of the GNU General Public License as published by the Free |
|
|
17 // Software Foundation; either version 2 or (at your option) any later version. |
|
|
18 // |
|
|
19 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY |
|
|
20 // WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
|
21 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
|
22 // for more details. |
|
|
23 // |
|
|
24 // You should have received a copy of the GNU General Public License along |
|
|
25 // with eCos; if not, write to the Free Software Foundation, Inc., |
|
|
26 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
|
|
27 // |
|
|
28 // As a special exception, if other files instantiate templates or use macros |
|
|
29 // or inline functions from this file, or you compile this file and link it |
|
|
30 // with other works to produce a work based on this file, this file does not |
|
|
31 // by itself cause the resulting work to be covered by the GNU General Public |
|
|
32 // License. However the source code for this file must still be made available |
|
|
33 // in accordance with section (3) of the GNU General Public License. |
|
|
34 // |
|
|
35 // This exception does not invalidate any other reasons why a work based on |
|
|
36 // this file might be covered by the GNU General Public License. |
|
|
37 // |
|
|
38 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc. |
|
|
39 // at http://sources.redhat.com/ecos/ecos-license/ |
|
|
40 // ------------------------------------------- |
|
|
41 //####ECOSGPLCOPYRIGHTEND#### |
|
|
42 //========================================================================== |
|
|
43 //#####DESCRIPTIONBEGIN#### |
|
|
44 // |
|
|
45 // Author(s): gthomas |
|
|
46 // Contributors: gthomas, Andrew Lunn |
|
|
47 // Date: 2000-07-14 |
|
|
48 // Purpose: |
|
|
49 // Description: |
|
|
50 // |
|
|
51 //####DESCRIPTIONEND#### |
|
|
52 // |
|
|
53 //========================================================================== |
|
|
54 #ifndef _IO_FLASH_PRIV_H_ |
|
|
55 #define _IO_FLASH_PRIV_H_ |
|
|
56 |
|
|
57 // Forward reference of the device structure |
|
|
58 struct cyg_flash_dev; |
|
|
59 |
|
|
60 // Structure of pointers to functions in the device driver |
|
|
61 struct cyg_flash_dev_funs { |
|
|
62 int (*flash_init) (struct cyg_flash_dev *dev); |
|
|
63 size_t (*flash_query) (struct cyg_flash_dev *dev, void * data, |
|
|
64 const size_t len); |
|
|
65 int (*flash_erase_block) (struct cyg_flash_dev *dev, |
|
|
66 const cyg_flashaddr_t block_base); |
|
|
67 int (*flash_program) (struct cyg_flash_dev *dev, |
|
|
68 cyg_flashaddr_t base, |
|
|
69 const void* data, const size_t len); |
|
|
70 int (*flash_read) (struct cyg_flash_dev *dev, |
|
|
71 const cyg_flashaddr_t base, |
|
|
72 void* data, const size_t len); |
|
|
73 int (*flash_hwr_map_error) (struct cyg_flash_dev *dev, int err); |
|
|
74 int (*flash_block_lock) (struct cyg_flash_dev *dev, |
|
|
75 const cyg_flashaddr_t block_base); |
|
|
76 int (*flash_block_unlock) (struct cyg_flash_dev *dev, |
|
|
77 const cyg_flashaddr_t block_base); |
|
|
78 }; |
|
|
79 |
|
|
80 // Structure each device places in the HAL table |
|
|
81 struct cyg_flash_dev { |
|
|
82 struct cyg_flash_dev_funs *funs; // Function pointers |
|
|
83 cyg_flashaddr_t start; // First address |
|
|
84 cyg_flashaddr_t end; // Last address |
|
|
85 cyg_uint32 num_block_infos; // Number of entries |
|
|
86 cyg_block_info_t *block_info; // Info about one block size |
|
|
87 |
|
|
88 void *priv; // Devices private data |
|
|
89 void *config; // Configuration info |
|
|
90 |
|
|
91 // The following are only written to by the FLASH IO layer. |
|
|
92 cyg_flash_printf *pf; // Pointer to diagnostic printf |
|
|
93 bool init; // Device has been initialised |
|
|
94 #ifdef CYGPKG_KERNEL |
|
|
95 cyg_mutex_t mutex; // Mutex for thread safeness |
|
|
96 #endif |
|
|
97 struct cyg_flash_dev *next; // Pointer to next device |
|
|
98 } CYG_HAL_TABLE_TYPE; |
|
|
99 |
|
|
100 #define CYG_FLASH_FUNS(funs,init,query,erase,prog,read,map,lock,unlock)\ |
|
|
101 struct cyg_flash_dev_funs funs = \ |
|
|
102 { \ |
|
|
103 init, \ |
|
|
104 query, \ |
|
|
105 erase, \ |
|
|
106 prog, \ |
|
|
107 read, \ |
|
|
108 map, \ |
|
|
109 lock, \ |
|
|
110 unlock \ |
|
|
111 }; |
|
|
112 |
|
|
113 // We assume HAL tables are placed into RAM. |
|
|
114 #define CYG_FLASH_DRIVER(name, _funs, _config, _start, _priv_size) \ |
|
|
115 static char priv_ ## name [_priv_size]; \ |
|
|
116 struct cyg_flash_dev name CYG_HAL_TABLE_ENTRY(cyg_flashdev) = { \ |
|
|
117 .funs = _funs, \ |
|
|
118 .config = _config, \ |
|
|
119 .priv = priv_ ## name, \ |
|
|
120 .start = _start, \ |
|
|
121 }; |
|
|
122 |
|
|
123 #ifdef CYGSEM_IO_FLASH_LEGACY_DEVICE_API |
|
|
124 struct flash_info { |
|
|
125 int block_size; // Assuming fixed size "blocks" |
|
|
126 int blocks; // Number of blocks |
|
|
127 int buffer_size; // Size of write buffer (only defined for some devices) |
|
|
128 unsigned long block_mask; |
|
|
129 void *start, *end; // Address range |
|
|
130 int init; |
|
|
131 cyg_flash_printf *pf; |
|
|
132 }; |
|
|
133 |
|
|
134 externC struct flash_info flash_info; |
|
|
135 externC int flash_hwr_init(void); |
|
|
136 externC int flash_hwr_map_error(int err); |
|
|
137 externC void flash_dev_query(void *data); |
|
|
138 #endif // CYGSEM_IO_FLASH_LEGACY_DEVICE_API |
|
|
139 // |
|
|
140 // Some FLASH devices may require additional support, e.g. to turn on |
|
|
141 // appropriate voltage drivers, before any operation. |
|
|
142 // |
|
|
143 #ifdef CYGIMP_FLASH_ENABLE |
|
|
144 #define FLASH_Enable CYGIMP_FLASH_ENABLE |
|
|
145 extern void CYGIMP_FLASH_ENABLE(void *, void *); |
|
|
146 #else |
|
|
147 #define FLASH_Enable(_start_, _end_) |
|
|
148 #endif |
|
|
149 #ifdef CYGIMP_FLASH_DISABLE |
|
|
150 #define FLASH_Disable CYGIMP_FLASH_DISABLE |
|
|
151 extern void CYGIMP_FLASH_DISABLE(void *, void *); |
|
|
152 #else |
|
|
153 #define FLASH_Disable(_start_, _end_) |
|
|
154 #endif |
|
|
155 |
|
|
156 // |
|
|
157 // Some platforms have a DIP switch or jumper that tells the software that |
|
|
158 // the flash is write protected. |
|
|
159 // |
|
|
160 #ifdef CYGSEM_IO_FLASH_SOFT_WRITE_PROTECT |
|
|
161 externC cyg_bool plf_flash_query_soft_wp(void *addr, int len); |
|
|
162 #endif |
|
|
163 |
|
|
164 //--------------------------------------------------------------------------- |
|
|
165 // Execution of flash code must be done inside a |
|
|
166 // HAL_FLASH_CACHES_OFF/HAL_FLASH_CACHES_ON region - disabling the |
|
|
167 // cache on unified cache systems is necessary to prevent burst access |
|
|
168 // to the flash area being programmed. With Harvard style caches, only |
|
|
169 // the data cache needs to be disabled, but the instruction cache is |
|
|
170 // disabled for consistency. |
|
|
171 |
|
|
172 // Targets may provide alternative implementations for these macros in |
|
|
173 // the hal_cache.h (or var/plf) files. |
|
|
174 |
|
|
175 // The first part below is a generic, optimal implementation. The |
|
|
176 // second part is the old implementation that has been tested to work |
|
|
177 // on some targets - but it is not be suitable for targets that would |
|
|
178 // do burst access to the flash (it does not disable the data cache). |
|
|
179 |
|
|
180 // Both implementations must be called with interrupts disabled. |
|
|
181 |
|
|
182 // NOTE: Do _not_ change any of the below macros without checking that |
|
|
183 // the changed code still works on _all_ platforms that rely on these |
|
|
184 // macros. There is no such thing as logical and correct when dealing |
|
|
185 // with different cache and IO models, so _do not_ mess with this code |
|
|
186 // unless you test it properly afterwards. |
|
|
187 |
|
|
188 #ifndef HAL_FLASH_CACHES_OFF |
|
|
189 |
|
|
190 // Some drivers have only been tested with the old macros below. |
|
|
191 #ifndef HAL_FLASH_CACHES_OLD_MACROS |
|
|
192 |
|
|
193 #ifdef HAL_CACHE_UNIFIED |
|
|
194 |
|
|
195 // Note: the ucache code has not been tested yet on any target. |
|
|
196 #define HAL_FLASH_CACHES_OFF(_d_, _i_) \ |
|
|
197 CYG_MACRO_START \ |
|
|
198 _i_ = 0; /* avoids warning */ \ |
|
|
199 HAL_UCACHE_IS_ENABLED(_d_); \ |
|
|
200 HAL_UCACHE_SYNC(); \ |
|
|
201 HAL_UCACHE_INVALIDATE_ALL(); \ |
|
|
202 HAL_UCACHE_DISABLE(); \ |
|
|
203 CYG_MACRO_END |
|
|
204 |
|
|
205 #define HAL_FLASH_CACHES_ON(_d_, _i_) \ |
|
|
206 CYG_MACRO_START \ |
|
|
207 if (_d_) HAL_UCACHE_ENABLE(); \ |
|
|
208 CYG_MACRO_END |
|
|
209 |
|
|
210 #else // HAL_CACHE_UNIFIED |
|
|
211 |
|
|
212 #define HAL_FLASH_CACHES_OFF(_d_, _i_) \ |
|
|
213 CYG_MACRO_START \ |
|
|
214 _i_ = 0; /* avoids warning */ \ |
|
|
215 HAL_DCACHE_IS_ENABLED(_d_); \ |
|
|
216 HAL_DCACHE_SYNC(); \ |
|
|
217 HAL_DCACHE_INVALIDATE_ALL(); \ |
|
|
218 HAL_DCACHE_DISABLE(); \ |
|
|
219 HAL_ICACHE_INVALIDATE_ALL(); \ |
|
|
220 CYG_MACRO_END |
|
|
221 |
|
|
222 #define HAL_FLASH_CACHES_ON(_d_, _i_) \ |
|
|
223 CYG_MACRO_START \ |
|
|
224 if (_d_) HAL_DCACHE_ENABLE(); \ |
|
|
225 CYG_MACRO_END |
|
|
226 |
|
|
227 #endif // HAL_CACHE_UNIFIED |
|
|
228 |
|
|
229 #else // HAL_FLASH_CACHES_OLD_MACROS |
|
|
230 |
|
|
231 // Note: This implementation is broken as it will always enable the i-cache |
|
|
232 // even if it was not enabled before. It also doesn't work if the |
|
|
233 // target uses burst access to flash since the d-cache is left enabled. |
|
|
234 // However, this does not mean you can change this code! Leave it as |
|
|
235 // is - if you want a different implementation, provide it in the |
|
|
236 // arch/var/platform cache header file. |
|
|
237 |
|
|
238 #define HAL_FLASH_CACHES_OFF(_d_, _i_) \ |
|
|
239 _d_ = 0; /* avoids warning */ \ |
|
|
240 _i_ = 0; /* avoids warning */ \ |
|
|
241 HAL_DCACHE_SYNC(); \ |
|
|
242 HAL_ICACHE_DISABLE(); |
|
|
243 |
|
|
244 #define HAL_FLASH_CACHES_ON(_d_, _i_) \ |
|
|
245 HAL_ICACHE_ENABLE(); |
|
|
246 |
|
|
247 #endif // HAL_FLASH_CACHES_OLD_MACROS |
|
|
248 |
|
|
249 #endif // HAL_FLASH_CACHES_OFF |
|
|
250 |
|
|
251 #endif // _IO_FLASH_PRIV_H_ |