1 // SPDX-License-Identifier: GPL-2.0+
3 * URB OHCI HCD (Host Controller Driver) for USB on the AT91RM9200 and PCI bus.
5 * Interrupt support is added. Now, it has been tested
6 * on ULI1575 chip and works well with USB keyboard.
9 * Zhang Wei, Freescale Semiconductor, Inc. <wei.zhang@freescale.com>
12 * Gary Jennejohn, DENX Software Engineering <garyj@denx.de>
14 * Note: Much of this code has been derived from Linux 2.4
15 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
16 * (C) Copyright 2000-2002 David Brownell
18 * Modified for the MP2USB by (C) Copyright 2005 Eric Benard
19 * ebenard@eukrea.com - based on s3c24x0's driver
23 * 1 - Read doc/README.generic_usb_ohci
24 * 2 - this driver is intended for use with USB Mass Storage Devices
25 * (BBB) and USB keyboard. There is NO support for Isochronous pipes!
26 * 2 - when running on a PQFP208 AT91RM9200, define CONFIG_AT91C_PQFP_UHPBUG
27 * to activate workaround for bug #41 or this driver will NOT work!
31 #include <asm/byteorder.h>
35 #if defined(CONFIG_PCI_OHCI)
37 #if !defined(CONFIG_PCI_OHCI_DEVNO)
38 #define CONFIG_PCI_OHCI_DEVNO 0
48 #ifdef CONFIG_AT91RM9200
49 #include <asm/arch/hardware.h> /* needed for AT91_USB_HOST_BASE */
52 #if defined(CONFIG_CPU_ARM920T) || \
53 defined(CONFIG_PCI_OHCI) || \
54 defined(CONFIG_SYS_OHCI_USE_NPS)
55 # define OHCI_USE_NPS /* force NoPowerSwitching mode */
58 #undef OHCI_VERBOSE_DEBUG /* not always helpful */
61 #undef OHCI_FILL_TRACE
63 /* For initializing controller (mask in an HCFS mode too) */
64 #define OHCI_CONTROL_INIT \
65 (OHCI_CTRL_CBSR & 0x3) | OHCI_CTRL_IE | OHCI_CTRL_PLE
67 #ifdef CONFIG_PCI_OHCI
68 static struct pci_device_id ohci_pci_ids[] = {
69 {0x10b9, 0x5237}, /* ULI1575 PCI OHCI module ids */
70 {0x1033, 0x0035}, /* NEC PCI OHCI module ids */
71 {0x1131, 0x1561}, /* Philips 1561 PCI OHCI module ids */
72 /* Please add supported PCI OHCI controller ids here */
77 #ifdef CONFIG_PCI_EHCI_DEVNO
78 static struct pci_device_id ehci_pci_ids[] = {
79 {0x1131, 0x1562}, /* Philips 1562 PCI EHCI module ids */
80 /* Please add supported PCI EHCI controller ids here */
86 #define dbg(format, arg...) printf("DEBUG: " format "\n", ## arg)
88 #define dbg(format, arg...) do {} while (0)
90 #define err(format, arg...) printf("ERROR: " format "\n", ## arg)
92 #define info(format, arg...) printf("INFO: " format "\n", ## arg)
94 #define info(format, arg...) do {} while (0)
97 #ifdef CONFIG_SYS_OHCI_BE_CONTROLLER
98 # define m16_swap(x) cpu_to_be16(x)
99 # define m32_swap(x) cpu_to_be32(x)
101 # define m16_swap(x) cpu_to_le16(x)
102 # define m32_swap(x) cpu_to_le32(x)
103 #endif /* CONFIG_SYS_OHCI_BE_CONTROLLER */
105 /* We really should do proper cache flushing everywhere */
106 #define flush_dcache_buffer(addr, size) \
107 flush_dcache_range((unsigned long)(addr), \
108 ALIGN((unsigned long)(addr) + size, ARCH_DMA_MINALIGN))
109 #define invalidate_dcache_buffer(addr, size) \
110 invalidate_dcache_range((unsigned long)(addr), \
111 ALIGN((unsigned long)(addr) + size, ARCH_DMA_MINALIGN))
113 /* Do not use sizeof(ed / td) as our ed / td structs contain extra members */
114 #define flush_dcache_ed(addr) flush_dcache_buffer(addr, 16)
115 #define flush_dcache_td(addr) flush_dcache_buffer(addr, 16)
116 #define flush_dcache_iso_td(addr) flush_dcache_buffer(addr, 32)
117 #define flush_dcache_hcca(addr) flush_dcache_buffer(addr, 256)
118 #define invalidate_dcache_ed(addr) invalidate_dcache_buffer(addr, 16)
119 #define invalidate_dcache_td(addr) invalidate_dcache_buffer(addr, 16)
120 #define invalidate_dcache_iso_td(addr) invalidate_dcache_buffer(addr, 32)
121 #define invalidate_dcache_hcca(addr) invalidate_dcache_buffer(addr, 256)
123 #if CONFIG_IS_ENABLED(DM_USB)
125 * The various ohci_mdelay(1) calls in the code seem unnecessary. We keep
126 * them around when building for older boards not yet converted to the dm
127 * just in case (to avoid regressions), for dm this turns them into nops.
129 #define ohci_mdelay(x)
131 #define ohci_mdelay(x) mdelay(x)
134 #if !CONFIG_IS_ENABLED(DM_USB)
137 /* this must be aligned to a 256 byte boundary */
138 struct ohci_hcca ghcca[1];
141 /* mapping of the OHCI CC status to error codes */
142 static int cc_to_error[16] = {
144 /* CRC Error */ USB_ST_CRC_ERR,
145 /* Bit Stuff */ USB_ST_BIT_ERR,
146 /* Data Togg */ USB_ST_CRC_ERR,
147 /* Stall */ USB_ST_STALLED,
149 /* PIDCheck */ USB_ST_BIT_ERR,
150 /* UnExpPID */ USB_ST_BIT_ERR,
151 /* DataOver */ USB_ST_BUF_ERR,
152 /* DataUnder */ USB_ST_BUF_ERR,
155 /* BufferOver */ USB_ST_BUF_ERR,
156 /* BuffUnder */ USB_ST_BUF_ERR,
161 static const char *cc_to_string[16] = {
163 "CRC: Last data packet from endpoint contained a CRC error.",
164 "BITSTUFFING: Last data packet from endpoint contained a bit " \
165 "stuffing violation",
166 "DATATOGGLEMISMATCH: Last packet from endpoint had data toggle PID\n" \
167 "that did not match the expected value.",
168 "STALL: TD was moved to the Done Queue because the endpoint returned" \
170 "DEVICENOTRESPONDING: Device did not respond to token (IN) or did\n" \
171 "not provide a handshake (OUT)",
172 "PIDCHECKFAILURE: Check bits on PID from endpoint failed on data PID\n"\
173 "(IN) or handshake (OUT)",
174 "UNEXPECTEDPID: Receive PID was not valid when encountered or PID\n" \
175 "value is not defined.",
176 "DATAOVERRUN: The amount of data returned by the endpoint exceeded\n" \
177 "either the size of the maximum data packet allowed\n" \
178 "from the endpoint (found in MaximumPacketSize field\n" \
179 "of ED) or the remaining buffer size.",
180 "DATAUNDERRUN: The endpoint returned less than MaximumPacketSize\n" \
181 "and that amount was not sufficient to fill the\n" \
185 "BUFFEROVERRUN: During an IN, HC received data from endpoint faster\n" \
186 "than it could be written to system memory",
187 "BUFFERUNDERRUN: During an OUT, HC could not retrieve data from\n" \
188 "system memory fast enough to keep up with data USB " \
190 "NOT ACCESSED: This code is set by software before the TD is placed" \
191 "on a list to be processed by the HC.(1)",
192 "NOT ACCESSED: This code is set by software before the TD is placed" \
193 "on a list to be processed by the HC.(2)",
196 static inline u32 roothub_a(struct ohci *hc)
197 { return ohci_readl(&hc->regs->roothub.a); }
198 static inline u32 roothub_b(struct ohci *hc)
199 { return ohci_readl(&hc->regs->roothub.b); }
200 static inline u32 roothub_status(struct ohci *hc)
201 { return ohci_readl(&hc->regs->roothub.status); }
202 static inline u32 roothub_portstatus(struct ohci *hc, int i)
203 { return ohci_readl(&hc->regs->roothub.portstatus[i]); }
205 /* forward declaration */
206 static int hc_interrupt(ohci_t *ohci);
207 static void td_submit_job(ohci_t *ohci, struct usb_device *dev,
208 unsigned long pipe, void *buffer, int transfer_len,
209 struct devrequest *setup, urb_priv_t *urb,
211 static int ep_link(ohci_t * ohci, ed_t * ed);
212 static int ep_unlink(ohci_t * ohci, ed_t * ed);
213 static ed_t *ep_add_ed(ohci_dev_t *ohci_dev, struct usb_device *usb_dev,
214 unsigned long pipe, int interval, int load);
216 /*-------------------------------------------------------------------------*/
219 static struct td *td_alloc(ohci_dev_t *ohci_dev, struct usb_device *usb_dev)
225 for (i = 0; i < NUM_TD; i++)
227 if (ohci_dev->tds[i].usb_dev == NULL)
229 td = &ohci_dev->tds[i];
230 td->usb_dev = usb_dev;
238 static inline void ed_free(struct ed *ed)
243 /*-------------------------------------------------------------------------*
244 * URB support functions
245 *-------------------------------------------------------------------------*/
247 /* free HCD-private data associated with this URB */
249 static void urb_free_priv(urb_priv_t *urb)
255 last = urb->length - 1;
257 for (i = 0; i <= last; i++) {
268 /*-------------------------------------------------------------------------*/
271 static int sohci_get_current_frame_number(ohci_t *ohci);
273 /* debug| print the main components of an URB
274 * small: 0) header + data packets 1) just header */
276 static void pkt_print(ohci_t *ohci, urb_priv_t *purb, struct usb_device *dev,
277 unsigned long pipe, void *buffer, int transfer_len,
278 struct devrequest *setup, char *str, int small)
280 dbg("%s URB:[%4x] dev:%2lu,ep:%2lu-%c,type:%s,len:%d/%d stat:%#lx",
282 sohci_get_current_frame_number(ohci),
283 usb_pipedevice(pipe),
284 usb_pipeendpoint(pipe),
285 usb_pipeout(pipe)? 'O': 'I',
286 usb_pipetype(pipe) < 2 ? \
287 (usb_pipeint(pipe)? "INTR": "ISOC"): \
288 (usb_pipecontrol(pipe)? "CTRL": "BULK"),
289 (purb ? purb->actual_length : 0),
290 transfer_len, dev->status);
291 #ifdef OHCI_VERBOSE_DEBUG
295 if (usb_pipecontrol(pipe)) {
296 printf(__FILE__ ": cmd(8):");
297 for (i = 0; i < 8 ; i++)
298 printf(" %02x", ((__u8 *) setup) [i]);
301 if (transfer_len > 0 && buffer) {
302 printf(__FILE__ ": data(%d/%d):",
303 (purb ? purb->actual_length : 0),
305 len = usb_pipeout(pipe)? transfer_len:
306 (purb ? purb->actual_length : 0);
307 for (i = 0; i < 16 && i < len; i++)
308 printf(" %02x", ((__u8 *) buffer) [i]);
309 printf("%s\n", i < len? "...": "");
315 /* just for debugging; prints non-empty branches of the int ed tree
316 * inclusive iso eds */
317 void ep_print_int_eds(ohci_t *ohci, char *str)
321 for (i = 0; i < 32; i++) {
323 ed_p = &(ohci->hcca->int_table [i]);
326 invalidate_dcache_ed(ed_p);
327 printf(__FILE__ ": %s branch int %2d(%2x):", str, i, i);
328 while (*ed_p != 0 && j--) {
329 ed_t *ed = (ed_t *)m32_swap(ed_p);
330 invalidate_dcache_ed(ed);
331 printf(" ed: %4x;", ed->hwINFO);
332 ed_p = &ed->hwNextED;
338 static void ohci_dump_intr_mask(char *label, __u32 mask)
340 dbg("%s: 0x%08x%s%s%s%s%s%s%s%s%s",
343 (mask & OHCI_INTR_MIE) ? " MIE" : "",
344 (mask & OHCI_INTR_OC) ? " OC" : "",
345 (mask & OHCI_INTR_RHSC) ? " RHSC" : "",
346 (mask & OHCI_INTR_FNO) ? " FNO" : "",
347 (mask & OHCI_INTR_UE) ? " UE" : "",
348 (mask & OHCI_INTR_RD) ? " RD" : "",
349 (mask & OHCI_INTR_SF) ? " SF" : "",
350 (mask & OHCI_INTR_WDH) ? " WDH" : "",
351 (mask & OHCI_INTR_SO) ? " SO" : ""
355 static void maybe_print_eds(char *label, __u32 value)
357 ed_t *edp = (ed_t *)value;
360 dbg("%s %08x", label, value);
361 invalidate_dcache_ed(edp);
362 dbg("%08x", edp->hwINFO);
363 dbg("%08x", edp->hwTailP);
364 dbg("%08x", edp->hwHeadP);
365 dbg("%08x", edp->hwNextED);
369 static char *hcfs2string(int state)
372 case OHCI_USB_RESET: return "reset";
373 case OHCI_USB_RESUME: return "resume";
374 case OHCI_USB_OPER: return "operational";
375 case OHCI_USB_SUSPEND: return "suspend";
380 /* dump control and status registers */
381 static void ohci_dump_status(ohci_t *controller)
383 struct ohci_regs *regs = controller->regs;
386 temp = ohci_readl(®s->revision) & 0xff;
388 dbg("spec %d.%d", (temp >> 4), (temp & 0x0f));
390 temp = ohci_readl(®s->control);
391 dbg("control: 0x%08x%s%s%s HCFS=%s%s%s%s%s CBSR=%d", temp,
392 (temp & OHCI_CTRL_RWE) ? " RWE" : "",
393 (temp & OHCI_CTRL_RWC) ? " RWC" : "",
394 (temp & OHCI_CTRL_IR) ? " IR" : "",
395 hcfs2string(temp & OHCI_CTRL_HCFS),
396 (temp & OHCI_CTRL_BLE) ? " BLE" : "",
397 (temp & OHCI_CTRL_CLE) ? " CLE" : "",
398 (temp & OHCI_CTRL_IE) ? " IE" : "",
399 (temp & OHCI_CTRL_PLE) ? " PLE" : "",
400 temp & OHCI_CTRL_CBSR
403 temp = ohci_readl(®s->cmdstatus);
404 dbg("cmdstatus: 0x%08x SOC=%d%s%s%s%s", temp,
405 (temp & OHCI_SOC) >> 16,
406 (temp & OHCI_OCR) ? " OCR" : "",
407 (temp & OHCI_BLF) ? " BLF" : "",
408 (temp & OHCI_CLF) ? " CLF" : "",
409 (temp & OHCI_HCR) ? " HCR" : ""
412 ohci_dump_intr_mask("intrstatus", ohci_readl(®s->intrstatus));
413 ohci_dump_intr_mask("intrenable", ohci_readl(®s->intrenable));
415 maybe_print_eds("ed_periodcurrent",
416 ohci_readl(®s->ed_periodcurrent));
418 maybe_print_eds("ed_controlhead", ohci_readl(®s->ed_controlhead));
419 maybe_print_eds("ed_controlcurrent",
420 ohci_readl(®s->ed_controlcurrent));
422 maybe_print_eds("ed_bulkhead", ohci_readl(®s->ed_bulkhead));
423 maybe_print_eds("ed_bulkcurrent", ohci_readl(®s->ed_bulkcurrent));
425 maybe_print_eds("donehead", ohci_readl(®s->donehead));
428 static void ohci_dump_roothub(ohci_t *controller, int verbose)
432 temp = roothub_a(controller);
433 ndp = (temp & RH_A_NDP);
434 #ifdef CONFIG_AT91C_PQFP_UHPBUG
435 ndp = (ndp == 2) ? 1:0;
438 dbg("roothub.a: %08x POTPGT=%d%s%s%s%s%s NDP=%d", temp,
439 ((temp & RH_A_POTPGT) >> 24) & 0xff,
440 (temp & RH_A_NOCP) ? " NOCP" : "",
441 (temp & RH_A_OCPM) ? " OCPM" : "",
442 (temp & RH_A_DT) ? " DT" : "",
443 (temp & RH_A_NPS) ? " NPS" : "",
444 (temp & RH_A_PSM) ? " PSM" : "",
447 temp = roothub_b(controller);
448 dbg("roothub.b: %08x PPCM=%04x DR=%04x",
450 (temp & RH_B_PPCM) >> 16,
453 temp = roothub_status(controller);
454 dbg("roothub.status: %08x%s%s%s%s%s%s",
456 (temp & RH_HS_CRWE) ? " CRWE" : "",
457 (temp & RH_HS_OCIC) ? " OCIC" : "",
458 (temp & RH_HS_LPSC) ? " LPSC" : "",
459 (temp & RH_HS_DRWE) ? " DRWE" : "",
460 (temp & RH_HS_OCI) ? " OCI" : "",
461 (temp & RH_HS_LPS) ? " LPS" : ""
465 for (i = 0; i < ndp; i++) {
466 temp = roothub_portstatus(controller, i);
467 dbg("roothub.portstatus [%d] = 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s",
470 (temp & RH_PS_PRSC) ? " PRSC" : "",
471 (temp & RH_PS_OCIC) ? " OCIC" : "",
472 (temp & RH_PS_PSSC) ? " PSSC" : "",
473 (temp & RH_PS_PESC) ? " PESC" : "",
474 (temp & RH_PS_CSC) ? " CSC" : "",
476 (temp & RH_PS_LSDA) ? " LSDA" : "",
477 (temp & RH_PS_PPS) ? " PPS" : "",
478 (temp & RH_PS_PRS) ? " PRS" : "",
479 (temp & RH_PS_POCI) ? " POCI" : "",
480 (temp & RH_PS_PSS) ? " PSS" : "",
482 (temp & RH_PS_PES) ? " PES" : "",
483 (temp & RH_PS_CCS) ? " CCS" : ""
488 static void ohci_dump(ohci_t *controller, int verbose)
490 dbg("OHCI controller usb-%s state", controller->slot_name);
492 /* dumps some of the state we know about */
493 ohci_dump_status(controller);
495 ep_print_int_eds(controller, "hcca");
496 invalidate_dcache_hcca(controller->hcca);
497 dbg("hcca frame #%04x", controller->hcca->frame_no);
498 ohci_dump_roothub(controller, 1);
502 /*-------------------------------------------------------------------------*
503 * Interface functions (URB)
504 *-------------------------------------------------------------------------*/
506 /* get a transfer request */
508 int sohci_submit_job(ohci_t *ohci, ohci_dev_t *ohci_dev, urb_priv_t *urb,
509 struct devrequest *setup)
512 urb_priv_t *purb_priv = urb;
514 struct usb_device *dev = urb->dev;
515 unsigned long pipe = urb->pipe;
516 void *buffer = urb->transfer_buffer;
517 int transfer_len = urb->transfer_buffer_length;
518 int interval = urb->interval;
520 /* when controller's hung, permit only roothub cleanup attempts
521 * such as powering down ports */
522 if (ohci->disabled) {
523 err("sohci_submit_job: EPIPE");
527 /* we're about to begin a new transaction here so mark the
531 /* every endpoint has a ed, locate and fill it */
532 ed = ep_add_ed(ohci_dev, dev, pipe, interval, 1);
534 err("sohci_submit_job: ENOMEM");
538 /* for the private part of the URB we need the number of TDs (size) */
539 switch (usb_pipetype(pipe)) {
540 case PIPE_BULK: /* one TD for every 4096 Byte */
541 size = (transfer_len - 1) / 4096 + 1;
543 case PIPE_CONTROL:/* 1 TD for setup, 1 for ACK and 1 for every 4096 B */
544 size = (transfer_len == 0)? 2:
545 (transfer_len - 1) / 4096 + 3;
547 case PIPE_INTERRUPT: /* 1 TD */
554 if (size >= (N_URB_TD - 1)) {
555 err("need %d TDs, only have %d", size, N_URB_TD);
558 purb_priv->pipe = pipe;
560 /* fill the private part of the URB */
561 purb_priv->length = size;
563 purb_priv->actual_length = 0;
565 /* allocate the TDs */
566 /* note that td[0] was allocated in ep_add_ed */
567 for (i = 0; i < size; i++) {
568 purb_priv->td[i] = td_alloc(ohci_dev, dev);
569 if (!purb_priv->td[i]) {
570 purb_priv->length = i;
571 urb_free_priv(purb_priv);
572 err("sohci_submit_job: ENOMEM");
577 if (ed->state == ED_NEW || (ed->state & ED_DEL)) {
578 urb_free_priv(purb_priv);
579 err("sohci_submit_job: EINVAL");
583 /* link the ed into a chain if is not already */
584 if (ed->state != ED_OPER)
587 /* fill the TDs and link it to the ed */
588 td_submit_job(ohci, dev, pipe, buffer, transfer_len,
589 setup, purb_priv, interval);
594 /*-------------------------------------------------------------------------*/
597 /* tell us the current USB frame number */
598 static int sohci_get_current_frame_number(ohci_t *ohci)
600 invalidate_dcache_hcca(ohci->hcca);
601 return m16_swap(ohci->hcca->frame_no);
605 /*-------------------------------------------------------------------------*
606 * ED handling functions
607 *-------------------------------------------------------------------------*/
609 /* search for the right branch to insert an interrupt ed into the int tree
610 * do some load ballancing;
611 * returns the branch and
612 * sets the interval to interval = 2^integer (ld (interval)) */
614 static int ep_int_ballance(ohci_t *ohci, int interval, int load)
618 /* search for the least loaded interrupt endpoint
619 * branch of all 32 branches
621 for (i = 0; i < 32; i++)
622 if (ohci->ohci_int_load [branch] > ohci->ohci_int_load [i])
625 branch = branch % interval;
626 for (i = branch; i < 32; i += interval)
627 ohci->ohci_int_load [i] += load;
632 /*-------------------------------------------------------------------------*/
634 /* 2^int( ld (inter)) */
636 static int ep_2_n_interval(int inter)
639 for (i = 0; ((inter >> i) > 1) && (i < 5); i++);
643 /*-------------------------------------------------------------------------*/
645 /* the int tree is a binary tree
646 * in order to process it sequentially the indexes of the branches have to
647 * be mapped the mapping reverses the bits of a word of num_bits length */
648 static int ep_rev(int num_bits, int word)
652 for (i = 0; i < num_bits; i++)
653 wout |= (((word >> i) & 1) << (num_bits - i - 1));
657 /*-------------------------------------------------------------------------*
658 * ED handling functions
659 *-------------------------------------------------------------------------*/
661 /* link an ed into one of the HC chains */
663 static int ep_link(ohci_t *ohci, ed_t *edi)
665 volatile ed_t *ed = edi;
674 ed->int_interval = 0;
680 if (ohci->ed_controltail == NULL)
681 ohci_writel((uintptr_t)ed, &ohci->regs->ed_controlhead);
683 ohci->ed_controltail->hwNextED =
684 m32_swap((unsigned long)ed);
686 ed->ed_prev = ohci->ed_controltail;
687 if (!ohci->ed_controltail && !ohci->ed_rm_list[0] &&
688 !ohci->ed_rm_list[1] && !ohci->sleeping) {
689 ohci->hc_control |= OHCI_CTRL_CLE;
690 ohci_writel(ohci->hc_control, &ohci->regs->control);
692 ohci->ed_controltail = edi;
698 if (ohci->ed_bulktail == NULL)
699 ohci_writel((uintptr_t)ed, &ohci->regs->ed_bulkhead);
701 ohci->ed_bulktail->hwNextED =
702 m32_swap((unsigned long)ed);
704 ed->ed_prev = ohci->ed_bulktail;
705 if (!ohci->ed_bulktail && !ohci->ed_rm_list[0] &&
706 !ohci->ed_rm_list[1] && !ohci->sleeping) {
707 ohci->hc_control |= OHCI_CTRL_BLE;
708 ohci_writel(ohci->hc_control, &ohci->regs->control);
710 ohci->ed_bulktail = edi;
715 interval = ep_2_n_interval(ed->int_period);
716 ed->int_interval = interval;
717 int_branch = ep_int_ballance(ohci, interval, load);
718 ed->int_branch = int_branch;
720 for (i = 0; i < ep_rev(6, interval); i += inter) {
722 for (ed_p = &(ohci->hcca->int_table[\
723 ep_rev(5, i) + int_branch]);
725 (((ed_t *)ed_p)->int_interval >= interval);
726 ed_p = &(((ed_t *)ed_p)->hwNextED))
728 ((ed_t *)ed_p)->int_interval);
729 ed->hwNextED = *ed_p;
731 *ed_p = m32_swap((unsigned long)ed);
732 flush_dcache_hcca(ohci->hcca);
739 /*-------------------------------------------------------------------------*/
741 /* scan the periodic table to find and unlink this ED */
742 static void periodic_unlink(struct ohci *ohci, volatile struct ed *ed,
743 unsigned index, unsigned period)
745 __maybe_unused unsigned long aligned_ed_p;
747 for (; index < NUM_INTS; index += period) {
748 __u32 *ed_p = &ohci->hcca->int_table [index];
750 /* ED might have been unlinked through another path */
752 if (((struct ed *)(uintptr_t)
753 m32_swap((unsigned long)ed_p)) == ed) {
754 *ed_p = ed->hwNextED;
755 aligned_ed_p = (unsigned long)ed_p;
756 aligned_ed_p &= ~(ARCH_DMA_MINALIGN - 1);
757 flush_dcache_range(aligned_ed_p,
758 aligned_ed_p + ARCH_DMA_MINALIGN);
761 ed_p = &(((struct ed *)(uintptr_t)
762 m32_swap((unsigned long)ed_p))->hwNextED);
767 /* unlink an ed from one of the HC chains.
768 * just the link to the ed is unlinked.
769 * the link from the ed still points to another operational ed or 0
770 * so the HC can eventually finish the processing of the unlinked ed */
772 static int ep_unlink(ohci_t *ohci, ed_t *edi)
774 volatile ed_t *ed = edi;
777 ed->hwINFO |= m32_swap(OHCI_ED_SKIP);
782 if (ed->ed_prev == NULL) {
784 ohci->hc_control &= ~OHCI_CTRL_CLE;
785 ohci_writel(ohci->hc_control,
786 &ohci->regs->control);
788 ohci_writel(m32_swap(*((__u32 *)&ed->hwNextED)),
789 &ohci->regs->ed_controlhead);
791 ed->ed_prev->hwNextED = ed->hwNextED;
792 flush_dcache_ed(ed->ed_prev);
794 if (ohci->ed_controltail == ed) {
795 ohci->ed_controltail = ed->ed_prev;
797 ((ed_t *)(uintptr_t)m32_swap(
798 *((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
803 if (ed->ed_prev == NULL) {
805 ohci->hc_control &= ~OHCI_CTRL_BLE;
806 ohci_writel(ohci->hc_control,
807 &ohci->regs->control);
809 ohci_writel(m32_swap(*((__u32 *)&ed->hwNextED)),
810 &ohci->regs->ed_bulkhead);
812 ed->ed_prev->hwNextED = ed->hwNextED;
813 flush_dcache_ed(ed->ed_prev);
815 if (ohci->ed_bulktail == ed) {
816 ohci->ed_bulktail = ed->ed_prev;
818 ((ed_t *)(uintptr_t)m32_swap(
819 *((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
824 periodic_unlink(ohci, ed, 0, 1);
825 for (i = ed->int_branch; i < 32; i += ed->int_interval)
826 ohci->ohci_int_load[i] -= ed->int_load;
829 ed->state = ED_UNLINK;
833 /*-------------------------------------------------------------------------*/
835 /* add/reinit an endpoint; this should be done once at the
836 * usb_set_configuration command, but the USB stack is a little bit
837 * stateless so we do it at every transaction if the state of the ed
838 * is ED_NEW then a dummy td is added and the state is changed to
839 * ED_UNLINK in all other cases the state is left unchanged the ed
840 * info fields are setted anyway even though most of them should not
843 static ed_t *ep_add_ed(ohci_dev_t *ohci_dev, struct usb_device *usb_dev,
844 unsigned long pipe, int interval, int load)
850 ed = ed_ret = &ohci_dev->ed[(usb_pipeendpoint(pipe) << 1) |
851 (usb_pipecontrol(pipe)? 0: usb_pipeout(pipe))];
853 if ((ed->state & ED_DEL) || (ed->state & ED_URB_DEL)) {
854 err("ep_add_ed: pending delete");
855 /* pending delete request */
859 if (ed->state == ED_NEW) {
860 /* dummy td; end of td list for ed */
861 td = td_alloc(ohci_dev, usb_dev);
862 ed->hwTailP = m32_swap((unsigned long)td);
863 ed->hwHeadP = ed->hwTailP;
864 ed->state = ED_UNLINK;
865 ed->type = usb_pipetype(pipe);
869 ed->hwINFO = m32_swap(usb_pipedevice(pipe)
870 | usb_pipeendpoint(pipe) << 7
871 | (usb_pipeisoc(pipe)? 0x8000: 0)
872 | (usb_pipecontrol(pipe)? 0: \
873 (usb_pipeout(pipe)? 0x800: 0x1000))
874 | (usb_dev->speed == USB_SPEED_LOW) << 13
875 | usb_maxpacket(usb_dev, pipe) << 16);
877 if (ed->type == PIPE_INTERRUPT && ed->state == ED_UNLINK) {
878 ed->int_period = interval;
887 /*-------------------------------------------------------------------------*
888 * TD handling functions
889 *-------------------------------------------------------------------------*/
891 /* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
893 static void td_fill(ohci_t *ohci, unsigned int info,
895 struct usb_device *dev, int index, urb_priv_t *urb_priv)
897 volatile td_t *td, *td_pt;
898 #ifdef OHCI_FILL_TRACE
902 if (index > urb_priv->length) {
903 err("index > length");
906 /* use this td as the next dummy */
907 td_pt = urb_priv->td [index];
909 flush_dcache_td(td_pt);
911 /* fill the old dummy TD */
912 td = urb_priv->td [index] =
914 (m32_swap(urb_priv->ed->hwTailP) & ~0xf);
916 td->ed = urb_priv->ed;
917 td->next_dl_td = NULL;
919 td->data = (uintptr_t)data;
920 #ifdef OHCI_FILL_TRACE
921 if (usb_pipebulk(urb_priv->pipe) && usb_pipeout(urb_priv->pipe)) {
922 for (i = 0; i < len; i++)
923 printf("td->data[%d] %#2x ", i, ((unsigned char *)td->data)[i]);
930 td->hwINFO = m32_swap(info);
931 td->hwCBP = m32_swap((unsigned long)data);
933 td->hwBE = m32_swap((unsigned long)(data + len - 1));
937 td->hwNextTD = m32_swap((unsigned long)td_pt);
940 /* append to queue */
941 td->ed->hwTailP = td->hwNextTD;
942 flush_dcache_ed(td->ed);
945 /*-------------------------------------------------------------------------*/
947 /* prepare all TDs of a transfer */
949 static void td_submit_job(ohci_t *ohci, struct usb_device *dev,
950 unsigned long pipe, void *buffer, int transfer_len,
951 struct devrequest *setup, urb_priv_t *urb,
954 int data_len = transfer_len;
958 unsigned int toggle = 0;
960 flush_dcache_buffer(buffer, data_len);
962 /* OHCI handles the DATA-toggles itself, we just use the USB-toggle
963 * bits for resetting */
964 if (usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe))) {
965 toggle = TD_T_TOGGLE;
968 usb_settoggle(dev, usb_pipeendpoint(pipe),
969 usb_pipeout(pipe), 1);
977 switch (usb_pipetype(pipe)) {
979 info = usb_pipeout(pipe)?
980 TD_CC | TD_DP_OUT : TD_CC | TD_DP_IN ;
981 while (data_len > 4096) {
982 td_fill(ohci, info | (cnt? TD_T_TOGGLE:toggle),
983 data, 4096, dev, cnt, urb);
984 data += 4096; data_len -= 4096; cnt++;
986 info = usb_pipeout(pipe)?
987 TD_CC | TD_DP_OUT : TD_CC | TD_R | TD_DP_IN ;
988 td_fill(ohci, info | (cnt? TD_T_TOGGLE:toggle), data,
989 data_len, dev, cnt, urb);
992 if (!ohci->sleeping) {
993 /* start bulk list */
994 ohci_writel(OHCI_BLF, &ohci->regs->cmdstatus);
1000 info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
1001 flush_dcache_buffer(setup, 8);
1002 td_fill(ohci, info, setup, 8, dev, cnt++, urb);
1004 /* Optional Data phase */
1006 info = usb_pipeout(pipe)?
1007 TD_CC | TD_R | TD_DP_OUT | TD_T_DATA1 :
1008 TD_CC | TD_R | TD_DP_IN | TD_T_DATA1;
1009 /* NOTE: mishandles transfers >8K, some >4K */
1010 td_fill(ohci, info, data, data_len, dev, cnt++, urb);
1014 info = (usb_pipeout(pipe) || data_len == 0) ?
1015 TD_CC | TD_DP_IN | TD_T_DATA1:
1016 TD_CC | TD_DP_OUT | TD_T_DATA1;
1017 td_fill(ohci, info, data, 0, dev, cnt++, urb);
1019 if (!ohci->sleeping) {
1020 /* start Control list */
1021 ohci_writel(OHCI_CLF, &ohci->regs->cmdstatus);
1025 case PIPE_INTERRUPT:
1026 info = usb_pipeout(urb->pipe)?
1027 TD_CC | TD_DP_OUT | toggle:
1028 TD_CC | TD_R | TD_DP_IN | toggle;
1029 td_fill(ohci, info, data, data_len, dev, cnt++, urb);
1032 if (urb->length != cnt)
1033 dbg("TD LENGTH %d != CNT %d", urb->length, cnt);
1036 /*-------------------------------------------------------------------------*
1037 * Done List handling functions
1038 *-------------------------------------------------------------------------*/
1040 /* calculate the transfer length and update the urb */
1042 static void dl_transfer_length(td_t *td)
1045 urb_priv_t *lurb_priv = td->ed->purb;
1047 tdBE = m32_swap(td->hwBE);
1048 tdCBP = m32_swap(td->hwCBP);
1050 if (!(usb_pipecontrol(lurb_priv->pipe) &&
1051 ((td->index == 0) || (td->index == lurb_priv->length - 1)))) {
1054 lurb_priv->actual_length += tdBE - td->data + 1;
1056 lurb_priv->actual_length += tdCBP - td->data;
1061 /*-------------------------------------------------------------------------*/
1062 static void check_status(td_t *td_list)
1064 urb_priv_t *lurb_priv = td_list->ed->purb;
1065 int urb_len = lurb_priv->length;
1066 __u32 *phwHeadP = &td_list->ed->hwHeadP;
1069 cc = TD_CC_GET(m32_swap(td_list->hwINFO));
1071 err(" USB-error: %s (%x)", cc_to_string[cc], cc);
1073 invalidate_dcache_ed(td_list->ed);
1074 if (*phwHeadP & m32_swap(0x1)) {
1076 ((td_list->index + 1) < urb_len)) {
1078 (lurb_priv->td[urb_len - 1]->hwNextTD &\
1079 m32_swap(0xfffffff0)) |
1080 (*phwHeadP & m32_swap(0x2));
1082 lurb_priv->td_cnt += urb_len -
1085 *phwHeadP &= m32_swap(0xfffffff2);
1086 flush_dcache_ed(td_list->ed);
1091 /* replies to the request have to be on a FIFO basis so
1092 * we reverse the reversed done-list */
1093 static td_t *dl_reverse_done_list(ohci_t *ohci)
1095 uintptr_t td_list_hc;
1096 td_t *td_rev = NULL;
1097 td_t *td_list = NULL;
1099 invalidate_dcache_hcca(ohci->hcca);
1100 td_list_hc = m32_swap(ohci->hcca->done_head) & 0xfffffff0;
1101 ohci->hcca->done_head = 0;
1102 flush_dcache_hcca(ohci->hcca);
1104 while (td_list_hc) {
1105 td_list = (td_t *)td_list_hc;
1106 invalidate_dcache_td(td_list);
1107 check_status(td_list);
1108 td_list->next_dl_td = td_rev;
1110 td_list_hc = m32_swap(td_list->hwNextTD) & 0xfffffff0;
1115 /*-------------------------------------------------------------------------*/
1116 /*-------------------------------------------------------------------------*/
1118 static void finish_urb(ohci_t *ohci, urb_priv_t *urb, int status)
1120 if ((status & (ED_OPER | ED_UNLINK)) && (urb->state != URB_DEL))
1123 dbg("finish_urb: strange.., ED state %x, \n", status);
1127 * Used to take back a TD from the host controller. This would normally be
1128 * called from within dl_done_list, however it may be called directly if the
1129 * HC no longer sees the TD and it has not appeared on the donelist (after
1130 * two frames). This bug has been observed on ZF Micro systems.
1132 static int takeback_td(ohci_t *ohci, td_t *td_list)
1138 urb_priv_t *lurb_priv;
1139 __u32 tdINFO, edHeadP, edTailP;
1141 invalidate_dcache_td(td_list);
1142 tdINFO = m32_swap(td_list->hwINFO);
1145 lurb_priv = ed->purb;
1147 dl_transfer_length(td_list);
1149 lurb_priv->td_cnt++;
1151 /* error code of transfer */
1152 cc = TD_CC_GET(tdINFO);
1154 err("USB-error: %s (%x)", cc_to_string[cc], cc);
1155 stat = cc_to_error[cc];
1158 /* see if this done list makes for all TD's of current URB,
1159 * and mark the URB finished if so */
1160 if (lurb_priv->td_cnt == lurb_priv->length)
1161 finish_urb(ohci, lurb_priv, ed->state);
1163 dbg("dl_done_list: processing TD %x, len %x\n",
1164 lurb_priv->td_cnt, lurb_priv->length);
1166 if (ed->state != ED_NEW && (!usb_pipeint(lurb_priv->pipe))) {
1167 invalidate_dcache_ed(ed);
1168 edHeadP = m32_swap(ed->hwHeadP) & 0xfffffff0;
1169 edTailP = m32_swap(ed->hwTailP);
1171 /* unlink eds if they are not busy */
1172 if ((edHeadP == edTailP) && (ed->state == ED_OPER))
1173 ep_unlink(ohci, ed);
1178 static int dl_done_list(ohci_t *ohci)
1181 td_t *td_list = dl_reverse_done_list(ohci);
1184 td_t *td_next = td_list->next_dl_td;
1185 stat = takeback_td(ohci, td_list);
1191 /*-------------------------------------------------------------------------*
1193 *-------------------------------------------------------------------------*/
1195 #include <usbroothubdes.h>
1197 /* Hub class-specific descriptor is constructed dynamically */
1199 /*-------------------------------------------------------------------------*/
1201 #define OK(x) len = (x); break
1203 #define WR_RH_STAT(x) {info("WR:status %#8x", (x)); ohci_writel((x), \
1204 &ohci->regs->roothub.status); }
1205 #define WR_RH_PORTSTAT(x) {info("WR:portstatus[%d] %#8x", wIndex-1, \
1206 (x)); ohci_writel((x), &ohci->regs->roothub.portstatus[wIndex-1]); }
1208 #define WR_RH_STAT(x) ohci_writel((x), &ohci->regs->roothub.status)
1209 #define WR_RH_PORTSTAT(x) ohci_writel((x), \
1210 &ohci->regs->roothub.portstatus[wIndex-1])
1212 #define RD_RH_STAT roothub_status(ohci)
1213 #define RD_RH_PORTSTAT roothub_portstatus(ohci, wIndex-1)
1215 /* request to virtual root hub */
1217 int rh_check_port_status(ohci_t *controller)
1223 temp = roothub_a(controller);
1224 ndp = (temp & RH_A_NDP);
1225 #ifdef CONFIG_AT91C_PQFP_UHPBUG
1226 ndp = (ndp == 2) ? 1:0;
1228 for (i = 0; i < ndp; i++) {
1229 temp = roothub_portstatus(controller, i);
1230 /* check for a device disconnect */
1231 if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
1232 (RH_PS_PESC | RH_PS_CSC)) &&
1233 ((temp & RH_PS_CCS) == 0)) {
1241 static int ohci_submit_rh_msg(ohci_t *ohci, struct usb_device *dev,
1242 unsigned long pipe, void *buffer, int transfer_len,
1243 struct devrequest *cmd)
1245 void *data = buffer;
1246 int leni = transfer_len;
1253 ALLOC_ALIGN_BUFFER(__u8, databuf, 16, sizeof(u32));
1256 pkt_print(ohci, NULL, dev, pipe, buffer, transfer_len,
1257 cmd, "SUB(rh)", usb_pipein(pipe));
1261 if (usb_pipeint(pipe)) {
1262 info("Root-Hub submit IRQ: NOT implemented");
1266 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
1267 wValue = le16_to_cpu(cmd->value);
1268 wIndex = le16_to_cpu(cmd->index);
1269 wLength = le16_to_cpu(cmd->length);
1271 info("Root-Hub: adr: %2x cmd(%1x): %08x %04x %04x %04x",
1272 dev->devnum, 8, bmRType_bReq, wValue, wIndex, wLength);
1274 switch (bmRType_bReq) {
1275 /* Request Destination:
1276 without flags: Device,
1277 RH_INTERFACE: interface,
1278 RH_ENDPOINT: endpoint,
1279 RH_CLASS means HUB here,
1280 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1284 *(u16 *)databuf = cpu_to_le16(1);
1286 case RH_GET_STATUS | RH_INTERFACE:
1287 *(u16 *)databuf = cpu_to_le16(0);
1289 case RH_GET_STATUS | RH_ENDPOINT:
1290 *(u16 *)databuf = cpu_to_le16(0);
1292 case RH_GET_STATUS | RH_CLASS:
1293 *(u32 *)databuf = cpu_to_le32(
1294 RD_RH_STAT & ~(RH_HS_CRWE | RH_HS_DRWE));
1296 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
1297 *(u32 *)databuf = cpu_to_le32(RD_RH_PORTSTAT);
1300 case RH_CLEAR_FEATURE | RH_ENDPOINT:
1302 case (RH_ENDPOINT_STALL):
1307 case RH_CLEAR_FEATURE | RH_CLASS:
1309 case RH_C_HUB_LOCAL_POWER:
1311 case (RH_C_HUB_OVER_CURRENT):
1312 WR_RH_STAT(RH_HS_OCIC);
1317 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1319 case (RH_PORT_ENABLE): WR_RH_PORTSTAT(RH_PS_CCS); OK(0);
1320 case (RH_PORT_SUSPEND): WR_RH_PORTSTAT(RH_PS_POCI); OK(0);
1321 case (RH_PORT_POWER): WR_RH_PORTSTAT(RH_PS_LSDA); OK(0);
1322 case (RH_C_PORT_CONNECTION): WR_RH_PORTSTAT(RH_PS_CSC); OK(0);
1323 case (RH_C_PORT_ENABLE): WR_RH_PORTSTAT(RH_PS_PESC); OK(0);
1324 case (RH_C_PORT_SUSPEND): WR_RH_PORTSTAT(RH_PS_PSSC); OK(0);
1325 case (RH_C_PORT_OVER_CURRENT):WR_RH_PORTSTAT(RH_PS_OCIC); OK(0);
1326 case (RH_C_PORT_RESET): WR_RH_PORTSTAT(RH_PS_PRSC); OK(0);
1330 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1332 case (RH_PORT_SUSPEND):
1333 WR_RH_PORTSTAT(RH_PS_PSS); OK(0);
1334 case (RH_PORT_RESET): /* BUG IN HUP CODE *********/
1335 if (RD_RH_PORTSTAT & RH_PS_CCS)
1336 WR_RH_PORTSTAT(RH_PS_PRS);
1338 case (RH_PORT_POWER):
1339 WR_RH_PORTSTAT(RH_PS_PPS);
1341 case (RH_PORT_ENABLE): /* BUG IN HUP CODE *********/
1342 if (RD_RH_PORTSTAT & RH_PS_CCS)
1343 WR_RH_PORTSTAT(RH_PS_PES);
1348 case RH_SET_ADDRESS:
1349 ohci->rh.devnum = wValue;
1352 case RH_GET_DESCRIPTOR:
1353 switch ((wValue & 0xff00) >> 8) {
1354 case (0x01): /* device descriptor */
1355 len = min_t(unsigned int,
1358 sizeof(root_hub_dev_des),
1360 databuf = root_hub_dev_des; OK(len);
1361 case (0x02): /* configuration descriptor */
1362 len = min_t(unsigned int,
1365 sizeof(root_hub_config_des),
1367 databuf = root_hub_config_des; OK(len);
1368 case (0x03): /* string descriptors */
1369 if (wValue == 0x0300) {
1370 len = min_t(unsigned int,
1373 sizeof(root_hub_str_index0),
1375 databuf = root_hub_str_index0;
1378 if (wValue == 0x0301) {
1379 len = min_t(unsigned int,
1382 sizeof(root_hub_str_index1),
1384 databuf = root_hub_str_index1;
1388 stat = USB_ST_STALLED;
1392 case RH_GET_DESCRIPTOR | RH_CLASS:
1394 __u32 temp = roothub_a(ohci);
1396 databuf[0] = 9; /* min length; */
1398 databuf[2] = temp & RH_A_NDP;
1399 #ifdef CONFIG_AT91C_PQFP_UHPBUG
1400 databuf[2] = (databuf[2] == 2) ? 1 : 0;
1403 if (temp & RH_A_PSM) /* per-port power switching? */
1405 if (temp & RH_A_NOCP) /* no overcurrent reporting? */
1407 else if (temp & RH_A_OCPM)/* per-port overcurrent reporting? */
1411 databuf[5] = (temp & RH_A_POTPGT) >> 24;
1413 temp = roothub_b(ohci);
1414 databuf[7] = temp & RH_B_DR;
1415 if (databuf[2] < 7) {
1419 databuf[8] = (temp & RH_B_DR) >> 8;
1420 databuf[10] = databuf[9] = 0xff;
1423 len = min_t(unsigned int, leni,
1424 min_t(unsigned int, databuf[0], wLength));
1428 case RH_GET_CONFIGURATION:
1432 case RH_SET_CONFIGURATION:
1433 WR_RH_STAT(0x10000);
1437 dbg("unsupported root hub command");
1438 stat = USB_ST_STALLED;
1442 ohci_dump_roothub(ohci, 1);
1447 len = min_t(int, len, leni);
1448 if (data != databuf)
1449 memcpy(data, databuf, len);
1454 pkt_print(ohci, NULL, dev, pipe, buffer,
1455 transfer_len, cmd, "RET(rh)", 0/*usb_pipein(pipe)*/);
1463 /*-------------------------------------------------------------------------*/
1465 static ohci_dev_t *ohci_get_ohci_dev(ohci_t *ohci, int devnum, int intr)
1470 return &ohci->ohci_dev;
1472 /* First see if we already have an ohci_dev for this dev. */
1473 for (i = 0; i < NUM_INT_DEVS; i++) {
1474 if (ohci->int_dev[i].devnum == devnum)
1475 return &ohci->int_dev[i];
1478 /* If not then find a free one. */
1479 for (i = 0; i < NUM_INT_DEVS; i++) {
1480 if (ohci->int_dev[i].devnum == -1) {
1481 ohci->int_dev[i].devnum = devnum;
1482 return &ohci->int_dev[i];
1486 printf("ohci: Error out of ohci_devs for interrupt endpoints\n");
1490 /* common code for handling submit messages - used for all but root hub */
1492 static urb_priv_t *ohci_alloc_urb(struct usb_device *dev, unsigned long pipe,
1493 void *buffer, int transfer_len, int interval)
1497 urb = calloc(1, sizeof(urb_priv_t));
1499 printf("ohci: Error out of memory allocating urb\n");
1505 urb->transfer_buffer = buffer;
1506 urb->transfer_buffer_length = transfer_len;
1507 urb->interval = interval;
1512 static int submit_common_msg(ohci_t *ohci, struct usb_device *dev,
1513 unsigned long pipe, void *buffer, int transfer_len,
1514 struct devrequest *setup, int interval)
1517 int maxsize = usb_maxpacket(dev, pipe);
1520 ohci_dev_t *ohci_dev;
1522 urb = ohci_alloc_urb(dev, pipe, buffer, transfer_len, interval);
1527 urb->actual_length = 0;
1528 pkt_print(ohci, urb, dev, pipe, buffer, transfer_len,
1529 setup, "SUB", usb_pipein(pipe));
1534 err("submit_common_message: pipesize for pipe %lx is zero",
1539 ohci_dev = ohci_get_ohci_dev(ohci, dev->devnum, usb_pipeint(pipe));
1543 if (sohci_submit_job(ohci, ohci_dev, urb, setup) < 0) {
1544 err("sohci_submit_job failed");
1549 /* ohci_dump_status(ohci); */
1551 timeout = USB_TIMEOUT_MS(pipe);
1553 /* wait for it to complete */
1555 /* check whether the controller is done */
1556 stat = hc_interrupt(ohci);
1558 stat = USB_ST_CRC_ERR;
1562 /* NOTE: since we are not interrupt driven in U-Boot and always
1563 * handle only one URB at a time, we cannot assume the
1564 * transaction finished on the first successful return from
1565 * hc_interrupt().. unless the flag for current URB is set,
1566 * meaning that all TD's to/from device got actually
1567 * transferred and processed. If the current URB is not
1568 * finished we need to re-iterate this loop so as
1569 * hc_interrupt() gets called again as there needs to be some
1570 * more TD's to process still */
1571 if ((stat >= 0) && (stat != 0xff) && (urb->finished)) {
1572 /* 0xff is returned for an SF-interrupt */
1582 if (!usb_pipeint(pipe))
1583 err("CTL:TIMEOUT ");
1584 dbg("submit_common_msg: TO status %x\n", stat);
1586 stat = USB_ST_CRC_ERR;
1592 dev->act_len = urb->actual_length;
1594 if (usb_pipein(pipe) && dev->status == 0 && dev->act_len)
1595 invalidate_dcache_buffer(buffer, dev->act_len);
1598 pkt_print(ohci, urb, dev, pipe, buffer, transfer_len,
1599 setup, "RET(ctlr)", usb_pipein(pipe));
1607 #define MAX_INT_QUEUESIZE 8
1612 urb_priv_t *urb[MAX_INT_QUEUESIZE];
1615 static struct int_queue *_ohci_create_int_queue(ohci_t *ohci,
1616 struct usb_device *udev, unsigned long pipe, int queuesize,
1617 int elementsize, void *buffer, int interval)
1619 struct int_queue *queue;
1620 ohci_dev_t *ohci_dev;
1623 if (queuesize > MAX_INT_QUEUESIZE)
1626 ohci_dev = ohci_get_ohci_dev(ohci, udev->devnum, 1);
1630 queue = malloc(sizeof(*queue));
1632 printf("ohci: Error out of memory allocating int queue\n");
1636 for (i = 0; i < queuesize; i++) {
1637 queue->urb[i] = ohci_alloc_urb(udev, pipe,
1638 buffer + i * elementsize,
1639 elementsize, interval);
1643 if (sohci_submit_job(ohci, ohci_dev, queue->urb[i], NULL)) {
1644 printf("ohci: Error submitting int queue job\n");
1645 urb_free_priv(queue->urb[i]);
1650 /* We did not succeed in submitting even 1 urb */
1655 queue->queuesize = i;
1656 queue->curr_urb = 0;
1661 static void *_ohci_poll_int_queue(ohci_t *ohci, struct usb_device *udev,
1662 struct int_queue *queue)
1664 if (queue->curr_urb == queue->queuesize)
1665 return NULL; /* Queue depleted */
1667 if (hc_interrupt(ohci) < 0)
1670 if (queue->urb[queue->curr_urb]->finished) {
1671 void *ret = queue->urb[queue->curr_urb]->transfer_buffer;
1679 static int _ohci_destroy_int_queue(ohci_t *ohci, struct usb_device *dev,
1680 struct int_queue *queue)
1684 for (i = 0; i < queue->queuesize; i++)
1685 urb_free_priv(queue->urb[i]);
1692 #if !CONFIG_IS_ENABLED(DM_USB)
1693 /* submit routines called from usb.c */
1694 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1697 info("submit_bulk_msg");
1698 return submit_common_msg(&gohci, dev, pipe, buffer, transfer_len,
1702 int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1703 int transfer_len, int interval)
1705 info("submit_int_msg");
1706 return submit_common_msg(&gohci, dev, pipe, buffer, transfer_len, NULL,
1710 struct int_queue *create_int_queue(struct usb_device *dev,
1711 unsigned long pipe, int queuesize, int elementsize,
1712 void *buffer, int interval)
1714 return _ohci_create_int_queue(&gohci, dev, pipe, queuesize,
1715 elementsize, buffer, interval);
1718 void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
1720 return _ohci_poll_int_queue(&gohci, dev, queue);
1723 int destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
1725 return _ohci_destroy_int_queue(&gohci, dev, queue);
1729 static int _ohci_submit_control_msg(ohci_t *ohci, struct usb_device *dev,
1730 unsigned long pipe, void *buffer, int transfer_len,
1731 struct devrequest *setup)
1733 int maxsize = usb_maxpacket(dev, pipe);
1735 info("submit_control_msg");
1737 pkt_print(ohci, NULL, dev, pipe, buffer, transfer_len,
1738 setup, "SUB", usb_pipein(pipe));
1743 err("submit_control_message: pipesize for pipe %lx is zero",
1747 if (((pipe >> 8) & 0x7f) == ohci->rh.devnum) {
1749 /* root hub - redirect */
1750 return ohci_submit_rh_msg(ohci, dev, pipe, buffer,
1751 transfer_len, setup);
1754 return submit_common_msg(ohci, dev, pipe, buffer, transfer_len,
1758 /*-------------------------------------------------------------------------*
1760 *-------------------------------------------------------------------------*/
1762 /* reset the HC and BUS */
1764 static int hc_reset(ohci_t *ohci)
1766 #ifdef CONFIG_PCI_EHCI_DEVNO
1770 int smm_timeout = 50; /* 0,5 sec */
1772 dbg("%s\n", __FUNCTION__);
1774 #ifdef CONFIG_PCI_EHCI_DEVNO
1776 * Some multi-function controllers (e.g. ISP1562) allow root hub
1777 * resetting via EHCI registers only.
1779 pdev = pci_find_devices(ehci_pci_ids, CONFIG_PCI_EHCI_DEVNO);
1784 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &base);
1785 base += EHCI_USBCMD_OFF;
1786 ohci_writel(ohci_readl(base) | EHCI_USBCMD_HCRESET, base);
1788 while (ohci_readl(base) & EHCI_USBCMD_HCRESET) {
1789 if (timeout-- <= 0) {
1790 printf("USB RootHub reset timed out!");
1796 printf("No EHCI func at %d index!\n", CONFIG_PCI_EHCI_DEVNO);
1798 if (ohci_readl(&ohci->regs->control) & OHCI_CTRL_IR) {
1799 /* SMM owns the HC, request ownership */
1800 ohci_writel(OHCI_OCR, &ohci->regs->cmdstatus);
1801 info("USB HC TakeOver from SMM");
1802 while (ohci_readl(&ohci->regs->control) & OHCI_CTRL_IR) {
1804 if (--smm_timeout == 0) {
1805 err("USB HC TakeOver failed!");
1811 /* Disable HC interrupts */
1812 ohci_writel(OHCI_INTR_MIE, &ohci->regs->intrdisable);
1814 dbg("USB HC reset_hc usb-%s: ctrl = 0x%X ;\n",
1816 ohci_readl(&ohci->regs->control));
1818 /* Reset USB (needed by some controllers) */
1819 ohci->hc_control = 0;
1820 ohci_writel(ohci->hc_control, &ohci->regs->control);
1822 /* HC Reset requires max 10 us delay */
1823 ohci_writel(OHCI_HCR, &ohci->regs->cmdstatus);
1824 while ((ohci_readl(&ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
1825 if (--timeout == 0) {
1826 err("USB HC reset timed out!");
1834 /*-------------------------------------------------------------------------*/
1836 /* Start an OHCI controller, set the BUS operational
1838 * connect the virtual root hub */
1840 static int hc_start(ohci_t *ohci)
1843 unsigned int fminterval;
1847 for (i = 0; i < NUM_INT_DEVS; i++)
1848 ohci->int_dev[i].devnum = -1;
1850 /* Tell the controller where the control and bulk lists are
1851 * The lists are empty now. */
1853 ohci_writel(0, &ohci->regs->ed_controlhead);
1854 ohci_writel(0, &ohci->regs->ed_bulkhead);
1856 ohci_writel((uintptr_t)ohci->hcca,
1857 &ohci->regs->hcca); /* reset clears this */
1859 fminterval = 0x2edf;
1860 ohci_writel((fminterval * 9) / 10, &ohci->regs->periodicstart);
1861 fminterval |= ((((fminterval - 210) * 6) / 7) << 16);
1862 ohci_writel(fminterval, &ohci->regs->fminterval);
1863 ohci_writel(0x628, &ohci->regs->lsthresh);
1865 /* start controller operations */
1866 ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
1868 ohci_writel(ohci->hc_control, &ohci->regs->control);
1870 /* disable all interrupts */
1871 mask = (OHCI_INTR_SO | OHCI_INTR_WDH | OHCI_INTR_SF | OHCI_INTR_RD |
1872 OHCI_INTR_UE | OHCI_INTR_FNO | OHCI_INTR_RHSC |
1873 OHCI_INTR_OC | OHCI_INTR_MIE);
1874 ohci_writel(mask, &ohci->regs->intrdisable);
1875 /* clear all interrupts */
1876 mask &= ~OHCI_INTR_MIE;
1877 ohci_writel(mask, &ohci->regs->intrstatus);
1878 /* Choose the interrupts we care about now - but w/o MIE */
1879 mask = OHCI_INTR_RHSC | OHCI_INTR_UE | OHCI_INTR_WDH | OHCI_INTR_SO;
1880 ohci_writel(mask, &ohci->regs->intrenable);
1883 /* required for AMD-756 and some Mac platforms */
1884 ohci_writel((roothub_a(ohci) | RH_A_NPS) & ~RH_A_PSM,
1885 &ohci->regs->roothub.a);
1886 ohci_writel(RH_HS_LPSC, &ohci->regs->roothub.status);
1887 #endif /* OHCI_USE_NPS */
1889 /* connect the virtual root hub */
1890 ohci->rh.devnum = 0;
1895 /*-------------------------------------------------------------------------*/
1897 /* an interrupt happens */
1899 static int hc_interrupt(ohci_t *ohci)
1901 struct ohci_regs *regs = ohci->regs;
1905 invalidate_dcache_hcca(ohci->hcca);
1907 if ((ohci->hcca->done_head != 0) &&
1908 !(m32_swap(ohci->hcca->done_head) & 0x01)) {
1909 ints = OHCI_INTR_WDH;
1911 ints = ohci_readl(®s->intrstatus);
1912 if (ints == ~(u32)0) {
1914 err("%s device removed!", ohci->slot_name);
1917 ints &= ohci_readl(®s->intrenable);
1919 dbg("hc_interrupt: returning..\n");
1925 /* dbg("Interrupt: %x frame: %x", ints,
1926 le16_to_cpu(ohci->hcca->frame_no)); */
1928 if (ints & OHCI_INTR_RHSC)
1931 if (ints & OHCI_INTR_UE) {
1933 err("OHCI Unrecoverable Error, controller usb-%s disabled",
1935 /* e.g. due to PCI Master/Target Abort */
1942 /* FIXME: be optimistic, hope that bug won't repeat often. */
1943 /* Make some non-interrupt context restart the controller. */
1944 /* Count and limit the retries though; either hardware or */
1945 /* software errors can go forever... */
1950 if (ints & OHCI_INTR_WDH) {
1952 ohci_writel(OHCI_INTR_WDH, ®s->intrdisable);
1953 (void)ohci_readl(®s->intrdisable); /* flush */
1954 stat = dl_done_list(ohci);
1955 ohci_writel(OHCI_INTR_WDH, ®s->intrenable);
1956 (void)ohci_readl(®s->intrdisable); /* flush */
1959 if (ints & OHCI_INTR_SO) {
1960 dbg("USB Schedule overrun\n");
1961 ohci_writel(OHCI_INTR_SO, ®s->intrenable);
1965 /* FIXME: this assumes SOF (1/ms) interrupts don't get lost... */
1966 if (ints & OHCI_INTR_SF) {
1967 unsigned int frame = m16_swap(ohci->hcca->frame_no) & 1;
1969 ohci_writel(OHCI_INTR_SF, ®s->intrdisable);
1970 if (ohci->ed_rm_list[frame] != NULL)
1971 ohci_writel(OHCI_INTR_SF, ®s->intrenable);
1975 ohci_writel(ints, ®s->intrstatus);
1979 /*-------------------------------------------------------------------------*/
1981 #if !CONFIG_IS_ENABLED(DM_USB)
1983 /*-------------------------------------------------------------------------*/
1985 /* De-allocate all resources.. */
1987 static void hc_release_ohci(ohci_t *ohci)
1989 dbg("USB HC release ohci usb-%s", ohci->slot_name);
1991 if (!ohci->disabled)
1995 /*-------------------------------------------------------------------------*/
1998 * low level initalisation routine, called from usb.c
2000 static char ohci_inited = 0;
2002 int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
2004 #ifdef CONFIG_PCI_OHCI
2008 #ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
2009 /* cpu dependant init */
2014 #ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
2015 /* board dependant init */
2016 if (board_usb_init(index, USB_INIT_HOST))
2019 memset(&gohci, 0, sizeof(ohci_t));
2021 /* align the storage */
2022 if ((__u32)&ghcca[0] & 0xff) {
2023 err("HCCA not aligned!!");
2026 gohci.hcca = &ghcca[0];
2027 info("aligned ghcca %p", gohci.hcca);
2028 memset(gohci.hcca, 0, sizeof(struct ohci_hcca));
2033 #ifdef CONFIG_PCI_OHCI
2034 pdev = pci_find_devices(ohci_pci_ids, CONFIG_PCI_OHCI_DEVNO);
2039 pci_read_config_word(pdev, PCI_VENDOR_ID, &vid);
2040 pci_read_config_word(pdev, PCI_DEVICE_ID, &did);
2041 printf("OHCI pci controller (%04x, %04x) found @(%d:%d:%d)\n",
2042 vid, did, (pdev >> 16) & 0xff,
2043 (pdev >> 11) & 0x1f, (pdev >> 8) & 0x7);
2044 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &base);
2045 printf("OHCI regs address 0x%08x\n", base);
2046 gohci.regs = (struct ohci_regs *)base;
2050 gohci.regs = (struct ohci_regs *)CONFIG_SYS_USB_OHCI_REGS_BASE;
2054 gohci.slot_name = CONFIG_SYS_USB_OHCI_SLOT_NAME;
2056 if (hc_reset (&gohci) < 0) {
2057 hc_release_ohci (&gohci);
2058 err ("can't reset usb-%s", gohci.slot_name);
2059 #ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
2060 /* board dependant cleanup */
2061 board_usb_cleanup(index, USB_INIT_HOST);
2064 #ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
2065 /* cpu dependant cleanup */
2066 usb_cpu_init_fail();
2071 if (hc_start(&gohci) < 0) {
2072 err("can't start usb-%s", gohci.slot_name);
2073 hc_release_ohci(&gohci);
2074 /* Initialization failed */
2075 #ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
2076 /* board dependant cleanup */
2080 #ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
2081 /* cpu dependant cleanup */
2088 ohci_dump(&gohci, 1);
2096 int usb_lowlevel_stop(int index)
2098 /* this gets called really early - before the controller has */
2099 /* even been initialized! */
2102 /* TODO release any interrupts, etc. */
2103 /* call hc_release_ohci() here ? */
2106 #ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
2107 /* board dependant cleanup */
2108 if (usb_board_stop())
2112 #ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
2113 /* cpu dependant cleanup */
2117 /* This driver is no longer initialised. It needs a new low-level
2118 * init (board/cpu) before it can be used again. */
2123 int submit_control_msg(struct usb_device *dev, unsigned long pipe,
2124 void *buffer, int transfer_len, struct devrequest *setup)
2126 return _ohci_submit_control_msg(&gohci, dev, pipe, buffer,
2127 transfer_len, setup);
2131 #if CONFIG_IS_ENABLED(DM_USB)
2132 static int ohci_submit_control_msg(struct udevice *dev, struct usb_device *udev,
2133 unsigned long pipe, void *buffer, int length,
2134 struct devrequest *setup)
2136 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2138 return _ohci_submit_control_msg(ohci, udev, pipe, buffer,
2142 static int ohci_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
2143 unsigned long pipe, void *buffer, int length)
2145 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2147 return submit_common_msg(ohci, udev, pipe, buffer, length, NULL, 0);
2150 static int ohci_submit_int_msg(struct udevice *dev, struct usb_device *udev,
2151 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,
2160 static struct int_queue *ohci_create_int_queue(struct udevice *dev,
2161 struct usb_device *udev, unsigned long pipe, int queuesize,
2162 int elementsize, void *buffer, int interval)
2164 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2166 return _ohci_create_int_queue(ohci, udev, pipe, queuesize, elementsize,
2170 static void *ohci_poll_int_queue(struct udevice *dev, struct usb_device *udev,
2171 struct int_queue *queue)
2173 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2175 return _ohci_poll_int_queue(ohci, udev, queue);
2178 static int ohci_destroy_int_queue(struct udevice *dev, struct usb_device *udev,
2179 struct int_queue *queue)
2181 ohci_t *ohci = dev_get_priv(usb_get_bus(dev));
2183 return _ohci_destroy_int_queue(ohci, udev, queue);
2186 int ohci_register(struct udevice *dev, struct ohci_regs *regs)
2188 struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
2189 ohci_t *ohci = dev_get_priv(dev);
2192 priv->desc_before_addr = true;
2195 ohci->hcca = memalign(256, sizeof(struct ohci_hcca));
2198 memset(ohci->hcca, 0, sizeof(struct ohci_hcca));
2199 flush_dcache_hcca(ohci->hcca);
2201 if (hc_reset(ohci) < 0)
2204 if (hc_start(ohci) < 0)
2207 reg = ohci_readl(®s->revision);
2208 printf("USB OHCI %x.%x\n", (reg >> 4) & 0xf, reg & 0xf);
2213 int ohci_deregister(struct udevice *dev)
2215 ohci_t *ohci = dev_get_priv(dev);
2217 if (hc_reset(ohci) < 0)
2225 struct dm_usb_ops ohci_usb_ops = {
2226 .control = ohci_submit_control_msg,
2227 .bulk = ohci_submit_bulk_msg,
2228 .interrupt = ohci_submit_int_msg,
2229 .create_int_queue = ohci_create_int_queue,
2230 .poll_int_queue = ohci_poll_int_queue,
2231 .destroy_int_queue = ohci_destroy_int_queue,