libpayload: remove uhci_reg_maskX
[coreboot.git] / payloads / libpayload / drivers / usb / uhci.c
index 198b6c0846428ac02cd3f276c0a4745345247baa..b964ee7a0b168245ce4ea2830ef27de981de8c42 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the libpayload project.
  *
- * Copyright (C) 2008 coresystems GmbH
+ * Copyright (C) 2008-2010 coresystems GmbH
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * SUCH DAMAGE.
  */
 
+//#define USB_DEBUG
+
+#include <arch/virtual.h>
 #include <usb/usb.h>
 #include "uhci.h"
-#include <arch/virtual.h>
+#include "uhci_private.h"
 
 static void uhci_start (hci_t *controller);
 static void uhci_stop (hci_t *controller);
 static void uhci_reset (hci_t *controller);
 static void uhci_shutdown (hci_t *controller);
-static int uhci_packet (usbdev_t *dev, int endp, int pid, int toggle,
-                       int length, u8 *data);
 static int uhci_bulk (endpoint_t *ep, int size, u8 *data, int finalize);
-static int uhci_control (usbdev_t *dev, pid_t dir, int drlen, void *devreq,
+static int uhci_control (usbdev_t *dev, direction_t dir, int drlen, void *devreq,
                         int dalen, u8 *data);
 static void* uhci_create_intr_queue (endpoint_t *ep, int reqsize, int reqcount, int reqtiming);
 static void uhci_destroy_intr_queue (endpoint_t *ep, void *queue);
@@ -49,14 +50,14 @@ static u8* uhci_poll_intr_queue (void *queue);
 static void
 uhci_dump (hci_t *controller)
 {
-       printf ("dump:\nUSBCMD: %x\n", uhci_reg_read16 (controller, USBCMD));
-       printf ("USBSTS: %x\n", uhci_reg_read16 (controller, USBSTS));
-       printf ("USBINTR: %x\n", uhci_reg_read16 (controller, USBINTR));
-       printf ("FRNUM: %x\n", uhci_reg_read16 (controller, FRNUM));
-       printf ("FLBASEADD: %x\n", uhci_reg_read32 (controller, FLBASEADD));
-       printf ("SOFMOD: %x\n", uhci_reg_read8 (controller, SOFMOD));
-       printf ("PORTSC1: %x\n", uhci_reg_read16 (controller, PORTSC1));
-       printf ("PORTSC2: %x\n", uhci_reg_read16 (controller, PORTSC2));
+       debug ("dump:\nUSBCMD: %x\n", uhci_reg_read16 (controller, USBCMD));
+       debug ("USBSTS: %x\n", uhci_reg_read16 (controller, USBSTS));
+       debug ("USBINTR: %x\n", uhci_reg_read16 (controller, USBINTR));
+       debug ("FRNUM: %x\n", uhci_reg_read16 (controller, FRNUM));
+       debug ("FLBASEADD: %x\n", uhci_reg_read32 (controller, FLBASEADD));
+       debug ("SOFMOD: %x\n", uhci_reg_read8 (controller, SOFMOD));
+       debug ("PORTSC1: %x\n", uhci_reg_read16 (controller, PORTSC1));
+       debug ("PORTSC2: %x\n", uhci_reg_read16 (controller, PORTSC2));
 }
 #endif
 
@@ -64,36 +65,36 @@ static void
 td_dump (td_t *td)
 {
        char td_value[3];
-       char *td_type;
+       const char *td_type;
        switch (td->pid) {
-               case SETUP:
+               case UHCI_SETUP:
                        td_type="SETUP";
                        break;
-               case IN:
+               case UHCI_IN:
                        td_type="IN";
                        break;
-               case OUT:
+               case UHCI_OUT:
                        td_type="OUT";
                        break;
                default:
                        sprintf(td_value, "%x", td->pid);
                        td_type=td_value;
        }
-       printf ("%s packet (at %lx) to %x.%x failed\n", td_type,
+       debug ("%s packet (at %lx) to %x.%x failed\n", td_type,
                virt_to_phys (td), td->dev_addr, td->endp);
-       printf ("td (counter at %x) returns: ", td->counter);
-       printf (" bitstuff err: %x, ", td->status_bitstuff_err);
-       printf (" CRC err: %x, ", td->status_crc_err);
-       printf (" NAK rcvd: %x, ", td->status_nakrcvd);
-       printf (" Babble: %x, ", td->status_babble);
-       printf (" Data Buffer err: %x, ", td->status_databuf_err);
-       printf (" Stalled: %x, ", td->status_stalled);
-       printf (" Active: %x\n", td->status_active);
+       debug ("td (counter at %x) returns: ", td->counter);
+       debug (" bitstuff err: %x, ", td->status_bitstuff_err);
+       debug (" CRC err: %x, ", td->status_crc_err);
+       debug (" NAK rcvd: %x, ", td->status_nakrcvd);
+       debug (" Babble: %x, ", td->status_babble);
+       debug (" Data Buffer err: %x, ", td->status_databuf_err);
+       debug (" Stalled: %x, ", td->status_stalled);
+       debug (" Active: %x\n", td->status_active);
        if (td->status_babble)
-               printf (" Babble because of %s\n",
+               debug (" Babble because of %s\n",
                        td->status_bitstuff_err ? "host" : "device");
        if (td->status_active)
-               printf (" still active - timeout?\n");
+               debug (" still active - timeout?\n");
 }
 
 static void
@@ -111,7 +112,7 @@ uhci_reset (hci_t *controller)
        uhci_reg_write32 (controller, FLBASEADD,
                          (u32) virt_to_phys (UHCI_INST (controller)->
                                              framelistptr));
-       //printf ("framelist at %p\n",UHCI_INST(controller)->framelistptr);
+       //debug ("framelist at %p\n",UHCI_INST(controller)->framelistptr);
 
        /* disable irqs */
        uhci_reg_write16 (controller, USBINTR, 0);
@@ -119,7 +120,8 @@ uhci_reset (hci_t *controller)
        /* reset framelist index */
        uhci_reg_write16 (controller, FRNUM, 0);
 
-       uhci_reg_mask16 (controller, USBCMD, ~0, 0xc0); // max packets, configure flag
+       uhci_reg_write16(controller, USBCMD,
+                        uhci_reg_read16(controller, USBCMD) | 0xc0);   // max packets, configure flag
 
        uhci_start (controller);
 }
@@ -128,14 +130,21 @@ hci_t *
 uhci_init (pcidev_t addr)
 {
        int i;
+       u16 reg16;
+
        hci_t *controller = new_controller ();
 
+       if (!controller)
+               fatal("Could not create USB controller instance.\n");
+
        controller->instance = malloc (sizeof (uhci_t));
+       if(!controller->instance)
+               fatal("Not enough memory creating USB controller instance.\n");
+
        controller->start = uhci_start;
        controller->stop = uhci_stop;
        controller->reset = uhci_reset;
        controller->shutdown = uhci_shutdown;
-       controller->packet = uhci_packet;
        controller->bulk = uhci_bulk;
        controller->control = uhci_control;
        controller->create_intr_queue = uhci_create_intr_queue;
@@ -154,9 +163,14 @@ uhci_init (pcidev_t addr)
        uhci_stop (controller);
        mdelay (1);
        uhci_reg_write16 (controller, USBSTS, 0x3f);
-       pci_write_config32 (controller->bus_address, 0xc0, 0x8f00);
+       reg16 = pci_read_config16(controller->bus_address, 0xc0);
+       reg16 &= 0xdf80;
+       pci_write_config16 (controller->bus_address, 0xc0, reg16);
 
        UHCI_INST (controller)->framelistptr = memalign (0x1000, 1024 * sizeof (flistp_t *));   /* 4kb aligned to 4kb */
+       if (! UHCI_INST (controller)->framelistptr)
+               fatal("Not enough memory for USB frame list pointer.\n");
+
        memset (UHCI_INST (controller)->framelistptr, 0,
                1024 * sizeof (flistp_t));
 
@@ -168,6 +182,8 @@ uhci_init (pcidev_t addr)
                  for some reason. Not a problem now.
           */
        td_t *antiberserk = memalign(16, sizeof(td_t));
+       if (!antiberserk)
+               fatal("Not enough memory for chipset workaround.\n");
        memset(antiberserk, 0, sizeof(td_t));
 
        UHCI_INST (controller)->qh_prei = memalign (16, sizeof (qh_t));
@@ -175,6 +191,12 @@ uhci_init (pcidev_t addr)
        UHCI_INST (controller)->qh_data = memalign (16, sizeof (qh_t));
        UHCI_INST (controller)->qh_last = memalign (16, sizeof (qh_t));
 
+       if (! UHCI_INST (controller)->qh_prei ||
+           ! UHCI_INST (controller)->qh_intr ||
+           ! UHCI_INST (controller)->qh_data ||
+           ! UHCI_INST (controller)->qh_last)
+               fatal("Not enough memory for USB controller queues.\n");
+
        UHCI_INST (controller)->qh_prei->headlinkptr.ptr =
                virt_to_phys (UHCI_INST (controller)->qh_intr);
        UHCI_INST (controller)->qh_prei->headlinkptr.queue_head = 1;
@@ -219,7 +241,8 @@ uhci_shutdown (hci_t *controller)
        detach_controller (controller);
        UHCI_INST (controller)->roothub->destroy (UHCI_INST (controller)->
                                                  roothub);
-       uhci_reg_mask16 (controller, USBCMD, 0, 0);     // stop work
+       uhci_reg_write16(controller, USBCMD,
+                        uhci_reg_read16(controller, USBCMD) & 0);      // stop work
        free (UHCI_INST (controller)->framelistptr);
        free (UHCI_INST (controller)->qh_prei);
        free (UHCI_INST (controller)->qh_intr);
@@ -232,13 +255,15 @@ uhci_shutdown (hci_t *controller)
 static void
 uhci_start (hci_t *controller)
 {
-       uhci_reg_mask16 (controller, USBCMD, ~0, 1);    // start work on schedule
+       uhci_reg_write16(controller, USBCMD,
+                        uhci_reg_read16(controller, USBCMD) | 1);      // start work on schedule
 }
 
 static void
 uhci_stop (hci_t *controller)
 {
-       uhci_reg_mask16 (controller, USBCMD, ~1, 0);    // stop work on schedule
+       uhci_reg_write16(controller, USBCMD,
+                        uhci_reg_read16(controller, USBCMD) & ~1);     // stop work on schedule
 }
 
 #define GET_TD(x) ((void*)(((unsigned int)(x))&~0xf))
@@ -253,25 +278,14 @@ wait_for_completed_qh (hci_t *controller, qh_t *qh)
                        current = GET_TD (qh->elementlinkptr.ptr);
                        timeout = 1000000;
                }
-               uhci_reg_mask16 (controller, USBSTS, ~0, 0);    // clear resettable registers
+               uhci_reg_write16(controller, USBSTS,
+                                uhci_reg_read16(controller, USBSTS) | 0);      // clear resettable registers
                udelay (30);
        }
        return (GET_TD (qh->elementlinkptr.ptr) ==
                0) ? 0 : GET_TD (phys_to_virt (qh->elementlinkptr.ptr));
 }
 
