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 if (dev_read_bool(dev, "pci,no-autoconfig"))
541 ret = dm_pciauto_config_device(dev);
545 sub_bus = max(sub_bus, max_bus);
547 pplat = dev_get_parent_platdata(dev);
548 if (pplat->class == (PCI_CLASS_DISPLAY_VGA << 8))
549 set_vga_bridge_bits(dev);
551 debug("%s: done\n", __func__);
556 int pci_generic_mmap_write_config(
557 const struct udevice *bus,
558 int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
563 enum pci_size_t size)
567 if (addr_f(bus, bdf, offset, &address) < 0)
572 writeb(value, address);
575 writew(value, address);
578 writel(value, address);
585 int pci_generic_mmap_read_config(
586 const struct udevice *bus,
587 int (*addr_f)(const struct udevice *bus, pci_dev_t bdf, uint offset,
592 enum pci_size_t size)
596 if (addr_f(bus, bdf, offset, &address) < 0) {
597 *valuep = pci_get_ff(size);
603 *valuep = readb(address);
606 *valuep = readw(address);
609 *valuep = readl(address);
616 int dm_pci_hose_probe_bus(struct udevice *bus)
621 debug("%s\n", __func__);
623 sub_bus = pci_get_bus_max() + 1;
624 debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
625 dm_pciauto_prescan_setup_bridge(bus, sub_bus);
627 ret = device_probe(bus);
629 debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name,
633 if (sub_bus != bus->seq) {
634 printf("%s: Internal error, bus '%s' got seq %d, expected %d\n",
635 __func__, bus->name, bus->seq, sub_bus);
638 sub_bus = pci_get_bus_max();
639 dm_pciauto_postscan_setup_bridge(bus, sub_bus);
645 * pci_match_one_device - Tell if a PCI device structure has a matching
646 * PCI device id structure
647 * @id: single PCI device id structure to match
648 * @find: the PCI device id structure to match against
650 * Returns true if the finding pci_device_id structure matched or false if
653 static bool pci_match_one_id(const struct pci_device_id *id,
654 const struct pci_device_id *find)
656 if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
657 (id->device == PCI_ANY_ID || id->device == find->device) &&
658 (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
659 (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
660 !((id->class ^ find->class) & id->class_mask))
667 * pci_find_and_bind_driver() - Find and bind the right PCI driver
669 * This only looks at certain fields in the descriptor.
671 * @parent: Parent bus
672 * @find_id: Specification of the driver to find
673 * @bdf: Bus/device/function addreess - see PCI_BDF()
674 * @devp: Returns a pointer to the device created
675 * @return 0 if OK, -EPERM if the device is not needed before relocation and
676 * therefore was not created, other -ve value on error
678 static int pci_find_and_bind_driver(struct udevice *parent,
679 struct pci_device_id *find_id,
680 pci_dev_t bdf, struct udevice **devp)
682 struct pci_driver_entry *start, *entry;
683 ofnode node = ofnode_null();
692 debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
693 find_id->vendor, find_id->device);
695 /* Determine optional OF node */
696 pci_dev_find_ofnode(parent, bdf, &node);
698 if (ofnode_valid(node) && !ofnode_is_available(node)) {
699 debug("%s: Ignoring disabled device\n", __func__);
703 start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
704 n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
705 for (entry = start; entry != start + n_ents; entry++) {
706 const struct pci_device_id *id;
708 const struct driver *drv;
710 for (id = entry->match;
711 id->vendor || id->subvendor || id->class_mask;
713 if (!pci_match_one_id(id, find_id))
719 * In the pre-relocation phase, we only bind devices
720 * whose driver has the DM_FLAG_PRE_RELOC set, to save
721 * precious memory space as on some platforms as that
722 * space is pretty limited (ie: using Cache As RAM).
724 if (!(gd->flags & GD_FLG_RELOC) &&
725 !(drv->flags & DM_FLAG_PRE_RELOC))
729 * We could pass the descriptor to the driver as
730 * platdata (instead of NULL) and allow its bind()
731 * method to return -ENOENT if it doesn't support this
732 * device. That way we could continue the search to
733 * find another driver. For now this doesn't seem
734 * necesssary, so just bind the first match.
736 ret = device_bind_ofnode(parent, drv, drv->name, NULL,
740 debug("%s: Match found: %s\n", __func__, drv->name);
741 dev->driver_data = id->driver_data;
747 bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
749 * In the pre-relocation phase, we only bind bridge devices to save
750 * precious memory space as on some platforms as that space is pretty
751 * limited (ie: using Cache As RAM).
753 if (!(gd->flags & GD_FLG_RELOC) && !bridge)
756 /* Bind a generic driver so that the device can be used */
757 sprintf(name, "pci_%x:%x.%x", parent->seq, PCI_DEV(bdf),
762 drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
764 ret = device_bind_driver_to_node(parent, drv, str, node, devp);
766 debug("%s: Failed to bind generic driver: %d\n", __func__, ret);
770 debug("%s: No match found: bound generic driver instead\n", __func__);
775 debug("%s: No match found: error %d\n", __func__, ret);
779 int pci_bind_bus_devices(struct udevice *bus)
781 ulong vendor, device;
788 end = PCI_BDF(bus->seq, PCI_MAX_PCI_DEVICES - 1,
789 PCI_MAX_PCI_FUNCTIONS - 1);
790 for (bdf = PCI_BDF(bus->seq, 0, 0); bdf <= end;
791 bdf += PCI_BDF(0, 0, 1)) {
792 struct pci_child_platdata *pplat;
798 if (PCI_FUNC(bdf) && !found_multi)
801 /* Check only the first access, we don't expect problems */
802 ret = pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
807 if (vendor == 0xffff || vendor == 0x0000)
810 pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
811 &header_type, PCI_SIZE_8);
814 found_multi = header_type & 0x80;
816 debug("%s: bus %d/%s: found device %x, function %d", __func__,
817 bus->seq, bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
818 pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
820 pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
824 /* Find this device in the device tree */
825 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
826 debug(": find ret=%d\n", ret);
828 /* If nothing in the device tree, bind a device */
829 if (ret == -ENODEV) {
830 struct pci_device_id find_id;
833 memset(&find_id, '\0', sizeof(find_id));
834 find_id.vendor = vendor;
835 find_id.device = device;
836 find_id.class = class;
837 if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
838 pci_bus_read_config(bus, bdf,
839 PCI_SUBSYSTEM_VENDOR_ID,
841 find_id.subvendor = val & 0xffff;
842 find_id.subdevice = val >> 16;
844 ret = pci_find_and_bind_driver(bus, &find_id, bdf,
852 /* Update the platform data */
853 pplat = dev_get_parent_platdata(dev);
854 pplat->devfn = PCI_MASK_BUS(bdf);
855 pplat->vendor = vendor;
856 pplat->device = device;
857 pplat->class = class;
862 printf("Cannot read bus configuration: %d\n", ret);
867 static void decode_regions(struct pci_controller *hose, ofnode parent_node,
870 int pci_addr_cells, addr_cells, size_cells;
871 int cells_per_record;
876 prop = ofnode_get_property(node, "ranges", &len);
878 debug("%s: Cannot decode regions\n", __func__);
882 pci_addr_cells = ofnode_read_simple_addr_cells(node);
883 addr_cells = ofnode_read_simple_addr_cells(parent_node);
884 size_cells = ofnode_read_simple_size_cells(node);
886 /* PCI addresses are always 3-cells */
888 cells_per_record = pci_addr_cells + addr_cells + size_cells;
889 hose->region_count = 0;
890 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
892 for (i = 0; i < MAX_PCI_REGIONS; i++, len -= cells_per_record) {
893 u64 pci_addr, addr, size;
899 if (len < cells_per_record)
901 flags = fdt32_to_cpu(prop[0]);
902 space_code = (flags >> 24) & 3;
903 pci_addr = fdtdec_get_number(prop + 1, 2);
904 prop += pci_addr_cells;
905 addr = fdtdec_get_number(prop, addr_cells);
907 size = fdtdec_get_number(prop, size_cells);
909 debug("%s: region %d, pci_addr=%llx, addr=%llx, size=%llx, space_code=%d\n",
910 __func__, hose->region_count, pci_addr, addr, size, space_code);
911 if (space_code & 2) {
912 type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
914 } else if (space_code & 1) {
915 type = PCI_REGION_IO;
920 if (!IS_ENABLED(CONFIG_SYS_PCI_64BIT) &&
921 type == PCI_REGION_MEM && upper_32_bits(pci_addr)) {
922 debug(" - beyond the 32-bit boundary, ignoring\n");
927 for (i = 0; i < hose->region_count; i++) {
928 if (hose->regions[i].flags == type)
932 pos = hose->region_count++;
933 debug(" - type=%d, pos=%d\n", type, pos);
934 pci_set_region(hose->regions + pos, pci_addr, addr, size, type);
937 /* Add a region for our local memory */
938 #ifdef CONFIG_NR_DRAM_BANKS
944 for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
945 if (hose->region_count == MAX_PCI_REGIONS) {
946 pr_err("maximum number of regions parsed, aborting\n");
950 if (bd->bi_dram[i].size) {
951 pci_set_region(hose->regions + hose->region_count++,
952 bd->bi_dram[i].start,
953 bd->bi_dram[i].start,
955 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
959 phys_addr_t base = 0, size;
962 #ifdef CONFIG_SYS_SDRAM_BASE
963 base = CONFIG_SYS_SDRAM_BASE;
965 if (gd->pci_ram_top && gd->pci_ram_top < base + size)
966 size = gd->pci_ram_top - base;
968 pci_set_region(hose->regions + hose->region_count++, base,
969 base, size, PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
975 static int pci_uclass_pre_probe(struct udevice *bus)
977 struct pci_controller *hose;
979 debug("%s, bus=%d/%s, parent=%s\n", __func__, bus->seq, bus->name,
981 hose = bus->uclass_priv;
983 /* For bridges, use the top-level PCI controller */
984 if (!device_is_on_pci_bus(bus)) {
986 decode_regions(hose, dev_ofnode(bus->parent), dev_ofnode(bus));
988 struct pci_controller *parent_hose;
990 parent_hose = dev_get_uclass_priv(bus->parent);
991 hose->ctlr = parent_hose->bus;
994 hose->first_busno = bus->seq;
995 hose->last_busno = bus->seq;
996 hose->skip_auto_config_until_reloc =
997 dev_read_bool(bus, "u-boot,skip-auto-config-until-reloc");
1002 static int pci_uclass_post_probe(struct udevice *bus)
1004 struct pci_controller *hose = dev_get_uclass_priv(bus);
1007 debug("%s: probing bus %d\n", __func__, bus->seq);
1008 ret = pci_bind_bus_devices(bus);
1012 if (CONFIG_IS_ENABLED(PCI_PNP) &&
1013 (!hose->skip_auto_config_until_reloc ||
1014 (gd->flags & GD_FLG_RELOC))) {
1015 ret = pci_auto_config_devices(bus);
1017 return log_msg_ret("pci auto-config", ret);
1020 #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
1022 * Per Intel FSP specification, we should call FSP notify API to
1023 * inform FSP that PCI enumeration has been done so that FSP will
1024 * do any necessary initialization as required by the chipset's
1025 * BIOS Writer's Guide (BWG).
1027 * Unfortunately we have to put this call here as with driver model,
1028 * the enumeration is all done on a lazy basis as needed, so until
1029 * something is touched on PCI it won't happen.
1031 * Note we only call this 1) after U-Boot is relocated, and 2)
1032 * root bus has finished probing.
1034 if ((gd->flags & GD_FLG_RELOC) && (bus->seq == 0)) {
1035 ret = fsp_init_phase_pci();
1044 static int pci_uclass_child_post_bind(struct udevice *dev)
1046 struct pci_child_platdata *pplat;
1048 if (!dev_of_valid(dev))
1051 pplat = dev_get_parent_platdata(dev);
1053 /* Extract vendor id and device id if available */
1054 ofnode_read_pci_vendev(dev_ofnode(dev), &pplat->vendor, &pplat->device);
1056 /* Extract the devfn from fdt_pci_addr */
1057 pplat->devfn = pci_get_devfn(dev);
1062 static int pci_bridge_read_config(const struct udevice *bus, pci_dev_t bdf,
1063 uint offset, ulong *valuep,
1064 enum pci_size_t size)
1066 struct pci_controller *hose = bus->uclass_priv;
1068 return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
1071 static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
1072 uint offset, ulong value,
1073 enum pci_size_t size)
1075 struct pci_controller *hose = bus->uclass_priv;
1077 return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
1080 static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
1082 struct udevice *dev;
1086 * Scan through all the PCI controllers. On x86 there will only be one
1087 * but that is not necessarily true on other hardware.
1090 device_find_first_child(bus, &dev);
1095 ret = uclass_next_device(&bus);
1103 int pci_find_next_device(struct udevice **devp)
1105 struct udevice *child = *devp;
1106 struct udevice *bus = child->parent;
1109 /* First try all the siblings */
1112 device_find_next_child(&child);
1119 /* We ran out of siblings. Try the next bus */
1120 ret = uclass_next_device(&bus);
1124 return bus ? skip_to_next_device(bus, devp) : 0;
1127 int pci_find_first_device(struct udevice **devp)
1129 struct udevice *bus;
1133 ret = uclass_first_device(UCLASS_PCI, &bus);
1137 return skip_to_next_device(bus, devp);
1140 ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size)
1144 return (value >> ((offset & 3) * 8)) & 0xff;
1146 return (value >> ((offset & 2) * 8)) & 0xffff;
1152 ulong pci_conv_size_to_32(ulong old, ulong value, uint offset,
1153 enum pci_size_t size)
1156 uint val_mask, shift;
1171 shift = (offset & off_mask) * 8;
1172 ldata = (value & val_mask) << shift;
1173 mask = val_mask << shift;
1174 value = (old & ~mask) | ldata;
1179 int pci_get_regions(struct udevice *dev, struct pci_region **iop,
1180 struct pci_region **memp, struct pci_region **prefp)
1182 struct udevice *bus = pci_get_controller(dev);
1183 struct pci_controller *hose = dev_get_uclass_priv(bus);
1189 for (i = 0; i < hose->region_count; i++) {
1190 switch (hose->regions[i].flags) {
1192 if (!*iop || (*iop)->size < hose->regions[i].size)
1193 *iop = hose->regions + i;
1195 case PCI_REGION_MEM:
1196 if (!*memp || (*memp)->size < hose->regions[i].size)
1197 *memp = hose->regions + i;
1199 case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
1200 if (!*prefp || (*prefp)->size < hose->regions[i].size)
1201 *prefp = hose->regions + i;
1206 return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL);
1209 u32 dm_pci_read_bar32(const struct udevice *dev, int barnum)
1214 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1215 dm_pci_read_config32(dev, bar, &addr);
1218 * If we get an invalid address, return this so that comparisons with
1219 * FDT_ADDR_T_NONE work correctly
1221 if (addr == 0xffffffff)
1223 else if (addr & PCI_BASE_ADDRESS_SPACE_IO)
1224 return addr & PCI_BASE_ADDRESS_IO_MASK;
1226 return addr & PCI_BASE_ADDRESS_MEM_MASK;
1229 void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr)
1233 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1234 dm_pci_write_config32(dev, bar, addr);
1237 static int _dm_pci_bus_to_phys(struct udevice *ctlr,
1238 pci_addr_t bus_addr, unsigned long flags,
1239 unsigned long skip_mask, phys_addr_t *pa)
1241 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
1242 struct pci_region *res;
1245 if (hose->region_count == 0) {
1250 for (i = 0; i < hose->region_count; i++) {
1251 res = &hose->regions[i];
1253 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1256 if (res->flags & skip_mask)
1259 if (bus_addr >= res->bus_start &&
1260 (bus_addr - res->bus_start) < res->size) {
1261 *pa = (bus_addr - res->bus_start + res->phys_start);
1269 phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr,
1270 unsigned long flags)
1272 phys_addr_t phys_addr = 0;
1273 struct udevice *ctlr;
1276 /* The root controller has the region information */
1277 ctlr = pci_get_controller(dev);
1280 * if PCI_REGION_MEM is set we do a two pass search with preference
1281 * on matches that don't have PCI_REGION_SYS_MEMORY set
1283 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1284 ret = _dm_pci_bus_to_phys(ctlr, bus_addr,
1285 flags, PCI_REGION_SYS_MEMORY,
1291 ret = _dm_pci_bus_to_phys(ctlr, bus_addr, flags, 0, &phys_addr);
1294 puts("pci_hose_bus_to_phys: invalid physical address\n");
1299 int _dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1300 unsigned long flags, unsigned long skip_mask,
1303 struct pci_region *res;
1304 struct udevice *ctlr;
1305 pci_addr_t bus_addr;
1307 struct pci_controller *hose;
1309 /* The root controller has the region information */
1310 ctlr = pci_get_controller(dev);
1311 hose = dev_get_uclass_priv(ctlr);
1313 if (hose->region_count == 0) {
1318 for (i = 0; i < hose->region_count; i++) {
1319 res = &hose->regions[i];
1321 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1324 if (res->flags & skip_mask)
1327 bus_addr = phys_addr - res->phys_start + res->bus_start;
1329 if (bus_addr >= res->bus_start &&
1330 (bus_addr - res->bus_start) < res->size) {
1339 pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1340 unsigned long flags)
1342 pci_addr_t bus_addr = 0;
1346 * if PCI_REGION_MEM is set we do a two pass search with preference
1347 * on matches that don't have PCI_REGION_SYS_MEMORY set
1349 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1350 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags,
1351 PCI_REGION_SYS_MEMORY, &bus_addr);
1356 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags, 0, &bus_addr);
1359 puts("pci_hose_phys_to_bus: invalid physical address\n");
1364 static void *dm_pci_map_ea_bar(struct udevice *dev, int bar, int flags,
1367 int ea_cnt, i, entry_size;
1368 int bar_id = (bar - PCI_BASE_ADDRESS_0) >> 2;
1372 /* EA capability structure header */
1373 dm_pci_read_config32(dev, ea_off, &ea_entry);
1374 ea_cnt = (ea_entry >> 16) & PCI_EA_NUM_ENT_MASK;
1375 ea_off += PCI_EA_FIRST_ENT;
1377 for (i = 0; i < ea_cnt; i++, ea_off += entry_size) {
1379 dm_pci_read_config32(dev, ea_off, &ea_entry);
1380 entry_size = ((ea_entry & PCI_EA_ES) + 1) << 2;
1382 if (((ea_entry & PCI_EA_BEI) >> 4) != bar_id)
1385 /* Base address, 1st DW */
1386 dm_pci_read_config32(dev, ea_off + 4, &ea_entry);
1387 addr = ea_entry & PCI_EA_FIELD_MASK;
1388 if (ea_entry & PCI_EA_IS_64) {
1389 /* Base address, 2nd DW, skip over 4B MaxOffset */
1390 dm_pci_read_config32(dev, ea_off + 12, &ea_entry);
1391 addr |= ((u64)ea_entry) << 32;
1394 /* size ignored for now */
1395 return map_physmem(addr, flags, 0);
1401 void *dm_pci_map_bar(struct udevice *dev, int bar, int flags)
1403 pci_addr_t pci_bus_addr;
1408 * if the function supports Enhanced Allocation use that instead of
1411 ea_off = dm_pci_find_capability(dev, PCI_CAP_ID_EA);
1413 return dm_pci_map_ea_bar(dev, bar, flags, ea_off);
1415 /* read BAR address */
1416 dm_pci_read_config32(dev, bar, &bar_response);
1417 pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
1420 * Pass "0" as the length argument to pci_bus_to_virt. The arg
1421 * isn't actualy used on any platform because u-boot assumes a static
1422 * linear mapping. In the future, this could read the BAR size
1423 * and pass that as the size if needed.
1425 return dm_pci_bus_to_virt(dev, pci_bus_addr, flags, 0, MAP_NOCACHE);
1428 static int _dm_pci_find_next_capability(struct udevice *dev, u8 pos, int cap)
1430 int ttl = PCI_FIND_CAP_TTL;
1434 dm_pci_read_config8(dev, pos, &pos);
1437 if (pos < PCI_STD_HEADER_SIZEOF)
1440 dm_pci_read_config16(dev, pos, &ent);
1453 int dm_pci_find_next_capability(struct udevice *dev, u8 start, int cap)
1455 return _dm_pci_find_next_capability(dev, start + PCI_CAP_LIST_NEXT,
1459 int dm_pci_find_capability(struct udevice *dev, int cap)
1465 dm_pci_read_config16(dev, PCI_STATUS, &status);
1466 if (!(status & PCI_STATUS_CAP_LIST))
1469 dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
1470 if ((header_type & 0x7f) == PCI_HEADER_TYPE_CARDBUS)
1471 pos = PCI_CB_CAPABILITY_LIST;
1473 pos = PCI_CAPABILITY_LIST;
1475 return _dm_pci_find_next_capability(dev, pos, cap);
1478 int dm_pci_find_next_ext_capability(struct udevice *dev, int start, int cap)
1482 int pos = PCI_CFG_SPACE_SIZE;
1484 /* minimum 8 bytes per capability */
1485 ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
1490 dm_pci_read_config32(dev, pos, &header);
1492 * If we have no capabilities, this is indicated by cap ID,
1493 * cap version and next pointer all being 0.
1499 if (PCI_EXT_CAP_ID(header) == cap)
1502 pos = PCI_EXT_CAP_NEXT(header);
1503 if (pos < PCI_CFG_SPACE_SIZE)
1506 dm_pci_read_config32(dev, pos, &header);
1512 int dm_pci_find_ext_capability(struct udevice *dev, int cap)
1514 return dm_pci_find_next_ext_capability(dev, 0, cap);
1517 int dm_pci_flr(struct udevice *dev)
1522 /* look for PCI Express Capability */
1523 pcie_off = dm_pci_find_capability(dev, PCI_CAP_ID_EXP);
1527 /* check FLR capability */
1528 dm_pci_read_config32(dev, pcie_off + PCI_EXP_DEVCAP, &cap);
1529 if (!(cap & PCI_EXP_DEVCAP_FLR))
1532 dm_pci_clrset_config16(dev, pcie_off + PCI_EXP_DEVCTL, 0,
1533 PCI_EXP_DEVCTL_BCR_FLR);
1535 /* wait 100ms, per PCI spec */
1541 UCLASS_DRIVER(pci) = {
1544 .flags = DM_UC_FLAG_SEQ_ALIAS,
1545 .post_bind = dm_scan_fdt_dev,
1546 .pre_probe = pci_uclass_pre_probe,
1547 .post_probe = pci_uclass_post_probe,
1548 .child_post_bind = pci_uclass_child_post_bind,
1549 .per_device_auto_alloc_size = sizeof(struct pci_controller),
1550 .per_child_platdata_auto_alloc_size =
1551 sizeof(struct pci_child_platdata),
1554 static const struct dm_pci_ops pci_bridge_ops = {
1555 .read_config = pci_bridge_read_config,
1556 .write_config = pci_bridge_write_config,
1559 static const struct udevice_id pci_bridge_ids[] = {
1560 { .compatible = "pci-bridge" },
1564 U_BOOT_DRIVER(pci_bridge_drv) = {
1565 .name = "pci_bridge_drv",
1567 .of_match = pci_bridge_ids,
1568 .ops = &pci_bridge_ops,
1571 UCLASS_DRIVER(pci_generic) = {
1572 .id = UCLASS_PCI_GENERIC,
1573 .name = "pci_generic",
1576 static const struct udevice_id pci_generic_ids[] = {
1577 { .compatible = "pci-generic" },
1581 U_BOOT_DRIVER(pci_generic_drv) = {
1582 .name = "pci_generic_drv",
1583 .id = UCLASS_PCI_GENERIC,
1584 .of_match = pci_generic_ids,
1589 struct udevice *bus;
1592 * Enumerate all known controller devices. Enumeration has the side-
1593 * effect of probing them, so PCIe devices will be enumerated too.
1595 for (uclass_first_device_check(UCLASS_PCI, &bus);
1597 uclass_next_device_check(&bus)) {