blob: 811b70937cf4b352d6e9e78c8b3ea4826b63f069 [file] [log] [blame]
Angel Ponsc74dae92020-04-02 23:48:16 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Uwe Hermanne4870472010-11-04 23:23:47 +00002
3/*
Martin Roth99f83bb2019-09-15 20:57:18 -07004 * Originally based on the Linux kernel (arch/i386/kernel/pci-pc.c).
Eric Biederman8ca8d762003-04-22 19:02:15 +00005 */
6
7#include <console/console.h>
Eric Biederman5899fd82003-04-24 06:25:08 +00008#include <device/device.h>
Kyösti Mälkki318066f2014-02-12 14:18:50 +02009#include <device/pci_def.h>
Li-Ta Lo54f05f62004-05-14 17:20:29 +000010#include <device/pci_ids.h>
Kyösti Mälkkice39ba92020-01-04 16:15:50 +020011#include <post.h>
Eric Biedermane9a271e32003-09-02 03:36:25 +000012#include <stdlib.h>
13#include <string.h>
Eric Biederman03acab62004-10-14 21:25:53 +000014#include <smp/spinlock.h>
Aaron Durbin05294292013-04-30 15:41:13 -050015#include <timer.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000016
Li-Ta Loe5266692004-03-23 21:28:05 +000017/** Pointer to the last device */
Myles Watson70679a02010-09-01 21:03:03 +000018extern struct device *last_dev;
Myles Watsonc25cc112010-05-21 14:33:48 +000019/** Linked list of free resources */
20struct resource *free_resources = NULL;
Subrata Baniked5c7ac2021-06-10 13:04:07 +053021/* Disable a PCI device based on bus, device and function. */
22void devfn_disable(const struct bus *bus, unsigned int devfn)
23{
24 struct device *dev = pcidev_path_behind(bus, devfn);
25 if (dev)
26 dev->enabled = 0;
27}
Eric Biederman8ca8d762003-04-22 19:02:15 +000028
Nico Huberacd7d952012-07-25 10:33:05 +020029/**
30 * Initialize all chips of statically known devices.
31 *
32 * Will be called before bus enumeration to initialize chips stated in the
33 * device tree.
34 */
35void dev_initialize_chips(void)
36{
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +020037 const struct device *dev;
Nico Huberacd7d952012-07-25 10:33:05 +020038
39 for (dev = all_devices; dev; dev = dev->next) {
40 /* Initialize chip if we haven't yet. */
41 if (dev->chip_ops && dev->chip_ops->init &&
42 !dev->chip_ops->initialized) {
Duncan Laurie8adf7a22013-06-10 10:34:20 -070043 post_log_path(dev);
Nico Huberacd7d952012-07-25 10:33:05 +020044 dev->chip_ops->init(dev->chip_info);
45 dev->chip_ops->initialized = 1;
46 }
47 }
Duncan Laurie8adf7a22013-06-10 10:34:20 -070048 post_log_clear();
Nico Huberacd7d952012-07-25 10:33:05 +020049}
50
Marc Jones2a58ecd2013-10-29 17:32:00 -060051/**
52 * Finalize all chips of statically known devices.
53 *
54 * This is the last call before calling the payload. This is a good place
55 * to lock registers or other final cleanup.
56 */
57void dev_finalize_chips(void)
58{
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +020059 const struct device *dev;
Marc Jones2a58ecd2013-10-29 17:32:00 -060060
61 for (dev = all_devices; dev; dev = dev->next) {
62 /* Initialize chip if we haven't yet. */
63 if (dev->chip_ops && dev->chip_ops->final &&
64 !dev->chip_ops->finalized) {
65 dev->chip_ops->final(dev->chip_info);
66 dev->chip_ops->finalized = 1;
67 }
68 }
69}
70
Uwe Hermannc1ee4292010-10-17 19:01:48 +000071DECLARE_SPIN_LOCK(dev_lock)
Eric Biederman8ca8d762003-04-22 19:02:15 +000072
Li-Ta Loe5266692004-03-23 21:28:05 +000073/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +000074 * Allocate a new device structure.
Myles Watson032a9652009-05-11 22:24:53 +000075 *
Martin Roth63373ed2013-07-08 16:24:19 -060076 * Allocate a new device structure and attach it to the device tree as a
Li-Ta Lo9f0d0f92004-05-10 16:05:16 +000077 * child of the parent bus.
Li-Ta Loe5266692004-03-23 21:28:05 +000078 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +000079 * @param parent Parent bus the newly created device should be attached to.
80 * @param path Path to the device to be created.
81 * @return Pointer to the newly created device structure.
Li-Ta Loe5266692004-03-23 21:28:05 +000082 *
83 * @see device_path
Eric Biederman8ca8d762003-04-22 19:02:15 +000084 */
Elyes HAOUASd34a7852018-09-17 10:44:14 +020085static struct device *__alloc_dev(struct bus *parent, struct device_path *path)
Eric Biederman8ca8d762003-04-22 19:02:15 +000086{
Elyes HAOUASe3480662018-05-06 20:32:23 +020087 struct device *dev, *child;
Li-Ta Loe5266692004-03-23 21:28:05 +000088
Myles Watson29cc9ed2009-07-02 18:56:24 +000089 /* Find the last child of our parent. */
Elyes HAOUASa342f392018-10-17 10:56:26 +020090 for (child = parent->children; child && child->sibling; /* */)
Eric Biedermane9a271e32003-09-02 03:36:25 +000091 child = child->sibling;
Li-Ta Lo3a812852004-12-03 22:39:34 +000092
Eric Biedermane9a271e32003-09-02 03:36:25 +000093 dev = malloc(sizeof(*dev));
Myles Watson29cc9ed2009-07-02 18:56:24 +000094 if (dev == 0)
Uwe Hermanne4870472010-11-04 23:23:47 +000095 die("alloc_dev(): out of memory.\n");
Myles Watson29cc9ed2009-07-02 18:56:24 +000096
Eric Biedermane9a271e32003-09-02 03:36:25 +000097 memset(dev, 0, sizeof(*dev));
98 memcpy(&dev->path, path, sizeof(*path));
99
Myles Watson29cc9ed2009-07-02 18:56:24 +0000100 /* By default devices are enabled. */
Eric Biederman03acab62004-10-14 21:25:53 +0000101 dev->enabled = 1;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000102
Eric Biedermanb78c1972004-10-14 20:54:17 +0000103 /* Add the new device to the list of children of the bus. */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000104 dev->bus = parent;
Uwe Hermanne4870472010-11-04 23:23:47 +0000105 if (child)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000106 child->sibling = dev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000107 else
Eric Biedermane9a271e32003-09-02 03:36:25 +0000108 parent->children = dev;
Li-Ta Loe5266692004-03-23 21:28:05 +0000109
Eric Biederman03acab62004-10-14 21:25:53 +0000110 /* Append a new device to the global device list.
111 * The list is used to find devices once everything is set up.
112 */
Myles Watson70679a02010-09-01 21:03:03 +0000113 last_dev->next = dev;
114 last_dev = dev;
Eric Biederman03acab62004-10-14 21:25:53 +0000115
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300116 return dev;
117}
118
Elyes HAOUASd34a7852018-09-17 10:44:14 +0200119struct device *alloc_dev(struct bus *parent, struct device_path *path)
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300120{
Elyes HAOUASe3480662018-05-06 20:32:23 +0200121 struct device *dev;
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300122 spin_lock(&dev_lock);
123 dev = __alloc_dev(parent, path);
Eric Biederman03acab62004-10-14 21:25:53 +0000124 spin_unlock(&dev_lock);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000125 return dev;
126}
Eric Biederman8ca8d762003-04-22 19:02:15 +0000127
Li-Ta Loe5266692004-03-23 21:28:05 +0000128/**
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300129 * See if a device structure already exists and if not allocate it.
130 *
131 * @param parent The bus to find the device on.
132 * @param path The relative path from the bus to the appropriate device.
133 * @return Pointer to a device structure for the device on bus at path.
134 */
Elyes HAOUASd34a7852018-09-17 10:44:14 +0200135struct device *alloc_find_dev(struct bus *parent, struct device_path *path)
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300136{
Elyes HAOUASe3480662018-05-06 20:32:23 +0200137 struct device *child;
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300138 spin_lock(&dev_lock);
139 child = find_dev_path(parent, path);
140 if (!child)
141 child = __alloc_dev(parent, path);
142 spin_unlock(&dev_lock);
143 return child;
144}
145
146/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000147 * Read the resources on all devices of a given bus.
148 *
149 * @param bus Bus to read the resources on.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000150 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000151static void read_resources(struct bus *bus)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000152{
153 struct device *curdev;
154
Felix Held3b5b66d2024-01-11 22:26:18 +0100155 printk(BIOS_SPEW, "%s %s segment group %d bus %d link: %d\n", dev_path(bus->dev),
156 __func__, bus->segment_group, bus->secondary, bus->link_num);
Eric Biederman448bd632004-10-14 22:52:15 +0000157
Myles Watson29cc9ed2009-07-02 18:56:24 +0000158 /* Walk through all devices and find which resources they need. */
159 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
Myles Watson894a3472010-06-09 22:41:35 +0000160 struct bus *link;
Uwe Hermanne4870472010-11-04 23:23:47 +0000161
162 if (!curdev->enabled)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000163 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000164
Eric Biedermane9a271e32003-09-02 03:36:25 +0000165 if (!curdev->ops || !curdev->ops->read_resources) {
Martin Roth7bc74ab2015-11-18 20:23:02 -0700166 if (curdev->path.type != DEVICE_PATH_APIC)
Frans Hendriksa9caa502021-02-01 11:44:37 +0100167 printk(BIOS_ERR, "%s missing %s\n",
168 dev_path(curdev), __func__);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000169 continue;
170 }
Duncan Laurie7ed39762013-07-09 10:46:52 -0700171 post_log_path(curdev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000172 curdev->ops->read_resources(curdev);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000173
174 /* Read in the resources behind the current device's links. */
Myles Watson894a3472010-06-09 22:41:35 +0000175 for (link = curdev->link_list; link; link = link->next)
176 read_resources(link);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000177 }
Duncan Laurie7ed39762013-07-09 10:46:52 -0700178 post_log_clear();
Felix Held3b5b66d2024-01-11 22:26:18 +0100179 printk(BIOS_SPEW, "%s %s segment group %d bus %d link: %d done\n",
180 dev_path(bus->dev), __func__, bus->segment_group, bus->secondary, bus->link_num);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000181}
182
Elyes HAOUASe3480662018-05-06 20:32:23 +0200183struct device *vga_pri = NULL;
Myles Watsonc7233e02009-07-02 19:02:33 +0000184static void set_vga_bridge_bits(void)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000185{
Uwe Hermann312673c2009-10-27 21:49:33 +0000186 /*
Martin Roth63373ed2013-07-08 16:24:19 -0600187 * FIXME: Modify set_vga_bridge() so it is less PCI-centric!
Uwe Hermann312673c2009-10-27 21:49:33 +0000188 * This function knows too much about PCI stuff, it should be just
189 * an iterator/visitor.
190 */
Li-Ta Loe5266692004-03-23 21:28:05 +0000191
Myles Watson29cc9ed2009-07-02 18:56:24 +0000192 /* FIXME: Handle the VGA palette snooping. */
Patrick Georgi557ecf22012-07-20 12:16:17 +0200193 struct device *dev, *vga, *vga_onboard;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000194 struct bus *bus;
Uwe Hermanne4870472010-11-04 23:23:47 +0000195
Eric Biedermanb78c1972004-10-14 20:54:17 +0000196 bus = 0;
197 vga = 0;
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000198 vga_onboard = 0;
Uwe Hermanne4870472010-11-04 23:23:47 +0000199
Patrick Georgi557ecf22012-07-20 12:16:17 +0200200 dev = NULL;
201 while ((dev = dev_find_class(PCI_CLASS_DISPLAY_VGA << 8, dev))) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000202 if (!dev->enabled)
203 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000204
Patrick Georgi557ecf22012-07-20 12:16:17 +0200205 printk(BIOS_DEBUG, "found VGA at %s\n", dev_path(dev));
Nico Huber061b9052019-09-21 15:58:23 +0200206 if (dev->bus->no_vga16) {
207 printk(BIOS_WARNING,
208 "A bridge on the path doesn't support 16-bit VGA decoding!");
209 }
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000210
Frans Hendriksa9caa502021-02-01 11:44:37 +0100211 if (dev->on_mainboard)
Patrick Georgi557ecf22012-07-20 12:16:17 +0200212 vga_onboard = dev;
Frans Hendriksa9caa502021-02-01 11:44:37 +0100213 else
Patrick Georgi557ecf22012-07-20 12:16:17 +0200214 vga = dev;
Myles Watson032a9652009-05-11 22:24:53 +0000215
Patrick Georgi557ecf22012-07-20 12:16:17 +0200216 /* It isn't safe to enable all VGA cards. */
217 dev->command &= ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
Patrick Georgi594473d2012-07-25 08:55:53 +0200218 }
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000219
Uwe Hermanne4870472010-11-04 23:23:47 +0000220 if (!vga)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000221 vga = vga_onboard;
Patrick Georgi557ecf22012-07-20 12:16:17 +0200222
Julius Werner5d1f9a02019-03-07 17:07:26 -0800223 if (CONFIG(ONBOARD_VGA_IS_PRIMARY) && vga_onboard)
Patrick Georgi557ecf22012-07-20 12:16:17 +0200224 vga = vga_onboard;
Myles Watson032a9652009-05-11 22:24:53 +0000225
Patrick Georgi5869fa22012-07-20 12:29:33 +0200226 /* If we prefer plugin VGA over chipset VGA, the chipset might
227 want to know. */
Julius Werner5d1f9a02019-03-07 17:07:26 -0800228 if (!CONFIG(ONBOARD_VGA_IS_PRIMARY) && (vga != vga_onboard) &&
Arthur Heymansb238caa2021-02-22 18:33:08 +0100229 vga_onboard && vga_onboard->ops && vga_onboard->ops->vga_disable) {
Patrick Georgi5869fa22012-07-20 12:29:33 +0200230 printk(BIOS_DEBUG, "Use plugin graphics over integrated.\n");
Arthur Heymansb238caa2021-02-22 18:33:08 +0100231 vga_onboard->ops->vga_disable(vga_onboard);
Patrick Georgi5869fa22012-07-20 12:29:33 +0200232 }
233
arch import user (historical)dc811182005-07-06 17:16:09 +0000234 if (vga) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000235 /* VGA is first add-on card or the only onboard VGA. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000236 printk(BIOS_DEBUG, "Setting up VGA for %s\n", dev_path(vga));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000237 /* All legacy VGA cards have MEM & I/O space registers. */
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000238 vga->command |= (PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
239 vga_pri = vga;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000240 bus = vga->bus;
241 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000242
Myles Watson29cc9ed2009-07-02 18:56:24 +0000243 /* Now walk up the bridges setting the VGA enable. */
244 while (bus) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000245 printk(BIOS_DEBUG, "Setting PCI_BRIDGE_CTL_VGA for bridge %s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000246 dev_path(bus->dev));
Nico Huber061b9052019-09-21 15:58:23 +0200247 bus->bridge_ctrl |= PCI_BRIDGE_CTL_VGA | PCI_BRIDGE_CTL_VGA16;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000248 bus = (bus == bus->dev->bus) ? 0 : bus->dev->bus;
Myles Watson032a9652009-05-11 22:24:53 +0000249 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000250}
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000251
Li-Ta Lo04930692004-11-25 17:37:19 +0000252/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000253 * Assign the computed resources to the devices on the bus.
Li-Ta Lo04930692004-11-25 17:37:19 +0000254 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000255 * Use the device specific set_resources() method to store the computed
Li-Ta Lo04930692004-11-25 17:37:19 +0000256 * resources to hardware. For bridge devices, the set_resources() method
257 * has to recurse into every down stream buses.
258 *
259 * Mutual recursion:
260 * assign_resources() -> device_operation::set_resources()
261 * device_operation::set_resources() -> assign_resources()
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000262 *
263 * @param bus Pointer to the structure for this bus.
Li-Ta Lo04930692004-11-25 17:37:19 +0000264 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000265void assign_resources(struct bus *bus)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000266{
267 struct device *curdev;
268
Felix Held3b5b66d2024-01-11 22:26:18 +0100269 printk(BIOS_SPEW, "%s %s, segment group %d bus %d link: %d\n",
270 dev_path(bus->dev), __func__, bus->segment_group, bus->secondary, bus->link_num);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000271
Myles Watson29cc9ed2009-07-02 18:56:24 +0000272 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000273 if (!curdev->enabled || !curdev->resource_list)
Eric Biederman03acab62004-10-14 21:25:53 +0000274 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000275
Eric Biedermane9a271e32003-09-02 03:36:25 +0000276 if (!curdev->ops || !curdev->ops->set_resources) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000277 printk(BIOS_ERR, "%s missing set_resources\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000278 dev_path(curdev));
Eric Biedermane9a271e32003-09-02 03:36:25 +0000279 continue;
280 }
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700281 post_log_path(curdev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000282 curdev->ops->set_resources(curdev);
283 }
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700284 post_log_clear();
Felix Held3b5b66d2024-01-11 22:26:18 +0100285 printk(BIOS_SPEW, "%s %s, segment group %d bus %d link: %d done\n",
286 dev_path(bus->dev), __func__, bus->segment_group, bus->secondary, bus->link_num);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000287}
288
Li-Ta Lo5782d272004-04-26 17:51:20 +0000289/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000290 * Enable the resources for devices on a link.
Li-Ta Lo5782d272004-04-26 17:51:20 +0000291 *
292 * Enable resources of the device by calling the device specific
293 * enable_resources() method.
294 *
295 * The parent's resources should be enabled first to avoid having enabling
296 * order problem. This is done by calling the parent's enable_resources()
Martin Roth63373ed2013-07-08 16:24:19 -0600297 * method before its children's enable_resources() methods.
Li-Ta Lo9f0d0f92004-05-10 16:05:16 +0000298 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000299 * @param link The link whose devices' resources are to be enabled.
Li-Ta Lo5782d272004-04-26 17:51:20 +0000300 */
Myles Watson7eac4452010-06-17 16:16:56 +0000301static void enable_resources(struct bus *link)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000302{
Myles Watson7eac4452010-06-17 16:16:56 +0000303 struct device *dev;
304 struct bus *c_link;
305
306 for (dev = link->children; dev; dev = dev->sibling) {
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700307 if (dev->enabled && dev->ops && dev->ops->enable_resources) {
308 post_log_path(dev);
Myles Watson7eac4452010-06-17 16:16:56 +0000309 dev->ops->enable_resources(dev);
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700310 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000311 }
Myles Watson7eac4452010-06-17 16:16:56 +0000312
313 for (dev = link->children; dev; dev = dev->sibling) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000314 for (c_link = dev->link_list; c_link; c_link = c_link->next)
Myles Watson7eac4452010-06-17 16:16:56 +0000315 enable_resources(c_link);
Eric Biederman83b991a2003-10-11 06:20:25 +0000316 }
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700317 post_log_clear();
Eric Biederman8ca8d762003-04-22 19:02:15 +0000318}
319
Myles Watson032a9652009-05-11 22:24:53 +0000320/**
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000321 * Reset all of the devices on a bus and clear the bus's reset_needed flag.
322 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000323 * @param bus Pointer to the bus structure.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000324 * @return 1 if the bus was successfully reset, 0 otherwise.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000325 */
326int reset_bus(struct bus *bus)
327{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000328 if (bus && bus->dev && bus->dev->ops && bus->dev->ops->reset_bus) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000329 bus->dev->ops->reset_bus(bus);
330 bus->reset_needed = 0;
331 return 1;
332 }
333 return 0;
334}
335
Myles Watson032a9652009-05-11 22:24:53 +0000336/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000337 * Scan for devices on a bus.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000338 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000339 * If there are bridges on the bus, recursively scan the buses behind the
340 * bridges. If the setting up and tuning of the bus causes a reset to be
341 * required, reset the bus and scan it again.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000342 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000343 * @param busdev Pointer to the bus device.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000344 */
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200345static void scan_bus(struct device *busdev)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000346{
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000347 int do_scan_bus;
Paul Menzel662380f2015-10-20 10:21:08 +0200348 struct stopwatch sw;
Kyösti Mälkkie3d9d672019-12-01 12:27:44 +0200349 long scan_time;
Uwe Hermanne4870472010-11-04 23:23:47 +0000350
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200351 if (!busdev->enabled)
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200352 return;
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200353
Kyösti Mälkkie3d9d672019-12-01 12:27:44 +0200354 printk(BIOS_DEBUG, "%s scanning...\n", dev_path(busdev));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000355
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700356 post_log_path(busdev);
357
Kyösti Mälkkie3d9d672019-12-01 12:27:44 +0200358 stopwatch_init(&sw);
359
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000360 do_scan_bus = 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000361 while (do_scan_bus) {
Myles Watson894a3472010-06-09 22:41:35 +0000362 struct bus *link;
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200363 busdev->ops->scan_bus(busdev);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000364 do_scan_bus = 0;
Myles Watson894a3472010-06-09 22:41:35 +0000365 for (link = busdev->link_list; link; link = link->next) {
366 if (link->reset_needed) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000367 if (reset_bus(link))
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000368 do_scan_bus = 1;
Uwe Hermanne4870472010-11-04 23:23:47 +0000369 else
Myles Watson29cc9ed2009-07-02 18:56:24 +0000370 busdev->bus->reset_needed = 1;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000371 }
372 }
373 }
Paul Menzel662380f2015-10-20 10:21:08 +0200374
Kyösti Mälkkie3d9d672019-12-01 12:27:44 +0200375 scan_time = stopwatch_duration_msecs(&sw);
376 printk(BIOS_DEBUG, "%s: bus %s finished in %ld msecs\n", __func__,
377 dev_path(busdev), scan_time);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000378}
379
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200380void scan_bridges(struct bus *bus)
381{
382 struct device *child;
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200383
384 for (child = bus->children; child; child = child->sibling) {
385 if (!child->ops || !child->ops->scan_bus)
386 continue;
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200387 scan_bus(child);
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200388 }
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200389}
390
Li-Ta Loe5266692004-03-23 21:28:05 +0000391/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000392 * Determine the existence of devices and extend the device tree.
Li-Ta Lo04930692004-11-25 17:37:19 +0000393 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000394 * Most of the devices in the system are listed in the mainboard devicetree.cb
Li-Ta Lo04930692004-11-25 17:37:19 +0000395 * file. The device structures for these devices are generated at compile
396 * time by the config tool and are organized into the device tree. This
397 * function determines if the devices created at compile time actually exist
398 * in the physical system.
399 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000400 * For devices in the physical system but not listed in devicetree.cb,
Li-Ta Lo04930692004-11-25 17:37:19 +0000401 * the device structures have to be created at run time and attached to the
Li-Ta Loe5266692004-03-23 21:28:05 +0000402 * device tree.
403 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000404 * This function starts from the root device 'dev_root', scans the buses in
405 * the system recursively, and modifies the device tree according to the
406 * result of the probe.
Li-Ta Loe5266692004-03-23 21:28:05 +0000407 *
Li-Ta Lo5782d272004-04-26 17:51:20 +0000408 * This function has no idea how to scan and probe buses and devices at all.
409 * It depends on the bus/device specific scan_bus() method to do it. The
Li-Ta Lo04930692004-11-25 17:37:19 +0000410 * scan_bus() method also has to create the device structure and attach
Myles Watson032a9652009-05-11 22:24:53 +0000411 * it to the device tree.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000412 */
413void dev_enumerate(void)
414{
415 struct device *root;
Uwe Hermanne4870472010-11-04 23:23:47 +0000416
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000417 printk(BIOS_INFO, "Enumerating buses...\n");
Uwe Hermanne4870472010-11-04 23:23:47 +0000418
Eric Biederman8ca8d762003-04-22 19:02:15 +0000419 root = &dev_root;
Myles Watsoncd5d7562009-05-12 13:43:34 +0000420
Uwe Hermanne4870472010-11-04 23:23:47 +0000421 show_all_devs(BIOS_SPEW, "Before device enumeration.");
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000422 printk(BIOS_SPEW, "Compare with tree...\n");
Kyösti Mälkki3d3c8c32016-08-15 10:04:21 +0300423 show_devs_tree(root, BIOS_SPEW, 0);
Myles Watsoncd5d7562009-05-12 13:43:34 +0000424
Stefan Reinauera675d492012-08-07 14:50:47 -0700425 if (root->chip_ops && root->chip_ops->enable_dev)
426 root->chip_ops->enable_dev(root);
Uwe Hermanne4870472010-11-04 23:23:47 +0000427
Eric Biedermanb78c1972004-10-14 20:54:17 +0000428 if (!root->ops || !root->ops->scan_bus) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000429 printk(BIOS_ERR, "dev_root missing scan_bus operation");
Eric Biedermanb78c1972004-10-14 20:54:17 +0000430 return;
431 }
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200432 scan_bus(root);
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700433 post_log_clear();
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000434 printk(BIOS_INFO, "done\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000435}
436
Li-Ta Loe5266692004-03-23 21:28:05 +0000437/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000438 * Configure devices on the devices tree.
Myles Watson032a9652009-05-11 22:24:53 +0000439 *
Li-Ta Lo04930692004-11-25 17:37:19 +0000440 * Starting at the root of the device tree, travel it recursively in two
441 * passes. In the first pass, we compute and allocate resources (ranges)
Martin Roth63373ed2013-07-08 16:24:19 -0600442 * required by each device. In the second pass, the resources ranges are
Li-Ta Lo04930692004-11-25 17:37:19 +0000443 * relocated to their final position and stored to the hardware.
Li-Ta Lo5782d272004-04-26 17:51:20 +0000444 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000445 * I/O resources grow upward. MEM resources grow downward.
Li-Ta Lo5782d272004-04-26 17:51:20 +0000446 *
447 * Since the assignment is hierarchical we set the values into the dev_root
Myles Watson032a9652009-05-11 22:24:53 +0000448 * struct.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000449 */
450void dev_configure(void)
451{
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200452 const struct device *root;
Li-Ta Loe5266692004-03-23 21:28:05 +0000453
Marc Jones80bd74c2012-02-21 17:44:35 +0100454 set_vga_bridge_bits();
Marc Jones80bd74c2012-02-21 17:44:35 +0100455
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000456 printk(BIOS_INFO, "Allocating resources...\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000457
Eric Biedermanb78c1972004-10-14 20:54:17 +0000458 root = &dev_root;
Myles Watsoncd5d7562009-05-12 13:43:34 +0000459
Uwe Hermanne4870472010-11-04 23:23:47 +0000460 /*
461 * Each domain should create resources which contain the entire address
Myles Watson29cc9ed2009-07-02 18:56:24 +0000462 * space for IO, MEM, and PREFMEM resources in the domain. The
463 * allocation of device resources will be done from this address space.
464 */
Myles Watsoncd5d7562009-05-12 13:43:34 +0000465
Myles Watson29cc9ed2009-07-02 18:56:24 +0000466 /* Read the resources for the entire tree. */
Li-Ta Lo04930692004-11-25 17:37:19 +0000467
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000468 printk(BIOS_INFO, "Reading resources...\n");
Myles Watson894a3472010-06-09 22:41:35 +0000469 read_resources(root->link_list);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000470 printk(BIOS_INFO, "Done reading resources.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000471
Stefan Reinauer39e72292009-10-26 16:47:05 +0000472 print_resource_tree(root, BIOS_SPEW, "After reading.");
Myles Watsoncd5d7562009-05-12 13:43:34 +0000473
Furquan Shaikh69395742020-05-15 15:43:15 -0700474 allocate_resources(root);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000475
Myles Watson894a3472010-06-09 22:41:35 +0000476 assign_resources(root->link_list);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000477 printk(BIOS_INFO, "Done setting resources.\n");
Stefan Reinauer39e72292009-10-26 16:47:05 +0000478 print_resource_tree(root, BIOS_SPEW, "After assigning values.");
Eric Biederman03acab62004-10-14 21:25:53 +0000479
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000480 printk(BIOS_INFO, "Done allocating resources.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000481}
482
Li-Ta Loe5266692004-03-23 21:28:05 +0000483/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000484 * Enable devices on the device tree.
Li-Ta Loe5266692004-03-23 21:28:05 +0000485 *
486 * Starting at the root, walk the tree and enable all devices/bridges by
487 * calling the device's enable_resources() method.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000488 */
489void dev_enable(void)
490{
Myles Watson7eac4452010-06-17 16:16:56 +0000491 struct bus *link;
492
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000493 printk(BIOS_INFO, "Enabling resources...\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000494
Uwe Hermanne4870472010-11-04 23:23:47 +0000495 /* Now enable everything. */
Myles Watson7eac4452010-06-17 16:16:56 +0000496 for (link = dev_root.link_list; link; link = link->next)
497 enable_resources(link);
Li-Ta Loe5266692004-03-23 21:28:05 +0000498
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000499 printk(BIOS_INFO, "done.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000500}
501
Li-Ta Loe5266692004-03-23 21:28:05 +0000502/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000503 * Initialize a specific device.
Myles Watson7eac4452010-06-17 16:16:56 +0000504 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000505 * The parent should be initialized first to avoid having an ordering problem.
Martin Roth63373ed2013-07-08 16:24:19 -0600506 * This is done by calling the parent's init() method before its children's
Uwe Hermanne4870472010-11-04 23:23:47 +0000507 * init() methods.
Myles Watson7eac4452010-06-17 16:16:56 +0000508 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000509 * @param dev The device to be initialized.
Myles Watson7eac4452010-06-17 16:16:56 +0000510 */
511static void init_dev(struct device *dev)
512{
Uwe Hermanne4870472010-11-04 23:23:47 +0000513 if (!dev->enabled)
Myles Watson7eac4452010-06-17 16:16:56 +0000514 return;
Myles Watson7eac4452010-06-17 16:16:56 +0000515
516 if (!dev->initialized && dev->ops && dev->ops->init) {
Aaron Durbin46ba4802014-09-23 16:34:40 -0500517 struct stopwatch sw;
Kyösti Mälkkie3d9d672019-12-01 12:27:44 +0200518 long init_time;
519
Myles Watson7eac4452010-06-17 16:16:56 +0000520 if (dev->path.type == DEVICE_PATH_I2C) {
521 printk(BIOS_DEBUG, "smbus: %s[%d]->",
522 dev_path(dev->bus->dev), dev->bus->link_num);
523 }
524
Kyösti Mälkkie3d9d672019-12-01 12:27:44 +0200525 printk(BIOS_DEBUG, "%s init\n", dev_path(dev));
526
527 stopwatch_init(&sw);
Myles Watson7eac4452010-06-17 16:16:56 +0000528 dev->initialized = 1;
529 dev->ops->init(dev);
Kyösti Mälkkie3d9d672019-12-01 12:27:44 +0200530
531 init_time = stopwatch_duration_msecs(&sw);
532 printk(BIOS_DEBUG, "%s init finished in %ld msecs\n", dev_path(dev),
533 init_time);
Myles Watson7eac4452010-06-17 16:16:56 +0000534 }
535}
536
537static void init_link(struct bus *link)
538{
539 struct device *dev;
540 struct bus *c_link;
541
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700542 for (dev = link->children; dev; dev = dev->sibling) {
lilacious40cb3fe2023-06-21 23:24:14 +0200543 post_code(POSTCODE_BS_DEV_INIT);
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700544 post_log_path(dev);
Myles Watson7eac4452010-06-17 16:16:56 +0000545 init_dev(dev);
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700546 }
Myles Watson7eac4452010-06-17 16:16:56 +0000547
548 for (dev = link->children; dev; dev = dev->sibling) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000549 for (c_link = dev->link_list; c_link; c_link = c_link->next)
Myles Watson7eac4452010-06-17 16:16:56 +0000550 init_link(c_link);
Myles Watson7eac4452010-06-17 16:16:56 +0000551 }
552}
553
554/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000555 * Initialize all devices in the global device tree.
Myles Watson7eac4452010-06-17 16:16:56 +0000556 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000557 * Starting at the root device, call the device's init() method to do
558 * device-specific setup, then call each child's init() method.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000559 */
560void dev_initialize(void)
561{
Myles Watson7eac4452010-06-17 16:16:56 +0000562 struct bus *link;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000563
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000564 printk(BIOS_INFO, "Initializing devices...\n");
Myles Watson7eac4452010-06-17 16:16:56 +0000565
Myles Watson1bd3fb72010-08-16 16:25:23 +0000566 /* First call the mainboard init. */
567 init_dev(&dev_root);
568
Uwe Hermanne4870472010-11-04 23:23:47 +0000569 /* Now initialize everything. */
Myles Watson7eac4452010-06-17 16:16:56 +0000570 for (link = dev_root.link_list; link; link = link->next)
571 init_link(link);
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700572 post_log_clear();
Myles Watson7eac4452010-06-17 16:16:56 +0000573
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000574 printk(BIOS_INFO, "Devices initialized\n");
Stefan Reinauer39e72292009-10-26 16:47:05 +0000575 show_all_devs(BIOS_SPEW, "After init.");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000576}
Marc Jones2a58ecd2013-10-29 17:32:00 -0600577
578/**
579 * Finalize a specific device.
580 *
581 * The parent should be finalized first to avoid having an ordering problem.
Martin Roth74f18772023-09-03 21:38:29 -0600582 * This is done by calling the parent's final() method before its children's
Marc Jones2a58ecd2013-10-29 17:32:00 -0600583 * final() methods.
584 *
585 * @param dev The device to be initialized.
586 */
587static void final_dev(struct device *dev)
588{
589 if (!dev->enabled)
590 return;
591
592 if (dev->ops && dev->ops->final) {
593 printk(BIOS_DEBUG, "%s final\n", dev_path(dev));
594 dev->ops->final(dev);
595 }
596}
597
598static void final_link(struct bus *link)
599{
600 struct device *dev;
601 struct bus *c_link;
602
603 for (dev = link->children; dev; dev = dev->sibling)
604 final_dev(dev);
605
606 for (dev = link->children; dev; dev = dev->sibling) {
607 for (c_link = dev->link_list; c_link; c_link = c_link->next)
608 final_link(c_link);
609 }
610}
611/**
612 * Finalize all devices in the global device tree.
613 *
614 * Starting at the root device, call the device's final() method to do
615 * device-specific cleanup, then call each child's final() method.
616 */
617void dev_finalize(void)
618{
619 struct bus *link;
620
621 printk(BIOS_INFO, "Finalize devices...\n");
622
623 /* First call the mainboard finalize. */
624 final_dev(&dev_root);
625
626 /* Now finalize everything. */
627 for (link = dev_root.link_list; link; link = link->next)
628 final_link(link);
629
630 printk(BIOS_INFO, "Devices finalized\n");
631}