blob: 6062fa638700baaed613fc64ee56a1abf287b8ec [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}
Li-Ta Lo9a5b4962004-12-23 21:48:01 +000040
Eric Biederman03acab62004-10-14 21:25:53 +000041static uint16_t pci_moving_config16(struct device *dev, unsigned reg)
42{
43 uint16_t value, ones, zeroes;
44 value = pci_read_config16(dev, reg);
45
46 pci_write_config16(dev, reg, 0xffff);
47 ones = pci_read_config16(dev, reg);
48
49 pci_write_config16(dev, reg, 0x0000);
50 zeroes = pci_read_config16(dev, reg);
51
52 pci_write_config16(dev, reg, value);
53
54 return ones ^ zeroes;
55}
56static uint32_t pci_moving_config32(struct device *dev, unsigned reg)
57{
58 uint32_t value, ones, zeroes;
59 value = pci_read_config32(dev, reg);
60
61 pci_write_config32(dev, reg, 0xffffffff);
62 ones = pci_read_config32(dev, reg);
63
64 pci_write_config32(dev, reg, 0x00000000);
65 zeroes = pci_read_config32(dev, reg);
66
67 pci_write_config32(dev, reg, value);
68
69 return ones ^ zeroes;
70}
71
72unsigned pci_find_capability(device_t dev, unsigned cap)
73{
74 unsigned pos;
75 pos = 0;
76 switch(dev->hdr_type & 0x7f) {
77 case PCI_HEADER_TYPE_NORMAL:
78 case PCI_HEADER_TYPE_BRIDGE:
79 pos = PCI_CAPABILITY_LIST;
80 break;
81 }
82 if (pos > PCI_CAP_LIST_NEXT) {
83 pos = pci_read_config8(dev, pos);
84 }
Li-Ta Lo9a5b4962004-12-23 21:48:01 +000085 while (pos != 0) { /* loop through the linked list */
Eric Biederman03acab62004-10-14 21:25:53 +000086 int this_cap;
87 this_cap = pci_read_config8(dev, pos + PCI_CAP_LIST_ID);
88 if (this_cap == cap) {
89 return pos;
90 }
91 }
92 return 0;
93}
94
Eric Biederman8ca8d762003-04-22 19:02:15 +000095
Eric Biederman8ca8d762003-04-22 19:02:15 +000096/** Given a device and register, read the size of the BAR for that register.
97 * @param dev Pointer to the device structure
98 * @param resource Pointer to the resource structure
99 * @param index Address of the pci configuration register
100 */
Eric Biederman03acab62004-10-14 21:25:53 +0000101struct resource *pci_get_resource(struct device *dev, unsigned long index)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000102{
Eric Biederman5cd81732004-03-11 15:01:31 +0000103 struct resource *resource;
Eric Biederman03acab62004-10-14 21:25:53 +0000104 unsigned long value, attr;
105 resource_t moving, limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000106
107 /* Initialize the resources to nothing */
Eric Biederman03acab62004-10-14 21:25:53 +0000108 resource = new_resource(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000109
Eric Biederman03acab62004-10-14 21:25:53 +0000110 /* Get the initial value */
111 value = pci_read_config32(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000112
Eric Biederman03acab62004-10-14 21:25:53 +0000113 /* See which bits move */
114 moving = pci_moving_config32(dev, index);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000115
Eric Biederman03acab62004-10-14 21:25:53 +0000116 /* Initialize attr to the bits that do not move */
117 attr = value & ~moving;
118
119 /* If it is a 64bit resource look at the high half as well */
120 if (((attr & PCI_BASE_ADDRESS_SPACE_IO) == 0) &&
Li-Ta Lo3a812852004-12-03 22:39:34 +0000121 ((attr & PCI_BASE_ADDRESS_MEM_LIMIT_MASK) == PCI_BASE_ADDRESS_MEM_LIMIT_64))
Eric Biederman03acab62004-10-14 21:25:53 +0000122 {
123 /* Find the high bits that move */
124 moving |= ((resource_t)pci_moving_config32(dev, index + 4)) << 32;
125 }
126 /* Find the resource constraints.
127 *
128 * Start by finding the bits that move. From there:
129 * - Size is the least significant bit of the bits that move.
130 * - Limit is all of the bits that move plus all of the lower bits.
131 * See PCI Spec 6.2.5.1 ...
Eric Biederman8ca8d762003-04-22 19:02:15 +0000132 */
Eric Biederman03acab62004-10-14 21:25:53 +0000133 limit = 0;
134 if (moving) {
135 resource->size = 1;
136 resource->align = resource->gran = 0;
137 while(!(moving & resource->size)) {
138 resource->size <<= 1;
139 resource->align += 1;
140 resource->gran += 1;
141 }
142 resource->limit = limit = moving | (resource->size - 1);
143 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000144 /*
145 * some broken hardware has read-only registers that do not
Eric Biederman03acab62004-10-14 21:25:53 +0000146 * really size correctly.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000147 * Example: the acer m7229 has BARs 1-4 normally read-only.
148 * so BAR1 at offset 0x10 reads 0x1f1. If you size that register
149 * by writing 0xffffffff to it, it will read back as 0x1f1 -- a
150 * violation of the spec.
Eric Biederman03acab62004-10-14 21:25:53 +0000151 * We catch this case and ignore it by observing which bits move,
152 * This also catches the common case unimplemented registers
153 * that always read back as 0.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000154 */
Eric Biederman03acab62004-10-14 21:25:53 +0000155 if (moving == 0) {
156 if (value != 0) {
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000157 printk_debug("%s register %02x(%08x), read-only ignoring it\n",
158 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 ->",
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000202 dev_path(dev), resource->index);
Eric Biederman03acab62004-10-14 21:25:53 +0000203 printk_debug(" value: 0x%08Lx zeroes: 0x%08Lx ones: 0x%08Lx attr: %08lx\n",
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000204 value, zeroes, ones, attr);
Eric Biederman03acab62004-10-14 21:25:53 +0000205 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
Li-Ta Lo9a5b4962004-12-23 21:48:01 +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
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000236static void pci_read_rom_resource(struct device *dev, unsigned long index)
237{
238 struct resource *resource;
239 unsigned long value, attr;
240 resource_t moving, limit;
241
242 /* Initialize the resources to nothing */
243 resource = new_resource(dev, index);
244
245 /* Get the initial value */
246 value = pci_read_config32(dev, index);
247
248 /* See which bits move */
249 moving = pci_moving_config32(dev, index);
250 /* clear the Enable bit */
251 moving = moving & 0xffffffffe;
252
253 /* Find the resource constraints.
254 *
255 * Start by finding the bits that move. From there:
256 * - Size is the least significant bit of the bits that move.
257 * - Limit is all of the bits that move plus all of the lower bits.
258 * See PCI Spec 6.2.5.1 ...
259 */
260 limit = 0;
261
262 if (moving) {
263 resource->size = 1;
264 resource->align = resource->gran = 0;
265 while(!(moving & resource->size)) {
266 resource->size <<= 1;
267 resource->align += 1;
268 resource->gran += 1;
269 }
270 resource->limit = limit = moving | (resource->size - 1);
271 printk_debug("%s, rom size: %x, limit: %x\n",
272 dev_path(dev), resource->size, limit);
273 }
274
275 if (moving == 0) {
276 if (value != 0) {
277 printk_debug("%s register %02x(%08x), read-only ignoring it\n",
278 dev_path(dev), index, value);
279 }
280 resource->flags = 0;
281 } else {
282 resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY;
283 }
284 compact_resources(dev);
285
286}
287
Eric Biederman03acab62004-10-14 21:25:53 +0000288static void pci_set_resource(struct device *dev, struct resource *resource);
289
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000290static void pci_record_bridge_resource( struct device *dev, resource_t moving,
291 unsigned index, unsigned long mask,
292 unsigned long type)
Eric Biederman03acab62004-10-14 21:25:53 +0000293{
294 /* Initiliaze the constraints on the current bus */
295 struct resource *resource;
296 resource = 0;
297 if (moving) {
298 unsigned long gran;
299 resource_t step;
300 resource = new_resource(dev, index);
301 resource->size = 0;
302 gran = 0;
303 step = 1;
304 while((moving & step) == 0) {
305 gran += 1;
306 step <<= 1;
307 }
308 resource->gran = gran;
309 resource->align = gran;
310 resource->limit = moving | (step - 1);
311 resource->flags = type | IORESOURCE_PCI_BRIDGE;
312 compute_allocate_resource(&dev->link[0], resource, mask, type);
313 /* If there is nothing behind the resource,
314 * clear it and forget it.
315 */
316 if (resource->size == 0) {
317 resource->base = moving;
318 resource->flags |= IORESOURCE_ASSIGNED;
319 resource->flags &= ~IORESOURCE_STORED;
320 pci_set_resource(dev, resource);
321 resource->flags = 0;
322 }
323 }
324 return;
325}
326
Eric Biederman8ca8d762003-04-22 19:02:15 +0000327static void pci_bridge_read_bases(struct device *dev)
328{
Eric Biederman03acab62004-10-14 21:25:53 +0000329 resource_t moving_base, moving_limit, moving;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000330
Eric Biederman03acab62004-10-14 21:25:53 +0000331
332 /* See if the bridge I/O resources are implemented */
333 moving_base = ((uint32_t)pci_moving_config8(dev, PCI_IO_BASE)) << 8;
334 moving_base |= ((uint32_t)pci_moving_config16(dev, PCI_IO_BASE_UPPER16)) << 16;
335
336 moving_limit = ((uint32_t)pci_moving_config8(dev, PCI_IO_LIMIT)) << 8;
337 moving_limit |= ((uint32_t)pci_moving_config16(dev, PCI_IO_LIMIT_UPPER16)) << 16;
338
339 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000340
341 /* Initialize the io space constraints on the current bus */
Eric Biederman03acab62004-10-14 21:25:53 +0000342 pci_record_bridge_resource(
343 dev, moving, PCI_IO_BASE,
Eric Biedermanb78c1972004-10-14 20:54:17 +0000344 IORESOURCE_IO, IORESOURCE_IO);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000345
Eric Biederman03acab62004-10-14 21:25:53 +0000346
347 /* See if the bridge prefmem resources are implemented */
348 moving_base = ((resource_t)pci_moving_config16(dev, PCI_PREF_MEMORY_BASE)) << 16;
349 moving_base |= ((resource_t)pci_moving_config32(dev, PCI_PREF_BASE_UPPER32)) << 32;
350
351 moving_limit = ((resource_t)pci_moving_config16(dev, PCI_PREF_MEMORY_LIMIT)) << 16;
352 moving_limit |= ((resource_t)pci_moving_config32(dev, PCI_PREF_LIMIT_UPPER32)) << 32;
353
354 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000355 /* Initiliaze the prefetchable memory constraints on the current bus */
Eric Biederman03acab62004-10-14 21:25:53 +0000356 pci_record_bridge_resource(
357 dev, moving, PCI_PREF_MEMORY_BASE,
358 IORESOURCE_MEM | IORESOURCE_PREFETCH,
Eric Biedermanb78c1972004-10-14 20:54:17 +0000359 IORESOURCE_MEM | IORESOURCE_PREFETCH);
Eric Biederman03acab62004-10-14 21:25:53 +0000360
361
362 /* See if the bridge mem resources are implemented */
363 moving_base = ((uint32_t)pci_moving_config16(dev, PCI_MEMORY_BASE)) << 16;
364 moving_limit = ((uint32_t)pci_moving_config16(dev, PCI_MEMORY_LIMIT)) << 16;
365
366 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000367
368 /* Initialize the memory resources on the current bus */
Eric Biederman03acab62004-10-14 21:25:53 +0000369 pci_record_bridge_resource(
370 dev, moving, PCI_MEMORY_BASE,
Eric Biedermanb78c1972004-10-14 20:54:17 +0000371 IORESOURCE_MEM | IORESOURCE_PREFETCH,
372 IORESOURCE_MEM);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000373
Eric Biederman5cd81732004-03-11 15:01:31 +0000374 compact_resources(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000375}
376
Eric Biederman5899fd82003-04-24 06:25:08 +0000377void pci_dev_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000378{
379 uint32_t addr;
Li-Ta Loe5266692004-03-23 21:28:05 +0000380
Eric Biederman8ca8d762003-04-22 19:02:15 +0000381 pci_read_bases(dev, 6);
Li-Ta Loe5266692004-03-23 21:28:05 +0000382
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000383 pci_read_rom_resource(dev, PCI_ROM_ADDRESS);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000384}
385
Eric Biederman5899fd82003-04-24 06:25:08 +0000386void pci_bus_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000387{
388 uint32_t addr;
Li-Ta Loe5266692004-03-23 21:28:05 +0000389
Eric Biederman8ca8d762003-04-22 19:02:15 +0000390 pci_bridge_read_bases(dev);
391 pci_read_bases(dev, 2);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000392
393 pci_read_rom_resource(dev, PCI_ROM_ADDRESS1);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000394}
395
Eric Biederman8ca8d762003-04-22 19:02:15 +0000396static void pci_set_resource(struct device *dev, struct resource *resource)
397{
Eric Biederman03acab62004-10-14 21:25:53 +0000398 resource_t base, end;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000399
Eric Biederman8ca8d762003-04-22 19:02:15 +0000400 /* Make certain the resource has actually been set */
Eric Biederman5cd81732004-03-11 15:01:31 +0000401 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000402 printk_err("ERROR: %s %02x not allocated\n",
Eric Biedermanb78c1972004-10-14 20:54:17 +0000403 dev_path(dev), resource->index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000404 return;
405 }
406
Eric Biederman5cd81732004-03-11 15:01:31 +0000407 /* If I have already stored this resource don't worry about it */
408 if (resource->flags & IORESOURCE_STORED) {
409 return;
410 }
411
Eric Biederman03acab62004-10-14 21:25:53 +0000412 /* If the resources is substractive don't worry about it */
413 if (resource->flags & IORESOURCE_SUBTRACTIVE) {
414 return;
415 }
416
Eric Biederman8ca8d762003-04-22 19:02:15 +0000417 /* Only handle PCI memory and IO resources for now */
418 if (!(resource->flags & (IORESOURCE_MEM |IORESOURCE_IO)))
419 return;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000420
Eric Biederman03acab62004-10-14 21:25:53 +0000421 /* Enable the resources in the command register */
422 if (resource->size) {
423 if (resource->flags & IORESOURCE_MEM) {
424 dev->command |= PCI_COMMAND_MEMORY;
425 }
426 if (resource->flags & IORESOURCE_IO) {
427 dev->command |= PCI_COMMAND_IO;
428 }
429 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
430 dev->command |= PCI_COMMAND_MASTER;
431 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000432 }
433 /* Get the base address */
434 base = resource->base;
Eric Biederman5cd81732004-03-11 15:01:31 +0000435
Eric Biederman03acab62004-10-14 21:25:53 +0000436 /* Get the end */
437 end = resource_end(resource);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000438
Eric Biederman5cd81732004-03-11 15:01:31 +0000439 /* Now store the resource */
440 resource->flags |= IORESOURCE_STORED;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000441 if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) {
Eric Biederman03acab62004-10-14 21:25:53 +0000442 unsigned long base_lo, base_hi;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000443 /*
444 * some chipsets allow us to set/clear the IO bit.
445 * (e.g. VIA 82c686a.) So set it to be safe)
446 */
Eric Biederman03acab62004-10-14 21:25:53 +0000447 base_lo = base & 0xffffffff;
448 base_hi = (base >> 32) & 0xffffffff;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000449 if (resource->flags & IORESOURCE_IO) {
Eric Biederman03acab62004-10-14 21:25:53 +0000450 base_lo |= PCI_BASE_ADDRESS_SPACE_IO;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000451 }
Eric Biederman03acab62004-10-14 21:25:53 +0000452 pci_write_config32(dev, resource->index, base_lo);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000453 if (resource->flags & IORESOURCE_PCI64) {
Eric Biederman03acab62004-10-14 21:25:53 +0000454 pci_write_config32(dev, resource->index + 4, base_hi);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000455 }
Eric Biedermanb78c1972004-10-14 20:54:17 +0000456 }
457 else if (resource->index == PCI_IO_BASE) {
Eric Biederman03acab62004-10-14 21:25:53 +0000458 /* set the IO ranges */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000459 compute_allocate_resource(&dev->link[0], resource,
Eric Biedermanb78c1972004-10-14 20:54:17 +0000460 IORESOURCE_IO, IORESOURCE_IO);
Eric Biederman03acab62004-10-14 21:25:53 +0000461 pci_write_config8(dev, PCI_IO_BASE, base >> 8);
462 pci_write_config16(dev, PCI_IO_BASE_UPPER16, base >> 16);
463 pci_write_config8(dev, PCI_IO_LIMIT, end >> 8);
464 pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, end >> 16);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000465 }
466 else if (resource->index == PCI_MEMORY_BASE) {
Eric Biederman03acab62004-10-14 21:25:53 +0000467 /* set the memory range */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000468 compute_allocate_resource(&dev->link[0], resource,
Eric Biedermanb78c1972004-10-14 20:54:17 +0000469 IORESOURCE_MEM | IORESOURCE_PREFETCH,
470 IORESOURCE_MEM);
Eric Biederman7a5416a2003-06-12 19:23:51 +0000471 pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16);
Eric Biederman03acab62004-10-14 21:25:53 +0000472 pci_write_config16(dev, PCI_MEMORY_LIMIT, end >> 16);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000473 }
474 else if (resource->index == PCI_PREF_MEMORY_BASE) {
Eric Biederman03acab62004-10-14 21:25:53 +0000475 /* set the prefetchable memory range */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000476 compute_allocate_resource(&dev->link[0], resource,
Eric Biedermanb78c1972004-10-14 20:54:17 +0000477 IORESOURCE_MEM | IORESOURCE_PREFETCH,
478 IORESOURCE_MEM | IORESOURCE_PREFETCH);
Eric Biederman03acab62004-10-14 21:25:53 +0000479 pci_write_config16(dev, PCI_PREF_MEMORY_BASE, base >> 16);
480 pci_write_config32(dev, PCI_PREF_BASE_UPPER32, base >> 32);
481 pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, end >> 16);
482 pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, end >> 32);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000483 }
484 else {
Eric Biederman5cd81732004-03-11 15:01:31 +0000485 /* Don't let me think I stored the resource */
486 resource->flags &= ~IORESOURCE_STORED;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000487 printk_err("ERROR: invalid resource->index %x\n",
Eric Biedermanb78c1972004-10-14 20:54:17 +0000488 resource->index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000489 }
Eric Biederman03acab62004-10-14 21:25:53 +0000490 report_resource_stored(dev, resource, "");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000491 return;
492}
493
Eric Biederman5899fd82003-04-24 06:25:08 +0000494void pci_dev_set_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000495{
496 struct resource *resource, *last;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000497 unsigned link;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000498 uint8_t line;
499
500 last = &dev->resource[dev->resources];
Eric Biedermanb78c1972004-10-14 20:54:17 +0000501
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000502 for (resource = &dev->resource[0]; resource < last; resource++) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000503 pci_set_resource(dev, resource);
504 }
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000505 for (link = 0; link < dev->links; link++) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000506 struct bus *bus;
507 bus = &dev->link[link];
508 if (bus->children) {
509 assign_resources(bus);
510 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000511 }
512
513 /* set a default latency timer */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000514 pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000515
516 /* set a default secondary latency timer */
517 if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
Eric Biederman7a5416a2003-06-12 19:23:51 +0000518 pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000519 }
520
521 /* zero the irq settings */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000522 line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000523 if (line) {
Eric Biederman7a5416a2003-06-12 19:23:51 +0000524 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000525 }
526 /* set the cache line size, so far 64 bytes is good for everyone */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000527 pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000528}
529
Eric Biedermane9a271e32003-09-02 03:36:25 +0000530void pci_dev_enable_resources(struct device *dev)
531{
Eric Biedermana9e632c2004-11-18 22:38:08 +0000532 const struct pci_operations *ops;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000533 uint16_t command;
Eric Biederman03acab62004-10-14 21:25:53 +0000534
535 /* Set the subsystem vendor and device id for mainboard devices */
536 ops = ops_pci(dev);
Eric Biedermandbec2d42004-10-21 10:44:08 +0000537 if (dev->on_mainboard && ops && ops->set_subsystem) {
Eric Biederman03acab62004-10-14 21:25:53 +0000538 printk_debug("%s subsystem <- %02x/%02x\n",
539 dev_path(dev),
540 MAINBOARD_PCI_SUBSYSTEM_VENDOR_ID,
541 MAINBOARD_PCI_SUBSYSTEM_DEVICE_ID);
542 ops->set_subsystem(dev,
543 MAINBOARD_PCI_SUBSYSTEM_VENDOR_ID,
544 MAINBOARD_PCI_SUBSYSTEM_DEVICE_ID);
545 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000546 command = pci_read_config16(dev, PCI_COMMAND);
547 command |= dev->command;
Eric Biederman5cd81732004-03-11 15:01:31 +0000548 command |= (PCI_COMMAND_PARITY + PCI_COMMAND_SERR); /* error check */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000549 printk_debug("%s cmd <- %02x\n", dev_path(dev), command);
550 pci_write_config16(dev, PCI_COMMAND, command);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000551}
552
553void pci_bus_enable_resources(struct device *dev)
554{
555 uint16_t ctrl;
556 ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
557 ctrl |= dev->link[0].bridge_ctrl;
Eric Biederman5cd81732004-03-11 15:01:31 +0000558 ctrl |= (PCI_BRIDGE_CTL_PARITY + PCI_BRIDGE_CTL_SERR); /* error check */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000559 printk_debug("%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
560 pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
561
562 pci_dev_enable_resources(dev);
Eric Biedermandbec2d42004-10-21 10:44:08 +0000563
564 enable_childrens_resources(dev);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000565}
566
Eric Biedermana9e632c2004-11-18 22:38:08 +0000567
Eric Biedermandbec2d42004-10-21 10:44:08 +0000568void pci_dev_set_subsystem(device_t dev, unsigned vendor, unsigned device)
Eric Biederman03acab62004-10-14 21:25:53 +0000569{
570 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
571 ((device & 0xffff) << 16) | (vendor & 0xffff));
572}
573
Li-Ta Loe5266692004-03-23 21:28:05 +0000574/** Default device operation for PCI devices */
Eric Biedermana9e632c2004-11-18 22:38:08 +0000575static struct pci_operations pci_dev_ops_pci = {
Eric Biederman03acab62004-10-14 21:25:53 +0000576 .set_subsystem = pci_dev_set_subsystem,
577};
578
Eric Biederman8ca8d762003-04-22 19:02:15 +0000579struct device_operations default_pci_ops_dev = {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000580 .read_resources = pci_dev_read_resources,
581 .set_resources = pci_dev_set_resources,
582 .enable_resources = pci_dev_enable_resources,
Li-Ta Loe5266692004-03-23 21:28:05 +0000583 .init = 0,
584 .scan_bus = 0,
Eric Biederman03acab62004-10-14 21:25:53 +0000585 .enable = 0,
Eric Biedermana9e632c2004-11-18 22:38:08 +0000586 .ops_pci = &pci_dev_ops_pci,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000587};
Li-Ta Loe5266692004-03-23 21:28:05 +0000588
589/** Default device operations for PCI bridges */
Eric Biedermana9e632c2004-11-18 22:38:08 +0000590static struct pci_operations pci_bus_ops_pci = {
Eric Biederman03acab62004-10-14 21:25:53 +0000591 .set_subsystem = 0,
592};
Eric Biederman8ca8d762003-04-22 19:02:15 +0000593struct device_operations default_pci_ops_bus = {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000594 .read_resources = pci_bus_read_resources,
595 .set_resources = pci_dev_set_resources,
596 .enable_resources = pci_bus_enable_resources,
Li-Ta Loe5266692004-03-23 21:28:05 +0000597 .init = 0,
598 .scan_bus = pci_scan_bridge,
Eric Biederman03acab62004-10-14 21:25:53 +0000599 .enable = 0,
Eric Biedermana9e632c2004-11-18 22:38:08 +0000600 .ops_pci = &pci_bus_ops_pci,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000601};
Li-Ta Loe5266692004-03-23 21:28:05 +0000602
603/**
604 * @brief Set up PCI device operation
605 *
606 *
607 * @param dev
608 *
609 * @see pci_drivers
610 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000611static void set_pci_ops(struct device *dev)
612{
613 struct pci_driver *driver;
614 if (dev->ops) {
615 return;
616 }
Li-Ta Loe5266692004-03-23 21:28:05 +0000617
Eric Biederman8ca8d762003-04-22 19:02:15 +0000618 /* Look through the list of setup drivers and find one for
Eric Biedermanb78c1972004-10-14 20:54:17 +0000619 * this pci device
620 */
Li-Ta Lo04930692004-11-25 17:37:19 +0000621 for (driver = &pci_drivers[0]; driver != &epci_drivers[0]; driver++) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000622 if ((driver->vendor == dev->vendor) &&
Li-Ta Lo04930692004-11-25 17:37:19 +0000623 (driver->device == dev->device))
Eric Biedermanb78c1972004-10-14 20:54:17 +0000624 {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000625 dev->ops = driver->ops;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000626 printk_debug("%s [%04x/%04x] %sops\n",
627 dev_path(dev),
628 driver->vendor, driver->device,
629 (driver->ops->scan_bus?"bus ":""));
Eric Biederman5899fd82003-04-24 06:25:08 +0000630 return;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000631 }
632 }
Li-Ta Loe5266692004-03-23 21:28:05 +0000633
Eric Biederman8ca8d762003-04-22 19:02:15 +0000634 /* If I don't have a specific driver use the default operations */
635 switch(dev->hdr_type & 0x7f) { /* header type */
636 case PCI_HEADER_TYPE_NORMAL: /* standard header */
637 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
638 goto bad;
639 dev->ops = &default_pci_ops_dev;
640 break;
641 case PCI_HEADER_TYPE_BRIDGE:
642 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
643 goto bad;
644 dev->ops = &default_pci_ops_bus;
645 break;
646 default:
647 bad:
Li-Ta Lo69c5a902004-04-29 20:08:54 +0000648 if (dev->enabled) {
Eric Biederman83b991a2003-10-11 06:20:25 +0000649 printk_err("%s [%04x/%04x/%06x] has unknown header "
Eric Biedermanb78c1972004-10-14 20:54:17 +0000650 "type %02x, ignoring.\n",
651 dev_path(dev),
652 dev->vendor, dev->device,
653 dev->class >> 8, dev->hdr_type);
Eric Biederman83b991a2003-10-11 06:20:25 +0000654 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000655 }
656 return;
657}
658
659/**
Eric Biederman03acab62004-10-14 21:25:53 +0000660 * @brief See if we have already allocated a device structure for a given devfn.
Li-Ta Loe5266692004-03-23 21:28:05 +0000661 *
662 * Given a linked list of PCI device structures and a devfn number, find the
Li-Ta Lo3a812852004-12-03 22:39:34 +0000663 * device structure correspond to the devfn, if present. This function also
664 * removes the device structure from the linked list.
Li-Ta Loe5266692004-03-23 21:28:05 +0000665 *
666 * @param list the device structure list
Eric Biederman8ca8d762003-04-22 19:02:15 +0000667 * @param devfn a device/function number
Li-Ta Loe5266692004-03-23 21:28:05 +0000668 *
Li-Ta Lo3a812852004-12-03 22:39:34 +0000669 * @return pointer to the device structure found or null of we have not
670 * allocated a device for this devfn yet.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000671 */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000672static struct device *pci_scan_get_dev(struct device **list, unsigned int devfn)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000673{
Eric Biedermanb78c1972004-10-14 20:54:17 +0000674 struct device *dev;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000675 dev = 0;
676 for(; *list; list = &(*list)->sibling) {
Eric Biedermanad1b35a2003-10-14 02:36:51 +0000677 if ((*list)->path.type != DEVICE_PATH_PCI) {
Li-Ta Loe5266692004-03-23 21:28:05 +0000678 printk_err("child %s not a pci device\n",
Li-Ta Lo3a812852004-12-03 22:39:34 +0000679 dev_path(*list));
Eric Biedermanad1b35a2003-10-14 02:36:51 +0000680 continue;
681 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000682 if ((*list)->path.u.pci.devfn == devfn) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000683 /* Unlink from the list */
684 dev = *list;
685 *list = (*list)->sibling;
686 dev->sibling = 0;
687 break;
688 }
689 }
Li-Ta Lo3a812852004-12-03 22:39:34 +0000690 /* Just like alloc_dev add the device to the list of device on the bus.
691 * When the list of devices was formed we removed all of the parents
692 * children, and now we are interleaving static and dynamic devices in
693 * order on the bus.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000694 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000695 if (dev) {
696 device_t child;
697 /* Find the last child of our parent */
Li-Ta Lo3a812852004-12-03 22:39:34 +0000698 for (child = dev->bus->children; child && child->sibling; ) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000699 child = child->sibling;
700 }
701 /* Place the device on the list of children of it's parent. */
702 if (child) {
703 child->sibling = dev;
704 } else {
705 dev->bus->children = dev;
706 }
707 }
708
Eric Biederman8ca8d762003-04-22 19:02:15 +0000709 return dev;
710}
711
Eric Biedermanb78c1972004-10-14 20:54:17 +0000712/**
713 * @brief Scan a PCI bus.
Li-Ta Loe5266692004-03-23 21:28:05 +0000714 *
715 * Determine the existence of devices and bridges on a PCI bus. If there are
716 * bridges on the bus, recursively scan the buses behind the bridges.
717 *
Eric Biedermane9a271e32003-09-02 03:36:25 +0000718 * @param bus pointer to the bus structure
719 * @param min_devfn minimum devfn to look at in the scan usually 0x00
720 * @param max_devfn maximum devfn to look at in the scan usually 0xff
Eric Biederman8ca8d762003-04-22 19:02:15 +0000721 * @param max current bus number
Li-Ta Loe5266692004-03-23 21:28:05 +0000722 *
Eric Biederman8ca8d762003-04-22 19:02:15 +0000723 * @return The maximum bus number found, after scanning all subordinate busses
724 */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000725unsigned int pci_scan_bus(struct bus *bus,
726 unsigned min_devfn, unsigned max_devfn,
727 unsigned int max)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000728{
729 unsigned int devfn;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000730 device_t dev;
731 device_t old_devices;
732 device_t child;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000733
734 printk_debug("PCI: pci_scan_bus for bus %d\n", bus->secondary);
735
736 old_devices = bus->children;
737 bus->children = 0;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000738
739 post_code(0x24);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000740
Li-Ta Lo9782f752004-05-05 21:15:42 +0000741 /* probe all devices/functions on this bus with some optimization for
Eric Biedermanb78c1972004-10-14 20:54:17 +0000742 * non-existence and single funcion devices
743 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000744 for (devfn = min_devfn; devfn <= max_devfn; devfn++) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000745 uint32_t id, class;
Eric Biederman30e143a2003-09-01 23:45:32 +0000746 uint8_t hdr_type;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000747
Eric Biederman03acab62004-10-14 21:25:53 +0000748 /* First thing setup the device structure */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000749 dev = pci_scan_get_dev(&old_devices, devfn);
Eric Biederman03acab62004-10-14 21:25:53 +0000750
751 /* Detect if a device is present */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000752 if (!dev) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000753 struct device dummy;
754 dummy.bus = bus;
755 dummy.path.type = DEVICE_PATH_PCI;
756 dummy.path.u.pci.devfn = devfn;
757 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
758 /* some broken boards return 0 if a slot is empty: */
Li-Ta Lo04930692004-11-25 17:37:19 +0000759 if ((id == 0xffffffff) || (id == 0x00000000) ||
760 (id == 0x0000ffff) || (id == 0xffff0000))
Eric Biedermanb78c1972004-10-14 20:54:17 +0000761 {
762 printk_spew("PCI: devfn 0x%x, bad id 0x%x\n", devfn, id);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000763 if (PCI_FUNC(devfn) == 0x00) {
Eric Biedermanb78c1972004-10-14 20:54:17 +0000764 /* if this is a function 0 device and
765 * it is not present,
766 * skip to next device
767 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000768 devfn += 0x07;
769 }
Eric Biedermanb78c1972004-10-14 20:54:17 +0000770 /* This function in a multi function device is
771 * not present, skip to the next function.
772 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000773 continue;
774 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000775 dev = alloc_dev(bus, &dummy.path);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000776 }
777 else {
Eric Biederman03acab62004-10-14 21:25:53 +0000778 /* Enable/disable the device. Once we have
779 * found the device specific operations this
780 * operations we will disable the device with
781 * those as well.
782 *
Eric Biedermanb78c1972004-10-14 20:54:17 +0000783 * This is geared toward devices that have subfunctions
784 * that do not show up by default.
785 *
786 * If a device is a stuff option on the motherboard
787 * it may be absent and enable_dev must cope.
788 *
789 */
Eric Biederman7003ba42004-10-16 06:20:29 +0000790 if (dev->chip_ops && dev->chip_ops->enable_dev)
Eric Biedermanb78c1972004-10-14 20:54:17 +0000791 {
Eric Biederman7003ba42004-10-16 06:20:29 +0000792 dev->chip_ops->enable_dev(dev);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000793 }
794 /* Now read the vendor and device id */
795 id = pci_read_config32(dev, PCI_VENDOR_ID);
Eric Biederman03acab62004-10-14 21:25:53 +0000796
797 /* If the device does not have a pci id disable it.
798 * Possibly this is because we have already disabled
799 * the device. But this also handles optional devices
800 * that may not always show up.
801 */
802 if (id == 0xffffffff || id == 0x00000000 ||
Li-Ta Lo04930692004-11-25 17:37:19 +0000803 id == 0x0000ffff || id == 0xffff0000)
Eric Biederman03acab62004-10-14 21:25:53 +0000804 {
805 if (dev->enabled) {
806 printk_info("Disabling static device: %s\n",
807 dev_path(dev));
808 dev->enabled = 0;
809 }
810 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000811 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000812 /* Read the rest of the pci configuration information */
813 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
814 class = pci_read_config32(dev, PCI_CLASS_REVISION);
Li-Ta Lo9782f752004-05-05 21:15:42 +0000815
Eric Biedermane9a271e32003-09-02 03:36:25 +0000816 /* Store the interesting information in the device structure */
817 dev->vendor = id & 0xffff;
818 dev->device = (id >> 16) & 0xffff;
819 dev->hdr_type = hdr_type;
820 /* class code, the upper 3 bytes of PCI_CLASS_REVISION */
821 dev->class = class >> 8;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000822
Eric Biederman03acab62004-10-14 21:25:53 +0000823 /* Architectural/System devices always need to
824 * be bus masters.
825 */
826 if ((dev->class >> 16) == PCI_BASE_CLASS_SYSTEM) {
827 dev->command |= PCI_COMMAND_MASTER;
828 }
829
Eric Biederman8ca8d762003-04-22 19:02:15 +0000830 /* Look at the vendor and device id, or at least the
Li-Ta Loe5266692004-03-23 21:28:05 +0000831 * header type and class and figure out which set of
832 * configuration methods to use. Unless we already
833 * have some pci ops.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000834 */
Eric Biederman83b991a2003-10-11 06:20:25 +0000835 set_pci_ops(dev);
836 /* Error if we don't have some pci operations for it */
Eric Biederman5cd81732004-03-11 15:01:31 +0000837 if (!dev->ops) {
Eric Biederman83b991a2003-10-11 06:20:25 +0000838 printk_err("%s No device operations\n",
Eric Biedermanb78c1972004-10-14 20:54:17 +0000839 dev_path(dev));
Eric Biederman83b991a2003-10-11 06:20:25 +0000840 continue;
841 }
842
843 /* Now run the magic enable/disable sequence for the device */
844 if (dev->ops && dev->ops->enable) {
845 dev->ops->enable(dev);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000846 }
Eric Biederman4086d162003-07-17 03:26:03 +0000847
Eric Biedermane9a271e32003-09-02 03:36:25 +0000848 printk_debug("%s [%04x/%04x] %s\n",
Eric Biederman03acab62004-10-14 21:25:53 +0000849 dev_path(dev),
850 dev->vendor, dev->device,
851 dev->enabled?"enabled": "disabled");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000852
Eric Biederman8ca8d762003-04-22 19:02:15 +0000853 if (PCI_FUNC(devfn) == 0x00 && (hdr_type & 0x80) != 0x80) {
Eric Biedermanb78c1972004-10-14 20:54:17 +0000854 /* if this is not a multi function device,
855 * don't waste time probing another function.
856 * Skip to next device.
857 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000858 devfn += 0x07;
859 }
860 }
861 post_code(0x25);
862
Eric Biedermanb78c1972004-10-14 20:54:17 +0000863 /* For all children that implement scan_bus (i.e. bridges)
864 * scan the bus behind that child.
865 */
Li-Ta Lo04930692004-11-25 17:37:19 +0000866 for (child = bus->children; child; child = child->sibling) {
Eric Biedermanb78c1972004-10-14 20:54:17 +0000867 if (!child->enabled ||
Li-Ta Lo04930692004-11-25 17:37:19 +0000868 !child->ops ||
869 !child->ops->scan_bus)
Eric Biedermanb78c1972004-10-14 20:54:17 +0000870 {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000871 continue;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000872 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000873 max = child->ops->scan_bus(child, max);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000874 }
Li-Ta Loe5266692004-03-23 21:28:05 +0000875
Eric Biederman8ca8d762003-04-22 19:02:15 +0000876 /*
877 * We've scanned the bus and so we know all about what's on
878 * the other side of any bridges that may be on this bus plus
879 * any devices.
880 *
881 * Return how far we've got finding sub-buses.
882 */
883 printk_debug("PCI: pci_scan_bus returning with max=%02x\n", max);
884 post_code(0x55);
885 return max;
886}
887
Li-Ta Loe5266692004-03-23 21:28:05 +0000888/**
889 * @brief Scan a PCI bridge and the buses behind the bridge.
890 *
891 * Determine the existence of buses behind the bridge. Set up the bridge
892 * according to the result of the scan.
893 *
894 * This function is the default scan_bus() method for PCI bridge devices.
895 *
896 * @param dev pointer to the bridge device
897 * @param max the highest bus number assgined up to now
898 *
Eric Biederman8ca8d762003-04-22 19:02:15 +0000899 * @return The maximum bus number found, after scanning all subordinate busses
900 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000901unsigned int pci_scan_bridge(struct device *dev, unsigned int max)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000902{
Eric Biedermane9a271e32003-09-02 03:36:25 +0000903 struct bus *bus;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000904 uint32_t buses;
905 uint16_t cr;
Eric Biederman83b991a2003-10-11 06:20:25 +0000906
Li-Ta Lo3a812852004-12-03 22:39:34 +0000907 printk_spew("%s for %s\n", __func__, dev_path(dev));
908
Eric Biedermane9a271e32003-09-02 03:36:25 +0000909 bus = &dev->link[0];
Eric Biedermana9e632c2004-11-18 22:38:08 +0000910 bus->dev = dev;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000911 dev->links = 1;
912
Eric Biederman8ca8d762003-04-22 19:02:15 +0000913 /* Set up the primary, secondary and subordinate bus numbers. We have
914 * no idea how many buses are behind this bridge yet, so we set the
Eric Biedermanb78c1972004-10-14 20:54:17 +0000915 * subordinate bus number to 0xff for the moment.
916 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000917 bus->secondary = ++max;
918 bus->subordinate = 0xff;
Li-Ta Loe5266692004-03-23 21:28:05 +0000919
Eric Biederman8ca8d762003-04-22 19:02:15 +0000920 /* Clear all status bits and turn off memory, I/O and master enables. */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000921 cr = pci_read_config16(dev, PCI_COMMAND);
922 pci_write_config16(dev, PCI_COMMAND, 0x0000);
923 pci_write_config16(dev, PCI_STATUS, 0xffff);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000924
Eric Biedermanb78c1972004-10-14 20:54:17 +0000925 /*
926 * Read the existing primary/secondary/subordinate bus
927 * number configuration.
928 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000929 buses = pci_read_config32(dev, PCI_PRIMARY_BUS);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000930
931 /* Configure the bus numbers for this bridge: the configuration
932 * transactions will not be propagated by the bridge if it is not
Eric Biedermanb78c1972004-10-14 20:54:17 +0000933 * correctly configured.
934 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000935 buses &= 0xff000000;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000936 buses |= (((unsigned int) (dev->bus->secondary) << 0) |
Li-Ta Lo3a812852004-12-03 22:39:34 +0000937 ((unsigned int) (bus->secondary) << 8) |
938 ((unsigned int) (bus->subordinate) << 16));
Eric Biedermane9a271e32003-09-02 03:36:25 +0000939 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
Li-Ta Lo3a812852004-12-03 22:39:34 +0000940
Eric Biedermanb78c1972004-10-14 20:54:17 +0000941 /* Now we can scan all subordinate buses
942 * i.e. the bus behind the bridge.
943 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000944 max = pci_scan_bus(bus, 0x00, 0xff, max);
Li-Ta Lo3a812852004-12-03 22:39:34 +0000945
Eric Biederman8ca8d762003-04-22 19:02:15 +0000946 /* We know the number of buses behind this bridge. Set the subordinate
Eric Biedermanb78c1972004-10-14 20:54:17 +0000947 * bus number to its real value.
948 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000949 bus->subordinate = max;
950 buses = (buses & 0xff00ffff) |
951 ((unsigned int) (bus->subordinate) << 16);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000952 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
953 pci_write_config16(dev, PCI_COMMAND, cr);
Li-Ta Lo3a812852004-12-03 22:39:34 +0000954
Eric Biedermanb78c1972004-10-14 20:54:17 +0000955 printk_spew("%s returns max %d\n", __func__, max);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000956 return max;
957}
Li-Ta Loe5266692004-03-23 21:28:05 +0000958
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +0000959/*
960 Tell the EISA int controller this int must be level triggered
961 THIS IS A KLUDGE -- sorry, this needs to get cleaned up.
962*/
963static void pci_level_irq(unsigned char intNum)
964{
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +0000965 unsigned short intBits = inb(0x4d0) | (((unsigned) inb(0x4d1)) << 8);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +0000966
Eric Biedermanb78c1972004-10-14 20:54:17 +0000967 printk_spew("%s: current ints are 0x%x\n", __func__, intBits);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +0000968 intBits |= (1 << intNum);
969
Eric Biedermanb78c1972004-10-14 20:54:17 +0000970 printk_spew("%s: try to set ints 0x%x\n", __func__, intBits);
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +0000971
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +0000972 // Write new values
973 outb((unsigned char) intBits, 0x4d0);
974 outb((unsigned char) (intBits >> 8), 0x4d1);
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +0000975
Ronald G. Minnichb56ef072003-10-15 20:05:11 +0000976 /* this seems like an error but is not ... */
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000977#if 1
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +0000978 if (inb(0x4d0) != (intBits & 0xf)) {
979 printk_err("%s: lower order bits are wrong: want 0x%x, got 0x%x\n",
Eric Biedermanb78c1972004-10-14 20:54:17 +0000980 __func__, intBits &0xf, inb(0x4d0));
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +0000981 }
982 if (inb(0x4d1) != ((intBits >> 8) & 0xf)) {
983 printk_err("%s: lower order bits are wrong: want 0x%x, got 0x%x\n",
Eric Biedermanb78c1972004-10-14 20:54:17 +0000984 __func__, (intBits>>8) &0xf, inb(0x4d1));
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +0000985 }
Ronald G. Minnichb56ef072003-10-15 20:05:11 +0000986#endif
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +0000987}
988
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +0000989/*
990 This function assigns IRQs for all functions contained within
991 the indicated device address. If the device does not exist or does
992 not require interrupts then this function has no effect.
993
994 This function should be called for each PCI slot in your system.
995
996 pIntAtoD is an array of IRQ #s that are assigned to PINTA through PINTD of
997 this slot.
998 The particular irq #s that are passed in depend on the routing inside
999 your southbridge and on your motherboard.
1000
1001 -kevinh@ispiri.com
1002*/
1003void pci_assign_irqs(unsigned bus, unsigned slot,
1004 const unsigned char pIntAtoD[4])
1005{
1006 unsigned functNum;
1007 device_t pdev;
1008 unsigned char line;
1009 unsigned char irq;
1010 unsigned char readback;
1011
1012 /* Each slot may contain up to eight functions */
1013 for (functNum = 0; functNum < 8; functNum++) {
1014 pdev = dev_find_slot(bus, (slot << 3) + functNum);
1015
1016 if (pdev) {
1017 line = pci_read_config8(pdev, PCI_INTERRUPT_PIN);
1018
1019 // PCI spec says all other values are reserved
1020 if ((line >= 1) && (line <= 4)) {
1021 irq = pIntAtoD[line - 1];
1022
1023 printk_debug("Assigning IRQ %d to %d:%x.%d\n", \
1024 irq, bus, slot, functNum);
1025
1026 pci_write_config8(pdev, PCI_INTERRUPT_LINE,\
1027 pIntAtoD[line - 1]);
1028
1029 readback = pci_read_config8(pdev, PCI_INTERRUPT_LINE);
1030 printk_debug(" Readback = %d\n", readback);
1031
1032 // Change to level triggered
1033 pci_level_irq(pIntAtoD[line - 1]);
1034 }
1035 }
1036 }
1037}