ae022773631586b098b39cdbc7bc9c4cb5ca7c3b
[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 encompase 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         int res = 0;
13
14         printk_spew("%s . Root is %p\n", __FUNCTION__, root);
15         /* Initialize the system wide io space constraints */
16         root->resource[res].base  = 0x400;
17         root->resource[res].size  = 0;
18         root->resource[res].align = 0;
19         root->resource[res].gran  = 0;
20         root->resource[res].limit = 0xffffUL;
21         root->resource[res].flags = IORESOURCE_IO;
22         root->resource[res].index = 0;
23         printk_spew("%s . link %p, resource %p\n", __FUNCTION__, 
24                                         &root->link[0], &root->resource[res]);
25         compute_allocate_resource(&root->link[0], &root->resource[res], 
26                 IORESOURCE_IO, IORESOURCE_IO);
27         res++;
28
29         /* Initialize the system wide memory resources constraints */
30         root->resource[res].base  = 0;
31         root->resource[res].size  = 0;
32         root->resource[res].align = 0;
33         root->resource[res].gran  = 0;
34         root->resource[res].limit = 0xffffffffUL;
35         root->resource[res].flags = IORESOURCE_MEM;
36         root->resource[res].index = 1;
37         printk_spew("%s . link %p, resource %p\n", __FUNCTION__, 
38                                 &root->link[0], &root->resource[res]);
39         compute_allocate_resource(&root->link[0], &root->resource[res], 
40                 IORESOURCE_MEM, IORESOURCE_MEM);
41         res++;
42
43         root->resources = res;
44         printk_spew("%s DONE\n", __FUNCTION__);
45 }
46
47 /**
48  * Write the resources for the root device,
49  * and every device under it which are all of the devices.
50  * @param root Pointer to the device structure for the system root device
51  */
52 void root_dev_set_resources(device_t root)
53 {
54         struct bus *bus;
55         bus = &root->link[0];
56         compute_allocate_resource(bus,
57                 &root->resource[0], IORESOURCE_IO, IORESOURCE_IO);
58         compute_allocate_resource(bus, 
59                 &root->resource[1], IORESOURCE_MEM, IORESOURCE_MEM);
60         assign_resources(bus);
61 }
62
63 /**
64  * Walk through devices on the motherboard and scan for devices behind
65  * them.
66  * @param root Pointer to the device structure for the system root device
67  * @param max  Maximum bus number allowed in the system.
68  * @return Largest bus number used.
69  */
70 unsigned int walk_static_devices(device_t bus, unsigned int max)
71 {
72         device_t child;
73         unsigned link;
74         for(link = 0; link < bus->links; link++) {
75                 for(child = bus->link[link].children; child; child = child->sibling) {
76                         if (child->ops && child->ops->enable) {
77                                 child->ops->enable(child);
78                         }
79                         printk_debug("%s %s\n",
80                                 dev_path(child),
81                                 child->enable?"enabled": "disabled");
82                 }
83         }
84         for(link = 0; link < bus->links; link++) {
85                 for(child = bus->link[link].children; child; child = child->sibling) {
86                         if (!child->ops || !child->ops->scan_bus)
87                                 continue;
88                         printk_debug("%s scanning...\n", dev_path(child));
89                         max = child->ops->scan_bus(child, max);
90                 }
91         }
92         return max;
93 }
94
95 void enable_childrens_resources(device_t dev)
96 {
97         unsigned link;
98         for(link = 0; link < dev->links; link++) {
99                 device_t child;
100                 for(child = dev->link[link].children; child; child = child->sibling) {
101                         enable_resources(child);
102                 }
103         }
104 }
105
106 unsigned int root_dev_scan_pci_bus(device_t root, unsigned int max)
107 {
108         return pci_scan_bus(&root->link[0], 0, 0xff, max);
109 }
110
111 struct device_operations default_dev_ops_root = {
112         .read_resources   = root_dev_read_resources,
113         .set_resources    = root_dev_set_resources,
114         .enable_resources = enable_childrens_resources,
115         .init             = 0,
116         .scan_bus         = root_dev_scan_pci_bus,
117 };
118
119 /**
120  * This is the root of the device tree. A PCI tree always has 
121  * one bus, bus 0. Bus 0 contains devices and bridges. 
122  */
123 struct device dev_root = {
124         .ops = &default_dev_ops_root,
125         .bus = &dev_root.link[0],
126         .links = 1,
127         .link = {
128                 [0] = {
129                         .dev = &dev_root,
130                         .link = 0,
131                 },
132         },
133 };