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 /* this must be aligned to a 256 byte boundary */
109 struct ohci_hcca ghcca[1];
111 /* mapping of the OHCI CC status to error codes */
112 static int cc_to_error[16] = {
114 /* CRC Error */ USB_ST_CRC_ERR,
115 /* Bit Stuff */ USB_ST_BIT_ERR,
116 /* Data Togg */ USB_ST_CRC_ERR,
117 /* Stall */ USB_ST_STALLED,
119 /* PIDCheck */ USB_ST_BIT_ERR,
120 /* UnExpPID */ USB_ST_BIT_ERR,
121 /* DataOver */ USB_ST_BUF_ERR,
122 /* DataUnder */ USB_ST_BUF_ERR,
125 /* BufferOver */ USB_ST_BUF_ERR,
126 /* BuffUnder */ USB_ST_BUF_ERR,
131 static const char *cc_to_string[16] = {
133 "CRC: Last data packet from endpoint contained a CRC error.",
134 "BITSTUFFING: Last data packet from endpoint contained a bit " \
135 "stuffing violation",
136 "DATATOGGLEMISMATCH: Last packet from endpoint had data toggle PID\n" \
137 "that did not match the expected value.",
138 "STALL: TD was moved to the Done Queue because the endpoint returned" \
140 "DEVICENOTRESPONDING: Device did not respond to token (IN) or did\n" \
141 "not provide a handshake (OUT)",
142 "PIDCHECKFAILURE: Check bits on PID from endpoint failed on data PID\n"\
143 "(IN) or handshake (OUT)",
144 "UNEXPECTEDPID: Receive PID was not valid when encountered or PID\n" \
145 "value is not defined.",
146 "DATAOVERRUN: The amount of data returned by the endpoint exceeded\n" \
147 "either the size of the maximum data packet allowed\n" \
148 "from the endpoint (found in MaximumPacketSize field\n" \
149 "of ED) or the remaining buffer size.",
150 "DATAUNDERRUN: The endpoint returned less than MaximumPacketSize\n" \
151 "and that amount was not sufficient to fill the\n" \
155 "BUFFEROVERRUN: During an IN, HC received data from endpoint faster\n" \
156 "than it could be written to system memory",
157 "BUFFERUNDERRUN: During an OUT, HC could not retrieve data from\n" \
158 "system memory fast enough to keep up with data USB " \
160 "NOT ACCESSED: This code is set by software before the TD is placed" \
161 "on a list to be processed by the HC.(1)",
162 "NOT ACCESSED: This code is set by software before the TD is placed" \
163 "on a list to be processed by the HC.(2)",
166 static inline u32 roothub_a(struct ohci *hc)
167 { return ohci_readl(&hc->regs->roothub.a); }
168 static inline u32 roothub_b(struct ohci *hc)
169 { return ohci_readl(&hc->regs->roothub.b); }
170 static inline u32 roothub_status(struct ohci *hc)
171 { return ohci_readl(&hc->regs->roothub.status); }
172 static inline u32 roothub_portstatus(struct ohci *hc, int i)
173 { return ohci_readl(&hc->regs->roothub.portstatus[i]); }
175 /* forward declaration */
176 static int hc_interrupt(ohci_t *ohci);
177 static void td_submit_job(ohci_t *ohci, struct usb_device *dev,
178 unsigned long pipe, void *buffer, int transfer_len,
179 struct devrequest *setup, urb_priv_t *urb,
181 static int ep_link(ohci_t * ohci, ed_t * ed);
182 static int ep_unlink(ohci_t * ohci, ed_t * ed);
183 static ed_t *ep_add_ed(ohci_dev_t *ohci_dev, struct usb_device *usb_dev,
184 unsigned long pipe, int interval, int load);
186 /*-------------------------------------------------------------------------*/
189 static struct td *td_alloc(ohci_dev_t *ohci_dev, struct usb_device *usb_dev)
195 for (i = 0; i < NUM_TD; i++)
197 if (ohci_dev->tds[i].usb_dev == NULL)
199 td = &ohci_dev->tds[i];
200 td->usb_dev = usb_dev;
208 static inline void ed_free(struct ed *ed)
213 /*-------------------------------------------------------------------------*
214 * URB support functions
215 *-------------------------------------------------------------------------*/
217 /* free HCD-private data associated with this URB */
219 static void urb_free_priv(urb_priv_t *urb)
225 last = urb->length - 1;
227 for (i = 0; i <= last; i++) {
238 /*-------------------------------------------------------------------------*/
241 static int sohci_get_current_frame_number(ohci_t *ohci);
243 /* debug| print the main components of an URB
244 * small: 0) header + data packets 1) just header */
246 static void pkt_print(ohci_t *ohci, urb_priv_t *purb, struct usb_device *dev,
247 unsigned long pipe, void *buffer, int transfer_len,
248 struct devrequest *setup, char *str, int small)
250 dbg("%s URB:[%4x] dev:%2lu,ep:%2lu-%c,type:%s,len:%d/%d stat:%#lx",
252 sohci_get_current_frame_number(ohci),
253 usb_pipedevice(pipe),
254 usb_pipeendpoint(pipe),
255 usb_pipeout(pipe)? 'O': 'I',
256 usb_pipetype(pipe) < 2 ? \
257 (usb_pipeint(pipe)? "INTR": "ISOC"): \
258 (usb_pipecontrol(pipe)? "CTRL": "BULK"),
259 (purb ? purb->actual_length : 0),
260 transfer_len, dev->status);
261 #ifdef OHCI_VERBOSE_DEBUG
265 if (usb_pipecontrol(pipe)) {
266 printf(__FILE__ ": cmd(8):");
267 for (i = 0; i < 8 ; i++)
268 printf(" %02x", ((__u8 *) setup) [i]);
271 if (transfer_len > 0 && buffer) {
272 printf(__FILE__ ": data(%d/%d):",
273 (purb ? purb->actual_length : 0),
275 len = usb_pipeout(pipe)? transfer_len:
276 (purb ? purb->actual_length : 0);
277 for (i = 0; i < 16 && i < len; i++)
278 printf(" %02x", ((__u8 *) buffer) [i]);
279 printf("%s\n", i < len? "...": "");
285 /* just for debugging; prints non-empty branches of the int ed tree
286 * inclusive iso eds */
287 void ep_print_int_eds(ohci_t *ohci, char *str)
291 for (i = 0; i < 32; i++) {
293 ed_p = &(ohci->hcca->int_table [i]);
296 printf(__FILE__ ": %s branch int %2d(%2x):", str, i, i);
297 while (*ed_p != 0 && j--) {
298 ed_t *ed = (ed_t *)m32_swap(ed_p);
299 printf(" ed: %4x;", ed->hwINFO);
300 ed_p = &ed->hwNextED;
306 static void ohci_dump_intr_mask(char *label, __u32 mask)
308 dbg("%s: 0x%08x%s%s%s%s%s%s%s%s%s",
311 (mask & OHCI_INTR_MIE) ? " MIE" : "",
312 (mask & OHCI_INTR_OC) ? " OC" : "",
313 (mask & OHCI_INTR_RHSC) ? " RHSC" : "",
314 (mask & OHCI_INTR_FNO) ? " FNO" : "",
315 (mask & OHCI_INTR_UE) ? " UE" : "",
316 (mask & OHCI_INTR_RD) ? " RD" : "",
317 (mask & OHCI_INTR_SF) ? " SF" : "",
318 (mask & OHCI_INTR_WDH) ? " WDH" : "",
319 (mask & OHCI_INTR_SO) ? " SO" : ""
323 static void maybe_print_eds(char *label, __u32 value)
325 ed_t *edp = (ed_t *)value;
328 dbg("%s %08x", label, value);
329 dbg("%08x", edp->hwINFO);
330 dbg("%08x", edp->hwTailP);
331 dbg("%08x", edp->hwHeadP);
332 dbg("%08x", edp->hwNextED);
336 static char *hcfs2string(int state)
339 case OHCI_USB_RESET: return "reset";
340 case OHCI_USB_RESUME: return "resume";
341 case OHCI_USB_OPER: return "operational";
342 case OHCI_USB_SUSPEND: return "suspend";
347 /* dump control and status registers */
348 static void ohci_dump_status(ohci_t *controller)
350 struct ohci_regs *regs = controller->regs;
353 temp = ohci_readl(®s->revision) & 0xff;
355 dbg("spec %d.%d", (temp >> 4), (temp & 0x0f));
357 temp = ohci_readl(®s->control);
358 dbg("control: 0x%08x%s%s%s HCFS=%s%s%s%s%s CBSR=%d", temp,
359 (temp & OHCI_CTRL_RWE) ? " RWE" : "",
360 (temp & OHCI_CTRL_RWC) ? " RWC" : "",
361 (temp & OHCI_CTRL_IR) ? " IR" : "",
362 hcfs2string(temp & OHCI_CTRL_HCFS),
363 (temp & OHCI_CTRL_BLE) ? " BLE" : "",
364 (temp & OHCI_CTRL_CLE) ? " CLE" : "",
365 (temp & OHCI_CTRL_IE) ? " IE" : "",
366 (temp & OHCI_CTRL_PLE) ? " PLE" : "",
367 temp & OHCI_CTRL_CBSR
370 temp = ohci_readl(®s->cmdstatus);
371 dbg("cmdstatus: 0x%08x SOC=%d%s%s%s%s", temp,
372 (temp & OHCI_SOC) >> 16,
373 (temp & OHCI_OCR) ? " OCR" : "",
374 (temp & OHCI_BLF) ? " BLF" : "",
375 (temp & OHCI_CLF) ? " CLF" : "",
376 (temp & OHCI_HCR) ? " HCR" : ""
379 ohci_dump_intr_mask("intrstatus", ohci_readl(®s->intrstatus));
380 ohci_dump_intr_mask("intrenable", ohci_readl(®s->intrenable));
382 maybe_print_eds("ed_periodcurrent",
383 ohci_readl(®s->ed_periodcurrent));
385 maybe_print_eds("ed_controlhead", ohci_readl(®s->ed_controlhead));
386 maybe_print_eds("ed_controlcurrent",
387 ohci_readl(®s->ed_controlcurrent));
389 maybe_print_eds("ed_bulkhead", ohci_readl(®s->ed_bulkhead));
390 maybe_print_eds("ed_bulkcurrent", ohci_readl(®s->ed_bulkcurrent));
392 maybe_print_eds("donehead", ohci_readl(®s->donehead));
395 static void ohci_dump_roothub(ohci_t *controller, int verbose)
399 temp = roothub_a(controller);
400 ndp = (temp & RH_A_NDP);
401 #ifdef CONFIG_AT91C_PQFP_UHPBUG
402 ndp = (ndp == 2) ? 1:0;
405 dbg("roothub.a: %08x POTPGT=%d%s%s%s%s%s NDP=%d", temp,
406 ((temp & RH_A_POTPGT) >> 24) & 0xff,
407 (temp & RH_A_NOCP) ? " NOCP" : "",
408 (temp & RH_A_OCPM) ? " OCPM" : "",
409 (temp & RH_A_DT) ? " DT" : "",
410 (temp & RH_A_NPS) ? " NPS" : "",
411 (temp & RH_A_PSM) ? " PSM" : "",
414 temp = roothub_b(controller);
415 dbg("roothub.b: %08x PPCM=%04x DR=%04x",
417 (temp & RH_B_PPCM) >> 16,
420 temp = roothub_status(controller);
421 dbg("roothub.status: %08x%s%s%s%s%s%s",
423 (temp & RH_HS_CRWE) ? " CRWE" : "",
424 (temp & RH_HS_OCIC) ? " OCIC" : "",
425 (temp & RH_HS_LPSC) ? " LPSC" : "",
426 (temp & RH_HS_DRWE) ? " DRWE" : "",
427 (temp & RH_HS_OCI) ? " OCI" : "",
428 (temp & RH_HS_LPS) ? " LPS" : ""
432 for (i = 0; i < ndp; i++) {
433 temp = roothub_portstatus(controller, i);
434 dbg("roothub.portstatus [%d] = 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s",
437 (temp & RH_PS_PRSC) ? " PRSC" : "",
438 (temp & RH_PS_OCIC) ? " OCIC" : "",
439 (temp & RH_PS_PSSC) ? " PSSC" : "",
440 (temp & RH_PS_PESC) ? " PESC" : "",
441 (temp & RH_PS_CSC) ? " CSC" : "",
443 (temp & RH_PS_LSDA) ? " LSDA" : "",
444 (temp & RH_PS_PPS) ? " PPS" : "",
445 (temp & RH_PS_PRS) ? " PRS" : "",
446 (temp & RH_PS_POCI) ? " POCI" : "",
447 (temp & RH_PS_PSS) ? " PSS" : "",
449 (temp & RH_PS_PES) ? " PES" : "",
450 (temp & RH_PS_CCS) ? " CCS" : ""
455 static void ohci_dump(ohci_t *controller, int verbose)
457 dbg("OHCI controller usb-%s state", controller->slot_name);
459 /* dumps some of the state we know about */
460 ohci_dump_status(controller);
462 ep_print_int_eds(controller, "hcca");
463 dbg("hcca frame #%04x", controller->hcca->frame_no);
464 ohci_dump_roothub(controller, 1);
468 /*-------------------------------------------------------------------------*
469 * Interface functions (URB)
470 *-------------------------------------------------------------------------*/
472 /* get a transfer request */
474 int sohci_submit_job(ohci_t *ohci, ohci_dev_t *ohci_dev, urb_priv_t *urb,
475 struct devrequest *setup)
478 urb_priv_t *purb_priv = urb;
480 struct usb_device *dev = urb->dev;
481 unsigned long pipe = urb->pipe;
482 void *buffer = urb->transfer_buffer;
483 int transfer_len = urb->transfer_buffer_length;
484 int interval = urb->interval;
486 /* when controller's hung, permit only roothub cleanup attempts
487 * such as powering down ports */
488 if (ohci->disabled) {
489 err("sohci_submit_job: EPIPE");
493 /* we're about to begin a new transaction here so mark the
497 /* every endpoint has a ed, locate and fill it */
498 ed = ep_add_ed(ohci_dev, dev, pipe, interval, 1);
500 err("sohci_submit_job: ENOMEM");
504 /* for the private part of the URB we need the number of TDs (size) */
505 switch (usb_pipetype(pipe)) {
506 case PIPE_BULK: /* one TD for every 4096 Byte */
507 size = (transfer_len - 1) / 4096 + 1;
509 case PIPE_CONTROL:/* 1 TD for setup, 1 for ACK and 1 for every 4096 B */
510 size = (transfer_len == 0)? 2:
511 (transfer_len - 1) / 4096 + 3;
513 case PIPE_INTERRUPT: /* 1 TD */
520 if (size >= (N_URB_TD - 1)) {
521 err("need %d TDs, only have %d", size, N_URB_TD);
524 purb_priv->pipe = pipe;
526 /* fill the private part of the URB */
527 purb_priv->length = size;
529 purb_priv->actual_length = 0;
531 /* allocate the TDs */
532 /* note that td[0] was allocated in ep_add_ed */
533 for (i = 0; i < size; i++) {
534 purb_priv->td[i] = td_alloc(ohci_dev, dev);
535 if (!purb_priv->td[i]) {
536 purb_priv->length = i;
537 urb_free_priv(purb_priv);
538 err("sohci_submit_job: ENOMEM");
543 if (ed->state == ED_NEW || (ed->state & ED_DEL)) {
544 urb_free_priv(purb_priv);
545 err("sohci_submit_job: EINVAL");
549 /* link the ed into a chain if is not already */
550 if (ed->state != ED_OPER)
553 /* fill the TDs and link it to the ed */
554 td_submit_job(ohci, dev, pipe, buffer, transfer_len,
555 setup, purb_priv, interval);
560 static inline int sohci_return_job(struct ohci *hc, urb_priv_t *urb)
562 struct ohci_regs *regs = hc->regs;
564 switch (usb_pipetype(urb->pipe)) {
566 /* implicitly requeued */
567 if (urb->dev->irq_handle &&
568 (urb->dev->irq_act_len = urb->actual_length)) {
569 ohci_writel(OHCI_INTR_WDH, ®s->intrenable);
570 ohci_readl(®s->intrenable); /* PCI posting flush */
571 urb->dev->irq_handle(urb->dev);
572 ohci_writel(OHCI_INTR_WDH, ®s->intrdisable);
573 ohci_readl(®s->intrdisable); /* PCI posting flush */
575 urb->actual_length = 0;
579 urb->transfer_buffer,
580 urb->transfer_buffer_length,
594 /*-------------------------------------------------------------------------*/
597 /* tell us the current USB frame number */
598 static int sohci_get_current_frame_number(ohci_t *ohci)
600 return m16_swap(ohci->hcca->frame_no);
604 /*-------------------------------------------------------------------------*
605 * ED handling functions
606 *-------------------------------------------------------------------------*/
608 /* search for the right branch to insert an interrupt ed into the int tree
609 * do some load ballancing;
610 * returns the branch and
611 * sets the interval to interval = 2^integer (ld (interval)) */
613 static int ep_int_ballance(ohci_t *ohci, int interval, int load)
617 /* search for the least loaded interrupt endpoint
618 * branch of all 32 branches
620 for (i = 0; i < 32; i++)
621 if (ohci->ohci_int_load [branch] > ohci->ohci_int_load [i])
624 branch = branch % interval;
625 for (i = branch; i < 32; i += interval)
626 ohci->ohci_int_load [i] += load;
631 /*-------------------------------------------------------------------------*/
633 /* 2^int( ld (inter)) */
635 static int ep_2_n_interval(int inter)
638 for (i = 0; ((inter >> i) > 1) && (i < 5); i++);
642 /*-------------------------------------------------------------------------*/
644 /* the int tree is a binary tree
645 * in order to process it sequentially the indexes of the branches have to
646 * be mapped the mapping reverses the bits of a word of num_bits length */
647 static int ep_rev(int num_bits, int word)
651 for (i = 0; i < num_bits; i++)
652 wout |= (((word >> i) & 1) << (num_bits - i - 1));
656 /*-------------------------------------------------------------------------*
657 * ED handling functions
658 *-------------------------------------------------------------------------*/
660 /* link an ed into one of the HC chains */
662 static int ep_link(ohci_t *ohci, ed_t *edi)
664 volatile ed_t *ed = edi;
673 ed->int_interval = 0;
678 if (ohci->ed_controltail == NULL)
679 ohci_writel(ed, &ohci->regs->ed_controlhead);
681 ohci->ed_controltail->hwNextED =
682 m32_swap((unsigned long)ed);
684 ed->ed_prev = ohci->ed_controltail;
685 if (!ohci->ed_controltail && !ohci->ed_rm_list[0] &&
686 !ohci->ed_rm_list[1] && !ohci->sleeping) {
687 ohci->hc_control |= OHCI_CTRL_CLE;
688 ohci_writel(ohci->hc_control, &ohci->regs->control);
690 ohci->ed_controltail = edi;
695 if (ohci->ed_bulktail == NULL)
696 ohci_writel(ed, &ohci->regs->ed_bulkhead);
698 ohci->ed_bulktail->hwNextED =
699 m32_swap((unsigned long)ed);
701 ed->ed_prev = ohci->ed_bulktail;
702 if (!ohci->ed_bulktail && !ohci->ed_rm_list[0] &&
703 !ohci->ed_rm_list[1] && !ohci->sleeping) {
704 ohci->hc_control |= OHCI_CTRL_BLE;
705 ohci_writel(ohci->hc_control, &ohci->regs->control);
707 ohci->ed_bulktail = edi;
712 interval = ep_2_n_interval(ed->int_period);
713 ed->int_interval = interval;
714 int_branch = ep_int_ballance(ohci, interval, load);
715 ed->int_branch = int_branch;
717 for (i = 0; i < ep_rev(6, interval); i += inter) {
719 for (ed_p = &(ohci->hcca->int_table[\
720 ep_rev(5, i) + int_branch]);
722 (((ed_t *)ed_p)->int_interval >= interval);
723 ed_p = &(((ed_t *)ed_p)->hwNextED))
725 ((ed_t *)ed_p)->int_interval);
726 ed->hwNextED = *ed_p;
727 *ed_p = m32_swap((unsigned long)ed);
734 /*-------------------------------------------------------------------------*/
736 /* scan the periodic table to find and unlink this ED */
737 static void periodic_unlink(struct ohci *ohci, volatile struct ed *ed,
738 unsigned index, unsigned period)
740 for (; index < NUM_INTS; index += period) {
741 __u32 *ed_p = &ohci->hcca->int_table [index];
743 /* ED might have been unlinked through another path */
746 m32_swap((unsigned long)ed_p)) == ed) {
747 *ed_p = ed->hwNextED;
750 ed_p = &(((struct ed *)
751 m32_swap((unsigned long)ed_p))->hwNextED);
756 /* unlink an ed from one of the HC chains.
757 * just the link to the ed is unlinked.
758 * the link from the ed still points to another operational ed or 0
759 * so the HC can eventually finish the processing of the unlinked ed */
761 static int ep_unlink(ohci_t *ohci, ed_t *edi)
763 volatile ed_t *ed = edi;
766 ed->hwINFO |= m32_swap(OHCI_ED_SKIP);
770 if (ed->ed_prev == NULL) {
772 ohci->hc_control &= ~OHCI_CTRL_CLE;
773 ohci_writel(ohci->hc_control,
774 &ohci->regs->control);
776 ohci_writel(m32_swap(*((__u32 *)&ed->hwNextED)),
777 &ohci->regs->ed_controlhead);
779 ed->ed_prev->hwNextED = ed->hwNextED;
781 if (ohci->ed_controltail == ed) {
782 ohci->ed_controltail = ed->ed_prev;
785 *((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
790 if (ed->ed_prev == NULL) {
792 ohci->hc_control &= ~OHCI_CTRL_BLE;
793 ohci_writel(ohci->hc_control,
794 &ohci->regs->control);
796 ohci_writel(m32_swap(*((__u32 *)&ed->hwNextED)),
797 &ohci->regs->ed_bulkhead);
799 ed->ed_prev->hwNextED = ed->hwNextED;
801 if (ohci->ed_bulktail == ed) {
802 ohci->ed_bulktail = ed->ed_prev;
805 *((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
810 periodic_unlink(ohci, ed, 0, 1);
811 for (i = ed->int_branch; i < 32; i += ed->int_interval)
812 ohci->ohci_int_load[i] -= ed->int_load;
815 ed->state = ED_UNLINK;
819 /*-------------------------------------------------------------------------*/
821 /* add/reinit an endpoint; this should be done once at the
822 * usb_set_configuration command, but the USB stack is a little bit
823 * stateless so we do it at every transaction if the state of the ed
824 * is ED_NEW then a dummy td is added and the state is changed to
825 * ED_UNLINK in all other cases the state is left unchanged the ed
826 * info fields are setted anyway even though most of them should not
829 static ed_t *ep_add_ed(ohci_dev_t *ohci_dev, struct usb_device *usb_dev,
830 unsigned long pipe, int interval, int load)
836 ed = ed_ret = &ohci_dev->ed[(usb_pipeendpoint(pipe) << 1) |
837 (usb_pipecontrol(pipe)? 0: usb_pipeout(pipe))];
839 if ((ed->state & ED_DEL) || (ed->state & ED_URB_DEL)) {
840 err("ep_add_ed: pending delete");
841 /* pending delete request */
845 if (ed->state == ED_NEW) {
846 /* dummy td; end of td list for ed */
847 td = td_alloc(ohci_dev, usb_dev);
848 ed->hwTailP = m32_swap((unsigned long)td);
849 ed->hwHeadP = ed->hwTailP;
850 ed->state = ED_UNLINK;
851 ed->type = usb_pipetype(pipe);
855 ed->hwINFO = m32_swap(usb_pipedevice(pipe)
856 | usb_pipeendpoint(pipe) << 7
857 | (usb_pipeisoc(pipe)? 0x8000: 0)
858 | (usb_pipecontrol(pipe)? 0: \
859 (usb_pipeout(pipe)? 0x800: 0x1000))
860 | (usb_dev->speed == USB_SPEED_LOW) << 13
861 | usb_maxpacket(usb_dev, pipe) << 16);
863 if (ed->type == PIPE_INTERRUPT && ed->state == ED_UNLINK) {
864 ed->int_period = interval;
871 /*-------------------------------------------------------------------------*
872 * TD handling functions
873 *-------------------------------------------------------------------------*/
875 /* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
877 static void td_fill(ohci_t *ohci, unsigned int info,
879 struct usb_device *dev, int index, urb_priv_t *urb_priv)
881 volatile td_t *td, *td_pt;
882 #ifdef OHCI_FILL_TRACE
886 if (index > urb_priv->length) {
887 err("index > length");
890 /* use this td as the next dummy */
891 td_pt = urb_priv->td [index];
894 /* fill the old dummy TD */
895 td = urb_priv->td [index] =
896 (td_t *)(m32_swap(urb_priv->ed->hwTailP) & ~0xf);
898 td->ed = urb_priv->ed;
899 td->next_dl_td = NULL;
901 td->data = (__u32)data;
902 #ifdef OHCI_FILL_TRACE
903 if (usb_pipebulk(urb_priv->pipe) && usb_pipeout(urb_priv->pipe)) {
904 for (i = 0; i < len; i++)
905 printf("td->data[%d] %#2x ", i, ((unsigned char *)td->data)[i]);
912 td->hwINFO = m32_swap(info);
913 td->hwCBP = m32_swap((unsigned long)data);
915 td->hwBE = m32_swap((unsigned long)(data + len - 1));
919 td->hwNextTD = m32_swap((unsigned long)td_pt);
921 /* append to queue */
922 td->ed->hwTailP = td->hwNextTD;
925 /*-------------------------------------------------------------------------*/
927 /* prepare all TDs of a transfer */
929 static void td_submit_job(ohci_t *ohci, struct usb_device *dev,
930 unsigned long pipe, void *buffer, int transfer_len,
931 struct devrequest *setup, urb_priv_t *urb,
934 int data_len = transfer_len;
938 unsigned int toggle = 0;
940 /* OHCI handles the DATA-toggles itself, we just use the USB-toggle
941 * bits for reseting */
942 if (usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe))) {
943 toggle = TD_T_TOGGLE;
946 usb_settoggle(dev, usb_pipeendpoint(pipe),
947 usb_pipeout(pipe), 1);
955 switch (usb_pipetype(pipe)) {
957 info = usb_pipeout(pipe)?
958 TD_CC | TD_DP_OUT : TD_CC | TD_DP_IN ;
959 while (data_len > 4096) {
960 td_fill(ohci, info | (cnt? TD_T_TOGGLE:toggle),
961 data, 4096, dev, cnt, urb);
962 data += 4096; data_len -= 4096; cnt++;
964 info = usb_pipeout(pipe)?
965 TD_CC | TD_DP_OUT : TD_CC | TD_R | TD_DP_IN ;
966 td_fill(ohci, info | (cnt? TD_T_TOGGLE:toggle), data,
967 data_len, dev, cnt, urb);
970 if (!ohci->sleeping) {
971 /* start bulk list */
972 ohci_writel(OHCI_BLF, &ohci->regs->cmdstatus);
978 info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
979 td_fill(ohci, info, setup, 8, dev, cnt++, urb);
981 /* Optional Data phase */
983 info = usb_pipeout(pipe)?
984 TD_CC | TD_R | TD_DP_OUT | TD_T_DATA1 :
985 TD_CC | TD_R | TD_DP_IN | TD_T_DATA1;
986 /* NOTE: mishandles transfers >8K, some >4K */
987 td_fill(ohci, info, data, data_len, dev, cnt++, urb);
991 info = (usb_pipeout(pipe) || data_len == 0) ?
992 TD_CC | TD_DP_IN | TD_T_DATA1:
993 TD_CC | TD_DP_OUT | TD_T_DATA1;
994 td_fill(ohci, info, data, 0, dev, cnt++, urb);
996 if (!ohci->sleeping) {
997 /* start Control list */
998 ohci_writel(OHCI_CLF, &ohci->regs->cmdstatus);
1002 case PIPE_INTERRUPT:
1003 info = usb_pipeout(urb->pipe)?
1004 TD_CC | TD_DP_OUT | toggle:
1005 TD_CC | TD_R | TD_DP_IN | toggle;
1006 td_fill(ohci, info, data, data_len, dev, cnt++, urb);
1009 if (urb->length != cnt)
1010 dbg("TD LENGTH %d != CNT %d", urb->length, cnt);
1013 /*-------------------------------------------------------------------------*
1014 * Done List handling functions
1015 *-------------------------------------------------------------------------*/
1017 /* calculate the transfer length and update the urb */
1019 static void dl_transfer_length(td_t *td)
1022 urb_priv_t *lurb_priv = td->ed->purb;
1024 tdBE = m32_swap(td->hwBE);
1025 tdCBP = m32_swap(td->hwCBP);
1027 if (!(usb_pipecontrol(lurb_priv->pipe) &&
1028 ((td->index == 0) || (td->index == lurb_priv->length - 1)))) {
1031 lurb_priv->actual_length += tdBE - td->data + 1;
1033 lurb_priv->actual_length += tdCBP - td->data;
1038 /*-------------------------------------------------------------------------*/
1039 static void check_status(td_t *td_list)
1041 urb_priv_t *lurb_priv = td_list->ed->purb;
1042 int urb_len = lurb_priv->length;
1043 __u32 *phwHeadP = &td_list->ed->hwHeadP;
1046 cc = TD_CC_GET(m32_swap(td_list->hwINFO));
1048 err(" USB-error: %s (%x)", cc_to_string[cc], cc);
1050 if (*phwHeadP & m32_swap(0x1)) {
1052 ((td_list->index + 1) < urb_len)) {
1054 (lurb_priv->td[urb_len - 1]->hwNextTD &\
1055 m32_swap(0xfffffff0)) |
1056 (*phwHeadP & m32_swap(0x2));
1058 lurb_priv->td_cnt += urb_len -
1061 *phwHeadP &= m32_swap(0xfffffff2);
1063 #ifdef CONFIG_MPC5200
1064 td_list->hwNextTD = 0;
1069 /* replies to the request have to be on a FIFO basis so
1070 * we reverse the reversed done-list */
1071 static td_t *dl_reverse_done_list(ohci_t *ohci)
1074 td_t *td_rev = NULL;
1075 td_t *td_list = NULL;
1077 td_list_hc = m32_swap(ohci->hcca->done_head) & 0xfffffff0;
1078 ohci->hcca->done_head = 0;
1080 while (td_list_hc) {
1081 td_list = (td_t *)td_list_hc;
1082 check_status(td_list);
1083 td_list->next_dl_td = td_rev;
1085 td_list_hc = m32_swap(td_list->hwNextTD) & 0xfffffff0;
1090 /*-------------------------------------------------------------------------*/
1091 /*-------------------------------------------------------------------------*/
1093 static void finish_urb(ohci_t *ohci, urb_priv_t *urb, int status)
1095 if ((status & (ED_OPER | ED_UNLINK)) && (urb->state != URB_DEL))
1096 urb->finished = sohci_return_job(ohci, urb);
1098 dbg("finish_urb: strange.., ED state %x, \n", status);
1102 * Used to take back a TD from the host controller. This would normally be
1103 * called from within dl_done_list, however it may be called directly if the
1104 * HC no longer sees the TD and it has not appeared on the donelist (after
1105 * two frames). This bug has been observed on ZF Micro systems.
1107 static int takeback_td(ohci_t *ohci, td_t *td_list)
1113 urb_priv_t *lurb_priv;
1114 __u32 tdINFO, edHeadP, edTailP;
1116 tdINFO = m32_swap(td_list->hwINFO);
1119 lurb_priv = ed->purb;
1121 dl_transfer_length(td_list);
1123 lurb_priv->td_cnt++;
1125 /* error code of transfer */
1126 cc = TD_CC_GET(tdINFO);
1128 err("USB-error: %s (%x)", cc_to_string[cc], cc);
1129 stat = cc_to_error[cc];
1132 /* see if this done list makes for all TD's of current URB,
1133 * and mark the URB finished if so */
1134 if (lurb_priv->td_cnt == lurb_priv->length)
1135 finish_urb(ohci, lurb_priv, ed->state);
1137 dbg("dl_done_list: processing TD %x, len %x\n",
1138 lurb_priv->td_cnt, lurb_priv->length);
1140 if (ed->state != ED_NEW && (!usb_pipeint(lurb_priv->pipe))) {
1141 edHeadP = m32_swap(ed->hwHeadP) & 0xfffffff0;
1142 edTailP = m32_swap(ed->hwTailP);
1144 /* unlink eds if they are not busy */
1145 if ((edHeadP == edTailP) && (ed->state == ED_OPER))
1146 ep_unlink(ohci, ed);
1151 static int dl_done_list(ohci_t *ohci)
1154 td_t *td_list = dl_reverse_done_list(ohci);
1157 td_t *td_next = td_list->next_dl_td;
1158 stat = takeback_td(ohci, td_list);
1164 /*-------------------------------------------------------------------------*
1166 *-------------------------------------------------------------------------*/
1168 #include <usbroothubdes.h>
1170 /* Hub class-specific descriptor is constructed dynamically */
1172 /*-------------------------------------------------------------------------*/
1174 #define OK(x) len = (x); break
1176 #define WR_RH_STAT(x) {info("WR:status %#8x", (x)); ohci_writel((x), \
1177 &ohci->regs->roothub.status); }
1178 #define WR_RH_PORTSTAT(x) {info("WR:portstatus[%d] %#8x", wIndex-1, \
1179 (x)); ohci_writel((x), &ohci->regs->roothub.portstatus[wIndex-1]); }
1181 #define WR_RH_STAT(x) ohci_writel((x), &ohci->regs->roothub.status)
1182 #define WR_RH_PORTSTAT(x) ohci_writel((x), \
1183 &ohci->regs->roothub.portstatus[wIndex-1])
1185 #define RD_RH_STAT roothub_status(ohci)
1186 #define RD_RH_PORTSTAT roothub_portstatus(ohci, wIndex-1)
1188 /* request to virtual root hub */
1190 int rh_check_port_status(ohci_t *controller)
1196 temp = roothub_a(controller);
1197 ndp = (temp & RH_A_NDP);
1198 #ifdef CONFIG_AT91C_PQFP_UHPBUG
1199 ndp = (ndp == 2) ? 1:0;
1201 for (i = 0; i < ndp; i++) {
1202 temp = roothub_portstatus(controller, i);
1203 /* check for a device disconnect */
1204 if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
1205 (RH_PS_PESC | RH_PS_CSC)) &&
1206 ((temp & RH_PS_CCS) == 0)) {
1214 static int ohci_submit_rh_msg(ohci_t *ohci, struct usb_device *dev,
1215 unsigned long pipe, void *buffer, int transfer_len,
1216 struct devrequest *cmd)
1218 void *data = buffer;
1219 int leni = transfer_len;
1226 ALLOC_ALIGN_BUFFER(__u8, databuf, 16, sizeof(u32));
1229 pkt_print(ohci, NULL, dev, pipe, buffer, transfer_len,
1230 cmd, "SUB(rh)", usb_pipein(pipe));
1234 if (usb_pipeint(pipe)) {
1235 info("Root-Hub submit IRQ: NOT implemented");
1239 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
1240 wValue = le16_to_cpu(cmd->value);
1241 wIndex = le16_to_cpu(cmd->index);
1242 wLength = le16_to_cpu(cmd->length);
1244 info("Root-Hub: adr: %2x cmd(%1x): %08x %04x %04x %04x",
1245 dev->devnum, 8, bmRType_bReq, wValue, wIndex, wLength);
1247 switch (bmRType_bReq) {
1248 /* Request Destination:
1249 without flags: Device,
1250 RH_INTERFACE: interface,
1251 RH_ENDPOINT: endpoint,
1252 RH_CLASS means HUB here,
1253 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1257 *(u16 *)databuf = cpu_to_le16(1);
1259 case RH_GET_STATUS | RH_INTERFACE:
1260 *(u16 *)databuf = cpu_to_le16(0);
1262 case RH_GET_STATUS | RH_ENDPOINT:
1263 *(u16 *)databuf = cpu_to_le16(0);
1265 case RH_GET_STATUS | RH_CLASS:
1266 *(u32 *)databuf = cpu_to_le32(
1267 RD_RH_STAT & ~(RH_HS_CRWE | RH_HS_DRWE));
1269 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
1270 *(u32 *)databuf = cpu_to_le32(RD_RH_PORTSTAT);
1273 case RH_CLEAR_FEATURE | RH_ENDPOINT:
1275 case (RH_ENDPOINT_STALL):
1280 case RH_CLEAR_FEATURE | RH_CLASS:
1282 case RH_C_HUB_LOCAL_POWER:
1284 case (RH_C_HUB_OVER_CURRENT):
1285 WR_RH_STAT(RH_HS_OCIC);
1290 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1292 case (RH_PORT_ENABLE): WR_RH_PORTSTAT(RH_PS_CCS); OK(0);
1293 case (RH_PORT_SUSPEND): WR_RH_PORTSTAT(RH_PS_POCI); OK(0);
1294 case (RH_PORT_POWER): WR_RH_PORTSTAT(RH_PS_LSDA); OK(0);
1295 case (RH_C_PORT_CONNECTION): WR_RH_PORTSTAT(RH_PS_CSC); OK(0);
1296 case (RH_C_PORT_ENABLE): WR_RH_PORTSTAT(RH_PS_PESC); OK(0);
1297 case (RH_C_PORT_SUSPEND): WR_RH_PORTSTAT(RH_PS_PSSC); OK(0);
1298 case (RH_C_PORT_OVER_CURRENT):WR_RH_PORTSTAT(RH_PS_OCIC); OK(0);
1299 case (RH_C_PORT_RESET): WR_RH_PORTSTAT(RH_PS_PRSC); OK(0);
1303 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1305 case (RH_PORT_SUSPEND):
1306 WR_RH_PORTSTAT(RH_PS_PSS); OK(0);
1307 case (RH_PORT_RESET): /* BUG IN HUP CODE *********/
1308 if (RD_RH_PORTSTAT & RH_PS_CCS)
1309 WR_RH_PORTSTAT(RH_PS_PRS);
1311 case (RH_PORT_POWER):
1312 WR_RH_PORTSTAT(RH_PS_PPS);
1315 case (RH_PORT_ENABLE): /* BUG IN HUP CODE *********/
1316 if (RD_RH_PORTSTAT & RH_PS_CCS)
1317 WR_RH_PORTSTAT(RH_PS_PES);
1322 case RH_SET_ADDRESS:
1323 ohci->rh.devnum = wValue;
1326 case RH_GET_DESCRIPTOR:
1327 switch ((wValue & 0xff00) >> 8) {
1328 case (0x01): /* device descriptor */
1329 len = min_t(unsigned int,
1332 sizeof(root_hub_dev_des),
1334 databuf = root_hub_dev_des; OK(len);
1335 case (0x02): /* configuration descriptor */
1336 len = min_t(unsigned int,
1339 sizeof(root_hub_config_des),
1341 databuf = root_hub_config_des; OK(len);
1342 case (0x03): /* string descriptors */
1343 if (wValue == 0x0300) {
1344 len = min_t(unsigned int,
1347 sizeof(root_hub_str_index0),
1349 databuf = root_hub_str_index0;
1352 if (wValue == 0x0301) {
1353 len = min_t(unsigned int,
1356 sizeof(root_hub_str_index1),
1358 databuf = root_hub_str_index1;
1362 stat = USB_ST_STALLED;
1366 case RH_GET_DESCRIPTOR | RH_CLASS:
1368 __u32 temp = roothub_a(ohci);
1370 databuf[0] = 9; /* min length; */
1372 databuf[2] = temp & RH_A_NDP;
1373 #ifdef CONFIG_AT91C_PQFP_UHPBUG
1374 databuf[2] = (databuf[2] == 2) ? 1 : 0;
1377 if (temp & RH_A_PSM) /* per-port power switching? */
1379 if (temp & RH_A_NOCP) /* no overcurrent reporting? */
1381 else if (temp & RH_A_OCPM)/* per-port overcurrent reporting? */
1385 databuf[5] = (temp & RH_A_POTPGT) >> 24;
1387 temp = roothub_b(ohci);
1388 databuf[7] = temp & RH_B_DR;
1389 if (databuf[2] < 7) {
1393 databuf[8] = (temp & RH_B_DR) >> 8;
1394 databuf[10] = databuf[9] = 0xff;
1397 len = min_t(unsigned int, leni,
1398 min_t(unsigned int, databuf[0], wLength));
1402 case RH_GET_CONFIGURATION:
1406 case RH_SET_CONFIGURATION:
1407 WR_RH_STAT(0x10000);
1411 dbg("unsupported root hub command");
1412 stat = USB_ST_STALLED;
1416 ohci_dump_roothub(ohci, 1);
1421 len = min_t(int, len, leni);
1422 if (data != databuf)
1423 memcpy(data, databuf, len);
1428 pkt_print(ohci, NULL, dev, pipe, buffer,
1429 transfer_len, cmd, "RET(rh)", 0/*usb_pipein(pipe)*/);
1437 /*-------------------------------------------------------------------------*/
1439 /* common code for handling submit messages - used for all but root hub */
1441 static int submit_common_msg(ohci_t *ohci, struct usb_device *dev,
1442 unsigned long pipe, void *buffer, int transfer_len,
1443 struct devrequest *setup, int interval)
1446 int maxsize = usb_maxpacket(dev, pipe);
1450 urb = malloc(sizeof(urb_priv_t));
1451 memset(urb, 0, sizeof(urb_priv_t));
1455 urb->transfer_buffer = buffer;
1456 urb->transfer_buffer_length = transfer_len;
1457 urb->interval = interval;
1460 urb->actual_length = 0;
1461 pkt_print(ohci, urb, dev, pipe, buffer, transfer_len,
1462 setup, "SUB", usb_pipein(pipe));
1467 err("submit_common_message: pipesize for pipe %lx is zero",
1472 if (sohci_submit_job(ohci, &ohci->ohci_dev, urb, setup) < 0) {
1473 err("sohci_submit_job failed");
1479 /* ohci_dump_status(ohci); */
1482 timeout = USB_TIMEOUT_MS(pipe);
1484 /* wait for it to complete */
1486 /* check whether the controller is done */
1487 stat = hc_interrupt(ohci);
1489 stat = USB_ST_CRC_ERR;
1493 /* NOTE: since we are not interrupt driven in U-Boot and always
1494 * handle only one URB at a time, we cannot assume the
1495 * transaction finished on the first successful return from
1496 * hc_interrupt().. unless the flag for current URB is set,
1497 * meaning that all TD's to/from device got actually
1498 * transferred and processed. If the current URB is not
1499 * finished we need to re-iterate this loop so as
1500 * hc_interrupt() gets called again as there needs to be some
1501 * more TD's to process still */
1502 if ((stat >= 0) && (stat != 0xff) && (urb->finished)) {
1503 /* 0xff is returned for an SF-interrupt */
1513 err("CTL:TIMEOUT ");
1514 dbg("submit_common_msg: TO status %x\n", stat);
1516 stat = USB_ST_CRC_ERR;
1522 dev->act_len = urb->actual_length;
1525 pkt_print(ohci, urb, dev, pipe, buffer, transfer_len,
1526 setup, "RET(ctlr)", usb_pipein(pipe));
1531 /* free TDs in urb_priv */
1532 if (!usb_pipeint(pipe))
1537 /* submit routines called from usb.c */
1538 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1541 info("submit_bulk_msg");
1542 return submit_common_msg(&gohci, dev, pipe, buffer, transfer_len,
1546 int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1547 int transfer_len, int interval)
1549 info("submit_int_msg");
1550 return submit_common_msg(&gohci, dev, pipe, buffer, transfer_len, NULL,
1554 static int _ohci_submit_control_msg(ohci_t *ohci, struct usb_device *dev,
1555 unsigned long pipe, void *buffer, int transfer_len,
1556 struct devrequest *setup)
1558 int maxsize = usb_maxpacket(dev, pipe);
1560 info("submit_control_msg");
1562 pkt_print(ohci, NULL, dev, pipe, buffer, transfer_len,
1563 setup, "SUB", usb_pipein(pipe));
1568 err("submit_control_message: pipesize for pipe %lx is zero",
1572 if (((pipe >> 8) & 0x7f) == ohci->rh.devnum) {
1574 /* root hub - redirect */
1575 return ohci_submit_rh_msg(ohci, dev, pipe, buffer,
1576 transfer_len, setup);
1579 return submit_common_msg(ohci, dev, pipe, buffer, transfer_len,
1583 /*-------------------------------------------------------------------------*
1585 *-------------------------------------------------------------------------*/
1587 /* reset the HC and BUS */
1589 static int hc_reset(ohci_t *ohci)
1591 #ifdef CONFIG_PCI_EHCI_DEVNO
1595 int smm_timeout = 50; /* 0,5 sec */
1597 dbg("%s\n", __FUNCTION__);
1599 #ifdef CONFIG_PCI_EHCI_DEVNO
1601 * Some multi-function controllers (e.g. ISP1562) allow root hub
1602 * resetting via EHCI registers only.
1604 pdev = pci_find_devices(ehci_pci_ids, CONFIG_PCI_EHCI_DEVNO);
1609 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &base);
1610 base += EHCI_USBCMD_OFF;
1611 ohci_writel(ohci_readl(base) | EHCI_USBCMD_HCRESET, base);
1613 while (ohci_readl(base) & EHCI_USBCMD_HCRESET) {
1614 if (timeout-- <= 0) {
1615 printf("USB RootHub reset timed out!");
1621 printf("No EHCI func at %d index!\n", CONFIG_PCI_EHCI_DEVNO);
1623 if (ohci_readl(&ohci->regs->control) & OHCI_CTRL_IR) {
1624 /* SMM owns the HC, request ownership */
1625 ohci_writel(OHCI_OCR, &ohci->regs->cmdstatus);
1626 info("USB HC TakeOver from SMM");
1627 while (ohci_readl(&ohci->regs->control) & OHCI_CTRL_IR) {
1629 if (--smm_timeout == 0) {
1630 err("USB HC TakeOver failed!");
1636 /* Disable HC interrupts */
1637 ohci_writel(OHCI_INTR_MIE, &ohci->regs->intrdisable);
1639 dbg("USB HC reset_hc usb-%s: ctrl = 0x%X ;\n",
1641 ohci_readl(&ohci->regs->control));
1643 /* Reset USB (needed by some controllers) */
1644 ohci->hc_control = 0;
1645 ohci_writel(ohci->hc_control, &ohci->regs->control);
1647 /* HC Reset requires max 10 us delay */
1648 ohci_writel(OHCI_HCR, &ohci->regs->cmdstatus);
1649 while ((ohci_readl(&ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
1650 if (--timeout == 0) {
1651 err("USB HC reset timed out!");
1659 /*-------------------------------------------------------------------------*/
1661 /* Start an OHCI controller, set the BUS operational
1663 * connect the virtual root hub */
1665 static int hc_start(ohci_t *ohci)
1668 unsigned int fminterval;
1672 /* Tell the controller where the control and bulk lists are
1673 * The lists are empty now. */
1675 ohci_writel(0, &ohci->regs->ed_controlhead);
1676 ohci_writel(0, &ohci->regs->ed_bulkhead);
1678 ohci_writel((__u32)ohci->hcca,
1679 &ohci->regs->hcca); /* reset clears this */
1681 fminterval = 0x2edf;
1682 ohci_writel((fminterval * 9) / 10, &ohci->regs->periodicstart);
1683 fminterval |= ((((fminterval - 210) * 6) / 7) << 16);
1684 ohci_writel(fminterval, &ohci->regs->fminterval);
1685 ohci_writel(0x628, &ohci->regs->lsthresh);
1687 /* start controller operations */
1688 ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
1690 ohci_writel(ohci->hc_control, &ohci->regs->control);
1692 /* disable all interrupts */
1693 mask = (OHCI_INTR_SO | OHCI_INTR_WDH | OHCI_INTR_SF | OHCI_INTR_RD |
1694 OHCI_INTR_UE | OHCI_INTR_FNO | OHCI_INTR_RHSC |
1695 OHCI_INTR_OC | OHCI_INTR_MIE);
1696 ohci_writel(mask, &ohci->regs->intrdisable);
1697 /* clear all interrupts */
1698 mask &= ~OHCI_INTR_MIE;
1699 ohci_writel(mask, &ohci->regs->intrstatus);
1700 /* Choose the interrupts we care about now - but w/o MIE */
1701 mask = OHCI_INTR_RHSC | OHCI_INTR_UE | OHCI_INTR_WDH | OHCI_INTR_SO;
1702 ohci_writel(mask, &ohci->regs->intrenable);
1705 /* required for AMD-756 and some Mac platforms */
1706 ohci_writel((roothub_a(ohci) | RH_A_NPS) & ~RH_A_PSM,
1707 &ohci->regs->roothub.a);
1708 ohci_writel(RH_HS_LPSC, &ohci->regs->roothub.status);
1709 #endif /* OHCI_USE_NPS */
1711 /* POTPGT delay is bits 24-31, in 2 ms units. */
1712 mdelay((roothub_a(ohci) >> 23) & 0x1fe);
1714 /* connect the virtual root hub */
1715 ohci->rh.devnum = 0;
1720 /*-------------------------------------------------------------------------*/
1722 /* an interrupt happens */
1724 static int hc_interrupt(ohci_t *ohci)
1726 struct ohci_regs *regs = ohci->regs;
1730 if ((ohci->hcca->done_head != 0) &&
1731 !(m32_swap(ohci->hcca->done_head) & 0x01)) {
1732 ints = OHCI_INTR_WDH;
1734 ints = ohci_readl(®s->intrstatus);
1735 if (ints == ~(u32)0) {
1737 err("%s device removed!", ohci->slot_name);
1740 ints &= ohci_readl(®s->intrenable);
1742 dbg("hc_interrupt: returning..\n");
1748 /* dbg("Interrupt: %x frame: %x", ints,
1749 le16_to_cpu(ohci->hcca->frame_no)); */
1751 if (ints & OHCI_INTR_RHSC)
1754 if (ints & OHCI_INTR_UE) {
1756 err("OHCI Unrecoverable Error, controller usb-%s disabled",
1758 /* e.g. due to PCI Master/Target Abort */
1765 /* FIXME: be optimistic, hope that bug won't repeat often. */
1766 /* Make some non-interrupt context restart the controller. */
1767 /* Count and limit the retries though; either hardware or */
1768 /* software errors can go forever... */
1773 if (ints & OHCI_INTR_WDH) {
1775 ohci_writel(OHCI_INTR_WDH, ®s->intrdisable);
1776 (void)ohci_readl(®s->intrdisable); /* flush */
1777 stat = dl_done_list(ohci);
1778 ohci_writel(OHCI_INTR_WDH, ®s->intrenable);
1779 (void)ohci_readl(®s->intrdisable); /* flush */
1782 if (ints & OHCI_INTR_SO) {
1783 dbg("USB Schedule overrun\n");
1784 ohci_writel(OHCI_INTR_SO, ®s->intrenable);
1788 /* FIXME: this assumes SOF (1/ms) interrupts don't get lost... */
1789 if (ints & OHCI_INTR_SF) {
1790 unsigned int frame = m16_swap(ohci->hcca->frame_no) & 1;
1792 ohci_writel(OHCI_INTR_SF, ®s->intrdisable);
1793 if (ohci->ed_rm_list[frame] != NULL)
1794 ohci_writel(OHCI_INTR_SF, ®s->intrenable);
1798 ohci_writel(ints, ®s->intrstatus);
1802 /*-------------------------------------------------------------------------*/
1804 /*-------------------------------------------------------------------------*/
1806 /* De-allocate all resources.. */
1808 static void hc_release_ohci(ohci_t *ohci)
1810 dbg("USB HC release ohci usb-%s", ohci->slot_name);
1812 if (!ohci->disabled)
1816 /*-------------------------------------------------------------------------*/
1819 * low level initalisation routine, called from usb.c
1821 static char ohci_inited = 0;
1823 int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
1825 #ifdef CONFIG_PCI_OHCI
1829 #ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
1830 /* cpu dependant init */
1835 #ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
1836 /* board dependant init */
1837 if (board_usb_init(index, USB_INIT_HOST))
1840 memset(&gohci, 0, sizeof(ohci_t));
1842 /* align the storage */
1843 if ((__u32)&ghcca[0] & 0xff) {
1844 err("HCCA not aligned!!");
1847 gohci.hcca = &ghcca[0];
1848 info("aligned ghcca %p", gohci.hcca);
1849 memset(gohci.hcca, 0, sizeof(struct ohci_hcca));
1854 #ifdef CONFIG_PCI_OHCI
1855 pdev = pci_find_devices(ohci_pci_ids, CONFIG_PCI_OHCI_DEVNO);
1860 pci_read_config_word(pdev, PCI_VENDOR_ID, &vid);
1861 pci_read_config_word(pdev, PCI_DEVICE_ID, &did);
1862 printf("OHCI pci controller (%04x, %04x) found @(%d:%d:%d)\n",
1863 vid, did, (pdev >> 16) & 0xff,
1864 (pdev >> 11) & 0x1f, (pdev >> 8) & 0x7);
1865 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &base);
1866 printf("OHCI regs address 0x%08x\n", base);
1867 gohci.regs = (struct ohci_regs *)base;
1871 gohci.regs = (struct ohci_regs *)CONFIG_SYS_USB_OHCI_REGS_BASE;
1875 gohci.slot_name = CONFIG_SYS_USB_OHCI_SLOT_NAME;
1877 if (hc_reset (&gohci) < 0) {
1878 hc_release_ohci (&gohci);
1879 err ("can't reset usb-%s", gohci.slot_name);
1880 #ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
1881 /* board dependant cleanup */
1882 board_usb_cleanup(index, USB_INIT_HOST);
1885 #ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
1886 /* cpu dependant cleanup */
1887 usb_cpu_init_fail();
1892 if (hc_start(&gohci) < 0) {
1893 err("can't start usb-%s", gohci.slot_name);
1894 hc_release_ohci(&gohci);
1895 /* Initialization failed */
1896 #ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
1897 /* board dependant cleanup */
1901 #ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
1902 /* cpu dependant cleanup */
1909 ohci_dump(&gohci, 1);
1917 int usb_lowlevel_stop(int index)
1919 /* this gets called really early - before the controller has */
1920 /* even been initialized! */
1923 /* TODO release any interrupts, etc. */
1924 /* call hc_release_ohci() here ? */
1927 #ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
1928 /* board dependant cleanup */
1929 if (usb_board_stop())
1933 #ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
1934 /* cpu dependant cleanup */
1938 /* This driver is no longer initialised. It needs a new low-level
1939 * init (board/cpu) before it can be used again. */
1944 int submit_control_msg(struct usb_device *dev, unsigned long pipe,
1945 void *buffer, int transfer_len, struct devrequest *setup)
1947 return _ohci_submit_control_msg(&gohci, dev, pipe, buffer,
1948 transfer_len, setup);