blob: 478976880d243fc58f4d70316626e88f84380d0d [file] [log] [blame]
Kevin O'Connor59f02832009-10-12 10:09:15 -04001// Code for handling OHCI 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'Connor4f6563e2012-05-19 22:42:51 -04007#include "biosvar.h" // GET_LOWFLAT
Kevin O'Connor2d2fa312013-09-14 21:55:26 -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_OHCI
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040013#include "pci_regs.h" // PCI_BASE_ADDRESS_0
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
16#include "usb-ohci.h" // struct ohci_hcca
17#include "util.h" // msleep
Kevin O'Connorb9c6a962013-09-14 13:01:30 -040018#include "x86.h" // readl
Kevin O'Connor59f02832009-10-12 10:09:15 -040019
Kevin O'Connor1c46a542009-10-17 23:53:32 -040020#define FIT (1 << 31)
21
Kevin O'Connor406fad62010-02-28 02:23:19 -050022struct usb_ohci_s {
23 struct usb_s usb;
24 struct ohci_regs *regs;
25};
26
Kevin O'Connor98af3532012-02-20 12:54:49 -050027struct ohci_pipe {
28 struct ohci_ed ed;
29 struct usb_pipe pipe;
30 void *data;
31 int count;
32 struct ohci_td *tds;
33};
34
Kevin O'Connor357bdfa2010-02-26 08:57:13 -050035
36/****************************************************************
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050037 * Root hub
38 ****************************************************************/
39
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040040// Check if device attached to port
41static int
42ohci_hub_detect(struct usbhub_s *hub, u32 port)
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050043{
Kevin O'Connor406fad62010-02-28 02:23:19 -050044 struct usb_ohci_s *cntl = container_of(hub->cntl, struct usb_ohci_s, usb);
Kevin O'Connor0f681302014-09-10 11:33:01 -040045 u32 sts = readl(&cntl->regs->roothub_portstatus[port]);
46 return (sts & RH_PS_CCS) ? 1 : 0;
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040047}
48
49// Disable port
50static void
51ohci_hub_disconnect(struct usbhub_s *hub, u32 port)
52{
53 struct usb_ohci_s *cntl = container_of(hub->cntl, struct usb_ohci_s, usb);
54 writel(&cntl->regs->roothub_portstatus[port], RH_PS_CCS|RH_PS_LSDA);
55}
56
57// Reset device on port
58static int
59ohci_hub_reset(struct usbhub_s *hub, u32 port)
60{
61 struct usb_ohci_s *cntl = container_of(hub->cntl, struct usb_ohci_s, usb);
Kevin O'Connor406fad62010-02-28 02:23:19 -050062 writel(&cntl->regs->roothub_portstatus[port], RH_PS_PRS);
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040063 u32 sts;
Kevin O'Connor018bdd72013-07-20 18:22:57 -040064 u32 end = timer_calc(USB_TIME_DRSTR * 2);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050065 for (;;) {
Kevin O'Connor406fad62010-02-28 02:23:19 -050066 sts = readl(&cntl->regs->roothub_portstatus[port]);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050067 if (!(sts & RH_PS_PRS))
68 // XXX - need to ensure USB_TIME_DRSTR time in reset?
69 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -040070 if (timer_check(end)) {
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050071 // Timeout.
72 warn_timeout();
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040073 ohci_hub_disconnect(hub, port);
74 return -1;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050075 }
76 yield();
77 }
78
79 if ((sts & (RH_PS_CCS|RH_PS_PES)) != (RH_PS_CCS|RH_PS_PES))
80 // Device no longer present
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040081 return -1;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050082
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040083 return !!(sts & RH_PS_LSDA);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050084}
85
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040086static struct usbhub_op_s ohci_HubOp = {
87 .detect = ohci_hub_detect,
88 .reset = ohci_hub_reset,
89 .disconnect = ohci_hub_disconnect,
90};
91
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050092// Find any devices connected to the root hub.
93static int
Kevin O'Connor406fad62010-02-28 02:23:19 -050094check_ohci_ports(struct usb_ohci_s *cntl)
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050095{
96 ASSERT32FLAT();
97 // Turn on power for all devices on roothub.
Kevin O'Connor406fad62010-02-28 02:23:19 -050098 u32 rha = readl(&cntl->regs->roothub_a);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050099 rha &= ~(RH_A_PSM | RH_A_OCPM);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500100 writel(&cntl->regs->roothub_status, RH_HS_LPSC);
101 writel(&cntl->regs->roothub_b, RH_B_PPCM);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500102 msleep((rha >> 24) * 2);
103 // XXX - need to sleep for USB_TIME_SIGATT if just powered up?
104
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500105 struct usbhub_s hub;
106 memset(&hub, 0, sizeof(hub));
Kevin O'Connor406fad62010-02-28 02:23:19 -0500107 hub.cntl = &cntl->usb;
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400108 hub.portcount = rha & RH_A_NDP;
109 hub.op = &ohci_HubOp;
110 usb_enumerate(&hub);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500111 return hub.devcount;
112}
113
114
115/****************************************************************
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500116 * Setup
117 ****************************************************************/
118
Kevin O'Connor98af3532012-02-20 12:54:49 -0500119// Wait for next USB frame to start - for ensuring safe memory release.
120static void
121ohci_waittick(struct usb_ohci_s *cntl)
122{
123 barrier();
124 struct ohci_hcca *hcca = (void*)cntl->regs->hcca;
125 u32 startframe = hcca->frame_no;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400126 u32 end = timer_calc(1000 * 5);
Kevin O'Connor98af3532012-02-20 12:54:49 -0500127 for (;;) {
128 if (hcca->frame_no != startframe)
129 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400130 if (timer_check(end)) {
Kevin O'Connor98af3532012-02-20 12:54:49 -0500131 warn_timeout();
132 return;
133 }
134 yield();
135 }
136}
137
138static void
139ohci_free_pipes(struct usb_ohci_s *cntl)
140{
141 dprintf(7, "ohci_free_pipes %p\n", cntl);
142
143 u32 creg = readl(&cntl->regs->control);
144 if (creg & (OHCI_CTRL_CLE|OHCI_CTRL_BLE)) {
145 writel(&cntl->regs->control, creg & ~(OHCI_CTRL_CLE|OHCI_CTRL_BLE));
146 ohci_waittick(cntl);
147 }
148
149 u32 *pos = &cntl->regs->ed_controlhead;
150 for (;;) {
151 struct ohci_ed *next = (void*)*pos;
152 if (!next)
153 break;
154 struct ohci_pipe *pipe = container_of(next, struct ohci_pipe, ed);
Kevin O'Connor2bfd1702014-10-16 12:31:42 -0400155 if (usb_is_freelist(&cntl->usb, &pipe->pipe)) {
Kevin O'Connor98af3532012-02-20 12:54:49 -0500156 *pos = next->hwNextED;
157 free(pipe);
158 } else {
159 pos = &next->hwNextED;
160 }
161 }
162
163 writel(&cntl->regs->ed_controlcurrent, 0);
164 writel(&cntl->regs->ed_bulkcurrent, 0);
165 writel(&cntl->regs->control, creg);
166 cntl->usb.freelist = NULL;
167}
168
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400169static int
Kevin O'Connor406fad62010-02-28 02:23:19 -0500170start_ohci(struct usb_ohci_s *cntl, struct ohci_hcca *hcca)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400171{
Kevin O'Connor406fad62010-02-28 02:23:19 -0500172 u32 oldfminterval = readl(&cntl->regs->fminterval);
173 u32 oldrwc = readl(&cntl->regs->control) & OHCI_CTRL_RWC;
Kevin O'Connor59f02832009-10-12 10:09:15 -0400174
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400175 // XXX - check if already running?
Kevin O'Connor59f02832009-10-12 10:09:15 -0400176
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400177 // Do reset
Kevin O'Connor406fad62010-02-28 02:23:19 -0500178 writel(&cntl->regs->control, OHCI_USB_RESET | oldrwc);
179 readl(&cntl->regs->control); // flush writes
Kevin O'Connor8bbc79c2010-02-14 12:16:32 -0500180 msleep(USB_TIME_DRSTR);
Kevin O'Connor59f02832009-10-12 10:09:15 -0400181
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400182 // Do software init (min 10us, max 2ms)
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400183 u32 end = timer_calc_usec(10);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500184 writel(&cntl->regs->cmdstatus, OHCI_HCR);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400185 for (;;) {
Kevin O'Connor406fad62010-02-28 02:23:19 -0500186 u32 status = readl(&cntl->regs->cmdstatus);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400187 if (! status & OHCI_HCR)
188 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400189 if (timer_check(end)) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500190 warn_timeout();
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400191 return -1;
192 }
Kevin O'Connor59f02832009-10-12 10:09:15 -0400193 }
194
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400195 // Init memory
Kevin O'Connor406fad62010-02-28 02:23:19 -0500196 writel(&cntl->regs->ed_controlhead, 0);
197 writel(&cntl->regs->ed_bulkhead, 0);
198 writel(&cntl->regs->hcca, (u32)hcca);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400199
200 // Init fminterval
201 u32 fi = oldfminterval & 0x3fff;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500202 writel(&cntl->regs->fminterval
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400203 , (((oldfminterval & FIT) ^ FIT)
204 | fi | (((6 * (fi - 210)) / 7) << 16)));
Kevin O'Connor406fad62010-02-28 02:23:19 -0500205 writel(&cntl->regs->periodicstart, ((9 * fi) / 10) & 0x3fff);
206 readl(&cntl->regs->control); // flush writes
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400207
208 // XXX - verify that fminterval was setup correctly.
209
210 // Go into operational state
Kevin O'Connor406fad62010-02-28 02:23:19 -0500211 writel(&cntl->regs->control
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400212 , (OHCI_CTRL_CBSR | OHCI_CTRL_CLE | OHCI_CTRL_PLE
213 | OHCI_USB_OPER | oldrwc));
Kevin O'Connor406fad62010-02-28 02:23:19 -0500214 readl(&cntl->regs->control); // flush writes
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400215
216 return 0;
Kevin O'Connor59f02832009-10-12 10:09:15 -0400217}
218
219static void
Kevin O'Connor406fad62010-02-28 02:23:19 -0500220stop_ohci(struct usb_ohci_s *cntl)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400221{
Kevin O'Connor406fad62010-02-28 02:23:19 -0500222 u32 oldrwc = readl(&cntl->regs->control) & OHCI_CTRL_RWC;
223 writel(&cntl->regs->control, oldrwc);
224 readl(&cntl->regs->control); // flush writes
Kevin O'Connor59f02832009-10-12 10:09:15 -0400225}
226
Kevin O'Connor406fad62010-02-28 02:23:19 -0500227static void
228configure_ohci(void *data)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400229{
Kevin O'Connor406fad62010-02-28 02:23:19 -0500230 struct usb_ohci_s *cntl = data;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400231
232 // Allocate memory
233 struct ohci_hcca *hcca = memalign_high(256, sizeof(*hcca));
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500234 struct ohci_ed *intr_ed = malloc_high(sizeof(*intr_ed));
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500235 if (!hcca || !intr_ed) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500236 warn_noalloc();
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500237 goto free;
Kevin O'Connor59f02832009-10-12 10:09:15 -0400238 }
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400239 memset(hcca, 0, sizeof(*hcca));
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500240 memset(intr_ed, 0, sizeof(*intr_ed));
241 intr_ed->hwINFO = ED_SKIP;
242 int i;
243 for (i=0; i<ARRAY_SIZE(hcca->int_table); i++)
244 hcca->int_table[i] = (u32)intr_ed;
Kevin O'Connor59f02832009-10-12 10:09:15 -0400245
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400246 int ret = start_ohci(cntl, hcca);
247 if (ret)
248 goto err;
249
250 int count = check_ohci_ports(cntl);
Kevin O'Connor98af3532012-02-20 12:54:49 -0500251 ohci_free_pipes(cntl);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400252 if (! count)
253 goto err;
Kevin O'Connora5826b52009-10-24 17:57:29 -0400254 return;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400255
256err:
257 stop_ohci(cntl);
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500258free:
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400259 free(hcca);
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500260 free(intr_ed);
Kevin O'Connor59f02832009-10-12 10:09:15 -0400261}
262
Kevin O'Connorec443ff2013-12-05 18:43:20 -0500263static void
264ohci_controller_setup(struct pci_device *pci)
Kevin O'Connor406fad62010-02-28 02:23:19 -0500265{
Kevin O'Connor406fad62010-02-28 02:23:19 -0500266 struct usb_ohci_s *cntl = malloc_tmphigh(sizeof(*cntl));
Kevin O'Connorffdcd3a2011-07-10 15:48:00 -0400267 if (!cntl) {
268 warn_noalloc();
269 return;
270 }
Kevin O'Connor406fad62010-02-28 02:23:19 -0500271 memset(cntl, 0, sizeof(*cntl));
Kevin O'Connor8ff8e012011-07-09 14:11:21 -0400272 cntl->usb.pci = pci;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500273 cntl->usb.type = USB_TYPE_OHCI;
274
Kevin O'Connorec443ff2013-12-05 18:43:20 -0500275 wait_preempt(); // Avoid pci_config_readl when preempting
Kevin O'Connor8ff8e012011-07-09 14:11:21 -0400276 u16 bdf = pci->bdf;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500277 u32 baseaddr = pci_config_readl(bdf, PCI_BASE_ADDRESS_0);
278 cntl->regs = (void*)(baseaddr & PCI_BASE_ADDRESS_MEM_MASK);
279
Kevin O'Connor9dc243e2010-03-20 17:53:03 -0400280 dprintf(1, "OHCI init on dev %02x:%02x.%x (regs=%p)\n"
Kevin O'Connor406fad62010-02-28 02:23:19 -0500281 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf)
282 , pci_bdf_to_fn(bdf), cntl->regs);
283
284 // Enable bus mastering and memory access.
285 pci_config_maskw(bdf, PCI_COMMAND
286 , 0, PCI_COMMAND_MASTER|PCI_COMMAND_MEMORY);
287
288 // XXX - check for and disable SMM control?
289
290 // Disable interrupts
291 writel(&cntl->regs->intrdisable, ~0);
292 writel(&cntl->regs->intrstatus, ~0);
293
294 run_thread(configure_ohci, cntl);
295}
296
Kevin O'Connorec443ff2013-12-05 18:43:20 -0500297void
298ohci_setup(void)
299{
300 if (! CONFIG_USB_OHCI)
301 return;
302 struct pci_device *pci;
303 foreachpci(pci) {
304 if (pci_classprog(pci) == PCI_CLASS_SERIAL_USB_OHCI)
305 ohci_controller_setup(pci);
306 }
307}
308
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500309
310/****************************************************************
311 * End point communication
312 ****************************************************************/
313
Kevin O'Connor0423dda2012-03-10 12:12:27 -0500314// Setup fields in ed
315static void
316ohci_desc2pipe(struct ohci_pipe *pipe, struct usbdevice_s *usbdev
317 , struct usb_endpoint_descriptor *epdesc)
318{
319 usb_desc2pipe(&pipe->pipe, usbdev, epdesc);
320 pipe->ed.hwINFO = (ED_SKIP | usbdev->devaddr | (pipe->pipe.ep << 7)
321 | (epdesc->wMaxPacketSize << 16)
322 | (usbdev->speed ? ED_LOWSPEED : 0));
323}
324
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500325static struct usb_pipe *
Kevin O'Connor6152c322012-03-10 08:53:58 -0500326ohci_alloc_intr_pipe(struct usbdevice_s *usbdev
327 , struct usb_endpoint_descriptor *epdesc)
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400328{
Kevin O'Connor6152c322012-03-10 08:53:58 -0500329 struct usb_ohci_s *cntl = container_of(
330 usbdev->hub->cntl, struct usb_ohci_s, usb);
Kevin O'Connor98cdad32014-10-16 12:05:09 -0400331 int frameexp = usb_get_period(usbdev, epdesc);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500332 dprintf(7, "ohci_alloc_intr_pipe %p %d\n", &cntl->usb, frameexp);
333
334 if (frameexp > 5)
335 frameexp = 5;
336 int maxpacket = epdesc->wMaxPacketSize;
337 // Determine number of entries needed for 2 timer ticks.
338 int ms = 1<<frameexp;
Kevin O'Connor69013372013-07-20 12:08:48 -0400339 int count = DIV_ROUND_UP(ticks_to_ms(2), ms) + 1;
Kevin O'Connor6152c322012-03-10 08:53:58 -0500340 struct ohci_pipe *pipe = malloc_low(sizeof(*pipe));
341 struct ohci_td *tds = malloc_low(sizeof(*tds) * count);
342 void *data = malloc_low(maxpacket * count);
343 if (!pipe || !tds || !data)
344 goto err;
345 memset(pipe, 0, sizeof(*pipe));
Kevin O'Connor0423dda2012-03-10 12:12:27 -0500346 ohci_desc2pipe(pipe, usbdev, epdesc);
347 pipe->ed.hwINFO &= ~ED_SKIP;
Kevin O'Connor6152c322012-03-10 08:53:58 -0500348 pipe->data = data;
349 pipe->count = count;
350 pipe->tds = tds;
351
352 struct ohci_ed *ed = &pipe->ed;
353 ed->hwHeadP = (u32)&tds[0];
354 ed->hwTailP = (u32)&tds[count-1];
Kevin O'Connor6152c322012-03-10 08:53:58 -0500355
356 int i;
357 for (i=0; i<count-1; i++) {
358 tds[i].hwINFO = TD_DP_IN | TD_T_TOGGLE | TD_CC;
359 tds[i].hwCBP = (u32)data + maxpacket * i;
360 tds[i].hwNextTD = (u32)&tds[i+1];
361 tds[i].hwBE = tds[i].hwCBP + maxpacket - 1;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400362 }
Kevin O'Connor6152c322012-03-10 08:53:58 -0500363
364 // Add to interrupt schedule.
Kevin O'Connor6152c322012-03-10 08:53:58 -0500365 struct ohci_hcca *hcca = (void*)cntl->regs->hcca;
366 if (frameexp == 0) {
367 // Add to existing interrupt entry.
368 struct ohci_ed *intr_ed = (void*)hcca->int_table[0];
369 ed->hwNextED = intr_ed->hwNextED;
Kevin O'Connorc37147d2012-03-10 12:30:27 -0500370 barrier();
Kevin O'Connor6152c322012-03-10 08:53:58 -0500371 intr_ed->hwNextED = (u32)ed;
372 } else {
373 int startpos = 1<<(frameexp-1);
374 ed->hwNextED = hcca->int_table[startpos];
Kevin O'Connorc37147d2012-03-10 12:30:27 -0500375 barrier();
Kevin O'Connor6152c322012-03-10 08:53:58 -0500376 for (i=startpos; i<ARRAY_SIZE(hcca->int_table); i+=ms)
377 hcca->int_table[i] = (u32)ed;
378 }
379
380 return &pipe->pipe;
381
382err:
383 free(pipe);
384 free(tds);
385 free(data);
386 return NULL;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400387}
388
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500389struct usb_pipe *
Kevin O'Connor2e0c2ea2014-10-16 13:36:41 -0400390ohci_realloc_pipe(struct usbdevice_s *usbdev, struct usb_pipe *upipe
391 , struct usb_endpoint_descriptor *epdesc)
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500392{
393 if (! CONFIG_USB_OHCI)
394 return NULL;
Kevin O'Connor2e0c2ea2014-10-16 13:36:41 -0400395 usb_add_freelist(upipe);
396 if (!epdesc)
397 return NULL;
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500398 u8 eptype = epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500399 if (eptype == USB_ENDPOINT_XFER_INT)
400 return ohci_alloc_intr_pipe(usbdev, epdesc);
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500401 if (eptype != USB_ENDPOINT_XFER_CONTROL) {
Kevin O'Connor01de9bd2012-03-05 22:41:21 -0500402 dprintf(1, "OHCI Bulk transfers not supported.\n");
403 return NULL;
404 }
Kevin O'Connor406fad62010-02-28 02:23:19 -0500405 struct usb_ohci_s *cntl = container_of(
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500406 usbdev->hub->cntl, struct usb_ohci_s, usb);
Kevin O'Connor01de9bd2012-03-05 22:41:21 -0500407 dprintf(7, "ohci_alloc_async_pipe %p\n", &cntl->usb);
Kevin O'Connor98af3532012-02-20 12:54:49 -0500408
Kevin O'Connor2bfd1702014-10-16 12:31:42 -0400409 struct usb_pipe *usbpipe = usb_get_freelist(&cntl->usb, eptype);
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500410 if (usbpipe) {
411 // Use previously allocated pipe.
Kevin O'Connor0423dda2012-03-10 12:12:27 -0500412 struct ohci_pipe *pipe = container_of(usbpipe, struct ohci_pipe, pipe);
413 ohci_desc2pipe(pipe, usbdev, epdesc);
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500414 return usbpipe;
415 }
416
Kevin O'Connor98af3532012-02-20 12:54:49 -0500417 // Allocate a new queue head.
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500418 struct ohci_pipe *pipe = malloc_tmphigh(sizeof(*pipe));
419 if (!pipe) {
420 warn_noalloc();
421 return NULL;
422 }
423 memset(pipe, 0, sizeof(*pipe));
Kevin O'Connor0423dda2012-03-10 12:12:27 -0500424 ohci_desc2pipe(pipe, usbdev, epdesc);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500425
426 // Add queue head to controller list.
Kevin O'Connor406fad62010-02-28 02:23:19 -0500427 pipe->ed.hwNextED = cntl->regs->ed_controlhead;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500428 barrier();
Kevin O'Connor406fad62010-02-28 02:23:19 -0500429 cntl->regs->ed_controlhead = (u32)&pipe->ed;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500430 return &pipe->pipe;
431}
432
Kevin O'Connor6152c322012-03-10 08:53:58 -0500433static int
Kevin O'Connor083ee292014-06-14 12:50:38 -0400434wait_ed(struct ohci_ed *ed, int timeout)
Kevin O'Connor6152c322012-03-10 08:53:58 -0500435{
Kevin O'Connor083ee292014-06-14 12:50:38 -0400436 u32 end = timer_calc(timeout);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500437 for (;;) {
438 if (ed->hwHeadP == ed->hwTailP)
439 return 0;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400440 if (timer_check(end)) {
Kevin O'Connor6152c322012-03-10 08:53:58 -0500441 warn_timeout();
442 return -1;
443 }
444 yield();
445 }
446}
447
Kevin O'Connor59f02832009-10-12 10:09:15 -0400448int
Kevin O'Connor20c9be32014-10-16 11:55:16 -0400449ohci_send_control(struct usb_pipe *p, int dir, const void *cmd, int cmdsize
450 , void *data, int datasize)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400451{
452 if (! CONFIG_USB_OHCI)
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400453 return -1;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500454 dprintf(5, "ohci_control %p\n", p);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500455 if (datasize > 4096) {
456 // XXX - should support larger sizes.
457 warn_noalloc();
458 return -1;
459 }
460 struct ohci_pipe *pipe = container_of(p, struct ohci_pipe, pipe);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500461 struct usb_ohci_s *cntl = container_of(
462 pipe->pipe.cntl, struct usb_ohci_s, usb);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400463
464 // Setup transfer descriptors
465 struct ohci_td *tds = malloc_tmphigh(sizeof(*tds) * 3);
Kevin O'Connor0770d672010-03-09 19:33:22 -0500466 if (!tds) {
467 warn_noalloc();
468 return -1;
469 }
470 struct ohci_td *td = tds;
471 td->hwINFO = TD_DP_SETUP | TD_T_DATA0 | TD_CC;
472 td->hwCBP = (u32)cmd;
473 td->hwNextTD = (u32)&td[1];
474 td->hwBE = (u32)cmd + cmdsize - 1;
475 td++;
476 if (datasize) {
477 td->hwINFO = (dir ? TD_DP_IN : TD_DP_OUT) | TD_T_DATA1 | TD_CC;
478 td->hwCBP = (u32)data;
479 td->hwNextTD = (u32)&td[1];
480 td->hwBE = (u32)data + datasize - 1;
481 td++;
482 }
483 td->hwINFO = (dir ? TD_DP_OUT : TD_DP_IN) | TD_T_DATA1 | TD_CC;
484 td->hwCBP = 0;
485 td->hwNextTD = (u32)&td[1];
486 td->hwBE = 0;
487 td++;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400488
489 // Transfer data
Kevin O'Connor0770d672010-03-09 19:33:22 -0500490 pipe->ed.hwHeadP = (u32)tds;
491 pipe->ed.hwTailP = (u32)td;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400492 barrier();
Kevin O'Connor0423dda2012-03-10 12:12:27 -0500493 pipe->ed.hwINFO &= ~ED_SKIP;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500494 writel(&cntl->regs->cmdstatus, OHCI_CLF);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400495
Kevin O'Connor083ee292014-06-14 12:50:38 -0400496 int ret = wait_ed(&pipe->ed, usb_xfer_time(p, datasize));
Kevin O'Connor0423dda2012-03-10 12:12:27 -0500497 pipe->ed.hwINFO |= ED_SKIP;
Kevin O'Connorad901592009-12-13 11:25:25 -0500498 if (ret)
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500499 ohci_waittick(cntl);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400500 free(tds);
501 return ret;
Kevin O'Connor59f02832009-10-12 10:09:15 -0400502}
503
Kevin O'Connor2f968002010-09-19 22:10:08 -0400504int
505ohci_send_bulk(struct usb_pipe *p, int dir, void *data, int datasize)
506{
507 return -1;
508}
509
Kevin O'Connor59f02832009-10-12 10:09:15 -0400510int
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500511ohci_poll_intr(struct usb_pipe *p, void *data)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400512{
513 ASSERT16();
514 if (! CONFIG_USB_OHCI)
515 return -1;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400516
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500517 struct ohci_pipe *pipe = container_of(p, struct ohci_pipe, pipe);
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400518 struct ohci_td *tds = GET_LOWFLAT(pipe->tds);
519 struct ohci_td *head = (void*)(GET_LOWFLAT(pipe->ed.hwHeadP) & ~(ED_C|ED_H));
520 struct ohci_td *tail = (void*)GET_LOWFLAT(pipe->ed.hwTailP);
521 int count = GET_LOWFLAT(pipe->count);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400522 int pos = (tail - tds + 1) % count;
523 struct ohci_td *next = &tds[pos];
524 if (head == next)
525 // No intrs found.
526 return -1;
527 // XXX - check for errors.
528
529 // Copy data.
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400530 int maxpacket = GET_LOWFLAT(pipe->pipe.maxpacket);
531 void *pipedata = GET_LOWFLAT((pipe->data));
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400532 void *intrdata = pipedata + maxpacket * pos;
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400533 memcpy_far(GET_SEG(SS), data, SEG_LOW, LOWFLAT2LOW(intrdata), maxpacket);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400534
535 // Reenable this td.
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400536 SET_LOWFLAT(tail->hwINFO, TD_DP_IN | TD_T_TOGGLE | TD_CC);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400537 intrdata = pipedata + maxpacket * (tail-tds);
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400538 SET_LOWFLAT(tail->hwCBP, (u32)intrdata);
539 SET_LOWFLAT(tail->hwNextTD, (u32)next);
540 SET_LOWFLAT(tail->hwBE, (u32)intrdata + maxpacket - 1);
Kevin O'Connor0770d672010-03-09 19:33:22 -0500541 barrier();
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400542 SET_LOWFLAT(pipe->ed.hwTailP, (u32)next);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400543
544 return 0;
Kevin O'Connor59f02832009-10-12 10:09:15 -0400545}