Merge branch 'master' of git://git.denx.de/u-boot-x86
[oweals/u-boot.git] / common / cmd_pci.c
index c0765fd7fb49b143c816ceeda1b656430460f6c8..8094d3380fbd30284ab0438dc21dbea128980872 100644 (file)
@@ -18,6 +18,7 @@
 #include <cli.h>
 #include <command.h>
 #include <console.h>
+#include <dm.h>
 #include <asm/processor.h>
 #include <asm/io.h>
 #include <pci.h>
@@ -28,19 +29,37 @@ struct pci_reg_info {
        u8 offset;
 };
 
-static int pci_field_width(enum pci_size_t size)
+static int pci_byte_size(enum pci_size_t size)
 {
        switch (size) {
        case PCI_SIZE_8:
-               return 2;
+               return 1;
        case PCI_SIZE_16:
-               return 4;
+               return 2;
        case PCI_SIZE_32:
        default:
-               return 8;
+               return 4;
        }
 }
 
+static int pci_field_width(enum pci_size_t size)
+{
+       return pci_byte_size(size) * 2;
+}
+
+#ifdef CONFIG_DM_PCI
+static void pci_show_regs(struct udevice *dev, struct pci_reg_info *regs)
+{
+       for (; regs->name; regs++) {
+               unsigned long val;
+
+               dm_pci_read_config(dev, regs->offset, &val, regs->size);
+               printf("  %s =%*s%#.*lx\n", regs->name,
+                      (int)(28 - strlen(regs->name)), "",
+                      pci_field_width(regs->size), val);
+       }
+}
+#else
 static unsigned long pci_read_config(pci_dev_t dev, int offset,
                                     enum pci_size_t size)
 {
@@ -71,6 +90,7 @@ static void pci_show_regs(pci_dev_t dev, struct pci_reg_info *regs)
                       pci_read_config(dev, regs->offset, regs->size));
        }
 }
