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