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(sizeof(td->actlen));
155 (void) memcpy((void*) phys_to_virt(tmptd->cbp), td->buffer, sizeof(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;
173 printf("tmptd hexump (before):\n");
174 hexdump(tmptd, sizeof(struct general_td));
175 printf("tmptd-cbp hexump (before):\n");
176 hexdump((void*) phys_to_virt(tmptd->cbp), sizeof(td->actlen));
178 sync_after_write((void*) (tmptd->cbp), sizeof(td->actlen));
179 sync_after_write(tmptd, sizeof(struct general_td));
181 struct endpoint_descriptor *dummyconfig = allocate_endpoint();
183 printf("tmpdt & ED_MASK: 0x%08X\n", virt_to_phys((void*) ((u32)tmptd & ED_MASK)));
184 #define ED_MASK ((u32)~0x0f)
185 dummyconfig->tailp = /* dummyconfig->headp = */ virt_to_phys((void*) ((u32)tmptd & ED_MASK));
186 // dummyconfig->flags |= OHCI_ENDPOINT_DIRECTION_OUT;
187 sync_after_write(dummyconfig, 64);
188 write32(OHCI0_HC_CTRL_HEAD_ED, virt_to_phys(dummyconfig));
190 printf("OHCI_CTRL_CLE: 0x%08X\n", read32(OHCI0_HC_CONTROL)&OHCI_CTRL_CLE);
191 printf("OHCI_CLF: 0x%08X\n", read32(OHCI0_HC_COMMAND_STATUS)&OHCI_CLF);
192 set32(OHCI0_HC_CONTROL, OHCI_CTRL_CLE);
193 write32(OHCI0_HC_COMMAND_STATUS, OHCI_CLF);
195 printf("+++++++++++++++++++++++++++++\n");
196 /* spin until the controller is done with the control list */
197 u32 current = read32(OHCI0_HC_CTRL_CURRENT_ED);
198 printf("current: 0x%08X\n", current);
201 current = read32(OHCI0_HC_CTRL_CURRENT_ED);
207 current = read32(OHCI0_HC_CTRL_CURRENT_ED);
208 printf("current: 0x%08X\n", current);
209 printf("+++++++++++++++++++++++++++++\n");
219 sync_before_read(tmptd, sizeof(struct general_td));
220 printf("tmptd hexump (after):\n");
221 hexdump(tmptd, sizeof(struct general_td));
223 sync_before_read((void*) (tmptd->cbp), sizeof(td->actlen));
224 printf("tmptd-cbp hexump (after):\n");
225 hexdump((void*) (tmptd->cbp), sizeof(td->actlen));
227 printf("done head (vor sync): 0x%08X\n", hcca_oh0.done_head);
228 sync_before_read(&hcca_oh0, 256);
229 printf("done head (nach sync): 0x%08X\n", hcca_oh0.done_head);
236 * Remove an transfer descriptor from transfer queue.
238 u8 hcdi_dequeue(usb_transfer_descriptor *td) {
244 printf("ohci-- init\n");
247 /* disable hc interrupts */
248 set32(OHCI0_HC_INT_DISABLE, OHCI_INTR_MIE);
250 /* save fmInterval and calculate FSMPS */
251 #define FSMP(fi) (0x7fff & ((6 * ((fi) - 210)) / 7))
252 #define FI 0x2edf /* 12000 bits per frame (-1) */
253 u32 fmint = read32(OHCI0_HC_FM_INTERVAL) & 0x3fff;
255 printf("ohci-- fminterval delta: %d\n", fmint - FI);
256 fmint |= FSMP (fmint) << 16;
258 /* enable interrupts of both usb host controllers */
259 set32(EHCI_CTL, EHCI_CTL_OH0INTE | EHCI_CTL_OH1INTE | 0xe0000);
262 write32(OHCI0_HC_COMMAND_STATUS, OHCI_HCR);
266 while ((read32(OHCI0_HC_COMMAND_STATUS) & OHCI_HCR) != 0) {
268 printf("ohci-- FAILED");
274 /* disable interrupts; 2ms timelimit here!
275 now we're in the SUSPEND state ... must go OPERATIONAL
276 within 2msec else HC enters RESUME */
278 u32 cookie = irq_kill();
280 /* Tell the controller where the control and bulk lists are
281 * The lists are empty now. */
282 write32(OHCI0_HC_CTRL_HEAD_ED, 0);
283 write32(OHCI0_HC_BULK_HEAD_ED, 0);
285 /* set hcca adress */
286 sync_after_write(&hcca_oh0, 256);
287 write32(OHCI0_HC_HCCA, virt_to_phys(&hcca_oh0));
289 /* set periodicstart */
291 u32 fmInterval = read32(OHCI0_HC_FM_INTERVAL) &0x3fff;
292 u32 fit = read32(OHCI0_HC_FM_INTERVAL) & FIT;
294 write32(OHCI0_HC_FM_INTERVAL, fmint | (fit ^ FIT));
295 write32(OHCI0_HC_PERIODIC_START, ((9*fmInterval)/10)&0x3fff);
298 if ((read32(OHCI0_HC_FM_INTERVAL) & 0x3fff0000) == 0 || !read32(OHCI0_HC_PERIODIC_START)) {
299 printf("ohci-- w00t, fail!! see ohci-hcd.c:669\n");
302 /* start HC operations */
303 write32(OHCI0_HC_CONTROL, OHCI_CONTROL_INIT | OHCI_USB_OPER);
305 /* wake on ConnectStatusChange, matching external hubs */
306 set32(OHCI0_HC_RH_STATUS, RH_HS_DRWE);
308 /* Choose the interrupts we care about now, others later on demand */
309 write32(OHCI0_HC_INT_STATUS, ~0);
310 write32(OHCI0_HC_INT_ENABLE, OHCI_INTR_INIT);
319 /* read interrupt status */
320 u32 flags = read32(OHCI0_HC_INT_STATUS);
322 /* when all bits are set to 1 some problem occured */
323 if (flags == 0xffffffff) {
324 printf("ohci-- Houston, we have a serious problem! :(\n");
328 /* only care about interrupts that are enabled */
329 flags &= read32(OHCI0_HC_INT_ENABLE);
335 printf("OHCI Interrupt occured: ");
336 /* UnrecoverableError */
337 if (flags & OHCI_INTR_UE) {
338 printf("UnrecoverableError\n");
339 /* TODO: well, I don't know... nothing,
340 * because it won't happen anyway? ;-) */
343 /* RootHubStatusChange */
344 if (flags & OHCI_INTR_RHSC) {
345 printf("RootHubStatusChange\n");
346 /* TODO: set some next_statechange variable... */
347 write32(OHCI0_HC_INT_STATUS, OHCI_INTR_RD | OHCI_INTR_RHSC);
350 else if (flags & OHCI_INTR_RD) {
351 printf("ResumeDetected\n");
352 write32(OHCI0_HC_INT_STATUS, OHCI_INTR_RD);
353 /* TODO: figure out what the linux kernel does here... */
356 /* WritebackDoneHead */
357 if (flags & OHCI_INTR_WDH) {
358 printf("WritebackDoneHead\n");
359 /* TODO: figure out what the linux kernel does here... */
362 /* TODO: handle any pending URB/ED unlinks... */
364 #define HC_IS_RUNNING() 1 /* dirty, i know... just a temporary solution */
365 if (HC_IS_RUNNING()) {
366 write32(OHCI0_HC_INT_STATUS, flags);
367 write32(OHCI0_HC_INT_ENABLE, OHCI_INTR_MIE);