blob: 313e3fd44de3434c89f6bc4d1ef5704676879b37 [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
12#include "pci_regs.h" // PCI_BASE_ADDRESS_0
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040013#include "string.h" // memset
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040014#include "usb.h" // struct usb_s
15#include "usb-ohci.h" // struct ohci_hcca
16#include "util.h" // msleep
Kevin O'Connorb9c6a962013-09-14 13:01:30 -040017#include "x86.h" // readl
Kevin O'Connor59f02832009-10-12 10:09:15 -040018
Kevin O'Connor1c46a542009-10-17 23:53:32 -040019#define FIT (1 << 31)
20
Kevin O'Connor406fad62010-02-28 02:23:19 -050021struct usb_ohci_s {
22 struct usb_s usb;
23 struct ohci_regs *regs;
24};
25
Kevin O'Connor98af3532012-02-20 12:54:49 -050026struct ohci_pipe {
27 struct ohci_ed ed;
28 struct usb_pipe pipe;
29 void *data;
30 int count;
31 struct ohci_td *tds;
32};
33
Kevin O'Connor357bdfa2010-02-26 08:57:13 -050034
35/****************************************************************
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050036 * Root hub
37 ****************************************************************/
38
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040039// Check if device attached to port
40static int
41ohci_hub_detect(struct usbhub_s *hub, u32 port)
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050042{
Kevin O'Connor406fad62010-02-28 02:23:19 -050043 struct usb_ohci_s *cntl = container_of(hub->cntl, struct usb_ohci_s, usb);
Kevin O'Connor406fad62010-02-28 02:23:19 -050044 u32 sts = readl(&cntl->regs->roothub_portstatus[port]);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050045 if (!(sts & RH_PS_CCS))
46 // No device.
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040047 return -1;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050048
49 // XXX - need to wait for USB_TIME_ATTDB if just powered up?
50
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040051 return 0;
52}
53
54// Disable port
55static void
56ohci_hub_disconnect(struct usbhub_s *hub, u32 port)
57{
58 struct usb_ohci_s *cntl = container_of(hub->cntl, struct usb_ohci_s, usb);
59 writel(&cntl->regs->roothub_portstatus[port], RH_PS_CCS|RH_PS_LSDA);
60}
61
62// Reset device on port
63static int
64ohci_hub_reset(struct usbhub_s *hub, u32 port)
65{
66 struct usb_ohci_s *cntl = container_of(hub->cntl, struct usb_ohci_s, usb);
Kevin O'Connor406fad62010-02-28 02:23:19 -050067 writel(&cntl->regs->roothub_portstatus[port], RH_PS_PRS);
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040068 u32 sts;
Kevin O'Connor018bdd72013-07-20 18:22:57 -040069 u32 end = timer_calc(USB_TIME_DRSTR * 2);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050070 for (;;) {
Kevin O'Connor406fad62010-02-28 02:23:19 -050071 sts = readl(&cntl->regs->roothub_portstatus[port]);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050072 if (!(sts & RH_PS_PRS))
73 // XXX - need to ensure USB_TIME_DRSTR time in reset?
74 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -040075 if (timer_check(end)) {
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050076 // Timeout.
77 warn_timeout();
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040078 ohci_hub_disconnect(hub, port);
79 return -1;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050080 }
81 yield();
82 }
83
84 if ((sts & (RH_PS_CCS|RH_PS_PES)) != (RH_PS_CCS|RH_PS_PES))
85 // Device no longer present
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040086 return -1;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050087
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040088 return !!(sts & RH_PS_LSDA);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050089}
90
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040091static struct usbhub_op_s ohci_HubOp = {
92 .detect = ohci_hub_detect,
93 .reset = ohci_hub_reset,
94 .disconnect = ohci_hub_disconnect,
95};
96
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050097// Find any devices connected to the root hub.
98static int
Kevin O'Connor406fad62010-02-28 02:23:19 -050099check_ohci_ports(struct usb_ohci_s *cntl)
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500100{
101 ASSERT32FLAT();
102 // Turn on power for all devices on roothub.
Kevin O'Connor406fad62010-02-28 02:23:19 -0500103 u32 rha = readl(&cntl->regs->roothub_a);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500104 rha &= ~(RH_A_PSM | RH_A_OCPM);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500105 writel(&cntl->regs->roothub_status, RH_HS_LPSC);
106 writel(&cntl->regs->roothub_b, RH_B_PPCM);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500107 msleep((rha >> 24) * 2);
108 // XXX - need to sleep for USB_TIME_SIGATT if just powered up?
109
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500110 struct usbhub_s hub;
111 memset(&hub, 0, sizeof(hub));
Kevin O'Connor406fad62010-02-28 02:23:19 -0500112 hub.cntl = &cntl->usb;
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400113 hub.portcount = rha & RH_A_NDP;
114 hub.op = &ohci_HubOp;
115 usb_enumerate(&hub);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500116 return hub.devcount;
117}
118
119
120/****************************************************************
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500121 * Setup
122 ****************************************************************/
123
Kevin O'Connor98af3532012-02-20 12:54:49 -0500124// Wait for next USB frame to start - for ensuring safe memory release.
125static void
126ohci_waittick(struct usb_ohci_s *cntl)
127{
128 barrier();
129 struct ohci_hcca *hcca = (void*)cntl->regs->hcca;
130 u32 startframe = hcca->frame_no;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400131 u32 end = timer_calc(1000 * 5);
Kevin O'Connor98af3532012-02-20 12:54:49 -0500132 for (;;) {
133 if (hcca->frame_no != startframe)
134 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400135 if (timer_check(end)) {
Kevin O'Connor98af3532012-02-20 12:54:49 -0500136 warn_timeout();
137 return;
138 }
139 yield();
140 }
141}
142
143static void
144ohci_free_pipes(struct usb_ohci_s *cntl)
145{
146 dprintf(7, "ohci_free_pipes %p\n", cntl);
147
148 u32 creg = readl(&cntl->regs->control);
149 if (creg & (OHCI_CTRL_CLE|OHCI_CTRL_BLE)) {
150 writel(&cntl->regs->control, creg & ~(OHCI_CTRL_CLE|OHCI_CTRL_BLE));
151 ohci_waittick(cntl);
152 }
153
154 u32 *pos = &cntl->regs->ed_controlhead;
155 for (;;) {
156 struct ohci_ed *next = (void*)*pos;
157 if (!next)
158 break;
159 struct ohci_pipe *pipe = container_of(next, struct ohci_pipe, ed);
160 if (pipe->pipe.cntl != &cntl->usb) {
161 *pos = next->hwNextED;
162 free(pipe);
163 } else {
164 pos = &next->hwNextED;
165 }
166 }
167
168 writel(&cntl->regs->ed_controlcurrent, 0);
169 writel(&cntl->regs->ed_bulkcurrent, 0);
170 writel(&cntl->regs->control, creg);
171 cntl->usb.freelist = NULL;
172}
173
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400174static int
Kevin O'Connor406fad62010-02-28 02:23:19 -0500175start_ohci(struct usb_ohci_s *cntl, struct ohci_hcca *hcca)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400176{
Kevin O'Connor406fad62010-02-28 02:23:19 -0500177 u32 oldfminterval = readl(&cntl->regs->fminterval);
178 u32 oldrwc = readl(&cntl->regs->control) & OHCI_CTRL_RWC;
Kevin O'Connor59f02832009-10-12 10:09:15 -0400179
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400180 // XXX - check if already running?
Kevin O'Connor59f02832009-10-12 10:09:15 -0400181
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400182 // Do reset
Kevin O'Connor406fad62010-02-28 02:23:19 -0500183 writel(&cntl->regs->control, OHCI_USB_RESET | oldrwc);
184 readl(&cntl->regs->control); // flush writes
Kevin O'Connor8bbc79c2010-02-14 12:16:32 -0500185 msleep(USB_TIME_DRSTR);
Kevin O'Connor59f02832009-10-12 10:09:15 -0400186
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400187 // Do software init (min 10us, max 2ms)
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400188 u32 end = timer_calc_usec(10);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500189 writel(&cntl->regs->cmdstatus, OHCI_HCR);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400190 for (;;) {
Kevin O'Connor406fad62010-02-28 02:23:19 -0500191 u32 status = readl(&cntl->regs->cmdstatus);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400192 if (! status & OHCI_HCR)
193 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400194 if (timer_check(end)) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500195 warn_timeout();
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400196 return -1;
197 }
Kevin O'Connor59f02832009-10-12 10:09:15 -0400198 }
199
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400200 // Init memory
Kevin O'Connor406fad62010-02-28 02:23:19 -0500201 writel(&cntl->regs->ed_controlhead, 0);
202 writel(&cntl->regs->ed_bulkhead, 0);
203 writel(&cntl->regs->hcca, (u32)hcca);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400204
205 // Init fminterval
206 u32 fi = oldfminterval & 0x3fff;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500207 writel(&cntl->regs->fminterval
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400208 , (((oldfminterval & FIT) ^ FIT)
209 | fi | (((6 * (fi - 210)) / 7) << 16)));
Kevin O'Connor406fad62010-02-28 02:23:19 -0500210 writel(&cntl->regs->periodicstart, ((9 * fi) / 10) & 0x3fff);
211 readl(&cntl->regs->control); // flush writes
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400212
213 // XXX - verify that fminterval was setup correctly.
214
215 // Go into operational state
Kevin O'Connor406fad62010-02-28 02:23:19 -0500216 writel(&cntl->regs->control
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400217 , (OHCI_CTRL_CBSR | OHCI_CTRL_CLE | OHCI_CTRL_PLE
218 | OHCI_USB_OPER | oldrwc));
Kevin O'Connor406fad62010-02-28 02:23:19 -0500219 readl(&cntl->regs->control); // flush writes
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400220
221 return 0;
Kevin O'Connor59f02832009-10-12 10:09:15 -0400222}
223
224static void
Kevin O'Connor406fad62010-02-28 02:23:19 -0500225stop_ohci(struct usb_ohci_s *cntl)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400226{
Kevin O'Connor406fad62010-02-28 02:23:19 -0500227 u32 oldrwc = readl(&cntl->regs->control) & OHCI_CTRL_RWC;
228 writel(&cntl->regs->control, oldrwc);
229 readl(&cntl->regs->control); // flush writes
Kevin O'Connor59f02832009-10-12 10:09:15 -0400230}
231
Kevin O'Connor406fad62010-02-28 02:23:19 -0500232static void
233configure_ohci(void *data)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400234{
Kevin O'Connor406fad62010-02-28 02:23:19 -0500235 struct usb_ohci_s *cntl = data;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400236
237 // Allocate memory
238 struct ohci_hcca *hcca = memalign_high(256, sizeof(*hcca));
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500239 struct ohci_ed *intr_ed = malloc_high(sizeof(*intr_ed));
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500240 if (!hcca || !intr_ed) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500241 warn_noalloc();
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500242 goto free;
Kevin O'Connor59f02832009-10-12 10:09:15 -0400243 }
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400244 memset(hcca, 0, sizeof(*hcca));
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500245 memset(intr_ed, 0, sizeof(*intr_ed));
246 intr_ed->hwINFO = ED_SKIP;
247 int i;
248 for (i=0; i<ARRAY_SIZE(hcca->int_table); i++)
249 hcca->int_table[i] = (u32)intr_ed;
Kevin O'Connor59f02832009-10-12 10:09:15 -0400250
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400251 int ret = start_ohci(cntl, hcca);
252 if (ret)
253 goto err;
254
255 int count = check_ohci_ports(cntl);
Kevin O'Connor98af3532012-02-20 12:54:49 -0500256 ohci_free_pipes(cntl);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400257 if (! count)
258 goto err;
Kevin O'Connora5826b52009-10-24 17:57:29 -0400259 return;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400260
261err:
262 stop_ohci(cntl);
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500263free:
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400264 free(hcca);
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500265 free(intr_ed);
Kevin O'Connor59f02832009-10-12 10:09:15 -0400266}
267
Kevin O'Connor406fad62010-02-28 02:23:19 -0500268void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500269ohci_setup(struct pci_device *pci, int busid)
Kevin O'Connor406fad62010-02-28 02:23:19 -0500270{
271 if (! CONFIG_USB_OHCI)
272 return;
273 struct usb_ohci_s *cntl = malloc_tmphigh(sizeof(*cntl));
Kevin O'Connorffdcd3a2011-07-10 15:48:00 -0400274 if (!cntl) {
275 warn_noalloc();
276 return;
277 }
Kevin O'Connor406fad62010-02-28 02:23:19 -0500278 memset(cntl, 0, sizeof(*cntl));
279 cntl->usb.busid = busid;
Kevin O'Connor8ff8e012011-07-09 14:11:21 -0400280 cntl->usb.pci = pci;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500281 cntl->usb.type = USB_TYPE_OHCI;
282
Kevin O'Connor8ff8e012011-07-09 14:11:21 -0400283 u16 bdf = pci->bdf;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500284 u32 baseaddr = pci_config_readl(bdf, PCI_BASE_ADDRESS_0);
285 cntl->regs = (void*)(baseaddr & PCI_BASE_ADDRESS_MEM_MASK);
286
Kevin O'Connor9dc243e2010-03-20 17:53:03 -0400287 dprintf(1, "OHCI init on dev %02x:%02x.%x (regs=%p)\n"
Kevin O'Connor406fad62010-02-28 02:23:19 -0500288 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf)
289 , pci_bdf_to_fn(bdf), cntl->regs);
290
291 // Enable bus mastering and memory access.
292 pci_config_maskw(bdf, PCI_COMMAND
293 , 0, PCI_COMMAND_MASTER|PCI_COMMAND_MEMORY);
294
295 // XXX - check for and disable SMM control?
296
297 // Disable interrupts
298 writel(&cntl->regs->intrdisable, ~0);
299 writel(&cntl->regs->intrstatus, ~0);
300
301 run_thread(configure_ohci, cntl);
302}
303
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500304
305/****************************************************************
306 * End point communication
307 ****************************************************************/
308
Kevin O'Connor0423dda2012-03-10 12:12:27 -0500309// Setup fields in ed
310static void
311ohci_desc2pipe(struct ohci_pipe *pipe, struct usbdevice_s *usbdev
312 , struct usb_endpoint_descriptor *epdesc)
313{
314 usb_desc2pipe(&pipe->pipe, usbdev, epdesc);
315 pipe->ed.hwINFO = (ED_SKIP | usbdev->devaddr | (pipe->pipe.ep << 7)
316 | (epdesc->wMaxPacketSize << 16)
317 | (usbdev->speed ? ED_LOWSPEED : 0));
318}
319
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500320static struct usb_pipe *
Kevin O'Connor6152c322012-03-10 08:53:58 -0500321ohci_alloc_intr_pipe(struct usbdevice_s *usbdev
322 , struct usb_endpoint_descriptor *epdesc)
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400323{
Kevin O'Connor6152c322012-03-10 08:53:58 -0500324 struct usb_ohci_s *cntl = container_of(
325 usbdev->hub->cntl, struct usb_ohci_s, usb);
326 int frameexp = usb_getFrameExp(usbdev, epdesc);
327 dprintf(7, "ohci_alloc_intr_pipe %p %d\n", &cntl->usb, frameexp);
328
329 if (frameexp > 5)
330 frameexp = 5;
331 int maxpacket = epdesc->wMaxPacketSize;
332 // Determine number of entries needed for 2 timer ticks.
333 int ms = 1<<frameexp;
Kevin O'Connor69013372013-07-20 12:08:48 -0400334 int count = DIV_ROUND_UP(ticks_to_ms(2), ms) + 1;
Kevin O'Connor6152c322012-03-10 08:53:58 -0500335 struct ohci_pipe *pipe = malloc_low(sizeof(*pipe));
336 struct ohci_td *tds = malloc_low(sizeof(*tds) * count);
337 void *data = malloc_low(maxpacket * count);
338 if (!pipe || !tds || !data)
339 goto err;
340 memset(pipe, 0, sizeof(*pipe));
Kevin O'Connor0423dda2012-03-10 12:12:27 -0500341 ohci_desc2pipe(pipe, usbdev, epdesc);
342 pipe->ed.hwINFO &= ~ED_SKIP;
Kevin O'Connor6152c322012-03-10 08:53:58 -0500343 pipe->data = data;
344 pipe->count = count;
345 pipe->tds = tds;
346
347 struct ohci_ed *ed = &pipe->ed;
348 ed->hwHeadP = (u32)&tds[0];
349 ed->hwTailP = (u32)&tds[count-1];
Kevin O'Connor6152c322012-03-10 08:53:58 -0500350
351 int i;
352 for (i=0; i<count-1; i++) {
353 tds[i].hwINFO = TD_DP_IN | TD_T_TOGGLE | TD_CC;
354 tds[i].hwCBP = (u32)data + maxpacket * i;
355 tds[i].hwNextTD = (u32)&tds[i+1];
356 tds[i].hwBE = tds[i].hwCBP + maxpacket - 1;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400357 }
Kevin O'Connor6152c322012-03-10 08:53:58 -0500358
359 // Add to interrupt schedule.
Kevin O'Connor6152c322012-03-10 08:53:58 -0500360 struct ohci_hcca *hcca = (void*)cntl->regs->hcca;
361 if (frameexp == 0) {
362 // Add to existing interrupt entry.
363 struct ohci_ed *intr_ed = (void*)hcca->int_table[0];
364 ed->hwNextED = intr_ed->hwNextED;
Kevin O'Connorc37147d2012-03-10 12:30:27 -0500365 barrier();
Kevin O'Connor6152c322012-03-10 08:53:58 -0500366 intr_ed->hwNextED = (u32)ed;
367 } else {
368 int startpos = 1<<(frameexp-1);
369 ed->hwNextED = hcca->int_table[startpos];
Kevin O'Connorc37147d2012-03-10 12:30:27 -0500370 barrier();
Kevin O'Connor6152c322012-03-10 08:53:58 -0500371 for (i=startpos; i<ARRAY_SIZE(hcca->int_table); i+=ms)
372 hcca->int_table[i] = (u32)ed;
373 }
374
375 return &pipe->pipe;
376
377err:
378 free(pipe);
379 free(tds);
380 free(data);
381 return NULL;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400382}
383
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500384struct usb_pipe *
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500385ohci_alloc_pipe(struct usbdevice_s *usbdev
386 , struct usb_endpoint_descriptor *epdesc)
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500387{
388 if (! CONFIG_USB_OHCI)
389 return NULL;
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500390 u8 eptype = epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500391 if (eptype == USB_ENDPOINT_XFER_INT)
392 return ohci_alloc_intr_pipe(usbdev, epdesc);
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500393 if (eptype != USB_ENDPOINT_XFER_CONTROL) {
Kevin O'Connor01de9bd2012-03-05 22:41:21 -0500394 dprintf(1, "OHCI Bulk transfers not supported.\n");
395 return NULL;
396 }
Kevin O'Connor406fad62010-02-28 02:23:19 -0500397 struct usb_ohci_s *cntl = container_of(
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500398 usbdev->hub->cntl, struct usb_ohci_s, usb);
Kevin O'Connor01de9bd2012-03-05 22:41:21 -0500399 dprintf(7, "ohci_alloc_async_pipe %p\n", &cntl->usb);
Kevin O'Connor98af3532012-02-20 12:54:49 -0500400
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500401 struct usb_pipe *usbpipe = usb_getFreePipe(&cntl->usb, eptype);
402 if (usbpipe) {
403 // Use previously allocated pipe.
Kevin O'Connor0423dda2012-03-10 12:12:27 -0500404 struct ohci_pipe *pipe = container_of(usbpipe, struct ohci_pipe, pipe);
405 ohci_desc2pipe(pipe, usbdev, epdesc);
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500406 return usbpipe;
407 }
408
Kevin O'Connor98af3532012-02-20 12:54:49 -0500409 // Allocate a new queue head.
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500410 struct ohci_pipe *pipe = malloc_tmphigh(sizeof(*pipe));
411 if (!pipe) {
412 warn_noalloc();
413 return NULL;
414 }
415 memset(pipe, 0, sizeof(*pipe));
Kevin O'Connor0423dda2012-03-10 12:12:27 -0500416 ohci_desc2pipe(pipe, usbdev, epdesc);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500417
418 // Add queue head to controller list.
Kevin O'Connor406fad62010-02-28 02:23:19 -0500419 pipe->ed.hwNextED = cntl->regs->ed_controlhead;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500420 barrier();
Kevin O'Connor406fad62010-02-28 02:23:19 -0500421 cntl->regs->ed_controlhead = (u32)&pipe->ed;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500422 return &pipe->pipe;
423}
424
Kevin O'Connor6152c322012-03-10 08:53:58 -0500425static int
426wait_ed(struct ohci_ed *ed)
427{
428 // XXX - 500ms just a guess
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400429 u32 end = timer_calc(500);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500430 for (;;) {
431 if (ed->hwHeadP == ed->hwTailP)
432 return 0;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400433 if (timer_check(end)) {
Kevin O'Connor6152c322012-03-10 08:53:58 -0500434 warn_timeout();
435 return -1;
436 }
437 yield();
438 }
439}
440
Kevin O'Connor59f02832009-10-12 10:09:15 -0400441int
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500442ohci_control(struct usb_pipe *p, int dir, const void *cmd, int cmdsize
Kevin O'Connor59f02832009-10-12 10:09:15 -0400443 , void *data, int datasize)
444{
445 if (! CONFIG_USB_OHCI)
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400446 return -1;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500447 dprintf(5, "ohci_control %p\n", p);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500448 if (datasize > 4096) {
449 // XXX - should support larger sizes.
450 warn_noalloc();
451 return -1;
452 }
453 struct ohci_pipe *pipe = container_of(p, struct ohci_pipe, pipe);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500454 struct usb_ohci_s *cntl = container_of(
455 pipe->pipe.cntl, struct usb_ohci_s, usb);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400456
457 // Setup transfer descriptors
458 struct ohci_td *tds = malloc_tmphigh(sizeof(*tds) * 3);
Kevin O'Connor0770d672010-03-09 19:33:22 -0500459 if (!tds) {
460 warn_noalloc();
461 return -1;
462 }
463 struct ohci_td *td = tds;
464 td->hwINFO = TD_DP_SETUP | TD_T_DATA0 | TD_CC;
465 td->hwCBP = (u32)cmd;
466 td->hwNextTD = (u32)&td[1];
467 td->hwBE = (u32)cmd + cmdsize - 1;
468 td++;
469 if (datasize) {
470 td->hwINFO = (dir ? TD_DP_IN : TD_DP_OUT) | TD_T_DATA1 | TD_CC;
471 td->hwCBP = (u32)data;
472 td->hwNextTD = (u32)&td[1];
473 td->hwBE = (u32)data + datasize - 1;
474 td++;
475 }
476 td->hwINFO = (dir ? TD_DP_OUT : TD_DP_IN) | TD_T_DATA1 | TD_CC;
477 td->hwCBP = 0;
478 td->hwNextTD = (u32)&td[1];
479 td->hwBE = 0;
480 td++;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400481
482 // Transfer data
Kevin O'Connor0770d672010-03-09 19:33:22 -0500483 pipe->ed.hwHeadP = (u32)tds;
484 pipe->ed.hwTailP = (u32)td;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400485 barrier();
Kevin O'Connor0423dda2012-03-10 12:12:27 -0500486 pipe->ed.hwINFO &= ~ED_SKIP;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500487 writel(&cntl->regs->cmdstatus, OHCI_CLF);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400488
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500489 int ret = wait_ed(&pipe->ed);
Kevin O'Connor0423dda2012-03-10 12:12:27 -0500490 pipe->ed.hwINFO |= ED_SKIP;
Kevin O'Connorad901592009-12-13 11:25:25 -0500491 if (ret)
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500492 ohci_waittick(cntl);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400493 free(tds);
494 return ret;
Kevin O'Connor59f02832009-10-12 10:09:15 -0400495}
496
Kevin O'Connor2f968002010-09-19 22:10:08 -0400497int
498ohci_send_bulk(struct usb_pipe *p, int dir, void *data, int datasize)
499{
500 return -1;
501}
502
Kevin O'Connor59f02832009-10-12 10:09:15 -0400503int
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500504ohci_poll_intr(struct usb_pipe *p, void *data)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400505{
506 ASSERT16();
507 if (! CONFIG_USB_OHCI)
508 return -1;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400509
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500510 struct ohci_pipe *pipe = container_of(p, struct ohci_pipe, pipe);
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400511 struct ohci_td *tds = GET_LOWFLAT(pipe->tds);
512 struct ohci_td *head = (void*)(GET_LOWFLAT(pipe->ed.hwHeadP) & ~(ED_C|ED_H));
513 struct ohci_td *tail = (void*)GET_LOWFLAT(pipe->ed.hwTailP);
514 int count = GET_LOWFLAT(pipe->count);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400515 int pos = (tail - tds + 1) % count;
516 struct ohci_td *next = &tds[pos];
517 if (head == next)
518 // No intrs found.
519 return -1;
520 // XXX - check for errors.
521
522 // Copy data.
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400523 int maxpacket = GET_LOWFLAT(pipe->pipe.maxpacket);
524 void *pipedata = GET_LOWFLAT((pipe->data));
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400525 void *intrdata = pipedata + maxpacket * pos;
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400526 memcpy_far(GET_SEG(SS), data, SEG_LOW, LOWFLAT2LOW(intrdata), maxpacket);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400527
528 // Reenable this td.
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400529 SET_LOWFLAT(tail->hwINFO, TD_DP_IN | TD_T_TOGGLE | TD_CC);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400530 intrdata = pipedata + maxpacket * (tail-tds);
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400531 SET_LOWFLAT(tail->hwCBP, (u32)intrdata);
532 SET_LOWFLAT(tail->hwNextTD, (u32)next);
533 SET_LOWFLAT(tail->hwBE, (u32)intrdata + maxpacket - 1);
Kevin O'Connor0770d672010-03-09 19:33:22 -0500534 barrier();
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400535 SET_LOWFLAT(pipe->ed.hwTailP, (u32)next);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400536
537 return 0;
Kevin O'Connor59f02832009-10-12 10:09:15 -0400538}