2 * URB OHCI HCD (Host Controller Driver) for USB on the AT91RM9200 and PCI bus.
4 * Interrupt support is added. Now, it has been tested
5 * on ULI1575 chip and works well with USB keyboard.
8 * Zhang Wei, Freescale Semiconductor, Inc. <wei.zhang@freescale.com>
11 * Gary Jennejohn, DENX Software Engineering <garyj@denx.de>
13 * Note: Much of this code has been derived from Linux 2.4
14 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
15 * (C) Copyright 2000-2002 David Brownell
17 * Modified for the MP2USB by (C) Copyright 2005 Eric Benard
18 * ebenard@eukrea.com - based on s3c24x0's driver
20 * SPDX-License-Identifier: GPL-2.0+
24 * 1 - Read doc/README.generic_usb_ohci
25 * 2 - this driver is intended for use with USB Mass Storage Devices
26 * (BBB) and USB keyboard. There is NO support for Isochronous pipes!
27 * 2 - when running on a PQFP208 AT91RM9200, define CONFIG_AT91C_PQFP_UHPBUG
28 * to activate workaround for bug #41 or this driver will NOT work!
32 #include <asm/byteorder.h>
36 #if defined(CONFIG_PCI_OHCI)
38 #if !defined(CONFIG_PCI_OHCI_DEVNO)
39 #define CONFIG_PCI_OHCI_DEVNO 0
49 #ifdef CONFIG_AT91RM9200
50 #include <asm/arch/hardware.h> /* needed for AT91_USB_HOST_BASE */
53 #if defined(CONFIG_CPU_ARM920T) || \
54 defined(CONFIG_440EP) || \
55 defined(CONFIG_PCI_OHCI) || \
56 defined(CONFIG_MPC5200) || \
57 defined(CONFIG_SYS_OHCI_USE_NPS)
58 # define OHCI_USE_NPS /* force NoPowerSwitching mode */
61 #undef OHCI_VERBOSE_DEBUG /* not always helpful */
64 #undef OHCI_FILL_TRACE
66 /* For initializing controller (mask in an HCFS mode too) */
67 #define OHCI_CONTROL_INIT \
68 (OHCI_CTRL_CBSR & 0x3) | OHCI_CTRL_IE | OHCI_CTRL_PLE
70 #ifdef CONFIG_PCI_OHCI
71 static struct pci_device_id ohci_pci_ids[] = {
72 {0x10b9, 0x5237}, /* ULI1575 PCI OHCI module ids */
73 {0x1033, 0x0035}, /* NEC PCI OHCI module ids */
74 {0x1131, 0x1561}, /* Philips 1561 PCI OHCI module ids */
75 /* Please add supported PCI OHCI controller ids here */
80 #ifdef CONFIG_PCI_EHCI_DEVNO
81 static struct pci_device_id ehci_pci_ids[] = {
82 {0x1131, 0x1562}, /* Philips 1562 PCI EHCI module ids */
83 /* Please add supported PCI EHCI controller ids here */
89 #define dbg(format, arg...) printf("DEBUG: " format "\n", ## arg)
91 #define dbg(format, arg...) do {} while (0)
93 #define err(format, arg...) printf("ERROR: " format "\n", ## arg)
95 #define info(format, arg...) printf("INFO: " format "\n", ## arg)
97 #define info(format, arg...) do {} while (0)
100 #ifdef CONFIG_SYS_OHCI_BE_CONTROLLER
101 # define m16_swap(x) cpu_to_be16(x)
102 # define m32_swap(x) cpu_to_be32(x)
104 # define m16_swap(x) cpu_to_le16(x)
105 # define m32_swap(x) cpu_to_le32(x)
106 #endif /* CONFIG_SYS_OHCI_BE_CONTROLLER */
108 /* We really should do proper cache flushing everywhere */
109 #define flush_dcache_buffer(addr, size) \
110 flush_dcache_range((unsigned long)(addr), \
111 ALIGN((unsigned long)(addr) + size, ARCH_DMA_MINALIGN))
112 #define invalidate_dcache_buffer(addr, size) \
113 invalidate_dcache_range((unsigned long)(addr), \
114 ALIGN((unsigned long)(addr) + size, ARCH_DMA_MINALIGN))
116 /* Do not use sizeof(ed / td) as our ed / td structs contain extra members */
117 #define flush_dcache_ed(addr) flush_dcache_buffer(addr, 16)
118 #define flush_dcache_td(addr) flush_dcache_buffer(addr, 16)
119 #define flush_dcache_iso_td(addr) flush_dcache_buffer(addr, 32)
120 #define flush_dcache_hcca(addr) flush_dcache_buffer(addr, 256)
121 #define invalidate_dcache_ed(addr) invalidate_dcache_buffer(addr, 16)
122 #define invalidate_dcache_td(addr) invalidate_dcache_buffer(addr, 16)
123 #define invalidate_dcache_iso_td(addr) invalidate_dcache_buffer(addr, 32)
124 #define invalidate_dcache_hcca(addr) invalidate_dcache_buffer(addr, 256)
128 * The various ohci_mdelay(1) calls in the code seem unnecessary. We keep
129 * them around when building for older boards not yet converted to the dm
130 * just in case (to avoid regressions), for dm this turns them into nops.
132 #define ohci_mdelay(x)
134 #define ohci_mdelay(x) mdelay(x)
137 #ifndef CONFIG_DM_USB
140 /* this must be aligned to a 256 byte boundary */
141 struct ohci_hcca ghcca[1];
144 /* mapping of the OHCI CC status to error codes */
145 static int cc_to_error[16] = {
147 /* CRC Error */ USB_ST_CRC_ERR,
148 /* Bit Stuff */ USB_ST_BIT_ERR,
149 /* Data Togg */ USB_ST_CRC_ERR,
150 /* Stall */ USB_ST_STALLED,
152 /* PIDCheck */ USB_ST_BIT_ERR,
153 /* UnExpPID */ USB_ST_BIT_ERR,
154 /* DataOver */ USB_ST_BUF_ERR,
155 /* DataUnder */ USB_ST_BUF_ERR,
158 /* BufferOver */ USB_ST_BUF_ERR,
159 /* BuffUnder */ USB_ST_BUF_ERR,
164 static const char *cc_to_string[16] = {
166 "CRC: Last data packet from endpoint contained a CRC error.",
167 "BITSTUFFING: Last data packet from endpoint contained a bit " \
168 "stuffing violation",
169 "DATATOGGLEMISMATCH: Last packet from endpoint had data toggle PID\n" \
170 "that did not match the expected value.",
171 "STALL: TD was moved to the Done Queue because the endpoint returned" \
173 "DEVICENOTRESPONDING: Device did not respond to token (IN) or did\n" \
174 "not provide a handshake (OUT)",
175 "PIDCHECKFAILURE: Check bits on PID from endpoint failed on data PID\n"\
176 "(IN) or handshake (OUT)",
177 "UNEXPECTEDPID: Receive PID was not valid when encountered or PID\n" \
178 "value is not defined.",
179 "DATAOVERRUN: The amount of data returned by the endpoint exceeded\n" \
180 "either the size of the maximum data packet allowed\n" \
181 "from the endpoint (found in MaximumPacketSize field\n" \
182 "of ED) or the remaining buffer size.",
183 "DATAUNDERRUN: The endpoint returned less than MaximumPacketSize\n" \
184 "and that amount was not sufficient to fill the\n" \
188 "BUFFEROVERRUN: During an IN, HC received data from endpoint faster\n" \
189 "than it could be written to system memory",
190 "BUFFERUNDERRUN: During an OUT, HC could not retrieve data from\n" \
191 "system memory fast enough to keep up with data USB " \
193 "NOT ACCESSED: This code is set by software before the TD is placed" \
194 "on a list to be processed by the HC.(1)",
195 "NOT ACCESSED: This code is set by software before the TD is placed" \
196 "on a list to be processed by the HC.(2)",
199 static inline u32 roothub_a(struct ohci *hc)
200 { return ohci_readl(&hc->regs->roothub.a); }
201 static inline u32 roothub_b(struct ohci *hc)
202 { return ohci_readl(&hc->regs->roothub.b); }
203 static inline u32 roothub_status(struct ohci *hc)
204 { return ohci_readl(&hc->regs->roothub.status); }
205 static inline u32 roothub_portstatus(struct ohci *hc, int i)
206 { return ohci_readl(&hc->regs->roothub.portstatus[i]); }
208 /* forward declaration */
209 static int hc_interrupt(ohci_t *ohci);
210 static void td_submit_job(ohci_t *ohci, struct usb_device *dev,
211 unsigned long pipe, void *buffer, int transfer_len,
212 struct devrequest *setup, urb_priv_t *urb,
214 static int ep_link(ohci_t * ohci, ed_t * ed);
215 static int ep_unlink(ohci_t * ohci, ed_t * ed);
216 static ed_t *ep_add_ed(ohci_dev_t *ohci_dev, struct usb_device *usb_dev,
217 unsigned long pipe, int interval, int load);
219 /*-------------------------------------------------------------------------*/
222 static struct td *td_alloc(ohci_dev_t *ohci_dev, struct usb_device *usb_dev)
228 for (i = 0; i < NUM_TD; i++)
230 if (ohci_dev->tds[i].usb_dev == NULL)
232 td = &ohci_dev->tds[i];
233 td->usb_dev = usb_dev;
241 static inline void ed_free(struct ed *ed)
246 /*-------------------------------------------------------------------------*
247 * URB support functions
248 *-------------------------------------------------------------------------*/
250 /* free HCD-private data associated with this URB */
252 static void urb_free_priv(urb_priv_t *urb)
258 last = urb->length - 1;
260 for (i = 0; i <= last; i++) {
271 /*-------------------------------------------------------------------------*/
274 static int sohci_get_current_frame_number(ohci_t *ohci);
276 /* debug| print the main components of an URB
277 * small: 0) header + data packets 1) just header */
279 static void pkt_print(ohci_t *ohci, urb_priv_t *purb, struct usb_device *dev,
280 unsigned long pipe, void *buffer, int transfer_len,
281 struct devrequest *setup, char *str, int small)
283 dbg("%s URB:[%4x] dev:%2lu,ep:%2lu-%c,type:%s,len:%d/%d stat:%#lx",
285 sohci_get_current_frame_number(ohci),
286 usb_pipedevice(pipe),
287 usb_pipeendpoint(pipe),
288 usb_pipeout(pipe)? 'O': 'I',
289 usb_pipetype(pipe) < 2 ? \
290 (usb_pipeint(pipe)? "INTR": "ISOC"): \
291 (usb_pipecontrol(pipe)? "CTRL": "BULK"),
292 (purb ? purb->actual_length : 0),
293 transfer_len, dev->status);
294 #ifdef OHCI_VERBOSE_DEBUG
298 if (usb_pipecontrol(pipe)) {
299 printf(__FILE__ ": cmd(8):");
300 for (i = 0; i < 8 ; i++)
301 printf(" %02x", ((__u8 *) setup) [i]);
304 if (transfer_len > 0 && buffer) {
305 printf(__FILE__ ": data(%d/%d):",
306 (purb ? purb->actual_length : 0),
308 len = usb_pipeout(pipe)? transfer_len:
309 (purb ? purb->actual_length : 0);
310 for (i = 0; i < 16 && i < len; i++)
311 printf(" %02x", ((__u8 *) buffer) [i]);
312 printf("%s\n", i < len? "...": "");
318 /* just for debugging; prints non-empty branches of the int ed tree
319 * inclusive iso eds */
320 void ep_print_int_eds(ohci_t *ohci, char *str)
324 for (i = 0; i < 32; i++) {
326 ed_p = &(ohci->hcca->int_table [i]);
329 invalidate_dcache_ed(ed_p);
330 printf(__FILE__ ": %s branch int %2d(%2x):", str, i, i);
331 while (*ed_p != 0 && j--) {
332 ed_t *ed = (ed_t *)m32_swap(ed_p);
333 invalidate_dcache_ed(ed);
334 printf(" ed: %4x;", ed->hwINFO);
335 ed_p = &ed->hwNextED;
341 static void ohci_dump_intr_mask(char *label, __u32 mask)
343 dbg("%s: 0x%08x%s%s%s%s%s%s%s%s%s",
346 (mask & OHCI_INTR_MIE) ? " MIE" : "",
347 (mask & OHCI_INTR_OC) ? " OC" : "",
348 (mask & OHCI_INTR_RHSC) ? " RHSC" : "",
349 (mask & OHCI_INTR_FNO) ? " FNO" : "",
350 (mask & OHCI_INTR_UE) ? " UE" : "",
351 (mask & OHCI_INTR_RD) ? " RD" : "",
352 (mask & OHCI_INTR_SF) ? " SF" : "",
353 (mask & OHCI_INTR_WDH) ? " WDH" : "",
354 (mask & OHCI_INTR_SO) ? " SO" : ""
358 static void maybe_print_eds(char *label, __u32 value)
360 ed_t *edp = (ed_t *)value;
363 dbg("%s %08x", label, value);
364 invalidate_dcache_ed(edp);
365 dbg("%08x", edp->hwINFO);
366 dbg("%08x", edp->hwTailP);
367 dbg("%08x", edp->hwHeadP);
368 dbg("%08x", edp->hwNextED);
372 static char *hcfs2string(int state)
375 case OHCI_USB_RESET: return "reset";
376 case OHCI_USB_RESUME: return "resume";
377 case OHCI_USB_OPER: return "operational";
378 case OHCI_USB_SUSPEND: return "suspend";
383 /* dump control and status registers */
384 static void ohci_dump_status(ohci_t *controller)
386 struct ohci_regs *regs = controller->regs;
389 temp = ohci_readl(®s->revision) & 0xff;
391 dbg("spec %d.%d", (temp >> 4), (temp & 0x0f));
393 temp = ohci_readl(®s->control);
394 dbg("control: 0x%08x%s%s%s HCFS=%s%s%s%s%s CBSR=%d", temp,
395 (temp & OHCI_CTRL_RWE) ? " RWE" : "",
396 (temp & OHCI_CTRL_RWC) ? " RWC" : "",
397 (temp & OHCI_CTRL_IR) ? " IR" : "",
398 hcfs2string(temp & OHCI_CTRL_HCFS),
399 (temp & OHCI_CTRL_BLE) ? " BLE" : "",
400 (temp & OHCI_CTRL_CLE) ? " CLE" : "",
401 (temp & OHCI_CTRL_IE) ? " IE" : "",
402 (temp & OHCI_CTRL_PLE) ? " PLE" : "",
403 temp & OHCI_CTRL_CBSR
406 temp = ohci_readl(®s->cmdstatus);
407 dbg("cmdstatus: 0x%08x SOC=%d%s%s%s%s", temp,
408 (temp & OHCI_SOC) >> 16,
409 (temp & OHCI_OCR) ? " OCR" : "",
410 (temp & OHCI_BLF) ? " BLF" : "",
411 (temp & OHCI_CLF) ? " CLF" : "",
412 (temp & OHCI_HCR) ? " HCR" : ""
415 ohci_dump_intr_mask("intrstatus", ohci_readl(®s->intrstatus));
416 ohci_dump_intr_mask("intrenable", ohci_readl(®s->intrenable));
418 maybe_print_eds("ed_periodcurrent",
419 ohci_readl(®s->ed_periodcurrent));
421 maybe_print_eds("ed_controlhead", ohci_readl(®s->ed_controlhead));
422 maybe_print_eds("ed_controlcurrent",
423 ohci_readl(®s->ed_controlcurrent));
425 maybe_print_eds("ed_bulkhead", ohci_readl(®s->ed_bulkhead));
426 maybe_print_eds("ed_bulkcurrent", ohci_readl(®s->ed_bulkcurrent));
428 maybe_print_eds("donehead", ohci_readl(®s->donehead));
431 static void ohci_dump_roothub(ohci_t *controller, int verbose)
435 temp = roothub_a(controller);
436 ndp = (temp & RH_A_NDP);
437 #ifdef CONFIG_AT91C_PQFP_UHPBUG
438 ndp = (ndp == 2) ? 1:0;
441 dbg("roothub.a: %08x POTPGT=%d%s%s%s%s%s NDP=%d", temp,
442 ((temp & RH_A_POTPGT) >> 24) & 0xff,
443 (temp & RH_A_NOCP) ? " NOCP" : "",
444 (temp & RH_A_OCPM) ? " OCPM" : "",
445 (temp & RH_A_DT) ? " DT" : "",
446 (temp & RH_A_NPS) ? " NPS" : "",
447 (temp & RH_A_PSM) ? " PSM" : "",
450 temp = roothub_b(controller);
451 dbg("roothub.b: %08x PPCM=%04x DR=%04x",
453 (temp & RH_B_PPCM) >> 16,
456 temp = roothub_status(controller);
457 dbg("roothub.status: %08x%s%s%s%s%s%s",
459 (temp & RH_HS_CRWE) ? " CRWE" : "",
460 (temp & RH_HS_OCIC) ? " OCIC" : "",
461 (temp & RH_HS_LPSC) ? " LPSC" : "",
462 (temp & RH_HS_DRWE) ? " DRWE" : "",
463 (temp & RH_HS_OCI) ? " OCI" : "",
464 (temp & RH_HS_LPS) ? " LPS" : ""
468 for (i = 0; i < ndp; i++) {
469 temp = roothub_portstatus(controller, i);
470 dbg("roothub.portstatus [%d] = 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s",
473 (temp & RH_PS_PRSC) ? " PRSC" : "",
474 (temp & RH_PS_OCIC) ? " OCIC" : "",
475 (temp & RH_PS_PSSC) ? " PSSC" : "",
476 (temp & RH_PS_PESC) ? " PESC" : "",
477 (temp & RH_PS_CSC) ? " CSC" : "",
479 (temp & RH_PS_LSDA) ? " LSDA" : "",
480 (temp & RH_PS_PPS) ? " PPS" : "",
481 (temp & RH_PS_PRS) ? " PRS" : "",
482 (temp & RH_PS_POCI) ? " POCI" : "",
483 (temp & RH_PS_PSS) ? " PSS" : "",
485 (temp & RH_PS_PES) ? " PES" : "",
486 (temp & RH_PS_CCS) ? " CCS" : ""
491 static void ohci_dump(ohci_t *controller, int verbose)
493 dbg("OHCI controller usb-%s state", controller->slot_name);
495 /* dumps some of the state we know about */
496 ohci_dump_status(controller);
498 ep_print_int_eds(controller, "hcca");
499 invalidate_dcache_hcca(controller->hcca);
500 dbg("hcca frame #%04x", controller->hcca->frame_no);
501 ohci_dump_roothub(controller, 1);
505 /*-------------------------------------------------------------------------*
506 * Interface functions (URB)
507 *-------------------------------------------------------------------------*/
509 /* get a transfer request */
511 int sohci_submit_job(ohci_t *ohci, ohci_dev_t *ohci_dev, urb_priv_t *urb,
512 struct devrequest *setup)
515 urb_priv_t *purb_priv = urb;
517 struct usb_device *dev = urb->dev;
518 unsigned long pipe = urb->pipe;
519 void *buffer = urb->transfer_buffer;
520 int transfer_len = urb->transfer_buffer_length;
521 int interval = urb->interval;
523 /* when controller's hung, permit only roothub cleanup attempts
524 * such as powering down ports */
525 if (ohci->disabled) {
526 err("sohci_submit_job: EPIPE");
530 /* we're about to begin a new transaction here so mark the
534 /* every endpoint has a ed, locate and fill it */
535 ed = ep_add_ed(ohci_dev, dev, pipe, interval, 1);
537 err("sohci_submit_job: ENOMEM");
541 /* for the private part of the URB we need the number of TDs (size) */
542 switch (usb_pipetype(pipe)) {
543 case PIPE_BULK: /* one TD for every 4096 Byte */
544 size = (transfer_len - 1) / 4096 + 1;
546 case PIPE_CONTROL:/* 1 TD for setup, 1 for ACK and 1 for every 4096 B */
547 size = (transfer_len == 0)? 2:
548 (transfer_len - 1) / 4096 + 3;
550 case PIPE_INTERRUPT: /* 1 TD */
557 if (size >= (N_URB_TD - 1)) {
558 err("need %d TDs, only have %d", size, N_URB_TD);
561 purb_priv->pipe = pipe;
563 /* fill the private part of the URB */
564 purb_priv->length = size;
566 purb_priv->actual_length = 0;
568 /* allocate the TDs */
569 /* note that td[0] was allocated in ep_add_ed */
570 for (i = 0; i < size; i++) {
571 purb_priv->td[i] = td_alloc(ohci_dev, dev);
572 if (!purb_priv->td[i]) {
573 purb_priv->length = i;
574 urb_free_priv(purb_priv);
575 err("sohci_submit_job: ENOMEM");
580 if (ed->state == ED_NEW || (ed->state & ED_DEL)) {
581 urb_free_priv(purb_priv);
582 err("sohci_submit_job: EINVAL");
586 /* link the ed into a chain if is not already */
587 if (ed->state != ED_OPER)
590 /* fill the TDs and link it to the ed */
591 td_submit_job(ohci, dev, pipe, buffer, transfer_len,
592 setup, purb_priv, interval);
597 /*-------------------------------------------------------------------------*/
600 /* tell us the current USB frame number */
601 static int sohci_get_current_frame_number(ohci_t *ohci)
603 invalidate_dcache_hcca(ohci->hcca);
604 return m16_swap(ohci->hcca->frame_no);
608 /*-------------------------------------------------------------------------*
609 * ED handling functions
610 *-------------------------------------------------------------------------*/
612 /* search for the right branch to insert an interrupt ed into the int tree
613 * do some load ballancing;
614 * returns the branch and
615 * sets the interval to interval = 2^integer (ld (interval)) */
617 static int ep_int_ballance(ohci_t *ohci, int interval, int load)
621 /* search for the least loaded interrupt endpoint
622 * branch of all 32 branches
624 for (i = 0; i < 32; i++)
625 if (ohci->ohci_int_load [branch] > ohci->ohci_int_load [i])
628 branch = branch % interval;
629 for (i = branch; i < 32; i += interval)
630 ohci->ohci_int_load [i] += load;
635 /*-------------------------------------------------------------------------*/
637 /* 2^int( ld (inter)) */
639 static int ep_2_n_interval(int inter)
642 for (i = 0; ((inter >> i) > 1) && (i < 5); i++);
646 /*-------------------------------------------------------------------------*/
648 /* the int tree is a binary tree
649 * in order to process it sequentially the indexes of the branches have to
650 * be mapped the mapping reverses the bits of a word of num_bits length */
651 static int ep_rev(int num_bits, int word)
655 for (i = 0; i < num_bits; i++)
656 wout |= (((word >> i) & 1) << (num_bits - i - 1));
660 /*-------------------------------------------------------------------------*
661 * ED handling functions
662 *-------------------------------------------------------------------------*/
664 /* link an ed into one of the HC chains */
666 static int ep_link(ohci_t *ohci, ed_t *edi)
668 volatile ed_t *ed = edi;
677 ed->int_interval = 0;
683 if (ohci->ed_controltail == NULL)
684 ohci_writel((uintptr_t)ed, &ohci->regs->ed_controlhead);
686 ohci->ed_controltail->hwNextED =
687 m32_swap((unsigned long)ed);
689 ed->ed_prev = ohci->ed_controltail;
690 if (!ohci->ed_controltail && !ohci->ed_rm_list[0] &&
691 !ohci->ed_rm_list[1] && !ohci->sleeping) {
692 ohci->hc_control |= OHCI_CTRL_CLE;
693 ohci_writel(ohci->hc_control, &ohci->regs->control);
695 ohci->ed_controltail = edi;
701 if (ohci->ed_bulktail == NULL)
702 ohci_writel((uintptr_t)ed, &ohci->regs->ed_bulkhead);
704 ohci->ed_bulktail->hwNextED =
705 m32_swap((unsigned long)ed);
707 ed->ed_prev = ohci->ed_bulktail;
708 if (!ohci->ed_bulktail && !ohci->ed_rm_list[0] &&
709 !ohci->ed_rm_list[1] && !ohci->sleeping) {
710 ohci->hc_control |= OHCI_CTRL_BLE;
711 ohci_writel(ohci->hc_control, &ohci->regs->control);
713 ohci->ed_bulktail = edi;
718 interval = ep_2_n_interval(ed->int_period);
719 ed->int_interval = interval;
720 int_branch = ep_int_ballance(ohci, interval, load);
721 ed->int_branch = int_branch;
723 for (i = 0; i < ep_rev(6, interval); i += inter) {
725 for (ed_p = &(ohci->hcca->int_table[\
726 ep_rev(5, i) + int_branch]);
728 (((ed_t *)ed_p)->int_interval >= interval);
729 ed_p = &(((ed_t *)ed_p)->hwNextED))
731 ((ed_t *)ed_p)->int_interval);
732 ed->hwNextED = *ed_p;
734 *ed_p = m32_swap((unsigned long)ed);
735 flush_dcache_hcca(ohci->hcca);
742 /*-------------------------------------------------------------------------*/
744 /* scan the periodic table to find and unlink this ED */
745 static void periodic_unlink(struct ohci *ohci, volatile struct ed *ed,
746 unsigned index, unsigned period)
748 __maybe_unused unsigned long aligned_ed_p;
750 for (; index < NUM_INTS; index += period) {
751 __u32 *ed_p = &ohci->hcca->int_table [index];
753 /* ED might have been unlinked through another path */
755 if (((struct ed *)(uintptr_t)
756 m32_swap((unsigned long)ed_p)) == ed) {
757 *ed_p = ed->hwNextED;
758 aligned_ed_p = (unsigned long)ed_p;
759 aligned_ed_p &= ~(ARCH_DMA_MINALIGN - 1);
760 flush_dcache_range(aligned_ed_p,
761 aligned_ed_p + ARCH_DMA_MINALIGN);
764 ed_p = &(((struct ed *)(uintptr_t)
765 m32_swap((unsigned long)ed_p))->hwNextED);
770 /* unlink an ed from one of the HC chains.
771 * just the link to the ed is unlinked.
772 * the link from the ed still points to another operational ed or 0
773 * so the HC can eventually finish the processing of the unlinked ed */
775 static int ep_unlink(ohci_t *ohci, ed_t *edi)
777 volatile ed_t *ed = edi;
780 ed->hwINFO |= m32_swap(OHCI_ED_SKIP);
785 if (ed->ed_prev == NULL) {
787 ohci->hc_control &= ~OHCI_CTRL_CLE;
788 ohci_writel(ohci->hc_control,
789 &ohci->regs->control);
791 ohci_writel(m32_swap(*((__u32 *)&ed->hwNextED)),
792 &ohci->regs->ed_controlhead);
794 ed->ed_prev->hwNextED = ed->hwNextED;
795 flush_dcache_ed(ed->ed_prev);
797 if (ohci->ed_controltail == ed) {
798 ohci->ed_controltail = ed->ed_prev;
800 ((ed_t *)(uintptr_t)m32_swap(
801 *((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
806 if (ed->ed_prev == NULL) {
808 ohci->hc_control &= ~OHCI_CTRL_BLE;
809 ohci_writel(ohci->hc_control,
810 &ohci->regs->control);
812 ohci_writel(m32_swap(*((__u32 *)&ed->hwNextED)),
813 &ohci->regs->ed_bulkhead);
815 ed->ed_prev->hwNextED = ed->hwNextED;
816 flush_dcache_ed(ed->ed_prev);
818 if (ohci->ed_bulktail == ed) {
819 ohci->ed_bulktail = ed->ed_prev;
821 ((ed_t *)(uintptr_t)m32_swap(
822 *((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
827 periodic_unlink(ohci, ed, 0, 1);
828 for (i = ed->int_branch; i < 32; i += ed->int_interval)
829 ohci->ohci_int_load[i] -= ed->int_load;
832 ed->state = ED_UNLINK;
836 /*-------------------------------------------------------------------------*/
838 /* add/reinit an endpoint; this should be done once at the
839 * usb_set_configuration command, but the USB stack is a little bit
840 * stateless so we do it at every transaction if the state of the ed
841 * is ED_NEW then a dummy td is added and the state is changed to
842 * ED_UNLINK in all other cases the state is left unchanged the ed
843 * info fields are setted anyway even though most of them should not
846 static ed_t *ep_add_ed(ohci_dev_t *ohci_dev, struct usb_device *usb_dev,
847 unsigned long pipe, int interval, int load)
853 ed = ed_ret = &ohci_dev->ed[(usb_pipeendpoint(pipe) << 1) |
854 (usb_pipecontrol(pipe)? 0: usb_pipeout(pipe))];
856 if ((ed->state & ED_DEL) || (ed->state & ED_URB_DEL)) {
857 err("ep_add_ed: pending delete");
858 /* pending delete request */
862 if (ed->state == ED_NEW) {
863 /* dummy td; end of td list for ed */
864 td = td_alloc(ohci_dev, usb_dev);
865 ed->hwTailP = m32_swap((unsigned long)td);
866 ed->hwHeadP = ed->hwTailP;
867 ed->state = ED_UNLINK;
868 ed->type = usb_pipetype(pipe);
872 ed->hwINFO = m32_swap(usb_pipedevice(pipe)
873 | usb_pipeendpoint(pipe) << 7
874 | (usb_pipeisoc(pipe)? 0x8000: 0)
875 | (usb_pipecontrol(pipe)? 0: \
876 (usb_pipeout(pipe)? 0x800: 0x1000))
877 | (usb_dev->speed == USB_SPEED_LOW) << 13
878 | usb_maxpacket(usb_dev, pipe) << 16);
880 if (ed->type == PIPE_INTERRUPT && ed->state == ED_UNLINK) {
881 ed->int_period = interval;
890 /*-------------------------------------------------------------------------*
891 * TD handling functions
892 *-------------------------------------------------------------------------*/
894 /* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
896 static void td_fill(ohci_t *ohci, unsigned int info,
898 struct usb_device *dev, int index, urb_priv_t *urb_priv)
900 volatile td_t *td, *td_pt;
901 #ifdef OHCI_FILL_TRACE
905 if (index > urb_priv->length) {
906 err("index > length");
909 /* use this td as the next dummy */
910 td_pt = urb_priv->td [index];
912 flush_dcache_td(td_pt);
914 /* fill the old dummy TD */
915 td = urb_priv->td [index] =
917 (m32_swap(urb_priv->ed->hwTailP) & ~0xf);
919 td->ed = urb_priv->ed;
920 td->next_dl_td = NULL;
922 td->data = (uintptr_t)data;
923 #ifdef OHCI_FILL_TRACE
924 if (usb_pipebulk(urb_priv->pipe) && usb_pipeout(urb_priv->pipe)) {
925 for (i = 0; i < len; i++)
926 printf("td->data[%d] %#2x ", i, ((unsigned char *)td->data)[i]);
933 td->hwINFO = m32_swap(info);
934 td->hwCBP = m32_swap((unsigned long)data);
936 td->hwBE = m32_swap((unsigned long)(data + len - 1));
940 td->hwNextTD = m32_swap((unsigned long)td_pt);
943 /* append to queue */
944 td->ed->hwTailP = td->hwNextTD;
945 flush_dcache_ed(td->ed);
948 /*-------------------------------------------------------------------------*/
950 /* prepare all TDs of a transfer */
952 static void td_submit_job(ohci_t *ohci, struct usb_device *dev,
953 unsigned long pipe, void *buffer, int transfer_len,
954 struct devrequest *setup, urb_priv_t *urb,
957 int data_len = transfer_len;
961 unsigned int toggle = 0;
963 flush_dcache_buffer(buffer, data_len);
965 /* OHCI handles the DATA-toggles itself, we just use the USB-toggle
966 * bits for resetting */
967 if (usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe))) {
968 toggle = TD_T_TOGGLE;
971 usb_settoggle(dev, usb_pipeendpoint(pipe),
972 usb_pipeout(pipe), 1);
980 switch (usb_pipetype(pipe)) {
982 info = usb_pipeout(pipe)?
983 TD_CC | TD_DP_OUT : TD_CC | TD_DP_IN ;
984 while (data_len > 4096) {
985 td_fill(ohci, info | (cnt? TD_T_TOGGLE:toggle),
986 data, 4096, dev, cnt, urb);
987 data += 4096; data_len -= 4096; cnt++;
989 info = usb_pipeout(pipe)?
990 TD_CC | TD_DP_OUT : TD_CC | TD_R | TD_DP_IN ;
991 td_fill(ohci, info | (cnt? TD_T_TOGGLE:toggle), data,
992 data_len, dev, cnt, urb);
995 if (!ohci->sleeping) {
996 /* start bulk list */
997 ohci_writel(OHCI_BLF, &ohci->regs->cmdstatus);
1003 info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
1004 flush_dcache_buffer(setup, 8);
1005 td_fill(ohci, info, setup, 8, dev, cnt++, urb);
1007 /* Optional Data phase */
1009 info = usb_pipeout(pipe)?
1010 TD_CC | TD_R | TD_DP_OUT | TD_T_DATA1 :
1011 TD_CC | TD_R | TD_DP_IN | TD_T_DATA1;
1012 /* NOTE: mishandles transfers >8K, some >4K */
1013 td_fill(ohci, info, data, data_len, dev, cnt++, urb);
1017 info = (usb_pipeout(pipe) || data_len == 0) ?
1018 TD_CC | TD_DP_IN | TD_T_DATA1:
1019 TD_CC | TD_DP_OUT | TD_T_DATA1;
1020 td_fill(ohci, info, data, 0, dev, cnt++, urb);
1022 if (!ohci->sleeping) {
1023 /* start Control list */
1024 ohci_writel(OHCI_CLF, &ohci->regs->cmdstatus);
1028 case PIPE_INTERRUPT:
1029 info = usb_pipeout(urb->pipe)?
1030 TD_CC | TD_DP_OUT | toggle:
1031 TD_CC | TD_R | TD_DP_IN | toggle;
1032 td_fill(ohci, info, data, data_len, dev, cnt++, urb);
1035 if (urb->length != cnt)
1036 dbg("TD LENGTH %d != CNT %d", urb->length, cnt);
1039 /*-------------------------------------------------------------------------*
1040 * Done List handling functions
1041 *-------------------------------------------------------------------------*/
1043 /* calculate the transfer length and update the urb */
1045 static void dl_transfer_length(td_t *td)
1048 urb_priv_t *lurb_priv = td->ed->purb;
1050 tdBE = m32_swap(td->hwBE);
1051 tdCBP = m32_swap(td->hwCBP);
1053 if (!(usb_pipecontrol(lurb_priv->pipe) &&
1054 ((td->index == 0) || (td->index == lurb_priv->length - 1)))) {
1057 lurb_priv->actual_length += tdBE - td->data + 1;
1059 lurb_priv->actual_length += tdCBP - td->data;
1064 /*-------------------------------------------------------------------------*/
1065 static void check_status(td_t *td_list)
1067 urb_priv_t *lurb_priv = td_list->ed->purb;
1068 int urb_len = lurb_priv->length;
1069 __u32 *phwHeadP = &td_list->ed->hwHeadP;
1072 cc = TD_CC_GET(m32_swap(td_list->hwINFO));
1074 err(" USB-error: %s (%x)", cc_to_string[cc], cc);
1076 invalidate_dcache_ed(td_list->ed);
1077 if (*phwHeadP & m32_swap(0x1)) {
1079 ((td_list->index + 1) < urb_len)) {
1081 (lurb_priv->td[urb_len - 1]->hwNextTD &\
1082 m32_swap(0xfffffff0)) |
1083 (*phwHeadP & m32_swap(0x2));
1085 lurb_priv->td_cnt += urb_len -
1088 *phwHeadP &= m32_swap(0xfffffff2);
1089 flush_dcache_ed(td_list->ed);
1091 #ifdef CONFIG_MPC5200
1092 td_list->hwNextTD = 0;
1093 flush_dcache_td(td_list);
1098 /* replies to the request have to be on a FIFO basis so
1099 * we reverse the reversed done-list */
1100 static td_t *dl_reverse_done_list(ohci_t *ohci)
1102 uintptr_t td_list_hc;
1103 td_t *td_rev = NULL;
1104 td_t *td_list = NULL;
1106 invalidate_dcache_hcca(ohci->hcca);
1107 td_list_hc = m32_swap(ohci->hcca->done_head) & 0xfffffff0;
1108 ohci->hcca->done_head = 0;
1109 flush_dcache_hcca(ohci->hcca);
1111 while (td_list_hc) {
1112 td_list = (td_t *)td_list_hc;
1113 invalidate_dcache_td(td_list);
1114 check_status(td_list);
1115 td_list->next_dl_td = td_rev;
1117 td_list_hc = m32_swap(td_list->hwNextTD) & 0xfffffff0;
1122 /*-------------------------------------------------------------------------*/
1123 /*-------------------------------------------------------------------------*/
1125 static void finish_urb(ohci_t *ohci, urb_priv_t *urb, int status)
1127 if ((status & (ED_OPER | ED_UNLINK)) && (urb->state != URB_DEL))
1130 dbg("finish_urb: strange.., ED state %x, \n", status);
1134 * Used to take back a TD from the host controller. This would normally be
1135 * called from within dl_done_list, however it may be called directly if the
1136 * HC no longer sees the TD and it has not appeared on the donelist (after
1137 * two frames). This bug has been observed on ZF Micro systems.
1139 static int takeback_td(ohci_t *ohci, td_t *td_list)
1145 urb_priv_t *lurb_priv;
1146 __u32 tdINFO, edHeadP, edTailP;
1148 invalidate_dcache_td(td_list);
1149 tdINFO = m32_swap(td_list->hwINFO);
1152 lurb_priv = ed->purb;
1154 dl_transfer_length(td_list);
1156 lurb_priv->td_cnt++;
1158 /* error code of transfer */
1159 cc = TD_CC_GET(tdINFO);
1161 err("USB-error: %s (%x)", cc_to_string[cc], cc);
1162 stat = cc_to_error[cc];
1165 /* see if this done list makes for all TD's of current URB,
1166 * and mark the URB finished if so */
1167 if (lurb_priv->td_cnt == lurb_priv->length)
1168 finish_urb(ohci, lurb_priv, ed->state);
1170 dbg("dl_done_list: processing TD %x, len %x\n",
1171 lurb_priv->td_cnt, lurb_priv->length);
1173 if (ed->state != ED_NEW && (!usb_pipeint(lurb_priv->pipe))) {
1174 invalidate_dcache_ed(ed);
1175 edHeadP = m32_swap(ed->hwHeadP) & 0xfffffff0;
1176 edTailP = m32_swap(ed->hwTailP);
1178 /* unlink eds if they are not busy */
1179 if ((edHeadP == edTailP) && (ed->state == ED_OPER))
1180 ep_unlink(ohci, ed);
1185 static int dl_done_list(ohci_t *ohci)
1188 td_t *td_list = dl_reverse_done_list(ohci);
1191 td_t *td_next = td_list->next_dl_td;
1192 stat = takeback_td(ohci, td_list);
1198 /*-------------------------------------------------------------------------*
1200 *-------------------------------------------------------------------------*/
1202 #include <usbroothubdes.h>
1204 /* Hub class-specific descriptor is constructed dynamically */
1206 /*-------------------------------------------------------------------------*/
1208 #define OK(x) len = (x); break
1210 #define WR_RH_STAT(x) {info("WR:status %#8x", (x)); ohci_writel((x), \
1211 &ohci->regs->roothub.status); }
1212 #define WR_RH_PORTSTAT(x) {info("WR:portstatus[%d] %#8x", wIndex-1, \
1213 (x)); ohci_writel((x), &ohci->regs->roothub.portstatus[wIndex-1]); }
1215 #define WR_RH_STAT(x) ohci_writel((x), &ohci->regs->roothub.status)
1216 #define WR_RH_PORTSTAT(x) ohci_writel((x), \
1217 &ohci->regs->roothub.portstatus[wIndex-1])
1219 #define RD_RH_STAT roothub_status(ohci)
1220 #define RD_RH_PORTSTAT roothub_portstatus(ohci, wIndex-1)
1222 /* request to virtual root hub */
1224 int rh_check_port_status(ohci_t *controller)
1230 temp = roothub_a(controller);
1231 ndp = (temp & RH_A_NDP);
1232 #ifdef CONFIG_AT91C_PQFP_UHPBUG
1233 ndp = (ndp == 2) ? 1:0;
1235 for (i = 0; i < ndp; i++) {
1236 temp = roothub_portstatus(controller, i);
1237 /* check for a device disconnect */
1238 if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
1239 (RH_PS_PESC | RH_PS_CSC)) &&
1240 ((temp & RH_PS_CCS) == 0)) {
1248 static int ohci_submit_rh_msg(ohci_t *ohci, struct usb_device *dev,
1249 unsigned long pipe, void *buffer, int transfer_len,
1250 struct devrequest *cmd)
1252 void *data = buffer;
1253 int leni = transfer_len;
1260 ALLOC_ALIGN_BUFFER(__u8, databuf, 16, sizeof(u32));
1263 pkt_print(ohci, NULL, dev, pipe, buffer, transfer_len,
1264 cmd, "SUB(rh)", usb_pipein(pipe));
1268 if (usb_pipeint(pipe)) {
1269 info("Root-Hub submit IRQ: NOT implemented");
1273 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
1274 wValue = le16_to_cpu(cmd->value);
1275 wIndex = le16_to_cpu(cmd->index);
1276 wLength = le16_to_cpu(cmd->length);
1278 info("Root-Hub: adr: %2x cmd(%1x): %08x %04x %04x %04x",
1279 dev->devnum, 8, bmRType_bReq, wValue, wIndex, wLength);
1281 switch (bmRType_bReq) {
1282 /* Request Destination:
1283 without flags: Device,
1284 RH_INTERFACE: interface,
1285 RH_ENDPOINT: endpoint,
1286 RH_CLASS means HUB here,
1287 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1291 *(u16 *)databuf = cpu_to_le16(1);
1293 case RH_GET_STATUS | RH_INTERFACE:
1294 *(u16 *)databuf = cpu_to_le16(0);
1296 case RH_GET_STATUS | RH_ENDPOINT:
1297 *(u16 *)databuf = cpu_to_le16(0);
1299 case RH_GET_STATUS | RH_CLASS:
1300 *(u32 *)databuf = cpu_to_le32(
1301 RD_RH_STAT & ~(RH_HS_CRWE | RH_HS_DRWE));
1303 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
1304 *(u32 *)databuf = cpu_to_le32(RD_RH_PORTSTAT);
1307 case RH_CLEAR_FEATURE | RH_ENDPOINT:
1309 case (RH_ENDPOINT_STALL):
1314 case RH_CLEAR_FEATURE | RH_CLASS:
1316 case RH_C_HUB_LOCAL_POWER:
1318 case (RH_C_HUB_OVER_CURRENT):
1319 WR_RH_STAT(RH_HS_OCIC);
1324 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1326 case (RH_PORT_ENABLE): WR_RH_PORTSTAT(RH_PS_CCS); OK(0);
1327 case (RH_PORT_SUSPEND): WR_RH_PORTSTAT(RH_PS_POCI); OK(0);
1328 case (RH_PORT_POWER): WR_RH_PORTSTAT(RH_PS_LSDA); OK(0);
1329 case (RH_C_PORT_CONNECTION): WR_RH_PORTSTAT(RH_PS_CSC); OK(0);
1330 case (RH_C_PORT_ENABLE): WR_RH_PORTSTAT(RH_PS_PESC); OK(0);
1331 case (RH_C_PORT_SUSPEND): WR_RH_PORTSTAT(RH_PS_PSSC); OK(0);
1332 case (RH_C_PORT_OVER_CURRENT):WR_RH_PORTSTAT(RH_PS_OCIC); OK(0);
1333 case (RH_C_PORT_RESET): WR_RH_PORTSTAT(RH_PS_PRSC); OK(0);
1337 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1339 case (RH_PORT_SUSPEND):
1340 WR_RH_PORTSTAT(RH_PS_PSS); OK(0);
1341 case (RH_PORT_RESET): /* BUG IN HUP CODE *********/
1342 if (RD_RH_PORTSTAT & RH_PS_CCS)
1343 WR_RH_PORTSTAT(RH_PS_PRS);
1345 case (RH_PORT_POWER):
1346 WR_RH_PORTSTAT(RH_PS_PPS);
1348 case (RH_PORT_ENABLE): /* BUG IN HUP CODE *********/
1349 if (RD_RH_PORTSTAT & RH_PS_CCS)
1350 WR_RH_PORTSTAT(RH_PS_PES);
1355 case RH_SET_ADDRESS:
1356 ohci->rh.devnum = wValue;
1359 case RH_GET_DESCRIPTOR:
1360 switch ((wValue & 0xff00) >> 8) {
1361 case (0x01): /* device descriptor */
1362 len = min_t(unsigned int,
1365 sizeof(root_hub_dev_des),
1367 databuf = root_hub_dev_des; OK(len);
1368 case (0x02): /* configuration descriptor */
1369 len = min_t(unsigned int,
1372 sizeof(root_hub_config_des),
1374 databuf = root_hub_config_des; OK(len);
1375 case (0x03): /* string descriptors */
1376 if (wValue == 0x0300) {
1377 len = min_t(unsigned int,
1380 sizeof(root_hub_str_index0),
1382 databuf = root_hub_str_index0;
1385 if (wValue == 0x0301) {
1386 len = min_t(unsigned int,
1389 sizeof(root_hub_str_index1),
1391 databuf = root_hub_str_index1;
1395 stat = USB_ST_STALLED;
1399 case RH_GET_DESCRIPTOR | RH_CLASS:
1401 __u32 temp = roothub_a(ohci);
1403 databuf[0] = 9; /* min length; */
1405 databuf[2] = temp & RH_A_NDP;
1406 #ifdef CONFIG_AT91C_PQFP_UHPBUG
1407 databuf[2] = (databuf[2] == 2) ? 1 : 0;
1410 if (temp & RH_A_PSM) /* per-port power switching? */
1412 if (temp & RH_A_NOCP) /* no overcurrent reporting? */
1414 else if (temp & RH_A_OCPM)/* per-port overcurrent reporting? */
1418 databuf[5] = (temp & RH_A_POTPGT) >> 24;
1420 temp = roothub_b(ohci);
1421 databuf[7] = temp & RH_B_DR;
1422 if (databuf[2] < 7) {
1426 databuf[8] = (temp & RH_B_DR) >> 8;
1427 databuf[10] = databuf[9] = 0xff;
1430 len = min_t(unsigned int, leni,
1431 min_t(unsigned int, databuf[0], wLength));
1435 case RH_GET_CONFIGURATION:
1439 case RH_SET_CONFIGURATION:
1440 WR_RH_STAT(0x10000);
1444 dbg("unsupported root hub command");
1445 stat = USB_ST_STALLED;
1449 ohci_dump_roothub(ohci, 1);
1454 len = min_t(int, len, leni);
1455 if (data != databuf)
1456 memcpy(data, databuf, len);
1461 pkt_print(ohci, NULL, dev, pipe, buffer,
1462 transfer_len, cmd, "RET(rh)", 0/*usb_pipein(pipe)*/);
1470 /*-------------------------------------------------------------------------*/
1472 static ohci_dev_t *ohci_get_ohci_dev(ohci_t *ohci, int devnum, int intr)
1477 return &ohci->ohci_dev;
1479 /* First see if we already have an ohci_dev for this dev. */
1480 for (i = 0; i < NUM_INT_DEVS; i++) {
1481 if (ohci->int_dev[i].devnum == devnum)
1482 return &ohci->int_dev[i];
1485 /* If not then find a free one. */
1486 for (i = 0; i < NUM_INT_DEVS; i++) {
1487 if (ohci->int_dev[i].devnum == -1) {
1488 ohci->int_dev[i].devnum = devnum;
1489 return &ohci->int_dev[i];
1493 printf("ohci: Error out of ohci_devs for interrupt endpoints\n");
1497 /* common code for handling submit messages - used for all but root hub */
1499 static urb_priv_t *ohci_alloc_urb(struct usb_device *dev, unsigned long pipe,
1500 void *buffer, int transfer_len, int interval)
1504 urb = calloc(1, sizeof(urb_priv_t));
1506 printf("ohci: Error out of memory allocating urb\n");
1512 urb->transfer_buffer = buffer;
1513 urb->transfer_buffer_length = transfer_len;
1514 urb->interval = interval;
1519 static int submit_common_msg(ohci_t *ohci, struct usb_device *dev,
1520 unsigned long pipe, void *buffer, int transfer_len,
1521 struct devrequest *setup, int interval)
1524 int maxsize = usb_maxpacket(dev, pipe);
1527 ohci_dev_t *ohci_dev;
1529 urb = ohci_alloc_urb(dev, pipe, buffer, transfer_len, interval);
1534 urb->actual_length = 0;
1535 pkt_print(ohci, urb, dev, pipe, buffer, transfer_len,
1536 setup, "SUB", usb_pipein(pipe));
1541 err("submit_common_message: pipesize for pipe %lx is zero",
1546 ohci_dev = ohci_get_ohci_dev(ohci, dev->devnum, usb_pipeint(pipe));
1550 if (sohci_submit_job(ohci, ohci_dev, urb, setup) < 0) {
1551 err("sohci_submit_job failed");
1557 /* ohci_dump_status(ohci); */
1560 timeout = USB_TIMEOUT_MS(pipe);
1562 /* wait for it to complete */
1564 /* check whether the controller is done */
1565 stat = hc_interrupt(ohci);
1567 stat = USB_ST_CRC_ERR;
1571 /* NOTE: since we are not interrupt driven in U-Boot and always
1572 * handle only one URB at a time, we cannot assume the
1573 * transaction finished on the first successful return from
1574 * hc_interrupt().. unless the flag for current URB is set,
1575 * meaning that all TD's to/from device got actually
1576 * transferred and processed. If the current URB is not
1577 * finished we need to re-iterate this loop so as
1578 * hc_interrupt() gets called again as there needs to be some
1579 * more TD's to process still */
1580 if ((stat >= 0) && (stat != 0xff) && (urb->finished)) {
1581 /* 0xff is returned for an SF-interrupt */
1591 if (!usb_pipeint(pipe))
1592 err("CTL:TIMEOUT ");
1593 dbg("submit_common_msg: TO status %x\n", stat);
1595 stat = USB_ST_CRC_ERR;
1601 dev->act_len = urb->actual_length;
1603 if (usb_pipein(pipe) && dev->status == 0 && dev->act_len)
1604 invalidate_dcache_buffer(buffer, dev->act_len);
1607 pkt_print(ohci, urb, dev, pipe, buffer, transfer_len,
1608 setup, "RET(ctlr)", usb_pipein(pipe));
1616 #define MAX_INT_QUEUESIZE 8
1621 urb_priv_t *urb[MAX_INT_QUEUESIZE];
1624 static struct int_queue *_ohci_create_int_queue(ohci_t *ohci,
1625 struct usb_device *udev, unsigned long pipe, int queuesize,
1626 int elementsize, void *buffer, int interval)
1628 struct int_queue *queue;
1629 ohci_dev_t *ohci_dev;
1632 if (queuesize > MAX_INT_QUEUESIZE)
1635 ohci_dev = ohci_get_ohci_dev(ohci, udev->devnum, 1);
1639 queue = malloc(sizeof(*queue));
1641 printf("ohci: Error out of memory allocating int queue\n");
1645 for (i = 0; i < queuesize; i++) {
1646 queue->urb[i] = ohci_alloc_urb(udev, pipe,
1647 buffer + i * elementsize,
1648 elementsize, interval);
1652 if (sohci_submit_job(ohci, ohci_dev, queue->urb[i], NULL)) {
1653 printf("ohci: Error submitting int queue job\n");
1654 urb_free_priv(queue->urb[i]);
1659 /* We did not succeed in submitting even 1 urb */
1664 queue->queuesize = i;
1665 queue->curr_urb = 0;
1670 static void *_ohci_poll_int_queue(ohci_t *ohci, struct usb_device *udev,
1671 struct int_queue *queue)
1673 if (queue->curr_urb == queue->queuesize)
1674 return NULL; /* Queue depleted */
1676 if (hc_interrupt(ohci) < 0)
1679 if (queue->urb[queue->curr_urb]->finished) {
1680 void *ret = queue->urb[queue->curr_urb]->transfer_buffer;
1688 static int _ohci_destroy_int_queue(ohci_t *ohci, struct usb_device *dev,
1689 struct int_queue *queue)
1693 for (i = 0; i < queue->queuesize; i++)
1694 urb_free_priv(queue->urb[i]);
1701 #ifndef CONFIG_DM_USB
1702 /* submit routines called from usb.c */
1703 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1706 info("submit_bulk_msg");
1707 return submit_common_msg(&gohci, dev, pipe, buffer, transfer_len,
1711 int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1712 int transfer_len, int interval)
1714 info("submit_int_msg");
1715 return submit_common_msg(&gohci, dev, pipe, buffer, transfer_len, NULL,
1719 struct int_queue *create_int_queue(struct usb_device *dev,
1720 unsigned long pipe, int queuesize, int elementsize,
1721 void *buffer, int interval)
1723 return _ohci_create_int_queue(&gohci, dev, pipe, queuesize,
1724 elementsize, buffer, interval);
1727 void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
1729 return _ohci_poll_int_queue(&gohci, dev, queue);
1732 int destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
1734 return _ohci_destroy_int_queue(&gohci, dev, queue);
1738 static int _ohci_submit_control_msg(ohci_t *ohci, struct usb_device *dev,
1739 unsigned long pipe, void *buffer, int transfer_len,
1740 struct devrequest *setup)
1742 int maxsize = usb_maxpacket(dev, pipe);
1744 info("submit_control_msg");
1746 pkt_print(ohci, NULL, dev, pipe, buffer, transfer_len,
1747 setup, "SUB", usb_pipein(pipe));
1752 err("submit_control_message: pipesize for pipe %lx is zero",
1756 if (((pipe >> 8) & 0x7f) == ohci->rh.devnum) {
1758 /* root hub - redirect */
1759 return ohci_submit_rh_msg(ohci, dev, pipe, buffer,
1760 transfer_len, setup);
1763 return submit_common_msg(ohci, dev, pipe, buffer, transfer_len,
1767 /*-------------------------------------------------------------------------*
1769 *-------------------------------------------------------------------------*/
1771 /* reset the HC and BUS */
1773 static int hc_reset(ohci_t *ohci)
1775 #ifdef CONFIG_PCI_EHCI_DEVNO
1779 int smm_timeout = 50; /* 0,5 sec */
1781 dbg("%s\n", __FUNCTION__);
1783 #ifdef CONFIG_PCI_EHCI_DEVNO
1785 * Some multi-function controllers (e.g. ISP1562) allow root hub
1786 * resetting via EHCI registers only.
1788 pdev = pci_find_devices(ehci_pci_ids, CONFIG_PCI_EHCI_DEVNO);
1793 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &base);
1794 base += EHCI_USBCMD_OFF;
1795 ohci_writel(ohci_readl(base) | EHCI_USBCMD_HCRESET, base);
1797 while (ohci_readl(base) & EHCI_USBCMD_HCRESET) {
1798 if (timeout-- <= 0) {
1799 printf("USB RootHub reset timed out!");
1805 printf("No EHCI func at %d index!\n", CONFIG_PCI_EHCI_DEVNO);
1807 if (ohci_readl(&ohci->regs->control) & OHCI_CTRL_IR) {
1808 /* SMM owns the HC, request ownership */
1809 ohci_writel(OHCI_OCR, &ohci->regs->cmdstatus);
1810 info("USB HC TakeOver from SMM");
1811 while (ohci_readl(&ohci->regs->control) & OHCI_CTRL_IR) {
1813 if (--smm_timeout == 0) {
1814 err("USB HC TakeOver failed!");
1820 /* Disable HC interrupts */
1821 ohci_writel(OHCI_INTR_MIE, &ohci->regs->intrdisable);
1823 dbg("USB HC reset_hc usb-%s: ctrl = 0x%X ;\n",
1825 ohci_readl(&ohci->regs->control));
1827 /* Reset USB (needed by some controllers) */
1828 ohci->hc_control = 0;
1829 ohci_writel(ohci->hc_control, &ohci->regs->control);
1831 /* HC Reset requires max 10 us delay */
1832 ohci_writel(OHCI_HCR, &ohci->regs->cmdstatus);
1833 while ((ohci_readl(&ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
1834 if (--timeout == 0) {
1835 err("USB HC reset timed out!");
1843 /*-------------------------------------------------------------------------*/
1845 /* Start an OHCI controller, set the BUS operational
1847 * connect the virtual root hub */
1849 static int hc_start(ohci_t *ohci)
1852 unsigned int fminterval;
1856 for (i = 0; i < NUM_INT_DEVS; i++)
1857 ohci->int_dev[i].devnum = -1;
1859 /* Tell the controller where the control and bulk lists are
1860 * The lists are empty now. */
1862 ohci_writel(0, &ohci->regs->ed_controlhead);
1863 ohci_writel(0, &ohci->regs->ed_bulkhead);
1865 ohci_writel((uintptr_t)ohci->hcca,
1866 &ohci->regs->hcca); /* reset clears this */
1868 fminterval = 0x2edf;
1869 ohci_writel((fminterval * 9) / 10, &ohci->regs->periodicstart);
1870 fminterval |= ((((fminterval - 210) * 6) / 7) << 16);
1871 ohci_writel(fminterval, &ohci->regs->fminterval);
1872 ohci_writel(0x628, &ohci->regs->lsthresh);
1874 /* start controller operations */
1875 ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
1877 ohci_writel(ohci->hc_control, &ohci->regs->control);
1879 /* disable all interrupts */
1880 mask = (OHCI_INTR_SO | OHCI_INTR_WDH | OHCI_INTR_SF | OHCI_INTR_RD |
1881 OHCI_INTR_UE | OHCI_INTR_FNO | OHCI_INTR_RHSC |
1882 OHCI_INTR_OC | OHCI_INTR_MIE);
1883 ohci_writel(mask, &ohci->regs->intrdisable);
1884 /* clear all interrupts */
1885 mask &= ~OHCI_INTR_MIE;
1886 ohci_writel(mask, &ohci->regs->intrstatus);
1887 /* Choose the interrupts we care about now - but w/o MIE */
1888 mask = OHCI_INTR_RHSC | OHCI_INTR_UE | OHCI_INTR_WDH | OHCI_INTR_SO;
1889 ohci_writel(mask, &ohci->regs->intrenable);
1892 /* required for AMD-756 and some Mac platforms */
1893 ohci_writel((roothub_a(ohci) | RH_A_NPS) & ~RH_A_PSM,
1894 &ohci->regs->roothub.a);
1895 ohci_writel(RH_HS_LPSC, &ohci->regs->roothub.status);
1896 #endif /* OHCI_USE_NPS */
1898 /* connect the virtual root hub */
1899 ohci->rh.devnum = 0;
1904 /*-------------------------------------------------------------------------*/
1906 /* an interrupt happens */
1908 static int hc_interrupt(ohci_t *ohci)
1910 struct ohci_regs *regs = ohci->regs;
1914 invalidate_dcache_hcca(ohci->hcca);
1916 if ((ohci->hcca->done_head != 0) &&
1917 !(m32_swap(ohci->hcca->done_head) & 0x01)) {
1918 ints = OHCI_INTR_WDH;
1920 ints = ohci_readl(®s->intrstatus);
1921 if (ints == ~(u32)0) {
1923 err("%s device removed!", ohci->slot_name);
1926 ints &= ohci_readl(®s->intrenable);
1928 dbg("hc_interrupt: returning..\n");
1934 /* dbg("Interrupt: %x frame: %x", ints,
1935 le16_to_cpu(ohci->hcca->frame_no)); */
1937 if (ints & OHCI_INTR_RHSC)
1940 if (ints & OHCI_INTR_UE) {
1942 err("OHCI Unrecoverable Error, controller usb-%s disabled",
1944 /* e.g. due to PCI Master/Target Abort */
1951 /* FIXME: be optimistic, hope that bug won't repeat often. */
1952 /* Make some non-interrupt context restart the controller. */
1953 /* Count and limit the retries though; either hardware or */
1954 /* software errors can go forever... */
1959 if (ints & OHCI_INTR_WDH) {
1961 ohci_writel(OHCI_INTR_WDH, ®s->intrdisable);
1962 (void)ohci_readl(®s->intrdisable); /* flush */
1963 stat = dl_done_list(ohci);
1964 ohci_writel(OHCI_INTR_WDH, ®s->intrenable);
1965 (void)ohci_readl(®s->intrdisable); /* flush */
1968 if (ints & OHCI_INTR_SO) {
1969 dbg("USB Schedule overrun\n");
1970 ohci_writel(OHCI_INTR_SO, ®s->intrenable);
1974 /* FIXME: this assumes SOF (1/ms) interrupts don't get lost... */
1975 if (ints & OHCI_INTR_SF) {
1976 unsigned int frame = m16_swap(ohci->hcca->frame_no) & 1;
1978 ohci_writel(OHCI_INTR_SF, ®s->intrdisable);
1979 if (ohci->ed_rm_list[frame] != NULL)
1980 ohci_writel(OHCI_INTR_SF, ®s->intrenable);
1984 ohci_writel(ints, ®s->intrstatus);
1988 /*-------------------------------------------------------------------------*/
1990 #ifndef CONFIG_DM_USB
1992 /*-------------------------------------------------------------------------*/
1994 /* De-allocate all resources.. */
1996 static void hc_release_ohci(ohci_t *ohci)
1998 dbg("USB HC release ohci usb-%s", ohci->slot_name);
2000 if (!ohci->disabled)
2004 /*-------------------------------------------------------------------------*/
2007 * low level initalisation routine, called from usb.c
2009 static char ohci_inited = 0;
2011 int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
2013 #ifdef CONFIG_PCI_OHCI
2017 #ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
2018 /* cpu dependant init */
2023 #ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
2024 /* board dependant init */
2025 if (board_usb_init(index, USB_INIT_HOST))
2028 memset(&gohci, 0, sizeof(ohci_t));
2030 /* align the storage */
2031 if ((__u32)&ghcca[0] & 0xff) {
2032 err("HCCA not aligned!!");
2035 gohci.hcca = &ghcca[0];
2036 info("aligned ghcca %p", gohci.hcca);
2037 memset(gohci.hcca, 0, sizeof(struct ohci_hcca));
2042 #ifdef CONFIG_PCI_OHCI
2043 pdev = pci_find_devices(ohci_pci_ids, CONFIG_PCI_OHCI_DEVNO);
2048 pci_read_config_word(pdev, PCI_VENDOR_ID, &vid);
2049 pci_read_config_word(pdev, PCI_DEVICE_ID, &did);
2050 printf("OHCI pci controller (%04x, %04x) found @(%d:%d:%d)\n",
2051 vid, did, (pdev >> 16) & 0xff,
2052 (pdev >> 11) & 0x1f, (pdev >> 8) & 0x7);
2053 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &base);
2054 printf("OHCI regs address 0x%08x\n", base);
2055 gohci.regs = (struct ohci_regs *)base;
2059 gohci.regs = (struct ohci_regs *)CONFIG_SYS_USB_OHCI_REGS_BASE;
2063 gohci.slot_name = CONFIG_SYS_USB_OHCI_SLOT_NAME;
2065 if (hc_reset (&gohci) < 0) {
2066 hc_release_ohci (&gohci);
2067 err ("can't reset usb-%s", gohci.slot_name);
2068 #ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
2069 /* board dependant cleanup */
2070 board_usb_cleanup(index, USB_INIT_HOST);
2073 #ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
2074 /* cpu dependant cleanup */
2075 usb_cpu_init_fail();
2080 if (hc_start(&gohci) < 0) {
2081 err("can't start usb-%s", gohci.slot_name);
2082 hc_release_ohci(&gohci);
2083 /* Initialization failed */
2084 #ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
2085 /* board dependant cleanup */
2089 #ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
2090 /* cpu dependant cleanup */
2097 ohci_dump(&gohci, 1);
2105 int usb_lowlevel_stop(int index)
2107 /* this gets called really early - before the controller has */
2108 /* even been initialized! */
2111 /* TODO release any interrupts, etc. */
2112 /* call hc_release_ohci() here ? */
2115 #ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
2116 /* board dependant cleanup */
2117 if (usb_board_stop())
2121 #ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
2122 /* cpu dependant cleanup */
2126 /* This driver is no longer initialised. It needs a new low-level
2127 * init (board/cpu) before it can be used again. */
2132 int submit_control_msg(struct usb_device *dev, unsigned long pipe,
2133 void *buffer, int transfer_len, struct devrequest *setup)
2135 return _ohci_submit_control_msg(&gohci, dev, pipe, buffer,
2136 transfer_len, setup);
2140 #ifdef CONFIG_DM_USB
2141 static int ohci_submit_control_msg(struct udevice *dev, struct usb_device *udev,
2142 unsigned long pipe, void *buffer, int length,
2143 struct devrequest *setup)
2145 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2147 return _ohci_submit_control_msg(ohci, udev, pipe, buffer,
2151 static int ohci_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
2152 unsigned long pipe, void *buffer, int length)
2154 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2156 return submit_common_msg(ohci, udev, pipe, buffer, length, NULL, 0);
2159 static int ohci_submit_int_msg(struct udevice *dev, struct usb_device *udev,
2160 unsigned long pipe, void *buffer, int length,
2163 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2165 return submit_common_msg(ohci, udev, pipe, buffer, length,
2169 static struct int_queue *ohci_create_int_queue(struct udevice *dev,
2170 struct usb_device *udev, unsigned long pipe, int queuesize,
2171 int elementsize, void *buffer, int interval)
2173 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2175 return _ohci_create_int_queue(ohci, udev, pipe, queuesize, elementsize,
2179 static void *ohci_poll_int_queue(struct udevice *dev, struct usb_device *udev,
2180 struct int_queue *queue)
2182 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2184 return _ohci_poll_int_queue(ohci, udev, queue);
2187 static int ohci_destroy_int_queue(struct udevice *dev, struct usb_device *udev,
2188 struct int_queue *queue)
2190 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2192 return _ohci_destroy_int_queue(ohci, udev, queue);
2195 int ohci_register(struct udevice *dev, struct ohci_regs *regs)
2197 struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
2198 ohci_t *ohci = dev_get_priv(dev);
2201 priv->desc_before_addr = true;
2204 ohci->hcca = memalign(256, sizeof(struct ohci_hcca));
2207 memset(ohci->hcca, 0, sizeof(struct ohci_hcca));
2208 flush_dcache_hcca(ohci->hcca);
2210 if (hc_reset(ohci) < 0)
2213 if (hc_start(ohci) < 0)
2216 reg = ohci_readl(®s->revision);
2217 printf("USB OHCI %x.%x\n", (reg >> 4) & 0xf, reg & 0xf);
2222 int ohci_deregister(struct udevice *dev)
2224 ohci_t *ohci = dev_get_priv(dev);
2226 if (hc_reset(ohci) < 0)
2234 struct dm_usb_ops ohci_usb_ops = {
2235 .control = ohci_submit_control_msg,
2236 .bulk = ohci_submit_bulk_msg,
2237 .interrupt = ohci_submit_int_msg,
2238 .create_int_queue = ohci_create_int_queue,
2239 .poll_int_queue = ohci_poll_int_queue,
2240 .destroy_int_queue = ohci_destroy_int_queue,