2 * URB OHCI HCD (Host Controller Driver) for USB on the S3C2400.
5 * Gary Jennejohn, DENX Software Engineering <garyj@denx.de>
7 * Note: Much of this code has been derived from Linux 2.4
8 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
9 * (C) Copyright 2000-2002 David Brownell
11 * SPDX-License-Identifier: GPL-2.0+
15 * 1 - this driver is intended for use with USB Mass Storage Devices
16 * (BBB) ONLY. There is NO support for Interrupt or Isochronous pipes!
20 /* #include <pci.h> no PCI on the S3C24X0 */
22 #if defined(CONFIG_USB_OHCI) && defined(CONFIG_S3C24X0)
24 #include <asm/arch/s3c24x0_cpu.h>
28 #include "ohci-s3c24xx.h"
30 #define OHCI_USE_NPS /* force NoPowerSwitching mode */
31 #undef OHCI_VERBOSE_DEBUG /* not always helpful */
34 /* For initializing controller (mask in an HCFS mode too) */
35 #define OHCI_CONTROL_INIT \
36 (OHCI_CTRL_CBSR & 0x3) | OHCI_CTRL_IE | OHCI_CTRL_PLE
40 #define dbg(format, arg...) printf("DEBUG: " format "\n", ## arg)
42 #define dbg(format, arg...) do {} while(0)
44 #define err(format, arg...) printf("ERROR: " format "\n", ## arg)
47 #define info(format, arg...) printf("INFO: " format "\n", ## arg)
49 #define info(format, arg...) do {} while(0)
52 #define m16_swap(x) swap_16(x)
53 #define m32_swap(x) swap_32(x)
55 /* global struct ohci */
56 static struct ohci gohci;
57 /* this must be aligned to a 256 byte boundary */
58 struct ohci_hcca ghcca[1];
59 /* a pointer to the aligned storage */
60 struct ohci_hcca *phcca;
61 /* this allocates EDs for all possible endpoints */
62 struct ohci_device ohci_dev;
64 struct urb_priv urb_priv;
67 /* device which was disconnected */
68 struct usb_device *devgone;
69 /* flag guarding URB transation */
72 /*-------------------------------------------------------------------------*/
74 /* AMD-756 (D2 rev) reports corrupt register contents in some cases.
75 * The erratum (#4) description is incorrect. AMD's workaround waits
76 * till some bits (mostly reserved) are clear; ok for all revs.
78 #define OHCI_QUIRK_AMD756 0xabcd
79 #define read_roothub(hc, register, mask) ({ \
80 u32 temp = readl (&hc->regs->roothub.register); \
81 if (hc->flags & OHCI_QUIRK_AMD756) \
83 temp = readl (&hc->regs->roothub.register); \
86 static u32 roothub_a(struct ohci *hc)
88 return read_roothub(hc, a, 0xfc0fe000);
90 static inline u32 roothub_b(struct ohci *hc)
92 return readl(&hc->regs->roothub.b);
94 static inline u32 roothub_status(struct ohci *hc)
96 return readl(&hc->regs->roothub.status);
98 static u32 roothub_portstatus(struct ohci *hc, int i)
100 return read_roothub(hc, portstatus[i], 0xffe0fce0);
103 /* forward declaration */
104 static int hc_interrupt(void);
105 static void td_submit_job(struct usb_device *dev, unsigned long pipe,
106 void *buffer, int transfer_len,
107 struct devrequest *setup, struct urb_priv *urb,
110 /*-------------------------------------------------------------------------*
111 * URB support functions
112 *-------------------------------------------------------------------------*/
114 /* free HCD-private data associated with this URB */
116 static void urb_free_priv(struct urb_priv *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,
146 struct urb_priv *purb = &urb_priv;
148 dbg("%s URB:[%4x] dev:%2d,ep:%2d-%c,type:%s,len:%d/%d stat:%#lx",
150 sohci_get_current_frame_number(dev),
151 usb_pipedevice(pipe),
152 usb_pipeendpoint(pipe),
153 usb_pipeout(pipe) ? 'O' : 'I',
154 usb_pipetype(pipe) < 2 ?
155 (usb_pipeint(pipe) ? "INTR" : "ISOC") :
156 (usb_pipecontrol(pipe) ? "CTRL" : "BULK"),
157 purb->actual_length, transfer_len, dev->status);
158 #ifdef OHCI_VERBOSE_DEBUG
162 if (usb_pipecontrol(pipe)) {
163 printf(__FILE__ ": cmd(8):");
164 for (i = 0; i < 8; i++)
165 printf(" %02x", ((__u8 *) setup)[i]);
168 if (transfer_len > 0 && buffer) {
169 printf(__FILE__ ": data(%d/%d):",
170 purb->actual_length, transfer_len);
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
182 int ed tree inclusive iso eds*/
183 void ep_print_int_eds(struct ohci *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 struct ed *ed = (struct ed *) m32_swap(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" : "");
218 static void maybe_print_eds(char *label, __u32 value)
220 struct ed *edp = (struct ed *) value;
223 dbg("%s %08x", label, value);
224 dbg("%08x", edp->hwINFO);
225 dbg("%08x", edp->hwTailP);
226 dbg("%08x", edp->hwHeadP);
227 dbg("%08x", edp->hwNextED);
231 static char *hcfs2string(int state)
236 case OHCI_USB_RESUME:
239 return "operational";
240 case OHCI_USB_SUSPEND:
246 /* dump control and status registers */
247 static void ohci_dump_status(struct ohci *controller)
249 struct ohci_regs *regs = controller->regs;
252 temp = readl(®s->revision) & 0xff;
254 dbg("spec %d.%d", (temp >> 4), (temp & 0x0f));
256 temp = readl(®s->control);
257 dbg("control: 0x%08x%s%s%s HCFS=%s%s%s%s%s CBSR=%d", temp,
258 (temp & OHCI_CTRL_RWE) ? " RWE" : "",
259 (temp & OHCI_CTRL_RWC) ? " RWC" : "",
260 (temp & OHCI_CTRL_IR) ? " IR" : "",
261 hcfs2string(temp & OHCI_CTRL_HCFS),
262 (temp & OHCI_CTRL_BLE) ? " BLE" : "",
263 (temp & OHCI_CTRL_CLE) ? " CLE" : "",
264 (temp & OHCI_CTRL_IE) ? " IE" : "",
265 (temp & OHCI_CTRL_PLE) ? " PLE" : "", temp & OHCI_CTRL_CBSR);
267 temp = readl(®s->cmdstatus);
268 dbg("cmdstatus: 0x%08x SOC=%d%s%s%s%s", temp,
269 (temp & OHCI_SOC) >> 16,
270 (temp & OHCI_OCR) ? " OCR" : "",
271 (temp & OHCI_BLF) ? " BLF" : "",
272 (temp & OHCI_CLF) ? " CLF" : "", (temp & OHCI_HCR) ? " HCR" : "");
274 ohci_dump_intr_mask("intrstatus", readl(®s->intrstatus));
275 ohci_dump_intr_mask("intrenable", readl(®s->intrenable));
277 maybe_print_eds("ed_periodcurrent", readl(®s->ed_periodcurrent));
279 maybe_print_eds("ed_controlhead", readl(®s->ed_controlhead));
280 maybe_print_eds("ed_controlcurrent", readl(®s->ed_controlcurrent));
282 maybe_print_eds("ed_bulkhead", readl(®s->ed_bulkhead));
283 maybe_print_eds("ed_bulkcurrent", readl(®s->ed_bulkcurrent));
285 maybe_print_eds("donehead", readl(®s->donehead));
288 static void ohci_dump_roothub(struct ohci *controller, int verbose)
292 temp = roothub_a(controller);
293 ndp = (temp & RH_A_NDP);
296 dbg("roothub.a: %08x POTPGT=%d%s%s%s%s%s NDP=%d", temp,
297 ((temp & RH_A_POTPGT) >> 24) & 0xff,
298 (temp & RH_A_NOCP) ? " NOCP" : "",
299 (temp & RH_A_OCPM) ? " OCPM" : "",
300 (temp & RH_A_DT) ? " DT" : "",
301 (temp & RH_A_NPS) ? " NPS" : "",
302 (temp & RH_A_PSM) ? " PSM" : "", ndp);
303 temp = roothub_b(controller);
304 dbg("roothub.b: %08x PPCM=%04x DR=%04x",
305 temp, (temp & RH_B_PPCM) >> 16, (temp & RH_B_DR)
307 temp = roothub_status(controller);
308 dbg("roothub.status: %08x%s%s%s%s%s%s",
310 (temp & RH_HS_CRWE) ? " CRWE" : "",
311 (temp & RH_HS_OCIC) ? " OCIC" : "",
312 (temp & RH_HS_LPSC) ? " LPSC" : "",
313 (temp & RH_HS_DRWE) ? " DRWE" : "",
314 (temp & RH_HS_OCI) ? " OCI" : "",
315 (temp & RH_HS_LPS) ? " LPS" : "");
318 for (i = 0; i < ndp; i++) {
319 temp = roothub_portstatus(controller, i);
320 dbg("roothub.portstatus [%d] = 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s",
323 (temp & RH_PS_PRSC) ? " PRSC" : "",
324 (temp & RH_PS_OCIC) ? " OCIC" : "",
325 (temp & RH_PS_PSSC) ? " PSSC" : "",
326 (temp & RH_PS_PESC) ? " PESC" : "",
327 (temp & RH_PS_CSC) ? " CSC" : "",
328 (temp & RH_PS_LSDA) ? " LSDA" : "",
329 (temp & RH_PS_PPS) ? " PPS" : "",
330 (temp & RH_PS_PRS) ? " PRS" : "",
331 (temp & RH_PS_POCI) ? " POCI" : "",
332 (temp & RH_PS_PSS) ? " PSS" : "",
333 (temp & RH_PS_PES) ? " PES" : "",
334 (temp & RH_PS_CCS) ? " CCS" : "");
338 static void ohci_dump(struct ohci *controller, int verbose)
340 dbg("OHCI controller usb-%s state", controller->slot_name);
342 /* dumps some of the state we know about */
343 ohci_dump_status(controller);
345 ep_print_int_eds(controller, "hcca");
346 dbg("hcca frame #%04x", controller->hcca->frame_no);
347 ohci_dump_roothub(controller, 1);
352 /*-------------------------------------------------------------------------*
353 * Interface functions (URB)
354 *-------------------------------------------------------------------------*/
356 /* get a transfer request */
358 int sohci_submit_job(struct usb_device *dev, unsigned long pipe, void *buffer,
359 int transfer_len, struct devrequest *setup, int interval)
363 struct urb_priv *purb_priv;
368 /* when controller's hung, permit only roothub cleanup attempts
369 * such as powering down ports */
370 if (ohci->disabled) {
371 err("sohci_submit_job: EPIPE");
375 /* if we have an unfinished URB from previous transaction let's
376 * fail and scream as quickly as possible so as not to corrupt
377 * further communication */
379 err("sohci_submit_job: URB NOT FINISHED");
382 /* we're about to begin a new transaction here
383 so mark the URB unfinished */
386 /* every endpoint has a ed, locate and fill it */
387 ed = ep_add_ed(dev, pipe);
389 err("sohci_submit_job: ENOMEM");
393 /* for the private part of the URB we need the number of TDs (size) */
394 switch (usb_pipetype(pipe)) {
396 /* one TD for every 4096 Byte */
397 size = (transfer_len - 1) / 4096 + 1;
400 /* 1 TD for setup, 1 for ACK and 1 for every 4096 B */
401 size = (transfer_len == 0) ? 2 : (transfer_len - 1) / 4096 + 3;
405 if (size >= (N_URB_TD - 1)) {
406 err("need %d TDs, only have %d", size, N_URB_TD);
409 purb_priv = &urb_priv;
410 purb_priv->pipe = pipe;
412 /* fill the private part of the URB */
413 purb_priv->length = size;
415 purb_priv->actual_length = 0;
417 /* allocate the TDs */
418 /* note that td[0] was allocated in ep_add_ed */
419 for (i = 0; i < size; i++) {
420 purb_priv->td[i] = td_alloc(dev);
421 if (!purb_priv->td[i]) {
422 purb_priv->length = i;
423 urb_free_priv(purb_priv);
424 err("sohci_submit_job: ENOMEM");
429 if (ed->state == ED_NEW || (ed->state & ED_DEL)) {
430 urb_free_priv(purb_priv);
431 err("sohci_submit_job: EINVAL");
435 /* link the ed into a chain if is not already */
436 if (ed->state != ED_OPER)
439 /* fill the TDs and link it to the ed */
440 td_submit_job(dev, pipe, buffer, transfer_len, setup, purb_priv,
446 /*-------------------------------------------------------------------------*/
449 /* tell us the current USB frame number */
451 static int sohci_get_current_frame_number(struct usb_device *usb_dev)
453 struct ohci *ohci = &gohci;
455 return m16_swap(ohci->hcca->frame_no);
459 /*-------------------------------------------------------------------------*
460 * ED handling functions
461 *-------------------------------------------------------------------------*/
463 /* link an ed into one of the HC chains */
465 static int ep_link(struct ohci *ohci, struct ed *edi)
474 if (ohci->ed_controltail == NULL) {
475 writel((u32)ed, &ohci->regs->ed_controlhead);
477 ohci->ed_controltail->hwNextED = (__u32) m32_swap(ed);
479 ed->ed_prev = ohci->ed_controltail;
480 if (!ohci->ed_controltail && !ohci->ed_rm_list[0] &&
481 !ohci->ed_rm_list[1] && !ohci->sleeping) {
482 ohci->hc_control |= OHCI_CTRL_CLE;
483 writel(ohci->hc_control, &ohci->regs->control);
485 ohci->ed_controltail = edi;
490 if (ohci->ed_bulktail == NULL) {
491 writel((u32)ed, &ohci->regs->ed_bulkhead);
493 ohci->ed_bulktail->hwNextED = (__u32) m32_swap(ed);
495 ed->ed_prev = ohci->ed_bulktail;
496 if (!ohci->ed_bulktail && !ohci->ed_rm_list[0] &&
497 !ohci->ed_rm_list[1] && !ohci->sleeping) {
498 ohci->hc_control |= OHCI_CTRL_BLE;
499 writel(ohci->hc_control, &ohci->regs->control);
501 ohci->ed_bulktail = edi;
507 /*-------------------------------------------------------------------------*/
509 /* unlink an ed from one of the HC chains.
510 * just the link to the ed is unlinked.
511 * the link from the ed still points to another operational ed or 0
512 * so the HC can eventually finish the processing of the unlinked ed */
514 static int ep_unlink(struct ohci *ohci, struct ed *ed)
517 ed->hwINFO |= m32_swap(OHCI_ED_SKIP);
521 if (ed->ed_prev == NULL) {
523 ohci->hc_control &= ~OHCI_CTRL_CLE;
524 writel(ohci->hc_control, &ohci->regs->control);
526 writel(m32_swap(*((__u32 *) &ed->hwNextED)),
527 &ohci->regs->ed_controlhead);
529 ed->ed_prev->hwNextED = ed->hwNextED;
531 if (ohci->ed_controltail == ed) {
532 ohci->ed_controltail = ed->ed_prev;
534 next = (struct ed *)m32_swap(*((__u32 *)&ed->hwNextED));
535 next->ed_prev = ed->ed_prev;
540 if (ed->ed_prev == NULL) {
542 ohci->hc_control &= ~OHCI_CTRL_BLE;
543 writel(ohci->hc_control, &ohci->regs->control);
545 writel(m32_swap(*((__u32 *) &ed->hwNextED)),
546 &ohci->regs->ed_bulkhead);
548 ed->ed_prev->hwNextED = ed->hwNextED;
550 if (ohci->ed_bulktail == ed) {
551 ohci->ed_bulktail = ed->ed_prev;
553 next = (struct ed *)m32_swap(*((__u32 *)&ed->hwNextED));
554 next->ed_prev = ed->ed_prev;
558 ed->state = ED_UNLINK;
562 /*-------------------------------------------------------------------------*/
564 /* add/reinit an endpoint; this should be done once at the usb_set_configuration
565 * command, but the USB stack is a little bit stateless so we do it at every
566 * transaction. If the state of the ed is ED_NEW then a dummy td is added and
567 * the state is changed to ED_UNLINK. In all other cases the state is left
568 * unchanged. The ed info fields are setted anyway even though most of them
569 * should not change */
571 static struct ed *ep_add_ed(struct usb_device *usb_dev, unsigned long pipe)
577 ed = ed_ret = &ohci_dev.ed[(usb_pipeendpoint(pipe) << 1) |
578 (usb_pipecontrol(pipe) ? 0 :
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 = m32_swap(OHCI_ED_SKIP); /* skip ed */
589 /* dummy td; end of td list for ed */
590 td = td_alloc(usb_dev);
591 ed->hwTailP = (__u32) m32_swap(td);
592 ed->hwHeadP = ed->hwTailP;
593 ed->state = ED_UNLINK;
594 ed->type = usb_pipetype(pipe);
598 ed->hwINFO = m32_swap(usb_pipedevice(pipe)
599 | usb_pipeendpoint(pipe) << 7
600 | (usb_pipeisoc(pipe) ? 0x8000 : 0)
601 | (usb_pipecontrol(pipe) ? 0 :
602 (usb_pipeout(pipe) ? 0x800 : 0x1000))
603 | (usb_dev->speed == USB_SPEED_LOW) << 13 |
604 usb_maxpacket(usb_dev, pipe) << 16);
609 /*-------------------------------------------------------------------------*
610 * TD handling functions
611 *-------------------------------------------------------------------------*/
613 /* enqueue next TD for this URB (OHCI spec 5.2.8.2) */
615 static void td_fill(struct ohci *ohci, unsigned int info, void *data, int len,
616 struct usb_device *dev, int index,
617 struct urb_priv *urb_priv)
619 struct td *td, *td_pt;
620 #ifdef OHCI_FILL_TRACE
624 if (index > urb_priv->length) {
625 err("index > length");
628 /* use this td as the next dummy */
629 td_pt = urb_priv->td[index];
632 /* fill the old dummy TD */
633 td = urb_priv->td[index] =
634 (struct td *) (m32_swap(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,
644 ((unsigned char *)td->data)[i]);
651 td->hwINFO = (__u32) m32_swap(info);
652 td->hwCBP = (__u32) m32_swap(data);
654 td->hwBE = (__u32) m32_swap(data + len - 1);
657 td->hwNextTD = (__u32) m32_swap(td_pt);
659 /* append to queue */
660 td->ed->hwTailP = td->hwNextTD;
663 /*-------------------------------------------------------------------------*/
665 /* prepare all TDs of a transfer */
667 static void td_submit_job(struct usb_device *dev, unsigned long pipe,
668 void *buffer, int transfer_len,
669 struct devrequest *setup, struct urb_priv *urb,
672 struct ohci *ohci = &gohci;
673 int data_len = transfer_len;
677 unsigned int toggle = 0;
679 /* OHCI handles the DATA-toggles itself, we just
680 use the USB-toggle bits for resetting */
681 if (usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe))) {
682 toggle = TD_T_TOGGLE;
685 usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe),
694 switch (usb_pipetype(pipe)) {
696 info = usb_pipeout(pipe) ? TD_CC | TD_DP_OUT : TD_CC | TD_DP_IN;
697 while (data_len > 4096) {
698 td_fill(ohci, info | (cnt ? TD_T_TOGGLE : toggle), data,
699 4096, dev, cnt, urb);
704 info = usb_pipeout(pipe) ?
706 TD_CC | TD_R | TD_DP_IN;
707 td_fill(ohci, info | (cnt ? TD_T_TOGGLE : toggle), data,
708 data_len, dev, cnt, urb);
712 /* start bulk list */
713 writel(OHCI_BLF, &ohci->regs->cmdstatus);
717 info = TD_CC | TD_DP_SETUP | TD_T_DATA0;
718 td_fill(ohci, info, setup, 8, dev, cnt++, urb);
720 info = usb_pipeout(pipe) ?
721 TD_CC | TD_R | TD_DP_OUT | TD_T_DATA1 :
722 TD_CC | TD_R | TD_DP_IN | TD_T_DATA1;
723 /* NOTE: mishandles transfers >8K, some >4K */
724 td_fill(ohci, info, data, data_len, dev, cnt++, urb);
726 info = usb_pipeout(pipe) ?
727 TD_CC | TD_DP_IN | TD_T_DATA1 :
728 TD_CC | TD_DP_OUT | TD_T_DATA1;
729 td_fill(ohci, info, data, 0, dev, cnt++, urb);
731 /* start Control list */
732 writel(OHCI_CLF, &ohci->regs->cmdstatus);
735 if (urb->length != cnt)
736 dbg("TD LENGTH %d != CNT %d", urb->length, cnt);
739 /*-------------------------------------------------------------------------*
740 * Done List handling functions
741 *-------------------------------------------------------------------------*/
744 /* calculate the transfer length and update the urb */
746 static void dl_transfer_length(struct td *td)
749 struct urb_priv *lurb_priv = &urb_priv;
751 tdBE = m32_swap(td->hwBE);
752 tdCBP = m32_swap(td->hwCBP);
754 if (!(usb_pipecontrol(lurb_priv->pipe) &&
755 ((td->index == 0) || (td->index == lurb_priv->length - 1)))) {
758 lurb_priv->actual_length += tdBE - td->data + 1;
760 lurb_priv->actual_length += tdCBP - td->data;
765 /*-------------------------------------------------------------------------*/
767 /* replies to the request have to be on a FIFO basis so
768 * we reverse the reversed done-list */
770 static struct td *dl_reverse_done_list(struct ohci *ohci)
774 struct td *td_rev = NULL;
775 struct td *td_list = NULL;
776 struct urb_priv *lurb_priv = NULL;
778 td_list_hc = m32_swap(ohci->hcca->done_head) & 0xfffffff0;
779 ohci->hcca->done_head = 0;
782 td_list = (struct td *) td_list_hc;
784 if (TD_CC_GET(m32_swap(td_list->hwINFO))) {
785 lurb_priv = &urb_priv;
786 dbg(" USB-error/status: %x : %p",
787 TD_CC_GET(m32_swap(td_list->hwINFO)), td_list);
788 if (td_list->ed->hwHeadP & m32_swap(0x1)) {
790 ((td_list->index+1) < lurb_priv->length)) {
791 tmp = lurb_priv->length - 1;
792 td_list->ed->hwHeadP =
793 (lurb_priv->td[tmp]->hwNextTD &
794 m32_swap(0xfffffff0)) |
795 (td_list->ed->hwHeadP &
797 lurb_priv->td_cnt += lurb_priv->length -
800 td_list->ed->hwHeadP &=
801 m32_swap(0xfffffff2);
805 td_list->next_dl_td = td_rev;
807 td_list_hc = m32_swap(td_list->hwNextTD) & 0xfffffff0;
813 /*-------------------------------------------------------------------------*/
816 static int dl_done_list(struct ohci *ohci, struct td *td_list)
818 struct td *td_list_next = NULL;
823 struct urb_priv *lurb_priv;
824 __u32 tdINFO, edHeadP, edTailP;
827 td_list_next = td_list->next_dl_td;
829 lurb_priv = &urb_priv;
830 tdINFO = m32_swap(td_list->hwINFO);
834 dl_transfer_length(td_list);
836 /* error code of transfer */
837 cc = TD_CC_GET(tdINFO);
839 dbg("ConditionCode %#x", cc);
840 stat = cc_to_error[cc];
843 /* see if this done list makes for all TD's of current URB,
844 * and mark the URB finished if so */
845 if (++(lurb_priv->td_cnt) == lurb_priv->length) {
846 if ((ed->state & (ED_OPER | ED_UNLINK)))
849 dbg("dl_done_list: strange.., ED state %x, "
852 dbg("dl_done_list: processing TD %x, len %x\n",
853 lurb_priv->td_cnt, lurb_priv->length);
855 if (ed->state != ED_NEW) {
856 edHeadP = m32_swap(ed->hwHeadP) & 0xfffffff0;
857 edTailP = m32_swap(ed->hwTailP);
859 /* unlink eds if they are not busy */
860 if ((edHeadP == edTailP) && (ed->state == ED_OPER))
864 td_list = td_list_next;
869 /*-------------------------------------------------------------------------*
871 *-------------------------------------------------------------------------*/
873 #include <usbroothubdes.h>
875 /* Hub class-specific descriptor is constructed dynamically */
878 /*-------------------------------------------------------------------------*/
880 #define OK(x) len = (x); break
882 #define WR_RH_STAT(x) \
884 info("WR:status %#8x", (x)); \
885 writel((x), &gohci.regs->roothub.status); \
887 #define WR_RH_PORTSTAT(x) \
889 info("WR:portstatus[%d] %#8x", wIndex-1, (x)); \
890 writel((x), &gohci.regs->roothub.portstatus[wIndex-1]); \
893 #define WR_RH_STAT(x) \
894 writel((x), &gohci.regs->roothub.status)
895 #define WR_RH_PORTSTAT(x)\
896 writel((x), &gohci.regs->roothub.portstatus[wIndex-1])
898 #define RD_RH_STAT roothub_status(&gohci)
899 #define RD_RH_PORTSTAT roothub_portstatus(&gohci, wIndex-1)
901 /* request to virtual root hub */
903 int rh_check_port_status(struct ohci *controller)
909 temp = roothub_a(controller);
910 ndp = (temp & RH_A_NDP);
911 for (i = 0; i < ndp; i++) {
912 temp = roothub_portstatus(controller, i);
913 /* check for a device disconnect */
914 if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
915 (RH_PS_PESC | RH_PS_CSC)) && ((temp & RH_PS_CCS) == 0)) {
923 static int ohci_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
924 void *buffer, int transfer_len,
925 struct devrequest *cmd)
928 int leni = transfer_len;
936 __u8 *data_buf = datab.byte;
943 urb_priv.actual_length = 0;
944 pkt_print(dev, pipe, buffer, transfer_len, cmd, "SUB(rh)",
949 if (usb_pipeint(pipe)) {
950 info("Root-Hub submit IRQ: NOT implemented");
954 bmRType_bReq = cmd->requesttype | (cmd->request << 8);
955 wValue = m16_swap(cmd->value);
956 wIndex = m16_swap(cmd->index);
957 wLength = m16_swap(cmd->length);
959 info("Root-Hub: adr: %2x cmd(%1x): %08x %04x %04x %04x",
960 dev->devnum, 8, bmRType_bReq, wValue, wIndex, wLength);
962 switch (bmRType_bReq) {
963 /* Request Destination:
964 without flags: Device,
965 RH_INTERFACE: interface,
966 RH_ENDPOINT: endpoint,
967 RH_CLASS means HUB here,
968 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
972 datab.hword[0] = m16_swap(1);
974 case RH_GET_STATUS | RH_INTERFACE:
975 datab.hword[0] = m16_swap(0);
977 case RH_GET_STATUS | RH_ENDPOINT:
978 datab.hword[0] = m16_swap(0);
980 case RH_GET_STATUS | RH_CLASS:
982 m32_swap(RD_RH_STAT & ~(RH_HS_CRWE | RH_HS_DRWE));
984 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
985 datab.word[0] = m32_swap(RD_RH_PORTSTAT);
988 case RH_CLEAR_FEATURE | RH_ENDPOINT:
990 case (RH_ENDPOINT_STALL):
995 case RH_CLEAR_FEATURE | RH_CLASS:
997 case RH_C_HUB_LOCAL_POWER:
999 case (RH_C_HUB_OVER_CURRENT):
1000 WR_RH_STAT(RH_HS_OCIC);
1005 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1007 case (RH_PORT_ENABLE):
1008 WR_RH_PORTSTAT(RH_PS_CCS);
1010 case (RH_PORT_SUSPEND):
1011 WR_RH_PORTSTAT(RH_PS_POCI);
1013 case (RH_PORT_POWER):
1014 WR_RH_PORTSTAT(RH_PS_LSDA);
1016 case (RH_C_PORT_CONNECTION):
1017 WR_RH_PORTSTAT(RH_PS_CSC);
1019 case (RH_C_PORT_ENABLE):
1020 WR_RH_PORTSTAT(RH_PS_PESC);
1022 case (RH_C_PORT_SUSPEND):
1023 WR_RH_PORTSTAT(RH_PS_PSSC);
1025 case (RH_C_PORT_OVER_CURRENT):
1026 WR_RH_PORTSTAT(RH_PS_OCIC);
1028 case (RH_C_PORT_RESET):
1029 WR_RH_PORTSTAT(RH_PS_PRSC);
1034 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1036 case (RH_PORT_SUSPEND):
1037 WR_RH_PORTSTAT(RH_PS_PSS);
1039 case (RH_PORT_RESET): /* BUG IN HUP CODE ******** */
1040 if (RD_RH_PORTSTAT & RH_PS_CCS)
1041 WR_RH_PORTSTAT(RH_PS_PRS);
1043 case (RH_PORT_POWER):
1044 WR_RH_PORTSTAT(RH_PS_PPS);
1046 case (RH_PORT_ENABLE): /* BUG IN HUP CODE ******** */
1047 if (RD_RH_PORTSTAT & RH_PS_CCS)
1048 WR_RH_PORTSTAT(RH_PS_PES);
1053 case RH_SET_ADDRESS:
1054 gohci.rh.devnum = wValue;
1057 case RH_GET_DESCRIPTOR:
1058 switch ((wValue & 0xff00) >> 8) {
1059 case (0x01): /* device descriptor */
1060 len = min_t(unsigned int,
1063 sizeof(root_hub_dev_des), wLength));
1064 data_buf = root_hub_dev_des;
1066 case (0x02): /* configuration descriptor */
1067 len = min_t(unsigned int,
1070 sizeof(root_hub_config_des),
1072 data_buf = root_hub_config_des;
1074 case (0x03): /* string descriptors */
1075 if (wValue == 0x0300) {
1076 len = min_t(unsigned int,
1079 sizeof(root_hub_str_index0),
1081 data_buf = root_hub_str_index0;
1084 if (wValue == 0x0301) {
1085 len = min_t(unsigned int,
1088 sizeof(root_hub_str_index1),
1090 data_buf = root_hub_str_index1;
1094 stat = USB_ST_STALLED;
1098 case RH_GET_DESCRIPTOR | RH_CLASS:
1100 __u32 temp = roothub_a(&gohci);
1102 data_buf[0] = 9; /* min length; */
1104 data_buf[2] = temp & RH_A_NDP;
1106 if (temp & RH_A_PSM)
1107 /* per-port power switching? */
1109 if (temp & RH_A_NOCP)
1110 /* no overcurrent reporting? */
1111 data_buf[3] |= 0x10;
1112 else if (temp & RH_A_OCPM)
1113 /* per-port overcurrent reporting? */
1116 /* corresponds to data_buf[4-7] */
1118 data_buf[5] = (temp & RH_A_POTPGT) >> 24;
1119 temp = roothub_b(&gohci);
1120 data_buf[7] = temp & RH_B_DR;
1121 if (data_buf[2] < 7) {
1125 data_buf[8] = (temp & RH_B_DR) >> 8;
1126 data_buf[10] = data_buf[9] = 0xff;
1129 len = min_t(unsigned int, leni,
1130 min_t(unsigned int, data_buf[0], wLength));
1134 case RH_GET_CONFIGURATION:
1135 *(__u8 *) data_buf = 0x01;
1138 case RH_SET_CONFIGURATION:
1139 WR_RH_STAT(0x10000);
1143 dbg("unsupported root hub command");
1144 stat = USB_ST_STALLED;
1148 ohci_dump_roothub(&gohci, 1);
1153 len = min_t(int, len, leni);
1154 if (data != data_buf)
1155 memcpy(data, data_buf, len);
1161 urb_priv.actual_length = transfer_len;
1162 pkt_print(dev, pipe, buffer, transfer_len, cmd, "RET(rh)",
1163 0 /*usb_pipein(pipe) */);
1171 /*-------------------------------------------------------------------------*/
1173 /* common code for handling submit messages - used for all but root hub */
1175 int submit_common_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1176 int transfer_len, struct devrequest *setup, int interval)
1179 int maxsize = usb_maxpacket(dev, pipe);
1182 /* device pulled? Shortcut the action. */
1183 if (devgone == dev) {
1184 dev->status = USB_ST_CRC_ERR;
1188 urb_priv.actual_length = 0;
1189 pkt_print(dev, pipe, buffer, transfer_len, setup, "SUB",
1195 err("submit_common_message: pipesize for pipe %lx is zero",
1200 if (sohci_submit_job(dev, pipe, buffer, transfer_len, setup, interval) <
1202 err("sohci_submit_job failed");
1207 /* ohci_dump_status(&gohci); */
1209 /* allow more time for a BULK device to react - some are slow */
1210 #define BULK_TO 5000 /* timeout in milliseconds */
1211 if (usb_pipebulk(pipe))
1216 /* wait for it to complete */
1218 /* check whether the controller is done */
1219 stat = hc_interrupt();
1222 stat = USB_ST_CRC_ERR;
1226 /* NOTE: since we are not interrupt driven in U-Boot and always
1227 * handle only one URB at a time, we cannot assume the
1228 * transaction finished on the first successful return from
1229 * hc_interrupt().. unless the flag for current URB is set,
1230 * meaning that all TD's to/from device got actually
1231 * transferred and processed. If the current URB is not
1232 * finished we need to re-iterate this loop so as
1233 * hc_interrupt() gets called again as there needs to be some
1234 * more TD's to process still */
1235 if ((stat >= 0) && (stat != 0xff) && (urb_finished)) {
1236 /* 0xff is returned for an SF-interrupt */
1246 err("CTL:TIMEOUT ");
1247 dbg("submit_common_msg: TO status %x\n", stat);
1248 stat = USB_ST_CRC_ERR;
1255 /* we got an Root Hub Status Change interrupt */
1258 ohci_dump_roothub(&gohci, 1);
1262 timeout = rh_check_port_status(&gohci);
1264 #if 0 /* this does nothing useful, but leave it here
1265 in case that changes */
1266 /* the called routine adds 1 to the passed value */
1267 usb_hub_port_connect_change(gohci.rh.dev, timeout - 1);
1271 * This is potentially dangerous because it assumes
1272 * that only one device is ever plugged in!
1280 dev->act_len = transfer_len;
1283 pkt_print(dev, pipe, buffer, transfer_len, setup, "RET(ctlr)",
1289 /* free TDs in urb_priv */
1290 urb_free_priv(&urb_priv);
1294 /* submit routines called from usb.c */
1295 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1298 info("submit_bulk_msg");
1299 return submit_common_msg(dev, pipe, buffer, transfer_len, NULL, 0);
1302 int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1303 int transfer_len, struct devrequest *setup)
1305 int maxsize = usb_maxpacket(dev, pipe);
1307 info("submit_control_msg");
1309 urb_priv.actual_length = 0;
1310 pkt_print(dev, pipe, buffer, transfer_len, setup, "SUB",
1316 err("submit_control_message: pipesize for pipe %lx is zero",
1320 if (((pipe >> 8) & 0x7f) == gohci.rh.devnum) {
1322 /* root hub - redirect */
1323 return ohci_submit_rh_msg(dev, pipe, buffer, transfer_len,
1327 return submit_common_msg(dev, pipe, buffer, transfer_len, setup, 0);
1330 int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1331 int transfer_len, int interval)
1333 info("submit_int_msg");
1337 /*-------------------------------------------------------------------------*
1339 *-------------------------------------------------------------------------*/
1341 /* reset the HC and BUS */
1343 static int hc_reset(struct ohci *ohci)
1346 int smm_timeout = 50; /* 0,5 sec */
1348 if (readl(&ohci->regs->control) & OHCI_CTRL_IR) {
1349 /* SMM owns the HC - request ownership */
1350 writel(OHCI_OCR, &ohci->regs->cmdstatus);
1351 info("USB HC TakeOver from SMM");
1352 while (readl(&ohci->regs->control) & OHCI_CTRL_IR) {
1354 if (--smm_timeout == 0) {
1355 err("USB HC TakeOver failed!");
1361 /* Disable HC interrupts */
1362 writel(OHCI_INTR_MIE, &ohci->regs->intrdisable);
1364 dbg("USB HC reset_hc usb-%s: ctrl = 0x%X ;",
1365 ohci->slot_name, readl(&ohci->regs->control));
1367 /* Reset USB (needed by some controllers) */
1368 writel(0, &ohci->regs->control);
1370 /* HC Reset requires max 10 us delay */
1371 writel(OHCI_HCR, &ohci->regs->cmdstatus);
1372 while ((readl(&ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
1373 if (--timeout == 0) {
1374 err("USB HC reset timed out!");
1382 /*-------------------------------------------------------------------------*/
1384 /* Start an OHCI controller, set the BUS operational
1386 * connect the virtual root hub */
1388 static int hc_start(struct ohci *ohci)
1391 unsigned int fminterval;
1395 /* Tell the controller where the control and bulk lists are
1396 * The lists are empty now. */
1398 writel(0, &ohci->regs->ed_controlhead);
1399 writel(0, &ohci->regs->ed_bulkhead);
1401 /* a reset clears this */
1402 writel((__u32) ohci->hcca, &ohci->regs->hcca);
1404 fminterval = 0x2edf;
1405 writel((fminterval * 9) / 10, &ohci->regs->periodicstart);
1406 fminterval |= ((((fminterval - 210) * 6) / 7) << 16);
1407 writel(fminterval, &ohci->regs->fminterval);
1408 writel(0x628, &ohci->regs->lsthresh);
1410 /* start controller operations */
1411 ohci->hc_control = OHCI_CONTROL_INIT | OHCI_USB_OPER;
1413 writel(ohci->hc_control, &ohci->regs->control);
1415 /* disable all interrupts */
1416 mask = (OHCI_INTR_SO | OHCI_INTR_WDH | OHCI_INTR_SF | OHCI_INTR_RD |
1417 OHCI_INTR_UE | OHCI_INTR_FNO | OHCI_INTR_RHSC |
1418 OHCI_INTR_OC | OHCI_INTR_MIE);
1419 writel(mask, &ohci->regs->intrdisable);
1420 /* clear all interrupts */
1421 mask &= ~OHCI_INTR_MIE;
1422 writel(mask, &ohci->regs->intrstatus);
1423 /* Choose the interrupts we care about now - but w/o MIE */
1424 mask = OHCI_INTR_RHSC | OHCI_INTR_UE | OHCI_INTR_WDH | OHCI_INTR_SO;
1425 writel(mask, &ohci->regs->intrenable);
1428 /* required for AMD-756 and some Mac platforms */
1429 writel((roothub_a(ohci) | RH_A_NPS) & ~RH_A_PSM,
1430 &ohci->regs->roothub.a);
1431 writel(RH_HS_LPSC, &ohci->regs->roothub.status);
1432 #endif /* OHCI_USE_NPS */
1434 /* POTPGT delay is bits 24-31, in 2 ms units. */
1435 mdelay((roothub_a(ohci) >> 23) & 0x1fe);
1437 /* connect the virtual root hub */
1438 ohci->rh.devnum = 0;
1443 /*-------------------------------------------------------------------------*/
1445 /* an interrupt happens */
1447 static int hc_interrupt(void)
1449 struct ohci *ohci = &gohci;
1450 struct ohci_regs *regs = ohci->regs;
1454 if ((ohci->hcca->done_head != 0) &&
1455 !(m32_swap(ohci->hcca->done_head) & 0x01)) {
1457 ints = OHCI_INTR_WDH;
1460 ints = readl(®s->intrstatus);
1461 if (ints == ~(u32) 0) {
1463 err("%s device removed!", ohci->slot_name);
1466 ints &= readl(®s->intrenable);
1468 dbg("hc_interrupt: returning..\n");
1473 /* dbg("Interrupt: %x frame: %x", ints,
1474 le16_to_cpu(ohci->hcca->frame_no)); */
1476 if (ints & OHCI_INTR_RHSC) {
1481 if (ints & OHCI_INTR_UE) {
1483 err("OHCI Unrecoverable Error, controller usb-%s disabled",
1485 /* e.g. due to PCI Master/Target Abort */
1492 /* FIXME: be optimistic, hope that bug won't repeat often. */
1493 /* Make some non-interrupt context restart the controller. */
1494 /* Count and limit the retries though; either hardware or */
1495 /* software errors can go forever... */
1500 if (ints & OHCI_INTR_WDH) {
1503 writel(OHCI_INTR_WDH, ®s->intrdisable);
1504 stat = dl_done_list(&gohci, dl_reverse_done_list(&gohci));
1505 writel(OHCI_INTR_WDH, ®s->intrenable);
1508 if (ints & OHCI_INTR_SO) {
1509 dbg("USB Schedule overrun\n");
1510 writel(OHCI_INTR_SO, ®s->intrenable);
1514 /* FIXME: this assumes SOF (1/ms) interrupts don't get lost... */
1515 if (ints & OHCI_INTR_SF) {
1516 unsigned int frame = m16_swap(ohci->hcca->frame_no) & 1;
1518 writel(OHCI_INTR_SF, ®s->intrdisable);
1519 if (ohci->ed_rm_list[frame] != NULL)
1520 writel(OHCI_INTR_SF, ®s->intrenable);
1524 writel(ints, ®s->intrstatus);
1528 /*-------------------------------------------------------------------------*/
1530 /*-------------------------------------------------------------------------*/
1532 /* De-allocate all resources.. */
1534 static void hc_release_ohci(struct ohci *ohci)
1536 dbg("USB HC release ohci usb-%s", ohci->slot_name);
1538 if (!ohci->disabled)
1542 /*-------------------------------------------------------------------------*/
1545 * low level initalisation routine, called from usb.c
1547 static char ohci_inited = 0;
1549 int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
1551 struct s3c24x0_clock_power *clk_power = s3c24x0_get_base_clock_power();
1552 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
1555 * Set the 48 MHz UPLL clocking. Values are taken from
1556 * "PLL value selection guide", 6-23, s3c2400_UM.pdf.
1558 clk_power->upllcon = ((40 << 12) + (1 << 4) + 2);
1559 gpio->misccr |= 0x8; /* 1 = use pads related USB for USB host */
1562 * Enable USB host clock.
1564 clk_power->clkcon |= (1 << 4);
1566 memset(&gohci, 0, sizeof(struct ohci));
1567 memset(&urb_priv, 0, sizeof(struct urb_priv));
1569 /* align the storage */
1570 if ((__u32) &ghcca[0] & 0xff) {
1571 err("HCCA not aligned!!");
1575 info("aligned ghcca %p", phcca);
1576 memset(&ohci_dev, 0, sizeof(struct ohci_device));
1577 if ((__u32) &ohci_dev.ed[0] & 0x7) {
1578 err("EDs not aligned!!");
1581 memset(gtd, 0, sizeof(struct td) * (NUM_TD + 1));
1582 if ((__u32) gtd & 0x7) {
1583 err("TDs not aligned!!");
1588 memset(phcca, 0, sizeof(struct ohci_hcca));
1593 gohci.regs = (struct ohci_regs *)S3C24X0_USB_HOST_BASE;
1596 gohci.slot_name = "s3c2400";
1598 if (hc_reset(&gohci) < 0) {
1599 hc_release_ohci(&gohci);
1600 /* Initialization failed */
1601 clk_power->clkcon &= ~(1 << 4);
1605 /* FIXME this is a second HC reset; why?? */
1606 gohci.hc_control = OHCI_USB_RESET;
1607 writel(gohci.hc_control, &gohci.regs->control);
1610 if (hc_start(&gohci) < 0) {
1611 err("can't start usb-%s", gohci.slot_name);
1612 hc_release_ohci(&gohci);
1613 /* Initialization failed */
1614 clk_power->clkcon &= ~(1 << 4);
1618 ohci_dump(&gohci, 1);
1628 int usb_lowlevel_stop(int index)
1630 struct s3c24x0_clock_power *clk_power = s3c24x0_get_base_clock_power();
1632 /* this gets called really early - before the controller has */
1633 /* even been initialized! */
1636 /* TODO release any interrupts, etc. */
1637 /* call hc_release_ohci() here ? */
1639 /* may not want to do this */
1640 clk_power->clkcon &= ~(1 << 4);
1644 #endif /* defined(CONFIG_USB_OHCI) && defined(CONFIG_S3C24X0) */
1646 #if defined(CONFIG_USB_OHCI_NEW) && \
1647 defined(CONFIG_SYS_USB_OHCI_CPU_INIT) && \
1648 defined(CONFIG_S3C24X0)
1650 int usb_cpu_init(void)
1652 struct s3c24x0_clock_power *clk_power = s3c24x0_get_base_clock_power();
1653 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
1656 * Set the 48 MHz UPLL clocking. Values are taken from
1657 * "PLL value selection guide", 6-23, s3c2400_UM.pdf.
1659 writel((40 << 12) + (1 << 4) + 2, &clk_power->upllcon);
1660 /* 1 = use pads related USB for USB host */
1661 writel(readl(&gpio->misccr) | 0x8, &gpio->misccr);
1664 * Enable USB host clock.
1666 writel(readl(&clk_power->clkcon) | (1 << 4), &clk_power->clkcon);
1671 int usb_cpu_stop(void)
1673 struct s3c24x0_clock_power *clk_power = s3c24x0_get_base_clock_power();
1674 /* may not want to do this */
1675 writel(readl(&clk_power->clkcon) & ~(1 << 4), &clk_power->clkcon);
1679 int usb_cpu_init_fail(void)
1681 struct s3c24x0_clock_power *clk_power = s3c24x0_get_base_clock_power();
1682 writel(readl(&clk_power->clkcon) & ~(1 << 4), &clk_power->clkcon);
1686 #endif /* defined(CONFIG_USB_OHCI_NEW) && \
1687 defined(CONFIG_SYS_USB_OHCI_CPU_INIT) && \
1688 defined(CONFIG_S3C24X0) */