09582fccf252f3e87a4d667c411e333d5afcec29
[coreboot.git] / src / devices / root_device.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2003-2004 Linux Networx
5  * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
6  * Copyright (C) 2003 Ronald G. Minnich <rminnich@gmail.com>
7  * Copyright (C) 2004-2005 Li-Ta Lo <ollie@lanl.gov>
8  * Copyright (C) 2005 Tyan
9  * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan)
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; version 2 of the License.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
23  */
24
25 #include <console/console.h>
26 #include <device/device.h>
27 #include <device/pci.h>
28 #include <reset.h>
29
30 /** 
31  * Read the resources for the root device,
32  * that encompass the resources for the entire system.
33  * @param root Pointer to the device structure for the system root device
34  */
35 void root_dev_read_resources(device_t root)
36 {
37         printk_err("%s should never be called.\n", __func__);
38 }
39
40 /**
41  * @brief Write the resources for every device
42  *
43  * Write the resources for the root device,
44  * and every device under it which are all of the devices.
45  * @param root Pointer to the device structure for the system root device
46  */
47 void root_dev_set_resources(device_t root)
48 {
49         printk_err("%s should never be called.\n", __func__);
50 }
51
52 /**
53  * @brief Scan devices on static buses.
54  *
55  * The enumeration of certain buses is purely static. The existence of
56  * devices on those buses can be completely determined at compile time
57  * and is specified in the config file. Typical examples are the 'PNP' 
58  * devices on a legacy ISA/LPC bus. There is no need of probing of any kind, 
59  * the only thing we have to do is to walk through the bus and 
60  * enable or disable devices as indicated in the config file.
61  *
62  * On the other hand, some devices are virtual and their existence is
63  * artificial. They can not be probed at run time. One example is the
64  * debug device. Those virtual devices have to be listed in the config
65  * file under some static bus in order to be enumerated at run time.
66  *
67  * This function is the default scan_bus() method for the root device and
68  * LPC bridges.
69  *
70  * @param bus Pointer to the device structure which the static buses are attached
71  * @param max  Maximum bus number currently used before scanning.
72  * @return Largest bus number used.
73  */
74 static int smbus_max = 0;
75 unsigned int scan_static_bus(device_t bus, unsigned int max)
76 {
77         device_t child;
78         unsigned link;
79
80         printk_spew("%s for %s\n", __func__, dev_path(bus));
81
82         for(link = 0; link < bus->links; link++) {
83                 /* for smbus bus enumerate */
84                 child = bus->link[link].children;
85                 if(child && child->path.type == DEVICE_PATH_I2C) {
86                         bus->link[link].secondary = ++smbus_max;
87                 }
88                 for(child = bus->link[link].children; child; child = child->sibling) {
89                         if (child->chip_ops && child->chip_ops->enable_dev) {
90                                 child->chip_ops->enable_dev(child);
91                         }
92                         if (child->ops && child->ops->enable) {
93                                 child->ops->enable(child);
94                         }
95                         if (child->path.type == DEVICE_PATH_I2C) {
96                                 printk_debug("smbus: %s[%d]->",  
97                                         dev_path(child->bus->dev), child->bus->link );
98                         }
99                         printk_debug("%s %s\n",
100                                 dev_path(child),
101                                 child->enabled?"enabled": "disabled");
102                 }
103         }
104         for(link = 0; link < bus->links; link++) {
105                 for(child = bus->link[link].children; child; child = child->sibling) {
106                         if (!child->ops || !child->ops->scan_bus)
107                                 continue;
108                         printk_spew("%s scanning...\n", dev_path(child));
109                         max = scan_bus(child, max);
110                 }
111         }
112
113         printk_spew("%s for %s done\n", __func__, dev_path(bus));
114
115         return max;
116 }
117
118 /**
119  * @brief Enable resources for children devices
120  *
121  * @param dev the device whos children's resources are to be enabled
122  *
123  * This function is called by the global enable_resource() indirectly via the
124  * device_operation::enable_resources() method of devices.
125  *
126  * Indirect mutual recursion:
127  *      enable_childrens_resources() -> enable_resources()
128  *      enable_resources() -> device_operation::enable_resources()
129  *      device_operation::enable_resources() -> enable_children_resources()
130  */
131 void enable_childrens_resources(device_t dev)
132 {
133         unsigned link;
134         for(link = 0; link < dev->links; link++) {
135                 device_t child;
136                 for(child = dev->link[link].children; child; child = child->sibling) {
137                         enable_resources(child);
138                 }
139         }
140 }
141
142 void root_dev_enable_resources(device_t dev)
143 {
144         enable_childrens_resources(dev);
145 }
146
147 /**
148  * @brief Scan root bus for generic systems
149  *
150  * @param root The root device structure
151  * @param max The current bus number scanned so far, usually 0x00
152  *
153  * This function is the default scan_bus() method of the root device.
154  */
155 unsigned int root_dev_scan_bus(device_t root, unsigned int max)
156 {
157         return scan_static_bus(root, max);
158 }
159
160 void root_dev_init(device_t root)
161 {
162 }
163
164 void root_dev_reset(struct bus *bus)
165 {
166         printk_info("Reseting board...\n");
167         hard_reset();
168 }
169
170 /**
171  * @brief Default device operation for root device
172  *
173  * This is the default device operation for root devices. These operations
174  * should be fully usable as is.  However the chip_operations::enable_dev()
175  * of a motherboard can override this if you want non-default behavior.
176  */
177 struct device_operations default_dev_ops_root = {
178         .read_resources   = root_dev_read_resources,
179         .set_resources    = root_dev_set_resources,
180         .enable_resources = root_dev_enable_resources,
181         .init             = root_dev_init,
182         .scan_bus         = root_dev_scan_bus,
183         .reset_bus        = root_dev_reset,
184 };