Enhance experimental option rom "threading" - enable preemption.
[seabios.git] / src / usb-ohci.c
1 // Code for handling OHCI USB controllers.
2 //
3 // Copyright (C) 2009  Kevin O'Connor <kevin@koconnor.net>
4 //
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
6
7 #include "util.h" // dprintf
8 #include "pci.h" // pci_bdf_to_bus
9 #include "config.h" // CONFIG_*
10 #include "usb-ohci.h" // struct ohci_hcca
11 #include "pci_regs.h" // PCI_BASE_ADDRESS_0
12 #include "usb.h" // struct usb_s
13 #include "farptr.h" // GET_FLATPTR
14
15 #define FIT                     (1 << 31)
16
17 static int
18 start_ohci(struct usb_s *cntl, struct ohci_hcca *hcca)
19 {
20     u32 oldfminterval = readl(&cntl->ohci.regs->fminterval);
21     u32 oldrwc = readl(&cntl->ohci.regs->control) & OHCI_CTRL_RWC;
22
23     // XXX - check if already running?
24
25     // Do reset
26     writel(&cntl->ohci.regs->control, OHCI_USB_RESET | oldrwc);
27     readl(&cntl->ohci.regs->control); // flush writes
28     msleep(50);
29
30     // Do software init (min 10us, max 2ms)
31     u64 end = calc_future_tsc_usec(10);
32     writel(&cntl->ohci.regs->cmdstatus, OHCI_HCR);
33     for (;;) {
34         u32 status = readl(&cntl->ohci.regs->cmdstatus);
35         if (! status & OHCI_HCR)
36             break;
37         if (check_time(end)) {
38             dprintf(1, "Timeout on ohci software reset\n");
39             return -1;
40         }
41     }
42
43     // Init memory
44     writel(&cntl->ohci.regs->ed_controlhead, (u32)cntl->ohci.control_ed);
45     writel(&cntl->ohci.regs->ed_bulkhead, 0);
46     writel(&cntl->ohci.regs->hcca, (u32)hcca);
47
48     // Init fminterval
49     u32 fi = oldfminterval & 0x3fff;
50     writel(&cntl->ohci.regs->fminterval
51            , (((oldfminterval & FIT) ^ FIT)
52               | fi | (((6 * (fi - 210)) / 7) << 16)));
53     writel(&cntl->ohci.regs->periodicstart, ((9 * fi) / 10) & 0x3fff);
54     readl(&cntl->ohci.regs->control); // flush writes
55
56     // XXX - verify that fminterval was setup correctly.
57
58     // Go into operational state
59     writel(&cntl->ohci.regs->control
60            , (OHCI_CTRL_CBSR | OHCI_CTRL_CLE | OHCI_CTRL_PLE
61               | OHCI_USB_OPER | oldrwc));
62     readl(&cntl->ohci.regs->control); // flush writes
63
64     return 0;
65 }
66
67 static void
68 stop_ohci(struct usb_s *cntl)
69 {
70     u32 oldrwc = readl(&cntl->ohci.regs->control) & OHCI_CTRL_RWC;
71     writel(&cntl->ohci.regs->control, oldrwc);
72     readl(&cntl->ohci.regs->control); // flush writes
73 }
74
75 // Find any devices connected to the root hub.
76 static int
77 check_ohci_ports(struct usb_s *cntl)
78 {
79     // Turn on power for all devices on roothub.
80     u32 rha = readl(&cntl->ohci.regs->roothub_a);
81     rha &= ~(RH_A_PSM | RH_A_OCPM);
82     writel(&cntl->ohci.regs->roothub_status, RH_HS_LPSC);
83     writel(&cntl->ohci.regs->roothub_b, RH_B_PPCM);
84     msleep((rha >> 24) * 2);
85
86     // Count and reset connected devices
87     int ports = rha & RH_A_NDP;
88     int totalcount = 0;
89     int i;
90     for (i=0; i<ports; i++)
91         if (readl(&cntl->ohci.regs->roothub_portstatus[i]) & RH_PS_CCS) {
92             writel(&cntl->ohci.regs->roothub_portstatus[i], RH_PS_PRS);
93             totalcount++;
94         }
95     if (!totalcount)
96         // No devices connected
97         goto shutdown;
98
99     msleep(60);    // XXX - should poll instead of using timer.
100
101     totalcount = 0;
102     for (i=0; i<ports; i++) {
103         u32 sts = readl(&cntl->ohci.regs->roothub_portstatus[i]);
104         if ((sts & (RH_PS_CCS|RH_PS_PES)) == (RH_PS_CCS|RH_PS_PES)) {
105             int count = configure_usb_device(cntl, !!(sts & RH_PS_LSDA));
106             if (! count)
107                 // Shutdown port
108                 writel(&cntl->ohci.regs->roothub_portstatus[i]
109                        , RH_PS_CCS|RH_PS_LSDA);
110             totalcount += count;
111         }
112     }
113     if (!totalcount)
114         goto shutdown;
115
116     return totalcount;
117
118 shutdown:
119     // Turn off power to all ports
120     writel(&cntl->ohci.regs->roothub_status, RH_HS_LPS);
121     return 0;
122 }
123
124 void
125 ohci_init(void *data)
126 {
127     if (! CONFIG_USB_OHCI)
128         return;
129     struct usb_s *cntl = data;
130
131     // XXX - don't call pci_config_XXX from a thread
132     cntl->type = USB_TYPE_OHCI;
133     u32 baseaddr = pci_config_readl(cntl->bdf, PCI_BASE_ADDRESS_0);
134     cntl->ohci.regs = (void*)(baseaddr & PCI_BASE_ADDRESS_MEM_MASK);
135
136     dprintf(3, "OHCI init on dev %02x:%02x.%x (regs=%p)\n"
137             , pci_bdf_to_bus(cntl->bdf), pci_bdf_to_dev(cntl->bdf)
138             , pci_bdf_to_fn(cntl->bdf), cntl->ohci.regs);
139
140     // Enable bus mastering and memory access.
141     pci_config_maskw(cntl->bdf, PCI_COMMAND
142                      , 0, PCI_COMMAND_MASTER|PCI_COMMAND_MEMORY);
143
144     // XXX - check for and disable SMM control?
145
146     // Disable interrupts
147     writel(&cntl->ohci.regs->intrdisable, ~0);
148     writel(&cntl->ohci.regs->intrstatus, ~0);
149
150     // Allocate memory
151     struct ohci_hcca *hcca = memalign_high(256, sizeof(*hcca));
152     struct ohci_ed *control_ed = malloc_high(sizeof(*control_ed));
153     if (!hcca || !control_ed) {
154         dprintf(1, "No ram for ohci init\n");
155         return;
156     }
157     memset(hcca, 0, sizeof(*hcca));
158     memset(control_ed, 0, sizeof(*control_ed));
159     control_ed->hwINFO = ED_SKIP;
160     cntl->ohci.control_ed = control_ed;
161
162     int ret = start_ohci(cntl, hcca);
163     if (ret)
164         goto err;
165
166     int count = check_ohci_ports(cntl);
167     if (! count)
168         goto err;
169     return;
170
171 err:
172     stop_ohci(cntl);
173     free(hcca);
174     free(control_ed);
175 }
176
177 static int
178 wait_ed(struct ohci_ed *ed)
179 {
180     // XXX - 500ms just a guess
181     u64 end = calc_future_tsc(500);
182     for (;;) {
183         if (ed->hwHeadP == ed->hwTailP)
184             return 0;
185         if (check_time(end)) {
186             dprintf(1, "Timeout on wait_ed %p\n", ed);
187             return -1;
188         }
189         yield();
190     }
191 }
192
193 int
194 ohci_control(u32 endp, int dir, const void *cmd, int cmdsize
195              , void *data, int datasize)
196 {
197     if (! CONFIG_USB_OHCI)
198         return -1;
199
200     dprintf(5, "ohci_control %x\n", endp);
201     struct usb_s *cntl = endp2cntl(endp);
202     int maxpacket = endp2maxsize(endp);
203     int lowspeed = endp2speed(endp);
204     int devaddr = endp2devaddr(endp) | (endp2ep(endp) << 7);
205
206     // Setup transfer descriptors
207     struct ohci_td *tds = malloc_tmphigh(sizeof(*tds) * 3);
208     tds[0].hwINFO = TD_DP_SETUP | TD_T_DATA0 | TD_CC;
209     tds[0].hwCBP = (u32)cmd;
210     tds[0].hwNextTD = (u32)&tds[1];
211     tds[0].hwBE = (u32)cmd + cmdsize - 1;
212     tds[1].hwINFO = (dir ? TD_DP_IN : TD_DP_OUT) | TD_T_DATA1 | TD_CC;
213     tds[1].hwCBP = datasize ? (u32)data : 0;
214     tds[1].hwNextTD = (u32)&tds[2];
215     tds[1].hwBE = (u32)data + datasize - 1;
216     tds[2].hwINFO = (dir ? TD_DP_OUT : TD_DP_IN) | TD_T_DATA1 | TD_CC;
217     tds[2].hwCBP = 0;
218     tds[2].hwNextTD = (u32)&tds[3];
219     tds[2].hwBE = 0;
220
221     // Transfer data
222     struct ohci_ed *ed = cntl->ohci.control_ed;
223     ed->hwINFO = ED_SKIP;
224     barrier();
225     ed->hwHeadP = (u32)&tds[0];
226     ed->hwTailP = (u32)&tds[3];
227     barrier();
228     ed->hwINFO = devaddr | (maxpacket << 16) | (lowspeed ? ED_LOWSPEED : 0);
229     writel(&cntl->ohci.regs->cmdstatus, OHCI_CLF);
230
231     int ret = wait_ed(ed);
232     ed->hwINFO = ED_SKIP;
233     if (ret)
234         usleep(1); // XXX - in case controller still accessing tds
235     free(tds);
236     return ret;
237 }
238
239 struct ohci_pipe {
240     struct ohci_ed ed;
241     struct usb_pipe pipe;
242     void *data;
243     int count;
244     struct ohci_td *tds;
245 };
246
247 struct usb_pipe *
248 ohci_alloc_intr_pipe(u32 endp, int period)
249 {
250     if (! CONFIG_USB_OHCI)
251         return NULL;
252
253     dprintf(7, "ohci_alloc_intr_pipe %x %d\n", endp, period);
254     struct usb_s *cntl = endp2cntl(endp);
255     int maxpacket = endp2maxsize(endp);
256     int lowspeed = endp2speed(endp);
257     int devaddr = endp2devaddr(endp) | (endp2ep(endp) << 7);
258     // XXX - just grab 20 for now.
259     int count = 20;
260     struct ohci_pipe *pipe = malloc_low(sizeof(*pipe));
261     struct ohci_td *tds = malloc_low(sizeof(*tds) * count);
262     void *data = malloc_low(maxpacket * count);
263     if (!pipe || !tds || !data)
264         goto err;
265
266     struct ohci_ed *ed = &pipe->ed;
267     ed->hwHeadP = (u32)&tds[0];
268     ed->hwTailP = (u32)&tds[count-1];
269     ed->hwINFO = devaddr | (maxpacket << 16) | (lowspeed ? ED_LOWSPEED : 0);
270     ed->hwNextED = 0;
271
272     int i;
273     for (i=0; i<count-1; i++) {
274         tds[i].hwINFO = TD_DP_IN | TD_T_TOGGLE | TD_CC;
275         tds[i].hwCBP = (u32)data + maxpacket * i;
276         tds[i].hwNextTD = (u32)&tds[i+1];
277         tds[i].hwBE = tds[i].hwCBP + maxpacket - 1;
278     }
279
280     // XXX - need schedule - just add to primary list for now.
281     barrier();
282     struct ohci_hcca *hcca = (void*)cntl->ohci.regs->hcca;
283     for (i=0; i<ARRAY_SIZE(hcca->int_table); i++)
284         hcca->int_table[i] = (u32)ed;
285
286     pipe->data = data;
287     pipe->count = count;
288     pipe->tds = tds;
289     pipe->pipe.endp = endp;
290     return &pipe->pipe;
291
292 err:
293     free(pipe);
294     free(tds);
295     free(data);
296     return NULL;
297 }
298
299 int
300 ohci_poll_intr(struct usb_pipe *pipe, void *data)
301 {
302     ASSERT16();
303     if (! CONFIG_USB_OHCI)
304         return -1;
305
306     struct ohci_pipe *p = container_of(pipe, struct ohci_pipe, pipe);
307     struct ohci_td *tds = GET_FLATPTR(p->tds);
308     struct ohci_td *head = (void*)GET_FLATPTR(p->ed.hwHeadP);
309     struct ohci_td *tail = (void*)GET_FLATPTR(p->ed.hwTailP);
310     int count = GET_FLATPTR(p->count);
311     int pos = (tail - tds + 1) % count;
312     struct ohci_td *next = &tds[pos];
313     if (head == next)
314         // No intrs found.
315         return -1;
316     // XXX - check for errors.
317
318     // Copy data.
319     u32 endp = GET_FLATPTR(p->pipe.endp);
320     int maxpacket = endp2maxsize(endp);
321     void *pipedata = GET_FLATPTR(p->data);
322     void *intrdata = pipedata + maxpacket * pos;
323     memcpy_far(GET_SEG(SS), data
324                , FLATPTR_TO_SEG(intrdata), (void*)FLATPTR_TO_OFFSET(intrdata)
325                , maxpacket);
326
327     // Reenable this td.
328     SET_FLATPTR(tail->hwINFO, TD_DP_IN | TD_T_TOGGLE | TD_CC);
329     intrdata = pipedata + maxpacket * (tail-tds);
330     SET_FLATPTR(tail->hwCBP, (u32)intrdata);
331     SET_FLATPTR(tail->hwNextTD, (u32)next);
332     SET_FLATPTR(tail->hwBE, (u32)intrdata + maxpacket - 1);
333
334     SET_FLATPTR(p->ed.hwTailP, (u32)next);
335
336     return 0;
337 }