merge 19488 back from the 3.1 branch
[kugel-rb.git] / firmware / export / system.h
blobb82e2b1ad0fbdc951585f5910237a29126004bce
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Alan Korr
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #ifndef __SYSTEM_H__
23 #define __SYSTEM_H__
25 #include "cpu.h"
26 #include "stdbool.h"
27 #include "kernel.h"
29 extern void system_reboot (void);
30 extern void system_init(void);
32 extern long cpu_frequency;
34 struct flash_header {
35 unsigned long magic;
36 unsigned long length;
37 char version[32];
40 bool detect_flashed_romimage(void);
41 bool detect_flashed_ramimage(void);
42 bool detect_original_firmware(void);
44 #if defined(HAVE_ADJUSTABLE_CPU_FREQ) \
45 && defined(ROCKBOX_HAS_LOGF) && (NUM_CORES == 1)
46 #define CPU_BOOST_LOGGING
47 #endif
49 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
50 #if NUM_CORES > 1
51 extern struct spinlock boostctrl_spin;
52 #endif
53 void cpu_boost_init(void);
54 #define FREQ cpu_frequency
55 void set_cpu_frequency(long frequency);
56 #ifdef CPU_BOOST_LOGGING
57 char * cpu_boost_log_getlog_first(void);
58 char * cpu_boost_log_getlog_next(void);
59 int cpu_boost_log_getcount(void);
60 void cpu_boost_(bool on_off, char* location, int line);
61 #else
62 void cpu_boost(bool on_off);
63 #endif
64 void cpu_idle_mode(bool on_off);
65 int get_cpu_boost_counter(void);
66 #else
67 #define FREQ CPU_FREQ
68 #define set_cpu_frequency(frequency)
69 #define cpu_boost(on_off)
70 #define cpu_boost_id(on_off, id)
71 #define cpu_idle_mode(on_off)
72 #define get_cpu_boost_counter()
73 #define get_cpu_boost_tracker()
74 #endif
76 #ifdef CPU_BOOST_LOGGING
77 #define cpu_boost(on_off) cpu_boost_(on_off,__FILE__, __LINE__)
78 #endif
80 #define BAUDRATE 9600
82 #ifndef NULL
83 #define NULL ((void*)0)
84 #endif
86 #ifndef MIN
87 #define MIN(a, b) (((a)<(b))?(a):(b))
88 #endif
90 #ifndef MAX
91 #define MAX(a, b) (((a)>(b))?(a):(b))
92 #endif
94 /* return number of elements in array a */
95 #define ARRAYLEN(a) (sizeof(a)/sizeof((a)[0]))
97 /* return p incremented by specified number of bytes */
98 #define SKIPBYTES(p, count) ((typeof (p))((char *)(p) + (count)))
100 #define P2_M1(p2) ((1 << (p2))-1)
102 /* align up or down to nearest 2^p2 */
103 #define ALIGN_DOWN_P2(n, p2) ((n) & ~P2_M1(p2))
104 #define ALIGN_UP_P2(n, p2) ALIGN_DOWN_P2((n) + P2_M1(p2),p2)
106 /* align up or down to nearest integer multiple of a */
107 #define ALIGN_DOWN(n, a) ((n)/(a)*(a))
108 #define ALIGN_UP(n, a) ALIGN_DOWN((n)+((a)-1),a)
110 /* live endianness conversion */
111 #ifdef ROCKBOX_LITTLE_ENDIAN
112 #define letoh16(x) (x)
113 #define letoh32(x) (x)
114 #define htole16(x) (x)
115 #define htole32(x) (x)
116 #define betoh16(x) swap16(x)
117 #define betoh32(x) swap32(x)
118 #define htobe16(x) swap16(x)
119 #define htobe32(x) swap32(x)
120 #define swap_odd_even_be32(x) (x)
121 #define swap_odd_even_le32(x) swap_odd_even32(x)
122 #else
123 #define letoh16(x) swap16(x)
124 #define letoh32(x) swap32(x)
125 #define htole16(x) swap16(x)
126 #define htole32(x) swap32(x)
127 #define betoh16(x) (x)
128 #define betoh32(x) (x)
129 #define htobe16(x) (x)
130 #define htobe32(x) (x)
131 #define swap_odd_even_be32(x) swap_odd_even32(x)
132 #define swap_odd_even_le32(x) (x)
133 #endif
135 /* static endianness conversion */
136 #define SWAP_16(x) ((typeof(x))(unsigned short)(((unsigned short)(x) >> 8) | \
137 ((unsigned short)(x) << 8)))
139 #define SWAP_32(x) ((typeof(x))(unsigned long)( ((unsigned long)(x) >> 24) | \
140 (((unsigned long)(x) & 0xff0000ul) >> 8) | \
141 (((unsigned long)(x) & 0xff00ul) << 8) | \
142 ((unsigned long)(x) << 24)))
144 #ifdef ROCKBOX_LITTLE_ENDIAN
145 #define LE_TO_H16(x) (x)
146 #define LE_TO_H32(x) (x)
147 #define H_TO_LE16(x) (x)
148 #define H_TO_LE32(x) (x)
149 #define BE_TO_H16(x) SWAP_16(x)
150 #define BE_TO_H32(x) SWAP_32(x)
151 #define H_TO_BE16(x) SWAP_16(x)
152 #define H_TO_BE32(x) SWAP_32(x)
153 #else
154 #define LE_TO_H16(x) SWAP_16(x)
155 #define LE_TO_H32(x) SWAP_32(x)
156 #define H_TO_LE16(x) SWAP_16(x)
157 #define H_TO_LE32(x) SWAP_32(x)
158 #define BE_TO_H16(x) (x)
159 #define BE_TO_H32(x) (x)
160 #define H_TO_BE16(x) (x)
161 #define H_TO_BE32(x) (x)
162 #endif
164 /* Get the byte offset of a type's member */
165 #define OFFSETOF(type, membername) ((off_t)&((type *)0)->membername)
167 /* Get the type pointer from one of its members */
168 #define TYPE_FROM_MEMBER(type, memberptr, membername) \
169 ((type *)((intptr_t)(memberptr) - OFFSETOF(type, membername)))
171 /* Use to give gcc hints on which branch is most likely taken */
172 #if defined(__GNUC__) && __GNUC__ >= 3
173 #define LIKELY(x) __builtin_expect(!!(x), 1)
174 #define UNLIKELY(x) __builtin_expect(!!(x), 0)
175 #else
176 #define LIKELY(x) (x)
177 #define UNLIKELY(x) (x)
178 #endif
180 /* returns index of first set bit + 1 or 0 if no bits are set */
181 int find_first_set_bit(uint32_t val);
183 static inline __attribute__((always_inline))
184 uint32_t isolate_first_bit(uint32_t val)
185 { return val & -val; }
187 /* gcc 3.4 changed the format of the constraints */
188 #if (__GNUC__ >= 3) && (__GNUC_MINOR__ > 3) || (__GNUC__ >= 4)
189 #define I_CONSTRAINT "I08"
190 #else
191 #define I_CONSTRAINT "I"
192 #endif
194 /* Utilize the user break controller to catch invalid memory accesses. */
195 int system_memory_guard(int newmode);
197 enum {
198 MEMGUARD_KEEP = -1, /* don't change the mode; for reading */
199 MEMGUARD_NONE = 0, /* catch nothing */
200 MEMGUARD_FLASH_WRITES, /* catch writes to area 02 (flash ROM) */
201 MEMGUARD_ZERO_AREA, /* catch all accesses to areas 00 and 01 */
202 MAXMEMGUARD
205 #ifndef SIMULATOR
206 #include "system-target.h"
207 #else /* SIMULATOR */
209 static inline uint16_t swap16(uint16_t value)
211 result[15..8] = value[ 7..0];
212 result[ 7..0] = value[15..8];
215 return (value >> 8) | (value << 8);
218 static inline uint32_t swap32(uint32_t value)
220 result[31..24] = value[ 7.. 0];
221 result[23..16] = value[15.. 8];
222 result[15.. 8] = value[23..16];
223 result[ 7.. 0] = value[31..24];
226 uint32_t hi = swap16(value >> 16);
227 uint32_t lo = swap16(value & 0xffff);
228 return (lo << 16) | hi;
231 static inline uint32_t swap_odd_even32(uint32_t value)
234 result[31..24],[15.. 8] = value[23..16],[ 7.. 0]
235 result[23..16],[ 7.. 0] = value[31..24],[15.. 8]
237 uint32_t t = value & 0xff00ff00;
238 return (t >> 8) | ((t ^ value) << 8);
241 #endif /* !SIMULATOR */
243 /* Declare this as HIGHEST_IRQ_LEVEL if they don't differ */
244 #ifndef DISABLE_INTERRUPTS
245 #define DISABLE_INTERRUPTS HIGHEST_IRQ_LEVEL
246 #endif
248 /* Just define these as empty if not declared */
249 #ifndef HAVE_INVALIDATE_ICACHE
250 #define invalidate_icache()
251 #endif
253 #ifndef HAVE_FLUSH_ICACHE
254 #define flush_icache()
255 #endif
257 #ifdef PROC_NEEDS_CACHEALIGN
258 /* Cache alignment attributes and sizes are enabled */
260 /* 2^CACHEALIGN_BITS = the byte size */
261 #define CACHEALIGN_SIZE (1u << CACHEALIGN_BITS)
263 #define CACHEALIGN_ATTR __attribute__((aligned(CACHEALIGN_SIZE)))
264 /* Aligns x up to a CACHEALIGN_SIZE boundary */
265 #define CACHEALIGN_UP(x) \
266 ((typeof (x))ALIGN_UP_P2((uintptr_t)(x), CACHEALIGN_BITS))
267 /* Aligns x down to a CACHEALIGN_SIZE boundary */
268 #define CACHEALIGN_DOWN(x) \
269 ((typeof (x))ALIGN_DOWN_P2((uintptr_t)(x), CACHEALIGN_BITS))
270 /* Aligns at least to the greater of size x or CACHEALIGN_SIZE */
271 #define CACHEALIGN_AT_LEAST_ATTR(x) \
272 __attribute__((aligned(CACHEALIGN_UP(x))))
273 /* Aligns a buffer pointer and size to proper boundaries */
274 #define CACHEALIGN_BUFFER(start, size) \
275 ({ align_buffer(PUN_PTR(void **, (start)), (size), CACHEALIGN_SIZE); })
277 #else /* ndef PROC_NEEDS_CACHEALIGN */
279 /* Cache alignment attributes and sizes are not enabled */
280 #define CACHEALIGN_ATTR
281 #define CACHEALIGN_AT_LEAST_ATTR(x) \
282 __attribute__((aligned(x)))
283 #define CACHEALIGN_UP(x) (x)
284 #define CACHEALIGN_DOWN(x) (x)
285 /* Make no adjustments */
286 #define CACHEALIGN_BUFFER(start, size) \
287 ({ (void)(start); (size); })
289 #endif /* PROC_NEEDS_CACHEALIGN */
291 /* Double-cast to avoid 'dereferencing type-punned pointer will
292 * break strict aliasing rules' B.S. */
293 #define PUN_PTR(type, p) ((type)(intptr_t)(p))
295 #endif /* __SYSTEM_H__ */