blob: 4db01027459996a53e8502762cf0dd28ac1a57dd [file] [log] [blame]
Eric Biederman8ca8d762003-04-22 19:02:15 +00001/*
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>
Eric Biederman8ca8d762003-04-22 19:02:15 +000016#include <string.h>
Eric Biederman5899fd82003-04-24 06:25:08 +000017#include <device/device.h>
18#include <device/pci.h>
19#include <device/pci_ids.h>
Eric Biedermane9a271e32003-09-02 03:36:25 +000020#include <part/hard_reset.h>
Eric Biederman30e143a2003-09-01 23:45:32 +000021#include <part/fallback_boot.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000022
Eric Biederman8ca8d762003-04-22 19:02:15 +000023/** 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 */
28static 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
Eric Biederman7a5416a2003-06-12 19:23:51 +000042 addr = pci_read_config32(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +000043
44 /* FIXME: more consideration for 64-bit PCI devices,
45 * we currently detect their size but otherwise
46 * treat them as 32-bit resources
47 */
48 /* get the size */
Eric Biederman7a5416a2003-06-12 19:23:51 +000049 pci_write_config32(dev, index, ~0);
50 size = pci_read_config32(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +000051
52 /* get the minimum value the bar can be set to */
Eric Biederman7a5416a2003-06-12 19:23:51 +000053 pci_write_config32(dev, index, 0);
54 base = pci_read_config32(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +000055
56 /* restore addr */
Eric Biederman7a5416a2003-06-12 19:23:51 +000057 pci_write_config32(dev, index, addr);
Eric Biederman8ca8d762003-04-22 19:02:15 +000058
59 /*
60 * some broken hardware has read-only registers that do not
61 * really size correctly. You can tell this if addr == size
62 * Example: the acer m7229 has BARs 1-4 normally read-only.
63 * so BAR1 at offset 0x10 reads 0x1f1. If you size that register
64 * by writing 0xffffffff to it, it will read back as 0x1f1 -- a
65 * violation of the spec.
66 * We catch this case and ignore it by settting size and type to 0.
67 * This incidentally catches the common case where registers
68 * read back as 0 for both address and size.
69 */
70 if ((addr == size) && (addr == base)) {
71 if (size != 0) {
72 printk_debug(
Eric Biedermane9a271e32003-09-02 03:36:25 +000073 "%s register %02x(%08x), read-only ignoring it\n",
74 dev_path(dev),
Eric Biederman8ca8d762003-04-22 19:02:15 +000075 index, addr);
76 }
77 resource->flags = 0;
78 }
79 /* Now compute the actual size, See PCI Spec 6.2.5.1 ... */
80 else if (size & PCI_BASE_ADDRESS_SPACE_IO) {
81 type = size & (~PCI_BASE_ADDRESS_IO_MASK);
82 /* BUG! Top 16 bits can be zero (or not)
83 * So set them to 0xffff so they go away ...
84 */
85 resource->size = (~((size | 0xffff0000) & PCI_BASE_ADDRESS_IO_MASK)) +1;
86 resource->align = log2(resource->size);
87 resource->gran = resource->align;
88 resource->flags = IORESOURCE_IO;
89 resource->limit = 0xffff;
90 }
91 else {
92 /* A Memory mapped base address */
93 type = size & (~PCI_BASE_ADDRESS_MEM_MASK);
94 resource->size = (~(size &PCI_BASE_ADDRESS_MEM_MASK)) +1;
95 resource->align = log2(resource->size);
96 resource->gran = resource->align;
97 resource->flags = IORESOURCE_MEM;
98 if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
99 resource->flags |= IORESOURCE_PREFETCH;
100 }
101 type &= PCI_BASE_ADDRESS_MEM_TYPE_MASK;
102 if (type == PCI_BASE_ADDRESS_MEM_TYPE_32) {
103 /* 32bit limit */
104 resource->limit = 0xffffffffUL;
105 }
106 else if (type == PCI_BASE_ADDRESS_MEM_TYPE_1M) {
107 /* 1MB limit */
108 resource->limit = 0x000fffffUL;
109 }
110 else if (type == PCI_BASE_ADDRESS_MEM_TYPE_64) {
111 unsigned long index_hi;
112 /* 64bit limit
113 * For now just treat this as a 32bit limit
114 */
115 index_hi = index + 4;
116 resource->limit = 0xffffffffUL;
117 resource->flags |= IORESOURCE_PCI64;
Eric Biederman7a5416a2003-06-12 19:23:51 +0000118 addr = pci_read_config32( dev, index_hi);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000119 /* get the extended size */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000120 pci_write_config32(dev, index_hi, 0xffffffffUL);
121 size = pci_read_config32( dev, index_hi);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000122
123 /* get the minimum value the bar can be set to */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000124 pci_write_config32(dev, index_hi, 0);
125 base = pci_read_config32(dev, index_hi);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000126
127 /* restore addr */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000128 pci_write_config32(dev, index_hi, addr);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000129
130 if ((size == 0xffffffff) && (base == 0)) {
131 /* Clear the top half of the bar */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000132 pci_write_config32(dev, index_hi, 0);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000133 }
134 else {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000135 printk_err("%s Unable to handle 64-bit address\n",
136 dev_path(dev));
Eric Biederman8ca8d762003-04-22 19:02:15 +0000137 resource->flags = IORESOURCE_PCI64;
138 }
139 }
140 else {
141 /* Invalid value */
142 resource->flags = 0;
143 }
144 }
145 /* dev->size holds the flags... */
146 return;
147}
148
149/** Read the base address registers for a given device.
150 * @param dev Pointer to the dev structure
151 * @param howmany How many registers to read (6 for device, 2 for bridge)
152 */
153static void pci_read_bases(struct device *dev, unsigned int howmany)
154{
155 unsigned int reg;
156 unsigned long index;
157
158 reg = dev->resources;
159 for(index = PCI_BASE_ADDRESS_0;
160 (reg < MAX_RESOURCES) && (index < PCI_BASE_ADDRESS_0 + (howmany << 2)); ) {
161 struct resource *resource;
162 resource = &dev->resource[reg];
163 pci_get_resource(dev, resource, index);
164 reg += (resource->flags & (IORESOURCE_IO | IORESOURCE_MEM))? 1:0;
165 index += (resource->flags & IORESOURCE_PCI64)?8:4;
166 }
167 dev->resources = reg;
168}
169
170
171static void pci_bridge_read_bases(struct device *dev)
172{
173 unsigned int reg = dev->resources;
174
175 /* FIXME handle bridges without some of the optional resources */
176
177 /* Initialize the io space constraints on the current bus */
178 dev->resource[reg].base = 0;
179 dev->resource[reg].size = 0;
180 dev->resource[reg].align = log2(PCI_IO_BRIDGE_ALIGN);
181 dev->resource[reg].gran = log2(PCI_IO_BRIDGE_ALIGN);
182 dev->resource[reg].limit = 0xffffUL;
183 dev->resource[reg].flags = IORESOURCE_IO | IORESOURCE_PCI_BRIDGE;
184 dev->resource[reg].index = PCI_IO_BASE;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000185 compute_allocate_resource(&dev->link[0], &dev->resource[reg],
Eric Biederman8ca8d762003-04-22 19:02:15 +0000186 IORESOURCE_IO, IORESOURCE_IO);
187 reg++;
188
189 /* Initiliaze the prefetchable memory constraints on the current bus */
190 dev->resource[reg].base = 0;
191 dev->resource[reg].size = 0;
192 dev->resource[reg].align = log2(PCI_MEM_BRIDGE_ALIGN);
193 dev->resource[reg].gran = log2(PCI_MEM_BRIDGE_ALIGN);
194 dev->resource[reg].limit = 0xffffffffUL;
195 dev->resource[reg].flags = IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_PCI_BRIDGE;
196 dev->resource[reg].index = PCI_PREF_MEMORY_BASE;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000197 compute_allocate_resource(&dev->link[0], &dev->resource[reg],
Eric Biederman8ca8d762003-04-22 19:02:15 +0000198 IORESOURCE_MEM | IORESOURCE_PREFETCH,
199 IORESOURCE_MEM | IORESOURCE_PREFETCH);
200 reg++;
201
202 /* Initialize the memory resources on the current bus */
203 dev->resource[reg].base = 0;
204 dev->resource[reg].size = 0;
205 dev->resource[reg].align = log2(PCI_MEM_BRIDGE_ALIGN);
206 dev->resource[reg].gran = log2(PCI_MEM_BRIDGE_ALIGN);
207 dev->resource[reg].limit = 0xffffffffUL;
208 dev->resource[reg].flags = IORESOURCE_MEM | IORESOURCE_PCI_BRIDGE;
209 dev->resource[reg].index = PCI_MEMORY_BASE;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000210 compute_allocate_resource(&dev->link[0], &dev->resource[reg],
Eric Biederman8ca8d762003-04-22 19:02:15 +0000211 IORESOURCE_MEM | IORESOURCE_PREFETCH,
212 IORESOURCE_MEM);
213 reg++;
214
215 dev->resources = reg;
216}
217
218
Eric Biederman5899fd82003-04-24 06:25:08 +0000219void pci_dev_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000220{
221 uint32_t addr;
222 dev->resources = 0;
223 memset(&dev->resource[0], 0, sizeof(dev->resource));
224 pci_read_bases(dev, 6);
Eric Biederman7a5416a2003-06-12 19:23:51 +0000225 addr = pci_read_config32(dev, PCI_ROM_ADDRESS);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000226 dev->rom_address = (addr == 0xffffffff)? 0 : addr;
227}
228
Eric Biederman5899fd82003-04-24 06:25:08 +0000229void pci_bus_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000230{
231 uint32_t addr;
232 dev->resources = 0;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000233 memset(&dev->resource, 0, sizeof(dev->resource));
Eric Biederman8ca8d762003-04-22 19:02:15 +0000234 pci_bridge_read_bases(dev);
235 pci_read_bases(dev, 2);
236
Eric Biederman7a5416a2003-06-12 19:23:51 +0000237 addr = pci_read_config32(dev, PCI_ROM_ADDRESS1);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000238 dev->rom_address = (addr == 0xffffffff)? 0 : addr;
239
240}
241
242
243static void pci_set_resource(struct device *dev, struct resource *resource)
244{
245 unsigned long base, limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000246 unsigned char buf[10];
Eric Biedermane9a271e32003-09-02 03:36:25 +0000247 unsigned long align;
248
Eric Biederman8ca8d762003-04-22 19:02:15 +0000249 /* Make certain the resource has actually been set */
250 if (!(resource->flags & IORESOURCE_SET)) {
251#if 1
Eric Biedermane9a271e32003-09-02 03:36:25 +0000252 printk_err("ERROR: %s %02x not allocated\n",
253 dev_path(dev), resource->index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000254#endif
255 return;
256 }
257
258 /* Only handle PCI memory and IO resources for now */
259 if (!(resource->flags & (IORESOURCE_MEM |IORESOURCE_IO)))
260 return;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000261
Eric Biederman8ca8d762003-04-22 19:02:15 +0000262 if (resource->flags & IORESOURCE_MEM) {
263 dev->command |= PCI_COMMAND_MEMORY;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000264 }
265 if (resource->flags & IORESOURCE_IO) {
266 dev->command |= PCI_COMMAND_IO;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000267 }
268 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
269 dev->command |= PCI_COMMAND_MASTER;
270 }
271 /* Get the base address */
272 base = resource->base;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000273 /* Get the resource alignment */
274 align = 1UL << resource->align;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000275
276 /* Get the limit (rounded up) */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000277 limit = base + ((resource->size + align - 1UL) & ~(align - 1UL)) -1UL;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000278
279 if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) {
280 /*
281 * some chipsets allow us to set/clear the IO bit.
282 * (e.g. VIA 82c686a.) So set it to be safe)
283 */
284 limit = base + resource->size -1;
285 if (resource->flags & IORESOURCE_IO) {
286 base |= PCI_BASE_ADDRESS_SPACE_IO;
287 }
Eric Biederman7a5416a2003-06-12 19:23:51 +0000288 pci_write_config32(dev, resource->index, base & 0xffffffff);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000289 if (resource->flags & IORESOURCE_PCI64) {
290 /* FIXME handle real 64bit base addresses */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000291 pci_write_config32(dev, resource->index + 4, 0);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000292 }
293 }
294 else if (resource->index == PCI_IO_BASE) {
295 /* set the IO ranges
296 * WARNING: we don't really do 32-bit addressing for IO yet!
297 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000298 compute_allocate_resource(&dev->link[0], resource,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000299 IORESOURCE_IO, IORESOURCE_IO);
Eric Biederman7a5416a2003-06-12 19:23:51 +0000300 pci_write_config8(dev, PCI_IO_BASE, base >> 8);
301 pci_write_config8(dev, PCI_IO_LIMIT, limit >> 8);
Eric Biederman5fb929e2003-07-17 02:15:46 +0000302 pci_write_config16(dev, PCI_IO_BASE_UPPER16, 0);
303 pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, 0);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000304 }
305 else if (resource->index == PCI_MEMORY_BASE) {
306 /* set the memory range
307 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000308 compute_allocate_resource(&dev->link[0], resource,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000309 IORESOURCE_MEM | IORESOURCE_PREFETCH,
310 IORESOURCE_MEM);
Eric Biederman7a5416a2003-06-12 19:23:51 +0000311 pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16);
312 pci_write_config16(dev, PCI_MEMORY_LIMIT, limit >> 16);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000313 }
314 else if (resource->index == PCI_PREF_MEMORY_BASE) {
315 /* set the prefetchable memory range
316 * WARNING: we don't really do 64-bit addressing for prefetchable memory yet!
317 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000318 compute_allocate_resource(&dev->link[0], resource,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000319 IORESOURCE_MEM | IORESOURCE_PREFETCH,
320 IORESOURCE_MEM | IORESOURCE_PREFETCH);
Eric Biederman7a5416a2003-06-12 19:23:51 +0000321 pci_write_config16(dev, PCI_PREF_MEMORY_BASE, base >> 16);
322 pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, limit >> 16);
Eric Biederman5fb929e2003-07-17 02:15:46 +0000323 pci_write_config32(dev, PCI_PREF_BASE_UPPER32, 0);
324 pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, 0);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000325 }
326 else {
327 printk_err("ERROR: invalid resource->index %x\n",
328 resource->index);
329 }
330 buf[0] = '\0';
331 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000332 sprintf(buf, "bus %d ", dev->link[0].secondary);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000333 }
334
335 printk_debug(
Eric Biedermane9a271e32003-09-02 03:36:25 +0000336 "%s %02x <- [0x%08lx - 0x%08lx] %s%s\n",
337 dev_path(dev),
Eric Biederman8ca8d762003-04-22 19:02:15 +0000338 resource->index,
339 resource->base, limit,
340 buf,
341 (resource->flags & IORESOURCE_IO)? "io":
342 (resource->flags & IORESOURCE_PREFETCH)? "prefmem": "mem");
343 return;
344}
345
Eric Biederman5899fd82003-04-24 06:25:08 +0000346void pci_dev_set_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000347{
348 struct resource *resource, *last;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000349 unsigned link;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000350 uint8_t line;
351
352 last = &dev->resource[dev->resources];
353
354 for(resource = &dev->resource[0]; resource < last; resource++) {
355 pci_set_resource(dev, resource);
356 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000357 for(link = 0; link < dev->links; link++) {
358 struct bus *bus;
359 bus = &dev->link[link];
360 if (bus->children) {
361 assign_resources(bus);
362 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000363 }
364
365 /* set a default latency timer */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000366 pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000367
368 /* set a default secondary latency timer */
369 if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
Eric Biederman7a5416a2003-06-12 19:23:51 +0000370 pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000371 }
372
373 /* zero the irq settings */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000374 line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000375 if (line) {
Eric Biederman7a5416a2003-06-12 19:23:51 +0000376 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000377 }
378 /* set the cache line size, so far 64 bytes is good for everyone */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000379 pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000380}
381
Eric Biedermane9a271e32003-09-02 03:36:25 +0000382void pci_dev_enable_resources(struct device *dev)
383{
384 uint16_t command;
385 command = pci_read_config16(dev, PCI_COMMAND);
386 command |= dev->command;
387 printk_debug("%s cmd <- %02x\n", dev_path(dev), command);
388 pci_write_config16(dev, PCI_COMMAND, command);
389
390 enable_childrens_resources(dev);
391}
392
393void pci_bus_enable_resources(struct device *dev)
394{
395 uint16_t ctrl;
396 ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
397 ctrl |= dev->link[0].bridge_ctrl;
398 printk_debug("%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
399 pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
400
401 pci_dev_enable_resources(dev);
402}
403
Eric Biederman8ca8d762003-04-22 19:02:15 +0000404struct device_operations default_pci_ops_dev = {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000405 .read_resources = pci_dev_read_resources,
406 .set_resources = pci_dev_set_resources,
407 .enable_resources = pci_dev_enable_resources,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000408 .init = 0,
409 .scan_bus = 0,
410};
411struct device_operations default_pci_ops_bus = {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000412 .read_resources = pci_bus_read_resources,
413 .set_resources = pci_dev_set_resources,
414 .enable_resources = pci_bus_enable_resources,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000415 .init = 0,
416 .scan_bus = pci_scan_bridge,
417};
418static void set_pci_ops(struct device *dev)
419{
420 struct pci_driver *driver;
421 if (dev->ops) {
422 return;
423 }
424 /* Look through the list of setup drivers and find one for
425 * this pci device
426 */
427 for(driver = &pci_drivers[0]; driver != &epci_drivers[0]; driver++) {
428 if ((driver->vendor == dev->vendor) &&
Eric Biederman5899fd82003-04-24 06:25:08 +0000429 (driver->device == dev->device)) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000430 dev->ops = driver->ops;
Eric Biederman5899fd82003-04-24 06:25:08 +0000431#if 1
Eric Biedermane9a271e32003-09-02 03:36:25 +0000432 printk_debug("%s [%04x/%04x] %sops\n",
433 dev_path(dev),
434 driver->vendor, driver->device,
435 (driver->ops->scan_bus?"bus ":"")
Eric Biederman5899fd82003-04-24 06:25:08 +0000436 );
437#endif
438 return;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000439 }
440 }
441 /* If I don't have a specific driver use the default operations */
442 switch(dev->hdr_type & 0x7f) { /* header type */
443 case PCI_HEADER_TYPE_NORMAL: /* standard header */
444 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
445 goto bad;
446 dev->ops = &default_pci_ops_dev;
447 break;
448 case PCI_HEADER_TYPE_BRIDGE:
449 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
450 goto bad;
451 dev->ops = &default_pci_ops_bus;
452 break;
453 default:
454 bad:
Eric Biedermane9a271e32003-09-02 03:36:25 +0000455 printk_err("%s [%04x/%04x/%06x] has unknown header "
Eric Biederman8ca8d762003-04-22 19:02:15 +0000456 "type %02x, ignoring.\n",
Eric Biedermane9a271e32003-09-02 03:36:25 +0000457 dev_path(dev),
Eric Biederman8ca8d762003-04-22 19:02:15 +0000458 dev->vendor, dev->device,
459 dev->class >> 8, dev->hdr_type);
460 }
461 return;
462}
463
464/**
465 * Given a bus and a devfn number, find the device structure
466 * @param bus The bus structure
467 * @param devfn a device/function number
468 * @return pointer to the device structure
469 */
470static struct device *pci_scan_get_dev(struct device **list, unsigned int devfn)
471{
472 struct device *dev = 0;
473 for(; *list; list = &(*list)->sibling) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000474 if ((*list)->path.u.pci.devfn == devfn) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000475 /* Unlink from the list */
476 dev = *list;
477 *list = (*list)->sibling;
478 dev->sibling = 0;
479 break;
480 }
481 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000482 if (dev) {
483 device_t child;
484 /* Find the last child of our parent */
485 for(child = dev->bus->children; child && child->sibling; ) {
486 child = child->sibling;
487 }
488 /* Place the device on the list of children of it's parent. */
489 if (child) {
490 child->sibling = dev;
491 } else {
492 dev->bus->children = dev;
493 }
494 }
495
Eric Biederman8ca8d762003-04-22 19:02:15 +0000496 return dev;
497}
498
Eric Biederman30e143a2003-09-01 23:45:32 +0000499void assign_id_set_links(device_t dev, uint8_t *pos,
500 uint8_t *previous_pos, unsigned previous_unitid,
501 unsigned last_unitid, int *reset_needed,
502 struct device *bus, unsigned *next_unitid)
503{
504 static const uint8_t link_width_to_pow2[]= { 3, 4, 0, 5, 1, 2, 0, 0 };
505 static const uint8_t pow2_to_link_width[] = { 0x7, 4, 5, 0, 1, 3 };
506 uint16_t flags;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000507 struct bus prev_bus;
508 struct device last, previous;
Eric Biederman30e143a2003-09-01 23:45:32 +0000509 unsigned count;
510 uint8_t present_width_cap;
511 uint16_t present_freq_cap;
512 uint8_t upstream_width_cap;
513 uint16_t upstream_freq_cap;
514 uint8_t ln_upstream_width_in, ln_present_width_in;
515 uint8_t ln_upstream_width_out, ln_present_width_out;
516 uint16_t mask;
517 uint8_t freq;
518 uint8_t old_freq;
519 uint8_t upstream_width, present_width;
520 uint8_t old_width;
521
522 flags = pci_read_config16(dev, (*pos) + PCI_CAP_FLAGS);
523 printk_debug("flags: 0x%04x\n", (unsigned)flags);
524 if ((flags >> 13) != 0)
525 return; /* Entry is a Host */
526 /* Entry is a Slave secondary */
527 flags &= ~0x1f; /* mask out base unit ID */
528 flags |= *next_unitid & 0x1f; /* assign ID */
529 count = (flags >> 5) & 0x1f; /* get unit count */
530 printk_debug("unitid: 0x%02x, count: 0x%02x\n",
531 *next_unitid, count);
532 pci_write_config16(dev, (*pos) + PCI_CAP_FLAGS, flags);
533 *next_unitid += count;
534 if (previous_unitid == 0) { /* the link is back to the host */
535 prev_bus.secondary = 0;
536 /* calculate the previous pos for the host */
537 *previous_pos = 0x80;
538 previous.bus = &prev_bus;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000539 previous.path.type = DEVICE_PATH_PCI;
540 previous.path.u.pci.devfn = 0x18 << 3;
Eric Biederman30e143a2003-09-01 23:45:32 +0000541 #warning "FIXME we should not hard code this!"
542 } else {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000543 previous.bus = bus;
544 previous.path.type = DEVICE_PATH_PCI;
545 previous.path.u.pci.devfn = previous_unitid << 3;
Eric Biederman30e143a2003-09-01 23:45:32 +0000546 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000547 last.bus = bus;
548 last.path.type = DEVICE_PATH_PCI;
549 last.path.u.pci.devfn = last_unitid << 3;
Eric Biederman30e143a2003-09-01 23:45:32 +0000550 /* Set link width and frequency */
551 present_freq_cap = pci_read_config16(&last,
552 (*pos) + PCI_HT_CAP_SLAVE_FREQ_CAP0);
553 present_width_cap = pci_read_config8(&last,
554 (*pos) + PCI_HT_CAP_SLAVE_WIDTH0);
555 if(previous_unitid == 0) { /* the link is back to the host */
556 upstream_freq_cap = pci_read_config16(&previous,
557 (*previous_pos) + PCI_HT_CAP_HOST_FREQ_CAP);
558 upstream_width_cap = pci_read_config8(&previous,
559 (*previous_pos) + PCI_HT_CAP_HOST_WIDTH);
560 }
561 else { /* The link is back up the chain */
562 upstream_freq_cap = pci_read_config16(&previous,
563 (*previous_pos) + PCI_HT_CAP_SLAVE_FREQ_CAP1);
564 upstream_width_cap = pci_read_config8(&previous,
565 (*previous_pos) + PCI_HT_CAP_SLAVE_WIDTH1);
566 }
567 /* Calculate the highest possible frequency */
568 /* Errata for 8131 - freq 5 has hardware problems don't support it */
569 freq = log2(present_freq_cap & upstream_freq_cap & 0x1f);
570
571 /* Calculate the highest width */
572 ln_upstream_width_in = link_width_to_pow2[upstream_width_cap & 7];
573 ln_present_width_out = link_width_to_pow2[(present_width_cap >> 4) & 7];
574 if (ln_upstream_width_in > ln_present_width_out) {
575 ln_upstream_width_in = ln_present_width_out;
576 }
577 upstream_width = pow2_to_link_width[ln_upstream_width_in];
578 present_width = pow2_to_link_width[ln_upstream_width_in] << 4;
579
580 ln_upstream_width_out = link_width_to_pow2[(upstream_width_cap >> 4) & 7];
581 ln_present_width_in = link_width_to_pow2[present_width_cap & 7];
582 if (ln_upstream_width_out > ln_present_width_in) {
583 ln_upstream_width_out = ln_present_width_in;
584 }
585 upstream_width |= pow2_to_link_width[ln_upstream_width_out] << 4;
586 present_width |= pow2_to_link_width[ln_upstream_width_out];
587
588 /* set the present device */
589 old_freq = pci_read_config8(&last, (*pos) + PCI_HT_CAP_SLAVE_FREQ0);
590 if(old_freq != freq) {
591 pci_write_config8(&last,
592 (*pos) + PCI_HT_CAP_SLAVE_FREQ0, freq);
593 *reset_needed = 1;
594 printk_debug("HyperT FreqP old %x new %x\n",old_freq,freq);
595 }
596 old_width = pci_read_config8(&last,
597 (*pos) + PCI_HT_CAP_SLAVE_WIDTH0 + 1);
598 if(present_width != old_width) {
599 pci_write_config8(&last,
600 (*pos) + PCI_HT_CAP_SLAVE_WIDTH0 + 1, present_width);
601 *reset_needed = 1;
602 printk_debug("HyperT widthP old %x new %x\n",
603 old_width, present_width);
604 }
605 /* set the upstream device */
606 if(previous_unitid == 0) { /* the link is back to the host */
607 old_freq = pci_read_config8(&previous,
608 (*previous_pos) + PCI_HT_CAP_HOST_FREQ);
609 old_freq &= 0x0f;
610 if(freq != old_freq) {
611 pci_write_config8(&previous,
612 (*previous_pos) + PCI_HT_CAP_HOST_FREQ, freq);
613 *reset_needed = 1;
614 printk_debug("HyperT freqUH old %x new %x\n",
615 old_freq, freq);
616 }
617 old_width = pci_read_config8(&previous,
618 (*previous_pos) + PCI_HT_CAP_HOST_WIDTH + 1);
619 if(upstream_width != old_width) {
620 pci_write_config8(&previous,
621 (*previous_pos) + PCI_HT_CAP_HOST_WIDTH + 1,
622 upstream_width);
623 *reset_needed = 1;
624 printk_debug("HyperT widthUH old %x new %x\n",
625 old_width, upstream_width);
626 }
627 }
628 else { /* The link is back up the chain */
629 old_freq = pci_read_config8(&previous,
630 (*previous_pos) + PCI_HT_CAP_SLAVE_FREQ1);
631 old_freq &= 0x0f;
632 if(freq != old_freq) {
633 pci_write_config8(&previous,
634 (*previous_pos) + PCI_HT_CAP_SLAVE_FREQ1,
635 freq);
636 *reset_needed = 1;
637 printk_debug("HyperT freqUL old %x new %x\n",
638 old_freq, freq);
639 }
640 old_width = pci_read_config8(&previous,
641 (*previous_pos) + PCI_HT_CAP_SLAVE_WIDTH1 + 1);
642 if(upstream_width != old_width) {
643 pci_write_config8(&previous,
644 (*previous_pos) + PCI_HT_CAP_SLAVE_WIDTH1,
645 upstream_width);
646 *reset_needed = 1;
647 printk_debug("HyperT widthUL old %x new %x\n",
648 old_width, upstream_width);
649 }
650 }
651 *previous_pos = *pos;
652 *pos=0;
653}
Eric Biederman8ca8d762003-04-22 19:02:15 +0000654
Eric Biederman5899fd82003-04-24 06:25:08 +0000655#define HYPERTRANSPORT_SUPPORT 1
Eric Biederman8ca8d762003-04-22 19:02:15 +0000656/** Scan the pci bus devices and bridges.
Eric Biedermane9a271e32003-09-02 03:36:25 +0000657 * @param bus pointer to the bus structure
658 * @param min_devfn minimum devfn to look at in the scan usually 0x00
659 * @param max_devfn maximum devfn to look at in the scan usually 0xff
Eric Biederman8ca8d762003-04-22 19:02:15 +0000660 * @param max current bus number
661 * @return The maximum bus number found, after scanning all subordinate busses
662 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000663unsigned int pci_scan_bus(struct bus *bus,
664 unsigned min_devfn, unsigned max_devfn,
665 unsigned int max)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000666{
667 unsigned int devfn;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000668 device_t dev;
669 device_t old_devices;
670 device_t child;
Eric Biederman5899fd82003-04-24 06:25:08 +0000671#if HYPERTRANSPORT_SUPPORT
Eric Biederman860ad372003-07-21 23:30:29 +0000672 unsigned next_unitid, last_unitid, previous_unitid;
Eric Biederman30e143a2003-09-01 23:45:32 +0000673 int reset_needed = 0;
674 uint8_t previous_pos;
Eric Biederman5899fd82003-04-24 06:25:08 +0000675#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +0000676
677 printk_debug("PCI: pci_scan_bus for bus %d\n", bus->secondary);
678
679 old_devices = bus->children;
680 bus->children = 0;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000681
682 post_code(0x24);
683
684
Eric Biederman5899fd82003-04-24 06:25:08 +0000685#if HYPERTRANSPORT_SUPPORT
Eric Biederman860ad372003-07-21 23:30:29 +0000686 /* Spin through the devices and collapse any early
687 * hypertransport enumeration.
688 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000689 for(devfn = min_devfn; devfn <= max_devfn; devfn += 8) {
Eric Biederman5899fd82003-04-24 06:25:08 +0000690 struct device dummy;
691 uint32_t id;
692 uint8_t hdr_type, pos;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000693 dummy.bus = bus;
694 dummy.path.type = DEVICE_PATH_PCI;
695 dummy.path.u.pci.devfn = devfn;
Eric Biederman7a5416a2003-06-12 19:23:51 +0000696 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000697 if ( (id == 0xffffffff) || (id == 0x00000000) ||
698 (id == 0x0000ffff) || (id == 0xffff0000)) {
Eric Biederman860ad372003-07-21 23:30:29 +0000699 continue;
Eric Biederman5899fd82003-04-24 06:25:08 +0000700 }
Eric Biederman7a5416a2003-06-12 19:23:51 +0000701 hdr_type = pci_read_config8(&dummy, PCI_HEADER_TYPE);
Eric Biederman5899fd82003-04-24 06:25:08 +0000702 pos = 0;
703 switch(hdr_type & 0x7f) {
704 case PCI_HEADER_TYPE_NORMAL:
705 case PCI_HEADER_TYPE_BRIDGE:
706 pos = PCI_CAPABILITY_LIST;
707 break;
708 }
709 if (pos > PCI_CAP_LIST_NEXT) {
Eric Biederman7a5416a2003-06-12 19:23:51 +0000710 pos = pci_read_config8(&dummy, pos);
Eric Biederman5899fd82003-04-24 06:25:08 +0000711 }
712 while(pos != 0) {
713 uint8_t cap;
Eric Biederman7a5416a2003-06-12 19:23:51 +0000714 cap = pci_read_config8(&dummy, pos + PCI_CAP_LIST_ID);
Eric Biederman5899fd82003-04-24 06:25:08 +0000715 printk_debug("Capability: 0x%02x @ 0x%02x\n", cap, pos);
716 if (cap == PCI_CAP_ID_HT) {
717 uint16_t flags;
Eric Biederman30e143a2003-09-01 23:45:32 +0000718 flags = pci_read_config16(&dummy,
719 pos + PCI_CAP_FLAGS);
720 printk_debug("flags: 0x%04x\n",
721 (unsigned)flags);
Eric Biederman5899fd82003-04-24 06:25:08 +0000722 if ((flags >> 13) == 0) {
Eric Biederman860ad372003-07-21 23:30:29 +0000723 /* Clear the unitid */
Eric Biederman5899fd82003-04-24 06:25:08 +0000724 flags &= ~0x1f;
Eric Biederman30e143a2003-09-01 23:45:32 +0000725 pci_write_config16(&dummy,
726 pos + PCI_CAP_FLAGS, flags);
Eric Biederman5899fd82003-04-24 06:25:08 +0000727 break;
728 }
729 }
Eric Biederman7a5416a2003-06-12 19:23:51 +0000730 pos = pci_read_config8(&dummy, pos + PCI_CAP_LIST_NEXT);
Eric Biederman5899fd82003-04-24 06:25:08 +0000731 }
Eric Biederman860ad372003-07-21 23:30:29 +0000732 }
733 /* If present assign unitid to a hypertransport chain */
734 last_unitid = 0;
735 next_unitid = 1;
Eric Biederman30e143a2003-09-01 23:45:32 +0000736 previous_pos = 0;
Eric Biederman860ad372003-07-21 23:30:29 +0000737 do {
738 struct device dummy;
739 uint32_t id;
Eric Biederman30e143a2003-09-01 23:45:32 +0000740 uint8_t hdr_type, pos;
Eric Biederman860ad372003-07-21 23:30:29 +0000741
742 previous_unitid = last_unitid;
743 last_unitid = next_unitid;
744
745 /* Read the next unassigned device off the stack */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000746 dummy.bus = bus;
747 dummy.path.type = DEVICE_PATH_PCI;
748 dummy.path.u.pci.devfn = 0;
Eric Biederman860ad372003-07-21 23:30:29 +0000749 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
750 /* If the chain is enumerated quit */
751 if (id == 0xffffffff || id == 0x00000000 ||
752 id == 0x0000ffff || id == 0xffff0000) {
753 break;
754 }
755 hdr_type = pci_read_config8(&dummy, PCI_HEADER_TYPE);
756 pos = 0;
757 switch(hdr_type & 0x7f) {
758 case PCI_HEADER_TYPE_NORMAL:
759 case PCI_HEADER_TYPE_BRIDGE:
760 pos = PCI_CAPABILITY_LIST;
761 break;
762 }
763 if (pos > PCI_CAP_LIST_NEXT) {
764 pos = pci_read_config8(&dummy, pos);
765 }
766 while(pos != 0) { /* loop through the linked list */
767 uint8_t cap;
768 cap = pci_read_config8(&dummy, pos + PCI_CAP_LIST_ID);
769 printk_debug("Capability: 0x%02x @ 0x%02x\n", cap, pos);
770 if (cap == PCI_CAP_ID_HT) {
Eric Biederman30e143a2003-09-01 23:45:32 +0000771 assign_id_set_links(&dummy,&pos,&previous_pos,
772 previous_unitid, last_unitid,
773 &reset_needed, bus,
774 &next_unitid);
Eric Biederman860ad372003-07-21 23:30:29 +0000775 }
Eric Biederman30e143a2003-09-01 23:45:32 +0000776 if(pos)
777 pos = pci_read_config8(&dummy,
778 pos + PCI_CAP_LIST_NEXT);
Eric Biederman860ad372003-07-21 23:30:29 +0000779 }
Eric Biederman5899fd82003-04-24 06:25:08 +0000780 } while((last_unitid != next_unitid) && (next_unitid <= 0x1f));
Eric Biederman30e143a2003-09-01 23:45:32 +0000781#if HAVE_HARD_RESET == 1
782 if(reset_needed) {
783 printk_debug("HyperT reset needed\n");
784 boot_successful();
785 hard_reset();
786 }
787 printk_debug("HyperT reset not needed\n");
788#endif /* HAVE_HARD_RESET */
Eric Biederman5899fd82003-04-24 06:25:08 +0000789#endif /* HYPERTRANSPORT_SUPPORT */
790
Eric Biederman8ca8d762003-04-22 19:02:15 +0000791 /* probe all devices on this bus with some optimization for non-existance and
792 single funcion devices */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000793 for (devfn = min_devfn; devfn <= max_devfn; devfn++) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000794 uint32_t id, class;
Eric Biederman30e143a2003-09-01 23:45:32 +0000795 uint8_t hdr_type;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000796
797 /* First thing setup the device structure */
798 dev = pci_scan_get_dev(&old_devices, devfn);
799
Eric Biedermane9a271e32003-09-02 03:36:25 +0000800 /* Detect if a device is present */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000801 if (!dev) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000802 struct device dummy;
803 dummy.bus = bus;
804 dummy.path.type = DEVICE_PATH_PCI;
805 dummy.path.u.pci.devfn = devfn;
806 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
807 /* some broken boards return 0 if a slot is empty: */
808 if ( (id == 0xffffffff) || (id == 0x00000000) ||
809 (id == 0x0000ffff) || (id == 0xffff0000))
810 {
811 printk_spew("PCI: devfn 0x%x, bad id 0x%x\n", devfn, id);
812 if (PCI_FUNC(devfn) == 0x00) {
813 /* if this is a function 0 device and it is not present,
814 skip to next device */
815 devfn += 0x07;
816 }
817 /* multi function device, skip to next function */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000818 continue;
819 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000820 dev = alloc_dev(bus, &dummy.path);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000821 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000822 else {
823 /* Run the magic enable/disable sequence for the device */
824 if (dev->ops && dev->ops->enable) {
825 dev->ops->enable(dev);
826 }
827 /* Now read the vendor and device id */
828 id = pci_read_config32(dev, PCI_VENDOR_ID);
829 }
830
831 /* Read the rest of the pci configuration information */
832 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
833 class = pci_read_config32(dev, PCI_CLASS_REVISION);
834
835 /* Store the interesting information in the device structure */
836 dev->vendor = id & 0xffff;
837 dev->device = (id >> 16) & 0xffff;
838 dev->hdr_type = hdr_type;
839 /* class code, the upper 3 bytes of PCI_CLASS_REVISION */
840 dev->class = class >> 8;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000841
Eric Biederman8ca8d762003-04-22 19:02:15 +0000842 /* Look at the vendor and device id, or at least the
843 * header type and class and figure out which set of configuration
844 * methods to use.
845 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000846 if (!dev->ops) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000847 set_pci_ops(dev);
848 /* Error if we don't have some pci operations for it */
849 if (!dev->ops) {
850 printk_err("%s No device operations\n",
851 dev_path(dev));
852 continue;
853 }
854 /* Now run the magic enable/disable sequence for the device */
855 if (dev->ops && dev->ops->enable) {
856 dev->ops->enable(dev);
857 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000858 }
Eric Biederman4086d162003-07-17 03:26:03 +0000859
Eric Biedermane9a271e32003-09-02 03:36:25 +0000860 printk_debug("%s [%04x/%04x] %s\n",
861 dev_path(dev),
Eric Biederman4086d162003-07-17 03:26:03 +0000862 dev->vendor, dev->device,
863 dev->enable?"enabled": "disabled");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000864
Eric Biederman8ca8d762003-04-22 19:02:15 +0000865 if (PCI_FUNC(devfn) == 0x00 && (hdr_type & 0x80) != 0x80) {
866 /* if this is not a multi function device, don't waste time probe
867 another function. Skip to next device. */
868 devfn += 0x07;
869 }
870 }
871 post_code(0x25);
872
873 for(child = bus->children; child; child = child->sibling) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000874 if (!child->ops->scan_bus) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000875 continue;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000876 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000877 max = child->ops->scan_bus(child, max);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000878 }
879 /*
880 * We've scanned the bus and so we know all about what's on
881 * the other side of any bridges that may be on this bus plus
882 * any devices.
883 *
884 * Return how far we've got finding sub-buses.
885 */
886 printk_debug("PCI: pci_scan_bus returning with max=%02x\n", max);
887 post_code(0x55);
888 return max;
889}
890
891/** Scan the bus, first for bridges and next for devices.
892 * @param pci_bus pointer to the bus structure
893 * @return The maximum bus number found, after scanning all subordinate busses
894 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000895unsigned int pci_scan_bridge(struct device *dev, unsigned int max)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000896{
Eric Biedermane9a271e32003-09-02 03:36:25 +0000897 struct bus *bus;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000898 uint32_t buses;
899 uint16_t cr;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000900
901 bus = &dev->link[0];
902 dev->links = 1;
903
Eric Biederman8ca8d762003-04-22 19:02:15 +0000904 /* Set up the primary, secondary and subordinate bus numbers. We have
905 * no idea how many buses are behind this bridge yet, so we set the
906 * subordinate bus number to 0xff for the moment
907 */
908 bus->secondary = ++max;
909 bus->subordinate = 0xff;
910
911 /* Clear all status bits and turn off memory, I/O and master enables. */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000912 cr = pci_read_config16(dev, PCI_COMMAND);
913 pci_write_config16(dev, PCI_COMMAND, 0x0000);
914 pci_write_config16(dev, PCI_STATUS, 0xffff);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000915
916 /*
917 * Read the existing primary/secondary/subordinate bus
918 * number configuration.
919 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000920 buses = pci_read_config32(dev, PCI_PRIMARY_BUS);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000921
922 /* Configure the bus numbers for this bridge: the configuration
923 * transactions will not be propagated by the bridge if it is not
924 * correctly configured
925 */
926 buses &= 0xff000000;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000927 buses |= (((unsigned int) (dev->bus->secondary) << 0) |
Eric Biederman8ca8d762003-04-22 19:02:15 +0000928 ((unsigned int) (bus->secondary) << 8) |
929 ((unsigned int) (bus->subordinate) << 16));
Eric Biedermane9a271e32003-09-02 03:36:25 +0000930 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000931
932 /* Now we can scan all subordinate buses i.e. the bus hehind the bridge */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000933 max = pci_scan_bus(bus, 0x00, 0xff, max);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000934
935 /* We know the number of buses behind this bridge. Set the subordinate
936 * bus number to its real value
937 */
938 bus->subordinate = max;
939 buses = (buses & 0xff00ffff) |
940 ((unsigned int) (bus->subordinate) << 16);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000941 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
942 pci_write_config16(dev, PCI_COMMAND, cr);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000943
944 return max;
945}