]> git.ipfire.org Git - people/ms/u-boot.git/blobdiff - drivers/pci/pci-uclass.c
pci: Fix decode regions for memory banks
[people/ms/u-boot.git] / drivers / pci / pci-uclass.c
index 519052efe3833b4523d776a3adbf8726b1c90f9f..ad43e8a27cc12404a0b198ece82e0dd8760ea75b 100644 (file)
@@ -8,13 +8,11 @@
 #include <common.h>
 #include <dm.h>
 #include <errno.h>
-#include <fdtdec.h>
 #include <inttypes.h>
 #include <pci.h>
 #include <asm/io.h>
-#include <dm/lists.h>
-#include <dm/root.h>
 #include <dm/device-internal.h>
+#include <dm/lists.h>
 #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
 #include <asm/fsp/fsp_support.h>
 #endif
@@ -250,6 +248,21 @@ int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset,
        return ops->write_config(bus, bdf, offset, value, size);
 }
 
+int pci_bus_clrset_config32(struct udevice *bus, pci_dev_t bdf, int offset,
+                           u32 clr, u32 set)
+{
+       ulong val;
+       int ret;
+
+       ret = pci_bus_read_config(bus, bdf, offset, &val, PCI_SIZE_32);
+       if (ret)
+               return ret;
+       val &= ~clr;
+       val |= set;
+
+       return pci_bus_write_config(bus, bdf, offset, val, PCI_SIZE_32);
+}
+
 int pci_write_config(pci_dev_t bdf, int offset, unsigned long value,
                     enum pci_size_t size)
 {
@@ -274,7 +287,6 @@ int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value,
                                    size);
 }
 
-
 int pci_write_config32(pci_dev_t bdf, int offset, u32 value)
 {
        return pci_write_config(bdf, offset, value, PCI_SIZE_32);
@@ -418,6 +430,48 @@ int dm_pci_read_config32(struct udevice *dev, int offset, u32 *valuep)
        return 0;
 }
 
+int dm_pci_clrset_config8(struct udevice *dev, int offset, u32 clr, u32 set)
+{
+       u8 val;
+       int ret;
+
+       ret = dm_pci_read_config8(dev, offset, &val);
+       if (ret)
+               return ret;
+       val &= ~clr;
+       val |= set;
+
+       return dm_pci_write_config8(dev, offset, val);
+}
+
+int dm_pci_clrset_config16(struct udevice *dev, int offset, u32 clr, u32 set)
+{
+       u16 val;
+       int ret;
+
+       ret = dm_pci_read_config16(dev, offset, &val);
+       if (ret)
+               return ret;
+       val &= ~clr;
+       val |= set;
+
+       return dm_pci_write_config16(dev, offset, val);
+}
+
+int dm_pci_clrset_config32(struct udevice *dev, int offset, u32 clr, u32 set)
+{
+       u32 val;
+       int ret;
+
+       ret = dm_pci_read_config32(dev, offset, &val);
+       if (ret)
+               return ret;
+       val &= ~clr;
+       val |= set;
+
+       return dm_pci_write_config32(dev, offset, val);
+}
+
 static void set_vga_bridge_bits(struct udevice *dev)
 {
        struct udevice *parent = dev->parent;
@@ -464,6 +518,64 @@ int pci_auto_config_devices(struct udevice *bus)
        return sub_bus;
 }
 
