blob: f666ab7c3d32e4c2bf455bb23fc80f3cc22131a1 [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'Connor8ebcac02010-03-09 19:58:23 -050015#include "usb-hub.h" // struct usbhub_s
16
Kevin O'Connor406fad62010-02-28 02:23:19 -050017struct usb_uhci_s {
18 struct usb_s usb;
19 u16 iobase;
20 struct uhci_qh *control_qh, *bulk_qh;
21 struct uhci_framelist *framelist;
22};
23
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050024
25/****************************************************************
26 * Root hub
27 ****************************************************************/
28
29static void
30init_uhci_port(void *data)
31{
32 struct usbhub_s *hub = data;
33 u32 port = hub->port; // XXX - find better way to pass port
Kevin O'Connor406fad62010-02-28 02:23:19 -050034 struct usb_uhci_s *cntl = container_of(hub->cntl, struct usb_uhci_s, usb);
35 u16 ioport = cntl->iobase + USBPORTSC1 + port * 2;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050036
37 u16 status = inw(ioport);
38 if (!(status & USBPORTSC_CCS))
39 // No device
40 goto done;
41
42 // XXX - if just powered up, need to wait for USB_TIME_ATTDB?
43
44 // Reset port
45 outw(USBPORTSC_PR, ioport);
46 msleep(USB_TIME_DRSTR);
Kevin O'Connor406fad62010-02-28 02:23:19 -050047 mutex_lock(&cntl->usb.resetlock);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050048 outw(0, ioport);
49 udelay(6); // 64 high-speed bit times
50 status = inw(ioport);
51 if (!(status & USBPORTSC_CCS))
52 // No longer connected
53 goto resetfail;
54 outw(USBPORTSC_PE, ioport);
55 struct usb_pipe *pipe = usb_set_address(
Kevin O'Connor190cc622010-03-09 19:43:52 -050056 hub, port, !!(status & USBPORTSC_LSDA));
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050057 if (!pipe)
58 goto resetfail;
Kevin O'Connor406fad62010-02-28 02:23:19 -050059 mutex_unlock(&cntl->usb.resetlock);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050060
61 // Configure port
62 int count = configure_usb_device(pipe);
63 free_pipe(pipe);
64 if (! count)
65 // Disable port
66 outw(0, ioport);
67 hub->devcount += count;
68done:
69 hub->threads--;
70 return;
71
72resetfail:
73 outw(0, ioport);
Kevin O'Connor406fad62010-02-28 02:23:19 -050074 mutex_unlock(&cntl->usb.resetlock);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050075 goto done;
76}
77
78// Find any devices connected to the root hub.
79static int
Kevin O'Connor406fad62010-02-28 02:23:19 -050080check_uhci_ports(struct usb_uhci_s *cntl)
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050081{
82 ASSERT32FLAT();
83 struct usbhub_s hub;
84 memset(&hub, 0, sizeof(hub));
Kevin O'Connor406fad62010-02-28 02:23:19 -050085 hub.cntl = &cntl->usb;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050086 hub.threads = 2;
87
88 // Launch a thread for every port.
89 run_thread(init_uhci_port, &hub);
90 hub.port = 1;
91 run_thread(init_uhci_port, &hub);
92
93 // Wait for threads to complete.
94 while (hub.threads)
95 yield();
96
97 return hub.devcount;
98}
Kevin O'Connor357bdfa2010-02-26 08:57:13 -050099
100
101/****************************************************************
102 * Setup
103 ****************************************************************/
Kevin O'Connor114592f2009-09-28 21:32:08 -0400104
105static void
Kevin O'Connor406fad62010-02-28 02:23:19 -0500106reset_uhci(struct usb_uhci_s *cntl, u16 bdf)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400107{
108 // XXX - don't reset if not needed.
109
110 // Reset PIRQ and SMI
Kevin O'Connor406fad62010-02-28 02:23:19 -0500111 pci_config_writew(bdf, USBLEGSUP, USBLEGSUP_RWC);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400112
113 // Reset the HC
Kevin O'Connor406fad62010-02-28 02:23:19 -0500114 outw(USBCMD_HCRESET, cntl->iobase + USBCMD);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400115 udelay(5);
116
117 // Disable interrupts and commands (just to be safe).
Kevin O'Connor406fad62010-02-28 02:23:19 -0500118 outw(0, cntl->iobase + USBINTR);
119 outw(0, cntl->iobase + USBCMD);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400120}
121
122static void
Kevin O'Connor406fad62010-02-28 02:23:19 -0500123configure_uhci(void *data)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400124{
Kevin O'Connor406fad62010-02-28 02:23:19 -0500125 struct usb_uhci_s *cntl = data;
126
Kevin O'Connor114592f2009-09-28 21:32:08 -0400127 // Allocate ram for schedule storage
128 struct uhci_td *term_td = malloc_high(sizeof(*term_td));
129 struct uhci_framelist *fl = memalign_high(sizeof(*fl), sizeof(*fl));
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500130 struct uhci_qh *intr_qh = malloc_high(sizeof(*intr_qh));
Kevin O'Connor114592f2009-09-28 21:32:08 -0400131 struct uhci_qh *term_qh = malloc_high(sizeof(*term_qh));
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500132 if (!term_td || !fl || !intr_qh || !term_qh) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500133 warn_noalloc();
Kevin O'Connor406fad62010-02-28 02:23:19 -0500134 goto fail;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400135 }
136
137 // Work around for PIIX errata
138 memset(term_td, 0, sizeof(*term_td));
139 term_td->link = UHCI_PTR_TERM;
140 term_td->token = (uhci_explen(0) | (0x7f << TD_TOKEN_DEVADDR_SHIFT)
141 | USB_PID_IN);
142 memset(term_qh, 0, sizeof(*term_qh));
143 term_qh->element = (u32)term_td;
144 term_qh->link = UHCI_PTR_TERM;
145
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500146 // Set schedule to point to primary intr queue head
147 memset(intr_qh, 0, sizeof(*intr_qh));
148 intr_qh->element = UHCI_PTR_TERM;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500149 intr_qh->link = (u32)term_qh | UHCI_PTR_QH;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400150 int i;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500151 for (i=0; i<ARRAY_SIZE(fl->links); i++)
152 fl->links[i] = (u32)intr_qh | UHCI_PTR_QH;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500153 cntl->framelist = fl;
154 cntl->control_qh = cntl->bulk_qh = intr_qh;
Kevin O'Connor95714392010-02-17 22:59:53 -0500155 barrier();
Kevin O'Connor114592f2009-09-28 21:32:08 -0400156
157 // Set the frame length to the default: 1 ms exactly
Kevin O'Connor406fad62010-02-28 02:23:19 -0500158 outb(USBSOF_DEFAULT, cntl->iobase + USBSOF);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400159
160 // Store the frame list base address
Kevin O'Connor406fad62010-02-28 02:23:19 -0500161 outl((u32)fl->links, cntl->iobase + USBFLBASEADD);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400162
163 // Set the current frame number
Kevin O'Connor406fad62010-02-28 02:23:19 -0500164 outw(0, cntl->iobase + USBFRNUM);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400165
Kevin O'Connor114592f2009-09-28 21:32:08 -0400166 // Mark as configured and running with a 64-byte max packet.
Kevin O'Connor406fad62010-02-28 02:23:19 -0500167 outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, cntl->iobase + USBCMD);
168
169 // Find devices
170 int count = check_uhci_ports(cntl);
171 free_pipe(cntl->usb.defaultpipe);
172 if (count)
173 // Success
174 return;
175
176 // No devices found - shutdown and free controller.
177 outw(0, cntl->iobase + USBCMD);
178fail:
179 free(term_td);
180 free(fl);
181 free(intr_qh);
182 free(term_qh);
Kevin O'Connor0770d672010-03-09 19:33:22 -0500183 free(cntl);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400184}
185
Kevin O'Connora5826b52009-10-24 17:57:29 -0400186void
Kevin O'Connor406fad62010-02-28 02:23:19 -0500187uhci_init(u16 bdf, int busid)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400188{
189 if (! CONFIG_USB_UHCI)
Kevin O'Connora5826b52009-10-24 17:57:29 -0400190 return;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500191 struct usb_uhci_s *cntl = malloc_tmphigh(sizeof(*cntl));
192 memset(cntl, 0, sizeof(*cntl));
193 cntl->usb.busid = busid;
194 cntl->usb.type = USB_TYPE_UHCI;
195 cntl->iobase = (pci_config_readl(bdf, PCI_BASE_ADDRESS_4)
196 & PCI_BASE_ADDRESS_IO_MASK);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400197
198 dprintf(3, "UHCI init on dev %02x:%02x.%x (io=%x)\n"
Kevin O'Connor406fad62010-02-28 02:23:19 -0500199 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf)
200 , pci_bdf_to_fn(bdf), cntl->iobase);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400201
Kevin O'Connor406fad62010-02-28 02:23:19 -0500202 pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400203
Kevin O'Connor406fad62010-02-28 02:23:19 -0500204 reset_uhci(cntl, bdf);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400205
Kevin O'Connor406fad62010-02-28 02:23:19 -0500206 run_thread(configure_uhci, cntl);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400207}
208
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500209
210/****************************************************************
211 * End point communication
212 ****************************************************************/
213
Kevin O'Connor114592f2009-09-28 21:32:08 -0400214static int
Kevin O'Connor406fad62010-02-28 02:23:19 -0500215wait_qh(struct usb_uhci_s *cntl, struct uhci_qh *qh)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400216{
217 // XXX - 500ms just a guess
218 u64 end = calc_future_tsc(500);
219 for (;;) {
220 if (qh->element & UHCI_PTR_TERM)
221 return 0;
Kevin O'Connor89eb6242009-10-22 22:30:37 -0400222 if (check_time(end)) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500223 warn_timeout();
Kevin O'Connorfcc116f2009-10-30 23:16:07 -0400224 struct uhci_td *td = (void*)(qh->element & ~UHCI_PTR_BITS);
225 dprintf(1, "Timeout on wait_qh %p (td=%p s=%x c=%x/%x)\n"
226 , qh, td, td->status
Kevin O'Connor406fad62010-02-28 02:23:19 -0500227 , inw(cntl->iobase + USBCMD)
228 , inw(cntl->iobase + USBSTS));
Kevin O'Connor114592f2009-09-28 21:32:08 -0400229 return -1;
230 }
Kevin O'Connor229e3be2009-10-31 13:55:59 -0400231 yield();
Kevin O'Connor114592f2009-09-28 21:32:08 -0400232 }
233}
234
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500235// Wait for next USB frame to start - for ensuring safe memory release.
Kevin O'Connor95714392010-02-17 22:59:53 -0500236static void
Kevin O'Connor406fad62010-02-28 02:23:19 -0500237uhci_waittick(u16 iobase)
Kevin O'Connor95714392010-02-17 22:59:53 -0500238{
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500239 barrier();
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500240 u16 startframe = inw(iobase + USBFRNUM);
241 u64 end = calc_future_tsc(1000 * 5);
242 for (;;) {
243 if (inw(iobase + USBFRNUM) != startframe)
244 break;
245 if (check_time(end)) {
246 warn_timeout();
247 return;
248 }
249 yield();
250 }
251}
252
253struct uhci_pipe {
254 struct uhci_qh qh;
255 struct uhci_td *next_td;
256 struct usb_pipe pipe;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500257 u16 iobase;
Kevin O'Connor0770d672010-03-09 19:33:22 -0500258 u8 toggle;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500259};
260
261void
262uhci_free_pipe(struct usb_pipe *p)
263{
264 if (! CONFIG_USB_UHCI)
265 return;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500266 dprintf(7, "uhci_free_pipe %p\n", p);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500267 struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500268 struct usb_uhci_s *cntl = container_of(
269 pipe->pipe.cntl, struct usb_uhci_s, usb);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500270
Kevin O'Connor406fad62010-02-28 02:23:19 -0500271 struct uhci_qh *pos = (void*)(cntl->framelist->links[0] & ~UHCI_PTR_BITS);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500272 for (;;) {
273 u32 link = pos->link;
274 if (link == UHCI_PTR_TERM) {
275 // Not found?! Exit without freeing.
276 warn_internalerror();
277 return;
278 }
279 struct uhci_qh *next = (void*)(link & ~UHCI_PTR_BITS);
280 if (next == &pipe->qh) {
281 pos->link = next->link;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500282 if (cntl->control_qh == next)
283 cntl->control_qh = pos;
284 if (cntl->bulk_qh == next)
285 cntl->bulk_qh = pos;
286 uhci_waittick(cntl->iobase);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500287 free(pipe);
288 return;
289 }
290 pos = next;
291 }
292}
293
294struct usb_pipe *
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500295uhci_alloc_control_pipe(struct usb_pipe *dummy)
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500296{
297 if (! CONFIG_USB_UHCI)
298 return NULL;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500299 struct usb_uhci_s *cntl = container_of(
300 dummy->cntl, struct usb_uhci_s, usb);
301 dprintf(7, "uhci_alloc_control_pipe %p\n", &cntl->usb);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500302
303 // Allocate a queue head.
304 struct uhci_pipe *pipe = malloc_tmphigh(sizeof(*pipe));
305 if (!pipe) {
306 warn_noalloc();
307 return NULL;
308 }
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500309 memset(pipe, 0, sizeof(*pipe));
Kevin O'Connor0770d672010-03-09 19:33:22 -0500310 memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500311 pipe->qh.element = UHCI_PTR_TERM;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500312 pipe->iobase = cntl->iobase;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500313
314 // Add queue head to controller list.
Kevin O'Connor406fad62010-02-28 02:23:19 -0500315 struct uhci_qh *control_qh = cntl->control_qh;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500316 pipe->qh.link = control_qh->link;
317 barrier();
318 control_qh->link = (u32)&pipe->qh | UHCI_PTR_QH;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500319 if (cntl->bulk_qh == control_qh)
320 cntl->bulk_qh = &pipe->qh;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500321 return &pipe->pipe;
Kevin O'Connor95714392010-02-17 22:59:53 -0500322}
323
Kevin O'Connor114592f2009-09-28 21:32:08 -0400324int
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500325uhci_control(struct usb_pipe *p, int dir, const void *cmd, int cmdsize
Kevin O'Connor114592f2009-09-28 21:32:08 -0400326 , void *data, int datasize)
327{
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500328 ASSERT32FLAT();
Kevin O'Connor114592f2009-09-28 21:32:08 -0400329 if (! CONFIG_USB_UHCI)
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400330 return -1;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500331 dprintf(5, "uhci_control %p\n", p);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500332 struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500333 struct usb_uhci_s *cntl = container_of(
334 pipe->pipe.cntl, struct usb_uhci_s, usb);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400335
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500336 int maxpacket = pipe->pipe.maxpacket;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500337 int lowspeed = pipe->pipe.speed;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500338 int devaddr = pipe->pipe.devaddr | (pipe->pipe.ep << 7);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400339
340 // Setup transfer descriptors
341 int count = 2 + DIV_ROUND_UP(datasize, maxpacket);
342 struct uhci_td *tds = malloc_tmphigh(sizeof(*tds) * count);
Kevin O'Connor0770d672010-03-09 19:33:22 -0500343 if (!tds) {
344 warn_noalloc();
345 return -1;
346 }
Kevin O'Connor114592f2009-09-28 21:32:08 -0400347
348 tds[0].link = (u32)&tds[1] | UHCI_PTR_DEPTH;
349 tds[0].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
350 | TD_CTRL_ACTIVE);
351 tds[0].token = (uhci_explen(cmdsize) | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
352 | USB_PID_SETUP);
353 tds[0].buffer = (void*)cmd;
354 int toggle = TD_TOKEN_TOGGLE;
355 int i;
356 for (i=1; i<count-1; i++) {
357 tds[i].link = (u32)&tds[i+1] | UHCI_PTR_DEPTH;
358 tds[i].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
359 | TD_CTRL_ACTIVE);
360 int len = (i == count-2 ? (datasize - (i-1)*maxpacket) : maxpacket);
361 tds[i].token = (uhci_explen(len) | toggle
362 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
363 | (dir ? USB_PID_IN : USB_PID_OUT));
364 tds[i].buffer = data + (i-1) * maxpacket;
365 toggle ^= TD_TOKEN_TOGGLE;
366 }
367 tds[i].link = UHCI_PTR_TERM;
368 tds[i].status = (uhci_maxerr(0) | (lowspeed ? TD_CTRL_LS : 0)
369 | TD_CTRL_ACTIVE);
370 tds[i].token = (uhci_explen(0) | TD_TOKEN_TOGGLE
371 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
372 | (dir ? USB_PID_OUT : USB_PID_IN));
373 tds[i].buffer = 0;
374
375 // Transfer data
Kevin O'Connor95714392010-02-17 22:59:53 -0500376 barrier();
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500377 pipe->qh.element = (u32)&tds[0];
378 int ret = wait_qh(cntl, &pipe->qh);
Kevin O'Connorad901592009-12-13 11:25:25 -0500379 if (ret) {
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500380 pipe->qh.element = UHCI_PTR_TERM;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500381 uhci_waittick(pipe->iobase);
Kevin O'Connorad901592009-12-13 11:25:25 -0500382 }
383 free(tds);
Kevin O'Connor95714392010-02-17 22:59:53 -0500384 return ret;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400385}
386
Kevin O'Connor59f02832009-10-12 10:09:15 -0400387struct usb_pipe *
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500388uhci_alloc_bulk_pipe(struct usb_pipe *dummy)
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500389{
390 if (! CONFIG_USB_UHCI)
391 return NULL;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500392 struct usb_uhci_s *cntl = container_of(
393 dummy->cntl, struct usb_uhci_s, usb);
394 dprintf(7, "uhci_alloc_bulk_pipe %p\n", &cntl->usb);
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500395
396 // Allocate a queue head.
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500397 struct uhci_pipe *pipe = malloc_low(sizeof(*pipe));
398 if (!pipe) {
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500399 warn_noalloc();
400 return NULL;
401 }
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500402 memset(pipe, 0, sizeof(*pipe));
Kevin O'Connor0770d672010-03-09 19:33:22 -0500403 memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500404 pipe->qh.element = UHCI_PTR_TERM;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500405 pipe->iobase = cntl->iobase;
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500406
407 // Add queue head to controller list.
Kevin O'Connor406fad62010-02-28 02:23:19 -0500408 struct uhci_qh *bulk_qh = cntl->bulk_qh;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500409 pipe->qh.link = bulk_qh->link;
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500410 barrier();
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500411 bulk_qh->link = (u32)&pipe->qh | UHCI_PTR_QH;
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500412
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500413 return &pipe->pipe;
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500414}
415
416static int
417wait_td(struct uhci_td *td)
418{
419 u64 end = calc_future_tsc(5000); // XXX - lookup real time.
420 u32 status;
421 for (;;) {
422 status = td->status;
423 if (!(status & TD_CTRL_ACTIVE))
424 break;
425 if (check_time(end)) {
426 warn_timeout();
427 return -1;
428 }
429 yield();
430 }
431 if (status & TD_CTRL_ANY_ERROR) {
432 dprintf(1, "wait_td error - status=%x\n", status);
433 return -2;
434 }
435 return 0;
436}
437
438#define STACKTDS 4
439#define TDALIGN 16
440
441int
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500442uhci_send_bulk(struct usb_pipe *p, int dir, void *data, int datasize)
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500443{
Kevin O'Connor0770d672010-03-09 19:33:22 -0500444 if (! CONFIG_USB_UHCI)
445 return -1;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500446 struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500447 dprintf(7, "uhci_send_bulk qh=%p dir=%d data=%p size=%d\n"
448 , &pipe->qh, dir, data, datasize);
449 int maxpacket = GET_FLATPTR(pipe->pipe.maxpacket);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500450 int lowspeed = GET_FLATPTR(pipe->pipe.speed);
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500451 int devaddr = (GET_FLATPTR(pipe->pipe.devaddr)
452 | (GET_FLATPTR(pipe->pipe.ep) << 7));
Kevin O'Connor0770d672010-03-09 19:33:22 -0500453 int toggle = GET_FLATPTR(pipe->toggle) ? TD_TOKEN_TOGGLE : 0;
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500454
455 // Allocate 4 tds on stack (16byte aligned)
456 u8 tdsbuf[sizeof(struct uhci_td) * STACKTDS + TDALIGN - 1];
457 struct uhci_td *tds = (void*)ALIGN((u32)tdsbuf, TDALIGN);
458 memset(tds, 0, sizeof(*tds) * STACKTDS);
459
460 // Enable tds
Kevin O'Connor0770d672010-03-09 19:33:22 -0500461 barrier();
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500462 SET_FLATPTR(pipe->qh.element, (u32)MAKE_FLATPTR(GET_SEG(SS), tds));
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500463
464 int tdpos = 0;
465 while (datasize) {
466 struct uhci_td *td = &tds[tdpos++ % STACKTDS];
467 int ret = wait_td(td);
468 if (ret)
469 goto fail;
470
471 int transfer = datasize;
472 if (transfer > maxpacket)
473 transfer = maxpacket;
474 struct uhci_td *nexttd_fl = MAKE_FLATPTR(GET_SEG(SS)
475 , &tds[tdpos % STACKTDS]);
476 td->link = (transfer==datasize ? UHCI_PTR_TERM : (u32)nexttd_fl);
477 td->token = (uhci_explen(transfer) | toggle
478 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
479 | (dir ? USB_PID_IN : USB_PID_OUT));
480 td->buffer = data;
481 barrier();
482 td->status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
483 | TD_CTRL_ACTIVE);
484 toggle ^= TD_TOKEN_TOGGLE;
485
486 data += transfer;
487 datasize -= transfer;
488 }
489 int i;
490 for (i=0; i<STACKTDS; i++) {
491 struct uhci_td *td = &tds[tdpos++ % STACKTDS];
492 int ret = wait_td(td);
493 if (ret)
494 goto fail;
495 }
496
Kevin O'Connor0770d672010-03-09 19:33:22 -0500497 SET_FLATPTR(pipe->toggle, !!toggle);
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500498 return 0;
499fail:
500 dprintf(1, "uhci_send_bulk failed\n");
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500501 SET_FLATPTR(pipe->qh.element, UHCI_PTR_TERM);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500502 uhci_waittick(GET_FLATPTR(pipe->iobase));
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500503 return -1;
504}
505
506struct usb_pipe *
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500507uhci_alloc_intr_pipe(struct usb_pipe *dummy, int frameexp)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400508{
509 if (! CONFIG_USB_UHCI)
510 return NULL;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500511 struct usb_uhci_s *cntl = container_of(
512 dummy->cntl, struct usb_uhci_s, usb);
513 dprintf(7, "uhci_alloc_intr_pipe %p %d\n", &cntl->usb, frameexp);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400514
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500515 if (frameexp > 10)
516 frameexp = 10;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500517 int maxpacket = dummy->maxpacket;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500518 int lowspeed = dummy->speed;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500519 int devaddr = dummy->devaddr | (dummy->ep << 7);
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500520 // Determine number of entries needed for 2 timer ticks.
521 int ms = 1<<frameexp;
522 int count = DIV_ROUND_UP(PIT_TICK_INTERVAL * 1000 * 2, PIT_TICK_RATE * ms);
Kevin O'Connor0770d672010-03-09 19:33:22 -0500523 count = ALIGN(count, 2);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500524 struct uhci_pipe *pipe = malloc_low(sizeof(*pipe));
Kevin O'Connor114592f2009-09-28 21:32:08 -0400525 struct uhci_td *tds = malloc_low(sizeof(*tds) * count);
Kevin O'Connor09e2f7c2010-02-27 23:50:48 -0500526 void *data = malloc_low(maxpacket * count);
527 if (!pipe || !tds || !data) {
Kevin O'Connor95714392010-02-17 22:59:53 -0500528 warn_noalloc();
529 goto fail;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500530 }
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500531 memset(pipe, 0, sizeof(*pipe));
Kevin O'Connor0770d672010-03-09 19:33:22 -0500532 memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500533 pipe->qh.element = (u32)tds;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500534 pipe->next_td = &tds[0];
Kevin O'Connor406fad62010-02-28 02:23:19 -0500535 pipe->iobase = cntl->iobase;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500536
Kevin O'Connor114592f2009-09-28 21:32:08 -0400537 int toggle = 0;
538 int i;
539 for (i=0; i<count; i++) {
540 tds[i].link = (i==count-1 ? (u32)&tds[0] : (u32)&tds[i+1]);
541 tds[i].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
542 | TD_CTRL_ACTIVE);
543 tds[i].token = (uhci_explen(maxpacket) | toggle
544 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
545 | USB_PID_IN);
Kevin O'Connor09e2f7c2010-02-27 23:50:48 -0500546 tds[i].buffer = data + maxpacket * i;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400547 toggle ^= TD_TOKEN_TOGGLE;
548 }
549
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500550 // Add to interrupt schedule.
Kevin O'Connor406fad62010-02-28 02:23:19 -0500551 struct uhci_framelist *fl = cntl->framelist;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500552 if (frameexp == 0) {
553 // Add to existing interrupt entry.
554 struct uhci_qh *intr_qh = (void*)(fl->links[0] & ~UHCI_PTR_BITS);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500555 pipe->qh.link = intr_qh->link;
Kevin O'Connor95714392010-02-17 22:59:53 -0500556 barrier();
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500557 intr_qh->link = (u32)&pipe->qh | UHCI_PTR_QH;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500558 if (cntl->control_qh == intr_qh)
559 cntl->control_qh = &pipe->qh;
560 if (cntl->bulk_qh == intr_qh)
561 cntl->bulk_qh = &pipe->qh;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500562 } else {
563 int startpos = 1<<(frameexp-1);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500564 pipe->qh.link = fl->links[startpos];
Kevin O'Connor95714392010-02-17 22:59:53 -0500565 barrier();
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500566 for (i=startpos; i<ARRAY_SIZE(fl->links); i+=ms)
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500567 fl->links[i] = (u32)&pipe->qh | UHCI_PTR_QH;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500568 }
Kevin O'Connor114592f2009-09-28 21:32:08 -0400569
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500570 return &pipe->pipe;
Kevin O'Connor95714392010-02-17 22:59:53 -0500571fail:
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500572 free(pipe);
Kevin O'Connor95714392010-02-17 22:59:53 -0500573 free(tds);
Kevin O'Connor09e2f7c2010-02-27 23:50:48 -0500574 free(data);
Kevin O'Connor95714392010-02-17 22:59:53 -0500575 return NULL;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400576}
577
578int
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500579uhci_poll_intr(struct usb_pipe *p, void *data)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400580{
581 ASSERT16();
582 if (! CONFIG_USB_UHCI)
583 return -1;
584
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500585 struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
586 struct uhci_td *td = GET_FLATPTR(pipe->next_td);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400587 u32 status = GET_FLATPTR(td->status);
588 u32 token = GET_FLATPTR(td->token);
589 if (status & TD_CTRL_ACTIVE)
590 // No intrs found.
591 return -1;
592 // XXX - check for errors.
593
594 // Copy data.
Kevin O'Connor09e2f7c2010-02-27 23:50:48 -0500595 void *tddata = GET_FLATPTR(td->buffer);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400596 memcpy_far(GET_SEG(SS), data
Kevin O'Connor09e2f7c2010-02-27 23:50:48 -0500597 , FLATPTR_TO_SEG(tddata), (void*)FLATPTR_TO_OFFSET(tddata)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400598 , uhci_expected_length(token));
599
600 // Reenable this td.
Kevin O'Connor09e2f7c2010-02-27 23:50:48 -0500601 struct uhci_td *next = (void*)(GET_FLATPTR(td->link) & ~UHCI_PTR_BITS);
602 SET_FLATPTR(pipe->next_td, next);
Kevin O'Connor95714392010-02-17 22:59:53 -0500603 barrier();
Kevin O'Connor114592f2009-09-28 21:32:08 -0400604 SET_FLATPTR(td->status, (uhci_maxerr(0) | (status & TD_CTRL_LS)
605 | TD_CTRL_ACTIVE));
Kevin O'Connor114592f2009-09-28 21:32:08 -0400606
607 return 0;
608}