blob: d55b64a91f11fa8b943ee37f7ef1d0bb91eb9723 [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'Connor406fad62010-02-28 02:23:19 -050045 u32 sts = readl(&cntl->regs->roothub_portstatus[port]);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050046 if (!(sts & RH_PS_CCS))
47 // No device.
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040048 return -1;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050049
50 // XXX - need to wait for USB_TIME_ATTDB if just powered up?
51
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040052 return 0;
53}
54
55// Disable port
56static void
57ohci_hub_disconnect(struct usbhub_s *hub, u32 port)
58{
59 struct usb_ohci_s *cntl = container_of(hub->cntl, struct usb_ohci_s, usb);
60 writel(&cntl->regs->roothub_portstatus[port], RH_PS_CCS|RH_PS_LSDA);
61}
62
63// Reset device on port
64static int
65ohci_hub_reset(struct usbhub_s *hub, u32 port)
66{
67 struct usb_ohci_s *cntl = container_of(hub->cntl, struct usb_ohci_s, usb);
Kevin O'Connor406fad62010-02-28 02:23:19 -050068 writel(&cntl->regs->roothub_portstatus[port], RH_PS_PRS);
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040069 u32 sts;
Kevin O'Connor018bdd72013-07-20 18:22:57 -040070 u32 end = timer_calc(USB_TIME_DRSTR * 2);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050071 for (;;) {
Kevin O'Connor406fad62010-02-28 02:23:19 -050072 sts = readl(&cntl->regs->roothub_portstatus[port]);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050073 if (!(sts & RH_PS_PRS))
74 // XXX - need to ensure USB_TIME_DRSTR time in reset?
75 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -040076 if (timer_check(end)) {
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050077 // Timeout.
78 warn_timeout();
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040079 ohci_hub_disconnect(hub, port);
80 return -1;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050081 }
82 yield();
83 }
84
85 if ((sts & (RH_PS_CCS|RH_PS_PES)) != (RH_PS_CCS|RH_PS_PES))
86 // Device no longer present
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040087 return -1;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050088
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040089 return !!(sts & RH_PS_LSDA);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050090}
91
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040092static struct usbhub_op_s ohci_HubOp = {
93 .detect = ohci_hub_detect,
94 .reset = ohci_hub_reset,
95 .disconnect = ohci_hub_disconnect,
96};
97
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050098// Find any devices connected to the root hub.
99static int
Kevin O'Connor406fad62010-02-28 02:23:19 -0500100check_ohci_ports(struct usb_ohci_s *cntl)
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500101{
102 ASSERT32FLAT();
103 // Turn on power for all devices on roothub.
Kevin O'Connor406fad62010-02-28 02:23:19 -0500104 u32 rha = readl(&cntl->regs->roothub_a);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500105 rha &= ~(RH_A_PSM | RH_A_OCPM);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500106 writel(&cntl->regs->roothub_status, RH_HS_LPSC);
107 writel(&cntl->regs->roothub_b, RH_B_PPCM);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500108 msleep((rha >> 24) * 2);
109 // XXX - need to sleep for USB_TIME_SIGATT if just powered up?
110
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500111 struct usbhub_s hub;
112 memset(&hub, 0, sizeof(hub));
Kevin O'Connor406fad62010-02-28 02:23:19 -0500113 hub.cntl = &cntl->usb;
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400114 hub.portcount = rha & RH_A_NDP;
115 hub.op = &ohci_HubOp;
116 usb_enumerate(&hub);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500117 return hub.devcount;
118}
119
120
121/****************************************************************
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500122 * Setup
123 ****************************************************************/
124
Kevin O'Connor98af3532012-02-20 12:54:49 -0500125// Wait for next USB frame to start - for ensuring safe memory release.
126static void
127ohci_waittick(struct usb_ohci_s *cntl)
128{
129 barrier();
130 struct ohci_hcca *hcca = (void*)cntl->regs->hcca;
131 u32 startframe = hcca->frame_no;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400132 u32 end = timer_calc(1000 * 5);
Kevin O'Connor98af3532012-02-20 12:54:49 -0500133 for (;;) {
134 if (hcca->frame_no != startframe)
135 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400136 if (timer_check(end)) {
Kevin O'Connor98af3532012-02-20 12:54:49 -0500137 warn_timeout();
138 return;
139 }
140 yield();
141 }
142}
143
144static void
145ohci_free_pipes(struct usb_ohci_s *cntl)
146{
147 dprintf(7, "ohci_free_pipes %p\n", cntl);
148
149 u32 creg = readl(&cntl->regs->control);
150 if (creg & (OHCI_CTRL_CLE|OHCI_CTRL_BLE)) {
151 writel(&cntl->regs->control, creg & ~(OHCI_CTRL_CLE|OHCI_CTRL_BLE));
152 ohci_waittick(cntl);
153 }
154
155 u32 *pos = &cntl->regs->ed_controlhead;
156 for (;;) {
157 struct ohci_ed *next = (void*)*pos;
158 if (!next)
159 break;
160 struct ohci_pipe *pipe = container_of(next, struct ohci_pipe, ed);
161 if (pipe->pipe.cntl != &cntl->usb) {
162 *pos = next->hwNextED;
163 free(pipe);
164 } else {
165 pos = &next->hwNextED;
166 }
167 }
168
169 writel(&cntl->regs->ed_controlcurrent, 0);
170 writel(&cntl->regs->ed_bulkcurrent, 0);
171 writel(&cntl->regs->control, creg);
172 cntl->usb.freelist = NULL;
173}
174
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400175static int
Kevin O'Connor406fad62010-02-28 02:23:19 -0500176start_ohci(struct usb_ohci_s *cntl, struct ohci_hcca *hcca)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400177{
Kevin O'Connor406fad62010-02-28 02:23:19 -0500178 u32 oldfminterval = readl(&cntl->regs->fminterval);
179 u32 oldrwc = readl(&cntl->regs->control) & OHCI_CTRL_RWC;
Kevin O'Connor59f02832009-10-12 10:09:15 -0400180
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400181 // XXX - check if already running?
Kevin O'Connor59f02832009-10-12 10:09:15 -0400182
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400183 // Do reset
Kevin O'Connor406fad62010-02-28 02:23:19 -0500184 writel(&cntl->regs->control, OHCI_USB_RESET | oldrwc);
185 readl(&cntl->regs->control); // flush writes
Kevin O'Connor8bbc79c2010-02-14 12:16:32 -0500186 msleep(USB_TIME_DRSTR);
Kevin O'Connor59f02832009-10-12 10:09:15 -0400187
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400188 // Do software init (min 10us, max 2ms)
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400189 u32 end = timer_calc_usec(10);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500190 writel(&cntl->regs->cmdstatus, OHCI_HCR);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400191 for (;;) {
Kevin O'Connor406fad62010-02-28 02:23:19 -0500192 u32 status = readl(&cntl->regs->cmdstatus);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400193 if (! status & OHCI_HCR)
194 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400195 if (timer_check(end)) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500196 warn_timeout();
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400197 return -1;
198 }
Kevin O'Connor59f02832009-10-12 10:09:15 -0400199 }
200
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400201 // Init memory
Kevin O'Connor406fad62010-02-28 02:23:19 -0500202 writel(&cntl->regs->ed_controlhead, 0);
203 writel(&cntl->regs->ed_bulkhead, 0);
204 writel(&cntl->regs->hcca, (u32)hcca);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400205
206 // Init fminterval
207 u32 fi = oldfminterval & 0x3fff;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500208 writel(&cntl->regs->fminterval
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400209 , (((oldfminterval & FIT) ^ FIT)
210 | fi | (((6 * (fi - 210)) / 7) << 16)));
Kevin O'Connor406fad62010-02-28 02:23:19 -0500211 writel(&cntl->regs->periodicstart, ((9 * fi) / 10) & 0x3fff);
212 readl(&cntl->regs->control); // flush writes
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400213
214 // XXX - verify that fminterval was setup correctly.
215
216 // Go into operational state
Kevin O'Connor406fad62010-02-28 02:23:19 -0500217 writel(&cntl->regs->control
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400218 , (OHCI_CTRL_CBSR | OHCI_CTRL_CLE | OHCI_CTRL_PLE
219 | OHCI_USB_OPER | oldrwc));
Kevin O'Connor406fad62010-02-28 02:23:19 -0500220 readl(&cntl->regs->control); // flush writes
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400221
222 return 0;
Kevin O'Connor59f02832009-10-12 10:09:15 -0400223}
224
225static void
Kevin O'Connor406fad62010-02-28 02:23:19 -0500226stop_ohci(struct usb_ohci_s *cntl)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400227{
Kevin O'Connor406fad62010-02-28 02:23:19 -0500228 u32 oldrwc = readl(&cntl->regs->control) & OHCI_CTRL_RWC;
229 writel(&cntl->regs->control, oldrwc);
230 readl(&cntl->regs->control); // flush writes
Kevin O'Connor59f02832009-10-12 10:09:15 -0400231}
232
Kevin O'Connor406fad62010-02-28 02:23:19 -0500233static void
234configure_ohci(void *data)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400235{
Kevin O'Connor406fad62010-02-28 02:23:19 -0500236 struct usb_ohci_s *cntl = data;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400237
238 // Allocate memory
239 struct ohci_hcca *hcca = memalign_high(256, sizeof(*hcca));
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500240 struct ohci_ed *intr_ed = malloc_high(sizeof(*intr_ed));
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500241 if (!hcca || !intr_ed) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500242 warn_noalloc();
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500243 goto free;
Kevin O'Connor59f02832009-10-12 10:09:15 -0400244 }
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400245 memset(hcca, 0, sizeof(*hcca));
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500246 memset(intr_ed, 0, sizeof(*intr_ed));
247 intr_ed->hwINFO = ED_SKIP;
248 int i;
249 for (i=0; i<ARRAY_SIZE(hcca->int_table); i++)
250 hcca->int_table[i] = (u32)intr_ed;
Kevin O'Connor59f02832009-10-12 10:09:15 -0400251
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400252 int ret = start_ohci(cntl, hcca);
253 if (ret)
254 goto err;
255
256 int count = check_ohci_ports(cntl);
Kevin O'Connor98af3532012-02-20 12:54:49 -0500257 ohci_free_pipes(cntl);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400258 if (! count)
259 goto err;
Kevin O'Connora5826b52009-10-24 17:57:29 -0400260 return;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400261
262err:
263 stop_ohci(cntl);
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500264free:
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400265 free(hcca);
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500266 free(intr_ed);
Kevin O'Connor59f02832009-10-12 10:09:15 -0400267}
268
Kevin O'Connorec443ff2013-12-05 18:43:20 -0500269static void
270ohci_controller_setup(struct pci_device *pci)
Kevin O'Connor406fad62010-02-28 02:23:19 -0500271{
Kevin O'Connor406fad62010-02-28 02:23:19 -0500272 struct usb_ohci_s *cntl = malloc_tmphigh(sizeof(*cntl));
Kevin O'Connorffdcd3a2011-07-10 15:48:00 -0400273 if (!cntl) {
274 warn_noalloc();
275 return;
276 }
Kevin O'Connor406fad62010-02-28 02:23:19 -0500277 memset(cntl, 0, sizeof(*cntl));
Kevin O'Connor8ff8e012011-07-09 14:11:21 -0400278 cntl->usb.pci = pci;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500279 cntl->usb.type = USB_TYPE_OHCI;
280
Kevin O'Connorec443ff2013-12-05 18:43:20 -0500281 wait_preempt(); // Avoid pci_config_readl when preempting
Kevin O'Connor8ff8e012011-07-09 14:11:21 -0400282 u16 bdf = pci->bdf;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500283 u32 baseaddr = pci_config_readl(bdf, PCI_BASE_ADDRESS_0);
284 cntl->regs = (void*)(baseaddr & PCI_BASE_ADDRESS_MEM_MASK);
285
Kevin O'Connor9dc243e2010-03-20 17:53:03 -0400286 dprintf(1, "OHCI init on dev %02x:%02x.%x (regs=%p)\n"
Kevin O'Connor406fad62010-02-28 02:23:19 -0500287 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf)
288 , pci_bdf_to_fn(bdf), cntl->regs);
289
290 // Enable bus mastering and memory access.
291 pci_config_maskw(bdf, PCI_COMMAND
292 , 0, PCI_COMMAND_MASTER|PCI_COMMAND_MEMORY);
293
294 // XXX - check for and disable SMM control?
295
296 // Disable interrupts
297 writel(&cntl->regs->intrdisable, ~0);
298 writel(&cntl->regs->intrstatus, ~0);
299
300 run_thread(configure_ohci, cntl);
301}
302
Kevin O'Connorec443ff2013-12-05 18:43:20 -0500303void
304ohci_setup(void)
305{
306 if (! CONFIG_USB_OHCI)
307 return;
308 struct pci_device *pci;
309 foreachpci(pci) {
310 if (pci_classprog(pci) == PCI_CLASS_SERIAL_USB_OHCI)
311 ohci_controller_setup(pci);
312 }
313}
314
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500315
316/****************************************************************
317 * End point communication
318 ****************************************************************/
319
Kevin O'Connor0423dda2012-03-10 12:12:27 -0500320// Setup fields in ed
321static void
322ohci_desc2pipe(struct ohci_pipe *pipe, struct usbdevice_s *usbdev
323 , struct usb_endpoint_descriptor *epdesc)
324{
325 usb_desc2pipe(&pipe->pipe, usbdev, epdesc);
326 pipe->ed.hwINFO = (ED_SKIP | usbdev->devaddr | (pipe->pipe.ep << 7)
327 | (epdesc->wMaxPacketSize << 16)
328 | (usbdev->speed ? ED_LOWSPEED : 0));
329}
330
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500331static struct usb_pipe *
Kevin O'Connor6152c322012-03-10 08:53:58 -0500332ohci_alloc_intr_pipe(struct usbdevice_s *usbdev
333 , struct usb_endpoint_descriptor *epdesc)
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400334{
Kevin O'Connor6152c322012-03-10 08:53:58 -0500335 struct usb_ohci_s *cntl = container_of(
336 usbdev->hub->cntl, struct usb_ohci_s, usb);
337 int frameexp = usb_getFrameExp(usbdev, epdesc);
338 dprintf(7, "ohci_alloc_intr_pipe %p %d\n", &cntl->usb, frameexp);
339
340 if (frameexp > 5)
341 frameexp = 5;
342 int maxpacket = epdesc->wMaxPacketSize;
343 // Determine number of entries needed for 2 timer ticks.
344 int ms = 1<<frameexp;
Kevin O'Connor69013372013-07-20 12:08:48 -0400345 int count = DIV_ROUND_UP(ticks_to_ms(2), ms) + 1;
Kevin O'Connor6152c322012-03-10 08:53:58 -0500346 struct ohci_pipe *pipe = malloc_low(sizeof(*pipe));
347 struct ohci_td *tds = malloc_low(sizeof(*tds) * count);
348 void *data = malloc_low(maxpacket * count);
349 if (!pipe || !tds || !data)
350 goto err;
351 memset(pipe, 0, sizeof(*pipe));
Kevin O'Connor0423dda2012-03-10 12:12:27 -0500352 ohci_desc2pipe(pipe, usbdev, epdesc);
353 pipe->ed.hwINFO &= ~ED_SKIP;
Kevin O'Connor6152c322012-03-10 08:53:58 -0500354 pipe->data = data;
355 pipe->count = count;
356 pipe->tds = tds;
357
358 struct ohci_ed *ed = &pipe->ed;
359 ed->hwHeadP = (u32)&tds[0];
360 ed->hwTailP = (u32)&tds[count-1];
Kevin O'Connor6152c322012-03-10 08:53:58 -0500361
362 int i;
363 for (i=0; i<count-1; i++) {
364 tds[i].hwINFO = TD_DP_IN | TD_T_TOGGLE | TD_CC;
365 tds[i].hwCBP = (u32)data + maxpacket * i;
366 tds[i].hwNextTD = (u32)&tds[i+1];
367 tds[i].hwBE = tds[i].hwCBP + maxpacket - 1;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400368 }
Kevin O'Connor6152c322012-03-10 08:53:58 -0500369
370 // Add to interrupt schedule.
Kevin O'Connor6152c322012-03-10 08:53:58 -0500371 struct ohci_hcca *hcca = (void*)cntl->regs->hcca;
372 if (frameexp == 0) {
373 // Add to existing interrupt entry.
374 struct ohci_ed *intr_ed = (void*)hcca->int_table[0];
375 ed->hwNextED = intr_ed->hwNextED;
Kevin O'Connorc37147d2012-03-10 12:30:27 -0500376 barrier();
Kevin O'Connor6152c322012-03-10 08:53:58 -0500377 intr_ed->hwNextED = (u32)ed;
378 } else {
379 int startpos = 1<<(frameexp-1);
380 ed->hwNextED = hcca->int_table[startpos];
Kevin O'Connorc37147d2012-03-10 12:30:27 -0500381 barrier();
Kevin O'Connor6152c322012-03-10 08:53:58 -0500382 for (i=startpos; i<ARRAY_SIZE(hcca->int_table); i+=ms)
383 hcca->int_table[i] = (u32)ed;
384 }
385
386 return &pipe->pipe;
387
388err:
389 free(pipe);
390 free(tds);
391 free(data);
392 return NULL;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400393}
394
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500395struct usb_pipe *
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500396ohci_alloc_pipe(struct usbdevice_s *usbdev
397 , struct usb_endpoint_descriptor *epdesc)
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500398{
399 if (! CONFIG_USB_OHCI)
400 return NULL;
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500401 u8 eptype = epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500402 if (eptype == USB_ENDPOINT_XFER_INT)
403 return ohci_alloc_intr_pipe(usbdev, epdesc);
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500404 if (eptype != USB_ENDPOINT_XFER_CONTROL) {
Kevin O'Connor01de9bd2012-03-05 22:41:21 -0500405 dprintf(1, "OHCI Bulk transfers not supported.\n");
406 return NULL;
407 }
Kevin O'Connor406fad62010-02-28 02:23:19 -0500408 struct usb_ohci_s *cntl = container_of(
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500409 usbdev->hub->cntl, struct usb_ohci_s, usb);
Kevin O'Connor01de9bd2012-03-05 22:41:21 -0500410 dprintf(7, "ohci_alloc_async_pipe %p\n", &cntl->usb);
Kevin O'Connor98af3532012-02-20 12:54:49 -0500411
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500412 struct usb_pipe *usbpipe = usb_getFreePipe(&cntl->usb, eptype);
413 if (usbpipe) {
414 // Use previously allocated pipe.
Kevin O'Connor0423dda2012-03-10 12:12:27 -0500415 struct ohci_pipe *pipe = container_of(usbpipe, struct ohci_pipe, pipe);
416 ohci_desc2pipe(pipe, usbdev, epdesc);
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500417 return usbpipe;
418 }
419
Kevin O'Connor98af3532012-02-20 12:54:49 -0500420 // Allocate a new queue head.
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500421 struct ohci_pipe *pipe = malloc_tmphigh(sizeof(*pipe));
422 if (!pipe) {
423 warn_noalloc();
424 return NULL;
425 }
426 memset(pipe, 0, sizeof(*pipe));
Kevin O'Connor0423dda2012-03-10 12:12:27 -0500427 ohci_desc2pipe(pipe, usbdev, epdesc);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500428
429 // Add queue head to controller list.
Kevin O'Connor406fad62010-02-28 02:23:19 -0500430 pipe->ed.hwNextED = cntl->regs->ed_controlhead;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500431 barrier();
Kevin O'Connor406fad62010-02-28 02:23:19 -0500432 cntl->regs->ed_controlhead = (u32)&pipe->ed;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500433 return &pipe->pipe;
434}
435
Kevin O'Connor6152c322012-03-10 08:53:58 -0500436static int
437wait_ed(struct ohci_ed *ed)
438{
439 // XXX - 500ms just a guess
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400440 u32 end = timer_calc(500);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500441 for (;;) {
442 if (ed->hwHeadP == ed->hwTailP)
443 return 0;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400444 if (timer_check(end)) {
Kevin O'Connor6152c322012-03-10 08:53:58 -0500445 warn_timeout();
446 return -1;
447 }
448 yield();
449 }
450}
451
Kevin O'Connor59f02832009-10-12 10:09:15 -0400452int
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500453ohci_control(struct usb_pipe *p, int dir, const void *cmd, int cmdsize
Kevin O'Connor59f02832009-10-12 10:09:15 -0400454 , void *data, int datasize)
455{
456 if (! CONFIG_USB_OHCI)
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400457 return -1;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500458 dprintf(5, "ohci_control %p\n", p);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500459 if (datasize > 4096) {
460 // XXX - should support larger sizes.
461 warn_noalloc();
462 return -1;
463 }
464 struct ohci_pipe *pipe = container_of(p, struct ohci_pipe, pipe);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500465 struct usb_ohci_s *cntl = container_of(
466 pipe->pipe.cntl, struct usb_ohci_s, usb);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400467
468 // Setup transfer descriptors
469 struct ohci_td *tds = malloc_tmphigh(sizeof(*tds) * 3);
Kevin O'Connor0770d672010-03-09 19:33:22 -0500470 if (!tds) {
471 warn_noalloc();
472 return -1;
473 }
474 struct ohci_td *td = tds;
475 td->hwINFO = TD_DP_SETUP | TD_T_DATA0 | TD_CC;
476 td->hwCBP = (u32)cmd;
477 td->hwNextTD = (u32)&td[1];
478 td->hwBE = (u32)cmd + cmdsize - 1;
479 td++;
480 if (datasize) {
481 td->hwINFO = (dir ? TD_DP_IN : TD_DP_OUT) | TD_T_DATA1 | TD_CC;
482 td->hwCBP = (u32)data;
483 td->hwNextTD = (u32)&td[1];
484 td->hwBE = (u32)data + datasize - 1;
485 td++;
486 }
487 td->hwINFO = (dir ? TD_DP_OUT : TD_DP_IN) | TD_T_DATA1 | TD_CC;
488 td->hwCBP = 0;
489 td->hwNextTD = (u32)&td[1];
490 td->hwBE = 0;
491 td++;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400492
493 // Transfer data
Kevin O'Connor0770d672010-03-09 19:33:22 -0500494 pipe->ed.hwHeadP = (u32)tds;
495 pipe->ed.hwTailP = (u32)td;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400496 barrier();
Kevin O'Connor0423dda2012-03-10 12:12:27 -0500497 pipe->ed.hwINFO &= ~ED_SKIP;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500498 writel(&cntl->regs->cmdstatus, OHCI_CLF);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400499
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500500 int ret = wait_ed(&pipe->ed);
Kevin O'Connor0423dda2012-03-10 12:12:27 -0500501 pipe->ed.hwINFO |= ED_SKIP;
Kevin O'Connorad901592009-12-13 11:25:25 -0500502 if (ret)
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500503 ohci_waittick(cntl);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400504 free(tds);
505 return ret;
Kevin O'Connor59f02832009-10-12 10:09:15 -0400506}
507
Kevin O'Connor2f968002010-09-19 22:10:08 -0400508int
509ohci_send_bulk(struct usb_pipe *p, int dir, void *data, int datasize)
510{
511 return -1;
512}
513
Kevin O'Connor59f02832009-10-12 10:09:15 -0400514int
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500515ohci_poll_intr(struct usb_pipe *p, void *data)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400516{
517 ASSERT16();
518 if (! CONFIG_USB_OHCI)
519 return -1;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400520
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500521 struct ohci_pipe *pipe = container_of(p, struct ohci_pipe, pipe);
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400522 struct ohci_td *tds = GET_LOWFLAT(pipe->tds);
523 struct ohci_td *head = (void*)(GET_LOWFLAT(pipe->ed.hwHeadP) & ~(ED_C|ED_H));
524 struct ohci_td *tail = (void*)GET_LOWFLAT(pipe->ed.hwTailP);
525 int count = GET_LOWFLAT(pipe->count);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400526 int pos = (tail - tds + 1) % count;
527 struct ohci_td *next = &tds[pos];
528 if (head == next)
529 // No intrs found.
530 return -1;
531 // XXX - check for errors.
532
533 // Copy data.
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400534 int maxpacket = GET_LOWFLAT(pipe->pipe.maxpacket);
535 void *pipedata = GET_LOWFLAT((pipe->data));
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400536 void *intrdata = pipedata + maxpacket * pos;
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400537 memcpy_far(GET_SEG(SS), data, SEG_LOW, LOWFLAT2LOW(intrdata), maxpacket);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400538
539 // Reenable this td.
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400540 SET_LOWFLAT(tail->hwINFO, TD_DP_IN | TD_T_TOGGLE | TD_CC);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400541 intrdata = pipedata + maxpacket * (tail-tds);
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400542 SET_LOWFLAT(tail->hwCBP, (u32)intrdata);
543 SET_LOWFLAT(tail->hwNextTD, (u32)next);
544 SET_LOWFLAT(tail->hwBE, (u32)intrdata + maxpacket - 1);
Kevin O'Connor0770d672010-03-09 19:33:22 -0500545 barrier();
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400546 SET_LOWFLAT(pipe->ed.hwTailP, (u32)next);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400547
548 return 0;
Kevin O'Connor59f02832009-10-12 10:09:15 -0400549}