Merge branch 'next' of git://git.denx.de/u-boot-avr32
[oweals/u-boot.git] / cpu / arm920t / s3c24x0 / usb_ohci.c
index 6f4a9f7ae9d95001eab8970f56ff2685b7c313ba..b57c2d8950b2c90c328a9926be635165cecabd19 100644 (file)
@@ -4,6 +4,10 @@
  * (C) Copyright 2003
  * Gary Jennejohn, DENX Software Engineering <gj@denx.de>
  *
+ * 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.
  *
@@ -22,8 +26,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
@@ -56,8 +58,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; })
 
@@ -92,6 +94,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;
 
 /*-------------------------------------------------------------------------*/
 
@@ -396,6 +400,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");
@@ -484,7 +498,7 @@ static int ep_link (ohci_t *ohci, ed_t *edi)
                if (ohci->ed_controltail == NULL) {
                        writel (ed, &ohci->regs->ed_controlhead);
                } else {
-                       ohci->ed_controltail->hwNextED = m32_swap (ed);
+                       ohci->ed_controltail->hwNextED = (__u32)m32_swap (ed);
                }
                ed->ed_prev = ohci->ed_controltail;
                if (!ohci->ed_controltail && !ohci->ed_rm_list[0] &&
@@ -500,7 +514,7 @@ static int ep_link (ohci_t *ohci, ed_t *edi)
                if (ohci->ed_bulktail == NULL) {
                        writel (ed, &ohci->regs->ed_bulkhead);
                } else {
-                       ohci->ed_bulktail->hwNextED = m32_swap (ed);
+                       ohci->ed_bulktail->hwNextED = (__u32)m32_swap (ed);
                }
                ed->ed_prev = ohci->ed_bulktail;
                if (!ohci->ed_bulktail && !ohci->ed_rm_list[0] &&
@@ -592,7 +606,7 @@ static ed_t * ep_add_ed (struct usb_device *usb_dev, unsigned long pipe)
                ed->hwINFO = m32_swap (OHCI_ED_SKIP); /* skip ed */
                /* dummy td; end of td list for ed */
                td = td_alloc (usb_dev);
-               ed->hwTailP = m32_swap (td);
+               ed->hwTailP = (__u32)m32_swap (td);
                ed->hwHeadP = ed->hwTailP;
                ed->state = ED_UNLINK;
                ed->type = usb_pipetype (pipe);
@@ -649,14 +663,13 @@ static void td_fill (ohci_t *ohci, unsigned int info,
        if (!len)
                data = 0;
 
-       td->hwINFO = m32_swap (info);
-       td->hwCBP = m32_swap (data);
+       td->hwINFO = (__u32)m32_swap (info);
+       td->hwCBP = (__u32)m32_swap (data);
        if (data)
-               td->hwBE = m32_swap (data + len - 1);
+               td->hwBE = (__u32)m32_swap (data + len - 1);
        else
                td->hwBE = 0;
-       td->hwNextTD = m32_swap (td_pt);
-       td->hwPSW [0] = m16_swap (((__u32)data & 0x0FFF) | 0xE000);
+       td->hwNextTD = (__u32)m32_swap (td_pt);
 
        /* append to queue */
        td->ed->hwTailP = td->hwNextTD;
@@ -791,6 +804,7 @@ static td_t * dl_reverse_done_list (ohci_t *ohci)
                td_rev = td_list;
                td_list_hc = m32_swap (td_list->hwNextTD) & 0xfffffff0;
        }
+
        return td_list;
 }
 
@@ -824,6 +838,17 @@ static int dl_done_list (ohci_t *ohci, td_t *td_list)
                        stat = cc_to_error[cc];
                }
 
