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