blob: 51232298c671996e7a1c6767fbb0dc7398e3e92a [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
Edward O'Callaghan6c992502014-06-20 21:19:06 +100027#include <arch/acpi.h>
28#include <arch/io.h>
29#include <bootmode.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000030#include <console/console.h>
31#include <stdlib.h>
32#include <stdint.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000033#include <string.h>
Edward O'Callaghan6c992502014-06-20 21:19:06 +100034#include <delay.h>
Edward O'Callaghan6c992502014-06-20 21:19:06 +100035#include <device/cardbus.h>
Eric Biederman5899fd82003-04-24 06:25:08 +000036#include <device/device.h>
37#include <device/pci.h>
38#include <device/pci_ids.h>
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000039#include <device/pcix.h>
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000040#include <device/pciexp.h>
Edward O'Callaghan6c992502014-06-20 21:19:06 +100041#include <device/hypertransport.h>
Stefan Reinauer4d933dd2009-07-21 21:36:41 +000042#include <pc80/i8259.h>
Edward O'Callaghan6c992502014-06-20 21:19:06 +100043#include <kconfig.h>
Stefan Reinauer74a0efe2012-03-30 17:10:49 -070044#include <vendorcode/google/chromeos/chromeos.h>
Eric Biederman03acab62004-10-14 21:25:53 +000045
Myles Watson29cc9ed2009-07-02 18:56:24 +000046u8 pci_moving_config8(struct device *dev, unsigned int reg)
Eric Biederman03acab62004-10-14 21:25:53 +000047{
Myles Watson29cc9ed2009-07-02 18:56:24 +000048 u8 value, ones, zeroes;
Uwe Hermanne4870472010-11-04 23:23:47 +000049
Eric Biederman03acab62004-10-14 21:25:53 +000050 value = pci_read_config8(dev, reg);
Myles Watson032a9652009-05-11 22:24:53 +000051
Eric Biederman03acab62004-10-14 21:25:53 +000052 pci_write_config8(dev, reg, 0xff);
53 ones = pci_read_config8(dev, reg);
54
55 pci_write_config8(dev, reg, 0x00);
56 zeroes = pci_read_config8(dev, reg);
57
58 pci_write_config8(dev, reg, value);
59
60 return ones ^ zeroes;
61}
Li-Ta Lo9a5b4962004-12-23 21:48:01 +000062
Uwe Hermanne4870472010-11-04 23:23:47 +000063u16 pci_moving_config16(struct device *dev, unsigned int reg)
Eric Biederman03acab62004-10-14 21:25:53 +000064{
Myles Watson29cc9ed2009-07-02 18:56:24 +000065 u16 value, ones, zeroes;
Uwe Hermanne4870472010-11-04 23:23:47 +000066
Eric Biederman03acab62004-10-14 21:25:53 +000067 value = pci_read_config16(dev, reg);
Myles Watson032a9652009-05-11 22:24:53 +000068
Eric Biederman03acab62004-10-14 21:25:53 +000069 pci_write_config16(dev, reg, 0xffff);
70 ones = pci_read_config16(dev, reg);
71
72 pci_write_config16(dev, reg, 0x0000);
73 zeroes = pci_read_config16(dev, reg);
74
75 pci_write_config16(dev, reg, value);
76
77 return ones ^ zeroes;
78}
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +000079
Uwe Hermanne4870472010-11-04 23:23:47 +000080u32 pci_moving_config32(struct device *dev, unsigned int reg)
Eric Biederman03acab62004-10-14 21:25:53 +000081{
Myles Watson29cc9ed2009-07-02 18:56:24 +000082 u32 value, ones, zeroes;
Uwe Hermanne4870472010-11-04 23:23:47 +000083
Eric Biederman03acab62004-10-14 21:25:53 +000084 value = pci_read_config32(dev, reg);
Myles Watson032a9652009-05-11 22:24:53 +000085
Eric Biederman03acab62004-10-14 21:25:53 +000086 pci_write_config32(dev, reg, 0xffffffff);
87 ones = pci_read_config32(dev, reg);
88
89 pci_write_config32(dev, reg, 0x00000000);
90 zeroes = pci_read_config32(dev, reg);
91
92 pci_write_config32(dev, reg, value);
93
94 return ones ^ zeroes;
95}
96
Myles Watson29cc9ed2009-07-02 18:56:24 +000097/**
98 * Given a device, a capability type, and a last position, return the next
99 * matching capability. Always start at the head of the list.
100 *
101 * @param dev Pointer to the device structure.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000102 * @param cap PCI_CAP_LIST_ID of the PCI capability we're looking for.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000103 * @param last Location of the PCI capability register to start from.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000104 * @return The next matching capability.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000105 */
106unsigned pci_find_next_capability(struct device *dev, unsigned cap,
107 unsigned last)
Eric Biederman03acab62004-10-14 21:25:53 +0000108{
Stefan Reinauer4d933dd2009-07-21 21:36:41 +0000109 unsigned pos = 0;
Uwe Hermanne4870472010-11-04 23:23:47 +0000110 u16 status;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000111 unsigned reps = 48;
Stefan Reinauer4d933dd2009-07-21 21:36:41 +0000112
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000113 status = pci_read_config16(dev, PCI_STATUS);
Uwe Hermanne4870472010-11-04 23:23:47 +0000114 if (!(status & PCI_STATUS_CAP_LIST))
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000115 return 0;
Uwe Hermanne4870472010-11-04 23:23:47 +0000116
Myles Watson29cc9ed2009-07-02 18:56:24 +0000117 switch (dev->hdr_type & 0x7f) {
Eric Biederman03acab62004-10-14 21:25:53 +0000118 case PCI_HEADER_TYPE_NORMAL:
119 case PCI_HEADER_TYPE_BRIDGE:
120 pos = PCI_CAPABILITY_LIST;
121 break;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000122 case PCI_HEADER_TYPE_CARDBUS:
123 pos = PCI_CB_CAPABILITY_LIST;
124 break;
125 default:
126 return 0;
Eric Biederman03acab62004-10-14 21:25:53 +0000127 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000128
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000129 pos = pci_read_config8(dev, pos);
Uwe Hermanne4870472010-11-04 23:23:47 +0000130 while (reps-- && (pos >= 0x40)) { /* Loop through the linked list. */
Eric Biederman03acab62004-10-14 21:25:53 +0000131 int this_cap;
Uwe Hermanne4870472010-11-04 23:23:47 +0000132
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000133 pos &= ~3;
Eric Biederman03acab62004-10-14 21:25:53 +0000134 this_cap = pci_read_config8(dev, pos + PCI_CAP_LIST_ID);
Uwe Hermanne4870472010-11-04 23:23:47 +0000135 printk(BIOS_SPEW, "Capability: type 0x%02x @ 0x%02x\n",
136 this_cap, pos);
137 if (this_cap == 0xff)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000138 break;
Uwe Hermanne4870472010-11-04 23:23:47 +0000139
140 if (!last && (this_cap == cap))
Eric Biederman03acab62004-10-14 21:25:53 +0000141 return pos;
Uwe Hermanne4870472010-11-04 23:23:47 +0000142
143 if (last == pos)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000144 last = 0;
Uwe Hermanne4870472010-11-04 23:23:47 +0000145
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000146 pos = pci_read_config8(dev, pos + PCI_CAP_LIST_NEXT);
Eric Biederman03acab62004-10-14 21:25:53 +0000147 }
148 return 0;
149}
150
Myles Watson29cc9ed2009-07-02 18:56:24 +0000151/**
152 * Given a device, and a capability type, return the next matching
153 * capability. Always start at the head of the list.
154 *
155 * @param dev Pointer to the device structure.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000156 * @param cap PCI_CAP_LIST_ID of the PCI capability we're looking for.
157 * @return The next matching capability.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000158 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000159unsigned pci_find_capability(device_t dev, unsigned cap)
160{
161 return pci_find_next_capability(dev, cap, 0);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000162}
163
Myles Watson29cc9ed2009-07-02 18:56:24 +0000164/**
165 * Given a device and register, read the size of the BAR for that register.
166 *
167 * @param dev Pointer to the device structure.
168 * @param index Address of the PCI configuration register.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000169 * @return TODO
Eric Biederman8ca8d762003-04-22 19:02:15 +0000170 */
Eric Biederman03acab62004-10-14 21:25:53 +0000171struct resource *pci_get_resource(struct device *dev, unsigned long index)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000172{
Eric Biederman5cd81732004-03-11 15:01:31 +0000173 struct resource *resource;
Eric Biederman03acab62004-10-14 21:25:53 +0000174 unsigned long value, attr;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000175 resource_t moving, limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000176
Myles Watson29cc9ed2009-07-02 18:56:24 +0000177 /* Initialize the resources to nothing. */
Eric Biederman03acab62004-10-14 21:25:53 +0000178 resource = new_resource(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000179
Myles Watson29cc9ed2009-07-02 18:56:24 +0000180 /* Get the initial value. */
Eric Biederman03acab62004-10-14 21:25:53 +0000181 value = pci_read_config32(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000182
Myles Watson29cc9ed2009-07-02 18:56:24 +0000183 /* See which bits move. */
Eric Biederman03acab62004-10-14 21:25:53 +0000184 moving = pci_moving_config32(dev, index);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000185
Myles Watson29cc9ed2009-07-02 18:56:24 +0000186 /* Initialize attr to the bits that do not move. */
Eric Biederman03acab62004-10-14 21:25:53 +0000187 attr = value & ~moving;
188
Myles Watson29cc9ed2009-07-02 18:56:24 +0000189 /* If it is a 64bit resource look at the high half as well. */
Eric Biederman03acab62004-10-14 21:25:53 +0000190 if (((attr & PCI_BASE_ADDRESS_SPACE_IO) == 0) &&
Myles Watson29cc9ed2009-07-02 18:56:24 +0000191 ((attr & PCI_BASE_ADDRESS_MEM_LIMIT_MASK) ==
192 PCI_BASE_ADDRESS_MEM_LIMIT_64)) {
193 /* Find the high bits that move. */
194 moving |=
195 ((resource_t) pci_moving_config32(dev, index + 4)) << 32;
Eric Biederman03acab62004-10-14 21:25:53 +0000196 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000197
Myles Watson032a9652009-05-11 22:24:53 +0000198 /* Find the resource constraints.
Eric Biederman03acab62004-10-14 21:25:53 +0000199 * Start by finding the bits that move. From there:
200 * - Size is the least significant bit of the bits that move.
201 * - Limit is all of the bits that move plus all of the lower bits.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000202 * See PCI Spec 6.2.5.1.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000203 */
Eric Biederman03acab62004-10-14 21:25:53 +0000204 limit = 0;
205 if (moving) {
206 resource->size = 1;
207 resource->align = resource->gran = 0;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000208 while (!(moving & resource->size)) {
Eric Biederman03acab62004-10-14 21:25:53 +0000209 resource->size <<= 1;
210 resource->align += 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000211 resource->gran += 1;
Eric Biederman03acab62004-10-14 21:25:53 +0000212 }
213 resource->limit = limit = moving | (resource->size - 1);
Nico Huber8193b062015-10-21 15:43:41 +0200214
215 if (pci_base_address_is_memory_space(attr)) {
216 /* Page-align to allow individual mapping of devices. */
217 if (resource->align < 12)
218 resource->align = 12;
219 }
Eric Biederman03acab62004-10-14 21:25:53 +0000220 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000221
Uwe Hermanne4870472010-11-04 23:23:47 +0000222 /*
223 * Some broken hardware has read-only registers that do not
Eric Biederman03acab62004-10-14 21:25:53 +0000224 * really size correctly.
Uwe Hermanne4870472010-11-04 23:23:47 +0000225 *
226 * Example: the Acer M7229 has BARs 1-4 normally read-only,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000227 * so BAR1 at offset 0x10 reads 0x1f1. If you size that register
Uwe Hermanne4870472010-11-04 23:23:47 +0000228 * by writing 0xffffffff to it, it will read back as 0x1f1 -- which
229 * is a violation of the spec.
230 *
231 * We catch this case and ignore it by observing which bits move.
232 *
233 * This also catches the common case of unimplemented registers
Eric Biederman03acab62004-10-14 21:25:53 +0000234 * that always read back as 0.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000235 */
Eric Biederman03acab62004-10-14 21:25:53 +0000236 if (moving == 0) {
237 if (value != 0) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000238 printk(BIOS_DEBUG, "%s register %02lx(%08lx), "
239 "read-only ignoring it\n",
240 dev_path(dev), index, value);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000241 }
242 resource->flags = 0;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000243 } else if (attr & PCI_BASE_ADDRESS_SPACE_IO) {
244 /* An I/O mapped base address. */
Eric Biederman03acab62004-10-14 21:25:53 +0000245 attr &= PCI_BASE_ADDRESS_IO_ATTR_MASK;
Eric Biederman5cd81732004-03-11 15:01:31 +0000246 resource->flags |= IORESOURCE_IO;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000247 /* I don't want to deal with 32bit I/O resources. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000248 resource->limit = 0xffff;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000249 } else {
250 /* A Memory mapped base address. */
Eric Biederman03acab62004-10-14 21:25:53 +0000251 attr &= PCI_BASE_ADDRESS_MEM_ATTR_MASK;
Eric Biederman5cd81732004-03-11 15:01:31 +0000252 resource->flags |= IORESOURCE_MEM;
Uwe Hermanne4870472010-11-04 23:23:47 +0000253 if (attr & PCI_BASE_ADDRESS_MEM_PREFETCH)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000254 resource->flags |= IORESOURCE_PREFETCH;
Eric Biederman03acab62004-10-14 21:25:53 +0000255 attr &= PCI_BASE_ADDRESS_MEM_LIMIT_MASK;
256 if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_32) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000257 /* 32bit limit. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000258 resource->limit = 0xffffffffUL;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000259 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_1M) {
260 /* 1MB limit. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000261 resource->limit = 0x000fffffUL;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000262 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_64) {
263 /* 64bit limit. */
Eric Biederman03acab62004-10-14 21:25:53 +0000264 resource->limit = 0xffffffffffffffffULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000265 resource->flags |= IORESOURCE_PCI64;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000266 } else {
267 /* Invalid value. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000268 printk(BIOS_ERR, "Broken BAR with value %lx\n", attr);
269 printk(BIOS_ERR, " on dev %s at index %02lx\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000270 dev_path(dev), index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000271 resource->flags = 0;
272 }
273 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000274
Myles Watson29cc9ed2009-07-02 18:56:24 +0000275 /* Don't let the limit exceed which bits can move. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000276 if (resource->limit > limit)
Eric Biederman03acab62004-10-14 21:25:53 +0000277 resource->limit = limit;
Eric Biederman03acab62004-10-14 21:25:53 +0000278
Eric Biederman5cd81732004-03-11 15:01:31 +0000279 return resource;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000280}
281
Myles Watson29cc9ed2009-07-02 18:56:24 +0000282/**
283 * Given a device and an index, read the size of the BAR for that register.
284 *
285 * @param dev Pointer to the device structure.
286 * @param index Address of the PCI configuration register.
287 */
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000288static void pci_get_rom_resource(struct device *dev, unsigned long index)
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000289{
290 struct resource *resource;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000291 unsigned long value;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000292 resource_t moving;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000293
Myles Watson29cc9ed2009-07-02 18:56:24 +0000294 /* Initialize the resources to nothing. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000295 resource = new_resource(dev, index);
296
Myles Watson29cc9ed2009-07-02 18:56:24 +0000297 /* Get the initial value. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000298 value = pci_read_config32(dev, index);
299
Myles Watson29cc9ed2009-07-02 18:56:24 +0000300 /* See which bits move. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000301 moving = pci_moving_config32(dev, index);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000302
303 /* Clear the Enable bit. */
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000304 moving = moving & ~PCI_ROM_ADDRESS_ENABLE;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000305
Myles Watson032a9652009-05-11 22:24:53 +0000306 /* Find the resource constraints.
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000307 * Start by finding the bits that move. From there:
308 * - Size is the least significant bit of the bits that move.
309 * - Limit is all of the bits that move plus all of the lower bits.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000310 * See PCI Spec 6.2.5.1.
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000311 */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000312 if (moving) {
313 resource->size = 1;
314 resource->align = resource->gran = 0;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000315 while (!(moving & resource->size)) {
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000316 resource->size <<= 1;
317 resource->align += 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000318 resource->gran += 1;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000319 }
Patrick Georgi16cdbb22009-04-21 20:14:31 +0000320 resource->limit = moving | (resource->size - 1);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000321 resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY;
322 } else {
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000323 if (value != 0) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000324 printk(BIOS_DEBUG, "%s register %02lx(%08lx), "
325 "read-only ignoring it\n",
326 dev_path(dev), index, value);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000327 }
328 resource->flags = 0;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000329 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000330 compact_resources(dev);
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000331}
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000332
Myles Watson29cc9ed2009-07-02 18:56:24 +0000333/**
334 * Read the base address registers for a given device.
335 *
336 * @param dev Pointer to the dev structure.
337 * @param howmany How many registers to read (6 for device, 2 for bridge).
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000338 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000339static void pci_read_bases(struct device *dev, unsigned int howmany)
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000340{
341 unsigned long index;
342
Myles Watson29cc9ed2009-07-02 18:56:24 +0000343 for (index = PCI_BASE_ADDRESS_0;
344 (index < PCI_BASE_ADDRESS_0 + (howmany << 2));) {
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000345 struct resource *resource;
346 resource = pci_get_resource(dev, index);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000347 index += (resource->flags & IORESOURCE_PCI64) ? 8 : 4;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000348 }
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000349
350 compact_resources(dev);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000351}
352
Myles Watson29cc9ed2009-07-02 18:56:24 +0000353static void pci_record_bridge_resource(struct device *dev, resource_t moving,
354 unsigned index, unsigned long type)
Eric Biederman03acab62004-10-14 21:25:53 +0000355{
Eric Biederman03acab62004-10-14 21:25:53 +0000356 struct resource *resource;
Uwe Hermanne4870472010-11-04 23:23:47 +0000357 unsigned long gran;
358 resource_t step;
359
Myles Watson29cc9ed2009-07-02 18:56:24 +0000360 resource = NULL;
Uwe Hermanne4870472010-11-04 23:23:47 +0000361
362 if (!moving)
363 return;
364
365 /* Initialize the constraints on the current bus. */
366 resource = new_resource(dev, index);
367 resource->size = 0;
368 gran = 0;
369 step = 1;
370 while ((moving & step) == 0) {
371 gran += 1;
372 step <<= 1;
Eric Biederman03acab62004-10-14 21:25:53 +0000373 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000374 resource->gran = gran;
375 resource->align = gran;
376 resource->limit = moving | (step - 1);
377 resource->flags = type | IORESOURCE_PCI_BRIDGE |
378 IORESOURCE_BRIDGE;
Eric Biederman03acab62004-10-14 21:25:53 +0000379}
380
Eric Biederman8ca8d762003-04-22 19:02:15 +0000381static void pci_bridge_read_bases(struct device *dev)
382{
Eric Biederman03acab62004-10-14 21:25:53 +0000383 resource_t moving_base, moving_limit, moving;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000384
Myles Watson29cc9ed2009-07-02 18:56:24 +0000385 /* See if the bridge I/O resources are implemented. */
386 moving_base = ((u32) pci_moving_config8(dev, PCI_IO_BASE)) << 8;
387 moving_base |=
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000388 ((u32) pci_moving_config16(dev, PCI_IO_BASE_UPPER16)) << 16;
Eric Biederman03acab62004-10-14 21:25:53 +0000389
Myles Watson29cc9ed2009-07-02 18:56:24 +0000390 moving_limit = ((u32) pci_moving_config8(dev, PCI_IO_LIMIT)) << 8;
391 moving_limit |=
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000392 ((u32) pci_moving_config16(dev, PCI_IO_LIMIT_UPPER16)) << 16;
Eric Biederman03acab62004-10-14 21:25:53 +0000393
394 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000395
Myles Watson29cc9ed2009-07-02 18:56:24 +0000396 /* Initialize the I/O space constraints on the current bus. */
397 pci_record_bridge_resource(dev, moving, PCI_IO_BASE, IORESOURCE_IO);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000398
Myles Watson29cc9ed2009-07-02 18:56:24 +0000399 /* See if the bridge prefmem resources are implemented. */
400 moving_base =
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000401 ((resource_t) pci_moving_config16(dev, PCI_PREF_MEMORY_BASE)) << 16;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000402 moving_base |=
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000403 ((resource_t) pci_moving_config32(dev, PCI_PREF_BASE_UPPER32)) << 32;
Eric Biederman03acab62004-10-14 21:25:53 +0000404
Myles Watson29cc9ed2009-07-02 18:56:24 +0000405 moving_limit =
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000406 ((resource_t) pci_moving_config16(dev, PCI_PREF_MEMORY_LIMIT)) << 16;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000407 moving_limit |=
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000408 ((resource_t) pci_moving_config32(dev, PCI_PREF_LIMIT_UPPER32)) << 32;
Myles Watson032a9652009-05-11 22:24:53 +0000409
Eric Biederman03acab62004-10-14 21:25:53 +0000410 moving = moving_base & moving_limit;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000411 /* Initialize the prefetchable memory constraints on the current bus. */
412 pci_record_bridge_resource(dev, moving, PCI_PREF_MEMORY_BASE,
413 IORESOURCE_MEM | IORESOURCE_PREFETCH);
Myles Watson032a9652009-05-11 22:24:53 +0000414
Myles Watson29cc9ed2009-07-02 18:56:24 +0000415 /* See if the bridge mem resources are implemented. */
416 moving_base = ((u32) pci_moving_config16(dev, PCI_MEMORY_BASE)) << 16;
417 moving_limit = ((u32) pci_moving_config16(dev, PCI_MEMORY_LIMIT)) << 16;
Eric Biederman03acab62004-10-14 21:25:53 +0000418
419 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000420
Myles Watson29cc9ed2009-07-02 18:56:24 +0000421 /* Initialize the memory resources on the current bus. */
422 pci_record_bridge_resource(dev, moving, PCI_MEMORY_BASE,
423 IORESOURCE_MEM);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000424
Eric Biederman5cd81732004-03-11 15:01:31 +0000425 compact_resources(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000426}
427
Eric Biederman5899fd82003-04-24 06:25:08 +0000428void pci_dev_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000429{
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000430 pci_read_bases(dev, 6);
431 pci_get_rom_resource(dev, PCI_ROM_ADDRESS);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000432}
433
Eric Biederman5899fd82003-04-24 06:25:08 +0000434void pci_bus_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000435{
Eric Biederman8ca8d762003-04-22 19:02:15 +0000436 pci_bridge_read_bases(dev);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000437 pci_read_bases(dev, 2);
438 pci_get_rom_resource(dev, PCI_ROM_ADDRESS1);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000439}
440
Myles Watson29cc9ed2009-07-02 18:56:24 +0000441void pci_domain_read_resources(struct device *dev)
442{
443 struct resource *res;
444
445 /* Initialize the system-wide I/O space constraints. */
446 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
447 res->limit = 0xffffUL;
448 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
449 IORESOURCE_ASSIGNED;
450
451 /* Initialize the system-wide memory resources constraints. */
452 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
453 res->limit = 0xffffffffULL;
454 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
455 IORESOURCE_ASSIGNED;
456}
457
Eric Biederman8ca8d762003-04-22 19:02:15 +0000458static void pci_set_resource(struct device *dev, struct resource *resource)
459{
Eric Biederman03acab62004-10-14 21:25:53 +0000460 resource_t base, end;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000461
Myles Watson29cc9ed2009-07-02 18:56:24 +0000462 /* Make certain the resource has actually been assigned a value. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000463 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000464 printk(BIOS_ERR, "ERROR: %s %02lx %s size: 0x%010llx not "
465 "assigned\n", dev_path(dev), resource->index,
466 resource_type(resource), resource->size);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000467 return;
468 }
469
Myles Watsoneb81a5b2009-11-05 20:06:19 +0000470 /* If this resource is fixed don't worry about it. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000471 if (resource->flags & IORESOURCE_FIXED)
Myles Watsoneb81a5b2009-11-05 20:06:19 +0000472 return;
Myles Watsoneb81a5b2009-11-05 20:06:19 +0000473
Myles Watson29cc9ed2009-07-02 18:56:24 +0000474 /* If I have already stored this resource don't worry about it. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000475 if (resource->flags & IORESOURCE_STORED)
Eric Biederman5cd81732004-03-11 15:01:31 +0000476 return;
Eric Biederman5cd81732004-03-11 15:01:31 +0000477
Myles Watson29cc9ed2009-07-02 18:56:24 +0000478 /* If the resource is subtractive don't worry about it. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000479 if (resource->flags & IORESOURCE_SUBTRACTIVE)
Eric Biederman03acab62004-10-14 21:25:53 +0000480 return;
Eric Biederman03acab62004-10-14 21:25:53 +0000481
Myles Watson29cc9ed2009-07-02 18:56:24 +0000482 /* Only handle PCI memory and I/O resources for now. */
483 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
Eric Biederman8ca8d762003-04-22 19:02:15 +0000484 return;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000485
Myles Watson29cc9ed2009-07-02 18:56:24 +0000486 /* Enable the resources in the command register. */
Eric Biederman03acab62004-10-14 21:25:53 +0000487 if (resource->size) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000488 if (resource->flags & IORESOURCE_MEM)
Eric Biederman03acab62004-10-14 21:25:53 +0000489 dev->command |= PCI_COMMAND_MEMORY;
Uwe Hermanne4870472010-11-04 23:23:47 +0000490 if (resource->flags & IORESOURCE_IO)
Eric Biederman03acab62004-10-14 21:25:53 +0000491 dev->command |= PCI_COMMAND_IO;
Uwe Hermanne4870472010-11-04 23:23:47 +0000492 if (resource->flags & IORESOURCE_PCI_BRIDGE)
Eric Biederman03acab62004-10-14 21:25:53 +0000493 dev->command |= PCI_COMMAND_MASTER;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000494 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000495
Myles Watson29cc9ed2009-07-02 18:56:24 +0000496 /* Get the base address. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000497 base = resource->base;
Eric Biederman5cd81732004-03-11 15:01:31 +0000498
Myles Watson29cc9ed2009-07-02 18:56:24 +0000499 /* Get the end. */
Eric Biederman03acab62004-10-14 21:25:53 +0000500 end = resource_end(resource);
Myles Watson032a9652009-05-11 22:24:53 +0000501
Myles Watson29cc9ed2009-07-02 18:56:24 +0000502 /* Now store the resource. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000503 resource->flags |= IORESOURCE_STORED;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000504
Uwe Hermanne4870472010-11-04 23:23:47 +0000505 /*
506 * PCI bridges have no enable bit. They are disabled if the base of
507 * the range is greater than the limit. If the size is zero, disable
Myles Watson29cc9ed2009-07-02 18:56:24 +0000508 * by setting the base = limit and end = limit - 2^gran.
509 */
510 if (resource->size == 0 && (resource->flags & IORESOURCE_PCI_BRIDGE)) {
511 base = resource->limit;
512 end = resource->limit - (1 << resource->gran);
513 resource->base = base;
514 }
515
Eric Biederman8ca8d762003-04-22 19:02:15 +0000516 if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) {
Eric Biederman03acab62004-10-14 21:25:53 +0000517 unsigned long base_lo, base_hi;
Uwe Hermanne4870472010-11-04 23:23:47 +0000518
519 /*
520 * Some chipsets allow us to set/clear the I/O bit
521 * (e.g. VIA 82C686A). So set it to be safe.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000522 */
Eric Biederman03acab62004-10-14 21:25:53 +0000523 base_lo = base & 0xffffffff;
524 base_hi = (base >> 32) & 0xffffffff;
Uwe Hermanne4870472010-11-04 23:23:47 +0000525 if (resource->flags & IORESOURCE_IO)
Eric Biederman03acab62004-10-14 21:25:53 +0000526 base_lo |= PCI_BASE_ADDRESS_SPACE_IO;
Eric Biederman03acab62004-10-14 21:25:53 +0000527 pci_write_config32(dev, resource->index, base_lo);
Uwe Hermanne4870472010-11-04 23:23:47 +0000528 if (resource->flags & IORESOURCE_PCI64)
Eric Biederman03acab62004-10-14 21:25:53 +0000529 pci_write_config32(dev, resource->index + 4, base_hi);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000530 } else if (resource->index == PCI_IO_BASE) {
531 /* Set the I/O ranges. */
532 pci_write_config8(dev, PCI_IO_BASE, base >> 8);
Eric Biederman03acab62004-10-14 21:25:53 +0000533 pci_write_config16(dev, PCI_IO_BASE_UPPER16, base >> 16);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000534 pci_write_config8(dev, PCI_IO_LIMIT, end >> 8);
Eric Biederman03acab62004-10-14 21:25:53 +0000535 pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, end >> 16);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000536 } else if (resource->index == PCI_MEMORY_BASE) {
537 /* Set the memory range. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000538 pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16);
Eric Biederman03acab62004-10-14 21:25:53 +0000539 pci_write_config16(dev, PCI_MEMORY_LIMIT, end >> 16);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000540 } else if (resource->index == PCI_PREF_MEMORY_BASE) {
541 /* Set the prefetchable memory range. */
Eric Biederman03acab62004-10-14 21:25:53 +0000542 pci_write_config16(dev, PCI_PREF_MEMORY_BASE, base >> 16);
543 pci_write_config32(dev, PCI_PREF_BASE_UPPER32, base >> 32);
544 pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, end >> 16);
545 pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, end >> 32);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000546 } else {
547 /* Don't let me think I stored the resource. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000548 resource->flags &= ~IORESOURCE_STORED;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000549 printk(BIOS_ERR, "ERROR: invalid resource->index %lx\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000550 resource->index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000551 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000552
Eric Biederman03acab62004-10-14 21:25:53 +0000553 report_resource_stored(dev, resource, "");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000554}
555
Eric Biederman5899fd82003-04-24 06:25:08 +0000556void pci_dev_set_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000557{
Myles Watsonc25cc112010-05-21 14:33:48 +0000558 struct resource *res;
Myles Watson894a3472010-06-09 22:41:35 +0000559 struct bus *bus;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000560 u8 line;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000561
Uwe Hermanne4870472010-11-04 23:23:47 +0000562 for (res = dev->resource_list; res; res = res->next)
Myles Watsonc25cc112010-05-21 14:33:48 +0000563 pci_set_resource(dev, res);
Uwe Hermanne4870472010-11-04 23:23:47 +0000564
Myles Watson894a3472010-06-09 22:41:35 +0000565 for (bus = dev->link_list; bus; bus = bus->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000566 if (bus->children)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000567 assign_resources(bus);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000568 }
569
Myles Watson29cc9ed2009-07-02 18:56:24 +0000570 /* Set a default latency timer. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000571 pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000572
Myles Watson29cc9ed2009-07-02 18:56:24 +0000573 /* Set a default secondary latency timer. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000574 if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE)
Eric Biederman7a5416a2003-06-12 19:23:51 +0000575 pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000576
Myles Watson29cc9ed2009-07-02 18:56:24 +0000577 /* Zero the IRQ settings. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000578 line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
Uwe Hermanne4870472010-11-04 23:23:47 +0000579 if (line)
Eric Biederman7a5416a2003-06-12 19:23:51 +0000580 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
Uwe Hermanne4870472010-11-04 23:23:47 +0000581
Myles Watson29cc9ed2009-07-02 18:56:24 +0000582 /* Set the cache line size, so far 64 bytes is good for everyone. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000583 pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000584}
585
Eric Biedermane9a271e32003-09-02 03:36:25 +0000586void pci_dev_enable_resources(struct device *dev)
587{
Eric Biedermana9e632c2004-11-18 22:38:08 +0000588 const struct pci_operations *ops;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000589 u16 command;
Eric Biederman03acab62004-10-14 21:25:53 +0000590
Uwe Hermanne4870472010-11-04 23:23:47 +0000591 /* Set the subsystem vendor and device ID for mainboard devices. */
Eric Biederman03acab62004-10-14 21:25:53 +0000592 ops = ops_pci(dev);
Eric Biedermandbec2d42004-10-21 10:44:08 +0000593 if (dev->on_mainboard && ops && ops->set_subsystem) {
Duncan Laurie7e1c83e2013-08-09 07:55:10 -0700594 if (CONFIG_SUBSYSTEM_VENDOR_ID)
595 dev->subsystem_vendor = CONFIG_SUBSYSTEM_VENDOR_ID;
596 if (CONFIG_SUBSYSTEM_DEVICE_ID)
597 dev->subsystem_device = CONFIG_SUBSYSTEM_DEVICE_ID;
Sven Schnelle91321022011-03-01 19:58:47 +0000598 printk(BIOS_DEBUG, "%s subsystem <- %04x/%04x\n",
599 dev_path(dev), dev->subsystem_vendor,
600 dev->subsystem_device);
601 ops->set_subsystem(dev, dev->subsystem_vendor,
602 dev->subsystem_device);
Eric Biederman03acab62004-10-14 21:25:53 +0000603 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000604 command = pci_read_config16(dev, PCI_COMMAND);
605 command |= dev->command;
Uwe Hermanne4870472010-11-04 23:23:47 +0000606
Myles Watson29cc9ed2009-07-02 18:56:24 +0000607 /* v3 has
608 * command |= (PCI_COMMAND_PARITY + PCI_COMMAND_SERR); // Error check.
609 */
Uwe Hermanne4870472010-11-04 23:23:47 +0000610
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000611 printk(BIOS_DEBUG, "%s cmd <- %02x\n", dev_path(dev), command);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000612 pci_write_config16(dev, PCI_COMMAND, command);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000613}
614
615void pci_bus_enable_resources(struct device *dev)
616{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000617 u16 ctrl;
618
Uwe Hermanne4870472010-11-04 23:23:47 +0000619 /*
620 * Enable I/O in command register if there is VGA card
Myles Watson29cc9ed2009-07-02 18:56:24 +0000621 * connected with (even it does not claim I/O resource).
622 */
Myles Watson894a3472010-06-09 22:41:35 +0000623 if (dev->link_list->bridge_ctrl & PCI_BRIDGE_CTL_VGA)
Li-Ta Lo515f6c72005-01-11 22:48:54 +0000624 dev->command |= PCI_COMMAND_IO;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000625 ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
Myles Watson894a3472010-06-09 22:41:35 +0000626 ctrl |= dev->link_list->bridge_ctrl;
Uwe Hermanne4870472010-11-04 23:23:47 +0000627 ctrl |= (PCI_BRIDGE_CTL_PARITY + PCI_BRIDGE_CTL_SERR); /* Error check. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000628 printk(BIOS_DEBUG, "%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000629 pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
630
631 pci_dev_enable_resources(dev);
632}
633
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000634void pci_bus_reset(struct bus *bus)
635{
Uwe Hermanne4870472010-11-04 23:23:47 +0000636 u16 ctl;
637
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000638 ctl = pci_read_config16(bus->dev, PCI_BRIDGE_CONTROL);
639 ctl |= PCI_BRIDGE_CTL_BUS_RESET;
640 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
641 mdelay(10);
Uwe Hermanne4870472010-11-04 23:23:47 +0000642
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000643 ctl &= ~PCI_BRIDGE_CTL_BUS_RESET;
644 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
645 delay(1);
646}
647
Myles Watson29cc9ed2009-07-02 18:56:24 +0000648void pci_dev_set_subsystem(struct device *dev, unsigned vendor, unsigned device)
Eric Biederman03acab62004-10-14 21:25:53 +0000649{
Myles Watson032a9652009-05-11 22:24:53 +0000650 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000651 ((device & 0xffff) << 16) | (vendor & 0xffff));
Eric Biederman03acab62004-10-14 21:25:53 +0000652}
653
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300654#if CONFIG_VGA_ROM_RUN
655static int should_run_oprom(struct device *dev)
656{
657 static int should_run = -1;
658
659 if (should_run >= 0)
660 return should_run;
661
Kyösti Mälkki9ab1c102013-12-22 00:22:49 +0200662 /* Don't run VGA option ROMs, unless we have to print
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300663 * something on the screen before the kernel is loaded.
664 */
Kyösti Mälkki9ab1c102013-12-22 00:22:49 +0200665 should_run = !IS_ENABLED(CONFIG_BOOTMODE_STRAPS) ||
666 developer_mode_enabled() || recovery_mode_enabled();
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300667
Kyösti Mälkki9ab1c102013-12-22 00:22:49 +0200668#if CONFIG_CHROMEOS
669 if (!should_run)
670 should_run = vboot_wants_oprom();
671#endif
672 if (!should_run)
673 printk(BIOS_DEBUG, "Not running VGA Option ROM\n");
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300674 return should_run;
675}
676
677static int should_load_oprom(struct device *dev)
678{
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300679 /* If S3_VGA_ROM_RUN is disabled, skip running VGA option
680 * ROMs when coming out of an S3 resume.
681 */
Kyösti Mälkki58ceb002014-06-20 06:21:01 +0300682 if (!IS_ENABLED(CONFIG_S3_VGA_ROM_RUN) && acpi_is_wakeup_s3() &&
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300683 ((dev->class >> 8) == PCI_CLASS_DISPLAY_VGA))
684 return 0;
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300685 if (IS_ENABLED(CONFIG_ALWAYS_LOAD_OPROM))
686 return 1;
687 if (should_run_oprom(dev))
688 return 1;
689
690 return 0;
691}
692#endif /* CONFIG_VGA_ROM_RUN */
693
Uwe Hermanne4870472010-11-04 23:23:47 +0000694/** Default handler: only runs the relevant PCI BIOS. */
Li-Ta Lo883b8792005-01-10 23:16:22 +0000695void pci_dev_init(struct device *dev)
696{
Vladimir Serbinenkob32816e2013-12-20 17:47:19 +0100697#if CONFIG_VGA_ROM_RUN
Li-Ta Lo883b8792005-01-10 23:16:22 +0000698 struct rom_header *rom, *ram;
699
Vladimir Serbinenkob32816e2013-12-20 17:47:19 +0100700 /* Only execute VGA ROMs. */
701 if (((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA))
Myles Watson17aeeca2009-10-07 18:41:08 +0000702 return;
Roman Kononov778a42b2007-04-06 18:34:39 +0000703
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300704 if (!should_load_oprom(dev))
Stefan Reinauer74a0efe2012-03-30 17:10:49 -0700705 return;
Aaron Durbince872cb2013-03-28 15:59:19 -0500706
707 rom = pci_rom_probe(dev);
708 if (rom == NULL)
709 return;
710
711 ram = pci_rom_load(dev, rom);
712 if (ram == NULL)
713 return;
714
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300715 if (!should_run_oprom(dev))
716 return;
717
Stefan Reinauerd98cf5b2008-08-01 11:25:41 +0000718 run_bios(dev, (unsigned long)ram);
Kyösti Mälkkiab56b3b2013-11-28 16:44:51 +0200719 gfx_set_init_done(1);
720 printk(BIOS_DEBUG, "VGA Option ROM was run\n");
Vladimir Serbinenkob32816e2013-12-20 17:47:19 +0100721#endif /* CONFIG_VGA_ROM_RUN */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000722}
Li-Ta Lo883b8792005-01-10 23:16:22 +0000723
Li-Ta Loe5266692004-03-23 21:28:05 +0000724/** Default device operation for PCI devices */
Eric Biedermana9e632c2004-11-18 22:38:08 +0000725static struct pci_operations pci_dev_ops_pci = {
Eric Biederman03acab62004-10-14 21:25:53 +0000726 .set_subsystem = pci_dev_set_subsystem,
727};
728
Eric Biederman8ca8d762003-04-22 19:02:15 +0000729struct device_operations default_pci_ops_dev = {
Uwe Hermanne4870472010-11-04 23:23:47 +0000730 .read_resources = pci_dev_read_resources,
731 .set_resources = pci_dev_set_resources,
Eric Biedermane9a271e32003-09-02 03:36:25 +0000732 .enable_resources = pci_dev_enable_resources,
Uwe Hermanne4870472010-11-04 23:23:47 +0000733 .init = pci_dev_init,
734 .scan_bus = 0,
735 .enable = 0,
736 .ops_pci = &pci_dev_ops_pci,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000737};
Li-Ta Loe5266692004-03-23 21:28:05 +0000738
739/** Default device operations for PCI bridges */
Eric Biedermana9e632c2004-11-18 22:38:08 +0000740static struct pci_operations pci_bus_ops_pci = {
Eric Biederman03acab62004-10-14 21:25:53 +0000741 .set_subsystem = 0,
742};
Li-Ta Lo883b8792005-01-10 23:16:22 +0000743
Eric Biederman8ca8d762003-04-22 19:02:15 +0000744struct device_operations default_pci_ops_bus = {
Uwe Hermanne4870472010-11-04 23:23:47 +0000745 .read_resources = pci_bus_read_resources,
746 .set_resources = pci_dev_set_resources,
Eric Biedermane9a271e32003-09-02 03:36:25 +0000747 .enable_resources = pci_bus_enable_resources,
Uwe Hermanne4870472010-11-04 23:23:47 +0000748 .init = 0,
749 .scan_bus = pci_scan_bridge,
750 .enable = 0,
751 .reset_bus = pci_bus_reset,
752 .ops_pci = &pci_bus_ops_pci,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000753};
Li-Ta Loe5266692004-03-23 21:28:05 +0000754
755/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000756 * Detect the type of downstream bridge.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000757 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000758 * This function is a heuristic to detect which type of bus is downstream
759 * of a PCI-to-PCI bridge. This functions by looking for various capability
760 * blocks to figure out the type of downstream bridge. PCI-X, PCI-E, and
761 * Hypertransport all seem to have appropriate capabilities.
Myles Watson032a9652009-05-11 22:24:53 +0000762 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000763 * When only a PCI-Express capability is found the type is examined to see
764 * which type of bridge we have.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000765 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000766 * @param dev Pointer to the device structure of the bridge.
767 * @return Appropriate bridge operations.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000768 */
769static struct device_operations *get_pci_bridge_ops(device_t dev)
770{
Patrick Georgie1667822012-05-05 15:29:32 +0200771#if CONFIG_PCIX_PLUGIN_SUPPORT
Ronald G. Minnich78a16672012-11-29 16:28:21 -0800772 unsigned int pcixpos;
773 pcixpos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
774 if (pcixpos) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000775 printk(BIOS_DEBUG, "%s subordinate bus PCI-X\n", dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000776 return &default_pcix_ops_bus;
777 }
778#endif
Patrick Georgie1667822012-05-05 15:29:32 +0200779#if CONFIG_HYPERTRANSPORT_PLUGIN_SUPPORT
Ronald G. Minnich78a16672012-11-29 16:28:21 -0800780 unsigned int htpos = 0;
781 while ((htpos = pci_find_next_capability(dev, PCI_CAP_ID_HT, htpos))) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000782 u16 flags;
Ronald G. Minnich78a16672012-11-29 16:28:21 -0800783 flags = pci_read_config16(dev, htpos + PCI_CAP_FLAGS);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000784 if ((flags >> 13) == 1) {
785 /* Host or Secondary Interface */
Uwe Hermanne4870472010-11-04 23:23:47 +0000786 printk(BIOS_DEBUG, "%s subordinate bus HT\n",
787 dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000788 return &default_ht_ops_bus;
789 }
790 }
791#endif
Patrick Georgie1667822012-05-05 15:29:32 +0200792#if CONFIG_PCIEXP_PLUGIN_SUPPORT
Ronald G. Minnich78a16672012-11-29 16:28:21 -0800793 unsigned int pciexpos;
794 pciexpos = pci_find_capability(dev, PCI_CAP_ID_PCIE);
795 if (pciexpos) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000796 u16 flags;
Ronald G. Minnich78a16672012-11-29 16:28:21 -0800797 flags = pci_read_config16(dev, pciexpos + PCI_EXP_FLAGS);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000798 switch ((flags & PCI_EXP_FLAGS_TYPE) >> 4) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000799 case PCI_EXP_TYPE_ROOT_PORT:
800 case PCI_EXP_TYPE_UPSTREAM:
801 case PCI_EXP_TYPE_DOWNSTREAM:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000802 printk(BIOS_DEBUG, "%s subordinate bus PCI Express\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000803 dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000804 return &default_pciexp_ops_bus;
805 case PCI_EXP_TYPE_PCI_BRIDGE:
Uwe Hermanne4870472010-11-04 23:23:47 +0000806 printk(BIOS_DEBUG, "%s subordinate PCI\n",
807 dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000808 return &default_pci_ops_bus;
809 default:
810 break;
811 }
812 }
813#endif
814 return &default_pci_ops_bus;
815}
816
817/**
Vadim Bendebury8049fc92012-04-24 12:53:19 -0700818 * Check if a device id matches a PCI driver entry.
819 *
820 * The driver entry can either point at a zero terminated array of acceptable
821 * device IDs, or include a single device ID.
822 *
Martin Roth98b698c2015-01-06 21:02:52 -0700823 * @param driver pointer to the PCI driver entry being checked
824 * @param device_id PCI device ID of the device being matched
Vadim Bendebury8049fc92012-04-24 12:53:19 -0700825 */
826static int device_id_match(struct pci_driver *driver, unsigned short device_id)
827{
828 if (driver->devices) {
829 unsigned short check_id;
830 const unsigned short *device_list = driver->devices;
831 while ((check_id = *device_list++) != 0)
832 if (check_id == device_id)
833 return 1;
834 }
835
836 return (driver->device == device_id);
837}
838
839/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000840 * Set up PCI device operation.
841 *
842 * Check if it already has a driver. If not, use find_device_operations(),
843 * or set to a default based on type.
Li-Ta Loe5266692004-03-23 21:28:05 +0000844 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000845 * @param dev Pointer to the device whose pci_ops you want to set.
Li-Ta Loe5266692004-03-23 21:28:05 +0000846 * @see pci_drivers
847 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000848static void set_pci_ops(struct device *dev)
849{
850 struct pci_driver *driver;
Li-Ta Loe5266692004-03-23 21:28:05 +0000851
Uwe Hermanne4870472010-11-04 23:23:47 +0000852 if (dev->ops)
853 return;
854
855 /*
856 * Look through the list of setup drivers and find one for
Myles Watson29cc9ed2009-07-02 18:56:24 +0000857 * this PCI device.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000858 */
Aaron Durbin03758152015-09-03 17:23:08 -0500859 for (driver = &_pci_drivers[0]; driver != &_epci_drivers[0]; driver++) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000860 if ((driver->vendor == dev->vendor) &&
Vadim Bendebury8049fc92012-04-24 12:53:19 -0700861 device_id_match(driver, dev->device)) {
Uwe Hermann312673c2009-10-27 21:49:33 +0000862 dev->ops = (struct device_operations *)driver->ops;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000863 printk(BIOS_SPEW, "%s [%04x/%04x] %sops\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000864 dev_path(dev), driver->vendor, driver->device,
865 (driver->ops->scan_bus ? "bus " : ""));
Eric Biederman5899fd82003-04-24 06:25:08 +0000866 return;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000867 }
868 }
Li-Ta Loe5266692004-03-23 21:28:05 +0000869
Uwe Hermanne4870472010-11-04 23:23:47 +0000870 /* If I don't have a specific driver use the default operations. */
871 switch (dev->hdr_type & 0x7f) { /* Header type */
872 case PCI_HEADER_TYPE_NORMAL:
Eric Biederman8ca8d762003-04-22 19:02:15 +0000873 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
874 goto bad;
875 dev->ops = &default_pci_ops_dev;
876 break;
877 case PCI_HEADER_TYPE_BRIDGE:
878 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
879 goto bad;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000880 dev->ops = get_pci_bridge_ops(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000881 break;
Patrick Georgie1667822012-05-05 15:29:32 +0200882#if CONFIG_CARDBUS_PLUGIN_SUPPORT
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000883 case PCI_HEADER_TYPE_CARDBUS:
884 dev->ops = &default_cardbus_ops_bus;
885 break;
886#endif
Uwe Hermanne4870472010-11-04 23:23:47 +0000887default:
888bad:
Li-Ta Lo69c5a902004-04-29 20:08:54 +0000889 if (dev->enabled) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000890 printk(BIOS_ERR, "%s [%04x/%04x/%06x] has unknown "
891 "header type %02x, ignoring.\n", dev_path(dev),
892 dev->vendor, dev->device,
893 dev->class >> 8, dev->hdr_type);
Eric Biederman83b991a2003-10-11 06:20:25 +0000894 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000895 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000896}
897
898/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000899 * See if we have already allocated a device structure for a given devfn.
Li-Ta Loe5266692004-03-23 21:28:05 +0000900 *
901 * Given a linked list of PCI device structures and a devfn number, find the
Li-Ta Lo3a812852004-12-03 22:39:34 +0000902 * device structure correspond to the devfn, if present. This function also
903 * removes the device structure from the linked list.
Li-Ta Loe5266692004-03-23 21:28:05 +0000904 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000905 * @param list The device structure list.
906 * @param devfn A device/function number.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000907 * @return Pointer to the device structure found or NULL if we have not
Li-Ta Lo3a812852004-12-03 22:39:34 +0000908 * allocated a device for this devfn yet.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000909 */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000910static struct device *pci_scan_get_dev(struct device **list, unsigned int devfn)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000911{
Eric Biedermanb78c1972004-10-14 20:54:17 +0000912 struct device *dev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000913
Eric Biedermanb78c1972004-10-14 20:54:17 +0000914 dev = 0;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000915 for (; *list; list = &(*list)->sibling) {
Eric Biedermanad1b35a2003-10-14 02:36:51 +0000916 if ((*list)->path.type != DEVICE_PATH_PCI) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000917 printk(BIOS_ERR, "child %s not a PCI device\n",
918 dev_path(*list));
Eric Biedermanad1b35a2003-10-14 02:36:51 +0000919 continue;
920 }
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000921 if ((*list)->path.pci.devfn == devfn) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000922 /* Unlink from the list. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000923 dev = *list;
924 *list = (*list)->sibling;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000925 dev->sibling = NULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000926 break;
927 }
928 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000929
Uwe Hermanne4870472010-11-04 23:23:47 +0000930 /*
931 * Just like alloc_dev() add the device to the list of devices on the
Myles Watson29cc9ed2009-07-02 18:56:24 +0000932 * bus. When the list of devices was formed we removed all of the
933 * parents children, and now we are interleaving static and dynamic
934 * devices in order on the bus.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000935 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000936 if (dev) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000937 struct device *child;
Uwe Hermanne4870472010-11-04 23:23:47 +0000938
Myles Watson29cc9ed2009-07-02 18:56:24 +0000939 /* Find the last child of our parent. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000940 for (child = dev->bus->children; child && child->sibling;)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000941 child = child->sibling;
Uwe Hermanne4870472010-11-04 23:23:47 +0000942
Myles Watson29cc9ed2009-07-02 18:56:24 +0000943 /* Place the device on the list of children of its parent. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000944 if (child)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000945 child->sibling = dev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000946 else
Eric Biedermane9a271e32003-09-02 03:36:25 +0000947 dev->bus->children = dev;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000948 }
949
Eric Biederman8ca8d762003-04-22 19:02:15 +0000950 return dev;
951}
952
Myles Watson032a9652009-05-11 22:24:53 +0000953/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000954 * Scan a PCI bus.
Li-Ta Loe5266692004-03-23 21:28:05 +0000955 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000956 * Determine the existence of a given PCI device. Allocate a new struct device
957 * if dev==NULL was passed in and the device exists in hardware.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000958 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000959 * @param dev Pointer to the dev structure.
960 * @param bus Pointer to the bus structure.
961 * @param devfn A device/function number to look at.
962 * @return The device structure for the device (if found), NULL otherwise.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000963 */
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000964device_t pci_probe_dev(device_t dev, struct bus *bus, unsigned devfn)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000965{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000966 u32 id, class;
967 u8 hdr_type;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000968
Myles Watson29cc9ed2009-07-02 18:56:24 +0000969 /* Detect if a device is present. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000970 if (!dev) {
971 struct device dummy;
Uwe Hermanne4870472010-11-04 23:23:47 +0000972
Myles Watson29cc9ed2009-07-02 18:56:24 +0000973 dummy.bus = bus;
974 dummy.path.type = DEVICE_PATH_PCI;
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000975 dummy.path.pci.devfn = devfn;
Uwe Hermanne4870472010-11-04 23:23:47 +0000976
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000977 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
Uwe Hermanne4870472010-11-04 23:23:47 +0000978 /*
979 * Have we found something? Some broken boards return 0 if a
980 * slot is empty, but the expected answer is 0xffffffff.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000981 */
Uwe Hermanne4870472010-11-04 23:23:47 +0000982 if (id == 0xffffffff)
Stefan Reinauer7355c752010-04-02 16:30:25 +0000983 return NULL;
Uwe Hermanne4870472010-11-04 23:23:47 +0000984
Stefan Reinauer7355c752010-04-02 16:30:25 +0000985 if ((id == 0x00000000) || (id == 0x0000ffff) ||
986 (id == 0xffff0000)) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000987 printk(BIOS_SPEW, "%s, bad id 0x%x\n",
988 dev_path(&dummy), id);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000989 return NULL;
990 }
991 dev = alloc_dev(bus, &dummy.path);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000992 } else {
Uwe Hermanne4870472010-11-04 23:23:47 +0000993 /*
994 * Enable/disable the device. Once we have found the device-
Myles Watson29cc9ed2009-07-02 18:56:24 +0000995 * specific operations this operations we will disable the
996 * device with those as well.
Myles Watson032a9652009-05-11 22:24:53 +0000997 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000998 * This is geared toward devices that have subfunctions
999 * that do not show up by default.
Myles Watson032a9652009-05-11 22:24:53 +00001000 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001001 * If a device is a stuff option on the motherboard
Myles Watson29cc9ed2009-07-02 18:56:24 +00001002 * it may be absent and enable_dev() must cope.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001003 */
Myles Watson29cc9ed2009-07-02 18:56:24 +00001004 /* Run the magic enable sequence for the device. */
Uwe Hermanne4870472010-11-04 23:23:47 +00001005 if (dev->chip_ops && dev->chip_ops->enable_dev)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001006 dev->chip_ops->enable_dev(dev);
Uwe Hermanne4870472010-11-04 23:23:47 +00001007
Myles Watson29cc9ed2009-07-02 18:56:24 +00001008 /* Now read the vendor and device ID. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001009 id = pci_read_config32(dev, PCI_VENDOR_ID);
Myles Watson032a9652009-05-11 22:24:53 +00001010
Uwe Hermanne4870472010-11-04 23:23:47 +00001011 /*
1012 * If the device does not have a PCI ID disable it. Possibly
Myles Watson29cc9ed2009-07-02 18:56:24 +00001013 * this is because we have already disabled the device. But
1014 * this also handles optional devices that may not always
1015 * show up.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001016 */
1017 /* If the chain is fully enumerated quit */
Myles Watson29cc9ed2009-07-02 18:56:24 +00001018 if ((id == 0xffffffff) || (id == 0x00000000) ||
1019 (id == 0x0000ffff) || (id == 0xffff0000)) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001020 if (dev->enabled) {
Uwe Hermanne4870472010-11-04 23:23:47 +00001021 printk(BIOS_INFO, "PCI: Static device %s not "
1022 "found, disabling it.\n", dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001023 dev->enabled = 0;
1024 }
1025 return dev;
1026 }
1027 }
Uwe Hermanne4870472010-11-04 23:23:47 +00001028
Myles Watson29cc9ed2009-07-02 18:56:24 +00001029 /* Read the rest of the PCI configuration information. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001030 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
1031 class = pci_read_config32(dev, PCI_CLASS_REVISION);
Myles Watson032a9652009-05-11 22:24:53 +00001032
Myles Watson29cc9ed2009-07-02 18:56:24 +00001033 /* Store the interesting information in the device structure. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001034 dev->vendor = id & 0xffff;
1035 dev->device = (id >> 16) & 0xffff;
1036 dev->hdr_type = hdr_type;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001037
1038 /* Class code, the upper 3 bytes of PCI_CLASS_REVISION. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001039 dev->class = class >> 8;
Myles Watson032a9652009-05-11 22:24:53 +00001040
Myles Watson29cc9ed2009-07-02 18:56:24 +00001041 /* Architectural/System devices always need to be bus masters. */
Uwe Hermanne4870472010-11-04 23:23:47 +00001042 if ((dev->class >> 16) == PCI_BASE_CLASS_SYSTEM)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001043 dev->command |= PCI_COMMAND_MASTER;
Uwe Hermanne4870472010-11-04 23:23:47 +00001044
1045 /*
1046 * Look at the vendor and device ID, or at least the header type and
Myles Watson29cc9ed2009-07-02 18:56:24 +00001047 * class and figure out which set of configuration methods to use.
1048 * Unless we already have some PCI ops.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001049 */
1050 set_pci_ops(dev);
1051
Myles Watson29cc9ed2009-07-02 18:56:24 +00001052 /* Now run the magic enable/disable sequence for the device. */
Uwe Hermanne4870472010-11-04 23:23:47 +00001053 if (dev->ops && dev->ops->enable)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001054 dev->ops->enable(dev);
Myles Watson032a9652009-05-11 22:24:53 +00001055
Myles Watson29cc9ed2009-07-02 18:56:24 +00001056 /* Display the device. */
Uwe Hermanne4870472010-11-04 23:23:47 +00001057 printk(BIOS_DEBUG, "%s [%04x/%04x] %s%s\n", dev_path(dev),
1058 dev->vendor, dev->device, dev->enabled ? "enabled" : "disabled",
1059 dev->ops ? "" : " No operations");
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001060
1061 return dev;
1062}
1063
Myles Watson032a9652009-05-11 22:24:53 +00001064/**
Kyösti Mälkkic73acdb2013-06-15 17:16:56 +03001065 * Test for match between romstage and ramstage device instance.
1066 *
1067 * @param dev Pointer to the device structure.
1068 * @param sdev Simple device model identifier, created with PCI_DEV().
1069 * @return Non-zero if bus:dev.fn of device matches.
1070 */
1071unsigned int pci_match_simple_dev(device_t dev, pci_devfn_t sdev)
1072{
1073 return dev->bus->secondary == PCI_DEV2SEGBUS(sdev) &&
1074 dev->path.pci.devfn == PCI_DEV2DEVFN(sdev);
1075}
1076
1077/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001078 * Scan a PCI bus.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001079 *
Li-Ta Loe5266692004-03-23 21:28:05 +00001080 * Determine the existence of devices and bridges on a PCI bus. If there are
1081 * bridges on the bus, recursively scan the buses behind the bridges.
1082 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001083 * @param bus Pointer to the bus structure.
1084 * @param min_devfn Minimum devfn to look at in the scan, usually 0x00.
1085 * @param max_devfn Maximum devfn to look at in the scan, usually 0xff.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001086 */
Kyösti Mälkkide271a82015-03-18 13:09:47 +02001087void pci_scan_bus(struct bus *bus, unsigned min_devfn,
1088 unsigned max_devfn)
Eric Biederman8ca8d762003-04-22 19:02:15 +00001089{
1090 unsigned int devfn;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001091 struct device *old_devices;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001092
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001093 printk(BIOS_DEBUG, "PCI: pci_scan_bus for bus %02x\n", bus->secondary);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001094
Uwe Hermanne4870472010-11-04 23:23:47 +00001095 /* Maximum sane devfn is 0xFF. */
Juhana Helovuo50b78b62010-09-13 14:43:02 +00001096 if (max_devfn > 0xff) {
Uwe Hermanne4870472010-11-04 23:23:47 +00001097 printk(BIOS_ERR, "PCI: pci_scan_bus limits devfn %x - "
1098 "devfn %x\n", min_devfn, max_devfn);
1099 printk(BIOS_ERR, "PCI: pci_scan_bus upper limit too big. "
1100 "Using 0xff.\n");
Juhana Helovuo50b78b62010-09-13 14:43:02 +00001101 max_devfn=0xff;
1102 }
1103
Eric Biederman8ca8d762003-04-22 19:02:15 +00001104 old_devices = bus->children;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001105 bus->children = NULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001106
1107 post_code(0x24);
Uwe Hermanne4870472010-11-04 23:23:47 +00001108
1109 /*
1110 * Probe all devices/functions on this bus with some optimization for
Myles Watson29cc9ed2009-07-02 18:56:24 +00001111 * non-existence and single function devices.
Eric Biedermanb78c1972004-10-14 20:54:17 +00001112 */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001113 for (devfn = min_devfn; devfn <= max_devfn; devfn++) {
Myles Watson29cc9ed2009-07-02 18:56:24 +00001114 struct device *dev;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001115
Uwe Hermanne4870472010-11-04 23:23:47 +00001116 /* First thing setup the device structure. */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001117 dev = pci_scan_get_dev(&old_devices, devfn);
Li-Ta Lo9782f752004-05-05 21:15:42 +00001118
Myles Watson29cc9ed2009-07-02 18:56:24 +00001119 /* See if a device is present and setup the device structure. */
Myles Watson032a9652009-05-11 22:24:53 +00001120 dev = pci_probe_dev(dev, bus, devfn);
Eric Biederman03acab62004-10-14 21:25:53 +00001121
Uwe Hermanne4870472010-11-04 23:23:47 +00001122 /*
1123 * If this is not a multi function device, or the device is
Myles Watson29cc9ed2009-07-02 18:56:24 +00001124 * not present don't waste time probing another function.
Myles Watson032a9652009-05-11 22:24:53 +00001125 * Skip to next device.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001126 */
Uwe Hermanne4870472010-11-04 23:23:47 +00001127 if ((PCI_FUNC(devfn) == 0x00) && (!dev
Myles Watson29cc9ed2009-07-02 18:56:24 +00001128 || (dev->enabled && ((dev->hdr_type & 0x80) != 0x80)))) {
Eric Biederman8ca8d762003-04-22 19:02:15 +00001129 devfn += 0x07;
1130 }
1131 }
Uwe Hermanne4870472010-11-04 23:23:47 +00001132
Eric Biederman8ca8d762003-04-22 19:02:15 +00001133 post_code(0x25);
1134
Uwe Hermanne4870472010-11-04 23:23:47 +00001135 /*
1136 * Warn if any leftover static devices are are found.
1137 * There's probably a problem in devicetree.cb.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001138 */
1139 if (old_devices) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001140 device_t left;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001141 printk(BIOS_WARNING, "PCI: Left over static devices:\n");
Uwe Hermanne4870472010-11-04 23:23:47 +00001142 for (left = old_devices; left; left = left->sibling)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001143 printk(BIOS_WARNING, "%s\n", dev_path(left));
Uwe Hermanne4870472010-11-04 23:23:47 +00001144
1145 printk(BIOS_WARNING, "PCI: Check your devicetree.cb.\n");
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001146 }
1147
Uwe Hermanne4870472010-11-04 23:23:47 +00001148 /*
1149 * For all children that implement scan_bus() (i.e. bridges)
Eric Biedermanb78c1972004-10-14 20:54:17 +00001150 * scan the bus behind that child.
1151 */
Kyösti Mälkkide271a82015-03-18 13:09:47 +02001152
Kyösti Mälkki2d2367c2015-02-20 21:28:31 +02001153 scan_bridges(bus);
Kyösti Mälkkide271a82015-03-18 13:09:47 +02001154
Uwe Hermanne4870472010-11-04 23:23:47 +00001155 /*
1156 * We've scanned the bus and so we know all about what's on the other
Myles Watson29cc9ed2009-07-02 18:56:24 +00001157 * side of any bridges that may be on this bus plus any devices.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001158 * Return how far we've got finding sub-buses.
1159 */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001160 post_code(0x55);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001161}
1162
Kyösti Mälkki33452402015-02-23 06:58:26 +02001163typedef enum {
1164 PCI_ROUTE_CLOSE,
1165 PCI_ROUTE_SCAN,
1166 PCI_ROUTE_FINAL,
1167} scan_state;
1168
1169static void pci_bridge_route(struct bus *link, scan_state state)
1170{
1171 struct device *dev = link->dev;
1172 struct bus *parent = dev->bus;
1173 u32 reg, buses = 0;
1174
Kyösti Mälkki757c8b42015-02-23 06:58:26 +02001175 if (state == PCI_ROUTE_SCAN) {
1176 link->secondary = parent->subordinate + 1;
1177 link->subordinate = link->secondary;
1178 }
1179
Kyösti Mälkki33452402015-02-23 06:58:26 +02001180 if (state == PCI_ROUTE_CLOSE) {
1181 buses |= 0xfeff << 8;
1182 } else if (state == PCI_ROUTE_SCAN) {
Timothy Pearson7d8a4782015-10-24 20:34:57 -05001183 buses |= parent->secondary & 0xff;
Kyösti Mälkki33452402015-02-23 06:58:26 +02001184 buses |= ((u32) link->secondary & 0xff) << 8;
Kyösti Mälkki757c8b42015-02-23 06:58:26 +02001185 buses |= 0xff << 16; /* MAX PCI_BUS number here */
Kyösti Mälkki33452402015-02-23 06:58:26 +02001186 } else if (state == PCI_ROUTE_FINAL) {
1187 buses |= parent->secondary & 0xff;
1188 buses |= ((u32) link->secondary & 0xff) << 8;
1189 buses |= ((u32) link->subordinate & 0xff) << 16;
1190 }
1191
1192 if (state == PCI_ROUTE_SCAN) {
1193 /* Clear all status bits and turn off memory, I/O and master enables. */
1194 link->bridge_cmd = pci_read_config16(dev, PCI_COMMAND);
1195 pci_write_config16(dev, PCI_COMMAND, 0x0000);
1196 pci_write_config16(dev, PCI_STATUS, 0xffff);
1197 }
1198
1199 /*
1200 * Configure the bus numbers for this bridge: the configuration
1201 * transactions will not be propagated by the bridge if it is not
1202 * correctly configured.
1203 */
1204
1205 reg = pci_read_config32(dev, PCI_PRIMARY_BUS);
1206 reg &= 0xff000000;
1207 reg |= buses;
1208 pci_write_config32(dev, PCI_PRIMARY_BUS, reg);
1209
1210 if (state == PCI_ROUTE_FINAL) {
1211 pci_write_config16(dev, PCI_COMMAND, link->bridge_cmd);
Kyösti Mälkki757c8b42015-02-23 06:58:26 +02001212 parent->subordinate = link->subordinate;
Kyösti Mälkki33452402015-02-23 06:58:26 +02001213 }
1214}
1215
Li-Ta Loe5266692004-03-23 21:28:05 +00001216/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001217 * Scan a PCI bridge and the buses behind the bridge.
Li-Ta Loe5266692004-03-23 21:28:05 +00001218 *
1219 * Determine the existence of buses behind the bridge. Set up the bridge
1220 * according to the result of the scan.
1221 *
1222 * This function is the default scan_bus() method for PCI bridge devices.
1223 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001224 * @param dev Pointer to the bridge device.
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001225 * @param do_scan_bus TODO
Eric Biederman8ca8d762003-04-22 19:02:15 +00001226 */
Kyösti Mälkki580e7222015-03-19 21:04:23 +02001227void do_pci_scan_bridge(struct device *dev,
Kyösti Mälkkide271a82015-03-18 13:09:47 +02001228 void (*do_scan_bus) (struct bus * bus,
Myles Watson29cc9ed2009-07-02 18:56:24 +00001229 unsigned min_devfn,
Kyösti Mälkkide271a82015-03-18 13:09:47 +02001230 unsigned max_devfn))
Eric Biederman8ca8d762003-04-22 19:02:15 +00001231{
Eric Biedermane9a271e32003-09-02 03:36:25 +00001232 struct bus *bus;
Eric Biederman83b991a2003-10-11 06:20:25 +00001233
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001234 printk(BIOS_SPEW, "%s for %s\n", __func__, dev_path(dev));
Li-Ta Lo3a812852004-12-03 22:39:34 +00001235
Myles Watson894a3472010-06-09 22:41:35 +00001236 if (dev->link_list == NULL) {
1237 struct bus *link;
1238 link = malloc(sizeof(*link));
1239 if (link == NULL)
1240 die("Couldn't allocate a link!\n");
1241 memset(link, 0, sizeof(*link));
1242 link->dev = dev;
1243 dev->link_list = link;
1244 }
1245
1246 bus = dev->link_list;
Eric Biedermane9a271e32003-09-02 03:36:25 +00001247
Kyösti Mälkki33452402015-02-23 06:58:26 +02001248 pci_bridge_route(bus, PCI_ROUTE_SCAN);
Li-Ta Lo3a812852004-12-03 22:39:34 +00001249
Kyösti Mälkkide271a82015-03-18 13:09:47 +02001250 do_scan_bus(bus, 0x00, 0xff);
Kyösti Mälkki33452402015-02-23 06:58:26 +02001251
1252 pci_bridge_route(bus, PCI_ROUTE_FINAL);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001253}
Li-Ta Loe5266692004-03-23 21:28:05 +00001254
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001255/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001256 * Scan a PCI bridge and the buses behind the bridge.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001257 *
1258 * Determine the existence of buses behind the bridge. Set up the bridge
1259 * according to the result of the scan.
1260 *
1261 * This function is the default scan_bus() method for PCI bridge devices.
1262 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001263 * @param dev Pointer to the bridge device.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001264 */
Kyösti Mälkki580e7222015-03-19 21:04:23 +02001265void pci_scan_bridge(struct device *dev)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001266{
Kyösti Mälkki580e7222015-03-19 21:04:23 +02001267 do_pci_scan_bridge(dev, pci_scan_bus);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001268}
1269
Myles Watson29cc9ed2009-07-02 18:56:24 +00001270/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001271 * Scan a PCI domain.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001272 *
1273 * This function is the default scan_bus() method for PCI domains.
1274 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001275 * @param dev Pointer to the domain.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001276 */
Kyösti Mälkki580e7222015-03-19 21:04:23 +02001277void pci_domain_scan_bus(device_t dev)
Myles Watson29cc9ed2009-07-02 18:56:24 +00001278{
Kyösti Mälkki6f370172015-03-19 15:26:52 +02001279 struct bus *link = dev->link_list;
Kyösti Mälkkide271a82015-03-18 13:09:47 +02001280 pci_scan_bus(link, PCI_DEVFN(0, 0), 0xff);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001281}
1282
Mike Loptien0f5cf5e2014-05-12 21:46:31 -06001283/**
1284 * Take an INT_PIN number (0, 1 - 4) and convert
1285 * it to a string ("NO PIN", "PIN A" - "PIN D")
1286 *
1287 * @param pin PCI Interrupt Pin number (0, 1 - 4)
1288 * @return A string corresponding to the pin number or "Invalid"
1289 */
1290const char *pin_to_str(int pin)
1291{
1292 const char *str[5] = {
1293 "NO PIN",
1294 "PIN A",
1295 "PIN B",
1296 "PIN C",
1297 "PIN D",
1298 };
1299
1300 if (pin >= 0 && pin <= 4)
1301 return str[pin];
1302 else
1303 return "Invalid PIN, not 0 - 4";
1304}
1305
1306/**
1307 * Get the PCI INT_PIN swizzle for a device defined as:
1308 * pin_parent = (pin_child + devn_child) % 4 + 1
1309 * where PIN A = 1 ... PIN_D = 4
1310 *
1311 * Given a PCI device structure 'dev', find the interrupt pin
1312 * that will be triggered on its parent bridge device when
1313 * generating an interrupt. For example: Device 1:3.2 may
1314 * use INT_PIN A but will trigger PIN D on its parent bridge
1315 * device. In this case, this function will return 4 (PIN D).
1316 *
1317 * @param dev A PCI device structure to swizzle interrupt pins for
Martin Roth32bc6b62015-01-04 16:54:35 -07001318 * @param *parent_bridge The PCI device structure for the bridge
Mike Loptien0f5cf5e2014-05-12 21:46:31 -06001319 * device 'dev' is attached to
1320 * @return The interrupt pin number (1 - 4) that 'dev' will
1321 * trigger when generating an interrupt
1322 */
1323static int swizzle_irq_pins(device_t dev, device_t *parent_bridge)
1324{
1325 device_t parent; /* Our current device's parent device */
1326 device_t child; /* The child device of the parent */
1327 uint8_t parent_bus = 0; /* Parent Bus number */
1328 uint16_t parent_devfn = 0; /* Parent Device and Function number */
1329 uint16_t child_devfn = 0; /* Child Device and Function number */
1330 uint8_t swizzled_pin = 0; /* Pin swizzled across a bridge */
1331
1332 /* Start with PIN A = 0 ... D = 3 */
1333 swizzled_pin = pci_read_config8(dev, PCI_INTERRUPT_PIN) - 1;
1334
1335 /* While our current device has parent devices */
1336 child = dev;
1337 for (parent = child->bus->dev; parent; parent = parent->bus->dev) {
1338 parent_bus = parent->bus->secondary;
1339 parent_devfn = parent->path.pci.devfn;
1340 child_devfn = child->path.pci.devfn;
1341
1342 /* Swizzle the INT_PIN for any bridges not on root bus */
1343 swizzled_pin = (PCI_SLOT(child_devfn) + swizzled_pin) % 4;
1344 printk(BIOS_SPEW, "\tWith INT_PIN swizzled to %s\n"
1345 "\tAttached to bridge device %01X:%02Xh.%02Xh\n",
1346 pin_to_str(swizzled_pin + 1), parent_bus,
1347 PCI_SLOT(parent_devfn), PCI_FUNC(parent_devfn));
1348
1349 /* Continue until we find the root bus */
1350 if (parent_bus > 0) {
1351 /*
1352 * We will go on to the next parent so this parent
1353 * becomes the child
1354 */
1355 child = parent;
1356 continue;
1357 } else {
1358 /*
1359 * Found the root bridge device,
1360 * fill in the structure and exit
1361 */
1362 *parent_bridge = parent;
1363 break;
1364 }
1365 }
1366
1367 /* End with PIN A = 1 ... D = 4 */
1368 return swizzled_pin + 1;
1369}
1370
1371/**
1372 * Given a device structure 'dev', find its interrupt pin
1373 * and its parent bridge 'parent_bdg' device structure.
1374 * If it is behind a bridge, it will return the interrupt
1375 * pin number (1 - 4) of the parent bridge that the device
1376 * interrupt pin has been swizzled to, otherwise it will
1377 * return the interrupt pin that is programmed into the
1378 * PCI config space of the target device. If 'dev' is
1379 * behind a bridge, it will fill in 'parent_bdg' with the
1380 * device structure of the bridge it is behind, otherwise
1381 * it will copy 'dev' into 'parent_bdg'.
1382 *
1383 * @param dev A PCI device structure to get interrupt pins for.
1384 * @param *parent_bdg The PCI device structure for the bridge
1385 * device 'dev' is attached to.
1386 * @return The interrupt pin number (1 - 4) that 'dev' will
1387 * trigger when generating an interrupt.
1388 * Errors: -1 is returned if the device is not enabled
1389 * -2 is returned if a parent bridge could not be found.
1390 */
1391int get_pci_irq_pins(device_t dev, device_t *parent_bdg)
1392{
1393 uint8_t bus = 0; /* The bus this device is on */
1394 uint16_t devfn = 0; /* This device's device and function numbers */
1395 uint8_t int_pin = 0; /* Interrupt pin used by the device */
1396 uint8_t target_pin = 0; /* Interrupt pin we want to assign an IRQ to */
1397
1398 /* Make sure this device is enabled */
1399 if (!(dev->enabled && (dev->path.type == DEVICE_PATH_PCI)))
1400 return -1;
1401
1402 bus = dev->bus->secondary;
1403 devfn = dev->path.pci.devfn;
1404
1405 /* Get and validate the interrupt pin used. Only 1-4 are allowed */
1406 int_pin = pci_read_config8(dev, PCI_INTERRUPT_PIN);
1407 if (int_pin < 1 || int_pin > 4)
1408 return -1;
1409
1410 printk(BIOS_SPEW, "PCI IRQ: Found device %01X:%02X.%02X using %s\n",
1411 bus, PCI_SLOT(devfn), PCI_FUNC(devfn), pin_to_str(int_pin));
1412
1413 /* If this device is on a bridge, swizzle its INT_PIN */
1414 if (bus) {
1415 /* Swizzle its INT_PINs */
1416 target_pin = swizzle_irq_pins(dev, parent_bdg);
1417
1418 /* Make sure the swizzle returned valid structures */
1419 if (parent_bdg == NULL) {
1420 printk(BIOS_WARNING,
1421 "Warning: Could not find parent bridge for this device!\n");
1422 return -2;
1423 }
1424 } else { /* Device is not behind a bridge */
1425 target_pin = int_pin; /* Return its own interrupt pin */
1426 *parent_bdg = dev; /* Return its own structure */
1427 }
1428
1429 /* Target pin is the interrupt pin we want to assign an IRQ to */
1430 return target_pin;
1431}
1432
Patrick Georgie1667822012-05-05 15:29:32 +02001433#if CONFIG_PC80_SYSTEM
Myles Watson29cc9ed2009-07-02 18:56:24 +00001434/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001435 * Assign IRQ numbers.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001436 *
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001437 * This function assigns IRQs for all functions contained within the indicated
Uwe Hermanne4870472010-11-04 23:23:47 +00001438 * device address. If the device does not exist or does not require interrupts
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001439 * then this function has no effect.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001440 *
1441 * This function should be called for each PCI slot in your system.
1442 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001443 * @param bus Pointer to the bus structure.
1444 * @param slot TODO
1445 * @param pIntAtoD An array of IRQ #s that are assigned to PINTA through PINTD
1446 * of this slot. The particular IRQ #s that are passed in depend on the
1447 * routing inside your southbridge and on your board.
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001448 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001449void pci_assign_irqs(unsigned bus, unsigned slot,
Uwe Hermanne4870472010-11-04 23:23:47 +00001450 const unsigned char pIntAtoD[4])
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001451{
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001452 unsigned int funct;
1453 device_t pdev;
Uwe Hermanne4870472010-11-04 23:23:47 +00001454 u8 line, irq;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001455
Uwe Hermanne4870472010-11-04 23:23:47 +00001456 /* Each slot may contain up to eight functions. */
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001457 for (funct = 0; funct < 8; funct++) {
1458 pdev = dev_find_slot(bus, (slot << 3) + funct);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001459
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001460 if (!pdev)
1461 continue;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001462
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001463 line = pci_read_config8(pdev, PCI_INTERRUPT_PIN);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001464
Uwe Hermanne4870472010-11-04 23:23:47 +00001465 /* PCI spec says all values except 1..4 are reserved. */
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001466 if ((line < 1) || (line > 4))
1467 continue;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001468
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001469 irq = pIntAtoD[line - 1];
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001470
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001471 printk(BIOS_DEBUG, "Assigning IRQ %d to %d:%x.%d\n",
Uwe Hermanne4870472010-11-04 23:23:47 +00001472 irq, bus, slot, funct);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001473
Stefan Reinauer14e22772010-04-27 06:56:47 +00001474 pci_write_config8(pdev, PCI_INTERRUPT_LINE,
Uwe Hermanne4870472010-11-04 23:23:47 +00001475 pIntAtoD[line - 1]);
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001476
1477#ifdef PARANOID_IRQ_ASSIGNMENTS
Myles Watson17aeeca2009-10-07 18:41:08 +00001478 irq = pci_read_config8(pdev, PCI_INTERRUPT_LINE);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001479 printk(BIOS_DEBUG, " Readback = %d\n", irq);
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001480#endif
1481
Patrick Georgie1667822012-05-05 15:29:32 +02001482#if CONFIG_PC80_SYSTEM
Uwe Hermanne4870472010-11-04 23:23:47 +00001483 /* Change to level triggered. */
1484 i8259_configure_irq_trigger(pIntAtoD[line - 1],
1485 IRQ_LEVEL_TRIGGERED);
Stefan Reinauer5fb62162010-12-16 23:52:04 +00001486#endif
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001487 }
1488}
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001489#endif