blob: 17145e503f5749a990e95949702f0cada72c4545 [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
Kevin O'Connor4f6563e2012-05-19 22:42:51 -040014#include "biosvar.h" // GET_LOWFLAT
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;
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -050019 struct uhci_qh *control_qh;
Kevin O'Connor406fad62010-02-28 02:23:19 -050020 struct uhci_framelist *framelist;
21};
22
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -050023struct uhci_pipe {
24 struct uhci_qh qh;
25 struct uhci_td *next_td;
26 struct usb_pipe pipe;
27 u16 iobase;
28 u8 toggle;
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -050029};
30
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050031
32/****************************************************************
33 * Root hub
34 ****************************************************************/
35
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040036// Check if device attached to a given port
37static int
38uhci_hub_detect(struct usbhub_s *hub, u32 port)
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050039{
Kevin O'Connor406fad62010-02-28 02:23:19 -050040 struct usb_uhci_s *cntl = container_of(hub->cntl, struct usb_uhci_s, usb);
41 u16 ioport = cntl->iobase + USBPORTSC1 + port * 2;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050042
43 u16 status = inw(ioport);
44 if (!(status & USBPORTSC_CCS))
45 // No device
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040046 return -1;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050047
48 // XXX - if just powered up, need to wait for USB_TIME_ATTDB?
49
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040050 // Begin reset on port
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050051 outw(USBPORTSC_PR, ioport);
52 msleep(USB_TIME_DRSTR);
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040053 return 0;
54}
55
56// Reset device on port
57static int
58uhci_hub_reset(struct usbhub_s *hub, u32 port)
59{
60 struct usb_uhci_s *cntl = container_of(hub->cntl, struct usb_uhci_s, usb);
61 u16 ioport = cntl->iobase + USBPORTSC1 + port * 2;
62
63 // Finish reset on port
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050064 outw(0, ioport);
65 udelay(6); // 64 high-speed bit times
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040066 u16 status = inw(ioport);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050067 if (!(status & USBPORTSC_CCS))
68 // No longer connected
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040069 return -1;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050070 outw(USBPORTSC_PE, ioport);
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040071 return !!(status & USBPORTSC_LSDA);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050072}
73
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040074// Disable port
75static void
76uhci_hub_disconnect(struct usbhub_s *hub, u32 port)
77{
78 struct usb_uhci_s *cntl = container_of(hub->cntl, struct usb_uhci_s, usb);
79 u16 ioport = cntl->iobase + USBPORTSC1 + port * 2;
80 outw(0, ioport);
81}
82
83static struct usbhub_op_s uhci_HubOp = {
84 .detect = uhci_hub_detect,
85 .reset = uhci_hub_reset,
86 .disconnect = uhci_hub_disconnect,
87};
88
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050089// Find any devices connected to the root hub.
90static int
Kevin O'Connor406fad62010-02-28 02:23:19 -050091check_uhci_ports(struct usb_uhci_s *cntl)
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050092{
93 ASSERT32FLAT();
94 struct usbhub_s hub;
95 memset(&hub, 0, sizeof(hub));
Kevin O'Connor406fad62010-02-28 02:23:19 -050096 hub.cntl = &cntl->usb;
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040097 hub.portcount = 2;
98 hub.op = &uhci_HubOp;
99 usb_enumerate(&hub);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500100 return hub.devcount;
101}
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500102
103
104/****************************************************************
105 * Setup
106 ****************************************************************/
Kevin O'Connor114592f2009-09-28 21:32:08 -0400107
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500108// Wait for next USB frame to start - for ensuring safe memory release.
109static void
110uhci_waittick(u16 iobase)
111{
112 barrier();
113 u16 startframe = inw(iobase + USBFRNUM);
114 u64 end = calc_future_tsc(1000 * 5);
115 for (;;) {
116 if (inw(iobase + USBFRNUM) != startframe)
117 break;
118 if (check_tsc(end)) {
119 warn_timeout();
120 return;
121 }
122 yield();
123 }
124}
125
126static void
127uhci_free_pipes(struct usb_uhci_s *cntl)
128{
129 dprintf(7, "uhci_free_pipes %p\n", cntl);
130
131 struct uhci_qh *pos = (void*)(cntl->framelist->links[0] & ~UHCI_PTR_BITS);
132 for (;;) {
133 u32 link = pos->link;
134 if (link == UHCI_PTR_TERM)
135 break;
136 struct uhci_qh *next = (void*)(link & ~UHCI_PTR_BITS);
137 struct uhci_pipe *pipe = container_of(next, struct uhci_pipe, qh);
138 if (pipe->pipe.cntl != &cntl->usb)
139 pos->link = next->link;
140 else
141 pos = next;
142 }
143 uhci_waittick(cntl->iobase);
144 for (;;) {
145 struct usb_pipe *usbpipe = cntl->usb.freelist;
146 if (!usbpipe)
147 break;
148 cntl->usb.freelist = usbpipe->freenext;
149 struct uhci_pipe *pipe = container_of(usbpipe, struct uhci_pipe, pipe);
150 free(pipe);
151 }
152}
153
Kevin O'Connor114592f2009-09-28 21:32:08 -0400154static void
Kevin O'Connor406fad62010-02-28 02:23:19 -0500155reset_uhci(struct usb_uhci_s *cntl, u16 bdf)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400156{
157 // XXX - don't reset if not needed.
158
159 // Reset PIRQ and SMI
Kevin O'Connor406fad62010-02-28 02:23:19 -0500160 pci_config_writew(bdf, USBLEGSUP, USBLEGSUP_RWC);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400161
162 // Reset the HC
Kevin O'Connor406fad62010-02-28 02:23:19 -0500163 outw(USBCMD_HCRESET, cntl->iobase + USBCMD);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400164 udelay(5);
165
166 // Disable interrupts and commands (just to be safe).
Kevin O'Connor406fad62010-02-28 02:23:19 -0500167 outw(0, cntl->iobase + USBINTR);
168 outw(0, cntl->iobase + USBCMD);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400169}
170
171static void
Kevin O'Connor406fad62010-02-28 02:23:19 -0500172configure_uhci(void *data)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400173{
Kevin O'Connor406fad62010-02-28 02:23:19 -0500174 struct usb_uhci_s *cntl = data;
175
Kevin O'Connor114592f2009-09-28 21:32:08 -0400176 // Allocate ram for schedule storage
177 struct uhci_td *term_td = malloc_high(sizeof(*term_td));
178 struct uhci_framelist *fl = memalign_high(sizeof(*fl), sizeof(*fl));
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500179 struct uhci_pipe *intr_pipe = malloc_high(sizeof(*intr_pipe));
180 struct uhci_pipe *term_pipe = malloc_high(sizeof(*term_pipe));
181 if (!term_td || !fl || !intr_pipe || !term_pipe) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500182 warn_noalloc();
Kevin O'Connor406fad62010-02-28 02:23:19 -0500183 goto fail;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400184 }
185
186 // Work around for PIIX errata
187 memset(term_td, 0, sizeof(*term_td));
188 term_td->link = UHCI_PTR_TERM;
189 term_td->token = (uhci_explen(0) | (0x7f << TD_TOKEN_DEVADDR_SHIFT)
190 | USB_PID_IN);
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500191 memset(term_pipe, 0, sizeof(*term_pipe));
192 term_pipe->qh.element = (u32)term_td;
193 term_pipe->qh.link = UHCI_PTR_TERM;
194 term_pipe->pipe.cntl = &cntl->usb;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400195
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500196 // Set schedule to point to primary intr queue head
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500197 memset(intr_pipe, 0, sizeof(*intr_pipe));
198 intr_pipe->qh.element = UHCI_PTR_TERM;
199 intr_pipe->qh.link = (u32)&term_pipe->qh | UHCI_PTR_QH;
200 intr_pipe->pipe.cntl = &cntl->usb;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400201 int i;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500202 for (i=0; i<ARRAY_SIZE(fl->links); i++)
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500203 fl->links[i] = (u32)&intr_pipe->qh | UHCI_PTR_QH;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500204 cntl->framelist = fl;
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500205 cntl->control_qh = &intr_pipe->qh;
Kevin O'Connor95714392010-02-17 22:59:53 -0500206 barrier();
Kevin O'Connor114592f2009-09-28 21:32:08 -0400207
208 // Set the frame length to the default: 1 ms exactly
Kevin O'Connor406fad62010-02-28 02:23:19 -0500209 outb(USBSOF_DEFAULT, cntl->iobase + USBSOF);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400210
211 // Store the frame list base address
Kevin O'Connor406fad62010-02-28 02:23:19 -0500212 outl((u32)fl->links, cntl->iobase + USBFLBASEADD);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400213
214 // Set the current frame number
Kevin O'Connor406fad62010-02-28 02:23:19 -0500215 outw(0, cntl->iobase + USBFRNUM);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400216
Kevin O'Connor114592f2009-09-28 21:32:08 -0400217 // Mark as configured and running with a 64-byte max packet.
Kevin O'Connor406fad62010-02-28 02:23:19 -0500218 outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, cntl->iobase + USBCMD);
219
220 // Find devices
221 int count = check_uhci_ports(cntl);
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500222 uhci_free_pipes(cntl);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500223 if (count)
224 // Success
225 return;
226
227 // No devices found - shutdown and free controller.
228 outw(0, cntl->iobase + USBCMD);
229fail:
230 free(term_td);
231 free(fl);
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500232 free(intr_pipe);
233 free(term_pipe);
Kevin O'Connor0770d672010-03-09 19:33:22 -0500234 free(cntl);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400235}
236
Kevin O'Connora5826b52009-10-24 17:57:29 -0400237void
Kevin O'Connor8ff8e012011-07-09 14:11:21 -0400238uhci_init(struct pci_device *pci, int busid)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400239{
240 if (! CONFIG_USB_UHCI)
Kevin O'Connora5826b52009-10-24 17:57:29 -0400241 return;
Kevin O'Connor8ff8e012011-07-09 14:11:21 -0400242 u16 bdf = pci->bdf;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500243 struct usb_uhci_s *cntl = malloc_tmphigh(sizeof(*cntl));
Kevin O'Connorffdcd3a2011-07-10 15:48:00 -0400244 if (!cntl) {
245 warn_noalloc();
246 return;
247 }
Kevin O'Connor406fad62010-02-28 02:23:19 -0500248 memset(cntl, 0, sizeof(*cntl));
249 cntl->usb.busid = busid;
Kevin O'Connor8ff8e012011-07-09 14:11:21 -0400250 cntl->usb.pci = pci;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500251 cntl->usb.type = USB_TYPE_UHCI;
252 cntl->iobase = (pci_config_readl(bdf, PCI_BASE_ADDRESS_4)
253 & PCI_BASE_ADDRESS_IO_MASK);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400254
Kevin O'Connor9dc243e2010-03-20 17:53:03 -0400255 dprintf(1, "UHCI init on dev %02x:%02x.%x (io=%x)\n"
Kevin O'Connor406fad62010-02-28 02:23:19 -0500256 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf)
257 , pci_bdf_to_fn(bdf), cntl->iobase);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400258
Kevin O'Connor406fad62010-02-28 02:23:19 -0500259 pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400260
Kevin O'Connor406fad62010-02-28 02:23:19 -0500261 reset_uhci(cntl, bdf);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400262
Kevin O'Connor406fad62010-02-28 02:23:19 -0500263 run_thread(configure_uhci, cntl);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400264}
265
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500266
267/****************************************************************
268 * End point communication
269 ****************************************************************/
270
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500271static struct usb_pipe *
Kevin O'Connoreb50eae2012-03-10 08:48:31 -0500272uhci_alloc_intr_pipe(struct usbdevice_s *usbdev
273 , struct usb_endpoint_descriptor *epdesc)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400274{
Kevin O'Connor406fad62010-02-28 02:23:19 -0500275 struct usb_uhci_s *cntl = container_of(
Kevin O'Connoreb50eae2012-03-10 08:48:31 -0500276 usbdev->hub->cntl, struct usb_uhci_s, usb);
277 int frameexp = usb_getFrameExp(usbdev, epdesc);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500278 dprintf(7, "uhci_alloc_intr_pipe %p %d\n", &cntl->usb, frameexp);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400279
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500280 if (frameexp > 10)
281 frameexp = 10;
Kevin O'Connoreb50eae2012-03-10 08:48:31 -0500282 int maxpacket = epdesc->wMaxPacketSize;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500283 // Determine number of entries needed for 2 timer ticks.
284 int ms = 1<<frameexp;
285 int count = DIV_ROUND_UP(PIT_TICK_INTERVAL * 1000 * 2, PIT_TICK_RATE * ms);
Kevin O'Connor0770d672010-03-09 19:33:22 -0500286 count = ALIGN(count, 2);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500287 struct uhci_pipe *pipe = malloc_low(sizeof(*pipe));
Kevin O'Connor114592f2009-09-28 21:32:08 -0400288 struct uhci_td *tds = malloc_low(sizeof(*tds) * count);
Kevin O'Connor09e2f7c2010-02-27 23:50:48 -0500289 void *data = malloc_low(maxpacket * count);
290 if (!pipe || !tds || !data) {
Kevin O'Connor95714392010-02-17 22:59:53 -0500291 warn_noalloc();
292 goto fail;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500293 }
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500294 memset(pipe, 0, sizeof(*pipe));
Kevin O'Connoreb50eae2012-03-10 08:48:31 -0500295 usb_desc2pipe(&pipe->pipe, usbdev, epdesc);
296 int lowspeed = pipe->pipe.speed;
297 int devaddr = pipe->pipe.devaddr | (pipe->pipe.ep << 7);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500298 pipe->qh.element = (u32)tds;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500299 pipe->next_td = &tds[0];
Kevin O'Connor406fad62010-02-28 02:23:19 -0500300 pipe->iobase = cntl->iobase;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500301
Kevin O'Connor114592f2009-09-28 21:32:08 -0400302 int toggle = 0;
303 int i;
304 for (i=0; i<count; i++) {
305 tds[i].link = (i==count-1 ? (u32)&tds[0] : (u32)&tds[i+1]);
306 tds[i].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
307 | TD_CTRL_ACTIVE);
308 tds[i].token = (uhci_explen(maxpacket) | toggle
309 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
310 | USB_PID_IN);
Kevin O'Connor09e2f7c2010-02-27 23:50:48 -0500311 tds[i].buffer = data + maxpacket * i;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400312 toggle ^= TD_TOKEN_TOGGLE;
313 }
314
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500315 // Add to interrupt schedule.
Kevin O'Connor406fad62010-02-28 02:23:19 -0500316 struct uhci_framelist *fl = cntl->framelist;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500317 if (frameexp == 0) {
318 // Add to existing interrupt entry.
319 struct uhci_qh *intr_qh = (void*)(fl->links[0] & ~UHCI_PTR_BITS);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500320 pipe->qh.link = intr_qh->link;
Kevin O'Connor95714392010-02-17 22:59:53 -0500321 barrier();
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500322 intr_qh->link = (u32)&pipe->qh | UHCI_PTR_QH;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500323 if (cntl->control_qh == intr_qh)
324 cntl->control_qh = &pipe->qh;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500325 } else {
326 int startpos = 1<<(frameexp-1);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500327 pipe->qh.link = fl->links[startpos];
Kevin O'Connor95714392010-02-17 22:59:53 -0500328 barrier();
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500329 for (i=startpos; i<ARRAY_SIZE(fl->links); i+=ms)
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500330 fl->links[i] = (u32)&pipe->qh | UHCI_PTR_QH;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500331 }
Kevin O'Connor114592f2009-09-28 21:32:08 -0400332
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500333 return &pipe->pipe;
Kevin O'Connor95714392010-02-17 22:59:53 -0500334fail:
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500335 free(pipe);
Kevin O'Connor95714392010-02-17 22:59:53 -0500336 free(tds);
Kevin O'Connor09e2f7c2010-02-27 23:50:48 -0500337 free(data);
Kevin O'Connor95714392010-02-17 22:59:53 -0500338 return NULL;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400339}
340
Kevin O'Connor6152c322012-03-10 08:53:58 -0500341struct usb_pipe *
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500342uhci_alloc_pipe(struct usbdevice_s *usbdev
343 , struct usb_endpoint_descriptor *epdesc)
Kevin O'Connor6152c322012-03-10 08:53:58 -0500344{
345 if (! CONFIG_USB_UHCI)
346 return NULL;
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500347 u8 eptype = epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
348 if (eptype == USB_ENDPOINT_XFER_INT)
349 return uhci_alloc_intr_pipe(usbdev, epdesc);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500350 struct usb_uhci_s *cntl = container_of(
351 usbdev->hub->cntl, struct usb_uhci_s, usb);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500352 dprintf(7, "uhci_alloc_async_pipe %p %d\n", &cntl->usb, eptype);
353
354 struct usb_pipe *usbpipe = usb_getFreePipe(&cntl->usb, eptype);
355 if (usbpipe) {
356 // Use previously allocated pipe.
357 usb_desc2pipe(usbpipe, usbdev, epdesc);
358 return usbpipe;
359 }
360
361 // Allocate a new queue head.
362 struct uhci_pipe *pipe;
363 if (eptype == USB_ENDPOINT_XFER_CONTROL)
364 pipe = malloc_tmphigh(sizeof(*pipe));
365 else
366 pipe = malloc_low(sizeof(*pipe));
367 if (!pipe) {
368 warn_noalloc();
369 return NULL;
370 }
371 memset(pipe, 0, sizeof(*pipe));
372 usb_desc2pipe(&pipe->pipe, usbdev, epdesc);
373 pipe->qh.element = UHCI_PTR_TERM;
374 pipe->iobase = cntl->iobase;
375
376 // Add queue head to controller list.
377 struct uhci_qh *control_qh = cntl->control_qh;
378 pipe->qh.link = control_qh->link;
379 barrier();
380 control_qh->link = (u32)&pipe->qh | UHCI_PTR_QH;
381 if (eptype == USB_ENDPOINT_XFER_CONTROL)
382 cntl->control_qh = &pipe->qh;
383 return &pipe->pipe;
384}
385
386static int
387wait_pipe(struct uhci_pipe *pipe, int timeout)
388{
389 u64 end = calc_future_tsc(timeout);
390 for (;;) {
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400391 u32 el_link = GET_LOWFLAT(pipe->qh.element);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500392 if (el_link & UHCI_PTR_TERM)
393 return 0;
394 if (check_tsc(end)) {
395 warn_timeout();
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400396 u16 iobase = GET_LOWFLAT(pipe->iobase);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500397 struct uhci_td *td = (void*)(el_link & ~UHCI_PTR_BITS);
398 dprintf(1, "Timeout on wait_pipe %p (td=%p s=%x c=%x/%x)\n"
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400399 , pipe, (void*)el_link, GET_LOWFLAT(td->status)
Kevin O'Connor6152c322012-03-10 08:53:58 -0500400 , inw(iobase + USBCMD)
401 , inw(iobase + USBSTS));
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400402 SET_LOWFLAT(pipe->qh.element, UHCI_PTR_TERM);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500403 uhci_waittick(iobase);
404 return -1;
405 }
406 yield();
407 }
408}
409
410static int
411wait_td(struct uhci_td *td)
412{
413 u64 end = calc_future_tsc(5000); // XXX - lookup real time.
414 u32 status;
415 for (;;) {
416 status = td->status;
417 if (!(status & TD_CTRL_ACTIVE))
418 break;
419 if (check_tsc(end)) {
420 warn_timeout();
421 return -1;
422 }
423 yield();
424 }
425 if (status & TD_CTRL_ANY_ERROR) {
426 dprintf(1, "wait_td error - status=%x\n", status);
427 return -2;
428 }
429 return 0;
430}
431
432int
433uhci_control(struct usb_pipe *p, int dir, const void *cmd, int cmdsize
434 , void *data, int datasize)
435{
436 ASSERT32FLAT();
437 if (! CONFIG_USB_UHCI)
438 return -1;
439 dprintf(5, "uhci_control %p\n", p);
440 struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
441
442 int maxpacket = pipe->pipe.maxpacket;
443 int lowspeed = pipe->pipe.speed;
444 int devaddr = pipe->pipe.devaddr | (pipe->pipe.ep << 7);
445
446 // Setup transfer descriptors
447 int count = 2 + DIV_ROUND_UP(datasize, maxpacket);
448 struct uhci_td *tds = malloc_tmphigh(sizeof(*tds) * count);
449 if (!tds) {
450 warn_noalloc();
451 return -1;
452 }
453
454 tds[0].link = (u32)&tds[1] | UHCI_PTR_DEPTH;
455 tds[0].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
456 | TD_CTRL_ACTIVE);
457 tds[0].token = (uhci_explen(cmdsize) | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
458 | USB_PID_SETUP);
459 tds[0].buffer = (void*)cmd;
460 int toggle = TD_TOKEN_TOGGLE;
461 int i;
462 for (i=1; i<count-1; i++) {
463 tds[i].link = (u32)&tds[i+1] | UHCI_PTR_DEPTH;
464 tds[i].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
465 | TD_CTRL_ACTIVE);
466 int len = (i == count-2 ? (datasize - (i-1)*maxpacket) : maxpacket);
467 tds[i].token = (uhci_explen(len) | toggle
468 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
469 | (dir ? USB_PID_IN : USB_PID_OUT));
470 tds[i].buffer = data + (i-1) * maxpacket;
471 toggle ^= TD_TOKEN_TOGGLE;
472 }
473 tds[i].link = UHCI_PTR_TERM;
474 tds[i].status = (uhci_maxerr(0) | (lowspeed ? TD_CTRL_LS : 0)
475 | TD_CTRL_ACTIVE);
476 tds[i].token = (uhci_explen(0) | TD_TOKEN_TOGGLE
477 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
478 | (dir ? USB_PID_OUT : USB_PID_IN));
479 tds[i].buffer = 0;
480
481 // Transfer data
482 barrier();
483 pipe->qh.element = (u32)&tds[0];
484 int ret = wait_pipe(pipe, 500);
485 free(tds);
486 return ret;
487}
488
489#define STACKTDS 4
490#define TDALIGN 16
491
492int
493uhci_send_bulk(struct usb_pipe *p, int dir, void *data, int datasize)
494{
495 if (! CONFIG_USB_UHCI)
496 return -1;
497 struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
498 dprintf(7, "uhci_send_bulk qh=%p dir=%d data=%p size=%d\n"
499 , &pipe->qh, dir, data, datasize);
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400500 int maxpacket = GET_LOWFLAT(pipe->pipe.maxpacket);
501 int lowspeed = GET_LOWFLAT(pipe->pipe.speed);
502 int devaddr = (GET_LOWFLAT(pipe->pipe.devaddr)
503 | (GET_LOWFLAT(pipe->pipe.ep) << 7));
504 int toggle = GET_LOWFLAT(pipe->toggle) ? TD_TOKEN_TOGGLE : 0;
Kevin O'Connor6152c322012-03-10 08:53:58 -0500505
506 // Allocate 4 tds on stack (16byte aligned)
507 u8 tdsbuf[sizeof(struct uhci_td) * STACKTDS + TDALIGN - 1];
508 struct uhci_td *tds = (void*)ALIGN((u32)tdsbuf, TDALIGN);
509 memset(tds, 0, sizeof(*tds) * STACKTDS);
510
511 // Enable tds
512 barrier();
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400513 SET_LOWFLAT(pipe->qh.element, (u32)MAKE_FLATPTR(GET_SEG(SS), tds));
Kevin O'Connor6152c322012-03-10 08:53:58 -0500514
515 int tdpos = 0;
516 while (datasize) {
517 struct uhci_td *td = &tds[tdpos++ % STACKTDS];
518 int ret = wait_td(td);
519 if (ret)
520 goto fail;
521
522 int transfer = datasize;
523 if (transfer > maxpacket)
524 transfer = maxpacket;
525 struct uhci_td *nexttd_fl = MAKE_FLATPTR(GET_SEG(SS)
526 , &tds[tdpos % STACKTDS]);
527 td->link = (transfer==datasize ? UHCI_PTR_TERM : (u32)nexttd_fl);
528 td->token = (uhci_explen(transfer) | toggle
529 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
530 | (dir ? USB_PID_IN : USB_PID_OUT));
531 td->buffer = data;
532 barrier();
533 td->status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
534 | TD_CTRL_ACTIVE);
535 toggle ^= TD_TOKEN_TOGGLE;
536
537 data += transfer;
538 datasize -= transfer;
539 }
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400540 SET_LOWFLAT(pipe->toggle, !!toggle);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500541 return wait_pipe(pipe, 5000);
542fail:
543 dprintf(1, "uhci_send_bulk failed\n");
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400544 SET_LOWFLAT(pipe->qh.element, UHCI_PTR_TERM);
545 uhci_waittick(GET_LOWFLAT(pipe->iobase));
Kevin O'Connor6152c322012-03-10 08:53:58 -0500546 return -1;
547}
548
Kevin O'Connor114592f2009-09-28 21:32:08 -0400549int
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500550uhci_poll_intr(struct usb_pipe *p, void *data)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400551{
552 ASSERT16();
553 if (! CONFIG_USB_UHCI)
554 return -1;
555
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500556 struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400557 struct uhci_td *td = GET_LOWFLAT(pipe->next_td);
558 u32 status = GET_LOWFLAT(td->status);
559 u32 token = GET_LOWFLAT(td->token);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400560 if (status & TD_CTRL_ACTIVE)
561 // No intrs found.
562 return -1;
563 // XXX - check for errors.
564
565 // Copy data.
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400566 void *tddata = GET_LOWFLAT(td->buffer);
567 memcpy_far(GET_SEG(SS), data, SEG_LOW, LOWFLAT2LOW(tddata)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400568 , uhci_expected_length(token));
569
570 // Reenable this td.
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400571 struct uhci_td *next = (void*)(GET_LOWFLAT(td->link) & ~UHCI_PTR_BITS);
572 SET_LOWFLAT(pipe->next_td, next);
Kevin O'Connor95714392010-02-17 22:59:53 -0500573 barrier();
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400574 SET_LOWFLAT(td->status, (uhci_maxerr(0) | (status & TD_CTRL_LS)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400575 | TD_CTRL_ACTIVE));
Kevin O'Connor114592f2009-09-28 21:32:08 -0400576
577 return 0;
578}