changed dev->enable to dev->enabled. Sorry, I am the only one who can't speak
[coreboot.git] / src / devices / chip.c
1 /* chips are arbitrary chips (superio, southbridge, etc.)
2  * They have private structures that define chip resources and default 
3  * settings. They have four externally visible functions for control. 
4  * They have a generic component which applies to all chips for 
5  * path, etc. 
6  */
7
8 #include <console/console.h>
9 #include <device/chip.h>
10 #include <device/pci.h>
11
12 /**
13  * @brief Configure static devices
14  *
15  * Starting from the static device 'root', walk the tree and configure each
16  * device by calling the device specific chip_control::enable().
17  *
18  * This function is only an iterator, the exact definition of 'configure'
19  * depends on the device specific implementation of chip_control::enable(). 
20  *
21  * @param root root of the static device tree to be configured.
22  * @param pass pass of the configuration operation to be perfromed.
23  *
24  * @see chip_pass
25  * @see chip_control::enable
26  */  
27 void chip_configure(struct chip *root, enum chip_pass pass)
28 {
29         struct chip *c;
30
31         for (c = root; c; c = c->next) {
32                 if (c->control && c->control->enable)
33                         c->control->enable(c, pass);
34         }
35
36         for (c = root; c; c = c->next) {
37                 if (c->children)
38                         chip_configure(c->children, pass);
39         }
40 }
41
42 /**
43  * @brief Convert static device structures to dynamic structures.
44  *
45  * A static device may contain one or more dynamic devices. Dynamic device
46  * structures of these devices have to be generated before the enumeration
47  * of dynamic devices. This function converts a static chip structure to a
48  * set of dynamic device structures.
49  *
50  * This function is the generic method called by enumerate_static_device_chain()
51  * for static devices. Devices extend this default behavior by defining their
52  * own chip_controll::enumerate(). Generally, device specific
53  * chip_control::enumerate() method calls this function as its last operation.
54  *
55  * @param chip static chip structure to be converted.
56  *
57  */
58 void chip_enumerate(struct chip *chip)
59 {
60         struct chip *child;
61         device_t dev;
62         int link;
63         int i;
64
65         dev = 0;
66         link = 0;
67
68         if (chip->control && chip->control->name) {
69                 printk_debug("Enumerating: %s\n", chip->control->name);
70         }
71
72         for (i = 0; i < MAX_CHIP_PATHS; i++) {
73                 int identical_paths;
74                 identical_paths = 
75                         (i > 0) &&
76                         (path_eq(&chip->path[i - 1].path, &chip->path[i].path));
77                 if (!identical_paths) {
78                         struct bus *parent;
79                         int bus;
80                         link = 0;
81                         dev = 0;
82                         parent = chip->bus;
83                         switch(chip->path[i].path.type) {
84                         case DEVICE_PATH_NONE:
85                                 /* no dynamic device associated */
86                                 break;
87                         case DEVICE_PATH_PCI:
88                                 bus = chip->path[i].path.u.pci.bus;
89                                 if (bus != 0) {
90                                         device_t dev;
91                                         int i = 1;
92                                         dev = chip->dev;
93                                         while (dev && (i != bus)) {
94                                                 dev = dev->next;
95                                                 i++;
96                                         }
97                                         if ((i == bus) && dev) {
98                                                 parent = &dev->link[0];
99                                         }
100                                 }
101                                 /* Fall through */
102                         default:
103                                 dev = alloc_dev(parent, &chip->path[i].path);
104                                 break;
105                         }
106                 } else {
107                         link += 1;
108                 }
109
110                 if (dev) {
111                         struct chip_resource *res, *res_limit;
112                         printk_spew("path (%p) %s %s",
113                                     dev, dev_path(dev),
114                                     identical_paths?"identical":"");
115                         printk_spew(" parent: (%p) %s\n",
116                                     dev->bus->dev,  dev_path(dev->bus->dev));
117                         dev->chip = chip;
118                         dev->enabled = chip->path[i].enable;
119                         dev->links = link + 1;
120                         for (child = chip->children; child; child = child->next) {
121                                 if (!child->bus && child->link == i) {
122                                         child->bus = &dev->link[link];
123                                 }
124                         }
125                         res = &chip->path[i].resource[0];
126                         res_limit = &chip->path[i].resource[MAX_RESOURCES];
127                         for(; res < res_limit; res++) {
128                                 if (res->flags) {
129                                         struct resource *resource;
130                                         resource = get_resource(dev, res->index);
131                                         resource->flags = res->flags | IORESOURCE_FIXED | IORESOURCE_ASSIGNED;
132                                         resource->base = res->base;
133                                 }
134                         }
135                 }
136
137                 if (dev && !chip->dev) {
138                         chip->dev = dev;
139                 }
140         }
141
142         for (child = chip->children; child; child = child->next) {
143                 if (!child->bus) {
144                         child->bus = &chip->dev->link[0];
145                 }
146         }
147 }
148
149 /**
150  * @brief Enumerate a static device tree.
151  *
152  * A static device chain is a linked list of static device structures which are
153  * on the same branch of the static device tree. This function does not only
154  * enumerate the devices on a single chain, as its name suggest, it also walks
155  * into the subordinary chains by recursion. It calls the device specific
156  * chip_control::enumerate() of the device if one exists or calls the generic
157  * chip_enumerate(). 
158  *
159  * This function is only an iterator, the exact definition of 'enumerate'
160  * depends on the implementation of the generic chip_enumerate() and/or device
161  * specific chip_control::enumerate(). 
162  *
163  * @param root static chip structure to start with.
164  *
165  * @see chip_control::enumerate()
166  */
167 static void enumerate_static_device_chain(struct chip *root)
168 {
169         struct chip *chip;
170
171         for (chip = root; chip; chip = chip->next) {
172                 void (*enumerate)(struct chip *chip);
173                 enumerate = chip_enumerate;
174                 if (chip->control && chip->control->enumerate) {
175                         enumerate = chip->control->enumerate;
176                 }
177                 enumerate(chip);
178         }
179
180         for (chip = root; chip; chip = chip->next) {
181                 if (chip->children) {
182                         enumerate_static_device_chain(chip->children);
183                 }
184         }
185 }
186
187 /**
188  * @brief Enumerate static devices in the system.
189  *
190  * Static device is . Static devices are actually enumerated or "listed" in
191  * the Config.lb config file and the corresponding data structures are
192  * generated by config tool in the static.c. 
193  *
194  * \note The definition of 'enumerate' is not clear in this context. Does it
195  * mean probe ?
196  *
197  * \note How do we determine the existence of the static devices ? Static
198  * devices are listed in the config file and generated at compile time by config
199  * tool. This function is called at certain point in the early stage of
200  * LinuxBIOS. It uses the chip_enumerate() function to convert the static
201  * structures into dynamic ones. What if the static devices listed in the config
202  * file does actually not exist in the system ? Is there any side effect of
203  * these 'phantom' device structures
204  *
205  * The static device does not necesarry conform to the dynamic device tree in
206  * the system.
207  */
208 void enumerate_static_devices(void)
209 {
210         printk_info("Enumerating static devices...\n");
211         enumerate_static_device_chain(&static_root);
212 }