blob: 1c8c8bea8494b4d35f5940f794fd89073c651d9f [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>
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +000017#include <arch/io.h>
Eric Biederman5899fd82003-04-24 06:25:08 +000018#include <device/device.h>
19#include <device/pci.h>
20#include <device/pci_ids.h>
Eric Biedermane9a271e32003-09-02 03:36:25 +000021#include <part/hard_reset.h>
Eric Biederman30e143a2003-09-01 23:45:32 +000022#include <part/fallback_boot.h>
Eric Biederman03acab62004-10-14 21:25:53 +000023#include <delay.h>
24
25static uint8_t pci_moving_config8(struct device *dev, unsigned reg)
26{
27 uint8_t value, ones, zeroes;
28 value = pci_read_config8(dev, reg);
29
30 pci_write_config8(dev, reg, 0xff);
31 ones = pci_read_config8(dev, reg);
32
33 pci_write_config8(dev, reg, 0x00);
34 zeroes = pci_read_config8(dev, reg);
35
36 pci_write_config8(dev, reg, value);
37
38 return ones ^ zeroes;
39}
40static uint16_t pci_moving_config16(struct device *dev, unsigned reg)
41{
42 uint16_t value, ones, zeroes;
43 value = pci_read_config16(dev, reg);
44
45 pci_write_config16(dev, reg, 0xffff);
46 ones = pci_read_config16(dev, reg);
47
48 pci_write_config16(dev, reg, 0x0000);
49 zeroes = pci_read_config16(dev, reg);
50
51 pci_write_config16(dev, reg, value);
52
53 return ones ^ zeroes;
54}
55static uint32_t pci_moving_config32(struct device *dev, unsigned reg)
56{
57 uint32_t value, ones, zeroes;
58 value = pci_read_config32(dev, reg);
59
60 pci_write_config32(dev, reg, 0xffffffff);
61 ones = pci_read_config32(dev, reg);
62
63 pci_write_config32(dev, reg, 0x00000000);
64 zeroes = pci_read_config32(dev, reg);
65
66 pci_write_config32(dev, reg, value);
67
68 return ones ^ zeroes;
69}
70
71unsigned pci_find_capability(device_t dev, unsigned cap)
72{
73 unsigned pos;
74 pos = 0;
75 switch(dev->hdr_type & 0x7f) {
76 case PCI_HEADER_TYPE_NORMAL:
77 case PCI_HEADER_TYPE_BRIDGE:
78 pos = PCI_CAPABILITY_LIST;
79 break;
80 }
81 if (pos > PCI_CAP_LIST_NEXT) {
82 pos = pci_read_config8(dev, pos);
83 }
84 while(pos != 0) { /* loop through the linked list */
85 int this_cap;
86 this_cap = pci_read_config8(dev, pos + PCI_CAP_LIST_ID);
87 if (this_cap == cap) {
88 return pos;
89 }
90 }
91 return 0;
92}
93
Eric Biederman8ca8d762003-04-22 19:02:15 +000094
Eric Biederman8ca8d762003-04-22 19:02:15 +000095/** Given a device and register, read the size of the BAR for that register.
96 * @param dev Pointer to the device structure
97 * @param resource Pointer to the resource structure
98 * @param index Address of the pci configuration register
99 */
Eric Biederman03acab62004-10-14 21:25:53 +0000100struct resource *pci_get_resource(struct device *dev, unsigned long index)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000101{
Eric Biederman5cd81732004-03-11 15:01:31 +0000102 struct resource *resource;
Eric Biederman03acab62004-10-14 21:25:53 +0000103 unsigned long value, attr;
104 resource_t moving, limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000105
106 /* Initialize the resources to nothing */
Eric Biederman03acab62004-10-14 21:25:53 +0000107 resource = new_resource(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000108
Eric Biederman03acab62004-10-14 21:25:53 +0000109 /* Get the initial value */
110 value = pci_read_config32(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000111
Eric Biederman03acab62004-10-14 21:25:53 +0000112 /* See which bits move */
113 moving = pci_moving_config32(dev, index);
114
115 /* Initialize attr to the bits that do not move */
116 attr = value & ~moving;
117
118 /* If it is a 64bit resource look at the high half as well */
119 if (((attr & PCI_BASE_ADDRESS_SPACE_IO) == 0) &&
120 ((attr & PCI_BASE_ADDRESS_MEM_LIMIT_MASK) == PCI_BASE_ADDRESS_MEM_LIMIT_64))
121 {
122 /* Find the high bits that move */
123 moving |= ((resource_t)pci_moving_config32(dev, index + 4)) << 32;
124 }
125 /* Find the resource constraints.
126 *
127 * Start by finding the bits that move. From there:
128 * - Size is the least significant bit of the bits that move.
129 * - Limit is all of the bits that move plus all of the lower bits.
130 * See PCI Spec 6.2.5.1 ...
Eric Biederman8ca8d762003-04-22 19:02:15 +0000131 */
Eric Biederman03acab62004-10-14 21:25:53 +0000132 limit = 0;
133 if (moving) {
134 resource->size = 1;
135 resource->align = resource->gran = 0;
136 while(!(moving & resource->size)) {
137 resource->size <<= 1;
138 resource->align += 1;
139 resource->gran += 1;
140 }
141 resource->limit = limit = moving | (resource->size - 1);
142 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000143 /*
144 * some broken hardware has read-only registers that do not
Eric Biederman03acab62004-10-14 21:25:53 +0000145 * really size correctly.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000146 * Example: the acer m7229 has BARs 1-4 normally read-only.
147 * so BAR1 at offset 0x10 reads 0x1f1. If you size that register
148 * by writing 0xffffffff to it, it will read back as 0x1f1 -- a
149 * violation of the spec.
Eric Biederman03acab62004-10-14 21:25:53 +0000150 * We catch this case and ignore it by observing which bits move,
151 * This also catches the common case unimplemented registers
152 * that always read back as 0.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000153 */
Eric Biederman03acab62004-10-14 21:25:53 +0000154 if (moving == 0) {
155 if (value != 0) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000156 printk_debug(
Eric Biedermane9a271e32003-09-02 03:36:25 +0000157 "%s register %02x(%08x), read-only ignoring it\n",
Eric Biederman03acab62004-10-14 21:25:53 +0000158 dev_path(dev), index, value);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000159 }
160 resource->flags = 0;
161 }
Eric Biederman03acab62004-10-14 21:25:53 +0000162 else if (attr & PCI_BASE_ADDRESS_SPACE_IO) {
163 /* An I/O mapped base address */
164 attr &= PCI_BASE_ADDRESS_IO_ATTR_MASK;
Eric Biederman5cd81732004-03-11 15:01:31 +0000165 resource->flags |= IORESOURCE_IO;
Eric Biederman03acab62004-10-14 21:25:53 +0000166 /* I don't want to deal with 32bit I/O resources */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000167 resource->limit = 0xffff;
168 }
169 else {
170 /* A Memory mapped base address */
Eric Biederman03acab62004-10-14 21:25:53 +0000171 attr &= PCI_BASE_ADDRESS_MEM_ATTR_MASK;
Eric Biederman5cd81732004-03-11 15:01:31 +0000172 resource->flags |= IORESOURCE_MEM;
Eric Biederman03acab62004-10-14 21:25:53 +0000173 if (attr & PCI_BASE_ADDRESS_MEM_PREFETCH) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000174 resource->flags |= IORESOURCE_PREFETCH;
175 }
Eric Biederman03acab62004-10-14 21:25:53 +0000176 attr &= PCI_BASE_ADDRESS_MEM_LIMIT_MASK;
177 if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_32) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000178 /* 32bit limit */
179 resource->limit = 0xffffffffUL;
180 }
Eric Biederman03acab62004-10-14 21:25:53 +0000181 else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_1M) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000182 /* 1MB limit */
183 resource->limit = 0x000fffffUL;
184 }
Eric Biederman03acab62004-10-14 21:25:53 +0000185 else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_64) {
186 /* 64bit limit */
187 resource->limit = 0xffffffffffffffffULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000188 resource->flags |= IORESOURCE_PCI64;
Eric Biederman03acab62004-10-14 21:25:53 +0000189 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000190 else {
191 /* Invalid value */
192 resource->flags = 0;
193 }
194 }
Eric Biederman03acab62004-10-14 21:25:53 +0000195 /* Don't let the limit exceed which bits can move */
196 if (resource->limit > limit) {
197 resource->limit = limit;
198 }
199#if 0
200 if (resource->flags) {
201 printk_debug("%s %02x ->",
202 dev_path(dev), resource->index);
203 printk_debug(" value: 0x%08Lx zeroes: 0x%08Lx ones: 0x%08Lx attr: %08lx\n",
204 value, zeroes, ones, attr);
205 printk_debug(
206 "%s %02x -> size: 0x%08Lx max: 0x%08Lx %s%s\n ",
207 dev_path(dev),
208 resource->index,
209 resource->size, resource->limit,
210 (resource->flags == 0) ? "unused":
211 (resource->flags & IORESOURCE_IO)? "io":
212 (resource->flags & IORESOURCE_PREFETCH)? "prefmem": "mem",
213 (resource->flags & IORESOURCE_PCI64)?"64":"");
214 }
215#endif
216
Eric Biederman5cd81732004-03-11 15:01:31 +0000217 return resource;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000218}
219
220/** Read the base address registers for a given device.
221 * @param dev Pointer to the dev structure
222 * @param howmany How many registers to read (6 for device, 2 for bridge)
223 */
224static void pci_read_bases(struct device *dev, unsigned int howmany)
225{
Eric Biederman8ca8d762003-04-22 19:02:15 +0000226 unsigned long index;
227
Eric Biedermanb78c1972004-10-14 20:54:17 +0000228 for(index = PCI_BASE_ADDRESS_0; (index < PCI_BASE_ADDRESS_0 + (howmany << 2)); ) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000229 struct resource *resource;
Eric Biederman5cd81732004-03-11 15:01:31 +0000230 resource = pci_get_resource(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000231 index += (resource->flags & IORESOURCE_PCI64)?8:4;
232 }
Eric Biederman5cd81732004-03-11 15:01:31 +0000233 compact_resources(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000234}
235
Eric Biederman03acab62004-10-14 21:25:53 +0000236static void pci_set_resource(struct device *dev, struct resource *resource);
237
238static void pci_record_bridge_resource(
239 struct device *dev, resource_t moving,
240 unsigned index, unsigned long mask, unsigned long type)
241{
242 /* Initiliaze the constraints on the current bus */
243 struct resource *resource;
244 resource = 0;
245 if (moving) {
246 unsigned long gran;
247 resource_t step;
248 resource = new_resource(dev, index);
249 resource->size = 0;
250 gran = 0;
251 step = 1;
252 while((moving & step) == 0) {
253 gran += 1;
254 step <<= 1;
255 }
256 resource->gran = gran;
257 resource->align = gran;
258 resource->limit = moving | (step - 1);
259 resource->flags = type | IORESOURCE_PCI_BRIDGE;
260 compute_allocate_resource(&dev->link[0], resource, mask, type);
261 /* If there is nothing behind the resource,
262 * clear it and forget it.
263 */
264 if (resource->size == 0) {
265 resource->base = moving;
266 resource->flags |= IORESOURCE_ASSIGNED;
267 resource->flags &= ~IORESOURCE_STORED;
268 pci_set_resource(dev, resource);
269 resource->flags = 0;
270 }
271 }
272 return;
273}
274
275
Eric Biederman8ca8d762003-04-22 19:02:15 +0000276static void pci_bridge_read_bases(struct device *dev)
277{
Eric Biederman03acab62004-10-14 21:25:53 +0000278 resource_t moving_base, moving_limit, moving;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000279
Eric Biederman03acab62004-10-14 21:25:53 +0000280
281 /* See if the bridge I/O resources are implemented */
282 moving_base = ((uint32_t)pci_moving_config8(dev, PCI_IO_BASE)) << 8;
283 moving_base |= ((uint32_t)pci_moving_config16(dev, PCI_IO_BASE_UPPER16)) << 16;
284
285 moving_limit = ((uint32_t)pci_moving_config8(dev, PCI_IO_LIMIT)) << 8;
286 moving_limit |= ((uint32_t)pci_moving_config16(dev, PCI_IO_LIMIT_UPPER16)) << 16;
287
288 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000289
290 /* Initialize the io space constraints on the current bus */
Eric Biederman03acab62004-10-14 21:25:53 +0000291 pci_record_bridge_resource(
292 dev, moving, PCI_IO_BASE,
Eric Biedermanb78c1972004-10-14 20:54:17 +0000293 IORESOURCE_IO, IORESOURCE_IO);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000294
Eric Biederman03acab62004-10-14 21:25:53 +0000295
296 /* See if the bridge prefmem resources are implemented */
297 moving_base = ((resource_t)pci_moving_config16(dev, PCI_PREF_MEMORY_BASE)) << 16;
298 moving_base |= ((resource_t)pci_moving_config32(dev, PCI_PREF_BASE_UPPER32)) << 32;
299
300 moving_limit = ((resource_t)pci_moving_config16(dev, PCI_PREF_MEMORY_LIMIT)) << 16;
301 moving_limit |= ((resource_t)pci_moving_config32(dev, PCI_PREF_LIMIT_UPPER32)) << 32;
302
303 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000304 /* Initiliaze the prefetchable memory constraints on the current bus */
Eric Biederman03acab62004-10-14 21:25:53 +0000305 pci_record_bridge_resource(
306 dev, moving, PCI_PREF_MEMORY_BASE,
307 IORESOURCE_MEM | IORESOURCE_PREFETCH,
Eric Biedermanb78c1972004-10-14 20:54:17 +0000308 IORESOURCE_MEM | IORESOURCE_PREFETCH);
Eric Biederman03acab62004-10-14 21:25:53 +0000309
310
311 /* See if the bridge mem resources are implemented */
312 moving_base = ((uint32_t)pci_moving_config16(dev, PCI_MEMORY_BASE)) << 16;
313 moving_limit = ((uint32_t)pci_moving_config16(dev, PCI_MEMORY_LIMIT)) << 16;
314
315 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000316
317 /* Initialize the memory resources on the current bus */
Eric Biederman03acab62004-10-14 21:25:53 +0000318 pci_record_bridge_resource(
319 dev, moving, PCI_MEMORY_BASE,
Eric Biedermanb78c1972004-10-14 20:54:17 +0000320 IORESOURCE_MEM | IORESOURCE_PREFETCH,
321 IORESOURCE_MEM);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000322
Eric Biederman5cd81732004-03-11 15:01:31 +0000323 compact_resources(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000324}
325
Eric Biederman5899fd82003-04-24 06:25:08 +0000326void pci_dev_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000327{
328 uint32_t addr;
Li-Ta Loe5266692004-03-23 21:28:05 +0000329
Eric Biederman8ca8d762003-04-22 19:02:15 +0000330 pci_read_bases(dev, 6);
Li-Ta Loe5266692004-03-23 21:28:05 +0000331
Eric Biederman7a5416a2003-06-12 19:23:51 +0000332 addr = pci_read_config32(dev, PCI_ROM_ADDRESS);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000333 dev->rom_address = (addr == 0xffffffff)? 0 : addr;
334}
335
Eric Biederman5899fd82003-04-24 06:25:08 +0000336void pci_bus_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000337{
338 uint32_t addr;
Li-Ta Loe5266692004-03-23 21:28:05 +0000339
Eric Biederman8ca8d762003-04-22 19:02:15 +0000340 pci_bridge_read_bases(dev);
341 pci_read_bases(dev, 2);
342
Eric Biederman7a5416a2003-06-12 19:23:51 +0000343 addr = pci_read_config32(dev, PCI_ROM_ADDRESS1);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000344 dev->rom_address = (addr == 0xffffffff)? 0 : addr;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000345}
346
Eric Biederman8ca8d762003-04-22 19:02:15 +0000347static void pci_set_resource(struct device *dev, struct resource *resource)
348{
Eric Biederman03acab62004-10-14 21:25:53 +0000349 resource_t base, end;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000350
Eric Biederman8ca8d762003-04-22 19:02:15 +0000351 /* Make certain the resource has actually been set */
Eric Biederman5cd81732004-03-11 15:01:31 +0000352 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000353 printk_err("ERROR: %s %02x not allocated\n",
Eric Biedermanb78c1972004-10-14 20:54:17 +0000354 dev_path(dev), resource->index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000355 return;
356 }
357
Eric Biederman5cd81732004-03-11 15:01:31 +0000358 /* If I have already stored this resource don't worry about it */
359 if (resource->flags & IORESOURCE_STORED) {
360 return;
361 }
362
Eric Biederman03acab62004-10-14 21:25:53 +0000363 /* If the resources is substractive don't worry about it */
364 if (resource->flags & IORESOURCE_SUBTRACTIVE) {
365 return;
366 }
367
Eric Biederman8ca8d762003-04-22 19:02:15 +0000368 /* Only handle PCI memory and IO resources for now */
369 if (!(resource->flags & (IORESOURCE_MEM |IORESOURCE_IO)))
370 return;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000371
Eric Biederman03acab62004-10-14 21:25:53 +0000372 /* Enable the resources in the command register */
373 if (resource->size) {
374 if (resource->flags & IORESOURCE_MEM) {
375 dev->command |= PCI_COMMAND_MEMORY;
376 }
377 if (resource->flags & IORESOURCE_IO) {
378 dev->command |= PCI_COMMAND_IO;
379 }
380 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
381 dev->command |= PCI_COMMAND_MASTER;
382 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000383 }
384 /* Get the base address */
385 base = resource->base;
Eric Biederman5cd81732004-03-11 15:01:31 +0000386
Eric Biederman03acab62004-10-14 21:25:53 +0000387 /* Get the end */
388 end = resource_end(resource);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000389
Eric Biederman5cd81732004-03-11 15:01:31 +0000390 /* Now store the resource */
391 resource->flags |= IORESOURCE_STORED;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000392 if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) {
Eric Biederman03acab62004-10-14 21:25:53 +0000393 unsigned long base_lo, base_hi;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000394 /*
395 * some chipsets allow us to set/clear the IO bit.
396 * (e.g. VIA 82c686a.) So set it to be safe)
397 */
Eric Biederman03acab62004-10-14 21:25:53 +0000398 base_lo = base & 0xffffffff;
399 base_hi = (base >> 32) & 0xffffffff;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000400 if (resource->flags & IORESOURCE_IO) {
Eric Biederman03acab62004-10-14 21:25:53 +0000401 base_lo |= PCI_BASE_ADDRESS_SPACE_IO;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000402 }
Eric Biederman03acab62004-10-14 21:25:53 +0000403 pci_write_config32(dev, resource->index, base_lo);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000404 if (resource->flags & IORESOURCE_PCI64) {
Eric Biederman03acab62004-10-14 21:25:53 +0000405 pci_write_config32(dev, resource->index + 4, base_hi);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000406 }
Eric Biedermanb78c1972004-10-14 20:54:17 +0000407 }
408 else if (resource->index == PCI_IO_BASE) {
Eric Biederman03acab62004-10-14 21:25:53 +0000409 /* set the IO ranges */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000410 compute_allocate_resource(&dev->link[0], resource,
Eric Biedermanb78c1972004-10-14 20:54:17 +0000411 IORESOURCE_IO, IORESOURCE_IO);
Eric Biederman03acab62004-10-14 21:25:53 +0000412 pci_write_config8(dev, PCI_IO_BASE, base >> 8);
413 pci_write_config16(dev, PCI_IO_BASE_UPPER16, base >> 16);
414 pci_write_config8(dev, PCI_IO_LIMIT, end >> 8);
415 pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, end >> 16);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000416 }
417 else if (resource->index == PCI_MEMORY_BASE) {
Eric Biederman03acab62004-10-14 21:25:53 +0000418 /* set the memory range */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000419 compute_allocate_resource(&dev->link[0], resource,
Eric Biedermanb78c1972004-10-14 20:54:17 +0000420 IORESOURCE_MEM | IORESOURCE_PREFETCH,
421 IORESOURCE_MEM);
Eric Biederman7a5416a2003-06-12 19:23:51 +0000422 pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16);
Eric Biederman03acab62004-10-14 21:25:53 +0000423 pci_write_config16(dev, PCI_MEMORY_LIMIT, end >> 16);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000424 }
425 else if (resource->index == PCI_PREF_MEMORY_BASE) {
Eric Biederman03acab62004-10-14 21:25:53 +0000426 /* set the prefetchable memory range */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000427 compute_allocate_resource(&dev->link[0], resource,
Eric Biedermanb78c1972004-10-14 20:54:17 +0000428 IORESOURCE_MEM | IORESOURCE_PREFETCH,
429 IORESOURCE_MEM | IORESOURCE_PREFETCH);
Eric Biederman03acab62004-10-14 21:25:53 +0000430 pci_write_config16(dev, PCI_PREF_MEMORY_BASE, base >> 16);
431 pci_write_config32(dev, PCI_PREF_BASE_UPPER32, base >> 32);
432 pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, end >> 16);
433 pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, end >> 32);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000434 }
435 else {
Eric Biederman5cd81732004-03-11 15:01:31 +0000436 /* Don't let me think I stored the resource */
437 resource->flags &= ~IORESOURCE_STORED;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000438 printk_err("ERROR: invalid resource->index %x\n",
Eric Biedermanb78c1972004-10-14 20:54:17 +0000439 resource->index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000440 }
Eric Biederman03acab62004-10-14 21:25:53 +0000441 report_resource_stored(dev, resource, "");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000442 return;
443}
444
Eric Biederman5899fd82003-04-24 06:25:08 +0000445void pci_dev_set_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000446{
447 struct resource *resource, *last;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000448 unsigned link;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000449 uint8_t line;
450
451 last = &dev->resource[dev->resources];
Eric Biedermanb78c1972004-10-14 20:54:17 +0000452
453 for(resource = &dev->resource[0]; resource < last; resource++) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000454 pci_set_resource(dev, resource);
455 }
Eric Biedermanb78c1972004-10-14 20:54:17 +0000456 for(link = 0; link < dev->links; link++) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000457 struct bus *bus;
458 bus = &dev->link[link];
459 if (bus->children) {
460 assign_resources(bus);
461 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000462 }
463
464 /* set a default latency timer */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000465 pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000466
467 /* set a default secondary latency timer */
468 if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
Eric Biederman7a5416a2003-06-12 19:23:51 +0000469 pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000470 }
471
472 /* zero the irq settings */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000473 line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000474 if (line) {
Eric Biederman7a5416a2003-06-12 19:23:51 +0000475 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000476 }
477 /* set the cache line size, so far 64 bytes is good for everyone */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000478 pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000479}
480
Eric Biedermane9a271e32003-09-02 03:36:25 +0000481void pci_dev_enable_resources(struct device *dev)
482{
Eric Biederman03acab62004-10-14 21:25:53 +0000483 struct pci_operations *ops;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000484 uint16_t command;
Eric Biederman03acab62004-10-14 21:25:53 +0000485
486 /* Set the subsystem vendor and device id for mainboard devices */
487 ops = ops_pci(dev);
Eric Biedermandbec2d42004-10-21 10:44:08 +0000488 if (dev->on_mainboard && ops && ops->set_subsystem) {
Eric Biederman03acab62004-10-14 21:25:53 +0000489 printk_debug("%s subsystem <- %02x/%02x\n",
490 dev_path(dev),
491 MAINBOARD_PCI_SUBSYSTEM_VENDOR_ID,
492 MAINBOARD_PCI_SUBSYSTEM_DEVICE_ID);
493 ops->set_subsystem(dev,
494 MAINBOARD_PCI_SUBSYSTEM_VENDOR_ID,
495 MAINBOARD_PCI_SUBSYSTEM_DEVICE_ID);
496 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000497 command = pci_read_config16(dev, PCI_COMMAND);
498 command |= dev->command;
Eric Biederman5cd81732004-03-11 15:01:31 +0000499 command |= (PCI_COMMAND_PARITY + PCI_COMMAND_SERR); /* error check */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000500 printk_debug("%s cmd <- %02x\n", dev_path(dev), command);
501 pci_write_config16(dev, PCI_COMMAND, command);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000502}
503
504void pci_bus_enable_resources(struct device *dev)
505{
506 uint16_t ctrl;
507 ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
508 ctrl |= dev->link[0].bridge_ctrl;
Eric Biederman5cd81732004-03-11 15:01:31 +0000509 ctrl |= (PCI_BRIDGE_CTL_PARITY + PCI_BRIDGE_CTL_SERR); /* error check */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000510 printk_debug("%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
511 pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
512
513 pci_dev_enable_resources(dev);
Eric Biedermandbec2d42004-10-21 10:44:08 +0000514
515 enable_childrens_resources(dev);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000516}
517
Eric Biedermandbec2d42004-10-21 10:44:08 +0000518void pci_dev_set_subsystem(device_t dev, unsigned vendor, unsigned device)
Eric Biederman03acab62004-10-14 21:25:53 +0000519{
520 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
521 ((device & 0xffff) << 16) | (vendor & 0xffff));
522}
523
Li-Ta Loe5266692004-03-23 21:28:05 +0000524/** Default device operation for PCI devices */
Eric Biederman03acab62004-10-14 21:25:53 +0000525static struct pci_operations pci_ops_pci_dev = {
526 .set_subsystem = pci_dev_set_subsystem,
527};
528
Eric Biederman8ca8d762003-04-22 19:02:15 +0000529struct device_operations default_pci_ops_dev = {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000530 .read_resources = pci_dev_read_resources,
531 .set_resources = pci_dev_set_resources,
532 .enable_resources = pci_dev_enable_resources,
Li-Ta Loe5266692004-03-23 21:28:05 +0000533 .init = 0,
534 .scan_bus = 0,
Eric Biederman03acab62004-10-14 21:25:53 +0000535 .enable = 0,
536 .ops_pci = &pci_ops_pci_dev,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000537};
Li-Ta Loe5266692004-03-23 21:28:05 +0000538
539/** Default device operations for PCI bridges */
Eric Biederman03acab62004-10-14 21:25:53 +0000540static struct pci_operations pci_ops_pci_bus = {
541 .set_subsystem = 0,
542};
Eric Biederman8ca8d762003-04-22 19:02:15 +0000543struct device_operations default_pci_ops_bus = {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000544 .read_resources = pci_bus_read_resources,
545 .set_resources = pci_dev_set_resources,
546 .enable_resources = pci_bus_enable_resources,
Li-Ta Loe5266692004-03-23 21:28:05 +0000547 .init = 0,
548 .scan_bus = pci_scan_bridge,
Eric Biederman03acab62004-10-14 21:25:53 +0000549 .enable = 0,
550 .ops_pci = &pci_ops_pci_bus,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000551};
Li-Ta Loe5266692004-03-23 21:28:05 +0000552
553/**
554 * @brief Set up PCI device operation
555 *
556 *
557 * @param dev
558 *
559 * @see pci_drivers
560 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000561static void set_pci_ops(struct device *dev)
562{
563 struct pci_driver *driver;
564 if (dev->ops) {
565 return;
566 }
Li-Ta Loe5266692004-03-23 21:28:05 +0000567
Eric Biederman8ca8d762003-04-22 19:02:15 +0000568 /* Look through the list of setup drivers and find one for
Eric Biedermanb78c1972004-10-14 20:54:17 +0000569 * this pci device
570 */
571 for(driver = &pci_drivers[0]; driver != &epci_drivers[0]; driver++) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000572 if ((driver->vendor == dev->vendor) &&
Eric Biedermanb78c1972004-10-14 20:54:17 +0000573 (driver->device == dev->device))
574 {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000575 dev->ops = driver->ops;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000576 printk_debug("%s [%04x/%04x] %sops\n",
577 dev_path(dev),
578 driver->vendor, driver->device,
579 (driver->ops->scan_bus?"bus ":""));
Eric Biederman5899fd82003-04-24 06:25:08 +0000580 return;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000581 }
582 }
Li-Ta Loe5266692004-03-23 21:28:05 +0000583
Eric Biederman8ca8d762003-04-22 19:02:15 +0000584 /* If I don't have a specific driver use the default operations */
585 switch(dev->hdr_type & 0x7f) { /* header type */
586 case PCI_HEADER_TYPE_NORMAL: /* standard header */
587 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
588 goto bad;
589 dev->ops = &default_pci_ops_dev;
590 break;
591 case PCI_HEADER_TYPE_BRIDGE:
592 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
593 goto bad;
594 dev->ops = &default_pci_ops_bus;
595 break;
596 default:
597 bad:
Li-Ta Lo69c5a902004-04-29 20:08:54 +0000598 if (dev->enabled) {
Eric Biederman83b991a2003-10-11 06:20:25 +0000599 printk_err("%s [%04x/%04x/%06x] has unknown header "
Eric Biedermanb78c1972004-10-14 20:54:17 +0000600 "type %02x, ignoring.\n",
601 dev_path(dev),
602 dev->vendor, dev->device,
603 dev->class >> 8, dev->hdr_type);
Eric Biederman83b991a2003-10-11 06:20:25 +0000604 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000605 }
606 return;
607}
608
609/**
Eric Biederman03acab62004-10-14 21:25:53 +0000610 * @brief See if we have already allocated a device structure for a given devfn.
Li-Ta Loe5266692004-03-23 21:28:05 +0000611 *
612 * Given a linked list of PCI device structures and a devfn number, find the
Eric Biedermanb78c1972004-10-14 20:54:17 +0000613 * device structure correspond to the devfn, if present.
Li-Ta Loe5266692004-03-23 21:28:05 +0000614 *
615 * @param list the device structure list
Eric Biederman8ca8d762003-04-22 19:02:15 +0000616 * @param devfn a device/function number
Li-Ta Loe5266692004-03-23 21:28:05 +0000617 *
Eric Biedermanb78c1972004-10-14 20:54:17 +0000618 * @return pointer to the device structure found or null of we have not allocated
619 * a device for this devfn yet.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000620 */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000621static struct device *pci_scan_get_dev(struct device **list, unsigned int devfn)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000622{
Eric Biedermanb78c1972004-10-14 20:54:17 +0000623 struct device *dev;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000624 dev = 0;
625 for(; *list; list = &(*list)->sibling) {
Eric Biedermanad1b35a2003-10-14 02:36:51 +0000626 if ((*list)->path.type != DEVICE_PATH_PCI) {
Li-Ta Loe5266692004-03-23 21:28:05 +0000627 printk_err("child %s not a pci device\n",
Eric Biedermanb78c1972004-10-14 20:54:17 +0000628 dev_path(*list));
Eric Biedermanad1b35a2003-10-14 02:36:51 +0000629 continue;
630 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000631 if ((*list)->path.u.pci.devfn == devfn) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000632 /* Unlink from the list */
633 dev = *list;
634 *list = (*list)->sibling;
635 dev->sibling = 0;
636 break;
637 }
638 }
Eric Biedermanb78c1972004-10-14 20:54:17 +0000639 /* Just like alloc_dev add the device to the
640 * list of device on the bus. When the list of devices was formed
641 * we removed all of the parents children, and now we are interleaving
642 * static and dynamic devices in order on the bus.
643 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000644 if (dev) {
645 device_t child;
646 /* Find the last child of our parent */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000647 for(child = dev->bus->children; child && child->sibling; ) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000648 child = child->sibling;
649 }
650 /* Place the device on the list of children of it's parent. */
651 if (child) {
652 child->sibling = dev;
653 } else {
654 dev->bus->children = dev;
655 }
656 }
657
Eric Biederman8ca8d762003-04-22 19:02:15 +0000658 return dev;
659}
660
Eric Biedermanb78c1972004-10-14 20:54:17 +0000661/**
662 * @brief Scan a PCI bus.
Li-Ta Loe5266692004-03-23 21:28:05 +0000663 *
664 * Determine the existence of devices and bridges on a PCI bus. If there are
665 * bridges on the bus, recursively scan the buses behind the bridges.
666 *
667 * This function is the default scan_bus() method for the root device
668 * 'dev_root'.
669 *
Eric Biedermane9a271e32003-09-02 03:36:25 +0000670 * @param bus pointer to the bus structure
671 * @param min_devfn minimum devfn to look at in the scan usually 0x00
672 * @param max_devfn maximum devfn to look at in the scan usually 0xff
Eric Biederman8ca8d762003-04-22 19:02:15 +0000673 * @param max current bus number
Li-Ta Loe5266692004-03-23 21:28:05 +0000674 *
Eric Biederman8ca8d762003-04-22 19:02:15 +0000675 * @return The maximum bus number found, after scanning all subordinate busses
676 */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000677unsigned int pci_scan_bus(struct bus *bus,
678 unsigned min_devfn, unsigned max_devfn,
679 unsigned int max)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000680{
681 unsigned int devfn;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000682 device_t dev;
683 device_t old_devices;
684 device_t child;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000685
686 printk_debug("PCI: pci_scan_bus for bus %d\n", bus->secondary);
687
688 old_devices = bus->children;
689 bus->children = 0;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000690
691 post_code(0x24);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000692
Li-Ta Lo9782f752004-05-05 21:15:42 +0000693 /* probe all devices/functions on this bus with some optimization for
Eric Biedermanb78c1972004-10-14 20:54:17 +0000694 * non-existence and single funcion devices
695 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000696 for (devfn = min_devfn; devfn <= max_devfn; devfn++) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000697 uint32_t id, class;
Eric Biederman30e143a2003-09-01 23:45:32 +0000698 uint8_t hdr_type;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000699
Eric Biederman03acab62004-10-14 21:25:53 +0000700 /* First thing setup the device structure */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000701 dev = pci_scan_get_dev(&old_devices, devfn);
Eric Biederman03acab62004-10-14 21:25:53 +0000702
703 /* Detect if a device is present */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000704 if (!dev) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000705 struct device dummy;
706 dummy.bus = bus;
707 dummy.path.type = DEVICE_PATH_PCI;
708 dummy.path.u.pci.devfn = devfn;
709 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
710 /* some broken boards return 0 if a slot is empty: */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000711 if ( (id == 0xffffffff) || (id == 0x00000000) ||
712 (id == 0x0000ffff) || (id == 0xffff0000))
713 {
714 printk_spew("PCI: devfn 0x%x, bad id 0x%x\n", devfn, id);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000715 if (PCI_FUNC(devfn) == 0x00) {
Eric Biedermanb78c1972004-10-14 20:54:17 +0000716 /* if this is a function 0 device and
717 * it is not present,
718 * skip to next device
719 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000720 devfn += 0x07;
721 }
Eric Biedermanb78c1972004-10-14 20:54:17 +0000722 /* This function in a multi function device is
723 * not present, skip to the next function.
724 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000725 continue;
726 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000727 dev = alloc_dev(bus, &dummy.path);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000728 }
729 else {
Eric Biederman03acab62004-10-14 21:25:53 +0000730 /* Enable/disable the device. Once we have
731 * found the device specific operations this
732 * operations we will disable the device with
733 * those as well.
734 *
Eric Biedermanb78c1972004-10-14 20:54:17 +0000735 * This is geared toward devices that have subfunctions
736 * that do not show up by default.
737 *
738 * If a device is a stuff option on the motherboard
739 * it may be absent and enable_dev must cope.
740 *
741 */
Eric Biederman7003ba42004-10-16 06:20:29 +0000742 if (dev->chip_ops && dev->chip_ops->enable_dev)
Eric Biedermanb78c1972004-10-14 20:54:17 +0000743 {
Eric Biederman7003ba42004-10-16 06:20:29 +0000744 dev->chip_ops->enable_dev(dev);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000745 }
746 /* Now read the vendor and device id */
747 id = pci_read_config32(dev, PCI_VENDOR_ID);
Eric Biederman03acab62004-10-14 21:25:53 +0000748
749 /* If the device does not have a pci id disable it.
750 * Possibly this is because we have already disabled
751 * the device. But this also handles optional devices
752 * that may not always show up.
753 */
754 if (id == 0xffffffff || id == 0x00000000 ||
755 id == 0x0000ffff || id == 0xffff0000)
756 {
757 if (dev->enabled) {
758 printk_info("Disabling static device: %s\n",
759 dev_path(dev));
760 dev->enabled = 0;
761 }
762 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000763 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000764 /* Read the rest of the pci configuration information */
765 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
766 class = pci_read_config32(dev, PCI_CLASS_REVISION);
Li-Ta Lo9782f752004-05-05 21:15:42 +0000767
Eric Biedermane9a271e32003-09-02 03:36:25 +0000768 /* Store the interesting information in the device structure */
769 dev->vendor = id & 0xffff;
770 dev->device = (id >> 16) & 0xffff;
771 dev->hdr_type = hdr_type;
772 /* class code, the upper 3 bytes of PCI_CLASS_REVISION */
773 dev->class = class >> 8;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000774
Eric Biederman03acab62004-10-14 21:25:53 +0000775 /* Architectural/System devices always need to
776 * be bus masters.
777 */
778 if ((dev->class >> 16) == PCI_BASE_CLASS_SYSTEM) {
779 dev->command |= PCI_COMMAND_MASTER;
780 }
781
Eric Biederman8ca8d762003-04-22 19:02:15 +0000782 /* Look at the vendor and device id, or at least the
Li-Ta Loe5266692004-03-23 21:28:05 +0000783 * header type and class and figure out which set of
784 * configuration methods to use. Unless we already
785 * have some pci ops.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000786 */
Eric Biederman83b991a2003-10-11 06:20:25 +0000787 set_pci_ops(dev);
788 /* Error if we don't have some pci operations for it */
Eric Biederman5cd81732004-03-11 15:01:31 +0000789 if (!dev->ops) {
Eric Biederman83b991a2003-10-11 06:20:25 +0000790 printk_err("%s No device operations\n",
Eric Biedermanb78c1972004-10-14 20:54:17 +0000791 dev_path(dev));
Eric Biederman83b991a2003-10-11 06:20:25 +0000792 continue;
793 }
794
795 /* Now run the magic enable/disable sequence for the device */
796 if (dev->ops && dev->ops->enable) {
797 dev->ops->enable(dev);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000798 }
Eric Biederman4086d162003-07-17 03:26:03 +0000799
Eric Biedermane9a271e32003-09-02 03:36:25 +0000800 printk_debug("%s [%04x/%04x] %s\n",
Eric Biederman03acab62004-10-14 21:25:53 +0000801 dev_path(dev),
802 dev->vendor, dev->device,
803 dev->enabled?"enabled": "disabled");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000804
Eric Biederman8ca8d762003-04-22 19:02:15 +0000805 if (PCI_FUNC(devfn) == 0x00 && (hdr_type & 0x80) != 0x80) {
Eric Biedermanb78c1972004-10-14 20:54:17 +0000806 /* if this is not a multi function device,
807 * don't waste time probing another function.
808 * Skip to next device.
809 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000810 devfn += 0x07;
811 }
812 }
813 post_code(0x25);
814
Eric Biedermanb78c1972004-10-14 20:54:17 +0000815 /* For all children that implement scan_bus (i.e. bridges)
816 * scan the bus behind that child.
817 */
818 for(child = bus->children; child; child = child->sibling) {
819 if (!child->enabled ||
820 !child->ops ||
821 !child->ops->scan_bus)
822 {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000823 continue;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000824 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000825 max = child->ops->scan_bus(child, max);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000826 }
Li-Ta Loe5266692004-03-23 21:28:05 +0000827
Eric Biederman8ca8d762003-04-22 19:02:15 +0000828 /*
829 * We've scanned the bus and so we know all about what's on
830 * the other side of any bridges that may be on this bus plus
831 * any devices.
832 *
833 * Return how far we've got finding sub-buses.
834 */
835 printk_debug("PCI: pci_scan_bus returning with max=%02x\n", max);
836 post_code(0x55);
837 return max;
838}
839
Li-Ta Loe5266692004-03-23 21:28:05 +0000840/**
841 * @brief Scan a PCI bridge and the buses behind the bridge.
842 *
843 * Determine the existence of buses behind the bridge. Set up the bridge
844 * according to the result of the scan.
845 *
846 * This function is the default scan_bus() method for PCI bridge devices.
847 *
848 * @param dev pointer to the bridge device
849 * @param max the highest bus number assgined up to now
850 *
Eric Biederman8ca8d762003-04-22 19:02:15 +0000851 * @return The maximum bus number found, after scanning all subordinate busses
852 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000853unsigned int pci_scan_bridge(struct device *dev, unsigned int max)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000854{
Eric Biedermane9a271e32003-09-02 03:36:25 +0000855 struct bus *bus;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000856 uint32_t buses;
857 uint16_t cr;
Eric Biederman83b991a2003-10-11 06:20:25 +0000858
Eric Biedermane9a271e32003-09-02 03:36:25 +0000859 bus = &dev->link[0];
860 dev->links = 1;
861
Eric Biederman8ca8d762003-04-22 19:02:15 +0000862 /* Set up the primary, secondary and subordinate bus numbers. We have
863 * no idea how many buses are behind this bridge yet, so we set the
Eric Biedermanb78c1972004-10-14 20:54:17 +0000864 * subordinate bus number to 0xff for the moment.
865 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000866 bus->secondary = ++max;
867 bus->subordinate = 0xff;
Li-Ta Loe5266692004-03-23 21:28:05 +0000868
Eric Biederman8ca8d762003-04-22 19:02:15 +0000869 /* Clear all status bits and turn off memory, I/O and master enables. */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000870 cr = pci_read_config16(dev, PCI_COMMAND);
871 pci_write_config16(dev, PCI_COMMAND, 0x0000);
872 pci_write_config16(dev, PCI_STATUS, 0xffff);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000873
Eric Biedermanb78c1972004-10-14 20:54:17 +0000874 /*
875 * Read the existing primary/secondary/subordinate bus
876 * number configuration.
877 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000878 buses = pci_read_config32(dev, PCI_PRIMARY_BUS);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000879
880 /* Configure the bus numbers for this bridge: the configuration
881 * transactions will not be propagated by the bridge if it is not
Eric Biedermanb78c1972004-10-14 20:54:17 +0000882 * correctly configured.
883 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000884 buses &= 0xff000000;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000885 buses |= (((unsigned int) (dev->bus->secondary) << 0) |
Eric Biedermanb78c1972004-10-14 20:54:17 +0000886 ((unsigned int) (bus->secondary) << 8) |
887 ((unsigned int) (bus->subordinate) << 16));
Eric Biedermane9a271e32003-09-02 03:36:25 +0000888 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000889
890 /* Now we can scan all subordinate buses
891 * i.e. the bus behind the bridge.
892 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000893 max = pci_scan_bus(bus, 0x00, 0xff, max);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000894
Eric Biederman8ca8d762003-04-22 19:02:15 +0000895 /* We know the number of buses behind this bridge. Set the subordinate
Eric Biedermanb78c1972004-10-14 20:54:17 +0000896 * bus number to its real value.
897 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000898 bus->subordinate = max;
899 buses = (buses & 0xff00ffff) |
900 ((unsigned int) (bus->subordinate) << 16);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000901 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
902 pci_write_config16(dev, PCI_COMMAND, cr);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000903
904 printk_spew("%s returns max %d\n", __func__, max);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000905 return max;
906}
Li-Ta Loe5266692004-03-23 21:28:05 +0000907
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +0000908/*
909 Tell the EISA int controller this int must be level triggered
910 THIS IS A KLUDGE -- sorry, this needs to get cleaned up.
911*/
912static void pci_level_irq(unsigned char intNum)
913{
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +0000914 unsigned short intBits = inb(0x4d0) | (((unsigned) inb(0x4d1)) << 8);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +0000915
Eric Biedermanb78c1972004-10-14 20:54:17 +0000916 printk_spew("%s: current ints are 0x%x\n", __func__, intBits);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +0000917 intBits |= (1 << intNum);
918
Eric Biedermanb78c1972004-10-14 20:54:17 +0000919 printk_spew("%s: try to set ints 0x%x\n", __func__, intBits);
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +0000920
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +0000921 // Write new values
922 outb((unsigned char) intBits, 0x4d0);
923 outb((unsigned char) (intBits >> 8), 0x4d1);
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +0000924
Ronald G. Minnichb56ef072003-10-15 20:05:11 +0000925 /* this seems like an error but is not ... */
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000926#if 1
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +0000927 if (inb(0x4d0) != (intBits & 0xf)) {
928 printk_err("%s: lower order bits are wrong: want 0x%x, got 0x%x\n",
Eric Biedermanb78c1972004-10-14 20:54:17 +0000929 __func__, intBits &0xf, inb(0x4d0));
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +0000930 }
931 if (inb(0x4d1) != ((intBits >> 8) & 0xf)) {
932 printk_err("%s: lower order bits are wrong: want 0x%x, got 0x%x\n",
Eric Biedermanb78c1972004-10-14 20:54:17 +0000933 __func__, (intBits>>8) &0xf, inb(0x4d1));
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +0000934 }
Ronald G. Minnichb56ef072003-10-15 20:05:11 +0000935#endif
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +0000936}
937
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +0000938/*
939 This function assigns IRQs for all functions contained within
940 the indicated device address. If the device does not exist or does
941 not require interrupts then this function has no effect.
942
943 This function should be called for each PCI slot in your system.
944
945 pIntAtoD is an array of IRQ #s that are assigned to PINTA through PINTD of
946 this slot.
947 The particular irq #s that are passed in depend on the routing inside
948 your southbridge and on your motherboard.
949
950 -kevinh@ispiri.com
951*/
952void pci_assign_irqs(unsigned bus, unsigned slot,
953 const unsigned char pIntAtoD[4])
954{
955 unsigned functNum;
956 device_t pdev;
957 unsigned char line;
958 unsigned char irq;
959 unsigned char readback;
960
961 /* Each slot may contain up to eight functions */
962 for (functNum = 0; functNum < 8; functNum++) {
963 pdev = dev_find_slot(bus, (slot << 3) + functNum);
964
965 if (pdev) {
966 line = pci_read_config8(pdev, PCI_INTERRUPT_PIN);
967
968 // PCI spec says all other values are reserved
969 if ((line >= 1) && (line <= 4)) {
970 irq = pIntAtoD[line - 1];
971
972 printk_debug("Assigning IRQ %d to %d:%x.%d\n", \
973 irq, bus, slot, functNum);
974
975 pci_write_config8(pdev, PCI_INTERRUPT_LINE,\
976 pIntAtoD[line - 1]);
977
978 readback = pci_read_config8(pdev, PCI_INTERRUPT_LINE);
979 printk_debug(" Readback = %d\n", readback);
980
981 // Change to level triggered
982 pci_level_irq(pIntAtoD[line - 1]);
983 }
984 }
985 }
986}