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 <gj@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 * See file CREDITS for list of people who contributed to this
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License as
25 * published by the Free Software Foundation; either version 2 of
26 * the License, or (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
41 * 1 - Read doc/README.generic_usb_ohci
42 * 2 - this driver is intended for use with USB Mass Storage Devices
43 * (BBB) and USB keyboard. There is NO support for Isochronous pipes!
44 * 2 - when running on a PQFP208 AT91RM9200, define CONFIG_AT91C_PQFP_UHPBUG
45 * to activate workaround for bug #41 or this driver will NOT work!
50 #ifdef CONFIG_USB_OHCI_NEW
52 #include <asm/byteorder.h>
54 #if defined(CONFIG_PCI_OHCI)
56 #if !defined(CONFIG_PCI_OHCI_DEVNO)
57 #define CONFIG_PCI_OHCI_DEVNO 0
65 #ifdef CONFIG_AT91RM9200
66 #include <asm/arch/hardware.h> /* needed for AT91_USB_HOST_BASE */
69 #if defined(CONFIG_ARM920T) || \
70 defined(CONFIG_S3C2400) || \
71 defined(CONFIG_S3C2410) || \
72 defined(CONFIG_440EP) || \
73 defined(CONFIG_PCI_OHCI) || \
74 defined(CONFIG_MPC5200) || \
75 defined(CFG_OHCI_USE_NPS)
76 # define OHCI_USE_NPS /* force NoPowerSwitching mode */
79 #undef OHCI_VERBOSE_DEBUG /* not always helpful */
82 #undef OHCI_FILL_TRACE
84 /* For initializing controller (mask in an HCFS mode too) */
85 #define OHCI_CONTROL_INIT \
86 (OHCI_CTRL_CBSR & 0x3) | OHCI_CTRL_IE | OHCI_CTRL_PLE
89 * e.g. PCI controllers need this
91 #ifdef CFG_OHCI_SWAP_REG_ACCESS
92 # define readl(a) __swap_32(*((volatile u32 *)(a)))
93 # define writel(a, b) (*((volatile u32 *)(b)) = __swap_32((volatile u32)a))
95 # define readl(a) (*((volatile u32 *)(a)))
96 # define writel(a, b) (*((volatile u32 *)(b)) = ((volatile u32)a))
97 #endif /* CFG_OHCI_SWAP_REG_ACCESS */
99 #define min_t(type,x,y) ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
101 #ifdef CONFIG_PCI_OHCI
102 static struct pci_device_id ohci_pci_ids[] = {
103 {0x10b9, 0x5237}, /* ULI1575 PCI OHCI module ids */
104 {0x1033, 0x0035}, /* NEC PCI OHCI module ids */
105 {0x1131, 0x1561}, /* Philips 1561 PCI OHCI module ids */
106 /* Please add supported PCI OHCI controller ids here */
112 #define dbg(format, arg...) printf("DEBUG: " format "\n", ## arg)
114 #define dbg(format, arg...) do {} while(0)
116 #define err(format, arg...) printf("ERROR: " format "\n", ## arg)
118 #define info(format, arg...) printf("INFO: " format "\n", ## arg)
120 #define info(format, arg...) do {} while(0)
123 #ifdef CFG_OHCI_BE_CONTROLLER
124 # define m16_swap(x) cpu_to_be16(x)
125 # define m32_swap(x) cpu_to_be32(x)
127 # define m16_swap(x) cpu_to_le16(x)
128 # define m32_swap(x) cpu_to_le32(x)
129 #endif /* CFG_OHCI_BE_CONTROLLER */
133 /* this must be aligned to a 256 byte boundary */
134 struct ohci_hcca ghcca[1];
135 /* a pointer to the aligned storage */
136 struct ohci_hcca *phcca;
137 /* this allocates EDs for all possible endpoints */
138 struct ohci_device ohci_dev;
141 /* device which was disconnected */
142 struct usb_device *devgone;
144 static inline u32 roothub_a (struct ohci *hc)
145 { return readl (&hc->regs->roothub.a); }
146 static inline u32 roothub_b (struct ohci *hc)
147 { return readl (&hc->regs->roothub.b); }
148 static inline u32 roothub_status (struct ohci *hc)
149 { return readl (&hc->regs->roothub.status); }
150 static inline u32 roothub_portstatus (struct ohci *hc, int i)
151 { return readl (&hc->regs->roothub.portstatus[i]); }
153 /* forward declaration */
154 static int hc_interrupt (void);
156 td_submit_job (struct usb_device * dev, unsigned long pipe, void * buffer,
157 int transfer_len, struct devrequest * setup, urb_priv_t * urb, int interval);
159 /*-------------------------------------------------------------------------*
160 * URB support functions
161 *-------------------------------------------------------------------------*/
163 /* free HCD-private data associated with this URB */
165 static void urb_free_priv (urb_priv_t * urb)
171 last = urb->length - 1;
173 for (i = 0; i <= last; i++) {
184 /*-------------------------------------------------------------------------*/
187 static int sohci_get_current_frame_number (struct usb_device * dev);
189 /* debug| print the main components of an URB
190 * small: 0) header + data packets 1) just header */
192 static void pkt_print (urb_priv_t *purb, struct usb_device * dev,
193 unsigned long pipe, void * buffer,
194 int transfer_len, struct devrequest * setup, char * str, int small)
196 dbg("%s URB:[%4x] dev:%2d,ep:%2d-%c,type:%s,len:%d/%d stat:%#lx",
198 sohci_get_current_frame_number (dev),
199 usb_pipedevice (pipe),
200 usb_pipeendpoint (pipe),
201 usb_pipeout (pipe)? 'O': 'I',
202 usb_pipetype (pipe) < 2? (usb_pipeint (pipe)? "INTR": "ISOC"):
203 (usb_pipecontrol (pipe)? "CTRL": "BULK"),
204 (purb ? purb->actual_length : 0),
205 transfer_len, dev->status);
206 #ifdef OHCI_VERBOSE_DEBUG
210 if (usb_pipecontrol (pipe)) {
211 printf (__FILE__ ": cmd(8):");
212 for (i = 0; i < 8 ; i++)
213 printf (" %02x", ((__u8 *) setup) [i]);
216 if (transfer_len > 0 && buffer) {
217 printf (__FILE__ ": data(%d/%d):",
218 (purb ? purb->actual_length : 0),
220 len = usb_pipeout (pipe)?
222 (purb ? purb->actual_length : 0);
223 for (i = 0; i < 16 && i < len; i++)
224 printf (" %02x", ((__u8 *) buffer) [i]);
225 printf ("%s\n", i < len? "...": "");
231 /* just for debugging; prints non-empty branches of the int ed tree inclusive iso eds*/
232 void ep_print_int_eds (ohci_t *ohci, char * str) {
235 for (i= 0; i < 32; i++) {
237 ed_p = &(ohci->hcca->int_table [i]);
240 printf (__FILE__ ": %s branch int %2d(%2x):", str, i, i);
241 while (*ed_p != 0 && j--) {
242 ed_t *ed = (ed_t *)m32_swap(ed_p);
243 printf (" ed: %4x;", ed->hwINFO);
244 ed_p = &ed->hwNextED;
250 static void ohci_dump_intr_mask (char *label, __u32 mask)
252 dbg ("%s: 0x%08x%s%s%s%s%s%s%s%s%s",
255 (mask & OHCI_INTR_MIE) ? " MIE" : "",
256 (mask & OHCI_INTR_OC) ? " OC" : "",
257 (mask & OHCI_INTR_RHSC) ? " RHSC" : "",
258 (mask & OHCI_INTR_FNO) ? " FNO" : "",
259 (mask & OHCI_INTR_UE) ? " UE" : "",
260 (mask & OHCI_INTR_RD) ? " RD" : "",
261 (mask & OHCI_INTR_SF) ? " SF" : "",
262 (mask & OHCI_INTR_WDH) ? " WDH" : "",
263 (mask & OHCI_INTR_SO) ? " SO" : ""
267 static void maybe_print_eds (char *label, __u32 value)
269 ed_t *edp = (ed_t *)value;
272 dbg ("%s %08x", label, value);
273 dbg ("%08x", edp->hwINFO);
274 dbg ("%08x", edp->hwTailP);
275 dbg ("%08x", edp->hwHeadP);
276 dbg ("%08x", edp->hwNextED);
280 static char * hcfs2string (int state)
283 case OHCI_USB_RESET: return "reset";
284 case OHCI_USB_RESUME: return "resume";
285 case OHCI_USB_OPER: return "operational";
286 case OHCI_USB_SUSPEND: return "suspend";
291 /* dump control and status registers */
292 static void ohci_dump_status (ohci_t *controller)
294 struct ohci_regs *regs = controller->regs;
297 temp = readl (®s->revision) & 0xff;
299 dbg ("spec %d.%d", (temp >> 4), (temp & 0x0f));
301 temp = readl (®s->control);
302 dbg ("control: 0x%08x%s%s%s HCFS=%s%s%s%s%s CBSR=%d", temp,
303 (temp & OHCI_CTRL_RWE) ? " RWE" : "",
304 (temp & OHCI_CTRL_RWC) ? " RWC" : "",
305 (temp & OHCI_CTRL_IR) ? " IR" : "",
306 hcfs2string (temp & OHCI_CTRL_HCFS),
307 (temp & OHCI_CTRL_BLE) ? " BLE" : "",
308 (temp & OHCI_CTRL_CLE) ? " CLE" : "",
309 (temp & OHCI_CTRL_IE) ? " IE" : "",
310 (temp & OHCI_CTRL_PLE) ? " PLE" : "",
311 temp & OHCI_CTRL_CBSR
314 temp = readl (®s->cmdstatus);
315 dbg ("cmdstatus: 0x%08x SOC=%d%s%s%s%s", temp,
316 (temp & OHCI_SOC) >> 16,
317 (temp & OHCI_OCR) ? " OCR" : "",
318 (temp & OHCI_BLF) ? " BLF" : "",
319 (temp & OHCI_CLF) ? " CLF" : "",
320 (temp & OHCI_HCR) ? " HCR" : ""
323 ohci_dump_intr_mask ("intrstatus", readl (®s->intrstatus));
324 ohci_dump_intr_mask ("intrenable", readl (®s->intrenable));
326 maybe_print_eds ("ed_periodcurrent", readl (®s->ed_periodcurrent));
328 maybe_print_eds ("ed_controlhead", readl (®s->ed_controlhead));
329 maybe_print_eds ("ed_controlcurrent", readl (®s->ed_controlcurrent));
331 maybe_print_eds ("ed_bulkhead", readl (®s->ed_bulkhead));
332 maybe_print_eds ("ed_bulkcurrent", readl (®s->ed_bulkcurrent));
334 maybe_print_eds ("donehead", readl (®s->donehead));
337 static void ohci_dump_roothub (ohci_t *controller, int verbose)
341 temp = roothub_a (controller);
342 ndp = (temp & RH_A_NDP);
343 #ifdef CONFIG_AT91C_PQFP_UHPBUG
344 ndp = (ndp == 2) ? 1:0;
347 dbg ("roothub.a: %08x POTPGT=%d%s%s%s%s%s NDP=%d", temp,
348 ((temp & RH_A_POTPGT) >> 24) & 0xff,
349 (temp & RH_A_NOCP) ? " NOCP" : "",
350 (temp & RH_A_OCPM) ? " OCPM" : "",
351 (temp & RH_A_DT) ? " DT" : "",
352 (temp & RH_A_NPS) ? " NPS" : "",
353 (temp & RH_A_PSM) ? " PSM" : "",
356 temp = roothub_b (controller);
357 dbg ("roothub.b: %08x PPCM=%04x DR=%04x",
359 (temp & RH_B_PPCM) >> 16,
362 temp = roothub_status (controller);
363 dbg ("roothub.status: %08x%s%s%s%s%s%s",
365 (temp & RH_HS_CRWE) ? " CRWE" : "",
366 (temp & RH_HS_OCIC) ? " OCIC" : "",
367 (temp & RH_HS_LPSC) ? " LPSC" : "",
368 (temp & RH_HS_DRWE) ? " DRWE" : "",
369 (temp & RH_HS_OCI) ? " OCI" : "",
370 (temp & RH_HS_LPS) ? " LPS" : ""
374 for (i = 0; i < ndp; i++) {
375 temp = roothub_portstatus (controller, i);
376 dbg ("roothub.portstatus [%d] = 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s",
379 (temp & RH_PS_PRSC) ? " PRSC" : "",
380 (temp & RH_PS_OCIC) ? " OCIC" : "",
381 (temp & RH_PS_PSSC) ? " PSSC" : "",
382 (temp & RH_PS_PESC) ? " PESC" : "",
383 (temp & RH_PS_CSC) ? " CSC" : "",
385 (temp & RH_PS_LSDA) ? " LSDA" : "",
386 (temp & RH_PS_PPS) ? " PPS" : "",
387 (temp & RH_PS_PRS) ? " PRS" : "",
388 (temp & RH_PS_POCI) ? " POCI" : "",
389 (temp & RH_PS_PSS) ? " PSS" : "",
391 (temp & RH_PS_PES) ? " PES" : "",
392 (temp & RH_PS_CCS) ? " CCS" : ""
397 static void ohci_dump (ohci_t *controller, int verbose)
399 dbg ("OHCI controller usb-%s state", controller->slot_name);
401 /* dumps some of the state we know about */
402 ohci_dump_status (controller);
404 ep_print_int_eds (controller, "hcca");
405 dbg ("hcca frame #%04x", controller->hcca->frame_no);
406 ohci_dump_roothub (controller, 1);
410 /*-------------------------------------------------------------------------*
411 * Interface functions (URB)
412 *-------------------------------------------------------------------------*/
414 /* get a transfer request */
416 int sohci_submit_job(urb_priv_t *urb, struct devrequest *setup)
420 urb_priv_t *purb_priv = urb;
422 struct usb_device *dev = urb->dev;
423 unsigned long pipe = urb->pipe;
424 void *buffer = urb->transfer_buffer;
425 int transfer_len = urb->transfer_buffer_length;
426 int interval = urb->interval;
430 /* when controller's hung, permit only roothub cleanup attempts
431 * such as powering down ports */
432 if (ohci->disabled) {
433 err("sohci_submit_job: EPIPE");
437 /* we're about to begin a new transaction here so mark the URB unfinished */
440 /* every endpoint has a ed, locate and fill it */
441 if (!(ed = ep_add_ed (dev, pipe, interval, 1))) {
442 err("sohci_submit_job: ENOMEM");
446 /* for the private part of the URB we need the number of TDs (size) */
447 switch (usb_pipetype (pipe)) {
448 case PIPE_BULK: /* one TD for every 4096 Byte */
449 size = (transfer_len - 1) / 4096 + 1;
451 case PIPE_CONTROL: /* 1 TD for setup, 1 for ACK and 1 for every 4096 B */
452 size = (transfer_len == 0)? 2:
453 (transfer_len - 1) / 4096 + 3;
455 case PIPE_INTERRUPT: /* 1 TD */
462 if (size >= (N_URB_TD - 1)) {
463 err("need %d TDs, only have %d", size, N_URB_TD);
466 purb_priv->pipe = pipe;
468 /* fill the private part of the URB */
469 purb_priv->length = size;
471 purb_priv->actual_length = 0;
473 /* allocate the TDs */
474 /* note that td[0] was allocated in ep_add_ed */
475 for (i = 0; i < size; i++) {
476 purb_priv->td[i] = td_alloc (dev);
477 if (!purb_priv->td[i]) {
478 purb_priv->length = i;
479 urb_free_priv (purb_priv);
480 err("sohci_submit_job: ENOMEM");
485 if (ed->state == ED_NEW || (ed->state & ED_DEL)) {
486 urb_free_priv (purb_priv);
487 err("sohci_submit_job: EINVAL");
491 /* link the ed into a chain if is not already */
492 if (ed->state != ED_OPER)
495 /* fill the TDs and link it to the ed */
496 td_submit_job(dev, pipe, buffer, transfer_len, setup, purb_priv, interval);
501 static inline int sohci_return_job(struct ohci *hc, urb_priv_t *urb)
503 struct ohci_regs *regs = hc->regs;
505 switch (usb_pipetype (urb->pipe)) {
507 /* implicitly requeued */
508 if (urb->dev->irq_handle &&
509 (urb->dev->irq_act_len = urb->actual_length)) {
510 writel (OHCI_INTR_WDH, ®s->intrenable);
511 readl (®s->intrenable); /* PCI posting flush */
512 urb->dev->irq_handle(urb->dev);
513 writel (OHCI_INTR_WDH, ®s->intrdisable);
514 readl (®s->intrdisable); /* PCI posting flush */
516 urb->actual_length = 0;
520 urb->transfer_buffer,
521 urb->transfer_buffer_length,
535 /*-------------------------------------------------------------------------*/
538 /* tell us the current USB frame number */
540 static int sohci_get_current_frame_number (struct usb_device *usb_dev)
542 ohci_t *ohci = &gohci;
544 return m16_swap (ohci->hcca->frame_no);
548 /*-------------------------------------------------------------------------*
549 * ED handling functions
550 *-------------------------------------------------------------------------*/
552 /* search for the right branch to insert an interrupt ed into the int tree
553 * do some load ballancing;
554 * returns the branch and
555 * sets the interval to interval = 2^integer (ld (interval)) */
557 static int ep_int_ballance (ohci_t * ohci, int interval, int load)
561 /* search for the least loaded interrupt endpoint
562 * branch of all 32 branches
564 for (i = 0; i < 32; i++)
565 if (ohci->ohci_int_load [branch] > ohci->ohci_int_load [i])
568 branch = branch % interval;
569 for (i = branch; i < 32; i += interval)
570 ohci->ohci_int_load [i] += load;
575 /*-------------------------------------------------------------------------*/
577 /* 2^int( ld (inter)) */
579 static int ep_2_n_interval (int inter)
582 for (i = 0; ((inter >> i) > 1 ) && (i < 5); i++);
586 /*-------------------------------------------------------------------------*/
588 /* the int tree is a binary tree
589 * in order to process it sequentially the indexes of the branches have to be mapped
590 * the mapping reverses the bits of a word of num_bits length */
592 static int ep_rev (int num_bits, int word)
596 for (i = 0; i < num_bits; i++)
597 wout |= (((word >> i) & 1) << (num_bits - i - 1));
601 /*-------------------------------------------------------------------------*
602 * ED handling functions
603 *-------------------------------------------------------------------------*/
605 /* link an ed into one of the HC chains */
607 static int ep_link (ohci_t *ohci, ed_t *edi)
609 volatile ed_t *ed = edi;
618 ed->int_interval = 0;
623 if (ohci->ed_controltail == NULL) {
624 writel (ed, &ohci->regs->ed_controlhead);
626 ohci->ed_controltail->hwNextED = m32_swap ((unsigned long)ed);
628 ed->ed_prev = ohci->ed_controltail;
629 if (!ohci->ed_controltail && !ohci->ed_rm_list[0] &&
630 !ohci->ed_rm_list[1] && !ohci->sleeping) {
631 ohci->hc_control |= OHCI_CTRL_CLE;
632 writel (ohci->hc_control, &ohci->regs->control);
634 ohci->ed_controltail = edi;
639 if (ohci->ed_bulktail == NULL) {
640 writel (ed, &ohci->regs->ed_bulkhead);
642 ohci->ed_bulktail->hwNextED = m32_swap ((unsigned long)ed);
644 ed->ed_prev = ohci->ed_bulktail;
645 if (!ohci->ed_bulktail && !ohci->ed_rm_list[0] &&
646 !ohci->ed_rm_list[1] && !ohci->sleeping) {
647 ohci->hc_control |= OHCI_CTRL_BLE;
648 writel (ohci->hc_control, &ohci->regs->control);
650 ohci->ed_bulktail = edi;
655 interval = ep_2_n_interval (ed->int_period);
656 ed->int_interval = interval;
657 int_branch = ep_int_ballance (ohci, interval, load);
658 ed->int_branch = int_branch;
660 for (i = 0; i < ep_rev (6, interval); i += inter) {
662 for (ed_p = &(ohci->hcca->int_table[ep_rev (5, i) + int_branch]);
663 (*ed_p != 0) && (((ed_t *)ed_p)->int_interval >= interval);
664 ed_p = &(((ed_t *)ed_p)->hwNextED))
665 inter = ep_rev (6, ((ed_t *)ed_p)->int_interval);
666 ed->hwNextED = *ed_p;
667 *ed_p = m32_swap((unsigned long)ed);
674 /*-------------------------------------------------------------------------*/
676 /* scan the periodic table to find and unlink this ED */
677 static void periodic_unlink ( struct ohci *ohci, volatile struct ed *ed,
678 unsigned index, unsigned period)
680 for (; index < NUM_INTS; index += period) {
681 __u32 *ed_p = &ohci->hcca->int_table [index];
683 /* ED might have been unlinked through another path */
685 if (((struct ed *)m32_swap ((unsigned long)ed_p)) == ed) {
686 *ed_p = ed->hwNextED;
689 ed_p = & (((struct ed *)m32_swap ((unsigned long)ed_p))->hwNextED);
694 /* unlink an ed from one of the HC chains.
695 * just the link to the ed is unlinked.
696 * the link from the ed still points to another operational ed or 0
697 * so the HC can eventually finish the processing of the unlinked ed */
699 static int ep_unlink (ohci_t *ohci, ed_t *edi)
701 volatile ed_t *ed = edi;
704 ed->hwINFO |= m32_swap (OHCI_ED_SKIP);
708 if (ed->ed_prev == NULL) {
710 ohci->hc_control &= ~OHCI_CTRL_CLE;
711 writel (ohci->hc_control, &ohci->regs->control);
713 writel (m32_swap (*((__u32 *)&ed->hwNextED)), &ohci->regs->ed_controlhead);
715 ed->ed_prev->hwNextED = ed->hwNextED;
717 if (ohci->ed_controltail == ed) {
718 ohci->ed_controltail = ed->ed_prev;
720 ((ed_t *)m32_swap (*((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
725 if (ed->ed_prev == NULL) {
727 ohci->hc_control &= ~OHCI_CTRL_BLE;
728 writel (ohci->hc_control, &ohci->regs->control);
730 writel (m32_swap (*((__u32 *)&ed->hwNextED)), &ohci->regs->ed_bulkhead);
732 ed->ed_prev->hwNextED = ed->hwNextED;
734 if (ohci->ed_bulktail == ed) {
735 ohci->ed_bulktail = ed->ed_prev;
737 ((ed_t *)m32_swap (*((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
742 periodic_unlink (ohci, ed, 0, 1);
743 for (i = ed->int_branch; i < 32; i += ed->int_interval)
744 ohci->ohci_int_load[i] -= ed->int_load;
747 ed->state = ED_UNLINK;
751 /*-------------------------------------------------------------------------*/
753 /* add/reinit an endpoint; this should be done once at the
754 * usb_set_configuration command, but the USB stack is a little bit
755 * stateless so we do it at every transaction if the state of the ed
756 * is ED_NEW then a dummy td is added and the state is changed to
757 * ED_UNLINK in all other cases the state is left unchanged the ed
758 * info fields are setted anyway even though most of them should not
761 static ed_t * ep_add_ed (struct usb_device *usb_dev, unsigned long pipe,
762 int interval, int load)
768 ed = ed_ret = &ohci_dev.ed[(usb_pipeendpoint (pipe) << 1) |
769 (usb_pipecontrol (pipe)? 0: usb_pipeout (pipe))];
771 if ((ed->state & ED_DEL) || (ed->state & ED_URB_DEL)) {
772 err("ep_add_ed: pending delete");
773 /* pending delete request */
777 if (ed->state == ED_NEW) {
778 ed->hwINFO = m32_swap (OHCI_ED_SKIP); /* skip ed */
779 /* dummy td; end of td list for ed */
780 td = td_alloc (usb_dev);
781 ed->hwTailP = m32_swap ((unsigned long)td);
782 ed->hwHeadP = ed->hwTailP;
783 ed->state = ED_UNLINK;
784 ed->type = usb_pipetype (pipe);
788 ed->hwINFO = m32_swap (usb_pipedevice (pipe)
789 | usb_pipeendpoint (pipe) << 7
790 | (usb_pipeisoc (pipe)? 0x8000: 0)
791 | (usb_pipecontrol (pipe)? 0: (usb_pipeout (pipe)? 0x800: 0x1000))
792 | usb_pipeslow (pipe) << 13
793 | usb_maxpacket (usb_dev, pipe) << 16);
795 if (ed->type == PIPE_INTERRUPT && ed->state == ED_UNLINK) {
796 ed->int_period = interval;
803 /*-------------------------------------------------------------------------*
804 * TD handling functions
805 *-------------------------------------------------------------------------*/
807 /* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
809 static void td_fill (ohci_t *ohci, unsigned int info,
811 struct usb_device *dev, int index, urb_priv_t *urb_priv)
813 volatile td_t *td, *td_pt;
814 #ifdef OHCI_FILL_TRACE
818 if (index > urb_priv->length) {
819 err("index > length");
822 /* use this td as the next dummy */
823 td_pt = urb_priv->td [index];
826 /* fill the old dummy TD */
827 td = urb_priv->td [index] = (td_t *)(m32_swap (urb_priv->ed->hwTailP) & ~0xf);
829 td->ed = urb_priv->ed;
830 td->next_dl_td = NULL;
832 td->data = (__u32)data;
833 #ifdef OHCI_FILL_TRACE
834 if ((usb_pipetype(urb_priv->pipe) == PIPE_BULK) && usb_pipeout(urb_priv->pipe)) {
835 for (i = 0; i < len; i++)
836 printf("td->data[%d] %#2x ",i, ((unsigned char *)td->data)[i]);
843 td->hwINFO = m32_swap (info);
844 td->hwCBP = m32_swap ((unsigned long)data);
846 td->hwBE = m32_swap ((unsigned long)(data + len - 1));
849 td->hwNextTD = m32_swap ((unsigned long)td_pt);
851 /* append to queue */
852 td->ed->hwTailP = td->hwNextTD;
855 /*-------------------------------------------------------------------------*/
857 /* prepare all TDs of a transfer */
859 static void td_submit_job (struct usb_device *dev, unsigned long pipe, void *buffer,
860 int transfer_len, struct devrequest *setup, urb_priv_t *urb, int interval)
862 ohci_t *ohci = &gohci;
863 int data_len = transfer_len;
867 unsigned int toggle = 0;
869 /* OHCI handles the DATA-toggles itself, we just use the USB-toggle bits for reseting */
870 if(usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe))) {
871 toggle = TD_T_TOGGLE;
874 usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), 1);
882 switch (usb_pipetype (pipe)) {
884 info = usb_pipeout (pipe)?
885 TD_CC | TD_DP_OUT : TD_CC | TD_DP_IN ;
886 while(data_len > 4096) {
887 td_fill (ohci, info | (cnt? TD_T_TOGGLE:toggle), data, 4096, dev, cnt, urb);
888 data += 4096; data_len -= 4096; cnt++;
890 info = usb_pipeout (pipe)?
891 TD_CC | TD_DP_OUT : TD_CC | TD_R | TD_DP_IN ;
892 td_fill (ohci, info | (cnt? TD_T_TOGGLE:toggle), data, data_len, dev, cnt, urb);
896 writel (OHCI_BLF, &ohci->regs->cmdstatus); /* start bulk list */
900 info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
901 td_fill (ohci, info, setup, 8, dev, cnt++, urb);
903 info = usb_pipeout (pipe)?
904 TD_CC | TD_R | TD_DP_OUT | TD_T_DATA1 : TD_CC | TD_R | TD_DP_IN | TD_T_DATA1;
905 /* NOTE: mishandles transfers >8K, some >4K */
906 td_fill (ohci, info, data, data_len, dev, cnt++, urb);
908 info = usb_pipeout (pipe)?
909 TD_CC | TD_DP_IN | TD_T_DATA1: TD_CC | TD_DP_OUT | TD_T_DATA1;
910 td_fill (ohci, info, data, 0, dev, cnt++, urb);
912 writel (OHCI_CLF, &ohci->regs->cmdstatus); /* start Control list */
916 info = usb_pipeout (urb->pipe)?
917 TD_CC | TD_DP_OUT | toggle:
918 TD_CC | TD_R | TD_DP_IN | toggle;
919 td_fill (ohci, info, data, data_len, dev, cnt++, urb);
922 if (urb->length != cnt)
923 dbg("TD LENGTH %d != CNT %d", urb->length, cnt);
926 /*-------------------------------------------------------------------------*
927 * Done List handling functions
928 *-------------------------------------------------------------------------*/
930 /* calculate the transfer length and update the urb */
932 static void dl_transfer_length(td_t * td)
934 __u32 tdINFO, tdBE, tdCBP;
935 urb_priv_t *lurb_priv = td->ed->purb;
937 tdINFO = m32_swap (td->hwINFO);
938 tdBE = m32_swap (td->hwBE);
939 tdCBP = m32_swap (td->hwCBP);
941 if (!(usb_pipetype (lurb_priv->pipe) == PIPE_CONTROL &&
942 ((td->index == 0) || (td->index == lurb_priv->length - 1)))) {
945 lurb_priv->actual_length += tdBE - td->data + 1;
947 lurb_priv->actual_length += tdCBP - td->data;
952 /*-------------------------------------------------------------------------*/
954 /* replies to the request have to be on a FIFO basis so
955 * we reverse the reversed done-list */
957 static td_t * dl_reverse_done_list (ohci_t *ohci)
961 td_t *td_list = NULL;
962 urb_priv_t *lurb_priv = NULL;
964 td_list_hc = m32_swap (ohci->hcca->done_head) & 0xfffffff0;
965 ohci->hcca->done_head = 0;
968 td_list = (td_t *)td_list_hc;
970 if (TD_CC_GET (m32_swap (td_list->hwINFO))) {
971 lurb_priv = td_list->ed->purb;
972 dbg(" USB-error/status: %x : %p",
973 TD_CC_GET (m32_swap (td_list->hwINFO)), td_list);
974 if (td_list->ed->hwHeadP & m32_swap (0x1)) {
975 if (lurb_priv && ((td_list->index + 1) < lurb_priv->length)) {
976 td_list->ed->hwHeadP =
977 (lurb_priv->td[lurb_priv->length - 1]->hwNextTD & m32_swap (0xfffffff0)) |
978 (td_list->ed->hwHeadP & m32_swap (0x2));
979 lurb_priv->td_cnt += lurb_priv->length - td_list->index - 1;
981 td_list->ed->hwHeadP &= m32_swap (0xfffffff2);
983 #ifdef CONFIG_MPC5200
984 td_list->hwNextTD = 0;
988 td_list->next_dl_td = td_rev;
990 td_list_hc = m32_swap (td_list->hwNextTD) & 0xfffffff0;
995 /*-------------------------------------------------------------------------*/
998 static int dl_done_list (ohci_t *ohci, td_t *td_list)
1000 td_t *td_list_next = NULL;
1005 urb_priv_t *lurb_priv;
1006 __u32 tdINFO, edHeadP, edTailP;
1009 td_list_next = td_list->next_dl_td;
1011 tdINFO = m32_swap (td_list->hwINFO);
1014 lurb_priv = ed->purb;
1016 dl_transfer_length(td_list);
1018 /* error code of transfer */
1019 cc = TD_CC_GET (tdINFO);
1021 dbg("ConditionCode %#x", cc);
1022 stat = cc_to_error[cc];
1025 /* see if this done list makes for all TD's of current URB,
1026 * and mark the URB finished if so */
1027 if (++(lurb_priv->td_cnt) == lurb_priv->length) {
1029 if ((ed->state & (ED_OPER | ED_UNLINK)) &&
1030 (lurb_priv->state != URB_DEL))
1032 if ((ed->state & (ED_OPER | ED_UNLINK)))
1034 lurb_priv->finished = sohci_return_job(ohci,
1037 dbg("dl_done_list: strange.., ED state %x, ed->state\n");
1039 dbg("dl_done_list: processing TD %x, len %x\n", lurb_priv->td_cnt,
1041 if (ed->state != ED_NEW &&
1042 (usb_pipetype (lurb_priv->pipe) != PIPE_INTERRUPT)) {
1043 edHeadP = m32_swap (ed->hwHeadP) & 0xfffffff0;
1044 edTailP = m32_swap (ed->hwTailP);
1046 /* unlink eds if they are not busy */
1047 if ((edHeadP == edTailP) && (ed->state == ED_OPER))
1048 ep_unlink (ohci, ed);
1051 td_list = td_list_next;
1056 /*-------------------------------------------------------------------------*
1058 *-------------------------------------------------------------------------*/
1060 /* Device descriptor */
1061 static __u8 root_hub_dev_des[] =
1063 0x12, /* __u8 bLength; */
1064 0x01, /* __u8 bDescriptorType; Device */
1065 0x10, /* __u16 bcdUSB; v1.1 */
1067 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
1068 0x00, /* __u8 bDeviceSubClass; */
1069 0x00, /* __u8 bDeviceProtocol; */
1070 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
1071 0x00, /* __u16 idVendor; */
1073 0x00, /* __u16 idProduct; */
1075 0x00, /* __u16 bcdDevice; */
1077 0x00, /* __u8 iManufacturer; */
1078 0x01, /* __u8 iProduct; */
1079 0x00, /* __u8 iSerialNumber; */
1080 0x01 /* __u8 bNumConfigurations; */
1083 /* Configuration descriptor */
1084 static __u8 root_hub_config_des[] =
1086 0x09, /* __u8 bLength; */
1087 0x02, /* __u8 bDescriptorType; Configuration */
1088 0x19, /* __u16 wTotalLength; */
1090 0x01, /* __u8 bNumInterfaces; */
1091 0x01, /* __u8 bConfigurationValue; */
1092 0x00, /* __u8 iConfiguration; */
1093 0x40, /* __u8 bmAttributes;
1094 Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup, 4..0: resvd */
1095 0x00, /* __u8 MaxPower; */
1098 0x09, /* __u8 if_bLength; */
1099 0x04, /* __u8 if_bDescriptorType; Interface */
1100 0x00, /* __u8 if_bInterfaceNumber; */
1101 0x00, /* __u8 if_bAlternateSetting; */
1102 0x01, /* __u8 if_bNumEndpoints; */
1103 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
1104 0x00, /* __u8 if_bInterfaceSubClass; */
1105 0x00, /* __u8 if_bInterfaceProtocol; */
1106 0x00, /* __u8 if_iInterface; */
1109 0x07, /* __u8 ep_bLength; */
1110 0x05, /* __u8 ep_bDescriptorType; Endpoint */
1111 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
1112 0x03, /* __u8 ep_bmAttributes; Interrupt */
1113 0x02, /* __u16 ep_wMaxPacketSize; ((MAX_ROOT_PORTS + 1) / 8 */
1115 0xff /* __u8 ep_bInterval; 255 ms */
1118 static unsigned char root_hub_str_index0[] =
1120 0x04, /* __u8 bLength; */
1121 0x03, /* __u8 bDescriptorType; String-descriptor */
1122 0x09, /* __u8 lang ID */
1123 0x04, /* __u8 lang ID */
1126 static unsigned char root_hub_str_index1[] =
1128 28, /* __u8 bLength; */
1129 0x03, /* __u8 bDescriptorType; String-descriptor */
1130 'O', /* __u8 Unicode */
1131 0, /* __u8 Unicode */
1132 'H', /* __u8 Unicode */
1133 0, /* __u8 Unicode */
1134 'C', /* __u8 Unicode */
1135 0, /* __u8 Unicode */
1136 'I', /* __u8 Unicode */
1137 0, /* __u8 Unicode */
1138 ' ', /* __u8 Unicode */
1139 0, /* __u8 Unicode */
1140 'R', /* __u8 Unicode */
1141 0, /* __u8 Unicode */
1142 'o', /* __u8 Unicode */
1143 0, /* __u8 Unicode */
1144 'o', /* __u8 Unicode */
1145 0, /* __u8 Unicode */
1146 't', /* __u8 Unicode */
1147 0, /* __u8 Unicode */
1148 ' ', /* __u8 Unicode */
1149 0, /* __u8 Unicode */
1150 'H', /* __u8 Unicode */
1151 0, /* __u8 Unicode */
1152 'u', /* __u8 Unicode */
1153 0, /* __u8 Unicode */
1154 'b', /* __u8 Unicode */
1155 0, /* __u8 Unicode */
1158 /* Hub class-specific descriptor is constructed dynamically */
1160 /*-------------------------------------------------------------------------*/
1162 #define OK(x) len = (x); break
1164 #define WR_RH_STAT(x) {info("WR:status %#8x", (x));writel((x), &gohci.regs->roothub.status);}
1165 #define WR_RH_PORTSTAT(x) {info("WR:portstatus[%d] %#8x", wIndex-1, (x));writel((x), &gohci.regs->roothub.portstatus[wIndex-1]);}
1167 #define WR_RH_STAT(x) writel((x), &gohci.regs->roothub.status)
1168 #define WR_RH_PORTSTAT(x) writel((x), &gohci.regs->roothub.portstatus[wIndex-1])
1170 #define RD_RH_STAT roothub_status(&gohci)
1171 #define RD_RH_PORTSTAT roothub_portstatus(&gohci,wIndex-1)
1173 /* request to virtual root hub */
1175 int rh_check_port_status(ohci_t *controller)
1181 temp = roothub_a (controller);
1182 ndp = (temp & RH_A_NDP);
1183 #ifdef CONFIG_AT91C_PQFP_UHPBUG
1184 ndp = (ndp == 2) ? 1:0;
1186 for (i = 0; i < ndp; i++) {
1187 temp = roothub_portstatus (controller, i);
1188 /* check for a device disconnect */
1189 if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
1190 (RH_PS_PESC | RH_PS_CSC)) &&
1191 ((temp & RH_PS_CCS) == 0)) {
1199 static int ohci_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
1200 void *buffer, int transfer_len, struct devrequest *cmd)
1202 void * data = buffer;
1203 int leni = transfer_len;
1207 __u8 *data_buf = (__u8 *)datab;
1214 pkt_print(NULL, dev, pipe, buffer, transfer_len, cmd, "SUB(rh)", usb_pipein(pipe));
1218 if ((pipe & PIPE_INTERRUPT) == PIPE_INTERRUPT) {
1219 info("Root-Hub submit IRQ: NOT implemented");
1223 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
1224 wValue = le16_to_cpu (cmd->value);
1225 wIndex = le16_to_cpu (cmd->index);
1226 wLength = le16_to_cpu (cmd->length);
1228 info("Root-Hub: adr: %2x cmd(%1x): %08x %04x %04x %04x",
1229 dev->devnum, 8, bmRType_bReq, wValue, wIndex, wLength);
1231 switch (bmRType_bReq) {
1232 /* Request Destination:
1233 without flags: Device,
1234 RH_INTERFACE: interface,
1235 RH_ENDPOINT: endpoint,
1236 RH_CLASS means HUB here,
1237 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1241 *(__u16 *) data_buf = cpu_to_le16 (1); OK (2);
1242 case RH_GET_STATUS | RH_INTERFACE:
1243 *(__u16 *) data_buf = cpu_to_le16 (0); OK (2);
1244 case RH_GET_STATUS | RH_ENDPOINT:
1245 *(__u16 *) data_buf = cpu_to_le16 (0); OK (2);
1246 case RH_GET_STATUS | RH_CLASS:
1247 *(__u32 *) data_buf = cpu_to_le32 (
1248 RD_RH_STAT & ~(RH_HS_CRWE | RH_HS_DRWE));
1250 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
1251 *(__u32 *) data_buf = cpu_to_le32 (RD_RH_PORTSTAT); OK (4);
1253 case RH_CLEAR_FEATURE | RH_ENDPOINT:
1255 case (RH_ENDPOINT_STALL): OK (0);
1259 case RH_CLEAR_FEATURE | RH_CLASS:
1261 case RH_C_HUB_LOCAL_POWER:
1263 case (RH_C_HUB_OVER_CURRENT):
1264 WR_RH_STAT(RH_HS_OCIC); OK (0);
1268 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1270 case (RH_PORT_ENABLE):
1271 WR_RH_PORTSTAT (RH_PS_CCS ); OK (0);
1272 case (RH_PORT_SUSPEND):
1273 WR_RH_PORTSTAT (RH_PS_POCI); OK (0);
1274 case (RH_PORT_POWER):
1275 WR_RH_PORTSTAT (RH_PS_LSDA); OK (0);
1276 case (RH_C_PORT_CONNECTION):
1277 WR_RH_PORTSTAT (RH_PS_CSC ); OK (0);
1278 case (RH_C_PORT_ENABLE):
1279 WR_RH_PORTSTAT (RH_PS_PESC); OK (0);
1280 case (RH_C_PORT_SUSPEND):
1281 WR_RH_PORTSTAT (RH_PS_PSSC); OK (0);
1282 case (RH_C_PORT_OVER_CURRENT):
1283 WR_RH_PORTSTAT (RH_PS_OCIC); OK (0);
1284 case (RH_C_PORT_RESET):
1285 WR_RH_PORTSTAT (RH_PS_PRSC); OK (0);
1289 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1291 case (RH_PORT_SUSPEND):
1292 WR_RH_PORTSTAT (RH_PS_PSS ); OK (0);
1293 case (RH_PORT_RESET): /* BUG IN HUP CODE *********/
1294 if (RD_RH_PORTSTAT & RH_PS_CCS)
1295 WR_RH_PORTSTAT (RH_PS_PRS);
1297 case (RH_PORT_POWER):
1298 WR_RH_PORTSTAT (RH_PS_PPS );
1301 case (RH_PORT_ENABLE): /* BUG IN HUP CODE *********/
1302 if (RD_RH_PORTSTAT & RH_PS_CCS)
1303 WR_RH_PORTSTAT (RH_PS_PES );
1308 case RH_SET_ADDRESS: gohci.rh.devnum = wValue; OK(0);
1310 case RH_GET_DESCRIPTOR:
1311 switch ((wValue & 0xff00) >> 8) {
1312 case (0x01): /* device descriptor */
1313 len = min_t(unsigned int,
1316 sizeof (root_hub_dev_des),
1318 data_buf = root_hub_dev_des; OK(len);
1319 case (0x02): /* configuration descriptor */
1320 len = min_t(unsigned int,
1323 sizeof (root_hub_config_des),
1325 data_buf = root_hub_config_des; OK(len);
1326 case (0x03): /* string descriptors */
1327 if(wValue==0x0300) {
1328 len = min_t(unsigned int,
1331 sizeof (root_hub_str_index0),
1333 data_buf = root_hub_str_index0;
1336 if(wValue==0x0301) {
1337 len = min_t(unsigned int,
1340 sizeof (root_hub_str_index1),
1342 data_buf = root_hub_str_index1;
1346 stat = USB_ST_STALLED;
1350 case RH_GET_DESCRIPTOR | RH_CLASS:
1352 __u32 temp = roothub_a (&gohci);
1354 data_buf [0] = 9; /* min length; */
1355 data_buf [1] = 0x29;
1356 data_buf [2] = temp & RH_A_NDP;
1357 #ifdef CONFIG_AT91C_PQFP_UHPBUG
1358 data_buf [2] = (data_buf [2] == 2) ? 1:0;
1361 if (temp & RH_A_PSM) /* per-port power switching? */
1362 data_buf [3] |= 0x1;
1363 if (temp & RH_A_NOCP) /* no overcurrent reporting? */
1364 data_buf [3] |= 0x10;
1365 else if (temp & RH_A_OCPM) /* per-port overcurrent reporting? */
1366 data_buf [3] |= 0x8;
1368 /* corresponds to data_buf[4-7] */
1370 data_buf [5] = (temp & RH_A_POTPGT) >> 24;
1371 temp = roothub_b (&gohci);
1372 data_buf [7] = temp & RH_B_DR;
1373 if (data_buf [2] < 7) {
1374 data_buf [8] = 0xff;
1377 data_buf [8] = (temp & RH_B_DR) >> 8;
1378 data_buf [10] = data_buf [9] = 0xff;
1381 len = min_t(unsigned int, leni,
1382 min_t(unsigned int, data_buf [0], wLength));
1386 case RH_GET_CONFIGURATION: *(__u8 *) data_buf = 0x01; OK (1);
1388 case RH_SET_CONFIGURATION: WR_RH_STAT (0x10000); OK (0);
1391 dbg ("unsupported root hub command");
1392 stat = USB_ST_STALLED;
1396 ohci_dump_roothub (&gohci, 1);
1401 len = min_t(int, len, leni);
1402 if (data != data_buf)
1403 memcpy (data, data_buf, len);
1408 pkt_print(NULL, dev, pipe, buffer, transfer_len, cmd, "RET(rh)", 0/*usb_pipein(pipe)*/);
1416 /*-------------------------------------------------------------------------*/
1418 /* common code for handling submit messages - used for all but root hub */
1420 int submit_common_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1421 int transfer_len, struct devrequest *setup, int interval)
1424 int maxsize = usb_maxpacket(dev, pipe);
1428 urb = malloc(sizeof(urb_priv_t));
1429 memset(urb, 0, sizeof(urb_priv_t));
1433 urb->transfer_buffer = buffer;
1434 urb->transfer_buffer_length = transfer_len;
1435 urb->interval = interval;
1437 /* device pulled? Shortcut the action. */
1438 if (devgone == dev) {
1439 dev->status = USB_ST_CRC_ERR;
1444 urb->actual_length = 0;
1445 pkt_print(urb, dev, pipe, buffer, transfer_len, setup, "SUB", usb_pipein(pipe));
1450 err("submit_common_message: pipesize for pipe %lx is zero",
1455 if (sohci_submit_job(urb, setup) < 0) {
1456 err("sohci_submit_job failed");
1462 /* ohci_dump_status(&gohci); */
1465 /* allow more time for a BULK device to react - some are slow */
1466 #define BULK_TO 5000 /* timeout in milliseconds */
1467 if (usb_pipetype (pipe) == PIPE_BULK)
1472 /* wait for it to complete */
1474 /* check whether the controller is done */
1475 stat = hc_interrupt();
1477 stat = USB_ST_CRC_ERR;
1481 /* NOTE: since we are not interrupt driven in U-Boot and always
1482 * handle only one URB at a time, we cannot assume the
1483 * transaction finished on the first successful return from
1484 * hc_interrupt().. unless the flag for current URB is set,
1485 * meaning that all TD's to/from device got actually
1486 * transferred and processed. If the current URB is not
1487 * finished we need to re-iterate this loop so as
1488 * hc_interrupt() gets called again as there needs to be some
1489 * more TD's to process still */
1490 if ((stat >= 0) && (stat != 0xff) && (urb->finished)) {
1491 /* 0xff is returned for an SF-interrupt */
1501 err("CTL:TIMEOUT ");
1502 dbg("submit_common_msg: TO status %x\n", stat);
1504 stat = USB_ST_CRC_ERR;
1510 dev->act_len = transfer_len;
1513 pkt_print(urb, dev, pipe, buffer, transfer_len, setup, "RET(ctlr)", usb_pipein(pipe));
1518 /* free TDs in urb_priv */
1519 if (usb_pipetype (pipe) != PIPE_INTERRUPT)
1520 urb_free_priv (urb);
1524 /* submit routines called from usb.c */
1525 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1528 info("submit_bulk_msg");
1529 return submit_common_msg(dev, pipe, buffer, transfer_len, NULL, 0);
1532 int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1533 int transfer_len, struct devrequest *setup)
1535 int maxsize = usb_maxpacket(dev, pipe);
1537 info("submit_control_msg");
1539 pkt_print(NULL, dev, pipe, buffer, transfer_len, setup, "SUB", usb_pipein(pipe));
1544 err("submit_control_message: pipesize for pipe %lx is zero",
1548 if (((pipe >> 8) & 0x7f) == gohci.rh.devnum) {
1550 /* root hub - redirect */
1551 return ohci_submit_rh_msg(dev, pipe, buffer, transfer_len,
1555 return submit_common_msg(dev, pipe, buffer, transfer_len, setup, 0);
1558 int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1559 int transfer_len, int interval)
1561 info("submit_int_msg");
1562 return submit_common_msg(dev, pipe, buffer, transfer_len, NULL,
1566 /*-------------------------------------------------------------------------*
1568 *-------------------------------------------------------------------------*/
1570 /* reset the HC and BUS */
1572 static int hc_reset (ohci_t *ohci)
1575 int smm_timeout = 50; /* 0,5 sec */
1577 dbg("%s\n", __FUNCTION__);
1579 if (readl (&ohci->regs->control) & OHCI_CTRL_IR) { /* SMM owns the HC */
1580 writel (OHCI_OCR, &ohci->regs->cmdstatus); /* request ownership */
1581 info("USB HC TakeOver from SMM");
1582 while (readl (&ohci->regs->control) & OHCI_CTRL_IR) {
1584 if (--smm_timeout == 0) {
1585 err("USB HC TakeOver failed!");
1591 /* Disable HC interrupts */
1592 writel (OHCI_INTR_MIE, &ohci->regs->intrdisable);
1594 dbg("USB HC reset_hc usb-%s: ctrl = 0x%X ;\n",
1596 readl(&ohci->regs->control));
1598 /* Reset USB (needed by some controllers) */
1599 ohci->hc_control = 0;
1600 writel (ohci->hc_control, &ohci->regs->control);
1602 /* HC Reset requires max 10 us delay */
1603 writel (OHCI_HCR, &ohci->regs->cmdstatus);
1604 while ((readl (&ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
1605 if (--timeout == 0) {
1606 err("USB HC reset timed out!");
1614 /*-------------------------------------------------------------------------*/
1616 /* Start an OHCI controller, set the BUS operational
1618 * connect the virtual root hub */
1620 static int hc_start (ohci_t * ohci)
1623 unsigned int fminterval;
1627 /* Tell the controller where the control and bulk lists are
1628 * The lists are empty now. */
1630 writel (0, &ohci->regs->ed_controlhead);
1631 writel (0, &ohci->regs->ed_bulkhead);
1633 writel ((__u32)ohci->hcca, &ohci->regs->hcca); /* a reset clears this */
1635 fminterval = 0x2edf;
1636 writel ((fminterval * 9) / 10, &ohci->regs->periodicstart);
1637 fminterval |= ((((fminterval - 210) * 6) / 7) << 16);
1638 writel (fminterval, &ohci->regs->fminterval);
1639 writel (0x628, &ohci->regs->lsthresh);
1641 /* start controller operations */
1642 ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
1644 writel (ohci->hc_control, &ohci->regs->control);
1646 /* disable all interrupts */
1647 mask = (OHCI_INTR_SO | OHCI_INTR_WDH | OHCI_INTR_SF | OHCI_INTR_RD |
1648 OHCI_INTR_UE | OHCI_INTR_FNO | OHCI_INTR_RHSC |
1649 OHCI_INTR_OC | OHCI_INTR_MIE);
1650 writel (mask, &ohci->regs->intrdisable);
1651 /* clear all interrupts */
1652 mask &= ~OHCI_INTR_MIE;
1653 writel (mask, &ohci->regs->intrstatus);
1654 /* Choose the interrupts we care about now - but w/o MIE */
1655 mask = OHCI_INTR_RHSC | OHCI_INTR_UE | OHCI_INTR_WDH | OHCI_INTR_SO;
1656 writel (mask, &ohci->regs->intrenable);
1659 /* required for AMD-756 and some Mac platforms */
1660 writel ((roothub_a (ohci) | RH_A_NPS) & ~RH_A_PSM,
1661 &ohci->regs->roothub.a);
1662 writel (RH_HS_LPSC, &ohci->regs->roothub.status);
1663 #endif /* OHCI_USE_NPS */
1665 #define mdelay(n) ({unsigned long msec=(n); while (msec--) udelay(1000);})
1666 /* POTPGT delay is bits 24-31, in 2 ms units. */
1667 mdelay ((roothub_a (ohci) >> 23) & 0x1fe);
1669 /* connect the virtual root hub */
1670 ohci->rh.devnum = 0;
1675 /*-------------------------------------------------------------------------*/
1677 /* Poll USB interrupt. */
1678 void usb_event_poll(void)
1683 /* an interrupt happens */
1685 static int hc_interrupt (void)
1687 ohci_t *ohci = &gohci;
1688 struct ohci_regs *regs = ohci->regs;
1692 if ((ohci->hcca->done_head != 0) &&
1693 !(m32_swap (ohci->hcca->done_head) & 0x01)) {
1694 ints = OHCI_INTR_WDH;
1695 } else if ((ints = readl (®s->intrstatus)) == ~(u32)0) {
1697 err ("%s device removed!", ohci->slot_name);
1699 } else if ((ints &= readl (®s->intrenable)) == 0) {
1700 dbg("hc_interrupt: returning..\n");
1704 /* dbg("Interrupt: %x frame: %x", ints, le16_to_cpu (ohci->hcca->frame_no)); */
1706 if (ints & OHCI_INTR_RHSC) {
1711 if (ints & OHCI_INTR_UE) {
1713 err ("OHCI Unrecoverable Error, controller usb-%s disabled",
1715 /* e.g. due to PCI Master/Target Abort */
1718 ohci_dump (ohci, 1);
1722 /* FIXME: be optimistic, hope that bug won't repeat often. */
1723 /* Make some non-interrupt context restart the controller. */
1724 /* Count and limit the retries though; either hardware or */
1725 /* software errors can go forever... */
1730 if (ints & OHCI_INTR_WDH) {
1732 writel (OHCI_INTR_WDH, ®s->intrdisable);
1733 (void)readl (®s->intrdisable); /* flush */
1734 stat = dl_done_list (&gohci, dl_reverse_done_list (&gohci));
1735 writel (OHCI_INTR_WDH, ®s->intrenable);
1736 (void)readl (®s->intrdisable); /* flush */
1739 if (ints & OHCI_INTR_SO) {
1740 dbg("USB Schedule overrun\n");
1741 writel (OHCI_INTR_SO, ®s->intrenable);
1745 /* FIXME: this assumes SOF (1/ms) interrupts don't get lost... */
1746 if (ints & OHCI_INTR_SF) {
1747 unsigned int frame = m16_swap (ohci->hcca->frame_no) & 1;
1749 writel (OHCI_INTR_SF, ®s->intrdisable);
1750 if (ohci->ed_rm_list[frame] != NULL)
1751 writel (OHCI_INTR_SF, ®s->intrenable);
1755 writel (ints, ®s->intrstatus);
1759 /*-------------------------------------------------------------------------*/
1761 /*-------------------------------------------------------------------------*/
1763 /* De-allocate all resources.. */
1765 static void hc_release_ohci (ohci_t *ohci)
1767 dbg ("USB HC release ohci usb-%s", ohci->slot_name);
1769 if (!ohci->disabled)
1773 /*-------------------------------------------------------------------------*/
1776 * low level initalisation routine, called from usb.c
1778 static char ohci_inited = 0;
1780 int usb_lowlevel_init(void)
1782 #ifdef CONFIG_PCI_OHCI
1786 #ifdef CFG_USB_OHCI_CPU_INIT
1787 /* cpu dependant init */
1792 #ifdef CFG_USB_OHCI_BOARD_INIT
1793 /* board dependant init */
1794 if(usb_board_init())
1797 memset (&gohci, 0, sizeof (ohci_t));
1799 /* align the storage */
1800 if ((__u32)&ghcca[0] & 0xff) {
1801 err("HCCA not aligned!!");
1805 info("aligned ghcca %p", phcca);
1806 memset(&ohci_dev, 0, sizeof(struct ohci_device));
1807 if ((__u32)&ohci_dev.ed[0] & 0x7) {
1808 err("EDs not aligned!!");
1811 memset(gtd, 0, sizeof(td_t) * (NUM_TD + 1));
1812 if ((__u32)gtd & 0x7) {
1813 err("TDs not aligned!!");
1818 memset (phcca, 0, sizeof (struct ohci_hcca));
1823 #ifdef CONFIG_PCI_OHCI
1824 pdev = pci_find_devices(ohci_pci_ids, CONFIG_PCI_OHCI_DEVNO);
1829 pci_read_config_word(pdev, PCI_VENDOR_ID, &vid);
1830 pci_read_config_word(pdev, PCI_DEVICE_ID, &did);
1831 printf("OHCI pci controller (%04x, %04x) found @(%d:%d:%d)\n",
1832 vid, did, (pdev >> 16) & 0xff,
1833 (pdev >> 11) & 0x1f, (pdev >> 8) & 0x7);
1834 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0, &base);
1835 printf("OHCI regs address 0x%08x\n", base);
1836 gohci.regs = (struct ohci_regs *)base;
1840 gohci.regs = (struct ohci_regs *)CFG_USB_OHCI_REGS_BASE;
1844 gohci.slot_name = CFG_USB_OHCI_SLOT_NAME;
1846 if (hc_reset (&gohci) < 0) {
1847 hc_release_ohci (&gohci);
1848 err ("can't reset usb-%s", gohci.slot_name);
1849 #ifdef CFG_USB_OHCI_BOARD_INIT
1850 /* board dependant cleanup */
1851 usb_board_init_fail();
1854 #ifdef CFG_USB_OHCI_CPU_INIT
1855 /* cpu dependant cleanup */
1856 usb_cpu_init_fail();
1861 /* FIXME this is a second HC reset; why?? */
1862 /* writel(gohci.hc_control = OHCI_USB_RESET, &gohci.regs->control);
1864 if (hc_start (&gohci) < 0) {
1865 err ("can't start usb-%s", gohci.slot_name);
1866 hc_release_ohci (&gohci);
1867 /* Initialization failed */
1868 #ifdef CFG_USB_OHCI_BOARD_INIT
1869 /* board dependant cleanup */
1873 #ifdef CFG_USB_OHCI_CPU_INIT
1874 /* cpu dependant cleanup */
1881 ohci_dump (&gohci, 1);
1889 int usb_lowlevel_stop(void)
1891 /* this gets called really early - before the controller has */
1892 /* even been initialized! */
1895 /* TODO release any interrupts, etc. */
1896 /* call hc_release_ohci() here ? */
1899 #ifdef CFG_USB_OHCI_BOARD_INIT
1900 /* board dependant cleanup */
1901 if(usb_board_stop())
1905 #ifdef CFG_USB_OHCI_CPU_INIT
1906 /* cpu dependant cleanup */
1913 #endif /* CONFIG_USB_OHCI_NEW */