blob: bb02293a1ce4af8dc8f7aa03662d71ba91c26f81 [file] [log] [blame]
Kevin O'Connor190cc622010-03-09 19:43:52 -05001// Code for handling EHCI USB controllers.
2//
3// Copyright (C) 2010 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-ehci.h" // struct ehci_qh
12#include "pci_ids.h" // PCI_CLASS_SERIAL_USB_UHCI
13#include "pci_regs.h" // PCI_BASE_ADDRESS_0
14#include "usb.h" // struct usb_s
Kevin O'Connor4f6563e2012-05-19 22:42:51 -040015#include "biosvar.h" // GET_LOWFLAT
Kevin O'Connord83c87b2013-01-21 01:14:12 -050016#include "usb-uhci.h" // uhci_setup
17#include "usb-ohci.h" // ohci_setup
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040018#include "string.h" // memset
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'Connor8ff8e012011-07-09 14:11:21 -040026 struct pci_device *companion[8];
Kevin O'Connor190cc622010-03-09 19:43:52 -050027 int checkports;
28 int legacycount;
29};
30
Kevin O'Connor402f9a02012-02-20 12:54:49 -050031struct ehci_pipe {
32 struct ehci_qh qh;
33 struct ehci_qtd *next_td, *tds;
34 void *data;
35 struct usb_pipe pipe;
Kevin O'Connor402f9a02012-02-20 12:54:49 -050036};
37
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 need companion controllers for full/low speed devices
Kevin O'Connor190cc622010-03-09 19:43:52 -050047static void
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040048ehci_note_port(struct usb_ehci_s *cntl)
Kevin O'Connor190cc622010-03-09 19:43:52 -050049{
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040050 if (--cntl->checkports)
51 // Ports still being detected.
52 return;
Kevin O'Connor190cc622010-03-09 19:43:52 -050053 if (! cntl->legacycount)
54 // No full/low speed devices found.
55 return;
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040056 // Start companion controllers.
Kevin O'Connor190cc622010-03-09 19:43:52 -050057 int i;
58 for (i=0; i<ARRAY_SIZE(cntl->companion); i++) {
Kevin O'Connor8ff8e012011-07-09 14:11:21 -040059 struct pci_device *pci = cntl->companion[i];
60 if (!pci)
61 break;
Kevin O'Connor6f500512011-07-10 15:41:55 -040062
Kevin O'Connord83c87b2013-01-21 01:14:12 -050063 // ohci/uhci_setup call pci_config_X - don't run from irq handler.
Kevin O'Connor6f500512011-07-10 15:41:55 -040064 wait_preempt();
65
Kevin O'Connor8ff8e012011-07-09 14:11:21 -040066 if (pci_classprog(pci) == PCI_CLASS_SERIAL_USB_UHCI)
Kevin O'Connord83c87b2013-01-21 01:14:12 -050067 uhci_setup(pci, cntl->usb.busid + i);
Kevin O'Connor8ff8e012011-07-09 14:11:21 -040068 else if (pci_classprog(pci) == PCI_CLASS_SERIAL_USB_OHCI)
Kevin O'Connord83c87b2013-01-21 01:14:12 -050069 ohci_setup(pci, cntl->usb.busid + i);
Kevin O'Connor190cc622010-03-09 19:43:52 -050070 }
71}
72
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040073// Check if device attached to port
74static int
75ehci_hub_detect(struct usbhub_s *hub, u32 port)
Kevin O'Connor190cc622010-03-09 19:43:52 -050076{
Kevin O'Connor190cc622010-03-09 19:43:52 -050077 struct usb_ehci_s *cntl = container_of(hub->cntl, struct usb_ehci_s, usb);
Kevin O'Connor190cc622010-03-09 19:43:52 -050078 u32 *portreg = &cntl->regs->portsc[port];
79 u32 portsc = readl(portreg);
80
81 // Power up port.
82 if (!(portsc & PORT_POWER)) {
83 portsc |= PORT_POWER;
84 writel(portreg, portsc);
85 msleep(EHCI_TIME_POSTPOWER);
Kevin O'Connor87ab2fb2010-03-20 23:25:11 -040086 } else {
Kevin O'Connorc0207882012-08-01 20:40:03 -040087 // Port is already powered up, but we don't know how long it
88 // has been powered up, so wait the 20ms.
89 msleep(EHCI_TIME_POSTPOWER);
Kevin O'Connor190cc622010-03-09 19:43:52 -050090 }
Kevin O'Connor87ab2fb2010-03-20 23:25:11 -040091 portsc = readl(portreg);
Kevin O'Connor190cc622010-03-09 19:43:52 -050092
93 if (!(portsc & PORT_CONNECT))
94 // No device present
Kevin O'Connor87ab2fb2010-03-20 23:25:11 -040095 goto doneearly;
Kevin O'Connor190cc622010-03-09 19:43:52 -050096
97 if ((portsc & PORT_LINESTATUS_MASK) == PORT_LINESTATUS_KSTATE) {
98 // low speed device
99 cntl->legacycount++;
100 writel(portreg, portsc | PORT_OWNER);
Kevin O'Connor87ab2fb2010-03-20 23:25:11 -0400101 goto doneearly;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500102 }
103
104 // XXX - if just powered up, need to wait for USB_TIME_ATTDB?
105
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400106 // Begin reset on port
Kevin O'Connor190cc622010-03-09 19:43:52 -0500107 portsc = (portsc & ~PORT_PE) | PORT_RESET;
108 writel(portreg, portsc);
109 msleep(USB_TIME_DRSTR);
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400110 return 0;
111
112doneearly:
113 ehci_note_port(cntl);
114 return -1;
115}
116
117// Reset device on port
118static int
119ehci_hub_reset(struct usbhub_s *hub, u32 port)
120{
121 struct usb_ehci_s *cntl = container_of(hub->cntl, struct usb_ehci_s, usb);
122 u32 *portreg = &cntl->regs->portsc[port];
123 u32 portsc = readl(portreg);
124
125 // Finish reset on port
Kevin O'Connor190cc622010-03-09 19:43:52 -0500126 portsc &= ~PORT_RESET;
127 writel(portreg, portsc);
128 msleep(EHCI_TIME_POSTRESET);
129
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400130 int rv = -1;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500131 portsc = readl(portreg);
132 if (!(portsc & PORT_CONNECT))
133 // No longer connected
134 goto resetfail;
135 if (!(portsc & PORT_PE)) {
136 // full speed device
137 cntl->legacycount++;
138 writel(portreg, portsc | PORT_OWNER);
139 goto resetfail;
140 }
Kevin O'Connor87ab2fb2010-03-20 23:25:11 -0400141
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400142 rv = USB_HIGHSPEED;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500143resetfail:
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400144 ehci_note_port(cntl);
145 return rv;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500146}
147
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400148// Disable port
149static void
150ehci_hub_disconnect(struct usbhub_s *hub, u32 port)
151{
152 struct usb_ehci_s *cntl = container_of(hub->cntl, struct usb_ehci_s, usb);
153 u32 *portreg = &cntl->regs->portsc[port];
154 u32 portsc = readl(portreg);
155 writel(portreg, portsc & ~PORT_PE);
156}
157
158static struct usbhub_op_s ehci_HubOp = {
159 .detect = ehci_hub_detect,
160 .reset = ehci_hub_reset,
161 .disconnect = ehci_hub_disconnect,
162};
163
Kevin O'Connor190cc622010-03-09 19:43:52 -0500164// Find any devices connected to the root hub.
165static int
166check_ehci_ports(struct usb_ehci_s *cntl)
167{
168 ASSERT32FLAT();
Kevin O'Connor190cc622010-03-09 19:43:52 -0500169 struct usbhub_s hub;
170 memset(&hub, 0, sizeof(hub));
171 hub.cntl = &cntl->usb;
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400172 hub.portcount = cntl->checkports;
173 hub.op = &ehci_HubOp;
174 usb_enumerate(&hub);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500175 return hub.devcount;
176}
177
178
179/****************************************************************
180 * Setup
181 ****************************************************************/
182
Kevin O'Connor402f9a02012-02-20 12:54:49 -0500183// Wait for next USB async frame to start - for ensuring safe memory release.
184static void
185ehci_waittick(struct usb_ehci_s *cntl)
186{
187 if (MODE16) {
188 msleep(10);
189 return;
190 }
191 // Wait for access to "doorbell"
192 barrier();
193 u32 cmd, sts;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400194 u32 end = timer_calc(100);
Kevin O'Connor402f9a02012-02-20 12:54:49 -0500195 for (;;) {
196 sts = readl(&cntl->regs->usbsts);
197 if (!(sts & STS_IAA)) {
198 cmd = readl(&cntl->regs->usbcmd);
199 if (!(cmd & CMD_IAAD))
200 break;
201 }
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400202 if (timer_check(end)) {
Kevin O'Connor402f9a02012-02-20 12:54:49 -0500203 warn_timeout();
204 return;
205 }
206 yield();
207 }
208 // Ring "doorbell"
209 writel(&cntl->regs->usbcmd, cmd | CMD_IAAD);
210 // Wait for completion
211 for (;;) {
212 sts = readl(&cntl->regs->usbsts);
213 if (sts & STS_IAA)
214 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400215 if (timer_check(end)) {
Kevin O'Connor402f9a02012-02-20 12:54:49 -0500216 warn_timeout();
217 return;
218 }
219 yield();
220 }
221 // Ack completion
222 writel(&cntl->regs->usbsts, STS_IAA);
223}
224
225static void
226ehci_free_pipes(struct usb_ehci_s *cntl)
227{
228 dprintf(7, "ehci_free_pipes %p\n", cntl);
229
230 struct ehci_qh *start = cntl->async_qh;
231 struct ehci_qh *pos = start;
232 for (;;) {
233 struct ehci_qh *next = (void*)(pos->next & ~EHCI_PTR_BITS);
234 if (next == start)
235 break;
236 struct ehci_pipe *pipe = container_of(next, struct ehci_pipe, qh);
237 if (pipe->pipe.cntl != &cntl->usb)
238 pos->next = next->next;
239 else
240 pos = next;
241 }
242 ehci_waittick(cntl);
243 for (;;) {
244 struct usb_pipe *usbpipe = cntl->usb.freelist;
245 if (!usbpipe)
246 break;
247 cntl->usb.freelist = usbpipe->freenext;
248 struct ehci_pipe *pipe = container_of(usbpipe, struct ehci_pipe, pipe);
249 free(pipe);
250 }
251}
252
Kevin O'Connor190cc622010-03-09 19:43:52 -0500253static void
254configure_ehci(void *data)
255{
256 struct usb_ehci_s *cntl = data;
257
258 // Allocate ram for schedule storage
259 struct ehci_framelist *fl = memalign_high(sizeof(*fl), sizeof(*fl));
260 struct ehci_qh *intr_qh = memalign_high(EHCI_QH_ALIGN, sizeof(*intr_qh));
261 struct ehci_qh *async_qh = memalign_high(EHCI_QH_ALIGN, sizeof(*async_qh));
262 if (!fl || !intr_qh || !async_qh) {
263 warn_noalloc();
264 goto fail;
265 }
266
267 // XXX - check for halted?
268
269 // Reset the HC
270 u32 cmd = readl(&cntl->regs->usbcmd);
271 writel(&cntl->regs->usbcmd, (cmd & ~(CMD_ASE | CMD_PSE)) | CMD_HCRESET);
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400272 u32 end = timer_calc(250);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500273 for (;;) {
274 cmd = readl(&cntl->regs->usbcmd);
275 if (!(cmd & CMD_HCRESET))
276 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400277 if (timer_check(end)) {
Kevin O'Connor190cc622010-03-09 19:43:52 -0500278 warn_timeout();
279 goto fail;
280 }
Kevin O'Connor698d3f92010-04-17 16:59:12 -0400281 yield();
Kevin O'Connor190cc622010-03-09 19:43:52 -0500282 }
283
284 // Disable interrupts (just to be safe).
285 writel(&cntl->regs->usbintr, 0);
286
287 // Set schedule to point to primary intr queue head
288 memset(intr_qh, 0, sizeof(*intr_qh));
289 intr_qh->next = EHCI_PTR_TERM;
290 intr_qh->info2 = (0x01 << QH_SMASK_SHIFT);
291 intr_qh->token = QTD_STS_HALT;
292 intr_qh->qtd_next = intr_qh->alt_next = EHCI_PTR_TERM;
293 int i;
294 for (i=0; i<ARRAY_SIZE(fl->links); i++)
295 fl->links[i] = (u32)intr_qh | EHCI_PTR_QH;
296 writel(&cntl->regs->periodiclistbase, (u32)fl);
297
298 // Set async list to point to primary async queue head
299 memset(async_qh, 0, sizeof(*async_qh));
300 async_qh->next = (u32)async_qh | EHCI_PTR_QH;
301 async_qh->info1 = QH_HEAD;
302 async_qh->token = QTD_STS_HALT;
303 async_qh->qtd_next = async_qh->alt_next = EHCI_PTR_TERM;
304 cntl->async_qh = async_qh;
305 writel(&cntl->regs->asynclistbase, (u32)async_qh);
306
307 // Enable queues
308 writel(&cntl->regs->usbcmd, cmd | CMD_ASE | CMD_PSE | CMD_RUN);
309
310 // Set default of high speed for root hub.
311 writel(&cntl->regs->configflag, 1);
312 cntl->checkports = readl(&cntl->caps->hcsparams) & HCS_N_PORTS_MASK;
313
314 // Find devices
315 int count = check_ehci_ports(cntl);
Kevin O'Connor402f9a02012-02-20 12:54:49 -0500316 ehci_free_pipes(cntl);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500317 if (count)
318 // Success
319 return;
320
321 // No devices found - shutdown and free controller.
322 writel(&cntl->regs->usbcmd, cmd & ~CMD_RUN);
323 msleep(4); // 2ms to stop reading memory - XXX
324fail:
325 free(fl);
326 free(intr_qh);
327 free(async_qh);
328 free(cntl);
329}
330
331int
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500332ehci_setup(struct pci_device *pci, int busid, struct pci_device *comppci)
Kevin O'Connor190cc622010-03-09 19:43:52 -0500333{
334 if (! CONFIG_USB_EHCI)
335 return -1;
336
Kevin O'Connor8ff8e012011-07-09 14:11:21 -0400337 u16 bdf = pci->bdf;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500338 u32 baseaddr = pci_config_readl(bdf, PCI_BASE_ADDRESS_0);
339 struct ehci_caps *caps = (void*)(baseaddr & PCI_BASE_ADDRESS_MEM_MASK);
340 u32 hcc_params = readl(&caps->hccparams);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500341
342 struct usb_ehci_s *cntl = malloc_tmphigh(sizeof(*cntl));
Kevin O'Connorffdcd3a2011-07-10 15:48:00 -0400343 if (!cntl) {
344 warn_noalloc();
345 return -1;
346 }
Kevin O'Connor190cc622010-03-09 19:43:52 -0500347 memset(cntl, 0, sizeof(*cntl));
348 cntl->usb.busid = busid;
Kevin O'Connor8ff8e012011-07-09 14:11:21 -0400349 cntl->usb.pci = pci;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500350 cntl->usb.type = USB_TYPE_EHCI;
351 cntl->caps = caps;
Avik Sil7cac6002013-02-14 10:54:57 +0530352 cntl->regs = (void*)caps + readb(&caps->caplength);
Sven Schnellea3fa66c2012-06-12 09:20:23 +0200353 if (hcc_params & HCC_64BIT_ADDR)
354 cntl->regs->ctrldssegment = 0;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500355
Kevin O'Connor9dc243e2010-03-20 17:53:03 -0400356 dprintf(1, "EHCI init on dev %02x:%02x.%x (regs=%p)\n"
Kevin O'Connor190cc622010-03-09 19:43:52 -0500357 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf)
358 , pci_bdf_to_fn(bdf), cntl->regs);
359
360 pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER);
361
362 // XXX - check for and disable SMM control?
363
364 // Find companion controllers.
365 int count = 0;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500366 for (;;) {
Kevin O'Connor8ff8e012011-07-09 14:11:21 -0400367 if (!comppci || comppci == pci)
Kevin O'Connor190cc622010-03-09 19:43:52 -0500368 break;
Kevin O'Connor8ff8e012011-07-09 14:11:21 -0400369 if (pci_classprog(comppci) == PCI_CLASS_SERIAL_USB_UHCI)
370 cntl->companion[count++] = comppci;
371 else if (pci_classprog(comppci) == PCI_CLASS_SERIAL_USB_OHCI)
372 cntl->companion[count++] = comppci;
Kevin O'Connorb98a4b12013-06-08 21:53:36 -0400373 comppci = container_of(comppci->node.next, struct pci_device, node);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500374 }
375
376 run_thread(configure_ehci, cntl);
377 return 0;
378}
379
380
381/****************************************************************
382 * End point communication
383 ****************************************************************/
384
Kevin O'Connor8915ed22012-03-10 10:09:56 -0500385// Setup fields in qh
386static void
387ehci_desc2pipe(struct ehci_pipe *pipe, struct usbdevice_s *usbdev
388 , struct usb_endpoint_descriptor *epdesc)
389{
390 usb_desc2pipe(&pipe->pipe, usbdev, epdesc);
391
Kevin O'Connor32d68ab2012-03-10 21:07:26 -0500392 pipe->qh.info1 = ((pipe->pipe.maxpacket << QH_MAXPACKET_SHIFT)
393 | (pipe->pipe.speed << QH_SPEED_SHIFT)
394 | (pipe->pipe.ep << QH_EP_SHIFT)
395 | (pipe->pipe.devaddr << QH_DEVADDR_SHIFT));
Kevin O'Connor8915ed22012-03-10 10:09:56 -0500396
397 pipe->qh.info2 = (1 << QH_MULT_SHIFT);
398 struct usbdevice_s *hubdev = usbdev->hub->usbdev;
399 if (hubdev) {
400 struct ehci_pipe *hpipe = container_of(
401 hubdev->defpipe, struct ehci_pipe, pipe);
402 if (hpipe->pipe.speed == USB_HIGHSPEED)
403 pipe->qh.info2 |= ((usbdev->port << QH_HUBPORT_SHIFT)
404 | (hpipe->pipe.devaddr << QH_HUBADDR_SHIFT));
405 else
406 pipe->qh.info2 = hpipe->qh.info2;
407 }
408
409 u8 eptype = epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
410 if (eptype == USB_ENDPOINT_XFER_CONTROL)
411 pipe->qh.info1 |= ((pipe->pipe.speed != USB_HIGHSPEED ? QH_CONTROL : 0)
412 | QH_TOGGLECONTROL);
413 else if (eptype == USB_ENDPOINT_XFER_INT)
414 pipe->qh.info2 |= (0x01 << QH_SMASK_SHIFT) | (0x1c << QH_CMASK_SHIFT);
415}
416
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500417static struct usb_pipe *
Kevin O'Connor6152c322012-03-10 08:53:58 -0500418ehci_alloc_intr_pipe(struct usbdevice_s *usbdev
419 , struct usb_endpoint_descriptor *epdesc)
Kevin O'Connor49ecf692011-11-19 14:21:51 -0500420{
Kevin O'Connor6152c322012-03-10 08:53:58 -0500421 struct usb_ehci_s *cntl = container_of(
422 usbdev->hub->cntl, struct usb_ehci_s, usb);
423 int frameexp = usb_getFrameExp(usbdev, epdesc);
424 dprintf(7, "ehci_alloc_intr_pipe %p %d\n", &cntl->usb, frameexp);
Kevin O'Connor49ecf692011-11-19 14:21:51 -0500425
Kevin O'Connor6152c322012-03-10 08:53:58 -0500426 if (frameexp > 10)
427 frameexp = 10;
428 int maxpacket = epdesc->wMaxPacketSize;
429 // Determine number of entries needed for 2 timer ticks.
430 int ms = 1<<frameexp;
Kevin O'Connor69013372013-07-20 12:08:48 -0400431 int count = DIV_ROUND_UP(ticks_to_ms(2), ms);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500432 struct ehci_pipe *pipe = memalign_low(EHCI_QH_ALIGN, sizeof(*pipe));
433 struct ehci_qtd *tds = memalign_low(EHCI_QTD_ALIGN, sizeof(*tds) * count);
434 void *data = malloc_low(maxpacket * count);
435 if (!pipe || !tds || !data) {
436 warn_noalloc();
437 goto fail;
Kevin O'Connor49ecf692011-11-19 14:21:51 -0500438 }
Kevin O'Connor6152c322012-03-10 08:53:58 -0500439 memset(pipe, 0, sizeof(*pipe));
Kevin O'Connor8915ed22012-03-10 10:09:56 -0500440 ehci_desc2pipe(pipe, usbdev, epdesc);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500441 pipe->next_td = pipe->tds = tds;
442 pipe->data = data;
Kevin O'Connor6152c322012-03-10 08:53:58 -0500443 pipe->qh.qtd_next = (u32)tds;
444
445 int i;
446 for (i=0; i<count; i++) {
447 struct ehci_qtd *td = &tds[i];
448 td->qtd_next = (i==count-1 ? (u32)tds : (u32)&td[1]);
449 td->alt_next = EHCI_PTR_TERM;
450 td->token = (ehci_explen(maxpacket) | QTD_STS_ACTIVE
451 | QTD_PID_IN | ehci_maxerr(3));
452 td->buf[0] = (u32)data + maxpacket * i;
Kevin O'Connor49ecf692011-11-19 14:21:51 -0500453 }
Kevin O'Connor6152c322012-03-10 08:53:58 -0500454
455 // Add to interrupt schedule.
456 struct ehci_framelist *fl = (void*)readl(&cntl->regs->periodiclistbase);
457 if (frameexp == 0) {
458 // Add to existing interrupt entry.
459 struct ehci_qh *intr_qh = (void*)(fl->links[0] & ~EHCI_PTR_BITS);
460 pipe->qh.next = intr_qh->next;
461 barrier();
462 intr_qh->next = (u32)&pipe->qh | EHCI_PTR_QH;
463 } else {
464 int startpos = 1<<(frameexp-1);
465 pipe->qh.next = fl->links[startpos];
466 barrier();
467 for (i=startpos; i<ARRAY_SIZE(fl->links); i+=ms)
468 fl->links[i] = (u32)&pipe->qh | EHCI_PTR_QH;
469 }
470
471 return &pipe->pipe;
472fail:
473 free(pipe);
474 free(tds);
475 free(data);
476 return NULL;
Kevin O'Connor49ecf692011-11-19 14:21:51 -0500477}
Kevin O'Connor190cc622010-03-09 19:43:52 -0500478
Kevin O'Connor01de9bd2012-03-05 22:41:21 -0500479struct usb_pipe *
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500480ehci_alloc_pipe(struct usbdevice_s *usbdev
481 , struct usb_endpoint_descriptor *epdesc)
Kevin O'Connor190cc622010-03-09 19:43:52 -0500482{
483 if (! CONFIG_USB_EHCI)
484 return NULL;
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500485 u8 eptype = epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
486 if (eptype == USB_ENDPOINT_XFER_INT)
487 return ehci_alloc_intr_pipe(usbdev, epdesc);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500488 struct usb_ehci_s *cntl = container_of(
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500489 usbdev->hub->cntl, struct usb_ehci_s, usb);
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500490 dprintf(7, "ehci_alloc_async_pipe %p %d\n", &cntl->usb, eptype);
491
492 struct usb_pipe *usbpipe = usb_getFreePipe(&cntl->usb, eptype);
493 if (usbpipe) {
494 // Use previously allocated pipe.
Kevin O'Connor8915ed22012-03-10 10:09:56 -0500495 struct ehci_pipe *pipe = container_of(usbpipe, struct ehci_pipe, pipe);
496 ehci_desc2pipe(pipe, usbdev, epdesc);
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500497 return usbpipe;
498 }
Kevin O'Connor402f9a02012-02-20 12:54:49 -0500499
500 // Allocate a new queue head.
501 struct ehci_pipe *pipe;
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500502 if (eptype == USB_ENDPOINT_XFER_CONTROL)
Kevin O'Connor402f9a02012-02-20 12:54:49 -0500503 pipe = memalign_tmphigh(EHCI_QH_ALIGN, sizeof(*pipe));
504 else
505 pipe = memalign_low(EHCI_QH_ALIGN, sizeof(*pipe));
Kevin O'Connor190cc622010-03-09 19:43:52 -0500506 if (!pipe) {
507 warn_noalloc();
508 return NULL;
509 }
510 memset(pipe, 0, sizeof(*pipe));
Kevin O'Connor8915ed22012-03-10 10:09:56 -0500511 ehci_desc2pipe(pipe, usbdev, epdesc);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500512 pipe->qh.qtd_next = pipe->qh.alt_next = EHCI_PTR_TERM;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500513
514 // Add queue head to controller list.
515 struct ehci_qh *async_qh = cntl->async_qh;
516 pipe->qh.next = async_qh->next;
517 barrier();
518 async_qh->next = (u32)&pipe->qh | EHCI_PTR_QH;
519 return &pipe->pipe;
520}
521
Kevin O'Connor6152c322012-03-10 08:53:58 -0500522static void
523ehci_reset_pipe(struct ehci_pipe *pipe)
524{
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400525 SET_LOWFLAT(pipe->qh.qtd_next, EHCI_PTR_TERM);
526 SET_LOWFLAT(pipe->qh.alt_next, EHCI_PTR_TERM);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500527 barrier();
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400528 SET_LOWFLAT(pipe->qh.token, GET_LOWFLAT(pipe->qh.token) & QTD_TOGGLE);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500529}
530
531static int
532ehci_wait_td(struct ehci_pipe *pipe, struct ehci_qtd *td, int timeout)
533{
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400534 u32 end = timer_calc(timeout);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500535 u32 status;
536 for (;;) {
537 status = td->token;
538 if (!(status & QTD_STS_ACTIVE))
539 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400540 if (timer_check(end)) {
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400541 u32 cur = GET_LOWFLAT(pipe->qh.current);
542 u32 tok = GET_LOWFLAT(pipe->qh.token);
543 u32 next = GET_LOWFLAT(pipe->qh.qtd_next);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500544 warn_timeout();
545 dprintf(1, "ehci pipe=%p cur=%08x tok=%08x next=%x td=%p status=%x\n"
546 , pipe, cur, tok, next, td, status);
547 ehci_reset_pipe(pipe);
548 struct usb_ehci_s *cntl = container_of(
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400549 GET_LOWFLAT(pipe->pipe.cntl), struct usb_ehci_s, usb);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500550 ehci_waittick(cntl);
551 return -1;
552 }
553 yield();
554 }
555 if (status & QTD_STS_HALT) {
556 dprintf(1, "ehci_wait_td error - status=%x\n", status);
557 ehci_reset_pipe(pipe);
558 return -2;
559 }
560 return 0;
561}
562
Kevin O'Connor190cc622010-03-09 19:43:52 -0500563static int
564fillTDbuffer(struct ehci_qtd *td, u16 maxpacket, const void *buf, int bytes)
565{
566 u32 dest = (u32)buf;
567 u32 *pos = td->buf;
568 while (bytes) {
569 if (pos >= &td->buf[ARRAY_SIZE(td->buf)])
570 // More data than can transfer in a single qtd - only use
571 // full packets to prevent a babble error.
572 return ALIGN_DOWN(dest - (u32)buf, maxpacket);
573 u32 count = bytes;
574 u32 max = 0x1000 - (dest & 0xfff);
575 if (count > max)
576 count = max;
577 *pos = dest;
578 bytes -= count;
579 dest += count;
580 pos++;
581 }
582 return dest - (u32)buf;
583}
584
585int
586ehci_control(struct usb_pipe *p, int dir, const void *cmd, int cmdsize
587 , void *data, int datasize)
588{
589 ASSERT32FLAT();
590 if (! CONFIG_USB_EHCI)
591 return -1;
Kevin O'Connor49ecf692011-11-19 14:21:51 -0500592 dprintf(5, "ehci_control %p (dir=%d cmd=%d data=%d)\n"
593 , p, dir, cmdsize, datasize);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500594 if (datasize > 4*4096 || cmdsize > 4*4096) {
595 // XXX - should support larger sizes.
596 warn_noalloc();
597 return -1;
598 }
599 struct ehci_pipe *pipe = container_of(p, struct ehci_pipe, pipe);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500600
Kevin O'Connor190cc622010-03-09 19:43:52 -0500601 // Setup transfer descriptors
602 struct ehci_qtd *tds = memalign_tmphigh(EHCI_QTD_ALIGN, sizeof(*tds) * 3);
603 if (!tds) {
604 warn_noalloc();
605 return -1;
606 }
607 memset(tds, 0, sizeof(*tds) * 3);
608 struct ehci_qtd *td = tds;
609
610 td->qtd_next = (u32)&td[1];
611 td->alt_next = EHCI_PTR_TERM;
612 td->token = (ehci_explen(cmdsize) | QTD_STS_ACTIVE
613 | QTD_PID_SETUP | ehci_maxerr(3));
Kevin O'Connor8915ed22012-03-10 10:09:56 -0500614 u16 maxpacket = pipe->pipe.maxpacket;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500615 fillTDbuffer(td, maxpacket, cmd, cmdsize);
616 td++;
617
618 if (datasize) {
619 td->qtd_next = (u32)&td[1];
620 td->alt_next = EHCI_PTR_TERM;
621 td->token = (QTD_TOGGLE | ehci_explen(datasize) | QTD_STS_ACTIVE
622 | (dir ? QTD_PID_IN : QTD_PID_OUT) | ehci_maxerr(3));
623 fillTDbuffer(td, maxpacket, data, datasize);
624 td++;
625 }
626
627 td->qtd_next = EHCI_PTR_TERM;
628 td->alt_next = EHCI_PTR_TERM;
629 td->token = (QTD_TOGGLE | QTD_STS_ACTIVE
630 | (dir ? QTD_PID_OUT : QTD_PID_IN) | ehci_maxerr(3));
631
632 // Transfer data
633 barrier();
634 pipe->qh.qtd_next = (u32)tds;
Kevin O'Connor49ecf692011-11-19 14:21:51 -0500635 int i, ret=0;
636 for (i=0; i<3; i++) {
637 struct ehci_qtd *td = &tds[i];
638 ret = ehci_wait_td(pipe, td, 500);
639 if (ret)
640 break;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500641 }
642 free(tds);
643 return ret;
644}
645
Kevin O'Connor190cc622010-03-09 19:43:52 -0500646#define STACKQTDS 4
647
648int
649ehci_send_bulk(struct usb_pipe *p, int dir, void *data, int datasize)
650{
651 if (! CONFIG_USB_EHCI)
652 return -1;
653 struct ehci_pipe *pipe = container_of(p, struct ehci_pipe, pipe);
654 dprintf(7, "ehci_send_bulk qh=%p dir=%d data=%p size=%d\n"
655 , &pipe->qh, dir, data, datasize);
656
Kevin O'Connor8915ed22012-03-10 10:09:56 -0500657 // Allocate 4 tds on stack (with required alignment)
Kevin O'Connor190cc622010-03-09 19:43:52 -0500658 u8 tdsbuf[sizeof(struct ehci_qtd) * STACKQTDS + EHCI_QTD_ALIGN - 1];
659 struct ehci_qtd *tds = (void*)ALIGN((u32)tdsbuf, EHCI_QTD_ALIGN);
660 memset(tds, 0, sizeof(*tds) * STACKQTDS);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500661 barrier();
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400662 SET_LOWFLAT(pipe->qh.qtd_next, (u32)MAKE_FLATPTR(GET_SEG(SS), tds));
Kevin O'Connor190cc622010-03-09 19:43:52 -0500663
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400664 u16 maxpacket = GET_LOWFLAT(pipe->pipe.maxpacket);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500665 int tdpos = 0;
666 while (datasize) {
667 struct ehci_qtd *td = &tds[tdpos++ % STACKQTDS];
Kevin O'Connor49ecf692011-11-19 14:21:51 -0500668 int ret = ehci_wait_td(pipe, td, 5000);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500669 if (ret)
Kevin O'Connor49ecf692011-11-19 14:21:51 -0500670 return -1;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500671
672 struct ehci_qtd *nexttd_fl = MAKE_FLATPTR(GET_SEG(SS)
673 , &tds[tdpos % STACKQTDS]);
674
675 int transfer = fillTDbuffer(td, maxpacket, data, datasize);
676 td->qtd_next = (transfer==datasize ? EHCI_PTR_TERM : (u32)nexttd_fl);
677 td->alt_next = EHCI_PTR_TERM;
678 barrier();
679 td->token = (ehci_explen(transfer) | QTD_STS_ACTIVE
680 | (dir ? QTD_PID_IN : QTD_PID_OUT) | ehci_maxerr(3));
681
682 data += transfer;
683 datasize -= transfer;
684 }
685 int i;
686 for (i=0; i<STACKQTDS; i++) {
687 struct ehci_qtd *td = &tds[tdpos++ % STACKQTDS];
Kevin O'Connor49ecf692011-11-19 14:21:51 -0500688 int ret = ehci_wait_td(pipe, td, 5000);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500689 if (ret)
Kevin O'Connor49ecf692011-11-19 14:21:51 -0500690 return -1;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500691 }
692
693 return 0;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500694}
695
Kevin O'Connor190cc622010-03-09 19:43:52 -0500696int
697ehci_poll_intr(struct usb_pipe *p, void *data)
698{
699 ASSERT16();
700 if (! CONFIG_USB_EHCI)
701 return -1;
702 struct ehci_pipe *pipe = container_of(p, struct ehci_pipe, pipe);
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400703 struct ehci_qtd *td = GET_LOWFLAT(pipe->next_td);
704 u32 token = GET_LOWFLAT(td->token);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500705 if (token & QTD_STS_ACTIVE)
706 // No intrs found.
707 return -1;
708 // XXX - check for errors.
709
710 // Copy data.
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400711 int maxpacket = GET_LOWFLAT(pipe->pipe.maxpacket);
712 int pos = td - GET_LOWFLAT(pipe->tds);
713 void *tddata = GET_LOWFLAT(pipe->data) + maxpacket * pos;
714 memcpy_far(GET_SEG(SS), data, SEG_LOW, LOWFLAT2LOW(tddata), maxpacket);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500715
716 // Reenable this td.
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400717 struct ehci_qtd *next = (void*)(GET_LOWFLAT(td->qtd_next) & ~EHCI_PTR_BITS);
718 SET_LOWFLAT(pipe->next_td, next);
719 SET_LOWFLAT(td->buf[0], (u32)tddata);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500720 barrier();
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400721 SET_LOWFLAT(td->token, (ehci_explen(maxpacket) | QTD_STS_ACTIVE
Kevin O'Connor190cc622010-03-09 19:43:52 -0500722 | QTD_PID_IN | ehci_maxerr(3)));
723
724 return 0;
725}