blob: 90f60e6452957552412d2aa9958f00141473437a [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
Kevin O'Connor4d8510c2016-02-03 01:28:20 -050012#include "pcidevice.h" // foreachpci
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
Kevin O'Connor32a2b0e2016-02-02 14:28:13 -050017#include "usb-ehci.h" // ehci_wait_controllers
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040018#include "usb-ohci.h" // struct ohci_hcca
19#include "util.h" // msleep
Kevin O'Connorb9c6a962013-09-14 13:01:30 -040020#include "x86.h" // readl
Kevin O'Connor59f02832009-10-12 10:09:15 -040021
Kevin O'Connor1c46a542009-10-17 23:53:32 -040022#define FIT (1 << 31)
23
Kevin O'Connor406fad62010-02-28 02:23:19 -050024struct usb_ohci_s {
25 struct usb_s usb;
26 struct ohci_regs *regs;
27};
28
Kevin O'Connor98af3532012-02-20 12:54:49 -050029struct ohci_pipe {
30 struct ohci_ed ed;
31 struct usb_pipe pipe;
Kevin O'Connor746d0a72014-12-29 13:06:23 -050032 struct ohci_regs *regs;
Kevin O'Connor98af3532012-02-20 12:54:49 -050033 void *data;
34 int count;
35 struct ohci_td *tds;
36};
37
Kevin O'Connor357bdfa2010-02-26 08:57:13 -050038
39/****************************************************************
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050040 * Root hub
41 ****************************************************************/
42
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040043// Check if device attached to port
44static int
45ohci_hub_detect(struct usbhub_s *hub, u32 port)
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050046{
Kevin O'Connor406fad62010-02-28 02:23:19 -050047 struct usb_ohci_s *cntl = container_of(hub->cntl, struct usb_ohci_s, usb);
Kevin O'Connor0f681302014-09-10 11:33:01 -040048 u32 sts = readl(&cntl->regs->roothub_portstatus[port]);
49 return (sts & RH_PS_CCS) ? 1 : 0;
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040050}
51
52// Disable port
53static void
54ohci_hub_disconnect(struct usbhub_s *hub, u32 port)
55{
56 struct usb_ohci_s *cntl = container_of(hub->cntl, struct usb_ohci_s, usb);
57 writel(&cntl->regs->roothub_portstatus[port], RH_PS_CCS|RH_PS_LSDA);
58}
59
60// Reset device on port
61static int
62ohci_hub_reset(struct usbhub_s *hub, u32 port)
63{
64 struct usb_ohci_s *cntl = container_of(hub->cntl, struct usb_ohci_s, usb);
Kevin O'Connor406fad62010-02-28 02:23:19 -050065 writel(&cntl->regs->roothub_portstatus[port], RH_PS_PRS);
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040066 u32 sts;
Kevin O'Connor018bdd72013-07-20 18:22:57 -040067 u32 end = timer_calc(USB_TIME_DRSTR * 2);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050068 for (;;) {
Kevin O'Connor406fad62010-02-28 02:23:19 -050069 sts = readl(&cntl->regs->roothub_portstatus[port]);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050070 if (!(sts & RH_PS_PRS))
71 // XXX - need to ensure USB_TIME_DRSTR time in reset?
72 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -040073 if (timer_check(end)) {
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050074 // Timeout.
75 warn_timeout();
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040076 ohci_hub_disconnect(hub, port);
77 return -1;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050078 }
79 yield();
80 }
81
82 if ((sts & (RH_PS_CCS|RH_PS_PES)) != (RH_PS_CCS|RH_PS_PES))
83 // Device no longer present
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040084 return -1;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050085
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040086 return !!(sts & RH_PS_LSDA);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050087}
88
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040089static struct usbhub_op_s ohci_HubOp = {
90 .detect = ohci_hub_detect,
91 .reset = ohci_hub_reset,
92 .disconnect = ohci_hub_disconnect,
93};
94
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050095// Find any devices connected to the root hub.
96static int
Kevin O'Connor406fad62010-02-28 02:23:19 -050097check_ohci_ports(struct usb_ohci_s *cntl)
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050098{
99 ASSERT32FLAT();
Kevin O'Connor32a2b0e2016-02-02 14:28:13 -0500100 // Wait for ehci init - in case this is a "companion controller"
101 ehci_wait_controllers();
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500102 // 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
Kevin O'Connor746d0a72014-12-29 13:06:23 -0500126ohci_waittick(struct ohci_regs *regs)
Kevin O'Connor98af3532012-02-20 12:54:49 -0500127{
128 barrier();
Kevin O'Connor746d0a72014-12-29 13:06:23 -0500129 struct ohci_hcca *hcca = (void*)regs->hcca;
Kevin O'Connor98af3532012-02-20 12:54:49 -0500130 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));
Kevin O'Connor746d0a72014-12-29 13:06:23 -0500151 ohci_waittick(cntl->regs);
Kevin O'Connor98af3532012-02-20 12:54:49 -0500152 }
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);
Kevin O'Connor2bfd1702014-10-16 12:31:42 -0400160 if (usb_is_freelist(&cntl->usb, &pipe->pipe)) {
Kevin O'Connor98af3532012-02-20 12:54:49 -0500161 *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'Connor746d0a72014-12-29 13:06:23 -0500217 , (OHCI_CTRL_CBSR | OHCI_CTRL_CLE | OHCI_CTRL_BLE | OHCI_CTRL_PLE
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400218 | 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'Connorec443ff2013-12-05 18:43:20 -0500268static void
269ohci_controller_setup(struct pci_device *pci)
Kevin O'Connor406fad62010-02-28 02:23:19 -0500270{
Kevin O'Connor466615d2016-02-02 22:29:14 -0500271 struct ohci_regs *regs = pci_enable_membar(pci, PCI_BASE_ADDRESS_0);
272 if (!regs)
273 return;
274
Kevin O'Connor406fad62010-02-28 02:23:19 -0500275 struct usb_ohci_s *cntl = malloc_tmphigh(sizeof(*cntl));
Kevin O'Connorffdcd3a2011-07-10 15:48:00 -0400276 if (!cntl) {
277 warn_noalloc();
278 return;
279 }
Kevin O'Connor406fad62010-02-28 02:23:19 -0500280 memset(cntl, 0, sizeof(*cntl));
Kevin O'Connor8ff8e012011-07-09 14:11:21 -0400281 cntl->usb.pci = pci;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500282 cntl->usb.type = USB_TYPE_OHCI;
Kevin O'Connor466615d2016-02-02 22:29:14 -0500283 cntl->regs = regs;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500284
Kevin O'Connor7b673002016-02-03 03:03:15 -0500285 dprintf(1, "OHCI init on dev %pP (regs=%p)\n", pci, cntl->regs);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500286
Kevin O'Connor466615d2016-02-02 22:29:14 -0500287 pci_enable_busmaster(pci);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500288
289 // XXX - check for and disable SMM control?
290
291 // Disable interrupts
292 writel(&cntl->regs->intrdisable, ~0);
293 writel(&cntl->regs->intrstatus, ~0);
294
295 run_thread(configure_ohci, cntl);
296}
297
Kevin O'Connorec443ff2013-12-05 18:43:20 -0500298void
299ohci_setup(void)
300{
301 if (! CONFIG_USB_OHCI)
302 return;
303 struct pci_device *pci;
304 foreachpci(pci) {
305 if (pci_classprog(pci) == PCI_CLASS_SERIAL_USB_OHCI)
306 ohci_controller_setup(pci);
307 }
308}
309
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500310
311/****************************************************************
312 * End point communication
313 ****************************************************************/
314
Kevin O'Connor0423dda2012-03-10 12:12:27 -0500315// Setup fields in ed
316static void
317ohci_desc2pipe(struct ohci_pipe *pipe, struct usbdevice_s *usbdev
318 , struct usb_endpoint_descriptor *epdesc)
319{
320 usb_desc2pipe(&pipe->pipe, usbdev, epdesc);
321 pipe->ed.hwINFO = (ED_SKIP | usbdev->devaddr | (pipe->pipe.ep << 7)
322 | (epdesc->wMaxPacketSize << 16)
323 | (usbdev->speed ? ED_LOWSPEED : 0));
Kevin O'Connor746d0a72014-12-29 13:06:23 -0500324 struct usb_ohci_s *cntl = container_of(
325 usbdev->hub->cntl, struct usb_ohci_s, usb);
326 pipe->regs = cntl->regs;
Kevin O'Connor0423dda2012-03-10 12:12:27 -0500327}
328
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500329static struct usb_pipe *
Kevin O'Connor6152c322012-03-10 08:53:58 -0500330ohci_alloc_intr_pipe(struct usbdevice_s *usbdev
331 , struct usb_endpoint_descriptor *epdesc)
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400332{
Kevin O'Connor6152c322012-03-10 08:53:58 -0500333 struct usb_ohci_s *cntl = container_of(
334 usbdev->hub->cntl, struct usb_ohci_s, usb);
Kevin O'Connor98cdad32014-10-16 12:05:09 -0400335 int frameexp = usb_get_period(usbdev, epdesc);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500336 dprintf(7, "ohci_alloc_intr_pipe %p %d\n", &cntl->usb, frameexp);
337
338 if (frameexp > 5)
339 frameexp = 5;
340 int maxpacket = epdesc->wMaxPacketSize;
341 // Determine number of entries needed for 2 timer ticks.
342 int ms = 1<<frameexp;
Kevin O'Connor69013372013-07-20 12:08:48 -0400343 int count = DIV_ROUND_UP(ticks_to_ms(2), ms) + 1;
Kevin O'Connor6152c322012-03-10 08:53:58 -0500344 struct ohci_pipe *pipe = malloc_low(sizeof(*pipe));
345 struct ohci_td *tds = malloc_low(sizeof(*tds) * count);
346 void *data = malloc_low(maxpacket * count);
347 if (!pipe || !tds || !data)
348 goto err;
349 memset(pipe, 0, sizeof(*pipe));
Kevin O'Connor0423dda2012-03-10 12:12:27 -0500350 ohci_desc2pipe(pipe, usbdev, epdesc);
351 pipe->ed.hwINFO &= ~ED_SKIP;
Kevin O'Connor6152c322012-03-10 08:53:58 -0500352 pipe->data = data;
353 pipe->count = count;
354 pipe->tds = tds;
355
356 struct ohci_ed *ed = &pipe->ed;
357 ed->hwHeadP = (u32)&tds[0];
358 ed->hwTailP = (u32)&tds[count-1];
Kevin O'Connor6152c322012-03-10 08:53:58 -0500359
360 int i;
361 for (i=0; i<count-1; i++) {
362 tds[i].hwINFO = TD_DP_IN | TD_T_TOGGLE | TD_CC;
363 tds[i].hwCBP = (u32)data + maxpacket * i;
364 tds[i].hwNextTD = (u32)&tds[i+1];
365 tds[i].hwBE = tds[i].hwCBP + maxpacket - 1;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400366 }
Kevin O'Connor6152c322012-03-10 08:53:58 -0500367
368 // Add to interrupt schedule.
Kevin O'Connor6152c322012-03-10 08:53:58 -0500369 struct ohci_hcca *hcca = (void*)cntl->regs->hcca;
370 if (frameexp == 0) {
371 // Add to existing interrupt entry.
372 struct ohci_ed *intr_ed = (void*)hcca->int_table[0];
373 ed->hwNextED = intr_ed->hwNextED;
Kevin O'Connorc37147d2012-03-10 12:30:27 -0500374 barrier();
Kevin O'Connor6152c322012-03-10 08:53:58 -0500375 intr_ed->hwNextED = (u32)ed;
376 } else {
377 int startpos = 1<<(frameexp-1);
378 ed->hwNextED = hcca->int_table[startpos];
Kevin O'Connorc37147d2012-03-10 12:30:27 -0500379 barrier();
Kevin O'Connor6152c322012-03-10 08:53:58 -0500380 for (i=startpos; i<ARRAY_SIZE(hcca->int_table); i+=ms)
381 hcca->int_table[i] = (u32)ed;
382 }
383
384 return &pipe->pipe;
385
386err:
387 free(pipe);
388 free(tds);
389 free(data);
390 return NULL;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400391}
392
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500393struct usb_pipe *
Kevin O'Connor2e0c2ea2014-10-16 13:36:41 -0400394ohci_realloc_pipe(struct usbdevice_s *usbdev, struct usb_pipe *upipe
395 , struct usb_endpoint_descriptor *epdesc)
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500396{
397 if (! CONFIG_USB_OHCI)
398 return NULL;
Kevin O'Connor2e0c2ea2014-10-16 13:36:41 -0400399 usb_add_freelist(upipe);
400 if (!epdesc)
401 return NULL;
Kevin O'Connor54bf2072012-03-09 07:52:33 -0500402 u8 eptype = epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500403 if (eptype == USB_ENDPOINT_XFER_INT)
404 return ohci_alloc_intr_pipe(usbdev, epdesc);
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'Connor746d0a72014-12-29 13:06:23 -0500418 struct ohci_pipe *pipe;
419 if (eptype == USB_ENDPOINT_XFER_CONTROL)
420 pipe = malloc_tmphigh(sizeof(*pipe));
421 else
422 pipe = malloc_low(sizeof(*pipe));
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500423 if (!pipe) {
424 warn_noalloc();
425 return NULL;
426 }
427 memset(pipe, 0, sizeof(*pipe));
Kevin O'Connor0423dda2012-03-10 12:12:27 -0500428 ohci_desc2pipe(pipe, usbdev, epdesc);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500429
430 // Add queue head to controller list.
Kevin O'Connor746d0a72014-12-29 13:06:23 -0500431 u32 *head = &cntl->regs->ed_controlhead;
432 if (eptype != USB_ENDPOINT_XFER_CONTROL)
433 head = &cntl->regs->ed_bulkhead;
434 pipe->ed.hwNextED = *head;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500435 barrier();
Kevin O'Connor746d0a72014-12-29 13:06:23 -0500436 *head = (u32)&pipe->ed;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500437 return &pipe->pipe;
438}
439
Kevin O'Connor6152c322012-03-10 08:53:58 -0500440static int
Kevin O'Connor083ee292014-06-14 12:50:38 -0400441wait_ed(struct ohci_ed *ed, int timeout)
Kevin O'Connor6152c322012-03-10 08:53:58 -0500442{
Kevin O'Connor083ee292014-06-14 12:50:38 -0400443 u32 end = timer_calc(timeout);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500444 for (;;) {
Kevin O'Connor746d0a72014-12-29 13:06:23 -0500445 if ((ed->hwHeadP & ~(ED_C|ED_H)) == ed->hwTailP)
Kevin O'Connor6152c322012-03-10 08:53:58 -0500446 return 0;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400447 if (timer_check(end)) {
Kevin O'Connor6152c322012-03-10 08:53:58 -0500448 warn_timeout();
Kevin O'Connor746d0a72014-12-29 13:06:23 -0500449 dprintf(1, "ohci ed info=%x tail=%x head=%x next=%x\n"
450 , ed->hwINFO, ed->hwTailP, ed->hwHeadP, ed->hwNextED);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500451 return -1;
452 }
453 yield();
454 }
455}
456
Kevin O'Connor5372a3f2014-12-31 01:55:48 -0500457#define STACKOTDS 18
Kevin O'Connor746d0a72014-12-29 13:06:23 -0500458#define OHCI_TD_ALIGN 16
459
Kevin O'Connor2f968002010-09-19 22:10:08 -0400460int
Kevin O'Connor2891a832014-12-31 17:41:14 -0500461ohci_send_pipe(struct usb_pipe *p, int dir, const void *cmd
Kevin O'Connor5372a3f2014-12-31 01:55:48 -0500462 , void *data, int datasize)
Kevin O'Connor2f968002010-09-19 22:10:08 -0400463{
Kevin O'Connor746d0a72014-12-29 13:06:23 -0500464 ASSERT32FLAT();
465 if (! CONFIG_USB_OHCI)
466 return -1;
Kevin O'Connor5372a3f2014-12-31 01:55:48 -0500467 dprintf(7, "ohci_send_pipe %p\n", p);
Kevin O'Connor746d0a72014-12-29 13:06:23 -0500468 struct ohci_pipe *pipe = container_of(p, struct ohci_pipe, pipe);
469
Kevin O'Connor5372a3f2014-12-31 01:55:48 -0500470 // Allocate tds on stack (with required alignment)
Kevin O'Connor746d0a72014-12-29 13:06:23 -0500471 u8 tdsbuf[sizeof(struct ohci_td) * STACKOTDS + OHCI_TD_ALIGN - 1];
472 struct ohci_td *tds = (void*)ALIGN((u32)tdsbuf, OHCI_TD_ALIGN), *td = tds;
473 memset(tds, 0, sizeof(*tds) * STACKOTDS);
474
475 // Setup transfer descriptors
476 u16 maxpacket = pipe->pipe.maxpacket;
Kevin O'Connor5372a3f2014-12-31 01:55:48 -0500477 u32 toggle = 0, statuscmd = OHCI_BLF;
478 if (cmd) {
479 // Send setup pid on control transfers
480 td->hwINFO = TD_DP_SETUP | TD_T_DATA0 | TD_CC;
481 td->hwCBP = (u32)cmd;
482 td->hwNextTD = (u32)&td[1];
Kevin O'Connor2891a832014-12-31 17:41:14 -0500483 td->hwBE = (u32)cmd + USB_CONTROL_SETUP_SIZE - 1;
Kevin O'Connor5372a3f2014-12-31 01:55:48 -0500484 td++;
485 toggle = TD_T_DATA1;
486 statuscmd = OHCI_CLF;
487 }
Kevin O'Connor746d0a72014-12-29 13:06:23 -0500488 u32 dest = (u32)data, dataend = dest + datasize;
489 while (dest < dataend) {
490 // Send data pids
491 if (td >= &tds[STACKOTDS]) {
492 warn_noalloc();
493 return -1;
494 }
495 int maxtransfer = 2*PAGE_SIZE - (dest & (PAGE_SIZE-1));
496 int transfer = dataend - dest;
497 if (transfer > maxtransfer)
498 transfer = ALIGN_DOWN(maxtransfer, maxpacket);
Kevin O'Connor5372a3f2014-12-31 01:55:48 -0500499 td->hwINFO = (dir ? TD_DP_IN : TD_DP_OUT) | toggle | TD_CC;
Kevin O'Connor746d0a72014-12-29 13:06:23 -0500500 td->hwCBP = dest;
501 td->hwNextTD = (u32)&td[1];
502 td->hwBE = dest + transfer - 1;
503 td++;
504 dest += transfer;
505 }
Kevin O'Connor5372a3f2014-12-31 01:55:48 -0500506 if (cmd) {
507 // Send status pid on control transfers
508 if (td >= &tds[STACKOTDS]) {
509 warn_noalloc();
510 return -1;
511 }
512 td->hwINFO = (dir ? TD_DP_OUT : TD_DP_IN) | TD_T_DATA1 | TD_CC;
513 td->hwCBP = 0;
514 td->hwNextTD = (u32)&td[1];
515 td->hwBE = 0;
516 td++;
517 }
Kevin O'Connor746d0a72014-12-29 13:06:23 -0500518
519 // Transfer data
520 pipe->ed.hwHeadP = (u32)tds | (pipe->ed.hwHeadP & ED_C);
521 pipe->ed.hwTailP = (u32)td;
522 barrier();
523 pipe->ed.hwINFO &= ~ED_SKIP;
Kevin O'Connor5372a3f2014-12-31 01:55:48 -0500524 writel(&pipe->regs->cmdstatus, statuscmd);
Kevin O'Connor746d0a72014-12-29 13:06:23 -0500525
526 int ret = wait_ed(&pipe->ed, usb_xfer_time(p, datasize));
527 pipe->ed.hwINFO |= ED_SKIP;
528 if (ret)
529 ohci_waittick(pipe->regs);
530 return ret;
Kevin O'Connor2f968002010-09-19 22:10:08 -0400531}
532
Kevin O'Connor59f02832009-10-12 10:09:15 -0400533int
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500534ohci_poll_intr(struct usb_pipe *p, void *data)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400535{
536 ASSERT16();
537 if (! CONFIG_USB_OHCI)
538 return -1;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400539
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500540 struct ohci_pipe *pipe = container_of(p, struct ohci_pipe, pipe);
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400541 struct ohci_td *tds = GET_LOWFLAT(pipe->tds);
542 struct ohci_td *head = (void*)(GET_LOWFLAT(pipe->ed.hwHeadP) & ~(ED_C|ED_H));
543 struct ohci_td *tail = (void*)GET_LOWFLAT(pipe->ed.hwTailP);
544 int count = GET_LOWFLAT(pipe->count);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400545 int pos = (tail - tds + 1) % count;
546 struct ohci_td *next = &tds[pos];
547 if (head == next)
548 // No intrs found.
549 return -1;
550 // XXX - check for errors.
551
552 // Copy data.
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400553 int maxpacket = GET_LOWFLAT(pipe->pipe.maxpacket);
554 void *pipedata = GET_LOWFLAT((pipe->data));
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400555 void *intrdata = pipedata + maxpacket * pos;
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400556 memcpy_far(GET_SEG(SS), data, SEG_LOW, LOWFLAT2LOW(intrdata), maxpacket);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400557
558 // Reenable this td.
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400559 SET_LOWFLAT(tail->hwINFO, TD_DP_IN | TD_T_TOGGLE | TD_CC);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400560 intrdata = pipedata + maxpacket * (tail-tds);
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400561 SET_LOWFLAT(tail->hwCBP, (u32)intrdata);
562 SET_LOWFLAT(tail->hwNextTD, (u32)next);
563 SET_LOWFLAT(tail->hwBE, (u32)intrdata + maxpacket - 1);
Kevin O'Connor0770d672010-03-09 19:33:22 -0500564 barrier();
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400565 SET_LOWFLAT(pipe->ed.hwTailP, (u32)next);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400566
567 return 0;
Kevin O'Connor59f02832009-10-12 10:09:15 -0400568}