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 USB-toggle bits for reseting */
674 if(usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe))) {
675 toggle = TD_T_TOGGLE;
678 usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), 1);
686 switch (usb_pipetype (pipe)) {
688 info = usb_pipeout (pipe)?
689 TD_CC | TD_DP_OUT : TD_CC | TD_DP_IN ;
690 while(data_len > 4096) {
691 td_fill (ohci, info | (cnt? TD_T_TOGGLE:toggle), data, 4096, dev, cnt, urb);
692 data += 4096; data_len -= 4096; cnt++;
694 info = usb_pipeout (pipe)?
695 TD_CC | TD_DP_OUT : TD_CC | TD_R | TD_DP_IN ;
696 td_fill (ohci, info | (cnt? TD_T_TOGGLE:toggle), data, data_len, dev, cnt, urb);
700 writel (OHCI_BLF, &ohci->regs->cmdstatus); /* start bulk list */
704 info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
705 td_fill (ohci, info, setup, 8, dev, cnt++, urb);
707 info = usb_pipeout (pipe)?
708 TD_CC | TD_R | TD_DP_OUT | TD_T_DATA1 : TD_CC | TD_R | TD_DP_IN | TD_T_DATA1;
709 /* NOTE: mishandles transfers >8K, some >4K */
710 td_fill (ohci, info, data, data_len, dev, cnt++, urb);
712 info = usb_pipeout (pipe)?
713 TD_CC | TD_DP_IN | TD_T_DATA1: TD_CC | TD_DP_OUT | TD_T_DATA1;
714 td_fill (ohci, info, data, 0, dev, cnt++, urb);
716 writel (OHCI_CLF, &ohci->regs->cmdstatus); /* start Control list */
719 if (urb->length != cnt)
720 dbg("TD LENGTH %d != CNT %d", urb->length, cnt);
723 /*-------------------------------------------------------------------------*
724 * Done List handling functions
725 *-------------------------------------------------------------------------*/
728 /* calculate the transfer length and update the urb */
730 static void dl_transfer_length(td_t * td)
733 urb_priv_t *lurb_priv = &urb_priv;
735 tdBE = ohci_cpu_to_le32 (td->hwBE);
736 tdCBP = ohci_cpu_to_le32 (td->hwCBP);
739 if (!(usb_pipecontrol(lurb_priv->pipe) &&
740 ((td->index == 0) || (td->index == lurb_priv->length - 1)))) {
743 lurb_priv->actual_length += tdBE - td->data + 1;
745 lurb_priv->actual_length += tdCBP - td->data;
750 /*-------------------------------------------------------------------------*/
752 /* replies to the request have to be on a FIFO basis so
753 * we reverse the reversed done-list */
755 static td_t * dl_reverse_done_list (ohci_t *ohci)
759 td_t *td_list = NULL;
760 urb_priv_t *lurb_priv = NULL;
762 td_list_hc = ohci_cpu_to_le32 (ohci->hcca->done_head) & 0xfffffff0;
763 ohci->hcca->done_head = 0;
766 td_list = (td_t *)td_list_hc;
768 if (TD_CC_GET (ohci_cpu_to_le32 (td_list->hwINFO))) {
769 lurb_priv = &urb_priv;
770 dbg(" USB-error/status: %x : %p",
771 TD_CC_GET (ohci_cpu_to_le32 (td_list->hwINFO)), td_list);
772 if (td_list->ed->hwHeadP & ohci_cpu_to_le32 (0x1)) {
773 if (lurb_priv && ((td_list->index + 1) < lurb_priv->length)) {
774 td_list->ed->hwHeadP =
775 (lurb_priv->td[lurb_priv->length - 1]->hwNextTD & ohci_cpu_to_le32 (0xfffffff0)) |
776 (td_list->ed->hwHeadP & ohci_cpu_to_le32 (0x2));
777 lurb_priv->td_cnt += lurb_priv->length - td_list->index - 1;
779 td_list->ed->hwHeadP &= ohci_cpu_to_le32 (0xfffffff2);
781 td_list->hwNextTD = 0;
784 td_list->next_dl_td = td_rev;
786 td_list_hc = ohci_cpu_to_le32 (td_list->hwNextTD) & 0xfffffff0;
791 /*-------------------------------------------------------------------------*/
794 static int dl_done_list (ohci_t *ohci, td_t *td_list)
796 td_t *td_list_next = NULL;
801 urb_priv_t *lurb_priv;
802 __u32 tdINFO, edHeadP, edTailP;
805 td_list_next = td_list->next_dl_td;
807 lurb_priv = &urb_priv;
808 tdINFO = ohci_cpu_to_le32 (td_list->hwINFO);
812 dl_transfer_length(td_list);
814 /* error code of transfer */
815 cc = TD_CC_GET (tdINFO);
816 if (++(lurb_priv->td_cnt) == lurb_priv->length) {
817 if ((ed->state & (ED_OPER | ED_UNLINK))
818 && (lurb_priv->state != URB_DEL)) {
819 dbg("ConditionCode %#x", cc);
820 stat = cc_to_error[cc];
825 if (ed->state != ED_NEW) {
826 edHeadP = ohci_cpu_to_le32 (ed->hwHeadP) & 0xfffffff0;
827 edTailP = ohci_cpu_to_le32 (ed->hwTailP);
829 /* unlink eds if they are not busy */
830 if ((edHeadP == edTailP) && (ed->state == ED_OPER))
831 ep_unlink (ohci, ed);
834 td_list = td_list_next;
839 /*-------------------------------------------------------------------------*
841 *-------------------------------------------------------------------------*/
843 #include <usbroothubdes.h>
845 /* Hub class-specific descriptor is constructed dynamically */
848 /*-------------------------------------------------------------------------*/
850 #define OK(x) len = (x); break
852 #define WR_RH_STAT(x) {info("WR:status %#8x", (x));writel((x), &gohci.regs->roothub.status);}
853 #define WR_RH_PORTSTAT(x) {info("WR:portstatus[%d] %#8x", wIndex-1, (x));writel((x), &gohci.regs->roothub.portstatus[wIndex-1]);}
855 #define WR_RH_STAT(x) writel((x), &gohci.regs->roothub.status)
856 #define WR_RH_PORTSTAT(x) writel((x), &gohci.regs->roothub.portstatus[wIndex-1])
858 #define RD_RH_STAT roothub_status(&gohci)
859 #define RD_RH_PORTSTAT roothub_portstatus(&gohci,wIndex-1)
861 /* request to virtual root hub */
863 int rh_check_port_status(ohci_t *controller)
869 temp = roothub_a (controller);
870 ndp = (temp & RH_A_NDP);
871 for (i = 0; i < ndp; i++) {
872 temp = roothub_portstatus (controller, i);
873 /* check for a device disconnect */
874 if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
875 (RH_PS_PESC | RH_PS_CSC)) &&
876 ((temp & RH_PS_CCS) == 0)) {
884 static int ohci_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
885 void *buffer, int transfer_len, struct devrequest *cmd)
887 void * data = buffer;
888 int leni = transfer_len;
892 __u8 *data_buf = (__u8 *)datab;
899 urb_priv.actual_length = 0;
900 pkt_print(dev, pipe, buffer, transfer_len, cmd, "SUB(rh)", usb_pipein(pipe));
902 if (usb_pipeint(pipe)) {
903 info("Root-Hub submit IRQ: NOT implemented");
907 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
908 wValue = m16_swap (cmd->value);
909 wIndex = m16_swap (cmd->index);
910 wLength = m16_swap (cmd->length);
912 info("Root-Hub: adr: %2x cmd(%1x): %08x %04x %04x %04x",
913 dev->devnum, 8, bmRType_bReq, wValue, wIndex, wLength);
915 switch (bmRType_bReq) {
916 /* Request Destination:
917 without flags: Device,
918 RH_INTERFACE: interface,
919 RH_ENDPOINT: endpoint,
920 RH_CLASS means HUB here,
921 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
925 *(__u16 *) data_buf = m16_swap (1); OK (2);
926 case RH_GET_STATUS | RH_INTERFACE:
927 *(__u16 *) data_buf = m16_swap (0); OK (2);
928 case RH_GET_STATUS | RH_ENDPOINT:
929 *(__u16 *) data_buf = m16_swap (0); OK (2);
930 case RH_GET_STATUS | RH_CLASS:
931 *(__u32 *) data_buf = m32_swap (
932 RD_RH_STAT & ~(RH_HS_CRWE | RH_HS_DRWE));
934 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
935 *(__u32 *) data_buf = m32_swap (RD_RH_PORTSTAT); OK (4);
937 case RH_CLEAR_FEATURE | RH_ENDPOINT:
939 case (RH_ENDPOINT_STALL): OK (0);
943 case RH_CLEAR_FEATURE | RH_CLASS:
945 case RH_C_HUB_LOCAL_POWER:
947 case (RH_C_HUB_OVER_CURRENT):
948 WR_RH_STAT(RH_HS_OCIC); OK (0);
952 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
954 case (RH_PORT_ENABLE):
955 WR_RH_PORTSTAT (RH_PS_CCS ); OK (0);
956 case (RH_PORT_SUSPEND):
957 WR_RH_PORTSTAT (RH_PS_POCI); OK (0);
958 case (RH_PORT_POWER):
959 WR_RH_PORTSTAT (RH_PS_LSDA); OK (0);
960 case (RH_C_PORT_CONNECTION):
961 WR_RH_PORTSTAT (RH_PS_CSC ); OK (0);
962 case (RH_C_PORT_ENABLE):
963 WR_RH_PORTSTAT (RH_PS_PESC); OK (0);
964 case (RH_C_PORT_SUSPEND):
965 WR_RH_PORTSTAT (RH_PS_PSSC); OK (0);
966 case (RH_C_PORT_OVER_CURRENT):
967 WR_RH_PORTSTAT (RH_PS_OCIC); OK (0);
968 case (RH_C_PORT_RESET):
969 WR_RH_PORTSTAT (RH_PS_PRSC); OK (0);
973 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
975 case (RH_PORT_SUSPEND):
976 WR_RH_PORTSTAT (RH_PS_PSS ); OK (0);
977 case (RH_PORT_RESET): /* BUG IN HUP CODE *********/
978 if (RD_RH_PORTSTAT & RH_PS_CCS)
979 WR_RH_PORTSTAT (RH_PS_PRS);
981 case (RH_PORT_POWER):
982 WR_RH_PORTSTAT (RH_PS_PPS ); OK (0);
983 case (RH_PORT_ENABLE): /* BUG IN HUP CODE *********/
984 if (RD_RH_PORTSTAT & RH_PS_CCS)
985 WR_RH_PORTSTAT (RH_PS_PES );
990 case RH_SET_ADDRESS: gohci.rh.devnum = wValue; OK(0);
992 case RH_GET_DESCRIPTOR:
993 switch ((wValue & 0xff00) >> 8) {
994 case (0x01): /* device descriptor */
995 len = min_t(unsigned int,
998 sizeof (root_hub_dev_des),
1000 data_buf = root_hub_dev_des; OK(len);
1001 case (0x02): /* configuration descriptor */
1002 len = min_t(unsigned int,
1005 sizeof (root_hub_config_des),
1007 data_buf = root_hub_config_des; OK(len);
1008 case (0x03): /* string descriptors */
1009 if(wValue==0x0300) {
1010 len = min_t(unsigned int,
1013 sizeof (root_hub_str_index0),
1015 data_buf = root_hub_str_index0;
1018 if(wValue==0x0301) {
1019 len = min_t(unsigned int,
1022 sizeof (root_hub_str_index1),
1024 data_buf = root_hub_str_index1;
1028 stat = USB_ST_STALLED;
1032 case RH_GET_DESCRIPTOR | RH_CLASS:
1034 __u32 temp = roothub_a (&gohci);
1036 data_buf [0] = 9; /* min length; */
1037 data_buf [1] = 0x29;
1038 data_buf [2] = temp & RH_A_NDP;
1040 if (temp & RH_A_PSM) /* per-port power switching? */
1041 data_buf [3] |= 0x1;
1042 if (temp & RH_A_NOCP) /* no overcurrent reporting? */
1043 data_buf [3] |= 0x10;
1044 else if (temp & RH_A_OCPM) /* per-port overcurrent reporting? */
1045 data_buf [3] |= 0x8;
1047 /* corresponds to data_buf[4-7] */
1049 data_buf [5] = (temp & RH_A_POTPGT) >> 24;
1050 temp = roothub_b (&gohci);
1051 data_buf [7] = temp & RH_B_DR;
1052 if (data_buf [2] < 7) {
1053 data_buf [8] = 0xff;
1056 data_buf [8] = (temp & RH_B_DR) >> 8;
1057 data_buf [10] = data_buf [9] = 0xff;
1060 len = min_t(unsigned int, leni,
1061 min_t(unsigned int, data_buf [0], wLength));
1065 case RH_GET_CONFIGURATION: *(__u8 *) data_buf = 0x01; OK (1);
1067 case RH_SET_CONFIGURATION: WR_RH_STAT (0x10000); OK (0);
1070 dbg ("unsupported root hub command");
1071 stat = USB_ST_STALLED;
1075 ohci_dump_roothub (&gohci, 1);
1078 len = min_t(int, len, leni);
1079 if (data != data_buf)
1080 memcpy (data, data_buf, len);
1086 urb_priv.actual_length = transfer_len;
1087 pkt_print(dev, pipe, buffer, transfer_len, cmd, "RET(rh)", 0/*usb_pipein(pipe)*/);
1093 /*-------------------------------------------------------------------------*/
1095 /* common code for handling submit messages - used for all but root hub */
1097 int submit_common_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1098 int transfer_len, struct devrequest *setup, int interval)
1101 int maxsize = usb_maxpacket(dev, pipe);
1104 /* device pulled? Shortcut the action. */
1105 if (devgone == dev) {
1106 dev->status = USB_ST_CRC_ERR;
1111 urb_priv.actual_length = 0;
1112 pkt_print(dev, pipe, buffer, transfer_len, setup, "SUB", usb_pipein(pipe));
1115 err("submit_common_message: pipesize for pipe %lx is zero",
1120 if (sohci_submit_job(dev, pipe, buffer, transfer_len, setup, interval) < 0) {
1121 err("sohci_submit_job failed");
1125 /* allow more time for a BULK device to react - some are slow */
1126 #define BULK_TO 5000 /* timeout in milliseconds */
1127 if (usb_pipebulk(pipe))
1132 /* wait for it to complete */
1134 /* check whether the controller is done */
1135 stat = hc_interrupt();
1137 stat = USB_ST_CRC_ERR;
1141 /* NOTE: since we are not interrupt driven in U-Boot and always
1142 * handle only one URB at a time, we cannot assume the
1143 * transaction finished on the first successful return from
1144 * hc_interrupt().. unless the flag for current URB is set,
1145 * meaning that all TD's to/from device got actually
1146 * transferred and processed. If the current URB is not
1147 * finished we need to re-iterate this loop so as
1148 * hc_interrupt() gets called again as there needs to be some
1149 * more TD's to process still */
1150 if ((stat >= 0) && (stat != 0xff) && (urb_finished)) {
1151 /* 0xff is returned for an SF-interrupt */
1161 err("CTL:TIMEOUT ");
1162 dbg("submit_common_msg: TO status %x\n", stat);
1163 stat = USB_ST_CRC_ERR;
1169 /* we got an Root Hub Status Change interrupt */
1172 ohci_dump_roothub (&gohci, 1);
1176 timeout = rh_check_port_status(&gohci);
1178 #if 0 /* this does nothing useful, but leave it here in case that changes */
1179 /* the called routine adds 1 to the passed value */
1180 usb_hub_port_connect_change(gohci.rh.dev, timeout - 1);
1184 * This is potentially dangerous because it assumes
1185 * that only one device is ever plugged in!
1193 dev->act_len = transfer_len;
1196 pkt_print(dev, pipe, buffer, transfer_len, setup, "RET(ctlr)", usb_pipein(pipe));
1199 /* free TDs in urb_priv */
1200 urb_free_priv (&urb_priv);
1204 /* submit routines called from usb.c */
1205 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1208 info("submit_bulk_msg");
1209 return submit_common_msg(dev, pipe, buffer, transfer_len, NULL, 0);
1212 int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1213 int transfer_len, struct devrequest *setup)
1215 int maxsize = usb_maxpacket(dev, pipe);
1217 info("submit_control_msg");
1219 urb_priv.actual_length = 0;
1220 pkt_print(dev, pipe, buffer, transfer_len, setup, "SUB", usb_pipein(pipe));
1223 err("submit_control_message: pipesize for pipe %lx is zero",
1227 if (((pipe >> 8) & 0x7f) == gohci.rh.devnum) {
1229 /* root hub - redirect */
1230 return ohci_submit_rh_msg(dev, pipe, buffer, transfer_len,
1234 return submit_common_msg(dev, pipe, buffer, transfer_len, setup, 0);
1237 int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1238 int transfer_len, int interval)
1240 info("submit_int_msg");
1244 /*-------------------------------------------------------------------------*
1246 *-------------------------------------------------------------------------*/
1248 /* reset the HC and BUS */
1250 static int hc_reset (ohci_t *ohci)
1253 int smm_timeout = 50; /* 0,5 sec */
1255 if (readl (&ohci->regs->control) & OHCI_CTRL_IR) { /* SMM owns the HC */
1256 writel (OHCI_OCR, &ohci->regs->cmdstatus); /* request ownership */
1257 info("USB HC TakeOver from SMM");
1258 while (readl (&ohci->regs->control) & OHCI_CTRL_IR) {
1260 if (--smm_timeout == 0) {
1261 err("USB HC TakeOver failed!");
1267 /* Disable HC interrupts */
1268 writel (OHCI_INTR_MIE, &ohci->regs->intrdisable);
1270 dbg("USB HC reset_hc usb-%s: ctrl = 0x%X ;",
1272 readl (&ohci->regs->control));
1274 /* Reset USB (needed by some controllers) */
1275 ohci->hc_control = 0;
1276 writel (ohci->hc_control, &ohci->regs->control);
1278 /* HC Reset requires max 10 us delay */
1279 writel (OHCI_HCR, &ohci->regs->cmdstatus);
1280 while ((readl (&ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
1281 if (--timeout == 0) {
1282 err("USB HC reset timed out!");
1290 /*-------------------------------------------------------------------------*/
1292 /* Start an OHCI controller, set the BUS operational
1294 * connect the virtual root hub */
1296 static int hc_start (ohci_t * ohci)
1299 unsigned int fminterval;
1303 /* Tell the controller where the control and bulk lists are
1304 * The lists are empty now. */
1306 writel (0, &ohci->regs->ed_controlhead);
1307 writel (0, &ohci->regs->ed_bulkhead);
1309 writel ((__u32)ohci->hcca, &ohci->regs->hcca); /* a reset clears this */
1311 fminterval = 0x2edf;
1312 writel ((fminterval * 9) / 10, &ohci->regs->periodicstart);
1313 fminterval |= ((((fminterval - 210) * 6) / 7) << 16);
1314 writel (fminterval, &ohci->regs->fminterval);
1315 writel (0x628, &ohci->regs->lsthresh);
1317 /* start controller operations */
1318 ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
1320 writel (ohci->hc_control, &ohci->regs->control);
1322 /* disable all interrupts */
1323 mask = (OHCI_INTR_SO | OHCI_INTR_WDH | OHCI_INTR_SF | OHCI_INTR_RD |
1324 OHCI_INTR_UE | OHCI_INTR_FNO | OHCI_INTR_RHSC |
1325 OHCI_INTR_OC | OHCI_INTR_MIE);
1326 writel (mask, &ohci->regs->intrdisable);
1327 /* clear all interrupts */
1328 mask &= ~OHCI_INTR_MIE;
1329 writel (mask, &ohci->regs->intrstatus);
1330 /* Choose the interrupts we care about now - but w/o MIE */
1331 mask = OHCI_INTR_RHSC | OHCI_INTR_UE | OHCI_INTR_WDH | OHCI_INTR_SO;
1332 writel (mask, &ohci->regs->intrenable);
1335 /* required for AMD-756 and some Mac platforms */
1336 writel ((roothub_a (ohci) | RH_A_NPS) & ~RH_A_PSM,
1337 &ohci->regs->roothub.a);
1338 writel (RH_HS_LPSC, &ohci->regs->roothub.status);
1339 #endif /* OHCI_USE_NPS */
1341 /* POTPGT delay is bits 24-31, in 2 ms units. */
1342 mdelay ((roothub_a (ohci) >> 23) & 0x1fe);
1344 /* connect the virtual root hub */
1345 ohci->rh.devnum = 0;
1350 /*-------------------------------------------------------------------------*/
1352 /* an interrupt happens */
1357 ohci_t *ohci = &gohci;
1358 struct ohci_regs *regs = ohci->regs;
1362 if ((ohci->hcca->done_head != 0) &&
1363 !(ohci_cpu_to_le32(ohci->hcca->done_head) & 0x01)) {
1365 ints = OHCI_INTR_WDH;
1367 } else if ((ints = readl (®s->intrstatus)) == ~(u32)0) {
1369 err ("%s device removed!", ohci->slot_name);
1372 } else if ((ints &= readl (®s->intrenable)) == 0) {
1373 dbg("hc_interrupt: returning..\n");
1377 /* dbg("Interrupt: %x frame: %x", ints, le16_to_cpu (ohci->hcca->frame_no)); */
1379 if (ints & OHCI_INTR_RHSC) {
1384 if (ints & OHCI_INTR_UE) {
1386 err ("OHCI Unrecoverable Error, controller usb-%s disabled",
1388 /* e.g. due to PCI Master/Target Abort */
1391 ohci_dump (ohci, 1);
1393 /* FIXME: be optimistic, hope that bug won't repeat often. */
1394 /* Make some non-interrupt context restart the controller. */
1395 /* Count and limit the retries though; either hardware or */
1396 /* software errors can go forever... */
1401 if (ints & OHCI_INTR_WDH) {
1402 writel (OHCI_INTR_WDH, ®s->intrdisable);
1403 stat = dl_done_list (&gohci, dl_reverse_done_list (&gohci));
1404 writel (OHCI_INTR_WDH, ®s->intrenable);
1407 if (ints & OHCI_INTR_SO) {
1408 dbg("USB Schedule overrun\n");
1409 writel (OHCI_INTR_SO, ®s->intrenable);
1413 /* FIXME: this assumes SOF (1/ms) interrupts don't get lost... */
1414 if (ints & OHCI_INTR_SF) {
1415 unsigned int frame = ohci_cpu_to_le16 (ohci->hcca->frame_no) & 1;
1417 writel (OHCI_INTR_SF, ®s->intrdisable);
1418 if (ohci->ed_rm_list[frame] != NULL)
1419 writel (OHCI_INTR_SF, ®s->intrenable);
1423 writel (ints, ®s->intrstatus);
1427 /*-------------------------------------------------------------------------*/
1429 /*-------------------------------------------------------------------------*/
1431 /* De-allocate all resources.. */
1433 static void hc_release_ohci (ohci_t *ohci)
1435 dbg ("USB HC release ohci usb-%s", ohci->slot_name);
1437 if (!ohci->disabled)
1441 /*-------------------------------------------------------------------------*/
1444 * low level initalisation routine, called from usb.c
1446 static char ohci_inited = 0;
1448 int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
1451 /* Set the USB Clock */
1452 *(vu_long *)MPC5XXX_CDM_48_FDC = CONFIG_USB_CLOCK;
1454 #ifdef CONFIG_PSC3_USB /* USB is using the alternate configuration */
1455 /* remove all PSC3 USB bits first before ORing in ours */
1456 *(vu_long *)MPC5XXX_GPS_PORT_CONFIG &= ~0x00804f00;
1458 /* remove all USB bits first before ORing in ours */
1459 *(vu_long *)MPC5XXX_GPS_PORT_CONFIG &= ~0x00807000;
1461 /* Activate USB port */
1462 *(vu_long *)MPC5XXX_GPS_PORT_CONFIG |= CONFIG_USB_CONFIG;
1464 memset (&gohci, 0, sizeof (ohci_t));
1465 memset (&urb_priv, 0, sizeof (urb_priv_t));
1467 /* align the storage */
1468 if ((__u32)&ghcca[0] & 0xff) {
1469 err("HCCA not aligned!!");
1473 info("aligned ghcca %p", phcca);
1474 memset(&ohci_dev, 0, sizeof(struct ohci_device));
1475 if ((__u32)&ohci_dev.ed[0] & 0x7) {
1476 err("EDs not aligned!!");
1479 memset(gtd, 0, sizeof(td_t) * (NUM_TD + 1));
1480 if ((__u32)gtd & 0x7) {
1481 err("TDs not aligned!!");
1486 memset (phcca, 0, sizeof (struct ohci_hcca));
1491 gohci.regs = (struct ohci_regs *)MPC5XXX_USB;
1494 gohci.slot_name = "mpc5200";
1496 if (hc_reset (&gohci) < 0) {
1497 hc_release_ohci (&gohci);
1501 if (hc_start (&gohci) < 0) {
1502 err ("can't start usb-%s", gohci.slot_name);
1503 hc_release_ohci (&gohci);
1508 ohci_dump (&gohci, 1);
1516 int usb_lowlevel_stop(int index)
1518 /* this gets called really early - before the controller has */
1519 /* even been initialized! */
1522 /* TODO release any interrupts, etc. */
1523 /* call hc_release_ohci() here ? */
1528 #endif /* CONFIG_USB_OHCI */