blob: ee31b8361cbb0065f603da0dc420d928fca0c2ed [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'Connor746d0a72014-12-29 13:06:23 -050010#include "memmap.h" // PAGE_SIZE
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040011#include "output.h" // dprintf
12#include "pci.h" // pci_bdf_to_bus
Kevin O'Connorec443ff2013-12-05 18:43:20 -050013#include "pci_ids.h" // PCI_CLASS_SERIAL_USB_OHCI
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040014#include "pci_regs.h" // PCI_BASE_ADDRESS_0
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040015#include "string.h" // memset
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040016#include "usb.h" // struct usb_s
17#include "usb-ohci.h" // struct ohci_hcca
18#include "util.h" // msleep
Kevin O'Connorb9c6a962013-09-14 13:01:30 -040019#include "x86.h" // readl
Kevin O'Connor59f02832009-10-12 10:09:15 -040020
Kevin O'Connor1c46a542009-10-17 23:53:32 -040021#define FIT (1 << 31)
22
Kevin O'Connor406fad62010-02-28 02:23:19 -050023struct usb_ohci_s {
24 struct usb_s usb;
25 struct ohci_regs *regs;
26};
27
Kevin O'Connor98af3532012-02-20 12:54:49 -050028struct ohci_pipe {
29 struct ohci_ed ed;
30 struct usb_pipe pipe;
Kevin O'Connor746d0a72014-12-29 13:06:23 -050031 struct ohci_regs *regs;
Kevin O'Connor98af3532012-02-20 12:54:49 -050032 void *data;
33 int count;
34 struct ohci_td *tds;
35};
36
Kevin O'Connor357bdfa2010-02-26 08:57:13 -050037
38/****************************************************************
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050039 * Root hub
40 ****************************************************************/
41
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040042// Check if device attached to port
43static int
44ohci_hub_detect(struct usbhub_s *hub, u32 port)
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050045{
Kevin O'Connor406fad62010-02-28 02:23:19 -050046 struct usb_ohci_s *cntl = container_of(hub->cntl, struct usb_ohci_s, usb);
Kevin O'Connor0f681302014-09-10 11:33:01 -040047 u32 sts = readl(&cntl->regs->roothub_portstatus[port]);
48 return (sts & RH_PS_CCS) ? 1 : 0;
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040049}
50
51// Disable port
52static void
53ohci_hub_disconnect(struct usbhub_s *hub, u32 port)
54{
55 struct usb_ohci_s *cntl = container_of(hub->cntl, struct usb_ohci_s, usb);
56 writel(&cntl->regs->roothub_portstatus[port], RH_PS_CCS|RH_PS_LSDA);
57}
58
59// Reset device on port
60static int
61ohci_hub_reset(struct usbhub_s *hub, u32 port)
62{
63 struct usb_ohci_s *cntl = container_of(hub->cntl, struct usb_ohci_s, usb);
Kevin O'Connor406fad62010-02-28 02:23:19 -050064 writel(&cntl->regs->roothub_portstatus[port], RH_PS_PRS);
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040065 u32 sts;
Kevin O'Connor018bdd72013-07-20 18:22:57 -040066 u32 end = timer_calc(USB_TIME_DRSTR * 2);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050067 for (;;) {
Kevin O'Connor406fad62010-02-28 02:23:19 -050068 sts = readl(&cntl->regs->roothub_portstatus[port]);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050069 if (!(sts & RH_PS_PRS))
70 // XXX - need to ensure USB_TIME_DRSTR time in reset?
71 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -040072 if (timer_check(end)) {
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050073 // Timeout.
74 warn_timeout();
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040075 ohci_hub_disconnect(hub, port);
76 return -1;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050077 }
78 yield();
79 }
80
81 if ((sts & (RH_PS_CCS|RH_PS_PES)) != (RH_PS_CCS|RH_PS_PES))
82 // Device no longer present
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040083 return -1;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050084
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040085 return !!(sts & RH_PS_LSDA);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050086}
87
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040088static struct usbhub_op_s ohci_HubOp = {
89 .detect = ohci_hub_detect,
90 .reset = ohci_hub_reset,
91 .disconnect = ohci_hub_disconnect,
92};
93
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050094// Find any devices connected to the root hub.
95static int
Kevin O'Connor406fad62010-02-28 02:23:19 -050096check_ohci_ports(struct usb_ohci_s *cntl)
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050097{
98 ASSERT32FLAT();
99 // Turn on power for all devices on roothub.
Kevin O'Connor406fad62010-02-28 02:23:19 -0500100 u32 rha = readl(&cntl->regs->roothub_a);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500101 rha &= ~(RH_A_PSM | RH_A_OCPM);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500102 writel(&cntl->regs->roothub_status, RH_HS_LPSC);
103 writel(&cntl->regs->roothub_b, RH_B_PPCM);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500104 msleep((rha >> 24) * 2);
105 // XXX - need to sleep for USB_TIME_SIGATT if just powered up?
106
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500107 struct usbhub_s hub;
108 memset(&hub, 0, sizeof(hub));
Kevin O'Connor406fad62010-02-28 02:23:19 -0500109 hub.cntl = &cntl->usb;
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400110 hub.portcount = rha & RH_A_NDP;
111 hub.op = &ohci_HubOp;
112 usb_enumerate(&hub);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500113 return hub.devcount;
114}
115
116
117/****************************************************************
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500118 * Setup
119 ****************************************************************/
120
Kevin O'Connor98af3532012-02-20 12:54:49 -0500121// Wait for next USB frame to start - for ensuring safe memory release.
122static void
Kevin O'Connor746d0a72014-12-29 13:06:23 -0500123ohci_waittick(struct ohci_regs *regs)
Kevin O'Connor98af3532012-02-20 12:54:49 -0500124{
125 barrier();
Kevin O'Connor746d0a72014-12-29 13:06:23 -0500126 struct ohci_hcca *hcca = (void*)regs->hcca;
Kevin O'Connor98af3532012-02-20 12:54:49 -0500127 u32 startframe = hcca->frame_no;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400128 u32 end = timer_calc(1000 * 5);
Kevin O'Connor98af3532012-02-20 12:54:49 -0500129 for (;;) {
130 if (hcca->frame_no != startframe)
131 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400132 if (timer_check(end)) {
Kevin O'Connor98af3532012-02-20 12:54:49 -0500133 warn_timeout();
134 return;
135 }
136 yield();
137 }
138}
139
140static void
141ohci_free_pipes(struct usb_ohci_s *cntl)
142{
143 dprintf(7, "ohci_free_pipes %p\n", cntl);
144
145 u32 creg = readl(&cntl->regs->control);
146 if (creg & (OHCI_CTRL_CLE|OHCI_CTRL_BLE)) {
147 writel(&cntl->regs->control, creg & ~(OHCI_CTRL_CLE|OHCI_CTRL_BLE));
Kevin O'Connor746d0a72014-12-29 13:06:23 -0500148 ohci_waittick(cntl->regs);
Kevin O'Connor98af3532012-02-20 12:54:49 -0500149 }
150
151 u32 *pos = &cntl->regs->ed_controlhead;
152 for (;;) {
153 struct ohci_ed *next = (void*)*pos;
154 if (!next)
155 break;
156 struct ohci_pipe *pipe = container_of(next, struct ohci_pipe, ed);
Kevin O'Connor2bfd1702014-10-16 12:31:42 -0400157 if (usb_is_freelist(&cntl->usb, &pipe->pipe)) {
Kevin O'Connor98af3532012-02-20 12:54:49 -0500158 *pos = next->hwNextED;
159 free(pipe);
160 } else {
161 pos = &next->hwNextED;
162 }
163 }
164
165 writel(&cntl->regs->ed_controlcurrent, 0);
166 writel(&cntl->regs->ed_bulkcurrent, 0);
167 writel(&cntl->regs->control, creg);
168 cntl->usb.freelist = NULL;
169}
170
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400171static int
Kevin O'Connor406fad62010-02-28 02:23:19 -0500172start_ohci(struct usb_ohci_s *cntl, struct ohci_hcca *hcca)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400173{
Kevin O'Connor406fad62010-02-28 02:23:19 -0500174 u32 oldfminterval = readl(&cntl->regs->fminterval);
175 u32 oldrwc = readl(&cntl->regs->control) & OHCI_CTRL_RWC;
Kevin O'Connor59f02832009-10-12 10:09:15 -0400176
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400177 // XXX - check if already running?
Kevin O'Connor59f02832009-10-12 10:09:15 -0400178
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400179 // Do reset
Kevin O'Connor406fad62010-02-28 02:23:19 -0500180 writel(&cntl->regs->control, OHCI_USB_RESET | oldrwc);
181 readl(&cntl->regs->control); // flush writes
Kevin O'Connor8bbc79c2010-02-14 12:16:32 -0500182 msleep(USB_TIME_DRSTR);
Kevin O'Connor59f02832009-10-12 10:09:15 -0400183
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400184 // Do software init (min 10us, max 2ms)
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400185 u32 end = timer_calc_usec(10);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500186 writel(&cntl->regs->cmdstatus, OHCI_HCR);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400187 for (;;) {
Kevin O'Connor406fad62010-02-28 02:23:19 -0500188 u32 status = readl(&cntl->regs->cmdstatus);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400189 if (! status & OHCI_HCR)
190 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400191 if (timer_check(end)) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500192 warn_timeout();
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400193 return -1;
194 }
Kevin O'Connor59f02832009-10-12 10:09:15 -0400195 }
196
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400197 // Init memory
Kevin O'Connor406fad62010-02-28 02:23:19 -0500198 writel(&cntl->regs->ed_controlhead, 0);
199 writel(&cntl->regs->ed_bulkhead, 0);
200 writel(&cntl->regs->hcca, (u32)hcca);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400201
202 // Init fminterval
203 u32 fi = oldfminterval & 0x3fff;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500204 writel(&cntl->regs->fminterval
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400205 , (((oldfminterval & FIT) ^ FIT)
206 | fi | (((6 * (fi - 210)) / 7) << 16)));
Kevin O'Connor406fad62010-02-28 02:23:19 -0500207 writel(&cntl->regs->periodicstart, ((9 * fi) / 10) & 0x3fff);
208 readl(&cntl->regs->control); // flush writes
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400209
210 // XXX - verify that fminterval was setup correctly.
211
212 // Go into operational state
Kevin O'Connor406fad62010-02-28 02:23:19 -0500213 writel(&cntl->regs->control
Kevin O'Connor746d0a72014-12-29 13:06:23 -0500214 , (OHCI_CTRL_CBSR | OHCI_CTRL_CLE | OHCI_CTRL_BLE | OHCI_CTRL_PLE
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400215 | OHCI_USB_OPER | oldrwc));
Kevin O'Connor406fad62010-02-28 02:23:19 -0500216 readl(&cntl->regs->control); // flush writes
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400217
218 return 0;
Kevin O'Connor59f02832009-10-12 10:09:15 -0400219}
220
221static void
Kevin O'Connor406fad62010-02-28 02:23:19 -0500222stop_ohci(struct usb_ohci_s *cntl)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400223{
Kevin O'Connor406fad62010-02-28 02:23:19 -0500224 u32 oldrwc = readl(&cntl->regs->control) & OHCI_CTRL_RWC;
225 writel(&cntl->regs->control, oldrwc);
226 readl(&cntl->regs->control); // flush writes
Kevin O'Connor59f02832009-10-12 10:09:15 -0400227}
228
Kevin O'Connor406fad62010-02-28 02:23:19 -0500229static void
230configure_ohci(void *data)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400231{
Kevin O'Connor406fad62010-02-28 02:23:19 -0500232 struct usb_ohci_s *cntl = data;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400233
234 // Allocate memory
235 struct ohci_hcca *hcca = memalign_high(256, sizeof(*hcca));
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500236 struct ohci_ed *intr_ed = malloc_high(sizeof(*intr_ed));
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500237 if (!hcca || !intr_ed) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500238 warn_noalloc();
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500239 goto free;
Kevin O'Connor59f02832009-10-12 10:09:15 -0400240 }
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400241 memset(hcca, 0, sizeof(*hcca));
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500242 memset(intr_ed, 0, sizeof(*intr_ed));
243 intr_ed->hwINFO = ED_SKIP;
244 int i;
245 for (i=0; i<ARRAY_SIZE(hcca->int_table); i++)
246 hcca->int_table[i] = (u32)intr_ed;
Kevin O'Connor59f02832009-10-12 10:09:15 -0400247
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400248 int ret = start_ohci(cntl, hcca);
249 if (ret)
250 goto err;
251
252 int count = check_ohci_ports(cntl);
Kevin O'Connor98af3532012-02-20 12:54:49 -0500253 ohci_free_pipes(cntl);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400254 if (! count)
255 goto err;
Kevin O'Connora5826b52009-10-24 17:57:29 -0400256 return;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400257
258err:
259 stop_ohci(cntl);
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500260free:
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400261 free(hcca);
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500262 free(intr_ed);
Kevin O'Connor59f02832009-10-12 10:09:15 -0400263}
264
Kevin O'Connorec443ff2013-12-05 18:43:20 -0500265static void
266ohci_controller_setup(struct pci_device *pci)
Kevin O'Connor406fad62010-02-28 02:23:19 -0500267{
Kevin O'Connor406fad62010-02-28 02:23:19 -0500268 struct usb_ohci_s *cntl = malloc_tmphigh(sizeof(*cntl));
Kevin O'Connorffdcd3a2011-07-10 15:48:00 -0400269 if (!cntl) {
270 warn_noalloc();
271 return;
272 }
Kevin O'Connor406fad62010-02-28 02:23:19 -0500273 memset(cntl, 0, sizeof(*cntl));
Kevin O'Connor8ff8e012011-07-09 14:11:21 -0400274 cntl->usb.pci = pci;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500275 cntl->usb.type = USB_TYPE_OHCI;
276
Kevin O'Connorec443ff2013-12-05 18:43:20 -0500277 wait_preempt(); // Avoid pci_config_readl when preempting
Kevin O'Connor8ff8e012011-07-09 14:11:21 -0400278 u16 bdf = pci->bdf;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500279 u32 baseaddr = pci_config_readl(bdf, PCI_BASE_ADDRESS_0);
280 cntl->regs = (void*)(baseaddr & PCI_BASE_ADDRESS_MEM_MASK);
281
Kevin O'Connor9dc243e2010-03-20 17:53:03 -0400282 dprintf(1, "OHCI init on dev %02x:%02x.%x (regs=%p)\n"
Kevin O'Connor406fad62010-02-28 02:23:19 -0500283 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf)
284 , pci_bdf_to_fn(bdf), cntl->regs);
285
286 // Enable bus mastering and memory access.
287 pci_config_maskw(bdf, PCI_COMMAND
288 , 0, PCI_COMMAND_MASTER|PCI_COMMAND_MEMORY);
289
290 // XXX - check for and disable SMM control?
291
292 // Disable interrupts
293 writel(&cntl->regs->intrdisable, ~0);
294 writel(&cntl->regs->intrstatus, ~0);
295
296 run_thread(configure_ohci, cntl);
297}
298
Kevin O'Connorec443ff2013-12-05 18:43:20 -0500299void
300ohci_setup(void)
301{
302 if (! CONFIG_USB_OHCI)
303 return;
304 struct pci_device *pci;
305 foreachpci(pci) {
306 if (pci_classprog(pci) == PCI_CLASS_SERIAL_USB_OHCI)
307 ohci_controller_setup(pci);
308 }
309}
310
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500311
312/****************************************************************
313 * End point communication
314 ****************************************************************/
315
Kevin O'Connor0423dda2012-03-10 12:12:27 -0500316// Setup fields in ed
317static void
318ohci_desc2pipe(struct ohci_pipe *pipe, struct usbdevice_s *usbdev
319 , struct usb_endpoint_descriptor *epdesc)
320{
321 usb_desc2pipe(&pipe->pipe, usbdev, epdesc);
322 pipe->ed.hwINFO = (ED_SKIP | usbdev->devaddr | (pipe->pipe.ep << 7)
323 | (epdesc->wMaxPacketSize << 16)
324 | (usbdev->speed ? ED_LOWSPEED : 0));
Kevin O'Connor746d0a72014-12-29 13:06:23 -0500325 struct usb_ohci_s *cntl = container_of(
326 usbdev->hub->cntl, struct usb_ohci_s, usb);
327 pipe->regs = cntl->regs;
Kevin O'Connor0423dda2012-03-10 12:12:27 -0500328}
329
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500330static struct usb_pipe *
Kevin O'Connor6152c322012-03-10 08:53:58 -0500331ohci_alloc_intr_pipe(struct usbdevice_s *usbdev
332 , struct usb_endpoint_descriptor *epdesc)
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400333{
Kevin O'Connor6152c322012-03-10 08:53:58 -0500334 struct usb_ohci_s *cntl = container_of(
335 usbdev->hub->cntl, struct usb_ohci_s, usb);
Kevin O'Connor98cdad32014-10-16 12:05:09 -0400336 int frameexp = usb_get_period(usbdev, epdesc);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500337 dprintf(7, "ohci_alloc_intr_pipe %p %d\n", &cntl->usb, frameexp);
338
339 if (frameexp > 5)
340 frameexp = 5;
341 int maxpacket = epdesc->wMaxPacketSize;
342 // Determine number of entries needed for 2 timer ticks.
343 int ms = 1<<frameexp;
Kevin O'Connor69013372013-07-20 12:08:48 -0400344 int count = DIV_ROUND_UP(ticks_to_ms(2), ms) + 1;
Kevin O'Connor6152c322012-03-10 08:53:58 -0500345 struct ohci_pipe *pipe = malloc_low(sizeof(*pipe));
346 struct ohci_td *tds = malloc_low(sizeof(*tds) * count);
347 void *data = malloc_low(maxpacket * count);
348 if (!pipe || !tds || !data)
349 goto err;
350 memset(pipe, 0, sizeof(*pipe));
Kevin O'Connor0423dda2012-03-10 12:12:27 -0500351 ohci_desc2pipe(pipe, usbdev, epdesc);
352 pipe->ed.hwINFO &= ~ED_SKIP;
Kevin O'Connor6152c322012-03-10 08:53:58 -0500353 pipe->data = data;
354 pipe->count = count;
355 pipe->tds = tds;
356
357 struct ohci_ed *ed = &pipe->ed;
358 ed->hwHeadP = (u32)&tds[0];
359 ed->hwTailP = (u32)&tds[count-1];
Kevin O'Connor6152c322012-03-10 08:53:58 -0500360
361 int i;
362 for (i=0; i<count-1; i++) {
363 tds[i].hwINFO = TD_DP_IN | TD_T_TOGGLE | TD_CC;
364 tds[i].hwCBP = (u32)data + maxpacket * i;
365 tds[i].hwNextTD = (u32)&tds[i+1];
366 tds[i].hwBE = tds[i].hwCBP + maxpacket - 1;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400367 }
Kevin O'Connor6152c322012-03-10 08:53:58 -0500368
369 // Add to interrupt schedule.
Kevin O'Connor6152c322012-03-10 08:53:58 -0500370 struct ohci_hcca *hcca = (void*)cntl->regs->hcca;
371 if (frameexp == 0) {
372 // Add to existing interrupt entry.
373 struct ohci_ed *intr_ed = (void*)hcca->int_table[0];
374 ed->hwNextED = intr_ed->hwNextED;
Kevin O'Connorc37147d2012-03-10 12:30:27 -0500375 barrier();
Kevin O'Connor6152c322012-03-10 08:53:58 -0500376 intr_ed->hwNextED = (u32)ed;
377 } else {
378 int startpos = 1<<(frameexp-1);
379 ed->hwNextED = hcca->int_table[startpos];
Kevin O'Connorc37147d2012-03-10 12:30:27 -0500380 barrier();
Kevin O'Connor6152c322012-03-10 08:53:58 -0500381 for (i=startpos; i<ARRAY_SIZE(hcca->int_table); i+=ms)
382 hcca->int_table[i] = (u32)ed;
383 }
384
385 return &pipe->pipe;
386
387err:
388 free(pipe);
389 free(tds);
390 free(data);
391 return NULL;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400392}
393
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500394struct usb_pipe *
Kevin O'Connor2e0c2ea2014-10-16 13:36:41 -0400395ohci_realloc_pipe(struct usbdevice_s *usbdev, struct usb_pipe *upipe
396 , struct usb_endpoint_descriptor *epdesc)
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500397{
398 if (! CONFIG_USB_OHCI)
399 return NULL;
Kevin O'Connor2e0c2ea2014-10-16 13:36:41 -0400400 usb_add_freelist(upipe);
401 if (!epdesc)
402 return NULL;
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500403 u8 eptype = epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500404 if (eptype == USB_ENDPOINT_XFER_INT)
405 return ohci_alloc_intr_pipe(usbdev, epdesc);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500406 struct usb_ohci_s *cntl = container_of(
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500407 usbdev->hub->cntl, struct usb_ohci_s, usb);
Kevin O'Connor01de9bd2012-03-05 22:41:21 -0500408 dprintf(7, "ohci_alloc_async_pipe %p\n", &cntl->usb);
Kevin O'Connor98af3532012-02-20 12:54:49 -0500409
Kevin O'Connor2bfd1702014-10-16 12:31:42 -0400410 struct usb_pipe *usbpipe = usb_get_freelist(&cntl->usb, eptype);
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500411 if (usbpipe) {
412 // Use previously allocated pipe.
Kevin O'Connor0423dda2012-03-10 12:12:27 -0500413 struct ohci_pipe *pipe = container_of(usbpipe, struct ohci_pipe, pipe);
414 ohci_desc2pipe(pipe, usbdev, epdesc);
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500415 return usbpipe;
416 }
417
Kevin O'Connor98af3532012-02-20 12:54:49 -0500418 // Allocate a new queue head.
Kevin O'Connor746d0a72014-12-29 13:06:23 -0500419 struct ohci_pipe *pipe;
420 if (eptype == USB_ENDPOINT_XFER_CONTROL)
421 pipe = malloc_tmphigh(sizeof(*pipe));
422 else
423 pipe = malloc_low(sizeof(*pipe));
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500424 if (!pipe) {
425 warn_noalloc();
426 return NULL;
427 }
428 memset(pipe, 0, sizeof(*pipe));
Kevin O'Connor0423dda2012-03-10 12:12:27 -0500429 ohci_desc2pipe(pipe, usbdev, epdesc);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500430
431 // Add queue head to controller list.
Kevin O'Connor746d0a72014-12-29 13:06:23 -0500432 u32 *head = &cntl->regs->ed_controlhead;
433 if (eptype != USB_ENDPOINT_XFER_CONTROL)
434 head = &cntl->regs->ed_bulkhead;
435 pipe->ed.hwNextED = *head;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500436 barrier();
Kevin O'Connor746d0a72014-12-29 13:06:23 -0500437 *head = (u32)&pipe->ed;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500438 return &pipe->pipe;
439}
440
Kevin O'Connor6152c322012-03-10 08:53:58 -0500441static int
Kevin O'Connor083ee292014-06-14 12:50:38 -0400442wait_ed(struct ohci_ed *ed, int timeout)
Kevin O'Connor6152c322012-03-10 08:53:58 -0500443{
Kevin O'Connor083ee292014-06-14 12:50:38 -0400444 u32 end = timer_calc(timeout);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500445 for (;;) {
Kevin O'Connor746d0a72014-12-29 13:06:23 -0500446 if ((ed->hwHeadP & ~(ED_C|ED_H)) == ed->hwTailP)
Kevin O'Connor6152c322012-03-10 08:53:58 -0500447 return 0;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400448 if (timer_check(end)) {
Kevin O'Connor6152c322012-03-10 08:53:58 -0500449 warn_timeout();
Kevin O'Connor746d0a72014-12-29 13:06:23 -0500450 dprintf(1, "ohci ed info=%x tail=%x head=%x next=%x\n"
451 , ed->hwINFO, ed->hwTailP, ed->hwHeadP, ed->hwNextED);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500452 return -1;
453 }
454 yield();
455 }
456}
457
Kevin O'Connor59f02832009-10-12 10:09:15 -0400458int
Kevin O'Connor20c9be32014-10-16 11:55:16 -0400459ohci_send_control(struct usb_pipe *p, int dir, const void *cmd, int cmdsize
460 , void *data, int datasize)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400461{
462 if (! CONFIG_USB_OHCI)
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400463 return -1;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500464 dprintf(5, "ohci_control %p\n", p);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500465 if (datasize > 4096) {
466 // XXX - should support larger sizes.
467 warn_noalloc();
468 return -1;
469 }
470 struct ohci_pipe *pipe = container_of(p, struct ohci_pipe, pipe);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400471
472 // Setup transfer descriptors
473 struct ohci_td *tds = malloc_tmphigh(sizeof(*tds) * 3);
Kevin O'Connor0770d672010-03-09 19:33:22 -0500474 if (!tds) {
475 warn_noalloc();
476 return -1;
477 }
478 struct ohci_td *td = tds;
479 td->hwINFO = TD_DP_SETUP | TD_T_DATA0 | TD_CC;
480 td->hwCBP = (u32)cmd;
481 td->hwNextTD = (u32)&td[1];
482 td->hwBE = (u32)cmd + cmdsize - 1;
483 td++;
484 if (datasize) {
485 td->hwINFO = (dir ? TD_DP_IN : TD_DP_OUT) | TD_T_DATA1 | TD_CC;
486 td->hwCBP = (u32)data;
487 td->hwNextTD = (u32)&td[1];
488 td->hwBE = (u32)data + datasize - 1;
489 td++;
490 }
491 td->hwINFO = (dir ? TD_DP_OUT : TD_DP_IN) | TD_T_DATA1 | TD_CC;
492 td->hwCBP = 0;
493 td->hwNextTD = (u32)&td[1];
494 td->hwBE = 0;
495 td++;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400496
497 // Transfer data
Kevin O'Connor0770d672010-03-09 19:33:22 -0500498 pipe->ed.hwHeadP = (u32)tds;
499 pipe->ed.hwTailP = (u32)td;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400500 barrier();
Kevin O'Connor0423dda2012-03-10 12:12:27 -0500501 pipe->ed.hwINFO &= ~ED_SKIP;
Kevin O'Connor746d0a72014-12-29 13:06:23 -0500502 writel(&pipe->regs->cmdstatus, OHCI_CLF);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400503
Kevin O'Connor083ee292014-06-14 12:50:38 -0400504 int ret = wait_ed(&pipe->ed, usb_xfer_time(p, datasize));
Kevin O'Connor0423dda2012-03-10 12:12:27 -0500505 pipe->ed.hwINFO |= ED_SKIP;
Kevin O'Connorad901592009-12-13 11:25:25 -0500506 if (ret)
Kevin O'Connor746d0a72014-12-29 13:06:23 -0500507 ohci_waittick(pipe->regs);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400508 free(tds);
509 return ret;
Kevin O'Connor59f02832009-10-12 10:09:15 -0400510}
511
Kevin O'Connor746d0a72014-12-29 13:06:23 -0500512#define STACKOTDS 16
513#define OHCI_TD_ALIGN 16
514
Kevin O'Connor2f968002010-09-19 22:10:08 -0400515int
516ohci_send_bulk(struct usb_pipe *p, int dir, void *data, int datasize)
517{
Kevin O'Connor746d0a72014-12-29 13:06:23 -0500518 ASSERT32FLAT();
519 if (! CONFIG_USB_OHCI)
520 return -1;
521 dprintf(7, "ohci_send_bulk %p\n", p);
522 struct ohci_pipe *pipe = container_of(p, struct ohci_pipe, pipe);
523
524 // Allocate 16 tds on stack (with required alignment)
525 u8 tdsbuf[sizeof(struct ohci_td) * STACKOTDS + OHCI_TD_ALIGN - 1];
526 struct ohci_td *tds = (void*)ALIGN((u32)tdsbuf, OHCI_TD_ALIGN), *td = tds;
527 memset(tds, 0, sizeof(*tds) * STACKOTDS);
528
529 // Setup transfer descriptors
530 u16 maxpacket = pipe->pipe.maxpacket;
531 u32 dest = (u32)data, dataend = dest + datasize;
532 while (dest < dataend) {
533 // Send data pids
534 if (td >= &tds[STACKOTDS]) {
535 warn_noalloc();
536 return -1;
537 }
538 int maxtransfer = 2*PAGE_SIZE - (dest & (PAGE_SIZE-1));
539 int transfer = dataend - dest;
540 if (transfer > maxtransfer)
541 transfer = ALIGN_DOWN(maxtransfer, maxpacket);
542 td->hwINFO = (dir ? TD_DP_IN : TD_DP_OUT) | TD_CC;
543 td->hwCBP = dest;
544 td->hwNextTD = (u32)&td[1];
545 td->hwBE = dest + transfer - 1;
546 td++;
547 dest += transfer;
548 }
549
550 // Transfer data
551 pipe->ed.hwHeadP = (u32)tds | (pipe->ed.hwHeadP & ED_C);
552 pipe->ed.hwTailP = (u32)td;
553 barrier();
554 pipe->ed.hwINFO &= ~ED_SKIP;
555 writel(&pipe->regs->cmdstatus, OHCI_BLF);
556
557 int ret = wait_ed(&pipe->ed, usb_xfer_time(p, datasize));
558 pipe->ed.hwINFO |= ED_SKIP;
559 if (ret)
560 ohci_waittick(pipe->regs);
561 return ret;
Kevin O'Connor2f968002010-09-19 22:10:08 -0400562}
563
Kevin O'Connor59f02832009-10-12 10:09:15 -0400564int
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500565ohci_poll_intr(struct usb_pipe *p, void *data)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400566{
567 ASSERT16();
568 if (! CONFIG_USB_OHCI)
569 return -1;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400570
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500571 struct ohci_pipe *pipe = container_of(p, struct ohci_pipe, pipe);
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400572 struct ohci_td *tds = GET_LOWFLAT(pipe->tds);
573 struct ohci_td *head = (void*)(GET_LOWFLAT(pipe->ed.hwHeadP) & ~(ED_C|ED_H));
574 struct ohci_td *tail = (void*)GET_LOWFLAT(pipe->ed.hwTailP);
575 int count = GET_LOWFLAT(pipe->count);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400576 int pos = (tail - tds + 1) % count;
577 struct ohci_td *next = &tds[pos];
578 if (head == next)
579 // No intrs found.
580 return -1;
581 // XXX - check for errors.
582
583 // Copy data.
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400584 int maxpacket = GET_LOWFLAT(pipe->pipe.maxpacket);
585 void *pipedata = GET_LOWFLAT((pipe->data));
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400586 void *intrdata = pipedata + maxpacket * pos;
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400587 memcpy_far(GET_SEG(SS), data, SEG_LOW, LOWFLAT2LOW(intrdata), maxpacket);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400588
589 // Reenable this td.
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400590 SET_LOWFLAT(tail->hwINFO, TD_DP_IN | TD_T_TOGGLE | TD_CC);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400591 intrdata = pipedata + maxpacket * (tail-tds);
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400592 SET_LOWFLAT(tail->hwCBP, (u32)intrdata);
593 SET_LOWFLAT(tail->hwNextTD, (u32)next);
594 SET_LOWFLAT(tail->hwBE, (u32)intrdata + maxpacket - 1);
Kevin O'Connor0770d672010-03-09 19:33:22 -0500595 barrier();
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400596 SET_LOWFLAT(pipe->ed.hwTailP, (u32)next);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400597
598 return 0;
Kevin O'Connor59f02832009-10-12 10:09:15 -0400599}