1 |
27 |
unneback |
#ifndef CYGONCE_HAL_CACHE_H
|
2 |
|
|
#define CYGONCE_HAL_CACHE_H
|
3 |
|
|
|
4 |
|
|
//=============================================================================
|
5 |
|
|
//
|
6 |
|
|
// hal_cache.h
|
7 |
|
|
//
|
8 |
|
|
// HAL cache control API
|
9 |
|
|
//
|
10 |
|
|
//=============================================================================
|
11 |
|
|
//####ECOSGPLCOPYRIGHTBEGIN####
|
12 |
|
|
// -------------------------------------------
|
13 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
14 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
|
15 |
|
|
//
|
16 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
17 |
|
|
// the terms of the GNU General Public License as published by the Free
|
18 |
|
|
// Software Foundation; either version 2 or (at your option) any later version.
|
19 |
|
|
//
|
20 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
|
21 |
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
22 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
23 |
|
|
// for more details.
|
24 |
|
|
//
|
25 |
|
|
// You should have received a copy of the GNU General Public License along
|
26 |
|
|
// with eCos; if not, write to the Free Software Foundation, Inc.,
|
27 |
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
28 |
|
|
//
|
29 |
|
|
// As a special exception, if other files instantiate templates or use macros
|
30 |
|
|
// or inline functions from this file, or you compile this file and link it
|
31 |
|
|
// with other works to produce a work based on this file, this file does not
|
32 |
|
|
// by itself cause the resulting work to be covered by the GNU General Public
|
33 |
|
|
// License. However the source code for this file must still be made available
|
34 |
|
|
// in accordance with section (3) of the GNU General Public License.
|
35 |
|
|
//
|
36 |
|
|
// This exception does not invalidate any other reasons why a work based on
|
37 |
|
|
// this file might be covered by the GNU General Public License.
|
38 |
|
|
//
|
39 |
|
|
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
|
40 |
|
|
// at http://sources.redhat.com/ecos/ecos-license/
|
41 |
|
|
// -------------------------------------------
|
42 |
|
|
//####ECOSGPLCOPYRIGHTEND####
|
43 |
|
|
//=============================================================================
|
44 |
|
|
//#####DESCRIPTIONBEGIN####
|
45 |
|
|
//
|
46 |
|
|
// Author(s): hmt
|
47 |
|
|
// Contributors:hmt
|
48 |
|
|
// Date: 1999-07-05
|
49 |
|
|
// Purpose: Cache control API
|
50 |
|
|
// Description: The macros defined here provide the HAL APIs for handling
|
51 |
|
|
// cache control operations.
|
52 |
|
|
// Usage:
|
53 |
|
|
// #include <cyg/hal/hal_cache.h>
|
54 |
|
|
// ...
|
55 |
|
|
//
|
56 |
|
|
//
|
57 |
|
|
//####DESCRIPTIONEND####
|
58 |
|
|
//
|
59 |
|
|
//=============================================================================
|
60 |
|
|
|
61 |
|
|
#include <cyg/infra/cyg_type.h>
|
62 |
|
|
//#include <cyg/hal/hal_mmu.h>
|
63 |
|
|
|
64 |
|
|
//-----------------------------------------------------------------------------
|
65 |
|
|
// FIXME: This definition forces the IO flash driver to use a
|
66 |
|
|
// known-good procedure for fiddling flash before calling flash device
|
67 |
|
|
// driver functions. The procedure breaks on other platform/driver
|
68 |
|
|
// combinations though so is depricated. Hence this definition.
|
69 |
|
|
//
|
70 |
|
|
// If you work on this target, please try to remove this definition
|
71 |
|
|
// and verify that the flash driver still works (both from RAM and
|
72 |
|
|
// flash). If it does, remove the definition and this comment for good
|
73 |
|
|
// [and the old macro definition if this happens to be the last client
|
74 |
|
|
// of that code].
|
75 |
|
|
#define HAL_FLASH_CACHES_OLD_MACROS
|
76 |
|
|
|
77 |
|
|
//-----------------------------------------------------------------------------
|
78 |
|
|
// Cache dimensions
|
79 |
|
|
|
80 |
|
|
#define HAL_DCACHE_SIZE 0x4000 // Size of data cache in bytes
|
81 |
|
|
#define HAL_DCACHE_LINE_SIZE 32 // Size of a data cache line
|
82 |
|
|
#define HAL_DCACHE_WAYS 32 // Associativity of the cache
|
83 |
|
|
#define HAL_DCACHE_SETS (HAL_UCACHE_SIZE/(HAL_UCACHE_LINE_SIZE*HAL_UCACHE_WAYS))
|
84 |
|
|
|
85 |
|
|
#define HAL_ICACHE_SIZE 0x4000 // Size of instruction cache in bytes
|
86 |
|
|
#define HAL_ICACHE_LINE_SIZE 32 // Size of ins cache line
|
87 |
|
|
#define HAL_ICACHE_WAYS 32 // Associativity of the cache
|
88 |
|
|
#define HAL_ICACHE_SETS (HAL_UCACHE_SIZE/(HAL_UCACHE_LINE_SIZE*HAL_UCACHE_WAYS))
|
89 |
|
|
|
90 |
|
|
//-----------------------------------------------------------------------------
|
91 |
|
|
// Global control of data cache
|
92 |
|
|
|
93 |
|
|
// Enable the data cache
|
94 |
|
|
#define HAL_DCACHE_ENABLE() \
|
95 |
|
|
CYG_MACRO_START \
|
96 |
|
|
/* SA-110 manual states that the control reg is read-write */ \
|
97 |
|
|
asm volatile ( \
|
98 |
|
|
"mrc p15,0,r1,c1,c0,0;" \
|
99 |
|
|
"orr r1,r1,#0x000F;" /* ensure MM is enabled */ \
|
100 |
|
|
"mcr p15,0,r1,c1,c0,0" \
|
101 |
|
|
: \
|
102 |
|
|
: \
|
103 |
|
|
: "r1" /* Clobber list */ \
|
104 |
|
|
); \
|
105 |
|
|
CYG_MACRO_END
|
106 |
|
|
|
107 |
|
|
// Disable the data cache (and invalidate it, required semanitcs)
|
108 |
|
|
#define HAL_DCACHE_DISABLE() \
|
109 |
|
|
CYG_MACRO_START \
|
110 |
|
|
/* SA-110 manual states that the control reg is read-write */ \
|
111 |
|
|
asm volatile ( \
|
112 |
|
|
"mrc p15,0,r1,c1,c0,0;" \
|
113 |
|
|
"bic r1,r1,#0x000C;" /* but leave MM alone */ \
|
114 |
|
|
"mcr p15,0,r1,c1,c0,0;" \
|
115 |
|
|
"mov r1,#0;" \
|
116 |
|
|
"mcr p15,0,r1,c7,c6,0" /* clear data cache */ \
|
117 |
|
|
: \
|
118 |
|
|
: \
|
119 |
|
|
: "r1" /* Clobber list */ \
|
120 |
|
|
); \
|
121 |
|
|
CYG_MACRO_END
|
122 |
|
|
|
123 |
|
|
// Invalidate the entire cache (and both TLBs, just in case)
|
124 |
|
|
#define HAL_DCACHE_INVALIDATE_ALL() \
|
125 |
|
|
CYG_MACRO_START \
|
126 |
|
|
/* this macro can discard dirty cache lines. */ \
|
127 |
|
|
asm volatile ( \
|
128 |
|
|
"mov r1,#0;" \
|
129 |
|
|
"mcr p15,0,r1,c7,c6,0;" /* clear data cache */ \
|
130 |
|
|
"mcr p15,0,r1,c8,c7,0;" /* flush I+D TLBs */ \
|
131 |
|
|
: \
|
132 |
|
|
: \
|
133 |
|
|
: "r1","memory" /* Clobber list */ \
|
134 |
|
|
); \
|
135 |
|
|
CYG_MACRO_END
|
136 |
|
|
|
137 |
|
|
|
138 |
|
|
// Synchronize the contents of the cache with memory.
|
139 |
|
|
#define HAL_DCACHE_SYNC() \
|
140 |
|
|
CYG_MACRO_START \
|
141 |
|
|
/* This is slightly naff in that the only way to force a dirty */ \
|
142 |
|
|
/* line out is by loading other data into its slot, so */ \
|
143 |
|
|
/* invalidating that slot. */ \
|
144 |
|
|
asm volatile ( \
|
145 |
|
|
"mov r0, #0x50000000;" /* 21285~s cache flush region */ \
|
146 |
|
|
"add r1, r0, #0x4000;" /* 16MB of fast don~t-care amnesia */ \
|
147 |
|
|
"667: " /* We read in 16kB of it */ \
|
148 |
|
|
"ldr r2, [r0], #32;" \
|
149 |
|
|
"teq r1, r0;" \
|
150 |
|
|
"bne 667b;" \
|
151 |
|
|
"mov r0,#0;" \
|
152 |
|
|
"mcr p15,0,r0,c7,c6,0;" /* clear data cache */ \
|
153 |
|
|
"mcr p15,0,r0,c7,c10,4;" /* and drain the write buffer */ \
|
154 |
|
|
: \
|
155 |
|
|
: \
|
156 |
|
|
: "r0","r1","r2" /* Clobber list */ \
|
157 |
|
|
); \
|
158 |
|
|
CYG_MACRO_END
|
159 |
|
|
|
160 |
|
|
// Query the state of the data cache
|
161 |
|
|
#define HAL_DCACHE_IS_ENABLED(_state_) \
|
162 |
|
|
CYG_MACRO_START \
|
163 |
|
|
/* SA-110 manual states clearly that the control reg is readable */ \
|
164 |
|
|
register int reg; \
|
165 |
|
|
asm volatile ("mrc p15,0,%0,c1,c0,0" \
|
166 |
|
|
: "=r"(reg) \
|
167 |
|
|
: \
|
168 |
|
|
/*:*/ \
|
169 |
|
|
); \
|
170 |
|
|
(_state_) = (0 != (4 & reg)); /* Bit 2 is DCache enable */ \
|
171 |
|
|
CYG_MACRO_END
|
172 |
|
|
|
173 |
|
|
// Set the data cache refill burst size
|
174 |
|
|
//#define HAL_DCACHE_BURST_SIZE(_size_)
|
175 |
|
|
|
176 |
|
|
// Set the data cache write mode
|
177 |
|
|
//#define HAL_DCACHE_WRITE_MODE( _mode_ )
|
178 |
|
|
|
179 |
|
|
#define HAL_DCACHE_WRITETHRU_MODE 0
|
180 |
|
|
#define HAL_DCACHE_WRITEBACK_MODE 1
|
181 |
|
|
|
182 |
|
|
// Get the current writeback mode - or only writeback mode if fixed
|
183 |
|
|
#define HAL_DCACHE_QUERY_WRITE_MODE( _mode_ ) CYG_MACRO_START \
|
184 |
|
|
_mode_ = HAL_DCACHE_WRITEBACK_MODE; \
|
185 |
|
|
CYG_MACRO_END
|
186 |
|
|
|
187 |
|
|
// Load the contents of the given address range into the data cache
|
188 |
|
|
// and then lock the cache so that it stays there.
|
189 |
|
|
//#define HAL_DCACHE_LOCK(_base_, _size_)
|
190 |
|
|
|
191 |
|
|
// Undo a previous lock operation
|
192 |
|
|
//#define HAL_DCACHE_UNLOCK(_base_, _size_)
|
193 |
|
|
|
194 |
|
|
// Unlock entire cache
|
195 |
|
|
//#define HAL_DCACHE_UNLOCK_ALL()
|
196 |
|
|
|
197 |
|
|
//-----------------------------------------------------------------------------
|
198 |
|
|
// Data cache line control
|
199 |
|
|
|
200 |
|
|
// Allocate cache lines for the given address range without reading its
|
201 |
|
|
// contents from memory.
|
202 |
|
|
//#define HAL_DCACHE_ALLOCATE( _base_ , _size_ )
|
203 |
|
|
|
204 |
|
|
// Write dirty cache lines to memory and invalidate the cache entries
|
205 |
|
|
// for the given address range.
|
206 |
|
|
#define HAL_DCACHE_FLUSH( _base_ , _size_ ) \
|
207 |
|
|
CYG_MACRO_START \
|
208 |
|
|
HAL_DCACHE_STORE( _base_ , _size_ ); \
|
209 |
|
|
HAL_DCACHE_INVALIDATE( _base_ , _size_ ); \
|
210 |
|
|
CYG_MACRO_END
|
211 |
|
|
|
212 |
|
|
// Invalidate cache lines in the given range without writing to memory.
|
213 |
|
|
#define HAL_DCACHE_INVALIDATE( _base_ , _size_ ) \
|
214 |
|
|
CYG_MACRO_START \
|
215 |
|
|
register int addr, enda; \
|
216 |
|
|
for ( addr = (~(HAL_DCACHE_LINE_SIZE - 1)) & (int)(_base_), \
|
217 |
|
|
enda = (int)(_base_) + (_size_); \
|
218 |
|
|
addr < enda ; \
|
219 |
|
|
addr += HAL_DCACHE_LINE_SIZE ) \
|
220 |
|
|
{ \
|
221 |
|
|
asm volatile ( \
|
222 |
|
|
"mcr p15,0,%0,c7,c6,1;" /* flush entry away */ \
|
223 |
|
|
: \
|
224 |
|
|
: "r"(addr) \
|
225 |
|
|
: "memory" \
|
226 |
|
|
); \
|
227 |
|
|
} \
|
228 |
|
|
CYG_MACRO_END
|
229 |
|
|
|
230 |
|
|
// Write dirty cache lines to memory for the given address range.
|
231 |
|
|
#define HAL_DCACHE_STORE( _base_ , _size_ ) \
|
232 |
|
|
CYG_MACRO_START \
|
233 |
|
|
register int addr, enda; \
|
234 |
|
|
for ( addr = (~(HAL_DCACHE_LINE_SIZE - 1)) & (int)(_base_), \
|
235 |
|
|
enda = (int)(_base_) + (_size_); \
|
236 |
|
|
addr < enda ; \
|
237 |
|
|
addr += HAL_DCACHE_LINE_SIZE ) \
|
238 |
|
|
{ \
|
239 |
|
|
asm volatile ("mcr p15,0,%0,c7,c10,1;" /* push entry to RAM */ \
|
240 |
|
|
: \
|
241 |
|
|
: "r"(addr) \
|
242 |
|
|
: "memory" \
|
243 |
|
|
); \
|
244 |
|
|
} \
|
245 |
|
|
/* and also drain the write buffer */ \
|
246 |
|
|
asm volatile ( \
|
247 |
|
|
"mov r1,#0;" \
|
248 |
|
|
"mcr p15,0,r1,c7,c10,4;" \
|
249 |
|
|
: \
|
250 |
|
|
: \
|
251 |
|
|
: "r1", "memory" ); \
|
252 |
|
|
CYG_MACRO_END
|
253 |
|
|
|
254 |
|
|
// Preread the given range into the cache with the intention of reading
|
255 |
|
|
// from it later.
|
256 |
|
|
//#define HAL_DCACHE_READ_HINT( _base_ , _size_ )
|
257 |
|
|
|
258 |
|
|
// Preread the given range into the cache with the intention of writing
|
259 |
|
|
// to it later.
|
260 |
|
|
//#define HAL_DCACHE_WRITE_HINT( _base_ , _size_ )
|
261 |
|
|
|
262 |
|
|
// Allocate and zero the cache lines associated with the given range.
|
263 |
|
|
//#define HAL_DCACHE_ZERO( _base_ , _size_ )
|
264 |
|
|
|
265 |
|
|
//-----------------------------------------------------------------------------
|
266 |
|
|
// Global control of Instruction cache
|
267 |
|
|
|
268 |
|
|
// Enable the instruction cache
|
269 |
|
|
#define HAL_ICACHE_ENABLE() \
|
270 |
|
|
CYG_MACRO_START \
|
271 |
|
|
/* SA-110 manual states that the control reg is read-write */ \
|
272 |
|
|
asm volatile ( \
|
273 |
|
|
"mrc p15,0,r1,c1,c0,0;" \
|
274 |
|
|
"orr r1,r1,#0x0003;" /* ensure MM is enabled */ \
|
275 |
|
|
"orr r1,r1,#0x1000;" /* enable ICache */ \
|
276 |
|
|
"mcr p15,0,r1,c1,c0,0" \
|
277 |
|
|
: \
|
278 |
|
|
: \
|
279 |
|
|
: "r1" /* Clobber list */ \
|
280 |
|
|
); \
|
281 |
|
|
CYG_MACRO_END
|
282 |
|
|
|
283 |
|
|
// Disable the instruction cache (and invalidate it, required semanitcs)
|
284 |
|
|
#define HAL_ICACHE_DISABLE() \
|
285 |
|
|
CYG_MACRO_START \
|
286 |
|
|
/* SA-110 manual states that the control reg is read-write */ \
|
287 |
|
|
asm volatile ( \
|
288 |
|
|
"mrc p15,0,r1,c1,c0,0;" \
|
289 |
|
|
"bic r1,r1,#0x1000;" /* but leave MM alone */ \
|
290 |
|
|
"mcr p15,0,r1,c1,c0,0;" \
|
291 |
|
|
"mov r1, #0;" \
|
292 |
|
|
"mcr p15,0,r1,c7,c5,0;" /* clear instruction cache */ \
|
293 |
|
|
"nop;" /* next few instructions may be via cache */ \
|
294 |
|
|
"nop;" \
|
295 |
|
|
"nop;" \
|
296 |
|
|
"nop;" \
|
297 |
|
|
"nop;" \
|
298 |
|
|
"nop" \
|
299 |
|
|
: \
|
300 |
|
|
: \
|
301 |
|
|
: "r1" /* Clobber list */ \
|
302 |
|
|
); \
|
303 |
|
|
CYG_MACRO_END
|
304 |
|
|
|
305 |
|
|
// Invalidate the entire cache
|
306 |
|
|
#define HAL_ICACHE_INVALIDATE_ALL() \
|
307 |
|
|
CYG_MACRO_START \
|
308 |
|
|
/* this macro can discard dirty cache lines (N/A for ICache) */ \
|
309 |
|
|
asm volatile ( \
|
310 |
|
|
"mov r1, #0;" \
|
311 |
|
|
"mcr p15,0,r1,c7,c5,0;" /* clear instruction cache */ \
|
312 |
|
|
"mcr p15,0,r1,c8,c5,0;" /* flush I TLB only */ \
|
313 |
|
|
"nop;" /* next few instructions may be via cache */ \
|
314 |
|
|
"nop;" \
|
315 |
|
|
"nop;" \
|
316 |
|
|
"nop;" \
|
317 |
|
|
"nop;" \
|
318 |
|
|
"nop" \
|
319 |
|
|
: \
|
320 |
|
|
: \
|
321 |
|
|
: "r1" /* Clobber list */ \
|
322 |
|
|
); \
|
323 |
|
|
CYG_MACRO_END
|
324 |
|
|
|
325 |
|
|
|
326 |
|
|
// Synchronize the contents of the cache with memory.
|
327 |
|
|
// (which includes flushing out pending writes)
|
328 |
|
|
#define HAL_ICACHE_SYNC() \
|
329 |
|
|
CYG_MACRO_START \
|
330 |
|
|
HAL_DCACHE_SYNC(); /* ensure data gets to RAM */ \
|
331 |
|
|
HAL_ICACHE_INVALIDATE_ALL(); /* forget all we know */ \
|
332 |
|
|
CYG_MACRO_END
|
333 |
|
|
|
334 |
|
|
// Query the state of the instruction cache
|
335 |
|
|
#define HAL_ICACHE_IS_ENABLED(_state_) \
|
336 |
|
|
CYG_MACRO_START \
|
337 |
|
|
/* SA-110 manual states clearly that the control reg is readable */ \
|
338 |
|
|
register cyg_uint32 reg; \
|
339 |
|
|
asm volatile ("mrc p15,0,%0,c1,c0,0" \
|
340 |
|
|
: "=r"(reg) \
|
341 |
|
|
: \
|
342 |
|
|
/*:*/ \
|
343 |
|
|
); \
|
344 |
|
|
(_state_) = (0 != (0x1000 & reg)); /* Bit 12 is ICache enable */ \
|
345 |
|
|
CYG_MACRO_END
|
346 |
|
|
|
347 |
|
|
// Set the instruction cache refill burst size
|
348 |
|
|
//#define HAL_ICACHE_BURST_SIZE(_size_)
|
349 |
|
|
|
350 |
|
|
// Load the contents of the given address range into the instruction cache
|
351 |
|
|
// and then lock the cache so that it stays there.
|
352 |
|
|
//#define HAL_ICACHE_LOCK(_base_, _size_)
|
353 |
|
|
|
354 |
|
|
// Undo a previous lock operation
|
355 |
|
|
//#define HAL_ICACHE_UNLOCK(_base_, _size_)
|
356 |
|
|
|
357 |
|
|
// Unlock entire cache
|
358 |
|
|
//#define HAL_ICACHE_UNLOCK_ALL()
|
359 |
|
|
|
360 |
|
|
//-----------------------------------------------------------------------------
|
361 |
|
|
// Instruction cache line control
|
362 |
|
|
|
363 |
|
|
// Invalidate cache lines in the given range without writing to memory.
|
364 |
|
|
//#define HAL_ICACHE_INVALIDATE( _base_ , _size_ )
|
365 |
|
|
|
366 |
|
|
//-----------------------------------------------------------------------------
|
367 |
|
|
#endif // ifndef CYGONCE_HAL_CACHE_H
|
368 |
|
|
// End of hal_cache.h
|