2 * (c) 1999--2000 Martin Mares <mj@suse.cz>
3 * (c) 2003 Eric Biederman <ebiederm@xmission.com>
5 /* lots of mods by ron minnich (rminnich@lanl.gov), with
6 * the final architecture guidance from Tom Merritt (tjm@codegen.com)
7 * In particular, we changed from the one-pass original version to
8 * Tom's recommended multiple-pass version. I wasn't sure about doing
9 * it with multiple passes, until I actually started doing it and saw
10 * the wisdom of Tom's recommendations ...
12 * Lots of cleanups by Eric Biederman to handle bridges, and to
13 * handle resource allocation for non-pci devices.
16 #include <console/console.h>
19 #include <device/device.h>
20 #include <device/pci.h>
23 * This is the root of the device tree. A PCI tree always has
24 * one bus, bus 0. Bus 0 contains devices and bridges.
26 struct device dev_root;
27 /* Linked list of ALL devices */
28 struct device *all_devices = 0;
29 /* pointer to the last device */
30 static struct device **last_dev_p = &all_devices;
32 #define DEVICE_MEM_HIGH 0xFEC00000UL /* Reserve 20M for the system */
33 #define DEVICE_IO_START 0x1000
36 unsigned long device_memory_base;
39 /* Append a new device to the global device chain.
40 * The chain is used to find devices once everything is set up.
42 void append_device(struct device *dev)
45 last_dev_p = &dev->next;
49 /** round a number to an alignment.
50 * @param val the starting value
51 * @param roundup Alignment as a power of two
52 * @returns rounded up number
54 static unsigned long round(unsigned long val, unsigned long roundup)
56 /* ROUNDUP MUST BE A POWER OF TWO. */
57 unsigned long inverse;
58 inverse = ~(roundup - 1);
64 static unsigned long round_down(unsigned long val, unsigned long round_down)
66 /* ROUND_DOWN MUST BE A POWER OF TWO. */
67 unsigned long inverse;
68 inverse = ~(round_down - 1);
74 /** Read the resources on all devices of a given bus.
75 * @param bus bus to read the resources on.
77 static void read_resources(struct device *bus)
79 struct device *curdev;
82 /* Walk through all of the devices and find which resources they need. */
83 for(curdev = bus->children; curdev; curdev = curdev->sibling) {
84 if (curdev->resources > 0) {
87 curdev->ops->read_resources(curdev);
91 static struct device *largest_resource(struct device *bus, struct resource **result_res,
92 unsigned long type_mask, unsigned long type)
94 struct device *curdev;
95 struct device *result_dev = 0;
96 struct resource *last = *result_res;
97 struct resource *result = 0;
99 for(curdev = bus->children; curdev; curdev = curdev->sibling) {
101 for(i = 0; i < curdev->resources; i++) {
102 struct resource *resource = &curdev->resource[i];
103 /* If it isn't the right kind of resource ignore it */
104 if ((resource->flags & type_mask) != type) {
107 /* Be certain to pick the successor to last */
108 if (resource == last) {
113 (last->align < resource->align) ||
114 ((last->align == resource->align) &&
115 (last->size < resource->size)) ||
116 ((last->align == resource->align) &&
117 (last->size == resource->size) &&
122 (result->align < resource->align) ||
123 ((result->align == resource->align) &&
124 (result->size < resource->size))) {
130 *result_res = result;
134 /* Compute allocate resources is the guts of the resource allocator.
137 * - Allocate resources locations for every device.
138 * - Don't overlap, and follow the rules of bridges.
139 * - Don't overlap with resources in fixed locations.
140 * - Be efficient so we don't have ugly strategies.
143 * - Devices that have fixed addresses are the minority so don't
144 * worry about them too much. Instead only use part of the address
145 * space for devices with programmable addresses. This easily handles
146 * everything except bridges.
148 * - PCI devices are required to have thier sizes and their alignments
149 * equal. In this case an optimal solution to the packing problem
150 * exists. Allocate all devices from highest alignment to least
151 * alignment or vice versa. Use this.
153 * - So we can handle more than PCI run two allocation passes on
154 * bridges. The first to see how large the resources are behind
155 * the bridge, and what their alignment requirements are. The
156 * second to assign a safe address to the devices behind the
157 * bridge. This allows me to treat a bridge as just a device with
158 * a couple of resources, and not need to special case it in the
159 * allocator. Also this allows handling of other types of bridges.
163 void compute_allocate_resource(
165 struct resource *bridge,
166 unsigned long type_mask,
170 struct resource *resource;
172 unsigned long align, min_align;
176 /* We want different minimum alignments for different kinds of
177 * resources. These minimums are not device type specific
178 * but resource type specific.
180 if (bridge->flags & IORESOURCE_IO) {
181 min_align = log2(DEVICE_IO_ALIGN);
183 if (bridge->flags & IORESOURCE_MEM) {
184 min_align = log2(DEVICE_MEM_ALIGN);
187 printk_spew("DEV: %02x:%02x.%01x compute_allocate_%s: base: %08lx size: %08lx align: %d gran: %d\n",
189 PCI_SLOT(bus->devfn), PCI_FUNC(bus->devfn),
190 (bridge->flags & IORESOURCE_IO)? "io":
191 (bridge->flags & IORESOURCE_PREFETCH)? "prefmem" : "mem",
192 base, bridge->size, bridge->align, bridge->gran);
194 /* Make certain I have read in all of the resources */
197 /* Remember I haven't found anything yet. */
200 /* Walk through all the devices on the current bus and compute the addresses */
201 while((dev = largest_resource(bus, &resource, type_mask, type))) {
203 /* Do NOT I repeat do not ignore resources which have zero size.
204 * If they need to be ignored dev->read_resources should not even
205 * return them. Some resources must be set even when they have
206 * no size. PCI bridge resources are a good example of this.
209 /* Propogate the resource alignment to the bridge register */
210 if (resource->align > bridge->align) {
211 bridge->align = resource->align;
214 /* Make certain we are dealing with a good minimum size */
215 size = resource->size;
216 align = resource->align;
217 if (align < min_align) {
220 if (resource->flags & IORESOURCE_IO) {
221 /* Don't allow potential aliases over the
222 * legacy pci expansion card addresses.
224 if ((base > 0x3ff) && ((base & 0x300) != 0)) {
225 base = (base & ~0x3ff) + 0x400;
227 /* Don't allow allocations in the VGA IO range.
228 * PCI has special cases for that.
230 else if ((base >= 0x3b0) && (base <= 0x3df)) {
234 if (((round(base, 1UL << align) + size) -1) <= resource->limit) {
235 /* base must be aligned to size */
236 base = round(base, 1UL << align);
237 resource->base = base;
238 resource->flags |= IORESOURCE_SET;
242 "DEV: %02x:%02x.%01x %02x * [0x%08lx - 0x%08lx] %s\n",
244 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
246 resource->base, resource->base + resource->size -1,
247 (resource->flags & IORESOURCE_IO)? "io":
248 (resource->flags & IORESOURCE_PREFETCH)? "prefmem": "mem");
252 /* A pci bridge resource does not need to be a power
253 * of two size, but it does have a minimum granularity.
254 * Round the size up to that minimum granularity so we
255 * know not to place something else at an address postitively
256 * decoded by the bridge.
258 bridge->size = round(base, 1UL << bridge->gran) - bridge->base;
260 printk_spew("DEV: %02x:%02x.%01x compute_allocate_%s: base: %08lx size: %08lx align: %d gran: %d done\n",
262 PCI_SLOT(bus->devfn), PCI_FUNC(bus->devfn),
263 (bridge->flags & IORESOURCE_IO)? "io":
264 (bridge->flags & IORESOURCE_PREFETCH)? "prefmem" : "mem",
265 base, bridge->size, bridge->align, bridge->gran);
270 static void allocate_vga_resource(void)
272 /* FIXME handle the VGA pallette snooping */
273 struct device *dev, *vga, *bus;
275 for(dev = all_devices; dev; dev = dev->next) {
276 uint32_t class_revision;
277 class_revision = pci_read_config32(dev, PCI_CLASS_REVISION);
278 if (((class_revision >> 24) == 0x03) &&
279 ((class_revision >> 16) != 0x380)) {
281 printk_debug("Allocating VGA resource\n");
285 /* All legacy VGA cards have MEM & I/O space registers */
286 dev->command |= PCI_COMMAND_MEMORY | PCI_COMMAND_IO;
288 /* It isn't safe to enable other VGA cards */
289 dev->command &= ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
296 /* Now walk up the bridges setting the VGA enable */
299 ctrl = pci_read_config16(bus, PCI_BRIDGE_CONTROL);
300 ctrl |= PCI_BRIDGE_CTL_VGA;
301 pci_write_config16(bus, PCI_BRIDGE_CONTROL, ctrl);
302 bus = (bus == bus->bus)? 0 : bus->bus;
307 /** Assign the computed resources to the bridges and devices on the bus.
308 * Recurse to any bridges found on this bus first. Then do the devices
310 * @param bus Pointer to the structure for this bus
312 void assign_resources(struct device *bus)
314 struct device *curdev;
316 printk_debug("ASSIGN RESOURCES, bus %d\n", bus->secondary);
318 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
319 curdev->ops->set_resources(curdev);
321 printk_debug("ASSIGNED RESOURCES, bus %d\n", bus->secondary);
324 static void enable_resources(struct device *bus)
326 struct device *curdev;
328 /* Walk through the chain of all pci devices and enable them.
329 * This is effectively a breadth first traversal so we should
330 * not have enalbing ordering problems.
332 for (curdev = all_devices; curdev; curdev = curdev->next) {
334 command = pci_read_config16(curdev, PCI_COMMAND);
335 command |= curdev->command;
336 printk_debug("DEV: %02x:%02x.%01x cmd <- %02x\n",
337 curdev->bus->secondary,
338 PCI_SLOT(curdev->devfn), PCI_FUNC(curdev->devfn),
340 pci_write_config16(curdev, PCI_COMMAND, command);
344 /** Enumerate the resources on the PCI by calling pci_init
346 void dev_enumerate(void)
349 printk_info("Enumerating buses...");
352 root->ops = &default_pci_ops_root;
354 root->subordinate = root->ops->scan_bus(root, 0);
355 printk_info("done\n");
358 /** Starting at the root, compute what resources are needed and allocate them.
359 * I/O starts at PCI_IO_START. Since the assignment is hierarchical we
360 * set the values into the dev_root struct.
362 void dev_configure(void)
364 struct device *root = &dev_root;
365 printk_info("Allocating resources...");
369 root->ops->read_resources(root);
371 /* Make certain the io devices are allocated somewhere
374 root->resource[0].base = DEVICE_IO_START;
375 root->resource[0].flags |= IORESOURCE_SET;
376 /* Now reallocate the pci resources memory with the
377 * highest addresses I can manage.
379 root->resource[1].base =
380 round_down(DEVICE_MEM_HIGH - root->resource[1].size,
381 1UL << root->resource[1].align);
382 device_memory_base = root->resource[1].base;
383 root->resource[1].flags |= IORESOURCE_SET;
384 // now just set things into registers ... we hope ...
385 root->ops->set_resources(root);
387 allocate_vga_resource();
389 printk_info("done.\n");
392 /** Starting at the root, walk the tree and enable all devices/bridges.
393 * What really happens is computed COMMAND bits get set in register 4
395 void dev_enable(void)
397 printk_info("Enabling resourcess...");
399 /* now enable everything. */
400 enable_resources(&dev_root);
401 printk_info("done.\n");
404 /** Starting at the root, walk the tree and call a driver to
405 * do device specific setup.
407 void dev_initialize(void)
411 printk_info("Initializing devices...\n");
412 for (dev = all_devices; dev; dev = dev->next) {
413 if (dev->ops->init) {
414 printk_debug("PCI: %02x:%02x.%01x init\n",
416 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
420 printk_info("Devices initialized\n");