blob: b7ff394636c1c78200ef0187242906fb8fdcf65e [file] [log] [blame]
Kevin O'Connor114592f2009-09-28 21:32:08 -04001// 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
Kevin O'Connor114592f2009-09-28 21:32:08 -040015
16static void
17reset_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
Kevin O'Connor59f02832009-10-12 10:09:15 -040025 outw(USBCMD_HCRESET, cntl->uhci.iobase + USBCMD);
Kevin O'Connor114592f2009-09-28 21:32:08 -040026 udelay(5);
27
28 // Disable interrupts and commands (just to be safe).
Kevin O'Connor59f02832009-10-12 10:09:15 -040029 outw(0, cntl->uhci.iobase + USBINTR);
30 outw(0, cntl->uhci.iobase + USBCMD);
Kevin O'Connor114592f2009-09-28 21:32:08 -040031}
32
33static void
34configure_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));
Kevin O'Connor991eaff2010-02-13 21:51:47 -050039 struct uhci_qh *intr_qh = malloc_high(sizeof(*intr_qh));
40 struct uhci_qh *data_qh = malloc_high(sizeof(*data_qh));
Kevin O'Connor114592f2009-09-28 21:32:08 -040041 struct uhci_qh *term_qh = malloc_high(sizeof(*term_qh));
Kevin O'Connor991eaff2010-02-13 21:51:47 -050042 if (!term_td || !fl || !intr_qh || !data_qh || !term_qh) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -050043 warn_noalloc();
Kevin O'Connor991eaff2010-02-13 21:51:47 -050044 free(term_td);
45 free(fl);
46 free(intr_qh);
47 free(data_qh);
48 free(term_qh);
Kevin O'Connor114592f2009-09-28 21:32:08 -040049 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;
Kevin O'Connor59f02832009-10-12 10:09:15 -040065 cntl->uhci.qh = data_qh;
Kevin O'Connor114592f2009-09-28 21:32:08 -040066
Kevin O'Connor991eaff2010-02-13 21:51:47 -050067 // 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;
Kevin O'Connor114592f2009-09-28 21:32:08 -040071 int i;
Kevin O'Connor991eaff2010-02-13 21:51:47 -050072 for (i=0; i<ARRAY_SIZE(fl->links); i++)
73 fl->links[i] = (u32)intr_qh | UHCI_PTR_QH;
74 cntl->uhci.framelist = fl;
Kevin O'Connor114592f2009-09-28 21:32:08 -040075
76 // Set the frame length to the default: 1 ms exactly
Kevin O'Connor59f02832009-10-12 10:09:15 -040077 outb(USBSOF_DEFAULT, cntl->uhci.iobase + USBSOF);
Kevin O'Connor114592f2009-09-28 21:32:08 -040078
79 // Store the frame list base address
Kevin O'Connor59f02832009-10-12 10:09:15 -040080 outl((u32)fl->links, cntl->uhci.iobase + USBFLBASEADD);
Kevin O'Connor114592f2009-09-28 21:32:08 -040081
82 // Set the current frame number
Kevin O'Connor59f02832009-10-12 10:09:15 -040083 outw(0, cntl->uhci.iobase + USBFRNUM);
Kevin O'Connor114592f2009-09-28 21:32:08 -040084}
85
86static void
87start_uhci(struct usb_s *cntl)
88{
89 // Mark as configured and running with a 64-byte max packet.
Kevin O'Connor59f02832009-10-12 10:09:15 -040090 outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, cntl->uhci.iobase + USBCMD);
Kevin O'Connor114592f2009-09-28 21:32:08 -040091}
92
93// Find any devices connected to the root hub.
94static int
95check_ports(struct usb_s *cntl)
96{
Kevin O'Connor59f02832009-10-12 10:09:15 -040097 u16 port1 = inw(cntl->uhci.iobase + USBPORTSC1);
98 u16 port2 = inw(cntl->uhci.iobase + USBPORTSC2);
Kevin O'Connor114592f2009-09-28 21:32:08 -040099
100 if (!((port1 & USBPORTSC_CCS) || (port2 & USBPORTSC_CCS)))
101 // No devices
102 return 0;
103
104 // reset ports
105 if (port1 & USBPORTSC_CCS)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400106 outw(USBPORTSC_PR, cntl->uhci.iobase + USBPORTSC1);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400107 if (port2 & USBPORTSC_CCS)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400108 outw(USBPORTSC_PR, cntl->uhci.iobase + USBPORTSC2);
Kevin O'Connor8bbc79c2010-02-14 12:16:32 -0500109 msleep(USB_TIME_DRSTR);
Kevin O'Connor59f02832009-10-12 10:09:15 -0400110 outw(0, cntl->uhci.iobase + USBPORTSC1);
111 outw(0, cntl->uhci.iobase + USBPORTSC2);
Kevin O'Connor8bbc79c2010-02-14 12:16:32 -0500112 msleep(USB_TIME_RSTRCY);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400113
114 // Configure ports
115 int totalcount = 0;
Kevin O'Connor59f02832009-10-12 10:09:15 -0400116 port1 = inw(cntl->uhci.iobase + USBPORTSC1);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400117 if (port1 & USBPORTSC_CCS) {
Kevin O'Connor59f02832009-10-12 10:09:15 -0400118 outw(USBPORTSC_PE, cntl->uhci.iobase + USBPORTSC1);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400119 int count = configure_usb_device(cntl, !!(port1 & USBPORTSC_LSDA));
120 if (! count)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400121 outw(0, cntl->uhci.iobase + USBPORTSC1);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400122 totalcount += count;
123 }
Kevin O'Connor59f02832009-10-12 10:09:15 -0400124 port2 = inw(cntl->uhci.iobase + USBPORTSC2);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400125 if (port2 & USBPORTSC_CCS) {
Kevin O'Connor59f02832009-10-12 10:09:15 -0400126 outw(USBPORTSC_PE, cntl->uhci.iobase + USBPORTSC2);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400127 int count = configure_usb_device(cntl, !!(port2 & USBPORTSC_LSDA));
128 if (! count)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400129 outw(0, cntl->uhci.iobase + USBPORTSC2);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400130 totalcount += count;
131 }
132 return totalcount;
133}
134
Kevin O'Connora5826b52009-10-24 17:57:29 -0400135void
136uhci_init(void *data)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400137{
138 if (! CONFIG_USB_UHCI)
Kevin O'Connora5826b52009-10-24 17:57:29 -0400139 return;
140 struct usb_s *cntl = data;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400141
Kevin O'Connorad901592009-12-13 11:25:25 -0500142 // XXX - don't call pci_config_XXX from a thread
Kevin O'Connor59f02832009-10-12 10:09:15 -0400143 cntl->type = USB_TYPE_UHCI;
144 cntl->uhci.iobase = (pci_config_readl(cntl->bdf, PCI_BASE_ADDRESS_4)
145 & PCI_BASE_ADDRESS_IO_MASK);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400146
147 dprintf(3, "UHCI init on dev %02x:%02x.%x (io=%x)\n"
148 , pci_bdf_to_bus(cntl->bdf), pci_bdf_to_dev(cntl->bdf)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400149 , pci_bdf_to_fn(cntl->bdf), cntl->uhci.iobase);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400150
Kevin O'Connor59f02832009-10-12 10:09:15 -0400151 pci_config_maskw(cntl->bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400152
153 reset_uhci(cntl);
154 configure_uhci(cntl);
155 start_uhci(cntl);
156
157 int count = check_ports(cntl);
158 if (! count) {
159 // XXX - no devices; free data structures.
Kevin O'Connor114592f2009-09-28 21:32:08 -0400160 }
Kevin O'Connor114592f2009-09-28 21:32:08 -0400161}
162
163static int
Kevin O'Connorfcc116f2009-10-30 23:16:07 -0400164wait_qh(struct usb_s *cntl, struct uhci_qh *qh)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400165{
166 // XXX - 500ms just a guess
167 u64 end = calc_future_tsc(500);
168 for (;;) {
169 if (qh->element & UHCI_PTR_TERM)
170 return 0;
Kevin O'Connor89eb6242009-10-22 22:30:37 -0400171 if (check_time(end)) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500172 warn_timeout();
Kevin O'Connorfcc116f2009-10-30 23:16:07 -0400173 struct uhci_td *td = (void*)(qh->element & ~UHCI_PTR_BITS);
174 dprintf(1, "Timeout on wait_qh %p (td=%p s=%x c=%x/%x)\n"
175 , qh, td, td->status
176 , inw(cntl->uhci.iobase + USBCMD)
177 , inw(cntl->uhci.iobase + USBSTS));
Kevin O'Connor114592f2009-09-28 21:32:08 -0400178 return -1;
179 }
Kevin O'Connor229e3be2009-10-31 13:55:59 -0400180 yield();
Kevin O'Connor114592f2009-09-28 21:32:08 -0400181 }
182}
183
184int
185uhci_control(u32 endp, int dir, const void *cmd, int cmdsize
186 , void *data, int datasize)
187{
188 if (! CONFIG_USB_UHCI)
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400189 return -1;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400190
191 dprintf(5, "uhci_control %x\n", endp);
192 struct usb_s *cntl = endp2cntl(endp);
193 int maxpacket = endp2maxsize(endp);
194 int lowspeed = endp2speed(endp);
195 int devaddr = endp2devaddr(endp) | (endp2ep(endp) << 7);
196
197 // Setup transfer descriptors
198 int count = 2 + DIV_ROUND_UP(datasize, maxpacket);
199 struct uhci_td *tds = malloc_tmphigh(sizeof(*tds) * count);
200
201 tds[0].link = (u32)&tds[1] | UHCI_PTR_DEPTH;
202 tds[0].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
203 | TD_CTRL_ACTIVE);
204 tds[0].token = (uhci_explen(cmdsize) | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
205 | USB_PID_SETUP);
206 tds[0].buffer = (void*)cmd;
207 int toggle = TD_TOKEN_TOGGLE;
208 int i;
209 for (i=1; i<count-1; i++) {
210 tds[i].link = (u32)&tds[i+1] | UHCI_PTR_DEPTH;
211 tds[i].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
212 | TD_CTRL_ACTIVE);
213 int len = (i == count-2 ? (datasize - (i-1)*maxpacket) : maxpacket);
214 tds[i].token = (uhci_explen(len) | toggle
215 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
216 | (dir ? USB_PID_IN : USB_PID_OUT));
217 tds[i].buffer = data + (i-1) * maxpacket;
218 toggle ^= TD_TOKEN_TOGGLE;
219 }
220 tds[i].link = UHCI_PTR_TERM;
221 tds[i].status = (uhci_maxerr(0) | (lowspeed ? TD_CTRL_LS : 0)
222 | TD_CTRL_ACTIVE);
223 tds[i].token = (uhci_explen(0) | TD_TOKEN_TOGGLE
224 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
225 | (dir ? USB_PID_OUT : USB_PID_IN));
226 tds[i].buffer = 0;
227
228 // Transfer data
Kevin O'Connor59f02832009-10-12 10:09:15 -0400229 struct uhci_qh *data_qh = cntl->uhci.qh;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400230 data_qh->element = (u32)&tds[0];
Kevin O'Connorfcc116f2009-10-30 23:16:07 -0400231 int ret = wait_qh(cntl, data_qh);
Kevin O'Connorad901592009-12-13 11:25:25 -0500232 if (ret) {
233 data_qh->element = UHCI_PTR_TERM;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400234 // XXX - leak tds
235 return ret;
Kevin O'Connorad901592009-12-13 11:25:25 -0500236 }
237 free(tds);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400238 return 0;
239}
240
Kevin O'Connor59f02832009-10-12 10:09:15 -0400241struct usb_pipe *
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500242uhci_alloc_intr_pipe(u32 endp, int frameexp)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400243{
244 if (! CONFIG_USB_UHCI)
245 return NULL;
246
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500247 dprintf(7, "uhci_alloc_intr_pipe %x %d\n", endp, frameexp);
248 if (frameexp > 10)
249 frameexp = 10;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400250 struct usb_s *cntl = endp2cntl(endp);
251 int maxpacket = endp2maxsize(endp);
252 int lowspeed = endp2speed(endp);
253 int devaddr = endp2devaddr(endp) | (endp2ep(endp) << 7);
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500254 // Determine number of entries needed for 2 timer ticks.
255 int ms = 1<<frameexp;
256 int count = DIV_ROUND_UP(PIT_TICK_INTERVAL * 1000 * 2, PIT_TICK_RATE * ms);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400257 struct uhci_qh *qh = malloc_low(sizeof(*qh));
258 struct uhci_td *tds = malloc_low(sizeof(*tds) * count);
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500259 if (!qh || !tds || maxpacket > sizeof(tds[0].data)) {
260 free(qh);
261 free(tds);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400262 return NULL;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500263 }
Kevin O'Connor114592f2009-09-28 21:32:08 -0400264 qh->element = (u32)tds;
265 int toggle = 0;
266 int i;
267 for (i=0; i<count; i++) {
268 tds[i].link = (i==count-1 ? (u32)&tds[0] : (u32)&tds[i+1]);
269 tds[i].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
270 | TD_CTRL_ACTIVE);
271 tds[i].token = (uhci_explen(maxpacket) | toggle
272 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
273 | USB_PID_IN);
274 tds[i].buffer = &tds[i].data;
275 toggle ^= TD_TOKEN_TOGGLE;
276 }
277
278 qh->next_td = &tds[0];
Kevin O'Connor59f02832009-10-12 10:09:15 -0400279 qh->pipe.endp = endp;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400280
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500281 // Add to interrupt schedule.
282 struct uhci_framelist *fl = cntl->uhci.framelist;
283 if (frameexp == 0) {
284 // Add to existing interrupt entry.
285 struct uhci_qh *intr_qh = (void*)(fl->links[0] & ~UHCI_PTR_BITS);
286 qh->link = intr_qh->link;
287 intr_qh->link = (u32)qh | UHCI_PTR_QH;
288 } else {
289 int startpos = 1<<(frameexp-1);
290 qh->link = fl->links[startpos];
291 for (i=startpos; i<ARRAY_SIZE(fl->links); i+=ms)
292 fl->links[i] = (u32)qh | UHCI_PTR_QH;
293 }
Kevin O'Connor114592f2009-09-28 21:32:08 -0400294
Kevin O'Connor59f02832009-10-12 10:09:15 -0400295 return &qh->pipe;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400296}
297
298int
Kevin O'Connor59f02832009-10-12 10:09:15 -0400299uhci_poll_intr(struct usb_pipe *pipe, void *data)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400300{
301 ASSERT16();
302 if (! CONFIG_USB_UHCI)
303 return -1;
304
Kevin O'Connor59f02832009-10-12 10:09:15 -0400305 struct uhci_qh *qh = container_of(pipe, struct uhci_qh, pipe);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400306 struct uhci_td *td = GET_FLATPTR(qh->next_td);
307 u32 status = GET_FLATPTR(td->status);
308 u32 token = GET_FLATPTR(td->token);
309 if (status & TD_CTRL_ACTIVE)
310 // No intrs found.
311 return -1;
312 // XXX - check for errors.
313
314 // Copy data.
315 memcpy_far(GET_SEG(SS), data
316 , FLATPTR_TO_SEG(td->data), (void*)FLATPTR_TO_OFFSET(td->data)
317 , uhci_expected_length(token));
318
319 // Reenable this td.
320 u32 next = GET_FLATPTR(td->link);
321 SET_FLATPTR(td->status, (uhci_maxerr(0) | (status & TD_CTRL_LS)
322 | TD_CTRL_ACTIVE));
323 SET_FLATPTR(qh->next_td, (void*)(next & ~UHCI_PTR_BITS));
324
325 return 0;
326}