+#endif
 
 static struct pci_reg_info regs_start[] = {
        { "vendor ID", PCI_SIZE_16, PCI_VENDOR_ID },
@@ -166,15 +186,25 @@ static struct pci_reg_info regs_cardbus[] = {
  *
  * @dev: Bus+Device+Function number
  */
+#ifdef CONFIG_DM_PCI
+void pci_header_show(struct udevice *dev)
+#else
 void pci_header_show(pci_dev_t dev)
+#endif
 {
+#ifdef CONFIG_DM_PCI
+       unsigned long class, header_type;
+
+       dm_pci_read_config(dev, PCI_CLASS_CODE, &class, PCI_SIZE_8);
+       dm_pci_read_config(dev, PCI_HEADER_TYPE, &header_type, PCI_SIZE_8);
+#else
        u8 class, header_type;
 
        pci_read_config_byte(dev, PCI_CLASS_CODE, &class);
        pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
+#endif
        pci_show_regs(dev, regs_start);
-
-       printf("  class code =                  0x%.2x (%s)\n", class,
+       printf("  class code =                  0x%.2x (%s)\n", (int)class,
               pci_class_str(class));
        pci_show_regs(dev, regs_rest);
 
@@ -195,6 +225,65 @@ void pci_header_show(pci_dev_t dev)
     }
 }
 
+void pciinfo_header(int busnum, bool short_listing)
+{
+       printf("Scanning PCI devices on bus %d\n", busnum);
+
+       if (short_listing) {
+               printf("BusDevFun  VendorId   DeviceId   Device Class       Sub-Class\n");
+               printf("_____________________________________________________________\n");
+       }
+}
+
+#ifdef CONFIG_DM_PCI
+/**
+ * pci_header_show_brief() - Show the short-form PCI device header
+ *
+ * Reads and prints the header of the specified PCI device in short form.
+ *
+ * @dev: PCI device to show
+ */
+static void pci_header_show_brief(struct udevice *dev)
+{
+       ulong vendor, device;
+       ulong class, subclass;
+
+       dm_pci_read_config(dev, PCI_VENDOR_ID, &vendor, PCI_SIZE_16);
+       dm_pci_read_config(dev, PCI_DEVICE_ID, &device, PCI_SIZE_16);
+       dm_pci_read_config(dev, PCI_CLASS_CODE, &class, PCI_SIZE_8);
+       dm_pci_read_config(dev, PCI_CLASS_SUB_CODE, &subclass, PCI_SIZE_8);
+
+       printf("0x%.4lx     0x%.4lx     %-23s 0x%.2lx\n",
+              vendor, device,
+              pci_class_str(class), subclass);
+}
+
+static void pciinfo(struct udevice *bus, bool short_listing)
+{
+       struct udevice *dev;
+
+       pciinfo_header(bus->seq, short_listing);
+
+       for (device_find_first_child(bus, &dev);
+            dev;
+            device_find_next_child(&dev)) {
+               struct pci_child_platdata *pplat;
+
+               pplat = dev_get_parent_platdata(dev);
+               if (short_listing) {
+                       printf("%02x.%02x.%02x   ", bus->seq,
+                              PCI_DEV(pplat->devfn), PCI_FUNC(pplat->devfn));
+                       pci_header_show_brief(dev);
+               } else {
+                       printf("\nFound PCI device %02x.%02x.%02x:\n", bus->seq,
+                              PCI_DEV(pplat->devfn), PCI_FUNC(pplat->devfn));
+                       pci_header_show(dev);
+               }
+       }
+}
+
+#else
+
 /**
  * pci_header_show_brief() - Show the short-form PCI device header
  *
@@ -240,12 +329,7 @@ void pciinfo(int bus_num, int short_pci_listing)
        if (!hose)
                return;
 
-       printf("Scanning PCI devices on bus %d\n", bus_num);
-
-       if (short_pci_listing) {
-               printf("BusDevFun  VendorId   DeviceId   Device Class       Sub-Class\n");
-               printf("_____________________________________________________________\n");
-       }
+       pciinfo_header(bus_num, short_pci_listing);
 
        for (device = 0; device < PCI_MAX_PCI_DEVICES; device++) {
                header_type = 0;
@@ -292,7 +376,7 @@ void pciinfo(int bus_num, int short_pci_listing)
 error:
        printf("Cannot read bus configuration: %d\n", ret);
 }
-
+#endif
 
 /**
  * get_pci_dev() - Convert the "bus.device.function" identifier into a number
@@ -325,38 +409,40 @@ static pci_dev_t get_pci_dev(char *name)
        return PCI_BDF(bdfs[0], bdfs[1], bdfs[2]);
 }
 
-static int pci_cfg_display(pci_dev_t bdf, ulong addr, ulong size, ulong length)
+#ifdef CONFIG_DM_PCI
+static int pci_cfg_display(struct udevice *dev, ulong addr,
+                          enum pci_size_t size, ulong length)
+#else
+static int pci_cfg_display(pci_dev_t bdf, ulong addr, enum pci_size_t size,
+                          ulong length)
+#endif
 {
 #define DISP_LINE_LEN  16
        ulong i, nbytes, linebytes;
+       int byte_size;
        int rc = 0;
 
+       byte_size = pci_byte_size(size);
        if (length == 0)
-               length = 0x40 / size; /* Standard PCI configuration space */
+               length = 0x40 / byte_size; /* Standard PCI config space */
 
        /* Print the lines.
         * once, and all accesses are with the specified bus width.
         */
-       nbytes = length * size;
+       nbytes = length * byte_size;
        do {
-               uint    val4;
-               ushort  val2;
-               u_char  val1;
-
                printf("%08lx:", addr);
-               linebytes = (nbytes>DISP_LINE_LEN)?DISP_LINE_LEN:nbytes;
-               for (i=0; i<linebytes; i+= size) {
-                       if (size == 4) {
-                               pci_read_config_dword(bdf, addr, &val4);
-                               printf(" %08x", val4);
-                       } else if (size == 2) {
-                               pci_read_config_word(bdf, addr, &val2);
-                               printf(" %04x", val2);
-                       } else {
-                               pci_read_config_byte(bdf, addr, &val1);
-                               printf(" %02x", val1);
-                       }
-                       addr += size;
+               linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
+               for (i = 0; i < linebytes; i += byte_size) {
+                       unsigned long val;
+
+#ifdef CONFIG_DM_PCI
+                       dm_pci_read_config(dev, addr, &val, size);
+#else
+                       val = pci_read_config(bdf, addr, size);
+#endif
+                       printf(" %0*lx", pci_field_width(size), val);
+                       addr += byte_size;
                }
                printf("\n");
                nbytes -= linebytes;
@@ -369,6 +455,7 @@ static int pci_cfg_display(pci_dev_t bdf, ulong addr, ulong size, ulong length)
        return (rc);
 }
 
+#ifndef CONFIG_DM_PCI
 static int pci_cfg_write (pci_dev_t bdf, ulong addr, ulong size, ulong value)
 {
        if (size == 4) {
@@ -384,33 +471,31 @@ static int pci_cfg_write (pci_dev_t bdf, ulong addr, ulong size, ulong value)
        }
        return 0;
 }
+#endif
 
-static int
-pci_cfg_modify (pci_dev_t bdf, ulong addr, ulong size, ulong value, int incrflag)
+#ifdef CONFIG_DM_PCI
+static int pci_cfg_modify(struct udevice *dev, ulong addr, ulong size,
+                         ulong value, int incrflag)
+#else
+static int pci_cfg_modify(pci_dev_t bdf, ulong addr, ulong size, ulong value,
+                         int incrflag)
+#endif
 {
        ulong   i;
        int     nbytes;
-       uint    val4;
-       ushort  val2;
-       u_char  val1;
+       ulong val;
 
        /* Print the address, followed by value.  Then accept input for
         * the next value.  A non-converted value exits.
         */
        do {
                printf("%08lx:", addr);
-               if (size == 4) {
-                       pci_read_config_dword(bdf, addr, &val4);
-                       printf(" %08x", val4);
-               }
-               else if (size == 2) {
-                       pci_read_config_word(bdf, addr, &val2);
-                       printf(" %04x", val2);
-               }
-               else {
-                       pci_read_config_byte(bdf, addr, &val1);
-                       printf(" %02x", val1);
-               }
+#ifdef CONFIG_DM_PCI
+               dm_pci_read_config(dev, addr, &val, size);
+#else
+               val = pci_read_config(bdf, addr, size);
+#endif
+               printf(" %0*lx", pci_field_width(size), val);
 
                nbytes = cli_readline(" ? ");
                if (nbytes == 0 || (nbytes == 1 && console_buffer[0] == '-')) {
@@ -436,7 +521,11 @@ pci_cfg_modify (pci_dev_t bdf, ulong addr, ulong size, ulong value, int incrflag
                                /* good enough to not time out
                                 */
                                bootretry_reset_cmd_timeout();
-                               pci_cfg_write (bdf, addr, size, i);
+#ifdef CONFIG_DM_PCI
+                               dm_pci_write_config(dev, addr, i, size);
+#else
+                               pci_cfg_write(bdf, addr, size, i);
+#endif
                                if (incrflag)
                                        addr += size;
                        }
@@ -456,7 +545,13 @@ pci_cfg_modify (pci_dev_t bdf, ulong addr, ulong size, ulong value, int incrflag
  */
 static int do_pci(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
-       ulong addr = 0, value = 0, size = 0;
+       ulong addr = 0, value = 0, cmd_size = 0;
+       enum pci_size_t size = PCI_SIZE_32;
+#ifdef CONFIG_DM_PCI
+       struct udevice *dev, *bus;
+#else
+       pci_dev_t dev;
+#endif
        int busnum = 0;
        pci_dev_t bdf = 0;
        char cmd = 's';
@@ -471,7 +566,8 @@ static int do_pci(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
        case 'm':               /* modify */
        case 'w':               /* write */
                /* Check for a size specification. */
-               size = cmd_get_data_size(argv[1], 4);
+               cmd_size = cmd_get_data_size(argv[1], 4);
+               size = (cmd_size == 4) ? PCI_SIZE_32 : cmd_size - 1;
                if (argc > 3)
                        addr = simple_strtoul(argv[3], NULL, 16);
                if (argc > 4)
@@ -496,16 +592,35 @@ static int do_pci(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
                        if (argc > 1)
                                busnum = simple_strtoul(argv[1], NULL, 16);
                }
+#ifdef CONFIG_DM_PCI
+               ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, &bus);
+               if (ret) {
+                       printf("No such bus\n");
+                       return CMD_RET_FAILURE;
+               }
+               pciinfo(bus, value);
+#else
                pciinfo(busnum, value);
+#endif
                return 0;
        }
 
+#ifdef CONFIG_DM_PCI
+       ret = dm_pci_bus_find_bdf(bdf, &dev);
+       if (ret) {
+               printf("No such device\n");
+               return CMD_RET_FAILURE;
+       }
+#else
+       dev = bdf;
+#endif
+
        switch (argv[1][0]) {
        case 'h':               /* header */
-               pci_header_show(bdf);
+               pci_header_show(dev);
                break;
        case 'd':               /* display */
-               return pci_cfg_display(bdf, addr, size, value);
+               return pci_cfg_display(dev, addr, size, value);
 #ifdef CONFIG_CMD_PCI_ENUM
        case 'e':
 # ifdef CONFIG_DM_PCI
@@ -518,17 +633,21 @@ static int do_pci(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
        case 'n':               /* next */
                if (argc < 4)
                        goto usage;
-               ret = pci_cfg_modify(bdf, addr, size, value, 0);
+               ret = pci_cfg_modify(dev, addr, size, value, 0);
                break;
        case 'm':               /* modify */
                if (argc < 4)
                        goto usage;
-               ret = pci_cfg_modify(bdf, addr, size, value, 1);
+               ret = pci_cfg_modify(dev, addr, size, value, 1);
                break;
        case 'w':               /* write */
                if (argc < 5)
                        goto usage;
-               ret = pci_cfg_write(bdf, addr, size, value);
+#ifdef CONFIG_DM_PCI
+               ret = dm_pci_write_config(dev, addr, value, size);
+#else
+               ret = pci_cfg_write(dev, addr, size, value);
+#endif
                break;
        default:
                ret = CMD_RET_USAGE;