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>
34 #if defined(CONFIG_PCI_OHCI)
36 #if !defined(CONFIG_PCI_OHCI_DEVNO)
37 #define CONFIG_PCI_OHCI_DEVNO 0
46 #ifdef CONFIG_AT91RM9200
47 #include <asm/arch/hardware.h> /* needed for AT91_USB_HOST_BASE */
50 #if defined(CONFIG_CPU_ARM920T) || \
51 defined(CONFIG_S3C24X0) || \
52 defined(CONFIG_440EP) || \
53 defined(CONFIG_PCI_OHCI) || \
54 defined(CONFIG_MPC5200) || \
55 defined(CONFIG_SYS_OHCI_USE_NPS)
56 # define OHCI_USE_NPS /* force NoPowerSwitching mode */
59 #undef OHCI_VERBOSE_DEBUG /* not always helpful */
62 #undef OHCI_FILL_TRACE
64 /* For initializing controller (mask in an HCFS mode too) */
65 #define OHCI_CONTROL_INIT \
66 (OHCI_CTRL_CBSR & 0x3) | OHCI_CTRL_IE | OHCI_CTRL_PLE
68 #ifdef CONFIG_PCI_OHCI
69 static struct pci_device_id ohci_pci_ids[] = {
70 {0x10b9, 0x5237}, /* ULI1575 PCI OHCI module ids */
71 {0x1033, 0x0035}, /* NEC PCI OHCI module ids */
72 {0x1131, 0x1561}, /* Philips 1561 PCI OHCI module ids */
73 /* Please add supported PCI OHCI controller ids here */
78 #ifdef CONFIG_PCI_EHCI_DEVNO
79 static struct pci_device_id ehci_pci_ids[] = {
80 {0x1131, 0x1562}, /* Philips 1562 PCI EHCI module ids */
81 /* Please add supported PCI EHCI controller ids here */
87 #define dbg(format, arg...) printf("DEBUG: " format "\n", ## arg)
89 #define dbg(format, arg...) do {} while (0)
91 #define err(format, arg...) printf("ERROR: " format "\n", ## arg)
93 #define info(format, arg...) printf("INFO: " format "\n", ## arg)
95 #define info(format, arg...) do {} while (0)
98 #ifdef CONFIG_SYS_OHCI_BE_CONTROLLER
99 # define m16_swap(x) cpu_to_be16(x)
100 # define m32_swap(x) cpu_to_be32(x)
102 # define m16_swap(x) cpu_to_le16(x)
103 # define m32_swap(x) cpu_to_le32(x)
104 #endif /* CONFIG_SYS_OHCI_BE_CONTROLLER */
108 * We really should do proper cache flushing everywhere, but for now we only
109 * do it for new (driver-model) usb code to avoid regressions.
111 #define flush_dcache_buffer(addr, size) \
112 flush_dcache_range((unsigned long)(addr), \
113 ALIGN((unsigned long)(addr) + size, ARCH_DMA_MINALIGN))
114 #define invalidate_dcache_buffer(addr, size) \
115 invalidate_dcache_range((unsigned long)(addr), \
116 ALIGN((unsigned long)(addr) + size, ARCH_DMA_MINALIGN))
118 #define flush_dcache_buffer(addr, size)
119 #define invalidate_dcache_buffer(addr, size)
122 /* Do not use sizeof(ed / td) as our ed / td structs contain extra members */
123 #define flush_dcache_ed(addr) flush_dcache_buffer(addr, 16)
124 #define flush_dcache_td(addr) flush_dcache_buffer(addr, 16)
125 #define flush_dcache_iso_td(addr) flush_dcache_buffer(addr, 32)
126 #define flush_dcache_hcca(addr) flush_dcache_buffer(addr, 256)
127 #define invalidate_dcache_ed(addr) invalidate_dcache_buffer(addr, 16)
128 #define invalidate_dcache_td(addr) invalidate_dcache_buffer(addr, 16)
129 #define invalidate_dcache_iso_td(addr) invalidate_dcache_buffer(addr, 32)
130 #define invalidate_dcache_hcca(addr) invalidate_dcache_buffer(addr, 256)
134 * The various ohci_mdelay(1) calls in the code seem unnecessary. We keep
135 * them around when building for older boards not yet converted to the dm
136 * just in case (to avoid regressions), for dm this turns them into nops.
138 #define ohci_mdelay(x)
140 #define ohci_mdelay(x) mdelay(x)
145 /* this must be aligned to a 256 byte boundary */
146 struct ohci_hcca ghcca[1];
148 /* mapping of the OHCI CC status to error codes */
149 static int cc_to_error[16] = {
151 /* CRC Error */ USB_ST_CRC_ERR,
152 /* Bit Stuff */ USB_ST_BIT_ERR,
153 /* Data Togg */ USB_ST_CRC_ERR,
154 /* Stall */ USB_ST_STALLED,
156 /* PIDCheck */ USB_ST_BIT_ERR,
157 /* UnExpPID */ USB_ST_BIT_ERR,
158 /* DataOver */ USB_ST_BUF_ERR,
159 /* DataUnder */ USB_ST_BUF_ERR,
162 /* BufferOver */ USB_ST_BUF_ERR,
163 /* BuffUnder */ USB_ST_BUF_ERR,
168 static const char *cc_to_string[16] = {
170 "CRC: Last data packet from endpoint contained a CRC error.",
171 "BITSTUFFING: Last data packet from endpoint contained a bit " \
172 "stuffing violation",
173 "DATATOGGLEMISMATCH: Last packet from endpoint had data toggle PID\n" \
174 "that did not match the expected value.",
175 "STALL: TD was moved to the Done Queue because the endpoint returned" \
177 "DEVICENOTRESPONDING: Device did not respond to token (IN) or did\n" \
178 "not provide a handshake (OUT)",
179 "PIDCHECKFAILURE: Check bits on PID from endpoint failed on data PID\n"\
180 "(IN) or handshake (OUT)",
181 "UNEXPECTEDPID: Receive PID was not valid when encountered or PID\n" \
182 "value is not defined.",
183 "DATAOVERRUN: The amount of data returned by the endpoint exceeded\n" \
184 "either the size of the maximum data packet allowed\n" \
185 "from the endpoint (found in MaximumPacketSize field\n" \
186 "of ED) or the remaining buffer size.",
187 "DATAUNDERRUN: The endpoint returned less than MaximumPacketSize\n" \
188 "and that amount was not sufficient to fill the\n" \
192 "BUFFEROVERRUN: During an IN, HC received data from endpoint faster\n" \
193 "than it could be written to system memory",
194 "BUFFERUNDERRUN: During an OUT, HC could not retrieve data from\n" \
195 "system memory fast enough to keep up with data USB " \
197 "NOT ACCESSED: This code is set by software before the TD is placed" \
198 "on a list to be processed by the HC.(1)",
199 "NOT ACCESSED: This code is set by software before the TD is placed" \
200 "on a list to be processed by the HC.(2)",
203 static inline u32 roothub_a(struct ohci *hc)
204 { return ohci_readl(&hc->regs->roothub.a); }
205 static inline u32 roothub_b(struct ohci *hc)
206 { return ohci_readl(&hc->regs->roothub.b); }
207 static inline u32 roothub_status(struct ohci *hc)
208 { return ohci_readl(&hc->regs->roothub.status); }
209 static inline u32 roothub_portstatus(struct ohci *hc, int i)
210 { return ohci_readl(&hc->regs->roothub.portstatus[i]); }
212 /* forward declaration */
213 static int hc_interrupt(ohci_t *ohci);
214 static void td_submit_job(ohci_t *ohci, struct usb_device *dev,
215 unsigned long pipe, void *buffer, int transfer_len,
216 struct devrequest *setup, urb_priv_t *urb,
218 static int ep_link(ohci_t * ohci, ed_t * ed);
219 static int ep_unlink(ohci_t * ohci, ed_t * ed);
220 static ed_t *ep_add_ed(ohci_dev_t *ohci_dev, struct usb_device *usb_dev,
221 unsigned long pipe, int interval, int load);
223 /*-------------------------------------------------------------------------*/
226 static struct td *td_alloc(ohci_dev_t *ohci_dev, struct usb_device *usb_dev)
232 for (i = 0; i < NUM_TD; i++)
234 if (ohci_dev->tds[i].usb_dev == NULL)
236 td = &ohci_dev->tds[i];
237 td->usb_dev = usb_dev;
245 static inline void ed_free(struct ed *ed)
250 /*-------------------------------------------------------------------------*
251 * URB support functions
252 *-------------------------------------------------------------------------*/
254 /* free HCD-private data associated with this URB */
256 static void urb_free_priv(urb_priv_t *urb)
262 last = urb->length - 1;
264 for (i = 0; i <= last; i++) {
275 /*-------------------------------------------------------------------------*/
278 static int sohci_get_current_frame_number(ohci_t *ohci);
280 /* debug| print the main components of an URB
281 * small: 0) header + data packets 1) just header */
283 static void pkt_print(ohci_t *ohci, urb_priv_t *purb, struct usb_device *dev,
284 unsigned long pipe, void *buffer, int transfer_len,
285 struct devrequest *setup, char *str, int small)
287 dbg("%s URB:[%4x] dev:%2lu,ep:%2lu-%c,type:%s,len:%d/%d stat:%#lx",
289 sohci_get_current_frame_number(ohci),
290 usb_pipedevice(pipe),
291 usb_pipeendpoint(pipe),
292 usb_pipeout(pipe)? 'O': 'I',
293 usb_pipetype(pipe) < 2 ? \
294 (usb_pipeint(pipe)? "INTR": "ISOC"): \
295 (usb_pipecontrol(pipe)? "CTRL": "BULK"),
296 (purb ? purb->actual_length : 0),
297 transfer_len, dev->status);
298 #ifdef OHCI_VERBOSE_DEBUG
302 if (usb_pipecontrol(pipe)) {
303 printf(__FILE__ ": cmd(8):");
304 for (i = 0; i < 8 ; i++)
305 printf(" %02x", ((__u8 *) setup) [i]);
308 if (transfer_len > 0 && buffer) {
309 printf(__FILE__ ": data(%d/%d):",
310 (purb ? purb->actual_length : 0),
312 len = usb_pipeout(pipe)? transfer_len:
313 (purb ? purb->actual_length : 0);
314 for (i = 0; i < 16 && i < len; i++)
315 printf(" %02x", ((__u8 *) buffer) [i]);
316 printf("%s\n", i < len? "...": "");
322 /* just for debugging; prints non-empty branches of the int ed tree
323 * inclusive iso eds */
324 void ep_print_int_eds(ohci_t *ohci, char *str)
328 for (i = 0; i < 32; i++) {
330 ed_p = &(ohci->hcca->int_table [i]);
333 invalidate_dcache_ed(ed_p);
334 printf(__FILE__ ": %s branch int %2d(%2x):", str, i, i);
335 while (*ed_p != 0 && j--) {
336 ed_t *ed = (ed_t *)m32_swap(ed_p);
337 invalidate_dcache_ed(ed);
338 printf(" ed: %4x;", ed->hwINFO);
339 ed_p = &ed->hwNextED;
345 static void ohci_dump_intr_mask(char *label, __u32 mask)
347 dbg("%s: 0x%08x%s%s%s%s%s%s%s%s%s",
350 (mask & OHCI_INTR_MIE) ? " MIE" : "",
351 (mask & OHCI_INTR_OC) ? " OC" : "",
352 (mask & OHCI_INTR_RHSC) ? " RHSC" : "",
353 (mask & OHCI_INTR_FNO) ? " FNO" : "",
354 (mask & OHCI_INTR_UE) ? " UE" : "",
355 (mask & OHCI_INTR_RD) ? " RD" : "",
356 (mask & OHCI_INTR_SF) ? " SF" : "",
357 (mask & OHCI_INTR_WDH) ? " WDH" : "",
358 (mask & OHCI_INTR_SO) ? " SO" : ""
362 static void maybe_print_eds(char *label, __u32 value)
364 ed_t *edp = (ed_t *)value;
367 dbg("%s %08x", label, value);
368 invalidate_dcache_ed(edp);
369 dbg("%08x", edp->hwINFO);
370 dbg("%08x", edp->hwTailP);
371 dbg("%08x", edp->hwHeadP);
372 dbg("%08x", edp->hwNextED);
376 static char *hcfs2string(int state)
379 case OHCI_USB_RESET: return "reset";
380 case OHCI_USB_RESUME: return "resume";
381 case OHCI_USB_OPER: return "operational";
382 case OHCI_USB_SUSPEND: return "suspend";
387 /* dump control and status registers */
388 static void ohci_dump_status(ohci_t *controller)
390 struct ohci_regs *regs = controller->regs;
393 temp = ohci_readl(®s->revision) & 0xff;
395 dbg("spec %d.%d", (temp >> 4), (temp & 0x0f));
397 temp = ohci_readl(®s->control);
398 dbg("control: 0x%08x%s%s%s HCFS=%s%s%s%s%s CBSR=%d", temp,
399 (temp & OHCI_CTRL_RWE) ? " RWE" : "",
400 (temp & OHCI_CTRL_RWC) ? " RWC" : "",
401 (temp & OHCI_CTRL_IR) ? " IR" : "",
402 hcfs2string(temp & OHCI_CTRL_HCFS),
403 (temp & OHCI_CTRL_BLE) ? " BLE" : "",
404 (temp & OHCI_CTRL_CLE) ? " CLE" : "",
405 (temp & OHCI_CTRL_IE) ? " IE" : "",
406 (temp & OHCI_CTRL_PLE) ? " PLE" : "",
407 temp & OHCI_CTRL_CBSR
410 temp = ohci_readl(®s->cmdstatus);
411 dbg("cmdstatus: 0x%08x SOC=%d%s%s%s%s", temp,
412 (temp & OHCI_SOC) >> 16,
413 (temp & OHCI_OCR) ? " OCR" : "",
414 (temp & OHCI_BLF) ? " BLF" : "",
415 (temp & OHCI_CLF) ? " CLF" : "",
416 (temp & OHCI_HCR) ? " HCR" : ""
419 ohci_dump_intr_mask("intrstatus", ohci_readl(®s->intrstatus));
420 ohci_dump_intr_mask("intrenable", ohci_readl(®s->intrenable));
422 maybe_print_eds("ed_periodcurrent",
423 ohci_readl(®s->ed_periodcurrent));
425 maybe_print_eds("ed_controlhead", ohci_readl(®s->ed_controlhead));
426 maybe_print_eds("ed_controlcurrent",
427 ohci_readl(®s->ed_controlcurrent));
429 maybe_print_eds("ed_bulkhead", ohci_readl(®s->ed_bulkhead));
430 maybe_print_eds("ed_bulkcurrent", ohci_readl(®s->ed_bulkcurrent));
432 maybe_print_eds("donehead", ohci_readl(®s->donehead));
435 static void ohci_dump_roothub(ohci_t *controller, int verbose)
439 temp = roothub_a(controller);
440 ndp = (temp & RH_A_NDP);
441 #ifdef CONFIG_AT91C_PQFP_UHPBUG
442 ndp = (ndp == 2) ? 1:0;
445 dbg("roothub.a: %08x POTPGT=%d%s%s%s%s%s NDP=%d", temp,
446 ((temp & RH_A_POTPGT) >> 24) & 0xff,
447 (temp & RH_A_NOCP) ? " NOCP" : "",
448 (temp & RH_A_OCPM) ? " OCPM" : "",
449 (temp & RH_A_DT) ? " DT" : "",
450 (temp & RH_A_NPS) ? " NPS" : "",
451 (temp & RH_A_PSM) ? " PSM" : "",
454 temp = roothub_b(controller);
455 dbg("roothub.b: %08x PPCM=%04x DR=%04x",
457 (temp & RH_B_PPCM) >> 16,
460 temp = roothub_status(controller);
461 dbg("roothub.status: %08x%s%s%s%s%s%s",
463 (temp & RH_HS_CRWE) ? " CRWE" : "",
464 (temp & RH_HS_OCIC) ? " OCIC" : "",
465 (temp & RH_HS_LPSC) ? " LPSC" : "",
466 (temp & RH_HS_DRWE) ? " DRWE" : "",
467 (temp & RH_HS_OCI) ? " OCI" : "",
468 (temp & RH_HS_LPS) ? " LPS" : ""
472 for (i = 0; i < ndp; i++) {
473 temp = roothub_portstatus(controller, i);
474 dbg("roothub.portstatus [%d] = 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s",
477 (temp & RH_PS_PRSC) ? " PRSC" : "",
478 (temp & RH_PS_OCIC) ? " OCIC" : "",
479 (temp & RH_PS_PSSC) ? " PSSC" : "",
480 (temp & RH_PS_PESC) ? " PESC" : "",
481 (temp & RH_PS_CSC) ? " CSC" : "",
483 (temp & RH_PS_LSDA) ? " LSDA" : "",
484 (temp & RH_PS_PPS) ? " PPS" : "",
485 (temp & RH_PS_PRS) ? " PRS" : "",
486 (temp & RH_PS_POCI) ? " POCI" : "",
487 (temp & RH_PS_PSS) ? " PSS" : "",
489 (temp & RH_PS_PES) ? " PES" : "",
490 (temp & RH_PS_CCS) ? " CCS" : ""
495 static void ohci_dump(ohci_t *controller, int verbose)
497 dbg("OHCI controller usb-%s state", controller->slot_name);
499 /* dumps some of the state we know about */
500 ohci_dump_status(controller);
502 ep_print_int_eds(controller, "hcca");
503 invalidate_dcache_hcca(controller->hcca);
504 dbg("hcca frame #%04x", controller->hcca->frame_no);
505 ohci_dump_roothub(controller, 1);
509 /*-------------------------------------------------------------------------*
510 * Interface functions (URB)
511 *-------------------------------------------------------------------------*/
513 /* get a transfer request */
515 int sohci_submit_job(ohci_t *ohci, ohci_dev_t *ohci_dev, urb_priv_t *urb,
516 struct devrequest *setup)
519 urb_priv_t *purb_priv = urb;
521 struct usb_device *dev = urb->dev;
522 unsigned long pipe = urb->pipe;
523 void *buffer = urb->transfer_buffer;
524 int transfer_len = urb->transfer_buffer_length;
525 int interval = urb->interval;
527 /* when controller's hung, permit only roothub cleanup attempts
528 * such as powering down ports */
529 if (ohci->disabled) {
530 err("sohci_submit_job: EPIPE");
534 /* we're about to begin a new transaction here so mark the
538 /* every endpoint has a ed, locate and fill it */
539 ed = ep_add_ed(ohci_dev, dev, pipe, interval, 1);
541 err("sohci_submit_job: ENOMEM");
545 /* for the private part of the URB we need the number of TDs (size) */
546 switch (usb_pipetype(pipe)) {
547 case PIPE_BULK: /* one TD for every 4096 Byte */
548 size = (transfer_len - 1) / 4096 + 1;
550 case PIPE_CONTROL:/* 1 TD for setup, 1 for ACK and 1 for every 4096 B */
551 size = (transfer_len == 0)? 2:
552 (transfer_len - 1) / 4096 + 3;
554 case PIPE_INTERRUPT: /* 1 TD */
561 if (size >= (N_URB_TD - 1)) {
562 err("need %d TDs, only have %d", size, N_URB_TD);
565 purb_priv->pipe = pipe;
567 /* fill the private part of the URB */
568 purb_priv->length = size;
570 purb_priv->actual_length = 0;
572 /* allocate the TDs */
573 /* note that td[0] was allocated in ep_add_ed */
574 for (i = 0; i < size; i++) {
575 purb_priv->td[i] = td_alloc(ohci_dev, dev);
576 if (!purb_priv->td[i]) {
577 purb_priv->length = i;
578 urb_free_priv(purb_priv);
579 err("sohci_submit_job: ENOMEM");
584 if (ed->state == ED_NEW || (ed->state & ED_DEL)) {
585 urb_free_priv(purb_priv);
586 err("sohci_submit_job: EINVAL");
590 /* link the ed into a chain if is not already */
591 if (ed->state != ED_OPER)
594 /* fill the TDs and link it to the ed */
595 td_submit_job(ohci, dev, pipe, buffer, transfer_len,
596 setup, purb_priv, interval);
601 /*-------------------------------------------------------------------------*/
604 /* tell us the current USB frame number */
605 static int sohci_get_current_frame_number(ohci_t *ohci)
607 invalidate_dcache_hcca(ohci->hcca);
608 return m16_swap(ohci->hcca->frame_no);
612 /*-------------------------------------------------------------------------*
613 * ED handling functions
614 *-------------------------------------------------------------------------*/
616 /* search for the right branch to insert an interrupt ed into the int tree
617 * do some load ballancing;
618 * returns the branch and
619 * sets the interval to interval = 2^integer (ld (interval)) */
621 static int ep_int_ballance(ohci_t *ohci, int interval, int load)
625 /* search for the least loaded interrupt endpoint
626 * branch of all 32 branches
628 for (i = 0; i < 32; i++)
629 if (ohci->ohci_int_load [branch] > ohci->ohci_int_load [i])
632 branch = branch % interval;
633 for (i = branch; i < 32; i += interval)
634 ohci->ohci_int_load [i] += load;
639 /*-------------------------------------------------------------------------*/
641 /* 2^int( ld (inter)) */
643 static int ep_2_n_interval(int inter)
646 for (i = 0; ((inter >> i) > 1) && (i < 5); i++);
650 /*-------------------------------------------------------------------------*/
652 /* the int tree is a binary tree
653 * in order to process it sequentially the indexes of the branches have to
654 * be mapped the mapping reverses the bits of a word of num_bits length */
655 static int ep_rev(int num_bits, int word)
659 for (i = 0; i < num_bits; i++)
660 wout |= (((word >> i) & 1) << (num_bits - i - 1));
664 /*-------------------------------------------------------------------------*
665 * ED handling functions
666 *-------------------------------------------------------------------------*/
668 /* link an ed into one of the HC chains */
670 static int ep_link(ohci_t *ohci, ed_t *edi)
672 volatile ed_t *ed = edi;
681 ed->int_interval = 0;
687 if (ohci->ed_controltail == NULL)
688 ohci_writel(ed, &ohci->regs->ed_controlhead);
690 ohci->ed_controltail->hwNextED =
691 m32_swap((unsigned long)ed);
693 ed->ed_prev = ohci->ed_controltail;
694 if (!ohci->ed_controltail && !ohci->ed_rm_list[0] &&
695 !ohci->ed_rm_list[1] && !ohci->sleeping) {
696 ohci->hc_control |= OHCI_CTRL_CLE;
697 ohci_writel(ohci->hc_control, &ohci->regs->control);
699 ohci->ed_controltail = edi;
705 if (ohci->ed_bulktail == NULL)
706 ohci_writel(ed, &ohci->regs->ed_bulkhead);
708 ohci->ed_bulktail->hwNextED =
709 m32_swap((unsigned long)ed);
711 ed->ed_prev = ohci->ed_bulktail;
712 if (!ohci->ed_bulktail && !ohci->ed_rm_list[0] &&
713 !ohci->ed_rm_list[1] && !ohci->sleeping) {
714 ohci->hc_control |= OHCI_CTRL_BLE;
715 ohci_writel(ohci->hc_control, &ohci->regs->control);
717 ohci->ed_bulktail = edi;
722 interval = ep_2_n_interval(ed->int_period);
723 ed->int_interval = interval;
724 int_branch = ep_int_ballance(ohci, interval, load);
725 ed->int_branch = int_branch;
727 for (i = 0; i < ep_rev(6, interval); i += inter) {
729 for (ed_p = &(ohci->hcca->int_table[\
730 ep_rev(5, i) + int_branch]);
732 (((ed_t *)ed_p)->int_interval >= interval);
733 ed_p = &(((ed_t *)ed_p)->hwNextED))
735 ((ed_t *)ed_p)->int_interval);
736 ed->hwNextED = *ed_p;
738 *ed_p = m32_swap((unsigned long)ed);
739 flush_dcache_hcca(ohci->hcca);
746 /*-------------------------------------------------------------------------*/
748 /* scan the periodic table to find and unlink this ED */
749 static void periodic_unlink(struct ohci *ohci, volatile struct ed *ed,
750 unsigned index, unsigned period)
752 __maybe_unused unsigned long aligned_ed_p;
754 for (; index < NUM_INTS; index += period) {
755 __u32 *ed_p = &ohci->hcca->int_table [index];
757 /* ED might have been unlinked through another path */
760 m32_swap((unsigned long)ed_p)) == ed) {
761 *ed_p = ed->hwNextED;
763 aligned_ed_p = (unsigned long)ed_p;
764 aligned_ed_p &= ~(ARCH_DMA_MINALIGN - 1);
765 flush_dcache_range(aligned_ed_p,
766 aligned_ed_p + ARCH_DMA_MINALIGN);
770 ed_p = &(((struct ed *)
771 m32_swap((unsigned long)ed_p))->hwNextED);
776 /* unlink an ed from one of the HC chains.
777 * just the link to the ed is unlinked.
778 * the link from the ed still points to another operational ed or 0
779 * so the HC can eventually finish the processing of the unlinked ed */
781 static int ep_unlink(ohci_t *ohci, ed_t *edi)
783 volatile ed_t *ed = edi;
786 ed->hwINFO |= m32_swap(OHCI_ED_SKIP);
791 if (ed->ed_prev == NULL) {
793 ohci->hc_control &= ~OHCI_CTRL_CLE;
794 ohci_writel(ohci->hc_control,
795 &ohci->regs->control);
797 ohci_writel(m32_swap(*((__u32 *)&ed->hwNextED)),
798 &ohci->regs->ed_controlhead);
800 ed->ed_prev->hwNextED = ed->hwNextED;
801 flush_dcache_ed(ed->ed_prev);
803 if (ohci->ed_controltail == ed) {
804 ohci->ed_controltail = ed->ed_prev;
807 *((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
812 if (ed->ed_prev == NULL) {
814 ohci->hc_control &= ~OHCI_CTRL_BLE;
815 ohci_writel(ohci->hc_control,
816 &ohci->regs->control);
818 ohci_writel(m32_swap(*((__u32 *)&ed->hwNextED)),
819 &ohci->regs->ed_bulkhead);
821 ed->ed_prev->hwNextED = ed->hwNextED;
822 flush_dcache_ed(ed->ed_prev);
824 if (ohci->ed_bulktail == ed) {
825 ohci->ed_bulktail = ed->ed_prev;
828 *((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
833 periodic_unlink(ohci, ed, 0, 1);
834 for (i = ed->int_branch; i < 32; i += ed->int_interval)
835 ohci->ohci_int_load[i] -= ed->int_load;
838 ed->state = ED_UNLINK;
842 /*-------------------------------------------------------------------------*/
844 /* add/reinit an endpoint; this should be done once at the
845 * usb_set_configuration command, but the USB stack is a little bit
846 * stateless so we do it at every transaction if the state of the ed
847 * is ED_NEW then a dummy td is added and the state is changed to
848 * ED_UNLINK in all other cases the state is left unchanged the ed
849 * info fields are setted anyway even though most of them should not
852 static ed_t *ep_add_ed(ohci_dev_t *ohci_dev, struct usb_device *usb_dev,
853 unsigned long pipe, int interval, int load)
859 ed = ed_ret = &ohci_dev->ed[(usb_pipeendpoint(pipe) << 1) |
860 (usb_pipecontrol(pipe)? 0: usb_pipeout(pipe))];
862 if ((ed->state & ED_DEL) || (ed->state & ED_URB_DEL)) {
863 err("ep_add_ed: pending delete");
864 /* pending delete request */
868 if (ed->state == ED_NEW) {
869 /* dummy td; end of td list for ed */
870 td = td_alloc(ohci_dev, usb_dev);
871 ed->hwTailP = m32_swap((unsigned long)td);
872 ed->hwHeadP = ed->hwTailP;
873 ed->state = ED_UNLINK;
874 ed->type = usb_pipetype(pipe);
878 ed->hwINFO = m32_swap(usb_pipedevice(pipe)
879 | usb_pipeendpoint(pipe) << 7
880 | (usb_pipeisoc(pipe)? 0x8000: 0)
881 | (usb_pipecontrol(pipe)? 0: \
882 (usb_pipeout(pipe)? 0x800: 0x1000))
883 | (usb_dev->speed == USB_SPEED_LOW) << 13
884 | usb_maxpacket(usb_dev, pipe) << 16);
886 if (ed->type == PIPE_INTERRUPT && ed->state == ED_UNLINK) {
887 ed->int_period = interval;
896 /*-------------------------------------------------------------------------*
897 * TD handling functions
898 *-------------------------------------------------------------------------*/
900 /* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
902 static void td_fill(ohci_t *ohci, unsigned int info,
904 struct usb_device *dev, int index, urb_priv_t *urb_priv)
906 volatile td_t *td, *td_pt;
907 #ifdef OHCI_FILL_TRACE
911 if (index > urb_priv->length) {
912 err("index > length");
915 /* use this td as the next dummy */
916 td_pt = urb_priv->td [index];
918 flush_dcache_td(td_pt);
920 /* fill the old dummy TD */
921 td = urb_priv->td [index] =
922 (td_t *)(m32_swap(urb_priv->ed->hwTailP) & ~0xf);
924 td->ed = urb_priv->ed;
925 td->next_dl_td = NULL;
927 td->data = (__u32)data;
928 #ifdef OHCI_FILL_TRACE
929 if (usb_pipebulk(urb_priv->pipe) && usb_pipeout(urb_priv->pipe)) {
930 for (i = 0; i < len; i++)
931 printf("td->data[%d] %#2x ", i, ((unsigned char *)td->data)[i]);
938 td->hwINFO = m32_swap(info);
939 td->hwCBP = m32_swap((unsigned long)data);
941 td->hwBE = m32_swap((unsigned long)(data + len - 1));
945 td->hwNextTD = m32_swap((unsigned long)td_pt);
948 /* append to queue */
949 td->ed->hwTailP = td->hwNextTD;
950 flush_dcache_ed(td->ed);
953 /*-------------------------------------------------------------------------*/
955 /* prepare all TDs of a transfer */
957 static void td_submit_job(ohci_t *ohci, struct usb_device *dev,
958 unsigned long pipe, void *buffer, int transfer_len,
959 struct devrequest *setup, urb_priv_t *urb,
962 int data_len = transfer_len;
966 unsigned int toggle = 0;
968 flush_dcache_buffer(buffer, data_len);
970 /* OHCI handles the DATA-toggles itself, we just use the USB-toggle
971 * bits for reseting */
972 if (usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe))) {
973 toggle = TD_T_TOGGLE;
976 usb_settoggle(dev, usb_pipeendpoint(pipe),
977 usb_pipeout(pipe), 1);
985 switch (usb_pipetype(pipe)) {
987 info = usb_pipeout(pipe)?
988 TD_CC | TD_DP_OUT : TD_CC | TD_DP_IN ;
989 while (data_len > 4096) {
990 td_fill(ohci, info | (cnt? TD_T_TOGGLE:toggle),
991 data, 4096, dev, cnt, urb);
992 data += 4096; data_len -= 4096; cnt++;
994 info = usb_pipeout(pipe)?
995 TD_CC | TD_DP_OUT : TD_CC | TD_R | TD_DP_IN ;
996 td_fill(ohci, info | (cnt? TD_T_TOGGLE:toggle), data,
997 data_len, dev, cnt, urb);
1000 if (!ohci->sleeping) {
1001 /* start bulk list */
1002 ohci_writel(OHCI_BLF, &ohci->regs->cmdstatus);
1008 info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
1009 flush_dcache_buffer(setup, 8);
1010 td_fill(ohci, info, setup, 8, dev, cnt++, urb);
1012 /* Optional Data phase */
1014 info = usb_pipeout(pipe)?
1015 TD_CC | TD_R | TD_DP_OUT | TD_T_DATA1 :
1016 TD_CC | TD_R | TD_DP_IN | TD_T_DATA1;
1017 /* NOTE: mishandles transfers >8K, some >4K */
1018 td_fill(ohci, info, data, data_len, dev, cnt++, urb);
1022 info = (usb_pipeout(pipe) || data_len == 0) ?
1023 TD_CC | TD_DP_IN | TD_T_DATA1:
1024 TD_CC | TD_DP_OUT | TD_T_DATA1;
1025 td_fill(ohci, info, data, 0, dev, cnt++, urb);
1027 if (!ohci->sleeping) {
1028 /* start Control list */
1029 ohci_writel(OHCI_CLF, &ohci->regs->cmdstatus);
1033 case PIPE_INTERRUPT:
1034 info = usb_pipeout(urb->pipe)?
1035 TD_CC | TD_DP_OUT | toggle:
1036 TD_CC | TD_R | TD_DP_IN | toggle;
1037 td_fill(ohci, info, data, data_len, dev, cnt++, urb);
1040 if (urb->length != cnt)
1041 dbg("TD LENGTH %d != CNT %d", urb->length, cnt);
1044 /*-------------------------------------------------------------------------*
1045 * Done List handling functions
1046 *-------------------------------------------------------------------------*/
1048 /* calculate the transfer length and update the urb */
1050 static void dl_transfer_length(td_t *td)
1053 urb_priv_t *lurb_priv = td->ed->purb;
1055 tdBE = m32_swap(td->hwBE);
1056 tdCBP = m32_swap(td->hwCBP);
1058 if (!(usb_pipecontrol(lurb_priv->pipe) &&
1059 ((td->index == 0) || (td->index == lurb_priv->length - 1)))) {
1062 lurb_priv->actual_length += tdBE - td->data + 1;
1064 lurb_priv->actual_length += tdCBP - td->data;
1069 /*-------------------------------------------------------------------------*/
1070 static void check_status(td_t *td_list)
1072 urb_priv_t *lurb_priv = td_list->ed->purb;
1073 int urb_len = lurb_priv->length;
1074 __u32 *phwHeadP = &td_list->ed->hwHeadP;
1077 cc = TD_CC_GET(m32_swap(td_list->hwINFO));
1079 err(" USB-error: %s (%x)", cc_to_string[cc], cc);
1081 invalidate_dcache_ed(td_list->ed);
1082 if (*phwHeadP & m32_swap(0x1)) {
1084 ((td_list->index + 1) < urb_len)) {
1086 (lurb_priv->td[urb_len - 1]->hwNextTD &\
1087 m32_swap(0xfffffff0)) |
1088 (*phwHeadP & m32_swap(0x2));
1090 lurb_priv->td_cnt += urb_len -
1093 *phwHeadP &= m32_swap(0xfffffff2);
1094 flush_dcache_ed(td_list->ed);
1096 #ifdef CONFIG_MPC5200
1097 td_list->hwNextTD = 0;
1098 flush_dcache_td(td_list);
1103 /* replies to the request have to be on a FIFO basis so
1104 * we reverse the reversed done-list */
1105 static td_t *dl_reverse_done_list(ohci_t *ohci)
1108 td_t *td_rev = NULL;
1109 td_t *td_list = NULL;
1111 invalidate_dcache_hcca(ohci->hcca);
1112 td_list_hc = m32_swap(ohci->hcca->done_head) & 0xfffffff0;
1113 ohci->hcca->done_head = 0;
1114 flush_dcache_hcca(ohci->hcca);
1116 while (td_list_hc) {
1117 td_list = (td_t *)td_list_hc;
1118 invalidate_dcache_td(td_list);
1119 check_status(td_list);
1120 td_list->next_dl_td = td_rev;
1122 td_list_hc = m32_swap(td_list->hwNextTD) & 0xfffffff0;
1127 /*-------------------------------------------------------------------------*/
1128 /*-------------------------------------------------------------------------*/
1130 static void finish_urb(ohci_t *ohci, urb_priv_t *urb, int status)
1132 if ((status & (ED_OPER | ED_UNLINK)) && (urb->state != URB_DEL))
1135 dbg("finish_urb: strange.., ED state %x, \n", status);
1139 * Used to take back a TD from the host controller. This would normally be
1140 * called from within dl_done_list, however it may be called directly if the
1141 * HC no longer sees the TD and it has not appeared on the donelist (after
1142 * two frames). This bug has been observed on ZF Micro systems.
1144 static int takeback_td(ohci_t *ohci, td_t *td_list)
1150 urb_priv_t *lurb_priv;
1151 __u32 tdINFO, edHeadP, edTailP;
1153 invalidate_dcache_td(td_list);
1154 tdINFO = m32_swap(td_list->hwINFO);
1157 lurb_priv = ed->purb;
1159 dl_transfer_length(td_list);
1161 lurb_priv->td_cnt++;
1163 /* error code of transfer */
1164 cc = TD_CC_GET(tdINFO);
1166 err("USB-error: %s (%x)", cc_to_string[cc], cc);
1167 stat = cc_to_error[cc];
1170 /* see if this done list makes for all TD's of current URB,
1171 * and mark the URB finished if so */
1172 if (lurb_priv->td_cnt == lurb_priv->length)
1173 finish_urb(ohci, lurb_priv, ed->state);
1175 dbg("dl_done_list: processing TD %x, len %x\n",
1176 lurb_priv->td_cnt, lurb_priv->length);
1178 if (ed->state != ED_NEW && (!usb_pipeint(lurb_priv->pipe))) {
1179 invalidate_dcache_ed(ed);
1180 edHeadP = m32_swap(ed->hwHeadP) & 0xfffffff0;
1181 edTailP = m32_swap(ed->hwTailP);
1183 /* unlink eds if they are not busy */
1184 if ((edHeadP == edTailP) && (ed->state == ED_OPER))
1185 ep_unlink(ohci, ed);
1190 static int dl_done_list(ohci_t *ohci)
1193 td_t *td_list = dl_reverse_done_list(ohci);
1196 td_t *td_next = td_list->next_dl_td;
1197 stat = takeback_td(ohci, td_list);
1203 /*-------------------------------------------------------------------------*
1205 *-------------------------------------------------------------------------*/
1207 #include <usbroothubdes.h>
1209 /* Hub class-specific descriptor is constructed dynamically */
1211 /*-------------------------------------------------------------------------*/
1213 #define OK(x) len = (x); break
1215 #define WR_RH_STAT(x) {info("WR:status %#8x", (x)); ohci_writel((x), \
1216 &ohci->regs->roothub.status); }
1217 #define WR_RH_PORTSTAT(x) {info("WR:portstatus[%d] %#8x", wIndex-1, \
1218 (x)); ohci_writel((x), &ohci->regs->roothub.portstatus[wIndex-1]); }
1220 #define WR_RH_STAT(x) ohci_writel((x), &ohci->regs->roothub.status)
1221 #define WR_RH_PORTSTAT(x) ohci_writel((x), \
1222 &ohci->regs->roothub.portstatus[wIndex-1])
1224 #define RD_RH_STAT roothub_status(ohci)
1225 #define RD_RH_PORTSTAT roothub_portstatus(ohci, wIndex-1)
1227 /* request to virtual root hub */
1229 int rh_check_port_status(ohci_t *controller)
1235 temp = roothub_a(controller);
1236 ndp = (temp & RH_A_NDP);
1237 #ifdef CONFIG_AT91C_PQFP_UHPBUG
1238 ndp = (ndp == 2) ? 1:0;
1240 for (i = 0; i < ndp; i++) {
1241 temp = roothub_portstatus(controller, i);
1242 /* check for a device disconnect */
1243 if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
1244 (RH_PS_PESC | RH_PS_CSC)) &&
1245 ((temp & RH_PS_CCS) == 0)) {
1253 static int ohci_submit_rh_msg(ohci_t *ohci, struct usb_device *dev,
1254 unsigned long pipe, void *buffer, int transfer_len,
1255 struct devrequest *cmd)
1257 void *data = buffer;
1258 int leni = transfer_len;
1265 ALLOC_ALIGN_BUFFER(__u8, databuf, 16, sizeof(u32));
1268 pkt_print(ohci, NULL, dev, pipe, buffer, transfer_len,
1269 cmd, "SUB(rh)", usb_pipein(pipe));
1273 if (usb_pipeint(pipe)) {
1274 info("Root-Hub submit IRQ: NOT implemented");
1278 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
1279 wValue = le16_to_cpu(cmd->value);
1280 wIndex = le16_to_cpu(cmd->index);
1281 wLength = le16_to_cpu(cmd->length);
1283 info("Root-Hub: adr: %2x cmd(%1x): %08x %04x %04x %04x",
1284 dev->devnum, 8, bmRType_bReq, wValue, wIndex, wLength);
1286 switch (bmRType_bReq) {
1287 /* Request Destination:
1288 without flags: Device,
1289 RH_INTERFACE: interface,
1290 RH_ENDPOINT: endpoint,
1291 RH_CLASS means HUB here,
1292 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1296 *(u16 *)databuf = cpu_to_le16(1);
1298 case RH_GET_STATUS | RH_INTERFACE:
1299 *(u16 *)databuf = cpu_to_le16(0);
1301 case RH_GET_STATUS | RH_ENDPOINT:
1302 *(u16 *)databuf = cpu_to_le16(0);
1304 case RH_GET_STATUS | RH_CLASS:
1305 *(u32 *)databuf = cpu_to_le32(
1306 RD_RH_STAT & ~(RH_HS_CRWE | RH_HS_DRWE));
1308 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
1309 *(u32 *)databuf = cpu_to_le32(RD_RH_PORTSTAT);
1312 case RH_CLEAR_FEATURE | RH_ENDPOINT:
1314 case (RH_ENDPOINT_STALL):
1319 case RH_CLEAR_FEATURE | RH_CLASS:
1321 case RH_C_HUB_LOCAL_POWER:
1323 case (RH_C_HUB_OVER_CURRENT):
1324 WR_RH_STAT(RH_HS_OCIC);
1329 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1331 case (RH_PORT_ENABLE): WR_RH_PORTSTAT(RH_PS_CCS); OK(0);
1332 case (RH_PORT_SUSPEND): WR_RH_PORTSTAT(RH_PS_POCI); OK(0);
1333 case (RH_PORT_POWER): WR_RH_PORTSTAT(RH_PS_LSDA); OK(0);
1334 case (RH_C_PORT_CONNECTION): WR_RH_PORTSTAT(RH_PS_CSC); OK(0);
1335 case (RH_C_PORT_ENABLE): WR_RH_PORTSTAT(RH_PS_PESC); OK(0);
1336 case (RH_C_PORT_SUSPEND): WR_RH_PORTSTAT(RH_PS_PSSC); OK(0);
1337 case (RH_C_PORT_OVER_CURRENT):WR_RH_PORTSTAT(RH_PS_OCIC); OK(0);
1338 case (RH_C_PORT_RESET): WR_RH_PORTSTAT(RH_PS_PRSC); OK(0);
1342 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1344 case (RH_PORT_SUSPEND):
1345 WR_RH_PORTSTAT(RH_PS_PSS); OK(0);
1346 case (RH_PORT_RESET): /* BUG IN HUP CODE *********/
1347 if (RD_RH_PORTSTAT & RH_PS_CCS)
1348 WR_RH_PORTSTAT(RH_PS_PRS);
1350 case (RH_PORT_POWER):
1351 WR_RH_PORTSTAT(RH_PS_PPS);
1353 case (RH_PORT_ENABLE): /* BUG IN HUP CODE *********/
1354 if (RD_RH_PORTSTAT & RH_PS_CCS)
1355 WR_RH_PORTSTAT(RH_PS_PES);
1360 case RH_SET_ADDRESS:
1361 ohci->rh.devnum = wValue;
1364 case RH_GET_DESCRIPTOR:
1365 switch ((wValue & 0xff00) >> 8) {
1366 case (0x01): /* device descriptor */
1367 len = min_t(unsigned int,
1370 sizeof(root_hub_dev_des),
1372 databuf = root_hub_dev_des; OK(len);
1373 case (0x02): /* configuration descriptor */
1374 len = min_t(unsigned int,
1377 sizeof(root_hub_config_des),
1379 databuf = root_hub_config_des; OK(len);
1380 case (0x03): /* string descriptors */
1381 if (wValue == 0x0300) {
1382 len = min_t(unsigned int,
1385 sizeof(root_hub_str_index0),
1387 databuf = root_hub_str_index0;
1390 if (wValue == 0x0301) {
1391 len = min_t(unsigned int,
1394 sizeof(root_hub_str_index1),
1396 databuf = root_hub_str_index1;
1400 stat = USB_ST_STALLED;
1404 case RH_GET_DESCRIPTOR | RH_CLASS:
1406 __u32 temp = roothub_a(ohci);
1408 databuf[0] = 9; /* min length; */
1410 databuf[2] = temp & RH_A_NDP;
1411 #ifdef CONFIG_AT91C_PQFP_UHPBUG
1412 databuf[2] = (databuf[2] == 2) ? 1 : 0;
1415 if (temp & RH_A_PSM) /* per-port power switching? */
1417 if (temp & RH_A_NOCP) /* no overcurrent reporting? */
1419 else if (temp & RH_A_OCPM)/* per-port overcurrent reporting? */
1423 databuf[5] = (temp & RH_A_POTPGT) >> 24;
1425 temp = roothub_b(ohci);
1426 databuf[7] = temp & RH_B_DR;
1427 if (databuf[2] < 7) {
1431 databuf[8] = (temp & RH_B_DR) >> 8;
1432 databuf[10] = databuf[9] = 0xff;
1435 len = min_t(unsigned int, leni,
1436 min_t(unsigned int, databuf[0], wLength));
1440 case RH_GET_CONFIGURATION:
1444 case RH_SET_CONFIGURATION:
1445 WR_RH_STAT(0x10000);
1449 dbg("unsupported root hub command");
1450 stat = USB_ST_STALLED;
1454 ohci_dump_roothub(ohci, 1);
1459 len = min_t(int, len, leni);
1460 if (data != databuf)
1461 memcpy(data, databuf, len);
1466 pkt_print(ohci, NULL, dev, pipe, buffer,
1467 transfer_len, cmd, "RET(rh)", 0/*usb_pipein(pipe)*/);
1475 /*-------------------------------------------------------------------------*/
1477 /* common code for handling submit messages - used for all but root hub */
1479 static int submit_common_msg(ohci_t *ohci, struct usb_device *dev,
1480 unsigned long pipe, void *buffer, int transfer_len,
1481 struct devrequest *setup, int interval)
1484 int maxsize = usb_maxpacket(dev, pipe);
1488 urb = malloc(sizeof(urb_priv_t));
1489 memset(urb, 0, sizeof(urb_priv_t));
1493 urb->transfer_buffer = buffer;
1494 urb->transfer_buffer_length = transfer_len;
1495 urb->interval = interval;
1498 urb->actual_length = 0;
1499 pkt_print(ohci, urb, dev, pipe, buffer, transfer_len,
1500 setup, "SUB", usb_pipein(pipe));
1505 err("submit_common_message: pipesize for pipe %lx is zero",
1510 if (sohci_submit_job(ohci, &ohci->ohci_dev, urb, setup) < 0) {
1511 err("sohci_submit_job failed");
1517 /* ohci_dump_status(ohci); */
1520 timeout = USB_TIMEOUT_MS(pipe);
1522 /* wait for it to complete */
1524 /* check whether the controller is done */
1525 stat = hc_interrupt(ohci);
1527 stat = USB_ST_CRC_ERR;
1531 /* NOTE: since we are not interrupt driven in U-Boot and always
1532 * handle only one URB at a time, we cannot assume the
1533 * transaction finished on the first successful return from
1534 * hc_interrupt().. unless the flag for current URB is set,
1535 * meaning that all TD's to/from device got actually
1536 * transferred and processed. If the current URB is not
1537 * finished we need to re-iterate this loop so as
1538 * hc_interrupt() gets called again as there needs to be some
1539 * more TD's to process still */
1540 if ((stat >= 0) && (stat != 0xff) && (urb->finished)) {
1541 /* 0xff is returned for an SF-interrupt */
1551 if (!usb_pipeint(pipe))
1552 err("CTL:TIMEOUT ");
1553 dbg("submit_common_msg: TO status %x\n", stat);
1555 stat = USB_ST_CRC_ERR;
1561 dev->act_len = urb->actual_length;
1563 if (usb_pipein(pipe) && dev->status == 0 && dev->act_len)
1564 invalidate_dcache_buffer(buffer, dev->act_len);
1567 pkt_print(ohci, urb, dev, pipe, buffer, transfer_len,
1568 setup, "RET(ctlr)", usb_pipein(pipe));
1576 /* submit routines called from usb.c */
1577 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1580 info("submit_bulk_msg");
1581 return submit_common_msg(&gohci, dev, pipe, buffer, transfer_len,
1585 int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1586 int transfer_len, int interval)
1588 info("submit_int_msg");
1589 return submit_common_msg(&gohci, dev, pipe, buffer, transfer_len, NULL,
1593 static int _ohci_submit_control_msg(ohci_t *ohci, struct usb_device *dev,
1594 unsigned long pipe, void *buffer, int transfer_len,
1595 struct devrequest *setup)
1597 int maxsize = usb_maxpacket(dev, pipe);
1599 info("submit_control_msg");
1601 pkt_print(ohci, NULL, dev, pipe, buffer, transfer_len,
1602 setup, "SUB", usb_pipein(pipe));
1607 err("submit_control_message: pipesize for pipe %lx is zero",
1611 if (((pipe >> 8) & 0x7f) == ohci->rh.devnum) {
1613 /* root hub - redirect */
1614 return ohci_submit_rh_msg(ohci, dev, pipe, buffer,
1615 transfer_len, setup);
1618 return submit_common_msg(ohci, dev, pipe, buffer, transfer_len,
1622 /*-------------------------------------------------------------------------*
1624 *-------------------------------------------------------------------------*/
1626 /* reset the HC and BUS */
1628 static int hc_reset(ohci_t *ohci)
1630 #ifdef CONFIG_PCI_EHCI_DEVNO
1634 int smm_timeout = 50; /* 0,5 sec */
1636 dbg("%s\n", __FUNCTION__);
1638 #ifdef CONFIG_PCI_EHCI_DEVNO
1640 * Some multi-function controllers (e.g. ISP1562) allow root hub
1641 * resetting via EHCI registers only.
1643 pdev = pci_find_devices(ehci_pci_ids, CONFIG_PCI_EHCI_DEVNO);
1648 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &base);
1649 base += EHCI_USBCMD_OFF;
1650 ohci_writel(ohci_readl(base) | EHCI_USBCMD_HCRESET, base);
1652 while (ohci_readl(base) & EHCI_USBCMD_HCRESET) {
1653 if (timeout-- <= 0) {
1654 printf("USB RootHub reset timed out!");
1660 printf("No EHCI func at %d index!\n", CONFIG_PCI_EHCI_DEVNO);
1662 if (ohci_readl(&ohci->regs->control) & OHCI_CTRL_IR) {
1663 /* SMM owns the HC, request ownership */
1664 ohci_writel(OHCI_OCR, &ohci->regs->cmdstatus);
1665 info("USB HC TakeOver from SMM");
1666 while (ohci_readl(&ohci->regs->control) & OHCI_CTRL_IR) {
1668 if (--smm_timeout == 0) {
1669 err("USB HC TakeOver failed!");
1675 /* Disable HC interrupts */
1676 ohci_writel(OHCI_INTR_MIE, &ohci->regs->intrdisable);
1678 dbg("USB HC reset_hc usb-%s: ctrl = 0x%X ;\n",
1680 ohci_readl(&ohci->regs->control));
1682 /* Reset USB (needed by some controllers) */
1683 ohci->hc_control = 0;
1684 ohci_writel(ohci->hc_control, &ohci->regs->control);
1686 /* HC Reset requires max 10 us delay */
1687 ohci_writel(OHCI_HCR, &ohci->regs->cmdstatus);
1688 while ((ohci_readl(&ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
1689 if (--timeout == 0) {
1690 err("USB HC reset timed out!");
1698 /*-------------------------------------------------------------------------*/
1700 /* Start an OHCI controller, set the BUS operational
1702 * connect the virtual root hub */
1704 static int hc_start(ohci_t *ohci)
1707 unsigned int fminterval;
1711 /* Tell the controller where the control and bulk lists are
1712 * The lists are empty now. */
1714 ohci_writel(0, &ohci->regs->ed_controlhead);
1715 ohci_writel(0, &ohci->regs->ed_bulkhead);
1717 ohci_writel((__u32)ohci->hcca,
1718 &ohci->regs->hcca); /* reset clears this */
1720 fminterval = 0x2edf;
1721 ohci_writel((fminterval * 9) / 10, &ohci->regs->periodicstart);
1722 fminterval |= ((((fminterval - 210) * 6) / 7) << 16);
1723 ohci_writel(fminterval, &ohci->regs->fminterval);
1724 ohci_writel(0x628, &ohci->regs->lsthresh);
1726 /* start controller operations */
1727 ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
1729 ohci_writel(ohci->hc_control, &ohci->regs->control);
1731 /* disable all interrupts */
1732 mask = (OHCI_INTR_SO | OHCI_INTR_WDH | OHCI_INTR_SF | OHCI_INTR_RD |
1733 OHCI_INTR_UE | OHCI_INTR_FNO | OHCI_INTR_RHSC |
1734 OHCI_INTR_OC | OHCI_INTR_MIE);
1735 ohci_writel(mask, &ohci->regs->intrdisable);
1736 /* clear all interrupts */
1737 mask &= ~OHCI_INTR_MIE;
1738 ohci_writel(mask, &ohci->regs->intrstatus);
1739 /* Choose the interrupts we care about now - but w/o MIE */
1740 mask = OHCI_INTR_RHSC | OHCI_INTR_UE | OHCI_INTR_WDH | OHCI_INTR_SO;
1741 ohci_writel(mask, &ohci->regs->intrenable);
1744 /* required for AMD-756 and some Mac platforms */
1745 ohci_writel((roothub_a(ohci) | RH_A_NPS) & ~RH_A_PSM,
1746 &ohci->regs->roothub.a);
1747 ohci_writel(RH_HS_LPSC, &ohci->regs->roothub.status);
1748 #endif /* OHCI_USE_NPS */
1750 /* connect the virtual root hub */
1751 ohci->rh.devnum = 0;
1756 /*-------------------------------------------------------------------------*/
1758 /* an interrupt happens */
1760 static int hc_interrupt(ohci_t *ohci)
1762 struct ohci_regs *regs = ohci->regs;
1766 invalidate_dcache_hcca(ohci->hcca);
1768 if ((ohci->hcca->done_head != 0) &&
1769 !(m32_swap(ohci->hcca->done_head) & 0x01)) {
1770 ints = OHCI_INTR_WDH;
1772 ints = ohci_readl(®s->intrstatus);
1773 if (ints == ~(u32)0) {
1775 err("%s device removed!", ohci->slot_name);
1778 ints &= ohci_readl(®s->intrenable);
1780 dbg("hc_interrupt: returning..\n");
1786 /* dbg("Interrupt: %x frame: %x", ints,
1787 le16_to_cpu(ohci->hcca->frame_no)); */
1789 if (ints & OHCI_INTR_RHSC)
1792 if (ints & OHCI_INTR_UE) {
1794 err("OHCI Unrecoverable Error, controller usb-%s disabled",
1796 /* e.g. due to PCI Master/Target Abort */
1803 /* FIXME: be optimistic, hope that bug won't repeat often. */
1804 /* Make some non-interrupt context restart the controller. */
1805 /* Count and limit the retries though; either hardware or */
1806 /* software errors can go forever... */
1811 if (ints & OHCI_INTR_WDH) {
1813 ohci_writel(OHCI_INTR_WDH, ®s->intrdisable);
1814 (void)ohci_readl(®s->intrdisable); /* flush */
1815 stat = dl_done_list(ohci);
1816 ohci_writel(OHCI_INTR_WDH, ®s->intrenable);
1817 (void)ohci_readl(®s->intrdisable); /* flush */
1820 if (ints & OHCI_INTR_SO) {
1821 dbg("USB Schedule overrun\n");
1822 ohci_writel(OHCI_INTR_SO, ®s->intrenable);
1826 /* FIXME: this assumes SOF (1/ms) interrupts don't get lost... */
1827 if (ints & OHCI_INTR_SF) {
1828 unsigned int frame = m16_swap(ohci->hcca->frame_no) & 1;
1830 ohci_writel(OHCI_INTR_SF, ®s->intrdisable);
1831 if (ohci->ed_rm_list[frame] != NULL)
1832 ohci_writel(OHCI_INTR_SF, ®s->intrenable);
1836 ohci_writel(ints, ®s->intrstatus);
1840 /*-------------------------------------------------------------------------*/
1842 /*-------------------------------------------------------------------------*/
1844 /* De-allocate all resources.. */
1846 static void hc_release_ohci(ohci_t *ohci)
1848 dbg("USB HC release ohci usb-%s", ohci->slot_name);
1850 if (!ohci->disabled)
1854 /*-------------------------------------------------------------------------*/
1857 * low level initalisation routine, called from usb.c
1859 static char ohci_inited = 0;
1861 int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
1863 #ifdef CONFIG_PCI_OHCI
1867 #ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
1868 /* cpu dependant init */
1873 #ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
1874 /* board dependant init */
1875 if (board_usb_init(index, USB_INIT_HOST))
1878 memset(&gohci, 0, sizeof(ohci_t));
1880 /* align the storage */
1881 if ((__u32)&ghcca[0] & 0xff) {
1882 err("HCCA not aligned!!");
1885 gohci.hcca = &ghcca[0];
1886 info("aligned ghcca %p", gohci.hcca);
1887 memset(gohci.hcca, 0, sizeof(struct ohci_hcca));
1892 #ifdef CONFIG_PCI_OHCI
1893 pdev = pci_find_devices(ohci_pci_ids, CONFIG_PCI_OHCI_DEVNO);
1898 pci_read_config_word(pdev, PCI_VENDOR_ID, &vid);
1899 pci_read_config_word(pdev, PCI_DEVICE_ID, &did);
1900 printf("OHCI pci controller (%04x, %04x) found @(%d:%d:%d)\n",
1901 vid, did, (pdev >> 16) & 0xff,
1902 (pdev >> 11) & 0x1f, (pdev >> 8) & 0x7);
1903 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &base);
1904 printf("OHCI regs address 0x%08x\n", base);
1905 gohci.regs = (struct ohci_regs *)base;
1909 gohci.regs = (struct ohci_regs *)CONFIG_SYS_USB_OHCI_REGS_BASE;
1913 gohci.slot_name = CONFIG_SYS_USB_OHCI_SLOT_NAME;
1915 if (hc_reset (&gohci) < 0) {
1916 hc_release_ohci (&gohci);
1917 err ("can't reset usb-%s", gohci.slot_name);
1918 #ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
1919 /* board dependant cleanup */
1920 board_usb_cleanup(index, USB_INIT_HOST);
1923 #ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
1924 /* cpu dependant cleanup */
1925 usb_cpu_init_fail();
1930 if (hc_start(&gohci) < 0) {
1931 err("can't start usb-%s", gohci.slot_name);
1932 hc_release_ohci(&gohci);
1933 /* Initialization failed */
1934 #ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
1935 /* board dependant cleanup */
1939 #ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
1940 /* cpu dependant cleanup */
1947 ohci_dump(&gohci, 1);
1955 int usb_lowlevel_stop(int index)
1957 /* this gets called really early - before the controller has */
1958 /* even been initialized! */
1961 /* TODO release any interrupts, etc. */
1962 /* call hc_release_ohci() here ? */
1965 #ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
1966 /* board dependant cleanup */
1967 if (usb_board_stop())
1971 #ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
1972 /* cpu dependant cleanup */
1976 /* This driver is no longer initialised. It needs a new low-level
1977 * init (board/cpu) before it can be used again. */
1982 int submit_control_msg(struct usb_device *dev, unsigned long pipe,
1983 void *buffer, int transfer_len, struct devrequest *setup)
1985 return _ohci_submit_control_msg(&gohci, dev, pipe, buffer,
1986 transfer_len, setup);