blob: e6fc99463da3d6245587c822e0cc616ef2f609e5 [file] [log] [blame]
Eric Biederman8ca8d762003-04-22 19:02:15 +00001/*
Stefan Reinauer7e61e452008-01-18 10:35:56 +00002 * This file is part of the coreboot project.
Uwe Hermannb80dbf02007-04-22 19:08:13 +00003 *
4 * It was originally based on the Linux kernel (arch/i386/kernel/pci-pc.c).
5 *
6 * Modifications are:
7 * Copyright (C) 2003 Eric Biederman <ebiederm@xmission.com>
8 * Copyright (C) 2003-2004 Linux Networx
9 * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
10 * Copyright (C) 2003 Ronald G. Minnich <rminnich@gmail.com>
11 * Copyright (C) 2004-2005 Li-Ta Lo <ollie@lanl.gov>
12 * Copyright (C) 2005-2006 Tyan
13 * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan)
14 * Copyright (C) 2005-2006 Stefan Reinauer <stepan@openbios.org>
15 */
16
17/*
Eric Biederman8ca8d762003-04-22 19:02:15 +000018 * (c) 1999--2000 Martin Mares <mj@suse.cz>
Eric Biederman8ca8d762003-04-22 19:02:15 +000019 */
20/* lots of mods by ron minnich (rminnich@lanl.gov), with
21 * the final architecture guidance from Tom Merritt (tjm@codegen.com)
22 * In particular, we changed from the one-pass original version to
23 * Tom's recommended multiple-pass version. I wasn't sure about doing
24 * it with multiple passes, until I actually started doing it and saw
25 * the wisdom of Tom's recommendations ...
26 *
27 * Lots of cleanups by Eric Biederman to handle bridges, and to
28 * handle resource allocation for non-pci devices.
29 */
30
31#include <console/console.h>
32#include <bitops.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000033#include <arch/io.h>
Eric Biederman5899fd82003-04-24 06:25:08 +000034#include <device/device.h>
35#include <device/pci.h>
Li-Ta Lo54f05f62004-05-14 17:20:29 +000036#include <device/pci_ids.h>
Eric Biedermane9a271e32003-09-02 03:36:25 +000037#include <stdlib.h>
38#include <string.h>
Eric Biederman03acab62004-10-14 21:25:53 +000039#include <smp/spinlock.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000040
Li-Ta Loe5266692004-03-23 21:28:05 +000041/** Linked list of ALL devices */
Eric Biederman5cd81732004-03-11 15:01:31 +000042struct device *all_devices = &dev_root;
Li-Ta Loe5266692004-03-23 21:28:05 +000043/** Pointer to the last device */
Eric Biederman7003ba42004-10-16 06:20:29 +000044extern struct device **last_dev_p;
Eric Biederman8ca8d762003-04-22 19:02:15 +000045
Li-Ta Loe5266692004-03-23 21:28:05 +000046/** The upper limit of MEM resource of the devices.
47 * Reserve 20M for the system */
Eric Biederman03acab62004-10-14 21:25:53 +000048#define DEVICE_MEM_HIGH 0xFEBFFFFFUL
Li-Ta Loe5266692004-03-23 21:28:05 +000049/** The lower limit of IO resource of the devices.
50 * Reserve 4k for ISA/Legacy devices */
Eric Biederman8ca8d762003-04-22 19:02:15 +000051#define DEVICE_IO_START 0x1000
52
Li-Ta Loe5266692004-03-23 21:28:05 +000053/**
54 * @brief Allocate a new device structure.
55 *
Li-Ta Lo9f0d0f92004-05-10 16:05:16 +000056 * Allocte a new device structure and attached it to the device tree as a
57 * child of the parent bus.
Li-Ta Loe5266692004-03-23 21:28:05 +000058 *
59 * @param parent parent bus the newly created device attached to.
60 * @param path path to the device to be created.
61 *
62 * @return pointer to the newly created device structure.
63 *
64 * @see device_path
Eric Biederman8ca8d762003-04-22 19:02:15 +000065 */
Eric Biederman448bd632004-10-14 22:52:15 +000066static spinlock_t dev_lock = SPIN_LOCK_UNLOCKED;
Eric Biedermane9a271e32003-09-02 03:36:25 +000067device_t alloc_dev(struct bus *parent, struct device_path *path)
Eric Biederman8ca8d762003-04-22 19:02:15 +000068{
Eric Biedermane9a271e32003-09-02 03:36:25 +000069 device_t dev, child;
70 int link;
Li-Ta Loe5266692004-03-23 21:28:05 +000071
Eric Biederman03acab62004-10-14 21:25:53 +000072 spin_lock(&dev_lock);
Li-Ta Lo3a812852004-12-03 22:39:34 +000073
Eric Biedermane9a271e32003-09-02 03:36:25 +000074 /* Find the last child of our parent */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000075 for(child = parent->children; child && child->sibling; ) {
Eric Biedermane9a271e32003-09-02 03:36:25 +000076 child = child->sibling;
77 }
Li-Ta Lo3a812852004-12-03 22:39:34 +000078
Eric Biedermane9a271e32003-09-02 03:36:25 +000079 dev = malloc(sizeof(*dev));
80 if (dev == 0) {
81 die("DEV: out of memory.\n");
82 }
83 memset(dev, 0, sizeof(*dev));
84 memcpy(&dev->path, path, sizeof(*path));
85
Eric Biedermane9a271e32003-09-02 03:36:25 +000086 /* Initialize the back pointers in the link fields */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000087 for(link = 0; link < MAX_LINKS; link++) {
Eric Biedermane9a271e32003-09-02 03:36:25 +000088 dev->link[link].dev = dev;
89 dev->link[link].link = link;
90 }
Li-Ta Lo3a812852004-12-03 22:39:34 +000091
Eric Biederman03acab62004-10-14 21:25:53 +000092 /* By default devices are enabled */
93 dev->enabled = 1;
Eric Biedermane9a271e32003-09-02 03:36:25 +000094
Eric Biedermanb78c1972004-10-14 20:54:17 +000095 /* Add the new device to the list of children of the bus. */
Eric Biedermane9a271e32003-09-02 03:36:25 +000096 dev->bus = parent;
97 if (child) {
98 child->sibling = dev;
99 } else {
100 parent->children = dev;
101 }
Li-Ta Loe5266692004-03-23 21:28:05 +0000102
Eric Biederman03acab62004-10-14 21:25:53 +0000103 /* Append a new device to the global device list.
104 * The list is used to find devices once everything is set up.
105 */
106 *last_dev_p = dev;
107 last_dev_p = &dev->next;
108
109 spin_unlock(&dev_lock);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000110 return dev;
111}
Eric Biederman8ca8d762003-04-22 19:02:15 +0000112
Li-Ta Loe5266692004-03-23 21:28:05 +0000113/**
114 * @brief round a number up to an alignment.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000115 * @param val the starting value
116 * @param roundup Alignment as a power of two
117 * @returns rounded up number
118 */
Eric Biederman448bd632004-10-14 22:52:15 +0000119static resource_t round(resource_t val, unsigned long pow)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000120{
Eric Biederman448bd632004-10-14 22:52:15 +0000121 resource_t mask;
122 mask = (1ULL << pow) - 1ULL;
123 val += mask;
124 val &= ~mask;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000125 return val;
126}
127
Eric Biederman8ca8d762003-04-22 19:02:15 +0000128/** Read the resources on all devices of a given bus.
129 * @param bus bus to read the resources on.
130 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000131static void read_resources(struct bus *bus)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000132{
133 struct device *curdev;
134
Eric Biederman448bd632004-10-14 22:52:15 +0000135 printk_spew("%s read_resources bus %d link: %d\n",
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000136 dev_path(bus->dev), bus->secondary, bus->link);
Eric Biederman448bd632004-10-14 22:52:15 +0000137
Eric Biederman8ca8d762003-04-22 19:02:15 +0000138 /* Walk through all of the devices and find which resources they need. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000139 for(curdev = bus->children; curdev; curdev = curdev->sibling) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000140 unsigned links;
141 int i;
Eric Biederman448bd632004-10-14 22:52:15 +0000142 if (curdev->have_resources) {
143 continue;
144 }
145 if (!curdev->enabled) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000146 continue;
147 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000148 if (!curdev->ops || !curdev->ops->read_resources) {
149 printk_err("%s missing read_resources\n",
Eric Biedermanb78c1972004-10-14 20:54:17 +0000150 dev_path(curdev));
Eric Biedermane9a271e32003-09-02 03:36:25 +0000151 continue;
152 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000153 curdev->ops->read_resources(curdev);
Eric Biederman448bd632004-10-14 22:52:15 +0000154 curdev->have_resources = 1;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000155 /* Read in subtractive resources behind the current device */
156 links = 0;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000157 for(i = 0; i < curdev->resources; i++) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000158 struct resource *resource;
Eric Biederman448bd632004-10-14 22:52:15 +0000159 unsigned link;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000160 resource = &curdev->resource[i];
Eric Biederman448bd632004-10-14 22:52:15 +0000161 if (!(resource->flags & IORESOURCE_SUBTRACTIVE))
162 continue;
163 link = IOINDEX_SUBTRACTIVE_LINK(resource->index);
164 if (link > MAX_LINKS) {
165 printk_err("%s subtractive index on link: %d\n",
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000166 dev_path(curdev), link);
Eric Biederman448bd632004-10-14 22:52:15 +0000167 continue;
168 }
169 if (!(links & (1 << link))) {
170 links |= (1 << link);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000171 read_resources(&curdev->link[link]);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000172 }
173 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000174 }
Eric Biederman448bd632004-10-14 22:52:15 +0000175 printk_spew("%s read_resources bus %d link: %d done\n",
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000176 dev_path(bus->dev), bus->secondary, bus->link);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000177}
178
Eric Biedermane9a271e32003-09-02 03:36:25 +0000179struct pick_largest_state {
180 struct resource *last;
181 struct device *result_dev;
182 struct resource *result;
183 int seen_last;
184};
185
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000186static void pick_largest_resource(void *gp,
Eric Biedermanb78c1972004-10-14 20:54:17 +0000187 struct device *dev, struct resource *resource)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000188{
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000189 struct pick_largest_state *state = gp;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000190 struct resource *last;
191 last = state->last;
192 /* Be certain to pick the successor to last */
193 if (resource == last) {
194 state->seen_last = 1;
195 return;
196 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000197 if (resource->flags & IORESOURCE_FIXED ) return; //skip it
Eric Biedermanb78c1972004-10-14 20:54:17 +0000198 if (last && (
199 (last->align < resource->align) ||
200 ((last->align == resource->align) &&
201 (last->size < resource->size)) ||
202 ((last->align == resource->align) &&
203 (last->size == resource->size) &&
204 (!state->seen_last)))) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000205 return;
206 }
Eric Biedermanb78c1972004-10-14 20:54:17 +0000207 if (!state->result ||
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000208 (state->result->align < resource->align) ||
209 ((state->result->align == resource->align) &&
210 (state->result->size < resource->size)))
211 {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000212 state->result_dev = dev;
213 state->result = resource;
Li-Ta Loe5266692004-03-23 21:28:05 +0000214 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000215}
216
Eric Biederman448bd632004-10-14 22:52:15 +0000217static struct device *largest_resource(struct bus *bus, struct resource **result_res,
218 unsigned long type_mask, unsigned long type)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000219{
220 struct pick_largest_state state;
221
222 state.last = *result_res;
223 state.result_dev = 0;
224 state.result = 0;
225 state.seen_last = 0;
226
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000227 search_bus_resources(bus, type_mask, type, pick_largest_resource, &state);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000228
229 *result_res = state.result;
230 return state.result_dev;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000231}
232
233/* Compute allocate resources is the guts of the resource allocator.
234 *
235 * The problem.
236 * - Allocate resources locations for every device.
237 * - Don't overlap, and follow the rules of bridges.
238 * - Don't overlap with resources in fixed locations.
239 * - Be efficient so we don't have ugly strategies.
240 *
241 * The strategy.
242 * - Devices that have fixed addresses are the minority so don't
243 * worry about them too much. Instead only use part of the address
244 * space for devices with programmable addresses. This easily handles
245 * everything except bridges.
246 *
247 * - PCI devices are required to have thier sizes and their alignments
248 * equal. In this case an optimal solution to the packing problem
249 * exists. Allocate all devices from highest alignment to least
250 * alignment or vice versa. Use this.
251 *
252 * - So we can handle more than PCI run two allocation passes on
253 * bridges. The first to see how large the resources are behind
254 * the bridge, and what their alignment requirements are. The
255 * second to assign a safe address to the devices behind the
256 * bridge. This allows me to treat a bridge as just a device with
257 * a couple of resources, and not need to special case it in the
258 * allocator. Also this allows handling of other types of bridges.
259 *
260 */
261
262void compute_allocate_resource(
Eric Biedermane9a271e32003-09-02 03:36:25 +0000263 struct bus *bus,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000264 struct resource *bridge,
265 unsigned long type_mask,
266 unsigned long type)
267{
268 struct device *dev;
269 struct resource *resource;
Eric Biederman03acab62004-10-14 21:25:53 +0000270 resource_t base;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000271 unsigned long align, min_align;
272 min_align = 0;
273 base = bridge->base;
274
Stefan Reinauer51754a32008-08-01 12:28:38 +0000275 printk_spew("%s compute_allocate_resource %s: base: %08Lx size: %08Lx align: %d gran: %d\n",
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000276 dev_path(bus->dev),
277 (bridge->flags & IORESOURCE_IO)? "io":
278 (bridge->flags & IORESOURCE_PREFETCH)? "prefmem" : "mem",
279 base, bridge->size, bridge->align, bridge->gran);
Ronald G. Minnich99dcf232003-09-30 02:16:47 +0000280
Eric Biederman8ca8d762003-04-22 19:02:15 +0000281 /* We want different minimum alignments for different kinds of
282 * resources. These minimums are not device type specific
283 * but resource type specific.
284 */
285 if (bridge->flags & IORESOURCE_IO) {
286 min_align = log2(DEVICE_IO_ALIGN);
287 }
288 if (bridge->flags & IORESOURCE_MEM) {
289 min_align = log2(DEVICE_MEM_ALIGN);
290 }
291
Eric Biederman8ca8d762003-04-22 19:02:15 +0000292 /* Make certain I have read in all of the resources */
293 read_resources(bus);
294
295 /* Remember I haven't found anything yet. */
296 resource = 0;
297
Eric Biedermanb78c1972004-10-14 20:54:17 +0000298 /* Walk through all the devices on the current bus and
299 * compute the addresses.
300 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000301 while((dev = largest_resource(bus, &resource, type_mask, type))) {
Eric Biederman03acab62004-10-14 21:25:53 +0000302 resource_t size;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000303 /* Do NOT I repeat do not ignore resources which have zero size.
304 * If they need to be ignored dev->read_resources should not even
305 * return them. Some resources must be set even when they have
306 * no size. PCI bridge resources are a good example of this.
307 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000308 /* Make certain we are dealing with a good minimum size */
309 size = resource->size;
310 align = resource->align;
311 if (align < min_align) {
312 align = min_align;
313 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000314
Stefan Reinauer51754a32008-08-01 12:28:38 +0000315 /* Propagate the resource alignment to the bridge register */
Aaron Lwe7ca3ec22008-04-25 02:02:33 +0000316 if (align > bridge->align) {
317 bridge->align = align;
318 }
319
Eric Biedermane9a271e32003-09-02 03:36:25 +0000320 if (resource->flags & IORESOURCE_FIXED) {
321 continue;
322 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000323
Eric Biedermandbec2d42004-10-21 10:44:08 +0000324 /* Propogate the resource limit to the bridge register */
325 if (bridge->limit > resource->limit) {
326 bridge->limit = resource->limit;
327 }
Stefan Reinauer11a20142008-08-01 13:08:33 +0000328#warning This heuristic should be replaced by real devices with fixed resources.
Eric Biedermandbec2d42004-10-21 10:44:08 +0000329 /* Artificially deny limits between DEVICE_MEM_HIGH and 0xffffffff */
330 if ((bridge->limit > DEVICE_MEM_HIGH) && (bridge->limit <= 0xffffffff)) {
331 bridge->limit = DEVICE_MEM_HIGH;
332 }
Stefan Reinauer51754a32008-08-01 12:28:38 +0000333
Eric Biederman8ca8d762003-04-22 19:02:15 +0000334 if (resource->flags & IORESOURCE_IO) {
335 /* Don't allow potential aliases over the
336 * legacy pci expansion card addresses.
Eric Biedermanbbb6d102003-08-04 19:54:48 +0000337 * The legacy pci decodes only 10 bits,
338 * uses 100h - 3ffh. Therefor, only 0 - ff
339 * can be used out of each 400h block of io
340 * space.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000341 */
Eric Biedermanbbb6d102003-08-04 19:54:48 +0000342 if ((base & 0x300) != 0) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000343 base = (base & ~0x3ff) + 0x400;
344 }
345 /* Don't allow allocations in the VGA IO range.
346 * PCI has special cases for that.
347 */
348 else if ((base >= 0x3b0) && (base <= 0x3df)) {
349 base = 0x3e0;
350 }
351 }
Eric Biederman03acab62004-10-14 21:25:53 +0000352 if (((round(base, align) + size) -1) <= resource->limit) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000353 /* base must be aligned to size */
Eric Biederman03acab62004-10-14 21:25:53 +0000354 base = round(base, align);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000355 resource->base = base;
Eric Biederman5cd81732004-03-11 15:01:31 +0000356 resource->flags |= IORESOURCE_ASSIGNED;
357 resource->flags &= ~IORESOURCE_STORED;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000358 base += size;
359
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000360 printk_spew(
361 "%s %02x * [0x%08Lx - 0x%08Lx] %s\n",
362 dev_path(dev),
363 resource->index,
364 resource->base,
365 resource->base + resource->size - 1,
366 (resource->flags & IORESOURCE_IO)? "io":
367 (resource->flags & IORESOURCE_PREFETCH)? "prefmem": "mem");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000368 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000369 }
370 /* A pci bridge resource does not need to be a power
371 * of two size, but it does have a minimum granularity.
372 * Round the size up to that minimum granularity so we
373 * know not to place something else at an address postitively
374 * decoded by the bridge.
375 */
Eric Biederman03acab62004-10-14 21:25:53 +0000376 bridge->size = round(base, bridge->gran) - bridge->base;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000377
Stefan Reinauer51754a32008-08-01 12:28:38 +0000378 printk_spew("%s compute_allocate_resource %s: base: %08Lx size: %08Lx align: %d gran: %d done\n",
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000379 dev_path(bus->dev),
380 (bridge->flags & IORESOURCE_IO)? "io":
381 (bridge->flags & IORESOURCE_PREFETCH)? "prefmem" : "mem",
382 base, bridge->size, bridge->align, bridge->gran);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000383
384
385}
arch import user (historical)dc811182005-07-06 17:16:09 +0000386
Yinghai Lu9e4faef2005-01-14 22:04:49 +0000387#if CONFIG_CONSOLE_VGA == 1
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000388device_t vga_pri = 0;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000389static void allocate_vga_resource(void)
390{
Eric Biedermane9a271e32003-09-02 03:36:25 +0000391#warning "FIXME modify allocate_vga_resource so it is less pci centric!"
Li-Ta Loe5266692004-03-23 21:28:05 +0000392#warning "This function knows to much about PCI stuff, it should be just a ietrator/visitor."
393
Eric Biederman8ca8d762003-04-22 19:02:15 +0000394 /* FIXME handle the VGA pallette snooping */
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000395 struct device *dev, *vga, *vga_onboard, *vga_first, *vga_last;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000396 struct bus *bus;
397 bus = 0;
398 vga = 0;
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000399 vga_onboard = 0;
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000400 vga_first = 0;
401 vga_last = 0;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000402 for(dev = all_devices; dev; dev = dev->next) {
arch import user (historical)dc811182005-07-06 17:16:09 +0000403 if (!dev->enabled) continue;
Li-Ta Lo54f05f62004-05-14 17:20:29 +0000404 if (((dev->class >> 16) == PCI_BASE_CLASS_DISPLAY) &&
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000405 ((dev->class >> 8) != PCI_CLASS_DISPLAY_OTHER))
406 {
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000407 if (!vga_first) {
408 if (dev->on_mainboard) {
409 vga_onboard = dev;
410 } else {
411 vga_first = dev;
412 }
413 } else {
414 if (dev->on_mainboard) {
415 vga_onboard = dev;
416 } else {
417 vga_last = dev;
418 }
419 }
420
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000421 /* It isn't safe to enable other VGA cards */
422 dev->command &= ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000423 }
424 }
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000425
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000426 vga = vga_last;
427
428 if(!vga) {
429 vga = vga_first;
430 }
431
Yinghai Lu2b396cd2006-05-18 16:54:30 +0000432#if CONFIG_CONSOLE_VGA_ONBOARD_AT_FIRST == 1
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000433 if (vga_onboard) // will use on board vga as pri
434#else
435 if (!vga) // will use last add on adapter as pri
436#endif
437 {
438 vga = vga_onboard;
439 }
440
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000441
arch import user (historical)dc811182005-07-06 17:16:09 +0000442 if (vga) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000443 /* vga is first add on card or the only onboard vga */
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000444 printk_debug("Allocating VGA resource %s\n", dev_path(vga));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000445 /* All legacy VGA cards have MEM & I/O space registers */
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000446 vga->command |= (PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
447 vga_pri = vga;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000448 bus = vga->bus;
449 }
450 /* Now walk up the bridges setting the VGA enable */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000451 while(bus) {
Li-Ta Lo515f6c72005-01-11 22:48:54 +0000452 printk_debug("Setting PCI_BRIDGE_CTL_VGA for bridge %s\n",
453 dev_path(bus->dev));
Li-Ta Lodb7f47c2004-01-08 21:15:49 +0000454 bus->bridge_ctrl |= PCI_BRIDGE_CTL_VGA;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000455 bus = (bus == bus->dev->bus)? 0 : bus->dev->bus;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000456 }
457}
Stefan Reinauer7ce8c542005-12-02 21:52:30 +0000458
Yinghai Lu9e4faef2005-01-14 22:04:49 +0000459#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +0000460
Yinghai Lu1f1085b2005-01-17 21:37:12 +0000461
Li-Ta Lo04930692004-11-25 17:37:19 +0000462/**
463 * @brief Assign the computed resources to the devices on the bus.
464 *
Eric Biederman8ca8d762003-04-22 19:02:15 +0000465 * @param bus Pointer to the structure for this bus
Li-Ta Lo04930692004-11-25 17:37:19 +0000466 *
467 * Use the device specific set_resources method to store the computed
468 * resources to hardware. For bridge devices, the set_resources() method
469 * has to recurse into every down stream buses.
470 *
471 * Mutual recursion:
472 * assign_resources() -> device_operation::set_resources()
473 * device_operation::set_resources() -> assign_resources()
474 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000475void assign_resources(struct bus *bus)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000476{
477 struct device *curdev;
478
Eric Biederman03acab62004-10-14 21:25:53 +0000479 printk_spew("%s assign_resources, bus %d link: %d\n",
480 dev_path(bus->dev), bus->secondary, bus->link);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000481
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000482 for(curdev = bus->children; curdev; curdev = curdev->sibling) {
Eric Biederman03acab62004-10-14 21:25:53 +0000483 if (!curdev->enabled || !curdev->resources) {
484 continue;
485 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000486 if (!curdev->ops || !curdev->ops->set_resources) {
487 printk_err("%s missing set_resources\n",
Eric Biedermanb78c1972004-10-14 20:54:17 +0000488 dev_path(curdev));
Eric Biedermane9a271e32003-09-02 03:36:25 +0000489 continue;
490 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000491 curdev->ops->set_resources(curdev);
492 }
Eric Biederman03acab62004-10-14 21:25:53 +0000493 printk_spew("%s assign_resources, bus %d link: %d\n",
494 dev_path(bus->dev), bus->secondary, bus->link);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000495}
496
Li-Ta Lo5782d272004-04-26 17:51:20 +0000497/**
498 * @brief Enable the resources for a specific device
499 *
500 * @param dev the device whose resources are to be enabled
501 *
502 * Enable resources of the device by calling the device specific
503 * enable_resources() method.
504 *
505 * The parent's resources should be enabled first to avoid having enabling
506 * order problem. This is done by calling the parent's enable_resources()
Li-Ta Lo04930692004-11-25 17:37:19 +0000507 * method and let that method to call it's children's enable_resoruces()
508 * method via the (global) enable_childrens_resources().
Li-Ta Lo9f0d0f92004-05-10 16:05:16 +0000509 *
510 * Indirect mutual recursion:
Li-Ta Lo04930692004-11-25 17:37:19 +0000511 * enable_resources() -> device_operations::enable_resource()
512 * device_operations::enable_resource() -> enable_children_resources()
513 * enable_children_resources() -> enable_resources()
Li-Ta Lo5782d272004-04-26 17:51:20 +0000514 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000515void enable_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000516{
Eric Biederman03acab62004-10-14 21:25:53 +0000517 if (!dev->enabled) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000518 return;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000519 }
Eric Biederman03acab62004-10-14 21:25:53 +0000520 if (!dev->ops || !dev->ops->enable_resources) {
521 printk_err("%s missing enable_resources\n", dev_path(dev));
Eric Biederman83b991a2003-10-11 06:20:25 +0000522 return;
523 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000524 dev->ops->enable_resources(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000525}
526
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000527/**
528 * @brief Reset all of the devices a bus
529 *
530 * Reset all of the devices on a bus and clear the bus's reset_needed flag.
531 *
532 * @param bus pointer to the bus structure
533 *
534 * @return 1 if the bus was successfully reset, 0 otherwise.
535 *
536 */
537int reset_bus(struct bus *bus)
538{
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000539 if (bus && bus->dev && bus->dev->ops && bus->dev->ops->reset_bus)
540 {
541 bus->dev->ops->reset_bus(bus);
542 bus->reset_needed = 0;
543 return 1;
544 }
545 return 0;
546}
547
548/**
549 * @brief Scan for devices on a bus.
550 *
551 * If there are bridges on the bus, recursively scan the buses behind the bridges.
552 * If the setting up and tuning of the bus causes a reset to be required,
553 * reset the bus and scan it again.
554 *
555 * @param bus pointer to the bus device
556 * @param max current bus number
557 *
558 * @return The maximum bus number found, after scanning all subordinate busses
559 */
560unsigned int scan_bus(device_t bus, unsigned int max)
561{
562 unsigned int new_max;
563 int do_scan_bus;
564 if ( !bus ||
565 !bus->enabled ||
566 !bus->ops ||
567 !bus->ops->scan_bus)
568 {
569 return max;
570 }
571 do_scan_bus = 1;
572 while(do_scan_bus) {
573 int link;
574 new_max = bus->ops->scan_bus(bus, max);
575 do_scan_bus = 0;
576 for(link = 0; link < bus->links; link++) {
577 if (bus->link[link].reset_needed) {
578 if (reset_bus(&bus->link[link])) {
579 do_scan_bus = 1;
580 } else {
581 bus->bus->reset_needed = 1;
582 }
583 }
584 }
585 }
586 return new_max;
587}
588
589
Li-Ta Loe5266692004-03-23 21:28:05 +0000590/**
Li-Ta Lo04930692004-11-25 17:37:19 +0000591 * @brief Determine the existence of devices and extend the device tree.
592 *
593 * Most of the devices in the system are listed in the mainboard Config.lb
594 * file. The device structures for these devices are generated at compile
595 * time by the config tool and are organized into the device tree. This
596 * function determines if the devices created at compile time actually exist
597 * in the physical system.
598 *
599 * For devices in the physical system but not listed in the Config.lb file,
600 * the device structures have to be created at run time and attached to the
Li-Ta Loe5266692004-03-23 21:28:05 +0000601 * device tree.
602 *
Li-Ta Lo04930692004-11-25 17:37:19 +0000603 * This function starts from the root device 'dev_root', scan the buses in
604 * the system recursively, modify the device tree according to the result of
605 * the probe.
Li-Ta Loe5266692004-03-23 21:28:05 +0000606 *
Li-Ta Lo5782d272004-04-26 17:51:20 +0000607 * This function has no idea how to scan and probe buses and devices at all.
608 * It depends on the bus/device specific scan_bus() method to do it. The
Li-Ta Lo04930692004-11-25 17:37:19 +0000609 * scan_bus() method also has to create the device structure and attach
Li-Ta Loe5266692004-03-23 21:28:05 +0000610 * it to the device tree.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000611 */
612void dev_enumerate(void)
613{
614 struct device *root;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000615 unsigned subordinate;
Li-Ta Loe5266692004-03-23 21:28:05 +0000616 printk_info("Enumerating buses...\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000617 root = &dev_root;
Eric Biederman7003ba42004-10-16 06:20:29 +0000618 if (root->chip_ops && root->chip_ops->enable_dev) {
619 root->chip_ops->enable_dev(root);
620 }
Eric Biedermanb78c1972004-10-14 20:54:17 +0000621 if (!root->ops || !root->ops->scan_bus) {
622 printk_err("dev_root missing scan_bus operation");
623 return;
624 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000625 subordinate = scan_bus(root, 0);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000626 printk_info("done\n");
627}
628
Li-Ta Loe5266692004-03-23 21:28:05 +0000629/**
630 * @brief Configure devices on the devices tree.
631 *
Li-Ta Lo04930692004-11-25 17:37:19 +0000632 * Starting at the root of the device tree, travel it recursively in two
633 * passes. In the first pass, we compute and allocate resources (ranges)
634 * requried by each device. In the second pass, the resources ranges are
635 * relocated to their final position and stored to the hardware.
Li-Ta Lo5782d272004-04-26 17:51:20 +0000636 *
637 * I/O resources start at DEVICE_IO_START and grow upward. MEM resources start
Stefan Reinauer51754a32008-08-01 12:28:38 +0000638 * at DEVICE_MEM_HIGH and grow downward.
Li-Ta Lo5782d272004-04-26 17:51:20 +0000639 *
640 * Since the assignment is hierarchical we set the values into the dev_root
641 * struct.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000642 */
643void dev_configure(void)
644{
Eric Biederman03acab62004-10-14 21:25:53 +0000645 struct resource *io, *mem;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000646 struct device *root;
Li-Ta Loe5266692004-03-23 21:28:05 +0000647
Li-Ta Lo54f05f62004-05-14 17:20:29 +0000648 printk_info("Allocating resources...\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000649
Eric Biedermanb78c1972004-10-14 20:54:17 +0000650 root = &dev_root;
651 if (!root->ops || !root->ops->read_resources) {
652 printk_err("dev_root missing read_resources\n");
653 return;
654 }
655 if (!root->ops || !root->ops->set_resources) {
656 printk_err("dev_root missing set_resources\n");
657 return;
658 }
Li-Ta Lo04930692004-11-25 17:37:19 +0000659
660 printk_info("Reading resources...\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000661 root->ops->read_resources(root);
Li-Ta Lo3a812852004-12-03 22:39:34 +0000662 printk_info("Done reading resources.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000663
Eric Biederman03acab62004-10-14 21:25:53 +0000664 /* Get the resources */
665 io = &root->resource[0];
666 mem = &root->resource[1];
Li-Ta Loe5266692004-03-23 21:28:05 +0000667 /* Make certain the io devices are allocated somewhere safe. */
Eric Biederman03acab62004-10-14 21:25:53 +0000668 io->base = DEVICE_IO_START;
669 io->flags |= IORESOURCE_ASSIGNED;
670 io->flags &= ~IORESOURCE_STORED;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000671 /* Now reallocate the pci resources memory with the
672 * highest addresses I can manage.
673 */
Eric Biederman03acab62004-10-14 21:25:53 +0000674 mem->base = resource_max(&root->resource[1]);
675 mem->flags |= IORESOURCE_ASSIGNED;
676 mem->flags &= ~IORESOURCE_STORED;
Eric Biederman5cd81732004-03-11 15:01:31 +0000677
Yinghai Lu9e4faef2005-01-14 22:04:49 +0000678#if CONFIG_CONSOLE_VGA == 1
Li-Ta Loe5266692004-03-23 21:28:05 +0000679 /* Allocate the VGA I/O resource.. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000680 allocate_vga_resource();
Yinghai Lu9e4faef2005-01-14 22:04:49 +0000681#endif
Eric Biederman5cd81732004-03-11 15:01:31 +0000682
Eric Biedermanb78c1972004-10-14 20:54:17 +0000683 /* Store the computed resource allocations into device registers ... */
Li-Ta Lo04930692004-11-25 17:37:19 +0000684 printk_info("Setting resources...\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000685 root->ops->set_resources(root);
Li-Ta Lo3a812852004-12-03 22:39:34 +0000686 printk_info("Done setting resources.\n");
Eric Biederman03acab62004-10-14 21:25:53 +0000687#if 0
688 mem->flags |= IORESOURCE_STORED;
689 report_resource_stored(root, mem, "");
690#endif
691
Li-Ta Lo3a812852004-12-03 22:39:34 +0000692 printk_info("Done allocating resources.\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000693}
694
Li-Ta Loe5266692004-03-23 21:28:05 +0000695/**
696 * @brief Enable devices on the device tree.
697 *
698 * Starting at the root, walk the tree and enable all devices/bridges by
699 * calling the device's enable_resources() method.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000700 */
701void dev_enable(void)
702{
Stefan Reinauerdba3f842006-03-17 22:48:23 +0000703 printk_info("Enabling resources...\n");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000704
705 /* now enable everything. */
706 enable_resources(&dev_root);
Li-Ta Loe5266692004-03-23 21:28:05 +0000707
Eric Biederman8ca8d762003-04-22 19:02:15 +0000708 printk_info("done.\n");
709}
710
Li-Ta Loe5266692004-03-23 21:28:05 +0000711/**
712 * @brief Initialize all devices in the global device list.
713 *
714 * Starting at the first device on the global device link list,
Li-Ta Lo04930692004-11-25 17:37:19 +0000715 * walk the list and call the device's init() method to do deivce
716 * specific setup.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000717 */
718void dev_initialize(void)
719{
720 struct device *dev;
721
722 printk_info("Initializing devices...\n");
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000723 for(dev = all_devices; dev; dev = dev->next) {
724 if (dev->enabled && !dev->initialized &&
725 dev->ops && dev->ops->init)
726 {
727 if (dev->path.type == DEVICE_PATH_I2C) {
728 printk_debug("smbus: %s[%d]->",
729 dev_path(dev->bus->dev), dev->bus->link);
730 }
731 printk_debug("%s init\n", dev_path(dev));
732 dev->initialized = 1;
733 dev->ops->init(dev);
734 }
735 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000736 printk_info("Devices initialized\n");
737}
Eric Biederman448bd632004-10-14 22:52:15 +0000738