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 * 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!
49 #include <asm/byteorder.h>
51 #if defined(CONFIG_PCI_OHCI)
53 #if !defined(CONFIG_PCI_OHCI_DEVNO)
54 #define CONFIG_PCI_OHCI_DEVNO 0
63 #ifdef CONFIG_AT91RM9200
64 #include <asm/arch/hardware.h> /* needed for AT91_USB_HOST_BASE */
67 #if defined(CONFIG_ARM920T) || \
68 defined(CONFIG_S3C24X0) || \
69 defined(CONFIG_440EP) || \
70 defined(CONFIG_PCI_OHCI) || \
71 defined(CONFIG_MPC5200) || \
72 defined(CONFIG_SYS_OHCI_USE_NPS)
73 # define OHCI_USE_NPS /* force NoPowerSwitching mode */
76 #undef OHCI_VERBOSE_DEBUG /* not always helpful */
79 #undef OHCI_FILL_TRACE
81 /* For initializing controller (mask in an HCFS mode too) */
82 #define OHCI_CONTROL_INIT \
83 (OHCI_CTRL_CBSR & 0x3) | OHCI_CTRL_IE | OHCI_CTRL_PLE
85 #define min_t(type, x, y) \
86 ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
88 #ifdef CONFIG_PCI_OHCI
89 static struct pci_device_id ohci_pci_ids
[] = {
90 {0x10b9, 0x5237}, /* ULI1575 PCI OHCI module ids */
91 {0x1033, 0x0035}, /* NEC PCI OHCI module ids */
92 {0x1131, 0x1561}, /* Philips 1561 PCI OHCI module ids */
93 /* Please add supported PCI OHCI controller ids here */
98 #ifdef CONFIG_PCI_EHCI_DEVNO
99 static struct pci_device_id ehci_pci_ids
[] = {
100 {0x1131, 0x1562}, /* Philips 1562 PCI EHCI module ids */
101 /* Please add supported PCI EHCI controller ids here */
107 #define dbg(format, arg...) printf("DEBUG: " format "\n", ## arg)
109 #define dbg(format, arg...) do {} while (0)
111 #define err(format, arg...) printf("ERROR: " format "\n", ## arg)
113 #define info(format, arg...) printf("INFO: " format "\n", ## arg)
115 #define info(format, arg...) do {} while (0)
118 #ifdef CONFIG_SYS_OHCI_BE_CONTROLLER
119 # define m16_swap(x) cpu_to_be16(x)
120 # define m32_swap(x) cpu_to_be32(x)
122 # define m16_swap(x) cpu_to_le16(x)
123 # define m32_swap(x) cpu_to_le32(x)
124 #endif /* CONFIG_SYS_OHCI_BE_CONTROLLER */
128 /* this must be aligned to a 256 byte boundary */
129 struct ohci_hcca ghcca
[1];
130 /* a pointer to the aligned storage */
131 struct ohci_hcca
*phcca
;
132 /* this allocates EDs for all possible endpoints */
133 struct ohci_device ohci_dev
;
134 /* device which was disconnected */
135 struct usb_device
*devgone
;
137 static inline u32
roothub_a(struct ohci
*hc
)
138 { return ohci_readl(&hc
->regs
->roothub
.a
); }
139 static inline u32
roothub_b(struct ohci
*hc
)
140 { return ohci_readl(&hc
->regs
->roothub
.b
); }
141 static inline u32
roothub_status(struct ohci
*hc
)
142 { return ohci_readl(&hc
->regs
->roothub
.status
); }
143 static inline u32
roothub_portstatus(struct ohci
*hc
, int i
)
144 { return ohci_readl(&hc
->regs
->roothub
.portstatus
[i
]); }
146 /* forward declaration */
147 static int hc_interrupt(void);
148 static void td_submit_job(struct usb_device
*dev
, unsigned long pipe
,
149 void *buffer
, int transfer_len
,
150 struct devrequest
*setup
, urb_priv_t
*urb
,
153 /*-------------------------------------------------------------------------*
154 * URB support functions
155 *-------------------------------------------------------------------------*/
157 /* free HCD-private data associated with this URB */
159 static void urb_free_priv(urb_priv_t
*urb
)
165 last
= urb
->length
- 1;
167 for (i
= 0; i
<= last
; i
++) {
178 /*-------------------------------------------------------------------------*/
181 static int sohci_get_current_frame_number(struct usb_device
*dev
);
183 /* debug| print the main components of an URB
184 * small: 0) header + data packets 1) just header */
186 static void pkt_print(urb_priv_t
*purb
, struct usb_device
*dev
,
187 unsigned long pipe
, void *buffer
, int transfer_len
,
188 struct devrequest
*setup
, char *str
, int small
)
190 dbg("%s URB:[%4x] dev:%2lu,ep:%2lu-%c,type:%s,len:%d/%d stat:%#lx",
192 sohci_get_current_frame_number(dev
),
193 usb_pipedevice(pipe
),
194 usb_pipeendpoint(pipe
),
195 usb_pipeout(pipe
)? 'O': 'I',
196 usb_pipetype(pipe
) < 2 ? \
197 (usb_pipeint(pipe
)? "INTR": "ISOC"): \
198 (usb_pipecontrol(pipe
)? "CTRL": "BULK"),
199 (purb
? purb
->actual_length
: 0),
200 transfer_len
, dev
->status
);
201 #ifdef OHCI_VERBOSE_DEBUG
205 if (usb_pipecontrol(pipe
)) {
206 printf(__FILE__
": cmd(8):");
207 for (i
= 0; i
< 8 ; i
++)
208 printf(" %02x", ((__u8
*) setup
) [i
]);
211 if (transfer_len
> 0 && buffer
) {
212 printf(__FILE__
": data(%d/%d):",
213 (purb
? purb
->actual_length
: 0),
215 len
= usb_pipeout(pipe
)? transfer_len
:
216 (purb
? purb
->actual_length
: 0);
217 for (i
= 0; i
< 16 && i
< len
; i
++)
218 printf(" %02x", ((__u8
*) buffer
) [i
]);
219 printf("%s\n", i
< len
? "...": "");
225 /* just for debugging; prints non-empty branches of the int ed tree
226 * inclusive iso eds */
227 void ep_print_int_eds(ohci_t
*ohci
, char *str
)
231 for (i
= 0; i
< 32; i
++) {
233 ed_p
= &(ohci
->hcca
->int_table
[i
]);
236 printf(__FILE__
": %s branch int %2d(%2x):", str
, i
, i
);
237 while (*ed_p
!= 0 && j
--) {
238 ed_t
*ed
= (ed_t
*)m32_swap(ed_p
);
239 printf(" ed: %4x;", ed
->hwINFO
);
240 ed_p
= &ed
->hwNextED
;
246 static void ohci_dump_intr_mask(char *label
, __u32 mask
)
248 dbg("%s: 0x%08x%s%s%s%s%s%s%s%s%s",
251 (mask
& OHCI_INTR_MIE
) ? " MIE" : "",
252 (mask
& OHCI_INTR_OC
) ? " OC" : "",
253 (mask
& OHCI_INTR_RHSC
) ? " RHSC" : "",
254 (mask
& OHCI_INTR_FNO
) ? " FNO" : "",
255 (mask
& OHCI_INTR_UE
) ? " UE" : "",
256 (mask
& OHCI_INTR_RD
) ? " RD" : "",
257 (mask
& OHCI_INTR_SF
) ? " SF" : "",
258 (mask
& OHCI_INTR_WDH
) ? " WDH" : "",
259 (mask
& OHCI_INTR_SO
) ? " SO" : ""
263 static void maybe_print_eds(char *label
, __u32 value
)
265 ed_t
*edp
= (ed_t
*)value
;
268 dbg("%s %08x", label
, value
);
269 dbg("%08x", edp
->hwINFO
);
270 dbg("%08x", edp
->hwTailP
);
271 dbg("%08x", edp
->hwHeadP
);
272 dbg("%08x", edp
->hwNextED
);
276 static char *hcfs2string(int state
)
279 case OHCI_USB_RESET
: return "reset";
280 case OHCI_USB_RESUME
: return "resume";
281 case OHCI_USB_OPER
: return "operational";
282 case OHCI_USB_SUSPEND
: return "suspend";
287 /* dump control and status registers */
288 static void ohci_dump_status(ohci_t
*controller
)
290 struct ohci_regs
*regs
= controller
->regs
;
293 temp
= ohci_readl(®s
->revision
) & 0xff;
295 dbg("spec %d.%d", (temp
>> 4), (temp
& 0x0f));
297 temp
= ohci_readl(®s
->control
);
298 dbg("control: 0x%08x%s%s%s HCFS=%s%s%s%s%s CBSR=%d", temp
,
299 (temp
& OHCI_CTRL_RWE
) ? " RWE" : "",
300 (temp
& OHCI_CTRL_RWC
) ? " RWC" : "",
301 (temp
& OHCI_CTRL_IR
) ? " IR" : "",
302 hcfs2string(temp
& OHCI_CTRL_HCFS
),
303 (temp
& OHCI_CTRL_BLE
) ? " BLE" : "",
304 (temp
& OHCI_CTRL_CLE
) ? " CLE" : "",
305 (temp
& OHCI_CTRL_IE
) ? " IE" : "",
306 (temp
& OHCI_CTRL_PLE
) ? " PLE" : "",
307 temp
& OHCI_CTRL_CBSR
310 temp
= ohci_readl(®s
->cmdstatus
);
311 dbg("cmdstatus: 0x%08x SOC=%d%s%s%s%s", temp
,
312 (temp
& OHCI_SOC
) >> 16,
313 (temp
& OHCI_OCR
) ? " OCR" : "",
314 (temp
& OHCI_BLF
) ? " BLF" : "",
315 (temp
& OHCI_CLF
) ? " CLF" : "",
316 (temp
& OHCI_HCR
) ? " HCR" : ""
319 ohci_dump_intr_mask("intrstatus", ohci_readl(®s
->intrstatus
));
320 ohci_dump_intr_mask("intrenable", ohci_readl(®s
->intrenable
));
322 maybe_print_eds("ed_periodcurrent",
323 ohci_readl(®s
->ed_periodcurrent
));
325 maybe_print_eds("ed_controlhead", ohci_readl(®s
->ed_controlhead
));
326 maybe_print_eds("ed_controlcurrent",
327 ohci_readl(®s
->ed_controlcurrent
));
329 maybe_print_eds("ed_bulkhead", ohci_readl(®s
->ed_bulkhead
));
330 maybe_print_eds("ed_bulkcurrent", ohci_readl(®s
->ed_bulkcurrent
));
332 maybe_print_eds("donehead", ohci_readl(®s
->donehead
));
335 static void ohci_dump_roothub(ohci_t
*controller
, int verbose
)
339 temp
= roothub_a(controller
);
340 ndp
= (temp
& RH_A_NDP
);
341 #ifdef CONFIG_AT91C_PQFP_UHPBUG
342 ndp
= (ndp
== 2) ? 1:0;
345 dbg("roothub.a: %08x POTPGT=%d%s%s%s%s%s NDP=%d", temp
,
346 ((temp
& RH_A_POTPGT
) >> 24) & 0xff,
347 (temp
& RH_A_NOCP
) ? " NOCP" : "",
348 (temp
& RH_A_OCPM
) ? " OCPM" : "",
349 (temp
& RH_A_DT
) ? " DT" : "",
350 (temp
& RH_A_NPS
) ? " NPS" : "",
351 (temp
& RH_A_PSM
) ? " PSM" : "",
354 temp
= roothub_b(controller
);
355 dbg("roothub.b: %08x PPCM=%04x DR=%04x",
357 (temp
& RH_B_PPCM
) >> 16,
360 temp
= roothub_status(controller
);
361 dbg("roothub.status: %08x%s%s%s%s%s%s",
363 (temp
& RH_HS_CRWE
) ? " CRWE" : "",
364 (temp
& RH_HS_OCIC
) ? " OCIC" : "",
365 (temp
& RH_HS_LPSC
) ? " LPSC" : "",
366 (temp
& RH_HS_DRWE
) ? " DRWE" : "",
367 (temp
& RH_HS_OCI
) ? " OCI" : "",
368 (temp
& RH_HS_LPS
) ? " LPS" : ""
372 for (i
= 0; i
< ndp
; i
++) {
373 temp
= roothub_portstatus(controller
, i
);
374 dbg("roothub.portstatus [%d] = 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s",
377 (temp
& RH_PS_PRSC
) ? " PRSC" : "",
378 (temp
& RH_PS_OCIC
) ? " OCIC" : "",
379 (temp
& RH_PS_PSSC
) ? " PSSC" : "",
380 (temp
& RH_PS_PESC
) ? " PESC" : "",
381 (temp
& RH_PS_CSC
) ? " CSC" : "",
383 (temp
& RH_PS_LSDA
) ? " LSDA" : "",
384 (temp
& RH_PS_PPS
) ? " PPS" : "",
385 (temp
& RH_PS_PRS
) ? " PRS" : "",
386 (temp
& RH_PS_POCI
) ? " POCI" : "",
387 (temp
& RH_PS_PSS
) ? " PSS" : "",
389 (temp
& RH_PS_PES
) ? " PES" : "",
390 (temp
& RH_PS_CCS
) ? " CCS" : ""
395 static void ohci_dump(ohci_t
*controller
, int verbose
)
397 dbg("OHCI controller usb-%s state", controller
->slot_name
);
399 /* dumps some of the state we know about */
400 ohci_dump_status(controller
);
402 ep_print_int_eds(controller
, "hcca");
403 dbg("hcca frame #%04x", controller
->hcca
->frame_no
);
404 ohci_dump_roothub(controller
, 1);
408 /*-------------------------------------------------------------------------*
409 * Interface functions (URB)
410 *-------------------------------------------------------------------------*/
412 /* get a transfer request */
414 int sohci_submit_job(urb_priv_t
*urb
, struct devrequest
*setup
)
418 urb_priv_t
*purb_priv
= urb
;
420 struct usb_device
*dev
= urb
->dev
;
421 unsigned long pipe
= urb
->pipe
;
422 void *buffer
= urb
->transfer_buffer
;
423 int transfer_len
= urb
->transfer_buffer_length
;
424 int interval
= urb
->interval
;
428 /* when controller's hung, permit only roothub cleanup attempts
429 * such as powering down ports */
430 if (ohci
->disabled
) {
431 err("sohci_submit_job: EPIPE");
435 /* we're about to begin a new transaction here so mark the
439 /* every endpoint has a ed, locate and fill it */
440 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
,
497 setup
, purb_priv
, interval
);
502 static inline int sohci_return_job(struct ohci
*hc
, urb_priv_t
*urb
)
504 struct ohci_regs
*regs
= hc
->regs
;
506 switch (usb_pipetype(urb
->pipe
)) {
508 /* implicitly requeued */
509 if (urb
->dev
->irq_handle
&&
510 (urb
->dev
->irq_act_len
= urb
->actual_length
)) {
511 ohci_writel(OHCI_INTR_WDH
, ®s
->intrenable
);
512 ohci_readl(®s
->intrenable
); /* PCI posting flush */
513 urb
->dev
->irq_handle(urb
->dev
);
514 ohci_writel(OHCI_INTR_WDH
, ®s
->intrdisable
);
515 ohci_readl(®s
->intrdisable
); /* PCI posting flush */
517 urb
->actual_length
= 0;
521 urb
->transfer_buffer
,
522 urb
->transfer_buffer_length
,
536 /*-------------------------------------------------------------------------*/
539 /* tell us the current USB frame number */
541 static int sohci_get_current_frame_number(struct usb_device
*usb_dev
)
543 ohci_t
*ohci
= &gohci
;
545 return m16_swap(ohci
->hcca
->frame_no
);
549 /*-------------------------------------------------------------------------*
550 * ED handling functions
551 *-------------------------------------------------------------------------*/
553 /* search for the right branch to insert an interrupt ed into the int tree
554 * do some load ballancing;
555 * returns the branch and
556 * sets the interval to interval = 2^integer (ld (interval)) */
558 static int ep_int_ballance(ohci_t
*ohci
, int interval
, int load
)
562 /* search for the least loaded interrupt endpoint
563 * branch of all 32 branches
565 for (i
= 0; i
< 32; i
++)
566 if (ohci
->ohci_int_load
[branch
] > ohci
->ohci_int_load
[i
])
569 branch
= branch
% interval
;
570 for (i
= branch
; i
< 32; i
+= interval
)
571 ohci
->ohci_int_load
[i
] += load
;
576 /*-------------------------------------------------------------------------*/
578 /* 2^int( ld (inter)) */
580 static int ep_2_n_interval(int inter
)
583 for (i
= 0; ((inter
>> i
) > 1) && (i
< 5); i
++);
587 /*-------------------------------------------------------------------------*/
589 /* the int tree is a binary tree
590 * in order to process it sequentially the indexes of the branches have to
591 * be mapped 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 ohci_writel(ed
, &ohci
->regs
->ed_controlhead
);
626 ohci
->ed_controltail
->hwNextED
=
627 m32_swap((unsigned long)ed
);
629 ed
->ed_prev
= ohci
->ed_controltail
;
630 if (!ohci
->ed_controltail
&& !ohci
->ed_rm_list
[0] &&
631 !ohci
->ed_rm_list
[1] && !ohci
->sleeping
) {
632 ohci
->hc_control
|= OHCI_CTRL_CLE
;
633 ohci_writel(ohci
->hc_control
, &ohci
->regs
->control
);
635 ohci
->ed_controltail
= edi
;
640 if (ohci
->ed_bulktail
== NULL
)
641 ohci_writel(ed
, &ohci
->regs
->ed_bulkhead
);
643 ohci
->ed_bulktail
->hwNextED
=
644 m32_swap((unsigned long)ed
);
646 ed
->ed_prev
= ohci
->ed_bulktail
;
647 if (!ohci
->ed_bulktail
&& !ohci
->ed_rm_list
[0] &&
648 !ohci
->ed_rm_list
[1] && !ohci
->sleeping
) {
649 ohci
->hc_control
|= OHCI_CTRL_BLE
;
650 ohci_writel(ohci
->hc_control
, &ohci
->regs
->control
);
652 ohci
->ed_bulktail
= edi
;
657 interval
= ep_2_n_interval(ed
->int_period
);
658 ed
->int_interval
= interval
;
659 int_branch
= ep_int_ballance(ohci
, interval
, load
);
660 ed
->int_branch
= int_branch
;
662 for (i
= 0; i
< ep_rev(6, interval
); i
+= inter
) {
664 for (ed_p
= &(ohci
->hcca
->int_table
[\
665 ep_rev(5, i
) + int_branch
]);
667 (((ed_t
*)ed_p
)->int_interval
>= interval
);
668 ed_p
= &(((ed_t
*)ed_p
)->hwNextED
))
670 ((ed_t
*)ed_p
)->int_interval
);
671 ed
->hwNextED
= *ed_p
;
672 *ed_p
= m32_swap((unsigned long)ed
);
679 /*-------------------------------------------------------------------------*/
681 /* scan the periodic table to find and unlink this ED */
682 static void periodic_unlink(struct ohci
*ohci
, volatile struct ed
*ed
,
683 unsigned index
, unsigned period
)
685 for (; index
< NUM_INTS
; index
+= period
) {
686 __u32
*ed_p
= &ohci
->hcca
->int_table
[index
];
688 /* ED might have been unlinked through another path */
691 m32_swap((unsigned long)ed_p
)) == ed
) {
692 *ed_p
= ed
->hwNextED
;
695 ed_p
= &(((struct ed
*)
696 m32_swap((unsigned long)ed_p
))->hwNextED
);
701 /* unlink an ed from one of the HC chains.
702 * just the link to the ed is unlinked.
703 * the link from the ed still points to another operational ed or 0
704 * so the HC can eventually finish the processing of the unlinked ed */
706 static int ep_unlink(ohci_t
*ohci
, ed_t
*edi
)
708 volatile ed_t
*ed
= edi
;
711 ed
->hwINFO
|= m32_swap(OHCI_ED_SKIP
);
715 if (ed
->ed_prev
== NULL
) {
717 ohci
->hc_control
&= ~OHCI_CTRL_CLE
;
718 ohci_writel(ohci
->hc_control
,
719 &ohci
->regs
->control
);
721 ohci_writel(m32_swap(*((__u32
*)&ed
->hwNextED
)),
722 &ohci
->regs
->ed_controlhead
);
724 ed
->ed_prev
->hwNextED
= ed
->hwNextED
;
726 if (ohci
->ed_controltail
== ed
) {
727 ohci
->ed_controltail
= ed
->ed_prev
;
730 *((__u32
*)&ed
->hwNextED
)))->ed_prev
= ed
->ed_prev
;
735 if (ed
->ed_prev
== NULL
) {
737 ohci
->hc_control
&= ~OHCI_CTRL_BLE
;
738 ohci_writel(ohci
->hc_control
,
739 &ohci
->regs
->control
);
741 ohci_writel(m32_swap(*((__u32
*)&ed
->hwNextED
)),
742 &ohci
->regs
->ed_bulkhead
);
744 ed
->ed_prev
->hwNextED
= ed
->hwNextED
;
746 if (ohci
->ed_bulktail
== ed
) {
747 ohci
->ed_bulktail
= ed
->ed_prev
;
750 *((__u32
*)&ed
->hwNextED
)))->ed_prev
= ed
->ed_prev
;
755 periodic_unlink(ohci
, ed
, 0, 1);
756 for (i
= ed
->int_branch
; i
< 32; i
+= ed
->int_interval
)
757 ohci
->ohci_int_load
[i
] -= ed
->int_load
;
760 ed
->state
= ED_UNLINK
;
764 /*-------------------------------------------------------------------------*/
766 /* add/reinit an endpoint; this should be done once at the
767 * usb_set_configuration command, but the USB stack is a little bit
768 * stateless so we do it at every transaction if the state of the ed
769 * is ED_NEW then a dummy td is added and the state is changed to
770 * ED_UNLINK in all other cases the state is left unchanged the ed
771 * info fields are setted anyway even though most of them should not
774 static ed_t
*ep_add_ed(struct usb_device
*usb_dev
, unsigned long pipe
,
775 int interval
, int load
)
781 ed
= ed_ret
= &ohci_dev
.ed
[(usb_pipeendpoint(pipe
) << 1) |
782 (usb_pipecontrol(pipe
)? 0: usb_pipeout(pipe
))];
784 if ((ed
->state
& ED_DEL
) || (ed
->state
& ED_URB_DEL
)) {
785 err("ep_add_ed: pending delete");
786 /* pending delete request */
790 if (ed
->state
== ED_NEW
) {
791 /* dummy td; end of td list for ed */
792 td
= td_alloc(usb_dev
);
793 ed
->hwTailP
= m32_swap((unsigned long)td
);
794 ed
->hwHeadP
= ed
->hwTailP
;
795 ed
->state
= ED_UNLINK
;
796 ed
->type
= usb_pipetype(pipe
);
800 ed
->hwINFO
= m32_swap(usb_pipedevice(pipe
)
801 | usb_pipeendpoint(pipe
) << 7
802 | (usb_pipeisoc(pipe
)? 0x8000: 0)
803 | (usb_pipecontrol(pipe
)? 0: \
804 (usb_pipeout(pipe
)? 0x800: 0x1000))
805 | (usb_dev
->speed
== USB_SPEED_LOW
) << 13
806 | usb_maxpacket(usb_dev
, pipe
) << 16);
808 if (ed
->type
== PIPE_INTERRUPT
&& ed
->state
== ED_UNLINK
) {
809 ed
->int_period
= interval
;
816 /*-------------------------------------------------------------------------*
817 * TD handling functions
818 *-------------------------------------------------------------------------*/
820 /* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
822 static void td_fill(ohci_t
*ohci
, unsigned int info
,
824 struct usb_device
*dev
, int index
, urb_priv_t
*urb_priv
)
826 volatile td_t
*td
, *td_pt
;
827 #ifdef OHCI_FILL_TRACE
831 if (index
> urb_priv
->length
) {
832 err("index > length");
835 /* use this td as the next dummy */
836 td_pt
= urb_priv
->td
[index
];
839 /* fill the old dummy TD */
840 td
= urb_priv
->td
[index
] =
841 (td_t
*)(m32_swap(urb_priv
->ed
->hwTailP
) & ~0xf);
843 td
->ed
= urb_priv
->ed
;
844 td
->next_dl_td
= NULL
;
846 td
->data
= (__u32
)data
;
847 #ifdef OHCI_FILL_TRACE
848 if (usb_pipebulk(urb_priv
->pipe
) && usb_pipeout(urb_priv
->pipe
)) {
849 for (i
= 0; i
< len
; i
++)
850 printf("td->data[%d] %#2x ", i
, ((unsigned char *)td
->data
)[i
]);
857 td
->hwINFO
= m32_swap(info
);
858 td
->hwCBP
= m32_swap((unsigned long)data
);
860 td
->hwBE
= m32_swap((unsigned long)(data
+ len
- 1));
864 td
->hwNextTD
= m32_swap((unsigned long)td_pt
);
866 /* append to queue */
867 td
->ed
->hwTailP
= td
->hwNextTD
;
870 /*-------------------------------------------------------------------------*/
872 /* prepare all TDs of a transfer */
874 static void td_submit_job(struct usb_device
*dev
, unsigned long pipe
,
875 void *buffer
, int transfer_len
,
876 struct devrequest
*setup
, urb_priv_t
*urb
,
879 ohci_t
*ohci
= &gohci
;
880 int data_len
= transfer_len
;
884 unsigned int toggle
= 0;
886 /* OHCI handles the DATA-toggles itself, we just use the USB-toggle
887 * bits for reseting */
888 if (usb_gettoggle(dev
, usb_pipeendpoint(pipe
), usb_pipeout(pipe
))) {
889 toggle
= TD_T_TOGGLE
;
892 usb_settoggle(dev
, usb_pipeendpoint(pipe
),
893 usb_pipeout(pipe
), 1);
901 switch (usb_pipetype(pipe
)) {
903 info
= usb_pipeout(pipe
)?
904 TD_CC
| TD_DP_OUT
: TD_CC
| TD_DP_IN
;
905 while (data_len
> 4096) {
906 td_fill(ohci
, info
| (cnt
? TD_T_TOGGLE
:toggle
),
907 data
, 4096, dev
, cnt
, urb
);
908 data
+= 4096; data_len
-= 4096; cnt
++;
910 info
= usb_pipeout(pipe
)?
911 TD_CC
| TD_DP_OUT
: TD_CC
| TD_R
| TD_DP_IN
;
912 td_fill(ohci
, info
| (cnt
? TD_T_TOGGLE
:toggle
), data
,
913 data_len
, dev
, cnt
, urb
);
916 if (!ohci
->sleeping
) {
917 /* start bulk list */
918 ohci_writel(OHCI_BLF
, &ohci
->regs
->cmdstatus
);
924 info
= TD_CC
| TD_DP_SETUP
| TD_T_DATA0
;
925 td_fill(ohci
, info
, setup
, 8, dev
, cnt
++, urb
);
927 /* Optional Data phase */
929 info
= usb_pipeout(pipe
)?
930 TD_CC
| TD_R
| TD_DP_OUT
| TD_T_DATA1
:
931 TD_CC
| TD_R
| TD_DP_IN
| TD_T_DATA1
;
932 /* NOTE: mishandles transfers >8K, some >4K */
933 td_fill(ohci
, info
, data
, data_len
, dev
, cnt
++, urb
);
937 info
= usb_pipeout(pipe
)?
938 TD_CC
| TD_DP_IN
| TD_T_DATA1
:
939 TD_CC
| TD_DP_OUT
| TD_T_DATA1
;
940 td_fill(ohci
, info
, data
, 0, dev
, cnt
++, urb
);
942 if (!ohci
->sleeping
) {
943 /* start Control list */
944 ohci_writel(OHCI_CLF
, &ohci
->regs
->cmdstatus
);
949 info
= usb_pipeout(urb
->pipe
)?
950 TD_CC
| TD_DP_OUT
| toggle
:
951 TD_CC
| TD_R
| TD_DP_IN
| toggle
;
952 td_fill(ohci
, info
, data
, data_len
, dev
, cnt
++, urb
);
955 if (urb
->length
!= cnt
)
956 dbg("TD LENGTH %d != CNT %d", urb
->length
, cnt
);
959 /*-------------------------------------------------------------------------*
960 * Done List handling functions
961 *-------------------------------------------------------------------------*/
963 /* calculate the transfer length and update the urb */
965 static void dl_transfer_length(td_t
*td
)
968 urb_priv_t
*lurb_priv
= td
->ed
->purb
;
970 tdBE
= m32_swap(td
->hwBE
);
971 tdCBP
= m32_swap(td
->hwCBP
);
973 if (!(usb_pipecontrol(lurb_priv
->pipe
) &&
974 ((td
->index
== 0) || (td
->index
== lurb_priv
->length
- 1)))) {
977 lurb_priv
->actual_length
+= tdBE
- td
->data
+ 1;
979 lurb_priv
->actual_length
+= tdCBP
- td
->data
;
984 /*-------------------------------------------------------------------------*/
985 static void check_status(td_t
*td_list
)
987 urb_priv_t
*lurb_priv
= td_list
->ed
->purb
;
988 int urb_len
= lurb_priv
->length
;
989 __u32
*phwHeadP
= &td_list
->ed
->hwHeadP
;
992 cc
= TD_CC_GET(m32_swap(td_list
->hwINFO
));
994 err(" USB-error: %s (%x)", cc_to_string
[cc
], cc
);
996 if (*phwHeadP
& m32_swap(0x1)) {
998 ((td_list
->index
+ 1) < urb_len
)) {
1000 (lurb_priv
->td
[urb_len
- 1]->hwNextTD
&\
1001 m32_swap(0xfffffff0)) |
1002 (*phwHeadP
& m32_swap(0x2));
1004 lurb_priv
->td_cnt
+= urb_len
-
1007 *phwHeadP
&= m32_swap(0xfffffff2);
1009 #ifdef CONFIG_MPC5200
1010 td_list
->hwNextTD
= 0;
1015 /* replies to the request have to be on a FIFO basis so
1016 * we reverse the reversed done-list */
1017 static td_t
*dl_reverse_done_list(ohci_t
*ohci
)
1020 td_t
*td_rev
= NULL
;
1021 td_t
*td_list
= NULL
;
1023 td_list_hc
= m32_swap(ohci
->hcca
->done_head
) & 0xfffffff0;
1024 ohci
->hcca
->done_head
= 0;
1026 while (td_list_hc
) {
1027 td_list
= (td_t
*)td_list_hc
;
1028 check_status(td_list
);
1029 td_list
->next_dl_td
= td_rev
;
1031 td_list_hc
= m32_swap(td_list
->hwNextTD
) & 0xfffffff0;
1036 /*-------------------------------------------------------------------------*/
1037 /*-------------------------------------------------------------------------*/
1039 static void finish_urb(ohci_t
*ohci
, urb_priv_t
*urb
, int status
)
1041 if ((status
& (ED_OPER
| ED_UNLINK
)) && (urb
->state
!= URB_DEL
))
1042 urb
->finished
= sohci_return_job(ohci
, urb
);
1044 dbg("finish_urb: strange.., ED state %x, \n", status
);
1048 * Used to take back a TD from the host controller. This would normally be
1049 * called from within dl_done_list, however it may be called directly if the
1050 * HC no longer sees the TD and it has not appeared on the donelist (after
1051 * two frames). This bug has been observed on ZF Micro systems.
1053 static int takeback_td(ohci_t
*ohci
, td_t
*td_list
)
1059 urb_priv_t
*lurb_priv
;
1060 __u32 tdINFO
, edHeadP
, edTailP
;
1062 tdINFO
= m32_swap(td_list
->hwINFO
);
1065 lurb_priv
= ed
->purb
;
1067 dl_transfer_length(td_list
);
1069 lurb_priv
->td_cnt
++;
1071 /* error code of transfer */
1072 cc
= TD_CC_GET(tdINFO
);
1074 err("USB-error: %s (%x)", cc_to_string
[cc
], cc
);
1075 stat
= cc_to_error
[cc
];
1078 /* see if this done list makes for all TD's of current URB,
1079 * and mark the URB finished if so */
1080 if (lurb_priv
->td_cnt
== lurb_priv
->length
)
1081 finish_urb(ohci
, lurb_priv
, ed
->state
);
1083 dbg("dl_done_list: processing TD %x, len %x\n",
1084 lurb_priv
->td_cnt
, lurb_priv
->length
);
1086 if (ed
->state
!= ED_NEW
&& (!usb_pipeint(lurb_priv
->pipe
))) {
1087 edHeadP
= m32_swap(ed
->hwHeadP
) & 0xfffffff0;
1088 edTailP
= m32_swap(ed
->hwTailP
);
1090 /* unlink eds if they are not busy */
1091 if ((edHeadP
== edTailP
) && (ed
->state
== ED_OPER
))
1092 ep_unlink(ohci
, ed
);
1097 static int dl_done_list(ohci_t
*ohci
)
1100 td_t
*td_list
= dl_reverse_done_list(ohci
);
1103 td_t
*td_next
= td_list
->next_dl_td
;
1104 stat
= takeback_td(ohci
, td_list
);
1110 /*-------------------------------------------------------------------------*
1112 *-------------------------------------------------------------------------*/
1114 /* Device descriptor */
1115 static __u8 root_hub_dev_des
[] =
1117 0x12, /* __u8 bLength; */
1118 0x01, /* __u8 bDescriptorType; Device */
1119 0x10, /* __u16 bcdUSB; v1.1 */
1121 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
1122 0x00, /* __u8 bDeviceSubClass; */
1123 0x00, /* __u8 bDeviceProtocol; */
1124 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
1125 0x00, /* __u16 idVendor; */
1127 0x00, /* __u16 idProduct; */
1129 0x00, /* __u16 bcdDevice; */
1131 0x00, /* __u8 iManufacturer; */
1132 0x01, /* __u8 iProduct; */
1133 0x00, /* __u8 iSerialNumber; */
1134 0x01 /* __u8 bNumConfigurations; */
1137 /* Configuration descriptor */
1138 static __u8 root_hub_config_des
[] =
1140 0x09, /* __u8 bLength; */
1141 0x02, /* __u8 bDescriptorType; Configuration */
1142 0x19, /* __u16 wTotalLength; */
1144 0x01, /* __u8 bNumInterfaces; */
1145 0x01, /* __u8 bConfigurationValue; */
1146 0x00, /* __u8 iConfiguration; */
1147 0x40, /* __u8 bmAttributes;
1148 Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup, 4..0: resvd */
1149 0x00, /* __u8 MaxPower; */
1152 0x09, /* __u8 if_bLength; */
1153 0x04, /* __u8 if_bDescriptorType; Interface */
1154 0x00, /* __u8 if_bInterfaceNumber; */
1155 0x00, /* __u8 if_bAlternateSetting; */
1156 0x01, /* __u8 if_bNumEndpoints; */
1157 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
1158 0x00, /* __u8 if_bInterfaceSubClass; */
1159 0x00, /* __u8 if_bInterfaceProtocol; */
1160 0x00, /* __u8 if_iInterface; */
1163 0x07, /* __u8 ep_bLength; */
1164 0x05, /* __u8 ep_bDescriptorType; Endpoint */
1165 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
1166 0x03, /* __u8 ep_bmAttributes; Interrupt */
1167 0x02, /* __u16 ep_wMaxPacketSize; ((MAX_ROOT_PORTS + 1) / 8 */
1169 0xff /* __u8 ep_bInterval; 255 ms */
1172 static unsigned char root_hub_str_index0
[] =
1174 0x04, /* __u8 bLength; */
1175 0x03, /* __u8 bDescriptorType; String-descriptor */
1176 0x09, /* __u8 lang ID */
1177 0x04, /* __u8 lang ID */
1180 static unsigned char root_hub_str_index1
[] =
1182 28, /* __u8 bLength; */
1183 0x03, /* __u8 bDescriptorType; String-descriptor */
1184 'O', /* __u8 Unicode */
1185 0, /* __u8 Unicode */
1186 'H', /* __u8 Unicode */
1187 0, /* __u8 Unicode */
1188 'C', /* __u8 Unicode */
1189 0, /* __u8 Unicode */
1190 'I', /* __u8 Unicode */
1191 0, /* __u8 Unicode */
1192 ' ', /* __u8 Unicode */
1193 0, /* __u8 Unicode */
1194 'R', /* __u8 Unicode */
1195 0, /* __u8 Unicode */
1196 'o', /* __u8 Unicode */
1197 0, /* __u8 Unicode */
1198 'o', /* __u8 Unicode */
1199 0, /* __u8 Unicode */
1200 't', /* __u8 Unicode */
1201 0, /* __u8 Unicode */
1202 ' ', /* __u8 Unicode */
1203 0, /* __u8 Unicode */
1204 'H', /* __u8 Unicode */
1205 0, /* __u8 Unicode */
1206 'u', /* __u8 Unicode */
1207 0, /* __u8 Unicode */
1208 'b', /* __u8 Unicode */
1209 0, /* __u8 Unicode */
1212 /* Hub class-specific descriptor is constructed dynamically */
1214 /*-------------------------------------------------------------------------*/
1216 #define OK(x) len = (x); break
1218 #define WR_RH_STAT(x) {info("WR:status %#8x", (x)); ohci_writel((x), \
1219 &gohci.regs->roothub.status); }
1220 #define WR_RH_PORTSTAT(x) {info("WR:portstatus[%d] %#8x", wIndex-1, \
1221 (x)); ohci_writel((x), &gohci.regs->roothub.portstatus[wIndex-1]); }
1223 #define WR_RH_STAT(x) ohci_writel((x), &gohci.regs->roothub.status)
1224 #define WR_RH_PORTSTAT(x) ohci_writel((x), \
1225 &gohci.regs->roothub.portstatus[wIndex-1])
1227 #define RD_RH_STAT roothub_status(&gohci)
1228 #define RD_RH_PORTSTAT roothub_portstatus(&gohci, wIndex-1)
1230 /* request to virtual root hub */
1232 int rh_check_port_status(ohci_t
*controller
)
1238 temp
= roothub_a(controller
);
1239 ndp
= (temp
& RH_A_NDP
);
1240 #ifdef CONFIG_AT91C_PQFP_UHPBUG
1241 ndp
= (ndp
== 2) ? 1:0;
1243 for (i
= 0; i
< ndp
; i
++) {
1244 temp
= roothub_portstatus(controller
, i
);
1245 /* check for a device disconnect */
1246 if (((temp
& (RH_PS_PESC
| RH_PS_CSC
)) ==
1247 (RH_PS_PESC
| RH_PS_CSC
)) &&
1248 ((temp
& RH_PS_CCS
) == 0)) {
1256 static int ohci_submit_rh_msg(struct usb_device
*dev
, unsigned long pipe
,
1257 void *buffer
, int transfer_len
, struct devrequest
*cmd
)
1259 void *data
= buffer
;
1260 int leni
= transfer_len
;
1267 ALLOC_ALIGN_BUFFER(__u8
, databuf
, 16, sizeof(u32
));
1270 pkt_print(NULL
, dev
, pipe
, buffer
, transfer_len
,
1271 cmd
, "SUB(rh)", usb_pipein(pipe
));
1275 if (usb_pipeint(pipe
)) {
1276 info("Root-Hub submit IRQ: NOT implemented");
1280 bmRType_bReq
= cmd
->requesttype
| (cmd
->request
<< 8);
1281 wValue
= le16_to_cpu(cmd
->value
);
1282 wIndex
= le16_to_cpu(cmd
->index
);
1283 wLength
= le16_to_cpu(cmd
->length
);
1285 info("Root-Hub: adr: %2x cmd(%1x): %08x %04x %04x %04x",
1286 dev
->devnum
, 8, bmRType_bReq
, wValue
, wIndex
, wLength
);
1288 switch (bmRType_bReq
) {
1289 /* Request Destination:
1290 without flags: Device,
1291 RH_INTERFACE: interface,
1292 RH_ENDPOINT: endpoint,
1293 RH_CLASS means HUB here,
1294 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1298 *(u16
*)databuf
= cpu_to_le16(1);
1300 case RH_GET_STATUS
| RH_INTERFACE
:
1301 *(u16
*)databuf
= cpu_to_le16(0);
1303 case RH_GET_STATUS
| RH_ENDPOINT
:
1304 *(u16
*)databuf
= cpu_to_le16(0);
1306 case RH_GET_STATUS
| RH_CLASS
:
1307 *(u32
*)databuf
= cpu_to_le32(
1308 RD_RH_STAT
& ~(RH_HS_CRWE
| RH_HS_DRWE
));
1310 case RH_GET_STATUS
| RH_OTHER
| RH_CLASS
:
1311 *(u32
*)databuf
= cpu_to_le32(RD_RH_PORTSTAT
);
1314 case RH_CLEAR_FEATURE
| RH_ENDPOINT
:
1316 case (RH_ENDPOINT_STALL
):
1321 case RH_CLEAR_FEATURE
| RH_CLASS
:
1323 case RH_C_HUB_LOCAL_POWER
:
1325 case (RH_C_HUB_OVER_CURRENT
):
1326 WR_RH_STAT(RH_HS_OCIC
);
1331 case RH_CLEAR_FEATURE
| RH_OTHER
| RH_CLASS
:
1333 case (RH_PORT_ENABLE
): WR_RH_PORTSTAT(RH_PS_CCS
); OK(0);
1334 case (RH_PORT_SUSPEND
): WR_RH_PORTSTAT(RH_PS_POCI
); OK(0);
1335 case (RH_PORT_POWER
): WR_RH_PORTSTAT(RH_PS_LSDA
); OK(0);
1336 case (RH_C_PORT_CONNECTION
): WR_RH_PORTSTAT(RH_PS_CSC
); OK(0);
1337 case (RH_C_PORT_ENABLE
): WR_RH_PORTSTAT(RH_PS_PESC
); OK(0);
1338 case (RH_C_PORT_SUSPEND
): WR_RH_PORTSTAT(RH_PS_PSSC
); OK(0);
1339 case (RH_C_PORT_OVER_CURRENT
):WR_RH_PORTSTAT(RH_PS_OCIC
); OK(0);
1340 case (RH_C_PORT_RESET
): WR_RH_PORTSTAT(RH_PS_PRSC
); OK(0);
1344 case RH_SET_FEATURE
| RH_OTHER
| RH_CLASS
:
1346 case (RH_PORT_SUSPEND
):
1347 WR_RH_PORTSTAT(RH_PS_PSS
); OK(0);
1348 case (RH_PORT_RESET
): /* BUG IN HUP CODE *********/
1349 if (RD_RH_PORTSTAT
& RH_PS_CCS
)
1350 WR_RH_PORTSTAT(RH_PS_PRS
);
1352 case (RH_PORT_POWER
):
1353 WR_RH_PORTSTAT(RH_PS_PPS
);
1356 case (RH_PORT_ENABLE
): /* BUG IN HUP CODE *********/
1357 if (RD_RH_PORTSTAT
& RH_PS_CCS
)
1358 WR_RH_PORTSTAT(RH_PS_PES
);
1363 case RH_SET_ADDRESS
:
1364 gohci
.rh
.devnum
= wValue
;
1367 case RH_GET_DESCRIPTOR
:
1368 switch ((wValue
& 0xff00) >> 8) {
1369 case (0x01): /* device descriptor */
1370 len
= min_t(unsigned int,
1373 sizeof(root_hub_dev_des
),
1375 databuf
= root_hub_dev_des
; OK(len
);
1376 case (0x02): /* configuration descriptor */
1377 len
= min_t(unsigned int,
1380 sizeof(root_hub_config_des
),
1382 databuf
= root_hub_config_des
; OK(len
);
1383 case (0x03): /* string descriptors */
1384 if (wValue
== 0x0300) {
1385 len
= min_t(unsigned int,
1388 sizeof(root_hub_str_index0
),
1390 databuf
= root_hub_str_index0
;
1393 if (wValue
== 0x0301) {
1394 len
= min_t(unsigned int,
1397 sizeof(root_hub_str_index1
),
1399 databuf
= root_hub_str_index1
;
1403 stat
= USB_ST_STALLED
;
1407 case RH_GET_DESCRIPTOR
| RH_CLASS
:
1409 __u32 temp
= roothub_a(&gohci
);
1411 databuf
[0] = 9; /* min length; */
1413 databuf
[2] = temp
& RH_A_NDP
;
1414 #ifdef CONFIG_AT91C_PQFP_UHPBUG
1415 databuf
[2] = (databuf
[2] == 2) ? 1 : 0;
1418 if (temp
& RH_A_PSM
) /* per-port power switching? */
1420 if (temp
& RH_A_NOCP
) /* no overcurrent reporting? */
1422 else if (temp
& RH_A_OCPM
)/* per-port overcurrent reporting? */
1426 databuf
[5] = (temp
& RH_A_POTPGT
) >> 24;
1428 temp
= roothub_b(&gohci
);
1429 databuf
[7] = temp
& RH_B_DR
;
1430 if (databuf
[2] < 7) {
1434 databuf
[8] = (temp
& RH_B_DR
) >> 8;
1435 databuf
[10] = databuf
[9] = 0xff;
1438 len
= min_t(unsigned int, leni
,
1439 min_t(unsigned int, databuf
[0], wLength
));
1443 case RH_GET_CONFIGURATION
:
1447 case RH_SET_CONFIGURATION
:
1448 WR_RH_STAT(0x10000);
1452 dbg("unsupported root hub command");
1453 stat
= USB_ST_STALLED
;
1457 ohci_dump_roothub(&gohci
, 1);
1462 len
= min_t(int, len
, leni
);
1463 if (data
!= databuf
)
1464 memcpy(data
, databuf
, len
);
1469 pkt_print(NULL
, dev
, pipe
, buffer
,
1470 transfer_len
, cmd
, "RET(rh)", 0/*usb_pipein(pipe)*/);
1478 /*-------------------------------------------------------------------------*/
1480 /* common code for handling submit messages - used for all but root hub */
1482 int submit_common_msg(struct usb_device
*dev
, unsigned long pipe
, void *buffer
,
1483 int transfer_len
, struct devrequest
*setup
, int interval
)
1486 int maxsize
= usb_maxpacket(dev
, pipe
);
1490 urb
= malloc(sizeof(urb_priv_t
));
1491 memset(urb
, 0, sizeof(urb_priv_t
));
1495 urb
->transfer_buffer
= buffer
;
1496 urb
->transfer_buffer_length
= transfer_len
;
1497 urb
->interval
= interval
;
1499 /* device pulled? Shortcut the action. */
1500 if (devgone
== dev
) {
1501 dev
->status
= USB_ST_CRC_ERR
;
1506 urb
->actual_length
= 0;
1507 pkt_print(urb
, dev
, pipe
, buffer
, transfer_len
,
1508 setup
, "SUB", usb_pipein(pipe
));
1513 err("submit_common_message: pipesize for pipe %lx is zero",
1518 if (sohci_submit_job(urb
, setup
) < 0) {
1519 err("sohci_submit_job failed");
1525 /* ohci_dump_status(&gohci); */
1528 timeout
= USB_TIMEOUT_MS(pipe
);
1530 /* wait for it to complete */
1532 /* check whether the controller is done */
1533 stat
= hc_interrupt();
1535 stat
= USB_ST_CRC_ERR
;
1539 /* NOTE: since we are not interrupt driven in U-Boot and always
1540 * handle only one URB at a time, we cannot assume the
1541 * transaction finished on the first successful return from
1542 * hc_interrupt().. unless the flag for current URB is set,
1543 * meaning that all TD's to/from device got actually
1544 * transferred and processed. If the current URB is not
1545 * finished we need to re-iterate this loop so as
1546 * hc_interrupt() gets called again as there needs to be some
1547 * more TD's to process still */
1548 if ((stat
>= 0) && (stat
!= 0xff) && (urb
->finished
)) {
1549 /* 0xff is returned for an SF-interrupt */
1559 err("CTL:TIMEOUT ");
1560 dbg("submit_common_msg: TO status %x\n", stat
);
1562 stat
= USB_ST_CRC_ERR
;
1568 dev
->act_len
= transfer_len
;
1571 pkt_print(urb
, dev
, pipe
, buffer
, transfer_len
,
1572 setup
, "RET(ctlr)", usb_pipein(pipe
));
1577 /* free TDs in urb_priv */
1578 if (!usb_pipeint(pipe
))
1583 /* submit routines called from usb.c */
1584 int submit_bulk_msg(struct usb_device
*dev
, unsigned long pipe
, void *buffer
,
1587 info("submit_bulk_msg");
1588 return submit_common_msg(dev
, pipe
, buffer
, transfer_len
, NULL
, 0);
1591 int submit_control_msg(struct usb_device
*dev
, unsigned long pipe
, void *buffer
,
1592 int transfer_len
, struct devrequest
*setup
)
1594 int maxsize
= usb_maxpacket(dev
, pipe
);
1596 info("submit_control_msg");
1598 pkt_print(NULL
, dev
, pipe
, buffer
, transfer_len
,
1599 setup
, "SUB", usb_pipein(pipe
));
1604 err("submit_control_message: pipesize for pipe %lx is zero",
1608 if (((pipe
>> 8) & 0x7f) == gohci
.rh
.devnum
) {
1610 /* root hub - redirect */
1611 return ohci_submit_rh_msg(dev
, pipe
, buffer
, transfer_len
,
1615 return submit_common_msg(dev
, pipe
, buffer
, transfer_len
, setup
, 0);
1618 int submit_int_msg(struct usb_device
*dev
, unsigned long pipe
, void *buffer
,
1619 int transfer_len
, int interval
)
1621 info("submit_int_msg");
1622 return submit_common_msg(dev
, pipe
, buffer
, transfer_len
, NULL
,
1626 /*-------------------------------------------------------------------------*
1628 *-------------------------------------------------------------------------*/
1630 /* reset the HC and BUS */
1632 static int hc_reset(ohci_t
*ohci
)
1634 #ifdef CONFIG_PCI_EHCI_DEVNO
1638 int smm_timeout
= 50; /* 0,5 sec */
1640 dbg("%s\n", __FUNCTION__
);
1642 #ifdef CONFIG_PCI_EHCI_DEVNO
1644 * Some multi-function controllers (e.g. ISP1562) allow root hub
1645 * resetting via EHCI registers only.
1647 pdev
= pci_find_devices(ehci_pci_ids
, CONFIG_PCI_EHCI_DEVNO
);
1652 pci_read_config_dword(pdev
, PCI_BASE_ADDRESS_0
, &base
);
1653 base
+= EHCI_USBCMD_OFF
;
1654 ohci_writel(ohci_readl(base
) | EHCI_USBCMD_HCRESET
, base
);
1656 while (ohci_readl(base
) & EHCI_USBCMD_HCRESET
) {
1657 if (timeout
-- <= 0) {
1658 printf("USB RootHub reset timed out!");
1664 printf("No EHCI func at %d index!\n", CONFIG_PCI_EHCI_DEVNO
);
1666 if (ohci_readl(&ohci
->regs
->control
) & OHCI_CTRL_IR
) {
1667 /* SMM owns the HC, request ownership */
1668 ohci_writel(OHCI_OCR
, &ohci
->regs
->cmdstatus
);
1669 info("USB HC TakeOver from SMM");
1670 while (ohci_readl(&ohci
->regs
->control
) & OHCI_CTRL_IR
) {
1672 if (--smm_timeout
== 0) {
1673 err("USB HC TakeOver failed!");
1679 /* Disable HC interrupts */
1680 ohci_writel(OHCI_INTR_MIE
, &ohci
->regs
->intrdisable
);
1682 dbg("USB HC reset_hc usb-%s: ctrl = 0x%X ;\n",
1684 ohci_readl(&ohci
->regs
->control
));
1686 /* Reset USB (needed by some controllers) */
1687 ohci
->hc_control
= 0;
1688 ohci_writel(ohci
->hc_control
, &ohci
->regs
->control
);
1690 /* HC Reset requires max 10 us delay */
1691 ohci_writel(OHCI_HCR
, &ohci
->regs
->cmdstatus
);
1692 while ((ohci_readl(&ohci
->regs
->cmdstatus
) & OHCI_HCR
) != 0) {
1693 if (--timeout
== 0) {
1694 err("USB HC reset timed out!");
1702 /*-------------------------------------------------------------------------*/
1704 /* Start an OHCI controller, set the BUS operational
1706 * connect the virtual root hub */
1708 static int hc_start(ohci_t
*ohci
)
1711 unsigned int fminterval
;
1715 /* Tell the controller where the control and bulk lists are
1716 * The lists are empty now. */
1718 ohci_writel(0, &ohci
->regs
->ed_controlhead
);
1719 ohci_writel(0, &ohci
->regs
->ed_bulkhead
);
1721 ohci_writel((__u32
)ohci
->hcca
,
1722 &ohci
->regs
->hcca
); /* reset clears this */
1724 fminterval
= 0x2edf;
1725 ohci_writel((fminterval
* 9) / 10, &ohci
->regs
->periodicstart
);
1726 fminterval
|= ((((fminterval
- 210) * 6) / 7) << 16);
1727 ohci_writel(fminterval
, &ohci
->regs
->fminterval
);
1728 ohci_writel(0x628, &ohci
->regs
->lsthresh
);
1730 /* start controller operations */
1731 ohci
->hc_control
= OHCI_CONTROL_INIT
| OHCI_USB_OPER
;
1733 ohci_writel(ohci
->hc_control
, &ohci
->regs
->control
);
1735 /* disable all interrupts */
1736 mask
= (OHCI_INTR_SO
| OHCI_INTR_WDH
| OHCI_INTR_SF
| OHCI_INTR_RD
|
1737 OHCI_INTR_UE
| OHCI_INTR_FNO
| OHCI_INTR_RHSC
|
1738 OHCI_INTR_OC
| OHCI_INTR_MIE
);
1739 ohci_writel(mask
, &ohci
->regs
->intrdisable
);
1740 /* clear all interrupts */
1741 mask
&= ~OHCI_INTR_MIE
;
1742 ohci_writel(mask
, &ohci
->regs
->intrstatus
);
1743 /* Choose the interrupts we care about now - but w/o MIE */
1744 mask
= OHCI_INTR_RHSC
| OHCI_INTR_UE
| OHCI_INTR_WDH
| OHCI_INTR_SO
;
1745 ohci_writel(mask
, &ohci
->regs
->intrenable
);
1748 /* required for AMD-756 and some Mac platforms */
1749 ohci_writel((roothub_a(ohci
) | RH_A_NPS
) & ~RH_A_PSM
,
1750 &ohci
->regs
->roothub
.a
);
1751 ohci_writel(RH_HS_LPSC
, &ohci
->regs
->roothub
.status
);
1752 #endif /* OHCI_USE_NPS */
1754 /* POTPGT delay is bits 24-31, in 2 ms units. */
1755 mdelay((roothub_a(ohci
) >> 23) & 0x1fe);
1757 /* connect the virtual root hub */
1758 ohci
->rh
.devnum
= 0;
1763 /*-------------------------------------------------------------------------*/
1765 /* an interrupt happens */
1767 static int hc_interrupt(void)
1769 ohci_t
*ohci
= &gohci
;
1770 struct ohci_regs
*regs
= ohci
->regs
;
1774 if ((ohci
->hcca
->done_head
!= 0) &&
1775 !(m32_swap(ohci
->hcca
->done_head
) & 0x01)) {
1776 ints
= OHCI_INTR_WDH
;
1778 ints
= ohci_readl(®s
->intrstatus
);
1779 if (ints
== ~(u32
)0) {
1781 err("%s device removed!", ohci
->slot_name
);
1784 ints
&= ohci_readl(®s
->intrenable
);
1786 dbg("hc_interrupt: returning..\n");
1792 /* dbg("Interrupt: %x frame: %x", ints,
1793 le16_to_cpu(ohci->hcca->frame_no)); */
1795 if (ints
& OHCI_INTR_RHSC
)
1798 if (ints
& OHCI_INTR_UE
) {
1800 err("OHCI Unrecoverable Error, controller usb-%s disabled",
1802 /* e.g. due to PCI Master/Target Abort */
1809 /* FIXME: be optimistic, hope that bug won't repeat often. */
1810 /* Make some non-interrupt context restart the controller. */
1811 /* Count and limit the retries though; either hardware or */
1812 /* software errors can go forever... */
1817 if (ints
& OHCI_INTR_WDH
) {
1819 ohci_writel(OHCI_INTR_WDH
, ®s
->intrdisable
);
1820 (void)ohci_readl(®s
->intrdisable
); /* flush */
1821 stat
= dl_done_list(&gohci
);
1822 ohci_writel(OHCI_INTR_WDH
, ®s
->intrenable
);
1823 (void)ohci_readl(®s
->intrdisable
); /* flush */
1826 if (ints
& OHCI_INTR_SO
) {
1827 dbg("USB Schedule overrun\n");
1828 ohci_writel(OHCI_INTR_SO
, ®s
->intrenable
);
1832 /* FIXME: this assumes SOF (1/ms) interrupts don't get lost... */
1833 if (ints
& OHCI_INTR_SF
) {
1834 unsigned int frame
= m16_swap(ohci
->hcca
->frame_no
) & 1;
1836 ohci_writel(OHCI_INTR_SF
, ®s
->intrdisable
);
1837 if (ohci
->ed_rm_list
[frame
] != NULL
)
1838 ohci_writel(OHCI_INTR_SF
, ®s
->intrenable
);
1842 ohci_writel(ints
, ®s
->intrstatus
);
1846 /*-------------------------------------------------------------------------*/
1848 /*-------------------------------------------------------------------------*/
1850 /* De-allocate all resources.. */
1852 static void hc_release_ohci(ohci_t
*ohci
)
1854 dbg("USB HC release ohci usb-%s", ohci
->slot_name
);
1856 if (!ohci
->disabled
)
1860 /*-------------------------------------------------------------------------*/
1863 * low level initalisation routine, called from usb.c
1865 static char ohci_inited
= 0;
1867 int usb_lowlevel_init(int index
, void **controller
)
1869 #ifdef CONFIG_PCI_OHCI
1873 #ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
1874 /* cpu dependant init */
1879 #ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
1880 /* board dependant init */
1881 if (usb_board_init())
1884 memset(&gohci
, 0, sizeof(ohci_t
));
1886 /* align the storage */
1887 if ((__u32
)&ghcca
[0] & 0xff) {
1888 err("HCCA not aligned!!");
1892 info("aligned ghcca %p", phcca
);
1893 memset(&ohci_dev
, 0, sizeof(struct ohci_device
));
1894 if ((__u32
)&ohci_dev
.ed
[0] & 0x7) {
1895 err("EDs not aligned!!");
1898 memset(gtd
, 0, sizeof(td_t
) * (NUM_TD
+ 1));
1899 if ((__u32
)gtd
& 0x7) {
1900 err("TDs not aligned!!");
1905 memset(phcca
, 0, sizeof(struct ohci_hcca
));
1910 #ifdef CONFIG_PCI_OHCI
1911 pdev
= pci_find_devices(ohci_pci_ids
, CONFIG_PCI_OHCI_DEVNO
);
1916 pci_read_config_word(pdev
, PCI_VENDOR_ID
, &vid
);
1917 pci_read_config_word(pdev
, PCI_DEVICE_ID
, &did
);
1918 printf("OHCI pci controller (%04x, %04x) found @(%d:%d:%d)\n",
1919 vid
, did
, (pdev
>> 16) & 0xff,
1920 (pdev
>> 11) & 0x1f, (pdev
>> 8) & 0x7);
1921 pci_read_config_dword(pdev
, PCI_BASE_ADDRESS_0
, &base
);
1922 printf("OHCI regs address 0x%08x\n", base
);
1923 gohci
.regs
= (struct ohci_regs
*)base
;
1927 gohci
.regs
= (struct ohci_regs
*)CONFIG_SYS_USB_OHCI_REGS_BASE
;
1931 gohci
.slot_name
= CONFIG_SYS_USB_OHCI_SLOT_NAME
;
1933 if (hc_reset (&gohci
) < 0) {
1934 hc_release_ohci (&gohci
);
1935 err ("can't reset usb-%s", gohci
.slot_name
);
1936 #ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
1937 /* board dependant cleanup */
1938 usb_board_init_fail();
1941 #ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
1942 /* cpu dependant cleanup */
1943 usb_cpu_init_fail();
1948 if (hc_start(&gohci
) < 0) {
1949 err("can't start usb-%s", gohci
.slot_name
);
1950 hc_release_ohci(&gohci
);
1951 /* Initialization failed */
1952 #ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
1953 /* board dependant cleanup */
1957 #ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
1958 /* cpu dependant cleanup */
1965 ohci_dump(&gohci
, 1);
1973 int usb_lowlevel_stop(int index
)
1975 /* this gets called really early - before the controller has */
1976 /* even been initialized! */
1979 /* TODO release any interrupts, etc. */
1980 /* call hc_release_ohci() here ? */
1983 #ifdef CONFIG_SYS_USB_OHCI_BOARD_INIT
1984 /* board dependant cleanup */
1985 if (usb_board_stop())
1989 #ifdef CONFIG_SYS_USB_OHCI_CPU_INIT
1990 /* cpu dependant cleanup */
1994 /* This driver is no longer initialised. It needs a new low-level
1995 * init (board/cpu) before it can be used again. */