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 // macro for accessing u32 variables that need to be in little endian byte order;
22 // whenever you read or write from an u32 field that the ohci host controller
23 // will read or write from too, use this macro for access!
24 #define ACCESS_LE(dword) (u32)( (((dword) & 0xFF000000) >> 24) | \
25 (((dword) & 0x00FF0000) >> 8) | \
26 (((dword) & 0x0000FF00) << 8) | \
27 (((dword) & 0x000000FF) << 24) )
29 static struct endpoint_descriptor *allocate_endpoint();
30 static struct general_td *allocate_general_td(size_t);
31 static void control_quirk();
32 static void dbg_op_state();
33 static void dbg_td_flag(u32 flag);
34 static void configure_ports(u8 from_init);
35 static void setup_port(u32 reg, u8 from_init);
37 static struct ohci_hcca hcca_oh0;
40 static struct endpoint_descriptor *allocate_endpoint()
42 struct endpoint_descriptor *ep;
43 ep = (struct endpoint_descriptor *)memalign(16, sizeof(struct endpoint_descriptor));
44 ep->flags = ACCESS_LE(OHCI_ENDPOINT_GENERAL_FORMAT);
45 ep->headp = ep->tailp = ep->nexted = ACCESS_LE(0);
49 static struct general_td *allocate_general_td(size_t bsize)
51 struct general_td *td;
52 td = (struct general_td *)memalign(16, sizeof(struct general_td));
53 td->flags = ACCESS_LE(0);
54 td->nexttd = ACCESS_LE(0);
55 td->cbp = td->be = ACCESS_LE(0);
59 static void control_quirk()
61 static struct endpoint_descriptor *ed = 0; /* empty ED */
62 static struct general_td *td = 0; /* dummy TD */
69 * Allocate and keep a special empty ED with just a dummy TD.
72 ed = allocate_endpoint();
76 td = allocate_general_td(0);
83 #define ED_MASK ((u32)~0x0f)
84 ed->tailp = ed->headp = ACCESS_LE(virt_to_phys((void*) ((u32)td & ED_MASK)));
85 ed->flags |= ACCESS_LE(OHCI_ENDPOINT_DIRECTION_OUT);
89 * The OHCI USB host controllers on the Nintendo Wii
90 * video game console stop working when new TDs are
91 * added to a scheduled control ED after a transfer has
92 * has taken place on it.
94 * Before scheduling any new control TD, we make the
95 * controller happy by always loading a special control ED
96 * with a single dummy TD and letting the controller attempt
98 * The controller won't do anything with it, as the special
99 * ED has no TDs, but it will keep the controller from failing
100 * on the next transfer.
102 head = read32(OHCI0_HC_CTRL_HEAD_ED);
104 printf("head: 0x%08X\n", head);
106 * Load the special empty ED and tell the controller to
107 * process the control list.
109 sync_after_write(ed, 16);
110 sync_after_write(td, 16);
111 write32(OHCI0_HC_CTRL_HEAD_ED, virt_to_phys(ed));
113 status = read32(OHCI0_HC_CONTROL);
114 set32(OHCI0_HC_CONTROL, OHCI_CTRL_CLE);
115 write32(OHCI0_HC_COMMAND_STATUS, OHCI_CLF);
117 /* spin until the controller is done with the control list */
118 current = read32(OHCI0_HC_CTRL_CURRENT_ED);
121 current = read32(OHCI0_HC_CTRL_CURRENT_ED);
124 printf("current: 0x%08X\n", current);
126 /* restore the old control head and control settings */
127 write32(OHCI0_HC_CONTROL, status);
128 write32(OHCI0_HC_CTRL_HEAD_ED, head);
135 static void dbg_op_state()
137 switch (read32(OHCI0_HC_CONTROL) & OHCI_CTRL_HCFS) {
138 case OHCI_USB_SUSPEND:
139 printf("ohci-- OHCI_USB_SUSPEND\n");
142 printf("ohci-- OHCI_USB_RESET\n");
145 printf("ohci-- OHCI_USB_OPER\n");
147 case OHCI_USB_RESUME:
148 printf("ohci-- OHCI_USB_RESUME\n");
153 static void dbg_td_flag(u32 flag)
155 printf("**************** dbg_td_flag: 0x%08X ***************\n", flag);
156 printf("CC: %X\tshould be 0, see page 32 (ohci spec)\n", (flag>>28)&0xf);
157 printf("EC: %X\tsee page 20 (ohci spec)\n", (flag>>26)&3);
158 printf(" T: %X\n", (flag>>24)&3);
159 printf("DI: %X\n", (flag>>21)&7);
160 printf("DP: %X\n", (flag>>19)&3);
161 printf(" R: %X\n", (flag>>18)&1);
162 printf("********************************************************\n");
165 static void general_td_fill(struct general_td *dest, const usb_transfer_descriptor *src)
167 dest->cbp = ACCESS_LE(virt_to_phys(src->buffer));
168 dest->be = ACCESS_LE(ACCESS_LE(dest->cbp) + src->actlen - 1);
169 dest->flags &= ACCESS_LE(~OHCI_TD_DIRECTION_PID_MASK);
172 printf("pid_setup\n");
173 dest->flags |= ACCESS_LE(OHCI_TD_DIRECTION_PID_SETUP);
174 dest->flags |= ACCESS_LE(OHCI_TD_TOGGLE_0);
175 dest->flags |= ACCESS_LE(OHCI_TD_BUFFER_ROUNDING);
179 dest->flags |= ACCESS_LE(OHCI_TD_DIRECTION_PID_OUT);
180 dest->flags |= ACCESS_LE(OHCI_TD_BUFFER_ROUNDING);
183 * TODO: just temporary solution!
184 * there can be also regular PID_OUT pakets
186 dest->flags |= ACCESS_LE(OHCI_TD_TOGGLE_1);
190 dest->flags |= ACCESS_LE(OHCI_TD_DIRECTION_PID_IN);
191 dest->flags |= ACCESS_LE(OHCI_TD_BUFFER_ROUNDING);
193 * let the endpoint do the togglestuff!
194 * TODO: just temporary solution!
195 * there can be also inregular PID_IN pakets (@Status Stage)
197 dest->flags |= ACCESS_LE(OHCI_TD_TOGGLE_CARRY);
199 /* should be done by HC!
200 * first pid_in start with DATA0 */
202 dummyconfig.headp = ACCESS_LE( src->togl ?
203 ACCESS_LE(dummyconfig.headp) | OHCI_ENDPOINT_TOGGLE_CARRY :
204 ACCESS_LE(dummyconfig.headp) & ~OHCI_ENDPOINT_TOGGLE_CARRY);
208 dest->flags |= ACCESS_LE(OHCI_TD_SET_DELAY_INTERRUPT(7));
209 sync_after_write(dest, sizeof(struct general_td));
210 sync_after_write((void*) phys_to_virt(ACCESS_LE(dest->cbp)), src->actlen);
213 static void dump_address(void *addr, u32 size, const char* str)
215 sync_before_read(addr, size);
216 printf("%s hexdump @ 0x%08X:\n", str, addr);
221 * Enqueue a transfer descriptor.
224 u8 hcdi_enqueue(const usb_transfer_descriptor *td) {
225 static struct general_td *tSetup,*tData;
226 static u32 tSetupbuffer, tDatabuffer, tStatusbuffer;
227 static u32 tSetupblen, tDatablen, tStatusblen;
231 tSetup = allocate_general_td(td->actlen);
232 general_td_fill(tSetup, td);
233 tSetupbuffer = (u32) phys_to_virt(ACCESS_LE(tSetup->cbp));
234 tSetupblen = td->actlen;
240 tData = allocate_general_td(td->actlen);
241 general_td_fill(tData, td);
242 tDatabuffer = (u32) phys_to_virt(ACCESS_LE(tData->cbp));
243 tDatablen = td->actlen;
248 struct general_td *tStatus = allocate_general_td(td->actlen);
249 general_td_fill(tStatus, td);
250 tStatusbuffer = (u32) phys_to_virt(ACCESS_LE(tStatus->cbp));
251 tStatusblen = td->actlen;
253 printf( "===========================\n"
254 "===========================\n");
255 control_quirk(); //required? YES! :O ... erm... or no? :/ ... in fact I have no idea
257 struct endpoint_descriptor *dummyconfig = allocate_endpoint();
258 dummyconfig->flags = ACCESS_LE(OHCI_ENDPOINT_GENERAL_FORMAT);
259 dummyconfig->headp = dummyconfig->tailp = dummyconfig->nexted = ACCESS_LE(0);
260 dummyconfig->flags |= ACCESS_LE(OHCI_ENDPOINT_LOW_SPEED |
261 OHCI_ENDPOINT_SET_DEVICE_ADDRESS(td->devaddress) |
262 OHCI_ENDPOINT_SET_ENDPOINT_NUMBER(td->endpoint) |
263 OHCI_ENDPOINT_SET_MAX_PACKET_SIZE(td->maxp));
264 write32(OHCI0_HC_CTRL_HEAD_ED, virt_to_phys(dummyconfig));
266 #define ED_MASK ((u32)~0x0f)
267 dummyconfig->headp |= ACCESS_LE(virt_to_phys((void*) ((u32)tSetup & ED_MASK)));
268 tSetup->nexttd = ACCESS_LE(virt_to_phys((void*) ((u32)tData & ED_MASK)));
269 tData->nexttd = ACCESS_LE(virt_to_phys((void*) ((u32)tStatus & ED_MASK)));
271 sync_after_write(dummyconfig, 16);
272 sync_after_write(tSetup, sizeof(struct general_td));
273 sync_after_write(tData, sizeof(struct general_td));
274 sync_after_write(tStatus, sizeof(struct general_td));
276 dump_address(tSetup, sizeof(struct general_td), "tSetup(before)");
277 dump_address((void*) phys_to_virt(ACCESS_LE(tSetup->cbp)), tSetupblen, "tSetup->cbp(before)");
279 dump_address(tData, sizeof(struct general_td), "tData(before)");
280 dump_address((void*) phys_to_virt(ACCESS_LE(tData->cbp)), tDatablen, "tData->cbp(before)");
282 dump_address(tStatus, sizeof(struct general_td), "tStatus(before)");
283 dump_address((void*) phys_to_virt(ACCESS_LE(tStatus->cbp)), tStatusblen, "tStatus->cbp(before)");
285 dump_address(dummyconfig, sizeof(struct endpoint_descriptor), "dummyconfig(before)");
287 printf("ctrl head: 0x%08X\n", read32(OHCI0_HC_CTRL_HEAD_ED));
288 /* trigger control list */
289 set32(OHCI0_HC_CONTROL, OHCI_CTRL_CLE);
290 write32(OHCI0_HC_COMMAND_STATUS, OHCI_CLF);
292 //don't use this quirk stuff here!
294 while(!read32(OHCI0_HC_CTRL_CURRENT_ED)) {
299 u32 current = read32(OHCI0_HC_CTRL_CURRENT_ED);
300 printf("current: 0x%08X\n", current);
301 printf("+++++++++++++++++++++++++++++\n");
304 dump_address(tSetup, sizeof(struct general_td), "tSetup(after)");
305 dump_address((void*) phys_to_virt(ACCESS_LE(tSetup->cbp)), tSetupblen, "tSetup->cbp(after)");
306 dump_address((void*) tSetupbuffer, tSetupblen, "tSetupbuffer");
307 dbg_td_flag(ACCESS_LE(tSetup->flags));
309 dump_address(tData, sizeof(struct general_td), "tData(after)");
310 dump_address((void*) phys_to_virt(ACCESS_LE(tData->cbp)), tDatablen, "tData->cbp(after)");
311 dump_address((void*) tDatabuffer, tDatablen, "tDatabuffer");
312 dbg_td_flag(ACCESS_LE(tData->flags));
314 dump_address(tStatus, sizeof(struct general_td), "tStatus(after)");
315 //dump_address((void*) phys_to_virt(ACCESS_LE(tStatus->cbp)), tStatusblen, "tStatus->cbp(after)");
316 //dump_address((void*) tStatusbuffer, tStatusblen, "tStatusbuffer");
317 dbg_td_flag(ACCESS_LE(tStatus->flags));
319 dump_address(dummyconfig, sizeof(struct endpoint_descriptor), "dummyconfig(after)");
321 /* disable control list */
322 write32(OHCI0_HC_CONTROL, read32(OHCI0_HC_CONTROL)&~OHCI_CTRL_CLE);
325 * TD should be free'd after taking it from the done queue.
334 printf("hcdi_enqueue, done!\n");
339 * Remove an transfer descriptor from transfer queue.
341 u8 hcdi_dequeue(usb_transfer_descriptor *td) {
347 printf("ohci-- init\n");
350 /* disable hc interrupts */
351 set32(OHCI0_HC_INT_DISABLE, OHCI_INTR_MIE);
353 /* save fmInterval and calculate FSMPS */
354 #define FSMP(fi) (0x7fff & ((6 * ((fi) - 210)) / 7))
355 #define FI 0x2edf /* 12000 bits per frame (-1) */
356 u32 fmint = read32(OHCI0_HC_FM_INTERVAL) & 0x3fff;
358 printf("ohci-- fminterval delta: %d\n", fmint - FI);
359 fmint |= FSMP (fmint) << 16;
361 /* enable interrupts of both usb host controllers */
362 set32(EHCI_CTL, EHCI_CTL_OH0INTE | EHCI_CTL_OH1INTE | 0xe0000);
365 write32(OHCI0_HC_COMMAND_STATUS, OHCI_HCR);
369 while ((read32(OHCI0_HC_COMMAND_STATUS) & OHCI_HCR) != 0) {
371 printf("ohci-- FAILED");
377 /* disable interrupts; 2ms timelimit here!
378 now we're in the SUSPEND state ... must go OPERATIONAL
379 within 2msec else HC enters RESUME */
381 u32 cookie = irq_kill();
383 /* Tell the controller where the control and bulk lists are
384 * The lists are empty now. */
385 write32(OHCI0_HC_CTRL_HEAD_ED, 0);
386 write32(OHCI0_HC_BULK_HEAD_ED, 0);
388 /* set hcca adress */
389 sync_after_write(&hcca_oh0, 256);
390 write32(OHCI0_HC_HCCA, virt_to_phys(&hcca_oh0));
392 /* set periodicstart */
394 u32 fmInterval = read32(OHCI0_HC_FM_INTERVAL) &0x3fff;
395 u32 fit = read32(OHCI0_HC_FM_INTERVAL) & FIT;
397 write32(OHCI0_HC_FM_INTERVAL, fmint | (fit ^ FIT));
398 write32(OHCI0_HC_PERIODIC_START, ((9*fmInterval)/10)&0x3fff);
401 if ((read32(OHCI0_HC_FM_INTERVAL) & 0x3fff0000) == 0 || !read32(OHCI0_HC_PERIODIC_START)) {
402 printf("ohci-- w00t, fail!! see ohci-hcd.c:669\n");
405 /* start HC operations */
406 write32(OHCI0_HC_CONTROL, OHCI_CONTROL_INIT | OHCI_USB_OPER);
408 /* wake on ConnectStatusChange, matching external hubs */
409 write32(OHCI0_HC_RH_STATUS, /*RH_HS_DRWE |*/ RH_HS_LPSC);
411 /* Choose the interrupts we care about now, others later on demand */
412 write32(OHCI0_HC_INT_STATUS, ~0);
413 write32(OHCI0_HC_INT_ENABLE, OHCI_INTR_INIT);
416 wait_ms ((read32(OHCI0_HC_RH_DESCRIPTOR_A) >> 23) & 0x1fe);
418 configure_ports((u8)1);
424 static void configure_ports(u8 from_init)
426 printf("OHCI0_HC_RH_DESCRIPTOR_A:\t0x%08X\n", read32(OHCI0_HC_RH_DESCRIPTOR_A));
427 printf("OHCI0_HC_RH_DESCRIPTOR_B:\t0x%08X\n", read32(OHCI0_HC_RH_DESCRIPTOR_B));
428 printf("OHCI0_HC_RH_STATUS:\t\t0x%08X\n", read32(OHCI0_HC_RH_STATUS));
429 printf("OHCI0_HC_RH_PORT_STATUS_1:\t0x%08X\n", read32(OHCI0_HC_RH_PORT_STATUS_1));
430 printf("OHCI0_HC_RH_PORT_STATUS_2:\t0x%08X\n", read32(OHCI0_HC_RH_PORT_STATUS_2));
432 setup_port(OHCI0_HC_RH_PORT_STATUS_1, from_init);
433 setup_port(OHCI0_HC_RH_PORT_STATUS_2, from_init);
434 printf("configure_ports done\n");
437 static void setup_port(u32 reg, u8 from_init)
439 u32 port = read32(reg);
440 if((port & RH_PS_CCS) && ((port & RH_PS_CSC) || from_init)) {
441 write32(reg, RH_PS_CSC);
445 /* clear CSC flag, set PES and start port reset (PRS) */
446 write32(reg, RH_PS_PES);
447 while(!(read32(reg) & RH_PS_PES)) {
452 write32(reg, RH_PS_PRS);
454 /* spin until port reset is complete */
455 while(!(read32(reg) & RH_PS_PRSC)); // hint: it may stuck here
456 printf("loop done\n");
460 (void) usb_add_device();
466 /* read interrupt status */
467 u32 flags = read32(OHCI0_HC_INT_STATUS);
469 /* when all bits are set to 1 some problem occured */
470 if (flags == 0xffffffff) {
471 printf("ohci-- Houston, we have a serious problem! :(\n");
475 /* only care about interrupts that are enabled */
476 flags &= read32(OHCI0_HC_INT_ENABLE);
480 printf("OHCI Interrupt occured: but not for you! WTF?!\n");
484 printf("OHCI Interrupt occured: ");
485 /* UnrecoverableError */
486 if (flags & OHCI_INTR_UE) {
487 printf("UnrecoverableError\n");
488 /* TODO: well, I don't know... nothing,
489 * because it won't happen anyway? ;-) */
492 /* RootHubStatusChange */
493 if (flags & OHCI_INTR_RHSC) {
494 printf("RootHubStatusChange\n");
495 /* TODO: set some next_statechange variable... */
497 write32(OHCI0_HC_INT_STATUS, OHCI_INTR_RD | OHCI_INTR_RHSC);
500 else if (flags & OHCI_INTR_RD) {
501 printf("ResumeDetected\n");
502 write32(OHCI0_HC_INT_STATUS, OHCI_INTR_RD);
503 /* TODO: figure out what the linux kernel does here... */
506 /* WritebackDoneHead */
507 if (flags & OHCI_INTR_WDH) {
508 printf("WritebackDoneHead\n");
509 /* basically the linux irq handler reverse TDs to their urbs
510 * and set done_head to null.
511 * since we are polling atm, just should do the latter task.
512 * however, this won't work for now (i don't know why...)
516 sync_before_read(&hcca_oh0, 256);
517 hcca_oh0.done_head = 0;
518 sync_after_write(&hcca_oh0, 256);
522 /* TODO: handle any pending URB/ED unlinks... */
524 #define HC_IS_RUNNING() 1 /* dirty, i know... just a temporary solution */
525 if (HC_IS_RUNNING()) {
526 write32(OHCI0_HC_INT_STATUS, flags);
527 write32(OHCI0_HC_INT_ENABLE, OHCI_INTR_MIE);
533 sync_before_read(&hcca_oh0, 256);
534 printf("***** frame_no: %d *****\n", ACCESS_LE(hcca_oh0.frame_no));