attempt to get some TDs through the HC -> fail :(
[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
20 static struct ohci_hcca hcca_oh0;
21 static struct endpoint_descriptor *dummyconfig;
22
23 static struct endpoint_descriptor *allocate_endpoint()
24 {
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;
29         return ep;
30 }
31
32 static struct general_td *allocate_general_td(size_t bsize)
33 {
34         struct general_td *td;
35         td = (struct general_td *)calloc(sizeof(struct general_td), 16);
36         td->flags = 0;
37         td->nexttd = 0;
38         if(bsize == 0) {
39                 td->cbp = td->be = 0;
40         } else {
41                 td->cbp = (u32)malloc(bsize);
42                 td->be = td->cbp + bsize - 1;
43         }
44         return td;
45 }
46
47 static void dbg_op_state() 
48 {
49         switch (read32(OHCI0_HC_CONTROL) & OHCI_CTRL_HCFS) {
50                 case OHCI_USB_SUSPEND:
51                         printf("ohci-- OHCI_USB_SUSPEND\n");
52                         break;
53                 case OHCI_USB_RESET:
54                         printf("ohci-- OHCI_USB_RESET\n");
55                         break;
56                 case OHCI_USB_OPER:
57                         printf("ohci-- OHCI_USB_OPER\n");
58                         break;
59                 case OHCI_USB_RESUME:
60                         printf("ohci-- OHCI_USB_RESUME\n");
61                         break;
62         }
63 }
64
65
66 /**
67  * Enqueue a transfer descriptor.
68  */
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);
73
74         struct general_td *tmptd = allocate_general_td(sizeof(td->buffer));
75         (void) memcpy((void*) tmptd->cbp, td->buffer, sizeof(td->buffer));
76
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));
81
82         sync_after_write((void*) (tmptd->cbp), sizeof(tmptd->cbp));
83         sync_after_write(tmptd, sizeof(tmptd));
84
85         dummyconfig->headp = virt_to_phys(tmptd);
86         sync_after_write(dummyconfig, 64);
87
88         printf("+++++++++++++++++++++++++++++\n");
89         udelay(2000);
90         udelay(2000);
91         udelay(2000);
92         udelay(2000);
93         udelay(2000);
94         udelay(2000);
95         udelay(2000);
96         udelay(2000);
97
98         sync_before_read(tmptd, sizeof(tmptd));
99         printf("tmptd hexump (after):\n");
100         hexdump((void*) tmptd, sizeof(tmptd));
101
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));
105
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);
109         return 0;
110 }
111
112 /**
113  * Remove an transfer descriptor from transfer queue.
114  */
115 u8 hcdi_dequeue(usb_transfer_descriptor *td) {
116         return 0;
117 }
118
119 void hcdi_init() 
120 {
121         dummyconfig = allocate_endpoint();
122         printf("ohci-- init\n");
123         dbg_op_state();
124
125         /* disable hc interrupts */
126         set32(OHCI0_HC_INT_DISABLE, OHCI_INTR_MIE);
127
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;
132         if(fmint != FI)
133                 printf("ohci-- fminterval delta: %d\n", fmint - FI);
134         fmint |= FSMP (fmint) << 16;
135
136         /* enable interrupts of both usb host controllers */
137         set32(EHCI_CTL, EHCI_CTL_OH0INTE | EHCI_CTL_OH1INTE | 0xe0000);
138
139         /* reset HC */
140         write32(OHCI0_HC_COMMAND_STATUS, OHCI_HCR);
141
142         /* wait max. 30us */
143         u32 ts = 30;
144         while ((read32(OHCI0_HC_COMMAND_STATUS) & OHCI_HCR) != 0) {
145                  if(--ts == 0) {
146                         printf("ohci-- FAILED");
147                         return;
148                  }
149                  udelay(1);
150         }
151
152         /* disable interrupts; 2ms timelimit here! 
153            now we're in the SUSPEND state ... must go OPERATIONAL
154            within 2msec else HC enters RESUME */
155
156         u32 cookie = irq_kill();
157
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);
163
164         /* set hcca adress */
165         sync_after_write(&hcca_oh0, 256);
166         write32(OHCI0_HC_HCCA, virt_to_phys(&hcca_oh0));
167
168         /* set periodicstart */
169 #define FIT (1<<31)
170         u32 fmInterval = read32(OHCI0_HC_FM_INTERVAL) &0x3fff;
171         u32 fit = read32(OHCI0_HC_FM_INTERVAL) & FIT;
172
173         write32(OHCI0_HC_FM_INTERVAL, fmint | (fit ^ FIT));
174         write32(OHCI0_HC_PERIODIC_START, ((9*fmInterval)/10)&0x3fff);
175
176         /* testing bla */
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");
179         }
180         
181         /* start HC operations */
182         write32(OHCI0_HC_CONTROL, OHCI_CONTROL_INIT | OHCI_USB_OPER);
183
184         /* wake on ConnectStatusChange, matching external hubs */
185         set32(OHCI0_HC_RH_STATUS, RH_HS_DRWE);
186
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);
190
191         irq_restore(cookie);
192
193         dbg_op_state();
194 }
195
196 void hcdi_irq()
197 {
198         /* read interrupt status */
199         u32 flags = read32(OHCI0_HC_INT_STATUS);
200
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");
204                 return;
205         }
206
207         /* only care about interrupts that are enabled */
208         flags &= read32(OHCI0_HC_INT_ENABLE);
209
210         /* nothing to do? */
211         if (flags == 0)
212                 return;
213
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? ;-) */
220         }
221
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);
227         }
228         /* ResumeDetected */
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... */
233         }
234
235         /* WritebackDoneHead */
236         if (flags & OHCI_INTR_WDH) {
237                 printf("WritebackDoneHead\n");
238                 /* TODO: figure out what the linux kernel does here... */
239         }
240
241         /* TODO: handle any pending URB/ED unlinks... */
242
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);
247         }
248 }
249