Initial xloong code
[xloong.git] / sys / dev / usb / usb_uhci.c
blobaf558d26e396c6df6af757d29980882f019bdc14
1 /*
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
21 * Adapted for U-Boot:
22 * (C) Copyright 2001 Denis Peter, MPL AG Switzerland
24 * Adapted for Godson pmon
25 * (C) Copyright 2006 yanhua@ict.ac.cn
27 * See file CREDITS for list of people who contributed to this
28 * project.
30 * This program is free software; you can redistribute it and/or
31 * modify it under the terms of the GNU General Public License as
32 * published by the Free Software Foundation; either version 2 of
33 * the License, or (at your option) any later version.
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
40 * You should have received a copy of the GNU General Public License
41 * along with this program; if not, write to the Free Software
42 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
43 * MA 02111-1307 USA
48 /**********************************************************************
49 * How it works:
50 * -------------
51 * The framelist / Transfer descriptor / Queue Heads are similar like
52 * in the linux usb_uhci.c.
54 * During initialization, the following skeleton is allocated in init_skel:
56 * framespecific | common chain
58 * framelist[]
59 * [ 0 ]-----> TD ---------\
60 * [ 1 ]-----> TD ----------> TD ------> QH -------> QH -------> QH ---> NULL
61 * ... TD ---------/
62 * [1023]-----> TD --------/
64 * ^^ ^^ ^^ ^^ ^^
65 * 7 TDs for 1 TD for Start of Start of End Chain
66 * INT (2-128ms) 1ms-INT CTRL Chain BULK Chain
69 * Since this is a bootloader, the isochronous transfer descriptor have been removed.
71 * Interrupt Transfers.
72 * --------------------
73 * For Interupt transfers USB_MAX_TEMP_INT_TD Transfer descriptor are available. They
74 * will be inserted after the appropriate (depending the interval setting) skeleton TD.
75 * If an interrupt has been detected the dev->irqhandler is called. The status and number
76 * of transfered bytes is stored in dev->irq_status resp. dev->irq_act_len. If the
77 * dev->irqhandler returns 0, the interrupt TD is removed and disabled. If an 1 is returned,
78 * the interrupt TD will be reactivated.
80 * Control Transfers
81 * -----------------
82 * Control Transfers are issued by filling the tmp_td with the appropriate data and connect
83 * them to the qh_cntrl queue header. Before other control/bulk transfers can be issued,
84 * the programm has to wait for completion. This does not allows asynchronous data transfer.
86 * Bulk Transfers
87 * --------------
88 * Bulk Transfers are issued by filling the tmp_td with the appropriate data and connect
89 * them to the qh_bulk queue header. Before other control/bulk transfers can be issued,
90 * the programm has to wait for completion. This does not allows asynchronous data transfer.
95 #include <sys/param.h>
96 #include <sys/systm.h>
97 #include <sys/mbuf.h>
98 #include <sys/malloc.h>
99 #include <sys/kernel.h>
101 #include <vm/vm.h> /* for vtophys */
103 #include <machine/cpu.h>
104 #include <machine/bus.h>
105 #include <machine/intr.h>
107 #include <dev/pci/pcivar.h>
108 #include <dev/pci/pcireg.h>
109 #include <dev/pci/pcidevs.h>
110 #include <sys/device.h>
112 #include "usb.h"
113 #include "usb_uhci.h"
117 //#undef USB_UHCI_DEBUG
118 #define USB_UHCI_DEBUG
120 #ifdef USB_UHCI_DEBUG
121 #define USB_UHCI_PRINTF(fmt,args...) printf (fmt ,##args)
122 #else
123 #define USB_UHCI_PRINTF(fmt,args...)
124 #endif
127 #define out8r(addr, val) *((volatile u8*)(addr)) = (val)
128 #define out16r(addr, val) *((volatile u16*)(addr)) = (val)
129 #define out32r(addr, val) *((volatile u32*)(addr)) = (val)
130 #define in16r(addr) *((volatile u16*)(addr))
131 #define in32r(addr) *((volatile u32*)(addr))
133 static int uhci_stop = 1;
135 static int uhci_match(struct device *parent, void *match, void *aux);
136 static void uhci_attach(struct device *parent, struct device *self, void *aux);
139 static int uhci_submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,int transfer_len);
141 static int uhci_submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
142 int transfer_len,struct devrequest *setup);
144 static int uhci_submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,int transfer_len, int interval);
146 static int usb_lowlevel_init(void * hc_data);
148 int handle_usb_interrupt(void *);
150 struct cfattach uhci_ca = {
151 sizeof(struct uhci), uhci_match, uhci_attach
154 struct cfdriver uhci_cd = {
155 NULL, "usb-uhci", DV_DULL
158 struct usb_ops uhci_op = {
159 .submit_bulk_msg = uhci_submit_bulk_msg,
160 .submit_control_msg = uhci_submit_control_msg,
161 .submit_int_msg = uhci_submit_int_msg
165 static int uhci_match(struct device *parent, void *match, void *aux)
167 struct pci_attach_args *pa = aux;
168 static int addr=0xc000;
170 if(PCI_CLASS(pa->pa_class) == PCI_CLASS_SERIALBUS &&
171 PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_SERIALBUS_USB) {
172 if(((pa->pa_class >>8) & 0xff) == 0x00){
173 printf("usb %d/%d\n", pa->pa_device, pa->pa_function);
174 #if 0
175 if(!(pa->pa_function ==2))
176 return 0;
177 addr=_pci_allocate_io(_pci_head,0x20);
178 pci_conf_write(0, pa->pa_tag, 0x20, addr);
179 #endif
180 printf("Found usb uhci controller %x\n",
181 pci_conf_read(0, pa->pa_tag, 0x20));
182 return 1;
186 return 0;
190 static void uhci_attach(struct device *parent, struct device *self, void *aux)
193 uhci_t *uhci = (struct uhci*)self;
194 struct pci_attach_args *pa = (struct pci_attach_args *)aux;
196 const char *intrstr;
197 pci_chipset_tag_t pc = pa->pa_pc;
198 bus_space_tag_t iot = pa->pa_iot;
199 // bus_space_tag_t memt = pa->pa_memt;
200 pci_intr_handle_t ih;
201 bus_addr_t iobase;
202 bus_addr_t iosize;
204 #if 0
205 my_uhci = uhci;
206 #endif
208 if(pci_io_find(pc, pa->pa_tag, PCI_MEMBASE_1, &iobase, &iosize)){
209 printf("Can not find i/o space\n");
210 return ;
213 if(bus_space_map(iot, iobase, iosize, 0, &uhci->sc_sh)){
214 printf("Can not map i/o space\n");
215 return ;
218 printf("%s: iobase = %x/%x \n", self->dv_xname, iobase, uhci->sc_sh);
219 uhci->io_addr = uhci->sc_sh;
220 uhci->io_size = iosize;
221 uhci->sc_st = iot;
222 uhci->sc_pc = pc;
224 intrstr = pci_intr_string(pc, ih);
226 if(pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin,
227 pa->pa_intrline, &ih)) {
228 printf(": couldn't map interrupt\n");
229 return;
232 usb_lowlevel_init(self);
234 uhci->hc.uop = &uhci_op;
236 #if 1
237 uhci->sc_ih = pci_intr_establish(pc, ih, IPL_BIO, handle_usb_interrupt, uhci,
238 self->dv_xname);
239 if (uhci->sc_ih == NULL) {
240 printf(": couldn't establish interrupt");
241 if (intrstr != NULL)
242 printf(" at %s\n", intrstr);
243 return;
245 #endif
247 uhci->rdev = usb_alloc_new_device(uhci);
248 usb_new_device(uhci->rdev);
250 //usb_scan_devices(uhci);
255 /* temporary tds */
256 //FIXME
258 static struct virt_root_hub rh; /* struct for root hub */
260 /**********************************************************************
261 * some forward decleration
263 static int uhci_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
264 void *buffer, int transfer_len,struct devrequest *setup);
266 /* fill a td with the approproiate data. Link, status, info and buffer
267 * are used by the USB controller itselfes, dev is used to identify the
268 * "connected" device
270 void usb_fill_td(uhci_td_t* td,unsigned long link,unsigned long status,
271 unsigned long info, unsigned long buffer, unsigned long dev)
273 uhci_td_t *ptd = (uhci_td_t *)CACHED_TO_UNCACHED(td);
275 ptd->link=swap_32(link);
276 ptd->status=swap_32(status);
277 ptd->info=swap_32(info);
278 ptd->buffer=buffer;
279 //ptd->buffer=vtophys(buffer);
280 ptd->dev_ptr=dev;
283 /* fill a qh with the approproiate data. Head and element are used by the USB controller
284 * itselfes. As soon as a valid dev_ptr is filled, a td chain is connected to the qh.
285 * Please note, that after completion of the td chain, the entry element is removed /
286 * marked invalid by the USB controller.
288 void usb_fill_qh(uhci_qh_t* qh,unsigned long head,unsigned long element)
290 uhci_qh_t *pqh = (uhci_qh_t *)CACHED_TO_UNCACHED(qh);
292 pqh->head=swap_32(head);
293 pqh->element=(element);
294 pqh->dev_ptr=0L;
297 /* get the status of a td->status
299 unsigned long usb_uhci_td_stat(unsigned long status)
301 unsigned long result=0;
302 result |= (status & TD_CTRL_NAK) ? USB_ST_NAK_REC : 0;
303 result |= (status & TD_CTRL_STALLED) ? USB_ST_STALLED : 0;
304 result |= (status & TD_CTRL_DBUFERR) ? USB_ST_BUF_ERR : 0;
305 result |= (status & TD_CTRL_BABBLE) ? USB_ST_BABBLE_DET : 0;
306 result |= (status & TD_CTRL_CRCTIMEO) ? USB_ST_CRC_ERR : 0;
307 result |= (status & TD_CTRL_BITSTUFF) ? USB_ST_BIT_ERR : 0;
308 result |= (status & TD_CTRL_ACTIVE) ? USB_ST_NOT_PROC : 0;
309 return result;
312 /* get the status and the transfered len of a td chain.
313 * called from the completion handler
315 int usb_get_td_status(uhci_td_t *td,struct usb_device *dev)
317 unsigned long temp,info;
318 unsigned long stat;
319 int tlen;
320 uhci_td_t *mytd=(uhci_td_t *)CACHED_TO_UNCACHED(td);
322 if(dev->devnum==rh.devnum)
323 return 0;
324 dev->act_len=0;
325 stat=0;
326 do {
327 retry:
328 temp=swap_32(mytd->status);
329 wbflush();
330 stat=usb_uhci_td_stat(temp);
331 info=swap_32(mytd->info);
332 //printf("%x, stat =%x temp=%x\n", mytd, stat, temp);
333 if(stat & USB_ST_NOT_PROC){
334 #if 0
335 printf("dump qh\n");
337 uhci_td_t *int0 = (uhci_td_t *)CACHED_TO_UNCACHED(&td_int[0]);
338 uhci_qh_t *qh = (uhci_qh_t *)CACHED_TO_UNCACHED(&qh_cntrl);
339 unsigned long ltd = qh->element;
340 uhci_td_t *td;
341 printf("int0 %x\n", int0);
342 printf(" link= %x\n", int0->link);
343 printf(" status =%x\n", int0->status);
344 while(ltd != UHCI_PTR_TERM){
345 td = (uhci_td_t *)CACHED_TO_UNCACHED(ltd);
346 printf("td=%x \n", td);
347 printf("ltd= %x\n", ltd);
348 printf(" status %x\n", td->status);
349 printf(" info %x\n", td->info);
350 ltd = td->link;
354 #endif
355 delay(10);
356 goto retry;
359 if(((info & 0xff)!= USB_PID_SETUP) &&
360 (((info >> 21) & 0x7ff)!= 0x7ff))
361 { /* if not setup and not null data pack */
362 dev->act_len+= tlen = (temp +1 )& 0x7FF ; /* the transfered len is act_len + 1 */
363 //printf("act_len %d link %x\n", dev->act_len, mytd->link);
364 if(usb_pipecontrol(mytd->pipe) && usb_pipein(mytd->pipe)){
365 if(mytd->data)
366 memcpy(mytd->data, (void*)CACHED_TO_UNCACHED(mytd->buffer), tlen);
367 mytd->data = NULL;
370 if(usb_pipecontrol(mytd->pipe) && (tlen < (((info >>21) + 1) & 0x7ff ))){
371 if((info & 0xff) == USB_PID_IN){
372 uhci_qh_t *qh = (uhci_qh_t *)CACHED_TO_UNCACHED(dev->qpriv);
373 uhci_td_t *ltd = qh->last_td;
374 qh->element = vtophys(ltd);
375 mytd=CACHED_TO_UNCACHED(qh->last_td);
376 continue;
378 stat = 0;
379 break;
381 //otherwise bulk or int
383 if(stat) { /* status no ok */
384 dev->status=stat;
385 return -1;
387 temp=swap_32((unsigned long)mytd->link);
388 mytd=(uhci_td_t *)CACHED_TO_UNCACHED(temp & 0xfffffff0);
389 }while((temp & 0x1)==0); /* process all TDs */
390 dev->status=stat;
391 return 0; /* Ok */
395 /*-------------------------------------------------------------------
396 * LOW LEVEL STUFF
397 * assembles QHs und TDs for control, bulk and iso
398 *-------------------------------------------------------------------*/
400 /* Submits a control message. That is a Setup, Data and Status transfer.
401 * Routine does not wait for completion.
403 static int uhci_submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
404 int transfer_len,struct devrequest *setup)
406 unsigned long destination, status;
407 int maxsze = usb_maxpacket(dev, pipe);
408 unsigned long dataptr;
409 int len;
410 int pktsze;
411 int i=0;
412 int s;
413 uhci_t * uhci = dev->hc_private;
414 ((uhci_qh_t*)CACHED_TO_UNCACHED(&qh_cntrl))->element =UHCI_PTR_TERM;//0xffffffffL;
415 usb_fill_td(&td_int0,vtophys(&qh_cntrl) | UHCI_PTR_QH,0,0,0,0L);
416 memset(tmp_td, 0, sizeof(tmp_td));
417 pci_sync_cache(uhci->sc_pc, (vm_offset_t)&tmp_td, sizeof(tmp_td), SYNC_W);
419 if (!maxsze) {
420 USB_UHCI_PRINTF("uhci_submit_control_urb: pipesize for pipe %lx is zero\n", pipe);
421 return -1;
424 if(((pipe>>8)&0x7f)==rh.devnum) {
425 /* this is the root hub -> redirect it */
426 return uhci_submit_rh_msg(dev,pipe,buffer,transfer_len,setup);
428 USB_UHCI_PRINTF("uhci_submit_control start len %x, maxsize %x\n",transfer_len,maxsze);
429 s = splimp();
430 memset(tmp_td, 0, sizeof(tmp_td));
431 pci_sync_cache(uhci->sc_pc, (vm_offset_t)&tmp_td, sizeof(tmp_td), SYNC_W);
433 /* The "pipe" thing contains the destination in bits 8--18 */
434 destination = (pipe & PIPE_DEVEP_MASK) | USB_PID_SETUP; /* Setup stage */
435 /* 3 errors */
436 status = (pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | (3 << 27) |TD_CTRL_SPD;
437 /* Build the TD for the control request, try forever, 8 bytes of data */
438 memcpy(uhci->setup, setup, sizeof(*setup));
439 usb_fill_td(&tmp_td[i],UHCI_PTR_TERM ,status, destination | (7 << 21),vtophys((unsigned long)uhci->setup),(unsigned long)dev);
440 #if 0
442 char *sp=(char *)setup;
443 printf("SETUP to pipe %lx: %x %x %x %x %x %x %x %x\n", pipe,
444 sp[0],sp[1],sp[2],sp[3],sp[4],sp[5],sp[6],sp[7]);
446 #endif
447 if(usb_pipeout(pipe))
448 memcpy(uhci->control_buf, buffer, transfer_len);
450 if(transfer_len)
452 if(usb_pipeout(pipe))
453 pci_sync_cache(uhci->sc_pc, (vm_offset_t)uhci->control_buf, transfer_len, SYNC_W);
454 else
455 pci_sync_cache(uhci->sc_pc, (vm_offset_t)uhci->control_buf, transfer_len, SYNC_R);
457 dataptr = vtophys((unsigned long)uhci->control_buf);
459 else
460 dataptr = 0;
461 len=transfer_len;
463 /* If direction is "send", change the frame from SETUP (0x2D)
464 to OUT (0xE1). Else change it from SETUP to IN (0x69). */
465 destination = (pipe & PIPE_DEVEP_MASK) | ((pipe & USB_DIR_IN)==0 ? USB_PID_OUT : USB_PID_IN);
466 while (len > 0) {
467 /* data stage */
468 pktsze = len;
469 i++;
470 if (pktsze > maxsze)
471 pktsze = maxsze;
472 if(i>=USB_MAX_TEMP_TD){while(1)printf("there is no tds,please set USB_MAX_TEMP_TD larger than %d\n",USB_MAX_TEMP_TD);}
473 destination ^= 1 << TD_TOKEN_TOGGLE; /* toggle DATA0/1 */
474 usb_fill_td(&tmp_td[i],UHCI_PTR_TERM, status, destination | ((pktsze - 1) << 21),dataptr,(unsigned long)dev); /* Status, pktsze bytes of data */
475 ((uhci_td_t *)CACHED_TO_UNCACHED(&tmp_td[i-1]))->link=vtophys((unsigned long)&tmp_td[i]);
476 ((uhci_td_t *)CACHED_TO_UNCACHED(&tmp_td[i]))->pipe = pipe;
477 ((uhci_td_t *)CACHED_TO_UNCACHED(&tmp_td[i]))->data = buffer;
479 dataptr += pktsze;
480 buffer += pktsze;
481 len -= pktsze;
484 /* Build the final TD for control status */
485 /* It's only IN if the pipe is out AND we aren't expecting data */
487 destination &= ~UHCI_PID;
488 if (((pipe & USB_DIR_IN)==0) || (transfer_len == 0))
489 destination |= USB_PID_IN;
490 else
491 destination |= USB_PID_OUT;
492 destination |= 1 << TD_TOKEN_TOGGLE; /* End in Data1 */
493 i++;
494 status &= ~TD_CTRL_SPD;
495 /* no limit on errors on final packet , 0 bytes of data */
496 usb_fill_td(&tmp_td[i],UHCI_PTR_TERM, status | TD_CTRL_IOC, destination | (UHCI_NULL_DATA_SIZE << 21),0,(unsigned long)dev);
497 ((uhci_td_t*)CACHED_TO_UNCACHED(&tmp_td[i-1]))->link=vtophys((unsigned long)&tmp_td[i]); /* queue status td */
498 // usb_show_td(i+1);
499 USB_UHCI_PRINTF("uhci_submit_control end (%d tmp_tds used)\n",i);
500 /* first mark the control QH element terminated */
501 ((uhci_qh_t*)CACHED_TO_UNCACHED(&qh_cntrl))->element =UHCI_PTR_TERM;//0xffffffffL;
502 /* set qh active */
503 ((uhci_qh_t*)CACHED_TO_UNCACHED(&qh_cntrl))->dev_ptr=(unsigned long)dev;
504 dev->status = USB_ST_NOT_PROC;
505 /* fill in tmp_td_chain */
506 ((uhci_qh_t*)CACHED_TO_UNCACHED(&qh_cntrl))->element=vtophys((unsigned long)&tmp_td[0]);
508 ((uhci_qh_t*)CACHED_TO_UNCACHED(&qh_cntrl))->last_td = &tmp_td[i];
509 dev->qpriv = &qh_cntrl;
510 #if 1
511 s = spl0();
512 while(1){
513 if(!(dev->status & USB_ST_NOT_PROC)){
514 break;
516 spl0();
518 #else
519 while(1){
520 handle_usb_interrupt(uhci);
521 if(!(dev->status & USB_ST_NOT_PROC))
522 break;
524 #endif
525 splx(s);
526 return 0;
529 /*-------------------------------------------------------------------
530 * Prepare TDs for bulk transfers.
532 static int uhci_submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,int transfer_len)
534 unsigned long destination, status,info;
535 unsigned long dataptr;
536 int maxsze = usb_maxpacket(dev, pipe);
537 int len;
538 int i=0;
539 int s;
540 uhci_t *uhci = dev->hc_private;
542 ((uhci_qh_t *)CACHED_TO_UNCACHED(&qh_bulk))->element =UHCI_PTR_TERM;//0xffffffffL;
543 usb_fill_td(&td_int0,vtophys(&qh_bulk) | UHCI_PTR_QH,0,0,0,0L);
544 memset(tmp_td, 0, sizeof(tmp_td));
545 pci_sync_cache(uhci->sc_pc, (vm_offset_t)&tmp_td, sizeof(tmp_td), SYNC_W);
547 if(transfer_len < 0) {
548 printf("Negative transfer length in submit_bulk\n");
549 return -1;
551 //printf("uhci_submit_bulk_msg: transfer_len %x\n", transfer_len);
552 if (!maxsze)
553 return -1;
554 /* The "pipe" thing contains the destination in bits 8--18. */
555 s = splimp();
556 memset(tmp_td, 0, sizeof(tmp_td));
557 pci_sync_cache(uhci->sc_pc, (vm_offset_t)&tmp_td, sizeof(tmp_td), SYNC_W);
558 destination = (pipe & PIPE_DEVEP_MASK) | usb_packetid (pipe);
559 /* 3 errors */
560 status = (pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | (3 << 27);
561 /* ((urb->transfer_flags & USB_DISABLE_SPD) ? 0 : TD_CTRL_SPD) | (3 << 27); */
562 /* Build the TDs for the bulk request */
563 len = transfer_len;
564 dataptr = vtophys((unsigned long)buffer);
565 if(usb_pipeout(pipe))
566 pci_sync_cache(uhci->sc_pc, (vm_offset_t)buffer, transfer_len, SYNC_W);
567 else
568 pci_sync_cache(uhci->sc_pc, (vm_offset_t)buffer, transfer_len, SYNC_R);
570 do {
571 int pktsze = len;
572 if (pktsze > maxsze)
573 pktsze = maxsze;
574 /* pktsze bytes of data */
575 if(i>=USB_MAX_TEMP_TD){while(1)printf("there is no tds,please set USB_MAX_TEMP_TD larger than %d\n",USB_MAX_TEMP_TD);}
576 info = destination | (((pktsze - 1)&UHCI_NULL_DATA_SIZE) << 21) |
577 (usb_gettoggle (dev, usb_pipeendpoint (pipe), usb_pipeout (pipe)) << TD_TOKEN_TOGGLE);
579 if((len-pktsze)==0)
580 status |= TD_CTRL_IOC; /* last one generates INT */
582 usb_fill_td(&tmp_td[i],UHCI_PTR_TERM, status, info,dataptr,(unsigned long)dev); /* Status, pktsze bytes of data */
583 if(i>0)
584 ((uhci_td_t *)CACHED_TO_UNCACHED(&tmp_td[i-1]))->link=vtophys((unsigned long)&tmp_td[i] | 0x0004);
585 i++;
586 dataptr += pktsze;
587 len -= pktsze;
588 usb_dotoggle (dev, usb_pipeendpoint (pipe), usb_pipeout (pipe));
589 } while (len > 0);
590 /* first mark the bulk QH element terminated */
591 ((uhci_qh_t *)CACHED_TO_UNCACHED(&qh_bulk))->element =UHCI_PTR_TERM;//0xffffffffL;
592 /* set qh active */
593 ((uhci_qh_t *)CACHED_TO_UNCACHED(&qh_bulk))->dev_ptr=(unsigned long)dev;
594 dev->status = USB_ST_NOT_PROC;
595 /* fill in tmp_td_chain */
596 ((uhci_qh_t *)CACHED_TO_UNCACHED(&qh_bulk))->element=vtophys((unsigned long)&tmp_td[0] | 0x0004);
597 dev->qpriv = &qh_bulk;
599 #if 1
600 s = spl0();
601 while(1){
602 if(!(dev->status & USB_ST_NOT_PROC)){
603 break;
605 spl0();
607 #else
608 while(1){
609 handle_usb_interrupt(uhci);
610 if(!(dev->status & USB_ST_NOT_PROC))
611 break;
613 #endif
614 splx(s);
615 return 0;
619 /* search a free interrupt td
621 uhci_td_t *uhci_alloc_int_td(uhci_t *uhci)
623 int i;
624 for(i=0;i<USB_MAX_TEMP_INT_TD;i++) {
625 if(((uhci_td_t *)CACHED_TO_UNCACHED(&tmp_int_td[i]))->dev_ptr==0)
626 return &tmp_int_td[i];
628 return NULL;
631 #if 0
632 void uhci_show_temp_int_td(void)
634 int i;
635 for(i=0;i<USB_MAX_TEMP_INT_TD;i++) {
636 if((tmp_int_td[i].dev_ptr&0x01)!=0x1L) /* no device assigned -> free TD */
637 printf("temp_td %d is assigned to dev %lx\n",i,tmp_int_td[i].dev_ptr);
639 printf("all others temp_tds are free\n");
641 #endif
642 /*-------------------------------------------------------------------
643 * submits USB interrupt (ie. polling ;-)
645 static int uhci_submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,int transfer_len, int interval)
647 int nint, n;
648 unsigned long status, destination;
649 unsigned long info,tmp;
650 uhci_td_t *mytd;
651 uhci_t *uhci = dev->hc_private;
654 if (interval < 0 || interval >= 256)
655 return -1;
657 if (interval == 0)
658 nint = 0;
659 else {
660 for (nint = 0, n = 1; nint <= 8; nint++, n += n) /* round interval down to 2^n */
662 if(interval < n) {
663 interval = n / 2;
664 break;
667 nint--;
670 USB_UHCI_PRINTF("Rounded interval to %d, chain %d\n", interval, nint);
671 mytd=uhci_alloc_int_td(uhci);
672 if(mytd==NULL) {
673 printf("No free INT TDs found\n");
674 return -1;
678 status = (pipe & TD_CTRL_LS) | TD_CTRL_ACTIVE | TD_CTRL_IOC | (3 << 27);
679 /* (urb->transfer_flags & USB_DISABLE_SPD ? 0 : TD_CTRL_SPD) | (3 << 27);
682 destination =(pipe & PIPE_DEVEP_MASK) | usb_packetid (pipe) | (((transfer_len - 1) & 0x7ff) << 21);
684 info = destination | (usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe)) << TD_TOKEN_TOGGLE);
685 tmp = ((uhci_td_t *)CACHED_TO_UNCACHED(&td_int[nint]))->link;
686 /*only for small data < 64 byte, it shoutld be ok*/
687 if(usb_pipeout(pipe))
688 pci_sync_cache(uhci->sc_pc, (vm_offset_t)buffer, transfer_len, SYNC_W); //wb
689 else
690 pci_sync_cache(uhci->sc_pc, (vm_offset_t)buffer, transfer_len, SYNC_R); //wb
691 usb_fill_td(mytd,tmp,status, info,vtophys(buffer),(unsigned long)dev);
692 //usb_fill_td(mytd, tmp,status, info,(vm_offset_t)(buffer),(unsigned long)dev);
693 /* Link it */
694 tmp = swap_32((unsigned long)mytd);
695 ((uhci_td_t *)CACHED_TO_UNCACHED(&td_int[nint]))->link=vtophys(tmp);
697 usb_dotoggle (dev, usb_pipeendpoint (pipe), usb_pipeout (pipe));
699 return 0;
702 /**********************************************************************
703 * Low Level functions
707 void reset_hc(uhci_t *uhci)
710 /* Global reset for 100ms */
711 out16r( usb_base_addr + USBPORTSC1,0x0204);
712 out16r( usb_base_addr + USBPORTSC2,0x0204);
713 out16r( usb_base_addr + USBCMD,USBCMD_GRESET | USBCMD_RS);
714 /* Turn off all interrupts */
715 out16r(usb_base_addr + USBINTR,0);
716 wait_ms(50);
717 out16r( usb_base_addr + USBCMD,0);
718 wait_ms(10);
721 void start_hc(uhci_t *uhci)
723 int timeout = 1000;
725 while(in16r(usb_base_addr + USBCMD) & USBCMD_HCRESET) {
726 if (!--timeout) {
727 printf("USBCMD_HCRESET timed out!\n");
728 break;
732 out8r(usb_base_addr + USBSOF, 64);
733 /* Turn on all interrupts */
734 //out16r(usb_base_addr + USBINTR,USBINTR_TIMEOUT | USBINTR_RESUME | USBINTR_IOC | USBINTR_SP);
735 /* Start at frame 0 */
736 out16r(usb_base_addr + USBFRNUM,0);
737 /* set Framebuffer base address */
738 out32r(usb_base_addr+USBFLBASEADD,vtophys(&framelist));
739 /* Run and mark it configured with a 64-byte max packet */
740 out16r(usb_base_addr + USBCMD,USBCMD_RS | USBCMD_CF | USBCMD_MAXP);
743 /* Initialize the skeleton
745 void usb_init_skel(void *data)
747 unsigned long temp;
748 int n;
749 uhci_t *uhci =data;
751 for(n=0;n<USB_MAX_TEMP_INT_TD;n++)
752 tmp_int_td[n].dev_ptr=0L; /* no devices connected */
754 pci_sync_cache(uhci->sc_pc, (vm_offset_t)&tmp_int_td, sizeof(tmp_int_td), SYNC_W);
755 /* last td */
756 memset(&td_last, 0, sizeof(td_last));
757 pci_sync_cache(uhci->sc_pc, (vm_offset_t)&td_last, sizeof(td_last), SYNC_W);
759 usb_fill_td(&td_last,UHCI_PTR_TERM,TD_CTRL_IOC ,0,0,0L);
760 /* usb_fill_td(&td_last,UHCI_PTR_TERM,0,0,0); */
761 /* End Queue Header */
763 memset(&qh_end, 0, sizeof(qh_end));
764 pci_sync_cache(uhci->sc_pc, (vm_offset_t)&qh_end, sizeof(qh_end), SYNC_W);
766 usb_fill_qh(&qh_end,UHCI_PTR_TERM,vtophys((unsigned long)&td_last));
767 /* Bulk Queue Header */
769 memset(&qh_bulk, 0, sizeof(qh_bulk));
770 pci_sync_cache(uhci->sc_pc, (vm_offset_t)&qh_bulk, sizeof(qh_bulk), SYNC_W);
772 temp=(unsigned long)&qh_end;
773 usb_fill_qh(&qh_bulk,vtophys(temp) | UHCI_PTR_QH,UHCI_PTR_TERM);
774 /* Control Queue Header */
776 memset(&qh_cntrl, 0, sizeof(qh_cntrl));
777 pci_sync_cache(uhci->sc_pc, (vm_offset_t)&qh_cntrl, sizeof(qh_cntrl), SYNC_W);
779 // temp=(unsigned long)&qh_bulk;
780 temp=(unsigned long)&qh_end;
781 usb_fill_qh(&qh_cntrl, vtophys(temp) | UHCI_PTR_QH,UHCI_PTR_TERM);
782 /* 1ms Interrupt td */
784 memset(td_int, 0, sizeof(td_int));
785 pci_sync_cache(uhci->sc_pc, (vm_offset_t)&td_int, sizeof(td_int), SYNC_W);
787 memset(&td_int0, 0, sizeof(td_int0));
788 pci_sync_cache(uhci->sc_pc, (vm_offset_t)&td_int0, sizeof(td_int0), SYNC_W);
790 temp=(unsigned long)&qh_cntrl;
791 usb_fill_td(&td_int0,vtophys(temp) | UHCI_PTR_QH,0,0,0,0L);
793 temp=(unsigned long)&td_int0;
794 usb_fill_td(&td_int[0],vtophys(temp) ,0,0,0,0L);
796 memset(tmp_td, 0, sizeof(tmp_td));
797 pci_sync_cache(uhci->sc_pc, (vm_offset_t)&tmp_td, sizeof(tmp_td), SYNC_W);
799 temp=(unsigned long)&td_int[0];
800 for(n=1; n<8; n++)
801 usb_fill_td(&td_int[n],vtophys(temp),0,0,0,0L);
802 for (n = 0; n < 1024; n++) {
803 /* link all framelist pointers to one of the interrupts */
804 int m, o;
805 if ((n&127)==127)
806 framelist[n]= vtophys(swap_32((unsigned long)&td_int[0]));
807 else
808 for (o = 1, m = 2; m <= 128; o++, m += m)
809 if ((n & (m - 1)) == ((m - 1) / 2))
810 framelist[n]= vtophys(swap_32((unsigned long)&td_int[o]));
812 pci_sync_cache(uhci->sc_pc, (vm_offset_t)framelist, sizeof(framelist), SYNC_W);
816 /* check the common skeleton for completed transfers, and update the status
817 * of the "connected" device. Called from the IRQ routine.
819 void usb_check_skel(uhci_t *uhci)
821 struct usb_device *dev;
822 /* start with the control qh */
823 uhci_qh_t *qcntrl = (uhci_qh_t *)CACHED_TO_UNCACHED(&qh_cntrl);
824 uhci_qh_t *qbulk = (uhci_qh_t *)CACHED_TO_UNCACHED(&qh_bulk);
826 if(qcntrl->dev_ptr!=0) /* it's a device assigned check if this caused IRQ */
828 //printf("check qcntrl\n");
829 dev=(struct usb_device *)qcntrl->dev_ptr;
830 usb_get_td_status(&tmp_td[0],dev); /* update status */
831 if(!(dev->status & USB_ST_NOT_PROC)) { /* is not active anymore, disconnect devices */
832 qcntrl->dev_ptr=0;
833 if(!(qcntrl->element&1))qcntrl->element=UHCI_PTR_TERM;
836 /* now process the bulk */
837 if(qbulk->dev_ptr!=0) /* it's a device assigned check if this caused IRQ */
839 //printf("check qbulk\n");
840 dev=(struct usb_device *)qbulk->dev_ptr;
841 usb_get_td_status(&tmp_td[0],dev); /* update status */
842 if(!(dev->status & USB_ST_NOT_PROC)) { /* is not active anymore, disconnect devices */
843 qbulk->dev_ptr=0;
844 if(!(qbulk->element&1))qbulk->element=UHCI_PTR_TERM;
849 /* check the interrupt chain, ubdate the status of the appropriate device,
850 * call the appropriate irqhandler and reactivate the TD if the irqhandler
851 * returns with 1
853 void usb_check_int_chain(uhci_t *uhci)
855 int i,res;
856 unsigned long link,status;
857 struct usb_device *dev;
858 uhci_td_t *td,*prevtd;
860 for(i=0;i<8;i++) {
861 prevtd= (uhci_td_t *)CACHED_TO_UNCACHED(&td_int[i]); /* the first previous td is the skeleton td */
862 link=swap_32(((uhci_td_t *)CACHED_TO_UNCACHED(&td_int[i]))->link) & 0xfffffff0; /* next in chain */
863 td=(uhci_td_t *)CACHED_TO_UNCACHED(link); /* assign it */
864 /* all interrupt TDs are finally linked to the td_int[0].
865 * so we process all until we find the td_int[0].
866 * if int0 chain points to a QH, we're also done
868 while(((i>0) && (CACHED_TO_PHYS(link) != (unsigned long)CACHED_TO_PHYS(&td_int[0]))) ||
869 ((i==0) && !(swap_32(td->link) & UHCI_PTR_QH)))
871 /* check if a device is assigned with this td */
872 int len;
873 status=swap_32(td->status);
874 if((td->dev_ptr!=0L) && !(status & TD_CTRL_ACTIVE)) {
875 /* td is not active and a device is assigned -> call irqhandler */
876 dev=(struct usb_device *)td->dev_ptr;
877 dev->irq_act_len= len = ((status & 0x7FF)==0x7FF) ? 0 : (status & 0x7FF) + 1; /* transfered length */
878 dev->irq_status=usb_uhci_td_stat(status); /* get status */
879 res=dev->irq_handle(dev); /* call irqhandler */
880 if(res==1) {
881 /* reactivate */
882 status|=TD_CTRL_ACTIVE;
883 td->status=swap_32(status);
884 prevtd=td; /* previous td = this td */
886 else {
887 prevtd->link=td->link; /* link previous td directly to the nex td -> unlinked */
888 /* remove device pointer */
889 td->dev_ptr=0L;
891 } /* if we call the irq handler */
892 link=swap_32(td->link) & 0xfffffff0; /* next in chain */
893 td=(uhci_td_t *)CACHED_TO_UNCACHED(link); /* assign it */
894 } /* process all td in this int chain */
895 } /* next interrupt chain */
898 #if 0
899 void usb_unlink_inactive(uhci_qh_t *head)
901 uhci_qh_t *qh;
902 uhci_td_t *td, *ptd = NULL;
903 unsigned long link;
905 qh = (uhci_qh_t *)CACHED_TO_UNCACHED(head);
907 if(!(qh->element & UHCI_PTR_QH)){
908 link = qh->element;
909 td = (uhci_td_t *)CACHED_TO_UNCACHED(link & 0xfffffff0);
910 while(link != UHCI_PTR_TERM ){
911 printf("unlink: link %x\n", link);
912 printf(" status %x\n", td->status);
913 if(td->status & TD_CTRL_ACTIVE)
914 break;
915 printf("unlink %x\n", link);
916 link = td->link;
917 td = (uhci_td_t *)CACHED_TO_UNCACHED((td->link & 0xfffffff0));
919 qh->element = link;
922 #endif
923 /* usb interrupt service routine.
925 int handle_usb_interrupt(void *hc_data)
927 unsigned short status;
928 int s;
929 static int count = 0,count1=0;
930 uhci_t *uhci=hc_data;
932 s = splimp();
933 // if (count++%0x80000==0) printf("handle_usb_interrupt,count=%d\n",count);
935 * Read the interrupt status, and write it back to clear the
936 * interrupt cause
938 if (uhci_stop) return 0;
940 status = in16r(usb_base_addr + USBSTS);
942 if (!status) /* shared interrupt, not mine */
943 return 0;
945 // if (count1++%0x1000==0) printf("status=%x,count=%d\n",status,count);
946 if (status != 1) {
947 /* remove host controller halted state */
948 #if 1
949 if ((status&0x20) && ((in16r(usb_base_addr+USBCMD) && USBCMD_RS)==0)) {
950 out16r(usb_base_addr + USBCMD, USBCMD_RS | in16r(usb_base_addr + USBCMD));
952 #else
953 out16r(usb_base_addr + USBCMD, USBCMD_RS | in16r(usb_base_addr + USBCMD));
954 #endif
957 usb_check_int_chain(uhci); /* call interrupt handlers for int tds */
958 usb_check_skel(uhci); /* call completion handler for common transfer routines */
959 out16r(usb_base_addr+USBSTS,status);
961 splx(s);
962 return 0;
966 /* init uhci
968 static int usb_lowlevel_init(void * hc_data)
970 uhci_t *uhci = hc_data;
972 usb_base_addr = uhci->io_addr;
973 printf("usb_base_addr =%x\n", usb_base_addr);
975 rh.devnum = 0;
976 usb_init_skel(uhci);
978 memset(hc_setup, 0, sizeof(hc_setup));
979 memset(control_buf0, 0, sizeof(control_buf0));
980 pci_sync_cache(uhci->sc_pc, (vm_offset_t)hc_setup, sizeof(hc_setup), SYNC_W);
981 pci_sync_cache(uhci->sc_pc, (vm_offset_t)control_buf0, sizeof(control_buf0), SYNC_W);
983 uhci->setup = (unsigned char*)CACHED_TO_UNCACHED(hc_setup);
984 uhci->control_buf = (unsigned char*)CACHED_TO_UNCACHED(control_buf0);
986 reset_hc(uhci);
987 start_hc(uhci);
989 uhci_stop = 0;
991 return 0;
994 /* stop uhci
996 void usb_uhci_stop_one(uhci_t *uhci)
998 uhci_stop = 1;
999 reset_hc(uhci);
1002 void usb_uhci_stop()
1004 int cmd;
1005 uhci_t *tmp_uhci;
1006 struct device *dev, *next_dev;
1008 for (dev = TAILQ_FIRST(&alldevs); dev != NULL; dev = next_dev) {
1009 next_dev = TAILQ_NEXT(dev, dv_list);
1010 if((dev->dv_xname[4] == 'u') && (dev->dv_xname[5] == 'h') && (dev->dv_xname[6] == 'c') && (dev->dv_xname[7] == 'i'))
1012 tmp_uhci = (struct uhci*)dev;
1013 usb_uhci_stop_one(tmp_uhci);
1014 out16r( tmp_uhci->io_addr + USBPORTSC1,0);
1015 wait_ms(50);
1016 out16r( tmp_uhci->io_addr + USBPORTSC2,0);
1017 wait_ms(50);
1018 cmd=pci_conf_read(0, tmp_uhci->pa.pa_tag, 0x04);
1019 pci_conf_write(0, tmp_uhci->pa.pa_tag, 0x04, (cmd & ~0x7));
1023 /*******************************************************************************************
1024 * Virtual Root Hub
1025 * Since the uhci does not have a real HUB, we simulate one ;-)
1027 #define USB_RH_DEBUG
1029 #ifdef USB_RH_DEBUG
1030 #define USB_RH_PRINTF(fmt,args...) printf (fmt ,##args)
1031 static void usb_display_wValue(unsigned short wValue,unsigned short wIndex);
1032 static void usb_display_Req(unsigned short req);
1033 #else
1034 #define USB_RH_PRINTF(fmt,args...)
1035 static void usb_display_wValue(unsigned short wValue,unsigned short wIndex) {}
1036 static void usb_display_Req(unsigned short req) {}
1037 #endif
1039 static unsigned char root_hub_dev_des[] =
1041 0x12, /* __u8 bLength; */
1042 0x01, /* __u8 bDescriptorType; Device */
1043 0x00, /* __u16 bcdUSB; v1.0 */
1044 0x01,
1045 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
1046 0x00, /* __u8 bDeviceSubClass; */
1047 0x00, /* __u8 bDeviceProtocol; */
1048 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
1049 0x00, /* __u16 idVendor; */
1050 0x00,
1051 0x00, /* __u16 idProduct; */
1052 0x00,
1053 0x00, /* __u16 bcdDevice; */
1054 0x00,
1055 0x01, /* __u8 iManufacturer; */
1056 0x00, /* __u8 iProduct; */
1057 0x00, /* __u8 iSerialNumber; */
1058 0x01 /* __u8 bNumConfigurations; */
1062 /* Configuration descriptor */
1063 static unsigned char root_hub_config_des[] =
1065 0x09, /* __u8 bLength; */
1066 0x02, /* __u8 bDescriptorType; Configuration */
1067 0x19, /* __u16 wTotalLength; */
1068 0x00,
1069 0x01, /* __u8 bNumInterfaces; */
1070 0x01, /* __u8 bConfigurationValue; */
1071 0x00, /* __u8 iConfiguration; */
1072 0x40, /* __u8 bmAttributes;
1073 Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup, 4..0: resvd */
1074 0x00, /* __u8 MaxPower; */
1076 /* interface */
1077 0x09, /* __u8 if_bLength; */
1078 0x04, /* __u8 if_bDescriptorType; Interface */
1079 0x00, /* __u8 if_bInterfaceNumber; */
1080 0x00, /* __u8 if_bAlternateSetting; */
1081 0x01, /* __u8 if_bNumEndpoints; */
1082 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
1083 0x00, /* __u8 if_bInterfaceSubClass; */
1084 0x00, /* __u8 if_bInterfaceProtocol; */
1085 0x00, /* __u8 if_iInterface; */
1087 /* endpoint */
1088 0x07, /* __u8 ep_bLength; */
1089 0x05, /* __u8 ep_bDescriptorType; Endpoint */
1090 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
1091 0x03, /* __u8 ep_bmAttributes; Interrupt */
1092 0x08, /* __u16 ep_wMaxPacketSize; 8 Bytes */
1093 0x00,
1094 0xff /* __u8 ep_bInterval; 255 ms */
1098 static unsigned char root_hub_hub_des[] =
1100 0x09, /* __u8 bLength; */
1101 0x29, /* __u8 bDescriptorType; Hub-descriptor */
1102 0x02, /* __u8 bNbrPorts; */
1103 0x00, /* __u16 wHubCharacteristics; */
1104 0x00,
1105 0x01, /* __u8 bPwrOn2pwrGood; 2ms */
1106 0x00, /* __u8 bHubContrCurrent; 0 mA */
1107 0x00, /* __u8 DeviceRemovable; *** 7 Ports max *** */
1108 0xff /* __u8 PortPwrCtrlMask; *** 7 ports max *** */
1111 static unsigned char root_hub_str_index0[] =
1113 0x04, /* __u8 bLength; */
1114 0x03, /* __u8 bDescriptorType; String-descriptor */
1115 0x09, /* __u8 lang ID */
1116 0x04, /* __u8 lang ID */
1119 static unsigned char root_hub_str_index1[] =
1121 28, /* __u8 bLength; */
1122 0x03, /* __u8 bDescriptorType; String-descriptor */
1123 'U', /* __u8 Unicode */
1124 0, /* __u8 Unicode */
1125 'H', /* __u8 Unicode */
1126 0, /* __u8 Unicode */
1127 'C', /* __u8 Unicode */
1128 0, /* __u8 Unicode */
1129 'I', /* __u8 Unicode */
1130 0, /* __u8 Unicode */
1131 ' ', /* __u8 Unicode */
1132 0, /* __u8 Unicode */
1133 'R', /* __u8 Unicode */
1134 0, /* __u8 Unicode */
1135 'o', /* __u8 Unicode */
1136 0, /* __u8 Unicode */
1137 'o', /* __u8 Unicode */
1138 0, /* __u8 Unicode */
1139 't', /* __u8 Unicode */
1140 0, /* __u8 Unicode */
1141 ' ', /* __u8 Unicode */
1142 0, /* __u8 Unicode */
1143 'H', /* __u8 Unicode */
1144 0, /* __u8 Unicode */
1145 'u', /* __u8 Unicode */
1146 0, /* __u8 Unicode */
1147 'b', /* __u8 Unicode */
1148 0, /* __u8 Unicode */
1153 * Root Hub Control Pipe (interrupt Pipes are not supported)
1157 int uhci_submit_rh_msg(struct usb_device *dev, unsigned long pipe, void *buffer,int transfer_len,struct devrequest *cmd)
1159 void *data = buffer;
1160 int leni = transfer_len;
1161 int len = 0;
1162 int status = 0;
1163 int stat = 0;
1164 int i;
1166 unsigned short cstatus;
1168 unsigned short bmRType_bReq;
1169 unsigned short wValue;
1170 unsigned short wIndex;
1171 unsigned short wLength;
1172 uhci_t * uhci = dev->hc_private;
1174 if ((pipe & PIPE_INTERRUPT) == PIPE_INTERRUPT) {
1175 printf("Root-Hub submit IRQ: NOT implemented\n");
1176 #if 0
1177 uhci->rh.urb = urb;
1178 uhci->rh.send = 1;
1179 uhci->rh.interval = urb->interval;
1180 rh_init_int_timer (urb);
1181 #endif
1182 return 0;
1184 bmRType_bReq = cmd->requesttype | cmd->request << 8;
1185 wValue = swap_16(cmd->value);
1186 wIndex = swap_16(cmd->index);
1187 wLength = swap_16(cmd->length);
1188 usb_display_Req(bmRType_bReq);
1189 for (i = 0; i < 8; i++)
1190 rh.c_p_r[i] = 0;
1191 USB_RH_PRINTF("Root-Hub: adr: %2x cmd(%1x): %02x%02x %04x %04x %04x\n",
1192 dev->devnum, 8, cmd->requesttype,cmd->request, wValue, wIndex, wLength);
1194 switch (bmRType_bReq) {
1195 /* Request Destination:
1196 without flags: Device,
1197 RH_INTERFACE: interface,
1198 RH_ENDPOINT: endpoint,
1199 RH_CLASS means HUB here,
1200 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1203 case RH_GET_STATUS:
1204 *(unsigned short *) data = swap_16(1);
1205 len=2;
1206 break;
1207 case RH_GET_STATUS | RH_INTERFACE:
1208 *(unsigned short *) data = swap_16(0);
1209 len=2;
1210 break;
1211 case RH_GET_STATUS | RH_ENDPOINT:
1212 *(unsigned short *) data = swap_16(0);
1213 len=2;
1214 break;
1215 case RH_GET_STATUS | RH_CLASS:
1216 *(unsigned long *) data = swap_32(0);
1217 len=4;
1218 break; /* hub power ** */
1219 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
1221 status = in16r(usb_base_addr + USBPORTSC1 + 2 * (wIndex - 1));
1222 cstatus = ((status & USBPORTSC_CSC) >> (1 - 0)) |
1223 ((status & USBPORTSC_PEC) >> (3 - 1)) |
1224 (rh.c_p_r[wIndex - 1] << (0 + 4));
1225 status = (status & USBPORTSC_CCS) |
1226 ((status & USBPORTSC_PE) >> (2 - 1)) |
1227 ((status & USBPORTSC_SUSP) >> (12 - 2)) |
1228 ((status & USBPORTSC_PR) >> (9 - 4)) |
1229 (1 << 8) | /* power on ** */
1230 ((status & USBPORTSC_LSDA) << (-8 + 9));
1232 *(unsigned short *) data = swap_16(status);
1233 *(unsigned short *) (data + 2) = swap_16(cstatus);
1234 len=4;
1235 break;
1236 case RH_CLEAR_FEATURE | RH_ENDPOINT:
1237 switch (wValue) {
1238 case (RH_ENDPOINT_STALL):
1239 len=0;
1240 break;
1242 break;
1244 case RH_CLEAR_FEATURE | RH_CLASS:
1245 switch (wValue) {
1246 case (RH_C_HUB_OVER_CURRENT):
1247 len=0; /* hub power over current ** */
1248 break;
1250 break;
1252 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1253 usb_display_wValue(wValue,wIndex);
1254 switch (wValue) {
1255 case (RH_PORT_ENABLE):
1256 status = in16r(usb_base_addr+USBPORTSC1+2*(wIndex-1));
1257 status = (status & 0xfff5) & ~USBPORTSC_PE;
1258 out16r(usb_base_addr+USBPORTSC1+2*(wIndex-1),status);
1259 len=0;
1260 break;
1261 case (RH_PORT_SUSPEND):
1262 status = in16r(usb_base_addr+USBPORTSC1+2*(wIndex-1));
1263 status = (status & 0xfff5) & ~USBPORTSC_SUSP;
1264 out16r(usb_base_addr+USBPORTSC1+2*(wIndex-1),status);
1265 len=0;
1266 break;
1267 case (RH_PORT_POWER):
1268 len=0; /* port power ** */
1269 break;
1270 case (RH_C_PORT_CONNECTION):
1271 status = in16r(usb_base_addr+USBPORTSC1+2*(wIndex-1));
1272 status = (status & 0xfff5) | USBPORTSC_CSC;
1273 out16r(usb_base_addr+USBPORTSC1+2*(wIndex-1),status);
1274 len=0;
1275 break;
1276 case (RH_C_PORT_ENABLE):
1277 status = in16r(usb_base_addr+USBPORTSC1+2*(wIndex-1));
1278 status = (status & 0xfff5) | USBPORTSC_PEC;
1279 out16r(usb_base_addr+USBPORTSC1+2*(wIndex-1),status);
1280 len=0;
1281 break;
1282 case (RH_C_PORT_SUSPEND):
1283 /*** WR_RH_PORTSTAT(RH_PS_PSSC); */
1284 len=0;
1285 break;
1286 case (RH_C_PORT_OVER_CURRENT):
1287 len=0;
1288 break;
1289 case (RH_C_PORT_RESET):
1290 rh.c_p_r[wIndex - 1] = 0;
1291 len=0;
1292 break;
1294 break;
1295 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1296 usb_display_wValue(wValue,wIndex);
1297 switch (wValue) {
1298 case (RH_PORT_SUSPEND):
1299 status = in16r(usb_base_addr+USBPORTSC1+2*(wIndex-1));
1300 status = (status & 0xfff5) | USBPORTSC_SUSP;
1301 out16r(usb_base_addr+USBPORTSC1+2*(wIndex-1),status);
1302 len=0;
1303 break;
1304 case (RH_PORT_RESET):
1305 status = in16r(usb_base_addr+USBPORTSC1+2*(wIndex-1));
1306 status = (status & 0xfff5) | USBPORTSC_PR;
1307 out16r(usb_base_addr+USBPORTSC1+2*(wIndex-1),status);
1308 wait_ms(10);
1309 status = (status & 0xfff5) & ~USBPORTSC_PR;
1310 out16r(usb_base_addr+USBPORTSC1+2*(wIndex-1),status);
1311 udelay(10);
1312 status = (status & 0xfff5) | USBPORTSC_PE;
1313 out16r(usb_base_addr+USBPORTSC1+2*(wIndex-1),status);
1314 wait_ms(10);
1315 status = (status & 0xfff5) | 0xa;
1316 out16r(usb_base_addr+USBPORTSC1+2*(wIndex-1),status);
1317 len=0;
1318 break;
1319 case (RH_PORT_POWER):
1320 len=0; /* port power ** */
1321 break;
1322 case (RH_PORT_ENABLE):
1323 status = in16r(usb_base_addr+USBPORTSC1+2*(wIndex-1));
1324 status = (status & 0xfff5) | USBPORTSC_PE;
1325 out16r(usb_base_addr+USBPORTSC1+2*(wIndex-1),status);
1326 len=0;
1327 break;
1329 break;
1331 case RH_SET_ADDRESS:
1332 rh.devnum = wValue;
1333 len=0;
1334 break;
1335 case RH_GET_DESCRIPTOR:
1336 switch ((wValue & 0xff00) >> 8) {
1337 case (0x01): /* device descriptor */
1338 i=sizeof(root_hub_config_des);
1339 status=i > wLength ? wLength : i;
1340 len = leni > status ? status : leni;
1341 memcpy (data, root_hub_dev_des, len);
1342 break;
1343 case (0x02): /* configuration descriptor */
1344 i=sizeof(root_hub_config_des);
1345 status=i > wLength ? wLength : i;
1346 len = leni > status ? status : leni;
1347 memcpy (data, root_hub_config_des, len);
1348 break;
1349 case (0x03): /*string descriptors */
1350 if(wValue==0x0300) {
1351 i=sizeof(root_hub_str_index0);
1352 status = i > wLength ? wLength : i;
1353 len = leni > status ? status : leni;
1354 memcpy (data, root_hub_str_index0, len);
1355 break;
1357 if(wValue==0x0301) {
1358 i=sizeof(root_hub_str_index1);
1359 status = i > wLength ? wLength : i;
1360 len = leni > status ? status : leni;
1361 memcpy (data, root_hub_str_index1, len);
1362 break;
1364 stat = USB_ST_STALLED;
1366 break;
1368 case RH_GET_DESCRIPTOR | RH_CLASS:
1369 root_hub_hub_des[2] = 2;
1370 i=sizeof(root_hub_hub_des);
1371 status= i > wLength ? wLength : i;
1372 len = leni > status ? status : leni;
1373 memcpy (data, root_hub_hub_des, len);
1374 break;
1375 case RH_GET_CONFIGURATION:
1376 *(unsigned char *) data = 0x01;
1377 len = 1;
1378 break;
1379 case RH_SET_CONFIGURATION:
1380 len=0;
1381 break;
1382 default:
1383 stat = USB_ST_STALLED;
1385 USB_RH_PRINTF("Root-Hub stat %lx port1: %x port2: %x\n\n",stat,
1386 in16r(usb_base_addr + USBPORTSC1), in16r(usb_base_addr + USBPORTSC2));
1387 dev->act_len=len;
1388 dev->status=stat;
1389 return stat;
1393 /********************************************************************************
1394 * Some Debug Routines
1397 #ifdef USB_RH_DEBUG
1399 static void usb_display_Req(unsigned short req)
1401 USB_RH_PRINTF("- Root-Hub Request: ");
1402 switch (req) {
1403 case RH_GET_STATUS:
1404 USB_RH_PRINTF("Get Status ");
1405 break;
1406 case RH_GET_STATUS | RH_INTERFACE:
1407 USB_RH_PRINTF("Get Status Interface ");
1408 break;
1409 case RH_GET_STATUS | RH_ENDPOINT:
1410 USB_RH_PRINTF("Get Status Endpoint ");
1411 break;
1412 case RH_GET_STATUS | RH_CLASS:
1413 USB_RH_PRINTF("Get Status Class");
1414 break; /* hub power ** */
1415 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
1416 USB_RH_PRINTF("Get Status Class Others");
1417 break;
1418 case RH_CLEAR_FEATURE | RH_ENDPOINT:
1419 USB_RH_PRINTF("Clear Feature Endpoint ");
1420 break;
1421 case RH_CLEAR_FEATURE | RH_CLASS:
1422 USB_RH_PRINTF("Clear Feature Class ");
1423 break;
1424 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1425 USB_RH_PRINTF("Clear Feature Other Class ");
1426 break;
1427 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1428 USB_RH_PRINTF("Set Feature Other Class ");
1429 break;
1430 case RH_SET_ADDRESS:
1431 USB_RH_PRINTF("Set Address ");
1432 break;
1433 case RH_GET_DESCRIPTOR:
1434 USB_RH_PRINTF("Get Descriptor ");
1435 break;
1436 case RH_GET_DESCRIPTOR | RH_CLASS:
1437 USB_RH_PRINTF("Get Descriptor Class ");
1438 break;
1439 case RH_GET_CONFIGURATION:
1440 USB_RH_PRINTF("Get Configuration ");
1441 break;
1442 case RH_SET_CONFIGURATION:
1443 USB_RH_PRINTF("Get Configuration ");
1444 break;
1445 default:
1446 USB_RH_PRINTF("****UNKNOWN**** 0x%04X ",req);
1448 USB_RH_PRINTF("\n");
1452 static void usb_display_wValue(unsigned short wValue,unsigned short wIndex)
1454 switch (wValue) {
1455 case (RH_PORT_ENABLE):
1456 USB_RH_PRINTF("Root-Hub: Enable Port %d\n",wIndex);
1457 break;
1458 case (RH_PORT_SUSPEND):
1459 USB_RH_PRINTF("Root-Hub: Suspend Port %d\n",wIndex);
1460 break;
1461 case (RH_PORT_POWER):
1462 USB_RH_PRINTF("Root-Hub: Port Power %d\n",wIndex);
1463 break;
1464 case (RH_C_PORT_CONNECTION):
1465 USB_RH_PRINTF("Root-Hub: C Port Connection Port %d\n",wIndex);
1466 break;
1467 case (RH_C_PORT_ENABLE):
1468 USB_RH_PRINTF("Root-Hub: C Port Enable Port %d\n",wIndex);
1469 break;
1470 case (RH_C_PORT_SUSPEND):
1471 USB_RH_PRINTF("Root-Hub: C Port Suspend Port %d\n",wIndex);
1472 break;
1473 case (RH_C_PORT_OVER_CURRENT):
1474 USB_RH_PRINTF("Root-Hub: C Port Over Current Port %d\n",wIndex);
1475 break;
1476 case (RH_C_PORT_RESET):
1477 USB_RH_PRINTF("Root-Hub: C Port reset Port %d\n",wIndex);
1478 break;
1479 default:
1480 USB_RH_PRINTF("Root-Hub: unknown %x %x\n",wValue,wIndex);
1481 break;
1485 #endif
1488 #ifdef USB_UHCI_DEBUG
1490 static int usb_display_td(uhci_td_t *td)
1492 unsigned long tmp;
1493 int valid;
1495 printf("TD at %p:\n",td);
1497 tmp=swap_32(td->link);
1498 printf("Link points to 0x%08lX, %s first, %s, %s\n",tmp&0xfffffff0,
1499 ((tmp & 0x4)==0x4) ? "Depth" : "Breath",
1500 ((tmp & 0x2)==0x2) ? "QH" : "TD",
1501 ((tmp & 0x1)==0x1) ? "invalid" : "valid");
1502 valid=((tmp & 0x1)==0x0);
1503 tmp=swap_32(td->status);
1504 printf(" %s %ld Errors %s %s %s \n %s %s %s %s %s %s\n Len 0x%lX\n",
1505 (((tmp>>29)&0x1)==0x1) ? "SPD Enable" : "SPD Disable",
1506 ((tmp>>28)&0x3),
1507 (((tmp>>26)&0x1)==0x1) ? "Low Speed" : "Full Speed",
1508 (((tmp>>25)&0x1)==0x1) ? "ISO " : "",
1509 (((tmp>>24)&0x1)==0x1) ? "IOC " : "",
1510 (((tmp>>23)&0x1)==0x1) ? "Active " : "Inactive ",
1511 (((tmp>>22)&0x1)==0x1) ? "Stalled" : "",
1512 (((tmp>>21)&0x1)==0x1) ? "Data Buffer Error" : "",
1513 (((tmp>>20)&0x1)==0x1) ? "Babble" : "",
1514 (((tmp>>19)&0x1)==0x1) ? "NAK" : "",
1515 (((tmp>>18)&0x1)==0x1) ? "Bitstuff Error" : "",
1516 (tmp&0x7ff));
1517 tmp=swap_32(td->info);
1518 printf(" MaxLen 0x%lX\n",((tmp>>21)&0x7FF));
1519 printf(" %s Endpoint 0x%lX Dev Addr 0x%lX PID 0x%lX\n",((tmp>>19)&0x1)==0x1 ? "TOGGLE" : "",
1520 ((tmp>>15)&0xF),((tmp>>8)&0x7F),tmp&0xFF);
1521 tmp=swap_32(td->buffer);
1522 printf(" Buffer 0x%08lX\n",tmp);
1523 printf(" DEV %08lX\n",td->dev_ptr);
1524 return valid;
1528 void usb_show_td(uhci_t *uhci,int max)
1530 int i;
1531 if(max>0) {
1532 for(i=0;i<max;i++) {
1533 usb_display_td(&tmp_td[i]);
1536 else {
1537 i=0;
1538 do {
1539 printf("tmp_td[%d]\n",i);
1540 }while(usb_display_td(&tmp_td[i++]));
1544 #endif
1546 #include <pmon.h>
1548 static int uhci_debug (int argc, char *argv[])
1550 if(argc < 2){
1551 if(!uhci_stop)
1552 printf("uhci is running\n");
1553 return 0;
1556 if(!strcmp(argv[1],"start")){
1557 uhci_stop = 0;
1559 else if(!strcmp(argv[1], "stop"))
1561 usb_uhci_stop();
1562 uhci_stop = 1;
1564 else {
1565 printf("usage: uhci [stop|start]\n");
1567 return 0;
1570 static const Cmd Cmds[] = {
1571 {"UHCI debug"},
1572 {"uhci", "", NULL, "uhci debugger commands", uhci_debug, 1, 2, 0},
1573 {0, 0}
1576 static void init_cmd(void) __attribute__((constructor));
1578 static void init_cmd(void)
1580 printf("init uhci cmd called\n");
1581 cmdlist_expand(Cmds, 0);
1583 /* EOF */