blob: bf97997f551503ba1c26589a300c1c0d1bae77c7 [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>
Kyösti Mälkki7336f972020-06-08 06:05:03 +030015#if ENV_X86
Duncan Laurieb4aaaa72012-01-17 09:03:11 -080016#include <arch/ebda.h>
17#endif
Aaron Durbin05294292013-04-30 15:41:13 -050018#include <timer.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000019
Li-Ta Loe5266692004-03-23 21:28:05 +000020/** Pointer to the last device */
Myles Watson70679a02010-09-01 21:03:03 +000021extern struct device *last_dev;
Myles Watsonc25cc112010-05-21 14:33:48 +000022/** Linked list of free resources */
23struct resource *free_resources = NULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +000024
Nico Huberacd7d952012-07-25 10:33:05 +020025/**
26 * Initialize all chips of statically known devices.
27 *
28 * Will be called before bus enumeration to initialize chips stated in the
29 * device tree.
30 */
31void dev_initialize_chips(void)
32{
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +020033 const struct device *dev;
Nico Huberacd7d952012-07-25 10:33:05 +020034
35 for (dev = all_devices; dev; dev = dev->next) {
36 /* Initialize chip if we haven't yet. */
37 if (dev->chip_ops && dev->chip_ops->init &&
38 !dev->chip_ops->initialized) {
Duncan Laurie8adf7a22013-06-10 10:34:20 -070039 post_log_path(dev);
Nico Huberacd7d952012-07-25 10:33:05 +020040 dev->chip_ops->init(dev->chip_info);
41 dev->chip_ops->initialized = 1;
42 }
43 }
Duncan Laurie8adf7a22013-06-10 10:34:20 -070044 post_log_clear();
Nico Huberacd7d952012-07-25 10:33:05 +020045}
46
Marc Jones2a58ecd2013-10-29 17:32:00 -060047/**
48 * Finalize all chips of statically known devices.
49 *
50 * This is the last call before calling the payload. This is a good place
51 * to lock registers or other final cleanup.
52 */
53void dev_finalize_chips(void)
54{
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +020055 const struct device *dev;
Marc Jones2a58ecd2013-10-29 17:32:00 -060056
57 for (dev = all_devices; dev; dev = dev->next) {
58 /* Initialize chip if we haven't yet. */
59 if (dev->chip_ops && dev->chip_ops->final &&
60 !dev->chip_ops->finalized) {
61 dev->chip_ops->final(dev->chip_info);
62 dev->chip_ops->finalized = 1;
63 }
64 }
65}
66
Uwe Hermannc1ee4292010-10-17 19:01:48 +000067DECLARE_SPIN_LOCK(dev_lock)
Eric Biederman8ca8d762003-04-22 19:02:15 +000068
Li-Ta Loe5266692004-03-23 21:28:05 +000069/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +000070 * Allocate a new device structure.
Myles Watson032a9652009-05-11 22:24:53 +000071 *
Martin Roth63373ed2013-07-08 16:24:19 -060072 * Allocate a new device structure and attach it to the device tree as a
Li-Ta Lo9f0d0f92004-05-10 16:05:16 +000073 * child of the parent bus.
Li-Ta Loe5266692004-03-23 21:28:05 +000074 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +000075 * @param parent Parent bus the newly created device should be attached to.
76 * @param path Path to the device to be created.
77 * @return Pointer to the newly created device structure.
Li-Ta Loe5266692004-03-23 21:28:05 +000078 *
79 * @see device_path
Eric Biederman8ca8d762003-04-22 19:02:15 +000080 */
Elyes HAOUASd34a7852018-09-17 10:44:14 +020081static struct device *__alloc_dev(struct bus *parent, struct device_path *path)
Eric Biederman8ca8d762003-04-22 19:02:15 +000082{
Elyes HAOUASe3480662018-05-06 20:32:23 +020083 struct device *dev, *child;
Li-Ta Loe5266692004-03-23 21:28:05 +000084
Myles Watson29cc9ed2009-07-02 18:56:24 +000085 /* Find the last child of our parent. */
Elyes HAOUASa342f392018-10-17 10:56:26 +020086 for (child = parent->children; child && child->sibling; /* */)
Eric Biedermane9a271e32003-09-02 03:36:25 +000087 child = child->sibling;
Li-Ta Lo3a812852004-12-03 22:39:34 +000088
Eric Biedermane9a271e32003-09-02 03:36:25 +000089 dev = malloc(sizeof(*dev));
Myles Watson29cc9ed2009-07-02 18:56:24 +000090 if (dev == 0)
Uwe Hermanne4870472010-11-04 23:23:47 +000091 die("alloc_dev(): out of memory.\n");
Myles Watson29cc9ed2009-07-02 18:56:24 +000092
Eric Biedermane9a271e32003-09-02 03:36:25 +000093 memset(dev, 0, sizeof(*dev));
94 memcpy(&dev->path, path, sizeof(*path));
95
Myles Watson29cc9ed2009-07-02 18:56:24 +000096 /* By default devices are enabled. */
Eric Biederman03acab62004-10-14 21:25:53 +000097 dev->enabled = 1;
Eric Biedermane9a271e32003-09-02 03:36:25 +000098
Eric Biedermanb78c1972004-10-14 20:54:17 +000099 /* Add the new device to the list of children of the bus. */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000100 dev->bus = parent;
Uwe Hermanne4870472010-11-04 23:23:47 +0000101 if (child)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000102 child->sibling = dev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000103 else
Eric Biedermane9a271e32003-09-02 03:36:25 +0000104 parent->children = dev;
Li-Ta Loe5266692004-03-23 21:28:05 +0000105
Eric Biederman03acab62004-10-14 21:25:53 +0000106 /* Append a new device to the global device list.
107 * The list is used to find devices once everything is set up.
108 */
Myles Watson70679a02010-09-01 21:03:03 +0000109 last_dev->next = dev;
110 last_dev = dev;
Eric Biederman03acab62004-10-14 21:25:53 +0000111
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300112 return dev;
113}
114
Elyes HAOUASd34a7852018-09-17 10:44:14 +0200115struct device *alloc_dev(struct bus *parent, struct device_path *path)
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300116{
Elyes HAOUASe3480662018-05-06 20:32:23 +0200117 struct device *dev;
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300118 spin_lock(&dev_lock);
119 dev = __alloc_dev(parent, path);
Eric Biederman03acab62004-10-14 21:25:53 +0000120 spin_unlock(&dev_lock);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000121 return dev;
122}
Eric Biederman8ca8d762003-04-22 19:02:15 +0000123
Li-Ta Loe5266692004-03-23 21:28:05 +0000124/**
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300125 * See if a device structure already exists and if not allocate it.
126 *
127 * @param parent The bus to find the device on.
128 * @param path The relative path from the bus to the appropriate device.
129 * @return Pointer to a device structure for the device on bus at path.
130 */
Elyes HAOUASd34a7852018-09-17 10:44:14 +0200131struct device *alloc_find_dev(struct bus *parent, struct device_path *path)
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300132{
Elyes HAOUASe3480662018-05-06 20:32:23 +0200133 struct device *child;
Kyösti Mälkkia5650a42012-07-07 17:15:51 +0300134 spin_lock(&dev_lock);
135 child = find_dev_path(parent, path);
136 if (!child)
137 child = __alloc_dev(parent, path);
138 spin_unlock(&dev_lock);
139 return child;
140}
141
142/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000143 * Read the resources on all devices of a given bus.
144 *
145 * @param bus Bus to read the resources on.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000146 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000147static void read_resources(struct bus *bus)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000148{
149 struct device *curdev;
150
Angel Ponse354a4b2021-05-10 12:06:45 +0200151 printk(BIOS_SPEW, "%s %s bus %d link: %d\n", dev_path(bus->dev),
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000152 __func__, bus->secondary, bus->link_num);
Eric Biederman448bd632004-10-14 22:52:15 +0000153
Myles Watson29cc9ed2009-07-02 18:56:24 +0000154 /* Walk through all devices and find which resources they need. */
155 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
Myles Watson894a3472010-06-09 22:41:35 +0000156 struct bus *link;
Uwe Hermanne4870472010-11-04 23:23:47 +0000157
158 if (!curdev->enabled)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000159 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000160
Eric Biedermane9a271e32003-09-02 03:36:25 +0000161 if (!curdev->ops || !curdev->ops->read_resources) {
Martin Roth7bc74ab2015-11-18 20:23:02 -0700162 if (curdev->path.type != DEVICE_PATH_APIC)
Frans Hendriksa9caa502021-02-01 11:44:37 +0100163 printk(BIOS_ERR, "%s missing %s\n",
164 dev_path(curdev), __func__);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000165 continue;
166 }
Duncan Laurie7ed39762013-07-09 10:46:52 -0700167 post_log_path(curdev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000168 curdev->ops->read_resources(curdev);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000169
170 /* Read in the resources behind the current device's links. */
Myles Watson894a3472010-06-09 22:41:35 +0000171 for (link = curdev->link_list; link; link = link->next)
172 read_resources(link);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000173 }
Duncan Laurie7ed39762013-07-09 10:46:52 -0700174 post_log_clear();
Frans Hendriksa9caa502021-02-01 11:44:37 +0100175 printk(BIOS_SPEW, "%s %s bus %d link: %d done\n",
176 dev_path(bus->dev), __func__, bus->secondary, bus->link_num);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000177}
178
Elyes HAOUASe3480662018-05-06 20:32:23 +0200179struct device *vga_pri = NULL;
Myles Watsonc7233e02009-07-02 19:02:33 +0000180static void set_vga_bridge_bits(void)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000181{
Uwe Hermann312673c2009-10-27 21:49:33 +0000182 /*
Martin Roth63373ed2013-07-08 16:24:19 -0600183 * FIXME: Modify set_vga_bridge() so it is less PCI-centric!
Uwe Hermann312673c2009-10-27 21:49:33 +0000184 * This function knows too much about PCI stuff, it should be just
185 * an iterator/visitor.
186 */
Li-Ta Loe5266692004-03-23 21:28:05 +0000187
Myles Watson29cc9ed2009-07-02 18:56:24 +0000188 /* FIXME: Handle the VGA palette snooping. */
Patrick Georgi557ecf22012-07-20 12:16:17 +0200189 struct device *dev, *vga, *vga_onboard;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000190 struct bus *bus;
Uwe Hermanne4870472010-11-04 23:23:47 +0000191
Eric Biedermanb78c1972004-10-14 20:54:17 +0000192 bus = 0;
193 vga = 0;
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000194 vga_onboard = 0;
Uwe Hermanne4870472010-11-04 23:23:47 +0000195
Patrick Georgi557ecf22012-07-20 12:16:17 +0200196 dev = NULL;
197 while ((dev = dev_find_class(PCI_CLASS_DISPLAY_VGA << 8, dev))) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000198 if (!dev->enabled)
199 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000200
Patrick Georgi557ecf22012-07-20 12:16:17 +0200201 printk(BIOS_DEBUG, "found VGA at %s\n", dev_path(dev));
Nico Huber061b9052019-09-21 15:58:23 +0200202 if (dev->bus->no_vga16) {
203 printk(BIOS_WARNING,
204 "A bridge on the path doesn't support 16-bit VGA decoding!");
205 }
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000206
Frans Hendriksa9caa502021-02-01 11:44:37 +0100207 if (dev->on_mainboard)
Patrick Georgi557ecf22012-07-20 12:16:17 +0200208 vga_onboard = dev;
Frans Hendriksa9caa502021-02-01 11:44:37 +0100209 else
Patrick Georgi557ecf22012-07-20 12:16:17 +0200210 vga = dev;
Myles Watson032a9652009-05-11 22:24:53 +0000211
Patrick Georgi557ecf22012-07-20 12:16:17 +0200212 /* It isn't safe to enable all VGA cards. */
213 dev->command &= ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
Patrick Georgi594473d2012-07-25 08:55:53 +0200214 }
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000215
Uwe Hermanne4870472010-11-04 23:23:47 +0000216 if (!vga)
Myles Watson29cc9ed2009-07-02 18:56:24 +0000217 vga = vga_onboard;
Patrick Georgi557ecf22012-07-20 12:16:17 +0200218
Julius Werner5d1f9a02019-03-07 17:07:26 -0800219 if (CONFIG(ONBOARD_VGA_IS_PRIMARY) && vga_onboard)
Patrick Georgi557ecf22012-07-20 12:16:17 +0200220 vga = vga_onboard;
Myles Watson032a9652009-05-11 22:24:53 +0000221
Patrick Georgi5869fa22012-07-20 12:29:33 +0200222 /* If we prefer plugin VGA over chipset VGA, the chipset might
223 want to know. */
Julius Werner5d1f9a02019-03-07 17:07:26 -0800224 if (!CONFIG(ONBOARD_VGA_IS_PRIMARY) && (vga != vga_onboard) &&
Arthur Heymansb238caa2021-02-22 18:33:08 +0100225 vga_onboard && vga_onboard->ops && vga_onboard->ops->vga_disable) {
Patrick Georgi5869fa22012-07-20 12:29:33 +0200226 printk(BIOS_DEBUG, "Use plugin graphics over integrated.\n");
Arthur Heymansb238caa2021-02-22 18:33:08 +0100227 vga_onboard->ops->vga_disable(vga_onboard);
Patrick Georgi5869fa22012-07-20 12:29:33 +0200228 }
229
arch import user (historical)dc811182005-07-06 17:16:09 +0000230 if (vga) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000231 /* VGA is first add-on card or the only onboard VGA. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000232 printk(BIOS_DEBUG, "Setting up VGA for %s\n", dev_path(vga));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000233 /* All legacy VGA cards have MEM & I/O space registers. */
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000234 vga->command |= (PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
235 vga_pri = vga;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000236 bus = vga->bus;
237 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000238
Myles Watson29cc9ed2009-07-02 18:56:24 +0000239 /* Now walk up the bridges setting the VGA enable. */
240 while (bus) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000241 printk(BIOS_DEBUG, "Setting PCI_BRIDGE_CTL_VGA for bridge %s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000242 dev_path(bus->dev));
Nico Huber061b9052019-09-21 15:58:23 +0200243 bus->bridge_ctrl |= PCI_BRIDGE_CTL_VGA | PCI_BRIDGE_CTL_VGA16;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000244 bus = (bus == bus->dev->bus) ? 0 : bus->dev->bus;
Myles Watson032a9652009-05-11 22:24:53 +0000245 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000246}
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000247
Li-Ta Lo04930692004-11-25 17:37:19 +0000248/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000249 * Assign the computed resources to the devices on the bus.
Li-Ta Lo04930692004-11-25 17:37:19 +0000250 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000251 * Use the device specific set_resources() method to store the computed
Li-Ta Lo04930692004-11-25 17:37:19 +0000252 * resources to hardware. For bridge devices, the set_resources() method
253 * has to recurse into every down stream buses.
254 *
255 * Mutual recursion:
256 * assign_resources() -> device_operation::set_resources()
257 * device_operation::set_resources() -> assign_resources()
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000258 *
259 * @param bus Pointer to the structure for this bus.
Li-Ta Lo04930692004-11-25 17:37:19 +0000260 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000261void assign_resources(struct bus *bus)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000262{
263 struct device *curdev;
264
Frans Hendriksa9caa502021-02-01 11:44:37 +0100265 printk(BIOS_SPEW, "%s %s, bus %d link: %d\n",
266 dev_path(bus->dev), __func__, bus->secondary, bus->link_num);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000267
Myles Watson29cc9ed2009-07-02 18:56:24 +0000268 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000269 if (!curdev->enabled || !curdev->resource_list)
Eric Biederman03acab62004-10-14 21:25:53 +0000270 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000271
Eric Biedermane9a271e32003-09-02 03:36:25 +0000272 if (!curdev->ops || !curdev->ops->set_resources) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000273 printk(BIOS_ERR, "%s missing set_resources\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000274 dev_path(curdev));
Eric Biedermane9a271e32003-09-02 03:36:25 +0000275 continue;
276 }
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700277 post_log_path(curdev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000278 curdev->ops->set_resources(curdev);
279 }
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700280 post_log_clear();
Frans Hendriks739c0802021-02-01 11:52:51 +0100281 printk(BIOS_SPEW, "%s %s, bus %d link: %d done\n",
Frans Hendriksa9caa502021-02-01 11:44:37 +0100282 dev_path(bus->dev), __func__, bus->secondary, bus->link_num);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000283}
284
Li-Ta Lo5782d272004-04-26 17:51:20 +0000285/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000286 * Enable the resources for devices on a link.
Li-Ta Lo5782d272004-04-26 17:51:20 +0000287 *
288 * Enable resources of the device by calling the device specific
289 * enable_resources() method.
290 *
291 * The parent's resources should be enabled first to avoid having enabling
292 * order problem. This is done by calling the parent's enable_resources()
Martin Roth63373ed2013-07-08 16:24:19 -0600293 * method before its children's enable_resources() methods.
Li-Ta Lo9f0d0f92004-05-10 16:05:16 +0000294 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000295 * @param link The link whose devices' resources are to be enabled.
Li-Ta Lo5782d272004-04-26 17:51:20 +0000296 */
Myles Watson7eac4452010-06-17 16:16:56 +0000297static void enable_resources(struct bus *link)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000298{
Myles Watson7eac4452010-06-17 16:16:56 +0000299 struct device *dev;
300 struct bus *c_link;
301
302 for (dev = link->children; dev; dev = dev->sibling) {
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700303 if (dev->enabled && dev->ops && dev->ops->enable_resources) {
304 post_log_path(dev);
Myles Watson7eac4452010-06-17 16:16:56 +0000305 dev->ops->enable_resources(dev);
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700306 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000307 }
Myles Watson7eac4452010-06-17 16:16:56 +0000308
309 for (dev = link->children; dev; dev = dev->sibling) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000310 for (c_link = dev->link_list; c_link; c_link = c_link->next)
Myles Watson7eac4452010-06-17 16:16:56 +0000311 enable_resources(c_link);
Eric Biederman83b991a2003-10-11 06:20:25 +0000312 }
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700313 post_log_clear();
Eric Biederman8ca8d762003-04-22 19:02:15 +0000314}
315
Myles Watson032a9652009-05-11 22:24:53 +0000316/**
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000317 * Reset all of the devices on a bus and clear the bus's reset_needed flag.
318 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000319 * @param bus Pointer to the bus structure.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000320 * @return 1 if the bus was successfully reset, 0 otherwise.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000321 */
322int reset_bus(struct bus *bus)
323{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000324 if (bus && bus->dev && bus->dev->ops && bus->dev->ops->reset_bus) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000325 bus->dev->ops->reset_bus(bus);
326 bus->reset_needed = 0;
327 return 1;
328 }
329 return 0;
330}
331
Myles Watson032a9652009-05-11 22:24:53 +0000332/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000333 * Scan for devices on a bus.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000334 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000335 * If there are bridges on the bus, recursively scan the buses behind the
336 * bridges. If the setting up and tuning of the bus causes a reset to be
337 * required, reset the bus and scan it again.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000338 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000339 * @param busdev Pointer to the bus device.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000340 */
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200341static void scan_bus(struct device *busdev)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000342{
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000343 int do_scan_bus;
Paul Menzel662380f2015-10-20 10:21:08 +0200344 struct stopwatch sw;
Kyösti Mälkkie3d9d672019-12-01 12:27:44 +0200345 long scan_time;
Uwe Hermanne4870472010-11-04 23:23:47 +0000346
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200347 if (!busdev->enabled)
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200348 return;
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200349
Kyösti Mälkkie3d9d672019-12-01 12:27:44 +0200350 printk(BIOS_DEBUG, "%s scanning...\n", dev_path(busdev));
Myles Watson29cc9ed2009-07-02 18:56:24 +0000351
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700352 post_log_path(busdev);
353
Kyösti Mälkkie3d9d672019-12-01 12:27:44 +0200354 stopwatch_init(&sw);
355
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000356 do_scan_bus = 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000357 while (do_scan_bus) {
Myles Watson894a3472010-06-09 22:41:35 +0000358 struct bus *link;
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200359 busdev->ops->scan_bus(busdev);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000360 do_scan_bus = 0;
Myles Watson894a3472010-06-09 22:41:35 +0000361 for (link = busdev->link_list; link; link = link->next) {
362 if (link->reset_needed) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000363 if (reset_bus(link))
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000364 do_scan_bus = 1;
Uwe Hermanne4870472010-11-04 23:23:47 +0000365 else
Myles Watson29cc9ed2009-07-02 18:56:24 +0000366 busdev->bus->reset_needed = 1;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000367 }
368 }
369 }
Paul Menzel662380f2015-10-20 10:21:08 +0200370
Kyösti Mälkkie3d9d672019-12-01 12:27:44 +0200371 scan_time = stopwatch_duration_msecs(&sw);
372 printk(BIOS_DEBUG, "%s: bus %s finished in %ld msecs\n", __func__,
373 dev_path(busdev), scan_time);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000374}
375
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200376void scan_bridges(struct bus *bus)
377{
378 struct device *child;
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200379
380 for (child = bus->children; child; child = child->sibling) {
381 if (!child->ops || !child->ops->scan_bus)
382 continue;
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200383 scan_bus(child);
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200384 }
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +0200385}
386
Li-Ta Loe5266692004-03-23 21:28:05 +0000387/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000388 * Determine the existence of devices and extend the device tree.
Li-Ta Lo04930692004-11-25 17:37:19 +0000389 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000390 * Most of the devices in the system are listed in the mainboard devicetree.cb
Li-Ta Lo04930692004-11-25 17:37:19 +0000391 * file. The device structures for these devices are generated at compile
392 * time by the config tool and are organized into the device tree. This
393 * function determines if the devices created at compile time actually exist
394 * in the physical system.
395 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000396 * For devices in the physical system but not listed in devicetree.cb,
Li-Ta Lo04930692004-11-25 17:37:19 +0000397 * the device structures have to be created at run time and attached to the
Li-Ta Loe5266692004-03-23 21:28:05 +0000398 * device tree.
399 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000400 * This function starts from the root device 'dev_root', scans the buses in
401 * the system recursively, and modifies the device tree according to the
402 * result of the probe.
Li-Ta Loe5266692004-03-23 21:28:05 +0000403 *
Li-Ta Lo5782d272004-04-26 17:51:20 +0000404 * This function has no idea how to scan and probe buses and devices at all.
405 * It depends on the bus/device specific scan_bus() method to do it. The
Li-Ta Lo04930692004-11-25 17:37:19 +0000406 * scan_bus() method also has to create the device structure and attach
Myles Watson032a9652009-05-11 22:24:53 +0000407 * it to the device tree.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000408 */
409void dev_enumerate(void)
410{
411 struct device *root;
Uwe Hermanne4870472010-11-04 23:23:47 +0000412
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000413 printk(BIOS_INFO, "Enumerating buses...\n");
Uwe Hermanne4870472010-11-04 23:23:47 +0000414
Eric Biederman8ca8d762003-04-22 19:02:15 +0000415 root = &dev_root;
Myles Watsoncd5d7562009-05-12 13:43:34 +0000416
Uwe Hermanne4870472010-11-04 23:23:47 +0000417 show_all_devs(BIOS_SPEW, "Before device enumeration.");
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000418 printk(BIOS_SPEW, "Compare with tree...\n");
Kyösti Mälkki3d3c8c32016-08-15 10:04:21 +0300419 show_devs_tree(root, BIOS_SPEW, 0);
Myles Watsoncd5d7562009-05-12 13:43:34 +0000420
Stefan Reinauera675d492012-08-07 14:50:47 -0700421 if (root->chip_ops && root->chip_ops->enable_dev)
422 root->chip_ops->enable_dev(root);
Uwe Hermanne4870472010-11-04 23:23:47 +0000423
Eric Biedermanb78c1972004-10-14 20:54:17 +0000424 if (!root->ops || !root->ops->scan_bus) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000425 printk(BIOS_ERR, "dev_root missing scan_bus operation");
Eric Biedermanb78c1972004-10-14 20:54:17 +0000426 return;
427 }
Kyösti Mälkki580e7222015-03-19 21:04:23 +0200428 scan_bus(root);
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700429 post_log_clear();
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000430 printk(BIOS_INFO, "done\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000431}
432
Li-Ta Loe5266692004-03-23 21:28:05 +0000433/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000434 * Configure devices on the devices tree.
Myles Watson032a9652009-05-11 22:24:53 +0000435 *
Li-Ta Lo04930692004-11-25 17:37:19 +0000436 * Starting at the root of the device tree, travel it recursively in two
437 * passes. In the first pass, we compute and allocate resources (ranges)
Martin Roth63373ed2013-07-08 16:24:19 -0600438 * required by each device. In the second pass, the resources ranges are
Li-Ta Lo04930692004-11-25 17:37:19 +0000439 * relocated to their final position and stored to the hardware.
Li-Ta Lo5782d272004-04-26 17:51:20 +0000440 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000441 * I/O resources grow upward. MEM resources grow downward.
Li-Ta Lo5782d272004-04-26 17:51:20 +0000442 *
443 * Since the assignment is hierarchical we set the values into the dev_root
Myles Watson032a9652009-05-11 22:24:53 +0000444 * struct.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000445 */
446void dev_configure(void)
447{
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200448 const struct device *root;
Li-Ta Loe5266692004-03-23 21:28:05 +0000449
Marc Jones80bd74c2012-02-21 17:44:35 +0100450 set_vga_bridge_bits();
Marc Jones80bd74c2012-02-21 17:44:35 +0100451
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000452 printk(BIOS_INFO, "Allocating resources...\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000453
Eric Biedermanb78c1972004-10-14 20:54:17 +0000454 root = &dev_root;
Myles Watsoncd5d7562009-05-12 13:43:34 +0000455
Uwe Hermanne4870472010-11-04 23:23:47 +0000456 /*
457 * Each domain should create resources which contain the entire address
Myles Watson29cc9ed2009-07-02 18:56:24 +0000458 * space for IO, MEM, and PREFMEM resources in the domain. The
459 * allocation of device resources will be done from this address space.
460 */
Myles Watsoncd5d7562009-05-12 13:43:34 +0000461
Myles Watson29cc9ed2009-07-02 18:56:24 +0000462 /* Read the resources for the entire tree. */
Li-Ta Lo04930692004-11-25 17:37:19 +0000463
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000464 printk(BIOS_INFO, "Reading resources...\n");
Myles Watson894a3472010-06-09 22:41:35 +0000465 read_resources(root->link_list);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000466 printk(BIOS_INFO, "Done reading resources.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000467
Stefan Reinauer39e72292009-10-26 16:47:05 +0000468 print_resource_tree(root, BIOS_SPEW, "After reading.");
Myles Watsoncd5d7562009-05-12 13:43:34 +0000469
Furquan Shaikh69395742020-05-15 15:43:15 -0700470 allocate_resources(root);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000471
Myles Watson894a3472010-06-09 22:41:35 +0000472 assign_resources(root->link_list);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000473 printk(BIOS_INFO, "Done setting resources.\n");
Stefan Reinauer39e72292009-10-26 16:47:05 +0000474 print_resource_tree(root, BIOS_SPEW, "After assigning values.");
Eric Biederman03acab62004-10-14 21:25:53 +0000475
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000476 printk(BIOS_INFO, "Done allocating resources.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000477}
478
Li-Ta Loe5266692004-03-23 21:28:05 +0000479/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000480 * Enable devices on the device tree.
Li-Ta Loe5266692004-03-23 21:28:05 +0000481 *
482 * Starting at the root, walk the tree and enable all devices/bridges by
483 * calling the device's enable_resources() method.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000484 */
485void dev_enable(void)
486{
Myles Watson7eac4452010-06-17 16:16:56 +0000487 struct bus *link;
488
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000489 printk(BIOS_INFO, "Enabling resources...\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000490
Uwe Hermanne4870472010-11-04 23:23:47 +0000491 /* Now enable everything. */
Myles Watson7eac4452010-06-17 16:16:56 +0000492 for (link = dev_root.link_list; link; link = link->next)
493 enable_resources(link);
Li-Ta Loe5266692004-03-23 21:28:05 +0000494
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000495 printk(BIOS_INFO, "done.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000496}
497
Li-Ta Loe5266692004-03-23 21:28:05 +0000498/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000499 * Initialize a specific device.
Myles Watson7eac4452010-06-17 16:16:56 +0000500 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000501 * The parent should be initialized first to avoid having an ordering problem.
Martin Roth63373ed2013-07-08 16:24:19 -0600502 * This is done by calling the parent's init() method before its children's
Uwe Hermanne4870472010-11-04 23:23:47 +0000503 * init() methods.
Myles Watson7eac4452010-06-17 16:16:56 +0000504 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000505 * @param dev The device to be initialized.
Myles Watson7eac4452010-06-17 16:16:56 +0000506 */
507static void init_dev(struct device *dev)
508{
Uwe Hermanne4870472010-11-04 23:23:47 +0000509 if (!dev->enabled)
Myles Watson7eac4452010-06-17 16:16:56 +0000510 return;
Myles Watson7eac4452010-06-17 16:16:56 +0000511
512 if (!dev->initialized && dev->ops && dev->ops->init) {
Aaron Durbin46ba4802014-09-23 16:34:40 -0500513 struct stopwatch sw;
Kyösti Mälkkie3d9d672019-12-01 12:27:44 +0200514 long init_time;
515
Myles Watson7eac4452010-06-17 16:16:56 +0000516 if (dev->path.type == DEVICE_PATH_I2C) {
517 printk(BIOS_DEBUG, "smbus: %s[%d]->",
518 dev_path(dev->bus->dev), dev->bus->link_num);
519 }
520
Kyösti Mälkkie3d9d672019-12-01 12:27:44 +0200521 printk(BIOS_DEBUG, "%s init\n", dev_path(dev));
522
523 stopwatch_init(&sw);
Myles Watson7eac4452010-06-17 16:16:56 +0000524 dev->initialized = 1;
525 dev->ops->init(dev);
Kyösti Mälkkie3d9d672019-12-01 12:27:44 +0200526
527 init_time = stopwatch_duration_msecs(&sw);
528 printk(BIOS_DEBUG, "%s init finished in %ld msecs\n", dev_path(dev),
529 init_time);
Myles Watson7eac4452010-06-17 16:16:56 +0000530 }
531}
532
533static void init_link(struct bus *link)
534{
535 struct device *dev;
536 struct bus *c_link;
537
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700538 for (dev = link->children; dev; dev = dev->sibling) {
Duncan Lauriecb73a842013-06-10 10:41:04 -0700539 post_code(POST_BS_DEV_INIT);
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700540 post_log_path(dev);
Myles Watson7eac4452010-06-17 16:16:56 +0000541 init_dev(dev);
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700542 }
Myles Watson7eac4452010-06-17 16:16:56 +0000543
544 for (dev = link->children; dev; dev = dev->sibling) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000545 for (c_link = dev->link_list; c_link; c_link = c_link->next)
Myles Watson7eac4452010-06-17 16:16:56 +0000546 init_link(c_link);
Myles Watson7eac4452010-06-17 16:16:56 +0000547 }
548}
549
550/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000551 * Initialize all devices in the global device tree.
Myles Watson7eac4452010-06-17 16:16:56 +0000552 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000553 * Starting at the root device, call the device's init() method to do
554 * device-specific setup, then call each child's init() method.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000555 */
556void dev_initialize(void)
557{
Myles Watson7eac4452010-06-17 16:16:56 +0000558 struct bus *link;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000559
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000560 printk(BIOS_INFO, "Initializing devices...\n");
Myles Watson7eac4452010-06-17 16:16:56 +0000561
Kyösti Mälkki7336f972020-06-08 06:05:03 +0300562#if ENV_X86
Arthur Heymans8b7cd432019-10-26 20:31:41 +0200563 /* Ensure EBDA is prepared before Option ROMs. */
564 setup_default_ebda();
Duncan Laurieb4aaaa72012-01-17 09:03:11 -0800565#endif
566
Myles Watson1bd3fb72010-08-16 16:25:23 +0000567 /* First call the mainboard init. */
568 init_dev(&dev_root);
569
Uwe Hermanne4870472010-11-04 23:23:47 +0000570 /* Now initialize everything. */
Myles Watson7eac4452010-06-17 16:16:56 +0000571 for (link = dev_root.link_list; link; link = link->next)
572 init_link(link);
Duncan Laurie8adf7a22013-06-10 10:34:20 -0700573 post_log_clear();
Myles Watson7eac4452010-06-17 16:16:56 +0000574
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000575 printk(BIOS_INFO, "Devices initialized\n");
Stefan Reinauer39e72292009-10-26 16:47:05 +0000576 show_all_devs(BIOS_SPEW, "After init.");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000577}
Marc Jones2a58ecd2013-10-29 17:32:00 -0600578
579/**
580 * Finalize a specific device.
581 *
582 * The parent should be finalized first to avoid having an ordering problem.
583 * This is done by calling the parent's final() method before its childrens'
584 * final() methods.
585 *
586 * @param dev The device to be initialized.
587 */
588static void final_dev(struct device *dev)
589{
590 if (!dev->enabled)
591 return;
592
593 if (dev->ops && dev->ops->final) {
594 printk(BIOS_DEBUG, "%s final\n", dev_path(dev));
595 dev->ops->final(dev);
596 }
597}
598
599static void final_link(struct bus *link)
600{
601 struct device *dev;
602 struct bus *c_link;
603
604 for (dev = link->children; dev; dev = dev->sibling)
605 final_dev(dev);
606
607 for (dev = link->children; dev; dev = dev->sibling) {
608 for (c_link = dev->link_list; c_link; c_link = c_link->next)
609 final_link(c_link);
610 }
611}
612/**
613 * Finalize all devices in the global device tree.
614 *
615 * Starting at the root device, call the device's final() method to do
616 * device-specific cleanup, then call each child's final() method.
617 */
618void dev_finalize(void)
619{
620 struct bus *link;
621
622 printk(BIOS_INFO, "Finalize devices...\n");
623
624 /* First call the mainboard finalize. */
625 final_dev(&dev_root);
626
627 /* Now finalize everything. */
628 for (link = dev_root.link_list; link; link = link->next)
629 final_link(link);
630
631 printk(BIOS_INFO, "Devices finalized\n");
632}