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 /* activate control_quirk */
24 /* macro for accessing u32 variables that need to be in little endian byte order;
26 * whenever you read or write from an u32 field that the ohci host controller
27 * will read or write from too, use this macro for access!
29 #define LE(dword) (u32)( (((dword) & 0xFF000000) >> 24) | \
30 (((dword) & 0x00FF0000) >> 8) | \
31 (((dword) & 0x0000FF00) << 8) | \
32 (((dword) & 0x000000FF) << 24) )
34 static struct general_td *allocate_general_td();
35 static void dbg_op_state(u32 reg);
36 static void configure_ports(u8 from_init, u32 reg);
37 static void setup_port(u32 ohci, u32 reg, u8 from_init);
39 static struct ohci_hcca hcca_oh0;
40 static struct ohci_hcca hcca_oh1;
43 static struct general_td *allocate_general_td()
45 struct general_td *td;
46 td = (struct general_td *)memalign(16, sizeof(struct general_td));
47 memset(td, 0, sizeof(struct general_td));
50 td->cbp = td->be = LE(0);
55 static void dbg_op_state(u32 reg)
57 switch (read32(reg+OHCI_HC_CONTROL) & OHCI_CTRL_HCFS) {
58 case OHCI_USB_SUSPEND:
59 printf("ohci-- OHCI_USB_SUSPEND\n");
62 printf("ohci-- OHCI_USB_RESET\n");
65 printf("ohci-- OHCI_USB_OPER\n");
68 printf("ohci-- OHCI_USB_RESUME\n");
73 #ifdef _DU_OHCI_F_HALT
74 static void dbg_td_flag(u32 flag)
76 printf("**************** dbg_td_flag: 0x%08X ***************\n", flag);
77 printf("CC: %X\tshould be 0, see page 32 (ohci spec)\n", (flag>>28)&0xf);
78 printf("EC: %X\tsee page 20 (ohci spec)\n", (flag>>26)&3);
79 printf(" T: %X\n", (flag>>24)&3);
80 printf("DI: %X\n", (flag>>21)&7);
81 printf("DP: %X\n", (flag>>19)&3);
82 printf(" R: %X\n", (flag>>18)&1);
83 printf("********************************************************\n");
87 static void general_td_fill(struct general_td *dest, const struct usb_transfer_descriptor *src)
90 dest->cbp = LE(virt_to_phys(src->buffer));
91 dest->be = LE(LE(dest->cbp) + src->actlen - 1);
92 /* save virtual address here */
93 dest->bufaddr = (u32) src->buffer;
96 dest->cbp = dest->be = LE(0);
100 dest->buflen = src->actlen;
102 dest->flags &= LE(~OHCI_TD_DIRECTION_PID_MASK);
106 printf("pid_setup\n");
108 dest->flags |= LE(OHCI_TD_DIRECTION_PID_SETUP);
109 dest->flags |= LE(OHCI_TD_TOGGLE_0);
110 dest->flags |= LE(OHCI_TD_BUFFER_ROUNDING);
116 dest->flags |= LE(OHCI_TD_DIRECTION_PID_OUT);
117 dest->flags |= LE(OHCI_TD_BUFFER_ROUNDING);
119 dest->flags |= src->togl ? LE(OHCI_TD_TOGGLE_1) : LE(OHCI_TD_TOGGLE_0);
125 dest->flags |= LE(OHCI_TD_DIRECTION_PID_IN);
126 if(src->maxp > src->actlen) {
127 dest->flags |= LE(OHCI_TD_BUFFER_ROUNDING);
129 printf("round buffer!\n");
132 dest->flags |= src->togl ? LE(OHCI_TD_TOGGLE_1) : LE(OHCI_TD_TOGGLE_0);
135 dest->flags |= LE(OHCI_TD_SET_DELAY_INTERRUPT(7));
138 #ifdef _DU_OHCI_F_HALT
139 static void dump_address(void *addr, u32 size, const char* str)
141 printf("%s hexdump (%d) @ 0x%08X:\n", str, size, addr);
146 static struct endpoint_descriptor _edhead;
147 struct endpoint_descriptor *edhead = 0;
148 void hcdi_fire(u32 reg)
151 printf("<^> <^> <^> hcdi_fire(start)\n");
158 /* quirk... 11ms seems to be a minimum :O */
162 write32(reg+OHCI_HC_CTRL_HEAD_ED, virt_to_phys(edhead));
165 sync_after_write(edhead, sizeof(struct endpoint_descriptor));
167 dump_address(edhead, sizeof(struct endpoint_descriptor), "edhead(before)");
170 struct general_td *x = phys_to_virt(LE(edhead->headp) & OHCI_ENDPOINT_HEAD_MASK);
171 while(virt_to_phys(x)) {
172 sync_after_write(x, sizeof(struct general_td));
174 dump_address(x, sizeof(struct general_td), "x(before)");
178 sync_after_write((void*) phys_to_virt(LE(x->cbp)), x->buflen);
180 dump_address((void*) phys_to_virt(LE(x->cbp)), x->buflen, "x->cbp(before)");
183 x = phys_to_virt(LE(x->nexttd));
186 /* trigger control list */
187 set32(reg+OHCI_HC_CONTROL, OHCI_CTRL_CLE);
188 write32(reg+OHCI_HC_COMMAND_STATUS, OHCI_CLF);
190 struct general_td *n=0, *prev = 0, *next = 0;
191 /* poll until edhead->headp is null */
193 sync_before_read(edhead, sizeof(struct endpoint_descriptor));
195 printf("edhead->headp: 0x%08X\n", LE(edhead->headp));
199 /* if halted, debug output plz. will break the transfer */
200 if((LE(edhead->headp) & OHCI_ENDPOINT_HALTED)) {
201 n = phys_to_virt(LE(edhead->headp)&~0xf);
202 prev = phys_to_virt((u32)prev);
203 #ifdef _DU_OHCI_F_HALT
207 sync_before_read((void*) n, sizeof(struct general_td));
208 #ifdef _DU_OHCI_F_HALT
209 printf("n: 0x%08X\n", n);
210 dump_address(n, sizeof(struct general_td), "n(after)");
213 sync_before_read((void*) n->bufaddr, n->buflen);
214 #ifdef _DU_OHCI_F_HALT
215 dump_address((void*) n->bufaddr, n->buflen, "n->bufaddr(after)");
218 #ifdef _DU_OHCI_F_HALT
219 dbg_td_flag(LE(n->flags));
222 sync_before_read((void*) prev, sizeof(struct general_td));
223 #ifdef _DU_OHCI_F_HALT
224 printf("prev: 0x%08X\n", prev);
225 dump_address(prev, sizeof(struct general_td), "prev(after)");
227 if(prev->buflen >0) {
228 sync_before_read((void*) prev->bufaddr, prev->buflen);
229 #ifdef _DU_OHCI_F_HALT
230 dump_address((void*) prev->bufaddr, prev->buflen, "prev->bufaddr(after)");
233 #ifdef _DU_OHCI_F_HALT
234 dbg_td_flag(LE(prev->flags));
235 printf("halted end!\n");
239 prev = (struct general_td*) (LE(edhead->headp)&~0xf);
240 } while(LE(edhead->headp)&~0xf);
242 n = phys_to_virt(read32(reg+OHCI_HC_DONE_HEAD) & ~1);
244 printf("hc_done_head: 0x%08X\n", read32(reg+OHCI_HC_DONE_HEAD));
248 /* reverse done queue */
249 while(virt_to_phys(n) && edhead->tdcount) {
250 sync_before_read((void*) n, sizeof(struct general_td));
252 printf("n: 0x%08X\n", n);
253 printf("next: 0x%08X\n", next);
254 printf("prev: 0x%08X\n", prev);
258 n = (struct general_td*) phys_to_virt(LE(n->nexttd));
259 next->nexttd = (u32) prev;
267 while(virt_to_phys(n)) {
269 dump_address(n, sizeof(struct general_td), "n(after)");
272 sync_before_read((void*) n->bufaddr, n->buflen);
274 dump_address((void*) n->bufaddr, n->buflen, "n->bufaddr(after)");
278 dbg_td_flag(LE(n->flags));
281 n = (struct general_td*) n->nexttd;
285 if(reg == OHCI0_REG_BASE) {
286 hcca_oh0.done_head = 0;
287 sync_after_write(&hcca_oh0, sizeof(hcca_oh0));
288 } else if (reg == OHCI1_REG_BASE) {
289 hcca_oh1.done_head = 0;
290 sync_after_write(&hcca_oh1, sizeof(hcca_oh1));
294 write32(reg+OHCI_HC_CONTROL, read32(reg+OHCI_HC_CONTROL)&~OHCI_CTRL_CLE);
299 printf("<^> <^> <^> hcdi_fire(end)\n");
304 * Enqueue a transfer descriptor.
306 u8 hcdi_enqueue(const struct usb_transfer_descriptor *td, u32 reg) {
308 printf("*()*()*()*()*()*()*() hcdi_enqueue(start)\n");
312 memset(edhead, 0, sizeof(struct endpoint_descriptor));
313 edhead->flags = LE(OHCI_ENDPOINT_GENERAL_FORMAT);
314 edhead->headp = edhead->tailp = edhead->nexted = LE(0);
316 edhead->flags |= LE(OHCI_ENDPOINT_FULL_SPEED);
318 edhead->flags |= LE(OHCI_ENDPOINT_LOW_SPEED);
320 edhead->flags |= LE(OHCI_ENDPOINT_SET_DEVICE_ADDRESS(td->devaddress) |
321 OHCI_ENDPOINT_SET_ENDPOINT_NUMBER(td->endpoint) |
322 OHCI_ENDPOINT_SET_MAX_PACKET_SIZE(td->maxp));
326 struct general_td *tdhw = allocate_general_td();
327 general_td_fill(tdhw, td);
332 edhead->headp = LE(virt_to_phys((void*) ((u32)tdhw & OHCI_ENDPOINT_HEAD_MASK)));
335 /* headp in endpoint already exists
338 struct general_td *n = (struct general_td*) phys_to_virt(LE(edhead->headp) & OHCI_ENDPOINT_HEAD_MASK);
339 while(LE(n->nexttd)) {
340 n = phys_to_virt(LE(n->nexttd));
342 n->nexttd = LE(virt_to_phys((void*) ((u32)tdhw & OHCI_ENDPOINT_HEAD_MASK)));
344 printf("n: 0x%08X\n", n);
345 printf("n->nexttd: 0x%08X\n", phys_to_virt(LE(n->nexttd)));
350 printf("*()*()*()*()*()*()*() hcdi_enqueue(end)\n");
357 * Remove an transfer descriptor from transfer queue.
359 u8 hcdi_dequeue(struct usb_transfer_descriptor *td, u32 reg) {
363 void hcdi_init(u32 reg)
365 printf("ohci-- init\n");
368 /* disable hc interrupts */
369 set32(reg+OHCI_HC_INT_DISABLE, OHCI_INTR_MIE);
371 /* save fmInterval and calculate FSMPS */
372 #define FSMP(fi) (0x7fff & ((6 * ((fi) - 210)) / 7))
373 #define FI 0x2edf /* 12000 bits per frame (-1) */
374 u32 fmint = read32(reg+OHCI_HC_FM_INTERVAL) & 0x3fff;
376 printf("ohci-- fminterval delta: %d\n", fmint - FI);
377 fmint |= FSMP (fmint) << 16;
379 /* enable interrupts of both usb host controllers */
380 set32(EHCI_CTL, EHCI_CTL_OH0INTE | EHCI_CTL_OH1INTE | 0xe0000);
383 write32(reg+OHCI_HC_COMMAND_STATUS, OHCI_HCR);
387 while ((read32(reg+OHCI_HC_COMMAND_STATUS) & OHCI_HCR) != 0) {
389 printf("ohci-- FAILED");
395 /* disable interrupts; 2ms timelimit here!
396 now we're in the SUSPEND state ... must go OPERATIONAL
397 within 2msec else HC enters RESUME */
399 u32 cookie = irq_kill();
401 /* Tell the controller where the control and bulk lists are
402 * The lists are empty now. */
403 write32(reg+OHCI_HC_CTRL_HEAD_ED, 0);
404 write32(reg+OHCI_HC_BULK_HEAD_ED, 0);
406 /* set hcca adress */
407 if(reg == OHCI0_REG_BASE) {
408 sync_after_write(&hcca_oh0, 256);
409 write32(reg+OHCI_HC_HCCA, virt_to_phys(&hcca_oh0));
411 sync_after_write(&hcca_oh1, 256);
412 write32(reg+OHCI_HC_HCCA, virt_to_phys(&hcca_oh1));
415 /* set periodicstart */
417 u32 fmInterval = read32(reg+OHCI_HC_FM_INTERVAL) &0x3fff;
418 u32 fit = read32(reg+OHCI_HC_FM_INTERVAL) & FIT;
420 write32(reg+OHCI_HC_FM_INTERVAL, fmint | (fit ^ FIT));
421 write32(reg+OHCI_HC_PERIODIC_START, ((9*fmInterval)/10)&0x3fff);
424 if ((read32(reg+OHCI_HC_FM_INTERVAL) & 0x3fff0000) == 0 || !read32(reg+OHCI_HC_PERIODIC_START)) {
425 printf("ohci-- w00t, fail!! see ohci-hcd.c:669\n");
428 /* start HC operations */
429 write32(reg+OHCI_HC_CONTROL, OHCI_CONTROL_INIT | OHCI_USB_OPER);
431 /* wake on ConnectStatusChange, matching external hubs */
432 write32(reg+OHCI_HC_RH_STATUS, /*RH_HS_DRWE |*/ RH_HS_LPSC);
434 /* Choose the interrupts we care about now, others later on demand */
435 write32(reg+OHCI_HC_INT_STATUS, ~0);
436 write32(reg+OHCI_HC_INT_ENABLE, OHCI_INTR_INIT);
439 wait_ms ((read32(reg+OHCI_HC_RH_DESCRIPTOR_A) >> 23) & 0x1fe);
441 configure_ports((u8)1, reg);
447 static void configure_ports(u8 from_init, u32 reg)
450 printf("=== Roothub @ %s ===\n", reg == OHCI0_REG_BASE ? "OHCI0" : "OHCI1");
451 printf("OHCI_HC_RH_DESCRIPTOR_A:\t0x%08X\n", read32(reg+OHCI_HC_RH_DESCRIPTOR_A));
452 printf("OHCI_HC_RH_DESCRIPTOR_B:\t0x%08X\n", read32(reg+OHCI_HC_RH_DESCRIPTOR_B));
453 printf("OHCI_HC_RH_STATUS:\t\t0x%08X\n", read32(reg+OHCI_HC_RH_STATUS));
454 printf("OHCI_HC_RH_PORT_STATUS_1:\t0x%08X\n", read32(reg+OHCI_HC_RH_PORT_STATUS_1));
455 printf("OHCI_HC_RH_PORT_STATUS_2:\t0x%08X\n", read32(reg+OHCI_HC_RH_PORT_STATUS_2));
458 setup_port(reg, reg+OHCI_HC_RH_PORT_STATUS_1, from_init);
459 setup_port(reg, reg+OHCI_HC_RH_PORT_STATUS_2, from_init);
461 printf("configure_ports done\n");
465 static void setup_port(u32 ohci, u32 reg, u8 from_init)
467 u32 port = read32(reg);
468 if((port & RH_PS_CCS) && ((port & RH_PS_CSC) || from_init)) {
469 write32(reg, RH_PS_CSC);
473 /* clear CSC flag, set PES and start port reset (PRS) */
474 write32(reg, RH_PS_PES);
475 while(!(read32(reg) & RH_PS_PES)) {
482 write32(reg, RH_PS_PRS);
484 /* spin until port reset is complete */
485 while(!(read32(reg) & RH_PS_PRSC)); // hint: it may stuck here
487 printf("loop done\n");
490 /* returns usb_device struct */
491 (void) usb_add_device((read32(reg) & RH_PS_LSDA) >> 8, ohci);
495 void hcdi_irq(u32 reg)
497 /* read interrupt status */
498 u32 flags = read32(reg+OHCI_HC_INT_STATUS);
500 /* when all bits are set to 1 some problem occured */
501 if (flags == 0xffffffff) {
502 printf("ohci-- Houston, we have a serious problem! :(\n");
506 /* only care about interrupts that are enabled */
507 flags &= read32(reg+OHCI_HC_INT_ENABLE);
511 printf("OHCI Interrupt occured: but not for you! WTF?!\n");
515 printf("OHCI Interrupt occured: ");
516 /* UnrecoverableError */
517 if (flags & OHCI_INTR_UE) {
518 printf("UnrecoverableError\n");
519 /* TODO: well, I don't know... nothing,
520 * because it won't happen anyway? ;-) */
523 /* RootHubStatusChange */
524 if (flags & OHCI_INTR_RHSC) {
525 printf("RootHubStatusChange\n");
526 /* TODO: set some next_statechange variable... */
527 configure_ports(0, reg);
528 write32(reg+OHCI_HC_INT_STATUS, OHCI_INTR_RD | OHCI_INTR_RHSC);
531 else if (flags & OHCI_INTR_RD) {
532 printf("ResumeDetected\n");
533 write32(reg+OHCI_HC_INT_STATUS, OHCI_INTR_RD);
534 /* TODO: figure out what the linux kernel does here... */
537 /* WritebackDoneHead */
538 if (flags & OHCI_INTR_WDH) {
539 printf("WritebackDoneHead\n");
540 /* basically the linux irq handler reverse TDs to their urbs
541 * and set done_head to null.
542 * since we are polling atm, just should do the latter task.
543 * however, this won't work for now (i don't know why...)
547 sync_before_read(&hcca_oh0, 256);
548 hcca_oh0.done_head = 0;
549 sync_after_write(&hcca_oh0, 256);
553 /* TODO: handle any pending URB/ED unlinks... */
555 #define HC_IS_RUNNING() 1 /* dirty, i know... just a temporary solution */
556 if (HC_IS_RUNNING()) {
557 write32(reg+OHCI_HC_INT_STATUS, flags);
558 write32(reg+OHCI_HC_INT_ENABLE, OHCI_INTR_MIE);
562 void show_frame_no(u32 reg)
564 if(reg == OHCI0_REG_BASE) {
565 sync_before_read(&hcca_oh0, 256);
566 printf("***** frame_no: %d *****\n", LE(hcca_oh0.frame_no));
567 } else if (reg == OHCI1_REG_BASE) {
568 sync_before_read(&hcca_oh1, 256);
569 printf("***** frame_no: %d *****\n", LE(hcca_oh1.frame_no));