blob: 978ca687b01be750b06ffb42d4232d43ed9365a4 [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'Connor190cc622010-03-09 19:43:52 -050018
Kevin O'Connor190cc622010-03-09 19:43:52 -050019struct usb_ehci_s {
20 struct usb_s usb;
21 struct ehci_caps *caps;
22 struct ehci_regs *regs;
23 struct ehci_qh *async_qh;
Kevin O'Connor8ff8e012011-07-09 14:11:21 -040024 struct pci_device *companion[8];
Kevin O'Connor190cc622010-03-09 19:43:52 -050025 int checkports;
26 int legacycount;
27};
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'Connor190cc622010-03-09 19:43:52 -050036
37/****************************************************************
38 * Root hub
39 ****************************************************************/
40
41#define EHCI_TIME_POSTPOWER 20
42#define EHCI_TIME_POSTRESET 2
43
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040044// Check if need companion controllers for full/low speed devices
Kevin O'Connor190cc622010-03-09 19:43:52 -050045static void
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040046ehci_note_port(struct usb_ehci_s *cntl)
Kevin O'Connor190cc622010-03-09 19:43:52 -050047{
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040048 if (--cntl->checkports)
49 // Ports still being detected.
50 return;
Kevin O'Connor190cc622010-03-09 19:43:52 -050051 if (! cntl->legacycount)
52 // No full/low speed devices found.
53 return;
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040054 // Start companion controllers.
Kevin O'Connor190cc622010-03-09 19:43:52 -050055 int i;
56 for (i=0; i<ARRAY_SIZE(cntl->companion); i++) {
Kevin O'Connor8ff8e012011-07-09 14:11:21 -040057 struct pci_device *pci = cntl->companion[i];
58 if (!pci)
59 break;
Kevin O'Connor6f500512011-07-10 15:41:55 -040060
Kevin O'Connord83c87b2013-01-21 01:14:12 -050061 // ohci/uhci_setup call pci_config_X - don't run from irq handler.
Kevin O'Connor6f500512011-07-10 15:41:55 -040062 wait_preempt();
63
Kevin O'Connor8ff8e012011-07-09 14:11:21 -040064 if (pci_classprog(pci) == PCI_CLASS_SERIAL_USB_UHCI)
Kevin O'Connord83c87b2013-01-21 01:14:12 -050065 uhci_setup(pci, cntl->usb.busid + i);
Kevin O'Connor8ff8e012011-07-09 14:11:21 -040066 else if (pci_classprog(pci) == PCI_CLASS_SERIAL_USB_OHCI)
Kevin O'Connord83c87b2013-01-21 01:14:12 -050067 ohci_setup(pci, cntl->usb.busid + i);
Kevin O'Connor190cc622010-03-09 19:43:52 -050068 }
69}
70
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040071// Check if device attached to port
72static int
73ehci_hub_detect(struct usbhub_s *hub, u32 port)
Kevin O'Connor190cc622010-03-09 19:43:52 -050074{
Kevin O'Connor190cc622010-03-09 19:43:52 -050075 struct usb_ehci_s *cntl = container_of(hub->cntl, struct usb_ehci_s, usb);
Kevin O'Connor190cc622010-03-09 19:43:52 -050076 u32 *portreg = &cntl->regs->portsc[port];
77 u32 portsc = readl(portreg);
78
79 // Power up port.
80 if (!(portsc & PORT_POWER)) {
81 portsc |= PORT_POWER;
82 writel(portreg, portsc);
83 msleep(EHCI_TIME_POSTPOWER);
Kevin O'Connor87ab2fb2010-03-20 23:25:11 -040084 } else {
Kevin O'Connorc0207882012-08-01 20:40:03 -040085 // Port is already powered up, but we don't know how long it
86 // has been powered up, so wait the 20ms.
87 msleep(EHCI_TIME_POSTPOWER);
Kevin O'Connor190cc622010-03-09 19:43:52 -050088 }
Kevin O'Connor87ab2fb2010-03-20 23:25:11 -040089 portsc = readl(portreg);
Kevin O'Connor190cc622010-03-09 19:43:52 -050090
91 if (!(portsc & PORT_CONNECT))
92 // No device present
Kevin O'Connor87ab2fb2010-03-20 23:25:11 -040093 goto doneearly;
Kevin O'Connor190cc622010-03-09 19:43:52 -050094
95 if ((portsc & PORT_LINESTATUS_MASK) == PORT_LINESTATUS_KSTATE) {
96 // low speed device
97 cntl->legacycount++;
98 writel(portreg, portsc | PORT_OWNER);
Kevin O'Connor87ab2fb2010-03-20 23:25:11 -040099 goto doneearly;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500100 }
101
102 // XXX - if just powered up, need to wait for USB_TIME_ATTDB?
103
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400104 // Begin reset on port
Kevin O'Connor190cc622010-03-09 19:43:52 -0500105 portsc = (portsc & ~PORT_PE) | PORT_RESET;
106 writel(portreg, portsc);
107 msleep(USB_TIME_DRSTR);
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400108 return 0;
109
110doneearly:
111 ehci_note_port(cntl);
112 return -1;
113}
114
115// Reset device on port
116static int
117ehci_hub_reset(struct usbhub_s *hub, u32 port)
118{
119 struct usb_ehci_s *cntl = container_of(hub->cntl, struct usb_ehci_s, usb);
120 u32 *portreg = &cntl->regs->portsc[port];
121 u32 portsc = readl(portreg);
122
123 // Finish reset on port
Kevin O'Connor190cc622010-03-09 19:43:52 -0500124 portsc &= ~PORT_RESET;
125 writel(portreg, portsc);
126 msleep(EHCI_TIME_POSTRESET);
127
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400128 int rv = -1;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500129 portsc = readl(portreg);
130 if (!(portsc & PORT_CONNECT))
131 // No longer connected
132 goto resetfail;
133 if (!(portsc & PORT_PE)) {
134 // full speed device
135 cntl->legacycount++;
136 writel(portreg, portsc | PORT_OWNER);
137 goto resetfail;
138 }
Kevin O'Connor87ab2fb2010-03-20 23:25:11 -0400139
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400140 rv = USB_HIGHSPEED;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500141resetfail:
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400142 ehci_note_port(cntl);
143 return rv;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500144}
145
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400146// Disable port
147static void
148ehci_hub_disconnect(struct usbhub_s *hub, u32 port)
149{
150 struct usb_ehci_s *cntl = container_of(hub->cntl, struct usb_ehci_s, usb);
151 u32 *portreg = &cntl->regs->portsc[port];
152 u32 portsc = readl(portreg);
153 writel(portreg, portsc & ~PORT_PE);
154}
155
156static struct usbhub_op_s ehci_HubOp = {
157 .detect = ehci_hub_detect,
158 .reset = ehci_hub_reset,
159 .disconnect = ehci_hub_disconnect,
160};
161
Kevin O'Connor190cc622010-03-09 19:43:52 -0500162// Find any devices connected to the root hub.
163static int
164check_ehci_ports(struct usb_ehci_s *cntl)
165{
166 ASSERT32FLAT();
Kevin O'Connor190cc622010-03-09 19:43:52 -0500167 struct usbhub_s hub;
168 memset(&hub, 0, sizeof(hub));
169 hub.cntl = &cntl->usb;
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400170 hub.portcount = cntl->checkports;
171 hub.op = &ehci_HubOp;
172 usb_enumerate(&hub);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500173 return hub.devcount;
174}
175
176
177/****************************************************************
178 * Setup
179 ****************************************************************/
180
Kevin O'Connor402f9a02012-02-20 12:54:49 -0500181// Wait for next USB async frame to start - for ensuring safe memory release.
182static void
183ehci_waittick(struct usb_ehci_s *cntl)
184{
185 if (MODE16) {
186 msleep(10);
187 return;
188 }
189 // Wait for access to "doorbell"
190 barrier();
191 u32 cmd, sts;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400192 u32 end = timer_calc(100);
Kevin O'Connor402f9a02012-02-20 12:54:49 -0500193 for (;;) {
194 sts = readl(&cntl->regs->usbsts);
195 if (!(sts & STS_IAA)) {
196 cmd = readl(&cntl->regs->usbcmd);
197 if (!(cmd & CMD_IAAD))
198 break;
199 }
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400200 if (timer_check(end)) {
Kevin O'Connor402f9a02012-02-20 12:54:49 -0500201 warn_timeout();
202 return;
203 }
204 yield();
205 }
206 // Ring "doorbell"
207 writel(&cntl->regs->usbcmd, cmd | CMD_IAAD);
208 // Wait for completion
209 for (;;) {
210 sts = readl(&cntl->regs->usbsts);
211 if (sts & STS_IAA)
212 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400213 if (timer_check(end)) {
Kevin O'Connor402f9a02012-02-20 12:54:49 -0500214 warn_timeout();
215 return;
216 }
217 yield();
218 }
219 // Ack completion
220 writel(&cntl->regs->usbsts, STS_IAA);
221}
222
223static void
224ehci_free_pipes(struct usb_ehci_s *cntl)
225{
226 dprintf(7, "ehci_free_pipes %p\n", cntl);
227
228 struct ehci_qh *start = cntl->async_qh;
229 struct ehci_qh *pos = start;
230 for (;;) {
231 struct ehci_qh *next = (void*)(pos->next & ~EHCI_PTR_BITS);
232 if (next == start)
233 break;
234 struct ehci_pipe *pipe = container_of(next, struct ehci_pipe, qh);
235 if (pipe->pipe.cntl != &cntl->usb)
236 pos->next = next->next;
237 else
238 pos = next;
239 }
240 ehci_waittick(cntl);
241 for (;;) {
242 struct usb_pipe *usbpipe = cntl->usb.freelist;
243 if (!usbpipe)
244 break;
245 cntl->usb.freelist = usbpipe->freenext;
246 struct ehci_pipe *pipe = container_of(usbpipe, struct ehci_pipe, pipe);
247 free(pipe);
248 }
249}
250
Kevin O'Connor190cc622010-03-09 19:43:52 -0500251static void
252configure_ehci(void *data)
253{
254 struct usb_ehci_s *cntl = data;
255
256 // Allocate ram for schedule storage
257 struct ehci_framelist *fl = memalign_high(sizeof(*fl), sizeof(*fl));
258 struct ehci_qh *intr_qh = memalign_high(EHCI_QH_ALIGN, sizeof(*intr_qh));
259 struct ehci_qh *async_qh = memalign_high(EHCI_QH_ALIGN, sizeof(*async_qh));
260 if (!fl || !intr_qh || !async_qh) {
261 warn_noalloc();
262 goto fail;
263 }
264
265 // XXX - check for halted?
266
267 // Reset the HC
268 u32 cmd = readl(&cntl->regs->usbcmd);
269 writel(&cntl->regs->usbcmd, (cmd & ~(CMD_ASE | CMD_PSE)) | CMD_HCRESET);
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400270 u32 end = timer_calc(250);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500271 for (;;) {
272 cmd = readl(&cntl->regs->usbcmd);
273 if (!(cmd & CMD_HCRESET))
274 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400275 if (timer_check(end)) {
Kevin O'Connor190cc622010-03-09 19:43:52 -0500276 warn_timeout();
277 goto fail;
278 }
Kevin O'Connor698d3f92010-04-17 16:59:12 -0400279 yield();
Kevin O'Connor190cc622010-03-09 19:43:52 -0500280 }
281
282 // Disable interrupts (just to be safe).
283 writel(&cntl->regs->usbintr, 0);
284
285 // Set schedule to point to primary intr queue head
286 memset(intr_qh, 0, sizeof(*intr_qh));
287 intr_qh->next = EHCI_PTR_TERM;
288 intr_qh->info2 = (0x01 << QH_SMASK_SHIFT);
289 intr_qh->token = QTD_STS_HALT;
290 intr_qh->qtd_next = intr_qh->alt_next = EHCI_PTR_TERM;
291 int i;
292 for (i=0; i<ARRAY_SIZE(fl->links); i++)
293 fl->links[i] = (u32)intr_qh | EHCI_PTR_QH;
294 writel(&cntl->regs->periodiclistbase, (u32)fl);
295
296 // Set async list to point to primary async queue head
297 memset(async_qh, 0, sizeof(*async_qh));
298 async_qh->next = (u32)async_qh | EHCI_PTR_QH;
299 async_qh->info1 = QH_HEAD;
300 async_qh->token = QTD_STS_HALT;
301 async_qh->qtd_next = async_qh->alt_next = EHCI_PTR_TERM;
302 cntl->async_qh = async_qh;
303 writel(&cntl->regs->asynclistbase, (u32)async_qh);
304
305 // Enable queues
306 writel(&cntl->regs->usbcmd, cmd | CMD_ASE | CMD_PSE | CMD_RUN);
307
308 // Set default of high speed for root hub.
309 writel(&cntl->regs->configflag, 1);
310 cntl->checkports = readl(&cntl->caps->hcsparams) & HCS_N_PORTS_MASK;
311
312 // Find devices
313 int count = check_ehci_ports(cntl);
Kevin O'Connor402f9a02012-02-20 12:54:49 -0500314 ehci_free_pipes(cntl);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500315 if (count)
316 // Success
317 return;
318
319 // No devices found - shutdown and free controller.
320 writel(&cntl->regs->usbcmd, cmd & ~CMD_RUN);
321 msleep(4); // 2ms to stop reading memory - XXX
322fail:
323 free(fl);
324 free(intr_qh);
325 free(async_qh);
326 free(cntl);
327}
328
329int
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500330ehci_setup(struct pci_device *pci, int busid, struct pci_device *comppci)
Kevin O'Connor190cc622010-03-09 19:43:52 -0500331{
332 if (! CONFIG_USB_EHCI)
333 return -1;
334
Kevin O'Connor8ff8e012011-07-09 14:11:21 -0400335 u16 bdf = pci->bdf;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500336 u32 baseaddr = pci_config_readl(bdf, PCI_BASE_ADDRESS_0);
337 struct ehci_caps *caps = (void*)(baseaddr & PCI_BASE_ADDRESS_MEM_MASK);
338 u32 hcc_params = readl(&caps->hccparams);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500339
340 struct usb_ehci_s *cntl = malloc_tmphigh(sizeof(*cntl));
Kevin O'Connorffdcd3a2011-07-10 15:48:00 -0400341 if (!cntl) {
342 warn_noalloc();
343 return -1;
344 }
Kevin O'Connor190cc622010-03-09 19:43:52 -0500345 memset(cntl, 0, sizeof(*cntl));
346 cntl->usb.busid = busid;
Kevin O'Connor8ff8e012011-07-09 14:11:21 -0400347 cntl->usb.pci = pci;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500348 cntl->usb.type = USB_TYPE_EHCI;
349 cntl->caps = caps;
Avik Sil7cac6002013-02-14 10:54:57 +0530350 cntl->regs = (void*)caps + readb(&caps->caplength);
Sven Schnellea3fa66c2012-06-12 09:20:23 +0200351 if (hcc_params & HCC_64BIT_ADDR)
352 cntl->regs->ctrldssegment = 0;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500353
Kevin O'Connor9dc243e2010-03-20 17:53:03 -0400354 dprintf(1, "EHCI init on dev %02x:%02x.%x (regs=%p)\n"
Kevin O'Connor190cc622010-03-09 19:43:52 -0500355 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf)
356 , pci_bdf_to_fn(bdf), cntl->regs);
357
358 pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER);
359
360 // XXX - check for and disable SMM control?
361
362 // Find companion controllers.
363 int count = 0;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500364 for (;;) {
Kevin O'Connor8ff8e012011-07-09 14:11:21 -0400365 if (!comppci || comppci == pci)
Kevin O'Connor190cc622010-03-09 19:43:52 -0500366 break;
Kevin O'Connor8ff8e012011-07-09 14:11:21 -0400367 if (pci_classprog(comppci) == PCI_CLASS_SERIAL_USB_UHCI)
368 cntl->companion[count++] = comppci;
369 else if (pci_classprog(comppci) == PCI_CLASS_SERIAL_USB_OHCI)
370 cntl->companion[count++] = comppci;
Kevin O'Connorb98a4b12013-06-08 21:53:36 -0400371 comppci = container_of(comppci->node.next, struct pci_device, node);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500372 }
373
374 run_thread(configure_ehci, cntl);
375 return 0;
376}
377
378
379/****************************************************************
380 * End point communication
381 ****************************************************************/
382
Kevin O'Connor8915ed22012-03-10 10:09:56 -0500383// Setup fields in qh
384static void
385ehci_desc2pipe(struct ehci_pipe *pipe, struct usbdevice_s *usbdev
386 , struct usb_endpoint_descriptor *epdesc)
387{
388 usb_desc2pipe(&pipe->pipe, usbdev, epdesc);
389
Kevin O'Connor32d68ab2012-03-10 21:07:26 -0500390 pipe->qh.info1 = ((pipe->pipe.maxpacket << QH_MAXPACKET_SHIFT)
391 | (pipe->pipe.speed << QH_SPEED_SHIFT)
392 | (pipe->pipe.ep << QH_EP_SHIFT)
393 | (pipe->pipe.devaddr << QH_DEVADDR_SHIFT));
Kevin O'Connor8915ed22012-03-10 10:09:56 -0500394
395 pipe->qh.info2 = (1 << QH_MULT_SHIFT);
396 struct usbdevice_s *hubdev = usbdev->hub->usbdev;
397 if (hubdev) {
398 struct ehci_pipe *hpipe = container_of(
399 hubdev->defpipe, struct ehci_pipe, pipe);
400 if (hpipe->pipe.speed == USB_HIGHSPEED)
401 pipe->qh.info2 |= ((usbdev->port << QH_HUBPORT_SHIFT)
402 | (hpipe->pipe.devaddr << QH_HUBADDR_SHIFT));
403 else
404 pipe->qh.info2 = hpipe->qh.info2;
405 }
406
407 u8 eptype = epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
408 if (eptype == USB_ENDPOINT_XFER_CONTROL)
409 pipe->qh.info1 |= ((pipe->pipe.speed != USB_HIGHSPEED ? QH_CONTROL : 0)
410 | QH_TOGGLECONTROL);
411 else if (eptype == USB_ENDPOINT_XFER_INT)
412 pipe->qh.info2 |= (0x01 << QH_SMASK_SHIFT) | (0x1c << QH_CMASK_SHIFT);
413}
414
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500415static struct usb_pipe *
Kevin O'Connor6152c322012-03-10 08:53:58 -0500416ehci_alloc_intr_pipe(struct usbdevice_s *usbdev
417 , struct usb_endpoint_descriptor *epdesc)
Kevin O'Connor49ecf692011-11-19 14:21:51 -0500418{
Kevin O'Connor6152c322012-03-10 08:53:58 -0500419 struct usb_ehci_s *cntl = container_of(
420 usbdev->hub->cntl, struct usb_ehci_s, usb);
421 int frameexp = usb_getFrameExp(usbdev, epdesc);
422 dprintf(7, "ehci_alloc_intr_pipe %p %d\n", &cntl->usb, frameexp);
Kevin O'Connor49ecf692011-11-19 14:21:51 -0500423
Kevin O'Connor6152c322012-03-10 08:53:58 -0500424 if (frameexp > 10)
425 frameexp = 10;
426 int maxpacket = epdesc->wMaxPacketSize;
427 // Determine number of entries needed for 2 timer ticks.
428 int ms = 1<<frameexp;
Kevin O'Connor69013372013-07-20 12:08:48 -0400429 int count = DIV_ROUND_UP(ticks_to_ms(2), ms);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500430 struct ehci_pipe *pipe = memalign_low(EHCI_QH_ALIGN, sizeof(*pipe));
431 struct ehci_qtd *tds = memalign_low(EHCI_QTD_ALIGN, sizeof(*tds) * count);
432 void *data = malloc_low(maxpacket * count);
433 if (!pipe || !tds || !data) {
434 warn_noalloc();
435 goto fail;
Kevin O'Connor49ecf692011-11-19 14:21:51 -0500436 }
Kevin O'Connor6152c322012-03-10 08:53:58 -0500437 memset(pipe, 0, sizeof(*pipe));
Kevin O'Connor8915ed22012-03-10 10:09:56 -0500438 ehci_desc2pipe(pipe, usbdev, epdesc);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500439 pipe->next_td = pipe->tds = tds;
440 pipe->data = data;
Kevin O'Connor6152c322012-03-10 08:53:58 -0500441 pipe->qh.qtd_next = (u32)tds;
442
443 int i;
444 for (i=0; i<count; i++) {
445 struct ehci_qtd *td = &tds[i];
446 td->qtd_next = (i==count-1 ? (u32)tds : (u32)&td[1]);
447 td->alt_next = EHCI_PTR_TERM;
448 td->token = (ehci_explen(maxpacket) | QTD_STS_ACTIVE
449 | QTD_PID_IN | ehci_maxerr(3));
450 td->buf[0] = (u32)data + maxpacket * i;
Kevin O'Connor49ecf692011-11-19 14:21:51 -0500451 }
Kevin O'Connor6152c322012-03-10 08:53:58 -0500452
453 // Add to interrupt schedule.
454 struct ehci_framelist *fl = (void*)readl(&cntl->regs->periodiclistbase);
455 if (frameexp == 0) {
456 // Add to existing interrupt entry.
457 struct ehci_qh *intr_qh = (void*)(fl->links[0] & ~EHCI_PTR_BITS);
458 pipe->qh.next = intr_qh->next;
459 barrier();
460 intr_qh->next = (u32)&pipe->qh | EHCI_PTR_QH;
461 } else {
462 int startpos = 1<<(frameexp-1);
463 pipe->qh.next = fl->links[startpos];
464 barrier();
465 for (i=startpos; i<ARRAY_SIZE(fl->links); i+=ms)
466 fl->links[i] = (u32)&pipe->qh | EHCI_PTR_QH;
467 }
468
469 return &pipe->pipe;
470fail:
471 free(pipe);
472 free(tds);
473 free(data);
474 return NULL;
Kevin O'Connor49ecf692011-11-19 14:21:51 -0500475}
Kevin O'Connor190cc622010-03-09 19:43:52 -0500476
Kevin O'Connor01de9bd2012-03-05 22:41:21 -0500477struct usb_pipe *
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500478ehci_alloc_pipe(struct usbdevice_s *usbdev
479 , struct usb_endpoint_descriptor *epdesc)
Kevin O'Connor190cc622010-03-09 19:43:52 -0500480{
481 if (! CONFIG_USB_EHCI)
482 return NULL;
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500483 u8 eptype = epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
484 if (eptype == USB_ENDPOINT_XFER_INT)
485 return ehci_alloc_intr_pipe(usbdev, epdesc);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500486 struct usb_ehci_s *cntl = container_of(
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500487 usbdev->hub->cntl, struct usb_ehci_s, usb);
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500488 dprintf(7, "ehci_alloc_async_pipe %p %d\n", &cntl->usb, eptype);
489
490 struct usb_pipe *usbpipe = usb_getFreePipe(&cntl->usb, eptype);
491 if (usbpipe) {
492 // Use previously allocated pipe.
Kevin O'Connor8915ed22012-03-10 10:09:56 -0500493 struct ehci_pipe *pipe = container_of(usbpipe, struct ehci_pipe, pipe);
494 ehci_desc2pipe(pipe, usbdev, epdesc);
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500495 return usbpipe;
496 }
Kevin O'Connor402f9a02012-02-20 12:54:49 -0500497
498 // Allocate a new queue head.
499 struct ehci_pipe *pipe;
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500500 if (eptype == USB_ENDPOINT_XFER_CONTROL)
Kevin O'Connor402f9a02012-02-20 12:54:49 -0500501 pipe = memalign_tmphigh(EHCI_QH_ALIGN, sizeof(*pipe));
502 else
503 pipe = memalign_low(EHCI_QH_ALIGN, sizeof(*pipe));
Kevin O'Connor190cc622010-03-09 19:43:52 -0500504 if (!pipe) {
505 warn_noalloc();
506 return NULL;
507 }
508 memset(pipe, 0, sizeof(*pipe));
Kevin O'Connor8915ed22012-03-10 10:09:56 -0500509 ehci_desc2pipe(pipe, usbdev, epdesc);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500510 pipe->qh.qtd_next = pipe->qh.alt_next = EHCI_PTR_TERM;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500511
512 // Add queue head to controller list.
513 struct ehci_qh *async_qh = cntl->async_qh;
514 pipe->qh.next = async_qh->next;
515 barrier();
516 async_qh->next = (u32)&pipe->qh | EHCI_PTR_QH;
517 return &pipe->pipe;
518}
519
Kevin O'Connor6152c322012-03-10 08:53:58 -0500520static void
521ehci_reset_pipe(struct ehci_pipe *pipe)
522{
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400523 SET_LOWFLAT(pipe->qh.qtd_next, EHCI_PTR_TERM);
524 SET_LOWFLAT(pipe->qh.alt_next, EHCI_PTR_TERM);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500525 barrier();
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400526 SET_LOWFLAT(pipe->qh.token, GET_LOWFLAT(pipe->qh.token) & QTD_TOGGLE);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500527}
528
529static int
530ehci_wait_td(struct ehci_pipe *pipe, struct ehci_qtd *td, int timeout)
531{
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400532 u32 end = timer_calc(timeout);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500533 u32 status;
534 for (;;) {
535 status = td->token;
536 if (!(status & QTD_STS_ACTIVE))
537 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400538 if (timer_check(end)) {
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400539 u32 cur = GET_LOWFLAT(pipe->qh.current);
540 u32 tok = GET_LOWFLAT(pipe->qh.token);
541 u32 next = GET_LOWFLAT(pipe->qh.qtd_next);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500542 warn_timeout();
543 dprintf(1, "ehci pipe=%p cur=%08x tok=%08x next=%x td=%p status=%x\n"
544 , pipe, cur, tok, next, td, status);
545 ehci_reset_pipe(pipe);
546 struct usb_ehci_s *cntl = container_of(
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400547 GET_LOWFLAT(pipe->pipe.cntl), struct usb_ehci_s, usb);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500548 ehci_waittick(cntl);
549 return -1;
550 }
551 yield();
552 }
553 if (status & QTD_STS_HALT) {
554 dprintf(1, "ehci_wait_td error - status=%x\n", status);
555 ehci_reset_pipe(pipe);
556 return -2;
557 }
558 return 0;
559}
560
Kevin O'Connor190cc622010-03-09 19:43:52 -0500561static int
562fillTDbuffer(struct ehci_qtd *td, u16 maxpacket, const void *buf, int bytes)
563{
564 u32 dest = (u32)buf;
565 u32 *pos = td->buf;
566 while (bytes) {
567 if (pos >= &td->buf[ARRAY_SIZE(td->buf)])
568 // More data than can transfer in a single qtd - only use
569 // full packets to prevent a babble error.
570 return ALIGN_DOWN(dest - (u32)buf, maxpacket);
571 u32 count = bytes;
572 u32 max = 0x1000 - (dest & 0xfff);
573 if (count > max)
574 count = max;
575 *pos = dest;
576 bytes -= count;
577 dest += count;
578 pos++;
579 }
580 return dest - (u32)buf;
581}
582
583int
584ehci_control(struct usb_pipe *p, int dir, const void *cmd, int cmdsize
585 , void *data, int datasize)
586{
587 ASSERT32FLAT();
588 if (! CONFIG_USB_EHCI)
589 return -1;
Kevin O'Connor49ecf692011-11-19 14:21:51 -0500590 dprintf(5, "ehci_control %p (dir=%d cmd=%d data=%d)\n"
591 , p, dir, cmdsize, datasize);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500592 if (datasize > 4*4096 || cmdsize > 4*4096) {
593 // XXX - should support larger sizes.
594 warn_noalloc();
595 return -1;
596 }
597 struct ehci_pipe *pipe = container_of(p, struct ehci_pipe, pipe);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500598
Kevin O'Connor190cc622010-03-09 19:43:52 -0500599 // Setup transfer descriptors
600 struct ehci_qtd *tds = memalign_tmphigh(EHCI_QTD_ALIGN, sizeof(*tds) * 3);
601 if (!tds) {
602 warn_noalloc();
603 return -1;
604 }
605 memset(tds, 0, sizeof(*tds) * 3);
606 struct ehci_qtd *td = tds;
607
608 td->qtd_next = (u32)&td[1];
609 td->alt_next = EHCI_PTR_TERM;
610 td->token = (ehci_explen(cmdsize) | QTD_STS_ACTIVE
611 | QTD_PID_SETUP | ehci_maxerr(3));
Kevin O'Connor8915ed22012-03-10 10:09:56 -0500612 u16 maxpacket = pipe->pipe.maxpacket;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500613 fillTDbuffer(td, maxpacket, cmd, cmdsize);
614 td++;
615
616 if (datasize) {
617 td->qtd_next = (u32)&td[1];
618 td->alt_next = EHCI_PTR_TERM;
619 td->token = (QTD_TOGGLE | ehci_explen(datasize) | QTD_STS_ACTIVE
620 | (dir ? QTD_PID_IN : QTD_PID_OUT) | ehci_maxerr(3));
621 fillTDbuffer(td, maxpacket, data, datasize);
622 td++;
623 }
624
625 td->qtd_next = EHCI_PTR_TERM;
626 td->alt_next = EHCI_PTR_TERM;
627 td->token = (QTD_TOGGLE | QTD_STS_ACTIVE
628 | (dir ? QTD_PID_OUT : QTD_PID_IN) | ehci_maxerr(3));
629
630 // Transfer data
631 barrier();
632 pipe->qh.qtd_next = (u32)tds;
Kevin O'Connor49ecf692011-11-19 14:21:51 -0500633 int i, ret=0;
634 for (i=0; i<3; i++) {
635 struct ehci_qtd *td = &tds[i];
636 ret = ehci_wait_td(pipe, td, 500);
637 if (ret)
638 break;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500639 }
640 free(tds);
641 return ret;
642}
643
Kevin O'Connor190cc622010-03-09 19:43:52 -0500644#define STACKQTDS 4
645
646int
647ehci_send_bulk(struct usb_pipe *p, int dir, void *data, int datasize)
648{
649 if (! CONFIG_USB_EHCI)
650 return -1;
651 struct ehci_pipe *pipe = container_of(p, struct ehci_pipe, pipe);
652 dprintf(7, "ehci_send_bulk qh=%p dir=%d data=%p size=%d\n"
653 , &pipe->qh, dir, data, datasize);
654
Kevin O'Connor8915ed22012-03-10 10:09:56 -0500655 // Allocate 4 tds on stack (with required alignment)
Kevin O'Connor190cc622010-03-09 19:43:52 -0500656 u8 tdsbuf[sizeof(struct ehci_qtd) * STACKQTDS + EHCI_QTD_ALIGN - 1];
657 struct ehci_qtd *tds = (void*)ALIGN((u32)tdsbuf, EHCI_QTD_ALIGN);
658 memset(tds, 0, sizeof(*tds) * STACKQTDS);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500659 barrier();
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400660 SET_LOWFLAT(pipe->qh.qtd_next, (u32)MAKE_FLATPTR(GET_SEG(SS), tds));
Kevin O'Connor190cc622010-03-09 19:43:52 -0500661
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400662 u16 maxpacket = GET_LOWFLAT(pipe->pipe.maxpacket);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500663 int tdpos = 0;
664 while (datasize) {
665 struct ehci_qtd *td = &tds[tdpos++ % STACKQTDS];
Kevin O'Connor49ecf692011-11-19 14:21:51 -0500666 int ret = ehci_wait_td(pipe, td, 5000);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500667 if (ret)
Kevin O'Connor49ecf692011-11-19 14:21:51 -0500668 return -1;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500669
670 struct ehci_qtd *nexttd_fl = MAKE_FLATPTR(GET_SEG(SS)
671 , &tds[tdpos % STACKQTDS]);
672
673 int transfer = fillTDbuffer(td, maxpacket, data, datasize);
674 td->qtd_next = (transfer==datasize ? EHCI_PTR_TERM : (u32)nexttd_fl);
675 td->alt_next = EHCI_PTR_TERM;
676 barrier();
677 td->token = (ehci_explen(transfer) | QTD_STS_ACTIVE
678 | (dir ? QTD_PID_IN : QTD_PID_OUT) | ehci_maxerr(3));
679
680 data += transfer;
681 datasize -= transfer;
682 }
683 int i;
684 for (i=0; i<STACKQTDS; i++) {
685 struct ehci_qtd *td = &tds[tdpos++ % STACKQTDS];
Kevin O'Connor49ecf692011-11-19 14:21:51 -0500686 int ret = ehci_wait_td(pipe, td, 5000);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500687 if (ret)
Kevin O'Connor49ecf692011-11-19 14:21:51 -0500688 return -1;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500689 }
690
691 return 0;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500692}
693
Kevin O'Connor190cc622010-03-09 19:43:52 -0500694int
695ehci_poll_intr(struct usb_pipe *p, void *data)
696{
697 ASSERT16();
698 if (! CONFIG_USB_EHCI)
699 return -1;
700 struct ehci_pipe *pipe = container_of(p, struct ehci_pipe, pipe);
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400701 struct ehci_qtd *td = GET_LOWFLAT(pipe->next_td);
702 u32 token = GET_LOWFLAT(td->token);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500703 if (token & QTD_STS_ACTIVE)
704 // No intrs found.
705 return -1;
706 // XXX - check for errors.
707
708 // Copy data.
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400709 int maxpacket = GET_LOWFLAT(pipe->pipe.maxpacket);
710 int pos = td - GET_LOWFLAT(pipe->tds);
711 void *tddata = GET_LOWFLAT(pipe->data) + maxpacket * pos;
712 memcpy_far(GET_SEG(SS), data, SEG_LOW, LOWFLAT2LOW(tddata), maxpacket);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500713
714 // Reenable this td.
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400715 struct ehci_qtd *next = (void*)(GET_LOWFLAT(td->qtd_next) & ~EHCI_PTR_BITS);
716 SET_LOWFLAT(pipe->next_td, next);
717 SET_LOWFLAT(td->buf[0], (u32)tddata);
Kevin O'Connor190cc622010-03-09 19:43:52 -0500718 barrier();
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400719 SET_LOWFLAT(td->token, (ehci_explen(maxpacket) | QTD_STS_ACTIVE
Kevin O'Connor190cc622010-03-09 19:43:52 -0500720 | QTD_PID_IN | ehci_maxerr(3)));
721
722 return 0;
723}