xref: /aosp_15_r20/external/coreboot/src/device/root_device.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <console/console.h>
4 #include <device/device.h>
5 #include <device/pci.h>
6 #include <reset.h>
7 
enable_static_device(struct device * dev)8 void enable_static_device(struct device *dev)
9 {
10 	if (dev->chip_ops && dev->chip_ops->enable_dev)
11 		dev->chip_ops->enable_dev(dev);
12 
13 	if (dev->ops && dev->ops->enable)
14 		dev->ops->enable(dev);
15 
16 	printk(BIOS_DEBUG, "%s %s\n", dev_path(dev),
17 	       dev->enabled ? "enabled" : "disabled");
18 }
19 
20 /**
21  * Enable devices on static buses.
22  *
23  * The enumeration of certain buses is purely static. The existence of
24  * devices on those buses can be completely determined at compile time
25  * and is specified in the config file. Typical examples are the 'PNP'
26  * devices on a legacy ISA/LPC bus. There is no need of probing of any kind,
27  * the only thing we have to do is to walk through the bus and
28  * enable or disable devices as indicated in the config file.
29  *
30  * On the other hand, some devices are virtual and their existence is
31  * artificial. They can not be probed at run time. One example is the
32  * debug device. Those virtual devices have to be listed in the config
33  * file under some static bus in order to be enumerated at run time.
34  *
35  * @param bus Pointer to the device to which the static buses are attached to.
36  */
37 
enable_static_devices(struct device * bus)38 void enable_static_devices(struct device *bus)
39 {
40 	struct device *child;
41 
42 	if (!bus->downstream)
43 		return;
44 
45 	for (child = bus->downstream->children; child; child = child->sibling)
46 		enable_static_device(child);
47 }
48 
scan_generic_bus(struct device * bus)49 void scan_generic_bus(struct device *bus)
50 {
51 	struct device *child;
52 	static int bus_max = 0;
53 
54 	printk(BIOS_SPEW, "%s for %s\n", __func__, dev_path(bus));
55 
56 	if (bus->downstream) {
57 		bus->downstream->secondary = ++bus_max;
58 
59 		for (child = bus->downstream->children; child; child = child->sibling) {
60 			enable_static_device(child);
61 			printk(BIOS_DEBUG, "bus: %s->", dev_path(child->upstream->dev));
62 		}
63 	}
64 
65 	printk(BIOS_SPEW, "%s for %s done\n", __func__, dev_path(bus));
66 }
67 
scan_smbus(struct device * bus)68 void scan_smbus(struct device *bus)
69 {
70 	scan_generic_bus(bus);
71 }
72 
73 /*
74  * Default scan_bus() implementation
75  *
76  * This is the default implementation for buses that can't
77  * be probed at runtime. It simply walks through the topology
78  * given by the mainboard's `devicetree.cb`.
79  *
80  * First, all direct descendants of the given device are
81  * enabled. Then, downstream buses are scanned.
82  */
scan_static_bus(struct device * bus)83 void scan_static_bus(struct device *bus)
84 {
85 	printk(BIOS_SPEW, "%s for %s\n", __func__, dev_path(bus));
86 
87 	enable_static_devices(bus);
88 
89 	if (bus->downstream)
90 		scan_bridges(bus->downstream);
91 
92 	printk(BIOS_SPEW, "%s for %s done\n", __func__, dev_path(bus));
93 }
94 
root_dev_reset(struct bus * bus)95 static void root_dev_reset(struct bus *bus)
96 {
97 	printk(BIOS_INFO, "Resetting board...\n");
98 	board_reset();
99 }
100 
101 #if CONFIG(HAVE_ACPI_TABLES)
root_dev_acpi_name(const struct device * dev)102 static const char *root_dev_acpi_name(const struct device *dev)
103 {
104 	return "\\_SB";
105 }
106 #endif
107 
108 /**
109  * Default device operation for root device.
110  *
111  * This is the default device operation for root devices. These operations
112  * should be fully usable as is. However the chip_operations::enable_dev()
113  * of a motherboard can override this if you want non-default behavior.
114  */
115 struct device_operations default_dev_ops_root = {
116 	.read_resources   = noop_read_resources,
117 	.set_resources    = noop_set_resources,
118 	.scan_bus         = scan_static_bus,
119 	.reset_bus        = root_dev_reset,
120 #if CONFIG(HAVE_ACPI_TABLES)
121 	.acpi_name        = root_dev_acpi_name,
122 #endif
123 };
124