- Cleanups on the romcc side including a pci interface that uses
[coreboot.git] / src / devices / pci_device.c
1 /*
2  *      PCI Bus Services, see include/linux/pci.h for further explanation.
3  *
4  *      Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter,
5  *      David Mosberger-Tang
6  *
7  *      Copyright 1997 -- 1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
8  *      
9  *      Copyright 2003 -- Eric Biederman <ebiederman@lnxi.com>
10  */
11
12 #include <console/console.h>
13 #include <stdlib.h>
14 #include <stdint.h>
15 #include <bitops.h>
16 #include <string.h>
17 #include <device/device.h>
18 #include <device/pci.h>
19 #include <device/pci_ids.h>
20
21 static unsigned int pci_scan_bridge(struct device *bus, unsigned int max);
22
23 /** Given a device and register, read the size of the BAR for that register. 
24  * @param dev       Pointer to the device structure
25  * @param resource  Pointer to the resource structure
26  * @param index     Address of the pci configuration register
27  */
28 static void pci_get_resource(struct device *dev, struct resource *resource, unsigned long index)
29 {
30         uint32_t addr, size, base;
31         unsigned long type;
32
33         /* Initialize the resources to nothing */
34         resource->base = 0;
35         resource->size = 0;
36         resource->align = 0;
37         resource->gran = 0;
38         resource->limit = 0;
39         resource->flags = 0;
40         resource->index = index;
41
42         pci_read_config_dword(dev, index, &addr);
43         if (addr == 0xffffffffUL)
44                 return;
45
46         /* FIXME: more consideration for 64-bit PCI devices,
47          * we currently detect their size but otherwise
48          * treat them as 32-bit resources
49          */
50         /* get the size */
51         pci_write_config_dword(dev, index, ~0);
52         pci_read_config_dword(dev,  index, &size);
53
54         /* get the minimum value the bar can be set to */
55         pci_write_config_dword(dev, index, 0);
56         pci_read_config_dword(dev, index, &base);
57
58         /* restore addr */
59         pci_write_config_dword(dev, index, addr);
60
61         /*
62          * some broken hardware has read-only registers that do not 
63          * really size correctly. You can tell this if addr == size
64          * Example: the acer m7229 has BARs 1-4 normally read-only. 
65          * so BAR1 at offset 0x10 reads 0x1f1. If you size that register
66          * by writing 0xffffffff to it, it will read back as 0x1f1 -- a 
67          * violation of the spec. 
68          * We catch this case and ignore it by settting size and type to 0.
69          * This incidentally catches the common case where registers 
70          * read back as 0 for both address and size. 
71          */
72         if ((addr == size) && (addr == base)) {
73                 if (size != 0) {
74                         printk_debug(
75                                 "PCI: %02x:%02x.%01x register %02x(%08x), read-only ignoring it\n",
76                                 dev->bus->secondary,
77                                 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn), 
78                                 index, addr);
79                 }
80                 resource->flags = 0;
81         }
82         /* Now compute the actual size, See PCI Spec 6.2.5.1 ...  */
83         else if (size & PCI_BASE_ADDRESS_SPACE_IO) {
84                 type = size & (~PCI_BASE_ADDRESS_IO_MASK);
85                 /* BUG! Top 16 bits can be zero (or not) 
86                  * So set them to 0xffff so they go away ...
87                  */
88                 resource->size = (~((size | 0xffff0000) & PCI_BASE_ADDRESS_IO_MASK)) +1;
89                 resource->align = log2(resource->size);
90                 resource->gran = resource->align;
91                 resource->flags = IORESOURCE_IO;
92                 resource->limit = 0xffff;
93         } 
94         else {
95                 /* A Memory mapped base address */
96                 type = size & (~PCI_BASE_ADDRESS_MEM_MASK);
97                 resource->size = (~(size &PCI_BASE_ADDRESS_MEM_MASK)) +1;
98                 resource->align = log2(resource->size);
99                 resource->gran = resource->align;
100                 resource->flags = IORESOURCE_MEM;
101                 if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
102                         resource->flags |= IORESOURCE_PREFETCH;
103                 }
104                 type &= PCI_BASE_ADDRESS_MEM_TYPE_MASK;
105                 if (type == PCI_BASE_ADDRESS_MEM_TYPE_32) {
106                         /* 32bit limit */
107                         resource->limit = 0xffffffffUL;
108                 }
109                 else if (type == PCI_BASE_ADDRESS_MEM_TYPE_1M) {
110                         /* 1MB limit */
111                         resource->limit = 0x000fffffUL;
112                 }
113                 else if (type == PCI_BASE_ADDRESS_MEM_TYPE_64) {
114                         unsigned long index_hi;
115                         /* 64bit limit 
116                          * For now just treat this as a 32bit limit
117                          */
118                         index_hi = index + 4;
119                         resource->limit = 0xffffffffUL;
120                         resource->flags |= IORESOURCE_PCI64;
121                         pci_read_config_dword( dev, index_hi, &addr);
122                         /* get the extended size */
123                         pci_write_config_dword(dev, index_hi, 0xffffffffUL);
124                         pci_read_config_dword( dev, index_hi, &size);
125
126                         /* get the minimum value the bar can be set to */
127                         pci_write_config_dword(dev, index_hi, 0);
128                         pci_read_config_dword(dev,  index_hi, &base);
129
130                         /* restore addr */
131                         pci_write_config_dword(dev, index_hi, addr);
132                         
133                         if ((size == 0xffffffff) && (base == 0)) {
134                                 /* Clear the top half of the bar */
135                                 pci_write_config_dword(dev, index_hi, 0);
136                         }
137                         else {
138                                 printk_err("PCI: %02x:%02x.%01x Unable to handle 64-bit address\n",
139                                         dev->bus->secondary, 
140                                         PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
141                                 resource->flags = IORESOURCE_PCI64;
142                         }
143                 } 
144                 else {
145                         /* Invalid value */
146                         resource->flags = 0;
147                 }
148         }
149         /* dev->size holds the flags... */
150         return;
151 }
152
153 /** Read the base address registers for a given device. 
154  * @param dev Pointer to the dev structure
155  * @param howmany How many registers to read (6 for device, 2 for bridge)
156  */
157 static void pci_read_bases(struct device *dev, unsigned int howmany)
158 {
159         unsigned int reg;
160         unsigned long index;
161
162         reg = dev->resources;
163         for(index = PCI_BASE_ADDRESS_0; 
164             (reg < MAX_RESOURCES) && (index < PCI_BASE_ADDRESS_0 + (howmany << 2)); ) {
165                 struct resource *resource;
166                 resource = &dev->resource[reg];
167                 pci_get_resource(dev, resource, index);
168                 reg += (resource->flags & (IORESOURCE_IO | IORESOURCE_MEM))? 1:0;
169                 index += (resource->flags & IORESOURCE_PCI64)?8:4;
170         }
171         dev->resources = reg;
172 }
173
174
175 static void pci_bridge_read_bases(struct device *dev)
176 {
177         unsigned int reg = dev->resources;
178
179         /* FIXME handle bridges without some of the optional resources */
180
181         /* Initialize the io space constraints on the current bus */
182         dev->resource[reg].base  = 0;
183         dev->resource[reg].size  = 0;
184         dev->resource[reg].align = log2(PCI_IO_BRIDGE_ALIGN);
185         dev->resource[reg].gran  = log2(PCI_IO_BRIDGE_ALIGN);
186         dev->resource[reg].limit = 0xffffUL;
187         dev->resource[reg].flags = IORESOURCE_IO | IORESOURCE_PCI_BRIDGE;
188         dev->resource[reg].index = PCI_IO_BASE;
189         compute_allocate_resource(dev, &dev->resource[reg],
190                 IORESOURCE_IO, IORESOURCE_IO);
191         reg++;
192
193         /* Initiliaze the prefetchable memory constraints on the current bus */
194         dev->resource[reg].base = 0;
195         dev->resource[reg].size = 0;
196         dev->resource[reg].align = log2(PCI_MEM_BRIDGE_ALIGN);
197         dev->resource[reg].gran  = log2(PCI_MEM_BRIDGE_ALIGN);
198         dev->resource[reg].limit = 0xffffffffUL;
199         dev->resource[reg].flags = IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_PCI_BRIDGE;
200         dev->resource[reg].index = PCI_PREF_MEMORY_BASE;
201         compute_allocate_resource(dev, &dev->resource[reg],
202                 IORESOURCE_MEM | IORESOURCE_PREFETCH, 
203                 IORESOURCE_MEM | IORESOURCE_PREFETCH);
204         reg++;
205
206         /* Initialize the memory resources on the current bus */
207         dev->resource[reg].base = 0;
208         dev->resource[reg].size = 0;
209         dev->resource[reg].align = log2(PCI_MEM_BRIDGE_ALIGN);
210         dev->resource[reg].gran  = log2(PCI_MEM_BRIDGE_ALIGN);
211         dev->resource[reg].limit = 0xffffffffUL;
212         dev->resource[reg].flags = IORESOURCE_MEM | IORESOURCE_PCI_BRIDGE;
213         dev->resource[reg].index = PCI_MEMORY_BASE;
214         compute_allocate_resource(dev, &dev->resource[reg],
215                 IORESOURCE_MEM | IORESOURCE_PREFETCH, 
216                 IORESOURCE_MEM);
217         reg++;
218
219         dev->resources = reg;
220 }
221
222
223 void pci_dev_read_resources(struct device *dev)
224 {
225         uint32_t addr;
226         dev->resources = 0;
227         memset(&dev->resource[0], 0, sizeof(dev->resource));
228         pci_read_bases(dev, 6);
229         pci_read_config_dword(dev, PCI_ROM_ADDRESS, &addr);
230         dev->rom_address = (addr == 0xffffffff)? 0 : addr;
231 }
232
233 void pci_bus_read_resources(struct device *dev)
234 {
235         uint32_t addr;
236         dev->resources = 0;
237         memset(&dev->resource[0], 0, sizeof(dev->resource));
238         pci_bridge_read_bases(dev);
239         pci_read_bases(dev, 2);
240         
241         pci_read_config_dword(dev, PCI_ROM_ADDRESS1, &addr);
242         dev->rom_address = (addr == 0xffffffff)? 0 : addr;
243
244 }
245
246
247 static void pci_set_resource(struct device *dev, struct resource *resource)
248 {
249         unsigned long base, limit;
250         unsigned long bridge_align = PCI_MEM_BRIDGE_ALIGN;
251         unsigned char buf[10];
252         
253         /* Make certain the resource has actually been set */
254         if (!(resource->flags & IORESOURCE_SET)) {
255 #if 1
256                 printk_err("ERROR: %02x:%02x.%01x %02x not allocated\n",
257                         dev->bus->secondary,
258                         PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn), 
259                         resource->index);
260 #endif
261                 return;
262         }
263
264         /* Only handle PCI memory and IO resources for now */
265         if (!(resource->flags & (IORESOURCE_MEM |IORESOURCE_IO)))
266                 return;
267         
268         if (resource->flags & IORESOURCE_MEM) {
269                 dev->command |= PCI_COMMAND_MEMORY;
270                 bridge_align = PCI_MEM_BRIDGE_ALIGN;
271         }
272         if (resource->flags & IORESOURCE_IO) {
273                 dev->command |= PCI_COMMAND_IO;
274                 bridge_align = PCI_IO_BRIDGE_ALIGN;
275         }
276         if (resource->flags & IORESOURCE_PCI_BRIDGE) {
277                 dev->command |= PCI_COMMAND_MASTER;
278         }
279         /* Get the base address */
280         base = resource->base;
281         
282         /* Get the limit (rounded up) */
283         limit = base + ((resource->size + bridge_align - 1UL) & ~(bridge_align -1)) -1UL;
284         
285         if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) {
286                 /*
287                  * some chipsets allow us to set/clear the IO bit. 
288                  * (e.g. VIA 82c686a.) So set it to be safe)
289                  */
290                 limit = base + resource->size -1;
291                 if (resource->flags & IORESOURCE_IO) {
292                         base |= PCI_BASE_ADDRESS_SPACE_IO;
293                 }
294                 pci_write_config_dword(dev, resource->index, base & 0xffffffff);
295                 if (resource->flags & IORESOURCE_PCI64) {
296                         /* FIXME handle real 64bit base addresses */
297                         pci_write_config_dword(dev, resource->index + 4, 0);
298                 }
299         }
300         else if (resource->index == PCI_IO_BASE) {
301                 /* set the IO ranges
302                  * WARNING: we don't really do 32-bit addressing for IO yet! 
303                  */
304                 compute_allocate_resource(dev, resource, 
305                         IORESOURCE_IO, IORESOURCE_IO);
306                 pci_write_config_byte(dev, PCI_IO_BASE,  base >> 8);
307                 pci_write_config_byte(dev, PCI_IO_LIMIT, limit >> 8);
308         }
309         else if (resource->index == PCI_MEMORY_BASE) {
310                 /* set the memory range
311                  */
312                 compute_allocate_resource(dev, resource,
313                         IORESOURCE_MEM | IORESOURCE_PREFETCH, 
314                         IORESOURCE_MEM);
315                 pci_write_config_word(dev, PCI_MEMORY_BASE, base >> 16);
316                 pci_write_config_word(dev, PCI_MEMORY_LIMIT, limit >> 16);
317         }
318         else if (resource->index == PCI_PREF_MEMORY_BASE) {
319                 /* set the prefetchable memory range
320                  * WARNING: we don't really do 64-bit addressing for prefetchable memory yet!
321                  */
322                 compute_allocate_resource(dev, resource,
323                         IORESOURCE_MEM | IORESOURCE_PREFETCH, 
324                         IORESOURCE_MEM | IORESOURCE_PREFETCH);
325                 pci_write_config_word(dev, PCI_PREF_MEMORY_BASE,  base >> 16);
326                 pci_write_config_word(dev, PCI_PREF_MEMORY_LIMIT, limit >> 16);
327         }
328         else {
329                 printk_err("ERROR: invalid resource->index %x\n",
330                         resource->index);
331         }
332         buf[0] = '\0';
333         if (resource->flags & IORESOURCE_PCI_BRIDGE) {
334                 sprintf(buf, "bus %d ", dev->secondary);
335         }
336         
337         printk_debug(
338                 "PCI: %02x:%02x.%01x %02x <- [0x%08lx - 0x%08lx] %s%s\n",
339                 dev->bus->secondary, 
340                 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn), 
341                 resource->index, 
342                 resource->base, limit,
343                 buf,
344                 (resource->flags & IORESOURCE_IO)? "io":
345                 (resource->flags & IORESOURCE_PREFETCH)? "prefmem": "mem");
346         return;
347 }
348
349 void pci_dev_set_resources(struct device *dev)
350 {
351         struct resource *resource, *last;
352         uint8_t line;
353
354         last = &dev->resource[dev->resources];
355
356         for(resource = &dev->resource[0]; resource < last; resource++) {
357                 pci_set_resource(dev, resource);
358         }
359         if (dev->children) {
360                 assign_resources(dev);
361         }
362
363         /* set a default latency timer */
364         pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0x40);
365
366         /* set a default secondary latency timer */
367         if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
368                 pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER, 0x40);
369         }
370
371         /* zero the irq settings */
372         pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &line);
373         if (line) {
374                 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, 0);
375         }
376         /* set the cache line size, so far 64 bytes is good for everyone */
377         pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, 64 >> 2);
378 }
379
380 struct device_operations default_pci_ops_dev = {
381         .read_resources = pci_dev_read_resources,
382         .set_resources = pci_dev_set_resources,
383         .init = 0,
384         .scan_bus = 0,
385 };
386 struct device_operations default_pci_ops_bus = {
387         .read_resources = pci_bus_read_resources,
388         .set_resources = pci_dev_set_resources,
389         .init = 0,
390         .scan_bus = pci_scan_bridge,
391 };
392 static void set_pci_ops(struct device *dev)
393 {
394         struct pci_driver *driver;
395         if (dev->ops) {
396                 return;
397         }
398         /* Look through the list of setup drivers and find one for
399          * this pci device 
400          */
401         for(driver = &pci_drivers[0]; driver != &epci_drivers[0]; driver++) {
402                 if ((driver->vendor == dev->vendor) &&
403                         (driver->device == dev->device)) {
404                         dev->ops = driver->ops;
405 #if 1
406                         printk_debug("PCI: %02x:%02x.%01x [%04x/%04x] ops\n",
407                                 dev->bus->secondary,
408                                 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
409                                 driver->vendor, driver->device
410                                 );
411 #endif
412                         return;
413                 }
414         }
415         /* If I don't have a specific driver use the default operations */
416         switch(dev->hdr_type & 0x7f) {  /* header type */
417         case PCI_HEADER_TYPE_NORMAL:    /* standard header */
418                 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
419                         goto bad;
420                 dev->ops = &default_pci_ops_dev;
421                 break;
422         case PCI_HEADER_TYPE_BRIDGE:
423                 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
424                         goto bad;
425                 dev->ops = &default_pci_ops_bus;
426                 break;
427         default:
428         bad:
429                 printk_err("PCI: %02x:%02x.%01x [%04x/%04x/%06x] has unknown header "
430                         "type %02x, ignoring.\n",
431                         dev->bus->secondary, 
432                         PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn), 
433                         dev->vendor, dev->device, 
434                         dev->class >> 8, dev->hdr_type);
435         }
436         return;
437 }
438
439 /**
440  * Given a bus and a devfn number, find the device structure
441  * @param bus The bus structure
442  * @param devfn a device/function number
443  * @return pointer to the device structure
444  */
445 static struct device *pci_scan_get_dev(struct device **list, unsigned int devfn)
446 {
447         struct device *dev = 0;
448         for(; *list; list = &(*list)->sibling) {
449                 if ((*list)->devfn == devfn) {
450                         /* Unlink from the list */
451                         dev = *list;
452                         *list = (*list)->sibling;
453                         dev->sibling = 0;
454                         break;
455                 }
456         }
457         return dev;
458 }
459
460
461 #define HYPERTRANSPORT_SUPPORT 1
462 /** Scan the pci bus devices and bridges.
463  * @param pci_bus pointer to the bus structure
464  * @param max current bus number
465  * @return The maximum bus number found, after scanning all subordinate busses
466  */
467 unsigned int pci_scan_bus(struct device *bus, unsigned int max)
468 {
469         unsigned int devfn;
470         struct device *dev, **bus_last;
471         struct device *old_devices;
472         struct device *child;
473 #if HYPERTRANSPORT_SUPPORT
474         unsigned next_unitid, last_unitid;
475 #endif
476
477         printk_debug("PCI: pci_scan_bus for bus %d\n", bus->secondary);
478
479         old_devices = bus->children;
480         bus->children = 0;
481         bus_last = &bus->children;
482
483         post_code(0x24);
484         
485
486 #if HYPERTRANSPORT_SUPPORT
487         /* If present assign unitid to a hypertransport chain */
488         next_unitid = 1;
489         do {
490                 struct device dummy;
491                 uint32_t id;
492                 uint8_t hdr_type, pos;
493                 last_unitid = next_unitid;
494
495                 dummy.bus   = bus;
496                 dummy.devfn = 0;
497                 pci_read_config_dword(&dummy, PCI_VENDOR_ID, &id);
498                 if (id == 0xffffffff || id == 0x00000000 ||
499                         id == 0x0000ffff || id == 0xffff0000) {
500                         break;
501                 }
502                 pci_read_config_byte(&dummy, PCI_HEADER_TYPE, &hdr_type);
503                 pos = 0;
504                 switch(hdr_type & 0x7f) {
505                 case PCI_HEADER_TYPE_NORMAL:
506                 case PCI_HEADER_TYPE_BRIDGE:
507                         pos = PCI_CAPABILITY_LIST;
508                         break;
509                 }
510                 if (pos > PCI_CAP_LIST_NEXT) {
511                         pci_read_config_byte(&dummy, pos, &pos);
512                 }
513                 while(pos != 0) {
514                         uint8_t cap;
515                         pci_read_config_byte(&dummy, pos + PCI_CAP_LIST_ID, &cap);
516                         printk_debug("Capability: 0x%02x @ 0x%02x\n", cap, pos);
517                         if (cap == PCI_CAP_ID_HT) {
518                                 uint16_t flags;
519                                 pci_read_config_word(&dummy, pos + PCI_CAP_FLAGS, &flags);
520                                 printk_debug("flags: 0x%04x\n", (unsigned)flags);
521                                 if ((flags >> 13) == 0) {
522                                         unsigned count;
523                                         flags &= ~0x1f;
524                                         flags |= next_unitid & 0x1f;
525                                         count = (flags >> 5) & 0x1f;
526                                         printk_debug("unitid: 0x%02x, count: 0x%02x\n",
527                                                 next_unitid, count);
528                                         pci_write_config_word(&dummy, pos + PCI_CAP_FLAGS, flags);
529                                         next_unitid += count;
530                                         break;
531                                 }
532                         }
533                         pci_read_config_byte(&dummy, pos + PCI_CAP_LIST_NEXT, &pos);
534                 }
535         } while((last_unitid != next_unitid) && (next_unitid <= 0x1f));
536 #endif /* HYPERTRANSPORT_SUPPORT */
537
538         /* probe all devices on this bus with some optimization for non-existance and 
539            single funcion devices */
540         for (devfn = 0; devfn <= 0xff; devfn++) {
541                 struct device dummy;
542                 uint32_t id, class;
543                 uint8_t cmd, tmp, hdr_type;
544
545                 /* First thing setup the device structure */
546                 dev = pci_scan_get_dev(&old_devices, devfn);
547         
548                 dummy.bus = bus;
549                 dummy.devfn = devfn;
550                 pci_read_config_dword(&dummy, PCI_VENDOR_ID, &id);
551                 /* some broken boards return 0 if a slot is empty: */
552                 if (!dev &&
553                         (id == 0xffffffff || id == 0x00000000 || 
554                          id == 0x0000ffff || id == 0xffff0000)) {
555                         printk_spew("PCI: devfn 0x%x, bad id 0x%x\n", devfn, id);
556                         if (PCI_FUNC(devfn) == 0x00) {
557                                 /* if this is a function 0 device and it is not present,
558                                    skip to next device */
559                                 devfn += 0x07;
560                         }
561                         /* multi function device, skip to next function */
562                         continue;
563                 }
564                 pci_read_config_byte(&dummy, PCI_HEADER_TYPE, &hdr_type);
565                 pci_read_config_dword(&dummy, PCI_CLASS_REVISION, &class);
566
567                 if (!dev) {
568                         if ((dev = malloc(sizeof(*dev))) == 0) {
569                                 printk_err("PCI: out of memory.\n");
570                                 continue;
571                         }
572                         memset(dev, 0, sizeof(*dev));
573                 }
574
575                 dev->bus = bus;
576                 dev->devfn = devfn;
577                 dev->vendor = id & 0xffff;
578                 dev->device = (id >> 16) & 0xffff;
579                 dev->hdr_type = hdr_type;
580                 /* class code, the upper 3 bytes of PCI_CLASS_REVISION */
581                 dev->class = class >> 8;
582
583                 /* non-destructively determine if device can be a master: */
584                 pci_read_config_byte(dev, PCI_COMMAND, &cmd);
585                 pci_write_config_byte(dev, PCI_COMMAND, cmd | PCI_COMMAND_MASTER);
586                 pci_read_config_byte(dev, PCI_COMMAND, &tmp);
587
588                 dev->master = ((tmp & PCI_COMMAND_MASTER) != 0);
589                 pci_write_config_byte(dev, PCI_COMMAND, cmd);
590
591                 /* Look at the vendor and device id, or at least the 
592                  * header type and class and figure out which set of configuration
593                  * methods to use.
594                  */
595                 set_pci_ops(dev);
596                 /* Kill the device if we don't have some pci operations for it */
597                 if (!dev->ops) {
598                         free(dev);
599                         continue;
600                 }
601                 printk_debug("PCI: %02x:%02x.%01x [%04x/%04x]\n", 
602                         bus->secondary, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn), 
603                         dev->vendor, dev->device);
604
605                 /* Put it into the global device chain. */
606                 append_device(dev);
607
608                 /* Now insert it into the list of devices held by the parent bus. */
609                 *bus_last = dev;
610                 bus_last = &dev->sibling;
611
612                 if (PCI_FUNC(devfn) == 0x00 && (hdr_type & 0x80) != 0x80) {
613                         /* if this is not a multi function device, don't waste time probe
614                            another function. Skip to next device. */
615                         devfn += 0x07;
616                 }
617         }
618         post_code(0x25);
619
620         for(child = bus->children; child; child = child->sibling) {
621                 if (!child->ops->scan_bus)
622                         continue;
623                 max = child->ops->scan_bus(child, max);
624                 
625         }
626         /*
627          * We've scanned the bus and so we know all about what's on
628          * the other side of any bridges that may be on this bus plus
629          * any devices.
630          *
631          * Return how far we've got finding sub-buses.
632          */
633         printk_debug("PCI: pci_scan_bus returning with max=%02x\n", max);
634         post_code(0x55);
635         return max;
636 }
637
638 /** Scan the bus, first for bridges and next for devices. 
639  * @param pci_bus pointer to the bus structure
640  * @return The maximum bus number found, after scanning all subordinate busses
641  */
642 unsigned int pci_scan_bridge(struct device *bus, unsigned int max)
643 {
644         uint32_t buses;
645         uint16_t cr;
646         /* Set up the primary, secondary and subordinate bus numbers. We have
647          * no idea how many buses are behind this bridge yet, so we set the
648          * subordinate bus number to 0xff for the moment 
649          */
650         bus->secondary = ++max;
651         bus->subordinate = 0xff;
652         
653         /* Clear all status bits and turn off memory, I/O and master enables. */
654         pci_read_config_word(bus, PCI_COMMAND, &cr);
655         pci_write_config_word(bus, PCI_COMMAND, 0x0000);
656         pci_write_config_word(bus, PCI_STATUS, 0xffff);
657
658         /*
659          * Read the existing primary/secondary/subordinate bus
660          * number configuration.
661          */
662         pci_read_config_dword(bus, PCI_PRIMARY_BUS, &buses);
663
664         /* Configure the bus numbers for this bridge: the configuration
665          * transactions will not be propagated by the bridge if it is not
666          * correctly configured 
667          */
668         buses &= 0xff000000;
669         buses |= (((unsigned int) (bus->bus->secondary) << 0) |
670                 ((unsigned int) (bus->secondary) << 8) |
671                 ((unsigned int) (bus->subordinate) << 16));
672         pci_write_config_dword(bus, PCI_PRIMARY_BUS, buses);
673         
674         /* Now we can scan all subordinate buses i.e. the bus hehind the bridge */
675         max = pci_scan_bus(bus, max);
676         
677         /* We know the number of buses behind this bridge. Set the subordinate
678          *  bus number to its real value 
679          */
680         bus->subordinate = max;
681         buses = (buses & 0xff00ffff) |
682                 ((unsigned int) (bus->subordinate) << 16);
683         pci_write_config_dword(bus, PCI_PRIMARY_BUS, buses);
684         pci_write_config_word(bus, PCI_COMMAND, cr);
685                 
686         return max;
687 }
688
689
690 static void pci_root_read_resources(struct device *bus)
691 {
692         int res = 0;
693         /* Initialize the system wide io space constraints */
694         bus->resource[res].base  = 0x400;
695         bus->resource[res].size  = 0;
696         bus->resource[res].align = 0;
697         bus->resource[res].gran  = 0;
698         bus->resource[res].limit = 0xffffUL;
699         bus->resource[res].flags = IORESOURCE_IO;
700         bus->resource[res].index = PCI_IO_BASE;
701         compute_allocate_resource(bus, &bus->resource[res], 
702                 IORESOURCE_IO, IORESOURCE_IO);
703         res++;
704
705         /* Initialize the system wide memory resources constraints */
706         bus->resource[res].base  = 0;
707         bus->resource[res].size  = 0;
708         bus->resource[res].align = 0;
709         bus->resource[res].gran  = 0;
710         bus->resource[res].limit = 0xffffffffUL;
711         bus->resource[res].flags = IORESOURCE_MEM;
712         bus->resource[res].index = PCI_MEMORY_BASE;
713         compute_allocate_resource(bus, &bus->resource[res], 
714                 IORESOURCE_MEM, IORESOURCE_MEM);
715         res++;
716
717         bus->resources = res;
718 }
719 static void pci_root_set_resources(struct device *bus)
720 {
721         compute_allocate_resource(bus,
722                 &bus->resource[0], IORESOURCE_IO, IORESOURCE_IO);
723         compute_allocate_resource(bus, 
724                 &bus->resource[1], IORESOURCE_MEM, IORESOURCE_MEM);
725         assign_resources(bus);
726 }
727
728 struct device_operations default_pci_ops_root = {
729         .read_resources = pci_root_read_resources,
730         .set_resources = pci_root_set_resources,
731         .init = 0,
732         .scan_bus = pci_scan_bus,
733 };
734