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"
20 static struct ohci_hcca hcca_oh0;
21 static struct endpoint_descriptor *dummyconfig;
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);
41 td->cbp = (u32)malloc(bsize);
42 td->be = td->cbp + bsize - 1;
47 static void dbg_op_state()
49 switch (read32(OHCI0_HC_CONTROL) & OHCI_CTRL_HCFS) {
50 case OHCI_USB_SUSPEND:
51 printf("ohci-- OHCI_USB_SUSPEND\n");
54 printf("ohci-- OHCI_USB_RESET\n");
57 printf("ohci-- OHCI_USB_OPER\n");
60 printf("ohci-- OHCI_USB_RESUME\n");
67 * Enqueue a transfer descriptor.
69 u8 hcdi_enqueue(usb_transfer_descriptor *td) {
70 printf("===========================\ndone head (vor sync): 0x%08X\n", hcca_oh0.done_head);
71 sync_before_read(&hcca_oh0, 256);
72 printf("done head (nach sync): 0x%08X\n", hcca_oh0.done_head);
74 struct general_td *tmptd = allocate_general_td(sizeof(td->buffer));
75 (void) memcpy((void*) tmptd->cbp, td->buffer, sizeof(td->buffer));
77 printf("tmptd hexump (before):\n");
78 hexdump((void*) tmptd, sizeof(tmptd));
79 printf("tmptd-cbp hexump (before):\n");
80 hexdump((void*) (tmptd->cbp), sizeof(tmptd->cbp));
82 sync_after_write((void*) (tmptd->cbp), sizeof(tmptd->cbp));
83 sync_after_write(tmptd, sizeof(tmptd));
85 dummyconfig->headp = virt_to_phys(tmptd);
86 sync_after_write(dummyconfig, 64);
88 printf("+++++++++++++++++++++++++++++\n");
98 sync_before_read(tmptd, sizeof(tmptd));
99 printf("tmptd hexump (after):\n");
100 hexdump((void*) tmptd, sizeof(tmptd));
102 sync_before_read((void*) (tmptd->cbp), sizeof(tmptd->cbp));
103 printf("tmptd-cbp hexump (after):\n");
104 hexdump((void*) (tmptd->cbp), sizeof(tmptd->cbp));
106 printf("done head (vor sync): 0x%08X\n", hcca_oh0.done_head);
107 sync_before_read(&hcca_oh0, 256);
108 printf("done head (nach sync): 0x%08X\n", hcca_oh0.done_head);
113 * Remove an transfer descriptor from transfer queue.
115 u8 hcdi_dequeue(usb_transfer_descriptor *td) {
121 dummyconfig = allocate_endpoint();
122 printf("ohci-- init\n");
125 /* disable hc interrupts */
126 set32(OHCI0_HC_INT_DISABLE, OHCI_INTR_MIE);
128 /* save fmInterval and calculate FSMPS */
129 #define FSMP(fi) (0x7fff & ((6 * ((fi) - 210)) / 7))
130 #define FI 0x2edf /* 12000 bits per frame (-1) */
131 u32 fmint = read32(OHCI0_HC_FM_INTERVAL) & 0x3fff;
133 printf("ohci-- fminterval delta: %d\n", fmint - FI);
134 fmint |= FSMP (fmint) << 16;
136 /* enable interrupts of both usb host controllers */
137 set32(EHCI_CTL, EHCI_CTL_OH0INTE | EHCI_CTL_OH1INTE | 0xe0000);
140 write32(OHCI0_HC_COMMAND_STATUS, OHCI_HCR);
144 while ((read32(OHCI0_HC_COMMAND_STATUS) & OHCI_HCR) != 0) {
146 printf("ohci-- FAILED");
152 /* disable interrupts; 2ms timelimit here!
153 now we're in the SUSPEND state ... must go OPERATIONAL
154 within 2msec else HC enters RESUME */
156 u32 cookie = irq_kill();
158 /* Tell the controller where the control and bulk lists are
159 * The lists are empty now. */
160 sync_after_write(dummyconfig, 64);
161 write32(OHCI0_HC_CTRL_HEAD_ED, virt_to_phys(dummyconfig));
162 write32(OHCI0_HC_BULK_HEAD_ED, 0);
164 /* set hcca adress */
165 sync_after_write(&hcca_oh0, 256);
166 write32(OHCI0_HC_HCCA, virt_to_phys(&hcca_oh0));
168 /* set periodicstart */
170 u32 fmInterval = read32(OHCI0_HC_FM_INTERVAL) &0x3fff;
171 u32 fit = read32(OHCI0_HC_FM_INTERVAL) & FIT;
173 write32(OHCI0_HC_FM_INTERVAL, fmint | (fit ^ FIT));
174 write32(OHCI0_HC_PERIODIC_START, ((9*fmInterval)/10)&0x3fff);
177 if ((read32(OHCI0_HC_FM_INTERVAL) & 0x3fff0000) == 0 || !read32(OHCI0_HC_PERIODIC_START)) {
178 printf("ohci-- w00t, fail!! see ohci-hcd.c:669\n");
181 /* start HC operations */
182 write32(OHCI0_HC_CONTROL, OHCI_CONTROL_INIT | OHCI_USB_OPER);
184 /* wake on ConnectStatusChange, matching external hubs */
185 set32(OHCI0_HC_RH_STATUS, RH_HS_DRWE);
187 /* Choose the interrupts we care about now, others later on demand */
188 write32(OHCI0_HC_INT_STATUS, ~0);
189 write32(OHCI0_HC_INT_ENABLE, OHCI_INTR_INIT);
198 /* read interrupt status */
199 u32 flags = read32(OHCI0_HC_INT_STATUS);
201 /* when all bits are set to 1 some problem occured */
202 if (flags == 0xffffffff) {
203 printf("ohci-- Houston, we have a serious problem! :(\n");
207 /* only care about interrupts that are enabled */
208 flags &= read32(OHCI0_HC_INT_ENABLE);
214 printf("OHCI Interrupt occured: ");
215 /* UnrecoverableError */
216 if (flags & OHCI_INTR_UE) {
217 printf("UnrecoverableError\n");
218 /* TODO: well, I don't know... nothing,
219 * because it won't happen anyway? ;-) */
222 /* RootHubStatusChange */
223 if (flags & OHCI_INTR_RHSC) {
224 printf("RootHubStatusChange\n");
225 /* TODO: set some next_statechange variable... */
226 write32(OHCI0_HC_INT_STATUS, OHCI_INTR_RD | OHCI_INTR_RHSC);
229 else if (flags & OHCI_INTR_RD) {
230 printf("ResumeDetected\n");
231 write32(OHCI0_HC_INT_STATUS, OHCI_INTR_RD);
232 /* TODO: figure out what the linux kernel does here... */
235 /* WritebackDoneHead */
236 if (flags & OHCI_INTR_WDH) {
237 printf("WritebackDoneHead\n");
238 /* TODO: figure out what the linux kernel does here... */
241 /* TODO: handle any pending URB/ED unlinks... */
243 #define HC_IS_RUNNING() 1 /* dirty, i know... just a temporary solution */
244 if (HC_IS_RUNNING()) {
245 write32(OHCI0_HC_INT_STATUS, flags);
246 write32(OHCI0_HC_INT_ENABLE, OHCI_INTR_MIE);