blob: 3a54c2d5c839640e5b89b082927d10019f995efa [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)
Mike Loptien0f5cf5e2014-05-12 21:46:31 -060015 * Copyright (C) 2014 Sage Electronic Engineering, LLC.
Uwe Hermannb80dbf02007-04-22 19:08:13 +000016 */
17
18/*
Myles Watson29cc9ed2009-07-02 18:56:24 +000019 * PCI Bus Services, see include/linux/pci.h for further explanation.
Eric Biederman8ca8d762003-04-22 19:02:15 +000020 *
Myles Watson29cc9ed2009-07-02 18:56:24 +000021 * Copyright 1993 -- 1997 Drew Eckhardt, Frederic Potter,
22 * David Mosberger-Tang
Eric Biederman8ca8d762003-04-22 19:02:15 +000023 *
Myles Watson29cc9ed2009-07-02 18:56:24 +000024 * Copyright 1997 -- 1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
Eric Biederman8ca8d762003-04-22 19:02:15 +000025 */
26
Kyösti Mälkki580e5642014-05-01 16:31:34 +030027#include <kconfig.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000028#include <console/console.h>
29#include <stdlib.h>
30#include <stdint.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000031#include <string.h>
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +000032#include <arch/io.h>
Eric Biederman5899fd82003-04-24 06:25:08 +000033#include <device/device.h>
34#include <device/pci.h>
35#include <device/pci_ids.h>
Kyösti Mälkkiab56b3b2013-11-28 16:44:51 +020036#include <bootmode.h>
Eric Biederman03acab62004-10-14 21:25:53 +000037#include <delay.h>
Patrick Georgie1667822012-05-05 15:29:32 +020038#if CONFIG_HYPERTRANSPORT_PLUGIN_SUPPORT
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000039#include <device/hypertransport.h>
40#endif
Patrick Georgie1667822012-05-05 15:29:32 +020041#if CONFIG_PCIX_PLUGIN_SUPPORT
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000042#include <device/pcix.h>
43#endif
Patrick Georgie1667822012-05-05 15:29:32 +020044#if CONFIG_PCIEXP_PLUGIN_SUPPORT
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000045#include <device/pciexp.h>
46#endif
Patrick Georgie1667822012-05-05 15:29:32 +020047#if CONFIG_AGP_PLUGIN_SUPPORT
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000048#include <device/agp.h>
49#endif
Patrick Georgie1667822012-05-05 15:29:32 +020050#if CONFIG_CARDBUS_PLUGIN_SUPPORT
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000051#include <device/cardbus.h>
52#endif
Patrick Georgie1667822012-05-05 15:29:32 +020053#if CONFIG_PC80_SYSTEM
Stefan Reinauer4d933dd2009-07-21 21:36:41 +000054#include <pc80/i8259.h>
55#endif
Stefan Reinauer0a500842011-09-23 10:33:58 -070056#if CONFIG_HAVE_ACPI_RESUME && !CONFIG_S3_VGA_ROM_RUN
57#include <arch/acpi.h>
58#endif
Stefan Reinauer74a0efe2012-03-30 17:10:49 -070059#if CONFIG_CHROMEOS
60#include <vendorcode/google/chromeos/chromeos.h>
61#endif
Eric Biederman03acab62004-10-14 21:25:53 +000062
Myles Watson29cc9ed2009-07-02 18:56:24 +000063u8 pci_moving_config8(struct device *dev, unsigned int reg)
Eric Biederman03acab62004-10-14 21:25:53 +000064{
Myles Watson29cc9ed2009-07-02 18:56:24 +000065 u8 value, ones, zeroes;
Uwe Hermanne4870472010-11-04 23:23:47 +000066
Eric Biederman03acab62004-10-14 21:25:53 +000067 value = pci_read_config8(dev, reg);
Myles Watson032a9652009-05-11 22:24:53 +000068
Eric Biederman03acab62004-10-14 21:25:53 +000069 pci_write_config8(dev, reg, 0xff);
70 ones = pci_read_config8(dev, reg);
71
72 pci_write_config8(dev, reg, 0x00);
73 zeroes = pci_read_config8(dev, reg);
74
75 pci_write_config8(dev, reg, value);
76
77 return ones ^ zeroes;
78}
Li-Ta Lo9a5b4962004-12-23 21:48:01 +000079
Uwe Hermanne4870472010-11-04 23:23:47 +000080u16 pci_moving_config16(struct device *dev, unsigned int reg)
Eric Biederman03acab62004-10-14 21:25:53 +000081{
Myles Watson29cc9ed2009-07-02 18:56:24 +000082 u16 value, ones, zeroes;
Uwe Hermanne4870472010-11-04 23:23:47 +000083
Eric Biederman03acab62004-10-14 21:25:53 +000084 value = pci_read_config16(dev, reg);
Myles Watson032a9652009-05-11 22:24:53 +000085
Eric Biederman03acab62004-10-14 21:25:53 +000086 pci_write_config16(dev, reg, 0xffff);
87 ones = pci_read_config16(dev, reg);
88
89 pci_write_config16(dev, reg, 0x0000);
90 zeroes = pci_read_config16(dev, reg);
91
92 pci_write_config16(dev, reg, value);
93
94 return ones ^ zeroes;
95}
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +000096
Uwe Hermanne4870472010-11-04 23:23:47 +000097u32 pci_moving_config32(struct device *dev, unsigned int reg)
Eric Biederman03acab62004-10-14 21:25:53 +000098{
Myles Watson29cc9ed2009-07-02 18:56:24 +000099 u32 value, ones, zeroes;
Uwe Hermanne4870472010-11-04 23:23:47 +0000100
Eric Biederman03acab62004-10-14 21:25:53 +0000101 value = pci_read_config32(dev, reg);
Myles Watson032a9652009-05-11 22:24:53 +0000102
Eric Biederman03acab62004-10-14 21:25:53 +0000103 pci_write_config32(dev, reg, 0xffffffff);
104 ones = pci_read_config32(dev, reg);
105
106 pci_write_config32(dev, reg, 0x00000000);
107 zeroes = pci_read_config32(dev, reg);
108
109 pci_write_config32(dev, reg, value);
110
111 return ones ^ zeroes;
112}
113
Myles Watson29cc9ed2009-07-02 18:56:24 +0000114/**
115 * Given a device, a capability type, and a last position, return the next
116 * matching capability. Always start at the head of the list.
117 *
118 * @param dev Pointer to the device structure.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000119 * @param cap PCI_CAP_LIST_ID of the PCI capability we're looking for.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000120 * @param last Location of the PCI capability register to start from.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000121 * @return The next matching capability.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000122 */
123unsigned pci_find_next_capability(struct device *dev, unsigned cap,
124 unsigned last)
Eric Biederman03acab62004-10-14 21:25:53 +0000125{
Stefan Reinauer4d933dd2009-07-21 21:36:41 +0000126 unsigned pos = 0;
Uwe Hermanne4870472010-11-04 23:23:47 +0000127 u16 status;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000128 unsigned reps = 48;
Stefan Reinauer4d933dd2009-07-21 21:36:41 +0000129
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000130 status = pci_read_config16(dev, PCI_STATUS);
Uwe Hermanne4870472010-11-04 23:23:47 +0000131 if (!(status & PCI_STATUS_CAP_LIST))
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000132 return 0;
Uwe Hermanne4870472010-11-04 23:23:47 +0000133
Myles Watson29cc9ed2009-07-02 18:56:24 +0000134 switch (dev->hdr_type & 0x7f) {
Eric Biederman03acab62004-10-14 21:25:53 +0000135 case PCI_HEADER_TYPE_NORMAL:
136 case PCI_HEADER_TYPE_BRIDGE:
137 pos = PCI_CAPABILITY_LIST;
138 break;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000139 case PCI_HEADER_TYPE_CARDBUS:
140 pos = PCI_CB_CAPABILITY_LIST;
141 break;
142 default:
143 return 0;
Eric Biederman03acab62004-10-14 21:25:53 +0000144 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000145
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000146 pos = pci_read_config8(dev, pos);
Uwe Hermanne4870472010-11-04 23:23:47 +0000147 while (reps-- && (pos >= 0x40)) { /* Loop through the linked list. */
Eric Biederman03acab62004-10-14 21:25:53 +0000148 int this_cap;
Uwe Hermanne4870472010-11-04 23:23:47 +0000149
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000150 pos &= ~3;
Eric Biederman03acab62004-10-14 21:25:53 +0000151 this_cap = pci_read_config8(dev, pos + PCI_CAP_LIST_ID);
Uwe Hermanne4870472010-11-04 23:23:47 +0000152 printk(BIOS_SPEW, "Capability: type 0x%02x @ 0x%02x\n",
153 this_cap, pos);
154 if (this_cap == 0xff)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000155 break;
Uwe Hermanne4870472010-11-04 23:23:47 +0000156
157 if (!last && (this_cap == cap))
Eric Biederman03acab62004-10-14 21:25:53 +0000158 return pos;
Uwe Hermanne4870472010-11-04 23:23:47 +0000159
160 if (last == pos)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000161 last = 0;
Uwe Hermanne4870472010-11-04 23:23:47 +0000162
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000163 pos = pci_read_config8(dev, pos + PCI_CAP_LIST_NEXT);
Eric Biederman03acab62004-10-14 21:25:53 +0000164 }
165 return 0;
166}
167
Myles Watson29cc9ed2009-07-02 18:56:24 +0000168/**
169 * Given a device, and a capability type, return the next matching
170 * capability. Always start at the head of the list.
171 *
172 * @param dev Pointer to the device structure.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000173 * @param cap PCI_CAP_LIST_ID of the PCI capability we're looking for.
174 * @return The next matching capability.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000175 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000176unsigned pci_find_capability(device_t dev, unsigned cap)
177{
178 return pci_find_next_capability(dev, cap, 0);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000179}
180
Myles Watson29cc9ed2009-07-02 18:56:24 +0000181/**
182 * Given a device and register, read the size of the BAR for that register.
183 *
184 * @param dev Pointer to the device structure.
185 * @param index Address of the PCI configuration register.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000186 * @return TODO
Eric Biederman8ca8d762003-04-22 19:02:15 +0000187 */
Eric Biederman03acab62004-10-14 21:25:53 +0000188struct resource *pci_get_resource(struct device *dev, unsigned long index)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000189{
Eric Biederman5cd81732004-03-11 15:01:31 +0000190 struct resource *resource;
Eric Biederman03acab62004-10-14 21:25:53 +0000191 unsigned long value, attr;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000192 resource_t moving, limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000193
Myles Watson29cc9ed2009-07-02 18:56:24 +0000194 /* Initialize the resources to nothing. */
Eric Biederman03acab62004-10-14 21:25:53 +0000195 resource = new_resource(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000196
Myles Watson29cc9ed2009-07-02 18:56:24 +0000197 /* Get the initial value. */
Eric Biederman03acab62004-10-14 21:25:53 +0000198 value = pci_read_config32(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000199
Myles Watson29cc9ed2009-07-02 18:56:24 +0000200 /* See which bits move. */
Eric Biederman03acab62004-10-14 21:25:53 +0000201 moving = pci_moving_config32(dev, index);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000202
Myles Watson29cc9ed2009-07-02 18:56:24 +0000203 /* Initialize attr to the bits that do not move. */
Eric Biederman03acab62004-10-14 21:25:53 +0000204 attr = value & ~moving;
205
Myles Watson29cc9ed2009-07-02 18:56:24 +0000206 /* If it is a 64bit resource look at the high half as well. */
Eric Biederman03acab62004-10-14 21:25:53 +0000207 if (((attr & PCI_BASE_ADDRESS_SPACE_IO) == 0) &&
Myles Watson29cc9ed2009-07-02 18:56:24 +0000208 ((attr & PCI_BASE_ADDRESS_MEM_LIMIT_MASK) ==
209 PCI_BASE_ADDRESS_MEM_LIMIT_64)) {
210 /* Find the high bits that move. */
211 moving |=
212 ((resource_t) pci_moving_config32(dev, index + 4)) << 32;
Eric Biederman03acab62004-10-14 21:25:53 +0000213 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000214
Myles Watson032a9652009-05-11 22:24:53 +0000215 /* Find the resource constraints.
Eric Biederman03acab62004-10-14 21:25:53 +0000216 * Start by finding the bits that move. From there:
217 * - Size is the least significant bit of the bits that move.
218 * - Limit is all of the bits that move plus all of the lower bits.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000219 * See PCI Spec 6.2.5.1.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000220 */
Eric Biederman03acab62004-10-14 21:25:53 +0000221 limit = 0;
222 if (moving) {
223 resource->size = 1;
224 resource->align = resource->gran = 0;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000225 while (!(moving & resource->size)) {
Eric Biederman03acab62004-10-14 21:25:53 +0000226 resource->size <<= 1;
227 resource->align += 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000228 resource->gran += 1;
Eric Biederman03acab62004-10-14 21:25:53 +0000229 }
230 resource->limit = limit = moving | (resource->size - 1);
231 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000232
Uwe Hermanne4870472010-11-04 23:23:47 +0000233 /*
234 * Some broken hardware has read-only registers that do not
Eric Biederman03acab62004-10-14 21:25:53 +0000235 * really size correctly.
Uwe Hermanne4870472010-11-04 23:23:47 +0000236 *
237 * Example: the Acer M7229 has BARs 1-4 normally read-only,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000238 * so BAR1 at offset 0x10 reads 0x1f1. If you size that register
Uwe Hermanne4870472010-11-04 23:23:47 +0000239 * by writing 0xffffffff to it, it will read back as 0x1f1 -- which
240 * is a violation of the spec.
241 *
242 * We catch this case and ignore it by observing which bits move.
243 *
244 * This also catches the common case of unimplemented registers
Eric Biederman03acab62004-10-14 21:25:53 +0000245 * that always read back as 0.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000246 */
Eric Biederman03acab62004-10-14 21:25:53 +0000247 if (moving == 0) {
248 if (value != 0) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000249 printk(BIOS_DEBUG, "%s register %02lx(%08lx), "
250 "read-only ignoring it\n",
251 dev_path(dev), index, value);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000252 }
253 resource->flags = 0;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000254 } else if (attr & PCI_BASE_ADDRESS_SPACE_IO) {
255 /* An I/O mapped base address. */
Eric Biederman03acab62004-10-14 21:25:53 +0000256 attr &= PCI_BASE_ADDRESS_IO_ATTR_MASK;
Eric Biederman5cd81732004-03-11 15:01:31 +0000257 resource->flags |= IORESOURCE_IO;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000258 /* I don't want to deal with 32bit I/O resources. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000259 resource->limit = 0xffff;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000260 } else {
261 /* A Memory mapped base address. */
Eric Biederman03acab62004-10-14 21:25:53 +0000262 attr &= PCI_BASE_ADDRESS_MEM_ATTR_MASK;
Eric Biederman5cd81732004-03-11 15:01:31 +0000263 resource->flags |= IORESOURCE_MEM;
Uwe Hermanne4870472010-11-04 23:23:47 +0000264 if (attr & PCI_BASE_ADDRESS_MEM_PREFETCH)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000265 resource->flags |= IORESOURCE_PREFETCH;
Eric Biederman03acab62004-10-14 21:25:53 +0000266 attr &= PCI_BASE_ADDRESS_MEM_LIMIT_MASK;
267 if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_32) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000268 /* 32bit limit. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000269 resource->limit = 0xffffffffUL;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000270 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_1M) {
271 /* 1MB limit. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000272 resource->limit = 0x000fffffUL;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000273 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_64) {
274 /* 64bit limit. */
Eric Biederman03acab62004-10-14 21:25:53 +0000275 resource->limit = 0xffffffffffffffffULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000276 resource->flags |= IORESOURCE_PCI64;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000277 } else {
278 /* Invalid value. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000279 printk(BIOS_ERR, "Broken BAR with value %lx\n", attr);
280 printk(BIOS_ERR, " on dev %s at index %02lx\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000281 dev_path(dev), index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000282 resource->flags = 0;
283 }
284 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000285
Myles Watson29cc9ed2009-07-02 18:56:24 +0000286 /* Don't let the limit exceed which bits can move. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000287 if (resource->limit > limit)
Eric Biederman03acab62004-10-14 21:25:53 +0000288 resource->limit = limit;
Eric Biederman03acab62004-10-14 21:25:53 +0000289
Eric Biederman5cd81732004-03-11 15:01:31 +0000290 return resource;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000291}
292
Myles Watson29cc9ed2009-07-02 18:56:24 +0000293/**
294 * Given a device and an index, read the size of the BAR for that register.
295 *
296 * @param dev Pointer to the device structure.
297 * @param index Address of the PCI configuration register.
298 */
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000299static void pci_get_rom_resource(struct device *dev, unsigned long index)
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000300{
301 struct resource *resource;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000302 unsigned long value;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000303 resource_t moving;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000304
Myles Watson29cc9ed2009-07-02 18:56:24 +0000305 /* Initialize the resources to nothing. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000306 resource = new_resource(dev, index);
307
Myles Watson29cc9ed2009-07-02 18:56:24 +0000308 /* Get the initial value. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000309 value = pci_read_config32(dev, index);
310
Myles Watson29cc9ed2009-07-02 18:56:24 +0000311 /* See which bits move. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000312 moving = pci_moving_config32(dev, index);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000313
314 /* Clear the Enable bit. */
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000315 moving = moving & ~PCI_ROM_ADDRESS_ENABLE;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000316
Myles Watson032a9652009-05-11 22:24:53 +0000317 /* Find the resource constraints.
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000318 * Start by finding the bits that move. From there:
319 * - Size is the least significant bit of the bits that move.
320 * - Limit is all of the bits that move plus all of the lower bits.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000321 * See PCI Spec 6.2.5.1.
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000322 */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000323 if (moving) {
324 resource->size = 1;
325 resource->align = resource->gran = 0;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000326 while (!(moving & resource->size)) {
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000327 resource->size <<= 1;
328 resource->align += 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000329 resource->gran += 1;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000330 }
Patrick Georgi16cdbb22009-04-21 20:14:31 +0000331 resource->limit = moving | (resource->size - 1);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000332 resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY;
333 } else {
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000334 if (value != 0) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000335 printk(BIOS_DEBUG, "%s register %02lx(%08lx), "
336 "read-only ignoring it\n",
337 dev_path(dev), index, value);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000338 }
339 resource->flags = 0;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000340 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000341 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{
Eric Biederman03acab62004-10-14 21:25:53 +0000367 struct resource *resource;
Uwe Hermanne4870472010-11-04 23:23:47 +0000368 unsigned long gran;
369 resource_t step;
370
Myles Watson29cc9ed2009-07-02 18:56:24 +0000371 resource = NULL;
Uwe Hermanne4870472010-11-04 23:23:47 +0000372
373 if (!moving)
374 return;
375
376 /* Initialize the constraints on the current bus. */
377 resource = new_resource(dev, index);
378 resource->size = 0;
379 gran = 0;
380 step = 1;
381 while ((moving & step) == 0) {
382 gran += 1;
383 step <<= 1;
Eric Biederman03acab62004-10-14 21:25:53 +0000384 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000385 resource->gran = gran;
386 resource->align = gran;
387 resource->limit = moving | (step - 1);
388 resource->flags = type | IORESOURCE_PCI_BRIDGE |
389 IORESOURCE_BRIDGE;
Eric Biederman03acab62004-10-14 21:25:53 +0000390}
391
Eric Biederman8ca8d762003-04-22 19:02:15 +0000392static void pci_bridge_read_bases(struct device *dev)
393{
Eric Biederman03acab62004-10-14 21:25:53 +0000394 resource_t moving_base, moving_limit, moving;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000395
Myles Watson29cc9ed2009-07-02 18:56:24 +0000396 /* See if the bridge I/O resources are implemented. */
397 moving_base = ((u32) pci_moving_config8(dev, PCI_IO_BASE)) << 8;
398 moving_base |=
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000399 ((u32) pci_moving_config16(dev, PCI_IO_BASE_UPPER16)) << 16;
Eric Biederman03acab62004-10-14 21:25:53 +0000400
Myles Watson29cc9ed2009-07-02 18:56:24 +0000401 moving_limit = ((u32) pci_moving_config8(dev, PCI_IO_LIMIT)) << 8;
402 moving_limit |=
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000403 ((u32) pci_moving_config16(dev, PCI_IO_LIMIT_UPPER16)) << 16;
Eric Biederman03acab62004-10-14 21:25:53 +0000404
405 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000406
Myles Watson29cc9ed2009-07-02 18:56:24 +0000407 /* Initialize the I/O space constraints on the current bus. */
408 pci_record_bridge_resource(dev, moving, PCI_IO_BASE, IORESOURCE_IO);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000409
Myles Watson29cc9ed2009-07-02 18:56:24 +0000410 /* See if the bridge prefmem resources are implemented. */
411 moving_base =
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000412 ((resource_t) pci_moving_config16(dev, PCI_PREF_MEMORY_BASE)) << 16;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000413 moving_base |=
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000414 ((resource_t) pci_moving_config32(dev, PCI_PREF_BASE_UPPER32)) << 32;
Eric Biederman03acab62004-10-14 21:25:53 +0000415
Myles Watson29cc9ed2009-07-02 18:56:24 +0000416 moving_limit =
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000417 ((resource_t) pci_moving_config16(dev, PCI_PREF_MEMORY_LIMIT)) << 16;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000418 moving_limit |=
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000419 ((resource_t) pci_moving_config32(dev, PCI_PREF_LIMIT_UPPER32)) << 32;
Myles Watson032a9652009-05-11 22:24:53 +0000420
Eric Biederman03acab62004-10-14 21:25:53 +0000421 moving = moving_base & moving_limit;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000422 /* Initialize the prefetchable memory constraints on the current bus. */
423 pci_record_bridge_resource(dev, moving, PCI_PREF_MEMORY_BASE,
424 IORESOURCE_MEM | IORESOURCE_PREFETCH);
Myles Watson032a9652009-05-11 22:24:53 +0000425
Myles Watson29cc9ed2009-07-02 18:56:24 +0000426 /* See if the bridge mem resources are implemented. */
427 moving_base = ((u32) pci_moving_config16(dev, PCI_MEMORY_BASE)) << 16;
428 moving_limit = ((u32) pci_moving_config16(dev, PCI_MEMORY_LIMIT)) << 16;
Eric Biederman03acab62004-10-14 21:25:53 +0000429
430 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000431
Myles Watson29cc9ed2009-07-02 18:56:24 +0000432 /* Initialize the memory resources on the current bus. */
433 pci_record_bridge_resource(dev, moving, PCI_MEMORY_BASE,
434 IORESOURCE_MEM);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000435
Eric Biederman5cd81732004-03-11 15:01:31 +0000436 compact_resources(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000437}
438
Eric Biederman5899fd82003-04-24 06:25:08 +0000439void pci_dev_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000440{
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000441 pci_read_bases(dev, 6);
442 pci_get_rom_resource(dev, PCI_ROM_ADDRESS);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000443}
444
Eric Biederman5899fd82003-04-24 06:25:08 +0000445void pci_bus_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000446{
Eric Biederman8ca8d762003-04-22 19:02:15 +0000447 pci_bridge_read_bases(dev);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000448 pci_read_bases(dev, 2);
449 pci_get_rom_resource(dev, PCI_ROM_ADDRESS1);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000450}
451
Myles Watson29cc9ed2009-07-02 18:56:24 +0000452void pci_domain_read_resources(struct device *dev)
453{
454 struct resource *res;
455
456 /* Initialize the system-wide I/O space constraints. */
457 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
458 res->limit = 0xffffUL;
459 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
460 IORESOURCE_ASSIGNED;
461
462 /* Initialize the system-wide memory resources constraints. */
463 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
464 res->limit = 0xffffffffULL;
465 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
466 IORESOURCE_ASSIGNED;
467}
468
Eric Biederman8ca8d762003-04-22 19:02:15 +0000469static void pci_set_resource(struct device *dev, struct resource *resource)
470{
Eric Biederman03acab62004-10-14 21:25:53 +0000471 resource_t base, end;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000472
Myles Watson29cc9ed2009-07-02 18:56:24 +0000473 /* Make certain the resource has actually been assigned a value. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000474 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000475 printk(BIOS_ERR, "ERROR: %s %02lx %s size: 0x%010llx not "
476 "assigned\n", dev_path(dev), resource->index,
477 resource_type(resource), resource->size);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000478 return;
479 }
480
Myles Watsoneb81a5b2009-11-05 20:06:19 +0000481 /* If this resource is fixed don't worry about it. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000482 if (resource->flags & IORESOURCE_FIXED)
Myles Watsoneb81a5b2009-11-05 20:06:19 +0000483 return;
Myles Watsoneb81a5b2009-11-05 20:06:19 +0000484
Myles Watson29cc9ed2009-07-02 18:56:24 +0000485 /* If I have already stored this resource don't worry about it. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000486 if (resource->flags & IORESOURCE_STORED)
Eric Biederman5cd81732004-03-11 15:01:31 +0000487 return;
Eric Biederman5cd81732004-03-11 15:01:31 +0000488
Myles Watson29cc9ed2009-07-02 18:56:24 +0000489 /* If the resource is subtractive don't worry about it. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000490 if (resource->flags & IORESOURCE_SUBTRACTIVE)
Eric Biederman03acab62004-10-14 21:25:53 +0000491 return;
Eric Biederman03acab62004-10-14 21:25:53 +0000492
Myles Watson29cc9ed2009-07-02 18:56:24 +0000493 /* Only handle PCI memory and I/O resources for now. */
494 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
Eric Biederman8ca8d762003-04-22 19:02:15 +0000495 return;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000496
Myles Watson29cc9ed2009-07-02 18:56:24 +0000497 /* Enable the resources in the command register. */
Eric Biederman03acab62004-10-14 21:25:53 +0000498 if (resource->size) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000499 if (resource->flags & IORESOURCE_MEM)
Eric Biederman03acab62004-10-14 21:25:53 +0000500 dev->command |= PCI_COMMAND_MEMORY;
Uwe Hermanne4870472010-11-04 23:23:47 +0000501 if (resource->flags & IORESOURCE_IO)
Eric Biederman03acab62004-10-14 21:25:53 +0000502 dev->command |= PCI_COMMAND_IO;
Uwe Hermanne4870472010-11-04 23:23:47 +0000503 if (resource->flags & IORESOURCE_PCI_BRIDGE)
Eric Biederman03acab62004-10-14 21:25:53 +0000504 dev->command |= PCI_COMMAND_MASTER;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000505 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000506
Myles Watson29cc9ed2009-07-02 18:56:24 +0000507 /* Get the base address. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000508 base = resource->base;
Eric Biederman5cd81732004-03-11 15:01:31 +0000509
Myles Watson29cc9ed2009-07-02 18:56:24 +0000510 /* Get the end. */
Eric Biederman03acab62004-10-14 21:25:53 +0000511 end = resource_end(resource);
Myles Watson032a9652009-05-11 22:24:53 +0000512
Myles Watson29cc9ed2009-07-02 18:56:24 +0000513 /* Now store the resource. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000514 resource->flags |= IORESOURCE_STORED;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000515
Uwe Hermanne4870472010-11-04 23:23:47 +0000516 /*
517 * PCI bridges have no enable bit. They are disabled if the base of
518 * the range is greater than the limit. If the size is zero, disable
Myles Watson29cc9ed2009-07-02 18:56:24 +0000519 * by setting the base = limit and end = limit - 2^gran.
520 */
521 if (resource->size == 0 && (resource->flags & IORESOURCE_PCI_BRIDGE)) {
522 base = resource->limit;
523 end = resource->limit - (1 << resource->gran);
524 resource->base = base;
525 }
526
Eric Biederman8ca8d762003-04-22 19:02:15 +0000527 if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) {
Eric Biederman03acab62004-10-14 21:25:53 +0000528 unsigned long base_lo, base_hi;
Uwe Hermanne4870472010-11-04 23:23:47 +0000529
530 /*
531 * Some chipsets allow us to set/clear the I/O bit
532 * (e.g. VIA 82C686A). So set it to be safe.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000533 */
Eric Biederman03acab62004-10-14 21:25:53 +0000534 base_lo = base & 0xffffffff;
535 base_hi = (base >> 32) & 0xffffffff;
Uwe Hermanne4870472010-11-04 23:23:47 +0000536 if (resource->flags & IORESOURCE_IO)
Eric Biederman03acab62004-10-14 21:25:53 +0000537 base_lo |= PCI_BASE_ADDRESS_SPACE_IO;
Eric Biederman03acab62004-10-14 21:25:53 +0000538 pci_write_config32(dev, resource->index, base_lo);
Uwe Hermanne4870472010-11-04 23:23:47 +0000539 if (resource->flags & IORESOURCE_PCI64)
Eric Biederman03acab62004-10-14 21:25:53 +0000540 pci_write_config32(dev, resource->index + 4, base_hi);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000541 } else if (resource->index == PCI_IO_BASE) {
542 /* Set the I/O ranges. */
543 pci_write_config8(dev, PCI_IO_BASE, base >> 8);
Eric Biederman03acab62004-10-14 21:25:53 +0000544 pci_write_config16(dev, PCI_IO_BASE_UPPER16, base >> 16);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000545 pci_write_config8(dev, PCI_IO_LIMIT, end >> 8);
Eric Biederman03acab62004-10-14 21:25:53 +0000546 pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, end >> 16);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000547 } else if (resource->index == PCI_MEMORY_BASE) {
548 /* Set the memory range. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000549 pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16);
Eric Biederman03acab62004-10-14 21:25:53 +0000550 pci_write_config16(dev, PCI_MEMORY_LIMIT, end >> 16);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000551 } else if (resource->index == PCI_PREF_MEMORY_BASE) {
552 /* Set the prefetchable memory range. */
Eric Biederman03acab62004-10-14 21:25:53 +0000553 pci_write_config16(dev, PCI_PREF_MEMORY_BASE, base >> 16);
554 pci_write_config32(dev, PCI_PREF_BASE_UPPER32, base >> 32);
555 pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, end >> 16);
556 pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, end >> 32);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000557 } else {
558 /* Don't let me think I stored the resource. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000559 resource->flags &= ~IORESOURCE_STORED;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000560 printk(BIOS_ERR, "ERROR: invalid resource->index %lx\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000561 resource->index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000562 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000563
Eric Biederman03acab62004-10-14 21:25:53 +0000564 report_resource_stored(dev, resource, "");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000565}
566
Eric Biederman5899fd82003-04-24 06:25:08 +0000567void pci_dev_set_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000568{
Myles Watsonc25cc112010-05-21 14:33:48 +0000569 struct resource *res;
Myles Watson894a3472010-06-09 22:41:35 +0000570 struct bus *bus;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000571 u8 line;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000572
Uwe Hermanne4870472010-11-04 23:23:47 +0000573 for (res = dev->resource_list; res; res = res->next)
Myles Watsonc25cc112010-05-21 14:33:48 +0000574 pci_set_resource(dev, res);
Uwe Hermanne4870472010-11-04 23:23:47 +0000575
Myles Watson894a3472010-06-09 22:41:35 +0000576 for (bus = dev->link_list; bus; bus = bus->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000577 if (bus->children)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000578 assign_resources(bus);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000579 }
580
Myles Watson29cc9ed2009-07-02 18:56:24 +0000581 /* Set a default latency timer. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000582 pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000583
Myles Watson29cc9ed2009-07-02 18:56:24 +0000584 /* Set a default secondary latency timer. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000585 if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE)
Eric Biederman7a5416a2003-06-12 19:23:51 +0000586 pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000587
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);
Uwe Hermanne4870472010-11-04 23:23:47 +0000590 if (line)
Eric Biederman7a5416a2003-06-12 19:23:51 +0000591 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
Uwe Hermanne4870472010-11-04 23:23:47 +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
Uwe Hermanne4870472010-11-04 23:23:47 +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) {
Duncan Laurie7e1c83e2013-08-09 07:55:10 -0700605 if (CONFIG_SUBSYSTEM_VENDOR_ID)
606 dev->subsystem_vendor = CONFIG_SUBSYSTEM_VENDOR_ID;
607 if (CONFIG_SUBSYSTEM_DEVICE_ID)
608 dev->subsystem_device = CONFIG_SUBSYSTEM_DEVICE_ID;
Sven Schnelle91321022011-03-01 19:58:47 +0000609 printk(BIOS_DEBUG, "%s subsystem <- %04x/%04x\n",
610 dev_path(dev), dev->subsystem_vendor,
611 dev->subsystem_device);
612 ops->set_subsystem(dev, dev->subsystem_vendor,
613 dev->subsystem_device);
Eric Biederman03acab62004-10-14 21:25:53 +0000614 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000615 command = pci_read_config16(dev, PCI_COMMAND);
616 command |= dev->command;
Uwe Hermanne4870472010-11-04 23:23:47 +0000617
Myles Watson29cc9ed2009-07-02 18:56:24 +0000618 /* v3 has
619 * command |= (PCI_COMMAND_PARITY + PCI_COMMAND_SERR); // Error check.
620 */
Uwe Hermanne4870472010-11-04 23:23:47 +0000621
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000622 printk(BIOS_DEBUG, "%s cmd <- %02x\n", dev_path(dev), command);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000623 pci_write_config16(dev, PCI_COMMAND, command);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000624}
625
626void pci_bus_enable_resources(struct device *dev)
627{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000628 u16 ctrl;
629
Uwe Hermanne4870472010-11-04 23:23:47 +0000630 /*
631 * Enable I/O in command register if there is VGA card
Myles Watson29cc9ed2009-07-02 18:56:24 +0000632 * connected with (even it does not claim I/O resource).
633 */
Myles Watson894a3472010-06-09 22:41:35 +0000634 if (dev->link_list->bridge_ctrl & PCI_BRIDGE_CTL_VGA)
Li-Ta Lo515f6c72005-01-11 22:48:54 +0000635 dev->command |= PCI_COMMAND_IO;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000636 ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
Myles Watson894a3472010-06-09 22:41:35 +0000637 ctrl |= dev->link_list->bridge_ctrl;
Uwe Hermanne4870472010-11-04 23:23:47 +0000638 ctrl |= (PCI_BRIDGE_CTL_PARITY + PCI_BRIDGE_CTL_SERR); /* Error check. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000639 printk(BIOS_DEBUG, "%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000640 pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
641
642 pci_dev_enable_resources(dev);
643}
644
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000645void pci_bus_reset(struct bus *bus)
646{
Uwe Hermanne4870472010-11-04 23:23:47 +0000647 u16 ctl;
648
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000649 ctl = pci_read_config16(bus->dev, PCI_BRIDGE_CONTROL);
650 ctl |= PCI_BRIDGE_CTL_BUS_RESET;
651 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
652 mdelay(10);
Uwe Hermanne4870472010-11-04 23:23:47 +0000653
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000654 ctl &= ~PCI_BRIDGE_CTL_BUS_RESET;
655 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
656 delay(1);
657}
658
Myles Watson29cc9ed2009-07-02 18:56:24 +0000659void pci_dev_set_subsystem(struct device *dev, unsigned vendor, unsigned device)
Eric Biederman03acab62004-10-14 21:25:53 +0000660{
Myles Watson032a9652009-05-11 22:24:53 +0000661 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000662 ((device & 0xffff) << 16) | (vendor & 0xffff));
Eric Biederman03acab62004-10-14 21:25:53 +0000663}
664
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300665#if CONFIG_VGA_ROM_RUN
666static int should_run_oprom(struct device *dev)
667{
668 static int should_run = -1;
669
670 if (should_run >= 0)
671 return should_run;
672
Kyösti Mälkki9ab1c102013-12-22 00:22:49 +0200673 /* Don't run VGA option ROMs, unless we have to print
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300674 * something on the screen before the kernel is loaded.
675 */
Kyösti Mälkki9ab1c102013-12-22 00:22:49 +0200676 should_run = !IS_ENABLED(CONFIG_BOOTMODE_STRAPS) ||
677 developer_mode_enabled() || recovery_mode_enabled();
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300678
Kyösti Mälkki9ab1c102013-12-22 00:22:49 +0200679#if CONFIG_CHROMEOS
680 if (!should_run)
681 should_run = vboot_wants_oprom();
682#endif
683 if (!should_run)
684 printk(BIOS_DEBUG, "Not running VGA Option ROM\n");
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300685 return should_run;
686}
687
688static int should_load_oprom(struct device *dev)
689{
690#if CONFIG_HAVE_ACPI_RESUME && !CONFIG_S3_VGA_ROM_RUN
691 /* If S3_VGA_ROM_RUN is disabled, skip running VGA option
692 * ROMs when coming out of an S3 resume.
693 */
694 if ((acpi_slp_type == 3) &&
695 ((dev->class >> 8) == PCI_CLASS_DISPLAY_VGA))
696 return 0;
697#endif
698 if (IS_ENABLED(CONFIG_ALWAYS_LOAD_OPROM))
699 return 1;
700 if (should_run_oprom(dev))
701 return 1;
702
703 return 0;
704}
705#endif /* CONFIG_VGA_ROM_RUN */
706
Uwe Hermanne4870472010-11-04 23:23:47 +0000707/** Default handler: only runs the relevant PCI BIOS. */
Li-Ta Lo883b8792005-01-10 23:16:22 +0000708void pci_dev_init(struct device *dev)
709{
Vladimir Serbinenkob32816e2013-12-20 17:47:19 +0100710#if CONFIG_VGA_ROM_RUN
Li-Ta Lo883b8792005-01-10 23:16:22 +0000711 struct rom_header *rom, *ram;
712
Vladimir Serbinenkob32816e2013-12-20 17:47:19 +0100713 /* Only execute VGA ROMs. */
714 if (((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA))
Myles Watson17aeeca2009-10-07 18:41:08 +0000715 return;
Roman Kononov778a42b2007-04-06 18:34:39 +0000716
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300717 if (!should_load_oprom(dev))
Stefan Reinauer74a0efe2012-03-30 17:10:49 -0700718 return;
Aaron Durbince872cb2013-03-28 15:59:19 -0500719
720 rom = pci_rom_probe(dev);
721 if (rom == NULL)
722 return;
723
724 ram = pci_rom_load(dev, rom);
725 if (ram == NULL)
726 return;
727
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300728 if (!should_run_oprom(dev))
729 return;
730
Stefan Reinauerd98cf5b2008-08-01 11:25:41 +0000731 run_bios(dev, (unsigned long)ram);
Kyösti Mälkkiab56b3b2013-11-28 16:44:51 +0200732 gfx_set_init_done(1);
733 printk(BIOS_DEBUG, "VGA Option ROM was run\n");
Vladimir Serbinenkob32816e2013-12-20 17:47:19 +0100734#endif /* CONFIG_VGA_ROM_RUN */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000735}
Li-Ta Lo883b8792005-01-10 23:16:22 +0000736
Li-Ta Loe5266692004-03-23 21:28:05 +0000737/** Default device operation for PCI devices */
Eric Biedermana9e632c2004-11-18 22:38:08 +0000738static struct pci_operations pci_dev_ops_pci = {
Eric Biederman03acab62004-10-14 21:25:53 +0000739 .set_subsystem = pci_dev_set_subsystem,
740};
741
Eric Biederman8ca8d762003-04-22 19:02:15 +0000742struct device_operations default_pci_ops_dev = {
Uwe Hermanne4870472010-11-04 23:23:47 +0000743 .read_resources = pci_dev_read_resources,
744 .set_resources = pci_dev_set_resources,
Eric Biedermane9a271e32003-09-02 03:36:25 +0000745 .enable_resources = pci_dev_enable_resources,
Uwe Hermanne4870472010-11-04 23:23:47 +0000746 .init = pci_dev_init,
747 .scan_bus = 0,
748 .enable = 0,
749 .ops_pci = &pci_dev_ops_pci,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000750};
Li-Ta Loe5266692004-03-23 21:28:05 +0000751
752/** Default device operations for PCI bridges */
Eric Biedermana9e632c2004-11-18 22:38:08 +0000753static struct pci_operations pci_bus_ops_pci = {
Eric Biederman03acab62004-10-14 21:25:53 +0000754 .set_subsystem = 0,
755};
Li-Ta Lo883b8792005-01-10 23:16:22 +0000756
Eric Biederman8ca8d762003-04-22 19:02:15 +0000757struct device_operations default_pci_ops_bus = {
Uwe Hermanne4870472010-11-04 23:23:47 +0000758 .read_resources = pci_bus_read_resources,
759 .set_resources = pci_dev_set_resources,
Eric Biedermane9a271e32003-09-02 03:36:25 +0000760 .enable_resources = pci_bus_enable_resources,
Uwe Hermanne4870472010-11-04 23:23:47 +0000761 .init = 0,
762 .scan_bus = pci_scan_bridge,
763 .enable = 0,
764 .reset_bus = pci_bus_reset,
765 .ops_pci = &pci_bus_ops_pci,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000766};
Li-Ta Loe5266692004-03-23 21:28:05 +0000767
768/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000769 * Detect the type of downstream bridge.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000770 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000771 * This function is a heuristic to detect which type of bus is downstream
772 * of a PCI-to-PCI bridge. This functions by looking for various capability
773 * blocks to figure out the type of downstream bridge. PCI-X, PCI-E, and
774 * Hypertransport all seem to have appropriate capabilities.
Myles Watson032a9652009-05-11 22:24:53 +0000775 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000776 * When only a PCI-Express capability is found the type is examined to see
777 * which type of bridge we have.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000778 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000779 * @param dev Pointer to the device structure of the bridge.
780 * @return Appropriate bridge operations.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000781 */
782static struct device_operations *get_pci_bridge_ops(device_t dev)
783{
Patrick Georgie1667822012-05-05 15:29:32 +0200784#if CONFIG_PCIX_PLUGIN_SUPPORT
Ronald G. Minnich78a16672012-11-29 16:28:21 -0800785 unsigned int pcixpos;
786 pcixpos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
787 if (pcixpos) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000788 printk(BIOS_DEBUG, "%s subordinate bus PCI-X\n", dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000789 return &default_pcix_ops_bus;
790 }
791#endif
Patrick Georgie1667822012-05-05 15:29:32 +0200792#if CONFIG_AGP_PLUGIN_SUPPORT
Uwe Hermanne4870472010-11-04 23:23:47 +0000793 /* How do I detect a PCI to AGP bridge? */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000794#endif
Patrick Georgie1667822012-05-05 15:29:32 +0200795#if CONFIG_HYPERTRANSPORT_PLUGIN_SUPPORT
Ronald G. Minnich78a16672012-11-29 16:28:21 -0800796 unsigned int htpos = 0;
797 while ((htpos = pci_find_next_capability(dev, PCI_CAP_ID_HT, htpos))) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000798 u16 flags;
Ronald G. Minnich78a16672012-11-29 16:28:21 -0800799 flags = pci_read_config16(dev, htpos + PCI_CAP_FLAGS);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000800 if ((flags >> 13) == 1) {
801 /* Host or Secondary Interface */
Uwe Hermanne4870472010-11-04 23:23:47 +0000802 printk(BIOS_DEBUG, "%s subordinate bus HT\n",
803 dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000804 return &default_ht_ops_bus;
805 }
806 }
807#endif
Patrick Georgie1667822012-05-05 15:29:32 +0200808#if CONFIG_PCIEXP_PLUGIN_SUPPORT
Ronald G. Minnich78a16672012-11-29 16:28:21 -0800809 unsigned int pciexpos;
810 pciexpos = pci_find_capability(dev, PCI_CAP_ID_PCIE);
811 if (pciexpos) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000812 u16 flags;
Ronald G. Minnich78a16672012-11-29 16:28:21 -0800813 flags = pci_read_config16(dev, pciexpos + PCI_EXP_FLAGS);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000814 switch ((flags & PCI_EXP_FLAGS_TYPE) >> 4) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000815 case PCI_EXP_TYPE_ROOT_PORT:
816 case PCI_EXP_TYPE_UPSTREAM:
817 case PCI_EXP_TYPE_DOWNSTREAM:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000818 printk(BIOS_DEBUG, "%s subordinate bus PCI Express\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000819 dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000820 return &default_pciexp_ops_bus;
821 case PCI_EXP_TYPE_PCI_BRIDGE:
Uwe Hermanne4870472010-11-04 23:23:47 +0000822 printk(BIOS_DEBUG, "%s subordinate PCI\n",
823 dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000824 return &default_pci_ops_bus;
825 default:
826 break;
827 }
828 }
829#endif
830 return &default_pci_ops_bus;
831}
832
833/**
Vadim Bendebury8049fc92012-04-24 12:53:19 -0700834 * Check if a device id matches a PCI driver entry.
835 *
836 * The driver entry can either point at a zero terminated array of acceptable
837 * device IDs, or include a single device ID.
838 *
839 * @driver pointer to the PCI driver entry being checked
840 * @device_id PCI device ID of the device being matched
841 */
842static int device_id_match(struct pci_driver *driver, unsigned short device_id)
843{
844 if (driver->devices) {
845 unsigned short check_id;
846 const unsigned short *device_list = driver->devices;
847 while ((check_id = *device_list++) != 0)
848 if (check_id == device_id)
849 return 1;
850 }
851
852 return (driver->device == device_id);
853}
854
855/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000856 * Set up PCI device operation.
857 *
858 * Check if it already has a driver. If not, use find_device_operations(),
859 * or set to a default based on type.
Li-Ta Loe5266692004-03-23 21:28:05 +0000860 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000861 * @param dev Pointer to the device whose pci_ops you want to set.
Li-Ta Loe5266692004-03-23 21:28:05 +0000862 * @see pci_drivers
863 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000864static void set_pci_ops(struct device *dev)
865{
866 struct pci_driver *driver;
Li-Ta Loe5266692004-03-23 21:28:05 +0000867
Uwe Hermanne4870472010-11-04 23:23:47 +0000868 if (dev->ops)
869 return;
870
871 /*
872 * Look through the list of setup drivers and find one for
Myles Watson29cc9ed2009-07-02 18:56:24 +0000873 * this PCI device.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000874 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000875 for (driver = &pci_drivers[0]; driver != &epci_drivers[0]; driver++) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000876 if ((driver->vendor == dev->vendor) &&
Vadim Bendebury8049fc92012-04-24 12:53:19 -0700877 device_id_match(driver, dev->device)) {
Uwe Hermann312673c2009-10-27 21:49:33 +0000878 dev->ops = (struct device_operations *)driver->ops;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000879 printk(BIOS_SPEW, "%s [%04x/%04x] %sops\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000880 dev_path(dev), driver->vendor, driver->device,
881 (driver->ops->scan_bus ? "bus " : ""));
Eric Biederman5899fd82003-04-24 06:25:08 +0000882 return;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000883 }
884 }
Li-Ta Loe5266692004-03-23 21:28:05 +0000885
Uwe Hermanne4870472010-11-04 23:23:47 +0000886 /* If I don't have a specific driver use the default operations. */
887 switch (dev->hdr_type & 0x7f) { /* Header type */
888 case PCI_HEADER_TYPE_NORMAL:
Eric Biederman8ca8d762003-04-22 19:02:15 +0000889 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
890 goto bad;
891 dev->ops = &default_pci_ops_dev;
892 break;
893 case PCI_HEADER_TYPE_BRIDGE:
894 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
895 goto bad;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000896 dev->ops = get_pci_bridge_ops(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000897 break;
Patrick Georgie1667822012-05-05 15:29:32 +0200898#if CONFIG_CARDBUS_PLUGIN_SUPPORT
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000899 case PCI_HEADER_TYPE_CARDBUS:
900 dev->ops = &default_cardbus_ops_bus;
901 break;
902#endif
Uwe Hermanne4870472010-11-04 23:23:47 +0000903default:
904bad:
Li-Ta Lo69c5a902004-04-29 20:08:54 +0000905 if (dev->enabled) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000906 printk(BIOS_ERR, "%s [%04x/%04x/%06x] has unknown "
907 "header type %02x, ignoring.\n", dev_path(dev),
908 dev->vendor, dev->device,
909 dev->class >> 8, dev->hdr_type);
Eric Biederman83b991a2003-10-11 06:20:25 +0000910 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000911 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000912}
913
914/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000915 * See if we have already allocated a device structure for a given devfn.
Li-Ta Loe5266692004-03-23 21:28:05 +0000916 *
917 * Given a linked list of PCI device structures and a devfn number, find the
Li-Ta Lo3a812852004-12-03 22:39:34 +0000918 * device structure correspond to the devfn, if present. This function also
919 * removes the device structure from the linked list.
Li-Ta Loe5266692004-03-23 21:28:05 +0000920 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000921 * @param list The device structure list.
922 * @param devfn A device/function number.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000923 * @return Pointer to the device structure found or NULL if we have not
Li-Ta Lo3a812852004-12-03 22:39:34 +0000924 * allocated a device for this devfn yet.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000925 */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000926static struct device *pci_scan_get_dev(struct device **list, unsigned int devfn)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000927{
Eric Biedermanb78c1972004-10-14 20:54:17 +0000928 struct device *dev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000929
Eric Biedermanb78c1972004-10-14 20:54:17 +0000930 dev = 0;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000931 for (; *list; list = &(*list)->sibling) {
Eric Biedermanad1b35a2003-10-14 02:36:51 +0000932 if ((*list)->path.type != DEVICE_PATH_PCI) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000933 printk(BIOS_ERR, "child %s not a PCI device\n",
934 dev_path(*list));
Eric Biedermanad1b35a2003-10-14 02:36:51 +0000935 continue;
936 }
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000937 if ((*list)->path.pci.devfn == devfn) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000938 /* Unlink from the list. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000939 dev = *list;
940 *list = (*list)->sibling;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000941 dev->sibling = NULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000942 break;
943 }
944 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000945
Uwe Hermanne4870472010-11-04 23:23:47 +0000946 /*
947 * Just like alloc_dev() add the device to the list of devices on the
Myles Watson29cc9ed2009-07-02 18:56:24 +0000948 * bus. When the list of devices was formed we removed all of the
949 * parents children, and now we are interleaving static and dynamic
950 * devices in order on the bus.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000951 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000952 if (dev) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000953 struct device *child;
Uwe Hermanne4870472010-11-04 23:23:47 +0000954
Myles Watson29cc9ed2009-07-02 18:56:24 +0000955 /* Find the last child of our parent. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000956 for (child = dev->bus->children; child && child->sibling;)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000957 child = child->sibling;
Uwe Hermanne4870472010-11-04 23:23:47 +0000958
Myles Watson29cc9ed2009-07-02 18:56:24 +0000959 /* Place the device on the list of children of its parent. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000960 if (child)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000961 child->sibling = dev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000962 else
Eric Biedermane9a271e32003-09-02 03:36:25 +0000963 dev->bus->children = dev;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000964 }
965
Eric Biederman8ca8d762003-04-22 19:02:15 +0000966 return dev;
967}
968
Myles Watson032a9652009-05-11 22:24:53 +0000969/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000970 * Scan a PCI bus.
Li-Ta Loe5266692004-03-23 21:28:05 +0000971 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000972 * Determine the existence of a given PCI device. Allocate a new struct device
973 * if dev==NULL was passed in and the device exists in hardware.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000974 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000975 * @param dev Pointer to the dev structure.
976 * @param bus Pointer to the bus structure.
977 * @param devfn A device/function number to look at.
978 * @return The device structure for the device (if found), NULL otherwise.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000979 */
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000980device_t pci_probe_dev(device_t dev, struct bus *bus, unsigned devfn)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000981{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000982 u32 id, class;
983 u8 hdr_type;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000984
Myles Watson29cc9ed2009-07-02 18:56:24 +0000985 /* Detect if a device is present. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000986 if (!dev) {
987 struct device dummy;
Uwe Hermanne4870472010-11-04 23:23:47 +0000988
Myles Watson29cc9ed2009-07-02 18:56:24 +0000989 dummy.bus = bus;
990 dummy.path.type = DEVICE_PATH_PCI;
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000991 dummy.path.pci.devfn = devfn;
Uwe Hermanne4870472010-11-04 23:23:47 +0000992
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000993 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
Uwe Hermanne4870472010-11-04 23:23:47 +0000994 /*
995 * Have we found something? Some broken boards return 0 if a
996 * slot is empty, but the expected answer is 0xffffffff.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000997 */
Uwe Hermanne4870472010-11-04 23:23:47 +0000998 if (id == 0xffffffff)
Stefan Reinauer7355c752010-04-02 16:30:25 +0000999 return NULL;
Uwe Hermanne4870472010-11-04 23:23:47 +00001000
Stefan Reinauer7355c752010-04-02 16:30:25 +00001001 if ((id == 0x00000000) || (id == 0x0000ffff) ||
1002 (id == 0xffff0000)) {
Uwe Hermanne4870472010-11-04 23:23:47 +00001003 printk(BIOS_SPEW, "%s, bad id 0x%x\n",
1004 dev_path(&dummy), id);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001005 return NULL;
1006 }
1007 dev = alloc_dev(bus, &dummy.path);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001008 } else {
Uwe Hermanne4870472010-11-04 23:23:47 +00001009 /*
1010 * Enable/disable the device. Once we have found the device-
Myles Watson29cc9ed2009-07-02 18:56:24 +00001011 * specific operations this operations we will disable the
1012 * device with those as well.
Myles Watson032a9652009-05-11 22:24:53 +00001013 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001014 * This is geared toward devices that have subfunctions
1015 * that do not show up by default.
Myles Watson032a9652009-05-11 22:24:53 +00001016 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001017 * If a device is a stuff option on the motherboard
Myles Watson29cc9ed2009-07-02 18:56:24 +00001018 * it may be absent and enable_dev() must cope.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001019 */
Myles Watson29cc9ed2009-07-02 18:56:24 +00001020 /* Run the magic enable sequence for the device. */
Uwe Hermanne4870472010-11-04 23:23:47 +00001021 if (dev->chip_ops && dev->chip_ops->enable_dev)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001022 dev->chip_ops->enable_dev(dev);
Uwe Hermanne4870472010-11-04 23:23:47 +00001023
Myles Watson29cc9ed2009-07-02 18:56:24 +00001024 /* Now read the vendor and device ID. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001025 id = pci_read_config32(dev, PCI_VENDOR_ID);
Myles Watson032a9652009-05-11 22:24:53 +00001026
Uwe Hermanne4870472010-11-04 23:23:47 +00001027 /*
1028 * If the device does not have a PCI ID disable it. Possibly
Myles Watson29cc9ed2009-07-02 18:56:24 +00001029 * this is because we have already disabled the device. But
1030 * this also handles optional devices that may not always
1031 * show up.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001032 */
1033 /* If the chain is fully enumerated quit */
Myles Watson29cc9ed2009-07-02 18:56:24 +00001034 if ((id == 0xffffffff) || (id == 0x00000000) ||
1035 (id == 0x0000ffff) || (id == 0xffff0000)) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001036 if (dev->enabled) {
Uwe Hermanne4870472010-11-04 23:23:47 +00001037 printk(BIOS_INFO, "PCI: Static device %s not "
1038 "found, disabling it.\n", dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001039 dev->enabled = 0;
1040 }
1041 return dev;
1042 }
1043 }
Uwe Hermanne4870472010-11-04 23:23:47 +00001044
Myles Watson29cc9ed2009-07-02 18:56:24 +00001045 /* Read the rest of the PCI configuration information. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001046 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
1047 class = pci_read_config32(dev, PCI_CLASS_REVISION);
Myles Watson032a9652009-05-11 22:24:53 +00001048
Myles Watson29cc9ed2009-07-02 18:56:24 +00001049 /* Store the interesting information in the device structure. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001050 dev->vendor = id & 0xffff;
1051 dev->device = (id >> 16) & 0xffff;
1052 dev->hdr_type = hdr_type;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001053
1054 /* Class code, the upper 3 bytes of PCI_CLASS_REVISION. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001055 dev->class = class >> 8;
Myles Watson032a9652009-05-11 22:24:53 +00001056
Myles Watson29cc9ed2009-07-02 18:56:24 +00001057 /* Architectural/System devices always need to be bus masters. */
Uwe Hermanne4870472010-11-04 23:23:47 +00001058 if ((dev->class >> 16) == PCI_BASE_CLASS_SYSTEM)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001059 dev->command |= PCI_COMMAND_MASTER;
Uwe Hermanne4870472010-11-04 23:23:47 +00001060
1061 /*
1062 * Look at the vendor and device ID, or at least the header type and
Myles Watson29cc9ed2009-07-02 18:56:24 +00001063 * class and figure out which set of configuration methods to use.
1064 * Unless we already have some PCI ops.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001065 */
1066 set_pci_ops(dev);
1067
Myles Watson29cc9ed2009-07-02 18:56:24 +00001068 /* Now run the magic enable/disable sequence for the device. */
Uwe Hermanne4870472010-11-04 23:23:47 +00001069 if (dev->ops && dev->ops->enable)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001070 dev->ops->enable(dev);
Myles Watson032a9652009-05-11 22:24:53 +00001071
Myles Watson29cc9ed2009-07-02 18:56:24 +00001072 /* Display the device. */
Uwe Hermanne4870472010-11-04 23:23:47 +00001073 printk(BIOS_DEBUG, "%s [%04x/%04x] %s%s\n", dev_path(dev),
1074 dev->vendor, dev->device, dev->enabled ? "enabled" : "disabled",
1075 dev->ops ? "" : " No operations");
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001076
1077 return dev;
1078}
1079
Myles Watson032a9652009-05-11 22:24:53 +00001080/**
Kyösti Mälkkic73acdb2013-06-15 17:16:56 +03001081 * Test for match between romstage and ramstage device instance.
1082 *
1083 * @param dev Pointer to the device structure.
1084 * @param sdev Simple device model identifier, created with PCI_DEV().
1085 * @return Non-zero if bus:dev.fn of device matches.
1086 */
1087unsigned int pci_match_simple_dev(device_t dev, pci_devfn_t sdev)
1088{
1089 return dev->bus->secondary == PCI_DEV2SEGBUS(sdev) &&
1090 dev->path.pci.devfn == PCI_DEV2DEVFN(sdev);
1091}
1092
1093/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001094 * Scan a PCI bus.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001095 *
Li-Ta Loe5266692004-03-23 21:28:05 +00001096 * Determine the existence of devices and bridges on a PCI bus. If there are
1097 * bridges on the bus, recursively scan the buses behind the bridges.
1098 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001099 * This function is the default scan_bus() method for the root device
1100 * 'dev_root'.
1101 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001102 * @param bus Pointer to the bus structure.
1103 * @param min_devfn Minimum devfn to look at in the scan, usually 0x00.
1104 * @param max_devfn Maximum devfn to look at in the scan, usually 0xff.
1105 * @param max Current bus number.
1106 * @return The maximum bus number found, after scanning all subordinate busses.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001107 */
Uwe Hermanne4870472010-11-04 23:23:47 +00001108unsigned int pci_scan_bus(struct bus *bus, unsigned min_devfn,
1109 unsigned max_devfn, unsigned int max)
Eric Biederman8ca8d762003-04-22 19:02:15 +00001110{
1111 unsigned int devfn;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001112 struct device *old_devices;
1113 struct device *child;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001114
Stefan Reinauer08670622009-06-30 15:17:49 +00001115#if CONFIG_PCI_BUS_SEGN_BITS
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001116 printk(BIOS_DEBUG, "PCI: pci_scan_bus for bus %04x:%02x\n",
Uwe Hermanne4870472010-11-04 23:23:47 +00001117 bus->secondary >> 8, bus->secondary & 0xff);
Yinghai Lu5f9624d2006-10-04 22:56:21 +00001118#else
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001119 printk(BIOS_DEBUG, "PCI: pci_scan_bus for bus %02x\n", bus->secondary);
Yinghai Lu5f9624d2006-10-04 22:56:21 +00001120#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +00001121
Uwe Hermanne4870472010-11-04 23:23:47 +00001122 /* Maximum sane devfn is 0xFF. */
Juhana Helovuo50b78b62010-09-13 14:43:02 +00001123 if (max_devfn > 0xff) {
Uwe Hermanne4870472010-11-04 23:23:47 +00001124 printk(BIOS_ERR, "PCI: pci_scan_bus limits devfn %x - "
1125 "devfn %x\n", min_devfn, max_devfn);
1126 printk(BIOS_ERR, "PCI: pci_scan_bus upper limit too big. "
1127 "Using 0xff.\n");
Juhana Helovuo50b78b62010-09-13 14:43:02 +00001128 max_devfn=0xff;
1129 }
1130
Eric Biederman8ca8d762003-04-22 19:02:15 +00001131 old_devices = bus->children;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001132 bus->children = NULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001133
1134 post_code(0x24);
Uwe Hermanne4870472010-11-04 23:23:47 +00001135
1136 /*
1137 * Probe all devices/functions on this bus with some optimization for
Myles Watson29cc9ed2009-07-02 18:56:24 +00001138 * non-existence and single function devices.
Eric Biedermanb78c1972004-10-14 20:54:17 +00001139 */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001140 for (devfn = min_devfn; devfn <= max_devfn; devfn++) {
Myles Watson29cc9ed2009-07-02 18:56:24 +00001141 struct device *dev;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001142
Uwe Hermanne4870472010-11-04 23:23:47 +00001143 /* First thing setup the device structure. */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001144 dev = pci_scan_get_dev(&old_devices, devfn);
Li-Ta Lo9782f752004-05-05 21:15:42 +00001145
Myles Watson29cc9ed2009-07-02 18:56:24 +00001146 /* See if a device is present and setup the device structure. */
Myles Watson032a9652009-05-11 22:24:53 +00001147 dev = pci_probe_dev(dev, bus, devfn);
Eric Biederman03acab62004-10-14 21:25:53 +00001148
Uwe Hermanne4870472010-11-04 23:23:47 +00001149 /*
1150 * If this is not a multi function device, or the device is
Myles Watson29cc9ed2009-07-02 18:56:24 +00001151 * not present don't waste time probing another function.
Myles Watson032a9652009-05-11 22:24:53 +00001152 * Skip to next device.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001153 */
Uwe Hermanne4870472010-11-04 23:23:47 +00001154 if ((PCI_FUNC(devfn) == 0x00) && (!dev
Myles Watson29cc9ed2009-07-02 18:56:24 +00001155 || (dev->enabled && ((dev->hdr_type & 0x80) != 0x80)))) {
Eric Biederman8ca8d762003-04-22 19:02:15 +00001156 devfn += 0x07;
1157 }
1158 }
Uwe Hermanne4870472010-11-04 23:23:47 +00001159
Eric Biederman8ca8d762003-04-22 19:02:15 +00001160 post_code(0x25);
1161
Uwe Hermanne4870472010-11-04 23:23:47 +00001162 /*
1163 * Warn if any leftover static devices are are found.
1164 * There's probably a problem in devicetree.cb.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001165 */
1166 if (old_devices) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001167 device_t left;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001168 printk(BIOS_WARNING, "PCI: Left over static devices:\n");
Uwe Hermanne4870472010-11-04 23:23:47 +00001169 for (left = old_devices; left; left = left->sibling)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001170 printk(BIOS_WARNING, "%s\n", dev_path(left));
Uwe Hermanne4870472010-11-04 23:23:47 +00001171
1172 printk(BIOS_WARNING, "PCI: Check your devicetree.cb.\n");
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001173 }
1174
Uwe Hermanne4870472010-11-04 23:23:47 +00001175 /*
1176 * For all children that implement scan_bus() (i.e. bridges)
Eric Biedermanb78c1972004-10-14 20:54:17 +00001177 * scan the bus behind that child.
1178 */
Uwe Hermanne4870472010-11-04 23:23:47 +00001179 for (child = bus->children; child; child = child->sibling)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001180 max = scan_bus(child, max);
Li-Ta Loe5266692004-03-23 21:28:05 +00001181
Uwe Hermanne4870472010-11-04 23:23:47 +00001182 /*
1183 * We've scanned the bus and so we know all about what's on the other
Myles Watson29cc9ed2009-07-02 18:56:24 +00001184 * side of any bridges that may be on this bus plus any devices.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001185 * Return how far we've got finding sub-buses.
1186 */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001187 printk(BIOS_DEBUG, "PCI: pci_scan_bus returning with max=%03x\n", max);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001188 post_code(0x55);
1189 return max;
1190}
1191
Li-Ta Loe5266692004-03-23 21:28:05 +00001192/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001193 * Scan a PCI bridge and the buses behind the bridge.
Li-Ta Loe5266692004-03-23 21:28:05 +00001194 *
1195 * Determine the existence of buses behind the bridge. Set up the bridge
1196 * according to the result of the scan.
1197 *
1198 * This function is the default scan_bus() method for PCI bridge devices.
1199 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001200 * @param dev Pointer to the bridge device.
1201 * @param max The highest bus number assigned up to now.
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001202 * @param do_scan_bus TODO
Myles Watson29cc9ed2009-07-02 18:56:24 +00001203 * @return The maximum bus number found, after scanning all subordinate buses.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001204 */
Myles Watson032a9652009-05-11 22:24:53 +00001205unsigned int do_pci_scan_bridge(struct device *dev, unsigned int max,
Myles Watson29cc9ed2009-07-02 18:56:24 +00001206 unsigned int (*do_scan_bus) (struct bus * bus,
1207 unsigned min_devfn,
1208 unsigned max_devfn,
1209 unsigned int max))
Eric Biederman8ca8d762003-04-22 19:02:15 +00001210{
Eric Biedermane9a271e32003-09-02 03:36:25 +00001211 struct bus *bus;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001212 u32 buses;
1213 u16 cr;
Eric Biederman83b991a2003-10-11 06:20:25 +00001214
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001215 printk(BIOS_SPEW, "%s for %s\n", __func__, dev_path(dev));
Li-Ta Lo3a812852004-12-03 22:39:34 +00001216
Myles Watson894a3472010-06-09 22:41:35 +00001217 if (dev->link_list == NULL) {
1218 struct bus *link;
1219 link = malloc(sizeof(*link));
1220 if (link == NULL)
1221 die("Couldn't allocate a link!\n");
1222 memset(link, 0, sizeof(*link));
1223 link->dev = dev;
1224 dev->link_list = link;
1225 }
1226
1227 bus = dev->link_list;
Eric Biedermane9a271e32003-09-02 03:36:25 +00001228
Uwe Hermanne4870472010-11-04 23:23:47 +00001229 /*
1230 * Set up the primary, secondary and subordinate bus numbers. We have
Eric Biederman8ca8d762003-04-22 19:02:15 +00001231 * no idea how many buses are behind this bridge yet, so we set the
Myles Watson032a9652009-05-11 22:24:53 +00001232 * subordinate bus number to 0xff for the moment.
Eric Biedermanb78c1972004-10-14 20:54:17 +00001233 */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001234 bus->secondary = ++max;
1235 bus->subordinate = 0xff;
Li-Ta Loe5266692004-03-23 21:28:05 +00001236
Eric Biederman8ca8d762003-04-22 19:02:15 +00001237 /* Clear all status bits and turn off memory, I/O and master enables. */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001238 cr = pci_read_config16(dev, PCI_COMMAND);
1239 pci_write_config16(dev, PCI_COMMAND, 0x0000);
1240 pci_write_config16(dev, PCI_STATUS, 0xffff);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001241
Uwe Hermanne4870472010-11-04 23:23:47 +00001242 /*
1243 * Read the existing primary/secondary/subordinate bus
Eric Biedermanb78c1972004-10-14 20:54:17 +00001244 * number configuration.
1245 */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001246 buses = pci_read_config32(dev, PCI_PRIMARY_BUS);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001247
Uwe Hermanne4870472010-11-04 23:23:47 +00001248 /*
1249 * Configure the bus numbers for this bridge: the configuration
Eric Biederman8ca8d762003-04-22 19:02:15 +00001250 * transactions will not be propagated by the bridge if it is not
Eric Biedermanb78c1972004-10-14 20:54:17 +00001251 * correctly configured.
1252 */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001253 buses &= 0xff000000;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001254 buses |= (((unsigned int)(dev->bus->secondary) << 0) |
1255 ((unsigned int)(bus->secondary) << 8) |
1256 ((unsigned int)(bus->subordinate) << 16));
Eric Biedermane9a271e32003-09-02 03:36:25 +00001257 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
Li-Ta Lo3a812852004-12-03 22:39:34 +00001258
Uwe Hermanne4870472010-11-04 23:23:47 +00001259 /* Now we can scan all subordinate buses (those behind the bridge). */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001260 max = do_scan_bus(bus, 0x00, 0xff, max);
Li-Ta Lo3a812852004-12-03 22:39:34 +00001261
Uwe Hermanne4870472010-11-04 23:23:47 +00001262 /*
1263 * We know the number of buses behind this bridge. Set the subordinate
Eric Biedermanb78c1972004-10-14 20:54:17 +00001264 * bus number to its real value.
1265 */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001266 bus->subordinate = max;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001267 buses = (buses & 0xff00ffff) | ((unsigned int)(bus->subordinate) << 16);
Eric Biedermane9a271e32003-09-02 03:36:25 +00001268 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
1269 pci_write_config16(dev, PCI_COMMAND, cr);
Myles Watson032a9652009-05-11 22:24:53 +00001270
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001271 printk(BIOS_SPEW, "%s returns max %d\n", __func__, max);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001272 return max;
1273}
Li-Ta Loe5266692004-03-23 21:28:05 +00001274
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001275/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001276 * Scan a PCI bridge and the buses behind the bridge.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001277 *
1278 * Determine the existence of buses behind the bridge. Set up the bridge
1279 * according to the result of the scan.
1280 *
1281 * This function is the default scan_bus() method for PCI bridge devices.
1282 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001283 * @param dev Pointer to the bridge device.
1284 * @param max The highest bus number assigned up to now.
1285 * @return The maximum bus number found, after scanning all subordinate buses.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001286 */
1287unsigned int pci_scan_bridge(struct device *dev, unsigned int max)
1288{
1289 return do_pci_scan_bridge(dev, max, pci_scan_bus);
1290}
1291
Myles Watson29cc9ed2009-07-02 18:56:24 +00001292/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001293 * Scan a PCI domain.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001294 *
1295 * This function is the default scan_bus() method for PCI domains.
1296 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001297 * @param dev Pointer to the domain.
1298 * @param max The highest bus number assigned up to now.
1299 * @return The maximum bus number found, after scanning all subordinate busses.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001300 */
1301unsigned int pci_domain_scan_bus(device_t dev, unsigned int max)
1302{
Myles Watson894a3472010-06-09 22:41:35 +00001303 max = pci_scan_bus(dev->link_list, PCI_DEVFN(0, 0), 0xff, max);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001304 return max;
1305}
1306
Mike Loptien0f5cf5e2014-05-12 21:46:31 -06001307/**
1308 * Take an INT_PIN number (0, 1 - 4) and convert
1309 * it to a string ("NO PIN", "PIN A" - "PIN D")
1310 *
1311 * @param pin PCI Interrupt Pin number (0, 1 - 4)
1312 * @return A string corresponding to the pin number or "Invalid"
1313 */
1314const char *pin_to_str(int pin)
1315{
1316 const char *str[5] = {
1317 "NO PIN",
1318 "PIN A",
1319 "PIN B",
1320 "PIN C",
1321 "PIN D",
1322 };
1323
1324 if (pin >= 0 && pin <= 4)
1325 return str[pin];
1326 else
1327 return "Invalid PIN, not 0 - 4";
1328}
1329
1330/**
1331 * Get the PCI INT_PIN swizzle for a device defined as:
1332 * pin_parent = (pin_child + devn_child) % 4 + 1
1333 * where PIN A = 1 ... PIN_D = 4
1334 *
1335 * Given a PCI device structure 'dev', find the interrupt pin
1336 * that will be triggered on its parent bridge device when
1337 * generating an interrupt. For example: Device 1:3.2 may
1338 * use INT_PIN A but will trigger PIN D on its parent bridge
1339 * device. In this case, this function will return 4 (PIN D).
1340 *
1341 * @param dev A PCI device structure to swizzle interrupt pins for
1342 * @param *parent_bdg The PCI device structure for the bridge
1343 * device 'dev' is attached to
1344 * @return The interrupt pin number (1 - 4) that 'dev' will
1345 * trigger when generating an interrupt
1346 */
1347static int swizzle_irq_pins(device_t dev, device_t *parent_bridge)
1348{
1349 device_t parent; /* Our current device's parent device */
1350 device_t child; /* The child device of the parent */
1351 uint8_t parent_bus = 0; /* Parent Bus number */
1352 uint16_t parent_devfn = 0; /* Parent Device and Function number */
1353 uint16_t child_devfn = 0; /* Child Device and Function number */
1354 uint8_t swizzled_pin = 0; /* Pin swizzled across a bridge */
1355
1356 /* Start with PIN A = 0 ... D = 3 */
1357 swizzled_pin = pci_read_config8(dev, PCI_INTERRUPT_PIN) - 1;
1358
1359 /* While our current device has parent devices */
1360 child = dev;
1361 for (parent = child->bus->dev; parent; parent = parent->bus->dev) {
1362 parent_bus = parent->bus->secondary;
1363 parent_devfn = parent->path.pci.devfn;
1364 child_devfn = child->path.pci.devfn;
1365
1366 /* Swizzle the INT_PIN for any bridges not on root bus */
1367 swizzled_pin = (PCI_SLOT(child_devfn) + swizzled_pin) % 4;
1368 printk(BIOS_SPEW, "\tWith INT_PIN swizzled to %s\n"
1369 "\tAttached to bridge device %01X:%02Xh.%02Xh\n",
1370 pin_to_str(swizzled_pin + 1), parent_bus,
1371 PCI_SLOT(parent_devfn), PCI_FUNC(parent_devfn));
1372
1373 /* Continue until we find the root bus */
1374 if (parent_bus > 0) {
1375 /*
1376 * We will go on to the next parent so this parent
1377 * becomes the child
1378 */
1379 child = parent;
1380 continue;
1381 } else {
1382 /*
1383 * Found the root bridge device,
1384 * fill in the structure and exit
1385 */
1386 *parent_bridge = parent;
1387 break;
1388 }
1389 }
1390
1391 /* End with PIN A = 1 ... D = 4 */
1392 return swizzled_pin + 1;
1393}
1394
1395/**
1396 * Given a device structure 'dev', find its interrupt pin
1397 * and its parent bridge 'parent_bdg' device structure.
1398 * If it is behind a bridge, it will return the interrupt
1399 * pin number (1 - 4) of the parent bridge that the device
1400 * interrupt pin has been swizzled to, otherwise it will
1401 * return the interrupt pin that is programmed into the
1402 * PCI config space of the target device. If 'dev' is
1403 * behind a bridge, it will fill in 'parent_bdg' with the
1404 * device structure of the bridge it is behind, otherwise
1405 * it will copy 'dev' into 'parent_bdg'.
1406 *
1407 * @param dev A PCI device structure to get interrupt pins for.
1408 * @param *parent_bdg The PCI device structure for the bridge
1409 * device 'dev' is attached to.
1410 * @return The interrupt pin number (1 - 4) that 'dev' will
1411 * trigger when generating an interrupt.
1412 * Errors: -1 is returned if the device is not enabled
1413 * -2 is returned if a parent bridge could not be found.
1414 */
1415int get_pci_irq_pins(device_t dev, device_t *parent_bdg)
1416{
1417 uint8_t bus = 0; /* The bus this device is on */
1418 uint16_t devfn = 0; /* This device's device and function numbers */
1419 uint8_t int_pin = 0; /* Interrupt pin used by the device */
1420 uint8_t target_pin = 0; /* Interrupt pin we want to assign an IRQ to */
1421
1422 /* Make sure this device is enabled */
1423 if (!(dev->enabled && (dev->path.type == DEVICE_PATH_PCI)))
1424 return -1;
1425
1426 bus = dev->bus->secondary;
1427 devfn = dev->path.pci.devfn;
1428
1429 /* Get and validate the interrupt pin used. Only 1-4 are allowed */
1430 int_pin = pci_read_config8(dev, PCI_INTERRUPT_PIN);
1431 if (int_pin < 1 || int_pin > 4)
1432 return -1;
1433
1434 printk(BIOS_SPEW, "PCI IRQ: Found device %01X:%02X.%02X using %s\n",
1435 bus, PCI_SLOT(devfn), PCI_FUNC(devfn), pin_to_str(int_pin));
1436
1437 /* If this device is on a bridge, swizzle its INT_PIN */
1438 if (bus) {
1439 /* Swizzle its INT_PINs */
1440 target_pin = swizzle_irq_pins(dev, parent_bdg);
1441
1442 /* Make sure the swizzle returned valid structures */
1443 if (parent_bdg == NULL) {
1444 printk(BIOS_WARNING,
1445 "Warning: Could not find parent bridge for this device!\n");
1446 return -2;
1447 }
1448 } else { /* Device is not behind a bridge */
1449 target_pin = int_pin; /* Return its own interrupt pin */
1450 *parent_bdg = dev; /* Return its own structure */
1451 }
1452
1453 /* Target pin is the interrupt pin we want to assign an IRQ to */
1454 return target_pin;
1455}
1456
Patrick Georgie1667822012-05-05 15:29:32 +02001457#if CONFIG_PC80_SYSTEM
Myles Watson29cc9ed2009-07-02 18:56:24 +00001458/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001459 * Assign IRQ numbers.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001460 *
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001461 * This function assigns IRQs for all functions contained within the indicated
Uwe Hermanne4870472010-11-04 23:23:47 +00001462 * device address. If the device does not exist or does not require interrupts
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001463 * then this function has no effect.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001464 *
1465 * This function should be called for each PCI slot in your system.
1466 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001467 * @param bus Pointer to the bus structure.
1468 * @param slot TODO
1469 * @param pIntAtoD An array of IRQ #s that are assigned to PINTA through PINTD
1470 * of this slot. The particular IRQ #s that are passed in depend on the
1471 * routing inside your southbridge and on your board.
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001472 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001473void pci_assign_irqs(unsigned bus, unsigned slot,
Uwe Hermanne4870472010-11-04 23:23:47 +00001474 const unsigned char pIntAtoD[4])
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001475{
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001476 unsigned int funct;
1477 device_t pdev;
Uwe Hermanne4870472010-11-04 23:23:47 +00001478 u8 line, irq;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001479
Uwe Hermanne4870472010-11-04 23:23:47 +00001480 /* Each slot may contain up to eight functions. */
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001481 for (funct = 0; funct < 8; funct++) {
1482 pdev = dev_find_slot(bus, (slot << 3) + funct);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001483
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001484 if (!pdev)
1485 continue;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001486
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001487 line = pci_read_config8(pdev, PCI_INTERRUPT_PIN);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001488
Uwe Hermanne4870472010-11-04 23:23:47 +00001489 /* PCI spec says all values except 1..4 are reserved. */
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001490 if ((line < 1) || (line > 4))
1491 continue;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001492
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001493 irq = pIntAtoD[line - 1];
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001494
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001495 printk(BIOS_DEBUG, "Assigning IRQ %d to %d:%x.%d\n",
Uwe Hermanne4870472010-11-04 23:23:47 +00001496 irq, bus, slot, funct);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001497
Stefan Reinauer14e22772010-04-27 06:56:47 +00001498 pci_write_config8(pdev, PCI_INTERRUPT_LINE,
Uwe Hermanne4870472010-11-04 23:23:47 +00001499 pIntAtoD[line - 1]);
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001500
1501#ifdef PARANOID_IRQ_ASSIGNMENTS
Myles Watson17aeeca2009-10-07 18:41:08 +00001502 irq = pci_read_config8(pdev, PCI_INTERRUPT_LINE);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001503 printk(BIOS_DEBUG, " Readback = %d\n", irq);
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001504#endif
1505
Patrick Georgie1667822012-05-05 15:29:32 +02001506#if CONFIG_PC80_SYSTEM
Uwe Hermanne4870472010-11-04 23:23:47 +00001507 /* Change to level triggered. */
1508 i8259_configure_irq_trigger(pIntAtoD[line - 1],
1509 IRQ_LEVEL_TRIGGERED);
Stefan Reinauer5fb62162010-12-16 23:52:04 +00001510#endif
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001511 }
1512}
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001513#endif