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);
120 * TODO: just temporary solution! (consider it with len?)
121 * there can be also regular PID_OUT pakets
123 dest->flags |= LE(OHCI_TD_TOGGLE_1);
129 dest->flags |= LE(OHCI_TD_DIRECTION_PID_IN);
130 if(src->maxp > src->actlen) {
131 dest->flags |= LE(OHCI_TD_BUFFER_ROUNDING);
133 printf("round buffer!\n");
137 * let the endpoint do the togglestuff!
138 * TODO: just temporary solution!
139 * there can be also inregular PID_IN pakets (@Status Stage)
141 dest->flags |= LE(OHCI_TD_TOGGLE_CARRY);
144 dest->flags |= LE(OHCI_TD_SET_DELAY_INTERRUPT(7));
147 #ifdef _DU_OHCI_F_HALT
148 static void dump_address(void *addr, u32 size, const char* str)
150 printf("%s hexdump (%d) @ 0x%08X:\n", str, size, addr);
155 static struct endpoint_descriptor _edhead;
156 struct endpoint_descriptor *edhead = 0;
157 void hcdi_fire(u32 reg)
160 printf("<^> <^> <^> hcdi_fire(start)\n");
167 /* quirk... 11ms seems to be a minimum :O */
171 write32(reg+OHCI_HC_CTRL_HEAD_ED, virt_to_phys(edhead));
174 sync_after_write(edhead, sizeof(struct endpoint_descriptor));
176 dump_address(edhead, sizeof(struct endpoint_descriptor), "edhead(before)");
179 struct general_td *x = phys_to_virt(LE(edhead->headp) & OHCI_ENDPOINT_HEAD_MASK);
180 while(virt_to_phys(x)) {
181 sync_after_write(x, sizeof(struct general_td));
183 dump_address(x, sizeof(struct general_td), "x(before)");
187 sync_after_write((void*) phys_to_virt(LE(x->cbp)), x->buflen);
189 dump_address((void*) phys_to_virt(LE(x->cbp)), x->buflen, "x->cbp(before)");
192 x = phys_to_virt(LE(x->nexttd));
195 /* trigger control list */
196 set32(reg+OHCI_HC_CONTROL, OHCI_CTRL_CLE);
197 write32(reg+OHCI_HC_COMMAND_STATUS, OHCI_CLF);
199 struct general_td *n=0, *prev = 0, *next = 0;
200 /* poll until edhead->headp is null */
202 sync_before_read(edhead, sizeof(struct endpoint_descriptor));
204 printf("edhead->headp: 0x%08X\n", LE(edhead->headp));
208 /* if halted, debug output plz. will break the transfer */
209 if((LE(edhead->headp) & OHCI_ENDPOINT_HALTED)) {
210 n = phys_to_virt(LE(edhead->headp)&~0xf);
211 prev = phys_to_virt((u32)prev);
212 #ifdef _DU_OHCI_F_HALT
216 sync_before_read((void*) n, sizeof(struct general_td));
217 #ifdef _DU_OHCI_F_HALT
218 printf("n: 0x%08X\n", n);
219 dump_address(n, sizeof(struct general_td), "n(after)");
222 sync_before_read((void*) n->bufaddr, n->buflen);
223 #ifdef _DU_OHCI_F_HALT
224 dump_address((void*) n->bufaddr, n->buflen, "n->bufaddr(after)");
227 #ifdef _DU_OHCI_F_HALT
228 dbg_td_flag(LE(n->flags));
231 sync_before_read((void*) prev, sizeof(struct general_td));
232 #ifdef _DU_OHCI_F_HALT
233 printf("prev: 0x%08X\n", prev);
234 dump_address(prev, sizeof(struct general_td), "prev(after)");
236 if(prev->buflen >0) {
237 sync_before_read((void*) prev->bufaddr, prev->buflen);
238 #ifdef _DU_OHCI_F_HALT
239 dump_address((void*) prev->bufaddr, prev->buflen, "prev->bufaddr(after)");
242 #ifdef _DU_OHCI_F_HALT
243 dbg_td_flag(LE(prev->flags));
244 printf("halted end!\n");
248 prev = (struct general_td*) (LE(edhead->headp)&~0xf);
249 } while(LE(edhead->headp)&~0xf);
251 n = phys_to_virt(read32(reg+OHCI_HC_DONE_HEAD) & ~1);
253 printf("hc_done_head: 0x%08X\n", read32(reg+OHCI_HC_DONE_HEAD));
257 /* reverse done queue */
258 while(virt_to_phys(n) && edhead->tdcount) {
259 sync_before_read((void*) n, sizeof(struct general_td));
261 printf("n: 0x%08X\n", n);
262 printf("next: 0x%08X\n", next);
263 printf("prev: 0x%08X\n", prev);
267 n = (struct general_td*) phys_to_virt(LE(n->nexttd));
268 next->nexttd = (u32) prev;
276 while(virt_to_phys(n)) {
278 dump_address(n, sizeof(struct general_td), "n(after)");
281 sync_before_read((void*) n->bufaddr, n->buflen);
283 dump_address((void*) n->bufaddr, n->buflen, "n->bufaddr(after)");
287 dbg_td_flag(LE(n->flags));
290 n = (struct general_td*) n->nexttd;
294 if(reg == OHCI0_REG_BASE) {
295 hcca_oh0.done_head = 0;
296 sync_after_write(&hcca_oh0, sizeof(hcca_oh0));
297 } else if (reg == OHCI1_REG_BASE) {
298 hcca_oh1.done_head = 0;
299 sync_after_write(&hcca_oh1, sizeof(hcca_oh1));
302 write32(reg+OHCI_HC_CONTROL, read32(reg+OHCI_HC_CONTROL)&~OHCI_CTRL_CLE);
307 printf("<^> <^> <^> hcdi_fire(end)\n");
312 * Enqueue a transfer descriptor.
314 u8 hcdi_enqueue(const struct usb_transfer_descriptor *td, u32 reg) {
316 printf("*()*()*()*()*()*()*() hcdi_enqueue(start)\n");
320 memset(edhead, 0, sizeof(struct endpoint_descriptor));
321 edhead->flags = LE(OHCI_ENDPOINT_GENERAL_FORMAT);
322 edhead->headp = edhead->tailp = edhead->nexted = LE(0);
324 edhead->flags |= LE(OHCI_ENDPOINT_FULL_SPEED);
326 edhead->flags |= LE(OHCI_ENDPOINT_LOW_SPEED);
328 edhead->flags |= LE(OHCI_ENDPOINT_SET_DEVICE_ADDRESS(td->devaddress) |
329 OHCI_ENDPOINT_SET_ENDPOINT_NUMBER(td->endpoint) |
330 OHCI_ENDPOINT_SET_MAX_PACKET_SIZE(td->maxp));
334 struct general_td *tdhw = allocate_general_td();
335 general_td_fill(tdhw, td);
340 edhead->headp = LE(virt_to_phys((void*) ((u32)tdhw & OHCI_ENDPOINT_HEAD_MASK)));
343 /* headp in endpoint already exists
346 struct general_td *n = (struct general_td*) phys_to_virt(LE(edhead->headp) & OHCI_ENDPOINT_HEAD_MASK);
347 while(LE(n->nexttd)) {
348 n = phys_to_virt(LE(n->nexttd));
350 n->nexttd = LE(virt_to_phys((void*) ((u32)tdhw & OHCI_ENDPOINT_HEAD_MASK)));
352 printf("n: 0x%08X\n", n);
353 printf("n->nexttd: 0x%08X\n", phys_to_virt(LE(n->nexttd)));
358 printf("*()*()*()*()*()*()*() hcdi_enqueue(end)\n");
365 * Remove an transfer descriptor from transfer queue.
367 u8 hcdi_dequeue(struct usb_transfer_descriptor *td, u32 reg) {
371 void hcdi_init(u32 reg)
373 printf("ohci-- init\n");
376 /* disable hc interrupts */
377 set32(reg+OHCI_HC_INT_DISABLE, OHCI_INTR_MIE);
379 /* save fmInterval and calculate FSMPS */
380 #define FSMP(fi) (0x7fff & ((6 * ((fi) - 210)) / 7))
381 #define FI 0x2edf /* 12000 bits per frame (-1) */
382 u32 fmint = read32(reg+OHCI_HC_FM_INTERVAL) & 0x3fff;
384 printf("ohci-- fminterval delta: %d\n", fmint - FI);
385 fmint |= FSMP (fmint) << 16;
387 /* enable interrupts of both usb host controllers */
388 set32(EHCI_CTL, EHCI_CTL_OH0INTE | EHCI_CTL_OH1INTE | 0xe0000);
391 write32(reg+OHCI_HC_COMMAND_STATUS, OHCI_HCR);
395 while ((read32(reg+OHCI_HC_COMMAND_STATUS) & OHCI_HCR) != 0) {
397 printf("ohci-- FAILED");
403 /* disable interrupts; 2ms timelimit here!
404 now we're in the SUSPEND state ... must go OPERATIONAL
405 within 2msec else HC enters RESUME */
407 u32 cookie = irq_kill();
409 /* Tell the controller where the control and bulk lists are
410 * The lists are empty now. */
411 write32(reg+OHCI_HC_CTRL_HEAD_ED, 0);
412 write32(reg+OHCI_HC_BULK_HEAD_ED, 0);
414 /* set hcca adress */
415 if(reg == OHCI0_REG_BASE) {
416 sync_after_write(&hcca_oh0, 256);
417 write32(reg+OHCI_HC_HCCA, virt_to_phys(&hcca_oh0));
419 sync_after_write(&hcca_oh1, 256);
420 write32(reg+OHCI_HC_HCCA, virt_to_phys(&hcca_oh1));
423 /* set periodicstart */
425 u32 fmInterval = read32(reg+OHCI_HC_FM_INTERVAL) &0x3fff;
426 u32 fit = read32(reg+OHCI_HC_FM_INTERVAL) & FIT;
428 write32(reg+OHCI_HC_FM_INTERVAL, fmint | (fit ^ FIT));
429 write32(reg+OHCI_HC_PERIODIC_START, ((9*fmInterval)/10)&0x3fff);
432 if ((read32(reg+OHCI_HC_FM_INTERVAL) & 0x3fff0000) == 0 || !read32(reg+OHCI_HC_PERIODIC_START)) {
433 printf("ohci-- w00t, fail!! see ohci-hcd.c:669\n");
436 /* start HC operations */
437 write32(reg+OHCI_HC_CONTROL, OHCI_CONTROL_INIT | OHCI_USB_OPER);
439 /* wake on ConnectStatusChange, matching external hubs */
440 write32(reg+OHCI_HC_RH_STATUS, /*RH_HS_DRWE |*/ RH_HS_LPSC);
442 /* Choose the interrupts we care about now, others later on demand */
443 write32(reg+OHCI_HC_INT_STATUS, ~0);
444 write32(reg+OHCI_HC_INT_ENABLE, OHCI_INTR_INIT);
447 wait_ms ((read32(reg+OHCI_HC_RH_DESCRIPTOR_A) >> 23) & 0x1fe);
449 configure_ports((u8)1, reg);
455 static void configure_ports(u8 from_init, u32 reg)
458 printf("=== Roothub @ %s ===\n", reg == OHCI0_REG_BASE ? "OHCI0" : "OHCI1");
459 printf("OHCI_HC_RH_DESCRIPTOR_A:\t0x%08X\n", read32(reg+OHCI_HC_RH_DESCRIPTOR_A));
460 printf("OHCI_HC_RH_DESCRIPTOR_B:\t0x%08X\n", read32(reg+OHCI_HC_RH_DESCRIPTOR_B));
461 printf("OHCI_HC_RH_STATUS:\t\t0x%08X\n", read32(reg+OHCI_HC_RH_STATUS));
462 printf("OHCI_HC_RH_PORT_STATUS_1:\t0x%08X\n", read32(reg+OHCI_HC_RH_PORT_STATUS_1));
463 printf("OHCI_HC_RH_PORT_STATUS_2:\t0x%08X\n", read32(reg+OHCI_HC_RH_PORT_STATUS_2));
466 setup_port(reg, reg+OHCI_HC_RH_PORT_STATUS_1, from_init);
467 setup_port(reg, reg+OHCI_HC_RH_PORT_STATUS_2, from_init);
469 printf("configure_ports done\n");
473 static void setup_port(u32 ohci, u32 reg, u8 from_init)
475 u32 port = read32(reg);
476 if((port & RH_PS_CCS) && ((port & RH_PS_CSC) || from_init)) {
477 write32(reg, RH_PS_CSC);
481 /* clear CSC flag, set PES and start port reset (PRS) */
482 write32(reg, RH_PS_PES);
483 while(!(read32(reg) & RH_PS_PES)) {
490 write32(reg, RH_PS_PRS);
492 /* spin until port reset is complete */
493 while(!(read32(reg) & RH_PS_PRSC)); // hint: it may stuck here
495 printf("loop done\n");
498 /* returns usb_device struct */
499 (void) usb_add_device((read32(reg) & RH_PS_LSDA) >> 8, ohci);
503 void hcdi_irq(u32 reg)
505 /* read interrupt status */
506 u32 flags = read32(reg+OHCI_HC_INT_STATUS);
508 /* when all bits are set to 1 some problem occured */
509 if (flags == 0xffffffff) {
510 printf("ohci-- Houston, we have a serious problem! :(\n");
514 /* only care about interrupts that are enabled */
515 flags &= read32(reg+OHCI_HC_INT_ENABLE);
519 printf("OHCI Interrupt occured: but not for you! WTF?!\n");
523 printf("OHCI Interrupt occured: ");
524 /* UnrecoverableError */
525 if (flags & OHCI_INTR_UE) {
526 printf("UnrecoverableError\n");
527 /* TODO: well, I don't know... nothing,
528 * because it won't happen anyway? ;-) */
531 /* RootHubStatusChange */
532 if (flags & OHCI_INTR_RHSC) {
533 printf("RootHubStatusChange\n");
534 /* TODO: set some next_statechange variable... */
535 configure_ports(0, reg);
536 write32(reg+OHCI_HC_INT_STATUS, OHCI_INTR_RD | OHCI_INTR_RHSC);
539 else if (flags & OHCI_INTR_RD) {
540 printf("ResumeDetected\n");
541 write32(reg+OHCI_HC_INT_STATUS, OHCI_INTR_RD);
542 /* TODO: figure out what the linux kernel does here... */
545 /* WritebackDoneHead */
546 if (flags & OHCI_INTR_WDH) {
547 printf("WritebackDoneHead\n");
548 /* basically the linux irq handler reverse TDs to their urbs
549 * and set done_head to null.
550 * since we are polling atm, just should do the latter task.
551 * however, this won't work for now (i don't know why...)
555 sync_before_read(&hcca_oh0, 256);
556 hcca_oh0.done_head = 0;
557 sync_after_write(&hcca_oh0, 256);
561 /* TODO: handle any pending URB/ED unlinks... */
563 #define HC_IS_RUNNING() 1 /* dirty, i know... just a temporary solution */
564 if (HC_IS_RUNNING()) {
565 write32(reg+OHCI_HC_INT_STATUS, flags);
566 write32(reg+OHCI_HC_INT_ENABLE, OHCI_INTR_MIE);
570 void show_frame_no(u32 reg)
572 if(reg == OHCI0_REG_BASE) {
573 sync_before_read(&hcca_oh0, 256);
574 printf("***** frame_no: %d *****\n", LE(hcca_oh0.frame_no));
575 } else if (reg == OHCI1_REG_BASE) {
576 sync_before_read(&hcca_oh1, 256);
577 printf("***** frame_no: %d *****\n", LE(hcca_oh1.frame_no));