blob: 4a7a2cd9c58322da8cc9f0d23cc3e546b4d39cdc [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 Biedermane9a271e32003-09-02 03:36:25 +000035#include <part/hard_reset.h>
Eric Biederman30e143a2003-09-01 23:45:32 +000036#include <part/fallback_boot.h>
Eric Biederman03acab62004-10-14 21:25:53 +000037#include <delay.h>
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000038#if CONFIG_HYPERTRANSPORT_PLUGIN_SUPPORT == 1
39#include <device/hypertransport.h>
40#endif
41#if CONFIG_PCIX_PLUGIN_SUPPORT == 1
42#include <device/pcix.h>
43#endif
44#if CONFIG_PCIEXP_PLUGIN_SUPPORT == 1
45#include <device/pciexp.h>
46#endif
Stefan Reinauerec75a572009-03-16 15:27:00 +000047#if CONFIG_AGP_PLUGIN_SUPPORT == 1
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000048#include <device/agp.h>
49#endif
50#if CONFIG_CARDBUS_PLUGIN_SUPPORT == 1
51#include <device/cardbus.h>
52#endif
Eric Biederman03acab62004-10-14 21:25:53 +000053
Myles Watson29cc9ed2009-07-02 18:56:24 +000054u8 pci_moving_config8(struct device *dev, unsigned int reg)
Eric Biederman03acab62004-10-14 21:25:53 +000055{
Myles Watson29cc9ed2009-07-02 18:56:24 +000056 u8 value, ones, zeroes;
Eric Biederman03acab62004-10-14 21:25:53 +000057 value = pci_read_config8(dev, reg);
Myles Watson032a9652009-05-11 22:24:53 +000058
Eric Biederman03acab62004-10-14 21:25:53 +000059 pci_write_config8(dev, reg, 0xff);
60 ones = pci_read_config8(dev, reg);
61
62 pci_write_config8(dev, reg, 0x00);
63 zeroes = pci_read_config8(dev, reg);
64
65 pci_write_config8(dev, reg, value);
66
67 return ones ^ zeroes;
68}
Li-Ta Lo9a5b4962004-12-23 21:48:01 +000069
Myles Watson29cc9ed2009-07-02 18:56:24 +000070u16 pci_moving_config16(struct device * dev, unsigned int reg)
Eric Biederman03acab62004-10-14 21:25:53 +000071{
Myles Watson29cc9ed2009-07-02 18:56:24 +000072 u16 value, ones, zeroes;
Eric Biederman03acab62004-10-14 21:25:53 +000073 value = pci_read_config16(dev, reg);
Myles Watson032a9652009-05-11 22:24:53 +000074
Eric Biederman03acab62004-10-14 21:25:53 +000075 pci_write_config16(dev, reg, 0xffff);
76 ones = pci_read_config16(dev, reg);
77
78 pci_write_config16(dev, reg, 0x0000);
79 zeroes = pci_read_config16(dev, reg);
80
81 pci_write_config16(dev, reg, value);
82
83 return ones ^ zeroes;
84}
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +000085
Myles Watson29cc9ed2009-07-02 18:56:24 +000086u32 pci_moving_config32(struct device * dev, unsigned int reg)
Eric Biederman03acab62004-10-14 21:25:53 +000087{
Myles Watson29cc9ed2009-07-02 18:56:24 +000088 u32 value, ones, zeroes;
Eric Biederman03acab62004-10-14 21:25:53 +000089 value = pci_read_config32(dev, reg);
Myles Watson032a9652009-05-11 22:24:53 +000090
Eric Biederman03acab62004-10-14 21:25:53 +000091 pci_write_config32(dev, reg, 0xffffffff);
92 ones = pci_read_config32(dev, reg);
93
94 pci_write_config32(dev, reg, 0x00000000);
95 zeroes = pci_read_config32(dev, reg);
96
97 pci_write_config32(dev, reg, value);
98
99 return ones ^ zeroes;
100}
101
Myles Watson29cc9ed2009-07-02 18:56:24 +0000102/**
103 * Given a device, a capability type, and a last position, return the next
104 * matching capability. Always start at the head of the list.
105 *
106 * @param dev Pointer to the device structure.
107 * @param cap_type PCI_CAP_LIST_ID of the PCI capability we're looking for.
108 * @param last Location of the PCI capability register to start from.
109 */
110unsigned pci_find_next_capability(struct device *dev, unsigned cap,
111 unsigned last)
Eric Biederman03acab62004-10-14 21:25:53 +0000112{
113 unsigned pos;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000114 unsigned status;
115 unsigned reps = 48;
Eric Biederman03acab62004-10-14 21:25:53 +0000116 pos = 0;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000117 status = pci_read_config16(dev, PCI_STATUS);
118 if (!(status & PCI_STATUS_CAP_LIST)) {
119 return 0;
120 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000121 switch (dev->hdr_type & 0x7f) {
Eric Biederman03acab62004-10-14 21:25:53 +0000122 case PCI_HEADER_TYPE_NORMAL:
123 case PCI_HEADER_TYPE_BRIDGE:
124 pos = PCI_CAPABILITY_LIST;
125 break;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000126 case PCI_HEADER_TYPE_CARDBUS:
127 pos = PCI_CB_CAPABILITY_LIST;
128 break;
129 default:
130 return 0;
Eric Biederman03acab62004-10-14 21:25:53 +0000131 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000132 pos = pci_read_config8(dev, pos);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000133 while (reps-- && (pos >= 0x40)) { /* Loop through the linked list. */
Eric Biederman03acab62004-10-14 21:25:53 +0000134 int this_cap;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000135 pos &= ~3;
Eric Biederman03acab62004-10-14 21:25:53 +0000136 this_cap = pci_read_config8(dev, pos + PCI_CAP_LIST_ID);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000137 printk_spew("Capability: type 0x%02x @ 0x%02x\n", this_cap,
138 pos);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000139 if (this_cap == 0xff) {
140 break;
141 }
142 if (!last && (this_cap == cap)) {
Eric Biederman03acab62004-10-14 21:25:53 +0000143 return pos;
144 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000145 if (last == pos) {
146 last = 0;
147 }
148 pos = pci_read_config8(dev, pos + PCI_CAP_LIST_NEXT);
Eric Biederman03acab62004-10-14 21:25:53 +0000149 }
150 return 0;
151}
152
Myles Watson29cc9ed2009-07-02 18:56:24 +0000153/**
154 * Given a device, and a capability type, return the next matching
155 * capability. Always start at the head of the list.
156 *
157 * @param dev Pointer to the device structure.
158 * @param cap_type PCI_CAP_LIST_ID of the PCI capability we're looking for.
159 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000160unsigned pci_find_capability(device_t dev, unsigned cap)
161{
162 return pci_find_next_capability(dev, cap, 0);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000163}
164
Myles Watson29cc9ed2009-07-02 18:56:24 +0000165/**
166 * Given a device and register, read the size of the BAR for that register.
167 *
168 * @param dev Pointer to the device structure.
169 * @param index Address of the PCI configuration register.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000170 */
Eric Biederman03acab62004-10-14 21:25:53 +0000171struct resource *pci_get_resource(struct device *dev, unsigned long index)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000172{
Eric Biederman5cd81732004-03-11 15:01:31 +0000173 struct resource *resource;
Eric Biederman03acab62004-10-14 21:25:53 +0000174 unsigned long value, attr;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000175 resource_t moving, limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000176
Myles Watson29cc9ed2009-07-02 18:56:24 +0000177 /* Initialize the resources to nothing. */
Eric Biederman03acab62004-10-14 21:25:53 +0000178 resource = new_resource(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000179
Myles Watson29cc9ed2009-07-02 18:56:24 +0000180 /* Get the initial value. */
Eric Biederman03acab62004-10-14 21:25:53 +0000181 value = pci_read_config32(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000182
Myles Watson29cc9ed2009-07-02 18:56:24 +0000183 /* See which bits move. */
Eric Biederman03acab62004-10-14 21:25:53 +0000184 moving = pci_moving_config32(dev, index);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000185
Myles Watson29cc9ed2009-07-02 18:56:24 +0000186 /* Initialize attr to the bits that do not move. */
Eric Biederman03acab62004-10-14 21:25:53 +0000187 attr = value & ~moving;
188
Myles Watson29cc9ed2009-07-02 18:56:24 +0000189 /* If it is a 64bit resource look at the high half as well. */
Eric Biederman03acab62004-10-14 21:25:53 +0000190 if (((attr & PCI_BASE_ADDRESS_SPACE_IO) == 0) &&
Myles Watson29cc9ed2009-07-02 18:56:24 +0000191 ((attr & PCI_BASE_ADDRESS_MEM_LIMIT_MASK) ==
192 PCI_BASE_ADDRESS_MEM_LIMIT_64)) {
193 /* Find the high bits that move. */
194 moving |=
195 ((resource_t) pci_moving_config32(dev, index + 4)) << 32;
Eric Biederman03acab62004-10-14 21:25:53 +0000196 }
Myles Watson032a9652009-05-11 22:24:53 +0000197 /* Find the resource constraints.
Eric Biederman03acab62004-10-14 21:25:53 +0000198 * Start by finding the bits that move. From there:
199 * - Size is the least significant bit of the bits that move.
200 * - Limit is all of the bits that move plus all of the lower bits.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000201 * See PCI Spec 6.2.5.1.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000202 */
Eric Biederman03acab62004-10-14 21:25:53 +0000203 limit = 0;
204 if (moving) {
205 resource->size = 1;
206 resource->align = resource->gran = 0;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000207 while (!(moving & resource->size)) {
Eric Biederman03acab62004-10-14 21:25:53 +0000208 resource->size <<= 1;
209 resource->align += 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000210 resource->gran += 1;
Eric Biederman03acab62004-10-14 21:25:53 +0000211 }
212 resource->limit = limit = moving | (resource->size - 1);
213 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000214
215 /* Some broken hardware has read-only registers that do not
Eric Biederman03acab62004-10-14 21:25:53 +0000216 * really size correctly.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000217 * Example: the Acer M7229 has BARs 1-4 normally read-only.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000218 * so BAR1 at offset 0x10 reads 0x1f1. If you size that register
Myles Watson032a9652009-05-11 22:24:53 +0000219 * by writing 0xffffffff to it, it will read back as 0x1f1 -- a
220 * violation of the spec.
Eric Biederman03acab62004-10-14 21:25:53 +0000221 * We catch this case and ignore it by observing which bits move,
222 * This also catches the common case unimplemented registers
223 * that always read back as 0.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000224 */
Eric Biederman03acab62004-10-14 21:25:53 +0000225 if (moving == 0) {
226 if (value != 0) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000227 printk_debug
228 ("%s register %02lx(%08lx), read-only ignoring it\n",
229 dev_path(dev), index, value);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000230 }
231 resource->flags = 0;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000232 } else if (attr & PCI_BASE_ADDRESS_SPACE_IO) {
233 /* An I/O mapped base address. */
Eric Biederman03acab62004-10-14 21:25:53 +0000234 attr &= PCI_BASE_ADDRESS_IO_ATTR_MASK;
Eric Biederman5cd81732004-03-11 15:01:31 +0000235 resource->flags |= IORESOURCE_IO;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000236 /* I don't want to deal with 32bit I/O resources. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000237 resource->limit = 0xffff;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000238 } else {
239 /* A Memory mapped base address. */
Eric Biederman03acab62004-10-14 21:25:53 +0000240 attr &= PCI_BASE_ADDRESS_MEM_ATTR_MASK;
Eric Biederman5cd81732004-03-11 15:01:31 +0000241 resource->flags |= IORESOURCE_MEM;
Eric Biederman03acab62004-10-14 21:25:53 +0000242 if (attr & PCI_BASE_ADDRESS_MEM_PREFETCH) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000243 resource->flags |= IORESOURCE_PREFETCH;
244 }
Eric Biederman03acab62004-10-14 21:25:53 +0000245 attr &= PCI_BASE_ADDRESS_MEM_LIMIT_MASK;
246 if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_32) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000247 /* 32bit limit. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000248 resource->limit = 0xffffffffUL;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000249 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_1M) {
250 /* 1MB limit. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000251 resource->limit = 0x000fffffUL;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000252 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_64) {
253 /* 64bit limit. */
Eric Biederman03acab62004-10-14 21:25:53 +0000254 resource->limit = 0xffffffffffffffffULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000255 resource->flags |= IORESOURCE_PCI64;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000256 } else {
257 /* Invalid value. */
258 printk_err("Broken BAR with value %lx\n", attr);
259 printk_err(" on dev %s at index %02lx\n",
260 dev_path(dev), index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000261 resource->flags = 0;
262 }
263 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000264 /* Don't let the limit exceed which bits can move. */
Eric Biederman03acab62004-10-14 21:25:53 +0000265 if (resource->limit > limit) {
266 resource->limit = limit;
267 }
Eric Biederman03acab62004-10-14 21:25:53 +0000268
Eric Biederman5cd81732004-03-11 15:01:31 +0000269 return resource;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000270}
271
Myles Watson29cc9ed2009-07-02 18:56:24 +0000272/**
273 * Given a device and an index, read the size of the BAR for that register.
274 *
275 * @param dev Pointer to the device structure.
276 * @param index Address of the PCI configuration register.
277 */
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000278static void pci_get_rom_resource(struct device *dev, unsigned long index)
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000279{
280 struct resource *resource;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000281 unsigned long value;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000282 resource_t moving;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000283
Myles Watson29cc9ed2009-07-02 18:56:24 +0000284 if ((dev->on_mainboard) && (dev->rom_address == 0)) {
285 /* Skip it if rom_address is not set in the MB Config.lb. */
286 return;
287 }
Yinghai Lubcde1612005-01-14 05:34:09 +0000288
Myles Watson29cc9ed2009-07-02 18:56:24 +0000289 /* Initialize the resources to nothing. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000290 resource = new_resource(dev, index);
291
Myles Watson29cc9ed2009-07-02 18:56:24 +0000292 /* Get the initial value. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000293 value = pci_read_config32(dev, index);
294
Myles Watson29cc9ed2009-07-02 18:56:24 +0000295 /* See which bits move. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000296 moving = pci_moving_config32(dev, index);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000297
298 /* Clear the Enable bit. */
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000299 moving = moving & ~PCI_ROM_ADDRESS_ENABLE;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000300
Myles Watson032a9652009-05-11 22:24:53 +0000301 /* Find the resource constraints.
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000302 * Start by finding the bits that move. From there:
303 * - Size is the least significant bit of the bits that move.
304 * - Limit is all of the bits that move plus all of the lower bits.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000305 * See PCI Spec 6.2.5.1.
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000306 */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000307 if (moving) {
308 resource->size = 1;
309 resource->align = resource->gran = 0;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000310 while (!(moving & resource->size)) {
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000311 resource->size <<= 1;
312 resource->align += 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000313 resource->gran += 1;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000314 }
Patrick Georgi16cdbb22009-04-21 20:14:31 +0000315 resource->limit = moving | (resource->size - 1);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000316 resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY;
317 } else {
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000318 if (value != 0) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000319 printk_debug
320 ("%s register %02lx(%08lx), read-only ignoring it\n",
321 dev_path(dev), index, value);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000322 }
323 resource->flags = 0;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000324 }
Yinghai Luc7870ac2005-01-13 19:14:52 +0000325
Myles Watson29cc9ed2009-07-02 18:56:24 +0000326 /* For on board device with embedded ROM image, the ROM image is at
Yinghai Luc7870ac2005-01-13 19:14:52 +0000327 * fixed address specified in the Config.lb, the dev->rom_address is
328 * inited by driver_pci_onboard_ops::enable_dev() */
Yinghai Lubcde1612005-01-14 05:34:09 +0000329 if ((dev->on_mainboard) && (dev->rom_address != 0)) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000330 resource->base = dev->rom_address;
Yinghai Luc7870ac2005-01-13 19:14:52 +0000331 resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY |
Myles Watson29cc9ed2009-07-02 18:56:24 +0000332 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Myles Watson032a9652009-05-11 22:24:53 +0000333 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000334
335 compact_resources(dev);
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000336}
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000337
Myles Watson29cc9ed2009-07-02 18:56:24 +0000338/**
339 * Read the base address registers for a given device.
340 *
341 * @param dev Pointer to the dev structure.
342 * @param howmany How many registers to read (6 for device, 2 for bridge).
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000343 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000344static void pci_read_bases(struct device *dev, unsigned int howmany)
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000345{
346 unsigned long index;
347
Myles Watson29cc9ed2009-07-02 18:56:24 +0000348 for (index = PCI_BASE_ADDRESS_0;
349 (index < PCI_BASE_ADDRESS_0 + (howmany << 2));) {
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000350 struct resource *resource;
351 resource = pci_get_resource(dev, index);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000352 index += (resource->flags & IORESOURCE_PCI64) ? 8 : 4;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000353 }
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000354
355 compact_resources(dev);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000356}
357
Myles Watson29cc9ed2009-07-02 18:56:24 +0000358static void pci_record_bridge_resource(struct device *dev, resource_t moving,
359 unsigned index, unsigned long type)
Eric Biederman03acab62004-10-14 21:25:53 +0000360{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000361 /* Initialize the constraints on the current bus. */
Eric Biederman03acab62004-10-14 21:25:53 +0000362 struct resource *resource;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000363 resource = NULL;
Eric Biederman03acab62004-10-14 21:25:53 +0000364 if (moving) {
365 unsigned long gran;
366 resource_t step;
367 resource = new_resource(dev, index);
368 resource->size = 0;
369 gran = 0;
370 step = 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000371 while ((moving & step) == 0) {
Eric Biederman03acab62004-10-14 21:25:53 +0000372 gran += 1;
373 step <<= 1;
374 }
375 resource->gran = gran;
376 resource->align = gran;
377 resource->limit = moving | (step - 1);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000378 resource->flags = type | IORESOURCE_PCI_BRIDGE |
379 IORESOURCE_BRIDGE;
Eric Biederman03acab62004-10-14 21:25:53 +0000380 }
381 return;
382}
383
Eric Biederman8ca8d762003-04-22 19:02:15 +0000384static void pci_bridge_read_bases(struct device *dev)
385{
Eric Biederman03acab62004-10-14 21:25:53 +0000386 resource_t moving_base, moving_limit, moving;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000387
Myles Watson29cc9ed2009-07-02 18:56:24 +0000388 /* See if the bridge I/O resources are implemented. */
389 moving_base = ((u32) pci_moving_config8(dev, PCI_IO_BASE)) << 8;
390 moving_base |=
391 ((u32) pci_moving_config16(dev, PCI_IO_BASE_UPPER16)) << 16;
Eric Biederman03acab62004-10-14 21:25:53 +0000392
Myles Watson29cc9ed2009-07-02 18:56:24 +0000393 moving_limit = ((u32) pci_moving_config8(dev, PCI_IO_LIMIT)) << 8;
394 moving_limit |=
395 ((u32) pci_moving_config16(dev, PCI_IO_LIMIT_UPPER16)) << 16;
Eric Biederman03acab62004-10-14 21:25:53 +0000396
397 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000398
Myles Watson29cc9ed2009-07-02 18:56:24 +0000399 /* Initialize the I/O space constraints on the current bus. */
400 pci_record_bridge_resource(dev, moving, PCI_IO_BASE, IORESOURCE_IO);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000401
Myles Watson29cc9ed2009-07-02 18:56:24 +0000402 /* See if the bridge prefmem resources are implemented. */
403 moving_base =
404 ((resource_t) pci_moving_config16(dev, PCI_PREF_MEMORY_BASE)) << 16;
405 moving_base |=
406 ((resource_t) pci_moving_config32(dev, PCI_PREF_BASE_UPPER32)) <<
407 32;
Eric Biederman03acab62004-10-14 21:25:53 +0000408
Myles Watson29cc9ed2009-07-02 18:56:24 +0000409 moving_limit =
410 ((resource_t) pci_moving_config16(dev, PCI_PREF_MEMORY_LIMIT)) <<
411 16;
412 moving_limit |=
413 ((resource_t) pci_moving_config32(dev, PCI_PREF_LIMIT_UPPER32)) <<
414 32;
Myles Watson032a9652009-05-11 22:24:53 +0000415
Eric Biederman03acab62004-10-14 21:25:53 +0000416 moving = moving_base & moving_limit;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000417 /* Initialize the prefetchable memory constraints on the current bus. */
418 pci_record_bridge_resource(dev, moving, PCI_PREF_MEMORY_BASE,
419 IORESOURCE_MEM | IORESOURCE_PREFETCH);
Myles Watson032a9652009-05-11 22:24:53 +0000420
Myles Watson29cc9ed2009-07-02 18:56:24 +0000421 /* See if the bridge mem resources are implemented. */
422 moving_base = ((u32) pci_moving_config16(dev, PCI_MEMORY_BASE)) << 16;
423 moving_limit = ((u32) pci_moving_config16(dev, PCI_MEMORY_LIMIT)) << 16;
Eric Biederman03acab62004-10-14 21:25:53 +0000424
425 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000426
Myles Watson29cc9ed2009-07-02 18:56:24 +0000427 /* Initialize the memory resources on the current bus. */
428 pci_record_bridge_resource(dev, moving, PCI_MEMORY_BASE,
429 IORESOURCE_MEM);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000430
Eric Biederman5cd81732004-03-11 15:01:31 +0000431 compact_resources(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000432}
433
Eric Biederman5899fd82003-04-24 06:25:08 +0000434void pci_dev_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000435{
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000436 pci_read_bases(dev, 6);
437 pci_get_rom_resource(dev, PCI_ROM_ADDRESS);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000438}
439
Eric Biederman5899fd82003-04-24 06:25:08 +0000440void pci_bus_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000441{
Eric Biederman8ca8d762003-04-22 19:02:15 +0000442 pci_bridge_read_bases(dev);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000443 pci_read_bases(dev, 2);
444 pci_get_rom_resource(dev, PCI_ROM_ADDRESS1);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000445}
446
Myles Watson29cc9ed2009-07-02 18:56:24 +0000447void pci_domain_read_resources(struct device *dev)
448{
449 struct resource *res;
450
451 /* Initialize the system-wide I/O space constraints. */
452 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
453 res->limit = 0xffffUL;
454 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
455 IORESOURCE_ASSIGNED;
456
457 /* Initialize the system-wide memory resources constraints. */
458 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
459 res->limit = 0xffffffffULL;
460 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
461 IORESOURCE_ASSIGNED;
462}
463
Eric Biederman8ca8d762003-04-22 19:02:15 +0000464static void pci_set_resource(struct device *dev, struct resource *resource)
465{
Eric Biederman03acab62004-10-14 21:25:53 +0000466 resource_t base, end;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000467
Myles Watson29cc9ed2009-07-02 18:56:24 +0000468 /* Make certain the resource has actually been assigned a value. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000469 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000470 printk_err("ERROR: %s %02lx %s size: 0x%010llx not assigned\n",
471 dev_path(dev), resource->index,
472 resource_type(resource), resource->size);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000473 return;
474 }
475
Myles Watson29cc9ed2009-07-02 18:56:24 +0000476 /* If I have already stored this resource don't worry about it. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000477 if (resource->flags & IORESOURCE_STORED) {
478 return;
479 }
480
Myles Watson29cc9ed2009-07-02 18:56:24 +0000481 /* If the resource is subtractive don't worry about it. */
Eric Biederman03acab62004-10-14 21:25:53 +0000482 if (resource->flags & IORESOURCE_SUBTRACTIVE) {
483 return;
484 }
485
Myles Watson29cc9ed2009-07-02 18:56:24 +0000486 /* Only handle PCI memory and I/O resources for now. */
487 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
Eric Biederman8ca8d762003-04-22 19:02:15 +0000488 return;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000489
Myles Watson29cc9ed2009-07-02 18:56:24 +0000490 /* Enable the resources in the command register. */
Eric Biederman03acab62004-10-14 21:25:53 +0000491 if (resource->size) {
492 if (resource->flags & IORESOURCE_MEM) {
493 dev->command |= PCI_COMMAND_MEMORY;
494 }
495 if (resource->flags & IORESOURCE_IO) {
496 dev->command |= PCI_COMMAND_IO;
497 }
498 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
499 dev->command |= PCI_COMMAND_MASTER;
500 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000501 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000502 /* Get the base address. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000503 base = resource->base;
Eric Biederman5cd81732004-03-11 15:01:31 +0000504
Myles Watson29cc9ed2009-07-02 18:56:24 +0000505 /* Get the end. */
Eric Biederman03acab62004-10-14 21:25:53 +0000506 end = resource_end(resource);
Myles Watson032a9652009-05-11 22:24:53 +0000507
Myles Watson29cc9ed2009-07-02 18:56:24 +0000508 /* Now store the resource. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000509 resource->flags |= IORESOURCE_STORED;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000510
511 /* PCI Bridges have no enable bit. They are disabled if the base of
512 * the range is greater than the limit. If the size is zero, disable
513 * by setting the base = limit and end = limit - 2^gran.
514 */
515 if (resource->size == 0 && (resource->flags & IORESOURCE_PCI_BRIDGE)) {
516 base = resource->limit;
517 end = resource->limit - (1 << resource->gran);
518 resource->base = base;
519 }
520
Eric Biederman8ca8d762003-04-22 19:02:15 +0000521 if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) {
Eric Biederman03acab62004-10-14 21:25:53 +0000522 unsigned long base_lo, base_hi;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000523 /* Some chipsets allow us to set/clear the I/O bit
524 * (e.g. VIA 82c686a). So set it to be safe.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000525 */
Eric Biederman03acab62004-10-14 21:25:53 +0000526 base_lo = base & 0xffffffff;
527 base_hi = (base >> 32) & 0xffffffff;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000528 if (resource->flags & IORESOURCE_IO) {
Eric Biederman03acab62004-10-14 21:25:53 +0000529 base_lo |= PCI_BASE_ADDRESS_SPACE_IO;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000530 }
Eric Biederman03acab62004-10-14 21:25:53 +0000531 pci_write_config32(dev, resource->index, base_lo);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000532 if (resource->flags & IORESOURCE_PCI64) {
Eric Biederman03acab62004-10-14 21:25:53 +0000533 pci_write_config32(dev, resource->index + 4, base_hi);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000534 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000535 } else if (resource->index == PCI_IO_BASE) {
536 /* Set the I/O ranges. */
537 pci_write_config8(dev, PCI_IO_BASE, base >> 8);
Eric Biederman03acab62004-10-14 21:25:53 +0000538 pci_write_config16(dev, PCI_IO_BASE_UPPER16, base >> 16);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000539 pci_write_config8(dev, PCI_IO_LIMIT, end >> 8);
Eric Biederman03acab62004-10-14 21:25:53 +0000540 pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, end >> 16);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000541 } else if (resource->index == PCI_MEMORY_BASE) {
542 /* Set the memory range. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000543 pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16);
Eric Biederman03acab62004-10-14 21:25:53 +0000544 pci_write_config16(dev, PCI_MEMORY_LIMIT, end >> 16);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000545 } else if (resource->index == PCI_PREF_MEMORY_BASE) {
546 /* Set the prefetchable memory range. */
Eric Biederman03acab62004-10-14 21:25:53 +0000547 pci_write_config16(dev, PCI_PREF_MEMORY_BASE, base >> 16);
548 pci_write_config32(dev, PCI_PREF_BASE_UPPER32, base >> 32);
549 pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, end >> 16);
550 pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, end >> 32);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000551 } else {
552 /* Don't let me think I stored the resource. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000553 resource->flags &= ~IORESOURCE_STORED;
Myles Watsonc4ddbff2009-02-09 17:52:54 +0000554 printk_err("ERROR: invalid resource->index %lx\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000555 resource->index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000556 }
Eric Biederman03acab62004-10-14 21:25:53 +0000557 report_resource_stored(dev, resource, "");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000558 return;
559}
560
Eric Biederman5899fd82003-04-24 06:25:08 +0000561void pci_dev_set_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000562{
563 struct resource *resource, *last;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000564 unsigned link;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000565 u8 line;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000566
567 last = &dev->resource[dev->resources];
Eric Biedermanb78c1972004-10-14 20:54:17 +0000568
Myles Watson29cc9ed2009-07-02 18:56:24 +0000569 for (resource = &dev->resource[0]; resource < last; resource++) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000570 pci_set_resource(dev, resource);
571 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000572 for (link = 0; link < dev->links; link++) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000573 struct bus *bus;
574 bus = &dev->link[link];
575 if (bus->children) {
576 assign_resources(bus);
577 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000578 }
579
Myles Watson29cc9ed2009-07-02 18:56:24 +0000580 /* Set a default latency timer. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000581 pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000582
Myles Watson29cc9ed2009-07-02 18:56:24 +0000583 /* Set a default secondary latency timer. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000584 if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
Eric Biederman7a5416a2003-06-12 19:23:51 +0000585 pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000586 }
587
Myles Watson29cc9ed2009-07-02 18:56:24 +0000588 /* Zero the IRQ settings. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000589 line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000590 if (line) {
Eric Biederman7a5416a2003-06-12 19:23:51 +0000591 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000592 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000593 /* Set the cache line size, so far 64 bytes is good for everyone. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000594 pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000595}
596
Eric Biedermane9a271e32003-09-02 03:36:25 +0000597void pci_dev_enable_resources(struct device *dev)
598{
Eric Biedermana9e632c2004-11-18 22:38:08 +0000599 const struct pci_operations *ops;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000600 u16 command;
Eric Biederman03acab62004-10-14 21:25:53 +0000601
Myles Watson29cc9ed2009-07-02 18:56:24 +0000602 /* Set the subsystem vendor and device id for mainboard devices. */
Eric Biederman03acab62004-10-14 21:25:53 +0000603 ops = ops_pci(dev);
Eric Biedermandbec2d42004-10-21 10:44:08 +0000604 if (dev->on_mainboard && ops && ops->set_subsystem) {
Eric Biederman03acab62004-10-14 21:25:53 +0000605 printk_debug("%s subsystem <- %02x/%02x\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000606 dev_path(dev),
607 CONFIG_MAINBOARD_PCI_SUBSYSTEM_VENDOR_ID,
608 CONFIG_MAINBOARD_PCI_SUBSYSTEM_DEVICE_ID);
Myles Watson032a9652009-05-11 22:24:53 +0000609 ops->set_subsystem(dev,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000610 CONFIG_MAINBOARD_PCI_SUBSYSTEM_VENDOR_ID,
611 CONFIG_MAINBOARD_PCI_SUBSYSTEM_DEVICE_ID);
Eric Biederman03acab62004-10-14 21:25:53 +0000612 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000613 command = pci_read_config16(dev, PCI_COMMAND);
614 command |= dev->command;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000615 /* v3 has
616 * command |= (PCI_COMMAND_PARITY + PCI_COMMAND_SERR); // Error check.
617 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000618 printk_debug("%s cmd <- %02x\n", dev_path(dev), command);
619 pci_write_config16(dev, PCI_COMMAND, command);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000620}
621
622void pci_bus_enable_resources(struct device *dev)
623{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000624 u16 ctrl;
625
626 /* Enable I/O in command register if there is VGA card
627 * connected with (even it does not claim I/O resource).
628 */
Li-Ta Lo515f6c72005-01-11 22:48:54 +0000629 if (dev->link[0].bridge_ctrl & PCI_BRIDGE_CTL_VGA)
630 dev->command |= PCI_COMMAND_IO;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000631 ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
632 ctrl |= dev->link[0].bridge_ctrl;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000633 ctrl |= (PCI_BRIDGE_CTL_PARITY + PCI_BRIDGE_CTL_SERR); /* Error check. */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000634 printk_debug("%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
635 pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
636
637 pci_dev_enable_resources(dev);
Eric Biedermandbec2d42004-10-21 10:44:08 +0000638 enable_childrens_resources(dev);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000639}
640
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000641void pci_bus_reset(struct bus *bus)
642{
643 unsigned ctl;
644 ctl = pci_read_config16(bus->dev, PCI_BRIDGE_CONTROL);
645 ctl |= PCI_BRIDGE_CTL_BUS_RESET;
646 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
647 mdelay(10);
648 ctl &= ~PCI_BRIDGE_CTL_BUS_RESET;
649 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
650 delay(1);
651}
652
Myles Watson29cc9ed2009-07-02 18:56:24 +0000653void pci_dev_set_subsystem(struct device *dev, unsigned vendor, unsigned device)
Eric Biederman03acab62004-10-14 21:25:53 +0000654{
Myles Watson032a9652009-05-11 22:24:53 +0000655 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000656 ((device & 0xffff) << 16) | (vendor & 0xffff));
Eric Biederman03acab62004-10-14 21:25:53 +0000657}
658
Torsten Duwe1f2f8002008-01-06 01:10:54 +0000659/** default handler: only runs the relevant pci bios. */
Li-Ta Lo883b8792005-01-10 23:16:22 +0000660void pci_dev_init(struct device *dev)
661{
Torsten Duwe1f2f8002008-01-06 01:10:54 +0000662#if CONFIG_PCI_ROM_RUN == 1 || CONFIG_VGA_ROM_RUN == 1
Myles Watson29cc9ed2009-07-02 18:56:24 +0000663 void run_bios(struct device *dev, unsigned long addr);
Li-Ta Lo883b8792005-01-10 23:16:22 +0000664 struct rom_header *rom, *ram;
665
Roman Kononov778a42b2007-04-06 18:34:39 +0000666#if CONFIG_PCI_ROM_RUN != 1
Torsten Duwe1f2f8002008-01-06 01:10:54 +0000667 /* We want to execute VGA option ROMs when CONFIG_VGA_ROM_RUN
Roman Kononov778a42b2007-04-06 18:34:39 +0000668 * is set but CONFIG_PCI_ROM_RUN is not. In this case we skip
669 * all other option ROM types.
670 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000671 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
Roman Kononov778a42b2007-04-06 18:34:39 +0000672 return;
673#endif
674
Li-Ta Lo883b8792005-01-10 23:16:22 +0000675 rom = pci_rom_probe(dev);
676 if (rom == NULL)
677 return;
Roman Kononov778a42b2007-04-06 18:34:39 +0000678
Li-Ta Lo883b8792005-01-10 23:16:22 +0000679 ram = pci_rom_load(dev, rom);
Yinghai Lu9e4faef2005-01-14 22:04:49 +0000680 if (ram == NULL)
681 return;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000682
Stefan Reinauerd98cf5b2008-08-01 11:25:41 +0000683 run_bios(dev, (unsigned long)ram);
Roman Kononov778a42b2007-04-06 18:34:39 +0000684
685#if CONFIG_CONSOLE_VGA == 1
Luc Verhaegen5c5beb72009-05-29 03:04:16 +0000686 if ((dev->class>>8) == PCI_CLASS_DISPLAY_VGA)
Luc Verhaegen43bc5a9c2009-05-29 03:44:47 +0000687 vga_console_init();
Torsten Duwe1f2f8002008-01-06 01:10:54 +0000688#endif /* CONFIG_CONSOLE_VGA */
689#endif /* CONFIG_PCI_ROM_RUN || CONFIG_VGA_ROM_RUN */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000690}
Li-Ta Lo883b8792005-01-10 23:16:22 +0000691
Li-Ta Loe5266692004-03-23 21:28:05 +0000692/** Default device operation for PCI devices */
Eric Biedermana9e632c2004-11-18 22:38:08 +0000693static struct pci_operations pci_dev_ops_pci = {
Eric Biederman03acab62004-10-14 21:25:53 +0000694 .set_subsystem = pci_dev_set_subsystem,
695};
696
Eric Biederman8ca8d762003-04-22 19:02:15 +0000697struct device_operations default_pci_ops_dev = {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000698 .read_resources = pci_dev_read_resources,
699 .set_resources = pci_dev_set_resources,
Eric Biedermane9a271e32003-09-02 03:36:25 +0000700 .enable_resources = pci_dev_enable_resources,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000701 .init = pci_dev_init,
702 .scan_bus = 0,
703 .enable = 0,
704 .ops_pci = &pci_dev_ops_pci,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000705};
Li-Ta Loe5266692004-03-23 21:28:05 +0000706
707/** Default device operations for PCI bridges */
Eric Biedermana9e632c2004-11-18 22:38:08 +0000708static struct pci_operations pci_bus_ops_pci = {
Eric Biederman03acab62004-10-14 21:25:53 +0000709 .set_subsystem = 0,
710};
Li-Ta Lo883b8792005-01-10 23:16:22 +0000711
Eric Biederman8ca8d762003-04-22 19:02:15 +0000712struct device_operations default_pci_ops_bus = {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000713 .read_resources = pci_bus_read_resources,
714 .set_resources = pci_dev_set_resources,
Eric Biedermane9a271e32003-09-02 03:36:25 +0000715 .enable_resources = pci_bus_enable_resources,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000716 .init = 0,
717 .scan_bus = pci_scan_bridge,
718 .enable = 0,
719 .reset_bus = pci_bus_reset,
720 .ops_pci = &pci_bus_ops_pci,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000721};
Li-Ta Loe5266692004-03-23 21:28:05 +0000722
723/**
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000724 * @brief Detect the type of downstream bridge
725 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000726 * This function is a heuristic to detect which type of bus is downstream
727 * of a PCI-to-PCI bridge. This functions by looking for various capability
728 * blocks to figure out the type of downstream bridge. PCI-X, PCI-E, and
729 * Hypertransport all seem to have appropriate capabilities.
Myles Watson032a9652009-05-11 22:24:53 +0000730 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000731 * When only a PCI-Express capability is found the type
732 * is examined to see which type of bridge we have.
733 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000734 * @param dev Pointer to the device structure of the bridge.
735 * @return Appropriate bridge operations.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000736 */
737static struct device_operations *get_pci_bridge_ops(device_t dev)
738{
739 unsigned pos;
740
741#if CONFIG_PCIX_PLUGIN_SUPPORT == 1
742 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
743 if (pos) {
744 printk_debug("%s subbordinate bus PCI-X\n", dev_path(dev));
745 return &default_pcix_ops_bus;
746 }
747#endif
748#if CONFIG_AGP_PLUGIN_SUPPORT == 1
749 /* How do I detect an PCI to AGP bridge? */
750#endif
751#if CONFIG_HYPERTRANSPORT_PLUGIN_SUPPORT == 1
752 pos = 0;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000753 while ((pos = pci_find_next_capability(dev, PCI_CAP_ID_HT, pos))) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000754 unsigned flags;
755 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
756 if ((flags >> 13) == 1) {
757 /* Host or Secondary Interface */
Myles Watson032a9652009-05-11 22:24:53 +0000758 printk_debug("%s subbordinate bus Hypertransport\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000759 dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000760 return &default_ht_ops_bus;
761 }
762 }
763#endif
764#if CONFIG_PCIEXP_PLUGIN_SUPPORT == 1
765 pos = pci_find_capability(dev, PCI_CAP_ID_PCIE);
766 if (pos) {
767 unsigned flags;
768 flags = pci_read_config16(dev, pos + PCI_EXP_FLAGS);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000769 switch ((flags & PCI_EXP_FLAGS_TYPE) >> 4) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000770 case PCI_EXP_TYPE_ROOT_PORT:
771 case PCI_EXP_TYPE_UPSTREAM:
772 case PCI_EXP_TYPE_DOWNSTREAM:
Myles Watson032a9652009-05-11 22:24:53 +0000773 printk_debug("%s subbordinate bus PCI Express\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000774 dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000775 return &default_pciexp_ops_bus;
776 case PCI_EXP_TYPE_PCI_BRIDGE:
Myles Watson29cc9ed2009-07-02 18:56:24 +0000777 printk_debug("%s subbordinate PCI\n", dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000778 return &default_pci_ops_bus;
779 default:
780 break;
781 }
782 }
783#endif
784 return &default_pci_ops_bus;
785}
786
787/**
Myles Watson29cc9ed2009-07-02 18:56:24 +0000788 * Set up PCI device operation. Check if it already has a driver. If not, use
789 * find_device_operations, or set to a default based on type.
Li-Ta Loe5266692004-03-23 21:28:05 +0000790 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000791 * @param dev Pointer to the device whose pci_ops you want to set.
Li-Ta Loe5266692004-03-23 21:28:05 +0000792 * @see pci_drivers
793 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000794static void set_pci_ops(struct device *dev)
795{
796 struct pci_driver *driver;
797 if (dev->ops) {
798 return;
799 }
Li-Ta Loe5266692004-03-23 21:28:05 +0000800
Yinghai Lu5f9624d2006-10-04 22:56:21 +0000801 /* Look through the list of setup drivers and find one for
Myles Watson29cc9ed2009-07-02 18:56:24 +0000802 * this PCI device.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000803 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000804 for (driver = &pci_drivers[0]; driver != &epci_drivers[0]; driver++) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000805 if ((driver->vendor == dev->vendor) &&
Myles Watson29cc9ed2009-07-02 18:56:24 +0000806 (driver->device == dev->device)) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000807 dev->ops = driver->ops;
Myles Watson032a9652009-05-11 22:24:53 +0000808 printk_spew("%s [%04x/%04x] %sops\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000809 dev_path(dev),
810 driver->vendor, driver->device,
811 (driver->ops->scan_bus ? "bus " : ""));
Eric Biederman5899fd82003-04-24 06:25:08 +0000812 return;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000813 }
814 }
Li-Ta Loe5266692004-03-23 21:28:05 +0000815
Eric Biederman8ca8d762003-04-22 19:02:15 +0000816 /* If I don't have a specific driver use the default operations */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000817 switch (dev->hdr_type & 0x7f) { /* header type */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000818 case PCI_HEADER_TYPE_NORMAL: /* standard header */
819 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
820 goto bad;
821 dev->ops = &default_pci_ops_dev;
822 break;
823 case PCI_HEADER_TYPE_BRIDGE:
824 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
825 goto bad;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000826 dev->ops = get_pci_bridge_ops(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000827 break;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000828#if CONFIG_CARDBUS_PLUGIN_SUPPORT == 1
829 case PCI_HEADER_TYPE_CARDBUS:
830 dev->ops = &default_cardbus_ops_bus;
831 break;
832#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +0000833 default:
Myles Watson29cc9ed2009-07-02 18:56:24 +0000834 bad:
Li-Ta Lo69c5a902004-04-29 20:08:54 +0000835 if (dev->enabled) {
Eric Biederman83b991a2003-10-11 06:20:25 +0000836 printk_err("%s [%04x/%04x/%06x] has unknown header "
Myles Watson29cc9ed2009-07-02 18:56:24 +0000837 "type %02x, ignoring.\n",
838 dev_path(dev),
839 dev->vendor, dev->device,
840 dev->class >> 8, dev->hdr_type);
Eric Biederman83b991a2003-10-11 06:20:25 +0000841 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000842 }
843 return;
844}
845
846/**
Eric Biederman03acab62004-10-14 21:25:53 +0000847 * @brief See if we have already allocated a device structure for a given devfn.
Li-Ta Loe5266692004-03-23 21:28:05 +0000848 *
849 * Given a linked list of PCI device structures and a devfn number, find the
Li-Ta Lo3a812852004-12-03 22:39:34 +0000850 * device structure correspond to the devfn, if present. This function also
851 * removes the device structure from the linked list.
Li-Ta Loe5266692004-03-23 21:28:05 +0000852 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000853 * @param list The device structure list.
854 * @param devfn A device/function number.
Li-Ta Loe5266692004-03-23 21:28:05 +0000855 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000856 * @return Pointer to the device structure found or NULL if we have not
Li-Ta Lo3a812852004-12-03 22:39:34 +0000857 * allocated a device for this devfn yet.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000858 */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000859static struct device *pci_scan_get_dev(struct device **list, unsigned int devfn)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000860{
Eric Biedermanb78c1972004-10-14 20:54:17 +0000861 struct device *dev;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000862 dev = 0;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000863 for (; *list; list = &(*list)->sibling) {
Eric Biedermanad1b35a2003-10-14 02:36:51 +0000864 if ((*list)->path.type != DEVICE_PATH_PCI) {
Li-Ta Loe5266692004-03-23 21:28:05 +0000865 printk_err("child %s not a pci device\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000866 dev_path(*list));
Eric Biedermanad1b35a2003-10-14 02:36:51 +0000867 continue;
868 }
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000869 if ((*list)->path.pci.devfn == devfn) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000870 /* Unlink from the list. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000871 dev = *list;
872 *list = (*list)->sibling;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000873 dev->sibling = NULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000874 break;
875 }
876 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000877
878 /* Just like alloc_dev() add the device to the list of devices on the
879 * bus. When the list of devices was formed we removed all of the
880 * parents children, and now we are interleaving static and dynamic
881 * devices in order on the bus.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000882 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000883 if (dev) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000884 struct device *child;
885 /* Find the last child of our parent. */
886 for (child = dev->bus->children; child && child->sibling;) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000887 child = child->sibling;
888 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000889 /* Place the device on the list of children of its parent. */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000890 if (child) {
891 child->sibling = dev;
892 } else {
893 dev->bus->children = dev;
894 }
895 }
896
Eric Biederman8ca8d762003-04-22 19:02:15 +0000897 return dev;
898}
899
Myles Watson032a9652009-05-11 22:24:53 +0000900/**
Eric Biedermanb78c1972004-10-14 20:54:17 +0000901 * @brief Scan a PCI bus.
Li-Ta Loe5266692004-03-23 21:28:05 +0000902 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000903 * Determine the existence of a given PCI device. Allocate a new struct device
904 * if dev==NULL was passed in and the device exists in hardware.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000905 *
906 * @param bus pointer to the bus structure
907 * @param devfn to look at
908 *
909 * @return The device structure for hte device (if found)
910 * or the NULL if no device is found.
911 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000912device_t pci_probe_dev(device_t dev, struct bus * bus, unsigned devfn)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000913{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000914 u32 id, class;
915 u8 hdr_type;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000916
Myles Watson29cc9ed2009-07-02 18:56:24 +0000917 /* Detect if a device is present. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000918 if (!dev) {
919 struct device dummy;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000920 dummy.bus = bus;
921 dummy.path.type = DEVICE_PATH_PCI;
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000922 dummy.path.pci.devfn = devfn;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000923 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000924 /* Have we found something?
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000925 * Some broken boards return 0 if a slot is empty.
926 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000927 if ((id == 0xffffffff) || (id == 0x00000000) ||
928 (id == 0x0000ffff) || (id == 0xffff0000)) {
Stefan Reinauerf657d752008-09-11 06:52:22 +0000929 printk_spew("%s, bad id 0x%x\n", dev_path(&dummy), id);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000930 return NULL;
931 }
932 dev = alloc_dev(bus, &dummy.path);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000933 } else {
934 /* Enable/disable the device. Once we have found the device-
935 * specific operations this operations we will disable the
936 * device with those as well.
Myles Watson032a9652009-05-11 22:24:53 +0000937 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000938 * This is geared toward devices that have subfunctions
939 * that do not show up by default.
Myles Watson032a9652009-05-11 22:24:53 +0000940 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000941 * If a device is a stuff option on the motherboard
Myles Watson29cc9ed2009-07-02 18:56:24 +0000942 * it may be absent and enable_dev() must cope.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000943 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000944 /* Run the magic enable sequence for the device. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000945 if (dev->chip_ops && dev->chip_ops->enable_dev) {
946 dev->chip_ops->enable_dev(dev);
947 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000948 /* Now read the vendor and device ID. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000949 id = pci_read_config32(dev, PCI_VENDOR_ID);
Myles Watson032a9652009-05-11 22:24:53 +0000950
Myles Watson29cc9ed2009-07-02 18:56:24 +0000951 /* If the device does not have a PCI ID disable it. Possibly
952 * this is because we have already disabled the device. But
953 * this also handles optional devices that may not always
954 * show up.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000955 */
956 /* If the chain is fully enumerated quit */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000957 if ((id == 0xffffffff) || (id == 0x00000000) ||
958 (id == 0x0000ffff) || (id == 0xffff0000)) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000959 if (dev->enabled) {
960 printk_info("Disabling static device: %s\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000961 dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000962 dev->enabled = 0;
963 }
964 return dev;
965 }
966 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000967 /* Read the rest of the PCI configuration information. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000968 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
969 class = pci_read_config32(dev, PCI_CLASS_REVISION);
Myles Watson032a9652009-05-11 22:24:53 +0000970
Myles Watson29cc9ed2009-07-02 18:56:24 +0000971 /* Store the interesting information in the device structure. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000972 dev->vendor = id & 0xffff;
973 dev->device = (id >> 16) & 0xffff;
974 dev->hdr_type = hdr_type;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000975
976 /* Class code, the upper 3 bytes of PCI_CLASS_REVISION. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000977 dev->class = class >> 8;
Myles Watson032a9652009-05-11 22:24:53 +0000978
Myles Watson29cc9ed2009-07-02 18:56:24 +0000979 /* Architectural/System devices always need to be bus masters. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000980 if ((dev->class >> 16) == PCI_BASE_CLASS_SYSTEM) {
981 dev->command |= PCI_COMMAND_MASTER;
982 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000983 /* Look at the vendor and device ID, or at least the header type and
984 * class and figure out which set of configuration methods to use.
985 * Unless we already have some PCI ops.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000986 */
987 set_pci_ops(dev);
988
Myles Watson29cc9ed2009-07-02 18:56:24 +0000989 /* Now run the magic enable/disable sequence for the device. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000990 if (dev->ops && dev->ops->enable) {
991 dev->ops->enable(dev);
992 }
Myles Watson032a9652009-05-11 22:24:53 +0000993
Myles Watson29cc9ed2009-07-02 18:56:24 +0000994 /* Display the device. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000995 printk_debug("%s [%04x/%04x] %s%s\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000996 dev_path(dev),
997 dev->vendor, dev->device,
998 dev->enabled ? "enabled" : "disabled",
999 dev->ops ? "" : " No operations");
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001000
1001 return dev;
1002}
1003
Myles Watson032a9652009-05-11 22:24:53 +00001004/**
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001005 * @brief Scan a PCI bus.
1006 *
Li-Ta Loe5266692004-03-23 21:28:05 +00001007 * Determine the existence of devices and bridges on a PCI bus. If there are
1008 * bridges on the bus, recursively scan the buses behind the bridges.
1009 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001010 * This function is the default scan_bus() method for the root device
1011 * 'dev_root'.
1012 *
Eric Biedermane9a271e32003-09-02 03:36:25 +00001013 * @param bus pointer to the bus structure
1014 * @param min_devfn minimum devfn to look at in the scan usually 0x00
1015 * @param max_devfn maximum devfn to look at in the scan usually 0xff
Eric Biederman8ca8d762003-04-22 19:02:15 +00001016 * @param max current bus number
Li-Ta Loe5266692004-03-23 21:28:05 +00001017 *
Eric Biederman8ca8d762003-04-22 19:02:15 +00001018 * @return The maximum bus number found, after scanning all subordinate busses
1019 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001020unsigned int pci_scan_bus(struct bus *bus,
Myles Watson29cc9ed2009-07-02 18:56:24 +00001021 unsigned min_devfn, unsigned max_devfn,
1022 unsigned int max)
Eric Biederman8ca8d762003-04-22 19:02:15 +00001023{
1024 unsigned int devfn;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001025 struct device *old_devices;
1026 struct device *child;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001027
Stefan Reinauer08670622009-06-30 15:17:49 +00001028#if CONFIG_PCI_BUS_SEGN_BITS
Myles Watson29cc9ed2009-07-02 18:56:24 +00001029 printk_debug("PCI: pci_scan_bus for bus %04x:%02x\n",
1030 bus->secondary >> 8, bus->secondary & 0xff);
Yinghai Lu5f9624d2006-10-04 22:56:21 +00001031#else
1032 printk_debug("PCI: pci_scan_bus for bus %02x\n", bus->secondary);
1033#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +00001034
1035 old_devices = bus->children;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001036 bus->children = NULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001037
1038 post_code(0x24);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001039 /* Probe all devices/functions on this bus with some optimization for
1040 * non-existence and single function devices.
Eric Biedermanb78c1972004-10-14 20:54:17 +00001041 */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001042 for (devfn = min_devfn; devfn <= max_devfn; devfn++) {
Myles Watson29cc9ed2009-07-02 18:56:24 +00001043 struct device *dev;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001044
Eric Biederman03acab62004-10-14 21:25:53 +00001045 /* First thing setup the device structure */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001046 dev = pci_scan_get_dev(&old_devices, devfn);
Li-Ta Lo9782f752004-05-05 21:15:42 +00001047
Myles Watson29cc9ed2009-07-02 18:56:24 +00001048 /* See if a device is present and setup the device structure. */
Myles Watson032a9652009-05-11 22:24:53 +00001049 dev = pci_probe_dev(dev, bus, devfn);
Eric Biederman03acab62004-10-14 21:25:53 +00001050
Myles Watson29cc9ed2009-07-02 18:56:24 +00001051 /* If this is not a multi function device, or the device is
1052 * not present don't waste time probing another function.
Myles Watson032a9652009-05-11 22:24:53 +00001053 * Skip to next device.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001054 */
Myles Watson032a9652009-05-11 22:24:53 +00001055 if ((PCI_FUNC(devfn) == 0x00) &&
Myles Watson29cc9ed2009-07-02 18:56:24 +00001056 (!dev
1057 || (dev->enabled && ((dev->hdr_type & 0x80) != 0x80)))) {
Eric Biederman8ca8d762003-04-22 19:02:15 +00001058 devfn += 0x07;
1059 }
1060 }
1061 post_code(0x25);
1062
Myles Watson29cc9ed2009-07-02 18:56:24 +00001063 /* Warn if any leftover static devices are are found.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001064 * There's probably a problem in the Config.lb.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001065 */
1066 if (old_devices) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001067 device_t left;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001068 printk_warning("PCI: Left over static devices:\n");
1069 for (left = old_devices; left; left = left->sibling) {
1070 printk_warning("%s\n", dev_path(left));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001071 }
Myles Watson29cc9ed2009-07-02 18:56:24 +00001072 printk_warning("PCI: Check your mainboard Config.lb.\n");
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001073 }
1074
Myles Watson29cc9ed2009-07-02 18:56:24 +00001075 /* For all children that implement scan_bus() (i.e. bridges)
Eric Biedermanb78c1972004-10-14 20:54:17 +00001076 * scan the bus behind that child.
1077 */
Myles Watson29cc9ed2009-07-02 18:56:24 +00001078 for (child = bus->children; child; child = child->sibling) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001079 max = scan_bus(child, max);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001080 }
Li-Ta Loe5266692004-03-23 21:28:05 +00001081
Myles Watson29cc9ed2009-07-02 18:56:24 +00001082 /* We've scanned the bus and so we know all about what's on the other
1083 * side of any bridges that may be on this bus plus any devices.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001084 * Return how far we've got finding sub-buses.
1085 */
Yinghai Lu5f9624d2006-10-04 22:56:21 +00001086 printk_debug("PCI: pci_scan_bus returning with max=%03x\n", max);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001087 post_code(0x55);
1088 return max;
1089}
1090
Li-Ta Loe5266692004-03-23 21:28:05 +00001091/**
1092 * @brief Scan a PCI bridge and the buses behind the bridge.
1093 *
1094 * Determine the existence of buses behind the bridge. Set up the bridge
1095 * according to the result of the scan.
1096 *
1097 * This function is the default scan_bus() method for PCI bridge devices.
1098 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001099 * @param dev Pointer to the bridge device.
1100 * @param max The highest bus number assigned up to now.
1101 * @return The maximum bus number found, after scanning all subordinate buses.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001102 */
Myles Watson032a9652009-05-11 22:24:53 +00001103unsigned int do_pci_scan_bridge(struct device *dev, unsigned int max,
Myles Watson29cc9ed2009-07-02 18:56:24 +00001104 unsigned int (*do_scan_bus) (struct bus * bus,
1105 unsigned min_devfn,
1106 unsigned max_devfn,
1107 unsigned int max))
Eric Biederman8ca8d762003-04-22 19:02:15 +00001108{
Eric Biedermane9a271e32003-09-02 03:36:25 +00001109 struct bus *bus;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001110 u32 buses;
1111 u16 cr;
Eric Biederman83b991a2003-10-11 06:20:25 +00001112
Li-Ta Lo3a812852004-12-03 22:39:34 +00001113 printk_spew("%s for %s\n", __func__, dev_path(dev));
1114
Eric Biedermane9a271e32003-09-02 03:36:25 +00001115 bus = &dev->link[0];
Eric Biedermana9e632c2004-11-18 22:38:08 +00001116 bus->dev = dev;
Eric Biedermane9a271e32003-09-02 03:36:25 +00001117 dev->links = 1;
1118
Eric Biederman8ca8d762003-04-22 19:02:15 +00001119 /* Set up the primary, secondary and subordinate bus numbers. We have
1120 * no idea how many buses are behind this bridge yet, so we set the
Myles Watson032a9652009-05-11 22:24:53 +00001121 * subordinate bus number to 0xff for the moment.
Eric Biedermanb78c1972004-10-14 20:54:17 +00001122 */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001123 bus->secondary = ++max;
1124 bus->subordinate = 0xff;
Li-Ta Loe5266692004-03-23 21:28:05 +00001125
Eric Biederman8ca8d762003-04-22 19:02:15 +00001126 /* Clear all status bits and turn off memory, I/O and master enables. */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001127 cr = pci_read_config16(dev, PCI_COMMAND);
1128 pci_write_config16(dev, PCI_COMMAND, 0x0000);
1129 pci_write_config16(dev, PCI_STATUS, 0xffff);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001130
Myles Watson29cc9ed2009-07-02 18:56:24 +00001131 /* Read the existing primary/secondary/subordinate bus
Eric Biedermanb78c1972004-10-14 20:54:17 +00001132 * number configuration.
1133 */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001134 buses = pci_read_config32(dev, PCI_PRIMARY_BUS);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001135
1136 /* Configure the bus numbers for this bridge: the configuration
1137 * transactions will not be propagated by the bridge if it is not
Eric Biedermanb78c1972004-10-14 20:54:17 +00001138 * correctly configured.
1139 */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001140 buses &= 0xff000000;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001141 buses |= (((unsigned int)(dev->bus->secondary) << 0) |
1142 ((unsigned int)(bus->secondary) << 8) |
1143 ((unsigned int)(bus->subordinate) << 16));
Eric Biedermane9a271e32003-09-02 03:36:25 +00001144 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
Li-Ta Lo3a812852004-12-03 22:39:34 +00001145
Myles Watson032a9652009-05-11 22:24:53 +00001146 /* Now we can scan all subordinate buses
Eric Biedermanb78c1972004-10-14 20:54:17 +00001147 * i.e. the bus behind the bridge.
1148 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001149 max = do_scan_bus(bus, 0x00, 0xff, max);
Li-Ta Lo3a812852004-12-03 22:39:34 +00001150
Eric Biederman8ca8d762003-04-22 19:02:15 +00001151 /* We know the number of buses behind this bridge. Set the subordinate
Eric Biedermanb78c1972004-10-14 20:54:17 +00001152 * bus number to its real value.
1153 */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001154 bus->subordinate = max;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001155 buses = (buses & 0xff00ffff) | ((unsigned int)(bus->subordinate) << 16);
Eric Biedermane9a271e32003-09-02 03:36:25 +00001156 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
1157 pci_write_config16(dev, PCI_COMMAND, cr);
Myles Watson032a9652009-05-11 22:24:53 +00001158
Eric Biedermanb78c1972004-10-14 20:54:17 +00001159 printk_spew("%s returns max %d\n", __func__, max);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001160 return max;
1161}
Li-Ta Loe5266692004-03-23 21:28:05 +00001162
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001163/**
1164 * @brief Scan a PCI bridge and the buses behind the bridge.
1165 *
1166 * Determine the existence of buses behind the bridge. Set up the bridge
1167 * according to the result of the scan.
1168 *
1169 * This function is the default scan_bus() method for PCI bridge devices.
1170 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001171 * @param dev Pointer to the bridge device.
1172 * @param max The highest bus number assigned up to now.
1173 * @return The maximum bus number found, after scanning all subordinate buses.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001174 */
1175unsigned int pci_scan_bridge(struct device *dev, unsigned int max)
1176{
1177 return do_pci_scan_bridge(dev, max, pci_scan_bus);
1178}
1179
Myles Watson29cc9ed2009-07-02 18:56:24 +00001180/**
1181 * @brief Scan a PCI domain.
1182 *
1183 * This function is the default scan_bus() method for PCI domains.
1184 *
1185 * @param dev pointer to the domain
1186 * @param max the highest bus number assgined up to now
1187 *
1188 * @return The maximum bus number found, after scanning all subordinate busses
1189 */
1190unsigned int pci_domain_scan_bus(device_t dev, unsigned int max)
1191{
1192 max = pci_scan_bus(&dev->link[0], PCI_DEVFN(0, 0), 0xff, max);
1193 return max;
1194}
1195
1196/**
1197 * Tell the EISA int controller this int must be level triggered.
1198 *
1199 * THIS IS A KLUDGE -- sorry, this needs to get cleaned up.
1200 */
Ronald G. Minnich88fb1a62006-06-22 04:37:27 +00001201void pci_level_irq(unsigned char intNum)
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001202{
Myles Watson29cc9ed2009-07-02 18:56:24 +00001203 unsigned short intBits = inb(0x4d0) | (((unsigned)inb(0x4d1)) << 8);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001204
Eric Biedermanb78c1972004-10-14 20:54:17 +00001205 printk_spew("%s: current ints are 0x%x\n", __func__, intBits);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001206 intBits |= (1 << intNum);
1207
Eric Biedermanb78c1972004-10-14 20:54:17 +00001208 printk_spew("%s: try to set ints 0x%x\n", __func__, intBits);
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +00001209
Myles Watson29cc9ed2009-07-02 18:56:24 +00001210 /* Write new values. */
1211 outb((unsigned char)intBits, 0x4d0);
1212 outb((unsigned char)(intBits >> 8), 0x4d1);
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +00001213
Myles Watson29cc9ed2009-07-02 18:56:24 +00001214 /* This seems like an error but is not. */
Ronald G. Minnich2cf779d2006-09-18 22:50:51 +00001215 if (inb(0x4d0) != (intBits & 0xff)) {
Myles Watson29cc9ed2009-07-02 18:56:24 +00001216 printk_err(
1217 "%s: lower order bits are wrong: want 0x%x, got 0x%x\n",
1218 __func__, intBits & 0xff, inb(0x4d0));
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +00001219 }
Ronald G. Minnich2cf779d2006-09-18 22:50:51 +00001220 if (inb(0x4d1) != ((intBits >> 8) & 0xff)) {
Myles Watson29cc9ed2009-07-02 18:56:24 +00001221 printk_err(
1222 "%s: lower order bits are wrong: want 0x%x, got 0x%x\n",
1223 __func__, (intBits >> 8) & 0xff, inb(0x4d1));
Ronald G. Minnichcb3f4982003-10-02 18:16:07 +00001224 }
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001225}
1226
Myles Watson29cc9ed2009-07-02 18:56:24 +00001227/**
1228 * This function assigns IRQs for all functions contained within the
1229 * indicated device address. If the device does not exist or does not
1230 * require interrupts then this function has no effect.
1231 *
1232 * This function should be called for each PCI slot in your system.
1233 *
1234 * pIntAtoD is an array of IRQ #s that are assigned to PINTA through PINTD of
1235 * this slot.
1236 *
1237 * The particular irq #s that are passed in depend on the routing inside
1238 * your southbridge and on your motherboard.
1239 *
1240 * -kevinh@ispiri.com
1241 *
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001242*/
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001243void pci_assign_irqs(unsigned bus, unsigned slot,
Myles Watson29cc9ed2009-07-02 18:56:24 +00001244 const unsigned char pIntAtoD[4])
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001245{
1246 unsigned functNum;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001247 struct device *pdev;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001248 unsigned char line;
1249 unsigned char irq;
1250 unsigned char readback;
1251
Myles Watson29cc9ed2009-07-02 18:56:24 +00001252 /* Each slot may contain up to eight functions. */
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001253 for (functNum = 0; functNum < 8; functNum++) {
1254 pdev = dev_find_slot(bus, (slot << 3) + functNum);
1255
1256 if (pdev) {
Myles Watson29cc9ed2009-07-02 18:56:24 +00001257 line = pci_read_config8(pdev, PCI_INTERRUPT_PIN);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001258
Myles Watson29cc9ed2009-07-02 18:56:24 +00001259 /* PCI spec says all other values are reserved. */
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001260 if ((line >= 1) && (line <= 4)) {
1261 irq = pIntAtoD[line - 1];
1262
Myles Watson29cc9ed2009-07-02 18:56:24 +00001263 printk_debug("Assigning IRQ %d to %d:%x.%d\n",
1264 irq, bus, slot, functNum);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001265
Myles Watson29cc9ed2009-07-02 18:56:24 +00001266 pci_write_config8(pdev, PCI_INTERRUPT_LINE,
1267 pIntAtoD[line - 1]);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001268
Myles Watson29cc9ed2009-07-02 18:56:24 +00001269 readback =
1270 pci_read_config8(pdev, PCI_INTERRUPT_LINE);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001271 printk_debug(" Readback = %d\n", readback);
1272
Myles Watson29cc9ed2009-07-02 18:56:24 +00001273 // Change to level triggered.
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001274 pci_level_irq(pIntAtoD[line - 1]);
1275 }
1276 }
1277 }
1278}