blob: d7ba57b26204b0b05168c2fdb71ef6fbae89324b [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}
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +000056
Eric Biederman03acab62004-10-14 21:25:53 +000057static uint32_t pci_moving_config32(struct device *dev, unsigned reg)
58{
59 uint32_t value, ones, zeroes;
60 value = pci_read_config32(dev, reg);
61
62 pci_write_config32(dev, reg, 0xffffffff);
63 ones = pci_read_config32(dev, reg);
64
65 pci_write_config32(dev, reg, 0x00000000);
66 zeroes = pci_read_config32(dev, reg);
67
68 pci_write_config32(dev, reg, value);
69
70 return ones ^ zeroes;
71}
72
73unsigned pci_find_capability(device_t dev, unsigned cap)
74{
75 unsigned pos;
76 pos = 0;
77 switch(dev->hdr_type & 0x7f) {
78 case PCI_HEADER_TYPE_NORMAL:
79 case PCI_HEADER_TYPE_BRIDGE:
80 pos = PCI_CAPABILITY_LIST;
81 break;
82 }
83 if (pos > PCI_CAP_LIST_NEXT) {
84 pos = pci_read_config8(dev, pos);
85 }
Li-Ta Lo9a5b4962004-12-23 21:48:01 +000086 while (pos != 0) { /* loop through the linked list */
Eric Biederman03acab62004-10-14 21:25:53 +000087 int this_cap;
88 this_cap = pci_read_config8(dev, pos + PCI_CAP_LIST_ID);
89 if (this_cap == cap) {
90 return pos;
91 }
92 }
93 return 0;
94}
95
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;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000137 while (!(moving & resource->size)) {
Eric Biederman03acab62004-10-14 21:25:53 +0000138 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;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000161 } else if (attr & PCI_BASE_ADDRESS_SPACE_IO) {
Eric Biederman03acab62004-10-14 21:25:53 +0000162 /* An I/O mapped base address */
163 attr &= PCI_BASE_ADDRESS_IO_ATTR_MASK;
Eric Biederman5cd81732004-03-11 15:01:31 +0000164 resource->flags |= IORESOURCE_IO;
Eric Biederman03acab62004-10-14 21:25:53 +0000165 /* I don't want to deal with 32bit I/O resources */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000166 resource->limit = 0xffff;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000167 } else {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000168 /* A Memory mapped base address */
Eric Biederman03acab62004-10-14 21:25:53 +0000169 attr &= PCI_BASE_ADDRESS_MEM_ATTR_MASK;
Eric Biederman5cd81732004-03-11 15:01:31 +0000170 resource->flags |= IORESOURCE_MEM;
Eric Biederman03acab62004-10-14 21:25:53 +0000171 if (attr & PCI_BASE_ADDRESS_MEM_PREFETCH) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000172 resource->flags |= IORESOURCE_PREFETCH;
173 }
Eric Biederman03acab62004-10-14 21:25:53 +0000174 attr &= PCI_BASE_ADDRESS_MEM_LIMIT_MASK;
175 if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_32) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000176 /* 32bit limit */
177 resource->limit = 0xffffffffUL;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000178 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_1M) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000179 /* 1MB limit */
180 resource->limit = 0x000fffffUL;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000181 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_64) {
Eric Biederman03acab62004-10-14 21:25:53 +0000182 /* 64bit limit */
183 resource->limit = 0xffffffffffffffffULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000184 resource->flags |= IORESOURCE_PCI64;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000185 } else {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000186 /* Invalid value */
187 resource->flags = 0;
188 }
189 }
Eric Biederman03acab62004-10-14 21:25:53 +0000190 /* Don't let the limit exceed which bits can move */
191 if (resource->limit > limit) {
192 resource->limit = limit;
193 }
194#if 0
195 if (resource->flags) {
196 printk_debug("%s %02x ->",
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000197 dev_path(dev), resource->index);
Eric Biederman03acab62004-10-14 21:25:53 +0000198 printk_debug(" value: 0x%08Lx zeroes: 0x%08Lx ones: 0x%08Lx attr: %08lx\n",
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000199 value, zeroes, ones, attr);
Eric Biederman03acab62004-10-14 21:25:53 +0000200 printk_debug(
201 "%s %02x -> size: 0x%08Lx max: 0x%08Lx %s%s\n ",
202 dev_path(dev),
203 resource->index,
204 resource->size, resource->limit,
205 (resource->flags == 0) ? "unused":
206 (resource->flags & IORESOURCE_IO)? "io":
207 (resource->flags & IORESOURCE_PREFETCH)? "prefmem": "mem",
208 (resource->flags & IORESOURCE_PCI64)?"64":"");
209 }
210#endif
211
Eric Biederman5cd81732004-03-11 15:01:31 +0000212 return resource;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000213}
214
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000215static void pci_get_rom_resource(struct device *dev, unsigned long index)
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000216{
217 struct resource *resource;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000218 unsigned long value;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000219 resource_t moving, limit;
220
221 /* Initialize the resources to nothing */
222 resource = new_resource(dev, index);
223
224 /* Get the initial value */
225 value = pci_read_config32(dev, index);
226
227 /* See which bits move */
228 moving = pci_moving_config32(dev, index);
229 /* clear the Enable bit */
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000230 moving = moving & ~PCI_ROM_ADDRESS_ENABLE;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000231
232 /* Find the resource constraints.
233 *
234 * Start by finding the bits that move. From there:
235 * - Size is the least significant bit of the bits that move.
236 * - Limit is all of the bits that move plus all of the lower bits.
237 * See PCI Spec 6.2.5.1 ...
238 */
239 limit = 0;
240
241 if (moving) {
242 resource->size = 1;
243 resource->align = resource->gran = 0;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000244 while (!(moving & resource->size)) {
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000245 resource->size <<= 1;
246 resource->align += 1;
247 resource->gran += 1;
248 }
249 resource->limit = limit = moving | (resource->size - 1);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000250 }
251
252 if (moving == 0) {
253 if (value != 0) {
254 printk_debug("%s register %02x(%08x), read-only ignoring it\n",
255 dev_path(dev), index, value);
256 }
257 resource->flags = 0;
258 } else {
259 resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY;
260 }
Yinghai Luc7870ac2005-01-13 19:14:52 +0000261
262 /* for on board device with embedded ROM image, the ROM image is at
263 * fixed address specified in the Config.lb, the dev->rom_address is
264 * inited by driver_pci_onboard_ops::enable_dev() */
265 if ((dev->on_mainboard) && (dev->rom_address == 0)) {
266 resource->base = dev->rom_address;
267 resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY |
268 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
269 }
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000270}
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000271
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000272/** Read the base address registers for a given device.
273 * @param dev Pointer to the dev structure
274 * @param howmany How many registers to read (6 for device, 2 for bridge)
275 */
276static void pci_read_bases(struct device *dev, unsigned int howmany, unsigned long rom)
277{
278 unsigned long index;
279
280 for (index = PCI_BASE_ADDRESS_0; (index < PCI_BASE_ADDRESS_0 + (howmany << 2)); ) {
281 struct resource *resource;
282 resource = pci_get_resource(dev, index);
283 index += (resource->flags & IORESOURCE_PCI64)?8:4;
284 }
Li-Ta Lobc5399a2005-01-13 05:44:16 +0000285 if (rom)
286 pci_get_rom_resource(dev, rom);
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000287
288 compact_resources(dev);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000289}
290
Eric Biederman03acab62004-10-14 21:25:53 +0000291static void pci_set_resource(struct device *dev, struct resource *resource);
292
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000293static void pci_record_bridge_resource( struct device *dev, resource_t moving,
294 unsigned index, unsigned long mask,
295 unsigned long type)
Eric Biederman03acab62004-10-14 21:25:53 +0000296{
297 /* Initiliaze the constraints on the current bus */
298 struct resource *resource;
299 resource = 0;
300 if (moving) {
301 unsigned long gran;
302 resource_t step;
303 resource = new_resource(dev, index);
304 resource->size = 0;
305 gran = 0;
306 step = 1;
307 while((moving & step) == 0) {
308 gran += 1;
309 step <<= 1;
310 }
311 resource->gran = gran;
312 resource->align = gran;
313 resource->limit = moving | (step - 1);
314 resource->flags = type | IORESOURCE_PCI_BRIDGE;
315 compute_allocate_resource(&dev->link[0], resource, mask, type);
316 /* If there is nothing behind the resource,
317 * clear it and forget it.
318 */
319 if (resource->size == 0) {
320 resource->base = moving;
321 resource->flags |= IORESOURCE_ASSIGNED;
322 resource->flags &= ~IORESOURCE_STORED;
323 pci_set_resource(dev, resource);
324 resource->flags = 0;
325 }
326 }
327 return;
328}
329
Eric Biederman8ca8d762003-04-22 19:02:15 +0000330static void pci_bridge_read_bases(struct device *dev)
331{
Eric Biederman03acab62004-10-14 21:25:53 +0000332 resource_t moving_base, moving_limit, moving;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000333
Eric Biederman03acab62004-10-14 21:25:53 +0000334 /* See if the bridge I/O resources are implemented */
335 moving_base = ((uint32_t)pci_moving_config8(dev, PCI_IO_BASE)) << 8;
336 moving_base |= ((uint32_t)pci_moving_config16(dev, PCI_IO_BASE_UPPER16)) << 16;
337
338 moving_limit = ((uint32_t)pci_moving_config8(dev, PCI_IO_LIMIT)) << 8;
339 moving_limit |= ((uint32_t)pci_moving_config16(dev, PCI_IO_LIMIT_UPPER16)) << 16;
340
341 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000342
343 /* Initialize the io space constraints on the current bus */
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000344 pci_record_bridge_resource(dev, moving, PCI_IO_BASE,
345 IORESOURCE_IO, IORESOURCE_IO);
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 */
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000356 pci_record_bridge_resource(dev, moving, PCI_PREF_MEMORY_BASE,
357 IORESOURCE_MEM | IORESOURCE_PREFETCH,
358 IORESOURCE_MEM | IORESOURCE_PREFETCH);
Eric Biederman03acab62004-10-14 21:25:53 +0000359
360 /* See if the bridge mem resources are implemented */
361 moving_base = ((uint32_t)pci_moving_config16(dev, PCI_MEMORY_BASE)) << 16;
362 moving_limit = ((uint32_t)pci_moving_config16(dev, PCI_MEMORY_LIMIT)) << 16;
363
364 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000365
366 /* Initialize the memory resources on the current bus */
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000367 pci_record_bridge_resource(dev, moving, PCI_MEMORY_BASE,
368 IORESOURCE_MEM | IORESOURCE_PREFETCH,
369 IORESOURCE_MEM);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000370
Eric Biederman5cd81732004-03-11 15:01:31 +0000371 compact_resources(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000372}
373
Eric Biederman5899fd82003-04-24 06:25:08 +0000374void pci_dev_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000375{
376 uint32_t addr;
Li-Ta Loe5266692004-03-23 21:28:05 +0000377
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000378 pci_read_bases(dev, 6, PCI_ROM_ADDRESS);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000379}
380
Eric Biederman5899fd82003-04-24 06:25:08 +0000381void pci_bus_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000382{
383 uint32_t addr;
Li-Ta Loe5266692004-03-23 21:28:05 +0000384
Eric Biederman8ca8d762003-04-22 19:02:15 +0000385 pci_bridge_read_bases(dev);
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000386 pci_read_bases(dev, 2, PCI_ROM_ADDRESS1);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000387}
388
Eric Biederman8ca8d762003-04-22 19:02:15 +0000389static void pci_set_resource(struct device *dev, struct resource *resource)
390{
Eric Biederman03acab62004-10-14 21:25:53 +0000391 resource_t base, end;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000392
Eric Biederman8ca8d762003-04-22 19:02:15 +0000393 /* Make certain the resource has actually been set */
Eric Biederman5cd81732004-03-11 15:01:31 +0000394 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000395 printk_err("ERROR: %s %02x not allocated\n",
Eric Biedermanb78c1972004-10-14 20:54:17 +0000396 dev_path(dev), resource->index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000397 return;
398 }
399
Eric Biederman5cd81732004-03-11 15:01:31 +0000400 /* If I have already stored this resource don't worry about it */
401 if (resource->flags & IORESOURCE_STORED) {
402 return;
403 }
404
Eric Biederman03acab62004-10-14 21:25:53 +0000405 /* If the resources is substractive don't worry about it */
406 if (resource->flags & IORESOURCE_SUBTRACTIVE) {
407 return;
408 }
409
Eric Biederman8ca8d762003-04-22 19:02:15 +0000410 /* Only handle PCI memory and IO resources for now */
411 if (!(resource->flags & (IORESOURCE_MEM |IORESOURCE_IO)))
412 return;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000413
Eric Biederman03acab62004-10-14 21:25:53 +0000414 /* Enable the resources in the command register */
415 if (resource->size) {
416 if (resource->flags & IORESOURCE_MEM) {
417 dev->command |= PCI_COMMAND_MEMORY;
418 }
419 if (resource->flags & IORESOURCE_IO) {
420 dev->command |= PCI_COMMAND_IO;
421 }
422 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
423 dev->command |= PCI_COMMAND_MASTER;
424 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000425 }
426 /* Get the base address */
427 base = resource->base;
Eric Biederman5cd81732004-03-11 15:01:31 +0000428
Eric Biederman03acab62004-10-14 21:25:53 +0000429 /* Get the end */
430 end = resource_end(resource);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000431
Eric Biederman5cd81732004-03-11 15:01:31 +0000432 /* Now store the resource */
433 resource->flags |= IORESOURCE_STORED;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000434 if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) {
Eric Biederman03acab62004-10-14 21:25:53 +0000435 unsigned long base_lo, base_hi;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000436 /*
437 * some chipsets allow us to set/clear the IO bit.
438 * (e.g. VIA 82c686a.) So set it to be safe)
439 */
Eric Biederman03acab62004-10-14 21:25:53 +0000440 base_lo = base & 0xffffffff;
441 base_hi = (base >> 32) & 0xffffffff;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000442 if (resource->flags & IORESOURCE_IO) {
Eric Biederman03acab62004-10-14 21:25:53 +0000443 base_lo |= PCI_BASE_ADDRESS_SPACE_IO;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000444 }
Eric Biederman03acab62004-10-14 21:25:53 +0000445 pci_write_config32(dev, resource->index, base_lo);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000446 if (resource->flags & IORESOURCE_PCI64) {
Eric Biederman03acab62004-10-14 21:25:53 +0000447 pci_write_config32(dev, resource->index + 4, base_hi);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000448 }
Eric Biedermanb78c1972004-10-14 20:54:17 +0000449 }
450 else if (resource->index == PCI_IO_BASE) {
Eric Biederman03acab62004-10-14 21:25:53 +0000451 /* set the IO ranges */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000452 compute_allocate_resource(&dev->link[0], resource,
Eric Biedermanb78c1972004-10-14 20:54:17 +0000453 IORESOURCE_IO, IORESOURCE_IO);
Eric Biederman03acab62004-10-14 21:25:53 +0000454 pci_write_config8(dev, PCI_IO_BASE, base >> 8);
455 pci_write_config16(dev, PCI_IO_BASE_UPPER16, base >> 16);
456 pci_write_config8(dev, PCI_IO_LIMIT, end >> 8);
457 pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, end >> 16);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000458 }
459 else if (resource->index == PCI_MEMORY_BASE) {
Eric Biederman03acab62004-10-14 21:25:53 +0000460 /* set the memory range */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000461 compute_allocate_resource(&dev->link[0], resource,
Eric Biedermanb78c1972004-10-14 20:54:17 +0000462 IORESOURCE_MEM | IORESOURCE_PREFETCH,
463 IORESOURCE_MEM);
Eric Biederman7a5416a2003-06-12 19:23:51 +0000464 pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16);
Eric Biederman03acab62004-10-14 21:25:53 +0000465 pci_write_config16(dev, PCI_MEMORY_LIMIT, end >> 16);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000466 }
467 else if (resource->index == PCI_PREF_MEMORY_BASE) {
Eric Biederman03acab62004-10-14 21:25:53 +0000468 /* set the prefetchable memory range */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000469 compute_allocate_resource(&dev->link[0], resource,
Eric Biedermanb78c1972004-10-14 20:54:17 +0000470 IORESOURCE_MEM | IORESOURCE_PREFETCH,
471 IORESOURCE_MEM | IORESOURCE_PREFETCH);
Eric Biederman03acab62004-10-14 21:25:53 +0000472 pci_write_config16(dev, PCI_PREF_MEMORY_BASE, base >> 16);
473 pci_write_config32(dev, PCI_PREF_BASE_UPPER32, base >> 32);
474 pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, end >> 16);
475 pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, end >> 32);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000476 }
477 else {
Eric Biederman5cd81732004-03-11 15:01:31 +0000478 /* Don't let me think I stored the resource */
479 resource->flags &= ~IORESOURCE_STORED;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000480 printk_err("ERROR: invalid resource->index %x\n",
Eric Biedermanb78c1972004-10-14 20:54:17 +0000481 resource->index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000482 }
Eric Biederman03acab62004-10-14 21:25:53 +0000483 report_resource_stored(dev, resource, "");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000484 return;
485}
486
Eric Biederman5899fd82003-04-24 06:25:08 +0000487void pci_dev_set_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000488{
489 struct resource *resource, *last;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000490 unsigned link;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000491 uint8_t line;
492
493 last = &dev->resource[dev->resources];
Eric Biedermanb78c1972004-10-14 20:54:17 +0000494
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000495 for (resource = &dev->resource[0]; resource < last; resource++) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000496 pci_set_resource(dev, resource);
497 }
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000498 for (link = 0; link < dev->links; link++) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000499 struct bus *bus;
500 bus = &dev->link[link];
501 if (bus->children) {
502 assign_resources(bus);
503 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000504 }
505
506 /* set a default latency timer */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000507 pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000508
509 /* set a default secondary latency timer */
510 if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
Eric Biederman7a5416a2003-06-12 19:23:51 +0000511 pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000512 }
513
514 /* zero the irq settings */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000515 line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000516 if (line) {
Eric Biederman7a5416a2003-06-12 19:23:51 +0000517 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000518 }
519 /* set the cache line size, so far 64 bytes is good for everyone */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000520 pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000521}
522
Eric Biedermane9a271e32003-09-02 03:36:25 +0000523void pci_dev_enable_resources(struct device *dev)
524{
Eric Biedermana9e632c2004-11-18 22:38:08 +0000525 const struct pci_operations *ops;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000526 uint16_t command;
Eric Biederman03acab62004-10-14 21:25:53 +0000527
528 /* Set the subsystem vendor and device id for mainboard devices */
529 ops = ops_pci(dev);
Eric Biedermandbec2d42004-10-21 10:44:08 +0000530 if (dev->on_mainboard && ops && ops->set_subsystem) {
Eric Biederman03acab62004-10-14 21:25:53 +0000531 printk_debug("%s subsystem <- %02x/%02x\n",
532 dev_path(dev),
533 MAINBOARD_PCI_SUBSYSTEM_VENDOR_ID,
534 MAINBOARD_PCI_SUBSYSTEM_DEVICE_ID);
535 ops->set_subsystem(dev,
536 MAINBOARD_PCI_SUBSYSTEM_VENDOR_ID,
537 MAINBOARD_PCI_SUBSYSTEM_DEVICE_ID);
538 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000539 command = pci_read_config16(dev, PCI_COMMAND);
540 command |= dev->command;
Eric Biederman5cd81732004-03-11 15:01:31 +0000541 command |= (PCI_COMMAND_PARITY + PCI_COMMAND_SERR); /* error check */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000542 printk_debug("%s cmd <- %02x\n", dev_path(dev), command);
543 pci_write_config16(dev, PCI_COMMAND, command);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000544}
545
546void pci_bus_enable_resources(struct device *dev)
547{
548 uint16_t ctrl;
Li-Ta Lo515f6c72005-01-11 22:48:54 +0000549
550 /* enable IO in command register if there is VGA card
551 * connected with (even it does not claim IO resource) */
552 if (dev->link[0].bridge_ctrl & PCI_BRIDGE_CTL_VGA)
553 dev->command |= PCI_COMMAND_IO;
554
Eric Biedermane9a271e32003-09-02 03:36:25 +0000555 ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
556 ctrl |= dev->link[0].bridge_ctrl;
Eric Biederman5cd81732004-03-11 15:01:31 +0000557 ctrl |= (PCI_BRIDGE_CTL_PARITY + PCI_BRIDGE_CTL_SERR); /* error check */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000558 printk_debug("%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
559 pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
560
561 pci_dev_enable_resources(dev);
Eric Biedermandbec2d42004-10-21 10:44:08 +0000562
563 enable_childrens_resources(dev);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000564}
565
Eric Biedermandbec2d42004-10-21 10:44:08 +0000566void pci_dev_set_subsystem(device_t dev, unsigned vendor, unsigned device)
Eric Biederman03acab62004-10-14 21:25:53 +0000567{
568 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
569 ((device & 0xffff) << 16) | (vendor & 0xffff));
570}
571
Li-Ta Lo883b8792005-01-10 23:16:22 +0000572void pci_dev_init(struct device *dev)
573{
574 struct rom_header *rom, *ram;
575
576 rom = pci_rom_probe(dev);
577 if (rom == NULL)
578 return;
579 ram = pci_rom_load(dev, rom);
580
581 run_bios(dev, ram);
582}
583
Li-Ta Loe5266692004-03-23 21:28:05 +0000584/** Default device operation for PCI devices */
Eric Biedermana9e632c2004-11-18 22:38:08 +0000585static struct pci_operations pci_dev_ops_pci = {
Eric Biederman03acab62004-10-14 21:25:53 +0000586 .set_subsystem = pci_dev_set_subsystem,
587};
588
Eric Biederman8ca8d762003-04-22 19:02:15 +0000589struct device_operations default_pci_ops_dev = {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000590 .read_resources = pci_dev_read_resources,
591 .set_resources = pci_dev_set_resources,
592 .enable_resources = pci_dev_enable_resources,
Li-Ta Lo883b8792005-01-10 23:16:22 +0000593 .init = pci_dev_init,
Li-Ta Loe5266692004-03-23 21:28:05 +0000594 .scan_bus = 0,
Eric Biederman03acab62004-10-14 21:25:53 +0000595 .enable = 0,
Eric Biedermana9e632c2004-11-18 22:38:08 +0000596 .ops_pci = &pci_dev_ops_pci,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000597};
Li-Ta Loe5266692004-03-23 21:28:05 +0000598
599/** Default device operations for PCI bridges */
Eric Biedermana9e632c2004-11-18 22:38:08 +0000600static struct pci_operations pci_bus_ops_pci = {
Eric Biederman03acab62004-10-14 21:25:53 +0000601 .set_subsystem = 0,
602};
Li-Ta Lo883b8792005-01-10 23:16:22 +0000603
Eric Biederman8ca8d762003-04-22 19:02:15 +0000604struct device_operations default_pci_ops_bus = {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000605 .read_resources = pci_bus_read_resources,
606 .set_resources = pci_dev_set_resources,
607 .enable_resources = pci_bus_enable_resources,
Li-Ta Loe5266692004-03-23 21:28:05 +0000608 .init = 0,
609 .scan_bus = pci_scan_bridge,
Eric Biederman03acab62004-10-14 21:25:53 +0000610 .enable = 0,
Eric Biedermana9e632c2004-11-18 22:38:08 +0000611 .ops_pci = &pci_bus_ops_pci,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000612};
Li-Ta Loe5266692004-03-23 21:28:05 +0000613
614/**
615 * @brief Set up PCI device operation
616 *
617 *
618 * @param dev
619 *
620 * @see pci_drivers
621 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000622static void set_pci_ops(struct device *dev)
623{
624 struct pci_driver *driver;
625 if (dev->ops) {
626 return;
627 }
Li-Ta Loe5266692004-03-23 21:28:05 +0000628
Eric Biederman8ca8d762003-04-22 19:02:15 +0000629 /* Look through the list of setup drivers and find one for
Eric Biedermanb78c1972004-10-14 20:54:17 +0000630 * this pci device
631 */
Li-Ta Lo04930692004-11-25 17:37:19 +0000632 for (driver = &pci_drivers[0]; driver != &epci_drivers[0]; driver++) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000633 if ((driver->vendor == dev->vendor) &&
Li-Ta Lo04930692004-11-25 17:37:19 +0000634 (driver->device == dev->device))
Eric Biedermanb78c1972004-10-14 20:54:17 +0000635 {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000636 dev->ops = driver->ops;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000637 printk_debug("%s [%04x/%04x] %sops\n",
638 dev_path(dev),
639 driver->vendor, driver->device,
640 (driver->ops->scan_bus?"bus ":""));
Eric Biederman5899fd82003-04-24 06:25:08 +0000641 return;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000642 }
643 }
Li-Ta Loe5266692004-03-23 21:28:05 +0000644
Eric Biederman8ca8d762003-04-22 19:02:15 +0000645 /* If I don't have a specific driver use the default operations */
646 switch(dev->hdr_type & 0x7f) { /* header type */
647 case PCI_HEADER_TYPE_NORMAL: /* standard header */
648 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
649 goto bad;
650 dev->ops = &default_pci_ops_dev;
651 break;
652 case PCI_HEADER_TYPE_BRIDGE:
653 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
654 goto bad;
655 dev->ops = &default_pci_ops_bus;
656 break;
657 default:
658 bad:
Li-Ta Lo69c5a902004-04-29 20:08:54 +0000659 if (dev->enabled) {
Eric Biederman83b991a2003-10-11 06:20:25 +0000660 printk_err("%s [%04x/%04x/%06x] has unknown header "
Eric Biedermanb78c1972004-10-14 20:54:17 +0000661 "type %02x, ignoring.\n",
662 dev_path(dev),
663 dev->vendor, dev->device,
664 dev->class >> 8, dev->hdr_type);
Eric Biederman83b991a2003-10-11 06:20:25 +0000665 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000666 }
667 return;
668}
669
670/**
Eric Biederman03acab62004-10-14 21:25:53 +0000671 * @brief See if we have already allocated a device structure for a given devfn.
Li-Ta Loe5266692004-03-23 21:28:05 +0000672 *
673 * Given a linked list of PCI device structures and a devfn number, find the
Li-Ta Lo3a812852004-12-03 22:39:34 +0000674 * device structure correspond to the devfn, if present. This function also
675 * removes the device structure from the linked list.
Li-Ta Loe5266692004-03-23 21:28:05 +0000676 *
677 * @param list the device structure list
Eric Biederman8ca8d762003-04-22 19:02:15 +0000678 * @param devfn a device/function number
Li-Ta Loe5266692004-03-23 21:28:05 +0000679 *
Li-Ta Lo3a812852004-12-03 22:39:34 +0000680 * @return pointer to the device structure found or null of we have not
681 * allocated a device for this devfn yet.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000682 */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000683static struct device *pci_scan_get_dev(struct device **list, unsigned int devfn)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000684{
Eric Biedermanb78c1972004-10-14 20:54:17 +0000685 struct device *dev;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000686 dev = 0;
687 for(; *list; list = &(*list)->sibling) {
Eric Biedermanad1b35a2003-10-14 02:36:51 +0000688 if ((*list)->path.type != DEVICE_PATH_PCI) {
Li-Ta Loe5266692004-03-23 21:28:05 +0000689 printk_err("child %s not a pci device\n",
Li-Ta Lo3a812852004-12-03 22:39:34 +0000690 dev_path(*list));
Eric Biedermanad1b35a2003-10-14 02:36:51 +0000691 continue;
692 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000693 if ((*list)->path.u.pci.devfn == devfn) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000694 /* Unlink from the list */
695 dev = *list;
696 *list = (*list)->sibling;
697 dev->sibling = 0;
698 break;
699 }
700 }
Li-Ta Lo3a812852004-12-03 22:39:34 +0000701 /* Just like alloc_dev add the device to the list of device on the bus.
702 * When the list of devices was formed we removed all of the parents
703 * children, and now we are interleaving static and dynamic devices in
704 * order on the bus.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000705 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000706 if (dev) {
707 device_t child;
708 /* Find the last child of our parent */
Li-Ta Lo3a812852004-12-03 22:39:34 +0000709 for (child = dev->bus->children; child && child->sibling; ) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000710 child = child->sibling;
711 }
712 /* Place the device on the list of children of it's parent. */
713 if (child) {
714 child->sibling = dev;
715 } else {
716 dev->bus->children = dev;
717 }
718 }
719
Eric Biederman8ca8d762003-04-22 19:02:15 +0000720 return dev;
721}
722
Eric Biedermanb78c1972004-10-14 20:54:17 +0000723/**
724 * @brief Scan a PCI bus.
Li-Ta Loe5266692004-03-23 21:28:05 +0000725 *
726 * Determine the existence of devices and bridges on a PCI bus. If there are
727 * bridges on the bus, recursively scan the buses behind the bridges.
728 *
Eric Biedermane9a271e32003-09-02 03:36:25 +0000729 * @param bus pointer to the bus structure
730 * @param min_devfn minimum devfn to look at in the scan usually 0x00
731 * @param max_devfn maximum devfn to look at in the scan usually 0xff
Eric Biederman8ca8d762003-04-22 19:02:15 +0000732 * @param max current bus number
Li-Ta Loe5266692004-03-23 21:28:05 +0000733 *
Eric Biederman8ca8d762003-04-22 19:02:15 +0000734 * @return The maximum bus number found, after scanning all subordinate busses
735 */
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000736unsigned int pci_scan_bus(struct bus *bus, unsigned min_devfn, unsigned max_devfn,
737 unsigned int max)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000738{
739 unsigned int devfn;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000740 device_t dev;
741 device_t old_devices;
742 device_t child;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000743
744 printk_debug("PCI: pci_scan_bus for bus %d\n", bus->secondary);
745
746 old_devices = bus->children;
747 bus->children = 0;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000748
749 post_code(0x24);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000750
Li-Ta Lo9782f752004-05-05 21:15:42 +0000751 /* probe all devices/functions on this bus with some optimization for
Eric Biedermanb78c1972004-10-14 20:54:17 +0000752 * non-existence and single funcion devices
753 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000754 for (devfn = min_devfn; devfn <= max_devfn; devfn++) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000755 uint32_t id, class;
Eric Biederman30e143a2003-09-01 23:45:32 +0000756 uint8_t hdr_type;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000757
Eric Biederman03acab62004-10-14 21:25:53 +0000758 /* First thing setup the device structure */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000759 dev = pci_scan_get_dev(&old_devices, devfn);
Eric Biederman03acab62004-10-14 21:25:53 +0000760
761 /* Detect if a device is present */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000762 if (!dev) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000763 struct device dummy;
764 dummy.bus = bus;
765 dummy.path.type = DEVICE_PATH_PCI;
766 dummy.path.u.pci.devfn = devfn;
767 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
768 /* some broken boards return 0 if a slot is empty: */
Li-Ta Lo04930692004-11-25 17:37:19 +0000769 if ((id == 0xffffffff) || (id == 0x00000000) ||
770 (id == 0x0000ffff) || (id == 0xffff0000))
Eric Biedermanb78c1972004-10-14 20:54:17 +0000771 {
772 printk_spew("PCI: devfn 0x%x, bad id 0x%x\n", devfn, id);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000773 if (PCI_FUNC(devfn) == 0x00) {
Eric Biedermanb78c1972004-10-14 20:54:17 +0000774 /* if this is a function 0 device and
775 * it is not present,
776 * skip to next device
777 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000778 devfn += 0x07;
779 }
Eric Biedermanb78c1972004-10-14 20:54:17 +0000780 /* This function in a multi function device is
781 * not present, skip to the next function.
782 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000783 continue;
784 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000785 dev = alloc_dev(bus, &dummy.path);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000786 }
787 else {
Eric Biederman03acab62004-10-14 21:25:53 +0000788 /* Enable/disable the device. Once we have
789 * found the device specific operations this
790 * operations we will disable the device with
791 * those as well.
792 *
Eric Biedermanb78c1972004-10-14 20:54:17 +0000793 * This is geared toward devices that have subfunctions
794 * that do not show up by default.
795 *
796 * If a device is a stuff option on the motherboard
797 * it may be absent and enable_dev must cope.
798 *
799 */
Eric Biederman7003ba42004-10-16 06:20:29 +0000800 if (dev->chip_ops && dev->chip_ops->enable_dev)
Eric Biedermanb78c1972004-10-14 20:54:17 +0000801 {
Eric Biederman7003ba42004-10-16 06:20:29 +0000802 dev->chip_ops->enable_dev(dev);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000803 }
804 /* Now read the vendor and device id */
805 id = pci_read_config32(dev, PCI_VENDOR_ID);
Eric Biederman03acab62004-10-14 21:25:53 +0000806
807 /* If the device does not have a pci id disable it.
808 * Possibly this is because we have already disabled
809 * the device. But this also handles optional devices
810 * that may not always show up.
811 */
812 if (id == 0xffffffff || id == 0x00000000 ||
Li-Ta Lo04930692004-11-25 17:37:19 +0000813 id == 0x0000ffff || id == 0xffff0000)
Eric Biederman03acab62004-10-14 21:25:53 +0000814 {
815 if (dev->enabled) {
816 printk_info("Disabling static device: %s\n",
817 dev_path(dev));
818 dev->enabled = 0;
819 }
820 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000821 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000822 /* Read the rest of the pci configuration information */
823 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
824 class = pci_read_config32(dev, PCI_CLASS_REVISION);
Li-Ta Lo9782f752004-05-05 21:15:42 +0000825
Eric Biedermane9a271e32003-09-02 03:36:25 +0000826 /* Store the interesting information in the device structure */
827 dev->vendor = id & 0xffff;
828 dev->device = (id >> 16) & 0xffff;
829 dev->hdr_type = hdr_type;
830 /* class code, the upper 3 bytes of PCI_CLASS_REVISION */
831 dev->class = class >> 8;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000832
Eric Biederman03acab62004-10-14 21:25:53 +0000833 /* Architectural/System devices always need to
834 * be bus masters.
835 */
836 if ((dev->class >> 16) == PCI_BASE_CLASS_SYSTEM) {
837 dev->command |= PCI_COMMAND_MASTER;
838 }
839
Eric Biederman8ca8d762003-04-22 19:02:15 +0000840 /* Look at the vendor and device id, or at least the
Li-Ta Loe5266692004-03-23 21:28:05 +0000841 * header type and class and figure out which set of
842 * configuration methods to use. Unless we already
843 * have some pci ops.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000844 */
Eric Biederman83b991a2003-10-11 06:20:25 +0000845 set_pci_ops(dev);
846 /* Error if we don't have some pci operations for it */
Eric Biederman5cd81732004-03-11 15:01:31 +0000847 if (!dev->ops) {
Eric Biederman83b991a2003-10-11 06:20:25 +0000848 printk_err("%s No device operations\n",
Eric Biedermanb78c1972004-10-14 20:54:17 +0000849 dev_path(dev));
Eric Biederman83b991a2003-10-11 06:20:25 +0000850 continue;
851 }
852
853 /* Now run the magic enable/disable sequence for the device */
854 if (dev->ops && dev->ops->enable) {
855 dev->ops->enable(dev);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000856 }
Eric Biederman4086d162003-07-17 03:26:03 +0000857
Eric Biedermane9a271e32003-09-02 03:36:25 +0000858 printk_debug("%s [%04x/%04x] %s\n",
Eric Biederman03acab62004-10-14 21:25:53 +0000859 dev_path(dev),
860 dev->vendor, dev->device,
861 dev->enabled?"enabled": "disabled");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000862
Eric Biederman8ca8d762003-04-22 19:02:15 +0000863 if (PCI_FUNC(devfn) == 0x00 && (hdr_type & 0x80) != 0x80) {
Eric Biedermanb78c1972004-10-14 20:54:17 +0000864 /* if this is not a multi function device,
865 * don't waste time probing another function.
866 * Skip to next device.
867 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000868 devfn += 0x07;
869 }
870 }
871 post_code(0x25);
872
Eric Biedermanb78c1972004-10-14 20:54:17 +0000873 /* For all children that implement scan_bus (i.e. bridges)
874 * scan the bus behind that child.
875 */
Li-Ta Lo04930692004-11-25 17:37:19 +0000876 for (child = bus->children; child; child = child->sibling) {
Eric Biedermanb78c1972004-10-14 20:54:17 +0000877 if (!child->enabled ||
Li-Ta Lo04930692004-11-25 17:37:19 +0000878 !child->ops ||
879 !child->ops->scan_bus)
Eric Biedermanb78c1972004-10-14 20:54:17 +0000880 {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000881 continue;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000882 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000883 max = child->ops->scan_bus(child, max);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000884 }
Li-Ta Loe5266692004-03-23 21:28:05 +0000885
Eric Biederman8ca8d762003-04-22 19:02:15 +0000886 /*
887 * We've scanned the bus and so we know all about what's on
888 * the other side of any bridges that may be on this bus plus
889 * any devices.
890 *
891 * Return how far we've got finding sub-buses.
892 */
893 printk_debug("PCI: pci_scan_bus returning with max=%02x\n", max);
894 post_code(0x55);
895 return max;
896}
897
Li-Ta Loe5266692004-03-23 21:28:05 +0000898/**
899 * @brief Scan a PCI bridge and the buses behind the bridge.
900 *
901 * Determine the existence of buses behind the bridge. Set up the bridge
902 * according to the result of the scan.
903 *
904 * This function is the default scan_bus() method for PCI bridge devices.
905 *
906 * @param dev pointer to the bridge device
907 * @param max the highest bus number assgined up to now
908 *
Eric Biederman8ca8d762003-04-22 19:02:15 +0000909 * @return The maximum bus number found, after scanning all subordinate busses
910 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000911unsigned int pci_scan_bridge(struct device *dev, unsigned int max)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000912{
Eric Biedermane9a271e32003-09-02 03:36:25 +0000913 struct bus *bus;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000914 uint32_t buses;
915 uint16_t cr;
Eric Biederman83b991a2003-10-11 06:20:25 +0000916
Li-Ta Lo3a812852004-12-03 22:39:34 +0000917 printk_spew("%s for %s\n", __func__, dev_path(dev));
918
Eric Biedermane9a271e32003-09-02 03:36:25 +0000919 bus = &dev->link[0];
Eric Biedermana9e632c2004-11-18 22:38:08 +0000920 bus->dev = dev;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000921 dev->links = 1;
922
Eric Biederman8ca8d762003-04-22 19:02:15 +0000923 /* Set up the primary, secondary and subordinate bus numbers. We have
924 * no idea how many buses are behind this bridge yet, so we set the
Eric Biedermanb78c1972004-10-14 20:54:17 +0000925 * subordinate bus number to 0xff for the moment.
926 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000927 bus->secondary = ++max;
928 bus->subordinate = 0xff;
Li-Ta Loe5266692004-03-23 21:28:05 +0000929
Eric Biederman8ca8d762003-04-22 19:02:15 +0000930 /* Clear all status bits and turn off memory, I/O and master enables. */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000931 cr = pci_read_config16(dev, PCI_COMMAND);
932 pci_write_config16(dev, PCI_COMMAND, 0x0000);
933 pci_write_config16(dev, PCI_STATUS, 0xffff);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000934
Eric Biedermanb78c1972004-10-14 20:54:17 +0000935 /*
936 * Read the existing primary/secondary/subordinate bus
937 * number configuration.
938 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000939 buses = pci_read_config32(dev, PCI_PRIMARY_BUS);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000940
941 /* Configure the bus numbers for this bridge: the configuration
942 * transactions will not be propagated by the bridge if it is not
Eric Biedermanb78c1972004-10-14 20:54:17 +0000943 * correctly configured.
944 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000945 buses &= 0xff000000;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000946 buses |= (((unsigned int) (dev->bus->secondary) << 0) |
Li-Ta Lo3a812852004-12-03 22:39:34 +0000947 ((unsigned int) (bus->secondary) << 8) |
948 ((unsigned int) (bus->subordinate) << 16));
Eric Biedermane9a271e32003-09-02 03:36:25 +0000949 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
Li-Ta Lo3a812852004-12-03 22:39:34 +0000950
Eric Biedermanb78c1972004-10-14 20:54:17 +0000951 /* Now we can scan all subordinate buses
952 * i.e. the bus behind the bridge.
953 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000954 max = pci_scan_bus(bus, 0x00, 0xff, max);
Li-Ta Lo3a812852004-12-03 22:39:34 +0000955
Eric Biederman8ca8d762003-04-22 19:02:15 +0000956 /* We know the number of buses behind this bridge. Set the subordinate
Eric Biedermanb78c1972004-10-14 20:54:17 +0000957 * bus number to its real value.
958 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000959 bus->subordinate = max;
960 buses = (buses & 0xff00ffff) |
961 ((unsigned int) (bus->subordinate) << 16);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000962 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
963 pci_write_config16(dev, PCI_COMMAND, cr);
Li-Ta Lo3a812852004-12-03 22:39:34 +0000964
Eric Biedermanb78c1972004-10-14 20:54:17 +0000965 printk_spew("%s returns max %d\n", __func__, max);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000966 return max;
967}
Li-Ta Loe5266692004-03-23 21:28:05 +0000968
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +0000969/*
970 Tell the EISA int controller this int must be level triggered
971 THIS IS A KLUDGE -- sorry, this needs to get cleaned up.
972*/
973static void pci_level_irq(unsigned char intNum)
974{
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +0000975 unsigned short intBits = inb(0x4d0) | (((unsigned) inb(0x4d1)) << 8);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +0000976
Eric Biedermanb78c1972004-10-14 20:54:17 +0000977 printk_spew("%s: current ints are 0x%x\n", __func__, intBits);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +0000978 intBits |= (1 << intNum);
979
Eric Biedermanb78c1972004-10-14 20:54:17 +0000980 printk_spew("%s: try to set ints 0x%x\n", __func__, intBits);
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +0000981
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +0000982 // Write new values
983 outb((unsigned char) intBits, 0x4d0);
984 outb((unsigned char) (intBits >> 8), 0x4d1);
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +0000985
Ronald G. Minnichb56ef072003-10-15 20:05:11 +0000986 /* this seems like an error but is not ... */
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +0000987#if 1
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +0000988 if (inb(0x4d0) != (intBits & 0xf)) {
989 printk_err("%s: lower order bits are wrong: want 0x%x, got 0x%x\n",
Eric Biedermanb78c1972004-10-14 20:54:17 +0000990 __func__, intBits &0xf, inb(0x4d0));
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +0000991 }
992 if (inb(0x4d1) != ((intBits >> 8) & 0xf)) {
993 printk_err("%s: lower order bits are wrong: want 0x%x, got 0x%x\n",
Eric Biedermanb78c1972004-10-14 20:54:17 +0000994 __func__, (intBits>>8) &0xf, inb(0x4d1));
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +0000995 }
Ronald G. Minnichb56ef072003-10-15 20:05:11 +0000996#endif
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +0000997}
998
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +0000999/*
1000 This function assigns IRQs for all functions contained within
1001 the indicated device address. If the device does not exist or does
1002 not require interrupts then this function has no effect.
1003
1004 This function should be called for each PCI slot in your system.
1005
1006 pIntAtoD is an array of IRQ #s that are assigned to PINTA through PINTD of
1007 this slot.
1008 The particular irq #s that are passed in depend on the routing inside
1009 your southbridge and on your motherboard.
1010
1011 -kevinh@ispiri.com
1012*/
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +00001013void pci_assign_irqs(unsigned bus, unsigned slot, const unsigned char pIntAtoD[4])
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001014{
1015 unsigned functNum;
1016 device_t pdev;
1017 unsigned char line;
1018 unsigned char irq;
1019 unsigned char readback;
1020
1021 /* Each slot may contain up to eight functions */
1022 for (functNum = 0; functNum < 8; functNum++) {
1023 pdev = dev_find_slot(bus, (slot << 3) + functNum);
1024
1025 if (pdev) {
1026 line = pci_read_config8(pdev, PCI_INTERRUPT_PIN);
1027
1028 // PCI spec says all other values are reserved
1029 if ((line >= 1) && (line <= 4)) {
1030 irq = pIntAtoD[line - 1];
1031
1032 printk_debug("Assigning IRQ %d to %d:%x.%d\n", \
1033 irq, bus, slot, functNum);
1034
1035 pci_write_config8(pdev, PCI_INTERRUPT_LINE,\
1036 pIntAtoD[line - 1]);
1037
1038 readback = pci_read_config8(pdev, PCI_INTERRUPT_LINE);
1039 printk_debug(" Readback = %d\n", readback);
1040
1041 // Change to level triggered
1042 pci_level_irq(pIntAtoD[line - 1]);
1043 }
1044 }
1045 }
1046}