blob: 2360028756d57309b1eb92abba49d1ca7afc5103 [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
Stefan Reinauer4d933dd2009-07-21 21:36:41 +000053#define CONFIG_PC80_SYSTEM 1
54#if CONFIG_PC80_SYSTEM == 1
55#include <pc80/i8259.h>
56#endif
Eric Biederman03acab62004-10-14 21:25:53 +000057
Myles Watson29cc9ed2009-07-02 18:56:24 +000058u8 pci_moving_config8(struct device *dev, unsigned int reg)
Eric Biederman03acab62004-10-14 21:25:53 +000059{
Myles Watson29cc9ed2009-07-02 18:56:24 +000060 u8 value, ones, zeroes;
Eric Biederman03acab62004-10-14 21:25:53 +000061 value = pci_read_config8(dev, reg);
Myles Watson032a9652009-05-11 22:24:53 +000062
Eric Biederman03acab62004-10-14 21:25:53 +000063 pci_write_config8(dev, reg, 0xff);
64 ones = pci_read_config8(dev, reg);
65
66 pci_write_config8(dev, reg, 0x00);
67 zeroes = pci_read_config8(dev, reg);
68
69 pci_write_config8(dev, reg, value);
70
71 return ones ^ zeroes;
72}
Li-Ta Lo9a5b4962004-12-23 21:48:01 +000073
Myles Watson29cc9ed2009-07-02 18:56:24 +000074u16 pci_moving_config16(struct device * dev, unsigned int reg)
Eric Biederman03acab62004-10-14 21:25:53 +000075{
Myles Watson29cc9ed2009-07-02 18:56:24 +000076 u16 value, ones, zeroes;
Eric Biederman03acab62004-10-14 21:25:53 +000077 value = pci_read_config16(dev, reg);
Myles Watson032a9652009-05-11 22:24:53 +000078
Eric Biederman03acab62004-10-14 21:25:53 +000079 pci_write_config16(dev, reg, 0xffff);
80 ones = pci_read_config16(dev, reg);
81
82 pci_write_config16(dev, reg, 0x0000);
83 zeroes = pci_read_config16(dev, reg);
84
85 pci_write_config16(dev, reg, value);
86
87 return ones ^ zeroes;
88}
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +000089
Myles Watson29cc9ed2009-07-02 18:56:24 +000090u32 pci_moving_config32(struct device * dev, unsigned int reg)
Eric Biederman03acab62004-10-14 21:25:53 +000091{
Myles Watson29cc9ed2009-07-02 18:56:24 +000092 u32 value, ones, zeroes;
Eric Biederman03acab62004-10-14 21:25:53 +000093 value = pci_read_config32(dev, reg);
Myles Watson032a9652009-05-11 22:24:53 +000094
Eric Biederman03acab62004-10-14 21:25:53 +000095 pci_write_config32(dev, reg, 0xffffffff);
96 ones = pci_read_config32(dev, reg);
97
98 pci_write_config32(dev, reg, 0x00000000);
99 zeroes = pci_read_config32(dev, reg);
100
101 pci_write_config32(dev, reg, value);
102
103 return ones ^ zeroes;
104}
105
Myles Watson29cc9ed2009-07-02 18:56:24 +0000106/**
107 * Given a device, a capability type, and a last position, return the next
108 * matching capability. Always start at the head of the list.
109 *
110 * @param dev Pointer to the device structure.
111 * @param cap_type PCI_CAP_LIST_ID of the PCI capability we're looking for.
112 * @param last Location of the PCI capability register to start from.
113 */
114unsigned pci_find_next_capability(struct device *dev, unsigned cap,
115 unsigned last)
Eric Biederman03acab62004-10-14 21:25:53 +0000116{
Stefan Reinauer4d933dd2009-07-21 21:36:41 +0000117 unsigned pos = 0;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000118 unsigned status;
119 unsigned reps = 48;
Stefan Reinauer4d933dd2009-07-21 21:36:41 +0000120
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000121 status = pci_read_config16(dev, PCI_STATUS);
122 if (!(status & PCI_STATUS_CAP_LIST)) {
123 return 0;
124 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000125 switch (dev->hdr_type & 0x7f) {
Eric Biederman03acab62004-10-14 21:25:53 +0000126 case PCI_HEADER_TYPE_NORMAL:
127 case PCI_HEADER_TYPE_BRIDGE:
128 pos = PCI_CAPABILITY_LIST;
129 break;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000130 case PCI_HEADER_TYPE_CARDBUS:
131 pos = PCI_CB_CAPABILITY_LIST;
132 break;
133 default:
134 return 0;
Eric Biederman03acab62004-10-14 21:25:53 +0000135 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000136 pos = pci_read_config8(dev, pos);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000137 while (reps-- && (pos >= 0x40)) { /* Loop through the linked list. */
Eric Biederman03acab62004-10-14 21:25:53 +0000138 int this_cap;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000139 pos &= ~3;
Eric Biederman03acab62004-10-14 21:25:53 +0000140 this_cap = pci_read_config8(dev, pos + PCI_CAP_LIST_ID);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000141 printk_spew("Capability: type 0x%02x @ 0x%02x\n", this_cap,
142 pos);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000143 if (this_cap == 0xff) {
144 break;
145 }
146 if (!last && (this_cap == cap)) {
Eric Biederman03acab62004-10-14 21:25:53 +0000147 return pos;
148 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000149 if (last == pos) {
150 last = 0;
151 }
152 pos = pci_read_config8(dev, pos + PCI_CAP_LIST_NEXT);
Eric Biederman03acab62004-10-14 21:25:53 +0000153 }
154 return 0;
155}
156
Myles Watson29cc9ed2009-07-02 18:56:24 +0000157/**
158 * Given a device, and a capability type, return the next matching
159 * capability. Always start at the head of the list.
160 *
161 * @param dev Pointer to the device structure.
162 * @param cap_type PCI_CAP_LIST_ID of the PCI capability we're looking for.
163 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000164unsigned pci_find_capability(device_t dev, unsigned cap)
165{
166 return pci_find_next_capability(dev, cap, 0);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000167}
168
Myles Watson29cc9ed2009-07-02 18:56:24 +0000169/**
170 * Given a device and register, read the size of the BAR for that register.
171 *
172 * @param dev Pointer to the device structure.
173 * @param index Address of the PCI configuration register.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000174 */
Eric Biederman03acab62004-10-14 21:25:53 +0000175struct resource *pci_get_resource(struct device *dev, unsigned long index)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000176{
Eric Biederman5cd81732004-03-11 15:01:31 +0000177 struct resource *resource;
Eric Biederman03acab62004-10-14 21:25:53 +0000178 unsigned long value, attr;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000179 resource_t moving, limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000180
Myles Watson29cc9ed2009-07-02 18:56:24 +0000181 /* Initialize the resources to nothing. */
Eric Biederman03acab62004-10-14 21:25:53 +0000182 resource = new_resource(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000183
Myles Watson29cc9ed2009-07-02 18:56:24 +0000184 /* Get the initial value. */
Eric Biederman03acab62004-10-14 21:25:53 +0000185 value = pci_read_config32(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000186
Myles Watson29cc9ed2009-07-02 18:56:24 +0000187 /* See which bits move. */
Eric Biederman03acab62004-10-14 21:25:53 +0000188 moving = pci_moving_config32(dev, index);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000189
Myles Watson29cc9ed2009-07-02 18:56:24 +0000190 /* Initialize attr to the bits that do not move. */
Eric Biederman03acab62004-10-14 21:25:53 +0000191 attr = value & ~moving;
192
Myles Watson29cc9ed2009-07-02 18:56:24 +0000193 /* If it is a 64bit resource look at the high half as well. */
Eric Biederman03acab62004-10-14 21:25:53 +0000194 if (((attr & PCI_BASE_ADDRESS_SPACE_IO) == 0) &&
Myles Watson29cc9ed2009-07-02 18:56:24 +0000195 ((attr & PCI_BASE_ADDRESS_MEM_LIMIT_MASK) ==
196 PCI_BASE_ADDRESS_MEM_LIMIT_64)) {
197 /* Find the high bits that move. */
198 moving |=
199 ((resource_t) pci_moving_config32(dev, index + 4)) << 32;
Eric Biederman03acab62004-10-14 21:25:53 +0000200 }
Myles Watson032a9652009-05-11 22:24:53 +0000201 /* Find the resource constraints.
Eric Biederman03acab62004-10-14 21:25:53 +0000202 * Start by finding the bits that move. From there:
203 * - Size is the least significant bit of the bits that move.
204 * - Limit is all of the bits that move plus all of the lower bits.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000205 * See PCI Spec 6.2.5.1.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000206 */
Eric Biederman03acab62004-10-14 21:25:53 +0000207 limit = 0;
208 if (moving) {
209 resource->size = 1;
210 resource->align = resource->gran = 0;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000211 while (!(moving & resource->size)) {
Eric Biederman03acab62004-10-14 21:25:53 +0000212 resource->size <<= 1;
213 resource->align += 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000214 resource->gran += 1;
Eric Biederman03acab62004-10-14 21:25:53 +0000215 }
216 resource->limit = limit = moving | (resource->size - 1);
217 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000218
219 /* Some broken hardware has read-only registers that do not
Eric Biederman03acab62004-10-14 21:25:53 +0000220 * really size correctly.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000221 * Example: the Acer M7229 has BARs 1-4 normally read-only.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000222 * so BAR1 at offset 0x10 reads 0x1f1. If you size that register
Myles Watson032a9652009-05-11 22:24:53 +0000223 * by writing 0xffffffff to it, it will read back as 0x1f1 -- a
224 * violation of the spec.
Eric Biederman03acab62004-10-14 21:25:53 +0000225 * We catch this case and ignore it by observing which bits move,
226 * This also catches the common case unimplemented registers
227 * that always read back as 0.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000228 */
Eric Biederman03acab62004-10-14 21:25:53 +0000229 if (moving == 0) {
230 if (value != 0) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000231 printk_debug
232 ("%s register %02lx(%08lx), read-only ignoring it\n",
233 dev_path(dev), index, value);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000234 }
235 resource->flags = 0;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000236 } else if (attr & PCI_BASE_ADDRESS_SPACE_IO) {
237 /* An I/O mapped base address. */
Eric Biederman03acab62004-10-14 21:25:53 +0000238 attr &= PCI_BASE_ADDRESS_IO_ATTR_MASK;
Eric Biederman5cd81732004-03-11 15:01:31 +0000239 resource->flags |= IORESOURCE_IO;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000240 /* I don't want to deal with 32bit I/O resources. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000241 resource->limit = 0xffff;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000242 } else {
243 /* A Memory mapped base address. */
Eric Biederman03acab62004-10-14 21:25:53 +0000244 attr &= PCI_BASE_ADDRESS_MEM_ATTR_MASK;
Eric Biederman5cd81732004-03-11 15:01:31 +0000245 resource->flags |= IORESOURCE_MEM;
Eric Biederman03acab62004-10-14 21:25:53 +0000246 if (attr & PCI_BASE_ADDRESS_MEM_PREFETCH) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000247 resource->flags |= IORESOURCE_PREFETCH;
248 }
Eric Biederman03acab62004-10-14 21:25:53 +0000249 attr &= PCI_BASE_ADDRESS_MEM_LIMIT_MASK;
250 if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_32) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000251 /* 32bit limit. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000252 resource->limit = 0xffffffffUL;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000253 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_1M) {
254 /* 1MB limit. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000255 resource->limit = 0x000fffffUL;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000256 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_64) {
257 /* 64bit limit. */
Eric Biederman03acab62004-10-14 21:25:53 +0000258 resource->limit = 0xffffffffffffffffULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000259 resource->flags |= IORESOURCE_PCI64;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000260 } else {
261 /* Invalid value. */
262 printk_err("Broken BAR with value %lx\n", attr);
263 printk_err(" on dev %s at index %02lx\n",
264 dev_path(dev), index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000265 resource->flags = 0;
266 }
267 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000268 /* Don't let the limit exceed which bits can move. */
Eric Biederman03acab62004-10-14 21:25:53 +0000269 if (resource->limit > limit) {
270 resource->limit = limit;
271 }
Eric Biederman03acab62004-10-14 21:25:53 +0000272
Eric Biederman5cd81732004-03-11 15:01:31 +0000273 return resource;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000274}
275
Myles Watson29cc9ed2009-07-02 18:56:24 +0000276/**
277 * Given a device and an index, read the size of the BAR for that register.
278 *
279 * @param dev Pointer to the device structure.
280 * @param index Address of the PCI configuration register.
281 */
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000282static void pci_get_rom_resource(struct device *dev, unsigned long index)
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000283{
284 struct resource *resource;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000285 unsigned long value;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000286 resource_t moving;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000287
Myles Watson29cc9ed2009-07-02 18:56:24 +0000288 if ((dev->on_mainboard) && (dev->rom_address == 0)) {
289 /* Skip it if rom_address is not set in the MB Config.lb. */
290 return;
291 }
Yinghai Lubcde1612005-01-14 05:34:09 +0000292
Myles Watson29cc9ed2009-07-02 18:56:24 +0000293 /* Initialize the resources to nothing. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000294 resource = new_resource(dev, index);
295
Myles Watson29cc9ed2009-07-02 18:56:24 +0000296 /* Get the initial value. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000297 value = pci_read_config32(dev, index);
298
Myles Watson29cc9ed2009-07-02 18:56:24 +0000299 /* See which bits move. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000300 moving = pci_moving_config32(dev, index);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000301
302 /* Clear the Enable bit. */
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000303 moving = moving & ~PCI_ROM_ADDRESS_ENABLE;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000304
Myles Watson032a9652009-05-11 22:24:53 +0000305 /* Find the resource constraints.
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000306 * Start by finding the bits that move. From there:
307 * - Size is the least significant bit of the bits that move.
308 * - Limit is all of the bits that move plus all of the lower bits.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000309 * See PCI Spec 6.2.5.1.
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000310 */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000311 if (moving) {
312 resource->size = 1;
313 resource->align = resource->gran = 0;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000314 while (!(moving & resource->size)) {
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000315 resource->size <<= 1;
316 resource->align += 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000317 resource->gran += 1;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000318 }
Patrick Georgi16cdbb22009-04-21 20:14:31 +0000319 resource->limit = moving | (resource->size - 1);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000320 resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY;
321 } else {
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000322 if (value != 0) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000323 printk_debug
324 ("%s register %02lx(%08lx), read-only ignoring it\n",
325 dev_path(dev), index, value);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000326 }
327 resource->flags = 0;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000328 }
Yinghai Luc7870ac2005-01-13 19:14:52 +0000329
Myles Watson29cc9ed2009-07-02 18:56:24 +0000330 /* For on board device with embedded ROM image, the ROM image is at
Yinghai Luc7870ac2005-01-13 19:14:52 +0000331 * fixed address specified in the Config.lb, the dev->rom_address is
332 * inited by driver_pci_onboard_ops::enable_dev() */
Yinghai Lubcde1612005-01-14 05:34:09 +0000333 if ((dev->on_mainboard) && (dev->rom_address != 0)) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000334 resource->base = dev->rom_address;
Myles Watsonce9d8642009-08-19 19:12:39 +0000335 /* The resource allocator needs the size to be non-zero. */
336 resource->size = 0x100;
Yinghai Luc7870ac2005-01-13 19:14:52 +0000337 resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY |
Myles Watson29cc9ed2009-07-02 18:56:24 +0000338 IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
Myles Watson032a9652009-05-11 22:24:53 +0000339 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000340
341 compact_resources(dev);
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000342}
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000343
Myles Watson29cc9ed2009-07-02 18:56:24 +0000344/**
345 * Read the base address registers for a given device.
346 *
347 * @param dev Pointer to the dev structure.
348 * @param howmany How many registers to read (6 for device, 2 for bridge).
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000349 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000350static void pci_read_bases(struct device *dev, unsigned int howmany)
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000351{
352 unsigned long index;
353
Myles Watson29cc9ed2009-07-02 18:56:24 +0000354 for (index = PCI_BASE_ADDRESS_0;
355 (index < PCI_BASE_ADDRESS_0 + (howmany << 2));) {
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000356 struct resource *resource;
357 resource = pci_get_resource(dev, index);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000358 index += (resource->flags & IORESOURCE_PCI64) ? 8 : 4;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000359 }
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000360
361 compact_resources(dev);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000362}
363
Myles Watson29cc9ed2009-07-02 18:56:24 +0000364static void pci_record_bridge_resource(struct device *dev, resource_t moving,
365 unsigned index, unsigned long type)
Eric Biederman03acab62004-10-14 21:25:53 +0000366{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000367 /* Initialize the constraints on the current bus. */
Eric Biederman03acab62004-10-14 21:25:53 +0000368 struct resource *resource;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000369 resource = NULL;
Eric Biederman03acab62004-10-14 21:25:53 +0000370 if (moving) {
371 unsigned long gran;
372 resource_t step;
373 resource = new_resource(dev, index);
374 resource->size = 0;
375 gran = 0;
376 step = 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000377 while ((moving & step) == 0) {
Eric Biederman03acab62004-10-14 21:25:53 +0000378 gran += 1;
379 step <<= 1;
380 }
381 resource->gran = gran;
382 resource->align = gran;
383 resource->limit = moving | (step - 1);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000384 resource->flags = type | IORESOURCE_PCI_BRIDGE |
385 IORESOURCE_BRIDGE;
Eric Biederman03acab62004-10-14 21:25:53 +0000386 }
387 return;
388}
389
Eric Biederman8ca8d762003-04-22 19:02:15 +0000390static void pci_bridge_read_bases(struct device *dev)
391{
Eric Biederman03acab62004-10-14 21:25:53 +0000392 resource_t moving_base, moving_limit, moving;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000393
Myles Watson29cc9ed2009-07-02 18:56:24 +0000394 /* See if the bridge I/O resources are implemented. */
395 moving_base = ((u32) pci_moving_config8(dev, PCI_IO_BASE)) << 8;
396 moving_base |=
397 ((u32) pci_moving_config16(dev, PCI_IO_BASE_UPPER16)) << 16;
Eric Biederman03acab62004-10-14 21:25:53 +0000398
Myles Watson29cc9ed2009-07-02 18:56:24 +0000399 moving_limit = ((u32) pci_moving_config8(dev, PCI_IO_LIMIT)) << 8;
400 moving_limit |=
401 ((u32) pci_moving_config16(dev, PCI_IO_LIMIT_UPPER16)) << 16;
Eric Biederman03acab62004-10-14 21:25:53 +0000402
403 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000404
Myles Watson29cc9ed2009-07-02 18:56:24 +0000405 /* Initialize the I/O space constraints on the current bus. */
406 pci_record_bridge_resource(dev, moving, PCI_IO_BASE, IORESOURCE_IO);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000407
Myles Watson29cc9ed2009-07-02 18:56:24 +0000408 /* See if the bridge prefmem resources are implemented. */
409 moving_base =
410 ((resource_t) pci_moving_config16(dev, PCI_PREF_MEMORY_BASE)) << 16;
411 moving_base |=
412 ((resource_t) pci_moving_config32(dev, PCI_PREF_BASE_UPPER32)) <<
413 32;
Eric Biederman03acab62004-10-14 21:25:53 +0000414
Myles Watson29cc9ed2009-07-02 18:56:24 +0000415 moving_limit =
416 ((resource_t) pci_moving_config16(dev, PCI_PREF_MEMORY_LIMIT)) <<
417 16;
418 moving_limit |=
419 ((resource_t) pci_moving_config32(dev, PCI_PREF_LIMIT_UPPER32)) <<
420 32;
Myles Watson032a9652009-05-11 22:24:53 +0000421
Eric Biederman03acab62004-10-14 21:25:53 +0000422 moving = moving_base & moving_limit;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000423 /* Initialize the prefetchable memory constraints on the current bus. */
424 pci_record_bridge_resource(dev, moving, PCI_PREF_MEMORY_BASE,
425 IORESOURCE_MEM | IORESOURCE_PREFETCH);
Myles Watson032a9652009-05-11 22:24:53 +0000426
Myles Watson29cc9ed2009-07-02 18:56:24 +0000427 /* See if the bridge mem resources are implemented. */
428 moving_base = ((u32) pci_moving_config16(dev, PCI_MEMORY_BASE)) << 16;
429 moving_limit = ((u32) pci_moving_config16(dev, PCI_MEMORY_LIMIT)) << 16;
Eric Biederman03acab62004-10-14 21:25:53 +0000430
431 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000432
Myles Watson29cc9ed2009-07-02 18:56:24 +0000433 /* Initialize the memory resources on the current bus. */
434 pci_record_bridge_resource(dev, moving, PCI_MEMORY_BASE,
435 IORESOURCE_MEM);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000436
Eric Biederman5cd81732004-03-11 15:01:31 +0000437 compact_resources(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000438}
439
Eric Biederman5899fd82003-04-24 06:25:08 +0000440void pci_dev_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000441{
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000442 pci_read_bases(dev, 6);
443 pci_get_rom_resource(dev, PCI_ROM_ADDRESS);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000444}
445
Eric Biederman5899fd82003-04-24 06:25:08 +0000446void pci_bus_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000447{
Eric Biederman8ca8d762003-04-22 19:02:15 +0000448 pci_bridge_read_bases(dev);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000449 pci_read_bases(dev, 2);
450 pci_get_rom_resource(dev, PCI_ROM_ADDRESS1);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000451}
452
Myles Watson29cc9ed2009-07-02 18:56:24 +0000453void pci_domain_read_resources(struct device *dev)
454{
455 struct resource *res;
456
457 /* Initialize the system-wide I/O space constraints. */
458 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
459 res->limit = 0xffffUL;
460 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
461 IORESOURCE_ASSIGNED;
462
463 /* Initialize the system-wide memory resources constraints. */
464 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
465 res->limit = 0xffffffffULL;
466 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
467 IORESOURCE_ASSIGNED;
468}
469
Eric Biederman8ca8d762003-04-22 19:02:15 +0000470static void pci_set_resource(struct device *dev, struct resource *resource)
471{
Eric Biederman03acab62004-10-14 21:25:53 +0000472 resource_t base, end;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000473
Myles Watson29cc9ed2009-07-02 18:56:24 +0000474 /* Make certain the resource has actually been assigned a value. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000475 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000476 printk_err("ERROR: %s %02lx %s size: 0x%010llx not assigned\n",
477 dev_path(dev), resource->index,
478 resource_type(resource), resource->size);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000479 return;
480 }
481
Myles Watsoneb81a5b2009-11-05 20:06:19 +0000482 /* If this resource is fixed don't worry about it. */
483 if (resource->flags & IORESOURCE_FIXED) {
484 return;
485 }
486
Myles Watson29cc9ed2009-07-02 18:56:24 +0000487 /* If I have already stored this resource don't worry about it. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000488 if (resource->flags & IORESOURCE_STORED) {
489 return;
490 }
491
Myles Watson29cc9ed2009-07-02 18:56:24 +0000492 /* If the resource is subtractive don't worry about it. */
Eric Biederman03acab62004-10-14 21:25:53 +0000493 if (resource->flags & IORESOURCE_SUBTRACTIVE) {
494 return;
495 }
496
Myles Watson29cc9ed2009-07-02 18:56:24 +0000497 /* Only handle PCI memory and I/O resources for now. */
498 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
Eric Biederman8ca8d762003-04-22 19:02:15 +0000499 return;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000500
Myles Watson29cc9ed2009-07-02 18:56:24 +0000501 /* Enable the resources in the command register. */
Eric Biederman03acab62004-10-14 21:25:53 +0000502 if (resource->size) {
503 if (resource->flags & IORESOURCE_MEM) {
504 dev->command |= PCI_COMMAND_MEMORY;
505 }
506 if (resource->flags & IORESOURCE_IO) {
507 dev->command |= PCI_COMMAND_IO;
508 }
509 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
510 dev->command |= PCI_COMMAND_MASTER;
511 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000512 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000513 /* Get the base address. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000514 base = resource->base;
Eric Biederman5cd81732004-03-11 15:01:31 +0000515
Myles Watson29cc9ed2009-07-02 18:56:24 +0000516 /* Get the end. */
Eric Biederman03acab62004-10-14 21:25:53 +0000517 end = resource_end(resource);
Myles Watson032a9652009-05-11 22:24:53 +0000518
Myles Watson29cc9ed2009-07-02 18:56:24 +0000519 /* Now store the resource. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000520 resource->flags |= IORESOURCE_STORED;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000521
522 /* PCI Bridges have no enable bit. They are disabled if the base of
523 * the range is greater than the limit. If the size is zero, disable
524 * by setting the base = limit and end = limit - 2^gran.
525 */
526 if (resource->size == 0 && (resource->flags & IORESOURCE_PCI_BRIDGE)) {
527 base = resource->limit;
528 end = resource->limit - (1 << resource->gran);
529 resource->base = base;
530 }
531
Eric Biederman8ca8d762003-04-22 19:02:15 +0000532 if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) {
Eric Biederman03acab62004-10-14 21:25:53 +0000533 unsigned long base_lo, base_hi;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000534 /* Some chipsets allow us to set/clear the I/O bit
535 * (e.g. VIA 82c686a). So set it to be safe.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000536 */
Eric Biederman03acab62004-10-14 21:25:53 +0000537 base_lo = base & 0xffffffff;
538 base_hi = (base >> 32) & 0xffffffff;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000539 if (resource->flags & IORESOURCE_IO) {
Eric Biederman03acab62004-10-14 21:25:53 +0000540 base_lo |= PCI_BASE_ADDRESS_SPACE_IO;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000541 }
Eric Biederman03acab62004-10-14 21:25:53 +0000542 pci_write_config32(dev, resource->index, base_lo);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000543 if (resource->flags & IORESOURCE_PCI64) {
Eric Biederman03acab62004-10-14 21:25:53 +0000544 pci_write_config32(dev, resource->index + 4, base_hi);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000545 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000546 } else if (resource->index == PCI_IO_BASE) {
547 /* Set the I/O ranges. */
548 pci_write_config8(dev, PCI_IO_BASE, base >> 8);
Eric Biederman03acab62004-10-14 21:25:53 +0000549 pci_write_config16(dev, PCI_IO_BASE_UPPER16, base >> 16);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000550 pci_write_config8(dev, PCI_IO_LIMIT, end >> 8);
Eric Biederman03acab62004-10-14 21:25:53 +0000551 pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, end >> 16);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000552 } else if (resource->index == PCI_MEMORY_BASE) {
553 /* Set the memory range. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000554 pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16);
Eric Biederman03acab62004-10-14 21:25:53 +0000555 pci_write_config16(dev, PCI_MEMORY_LIMIT, end >> 16);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000556 } else if (resource->index == PCI_PREF_MEMORY_BASE) {
557 /* Set the prefetchable memory range. */
Eric Biederman03acab62004-10-14 21:25:53 +0000558 pci_write_config16(dev, PCI_PREF_MEMORY_BASE, base >> 16);
559 pci_write_config32(dev, PCI_PREF_BASE_UPPER32, base >> 32);
560 pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, end >> 16);
561 pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, end >> 32);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000562 } else {
563 /* Don't let me think I stored the resource. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000564 resource->flags &= ~IORESOURCE_STORED;
Myles Watsonc4ddbff2009-02-09 17:52:54 +0000565 printk_err("ERROR: invalid resource->index %lx\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000566 resource->index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000567 }
Eric Biederman03acab62004-10-14 21:25:53 +0000568 report_resource_stored(dev, resource, "");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000569 return;
570}
571
Eric Biederman5899fd82003-04-24 06:25:08 +0000572void pci_dev_set_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000573{
574 struct resource *resource, *last;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000575 unsigned link;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000576 u8 line;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000577
578 last = &dev->resource[dev->resources];
Eric Biedermanb78c1972004-10-14 20:54:17 +0000579
Myles Watson29cc9ed2009-07-02 18:56:24 +0000580 for (resource = &dev->resource[0]; resource < last; resource++) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000581 pci_set_resource(dev, resource);
582 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000583 for (link = 0; link < dev->links; link++) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000584 struct bus *bus;
585 bus = &dev->link[link];
586 if (bus->children) {
587 assign_resources(bus);
588 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000589 }
590
Myles Watson29cc9ed2009-07-02 18:56:24 +0000591 /* Set a default latency timer. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000592 pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000593
Myles Watson29cc9ed2009-07-02 18:56:24 +0000594 /* Set a default secondary latency timer. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000595 if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE) {
Eric Biederman7a5416a2003-06-12 19:23:51 +0000596 pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000597 }
598
Myles Watson29cc9ed2009-07-02 18:56:24 +0000599 /* Zero the IRQ settings. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000600 line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000601 if (line) {
Eric Biederman7a5416a2003-06-12 19:23:51 +0000602 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000603 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000604 /* Set the cache line size, so far 64 bytes is good for everyone. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000605 pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000606}
607
Eric Biedermane9a271e32003-09-02 03:36:25 +0000608void pci_dev_enable_resources(struct device *dev)
609{
Eric Biedermana9e632c2004-11-18 22:38:08 +0000610 const struct pci_operations *ops;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000611 u16 command;
Eric Biederman03acab62004-10-14 21:25:53 +0000612
Myles Watson29cc9ed2009-07-02 18:56:24 +0000613 /* Set the subsystem vendor and device id for mainboard devices. */
Eric Biederman03acab62004-10-14 21:25:53 +0000614 ops = ops_pci(dev);
Eric Biedermandbec2d42004-10-21 10:44:08 +0000615 if (dev->on_mainboard && ops && ops->set_subsystem) {
Eric Biederman03acab62004-10-14 21:25:53 +0000616 printk_debug("%s subsystem <- %02x/%02x\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000617 dev_path(dev),
618 CONFIG_MAINBOARD_PCI_SUBSYSTEM_VENDOR_ID,
619 CONFIG_MAINBOARD_PCI_SUBSYSTEM_DEVICE_ID);
Myles Watson032a9652009-05-11 22:24:53 +0000620 ops->set_subsystem(dev,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000621 CONFIG_MAINBOARD_PCI_SUBSYSTEM_VENDOR_ID,
622 CONFIG_MAINBOARD_PCI_SUBSYSTEM_DEVICE_ID);
Eric Biederman03acab62004-10-14 21:25:53 +0000623 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000624 command = pci_read_config16(dev, PCI_COMMAND);
625 command |= dev->command;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000626 /* v3 has
627 * command |= (PCI_COMMAND_PARITY + PCI_COMMAND_SERR); // Error check.
628 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000629 printk_debug("%s cmd <- %02x\n", dev_path(dev), command);
630 pci_write_config16(dev, PCI_COMMAND, command);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000631}
632
633void pci_bus_enable_resources(struct device *dev)
634{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000635 u16 ctrl;
636
637 /* Enable I/O in command register if there is VGA card
638 * connected with (even it does not claim I/O resource).
639 */
Li-Ta Lo515f6c72005-01-11 22:48:54 +0000640 if (dev->link[0].bridge_ctrl & PCI_BRIDGE_CTL_VGA)
641 dev->command |= PCI_COMMAND_IO;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000642 ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
643 ctrl |= dev->link[0].bridge_ctrl;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000644 ctrl |= (PCI_BRIDGE_CTL_PARITY + PCI_BRIDGE_CTL_SERR); /* Error check. */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000645 printk_debug("%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
646 pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
647
648 pci_dev_enable_resources(dev);
Eric Biedermandbec2d42004-10-21 10:44:08 +0000649 enable_childrens_resources(dev);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000650}
651
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000652void pci_bus_reset(struct bus *bus)
653{
654 unsigned ctl;
655 ctl = pci_read_config16(bus->dev, PCI_BRIDGE_CONTROL);
656 ctl |= PCI_BRIDGE_CTL_BUS_RESET;
657 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
658 mdelay(10);
659 ctl &= ~PCI_BRIDGE_CTL_BUS_RESET;
660 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
661 delay(1);
662}
663
Myles Watson29cc9ed2009-07-02 18:56:24 +0000664void pci_dev_set_subsystem(struct device *dev, unsigned vendor, unsigned device)
Eric Biederman03acab62004-10-14 21:25:53 +0000665{
Myles Watson032a9652009-05-11 22:24:53 +0000666 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000667 ((device & 0xffff) << 16) | (vendor & 0xffff));
Eric Biederman03acab62004-10-14 21:25:53 +0000668}
669
Torsten Duwe1f2f8002008-01-06 01:10:54 +0000670/** default handler: only runs the relevant pci bios. */
Li-Ta Lo883b8792005-01-10 23:16:22 +0000671void pci_dev_init(struct device *dev)
672{
Torsten Duwe1f2f8002008-01-06 01:10:54 +0000673#if CONFIG_PCI_ROM_RUN == 1 || CONFIG_VGA_ROM_RUN == 1
Myles Watson29cc9ed2009-07-02 18:56:24 +0000674 void run_bios(struct device *dev, unsigned long addr);
Li-Ta Lo883b8792005-01-10 23:16:22 +0000675 struct rom_header *rom, *ram;
676
Myles Watson17aeeca2009-10-07 18:41:08 +0000677 if (CONFIG_PCI_ROM_RUN != 1 && /* Only execute VGA ROMs. */
678 ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA))
Roman Kononov778a42b2007-04-06 18:34:39 +0000679 return;
Myles Watson17aeeca2009-10-07 18:41:08 +0000680
681 if (CONFIG_VGA_ROM_RUN != 1 && /* Only execute non-VGA ROMs. */
682 ((dev->class >> 8) == PCI_CLASS_DISPLAY_VGA))
683 return;
Roman Kononov778a42b2007-04-06 18:34:39 +0000684
Li-Ta Lo883b8792005-01-10 23:16:22 +0000685 rom = pci_rom_probe(dev);
686 if (rom == NULL)
687 return;
Roman Kononov778a42b2007-04-06 18:34:39 +0000688
Li-Ta Lo883b8792005-01-10 23:16:22 +0000689 ram = pci_rom_load(dev, rom);
Yinghai Lu9e4faef2005-01-14 22:04:49 +0000690 if (ram == NULL)
691 return;
Li-Ta Lo883b8792005-01-10 23:16:22 +0000692
Stefan Reinauerd98cf5b2008-08-01 11:25:41 +0000693 run_bios(dev, (unsigned long)ram);
Roman Kononov778a42b2007-04-06 18:34:39 +0000694
695#if CONFIG_CONSOLE_VGA == 1
Luc Verhaegen5c5beb72009-05-29 03:04:16 +0000696 if ((dev->class>>8) == PCI_CLASS_DISPLAY_VGA)
Luc Verhaegen43bc5a9c2009-05-29 03:44:47 +0000697 vga_console_init();
Torsten Duwe1f2f8002008-01-06 01:10:54 +0000698#endif /* CONFIG_CONSOLE_VGA */
699#endif /* CONFIG_PCI_ROM_RUN || CONFIG_VGA_ROM_RUN */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000700}
Li-Ta Lo883b8792005-01-10 23:16:22 +0000701
Li-Ta Loe5266692004-03-23 21:28:05 +0000702/** Default device operation for PCI devices */
Eric Biedermana9e632c2004-11-18 22:38:08 +0000703static struct pci_operations pci_dev_ops_pci = {
Eric Biederman03acab62004-10-14 21:25:53 +0000704 .set_subsystem = pci_dev_set_subsystem,
705};
706
Eric Biederman8ca8d762003-04-22 19:02:15 +0000707struct device_operations default_pci_ops_dev = {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000708 .read_resources = pci_dev_read_resources,
709 .set_resources = pci_dev_set_resources,
Eric Biedermane9a271e32003-09-02 03:36:25 +0000710 .enable_resources = pci_dev_enable_resources,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000711 .init = pci_dev_init,
712 .scan_bus = 0,
713 .enable = 0,
714 .ops_pci = &pci_dev_ops_pci,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000715};
Li-Ta Loe5266692004-03-23 21:28:05 +0000716
717/** Default device operations for PCI bridges */
Eric Biedermana9e632c2004-11-18 22:38:08 +0000718static struct pci_operations pci_bus_ops_pci = {
Eric Biederman03acab62004-10-14 21:25:53 +0000719 .set_subsystem = 0,
720};
Li-Ta Lo883b8792005-01-10 23:16:22 +0000721
Eric Biederman8ca8d762003-04-22 19:02:15 +0000722struct device_operations default_pci_ops_bus = {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000723 .read_resources = pci_bus_read_resources,
724 .set_resources = pci_dev_set_resources,
Eric Biedermane9a271e32003-09-02 03:36:25 +0000725 .enable_resources = pci_bus_enable_resources,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000726 .init = 0,
727 .scan_bus = pci_scan_bridge,
728 .enable = 0,
729 .reset_bus = pci_bus_reset,
730 .ops_pci = &pci_bus_ops_pci,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000731};
Li-Ta Loe5266692004-03-23 21:28:05 +0000732
733/**
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000734 * @brief Detect the type of downstream bridge
735 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000736 * This function is a heuristic to detect which type of bus is downstream
737 * of a PCI-to-PCI bridge. This functions by looking for various capability
738 * blocks to figure out the type of downstream bridge. PCI-X, PCI-E, and
739 * Hypertransport all seem to have appropriate capabilities.
Myles Watson032a9652009-05-11 22:24:53 +0000740 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000741 * When only a PCI-Express capability is found the type
742 * is examined to see which type of bridge we have.
743 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000744 * @param dev Pointer to the device structure of the bridge.
745 * @return Appropriate bridge operations.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000746 */
747static struct device_operations *get_pci_bridge_ops(device_t dev)
748{
749 unsigned pos;
750
751#if CONFIG_PCIX_PLUGIN_SUPPORT == 1
752 pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
753 if (pos) {
Myles Watson0b028a42009-08-21 14:59:14 +0000754 printk_debug("%s subordinate bus PCI-X\n", dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000755 return &default_pcix_ops_bus;
756 }
757#endif
758#if CONFIG_AGP_PLUGIN_SUPPORT == 1
759 /* How do I detect an PCI to AGP bridge? */
760#endif
761#if CONFIG_HYPERTRANSPORT_PLUGIN_SUPPORT == 1
762 pos = 0;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000763 while ((pos = pci_find_next_capability(dev, PCI_CAP_ID_HT, pos))) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000764 unsigned flags;
765 flags = pci_read_config16(dev, pos + PCI_CAP_FLAGS);
766 if ((flags >> 13) == 1) {
767 /* Host or Secondary Interface */
Myles Watson0b028a42009-08-21 14:59:14 +0000768 printk_debug("%s subordinate bus Hypertransport\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000769 dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000770 return &default_ht_ops_bus;
771 }
772 }
773#endif
774#if CONFIG_PCIEXP_PLUGIN_SUPPORT == 1
775 pos = pci_find_capability(dev, PCI_CAP_ID_PCIE);
776 if (pos) {
777 unsigned flags;
778 flags = pci_read_config16(dev, pos + PCI_EXP_FLAGS);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000779 switch ((flags & PCI_EXP_FLAGS_TYPE) >> 4) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000780 case PCI_EXP_TYPE_ROOT_PORT:
781 case PCI_EXP_TYPE_UPSTREAM:
782 case PCI_EXP_TYPE_DOWNSTREAM:
Myles Watson0b028a42009-08-21 14:59:14 +0000783 printk_debug("%s subordinate bus PCI Express\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000784 dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000785 return &default_pciexp_ops_bus;
786 case PCI_EXP_TYPE_PCI_BRIDGE:
Myles Watson0b028a42009-08-21 14:59:14 +0000787 printk_debug("%s subordinate PCI\n", dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000788 return &default_pci_ops_bus;
789 default:
790 break;
791 }
792 }
793#endif
794 return &default_pci_ops_bus;
795}
796
797/**
Myles Watson29cc9ed2009-07-02 18:56:24 +0000798 * Set up PCI device operation. Check if it already has a driver. If not, use
799 * find_device_operations, or set to a default based on type.
Li-Ta Loe5266692004-03-23 21:28:05 +0000800 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000801 * @param dev Pointer to the device whose pci_ops you want to set.
Li-Ta Loe5266692004-03-23 21:28:05 +0000802 * @see pci_drivers
803 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000804static void set_pci_ops(struct device *dev)
805{
806 struct pci_driver *driver;
807 if (dev->ops) {
808 return;
809 }
Li-Ta Loe5266692004-03-23 21:28:05 +0000810
Yinghai Lu5f9624d2006-10-04 22:56:21 +0000811 /* Look through the list of setup drivers and find one for
Myles Watson29cc9ed2009-07-02 18:56:24 +0000812 * this PCI device.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000813 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000814 for (driver = &pci_drivers[0]; driver != &epci_drivers[0]; driver++) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000815 if ((driver->vendor == dev->vendor) &&
Myles Watson29cc9ed2009-07-02 18:56:24 +0000816 (driver->device == dev->device)) {
Uwe Hermann312673c2009-10-27 21:49:33 +0000817 dev->ops = (struct device_operations *)driver->ops;
Myles Watson032a9652009-05-11 22:24:53 +0000818 printk_spew("%s [%04x/%04x] %sops\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000819 dev_path(dev),
820 driver->vendor, driver->device,
821 (driver->ops->scan_bus ? "bus " : ""));
Eric Biederman5899fd82003-04-24 06:25:08 +0000822 return;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000823 }
824 }
Li-Ta Loe5266692004-03-23 21:28:05 +0000825
Eric Biederman8ca8d762003-04-22 19:02:15 +0000826 /* If I don't have a specific driver use the default operations */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000827 switch (dev->hdr_type & 0x7f) { /* header type */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000828 case PCI_HEADER_TYPE_NORMAL: /* standard header */
829 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
830 goto bad;
831 dev->ops = &default_pci_ops_dev;
832 break;
833 case PCI_HEADER_TYPE_BRIDGE:
834 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
835 goto bad;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000836 dev->ops = get_pci_bridge_ops(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000837 break;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000838#if CONFIG_CARDBUS_PLUGIN_SUPPORT == 1
839 case PCI_HEADER_TYPE_CARDBUS:
840 dev->ops = &default_cardbus_ops_bus;
841 break;
842#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +0000843 default:
Myles Watson29cc9ed2009-07-02 18:56:24 +0000844 bad:
Li-Ta Lo69c5a902004-04-29 20:08:54 +0000845 if (dev->enabled) {
Eric Biederman83b991a2003-10-11 06:20:25 +0000846 printk_err("%s [%04x/%04x/%06x] has unknown header "
Myles Watson29cc9ed2009-07-02 18:56:24 +0000847 "type %02x, ignoring.\n",
848 dev_path(dev),
849 dev->vendor, dev->device,
850 dev->class >> 8, dev->hdr_type);
Eric Biederman83b991a2003-10-11 06:20:25 +0000851 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000852 }
853 return;
854}
855
856/**
Eric Biederman03acab62004-10-14 21:25:53 +0000857 * @brief See if we have already allocated a device structure for a given devfn.
Li-Ta Loe5266692004-03-23 21:28:05 +0000858 *
859 * Given a linked list of PCI device structures and a devfn number, find the
Li-Ta Lo3a812852004-12-03 22:39:34 +0000860 * device structure correspond to the devfn, if present. This function also
861 * removes the device structure from the linked list.
Li-Ta Loe5266692004-03-23 21:28:05 +0000862 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000863 * @param list The device structure list.
864 * @param devfn A device/function number.
Li-Ta Loe5266692004-03-23 21:28:05 +0000865 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000866 * @return Pointer to the device structure found or NULL if we have not
Li-Ta Lo3a812852004-12-03 22:39:34 +0000867 * allocated a device for this devfn yet.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000868 */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000869static struct device *pci_scan_get_dev(struct device **list, unsigned int devfn)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000870{
Eric Biedermanb78c1972004-10-14 20:54:17 +0000871 struct device *dev;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000872 dev = 0;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000873 for (; *list; list = &(*list)->sibling) {
Eric Biedermanad1b35a2003-10-14 02:36:51 +0000874 if ((*list)->path.type != DEVICE_PATH_PCI) {
Li-Ta Loe5266692004-03-23 21:28:05 +0000875 printk_err("child %s not a pci device\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000876 dev_path(*list));
Eric Biedermanad1b35a2003-10-14 02:36:51 +0000877 continue;
878 }
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000879 if ((*list)->path.pci.devfn == devfn) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000880 /* Unlink from the list. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000881 dev = *list;
882 *list = (*list)->sibling;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000883 dev->sibling = NULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000884 break;
885 }
886 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000887
888 /* Just like alloc_dev() add the device to the list of devices on the
889 * bus. When the list of devices was formed we removed all of the
890 * parents children, and now we are interleaving static and dynamic
891 * devices in order on the bus.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000892 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000893 if (dev) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000894 struct device *child;
895 /* Find the last child of our parent. */
896 for (child = dev->bus->children; child && child->sibling;) {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000897 child = child->sibling;
898 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000899 /* Place the device on the list of children of its parent. */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000900 if (child) {
901 child->sibling = dev;
902 } else {
903 dev->bus->children = dev;
904 }
905 }
906
Eric Biederman8ca8d762003-04-22 19:02:15 +0000907 return dev;
908}
909
Myles Watson032a9652009-05-11 22:24:53 +0000910/**
Eric Biedermanb78c1972004-10-14 20:54:17 +0000911 * @brief Scan a PCI bus.
Li-Ta Loe5266692004-03-23 21:28:05 +0000912 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000913 * Determine the existence of a given PCI device. Allocate a new struct device
914 * if dev==NULL was passed in and the device exists in hardware.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000915 *
916 * @param bus pointer to the bus structure
917 * @param devfn to look at
918 *
919 * @return The device structure for hte device (if found)
920 * or the NULL if no device is found.
921 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000922device_t pci_probe_dev(device_t dev, struct bus * bus, unsigned devfn)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000923{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000924 u32 id, class;
925 u8 hdr_type;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000926
Myles Watson29cc9ed2009-07-02 18:56:24 +0000927 /* Detect if a device is present. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000928 if (!dev) {
929 struct device dummy;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000930 dummy.bus = bus;
931 dummy.path.type = DEVICE_PATH_PCI;
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000932 dummy.path.pci.devfn = devfn;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000933 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000934 /* Have we found something?
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000935 * Some broken boards return 0 if a slot is empty.
936 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000937 if ((id == 0xffffffff) || (id == 0x00000000) ||
938 (id == 0x0000ffff) || (id == 0xffff0000)) {
Stefan Reinauerf657d752008-09-11 06:52:22 +0000939 printk_spew("%s, bad id 0x%x\n", dev_path(&dummy), id);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000940 return NULL;
941 }
942 dev = alloc_dev(bus, &dummy.path);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000943 } else {
944 /* Enable/disable the device. Once we have found the device-
945 * specific operations this operations we will disable the
946 * device with those as well.
Myles Watson032a9652009-05-11 22:24:53 +0000947 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000948 * This is geared toward devices that have subfunctions
949 * that do not show up by default.
Myles Watson032a9652009-05-11 22:24:53 +0000950 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000951 * If a device is a stuff option on the motherboard
Myles Watson29cc9ed2009-07-02 18:56:24 +0000952 * it may be absent and enable_dev() must cope.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000953 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000954 /* Run the magic enable sequence for the device. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000955 if (dev->chip_ops && dev->chip_ops->enable_dev) {
956 dev->chip_ops->enable_dev(dev);
957 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000958 /* Now read the vendor and device ID. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000959 id = pci_read_config32(dev, PCI_VENDOR_ID);
Myles Watson032a9652009-05-11 22:24:53 +0000960
Myles Watson29cc9ed2009-07-02 18:56:24 +0000961 /* If the device does not have a PCI ID disable it. Possibly
962 * this is because we have already disabled the device. But
963 * this also handles optional devices that may not always
964 * show up.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000965 */
966 /* If the chain is fully enumerated quit */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000967 if ((id == 0xffffffff) || (id == 0x00000000) ||
968 (id == 0x0000ffff) || (id == 0xffff0000)) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000969 if (dev->enabled) {
970 printk_info("Disabling static device: %s\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000971 dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000972 dev->enabled = 0;
973 }
974 return dev;
975 }
976 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000977 /* Read the rest of the PCI configuration information. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000978 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
979 class = pci_read_config32(dev, PCI_CLASS_REVISION);
Myles Watson032a9652009-05-11 22:24:53 +0000980
Myles Watson29cc9ed2009-07-02 18:56:24 +0000981 /* Store the interesting information in the device structure. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000982 dev->vendor = id & 0xffff;
983 dev->device = (id >> 16) & 0xffff;
984 dev->hdr_type = hdr_type;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000985
986 /* Class code, the upper 3 bytes of PCI_CLASS_REVISION. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000987 dev->class = class >> 8;
Myles Watson032a9652009-05-11 22:24:53 +0000988
Myles Watson29cc9ed2009-07-02 18:56:24 +0000989 /* Architectural/System devices always need to be bus masters. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000990 if ((dev->class >> 16) == PCI_BASE_CLASS_SYSTEM) {
991 dev->command |= PCI_COMMAND_MASTER;
992 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000993 /* Look at the vendor and device ID, or at least the header type and
994 * class and figure out which set of configuration methods to use.
995 * Unless we already have some PCI ops.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000996 */
997 set_pci_ops(dev);
998
Myles Watson29cc9ed2009-07-02 18:56:24 +0000999 /* Now run the magic enable/disable sequence for the device. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001000 if (dev->ops && dev->ops->enable) {
1001 dev->ops->enable(dev);
1002 }
Myles Watson032a9652009-05-11 22:24:53 +00001003
Myles Watson29cc9ed2009-07-02 18:56:24 +00001004 /* Display the device. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001005 printk_debug("%s [%04x/%04x] %s%s\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +00001006 dev_path(dev),
1007 dev->vendor, dev->device,
1008 dev->enabled ? "enabled" : "disabled",
1009 dev->ops ? "" : " No operations");
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001010
1011 return dev;
1012}
1013
Myles Watson032a9652009-05-11 22:24:53 +00001014/**
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001015 * @brief Scan a PCI bus.
1016 *
Li-Ta Loe5266692004-03-23 21:28:05 +00001017 * Determine the existence of devices and bridges on a PCI bus. If there are
1018 * bridges on the bus, recursively scan the buses behind the bridges.
1019 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001020 * This function is the default scan_bus() method for the root device
1021 * 'dev_root'.
1022 *
Eric Biedermane9a271e32003-09-02 03:36:25 +00001023 * @param bus pointer to the bus structure
1024 * @param min_devfn minimum devfn to look at in the scan usually 0x00
1025 * @param max_devfn maximum devfn to look at in the scan usually 0xff
Eric Biederman8ca8d762003-04-22 19:02:15 +00001026 * @param max current bus number
Li-Ta Loe5266692004-03-23 21:28:05 +00001027 *
Eric Biederman8ca8d762003-04-22 19:02:15 +00001028 * @return The maximum bus number found, after scanning all subordinate busses
1029 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001030unsigned int pci_scan_bus(struct bus *bus,
Myles Watson29cc9ed2009-07-02 18:56:24 +00001031 unsigned min_devfn, unsigned max_devfn,
1032 unsigned int max)
Eric Biederman8ca8d762003-04-22 19:02:15 +00001033{
1034 unsigned int devfn;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001035 struct device *old_devices;
1036 struct device *child;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001037
Stefan Reinauer08670622009-06-30 15:17:49 +00001038#if CONFIG_PCI_BUS_SEGN_BITS
Myles Watson29cc9ed2009-07-02 18:56:24 +00001039 printk_debug("PCI: pci_scan_bus for bus %04x:%02x\n",
1040 bus->secondary >> 8, bus->secondary & 0xff);
Yinghai Lu5f9624d2006-10-04 22:56:21 +00001041#else
1042 printk_debug("PCI: pci_scan_bus for bus %02x\n", bus->secondary);
1043#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +00001044
1045 old_devices = bus->children;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001046 bus->children = NULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001047
1048 post_code(0x24);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001049 /* Probe all devices/functions on this bus with some optimization for
1050 * non-existence and single function devices.
Eric Biedermanb78c1972004-10-14 20:54:17 +00001051 */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001052 for (devfn = min_devfn; devfn <= max_devfn; devfn++) {
Myles Watson29cc9ed2009-07-02 18:56:24 +00001053 struct device *dev;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001054
Eric Biederman03acab62004-10-14 21:25:53 +00001055 /* First thing setup the device structure */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001056 dev = pci_scan_get_dev(&old_devices, devfn);
Li-Ta Lo9782f752004-05-05 21:15:42 +00001057
Myles Watson29cc9ed2009-07-02 18:56:24 +00001058 /* See if a device is present and setup the device structure. */
Myles Watson032a9652009-05-11 22:24:53 +00001059 dev = pci_probe_dev(dev, bus, devfn);
Eric Biederman03acab62004-10-14 21:25:53 +00001060
Myles Watson29cc9ed2009-07-02 18:56:24 +00001061 /* If this is not a multi function device, or the device is
1062 * not present don't waste time probing another function.
Myles Watson032a9652009-05-11 22:24:53 +00001063 * Skip to next device.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001064 */
Myles Watson032a9652009-05-11 22:24:53 +00001065 if ((PCI_FUNC(devfn) == 0x00) &&
Myles Watson29cc9ed2009-07-02 18:56:24 +00001066 (!dev
1067 || (dev->enabled && ((dev->hdr_type & 0x80) != 0x80)))) {
Eric Biederman8ca8d762003-04-22 19:02:15 +00001068 devfn += 0x07;
1069 }
1070 }
1071 post_code(0x25);
1072
Myles Watson29cc9ed2009-07-02 18:56:24 +00001073 /* Warn if any leftover static devices are are found.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001074 * There's probably a problem in the Config.lb.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001075 */
1076 if (old_devices) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001077 device_t left;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001078 printk_warning("PCI: Left over static devices:\n");
1079 for (left = old_devices; left; left = left->sibling) {
1080 printk_warning("%s\n", dev_path(left));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001081 }
Myles Watson29cc9ed2009-07-02 18:56:24 +00001082 printk_warning("PCI: Check your mainboard Config.lb.\n");
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001083 }
1084
Myles Watson29cc9ed2009-07-02 18:56:24 +00001085 /* For all children that implement scan_bus() (i.e. bridges)
Eric Biedermanb78c1972004-10-14 20:54:17 +00001086 * scan the bus behind that child.
1087 */
Myles Watson29cc9ed2009-07-02 18:56:24 +00001088 for (child = bus->children; child; child = child->sibling) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001089 max = scan_bus(child, max);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001090 }
Li-Ta Loe5266692004-03-23 21:28:05 +00001091
Myles Watson29cc9ed2009-07-02 18:56:24 +00001092 /* We've scanned the bus and so we know all about what's on the other
1093 * side of any bridges that may be on this bus plus any devices.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001094 * Return how far we've got finding sub-buses.
1095 */
Yinghai Lu5f9624d2006-10-04 22:56:21 +00001096 printk_debug("PCI: pci_scan_bus returning with max=%03x\n", max);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001097 post_code(0x55);
1098 return max;
1099}
1100
Li-Ta Loe5266692004-03-23 21:28:05 +00001101/**
1102 * @brief Scan a PCI bridge and the buses behind the bridge.
1103 *
1104 * Determine the existence of buses behind the bridge. Set up the bridge
1105 * according to the result of the scan.
1106 *
1107 * This function is the default scan_bus() method for PCI bridge devices.
1108 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001109 * @param dev Pointer to the bridge device.
1110 * @param max The highest bus number assigned up to now.
1111 * @return The maximum bus number found, after scanning all subordinate buses.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001112 */
Myles Watson032a9652009-05-11 22:24:53 +00001113unsigned int do_pci_scan_bridge(struct device *dev, unsigned int max,
Myles Watson29cc9ed2009-07-02 18:56:24 +00001114 unsigned int (*do_scan_bus) (struct bus * bus,
1115 unsigned min_devfn,
1116 unsigned max_devfn,
1117 unsigned int max))
Eric Biederman8ca8d762003-04-22 19:02:15 +00001118{
Eric Biedermane9a271e32003-09-02 03:36:25 +00001119 struct bus *bus;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001120 u32 buses;
1121 u16 cr;
Eric Biederman83b991a2003-10-11 06:20:25 +00001122
Li-Ta Lo3a812852004-12-03 22:39:34 +00001123 printk_spew("%s for %s\n", __func__, dev_path(dev));
1124
Eric Biedermane9a271e32003-09-02 03:36:25 +00001125 bus = &dev->link[0];
Eric Biedermana9e632c2004-11-18 22:38:08 +00001126 bus->dev = dev;
Eric Biedermane9a271e32003-09-02 03:36:25 +00001127 dev->links = 1;
1128
Eric Biederman8ca8d762003-04-22 19:02:15 +00001129 /* Set up the primary, secondary and subordinate bus numbers. We have
1130 * no idea how many buses are behind this bridge yet, so we set the
Myles Watson032a9652009-05-11 22:24:53 +00001131 * subordinate bus number to 0xff for the moment.
Eric Biedermanb78c1972004-10-14 20:54:17 +00001132 */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001133 bus->secondary = ++max;
1134 bus->subordinate = 0xff;
Li-Ta Loe5266692004-03-23 21:28:05 +00001135
Eric Biederman8ca8d762003-04-22 19:02:15 +00001136 /* Clear all status bits and turn off memory, I/O and master enables. */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001137 cr = pci_read_config16(dev, PCI_COMMAND);
1138 pci_write_config16(dev, PCI_COMMAND, 0x0000);
1139 pci_write_config16(dev, PCI_STATUS, 0xffff);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001140
Myles Watson29cc9ed2009-07-02 18:56:24 +00001141 /* Read the existing primary/secondary/subordinate bus
Eric Biedermanb78c1972004-10-14 20:54:17 +00001142 * number configuration.
1143 */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001144 buses = pci_read_config32(dev, PCI_PRIMARY_BUS);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001145
1146 /* Configure the bus numbers for this bridge: the configuration
1147 * transactions will not be propagated by the bridge if it is not
Eric Biedermanb78c1972004-10-14 20:54:17 +00001148 * correctly configured.
1149 */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001150 buses &= 0xff000000;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001151 buses |= (((unsigned int)(dev->bus->secondary) << 0) |
1152 ((unsigned int)(bus->secondary) << 8) |
1153 ((unsigned int)(bus->subordinate) << 16));
Eric Biedermane9a271e32003-09-02 03:36:25 +00001154 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
Li-Ta Lo3a812852004-12-03 22:39:34 +00001155
Myles Watson032a9652009-05-11 22:24:53 +00001156 /* Now we can scan all subordinate buses
Eric Biedermanb78c1972004-10-14 20:54:17 +00001157 * i.e. the bus behind the bridge.
1158 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001159 max = do_scan_bus(bus, 0x00, 0xff, max);
Li-Ta Lo3a812852004-12-03 22:39:34 +00001160
Eric Biederman8ca8d762003-04-22 19:02:15 +00001161 /* We know the number of buses behind this bridge. Set the subordinate
Eric Biedermanb78c1972004-10-14 20:54:17 +00001162 * bus number to its real value.
1163 */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001164 bus->subordinate = max;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001165 buses = (buses & 0xff00ffff) | ((unsigned int)(bus->subordinate) << 16);
Eric Biedermane9a271e32003-09-02 03:36:25 +00001166 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
1167 pci_write_config16(dev, PCI_COMMAND, cr);
Myles Watson032a9652009-05-11 22:24:53 +00001168
Eric Biedermanb78c1972004-10-14 20:54:17 +00001169 printk_spew("%s returns max %d\n", __func__, max);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001170 return max;
1171}
Li-Ta Loe5266692004-03-23 21:28:05 +00001172
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001173/**
1174 * @brief Scan a PCI bridge and the buses behind the bridge.
1175 *
1176 * Determine the existence of buses behind the bridge. Set up the bridge
1177 * according to the result of the scan.
1178 *
1179 * This function is the default scan_bus() method for PCI bridge devices.
1180 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001181 * @param dev Pointer to the bridge device.
1182 * @param max The highest bus number assigned up to now.
1183 * @return The maximum bus number found, after scanning all subordinate buses.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001184 */
1185unsigned int pci_scan_bridge(struct device *dev, unsigned int max)
1186{
1187 return do_pci_scan_bridge(dev, max, pci_scan_bus);
1188}
1189
Myles Watson29cc9ed2009-07-02 18:56:24 +00001190/**
1191 * @brief Scan a PCI domain.
1192 *
1193 * This function is the default scan_bus() method for PCI domains.
1194 *
1195 * @param dev pointer to the domain
1196 * @param max the highest bus number assgined up to now
1197 *
1198 * @return The maximum bus number found, after scanning all subordinate busses
1199 */
1200unsigned int pci_domain_scan_bus(device_t dev, unsigned int max)
1201{
1202 max = pci_scan_bus(&dev->link[0], PCI_DEVFN(0, 0), 0xff, max);
1203 return max;
1204}
1205
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001206#if CONFIG_PC80_SYSTEM == 1
Myles Watson29cc9ed2009-07-02 18:56:24 +00001207/**
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001208 *
1209 * @brief Assign IRQ numbers
Myles Watson29cc9ed2009-07-02 18:56:24 +00001210 *
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001211 * This function assigns IRQs for all functions contained within the indicated
1212 * device address. If the device does not exist or does not require interrupts
1213 * then this function has no effect.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001214 *
1215 * This function should be called for each PCI slot in your system.
1216 *
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001217 * @param bus
1218 * @param slot
1219 * @param pIntAtoD is an array of IRQ #s that are assigned to PINTA through
1220 * PINTD of this slot. The particular irq #s that are passed in
1221 * depend on the routing inside your southbridge and on your
1222 * motherboard.
1223 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001224void pci_assign_irqs(unsigned bus, unsigned slot,
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001225 const unsigned char pIntAtoD[4])
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001226{
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001227 unsigned int funct;
1228 device_t pdev;
1229 u8 line;
1230 u8 irq;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001231
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001232 /* Each slot may contain up to eight functions */
1233 for (funct = 0; funct < 8; funct++) {
1234 pdev = dev_find_slot(bus, (slot << 3) + funct);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001235
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001236 if (!pdev)
1237 continue;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001238
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001239 line = pci_read_config8(pdev, PCI_INTERRUPT_PIN);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001240
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001241 // PCI spec says all values except 1..4 are reserved.
1242 if ((line < 1) || (line > 4))
1243 continue;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001244
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001245 irq = pIntAtoD[line - 1];
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001246
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001247 printk_debug("Assigning IRQ %d to %d:%x.%d\n",
1248 irq, bus, slot, funct);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001249
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001250 pci_write_config8(pdev, PCI_INTERRUPT_LINE,
1251 pIntAtoD[line - 1]);
1252
1253#ifdef PARANOID_IRQ_ASSIGNMENTS
Myles Watson17aeeca2009-10-07 18:41:08 +00001254 irq = pci_read_config8(pdev, PCI_INTERRUPT_LINE);
1255 printk_debug(" Readback = %d\n", irq);
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001256#endif
1257
1258 // Change to level triggered
1259 i8259_configure_irq_trigger(pIntAtoD[line - 1], IRQ_LEVEL_TRIGGERED);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001260 }
1261}
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001262#endif
1263