bd3793c5a44d943a01f8213a204d93d5ff5e71c1
[ppcskel.git] / usb / host / ohci.c
1 /*
2        ppcskel - a Free Software replacement for the Nintendo/BroadOn bootloader.
3        ohci hardware support
4
5 Copyright (C) 2009     Bernhard Urban <lewurm@gmx.net>
6 Copyright (C) 2009     Sebastian Falbesoner <sebastian.falbesoner@gmail.com>
7
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
10 */
11
12 #include "../../bootmii_ppc.h"
13 #include "../../hollywood.h"
14 #include "../../irq.h"
15 #include "../../string.h"
16 #include "../../malloc.h"
17 #include "ohci.h"
18 #include "host.h"
19 #include "../usbspec/usb11spec.h"
20
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) )
28
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);
36
37 static struct ohci_hcca hcca_oh0;
38
39
40 static struct endpoint_descriptor *allocate_endpoint()
41 {
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);
46         return ep;
47 }
48
49 static struct general_td *allocate_general_td(size_t bsize)
50 {
51         struct general_td *td;
52         td = (struct general_td *)memalign(16, sizeof(struct general_td));
53         td->flags = ACCESS_LE(0);
54         // TODO !! nexttd?
55         td->nexttd = ACCESS_LE(virt_to_phys(td));
56         //td->nexttd = ACCESS_LE(0);
57         if(bsize == 0) {
58                 td->cbp = td->be = ACCESS_LE(0);
59         } else {
60                 //td->cbp = ACCESS_LE(virt_to_phys(memalign(16, bsize))); //memailgn required here?
61                 td->cbp = ACCESS_LE(virt_to_phys(malloc(bsize)));
62                 memset(phys_to_virt(ACCESS_LE(td->cbp)), 0, bsize);
63                 td->be = ACCESS_LE(ACCESS_LE(td->cbp) + bsize - 1);
64         }
65         return td;
66 }
67
68 static void control_quirk()
69 {
70         static struct endpoint_descriptor *ed = 0; /* empty ED */
71         static struct general_td *td = 0; /* dummy TD */
72         u32 head;
73         u32 current;
74         u32 status;
75
76         /*
77          * One time only.
78          * Allocate and keep a special empty ED with just a dummy TD.
79          */
80         if (!ed) {
81                 ed = allocate_endpoint();
82                 if (!ed)
83                         return;
84
85                 td = allocate_general_td(0);
86                 if (!td) {
87                         free(ed);
88                         ed = NULL;
89                         return;
90                 }
91
92 #define ED_MASK ((u32)~0x0f)
93                 ed->tailp = ed->headp = ACCESS_LE(virt_to_phys((void*) ((u32)td & ED_MASK)));
94                 ed->flags |= ACCESS_LE(OHCI_ENDPOINT_DIRECTION_OUT);
95         }
96
97         /*
98          * The OHCI USB host controllers on the Nintendo Wii
99          * video game console stop working when new TDs are
100          * added to a scheduled control ED after a transfer has
101          * has taken place on it.
102          *
103          * Before scheduling any new control TD, we make the
104          * controller happy by always loading a special control ED
105          * with a single dummy TD and letting the controller attempt
106          * the transfer.
107          * The controller won't do anything with it, as the special
108          * ED has no TDs, but it will keep the controller from failing
109          * on the next transfer.
110          */
111         head = read32(OHCI0_HC_CTRL_HEAD_ED);
112         if (head) {
113                 printf("head: 0x%08X\n", head);
114                 /*
115                  * Load the special empty ED and tell the controller to
116                  * process the control list.
117                  */
118                 sync_after_write(ed, 16);
119                 sync_after_write(td, 16);
120                 write32(OHCI0_HC_CTRL_HEAD_ED, virt_to_phys(ed));
121
122                 status = read32(OHCI0_HC_CONTROL);
123                 set32(OHCI0_HC_CONTROL, OHCI_CTRL_CLE);
124                 write32(OHCI0_HC_COMMAND_STATUS, OHCI_CLF);
125
126                 /* spin until the controller is done with the control list */
127                 current = read32(OHCI0_HC_CTRL_CURRENT_ED);
128                 while(!current) {
129                         udelay(10);
130                         current = read32(OHCI0_HC_CTRL_CURRENT_ED);
131                 }
132
133                 printf("current: 0x%08X\n", current);
134                         
135                 /* restore the old control head and control settings */
136                 write32(OHCI0_HC_CONTROL, status);
137                 write32(OHCI0_HC_CTRL_HEAD_ED, head);
138         } else {
139                 printf("nohead!\n");
140         }
141 }
142
143
144 static void dbg_op_state() 
145 {
146         switch (read32(OHCI0_HC_CONTROL) & OHCI_CTRL_HCFS) {
147                 case OHCI_USB_SUSPEND:
148                         printf("ohci-- OHCI_USB_SUSPEND\n");
149                         break;
150                 case OHCI_USB_RESET:
151                         printf("ohci-- OHCI_USB_RESET\n");
152                         break;
153                 case OHCI_USB_OPER:
154                         printf("ohci-- OHCI_USB_OPER\n");
155                         break;
156                 case OHCI_USB_RESUME:
157                         printf("ohci-- OHCI_USB_RESUME\n");
158                         break;
159         }
160 }
161
162 static void dbg_td_flag(u32 flag)
163 {
164         printf("**************** dbg_td_flag: 0x%08X ***************\n", flag);
165         printf("CC: %X\tshould be 0, see page 32 (ohci spec)\n", (flag>>28)&0xf);
166         printf("EC: %X\tsee page 20 (ohci spec)\n", (flag>>26)&3);
167         printf(" T: %X\n", (flag>>24)&3);
168         printf("DI: %X\n", (flag>>21)&7);
169         printf("DP: %X\n", (flag>>19)&3);
170         printf(" R: %X\n", (flag>>18)&1);
171         printf("********************************************************\n");
172 }
173
174
175
176 /**
177  * Enqueue a transfer descriptor.
178  */
179 u8 hcdi_enqueue(usb_transfer_descriptor *td) {
180         control_quirk(); //required? YES! :O
181         u32 tmptdbuffer;
182
183         static struct endpoint_descriptor dummyconfig;
184         memset(&dummyconfig, 0, 16);
185         dummyconfig.flags = ACCESS_LE(OHCI_ENDPOINT_GENERAL_FORMAT);
186         dummyconfig.headp = dummyconfig.tailp = dummyconfig.nexted = ACCESS_LE(0);
187
188         printf( "===========================\n"
189                         "===========================\n");
190         printf("td->buffer(1): 0x%08X\n", (void*)td->buffer);
191         sync_before_read(&hcca_oh0, 256);
192         printf("done head (nach sync): 0x%08X\n", ACCESS_LE(hcca_oh0.done_head));
193         printf("HCCA->frame_no: %d\nhcca->hccapad1: %d\n",
194                         ((ACCESS_LE(hcca_oh0.frame_no) & 0xffff)>>16),
195                         ACCESS_LE(hcca_oh0.frame_no)&0x0000ffff );
196
197         struct general_td *tmptd = allocate_general_td(td->actlen);
198         (void) memcpy((void*) (phys_to_virt(ACCESS_LE(tmptd->cbp))), td->buffer, td->actlen); 
199
200         tmptd->flags &= ACCESS_LE(~OHCI_TD_DIRECTION_PID_MASK);
201         switch(td->pid) {
202                 case USB_PID_SETUP:
203                         printf("pid_setup\n");
204                         tmptd->flags |= ACCESS_LE(OHCI_TD_DIRECTION_PID_SETUP);
205                         break;
206                 case USB_PID_OUT:
207                         printf("pid_out\n");
208                         tmptd->flags |= ACCESS_LE(OHCI_TD_DIRECTION_PID_OUT);
209                         break;
210                 case USB_PID_IN:
211                         printf("pid_in\n");
212                         tmptd->flags |= ACCESS_LE(OHCI_TD_DIRECTION_PID_IN);
213                         break;
214         }
215         tmptd->flags |= ACCESS_LE((td->togl) ? OHCI_TD_TOGGLE_1 : OHCI_TD_TOGGLE_0);
216         //tmptd->flags |= ACCESS_LE(OHCI_TD_TOGGLE_1);
217
218         printf("tmptd hexdump (before) 0x%08X:\n", tmptd);
219         hexdump(tmptd, sizeof(struct general_td));
220         //save buffer adress here; HC may change tmptd->cbp
221         tmptdbuffer = (u32) phys_to_virt(ACCESS_LE(tmptd->cbp)); 
222         printf("tmptd->cbp hexdump (before) 0x%08X:\n", phys_to_virt(ACCESS_LE(tmptd->cbp)));
223         hexdump((void*) phys_to_virt(ACCESS_LE(tmptd->cbp)), td->actlen);
224
225         sync_after_write(tmptd, sizeof(struct general_td));
226         sync_after_write((void*) phys_to_virt(ACCESS_LE(tmptd->cbp)), td->actlen);
227
228 #define ED_MASK ((u32)~0x0f) 
229         dummyconfig.headp = ACCESS_LE(virt_to_phys((void*) ((u32)tmptd & ED_MASK)));
230
231         dummyconfig.flags |= ACCESS_LE(OHCI_ENDPOINT_LOW_SPEED | 
232                 OHCI_ENDPOINT_SET_DEVICE_ADDRESS(td->devaddress) | 
233                 OHCI_ENDPOINT_SET_ENDPOINT_NUMBER(td->endpoint) |
234                 OHCI_ENDPOINT_SET_MAX_PACKET_SIZE(td->maxp));
235
236         printf("dummyconfig hexdump (before) 0x%08X:\n", &dummyconfig);
237         hexdump((void*) &dummyconfig, 16);
238
239         sync_after_write(&dummyconfig, 16);
240         write32(OHCI0_HC_CTRL_HEAD_ED, virt_to_phys(&dummyconfig));
241
242         printf("OHCI_CTRL_CLE: 0x%08X || ", read32(OHCI0_HC_CONTROL)&OHCI_CTRL_CLE);
243         printf("OHCI_CLF: 0x%08X\n", read32(OHCI0_HC_COMMAND_STATUS)&OHCI_CLF);
244         set32(OHCI0_HC_CONTROL, OHCI_CTRL_CLE);
245         write32(OHCI0_HC_COMMAND_STATUS, OHCI_CLF);
246
247         //printf("+++++++++++++++++++++++++++++\n");
248         /* spin until the controller is done with the control list */
249         //printf("current: 0x%08X\n", current);
250
251         //don't use this quirk stuff here!
252 #if 1
253         while(!read32(OHCI0_HC_CTRL_CURRENT_ED)) {
254         }
255 #endif
256
257         udelay(20000);
258         u32 current = read32(OHCI0_HC_CTRL_CURRENT_ED);
259         printf("current: 0x%08X\n", current);
260         printf("+++++++++++++++++++++++++++++\n");
261         udelay(20000);
262
263         sync_before_read(tmptd, sizeof(struct general_td));
264         printf("tmptd hexdump (after) 0x%08X:\n", tmptd);
265         hexdump(tmptd, sizeof(struct general_td));
266         dbg_td_flag(ACCESS_LE(tmptd->flags));
267
268         sync_before_read((void*) phys_to_virt(ACCESS_LE(tmptd->cbp)), td->actlen);
269         printf("tmptd->cbp hexdump (after) 0x%08X:\n", phys_to_virt(ACCESS_LE(tmptd->cbp)));
270         hexdump((void*) phys_to_virt(ACCESS_LE(tmptd->cbp)), td->actlen);
271
272         sync_before_read(&dummyconfig, 16);
273         printf("dummyconfig hexdump (after) 0x%08X:\n", &dummyconfig);
274         hexdump((void*) &dummyconfig, 16);
275
276         sync_before_read(&hcca_oh0, 256);
277         printf("done head (nach sync): 0x%08X\n", ACCESS_LE(hcca_oh0.done_head));
278
279         printf("td->buffer(1): 0x%08X\n", (void*)td->buffer);
280         struct general_td* donetd = phys_to_virt(ACCESS_LE(hcca_oh0.done_head)&~1);
281         sync_before_read(donetd, 16);
282         printf("done head hexdump: 0x%08X\n", donetd);
283         hexdump((void*) donetd, 16);
284         printf("td->buffer(2): 0x%08X\n", (void*)td->buffer);
285
286         sync_before_read((void*) phys_to_virt(ACCESS_LE(tmptd->cbp)), td->actlen);
287         printf("td->buffer: 0x%08X\np2v(A_L(tmptd->cbp: 0x%08X\ntd->actlen: %d\n", (void*) (td->buffer), phys_to_virt(ACCESS_LE(tmptd->cbp)), td->actlen);
288         (void) memcpy((void*) (td->buffer), phys_to_virt(ACCESS_LE(tmptd->cbp)), td->actlen);
289
290         write32(OHCI0_HC_CONTROL, read32(OHCI0_HC_CONTROL)&~OHCI_CTRL_CLE);
291         dummyconfig.headp = dummyconfig.tailp = dummyconfig.nexted = ACCESS_LE(0);
292
293
294         /* 
295          * TD should be free'd after taking it from the done queue.
296          * but we are very very dirty and do it anyway :p
297          */
298
299         /* only when a buffer is allocated */
300         if(td->actlen)
301                 free((void*)tmptdbuffer);
302         free(tmptd);
303         return 0;
304 }
305
306 /**
307  * Remove an transfer descriptor from transfer queue.
308  */
309 u8 hcdi_dequeue(usb_transfer_descriptor *td) {
310         return 0;
311 }
312
313 void hcdi_init() 
314 {
315         printf("ohci-- init\n");
316         dbg_op_state();
317
318         /* disable hc interrupts */
319         set32(OHCI0_HC_INT_DISABLE, OHCI_INTR_MIE);
320
321         /* save fmInterval and calculate FSMPS */
322 #define FSMP(fi) (0x7fff & ((6 * ((fi) - 210)) / 7))
323 #define FI 0x2edf /* 12000 bits per frame (-1) */
324         u32 fmint = read32(OHCI0_HC_FM_INTERVAL) & 0x3fff;
325         if(fmint != FI)
326                 printf("ohci-- fminterval delta: %d\n", fmint - FI);
327         fmint |= FSMP (fmint) << 16;
328
329         /* enable interrupts of both usb host controllers */
330         set32(EHCI_CTL, EHCI_CTL_OH0INTE | EHCI_CTL_OH1INTE | 0xe0000);
331
332         /* reset HC */
333         write32(OHCI0_HC_COMMAND_STATUS, OHCI_HCR);
334
335         /* wait max. 30us */
336         u32 ts = 30;
337         while ((read32(OHCI0_HC_COMMAND_STATUS) & OHCI_HCR) != 0) {
338                  if(--ts == 0) {
339                         printf("ohci-- FAILED");
340                         return;
341                  }
342                  udelay(1);
343         }
344
345         /* disable interrupts; 2ms timelimit here! 
346            now we're in the SUSPEND state ... must go OPERATIONAL
347            within 2msec else HC enters RESUME */
348
349         u32 cookie = irq_kill();
350
351         /* Tell the controller where the control and bulk lists are
352          * The lists are empty now. */
353         write32(OHCI0_HC_CTRL_HEAD_ED, 0);
354         write32(OHCI0_HC_BULK_HEAD_ED, 0);
355
356         /* set hcca adress */
357         sync_after_write(&hcca_oh0, 256);
358         write32(OHCI0_HC_HCCA, virt_to_phys(&hcca_oh0));
359
360         /* set periodicstart */
361 #define FIT (1<<31)
362         u32 fmInterval = read32(OHCI0_HC_FM_INTERVAL) &0x3fff;
363         u32 fit = read32(OHCI0_HC_FM_INTERVAL) & FIT;
364
365         write32(OHCI0_HC_FM_INTERVAL, fmint | (fit ^ FIT));
366         write32(OHCI0_HC_PERIODIC_START, ((9*fmInterval)/10)&0x3fff);
367
368         /* testing bla */
369         if ((read32(OHCI0_HC_FM_INTERVAL) & 0x3fff0000) == 0 || !read32(OHCI0_HC_PERIODIC_START)) {
370                 printf("ohci-- w00t, fail!! see ohci-hcd.c:669\n");
371         }
372         
373         /* start HC operations */
374         write32(OHCI0_HC_CONTROL, OHCI_CONTROL_INIT | OHCI_USB_OPER);
375
376         /* wake on ConnectStatusChange, matching external hubs */
377         write32(OHCI0_HC_RH_STATUS, /*RH_HS_DRWE |*/ RH_HS_LPSC);
378
379         /* Choose the interrupts we care about now, others later on demand */
380         write32(OHCI0_HC_INT_STATUS, ~0);
381         write32(OHCI0_HC_INT_ENABLE, OHCI_INTR_INIT);
382
383         //wtf?
384         wait_ms ((read32(OHCI0_HC_RH_DESCRIPTOR_A) >> 23) & 0x1fe);
385
386         configure_ports((u8)1);
387         irq_restore(cookie);
388
389         dbg_op_state();
390 }
391
392 static void configure_ports(u8 from_init)
393 {
394         printf("OHCI0_HC_RH_DESCRIPTOR_A:\t0x%08X\n", read32(OHCI0_HC_RH_DESCRIPTOR_A));
395         printf("OHCI0_HC_RH_DESCRIPTOR_B:\t0x%08X\n", read32(OHCI0_HC_RH_DESCRIPTOR_B));
396         printf("OHCI0_HC_RH_STATUS:\t\t0x%08X\n", read32(OHCI0_HC_RH_STATUS));
397         printf("OHCI0_HC_RH_PORT_STATUS_1:\t0x%08X\n", read32(OHCI0_HC_RH_PORT_STATUS_1));
398         printf("OHCI0_HC_RH_PORT_STATUS_2:\t0x%08X\n", read32(OHCI0_HC_RH_PORT_STATUS_2));
399
400         setup_port(OHCI0_HC_RH_PORT_STATUS_1, from_init);
401         setup_port(OHCI0_HC_RH_PORT_STATUS_2, from_init);
402         printf("configure_ports done\n");
403 }
404
405 static void setup_port(u32 reg, u8 from_init)
406 {
407         u32 port = read32(reg);
408         if((port & RH_PS_CCS) && ((port & RH_PS_CSC) || from_init)) {
409                 write32(reg, RH_PS_CSC);
410
411                 wait_ms(150);
412
413                 /* clear CSC flag, set PES and start port reset (PRS) */
414                 write32(reg, RH_PS_PES);
415                 while(!(read32(reg) & RH_PS_PES)) {
416                         printf("fu\n");
417                         return;
418                 }
419
420                 write32(reg, RH_PS_PRS);
421
422                 /* spin until port reset is complete */
423                 while(!(read32(reg) & RH_PS_PRSC)); // hint: it may stuck here
424                 printf("loop done\n");
425
426                 (void) usb_add_device();
427         }
428 }
429
430 void hcdi_irq()
431 {
432         /* read interrupt status */
433         u32 flags = read32(OHCI0_HC_INT_STATUS);
434
435         /* when all bits are set to 1 some problem occured */
436         if (flags == 0xffffffff) {
437                 printf("ohci-- Houston, we have a serious problem! :(\n");
438                 return;
439         }
440
441         /* only care about interrupts that are enabled */
442         flags &= read32(OHCI0_HC_INT_ENABLE);
443
444         /* nothing to do? */
445         if (flags == 0) {
446                 printf("OHCI Interrupt occured: but not for you! WTF?!\n");
447                 return;
448         }
449
450         printf("OHCI Interrupt occured: ");
451         /* UnrecoverableError */
452         if (flags & OHCI_INTR_UE) {
453                 printf("UnrecoverableError\n");
454                 /* TODO: well, I don't know... nothing,
455                  *       because it won't happen anyway? ;-) */
456         }
457
458         /* RootHubStatusChange */
459         if (flags & OHCI_INTR_RHSC) {
460                 printf("RootHubStatusChange\n");
461                 /* TODO: set some next_statechange variable... */
462                 configure_ports(0);
463                 write32(OHCI0_HC_INT_STATUS, OHCI_INTR_RD | OHCI_INTR_RHSC);
464         }
465         /* ResumeDetected */
466         else if (flags & OHCI_INTR_RD) {
467                 printf("ResumeDetected\n");
468                 write32(OHCI0_HC_INT_STATUS, OHCI_INTR_RD);
469                 /* TODO: figure out what the linux kernel does here... */
470         }
471
472         /* WritebackDoneHead */
473         if (flags & OHCI_INTR_WDH) {
474                 printf("WritebackDoneHead\n");
475                 /* basically the linux irq handler reverse TDs to their urbs
476                  * and set done_head to null.
477                  * since we are polling atm, just should do the latter task.
478                  * however, this won't work for now (i don't know why...)
479                  * TODO!
480                  */
481 #if 0
482                 sync_before_read(&hcca_oh0, 256);
483                 hcca_oh0.done_head = 0;
484                 sync_after_write(&hcca_oh0, 256);
485 #endif
486         }
487
488         /* TODO: handle any pending URB/ED unlinks... */
489
490 #define HC_IS_RUNNING() 1 /* dirty, i know... just a temporary solution */
491         if (HC_IS_RUNNING()) {
492                 write32(OHCI0_HC_INT_STATUS, flags);
493                 write32(OHCI0_HC_INT_ENABLE, OHCI_INTR_MIE);
494         }
495 }
496
497 void show_frame_no()
498 {
499         sync_before_read(&hcca_oh0, 256);
500         printf("***** frame_no: %d *****\n", ACCESS_LE(hcca_oh0.frame_no));
501 }