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