mpc5xxx: Remove all references to MGT5100
[platform/kernel/u-boot.git] / cpu / mpc5xxx / usb_ohci.c
index 97ecb91..7976e4d 100644 (file)
@@ -2,11 +2,15 @@
  * URB OHCI HCD (Host Controller Driver) for USB on the MPC5200.
  *
  * (C) Copyright 2003-2004
- * Gary Jennejohn, DENX Software Engineering <gj@denx.de>
+ * Gary Jennejohn, DENX Software Engineering <garyj@denx.de>
  *
  * (C) Copyright 2004
  * Pierre Aubert, Staubli Faverges <p.aubert@staubli.com>
  *
+ * Note: Much of this code has been derived from Linux 2.4
+ * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
+ * (C) Copyright 2000-2002 David Brownell
+ *
  * See file CREDITS for list of people who contributed to this
  * project.
  *
@@ -25,8 +29,6 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  * MA 02111-1307 USA
  *
- * Note: Part of this code has been derived from linux
- *
  */
 /*
  * IMPORTANT NOTES
@@ -54,8 +56,8 @@
 #define OHCI_CONTROL_INIT \
        (OHCI_CTRL_CBSR & 0x3) | OHCI_CTRL_IE | OHCI_CTRL_PLE
 
-#define readl(a) (*((vu_long *)(a)))
-#define writel(a, b) (*((vu_long *)(b)) = ((vu_long)a))
+#define readl(a) (*((volatile u32 *)(a)))
+#define writel(a, b) (*((volatile u32 *)(b)) = ((volatile u32)a))
 
 #define min_t(type,x,y) ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
 
 #define m16_swap(x) swap_16(x)
 #define m32_swap(x) swap_32(x)
 
-#ifdef CONFIG_MPC5200
 #define ohci_cpu_to_le16(x) (x)
 #define ohci_cpu_to_le32(x) (x)
-#else
-#define ohci_cpu_to_le16(x) swap_16(x)
-#define ohci_cpu_to_le32(x) swap_32(x)
-#endif
 
 /* global ohci_t */
 static ohci_t gohci;
@@ -96,6 +93,8 @@ urb_priv_t urb_priv;
 int got_rhsc;
 /* device which was disconnected */
 struct usb_device *devgone;
+/* flag guarding URB transation */
+int urb_finished = 0;
 
 /*-------------------------------------------------------------------------*/
 
@@ -400,6 +399,16 @@ int sohci_submit_job(struct usb_device *dev, unsigned long pipe, void *buffer,
                return -1;
        }
 
