Don't leave USB UHCI ports disabled for extended time during reset.
[seabios.git] / src / usb-uhci.c
1 // Code for handling UHCI 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 "ioport.h" // outw
11 #include "usb-uhci.h" // USBLEGSUP
12 #include "pci_regs.h" // PCI_BASE_ADDRESS_4
13 #include "usb.h" // struct usb_s
14 #include "farptr.h" // GET_FLATPTR
15
16 static void
17 reset_uhci(struct usb_s *cntl)
18 {
19     // XXX - don't reset if not needed.
20
21     // Reset PIRQ and SMI
22     pci_config_writew(cntl->bdf, USBLEGSUP, USBLEGSUP_RWC);
23
24     // Reset the HC
25     outw(USBCMD_HCRESET, cntl->uhci.iobase + USBCMD);
26     udelay(5);
27
28     // Disable interrupts and commands (just to be safe).
29     outw(0, cntl->uhci.iobase + USBINTR);
30     outw(0, cntl->uhci.iobase + USBCMD);
31 }
32
33 static void
34 configure_uhci(struct usb_s *cntl)
35 {
36     // Allocate ram for schedule storage
37     struct uhci_td *term_td = malloc_high(sizeof(*term_td));
38     struct uhci_framelist *fl = memalign_high(sizeof(*fl), sizeof(*fl));
39     struct uhci_qh *intr_qh = malloc_high(sizeof(*intr_qh));
40     struct uhci_qh *data_qh = malloc_high(sizeof(*data_qh));
41     struct uhci_qh *term_qh = malloc_high(sizeof(*term_qh));
42     if (!term_td || !fl || !intr_qh || !data_qh || !term_qh) {
43         warn_noalloc();
44         free(term_td);
45         free(fl);
46         free(intr_qh);
47         free(data_qh);
48         free(term_qh);
49         return;
50     }
51
52     // Work around for PIIX errata
53     memset(term_td, 0, sizeof(*term_td));
54     term_td->link = UHCI_PTR_TERM;
55     term_td->token = (uhci_explen(0) | (0x7f << TD_TOKEN_DEVADDR_SHIFT)
56                       | USB_PID_IN);
57     memset(term_qh, 0, sizeof(*term_qh));
58     term_qh->element = (u32)term_td;
59     term_qh->link = UHCI_PTR_TERM;
60
61     // Setup primary queue head.
62     memset(data_qh, 0, sizeof(*data_qh));
63     data_qh->element = UHCI_PTR_TERM;
64     data_qh->link = (u32)term_qh | UHCI_PTR_QH;
65     cntl->uhci.qh = data_qh;
66
67     // Set schedule to point to primary intr queue head
68     memset(intr_qh, 0, sizeof(*intr_qh));
69     intr_qh->element = UHCI_PTR_TERM;
70     intr_qh->link = (u32)data_qh | UHCI_PTR_QH;
71     int i;
72     for (i=0; i<ARRAY_SIZE(fl->links); i++)
73         fl->links[i] = (u32)intr_qh | UHCI_PTR_QH;
74     cntl->uhci.framelist = fl;
75
76     // Set the frame length to the default: 1 ms exactly
77     outb(USBSOF_DEFAULT, cntl->uhci.iobase + USBSOF);
78
79     // Store the frame list base address
80     outl((u32)fl->links, cntl->uhci.iobase + USBFLBASEADD);
81
82     // Set the current frame number
83     outw(0, cntl->uhci.iobase + USBFRNUM);
84 }
85
86 static void
87 start_uhci(struct usb_s *cntl)
88 {
89     // Mark as configured and running with a 64-byte max packet.
90     outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, cntl->uhci.iobase + USBCMD);
91 }
92
93 // Find any devices connected to the root hub.
94 static int
95 check_ports(struct usb_s *cntl)
96 {
97     // XXX - if just powered up, need to wait for USB_TIME_SIGATT?
98     u16 port1 = inw(cntl->uhci.iobase + USBPORTSC1);
99     u16 port2 = inw(cntl->uhci.iobase + USBPORTSC2);
100
101     if (!((port1 & USBPORTSC_CCS) || (port2 & USBPORTSC_CCS)))
102         // No devices
103         return 0;
104
105     // XXX - if just powered up, need to wait for USB_TIME_ATTDB?
106
107     // reset ports
108     if (port1 & USBPORTSC_CCS)
109         outw(USBPORTSC_PR, cntl->uhci.iobase + USBPORTSC1);
110     if (port2 & USBPORTSC_CCS)
111         outw(USBPORTSC_PR, cntl->uhci.iobase + USBPORTSC2);
112     msleep(USB_TIME_DRSTR);
113
114     // Configure ports
115     int totalcount = 0;
116     outw(0, cntl->uhci.iobase + USBPORTSC1);
117     udelay(6); // 64 high-speed bit times
118     port1 = inw(cntl->uhci.iobase + USBPORTSC1);
119     if (port1 & USBPORTSC_CCS) {
120         outw(USBPORTSC_PE, cntl->uhci.iobase + USBPORTSC1);
121         msleep(USB_TIME_RSTRCY);
122         int count = configure_usb_device(cntl, !!(port1 & USBPORTSC_LSDA));
123         if (! count)
124             outw(0, cntl->uhci.iobase + USBPORTSC1);
125         totalcount += count;
126     }
127     outw(0, cntl->uhci.iobase + USBPORTSC2);
128     udelay(6);
129     port2 = inw(cntl->uhci.iobase + USBPORTSC2);
130     if (port2 & USBPORTSC_CCS) {
131         outw(USBPORTSC_PE, cntl->uhci.iobase + USBPORTSC2);
132         msleep(USB_TIME_RSTRCY);
133         int count = configure_usb_device(cntl, !!(port2 & USBPORTSC_LSDA));
134         if (! count)
135             outw(0, cntl->uhci.iobase + USBPORTSC2);
136         totalcount += count;
137     }
138     return totalcount;
139 }
140
141 void
142 uhci_init(void *data)
143 {
144     if (! CONFIG_USB_UHCI)
145         return;
146     struct usb_s *cntl = data;
147
148     // XXX - don't call pci_config_XXX from a thread
149     cntl->type = USB_TYPE_UHCI;
150     cntl->uhci.iobase = (pci_config_readl(cntl->bdf, PCI_BASE_ADDRESS_4)
151                          & PCI_BASE_ADDRESS_IO_MASK);
152
153     dprintf(3, "UHCI init on dev %02x:%02x.%x (io=%x)\n"
154             , pci_bdf_to_bus(cntl->bdf), pci_bdf_to_dev(cntl->bdf)
155             , pci_bdf_to_fn(cntl->bdf), cntl->uhci.iobase);
156
157     pci_config_maskw(cntl->bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER);
158
159     reset_uhci(cntl);
160     configure_uhci(cntl);
161     start_uhci(cntl);
162
163     int count = check_ports(cntl);
164     if (! count) {
165         // XXX - no devices; free data structures.
166     }
167 }
168
169 static int
170 wait_qh(struct usb_s *cntl, struct uhci_qh *qh)
171 {
172     // XXX - 500ms just a guess
173     u64 end = calc_future_tsc(500);
174     for (;;) {
175         if (qh->element & UHCI_PTR_TERM)
176             return 0;
177         if (check_time(end)) {
178             warn_timeout();
179             struct uhci_td *td = (void*)(qh->element & ~UHCI_PTR_BITS);
180             dprintf(1, "Timeout on wait_qh %p (td=%p s=%x c=%x/%x)\n"
181                     , qh, td, td->status
182                     , inw(cntl->uhci.iobase + USBCMD)
183                     , inw(cntl->uhci.iobase + USBSTS));
184             return -1;
185         }
186         yield();
187     }
188 }
189
190 int
191 uhci_control(u32 endp, int dir, const void *cmd, int cmdsize
192              , void *data, int datasize)
193 {
194     if (! CONFIG_USB_UHCI)
195         return -1;
196
197     dprintf(5, "uhci_control %x\n", endp);
198     struct usb_s *cntl = endp2cntl(endp);
199     int maxpacket = endp2maxsize(endp);
200     int lowspeed = endp2speed(endp);
201     int devaddr = endp2devaddr(endp) | (endp2ep(endp) << 7);
202
203     // Setup transfer descriptors
204     int count = 2 + DIV_ROUND_UP(datasize, maxpacket);
205     struct uhci_td *tds = malloc_tmphigh(sizeof(*tds) * count);
206
207     tds[0].link = (u32)&tds[1] | UHCI_PTR_DEPTH;
208     tds[0].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
209                      | TD_CTRL_ACTIVE);
210     tds[0].token = (uhci_explen(cmdsize) | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
211                     | USB_PID_SETUP);
212     tds[0].buffer = (void*)cmd;
213     int toggle = TD_TOKEN_TOGGLE;
214     int i;
215     for (i=1; i<count-1; i++) {
216         tds[i].link = (u32)&tds[i+1] | UHCI_PTR_DEPTH;
217         tds[i].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
218                          | TD_CTRL_ACTIVE);
219         int len = (i == count-2 ? (datasize - (i-1)*maxpacket) : maxpacket);
220         tds[i].token = (uhci_explen(len) | toggle
221                         | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
222                         | (dir ? USB_PID_IN : USB_PID_OUT));
223         tds[i].buffer = data + (i-1) * maxpacket;
224         toggle ^= TD_TOKEN_TOGGLE;
225     }
226     tds[i].link = UHCI_PTR_TERM;
227     tds[i].status = (uhci_maxerr(0) | (lowspeed ? TD_CTRL_LS : 0)
228                      | TD_CTRL_ACTIVE);
229     tds[i].token = (uhci_explen(0) | TD_TOKEN_TOGGLE
230                     | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
231                     | (dir ? USB_PID_OUT : USB_PID_IN));
232     tds[i].buffer = 0;
233
234     // Transfer data
235     struct uhci_qh *data_qh = cntl->uhci.qh;
236     data_qh->element = (u32)&tds[0];
237     int ret = wait_qh(cntl, data_qh);
238     if (ret) {
239         data_qh->element = UHCI_PTR_TERM;
240         // XXX - leak tds
241         return ret;
242     }
243     free(tds);
244     return 0;
245 }
246
247 struct usb_pipe *
248 uhci_alloc_intr_pipe(u32 endp, int frameexp)
249 {
250     if (! CONFIG_USB_UHCI)
251         return NULL;
252
253     dprintf(7, "uhci_alloc_intr_pipe %x %d\n", endp, frameexp);
254     if (frameexp > 10)
255         frameexp = 10;
256     struct usb_s *cntl = endp2cntl(endp);
257     int maxpacket = endp2maxsize(endp);
258     int lowspeed = endp2speed(endp);
259     int devaddr = endp2devaddr(endp) | (endp2ep(endp) << 7);
260     // Determine number of entries needed for 2 timer ticks.
261     int ms = 1<<frameexp;
262     int count = DIV_ROUND_UP(PIT_TICK_INTERVAL * 1000 * 2, PIT_TICK_RATE * ms);
263     struct uhci_qh *qh = malloc_low(sizeof(*qh));
264     struct uhci_td *tds = malloc_low(sizeof(*tds) * count);
265     if (!qh || !tds || maxpacket > sizeof(tds[0].data)) {
266         free(qh);
267         free(tds);
268         return NULL;
269     }
270     qh->element = (u32)tds;
271     int toggle = 0;
272     int i;
273     for (i=0; i<count; i++) {
274         tds[i].link = (i==count-1 ? (u32)&tds[0] : (u32)&tds[i+1]);
275         tds[i].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
276                          | TD_CTRL_ACTIVE);
277         tds[i].token = (uhci_explen(maxpacket) | toggle
278                         | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
279                         | USB_PID_IN);
280         tds[i].buffer = &tds[i].data;
281         toggle ^= TD_TOKEN_TOGGLE;
282     }
283
284     qh->next_td = &tds[0];
285     qh->pipe.endp = endp;
286
287     // Add to interrupt schedule.
288     struct uhci_framelist *fl = cntl->uhci.framelist;
289     if (frameexp == 0) {
290         // Add to existing interrupt entry.
291         struct uhci_qh *intr_qh = (void*)(fl->links[0] & ~UHCI_PTR_BITS);
292         qh->link = intr_qh->link;
293         intr_qh->link = (u32)qh | UHCI_PTR_QH;
294     } else {
295         int startpos = 1<<(frameexp-1);
296         qh->link = fl->links[startpos];
297         for (i=startpos; i<ARRAY_SIZE(fl->links); i+=ms)
298             fl->links[i] = (u32)qh | UHCI_PTR_QH;
299     }
300
301     return &qh->pipe;
302 }
303
304 int
305 uhci_poll_intr(struct usb_pipe *pipe, void *data)
306 {
307     ASSERT16();
308     if (! CONFIG_USB_UHCI)
309         return -1;
310
311     struct uhci_qh *qh = container_of(pipe, struct uhci_qh, pipe);
312     struct uhci_td *td = GET_FLATPTR(qh->next_td);
313     u32 status = GET_FLATPTR(td->status);
314     u32 token = GET_FLATPTR(td->token);
315     if (status & TD_CTRL_ACTIVE)
316         // No intrs found.
317         return -1;
318     // XXX - check for errors.
319
320     // Copy data.
321     memcpy_far(GET_SEG(SS), data
322                , FLATPTR_TO_SEG(td->data), (void*)FLATPTR_TO_OFFSET(td->data)
323                , uhci_expected_length(token));
324
325     // Reenable this td.
326     u32 next = GET_FLATPTR(td->link);
327     SET_FLATPTR(td->status, (uhci_maxerr(0) | (status & TD_CTRL_LS)
328                              | TD_CTRL_ACTIVE));
329     SET_FLATPTR(qh->next_td, (void*)(next & ~UHCI_PTR_BITS));
330
331     return 0;
332 }