blob: fbe8b335af72662d6cc4d9f4e09bde21e43f18cf [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{
553 struct resource *resource, *last;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000554 unsigned link;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000555 u8 line;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000556
557 last = &dev->resource[dev->resources];
Eric Biedermanb78c1972004-10-14 20:54:17 +0000558
Myles Watson29cc9ed2009-07-02 18:56:24 +0000559 for (resource = &dev->resource[0]; resource < last; resource++) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000560 pci_set_resource(dev, resource);
561 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000562 for (link = 0; link < dev->links; link++) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000563 struct bus *bus;
564 bus = &dev->link[link];
565 if (bus->children) {
566 assign_resources(bus);
567 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000568 }
569
Myles Watson29cc9ed2009-07-02 18:56:24 +0000570 /* Set a default latency timer. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000571 pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000572
Myles Watson29cc9ed2009-07-02 18:56:24 +0000573 /* Set a default secondary latency timer. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000574 if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
Eric Biederman7a5416a2003-06-12 19:23:51 +0000575 pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000576 }
577
Myles Watson29cc9ed2009-07-02 18:56:24 +0000578 /* Zero the IRQ settings. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000579 line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000580 if (line) {
Eric Biederman7a5416a2003-06-12 19:23:51 +0000581 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000582 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000583 /* Set the cache line size, so far 64 bytes is good for everyone. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000584 pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000585}
586
Eric Biedermane9a271e32003-09-02 03:36:25 +0000587void pci_dev_enable_resources(struct device *dev)
588{
Eric Biedermana9e632c2004-11-18 22:38:08 +0000589 const struct pci_operations *ops;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000590 u16 command;
Eric Biederman03acab62004-10-14 21:25:53 +0000591
Myles Watson29cc9ed2009-07-02 18:56:24 +0000592 /* Set the subsystem vendor and device id for mainboard devices. */
Eric Biederman03acab62004-10-14 21:25:53 +0000593 ops = ops_pci(dev);
Eric Biedermandbec2d42004-10-21 10:44:08 +0000594 if (dev->on_mainboard && ops && ops->set_subsystem) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000595 printk(BIOS_DEBUG, "%s subsystem <- %02x/%02x\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000596 dev_path(dev),
597 CONFIG_MAINBOARD_PCI_SUBSYSTEM_VENDOR_ID,
598 CONFIG_MAINBOARD_PCI_SUBSYSTEM_DEVICE_ID);
Myles Watson032a9652009-05-11 22:24:53 +0000599 ops->set_subsystem(dev,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000600 CONFIG_MAINBOARD_PCI_SUBSYSTEM_VENDOR_ID,
601 CONFIG_MAINBOARD_PCI_SUBSYSTEM_DEVICE_ID);
Eric Biederman03acab62004-10-14 21:25:53 +0000602 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000603 command = pci_read_config16(dev, PCI_COMMAND);
604 command |= dev->command;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000605 /* v3 has
606 * command |= (PCI_COMMAND_PARITY + PCI_COMMAND_SERR); // Error check.
607 */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000608 printk(BIOS_DEBUG, "%s cmd <- %02x\n", dev_path(dev), command);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000609 pci_write_config16(dev, PCI_COMMAND, command);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000610}
611
612void pci_bus_enable_resources(struct device *dev)
613{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000614 u16 ctrl;
615
616 /* Enable I/O in command register if there is VGA card
617 * connected with (even it does not claim I/O resource).
618 */
Li-Ta Lo515f6c72005-01-11 22:48:54 +0000619 if (dev->link[0].bridge_ctrl & PCI_BRIDGE_CTL_VGA)
620 dev->command |= PCI_COMMAND_IO;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000621 ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
622 ctrl |= dev->link[0].bridge_ctrl;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000623 ctrl |= (PCI_BRIDGE_CTL_PARITY + PCI_BRIDGE_CTL_SERR); /* Error check. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000624 printk(BIOS_DEBUG, "%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000625 pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
626
627 pci_dev_enable_resources(dev);
Eric Biedermandbec2d42004-10-21 10:44:08 +0000628 enable_childrens_resources(dev);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000629}
630
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000631void pci_bus_reset(struct bus *bus)
632{
633 unsigned ctl;
634 ctl = pci_read_config16(bus->dev, PCI_BRIDGE_CONTROL);
635 ctl |= PCI_BRIDGE_CTL_BUS_RESET;
636 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
637 mdelay(10);
638 ctl &= ~PCI_BRIDGE_CTL_BUS_RESET;
639 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
640 delay(1);
641}
642
Myles Watson29cc9ed2009-07-02 18:56:24 +0000643void pci_dev_set_subsystem(struct device *dev, unsigned vendor, unsigned device)
Eric Biederman03acab62004-10-14 21:25:53 +0000644{
Myles Watson032a9652009-05-11 22:24:53 +0000645 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000646 ((device & 0xffff) << 16) | (vendor & 0xffff));
Eric Biederman03acab62004-10-14 21:25:53 +0000647}
648
Torsten Duwe1f2f8002008-01-06 01:10:54 +0000649/** default handler: only runs the relevant pci bios. */
Li-Ta Lo883b8792005-01-10 23:16:22 +0000650void pci_dev_init(struct device *dev)
651{
Torsten Duwe1f2f8002008-01-06 01:10:54 +0000652#if CONFIG_PCI_ROM_RUN == 1 || CONFIG_VGA_ROM_RUN == 1
Li-Ta Lo883b8792005-01-10 23:16:22 +0000653 struct rom_header *rom, *ram;
654
Myles Watson17aeeca2009-10-07 18:41:08 +0000655 if (CONFIG_PCI_ROM_RUN != 1 && /* Only execute VGA ROMs. */
656 ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA))
Roman Kononov778a42b2007-04-06 18:34:39 +0000657 return;
Myles Watson17aeeca2009-10-07 18:41:08 +0000658
659 if (CONFIG_VGA_ROM_RUN != 1 && /* Only execute non-VGA ROMs. */
660 ((dev->class >> 8) == PCI_CLASS_DISPLAY_VGA))
661 return;
Roman Kononov778a42b2007-04-06 18:34:39 +0000662
Li-Ta Lo883b8792005-01-10 23:16:22 +0000663 rom = pci_rom_probe(dev);
664 if (rom == NULL)
665 return;
Roman Kononov778a42b2007-04-06 18:34:39 +0000666
Li-Ta Lo883b8792005-01-10 23:16:22 +0000667 ram = pci_rom_load(dev, rom);
Yinghai Lu9e4faef2005-01-14 22:04:49 +0000668 if (ram == NULL)
669 return;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000670
Stefan Reinauerd98cf5b2008-08-01 11:25:41 +0000671 run_bios(dev, (unsigned long)ram);
Roman Kononov778a42b2007-04-06 18:34:39 +0000672
673#if CONFIG_CONSOLE_VGA == 1
Luc Verhaegen5c5beb72009-05-29 03:04:16 +0000674 if ((dev->class>>8) == PCI_CLASS_DISPLAY_VGA)
Luc Verhaegen43bc5a9c2009-05-29 03:44:47 +0000675 vga_console_init();
Torsten Duwe1f2f8002008-01-06 01:10:54 +0000676#endif /* CONFIG_CONSOLE_VGA */
677#endif /* CONFIG_PCI_ROM_RUN || CONFIG_VGA_ROM_RUN */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000678}
Li-Ta Lo883b8792005-01-10 23:16:22 +0000679
Li-Ta Loe5266692004-03-23 21:28:05 +0000680/** Default device operation for PCI devices */
Eric Biedermana9e632c2004-11-18 22:38:08 +0000681static struct pci_operations pci_dev_ops_pci = {
Eric Biederman03acab62004-10-14 21:25:53 +0000682 .set_subsystem = pci_dev_set_subsystem,
683};
684
Eric Biederman8ca8d762003-04-22 19:02:15 +0000685struct device_operations default_pci_ops_dev = {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000686 .read_resources = pci_dev_read_resources,
687 .set_resources = pci_dev_set_resources,
Eric Biedermane9a271e32003-09-02 03:36:25 +0000688 .enable_resources = pci_dev_enable_resources,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000689 .init = pci_dev_init,
690 .scan_bus = 0,
691 .enable = 0,
692 .ops_pci = &pci_dev_ops_pci,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000693};
Li-Ta Loe5266692004-03-23 21:28:05 +0000694
695/** Default device operations for PCI bridges */
Eric Biedermana9e632c2004-11-18 22:38:08 +0000696static struct pci_operations pci_bus_ops_pci = {
Eric Biederman03acab62004-10-14 21:25:53 +0000697 .set_subsystem = 0,
698};
Li-Ta Lo883b8792005-01-10 23:16:22 +0000699
Eric Biederman8ca8d762003-04-22 19:02:15 +0000700struct device_operations default_pci_ops_bus = {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000701 .read_resources = pci_bus_read_resources,
702 .set_resources = pci_dev_set_resources,
Eric Biedermane9a271e32003-09-02 03:36:25 +0000703 .enable_resources = pci_bus_enable_resources,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000704 .init = 0,
705 .scan_bus = pci_scan_bridge,
706 .enable = 0,
707 .reset_bus = pci_bus_reset,
708 .ops_pci = &pci_bus_ops_pci,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000709};
Li-Ta Loe5266692004-03-23 21:28:05 +0000710
711/**
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000712 * @brief Detect the type of downstream bridge
713 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000714 * This function is a heuristic to detect which type of bus is downstream
715 * of a PCI-to-PCI bridge. This functions by looking for various capability
716 * blocks to figure out the type of downstream bridge. PCI-X, PCI-E, and
717 * Hypertransport all seem to have appropriate capabilities.
Myles Watson032a9652009-05-11 22:24:53 +0000718 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000719 * When only a PCI-Express capability is found the type
720 * is examined to see which type of bridge we have.
721 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000722 * @param dev Pointer to the device structure of the bridge.
723 * @return Appropriate bridge operations.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000724 */
725static struct device_operations *get_pci_bridge_ops(device_t dev)
726{
727 unsigned pos;
728
729#if CONFIG_PCIX_PLUGIN_SUPPORT == 1
730 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
731 if (pos) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000732 printk(BIOS_DEBUG, "%s subordinate bus PCI-X\n", dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000733 return &default_pcix_ops_bus;
734 }
735#endif
736#if CONFIG_AGP_PLUGIN_SUPPORT == 1
737 /* How do I detect an PCI to AGP bridge? */
738#endif
739#if CONFIG_HYPERTRANSPORT_PLUGIN_SUPPORT == 1
740 pos = 0;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000741 while ((pos = pci_find_next_capability(dev, PCI_CAP_ID_HT, pos))) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000742 unsigned flags;
743 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
744 if ((flags >> 13) == 1) {
745 /* Host or Secondary Interface */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000746 printk(BIOS_DEBUG, "%s subordinate bus Hypertransport\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000747 dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000748 return &default_ht_ops_bus;
749 }
750 }
751#endif
752#if CONFIG_PCIEXP_PLUGIN_SUPPORT == 1
753 pos = pci_find_capability(dev, PCI_CAP_ID_PCIE);
754 if (pos) {
755 unsigned flags;
756 flags = pci_read_config16(dev, pos + PCI_EXP_FLAGS);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000757 switch ((flags & PCI_EXP_FLAGS_TYPE) >> 4) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000758 case PCI_EXP_TYPE_ROOT_PORT:
759 case PCI_EXP_TYPE_UPSTREAM:
760 case PCI_EXP_TYPE_DOWNSTREAM:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000761 printk(BIOS_DEBUG, "%s subordinate bus PCI Express\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000762 dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000763 return &default_pciexp_ops_bus;
764 case PCI_EXP_TYPE_PCI_BRIDGE:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000765 printk(BIOS_DEBUG, "%s subordinate PCI\n", dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000766 return &default_pci_ops_bus;
767 default:
768 break;
769 }
770 }
771#endif
772 return &default_pci_ops_bus;
773}
774
775/**
Myles Watson29cc9ed2009-07-02 18:56:24 +0000776 * Set up PCI device operation. Check if it already has a driver. If not, use
777 * find_device_operations, or set to a default based on type.
Li-Ta Loe5266692004-03-23 21:28:05 +0000778 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000779 * @param dev Pointer to the device whose pci_ops you want to set.
Li-Ta Loe5266692004-03-23 21:28:05 +0000780 * @see pci_drivers
781 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000782static void set_pci_ops(struct device *dev)
783{
784 struct pci_driver *driver;
785 if (dev->ops) {
786 return;
787 }
Li-Ta Loe5266692004-03-23 21:28:05 +0000788
Yinghai Lu5f9624d2006-10-04 22:56:21 +0000789 /* Look through the list of setup drivers and find one for
Myles Watson29cc9ed2009-07-02 18:56:24 +0000790 * this PCI device.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000791 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000792 for (driver = &pci_drivers[0]; driver != &epci_drivers[0]; driver++) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000793 if ((driver->vendor == dev->vendor) &&
Myles Watson29cc9ed2009-07-02 18:56:24 +0000794 (driver->device == dev->device)) {
Uwe Hermann312673c2009-10-27 21:49:33 +0000795 dev->ops = (struct device_operations *)driver->ops;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000796 printk(BIOS_SPEW, "%s [%04x/%04x] %sops\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000797 dev_path(dev),
798 driver->vendor, driver->device,
799 (driver->ops->scan_bus ? "bus " : ""));
Eric Biederman5899fd82003-04-24 06:25:08 +0000800 return;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000801 }
802 }
Li-Ta Loe5266692004-03-23 21:28:05 +0000803
Eric Biederman8ca8d762003-04-22 19:02:15 +0000804 /* If I don't have a specific driver use the default operations */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000805 switch (dev->hdr_type & 0x7f) { /* header type */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000806 case PCI_HEADER_TYPE_NORMAL: /* standard header */
807 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
808 goto bad;
809 dev->ops = &default_pci_ops_dev;
810 break;
811 case PCI_HEADER_TYPE_BRIDGE:
812 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
813 goto bad;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000814 dev->ops = get_pci_bridge_ops(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000815 break;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000816#if CONFIG_CARDBUS_PLUGIN_SUPPORT == 1
817 case PCI_HEADER_TYPE_CARDBUS:
818 dev->ops = &default_cardbus_ops_bus;
819 break;
820#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +0000821 default:
Myles Watson29cc9ed2009-07-02 18:56:24 +0000822 bad:
Li-Ta Lo69c5a902004-04-29 20:08:54 +0000823 if (dev->enabled) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000824 printk(BIOS_ERR, "%s [%04x/%04x/%06x] has unknown header "
Myles Watson29cc9ed2009-07-02 18:56:24 +0000825 "type %02x, ignoring.\n",
826 dev_path(dev),
827 dev->vendor, dev->device,
828 dev->class >> 8, dev->hdr_type);
Eric Biederman83b991a2003-10-11 06:20:25 +0000829 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000830 }
831 return;
832}
833
834/**
Eric Biederman03acab62004-10-14 21:25:53 +0000835 * @brief See if we have already allocated a device structure for a given devfn.
Li-Ta Loe5266692004-03-23 21:28:05 +0000836 *
837 * Given a linked list of PCI device structures and a devfn number, find the
Li-Ta Lo3a812852004-12-03 22:39:34 +0000838 * device structure correspond to the devfn, if present. This function also
839 * removes the device structure from the linked list.
Li-Ta Loe5266692004-03-23 21:28:05 +0000840 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000841 * @param list The device structure list.
842 * @param devfn A device/function number.
Li-Ta Loe5266692004-03-23 21:28:05 +0000843 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000844 * @return Pointer to the device structure found or NULL if we have not
Li-Ta Lo3a812852004-12-03 22:39:34 +0000845 * allocated a device for this devfn yet.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000846 */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000847static struct device *pci_scan_get_dev(struct device **list, unsigned int devfn)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000848{
Eric Biedermanb78c1972004-10-14 20:54:17 +0000849 struct device *dev;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000850 dev = 0;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000851 for (; *list; list = &(*list)->sibling) {
Eric Biedermanad1b35a2003-10-14 02:36:51 +0000852 if ((*list)->path.type != DEVICE_PATH_PCI) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000853 printk(BIOS_ERR, "child %s not a pci device\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000854 dev_path(*list));
Eric Biedermanad1b35a2003-10-14 02:36:51 +0000855 continue;
856 }
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000857 if ((*list)->path.pci.devfn == devfn) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000858 /* Unlink from the list. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000859 dev = *list;
860 *list = (*list)->sibling;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000861 dev->sibling = NULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000862 break;
863 }
864 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000865
866 /* Just like alloc_dev() add the device to the list of devices on the
867 * bus. When the list of devices was formed we removed all of the
868 * parents children, and now we are interleaving static and dynamic
869 * devices in order on the bus.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000870 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000871 if (dev) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000872 struct device *child;
873 /* Find the last child of our parent. */
874 for (child = dev->bus->children; child && child->sibling;) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000875 child = child->sibling;
876 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000877 /* Place the device on the list of children of its parent. */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000878 if (child) {
879 child->sibling = dev;
880 } else {
881 dev->bus->children = dev;
882 }
883 }
884
Eric Biederman8ca8d762003-04-22 19:02:15 +0000885 return dev;
886}
887
Myles Watson032a9652009-05-11 22:24:53 +0000888/**
Eric Biedermanb78c1972004-10-14 20:54:17 +0000889 * @brief Scan a PCI bus.
Li-Ta Loe5266692004-03-23 21:28:05 +0000890 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000891 * Determine the existence of a given PCI device. Allocate a new struct device
892 * if dev==NULL was passed in and the device exists in hardware.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000893 *
894 * @param bus pointer to the bus structure
895 * @param devfn to look at
896 *
897 * @return The device structure for hte device (if found)
898 * or the NULL if no device is found.
899 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000900device_t pci_probe_dev(device_t dev, struct bus * bus, unsigned devfn)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000901{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000902 u32 id, class;
903 u8 hdr_type;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000904
Myles Watson29cc9ed2009-07-02 18:56:24 +0000905 /* Detect if a device is present. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000906 if (!dev) {
907 struct device dummy;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000908 dummy.bus = bus;
909 dummy.path.type = DEVICE_PATH_PCI;
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000910 dummy.path.pci.devfn = devfn;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000911 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000912 /* Have we found something?
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000913 * Some broken boards return 0 if a slot is empty.
914 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000915 if ((id == 0xffffffff) || (id == 0x00000000) ||
916 (id == 0x0000ffff) || (id == 0xffff0000)) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000917 printk(BIOS_SPEW, "%s, bad id 0x%x\n", dev_path(&dummy), id);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000918 return NULL;
919 }
920 dev = alloc_dev(bus, &dummy.path);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000921 } else {
922 /* Enable/disable the device. Once we have found the device-
923 * specific operations this operations we will disable the
924 * device with those as well.
Myles Watson032a9652009-05-11 22:24:53 +0000925 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000926 * This is geared toward devices that have subfunctions
927 * that do not show up by default.
Myles Watson032a9652009-05-11 22:24:53 +0000928 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000929 * If a device is a stuff option on the motherboard
Myles Watson29cc9ed2009-07-02 18:56:24 +0000930 * it may be absent and enable_dev() must cope.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000931 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000932 /* Run the magic enable sequence for the device. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000933 if (dev->chip_ops && dev->chip_ops->enable_dev) {
934 dev->chip_ops->enable_dev(dev);
935 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000936 /* Now read the vendor and device ID. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000937 id = pci_read_config32(dev, PCI_VENDOR_ID);
Myles Watson032a9652009-05-11 22:24:53 +0000938
Myles Watson29cc9ed2009-07-02 18:56:24 +0000939 /* If the device does not have a PCI ID disable it. Possibly
940 * this is because we have already disabled the device. But
941 * this also handles optional devices that may not always
942 * show up.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000943 */
944 /* If the chain is fully enumerated quit */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000945 if ((id == 0xffffffff) || (id == 0x00000000) ||
946 (id == 0x0000ffff) || (id == 0xffff0000)) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000947 if (dev->enabled) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000948 printk(BIOS_INFO, "PCI: Static device %s not found, disabling it.\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000949 dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000950 dev->enabled = 0;
951 }
952 return dev;
953 }
954 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000955 /* Read the rest of the PCI configuration information. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000956 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
957 class = pci_read_config32(dev, PCI_CLASS_REVISION);
Myles Watson032a9652009-05-11 22:24:53 +0000958
Myles Watson29cc9ed2009-07-02 18:56:24 +0000959 /* Store the interesting information in the device structure. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000960 dev->vendor = id & 0xffff;
961 dev->device = (id >> 16) & 0xffff;
962 dev->hdr_type = hdr_type;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000963
964 /* Class code, the upper 3 bytes of PCI_CLASS_REVISION. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000965 dev->class = class >> 8;
Myles Watson032a9652009-05-11 22:24:53 +0000966
Myles Watson29cc9ed2009-07-02 18:56:24 +0000967 /* Architectural/System devices always need to be bus masters. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000968 if ((dev->class >> 16) == PCI_BASE_CLASS_SYSTEM) {
969 dev->command |= PCI_COMMAND_MASTER;
970 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000971 /* Look at the vendor and device ID, or at least the header type and
972 * class and figure out which set of configuration methods to use.
973 * Unless we already have some PCI ops.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000974 */
975 set_pci_ops(dev);
976
Myles Watson29cc9ed2009-07-02 18:56:24 +0000977 /* Now run the magic enable/disable sequence for the device. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000978 if (dev->ops && dev->ops->enable) {
979 dev->ops->enable(dev);
980 }
Myles Watson032a9652009-05-11 22:24:53 +0000981
Myles Watson29cc9ed2009-07-02 18:56:24 +0000982 /* Display the device. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000983 printk(BIOS_DEBUG, "%s [%04x/%04x] %s%s\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000984 dev_path(dev),
985 dev->vendor, dev->device,
986 dev->enabled ? "enabled" : "disabled",
987 dev->ops ? "" : " No operations");
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000988
989 return dev;
990}
991
Myles Watson032a9652009-05-11 22:24:53 +0000992/**
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000993 * @brief Scan a PCI bus.
994 *
Li-Ta Loe5266692004-03-23 21:28:05 +0000995 * Determine the existence of devices and bridges on a PCI bus. If there are
996 * bridges on the bus, recursively scan the buses behind the bridges.
997 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000998 * This function is the default scan_bus() method for the root device
999 * 'dev_root'.
1000 *
Eric Biedermane9a271e32003-09-02 03:36:25 +00001001 * @param bus pointer to the bus structure
1002 * @param min_devfn minimum devfn to look at in the scan usually 0x00
1003 * @param max_devfn maximum devfn to look at in the scan usually 0xff
Eric Biederman8ca8d762003-04-22 19:02:15 +00001004 * @param max current bus number
Li-Ta Loe5266692004-03-23 21:28:05 +00001005 *
Eric Biederman8ca8d762003-04-22 19:02:15 +00001006 * @return The maximum bus number found, after scanning all subordinate busses
1007 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001008unsigned int pci_scan_bus(struct bus *bus,
Myles Watson29cc9ed2009-07-02 18:56:24 +00001009 unsigned min_devfn, unsigned max_devfn,
1010 unsigned int max)
Eric Biederman8ca8d762003-04-22 19:02:15 +00001011{
1012 unsigned int devfn;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001013 struct device *old_devices;
1014 struct device *child;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001015
Stefan Reinauer08670622009-06-30 15:17:49 +00001016#if CONFIG_PCI_BUS_SEGN_BITS
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001017 printk(BIOS_DEBUG, "PCI: pci_scan_bus for bus %04x:%02x\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +00001018 bus->secondary >> 8, bus->secondary & 0xff);
Yinghai Lu5f9624d2006-10-04 22:56:21 +00001019#else
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001020 printk(BIOS_DEBUG, "PCI: pci_scan_bus for bus %02x\n", bus->secondary);
Yinghai Lu5f9624d2006-10-04 22:56:21 +00001021#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +00001022
1023 old_devices = bus->children;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001024 bus->children = NULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001025
1026 post_code(0x24);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001027 /* Probe all devices/functions on this bus with some optimization for
1028 * non-existence and single function devices.
Eric Biedermanb78c1972004-10-14 20:54:17 +00001029 */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001030 for (devfn = min_devfn; devfn <= max_devfn; devfn++) {
Myles Watson29cc9ed2009-07-02 18:56:24 +00001031 struct device *dev;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001032
Eric Biederman03acab62004-10-14 21:25:53 +00001033 /* First thing setup the device structure */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001034 dev = pci_scan_get_dev(&old_devices, devfn);
Li-Ta Lo9782f752004-05-05 21:15:42 +00001035
Myles Watson29cc9ed2009-07-02 18:56:24 +00001036 /* See if a device is present and setup the device structure. */
Myles Watson032a9652009-05-11 22:24:53 +00001037 dev = pci_probe_dev(dev, bus, devfn);
Eric Biederman03acab62004-10-14 21:25:53 +00001038
Myles Watson29cc9ed2009-07-02 18:56:24 +00001039 /* If this is not a multi function device, or the device is
1040 * not present don't waste time probing another function.
Myles Watson032a9652009-05-11 22:24:53 +00001041 * Skip to next device.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001042 */
Myles Watson032a9652009-05-11 22:24:53 +00001043 if ((PCI_FUNC(devfn) == 0x00) &&
Myles Watson29cc9ed2009-07-02 18:56:24 +00001044 (!dev
1045 || (dev->enabled && ((dev->hdr_type & 0x80) != 0x80)))) {
Eric Biederman8ca8d762003-04-22 19:02:15 +00001046 devfn += 0x07;
1047 }
1048 }
1049 post_code(0x25);
1050
Myles Watson29cc9ed2009-07-02 18:56:24 +00001051 /* Warn if any leftover static devices are are found.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001052 * There's probably a problem in the Config.lb.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001053 */
1054 if (old_devices) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001055 device_t left;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001056 printk(BIOS_WARNING, "PCI: Left over static devices:\n");
Myles Watson29cc9ed2009-07-02 18:56:24 +00001057 for (left = old_devices; left; left = left->sibling) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001058 printk(BIOS_WARNING, "%s\n", dev_path(left));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001059 }
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001060 printk(BIOS_WARNING, "PCI: Check your mainboard Config.lb.\n");
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001061 }
1062
Myles Watson29cc9ed2009-07-02 18:56:24 +00001063 /* For all children that implement scan_bus() (i.e. bridges)
Eric Biedermanb78c1972004-10-14 20:54:17 +00001064 * scan the bus behind that child.
1065 */
Myles Watson29cc9ed2009-07-02 18:56:24 +00001066 for (child = bus->children; child; child = child->sibling) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001067 max = scan_bus(child, max);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001068 }
Li-Ta Loe5266692004-03-23 21:28:05 +00001069
Myles Watson29cc9ed2009-07-02 18:56:24 +00001070 /* We've scanned the bus and so we know all about what's on the other
1071 * side of any bridges that may be on this bus plus any devices.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001072 * Return how far we've got finding sub-buses.
1073 */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001074 printk(BIOS_DEBUG, "PCI: pci_scan_bus returning with max=%03x\n", max);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001075 post_code(0x55);
1076 return max;
1077}
1078
Li-Ta Loe5266692004-03-23 21:28:05 +00001079/**
1080 * @brief Scan a PCI bridge and the buses behind the bridge.
1081 *
1082 * Determine the existence of buses behind the bridge. Set up the bridge
1083 * according to the result of the scan.
1084 *
1085 * This function is the default scan_bus() method for PCI bridge devices.
1086 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001087 * @param dev Pointer to the bridge device.
1088 * @param max The highest bus number assigned up to now.
1089 * @return The maximum bus number found, after scanning all subordinate buses.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001090 */
Myles Watson032a9652009-05-11 22:24:53 +00001091unsigned int do_pci_scan_bridge(struct device *dev, unsigned int max,
Myles Watson29cc9ed2009-07-02 18:56:24 +00001092 unsigned int (*do_scan_bus) (struct bus * bus,
1093 unsigned min_devfn,
1094 unsigned max_devfn,
1095 unsigned int max))
Eric Biederman8ca8d762003-04-22 19:02:15 +00001096{
Eric Biedermane9a271e32003-09-02 03:36:25 +00001097 struct bus *bus;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001098 u32 buses;
1099 u16 cr;
Eric Biederman83b991a2003-10-11 06:20:25 +00001100
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001101 printk(BIOS_SPEW, "%s for %s\n", __func__, dev_path(dev));
Li-Ta Lo3a812852004-12-03 22:39:34 +00001102
Eric Biedermane9a271e32003-09-02 03:36:25 +00001103 bus = &dev->link[0];
Eric Biedermana9e632c2004-11-18 22:38:08 +00001104 bus->dev = dev;
Eric Biedermane9a271e32003-09-02 03:36:25 +00001105 dev->links = 1;
1106
Eric Biederman8ca8d762003-04-22 19:02:15 +00001107 /* Set up the primary, secondary and subordinate bus numbers. We have
1108 * no idea how many buses are behind this bridge yet, so we set the
Myles Watson032a9652009-05-11 22:24:53 +00001109 * subordinate bus number to 0xff for the moment.
Eric Biedermanb78c1972004-10-14 20:54:17 +00001110 */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001111 bus->secondary = ++max;
1112 bus->subordinate = 0xff;
Li-Ta Loe5266692004-03-23 21:28:05 +00001113
Eric Biederman8ca8d762003-04-22 19:02:15 +00001114 /* Clear all status bits and turn off memory, I/O and master enables. */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001115 cr = pci_read_config16(dev, PCI_COMMAND);
1116 pci_write_config16(dev, PCI_COMMAND, 0x0000);
1117 pci_write_config16(dev, PCI_STATUS, 0xffff);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001118
Myles Watson29cc9ed2009-07-02 18:56:24 +00001119 /* Read the existing primary/secondary/subordinate bus
Eric Biedermanb78c1972004-10-14 20:54:17 +00001120 * number configuration.
1121 */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001122 buses = pci_read_config32(dev, PCI_PRIMARY_BUS);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001123
1124 /* Configure the bus numbers for this bridge: the configuration
1125 * transactions will not be propagated by the bridge if it is not
Eric Biedermanb78c1972004-10-14 20:54:17 +00001126 * correctly configured.
1127 */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001128 buses &= 0xff000000;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001129 buses |= (((unsigned int)(dev->bus->secondary) << 0) |
1130 ((unsigned int)(bus->secondary) << 8) |
1131 ((unsigned int)(bus->subordinate) << 16));
Eric Biedermane9a271e32003-09-02 03:36:25 +00001132 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
Li-Ta Lo3a812852004-12-03 22:39:34 +00001133
Myles Watson032a9652009-05-11 22:24:53 +00001134 /* Now we can scan all subordinate buses
Eric Biedermanb78c1972004-10-14 20:54:17 +00001135 * i.e. the bus behind the bridge.
1136 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001137 max = do_scan_bus(bus, 0x00, 0xff, max);
Li-Ta Lo3a812852004-12-03 22:39:34 +00001138
Eric Biederman8ca8d762003-04-22 19:02:15 +00001139 /* We know the number of buses behind this bridge. Set the subordinate
Eric Biedermanb78c1972004-10-14 20:54:17 +00001140 * bus number to its real value.
1141 */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001142 bus->subordinate = max;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001143 buses = (buses & 0xff00ffff) | ((unsigned int)(bus->subordinate) << 16);
Eric Biedermane9a271e32003-09-02 03:36:25 +00001144 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
1145 pci_write_config16(dev, PCI_COMMAND, cr);
Myles Watson032a9652009-05-11 22:24:53 +00001146
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001147 printk(BIOS_SPEW, "%s returns max %d\n", __func__, max);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001148 return max;
1149}
Li-Ta Loe5266692004-03-23 21:28:05 +00001150
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001151/**
1152 * @brief Scan a PCI bridge and the buses behind the bridge.
1153 *
1154 * Determine the existence of buses behind the bridge. Set up the bridge
1155 * according to the result of the scan.
1156 *
1157 * This function is the default scan_bus() method for PCI bridge devices.
1158 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001159 * @param dev Pointer to the bridge device.
1160 * @param max The highest bus number assigned up to now.
1161 * @return The maximum bus number found, after scanning all subordinate buses.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001162 */
1163unsigned int pci_scan_bridge(struct device *dev, unsigned int max)
1164{
1165 return do_pci_scan_bridge(dev, max, pci_scan_bus);
1166}
1167
Myles Watson29cc9ed2009-07-02 18:56:24 +00001168/**
1169 * @brief Scan a PCI domain.
1170 *
1171 * This function is the default scan_bus() method for PCI domains.
1172 *
1173 * @param dev pointer to the domain
1174 * @param max the highest bus number assgined up to now
1175 *
1176 * @return The maximum bus number found, after scanning all subordinate busses
1177 */
1178unsigned int pci_domain_scan_bus(device_t dev, unsigned int max)
1179{
1180 max = pci_scan_bus(&dev->link[0], PCI_DEVFN(0, 0), 0xff, max);
1181 return max;
1182}
1183
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001184#if CONFIG_PC80_SYSTEM == 1
Myles Watson29cc9ed2009-07-02 18:56:24 +00001185/**
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001186 *
1187 * @brief Assign IRQ numbers
Myles Watson29cc9ed2009-07-02 18:56:24 +00001188 *
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001189 * This function assigns IRQs for all functions contained within the indicated
1190 * device address. If the device does not exist or does not require interrupts
1191 * then this function has no effect.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001192 *
1193 * This function should be called for each PCI slot in your system.
1194 *
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001195 * @param bus
1196 * @param slot
1197 * @param pIntAtoD is an array of IRQ #s that are assigned to PINTA through
1198 * PINTD of this slot. The particular irq #s that are passed in
1199 * depend on the routing inside your southbridge and on your
1200 * motherboard.
1201 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001202void pci_assign_irqs(unsigned bus, unsigned slot,
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001203 const unsigned char pIntAtoD[4])
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001204{
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001205 unsigned int funct;
1206 device_t pdev;
1207 u8 line;
1208 u8 irq;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001209
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001210 /* Each slot may contain up to eight functions */
1211 for (funct = 0; funct < 8; funct++) {
1212 pdev = dev_find_slot(bus, (slot << 3) + funct);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001213
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001214 if (!pdev)
1215 continue;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001216
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001217 line = pci_read_config8(pdev, PCI_INTERRUPT_PIN);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001218
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001219 // PCI spec says all values except 1..4 are reserved.
1220 if ((line < 1) || (line > 4))
1221 continue;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001222
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001223 irq = pIntAtoD[line - 1];
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001224
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001225 printk(BIOS_DEBUG, "Assigning IRQ %d to %d:%x.%d\n",
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001226 irq, bus, slot, funct);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001227
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001228 pci_write_config8(pdev, PCI_INTERRUPT_LINE,
1229 pIntAtoD[line - 1]);
1230
1231#ifdef PARANOID_IRQ_ASSIGNMENTS
Myles Watson17aeeca2009-10-07 18:41:08 +00001232 irq = pci_read_config8(pdev, PCI_INTERRUPT_LINE);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001233 printk(BIOS_DEBUG, " Readback = %d\n", irq);
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001234#endif
1235
1236 // Change to level triggered
1237 i8259_configure_irq_trigger(pIntAtoD[line - 1], IRQ_LEVEL_TRIGGERED);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001238 }
1239}
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001240#endif
1241