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