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