+       /* if we have an unfinished URB from previous transaction let's
+        * fail and scream as quickly as possible so as not to corrupt
+        * further communication */
+       if (!urb_finished) {
+               err("sohci_submit_job: URB NOT FINISHED");
+               return -1;
+       }
+       /* we're about to begin a new transaction here so mark the URB unfinished */
+       urb_finished = 0;
+
        /* every endpoint has a ed, locate and fill it */
        if (!(ed = ep_add_ed (dev, pipe))) {
                err("sohci_submit_job: ENOMEM");
@@ -646,7 +655,7 @@ static void td_fill (ohci_t *ohci, unsigned int info,
        td->index = index;
        td->data = (__u32)data;
 #ifdef OHCI_FILL_TRACE
-       if ((usb_pipetype(urb_priv->pipe) == PIPE_BULK) && usb_pipeout(urb_priv->pipe)) {
+       if (usb_pipebulk(urb_priv->pipe) && usb_pipeout(urb_priv->pipe)) {
                for (i = 0; i < len; i++)
                printf("td->data[%d] %#2x ",i, ((unsigned char *)td->data)[i]);
                printf("\n");
@@ -662,7 +671,6 @@ static void td_fill (ohci_t *ohci, unsigned int info,
        else
                td->hwBE = 0;
        td->hwNextTD = ohci_cpu_to_le32 ((unsigned long)td_pt);
-       td->hwPSW [0] = ohci_cpu_to_le16 (((__u32)data & 0x0FFF) | 0xE000);
 
        /* append to queue */
        td->ed->hwTailP = td->hwNextTD;
@@ -671,7 +679,6 @@ static void td_fill (ohci_t *ohci, unsigned int info,
 /*-------------------------------------------------------------------------*/
 
 /* prepare all TDs of a transfer */
-
 static void td_submit_job (struct usb_device *dev, unsigned long pipe, void *buffer,
        int transfer_len, struct devrequest *setup, urb_priv_t *urb, int interval)
 {
@@ -749,7 +756,7 @@ static void dl_transfer_length(td_t * td)
        tdCBP  = ohci_cpu_to_le32 (td->hwCBP);
 
 
-       if (!(usb_pipetype (lurb_priv->pipe) == PIPE_CONTROL &&
+       if (!(usb_pipecontrol(lurb_priv->pipe) &&
            ((td->index == 0) || (td->index == lurb_priv->length - 1)))) {
                if (tdBE != 0) {
                        if (td->hwCBP == 0)
@@ -791,9 +798,7 @@ static td_t * dl_reverse_done_list (ohci_t *ohci)
                                } else
                                        td_list->ed->hwHeadP &= ohci_cpu_to_le32 (0xfffffff2);
                        }
-#ifdef CONFIG_MPC5200
                        td_list->hwNextTD = 0;
-#endif
                }
 
                td_list->next_dl_td = td_rev;
@@ -811,7 +816,7 @@ static int dl_done_list (ohci_t *ohci, td_t *td_list)
        td_t *td_list_next = NULL;
        ed_t *ed;
        int cc = 0;
-       int stat = 0xff;
+       int stat = 0;
        /* urb_t *urb; */
        urb_priv_t *lurb_priv;
        __u32 tdINFO, edHeadP, edTailP;
@@ -833,6 +838,7 @@ static int dl_done_list (ohci_t *ohci, td_t *td_list)
                                        && (lurb_priv->state != URB_DEL)) {
                                dbg("ConditionCode %#x", cc);
                                stat = cc_to_error[cc];
+                               urb_finished = 1;
                        }
                }
 
@@ -1010,7 +1016,7 @@ static int ohci_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
 urb_priv.actual_length = 0;
 pkt_print(dev, pipe, buffer, transfer_len, cmd, "SUB(rh)", usb_pipein(pipe));
 #endif
-       if ((pipe & PIPE_INTERRUPT) == PIPE_INTERRUPT) {
+       if (usb_pipeint(pipe)) {
                info("Root-Hub submit IRQ: NOT implemented");
                return 0;
        }
@@ -1235,7 +1241,7 @@ int submit_common_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
 
        /* allow more time for a BULK device to react - some are slow */
 #define BULK_TO         5000   /* timeout in milliseconds */
-       if (usb_pipetype (pipe) == PIPE_BULK)
+       if (usb_pipebulk(pipe))
                timeout = BULK_TO;
        else
                timeout = 100;
@@ -1248,18 +1254,35 @@ int submit_common_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
                        stat = USB_ST_CRC_ERR;
                        break;
                }
-               if (stat >= 0 && stat < 0xff) {
+
+               /* NOTE: since we are not interrupt driven in U-Boot and always
+                * handle only one URB at a time, we cannot assume the
+                * transaction finished on the first successful return from
+                * hc_interrupt().. unless the flag for current URB is set,
+                * meaning that all TD's to/from device got actually
+                * transferred and processed. If the current URB is not
+                * finished we need to re-iterate this loop so as
+                * hc_interrupt() gets called again as there needs to be some
+                * more TD's to process still */
+               if ((stat >= 0) && (stat != 0xff) && (urb_finished)) {
                        /* 0xff is returned for an SF-interrupt */
                        break;
                }
+
                if (--timeout) {
                        wait_ms(1);
+                       if (!urb_finished)
+                               dbg("\%");
+
                } else {
                        err("CTL:TIMEOUT ");
+                       dbg("submit_common_msg: TO status %x\n", stat);
                        stat = USB_ST_CRC_ERR;
+                       urb_finished = 1;
                        break;
                }
        }
+#if 0
        /* we got an Root Hub Status Change interrupt */
        if (got_rhsc) {
 #ifdef DEBUG
@@ -1281,6 +1304,7 @@ int submit_common_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
                        devgone = dev;
                }
        }
+#endif
 
        dev->status = stat;
        dev->act_len = transfer_len;
@@ -1453,16 +1477,26 @@ hc_interrupt (void)
        int ints;
        int stat = -1;
 
-       if ((ohci->hcca->done_head != 0) && !(ohci_cpu_to_le32 (ohci->hcca->done_head) & 0x01)) {
-               ints =  OHCI_INTR_WDH;
-       } else {
-               ints = readl (&regs->intrstatus);
+       if ((ohci->hcca->done_head != 0) &&
+            !(ohci_cpu_to_le32(ohci->hcca->done_head) & 0x01)) {
+
+               ints =  OHCI_INTR_WDH;
+
+       } else if ((ints = readl (&regs->intrstatus)) == ~(u32)0) {
+               ohci->disabled++;
+               err ("%s device removed!", ohci->slot_name);
+               return -1;
+
+       } else if ((ints &= readl (&regs->intrenable)) == 0) {
+               dbg("hc_interrupt: returning..\n");
+               return 0xff;
        }
 
        /* dbg("Interrupt: %x frame: %x", ints, le16_to_cpu (ohci->hcca->frame_no)); */
 
        if (ints & OHCI_INTR_RHSC) {
                got_rhsc = 1;
+               stat = 0xff;
        }
 
        if (ints & OHCI_INTR_UE) {
@@ -1497,6 +1531,7 @@ hc_interrupt (void)
        /* FIXME:  this assumes SOF (1/ms) interrupts don't get lost... */
        if (ints & OHCI_INTR_SF) {
                unsigned int frame = ohci_cpu_to_le16 (ohci->hcca->frame_no) & 1;
+               wait_ms(1);
                writel (OHCI_INTR_SF, &regs->intrdisable);
                if (ohci->ed_rm_list[frame] != NULL)
                        writel (OHCI_INTR_SF, &regs->intrenable);
@@ -1532,12 +1567,17 @@ int usb_lowlevel_init(void)
 {
 
        /* Set the USB Clock                                                 */
-       *(vu_long *)MPC5XXX_CDM_48_FDC = CONFIG_USB_CDMFDC5xxx;
+       *(vu_long *)MPC5XXX_CDM_48_FDC = CONFIG_USB_CLOCK;
 
+#ifdef CONFIG_PSC3_USB /* USB is using the alternate configuration */
+       /* remove all PSC3 USB bits first before ORing in ours */
+       *(vu_long *)MPC5XXX_GPS_PORT_CONFIG &= ~0x00804f00;
+#else
        /* remove all USB bits first before ORing in ours */
        *(vu_long *)MPC5XXX_GPS_PORT_CONFIG &= ~0x00807000;
+#endif
        /* Activate USB port                                                 */
-       *(vu_long *)MPC5XXX_GPS_PORT_CONFIG |= CONFIG_USB_GPSCFG5xxx;
+       *(vu_long *)MPC5XXX_GPS_PORT_CONFIG |= CONFIG_USB_CONFIG;
 
        memset (&gohci, 0, sizeof (ohci_t));
        memset (&urb_priv, 0, sizeof (urb_priv_t));
@@ -1586,6 +1626,8 @@ int usb_lowlevel_init(void)
        ohci_dump (&gohci, 1);
 #endif
        ohci_inited = 1;
+       urb_finished = 1;
+
        return 0;
 }