blob: f65849ef06556dd98fde54494915f74cba3042b6 [file] [log] [blame]
Eric Biederman8ca8d762003-04-22 19:02:15 +00001/*
Stefan Reinauer7e61e452008-01-18 10:35:56 +00002 * This file is part of the coreboot project.
Uwe Hermannb80dbf02007-04-22 19:08:13 +00003 *
4 * It was originally based on the Linux kernel (drivers/pci/pci.c).
5 *
6 * Modifications are:
7 * Copyright (C) 2003-2004 Linux Networx
8 * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
9 * Copyright (C) 2003-2006 Ronald G. Minnich <rminnich@gmail.com>
10 * Copyright (C) 2004-2005 Li-Ta Lo <ollie@lanl.gov>
11 * Copyright (C) 2005-2006 Tyan
12 * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan)
13 * Copyright (C) 2005-2007 Stefan Reinauer <stepan@openbios.org>
14 */
15
16/*
Eric Biederman8ca8d762003-04-22 19:02:15 +000017 * PCI Bus Services, see include/linux/pci.h for further explanation.
18 *
19 * Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter,
20 * David Mosberger-Tang
21 *
22 * Copyright 1997 -- 1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
Eric Biederman8ca8d762003-04-22 19:02:15 +000023 */
24
25#include <console/console.h>
26#include <stdlib.h>
27#include <stdint.h>
28#include <bitops.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000029#include <string.h>
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +000030#include <arch/io.h>
Eric Biederman5899fd82003-04-24 06:25:08 +000031#include <device/device.h>
32#include <device/pci.h>
33#include <device/pci_ids.h>
Eric Biedermane9a271e32003-09-02 03:36:25 +000034#include <part/hard_reset.h>
Eric Biederman30e143a2003-09-01 23:45:32 +000035#include <part/fallback_boot.h>
Eric Biederman03acab62004-10-14 21:25:53 +000036#include <delay.h>
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000037#if CONFIG_HYPERTRANSPORT_PLUGIN_SUPPORT == 1
38#include <device/hypertransport.h>
39#endif
40#if CONFIG_PCIX_PLUGIN_SUPPORT == 1
41#include <device/pcix.h>
42#endif
43#if CONFIG_PCIEXP_PLUGIN_SUPPORT == 1
44#include <device/pciexp.h>
45#endif
46#if CONFGI_AGP_PLUGIN_SUPPORT == 1
47#include <device/agp.h>
48#endif
49#if CONFIG_CARDBUS_PLUGIN_SUPPORT == 1
50#include <device/cardbus.h>
51#endif
Eric Biederman03acab62004-10-14 21:25:53 +000052
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000053uint8_t pci_moving_config8(struct device *dev, unsigned reg)
Eric Biederman03acab62004-10-14 21:25:53 +000054{
55 uint8_t value, ones, zeroes;
56 value = pci_read_config8(dev, reg);
57
58 pci_write_config8(dev, reg, 0xff);
59 ones = pci_read_config8(dev, reg);
60
61 pci_write_config8(dev, reg, 0x00);
62 zeroes = pci_read_config8(dev, reg);
63
64 pci_write_config8(dev, reg, value);
65
66 return ones ^ zeroes;
67}
Li-Ta Lo9a5b4962004-12-23 21:48:01 +000068
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000069uint16_t pci_moving_config16(struct device *dev, unsigned reg)
Eric Biederman03acab62004-10-14 21:25:53 +000070{
71 uint16_t value, ones, zeroes;
72 value = pci_read_config16(dev, reg);
73
74 pci_write_config16(dev, reg, 0xffff);
75 ones = pci_read_config16(dev, reg);
76
77 pci_write_config16(dev, reg, 0x0000);
78 zeroes = pci_read_config16(dev, reg);
79
80 pci_write_config16(dev, reg, value);
81
82 return ones ^ zeroes;
83}
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +000084
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000085uint32_t pci_moving_config32(struct device *dev, unsigned reg)
Eric Biederman03acab62004-10-14 21:25:53 +000086{
87 uint32_t value, ones, zeroes;
88 value = pci_read_config32(dev, reg);
89
90 pci_write_config32(dev, reg, 0xffffffff);
91 ones = pci_read_config32(dev, reg);
92
93 pci_write_config32(dev, reg, 0x00000000);
94 zeroes = pci_read_config32(dev, reg);
95
96 pci_write_config32(dev, reg, value);
97
98 return ones ^ zeroes;
99}
100
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000101unsigned pci_find_next_capability(device_t dev, unsigned cap, unsigned last)
Eric Biederman03acab62004-10-14 21:25:53 +0000102{
103 unsigned pos;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000104 unsigned status;
105 unsigned reps = 48;
Eric Biederman03acab62004-10-14 21:25:53 +0000106 pos = 0;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000107 status = pci_read_config16(dev, PCI_STATUS);
108 if (!(status & PCI_STATUS_CAP_LIST)) {
109 return 0;
110 }
Eric Biederman03acab62004-10-14 21:25:53 +0000111 switch(dev->hdr_type & 0x7f) {
112 case PCI_HEADER_TYPE_NORMAL:
113 case PCI_HEADER_TYPE_BRIDGE:
114 pos = PCI_CAPABILITY_LIST;
115 break;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000116 case PCI_HEADER_TYPE_CARDBUS:
117 pos = PCI_CB_CAPABILITY_LIST;
118 break;
119 default:
120 return 0;
Eric Biederman03acab62004-10-14 21:25:53 +0000121 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000122 pos = pci_read_config8(dev, pos);
123 while(reps-- && (pos >= 0x40)) { /* loop through the linked list */
Eric Biederman03acab62004-10-14 21:25:53 +0000124 int this_cap;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000125 pos &= ~3;
Eric Biederman03acab62004-10-14 21:25:53 +0000126 this_cap = pci_read_config8(dev, pos + PCI_CAP_LIST_ID);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000127 printk_spew("Capability: 0x%02x @ 0x%02x\n", cap, pos);
128 if (this_cap == 0xff) {
129 break;
130 }
131 if (!last && (this_cap == cap)) {
Eric Biederman03acab62004-10-14 21:25:53 +0000132 return pos;
133 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000134 if (last == pos) {
135 last = 0;
136 }
137 pos = pci_read_config8(dev, pos + PCI_CAP_LIST_NEXT);
Eric Biederman03acab62004-10-14 21:25:53 +0000138 }
139 return 0;
140}
141
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000142unsigned pci_find_capability(device_t dev, unsigned cap)
143{
144 return pci_find_next_capability(dev, cap, 0);
145
146}
147
Eric Biederman8ca8d762003-04-22 19:02:15 +0000148/** Given a device and register, read the size of the BAR for that register.
149 * @param dev Pointer to the device structure
150 * @param resource Pointer to the resource structure
151 * @param index Address of the pci configuration register
152 */
Eric Biederman03acab62004-10-14 21:25:53 +0000153struct resource *pci_get_resource(struct device *dev, unsigned long index)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000154{
Eric Biederman5cd81732004-03-11 15:01:31 +0000155 struct resource *resource;
Eric Biederman03acab62004-10-14 21:25:53 +0000156 unsigned long value, attr;
157 resource_t moving, limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000158
159 /* Initialize the resources to nothing */
Eric Biederman03acab62004-10-14 21:25:53 +0000160 resource = new_resource(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000161
Eric Biederman03acab62004-10-14 21:25:53 +0000162 /* Get the initial value */
163 value = pci_read_config32(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000164
Eric Biederman03acab62004-10-14 21:25:53 +0000165 /* See which bits move */
166 moving = pci_moving_config32(dev, index);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000167
Eric Biederman03acab62004-10-14 21:25:53 +0000168 /* Initialize attr to the bits that do not move */
169 attr = value & ~moving;
170
171 /* If it is a 64bit resource look at the high half as well */
172 if (((attr & PCI_BASE_ADDRESS_SPACE_IO) == 0) &&
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000173 ((attr & PCI_BASE_ADDRESS_MEM_LIMIT_MASK) == PCI_BASE_ADDRESS_MEM_LIMIT_64))
Eric Biederman03acab62004-10-14 21:25:53 +0000174 {
175 /* Find the high bits that move */
176 moving |= ((resource_t)pci_moving_config32(dev, index + 4)) << 32;
177 }
178 /* Find the resource constraints.
179 *
180 * Start by finding the bits that move. From there:
181 * - Size is the least significant bit of the bits that move.
182 * - Limit is all of the bits that move plus all of the lower bits.
183 * See PCI Spec 6.2.5.1 ...
Eric Biederman8ca8d762003-04-22 19:02:15 +0000184 */
Eric Biederman03acab62004-10-14 21:25:53 +0000185 limit = 0;
186 if (moving) {
187 resource->size = 1;
188 resource->align = resource->gran = 0;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000189 while(!(moving & resource->size)) {
Eric Biederman03acab62004-10-14 21:25:53 +0000190 resource->size <<= 1;
191 resource->align += 1;
192 resource->gran += 1;
193 }
194 resource->limit = limit = moving | (resource->size - 1);
195 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000196 /*
197 * some broken hardware has read-only registers that do not
Eric Biederman03acab62004-10-14 21:25:53 +0000198 * really size correctly.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000199 * Example: the acer m7229 has BARs 1-4 normally read-only.
200 * so BAR1 at offset 0x10 reads 0x1f1. If you size that register
201 * by writing 0xffffffff to it, it will read back as 0x1f1 -- a
202 * violation of the spec.
Eric Biederman03acab62004-10-14 21:25:53 +0000203 * We catch this case and ignore it by observing which bits move,
204 * This also catches the common case unimplemented registers
205 * that always read back as 0.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000206 */
Eric Biederman03acab62004-10-14 21:25:53 +0000207 if (moving == 0) {
208 if (value != 0) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000209 printk_debug(
210 "%s register %02x(%08x), read-only ignoring it\n",
211 dev_path(dev), index, value);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000212 }
213 resource->flags = 0;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000214 }
215 else if (attr & PCI_BASE_ADDRESS_SPACE_IO) {
Eric Biederman03acab62004-10-14 21:25:53 +0000216 /* An I/O mapped base address */
217 attr &= PCI_BASE_ADDRESS_IO_ATTR_MASK;
Eric Biederman5cd81732004-03-11 15:01:31 +0000218 resource->flags |= IORESOURCE_IO;
Eric Biederman03acab62004-10-14 21:25:53 +0000219 /* I don't want to deal with 32bit I/O resources */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000220 resource->limit = 0xffff;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000221 }
222 else {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000223 /* A Memory mapped base address */
Eric Biederman03acab62004-10-14 21:25:53 +0000224 attr &= PCI_BASE_ADDRESS_MEM_ATTR_MASK;
Eric Biederman5cd81732004-03-11 15:01:31 +0000225 resource->flags |= IORESOURCE_MEM;
Eric Biederman03acab62004-10-14 21:25:53 +0000226 if (attr & PCI_BASE_ADDRESS_MEM_PREFETCH) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000227 resource->flags |= IORESOURCE_PREFETCH;
228 }
Eric Biederman03acab62004-10-14 21:25:53 +0000229 attr &= PCI_BASE_ADDRESS_MEM_LIMIT_MASK;
230 if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_32) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000231 /* 32bit limit */
232 resource->limit = 0xffffffffUL;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000233 }
234 else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_1M) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000235 /* 1MB limit */
236 resource->limit = 0x000fffffUL;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000237 }
238 else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_64) {
Eric Biederman03acab62004-10-14 21:25:53 +0000239 /* 64bit limit */
240 resource->limit = 0xffffffffffffffffULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000241 resource->flags |= IORESOURCE_PCI64;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000242 }
243 else {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000244 /* Invalid value */
245 resource->flags = 0;
246 }
247 }
Eric Biederman03acab62004-10-14 21:25:53 +0000248 /* Don't let the limit exceed which bits can move */
249 if (resource->limit > limit) {
250 resource->limit = limit;
251 }
252#if 0
253 if (resource->flags) {
254 printk_debug("%s %02x ->",
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000255 dev_path(dev), resource->index);
Eric Biederman03acab62004-10-14 21:25:53 +0000256 printk_debug(" value: 0x%08Lx zeroes: 0x%08Lx ones: 0x%08Lx attr: %08lx\n",
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000257 value, zeroes, ones, attr);
Eric Biederman03acab62004-10-14 21:25:53 +0000258 printk_debug(
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000259 "%s %02x -> size: 0x%08Lx max: 0x%08Lx %s\n ",
Eric Biederman03acab62004-10-14 21:25:53 +0000260 dev_path(dev),
261 resource->index,
262 resource->size, resource->limit,
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000263 resource_type(resource));
Eric Biederman03acab62004-10-14 21:25:53 +0000264 }
265#endif
266
Eric Biederman5cd81732004-03-11 15:01:31 +0000267 return resource;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000268}
269
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000270static void pci_get_rom_resource(struct device *dev, unsigned long index)
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000271{
272 struct resource *resource;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000273 unsigned long value;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000274 resource_t moving, limit;
275
Li-Ta Lobec039c2005-01-19 23:19:26 +0000276 if ((dev->on_mainboard) && (dev->rom_address == 0)) {
277 //skip it if rom_address is not set in MB Config.lb
Yinghai Lubcde1612005-01-14 05:34:09 +0000278 return;
279 }
280
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000281 /* Initialize the resources to nothing */
282 resource = new_resource(dev, index);
283
284 /* Get the initial value */
285 value = pci_read_config32(dev, index);
286
287 /* See which bits move */
288 moving = pci_moving_config32(dev, index);
289 /* clear the Enable bit */
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000290 moving = moving & ~PCI_ROM_ADDRESS_ENABLE;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000291
292 /* Find the resource constraints.
293 *
294 * Start by finding the bits that move. From there:
295 * - Size is the least significant bit of the bits that move.
296 * - Limit is all of the bits that move plus all of the lower bits.
297 * See PCI Spec 6.2.5.1 ...
298 */
299 limit = 0;
300
301 if (moving) {
302 resource->size = 1;
303 resource->align = resource->gran = 0;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000304 while (!(moving & resource->size)) {
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000305 resource->size <<= 1;
306 resource->align += 1;
307 resource->gran += 1;
308 }
309 resource->limit = limit = moving | (resource->size - 1);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000310 }
311
312 if (moving == 0) {
313 if (value != 0) {
314 printk_debug("%s register %02x(%08x), read-only ignoring it\n",
315 dev_path(dev), index, value);
316 }
317 resource->flags = 0;
318 } else {
319 resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY;
320 }
Yinghai Luc7870ac2005-01-13 19:14:52 +0000321
322 /* for on board device with embedded ROM image, the ROM image is at
323 * fixed address specified in the Config.lb, the dev->rom_address is
324 * inited by driver_pci_onboard_ops::enable_dev() */
Yinghai Lubcde1612005-01-14 05:34:09 +0000325 if ((dev->on_mainboard) && (dev->rom_address != 0)) {
Yinghai Luc7870ac2005-01-13 19:14:52 +0000326 resource->base = dev->rom_address;
327 resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY |
328 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
329 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000330
331 compact_resources(dev);
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000332}
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000333
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000334/** Read the base address registers for a given device.
335 * @param dev Pointer to the dev structure
336 * @param howmany How many registers to read (6 for device, 2 for bridge)
337 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000338static void pci_read_bases(struct device *dev, unsigned int howmany)
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000339{
340 unsigned long index;
341
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000342 for(index = PCI_BASE_ADDRESS_0; (index < PCI_BASE_ADDRESS_0 + (howmany << 2)); ) {
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000343 struct resource *resource;
344 resource = pci_get_resource(dev, index);
345 index += (resource->flags & IORESOURCE_PCI64)?8:4;
346 }
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000347
348 compact_resources(dev);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000349}
350
Eric Biederman03acab62004-10-14 21:25:53 +0000351static void pci_set_resource(struct device *dev, struct resource *resource);
352
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000353static void pci_record_bridge_resource(
354 struct device *dev, resource_t moving,
355 unsigned index, unsigned long mask, unsigned long type)
Eric Biederman03acab62004-10-14 21:25:53 +0000356{
357 /* Initiliaze the constraints on the current bus */
358 struct resource *resource;
359 resource = 0;
360 if (moving) {
361 unsigned long gran;
362 resource_t step;
363 resource = new_resource(dev, index);
364 resource->size = 0;
365 gran = 0;
366 step = 1;
367 while((moving & step) == 0) {
368 gran += 1;
369 step <<= 1;
370 }
371 resource->gran = gran;
372 resource->align = gran;
373 resource->limit = moving | (step - 1);
374 resource->flags = type | IORESOURCE_PCI_BRIDGE;
375 compute_allocate_resource(&dev->link[0], resource, mask, type);
376 /* If there is nothing behind the resource,
377 * clear it and forget it.
378 */
379 if (resource->size == 0) {
380 resource->base = moving;
381 resource->flags |= IORESOURCE_ASSIGNED;
382 resource->flags &= ~IORESOURCE_STORED;
383 pci_set_resource(dev, resource);
384 resource->flags = 0;
385 }
386 }
387 return;
388}
389
Eric Biederman8ca8d762003-04-22 19:02:15 +0000390static void pci_bridge_read_bases(struct device *dev)
391{
Eric Biederman03acab62004-10-14 21:25:53 +0000392 resource_t moving_base, moving_limit, moving;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000393
Eric Biederman03acab62004-10-14 21:25:53 +0000394 /* See if the bridge I/O resources are implemented */
395 moving_base = ((uint32_t)pci_moving_config8(dev, PCI_IO_BASE)) << 8;
396 moving_base |= ((uint32_t)pci_moving_config16(dev, PCI_IO_BASE_UPPER16)) << 16;
397
398 moving_limit = ((uint32_t)pci_moving_config8(dev, PCI_IO_LIMIT)) << 8;
399 moving_limit |= ((uint32_t)pci_moving_config16(dev, PCI_IO_LIMIT_UPPER16)) << 16;
400
401 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000402
403 /* Initialize the io space constraints on the current bus */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000404 pci_record_bridge_resource(
405 dev, moving, PCI_IO_BASE,
406 IORESOURCE_IO, IORESOURCE_IO);
407
Eric Biederman03acab62004-10-14 21:25:53 +0000408
409 /* See if the bridge prefmem resources are implemented */
410 moving_base = ((resource_t)pci_moving_config16(dev, PCI_PREF_MEMORY_BASE)) << 16;
411 moving_base |= ((resource_t)pci_moving_config32(dev, PCI_PREF_BASE_UPPER32)) << 32;
412
413 moving_limit = ((resource_t)pci_moving_config16(dev, PCI_PREF_MEMORY_LIMIT)) << 16;
414 moving_limit |= ((resource_t)pci_moving_config32(dev, PCI_PREF_LIMIT_UPPER32)) << 32;
415
416 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000417 /* Initiliaze the prefetchable memory constraints on the current bus */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000418 pci_record_bridge_resource(
419 dev, moving, PCI_PREF_MEMORY_BASE,
420 IORESOURCE_MEM | IORESOURCE_PREFETCH,
421 IORESOURCE_MEM | IORESOURCE_PREFETCH);
422
Eric Biederman03acab62004-10-14 21:25:53 +0000423
424 /* See if the bridge mem resources are implemented */
425 moving_base = ((uint32_t)pci_moving_config16(dev, PCI_MEMORY_BASE)) << 16;
426 moving_limit = ((uint32_t)pci_moving_config16(dev, PCI_MEMORY_LIMIT)) << 16;
427
428 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000429
430 /* Initialize the memory resources on the current bus */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000431 pci_record_bridge_resource(
432 dev, moving, PCI_MEMORY_BASE,
433 IORESOURCE_MEM | IORESOURCE_PREFETCH,
434 IORESOURCE_MEM);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000435
Eric Biederman5cd81732004-03-11 15:01:31 +0000436 compact_resources(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000437}
438
Eric Biederman5899fd82003-04-24 06:25:08 +0000439void pci_dev_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000440{
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000441 pci_read_bases(dev, 6);
442 pci_get_rom_resource(dev, PCI_ROM_ADDRESS);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000443}
444
Eric Biederman5899fd82003-04-24 06:25:08 +0000445void pci_bus_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000446{
Eric Biederman8ca8d762003-04-22 19:02:15 +0000447 pci_bridge_read_bases(dev);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000448 pci_read_bases(dev, 2);
449 pci_get_rom_resource(dev, PCI_ROM_ADDRESS1);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000450}
451
Eric Biederman8ca8d762003-04-22 19:02:15 +0000452static void pci_set_resource(struct device *dev, struct resource *resource)
453{
Eric Biederman03acab62004-10-14 21:25:53 +0000454 resource_t base, end;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000455
Eric Biederman8ca8d762003-04-22 19:02:15 +0000456 /* Make certain the resource has actually been set */
Eric Biederman5cd81732004-03-11 15:01:31 +0000457 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000458 printk_err("ERROR: %s %02x %s size: 0x%010Lx not assigned\n",
459 dev_path(dev), resource->index,
460 resource_type(resource),
461 resource->size);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000462 return;
463 }
464
Eric Biederman5cd81732004-03-11 15:01:31 +0000465 /* If I have already stored this resource don't worry about it */
466 if (resource->flags & IORESOURCE_STORED) {
467 return;
468 }
469
Eric Biederman03acab62004-10-14 21:25:53 +0000470 /* If the resources is substractive don't worry about it */
471 if (resource->flags & IORESOURCE_SUBTRACTIVE) {
472 return;
473 }
474
Eric Biederman8ca8d762003-04-22 19:02:15 +0000475 /* Only handle PCI memory and IO resources for now */
476 if (!(resource->flags & (IORESOURCE_MEM |IORESOURCE_IO)))
477 return;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000478
Eric Biederman03acab62004-10-14 21:25:53 +0000479 /* Enable the resources in the command register */
480 if (resource->size) {
481 if (resource->flags & IORESOURCE_MEM) {
482 dev->command |= PCI_COMMAND_MEMORY;
483 }
484 if (resource->flags & IORESOURCE_IO) {
485 dev->command |= PCI_COMMAND_IO;
486 }
487 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
488 dev->command |= PCI_COMMAND_MASTER;
489 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000490 }
491 /* Get the base address */
492 base = resource->base;
Eric Biederman5cd81732004-03-11 15:01:31 +0000493
Eric Biederman03acab62004-10-14 21:25:53 +0000494 /* Get the end */
495 end = resource_end(resource);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000496
Eric Biederman5cd81732004-03-11 15:01:31 +0000497 /* Now store the resource */
498 resource->flags |= IORESOURCE_STORED;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000499 if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) {
Eric Biederman03acab62004-10-14 21:25:53 +0000500 unsigned long base_lo, base_hi;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000501 /*
502 * some chipsets allow us to set/clear the IO bit.
503 * (e.g. VIA 82c686a.) So set it to be safe)
504 */
Eric Biederman03acab62004-10-14 21:25:53 +0000505 base_lo = base & 0xffffffff;
506 base_hi = (base >> 32) & 0xffffffff;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000507 if (resource->flags & IORESOURCE_IO) {
Eric Biederman03acab62004-10-14 21:25:53 +0000508 base_lo |= PCI_BASE_ADDRESS_SPACE_IO;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000509 }
Eric Biederman03acab62004-10-14 21:25:53 +0000510 pci_write_config32(dev, resource->index, base_lo);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000511 if (resource->flags & IORESOURCE_PCI64) {
Eric Biederman03acab62004-10-14 21:25:53 +0000512 pci_write_config32(dev, resource->index + 4, base_hi);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000513 }
Eric Biedermanb78c1972004-10-14 20:54:17 +0000514 }
515 else if (resource->index == PCI_IO_BASE) {
Eric Biederman03acab62004-10-14 21:25:53 +0000516 /* set the IO ranges */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000517 compute_allocate_resource(&dev->link[0], resource,
Eric Biedermanb78c1972004-10-14 20:54:17 +0000518 IORESOURCE_IO, IORESOURCE_IO);
Eric Biederman03acab62004-10-14 21:25:53 +0000519 pci_write_config8(dev, PCI_IO_BASE, base >> 8);
520 pci_write_config16(dev, PCI_IO_BASE_UPPER16, base >> 16);
521 pci_write_config8(dev, PCI_IO_LIMIT, end >> 8);
522 pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, end >> 16);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000523 }
524 else if (resource->index == PCI_MEMORY_BASE) {
Eric Biederman03acab62004-10-14 21:25:53 +0000525 /* set the memory range */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000526 compute_allocate_resource(&dev->link[0], resource,
Eric Biedermanb78c1972004-10-14 20:54:17 +0000527 IORESOURCE_MEM | IORESOURCE_PREFETCH,
528 IORESOURCE_MEM);
Eric Biederman7a5416a2003-06-12 19:23:51 +0000529 pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16);
Eric Biederman03acab62004-10-14 21:25:53 +0000530 pci_write_config16(dev, PCI_MEMORY_LIMIT, end >> 16);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000531 }
532 else if (resource->index == PCI_PREF_MEMORY_BASE) {
Eric Biederman03acab62004-10-14 21:25:53 +0000533 /* set the prefetchable memory range */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000534 compute_allocate_resource(&dev->link[0], resource,
Eric Biedermanb78c1972004-10-14 20:54:17 +0000535 IORESOURCE_MEM | IORESOURCE_PREFETCH,
536 IORESOURCE_MEM | IORESOURCE_PREFETCH);
Eric Biederman03acab62004-10-14 21:25:53 +0000537 pci_write_config16(dev, PCI_PREF_MEMORY_BASE, base >> 16);
538 pci_write_config32(dev, PCI_PREF_BASE_UPPER32, base >> 32);
539 pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, end >> 16);
540 pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, end >> 32);
Eric Biedermanb78c1972004-10-14 20:54:17 +0000541 }
542 else {
Eric Biederman5cd81732004-03-11 15:01:31 +0000543 /* Don't let me think I stored the resource */
544 resource->flags &= ~IORESOURCE_STORED;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000545 printk_err("ERROR: invalid resource->index %x\n",
Eric Biedermanb78c1972004-10-14 20:54:17 +0000546 resource->index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000547 }
Eric Biederman03acab62004-10-14 21:25:53 +0000548 report_resource_stored(dev, resource, "");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000549 return;
550}
551
Eric Biederman5899fd82003-04-24 06:25:08 +0000552void pci_dev_set_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000553{
554 struct resource *resource, *last;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000555 unsigned link;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000556 uint8_t line;
557
558 last = &dev->resource[dev->resources];
Eric Biedermanb78c1972004-10-14 20:54:17 +0000559
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000560 for(resource = &dev->resource[0]; resource < last; resource++) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000561 pci_set_resource(dev, resource);
562 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000563 for(link = 0; link < dev->links; link++) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000564 struct bus *bus;
565 bus = &dev->link[link];
566 if (bus->children) {
567 assign_resources(bus);
568 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000569 }
570
571 /* set a default latency timer */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000572 pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000573
574 /* set a default secondary latency timer */
575 if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
Eric Biederman7a5416a2003-06-12 19:23:51 +0000576 pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000577 }
578
579 /* zero the irq settings */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000580 line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000581 if (line) {
Eric Biederman7a5416a2003-06-12 19:23:51 +0000582 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000583 }
584 /* set the cache line size, so far 64 bytes is good for everyone */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000585 pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000586}
587
Eric Biedermane9a271e32003-09-02 03:36:25 +0000588void pci_dev_enable_resources(struct device *dev)
589{
Eric Biedermana9e632c2004-11-18 22:38:08 +0000590 const struct pci_operations *ops;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000591 uint16_t command;
Eric Biederman03acab62004-10-14 21:25:53 +0000592
593 /* Set the subsystem vendor and device id for mainboard devices */
594 ops = ops_pci(dev);
Eric Biedermandbec2d42004-10-21 10:44:08 +0000595 if (dev->on_mainboard && ops && ops->set_subsystem) {
Eric Biederman03acab62004-10-14 21:25:53 +0000596 printk_debug("%s subsystem <- %02x/%02x\n",
597 dev_path(dev),
598 MAINBOARD_PCI_SUBSYSTEM_VENDOR_ID,
599 MAINBOARD_PCI_SUBSYSTEM_DEVICE_ID);
600 ops->set_subsystem(dev,
601 MAINBOARD_PCI_SUBSYSTEM_VENDOR_ID,
602 MAINBOARD_PCI_SUBSYSTEM_DEVICE_ID);
603 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000604 command = pci_read_config16(dev, PCI_COMMAND);
605 command |= dev->command;
606 printk_debug("%s cmd <- %02x\n", dev_path(dev), command);
607 pci_write_config16(dev, PCI_COMMAND, command);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000608}
609
610void pci_bus_enable_resources(struct device *dev)
611{
612 uint16_t ctrl;
Li-Ta Lo515f6c72005-01-11 22:48:54 +0000613 /* enable IO in command register if there is VGA card
614 * connected with (even it does not claim IO resource) */
615 if (dev->link[0].bridge_ctrl & PCI_BRIDGE_CTL_VGA)
616 dev->command |= PCI_COMMAND_IO;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000617 ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
618 ctrl |= dev->link[0].bridge_ctrl;
Eric Biederman5cd81732004-03-11 15:01:31 +0000619 ctrl |= (PCI_BRIDGE_CTL_PARITY + PCI_BRIDGE_CTL_SERR); /* error check */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000620 printk_debug("%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
621 pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
622
623 pci_dev_enable_resources(dev);
Eric Biedermandbec2d42004-10-21 10:44:08 +0000624
625 enable_childrens_resources(dev);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000626}
627
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000628void pci_bus_reset(struct bus *bus)
629{
630 unsigned ctl;
631 ctl = pci_read_config16(bus->dev, PCI_BRIDGE_CONTROL);
632 ctl |= PCI_BRIDGE_CTL_BUS_RESET;
633 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
634 mdelay(10);
635 ctl &= ~PCI_BRIDGE_CTL_BUS_RESET;
636 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
637 delay(1);
638}
639
Eric Biedermandbec2d42004-10-21 10:44:08 +0000640void pci_dev_set_subsystem(device_t dev, unsigned vendor, unsigned device)
Eric Biederman03acab62004-10-14 21:25:53 +0000641{
642 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
643 ((device & 0xffff) << 16) | (vendor & 0xffff));
644}
645
Torsten Duwe1f2f8002008-01-06 01:10:54 +0000646/** default handler: only runs the relevant pci bios. */
Li-Ta Lo883b8792005-01-10 23:16:22 +0000647void pci_dev_init(struct device *dev)
648{
Torsten Duwe1f2f8002008-01-06 01:10:54 +0000649#if CONFIG_PCI_ROM_RUN == 1 || CONFIG_VGA_ROM_RUN == 1
Li-Ta Lo883b8792005-01-10 23:16:22 +0000650 struct rom_header *rom, *ram;
651
Roman Kononov778a42b2007-04-06 18:34:39 +0000652#if CONFIG_PCI_ROM_RUN != 1
Torsten Duwe1f2f8002008-01-06 01:10:54 +0000653 /* We want to execute VGA option ROMs when CONFIG_VGA_ROM_RUN
Roman Kononov778a42b2007-04-06 18:34:39 +0000654 * is set but CONFIG_PCI_ROM_RUN is not. In this case we skip
655 * all other option ROM types.
656 */
Ronald Hoogenboom9b6b63e2008-02-28 23:10:38 +0000657 if ((dev->class>>8)!=PCI_CLASS_DISPLAY_VGA)
Roman Kononov778a42b2007-04-06 18:34:39 +0000658 return;
659#endif
660
Li-Ta Lo883b8792005-01-10 23:16:22 +0000661 rom = pci_rom_probe(dev);
662 if (rom == NULL)
663 return;
Roman Kononov778a42b2007-04-06 18:34:39 +0000664
Li-Ta Lo883b8792005-01-10 23:16:22 +0000665 ram = pci_rom_load(dev, rom);
Yinghai Lu9e4faef2005-01-14 22:04:49 +0000666 if (ram == NULL)
667 return;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000668
669 run_bios(dev, ram);
Roman Kononov778a42b2007-04-06 18:34:39 +0000670
671#if CONFIG_CONSOLE_VGA == 1
Torsten Duwe1f2f8002008-01-06 01:10:54 +0000672 /* vga_inited is a trigger of the VGA console code. */
Ronald Hoogenboom9b6b63e2008-02-28 23:10:38 +0000673 if ((dev->class>>8) == PCI_CLASS_DISPLAY_VGA) {
Torsten Duwe1f2f8002008-01-06 01:10:54 +0000674 extern int vga_inited;
675 vga_inited = 1;
676 }
677#endif /* CONFIG_CONSOLE_VGA */
678#endif /* CONFIG_PCI_ROM_RUN || CONFIG_VGA_ROM_RUN */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000679}
Li-Ta Lo883b8792005-01-10 23:16:22 +0000680
Li-Ta Loe5266692004-03-23 21:28:05 +0000681/** Default device operation for PCI devices */
Eric Biedermana9e632c2004-11-18 22:38:08 +0000682static struct pci_operations pci_dev_ops_pci = {
Eric Biederman03acab62004-10-14 21:25:53 +0000683 .set_subsystem = pci_dev_set_subsystem,
684};
685
Eric Biederman8ca8d762003-04-22 19:02:15 +0000686struct device_operations default_pci_ops_dev = {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000687 .read_resources = pci_dev_read_resources,
688 .set_resources = pci_dev_set_resources,
689 .enable_resources = pci_dev_enable_resources,
Li-Ta Lo883b8792005-01-10 23:16:22 +0000690 .init = pci_dev_init,
Li-Ta Loe5266692004-03-23 21:28:05 +0000691 .scan_bus = 0,
Eric Biederman03acab62004-10-14 21:25:53 +0000692 .enable = 0,
Eric Biedermana9e632c2004-11-18 22:38:08 +0000693 .ops_pci = &pci_dev_ops_pci,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000694};
Li-Ta Loe5266692004-03-23 21:28:05 +0000695
696/** Default device operations for PCI bridges */
Eric Biedermana9e632c2004-11-18 22:38:08 +0000697static struct pci_operations pci_bus_ops_pci = {
Eric Biederman03acab62004-10-14 21:25:53 +0000698 .set_subsystem = 0,
699};
Li-Ta Lo883b8792005-01-10 23:16:22 +0000700
Eric Biederman8ca8d762003-04-22 19:02:15 +0000701struct device_operations default_pci_ops_bus = {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000702 .read_resources = pci_bus_read_resources,
703 .set_resources = pci_dev_set_resources,
704 .enable_resources = pci_bus_enable_resources,
Li-Ta Loe5266692004-03-23 21:28:05 +0000705 .init = 0,
706 .scan_bus = pci_scan_bridge,
Eric Biederman03acab62004-10-14 21:25:53 +0000707 .enable = 0,
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000708 .reset_bus = pci_bus_reset,
Eric Biedermana9e632c2004-11-18 22:38:08 +0000709 .ops_pci = &pci_bus_ops_pci,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000710};
Li-Ta Loe5266692004-03-23 21:28:05 +0000711
712/**
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000713 * @brief Detect the type of downstream bridge
714 *
715 * This function is a heuristic to detect which type
716 * of bus is downstream of a pci to pci bridge. This
717 * functions by looking for various capability blocks
718 * to figure out the type of downstream bridge. PCI-X
719 * PCI-E, and Hypertransport all seem to have appropriate
720 * capabilities.
721 *
722 * When only a PCI-Express capability is found the type
723 * is examined to see which type of bridge we have.
724 *
725 * @param dev
726 *
727 * @return appropriate bridge operations
728 */
729static struct device_operations *get_pci_bridge_ops(device_t dev)
730{
731 unsigned pos;
732
733#if CONFIG_PCIX_PLUGIN_SUPPORT == 1
734 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
735 if (pos) {
736 printk_debug("%s subbordinate bus PCI-X\n", dev_path(dev));
737 return &default_pcix_ops_bus;
738 }
739#endif
740#if CONFIG_AGP_PLUGIN_SUPPORT == 1
741 /* How do I detect an PCI to AGP bridge? */
742#endif
743#if CONFIG_HYPERTRANSPORT_PLUGIN_SUPPORT == 1
744 pos = 0;
745 while((pos = pci_find_next_capability(dev, PCI_CAP_ID_HT, pos))) {
746 unsigned flags;
747 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
748 if ((flags >> 13) == 1) {
749 /* Host or Secondary Interface */
750 printk_debug("%s subbordinate bus Hypertransport\n",
751 dev_path(dev));
752 return &default_ht_ops_bus;
753 }
754 }
755#endif
756#if CONFIG_PCIEXP_PLUGIN_SUPPORT == 1
757 pos = pci_find_capability(dev, PCI_CAP_ID_PCIE);
758 if (pos) {
759 unsigned flags;
760 flags = pci_read_config16(dev, pos + PCI_EXP_FLAGS);
761 switch((flags & PCI_EXP_FLAGS_TYPE) >> 4) {
762 case PCI_EXP_TYPE_ROOT_PORT:
763 case PCI_EXP_TYPE_UPSTREAM:
764 case PCI_EXP_TYPE_DOWNSTREAM:
765 printk_debug("%s subbordinate bus PCI Express\n",
766 dev_path(dev));
767 return &default_pciexp_ops_bus;
768 case PCI_EXP_TYPE_PCI_BRIDGE:
769 printk_debug("%s subbordinate PCI\n",
770 dev_path(dev));
771 return &default_pci_ops_bus;
772 default:
773 break;
774 }
775 }
776#endif
777 return &default_pci_ops_bus;
778}
779
780/**
Li-Ta Loe5266692004-03-23 21:28:05 +0000781 * @brief Set up PCI device operation
782 *
783 *
784 * @param dev
785 *
786 * @see pci_drivers
787 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000788static void set_pci_ops(struct device *dev)
789{
790 struct pci_driver *driver;
791 if (dev->ops) {
792 return;
793 }
Li-Ta Loe5266692004-03-23 21:28:05 +0000794
Yinghai Lu5f9624d2006-10-04 22:56:21 +0000795 /* Look through the list of setup drivers and find one for
Eric Biedermanb78c1972004-10-14 20:54:17 +0000796 * this pci device
797 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000798 for(driver = &pci_drivers[0]; driver != &epci_drivers[0]; driver++) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000799 if ((driver->vendor == dev->vendor) &&
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000800 (driver->device == dev->device))
Eric Biedermanb78c1972004-10-14 20:54:17 +0000801 {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000802 dev->ops = driver->ops;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000803 printk_spew("%s [%04x/%04x] %sops\n",
Eric Biedermanb78c1972004-10-14 20:54:17 +0000804 dev_path(dev),
805 driver->vendor, driver->device,
806 (driver->ops->scan_bus?"bus ":""));
Eric Biederman5899fd82003-04-24 06:25:08 +0000807 return;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000808 }
809 }
Li-Ta Loe5266692004-03-23 21:28:05 +0000810
Eric Biederman8ca8d762003-04-22 19:02:15 +0000811 /* If I don't have a specific driver use the default operations */
812 switch(dev->hdr_type & 0x7f) { /* header type */
813 case PCI_HEADER_TYPE_NORMAL: /* standard header */
814 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
815 goto bad;
816 dev->ops = &default_pci_ops_dev;
817 break;
818 case PCI_HEADER_TYPE_BRIDGE:
819 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
820 goto bad;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000821 dev->ops = get_pci_bridge_ops(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000822 break;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000823#if CONFIG_CARDBUS_PLUGIN_SUPPORT == 1
824 case PCI_HEADER_TYPE_CARDBUS:
825 dev->ops = &default_cardbus_ops_bus;
826 break;
827#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +0000828 default:
829 bad:
Li-Ta Lo69c5a902004-04-29 20:08:54 +0000830 if (dev->enabled) {
Eric Biederman83b991a2003-10-11 06:20:25 +0000831 printk_err("%s [%04x/%04x/%06x] has unknown header "
Eric Biedermanb78c1972004-10-14 20:54:17 +0000832 "type %02x, ignoring.\n",
833 dev_path(dev),
834 dev->vendor, dev->device,
835 dev->class >> 8, dev->hdr_type);
Eric Biederman83b991a2003-10-11 06:20:25 +0000836 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000837 }
838 return;
839}
840
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000841
842
Eric Biederman8ca8d762003-04-22 19:02:15 +0000843/**
Eric Biederman03acab62004-10-14 21:25:53 +0000844 * @brief See if we have already allocated a device structure for a given devfn.
Li-Ta Loe5266692004-03-23 21:28:05 +0000845 *
846 * Given a linked list of PCI device structures and a devfn number, find the
Li-Ta Lo3a812852004-12-03 22:39:34 +0000847 * device structure correspond to the devfn, if present. This function also
848 * removes the device structure from the linked list.
Li-Ta Loe5266692004-03-23 21:28:05 +0000849 *
850 * @param list the device structure list
Eric Biederman8ca8d762003-04-22 19:02:15 +0000851 * @param devfn a device/function number
Li-Ta Loe5266692004-03-23 21:28:05 +0000852 *
Li-Ta Lo3a812852004-12-03 22:39:34 +0000853 * @return pointer to the device structure found or null of we have not
854 * allocated a device for this devfn yet.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000855 */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000856static struct device *pci_scan_get_dev(struct device **list, unsigned int devfn)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000857{
Eric Biedermanb78c1972004-10-14 20:54:17 +0000858 struct device *dev;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000859 dev = 0;
860 for(; *list; list = &(*list)->sibling) {
Eric Biedermanad1b35a2003-10-14 02:36:51 +0000861 if ((*list)->path.type != DEVICE_PATH_PCI) {
Li-Ta Loe5266692004-03-23 21:28:05 +0000862 printk_err("child %s not a pci device\n",
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000863 dev_path(*list));
Eric Biedermanad1b35a2003-10-14 02:36:51 +0000864 continue;
865 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000866 if ((*list)->path.u.pci.devfn == devfn) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000867 /* Unlink from the list */
868 dev = *list;
869 *list = (*list)->sibling;
870 dev->sibling = 0;
871 break;
872 }
873 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000874 /* Just like alloc_dev add the device to the list of device on the bus.
875 * When the list of devices was formed we removed all of the parents
876 * children, and now we are interleaving static and dynamic devices in
Li-Ta Lo3a812852004-12-03 22:39:34 +0000877 * order on the bus.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000878 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000879 if (dev) {
880 device_t child;
881 /* Find the last child of our parent */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000882 for(child = dev->bus->children; child && child->sibling; ) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000883 child = child->sibling;
884 }
885 /* Place the device on the list of children of it's parent. */
886 if (child) {
887 child->sibling = dev;
888 } else {
889 dev->bus->children = dev;
890 }
891 }
892
Eric Biederman8ca8d762003-04-22 19:02:15 +0000893 return dev;
894}
895
Eric Biedermanb78c1972004-10-14 20:54:17 +0000896/**
897 * @brief Scan a PCI bus.
Li-Ta Loe5266692004-03-23 21:28:05 +0000898 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000899 * Determine the existence of a given PCI device.
900 *
901 * @param bus pointer to the bus structure
902 * @param devfn to look at
903 *
904 * @return The device structure for hte device (if found)
905 * or the NULL if no device is found.
906 */
907device_t pci_probe_dev(device_t dev, struct bus *bus, unsigned devfn)
908{
909 uint32_t id, class;
910 uint8_t hdr_type;
911
912 /* Detect if a device is present */
913 if (!dev) {
914 struct device dummy;
915 dummy.bus = bus;
916 dummy.path.type = DEVICE_PATH_PCI;
917 dummy.path.u.pci.devfn = devfn;
918 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
919 /* Have we found somthing?
920 * Some broken boards return 0 if a slot is empty.
921 */
922 if ( (id == 0xffffffff) || (id == 0x00000000) ||
923 (id == 0x0000ffff) || (id == 0xffff0000))
924 {
925 printk_spew("PCI: devfn 0x%x, bad id 0x%x\n", devfn, id);
926 return NULL;
927 }
928 dev = alloc_dev(bus, &dummy.path);
929 }
930 else {
931 /* Enable/disable the device. Once we have
932 * found the device specific operations this
933 * operations we will disable the device with
934 * those as well.
935 *
936 * This is geared toward devices that have subfunctions
937 * that do not show up by default.
938 *
939 * If a device is a stuff option on the motherboard
940 * it may be absent and enable_dev must cope.
941 *
942 */
943 /* Run the magice enable sequence for the device */
944 if (dev->chip_ops && dev->chip_ops->enable_dev) {
945 dev->chip_ops->enable_dev(dev);
946 }
947 /* Now read the vendor and device id */
948 id = pci_read_config32(dev, PCI_VENDOR_ID);
949
950
951 /* If the device does not have a pci id disable it.
952 * Possibly this is because we have already disabled
953 * the device. But this also handles optional devices
954 * that may not always show up.
955 */
956 /* If the chain is fully enumerated quit */
957 if ( (id == 0xffffffff) || (id == 0x00000000) ||
958 (id == 0x0000ffff) || (id == 0xffff0000))
959 {
960 if (dev->enabled) {
961 printk_info("Disabling static device: %s\n",
962 dev_path(dev));
963 dev->enabled = 0;
964 }
965 return dev;
966 }
967 }
968 /* Read the rest of the pci configuration information */
969 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
970 class = pci_read_config32(dev, PCI_CLASS_REVISION);
971
972 /* Store the interesting information in the device structure */
973 dev->vendor = id & 0xffff;
974 dev->device = (id >> 16) & 0xffff;
975 dev->hdr_type = hdr_type;
976 /* class code, the upper 3 bytes of PCI_CLASS_REVISION */
977 dev->class = class >> 8;
978
979
980 /* Architectural/System devices always need to
981 * be bus masters.
982 */
983 if ((dev->class >> 16) == PCI_BASE_CLASS_SYSTEM) {
984 dev->command |= PCI_COMMAND_MASTER;
985 }
986 /* Look at the vendor and device id, or at least the
987 * header type and class and figure out which set of
988 * configuration methods to use. Unless we already
989 * have some pci ops.
990 */
991 set_pci_ops(dev);
992
993 /* Now run the magic enable/disable sequence for the device */
994 if (dev->ops && dev->ops->enable) {
995 dev->ops->enable(dev);
996 }
997
998
999 /* Display the device and error if we don't have some pci operations
1000 * for it.
1001 */
1002 printk_debug("%s [%04x/%04x] %s%s\n",
1003 dev_path(dev),
1004 dev->vendor, dev->device,
1005 dev->enabled?"enabled": "disabled",
1006 dev->ops?"" : " No operations"
1007 );
1008
1009 return dev;
1010}
1011
1012/**
1013 * @brief Scan a PCI bus.
1014 *
Li-Ta Loe5266692004-03-23 21:28:05 +00001015 * Determine the existence of devices and bridges on a PCI bus. If there are
1016 * bridges on the bus, recursively scan the buses behind the bridges.
1017 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001018 * This function is the default scan_bus() method for the root device
1019 * 'dev_root'.
1020 *
Eric Biedermane9a271e32003-09-02 03:36:25 +00001021 * @param bus pointer to the bus structure
1022 * @param min_devfn minimum devfn to look at in the scan usually 0x00
1023 * @param max_devfn maximum devfn to look at in the scan usually 0xff
Eric Biederman8ca8d762003-04-22 19:02:15 +00001024 * @param max current bus number
Li-Ta Loe5266692004-03-23 21:28:05 +00001025 *
Eric Biederman8ca8d762003-04-22 19:02:15 +00001026 * @return The maximum bus number found, after scanning all subordinate busses
1027 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001028unsigned int pci_scan_bus(struct bus *bus,
1029 unsigned min_devfn, unsigned max_devfn,
1030 unsigned int max)
Eric Biederman8ca8d762003-04-22 19:02:15 +00001031{
1032 unsigned int devfn;
Eric Biedermane9a271e32003-09-02 03:36:25 +00001033 device_t old_devices;
1034 device_t child;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001035
Yinghai Lu5f9624d2006-10-04 22:56:21 +00001036#if PCI_BUS_SEGN_BITS
1037 printk_debug("PCI: pci_scan_bus for bus %04x:%02x\n", bus->secondary >> 8, bus->secondary & 0xff);
1038#else
1039 printk_debug("PCI: pci_scan_bus for bus %02x\n", bus->secondary);
1040#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +00001041
1042 old_devices = bus->children;
1043 bus->children = 0;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001044
1045 post_code(0x24);
Li-Ta Lo9782f752004-05-05 21:15:42 +00001046 /* probe all devices/functions on this bus with some optimization for
Eric Biedermanb78c1972004-10-14 20:54:17 +00001047 * non-existence and single funcion devices
1048 */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001049 for (devfn = min_devfn; devfn <= max_devfn; devfn++) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001050 device_t dev;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001051
Eric Biederman03acab62004-10-14 21:25:53 +00001052 /* First thing setup the device structure */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001053 dev = pci_scan_get_dev(&old_devices, devfn);
Li-Ta Lo9782f752004-05-05 21:15:42 +00001054
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001055 /* See if a device is present and setup the device
1056 * structure.
Eric Biederman03acab62004-10-14 21:25:53 +00001057 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001058 dev = pci_probe_dev(dev, bus, devfn);
Eric Biederman03acab62004-10-14 21:25:53 +00001059
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001060 /* if this is not a multi function device,
1061 * or the device is not present don't waste
1062 * time probing another function.
1063 * Skip to next device.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001064 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001065 if ((PCI_FUNC(devfn) == 0x00) &&
1066 (!dev || (dev->enabled && ((dev->hdr_type & 0x80) != 0x80))))
1067 {
Eric Biederman8ca8d762003-04-22 19:02:15 +00001068 devfn += 0x07;
1069 }
1070 }
1071 post_code(0x25);
1072
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001073 /* Die if any leftover Static devices are are found.
1074 * There's probably a problem in the Config.lb.
1075 */
1076 if(old_devices) {
1077 device_t left;
1078 for(left = old_devices; left; left = left->sibling) {
Ronald G. Minniche800b912006-01-17 21:12:03 +00001079 printk_err("%s\n", dev_path(left));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001080 }
Stefan Reinauer7ce8c542005-12-02 21:52:30 +00001081 die("PCI: Left over static devices. Check your Config.lb\n");
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001082 }
1083
Eric Biedermanb78c1972004-10-14 20:54:17 +00001084 /* For all children that implement scan_bus (i.e. bridges)
1085 * scan the bus behind that child.
1086 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001087 for(child = bus->children; child; child = child->sibling) {
1088 max = scan_bus(child, max);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001089 }
Li-Ta Loe5266692004-03-23 21:28:05 +00001090
Eric Biederman8ca8d762003-04-22 19:02:15 +00001091 /*
1092 * We've scanned the bus and so we know all about what's on
1093 * the other side of any bridges that may be on this bus plus
1094 * any devices.
1095 *
1096 * Return how far we've got finding sub-buses.
1097 */
Yinghai Lu5f9624d2006-10-04 22:56:21 +00001098 printk_debug("PCI: pci_scan_bus returning with max=%03x\n", max);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001099 post_code(0x55);
1100 return max;
1101}
1102
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001103
Li-Ta Loe5266692004-03-23 21:28:05 +00001104/**
1105 * @brief Scan a PCI bridge and the buses behind the bridge.
1106 *
1107 * Determine the existence of buses behind the bridge. Set up the bridge
1108 * according to the result of the scan.
1109 *
1110 * This function is the default scan_bus() method for PCI bridge devices.
1111 *
1112 * @param dev pointer to the bridge device
1113 * @param max the highest bus number assgined up to now
1114 *
Eric Biederman8ca8d762003-04-22 19:02:15 +00001115 * @return The maximum bus number found, after scanning all subordinate busses
1116 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001117unsigned int do_pci_scan_bridge(struct device *dev, unsigned int max,
1118 unsigned int (*do_scan_bus)(struct bus *bus,
1119 unsigned min_devfn, unsigned max_devfn, unsigned int max))
Eric Biederman8ca8d762003-04-22 19:02:15 +00001120{
Eric Biedermane9a271e32003-09-02 03:36:25 +00001121 struct bus *bus;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001122 uint32_t buses;
1123 uint16_t cr;
Eric Biederman83b991a2003-10-11 06:20:25 +00001124
Li-Ta Lo3a812852004-12-03 22:39:34 +00001125 printk_spew("%s for %s\n", __func__, dev_path(dev));
1126
Eric Biedermane9a271e32003-09-02 03:36:25 +00001127 bus = &dev->link[0];
Eric Biedermana9e632c2004-11-18 22:38:08 +00001128 bus->dev = dev;
Eric Biedermane9a271e32003-09-02 03:36:25 +00001129 dev->links = 1;
1130
Eric Biederman8ca8d762003-04-22 19:02:15 +00001131 /* Set up the primary, secondary and subordinate bus numbers. We have
1132 * no idea how many buses are behind this bridge yet, so we set the
Eric Biedermanb78c1972004-10-14 20:54:17 +00001133 * subordinate bus number to 0xff for the moment.
1134 */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001135 bus->secondary = ++max;
1136 bus->subordinate = 0xff;
Li-Ta Loe5266692004-03-23 21:28:05 +00001137
Eric Biederman8ca8d762003-04-22 19:02:15 +00001138 /* Clear all status bits and turn off memory, I/O and master enables. */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001139 cr = pci_read_config16(dev, PCI_COMMAND);
1140 pci_write_config16(dev, PCI_COMMAND, 0x0000);
1141 pci_write_config16(dev, PCI_STATUS, 0xffff);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001142
Eric Biedermanb78c1972004-10-14 20:54:17 +00001143 /*
1144 * Read the existing primary/secondary/subordinate bus
1145 * number configuration.
1146 */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001147 buses = pci_read_config32(dev, PCI_PRIMARY_BUS);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001148
1149 /* Configure the bus numbers for this bridge: the configuration
1150 * transactions will not be propagated by the bridge if it is not
Eric Biedermanb78c1972004-10-14 20:54:17 +00001151 * correctly configured.
1152 */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001153 buses &= 0xff000000;
Eric Biedermane9a271e32003-09-02 03:36:25 +00001154 buses |= (((unsigned int) (dev->bus->secondary) << 0) |
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001155 ((unsigned int) (bus->secondary) << 8) |
1156 ((unsigned int) (bus->subordinate) << 16));
Eric Biedermane9a271e32003-09-02 03:36:25 +00001157 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
Li-Ta Lo3a812852004-12-03 22:39:34 +00001158
Eric Biedermanb78c1972004-10-14 20:54:17 +00001159 /* Now we can scan all subordinate buses
1160 * i.e. the bus behind the bridge.
1161 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001162 max = do_scan_bus(bus, 0x00, 0xff, max);
Li-Ta Lo3a812852004-12-03 22:39:34 +00001163
Eric Biederman8ca8d762003-04-22 19:02:15 +00001164 /* We know the number of buses behind this bridge. Set the subordinate
Eric Biedermanb78c1972004-10-14 20:54:17 +00001165 * bus number to its real value.
1166 */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001167 bus->subordinate = max;
1168 buses = (buses & 0xff00ffff) |
1169 ((unsigned int) (bus->subordinate) << 16);
Eric Biedermane9a271e32003-09-02 03:36:25 +00001170 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
1171 pci_write_config16(dev, PCI_COMMAND, cr);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001172
Eric Biedermanb78c1972004-10-14 20:54:17 +00001173 printk_spew("%s returns max %d\n", __func__, max);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001174 return max;
1175}
Li-Ta Loe5266692004-03-23 21:28:05 +00001176
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001177/**
1178 * @brief Scan a PCI bridge and the buses behind the bridge.
1179 *
1180 * Determine the existence of buses behind the bridge. Set up the bridge
1181 * according to the result of the scan.
1182 *
1183 * This function is the default scan_bus() method for PCI bridge devices.
1184 *
1185 * @param dev pointer to the bridge device
1186 * @param max the highest bus number assgined up to now
1187 *
1188 * @return The maximum bus number found, after scanning all subordinate busses
1189 */
1190unsigned int pci_scan_bridge(struct device *dev, unsigned int max)
1191{
1192 return do_pci_scan_bridge(dev, max, pci_scan_bus);
1193}
1194
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001195/*
1196 Tell the EISA int controller this int must be level triggered
1197 THIS IS A KLUDGE -- sorry, this needs to get cleaned up.
1198*/
Ronald G. Minnich88fb1a62006-06-22 04:37:27 +00001199void pci_level_irq(unsigned char intNum)
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001200{
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +00001201 unsigned short intBits = inb(0x4d0) | (((unsigned) inb(0x4d1)) << 8);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001202
Eric Biedermanb78c1972004-10-14 20:54:17 +00001203 printk_spew("%s: current ints are 0x%x\n", __func__, intBits);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001204 intBits |= (1 << intNum);
1205
Eric Biedermanb78c1972004-10-14 20:54:17 +00001206 printk_spew("%s: try to set ints 0x%x\n", __func__, intBits);
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +00001207
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001208 // Write new values
1209 outb((unsigned char) intBits, 0x4d0);
1210 outb((unsigned char) (intBits >> 8), 0x4d1);
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +00001211
Ronald G. Minnichb56ef072003-10-15 20:05:11 +00001212 /* this seems like an error but is not ... */
Ronald G. Minnich02fa3b22004-10-06 17:33:54 +00001213#if 1
Ronald G. Minnich2cf779d2006-09-18 22:50:51 +00001214 if (inb(0x4d0) != (intBits & 0xff)) {
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +00001215 printk_err("%s: lower order bits are wrong: want 0x%x, got 0x%x\n",
Ronald G. Minnich2cf779d2006-09-18 22:50:51 +00001216 __func__, intBits &0xff, inb(0x4d0));
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +00001217 }
Ronald G. Minnich2cf779d2006-09-18 22:50:51 +00001218 if (inb(0x4d1) != ((intBits >> 8) & 0xff)) {
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +00001219 printk_err("%s: lower order bits are wrong: want 0x%x, got 0x%x\n",
Ronald G. Minnich2cf779d2006-09-18 22:50:51 +00001220 __func__, (intBits>>8) &0xff, inb(0x4d1));
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +00001221 }
Ronald G. Minnichb56ef072003-10-15 20:05:11 +00001222#endif
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001223}
1224
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001225/*
1226 This function assigns IRQs for all functions contained within
1227 the indicated device address. If the device does not exist or does
1228 not require interrupts then this function has no effect.
1229
1230 This function should be called for each PCI slot in your system.
1231
1232 pIntAtoD is an array of IRQ #s that are assigned to PINTA through PINTD of
1233 this slot.
1234 The particular irq #s that are passed in depend on the routing inside
1235 your southbridge and on your motherboard.
1236
1237 -kevinh@ispiri.com
1238*/
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001239void pci_assign_irqs(unsigned bus, unsigned slot,
1240 const unsigned char pIntAtoD[4])
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001241{
1242 unsigned functNum;
1243 device_t pdev;
1244 unsigned char line;
1245 unsigned char irq;
1246 unsigned char readback;
1247
1248 /* Each slot may contain up to eight functions */
1249 for (functNum = 0; functNum < 8; functNum++) {
1250 pdev = dev_find_slot(bus, (slot << 3) + functNum);
1251
1252 if (pdev) {
1253 line = pci_read_config8(pdev, PCI_INTERRUPT_PIN);
1254
1255 // PCI spec says all other values are reserved
1256 if ((line >= 1) && (line <= 4)) {
1257 irq = pIntAtoD[line - 1];
1258
1259 printk_debug("Assigning IRQ %d to %d:%x.%d\n", \
1260 irq, bus, slot, functNum);
1261
1262 pci_write_config8(pdev, PCI_INTERRUPT_LINE,\
1263 pIntAtoD[line - 1]);
1264
1265 readback = pci_read_config8(pdev, PCI_INTERRUPT_LINE);
1266 printk_debug(" Readback = %d\n", readback);
1267
1268 // Change to level triggered
1269 pci_level_irq(pIntAtoD[line - 1]);
1270 }
1271 }
1272 }
1273}