killed the ohci_init FM_INTERVAL fail
[ppcskel.git] / ohci.c
1 /*
2        mini - a Free Software replacement for the Nintendo/BroadOn IOS.
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 "ohci.h"
14 #include "irq.h"
15
16 #define gecko_printf printf
17 #define dma_addr(address) virt_to_phys(address)
18
19
20 static struct ohci_hcca hcca_oh0;
21
22 static void dbg_op_state() {
23        switch (read32(OHCI0_HC_CONTROL) & OHCI_CTRL_HCFS) {
24                case OHCI_USB_SUSPEND:
25                        gecko_printf("ohci-- OHCI_USB_SUSPEND\n");
26                        break;
27                case OHCI_USB_RESET:
28                        gecko_printf("ohci-- OHCI_USB_RESET\n");
29                        break;
30                case OHCI_USB_OPER:
31                        gecko_printf("ohci-- OHCI_USB_OPER\n");
32                        break;
33                case OHCI_USB_RESUME:
34                        gecko_printf("ohci-- OHCI_USB_RESUME\n");
35                        break;
36        }
37 }
38
39 void ohci_init() {
40        gecko_printf("ohci-- init\n");
41        dbg_op_state();
42        /*
43        u32 i = 0;
44        for(; i <= 0x200; i+=4) {
45                gecko_printf("0x0d050000 + %X: %X\n", i, read32(0x0d050000+i));
46                udelay(10000); //'cause usb gecko is lame
47        }
48        * see output in ohci.default
49        */
50
51        /* disable hc interrupts */
52        set32(OHCI0_HC_INT_DISABLE, OHCI_INTR_MIE);
53
54        /* save fmInterval and calculate FSMPS */
55 #define FSMP(fi) (0x7fff & ((6 * ((fi) - 210)) / 7))
56 #define FI 0x2edf /* 12000 bits per frame (-1) */
57        u32 fmint = read32(OHCI0_HC_FM_INTERVAL) & 0x3fff;
58        if(fmint != FI)
59                gecko_printf("ohci-- fminterval delta: %d\n", fmint - FI);
60        fmint |= FSMP (fmint) << 16;
61
62        /* enable interrupts of both usb host controllers */
63        set32(EHCI_CTL, EHCI_CTL_OH0INTE | EHCI_CTL_OH1INTE | 0xe0000);
64
65        /* reset HC */
66        set32(OHCI0_HC_COMMAND_STATUS, OHCI_HCR);
67
68        /* wait max. 30us */
69        u32 ts = 30;
70        while ((read32(OHCI0_HC_COMMAND_STATUS) & OHCI_HCR) != 0) {
71                if(--ts == 0) {
72                        gecko_printf("ohci-- FAILED");
73                        return;
74                }
75                udelay(1);
76        }
77
78        /* disable interrupts; 2ms timelimit here! 
79           now we're in the SUSPEND state ... must go OPERATIONAL
80           within 2msec else HC enters RESUME */
81
82
83            u32 cookie = irq_kill();
84
85        /* Tell the controller where the control and bulk lists are
86         * The lists are empty now. */
87        write32(OHCI0_HC_CTRL_HEAD_ED, 0);
88        write32(OHCI0_HC_BULK_HEAD_ED, 0);
89
90        /* set hcca adress */
91        write32(OHCI0_HC_HCCA, dma_addr(&hcca_oh0));
92
93        /* set periodicstart */
94 #define FIT (1<<31)
95        u32 fmInterval = read32(OHCI0_HC_FM_INTERVAL) &0x3fff;
96         printf("OHCI0_HC_FMINTERVAL: %08X OCHI0_HC_FMINTERVAL&0x3fff: %08X\n", read32(OHCI0_HC_FM_INTERVAL), fmInterval);
97        u32 fit = read32(OHCI0_HC_FM_INTERVAL) & FIT;
98
99        write32(OHCI0_HC_FM_INTERVAL, fmint | (fit ^ FIT));
100        write32(OHCI0_HC_PERIODIC_START, ((9*fmInterval)/10)&0x3fff);
101
102        /* testing bla */
103        if ((read32(OHCI0_HC_FM_INTERVAL) & 0x3fff0000) == 0 || !read32(OHCI0_HC_PERIODIC_START)) {
104                gecko_printf("ohci-- w00t, fail!! see ohci-hcd.c:669\n");
105        }
106        
107        /* start HC operations */
108        set32(OHCI0_HC_CONTROL, OHCI_CONTROL_INIT | OHCI_USB_OPER);
109
110        /* wake on ConnectStatusChange, matching external hubs */
111        set32(OHCI0_HC_RH_STATUS, RH_HS_DRWE);
112
113        /* Choose the interrupts we care about now, others later on demand */
114        write32(OHCI0_HC_INT_STATUS, ~0);
115        write32(OHCI0_HC_INT_ENABLE, OHCI_INTR_INIT);
116
117
118        irq_restore(cookie);
119
120        dbg_op_state();
121 }
122
123 void ohci0_irq() {
124         gecko_printf("ohci_irq\n");
125         write32(OHCI0_HC_INT_STATUS, ~0);
126 }
127
128