2 * Copyright (c) 2014 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * SPDX-License-Identifier: GPL-2.0+
14 #include <dm/device-internal.h>
16 #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
17 #include <asm/fsp/fsp_support.h>
19 #include "pci_internal.h"
21 DECLARE_GLOBAL_DATA_PTR;
23 int pci_get_bus(int busnum, struct udevice **busp)
27 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
29 /* Since buses may not be numbered yet try a little harder with bus 0 */
31 ret = uclass_first_device_err(UCLASS_PCI, busp);
34 ret = uclass_get_device_by_seq(UCLASS_PCI, busnum, busp);
40 struct udevice *pci_get_controller(struct udevice *dev)
42 while (device_is_on_pci_bus(dev))
48 pci_dev_t dm_pci_get_bdf(struct udevice *dev)
50 struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
51 struct udevice *bus = dev->parent;
53 return PCI_ADD_BUS(bus->seq, pplat->devfn);
57 * pci_get_bus_max() - returns the bus number of the last active bus
59 * @return last bus number, or -1 if no active buses
61 static int pci_get_bus_max(void)
67 ret = uclass_get(UCLASS_PCI, &uc);
68 uclass_foreach_dev(bus, uc) {
73 debug("%s: ret=%d\n", __func__, ret);
78 int pci_last_busno(void)
80 return pci_get_bus_max();
83 int pci_get_ff(enum pci_size_t size)
95 int pci_bus_find_devfn(struct udevice *bus, pci_dev_t find_devfn,
96 struct udevice **devp)
100 for (device_find_first_child(bus, &dev);
102 device_find_next_child(&dev)) {
103 struct pci_child_platdata *pplat;
105 pplat = dev_get_parent_platdata(dev);
106 if (pplat && pplat->devfn == find_devfn) {
115 int dm_pci_bus_find_bdf(pci_dev_t bdf, struct udevice **devp)
120 ret = pci_get_bus(PCI_BUS(bdf), &bus);
123 return pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), devp);
126 static int pci_device_matches_ids(struct udevice *dev,
127 struct pci_device_id *ids)
129 struct pci_child_platdata *pplat;
132 pplat = dev_get_parent_platdata(dev);
135 for (i = 0; ids[i].vendor != 0; i++) {
136 if (pplat->vendor == ids[i].vendor &&
137 pplat->device == ids[i].device)
144 int pci_bus_find_devices(struct udevice *bus, struct pci_device_id *ids,
145 int *indexp, struct udevice **devp)
149 /* Scan all devices on this bus */
150 for (device_find_first_child(bus, &dev);
152 device_find_next_child(&dev)) {
153 if (pci_device_matches_ids(dev, ids) >= 0) {
154 if ((*indexp)-- <= 0) {
164 int pci_find_device_id(struct pci_device_id *ids, int index,
165 struct udevice **devp)
169 /* Scan all known buses */
170 for (uclass_first_device(UCLASS_PCI, &bus);
172 uclass_next_device(&bus)) {
173 if (!pci_bus_find_devices(bus, ids, &index, devp))
181 static int dm_pci_bus_find_device(struct udevice *bus, unsigned int vendor,
182 unsigned int device, int *indexp,
183 struct udevice **devp)
185 struct pci_child_platdata *pplat;
188 for (device_find_first_child(bus, &dev);
190 device_find_next_child(&dev)) {
191 pplat = dev_get_parent_platdata(dev);
192 if (pplat->vendor == vendor && pplat->device == device) {
203 int dm_pci_find_device(unsigned int vendor, unsigned int device, int index,
204 struct udevice **devp)
208 /* Scan all known buses */
209 for (uclass_first_device(UCLASS_PCI, &bus);
211 uclass_next_device(&bus)) {
212 if (!dm_pci_bus_find_device(bus, vendor, device, &index, devp))
213 return device_probe(*devp);
220 int dm_pci_find_class(uint find_class, int index, struct udevice **devp)
224 /* Scan all known buses */
225 for (pci_find_first_device(&dev);
227 pci_find_next_device(&dev)) {
228 struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
230 if (pplat->class == find_class && !index--) {
232 return device_probe(*devp);
240 int pci_bus_write_config(struct udevice *bus, pci_dev_t bdf, int offset,
241 unsigned long value, enum pci_size_t size)
243 struct dm_pci_ops *ops;
245 ops = pci_get_ops(bus);
246 if (!ops->write_config)
248 return ops->write_config(bus, bdf, offset, value, size);
251 int pci_bus_clrset_config32(struct udevice *bus, pci_dev_t bdf, int offset,
257 ret = pci_bus_read_config(bus, bdf, offset, &val, PCI_SIZE_32);
263 return pci_bus_write_config(bus, bdf, offset, val, PCI_SIZE_32);
266 int pci_write_config(pci_dev_t bdf, int offset, unsigned long value,
267 enum pci_size_t size)
272 ret = pci_get_bus(PCI_BUS(bdf), &bus);
276 return pci_bus_write_config(bus, bdf, offset, value, size);
279 int dm_pci_write_config(struct udevice *dev, int offset, unsigned long value,
280 enum pci_size_t size)
284 for (bus = dev; device_is_on_pci_bus(bus);)
286 return pci_bus_write_config(bus, dm_pci_get_bdf(dev), offset, value,
290 int pci_write_config32(pci_dev_t bdf, int offset, u32 value)
292 return pci_write_config(bdf, offset, value, PCI_SIZE_32);
295 int pci_write_config16(pci_dev_t bdf, int offset, u16 value)
297 return pci_write_config(bdf, offset, value, PCI_SIZE_16);
300 int pci_write_config8(pci_dev_t bdf, int offset, u8 value)
302 return pci_write_config(bdf, offset, value, PCI_SIZE_8);
305 int dm_pci_write_config8(struct udevice *dev, int offset, u8 value)
307 return dm_pci_write_config(dev, offset, value, PCI_SIZE_8);
310 int dm_pci_write_config16(struct udevice *dev, int offset, u16 value)
312 return dm_pci_write_config(dev, offset, value, PCI_SIZE_16);
315 int dm_pci_write_config32(struct udevice *dev, int offset, u32 value)
317 return dm_pci_write_config(dev, offset, value, PCI_SIZE_32);
320 int pci_bus_read_config(struct udevice *bus, pci_dev_t bdf, int offset,
321 unsigned long *valuep, enum pci_size_t size)
323 struct dm_pci_ops *ops;
325 ops = pci_get_ops(bus);
326 if (!ops->read_config)
328 return ops->read_config(bus, bdf, offset, valuep, size);
331 int pci_read_config(pci_dev_t bdf, int offset, unsigned long *valuep,
332 enum pci_size_t size)
337 ret = pci_get_bus(PCI_BUS(bdf), &bus);
341 return pci_bus_read_config(bus, bdf, offset, valuep, size);
344 int dm_pci_read_config(struct udevice *dev, int offset, unsigned long *valuep,
345 enum pci_size_t size)
349 for (bus = dev; device_is_on_pci_bus(bus);)
351 return pci_bus_read_config(bus, dm_pci_get_bdf(dev), offset, valuep,
355 int pci_read_config32(pci_dev_t bdf, int offset, u32 *valuep)
360 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_32);
368 int pci_read_config16(pci_dev_t bdf, int offset, u16 *valuep)
373 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_16);
381 int pci_read_config8(pci_dev_t bdf, int offset, u8 *valuep)
386 ret = pci_read_config(bdf, offset, &value, PCI_SIZE_8);
394 int dm_pci_read_config8(struct udevice *dev, int offset, u8 *valuep)
399 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_8);
407 int dm_pci_read_config16(struct udevice *dev, int offset, u16 *valuep)
412 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_16);
420 int dm_pci_read_config32(struct udevice *dev, int offset, u32 *valuep)
425 ret = dm_pci_read_config(dev, offset, &value, PCI_SIZE_32);
433 int dm_pci_clrset_config8(struct udevice *dev, int offset, u32 clr, u32 set)
438 ret = dm_pci_read_config8(dev, offset, &val);
444 return dm_pci_write_config8(dev, offset, val);
447 int dm_pci_clrset_config16(struct udevice *dev, int offset, u32 clr, u32 set)
452 ret = dm_pci_read_config16(dev, offset, &val);
458 return dm_pci_write_config16(dev, offset, val);
461 int dm_pci_clrset_config32(struct udevice *dev, int offset, u32 clr, u32 set)
466 ret = dm_pci_read_config32(dev, offset, &val);
472 return dm_pci_write_config32(dev, offset, val);
475 static void set_vga_bridge_bits(struct udevice *dev)
477 struct udevice *parent = dev->parent;
480 while (parent->seq != 0) {
481 dm_pci_read_config16(parent, PCI_BRIDGE_CONTROL, &bc);
482 bc |= PCI_BRIDGE_CTL_VGA;
483 dm_pci_write_config16(parent, PCI_BRIDGE_CONTROL, bc);
484 parent = parent->parent;
488 int pci_auto_config_devices(struct udevice *bus)
490 struct pci_controller *hose = bus->uclass_priv;
491 struct pci_child_platdata *pplat;
492 unsigned int sub_bus;
497 debug("%s: start\n", __func__);
498 pciauto_config_init(hose);
499 for (ret = device_find_first_child(bus, &dev);
501 ret = device_find_next_child(&dev)) {
502 unsigned int max_bus;
505 debug("%s: device %s\n", __func__, dev->name);
506 ret = dm_pciauto_config_device(dev);
510 sub_bus = max(sub_bus, max_bus);
512 pplat = dev_get_parent_platdata(dev);
513 if (pplat->class == (PCI_CLASS_DISPLAY_VGA << 8))
514 set_vga_bridge_bits(dev);
516 debug("%s: done\n", __func__);
521 int pci_generic_mmap_write_config(
523 int (*addr_f)(struct udevice *bus, pci_dev_t bdf, uint offset, void **addrp),
527 enum pci_size_t size)
531 if (addr_f(bus, bdf, offset, &address) < 0)
536 writeb(value, address);
539 writew(value, address);
542 writel(value, address);
549 int pci_generic_mmap_read_config(
551 int (*addr_f)(struct udevice *bus, pci_dev_t bdf, uint offset, void **addrp),
555 enum pci_size_t size)
559 if (addr_f(bus, bdf, offset, &address) < 0) {
560 *valuep = pci_get_ff(size);
566 *valuep = readb(address);
569 *valuep = readw(address);
572 *valuep = readl(address);
579 int dm_pci_hose_probe_bus(struct udevice *bus)
584 debug("%s\n", __func__);
586 sub_bus = pci_get_bus_max() + 1;
587 debug("%s: bus = %d/%s\n", __func__, sub_bus, bus->name);
588 dm_pciauto_prescan_setup_bridge(bus, sub_bus);
590 ret = device_probe(bus);
592 debug("%s: Cannot probe bus %s: %d\n", __func__, bus->name,
596 if (sub_bus != bus->seq) {
597 printf("%s: Internal error, bus '%s' got seq %d, expected %d\n",
598 __func__, bus->name, bus->seq, sub_bus);
601 sub_bus = pci_get_bus_max();
602 dm_pciauto_postscan_setup_bridge(bus, sub_bus);
608 * pci_match_one_device - Tell if a PCI device structure has a matching
609 * PCI device id structure
610 * @id: single PCI device id structure to match
611 * @find: the PCI device id structure to match against
613 * Returns true if the finding pci_device_id structure matched or false if
616 static bool pci_match_one_id(const struct pci_device_id *id,
617 const struct pci_device_id *find)
619 if ((id->vendor == PCI_ANY_ID || id->vendor == find->vendor) &&
620 (id->device == PCI_ANY_ID || id->device == find->device) &&
621 (id->subvendor == PCI_ANY_ID || id->subvendor == find->subvendor) &&
622 (id->subdevice == PCI_ANY_ID || id->subdevice == find->subdevice) &&
623 !((id->class ^ find->class) & id->class_mask))
630 * pci_find_and_bind_driver() - Find and bind the right PCI driver
632 * This only looks at certain fields in the descriptor.
634 * @parent: Parent bus
635 * @find_id: Specification of the driver to find
636 * @bdf: Bus/device/function addreess - see PCI_BDF()
637 * @devp: Returns a pointer to the device created
638 * @return 0 if OK, -EPERM if the device is not needed before relocation and
639 * therefore was not created, other -ve value on error
641 static int pci_find_and_bind_driver(struct udevice *parent,
642 struct pci_device_id *find_id,
643 pci_dev_t bdf, struct udevice **devp)
645 struct pci_driver_entry *start, *entry;
654 debug("%s: Searching for driver: vendor=%x, device=%x\n", __func__,
655 find_id->vendor, find_id->device);
656 start = ll_entry_start(struct pci_driver_entry, pci_driver_entry);
657 n_ents = ll_entry_count(struct pci_driver_entry, pci_driver_entry);
658 for (entry = start; entry != start + n_ents; entry++) {
659 const struct pci_device_id *id;
661 const struct driver *drv;
663 for (id = entry->match;
664 id->vendor || id->subvendor || id->class_mask;
666 if (!pci_match_one_id(id, find_id))
672 * In the pre-relocation phase, we only bind devices
673 * whose driver has the DM_FLAG_PRE_RELOC set, to save
674 * precious memory space as on some platforms as that
675 * space is pretty limited (ie: using Cache As RAM).
677 if (!(gd->flags & GD_FLG_RELOC) &&
678 !(drv->flags & DM_FLAG_PRE_RELOC))
682 * We could pass the descriptor to the driver as
683 * platdata (instead of NULL) and allow its bind()
684 * method to return -ENOENT if it doesn't support this
685 * device. That way we could continue the search to
686 * find another driver. For now this doesn't seem
687 * necesssary, so just bind the first match.
689 ret = device_bind(parent, drv, drv->name, NULL, -1,
693 debug("%s: Match found: %s\n", __func__, drv->name);
694 dev->driver_data = find_id->driver_data;
700 bridge = (find_id->class >> 8) == PCI_CLASS_BRIDGE_PCI;
702 * In the pre-relocation phase, we only bind bridge devices to save
703 * precious memory space as on some platforms as that space is pretty
704 * limited (ie: using Cache As RAM).
706 if (!(gd->flags & GD_FLG_RELOC) && !bridge)
709 /* Bind a generic driver so that the device can be used */
710 sprintf(name, "pci_%x:%x.%x", parent->seq, PCI_DEV(bdf),
715 drv = bridge ? "pci_bridge_drv" : "pci_generic_drv";
717 ret = device_bind_driver(parent, drv, str, devp);
719 debug("%s: Failed to bind generic driver: %d\n", __func__, ret);
723 debug("%s: No match found: bound generic driver instead\n", __func__);
728 debug("%s: No match found: error %d\n", __func__, ret);
732 int pci_bind_bus_devices(struct udevice *bus)
734 ulong vendor, device;
741 end = PCI_BDF(bus->seq, PCI_MAX_PCI_DEVICES - 1,
742 PCI_MAX_PCI_FUNCTIONS - 1);
743 for (bdf = PCI_BDF(bus->seq, 0, 0); bdf <= end;
744 bdf += PCI_BDF(0, 0, 1)) {
745 struct pci_child_platdata *pplat;
749 if (PCI_FUNC(bdf) && !found_multi)
751 /* Check only the first access, we don't expect problems */
752 ret = pci_bus_read_config(bus, bdf, PCI_HEADER_TYPE,
753 &header_type, PCI_SIZE_8);
756 pci_bus_read_config(bus, bdf, PCI_VENDOR_ID, &vendor,
758 if (vendor == 0xffff || vendor == 0x0000)
762 found_multi = header_type & 0x80;
764 debug("%s: bus %d/%s: found device %x, function %d\n", __func__,
765 bus->seq, bus->name, PCI_DEV(bdf), PCI_FUNC(bdf));
766 pci_bus_read_config(bus, bdf, PCI_DEVICE_ID, &device,
768 pci_bus_read_config(bus, bdf, PCI_CLASS_REVISION, &class,
772 /* Find this device in the device tree */
773 ret = pci_bus_find_devfn(bus, PCI_MASK_BUS(bdf), &dev);
775 /* If nothing in the device tree, bind a device */
776 if (ret == -ENODEV) {
777 struct pci_device_id find_id;
780 memset(&find_id, '\0', sizeof(find_id));
781 find_id.vendor = vendor;
782 find_id.device = device;
783 find_id.class = class;
784 if ((header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL) {
785 pci_bus_read_config(bus, bdf,
786 PCI_SUBSYSTEM_VENDOR_ID,
788 find_id.subvendor = val & 0xffff;
789 find_id.subdevice = val >> 16;
791 ret = pci_find_and_bind_driver(bus, &find_id, bdf,
799 /* Update the platform data */
800 pplat = dev_get_parent_platdata(dev);
801 pplat->devfn = PCI_MASK_BUS(bdf);
802 pplat->vendor = vendor;
803 pplat->device = device;
804 pplat->class = class;
809 printf("Cannot read bus configuration: %d\n", ret);
814 static int decode_regions(struct pci_controller *hose, ofnode parent_node,
817 int pci_addr_cells, addr_cells, size_cells;
818 int cells_per_record;
823 prop = ofnode_get_property(node, "ranges", &len);
826 pci_addr_cells = ofnode_read_simple_addr_cells(node);
827 addr_cells = ofnode_read_simple_addr_cells(parent_node);
828 size_cells = ofnode_read_simple_size_cells(node);
830 /* PCI addresses are always 3-cells */
832 cells_per_record = pci_addr_cells + addr_cells + size_cells;
833 hose->region_count = 0;
834 debug("%s: len=%d, cells_per_record=%d\n", __func__, len,
836 for (i = 0; i < MAX_PCI_REGIONS; i++, len -= cells_per_record) {
837 u64 pci_addr, addr, size;
843 if (len < cells_per_record)
845 flags = fdt32_to_cpu(prop[0]);
846 space_code = (flags >> 24) & 3;
847 pci_addr = fdtdec_get_number(prop + 1, 2);
848 prop += pci_addr_cells;
849 addr = fdtdec_get_number(prop, addr_cells);
851 size = fdtdec_get_number(prop, size_cells);
853 debug("%s: region %d, pci_addr=%" PRIx64 ", addr=%" PRIx64
854 ", size=%" PRIx64 ", space_code=%d\n", __func__,
855 hose->region_count, pci_addr, addr, size, space_code);
856 if (space_code & 2) {
857 type = flags & (1U << 30) ? PCI_REGION_PREFETCH :
859 } else if (space_code & 1) {
860 type = PCI_REGION_IO;
865 for (i = 0; i < hose->region_count; i++) {
866 if (hose->regions[i].flags == type)
870 pos = hose->region_count++;
871 debug(" - type=%d, pos=%d\n", type, pos);
872 pci_set_region(hose->regions + pos, pci_addr, addr, size, type);
875 /* Add a region for our local memory */
876 #ifdef CONFIG_NR_DRAM_BANKS
882 for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
883 if (bd->bi_dram[i].size) {
884 pci_set_region(hose->regions + hose->region_count++,
885 bd->bi_dram[i].start,
886 bd->bi_dram[i].start,
888 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
892 phys_addr_t base = 0, size;
895 #ifdef CONFIG_SYS_SDRAM_BASE
896 base = CONFIG_SYS_SDRAM_BASE;
898 if (gd->pci_ram_top && gd->pci_ram_top < base + size)
899 size = gd->pci_ram_top - base;
901 pci_set_region(hose->regions + hose->region_count++, base,
902 base, size, PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
908 static int pci_uclass_pre_probe(struct udevice *bus)
910 struct pci_controller *hose;
913 debug("%s, bus=%d/%s, parent=%s\n", __func__, bus->seq, bus->name,
915 hose = bus->uclass_priv;
917 /* For bridges, use the top-level PCI controller */
918 if (!device_is_on_pci_bus(bus)) {
920 ret = decode_regions(hose, dev_ofnode(bus->parent),
923 debug("%s: Cannot decode regions\n", __func__);
927 struct pci_controller *parent_hose;
929 parent_hose = dev_get_uclass_priv(bus->parent);
930 hose->ctlr = parent_hose->bus;
933 hose->first_busno = bus->seq;
934 hose->last_busno = bus->seq;
939 static int pci_uclass_post_probe(struct udevice *bus)
943 debug("%s: probing bus %d\n", __func__, bus->seq);
944 ret = pci_bind_bus_devices(bus);
948 #ifdef CONFIG_PCI_PNP
949 ret = pci_auto_config_devices(bus);
954 #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
956 * Per Intel FSP specification, we should call FSP notify API to
957 * inform FSP that PCI enumeration has been done so that FSP will
958 * do any necessary initialization as required by the chipset's
959 * BIOS Writer's Guide (BWG).
961 * Unfortunately we have to put this call here as with driver model,
962 * the enumeration is all done on a lazy basis as needed, so until
963 * something is touched on PCI it won't happen.
965 * Note we only call this 1) after U-Boot is relocated, and 2)
966 * root bus has finished probing.
968 if ((gd->flags & GD_FLG_RELOC) && (bus->seq == 0)) {
969 ret = fsp_init_phase_pci();
978 static int pci_uclass_child_post_bind(struct udevice *dev)
980 struct pci_child_platdata *pplat;
981 struct fdt_pci_addr addr;
984 if (!dev_of_valid(dev))
988 * We could read vendor, device, class if available. But for now we
989 * just check the address.
991 pplat = dev_get_parent_platdata(dev);
992 ret = ofnode_read_pci_addr(dev_ofnode(dev), FDT_PCI_SPACE_CONFIG, "reg",
999 /* extract the devfn from fdt_pci_addr */
1000 pplat->devfn = addr.phys_hi & 0xff00;
1006 static int pci_bridge_read_config(struct udevice *bus, pci_dev_t bdf,
1007 uint offset, ulong *valuep,
1008 enum pci_size_t size)
1010 struct pci_controller *hose = bus->uclass_priv;
1012 return pci_bus_read_config(hose->ctlr, bdf, offset, valuep, size);
1015 static int pci_bridge_write_config(struct udevice *bus, pci_dev_t bdf,
1016 uint offset, ulong value,
1017 enum pci_size_t size)
1019 struct pci_controller *hose = bus->uclass_priv;
1021 return pci_bus_write_config(hose->ctlr, bdf, offset, value, size);
1024 static int skip_to_next_device(struct udevice *bus, struct udevice **devp)
1026 struct udevice *dev;
1030 * Scan through all the PCI controllers. On x86 there will only be one
1031 * but that is not necessarily true on other hardware.
1034 device_find_first_child(bus, &dev);
1039 ret = uclass_next_device(&bus);
1047 int pci_find_next_device(struct udevice **devp)
1049 struct udevice *child = *devp;
1050 struct udevice *bus = child->parent;
1053 /* First try all the siblings */
1056 device_find_next_child(&child);
1063 /* We ran out of siblings. Try the next bus */
1064 ret = uclass_next_device(&bus);
1068 return bus ? skip_to_next_device(bus, devp) : 0;
1071 int pci_find_first_device(struct udevice **devp)
1073 struct udevice *bus;
1077 ret = uclass_first_device(UCLASS_PCI, &bus);
1081 return skip_to_next_device(bus, devp);
1084 ulong pci_conv_32_to_size(ulong value, uint offset, enum pci_size_t size)
1088 return (value >> ((offset & 3) * 8)) & 0xff;
1090 return (value >> ((offset & 2) * 8)) & 0xffff;
1096 ulong pci_conv_size_to_32(ulong old, ulong value, uint offset,
1097 enum pci_size_t size)
1100 uint val_mask, shift;
1115 shift = (offset & off_mask) * 8;
1116 ldata = (value & val_mask) << shift;
1117 mask = val_mask << shift;
1118 value = (old & ~mask) | ldata;
1123 int pci_get_regions(struct udevice *dev, struct pci_region **iop,
1124 struct pci_region **memp, struct pci_region **prefp)
1126 struct udevice *bus = pci_get_controller(dev);
1127 struct pci_controller *hose = dev_get_uclass_priv(bus);
1133 for (i = 0; i < hose->region_count; i++) {
1134 switch (hose->regions[i].flags) {
1136 if (!*iop || (*iop)->size < hose->regions[i].size)
1137 *iop = hose->regions + i;
1139 case PCI_REGION_MEM:
1140 if (!*memp || (*memp)->size < hose->regions[i].size)
1141 *memp = hose->regions + i;
1143 case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
1144 if (!*prefp || (*prefp)->size < hose->regions[i].size)
1145 *prefp = hose->regions + i;
1150 return (*iop != NULL) + (*memp != NULL) + (*prefp != NULL);
1153 u32 dm_pci_read_bar32(struct udevice *dev, int barnum)
1158 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1159 dm_pci_read_config32(dev, bar, &addr);
1160 if (addr & PCI_BASE_ADDRESS_SPACE_IO)
1161 return addr & PCI_BASE_ADDRESS_IO_MASK;
1163 return addr & PCI_BASE_ADDRESS_MEM_MASK;
1166 void dm_pci_write_bar32(struct udevice *dev, int barnum, u32 addr)
1170 bar = PCI_BASE_ADDRESS_0 + barnum * 4;
1171 dm_pci_write_config32(dev, bar, addr);
1174 static int _dm_pci_bus_to_phys(struct udevice *ctlr,
1175 pci_addr_t bus_addr, unsigned long flags,
1176 unsigned long skip_mask, phys_addr_t *pa)
1178 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
1179 struct pci_region *res;
1182 for (i = 0; i < hose->region_count; i++) {
1183 res = &hose->regions[i];
1185 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1188 if (res->flags & skip_mask)
1191 if (bus_addr >= res->bus_start &&
1192 (bus_addr - res->bus_start) < res->size) {
1193 *pa = (bus_addr - res->bus_start + res->phys_start);
1201 phys_addr_t dm_pci_bus_to_phys(struct udevice *dev, pci_addr_t bus_addr,
1202 unsigned long flags)
1204 phys_addr_t phys_addr = 0;
1205 struct udevice *ctlr;
1208 /* The root controller has the region information */
1209 ctlr = pci_get_controller(dev);
1212 * if PCI_REGION_MEM is set we do a two pass search with preference
1213 * on matches that don't have PCI_REGION_SYS_MEMORY set
1215 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1216 ret = _dm_pci_bus_to_phys(ctlr, bus_addr,
1217 flags, PCI_REGION_SYS_MEMORY,
1223 ret = _dm_pci_bus_to_phys(ctlr, bus_addr, flags, 0, &phys_addr);
1226 puts("pci_hose_bus_to_phys: invalid physical address\n");
1231 int _dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1232 unsigned long flags, unsigned long skip_mask,
1235 struct pci_region *res;
1236 struct udevice *ctlr;
1237 pci_addr_t bus_addr;
1239 struct pci_controller *hose;
1241 /* The root controller has the region information */
1242 ctlr = pci_get_controller(dev);
1243 hose = dev_get_uclass_priv(ctlr);
1245 for (i = 0; i < hose->region_count; i++) {
1246 res = &hose->regions[i];
1248 if (((res->flags ^ flags) & PCI_REGION_TYPE) != 0)
1251 if (res->flags & skip_mask)
1254 bus_addr = phys_addr - res->phys_start + res->bus_start;
1256 if (bus_addr >= res->bus_start &&
1257 (bus_addr - res->bus_start) < res->size) {
1266 pci_addr_t dm_pci_phys_to_bus(struct udevice *dev, phys_addr_t phys_addr,
1267 unsigned long flags)
1269 pci_addr_t bus_addr = 0;
1273 * if PCI_REGION_MEM is set we do a two pass search with preference
1274 * on matches that don't have PCI_REGION_SYS_MEMORY set
1276 if ((flags & PCI_REGION_TYPE) == PCI_REGION_MEM) {
1277 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags,
1278 PCI_REGION_SYS_MEMORY, &bus_addr);
1283 ret = _dm_pci_phys_to_bus(dev, phys_addr, flags, 0, &bus_addr);
1286 puts("pci_hose_phys_to_bus: invalid physical address\n");
1291 void *dm_pci_map_bar(struct udevice *dev, int bar, int flags)
1293 pci_addr_t pci_bus_addr;
1296 /* read BAR address */
1297 dm_pci_read_config32(dev, bar, &bar_response);
1298 pci_bus_addr = (pci_addr_t)(bar_response & ~0xf);
1301 * Pass "0" as the length argument to pci_bus_to_virt. The arg
1302 * isn't actualy used on any platform because u-boot assumes a static
1303 * linear mapping. In the future, this could read the BAR size
1304 * and pass that as the size if needed.
1306 return dm_pci_bus_to_virt(dev, pci_bus_addr, flags, 0, MAP_NOCACHE);
1309 UCLASS_DRIVER(pci) = {
1312 .flags = DM_UC_FLAG_SEQ_ALIAS,
1313 .post_bind = dm_scan_fdt_dev,
1314 .pre_probe = pci_uclass_pre_probe,
1315 .post_probe = pci_uclass_post_probe,
1316 .child_post_bind = pci_uclass_child_post_bind,
1317 .per_device_auto_alloc_size = sizeof(struct pci_controller),
1318 .per_child_platdata_auto_alloc_size =
1319 sizeof(struct pci_child_platdata),
1322 static const struct dm_pci_ops pci_bridge_ops = {
1323 .read_config = pci_bridge_read_config,
1324 .write_config = pci_bridge_write_config,
1327 static const struct udevice_id pci_bridge_ids[] = {
1328 { .compatible = "pci-bridge" },
1332 U_BOOT_DRIVER(pci_bridge_drv) = {
1333 .name = "pci_bridge_drv",
1335 .of_match = pci_bridge_ids,
1336 .ops = &pci_bridge_ops,
1339 UCLASS_DRIVER(pci_generic) = {
1340 .id = UCLASS_PCI_GENERIC,
1341 .name = "pci_generic",
1344 static const struct udevice_id pci_generic_ids[] = {
1345 { .compatible = "pci-generic" },
1349 U_BOOT_DRIVER(pci_generic_drv) = {
1350 .name = "pci_generic_drv",
1351 .id = UCLASS_PCI_GENERIC,
1352 .of_match = pci_generic_ids,
1357 struct udevice *bus;
1360 * Enumerate all known controller devices. Enumeration has the side-
1361 * effect of probing them, so PCIe devices will be enumerated too.
1363 for (uclass_first_device(UCLASS_PCI, &bus);
1365 uclass_next_device(&bus)) {