Change r22610 a bit. The new parent viewport isn't really suitable.
[kugel-rb.git] / firmware / export / system.h
blobaa95167b8aee89f5b158d28e029746c96a426e61
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 /* Called from any UIE handler and panicf - wait for a key and return
31 * to reboot system. */
32 extern void system_exception_wait(void);
33 extern void system_init(void);
35 extern long cpu_frequency;
37 struct flash_header {
38 unsigned long magic;
39 unsigned long length;
40 char version[32];
43 bool detect_flashed_romimage(void);
44 bool detect_flashed_ramimage(void);
45 bool detect_original_firmware(void);
47 #if defined(HAVE_ADJUSTABLE_CPU_FREQ) \
48 && defined(ROCKBOX_HAS_LOGF) && (NUM_CORES == 1)
49 #define CPU_BOOST_LOGGING
50 #endif
52 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
53 #if NUM_CORES > 1
54 extern struct spinlock boostctrl_spin;
55 #endif
56 void cpu_boost_init(void);
57 #define FREQ cpu_frequency
58 void set_cpu_frequency(long frequency);
59 #ifdef CPU_BOOST_LOGGING
60 char * cpu_boost_log_getlog_first(void);
61 char * cpu_boost_log_getlog_next(void);
62 int cpu_boost_log_getcount(void);
63 void cpu_boost_(bool on_off, char* location, int line);
64 #else
65 void cpu_boost(bool on_off);
66 #endif
67 void cpu_idle_mode(bool on_off);
68 int get_cpu_boost_counter(void);
69 #else
70 #define FREQ CPU_FREQ
71 #define set_cpu_frequency(frequency)
72 #define cpu_boost(on_off)
73 #define cpu_boost_id(on_off, id)
74 #define cpu_idle_mode(on_off)
75 #define get_cpu_boost_counter()
76 #define get_cpu_boost_tracker()
77 #endif
79 #ifdef CPU_BOOST_LOGGING
80 #define cpu_boost(on_off) cpu_boost_(on_off,__FILE__, __LINE__)
81 #endif
83 #define BAUDRATE 9600
85 #ifndef NULL
86 #define NULL ((void*)0)
87 #endif
89 #ifndef MIN
90 #define MIN(a, b) (((a)<(b))?(a):(b))
91 #endif
93 #ifndef MAX
94 #define MAX(a, b) (((a)>(b))?(a):(b))
95 #endif
97 /* return number of elements in array a */
98 #define ARRAYLEN(a) (sizeof(a)/sizeof((a)[0]))
100 /* return p incremented by specified number of bytes */
101 #define SKIPBYTES(p, count) ((typeof (p))((char *)(p) + (count)))
103 #define P2_M1(p2) ((1 << (p2))-1)
105 /* align up or down to nearest 2^p2 */
106 #define ALIGN_DOWN_P2(n, p2) ((n) & ~P2_M1(p2))
107 #define ALIGN_UP_P2(n, p2) ALIGN_DOWN_P2((n) + P2_M1(p2),p2)
109 /* align up or down to nearest integer multiple of a */
110 #define ALIGN_DOWN(n, a) ((n)/(a)*(a))
111 #define ALIGN_UP(n, a) ALIGN_DOWN((n)+((a)-1),a)
113 /* align start and end of buffer to nearest integer multiple of a */
114 #define ALIGN_BUFFER(ptr,len,align) \
116 uintptr_t tmp_ptr1 = (uintptr_t)ptr; \
117 uintptr_t tmp_ptr2 = tmp_ptr1 + len;\
118 tmp_ptr1 = ALIGN_UP(tmp_ptr1,align); \
119 tmp_ptr2 = ALIGN_DOWN(tmp_ptr2,align); \
120 len = tmp_ptr2 - tmp_ptr1; \
121 ptr = (typeof(ptr))tmp_ptr1; \
124 #ifndef __PCTOOL__
126 /* newer? SDL includes endian.h, So we ignore it */
127 #ifdef SIMULATOR
128 #undef letoh16
129 #undef letoh32
130 #undef htole16
131 #undef htole32
132 #undef betoh16
133 #undef betoh32
134 #undef htobe16
135 #undef htobe32
136 #endif
138 /* live endianness conversion */
139 #ifdef ROCKBOX_LITTLE_ENDIAN
140 #define letoh16(x) (x)
141 #define letoh32(x) (x)
142 #define htole16(x) (x)
143 #define htole32(x) (x)
144 #define betoh16(x) swap16(x)
145 #define betoh32(x) swap32(x)
146 #define htobe16(x) swap16(x)
147 #define htobe32(x) swap32(x)
148 #define swap_odd_even_be32(x) (x)
149 #define swap_odd_even_le32(x) swap_odd_even32(x)
150 #else
151 #define letoh16(x) swap16(x)
152 #define letoh32(x) swap32(x)
153 #define htole16(x) swap16(x)
154 #define htole32(x) swap32(x)
155 #define betoh16(x) (x)
156 #define betoh32(x) (x)
157 #define htobe16(x) (x)
158 #define htobe32(x) (x)
159 #define swap_odd_even_be32(x) swap_odd_even32(x)
160 #define swap_odd_even_le32(x) (x)
161 #endif
163 #endif
165 /* static endianness conversion */
166 #define SWAP_16(x) ((typeof(x))(unsigned short)(((unsigned short)(x) >> 8) | \
167 ((unsigned short)(x) << 8)))
169 #define SWAP_32(x) ((typeof(x))(unsigned long)( ((unsigned long)(x) >> 24) | \
170 (((unsigned long)(x) & 0xff0000ul) >> 8) | \
171 (((unsigned long)(x) & 0xff00ul) << 8) | \
172 ((unsigned long)(x) << 24)))
174 #ifdef ROCKBOX_LITTLE_ENDIAN
175 #define LE_TO_H16(x) (x)
176 #define LE_TO_H32(x) (x)
177 #define H_TO_LE16(x) (x)
178 #define H_TO_LE32(x) (x)
179 #define BE_TO_H16(x) SWAP_16(x)
180 #define BE_TO_H32(x) SWAP_32(x)
181 #define H_TO_BE16(x) SWAP_16(x)
182 #define H_TO_BE32(x) SWAP_32(x)
183 #else
184 #define LE_TO_H16(x) SWAP_16(x)
185 #define LE_TO_H32(x) SWAP_32(x)
186 #define H_TO_LE16(x) SWAP_16(x)
187 #define H_TO_LE32(x) SWAP_32(x)
188 #define BE_TO_H16(x) (x)
189 #define BE_TO_H32(x) (x)
190 #define H_TO_BE16(x) (x)
191 #define H_TO_BE32(x) (x)
192 #endif
194 /* Get the byte offset of a type's member */
195 #define OFFSETOF(type, membername) ((off_t)&((type *)0)->membername)
197 /* Get the type pointer from one of its members */
198 #define TYPE_FROM_MEMBER(type, memberptr, membername) \
199 ((type *)((intptr_t)(memberptr) - OFFSETOF(type, membername)))
201 /* Use to give gcc hints on which branch is most likely taken */
202 #if defined(__GNUC__) && __GNUC__ >= 3
203 #define LIKELY(x) __builtin_expect(!!(x), 1)
204 #define UNLIKELY(x) __builtin_expect(!!(x), 0)
205 #else
206 #define LIKELY(x) (x)
207 #define UNLIKELY(x) (x)
208 #endif
210 /* returns index of first set bit + 1 or 0 if no bits are set */
211 int find_first_set_bit(uint32_t val);
213 static inline __attribute__((always_inline))
214 uint32_t isolate_first_bit(uint32_t val)
215 { return val & -val; }
217 /* gcc 3.4 changed the format of the constraints */
218 #if (__GNUC__ >= 3) && (__GNUC_MINOR__ > 3) || (__GNUC__ >= 4)
219 #define I_CONSTRAINT "I08"
220 #else
221 #define I_CONSTRAINT "I"
222 #endif
224 /* Utilize the user break controller to catch invalid memory accesses. */
225 int system_memory_guard(int newmode);
227 enum {
228 MEMGUARD_KEEP = -1, /* don't change the mode; for reading */
229 MEMGUARD_NONE = 0, /* catch nothing */
230 MEMGUARD_FLASH_WRITES, /* catch writes to area 02 (flash ROM) */
231 MEMGUARD_ZERO_AREA, /* catch all accesses to areas 00 and 01 */
232 MAXMEMGUARD
235 #ifndef SIMULATOR
236 #include "system-target.h"
237 #else /* SIMULATOR */
239 static inline uint16_t swap16(uint16_t value)
241 result[15..8] = value[ 7..0];
242 result[ 7..0] = value[15..8];
245 return (value >> 8) | (value << 8);
248 static inline uint32_t swap32(uint32_t value)
250 result[31..24] = value[ 7.. 0];
251 result[23..16] = value[15.. 8];
252 result[15.. 8] = value[23..16];
253 result[ 7.. 0] = value[31..24];
256 uint32_t hi = swap16(value >> 16);
257 uint32_t lo = swap16(value & 0xffff);
258 return (lo << 16) | hi;
261 static inline uint32_t swap_odd_even32(uint32_t value)
264 result[31..24],[15.. 8] = value[23..16],[ 7.. 0]
265 result[23..16],[ 7.. 0] = value[31..24],[15.. 8]
267 uint32_t t = value & 0xff00ff00;
268 return (t >> 8) | ((t ^ value) << 8);
271 #endif /* !SIMULATOR */
273 #ifndef BIT_N
274 #define BIT_N(n) (1U << (n))
275 #endif
277 /* Declare this as HIGHEST_IRQ_LEVEL if they don't differ */
278 #ifndef DISABLE_INTERRUPTS
279 #define DISABLE_INTERRUPTS HIGHEST_IRQ_LEVEL
280 #endif
282 /* Just define these as empty if not declared */
283 #ifdef HAVE_CPUCACHE_INVALIDATE
284 void cpucache_invalidate(void);
285 #else
286 static inline void cpucache_invalidate(void)
289 #endif
291 #ifdef HAVE_CPUCACHE_FLUSH
292 void cpucache_flush(void);
293 #else
294 static inline void cpucache_flush(void)
297 #endif
299 #ifdef PROC_NEEDS_CACHEALIGN
300 /* Cache alignment attributes and sizes are enabled */
302 /* 2^CACHEALIGN_BITS = the byte size */
303 #define CACHEALIGN_SIZE (1u << CACHEALIGN_BITS)
305 #define CACHEALIGN_ATTR __attribute__((aligned(CACHEALIGN_SIZE)))
306 /* Aligns x up to a CACHEALIGN_SIZE boundary */
307 #define CACHEALIGN_UP(x) \
308 ((typeof (x))ALIGN_UP_P2((uintptr_t)(x), CACHEALIGN_BITS))
309 /* Aligns x down to a CACHEALIGN_SIZE boundary */
310 #define CACHEALIGN_DOWN(x) \
311 ((typeof (x))ALIGN_DOWN_P2((uintptr_t)(x), CACHEALIGN_BITS))
312 /* Aligns at least to the greater of size x or CACHEALIGN_SIZE */
313 #define CACHEALIGN_AT_LEAST_ATTR(x) \
314 __attribute__((aligned(CACHEALIGN_UP(x))))
315 /* Aligns a buffer pointer and size to proper boundaries */
316 #define CACHEALIGN_BUFFER(start, size) \
317 ALIGN_BUFFER((start), (size), CACHEALIGN_SIZE)
319 #else /* ndef PROC_NEEDS_CACHEALIGN */
321 /* Cache alignment attributes and sizes are not enabled */
322 #define CACHEALIGN_ATTR
323 #define CACHEALIGN_AT_LEAST_ATTR(x) \
324 __attribute__((aligned(x)))
325 #define CACHEALIGN_UP(x) (x)
326 #define CACHEALIGN_DOWN(x) (x)
327 /* Make no adjustments */
328 #define CACHEALIGN_BUFFER(start, size)
330 #endif /* PROC_NEEDS_CACHEALIGN */
332 /* Double-cast to avoid 'dereferencing type-punned pointer will
333 * break strict aliasing rules' B.S. */
334 #define PUN_PTR(type, p) ((type)(intptr_t)(p))
336 #endif /* __SYSTEM_H__ */