blob: 075ed02cd854b1186342c9a667ddf4a1d43a9f51 [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
Kevin O'Connor2d2fa312013-09-14 21:55:26 -04007#include "biosvar.h" // GET_LOWFLAT
Kevin O'Connor114592f2009-09-28 21:32:08 -04008#include "config.h" // CONFIG_*
Kevin O'Connor9dea5902013-09-14 20:23:54 -04009#include "malloc.h" // free
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040010#include "output.h" // dprintf
Kevin O'Connor4d8510c2016-02-03 01:28:20 -050011#include "pci.h" // pci_config_writew
12#include "pcidevice.h" // foreachpci
Kevin O'Connorec443ff2013-12-05 18:43:20 -050013#include "pci_ids.h" // PCI_CLASS_SERIAL_USB_UHCI
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040014#include "pci_regs.h" // PCI_BASE_ADDRESS_4
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040015#include "string.h" // memset
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040016#include "usb.h" // struct usb_s
Kevin O'Connor32a2b0e2016-02-02 14:28:13 -050017#include "usb-ehci.h" // ehci_wait_controllers
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040018#include "usb-uhci.h" // USBLEGSUP
19#include "util.h" // msleep
Kevin O'Connor4ade5232013-09-18 21:41:48 -040020#include "x86.h" // outw
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050021
Kevin O'Connor406fad62010-02-28 02:23:19 -050022struct usb_uhci_s {
23 struct usb_s usb;
24 u16 iobase;
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -050025 struct uhci_qh *control_qh;
Kevin O'Connor406fad62010-02-28 02:23:19 -050026 struct uhci_framelist *framelist;
27};
28
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -050029struct uhci_pipe {
30 struct uhci_qh qh;
31 struct uhci_td *next_td;
32 struct usb_pipe pipe;
33 u16 iobase;
34 u8 toggle;
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -050035};
36
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050037
38/****************************************************************
39 * Root hub
40 ****************************************************************/
41
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040042// Check if device attached to a given port
43static int
44uhci_hub_detect(struct usbhub_s *hub, u32 port)
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050045{
Kevin O'Connor406fad62010-02-28 02:23:19 -050046 struct usb_uhci_s *cntl = container_of(hub->cntl, struct usb_uhci_s, usb);
47 u16 ioport = cntl->iobase + USBPORTSC1 + port * 2;
Kevin O'Connor0f681302014-09-10 11:33:01 -040048 u16 status = inw(ioport);
49 if (!(status & USBPORTSC_CCS))
50 // No device found.
51 return 0;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050052
53 // XXX - if just powered up, need to wait for USB_TIME_ATTDB?
54
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040055 // Begin reset on port
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050056 outw(USBPORTSC_PR, ioport);
57 msleep(USB_TIME_DRSTR);
Kevin O'Connor0f681302014-09-10 11:33:01 -040058 return 1;
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040059}
60
61// Reset device on port
62static int
63uhci_hub_reset(struct usbhub_s *hub, u32 port)
64{
65 struct usb_uhci_s *cntl = container_of(hub->cntl, struct usb_uhci_s, usb);
66 u16 ioport = cntl->iobase + USBPORTSC1 + port * 2;
67
68 // Finish reset on port
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050069 outw(0, ioport);
70 udelay(6); // 64 high-speed bit times
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040071 u16 status = inw(ioport);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050072 if (!(status & USBPORTSC_CCS))
73 // No longer connected
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040074 return -1;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050075 outw(USBPORTSC_PE, ioport);
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040076 return !!(status & USBPORTSC_LSDA);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050077}
78
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040079// Disable port
80static void
81uhci_hub_disconnect(struct usbhub_s *hub, u32 port)
82{
83 struct usb_uhci_s *cntl = container_of(hub->cntl, struct usb_uhci_s, usb);
84 u16 ioport = cntl->iobase + USBPORTSC1 + port * 2;
85 outw(0, ioport);
86}
87
88static struct usbhub_op_s uhci_HubOp = {
89 .detect = uhci_hub_detect,
90 .reset = uhci_hub_reset,
91 .disconnect = uhci_hub_disconnect,
92};
93
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050094// Find any devices connected to the root hub.
95static int
Kevin O'Connor406fad62010-02-28 02:23:19 -050096check_uhci_ports(struct usb_uhci_s *cntl)
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050097{
98 ASSERT32FLAT();
Kevin O'Connor32a2b0e2016-02-02 14:28:13 -050099 // Wait for ehci init - in case this is a "companion controller"
100 ehci_wait_controllers();
101
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500102 struct usbhub_s hub;
103 memset(&hub, 0, sizeof(hub));
Kevin O'Connor406fad62010-02-28 02:23:19 -0500104 hub.cntl = &cntl->usb;
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400105 hub.portcount = 2;
106 hub.op = &uhci_HubOp;
107 usb_enumerate(&hub);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500108 return hub.devcount;
109}
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500110
111
112/****************************************************************
113 * Setup
114 ****************************************************************/
Kevin O'Connor114592f2009-09-28 21:32:08 -0400115
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500116// Wait for next USB frame to start - for ensuring safe memory release.
117static void
118uhci_waittick(u16 iobase)
119{
120 barrier();
121 u16 startframe = inw(iobase + USBFRNUM);
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400122 u32 end = timer_calc(1000 * 5);
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500123 for (;;) {
124 if (inw(iobase + USBFRNUM) != startframe)
125 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400126 if (timer_check(end)) {
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500127 warn_timeout();
128 return;
129 }
130 yield();
131 }
132}
133
134static void
135uhci_free_pipes(struct usb_uhci_s *cntl)
136{
137 dprintf(7, "uhci_free_pipes %p\n", cntl);
138
139 struct uhci_qh *pos = (void*)(cntl->framelist->links[0] & ~UHCI_PTR_BITS);
140 for (;;) {
141 u32 link = pos->link;
142 if (link == UHCI_PTR_TERM)
143 break;
144 struct uhci_qh *next = (void*)(link & ~UHCI_PTR_BITS);
145 struct uhci_pipe *pipe = container_of(next, struct uhci_pipe, qh);
Kevin O'Connor2bfd1702014-10-16 12:31:42 -0400146 if (usb_is_freelist(&cntl->usb, &pipe->pipe))
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500147 pos->link = next->link;
148 else
149 pos = next;
150 }
151 uhci_waittick(cntl->iobase);
152 for (;;) {
153 struct usb_pipe *usbpipe = cntl->usb.freelist;
154 if (!usbpipe)
155 break;
156 cntl->usb.freelist = usbpipe->freenext;
157 struct uhci_pipe *pipe = container_of(usbpipe, struct uhci_pipe, pipe);
158 free(pipe);
159 }
160}
161
Kevin O'Connor114592f2009-09-28 21:32:08 -0400162static void
Kevin O'Connor406fad62010-02-28 02:23:19 -0500163reset_uhci(struct usb_uhci_s *cntl, u16 bdf)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400164{
165 // XXX - don't reset if not needed.
166
167 // Reset PIRQ and SMI
Kevin O'Connor406fad62010-02-28 02:23:19 -0500168 pci_config_writew(bdf, USBLEGSUP, USBLEGSUP_RWC);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400169
170 // Reset the HC
Kevin O'Connor406fad62010-02-28 02:23:19 -0500171 outw(USBCMD_HCRESET, cntl->iobase + USBCMD);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400172 udelay(5);
173
174 // Disable interrupts and commands (just to be safe).
Kevin O'Connor406fad62010-02-28 02:23:19 -0500175 outw(0, cntl->iobase + USBINTR);
176 outw(0, cntl->iobase + USBCMD);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400177}
178
179static void
Kevin O'Connor406fad62010-02-28 02:23:19 -0500180configure_uhci(void *data)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400181{
Kevin O'Connor406fad62010-02-28 02:23:19 -0500182 struct usb_uhci_s *cntl = data;
183
Kevin O'Connor114592f2009-09-28 21:32:08 -0400184 // Allocate ram for schedule storage
185 struct uhci_td *term_td = malloc_high(sizeof(*term_td));
186 struct uhci_framelist *fl = memalign_high(sizeof(*fl), sizeof(*fl));
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500187 struct uhci_pipe *intr_pipe = malloc_high(sizeof(*intr_pipe));
188 struct uhci_pipe *term_pipe = malloc_high(sizeof(*term_pipe));
189 if (!term_td || !fl || !intr_pipe || !term_pipe) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500190 warn_noalloc();
Kevin O'Connor406fad62010-02-28 02:23:19 -0500191 goto fail;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400192 }
193
194 // Work around for PIIX errata
195 memset(term_td, 0, sizeof(*term_td));
196 term_td->link = UHCI_PTR_TERM;
197 term_td->token = (uhci_explen(0) | (0x7f << TD_TOKEN_DEVADDR_SHIFT)
198 | USB_PID_IN);
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500199 memset(term_pipe, 0, sizeof(*term_pipe));
200 term_pipe->qh.element = (u32)term_td;
201 term_pipe->qh.link = UHCI_PTR_TERM;
202 term_pipe->pipe.cntl = &cntl->usb;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400203
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500204 // Set schedule to point to primary intr queue head
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500205 memset(intr_pipe, 0, sizeof(*intr_pipe));
206 intr_pipe->qh.element = UHCI_PTR_TERM;
207 intr_pipe->qh.link = (u32)&term_pipe->qh | UHCI_PTR_QH;
208 intr_pipe->pipe.cntl = &cntl->usb;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400209 int i;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500210 for (i=0; i<ARRAY_SIZE(fl->links); i++)
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500211 fl->links[i] = (u32)&intr_pipe->qh | UHCI_PTR_QH;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500212 cntl->framelist = fl;
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500213 cntl->control_qh = &intr_pipe->qh;
Kevin O'Connor95714392010-02-17 22:59:53 -0500214 barrier();
Kevin O'Connor114592f2009-09-28 21:32:08 -0400215
216 // Set the frame length to the default: 1 ms exactly
Kevin O'Connor406fad62010-02-28 02:23:19 -0500217 outb(USBSOF_DEFAULT, cntl->iobase + USBSOF);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400218
219 // Store the frame list base address
Kevin O'Connor406fad62010-02-28 02:23:19 -0500220 outl((u32)fl->links, cntl->iobase + USBFLBASEADD);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400221
222 // Set the current frame number
Kevin O'Connor406fad62010-02-28 02:23:19 -0500223 outw(0, cntl->iobase + USBFRNUM);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400224
Kevin O'Connor114592f2009-09-28 21:32:08 -0400225 // Mark as configured and running with a 64-byte max packet.
Kevin O'Connor406fad62010-02-28 02:23:19 -0500226 outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, cntl->iobase + USBCMD);
227
228 // Find devices
229 int count = check_uhci_ports(cntl);
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500230 uhci_free_pipes(cntl);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500231 if (count)
232 // Success
233 return;
234
235 // No devices found - shutdown and free controller.
236 outw(0, cntl->iobase + USBCMD);
237fail:
238 free(term_td);
239 free(fl);
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500240 free(intr_pipe);
241 free(term_pipe);
Kevin O'Connor0770d672010-03-09 19:33:22 -0500242 free(cntl);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400243}
244
Kevin O'Connorec443ff2013-12-05 18:43:20 -0500245static void
246uhci_controller_setup(struct pci_device *pci)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400247{
Kevin O'Connora46fdbf2016-02-02 22:29:49 -0500248 u16 iobase = pci_enable_iobar(pci, PCI_BASE_ADDRESS_4);
249 if (!iobase)
250 return;
251
Kevin O'Connor406fad62010-02-28 02:23:19 -0500252 struct usb_uhci_s *cntl = malloc_tmphigh(sizeof(*cntl));
Kevin O'Connorffdcd3a2011-07-10 15:48:00 -0400253 if (!cntl) {
254 warn_noalloc();
255 return;
256 }
Kevin O'Connor406fad62010-02-28 02:23:19 -0500257 memset(cntl, 0, sizeof(*cntl));
Kevin O'Connor8ff8e012011-07-09 14:11:21 -0400258 cntl->usb.pci = pci;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500259 cntl->usb.type = USB_TYPE_UHCI;
Kevin O'Connora46fdbf2016-02-02 22:29:49 -0500260 cntl->iobase = iobase;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400261
Kevin O'Connor7b673002016-02-03 03:03:15 -0500262 dprintf(1, "UHCI init on dev %pP (io=%x)\n", pci, cntl->iobase);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400263
Kevin O'Connora46fdbf2016-02-02 22:29:49 -0500264 pci_enable_busmaster(pci);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400265
Kevin O'Connora46fdbf2016-02-02 22:29:49 -0500266 reset_uhci(cntl, pci->bdf);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400267
Kevin O'Connor406fad62010-02-28 02:23:19 -0500268 run_thread(configure_uhci, cntl);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400269}
270
Kevin O'Connorec443ff2013-12-05 18:43:20 -0500271void
272uhci_setup(void)
273{
274 if (! CONFIG_USB_UHCI)
275 return;
276 struct pci_device *pci;
277 foreachpci(pci) {
278 if (pci_classprog(pci) == PCI_CLASS_SERIAL_USB_UHCI)
279 uhci_controller_setup(pci);
280 }
281}
282
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500283
284/****************************************************************
285 * End point communication
286 ****************************************************************/
287
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500288static struct usb_pipe *
Kevin O'Connoreb50eae2012-03-10 08:48:31 -0500289uhci_alloc_intr_pipe(struct usbdevice_s *usbdev
290 , struct usb_endpoint_descriptor *epdesc)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400291{
Kevin O'Connor406fad62010-02-28 02:23:19 -0500292 struct usb_uhci_s *cntl = container_of(
Kevin O'Connoreb50eae2012-03-10 08:48:31 -0500293 usbdev->hub->cntl, struct usb_uhci_s, usb);
Kevin O'Connor98cdad32014-10-16 12:05:09 -0400294 int frameexp = usb_get_period(usbdev, epdesc);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500295 dprintf(7, "uhci_alloc_intr_pipe %p %d\n", &cntl->usb, frameexp);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400296
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500297 if (frameexp > 10)
298 frameexp = 10;
Kevin O'Connoreb50eae2012-03-10 08:48:31 -0500299 int maxpacket = epdesc->wMaxPacketSize;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500300 // Determine number of entries needed for 2 timer ticks.
301 int ms = 1<<frameexp;
Kevin O'Connor69013372013-07-20 12:08:48 -0400302 int count = DIV_ROUND_UP(ticks_to_ms(2), ms);
Kevin O'Connor0770d672010-03-09 19:33:22 -0500303 count = ALIGN(count, 2);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500304 struct uhci_pipe *pipe = malloc_low(sizeof(*pipe));
Kevin O'Connor114592f2009-09-28 21:32:08 -0400305 struct uhci_td *tds = malloc_low(sizeof(*tds) * count);
Kevin O'Connor09e2f7c2010-02-27 23:50:48 -0500306 void *data = malloc_low(maxpacket * count);
307 if (!pipe || !tds || !data) {
Kevin O'Connor95714392010-02-17 22:59:53 -0500308 warn_noalloc();
309 goto fail;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500310 }
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500311 memset(pipe, 0, sizeof(*pipe));
Kevin O'Connoreb50eae2012-03-10 08:48:31 -0500312 usb_desc2pipe(&pipe->pipe, usbdev, epdesc);
313 int lowspeed = pipe->pipe.speed;
314 int devaddr = pipe->pipe.devaddr | (pipe->pipe.ep << 7);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500315 pipe->qh.element = (u32)tds;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500316 pipe->next_td = &tds[0];
Kevin O'Connor406fad62010-02-28 02:23:19 -0500317 pipe->iobase = cntl->iobase;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500318
Kevin O'Connor114592f2009-09-28 21:32:08 -0400319 int toggle = 0;
320 int i;
321 for (i=0; i<count; i++) {
322 tds[i].link = (i==count-1 ? (u32)&tds[0] : (u32)&tds[i+1]);
323 tds[i].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
324 | TD_CTRL_ACTIVE);
325 tds[i].token = (uhci_explen(maxpacket) | toggle
326 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
327 | USB_PID_IN);
Kevin O'Connor09e2f7c2010-02-27 23:50:48 -0500328 tds[i].buffer = data + maxpacket * i;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400329 toggle ^= TD_TOKEN_TOGGLE;
330 }
331
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500332 // Add to interrupt schedule.
Kevin O'Connor406fad62010-02-28 02:23:19 -0500333 struct uhci_framelist *fl = cntl->framelist;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500334 if (frameexp == 0) {
335 // Add to existing interrupt entry.
336 struct uhci_qh *intr_qh = (void*)(fl->links[0] & ~UHCI_PTR_BITS);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500337 pipe->qh.link = intr_qh->link;
Kevin O'Connor95714392010-02-17 22:59:53 -0500338 barrier();
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500339 intr_qh->link = (u32)&pipe->qh | UHCI_PTR_QH;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500340 if (cntl->control_qh == intr_qh)
341 cntl->control_qh = &pipe->qh;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500342 } else {
343 int startpos = 1<<(frameexp-1);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500344 pipe->qh.link = fl->links[startpos];
Kevin O'Connor95714392010-02-17 22:59:53 -0500345 barrier();
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500346 for (i=startpos; i<ARRAY_SIZE(fl->links); i+=ms)
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500347 fl->links[i] = (u32)&pipe->qh | UHCI_PTR_QH;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500348 }
Kevin O'Connor114592f2009-09-28 21:32:08 -0400349
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500350 return &pipe->pipe;
Kevin O'Connor95714392010-02-17 22:59:53 -0500351fail:
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500352 free(pipe);
Kevin O'Connor95714392010-02-17 22:59:53 -0500353 free(tds);
Kevin O'Connor09e2f7c2010-02-27 23:50:48 -0500354 free(data);
Kevin O'Connor95714392010-02-17 22:59:53 -0500355 return NULL;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400356}
357
Kevin O'Connor6152c322012-03-10 08:53:58 -0500358struct usb_pipe *
Kevin O'Connor89452cc2014-10-16 13:33:35 -0400359uhci_realloc_pipe(struct usbdevice_s *usbdev, struct usb_pipe *upipe
360 , struct usb_endpoint_descriptor *epdesc)
Kevin O'Connor6152c322012-03-10 08:53:58 -0500361{
362 if (! CONFIG_USB_UHCI)
363 return NULL;
Kevin O'Connor89452cc2014-10-16 13:33:35 -0400364 usb_add_freelist(upipe);
365 if (!epdesc)
366 return NULL;
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500367 u8 eptype = epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
368 if (eptype == USB_ENDPOINT_XFER_INT)
369 return uhci_alloc_intr_pipe(usbdev, epdesc);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500370 struct usb_uhci_s *cntl = container_of(
371 usbdev->hub->cntl, struct usb_uhci_s, usb);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500372 dprintf(7, "uhci_alloc_async_pipe %p %d\n", &cntl->usb, eptype);
373
Kevin O'Connor2bfd1702014-10-16 12:31:42 -0400374 struct usb_pipe *usbpipe = usb_get_freelist(&cntl->usb, eptype);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500375 if (usbpipe) {
376 // Use previously allocated pipe.
377 usb_desc2pipe(usbpipe, usbdev, epdesc);
378 return usbpipe;
379 }
380
381 // Allocate a new queue head.
382 struct uhci_pipe *pipe;
383 if (eptype == USB_ENDPOINT_XFER_CONTROL)
384 pipe = malloc_tmphigh(sizeof(*pipe));
385 else
386 pipe = malloc_low(sizeof(*pipe));
387 if (!pipe) {
388 warn_noalloc();
389 return NULL;
390 }
391 memset(pipe, 0, sizeof(*pipe));
392 usb_desc2pipe(&pipe->pipe, usbdev, epdesc);
393 pipe->qh.element = UHCI_PTR_TERM;
394 pipe->iobase = cntl->iobase;
395
396 // Add queue head to controller list.
397 struct uhci_qh *control_qh = cntl->control_qh;
398 pipe->qh.link = control_qh->link;
399 barrier();
400 control_qh->link = (u32)&pipe->qh | UHCI_PTR_QH;
401 if (eptype == USB_ENDPOINT_XFER_CONTROL)
402 cntl->control_qh = &pipe->qh;
403 return &pipe->pipe;
404}
405
406static int
Kevin O'Connorb53fe2c2014-06-14 12:50:20 -0400407wait_pipe(struct uhci_pipe *pipe, u32 end)
Kevin O'Connor6152c322012-03-10 08:53:58 -0500408{
Kevin O'Connor6152c322012-03-10 08:53:58 -0500409 for (;;) {
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400410 u32 el_link = GET_LOWFLAT(pipe->qh.element);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500411 if (el_link & UHCI_PTR_TERM)
412 return 0;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400413 if (timer_check(end)) {
Kevin O'Connor6152c322012-03-10 08:53:58 -0500414 warn_timeout();
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400415 u16 iobase = GET_LOWFLAT(pipe->iobase);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500416 struct uhci_td *td = (void*)(el_link & ~UHCI_PTR_BITS);
417 dprintf(1, "Timeout on wait_pipe %p (td=%p s=%x c=%x/%x)\n"
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400418 , pipe, (void*)el_link, GET_LOWFLAT(td->status)
Kevin O'Connor6152c322012-03-10 08:53:58 -0500419 , inw(iobase + USBCMD)
420 , inw(iobase + USBSTS));
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400421 SET_LOWFLAT(pipe->qh.element, UHCI_PTR_TERM);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500422 uhci_waittick(iobase);
423 return -1;
424 }
425 yield();
426 }
427}
428
429static int
Kevin O'Connorb53fe2c2014-06-14 12:50:20 -0400430wait_td(struct uhci_td *td, u32 end)
Kevin O'Connor6152c322012-03-10 08:53:58 -0500431{
Kevin O'Connor6152c322012-03-10 08:53:58 -0500432 u32 status;
433 for (;;) {
434 status = td->status;
435 if (!(status & TD_CTRL_ACTIVE))
436 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400437 if (timer_check(end)) {
Kevin O'Connor6152c322012-03-10 08:53:58 -0500438 warn_timeout();
439 return -1;
440 }
441 yield();
442 }
443 if (status & TD_CTRL_ANY_ERROR) {
444 dprintf(1, "wait_td error - status=%x\n", status);
445 return -2;
446 }
447 return 0;
448}
449
Kevin O'Connor6f86b892014-12-31 01:42:38 -0500450#define STACKTDS 16
Kevin O'Connor6152c322012-03-10 08:53:58 -0500451#define TDALIGN 16
452
453int
Kevin O'Connor2891a832014-12-31 17:41:14 -0500454uhci_send_pipe(struct usb_pipe *p, int dir, const void *cmd
Kevin O'Connoreb9f3ae2014-12-31 01:56:53 -0500455 , void *data, int datasize)
Kevin O'Connor6152c322012-03-10 08:53:58 -0500456{
457 if (! CONFIG_USB_UHCI)
458 return -1;
459 struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
Kevin O'Connoreb9f3ae2014-12-31 01:56:53 -0500460 dprintf(7, "uhci_send_pipe qh=%p dir=%d data=%p size=%d\n"
Kevin O'Connor6152c322012-03-10 08:53:58 -0500461 , &pipe->qh, dir, data, datasize);
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400462 int maxpacket = GET_LOWFLAT(pipe->pipe.maxpacket);
463 int lowspeed = GET_LOWFLAT(pipe->pipe.speed);
464 int devaddr = (GET_LOWFLAT(pipe->pipe.devaddr)
465 | (GET_LOWFLAT(pipe->pipe.ep) << 7));
466 int toggle = GET_LOWFLAT(pipe->toggle) ? TD_TOKEN_TOGGLE : 0;
Kevin O'Connor6152c322012-03-10 08:53:58 -0500467
Kevin O'Connor6f86b892014-12-31 01:42:38 -0500468 // Allocate 16 tds on stack (16byte aligned)
Kevin O'Connor6152c322012-03-10 08:53:58 -0500469 u8 tdsbuf[sizeof(struct uhci_td) * STACKTDS + TDALIGN - 1];
470 struct uhci_td *tds = (void*)ALIGN((u32)tdsbuf, TDALIGN);
471 memset(tds, 0, sizeof(*tds) * STACKTDS);
Kevin O'Connoreb9f3ae2014-12-31 01:56:53 -0500472 int tdpos = 0;
Kevin O'Connor6152c322012-03-10 08:53:58 -0500473
474 // Enable tds
Kevin O'Connorb53fe2c2014-06-14 12:50:20 -0400475 u32 end = timer_calc(usb_xfer_time(p, datasize));
Kevin O'Connor6152c322012-03-10 08:53:58 -0500476 barrier();
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400477 SET_LOWFLAT(pipe->qh.element, (u32)MAKE_FLATPTR(GET_SEG(SS), tds));
Kevin O'Connor6152c322012-03-10 08:53:58 -0500478
Kevin O'Connoreb9f3ae2014-12-31 01:56:53 -0500479 // Setup transfer descriptors
480 if (cmd) {
481 // Send setup pid on control transfers
482 struct uhci_td *td = &tds[tdpos++ % STACKTDS];
483 u32 nexttd = (u32)MAKE_FLATPTR(GET_SEG(SS), &tds[tdpos % STACKTDS]);
484 td->link = nexttd | UHCI_PTR_DEPTH;
Kevin O'Connor2891a832014-12-31 17:41:14 -0500485 td->token = (uhci_explen(USB_CONTROL_SETUP_SIZE)
486 | (devaddr << TD_TOKEN_DEVADDR_SHIFT) | USB_PID_SETUP);
Kevin O'Connoreb9f3ae2014-12-31 01:56:53 -0500487 td->buffer = (void*)cmd;
488 barrier();
489 td->status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
490 | TD_CTRL_ACTIVE);
491 toggle = TD_TOKEN_TOGGLE;
492 }
Kevin O'Connor6152c322012-03-10 08:53:58 -0500493 while (datasize) {
Kevin O'Connoreb9f3ae2014-12-31 01:56:53 -0500494 // Send data pids
Kevin O'Connor6152c322012-03-10 08:53:58 -0500495 struct uhci_td *td = &tds[tdpos++ % STACKTDS];
Kevin O'Connorb53fe2c2014-06-14 12:50:20 -0400496 int ret = wait_td(td, end);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500497 if (ret)
498 goto fail;
499
500 int transfer = datasize;
501 if (transfer > maxpacket)
502 transfer = maxpacket;
Kevin O'Connor1fdc26e2014-12-31 01:40:07 -0500503 u32 nexttd = (u32)MAKE_FLATPTR(GET_SEG(SS), &tds[tdpos % STACKTDS]);
Kevin O'Connoreb9f3ae2014-12-31 01:56:53 -0500504 td->link = ((transfer==datasize && !cmd)
Kevin O'Connor1fdc26e2014-12-31 01:40:07 -0500505 ? UHCI_PTR_TERM : (nexttd | UHCI_PTR_DEPTH));
Kevin O'Connor6152c322012-03-10 08:53:58 -0500506 td->token = (uhci_explen(transfer) | toggle
507 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
508 | (dir ? USB_PID_IN : USB_PID_OUT));
509 td->buffer = data;
510 barrier();
511 td->status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
512 | TD_CTRL_ACTIVE);
513 toggle ^= TD_TOKEN_TOGGLE;
514
515 data += transfer;
516 datasize -= transfer;
517 }
Kevin O'Connoreb9f3ae2014-12-31 01:56:53 -0500518 if (cmd) {
519 // Send status pid on control transfers
520 struct uhci_td *td = &tds[tdpos++ % STACKTDS];
521 int ret = wait_td(td, end);
522 if (ret)
523 goto fail;
524 td->link = UHCI_PTR_TERM;
525 td->token = (uhci_explen(0) | TD_TOKEN_TOGGLE
526 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
527 | (dir ? USB_PID_OUT : USB_PID_IN));
528 td->buffer = 0;
529 barrier();
530 td->status = (uhci_maxerr(0) | (lowspeed ? TD_CTRL_LS : 0)
531 | TD_CTRL_ACTIVE);
532 }
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400533 SET_LOWFLAT(pipe->toggle, !!toggle);
Kevin O'Connorb53fe2c2014-06-14 12:50:20 -0400534 return wait_pipe(pipe, end);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500535fail:
536 dprintf(1, "uhci_send_bulk failed\n");
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400537 SET_LOWFLAT(pipe->qh.element, UHCI_PTR_TERM);
538 uhci_waittick(GET_LOWFLAT(pipe->iobase));
Kevin O'Connor6152c322012-03-10 08:53:58 -0500539 return -1;
540}
541
Kevin O'Connor114592f2009-09-28 21:32:08 -0400542int
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500543uhci_poll_intr(struct usb_pipe *p, void *data)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400544{
545 ASSERT16();
546 if (! CONFIG_USB_UHCI)
547 return -1;
548
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500549 struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400550 struct uhci_td *td = GET_LOWFLAT(pipe->next_td);
551 u32 status = GET_LOWFLAT(td->status);
552 u32 token = GET_LOWFLAT(td->token);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400553 if (status & TD_CTRL_ACTIVE)
554 // No intrs found.
555 return -1;
556 // XXX - check for errors.
557
558 // Copy data.
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400559 void *tddata = GET_LOWFLAT(td->buffer);
560 memcpy_far(GET_SEG(SS), data, SEG_LOW, LOWFLAT2LOW(tddata)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400561 , uhci_expected_length(token));
562
563 // Reenable this td.
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400564 struct uhci_td *next = (void*)(GET_LOWFLAT(td->link) & ~UHCI_PTR_BITS);
565 SET_LOWFLAT(pipe->next_td, next);
Kevin O'Connor95714392010-02-17 22:59:53 -0500566 barrier();
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400567 SET_LOWFLAT(td->status, (uhci_maxerr(0) | (status & TD_CTRL_LS)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400568 | TD_CTRL_ACTIVE));
Kevin O'Connor114592f2009-09-28 21:32:08 -0400569
570 return 0;
571}