blob: ac799c5fec459593e0bc735dcda547ecad296290 [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)
Patrick Georgi16cdbb22009-04-21 20:14:31 +000013 * Copyright (C) 2005-2009 coresystems GmbH
14 * (Written by Stefan Reinauer <stepan@coresystems.de> for coresystems GmbH)
Uwe Hermannb80dbf02007-04-22 19:08:13 +000015 */
16
17/*
Myles Watson29cc9ed2009-07-02 18:56:24 +000018 * PCI Bus Services, see include/linux/pci.h for further explanation.
Eric Biederman8ca8d762003-04-22 19:02:15 +000019 *
Myles Watson29cc9ed2009-07-02 18:56:24 +000020 * Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter,
21 * David Mosberger-Tang
Eric Biederman8ca8d762003-04-22 19:02:15 +000022 *
Myles Watson29cc9ed2009-07-02 18:56:24 +000023 * Copyright 1997 -- 1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
Eric Biederman8ca8d762003-04-22 19:02:15 +000024 */
25
26#include <console/console.h>
27#include <stdlib.h>
28#include <stdint.h>
29#include <bitops.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000030#include <string.h>
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +000031#include <arch/io.h>
Eric Biederman5899fd82003-04-24 06:25:08 +000032#include <device/device.h>
33#include <device/pci.h>
34#include <device/pci_ids.h>
Eric Biederman03acab62004-10-14 21:25:53 +000035#include <delay.h>
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000036#if CONFIG_HYPERTRANSPORT_PLUGIN_SUPPORT == 1
37#include <device/hypertransport.h>
38#endif
39#if CONFIG_PCIX_PLUGIN_SUPPORT == 1
40#include <device/pcix.h>
41#endif
42#if CONFIG_PCIEXP_PLUGIN_SUPPORT == 1
43#include <device/pciexp.h>
44#endif
Stefan Reinauerec75a572009-03-16 15:27:00 +000045#if CONFIG_AGP_PLUGIN_SUPPORT == 1
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000046#include <device/agp.h>
47#endif
48#if CONFIG_CARDBUS_PLUGIN_SUPPORT == 1
49#include <device/cardbus.h>
50#endif
Stefan Reinauer4d933dd2009-07-21 21:36:41 +000051#define CONFIG_PC80_SYSTEM 1
52#if CONFIG_PC80_SYSTEM == 1
53#include <pc80/i8259.h>
54#endif
Eric Biederman03acab62004-10-14 21:25:53 +000055
Myles Watson29cc9ed2009-07-02 18:56:24 +000056u8 pci_moving_config8(struct device *dev, unsigned int reg)
Eric Biederman03acab62004-10-14 21:25:53 +000057{
Myles Watson29cc9ed2009-07-02 18:56:24 +000058 u8 value, ones, zeroes;
Eric Biederman03acab62004-10-14 21:25:53 +000059 value = pci_read_config8(dev, reg);
Myles Watson032a9652009-05-11 22:24:53 +000060
Eric Biederman03acab62004-10-14 21:25:53 +000061 pci_write_config8(dev, reg, 0xff);
62 ones = pci_read_config8(dev, reg);
63
64 pci_write_config8(dev, reg, 0x00);
65 zeroes = pci_read_config8(dev, reg);
66
67 pci_write_config8(dev, reg, value);
68
69 return ones ^ zeroes;
70}
Li-Ta Lo9a5b4962004-12-23 21:48:01 +000071
Myles Watson29cc9ed2009-07-02 18:56:24 +000072u16 pci_moving_config16(struct device * dev, unsigned int reg)
Eric Biederman03acab62004-10-14 21:25:53 +000073{
Myles Watson29cc9ed2009-07-02 18:56:24 +000074 u16 value, ones, zeroes;
Eric Biederman03acab62004-10-14 21:25:53 +000075 value = pci_read_config16(dev, reg);
Myles Watson032a9652009-05-11 22:24:53 +000076
Eric Biederman03acab62004-10-14 21:25:53 +000077 pci_write_config16(dev, reg, 0xffff);
78 ones = pci_read_config16(dev, reg);
79
80 pci_write_config16(dev, reg, 0x0000);
81 zeroes = pci_read_config16(dev, reg);
82
83 pci_write_config16(dev, reg, value);
84
85 return ones ^ zeroes;
86}
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +000087
Myles Watson29cc9ed2009-07-02 18:56:24 +000088u32 pci_moving_config32(struct device * dev, unsigned int reg)
Eric Biederman03acab62004-10-14 21:25:53 +000089{
Myles Watson29cc9ed2009-07-02 18:56:24 +000090 u32 value, ones, zeroes;
Eric Biederman03acab62004-10-14 21:25:53 +000091 value = pci_read_config32(dev, reg);
Myles Watson032a9652009-05-11 22:24:53 +000092
Eric Biederman03acab62004-10-14 21:25:53 +000093 pci_write_config32(dev, reg, 0xffffffff);
94 ones = pci_read_config32(dev, reg);
95
96 pci_write_config32(dev, reg, 0x00000000);
97 zeroes = pci_read_config32(dev, reg);
98
99 pci_write_config32(dev, reg, value);
100
101 return ones ^ zeroes;
102}
103
Myles Watson29cc9ed2009-07-02 18:56:24 +0000104/**
105 * Given a device, a capability type, and a last position, return the next
106 * matching capability. Always start at the head of the list.
107 *
108 * @param dev Pointer to the device structure.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000109 * @param cap PCI_CAP_LIST_ID of the PCI capability we're looking for.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000110 * @param last Location of the PCI capability register to start from.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000111 * @return The next matching capability.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000112 */
113unsigned pci_find_next_capability(struct device *dev, unsigned cap,
114 unsigned last)
Eric Biederman03acab62004-10-14 21:25:53 +0000115{
Stefan Reinauer4d933dd2009-07-21 21:36:41 +0000116 unsigned pos = 0;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000117 unsigned status;
118 unsigned reps = 48;
Stefan Reinauer4d933dd2009-07-21 21:36:41 +0000119
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000120 status = pci_read_config16(dev, PCI_STATUS);
121 if (!(status & PCI_STATUS_CAP_LIST)) {
122 return 0;
123 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000124 switch (dev->hdr_type & 0x7f) {
Eric Biederman03acab62004-10-14 21:25:53 +0000125 case PCI_HEADER_TYPE_NORMAL:
126 case PCI_HEADER_TYPE_BRIDGE:
127 pos = PCI_CAPABILITY_LIST;
128 break;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000129 case PCI_HEADER_TYPE_CARDBUS:
130 pos = PCI_CB_CAPABILITY_LIST;
131 break;
132 default:
133 return 0;
Eric Biederman03acab62004-10-14 21:25:53 +0000134 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000135 pos = pci_read_config8(dev, pos);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000136 while (reps-- && (pos >= 0x40)) { /* Loop through the linked list. */
Eric Biederman03acab62004-10-14 21:25:53 +0000137 int this_cap;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000138 pos &= ~3;
Eric Biederman03acab62004-10-14 21:25:53 +0000139 this_cap = pci_read_config8(dev, pos + PCI_CAP_LIST_ID);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000140 printk(BIOS_SPEW, "Capability: type 0x%02x @ 0x%02x\n", this_cap,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000141 pos);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000142 if (this_cap == 0xff) {
143 break;
144 }
145 if (!last && (this_cap == cap)) {
Eric Biederman03acab62004-10-14 21:25:53 +0000146 return pos;
147 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000148 if (last == pos) {
149 last = 0;
150 }
151 pos = pci_read_config8(dev, pos + PCI_CAP_LIST_NEXT);
Eric Biederman03acab62004-10-14 21:25:53 +0000152 }
153 return 0;
154}
155
Myles Watson29cc9ed2009-07-02 18:56:24 +0000156/**
157 * Given a device, and a capability type, return the next matching
158 * capability. Always start at the head of the list.
159 *
160 * @param dev Pointer to the device structure.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000161 * @param cap PCI_CAP_LIST_ID of the PCI capability we're looking for.
162 * @return The next matching capability.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000163 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000164unsigned pci_find_capability(device_t dev, unsigned cap)
165{
166 return pci_find_next_capability(dev, cap, 0);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000167}
168
Myles Watson29cc9ed2009-07-02 18:56:24 +0000169/**
170 * Given a device and register, read the size of the BAR for that register.
171 *
172 * @param dev Pointer to the device structure.
173 * @param index Address of the PCI configuration register.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000174 * @return TODO
Eric Biederman8ca8d762003-04-22 19:02:15 +0000175 */
Eric Biederman03acab62004-10-14 21:25:53 +0000176struct resource *pci_get_resource(struct device *dev, unsigned long index)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000177{
Eric Biederman5cd81732004-03-11 15:01:31 +0000178 struct resource *resource;
Eric Biederman03acab62004-10-14 21:25:53 +0000179 unsigned long value, attr;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000180 resource_t moving, limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000181
Myles Watson29cc9ed2009-07-02 18:56:24 +0000182 /* Initialize the resources to nothing. */
Eric Biederman03acab62004-10-14 21:25:53 +0000183 resource = new_resource(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000184
Myles Watson29cc9ed2009-07-02 18:56:24 +0000185 /* Get the initial value. */
Eric Biederman03acab62004-10-14 21:25:53 +0000186 value = pci_read_config32(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000187
Myles Watson29cc9ed2009-07-02 18:56:24 +0000188 /* See which bits move. */
Eric Biederman03acab62004-10-14 21:25:53 +0000189 moving = pci_moving_config32(dev, index);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000190
Myles Watson29cc9ed2009-07-02 18:56:24 +0000191 /* Initialize attr to the bits that do not move. */
Eric Biederman03acab62004-10-14 21:25:53 +0000192 attr = value & ~moving;
193
Myles Watson29cc9ed2009-07-02 18:56:24 +0000194 /* If it is a 64bit resource look at the high half as well. */
Eric Biederman03acab62004-10-14 21:25:53 +0000195 if (((attr & PCI_BASE_ADDRESS_SPACE_IO) == 0) &&
Myles Watson29cc9ed2009-07-02 18:56:24 +0000196 ((attr & PCI_BASE_ADDRESS_MEM_LIMIT_MASK) ==
197 PCI_BASE_ADDRESS_MEM_LIMIT_64)) {
198 /* Find the high bits that move. */
199 moving |=
200 ((resource_t) pci_moving_config32(dev, index + 4)) << 32;
Eric Biederman03acab62004-10-14 21:25:53 +0000201 }
Myles Watson032a9652009-05-11 22:24:53 +0000202 /* Find the resource constraints.
Eric Biederman03acab62004-10-14 21:25:53 +0000203 * Start by finding the bits that move. From there:
204 * - Size is the least significant bit of the bits that move.
205 * - Limit is all of the bits that move plus all of the lower bits.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000206 * See PCI Spec 6.2.5.1.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000207 */
Eric Biederman03acab62004-10-14 21:25:53 +0000208 limit = 0;
209 if (moving) {
210 resource->size = 1;
211 resource->align = resource->gran = 0;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000212 while (!(moving & resource->size)) {
Eric Biederman03acab62004-10-14 21:25:53 +0000213 resource->size <<= 1;
214 resource->align += 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000215 resource->gran += 1;
Eric Biederman03acab62004-10-14 21:25:53 +0000216 }
217 resource->limit = limit = moving | (resource->size - 1);
218 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000219
220 /* Some broken hardware has read-only registers that do not
Eric Biederman03acab62004-10-14 21:25:53 +0000221 * really size correctly.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000222 * Example: the Acer M7229 has BARs 1-4 normally read-only.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000223 * so BAR1 at offset 0x10 reads 0x1f1. If you size that register
Myles Watson032a9652009-05-11 22:24:53 +0000224 * by writing 0xffffffff to it, it will read back as 0x1f1 -- a
225 * violation of the spec.
Eric Biederman03acab62004-10-14 21:25:53 +0000226 * We catch this case and ignore it by observing which bits move,
227 * This also catches the common case unimplemented registers
228 * that always read back as 0.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000229 */
Eric Biederman03acab62004-10-14 21:25:53 +0000230 if (moving == 0) {
231 if (value != 0) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000232 printk(BIOS_DEBUG, "%s register %02lx(%08lx), read-only ignoring it\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000233 dev_path(dev), index, value);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000234 }
235 resource->flags = 0;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000236 } else if (attr & PCI_BASE_ADDRESS_SPACE_IO) {
237 /* An I/O mapped base address. */
Eric Biederman03acab62004-10-14 21:25:53 +0000238 attr &= PCI_BASE_ADDRESS_IO_ATTR_MASK;
Eric Biederman5cd81732004-03-11 15:01:31 +0000239 resource->flags |= IORESOURCE_IO;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000240 /* I don't want to deal with 32bit I/O resources. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000241 resource->limit = 0xffff;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000242 } else {
243 /* A Memory mapped base address. */
Eric Biederman03acab62004-10-14 21:25:53 +0000244 attr &= PCI_BASE_ADDRESS_MEM_ATTR_MASK;
Eric Biederman5cd81732004-03-11 15:01:31 +0000245 resource->flags |= IORESOURCE_MEM;
Eric Biederman03acab62004-10-14 21:25:53 +0000246 if (attr & PCI_BASE_ADDRESS_MEM_PREFETCH) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000247 resource->flags |= IORESOURCE_PREFETCH;
248 }
Eric Biederman03acab62004-10-14 21:25:53 +0000249 attr &= PCI_BASE_ADDRESS_MEM_LIMIT_MASK;
250 if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_32) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000251 /* 32bit limit. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000252 resource->limit = 0xffffffffUL;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000253 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_1M) {
254 /* 1MB limit. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000255 resource->limit = 0x000fffffUL;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000256 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_64) {
257 /* 64bit limit. */
Eric Biederman03acab62004-10-14 21:25:53 +0000258 resource->limit = 0xffffffffffffffffULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000259 resource->flags |= IORESOURCE_PCI64;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000260 } else {
261 /* Invalid value. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000262 printk(BIOS_ERR, "Broken BAR with value %lx\n", attr);
263 printk(BIOS_ERR, " on dev %s at index %02lx\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000264 dev_path(dev), index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000265 resource->flags = 0;
266 }
267 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000268 /* Don't let the limit exceed which bits can move. */
Eric Biederman03acab62004-10-14 21:25:53 +0000269 if (resource->limit > limit) {
270 resource->limit = limit;
271 }
Eric Biederman03acab62004-10-14 21:25:53 +0000272
Eric Biederman5cd81732004-03-11 15:01:31 +0000273 return resource;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000274}
275
Myles Watson29cc9ed2009-07-02 18:56:24 +0000276/**
277 * Given a device and an index, read the size of the BAR for that register.
278 *
279 * @param dev Pointer to the device structure.
280 * @param index Address of the PCI configuration register.
281 */
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000282static void pci_get_rom_resource(struct device *dev, unsigned long index)
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000283{
284 struct resource *resource;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000285 unsigned long value;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000286 resource_t moving;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000287
Myles Watson29cc9ed2009-07-02 18:56:24 +0000288 /* Initialize the resources to nothing. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000289 resource = new_resource(dev, index);
290
Myles Watson29cc9ed2009-07-02 18:56:24 +0000291 /* Get the initial value. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000292 value = pci_read_config32(dev, index);
293
Myles Watson29cc9ed2009-07-02 18:56:24 +0000294 /* See which bits move. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000295 moving = pci_moving_config32(dev, index);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000296
297 /* Clear the Enable bit. */
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000298 moving = moving & ~PCI_ROM_ADDRESS_ENABLE;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000299
Myles Watson032a9652009-05-11 22:24:53 +0000300 /* Find the resource constraints.
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000301 * Start by finding the bits that move. From there:
302 * - Size is the least significant bit of the bits that move.
303 * - Limit is all of the bits that move plus all of the lower bits.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000304 * See PCI Spec 6.2.5.1.
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000305 */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000306 if (moving) {
307 resource->size = 1;
308 resource->align = resource->gran = 0;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000309 while (!(moving & resource->size)) {
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000310 resource->size <<= 1;
311 resource->align += 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000312 resource->gran += 1;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000313 }
Patrick Georgi16cdbb22009-04-21 20:14:31 +0000314 resource->limit = moving | (resource->size - 1);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000315 resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY;
316 } else {
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000317 if (value != 0) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000318 printk(BIOS_DEBUG, "%s register %02lx(%08lx), read-only ignoring it\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000319 dev_path(dev), index, value);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000320 }
321 resource->flags = 0;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000322 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000323 compact_resources(dev);
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000324}
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000325
Myles Watson29cc9ed2009-07-02 18:56:24 +0000326/**
327 * Read the base address registers for a given device.
328 *
329 * @param dev Pointer to the dev structure.
330 * @param howmany How many registers to read (6 for device, 2 for bridge).
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000331 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000332static void pci_read_bases(struct device *dev, unsigned int howmany)
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000333{
334 unsigned long index;
335
Myles Watson29cc9ed2009-07-02 18:56:24 +0000336 for (index = PCI_BASE_ADDRESS_0;
337 (index < PCI_BASE_ADDRESS_0 + (howmany << 2));) {
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000338 struct resource *resource;
339 resource = pci_get_resource(dev, index);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000340 index += (resource->flags & IORESOURCE_PCI64) ? 8 : 4;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000341 }
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000342
343 compact_resources(dev);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000344}
345
Myles Watson29cc9ed2009-07-02 18:56:24 +0000346static void pci_record_bridge_resource(struct device *dev, resource_t moving,
347 unsigned index, unsigned long type)
Eric Biederman03acab62004-10-14 21:25:53 +0000348{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000349 /* Initialize the constraints on the current bus. */
Eric Biederman03acab62004-10-14 21:25:53 +0000350 struct resource *resource;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000351 resource = NULL;
Eric Biederman03acab62004-10-14 21:25:53 +0000352 if (moving) {
353 unsigned long gran;
354 resource_t step;
355 resource = new_resource(dev, index);
356 resource->size = 0;
357 gran = 0;
358 step = 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000359 while ((moving & step) == 0) {
Eric Biederman03acab62004-10-14 21:25:53 +0000360 gran += 1;
361 step <<= 1;
362 }
363 resource->gran = gran;
364 resource->align = gran;
365 resource->limit = moving | (step - 1);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000366 resource->flags = type | IORESOURCE_PCI_BRIDGE |
367 IORESOURCE_BRIDGE;
Eric Biederman03acab62004-10-14 21:25:53 +0000368 }
369 return;
370}
371
Eric Biederman8ca8d762003-04-22 19:02:15 +0000372static void pci_bridge_read_bases(struct device *dev)
373{
Eric Biederman03acab62004-10-14 21:25:53 +0000374 resource_t moving_base, moving_limit, moving;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000375
Myles Watson29cc9ed2009-07-02 18:56:24 +0000376 /* See if the bridge I/O resources are implemented. */
377 moving_base = ((u32) pci_moving_config8(dev, PCI_IO_BASE)) << 8;
378 moving_base |=
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000379 ((u32) pci_moving_config16(dev, PCI_IO_BASE_UPPER16)) << 16;
Eric Biederman03acab62004-10-14 21:25:53 +0000380
Myles Watson29cc9ed2009-07-02 18:56:24 +0000381 moving_limit = ((u32) pci_moving_config8(dev, PCI_IO_LIMIT)) << 8;
382 moving_limit |=
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000383 ((u32) pci_moving_config16(dev, PCI_IO_LIMIT_UPPER16)) << 16;
Eric Biederman03acab62004-10-14 21:25:53 +0000384
385 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000386
Myles Watson29cc9ed2009-07-02 18:56:24 +0000387 /* Initialize the I/O space constraints on the current bus. */
388 pci_record_bridge_resource(dev, moving, PCI_IO_BASE, IORESOURCE_IO);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000389
Myles Watson29cc9ed2009-07-02 18:56:24 +0000390 /* See if the bridge prefmem resources are implemented. */
391 moving_base =
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000392 ((resource_t) pci_moving_config16(dev, PCI_PREF_MEMORY_BASE)) << 16;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000393 moving_base |=
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000394 ((resource_t) pci_moving_config32(dev, PCI_PREF_BASE_UPPER32)) << 32;
Eric Biederman03acab62004-10-14 21:25:53 +0000395
Myles Watson29cc9ed2009-07-02 18:56:24 +0000396 moving_limit =
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000397 ((resource_t) pci_moving_config16(dev, PCI_PREF_MEMORY_LIMIT)) << 16;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000398 moving_limit |=
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000399 ((resource_t) pci_moving_config32(dev, PCI_PREF_LIMIT_UPPER32)) << 32;
Myles Watson032a9652009-05-11 22:24:53 +0000400
Eric Biederman03acab62004-10-14 21:25:53 +0000401 moving = moving_base & moving_limit;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000402 /* Initialize the prefetchable memory constraints on the current bus. */
403 pci_record_bridge_resource(dev, moving, PCI_PREF_MEMORY_BASE,
404 IORESOURCE_MEM | IORESOURCE_PREFETCH);
Myles Watson032a9652009-05-11 22:24:53 +0000405
Myles Watson29cc9ed2009-07-02 18:56:24 +0000406 /* See if the bridge mem resources are implemented. */
407 moving_base = ((u32) pci_moving_config16(dev, PCI_MEMORY_BASE)) << 16;
408 moving_limit = ((u32) pci_moving_config16(dev, PCI_MEMORY_LIMIT)) << 16;
Eric Biederman03acab62004-10-14 21:25:53 +0000409
410 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000411
Myles Watson29cc9ed2009-07-02 18:56:24 +0000412 /* Initialize the memory resources on the current bus. */
413 pci_record_bridge_resource(dev, moving, PCI_MEMORY_BASE,
414 IORESOURCE_MEM);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000415
Eric Biederman5cd81732004-03-11 15:01:31 +0000416 compact_resources(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000417}
418
Eric Biederman5899fd82003-04-24 06:25:08 +0000419void pci_dev_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000420{
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000421 pci_read_bases(dev, 6);
422 pci_get_rom_resource(dev, PCI_ROM_ADDRESS);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000423}
424
Eric Biederman5899fd82003-04-24 06:25:08 +0000425void pci_bus_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000426{
Eric Biederman8ca8d762003-04-22 19:02:15 +0000427 pci_bridge_read_bases(dev);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000428 pci_read_bases(dev, 2);
429 pci_get_rom_resource(dev, PCI_ROM_ADDRESS1);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000430}
431
Myles Watson29cc9ed2009-07-02 18:56:24 +0000432void pci_domain_read_resources(struct device *dev)
433{
434 struct resource *res;
435
436 /* Initialize the system-wide I/O space constraints. */
437 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
438 res->limit = 0xffffUL;
439 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
440 IORESOURCE_ASSIGNED;
441
442 /* Initialize the system-wide memory resources constraints. */
443 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
444 res->limit = 0xffffffffULL;
445 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
446 IORESOURCE_ASSIGNED;
447}
448
Eric Biederman8ca8d762003-04-22 19:02:15 +0000449static void pci_set_resource(struct device *dev, struct resource *resource)
450{
Eric Biederman03acab62004-10-14 21:25:53 +0000451 resource_t base, end;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000452
Myles Watson29cc9ed2009-07-02 18:56:24 +0000453 /* Make certain the resource has actually been assigned a value. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000454 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000455 printk(BIOS_ERR, "ERROR: %s %02lx %s size: 0x%010llx not assigned\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000456 dev_path(dev), resource->index,
457 resource_type(resource), resource->size);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000458 return;
459 }
460
Myles Watsoneb81a5b2009-11-05 20:06:19 +0000461 /* If this resource is fixed don't worry about it. */
462 if (resource->flags & IORESOURCE_FIXED) {
463 return;
464 }
465
Myles Watson29cc9ed2009-07-02 18:56:24 +0000466 /* If I have already stored this resource don't worry about it. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000467 if (resource->flags & IORESOURCE_STORED) {
468 return;
469 }
470
Myles Watson29cc9ed2009-07-02 18:56:24 +0000471 /* If the resource is subtractive don't worry about it. */
Eric Biederman03acab62004-10-14 21:25:53 +0000472 if (resource->flags & IORESOURCE_SUBTRACTIVE) {
473 return;
474 }
475
Myles Watson29cc9ed2009-07-02 18:56:24 +0000476 /* Only handle PCI memory and I/O resources for now. */
477 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
Eric Biederman8ca8d762003-04-22 19:02:15 +0000478 return;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000479
Myles Watson29cc9ed2009-07-02 18:56:24 +0000480 /* Enable the resources in the command register. */
Eric Biederman03acab62004-10-14 21:25:53 +0000481 if (resource->size) {
482 if (resource->flags & IORESOURCE_MEM) {
483 dev->command |= PCI_COMMAND_MEMORY;
484 }
485 if (resource->flags & IORESOURCE_IO) {
486 dev->command |= PCI_COMMAND_IO;
487 }
488 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
489 dev->command |= PCI_COMMAND_MASTER;
490 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000491 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000492 /* Get the base address. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000493 base = resource->base;
Eric Biederman5cd81732004-03-11 15:01:31 +0000494
Myles Watson29cc9ed2009-07-02 18:56:24 +0000495 /* Get the end. */
Eric Biederman03acab62004-10-14 21:25:53 +0000496 end = resource_end(resource);
Myles Watson032a9652009-05-11 22:24:53 +0000497
Myles Watson29cc9ed2009-07-02 18:56:24 +0000498 /* Now store the resource. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000499 resource->flags |= IORESOURCE_STORED;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000500
501 /* PCI Bridges have no enable bit. They are disabled if the base of
502 * the range is greater than the limit. If the size is zero, disable
503 * by setting the base = limit and end = limit - 2^gran.
504 */
505 if (resource->size == 0 && (resource->flags & IORESOURCE_PCI_BRIDGE)) {
506 base = resource->limit;
507 end = resource->limit - (1 << resource->gran);
508 resource->base = base;
509 }
510
Eric Biederman8ca8d762003-04-22 19:02:15 +0000511 if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) {
Eric Biederman03acab62004-10-14 21:25:53 +0000512 unsigned long base_lo, base_hi;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000513 /* Some chipsets allow us to set/clear the I/O bit
514 * (e.g. VIA 82c686a). So set it to be safe.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000515 */
Eric Biederman03acab62004-10-14 21:25:53 +0000516 base_lo = base & 0xffffffff;
517 base_hi = (base >> 32) & 0xffffffff;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000518 if (resource->flags & IORESOURCE_IO) {
Eric Biederman03acab62004-10-14 21:25:53 +0000519 base_lo |= PCI_BASE_ADDRESS_SPACE_IO;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000520 }
Eric Biederman03acab62004-10-14 21:25:53 +0000521 pci_write_config32(dev, resource->index, base_lo);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000522 if (resource->flags & IORESOURCE_PCI64) {
Eric Biederman03acab62004-10-14 21:25:53 +0000523 pci_write_config32(dev, resource->index + 4, base_hi);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000524 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000525 } else if (resource->index == PCI_IO_BASE) {
526 /* Set the I/O ranges. */
527 pci_write_config8(dev, PCI_IO_BASE, base >> 8);
Eric Biederman03acab62004-10-14 21:25:53 +0000528 pci_write_config16(dev, PCI_IO_BASE_UPPER16, base >> 16);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000529 pci_write_config8(dev, PCI_IO_LIMIT, end >> 8);
Eric Biederman03acab62004-10-14 21:25:53 +0000530 pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, end >> 16);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000531 } else if (resource->index == PCI_MEMORY_BASE) {
532 /* Set the memory range. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000533 pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16);
Eric Biederman03acab62004-10-14 21:25:53 +0000534 pci_write_config16(dev, PCI_MEMORY_LIMIT, end >> 16);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000535 } else if (resource->index == PCI_PREF_MEMORY_BASE) {
536 /* Set the prefetchable memory range. */
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);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000541 } else {
542 /* Don't let me think I stored the resource. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000543 resource->flags &= ~IORESOURCE_STORED;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000544 printk(BIOS_ERR, "ERROR: invalid resource->index %lx\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000545 resource->index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000546 }
Eric Biederman03acab62004-10-14 21:25:53 +0000547 report_resource_stored(dev, resource, "");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000548 return;
549}
550
Eric Biederman5899fd82003-04-24 06:25:08 +0000551void pci_dev_set_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000552{
Myles Watsonc25cc112010-05-21 14:33:48 +0000553 struct resource *res;
Myles Watson894a3472010-06-09 22:41:35 +0000554 struct bus *bus;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000555 u8 line;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000556
Myles Watsonc25cc112010-05-21 14:33:48 +0000557 for (res = dev->resource_list; res; res = res->next) {
558 pci_set_resource(dev, res);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000559 }
Myles Watson894a3472010-06-09 22:41:35 +0000560 for (bus = dev->link_list; bus; bus = bus->next) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000561 if (bus->children) {
562 assign_resources(bus);
563 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000564 }
565
Myles Watson29cc9ed2009-07-02 18:56:24 +0000566 /* Set a default latency timer. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000567 pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000568
Myles Watson29cc9ed2009-07-02 18:56:24 +0000569 /* Set a default secondary latency timer. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000570 if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
Eric Biederman7a5416a2003-06-12 19:23:51 +0000571 pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000572 }
573
Myles Watson29cc9ed2009-07-02 18:56:24 +0000574 /* Zero the IRQ settings. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000575 line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000576 if (line) {
Eric Biederman7a5416a2003-06-12 19:23:51 +0000577 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000578 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000579 /* Set the cache line size, so far 64 bytes is good for everyone. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000580 pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000581}
582
Eric Biedermane9a271e32003-09-02 03:36:25 +0000583void pci_dev_enable_resources(struct device *dev)
584{
Eric Biedermana9e632c2004-11-18 22:38:08 +0000585 const struct pci_operations *ops;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000586 u16 command;
Eric Biederman03acab62004-10-14 21:25:53 +0000587
Myles Watson29cc9ed2009-07-02 18:56:24 +0000588 /* Set the subsystem vendor and device id for mainboard devices. */
Eric Biederman03acab62004-10-14 21:25:53 +0000589 ops = ops_pci(dev);
Eric Biedermandbec2d42004-10-21 10:44:08 +0000590 if (dev->on_mainboard && ops && ops->set_subsystem) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000591 printk(BIOS_DEBUG, "%s subsystem <- %02x/%02x\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000592 dev_path(dev),
593 CONFIG_MAINBOARD_PCI_SUBSYSTEM_VENDOR_ID,
594 CONFIG_MAINBOARD_PCI_SUBSYSTEM_DEVICE_ID);
Myles Watson032a9652009-05-11 22:24:53 +0000595 ops->set_subsystem(dev,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000596 CONFIG_MAINBOARD_PCI_SUBSYSTEM_VENDOR_ID,
597 CONFIG_MAINBOARD_PCI_SUBSYSTEM_DEVICE_ID);
Eric Biederman03acab62004-10-14 21:25:53 +0000598 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000599 command = pci_read_config16(dev, PCI_COMMAND);
600 command |= dev->command;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000601 /* v3 has
602 * command |= (PCI_COMMAND_PARITY + PCI_COMMAND_SERR); // Error check.
603 */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000604 printk(BIOS_DEBUG, "%s cmd <- %02x\n", dev_path(dev), command);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000605 pci_write_config16(dev, PCI_COMMAND, command);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000606}
607
608void pci_bus_enable_resources(struct device *dev)
609{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000610 u16 ctrl;
611
612 /* Enable I/O in command register if there is VGA card
613 * connected with (even it does not claim I/O resource).
614 */
Myles Watson894a3472010-06-09 22:41:35 +0000615 if (dev->link_list->bridge_ctrl & PCI_BRIDGE_CTL_VGA)
Li-Ta Lo515f6c72005-01-11 22:48:54 +0000616 dev->command |= PCI_COMMAND_IO;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000617 ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
Myles Watson894a3472010-06-09 22:41:35 +0000618 ctrl |= dev->link_list->bridge_ctrl;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000619 ctrl |= (PCI_BRIDGE_CTL_PARITY + PCI_BRIDGE_CTL_SERR); /* Error check. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000620 printk(BIOS_DEBUG, "%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000621 pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
622
623 pci_dev_enable_resources(dev);
624}
625
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000626void pci_bus_reset(struct bus *bus)
627{
628 unsigned ctl;
629 ctl = pci_read_config16(bus->dev, PCI_BRIDGE_CONTROL);
630 ctl |= PCI_BRIDGE_CTL_BUS_RESET;
631 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
632 mdelay(10);
633 ctl &= ~PCI_BRIDGE_CTL_BUS_RESET;
634 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
635 delay(1);
636}
637
Myles Watson29cc9ed2009-07-02 18:56:24 +0000638void pci_dev_set_subsystem(struct device *dev, unsigned vendor, unsigned device)
Eric Biederman03acab62004-10-14 21:25:53 +0000639{
Myles Watson032a9652009-05-11 22:24:53 +0000640 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000641 ((device & 0xffff) << 16) | (vendor & 0xffff));
Eric Biederman03acab62004-10-14 21:25:53 +0000642}
643
Torsten Duwe1f2f8002008-01-06 01:10:54 +0000644/** default handler: only runs the relevant pci bios. */
Li-Ta Lo883b8792005-01-10 23:16:22 +0000645void pci_dev_init(struct device *dev)
646{
Torsten Duwe1f2f8002008-01-06 01:10:54 +0000647#if CONFIG_PCI_ROM_RUN == 1 || CONFIG_VGA_ROM_RUN == 1
Li-Ta Lo883b8792005-01-10 23:16:22 +0000648 struct rom_header *rom, *ram;
649
Myles Watson17aeeca2009-10-07 18:41:08 +0000650 if (CONFIG_PCI_ROM_RUN != 1 && /* Only execute VGA ROMs. */
651 ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA))
Roman Kononov778a42b2007-04-06 18:34:39 +0000652 return;
Myles Watson17aeeca2009-10-07 18:41:08 +0000653
654 if (CONFIG_VGA_ROM_RUN != 1 && /* Only execute non-VGA ROMs. */
655 ((dev->class >> 8) == PCI_CLASS_DISPLAY_VGA))
656 return;
Roman Kononov778a42b2007-04-06 18:34:39 +0000657
Li-Ta Lo883b8792005-01-10 23:16:22 +0000658 rom = pci_rom_probe(dev);
659 if (rom == NULL)
660 return;
Roman Kononov778a42b2007-04-06 18:34:39 +0000661
Li-Ta Lo883b8792005-01-10 23:16:22 +0000662 ram = pci_rom_load(dev, rom);
Yinghai Lu9e4faef2005-01-14 22:04:49 +0000663 if (ram == NULL)
664 return;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000665
Stefan Reinauerd98cf5b2008-08-01 11:25:41 +0000666 run_bios(dev, (unsigned long)ram);
Roman Kononov778a42b2007-04-06 18:34:39 +0000667
668#if CONFIG_CONSOLE_VGA == 1
Luc Verhaegen5c5beb72009-05-29 03:04:16 +0000669 if ((dev->class>>8) == PCI_CLASS_DISPLAY_VGA)
Luc Verhaegen43bc5a9c2009-05-29 03:44:47 +0000670 vga_console_init();
Torsten Duwe1f2f8002008-01-06 01:10:54 +0000671#endif /* CONFIG_CONSOLE_VGA */
672#endif /* CONFIG_PCI_ROM_RUN || CONFIG_VGA_ROM_RUN */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000673}
Li-Ta Lo883b8792005-01-10 23:16:22 +0000674
Li-Ta Loe5266692004-03-23 21:28:05 +0000675/** Default device operation for PCI devices */
Eric Biedermana9e632c2004-11-18 22:38:08 +0000676static struct pci_operations pci_dev_ops_pci = {
Eric Biederman03acab62004-10-14 21:25:53 +0000677 .set_subsystem = pci_dev_set_subsystem,
678};
679
Eric Biederman8ca8d762003-04-22 19:02:15 +0000680struct device_operations default_pci_ops_dev = {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000681 .read_resources = pci_dev_read_resources,
682 .set_resources = pci_dev_set_resources,
Eric Biedermane9a271e32003-09-02 03:36:25 +0000683 .enable_resources = pci_dev_enable_resources,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000684 .init = pci_dev_init,
685 .scan_bus = 0,
686 .enable = 0,
687 .ops_pci = &pci_dev_ops_pci,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000688};
Li-Ta Loe5266692004-03-23 21:28:05 +0000689
690/** Default device operations for PCI bridges */
Eric Biedermana9e632c2004-11-18 22:38:08 +0000691static struct pci_operations pci_bus_ops_pci = {
Eric Biederman03acab62004-10-14 21:25:53 +0000692 .set_subsystem = 0,
693};
Li-Ta Lo883b8792005-01-10 23:16:22 +0000694
Eric Biederman8ca8d762003-04-22 19:02:15 +0000695struct device_operations default_pci_ops_bus = {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000696 .read_resources = pci_bus_read_resources,
697 .set_resources = pci_dev_set_resources,
Eric Biedermane9a271e32003-09-02 03:36:25 +0000698 .enable_resources = pci_bus_enable_resources,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000699 .init = 0,
700 .scan_bus = pci_scan_bridge,
701 .enable = 0,
702 .reset_bus = pci_bus_reset,
703 .ops_pci = &pci_bus_ops_pci,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000704};
Li-Ta Loe5266692004-03-23 21:28:05 +0000705
706/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000707 * Detect the type of downstream bridge.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000708 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000709 * This function is a heuristic to detect which type of bus is downstream
710 * of a PCI-to-PCI bridge. This functions by looking for various capability
711 * blocks to figure out the type of downstream bridge. PCI-X, PCI-E, and
712 * Hypertransport all seem to have appropriate capabilities.
Myles Watson032a9652009-05-11 22:24:53 +0000713 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000714 * When only a PCI-Express capability is found the type
715 * is examined to see which type of bridge we have.
716 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000717 * @param dev Pointer to the device structure of the bridge.
718 * @return Appropriate bridge operations.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000719 */
720static struct device_operations *get_pci_bridge_ops(device_t dev)
721{
722 unsigned pos;
723
724#if CONFIG_PCIX_PLUGIN_SUPPORT == 1
725 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
726 if (pos) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000727 printk(BIOS_DEBUG, "%s subordinate bus PCI-X\n", dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000728 return &default_pcix_ops_bus;
729 }
730#endif
731#if CONFIG_AGP_PLUGIN_SUPPORT == 1
732 /* How do I detect an PCI to AGP bridge? */
733#endif
734#if CONFIG_HYPERTRANSPORT_PLUGIN_SUPPORT == 1
735 pos = 0;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000736 while ((pos = pci_find_next_capability(dev, PCI_CAP_ID_HT, pos))) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000737 unsigned flags;
738 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
739 if ((flags >> 13) == 1) {
740 /* Host or Secondary Interface */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000741 printk(BIOS_DEBUG, "%s subordinate bus Hypertransport\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000742 dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000743 return &default_ht_ops_bus;
744 }
745 }
746#endif
747#if CONFIG_PCIEXP_PLUGIN_SUPPORT == 1
748 pos = pci_find_capability(dev, PCI_CAP_ID_PCIE);
749 if (pos) {
750 unsigned flags;
751 flags = pci_read_config16(dev, pos + PCI_EXP_FLAGS);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000752 switch ((flags & PCI_EXP_FLAGS_TYPE) >> 4) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000753 case PCI_EXP_TYPE_ROOT_PORT:
754 case PCI_EXP_TYPE_UPSTREAM:
755 case PCI_EXP_TYPE_DOWNSTREAM:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000756 printk(BIOS_DEBUG, "%s subordinate bus PCI Express\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000757 dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000758 return &default_pciexp_ops_bus;
759 case PCI_EXP_TYPE_PCI_BRIDGE:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000760 printk(BIOS_DEBUG, "%s subordinate PCI\n", dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000761 return &default_pci_ops_bus;
762 default:
763 break;
764 }
765 }
766#endif
767 return &default_pci_ops_bus;
768}
769
770/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000771 * Set up PCI device operation.
772 *
773 * Check if it already has a driver. If not, use find_device_operations(),
774 * or set to a default based on type.
Li-Ta Loe5266692004-03-23 21:28:05 +0000775 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000776 * @param dev Pointer to the device whose pci_ops you want to set.
Li-Ta Loe5266692004-03-23 21:28:05 +0000777 * @see pci_drivers
778 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000779static void set_pci_ops(struct device *dev)
780{
781 struct pci_driver *driver;
782 if (dev->ops) {
783 return;
784 }
Li-Ta Loe5266692004-03-23 21:28:05 +0000785
Yinghai Lu5f9624d2006-10-04 22:56:21 +0000786 /* Look through the list of setup drivers and find one for
Myles Watson29cc9ed2009-07-02 18:56:24 +0000787 * this PCI device.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000788 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000789 for (driver = &pci_drivers[0]; driver != &epci_drivers[0]; driver++) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000790 if ((driver->vendor == dev->vendor) &&
Myles Watson29cc9ed2009-07-02 18:56:24 +0000791 (driver->device == dev->device)) {
Uwe Hermann312673c2009-10-27 21:49:33 +0000792 dev->ops = (struct device_operations *)driver->ops;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000793 printk(BIOS_SPEW, "%s [%04x/%04x] %sops\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000794 dev_path(dev),
795 driver->vendor, driver->device,
796 (driver->ops->scan_bus ? "bus " : ""));
Eric Biederman5899fd82003-04-24 06:25:08 +0000797 return;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000798 }
799 }
Li-Ta Loe5266692004-03-23 21:28:05 +0000800
Eric Biederman8ca8d762003-04-22 19:02:15 +0000801 /* If I don't have a specific driver use the default operations */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000802 switch (dev->hdr_type & 0x7f) { /* header type */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000803 case PCI_HEADER_TYPE_NORMAL: /* standard header */
804 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
805 goto bad;
806 dev->ops = &default_pci_ops_dev;
807 break;
808 case PCI_HEADER_TYPE_BRIDGE:
809 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
810 goto bad;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000811 dev->ops = get_pci_bridge_ops(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000812 break;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000813#if CONFIG_CARDBUS_PLUGIN_SUPPORT == 1
814 case PCI_HEADER_TYPE_CARDBUS:
815 dev->ops = &default_cardbus_ops_bus;
816 break;
817#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +0000818 default:
Myles Watson29cc9ed2009-07-02 18:56:24 +0000819 bad:
Li-Ta Lo69c5a902004-04-29 20:08:54 +0000820 if (dev->enabled) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000821 printk(BIOS_ERR, "%s [%04x/%04x/%06x] has unknown header "
Myles Watson29cc9ed2009-07-02 18:56:24 +0000822 "type %02x, ignoring.\n",
823 dev_path(dev),
824 dev->vendor, dev->device,
825 dev->class >> 8, dev->hdr_type);
Eric Biederman83b991a2003-10-11 06:20:25 +0000826 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000827 }
828 return;
829}
830
831/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000832 * See if we have already allocated a device structure for a given devfn.
Li-Ta Loe5266692004-03-23 21:28:05 +0000833 *
834 * Given a linked list of PCI device structures and a devfn number, find the
Li-Ta Lo3a812852004-12-03 22:39:34 +0000835 * device structure correspond to the devfn, if present. This function also
836 * removes the device structure from the linked list.
Li-Ta Loe5266692004-03-23 21:28:05 +0000837 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000838 * @param list The device structure list.
839 * @param devfn A device/function number.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000840 * @return Pointer to the device structure found or NULL if we have not
Li-Ta Lo3a812852004-12-03 22:39:34 +0000841 * allocated a device for this devfn yet.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000842 */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000843static struct device *pci_scan_get_dev(struct device **list, unsigned int devfn)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000844{
Eric Biedermanb78c1972004-10-14 20:54:17 +0000845 struct device *dev;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000846 dev = 0;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000847 for (; *list; list = &(*list)->sibling) {
Eric Biedermanad1b35a2003-10-14 02:36:51 +0000848 if ((*list)->path.type != DEVICE_PATH_PCI) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000849 printk(BIOS_ERR, "child %s not a pci device\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000850 dev_path(*list));
Eric Biedermanad1b35a2003-10-14 02:36:51 +0000851 continue;
852 }
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000853 if ((*list)->path.pci.devfn == devfn) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000854 /* Unlink from the list. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000855 dev = *list;
856 *list = (*list)->sibling;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000857 dev->sibling = NULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000858 break;
859 }
860 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000861
862 /* Just like alloc_dev() add the device to the list of devices on the
863 * bus. When the list of devices was formed we removed all of the
864 * parents children, and now we are interleaving static and dynamic
865 * devices in order on the bus.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000866 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000867 if (dev) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000868 struct device *child;
869 /* Find the last child of our parent. */
870 for (child = dev->bus->children; child && child->sibling;) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000871 child = child->sibling;
872 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000873 /* Place the device on the list of children of its parent. */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000874 if (child) {
875 child->sibling = dev;
876 } else {
877 dev->bus->children = dev;
878 }
879 }
880
Eric Biederman8ca8d762003-04-22 19:02:15 +0000881 return dev;
882}
883
Myles Watson032a9652009-05-11 22:24:53 +0000884/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000885 * Scan a PCI bus.
Li-Ta Loe5266692004-03-23 21:28:05 +0000886 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000887 * Determine the existence of a given PCI device. Allocate a new struct device
888 * if dev==NULL was passed in and the device exists in hardware.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000889 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000890 * @param dev Pointer to the dev structure.
891 * @param bus Pointer to the bus structure.
892 * @param devfn A device/function number to look at.
893 * @return The device structure for the device (if found), NULL otherwise.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000894 */
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000895device_t pci_probe_dev(device_t dev, struct bus *bus, unsigned devfn)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000896{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000897 u32 id, class;
898 u8 hdr_type;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000899
Myles Watson29cc9ed2009-07-02 18:56:24 +0000900 /* Detect if a device is present. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000901 if (!dev) {
902 struct device dummy;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000903 dummy.bus = bus;
904 dummy.path.type = DEVICE_PATH_PCI;
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000905 dummy.path.pci.devfn = devfn;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000906 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000907 /* Have we found something?
Stefan Reinauer7355c752010-04-02 16:30:25 +0000908 * Some broken boards return 0 if a slot is empty, but
909 * the expected answer is 0xffffffff
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000910 */
Stefan Reinauer7355c752010-04-02 16:30:25 +0000911 if (id == 0xffffffff) {
912 return NULL;
913 }
914 if ((id == 0x00000000) || (id == 0x0000ffff) ||
915 (id == 0xffff0000)) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000916 printk(BIOS_SPEW, "%s, bad id 0x%x\n", dev_path(&dummy), id);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000917 return NULL;
918 }
919 dev = alloc_dev(bus, &dummy.path);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000920 } else {
921 /* Enable/disable the device. Once we have found the device-
922 * specific operations this operations we will disable the
923 * device with those as well.
Myles Watson032a9652009-05-11 22:24:53 +0000924 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000925 * This is geared toward devices that have subfunctions
926 * that do not show up by default.
Myles Watson032a9652009-05-11 22:24:53 +0000927 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000928 * If a device is a stuff option on the motherboard
Myles Watson29cc9ed2009-07-02 18:56:24 +0000929 * it may be absent and enable_dev() must cope.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000930 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000931 /* Run the magic enable sequence for the device. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000932 if (dev->chip_ops && dev->chip_ops->enable_dev) {
933 dev->chip_ops->enable_dev(dev);
934 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000935 /* Now read the vendor and device ID. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000936 id = pci_read_config32(dev, PCI_VENDOR_ID);
Myles Watson032a9652009-05-11 22:24:53 +0000937
Myles Watson29cc9ed2009-07-02 18:56:24 +0000938 /* If the device does not have a PCI ID disable it. Possibly
939 * this is because we have already disabled the device. But
940 * this also handles optional devices that may not always
941 * show up.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000942 */
943 /* If the chain is fully enumerated quit */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000944 if ((id == 0xffffffff) || (id == 0x00000000) ||
945 (id == 0x0000ffff) || (id == 0xffff0000)) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000946 if (dev->enabled) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000947 printk(BIOS_INFO, "PCI: Static device %s not found, disabling it.\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000948 dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000949 dev->enabled = 0;
950 }
951 return dev;
952 }
953 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000954 /* Read the rest of the PCI configuration information. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000955 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
956 class = pci_read_config32(dev, PCI_CLASS_REVISION);
Myles Watson032a9652009-05-11 22:24:53 +0000957
Myles Watson29cc9ed2009-07-02 18:56:24 +0000958 /* Store the interesting information in the device structure. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000959 dev->vendor = id & 0xffff;
960 dev->device = (id >> 16) & 0xffff;
961 dev->hdr_type = hdr_type;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000962
963 /* Class code, the upper 3 bytes of PCI_CLASS_REVISION. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000964 dev->class = class >> 8;
Myles Watson032a9652009-05-11 22:24:53 +0000965
Myles Watson29cc9ed2009-07-02 18:56:24 +0000966 /* Architectural/System devices always need to be bus masters. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000967 if ((dev->class >> 16) == PCI_BASE_CLASS_SYSTEM) {
968 dev->command |= PCI_COMMAND_MASTER;
969 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000970 /* Look at the vendor and device ID, or at least the header type and
971 * class and figure out which set of configuration methods to use.
972 * Unless we already have some PCI ops.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000973 */
974 set_pci_ops(dev);
975
Myles Watson29cc9ed2009-07-02 18:56:24 +0000976 /* Now run the magic enable/disable sequence for the device. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000977 if (dev->ops && dev->ops->enable) {
978 dev->ops->enable(dev);
979 }
Myles Watson032a9652009-05-11 22:24:53 +0000980
Myles Watson29cc9ed2009-07-02 18:56:24 +0000981 /* Display the device. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000982 printk(BIOS_DEBUG, "%s [%04x/%04x] %s%s\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000983 dev_path(dev),
984 dev->vendor, dev->device,
985 dev->enabled ? "enabled" : "disabled",
986 dev->ops ? "" : " No operations");
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000987
988 return dev;
989}
990
Myles Watson032a9652009-05-11 22:24:53 +0000991/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000992 * Scan a PCI bus.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000993 *
Li-Ta Loe5266692004-03-23 21:28:05 +0000994 * Determine the existence of devices and bridges on a PCI bus. If there are
995 * bridges on the bus, recursively scan the buses behind the bridges.
996 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000997 * This function is the default scan_bus() method for the root device
998 * 'dev_root'.
999 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001000 * @param bus Pointer to the bus structure.
1001 * @param min_devfn Minimum devfn to look at in the scan, usually 0x00.
1002 * @param max_devfn Maximum devfn to look at in the scan, usually 0xff.
1003 * @param max Current bus number.
1004 * @return The maximum bus number found, after scanning all subordinate busses.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001005 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001006unsigned int pci_scan_bus(struct bus *bus,
Myles Watson29cc9ed2009-07-02 18:56:24 +00001007 unsigned min_devfn, unsigned max_devfn,
1008 unsigned int max)
Eric Biederman8ca8d762003-04-22 19:02:15 +00001009{
1010 unsigned int devfn;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001011 struct device *old_devices;
1012 struct device *child;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001013
Stefan Reinauer08670622009-06-30 15:17:49 +00001014#if CONFIG_PCI_BUS_SEGN_BITS
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001015 printk(BIOS_DEBUG, "PCI: pci_scan_bus for bus %04x:%02x\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +00001016 bus->secondary >> 8, bus->secondary & 0xff);
Yinghai Lu5f9624d2006-10-04 22:56:21 +00001017#else
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001018 printk(BIOS_DEBUG, "PCI: pci_scan_bus for bus %02x\n", bus->secondary);
Yinghai Lu5f9624d2006-10-04 22:56:21 +00001019#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +00001020
Juhana Helovuo50b78b62010-09-13 14:43:02 +00001021 // Maximum sane devfn is 0xFF
1022 if (max_devfn > 0xff) {
1023 printk(BIOS_ERR, "PCI: pci_scan_bus limits devfn %x - devfn %x\n",
1024 min_devfn, max_devfn );
1025 printk(BIOS_ERR, "PCI: pci_scan_bus upper limit too big. Using 0xff.\n");
1026 max_devfn=0xff;
1027 }
1028
Eric Biederman8ca8d762003-04-22 19:02:15 +00001029 old_devices = bus->children;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001030 bus->children = NULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001031
1032 post_code(0x24);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001033 /* Probe all devices/functions on this bus with some optimization for
1034 * non-existence and single function devices.
Eric Biedermanb78c1972004-10-14 20:54:17 +00001035 */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001036 for (devfn = min_devfn; devfn <= max_devfn; devfn++) {
Myles Watson29cc9ed2009-07-02 18:56:24 +00001037 struct device *dev;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001038
Eric Biederman03acab62004-10-14 21:25:53 +00001039 /* First thing setup the device structure */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001040 dev = pci_scan_get_dev(&old_devices, devfn);
Li-Ta Lo9782f752004-05-05 21:15:42 +00001041
Myles Watson29cc9ed2009-07-02 18:56:24 +00001042 /* See if a device is present and setup the device structure. */
Myles Watson032a9652009-05-11 22:24:53 +00001043 dev = pci_probe_dev(dev, bus, devfn);
Eric Biederman03acab62004-10-14 21:25:53 +00001044
Myles Watson29cc9ed2009-07-02 18:56:24 +00001045 /* If this is not a multi function device, or the device is
1046 * not present don't waste time probing another function.
Myles Watson032a9652009-05-11 22:24:53 +00001047 * Skip to next device.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001048 */
Myles Watson032a9652009-05-11 22:24:53 +00001049 if ((PCI_FUNC(devfn) == 0x00) &&
Myles Watson29cc9ed2009-07-02 18:56:24 +00001050 (!dev
1051 || (dev->enabled && ((dev->hdr_type & 0x80) != 0x80)))) {
Eric Biederman8ca8d762003-04-22 19:02:15 +00001052 devfn += 0x07;
1053 }
1054 }
1055 post_code(0x25);
1056
Myles Watson29cc9ed2009-07-02 18:56:24 +00001057 /* Warn if any leftover static devices are are found.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001058 * There's probably a problem in the Config.lb.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001059 */
1060 if (old_devices) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001061 device_t left;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001062 printk(BIOS_WARNING, "PCI: Left over static devices:\n");
Myles Watson29cc9ed2009-07-02 18:56:24 +00001063 for (left = old_devices; left; left = left->sibling) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001064 printk(BIOS_WARNING, "%s\n", dev_path(left));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001065 }
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001066 printk(BIOS_WARNING, "PCI: Check your mainboard Config.lb.\n");
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001067 }
1068
Myles Watson29cc9ed2009-07-02 18:56:24 +00001069 /* For all children that implement scan_bus() (i.e. bridges)
Eric Biedermanb78c1972004-10-14 20:54:17 +00001070 * scan the bus behind that child.
1071 */
Myles Watson29cc9ed2009-07-02 18:56:24 +00001072 for (child = bus->children; child; child = child->sibling) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001073 max = scan_bus(child, max);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001074 }
Li-Ta Loe5266692004-03-23 21:28:05 +00001075
Myles Watson29cc9ed2009-07-02 18:56:24 +00001076 /* We've scanned the bus and so we know all about what's on the other
1077 * side of any bridges that may be on this bus plus any devices.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001078 * Return how far we've got finding sub-buses.
1079 */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001080 printk(BIOS_DEBUG, "PCI: pci_scan_bus returning with max=%03x\n", max);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001081 post_code(0x55);
1082 return max;
1083}
1084
Li-Ta Loe5266692004-03-23 21:28:05 +00001085/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001086 * Scan a PCI bridge and the buses behind the bridge.
Li-Ta Loe5266692004-03-23 21:28:05 +00001087 *
1088 * Determine the existence of buses behind the bridge. Set up the bridge
1089 * according to the result of the scan.
1090 *
1091 * This function is the default scan_bus() method for PCI bridge devices.
1092 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001093 * @param dev Pointer to the bridge device.
1094 * @param max The highest bus number assigned up to now.
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001095 * @param do_scan_bus TODO
Myles Watson29cc9ed2009-07-02 18:56:24 +00001096 * @return The maximum bus number found, after scanning all subordinate buses.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001097 */
Myles Watson032a9652009-05-11 22:24:53 +00001098unsigned int do_pci_scan_bridge(struct device *dev, unsigned int max,
Myles Watson29cc9ed2009-07-02 18:56:24 +00001099 unsigned int (*do_scan_bus) (struct bus * bus,
1100 unsigned min_devfn,
1101 unsigned max_devfn,
1102 unsigned int max))
Eric Biederman8ca8d762003-04-22 19:02:15 +00001103{
Eric Biedermane9a271e32003-09-02 03:36:25 +00001104 struct bus *bus;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001105 u32 buses;
1106 u16 cr;
Eric Biederman83b991a2003-10-11 06:20:25 +00001107
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001108 printk(BIOS_SPEW, "%s for %s\n", __func__, dev_path(dev));
Li-Ta Lo3a812852004-12-03 22:39:34 +00001109
Myles Watson894a3472010-06-09 22:41:35 +00001110 if (dev->link_list == NULL) {
1111 struct bus *link;
1112 link = malloc(sizeof(*link));
1113 if (link == NULL)
1114 die("Couldn't allocate a link!\n");
1115 memset(link, 0, sizeof(*link));
1116 link->dev = dev;
1117 dev->link_list = link;
1118 }
1119
1120 bus = dev->link_list;
Eric Biedermane9a271e32003-09-02 03:36:25 +00001121
Eric Biederman8ca8d762003-04-22 19:02:15 +00001122 /* Set up the primary, secondary and subordinate bus numbers. We have
1123 * no idea how many buses are behind this bridge yet, so we set the
Myles Watson032a9652009-05-11 22:24:53 +00001124 * subordinate bus number to 0xff for the moment.
Eric Biedermanb78c1972004-10-14 20:54:17 +00001125 */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001126 bus->secondary = ++max;
1127 bus->subordinate = 0xff;
Li-Ta Loe5266692004-03-23 21:28:05 +00001128
Eric Biederman8ca8d762003-04-22 19:02:15 +00001129 /* Clear all status bits and turn off memory, I/O and master enables. */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001130 cr = pci_read_config16(dev, PCI_COMMAND);
1131 pci_write_config16(dev, PCI_COMMAND, 0x0000);
1132 pci_write_config16(dev, PCI_STATUS, 0xffff);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001133
Myles Watson29cc9ed2009-07-02 18:56:24 +00001134 /* Read the existing primary/secondary/subordinate bus
Eric Biedermanb78c1972004-10-14 20:54:17 +00001135 * number configuration.
1136 */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001137 buses = pci_read_config32(dev, PCI_PRIMARY_BUS);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001138
1139 /* Configure the bus numbers for this bridge: the configuration
1140 * transactions will not be propagated by the bridge if it is not
Eric Biedermanb78c1972004-10-14 20:54:17 +00001141 * correctly configured.
1142 */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001143 buses &= 0xff000000;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001144 buses |= (((unsigned int)(dev->bus->secondary) << 0) |
1145 ((unsigned int)(bus->secondary) << 8) |
1146 ((unsigned int)(bus->subordinate) << 16));
Eric Biedermane9a271e32003-09-02 03:36:25 +00001147 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
Li-Ta Lo3a812852004-12-03 22:39:34 +00001148
Myles Watson032a9652009-05-11 22:24:53 +00001149 /* Now we can scan all subordinate buses
Eric Biedermanb78c1972004-10-14 20:54:17 +00001150 * i.e. the bus behind the bridge.
1151 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001152 max = do_scan_bus(bus, 0x00, 0xff, max);
Li-Ta Lo3a812852004-12-03 22:39:34 +00001153
Eric Biederman8ca8d762003-04-22 19:02:15 +00001154 /* We know the number of buses behind this bridge. Set the subordinate
Eric Biedermanb78c1972004-10-14 20:54:17 +00001155 * bus number to its real value.
1156 */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001157 bus->subordinate = max;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001158 buses = (buses & 0xff00ffff) | ((unsigned int)(bus->subordinate) << 16);
Eric Biedermane9a271e32003-09-02 03:36:25 +00001159 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
1160 pci_write_config16(dev, PCI_COMMAND, cr);
Myles Watson032a9652009-05-11 22:24:53 +00001161
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001162 printk(BIOS_SPEW, "%s returns max %d\n", __func__, max);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001163 return max;
1164}
Li-Ta Loe5266692004-03-23 21:28:05 +00001165
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001166/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001167 * Scan a PCI bridge and the buses behind the bridge.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001168 *
1169 * Determine the existence of buses behind the bridge. Set up the bridge
1170 * according to the result of the scan.
1171 *
1172 * This function is the default scan_bus() method for PCI bridge devices.
1173 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001174 * @param dev Pointer to the bridge device.
1175 * @param max The highest bus number assigned up to now.
1176 * @return The maximum bus number found, after scanning all subordinate buses.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001177 */
1178unsigned int pci_scan_bridge(struct device *dev, unsigned int max)
1179{
1180 return do_pci_scan_bridge(dev, max, pci_scan_bus);
1181}
1182
Myles Watson29cc9ed2009-07-02 18:56:24 +00001183/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001184 * Scan a PCI domain.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001185 *
1186 * This function is the default scan_bus() method for PCI domains.
1187 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001188 * @param dev Pointer to the domain.
1189 * @param max The highest bus number assigned up to now.
1190 * @return The maximum bus number found, after scanning all subordinate busses.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001191 */
1192unsigned int pci_domain_scan_bus(device_t dev, unsigned int max)
1193{
Myles Watson894a3472010-06-09 22:41:35 +00001194 max = pci_scan_bus(dev->link_list, PCI_DEVFN(0, 0), 0xff, max);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001195 return max;
1196}
1197
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001198#if CONFIG_PC80_SYSTEM == 1
Myles Watson29cc9ed2009-07-02 18:56:24 +00001199/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001200 * Assign IRQ numbers.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001201 *
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001202 * This function assigns IRQs for all functions contained within the indicated
1203 * device address. If the device does not exist or does not require interrupts
1204 * then this function has no effect.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001205 *
1206 * This function should be called for each PCI slot in your system.
1207 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001208 * @param bus Pointer to the bus structure.
1209 * @param slot TODO
1210 * @param pIntAtoD An array of IRQ #s that are assigned to PINTA through PINTD
1211 * of this slot. The particular IRQ #s that are passed in depend on the
1212 * routing inside your southbridge and on your board.
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001213 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001214void pci_assign_irqs(unsigned bus, unsigned slot,
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001215 const unsigned char pIntAtoD[4])
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001216{
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001217 unsigned int funct;
1218 device_t pdev;
1219 u8 line;
1220 u8 irq;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001221
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001222 /* Each slot may contain up to eight functions */
1223 for (funct = 0; funct < 8; funct++) {
1224 pdev = dev_find_slot(bus, (slot << 3) + funct);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001225
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001226 if (!pdev)
1227 continue;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001228
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001229 line = pci_read_config8(pdev, PCI_INTERRUPT_PIN);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001230
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001231 // PCI spec says all values except 1..4 are reserved.
1232 if ((line < 1) || (line > 4))
1233 continue;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001234
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001235 irq = pIntAtoD[line - 1];
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001236
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001237 printk(BIOS_DEBUG, "Assigning IRQ %d to %d:%x.%d\n",
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001238 irq, bus, slot, funct);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001239
Stefan Reinauer14e22772010-04-27 06:56:47 +00001240 pci_write_config8(pdev, PCI_INTERRUPT_LINE,
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001241 pIntAtoD[line - 1]);
1242
1243#ifdef PARANOID_IRQ_ASSIGNMENTS
Myles Watson17aeeca2009-10-07 18:41:08 +00001244 irq = pci_read_config8(pdev, PCI_INTERRUPT_LINE);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001245 printk(BIOS_DEBUG, " Readback = %d\n", irq);
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001246#endif
1247
1248 // Change to level triggered
1249 i8259_configure_irq_trigger(pIntAtoD[line - 1], IRQ_LEVEL_TRIGGERED);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001250 }
1251}
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001252#endif
1253