+int pci_generic_mmap_write_config(
+       struct udevice *bus,
+       int (*addr_f)(struct udevice *bus, pci_dev_t bdf, uint offset, void **addrp),
+       pci_dev_t bdf,
+       uint offset,
+       ulong value,
+       enum pci_size_t size)
+{
+       void *address;
+
+       if (addr_f(bus, bdf, offset, &address) < 0)
+               return 0;
+
+       switch (size) {
+       case PCI_SIZE_8:
+               writeb(value, address);
+               return 0;
+       case PCI_SIZE_16:
+               writew(value, address);
+               return 0;
+       case PCI_SIZE_32:
+               writel(value, address);
+               return 0;
+       default:
+               return -EINVAL;
+       }
+}
+
+int pci_generic_mmap_read_config(
+       struct udevice *bus,
+       int (*addr_f)(struct udevice *bus, pci_dev_t bdf, uint offset, void **addrp),
+       pci_dev_t bdf,
+       uint offset,
+       ulong *valuep,
+       enum pci_size_t size)
+{
+       void *address;
+
+       if (addr_f(bus, bdf, offset, &address) < 0) {
+               *valuep = pci_get_ff(size);
+               return 0;
+       }
+
+       switch (size) {
+       case PCI_SIZE_8:
+               *valuep = readb(address);
+               return 0;
+       case PCI_SIZE_16:
+               *valuep = readw(address);
+               return 0;
+       case PCI_SIZE_32:
+               *valuep = readl(address);
+               return 0;
+       default:
+               return -EINVAL;
+       }
+}
+
 int dm_pci_hose_probe_bus(struct udevice *bus)
 {
        int sub_bus;
@@ -496,9 +608,10 @@ int dm_pci_hose_probe_bus(struct udevice *bus)
  * pci_match_one_device - Tell if a PCI device structure has a matching
  *                        PCI device id structure
  * @id: single PCI device id structure to match
- * @dev: the PCI device structure to match against
+ * @find: the PCI device id structure to match against
  *
- * Returns the matching pci_device_id structure or %NULL if there is no match.
+ * Returns true if the finding pci_device_id structure matched or false if
+ * there is no match.
  */
 static bool pci_match_one_id(const struct pci_device_id *id,
                             const struct pci_device_id *find)
@@ -604,6 +717,7 @@ static int pci_find_and_bind_driver(struct udevice *parent,
        ret = device_bind_driver(parent, drv, str, devp);
        if (ret) {
                debug("%s: Failed to bind generic driver: %d\n", __func__, ret);
+               free(str);
                return ret;
        }
        debug("%s: No match found: bound generic driver instead\n", __func__);
@@ -626,7 +740,7 @@ int pci_bind_bus_devices(struct udevice *bus)
        found_multi = false;
        end = PCI_BDF(bus->seq, PCI_MAX_PCI_DEVICES - 1,
                      PCI_MAX_PCI_FUNCTIONS - 1);
-       for (bdf = PCI_BDF(bus->seq, 0, 0); bdf < end;
+       for (bdf = PCI_BDF(bus->seq, 0, 0); bdf <= end;
             bdf += PCI_BDF(0, 0, 1)) {
                struct pci_child_platdata *pplat;
                struct udevice *dev;
@@ -697,43 +811,21 @@ error:
        return ret;
 }
 
-static int pci_uclass_post_bind(struct udevice *bus)
-{
-       /*
-        * If there is no pci device listed in the device tree,
-        * don't bother scanning the device tree.
-        */
-       if (bus->of_offset == -1)
-               return 0;
-
-       /*
-        * Scan the device tree for devices. This does not probe the PCI bus,
-        * as this is not permitted while binding. It just finds devices
-        * mentioned in the device tree.
-        *
-        * Before relocation, only bind devices marked for pre-relocation
-        * use.
-        */
-       return dm_scan_fdt_node(bus, gd->fdt_blob, bus->of_offset,
-                               gd->flags & GD_FLG_RELOC ? false : true);
-}
-
-static int decode_regions(struct pci_controller *hose, const void *blob,
-                         int parent_node, int node)
+static int decode_regions(struct pci_controller *hose, ofnode parent_node,
+                         ofnode node)
 {
        int pci_addr_cells, addr_cells, size_cells;
-       phys_addr_t base = 0, size;
        int cells_per_record;
        const u32 *prop;
        int len;
        int i;
 
-       prop = fdt_getprop(blob, node, "ranges", &len);
+       prop = ofnode_get_property(node, "ranges", &len);
        if (!prop)
                return -EINVAL;
-       pci_addr_cells = fdt_address_cells(blob, node);
-       addr_cells = fdt_address_cells(blob, parent_node);
-       size_cells = fdt_size_cells(blob, node);
+       pci_addr_cells = ofnode_read_simple_addr_cells(node);
+       addr_cells = ofnode_read_simple_addr_cells(parent_node);
+       size_cells = ofnode_read_simple_size_cells(node);
 
        /* PCI addresses are always 3-cells */
        len /= sizeof(u32);
@@ -781,6 +873,21 @@ static int decode_regions(struct pci_controller *hose, const void *blob,
        }
 
        /* Add a region for our local memory */
+#ifdef CONFIG_NR_DRAM_BANKS
+       bd_t *bd = gd->bd;
+
+       for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
+               if (bd->bi_dram[i].size) {
+                       pci_set_region(hose->regions + hose->region_count++,
+                                      bd->bi_dram[i].start,
+                                      bd->bi_dram[i].start,
+                                      bd->bi_dram[i].size,
+                                      PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
+               }
+       }
+#else
+       phys_addr_t base = 0, size;
+
        size = gd->ram_size;
 #ifdef CONFIG_SYS_SDRAM_BASE
        base = CONFIG_SYS_SDRAM_BASE;
@@ -789,6 +896,7 @@ static int decode_regions(struct pci_controller *hose, const void *blob,
                size = gd->pci_ram_top - base;
        pci_set_region(hose->regions + hose->region_count++, base, base,
                       size, PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
+#endif
 
        return 0;
 }
@@ -803,10 +911,10 @@ static int pci_uclass_pre_probe(struct udevice *bus)
        hose = bus->uclass_priv;
 
        /* For bridges, use the top-level PCI controller */
-       if (device_get_uclass_id(bus->parent) == UCLASS_ROOT) {
+       if (!device_is_on_pci_bus(bus)) {
                hose->ctlr = bus;
-               ret = decode_regions(hose, gd->fdt_blob, bus->parent->of_offset,
-                               bus->of_offset);
+               ret = decode_regions(hose, dev_ofnode(bus->parent),
+                                    dev_ofnode(bus));
                if (ret) {
                        debug("%s: Cannot decode regions\n", __func__);
                        return ret;
@@ -869,7 +977,7 @@ static int pci_uclass_child_post_bind(struct udevice *dev)
        struct fdt_pci_addr addr;
        int ret;
 
-       if (dev->of_offset == -1)
+       if (!dev_of_valid(dev))
                return 0;
 
        /*
@@ -877,8 +985,8 @@ static int pci_uclass_child_post_bind(struct udevice *dev)
         * just check the address.
         */
        pplat = dev_get_parent_platdata(dev);
-       ret = fdtdec_get_pci_addr(gd->fdt_blob, dev->of_offset,
-                                 FDT_PCI_SPACE_CONFIG, "reg", &addr);
+       ret = ofnode_read_pci_addr(dev_ofnode(dev), FDT_PCI_SPACE_CONFIG, "reg",
+                                  &addr);
 
        if (ret) {
                if (ret != -ENOENT)
@@ -1198,7 +1306,7 @@ UCLASS_DRIVER(pci) = {
        .id             = UCLASS_PCI,
        .name           = "pci",
        .flags          = DM_UC_FLAG_SEQ_ALIAS,
-       .post_bind      = pci_uclass_post_bind,
+       .post_bind      = dm_scan_fdt_dev,
        .pre_probe      = pci_uclass_pre_probe,
        .post_probe     = pci_uclass_post_probe,
        .child_post_bind = pci_uclass_child_post_bind,