2 * Copyright (c) 2014 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * SPDX-License-Identifier: GPL-2.0+
16 #include <dm/device-internal.h>
17 #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
18 #include <asm/fsp/fsp_support.h>
21 DECLARE_GLOBAL_DATA_PTR;
23 struct pci_controller *pci_bus_to_hose(int busnum)
28 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, &bus);
30 debug("%s: Cannot get bus %d: ret=%d\n", __func__, busnum, ret);
33 return dev_get_uclass_priv(bus);
36 pci_dev_t pci_get_bdf(struct udevice *dev)
38 struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
39 struct udevice *bus = dev->parent;
41 return PCI_ADD_BUS(bus->seq, pplat->devfn);
45 * pci_get_bus_max() - returns the bus number of the last active bus
47 * @return last bus number, or -1 if no active buses
49 static int pci_get_bus_max(void)
55 ret = uclass_get(UCLASS_PCI, &uc);
56 uclass_foreach_dev(bus, uc) {
61 debug("%s: ret=%d\n", __func__, ret);
66 int pci_last_busno(void)
68 struct pci_controller *hose;
73 debug("pci_last_busno\n");
74 ret = uclass_get(UCLASS_PCI, &uc);
75 if (ret || list_empty(&uc->dev_head))
78 /* Probe the last bus */
79 bus = list_entry(uc->dev_head.prev, struct udevice, uclass_node);
80 debug("bus = %p, %s\n", bus, bus->name);
82 ret = device_probe(bus);
86 /* If that bus has bridges, we may have new buses now. Get the last */
87 bus = list_entry(uc->dev_head.prev, struct udevice, uclass_node);
88 hose = dev_get_uclass_priv(bus);
89 debug("bus = %s, hose = %p\n", bus->name, hose);
91 return hose->last_busno;
94 int pci_get_ff(enum pci_size_t size)
106 int pci_bus_find_devfn(struct udevice *bus, pci_dev_t find_devfn,
107 struct udevice **devp)
111 for (device_find_first_child(bus, &dev);
113 device_find_next_child(&dev)) {
114 struct pci_child_platdata *pplat;
116 pplat = dev_get_parent_platdata(dev);
117 if (pplat && pplat->devfn == find_devfn) {
126 int pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp)
131 ret = uclass_get_device_by_seq(UCLASS_PCI, PCI_BUS(bdf), &bus);
134 return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp);
137 static int pci_device_matches_ids(struct udevice *dev,
138 struct pci_device_id *ids)
140 struct pci_child_platdata *pplat;
143 pplat = dev_get_parent_platdata(dev);
146 for (i = 0; ids[i].vendor != 0; i++) {
147 if (pplat->vendor == ids[i].vendor &&
148 pplat->device == ids[i].device)
155 int pci_bus_find_devices(struct udevice *bus, struct pci_device_id *ids,
156 int *indexp, struct udevice **devp)
160 /* Scan all devices on this bus */
161 for (device_find_first_child(bus, &dev);
163 device_find_next_child(&dev)) {
164 if (pci_device_matches_ids(dev, ids) >= 0) {
165 if ((*indexp)-- <= 0) {
175 int pci_find_device_id(struct pci_device_id *ids, int index,
176 struct udevice **devp)
180 /* Scan all known buses */
181 for (uclass_first_device(UCLASS_PCI, &bus);
183 uclass_next_device(&bus)) {
184 if (!pci_bus_find_devices(bus, ids, &index, devp))
192 int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset,
193 unsigned long value, enum pci_size_t size)
195 struct dm_pci_ops *ops;
197 ops = pci_get_ops(bus);
198 if (!ops->write_config)
200 return ops->write_config(bus, bdf, offset, value, size);
203 int pci_write_config(pci_dev_t bdf, int offset, unsigned long value,
204 enum pci_size_t size)
209 ret = uclass_get_device_by_seq(UCLASS_PCI, PCI_BUS(bdf), &bus);
213 return pci_bus_write_config(bus, bdf, offset, value, size);
216 int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value,
217 enum pci_size_t size)
221 for (bus = dev; device_get_uclass_id(bus->parent) == UCLASS_PCI;)
223 return pci_bus_write_config(bus, pci_get_bdf(dev), offset, value, size);
227 int pci_write_config32(pci_dev_t bdf, int offset, u32 value)
229 return pci_write_config(bdf, offset, value, PCI_SIZE_32);
232 int pci_write_config16(pci_dev_t bdf, int offset, u16 value)
234 return pci_write_config(bdf, offset, value, PCI_SIZE_16);
237 int pci_write_config8(pci_dev_t bdf, int offset, u8 value)
239 return pci_write_config(bdf, offset, value, PCI_SIZE_8);
242 int dm_pci_write_config8(struct udevice *dev, int offset, u8 value)
244 return dm_pci_write_config(dev, offset, value, PCI_SIZE_8);
247 int dm_pci_write_config16(struct udevice *dev, int offset, u16 value)
249 return dm_pci_write_config(dev, offset, value, PCI_SIZE_16);
252 int dm_pci_write_config32(struct udevice *dev, int offset, u32 value)
254 return dm_pci_write_config(dev, offset, value, PCI_SIZE_32);
257 int pci_bus_read_config(struct udevice *bus, pci_dev_t bdf, int offset,
258 unsigned long *valuep, enum pci_size_t size)
260 struct dm_pci_ops *ops;
262 ops = pci_get_ops(bus);
263 if (!ops->read_config)
265 return ops->read_config(bus, bdf, offset, valuep, size);
268 int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep,
269 enum pci_size_t size)
274 ret = uclass_get_device_by_seq(UCLASS_PCI, PCI_BUS(bdf), &bus);
278 return pci_bus_read_config(bus, bdf, offset, valuep, size);
281 int dm_pci_read_config(struct udevice *dev, int offset, unsigned long *valuep,
282 enum pci_size_t size)
286 for (bus = dev; device_get_uclass_id(bus->parent) == UCLASS_PCI;)
288 return pci_bus_read_config(bus, pci_get_bdf(dev), offset, valuep,
292 int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep)
297 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32);
305 int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep)
310 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16);
318 int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep)
323 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8);
331 int dm_pci_read_config8(struct udevice *dev, int offset, u8 *valuep)
336 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_8);
344 int dm_pci_read_config16(struct udevice *dev, int offset, u16 *valuep)
349 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_16);
357 int dm_pci_read_config32(struct udevice *dev, int offset, u32 *valuep)
362 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_32);
370 int pci_auto_config_devices(struct udevice *bus)
372 struct pci_controller *hose = bus->uclass_priv;
373 unsigned int sub_bus;
378 debug("%s: start\n", __func__);
379 pciauto_config_init(hose);
380 for (ret = device_find_first_child(bus, &dev);
382 ret = device_find_next_child(&dev)) {
383 unsigned int max_bus;
385 debug("%s: device %s\n", __func__, dev->name);
386 max_bus = pciauto_config_device(hose, pci_get_bdf(dev));
387 sub_bus = max(sub_bus, max_bus);
389 debug("%s: done\n", __func__);
394 int dm_pci_hose_probe_bus(struct pci_controller *hose, pci_dev_t bdf)
396 struct udevice *parent, *bus;
400 debug("%s\n", __func__);
403 /* Find the bus within the parent */
404 ret = pci_bus_find_devfn(parent, PCI_MASK_BUS(bdf), &bus);
406 debug("%s: Cannot find device %x on bus %s: %d\n", __func__,
407 bdf, parent->name, ret);
411 sub_bus = pci_get_bus_max() + 1;
412 debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
413 pciauto_prescan_setup_bridge(hose, bdf, sub_bus);
415 ret = device_probe(bus);
417 debug("%s: Cannot probe bus bus %s: %d\n", __func__, bus->name,
421 if (sub_bus != bus->seq) {
422 printf("%s: Internal error, bus '%s' got seq %d, expected %d\n",
423 __func__, bus->name, bus->seq, sub_bus);
426 sub_bus = pci_get_bus_max();
427 pciauto_postscan_setup_bridge(hose, bdf, sub_bus);
433 * pci_match_one_device - Tell if a PCI device structure has a matching
434 * PCI device id structure
435 * @id: single PCI device id structure to match
436 * @dev: the PCI device structure to match against
438 * Returns the matching pci_device_id structure or %NULL if there is no match.
440 static bool pci_match_one_id(const struct pci_device_id *id,
441 const struct pci_device_id *find)
443 if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
444 (id->device == PCI_ANY_ID || id->device == find->device) &&
445 (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
446 (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
447 !((id->class ^ find->class) & id->class_mask))
454 * pci_find_and_bind_driver() - Find and bind the right PCI driver
456 * This only looks at certain fields in the descriptor.
458 static int pci_find_and_bind_driver(struct udevice *parent,
459 struct pci_device_id *find_id, pci_dev_t bdf,
460 struct udevice **devp)
462 struct pci_driver_entry *start, *entry;
471 debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
472 find_id->vendor, find_id->device);
473 start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
474 n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
475 for (entry = start; entry != start + n_ents; entry++) {
476 const struct pci_device_id *id;
478 const struct driver *drv;
480 for (id = entry->match;
481 id->vendor || id->subvendor || id->class_mask;
483 if (!pci_match_one_id(id, find_id))
489 * In the pre-relocation phase, we only bind devices
490 * whose driver has the DM_FLAG_PRE_RELOC set, to save
491 * precious memory space as on some platforms as that
492 * space is pretty limited (ie: using Cache As RAM).
494 if (!(gd->flags & GD_FLG_RELOC) &&
495 !(drv->flags & DM_FLAG_PRE_RELOC))
499 * We could pass the descriptor to the driver as
500 * platdata (instead of NULL) and allow its bind()
501 * method to return -ENOENT if it doesn't support this
502 * device. That way we could continue the search to
503 * find another driver. For now this doesn't seem
504 * necesssary, so just bind the first match.
506 ret = device_bind(parent, drv, drv->name, NULL, -1,
510 debug("%s: Match found: %s\n", __func__, drv->name);
511 dev->driver_data = find_id->driver_data;
517 bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
519 * In the pre-relocation phase, we only bind bridge devices to save
520 * precious memory space as on some platforms as that space is pretty
521 * limited (ie: using Cache As RAM).
523 if (!(gd->flags & GD_FLG_RELOC) && !bridge)
526 /* Bind a generic driver so that the device can be used */
527 sprintf(name, "pci_%x:%x.%x", parent->seq, PCI_DEV(bdf),
532 drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
534 ret = device_bind_driver(parent, drv, str, devp);
536 debug("%s: Failed to bind generic driver: %d", __func__, ret);
539 debug("%s: No match found: bound generic driver instead\n", __func__);
544 debug("%s: No match found: error %d\n", __func__, ret);
548 int pci_bind_bus_devices(struct udevice *bus)
550 ulong vendor, device;
557 end = PCI_BDF(bus->seq, PCI_MAX_PCI_DEVICES - 1,
558 PCI_MAX_PCI_FUNCTIONS - 1);
559 for (bdf = PCI_BDF(bus->seq, 0, 0); bdf < end;
560 bdf += PCI_BDF(0, 0, 1)) {
561 struct pci_child_platdata *pplat;
565 if (PCI_FUNC(bdf) && !found_multi)
567 /* Check only the first access, we don't expect problems */
568 ret = pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
569 &header_type, PCI_SIZE_8);
572 pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
574 if (vendor == 0xffff || vendor == 0x0000)
578 found_multi = header_type & 0x80;
580 debug("%s: bus %d/%s: found device %x, function %d\n", __func__,
581 bus->seq, bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
582 pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
584 pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
588 /* Find this device in the device tree */
589 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
591 /* Search for a driver */
593 /* If nothing in the device tree, bind a generic device */
594 if (ret == -ENODEV) {
595 struct pci_device_id find_id;
598 memset(&find_id, '\0', sizeof(find_id));
599 find_id.vendor = vendor;
600 find_id.device = device;
601 find_id.class = class;
602 if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
603 pci_bus_read_config(bus, bdf,
604 PCI_SUBSYSTEM_VENDOR_ID,
606 find_id.subvendor = val & 0xffff;
607 find_id.subdevice = val >> 16;
609 ret = pci_find_and_bind_driver(bus, &find_id, bdf,
615 /* Update the platform data */
617 pplat = dev_get_parent_platdata(dev);
618 pplat->devfn = PCI_MASK_BUS(bdf);
619 pplat->vendor = vendor;
620 pplat->device = device;
621 pplat->class = class;
627 printf("Cannot read bus configuration: %d\n", ret);
632 static int pci_uclass_post_bind(struct udevice *bus)
635 * Scan the device tree for devices. This does not probe the PCI bus,
636 * as this is not permitted while binding. It just finds devices
637 * mentioned in the device tree.
639 * Before relocation, only bind devices marked for pre-relocation
642 return dm_scan_fdt_node(bus, gd->fdt_blob, bus->of_offset,
643 gd->flags & GD_FLG_RELOC ? false : true);
646 static int decode_regions(struct pci_controller *hose, const void *blob,
647 int parent_node, int node)
649 int pci_addr_cells, addr_cells, size_cells;
650 int cells_per_record;
656 prop = fdt_getprop(blob, node, "ranges", &len);
659 pci_addr_cells = fdt_address_cells(blob, node);
660 addr_cells = fdt_address_cells(blob, parent_node);
661 size_cells = fdt_size_cells(blob, node);
663 /* PCI addresses are always 3-cells */
665 cells_per_record = pci_addr_cells + addr_cells + size_cells;
666 hose->region_count = 0;
667 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
669 for (i = 0; i < MAX_PCI_REGIONS; i++, len -= cells_per_record) {
670 u64 pci_addr, addr, size;
675 if (len < cells_per_record)
677 flags = fdt32_to_cpu(prop[0]);
678 space_code = (flags >> 24) & 3;
679 pci_addr = fdtdec_get_number(prop + 1, 2);
680 prop += pci_addr_cells;
681 addr = fdtdec_get_number(prop, addr_cells);
683 size = fdtdec_get_number(prop, size_cells);
685 debug("%s: region %d, pci_addr=%" PRIx64 ", addr=%" PRIx64
686 ", size=%" PRIx64 ", space_code=%d\n", __func__,
687 hose->region_count, pci_addr, addr, size, space_code);
688 if (space_code & 2) {
689 type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
691 } else if (space_code & 1) {
692 type = PCI_REGION_IO;
696 debug(" - type=%d\n", type);
697 pci_set_region(hose->regions + hose->region_count++, pci_addr,
701 /* Add a region for our local memory */
703 if (gd->pci_ram_top && gd->pci_ram_top < addr)
704 addr = gd->pci_ram_top;
705 pci_set_region(hose->regions + hose->region_count++, 0, 0, addr,
706 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
711 static int pci_uclass_pre_probe(struct udevice *bus)
713 struct pci_controller *hose;
716 debug("%s, bus=%d/%s, parent=%s\n", __func__, bus->seq, bus->name,
718 hose = bus->uclass_priv;
720 /* For bridges, use the top-level PCI controller */
721 if (device_get_uclass_id(bus->parent) == UCLASS_ROOT) {
723 ret = decode_regions(hose, gd->fdt_blob, bus->parent->of_offset,
726 debug("%s: Cannot decode regions\n", __func__);
730 struct pci_controller *parent_hose;
732 parent_hose = dev_get_uclass_priv(bus->parent);
733 hose->ctlr = parent_hose->bus;
736 hose->first_busno = bus->seq;
737 hose->last_busno = bus->seq;
742 static int pci_uclass_post_probe(struct udevice *bus)
746 debug("%s: probing bus %d\n", __func__, bus->seq);
747 ret = pci_bind_bus_devices(bus);
751 #ifdef CONFIG_PCI_PNP
752 ret = pci_auto_config_devices(bus);
755 #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
757 * Per Intel FSP specification, we should call FSP notify API to
758 * inform FSP that PCI enumeration has been done so that FSP will
759 * do any necessary initialization as required by the chipset's
760 * BIOS Writer's Guide (BWG).
762 * Unfortunately we have to put this call here as with driver model,
763 * the enumeration is all done on a lazy basis as needed, so until
764 * something is touched on PCI it won't happen.
766 * Note we only call this 1) after U-Boot is relocated, and 2)
767 * root bus has finished probing.
769 if ((gd->flags & GD_FLG_RELOC) && (bus->seq == 0))
770 ret = fsp_init_phase_pci();
773 return ret < 0 ? ret : 0;
776 static int pci_uclass_child_post_bind(struct udevice *dev)
778 struct pci_child_platdata *pplat;
779 struct fdt_pci_addr addr;
782 if (dev->of_offset == -1)
786 * We could read vendor, device, class if available. But for now we
787 * just check the address.
789 pplat = dev_get_parent_platdata(dev);
790 ret = fdtdec_get_pci_addr(gd->fdt_blob, dev->of_offset,
791 FDT_PCI_SPACE_CONFIG, "reg", &addr);
797 /* extract the bdf from fdt_pci_addr */
798 pplat->devfn = addr.phys_hi & 0xffff00;
804 static int pci_bridge_read_config(struct udevice *bus, pci_dev_t bdf,
805 uint offset, ulong *valuep,
806 enum pci_size_t size)
808 struct pci_controller *hose = bus->uclass_priv;
810 return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
813 static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
814 uint offset, ulong value,
815 enum pci_size_t size)
817 struct pci_controller *hose = bus->uclass_priv;
819 return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
822 static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
828 * Scan through all the PCI controllers. On x86 there will only be one
829 * but that is not necessarily true on other hardware.
832 device_find_first_child(bus, &dev);
837 ret = uclass_next_device(&bus);
845 int pci_find_next_device(struct udevice **devp)
847 struct udevice *child = *devp;
848 struct udevice *bus = child->parent;
851 /* First try all the siblings */
854 device_find_next_child(&child);
861 /* We ran out of siblings. Try the next bus */
862 ret = uclass_next_device(&bus);
866 return bus ? skip_to_next_device(bus, devp) : 0;
869 int pci_find_first_device(struct udevice **devp)
875 ret = uclass_first_device(UCLASS_PCI, &bus);
879 return skip_to_next_device(bus, devp);
882 UCLASS_DRIVER(pci) = {
885 .flags = DM_UC_FLAG_SEQ_ALIAS,
886 .post_bind = pci_uclass_post_bind,
887 .pre_probe = pci_uclass_pre_probe,
888 .post_probe = pci_uclass_post_probe,
889 .child_post_bind = pci_uclass_child_post_bind,
890 .per_device_auto_alloc_size = sizeof(struct pci_controller),
891 .per_child_platdata_auto_alloc_size =
892 sizeof(struct pci_child_platdata),
895 static const struct dm_pci_ops pci_bridge_ops = {
896 .read_config = pci_bridge_read_config,
897 .write_config = pci_bridge_write_config,
900 static const struct udevice_id pci_bridge_ids[] = {
901 { .compatible = "pci-bridge" },
905 U_BOOT_DRIVER(pci_bridge_drv) = {
906 .name = "pci_bridge_drv",
908 .of_match = pci_bridge_ids,
909 .ops = &pci_bridge_ops,
912 UCLASS_DRIVER(pci_generic) = {
913 .id = UCLASS_PCI_GENERIC,
914 .name = "pci_generic",
917 static const struct udevice_id pci_generic_ids[] = {
918 { .compatible = "pci-generic" },
922 U_BOOT_DRIVER(pci_generic_drv) = {
923 .name = "pci_generic_drv",
924 .id = UCLASS_PCI_GENERIC,
925 .of_match = pci_generic_ids,