2 * URB OHCI HCD (Host Controller Driver) for USB on the MPC5200.
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
32 #define OHCI_USE_NPS /* force NoPowerSwitching mode */
33 #undef OHCI_VERBOSE_DEBUG /* not always helpful */
36 #undef OHCI_FILL_TRACE
38 /* For initializing controller (mask in an HCFS mode too) */
39 #define OHCI_CONTROL_INIT \
40 (OHCI_CTRL_CBSR & 0x3) | OHCI_CTRL_IE | OHCI_CTRL_PLE
42 #define readl(a) (*((volatile u32 *)(a)))
43 #define writel(a, b) (*((volatile u32 *)(b)) = ((volatile u32)a))
46 #define dbg(format, arg...) printf("DEBUG: " format "\n", ## arg)
48 #define dbg(format, arg...) do {} while(0)
50 #define err(format, arg...) printf("ERROR: " format "\n", ## arg)
52 #define info(format, arg...) printf("INFO: " format "\n", ## arg)
54 #define info(format, arg...) do {} while(0)
57 #define m16_swap(x) swap_16(x)
58 #define m32_swap(x) swap_32(x)
60 #define ohci_cpu_to_le16(x) (x)
61 #define ohci_cpu_to_le32(x) (x)
65 /* this must be aligned to a 256 byte boundary */
66 struct ohci_hcca ghcca[1];
67 /* a pointer to the aligned storage */
68 struct ohci_hcca *phcca;
69 /* this allocates EDs for all possible endpoints */
70 struct ohci_device ohci_dev;
75 /* device which was disconnected */
76 struct usb_device *devgone;
77 /* flag guarding URB transation */
80 /*-------------------------------------------------------------------------*/
82 /* AMD-756 (D2 rev) reports corrupt register contents in some cases.
83 * The erratum (#4) description is incorrect. AMD's workaround waits
84 * till some bits (mostly reserved) are clear; ok for all revs.
86 #define OHCI_QUIRK_AMD756 0xabcd
87 #define read_roothub(hc, register, mask) ({ \
88 u32 temp = readl (&hc->regs->roothub.register); \
89 if (hc->flags & OHCI_QUIRK_AMD756) \
91 temp = readl (&hc->regs->roothub.register); \
94 static u32 roothub_a (struct ohci *hc)
95 { return read_roothub (hc, a, 0xfc0fe000); }
96 static inline u32 roothub_b (struct ohci *hc)
97 { return readl (&hc->regs->roothub.b); }
98 static inline u32 roothub_status (struct ohci *hc)
99 { return readl (&hc->regs->roothub.status); }
100 static u32 roothub_portstatus (struct ohci *hc, int i)
101 { return read_roothub (hc, portstatus [i], 0xffe0fce0); }
104 /* forward declaration */
105 static int hc_interrupt (void);
107 td_submit_job (struct usb_device * dev, unsigned long pipe, void * buffer,
108 int transfer_len, struct devrequest * setup, urb_priv_t * urb, int interval);
110 /*-------------------------------------------------------------------------*
111 * URB support functions
112 *-------------------------------------------------------------------------*/
114 /* free HCD-private data associated with this URB */
116 static void urb_free_priv (urb_priv_t * urb)
122 last = urb->length - 1;
124 for (i = 0; i <= last; i++) {
134 /*-------------------------------------------------------------------------*/
137 static int sohci_get_current_frame_number (struct usb_device * dev);
139 /* debug| print the main components of an URB
140 * small: 0) header + data packets 1) just header */
142 static void pkt_print (struct usb_device * dev, unsigned long pipe, void * buffer,
143 int transfer_len, struct devrequest * setup, char * str, int small)
145 urb_priv_t * purb = &urb_priv;
147 dbg("%s URB:[%4x] dev:%2d,ep:%2d-%c,type:%s,len:%d/%d stat:%#lx",
149 sohci_get_current_frame_number (dev),
150 usb_pipedevice (pipe),
151 usb_pipeendpoint (pipe),
152 usb_pipeout (pipe)? 'O': 'I',
153 usb_pipetype (pipe) < 2? (usb_pipeint (pipe)? "INTR": "ISOC"):
154 (usb_pipecontrol (pipe)? "CTRL": "BULK"),
156 transfer_len, dev->status);
157 #ifdef OHCI_VERBOSE_DEBUG
161 if (usb_pipecontrol (pipe)) {
162 printf (__FILE__ ": cmd(8):");
163 for (i = 0; i < 8 ; i++)
164 printf (" %02x", ((__u8 *) setup) [i]);
167 if (transfer_len > 0 && buffer) {
168 printf (__FILE__ ": data(%d/%d):",
171 len = usb_pipeout (pipe)?
172 transfer_len: purb->actual_length;
173 for (i = 0; i < 16 && i < len; i++)
174 printf (" %02x", ((__u8 *) buffer) [i]);
175 printf ("%s\n", i < len? "...": "");
181 /* just for debugging; prints non-empty branches of the int ed tree inclusive iso eds*/
182 void ep_print_int_eds (ohci_t *ohci, char * str) {
185 for (i= 0; i < 32; i++) {
187 ed_p = &(ohci->hcca->int_table [i]);
190 printf (__FILE__ ": %s branch int %2d(%2x):", str, i, i);
191 while (*ed_p != 0 && j--) {
192 ed_t *ed = (ed_t *)ohci_cpu_to_le32(ed_p);
193 printf (" ed: %4x;", ed->hwINFO);
194 ed_p = &ed->hwNextED;
200 static void ohci_dump_intr_mask (char *label, __u32 mask)
202 dbg ("%s: 0x%08x%s%s%s%s%s%s%s%s%s",
205 (mask & OHCI_INTR_MIE) ? " MIE" : "",
206 (mask & OHCI_INTR_OC) ? " OC" : "",
207 (mask & OHCI_INTR_RHSC) ? " RHSC" : "",
208 (mask & OHCI_INTR_FNO) ? " FNO" : "",
209 (mask & OHCI_INTR_UE) ? " UE" : "",
210 (mask & OHCI_INTR_RD) ? " RD" : "",
211 (mask & OHCI_INTR_SF) ? " SF" : "",
212 (mask & OHCI_INTR_WDH) ? " WDH" : "",
213 (mask & OHCI_INTR_SO) ? " SO" : ""
217 static void maybe_print_eds (char *label, __u32 value)
219 ed_t *edp = (ed_t *)value;
222 dbg ("%s %08x", label, value);
223 dbg ("%08x", edp->hwINFO);
224 dbg ("%08x", edp->hwTailP);
225 dbg ("%08x", edp->hwHeadP);
226 dbg ("%08x", edp->hwNextED);
230 static char * hcfs2string (int state)
233 case OHCI_USB_RESET: return "reset";
234 case OHCI_USB_RESUME: return "resume";
235 case OHCI_USB_OPER: return "operational";
236 case OHCI_USB_SUSPEND: return "suspend";
241 /* dump control and status registers */
242 static void ohci_dump_status (ohci_t *controller)
244 struct ohci_regs *regs = controller->regs;
247 temp = readl (®s->revision) & 0xff;
249 dbg ("spec %d.%d", (temp >> 4), (temp & 0x0f));
251 temp = readl (®s->control);
252 dbg ("control: 0x%08x%s%s%s HCFS=%s%s%s%s%s CBSR=%d", temp,
253 (temp & OHCI_CTRL_RWE) ? " RWE" : "",
254 (temp & OHCI_CTRL_RWC) ? " RWC" : "",
255 (temp & OHCI_CTRL_IR) ? " IR" : "",
256 hcfs2string (temp & OHCI_CTRL_HCFS),
257 (temp & OHCI_CTRL_BLE) ? " BLE" : "",
258 (temp & OHCI_CTRL_CLE) ? " CLE" : "",
259 (temp & OHCI_CTRL_IE) ? " IE" : "",
260 (temp & OHCI_CTRL_PLE) ? " PLE" : "",
261 temp & OHCI_CTRL_CBSR
264 temp = readl (®s->cmdstatus);
265 dbg ("cmdstatus: 0x%08x SOC=%d%s%s%s%s", temp,
266 (temp & OHCI_SOC) >> 16,
267 (temp & OHCI_OCR) ? " OCR" : "",
268 (temp & OHCI_BLF) ? " BLF" : "",
269 (temp & OHCI_CLF) ? " CLF" : "",
270 (temp & OHCI_HCR) ? " HCR" : ""
273 ohci_dump_intr_mask ("intrstatus", readl (®s->intrstatus));
274 ohci_dump_intr_mask ("intrenable", readl (®s->intrenable));
276 maybe_print_eds ("ed_periodcurrent", readl (®s->ed_periodcurrent));
278 maybe_print_eds ("ed_controlhead", readl (®s->ed_controlhead));
279 maybe_print_eds ("ed_controlcurrent", readl (®s->ed_controlcurrent));
281 maybe_print_eds ("ed_bulkhead", readl (®s->ed_bulkhead));
282 maybe_print_eds ("ed_bulkcurrent", readl (®s->ed_bulkcurrent));
284 maybe_print_eds ("donehead", readl (®s->donehead));
287 static void ohci_dump_roothub (ohci_t *controller, int verbose)
291 temp = roothub_a (controller);
292 ndp = (temp & RH_A_NDP);
295 dbg ("roothub.a: %08x POTPGT=%d%s%s%s%s%s NDP=%d", temp,
296 ((temp & RH_A_POTPGT) >> 24) & 0xff,
297 (temp & RH_A_NOCP) ? " NOCP" : "",
298 (temp & RH_A_OCPM) ? " OCPM" : "",
299 (temp & RH_A_DT) ? " DT" : "",
300 (temp & RH_A_NPS) ? " NPS" : "",
301 (temp & RH_A_PSM) ? " PSM" : "",
304 temp = roothub_b (controller);
305 dbg ("roothub.b: %08x PPCM=%04x DR=%04x",
307 (temp & RH_B_PPCM) >> 16,
310 temp = roothub_status (controller);
311 dbg ("roothub.status: %08x%s%s%s%s%s%s",
313 (temp & RH_HS_CRWE) ? " CRWE" : "",
314 (temp & RH_HS_OCIC) ? " OCIC" : "",
315 (temp & RH_HS_LPSC) ? " LPSC" : "",
316 (temp & RH_HS_DRWE) ? " DRWE" : "",
317 (temp & RH_HS_OCI) ? " OCI" : "",
318 (temp & RH_HS_LPS) ? " LPS" : ""
322 for (i = 0; i < ndp; i++) {
323 temp = roothub_portstatus (controller, i);
324 dbg ("roothub.portstatus [%d] = 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s",
327 (temp & RH_PS_PRSC) ? " PRSC" : "",
328 (temp & RH_PS_OCIC) ? " OCIC" : "",
329 (temp & RH_PS_PSSC) ? " PSSC" : "",
330 (temp & RH_PS_PESC) ? " PESC" : "",
331 (temp & RH_PS_CSC) ? " CSC" : "",
333 (temp & RH_PS_LSDA) ? " LSDA" : "",
334 (temp & RH_PS_PPS) ? " PPS" : "",
335 (temp & RH_PS_PRS) ? " PRS" : "",
336 (temp & RH_PS_POCI) ? " POCI" : "",
337 (temp & RH_PS_PSS) ? " PSS" : "",
339 (temp & RH_PS_PES) ? " PES" : "",
340 (temp & RH_PS_CCS) ? " CCS" : ""
345 static void ohci_dump (ohci_t *controller, int verbose)
347 dbg ("OHCI controller usb-%s state", controller->slot_name);
349 /* dumps some of the state we know about */
350 ohci_dump_status (controller);
352 ep_print_int_eds (controller, "hcca");
353 dbg ("hcca frame #%04x", controller->hcca->frame_no);
354 ohci_dump_roothub (controller, 1);
360 /*-------------------------------------------------------------------------*
361 * Interface functions (URB)
362 *-------------------------------------------------------------------------*/
364 /* get a transfer request */
366 int sohci_submit_job(struct usb_device *dev, unsigned long pipe, void *buffer,
367 int transfer_len, struct devrequest *setup, int interval)
371 urb_priv_t *purb_priv;
376 /* when controller's hung, permit only roothub cleanup attempts
377 * such as powering down ports */
378 if (ohci->disabled) {
379 err("sohci_submit_job: EPIPE");
383 /* if we have an unfinished URB from previous transaction let's
384 * fail and scream as quickly as possible so as not to corrupt
385 * further communication */
387 err("sohci_submit_job: URB NOT FINISHED");
390 /* we're about to begin a new transaction here so mark the URB unfinished */
393 /* every endpoint has a ed, locate and fill it */
394 if (!(ed = ep_add_ed (dev, pipe))) {
395 err("sohci_submit_job: ENOMEM");
399 /* for the private part of the URB we need the number of TDs (size) */
400 switch (usb_pipetype (pipe)) {
401 case PIPE_BULK: /* one TD for every 4096 Byte */
402 size = (transfer_len - 1) / 4096 + 1;
404 case PIPE_CONTROL: /* 1 TD for setup, 1 for ACK and 1 for every 4096 B */
405 size = (transfer_len == 0)? 2:
406 (transfer_len - 1) / 4096 + 3;
410 if (size >= (N_URB_TD - 1)) {
411 err("need %d TDs, only have %d", size, N_URB_TD);
414 purb_priv = &urb_priv;
415 purb_priv->pipe = pipe;
417 /* fill the private part of the URB */
418 purb_priv->length = size;
420 purb_priv->actual_length = 0;
422 /* allocate the TDs */
423 /* note that td[0] was allocated in ep_add_ed */
424 for (i = 0; i < size; i++) {
425 purb_priv->td[i] = td_alloc (dev);
426 if (!purb_priv->td[i]) {
427 purb_priv->length = i;
428 urb_free_priv (purb_priv);
429 err("sohci_submit_job: ENOMEM");
434 if (ed->state == ED_NEW || (ed->state & ED_DEL)) {
435 urb_free_priv (purb_priv);
436 err("sohci_submit_job: EINVAL");
440 /* link the ed into a chain if is not already */
441 if (ed->state != ED_OPER)
444 /* fill the TDs and link it to the ed */
445 td_submit_job(dev, pipe, buffer, transfer_len, setup, purb_priv, interval);
450 /*-------------------------------------------------------------------------*/
453 /* tell us the current USB frame number */
455 static int sohci_get_current_frame_number (struct usb_device *usb_dev)
457 ohci_t *ohci = &gohci;
459 return ohci_cpu_to_le16 (ohci->hcca->frame_no);
463 /*-------------------------------------------------------------------------*
464 * ED handling functions
465 *-------------------------------------------------------------------------*/
467 /* link an ed into one of the HC chains */
469 static int ep_link (ohci_t *ohci, ed_t *edi)
471 volatile ed_t *ed = edi;
478 if (ohci->ed_controltail == NULL) {
479 writel (ed, &ohci->regs->ed_controlhead);
481 ohci->ed_controltail->hwNextED = ohci_cpu_to_le32 ((unsigned long)ed);
483 ed->ed_prev = ohci->ed_controltail;
484 if (!ohci->ed_controltail && !ohci->ed_rm_list[0] &&
485 !ohci->ed_rm_list[1] && !ohci->sleeping) {
486 ohci->hc_control |= OHCI_CTRL_CLE;
487 writel (ohci->hc_control, &ohci->regs->control);
489 ohci->ed_controltail = edi;
494 if (ohci->ed_bulktail == NULL) {
495 writel (ed, &ohci->regs->ed_bulkhead);
497 ohci->ed_bulktail->hwNextED = ohci_cpu_to_le32 ((unsigned long)ed);
499 ed->ed_prev = ohci->ed_bulktail;
500 if (!ohci->ed_bulktail && !ohci->ed_rm_list[0] &&
501 !ohci->ed_rm_list[1] && !ohci->sleeping) {
502 ohci->hc_control |= OHCI_CTRL_BLE;
503 writel (ohci->hc_control, &ohci->regs->control);
505 ohci->ed_bulktail = edi;
511 /*-------------------------------------------------------------------------*/
513 /* unlink an ed from one of the HC chains.
514 * just the link to the ed is unlinked.
515 * the link from the ed still points to another operational ed or 0
516 * so the HC can eventually finish the processing of the unlinked ed */
518 static int ep_unlink (ohci_t *ohci, ed_t *edi)
520 volatile ed_t *ed = edi;
522 ed->hwINFO |= ohci_cpu_to_le32 (OHCI_ED_SKIP);
526 if (ed->ed_prev == NULL) {
528 ohci->hc_control &= ~OHCI_CTRL_CLE;
529 writel (ohci->hc_control, &ohci->regs->control);
531 writel (ohci_cpu_to_le32 (*((__u32 *)&ed->hwNextED)), &ohci->regs->ed_controlhead);
533 ed->ed_prev->hwNextED = ed->hwNextED;
535 if (ohci->ed_controltail == ed) {
536 ohci->ed_controltail = ed->ed_prev;
538 ((ed_t *)ohci_cpu_to_le32 (*((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
543 if (ed->ed_prev == NULL) {
545 ohci->hc_control &= ~OHCI_CTRL_BLE;
546 writel (ohci->hc_control, &ohci->regs->control);
548 writel (ohci_cpu_to_le32 (*((__u32 *)&ed->hwNextED)), &ohci->regs->ed_bulkhead);
550 ed->ed_prev->hwNextED = ed->hwNextED;
552 if (ohci->ed_bulktail == ed) {
553 ohci->ed_bulktail = ed->ed_prev;
555 ((ed_t *)ohci_cpu_to_le32 (*((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
559 ed->state = ED_UNLINK;
564 /*-------------------------------------------------------------------------*/
566 /* add/reinit an endpoint; this should be done once at the usb_set_configuration command,
567 * but the USB stack is a little bit stateless so we do it at every transaction
568 * if the state of the ed is ED_NEW then a dummy td is added and the state is changed to ED_UNLINK
569 * in all other cases the state is left unchanged
570 * the ed info fields are setted anyway even though most of them should not change */
572 static ed_t * ep_add_ed (struct usb_device *usb_dev, unsigned long pipe)
578 ed = ed_ret = &ohci_dev.ed[(usb_pipeendpoint (pipe) << 1) |
579 (usb_pipecontrol (pipe)? 0: usb_pipeout (pipe))];
581 if ((ed->state & ED_DEL) || (ed->state & ED_URB_DEL)) {
582 err("ep_add_ed: pending delete");
583 /* pending delete request */
587 if (ed->state == ED_NEW) {
588 ed->hwINFO = ohci_cpu_to_le32 (OHCI_ED_SKIP); /* skip ed */
589 /* dummy td; end of td list for ed */
590 td = td_alloc (usb_dev);
591 ed->hwTailP = ohci_cpu_to_le32 ((unsigned long)td);
592 ed->hwHeadP = ed->hwTailP;
593 ed->state = ED_UNLINK;
594 ed->type = usb_pipetype (pipe);
598 ed->hwINFO = ohci_cpu_to_le32 (usb_pipedevice (pipe)
599 | usb_pipeendpoint (pipe) << 7
600 | (usb_pipeisoc (pipe)? 0x8000: 0)
601 | (usb_pipecontrol (pipe)? 0: (usb_pipeout (pipe)? 0x800: 0x1000))
602 | (usb_dev->speed == USB_SPEED_LOW) << 13
603 | usb_maxpacket (usb_dev, pipe) << 16);
608 /*-------------------------------------------------------------------------*
609 * TD handling functions
610 *-------------------------------------------------------------------------*/
612 /* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
614 static void td_fill (ohci_t *ohci, unsigned int info,
616 struct usb_device *dev, int index, urb_priv_t *urb_priv)
618 volatile td_t *td, *td_pt;
619 #ifdef OHCI_FILL_TRACE
623 if (index > urb_priv->length) {
624 err("index > length");
627 /* use this td as the next dummy */
628 td_pt = urb_priv->td [index];
631 /* fill the old dummy TD */
632 td = urb_priv->td [index] = (td_t *)(ohci_cpu_to_le32 (urb_priv->ed->hwTailP) & ~0xf);
634 td->ed = urb_priv->ed;
635 td->next_dl_td = NULL;
637 td->data = (__u32)data;
638 #ifdef OHCI_FILL_TRACE
639 if (usb_pipebulk(urb_priv->pipe) && usb_pipeout(urb_priv->pipe)) {
640 for (i = 0; i < len; i++)
641 printf("td->data[%d] %#2x ",i, ((unsigned char *)td->data)[i]);
648 td->hwINFO = ohci_cpu_to_le32 (info);
649 td->hwCBP = ohci_cpu_to_le32 ((unsigned long)data);
651 td->hwBE = ohci_cpu_to_le32 ((unsigned long)(data + len - 1));
654 td->hwNextTD = ohci_cpu_to_le32 ((unsigned long)td_pt);
656 /* append to queue */
657 td->ed->hwTailP = td->hwNextTD;
660 /*-------------------------------------------------------------------------*/
662 /* prepare all TDs of a transfer */
663 static void td_submit_job (struct usb_device *dev, unsigned long pipe, void *buffer,
664 int transfer_len, struct devrequest *setup, urb_priv_t *urb, int interval)
666 ohci_t *ohci = &gohci;
667 int data_len = transfer_len;
671 unsigned int toggle = 0;
673 /* OHCI handles the DATA-toggles itself, we just use the
674 USB-toggle bits for resetting */
675 if(usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe))) {
676 toggle = TD_T_TOGGLE;
679 usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), 1);
687 switch (usb_pipetype (pipe)) {
689 info = usb_pipeout (pipe)?
690 TD_CC | TD_DP_OUT : TD_CC | TD_DP_IN ;
691 while(data_len > 4096) {
692 td_fill (ohci, info | (cnt? TD_T_TOGGLE:toggle), data, 4096, dev, cnt, urb);
693 data += 4096; data_len -= 4096; cnt++;
695 info = usb_pipeout (pipe)?
696 TD_CC | TD_DP_OUT : TD_CC | TD_R | TD_DP_IN ;
697 td_fill (ohci, info | (cnt? TD_T_TOGGLE:toggle), data, data_len, dev, cnt, urb);
701 writel (OHCI_BLF, &ohci->regs->cmdstatus); /* start bulk list */
705 info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
706 td_fill (ohci, info, setup, 8, dev, cnt++, urb);
708 info = usb_pipeout (pipe)?
709 TD_CC | TD_R | TD_DP_OUT | TD_T_DATA1 : TD_CC | TD_R | TD_DP_IN | TD_T_DATA1;
710 /* NOTE: mishandles transfers >8K, some >4K */
711 td_fill (ohci, info, data, data_len, dev, cnt++, urb);
713 info = usb_pipeout (pipe)?
714 TD_CC | TD_DP_IN | TD_T_DATA1: TD_CC | TD_DP_OUT | TD_T_DATA1;
715 td_fill (ohci, info, data, 0, dev, cnt++, urb);
717 writel (OHCI_CLF, &ohci->regs->cmdstatus); /* start Control list */
720 if (urb->length != cnt)
721 dbg("TD LENGTH %d != CNT %d", urb->length, cnt);
724 /*-------------------------------------------------------------------------*
725 * Done List handling functions
726 *-------------------------------------------------------------------------*/
729 /* calculate the transfer length and update the urb */
731 static void dl_transfer_length(td_t * td)
734 urb_priv_t *lurb_priv = &urb_priv;
736 tdBE = ohci_cpu_to_le32 (td->hwBE);
737 tdCBP = ohci_cpu_to_le32 (td->hwCBP);
740 if (!(usb_pipecontrol(lurb_priv->pipe) &&
741 ((td->index == 0) || (td->index == lurb_priv->length - 1)))) {
744 lurb_priv->actual_length += tdBE - td->data + 1;
746 lurb_priv->actual_length += tdCBP - td->data;
751 /*-------------------------------------------------------------------------*/
753 /* replies to the request have to be on a FIFO basis so
754 * we reverse the reversed done-list */
756 static td_t * dl_reverse_done_list (ohci_t *ohci)
760 td_t *td_list = NULL;
761 urb_priv_t *lurb_priv = NULL;
763 td_list_hc = ohci_cpu_to_le32 (ohci->hcca->done_head) & 0xfffffff0;
764 ohci->hcca->done_head = 0;
767 td_list = (td_t *)td_list_hc;
769 if (TD_CC_GET (ohci_cpu_to_le32 (td_list->hwINFO))) {
770 lurb_priv = &urb_priv;
771 dbg(" USB-error/status: %x : %p",
772 TD_CC_GET (ohci_cpu_to_le32 (td_list->hwINFO)), td_list);
773 if (td_list->ed->hwHeadP & ohci_cpu_to_le32 (0x1)) {
774 if (lurb_priv && ((td_list->index + 1) < lurb_priv->length)) {
775 td_list->ed->hwHeadP =
776 (lurb_priv->td[lurb_priv->length - 1]->hwNextTD & ohci_cpu_to_le32 (0xfffffff0)) |
777 (td_list->ed->hwHeadP & ohci_cpu_to_le32 (0x2));
778 lurb_priv->td_cnt += lurb_priv->length - td_list->index - 1;
780 td_list->ed->hwHeadP &= ohci_cpu_to_le32 (0xfffffff2);
782 td_list->hwNextTD = 0;
785 td_list->next_dl_td = td_rev;
787 td_list_hc = ohci_cpu_to_le32 (td_list->hwNextTD) & 0xfffffff0;
792 /*-------------------------------------------------------------------------*/
795 static int dl_done_list (ohci_t *ohci, td_t *td_list)
797 td_t *td_list_next = NULL;
802 urb_priv_t *lurb_priv;
803 __u32 tdINFO, edHeadP, edTailP;
806 td_list_next = td_list->next_dl_td;
808 lurb_priv = &urb_priv;
809 tdINFO = ohci_cpu_to_le32 (td_list->hwINFO);
813 dl_transfer_length(td_list);
815 /* error code of transfer */
816 cc = TD_CC_GET (tdINFO);
817 if (++(lurb_priv->td_cnt) == lurb_priv->length) {
818 if ((ed->state & (ED_OPER | ED_UNLINK))
819 && (lurb_priv->state != URB_DEL)) {
820 dbg("ConditionCode %#x", cc);
821 stat = cc_to_error[cc];
826 if (ed->state != ED_NEW) {
827 edHeadP = ohci_cpu_to_le32 (ed->hwHeadP) & 0xfffffff0;
828 edTailP = ohci_cpu_to_le32 (ed->hwTailP);
830 /* unlink eds if they are not busy */
831 if ((edHeadP == edTailP) && (ed->state == ED_OPER))
832 ep_unlink (ohci, ed);
835 td_list = td_list_next;
840 /*-------------------------------------------------------------------------*
842 *-------------------------------------------------------------------------*/
844 #include <usbroothubdes.h>
846 /* Hub class-specific descriptor is constructed dynamically */
849 /*-------------------------------------------------------------------------*/
851 #define OK(x) len = (x); break
853 #define WR_RH_STAT(x) {info("WR:status %#8x", (x));writel((x), &gohci.regs->roothub.status);}
854 #define WR_RH_PORTSTAT(x) {info("WR:portstatus[%d] %#8x", wIndex-1, (x));writel((x), &gohci.regs->roothub.portstatus[wIndex-1]);}
856 #define WR_RH_STAT(x) writel((x), &gohci.regs->roothub.status)
857 #define WR_RH_PORTSTAT(x) writel((x), &gohci.regs->roothub.portstatus[wIndex-1])
859 #define RD_RH_STAT roothub_status(&gohci)
860 #define RD_RH_PORTSTAT roothub_portstatus(&gohci,wIndex-1)
862 /* request to virtual root hub */
864 int rh_check_port_status(ohci_t *controller)
870 temp = roothub_a (controller);
871 ndp = (temp & RH_A_NDP);
872 for (i = 0; i < ndp; i++) {
873 temp = roothub_portstatus (controller, i);
874 /* check for a device disconnect */
875 if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
876 (RH_PS_PESC | RH_PS_CSC)) &&
877 ((temp & RH_PS_CCS) == 0)) {
885 static int ohci_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
886 void *buffer, int transfer_len, struct devrequest *cmd)
888 void * data = buffer;
889 int leni = transfer_len;
893 __u8 *data_buf = (__u8 *)datab;
900 urb_priv.actual_length = 0;
901 pkt_print(dev, pipe, buffer, transfer_len, cmd, "SUB(rh)", usb_pipein(pipe));
903 if (usb_pipeint(pipe)) {
904 info("Root-Hub submit IRQ: NOT implemented");
908 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
909 wValue = m16_swap (cmd->value);
910 wIndex = m16_swap (cmd->index);
911 wLength = m16_swap (cmd->length);
913 info("Root-Hub: adr: %2x cmd(%1x): %08x %04x %04x %04x",
914 dev->devnum, 8, bmRType_bReq, wValue, wIndex, wLength);
916 switch (bmRType_bReq) {
917 /* Request Destination:
918 without flags: Device,
919 RH_INTERFACE: interface,
920 RH_ENDPOINT: endpoint,
921 RH_CLASS means HUB here,
922 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
926 *(__u16 *) data_buf = m16_swap (1); OK (2);
927 case RH_GET_STATUS | RH_INTERFACE:
928 *(__u16 *) data_buf = m16_swap (0); OK (2);
929 case RH_GET_STATUS | RH_ENDPOINT:
930 *(__u16 *) data_buf = m16_swap (0); OK (2);
931 case RH_GET_STATUS | RH_CLASS:
932 *(__u32 *) data_buf = m32_swap (
933 RD_RH_STAT & ~(RH_HS_CRWE | RH_HS_DRWE));
935 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
936 *(__u32 *) data_buf = m32_swap (RD_RH_PORTSTAT); OK (4);
938 case RH_CLEAR_FEATURE | RH_ENDPOINT:
940 case (RH_ENDPOINT_STALL): OK (0);
944 case RH_CLEAR_FEATURE | RH_CLASS:
946 case RH_C_HUB_LOCAL_POWER:
948 case (RH_C_HUB_OVER_CURRENT):
949 WR_RH_STAT(RH_HS_OCIC); OK (0);
953 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
955 case (RH_PORT_ENABLE):
956 WR_RH_PORTSTAT (RH_PS_CCS ); OK (0);
957 case (RH_PORT_SUSPEND):
958 WR_RH_PORTSTAT (RH_PS_POCI); OK (0);
959 case (RH_PORT_POWER):
960 WR_RH_PORTSTAT (RH_PS_LSDA); OK (0);
961 case (RH_C_PORT_CONNECTION):
962 WR_RH_PORTSTAT (RH_PS_CSC ); OK (0);
963 case (RH_C_PORT_ENABLE):
964 WR_RH_PORTSTAT (RH_PS_PESC); OK (0);
965 case (RH_C_PORT_SUSPEND):
966 WR_RH_PORTSTAT (RH_PS_PSSC); OK (0);
967 case (RH_C_PORT_OVER_CURRENT):
968 WR_RH_PORTSTAT (RH_PS_OCIC); OK (0);
969 case (RH_C_PORT_RESET):
970 WR_RH_PORTSTAT (RH_PS_PRSC); OK (0);
974 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
976 case (RH_PORT_SUSPEND):
977 WR_RH_PORTSTAT (RH_PS_PSS ); OK (0);
978 case (RH_PORT_RESET): /* BUG IN HUP CODE *********/
979 if (RD_RH_PORTSTAT & RH_PS_CCS)
980 WR_RH_PORTSTAT (RH_PS_PRS);
982 case (RH_PORT_POWER):
983 WR_RH_PORTSTAT (RH_PS_PPS ); OK (0);
984 case (RH_PORT_ENABLE): /* BUG IN HUP CODE *********/
985 if (RD_RH_PORTSTAT & RH_PS_CCS)
986 WR_RH_PORTSTAT (RH_PS_PES );
991 case RH_SET_ADDRESS: gohci.rh.devnum = wValue; OK(0);
993 case RH_GET_DESCRIPTOR:
994 switch ((wValue & 0xff00) >> 8) {
995 case (0x01): /* device descriptor */
996 len = min_t(unsigned int,
999 sizeof (root_hub_dev_des),
1001 data_buf = root_hub_dev_des; OK(len);
1002 case (0x02): /* configuration descriptor */
1003 len = min_t(unsigned int,
1006 sizeof (root_hub_config_des),
1008 data_buf = root_hub_config_des; OK(len);
1009 case (0x03): /* string descriptors */
1010 if(wValue==0x0300) {
1011 len = min_t(unsigned int,
1014 sizeof (root_hub_str_index0),
1016 data_buf = root_hub_str_index0;
1019 if(wValue==0x0301) {
1020 len = min_t(unsigned int,
1023 sizeof (root_hub_str_index1),
1025 data_buf = root_hub_str_index1;
1029 stat = USB_ST_STALLED;
1033 case RH_GET_DESCRIPTOR | RH_CLASS:
1035 __u32 temp = roothub_a (&gohci);
1037 data_buf [0] = 9; /* min length; */
1038 data_buf [1] = 0x29;
1039 data_buf [2] = temp & RH_A_NDP;
1041 if (temp & RH_A_PSM) /* per-port power switching? */
1042 data_buf [3] |= 0x1;
1043 if (temp & RH_A_NOCP) /* no overcurrent reporting? */
1044 data_buf [3] |= 0x10;
1045 else if (temp & RH_A_OCPM) /* per-port overcurrent reporting? */
1046 data_buf [3] |= 0x8;
1048 /* corresponds to data_buf[4-7] */
1050 data_buf [5] = (temp & RH_A_POTPGT) >> 24;
1051 temp = roothub_b (&gohci);
1052 data_buf [7] = temp & RH_B_DR;
1053 if (data_buf [2] < 7) {
1054 data_buf [8] = 0xff;
1057 data_buf [8] = (temp & RH_B_DR) >> 8;
1058 data_buf [10] = data_buf [9] = 0xff;
1061 len = min_t(unsigned int, leni,
1062 min_t(unsigned int, data_buf [0], wLength));
1066 case RH_GET_CONFIGURATION: *(__u8 *) data_buf = 0x01; OK (1);
1068 case RH_SET_CONFIGURATION: WR_RH_STAT (0x10000); OK (0);
1071 dbg ("unsupported root hub command");
1072 stat = USB_ST_STALLED;
1076 ohci_dump_roothub (&gohci, 1);
1079 len = min_t(int, len, leni);
1080 if (data != data_buf)
1081 memcpy (data, data_buf, len);
1087 urb_priv.actual_length = transfer_len;
1088 pkt_print(dev, pipe, buffer, transfer_len, cmd, "RET(rh)", 0/*usb_pipein(pipe)*/);
1094 /*-------------------------------------------------------------------------*/
1096 /* common code for handling submit messages - used for all but root hub */
1098 int submit_common_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1099 int transfer_len, struct devrequest *setup, int interval)
1102 int maxsize = usb_maxpacket(dev, pipe);
1105 /* device pulled? Shortcut the action. */
1106 if (devgone == dev) {
1107 dev->status = USB_ST_CRC_ERR;
1112 urb_priv.actual_length = 0;
1113 pkt_print(dev, pipe, buffer, transfer_len, setup, "SUB", usb_pipein(pipe));
1116 err("submit_common_message: pipesize for pipe %lx is zero",
1121 if (sohci_submit_job(dev, pipe, buffer, transfer_len, setup, interval) < 0) {
1122 err("sohci_submit_job failed");
1126 /* allow more time for a BULK device to react - some are slow */
1127 #define BULK_TO 5000 /* timeout in milliseconds */
1128 if (usb_pipebulk(pipe))
1133 /* wait for it to complete */
1135 /* check whether the controller is done */
1136 stat = hc_interrupt();
1138 stat = USB_ST_CRC_ERR;
1142 /* NOTE: since we are not interrupt driven in U-Boot and always
1143 * handle only one URB at a time, we cannot assume the
1144 * transaction finished on the first successful return from
1145 * hc_interrupt().. unless the flag for current URB is set,
1146 * meaning that all TD's to/from device got actually
1147 * transferred and processed. If the current URB is not
1148 * finished we need to re-iterate this loop so as
1149 * hc_interrupt() gets called again as there needs to be some
1150 * more TD's to process still */
1151 if ((stat >= 0) && (stat != 0xff) && (urb_finished)) {
1152 /* 0xff is returned for an SF-interrupt */
1162 err("CTL:TIMEOUT ");
1163 dbg("submit_common_msg: TO status %x\n", stat);
1164 stat = USB_ST_CRC_ERR;
1170 /* we got an Root Hub Status Change interrupt */
1173 ohci_dump_roothub (&gohci, 1);
1177 timeout = rh_check_port_status(&gohci);
1179 #if 0 /* this does nothing useful, but leave it here in case that changes */
1180 /* the called routine adds 1 to the passed value */
1181 usb_hub_port_connect_change(gohci.rh.dev, timeout - 1);
1185 * This is potentially dangerous because it assumes
1186 * that only one device is ever plugged in!
1194 dev->act_len = transfer_len;
1197 pkt_print(dev, pipe, buffer, transfer_len, setup, "RET(ctlr)", usb_pipein(pipe));
1200 /* free TDs in urb_priv */
1201 urb_free_priv (&urb_priv);
1205 /* submit routines called from usb.c */
1206 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1209 info("submit_bulk_msg");
1210 return submit_common_msg(dev, pipe, buffer, transfer_len, NULL, 0);
1213 int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1214 int transfer_len, struct devrequest *setup)
1216 int maxsize = usb_maxpacket(dev, pipe);
1218 info("submit_control_msg");
1220 urb_priv.actual_length = 0;
1221 pkt_print(dev, pipe, buffer, transfer_len, setup, "SUB", usb_pipein(pipe));
1224 err("submit_control_message: pipesize for pipe %lx is zero",
1228 if (((pipe >> 8) & 0x7f) == gohci.rh.devnum) {
1230 /* root hub - redirect */
1231 return ohci_submit_rh_msg(dev, pipe, buffer, transfer_len,
1235 return submit_common_msg(dev, pipe, buffer, transfer_len, setup, 0);
1238 int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1239 int transfer_len, int interval)
1241 info("submit_int_msg");
1245 /*-------------------------------------------------------------------------*
1247 *-------------------------------------------------------------------------*/
1249 /* reset the HC and BUS */
1251 static int hc_reset (ohci_t *ohci)
1254 int smm_timeout = 50; /* 0,5 sec */
1256 if (readl (&ohci->regs->control) & OHCI_CTRL_IR) { /* SMM owns the HC */
1257 writel (OHCI_OCR, &ohci->regs->cmdstatus); /* request ownership */
1258 info("USB HC TakeOver from SMM");
1259 while (readl (&ohci->regs->control) & OHCI_CTRL_IR) {
1261 if (--smm_timeout == 0) {
1262 err("USB HC TakeOver failed!");
1268 /* Disable HC interrupts */
1269 writel (OHCI_INTR_MIE, &ohci->regs->intrdisable);
1271 dbg("USB HC reset_hc usb-%s: ctrl = 0x%X ;",
1273 readl (&ohci->regs->control));
1275 /* Reset USB (needed by some controllers) */
1276 ohci->hc_control = 0;
1277 writel (ohci->hc_control, &ohci->regs->control);
1279 /* HC Reset requires max 10 us delay */
1280 writel (OHCI_HCR, &ohci->regs->cmdstatus);
1281 while ((readl (&ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
1282 if (--timeout == 0) {
1283 err("USB HC reset timed out!");
1291 /*-------------------------------------------------------------------------*/
1293 /* Start an OHCI controller, set the BUS operational
1295 * connect the virtual root hub */
1297 static int hc_start (ohci_t * ohci)
1300 unsigned int fminterval;
1304 /* Tell the controller where the control and bulk lists are
1305 * The lists are empty now. */
1307 writel (0, &ohci->regs->ed_controlhead);
1308 writel (0, &ohci->regs->ed_bulkhead);
1310 writel ((__u32)ohci->hcca, &ohci->regs->hcca); /* a reset clears this */
1312 fminterval = 0x2edf;
1313 writel ((fminterval * 9) / 10, &ohci->regs->periodicstart);
1314 fminterval |= ((((fminterval - 210) * 6) / 7) << 16);
1315 writel (fminterval, &ohci->regs->fminterval);
1316 writel (0x628, &ohci->regs->lsthresh);
1318 /* start controller operations */
1319 ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
1321 writel (ohci->hc_control, &ohci->regs->control);
1323 /* disable all interrupts */
1324 mask = (OHCI_INTR_SO | OHCI_INTR_WDH | OHCI_INTR_SF | OHCI_INTR_RD |
1325 OHCI_INTR_UE | OHCI_INTR_FNO | OHCI_INTR_RHSC |
1326 OHCI_INTR_OC | OHCI_INTR_MIE);
1327 writel (mask, &ohci->regs->intrdisable);
1328 /* clear all interrupts */
1329 mask &= ~OHCI_INTR_MIE;
1330 writel (mask, &ohci->regs->intrstatus);
1331 /* Choose the interrupts we care about now - but w/o MIE */
1332 mask = OHCI_INTR_RHSC | OHCI_INTR_UE | OHCI_INTR_WDH | OHCI_INTR_SO;
1333 writel (mask, &ohci->regs->intrenable);
1336 /* required for AMD-756 and some Mac platforms */
1337 writel ((roothub_a (ohci) | RH_A_NPS) & ~RH_A_PSM,
1338 &ohci->regs->roothub.a);
1339 writel (RH_HS_LPSC, &ohci->regs->roothub.status);
1340 #endif /* OHCI_USE_NPS */
1342 /* POTPGT delay is bits 24-31, in 2 ms units. */
1343 mdelay ((roothub_a (ohci) >> 23) & 0x1fe);
1345 /* connect the virtual root hub */
1346 ohci->rh.devnum = 0;
1351 /*-------------------------------------------------------------------------*/
1353 /* an interrupt happens */
1358 ohci_t *ohci = &gohci;
1359 struct ohci_regs *regs = ohci->regs;
1363 if ((ohci->hcca->done_head != 0) &&
1364 !(ohci_cpu_to_le32(ohci->hcca->done_head) & 0x01)) {
1366 ints = OHCI_INTR_WDH;
1368 } else if ((ints = readl (®s->intrstatus)) == ~(u32)0) {
1370 err ("%s device removed!", ohci->slot_name);
1373 } else if ((ints &= readl (®s->intrenable)) == 0) {
1374 dbg("hc_interrupt: returning..\n");
1378 /* dbg("Interrupt: %x frame: %x", ints, le16_to_cpu (ohci->hcca->frame_no)); */
1380 if (ints & OHCI_INTR_RHSC) {
1385 if (ints & OHCI_INTR_UE) {
1387 err ("OHCI Unrecoverable Error, controller usb-%s disabled",
1389 /* e.g. due to PCI Master/Target Abort */
1392 ohci_dump (ohci, 1);
1394 /* FIXME: be optimistic, hope that bug won't repeat often. */
1395 /* Make some non-interrupt context restart the controller. */
1396 /* Count and limit the retries though; either hardware or */
1397 /* software errors can go forever... */
1402 if (ints & OHCI_INTR_WDH) {
1403 writel (OHCI_INTR_WDH, ®s->intrdisable);
1404 stat = dl_done_list (&gohci, dl_reverse_done_list (&gohci));
1405 writel (OHCI_INTR_WDH, ®s->intrenable);
1408 if (ints & OHCI_INTR_SO) {
1409 dbg("USB Schedule overrun\n");
1410 writel (OHCI_INTR_SO, ®s->intrenable);
1414 /* FIXME: this assumes SOF (1/ms) interrupts don't get lost... */
1415 if (ints & OHCI_INTR_SF) {
1416 unsigned int frame = ohci_cpu_to_le16 (ohci->hcca->frame_no) & 1;
1418 writel (OHCI_INTR_SF, ®s->intrdisable);
1419 if (ohci->ed_rm_list[frame] != NULL)
1420 writel (OHCI_INTR_SF, ®s->intrenable);
1424 writel (ints, ®s->intrstatus);
1428 /*-------------------------------------------------------------------------*/
1430 /*-------------------------------------------------------------------------*/
1432 /* De-allocate all resources.. */
1434 static void hc_release_ohci (ohci_t *ohci)
1436 dbg ("USB HC release ohci usb-%s", ohci->slot_name);
1438 if (!ohci->disabled)
1442 /*-------------------------------------------------------------------------*/
1445 * low level initalisation routine, called from usb.c
1447 static char ohci_inited = 0;
1449 int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
1452 /* Set the USB Clock */
1453 *(vu_long *)MPC5XXX_CDM_48_FDC = CONFIG_USB_CLOCK;
1455 #ifdef CONFIG_PSC3_USB /* USB is using the alternate configuration */
1456 /* remove all PSC3 USB bits first before ORing in ours */
1457 *(vu_long *)MPC5XXX_GPS_PORT_CONFIG &= ~0x00804f00;
1459 /* remove all USB bits first before ORing in ours */
1460 *(vu_long *)MPC5XXX_GPS_PORT_CONFIG &= ~0x00807000;
1462 /* Activate USB port */
1463 *(vu_long *)MPC5XXX_GPS_PORT_CONFIG |= CONFIG_USB_CONFIG;
1465 memset (&gohci, 0, sizeof (ohci_t));
1466 memset (&urb_priv, 0, sizeof (urb_priv_t));
1468 /* align the storage */
1469 if ((__u32)&ghcca[0] & 0xff) {
1470 err("HCCA not aligned!!");
1474 info("aligned ghcca %p", phcca);
1475 memset(&ohci_dev, 0, sizeof(struct ohci_device));
1476 if ((__u32)&ohci_dev.ed[0] & 0x7) {
1477 err("EDs not aligned!!");
1480 memset(gtd, 0, sizeof(td_t) * (NUM_TD + 1));
1481 if ((__u32)gtd & 0x7) {
1482 err("TDs not aligned!!");
1487 memset (phcca, 0, sizeof (struct ohci_hcca));
1492 gohci.regs = (struct ohci_regs *)MPC5XXX_USB;
1495 gohci.slot_name = "mpc5200";
1497 if (hc_reset (&gohci) < 0) {
1498 hc_release_ohci (&gohci);
1502 if (hc_start (&gohci) < 0) {
1503 err ("can't start usb-%s", gohci.slot_name);
1504 hc_release_ohci (&gohci);
1509 ohci_dump (&gohci, 1);
1517 int usb_lowlevel_stop(int index)
1519 /* this gets called really early - before the controller has */
1520 /* even been initialized! */
1523 /* TODO release any interrupts, etc. */
1524 /* call hc_release_ohci() here ? */
1529 #endif /* CONFIG_USB_OHCI */