+               /* see if this done list makes for all TD's of current URB,
+                * and mark the URB finished if so */
+               if (++(lurb_priv->td_cnt) == lurb_priv->length) {
+                       if ((ed->state & (ED_OPER | ED_UNLINK)))
+                               urb_finished = 1;
+                       else
+                               dbg("dl_done_list: strange.., ED state %x, ed->state\n");
+               } else
+                       dbg("dl_done_list: processing TD %x, len %x\n", lurb_priv->td_cnt,
+                               lurb_priv->length);
+
                if (ed->state != ED_NEW) {
                        edHeadP = m32_swap (ed->hwHeadP) & 0xfffffff0;
                        edTailP = m32_swap (ed->hwTailP);
@@ -946,13 +971,13 @@ static unsigned char root_hub_str_index1[] =
 
 /*-------------------------------------------------------------------------*/
 
-#define OK(x)                  len = (x); break
+#define OK(x)                  len = (x); break
 #ifdef DEBUG
-#define WR_RH_STAT(x)          {info("WR:status %#8x", (x));writel((x), &gohci.regs->roothub.status);}
-#define WR_RH_PORTSTAT(x)      {info("WR:portstatus[%d] %#8x", wIndex-1, (x));writel((x), &gohci.regs->roothub.portstatus[wIndex-1]);}
+#define WR_RH_STAT(x)          {info("WR:status %#8x", (x));writel((x), &gohci.regs->roothub.status);}
+#define WR_RH_PORTSTAT(x)      {info("WR:portstatus[%d] %#8x", wIndex-1, (x));writel((x), &gohci.regs->roothub.portstatus[wIndex-1]);}
 #else
-#define WR_RH_STAT(x)          writel((x), &gohci.regs->roothub.status)
-#define WR_RH_PORTSTAT(x)      writel((x), &gohci.regs->roothub.portstatus[wIndex-1])
+#define WR_RH_STAT(x)          writel((x), &gohci.regs->roothub.status)
+#define WR_RH_PORTSTAT(x)      writel((x), &gohci.regs->roothub.portstatus[wIndex-1])
 #endif
 #define RD_RH_STAT             roothub_status(&gohci)
 #define RD_RH_PORTSTAT         roothub_portstatus(&gohci,wIndex-1)
@@ -1138,7 +1163,7 @@ pkt_print(dev, pipe, buffer, transfer_len, cmd, "SUB(rh)", usb_pipein(pipe));
                    data_buf [1] = 0x29;
                    data_buf [2] = temp & RH_A_NDP;
                    data_buf [3] = 0;
-                   if (temp & RH_A_PSM)        /* per-port power switching? */
+                   if (temp & RH_A_PSM)        /* per-port power switching? */
                        data_buf [3] |= 0x1;
                    if (temp & RH_A_NOCP)       /* no overcurrent reporting? */
                        data_buf [3] |= 0x10;
@@ -1163,9 +1188,9 @@ pkt_print(dev, pipe, buffer, transfer_len, cmd, "SUB(rh)", usb_pipein(pipe));
                    OK (len);
                }
 
-       case RH_GET_CONFIGURATION:      *(__u8 *) data_buf = 0x01; OK (1);
+       case RH_GET_CONFIGURATION:      *(__u8 *) data_buf = 0x01; OK (1);
 
-       case RH_SET_CONFIGURATION:      WR_RH_STAT (0x10000); OK (0);
+       case RH_SET_CONFIGURATION:      WR_RH_STAT (0x10000); OK (0);
 
        default:
                dbg ("unsupported root hub command");
@@ -1243,22 +1268,41 @@ int submit_common_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
        for (;;) {
                /* check whether the controller is done */
                stat = hc_interrupt();
+
                if (stat < 0) {
                        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
@@ -1280,6 +1324,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;
@@ -1455,16 +1500,26 @@ hc_interrupt (void)
        int ints;
        int stat = -1;
 
-       if ((ohci->hcca->done_head != 0) && !(m32_swap (ohci->hcca->done_head) & 0x01)) {
+       if ((ohci->hcca->done_head != 0) &&
+            !(m32_swap (ohci->hcca->done_head) & 0x01)) {
+
                ints =  OHCI_INTR_WDH;
-       } else {
-               ints = readl (&regs->intrstatus);
+
+       } 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) {
@@ -1488,6 +1543,7 @@ hc_interrupt (void)
 
        if (ints & OHCI_INTR_WDH) {
                wait_ms(1);
+
                writel (OHCI_INTR_WDH, &regs->intrdisable);
                stat = dl_done_list (&gohci, dl_reverse_done_list (&gohci));
                writel (OHCI_INTR_WDH, &regs->intrenable);
@@ -1591,7 +1647,8 @@ int usb_lowlevel_init(void)
        }
 
        /* FIXME this is a second HC reset; why?? */
-       writel (gohci.hc_control = OHCI_USB_RESET, &gohci.regs->control);
+       gohci.hc_control = OHCI_USB_RESET;
+       writel (gohci.hc_control, &gohci.regs->control);
        wait_ms (10);
 
        if (hc_start (&gohci) < 0) {
@@ -1608,6 +1665,8 @@ int usb_lowlevel_init(void)
        wait_ms(1);
 #endif
        ohci_inited = 1;
+       urb_finished = 1;
+
        return 0;
 }