ohci0 interrupt will be thrown - hooray++ \o/
[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 mask32(u32 addr, u32 clear, u32 set)
49 {
50         write32(addr, (read32(addr)&(~clear)) | set);
51 }
52
53 static inline u16 read16(u32 addr)
54 {
55         u16 x;
56
57         asm volatile("lhz %0,0(%1) ; sync" : "=r"(x) : "b"(0xc0000000 | addr));
58
59         return x;
60 }
61
62 static inline void write16(u32 addr, u16 x)
63 {
64         asm("sth %0,0(%1) ; eieio" : : "r"(x), "b"(0xc0000000 | addr));
65 }
66
67
68 // Address mapping.
69
70 static inline u32 virt_to_phys(const void *p)
71 {
72         return (u32)p & 0x7fffffff;
73 }
74
75 static inline void *phys_to_virt(u32 x)
76 {
77         return (void *)(x | 0x80000000);
78 }
79
80
81 // Cache synchronisation.
82
83 void sync_before_read(void *p, u32 len);
84 void sync_after_write(const void *p, u32 len);
85 void sync_before_exec(const void *p, u32 len);
86
87
88 // Time.
89
90 void udelay(u32 us);
91 u64 mftb(void);
92
93
94 // Special purpose registers.
95
96 #define mtspr(n, x) do { asm("mtspr %1,%0" : : "r"(x), "i"(n)); } while (0)
97 #define mfspr(n) ({ \
98         u32 x; asm volatile("mfspr %0,%1" : "=r"(x) : "i"(n)); x; \
99 })
100
101
102 // Exceptions.
103
104 void exception_init(void);
105
106
107 // Console.
108
109 void gecko_init(void);
110 int printf(const char *fmt, ...);
111
112
113 // Debug: blink the tray led.
114
115 static inline void blink(void)
116 {
117         write32(0x0d8000c0, read32(0x0d8000c0) ^ 0x20);
118 }
119
120 #endif
121