port-work; won't compile or even work
[ppcskel.git] / bootmii_ppc.h
1 /*
2         BootMii - a Free Software replacement for the Nintendo/BroadOn bootloader.
3         Requires mini.
4
5 Copyright (C) 2008              Segher Boessenkool <segher@kernel.crashing.org>
6
7 # This code is licensed to you under the terms of the GNU GPL, version 2;
8 # see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
9 */
10
11 #ifndef __PPC_H__
12 #define __PPC_H__
13
14 #include "types.h"
15 #include "printf.h"
16
17 #define OK 0
18 #define EFAIL 1
19
20 #define MEM2_BSS __attribute__ ((section (".bss.mem2")))
21 #define MEM2_DATA __attribute__ ((section (".data.mem2")))
22 #define MEM2_RODATA __attribute__ ((section (".rodata.mem2")))
23 #define ALIGNED(x) __attribute__((aligned(x)))
24
25 #define STACK_ALIGN(type, name, cnt, alignment)         \
26         u8 _al__##name[((sizeof(type)*(cnt)) + (alignment) + \
27         (((sizeof(type)*(cnt))%(alignment)) > 0 ? ((alignment) - \
28         ((sizeof(type)*(cnt))%(alignment))) : 0))]; \
29         type *name = (type*)(((u32)(_al__##name)) + ((alignment) - (( \
30         (u32)(_al__##name))&((alignment)-1))))
31
32 // Basic I/O.
33
34 static inline u32 read32(u32 addr)
35 {
36         u32 x;
37
38         asm volatile("lwz %0,0(%1) ; sync" : "=r"(x) : "b"(0xc0000000 | addr));
39
40         return x;
41 }
42
43 static inline void write32(u32 addr, u32 x)
44 {
45         asm("stw %0,0(%1) ; eieio" : : "r"(x), "b"(0xc0000000 | addr));
46 }
47
48 static inline void set32(u32 addr, u32 set)
49 {
50         write32(addr, read32(addr) | set);
51 }
52
53 static inline void clear32(u32 addr, u32 clear)
54 {
55         write32(addr, read32(addr)&(~clear));
56 }
57
58 static inline void mask32(u32 addr, u32 clear, u32 set)
59 {
60         write32(addr, (read32(addr)&(~clear)) | set);
61 }
62
63 static inline u16 read16(u32 addr)
64 {
65         u16 x;
66
67         asm volatile("lhz %0,0(%1) ; sync" : "=r"(x) : "b"(0xc0000000 | addr));
68
69         return x;
70 }
71
72 static inline void write16(u32 addr, u16 x)
73 {
74         asm("sth %0,0(%1) ; eieio" : : "r"(x), "b"(0xc0000000 | addr));
75 }
76
77
78 // Address mapping.
79
80 static inline u32 virt_to_phys(const void *p)
81 {
82         return (u32)p & 0x7fffffff;
83 }
84
85 static inline void *phys_to_virt(u32 x)
86 {
87         return (void *)(x | 0x80000000);
88 }
89
90
91 // Cache synchronisation.
92
93 void sync_before_read(void *p, u32 len);
94 void sync_after_write(const void *p, u32 len);
95 void sync_before_exec(const void *p, u32 len);
96
97
98 // Time.
99
100 void udelay(u32 us);
101 u64 mftb(void);
102
103
104 // Special purpose registers.
105
106 #define mtspr(n, x) do { asm("mtspr %1,%0" : : "r"(x), "i"(n)); } while (0)
107 #define mfspr(n) ({ \
108         u32 x; asm volatile("mfspr %0,%1" : "=r"(x) : "i"(n)); x; \
109 })
110
111
112 // Exceptions.
113
114 void exception_init(void);
115
116
117 // Console.
118
119 void gecko_init(void);
120 int printf(const char *fmt, ...);
121 void hexdump(void *d, int len);
122
123
124 // Debug: blink the tray led.
125
126 static inline void blink(void)
127 {
128         write32(0x0d8000c0, read32(0x0d8000c0) ^ 0x20);
129 }
130
131 #endif
132