Don't set CONFIG_CPU for __PCTOOL__, to avoid wrong asm
[maemo-rb.git] / firmware / export / system.h
blobb33edc6d3e892eb6581dce093d2346f8930a797c
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"
28 #include "gcc_extensions.h" /* for LIKELY/UNLIKELY */
30 extern void system_reboot (void);
31 /* Called from any UIE handler and panicf - wait for a key and return
32 * to reboot system. */
33 extern void system_exception_wait(void);
34 extern void system_init(void);
36 extern long cpu_frequency;
38 struct flash_header {
39 unsigned long magic;
40 unsigned long length;
41 char version[32];
44 bool detect_flashed_romimage(void);
45 bool detect_flashed_ramimage(void);
46 bool detect_original_firmware(void);
48 #if defined(HAVE_ADJUSTABLE_CPU_FREQ) \
49 && defined(ROCKBOX_HAS_LOGF) && (NUM_CORES == 1)
50 #define CPU_BOOST_LOGGING
51 #endif
53 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
54 #if NUM_CORES > 1
55 extern struct spinlock boostctrl_spin;
56 #endif
57 void cpu_boost_init(void);
58 #define FREQ cpu_frequency
59 void set_cpu_frequency(long frequency);
60 #ifdef CPU_BOOST_LOGGING
61 char * cpu_boost_log_getlog_first(void);
62 char * cpu_boost_log_getlog_next(void);
63 int cpu_boost_log_getcount(void);
64 void cpu_boost_(bool on_off, char* location, int line);
65 #else
66 void cpu_boost(bool on_off);
67 #endif
68 void cpu_idle_mode(bool on_off);
69 int get_cpu_boost_counter(void);
70 #else /* ndef HAVE_ADJUSTABLE_CPU_FREQ */
71 #ifndef FREQ
72 #define FREQ CPU_FREQ
73 #endif
74 #define set_cpu_frequency(frequency)
75 #define cpu_boost(on_off)
76 #define cpu_boost_id(on_off, id)
77 #define cpu_idle_mode(on_off)
78 #define get_cpu_boost_counter()
79 #define get_cpu_boost_tracker()
80 #endif /* HAVE_ADJUSTABLE_CPU_FREQ */
82 #ifdef CPU_BOOST_LOGGING
83 #define cpu_boost(on_off) cpu_boost_(on_off,__FILE__, __LINE__)
84 #endif
86 #define BAUDRATE 9600
88 #ifndef NULL
89 #define NULL ((void*)0)
90 #endif
92 #ifndef MIN
93 #define MIN(a, b) (((a)<(b))?(a):(b))
94 #endif
96 #ifndef MAX
97 #define MAX(a, b) (((a)>(b))?(a):(b))
98 #endif
100 /* return number of elements in array a */
101 #define ARRAYLEN(a) (sizeof(a)/sizeof((a)[0]))
103 /* return p incremented by specified number of bytes */
104 #define SKIPBYTES(p, count) ((typeof (p))((char *)(p) + (count)))
106 #define P2_M1(p2) ((1 << (p2))-1)
108 /* align up or down to nearest 2^p2 */
109 #define ALIGN_DOWN_P2(n, p2) ((n) & ~P2_M1(p2))
110 #define ALIGN_UP_P2(n, p2) ALIGN_DOWN_P2((n) + P2_M1(p2),p2)
112 /* align up or down to nearest integer multiple of a */
113 #define ALIGN_DOWN(n, a) ((typeof(n))((typeof(a))(n)/(a)*(a)))
114 #define ALIGN_UP(n, a) ALIGN_DOWN((n)+((a)-1),a)
116 /* align start and end of buffer to nearest integer multiple of a */
117 #define ALIGN_BUFFER(ptr,len,align) \
119 uintptr_t tmp_ptr1 = (uintptr_t)ptr; \
120 uintptr_t tmp_ptr2 = tmp_ptr1 + len;\
121 tmp_ptr1 = ALIGN_UP(tmp_ptr1,align); \
122 tmp_ptr2 = ALIGN_DOWN(tmp_ptr2,align); \
123 len = tmp_ptr2 - tmp_ptr1; \
124 ptr = (typeof(ptr))tmp_ptr1; \
127 #define PTR_ADD(ptr, x) ((typeof(ptr))((char*)(ptr) + (x)))
128 #define PTR_SUB(ptr, x) ((typeof(ptr))((char*)(ptr) - (x)))
130 /* newer? SDL includes endian.h, So we ignore it */
131 #if (CONFIG_PLATFORM & PLATFORM_HOSTED) || defined(__PCTOOL__)
132 #undef letoh16
133 #undef letoh32
134 #undef htole16
135 #undef htole32
136 #undef betoh16
137 #undef betoh32
138 #undef htobe16
139 #undef htobe32
140 #endif
142 /* Android NDK contains swap16 and swap32, ignore them */
143 #if (CONFIG_PLATFORM & PLATFORM_ANDROID)
144 #undef swap16
145 #undef swap32
146 #endif
148 /* Get the byte offset of a type's member */
149 #define OFFSETOF(type, membername) ((off_t)&((type *)0)->membername)
151 /* Get the type pointer from one of its members */
152 #define TYPE_FROM_MEMBER(type, memberptr, membername) \
153 ((type *)((intptr_t)(memberptr) - OFFSETOF(type, membername)))
155 /* returns index of first set bit or 32 if no bits are set */
156 int find_first_set_bit(uint32_t val);
158 static inline __attribute__((always_inline))
159 uint32_t isolate_first_bit(uint32_t val)
160 { return val & -val; }
162 /* Functions to set and clear register or variable bits atomically */
163 void bitmod16(volatile uint16_t *addr, uint16_t bits, uint16_t mask);
164 void bitset16(volatile uint16_t *addr, uint16_t mask);
165 void bitclr16(volatile uint16_t *addr, uint16_t mask);
167 void bitmod32(volatile uint32_t *addr, uint32_t bits, uint32_t mask);
168 void bitset32(volatile uint32_t *addr, uint32_t mask);
169 void bitclr32(volatile uint32_t *addr, uint32_t mask);
171 /* gcc 3.4 changed the format of the constraints */
172 #if (__GNUC__ >= 3) && (__GNUC_MINOR__ > 3) || (__GNUC__ >= 4)
173 #define I_CONSTRAINT "I08"
174 #else
175 #define I_CONSTRAINT "I"
176 #endif
178 /* Utilize the user break controller to catch invalid memory accesses. */
179 int system_memory_guard(int newmode);
181 enum {
182 MEMGUARD_KEEP = -1, /* don't change the mode; for reading */
183 MEMGUARD_NONE = 0, /* catch nothing */
184 MEMGUARD_FLASH_WRITES, /* catch writes to area 02 (flash ROM) */
185 MEMGUARD_ZERO_AREA, /* catch all accesses to areas 00 and 01 */
186 MAXMEMGUARD
189 #if !defined(SIMULATOR) && !defined(__PCTOOL__)
190 #include "system-target.h"
191 #elif defined(HAVE_SDL) /* SDL build */
192 #include "system-sdl.h"
193 #define NEED_GENERIC_BYTESWAPS
194 #elif defined(__PCTOOL__)
195 #define NEED_GENERIC_BYTESWAPS
196 #endif
197 #include "bitswap.h"
199 #ifdef NEED_GENERIC_BYTESWAPS
200 static inline uint16_t swap16_hw(uint16_t value)
202 result[15..8] = value[ 7..0];
203 result[ 7..0] = value[15..8];
206 return (value >> 8) | (value << 8);
209 static inline uint32_t swap32_hw(uint32_t value)
211 result[31..24] = value[ 7.. 0];
212 result[23..16] = value[15.. 8];
213 result[15.. 8] = value[23..16];
214 result[ 7.. 0] = value[31..24];
217 uint32_t hi = swap16_hw(value >> 16);
218 uint32_t lo = swap16_hw(value & 0xffff);
219 return (lo << 16) | hi;
222 static inline uint32_t swap_odd_even32_hw(uint32_t value)
225 result[31..24],[15.. 8] = value[23..16],[ 7.. 0]
226 result[23..16],[ 7.. 0] = value[31..24],[15.. 8]
228 uint32_t t = value & 0xff00ff00;
229 return (t >> 8) | ((t ^ value) << 8);
232 static inline uint32_t swaw32_hw(uint32_t value)
235 result[31..16] = value[15.. 0];
236 result[15.. 0] = value[31..16];
238 return (value >> 16) | (value << 16);
241 #endif /* NEED_GENERIC_BYTESWAPS */
243 /* static endianness conversion */
244 #define SWAP16_CONST(x) \
245 ((typeof(x))( ((uint16_t)(x) >> 8) | ((uint16_t)(x) << 8) ))
247 #define SWAP32_CONST(x) \
248 ((typeof(x))( ((uint32_t)(x) >> 24) | \
249 (((uint32_t)(x) & 0xff0000) >> 8) | \
250 (((uint32_t)(x) & 0xff00) << 8) | \
251 ((uint32_t)(x) << 24) ))
253 #define SWAP_ODD_EVEN32_CONST(x) \
254 ((typeof(x))( ((uint32_t)SWAP16_CONST((uint32_t)(x) >> 16) << 16) | \
255 SWAP16_CONST((uint32_t)(x))) )
257 #define SWAW32_CONST(x) \
258 ((typeof(x))( ((uint32_t)(x) << 16) | ((uint32_t)(x) >> 16) ))
260 /* Select best method based upon whether x is a constant expression */
261 #define swap16(x) \
262 ( __builtin_constant_p(x) ? SWAP16_CONST(x) : (typeof(x))swap16_hw(x) )
264 #define swap32(x) \
265 ( __builtin_constant_p(x) ? SWAP32_CONST(x) : (typeof(x))swap32_hw(x) )
267 #define swap_odd_even32(x) \
268 ( __builtin_constant_p(x) ? SWAP_ODD_EVEN32_CONST(x) : (typeof(x))swap_odd_even32_hw(x) )
270 #define swaw32(x) \
271 ( __builtin_constant_p(x) ? SWAW32_CONST(x) : (typeof(x))swaw32_hw(x) )
274 #ifdef ROCKBOX_LITTLE_ENDIAN
275 #define letoh16(x) (x)
276 #define letoh32(x) (x)
277 #define htole16(x) (x)
278 #define htole32(x) (x)
279 #define betoh16(x) swap16(x)
280 #define betoh32(x) swap32(x)
281 #define htobe16(x) swap16(x)
282 #define htobe32(x) swap32(x)
283 #define swap_odd_even_be32(x) (x)
284 #define swap_odd_even_le32(x) swap_odd_even32(x)
285 #else
286 #define letoh16(x) swap16(x)
287 #define letoh32(x) swap32(x)
288 #define htole16(x) swap16(x)
289 #define htole32(x) swap32(x)
290 #define betoh16(x) (x)
291 #define betoh32(x) (x)
292 #define htobe16(x) (x)
293 #define htobe32(x) (x)
294 #define swap_odd_even_be32(x) swap_odd_even32(x)
295 #define swap_odd_even_le32(x) (x)
296 #endif
298 #ifndef BIT_N
299 #define BIT_N(n) (1U << (n))
300 #endif
302 /* Declare this as HIGHEST_IRQ_LEVEL if they don't differ */
303 #ifndef DISABLE_INTERRUPTS
304 #define DISABLE_INTERRUPTS HIGHEST_IRQ_LEVEL
305 #endif
307 /* Define this, if the CPU may take advantage of cache aligment. Is enabled
308 * for all ARM CPUs. */
309 #ifdef CPU_ARM
310 #define HAVE_CPU_CACHE_ALIGN
311 #endif
313 /* Calculate CACHEALIGN_SIZE from CACHEALIGN_BITS */
314 #ifdef CACHEALIGN_SIZE
315 /* undefine, if defined. always calculate from CACHEALIGN_BITS */
316 #undef CACHEALIGN_SIZE
317 #endif
318 #ifdef CACHEALIGN_BITS
319 /* CACHEALIGN_SIZE = 2 ^ CACHEALIGN_BITS */
320 #define CACHEALIGN_SIZE (1u << CACHEALIGN_BITS)
321 #else
322 /* FIXME: set to maximum known cache alignment of supported CPUs */
323 #define CACHEALIGN_BITS 5
324 #define CACHEALIGN_SIZE 32
325 #endif
327 #ifdef HAVE_CPU_CACHE_ALIGN
328 /* Cache alignment attributes and sizes are enabled */
329 #define CACHEALIGN_ATTR __attribute__((aligned(CACHEALIGN_SIZE)))
330 /* Aligns x up to a CACHEALIGN_SIZE boundary */
331 #define CACHEALIGN_UP(x) \
332 ((typeof (x))ALIGN_UP_P2((uintptr_t)(x), CACHEALIGN_BITS))
333 /* Aligns x down to a CACHEALIGN_SIZE boundary */
334 #define CACHEALIGN_DOWN(x) \
335 ((typeof (x))ALIGN_DOWN_P2((uintptr_t)(x), CACHEALIGN_BITS))
336 /* Aligns at least to the greater of size x or CACHEALIGN_SIZE */
337 #define CACHEALIGN_AT_LEAST_ATTR(x) \
338 __attribute__((aligned(CACHEALIGN_UP(x))))
339 /* Aligns a buffer pointer and size to proper boundaries */
340 #define CACHEALIGN_BUFFER(start, size) \
341 ALIGN_BUFFER((start), (size), CACHEALIGN_SIZE)
342 #else
343 /* Cache alignment attributes and sizes are not enabled */
344 #define CACHEALIGN_ATTR
345 #define CACHEALIGN_AT_LEAST_ATTR(x) __attribute__((aligned(x)))
346 #define CACHEALIGN_UP(x) (x)
347 #define CACHEALIGN_DOWN(x) (x)
348 /* Make no adjustments */
349 #define CACHEALIGN_BUFFER(start, size)
350 #endif
352 /* Define MEM_ALIGN_ATTR which may be used to align e.g. buffers for faster
353 * access. */
354 #if defined(CPU_ARM)
355 /* Use ARMs cache alignment. */
356 #define MEM_ALIGN_ATTR CACHEALIGN_ATTR
357 #define MEM_ALIGN_SIZE CACHEALIGN_SIZE
358 #elif defined(CPU_COLDFIRE)
359 /* Use fixed alignment of 16 bytes. Speed up only for 'movem' in DRAM. */
360 #define MEM_ALIGN_ATTR __attribute__((aligned(16)))
361 #define MEM_ALIGN_SIZE 16
362 #else
363 /* Do nothing. */
364 #define MEM_ALIGN_ATTR
365 /* Align pointer size */
366 #define MEM_ALIGN_SIZE sizeof(intptr_t)
367 #endif
369 #ifdef STORAGE_WANTS_ALIGN
370 #define STORAGE_ALIGN_ATTR __attribute__((aligned(CACHEALIGN_SIZE)))
371 #define STORAGE_ALIGN_DOWN(x) \
372 ((typeof (x))ALIGN_DOWN_P2((uintptr_t)(x), CACHEALIGN_BITS))
373 /* Pad a size so the buffer can be aligned later */
374 #define STORAGE_PAD(x) ((x) + CACHEALIGN_SIZE - 1)
375 /* Number of bytes in the last cacheline assuming buffer of size x is aligned */
376 #define STORAGE_OVERLAP(x) ((x) & (CACHEALIGN_SIZE - 1))
377 #define STORAGE_ALIGN_BUFFER(start, size) \
378 ALIGN_BUFFER((start), (size), CACHEALIGN_SIZE)
379 #else
380 #define STORAGE_ALIGN_ATTR
381 #define STORAGE_ALIGN_DOWN(x) (x)
382 #define STORAGE_PAD(x) (x)
383 #define STORAGE_OVERLAP(x) 0
384 #define STORAGE_ALIGN_BUFFER(start, size)
385 #endif
387 /* Double-cast to avoid 'dereferencing type-punned pointer will
388 * break strict aliasing rules' B.S. */
389 #define PUN_PTR(type, p) ((type)(intptr_t)(p))
391 #ifndef SIMULATOR
392 bool dbg_ports(void);
393 #endif
394 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
395 bool dbg_hw_info(void);
396 #endif
398 #endif /* __SYSTEM_H__ */