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