2 * Part of this code has been derived from linux:
3 * Universal Host Controller Interface driver for USB (take II).
5 * (c) 1999-2001 Georg Acher, acher@in.tum.de (executive slave) (base guitar)
6 * Deti Fliegl, deti@fliegl.de (executive slave) (lead voice)
7 * Thomas Sailer, sailer@ife.ee.ethz.ch (chief consultant) (cheer leader)
8 * Roman Weissgaerber, weissg@vienna.at (virt root hub) (studio porter)
9 * (c) 2000 Yggdrasil Computing, Inc. (port of new PCI interface support
10 * from usb-ohci.c by Adam Richter, adam@yggdrasil.com).
11 * (C) 2000 David Brownell, david-b@pacbell.net (usb-ohci.c)
13 * HW-initalization based on material of
15 * (C) Copyright 1999 Linus Torvalds
16 * (C) Copyright 1999 Johannes Erdfelt
17 * (C) Copyright 1999 Randy Dunlap
18 * (C) Copyright 1999 Gregory P. Smith
22 * (C) Copyright 2001 Denis Peter, MPL AG Switzerland
24 * See file CREDITS for list of people who contributed to this
27 * This program is free software; you can redistribute it and/or
28 * modify it under the terms of the GNU General Public License as
29 * published by the Free Software Foundation; either version 2 of
30 * the License, or (at your option) any later version.
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with this program; if not, write to the Free Software
39 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
45 /**********************************************************************
48 * The framelist / Transfer descriptor / Queue Heads are similar like
49 * in the linux usb_uhci.c.
51 * During initialization, the following skeleton is allocated in init_skel:
53 * framespecific | common chain
56 * [ 0 ]-----> TD ---------\
57 * [ 1 ]-----> TD ----------> TD ------> QH -------> QH -------> QH ---> NULL
59 * [1023]-----> TD --------/
62 * 7 TDs for 1 TD for Start of Start of End Chain
63 * INT (2-128ms) 1ms-INT CTRL Chain BULK Chain
66 * Since this is a bootloader, the isochronous transfer descriptor have been removed.
68 * Interrupt Transfers.
69 * --------------------
70 * For Interupt transfers USB_MAX_TEMP_INT_TD Transfer descriptor are available. They
71 * will be inserted after the appropriate (depending the interval setting) skeleton TD.
72 * If an interrupt has been detected the dev->irqhandler is called. The status and number
73 * of transfered bytes is stored in dev->irq_status resp. dev->irq_act_len. If the
74 * dev->irqhandler returns 0, the interrupt TD is removed and disabled. If an 1 is returned,
75 * the interrupt TD will be reactivated.
79 * Control Transfers are issued by filling the tmp_td with the appropriate data and connect
80 * them to the qh_cntrl queue header. Before other control/bulk transfers can be issued,
81 * the programm has to wait for completion. This does not allows asynchronous data transfer.
85 * Bulk Transfers are issued by filling the tmp_td with the appropriate data and connect
86 * them to the qh_bulk queue header. Before other control/bulk transfers can be issued,
87 * the programm has to wait for completion. This does not allows asynchronous data transfer.
95 #ifdef CONFIG_USB_UHCI
100 #define USB_MAX_TEMP_TD 128 /* number of temporary TDs for bulk and control transfers */
101 #define USB_MAX_TEMP_INT_TD 32 /* number of temporary TDs for Interrupt transfers */
104 #undef USB_UHCI_DEBUG
106 #ifdef USB_UHCI_DEBUG
107 #define USB_UHCI_PRINTF(fmt,args...) printf (fmt ,##args)
109 #define USB_UHCI_PRINTF(fmt,args...)
113 static int irqvec
= -1; /* irq vector, if -1 uhci is stopped / reseted */
114 unsigned int usb_base_addr
; /* base address */
116 static uhci_td_t td_int
[8]; /* Interrupt Transfer descriptors */
117 static uhci_qh_t qh_cntrl
; /* control Queue Head */
118 static uhci_qh_t qh_bulk
; /* bulk Queue Head */
119 static uhci_qh_t qh_end
; /* end Queue Head */
120 static uhci_td_t td_last
; /* last TD (linked with end chain) */
123 static uhci_td_t tmp_td
[USB_MAX_TEMP_TD
]; /* temporary bulk/control td's */
124 static uhci_td_t tmp_int_td
[USB_MAX_TEMP_INT_TD
]; /* temporary interrupt td's */
126 static unsigned long framelist
[1024] __attribute__ ((aligned (0x1000))); /* frame list */
128 static struct virt_root_hub rh
; /* struct for root hub */
130 /**********************************************************************
131 * some forward decleration
133 int uhci_submit_rh_msg(struct usb_device
*dev
, unsigned long pipe
,
134 void *buffer
, int transfer_len
,struct devrequest
*setup
);
136 /* fill a td with the approproiate data. Link, status, info and buffer
137 * are used by the USB controller itselfes, dev is used to identify the
140 void usb_fill_td(uhci_td_t
* td
,unsigned long link
,unsigned long status
,
141 unsigned long info
, unsigned long buffer
, unsigned long dev
)
143 td
->link
=swap_32(link
);
144 td
->status
=swap_32(status
);
145 td
->info
=swap_32(info
);
146 td
->buffer
=swap_32(buffer
);
150 /* fill a qh with the approproiate data. Head and element are used by the USB controller
151 * itselfes. As soon as a valid dev_ptr is filled, a td chain is connected to the qh.
152 * Please note, that after completion of the td chain, the entry element is removed /
153 * marked invalid by the USB controller.
155 void usb_fill_qh(uhci_qh_t
* qh
,unsigned long head
,unsigned long element
)
157 qh
->head
=swap_32(head
);
158 qh
->element
=swap_32(element
);
162 /* get the status of a td->status
164 unsigned long usb_uhci_td_stat(unsigned long status
)
166 unsigned long result
=0;
167 result
|= (status
& TD_CTRL_NAK
) ? USB_ST_NAK_REC
: 0;
168 result
|= (status
& TD_CTRL_STALLED
) ? USB_ST_STALLED
: 0;
169 result
|= (status
& TD_CTRL_DBUFERR
) ? USB_ST_BUF_ERR
: 0;
170 result
|= (status
& TD_CTRL_BABBLE
) ? USB_ST_BABBLE_DET
: 0;
171 result
|= (status
& TD_CTRL_CRCTIMEO
) ? USB_ST_CRC_ERR
: 0;
172 result
|= (status
& TD_CTRL_BITSTUFF
) ? USB_ST_BIT_ERR
: 0;
173 result
|= (status
& TD_CTRL_ACTIVE
) ? USB_ST_NOT_PROC
: 0;
177 /* get the status and the transfered len of a td chain.
178 * called from the completion handler
180 int usb_get_td_status(uhci_td_t
*td
,struct usb_device
*dev
)
182 unsigned long temp
,info
;
186 if(dev
->devnum
==rh
.devnum
)
191 temp
=swap_32((unsigned long)mytd
->status
);
192 stat
=usb_uhci_td_stat(temp
);
193 info
=swap_32((unsigned long)mytd
->info
);
194 if(((info
& 0xff)!= USB_PID_SETUP
) &&
195 (((info
>> 21) & 0x7ff)!= 0x7ff) &&
196 (temp
& 0x7FF)!=0x7ff)
197 { /* if not setup and not null data pack */
198 dev
->act_len
+=(temp
& 0x7FF) + 1; /* the transfered len is act_len + 1 */
200 if(stat
) { /* status no ok */
204 temp
=swap_32((unsigned long)mytd
->link
);
205 mytd
=(uhci_td_t
*)(temp
& 0xfffffff0);
206 }while((temp
& 0x1)==0); /* process all TDs */
212 /*-------------------------------------------------------------------
214 * assembles QHs und TDs for control, bulk and iso
215 *-------------------------------------------------------------------*/
217 /* Submits a control message. That is a Setup, Data and Status transfer.
218 * Routine does not wait for completion.
220 int submit_control_msg(struct usb_device
*dev
, unsigned long pipe
, void *buffer
,
221 int transfer_len
,struct devrequest
*setup
)
223 unsigned long destination
, status
;
224 int maxsze
= usb_maxpacket(dev
, pipe
);
225 unsigned long dataptr
;
231 USB_UHCI_PRINTF("uhci_submit_control_urb: pipesize for pipe %lx is zero\n", pipe
);
234 if(((pipe
>>8)&0x7f)==rh
.devnum
) {
235 /* this is the root hub -> redirect it */
236 return uhci_submit_rh_msg(dev
,pipe
,buffer
,transfer_len
,setup
);
238 USB_UHCI_PRINTF("uhci_submit_control start len %x, maxsize %x\n",transfer_len
,maxsze
);
239 /* The "pipe" thing contains the destination in bits 8--18 */
240 destination
= (pipe
& PIPE_DEVEP_MASK
) | USB_PID_SETUP
; /* Setup stage */
242 status
= (pipe
& TD_CTRL_LS
) | TD_CTRL_ACTIVE
| (3 << 27);
243 /* (urb->transfer_flags & USB_DISABLE_SPD ? 0 : TD_CTRL_SPD); */
244 /* Build the TD for the control request, try forever, 8 bytes of data */
245 usb_fill_td(&tmp_td
[i
],UHCI_PTR_TERM
,status
, destination
| (7 << 21),(unsigned long)setup
,(unsigned long)dev
);
248 char *sp
=(char *)setup
;
249 printf("SETUP to pipe %lx: %x %x %x %x %x %x %x %x\n", pipe
,
250 sp
[0],sp
[1],sp
[2],sp
[3],sp
[4],sp
[5],sp
[6],sp
[7]);
253 dataptr
= (unsigned long)buffer
;
256 /* If direction is "send", change the frame from SETUP (0x2D)
257 to OUT (0xE1). Else change it from SETUP to IN (0x69). */
258 destination
= (pipe
& PIPE_DEVEP_MASK
) | ((pipe
& USB_DIR_IN
)==0 ? USB_PID_OUT
: USB_PID_IN
);
265 destination
^= 1 << TD_TOKEN_TOGGLE
; /* toggle DATA0/1 */
266 usb_fill_td(&tmp_td
[i
],UHCI_PTR_TERM
, status
, destination
| ((pktsze
- 1) << 21),dataptr
,(unsigned long)dev
); /* Status, pktsze bytes of data */
267 tmp_td
[i
-1].link
=swap_32((unsigned long)&tmp_td
[i
]);
273 /* Build the final TD for control status */
274 /* It's only IN if the pipe is out AND we aren't expecting data */
276 destination
&= ~UHCI_PID
;
277 if (((pipe
& USB_DIR_IN
)==0) || (transfer_len
== 0))
278 destination
|= USB_PID_IN
;
280 destination
|= USB_PID_OUT
;
281 destination
|= 1 << TD_TOKEN_TOGGLE
; /* End in Data1 */
283 status
&=~TD_CTRL_SPD
;
284 /* no limit on errors on final packet , 0 bytes of data */
285 usb_fill_td(&tmp_td
[i
],UHCI_PTR_TERM
, status
| TD_CTRL_IOC
, destination
| (UHCI_NULL_DATA_SIZE
<< 21),0,(unsigned long)dev
);
286 tmp_td
[i
-1].link
=swap_32((unsigned long)&tmp_td
[i
]); /* queue status td */
287 /* usb_show_td(i+1);*/
288 USB_UHCI_PRINTF("uhci_submit_control end (%d tmp_tds used)\n",i
);
289 /* first mark the control QH element terminated */
290 qh_cntrl
.element
=0xffffffffL
;
292 qh_cntrl
.dev_ptr
=(unsigned long)dev
;
293 /* fill in tmp_td_chain */
294 qh_cntrl
.element
=swap_32((unsigned long)&tmp_td
[0]);
298 /*-------------------------------------------------------------------
299 * Prepare TDs for bulk transfers.
301 int submit_bulk_msg(struct usb_device
*dev
, unsigned long pipe
, void *buffer
,int transfer_len
)
303 unsigned long destination
, status
,info
;
304 unsigned long dataptr
;
305 int maxsze
= usb_maxpacket(dev
, pipe
);
309 if(transfer_len
< 0) {
310 printf("Negative transfer length in submit_bulk\n");
315 /* The "pipe" thing contains the destination in bits 8--18. */
316 destination
= (pipe
& PIPE_DEVEP_MASK
) | usb_packetid (pipe
);
318 status
= (pipe
& TD_CTRL_LS
) | TD_CTRL_ACTIVE
| (3 << 27);
319 /* ((urb->transfer_flags & USB_DISABLE_SPD) ? 0 : TD_CTRL_SPD) | (3 << 27); */
320 /* Build the TDs for the bulk request */
322 dataptr
= (unsigned long)buffer
;
327 /* pktsze bytes of data */
328 info
= destination
| (((pktsze
- 1)&UHCI_NULL_DATA_SIZE
) << 21) |
329 (usb_gettoggle (dev
, usb_pipeendpoint (pipe
), usb_pipeout (pipe
)) << TD_TOKEN_TOGGLE
);
332 status
|= TD_CTRL_IOC
; /* last one generates INT */
334 usb_fill_td(&tmp_td
[i
],UHCI_PTR_TERM
, status
, info
,dataptr
,(unsigned long)dev
); /* Status, pktsze bytes of data */
336 tmp_td
[i
-1].link
=swap_32((unsigned long)&tmp_td
[i
]);
340 usb_dotoggle (dev
, usb_pipeendpoint (pipe
), usb_pipeout (pipe
));
342 /* first mark the bulk QH element terminated */
343 qh_bulk
.element
=0xffffffffL
;
345 qh_bulk
.dev_ptr
=(unsigned long)dev
;
346 /* fill in tmp_td_chain */
347 qh_bulk
.element
=swap_32((unsigned long)&tmp_td
[0]);
352 /* search a free interrupt td
354 uhci_td_t
*uhci_alloc_int_td(void)
357 for(i
=0;i
<USB_MAX_TEMP_INT_TD
;i
++) {
358 if(tmp_int_td
[i
].dev_ptr
==0) /* no device assigned -> free TD */
359 return &tmp_int_td
[i
];
365 void uhci_show_temp_int_td(void)
368 for(i
=0;i
<USB_MAX_TEMP_INT_TD
;i
++) {
369 if((tmp_int_td
[i
].dev_ptr
&0x01)!=0x1L
) /* no device assigned -> free TD */
370 printf("temp_td %d is assigned to dev %lx\n",i
,tmp_int_td
[i
].dev_ptr
);
372 printf("all others temp_tds are free\n");
375 /*-------------------------------------------------------------------
376 * submits USB interrupt (ie. polling ;-)
378 int submit_int_msg(struct usb_device
*dev
, unsigned long pipe
, void *buffer
,int transfer_len
, int interval
)
381 unsigned long status
, destination
;
382 unsigned long info
,tmp
;
384 if (interval
< 0 || interval
>= 256)
390 for (nint
= 0, n
= 1; nint
<= 8; nint
++, n
+= n
) /* round interval down to 2^n */
400 USB_UHCI_PRINTF("Rounded interval to %i, chain %i\n", interval
, nint
);
401 mytd
=uhci_alloc_int_td();
403 printf("No free INT TDs found\n");
406 status
= (pipe
& TD_CTRL_LS
) | TD_CTRL_ACTIVE
| TD_CTRL_IOC
| (3 << 27);
407 /* (urb->transfer_flags & USB_DISABLE_SPD ? 0 : TD_CTRL_SPD) | (3 << 27);
410 destination
=(pipe
& PIPE_DEVEP_MASK
) | usb_packetid (pipe
) | (((transfer_len
- 1) & 0x7ff) << 21);
412 info
= destination
| (usb_gettoggle(dev
, usb_pipeendpoint(pipe
), usb_pipeout(pipe
)) << TD_TOKEN_TOGGLE
);
413 tmp
= swap_32(td_int
[nint
].link
);
414 usb_fill_td(mytd
,tmp
,status
, info
,(unsigned long)buffer
,(unsigned long)dev
);
416 tmp
= swap_32((unsigned long)mytd
);
417 td_int
[nint
].link
=tmp
;
419 usb_dotoggle (dev
, usb_pipeendpoint (pipe
), usb_pipeout (pipe
));
424 /**********************************************************************
425 * Low Level functions
432 /* Global reset for 100ms */
433 out16r( usb_base_addr
+ USBPORTSC1
,0x0204);
434 out16r( usb_base_addr
+ USBPORTSC2
,0x0204);
435 out16r( usb_base_addr
+ USBCMD
,USBCMD_GRESET
| USBCMD_RS
);
436 /* Turn off all interrupts */
437 out16r(usb_base_addr
+ USBINTR
,0);
439 out16r( usb_base_addr
+ USBCMD
,0);
447 while(in16r(usb_base_addr
+ USBCMD
) & USBCMD_HCRESET
) {
449 printf("USBCMD_HCRESET timed out!\n");
453 /* Turn on all interrupts */
454 out16r(usb_base_addr
+ USBINTR
,USBINTR_TIMEOUT
| USBINTR_RESUME
| USBINTR_IOC
| USBINTR_SP
);
455 /* Start at frame 0 */
456 out16r(usb_base_addr
+ USBFRNUM
,0);
457 /* set Framebuffer base address */
458 out32r(usb_base_addr
+USBFLBASEADD
,(unsigned long)&framelist
);
459 /* Run and mark it configured with a 64-byte max packet */
460 out16r(usb_base_addr
+ USBCMD
,USBCMD_RS
| USBCMD_CF
| USBCMD_MAXP
);
463 /* Initialize the skeleton
465 void usb_init_skel(void)
470 for(n
=0;n
<USB_MAX_TEMP_INT_TD
;n
++)
471 tmp_int_td
[n
].dev_ptr
=0L; /* no devices connected */
473 usb_fill_td(&td_last
,UHCI_PTR_TERM
,TD_CTRL_IOC
,0,0,0L);
474 /* usb_fill_td(&td_last,UHCI_PTR_TERM,0,0,0); */
475 /* End Queue Header */
476 usb_fill_qh(&qh_end
,UHCI_PTR_TERM
,(unsigned long)&td_last
);
477 /* Bulk Queue Header */
478 temp
=(unsigned long)&qh_end
;
479 usb_fill_qh(&qh_bulk
,temp
| UHCI_PTR_QH
,UHCI_PTR_TERM
);
480 /* Control Queue Header */
481 temp
=(unsigned long)&qh_bulk
;
482 usb_fill_qh(&qh_cntrl
, temp
| UHCI_PTR_QH
,UHCI_PTR_TERM
);
483 /* 1ms Interrupt td */
484 temp
=(unsigned long)&qh_cntrl
;
485 usb_fill_td(&td_int
[0],temp
| UHCI_PTR_QH
,0,0,0,0L);
486 temp
=(unsigned long)&td_int
[0];
488 usb_fill_td(&td_int
[n
],temp
,0,0,0,0L);
489 for (n
= 0; n
< 1024; n
++) {
490 /* link all framelist pointers to one of the interrupts */
493 framelist
[n
]= swap_32((unsigned long)&td_int
[0]);
495 for (o
= 1, m
= 2; m
<= 128; o
++, m
+= m
)
496 if ((n
& (m
- 1)) == ((m
- 1) / 2))
497 framelist
[n
]= swap_32((unsigned long)&td_int
[o
]);
501 /* check the common skeleton for completed transfers, and update the status
502 * of the "connected" device. Called from the IRQ routine.
504 void usb_check_skel(void)
506 struct usb_device
*dev
;
507 /* start with the control qh */
508 if(qh_cntrl
.dev_ptr
!=0) /* it's a device assigned check if this caused IRQ */
510 dev
=(struct usb_device
*)qh_cntrl
.dev_ptr
;
511 usb_get_td_status(&tmp_td
[0],dev
); /* update status */
512 if(!(dev
->status
& USB_ST_NOT_PROC
)) { /* is not active anymore, disconnect devices */
516 /* now process the bulk */
517 if(qh_bulk
.dev_ptr
!=0) /* it's a device assigned check if this caused IRQ */
519 dev
=(struct usb_device
*)qh_bulk
.dev_ptr
;
520 usb_get_td_status(&tmp_td
[0],dev
); /* update status */
521 if(!(dev
->status
& USB_ST_NOT_PROC
)) { /* is not active anymore, disconnect devices */
527 /* check the interrupt chain, ubdate the status of the appropriate device,
528 * call the appropriate irqhandler and reactivate the TD if the irqhandler
531 void usb_check_int_chain(void)
534 unsigned long link
,status
;
535 struct usb_device
*dev
;
536 uhci_td_t
*td
,*prevtd
;
539 prevtd
= &td_int
[i
]; /* the first previous td is the skeleton td */
540 link
=swap_32(td_int
[i
].link
) & 0xfffffff0; /* next in chain */
541 td
=(uhci_td_t
*)link
; /* assign it */
542 /* all interrupt TDs are finally linked to the td_int[0].
543 * so we process all until we find the td_int[0].
544 * if int0 chain points to a QH, we're also done
546 while(((i
>0) && (link
!= (unsigned long)&td_int
[0])) ||
547 ((i
==0) && !(swap_32(td
->link
) & UHCI_PTR_QH
)))
549 /* check if a device is assigned with this td */
550 status
=swap_32(td
->status
);
551 if((td
->dev_ptr
!=0L) && !(status
& TD_CTRL_ACTIVE
)) {
552 /* td is not active and a device is assigned -> call irqhandler */
553 dev
=(struct usb_device
*)td
->dev_ptr
;
554 dev
->irq_act_len
=((status
& 0x7FF)==0x7FF) ? 0 : (status
& 0x7FF) + 1; /* transfered length */
555 dev
->irq_status
=usb_uhci_td_stat(status
); /* get status */
556 res
=dev
->irq_handle(dev
); /* call irqhandler */
559 status
|=TD_CTRL_ACTIVE
;
560 td
->status
=swap_32(status
);
561 prevtd
=td
; /* previous td = this td */
564 prevtd
->link
=td
->link
; /* link previous td directly to the nex td -> unlinked */
565 /* remove device pointer */
568 } /* if we call the irq handler */
569 link
=swap_32(td
->link
) & 0xfffffff0; /* next in chain */
570 td
=(uhci_td_t
*)link
; /* assign it */
571 } /* process all td in this int chain */
572 } /* next interrupt chain */
576 /* usb interrupt service routine.
578 void handle_usb_interrupt(void)
580 unsigned short status
;
583 * Read the interrupt status, and write it back to clear the
587 status
= in16r(usb_base_addr
+ USBSTS
);
589 if (!status
) /* shared interrupt, not mine */
592 /* remove host controller halted state */
593 if ((status
&0x20) && ((in16r(usb_base_addr
+USBCMD
) && USBCMD_RS
)==0)) {
594 out16r(usb_base_addr
+ USBCMD
, USBCMD_RS
| in16r(usb_base_addr
+ USBCMD
));
597 usb_check_int_chain(); /* call interrupt handlers for int tds */
598 usb_check_skel(); /* call completion handler for common transfer routines */
599 out16r(usb_base_addr
+USBSTS
,status
);
605 int usb_lowlevel_init(void)
610 busdevfunc
=pci_find_device(USB_UHCI_VEND_ID
,USB_UHCI_DEV_ID
,0); /* get PCI Device ID */
612 printf("Error USB UHCI (%04X,%04X) not found\n",USB_UHCI_VEND_ID
,USB_UHCI_DEV_ID
);
615 pci_read_config_byte(busdevfunc
,PCI_INTERRUPT_LINE
,&temp
);
617 irq_free_handler(irqvec
);
618 USB_UHCI_PRINTF("Interrupt Line = %d, is %d\n",irqvec
);
619 pci_read_config_byte(busdevfunc
,PCI_INTERRUPT_PIN
,&temp
);
620 USB_UHCI_PRINTF("Interrupt Pin = %ld\n",temp
);
621 pci_read_config_dword(busdevfunc
,PCI_BASE_ADDRESS_4
,&usb_base_addr
);
622 USB_UHCI_PRINTF("IO Base Address = 0x%lx\n",usb_base_addr
);
623 usb_base_addr
&=0xFFFFFFF0;
624 usb_base_addr
+=CONFIG_SYS_ISA_IO_BASE_ADDRESS
;
629 irq_install_handler(irqvec
, (interrupt_handler_t
*)handle_usb_interrupt
, NULL
);
635 int usb_lowlevel_stop(void)
639 irq_free_handler(irqvec
);
645 /*******************************************************************************************
647 * Since the uhci does not have a real HUB, we simulate one ;-)
652 #define USB_RH_PRINTF(fmt,args...) printf (fmt ,##args)
653 static void usb_display_wValue(unsigned short wValue
,unsigned short wIndex
);
654 static void usb_display_Req(unsigned short req
);
656 #define USB_RH_PRINTF(fmt,args...)
657 static void usb_display_wValue(unsigned short wValue
,unsigned short wIndex
) {}
658 static void usb_display_Req(unsigned short req
) {}
661 static unsigned char root_hub_dev_des
[] =
663 0x12, /* __u8 bLength; */
664 0x01, /* __u8 bDescriptorType; Device */
665 0x00, /* __u16 bcdUSB; v1.0 */
667 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
668 0x00, /* __u8 bDeviceSubClass; */
669 0x00, /* __u8 bDeviceProtocol; */
670 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
671 0x00, /* __u16 idVendor; */
673 0x00, /* __u16 idProduct; */
675 0x00, /* __u16 bcdDevice; */
677 0x01, /* __u8 iManufacturer; */
678 0x00, /* __u8 iProduct; */
679 0x00, /* __u8 iSerialNumber; */
680 0x01 /* __u8 bNumConfigurations; */
684 /* Configuration descriptor */
685 static unsigned char root_hub_config_des
[] =
687 0x09, /* __u8 bLength; */
688 0x02, /* __u8 bDescriptorType; Configuration */
689 0x19, /* __u16 wTotalLength; */
691 0x01, /* __u8 bNumInterfaces; */
692 0x01, /* __u8 bConfigurationValue; */
693 0x00, /* __u8 iConfiguration; */
694 0x40, /* __u8 bmAttributes;
695 Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup, 4..0: resvd */
696 0x00, /* __u8 MaxPower; */
699 0x09, /* __u8 if_bLength; */
700 0x04, /* __u8 if_bDescriptorType; Interface */
701 0x00, /* __u8 if_bInterfaceNumber; */
702 0x00, /* __u8 if_bAlternateSetting; */
703 0x01, /* __u8 if_bNumEndpoints; */
704 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
705 0x00, /* __u8 if_bInterfaceSubClass; */
706 0x00, /* __u8 if_bInterfaceProtocol; */
707 0x00, /* __u8 if_iInterface; */
710 0x07, /* __u8 ep_bLength; */
711 0x05, /* __u8 ep_bDescriptorType; Endpoint */
712 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
713 0x03, /* __u8 ep_bmAttributes; Interrupt */
714 0x08, /* __u16 ep_wMaxPacketSize; 8 Bytes */
716 0xff /* __u8 ep_bInterval; 255 ms */
720 static unsigned char root_hub_hub_des
[] =
722 0x09, /* __u8 bLength; */
723 0x29, /* __u8 bDescriptorType; Hub-descriptor */
724 0x02, /* __u8 bNbrPorts; */
725 0x00, /* __u16 wHubCharacteristics; */
727 0x01, /* __u8 bPwrOn2pwrGood; 2ms */
728 0x00, /* __u8 bHubContrCurrent; 0 mA */
729 0x00, /* __u8 DeviceRemovable; *** 7 Ports max *** */
730 0xff /* __u8 PortPwrCtrlMask; *** 7 ports max *** */
733 static unsigned char root_hub_str_index0
[] =
735 0x04, /* __u8 bLength; */
736 0x03, /* __u8 bDescriptorType; String-descriptor */
737 0x09, /* __u8 lang ID */
738 0x04, /* __u8 lang ID */
741 static unsigned char root_hub_str_index1
[] =
743 28, /* __u8 bLength; */
744 0x03, /* __u8 bDescriptorType; String-descriptor */
745 'U', /* __u8 Unicode */
746 0, /* __u8 Unicode */
747 'H', /* __u8 Unicode */
748 0, /* __u8 Unicode */
749 'C', /* __u8 Unicode */
750 0, /* __u8 Unicode */
751 'I', /* __u8 Unicode */
752 0, /* __u8 Unicode */
753 ' ', /* __u8 Unicode */
754 0, /* __u8 Unicode */
755 'R', /* __u8 Unicode */
756 0, /* __u8 Unicode */
757 'o', /* __u8 Unicode */
758 0, /* __u8 Unicode */
759 'o', /* __u8 Unicode */
760 0, /* __u8 Unicode */
761 't', /* __u8 Unicode */
762 0, /* __u8 Unicode */
763 ' ', /* __u8 Unicode */
764 0, /* __u8 Unicode */
765 'H', /* __u8 Unicode */
766 0, /* __u8 Unicode */
767 'u', /* __u8 Unicode */
768 0, /* __u8 Unicode */
769 'b', /* __u8 Unicode */
770 0, /* __u8 Unicode */
775 * Root Hub Control Pipe (interrupt Pipes are not supported)
779 int uhci_submit_rh_msg(struct usb_device
*dev
, unsigned long pipe
, void *buffer
,int transfer_len
,struct devrequest
*cmd
)
782 int leni
= transfer_len
;
788 unsigned short cstatus
;
790 unsigned short bmRType_bReq
;
791 unsigned short wValue
;
792 unsigned short wIndex
;
793 unsigned short wLength
;
795 if ((pipe
& PIPE_INTERRUPT
) == PIPE_INTERRUPT
) {
796 printf("Root-Hub submit IRQ: NOT implemented\n");
800 uhci
->rh
.interval
= urb
->interval
;
801 rh_init_int_timer (urb
);
805 bmRType_bReq
= cmd
->requesttype
| cmd
->request
<< 8;
806 wValue
= swap_16(cmd
->value
);
807 wIndex
= swap_16(cmd
->index
);
808 wLength
= swap_16(cmd
->length
);
809 usb_display_Req(bmRType_bReq
);
810 for (i
= 0; i
< 8; i
++)
812 USB_RH_PRINTF("Root-Hub: adr: %2x cmd(%1x): %02x%02x %04x %04x %04x\n",
813 dev
->devnum
, 8, cmd
->requesttype
,cmd
->request
, wValue
, wIndex
, wLength
);
815 switch (bmRType_bReq
) {
816 /* Request Destination:
817 without flags: Device,
818 RH_INTERFACE: interface,
819 RH_ENDPOINT: endpoint,
820 RH_CLASS means HUB here,
821 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
825 *(unsigned short *) data
= swap_16(1);
828 case RH_GET_STATUS
| RH_INTERFACE
:
829 *(unsigned short *) data
= swap_16(0);
832 case RH_GET_STATUS
| RH_ENDPOINT
:
833 *(unsigned short *) data
= swap_16(0);
836 case RH_GET_STATUS
| RH_CLASS
:
837 *(unsigned long *) data
= swap_32(0);
839 break; /* hub power ** */
840 case RH_GET_STATUS
| RH_OTHER
| RH_CLASS
:
842 status
= in16r(usb_base_addr
+ USBPORTSC1
+ 2 * (wIndex
- 1));
843 cstatus
= ((status
& USBPORTSC_CSC
) >> (1 - 0)) |
844 ((status
& USBPORTSC_PEC
) >> (3 - 1)) |
845 (rh
.c_p_r
[wIndex
- 1] << (0 + 4));
846 status
= (status
& USBPORTSC_CCS
) |
847 ((status
& USBPORTSC_PE
) >> (2 - 1)) |
848 ((status
& USBPORTSC_SUSP
) >> (12 - 2)) |
849 ((status
& USBPORTSC_PR
) >> (9 - 4)) |
850 (1 << 8) | /* power on ** */
851 ((status
& USBPORTSC_LSDA
) << (-8 + 9));
853 *(unsigned short *) data
= swap_16(status
);
854 *(unsigned short *) (data
+ 2) = swap_16(cstatus
);
857 case RH_CLEAR_FEATURE
| RH_ENDPOINT
:
859 case (RH_ENDPOINT_STALL
):
865 case RH_CLEAR_FEATURE
| RH_CLASS
:
867 case (RH_C_HUB_OVER_CURRENT
):
868 len
=0; /* hub power over current ** */
873 case RH_CLEAR_FEATURE
| RH_OTHER
| RH_CLASS
:
874 usb_display_wValue(wValue
,wIndex
);
876 case (RH_PORT_ENABLE
):
877 status
= in16r(usb_base_addr
+USBPORTSC1
+2*(wIndex
-1));
878 status
= (status
& 0xfff5) & ~USBPORTSC_PE
;
879 out16r(usb_base_addr
+USBPORTSC1
+2*(wIndex
-1),status
);
882 case (RH_PORT_SUSPEND
):
883 status
= in16r(usb_base_addr
+USBPORTSC1
+2*(wIndex
-1));
884 status
= (status
& 0xfff5) & ~USBPORTSC_SUSP
;
885 out16r(usb_base_addr
+USBPORTSC1
+2*(wIndex
-1),status
);
888 case (RH_PORT_POWER
):
889 len
=0; /* port power ** */
891 case (RH_C_PORT_CONNECTION
):
892 status
= in16r(usb_base_addr
+USBPORTSC1
+2*(wIndex
-1));
893 status
= (status
& 0xfff5) | USBPORTSC_CSC
;
894 out16r(usb_base_addr
+USBPORTSC1
+2*(wIndex
-1),status
);
897 case (RH_C_PORT_ENABLE
):
898 status
= in16r(usb_base_addr
+USBPORTSC1
+2*(wIndex
-1));
899 status
= (status
& 0xfff5) | USBPORTSC_PEC
;
900 out16r(usb_base_addr
+USBPORTSC1
+2*(wIndex
-1),status
);
903 case (RH_C_PORT_SUSPEND
):
904 /*** WR_RH_PORTSTAT(RH_PS_PSSC); */
907 case (RH_C_PORT_OVER_CURRENT
):
910 case (RH_C_PORT_RESET
):
911 rh
.c_p_r
[wIndex
- 1] = 0;
916 case RH_SET_FEATURE
| RH_OTHER
| RH_CLASS
:
917 usb_display_wValue(wValue
,wIndex
);
919 case (RH_PORT_SUSPEND
):
920 status
= in16r(usb_base_addr
+USBPORTSC1
+2*(wIndex
-1));
921 status
= (status
& 0xfff5) | USBPORTSC_SUSP
;
922 out16r(usb_base_addr
+USBPORTSC1
+2*(wIndex
-1),status
);
925 case (RH_PORT_RESET
):
926 status
= in16r(usb_base_addr
+USBPORTSC1
+2*(wIndex
-1));
927 status
= (status
& 0xfff5) | USBPORTSC_PR
;
928 out16r(usb_base_addr
+USBPORTSC1
+2*(wIndex
-1),status
);
930 status
= (status
& 0xfff5) & ~USBPORTSC_PR
;
931 out16r(usb_base_addr
+USBPORTSC1
+2*(wIndex
-1),status
);
933 status
= (status
& 0xfff5) | USBPORTSC_PE
;
934 out16r(usb_base_addr
+USBPORTSC1
+2*(wIndex
-1),status
);
936 status
= (status
& 0xfff5) | 0xa;
937 out16r(usb_base_addr
+USBPORTSC1
+2*(wIndex
-1),status
);
940 case (RH_PORT_POWER
):
941 len
=0; /* port power ** */
943 case (RH_PORT_ENABLE
):
944 status
= in16r(usb_base_addr
+USBPORTSC1
+2*(wIndex
-1));
945 status
= (status
& 0xfff5) | USBPORTSC_PE
;
946 out16r(usb_base_addr
+USBPORTSC1
+2*(wIndex
-1),status
);
956 case RH_GET_DESCRIPTOR
:
957 switch ((wValue
& 0xff00) >> 8) {
958 case (0x01): /* device descriptor */
959 i
=sizeof(root_hub_config_des
);
960 status
=i
> wLength
? wLength
: i
;
961 len
= leni
> status
? status
: leni
;
962 memcpy (data
, root_hub_dev_des
, len
);
964 case (0x02): /* configuration descriptor */
965 i
=sizeof(root_hub_config_des
);
966 status
=i
> wLength
? wLength
: i
;
967 len
= leni
> status
? status
: leni
;
968 memcpy (data
, root_hub_config_des
, len
);
970 case (0x03): /*string descriptors */
972 i
=sizeof(root_hub_str_index0
);
973 status
= i
> wLength
? wLength
: i
;
974 len
= leni
> status
? status
: leni
;
975 memcpy (data
, root_hub_str_index0
, len
);
979 i
=sizeof(root_hub_str_index1
);
980 status
= i
> wLength
? wLength
: i
;
981 len
= leni
> status
? status
: leni
;
982 memcpy (data
, root_hub_str_index1
, len
);
985 stat
= USB_ST_STALLED
;
989 case RH_GET_DESCRIPTOR
| RH_CLASS
:
990 root_hub_hub_des
[2] = 2;
991 i
=sizeof(root_hub_hub_des
);
992 status
= i
> wLength
? wLength
: i
;
993 len
= leni
> status
? status
: leni
;
994 memcpy (data
, root_hub_hub_des
, len
);
996 case RH_GET_CONFIGURATION
:
997 *(unsigned char *) data
= 0x01;
1000 case RH_SET_CONFIGURATION
:
1004 stat
= USB_ST_STALLED
;
1006 USB_RH_PRINTF("Root-Hub stat %lx port1: %x port2: %x\n\n",stat
,
1007 in16r(usb_base_addr
+ USBPORTSC1
), in16r(usb_base_addr
+ USBPORTSC2
));
1014 /********************************************************************************
1015 * Some Debug Routines
1020 static void usb_display_Req(unsigned short req
)
1022 USB_RH_PRINTF("- Root-Hub Request: ");
1025 USB_RH_PRINTF("Get Status ");
1027 case RH_GET_STATUS
| RH_INTERFACE
:
1028 USB_RH_PRINTF("Get Status Interface ");
1030 case RH_GET_STATUS
| RH_ENDPOINT
:
1031 USB_RH_PRINTF("Get Status Endpoint ");
1033 case RH_GET_STATUS
| RH_CLASS
:
1034 USB_RH_PRINTF("Get Status Class");
1035 break; /* hub power ** */
1036 case RH_GET_STATUS
| RH_OTHER
| RH_CLASS
:
1037 USB_RH_PRINTF("Get Status Class Others");
1039 case RH_CLEAR_FEATURE
| RH_ENDPOINT
:
1040 USB_RH_PRINTF("Clear Feature Endpoint ");
1042 case RH_CLEAR_FEATURE
| RH_CLASS
:
1043 USB_RH_PRINTF("Clear Feature Class ");
1045 case RH_CLEAR_FEATURE
| RH_OTHER
| RH_CLASS
:
1046 USB_RH_PRINTF("Clear Feature Other Class ");
1048 case RH_SET_FEATURE
| RH_OTHER
| RH_CLASS
:
1049 USB_RH_PRINTF("Set Feature Other Class ");
1051 case RH_SET_ADDRESS
:
1052 USB_RH_PRINTF("Set Address ");
1054 case RH_GET_DESCRIPTOR
:
1055 USB_RH_PRINTF("Get Descriptor ");
1057 case RH_GET_DESCRIPTOR
| RH_CLASS
:
1058 USB_RH_PRINTF("Get Descriptor Class ");
1060 case RH_GET_CONFIGURATION
:
1061 USB_RH_PRINTF("Get Configuration ");
1063 case RH_SET_CONFIGURATION
:
1064 USB_RH_PRINTF("Get Configuration ");
1067 USB_RH_PRINTF("****UNKNOWN**** 0x%04X ",req
);
1069 USB_RH_PRINTF("\n");
1073 static void usb_display_wValue(unsigned short wValue
,unsigned short wIndex
)
1076 case (RH_PORT_ENABLE
):
1077 USB_RH_PRINTF("Root-Hub: Enable Port %d\n",wIndex
);
1079 case (RH_PORT_SUSPEND
):
1080 USB_RH_PRINTF("Root-Hub: Suspend Port %d\n",wIndex
);
1082 case (RH_PORT_POWER
):
1083 USB_RH_PRINTF("Root-Hub: Port Power %d\n",wIndex
);
1085 case (RH_C_PORT_CONNECTION
):
1086 USB_RH_PRINTF("Root-Hub: C Port Connection Port %d\n",wIndex
);
1088 case (RH_C_PORT_ENABLE
):
1089 USB_RH_PRINTF("Root-Hub: C Port Enable Port %d\n",wIndex
);
1091 case (RH_C_PORT_SUSPEND
):
1092 USB_RH_PRINTF("Root-Hub: C Port Suspend Port %d\n",wIndex
);
1094 case (RH_C_PORT_OVER_CURRENT
):
1095 USB_RH_PRINTF("Root-Hub: C Port Over Current Port %d\n",wIndex
);
1097 case (RH_C_PORT_RESET
):
1098 USB_RH_PRINTF("Root-Hub: C Port reset Port %d\n",wIndex
);
1101 USB_RH_PRINTF("Root-Hub: unknown %x %x\n",wValue
,wIndex
);
1109 #ifdef USB_UHCI_DEBUG
1111 static int usb_display_td(uhci_td_t
*td
)
1116 printf("TD at %p:\n",td
);
1118 tmp
=swap_32(td
->link
);
1119 printf("Link points to 0x%08lX, %s first, %s, %s\n",tmp
&0xfffffff0,
1120 ((tmp
& 0x4)==0x4) ? "Depth" : "Breath",
1121 ((tmp
& 0x2)==0x2) ? "QH" : "TD",
1122 ((tmp
& 0x1)==0x1) ? "invalid" : "valid");
1123 valid
=((tmp
& 0x1)==0x0);
1124 tmp
=swap_32(td
->status
);
1125 printf(" %s %ld Errors %s %s %s \n %s %s %s %s %s %s\n Len 0x%lX\n",
1126 (((tmp
>>29)&0x1)==0x1) ? "SPD Enable" : "SPD Disable",
1128 (((tmp
>>26)&0x1)==0x1) ? "Low Speed" : "Full Speed",
1129 (((tmp
>>25)&0x1)==0x1) ? "ISO " : "",
1130 (((tmp
>>24)&0x1)==0x1) ? "IOC " : "",
1131 (((tmp
>>23)&0x1)==0x1) ? "Active " : "Inactive ",
1132 (((tmp
>>22)&0x1)==0x1) ? "Stalled" : "",
1133 (((tmp
>>21)&0x1)==0x1) ? "Data Buffer Error" : "",
1134 (((tmp
>>20)&0x1)==0x1) ? "Babble" : "",
1135 (((tmp
>>19)&0x1)==0x1) ? "NAK" : "",
1136 (((tmp
>>18)&0x1)==0x1) ? "Bitstuff Error" : "",
1138 tmp
=swap_32(td
->info
);
1139 printf(" MaxLen 0x%lX\n",((tmp
>>21)&0x7FF));
1140 printf(" %s Endpoint 0x%lX Dev Addr 0x%lX PID 0x%lX\n",((tmp
>>19)&0x1)==0x1 ? "TOGGLE" : "",
1141 ((tmp
>>15)&0xF),((tmp
>>8)&0x7F),tmp
&0xFF);
1142 tmp
=swap_32(td
->buffer
);
1143 printf(" Buffer 0x%08lX\n",tmp
);
1144 printf(" DEV %08lX\n",td
->dev_ptr
);
1149 void usb_show_td(int max
)
1153 for(i
=0;i
<max
;i
++) {
1154 usb_display_td(&tmp_td
[i
]);
1160 printf("tmp_td[%d]\n",i
);
1161 }while(usb_display_td(&tmp_td
[i
++]));
1167 #endif /* CONFIG_USB_UHCI */