1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2014 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
13 #include <dm/device-internal.h>
15 #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
16 #include <asm/fsp/fsp_support.h>
18 #include "pci_internal.h"
20 DECLARE_GLOBAL_DATA_PTR;
22 int pci_get_bus(int busnum, struct udevice **busp)
26 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
28 /* Since buses may not be numbered yet try a little harder with bus 0 */
30 ret = uclass_first_device_err(UCLASS_PCI, busp);
33 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
39 struct udevice *pci_get_controller(struct udevice *dev)
41 while (device_is_on_pci_bus(dev))
47 pci_dev_t dm_pci_get_bdf(const struct udevice *dev)
49 struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
50 struct udevice *bus = dev->parent;
53 * This error indicates that @dev is a device on an unprobed PCI bus.
54 * The bus likely has bus=seq == -1, so the PCI_ADD_BUS() macro below
55 * will produce a bad BDF>
57 * A common cause of this problem is that this function is called in the
58 * ofdata_to_platdata() method of @dev. Accessing the PCI bus in that
59 * method is not allowed, since it has not yet been probed. To fix this,
60 * move that access to the probe() method of @dev instead.
62 if (!device_active(bus))
63 log_err("PCI: Device '%s' on unprobed bus '%s'\n", dev->name,
65 return PCI_ADD_BUS(bus->seq, pplat->devfn);
69 * pci_get_bus_max() - returns the bus number of the last active bus
71 * @return last bus number, or -1 if no active buses
73 static int pci_get_bus_max(void)
79 ret = uclass_get(UCLASS_PCI, &uc);
80 uclass_foreach_dev(bus, uc) {
85 debug("%s: ret=%d\n", __func__, ret);
90 int pci_last_busno(void)
92 return pci_get_bus_max();
95 int pci_get_ff(enum pci_size_t size)
107 static void pci_dev_find_ofnode(struct udevice *bus, phys_addr_t bdf,
110 struct fdt_pci_addr addr;
114 dev_for_each_subnode(node, bus) {
115 ret = ofnode_read_pci_addr(node, FDT_PCI_SPACE_CONFIG, "reg",
120 if (PCI_MASK_BUS(addr.phys_hi) != PCI_MASK_BUS(bdf))
128 int pci_bus_find_devfn(const struct udevice *bus, pci_dev_t find_devfn,
129 struct udevice **devp)
133 for (device_find_first_child(bus, &dev);
135 device_find_next_child(&dev)) {
136 struct pci_child_platdata *pplat;
138 pplat = dev_get_parent_platdata(dev);
139 if (pplat && pplat->devfn == find_devfn) {
148 int dm_pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp)
153 ret = pci_get_bus(PCI_BUS(bdf), &bus);
156 return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp);
159 static int pci_device_matches_ids(struct udevice *dev,
160 struct pci_device_id *ids)
162 struct pci_child_platdata *pplat;
165 pplat = dev_get_parent_platdata(dev);
168 for (i = 0; ids[i].vendor != 0; i++) {
169 if (pplat->vendor == ids[i].vendor &&
170 pplat->device == ids[i].device)
177 int pci_bus_find_devices(struct udevice *bus, struct pci_device_id *ids,
178 int *indexp, struct udevice **devp)
182 /* Scan all devices on this bus */
183 for (device_find_first_child(bus, &dev);
185 device_find_next_child(&dev)) {
186 if (pci_device_matches_ids(dev, ids) >= 0) {
187 if ((*indexp)-- <= 0) {
197 int pci_find_device_id(struct pci_device_id *ids, int index,
198 struct udevice **devp)
202 /* Scan all known buses */
203 for (uclass_first_device(UCLASS_PCI, &bus);
205 uclass_next_device(&bus)) {
206 if (!pci_bus_find_devices(bus, ids, &index, devp))
214 static int dm_pci_bus_find_device(struct udevice *bus, unsigned int vendor,
215 unsigned int device, int *indexp,
216 struct udevice **devp)
218 struct pci_child_platdata *pplat;
221 for (device_find_first_child(bus, &dev);
223 device_find_next_child(&dev)) {
224 pplat = dev_get_parent_platdata(dev);
225 if (pplat->vendor == vendor && pplat->device == device) {
236 int dm_pci_find_device(unsigned int vendor, unsigned int device, int index,
237 struct udevice **devp)
241 /* Scan all known buses */
242 for (uclass_first_device(UCLASS_PCI, &bus);
244 uclass_next_device(&bus)) {
245 if (!dm_pci_bus_find_device(bus, vendor, device, &index, devp))
246 return device_probe(*devp);
253 int dm_pci_find_class(uint find_class, int index, struct udevice **devp)
257 /* Scan all known buses */
258 for (pci_find_first_device(&dev);
260 pci_find_next_device(&dev)) {
261 struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
263 if (pplat->class == find_class && !index--) {
265 return device_probe(*devp);
273 int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset,
274 unsigned long value, enum pci_size_t size)
276 struct dm_pci_ops *ops;
278 ops = pci_get_ops(bus);
279 if (!ops->write_config)
281 return ops->write_config(bus, bdf, offset, value, size);
284 int pci_bus_clrset_config32(struct udevice *bus, pci_dev_t bdf, int offset,
290 ret = pci_bus_read_config(bus, bdf, offset, &val, PCI_SIZE_32);
296 return pci_bus_write_config(bus, bdf, offset, val, PCI_SIZE_32);
299 int pci_write_config(pci_dev_t bdf, int offset, unsigned long value,
300 enum pci_size_t size)
305 ret = pci_get_bus(PCI_BUS(bdf), &bus);
309 return pci_bus_write_config(bus, bdf, offset, value, size);
312 int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value,
313 enum pci_size_t size)
317 for (bus = dev; device_is_on_pci_bus(bus);)
319 return pci_bus_write_config(bus, dm_pci_get_bdf(dev), offset, value,
323 int pci_write_config32(pci_dev_t bdf, int offset, u32 value)
325 return pci_write_config(bdf, offset, value, PCI_SIZE_32);
328 int pci_write_config16(pci_dev_t bdf, int offset, u16 value)
330 return pci_write_config(bdf, offset, value, PCI_SIZE_16);
333 int pci_write_config8(pci_dev_t bdf, int offset, u8 value)
335 return pci_write_config(bdf, offset, value, PCI_SIZE_8);
338 int dm_pci_write_config8(struct udevice *dev, int offset, u8 value)
340 return dm_pci_write_config(dev, offset, value, PCI_SIZE_8);
343 int dm_pci_write_config16(struct udevice *dev, int offset, u16 value)
345 return dm_pci_write_config(dev, offset, value, PCI_SIZE_16);
348 int dm_pci_write_config32(struct udevice *dev, int offset, u32 value)
350 return dm_pci_write_config(dev, offset, value, PCI_SIZE_32);
353 int pci_bus_read_config(const struct udevice *bus, pci_dev_t bdf, int offset,
354 unsigned long *valuep, enum pci_size_t size)
356 struct dm_pci_ops *ops;
358 ops = pci_get_ops(bus);
359 if (!ops->read_config)
361 return ops->read_config(bus, bdf, offset, valuep, size);
364 int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep,
365 enum pci_size_t size)
370 ret = pci_get_bus(PCI_BUS(bdf), &bus);
374 return pci_bus_read_config(bus, bdf, offset, valuep, size);
377 int dm_pci_read_config(const struct udevice *dev, int offset,
378 unsigned long *valuep, enum pci_size_t size)
380 const struct udevice *bus;
382 for (bus = dev; device_is_on_pci_bus(bus);)
384 return pci_bus_read_config(bus, dm_pci_get_bdf(dev), offset, valuep,
388 int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep)
393 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32);
401 int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep)
406 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16);
414 int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep)
419 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8);
427 int dm_pci_read_config8(const struct udevice *dev, int offset, u8 *valuep)
432 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_8);
440 int dm_pci_read_config16(const struct udevice *dev, int offset, u16 *valuep)
445 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_16);
453 int dm_pci_read_config32(const struct udevice *dev, int offset, u32 *valuep)
458 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_32);
466 int dm_pci_clrset_config8(struct udevice *dev, int offset, u32 clr, u32 set)
471 ret = dm_pci_read_config8(dev, offset, &val);
477 return dm_pci_write_config8(dev, offset, val);
480 int dm_pci_clrset_config16(struct udevice *dev, int offset, u32 clr, u32 set)
485 ret = dm_pci_read_config16(dev, offset, &val);
491 return dm_pci_write_config16(dev, offset, val);
494 int dm_pci_clrset_config32(struct udevice *dev, int offset, u32 clr, u32 set)
499 ret = dm_pci_read_config32(dev, offset, &val);
505 return dm_pci_write_config32(dev, offset, val);
508 static void set_vga_bridge_bits(struct udevice *dev)
510 struct udevice *parent = dev->parent;
513 while (parent->seq != 0) {
514 dm_pci_read_config16(parent, PCI_BRIDGE_CONTROL, &bc);
515 bc |= PCI_BRIDGE_CTL_VGA;
516 dm_pci_write_config16(parent, PCI_BRIDGE_CONTROL, bc);
517 parent = parent->parent;
521 int pci_auto_config_devices(struct udevice *bus)
523 struct pci_controller *hose = bus->uclass_priv;
524 struct pci_child_platdata *pplat;
525 unsigned int sub_bus;
530 debug("%s: start\n", __func__);
531 pciauto_config_init(hose);
532 for (ret = device_find_first_child(bus, &dev);
534 ret = device_find_next_child(&dev)) {
535 unsigned int max_bus;
538 debug("%s: device %s\n", __func__, dev->name);
539 ret = dm_pciauto_config_device(dev);
543 sub_bus = max(sub_bus, max_bus);
545 pplat = dev_get_parent_platdata(dev);
546 if (pplat->class == (PCI_CLASS_DISPLAY_VGA << 8))
547 set_vga_bridge_bits(dev);
549 debug("%s: done\n", __func__);
554 int pci_generic_mmap_write_config(
555 const struct udevice *bus,
556 int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
561 enum pci_size_t size)
565 if (addr_f(bus, bdf, offset, &address) < 0)
570 writeb(value, address);
573 writew(value, address);
576 writel(value, address);
583 int pci_generic_mmap_read_config(
584 const struct udevice *bus,
585 int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
590 enum pci_size_t size)
594 if (addr_f(bus, bdf, offset, &address) < 0) {
595 *valuep = pci_get_ff(size);
601 *valuep = readb(address);
604 *valuep = readw(address);
607 *valuep = readl(address);
614 int dm_pci_hose_probe_bus(struct udevice *bus)
619 debug("%s\n", __func__);
621 sub_bus = pci_get_bus_max() + 1;
622 debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
623 dm_pciauto_prescan_setup_bridge(bus, sub_bus);
625 ret = device_probe(bus);
627 debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name,
631 if (sub_bus != bus->seq) {
632 printf("%s: Internal error, bus '%s' got seq %d, expected %d\n",
633 __func__, bus->name, bus->seq, sub_bus);
636 sub_bus = pci_get_bus_max();
637 dm_pciauto_postscan_setup_bridge(bus, sub_bus);
643 * pci_match_one_device - Tell if a PCI device structure has a matching
644 * PCI device id structure
645 * @id: single PCI device id structure to match
646 * @find: the PCI device id structure to match against
648 * Returns true if the finding pci_device_id structure matched or false if
651 static bool pci_match_one_id(const struct pci_device_id *id,
652 const struct pci_device_id *find)
654 if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
655 (id->device == PCI_ANY_ID || id->device == find->device) &&
656 (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
657 (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
658 !((id->class ^ find->class) & id->class_mask))
665 * pci_find_and_bind_driver() - Find and bind the right PCI driver
667 * This only looks at certain fields in the descriptor.
669 * @parent: Parent bus
670 * @find_id: Specification of the driver to find
671 * @bdf: Bus/device/function addreess - see PCI_BDF()
672 * @devp: Returns a pointer to the device created
673 * @return 0 if OK, -EPERM if the device is not needed before relocation and
674 * therefore was not created, other -ve value on error
676 static int pci_find_and_bind_driver(struct udevice *parent,
677 struct pci_device_id *find_id,
678 pci_dev_t bdf, struct udevice **devp)
680 struct pci_driver_entry *start, *entry;
681 ofnode node = ofnode_null();
690 debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
691 find_id->vendor, find_id->device);
693 /* Determine optional OF node */
694 pci_dev_find_ofnode(parent, bdf, &node);
696 if (ofnode_valid(node) && !ofnode_is_available(node)) {
697 debug("%s: Ignoring disabled device\n", __func__);
701 start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
702 n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
703 for (entry = start; entry != start + n_ents; entry++) {
704 const struct pci_device_id *id;
706 const struct driver *drv;
708 for (id = entry->match;
709 id->vendor || id->subvendor || id->class_mask;
711 if (!pci_match_one_id(id, find_id))
717 * In the pre-relocation phase, we only bind devices
718 * whose driver has the DM_FLAG_PRE_RELOC set, to save
719 * precious memory space as on some platforms as that
720 * space is pretty limited (ie: using Cache As RAM).
722 if (!(gd->flags & GD_FLG_RELOC) &&
723 !(drv->flags & DM_FLAG_PRE_RELOC))
727 * We could pass the descriptor to the driver as
728 * platdata (instead of NULL) and allow its bind()
729 * method to return -ENOENT if it doesn't support this
730 * device. That way we could continue the search to
731 * find another driver. For now this doesn't seem
732 * necesssary, so just bind the first match.
734 ret = device_bind_ofnode(parent, drv, drv->name, NULL,
738 debug("%s: Match found: %s\n", __func__, drv->name);
739 dev->driver_data = id->driver_data;
745 bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
747 * In the pre-relocation phase, we only bind bridge devices to save
748 * precious memory space as on some platforms as that space is pretty
749 * limited (ie: using Cache As RAM).
751 if (!(gd->flags & GD_FLG_RELOC) && !bridge)
754 /* Bind a generic driver so that the device can be used */
755 sprintf(name, "pci_%x:%x.%x", parent->seq, PCI_DEV(bdf),
760 drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
762 ret = device_bind_driver_to_node(parent, drv, str, node, devp);
764 debug("%s: Failed to bind generic driver: %d\n", __func__, ret);
768 debug("%s: No match found: bound generic driver instead\n", __func__);
773 debug("%s: No match found: error %d\n", __func__, ret);
777 int pci_bind_bus_devices(struct udevice *bus)
779 ulong vendor, device;
786 end = PCI_BDF(bus->seq, PCI_MAX_PCI_DEVICES - 1,
787 PCI_MAX_PCI_FUNCTIONS - 1);
788 for (bdf = PCI_BDF(bus->seq, 0, 0); bdf <= end;
789 bdf += PCI_BDF(0, 0, 1)) {
790 struct pci_child_platdata *pplat;
796 if (PCI_FUNC(bdf) && !found_multi)
799 /* Check only the first access, we don't expect problems */
800 ret = pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
805 if (vendor == 0xffff || vendor == 0x0000)
808 pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
809 &header_type, PCI_SIZE_8);
812 found_multi = header_type & 0x80;
814 debug("%s: bus %d/%s: found device %x, function %d", __func__,
815 bus->seq, bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
816 pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
818 pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
822 /* Find this device in the device tree */
823 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
824 debug(": find ret=%d\n", ret);
826 /* If nothing in the device tree, bind a device */
827 if (ret == -ENODEV) {
828 struct pci_device_id find_id;
831 memset(&find_id, '\0', sizeof(find_id));
832 find_id.vendor = vendor;
833 find_id.device = device;
834 find_id.class = class;
835 if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
836 pci_bus_read_config(bus, bdf,
837 PCI_SUBSYSTEM_VENDOR_ID,
839 find_id.subvendor = val & 0xffff;
840 find_id.subdevice = val >> 16;
842 ret = pci_find_and_bind_driver(bus, &find_id, bdf,
850 /* Update the platform data */
851 pplat = dev_get_parent_platdata(dev);
852 pplat->devfn = PCI_MASK_BUS(bdf);
853 pplat->vendor = vendor;
854 pplat->device = device;
855 pplat->class = class;
860 printf("Cannot read bus configuration: %d\n", ret);
865 static void decode_regions(struct pci_controller *hose, ofnode parent_node,
868 int pci_addr_cells, addr_cells, size_cells;
869 int cells_per_record;
874 prop = ofnode_get_property(node, "ranges", &len);
876 debug("%s: Cannot decode regions\n", __func__);
880 pci_addr_cells = ofnode_read_simple_addr_cells(node);
881 addr_cells = ofnode_read_simple_addr_cells(parent_node);
882 size_cells = ofnode_read_simple_size_cells(node);
884 /* PCI addresses are always 3-cells */
886 cells_per_record = pci_addr_cells + addr_cells + size_cells;
887 hose->region_count = 0;
888 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
890 for (i = 0; i < MAX_PCI_REGIONS; i++, len -= cells_per_record) {
891 u64 pci_addr, addr, size;
897 if (len < cells_per_record)
899 flags = fdt32_to_cpu(prop[0]);
900 space_code = (flags >> 24) & 3;
901 pci_addr = fdtdec_get_number(prop + 1, 2);
902 prop += pci_addr_cells;
903 addr = fdtdec_get_number(prop, addr_cells);
905 size = fdtdec_get_number(prop, size_cells);
907 debug("%s: region %d, pci_addr=%llx, addr=%llx, size=%llx, space_code=%d\n",
908 __func__, hose->region_count, pci_addr, addr, size, space_code);
909 if (space_code & 2) {
910 type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
912 } else if (space_code & 1) {
913 type = PCI_REGION_IO;
918 if (!IS_ENABLED(CONFIG_SYS_PCI_64BIT) &&
919 type == PCI_REGION_MEM && upper_32_bits(pci_addr)) {
920 debug(" - beyond the 32-bit boundary, ignoring\n");
925 for (i = 0; i < hose->region_count; i++) {
926 if (hose->regions[i].flags == type)
930 pos = hose->region_count++;
931 debug(" - type=%d, pos=%d\n", type, pos);
932 pci_set_region(hose->regions + pos, pci_addr, addr, size, type);
935 /* Add a region for our local memory */
936 #ifdef CONFIG_NR_DRAM_BANKS
942 for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
943 if (hose->region_count == MAX_PCI_REGIONS) {
944 pr_err("maximum number of regions parsed, aborting\n");
948 if (bd->bi_dram[i].size) {
949 pci_set_region(hose->regions + hose->region_count++,
950 bd->bi_dram[i].start,
951 bd->bi_dram[i].start,
953 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
957 phys_addr_t base = 0, size;
960 #ifdef CONFIG_SYS_SDRAM_BASE
961 base = CONFIG_SYS_SDRAM_BASE;
963 if (gd->pci_ram_top && gd->pci_ram_top < base + size)
964 size = gd->pci_ram_top - base;
966 pci_set_region(hose->regions + hose->region_count++, base,
967 base, size, PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
973 static int pci_uclass_pre_probe(struct udevice *bus)
975 struct pci_controller *hose;
977 debug("%s, bus=%d/%s, parent=%s\n", __func__, bus->seq, bus->name,
979 hose = bus->uclass_priv;
981 /* For bridges, use the top-level PCI controller */
982 if (!device_is_on_pci_bus(bus)) {
984 decode_regions(hose, dev_ofnode(bus->parent), dev_ofnode(bus));
986 struct pci_controller *parent_hose;
988 parent_hose = dev_get_uclass_priv(bus->parent);
989 hose->ctlr = parent_hose->bus;
992 hose->first_busno = bus->seq;
993 hose->last_busno = bus->seq;
994 hose->skip_auto_config_until_reloc =
995 dev_read_bool(bus, "u-boot,skip-auto-config-until-reloc");
1000 static int pci_uclass_post_probe(struct udevice *bus)
1002 struct pci_controller *hose = dev_get_uclass_priv(bus);
1005 debug("%s: probing bus %d\n", __func__, bus->seq);
1006 ret = pci_bind_bus_devices(bus);
1010 if (CONFIG_IS_ENABLED(PCI_PNP) &&
1011 (!hose->skip_auto_config_until_reloc ||
1012 (gd->flags & GD_FLG_RELOC))) {
1013 ret = pci_auto_config_devices(bus);
1015 return log_msg_ret("pci auto-config", ret);
1018 #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
1020 * Per Intel FSP specification, we should call FSP notify API to
1021 * inform FSP that PCI enumeration has been done so that FSP will
1022 * do any necessary initialization as required by the chipset's
1023 * BIOS Writer's Guide (BWG).
1025 * Unfortunately we have to put this call here as with driver model,
1026 * the enumeration is all done on a lazy basis as needed, so until
1027 * something is touched on PCI it won't happen.
1029 * Note we only call this 1) after U-Boot is relocated, and 2)
1030 * root bus has finished probing.
1032 if ((gd->flags & GD_FLG_RELOC) && (bus->seq == 0)) {
1033 ret = fsp_init_phase_pci();
1042 static int pci_uclass_child_post_bind(struct udevice *dev)
1044 struct pci_child_platdata *pplat;
1046 if (!dev_of_valid(dev))
1049 pplat = dev_get_parent_platdata(dev);
1051 /* Extract vendor id and device id if available */
1052 ofnode_read_pci_vendev(dev_ofnode(dev), &pplat->vendor, &pplat->device);
1054 /* Extract the devfn from fdt_pci_addr */
1055 pplat->devfn = pci_get_devfn(dev);
1060 static int pci_bridge_read_config(const struct udevice *bus, pci_dev_t bdf,
1061 uint offset, ulong *valuep,
1062 enum pci_size_t size)
1064 struct pci_controller *hose = bus->uclass_priv;
1066 return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
1069 static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
1070 uint offset, ulong value,
1071 enum pci_size_t size)
1073 struct pci_controller *hose = bus->uclass_priv;
1075 return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
1078 static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
1080 struct udevice *dev;
1084 * Scan through all the PCI controllers. On x86 there will only be one
1085 * but that is not necessarily true on other hardware.
1088 device_find_first_child(bus, &dev);
1093 ret = uclass_next_device(&bus);
1101 int pci_find_next_device(struct udevice **devp)
1103 struct udevice *child = *devp;
1104 struct udevice *bus = child->parent;
1107 /* First try all the siblings */
1110 device_find_next_child(&child);
1117 /* We ran out of siblings. Try the next bus */
1118 ret = uclass_next_device(&bus);
1122 return bus ? skip_to_next_device(bus, devp) : 0;
1125 int pci_find_first_device(struct udevice **devp)
1127 struct udevice *bus;
1131 ret = uclass_first_device(UCLASS_PCI, &bus);
1135 return skip_to_next_device(bus, devp);
1138 ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size)
1142 return (value >> ((offset & 3) * 8)) & 0xff;
1144 return (value >> ((offset & 2) * 8)) & 0xffff;
1150 ulong pci_conv_size_to_32(ulong old, ulong value, uint offset,
1151 enum pci_size_t size)
1154 uint val_mask, shift;
1169 shift = (offset & off_mask) * 8;
1170 ldata = (value & val_mask) << shift;
1171 mask = val_mask << shift;
1172 value = (old & ~mask) | ldata;
1177 int pci_get_regions(struct udevice *dev, struct pci_region **iop,
1178 struct pci_region **memp, struct pci_region **prefp)
1180 struct udevice *bus = pci_get_controller(dev);
1181 struct pci_controller *hose = dev_get_uclass_priv(bus);
1187 for (i = 0; i < hose->region_count; i++) {
1188 switch (hose->regions[i].flags) {
1190 if (!*iop || (*iop)->size < hose->regions[i].size)
1191 *iop = hose->regions + i;
1193 case PCI_REGION_MEM:
1194 if (!*memp || (*memp)->size < hose->regions[i].size)
1195 *memp = hose->regions + i;
1197 case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
1198 if (!*prefp || (*prefp)->size < hose->regions[i].size)
1199 *prefp = hose->regions + i;
1204 return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL);
1207 u32 dm_pci_read_bar32(const struct udevice *dev, int barnum)
1212 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1213 dm_pci_read_config32(dev, bar, &addr);
1214 if (addr & PCI_BASE_ADDRESS_SPACE_IO)
1215 return addr & PCI_BASE_ADDRESS_IO_MASK;
1217 return addr & PCI_BASE_ADDRESS_MEM_MASK;
1220 void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr)
1224 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1225 dm_pci_write_config32(dev, bar, addr);
1228 static int _dm_pci_bus_to_phys(struct udevice *ctlr,
1229 pci_addr_t bus_addr, unsigned long flags,
1230 unsigned long skip_mask, phys_addr_t *pa)
1232 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
1233 struct pci_region *res;
1236 if (hose->region_count == 0) {
1241 for (i = 0; i < hose->region_count; i++) {
1242 res = &hose->regions[i];
1244 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1247 if (res->flags & skip_mask)
1250 if (bus_addr >= res->bus_start &&
1251 (bus_addr - res->bus_start) < res->size) {
1252 *pa = (bus_addr - res->bus_start + res->phys_start);
1260 phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr,
1261 unsigned long flags)
1263 phys_addr_t phys_addr = 0;
1264 struct udevice *ctlr;
1267 /* The root controller has the region information */
1268 ctlr = pci_get_controller(dev);
1271 * if PCI_REGION_MEM is set we do a two pass search with preference
1272 * on matches that don't have PCI_REGION_SYS_MEMORY set
1274 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1275 ret = _dm_pci_bus_to_phys(ctlr, bus_addr,
1276 flags, PCI_REGION_SYS_MEMORY,
1282 ret = _dm_pci_bus_to_phys(ctlr, bus_addr, flags, 0, &phys_addr);
1285 puts("pci_hose_bus_to_phys: invalid physical address\n");
1290 int _dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1291 unsigned long flags, unsigned long skip_mask,
1294 struct pci_region *res;
1295 struct udevice *ctlr;
1296 pci_addr_t bus_addr;
1298 struct pci_controller *hose;
1300 /* The root controller has the region information */
1301 ctlr = pci_get_controller(dev);
1302 hose = dev_get_uclass_priv(ctlr);
1304 if (hose->region_count == 0) {
1309 for (i = 0; i < hose->region_count; i++) {
1310 res = &hose->regions[i];
1312 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1315 if (res->flags & skip_mask)
1318 bus_addr = phys_addr - res->phys_start + res->bus_start;
1320 if (bus_addr >= res->bus_start &&
1321 (bus_addr - res->bus_start) < res->size) {
1330 pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1331 unsigned long flags)
1333 pci_addr_t bus_addr = 0;
1337 * if PCI_REGION_MEM is set we do a two pass search with preference
1338 * on matches that don't have PCI_REGION_SYS_MEMORY set
1340 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1341 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags,
1342 PCI_REGION_SYS_MEMORY, &bus_addr);
1347 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags, 0, &bus_addr);
1350 puts("pci_hose_phys_to_bus: invalid physical address\n");
1355 static void *dm_pci_map_ea_bar(struct udevice *dev, int bar, int flags,
1358 int ea_cnt, i, entry_size;
1359 int bar_id = (bar - PCI_BASE_ADDRESS_0) >> 2;
1363 /* EA capability structure header */
1364 dm_pci_read_config32(dev, ea_off, &ea_entry);
1365 ea_cnt = (ea_entry >> 16) & PCI_EA_NUM_ENT_MASK;
1366 ea_off += PCI_EA_FIRST_ENT;
1368 for (i = 0; i < ea_cnt; i++, ea_off += entry_size) {
1370 dm_pci_read_config32(dev, ea_off, &ea_entry);
1371 entry_size = ((ea_entry & PCI_EA_ES) + 1) << 2;
1373 if (((ea_entry & PCI_EA_BEI) >> 4) != bar_id)
1376 /* Base address, 1st DW */
1377 dm_pci_read_config32(dev, ea_off + 4, &ea_entry);
1378 addr = ea_entry & PCI_EA_FIELD_MASK;
1379 if (ea_entry & PCI_EA_IS_64) {
1380 /* Base address, 2nd DW, skip over 4B MaxOffset */
1381 dm_pci_read_config32(dev, ea_off + 12, &ea_entry);
1382 addr |= ((u64)ea_entry) << 32;
1385 /* size ignored for now */
1386 return map_physmem(addr, flags, 0);
1392 void *dm_pci_map_bar(struct udevice *dev, int bar, int flags)
1394 pci_addr_t pci_bus_addr;
1399 * if the function supports Enhanced Allocation use that instead of
1402 ea_off = dm_pci_find_capability(dev, PCI_CAP_ID_EA);
1404 return dm_pci_map_ea_bar(dev, bar, flags, ea_off);
1406 /* read BAR address */
1407 dm_pci_read_config32(dev, bar, &bar_response);
1408 pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
1411 * Pass "0" as the length argument to pci_bus_to_virt. The arg
1412 * isn't actualy used on any platform because u-boot assumes a static
1413 * linear mapping. In the future, this could read the BAR size
1414 * and pass that as the size if needed.
1416 return dm_pci_bus_to_virt(dev, pci_bus_addr, flags, 0, MAP_NOCACHE);
1419 static int _dm_pci_find_next_capability(struct udevice *dev, u8 pos, int cap)
1421 int ttl = PCI_FIND_CAP_TTL;
1425 dm_pci_read_config8(dev, pos, &pos);
1428 if (pos < PCI_STD_HEADER_SIZEOF)
1431 dm_pci_read_config16(dev, pos, &ent);
1444 int dm_pci_find_next_capability(struct udevice *dev, u8 start, int cap)
1446 return _dm_pci_find_next_capability(dev, start + PCI_CAP_LIST_NEXT,
1450 int dm_pci_find_capability(struct udevice *dev, int cap)
1456 dm_pci_read_config16(dev, PCI_STATUS, &status);
1457 if (!(status & PCI_STATUS_CAP_LIST))
1460 dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
1461 if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
1462 pos = PCI_CB_CAPABILITY_LIST;
1464 pos = PCI_CAPABILITY_LIST;
1466 return _dm_pci_find_next_capability(dev, pos, cap);
1469 int dm_pci_find_next_ext_capability(struct udevice *dev, int start, int cap)
1473 int pos = PCI_CFG_SPACE_SIZE;
1475 /* minimum 8 bytes per capability */
1476 ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
1481 dm_pci_read_config32(dev, pos, &header);
1483 * If we have no capabilities, this is indicated by cap ID,
1484 * cap version and next pointer all being 0.
1490 if (PCI_EXT_CAP_ID(header) == cap)
1493 pos = PCI_EXT_CAP_NEXT(header);
1494 if (pos < PCI_CFG_SPACE_SIZE)
1497 dm_pci_read_config32(dev, pos, &header);
1503 int dm_pci_find_ext_capability(struct udevice *dev, int cap)
1505 return dm_pci_find_next_ext_capability(dev, 0, cap);
1508 int dm_pci_flr(struct udevice *dev)
1513 /* look for PCI Express Capability */
1514 pcie_off = dm_pci_find_capability(dev, PCI_CAP_ID_EXP);
1518 /* check FLR capability */
1519 dm_pci_read_config32(dev, pcie_off + PCI_EXP_DEVCAP, &cap);
1520 if (!(cap & PCI_EXP_DEVCAP_FLR))
1523 dm_pci_clrset_config16(dev, pcie_off + PCI_EXP_DEVCTL, 0,
1524 PCI_EXP_DEVCTL_BCR_FLR);
1526 /* wait 100ms, per PCI spec */
1532 UCLASS_DRIVER(pci) = {
1535 .flags = DM_UC_FLAG_SEQ_ALIAS,
1536 .post_bind = dm_scan_fdt_dev,
1537 .pre_probe = pci_uclass_pre_probe,
1538 .post_probe = pci_uclass_post_probe,
1539 .child_post_bind = pci_uclass_child_post_bind,
1540 .per_device_auto_alloc_size = sizeof(struct pci_controller),
1541 .per_child_platdata_auto_alloc_size =
1542 sizeof(struct pci_child_platdata),
1545 static const struct dm_pci_ops pci_bridge_ops = {
1546 .read_config = pci_bridge_read_config,
1547 .write_config = pci_bridge_write_config,
1550 static const struct udevice_id pci_bridge_ids[] = {
1551 { .compatible = "pci-bridge" },
1555 U_BOOT_DRIVER(pci_bridge_drv) = {
1556 .name = "pci_bridge_drv",
1558 .of_match = pci_bridge_ids,
1559 .ops = &pci_bridge_ops,
1562 UCLASS_DRIVER(pci_generic) = {
1563 .id = UCLASS_PCI_GENERIC,
1564 .name = "pci_generic",
1567 static const struct udevice_id pci_generic_ids[] = {
1568 { .compatible = "pci-generic" },
1572 U_BOOT_DRIVER(pci_generic_drv) = {
1573 .name = "pci_generic_drv",
1574 .id = UCLASS_PCI_GENERIC,
1575 .of_match = pci_generic_ids,
1580 struct udevice *bus;
1583 * Enumerate all known controller devices. Enumeration has the side-
1584 * effect of probing them, so PCIe devices will be enumerated too.
1586 for (uclass_first_device_check(UCLASS_PCI, &bus);
1588 uclass_next_device_check(&bus)) {