usb: Clean up the USB initialization code.
Remove conditionally compiled code, and restructure things to be cleaner.
Change-Id: I6e9226f40138cebbcefde6fc9899d7855f8b66d7
diff --git a/src/libpayload/drivers/usb/usbinit.c b/src/libpayload/drivers/usb/usbinit.c
index 0c4c94c..bb46558 100644
--- a/src/libpayload/drivers/usb/usbinit.c
+++ b/src/libpayload/drivers/usb/usbinit.c
@@ -26,86 +26,76 @@
*/
//#define USB_DEBUG
+
#include <usb/usb.h>
-#include "uhci.h"
-#include "ohci.h"
-#include "ehci.h"
-#include "xhci.h"
-#include "dwc2.h"
-#if CONFIG_USB_PCI
-/**
- * Initializes USB controller attached to PCI
- *
- * @param bus PCI bus number
- * @param dev PCI device id at bus
- * @param func function id of the controller
- */
-static int usb_controller_initialize(int bus, int dev, int func)
+#include "drivers/bus/pci/pci.h"
+#include "libpayload/drivers/usb/dwc2.h"
+#include "libpayload/drivers/usb/ehci.h"
+#include "libpayload/drivers/usb/ohci.h"
+#include "libpayload/drivers/usb/uhci.h"
+#include "libpayload/drivers/usb/xhci.h"
+
+/* Initializes USB controller attached to PCI. */
+static int usb_controller_initialize(pcidev_t dev)
{
- uint32_t class;
- uint32_t devclass;
- uint32_t prog_if;
- pcidev_t pci_device;
- uint32_t pciid;
+ uint32_t class = pci_read_config32(dev, 8);
+ uint32_t pciid = pci_read_config32(dev, 0);
- pci_device = PCI_DEV (bus, dev, func);
- class = pci_read_config32(pci_device, 8);
- pciid = pci_read_config32(pci_device, 0);
+ uint32_t devclass = class >> 16;
+ uint32_t prog_if = (class >> 8) & 0xff;
- devclass = class >> 16;
- prog_if = (class >> 8) & 0xff;
-
- /* enable busmaster */
+ // Enable busmaster.
if (devclass == 0xc03) {
- uint32_t pci_command;
+ uint32_t command;
- pci_command = pci_read_config32(pci_device, PciConfCommand);
- pci_command |= PciConfCommandBm;
- pci_write_config32(pci_device, PciConfCommand, pci_command);
+ command = pci_read_config32(dev, PciConfCommand);
+ command |= PciConfCommandBm;
+ pci_write_config32(dev, PciConfCommand, command);
- usb_debug("%02x:%02x.%x %04x:%04x.%d ", bus, dev, func,
- pciid >> 16, pciid & 0xFFFF, func);
+ usb_debug("%02x:%02x.%x %04x:%04x.%d ", PCI_BUS(dev),
+ PCI_SLOT(dev), PCI_FUNC(dev),
+ pciid >> 16, pciid & 0xFFFF, PCI_FUNC(dev));
switch (prog_if) {
case 0x00:
-#if CONFIG_USB_UHCI
- usb_debug("UHCI controller\n");
- uhci_pci_init (pci_device);
-#else
- usb_debug("UHCI controller (not supported)\n");
-#endif
+ if (CONFIG_USB_UHCI) {
+ usb_debug("UHCI controller\n");
+ uhci_pci_init(dev);
+ } else {
+ usb_debug("UHCI controller (not supported)\n");
+ }
break;
case 0x10:
-#if CONFIG_USB_OHCI
- usb_debug("OHCI controller\n");
- ohci_pci_init(pci_device);
-#else
- usb_debug("OHCI controller (not supported)\n");
-#endif
+ if (CONFIG_USB_OHCI) {
+ usb_debug("OHCI controller\n");
+ ohci_pci_init(dev);
+ } else {
+ usb_debug("OHCI controller (not supported)\n");
+ }
break;
case 0x20:
-#if CONFIG_USB_EHCI
- usb_debug("EHCI controller\n");
- ehci_pci_init(pci_device);
-#else
- usb_debug("EHCI controller (not supported)\n");
-#endif
+ if (CONFIG_USB_EHCI) {
+ usb_debug("EHCI controller\n");
+ ehci_pci_init(dev);
+ } else {
+ usb_debug("EHCI controller (not supported)\n");
+ }
break;
case 0x30:
-#if CONFIG_USB_XHCI
- usb_debug("xHCI controller\n");
- xhci_pci_init(pci_device);
-#else
- usb_debug("xHCI controller (not supported)\n");
-#endif
+ if (CONFIG_USB_XHCI) {
+ usb_debug("xHCI controller\n");
+ xhci_pci_init(dev);
+ } else {
+ usb_debug("xHCI controller (not supported)\n");
+ }
break;
default:
usb_debug("unknown controller %x not supported\n",
- prog_if);
+ prog_if);
break;
}
}
@@ -113,16 +103,15 @@
return 0;
}
+static void usb_scan_pci_bus_dev(pcidev_t dev);
+
static void usb_scan_pci_bus(int bus)
{
- int dev, func;
- for (dev = 0; dev < 32; dev++) {
- uint8_t header_type;
- pcidev_t pci_device = PCI_DEV(bus, dev, 0);
+ for (int dev = 0; dev < 32; dev++) {
+ pcidev_t pci_dev = PCI_DEV(bus, dev, 0);
/* Check if there's a device here at all. */
- if (pci_read_config32(pci_device, PciConfVendorId) ==
- 0xffffffff)
+ if (pci_read_config32(pci_dev, PciConfVendorId) == 0xffffffff)
continue;
/*
@@ -133,66 +122,53 @@
*/
/* Check for a multifunction device. */
- header_type = pci_read_config8(pci_device, PciConfHeaderType);
- if (header_type & PciConfHeaderTypeMultifunction)
- func = 7;
- else
- func = 0;
+ uint8_t type = pci_read_config8(pci_dev, PciConfHeaderType);
+ int multifunction =
+ (type & PciConfHeaderTypeMultifunction) != 0;
- for (; func >= 0; func--) {
- pci_device = PCI_DEV(bus, dev, func);
- header_type = pci_read_config8(pci_device,
- PciConfHeaderType);
- /* If this is a bridge, scan the other side. */
- if ((header_type & ~PciConfHeaderTypeMultifunction) ==
- PciConfHeaderTypeBridge) {
- /* Verify that the bridge is enabled */
- if ((pci_read_config16(pci_device,
- PciConfCommand)
- & 3) != 0)
- usb_scan_pci_bus(pci_read_config8(
- pci_device,
- PciConfSecondaryBus));
- }
- else
- usb_controller_initialize(bus, dev, func);
- }
+ for (int func = multifunction ? 7 : 0; func >= 0; func--)
+ usb_scan_pci_bus_dev(PCI_DEV(bus, dev, func));
}
}
-#endif
-/**
- * Initialize all USB controllers attached to PCI.
- */
+static void usb_scan_pci_bus_dev(pcidev_t dev)
+{
+ uint8_t hdr_type = pci_read_config8(dev, PciConfHeaderType);
+ uint8_t type = hdr_type & ~PciConfHeaderTypeMultifunction;
+
+ // If this is a bridge, scan the other side.
+ if (type == PciConfHeaderTypeBridge) {
+ // Verify that the bridge is enabled.
+ uint16_t command = pci_read_config16(dev, PciConfCommand);
+ if ((command & 3) != 0) {
+ uint8_t secondary =
+ pci_read_config8(dev, PciConfSecondaryBus);
+ usb_scan_pci_bus(secondary);
+ }
+ } else {
+ usb_controller_initialize(dev);
+ }
+}
+
+/* Initialize all USB controllers attached to PCI. */
int usb_initialize(void)
{
-#if CONFIG_USB_PCI
- usb_scan_pci_bus(0);
-#endif
+ if (CONFIG_USB_PCI)
+ usb_scan_pci_bus(0);
return 0;
}
UsbDevHc *usb_add_mmio_hc(UsbHcType type, void *bar)
{
- switch (type) {
-#if CONFIG_USB_OHCI
- case UsbOhci:
+ if (CONFIG_USB_OHCI && type == UsbOhci)
return ohci_init((unsigned long)bar);
-#endif
-#if CONFIG_USB_EHCI
- case UsbEhci:
+ if (CONFIG_USB_EHCI && type == UsbEhci)
return ehci_init((unsigned long)bar);
-#endif
-#if CONFIG_USB_DWC2
- case UsbDwc2:
+ if (CONFIG_USB_DWC2 && type == UsbDwc2)
return dwc2_init(bar);
-#endif
-#if CONFIG_USB_XHCI
- case UsbXhci:
+ if (CONFIG_USB_XHCI && type == UsbXhci)
return xhci_init((unsigned long)bar);
-#endif
- default:
- usb_debug("HC type %d (at %p) is not supported!\n", type, bar);
- return NULL;
- }
+
+ usb_debug("HC type %d (at %p) is not supported!\n", type, bar);
+ return NULL;
}