blob: 9107db2bcd2af68e76fc7ceb9656d7dd4d1c73c6 [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
7#include "util.h" // dprintf
8#include "pci.h" // pci_bdf_to_bus
9#include "config.h" // CONFIG_*
Kevin O'Connor1c46a542009-10-17 23:53:32 -040010#include "usb-ohci.h" // struct ohci_hcca
11#include "pci_regs.h" // PCI_BASE_ADDRESS_0
Kevin O'Connor59f02832009-10-12 10:09:15 -040012#include "usb.h" // struct usb_s
13#include "farptr.h" // GET_FLATPTR
Kevin O'Connor59f02832009-10-12 10:09:15 -040014
Kevin O'Connor1c46a542009-10-17 23:53:32 -040015#define FIT (1 << 31)
16
Kevin O'Connor406fad62010-02-28 02:23:19 -050017struct usb_ohci_s {
18 struct usb_s usb;
19 struct ohci_regs *regs;
20};
21
Kevin O'Connor357bdfa2010-02-26 08:57:13 -050022
23/****************************************************************
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050024 * Root hub
25 ****************************************************************/
26
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040027// Check if device attached to port
28static int
29ohci_hub_detect(struct usbhub_s *hub, u32 port)
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050030{
Kevin O'Connor406fad62010-02-28 02:23:19 -050031 struct usb_ohci_s *cntl = container_of(hub->cntl, struct usb_ohci_s, usb);
Kevin O'Connor406fad62010-02-28 02:23:19 -050032 u32 sts = readl(&cntl->regs->roothub_portstatus[port]);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050033 if (!(sts & RH_PS_CCS))
34 // No device.
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040035 return -1;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050036
37 // XXX - need to wait for USB_TIME_ATTDB if just powered up?
38
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040039 return 0;
40}
41
42// Disable port
43static void
44ohci_hub_disconnect(struct usbhub_s *hub, u32 port)
45{
46 struct usb_ohci_s *cntl = container_of(hub->cntl, struct usb_ohci_s, usb);
47 writel(&cntl->regs->roothub_portstatus[port], RH_PS_CCS|RH_PS_LSDA);
48}
49
50// Reset device on port
51static int
52ohci_hub_reset(struct usbhub_s *hub, u32 port)
53{
54 struct usb_ohci_s *cntl = container_of(hub->cntl, struct usb_ohci_s, usb);
Kevin O'Connor406fad62010-02-28 02:23:19 -050055 writel(&cntl->regs->roothub_portstatus[port], RH_PS_PRS);
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040056 u32 sts;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050057 u64 end = calc_future_tsc(USB_TIME_DRSTR * 2);
58 for (;;) {
Kevin O'Connor406fad62010-02-28 02:23:19 -050059 sts = readl(&cntl->regs->roothub_portstatus[port]);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050060 if (!(sts & RH_PS_PRS))
61 // XXX - need to ensure USB_TIME_DRSTR time in reset?
62 break;
Kevin O'Connor144817b2010-05-23 10:46:49 -040063 if (check_tsc(end)) {
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050064 // Timeout.
65 warn_timeout();
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040066 ohci_hub_disconnect(hub, port);
67 return -1;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050068 }
69 yield();
70 }
71
72 if ((sts & (RH_PS_CCS|RH_PS_PES)) != (RH_PS_CCS|RH_PS_PES))
73 // Device no longer present
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040074 return -1;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050075
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040076 return !!(sts & RH_PS_LSDA);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050077}
78
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040079static struct usbhub_op_s ohci_HubOp = {
80 .detect = ohci_hub_detect,
81 .reset = ohci_hub_reset,
82 .disconnect = ohci_hub_disconnect,
83};
84
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050085// Find any devices connected to the root hub.
86static int
Kevin O'Connor406fad62010-02-28 02:23:19 -050087check_ohci_ports(struct usb_ohci_s *cntl)
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050088{
89 ASSERT32FLAT();
90 // Turn on power for all devices on roothub.
Kevin O'Connor406fad62010-02-28 02:23:19 -050091 u32 rha = readl(&cntl->regs->roothub_a);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050092 rha &= ~(RH_A_PSM | RH_A_OCPM);
Kevin O'Connor406fad62010-02-28 02:23:19 -050093 writel(&cntl->regs->roothub_status, RH_HS_LPSC);
94 writel(&cntl->regs->roothub_b, RH_B_PPCM);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050095 msleep((rha >> 24) * 2);
96 // XXX - need to sleep for USB_TIME_SIGATT if just powered up?
97
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050098 struct usbhub_s hub;
99 memset(&hub, 0, sizeof(hub));
Kevin O'Connor406fad62010-02-28 02:23:19 -0500100 hub.cntl = &cntl->usb;
Kevin O'Connord28b0fe2010-03-28 15:11:19 -0400101 hub.portcount = rha & RH_A_NDP;
102 hub.op = &ohci_HubOp;
103 usb_enumerate(&hub);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500104 return hub.devcount;
105}
106
107
108/****************************************************************
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500109 * Setup
110 ****************************************************************/
111
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400112static int
Kevin O'Connor406fad62010-02-28 02:23:19 -0500113start_ohci(struct usb_ohci_s *cntl, struct ohci_hcca *hcca)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400114{
Kevin O'Connor406fad62010-02-28 02:23:19 -0500115 u32 oldfminterval = readl(&cntl->regs->fminterval);
116 u32 oldrwc = readl(&cntl->regs->control) & OHCI_CTRL_RWC;
Kevin O'Connor59f02832009-10-12 10:09:15 -0400117
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400118 // XXX - check if already running?
Kevin O'Connor59f02832009-10-12 10:09:15 -0400119
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400120 // Do reset
Kevin O'Connor406fad62010-02-28 02:23:19 -0500121 writel(&cntl->regs->control, OHCI_USB_RESET | oldrwc);
122 readl(&cntl->regs->control); // flush writes
Kevin O'Connor8bbc79c2010-02-14 12:16:32 -0500123 msleep(USB_TIME_DRSTR);
Kevin O'Connor59f02832009-10-12 10:09:15 -0400124
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400125 // Do software init (min 10us, max 2ms)
126 u64 end = calc_future_tsc_usec(10);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500127 writel(&cntl->regs->cmdstatus, OHCI_HCR);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400128 for (;;) {
Kevin O'Connor406fad62010-02-28 02:23:19 -0500129 u32 status = readl(&cntl->regs->cmdstatus);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400130 if (! status & OHCI_HCR)
131 break;
Kevin O'Connor144817b2010-05-23 10:46:49 -0400132 if (check_tsc(end)) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500133 warn_timeout();
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400134 return -1;
135 }
Kevin O'Connor59f02832009-10-12 10:09:15 -0400136 }
137
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400138 // Init memory
Kevin O'Connor406fad62010-02-28 02:23:19 -0500139 writel(&cntl->regs->ed_controlhead, 0);
140 writel(&cntl->regs->ed_bulkhead, 0);
141 writel(&cntl->regs->hcca, (u32)hcca);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400142
143 // Init fminterval
144 u32 fi = oldfminterval & 0x3fff;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500145 writel(&cntl->regs->fminterval
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400146 , (((oldfminterval & FIT) ^ FIT)
147 | fi | (((6 * (fi - 210)) / 7) << 16)));
Kevin O'Connor406fad62010-02-28 02:23:19 -0500148 writel(&cntl->regs->periodicstart, ((9 * fi) / 10) & 0x3fff);
149 readl(&cntl->regs->control); // flush writes
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400150
151 // XXX - verify that fminterval was setup correctly.
152
153 // Go into operational state
Kevin O'Connor406fad62010-02-28 02:23:19 -0500154 writel(&cntl->regs->control
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400155 , (OHCI_CTRL_CBSR | OHCI_CTRL_CLE | OHCI_CTRL_PLE
156 | OHCI_USB_OPER | oldrwc));
Kevin O'Connor406fad62010-02-28 02:23:19 -0500157 readl(&cntl->regs->control); // flush writes
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400158
159 return 0;
Kevin O'Connor59f02832009-10-12 10:09:15 -0400160}
161
162static void
Kevin O'Connor406fad62010-02-28 02:23:19 -0500163stop_ohci(struct usb_ohci_s *cntl)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400164{
Kevin O'Connor406fad62010-02-28 02:23:19 -0500165 u32 oldrwc = readl(&cntl->regs->control) & OHCI_CTRL_RWC;
166 writel(&cntl->regs->control, oldrwc);
167 readl(&cntl->regs->control); // flush writes
Kevin O'Connor59f02832009-10-12 10:09:15 -0400168}
169
Kevin O'Connor406fad62010-02-28 02:23:19 -0500170static void
171configure_ohci(void *data)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400172{
Kevin O'Connor406fad62010-02-28 02:23:19 -0500173 struct usb_ohci_s *cntl = data;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400174
175 // Allocate memory
176 struct ohci_hcca *hcca = memalign_high(256, sizeof(*hcca));
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500177 struct ohci_ed *intr_ed = malloc_high(sizeof(*intr_ed));
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500178 if (!hcca || !intr_ed) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500179 warn_noalloc();
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500180 goto free;
Kevin O'Connor59f02832009-10-12 10:09:15 -0400181 }
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400182 memset(hcca, 0, sizeof(*hcca));
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500183 memset(intr_ed, 0, sizeof(*intr_ed));
184 intr_ed->hwINFO = ED_SKIP;
185 int i;
186 for (i=0; i<ARRAY_SIZE(hcca->int_table); i++)
187 hcca->int_table[i] = (u32)intr_ed;
Kevin O'Connor59f02832009-10-12 10:09:15 -0400188
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400189 int ret = start_ohci(cntl, hcca);
190 if (ret)
191 goto err;
192
193 int count = check_ohci_ports(cntl);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500194 free_pipe(cntl->usb.defaultpipe);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400195 if (! count)
196 goto err;
Kevin O'Connora5826b52009-10-24 17:57:29 -0400197 return;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400198
199err:
200 stop_ohci(cntl);
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500201free:
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400202 free(hcca);
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500203 free(intr_ed);
Kevin O'Connor59f02832009-10-12 10:09:15 -0400204}
205
Kevin O'Connor406fad62010-02-28 02:23:19 -0500206void
Kevin O'Connor8ff8e012011-07-09 14:11:21 -0400207ohci_init(struct pci_device *pci, int busid)
Kevin O'Connor406fad62010-02-28 02:23:19 -0500208{
209 if (! CONFIG_USB_OHCI)
210 return;
211 struct usb_ohci_s *cntl = malloc_tmphigh(sizeof(*cntl));
Kevin O'Connorffdcd3a2011-07-10 15:48:00 -0400212 if (!cntl) {
213 warn_noalloc();
214 return;
215 }
Kevin O'Connor406fad62010-02-28 02:23:19 -0500216 memset(cntl, 0, sizeof(*cntl));
217 cntl->usb.busid = busid;
Kevin O'Connor8ff8e012011-07-09 14:11:21 -0400218 cntl->usb.pci = pci;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500219 cntl->usb.type = USB_TYPE_OHCI;
220
Kevin O'Connor8ff8e012011-07-09 14:11:21 -0400221 u16 bdf = pci->bdf;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500222 u32 baseaddr = pci_config_readl(bdf, PCI_BASE_ADDRESS_0);
223 cntl->regs = (void*)(baseaddr & PCI_BASE_ADDRESS_MEM_MASK);
224
Kevin O'Connor9dc243e2010-03-20 17:53:03 -0400225 dprintf(1, "OHCI init on dev %02x:%02x.%x (regs=%p)\n"
Kevin O'Connor406fad62010-02-28 02:23:19 -0500226 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf)
227 , pci_bdf_to_fn(bdf), cntl->regs);
228
229 // Enable bus mastering and memory access.
230 pci_config_maskw(bdf, PCI_COMMAND
231 , 0, PCI_COMMAND_MASTER|PCI_COMMAND_MEMORY);
232
233 // XXX - check for and disable SMM control?
234
235 // Disable interrupts
236 writel(&cntl->regs->intrdisable, ~0);
237 writel(&cntl->regs->intrstatus, ~0);
238
239 run_thread(configure_ohci, cntl);
240}
241
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500242
243/****************************************************************
244 * End point communication
245 ****************************************************************/
246
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400247static int
248wait_ed(struct ohci_ed *ed)
249{
250 // XXX - 500ms just a guess
251 u64 end = calc_future_tsc(500);
252 for (;;) {
253 if (ed->hwHeadP == ed->hwTailP)
254 return 0;
Kevin O'Connor144817b2010-05-23 10:46:49 -0400255 if (check_tsc(end)) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500256 warn_timeout();
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400257 return -1;
258 }
Kevin O'Connor229e3be2009-10-31 13:55:59 -0400259 yield();
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400260 }
261}
262
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500263// Wait for next USB frame to start - for ensuring safe memory release.
264static void
Kevin O'Connor406fad62010-02-28 02:23:19 -0500265ohci_waittick(struct usb_ohci_s *cntl)
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500266{
267 barrier();
Kevin O'Connor406fad62010-02-28 02:23:19 -0500268 struct ohci_hcca *hcca = (void*)cntl->regs->hcca;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500269 u32 startframe = hcca->frame_no;
270 u64 end = calc_future_tsc(1000 * 5);
271 for (;;) {
272 if (hcca->frame_no != startframe)
273 break;
Kevin O'Connor144817b2010-05-23 10:46:49 -0400274 if (check_tsc(end)) {
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500275 warn_timeout();
276 return;
277 }
278 yield();
279 }
280}
281
282static void
Kevin O'Connor406fad62010-02-28 02:23:19 -0500283signal_freelist(struct usb_ohci_s *cntl)
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500284{
Kevin O'Connor406fad62010-02-28 02:23:19 -0500285 u32 v = readl(&cntl->regs->control);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500286 if (v & OHCI_CTRL_CLE) {
Kevin O'Connor406fad62010-02-28 02:23:19 -0500287 writel(&cntl->regs->control, v & ~(OHCI_CTRL_CLE|OHCI_CTRL_BLE));
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500288 ohci_waittick(cntl);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500289 writel(&cntl->regs->ed_controlcurrent, 0);
290 writel(&cntl->regs->ed_bulkcurrent, 0);
291 writel(&cntl->regs->control, v);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500292 } else {
293 ohci_waittick(cntl);
294 }
295}
296
297struct ohci_pipe {
298 struct ohci_ed ed;
299 struct usb_pipe pipe;
300 void *data;
301 int count;
302 struct ohci_td *tds;
303};
304
305void
306ohci_free_pipe(struct usb_pipe *p)
307{
308 if (! CONFIG_USB_OHCI)
309 return;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500310 dprintf(7, "ohci_free_pipe %p\n", p);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500311 struct ohci_pipe *pipe = container_of(p, struct ohci_pipe, pipe);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500312 struct usb_ohci_s *cntl = container_of(
313 pipe->pipe.cntl, struct usb_ohci_s, usb);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500314
Kevin O'Connor406fad62010-02-28 02:23:19 -0500315 u32 *pos = &cntl->regs->ed_controlhead;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500316 for (;;) {
317 struct ohci_ed *next = (void*)*pos;
318 if (!next) {
319 // Not found?! Exit without freeing.
320 warn_internalerror();
321 return;
322 }
323 if (next == &pipe->ed) {
324 *pos = next->hwNextED;
325 signal_freelist(cntl);
326 free(pipe);
327 return;
328 }
329 pos = &next->hwNextED;
330 }
331}
332
333struct usb_pipe *
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500334ohci_alloc_control_pipe(struct usb_pipe *dummy)
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500335{
336 if (! CONFIG_USB_OHCI)
337 return NULL;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500338 struct usb_ohci_s *cntl = container_of(
339 dummy->cntl, struct usb_ohci_s, usb);
340 dprintf(7, "ohci_alloc_control_pipe %p\n", &cntl->usb);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500341
342 // Allocate a queue head.
343 struct ohci_pipe *pipe = malloc_tmphigh(sizeof(*pipe));
344 if (!pipe) {
345 warn_noalloc();
346 return NULL;
347 }
348 memset(pipe, 0, sizeof(*pipe));
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500349 memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
Kevin O'Connor0770d672010-03-09 19:33:22 -0500350 pipe->ed.hwINFO = ED_SKIP;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500351
352 // Add queue head to controller list.
Kevin O'Connor406fad62010-02-28 02:23:19 -0500353 pipe->ed.hwNextED = cntl->regs->ed_controlhead;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500354 barrier();
Kevin O'Connor406fad62010-02-28 02:23:19 -0500355 cntl->regs->ed_controlhead = (u32)&pipe->ed;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500356 return &pipe->pipe;
357}
358
Kevin O'Connor59f02832009-10-12 10:09:15 -0400359int
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500360ohci_control(struct usb_pipe *p, int dir, const void *cmd, int cmdsize
Kevin O'Connor59f02832009-10-12 10:09:15 -0400361 , void *data, int datasize)
362{
363 if (! CONFIG_USB_OHCI)
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400364 return -1;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500365 dprintf(5, "ohci_control %p\n", p);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500366 if (datasize > 4096) {
367 // XXX - should support larger sizes.
368 warn_noalloc();
369 return -1;
370 }
371 struct ohci_pipe *pipe = container_of(p, struct ohci_pipe, pipe);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500372 struct usb_ohci_s *cntl = container_of(
373 pipe->pipe.cntl, struct usb_ohci_s, usb);
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500374 int maxpacket = pipe->pipe.maxpacket;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500375 int lowspeed = pipe->pipe.speed;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500376 int devaddr = pipe->pipe.devaddr | (pipe->pipe.ep << 7);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400377
378 // Setup transfer descriptors
379 struct ohci_td *tds = malloc_tmphigh(sizeof(*tds) * 3);
Kevin O'Connor0770d672010-03-09 19:33:22 -0500380 if (!tds) {
381 warn_noalloc();
382 return -1;
383 }
384 struct ohci_td *td = tds;
385 td->hwINFO = TD_DP_SETUP | TD_T_DATA0 | TD_CC;
386 td->hwCBP = (u32)cmd;
387 td->hwNextTD = (u32)&td[1];
388 td->hwBE = (u32)cmd + cmdsize - 1;
389 td++;
390 if (datasize) {
391 td->hwINFO = (dir ? TD_DP_IN : TD_DP_OUT) | TD_T_DATA1 | TD_CC;
392 td->hwCBP = (u32)data;
393 td->hwNextTD = (u32)&td[1];
394 td->hwBE = (u32)data + datasize - 1;
395 td++;
396 }
397 td->hwINFO = (dir ? TD_DP_OUT : TD_DP_IN) | TD_T_DATA1 | TD_CC;
398 td->hwCBP = 0;
399 td->hwNextTD = (u32)&td[1];
400 td->hwBE = 0;
401 td++;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400402
403 // Transfer data
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500404 pipe->ed.hwINFO = ED_SKIP;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400405 barrier();
Kevin O'Connor0770d672010-03-09 19:33:22 -0500406 pipe->ed.hwHeadP = (u32)tds;
407 pipe->ed.hwTailP = (u32)td;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400408 barrier();
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500409 pipe->ed.hwINFO = devaddr | (maxpacket << 16) | (lowspeed ? ED_LOWSPEED : 0);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500410 writel(&cntl->regs->cmdstatus, OHCI_CLF);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400411
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500412 int ret = wait_ed(&pipe->ed);
413 pipe->ed.hwINFO = ED_SKIP;
Kevin O'Connorad901592009-12-13 11:25:25 -0500414 if (ret)
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500415 ohci_waittick(cntl);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400416 free(tds);
417 return ret;
Kevin O'Connor59f02832009-10-12 10:09:15 -0400418}
419
420struct usb_pipe *
Kevin O'Connor2f968002010-09-19 22:10:08 -0400421ohci_alloc_bulk_pipe(struct usb_pipe *dummy)
422{
423 if (! CONFIG_USB_OHCI)
424 return NULL;
425 dprintf(1, "OHCI Bulk transfers not supported.\n");
426 return NULL;
427}
428
429int
430ohci_send_bulk(struct usb_pipe *p, int dir, void *data, int datasize)
431{
432 return -1;
433}
434
435struct usb_pipe *
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500436ohci_alloc_intr_pipe(struct usb_pipe *dummy, int frameexp)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400437{
438 if (! CONFIG_USB_OHCI)
439 return NULL;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500440 struct usb_ohci_s *cntl = container_of(
441 dummy->cntl, struct usb_ohci_s, usb);
442 dprintf(7, "ohci_alloc_intr_pipe %p %d\n", &cntl->usb, frameexp);
Kevin O'Connor59f02832009-10-12 10:09:15 -0400443
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500444 if (frameexp > 5)
445 frameexp = 5;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500446 int maxpacket = dummy->maxpacket;
Kevin O'Connor190cc622010-03-09 19:43:52 -0500447 int lowspeed = dummy->speed;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500448 int devaddr = dummy->devaddr | (dummy->ep << 7);
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500449 // Determine number of entries needed for 2 timer ticks.
450 int ms = 1<<frameexp;
Kevin O'Connorbfe7ca72010-02-28 02:36:32 -0500451 int count = DIV_ROUND_UP(PIT_TICK_INTERVAL * 1000 * 2, PIT_TICK_RATE * ms)+1;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400452 struct ohci_pipe *pipe = malloc_low(sizeof(*pipe));
453 struct ohci_td *tds = malloc_low(sizeof(*tds) * count);
454 void *data = malloc_low(maxpacket * count);
455 if (!pipe || !tds || !data)
456 goto err;
Kevin O'Connor0770d672010-03-09 19:33:22 -0500457 memset(pipe, 0, sizeof(*pipe));
458 memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
459 pipe->data = data;
460 pipe->count = count;
461 pipe->tds = tds;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400462
463 struct ohci_ed *ed = &pipe->ed;
464 ed->hwHeadP = (u32)&tds[0];
465 ed->hwTailP = (u32)&tds[count-1];
466 ed->hwINFO = devaddr | (maxpacket << 16) | (lowspeed ? ED_LOWSPEED : 0);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400467
468 int i;
469 for (i=0; i<count-1; i++) {
470 tds[i].hwINFO = TD_DP_IN | TD_T_TOGGLE | TD_CC;
471 tds[i].hwCBP = (u32)data + maxpacket * i;
472 tds[i].hwNextTD = (u32)&tds[i+1];
473 tds[i].hwBE = tds[i].hwCBP + maxpacket - 1;
474 }
475
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500476 // Add to interrupt schedule.
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400477 barrier();
Kevin O'Connor406fad62010-02-28 02:23:19 -0500478 struct ohci_hcca *hcca = (void*)cntl->regs->hcca;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500479 if (frameexp == 0) {
480 // Add to existing interrupt entry.
481 struct ohci_ed *intr_ed = (void*)hcca->int_table[0];
482 ed->hwNextED = intr_ed->hwNextED;
483 intr_ed->hwNextED = (u32)ed;
484 } else {
485 int startpos = 1<<(frameexp-1);
486 ed->hwNextED = hcca->int_table[startpos];
487 for (i=startpos; i<ARRAY_SIZE(hcca->int_table); i+=ms)
488 hcca->int_table[i] = (u32)ed;
489 }
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400490
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400491 return &pipe->pipe;
492
493err:
494 free(pipe);
495 free(tds);
496 free(data);
Kevin O'Connor59f02832009-10-12 10:09:15 -0400497 return NULL;
498}
499
500int
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500501ohci_poll_intr(struct usb_pipe *p, void *data)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400502{
503 ASSERT16();
504 if (! CONFIG_USB_OHCI)
505 return -1;
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400506
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500507 struct ohci_pipe *pipe = container_of(p, struct ohci_pipe, pipe);
508 struct ohci_td *tds = GET_FLATPTR(pipe->tds);
Kevin O'Connor3cabf782011-05-29 09:48:28 -0400509 struct ohci_td *head = (void*)(GET_FLATPTR(pipe->ed.hwHeadP) & ~(ED_C|ED_H));
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500510 struct ohci_td *tail = (void*)GET_FLATPTR(pipe->ed.hwTailP);
511 int count = GET_FLATPTR(pipe->count);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400512 int pos = (tail - tds + 1) % count;
513 struct ohci_td *next = &tds[pos];
514 if (head == next)
515 // No intrs found.
516 return -1;
517 // XXX - check for errors.
518
519 // Copy data.
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500520 int maxpacket = GET_FLATPTR(pipe->pipe.maxpacket);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500521 void *pipedata = GET_FLATPTR(pipe->data);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400522 void *intrdata = pipedata + maxpacket * pos;
523 memcpy_far(GET_SEG(SS), data
524 , FLATPTR_TO_SEG(intrdata), (void*)FLATPTR_TO_OFFSET(intrdata)
525 , maxpacket);
526
527 // Reenable this td.
528 SET_FLATPTR(tail->hwINFO, TD_DP_IN | TD_T_TOGGLE | TD_CC);
529 intrdata = pipedata + maxpacket * (tail-tds);
530 SET_FLATPTR(tail->hwCBP, (u32)intrdata);
531 SET_FLATPTR(tail->hwNextTD, (u32)next);
532 SET_FLATPTR(tail->hwBE, (u32)intrdata + maxpacket - 1);
Kevin O'Connor0770d672010-03-09 19:33:22 -0500533 barrier();
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500534 SET_FLATPTR(pipe->ed.hwTailP, (u32)next);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400535
536 return 0;
Kevin O'Connor59f02832009-10-12 10:09:15 -0400537}