blob: 7a11510c73010e9d10f6a3583c17b5ad68324160 [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
11#include "pci.h" // pci_bdf_to_bus
Kevin O'Connorec443ff2013-12-05 18:43:20 -050012#include "pci_ids.h" // PCI_CLASS_SERIAL_USB_UHCI
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040013#include "pci_regs.h" // PCI_BASE_ADDRESS_4
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040014#include "string.h" // memset
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040015#include "usb.h" // struct usb_s
Kevin O'Connor32a2b0e2016-02-02 14:28:13 -050016#include "usb-ehci.h" // ehci_wait_controllers
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040017#include "usb-uhci.h" // USBLEGSUP
18#include "util.h" // msleep
Kevin O'Connor4ade5232013-09-18 21:41:48 -040019#include "x86.h" // outw
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050020
Kevin O'Connor406fad62010-02-28 02:23:19 -050021struct usb_uhci_s {
22 struct usb_s usb;
23 u16 iobase;
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -050024 struct uhci_qh *control_qh;
Kevin O'Connor406fad62010-02-28 02:23:19 -050025 struct uhci_framelist *framelist;
26};
27
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -050028struct uhci_pipe {
29 struct uhci_qh qh;
30 struct uhci_td *next_td;
31 struct usb_pipe pipe;
32 u16 iobase;
33 u8 toggle;
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -050034};
35
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050036
37/****************************************************************
38 * Root hub
39 ****************************************************************/
40
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040041// Check if device attached to a given port
42static int
43uhci_hub_detect(struct usbhub_s *hub, u32 port)
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050044{
Kevin O'Connor406fad62010-02-28 02:23:19 -050045 struct usb_uhci_s *cntl = container_of(hub->cntl, struct usb_uhci_s, usb);
46 u16 ioport = cntl->iobase + USBPORTSC1 + port * 2;
Kevin O'Connor0f681302014-09-10 11:33:01 -040047 u16 status = inw(ioport);
48 if (!(status & USBPORTSC_CCS))
49 // No device found.
50 return 0;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050051
52 // XXX - if just powered up, need to wait for USB_TIME_ATTDB?
53
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040054 // Begin reset on port
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050055 outw(USBPORTSC_PR, ioport);
56 msleep(USB_TIME_DRSTR);
Kevin O'Connor0f681302014-09-10 11:33:01 -040057 return 1;
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040058}
59
60// Reset device on port
61static int
62uhci_hub_reset(struct usbhub_s *hub, u32 port)
63{
64 struct usb_uhci_s *cntl = container_of(hub->cntl, struct usb_uhci_s, usb);
65 u16 ioport = cntl->iobase + USBPORTSC1 + port * 2;
66
67 // Finish reset on port
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050068 outw(0, ioport);
69 udelay(6); // 64 high-speed bit times
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040070 u16 status = inw(ioport);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050071 if (!(status & USBPORTSC_CCS))
72 // No longer connected
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040073 return -1;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050074 outw(USBPORTSC_PE, ioport);
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040075 return !!(status & USBPORTSC_LSDA);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050076}
77
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040078// Disable port
79static void
80uhci_hub_disconnect(struct usbhub_s *hub, u32 port)
81{
82 struct usb_uhci_s *cntl = container_of(hub->cntl, struct usb_uhci_s, usb);
83 u16 ioport = cntl->iobase + USBPORTSC1 + port * 2;
84 outw(0, ioport);
85}
86
87static struct usbhub_op_s uhci_HubOp = {
88 .detect = uhci_hub_detect,
89 .reset = uhci_hub_reset,
90 .disconnect = uhci_hub_disconnect,
91};
92
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050093// Find any devices connected to the root hub.
94static int
Kevin O'Connor406fad62010-02-28 02:23:19 -050095check_uhci_ports(struct usb_uhci_s *cntl)
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050096{
97 ASSERT32FLAT();
Kevin O'Connor32a2b0e2016-02-02 14:28:13 -050098 // Wait for ehci init - in case this is a "companion controller"
99 ehci_wait_controllers();
100
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500101 struct usbhub_s hub;
102 memset(&hub, 0, sizeof(hub));
Kevin O'Connor406fad62010-02-28 02:23:19 -0500103 hub.cntl = &cntl->usb;
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400104 hub.portcount = 2;
105 hub.op = &uhci_HubOp;
106 usb_enumerate(&hub);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500107 return hub.devcount;
108}
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500109
110
111/****************************************************************
112 * Setup
113 ****************************************************************/
Kevin O'Connor114592f2009-09-28 21:32:08 -0400114
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500115// Wait for next USB frame to start - for ensuring safe memory release.
116static void
117uhci_waittick(u16 iobase)
118{
119 barrier();
120 u16 startframe = inw(iobase + USBFRNUM);
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400121 u32 end = timer_calc(1000 * 5);
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500122 for (;;) {
123 if (inw(iobase + USBFRNUM) != startframe)
124 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400125 if (timer_check(end)) {
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500126 warn_timeout();
127 return;
128 }
129 yield();
130 }
131}
132
133static void
134uhci_free_pipes(struct usb_uhci_s *cntl)
135{
136 dprintf(7, "uhci_free_pipes %p\n", cntl);
137
138 struct uhci_qh *pos = (void*)(cntl->framelist->links[0] & ~UHCI_PTR_BITS);
139 for (;;) {
140 u32 link = pos->link;
141 if (link == UHCI_PTR_TERM)
142 break;
143 struct uhci_qh *next = (void*)(link & ~UHCI_PTR_BITS);
144 struct uhci_pipe *pipe = container_of(next, struct uhci_pipe, qh);
Kevin O'Connor2bfd1702014-10-16 12:31:42 -0400145 if (usb_is_freelist(&cntl->usb, &pipe->pipe))
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500146 pos->link = next->link;
147 else
148 pos = next;
149 }
150 uhci_waittick(cntl->iobase);
151 for (;;) {
152 struct usb_pipe *usbpipe = cntl->usb.freelist;
153 if (!usbpipe)
154 break;
155 cntl->usb.freelist = usbpipe->freenext;
156 struct uhci_pipe *pipe = container_of(usbpipe, struct uhci_pipe, pipe);
157 free(pipe);
158 }
159}
160
Kevin O'Connor114592f2009-09-28 21:32:08 -0400161static void
Kevin O'Connor406fad62010-02-28 02:23:19 -0500162reset_uhci(struct usb_uhci_s *cntl, u16 bdf)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400163{
164 // XXX - don't reset if not needed.
165
166 // Reset PIRQ and SMI
Kevin O'Connor406fad62010-02-28 02:23:19 -0500167 pci_config_writew(bdf, USBLEGSUP, USBLEGSUP_RWC);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400168
169 // Reset the HC
Kevin O'Connor406fad62010-02-28 02:23:19 -0500170 outw(USBCMD_HCRESET, cntl->iobase + USBCMD);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400171 udelay(5);
172
173 // Disable interrupts and commands (just to be safe).
Kevin O'Connor406fad62010-02-28 02:23:19 -0500174 outw(0, cntl->iobase + USBINTR);
175 outw(0, cntl->iobase + USBCMD);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400176}
177
178static void
Kevin O'Connor406fad62010-02-28 02:23:19 -0500179configure_uhci(void *data)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400180{
Kevin O'Connor406fad62010-02-28 02:23:19 -0500181 struct usb_uhci_s *cntl = data;
182
Kevin O'Connor114592f2009-09-28 21:32:08 -0400183 // Allocate ram for schedule storage
184 struct uhci_td *term_td = malloc_high(sizeof(*term_td));
185 struct uhci_framelist *fl = memalign_high(sizeof(*fl), sizeof(*fl));
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500186 struct uhci_pipe *intr_pipe = malloc_high(sizeof(*intr_pipe));
187 struct uhci_pipe *term_pipe = malloc_high(sizeof(*term_pipe));
188 if (!term_td || !fl || !intr_pipe || !term_pipe) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500189 warn_noalloc();
Kevin O'Connor406fad62010-02-28 02:23:19 -0500190 goto fail;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400191 }
192
193 // Work around for PIIX errata
194 memset(term_td, 0, sizeof(*term_td));
195 term_td->link = UHCI_PTR_TERM;
196 term_td->token = (uhci_explen(0) | (0x7f << TD_TOKEN_DEVADDR_SHIFT)
197 | USB_PID_IN);
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500198 memset(term_pipe, 0, sizeof(*term_pipe));
199 term_pipe->qh.element = (u32)term_td;
200 term_pipe->qh.link = UHCI_PTR_TERM;
201 term_pipe->pipe.cntl = &cntl->usb;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400202
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500203 // Set schedule to point to primary intr queue head
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500204 memset(intr_pipe, 0, sizeof(*intr_pipe));
205 intr_pipe->qh.element = UHCI_PTR_TERM;
206 intr_pipe->qh.link = (u32)&term_pipe->qh | UHCI_PTR_QH;
207 intr_pipe->pipe.cntl = &cntl->usb;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400208 int i;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500209 for (i=0; i<ARRAY_SIZE(fl->links); i++)
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500210 fl->links[i] = (u32)&intr_pipe->qh | UHCI_PTR_QH;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500211 cntl->framelist = fl;
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500212 cntl->control_qh = &intr_pipe->qh;
Kevin O'Connor95714392010-02-17 22:59:53 -0500213 barrier();
Kevin O'Connor114592f2009-09-28 21:32:08 -0400214
215 // Set the frame length to the default: 1 ms exactly
Kevin O'Connor406fad62010-02-28 02:23:19 -0500216 outb(USBSOF_DEFAULT, cntl->iobase + USBSOF);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400217
218 // Store the frame list base address
Kevin O'Connor406fad62010-02-28 02:23:19 -0500219 outl((u32)fl->links, cntl->iobase + USBFLBASEADD);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400220
221 // Set the current frame number
Kevin O'Connor406fad62010-02-28 02:23:19 -0500222 outw(0, cntl->iobase + USBFRNUM);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400223
Kevin O'Connor114592f2009-09-28 21:32:08 -0400224 // Mark as configured and running with a 64-byte max packet.
Kevin O'Connor406fad62010-02-28 02:23:19 -0500225 outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, cntl->iobase + USBCMD);
226
227 // Find devices
228 int count = check_uhci_ports(cntl);
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500229 uhci_free_pipes(cntl);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500230 if (count)
231 // Success
232 return;
233
234 // No devices found - shutdown and free controller.
235 outw(0, cntl->iobase + USBCMD);
236fail:
237 free(term_td);
238 free(fl);
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500239 free(intr_pipe);
240 free(term_pipe);
Kevin O'Connor0770d672010-03-09 19:33:22 -0500241 free(cntl);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400242}
243
Kevin O'Connorec443ff2013-12-05 18:43:20 -0500244static void
245uhci_controller_setup(struct pci_device *pci)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400246{
Kevin O'Connor8ff8e012011-07-09 14:11:21 -0400247 u16 bdf = pci->bdf;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500248 struct usb_uhci_s *cntl = malloc_tmphigh(sizeof(*cntl));
Kevin O'Connorffdcd3a2011-07-10 15:48:00 -0400249 if (!cntl) {
250 warn_noalloc();
251 return;
252 }
Kevin O'Connor406fad62010-02-28 02:23:19 -0500253 memset(cntl, 0, sizeof(*cntl));
Kevin O'Connor8ff8e012011-07-09 14:11:21 -0400254 cntl->usb.pci = pci;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500255 cntl->usb.type = USB_TYPE_UHCI;
256 cntl->iobase = (pci_config_readl(bdf, PCI_BASE_ADDRESS_4)
257 & PCI_BASE_ADDRESS_IO_MASK);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400258
Kevin O'Connor9dc243e2010-03-20 17:53:03 -0400259 dprintf(1, "UHCI init on dev %02x:%02x.%x (io=%x)\n"
Kevin O'Connor406fad62010-02-28 02:23:19 -0500260 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf)
261 , pci_bdf_to_fn(bdf), cntl->iobase);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400262
Kevin O'Connor406fad62010-02-28 02:23:19 -0500263 pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400264
Kevin O'Connor406fad62010-02-28 02:23:19 -0500265 reset_uhci(cntl, bdf);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400266
Kevin O'Connor406fad62010-02-28 02:23:19 -0500267 run_thread(configure_uhci, cntl);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400268}
269
Kevin O'Connorec443ff2013-12-05 18:43:20 -0500270void
271uhci_setup(void)
272{
273 if (! CONFIG_USB_UHCI)
274 return;
275 struct pci_device *pci;
276 foreachpci(pci) {
277 if (pci_classprog(pci) == PCI_CLASS_SERIAL_USB_UHCI)
278 uhci_controller_setup(pci);
279 }
280}
281
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500282
283/****************************************************************
284 * End point communication
285 ****************************************************************/
286
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500287static struct usb_pipe *
Kevin O'Connoreb50eae2012-03-10 08:48:31 -0500288uhci_alloc_intr_pipe(struct usbdevice_s *usbdev
289 , struct usb_endpoint_descriptor *epdesc)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400290{
Kevin O'Connor406fad62010-02-28 02:23:19 -0500291 struct usb_uhci_s *cntl = container_of(
Kevin O'Connoreb50eae2012-03-10 08:48:31 -0500292 usbdev->hub->cntl, struct usb_uhci_s, usb);
Kevin O'Connor98cdad32014-10-16 12:05:09 -0400293 int frameexp = usb_get_period(usbdev, epdesc);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500294 dprintf(7, "uhci_alloc_intr_pipe %p %d\n", &cntl->usb, frameexp);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400295
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500296 if (frameexp > 10)
297 frameexp = 10;
Kevin O'Connoreb50eae2012-03-10 08:48:31 -0500298 int maxpacket = epdesc->wMaxPacketSize;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500299 // Determine number of entries needed for 2 timer ticks.
300 int ms = 1<<frameexp;
Kevin O'Connor69013372013-07-20 12:08:48 -0400301 int count = DIV_ROUND_UP(ticks_to_ms(2), ms);
Kevin O'Connor0770d672010-03-09 19:33:22 -0500302 count = ALIGN(count, 2);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500303 struct uhci_pipe *pipe = malloc_low(sizeof(*pipe));
Kevin O'Connor114592f2009-09-28 21:32:08 -0400304 struct uhci_td *tds = malloc_low(sizeof(*tds) * count);
Kevin O'Connor09e2f7c2010-02-27 23:50:48 -0500305 void *data = malloc_low(maxpacket * count);
306 if (!pipe || !tds || !data) {
Kevin O'Connor95714392010-02-17 22:59:53 -0500307 warn_noalloc();
308 goto fail;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500309 }
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500310 memset(pipe, 0, sizeof(*pipe));
Kevin O'Connoreb50eae2012-03-10 08:48:31 -0500311 usb_desc2pipe(&pipe->pipe, usbdev, epdesc);
312 int lowspeed = pipe->pipe.speed;
313 int devaddr = pipe->pipe.devaddr | (pipe->pipe.ep << 7);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500314 pipe->qh.element = (u32)tds;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500315 pipe->next_td = &tds[0];
Kevin O'Connor406fad62010-02-28 02:23:19 -0500316 pipe->iobase = cntl->iobase;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500317
Kevin O'Connor114592f2009-09-28 21:32:08 -0400318 int toggle = 0;
319 int i;
320 for (i=0; i<count; i++) {
321 tds[i].link = (i==count-1 ? (u32)&tds[0] : (u32)&tds[i+1]);
322 tds[i].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
323 | TD_CTRL_ACTIVE);
324 tds[i].token = (uhci_explen(maxpacket) | toggle
325 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
326 | USB_PID_IN);
Kevin O'Connor09e2f7c2010-02-27 23:50:48 -0500327 tds[i].buffer = data + maxpacket * i;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400328 toggle ^= TD_TOKEN_TOGGLE;
329 }
330
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500331 // Add to interrupt schedule.
Kevin O'Connor406fad62010-02-28 02:23:19 -0500332 struct uhci_framelist *fl = cntl->framelist;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500333 if (frameexp == 0) {
334 // Add to existing interrupt entry.
335 struct uhci_qh *intr_qh = (void*)(fl->links[0] & ~UHCI_PTR_BITS);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500336 pipe->qh.link = intr_qh->link;
Kevin O'Connor95714392010-02-17 22:59:53 -0500337 barrier();
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500338 intr_qh->link = (u32)&pipe->qh | UHCI_PTR_QH;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500339 if (cntl->control_qh == intr_qh)
340 cntl->control_qh = &pipe->qh;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500341 } else {
342 int startpos = 1<<(frameexp-1);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500343 pipe->qh.link = fl->links[startpos];
Kevin O'Connor95714392010-02-17 22:59:53 -0500344 barrier();
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500345 for (i=startpos; i<ARRAY_SIZE(fl->links); i+=ms)
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500346 fl->links[i] = (u32)&pipe->qh | UHCI_PTR_QH;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500347 }
Kevin O'Connor114592f2009-09-28 21:32:08 -0400348
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500349 return &pipe->pipe;
Kevin O'Connor95714392010-02-17 22:59:53 -0500350fail:
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500351 free(pipe);
Kevin O'Connor95714392010-02-17 22:59:53 -0500352 free(tds);
Kevin O'Connor09e2f7c2010-02-27 23:50:48 -0500353 free(data);
Kevin O'Connor95714392010-02-17 22:59:53 -0500354 return NULL;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400355}
356
Kevin O'Connor6152c322012-03-10 08:53:58 -0500357struct usb_pipe *
Kevin O'Connor89452cc2014-10-16 13:33:35 -0400358uhci_realloc_pipe(struct usbdevice_s *usbdev, struct usb_pipe *upipe
359 , struct usb_endpoint_descriptor *epdesc)
Kevin O'Connor6152c322012-03-10 08:53:58 -0500360{
361 if (! CONFIG_USB_UHCI)
362 return NULL;
Kevin O'Connor89452cc2014-10-16 13:33:35 -0400363 usb_add_freelist(upipe);
364 if (!epdesc)
365 return NULL;
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500366 u8 eptype = epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
367 if (eptype == USB_ENDPOINT_XFER_INT)
368 return uhci_alloc_intr_pipe(usbdev, epdesc);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500369 struct usb_uhci_s *cntl = container_of(
370 usbdev->hub->cntl, struct usb_uhci_s, usb);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500371 dprintf(7, "uhci_alloc_async_pipe %p %d\n", &cntl->usb, eptype);
372
Kevin O'Connor2bfd1702014-10-16 12:31:42 -0400373 struct usb_pipe *usbpipe = usb_get_freelist(&cntl->usb, eptype);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500374 if (usbpipe) {
375 // Use previously allocated pipe.
376 usb_desc2pipe(usbpipe, usbdev, epdesc);
377 return usbpipe;
378 }
379
380 // Allocate a new queue head.
381 struct uhci_pipe *pipe;
382 if (eptype == USB_ENDPOINT_XFER_CONTROL)
383 pipe = malloc_tmphigh(sizeof(*pipe));
384 else
385 pipe = malloc_low(sizeof(*pipe));
386 if (!pipe) {
387 warn_noalloc();
388 return NULL;
389 }
390 memset(pipe, 0, sizeof(*pipe));
391 usb_desc2pipe(&pipe->pipe, usbdev, epdesc);
392 pipe->qh.element = UHCI_PTR_TERM;
393 pipe->iobase = cntl->iobase;
394
395 // Add queue head to controller list.
396 struct uhci_qh *control_qh = cntl->control_qh;
397 pipe->qh.link = control_qh->link;
398 barrier();
399 control_qh->link = (u32)&pipe->qh | UHCI_PTR_QH;
400 if (eptype == USB_ENDPOINT_XFER_CONTROL)
401 cntl->control_qh = &pipe->qh;
402 return &pipe->pipe;
403}
404
405static int
Kevin O'Connorb53fe2c2014-06-14 12:50:20 -0400406wait_pipe(struct uhci_pipe *pipe, u32 end)
Kevin O'Connor6152c322012-03-10 08:53:58 -0500407{
Kevin O'Connor6152c322012-03-10 08:53:58 -0500408 for (;;) {
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400409 u32 el_link = GET_LOWFLAT(pipe->qh.element);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500410 if (el_link & UHCI_PTR_TERM)
411 return 0;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400412 if (timer_check(end)) {
Kevin O'Connor6152c322012-03-10 08:53:58 -0500413 warn_timeout();
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400414 u16 iobase = GET_LOWFLAT(pipe->iobase);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500415 struct uhci_td *td = (void*)(el_link & ~UHCI_PTR_BITS);
416 dprintf(1, "Timeout on wait_pipe %p (td=%p s=%x c=%x/%x)\n"
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400417 , pipe, (void*)el_link, GET_LOWFLAT(td->status)
Kevin O'Connor6152c322012-03-10 08:53:58 -0500418 , inw(iobase + USBCMD)
419 , inw(iobase + USBSTS));
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400420 SET_LOWFLAT(pipe->qh.element, UHCI_PTR_TERM);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500421 uhci_waittick(iobase);
422 return -1;
423 }
424 yield();
425 }
426}
427
428static int
Kevin O'Connorb53fe2c2014-06-14 12:50:20 -0400429wait_td(struct uhci_td *td, u32 end)
Kevin O'Connor6152c322012-03-10 08:53:58 -0500430{
Kevin O'Connor6152c322012-03-10 08:53:58 -0500431 u32 status;
432 for (;;) {
433 status = td->status;
434 if (!(status & TD_CTRL_ACTIVE))
435 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400436 if (timer_check(end)) {
Kevin O'Connor6152c322012-03-10 08:53:58 -0500437 warn_timeout();
438 return -1;
439 }
440 yield();
441 }
442 if (status & TD_CTRL_ANY_ERROR) {
443 dprintf(1, "wait_td error - status=%x\n", status);
444 return -2;
445 }
446 return 0;
447}
448
Kevin O'Connor6f86b892014-12-31 01:42:38 -0500449#define STACKTDS 16
Kevin O'Connor6152c322012-03-10 08:53:58 -0500450#define TDALIGN 16
451
452int
Kevin O'Connor2891a832014-12-31 17:41:14 -0500453uhci_send_pipe(struct usb_pipe *p, int dir, const void *cmd
Kevin O'Connoreb9f3ae2014-12-31 01:56:53 -0500454 , void *data, int datasize)
Kevin O'Connor6152c322012-03-10 08:53:58 -0500455{
456 if (! CONFIG_USB_UHCI)
457 return -1;
458 struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
Kevin O'Connoreb9f3ae2014-12-31 01:56:53 -0500459 dprintf(7, "uhci_send_pipe qh=%p dir=%d data=%p size=%d\n"
Kevin O'Connor6152c322012-03-10 08:53:58 -0500460 , &pipe->qh, dir, data, datasize);
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400461 int maxpacket = GET_LOWFLAT(pipe->pipe.maxpacket);
462 int lowspeed = GET_LOWFLAT(pipe->pipe.speed);
463 int devaddr = (GET_LOWFLAT(pipe->pipe.devaddr)
464 | (GET_LOWFLAT(pipe->pipe.ep) << 7));
465 int toggle = GET_LOWFLAT(pipe->toggle) ? TD_TOKEN_TOGGLE : 0;
Kevin O'Connor6152c322012-03-10 08:53:58 -0500466
Kevin O'Connor6f86b892014-12-31 01:42:38 -0500467 // Allocate 16 tds on stack (16byte aligned)
Kevin O'Connor6152c322012-03-10 08:53:58 -0500468 u8 tdsbuf[sizeof(struct uhci_td) * STACKTDS + TDALIGN - 1];
469 struct uhci_td *tds = (void*)ALIGN((u32)tdsbuf, TDALIGN);
470 memset(tds, 0, sizeof(*tds) * STACKTDS);
Kevin O'Connoreb9f3ae2014-12-31 01:56:53 -0500471 int tdpos = 0;
Kevin O'Connor6152c322012-03-10 08:53:58 -0500472
473 // Enable tds
Kevin O'Connorb53fe2c2014-06-14 12:50:20 -0400474 u32 end = timer_calc(usb_xfer_time(p, datasize));
Kevin O'Connor6152c322012-03-10 08:53:58 -0500475 barrier();
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400476 SET_LOWFLAT(pipe->qh.element, (u32)MAKE_FLATPTR(GET_SEG(SS), tds));
Kevin O'Connor6152c322012-03-10 08:53:58 -0500477
Kevin O'Connoreb9f3ae2014-12-31 01:56:53 -0500478 // Setup transfer descriptors
479 if (cmd) {
480 // Send setup pid on control transfers
481 struct uhci_td *td = &tds[tdpos++ % STACKTDS];
482 u32 nexttd = (u32)MAKE_FLATPTR(GET_SEG(SS), &tds[tdpos % STACKTDS]);
483 td->link = nexttd | UHCI_PTR_DEPTH;
Kevin O'Connor2891a832014-12-31 17:41:14 -0500484 td->token = (uhci_explen(USB_CONTROL_SETUP_SIZE)
485 | (devaddr << TD_TOKEN_DEVADDR_SHIFT) | USB_PID_SETUP);
Kevin O'Connoreb9f3ae2014-12-31 01:56:53 -0500486 td->buffer = (void*)cmd;
487 barrier();
488 td->status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
489 | TD_CTRL_ACTIVE);
490 toggle = TD_TOKEN_TOGGLE;
491 }
Kevin O'Connor6152c322012-03-10 08:53:58 -0500492 while (datasize) {
Kevin O'Connoreb9f3ae2014-12-31 01:56:53 -0500493 // Send data pids
Kevin O'Connor6152c322012-03-10 08:53:58 -0500494 struct uhci_td *td = &tds[tdpos++ % STACKTDS];
Kevin O'Connorb53fe2c2014-06-14 12:50:20 -0400495 int ret = wait_td(td, end);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500496 if (ret)
497 goto fail;
498
499 int transfer = datasize;
500 if (transfer > maxpacket)
501 transfer = maxpacket;
Kevin O'Connor1fdc26e2014-12-31 01:40:07 -0500502 u32 nexttd = (u32)MAKE_FLATPTR(GET_SEG(SS), &tds[tdpos % STACKTDS]);
Kevin O'Connoreb9f3ae2014-12-31 01:56:53 -0500503 td->link = ((transfer==datasize && !cmd)
Kevin O'Connor1fdc26e2014-12-31 01:40:07 -0500504 ? UHCI_PTR_TERM : (nexttd | UHCI_PTR_DEPTH));
Kevin O'Connor6152c322012-03-10 08:53:58 -0500505 td->token = (uhci_explen(transfer) | toggle
506 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
507 | (dir ? USB_PID_IN : USB_PID_OUT));
508 td->buffer = data;
509 barrier();
510 td->status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
511 | TD_CTRL_ACTIVE);
512 toggle ^= TD_TOKEN_TOGGLE;
513
514 data += transfer;
515 datasize -= transfer;
516 }
Kevin O'Connoreb9f3ae2014-12-31 01:56:53 -0500517 if (cmd) {
518 // Send status pid on control transfers
519 struct uhci_td *td = &tds[tdpos++ % STACKTDS];
520 int ret = wait_td(td, end);
521 if (ret)
522 goto fail;
523 td->link = UHCI_PTR_TERM;
524 td->token = (uhci_explen(0) | TD_TOKEN_TOGGLE
525 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
526 | (dir ? USB_PID_OUT : USB_PID_IN));
527 td->buffer = 0;
528 barrier();
529 td->status = (uhci_maxerr(0) | (lowspeed ? TD_CTRL_LS : 0)
530 | TD_CTRL_ACTIVE);
531 }
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400532 SET_LOWFLAT(pipe->toggle, !!toggle);
Kevin O'Connorb53fe2c2014-06-14 12:50:20 -0400533 return wait_pipe(pipe, end);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500534fail:
535 dprintf(1, "uhci_send_bulk failed\n");
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400536 SET_LOWFLAT(pipe->qh.element, UHCI_PTR_TERM);
537 uhci_waittick(GET_LOWFLAT(pipe->iobase));
Kevin O'Connor6152c322012-03-10 08:53:58 -0500538 return -1;
539}
540
Kevin O'Connor114592f2009-09-28 21:32:08 -0400541int
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500542uhci_poll_intr(struct usb_pipe *p, void *data)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400543{
544 ASSERT16();
545 if (! CONFIG_USB_UHCI)
546 return -1;
547
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500548 struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400549 struct uhci_td *td = GET_LOWFLAT(pipe->next_td);
550 u32 status = GET_LOWFLAT(td->status);
551 u32 token = GET_LOWFLAT(td->token);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400552 if (status & TD_CTRL_ACTIVE)
553 // No intrs found.
554 return -1;
555 // XXX - check for errors.
556
557 // Copy data.
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400558 void *tddata = GET_LOWFLAT(td->buffer);
559 memcpy_far(GET_SEG(SS), data, SEG_LOW, LOWFLAT2LOW(tddata)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400560 , uhci_expected_length(token));
561
562 // Reenable this td.
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400563 struct uhci_td *next = (void*)(GET_LOWFLAT(td->link) & ~UHCI_PTR_BITS);
564 SET_LOWFLAT(pipe->next_td, next);
Kevin O'Connor95714392010-02-17 22:59:53 -0500565 barrier();
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400566 SET_LOWFLAT(td->status, (uhci_maxerr(0) | (status & TD_CTRL_LS)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400567 | TD_CTRL_ACTIVE));
Kevin O'Connor114592f2009-09-28 21:32:08 -0400568
569 return 0;
570}