2 * URB OHCI HCD (Host Controller Driver) for USB on the PPC440EP.
4 * (C) Copyright 2003-2004
5 * Gary Jennejohn, DENX Software Engineering <garyj@denx.de>
8 * Pierre Aubert, Staubli Faverges <p.aubert@staubli.com>
10 * Note: Much of this code has been derived from Linux 2.4
11 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
12 * (C) Copyright 2000-2002 David Brownell
14 * SPDX-License-Identifier: GPL-2.0+
18 * 1 - this driver is intended for use with USB Mass Storage Devices
19 * (BBB) ONLY. There is NO support for Interrupt or Isochronous pipes!
24 #ifdef CONFIG_USB_OHCI
30 #define OHCI_USE_NPS /* force NoPowerSwitching mode */
31 #undef OHCI_VERBOSE_DEBUG /* not always helpful */
34 #undef OHCI_FILL_TRACE
36 /* For initializing controller (mask in an HCFS mode too) */
37 #define OHCI_CONTROL_INIT \
38 (OHCI_CTRL_CBSR & 0x3) | OHCI_CTRL_IE | OHCI_CTRL_PLE
40 #define readl(a) (*((volatile u32 *)(a)))
41 #define writel(a, b) (*((volatile u32 *)(b)) = ((volatile u32)a))
44 #define dbg(format, arg...) printf("DEBUG: " format "\n", ## arg)
46 #define dbg(format, arg...) do {} while(0)
48 #define err(format, arg...) printf("ERROR: " format "\n", ## arg)
50 #define info(format, arg...) printf("INFO: " format "\n", ## arg)
52 #define info(format, arg...) do {} while(0)
55 #define m16_swap(x) swap_16(x)
56 #define m32_swap(x) swap_32(x)
58 #if defined(CONFIG_405EZ) || defined(CONFIG_440EP) || defined(CONFIG_440EPX)
59 #define ohci_cpu_to_le16(x) (x)
60 #define ohci_cpu_to_le32(x) (x)
62 #define ohci_cpu_to_le16(x) swap_16(x)
63 #define ohci_cpu_to_le32(x) swap_32(x)
68 /* this must be aligned to a 256 byte boundary */
69 struct ohci_hcca ghcca[1];
70 /* a pointer to the aligned storage */
71 struct ohci_hcca *phcca;
72 /* this allocates EDs for all possible endpoints */
73 struct ohci_device ohci_dev;
78 /* device which was disconnected */
79 struct usb_device *devgone;
80 /* flag guarding URB transation */
83 /*-------------------------------------------------------------------------*/
85 /* AMD-756 (D2 rev) reports corrupt register contents in some cases.
86 * The erratum (#4) description is incorrect. AMD's workaround waits
87 * till some bits (mostly reserved) are clear; ok for all revs.
89 #define OHCI_QUIRK_AMD756 0xabcd
90 #define read_roothub(hc, register, mask) ({ \
91 u32 temp = readl (&hc->regs->roothub.register); \
92 if (hc->flags & OHCI_QUIRK_AMD756) \
94 temp = readl (&hc->regs->roothub.register); \
97 static u32 roothub_a (struct ohci *hc)
98 { return read_roothub (hc, a, 0xfc0fe000); }
99 static inline u32 roothub_b (struct ohci *hc)
100 { return readl (&hc->regs->roothub.b); }
101 static inline u32 roothub_status (struct ohci *hc)
102 { return readl (&hc->regs->roothub.status); }
103 static u32 roothub_portstatus (struct ohci *hc, int i)
104 { return read_roothub (hc, portstatus [i], 0xffe0fce0); }
107 /* forward declaration */
108 static int hc_interrupt (void);
110 td_submit_job (struct usb_device * dev, unsigned long pipe, void * buffer,
111 int transfer_len, struct devrequest * setup, urb_priv_t * urb, int interval);
113 /*-------------------------------------------------------------------------*
114 * URB support functions
115 *-------------------------------------------------------------------------*/
117 /* free HCD-private data associated with this URB */
119 static void urb_free_priv (urb_priv_t * urb)
125 last = urb->length - 1;
127 for (i = 0; i <= last; i++) {
137 /*-------------------------------------------------------------------------*/
140 static int sohci_get_current_frame_number (struct usb_device * dev);
142 /* debug| print the main components of an URB
143 * small: 0) header + data packets 1) just header */
145 static void pkt_print (struct usb_device * dev, unsigned long pipe, void * buffer,
146 int transfer_len, struct devrequest * setup, char * str, int small)
148 urb_priv_t * purb = &urb_priv;
150 dbg("%s URB:[%4x] dev:%2d,ep:%2d-%c,type:%s,len:%d/%d stat:%#lx",
152 sohci_get_current_frame_number (dev),
153 usb_pipedevice (pipe),
154 usb_pipeendpoint (pipe),
155 usb_pipeout (pipe)? 'O': 'I',
156 usb_pipetype (pipe) < 2? (usb_pipeint (pipe)? "INTR": "ISOC"):
157 (usb_pipecontrol (pipe)? "CTRL": "BULK"),
159 transfer_len, dev->status);
160 #ifdef OHCI_VERBOSE_DEBUG
164 if (usb_pipecontrol (pipe)) {
165 printf (__FILE__ ": cmd(8):");
166 for (i = 0; i < 8 ; i++)
167 printf (" %02x", ((__u8 *) setup) [i]);
170 if (transfer_len > 0 && buffer) {
171 printf (__FILE__ ": data(%d/%d):",
174 len = usb_pipeout (pipe)?
175 transfer_len: purb->actual_length;
176 for (i = 0; i < 16 && i < len; i++)
177 printf (" %02x", ((__u8 *) buffer) [i]);
178 printf ("%s\n", i < len? "...": "");
184 /* just for debugging; prints non-empty branches of the int ed tree inclusive iso eds*/
185 void ep_print_int_eds (ohci_t *ohci, char * str) {
188 for (i= 0; i < 32; i++) {
190 ed_p = &(ohci->hcca->int_table [i]);
193 printf (__FILE__ ": %s branch int %2d(%2x):", str, i, i);
194 while (*ed_p != 0 && j--) {
195 ed_t *ed = (ed_t *)ohci_cpu_to_le32(ed_p);
196 printf (" ed: %4x;", ed->hwINFO);
197 ed_p = &ed->hwNextED;
203 static void ohci_dump_intr_mask (char *label, __u32 mask)
205 dbg ("%s: 0x%08x%s%s%s%s%s%s%s%s%s",
208 (mask & OHCI_INTR_MIE) ? " MIE" : "",
209 (mask & OHCI_INTR_OC) ? " OC" : "",
210 (mask & OHCI_INTR_RHSC) ? " RHSC" : "",
211 (mask & OHCI_INTR_FNO) ? " FNO" : "",
212 (mask & OHCI_INTR_UE) ? " UE" : "",
213 (mask & OHCI_INTR_RD) ? " RD" : "",
214 (mask & OHCI_INTR_SF) ? " SF" : "",
215 (mask & OHCI_INTR_WDH) ? " WDH" : "",
216 (mask & OHCI_INTR_SO) ? " SO" : ""
220 static void maybe_print_eds (char *label, __u32 value)
222 ed_t *edp = (ed_t *)value;
225 dbg ("%s %08x", label, value);
226 dbg ("%08x", edp->hwINFO);
227 dbg ("%08x", edp->hwTailP);
228 dbg ("%08x", edp->hwHeadP);
229 dbg ("%08x", edp->hwNextED);
233 static char * hcfs2string (int state)
236 case OHCI_USB_RESET: return "reset";
237 case OHCI_USB_RESUME: return "resume";
238 case OHCI_USB_OPER: return "operational";
239 case OHCI_USB_SUSPEND: return "suspend";
244 /* dump control and status registers */
245 static void ohci_dump_status (ohci_t *controller)
247 struct ohci_regs *regs = controller->regs;
250 temp = readl (®s->revision) & 0xff;
252 dbg ("spec %d.%d", (temp >> 4), (temp & 0x0f));
254 temp = readl (®s->control);
255 dbg ("control: 0x%08x%s%s%s HCFS=%s%s%s%s%s CBSR=%d", temp,
256 (temp & OHCI_CTRL_RWE) ? " RWE" : "",
257 (temp & OHCI_CTRL_RWC) ? " RWC" : "",
258 (temp & OHCI_CTRL_IR) ? " IR" : "",
259 hcfs2string (temp & OHCI_CTRL_HCFS),
260 (temp & OHCI_CTRL_BLE) ? " BLE" : "",
261 (temp & OHCI_CTRL_CLE) ? " CLE" : "",
262 (temp & OHCI_CTRL_IE) ? " IE" : "",
263 (temp & OHCI_CTRL_PLE) ? " PLE" : "",
264 temp & OHCI_CTRL_CBSR
267 temp = readl (®s->cmdstatus);
268 dbg ("cmdstatus: 0x%08x SOC=%d%s%s%s%s", temp,
269 (temp & OHCI_SOC) >> 16,
270 (temp & OHCI_OCR) ? " OCR" : "",
271 (temp & OHCI_BLF) ? " BLF" : "",
272 (temp & OHCI_CLF) ? " CLF" : "",
273 (temp & OHCI_HCR) ? " HCR" : ""
276 ohci_dump_intr_mask ("intrstatus", readl (®s->intrstatus));
277 ohci_dump_intr_mask ("intrenable", readl (®s->intrenable));
279 maybe_print_eds ("ed_periodcurrent", readl (®s->ed_periodcurrent));
281 maybe_print_eds ("ed_controlhead", readl (®s->ed_controlhead));
282 maybe_print_eds ("ed_controlcurrent", readl (®s->ed_controlcurrent));
284 maybe_print_eds ("ed_bulkhead", readl (®s->ed_bulkhead));
285 maybe_print_eds ("ed_bulkcurrent", readl (®s->ed_bulkcurrent));
287 maybe_print_eds ("donehead", readl (®s->donehead));
290 static void ohci_dump_roothub (ohci_t *controller, int verbose)
294 temp = roothub_a (controller);
295 ndp = (temp & RH_A_NDP);
298 dbg ("roothub.a: %08x POTPGT=%d%s%s%s%s%s NDP=%d", temp,
299 ((temp & RH_A_POTPGT) >> 24) & 0xff,
300 (temp & RH_A_NOCP) ? " NOCP" : "",
301 (temp & RH_A_OCPM) ? " OCPM" : "",
302 (temp & RH_A_DT) ? " DT" : "",
303 (temp & RH_A_NPS) ? " NPS" : "",
304 (temp & RH_A_PSM) ? " PSM" : "",
307 temp = roothub_b (controller);
308 dbg ("roothub.b: %08x PPCM=%04x DR=%04x",
310 (temp & RH_B_PPCM) >> 16,
313 temp = roothub_status (controller);
314 dbg ("roothub.status: %08x%s%s%s%s%s%s",
316 (temp & RH_HS_CRWE) ? " CRWE" : "",
317 (temp & RH_HS_OCIC) ? " OCIC" : "",
318 (temp & RH_HS_LPSC) ? " LPSC" : "",
319 (temp & RH_HS_DRWE) ? " DRWE" : "",
320 (temp & RH_HS_OCI) ? " OCI" : "",
321 (temp & RH_HS_LPS) ? " LPS" : ""
325 for (i = 0; i < ndp; i++) {
326 temp = roothub_portstatus (controller, i);
327 dbg ("roothub.portstatus [%d] = 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s",
330 (temp & RH_PS_PRSC) ? " PRSC" : "",
331 (temp & RH_PS_OCIC) ? " OCIC" : "",
332 (temp & RH_PS_PSSC) ? " PSSC" : "",
333 (temp & RH_PS_PESC) ? " PESC" : "",
334 (temp & RH_PS_CSC) ? " CSC" : "",
336 (temp & RH_PS_LSDA) ? " LSDA" : "",
337 (temp & RH_PS_PPS) ? " PPS" : "",
338 (temp & RH_PS_PRS) ? " PRS" : "",
339 (temp & RH_PS_POCI) ? " POCI" : "",
340 (temp & RH_PS_PSS) ? " PSS" : "",
342 (temp & RH_PS_PES) ? " PES" : "",
343 (temp & RH_PS_CCS) ? " CCS" : ""
348 static void ohci_dump (ohci_t *controller, int verbose)
350 dbg ("OHCI controller usb-%s state", controller->slot_name);
352 /* dumps some of the state we know about */
353 ohci_dump_status (controller);
355 ep_print_int_eds (controller, "hcca");
356 dbg ("hcca frame #%04x", controller->hcca->frame_no);
357 ohci_dump_roothub (controller, 1);
363 /*-------------------------------------------------------------------------*
364 * Interface functions (URB)
365 *-------------------------------------------------------------------------*/
367 /* get a transfer request */
369 int sohci_submit_job(struct usb_device *dev, unsigned long pipe, void *buffer,
370 int transfer_len, struct devrequest *setup, int interval)
374 urb_priv_t *purb_priv;
379 /* when controller's hung, permit only roothub cleanup attempts
380 * such as powering down ports */
381 if (ohci->disabled) {
382 err("sohci_submit_job: EPIPE");
386 /* if we have an unfinished URB from previous transaction let's
387 * fail and scream as quickly as possible so as not to corrupt
388 * further communication */
390 err("sohci_submit_job: URB NOT FINISHED");
393 /* we're about to begin a new transaction here so mark the URB unfinished */
396 /* every endpoint has a ed, locate and fill it */
397 if (!(ed = ep_add_ed (dev, pipe))) {
398 err("sohci_submit_job: ENOMEM");
402 /* for the private part of the URB we need the number of TDs (size) */
403 switch (usb_pipetype (pipe)) {
404 case PIPE_BULK: /* one TD for every 4096 Byte */
405 size = (transfer_len - 1) / 4096 + 1;
407 case PIPE_CONTROL: /* 1 TD for setup, 1 for ACK and 1 for every 4096 B */
408 size = (transfer_len == 0)? 2:
409 (transfer_len - 1) / 4096 + 3;
413 if (size >= (N_URB_TD - 1)) {
414 err("need %d TDs, only have %d", size, N_URB_TD);
417 purb_priv = &urb_priv;
418 purb_priv->pipe = pipe;
420 /* fill the private part of the URB */
421 purb_priv->length = size;
423 purb_priv->actual_length = 0;
425 /* allocate the TDs */
426 /* note that td[0] was allocated in ep_add_ed */
427 for (i = 0; i < size; i++) {
428 purb_priv->td[i] = td_alloc (dev);
429 if (!purb_priv->td[i]) {
430 purb_priv->length = i;
431 urb_free_priv (purb_priv);
432 err("sohci_submit_job: ENOMEM");
437 if (ed->state == ED_NEW || (ed->state & ED_DEL)) {
438 urb_free_priv (purb_priv);
439 err("sohci_submit_job: EINVAL");
443 /* link the ed into a chain if is not already */
444 if (ed->state != ED_OPER)
447 /* fill the TDs and link it to the ed */
448 td_submit_job(dev, pipe, buffer, transfer_len, setup, purb_priv, interval);
453 /*-------------------------------------------------------------------------*/
456 /* tell us the current USB frame number */
458 static int sohci_get_current_frame_number (struct usb_device *usb_dev)
460 ohci_t *ohci = &gohci;
462 return ohci_cpu_to_le16 (ohci->hcca->frame_no);
466 /*-------------------------------------------------------------------------*
467 * ED handling functions
468 *-------------------------------------------------------------------------*/
470 /* link an ed into one of the HC chains */
472 static int ep_link (ohci_t *ohci, ed_t *edi)
474 volatile ed_t *ed = edi;
481 if (ohci->ed_controltail == NULL) {
482 writel (ed, &ohci->regs->ed_controlhead);
484 ohci->ed_controltail->hwNextED = ohci_cpu_to_le32 ((unsigned long)ed);
486 ed->ed_prev = ohci->ed_controltail;
487 if (!ohci->ed_controltail && !ohci->ed_rm_list[0] &&
488 !ohci->ed_rm_list[1] && !ohci->sleeping) {
489 ohci->hc_control |= OHCI_CTRL_CLE;
490 writel (ohci->hc_control, &ohci->regs->control);
492 ohci->ed_controltail = edi;
497 if (ohci->ed_bulktail == NULL) {
498 writel (ed, &ohci->regs->ed_bulkhead);
500 ohci->ed_bulktail->hwNextED = ohci_cpu_to_le32 ((unsigned long)ed);
502 ed->ed_prev = ohci->ed_bulktail;
503 if (!ohci->ed_bulktail && !ohci->ed_rm_list[0] &&
504 !ohci->ed_rm_list[1] && !ohci->sleeping) {
505 ohci->hc_control |= OHCI_CTRL_BLE;
506 writel (ohci->hc_control, &ohci->regs->control);
508 ohci->ed_bulktail = edi;
514 /*-------------------------------------------------------------------------*/
516 /* unlink an ed from one of the HC chains.
517 * just the link to the ed is unlinked.
518 * the link from the ed still points to another operational ed or 0
519 * so the HC can eventually finish the processing of the unlinked ed */
521 static int ep_unlink (ohci_t *ohci, ed_t *edi)
523 volatile ed_t *ed = edi;
525 ed->hwINFO |= ohci_cpu_to_le32 (OHCI_ED_SKIP);
529 if (ed->ed_prev == NULL) {
531 ohci->hc_control &= ~OHCI_CTRL_CLE;
532 writel (ohci->hc_control, &ohci->regs->control);
534 writel (ohci_cpu_to_le32 (*((__u32 *)&ed->hwNextED)), &ohci->regs->ed_controlhead);
536 ed->ed_prev->hwNextED = ed->hwNextED;
538 if (ohci->ed_controltail == ed) {
539 ohci->ed_controltail = ed->ed_prev;
541 ((ed_t *)ohci_cpu_to_le32 (*((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
546 if (ed->ed_prev == NULL) {
548 ohci->hc_control &= ~OHCI_CTRL_BLE;
549 writel (ohci->hc_control, &ohci->regs->control);
551 writel (ohci_cpu_to_le32 (*((__u32 *)&ed->hwNextED)), &ohci->regs->ed_bulkhead);
553 ed->ed_prev->hwNextED = ed->hwNextED;
555 if (ohci->ed_bulktail == ed) {
556 ohci->ed_bulktail = ed->ed_prev;
558 ((ed_t *)ohci_cpu_to_le32 (*((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
562 ed->state = ED_UNLINK;
567 /*-------------------------------------------------------------------------*/
569 /* add/reinit an endpoint; this should be done once at the usb_set_configuration command,
570 * but the USB stack is a little bit stateless so we do it at every transaction
571 * if the state of the ed is ED_NEW then a dummy td is added and the state is changed to ED_UNLINK
572 * in all other cases the state is left unchanged
573 * the ed info fields are setted anyway even though most of them should not change */
575 static ed_t * ep_add_ed (struct usb_device *usb_dev, unsigned long pipe)
581 ed = ed_ret = &ohci_dev.ed[(usb_pipeendpoint (pipe) << 1) |
582 (usb_pipecontrol (pipe)? 0: usb_pipeout (pipe))];
584 if ((ed->state & ED_DEL) || (ed->state & ED_URB_DEL)) {
585 err("ep_add_ed: pending delete");
586 /* pending delete request */
590 if (ed->state == ED_NEW) {
591 ed->hwINFO = ohci_cpu_to_le32 (OHCI_ED_SKIP); /* skip ed */
592 /* dummy td; end of td list for ed */
593 td = td_alloc (usb_dev);
594 ed->hwTailP = ohci_cpu_to_le32 ((unsigned long)td);
595 ed->hwHeadP = ed->hwTailP;
596 ed->state = ED_UNLINK;
597 ed->type = usb_pipetype (pipe);
601 ed->hwINFO = ohci_cpu_to_le32 (usb_pipedevice (pipe)
602 | usb_pipeendpoint (pipe) << 7
603 | (usb_pipeisoc (pipe)? 0x8000: 0)
604 | (usb_pipecontrol (pipe)? 0: (usb_pipeout (pipe)? 0x800: 0x1000))
605 | (usb_dev->speed == USB_SPEED_LOW) << 13
606 | usb_maxpacket (usb_dev, pipe) << 16);
611 /*-------------------------------------------------------------------------*
612 * TD handling functions
613 *-------------------------------------------------------------------------*/
615 /* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
617 static void td_fill (ohci_t *ohci, unsigned int info,
619 struct usb_device *dev, int index, urb_priv_t *urb_priv)
621 volatile td_t *td, *td_pt;
622 #ifdef OHCI_FILL_TRACE
626 if (index > urb_priv->length) {
627 err("index > length");
630 /* use this td as the next dummy */
631 td_pt = urb_priv->td [index];
634 /* fill the old dummy TD */
635 td = urb_priv->td [index] = (td_t *)(ohci_cpu_to_le32 (urb_priv->ed->hwTailP) & ~0xf);
637 td->ed = urb_priv->ed;
638 td->next_dl_td = NULL;
640 td->data = (__u32)data;
641 #ifdef OHCI_FILL_TRACE
642 if (usb_pipebulk(urb_priv->pipe) && usb_pipeout(urb_priv->pipe)) {
643 for (i = 0; i < len; i++)
644 printf("td->data[%d] %#2x ",i, ((unsigned char *)td->data)[i]);
651 td->hwINFO = ohci_cpu_to_le32 (info);
652 td->hwCBP = ohci_cpu_to_le32 ((unsigned long)data);
654 td->hwBE = ohci_cpu_to_le32 ((unsigned long)(data + len - 1));
657 td->hwNextTD = ohci_cpu_to_le32 ((unsigned long)td_pt);
659 /* append to queue */
660 td->ed->hwTailP = td->hwNextTD;
663 /*-------------------------------------------------------------------------*/
665 /* prepare all TDs of a transfer */
666 static void td_submit_job (struct usb_device *dev, unsigned long pipe, void *buffer,
667 int transfer_len, struct devrequest *setup, urb_priv_t *urb, int interval)
669 ohci_t *ohci = &gohci;
670 int data_len = transfer_len;
674 unsigned int toggle = 0;
676 /* OHCI handles the DATA-toggles itself, we just use the
677 USB-toggle bits for resetting */
678 if(usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe))) {
679 toggle = TD_T_TOGGLE;
682 usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), 1);
690 switch (usb_pipetype (pipe)) {
692 info = usb_pipeout (pipe)?
693 TD_CC | TD_DP_OUT : TD_CC | TD_DP_IN ;
694 while(data_len > 4096) {
695 td_fill (ohci, info | (cnt? TD_T_TOGGLE:toggle), data, 4096, dev, cnt, urb);
696 data += 4096; data_len -= 4096; cnt++;
698 info = usb_pipeout (pipe)?
699 TD_CC | TD_DP_OUT : TD_CC | TD_R | TD_DP_IN ;
700 td_fill (ohci, info | (cnt? TD_T_TOGGLE:toggle), data, data_len, dev, cnt, urb);
704 writel (OHCI_BLF, &ohci->regs->cmdstatus); /* start bulk list */
708 info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
709 td_fill (ohci, info, setup, 8, dev, cnt++, urb);
711 info = usb_pipeout (pipe)?
712 TD_CC | TD_R | TD_DP_OUT | TD_T_DATA1 : TD_CC | TD_R | TD_DP_IN | TD_T_DATA1;
713 /* NOTE: mishandles transfers >8K, some >4K */
714 td_fill (ohci, info, data, data_len, dev, cnt++, urb);
716 info = usb_pipeout (pipe)?
717 TD_CC | TD_DP_IN | TD_T_DATA1: TD_CC | TD_DP_OUT | TD_T_DATA1;
718 td_fill (ohci, info, data, 0, dev, cnt++, urb);
720 writel (OHCI_CLF, &ohci->regs->cmdstatus); /* start Control list */
723 if (urb->length != cnt)
724 dbg("TD LENGTH %d != CNT %d", urb->length, cnt);
727 /*-------------------------------------------------------------------------*
728 * Done List handling functions
729 *-------------------------------------------------------------------------*/
732 /* calculate the transfer length and update the urb */
734 static void dl_transfer_length(td_t * td)
737 urb_priv_t *lurb_priv = &urb_priv;
739 tdBE = ohci_cpu_to_le32 (td->hwBE);
740 tdCBP = ohci_cpu_to_le32 (td->hwCBP);
743 if (!(usb_pipecontrol(lurb_priv->pipe) &&
744 ((td->index == 0) || (td->index == lurb_priv->length - 1)))) {
747 lurb_priv->actual_length += tdBE - td->data + 1;
749 lurb_priv->actual_length += tdCBP - td->data;
754 /*-------------------------------------------------------------------------*/
756 /* replies to the request have to be on a FIFO basis so
757 * we reverse the reversed done-list */
759 static td_t * dl_reverse_done_list (ohci_t *ohci)
763 td_t *td_list = NULL;
764 urb_priv_t *lurb_priv = NULL;
766 td_list_hc = ohci_cpu_to_le32 (ohci->hcca->done_head) & 0xfffffff0;
767 ohci->hcca->done_head = 0;
770 td_list = (td_t *)td_list_hc;
772 if (TD_CC_GET (ohci_cpu_to_le32 (td_list->hwINFO))) {
773 lurb_priv = &urb_priv;
774 dbg(" USB-error/status: %x : %p",
775 TD_CC_GET (ohci_cpu_to_le32 (td_list->hwINFO)), td_list);
776 if (td_list->ed->hwHeadP & ohci_cpu_to_le32 (0x1)) {
777 if (lurb_priv && ((td_list->index + 1) < lurb_priv->length)) {
778 td_list->ed->hwHeadP =
779 (lurb_priv->td[lurb_priv->length - 1]->hwNextTD & ohci_cpu_to_le32 (0xfffffff0)) |
780 (td_list->ed->hwHeadP & ohci_cpu_to_le32 (0x2));
781 lurb_priv->td_cnt += lurb_priv->length - td_list->index - 1;
783 td_list->ed->hwHeadP &= ohci_cpu_to_le32 (0xfffffff2);
785 #ifdef CONFIG_MPC5200
786 td_list->hwNextTD = 0;
790 td_list->next_dl_td = td_rev;
792 td_list_hc = ohci_cpu_to_le32 (td_list->hwNextTD) & 0xfffffff0;
797 /*-------------------------------------------------------------------------*/
800 static int dl_done_list (ohci_t *ohci, td_t *td_list)
802 td_t *td_list_next = NULL;
807 urb_priv_t *lurb_priv;
808 __u32 tdINFO, edHeadP, edTailP;
811 td_list_next = td_list->next_dl_td;
813 lurb_priv = &urb_priv;
814 tdINFO = ohci_cpu_to_le32 (td_list->hwINFO);
818 dl_transfer_length(td_list);
820 /* error code of transfer */
821 cc = TD_CC_GET (tdINFO);
822 if (++(lurb_priv->td_cnt) == lurb_priv->length) {
823 if ((ed->state & (ED_OPER | ED_UNLINK))
824 && (lurb_priv->state != URB_DEL)) {
825 dbg("ConditionCode %#x", cc);
826 stat = cc_to_error[cc];
831 if (ed->state != ED_NEW) {
832 edHeadP = ohci_cpu_to_le32 (ed->hwHeadP) & 0xfffffff0;
833 edTailP = ohci_cpu_to_le32 (ed->hwTailP);
835 /* unlink eds if they are not busy */
836 if ((edHeadP == edTailP) && (ed->state == ED_OPER))
837 ep_unlink (ohci, ed);
840 td_list = td_list_next;
845 /*-------------------------------------------------------------------------*
847 *-------------------------------------------------------------------------*/
849 #include <usbroothubdes.h>
851 /* Hub class-specific descriptor is constructed dynamically */
854 /*-------------------------------------------------------------------------*/
856 #define OK(x) len = (x); break
858 #define WR_RH_STAT(x) {info("WR:status %#8x", (x));writel((x), &gohci.regs->roothub.status);}
859 #define WR_RH_PORTSTAT(x) {info("WR:portstatus[%d] %#8x", wIndex-1, (x));writel((x), &gohci.regs->roothub.portstatus[wIndex-1]);}
861 #define WR_RH_STAT(x) writel((x), &gohci.regs->roothub.status)
862 #define WR_RH_PORTSTAT(x) writel((x), &gohci.regs->roothub.portstatus[wIndex-1])
864 #define RD_RH_STAT roothub_status(&gohci)
865 #define RD_RH_PORTSTAT roothub_portstatus(&gohci,wIndex-1)
867 /* request to virtual root hub */
869 int rh_check_port_status(ohci_t *controller)
875 temp = roothub_a (controller);
876 ndp = (temp & RH_A_NDP);
877 for (i = 0; i < ndp; i++) {
878 temp = roothub_portstatus (controller, i);
879 /* check for a device disconnect */
880 if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
881 (RH_PS_PESC | RH_PS_CSC)) &&
882 ((temp & RH_PS_CCS) == 0)) {
890 static int ohci_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
891 void *buffer, int transfer_len, struct devrequest *cmd)
893 void * data = buffer;
894 int leni = transfer_len;
898 __u8 *data_buf = (__u8 *)datab;
905 urb_priv.actual_length = 0;
906 pkt_print(dev, pipe, buffer, transfer_len, cmd, "SUB(rh)", usb_pipein(pipe));
908 if (usb_pipeint(pipe)) {
909 info("Root-Hub submit IRQ: NOT implemented");
913 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
914 wValue = m16_swap (cmd->value);
915 wIndex = m16_swap (cmd->index);
916 wLength = m16_swap (cmd->length);
918 info("Root-Hub: adr: %2x cmd(%1x): %08x %04x %04x %04x",
919 dev->devnum, 8, bmRType_bReq, wValue, wIndex, wLength);
921 switch (bmRType_bReq) {
922 /* Request Destination:
923 without flags: Device,
924 RH_INTERFACE: interface,
925 RH_ENDPOINT: endpoint,
926 RH_CLASS means HUB here,
927 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
931 *(__u16 *) data_buf = m16_swap (1); OK (2);
932 case RH_GET_STATUS | RH_INTERFACE:
933 *(__u16 *) data_buf = m16_swap (0); OK (2);
934 case RH_GET_STATUS | RH_ENDPOINT:
935 *(__u16 *) data_buf = m16_swap (0); OK (2);
936 case RH_GET_STATUS | RH_CLASS:
937 *(__u32 *) data_buf = m32_swap (
938 RD_RH_STAT & ~(RH_HS_CRWE | RH_HS_DRWE));
940 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
941 *(__u32 *) data_buf = m32_swap (RD_RH_PORTSTAT); OK (4);
943 case RH_CLEAR_FEATURE | RH_ENDPOINT:
945 case (RH_ENDPOINT_STALL): OK (0);
949 case RH_CLEAR_FEATURE | RH_CLASS:
951 case RH_C_HUB_LOCAL_POWER:
953 case (RH_C_HUB_OVER_CURRENT):
954 WR_RH_STAT(RH_HS_OCIC); OK (0);
958 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
960 case (RH_PORT_ENABLE):
961 WR_RH_PORTSTAT (RH_PS_CCS ); OK (0);
962 case (RH_PORT_SUSPEND):
963 WR_RH_PORTSTAT (RH_PS_POCI); OK (0);
964 case (RH_PORT_POWER):
965 WR_RH_PORTSTAT (RH_PS_LSDA); OK (0);
966 case (RH_C_PORT_CONNECTION):
967 WR_RH_PORTSTAT (RH_PS_CSC ); OK (0);
968 case (RH_C_PORT_ENABLE):
969 WR_RH_PORTSTAT (RH_PS_PESC); OK (0);
970 case (RH_C_PORT_SUSPEND):
971 WR_RH_PORTSTAT (RH_PS_PSSC); OK (0);
972 case (RH_C_PORT_OVER_CURRENT):
973 WR_RH_PORTSTAT (RH_PS_OCIC); OK (0);
974 case (RH_C_PORT_RESET):
975 WR_RH_PORTSTAT (RH_PS_PRSC); OK (0);
979 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
981 case (RH_PORT_SUSPEND):
982 WR_RH_PORTSTAT (RH_PS_PSS ); OK (0);
983 case (RH_PORT_RESET): /* BUG IN HUP CODE *********/
984 if (RD_RH_PORTSTAT & RH_PS_CCS)
985 WR_RH_PORTSTAT (RH_PS_PRS);
987 case (RH_PORT_POWER):
988 WR_RH_PORTSTAT (RH_PS_PPS ); OK (0);
989 case (RH_PORT_ENABLE): /* BUG IN HUP CODE *********/
990 if (RD_RH_PORTSTAT & RH_PS_CCS)
991 WR_RH_PORTSTAT (RH_PS_PES );
996 case RH_SET_ADDRESS: gohci.rh.devnum = wValue; OK(0);
998 case RH_GET_DESCRIPTOR:
999 switch ((wValue & 0xff00) >> 8) {
1000 case (0x01): /* device descriptor */
1001 len = min_t(unsigned int,
1004 sizeof (root_hub_dev_des),
1006 data_buf = root_hub_dev_des; OK(len);
1007 case (0x02): /* configuration descriptor */
1008 len = min_t(unsigned int,
1011 sizeof (root_hub_config_des),
1013 data_buf = root_hub_config_des; OK(len);
1014 case (0x03): /* string descriptors */
1015 if(wValue==0x0300) {
1016 len = min_t(unsigned int,
1019 sizeof (root_hub_str_index0),
1021 data_buf = root_hub_str_index0;
1024 if(wValue==0x0301) {
1025 len = min_t(unsigned int,
1028 sizeof (root_hub_str_index1),
1030 data_buf = root_hub_str_index1;
1034 stat = USB_ST_STALLED;
1038 case RH_GET_DESCRIPTOR | RH_CLASS:
1040 __u32 temp = roothub_a (&gohci);
1042 data_buf [0] = 9; /* min length; */
1043 data_buf [1] = 0x29;
1044 data_buf [2] = temp & RH_A_NDP;
1046 if (temp & RH_A_PSM) /* per-port power switching? */
1047 data_buf [3] |= 0x1;
1048 if (temp & RH_A_NOCP) /* no overcurrent reporting? */
1049 data_buf [3] |= 0x10;
1050 else if (temp & RH_A_OCPM) /* per-port overcurrent reporting? */
1051 data_buf [3] |= 0x8;
1053 /* corresponds to data_buf[4-7] */
1055 data_buf [5] = (temp & RH_A_POTPGT) >> 24;
1056 temp = roothub_b (&gohci);
1057 data_buf [7] = temp & RH_B_DR;
1058 if (data_buf [2] < 7) {
1059 data_buf [8] = 0xff;
1062 data_buf [8] = (temp & RH_B_DR) >> 8;
1063 data_buf [10] = data_buf [9] = 0xff;
1066 len = min_t(unsigned int, leni,
1067 min_t(unsigned int, data_buf [0], wLength));
1071 case RH_GET_CONFIGURATION: *(__u8 *) data_buf = 0x01; OK (1);
1073 case RH_SET_CONFIGURATION: WR_RH_STAT (0x10000); OK (0);
1076 dbg ("unsupported root hub command");
1077 stat = USB_ST_STALLED;
1081 ohci_dump_roothub (&gohci, 1);
1084 len = min_t(int, len, leni);
1085 if (data != data_buf)
1086 memcpy (data, data_buf, len);
1092 urb_priv.actual_length = transfer_len;
1093 pkt_print(dev, pipe, buffer, transfer_len, cmd, "RET(rh)", 0/*usb_pipein(pipe)*/);
1099 /*-------------------------------------------------------------------------*/
1101 /* common code for handling submit messages - used for all but root hub */
1103 int submit_common_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1104 int transfer_len, struct devrequest *setup, int interval)
1107 int maxsize = usb_maxpacket(dev, pipe);
1110 /* device pulled? Shortcut the action. */
1111 if (devgone == dev) {
1112 dev->status = USB_ST_CRC_ERR;
1117 urb_priv.actual_length = 0;
1118 pkt_print(dev, pipe, buffer, transfer_len, setup, "SUB", usb_pipein(pipe));
1121 err("submit_common_message: pipesize for pipe %lx is zero",
1126 if (sohci_submit_job(dev, pipe, buffer, transfer_len, setup, interval) < 0) {
1127 err("sohci_submit_job failed");
1131 /* allow more time for a BULK device to react - some are slow */
1132 #define BULK_TO 5000 /* timeout in milliseconds */
1133 if (usb_pipebulk(pipe))
1138 /* wait for it to complete */
1140 /* check whether the controller is done */
1141 stat = hc_interrupt();
1143 stat = USB_ST_CRC_ERR;
1147 /* NOTE: since we are not interrupt driven in U-Boot and always
1148 * handle only one URB at a time, we cannot assume the
1149 * transaction finished on the first successful return from
1150 * hc_interrupt().. unless the flag for current URB is set,
1151 * meaning that all TD's to/from device got actually
1152 * transferred and processed. If the current URB is not
1153 * finished we need to re-iterate this loop so as
1154 * hc_interrupt() gets called again as there needs to be some
1155 * more TD's to process still */
1156 if ((stat >= 0) && (stat != 0xff) && (urb_finished)) {
1157 /* 0xff is returned for an SF-interrupt */
1167 err("CTL:TIMEOUT ");
1168 dbg("submit_common_msg: TO status %x\n", stat);
1169 stat = USB_ST_CRC_ERR;
1175 /* we got an Root Hub Status Change interrupt */
1178 ohci_dump_roothub (&gohci, 1);
1182 timeout = rh_check_port_status(&gohci);
1184 #if 0 /* this does nothing useful, but leave it here in case that changes */
1185 /* the called routine adds 1 to the passed value */
1186 usb_hub_port_connect_change(gohci.rh.dev, timeout - 1);
1190 * This is potentially dangerous because it assumes
1191 * that only one device is ever plugged in!
1199 dev->act_len = transfer_len;
1202 pkt_print(dev, pipe, buffer, transfer_len, setup, "RET(ctlr)", usb_pipein(pipe));
1205 /* free TDs in urb_priv */
1206 urb_free_priv (&urb_priv);
1210 /* submit routines called from usb.c */
1211 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1214 info("submit_bulk_msg");
1215 return submit_common_msg(dev, pipe, buffer, transfer_len, NULL, 0);
1218 int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1219 int transfer_len, struct devrequest *setup)
1221 int maxsize = usb_maxpacket(dev, pipe);
1223 info("submit_control_msg");
1225 urb_priv.actual_length = 0;
1226 pkt_print(dev, pipe, buffer, transfer_len, setup, "SUB", usb_pipein(pipe));
1229 err("submit_control_message: pipesize for pipe %lx is zero",
1233 if (((pipe >> 8) & 0x7f) == gohci.rh.devnum) {
1235 /* root hub - redirect */
1236 return ohci_submit_rh_msg(dev, pipe, buffer, transfer_len,
1240 return submit_common_msg(dev, pipe, buffer, transfer_len, setup, 0);
1243 int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1244 int transfer_len, int interval)
1246 info("submit_int_msg");
1250 /*-------------------------------------------------------------------------*
1252 *-------------------------------------------------------------------------*/
1254 /* reset the HC and BUS */
1256 static int hc_reset (ohci_t *ohci)
1259 int smm_timeout = 50; /* 0,5 sec */
1261 if (readl (&ohci->regs->control) & OHCI_CTRL_IR) { /* SMM owns the HC */
1262 writel (OHCI_OCR, &ohci->regs->cmdstatus); /* request ownership */
1263 info("USB HC TakeOver from SMM");
1264 while (readl (&ohci->regs->control) & OHCI_CTRL_IR) {
1266 if (--smm_timeout == 0) {
1267 err("USB HC TakeOver failed!");
1273 /* Disable HC interrupts */
1274 writel (OHCI_INTR_MIE, &ohci->regs->intrdisable);
1276 dbg("USB HC reset_hc usb-%s: ctrl = 0x%X ;",
1278 readl (&ohci->regs->control));
1280 /* Reset USB (needed by some controllers) */
1281 ohci->hc_control = 0;
1282 writel (ohci->hc_control, &ohci->regs->control);
1284 /* HC Reset requires max 10 us delay */
1285 writel (OHCI_HCR, &ohci->regs->cmdstatus);
1286 while ((readl (&ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
1287 if (--timeout == 0) {
1288 err("USB HC reset timed out!");
1296 /*-------------------------------------------------------------------------*/
1298 /* Start an OHCI controller, set the BUS operational
1300 * connect the virtual root hub */
1302 static int hc_start (ohci_t * ohci)
1305 unsigned int fminterval;
1309 /* Tell the controller where the control and bulk lists are
1310 * The lists are empty now. */
1312 writel (0, &ohci->regs->ed_controlhead);
1313 writel (0, &ohci->regs->ed_bulkhead);
1315 writel ((__u32)ohci->hcca, &ohci->regs->hcca); /* a reset clears this */
1317 fminterval = 0x2edf;
1318 writel ((fminterval * 9) / 10, &ohci->regs->periodicstart);
1319 fminterval |= ((((fminterval - 210) * 6) / 7) << 16);
1320 writel (fminterval, &ohci->regs->fminterval);
1321 writel (0x628, &ohci->regs->lsthresh);
1323 /* start controller operations */
1324 ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
1326 writel (ohci->hc_control, &ohci->regs->control);
1328 /* disable all interrupts */
1329 mask = (OHCI_INTR_SO | OHCI_INTR_WDH | OHCI_INTR_SF | OHCI_INTR_RD |
1330 OHCI_INTR_UE | OHCI_INTR_FNO | OHCI_INTR_RHSC |
1331 OHCI_INTR_OC | OHCI_INTR_MIE);
1332 writel (mask, &ohci->regs->intrdisable);
1333 /* clear all interrupts */
1334 mask &= ~OHCI_INTR_MIE;
1335 writel (mask, &ohci->regs->intrstatus);
1336 /* Choose the interrupts we care about now - but w/o MIE */
1337 mask = OHCI_INTR_RHSC | OHCI_INTR_UE | OHCI_INTR_WDH | OHCI_INTR_SO;
1338 writel (mask, &ohci->regs->intrenable);
1341 /* required for AMD-756 and some Mac platforms */
1342 writel ((roothub_a (ohci) | RH_A_NPS) & ~RH_A_PSM,
1343 &ohci->regs->roothub.a);
1344 writel (RH_HS_LPSC, &ohci->regs->roothub.status);
1345 #endif /* OHCI_USE_NPS */
1347 /* POTPGT delay is bits 24-31, in 2 ms units. */
1348 mdelay ((roothub_a (ohci) >> 23) & 0x1fe);
1350 /* connect the virtual root hub */
1351 ohci->rh.devnum = 0;
1356 /*-------------------------------------------------------------------------*/
1358 /* an interrupt happens */
1363 ohci_t *ohci = &gohci;
1364 struct ohci_regs *regs = ohci->regs;
1368 if ((ohci->hcca->done_head != 0) &&
1369 !(ohci_cpu_to_le32(ohci->hcca->done_head) & 0x01)) {
1371 ints = OHCI_INTR_WDH;
1373 } else if ((ints = readl (®s->intrstatus)) == ~(u32)0) {
1375 err ("%s device removed!", ohci->slot_name);
1378 } else if ((ints &= readl (®s->intrenable)) == 0) {
1379 dbg("hc_interrupt: returning..\n");
1383 /* dbg("Interrupt: %x frame: %x", ints, le16_to_cpu (ohci->hcca->frame_no)); */
1385 if (ints & OHCI_INTR_RHSC) {
1390 if (ints & OHCI_INTR_UE) {
1392 err ("OHCI Unrecoverable Error, controller usb-%s disabled",
1394 /* e.g. due to PCI Master/Target Abort */
1397 ohci_dump (ohci, 1);
1399 /* FIXME: be optimistic, hope that bug won't repeat often. */
1400 /* Make some non-interrupt context restart the controller. */
1401 /* Count and limit the retries though; either hardware or */
1402 /* software errors can go forever... */
1407 if (ints & OHCI_INTR_WDH) {
1408 writel (OHCI_INTR_WDH, ®s->intrdisable);
1409 stat = dl_done_list (&gohci, dl_reverse_done_list (&gohci));
1410 writel (OHCI_INTR_WDH, ®s->intrenable);
1413 if (ints & OHCI_INTR_SO) {
1414 dbg("USB Schedule overrun\n");
1415 writel (OHCI_INTR_SO, ®s->intrenable);
1419 /* FIXME: this assumes SOF (1/ms) interrupts don't get lost... */
1420 if (ints & OHCI_INTR_SF) {
1421 unsigned int frame = ohci_cpu_to_le16 (ohci->hcca->frame_no) & 1;
1423 writel (OHCI_INTR_SF, ®s->intrdisable);
1424 if (ohci->ed_rm_list[frame] != NULL)
1425 writel (OHCI_INTR_SF, ®s->intrenable);
1429 writel (ints, ®s->intrstatus);
1433 /*-------------------------------------------------------------------------*/
1435 /*-------------------------------------------------------------------------*/
1437 /* De-allocate all resources.. */
1439 static void hc_release_ohci (ohci_t *ohci)
1441 dbg ("USB HC release ohci usb-%s", ohci->slot_name);
1443 if (!ohci->disabled)
1447 /*-------------------------------------------------------------------------*/
1450 * low level initalisation routine, called from usb.c
1452 static char ohci_inited = 0;
1454 int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
1456 memset (&gohci, 0, sizeof (ohci_t));
1457 memset (&urb_priv, 0, sizeof (urb_priv_t));
1459 /* align the storage */
1460 if ((__u32)&ghcca[0] & 0xff) {
1461 err("HCCA not aligned!!");
1465 info("aligned ghcca %p", phcca);
1466 memset(&ohci_dev, 0, sizeof(struct ohci_device));
1467 if ((__u32)&ohci_dev.ed[0] & 0x7) {
1468 err("EDs not aligned!!");
1471 memset(gtd, 0, sizeof(td_t) * (NUM_TD + 1));
1472 if ((__u32)gtd & 0x7) {
1473 err("TDs not aligned!!");
1478 memset (phcca, 0, sizeof (struct ohci_hcca));
1483 #if defined(CONFIG_440EP)
1484 gohci.regs = (struct ohci_regs *)(CONFIG_SYS_PERIPHERAL_BASE | 0x1000);
1485 #elif defined(CONFIG_440EPX) || defined(CONFIG_SYS_USB_HOST)
1486 gohci.regs = (struct ohci_regs *)(CONFIG_SYS_USB_HOST);
1490 gohci.slot_name = "ppc440";
1492 if (hc_reset (&gohci) < 0) {
1493 hc_release_ohci (&gohci);
1497 if (hc_start (&gohci) < 0) {
1498 err ("can't start usb-%s", gohci.slot_name);
1499 hc_release_ohci (&gohci);
1504 ohci_dump (&gohci, 1);
1512 int usb_lowlevel_stop(int index)
1514 /* this gets called really early - before the controller has */
1515 /* even been initialized! */
1518 /* TODO release any interrupts, etc. */
1519 /* call hc_release_ohci() here ? */
1524 #endif /* CONFIG_USB_OHCI */