Call yield() while waiting for USB control transfers to complete.
[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 *data_qh = malloc_low(sizeof(*data_qh));
40     struct uhci_qh *term_qh = malloc_high(sizeof(*term_qh));
41     if (!term_td || !fl || !data_qh || !term_qh) {
42         dprintf(1, "No ram for uhci init\n");
43         return;
44     }
45
46     // Work around for PIIX errata
47     memset(term_td, 0, sizeof(*term_td));
48     term_td->link = UHCI_PTR_TERM;
49     term_td->token = (uhci_explen(0) | (0x7f << TD_TOKEN_DEVADDR_SHIFT)
50                       | USB_PID_IN);
51     memset(term_qh, 0, sizeof(*term_qh));
52     term_qh->element = (u32)term_td;
53     term_qh->link = UHCI_PTR_TERM;
54
55     // Setup primary queue head.
56     memset(data_qh, 0, sizeof(*data_qh));
57     data_qh->element = UHCI_PTR_TERM;
58     data_qh->link = (u32)term_qh | UHCI_PTR_QH;
59     cntl->uhci.qh = data_qh;
60
61     // Set schedule to point to primary queue head
62     int i;
63     for (i=0; i<ARRAY_SIZE(fl->links); i++) {
64         fl->links[i] = (u32)data_qh | UHCI_PTR_QH;
65     }
66
67     // Set the frame length to the default: 1 ms exactly
68     outb(USBSOF_DEFAULT, cntl->uhci.iobase + USBSOF);
69
70     // Store the frame list base address
71     outl((u32)fl->links, cntl->uhci.iobase + USBFLBASEADD);
72
73     // Set the current frame number
74     outw(0, cntl->uhci.iobase + USBFRNUM);
75 }
76
77 static void
78 start_uhci(struct usb_s *cntl)
79 {
80     // Mark as configured and running with a 64-byte max packet.
81     outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, cntl->uhci.iobase + USBCMD);
82 }
83
84 // Find any devices connected to the root hub.
85 static int
86 check_ports(struct usb_s *cntl)
87 {
88     u16 port1 = inw(cntl->uhci.iobase + USBPORTSC1);
89     u16 port2 = inw(cntl->uhci.iobase + USBPORTSC2);
90
91     if (!((port1 & USBPORTSC_CCS) || (port2 & USBPORTSC_CCS)))
92         // No devices
93         return 0;
94
95     // reset ports
96     if (port1 & USBPORTSC_CCS)
97         outw(USBPORTSC_PR, cntl->uhci.iobase + USBPORTSC1);
98     if (port2 & USBPORTSC_CCS)
99         outw(USBPORTSC_PR, cntl->uhci.iobase + USBPORTSC2);
100     msleep(50);
101     outw(0, cntl->uhci.iobase + USBPORTSC1);
102     outw(0, cntl->uhci.iobase + USBPORTSC2);
103     msleep(10);
104
105     // Configure ports
106     int totalcount = 0;
107     port1 = inw(cntl->uhci.iobase + USBPORTSC1);
108     if (port1 & USBPORTSC_CCS) {
109         outw(USBPORTSC_PE, cntl->uhci.iobase + USBPORTSC1);
110         int count = configure_usb_device(cntl, !!(port1 & USBPORTSC_LSDA));
111         if (! count)
112             outw(0, cntl->uhci.iobase + USBPORTSC1);
113         totalcount += count;
114     }
115     port2 = inw(cntl->uhci.iobase + USBPORTSC2);
116     if (port2 & USBPORTSC_CCS) {
117         outw(USBPORTSC_PE, cntl->uhci.iobase + USBPORTSC2);
118         int count = configure_usb_device(cntl, !!(port2 & USBPORTSC_LSDA));
119         if (! count)
120             outw(0, cntl->uhci.iobase + USBPORTSC2);
121         totalcount += count;
122     }
123     return totalcount;
124 }
125
126 void
127 uhci_init(void *data)
128 {
129     if (! CONFIG_USB_UHCI)
130         return;
131     struct usb_s *cntl = data;
132
133     cntl->type = USB_TYPE_UHCI;
134     cntl->uhci.iobase = (pci_config_readl(cntl->bdf, PCI_BASE_ADDRESS_4)
135                          & PCI_BASE_ADDRESS_IO_MASK);
136
137     dprintf(3, "UHCI init on dev %02x:%02x.%x (io=%x)\n"
138             , pci_bdf_to_bus(cntl->bdf), pci_bdf_to_dev(cntl->bdf)
139             , pci_bdf_to_fn(cntl->bdf), cntl->uhci.iobase);
140
141     pci_config_maskw(cntl->bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER);
142
143     reset_uhci(cntl);
144     configure_uhci(cntl);
145     start_uhci(cntl);
146
147     int count = check_ports(cntl);
148     if (! count) {
149         // XXX - no devices; free data structures.
150     }
151 }
152
153 static int
154 wait_qh(struct usb_s *cntl, struct uhci_qh *qh)
155 {
156     // XXX - 500ms just a guess
157     u64 end = calc_future_tsc(500);
158     for (;;) {
159         if (qh->element & UHCI_PTR_TERM)
160             return 0;
161         if (check_time(end)) {
162             struct uhci_td *td = (void*)(qh->element & ~UHCI_PTR_BITS);
163             dprintf(1, "Timeout on wait_qh %p (td=%p s=%x c=%x/%x)\n"
164                     , qh, td, td->status
165                     , inw(cntl->uhci.iobase + USBCMD)
166                     , inw(cntl->uhci.iobase + USBSTS));
167             return -1;
168         }
169         yield();
170     }
171 }
172
173 int
174 uhci_control(u32 endp, int dir, const void *cmd, int cmdsize
175              , void *data, int datasize)
176 {
177     if (! CONFIG_USB_UHCI)
178         return -1;
179
180     dprintf(5, "uhci_control %x\n", endp);
181     struct usb_s *cntl = endp2cntl(endp);
182     int maxpacket = endp2maxsize(endp);
183     int lowspeed = endp2speed(endp);
184     int devaddr = endp2devaddr(endp) | (endp2ep(endp) << 7);
185
186     // Setup transfer descriptors
187     int count = 2 + DIV_ROUND_UP(datasize, maxpacket);
188     struct uhci_td *tds = malloc_tmphigh(sizeof(*tds) * count);
189
190     tds[0].link = (u32)&tds[1] | UHCI_PTR_DEPTH;
191     tds[0].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
192                      | TD_CTRL_ACTIVE);
193     tds[0].token = (uhci_explen(cmdsize) | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
194                     | USB_PID_SETUP);
195     tds[0].buffer = (void*)cmd;
196     int toggle = TD_TOKEN_TOGGLE;
197     int i;
198     for (i=1; i<count-1; i++) {
199         tds[i].link = (u32)&tds[i+1] | UHCI_PTR_DEPTH;
200         tds[i].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
201                          | TD_CTRL_ACTIVE);
202         int len = (i == count-2 ? (datasize - (i-1)*maxpacket) : maxpacket);
203         tds[i].token = (uhci_explen(len) | toggle
204                         | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
205                         | (dir ? USB_PID_IN : USB_PID_OUT));
206         tds[i].buffer = data + (i-1) * maxpacket;
207         toggle ^= TD_TOKEN_TOGGLE;
208     }
209     tds[i].link = UHCI_PTR_TERM;
210     tds[i].status = (uhci_maxerr(0) | (lowspeed ? TD_CTRL_LS : 0)
211                      | TD_CTRL_ACTIVE);
212     tds[i].token = (uhci_explen(0) | TD_TOKEN_TOGGLE
213                     | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
214                     | (dir ? USB_PID_OUT : USB_PID_IN));
215     tds[i].buffer = 0;
216
217     // Transfer data
218     struct uhci_qh *data_qh = cntl->uhci.qh;
219     data_qh->element = (u32)&tds[0];
220     int ret = wait_qh(cntl, data_qh);
221     if (ret)
222         // XXX - leak tds
223         return ret;
224     // XXX - free(tds);
225     return 0;
226 }
227
228 struct usb_pipe *
229 uhci_alloc_intr_pipe(u32 endp, int period)
230 {
231     if (! CONFIG_USB_UHCI)
232         return NULL;
233
234     dprintf(7, "uhci_alloc_intr_pipe %x %d\n", endp, period);
235     struct usb_s *cntl = endp2cntl(endp);
236     int maxpacket = endp2maxsize(endp);
237     int lowspeed = endp2speed(endp);
238     int devaddr = endp2devaddr(endp) | (endp2ep(endp) << 7);
239     // XXX - just grab 20 for now.
240     int count = 20;
241     struct uhci_qh *qh = malloc_low(sizeof(*qh));
242     struct uhci_td *tds = malloc_low(sizeof(*tds) * count);
243     if (!qh || !tds)
244         return NULL;
245     if (maxpacket > sizeof(tds[0].data))
246         // XXX - free qh/tds
247         return NULL;
248
249     qh->element = (u32)tds;
250     int toggle = 0;
251     int i;
252     for (i=0; i<count; i++) {
253         tds[i].link = (i==count-1 ? (u32)&tds[0] : (u32)&tds[i+1]);
254         tds[i].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
255                          | TD_CTRL_ACTIVE);
256         tds[i].token = (uhci_explen(maxpacket) | toggle
257                         | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
258                         | USB_PID_IN);
259         tds[i].buffer = &tds[i].data;
260         toggle ^= TD_TOKEN_TOGGLE;
261     }
262
263     qh->next_td = &tds[0];
264     qh->pipe.endp = endp;
265
266     // XXX - need schedule - just add to primary list for now.
267     struct uhci_qh *data_qh = cntl->uhci.qh;
268     qh->link = data_qh->link;
269     data_qh->link = (u32)qh | UHCI_PTR_QH;
270
271     return &qh->pipe;
272 }
273
274 int
275 uhci_poll_intr(struct usb_pipe *pipe, void *data)
276 {
277     ASSERT16();
278     if (! CONFIG_USB_UHCI)
279         return -1;
280
281     struct uhci_qh *qh = container_of(pipe, struct uhci_qh, pipe);
282     struct uhci_td *td = GET_FLATPTR(qh->next_td);
283     u32 status = GET_FLATPTR(td->status);
284     u32 token = GET_FLATPTR(td->token);
285     if (status & TD_CTRL_ACTIVE)
286         // No intrs found.
287         return -1;
288     // XXX - check for errors.
289
290     // Copy data.
291     memcpy_far(GET_SEG(SS), data
292                , FLATPTR_TO_SEG(td->data), (void*)FLATPTR_TO_OFFSET(td->data)
293                , uhci_expected_length(token));
294
295     // Reenable this td.
296     u32 next = GET_FLATPTR(td->link);
297     SET_FLATPTR(td->status, (uhci_maxerr(0) | (status & TD_CTRL_LS)
298                              | TD_CTRL_ACTIVE));
299     SET_FLATPTR(qh->next_td, (void*)(next & ~UHCI_PTR_BITS));
300
301     return 0;
302 }