blob: 57ce5a8c6d58dde2f1ca83f70409edacc17c840f [file] [log] [blame]
Kevin O'Connor190cc622010-03-09 19:43:52 -05001// Code for handling EHCI USB controllers.
2//
Kevin O'Connorec443ff2013-12-05 18:43:20 -05003// Copyright (C) 2010-2013 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connor190cc622010-03-09 19:43:52 -05004//
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'Connor190cc622010-03-09 19:43:52 -05008#include "config.h" // CONFIG_*
Kevin O'Connor2d2fa312013-09-14 21:55:26 -04009#include "output.h" // dprintf
10#include "malloc.h" // free
Kevin O'Connor1bd976b2015-01-01 12:51:04 -050011#include "memmap.h" // PAGE_SIZE
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040012#include "pci.h" // pci_bdf_to_bus
Kevin O'Connor190cc622010-03-09 19:43:52 -050013#include "pci_ids.h" // PCI_CLASS_SERIAL_USB_UHCI
14#include "pci_regs.h" // PCI_BASE_ADDRESS_0
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
17#include "usb-ehci.h" // struct ehci_qh
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040018#include "util.h" // msleep
Kevin O'Connorb9c6a962013-09-14 13:01:30 -040019#include "x86.h" // readl
Kevin O'Connor190cc622010-03-09 19:43:52 -050020
Kevin O'Connor190cc622010-03-09 19:43:52 -050021struct usb_ehci_s {
22 struct usb_s usb;
23 struct ehci_caps *caps;
24 struct ehci_regs *regs;
25 struct ehci_qh *async_qh;
Kevin O'Connor190cc622010-03-09 19:43:52 -050026 int checkports;
Kevin O'Connor190cc622010-03-09 19:43:52 -050027};
28
Kevin O'Connor402f9a02012-02-20 12:54:49 -050029struct ehci_pipe {
30 struct ehci_qh qh;
31 struct ehci_qtd *next_td, *tds;
32 void *data;
33 struct usb_pipe pipe;
Kevin O'Connor402f9a02012-02-20 12:54:49 -050034};
35
Kevin O'Connore2d6fdd2014-09-10 11:04:26 -040036static int PendingEHCI;
Kevin O'Connorec443ff2013-12-05 18:43:20 -050037
Kevin O'Connor190cc622010-03-09 19:43:52 -050038
39/****************************************************************
40 * Root hub
41 ****************************************************************/
42
43#define EHCI_TIME_POSTPOWER 20
44#define EHCI_TIME_POSTRESET 2
45
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040046// Check if device attached to port
47static int
48ehci_hub_detect(struct usbhub_s *hub, u32 port)
Kevin O'Connor190cc622010-03-09 19:43:52 -050049{
Kevin O'Connor190cc622010-03-09 19:43:52 -050050 struct usb_ehci_s *cntl = container_of(hub->cntl, struct usb_ehci_s, usb);
Kevin O'Connor190cc622010-03-09 19:43:52 -050051 u32 *portreg = &cntl->regs->portsc[port];
52 u32 portsc = readl(portreg);
53
Kevin O'Connor190cc622010-03-09 19:43:52 -050054 if (!(portsc & PORT_CONNECT))
55 // No device present
Kevin O'Connor0f681302014-09-10 11:33:01 -040056 return 0;
Kevin O'Connor190cc622010-03-09 19:43:52 -050057
58 if ((portsc & PORT_LINESTATUS_MASK) == PORT_LINESTATUS_KSTATE) {
59 // low speed device
Kevin O'Connor190cc622010-03-09 19:43:52 -050060 writel(portreg, portsc | PORT_OWNER);
Kevin O'Connore2d6fdd2014-09-10 11:04:26 -040061 return -1;
Kevin O'Connor190cc622010-03-09 19:43:52 -050062 }
63
64 // XXX - if just powered up, need to wait for USB_TIME_ATTDB?
65
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040066 // Begin reset on port
Kevin O'Connor190cc622010-03-09 19:43:52 -050067 portsc = (portsc & ~PORT_PE) | PORT_RESET;
68 writel(portreg, portsc);
69 msleep(USB_TIME_DRSTR);
Kevin O'Connor0f681302014-09-10 11:33:01 -040070 return 1;
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040071}
72
73// Reset device on port
74static int
75ehci_hub_reset(struct usbhub_s *hub, u32 port)
76{
77 struct usb_ehci_s *cntl = container_of(hub->cntl, struct usb_ehci_s, usb);
78 u32 *portreg = &cntl->regs->portsc[port];
79 u32 portsc = readl(portreg);
80
81 // Finish reset on port
Kevin O'Connor190cc622010-03-09 19:43:52 -050082 portsc &= ~PORT_RESET;
83 writel(portreg, portsc);
84 msleep(EHCI_TIME_POSTRESET);
85
86 portsc = readl(portreg);
87 if (!(portsc & PORT_CONNECT))
88 // No longer connected
Kevin O'Connore2d6fdd2014-09-10 11:04:26 -040089 return -1;
Kevin O'Connor190cc622010-03-09 19:43:52 -050090 if (!(portsc & PORT_PE)) {
91 // full speed device
Kevin O'Connor190cc622010-03-09 19:43:52 -050092 writel(portreg, portsc | PORT_OWNER);
Kevin O'Connore2d6fdd2014-09-10 11:04:26 -040093 return -1;
Kevin O'Connor190cc622010-03-09 19:43:52 -050094 }
Kevin O'Connor87ab2fb2010-03-20 23:25:11 -040095
Kevin O'Connore2d6fdd2014-09-10 11:04:26 -040096 return USB_HIGHSPEED;
Kevin O'Connor190cc622010-03-09 19:43:52 -050097}
98
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040099// Disable port
100static void
101ehci_hub_disconnect(struct usbhub_s *hub, u32 port)
102{
103 struct usb_ehci_s *cntl = container_of(hub->cntl, struct usb_ehci_s, usb);
104 u32 *portreg = &cntl->regs->portsc[port];
105 u32 portsc = readl(portreg);
106 writel(portreg, portsc & ~PORT_PE);
107}
108
109static struct usbhub_op_s ehci_HubOp = {
110 .detect = ehci_hub_detect,
111 .reset = ehci_hub_reset,
112 .disconnect = ehci_hub_disconnect,
113};
114
Kevin O'Connor190cc622010-03-09 19:43:52 -0500115// Find any devices connected to the root hub.
116static int
117check_ehci_ports(struct usb_ehci_s *cntl)
118{
Kevin O'Connor5ac31b82014-09-10 10:02:20 -0400119 // Power up ports.
120 int i;
121 for (i=0; i<cntl->checkports; i++) {
122 u32 *portreg = &cntl->regs->portsc[i];
123 u32 portsc = readl(portreg);
124 if (!(portsc & PORT_POWER)) {
125 portsc |= PORT_POWER;
126 writel(portreg, portsc);
127 }
128 }
129 msleep(EHCI_TIME_POSTPOWER);
130
Kevin O'Connor190cc622010-03-09 19:43:52 -0500131 struct usbhub_s hub;
132 memset(&hub, 0, sizeof(hub));
133 hub.cntl = &cntl->usb;
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400134 hub.portcount = cntl->checkports;
135 hub.op = &ehci_HubOp;
136 usb_enumerate(&hub);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500137 return hub.devcount;
138}
139
140
141/****************************************************************
142 * Setup
143 ****************************************************************/
144
Kevin O'Connor402f9a02012-02-20 12:54:49 -0500145// Wait for next USB async frame to start - for ensuring safe memory release.
146static void
147ehci_waittick(struct usb_ehci_s *cntl)
148{
149 if (MODE16) {
150 msleep(10);
151 return;
152 }
153 // Wait for access to "doorbell"
154 barrier();
155 u32 cmd, sts;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400156 u32 end = timer_calc(100);
Kevin O'Connor402f9a02012-02-20 12:54:49 -0500157 for (;;) {
158 sts = readl(&cntl->regs->usbsts);
159 if (!(sts & STS_IAA)) {
160 cmd = readl(&cntl->regs->usbcmd);
161 if (!(cmd & CMD_IAAD))
162 break;
163 }
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400164 if (timer_check(end)) {
Kevin O'Connor402f9a02012-02-20 12:54:49 -0500165 warn_timeout();
166 return;
167 }
168 yield();
169 }
170 // Ring "doorbell"
171 writel(&cntl->regs->usbcmd, cmd | CMD_IAAD);
172 // Wait for completion
173 for (;;) {
174 sts = readl(&cntl->regs->usbsts);
175 if (sts & STS_IAA)
176 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400177 if (timer_check(end)) {
Kevin O'Connor402f9a02012-02-20 12:54:49 -0500178 warn_timeout();
179 return;
180 }
181 yield();
182 }
183 // Ack completion
184 writel(&cntl->regs->usbsts, STS_IAA);
185}
186
187static void
188ehci_free_pipes(struct usb_ehci_s *cntl)
189{
190 dprintf(7, "ehci_free_pipes %p\n", cntl);
191
192 struct ehci_qh *start = cntl->async_qh;
193 struct ehci_qh *pos = start;
194 for (;;) {
195 struct ehci_qh *next = (void*)(pos->next & ~EHCI_PTR_BITS);
196 if (next == start)
197 break;
198 struct ehci_pipe *pipe = container_of(next, struct ehci_pipe, qh);
Kevin O'Connor2bfd1702014-10-16 12:31:42 -0400199 if (usb_is_freelist(&cntl->usb, &pipe->pipe))
Kevin O'Connor402f9a02012-02-20 12:54:49 -0500200 pos->next = next->next;
201 else
202 pos = next;
203 }
204 ehci_waittick(cntl);
205 for (;;) {
206 struct usb_pipe *usbpipe = cntl->usb.freelist;
207 if (!usbpipe)
208 break;
209 cntl->usb.freelist = usbpipe->freenext;
210 struct ehci_pipe *pipe = container_of(usbpipe, struct ehci_pipe, pipe);
211 free(pipe);
212 }
213}
214
Kevin O'Connor190cc622010-03-09 19:43:52 -0500215static void
216configure_ehci(void *data)
217{
218 struct usb_ehci_s *cntl = data;
219
220 // Allocate ram for schedule storage
221 struct ehci_framelist *fl = memalign_high(sizeof(*fl), sizeof(*fl));
222 struct ehci_qh *intr_qh = memalign_high(EHCI_QH_ALIGN, sizeof(*intr_qh));
223 struct ehci_qh *async_qh = memalign_high(EHCI_QH_ALIGN, sizeof(*async_qh));
224 if (!fl || !intr_qh || !async_qh) {
225 warn_noalloc();
Kevin O'Connore2d6fdd2014-09-10 11:04:26 -0400226 PendingEHCI--;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500227 goto fail;
228 }
229
230 // XXX - check for halted?
231
232 // Reset the HC
233 u32 cmd = readl(&cntl->regs->usbcmd);
234 writel(&cntl->regs->usbcmd, (cmd & ~(CMD_ASE | CMD_PSE)) | CMD_HCRESET);
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400235 u32 end = timer_calc(250);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500236 for (;;) {
237 cmd = readl(&cntl->regs->usbcmd);
238 if (!(cmd & CMD_HCRESET))
239 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400240 if (timer_check(end)) {
Kevin O'Connor190cc622010-03-09 19:43:52 -0500241 warn_timeout();
Kevin O'Connore2d6fdd2014-09-10 11:04:26 -0400242 PendingEHCI--;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500243 goto fail;
244 }
Kevin O'Connor698d3f92010-04-17 16:59:12 -0400245 yield();
Kevin O'Connor190cc622010-03-09 19:43:52 -0500246 }
247
248 // Disable interrupts (just to be safe).
249 writel(&cntl->regs->usbintr, 0);
250
251 // Set schedule to point to primary intr queue head
252 memset(intr_qh, 0, sizeof(*intr_qh));
253 intr_qh->next = EHCI_PTR_TERM;
254 intr_qh->info2 = (0x01 << QH_SMASK_SHIFT);
255 intr_qh->token = QTD_STS_HALT;
256 intr_qh->qtd_next = intr_qh->alt_next = EHCI_PTR_TERM;
257 int i;
258 for (i=0; i<ARRAY_SIZE(fl->links); i++)
259 fl->links[i] = (u32)intr_qh | EHCI_PTR_QH;
260 writel(&cntl->regs->periodiclistbase, (u32)fl);
261
262 // Set async list to point to primary async queue head
263 memset(async_qh, 0, sizeof(*async_qh));
264 async_qh->next = (u32)async_qh | EHCI_PTR_QH;
265 async_qh->info1 = QH_HEAD;
266 async_qh->token = QTD_STS_HALT;
267 async_qh->qtd_next = async_qh->alt_next = EHCI_PTR_TERM;
268 cntl->async_qh = async_qh;
269 writel(&cntl->regs->asynclistbase, (u32)async_qh);
270
271 // Enable queues
272 writel(&cntl->regs->usbcmd, cmd | CMD_ASE | CMD_PSE | CMD_RUN);
273
274 // Set default of high speed for root hub.
275 writel(&cntl->regs->configflag, 1);
Kevin O'Connore2d6fdd2014-09-10 11:04:26 -0400276 PendingEHCI--;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500277
278 // Find devices
279 int count = check_ehci_ports(cntl);
Kevin O'Connor402f9a02012-02-20 12:54:49 -0500280 ehci_free_pipes(cntl);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500281 if (count)
282 // Success
283 return;
284
285 // No devices found - shutdown and free controller.
286 writel(&cntl->regs->usbcmd, cmd & ~CMD_RUN);
287 msleep(4); // 2ms to stop reading memory - XXX
288fail:
289 free(fl);
290 free(intr_qh);
291 free(async_qh);
292 free(cntl);
293}
294
Kevin O'Connorec443ff2013-12-05 18:43:20 -0500295static void
296ehci_controller_setup(struct pci_device *pci)
Kevin O'Connor190cc622010-03-09 19:43:52 -0500297{
Kevin O'Connor706eb892016-02-02 22:28:06 -0500298 struct ehci_caps *caps = pci_enable_membar(pci, PCI_BASE_ADDRESS_0);
299 if (!caps)
300 return;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500301 u32 hcc_params = readl(&caps->hccparams);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500302
303 struct usb_ehci_s *cntl = malloc_tmphigh(sizeof(*cntl));
Kevin O'Connorffdcd3a2011-07-10 15:48:00 -0400304 if (!cntl) {
305 warn_noalloc();
Kevin O'Connorec443ff2013-12-05 18:43:20 -0500306 return;
Kevin O'Connorffdcd3a2011-07-10 15:48:00 -0400307 }
Kevin O'Connor190cc622010-03-09 19:43:52 -0500308 memset(cntl, 0, sizeof(*cntl));
Kevin O'Connor8ff8e012011-07-09 14:11:21 -0400309 cntl->usb.pci = pci;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500310 cntl->usb.type = USB_TYPE_EHCI;
311 cntl->caps = caps;
Kevin O'Connorec443ff2013-12-05 18:43:20 -0500312 cntl->checkports = readl(&cntl->caps->hcsparams) & HCS_N_PORTS_MASK;
Avik Sil7cac6002013-02-14 10:54:57 +0530313 cntl->regs = (void*)caps + readb(&caps->caplength);
Sven Schnellea3fa66c2012-06-12 09:20:23 +0200314 if (hcc_params & HCC_64BIT_ADDR)
315 cntl->regs->ctrldssegment = 0;
Kevin O'Connore2d6fdd2014-09-10 11:04:26 -0400316 PendingEHCI++;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500317
Kevin O'Connor9dc243e2010-03-20 17:53:03 -0400318 dprintf(1, "EHCI init on dev %02x:%02x.%x (regs=%p)\n"
Kevin O'Connor706eb892016-02-02 22:28:06 -0500319 , pci_bdf_to_bus(pci->bdf), pci_bdf_to_dev(pci->bdf)
320 , pci_bdf_to_fn(pci->bdf), cntl->regs);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500321
Kevin O'Connor706eb892016-02-02 22:28:06 -0500322 pci_enable_busmaster(pci);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500323
324 // XXX - check for and disable SMM control?
325
Kevin O'Connorec443ff2013-12-05 18:43:20 -0500326 run_thread(configure_ehci, cntl);
327}
328
329void
330ehci_setup(void)
331{
332 if (! CONFIG_USB_EHCI)
333 return;
334 struct pci_device *pci;
335 foreachpci(pci) {
336 if (pci_classprog(pci) == PCI_CLASS_SERIAL_USB_EHCI)
337 ehci_controller_setup(pci);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500338 }
Kevin O'Connor32a2b0e2016-02-02 14:28:13 -0500339}
Kevin O'Connor190cc622010-03-09 19:43:52 -0500340
Kevin O'Connor32a2b0e2016-02-02 14:28:13 -0500341// Wait for all EHCI controllers to initialize. This forces OHCI/UHCI
342// setup to always be after any EHCI ports are routed to EHCI.
343void
344ehci_wait_controllers(void)
345{
346 while (CONFIG_USB_EHCI && CONFIG_THREADS && PendingEHCI)
Kevin O'Connorec443ff2013-12-05 18:43:20 -0500347 yield();
Kevin O'Connor190cc622010-03-09 19:43:52 -0500348}
349
350
351/****************************************************************
352 * End point communication
353 ****************************************************************/
354
Kevin O'Connor8915ed22012-03-10 10:09:56 -0500355// Setup fields in qh
356static void
357ehci_desc2pipe(struct ehci_pipe *pipe, struct usbdevice_s *usbdev
358 , struct usb_endpoint_descriptor *epdesc)
359{
360 usb_desc2pipe(&pipe->pipe, usbdev, epdesc);
361
Kevin O'Connor32d68ab2012-03-10 21:07:26 -0500362 pipe->qh.info1 = ((pipe->pipe.maxpacket << QH_MAXPACKET_SHIFT)
363 | (pipe->pipe.speed << QH_SPEED_SHIFT)
364 | (pipe->pipe.ep << QH_EP_SHIFT)
365 | (pipe->pipe.devaddr << QH_DEVADDR_SHIFT));
Kevin O'Connor8915ed22012-03-10 10:09:56 -0500366
367 pipe->qh.info2 = (1 << QH_MULT_SHIFT);
368 struct usbdevice_s *hubdev = usbdev->hub->usbdev;
369 if (hubdev) {
370 struct ehci_pipe *hpipe = container_of(
371 hubdev->defpipe, struct ehci_pipe, pipe);
372 if (hpipe->pipe.speed == USB_HIGHSPEED)
Kevin O'Connor67d1fbe2014-09-12 11:59:23 -0400373 pipe->qh.info2 |= (((usbdev->port+1) << QH_HUBPORT_SHIFT)
Kevin O'Connor8915ed22012-03-10 10:09:56 -0500374 | (hpipe->pipe.devaddr << QH_HUBADDR_SHIFT));
375 else
376 pipe->qh.info2 = hpipe->qh.info2;
377 }
378
379 u8 eptype = epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
380 if (eptype == USB_ENDPOINT_XFER_CONTROL)
381 pipe->qh.info1 |= ((pipe->pipe.speed != USB_HIGHSPEED ? QH_CONTROL : 0)
382 | QH_TOGGLECONTROL);
383 else if (eptype == USB_ENDPOINT_XFER_INT)
384 pipe->qh.info2 |= (0x01 << QH_SMASK_SHIFT) | (0x1c << QH_CMASK_SHIFT);
385}
386
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500387static struct usb_pipe *
Kevin O'Connor6152c322012-03-10 08:53:58 -0500388ehci_alloc_intr_pipe(struct usbdevice_s *usbdev
389 , struct usb_endpoint_descriptor *epdesc)
Kevin O'Connor49ecf692011-11-19 14:21:51 -0500390{
Kevin O'Connor6152c322012-03-10 08:53:58 -0500391 struct usb_ehci_s *cntl = container_of(
392 usbdev->hub->cntl, struct usb_ehci_s, usb);
Kevin O'Connor98cdad32014-10-16 12:05:09 -0400393 int frameexp = usb_get_period(usbdev, epdesc);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500394 dprintf(7, "ehci_alloc_intr_pipe %p %d\n", &cntl->usb, frameexp);
Kevin O'Connor49ecf692011-11-19 14:21:51 -0500395
Kevin O'Connor6152c322012-03-10 08:53:58 -0500396 if (frameexp > 10)
397 frameexp = 10;
398 int maxpacket = epdesc->wMaxPacketSize;
399 // Determine number of entries needed for 2 timer ticks.
400 int ms = 1<<frameexp;
Kevin O'Connor69013372013-07-20 12:08:48 -0400401 int count = DIV_ROUND_UP(ticks_to_ms(2), ms);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500402 struct ehci_pipe *pipe = memalign_low(EHCI_QH_ALIGN, sizeof(*pipe));
403 struct ehci_qtd *tds = memalign_low(EHCI_QTD_ALIGN, sizeof(*tds) * count);
404 void *data = malloc_low(maxpacket * count);
405 if (!pipe || !tds || !data) {
406 warn_noalloc();
407 goto fail;
Kevin O'Connor49ecf692011-11-19 14:21:51 -0500408 }
Kevin O'Connor6152c322012-03-10 08:53:58 -0500409 memset(pipe, 0, sizeof(*pipe));
Kevin O'Connor7d2fd492014-01-17 18:43:10 -0500410 memset(tds, 0, sizeof(*tds) * count);
411 memset(data, 0, maxpacket * count);
Kevin O'Connor8915ed22012-03-10 10:09:56 -0500412 ehci_desc2pipe(pipe, usbdev, epdesc);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500413 pipe->next_td = pipe->tds = tds;
414 pipe->data = data;
Kevin O'Connor6152c322012-03-10 08:53:58 -0500415 pipe->qh.qtd_next = (u32)tds;
416
417 int i;
418 for (i=0; i<count; i++) {
419 struct ehci_qtd *td = &tds[i];
420 td->qtd_next = (i==count-1 ? (u32)tds : (u32)&td[1]);
421 td->alt_next = EHCI_PTR_TERM;
422 td->token = (ehci_explen(maxpacket) | QTD_STS_ACTIVE
423 | QTD_PID_IN | ehci_maxerr(3));
424 td->buf[0] = (u32)data + maxpacket * i;
Kevin O'Connor49ecf692011-11-19 14:21:51 -0500425 }
Kevin O'Connor6152c322012-03-10 08:53:58 -0500426
427 // Add to interrupt schedule.
428 struct ehci_framelist *fl = (void*)readl(&cntl->regs->periodiclistbase);
429 if (frameexp == 0) {
430 // Add to existing interrupt entry.
431 struct ehci_qh *intr_qh = (void*)(fl->links[0] & ~EHCI_PTR_BITS);
432 pipe->qh.next = intr_qh->next;
433 barrier();
434 intr_qh->next = (u32)&pipe->qh | EHCI_PTR_QH;
435 } else {
436 int startpos = 1<<(frameexp-1);
437 pipe->qh.next = fl->links[startpos];
438 barrier();
439 for (i=startpos; i<ARRAY_SIZE(fl->links); i+=ms)
440 fl->links[i] = (u32)&pipe->qh | EHCI_PTR_QH;
441 }
442
443 return &pipe->pipe;
444fail:
445 free(pipe);
446 free(tds);
447 free(data);
448 return NULL;
Kevin O'Connor49ecf692011-11-19 14:21:51 -0500449}
Kevin O'Connor190cc622010-03-09 19:43:52 -0500450
Kevin O'Connor01de9bd2012-03-05 22:41:21 -0500451struct usb_pipe *
Kevin O'Connor277ea6f2014-10-16 13:40:42 -0400452ehci_realloc_pipe(struct usbdevice_s *usbdev, struct usb_pipe *upipe
453 , struct usb_endpoint_descriptor *epdesc)
Kevin O'Connor190cc622010-03-09 19:43:52 -0500454{
455 if (! CONFIG_USB_EHCI)
456 return NULL;
Kevin O'Connor277ea6f2014-10-16 13:40:42 -0400457 usb_add_freelist(upipe);
458 if (!epdesc)
459 return NULL;
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500460 u8 eptype = epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
461 if (eptype == USB_ENDPOINT_XFER_INT)
462 return ehci_alloc_intr_pipe(usbdev, epdesc);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500463 struct usb_ehci_s *cntl = container_of(
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500464 usbdev->hub->cntl, struct usb_ehci_s, usb);
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500465 dprintf(7, "ehci_alloc_async_pipe %p %d\n", &cntl->usb, eptype);
466
Kevin O'Connor2bfd1702014-10-16 12:31:42 -0400467 struct usb_pipe *usbpipe = usb_get_freelist(&cntl->usb, eptype);
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500468 if (usbpipe) {
469 // Use previously allocated pipe.
Kevin O'Connor8915ed22012-03-10 10:09:56 -0500470 struct ehci_pipe *pipe = container_of(usbpipe, struct ehci_pipe, pipe);
471 ehci_desc2pipe(pipe, usbdev, epdesc);
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500472 return usbpipe;
473 }
Kevin O'Connor402f9a02012-02-20 12:54:49 -0500474
475 // Allocate a new queue head.
476 struct ehci_pipe *pipe;
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500477 if (eptype == USB_ENDPOINT_XFER_CONTROL)
Kevin O'Connor402f9a02012-02-20 12:54:49 -0500478 pipe = memalign_tmphigh(EHCI_QH_ALIGN, sizeof(*pipe));
479 else
480 pipe = memalign_low(EHCI_QH_ALIGN, sizeof(*pipe));
Kevin O'Connor190cc622010-03-09 19:43:52 -0500481 if (!pipe) {
482 warn_noalloc();
483 return NULL;
484 }
485 memset(pipe, 0, sizeof(*pipe));
Kevin O'Connor8915ed22012-03-10 10:09:56 -0500486 ehci_desc2pipe(pipe, usbdev, epdesc);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500487 pipe->qh.qtd_next = pipe->qh.alt_next = EHCI_PTR_TERM;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500488
489 // Add queue head to controller list.
490 struct ehci_qh *async_qh = cntl->async_qh;
491 pipe->qh.next = async_qh->next;
492 barrier();
493 async_qh->next = (u32)&pipe->qh | EHCI_PTR_QH;
494 return &pipe->pipe;
495}
496
Kevin O'Connor6152c322012-03-10 08:53:58 -0500497static void
498ehci_reset_pipe(struct ehci_pipe *pipe)
499{
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400500 SET_LOWFLAT(pipe->qh.qtd_next, EHCI_PTR_TERM);
501 SET_LOWFLAT(pipe->qh.alt_next, EHCI_PTR_TERM);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500502 barrier();
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400503 SET_LOWFLAT(pipe->qh.token, GET_LOWFLAT(pipe->qh.token) & QTD_TOGGLE);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500504}
505
506static int
Kevin O'Connorab9d7712014-06-14 12:48:35 -0400507ehci_wait_td(struct ehci_pipe *pipe, struct ehci_qtd *td, u32 end)
Kevin O'Connor6152c322012-03-10 08:53:58 -0500508{
Kevin O'Connor6152c322012-03-10 08:53:58 -0500509 u32 status;
510 for (;;) {
511 status = td->token;
512 if (!(status & QTD_STS_ACTIVE))
513 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400514 if (timer_check(end)) {
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400515 u32 cur = GET_LOWFLAT(pipe->qh.current);
516 u32 tok = GET_LOWFLAT(pipe->qh.token);
517 u32 next = GET_LOWFLAT(pipe->qh.qtd_next);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500518 warn_timeout();
519 dprintf(1, "ehci pipe=%p cur=%08x tok=%08x next=%x td=%p status=%x\n"
520 , pipe, cur, tok, next, td, status);
521 ehci_reset_pipe(pipe);
522 struct usb_ehci_s *cntl = container_of(
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400523 GET_LOWFLAT(pipe->pipe.cntl), struct usb_ehci_s, usb);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500524 ehci_waittick(cntl);
525 return -1;
526 }
527 yield();
528 }
529 if (status & QTD_STS_HALT) {
530 dprintf(1, "ehci_wait_td error - status=%x\n", status);
531 ehci_reset_pipe(pipe);
532 return -2;
533 }
534 return 0;
535}
536
Kevin O'Connor1bd976b2015-01-01 12:51:04 -0500537static void
538ehci_fill_tdbuf(struct ehci_qtd *td, u32 dest, int transfer)
Kevin O'Connor190cc622010-03-09 19:43:52 -0500539{
Kevin O'Connor1bd976b2015-01-01 12:51:04 -0500540 u32 *pos = td->buf, end = dest + transfer;
541 for (; dest < end; dest = ALIGN_DOWN(dest + PAGE_SIZE, PAGE_SIZE))
542 *pos++ = dest;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500543}
544
Kevin O'Connorabd67422014-12-30 21:16:04 -0500545#define STACKQTDS 6
Kevin O'Connor190cc622010-03-09 19:43:52 -0500546
547int
Kevin O'Connor2891a832014-12-31 17:41:14 -0500548ehci_send_pipe(struct usb_pipe *p, int dir, const void *cmd
Kevin O'Connorabd67422014-12-30 21:16:04 -0500549 , void *data, int datasize)
Kevin O'Connor190cc622010-03-09 19:43:52 -0500550{
551 if (! CONFIG_USB_EHCI)
552 return -1;
553 struct ehci_pipe *pipe = container_of(p, struct ehci_pipe, pipe);
Kevin O'Connorabd67422014-12-30 21:16:04 -0500554 dprintf(7, "ehci_send_pipe qh=%p dir=%d data=%p size=%d\n"
Kevin O'Connor190cc622010-03-09 19:43:52 -0500555 , &pipe->qh, dir, data, datasize);
556
Kevin O'Connorabd67422014-12-30 21:16:04 -0500557 // Allocate tds on stack (with required alignment)
Kevin O'Connor190cc622010-03-09 19:43:52 -0500558 u8 tdsbuf[sizeof(struct ehci_qtd) * STACKQTDS + EHCI_QTD_ALIGN - 1];
Kevin O'Connor7f66e7b2014-12-30 20:50:44 -0500559 struct ehci_qtd *tds = (void*)ALIGN((u32)tdsbuf, EHCI_QTD_ALIGN), *td = tds;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500560 memset(tds, 0, sizeof(*tds) * STACKQTDS);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500561
Kevin O'Connor7f66e7b2014-12-30 20:50:44 -0500562 // Setup transfer descriptors
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400563 u16 maxpacket = GET_LOWFLAT(pipe->pipe.maxpacket);
Kevin O'Connorabd67422014-12-30 21:16:04 -0500564 u32 toggle = 0;
565 if (cmd) {
566 // Send setup pid on control transfers
567 td->qtd_next = (u32)MAKE_FLATPTR(GET_SEG(SS), td+1);
568 td->alt_next = EHCI_PTR_TERM;
Kevin O'Connor2891a832014-12-31 17:41:14 -0500569 td->token = (ehci_explen(USB_CONTROL_SETUP_SIZE) | QTD_STS_ACTIVE
Kevin O'Connorabd67422014-12-30 21:16:04 -0500570 | QTD_PID_SETUP | ehci_maxerr(3));
Kevin O'Connor2891a832014-12-31 17:41:14 -0500571 ehci_fill_tdbuf(td, (u32)cmd, USB_CONTROL_SETUP_SIZE);
Kevin O'Connorabd67422014-12-30 21:16:04 -0500572 td++;
573 toggle = QTD_TOGGLE;
574 }
Kevin O'Connor1bd976b2015-01-01 12:51:04 -0500575 u32 dest = (u32)data, dataend = dest + datasize;
576 while (dest < dataend) {
Kevin O'Connorabd67422014-12-30 21:16:04 -0500577 // Send data pids
Kevin O'Connor7f66e7b2014-12-30 20:50:44 -0500578 if (td >= &tds[STACKQTDS]) {
579 warn_noalloc();
Kevin O'Connor49ecf692011-11-19 14:21:51 -0500580 return -1;
Kevin O'Connor7f66e7b2014-12-30 20:50:44 -0500581 }
Kevin O'Connor1bd976b2015-01-01 12:51:04 -0500582 int maxtransfer = 5*PAGE_SIZE - (dest & (PAGE_SIZE-1));
583 int transfer = dataend - dest;
584 if (transfer > maxtransfer)
585 transfer = ALIGN_DOWN(maxtransfer, maxpacket);
Kevin O'Connor7f66e7b2014-12-30 20:50:44 -0500586 td->qtd_next = (u32)MAKE_FLATPTR(GET_SEG(SS), td+1);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500587 td->alt_next = EHCI_PTR_TERM;
Kevin O'Connorabd67422014-12-30 21:16:04 -0500588 td->token = (ehci_explen(transfer) | toggle | QTD_STS_ACTIVE
Kevin O'Connor190cc622010-03-09 19:43:52 -0500589 | (dir ? QTD_PID_IN : QTD_PID_OUT) | ehci_maxerr(3));
Kevin O'Connor1bd976b2015-01-01 12:51:04 -0500590 ehci_fill_tdbuf(td, dest, transfer);
Kevin O'Connor7f66e7b2014-12-30 20:50:44 -0500591 td++;
Kevin O'Connor1bd976b2015-01-01 12:51:04 -0500592 dest += transfer;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500593 }
Kevin O'Connorabd67422014-12-30 21:16:04 -0500594 if (cmd) {
595 // Send status pid on control transfers
596 if (td >= &tds[STACKQTDS]) {
597 warn_noalloc();
598 return -1;
599 }
600 td->qtd_next = EHCI_PTR_TERM;
601 td->alt_next = EHCI_PTR_TERM;
602 td->token = (QTD_TOGGLE | QTD_STS_ACTIVE
603 | (dir ? QTD_PID_OUT : QTD_PID_IN) | ehci_maxerr(3));
604 td++;
605 }
Kevin O'Connor7f66e7b2014-12-30 20:50:44 -0500606
607 // Transfer data
608 (td-1)->qtd_next = EHCI_PTR_TERM;
609 barrier();
610 SET_LOWFLAT(pipe->qh.qtd_next, (u32)MAKE_FLATPTR(GET_SEG(SS), tds));
Kevin O'Connor1bd976b2015-01-01 12:51:04 -0500611 u32 end = timer_calc(usb_xfer_time(p, datasize));
Kevin O'Connor190cc622010-03-09 19:43:52 -0500612 int i;
Kevin O'Connor7f66e7b2014-12-30 20:50:44 -0500613 for (i=0, td=tds; i<STACKQTDS; i++, td++) {
Kevin O'Connorab9d7712014-06-14 12:48:35 -0400614 int ret = ehci_wait_td(pipe, td, end);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500615 if (ret)
Kevin O'Connor49ecf692011-11-19 14:21:51 -0500616 return -1;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500617 }
618
619 return 0;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500620}
621
Kevin O'Connor190cc622010-03-09 19:43:52 -0500622int
623ehci_poll_intr(struct usb_pipe *p, void *data)
624{
625 ASSERT16();
626 if (! CONFIG_USB_EHCI)
627 return -1;
628 struct ehci_pipe *pipe = container_of(p, struct ehci_pipe, pipe);
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400629 struct ehci_qtd *td = GET_LOWFLAT(pipe->next_td);
630 u32 token = GET_LOWFLAT(td->token);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500631 if (token & QTD_STS_ACTIVE)
632 // No intrs found.
633 return -1;
634 // XXX - check for errors.
635
636 // Copy data.
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400637 int maxpacket = GET_LOWFLAT(pipe->pipe.maxpacket);
638 int pos = td - GET_LOWFLAT(pipe->tds);
639 void *tddata = GET_LOWFLAT(pipe->data) + maxpacket * pos;
640 memcpy_far(GET_SEG(SS), data, SEG_LOW, LOWFLAT2LOW(tddata), maxpacket);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500641
642 // Reenable this td.
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400643 struct ehci_qtd *next = (void*)(GET_LOWFLAT(td->qtd_next) & ~EHCI_PTR_BITS);
644 SET_LOWFLAT(pipe->next_td, next);
645 SET_LOWFLAT(td->buf[0], (u32)tddata);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500646 barrier();
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400647 SET_LOWFLAT(td->token, (ehci_explen(maxpacket) | QTD_STS_ACTIVE
Kevin O'Connor190cc622010-03-09 19:43:52 -0500648 | QTD_PID_IN | ehci_maxerr(3)));
649
650 return 0;
651}