Dynamically allocate USB controller structures.
[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 #include "usb-hub.h" // struct usbhub_s
15
16 #define FIT                     (1 << 31)
17
18 struct usb_ohci_s {
19     struct usb_s usb;
20     struct ohci_regs *regs;
21 };
22
23
24 /****************************************************************
25  * Root hub
26  ****************************************************************/
27
28 static void
29 init_ohci_port(void *data)
30 {
31     struct usbhub_s *hub = data;
32     u32 port = hub->port; // XXX - find better way to pass port
33     struct usb_ohci_s *cntl = container_of(hub->cntl, struct usb_ohci_s, usb);
34
35     u32 sts = readl(&cntl->regs->roothub_portstatus[port]);
36     if (!(sts & RH_PS_CCS))
37         // No device.
38         goto done;
39
40     // XXX - need to wait for USB_TIME_ATTDB if just powered up?
41
42     // Signal reset
43     mutex_lock(&cntl->usb.resetlock);
44     writel(&cntl->regs->roothub_portstatus[port], RH_PS_PRS);
45     u64 end = calc_future_tsc(USB_TIME_DRSTR * 2);
46     for (;;) {
47         sts = readl(&cntl->regs->roothub_portstatus[port]);
48         if (!(sts & RH_PS_PRS))
49             // XXX - need to ensure USB_TIME_DRSTR time in reset?
50             break;
51         if (check_time(end)) {
52             // Timeout.
53             warn_timeout();
54             goto resetfail;
55         }
56         yield();
57     }
58
59     if ((sts & (RH_PS_CCS|RH_PS_PES)) != (RH_PS_CCS|RH_PS_PES))
60         // Device no longer present
61         goto resetfail;
62
63     // Set address of port
64     struct usb_pipe *pipe = usb_set_address(&cntl->usb, !!(sts & RH_PS_LSDA));
65     if (!pipe)
66         goto resetfail;
67     mutex_unlock(&cntl->usb.resetlock);
68
69     // Configure the device
70     int count = configure_usb_device(pipe);
71     free_pipe(pipe);
72     if (! count)
73         // Shutdown port
74         writel(&cntl->regs->roothub_portstatus[port], RH_PS_CCS|RH_PS_LSDA);
75     hub->devcount += count;
76 done:
77     hub->threads--;
78     return;
79
80 resetfail:
81     // Shutdown port
82     writel(&cntl->regs->roothub_portstatus[port], RH_PS_CCS|RH_PS_LSDA);
83     mutex_unlock(&cntl->usb.resetlock);
84     goto done;
85 }
86
87 // Find any devices connected to the root hub.
88 static int
89 check_ohci_ports(struct usb_ohci_s *cntl)
90 {
91     ASSERT32FLAT();
92     // Turn on power for all devices on roothub.
93     u32 rha = readl(&cntl->regs->roothub_a);
94     rha &= ~(RH_A_PSM | RH_A_OCPM);
95     writel(&cntl->regs->roothub_status, RH_HS_LPSC);
96     writel(&cntl->regs->roothub_b, RH_B_PPCM);
97     msleep((rha >> 24) * 2);
98     // XXX - need to sleep for USB_TIME_SIGATT if just powered up?
99
100     // Lanuch a thread per port.
101     struct usbhub_s hub;
102     memset(&hub, 0, sizeof(hub));
103     hub.cntl = &cntl->usb;
104     int ports = rha & RH_A_NDP;
105     hub.threads = ports;
106     int i;
107     for (i=0; i<ports; i++) {
108         hub.port = i;
109         run_thread(init_ohci_port, &hub);
110     }
111
112     // Wait for threads to complete.
113     while (hub.threads)
114         yield();
115
116     return hub.devcount;
117 }
118
119
120 /****************************************************************
121  * Setup
122  ****************************************************************/
123
124 static int
125 start_ohci(struct usb_ohci_s *cntl, struct ohci_hcca *hcca)
126 {
127     u32 oldfminterval = readl(&cntl->regs->fminterval);
128     u32 oldrwc = readl(&cntl->regs->control) & OHCI_CTRL_RWC;
129
130     // XXX - check if already running?
131
132     // Do reset
133     writel(&cntl->regs->control, OHCI_USB_RESET | oldrwc);
134     readl(&cntl->regs->control); // flush writes
135     msleep(USB_TIME_DRSTR);
136
137     // Do software init (min 10us, max 2ms)
138     u64 end = calc_future_tsc_usec(10);
139     writel(&cntl->regs->cmdstatus, OHCI_HCR);
140     for (;;) {
141         u32 status = readl(&cntl->regs->cmdstatus);
142         if (! status & OHCI_HCR)
143             break;
144         if (check_time(end)) {
145             warn_timeout();
146             return -1;
147         }
148     }
149
150     // Init memory
151     writel(&cntl->regs->ed_controlhead, 0);
152     writel(&cntl->regs->ed_bulkhead, 0);
153     writel(&cntl->regs->hcca, (u32)hcca);
154
155     // Init fminterval
156     u32 fi = oldfminterval & 0x3fff;
157     writel(&cntl->regs->fminterval
158            , (((oldfminterval & FIT) ^ FIT)
159               | fi | (((6 * (fi - 210)) / 7) << 16)));
160     writel(&cntl->regs->periodicstart, ((9 * fi) / 10) & 0x3fff);
161     readl(&cntl->regs->control); // flush writes
162
163     // XXX - verify that fminterval was setup correctly.
164
165     // Go into operational state
166     writel(&cntl->regs->control
167            , (OHCI_CTRL_CBSR | OHCI_CTRL_CLE | OHCI_CTRL_PLE
168               | OHCI_USB_OPER | oldrwc));
169     readl(&cntl->regs->control); // flush writes
170
171     return 0;
172 }
173
174 static void
175 stop_ohci(struct usb_ohci_s *cntl)
176 {
177     u32 oldrwc = readl(&cntl->regs->control) & OHCI_CTRL_RWC;
178     writel(&cntl->regs->control, oldrwc);
179     readl(&cntl->regs->control); // flush writes
180 }
181
182 static void
183 configure_ohci(void *data)
184 {
185     struct usb_ohci_s *cntl = data;
186
187     // Allocate memory
188     struct ohci_hcca *hcca = memalign_high(256, sizeof(*hcca));
189     struct ohci_ed *intr_ed = malloc_high(sizeof(*intr_ed));
190     if (!hcca || !intr_ed) {
191         warn_noalloc();
192         goto free;
193     }
194     memset(hcca, 0, sizeof(*hcca));
195     memset(intr_ed, 0, sizeof(*intr_ed));
196     intr_ed->hwINFO = ED_SKIP;
197     int i;
198     for (i=0; i<ARRAY_SIZE(hcca->int_table); i++)
199         hcca->int_table[i] = (u32)intr_ed;
200
201     int ret = start_ohci(cntl, hcca);
202     if (ret)
203         goto err;
204
205     int count = check_ohci_ports(cntl);
206     free_pipe(cntl->usb.defaultpipe);
207     if (! count)
208         goto err;
209     return;
210
211 err:
212     stop_ohci(cntl);
213 free:
214     free(hcca);
215     free(intr_ed);
216 }
217
218 void
219 ohci_init(u16 bdf, int busid)
220 {
221     if (! CONFIG_USB_OHCI)
222         return;
223     struct usb_ohci_s *cntl = malloc_tmphigh(sizeof(*cntl));
224     memset(cntl, 0, sizeof(*cntl));
225     cntl->usb.busid = busid;
226     cntl->usb.type = USB_TYPE_OHCI;
227
228     u32 baseaddr = pci_config_readl(bdf, PCI_BASE_ADDRESS_0);
229     cntl->regs = (void*)(baseaddr & PCI_BASE_ADDRESS_MEM_MASK);
230
231     dprintf(3, "OHCI init on dev %02x:%02x.%x (regs=%p)\n"
232             , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf)
233             , pci_bdf_to_fn(bdf), cntl->regs);
234
235     // Enable bus mastering and memory access.
236     pci_config_maskw(bdf, PCI_COMMAND
237                      , 0, PCI_COMMAND_MASTER|PCI_COMMAND_MEMORY);
238
239     // XXX - check for and disable SMM control?
240
241     // Disable interrupts
242     writel(&cntl->regs->intrdisable, ~0);
243     writel(&cntl->regs->intrstatus, ~0);
244
245     run_thread(configure_ohci, cntl);
246 }
247
248
249 /****************************************************************
250  * End point communication
251  ****************************************************************/
252
253 static int
254 wait_ed(struct ohci_ed *ed)
255 {
256     // XXX - 500ms just a guess
257     u64 end = calc_future_tsc(500);
258     for (;;) {
259         if (ed->hwHeadP == ed->hwTailP)
260             return 0;
261         if (check_time(end)) {
262             warn_timeout();
263             return -1;
264         }
265         yield();
266     }
267 }
268
269 // Wait for next USB frame to start - for ensuring safe memory release.
270 static void
271 ohci_waittick(struct usb_ohci_s *cntl)
272 {
273     barrier();
274     struct ohci_hcca *hcca = (void*)cntl->regs->hcca;
275     u32 startframe = hcca->frame_no;
276     u64 end = calc_future_tsc(1000 * 5);
277     for (;;) {
278         if (hcca->frame_no != startframe)
279             break;
280         if (check_time(end)) {
281             warn_timeout();
282             return;
283         }
284         yield();
285     }
286 }
287
288 static void
289 signal_freelist(struct usb_ohci_s *cntl)
290 {
291     u32 v = readl(&cntl->regs->control);
292     if (v & OHCI_CTRL_CLE) {
293         writel(&cntl->regs->control, v & ~(OHCI_CTRL_CLE|OHCI_CTRL_BLE));
294         ohci_waittick(cntl);
295         writel(&cntl->regs->ed_controlcurrent, 0);
296         writel(&cntl->regs->ed_bulkcurrent, 0);
297         writel(&cntl->regs->control, v);
298     } else {
299         ohci_waittick(cntl);
300     }
301 }
302
303 struct ohci_pipe {
304     struct ohci_ed ed;
305     struct usb_pipe pipe;
306     void *data;
307     int count;
308     struct ohci_td *tds;
309 };
310
311 void
312 ohci_free_pipe(struct usb_pipe *p)
313 {
314     if (! CONFIG_USB_OHCI)
315         return;
316     dprintf(7, "ohci_free_pipe %p\n", p);
317     struct ohci_pipe *pipe = container_of(p, struct ohci_pipe, pipe);
318     struct usb_ohci_s *cntl = container_of(
319         pipe->pipe.cntl, struct usb_ohci_s, usb);
320
321     u32 *pos = &cntl->regs->ed_controlhead;
322     for (;;) {
323         struct ohci_ed *next = (void*)*pos;
324         if (!next) {
325             // Not found?!  Exit without freeing.
326             warn_internalerror();
327             return;
328         }
329         if (next == &pipe->ed) {
330             *pos = next->hwNextED;
331             signal_freelist(cntl);
332             free(pipe);
333             return;
334         }
335         pos = &next->hwNextED;
336     }
337 }
338
339 struct usb_pipe *
340 ohci_alloc_control_pipe(struct usb_pipe *dummy)
341 {
342     if (! CONFIG_USB_OHCI)
343         return NULL;
344     struct usb_ohci_s *cntl = container_of(
345         dummy->cntl, struct usb_ohci_s, usb);
346     dprintf(7, "ohci_alloc_control_pipe %p\n", &cntl->usb);
347
348     // Allocate a queue head.
349     struct ohci_pipe *pipe = malloc_tmphigh(sizeof(*pipe));
350     if (!pipe) {
351         warn_noalloc();
352         return NULL;
353     }
354     memset(pipe, 0, sizeof(*pipe));
355     pipe->ed.hwINFO = ED_SKIP;
356     memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
357
358     // Add queue head to controller list.
359     pipe->ed.hwNextED = cntl->regs->ed_controlhead;
360     barrier();
361     cntl->regs->ed_controlhead = (u32)&pipe->ed;
362     return &pipe->pipe;
363 }
364
365 int
366 ohci_control(struct usb_pipe *p, int dir, const void *cmd, int cmdsize
367              , void *data, int datasize)
368 {
369     if (! CONFIG_USB_OHCI)
370         return -1;
371     dprintf(5, "ohci_control %p\n", p);
372     if (datasize > 4096) {
373         // XXX - should support larger sizes.
374         warn_noalloc();
375         return -1;
376     }
377     struct ohci_pipe *pipe = container_of(p, struct ohci_pipe, pipe);
378     struct usb_ohci_s *cntl = container_of(
379         pipe->pipe.cntl, struct usb_ohci_s, usb);
380     int maxpacket = pipe->pipe.maxpacket;
381     int lowspeed = pipe->pipe.lowspeed;
382     int devaddr = pipe->pipe.devaddr | (pipe->pipe.ep << 7);
383
384     // Setup transfer descriptors
385     struct ohci_td *tds = malloc_tmphigh(sizeof(*tds) * 3);
386     tds[0].hwINFO = TD_DP_SETUP | TD_T_DATA0 | TD_CC;
387     tds[0].hwCBP = (u32)cmd;
388     tds[0].hwNextTD = (u32)&tds[1];
389     tds[0].hwBE = (u32)cmd + cmdsize - 1;
390     tds[1].hwINFO = (dir ? TD_DP_IN : TD_DP_OUT) | TD_T_DATA1 | TD_CC;
391     tds[1].hwCBP = datasize ? (u32)data : 0;
392     tds[1].hwNextTD = (u32)&tds[2];
393     tds[1].hwBE = (u32)data + datasize - 1;
394     tds[2].hwINFO = (dir ? TD_DP_OUT : TD_DP_IN) | TD_T_DATA1 | TD_CC;
395     tds[2].hwCBP = 0;
396     tds[2].hwNextTD = (u32)&tds[3];
397     tds[2].hwBE = 0;
398
399     // Transfer data
400     pipe->ed.hwINFO = ED_SKIP;
401     barrier();
402     pipe->ed.hwHeadP = (u32)&tds[0];
403     pipe->ed.hwTailP = (u32)&tds[3];
404     barrier();
405     pipe->ed.hwINFO = devaddr | (maxpacket << 16) | (lowspeed ? ED_LOWSPEED : 0);
406     writel(&cntl->regs->cmdstatus, OHCI_CLF);
407
408     int ret = wait_ed(&pipe->ed);
409     pipe->ed.hwINFO = ED_SKIP;
410     if (ret)
411         ohci_waittick(cntl);
412     free(tds);
413     return ret;
414 }
415
416 struct usb_pipe *
417 ohci_alloc_intr_pipe(struct usb_pipe *dummy, int frameexp)
418 {
419     if (! CONFIG_USB_OHCI)
420         return NULL;
421     struct usb_ohci_s *cntl = container_of(
422         dummy->cntl, struct usb_ohci_s, usb);
423     dprintf(7, "ohci_alloc_intr_pipe %p %d\n", &cntl->usb, frameexp);
424
425     if (frameexp > 5)
426         frameexp = 5;
427     int maxpacket = dummy->maxpacket;
428     int lowspeed = dummy->lowspeed;
429     int devaddr = dummy->devaddr | (dummy->ep << 7);
430     // Determine number of entries needed for 2 timer ticks.
431     int ms = 1<<frameexp;
432     int count = DIV_ROUND_UP(PIT_TICK_INTERVAL * 1000 * 2, PIT_TICK_RATE * ms);
433     struct ohci_pipe *pipe = malloc_low(sizeof(*pipe));
434     struct ohci_td *tds = malloc_low(sizeof(*tds) * count);
435     void *data = malloc_low(maxpacket * count);
436     if (!pipe || !tds || !data)
437         goto err;
438
439     struct ohci_ed *ed = &pipe->ed;
440     ed->hwHeadP = (u32)&tds[0];
441     ed->hwTailP = (u32)&tds[count-1];
442     ed->hwINFO = devaddr | (maxpacket << 16) | (lowspeed ? ED_LOWSPEED : 0);
443
444     int i;
445     for (i=0; i<count-1; i++) {
446         tds[i].hwINFO = TD_DP_IN | TD_T_TOGGLE | TD_CC;
447         tds[i].hwCBP = (u32)data + maxpacket * i;
448         tds[i].hwNextTD = (u32)&tds[i+1];
449         tds[i].hwBE = tds[i].hwCBP + maxpacket - 1;
450     }
451
452     // Add to interrupt schedule.
453     barrier();
454     struct ohci_hcca *hcca = (void*)cntl->regs->hcca;
455     if (frameexp == 0) {
456         // Add to existing interrupt entry.
457         struct ohci_ed *intr_ed = (void*)hcca->int_table[0];
458         ed->hwNextED = intr_ed->hwNextED;
459         intr_ed->hwNextED = (u32)ed;
460     } else {
461         int startpos = 1<<(frameexp-1);
462         ed->hwNextED = hcca->int_table[startpos];
463         for (i=startpos; i<ARRAY_SIZE(hcca->int_table); i+=ms)
464             hcca->int_table[i] = (u32)ed;
465     }
466
467     pipe->data = data;
468     pipe->count = count;
469     pipe->tds = tds;
470     memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
471     return &pipe->pipe;
472
473 err:
474     free(pipe);
475     free(tds);
476     free(data);
477     return NULL;
478 }
479
480 int
481 ohci_poll_intr(struct usb_pipe *p, void *data)
482 {
483     ASSERT16();
484     if (! CONFIG_USB_OHCI)
485         return -1;
486
487     struct ohci_pipe *pipe = container_of(p, struct ohci_pipe, pipe);
488     struct ohci_td *tds = GET_FLATPTR(pipe->tds);
489     struct ohci_td *head = (void*)GET_FLATPTR(pipe->ed.hwHeadP);
490     struct ohci_td *tail = (void*)GET_FLATPTR(pipe->ed.hwTailP);
491     int count = GET_FLATPTR(pipe->count);
492     int pos = (tail - tds + 1) % count;
493     struct ohci_td *next = &tds[pos];
494     if (head == next)
495         // No intrs found.
496         return -1;
497     // XXX - check for errors.
498
499     // Copy data.
500     int maxpacket = GET_FLATPTR(pipe->pipe.maxpacket);
501     void *pipedata = GET_FLATPTR(pipe->data);
502     void *intrdata = pipedata + maxpacket * pos;
503     memcpy_far(GET_SEG(SS), data
504                , FLATPTR_TO_SEG(intrdata), (void*)FLATPTR_TO_OFFSET(intrdata)
505                , maxpacket);
506
507     // Reenable this td.
508     SET_FLATPTR(tail->hwINFO, TD_DP_IN | TD_T_TOGGLE | TD_CC);
509     intrdata = pipedata + maxpacket * (tail-tds);
510     SET_FLATPTR(tail->hwCBP, (u32)intrdata);
511     SET_FLATPTR(tail->hwNextTD, (u32)next);
512     SET_FLATPTR(tail->hwBE, (u32)intrdata + maxpacket - 1);
513
514     SET_FLATPTR(pipe->ed.hwTailP, (u32)next);
515
516     return 0;
517 }