-static void
-wait_for_completed_td (hci_t *controller, td_t *td)
-{
-       int timeout = 10000;
-       while ((td->status_active == 1)
-              && ((uhci_reg_read16 (controller, USBSTS) & 2) == 0)
-              && (timeout-- > 0)) {
-               uhci_reg_mask16 (controller, USBSTS, ~0, 0);    // clear resettable registers
-               udelay (10);
-       }
-}
-
 static int
 maxlen (int size)
 {
@@ -288,7 +302,7 @@ min (int a, int b)
 }
 
 static int
-uhci_control (usbdev_t *dev, pid_t dir, int drlen, void *devreq, int dalen,
+uhci_control (usbdev_t *dev, direction_t dir, int drlen, void *devreq, int dalen,
              unsigned char *data)
 {
        int endp = 0;           /* this is control: always 0 */
@@ -308,25 +322,29 @@ uhci_control (usbdev_t *dev, pid_t dir, int drlen, void *devreq, int dalen,
        tds[count].depth_first = 1;
        tds[count].terminate = 1;
 
-       tds[0].pid = SETUP;
+       tds[0].pid = UHCI_SETUP;
        tds[0].dev_addr = dev->address;
        tds[0].endp = endp;
        tds[0].maxlen = maxlen (drlen);
        tds[0].counter = 3;
        tds[0].data_toggle = 0;
-       tds[0].lowspeed = dev->lowspeed;
+       tds[0].lowspeed = dev->speed;
        tds[0].bufptr = virt_to_phys (devreq);
        tds[0].status_active = 1;
 
        int toggle = 1;
        for (i = 1; i < count; i++) {
-               tds[i].pid = dir;
+               switch (dir) {
+                       case SETUP: tds[i].pid = UHCI_SETUP; break;
+                       case IN:    tds[i].pid = UHCI_IN;    break;
+                       case OUT:   tds[i].pid = UHCI_OUT;   break;
+               }
                tds[i].dev_addr = dev->address;
                tds[i].endp = endp;
                tds[i].maxlen = maxlen (min (mlen, dalen));
                tds[i].counter = 3;
                tds[i].data_toggle = toggle;
-               tds[i].lowspeed = dev->lowspeed;
+               tds[i].lowspeed = dev->speed;
                tds[i].bufptr = virt_to_phys (data);
                tds[i].status_active = 1;
                toggle ^= 1;
@@ -334,13 +352,14 @@ uhci_control (usbdev_t *dev, pid_t dir, int drlen, void *devreq, int dalen,
                data += mlen;
        }
 
-       tds[count].pid = (dir == OUT) ? IN : OUT;
+       tds[count].pid = (dir == OUT) ? UHCI_IN : UHCI_OUT;
        tds[count].dev_addr = dev->address;
        tds[count].endp = endp;
        tds[count].maxlen = maxlen (0);
        tds[count].counter = 0; /* as per linux 2.4.10 */
        tds[count].data_toggle = 1;
-       tds[count].lowspeed = dev->lowspeed, tds[count].bufptr = 0;
+       tds[count].lowspeed = dev->speed;
+       tds[count].bufptr = 0;
        tds[count].status_active = 1;
        UHCI_INST (dev->controller)->qh_data->elementlinkptr.ptr =
                virt_to_phys (tds);
@@ -353,7 +372,7 @@ uhci_control (usbdev_t *dev, pid_t dir, int drlen, void *devreq, int dalen,
        if (td == 0) {
                result = 0;
        } else {
-               printf ("control packet, req %x\n", req);
+               debug ("control packet, req %x\n", req);
                td_dump (td);
                result = 1;
        }
@@ -361,48 +380,6 @@ uhci_control (usbdev_t *dev, pid_t dir, int drlen, void *devreq, int dalen,
        return result;
 }
 
-static int
-uhci_packet (usbdev_t *dev, int endp, int pid, int toggle, int length,
-            unsigned char *data)
-{
-       static td_t *td = 0;
-       if (td == 0)
-               td = memalign (16, sizeof (td_t));
-
-       memset (td, 0, sizeof (td_t));
-       td->ptr = 0;
-       td->terminate = 1;
-       td->queue_head = 0;
-
-       td->pid = pid;
-       td->dev_addr = dev->address;
-       td->endp = endp & 0xf;
-       td->maxlen = maxlen (length);
-       if (pid == SETUP)
-               td->counter = 3;
-       else
-               td->counter = 0;
-       td->data_toggle = toggle & 1;
-       td->lowspeed = dev->lowspeed;
-       td->bufptr = virt_to_phys (data);
-
-       td->status_active = 1;
-
-       UHCI_INST (dev->controller)->qh_data->elementlinkptr.ptr =
-               virt_to_phys (td);
-       UHCI_INST (dev->controller)->qh_data->elementlinkptr.queue_head = 0;
-       UHCI_INST (dev->controller)->qh_data->elementlinkptr.terminate = 0;
-       wait_for_completed_td (dev->controller, td);
-       if ((td->status & 0x7f) == 0) {
-               //printf("successfully sent a %x packet to %x.%x\n",pid, dev->address,endp);
-               // success
-               return 0;
-       } else {
-               td_dump (td);
-               return 1;
-       }
-}
-
 static td_t *
 create_schedule (int numpackets)
 {
@@ -428,7 +405,11 @@ static void
 fill_schedule (td_t *td, endpoint_t *ep, int length, unsigned char *data,
               int *toggle)
 {
-       td->pid = ep->direction;
+       switch (ep->direction) {
+               case IN: td->pid = UHCI_IN; break;
+               case OUT: td->pid = UHCI_OUT; break;
+               case SETUP: td->pid = UHCI_SETUP; break;
+       }
        td->dev_addr = ep->dev->address;
        td->endp = ep->endpoint & 0xf;
        td->maxlen = maxlen (length);
@@ -437,7 +418,7 @@ fill_schedule (td_t *td, endpoint_t *ep, int length, unsigned char *data,
        else
                td->counter = 0;
        td->data_toggle = *toggle & 1;
-       td->lowspeed = ep->dev->lowspeed;
+       td->lowspeed = ep->dev->speed;
        td->bufptr = virt_to_phys (data);
 
        td->status_active = 1;
@@ -467,7 +448,7 @@ uhci_bulk (endpoint_t *ep, int size, u8 *data, int finalize)
 {
        int maxpsize = ep->maxpacketsize;
        if (maxpsize == 0)
-               fatal ("MaxPacketSize == 0!!!");
+               fatal("MaxPacketSize == 0!!!");
        int numpackets = (size + maxpsize - 1 + finalize) / maxpsize;
        if (numpackets == 0)
                return 0;
@@ -481,6 +462,7 @@ uhci_bulk (endpoint_t *ep, int size, u8 *data, int finalize)
                size -= maxpsize;
        }
        if (run_schedule (ep->dev, tds) == 1) {
+               debug("Stalled. Trying to clean up.\n");
                clear_stall (ep);
                free (tds);
                return 1;
@@ -508,11 +490,16 @@ uhci_create_intr_queue (endpoint_t *ep, int reqsize, int reqcount, int reqtiming
        td_t *tds = memalign(16, sizeof(td_t) * reqcount);
        qh_t *qh = memalign(16, sizeof(qh_t));
 
+       if (!data || !tds || !qh)
+               fatal("Not enough memory to create USB intr queue prerequisites.\n");
+
        qh->elementlinkptr.ptr = virt_to_phys(tds);
        qh->elementlinkptr.queue_head = 0;
        qh->elementlinkptr.terminate = 0;
 
        intr_q *q = malloc(sizeof(intr_q));
+       if (!q)
+               fatal("Not enough memory to create USB intr queue.\n");
        q->qh = qh;
        q->tds = tds;
        q->data = data;
@@ -529,13 +516,17 @@ uhci_create_intr_queue (endpoint_t *ep, int reqsize, int reqcount, int reqtiming
                tds[i].queue_head = 0;
                tds[i].depth_first = 0;
 
-               tds[i].pid = ep->direction;
+               switch (ep->direction) {
+                       case IN: tds[i].pid = UHCI_IN; break;
+                       case OUT: tds[i].pid = UHCI_OUT; break;
+                       case SETUP: tds[i].pid = UHCI_SETUP; break;
+               }
                tds[i].dev_addr = ep->dev->address;
                tds[i].endp = ep->endpoint & 0xf;
                tds[i].maxlen = maxlen (reqsize);
                tds[i].counter = 0;
                tds[i].data_toggle = ep->toggle & 1;
-               tds[i].lowspeed = ep->dev->lowspeed;
+               tds[i].lowspeed = ep->dev->speed;
                tds[i].bufptr = virt_to_phys (data);
                tds[i].status_active = 1;
                ep->toggle ^= 1;
@@ -650,24 +641,3 @@ uhci_reg_read8 (hci_t *ctrl, usbreg reg)
 {
        return inb (ctrl->reg_base + reg);
 }
-
-void
-uhci_reg_mask32 (hci_t *ctrl, usbreg reg, u32 andmask, u32 ormask)
-{
-       uhci_reg_write32 (ctrl, reg,
-                         (uhci_reg_read32 (ctrl, reg) & andmask) | ormask);
-}
-
-void
-uhci_reg_mask16 (hci_t *ctrl, usbreg reg, u16 andmask, u16 ormask)
-{
-       uhci_reg_write16 (ctrl, reg,
-                         (uhci_reg_read16 (ctrl, reg) & andmask) | ormask);
-}
-
-void
-uhci_reg_mask8 (hci_t *ctrl, usbreg reg, u8 andmask, u8 ormask)
-{
-       uhci_reg_write8 (ctrl, reg,
-                        (uhci_reg_read8 (ctrl, reg) & andmask) | ormask);
-}