blob: a78dbcaf63e679ac55d1c8f3fc11fd93454ec6b0 [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
Kevin O'Connor406fad62010-02-28 02:23:19 -050016struct usb_uhci_s {
17 struct usb_s usb;
18 u16 iobase;
19 struct uhci_qh *control_qh, *bulk_qh;
20 struct uhci_framelist *framelist;
21};
22
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050023
24/****************************************************************
25 * Root hub
26 ****************************************************************/
27
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040028// Check if device attached to a given port
29static int
30uhci_hub_detect(struct usbhub_s *hub, u32 port)
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050031{
Kevin O'Connor406fad62010-02-28 02:23:19 -050032 struct usb_uhci_s *cntl = container_of(hub->cntl, struct usb_uhci_s, usb);
33 u16 ioport = cntl->iobase + USBPORTSC1 + port * 2;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050034
35 u16 status = inw(ioport);
36 if (!(status & USBPORTSC_CCS))
37 // No device
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040038 return -1;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050039
40 // XXX - if just powered up, need to wait for USB_TIME_ATTDB?
41
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040042 // Begin reset on port
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050043 outw(USBPORTSC_PR, ioport);
44 msleep(USB_TIME_DRSTR);
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040045 return 0;
46}
47
48// Reset device on port
49static int
50uhci_hub_reset(struct usbhub_s *hub, u32 port)
51{
52 struct usb_uhci_s *cntl = container_of(hub->cntl, struct usb_uhci_s, usb);
53 u16 ioport = cntl->iobase + USBPORTSC1 + port * 2;
54
55 // Finish reset on port
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050056 outw(0, ioport);
57 udelay(6); // 64 high-speed bit times
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040058 u16 status = inw(ioport);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050059 if (!(status & USBPORTSC_CCS))
60 // No longer connected
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040061 return -1;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050062 outw(USBPORTSC_PE, ioport);
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040063 return !!(status & USBPORTSC_LSDA);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050064}
65
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040066// Disable port
67static void
68uhci_hub_disconnect(struct usbhub_s *hub, u32 port)
69{
70 struct usb_uhci_s *cntl = container_of(hub->cntl, struct usb_uhci_s, usb);
71 u16 ioport = cntl->iobase + USBPORTSC1 + port * 2;
72 outw(0, ioport);
73}
74
75static struct usbhub_op_s uhci_HubOp = {
76 .detect = uhci_hub_detect,
77 .reset = uhci_hub_reset,
78 .disconnect = uhci_hub_disconnect,
79};
80
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050081// Find any devices connected to the root hub.
82static int
Kevin O'Connor406fad62010-02-28 02:23:19 -050083check_uhci_ports(struct usb_uhci_s *cntl)
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050084{
85 ASSERT32FLAT();
86 struct usbhub_s hub;
87 memset(&hub, 0, sizeof(hub));
Kevin O'Connor406fad62010-02-28 02:23:19 -050088 hub.cntl = &cntl->usb;
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040089 hub.portcount = 2;
90 hub.op = &uhci_HubOp;
91 usb_enumerate(&hub);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050092 return hub.devcount;
93}
Kevin O'Connor357bdfa2010-02-26 08:57:13 -050094
95
96/****************************************************************
97 * Setup
98 ****************************************************************/
Kevin O'Connor114592f2009-09-28 21:32:08 -040099
100static void
Kevin O'Connor406fad62010-02-28 02:23:19 -0500101reset_uhci(struct usb_uhci_s *cntl, u16 bdf)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400102{
103 // XXX - don't reset if not needed.
104
105 // Reset PIRQ and SMI
Kevin O'Connor406fad62010-02-28 02:23:19 -0500106 pci_config_writew(bdf, USBLEGSUP, USBLEGSUP_RWC);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400107
108 // Reset the HC
Kevin O'Connor406fad62010-02-28 02:23:19 -0500109 outw(USBCMD_HCRESET, cntl->iobase + USBCMD);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400110 udelay(5);
111
112 // Disable interrupts and commands (just to be safe).
Kevin O'Connor406fad62010-02-28 02:23:19 -0500113 outw(0, cntl->iobase + USBINTR);
114 outw(0, cntl->iobase + USBCMD);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400115}
116
117static void
Kevin O'Connor406fad62010-02-28 02:23:19 -0500118configure_uhci(void *data)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400119{
Kevin O'Connor406fad62010-02-28 02:23:19 -0500120 struct usb_uhci_s *cntl = data;
121
Kevin O'Connor114592f2009-09-28 21:32:08 -0400122 // Allocate ram for schedule storage
123 struct uhci_td *term_td = malloc_high(sizeof(*term_td));
124 struct uhci_framelist *fl = memalign_high(sizeof(*fl), sizeof(*fl));
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500125 struct uhci_qh *intr_qh = malloc_high(sizeof(*intr_qh));
Kevin O'Connor114592f2009-09-28 21:32:08 -0400126 struct uhci_qh *term_qh = malloc_high(sizeof(*term_qh));
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500127 if (!term_td || !fl || !intr_qh || !term_qh) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500128 warn_noalloc();
Kevin O'Connor406fad62010-02-28 02:23:19 -0500129 goto fail;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400130 }
131
132 // Work around for PIIX errata
133 memset(term_td, 0, sizeof(*term_td));
134 term_td->link = UHCI_PTR_TERM;
135 term_td->token = (uhci_explen(0) | (0x7f << TD_TOKEN_DEVADDR_SHIFT)
136 | USB_PID_IN);
137 memset(term_qh, 0, sizeof(*term_qh));
138 term_qh->element = (u32)term_td;
139 term_qh->link = UHCI_PTR_TERM;
140
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500141 // Set schedule to point to primary intr queue head
142 memset(intr_qh, 0, sizeof(*intr_qh));
143 intr_qh->element = UHCI_PTR_TERM;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500144 intr_qh->link = (u32)term_qh | UHCI_PTR_QH;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400145 int i;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500146 for (i=0; i<ARRAY_SIZE(fl->links); i++)
147 fl->links[i] = (u32)intr_qh | UHCI_PTR_QH;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500148 cntl->framelist = fl;
149 cntl->control_qh = cntl->bulk_qh = intr_qh;
Kevin O'Connor95714392010-02-17 22:59:53 -0500150 barrier();
Kevin O'Connor114592f2009-09-28 21:32:08 -0400151
152 // Set the frame length to the default: 1 ms exactly
Kevin O'Connor406fad62010-02-28 02:23:19 -0500153 outb(USBSOF_DEFAULT, cntl->iobase + USBSOF);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400154
155 // Store the frame list base address
Kevin O'Connor406fad62010-02-28 02:23:19 -0500156 outl((u32)fl->links, cntl->iobase + USBFLBASEADD);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400157
158 // Set the current frame number
Kevin O'Connor406fad62010-02-28 02:23:19 -0500159 outw(0, cntl->iobase + USBFRNUM);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400160
Kevin O'Connor114592f2009-09-28 21:32:08 -0400161 // Mark as configured and running with a 64-byte max packet.
Kevin O'Connor406fad62010-02-28 02:23:19 -0500162 outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, cntl->iobase + USBCMD);
163
164 // Find devices
165 int count = check_uhci_ports(cntl);
166 free_pipe(cntl->usb.defaultpipe);
167 if (count)
168 // Success
169 return;
170
171 // No devices found - shutdown and free controller.
172 outw(0, cntl->iobase + USBCMD);
173fail:
174 free(term_td);
175 free(fl);
176 free(intr_qh);
177 free(term_qh);
Kevin O'Connor0770d672010-03-09 19:33:22 -0500178 free(cntl);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400179}
180
Kevin O'Connora5826b52009-10-24 17:57:29 -0400181void
Kevin O'Connor8ff8e012011-07-09 14:11:21 -0400182uhci_init(struct pci_device *pci, int busid)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400183{
184 if (! CONFIG_USB_UHCI)
Kevin O'Connora5826b52009-10-24 17:57:29 -0400185 return;
Kevin O'Connor8ff8e012011-07-09 14:11:21 -0400186 u16 bdf = pci->bdf;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500187 struct usb_uhci_s *cntl = malloc_tmphigh(sizeof(*cntl));
Kevin O'Connorffdcd3a2011-07-10 15:48:00 -0400188 if (!cntl) {
189 warn_noalloc();
190 return;
191 }
Kevin O'Connor406fad62010-02-28 02:23:19 -0500192 memset(cntl, 0, sizeof(*cntl));
193 cntl->usb.busid = busid;
Kevin O'Connor8ff8e012011-07-09 14:11:21 -0400194 cntl->usb.pci = pci;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500195 cntl->usb.type = USB_TYPE_UHCI;
196 cntl->iobase = (pci_config_readl(bdf, PCI_BASE_ADDRESS_4)
197 & PCI_BASE_ADDRESS_IO_MASK);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400198
Kevin O'Connor9dc243e2010-03-20 17:53:03 -0400199 dprintf(1, "UHCI init on dev %02x:%02x.%x (io=%x)\n"
Kevin O'Connor406fad62010-02-28 02:23:19 -0500200 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf)
201 , pci_bdf_to_fn(bdf), cntl->iobase);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400202
Kevin O'Connor406fad62010-02-28 02:23:19 -0500203 pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400204
Kevin O'Connor406fad62010-02-28 02:23:19 -0500205 reset_uhci(cntl, bdf);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400206
Kevin O'Connor406fad62010-02-28 02:23:19 -0500207 run_thread(configure_uhci, cntl);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400208}
209
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500210
211/****************************************************************
212 * End point communication
213 ****************************************************************/
214
Paolo Bonzini31dfd3f2011-11-17 10:22:59 +0100215struct uhci_pipe {
216 struct uhci_qh qh;
217 struct uhci_td *next_td;
218 struct usb_pipe pipe;
219 u16 iobase;
220 u8 toggle;
221};
Kevin O'Connor114592f2009-09-28 21:32:08 -0400222
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500223// Wait for next USB frame to start - for ensuring safe memory release.
Kevin O'Connor95714392010-02-17 22:59:53 -0500224static void
Kevin O'Connor406fad62010-02-28 02:23:19 -0500225uhci_waittick(u16 iobase)
Kevin O'Connor95714392010-02-17 22:59:53 -0500226{
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500227 barrier();
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500228 u16 startframe = inw(iobase + USBFRNUM);
229 u64 end = calc_future_tsc(1000 * 5);
230 for (;;) {
231 if (inw(iobase + USBFRNUM) != startframe)
232 break;
Kevin O'Connor144817b2010-05-23 10:46:49 -0400233 if (check_tsc(end)) {
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500234 warn_timeout();
235 return;
236 }
237 yield();
238 }
239}
240
Paolo Bonzini31dfd3f2011-11-17 10:22:59 +0100241static int
242wait_pipe(struct uhci_pipe *pipe, int timeout)
243{
244 u64 end = calc_future_tsc(timeout);
245 for (;;) {
246 u32 el_link = GET_FLATPTR(pipe->qh.element);
247 if (el_link & UHCI_PTR_TERM)
248 return 0;
249 if (check_tsc(end)) {
250 warn_timeout();
Kevin O'Connor08589ac2011-11-17 22:05:53 -0500251 u16 iobase = GET_FLATPTR(pipe->iobase);
Paolo Bonzini31dfd3f2011-11-17 10:22:59 +0100252 struct uhci_td *td = (void*)(el_link & ~UHCI_PTR_BITS);
253 dprintf(1, "Timeout on wait_pipe %p (td=%p s=%x c=%x/%x)\n"
254 , pipe, (void*)el_link, GET_FLATPTR(td->status)
Kevin O'Connor08589ac2011-11-17 22:05:53 -0500255 , inw(iobase + USBCMD)
256 , inw(iobase + USBSTS));
Paolo Bonzini31dfd3f2011-11-17 10:22:59 +0100257 SET_FLATPTR(pipe->qh.element, UHCI_PTR_TERM);
Kevin O'Connor08589ac2011-11-17 22:05:53 -0500258 uhci_waittick(iobase);
Paolo Bonzini31dfd3f2011-11-17 10:22:59 +0100259 return -1;
260 }
261 yield();
262 }
263}
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500264
265void
266uhci_free_pipe(struct usb_pipe *p)
267{
268 if (! CONFIG_USB_UHCI)
269 return;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500270 dprintf(7, "uhci_free_pipe %p\n", p);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500271 struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500272 struct usb_uhci_s *cntl = container_of(
273 pipe->pipe.cntl, struct usb_uhci_s, usb);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500274
Kevin O'Connor406fad62010-02-28 02:23:19 -0500275 struct uhci_qh *pos = (void*)(cntl->framelist->links[0] & ~UHCI_PTR_BITS);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500276 for (;;) {
277 u32 link = pos->link;
278 if (link == UHCI_PTR_TERM) {
279 // Not found?! Exit without freeing.
280 warn_internalerror();
281 return;
282 }
283 struct uhci_qh *next = (void*)(link & ~UHCI_PTR_BITS);
284 if (next == &pipe->qh) {
285 pos->link = next->link;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500286 if (cntl->control_qh == next)
287 cntl->control_qh = pos;
288 if (cntl->bulk_qh == next)
289 cntl->bulk_qh = pos;
290 uhci_waittick(cntl->iobase);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500291 free(pipe);
292 return;
293 }
294 pos = next;
295 }
296}
297
298struct usb_pipe *
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500299uhci_alloc_control_pipe(struct usb_pipe *dummy)
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500300{
301 if (! CONFIG_USB_UHCI)
302 return NULL;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500303 struct usb_uhci_s *cntl = container_of(
304 dummy->cntl, struct usb_uhci_s, usb);
305 dprintf(7, "uhci_alloc_control_pipe %p\n", &cntl->usb);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500306
307 // Allocate a queue head.
308 struct uhci_pipe *pipe = malloc_tmphigh(sizeof(*pipe));
309 if (!pipe) {
310 warn_noalloc();
311 return NULL;
312 }
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500313 memset(pipe, 0, sizeof(*pipe));
Kevin O'Connor0770d672010-03-09 19:33:22 -0500314 memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500315 pipe->qh.element = UHCI_PTR_TERM;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500316 pipe->iobase = cntl->iobase;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500317
318 // Add queue head to controller list.
Kevin O'Connor406fad62010-02-28 02:23:19 -0500319 struct uhci_qh *control_qh = cntl->control_qh;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500320 pipe->qh.link = control_qh->link;
321 barrier();
322 control_qh->link = (u32)&pipe->qh | UHCI_PTR_QH;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500323 if (cntl->bulk_qh == control_qh)
324 cntl->bulk_qh = &pipe->qh;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500325 return &pipe->pipe;
Kevin O'Connor95714392010-02-17 22:59:53 -0500326}
327
Kevin O'Connor114592f2009-09-28 21:32:08 -0400328int
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500329uhci_control(struct usb_pipe *p, int dir, const void *cmd, int cmdsize
Kevin O'Connor114592f2009-09-28 21:32:08 -0400330 , void *data, int datasize)
331{
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500332 ASSERT32FLAT();
Kevin O'Connor114592f2009-09-28 21:32:08 -0400333 if (! CONFIG_USB_UHCI)
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400334 return -1;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500335 dprintf(5, "uhci_control %p\n", p);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500336 struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400337
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500338 int maxpacket = pipe->pipe.maxpacket;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500339 int lowspeed = pipe->pipe.speed;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500340 int devaddr = pipe->pipe.devaddr | (pipe->pipe.ep << 7);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400341
342 // Setup transfer descriptors
343 int count = 2 + DIV_ROUND_UP(datasize, maxpacket);
344 struct uhci_td *tds = malloc_tmphigh(sizeof(*tds) * count);
Kevin O'Connor0770d672010-03-09 19:33:22 -0500345 if (!tds) {
346 warn_noalloc();
347 return -1;
348 }
Kevin O'Connor114592f2009-09-28 21:32:08 -0400349
350 tds[0].link = (u32)&tds[1] | UHCI_PTR_DEPTH;
351 tds[0].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
352 | TD_CTRL_ACTIVE);
353 tds[0].token = (uhci_explen(cmdsize) | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
354 | USB_PID_SETUP);
355 tds[0].buffer = (void*)cmd;
356 int toggle = TD_TOKEN_TOGGLE;
357 int i;
358 for (i=1; i<count-1; i++) {
359 tds[i].link = (u32)&tds[i+1] | UHCI_PTR_DEPTH;
360 tds[i].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
361 | TD_CTRL_ACTIVE);
362 int len = (i == count-2 ? (datasize - (i-1)*maxpacket) : maxpacket);
363 tds[i].token = (uhci_explen(len) | toggle
364 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
365 | (dir ? USB_PID_IN : USB_PID_OUT));
366 tds[i].buffer = data + (i-1) * maxpacket;
367 toggle ^= TD_TOKEN_TOGGLE;
368 }
369 tds[i].link = UHCI_PTR_TERM;
370 tds[i].status = (uhci_maxerr(0) | (lowspeed ? TD_CTRL_LS : 0)
371 | TD_CTRL_ACTIVE);
372 tds[i].token = (uhci_explen(0) | TD_TOKEN_TOGGLE
373 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
374 | (dir ? USB_PID_OUT : USB_PID_IN));
375 tds[i].buffer = 0;
376
377 // Transfer data
Kevin O'Connor95714392010-02-17 22:59:53 -0500378 barrier();
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500379 pipe->qh.element = (u32)&tds[0];
Paolo Bonzini31dfd3f2011-11-17 10:22:59 +0100380 int ret = wait_pipe(pipe, 500);
Kevin O'Connorad901592009-12-13 11:25:25 -0500381 free(tds);
Kevin O'Connor95714392010-02-17 22:59:53 -0500382 return ret;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400383}
384
Kevin O'Connor59f02832009-10-12 10:09:15 -0400385struct usb_pipe *
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500386uhci_alloc_bulk_pipe(struct usb_pipe *dummy)
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500387{
388 if (! CONFIG_USB_UHCI)
389 return NULL;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500390 struct usb_uhci_s *cntl = container_of(
391 dummy->cntl, struct usb_uhci_s, usb);
392 dprintf(7, "uhci_alloc_bulk_pipe %p\n", &cntl->usb);
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500393
394 // Allocate a queue head.
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500395 struct uhci_pipe *pipe = malloc_low(sizeof(*pipe));
396 if (!pipe) {
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500397 warn_noalloc();
398 return NULL;
399 }
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500400 memset(pipe, 0, sizeof(*pipe));
Kevin O'Connor0770d672010-03-09 19:33:22 -0500401 memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500402 pipe->qh.element = UHCI_PTR_TERM;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500403 pipe->iobase = cntl->iobase;
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500404
405 // Add queue head to controller list.
Kevin O'Connor406fad62010-02-28 02:23:19 -0500406 struct uhci_qh *bulk_qh = cntl->bulk_qh;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500407 pipe->qh.link = bulk_qh->link;
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500408 barrier();
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500409 bulk_qh->link = (u32)&pipe->qh | UHCI_PTR_QH;
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500410
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500411 return &pipe->pipe;
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500412}
413
414static int
415wait_td(struct uhci_td *td)
416{
417 u64 end = calc_future_tsc(5000); // XXX - lookup real time.
418 u32 status;
419 for (;;) {
420 status = td->status;
421 if (!(status & TD_CTRL_ACTIVE))
422 break;
Kevin O'Connor144817b2010-05-23 10:46:49 -0400423 if (check_tsc(end)) {
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500424 warn_timeout();
425 return -1;
426 }
427 yield();
428 }
429 if (status & TD_CTRL_ANY_ERROR) {
430 dprintf(1, "wait_td error - status=%x\n", status);
431 return -2;
432 }
433 return 0;
434}
435
436#define STACKTDS 4
437#define TDALIGN 16
438
439int
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500440uhci_send_bulk(struct usb_pipe *p, int dir, void *data, int datasize)
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500441{
Kevin O'Connor0770d672010-03-09 19:33:22 -0500442 if (! CONFIG_USB_UHCI)
443 return -1;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500444 struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500445 dprintf(7, "uhci_send_bulk qh=%p dir=%d data=%p size=%d\n"
446 , &pipe->qh, dir, data, datasize);
447 int maxpacket = GET_FLATPTR(pipe->pipe.maxpacket);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500448 int lowspeed = GET_FLATPTR(pipe->pipe.speed);
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500449 int devaddr = (GET_FLATPTR(pipe->pipe.devaddr)
450 | (GET_FLATPTR(pipe->pipe.ep) << 7));
Kevin O'Connor0770d672010-03-09 19:33:22 -0500451 int toggle = GET_FLATPTR(pipe->toggle) ? TD_TOKEN_TOGGLE : 0;
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500452
453 // Allocate 4 tds on stack (16byte aligned)
454 u8 tdsbuf[sizeof(struct uhci_td) * STACKTDS + TDALIGN - 1];
455 struct uhci_td *tds = (void*)ALIGN((u32)tdsbuf, TDALIGN);
456 memset(tds, 0, sizeof(*tds) * STACKTDS);
457
458 // Enable tds
Kevin O'Connor0770d672010-03-09 19:33:22 -0500459 barrier();
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500460 SET_FLATPTR(pipe->qh.element, (u32)MAKE_FLATPTR(GET_SEG(SS), tds));
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500461
462 int tdpos = 0;
463 while (datasize) {
464 struct uhci_td *td = &tds[tdpos++ % STACKTDS];
465 int ret = wait_td(td);
466 if (ret)
467 goto fail;
468
469 int transfer = datasize;
470 if (transfer > maxpacket)
471 transfer = maxpacket;
472 struct uhci_td *nexttd_fl = MAKE_FLATPTR(GET_SEG(SS)
473 , &tds[tdpos % STACKTDS]);
474 td->link = (transfer==datasize ? UHCI_PTR_TERM : (u32)nexttd_fl);
475 td->token = (uhci_explen(transfer) | toggle
476 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
477 | (dir ? USB_PID_IN : USB_PID_OUT));
478 td->buffer = data;
479 barrier();
480 td->status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
481 | TD_CTRL_ACTIVE);
482 toggle ^= TD_TOKEN_TOGGLE;
483
484 data += transfer;
485 datasize -= transfer;
486 }
Kevin O'Connor0770d672010-03-09 19:33:22 -0500487 SET_FLATPTR(pipe->toggle, !!toggle);
Paolo Bonzini13fcbb92011-11-17 10:23:00 +0100488 return wait_pipe(pipe, 5000);
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500489fail:
490 dprintf(1, "uhci_send_bulk failed\n");
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500491 SET_FLATPTR(pipe->qh.element, UHCI_PTR_TERM);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500492 uhci_waittick(GET_FLATPTR(pipe->iobase));
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500493 return -1;
494}
495
496struct usb_pipe *
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500497uhci_alloc_intr_pipe(struct usb_pipe *dummy, int frameexp)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400498{
499 if (! CONFIG_USB_UHCI)
500 return NULL;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500501 struct usb_uhci_s *cntl = container_of(
502 dummy->cntl, struct usb_uhci_s, usb);
503 dprintf(7, "uhci_alloc_intr_pipe %p %d\n", &cntl->usb, frameexp);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400504
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500505 if (frameexp > 10)
506 frameexp = 10;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500507 int maxpacket = dummy->maxpacket;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500508 int lowspeed = dummy->speed;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500509 int devaddr = dummy->devaddr | (dummy->ep << 7);
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500510 // Determine number of entries needed for 2 timer ticks.
511 int ms = 1<<frameexp;
512 int count = DIV_ROUND_UP(PIT_TICK_INTERVAL * 1000 * 2, PIT_TICK_RATE * ms);
Kevin O'Connor0770d672010-03-09 19:33:22 -0500513 count = ALIGN(count, 2);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500514 struct uhci_pipe *pipe = malloc_low(sizeof(*pipe));
Kevin O'Connor114592f2009-09-28 21:32:08 -0400515 struct uhci_td *tds = malloc_low(sizeof(*tds) * count);
Kevin O'Connor09e2f7c2010-02-27 23:50:48 -0500516 void *data = malloc_low(maxpacket * count);
517 if (!pipe || !tds || !data) {
Kevin O'Connor95714392010-02-17 22:59:53 -0500518 warn_noalloc();
519 goto fail;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500520 }
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500521 memset(pipe, 0, sizeof(*pipe));
Kevin O'Connor0770d672010-03-09 19:33:22 -0500522 memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500523 pipe->qh.element = (u32)tds;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500524 pipe->next_td = &tds[0];
Kevin O'Connor406fad62010-02-28 02:23:19 -0500525 pipe->iobase = cntl->iobase;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500526
Kevin O'Connor114592f2009-09-28 21:32:08 -0400527 int toggle = 0;
528 int i;
529 for (i=0; i<count; i++) {
530 tds[i].link = (i==count-1 ? (u32)&tds[0] : (u32)&tds[i+1]);
531 tds[i].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
532 | TD_CTRL_ACTIVE);
533 tds[i].token = (uhci_explen(maxpacket) | toggle
534 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
535 | USB_PID_IN);
Kevin O'Connor09e2f7c2010-02-27 23:50:48 -0500536 tds[i].buffer = data + maxpacket * i;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400537 toggle ^= TD_TOKEN_TOGGLE;
538 }
539
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500540 // Add to interrupt schedule.
Kevin O'Connor406fad62010-02-28 02:23:19 -0500541 struct uhci_framelist *fl = cntl->framelist;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500542 if (frameexp == 0) {
543 // Add to existing interrupt entry.
544 struct uhci_qh *intr_qh = (void*)(fl->links[0] & ~UHCI_PTR_BITS);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500545 pipe->qh.link = intr_qh->link;
Kevin O'Connor95714392010-02-17 22:59:53 -0500546 barrier();
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500547 intr_qh->link = (u32)&pipe->qh | UHCI_PTR_QH;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500548 if (cntl->control_qh == intr_qh)
549 cntl->control_qh = &pipe->qh;
550 if (cntl->bulk_qh == intr_qh)
551 cntl->bulk_qh = &pipe->qh;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500552 } else {
553 int startpos = 1<<(frameexp-1);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500554 pipe->qh.link = fl->links[startpos];
Kevin O'Connor95714392010-02-17 22:59:53 -0500555 barrier();
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500556 for (i=startpos; i<ARRAY_SIZE(fl->links); i+=ms)
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500557 fl->links[i] = (u32)&pipe->qh | UHCI_PTR_QH;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500558 }
Kevin O'Connor114592f2009-09-28 21:32:08 -0400559
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500560 return &pipe->pipe;
Kevin O'Connor95714392010-02-17 22:59:53 -0500561fail:
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500562 free(pipe);
Kevin O'Connor95714392010-02-17 22:59:53 -0500563 free(tds);
Kevin O'Connor09e2f7c2010-02-27 23:50:48 -0500564 free(data);
Kevin O'Connor95714392010-02-17 22:59:53 -0500565 return NULL;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400566}
567
568int
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500569uhci_poll_intr(struct usb_pipe *p, void *data)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400570{
571 ASSERT16();
572 if (! CONFIG_USB_UHCI)
573 return -1;
574
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500575 struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
576 struct uhci_td *td = GET_FLATPTR(pipe->next_td);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400577 u32 status = GET_FLATPTR(td->status);
578 u32 token = GET_FLATPTR(td->token);
579 if (status & TD_CTRL_ACTIVE)
580 // No intrs found.
581 return -1;
582 // XXX - check for errors.
583
584 // Copy data.
Kevin O'Connor09e2f7c2010-02-27 23:50:48 -0500585 void *tddata = GET_FLATPTR(td->buffer);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400586 memcpy_far(GET_SEG(SS), data
Kevin O'Connor09e2f7c2010-02-27 23:50:48 -0500587 , FLATPTR_TO_SEG(tddata), (void*)FLATPTR_TO_OFFSET(tddata)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400588 , uhci_expected_length(token));
589
590 // Reenable this td.
Kevin O'Connor09e2f7c2010-02-27 23:50:48 -0500591 struct uhci_td *next = (void*)(GET_FLATPTR(td->link) & ~UHCI_PTR_BITS);
592 SET_FLATPTR(pipe->next_td, next);
Kevin O'Connor95714392010-02-17 22:59:53 -0500593 barrier();
Kevin O'Connor114592f2009-09-28 21:32:08 -0400594 SET_FLATPTR(td->status, (uhci_maxerr(0) | (status & TD_CTRL_LS)
595 | TD_CTRL_ACTIVE));
Kevin O'Connor114592f2009-09-28 21:32:08 -0400596
597 return 0;
598}