2 ppcskel - a Free Software replacement for the Nintendo/BroadOn bootloader.
5 Copyright (C) 2009 Bernhard Urban <lewurm@gmx.net>
6 Copyright (C) 2009 Sebastian Falbesoner <sebastian.falbesoner@gmail.com>
8 # This code is licensed to you under the terms of the GNU GPL, version 2;
9 # see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
12 #include "../../bootmii_ppc.h"
13 #include "../../hollywood.h"
14 #include "../../irq.h"
15 #include "../../string.h"
16 #include "../../malloc.h"
19 #include "../usbspec/usb11spec.h"
21 static struct ohci_hcca hcca_oh0;
23 static struct endpoint_descriptor *allocate_endpoint()
25 struct endpoint_descriptor *ep;
26 ep = (struct endpoint_descriptor *)calloc(sizeof(struct endpoint_descriptor), 16);
27 ep->flags = OHCI_ENDPOINT_GENERAL_FORMAT;
28 ep->headp = ep->tailp = ep->nexted = 0;
32 static struct general_td *allocate_general_td(size_t bsize)
34 struct general_td *td;
35 td = (struct general_td *)calloc(sizeof(struct general_td), 16);
37 td->nexttd = virt_to_phys(td);
41 td->cbp = virt_to_phys(malloc(bsize));
42 td->be = td->cbp + bsize - 1;
47 static void control_quirk()
49 static struct endpoint_descriptor *ed; /* empty ED */
50 static struct general_td *td; /* dummy TD */
57 * Allocate and keep a special empty ED with just a dummy TD.
60 ed = allocate_endpoint();
64 td = allocate_general_td(0);
71 #define ED_MASK ((u32)~0x0f)
72 ed->tailp = ed->headp = virt_to_phys((void*) ((u32)td & ED_MASK));
73 ed->flags |= OHCI_ENDPOINT_DIRECTION_OUT;
77 * The OHCI USB host controllers on the Nintendo Wii
78 * video game console stop working when new TDs are
79 * added to a scheduled control ED after a transfer has
80 * has taken place on it.
82 * Before scheduling any new control TD, we make the
83 * controller happy by always loading a special control ED
84 * with a single dummy TD and letting the controller attempt
86 * The controller won't do anything with it, as the special
87 * ED has no TDs, but it will keep the controller from failing
88 * on the next transfer.
90 head = read32(OHCI0_HC_CTRL_HEAD_ED);
92 printf("head: 0x%08X\n", head);
94 * Load the special empty ED and tell the controller to
95 * process the control list.
97 sync_after_write(ed, 64);
98 sync_after_write(td, 64);
99 write32(OHCI0_HC_CTRL_HEAD_ED, virt_to_phys(ed));
101 status = read32(OHCI0_HC_CONTROL);
102 set32(OHCI0_HC_CONTROL, OHCI_CTRL_CLE);
103 write32(OHCI0_HC_COMMAND_STATUS, OHCI_CLF);
105 /* spin until the controller is done with the control list */
106 current = read32(OHCI0_HC_CTRL_CURRENT_ED);
109 current = read32(OHCI0_HC_CTRL_CURRENT_ED);
112 printf("current: 0x%08X\n", current);
114 /* restore the old control head and control settings */
115 write32(OHCI0_HC_CONTROL, status);
116 write32(OHCI0_HC_CTRL_HEAD_ED, head);
123 static void dbg_op_state()
125 switch (read32(OHCI0_HC_CONTROL) & OHCI_CTRL_HCFS) {
126 case OHCI_USB_SUSPEND:
127 printf("ohci-- OHCI_USB_SUSPEND\n");
130 printf("ohci-- OHCI_USB_RESET\n");
133 printf("ohci-- OHCI_USB_OPER\n");
135 case OHCI_USB_RESUME:
136 printf("ohci-- OHCI_USB_RESUME\n");
143 * Enqueue a transfer descriptor.
145 u8 hcdi_enqueue(usb_transfer_descriptor *td) {
148 printf( "===========================\n"
149 "===========================\n"
150 "done head (vor sync): 0x%08X\n", hcca_oh0.done_head);
151 sync_before_read(&hcca_oh0, 256);
152 printf("done head (nach sync): 0x%08X\n", hcca_oh0.done_head);
154 struct general_td *tmptd = allocate_general_td(td->actlen);
155 (void) memcpy((void*) phys_to_virt(tmptd->cbp), td->buffer, td->actlen); /* throws dsi exception after some time :X */
157 tmptd->flags &= ~OHCI_TD_DIRECTION_PID_MASK;
160 printf("pid_setup\n");
161 tmptd->flags |= OHCI_TD_DIRECTION_PID_SETUP;
165 tmptd->flags |= OHCI_TD_DIRECTION_PID_OUT;
169 tmptd->flags |= OHCI_TD_DIRECTION_PID_IN;
172 tmptd->flags |= (td->togl) ? OHCI_TD_TOGGLE_1 : OHCI_TD_TOGGLE_0;
174 printf("tmptd hexump (before):\n");
175 hexdump(tmptd, sizeof(struct general_td));
176 printf("tmptd-cbp hexump (before):\n");
177 hexdump((void*) phys_to_virt(tmptd->cbp), td->actlen);
179 sync_after_write((void*) (tmptd->cbp), td->actlen);
180 sync_after_write(tmptd, sizeof(struct general_td));
182 struct endpoint_descriptor *dummyconfig = allocate_endpoint();
184 u32 current2 = read32(OHCI0_HC_CTRL_CURRENT_ED);
185 printf("current2: 0x%08X\n", current2);
187 #define ED_MASK2 ~0 /*((u32)~0x0f) */
188 #define ED_MASK ((u32)~0x0f)
189 printf("tmpdt & ED_MASK: 0x%08X\n", virt_to_phys((void*) ((u32)tmptd & ED_MASK)));
190 dummyconfig->tailp = dummyconfig->headp = virt_to_phys((void*) ((u32)tmptd & ED_MASK));
192 dummyconfig->flags |= OHCI_ENDPOINT_LOW_SPEED |
193 OHCI_ENDPOINT_SET_DEVICE_ADDRESS(td->devaddress) |
194 OHCI_ENDPOINT_SET_ENDPOINT_NUMBER(td->endpoint) |
195 OHCI_ENDPOINT_SET_MAX_PACKET_SIZE(td->maxp);
197 sync_after_write(dummyconfig, 64);
198 write32(OHCI0_HC_CTRL_HEAD_ED, virt_to_phys(dummyconfig));
200 printf("OHCI_CTRL_CLE: 0x%08X\n", read32(OHCI0_HC_CONTROL)&OHCI_CTRL_CLE);
201 printf("OHCI_CLF: 0x%08X\n", read32(OHCI0_HC_COMMAND_STATUS)&OHCI_CLF);
202 set32(OHCI0_HC_CONTROL, OHCI_CTRL_CLE);
203 write32(OHCI0_HC_COMMAND_STATUS, OHCI_CLF);
205 printf("+++++++++++++++++++++++++++++\n");
206 /* spin until the controller is done with the control list */
207 u32 current = read32(OHCI0_HC_CTRL_CURRENT_ED);
208 printf("current: 0x%08X\n", current);
211 current = read32(OHCI0_HC_CTRL_CURRENT_ED);
217 current = read32(OHCI0_HC_CTRL_CURRENT_ED);
218 printf("current: 0x%08X\n", current);
219 printf("+++++++++++++++++++++++++++++\n");
229 sync_before_read(tmptd, sizeof(struct general_td));
230 printf("tmptd hexump (after):\n");
231 hexdump(tmptd, sizeof(struct general_td));
233 sync_before_read((void*) (tmptd->cbp), td->actlen);
234 printf("tmptd-cbp hexump (after):\n");
235 hexdump((void*) phys_to_virt(tmptd->cbp), td->actlen);
237 printf("done head (vor sync): 0x%08X\n", hcca_oh0.done_head);
238 sync_before_read(&hcca_oh0, 256);
239 printf("done head (nach sync): 0x%08X\n", hcca_oh0.done_head);
246 * Remove an transfer descriptor from transfer queue.
248 u8 hcdi_dequeue(usb_transfer_descriptor *td) {
254 printf("ohci-- init\n");
257 /* disable hc interrupts */
258 set32(OHCI0_HC_INT_DISABLE, OHCI_INTR_MIE);
260 /* save fmInterval and calculate FSMPS */
261 #define FSMP(fi) (0x7fff & ((6 * ((fi) - 210)) / 7))
262 #define FI 0x2edf /* 12000 bits per frame (-1) */
263 u32 fmint = read32(OHCI0_HC_FM_INTERVAL) & 0x3fff;
265 printf("ohci-- fminterval delta: %d\n", fmint - FI);
266 fmint |= FSMP (fmint) << 16;
268 /* enable interrupts of both usb host controllers */
269 set32(EHCI_CTL, EHCI_CTL_OH0INTE | EHCI_CTL_OH1INTE | 0xe0000);
272 write32(OHCI0_HC_COMMAND_STATUS, OHCI_HCR);
276 while ((read32(OHCI0_HC_COMMAND_STATUS) & OHCI_HCR) != 0) {
278 printf("ohci-- FAILED");
284 /* disable interrupts; 2ms timelimit here!
285 now we're in the SUSPEND state ... must go OPERATIONAL
286 within 2msec else HC enters RESUME */
288 u32 cookie = irq_kill();
290 /* Tell the controller where the control and bulk lists are
291 * The lists are empty now. */
292 write32(OHCI0_HC_CTRL_HEAD_ED, 0);
293 write32(OHCI0_HC_BULK_HEAD_ED, 0);
295 /* set hcca adress */
296 sync_after_write(&hcca_oh0, 256);
297 write32(OHCI0_HC_HCCA, virt_to_phys(&hcca_oh0));
299 /* set periodicstart */
301 u32 fmInterval = read32(OHCI0_HC_FM_INTERVAL) &0x3fff;
302 u32 fit = read32(OHCI0_HC_FM_INTERVAL) & FIT;
304 write32(OHCI0_HC_FM_INTERVAL, fmint | (fit ^ FIT));
305 write32(OHCI0_HC_PERIODIC_START, ((9*fmInterval)/10)&0x3fff);
308 if ((read32(OHCI0_HC_FM_INTERVAL) & 0x3fff0000) == 0 || !read32(OHCI0_HC_PERIODIC_START)) {
309 printf("ohci-- w00t, fail!! see ohci-hcd.c:669\n");
312 /* start HC operations */
313 write32(OHCI0_HC_CONTROL, OHCI_CONTROL_INIT | OHCI_USB_OPER);
315 /* wake on ConnectStatusChange, matching external hubs */
316 set32(OHCI0_HC_RH_STATUS, RH_HS_DRWE);
318 /* Choose the interrupts we care about now, others later on demand */
319 write32(OHCI0_HC_INT_STATUS, ~0);
320 write32(OHCI0_HC_INT_ENABLE, OHCI_INTR_INIT);
329 /* read interrupt status */
330 u32 flags = read32(OHCI0_HC_INT_STATUS);
332 /* when all bits are set to 1 some problem occured */
333 if (flags == 0xffffffff) {
334 printf("ohci-- Houston, we have a serious problem! :(\n");
338 /* only care about interrupts that are enabled */
339 flags &= read32(OHCI0_HC_INT_ENABLE);
345 printf("OHCI Interrupt occured: ");
346 /* UnrecoverableError */
347 if (flags & OHCI_INTR_UE) {
348 printf("UnrecoverableError\n");
349 /* TODO: well, I don't know... nothing,
350 * because it won't happen anyway? ;-) */
353 /* RootHubStatusChange */
354 if (flags & OHCI_INTR_RHSC) {
355 printf("RootHubStatusChange\n");
356 /* TODO: set some next_statechange variable... */
357 write32(OHCI0_HC_INT_STATUS, OHCI_INTR_RD | OHCI_INTR_RHSC);
360 else if (flags & OHCI_INTR_RD) {
361 printf("ResumeDetected\n");
362 write32(OHCI0_HC_INT_STATUS, OHCI_INTR_RD);
363 /* TODO: figure out what the linux kernel does here... */
366 /* WritebackDoneHead */
367 if (flags & OHCI_INTR_WDH) {
368 printf("WritebackDoneHead\n");
369 /* TODO: figure out what the linux kernel does here... */
372 /* TODO: handle any pending URB/ED unlinks... */
374 #define HC_IS_RUNNING() 1 /* dirty, i know... just a temporary solution */
375 if (HC_IS_RUNNING()) {
376 write32(OHCI0_HC_INT_STATUS, flags);
377 write32(OHCI0_HC_INT_ENABLE, OHCI_INTR_MIE);