blob: 16e8d3f20304bd784cae96df4ff54449a906e9bd [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.
109 * @param cap_type PCI_CAP_LIST_ID of the PCI capability we're looking for.
110 * @param last Location of the PCI capability register to start from.
111 */
112unsigned pci_find_next_capability(struct device *dev, unsigned cap,
113 unsigned last)
Eric Biederman03acab62004-10-14 21:25:53 +0000114{
Stefan Reinauer4d933dd2009-07-21 21:36:41 +0000115 unsigned pos = 0;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000116 unsigned status;
117 unsigned reps = 48;
Stefan Reinauer4d933dd2009-07-21 21:36:41 +0000118
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000119 status = pci_read_config16(dev, PCI_STATUS);
120 if (!(status & PCI_STATUS_CAP_LIST)) {
121 return 0;
122 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000123 switch (dev->hdr_type & 0x7f) {
Eric Biederman03acab62004-10-14 21:25:53 +0000124 case PCI_HEADER_TYPE_NORMAL:
125 case PCI_HEADER_TYPE_BRIDGE:
126 pos = PCI_CAPABILITY_LIST;
127 break;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000128 case PCI_HEADER_TYPE_CARDBUS:
129 pos = PCI_CB_CAPABILITY_LIST;
130 break;
131 default:
132 return 0;
Eric Biederman03acab62004-10-14 21:25:53 +0000133 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000134 pos = pci_read_config8(dev, pos);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000135 while (reps-- && (pos >= 0x40)) { /* Loop through the linked list. */
Eric Biederman03acab62004-10-14 21:25:53 +0000136 int this_cap;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000137 pos &= ~3;
Eric Biederman03acab62004-10-14 21:25:53 +0000138 this_cap = pci_read_config8(dev, pos + PCI_CAP_LIST_ID);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000139 printk(BIOS_SPEW, "Capability: type 0x%02x @ 0x%02x\n", this_cap,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000140 pos);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000141 if (this_cap == 0xff) {
142 break;
143 }
144 if (!last && (this_cap == cap)) {
Eric Biederman03acab62004-10-14 21:25:53 +0000145 return pos;
146 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000147 if (last == pos) {
148 last = 0;
149 }
150 pos = pci_read_config8(dev, pos + PCI_CAP_LIST_NEXT);
Eric Biederman03acab62004-10-14 21:25:53 +0000151 }
152 return 0;
153}
154
Myles Watson29cc9ed2009-07-02 18:56:24 +0000155/**
156 * Given a device, and a capability type, return the next matching
157 * capability. Always start at the head of the list.
158 *
159 * @param dev Pointer to the device structure.
160 * @param cap_type PCI_CAP_LIST_ID of the PCI capability we're looking for.
161 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000162unsigned pci_find_capability(device_t dev, unsigned cap)
163{
164 return pci_find_next_capability(dev, cap, 0);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000165}
166
Myles Watson29cc9ed2009-07-02 18:56:24 +0000167/**
168 * Given a device and register, read the size of the BAR for that register.
169 *
170 * @param dev Pointer to the device structure.
171 * @param index Address of the PCI configuration register.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000172 */
Eric Biederman03acab62004-10-14 21:25:53 +0000173struct resource *pci_get_resource(struct device *dev, unsigned long index)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000174{
Eric Biederman5cd81732004-03-11 15:01:31 +0000175 struct resource *resource;
Eric Biederman03acab62004-10-14 21:25:53 +0000176 unsigned long value, attr;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000177 resource_t moving, limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000178
Myles Watson29cc9ed2009-07-02 18:56:24 +0000179 /* Initialize the resources to nothing. */
Eric Biederman03acab62004-10-14 21:25:53 +0000180 resource = new_resource(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000181
Myles Watson29cc9ed2009-07-02 18:56:24 +0000182 /* Get the initial value. */
Eric Biederman03acab62004-10-14 21:25:53 +0000183 value = pci_read_config32(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000184
Myles Watson29cc9ed2009-07-02 18:56:24 +0000185 /* See which bits move. */
Eric Biederman03acab62004-10-14 21:25:53 +0000186 moving = pci_moving_config32(dev, index);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000187
Myles Watson29cc9ed2009-07-02 18:56:24 +0000188 /* Initialize attr to the bits that do not move. */
Eric Biederman03acab62004-10-14 21:25:53 +0000189 attr = value & ~moving;
190
Myles Watson29cc9ed2009-07-02 18:56:24 +0000191 /* If it is a 64bit resource look at the high half as well. */
Eric Biederman03acab62004-10-14 21:25:53 +0000192 if (((attr & PCI_BASE_ADDRESS_SPACE_IO) == 0) &&
Myles Watson29cc9ed2009-07-02 18:56:24 +0000193 ((attr & PCI_BASE_ADDRESS_MEM_LIMIT_MASK) ==
194 PCI_BASE_ADDRESS_MEM_LIMIT_64)) {
195 /* Find the high bits that move. */
196 moving |=
197 ((resource_t) pci_moving_config32(dev, index + 4)) << 32;
Eric Biederman03acab62004-10-14 21:25:53 +0000198 }
Myles Watson032a9652009-05-11 22:24:53 +0000199 /* Find the resource constraints.
Eric Biederman03acab62004-10-14 21:25:53 +0000200 * Start by finding the bits that move. From there:
201 * - Size is the least significant bit of the bits that move.
202 * - Limit is all of the bits that move plus all of the lower bits.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000203 * See PCI Spec 6.2.5.1.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000204 */
Eric Biederman03acab62004-10-14 21:25:53 +0000205 limit = 0;
206 if (moving) {
207 resource->size = 1;
208 resource->align = resource->gran = 0;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000209 while (!(moving & resource->size)) {
Eric Biederman03acab62004-10-14 21:25:53 +0000210 resource->size <<= 1;
211 resource->align += 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000212 resource->gran += 1;
Eric Biederman03acab62004-10-14 21:25:53 +0000213 }
214 resource->limit = limit = moving | (resource->size - 1);
215 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000216
217 /* Some broken hardware has read-only registers that do not
Eric Biederman03acab62004-10-14 21:25:53 +0000218 * really size correctly.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000219 * Example: the Acer M7229 has BARs 1-4 normally read-only.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000220 * so BAR1 at offset 0x10 reads 0x1f1. If you size that register
Myles Watson032a9652009-05-11 22:24:53 +0000221 * by writing 0xffffffff to it, it will read back as 0x1f1 -- a
222 * violation of the spec.
Eric Biederman03acab62004-10-14 21:25:53 +0000223 * We catch this case and ignore it by observing which bits move,
224 * This also catches the common case unimplemented registers
225 * that always read back as 0.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000226 */
Eric Biederman03acab62004-10-14 21:25:53 +0000227 if (moving == 0) {
228 if (value != 0) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000229 printk(BIOS_DEBUG, "%s register %02lx(%08lx), read-only ignoring it\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000230 dev_path(dev), index, value);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000231 }
232 resource->flags = 0;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000233 } else if (attr & PCI_BASE_ADDRESS_SPACE_IO) {
234 /* An I/O mapped base address. */
Eric Biederman03acab62004-10-14 21:25:53 +0000235 attr &= PCI_BASE_ADDRESS_IO_ATTR_MASK;
Eric Biederman5cd81732004-03-11 15:01:31 +0000236 resource->flags |= IORESOURCE_IO;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000237 /* I don't want to deal with 32bit I/O resources. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000238 resource->limit = 0xffff;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000239 } else {
240 /* A Memory mapped base address. */
Eric Biederman03acab62004-10-14 21:25:53 +0000241 attr &= PCI_BASE_ADDRESS_MEM_ATTR_MASK;
Eric Biederman5cd81732004-03-11 15:01:31 +0000242 resource->flags |= IORESOURCE_MEM;
Eric Biederman03acab62004-10-14 21:25:53 +0000243 if (attr & PCI_BASE_ADDRESS_MEM_PREFETCH) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000244 resource->flags |= IORESOURCE_PREFETCH;
245 }
Eric Biederman03acab62004-10-14 21:25:53 +0000246 attr &= PCI_BASE_ADDRESS_MEM_LIMIT_MASK;
247 if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_32) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000248 /* 32bit limit. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000249 resource->limit = 0xffffffffUL;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000250 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_1M) {
251 /* 1MB limit. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000252 resource->limit = 0x000fffffUL;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000253 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_64) {
254 /* 64bit limit. */
Eric Biederman03acab62004-10-14 21:25:53 +0000255 resource->limit = 0xffffffffffffffffULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000256 resource->flags |= IORESOURCE_PCI64;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000257 } else {
258 /* Invalid value. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000259 printk(BIOS_ERR, "Broken BAR with value %lx\n", attr);
260 printk(BIOS_ERR, " on dev %s at index %02lx\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000261 dev_path(dev), index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000262 resource->flags = 0;
263 }
264 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000265 /* Don't let the limit exceed which bits can move. */
Eric Biederman03acab62004-10-14 21:25:53 +0000266 if (resource->limit > limit) {
267 resource->limit = limit;
268 }
Eric Biederman03acab62004-10-14 21:25:53 +0000269
Eric Biederman5cd81732004-03-11 15:01:31 +0000270 return resource;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000271}
272
Myles Watson29cc9ed2009-07-02 18:56:24 +0000273/**
274 * Given a device and an index, read the size of the BAR for that register.
275 *
276 * @param dev Pointer to the device structure.
277 * @param index Address of the PCI configuration register.
278 */
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000279static void pci_get_rom_resource(struct device *dev, unsigned long index)
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000280{
281 struct resource *resource;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000282 unsigned long value;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000283 resource_t moving;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000284
Myles Watson29cc9ed2009-07-02 18:56:24 +0000285 /* Initialize the resources to nothing. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000286 resource = new_resource(dev, index);
287
Myles Watson29cc9ed2009-07-02 18:56:24 +0000288 /* Get the initial value. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000289 value = pci_read_config32(dev, index);
290
Myles Watson29cc9ed2009-07-02 18:56:24 +0000291 /* See which bits move. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000292 moving = pci_moving_config32(dev, index);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000293
294 /* Clear the Enable bit. */
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000295 moving = moving & ~PCI_ROM_ADDRESS_ENABLE;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000296
Myles Watson032a9652009-05-11 22:24:53 +0000297 /* Find the resource constraints.
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000298 * Start by finding the bits that move. From there:
299 * - Size is the least significant bit of the bits that move.
300 * - Limit is all of the bits that move plus all of the lower bits.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000301 * See PCI Spec 6.2.5.1.
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000302 */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000303 if (moving) {
304 resource->size = 1;
305 resource->align = resource->gran = 0;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000306 while (!(moving & resource->size)) {
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000307 resource->size <<= 1;
308 resource->align += 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000309 resource->gran += 1;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000310 }
Patrick Georgi16cdbb22009-04-21 20:14:31 +0000311 resource->limit = moving | (resource->size - 1);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000312 resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY;
313 } else {
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000314 if (value != 0) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000315 printk(BIOS_DEBUG, "%s register %02lx(%08lx), read-only ignoring it\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000316 dev_path(dev), index, value);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000317 }
318 resource->flags = 0;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000319 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000320 compact_resources(dev);
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000321}
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000322
Myles Watson29cc9ed2009-07-02 18:56:24 +0000323/**
324 * Read the base address registers for a given device.
325 *
326 * @param dev Pointer to the dev structure.
327 * @param howmany How many registers to read (6 for device, 2 for bridge).
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000328 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000329static void pci_read_bases(struct device *dev, unsigned int howmany)
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000330{
331 unsigned long index;
332
Myles Watson29cc9ed2009-07-02 18:56:24 +0000333 for (index = PCI_BASE_ADDRESS_0;
334 (index < PCI_BASE_ADDRESS_0 + (howmany << 2));) {
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000335 struct resource *resource;
336 resource = pci_get_resource(dev, index);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000337 index += (resource->flags & IORESOURCE_PCI64) ? 8 : 4;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000338 }
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000339
340 compact_resources(dev);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000341}
342
Myles Watson29cc9ed2009-07-02 18:56:24 +0000343static void pci_record_bridge_resource(struct device *dev, resource_t moving,
344 unsigned index, unsigned long type)
Eric Biederman03acab62004-10-14 21:25:53 +0000345{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000346 /* Initialize the constraints on the current bus. */
Eric Biederman03acab62004-10-14 21:25:53 +0000347 struct resource *resource;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000348 resource = NULL;
Eric Biederman03acab62004-10-14 21:25:53 +0000349 if (moving) {
350 unsigned long gran;
351 resource_t step;
352 resource = new_resource(dev, index);
353 resource->size = 0;
354 gran = 0;
355 step = 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000356 while ((moving & step) == 0) {
Eric Biederman03acab62004-10-14 21:25:53 +0000357 gran += 1;
358 step <<= 1;
359 }
360 resource->gran = gran;
361 resource->align = gran;
362 resource->limit = moving | (step - 1);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000363 resource->flags = type | IORESOURCE_PCI_BRIDGE |
364 IORESOURCE_BRIDGE;
Eric Biederman03acab62004-10-14 21:25:53 +0000365 }
366 return;
367}
368
Eric Biederman8ca8d762003-04-22 19:02:15 +0000369static void pci_bridge_read_bases(struct device *dev)
370{
Eric Biederman03acab62004-10-14 21:25:53 +0000371 resource_t moving_base, moving_limit, moving;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000372
Myles Watson29cc9ed2009-07-02 18:56:24 +0000373 /* See if the bridge I/O resources are implemented. */
374 moving_base = ((u32) pci_moving_config8(dev, PCI_IO_BASE)) << 8;
375 moving_base |=
376 ((u32) pci_moving_config16(dev, PCI_IO_BASE_UPPER16)) << 16;
Eric Biederman03acab62004-10-14 21:25:53 +0000377
Myles Watson29cc9ed2009-07-02 18:56:24 +0000378 moving_limit = ((u32) pci_moving_config8(dev, PCI_IO_LIMIT)) << 8;
379 moving_limit |=
380 ((u32) pci_moving_config16(dev, PCI_IO_LIMIT_UPPER16)) << 16;
Eric Biederman03acab62004-10-14 21:25:53 +0000381
382 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000383
Myles Watson29cc9ed2009-07-02 18:56:24 +0000384 /* Initialize the I/O space constraints on the current bus. */
385 pci_record_bridge_resource(dev, moving, PCI_IO_BASE, IORESOURCE_IO);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000386
Myles Watson29cc9ed2009-07-02 18:56:24 +0000387 /* See if the bridge prefmem resources are implemented. */
388 moving_base =
389 ((resource_t) pci_moving_config16(dev, PCI_PREF_MEMORY_BASE)) << 16;
390 moving_base |=
391 ((resource_t) pci_moving_config32(dev, PCI_PREF_BASE_UPPER32)) <<
392 32;
Eric Biederman03acab62004-10-14 21:25:53 +0000393
Myles Watson29cc9ed2009-07-02 18:56:24 +0000394 moving_limit =
395 ((resource_t) pci_moving_config16(dev, PCI_PREF_MEMORY_LIMIT)) <<
396 16;
397 moving_limit |=
398 ((resource_t) pci_moving_config32(dev, PCI_PREF_LIMIT_UPPER32)) <<
399 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/**
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000707 * @brief Detect the type of downstream bridge
708 *
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/**
Myles Watson29cc9ed2009-07-02 18:56:24 +0000771 * Set up PCI device operation. Check if it already has a driver. If not, use
772 * find_device_operations, or set to a default based on type.
Li-Ta Loe5266692004-03-23 21:28:05 +0000773 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000774 * @param dev Pointer to the device whose pci_ops you want to set.
Li-Ta Loe5266692004-03-23 21:28:05 +0000775 * @see pci_drivers
776 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000777static void set_pci_ops(struct device *dev)
778{
779 struct pci_driver *driver;
780 if (dev->ops) {
781 return;
782 }
Li-Ta Loe5266692004-03-23 21:28:05 +0000783
Yinghai Lu5f9624d2006-10-04 22:56:21 +0000784 /* Look through the list of setup drivers and find one for
Myles Watson29cc9ed2009-07-02 18:56:24 +0000785 * this PCI device.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000786 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000787 for (driver = &pci_drivers[0]; driver != &epci_drivers[0]; driver++) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000788 if ((driver->vendor == dev->vendor) &&
Myles Watson29cc9ed2009-07-02 18:56:24 +0000789 (driver->device == dev->device)) {
Uwe Hermann312673c2009-10-27 21:49:33 +0000790 dev->ops = (struct device_operations *)driver->ops;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000791 printk(BIOS_SPEW, "%s [%04x/%04x] %sops\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000792 dev_path(dev),
793 driver->vendor, driver->device,
794 (driver->ops->scan_bus ? "bus " : ""));
Eric Biederman5899fd82003-04-24 06:25:08 +0000795 return;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000796 }
797 }
Li-Ta Loe5266692004-03-23 21:28:05 +0000798
Eric Biederman8ca8d762003-04-22 19:02:15 +0000799 /* If I don't have a specific driver use the default operations */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000800 switch (dev->hdr_type & 0x7f) { /* header type */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000801 case PCI_HEADER_TYPE_NORMAL: /* standard header */
802 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
803 goto bad;
804 dev->ops = &default_pci_ops_dev;
805 break;
806 case PCI_HEADER_TYPE_BRIDGE:
807 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
808 goto bad;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000809 dev->ops = get_pci_bridge_ops(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000810 break;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000811#if CONFIG_CARDBUS_PLUGIN_SUPPORT == 1
812 case PCI_HEADER_TYPE_CARDBUS:
813 dev->ops = &default_cardbus_ops_bus;
814 break;
815#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +0000816 default:
Myles Watson29cc9ed2009-07-02 18:56:24 +0000817 bad:
Li-Ta Lo69c5a902004-04-29 20:08:54 +0000818 if (dev->enabled) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000819 printk(BIOS_ERR, "%s [%04x/%04x/%06x] has unknown header "
Myles Watson29cc9ed2009-07-02 18:56:24 +0000820 "type %02x, ignoring.\n",
821 dev_path(dev),
822 dev->vendor, dev->device,
823 dev->class >> 8, dev->hdr_type);
Eric Biederman83b991a2003-10-11 06:20:25 +0000824 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000825 }
826 return;
827}
828
829/**
Eric Biederman03acab62004-10-14 21:25:53 +0000830 * @brief See if we have already allocated a device structure for a given devfn.
Li-Ta Loe5266692004-03-23 21:28:05 +0000831 *
832 * Given a linked list of PCI device structures and a devfn number, find the
Li-Ta Lo3a812852004-12-03 22:39:34 +0000833 * device structure correspond to the devfn, if present. This function also
834 * removes the device structure from the linked list.
Li-Ta Loe5266692004-03-23 21:28:05 +0000835 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000836 * @param list The device structure list.
837 * @param devfn A device/function number.
Li-Ta Loe5266692004-03-23 21:28:05 +0000838 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000839 * @return Pointer to the device structure found or NULL if we have not
Li-Ta Lo3a812852004-12-03 22:39:34 +0000840 * allocated a device for this devfn yet.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000841 */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000842static struct device *pci_scan_get_dev(struct device **list, unsigned int devfn)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000843{
Eric Biedermanb78c1972004-10-14 20:54:17 +0000844 struct device *dev;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000845 dev = 0;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000846 for (; *list; list = &(*list)->sibling) {
Eric Biedermanad1b35a2003-10-14 02:36:51 +0000847 if ((*list)->path.type != DEVICE_PATH_PCI) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000848 printk(BIOS_ERR, "child %s not a pci device\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000849 dev_path(*list));
Eric Biedermanad1b35a2003-10-14 02:36:51 +0000850 continue;
851 }
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000852 if ((*list)->path.pci.devfn == devfn) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000853 /* Unlink from the list. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000854 dev = *list;
855 *list = (*list)->sibling;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000856 dev->sibling = NULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000857 break;
858 }
859 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000860
861 /* Just like alloc_dev() add the device to the list of devices on the
862 * bus. When the list of devices was formed we removed all of the
863 * parents children, and now we are interleaving static and dynamic
864 * devices in order on the bus.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000865 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000866 if (dev) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000867 struct device *child;
868 /* Find the last child of our parent. */
869 for (child = dev->bus->children; child && child->sibling;) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000870 child = child->sibling;
871 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000872 /* Place the device on the list of children of its parent. */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000873 if (child) {
874 child->sibling = dev;
875 } else {
876 dev->bus->children = dev;
877 }
878 }
879
Eric Biederman8ca8d762003-04-22 19:02:15 +0000880 return dev;
881}
882
Myles Watson032a9652009-05-11 22:24:53 +0000883/**
Eric Biedermanb78c1972004-10-14 20:54:17 +0000884 * @brief Scan a PCI bus.
Li-Ta Loe5266692004-03-23 21:28:05 +0000885 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000886 * Determine the existence of a given PCI device. Allocate a new struct device
887 * if dev==NULL was passed in and the device exists in hardware.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000888 *
889 * @param bus pointer to the bus structure
890 * @param devfn to look at
891 *
892 * @return The device structure for hte device (if found)
893 * or the NULL if no device is found.
894 */
Myles Watson29cc9ed2009-07-02 18:56:24 +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/**
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000992 * @brief Scan a PCI bus.
993 *
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 *
Eric Biedermane9a271e32003-09-02 03:36:25 +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
Eric Biederman8ca8d762003-04-22 19:02:15 +00001003 * @param max current bus number
Li-Ta Loe5266692004-03-23 21:28:05 +00001004 *
Eric Biederman8ca8d762003-04-22 19:02:15 +00001005 * @return The maximum bus number found, after scanning all subordinate busses
1006 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001007unsigned int pci_scan_bus(struct bus *bus,
Myles Watson29cc9ed2009-07-02 18:56:24 +00001008 unsigned min_devfn, unsigned max_devfn,
1009 unsigned int max)
Eric Biederman8ca8d762003-04-22 19:02:15 +00001010{
1011 unsigned int devfn;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001012 struct device *old_devices;
1013 struct device *child;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001014
Stefan Reinauer08670622009-06-30 15:17:49 +00001015#if CONFIG_PCI_BUS_SEGN_BITS
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001016 printk(BIOS_DEBUG, "PCI: pci_scan_bus for bus %04x:%02x\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +00001017 bus->secondary >> 8, bus->secondary & 0xff);
Yinghai Lu5f9624d2006-10-04 22:56:21 +00001018#else
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001019 printk(BIOS_DEBUG, "PCI: pci_scan_bus for bus %02x\n", bus->secondary);
Yinghai Lu5f9624d2006-10-04 22:56:21 +00001020#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +00001021
Juhana Helovuo50b78b62010-09-13 14:43:02 +00001022 // Maximum sane devfn is 0xFF
1023 if (max_devfn > 0xff) {
1024 printk(BIOS_ERR, "PCI: pci_scan_bus limits devfn %x - devfn %x\n",
1025 min_devfn, max_devfn );
1026 printk(BIOS_ERR, "PCI: pci_scan_bus upper limit too big. Using 0xff.\n");
1027 max_devfn=0xff;
1028 }
1029
Eric Biederman8ca8d762003-04-22 19:02:15 +00001030 old_devices = bus->children;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001031 bus->children = NULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001032
1033 post_code(0x24);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001034 /* Probe all devices/functions on this bus with some optimization for
1035 * non-existence and single function devices.
Eric Biedermanb78c1972004-10-14 20:54:17 +00001036 */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001037 for (devfn = min_devfn; devfn <= max_devfn; devfn++) {
Myles Watson29cc9ed2009-07-02 18:56:24 +00001038 struct device *dev;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001039
Eric Biederman03acab62004-10-14 21:25:53 +00001040 /* First thing setup the device structure */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001041 dev = pci_scan_get_dev(&old_devices, devfn);
Li-Ta Lo9782f752004-05-05 21:15:42 +00001042
Myles Watson29cc9ed2009-07-02 18:56:24 +00001043 /* See if a device is present and setup the device structure. */
Myles Watson032a9652009-05-11 22:24:53 +00001044 dev = pci_probe_dev(dev, bus, devfn);
Eric Biederman03acab62004-10-14 21:25:53 +00001045
Myles Watson29cc9ed2009-07-02 18:56:24 +00001046 /* If this is not a multi function device, or the device is
1047 * not present don't waste time probing another function.
Myles Watson032a9652009-05-11 22:24:53 +00001048 * Skip to next device.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001049 */
Myles Watson032a9652009-05-11 22:24:53 +00001050 if ((PCI_FUNC(devfn) == 0x00) &&
Myles Watson29cc9ed2009-07-02 18:56:24 +00001051 (!dev
1052 || (dev->enabled && ((dev->hdr_type & 0x80) != 0x80)))) {
Eric Biederman8ca8d762003-04-22 19:02:15 +00001053 devfn += 0x07;
1054 }
1055 }
1056 post_code(0x25);
1057
Myles Watson29cc9ed2009-07-02 18:56:24 +00001058 /* Warn if any leftover static devices are are found.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001059 * There's probably a problem in the Config.lb.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001060 */
1061 if (old_devices) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001062 device_t left;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001063 printk(BIOS_WARNING, "PCI: Left over static devices:\n");
Myles Watson29cc9ed2009-07-02 18:56:24 +00001064 for (left = old_devices; left; left = left->sibling) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001065 printk(BIOS_WARNING, "%s\n", dev_path(left));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001066 }
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001067 printk(BIOS_WARNING, "PCI: Check your mainboard Config.lb.\n");
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001068 }
1069
Myles Watson29cc9ed2009-07-02 18:56:24 +00001070 /* For all children that implement scan_bus() (i.e. bridges)
Eric Biedermanb78c1972004-10-14 20:54:17 +00001071 * scan the bus behind that child.
1072 */
Myles Watson29cc9ed2009-07-02 18:56:24 +00001073 for (child = bus->children; child; child = child->sibling) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001074 max = scan_bus(child, max);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001075 }
Li-Ta Loe5266692004-03-23 21:28:05 +00001076
Myles Watson29cc9ed2009-07-02 18:56:24 +00001077 /* We've scanned the bus and so we know all about what's on the other
1078 * side of any bridges that may be on this bus plus any devices.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001079 * Return how far we've got finding sub-buses.
1080 */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001081 printk(BIOS_DEBUG, "PCI: pci_scan_bus returning with max=%03x\n", max);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001082 post_code(0x55);
1083 return max;
1084}
1085
Li-Ta Loe5266692004-03-23 21:28:05 +00001086/**
1087 * @brief Scan a PCI bridge and the buses behind the bridge.
1088 *
1089 * Determine the existence of buses behind the bridge. Set up the bridge
1090 * according to the result of the scan.
1091 *
1092 * This function is the default scan_bus() method for PCI bridge devices.
1093 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001094 * @param dev Pointer to the bridge device.
1095 * @param max The highest bus number assigned up to now.
1096 * @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/**
1167 * @brief Scan a PCI bridge and the buses behind the bridge.
1168 *
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/**
1184 * @brief Scan a PCI domain.
1185 *
1186 * This function is the default scan_bus() method for PCI domains.
1187 *
1188 * @param dev pointer to the domain
1189 * @param max the highest bus number assgined up to now
1190 *
1191 * @return The maximum bus number found, after scanning all subordinate busses
1192 */
1193unsigned int pci_domain_scan_bus(device_t dev, unsigned int max)
1194{
Myles Watson894a3472010-06-09 22:41:35 +00001195 max = pci_scan_bus(dev->link_list, PCI_DEVFN(0, 0), 0xff, max);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001196 return max;
1197}
1198
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001199#if CONFIG_PC80_SYSTEM == 1
Myles Watson29cc9ed2009-07-02 18:56:24 +00001200/**
Stefan Reinauer14e22772010-04-27 06:56:47 +00001201 *
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001202 * @brief Assign IRQ numbers
Myles Watson29cc9ed2009-07-02 18:56:24 +00001203 *
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001204 * This function assigns IRQs for all functions contained within the indicated
1205 * device address. If the device does not exist or does not require interrupts
1206 * then this function has no effect.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001207 *
1208 * This function should be called for each PCI slot in your system.
1209 *
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001210 * @param bus
1211 * @param slot
1212 * @param pIntAtoD is an array of IRQ #s that are assigned to PINTA through
Stefan Reinauer14e22772010-04-27 06:56:47 +00001213 * PINTD of this slot. The particular irq #s that are passed in
1214 * depend on the routing inside your southbridge and on your
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001215 * motherboard.
1216 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001217void pci_assign_irqs(unsigned bus, unsigned slot,
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001218 const unsigned char pIntAtoD[4])
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001219{
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001220 unsigned int funct;
1221 device_t pdev;
1222 u8 line;
1223 u8 irq;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001224
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001225 /* Each slot may contain up to eight functions */
1226 for (funct = 0; funct < 8; funct++) {
1227 pdev = dev_find_slot(bus, (slot << 3) + funct);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001228
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001229 if (!pdev)
1230 continue;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001231
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001232 line = pci_read_config8(pdev, PCI_INTERRUPT_PIN);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001233
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001234 // PCI spec says all values except 1..4 are reserved.
1235 if ((line < 1) || (line > 4))
1236 continue;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001237
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001238 irq = pIntAtoD[line - 1];
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001239
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001240 printk(BIOS_DEBUG, "Assigning IRQ %d to %d:%x.%d\n",
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001241 irq, bus, slot, funct);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001242
Stefan Reinauer14e22772010-04-27 06:56:47 +00001243 pci_write_config8(pdev, PCI_INTERRUPT_LINE,
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001244 pIntAtoD[line - 1]);
1245
1246#ifdef PARANOID_IRQ_ASSIGNMENTS
Myles Watson17aeeca2009-10-07 18:41:08 +00001247 irq = pci_read_config8(pdev, PCI_INTERRUPT_LINE);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001248 printk(BIOS_DEBUG, " Readback = %d\n", irq);
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001249#endif
1250
1251 // Change to level triggered
1252 i8259_configure_irq_trigger(pIntAtoD[line - 1], IRQ_LEVEL_TRIGGERED);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001253 }
1254}
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001255#endif
1256