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))
45 #define min_t(type,x,y) ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
48 #define dbg(format, arg...) printf("DEBUG: " format "\n", ## arg)
50 #define dbg(format, arg...) do {} while(0)
52 #define err(format, arg...) printf("ERROR: " format "\n", ## arg)
54 #define info(format, arg...) printf("INFO: " format "\n", ## arg)
56 #define info(format, arg...) do {} while(0)
59 #define m16_swap(x) swap_16(x)
60 #define m32_swap(x) swap_32(x)
62 #define ohci_cpu_to_le16(x) (x)
63 #define ohci_cpu_to_le32(x) (x)
67 /* this must be aligned to a 256 byte boundary */
68 struct ohci_hcca ghcca[1];
69 /* a pointer to the aligned storage */
70 struct ohci_hcca *phcca;
71 /* this allocates EDs for all possible endpoints */
72 struct ohci_device ohci_dev;
77 /* device which was disconnected */
78 struct usb_device *devgone;
79 /* flag guarding URB transation */
82 /*-------------------------------------------------------------------------*/
84 /* AMD-756 (D2 rev) reports corrupt register contents in some cases.
85 * The erratum (#4) description is incorrect. AMD's workaround waits
86 * till some bits (mostly reserved) are clear; ok for all revs.
88 #define OHCI_QUIRK_AMD756 0xabcd
89 #define read_roothub(hc, register, mask) ({ \
90 u32 temp = readl (&hc->regs->roothub.register); \
91 if (hc->flags & OHCI_QUIRK_AMD756) \
93 temp = readl (&hc->regs->roothub.register); \
96 static u32 roothub_a (struct ohci *hc)
97 { return read_roothub (hc, a, 0xfc0fe000); }
98 static inline u32 roothub_b (struct ohci *hc)
99 { return readl (&hc->regs->roothub.b); }
100 static inline u32 roothub_status (struct ohci *hc)
101 { return readl (&hc->regs->roothub.status); }
102 static u32 roothub_portstatus (struct ohci *hc, int i)
103 { return read_roothub (hc, portstatus [i], 0xffe0fce0); }
106 /* forward declaration */
107 static int hc_interrupt (void);
109 td_submit_job (struct usb_device * dev, unsigned long pipe, void * buffer,
110 int transfer_len, struct devrequest * setup, urb_priv_t * urb, int interval);
112 /*-------------------------------------------------------------------------*
113 * URB support functions
114 *-------------------------------------------------------------------------*/
116 /* free HCD-private data associated with this URB */
118 static void urb_free_priv (urb_priv_t * urb)
124 last = urb->length - 1;
126 for (i = 0; i <= last; i++) {
136 /*-------------------------------------------------------------------------*/
139 static int sohci_get_current_frame_number (struct usb_device * dev);
141 /* debug| print the main components of an URB
142 * small: 0) header + data packets 1) just header */
144 static void pkt_print (struct usb_device * dev, unsigned long pipe, void * buffer,
145 int transfer_len, struct devrequest * setup, char * str, int small)
147 urb_priv_t * purb = &urb_priv;
149 dbg("%s URB:[%4x] dev:%2d,ep:%2d-%c,type:%s,len:%d/%d stat:%#lx",
151 sohci_get_current_frame_number (dev),
152 usb_pipedevice (pipe),
153 usb_pipeendpoint (pipe),
154 usb_pipeout (pipe)? 'O': 'I',
155 usb_pipetype (pipe) < 2? (usb_pipeint (pipe)? "INTR": "ISOC"):
156 (usb_pipecontrol (pipe)? "CTRL": "BULK"),
158 transfer_len, dev->status);
159 #ifdef OHCI_VERBOSE_DEBUG
163 if (usb_pipecontrol (pipe)) {
164 printf (__FILE__ ": cmd(8):");
165 for (i = 0; i < 8 ; i++)
166 printf (" %02x", ((__u8 *) setup) [i]);
169 if (transfer_len > 0 && buffer) {
170 printf (__FILE__ ": data(%d/%d):",
173 len = usb_pipeout (pipe)?
174 transfer_len: purb->actual_length;
175 for (i = 0; i < 16 && i < len; i++)
176 printf (" %02x", ((__u8 *) buffer) [i]);
177 printf ("%s\n", i < len? "...": "");
183 /* just for debugging; prints non-empty branches of the int ed tree inclusive iso eds*/
184 void ep_print_int_eds (ohci_t *ohci, char * str) {
187 for (i= 0; i < 32; i++) {
189 ed_p = &(ohci->hcca->int_table [i]);
192 printf (__FILE__ ": %s branch int %2d(%2x):", str, i, i);
193 while (*ed_p != 0 && j--) {
194 ed_t *ed = (ed_t *)ohci_cpu_to_le32(ed_p);
195 printf (" ed: %4x;", ed->hwINFO);
196 ed_p = &ed->hwNextED;
202 static void ohci_dump_intr_mask (char *label, __u32 mask)
204 dbg ("%s: 0x%08x%s%s%s%s%s%s%s%s%s",
207 (mask & OHCI_INTR_MIE) ? " MIE" : "",
208 (mask & OHCI_INTR_OC) ? " OC" : "",
209 (mask & OHCI_INTR_RHSC) ? " RHSC" : "",
210 (mask & OHCI_INTR_FNO) ? " FNO" : "",
211 (mask & OHCI_INTR_UE) ? " UE" : "",
212 (mask & OHCI_INTR_RD) ? " RD" : "",
213 (mask & OHCI_INTR_SF) ? " SF" : "",
214 (mask & OHCI_INTR_WDH) ? " WDH" : "",
215 (mask & OHCI_INTR_SO) ? " SO" : ""
219 static void maybe_print_eds (char *label, __u32 value)
221 ed_t *edp = (ed_t *)value;
224 dbg ("%s %08x", label, value);
225 dbg ("%08x", edp->hwINFO);
226 dbg ("%08x", edp->hwTailP);
227 dbg ("%08x", edp->hwHeadP);
228 dbg ("%08x", edp->hwNextED);
232 static char * hcfs2string (int state)
235 case OHCI_USB_RESET: return "reset";
236 case OHCI_USB_RESUME: return "resume";
237 case OHCI_USB_OPER: return "operational";
238 case OHCI_USB_SUSPEND: return "suspend";
243 /* dump control and status registers */
244 static void ohci_dump_status (ohci_t *controller)
246 struct ohci_regs *regs = controller->regs;
249 temp = readl (®s->revision) & 0xff;
251 dbg ("spec %d.%d", (temp >> 4), (temp & 0x0f));
253 temp = readl (®s->control);
254 dbg ("control: 0x%08x%s%s%s HCFS=%s%s%s%s%s CBSR=%d", temp,
255 (temp & OHCI_CTRL_RWE) ? " RWE" : "",
256 (temp & OHCI_CTRL_RWC) ? " RWC" : "",
257 (temp & OHCI_CTRL_IR) ? " IR" : "",
258 hcfs2string (temp & OHCI_CTRL_HCFS),
259 (temp & OHCI_CTRL_BLE) ? " BLE" : "",
260 (temp & OHCI_CTRL_CLE) ? " CLE" : "",
261 (temp & OHCI_CTRL_IE) ? " IE" : "",
262 (temp & OHCI_CTRL_PLE) ? " PLE" : "",
263 temp & OHCI_CTRL_CBSR
266 temp = readl (®s->cmdstatus);
267 dbg ("cmdstatus: 0x%08x SOC=%d%s%s%s%s", temp,
268 (temp & OHCI_SOC) >> 16,
269 (temp & OHCI_OCR) ? " OCR" : "",
270 (temp & OHCI_BLF) ? " BLF" : "",
271 (temp & OHCI_CLF) ? " CLF" : "",
272 (temp & OHCI_HCR) ? " HCR" : ""
275 ohci_dump_intr_mask ("intrstatus", readl (®s->intrstatus));
276 ohci_dump_intr_mask ("intrenable", readl (®s->intrenable));
278 maybe_print_eds ("ed_periodcurrent", readl (®s->ed_periodcurrent));
280 maybe_print_eds ("ed_controlhead", readl (®s->ed_controlhead));
281 maybe_print_eds ("ed_controlcurrent", readl (®s->ed_controlcurrent));
283 maybe_print_eds ("ed_bulkhead", readl (®s->ed_bulkhead));
284 maybe_print_eds ("ed_bulkcurrent", readl (®s->ed_bulkcurrent));
286 maybe_print_eds ("donehead", readl (®s->donehead));
289 static void ohci_dump_roothub (ohci_t *controller, int verbose)
293 temp = roothub_a (controller);
294 ndp = (temp & RH_A_NDP);
297 dbg ("roothub.a: %08x POTPGT=%d%s%s%s%s%s NDP=%d", temp,
298 ((temp & RH_A_POTPGT) >> 24) & 0xff,
299 (temp & RH_A_NOCP) ? " NOCP" : "",
300 (temp & RH_A_OCPM) ? " OCPM" : "",
301 (temp & RH_A_DT) ? " DT" : "",
302 (temp & RH_A_NPS) ? " NPS" : "",
303 (temp & RH_A_PSM) ? " PSM" : "",
306 temp = roothub_b (controller);
307 dbg ("roothub.b: %08x PPCM=%04x DR=%04x",
309 (temp & RH_B_PPCM) >> 16,
312 temp = roothub_status (controller);
313 dbg ("roothub.status: %08x%s%s%s%s%s%s",
315 (temp & RH_HS_CRWE) ? " CRWE" : "",
316 (temp & RH_HS_OCIC) ? " OCIC" : "",
317 (temp & RH_HS_LPSC) ? " LPSC" : "",
318 (temp & RH_HS_DRWE) ? " DRWE" : "",
319 (temp & RH_HS_OCI) ? " OCI" : "",
320 (temp & RH_HS_LPS) ? " LPS" : ""
324 for (i = 0; i < ndp; i++) {
325 temp = roothub_portstatus (controller, i);
326 dbg ("roothub.portstatus [%d] = 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s",
329 (temp & RH_PS_PRSC) ? " PRSC" : "",
330 (temp & RH_PS_OCIC) ? " OCIC" : "",
331 (temp & RH_PS_PSSC) ? " PSSC" : "",
332 (temp & RH_PS_PESC) ? " PESC" : "",
333 (temp & RH_PS_CSC) ? " CSC" : "",
335 (temp & RH_PS_LSDA) ? " LSDA" : "",
336 (temp & RH_PS_PPS) ? " PPS" : "",
337 (temp & RH_PS_PRS) ? " PRS" : "",
338 (temp & RH_PS_POCI) ? " POCI" : "",
339 (temp & RH_PS_PSS) ? " PSS" : "",
341 (temp & RH_PS_PES) ? " PES" : "",
342 (temp & RH_PS_CCS) ? " CCS" : ""
347 static void ohci_dump (ohci_t *controller, int verbose)
349 dbg ("OHCI controller usb-%s state", controller->slot_name);
351 /* dumps some of the state we know about */
352 ohci_dump_status (controller);
354 ep_print_int_eds (controller, "hcca");
355 dbg ("hcca frame #%04x", controller->hcca->frame_no);
356 ohci_dump_roothub (controller, 1);
362 /*-------------------------------------------------------------------------*
363 * Interface functions (URB)
364 *-------------------------------------------------------------------------*/
366 /* get a transfer request */
368 int sohci_submit_job(struct usb_device *dev, unsigned long pipe, void *buffer,
369 int transfer_len, struct devrequest *setup, int interval)
373 urb_priv_t *purb_priv;
378 /* when controller's hung, permit only roothub cleanup attempts
379 * such as powering down ports */
380 if (ohci->disabled) {
381 err("sohci_submit_job: EPIPE");
385 /* if we have an unfinished URB from previous transaction let's
386 * fail and scream as quickly as possible so as not to corrupt
387 * further communication */
389 err("sohci_submit_job: URB NOT FINISHED");
392 /* we're about to begin a new transaction here so mark the URB unfinished */
395 /* every endpoint has a ed, locate and fill it */
396 if (!(ed = ep_add_ed (dev, pipe))) {
397 err("sohci_submit_job: ENOMEM");
401 /* for the private part of the URB we need the number of TDs (size) */
402 switch (usb_pipetype (pipe)) {
403 case PIPE_BULK: /* one TD for every 4096 Byte */
404 size = (transfer_len - 1) / 4096 + 1;
406 case PIPE_CONTROL: /* 1 TD for setup, 1 for ACK and 1 for every 4096 B */
407 size = (transfer_len == 0)? 2:
408 (transfer_len - 1) / 4096 + 3;
412 if (size >= (N_URB_TD - 1)) {
413 err("need %d TDs, only have %d", size, N_URB_TD);
416 purb_priv = &urb_priv;
417 purb_priv->pipe = pipe;
419 /* fill the private part of the URB */
420 purb_priv->length = size;
422 purb_priv->actual_length = 0;
424 /* allocate the TDs */
425 /* note that td[0] was allocated in ep_add_ed */
426 for (i = 0; i < size; i++) {
427 purb_priv->td[i] = td_alloc (dev);
428 if (!purb_priv->td[i]) {
429 purb_priv->length = i;
430 urb_free_priv (purb_priv);
431 err("sohci_submit_job: ENOMEM");
436 if (ed->state == ED_NEW || (ed->state & ED_DEL)) {
437 urb_free_priv (purb_priv);
438 err("sohci_submit_job: EINVAL");
442 /* link the ed into a chain if is not already */
443 if (ed->state != ED_OPER)
446 /* fill the TDs and link it to the ed */
447 td_submit_job(dev, pipe, buffer, transfer_len, setup, purb_priv, interval);
452 /*-------------------------------------------------------------------------*/
455 /* tell us the current USB frame number */
457 static int sohci_get_current_frame_number (struct usb_device *usb_dev)
459 ohci_t *ohci = &gohci;
461 return ohci_cpu_to_le16 (ohci->hcca->frame_no);
465 /*-------------------------------------------------------------------------*
466 * ED handling functions
467 *-------------------------------------------------------------------------*/
469 /* link an ed into one of the HC chains */
471 static int ep_link (ohci_t *ohci, ed_t *edi)
473 volatile ed_t *ed = edi;
480 if (ohci->ed_controltail == NULL) {
481 writel (ed, &ohci->regs->ed_controlhead);
483 ohci->ed_controltail->hwNextED = ohci_cpu_to_le32 ((unsigned long)ed);
485 ed->ed_prev = ohci->ed_controltail;
486 if (!ohci->ed_controltail && !ohci->ed_rm_list[0] &&
487 !ohci->ed_rm_list[1] && !ohci->sleeping) {
488 ohci->hc_control |= OHCI_CTRL_CLE;
489 writel (ohci->hc_control, &ohci->regs->control);
491 ohci->ed_controltail = edi;
496 if (ohci->ed_bulktail == NULL) {
497 writel (ed, &ohci->regs->ed_bulkhead);
499 ohci->ed_bulktail->hwNextED = ohci_cpu_to_le32 ((unsigned long)ed);
501 ed->ed_prev = ohci->ed_bulktail;
502 if (!ohci->ed_bulktail && !ohci->ed_rm_list[0] &&
503 !ohci->ed_rm_list[1] && !ohci->sleeping) {
504 ohci->hc_control |= OHCI_CTRL_BLE;
505 writel (ohci->hc_control, &ohci->regs->control);
507 ohci->ed_bulktail = edi;
513 /*-------------------------------------------------------------------------*/
515 /* unlink an ed from one of the HC chains.
516 * just the link to the ed is unlinked.
517 * the link from the ed still points to another operational ed or 0
518 * so the HC can eventually finish the processing of the unlinked ed */
520 static int ep_unlink (ohci_t *ohci, ed_t *edi)
522 volatile ed_t *ed = edi;
524 ed->hwINFO |= ohci_cpu_to_le32 (OHCI_ED_SKIP);
528 if (ed->ed_prev == NULL) {
530 ohci->hc_control &= ~OHCI_CTRL_CLE;
531 writel (ohci->hc_control, &ohci->regs->control);
533 writel (ohci_cpu_to_le32 (*((__u32 *)&ed->hwNextED)), &ohci->regs->ed_controlhead);
535 ed->ed_prev->hwNextED = ed->hwNextED;
537 if (ohci->ed_controltail == ed) {
538 ohci->ed_controltail = ed->ed_prev;
540 ((ed_t *)ohci_cpu_to_le32 (*((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
545 if (ed->ed_prev == NULL) {
547 ohci->hc_control &= ~OHCI_CTRL_BLE;
548 writel (ohci->hc_control, &ohci->regs->control);
550 writel (ohci_cpu_to_le32 (*((__u32 *)&ed->hwNextED)), &ohci->regs->ed_bulkhead);
552 ed->ed_prev->hwNextED = ed->hwNextED;
554 if (ohci->ed_bulktail == ed) {
555 ohci->ed_bulktail = ed->ed_prev;
557 ((ed_t *)ohci_cpu_to_le32 (*((__u32 *)&ed->hwNextED)))->ed_prev = ed->ed_prev;
561 ed->state = ED_UNLINK;
566 /*-------------------------------------------------------------------------*/
568 /* add/reinit an endpoint; this should be done once at the usb_set_configuration command,
569 * but the USB stack is a little bit stateless so we do it at every transaction
570 * if the state of the ed is ED_NEW then a dummy td is added and the state is changed to ED_UNLINK
571 * in all other cases the state is left unchanged
572 * the ed info fields are setted anyway even though most of them should not change */
574 static ed_t * ep_add_ed (struct usb_device *usb_dev, unsigned long pipe)
580 ed = ed_ret = &ohci_dev.ed[(usb_pipeendpoint (pipe) << 1) |
581 (usb_pipecontrol (pipe)? 0: usb_pipeout (pipe))];
583 if ((ed->state & ED_DEL) || (ed->state & ED_URB_DEL)) {
584 err("ep_add_ed: pending delete");
585 /* pending delete request */
589 if (ed->state == ED_NEW) {
590 ed->hwINFO = ohci_cpu_to_le32 (OHCI_ED_SKIP); /* skip ed */
591 /* dummy td; end of td list for ed */
592 td = td_alloc (usb_dev);
593 ed->hwTailP = ohci_cpu_to_le32 ((unsigned long)td);
594 ed->hwHeadP = ed->hwTailP;
595 ed->state = ED_UNLINK;
596 ed->type = usb_pipetype (pipe);
600 ed->hwINFO = ohci_cpu_to_le32 (usb_pipedevice (pipe)
601 | usb_pipeendpoint (pipe) << 7
602 | (usb_pipeisoc (pipe)? 0x8000: 0)
603 | (usb_pipecontrol (pipe)? 0: (usb_pipeout (pipe)? 0x800: 0x1000))
604 | (usb_dev->speed == USB_SPEED_LOW) << 13
605 | usb_maxpacket (usb_dev, pipe) << 16);
610 /*-------------------------------------------------------------------------*
611 * TD handling functions
612 *-------------------------------------------------------------------------*/
614 /* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
616 static void td_fill (ohci_t *ohci, unsigned int info,
618 struct usb_device *dev, int index, urb_priv_t *urb_priv)
620 volatile td_t *td, *td_pt;
621 #ifdef OHCI_FILL_TRACE
625 if (index > urb_priv->length) {
626 err("index > length");
629 /* use this td as the next dummy */
630 td_pt = urb_priv->td [index];
633 /* fill the old dummy TD */
634 td = urb_priv->td [index] = (td_t *)(ohci_cpu_to_le32 (urb_priv->ed->hwTailP) & ~0xf);
636 td->ed = urb_priv->ed;
637 td->next_dl_td = NULL;
639 td->data = (__u32)data;
640 #ifdef OHCI_FILL_TRACE
641 if (usb_pipebulk(urb_priv->pipe) && usb_pipeout(urb_priv->pipe)) {
642 for (i = 0; i < len; i++)
643 printf("td->data[%d] %#2x ",i, ((unsigned char *)td->data)[i]);
650 td->hwINFO = ohci_cpu_to_le32 (info);
651 td->hwCBP = ohci_cpu_to_le32 ((unsigned long)data);
653 td->hwBE = ohci_cpu_to_le32 ((unsigned long)(data + len - 1));
656 td->hwNextTD = ohci_cpu_to_le32 ((unsigned long)td_pt);
658 /* append to queue */
659 td->ed->hwTailP = td->hwNextTD;
662 /*-------------------------------------------------------------------------*/
664 /* prepare all TDs of a transfer */
665 static void td_submit_job (struct usb_device *dev, unsigned long pipe, void *buffer,
666 int transfer_len, struct devrequest *setup, urb_priv_t *urb, int interval)
668 ohci_t *ohci = &gohci;
669 int data_len = transfer_len;
673 unsigned int toggle = 0;
675 /* OHCI handles the DATA-toggles itself, we just use the USB-toggle bits for reseting */
676 if(usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe))) {
677 toggle = TD_T_TOGGLE;
680 usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), 1);
688 switch (usb_pipetype (pipe)) {
690 info = usb_pipeout (pipe)?
691 TD_CC | TD_DP_OUT : TD_CC | TD_DP_IN ;
692 while(data_len > 4096) {
693 td_fill (ohci, info | (cnt? TD_T_TOGGLE:toggle), data, 4096, dev, cnt, urb);
694 data += 4096; data_len -= 4096; cnt++;
696 info = usb_pipeout (pipe)?
697 TD_CC | TD_DP_OUT : TD_CC | TD_R | TD_DP_IN ;
698 td_fill (ohci, info | (cnt? TD_T_TOGGLE:toggle), data, data_len, dev, cnt, urb);
702 writel (OHCI_BLF, &ohci->regs->cmdstatus); /* start bulk list */
706 info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
707 td_fill (ohci, info, setup, 8, dev, cnt++, urb);
709 info = usb_pipeout (pipe)?
710 TD_CC | TD_R | TD_DP_OUT | TD_T_DATA1 : TD_CC | TD_R | TD_DP_IN | TD_T_DATA1;
711 /* NOTE: mishandles transfers >8K, some >4K */
712 td_fill (ohci, info, data, data_len, dev, cnt++, urb);
714 info = usb_pipeout (pipe)?
715 TD_CC | TD_DP_IN | TD_T_DATA1: TD_CC | TD_DP_OUT | TD_T_DATA1;
716 td_fill (ohci, info, data, 0, dev, cnt++, urb);
718 writel (OHCI_CLF, &ohci->regs->cmdstatus); /* start Control list */
721 if (urb->length != cnt)
722 dbg("TD LENGTH %d != CNT %d", urb->length, cnt);
725 /*-------------------------------------------------------------------------*
726 * Done List handling functions
727 *-------------------------------------------------------------------------*/
730 /* calculate the transfer length and update the urb */
732 static void dl_transfer_length(td_t * td)
735 urb_priv_t *lurb_priv = &urb_priv;
737 tdBE = ohci_cpu_to_le32 (td->hwBE);
738 tdCBP = ohci_cpu_to_le32 (td->hwCBP);
741 if (!(usb_pipecontrol(lurb_priv->pipe) &&
742 ((td->index == 0) || (td->index == lurb_priv->length - 1)))) {
745 lurb_priv->actual_length += tdBE - td->data + 1;
747 lurb_priv->actual_length += tdCBP - td->data;
752 /*-------------------------------------------------------------------------*/
754 /* replies to the request have to be on a FIFO basis so
755 * we reverse the reversed done-list */
757 static td_t * dl_reverse_done_list (ohci_t *ohci)
761 td_t *td_list = NULL;
762 urb_priv_t *lurb_priv = NULL;
764 td_list_hc = ohci_cpu_to_le32 (ohci->hcca->done_head) & 0xfffffff0;
765 ohci->hcca->done_head = 0;
768 td_list = (td_t *)td_list_hc;
770 if (TD_CC_GET (ohci_cpu_to_le32 (td_list->hwINFO))) {
771 lurb_priv = &urb_priv;
772 dbg(" USB-error/status: %x : %p",
773 TD_CC_GET (ohci_cpu_to_le32 (td_list->hwINFO)), td_list);
774 if (td_list->ed->hwHeadP & ohci_cpu_to_le32 (0x1)) {
775 if (lurb_priv && ((td_list->index + 1) < lurb_priv->length)) {
776 td_list->ed->hwHeadP =
777 (lurb_priv->td[lurb_priv->length - 1]->hwNextTD & ohci_cpu_to_le32 (0xfffffff0)) |
778 (td_list->ed->hwHeadP & ohci_cpu_to_le32 (0x2));
779 lurb_priv->td_cnt += lurb_priv->length - td_list->index - 1;
781 td_list->ed->hwHeadP &= ohci_cpu_to_le32 (0xfffffff2);
783 td_list->hwNextTD = 0;
786 td_list->next_dl_td = td_rev;
788 td_list_hc = ohci_cpu_to_le32 (td_list->hwNextTD) & 0xfffffff0;
793 /*-------------------------------------------------------------------------*/
796 static int dl_done_list (ohci_t *ohci, td_t *td_list)
798 td_t *td_list_next = NULL;
803 urb_priv_t *lurb_priv;
804 __u32 tdINFO, edHeadP, edTailP;
807 td_list_next = td_list->next_dl_td;
809 lurb_priv = &urb_priv;
810 tdINFO = ohci_cpu_to_le32 (td_list->hwINFO);
814 dl_transfer_length(td_list);
816 /* error code of transfer */
817 cc = TD_CC_GET (tdINFO);
818 if (++(lurb_priv->td_cnt) == lurb_priv->length) {
819 if ((ed->state & (ED_OPER | ED_UNLINK))
820 && (lurb_priv->state != URB_DEL)) {
821 dbg("ConditionCode %#x", cc);
822 stat = cc_to_error[cc];
827 if (ed->state != ED_NEW) {
828 edHeadP = ohci_cpu_to_le32 (ed->hwHeadP) & 0xfffffff0;
829 edTailP = ohci_cpu_to_le32 (ed->hwTailP);
831 /* unlink eds if they are not busy */
832 if ((edHeadP == edTailP) && (ed->state == ED_OPER))
833 ep_unlink (ohci, ed);
836 td_list = td_list_next;
841 /*-------------------------------------------------------------------------*
843 *-------------------------------------------------------------------------*/
845 #include <usbroothubdes.h>
847 /* Hub class-specific descriptor is constructed dynamically */
850 /*-------------------------------------------------------------------------*/
852 #define OK(x) len = (x); break
854 #define WR_RH_STAT(x) {info("WR:status %#8x", (x));writel((x), &gohci.regs->roothub.status);}
855 #define WR_RH_PORTSTAT(x) {info("WR:portstatus[%d] %#8x", wIndex-1, (x));writel((x), &gohci.regs->roothub.portstatus[wIndex-1]);}
857 #define WR_RH_STAT(x) writel((x), &gohci.regs->roothub.status)
858 #define WR_RH_PORTSTAT(x) writel((x), &gohci.regs->roothub.portstatus[wIndex-1])
860 #define RD_RH_STAT roothub_status(&gohci)
861 #define RD_RH_PORTSTAT roothub_portstatus(&gohci,wIndex-1)
863 /* request to virtual root hub */
865 int rh_check_port_status(ohci_t *controller)
871 temp = roothub_a (controller);
872 ndp = (temp & RH_A_NDP);
873 for (i = 0; i < ndp; i++) {
874 temp = roothub_portstatus (controller, i);
875 /* check for a device disconnect */
876 if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
877 (RH_PS_PESC | RH_PS_CSC)) &&
878 ((temp & RH_PS_CCS) == 0)) {
886 static int ohci_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
887 void *buffer, int transfer_len, struct devrequest *cmd)
889 void * data = buffer;
890 int leni = transfer_len;
894 __u8 *data_buf = (__u8 *)datab;
901 urb_priv.actual_length = 0;
902 pkt_print(dev, pipe, buffer, transfer_len, cmd, "SUB(rh)", usb_pipein(pipe));
904 if (usb_pipeint(pipe)) {
905 info("Root-Hub submit IRQ: NOT implemented");
909 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
910 wValue = m16_swap (cmd->value);
911 wIndex = m16_swap (cmd->index);
912 wLength = m16_swap (cmd->length);
914 info("Root-Hub: adr: %2x cmd(%1x): %08x %04x %04x %04x",
915 dev->devnum, 8, bmRType_bReq, wValue, wIndex, wLength);
917 switch (bmRType_bReq) {
918 /* Request Destination:
919 without flags: Device,
920 RH_INTERFACE: interface,
921 RH_ENDPOINT: endpoint,
922 RH_CLASS means HUB here,
923 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
927 *(__u16 *) data_buf = m16_swap (1); OK (2);
928 case RH_GET_STATUS | RH_INTERFACE:
929 *(__u16 *) data_buf = m16_swap (0); OK (2);
930 case RH_GET_STATUS | RH_ENDPOINT:
931 *(__u16 *) data_buf = m16_swap (0); OK (2);
932 case RH_GET_STATUS | RH_CLASS:
933 *(__u32 *) data_buf = m32_swap (
934 RD_RH_STAT & ~(RH_HS_CRWE | RH_HS_DRWE));
936 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
937 *(__u32 *) data_buf = m32_swap (RD_RH_PORTSTAT); OK (4);
939 case RH_CLEAR_FEATURE | RH_ENDPOINT:
941 case (RH_ENDPOINT_STALL): OK (0);
945 case RH_CLEAR_FEATURE | RH_CLASS:
947 case RH_C_HUB_LOCAL_POWER:
949 case (RH_C_HUB_OVER_CURRENT):
950 WR_RH_STAT(RH_HS_OCIC); OK (0);
954 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
956 case (RH_PORT_ENABLE):
957 WR_RH_PORTSTAT (RH_PS_CCS ); OK (0);
958 case (RH_PORT_SUSPEND):
959 WR_RH_PORTSTAT (RH_PS_POCI); OK (0);
960 case (RH_PORT_POWER):
961 WR_RH_PORTSTAT (RH_PS_LSDA); OK (0);
962 case (RH_C_PORT_CONNECTION):
963 WR_RH_PORTSTAT (RH_PS_CSC ); OK (0);
964 case (RH_C_PORT_ENABLE):
965 WR_RH_PORTSTAT (RH_PS_PESC); OK (0);
966 case (RH_C_PORT_SUSPEND):
967 WR_RH_PORTSTAT (RH_PS_PSSC); OK (0);
968 case (RH_C_PORT_OVER_CURRENT):
969 WR_RH_PORTSTAT (RH_PS_OCIC); OK (0);
970 case (RH_C_PORT_RESET):
971 WR_RH_PORTSTAT (RH_PS_PRSC); OK (0);
975 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
977 case (RH_PORT_SUSPEND):
978 WR_RH_PORTSTAT (RH_PS_PSS ); OK (0);
979 case (RH_PORT_RESET): /* BUG IN HUP CODE *********/
980 if (RD_RH_PORTSTAT & RH_PS_CCS)
981 WR_RH_PORTSTAT (RH_PS_PRS);
983 case (RH_PORT_POWER):
984 WR_RH_PORTSTAT (RH_PS_PPS ); OK (0);
985 case (RH_PORT_ENABLE): /* BUG IN HUP CODE *********/
986 if (RD_RH_PORTSTAT & RH_PS_CCS)
987 WR_RH_PORTSTAT (RH_PS_PES );
992 case RH_SET_ADDRESS: gohci.rh.devnum = wValue; OK(0);
994 case RH_GET_DESCRIPTOR:
995 switch ((wValue & 0xff00) >> 8) {
996 case (0x01): /* device descriptor */
997 len = min_t(unsigned int,
1000 sizeof (root_hub_dev_des),
1002 data_buf = root_hub_dev_des; OK(len);
1003 case (0x02): /* configuration descriptor */
1004 len = min_t(unsigned int,
1007 sizeof (root_hub_config_des),
1009 data_buf = root_hub_config_des; OK(len);
1010 case (0x03): /* string descriptors */
1011 if(wValue==0x0300) {
1012 len = min_t(unsigned int,
1015 sizeof (root_hub_str_index0),
1017 data_buf = root_hub_str_index0;
1020 if(wValue==0x0301) {
1021 len = min_t(unsigned int,
1024 sizeof (root_hub_str_index1),
1026 data_buf = root_hub_str_index1;
1030 stat = USB_ST_STALLED;
1034 case RH_GET_DESCRIPTOR | RH_CLASS:
1036 __u32 temp = roothub_a (&gohci);
1038 data_buf [0] = 9; /* min length; */
1039 data_buf [1] = 0x29;
1040 data_buf [2] = temp & RH_A_NDP;
1042 if (temp & RH_A_PSM) /* per-port power switching? */
1043 data_buf [3] |= 0x1;
1044 if (temp & RH_A_NOCP) /* no overcurrent reporting? */
1045 data_buf [3] |= 0x10;
1046 else if (temp & RH_A_OCPM) /* per-port overcurrent reporting? */
1047 data_buf [3] |= 0x8;
1049 /* corresponds to data_buf[4-7] */
1051 data_buf [5] = (temp & RH_A_POTPGT) >> 24;
1052 temp = roothub_b (&gohci);
1053 data_buf [7] = temp & RH_B_DR;
1054 if (data_buf [2] < 7) {
1055 data_buf [8] = 0xff;
1058 data_buf [8] = (temp & RH_B_DR) >> 8;
1059 data_buf [10] = data_buf [9] = 0xff;
1062 len = min_t(unsigned int, leni,
1063 min_t(unsigned int, data_buf [0], wLength));
1067 case RH_GET_CONFIGURATION: *(__u8 *) data_buf = 0x01; OK (1);
1069 case RH_SET_CONFIGURATION: WR_RH_STAT (0x10000); OK (0);
1072 dbg ("unsupported root hub command");
1073 stat = USB_ST_STALLED;
1077 ohci_dump_roothub (&gohci, 1);
1080 len = min_t(int, len, leni);
1081 if (data != data_buf)
1082 memcpy (data, data_buf, len);
1088 urb_priv.actual_length = transfer_len;
1089 pkt_print(dev, pipe, buffer, transfer_len, cmd, "RET(rh)", 0/*usb_pipein(pipe)*/);
1095 /*-------------------------------------------------------------------------*/
1097 /* common code for handling submit messages - used for all but root hub */
1099 int submit_common_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1100 int transfer_len, struct devrequest *setup, int interval)
1103 int maxsize = usb_maxpacket(dev, pipe);
1106 /* device pulled? Shortcut the action. */
1107 if (devgone == dev) {
1108 dev->status = USB_ST_CRC_ERR;
1113 urb_priv.actual_length = 0;
1114 pkt_print(dev, pipe, buffer, transfer_len, setup, "SUB", usb_pipein(pipe));
1117 err("submit_common_message: pipesize for pipe %lx is zero",
1122 if (sohci_submit_job(dev, pipe, buffer, transfer_len, setup, interval) < 0) {
1123 err("sohci_submit_job failed");
1127 /* allow more time for a BULK device to react - some are slow */
1128 #define BULK_TO 5000 /* timeout in milliseconds */
1129 if (usb_pipebulk(pipe))
1134 /* wait for it to complete */
1136 /* check whether the controller is done */
1137 stat = hc_interrupt();
1139 stat = USB_ST_CRC_ERR;
1143 /* NOTE: since we are not interrupt driven in U-Boot and always
1144 * handle only one URB at a time, we cannot assume the
1145 * transaction finished on the first successful return from
1146 * hc_interrupt().. unless the flag for current URB is set,
1147 * meaning that all TD's to/from device got actually
1148 * transferred and processed. If the current URB is not
1149 * finished we need to re-iterate this loop so as
1150 * hc_interrupt() gets called again as there needs to be some
1151 * more TD's to process still */
1152 if ((stat >= 0) && (stat != 0xff) && (urb_finished)) {
1153 /* 0xff is returned for an SF-interrupt */
1163 err("CTL:TIMEOUT ");
1164 dbg("submit_common_msg: TO status %x\n", stat);
1165 stat = USB_ST_CRC_ERR;
1171 /* we got an Root Hub Status Change interrupt */
1174 ohci_dump_roothub (&gohci, 1);
1178 timeout = rh_check_port_status(&gohci);
1180 #if 0 /* this does nothing useful, but leave it here in case that changes */
1181 /* the called routine adds 1 to the passed value */
1182 usb_hub_port_connect_change(gohci.rh.dev, timeout - 1);
1186 * This is potentially dangerous because it assumes
1187 * that only one device is ever plugged in!
1195 dev->act_len = transfer_len;
1198 pkt_print(dev, pipe, buffer, transfer_len, setup, "RET(ctlr)", usb_pipein(pipe));
1201 /* free TDs in urb_priv */
1202 urb_free_priv (&urb_priv);
1206 /* submit routines called from usb.c */
1207 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1210 info("submit_bulk_msg");
1211 return submit_common_msg(dev, pipe, buffer, transfer_len, NULL, 0);
1214 int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1215 int transfer_len, struct devrequest *setup)
1217 int maxsize = usb_maxpacket(dev, pipe);
1219 info("submit_control_msg");
1221 urb_priv.actual_length = 0;
1222 pkt_print(dev, pipe, buffer, transfer_len, setup, "SUB", usb_pipein(pipe));
1225 err("submit_control_message: pipesize for pipe %lx is zero",
1229 if (((pipe >> 8) & 0x7f) == gohci.rh.devnum) {
1231 /* root hub - redirect */
1232 return ohci_submit_rh_msg(dev, pipe, buffer, transfer_len,
1236 return submit_common_msg(dev, pipe, buffer, transfer_len, setup, 0);
1239 int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1240 int transfer_len, int interval)
1242 info("submit_int_msg");
1246 /*-------------------------------------------------------------------------*
1248 *-------------------------------------------------------------------------*/
1250 /* reset the HC and BUS */
1252 static int hc_reset (ohci_t *ohci)
1255 int smm_timeout = 50; /* 0,5 sec */
1257 if (readl (&ohci->regs->control) & OHCI_CTRL_IR) { /* SMM owns the HC */
1258 writel (OHCI_OCR, &ohci->regs->cmdstatus); /* request ownership */
1259 info("USB HC TakeOver from SMM");
1260 while (readl (&ohci->regs->control) & OHCI_CTRL_IR) {
1262 if (--smm_timeout == 0) {
1263 err("USB HC TakeOver failed!");
1269 /* Disable HC interrupts */
1270 writel (OHCI_INTR_MIE, &ohci->regs->intrdisable);
1272 dbg("USB HC reset_hc usb-%s: ctrl = 0x%X ;",
1274 readl (&ohci->regs->control));
1276 /* Reset USB (needed by some controllers) */
1277 ohci->hc_control = 0;
1278 writel (ohci->hc_control, &ohci->regs->control);
1280 /* HC Reset requires max 10 us delay */
1281 writel (OHCI_HCR, &ohci->regs->cmdstatus);
1282 while ((readl (&ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
1283 if (--timeout == 0) {
1284 err("USB HC reset timed out!");
1292 /*-------------------------------------------------------------------------*/
1294 /* Start an OHCI controller, set the BUS operational
1296 * connect the virtual root hub */
1298 static int hc_start (ohci_t * ohci)
1301 unsigned int fminterval;
1305 /* Tell the controller where the control and bulk lists are
1306 * The lists are empty now. */
1308 writel (0, &ohci->regs->ed_controlhead);
1309 writel (0, &ohci->regs->ed_bulkhead);
1311 writel ((__u32)ohci->hcca, &ohci->regs->hcca); /* a reset clears this */
1313 fminterval = 0x2edf;
1314 writel ((fminterval * 9) / 10, &ohci->regs->periodicstart);
1315 fminterval |= ((((fminterval - 210) * 6) / 7) << 16);
1316 writel (fminterval, &ohci->regs->fminterval);
1317 writel (0x628, &ohci->regs->lsthresh);
1319 /* start controller operations */
1320 ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
1322 writel (ohci->hc_control, &ohci->regs->control);
1324 /* disable all interrupts */
1325 mask = (OHCI_INTR_SO | OHCI_INTR_WDH | OHCI_INTR_SF | OHCI_INTR_RD |
1326 OHCI_INTR_UE | OHCI_INTR_FNO | OHCI_INTR_RHSC |
1327 OHCI_INTR_OC | OHCI_INTR_MIE);
1328 writel (mask, &ohci->regs->intrdisable);
1329 /* clear all interrupts */
1330 mask &= ~OHCI_INTR_MIE;
1331 writel (mask, &ohci->regs->intrstatus);
1332 /* Choose the interrupts we care about now - but w/o MIE */
1333 mask = OHCI_INTR_RHSC | OHCI_INTR_UE | OHCI_INTR_WDH | OHCI_INTR_SO;
1334 writel (mask, &ohci->regs->intrenable);
1337 /* required for AMD-756 and some Mac platforms */
1338 writel ((roothub_a (ohci) | RH_A_NPS) & ~RH_A_PSM,
1339 &ohci->regs->roothub.a);
1340 writel (RH_HS_LPSC, &ohci->regs->roothub.status);
1341 #endif /* OHCI_USE_NPS */
1343 /* POTPGT delay is bits 24-31, in 2 ms units. */
1344 mdelay ((roothub_a (ohci) >> 23) & 0x1fe);
1346 /* connect the virtual root hub */
1347 ohci->rh.devnum = 0;
1352 /*-------------------------------------------------------------------------*/
1354 /* an interrupt happens */
1359 ohci_t *ohci = &gohci;
1360 struct ohci_regs *regs = ohci->regs;
1364 if ((ohci->hcca->done_head != 0) &&
1365 !(ohci_cpu_to_le32(ohci->hcca->done_head) & 0x01)) {
1367 ints = OHCI_INTR_WDH;
1369 } else if ((ints = readl (®s->intrstatus)) == ~(u32)0) {
1371 err ("%s device removed!", ohci->slot_name);
1374 } else if ((ints &= readl (®s->intrenable)) == 0) {
1375 dbg("hc_interrupt: returning..\n");
1379 /* dbg("Interrupt: %x frame: %x", ints, le16_to_cpu (ohci->hcca->frame_no)); */
1381 if (ints & OHCI_INTR_RHSC) {
1386 if (ints & OHCI_INTR_UE) {
1388 err ("OHCI Unrecoverable Error, controller usb-%s disabled",
1390 /* e.g. due to PCI Master/Target Abort */
1393 ohci_dump (ohci, 1);
1395 /* FIXME: be optimistic, hope that bug won't repeat often. */
1396 /* Make some non-interrupt context restart the controller. */
1397 /* Count and limit the retries though; either hardware or */
1398 /* software errors can go forever... */
1403 if (ints & OHCI_INTR_WDH) {
1404 writel (OHCI_INTR_WDH, ®s->intrdisable);
1405 stat = dl_done_list (&gohci, dl_reverse_done_list (&gohci));
1406 writel (OHCI_INTR_WDH, ®s->intrenable);
1409 if (ints & OHCI_INTR_SO) {
1410 dbg("USB Schedule overrun\n");
1411 writel (OHCI_INTR_SO, ®s->intrenable);
1415 /* FIXME: this assumes SOF (1/ms) interrupts don't get lost... */
1416 if (ints & OHCI_INTR_SF) {
1417 unsigned int frame = ohci_cpu_to_le16 (ohci->hcca->frame_no) & 1;
1419 writel (OHCI_INTR_SF, ®s->intrdisable);
1420 if (ohci->ed_rm_list[frame] != NULL)
1421 writel (OHCI_INTR_SF, ®s->intrenable);
1425 writel (ints, ®s->intrstatus);
1429 /*-------------------------------------------------------------------------*/
1431 /*-------------------------------------------------------------------------*/
1433 /* De-allocate all resources.. */
1435 static void hc_release_ohci (ohci_t *ohci)
1437 dbg ("USB HC release ohci usb-%s", ohci->slot_name);
1439 if (!ohci->disabled)
1443 /*-------------------------------------------------------------------------*/
1446 * low level initalisation routine, called from usb.c
1448 static char ohci_inited = 0;
1450 int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
1453 /* Set the USB Clock */
1454 *(vu_long *)MPC5XXX_CDM_48_FDC = CONFIG_USB_CLOCK;
1456 #ifdef CONFIG_PSC3_USB /* USB is using the alternate configuration */
1457 /* remove all PSC3 USB bits first before ORing in ours */
1458 *(vu_long *)MPC5XXX_GPS_PORT_CONFIG &= ~0x00804f00;
1460 /* remove all USB bits first before ORing in ours */
1461 *(vu_long *)MPC5XXX_GPS_PORT_CONFIG &= ~0x00807000;
1463 /* Activate USB port */
1464 *(vu_long *)MPC5XXX_GPS_PORT_CONFIG |= CONFIG_USB_CONFIG;
1466 memset (&gohci, 0, sizeof (ohci_t));
1467 memset (&urb_priv, 0, sizeof (urb_priv_t));
1469 /* align the storage */
1470 if ((__u32)&ghcca[0] & 0xff) {
1471 err("HCCA not aligned!!");
1475 info("aligned ghcca %p", phcca);
1476 memset(&ohci_dev, 0, sizeof(struct ohci_device));
1477 if ((__u32)&ohci_dev.ed[0] & 0x7) {
1478 err("EDs not aligned!!");
1481 memset(gtd, 0, sizeof(td_t) * (NUM_TD + 1));
1482 if ((__u32)gtd & 0x7) {
1483 err("TDs not aligned!!");
1488 memset (phcca, 0, sizeof (struct ohci_hcca));
1493 gohci.regs = (struct ohci_regs *)MPC5XXX_USB;
1496 gohci.slot_name = "mpc5200";
1498 if (hc_reset (&gohci) < 0) {
1499 hc_release_ohci (&gohci);
1503 if (hc_start (&gohci) < 0) {
1504 err ("can't start usb-%s", gohci.slot_name);
1505 hc_release_ohci (&gohci);
1510 ohci_dump (&gohci, 1);
1518 int usb_lowlevel_stop(int index)
1520 /* this gets called really early - before the controller has */
1521 /* even been initialized! */
1524 /* TODO release any interrupts, etc. */
1525 /* call hc_release_ohci() here ? */
1530 #endif /* CONFIG_USB_OHCI */