7b6929ec5b565d45a16d1295b8f9fdf7fb62b2b0
[coreboot.git] / src / devices / root_device.c
1 /*
2  * This file is part of the LinuxBIOS 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 <part/hard_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         struct resource *resource;
38
39         /* Initialize the system wide io space constraints */
40         resource = new_resource(root, 0);
41         resource->base  = 0x400;
42         resource->size  = 0;
43         resource->align = 0;
44         resource->gran  = 0;
45         resource->limit = 0xffffUL;
46         resource->flags = IORESOURCE_IO;
47         compute_allocate_resource(&root->link[0], resource, 
48                 IORESOURCE_IO, IORESOURCE_IO);
49
50         /* Initialize the system wide memory resources constraints */
51         resource = new_resource(root, 1);
52         resource->base  = 0;
53         resource->size  = 0;
54         resource->align = 0;
55         resource->gran  = 0;
56         resource->limit = 0xffffffffUL;
57         resource->flags = IORESOURCE_MEM;
58         compute_allocate_resource(&root->link[0], resource,
59                 IORESOURCE_MEM, IORESOURCE_MEM);
60 }
61
62 /**
63  * @brief Write the resources for every device
64  *
65  * Write the resources for the root device,
66  * and every device under it which are all of the devices.
67  * @param root Pointer to the device structure for the system root device
68  */
69 void root_dev_set_resources(device_t root)
70 {
71         struct bus *bus;
72
73         bus = &root->link[0];
74         compute_allocate_resource(bus,
75                 &root->resource[0], IORESOURCE_IO, IORESOURCE_IO);
76         compute_allocate_resource(bus, 
77                 &root->resource[1], IORESOURCE_MEM, IORESOURCE_MEM);
78         assign_resources(bus);
79 }
80
81 /**
82  * @brief Scan devices on static buses.
83  *
84  * The enumeration of certain buses is purely static. The existence of
85  * devices on those buses can be completely determined at compile time
86  * and is specified in the config file. Typical examples are the 'PNP' 
87  * devices on a legacy ISA/LPC bus. There is no need of probing of any kind, 
88  * the only thing we have to do is to walk through the bus and 
89  * enable or disable devices as indicated in the config file.
90  *
91  * On the other hand, some devices are virtual and their existence is
92  * artificial. They can not be probed at run time. One example is the
93  * debug device. Those virtual devices have to be listed in the config
94  * file under some static bus in order to be enumerated at run time.
95  *
96  * This function is the default scan_bus() method for the root device and
97  * LPC bridges.
98  *
99  * @param bus Pointer to the device structure which the static buses are attached
100  * @param max  Maximum bus number currently used before scanning.
101  * @return Largest bus number used.
102  */
103 static int smbus_max = 0;
104 unsigned int scan_static_bus(device_t bus, unsigned int max)
105 {
106         device_t child;
107         unsigned link;
108
109         printk_spew("%s for %s\n", __func__, dev_path(bus));
110
111         for(link = 0; link < bus->links; link++) {
112                 /* for smbus bus enumerate */
113                 child = bus->link[link].children;
114                 if(child && child->path.type == DEVICE_PATH_I2C) {
115                         bus->link[link].secondary = ++smbus_max;
116                 }
117                 for(child = bus->link[link].children; child; child = child->sibling) {
118                         if (child->chip_ops && child->chip_ops->enable_dev) {
119                                 child->chip_ops->enable_dev(child);
120                         }
121                         if (child->ops && child->ops->enable) {
122                                 child->ops->enable(child);
123                         }
124                         if (child->path.type == DEVICE_PATH_I2C) {
125                                 printk_debug("smbus: %s[%d]->",  
126                                         dev_path(child->bus->dev), child->bus->link );
127                         }
128                         printk_debug("%s %s\n",
129                                 dev_path(child),
130                                 child->enabled?"enabled": "disabled");
131                 }
132         }
133         for(link = 0; link < bus->links; link++) {
134                 for(child = bus->link[link].children; child; child = child->sibling) {
135                         if (!child->ops || !child->ops->scan_bus)
136                                 continue;
137                         printk_spew("%s scanning...\n", dev_path(child));
138                         max = scan_bus(child, max);
139                 }
140         }
141
142         printk_spew("%s for %s done\n", __func__, dev_path(bus));
143
144         return max;
145 }
146
147 /**
148  * @brief Enable resources for children devices
149  *
150  * @param dev the device whos children's resources are to be enabled
151  *
152  * This function is called by the global enable_resource() indirectly via the
153  * device_operation::enable_resources() method of devices.
154  *
155  * Indirect mutual recursion:
156  *      enable_childrens_resources() -> enable_resources()
157  *      enable_resources() -> device_operation::enable_resources()
158  *      device_operation::enable_resources() -> enable_children_resources()
159  */
160 void enable_childrens_resources(device_t dev)
161 {
162         unsigned link;
163         for(link = 0; link < dev->links; link++) {
164                 device_t child;
165                 for(child = dev->link[link].children; child; child = child->sibling) {
166                         enable_resources(child);
167                 }
168         }
169 }
170
171 void root_dev_enable_resources(device_t dev)
172 {
173         enable_childrens_resources(dev);
174 }
175
176 /**
177  * @brief Scan root bus for generic systems
178  *
179  * @param root The root device structure
180  * @param max The current bus number scanned so far, usually 0x00
181  *
182  * This function is the default scan_bus() method of the root device.
183  */
184 unsigned int root_dev_scan_bus(device_t root, unsigned int max)
185 {
186         return scan_static_bus(root, max);
187 }
188
189 void root_dev_init(device_t root)
190 {
191 }
192
193 void root_dev_reset(struct bus *bus)
194 {
195         printk_info("Reseting board...\n");
196         hard_reset();
197 }
198
199 /**
200  * @brief Default device operation for root device
201  *
202  * This is the default device operation for root devices. These operations
203  * should be fully usable as is.  However the chip_operations::enable_dev()
204  * of a motherboard can override this if you want non-default behavior.
205  */
206 struct device_operations default_dev_ops_root = {
207         .read_resources   = root_dev_read_resources,
208         .set_resources    = root_dev_set_resources,
209         .enable_resources = root_dev_enable_resources,
210         .init             = root_dev_init,
211         .scan_bus         = root_dev_scan_bus,
212         .reset_bus        = root_dev_reset,
213 };
214
215 /**
216  * @brief The root of device tree.
217  *
218  * This is the root of the device tree. The device tree is defined in the
219  * static.c file and is generated by config tool during compile time.
220  */
221 extern struct device dev_root;