2 BootMii - a Free Software replacement for the Nintendo/BroadOn bootloader.
5 Copyright (c) 2008 Nuke - <wiinuke@gmail.com>
6 Copyright (C) 2008, 2009 Hector Martin "marcan" <marcan@marcansoft.com>
7 Copyright (C) 2009 Andre Heider "dhewg" <dhewg@wiibrew.org>
9 # This code is licensed to you under the terms of the GNU GPL, version 2;
10 # see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
13 #include "bootmii_ppc.h"
16 #define EXI_REG_BASE 0xd806800
17 #define EXI0_REG_BASE (EXI_REG_BASE+0x000)
18 #define EXI1_REG_BASE (EXI_REG_BASE+0x014)
19 #define EXI2_REG_BASE (EXI_REG_BASE+0x028)
21 #define EXI0_CSR (EXI0_REG_BASE+0x000)
22 #define EXI0_MAR (EXI0_REG_BASE+0x004)
23 #define EXI0_LENGTH (EXI0_REG_BASE+0x008)
24 #define EXI0_CR (EXI0_REG_BASE+0x00c)
25 #define EXI0_DATA (EXI0_REG_BASE+0x010)
27 #define EXI1_CSR (EXI1_REG_BASE+0x000)
28 #define EXI1_MAR (EXI1_REG_BASE+0x004)
29 #define EXI1_LENGTH (EXI1_REG_BASE+0x008)
30 #define EXI1_CR (EXI1_REG_BASE+0x00c)
31 #define EXI1_DATA (EXI1_REG_BASE+0x010)
33 #define EXI2_CSR (EXI2_REG_BASE+0x000)
34 #define EXI2_MAR (EXI2_REG_BASE+0x004)
35 #define EXI2_LENGTH (EXI2_REG_BASE+0x008)
36 #define EXI2_CR (EXI2_REG_BASE+0x00c)
37 #define EXI2_DATA (EXI2_REG_BASE+0x010)
39 static int gecko_console_enabled = 0;
41 static u32 _gecko_command(u32 command) {
43 // Memory Card Port B (Channel 1, Device 0, Frequency 3 (32Mhz Clock))
44 write32(EXI1_CSR, 0xd0);
45 write32(EXI1_DATA, command);
46 write32(EXI1_CR, 0x19);
48 while ((read32(EXI1_CR) & 1) && (i--));
49 i = read32(EXI1_DATA);
54 static u32 _gecko_getid(void)
57 // Memory Card Port B (Channel 1, Device 0, Frequency 3 (32Mhz Clock))
58 write32(EXI1_CSR, 0xd0);
59 write32(EXI1_DATA, 0);
60 write32(EXI1_CR, 0x19);
61 while (read32(EXI1_CR) & 1);
62 write32(EXI1_CR, 0x39);
63 while (read32(EXI1_CR) & 1);
64 i = read32(EXI1_DATA);
69 static u32 _gecko_sendbyte(char sendbyte) {
71 i = _gecko_command(0xB0000000 | (sendbyte<<20));
73 return 1; // Return 1 if byte was sent
78 static u32 _gecko_recvbyte(char *recvbyte) {
81 i = _gecko_command(0xA0000000);
83 // Return 1 if byte was received
84 *recvbyte = (i>>16)&0xff;
90 static u32 _gecko_checksend(void) {
92 i = _gecko_command(0xC0000000);
94 return 1; // Return 1 if safe to send
98 static u32 _gecko_checkrecv(void) {
100 i = _gecko_command(0xD0000000);
102 return 1; // Return 1 if safe to recv
106 static void gecko_flush(void) {
108 while(_gecko_recvbyte(&tmp));
112 static int gecko_isalive(void) {
119 i = _gecko_command(0x90000000);
120 if ((i & 0xFFFF0000) != 0x04700000)
127 static int gecko_recvbuffer(void *buffer, u32 size) {
129 char *ptr = (char*)buffer;
132 if(!_gecko_recvbyte(ptr))
137 return (size - left);
141 static int gecko_sendbuffer(const void *buffer, u32 size) {
143 char *ptr = (char*)buffer;
146 if(!_gecko_sendbyte(*ptr))
151 return (size - left);
155 static int gecko_recvbuffer_safe(void *buffer, u32 size) {
157 char *ptr = (char*)buffer;
160 if(_gecko_checkrecv()) {
161 if(!_gecko_recvbyte(ptr))
167 return (size - left);
170 static int gecko_sendbuffer_safe(const void *buffer, u32 size) {
172 char *ptr = (char*)buffer;
175 if(_gecko_checksend()) {
176 if(!_gecko_sendbyte(*ptr))
182 return (size - left);
186 void gecko_init(void)
189 write32(0x0d00643c, 0);
191 write32(EXI0_CSR, 0);
192 write32(EXI1_CSR, 0);
193 write32(EXI2_CSR, 0);
194 write32(EXI0_CSR, 0x2000);
195 write32(EXI0_CSR, 3<<10);
196 write32(EXI1_CSR, 3<<10);
198 if (!gecko_isalive())
201 gecko_console_enabled = 1;
204 int printf(const char *fmt, ...) {
205 if (!gecko_console_enabled)
208 udelay(100); // <- evil hack :)
214 i = vsprintf(buffer, fmt, args);
217 return gecko_sendbuffer(buffer, i);