blob: 862ed0e7831d55b942769178fb4be4e6fd80e332 [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>
35#include <device/agp.h>
36#include <device/cardbus.h>
Eric Biederman5899fd82003-04-24 06:25:08 +000037#include <device/device.h>
38#include <device/pci.h>
39#include <device/pci_ids.h>
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000040#include <device/pcix.h>
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000041#include <device/pciexp.h>
Edward O'Callaghan6c992502014-06-20 21:19:06 +100042#include <device/hypertransport.h>
Stefan Reinauer4d933dd2009-07-21 21:36:41 +000043#include <pc80/i8259.h>
Edward O'Callaghan6c992502014-06-20 21:19:06 +100044#include <kconfig.h>
Stefan Reinauer74a0efe2012-03-30 17:10:49 -070045#include <vendorcode/google/chromeos/chromeos.h>
Eric Biederman03acab62004-10-14 21:25:53 +000046
Myles Watson29cc9ed2009-07-02 18:56:24 +000047u8 pci_moving_config8(struct device *dev, unsigned int reg)
Eric Biederman03acab62004-10-14 21:25:53 +000048{
Myles Watson29cc9ed2009-07-02 18:56:24 +000049 u8 value, ones, zeroes;
Uwe Hermanne4870472010-11-04 23:23:47 +000050
Eric Biederman03acab62004-10-14 21:25:53 +000051 value = pci_read_config8(dev, reg);
Myles Watson032a9652009-05-11 22:24:53 +000052
Eric Biederman03acab62004-10-14 21:25:53 +000053 pci_write_config8(dev, reg, 0xff);
54 ones = pci_read_config8(dev, reg);
55
56 pci_write_config8(dev, reg, 0x00);
57 zeroes = pci_read_config8(dev, reg);
58
59 pci_write_config8(dev, reg, value);
60
61 return ones ^ zeroes;
62}
Li-Ta Lo9a5b4962004-12-23 21:48:01 +000063
Uwe Hermanne4870472010-11-04 23:23:47 +000064u16 pci_moving_config16(struct device *dev, unsigned int reg)
Eric Biederman03acab62004-10-14 21:25:53 +000065{
Myles Watson29cc9ed2009-07-02 18:56:24 +000066 u16 value, ones, zeroes;
Uwe Hermanne4870472010-11-04 23:23:47 +000067
Eric Biederman03acab62004-10-14 21:25:53 +000068 value = pci_read_config16(dev, reg);
Myles Watson032a9652009-05-11 22:24:53 +000069
Eric Biederman03acab62004-10-14 21:25:53 +000070 pci_write_config16(dev, reg, 0xffff);
71 ones = pci_read_config16(dev, reg);
72
73 pci_write_config16(dev, reg, 0x0000);
74 zeroes = pci_read_config16(dev, reg);
75
76 pci_write_config16(dev, reg, value);
77
78 return ones ^ zeroes;
79}
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +000080
Uwe Hermanne4870472010-11-04 23:23:47 +000081u32 pci_moving_config32(struct device *dev, unsigned int reg)
Eric Biederman03acab62004-10-14 21:25:53 +000082{
Myles Watson29cc9ed2009-07-02 18:56:24 +000083 u32 value, ones, zeroes;
Uwe Hermanne4870472010-11-04 23:23:47 +000084
Eric Biederman03acab62004-10-14 21:25:53 +000085 value = pci_read_config32(dev, reg);
Myles Watson032a9652009-05-11 22:24:53 +000086
Eric Biederman03acab62004-10-14 21:25:53 +000087 pci_write_config32(dev, reg, 0xffffffff);
88 ones = pci_read_config32(dev, reg);
89
90 pci_write_config32(dev, reg, 0x00000000);
91 zeroes = pci_read_config32(dev, reg);
92
93 pci_write_config32(dev, reg, value);
94
95 return ones ^ zeroes;
96}
97
Myles Watson29cc9ed2009-07-02 18:56:24 +000098/**
99 * Given a device, a capability type, and a last position, return the next
100 * matching capability. Always start at the head of the list.
101 *
102 * @param dev Pointer to the device structure.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000103 * @param cap PCI_CAP_LIST_ID of the PCI capability we're looking for.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000104 * @param last Location of the PCI capability register to start from.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000105 * @return The next matching capability.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000106 */
107unsigned pci_find_next_capability(struct device *dev, unsigned cap,
108 unsigned last)
Eric Biederman03acab62004-10-14 21:25:53 +0000109{
Stefan Reinauer4d933dd2009-07-21 21:36:41 +0000110 unsigned pos = 0;
Uwe Hermanne4870472010-11-04 23:23:47 +0000111 u16 status;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000112 unsigned reps = 48;
Stefan Reinauer4d933dd2009-07-21 21:36:41 +0000113
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000114 status = pci_read_config16(dev, PCI_STATUS);
Uwe Hermanne4870472010-11-04 23:23:47 +0000115 if (!(status & PCI_STATUS_CAP_LIST))
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000116 return 0;
Uwe Hermanne4870472010-11-04 23:23:47 +0000117
Myles Watson29cc9ed2009-07-02 18:56:24 +0000118 switch (dev->hdr_type & 0x7f) {
Eric Biederman03acab62004-10-14 21:25:53 +0000119 case PCI_HEADER_TYPE_NORMAL:
120 case PCI_HEADER_TYPE_BRIDGE:
121 pos = PCI_CAPABILITY_LIST;
122 break;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000123 case PCI_HEADER_TYPE_CARDBUS:
124 pos = PCI_CB_CAPABILITY_LIST;
125 break;
126 default:
127 return 0;
Eric Biederman03acab62004-10-14 21:25:53 +0000128 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000129
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000130 pos = pci_read_config8(dev, pos);
Uwe Hermanne4870472010-11-04 23:23:47 +0000131 while (reps-- && (pos >= 0x40)) { /* Loop through the linked list. */
Eric Biederman03acab62004-10-14 21:25:53 +0000132 int this_cap;
Uwe Hermanne4870472010-11-04 23:23:47 +0000133
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000134 pos &= ~3;
Eric Biederman03acab62004-10-14 21:25:53 +0000135 this_cap = pci_read_config8(dev, pos + PCI_CAP_LIST_ID);
Uwe Hermanne4870472010-11-04 23:23:47 +0000136 printk(BIOS_SPEW, "Capability: type 0x%02x @ 0x%02x\n",
137 this_cap, pos);
138 if (this_cap == 0xff)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000139 break;
Uwe Hermanne4870472010-11-04 23:23:47 +0000140
141 if (!last && (this_cap == cap))
Eric Biederman03acab62004-10-14 21:25:53 +0000142 return pos;
Uwe Hermanne4870472010-11-04 23:23:47 +0000143
144 if (last == pos)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000145 last = 0;
Uwe Hermanne4870472010-11-04 23:23:47 +0000146
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000147 pos = pci_read_config8(dev, pos + PCI_CAP_LIST_NEXT);
Eric Biederman03acab62004-10-14 21:25:53 +0000148 }
149 return 0;
150}
151
Myles Watson29cc9ed2009-07-02 18:56:24 +0000152/**
153 * Given a device, and a capability type, return the next matching
154 * capability. Always start at the head of the list.
155 *
156 * @param dev Pointer to the device structure.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000157 * @param cap PCI_CAP_LIST_ID of the PCI capability we're looking for.
158 * @return The next matching capability.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000159 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000160unsigned pci_find_capability(device_t dev, unsigned cap)
161{
162 return pci_find_next_capability(dev, cap, 0);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000163}
164
Myles Watson29cc9ed2009-07-02 18:56:24 +0000165/**
166 * Given a device and register, read the size of the BAR for that register.
167 *
168 * @param dev Pointer to the device structure.
169 * @param index Address of the PCI configuration register.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000170 * @return TODO
Eric Biederman8ca8d762003-04-22 19:02:15 +0000171 */
Eric Biederman03acab62004-10-14 21:25:53 +0000172struct resource *pci_get_resource(struct device *dev, unsigned long index)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000173{
Eric Biederman5cd81732004-03-11 15:01:31 +0000174 struct resource *resource;
Eric Biederman03acab62004-10-14 21:25:53 +0000175 unsigned long value, attr;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000176 resource_t moving, limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000177
Myles Watson29cc9ed2009-07-02 18:56:24 +0000178 /* Initialize the resources to nothing. */
Eric Biederman03acab62004-10-14 21:25:53 +0000179 resource = new_resource(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000180
Myles Watson29cc9ed2009-07-02 18:56:24 +0000181 /* Get the initial value. */
Eric Biederman03acab62004-10-14 21:25:53 +0000182 value = pci_read_config32(dev, index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000183
Myles Watson29cc9ed2009-07-02 18:56:24 +0000184 /* See which bits move. */
Eric Biederman03acab62004-10-14 21:25:53 +0000185 moving = pci_moving_config32(dev, index);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000186
Myles Watson29cc9ed2009-07-02 18:56:24 +0000187 /* Initialize attr to the bits that do not move. */
Eric Biederman03acab62004-10-14 21:25:53 +0000188 attr = value & ~moving;
189
Myles Watson29cc9ed2009-07-02 18:56:24 +0000190 /* If it is a 64bit resource look at the high half as well. */
Eric Biederman03acab62004-10-14 21:25:53 +0000191 if (((attr & PCI_BASE_ADDRESS_SPACE_IO) == 0) &&
Myles Watson29cc9ed2009-07-02 18:56:24 +0000192 ((attr & PCI_BASE_ADDRESS_MEM_LIMIT_MASK) ==
193 PCI_BASE_ADDRESS_MEM_LIMIT_64)) {
194 /* Find the high bits that move. */
195 moving |=
196 ((resource_t) pci_moving_config32(dev, index + 4)) << 32;
Eric Biederman03acab62004-10-14 21:25:53 +0000197 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000198
Myles Watson032a9652009-05-11 22:24:53 +0000199 /* Find the resource constraints.
Eric Biederman03acab62004-10-14 21:25:53 +0000200 * Start by finding the bits that move. From there:
201 * - Size is the least significant bit of the bits that move.
202 * - Limit is all of the bits that move plus all of the lower bits.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000203 * See PCI Spec 6.2.5.1.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000204 */
Eric Biederman03acab62004-10-14 21:25:53 +0000205 limit = 0;
206 if (moving) {
207 resource->size = 1;
208 resource->align = resource->gran = 0;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000209 while (!(moving & resource->size)) {
Eric Biederman03acab62004-10-14 21:25:53 +0000210 resource->size <<= 1;
211 resource->align += 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000212 resource->gran += 1;
Eric Biederman03acab62004-10-14 21:25:53 +0000213 }
214 resource->limit = limit = moving | (resource->size - 1);
215 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000216
Uwe Hermanne4870472010-11-04 23:23:47 +0000217 /*
218 * Some broken hardware has read-only registers that do not
Eric Biederman03acab62004-10-14 21:25:53 +0000219 * really size correctly.
Uwe Hermanne4870472010-11-04 23:23:47 +0000220 *
221 * Example: the Acer M7229 has BARs 1-4 normally read-only,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000222 * so BAR1 at offset 0x10 reads 0x1f1. If you size that register
Uwe Hermanne4870472010-11-04 23:23:47 +0000223 * by writing 0xffffffff to it, it will read back as 0x1f1 -- which
224 * is a violation of the spec.
225 *
226 * We catch this case and ignore it by observing which bits move.
227 *
228 * This also catches the common case of unimplemented registers
Eric Biederman03acab62004-10-14 21:25:53 +0000229 * that always read back as 0.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000230 */
Eric Biederman03acab62004-10-14 21:25:53 +0000231 if (moving == 0) {
232 if (value != 0) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000233 printk(BIOS_DEBUG, "%s register %02lx(%08lx), "
234 "read-only ignoring it\n",
235 dev_path(dev), index, value);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000236 }
237 resource->flags = 0;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000238 } else if (attr & PCI_BASE_ADDRESS_SPACE_IO) {
239 /* An I/O mapped base address. */
Eric Biederman03acab62004-10-14 21:25:53 +0000240 attr &= PCI_BASE_ADDRESS_IO_ATTR_MASK;
Eric Biederman5cd81732004-03-11 15:01:31 +0000241 resource->flags |= IORESOURCE_IO;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000242 /* I don't want to deal with 32bit I/O resources. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000243 resource->limit = 0xffff;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000244 } else {
245 /* A Memory mapped base address. */
Eric Biederman03acab62004-10-14 21:25:53 +0000246 attr &= PCI_BASE_ADDRESS_MEM_ATTR_MASK;
Eric Biederman5cd81732004-03-11 15:01:31 +0000247 resource->flags |= IORESOURCE_MEM;
Uwe Hermanne4870472010-11-04 23:23:47 +0000248 if (attr & PCI_BASE_ADDRESS_MEM_PREFETCH)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000249 resource->flags |= IORESOURCE_PREFETCH;
Eric Biederman03acab62004-10-14 21:25:53 +0000250 attr &= PCI_BASE_ADDRESS_MEM_LIMIT_MASK;
251 if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_32) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000252 /* 32bit limit. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000253 resource->limit = 0xffffffffUL;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000254 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_1M) {
255 /* 1MB limit. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000256 resource->limit = 0x000fffffUL;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000257 } else if (attr == PCI_BASE_ADDRESS_MEM_LIMIT_64) {
258 /* 64bit limit. */
Eric Biederman03acab62004-10-14 21:25:53 +0000259 resource->limit = 0xffffffffffffffffULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000260 resource->flags |= IORESOURCE_PCI64;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000261 } else {
262 /* Invalid value. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000263 printk(BIOS_ERR, "Broken BAR with value %lx\n", attr);
264 printk(BIOS_ERR, " on dev %s at index %02lx\n",
Myles Watson29cc9ed2009-07-02 18:56:24 +0000265 dev_path(dev), index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000266 resource->flags = 0;
267 }
268 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000269
Myles Watson29cc9ed2009-07-02 18:56:24 +0000270 /* Don't let the limit exceed which bits can move. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000271 if (resource->limit > limit)
Eric Biederman03acab62004-10-14 21:25:53 +0000272 resource->limit = limit;
Eric Biederman03acab62004-10-14 21:25:53 +0000273
Eric Biederman5cd81732004-03-11 15:01:31 +0000274 return resource;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000275}
276
Myles Watson29cc9ed2009-07-02 18:56:24 +0000277/**
278 * Given a device and an index, read the size of the BAR for that register.
279 *
280 * @param dev Pointer to the device structure.
281 * @param index Address of the PCI configuration register.
282 */
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000283static void pci_get_rom_resource(struct device *dev, unsigned long index)
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000284{
285 struct resource *resource;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000286 unsigned long value;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000287 resource_t moving;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000288
Myles Watson29cc9ed2009-07-02 18:56:24 +0000289 /* Initialize the resources to nothing. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000290 resource = new_resource(dev, index);
291
Myles Watson29cc9ed2009-07-02 18:56:24 +0000292 /* Get the initial value. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000293 value = pci_read_config32(dev, index);
294
Myles Watson29cc9ed2009-07-02 18:56:24 +0000295 /* See which bits move. */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000296 moving = pci_moving_config32(dev, index);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000297
298 /* Clear the Enable bit. */
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000299 moving = moving & ~PCI_ROM_ADDRESS_ENABLE;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000300
Myles Watson032a9652009-05-11 22:24:53 +0000301 /* Find the resource constraints.
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000302 * Start by finding the bits that move. From there:
303 * - Size is the least significant bit of the bits that move.
304 * - Limit is all of the bits that move plus all of the lower bits.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000305 * See PCI Spec 6.2.5.1.
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000306 */
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000307 if (moving) {
308 resource->size = 1;
309 resource->align = resource->gran = 0;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000310 while (!(moving & resource->size)) {
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000311 resource->size <<= 1;
312 resource->align += 1;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000313 resource->gran += 1;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000314 }
Patrick Georgi16cdbb22009-04-21 20:14:31 +0000315 resource->limit = moving | (resource->size - 1);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000316 resource->flags |= IORESOURCE_MEM | IORESOURCE_READONLY;
317 } else {
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000318 if (value != 0) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000319 printk(BIOS_DEBUG, "%s register %02lx(%08lx), "
320 "read-only ignoring it\n",
321 dev_path(dev), index, value);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000322 }
323 resource->flags = 0;
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000324 }
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000325 compact_resources(dev);
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000326}
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000327
Myles Watson29cc9ed2009-07-02 18:56:24 +0000328/**
329 * Read the base address registers for a given device.
330 *
331 * @param dev Pointer to the dev structure.
332 * @param howmany How many registers to read (6 for device, 2 for bridge).
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000333 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000334static void pci_read_bases(struct device *dev, unsigned int howmany)
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000335{
336 unsigned long index;
337
Myles Watson29cc9ed2009-07-02 18:56:24 +0000338 for (index = PCI_BASE_ADDRESS_0;
339 (index < PCI_BASE_ADDRESS_0 + (howmany << 2));) {
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000340 struct resource *resource;
341 resource = pci_get_resource(dev, index);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000342 index += (resource->flags & IORESOURCE_PCI64) ? 8 : 4;
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000343 }
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +0000344
345 compact_resources(dev);
Li-Ta Lo9a5b4962004-12-23 21:48:01 +0000346}
347
Myles Watson29cc9ed2009-07-02 18:56:24 +0000348static void pci_record_bridge_resource(struct device *dev, resource_t moving,
349 unsigned index, unsigned long type)
Eric Biederman03acab62004-10-14 21:25:53 +0000350{
Eric Biederman03acab62004-10-14 21:25:53 +0000351 struct resource *resource;
Uwe Hermanne4870472010-11-04 23:23:47 +0000352 unsigned long gran;
353 resource_t step;
354
Myles Watson29cc9ed2009-07-02 18:56:24 +0000355 resource = NULL;
Uwe Hermanne4870472010-11-04 23:23:47 +0000356
357 if (!moving)
358 return;
359
360 /* Initialize the constraints on the current bus. */
361 resource = new_resource(dev, index);
362 resource->size = 0;
363 gran = 0;
364 step = 1;
365 while ((moving & step) == 0) {
366 gran += 1;
367 step <<= 1;
Eric Biederman03acab62004-10-14 21:25:53 +0000368 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000369 resource->gran = gran;
370 resource->align = gran;
371 resource->limit = moving | (step - 1);
372 resource->flags = type | IORESOURCE_PCI_BRIDGE |
373 IORESOURCE_BRIDGE;
Eric Biederman03acab62004-10-14 21:25:53 +0000374}
375
Eric Biederman8ca8d762003-04-22 19:02:15 +0000376static void pci_bridge_read_bases(struct device *dev)
377{
Eric Biederman03acab62004-10-14 21:25:53 +0000378 resource_t moving_base, moving_limit, moving;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000379
Myles Watson29cc9ed2009-07-02 18:56:24 +0000380 /* See if the bridge I/O resources are implemented. */
381 moving_base = ((u32) pci_moving_config8(dev, PCI_IO_BASE)) << 8;
382 moving_base |=
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000383 ((u32) pci_moving_config16(dev, PCI_IO_BASE_UPPER16)) << 16;
Eric Biederman03acab62004-10-14 21:25:53 +0000384
Myles Watson29cc9ed2009-07-02 18:56:24 +0000385 moving_limit = ((u32) pci_moving_config8(dev, PCI_IO_LIMIT)) << 8;
386 moving_limit |=
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000387 ((u32) pci_moving_config16(dev, PCI_IO_LIMIT_UPPER16)) << 16;
Eric Biederman03acab62004-10-14 21:25:53 +0000388
389 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000390
Myles Watson29cc9ed2009-07-02 18:56:24 +0000391 /* Initialize the I/O space constraints on the current bus. */
392 pci_record_bridge_resource(dev, moving, PCI_IO_BASE, IORESOURCE_IO);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000393
Myles Watson29cc9ed2009-07-02 18:56:24 +0000394 /* See if the bridge prefmem resources are implemented. */
395 moving_base =
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000396 ((resource_t) pci_moving_config16(dev, PCI_PREF_MEMORY_BASE)) << 16;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000397 moving_base |=
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000398 ((resource_t) pci_moving_config32(dev, PCI_PREF_BASE_UPPER32)) << 32;
Eric Biederman03acab62004-10-14 21:25:53 +0000399
Myles Watson29cc9ed2009-07-02 18:56:24 +0000400 moving_limit =
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000401 ((resource_t) pci_moving_config16(dev, PCI_PREF_MEMORY_LIMIT)) << 16;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000402 moving_limit |=
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000403 ((resource_t) pci_moving_config32(dev, PCI_PREF_LIMIT_UPPER32)) << 32;
Myles Watson032a9652009-05-11 22:24:53 +0000404
Eric Biederman03acab62004-10-14 21:25:53 +0000405 moving = moving_base & moving_limit;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000406 /* Initialize the prefetchable memory constraints on the current bus. */
407 pci_record_bridge_resource(dev, moving, PCI_PREF_MEMORY_BASE,
408 IORESOURCE_MEM | IORESOURCE_PREFETCH);
Myles Watson032a9652009-05-11 22:24:53 +0000409
Myles Watson29cc9ed2009-07-02 18:56:24 +0000410 /* See if the bridge mem resources are implemented. */
411 moving_base = ((u32) pci_moving_config16(dev, PCI_MEMORY_BASE)) << 16;
412 moving_limit = ((u32) pci_moving_config16(dev, PCI_MEMORY_LIMIT)) << 16;
Eric Biederman03acab62004-10-14 21:25:53 +0000413
414 moving = moving_base & moving_limit;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000415
Myles Watson29cc9ed2009-07-02 18:56:24 +0000416 /* Initialize the memory resources on the current bus. */
417 pci_record_bridge_resource(dev, moving, PCI_MEMORY_BASE,
418 IORESOURCE_MEM);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000419
Eric Biederman5cd81732004-03-11 15:01:31 +0000420 compact_resources(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000421}
422
Eric Biederman5899fd82003-04-24 06:25:08 +0000423void pci_dev_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000424{
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000425 pci_read_bases(dev, 6);
426 pci_get_rom_resource(dev, PCI_ROM_ADDRESS);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000427}
428
Eric Biederman5899fd82003-04-24 06:25:08 +0000429void pci_bus_read_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000430{
Eric Biederman8ca8d762003-04-22 19:02:15 +0000431 pci_bridge_read_bases(dev);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000432 pci_read_bases(dev, 2);
433 pci_get_rom_resource(dev, PCI_ROM_ADDRESS1);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000434}
435
Myles Watson29cc9ed2009-07-02 18:56:24 +0000436void pci_domain_read_resources(struct device *dev)
437{
438 struct resource *res;
439
440 /* Initialize the system-wide I/O space constraints. */
441 res = new_resource(dev, IOINDEX_SUBTRACTIVE(0, 0));
442 res->limit = 0xffffUL;
443 res->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
444 IORESOURCE_ASSIGNED;
445
446 /* Initialize the system-wide memory resources constraints. */
447 res = new_resource(dev, IOINDEX_SUBTRACTIVE(1, 0));
448 res->limit = 0xffffffffULL;
449 res->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
450 IORESOURCE_ASSIGNED;
451}
452
Eric Biederman8ca8d762003-04-22 19:02:15 +0000453static void pci_set_resource(struct device *dev, struct resource *resource)
454{
Eric Biederman03acab62004-10-14 21:25:53 +0000455 resource_t base, end;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000456
Myles Watson29cc9ed2009-07-02 18:56:24 +0000457 /* Make certain the resource has actually been assigned a value. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000458 if (!(resource->flags & IORESOURCE_ASSIGNED)) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000459 printk(BIOS_ERR, "ERROR: %s %02lx %s size: 0x%010llx not "
460 "assigned\n", dev_path(dev), resource->index,
461 resource_type(resource), resource->size);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000462 return;
463 }
464
Myles Watsoneb81a5b2009-11-05 20:06:19 +0000465 /* If this resource is fixed don't worry about it. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000466 if (resource->flags & IORESOURCE_FIXED)
Myles Watsoneb81a5b2009-11-05 20:06:19 +0000467 return;
Myles Watsoneb81a5b2009-11-05 20:06:19 +0000468
Myles Watson29cc9ed2009-07-02 18:56:24 +0000469 /* If I have already stored this resource don't worry about it. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000470 if (resource->flags & IORESOURCE_STORED)
Eric Biederman5cd81732004-03-11 15:01:31 +0000471 return;
Eric Biederman5cd81732004-03-11 15:01:31 +0000472
Myles Watson29cc9ed2009-07-02 18:56:24 +0000473 /* If the resource is subtractive don't worry about it. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000474 if (resource->flags & IORESOURCE_SUBTRACTIVE)
Eric Biederman03acab62004-10-14 21:25:53 +0000475 return;
Eric Biederman03acab62004-10-14 21:25:53 +0000476
Myles Watson29cc9ed2009-07-02 18:56:24 +0000477 /* Only handle PCI memory and I/O resources for now. */
478 if (!(resource->flags & (IORESOURCE_MEM | IORESOURCE_IO)))
Eric Biederman8ca8d762003-04-22 19:02:15 +0000479 return;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000480
Myles Watson29cc9ed2009-07-02 18:56:24 +0000481 /* Enable the resources in the command register. */
Eric Biederman03acab62004-10-14 21:25:53 +0000482 if (resource->size) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000483 if (resource->flags & IORESOURCE_MEM)
Eric Biederman03acab62004-10-14 21:25:53 +0000484 dev->command |= PCI_COMMAND_MEMORY;
Uwe Hermanne4870472010-11-04 23:23:47 +0000485 if (resource->flags & IORESOURCE_IO)
Eric Biederman03acab62004-10-14 21:25:53 +0000486 dev->command |= PCI_COMMAND_IO;
Uwe Hermanne4870472010-11-04 23:23:47 +0000487 if (resource->flags & IORESOURCE_PCI_BRIDGE)
Eric Biederman03acab62004-10-14 21:25:53 +0000488 dev->command |= PCI_COMMAND_MASTER;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000489 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000490
Myles Watson29cc9ed2009-07-02 18:56:24 +0000491 /* Get the base address. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000492 base = resource->base;
Eric Biederman5cd81732004-03-11 15:01:31 +0000493
Myles Watson29cc9ed2009-07-02 18:56:24 +0000494 /* Get the end. */
Eric Biederman03acab62004-10-14 21:25:53 +0000495 end = resource_end(resource);
Myles Watson032a9652009-05-11 22:24:53 +0000496
Myles Watson29cc9ed2009-07-02 18:56:24 +0000497 /* Now store the resource. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000498 resource->flags |= IORESOURCE_STORED;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000499
Uwe Hermanne4870472010-11-04 23:23:47 +0000500 /*
501 * PCI bridges have no enable bit. They are disabled if the base of
502 * the range is greater than the limit. If the size is zero, disable
Myles Watson29cc9ed2009-07-02 18:56:24 +0000503 * by setting the base = limit and end = limit - 2^gran.
504 */
505 if (resource->size == 0 && (resource->flags & IORESOURCE_PCI_BRIDGE)) {
506 base = resource->limit;
507 end = resource->limit - (1 << resource->gran);
508 resource->base = base;
509 }
510
Eric Biederman8ca8d762003-04-22 19:02:15 +0000511 if (!(resource->flags & IORESOURCE_PCI_BRIDGE)) {
Eric Biederman03acab62004-10-14 21:25:53 +0000512 unsigned long base_lo, base_hi;
Uwe Hermanne4870472010-11-04 23:23:47 +0000513
514 /*
515 * Some chipsets allow us to set/clear the I/O bit
516 * (e.g. VIA 82C686A). So set it to be safe.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000517 */
Eric Biederman03acab62004-10-14 21:25:53 +0000518 base_lo = base & 0xffffffff;
519 base_hi = (base >> 32) & 0xffffffff;
Uwe Hermanne4870472010-11-04 23:23:47 +0000520 if (resource->flags & IORESOURCE_IO)
Eric Biederman03acab62004-10-14 21:25:53 +0000521 base_lo |= PCI_BASE_ADDRESS_SPACE_IO;
Eric Biederman03acab62004-10-14 21:25:53 +0000522 pci_write_config32(dev, resource->index, base_lo);
Uwe Hermanne4870472010-11-04 23:23:47 +0000523 if (resource->flags & IORESOURCE_PCI64)
Eric Biederman03acab62004-10-14 21:25:53 +0000524 pci_write_config32(dev, resource->index + 4, base_hi);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000525 } else if (resource->index == PCI_IO_BASE) {
526 /* Set the I/O ranges. */
527 pci_write_config8(dev, PCI_IO_BASE, base >> 8);
Eric Biederman03acab62004-10-14 21:25:53 +0000528 pci_write_config16(dev, PCI_IO_BASE_UPPER16, base >> 16);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000529 pci_write_config8(dev, PCI_IO_LIMIT, end >> 8);
Eric Biederman03acab62004-10-14 21:25:53 +0000530 pci_write_config16(dev, PCI_IO_LIMIT_UPPER16, end >> 16);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000531 } else if (resource->index == PCI_MEMORY_BASE) {
532 /* Set the memory range. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000533 pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16);
Eric Biederman03acab62004-10-14 21:25:53 +0000534 pci_write_config16(dev, PCI_MEMORY_LIMIT, end >> 16);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000535 } else if (resource->index == PCI_PREF_MEMORY_BASE) {
536 /* Set the prefetchable memory range. */
Eric Biederman03acab62004-10-14 21:25:53 +0000537 pci_write_config16(dev, PCI_PREF_MEMORY_BASE, base >> 16);
538 pci_write_config32(dev, PCI_PREF_BASE_UPPER32, base >> 32);
539 pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, end >> 16);
540 pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, end >> 32);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000541 } else {
542 /* Don't let me think I stored the resource. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000543 resource->flags &= ~IORESOURCE_STORED;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000544 printk(BIOS_ERR, "ERROR: invalid resource->index %lx\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000545 resource->index);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000546 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000547
Eric Biederman03acab62004-10-14 21:25:53 +0000548 report_resource_stored(dev, resource, "");
Eric Biederman8ca8d762003-04-22 19:02:15 +0000549}
550
Eric Biederman5899fd82003-04-24 06:25:08 +0000551void pci_dev_set_resources(struct device *dev)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000552{
Myles Watsonc25cc112010-05-21 14:33:48 +0000553 struct resource *res;
Myles Watson894a3472010-06-09 22:41:35 +0000554 struct bus *bus;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000555 u8 line;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000556
Uwe Hermanne4870472010-11-04 23:23:47 +0000557 for (res = dev->resource_list; res; res = res->next)
Myles Watsonc25cc112010-05-21 14:33:48 +0000558 pci_set_resource(dev, res);
Uwe Hermanne4870472010-11-04 23:23:47 +0000559
Myles Watson894a3472010-06-09 22:41:35 +0000560 for (bus = dev->link_list; bus; bus = bus->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000561 if (bus->children)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000562 assign_resources(bus);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000563 }
564
Myles Watson29cc9ed2009-07-02 18:56:24 +0000565 /* Set a default latency timer. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000566 pci_write_config8(dev, PCI_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000567
Myles Watson29cc9ed2009-07-02 18:56:24 +0000568 /* Set a default secondary latency timer. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000569 if ((dev->hdr_type & 0x7f) == PCI_HEADER_TYPE_BRIDGE)
Eric Biederman7a5416a2003-06-12 19:23:51 +0000570 pci_write_config8(dev, PCI_SEC_LATENCY_TIMER, 0x40);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000571
Myles Watson29cc9ed2009-07-02 18:56:24 +0000572 /* Zero the IRQ settings. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000573 line = pci_read_config8(dev, PCI_INTERRUPT_PIN);
Uwe Hermanne4870472010-11-04 23:23:47 +0000574 if (line)
Eric Biederman7a5416a2003-06-12 19:23:51 +0000575 pci_write_config8(dev, PCI_INTERRUPT_LINE, 0);
Uwe Hermanne4870472010-11-04 23:23:47 +0000576
Myles Watson29cc9ed2009-07-02 18:56:24 +0000577 /* Set the cache line size, so far 64 bytes is good for everyone. */
Eric Biederman7a5416a2003-06-12 19:23:51 +0000578 pci_write_config8(dev, PCI_CACHE_LINE_SIZE, 64 >> 2);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000579}
580
Eric Biedermane9a271e32003-09-02 03:36:25 +0000581void pci_dev_enable_resources(struct device *dev)
582{
Eric Biedermana9e632c2004-11-18 22:38:08 +0000583 const struct pci_operations *ops;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000584 u16 command;
Eric Biederman03acab62004-10-14 21:25:53 +0000585
Uwe Hermanne4870472010-11-04 23:23:47 +0000586 /* Set the subsystem vendor and device ID for mainboard devices. */
Eric Biederman03acab62004-10-14 21:25:53 +0000587 ops = ops_pci(dev);
Eric Biedermandbec2d42004-10-21 10:44:08 +0000588 if (dev->on_mainboard && ops && ops->set_subsystem) {
Duncan Laurie7e1c83e2013-08-09 07:55:10 -0700589 if (CONFIG_SUBSYSTEM_VENDOR_ID)
590 dev->subsystem_vendor = CONFIG_SUBSYSTEM_VENDOR_ID;
591 if (CONFIG_SUBSYSTEM_DEVICE_ID)
592 dev->subsystem_device = CONFIG_SUBSYSTEM_DEVICE_ID;
Sven Schnelle91321022011-03-01 19:58:47 +0000593 printk(BIOS_DEBUG, "%s subsystem <- %04x/%04x\n",
594 dev_path(dev), dev->subsystem_vendor,
595 dev->subsystem_device);
596 ops->set_subsystem(dev, dev->subsystem_vendor,
597 dev->subsystem_device);
Eric Biederman03acab62004-10-14 21:25:53 +0000598 }
Eric Biedermane9a271e32003-09-02 03:36:25 +0000599 command = pci_read_config16(dev, PCI_COMMAND);
600 command |= dev->command;
Uwe Hermanne4870472010-11-04 23:23:47 +0000601
Myles Watson29cc9ed2009-07-02 18:56:24 +0000602 /* v3 has
603 * command |= (PCI_COMMAND_PARITY + PCI_COMMAND_SERR); // Error check.
604 */
Uwe Hermanne4870472010-11-04 23:23:47 +0000605
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000606 printk(BIOS_DEBUG, "%s cmd <- %02x\n", dev_path(dev), command);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000607 pci_write_config16(dev, PCI_COMMAND, command);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000608}
609
610void pci_bus_enable_resources(struct device *dev)
611{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000612 u16 ctrl;
613
Uwe Hermanne4870472010-11-04 23:23:47 +0000614 /*
615 * Enable I/O in command register if there is VGA card
Myles Watson29cc9ed2009-07-02 18:56:24 +0000616 * connected with (even it does not claim I/O resource).
617 */
Myles Watson894a3472010-06-09 22:41:35 +0000618 if (dev->link_list->bridge_ctrl & PCI_BRIDGE_CTL_VGA)
Li-Ta Lo515f6c72005-01-11 22:48:54 +0000619 dev->command |= PCI_COMMAND_IO;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000620 ctrl = pci_read_config16(dev, PCI_BRIDGE_CONTROL);
Myles Watson894a3472010-06-09 22:41:35 +0000621 ctrl |= dev->link_list->bridge_ctrl;
Uwe Hermanne4870472010-11-04 23:23:47 +0000622 ctrl |= (PCI_BRIDGE_CTL_PARITY + PCI_BRIDGE_CTL_SERR); /* Error check. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000623 printk(BIOS_DEBUG, "%s bridge ctrl <- %04x\n", dev_path(dev), ctrl);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000624 pci_write_config16(dev, PCI_BRIDGE_CONTROL, ctrl);
625
626 pci_dev_enable_resources(dev);
627}
628
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000629void pci_bus_reset(struct bus *bus)
630{
Uwe Hermanne4870472010-11-04 23:23:47 +0000631 u16 ctl;
632
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000633 ctl = pci_read_config16(bus->dev, PCI_BRIDGE_CONTROL);
634 ctl |= PCI_BRIDGE_CTL_BUS_RESET;
635 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
636 mdelay(10);
Uwe Hermanne4870472010-11-04 23:23:47 +0000637
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000638 ctl &= ~PCI_BRIDGE_CTL_BUS_RESET;
639 pci_write_config16(bus->dev, PCI_BRIDGE_CONTROL, ctl);
640 delay(1);
641}
642
Myles Watson29cc9ed2009-07-02 18:56:24 +0000643void pci_dev_set_subsystem(struct device *dev, unsigned vendor, unsigned device)
Eric Biederman03acab62004-10-14 21:25:53 +0000644{
Myles Watson032a9652009-05-11 22:24:53 +0000645 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
Myles Watson29cc9ed2009-07-02 18:56:24 +0000646 ((device & 0xffff) << 16) | (vendor & 0xffff));
Eric Biederman03acab62004-10-14 21:25:53 +0000647}
648
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300649#if CONFIG_VGA_ROM_RUN
650static int should_run_oprom(struct device *dev)
651{
652 static int should_run = -1;
653
654 if (should_run >= 0)
655 return should_run;
656
Kyösti Mälkki9ab1c102013-12-22 00:22:49 +0200657 /* Don't run VGA option ROMs, unless we have to print
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300658 * something on the screen before the kernel is loaded.
659 */
Kyösti Mälkki9ab1c102013-12-22 00:22:49 +0200660 should_run = !IS_ENABLED(CONFIG_BOOTMODE_STRAPS) ||
661 developer_mode_enabled() || recovery_mode_enabled();
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300662
Kyösti Mälkki9ab1c102013-12-22 00:22:49 +0200663#if CONFIG_CHROMEOS
664 if (!should_run)
665 should_run = vboot_wants_oprom();
666#endif
667 if (!should_run)
668 printk(BIOS_DEBUG, "Not running VGA Option ROM\n");
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300669 return should_run;
670}
671
672static int should_load_oprom(struct device *dev)
673{
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300674 /* If S3_VGA_ROM_RUN is disabled, skip running VGA option
675 * ROMs when coming out of an S3 resume.
676 */
Kyösti Mälkki58ceb002014-06-20 06:21:01 +0300677 if (!IS_ENABLED(CONFIG_S3_VGA_ROM_RUN) && acpi_is_wakeup_s3() &&
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300678 ((dev->class >> 8) == PCI_CLASS_DISPLAY_VGA))
679 return 0;
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300680 if (IS_ENABLED(CONFIG_ALWAYS_LOAD_OPROM))
681 return 1;
682 if (should_run_oprom(dev))
683 return 1;
684
685 return 0;
686}
687#endif /* CONFIG_VGA_ROM_RUN */
688
Uwe Hermanne4870472010-11-04 23:23:47 +0000689/** Default handler: only runs the relevant PCI BIOS. */
Li-Ta Lo883b8792005-01-10 23:16:22 +0000690void pci_dev_init(struct device *dev)
691{
Vladimir Serbinenkob32816e2013-12-20 17:47:19 +0100692#if CONFIG_VGA_ROM_RUN
Li-Ta Lo883b8792005-01-10 23:16:22 +0000693 struct rom_header *rom, *ram;
694
Vladimir Serbinenkob32816e2013-12-20 17:47:19 +0100695 /* Only execute VGA ROMs. */
696 if (((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA))
Myles Watson17aeeca2009-10-07 18:41:08 +0000697 return;
Roman Kononov778a42b2007-04-06 18:34:39 +0000698
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300699 if (!should_load_oprom(dev))
Stefan Reinauer74a0efe2012-03-30 17:10:49 -0700700 return;
Aaron Durbince872cb2013-03-28 15:59:19 -0500701
702 rom = pci_rom_probe(dev);
703 if (rom == NULL)
704 return;
705
706 ram = pci_rom_load(dev, rom);
707 if (ram == NULL)
708 return;
709
Kyösti Mälkki580e5642014-05-01 16:31:34 +0300710 if (!should_run_oprom(dev))
711 return;
712
Stefan Reinauerd98cf5b2008-08-01 11:25:41 +0000713 run_bios(dev, (unsigned long)ram);
Kyösti Mälkkiab56b3b2013-11-28 16:44:51 +0200714 gfx_set_init_done(1);
715 printk(BIOS_DEBUG, "VGA Option ROM was run\n");
Vladimir Serbinenkob32816e2013-12-20 17:47:19 +0100716#endif /* CONFIG_VGA_ROM_RUN */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000717}
Li-Ta Lo883b8792005-01-10 23:16:22 +0000718
Li-Ta Loe5266692004-03-23 21:28:05 +0000719/** Default device operation for PCI devices */
Eric Biedermana9e632c2004-11-18 22:38:08 +0000720static struct pci_operations pci_dev_ops_pci = {
Eric Biederman03acab62004-10-14 21:25:53 +0000721 .set_subsystem = pci_dev_set_subsystem,
722};
723
Eric Biederman8ca8d762003-04-22 19:02:15 +0000724struct device_operations default_pci_ops_dev = {
Uwe Hermanne4870472010-11-04 23:23:47 +0000725 .read_resources = pci_dev_read_resources,
726 .set_resources = pci_dev_set_resources,
Eric Biedermane9a271e32003-09-02 03:36:25 +0000727 .enable_resources = pci_dev_enable_resources,
Uwe Hermanne4870472010-11-04 23:23:47 +0000728 .init = pci_dev_init,
729 .scan_bus = 0,
730 .enable = 0,
731 .ops_pci = &pci_dev_ops_pci,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000732};
Li-Ta Loe5266692004-03-23 21:28:05 +0000733
734/** Default device operations for PCI bridges */
Eric Biedermana9e632c2004-11-18 22:38:08 +0000735static struct pci_operations pci_bus_ops_pci = {
Eric Biederman03acab62004-10-14 21:25:53 +0000736 .set_subsystem = 0,
737};
Li-Ta Lo883b8792005-01-10 23:16:22 +0000738
Eric Biederman8ca8d762003-04-22 19:02:15 +0000739struct device_operations default_pci_ops_bus = {
Uwe Hermanne4870472010-11-04 23:23:47 +0000740 .read_resources = pci_bus_read_resources,
741 .set_resources = pci_dev_set_resources,
Eric Biedermane9a271e32003-09-02 03:36:25 +0000742 .enable_resources = pci_bus_enable_resources,
Uwe Hermanne4870472010-11-04 23:23:47 +0000743 .init = 0,
744 .scan_bus = pci_scan_bridge,
745 .enable = 0,
746 .reset_bus = pci_bus_reset,
747 .ops_pci = &pci_bus_ops_pci,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000748};
Li-Ta Loe5266692004-03-23 21:28:05 +0000749
750/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000751 * Detect the type of downstream bridge.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000752 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000753 * This function is a heuristic to detect which type of bus is downstream
754 * of a PCI-to-PCI bridge. This functions by looking for various capability
755 * blocks to figure out the type of downstream bridge. PCI-X, PCI-E, and
756 * Hypertransport all seem to have appropriate capabilities.
Myles Watson032a9652009-05-11 22:24:53 +0000757 *
Uwe Hermanne4870472010-11-04 23:23:47 +0000758 * When only a PCI-Express capability is found the type is examined to see
759 * which type of bridge we have.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000760 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000761 * @param dev Pointer to the device structure of the bridge.
762 * @return Appropriate bridge operations.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000763 */
764static struct device_operations *get_pci_bridge_ops(device_t dev)
765{
Patrick Georgie1667822012-05-05 15:29:32 +0200766#if CONFIG_PCIX_PLUGIN_SUPPORT
Ronald G. Minnich78a16672012-11-29 16:28:21 -0800767 unsigned int pcixpos;
768 pcixpos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
769 if (pcixpos) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000770 printk(BIOS_DEBUG, "%s subordinate bus PCI-X\n", dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000771 return &default_pcix_ops_bus;
772 }
773#endif
Patrick Georgie1667822012-05-05 15:29:32 +0200774#if CONFIG_AGP_PLUGIN_SUPPORT
Uwe Hermanne4870472010-11-04 23:23:47 +0000775 /* How do I detect a PCI to AGP bridge? */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000776#endif
Patrick Georgie1667822012-05-05 15:29:32 +0200777#if CONFIG_HYPERTRANSPORT_PLUGIN_SUPPORT
Ronald G. Minnich78a16672012-11-29 16:28:21 -0800778 unsigned int htpos = 0;
779 while ((htpos = pci_find_next_capability(dev, PCI_CAP_ID_HT, htpos))) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000780 u16 flags;
Ronald G. Minnich78a16672012-11-29 16:28:21 -0800781 flags = pci_read_config16(dev, htpos + PCI_CAP_FLAGS);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000782 if ((flags >> 13) == 1) {
783 /* Host or Secondary Interface */
Uwe Hermanne4870472010-11-04 23:23:47 +0000784 printk(BIOS_DEBUG, "%s subordinate bus HT\n",
785 dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000786 return &default_ht_ops_bus;
787 }
788 }
789#endif
Patrick Georgie1667822012-05-05 15:29:32 +0200790#if CONFIG_PCIEXP_PLUGIN_SUPPORT
Ronald G. Minnich78a16672012-11-29 16:28:21 -0800791 unsigned int pciexpos;
792 pciexpos = pci_find_capability(dev, PCI_CAP_ID_PCIE);
793 if (pciexpos) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000794 u16 flags;
Ronald G. Minnich78a16672012-11-29 16:28:21 -0800795 flags = pci_read_config16(dev, pciexpos + PCI_EXP_FLAGS);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000796 switch ((flags & PCI_EXP_FLAGS_TYPE) >> 4) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000797 case PCI_EXP_TYPE_ROOT_PORT:
798 case PCI_EXP_TYPE_UPSTREAM:
799 case PCI_EXP_TYPE_DOWNSTREAM:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000800 printk(BIOS_DEBUG, "%s subordinate bus PCI Express\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000801 dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000802 return &default_pciexp_ops_bus;
803 case PCI_EXP_TYPE_PCI_BRIDGE:
Uwe Hermanne4870472010-11-04 23:23:47 +0000804 printk(BIOS_DEBUG, "%s subordinate PCI\n",
805 dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000806 return &default_pci_ops_bus;
807 default:
808 break;
809 }
810 }
811#endif
812 return &default_pci_ops_bus;
813}
814
815/**
Vadim Bendebury8049fc92012-04-24 12:53:19 -0700816 * Check if a device id matches a PCI driver entry.
817 *
818 * The driver entry can either point at a zero terminated array of acceptable
819 * device IDs, or include a single device ID.
820 *
Martin Roth98b698c2015-01-06 21:02:52 -0700821 * @param driver pointer to the PCI driver entry being checked
822 * @param device_id PCI device ID of the device being matched
Vadim Bendebury8049fc92012-04-24 12:53:19 -0700823 */
824static int device_id_match(struct pci_driver *driver, unsigned short device_id)
825{
826 if (driver->devices) {
827 unsigned short check_id;
828 const unsigned short *device_list = driver->devices;
829 while ((check_id = *device_list++) != 0)
830 if (check_id == device_id)
831 return 1;
832 }
833
834 return (driver->device == device_id);
835}
836
837/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000838 * Set up PCI device operation.
839 *
840 * Check if it already has a driver. If not, use find_device_operations(),
841 * or set to a default based on type.
Li-Ta Loe5266692004-03-23 21:28:05 +0000842 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000843 * @param dev Pointer to the device whose pci_ops you want to set.
Li-Ta Loe5266692004-03-23 21:28:05 +0000844 * @see pci_drivers
845 */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000846static void set_pci_ops(struct device *dev)
847{
848 struct pci_driver *driver;
Li-Ta Loe5266692004-03-23 21:28:05 +0000849
Uwe Hermanne4870472010-11-04 23:23:47 +0000850 if (dev->ops)
851 return;
852
853 /*
854 * Look through the list of setup drivers and find one for
Myles Watson29cc9ed2009-07-02 18:56:24 +0000855 * this PCI device.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000856 */
Myles Watson29cc9ed2009-07-02 18:56:24 +0000857 for (driver = &pci_drivers[0]; driver != &epci_drivers[0]; driver++) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000858 if ((driver->vendor == dev->vendor) &&
Vadim Bendebury8049fc92012-04-24 12:53:19 -0700859 device_id_match(driver, dev->device)) {
Uwe Hermann312673c2009-10-27 21:49:33 +0000860 dev->ops = (struct device_operations *)driver->ops;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000861 printk(BIOS_SPEW, "%s [%04x/%04x] %sops\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000862 dev_path(dev), driver->vendor, driver->device,
863 (driver->ops->scan_bus ? "bus " : ""));
Eric Biederman5899fd82003-04-24 06:25:08 +0000864 return;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000865 }
866 }
Li-Ta Loe5266692004-03-23 21:28:05 +0000867
Uwe Hermanne4870472010-11-04 23:23:47 +0000868 /* If I don't have a specific driver use the default operations. */
869 switch (dev->hdr_type & 0x7f) { /* Header type */
870 case PCI_HEADER_TYPE_NORMAL:
Eric Biederman8ca8d762003-04-22 19:02:15 +0000871 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
872 goto bad;
873 dev->ops = &default_pci_ops_dev;
874 break;
875 case PCI_HEADER_TYPE_BRIDGE:
876 if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
877 goto bad;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000878 dev->ops = get_pci_bridge_ops(dev);
Eric Biederman8ca8d762003-04-22 19:02:15 +0000879 break;
Patrick Georgie1667822012-05-05 15:29:32 +0200880#if CONFIG_CARDBUS_PLUGIN_SUPPORT
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000881 case PCI_HEADER_TYPE_CARDBUS:
882 dev->ops = &default_cardbus_ops_bus;
883 break;
884#endif
Uwe Hermanne4870472010-11-04 23:23:47 +0000885default:
886bad:
Li-Ta Lo69c5a902004-04-29 20:08:54 +0000887 if (dev->enabled) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000888 printk(BIOS_ERR, "%s [%04x/%04x/%06x] has unknown "
889 "header type %02x, ignoring.\n", dev_path(dev),
890 dev->vendor, dev->device,
891 dev->class >> 8, dev->hdr_type);
Eric Biederman83b991a2003-10-11 06:20:25 +0000892 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000893 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000894}
895
896/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000897 * See if we have already allocated a device structure for a given devfn.
Li-Ta Loe5266692004-03-23 21:28:05 +0000898 *
899 * Given a linked list of PCI device structures and a devfn number, find the
Li-Ta Lo3a812852004-12-03 22:39:34 +0000900 * device structure correspond to the devfn, if present. This function also
901 * removes the device structure from the linked list.
Li-Ta Loe5266692004-03-23 21:28:05 +0000902 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000903 * @param list The device structure list.
904 * @param devfn A device/function number.
Myles Watson29cc9ed2009-07-02 18:56:24 +0000905 * @return Pointer to the device structure found or NULL if we have not
Li-Ta Lo3a812852004-12-03 22:39:34 +0000906 * allocated a device for this devfn yet.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000907 */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000908static struct device *pci_scan_get_dev(struct device **list, unsigned int devfn)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000909{
Eric Biedermanb78c1972004-10-14 20:54:17 +0000910 struct device *dev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000911
Eric Biedermanb78c1972004-10-14 20:54:17 +0000912 dev = 0;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000913 for (; *list; list = &(*list)->sibling) {
Eric Biedermanad1b35a2003-10-14 02:36:51 +0000914 if ((*list)->path.type != DEVICE_PATH_PCI) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000915 printk(BIOS_ERR, "child %s not a PCI device\n",
916 dev_path(*list));
Eric Biedermanad1b35a2003-10-14 02:36:51 +0000917 continue;
918 }
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000919 if ((*list)->path.pci.devfn == devfn) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000920 /* Unlink from the list. */
Eric Biederman8ca8d762003-04-22 19:02:15 +0000921 dev = *list;
922 *list = (*list)->sibling;
Myles Watson29cc9ed2009-07-02 18:56:24 +0000923 dev->sibling = NULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000924 break;
925 }
926 }
Myles Watson29cc9ed2009-07-02 18:56:24 +0000927
Uwe Hermanne4870472010-11-04 23:23:47 +0000928 /*
929 * Just like alloc_dev() add the device to the list of devices on the
Myles Watson29cc9ed2009-07-02 18:56:24 +0000930 * bus. When the list of devices was formed we removed all of the
931 * parents children, and now we are interleaving static and dynamic
932 * devices in order on the bus.
Eric Biedermanb78c1972004-10-14 20:54:17 +0000933 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000934 if (dev) {
Myles Watson29cc9ed2009-07-02 18:56:24 +0000935 struct device *child;
Uwe Hermanne4870472010-11-04 23:23:47 +0000936
Myles Watson29cc9ed2009-07-02 18:56:24 +0000937 /* Find the last child of our parent. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000938 for (child = dev->bus->children; child && child->sibling;)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000939 child = child->sibling;
Uwe Hermanne4870472010-11-04 23:23:47 +0000940
Myles Watson29cc9ed2009-07-02 18:56:24 +0000941 /* Place the device on the list of children of its parent. */
Uwe Hermanne4870472010-11-04 23:23:47 +0000942 if (child)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000943 child->sibling = dev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000944 else
Eric Biedermane9a271e32003-09-02 03:36:25 +0000945 dev->bus->children = dev;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000946 }
947
Eric Biederman8ca8d762003-04-22 19:02:15 +0000948 return dev;
949}
950
Myles Watson032a9652009-05-11 22:24:53 +0000951/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000952 * Scan a PCI bus.
Li-Ta Loe5266692004-03-23 21:28:05 +0000953 *
Myles Watson29cc9ed2009-07-02 18:56:24 +0000954 * Determine the existence of a given PCI device. Allocate a new struct device
955 * if dev==NULL was passed in and the device exists in hardware.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000956 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000957 * @param dev Pointer to the dev structure.
958 * @param bus Pointer to the bus structure.
959 * @param devfn A device/function number to look at.
960 * @return The device structure for the device (if found), NULL otherwise.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000961 */
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000962device_t pci_probe_dev(device_t dev, struct bus *bus, unsigned devfn)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000963{
Myles Watson29cc9ed2009-07-02 18:56:24 +0000964 u32 id, class;
965 u8 hdr_type;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000966
Myles Watson29cc9ed2009-07-02 18:56:24 +0000967 /* Detect if a device is present. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000968 if (!dev) {
969 struct device dummy;
Uwe Hermanne4870472010-11-04 23:23:47 +0000970
Myles Watson29cc9ed2009-07-02 18:56:24 +0000971 dummy.bus = bus;
972 dummy.path.type = DEVICE_PATH_PCI;
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000973 dummy.path.pci.devfn = devfn;
Uwe Hermanne4870472010-11-04 23:23:47 +0000974
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000975 id = pci_read_config32(&dummy, PCI_VENDOR_ID);
Uwe Hermanne4870472010-11-04 23:23:47 +0000976 /*
977 * Have we found something? Some broken boards return 0 if a
978 * slot is empty, but the expected answer is 0xffffffff.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000979 */
Uwe Hermanne4870472010-11-04 23:23:47 +0000980 if (id == 0xffffffff)
Stefan Reinauer7355c752010-04-02 16:30:25 +0000981 return NULL;
Uwe Hermanne4870472010-11-04 23:23:47 +0000982
Stefan Reinauer7355c752010-04-02 16:30:25 +0000983 if ((id == 0x00000000) || (id == 0x0000ffff) ||
984 (id == 0xffff0000)) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000985 printk(BIOS_SPEW, "%s, bad id 0x%x\n",
986 dev_path(&dummy), id);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000987 return NULL;
988 }
989 dev = alloc_dev(bus, &dummy.path);
Myles Watson29cc9ed2009-07-02 18:56:24 +0000990 } else {
Uwe Hermanne4870472010-11-04 23:23:47 +0000991 /*
992 * Enable/disable the device. Once we have found the device-
Myles Watson29cc9ed2009-07-02 18:56:24 +0000993 * specific operations this operations we will disable the
994 * device with those as well.
Myles Watson032a9652009-05-11 22:24:53 +0000995 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000996 * This is geared toward devices that have subfunctions
997 * that do not show up by default.
Myles Watson032a9652009-05-11 22:24:53 +0000998 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000999 * If a device is a stuff option on the motherboard
Myles Watson29cc9ed2009-07-02 18:56:24 +00001000 * it may be absent and enable_dev() must cope.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001001 */
Myles Watson29cc9ed2009-07-02 18:56:24 +00001002 /* Run the magic enable sequence for the device. */
Uwe Hermanne4870472010-11-04 23:23:47 +00001003 if (dev->chip_ops && dev->chip_ops->enable_dev)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001004 dev->chip_ops->enable_dev(dev);
Uwe Hermanne4870472010-11-04 23:23:47 +00001005
Myles Watson29cc9ed2009-07-02 18:56:24 +00001006 /* Now read the vendor and device ID. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001007 id = pci_read_config32(dev, PCI_VENDOR_ID);
Myles Watson032a9652009-05-11 22:24:53 +00001008
Uwe Hermanne4870472010-11-04 23:23:47 +00001009 /*
1010 * If the device does not have a PCI ID disable it. Possibly
Myles Watson29cc9ed2009-07-02 18:56:24 +00001011 * this is because we have already disabled the device. But
1012 * this also handles optional devices that may not always
1013 * show up.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001014 */
1015 /* If the chain is fully enumerated quit */
Myles Watson29cc9ed2009-07-02 18:56:24 +00001016 if ((id == 0xffffffff) || (id == 0x00000000) ||
1017 (id == 0x0000ffff) || (id == 0xffff0000)) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001018 if (dev->enabled) {
Uwe Hermanne4870472010-11-04 23:23:47 +00001019 printk(BIOS_INFO, "PCI: Static device %s not "
1020 "found, disabling it.\n", dev_path(dev));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001021 dev->enabled = 0;
1022 }
1023 return dev;
1024 }
1025 }
Uwe Hermanne4870472010-11-04 23:23:47 +00001026
Myles Watson29cc9ed2009-07-02 18:56:24 +00001027 /* Read the rest of the PCI configuration information. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001028 hdr_type = pci_read_config8(dev, PCI_HEADER_TYPE);
1029 class = pci_read_config32(dev, PCI_CLASS_REVISION);
Myles Watson032a9652009-05-11 22:24:53 +00001030
Myles Watson29cc9ed2009-07-02 18:56:24 +00001031 /* Store the interesting information in the device structure. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001032 dev->vendor = id & 0xffff;
1033 dev->device = (id >> 16) & 0xffff;
1034 dev->hdr_type = hdr_type;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001035
1036 /* Class code, the upper 3 bytes of PCI_CLASS_REVISION. */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001037 dev->class = class >> 8;
Myles Watson032a9652009-05-11 22:24:53 +00001038
Myles Watson29cc9ed2009-07-02 18:56:24 +00001039 /* Architectural/System devices always need to be bus masters. */
Uwe Hermanne4870472010-11-04 23:23:47 +00001040 if ((dev->class >> 16) == PCI_BASE_CLASS_SYSTEM)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001041 dev->command |= PCI_COMMAND_MASTER;
Uwe Hermanne4870472010-11-04 23:23:47 +00001042
1043 /*
1044 * Look at the vendor and device ID, or at least the header type and
Myles Watson29cc9ed2009-07-02 18:56:24 +00001045 * class and figure out which set of configuration methods to use.
1046 * Unless we already have some PCI ops.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001047 */
1048 set_pci_ops(dev);
1049
Myles Watson29cc9ed2009-07-02 18:56:24 +00001050 /* Now run the magic enable/disable sequence for the device. */
Uwe Hermanne4870472010-11-04 23:23:47 +00001051 if (dev->ops && dev->ops->enable)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001052 dev->ops->enable(dev);
Myles Watson032a9652009-05-11 22:24:53 +00001053
Myles Watson29cc9ed2009-07-02 18:56:24 +00001054 /* Display the device. */
Uwe Hermanne4870472010-11-04 23:23:47 +00001055 printk(BIOS_DEBUG, "%s [%04x/%04x] %s%s\n", dev_path(dev),
1056 dev->vendor, dev->device, dev->enabled ? "enabled" : "disabled",
1057 dev->ops ? "" : " No operations");
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001058
1059 return dev;
1060}
1061
Myles Watson032a9652009-05-11 22:24:53 +00001062/**
Kyösti Mälkkic73acdb2013-06-15 17:16:56 +03001063 * Test for match between romstage and ramstage device instance.
1064 *
1065 * @param dev Pointer to the device structure.
1066 * @param sdev Simple device model identifier, created with PCI_DEV().
1067 * @return Non-zero if bus:dev.fn of device matches.
1068 */
1069unsigned int pci_match_simple_dev(device_t dev, pci_devfn_t sdev)
1070{
1071 return dev->bus->secondary == PCI_DEV2SEGBUS(sdev) &&
1072 dev->path.pci.devfn == PCI_DEV2DEVFN(sdev);
1073}
1074
1075/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001076 * Scan a PCI bus.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001077 *
Li-Ta Loe5266692004-03-23 21:28:05 +00001078 * Determine the existence of devices and bridges on a PCI bus. If there are
1079 * bridges on the bus, recursively scan the buses behind the bridges.
1080 *
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001081 * This function is the default scan_bus() method for the root device
1082 * 'dev_root'.
1083 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001084 * @param bus Pointer to the bus structure.
1085 * @param min_devfn Minimum devfn to look at in the scan, usually 0x00.
1086 * @param max_devfn Maximum devfn to look at in the scan, usually 0xff.
1087 * @param max Current bus number.
1088 * @return The maximum bus number found, after scanning all subordinate busses.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001089 */
Uwe Hermanne4870472010-11-04 23:23:47 +00001090unsigned int pci_scan_bus(struct bus *bus, unsigned min_devfn,
1091 unsigned max_devfn, unsigned int max)
Eric Biederman8ca8d762003-04-22 19:02:15 +00001092{
1093 unsigned int devfn;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001094 struct device *old_devices;
1095 struct device *child;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001096
Stefan Reinauer08670622009-06-30 15:17:49 +00001097#if CONFIG_PCI_BUS_SEGN_BITS
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001098 printk(BIOS_DEBUG, "PCI: pci_scan_bus for bus %04x:%02x\n",
Uwe Hermanne4870472010-11-04 23:23:47 +00001099 bus->secondary >> 8, bus->secondary & 0xff);
Yinghai Lu5f9624d2006-10-04 22:56:21 +00001100#else
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001101 printk(BIOS_DEBUG, "PCI: pci_scan_bus for bus %02x\n", bus->secondary);
Yinghai Lu5f9624d2006-10-04 22:56:21 +00001102#endif
Eric Biederman8ca8d762003-04-22 19:02:15 +00001103
Uwe Hermanne4870472010-11-04 23:23:47 +00001104 /* Maximum sane devfn is 0xFF. */
Juhana Helovuo50b78b62010-09-13 14:43:02 +00001105 if (max_devfn > 0xff) {
Uwe Hermanne4870472010-11-04 23:23:47 +00001106 printk(BIOS_ERR, "PCI: pci_scan_bus limits devfn %x - "
1107 "devfn %x\n", min_devfn, max_devfn);
1108 printk(BIOS_ERR, "PCI: pci_scan_bus upper limit too big. "
1109 "Using 0xff.\n");
Juhana Helovuo50b78b62010-09-13 14:43:02 +00001110 max_devfn=0xff;
1111 }
1112
Eric Biederman8ca8d762003-04-22 19:02:15 +00001113 old_devices = bus->children;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001114 bus->children = NULL;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001115
1116 post_code(0x24);
Uwe Hermanne4870472010-11-04 23:23:47 +00001117
1118 /*
1119 * Probe all devices/functions on this bus with some optimization for
Myles Watson29cc9ed2009-07-02 18:56:24 +00001120 * non-existence and single function devices.
Eric Biedermanb78c1972004-10-14 20:54:17 +00001121 */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001122 for (devfn = min_devfn; devfn <= max_devfn; devfn++) {
Myles Watson29cc9ed2009-07-02 18:56:24 +00001123 struct device *dev;
Eric Biederman8ca8d762003-04-22 19:02:15 +00001124
Uwe Hermanne4870472010-11-04 23:23:47 +00001125 /* First thing setup the device structure. */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001126 dev = pci_scan_get_dev(&old_devices, devfn);
Li-Ta Lo9782f752004-05-05 21:15:42 +00001127
Myles Watson29cc9ed2009-07-02 18:56:24 +00001128 /* See if a device is present and setup the device structure. */
Myles Watson032a9652009-05-11 22:24:53 +00001129 dev = pci_probe_dev(dev, bus, devfn);
Eric Biederman03acab62004-10-14 21:25:53 +00001130
Uwe Hermanne4870472010-11-04 23:23:47 +00001131 /*
1132 * If this is not a multi function device, or the device is
Myles Watson29cc9ed2009-07-02 18:56:24 +00001133 * not present don't waste time probing another function.
Myles Watson032a9652009-05-11 22:24:53 +00001134 * Skip to next device.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001135 */
Uwe Hermanne4870472010-11-04 23:23:47 +00001136 if ((PCI_FUNC(devfn) == 0x00) && (!dev
Myles Watson29cc9ed2009-07-02 18:56:24 +00001137 || (dev->enabled && ((dev->hdr_type & 0x80) != 0x80)))) {
Eric Biederman8ca8d762003-04-22 19:02:15 +00001138 devfn += 0x07;
1139 }
1140 }
Uwe Hermanne4870472010-11-04 23:23:47 +00001141
Eric Biederman8ca8d762003-04-22 19:02:15 +00001142 post_code(0x25);
1143
Uwe Hermanne4870472010-11-04 23:23:47 +00001144 /*
1145 * Warn if any leftover static devices are are found.
1146 * There's probably a problem in devicetree.cb.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001147 */
1148 if (old_devices) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001149 device_t left;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001150 printk(BIOS_WARNING, "PCI: Left over static devices:\n");
Uwe Hermanne4870472010-11-04 23:23:47 +00001151 for (left = old_devices; left; left = left->sibling)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001152 printk(BIOS_WARNING, "%s\n", dev_path(left));
Uwe Hermanne4870472010-11-04 23:23:47 +00001153
1154 printk(BIOS_WARNING, "PCI: Check your devicetree.cb.\n");
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001155 }
1156
Uwe Hermanne4870472010-11-04 23:23:47 +00001157 /*
1158 * For all children that implement scan_bus() (i.e. bridges)
Eric Biedermanb78c1972004-10-14 20:54:17 +00001159 * scan the bus behind that child.
1160 */
Uwe Hermanne4870472010-11-04 23:23:47 +00001161 for (child = bus->children; child; child = child->sibling)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001162 max = scan_bus(child, max);
Li-Ta Loe5266692004-03-23 21:28:05 +00001163
Uwe Hermanne4870472010-11-04 23:23:47 +00001164 /*
1165 * We've scanned the bus and so we know all about what's on the other
Myles Watson29cc9ed2009-07-02 18:56:24 +00001166 * side of any bridges that may be on this bus plus any devices.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001167 * Return how far we've got finding sub-buses.
1168 */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001169 printk(BIOS_DEBUG, "PCI: pci_scan_bus returning with max=%03x\n", max);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001170 post_code(0x55);
1171 return max;
1172}
1173
Li-Ta Loe5266692004-03-23 21:28:05 +00001174/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001175 * Scan a PCI bridge and the buses behind the bridge.
Li-Ta Loe5266692004-03-23 21:28:05 +00001176 *
1177 * Determine the existence of buses behind the bridge. Set up the bridge
1178 * according to the result of the scan.
1179 *
1180 * This function is the default scan_bus() method for PCI bridge devices.
1181 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001182 * @param dev Pointer to the bridge device.
1183 * @param max The highest bus number assigned up to now.
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001184 * @param do_scan_bus TODO
Myles Watson29cc9ed2009-07-02 18:56:24 +00001185 * @return The maximum bus number found, after scanning all subordinate buses.
Eric Biederman8ca8d762003-04-22 19:02:15 +00001186 */
Myles Watson032a9652009-05-11 22:24:53 +00001187unsigned int do_pci_scan_bridge(struct device *dev, unsigned int max,
Myles Watson29cc9ed2009-07-02 18:56:24 +00001188 unsigned int (*do_scan_bus) (struct bus * bus,
1189 unsigned min_devfn,
1190 unsigned max_devfn,
1191 unsigned int max))
Eric Biederman8ca8d762003-04-22 19:02:15 +00001192{
Eric Biedermane9a271e32003-09-02 03:36:25 +00001193 struct bus *bus;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001194 u32 buses;
1195 u16 cr;
Eric Biederman83b991a2003-10-11 06:20:25 +00001196
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001197 printk(BIOS_SPEW, "%s for %s\n", __func__, dev_path(dev));
Li-Ta Lo3a812852004-12-03 22:39:34 +00001198
Myles Watson894a3472010-06-09 22:41:35 +00001199 if (dev->link_list == NULL) {
1200 struct bus *link;
1201 link = malloc(sizeof(*link));
1202 if (link == NULL)
1203 die("Couldn't allocate a link!\n");
1204 memset(link, 0, sizeof(*link));
1205 link->dev = dev;
1206 dev->link_list = link;
1207 }
1208
1209 bus = dev->link_list;
Eric Biedermane9a271e32003-09-02 03:36:25 +00001210
Uwe Hermanne4870472010-11-04 23:23:47 +00001211 /*
1212 * Set up the primary, secondary and subordinate bus numbers. We have
Eric Biederman8ca8d762003-04-22 19:02:15 +00001213 * no idea how many buses are behind this bridge yet, so we set the
Myles Watson032a9652009-05-11 22:24:53 +00001214 * subordinate bus number to 0xff for the moment.
Eric Biedermanb78c1972004-10-14 20:54:17 +00001215 */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001216 bus->secondary = ++max;
1217 bus->subordinate = 0xff;
Li-Ta Loe5266692004-03-23 21:28:05 +00001218
Eric Biederman8ca8d762003-04-22 19:02:15 +00001219 /* Clear all status bits and turn off memory, I/O and master enables. */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001220 cr = pci_read_config16(dev, PCI_COMMAND);
1221 pci_write_config16(dev, PCI_COMMAND, 0x0000);
1222 pci_write_config16(dev, PCI_STATUS, 0xffff);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001223
Uwe Hermanne4870472010-11-04 23:23:47 +00001224 /*
1225 * Read the existing primary/secondary/subordinate bus
Eric Biedermanb78c1972004-10-14 20:54:17 +00001226 * number configuration.
1227 */
Eric Biedermane9a271e32003-09-02 03:36:25 +00001228 buses = pci_read_config32(dev, PCI_PRIMARY_BUS);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001229
Uwe Hermanne4870472010-11-04 23:23:47 +00001230 /*
1231 * Configure the bus numbers for this bridge: the configuration
Eric Biederman8ca8d762003-04-22 19:02:15 +00001232 * transactions will not be propagated by the bridge if it is not
Eric Biedermanb78c1972004-10-14 20:54:17 +00001233 * correctly configured.
1234 */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001235 buses &= 0xff000000;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001236 buses |= (((unsigned int)(dev->bus->secondary) << 0) |
1237 ((unsigned int)(bus->secondary) << 8) |
1238 ((unsigned int)(bus->subordinate) << 16));
Eric Biedermane9a271e32003-09-02 03:36:25 +00001239 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
Li-Ta Lo3a812852004-12-03 22:39:34 +00001240
Uwe Hermanne4870472010-11-04 23:23:47 +00001241 /* Now we can scan all subordinate buses (those behind the bridge). */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001242 max = do_scan_bus(bus, 0x00, 0xff, max);
Li-Ta Lo3a812852004-12-03 22:39:34 +00001243
Uwe Hermanne4870472010-11-04 23:23:47 +00001244 /*
1245 * We know the number of buses behind this bridge. Set the subordinate
Eric Biedermanb78c1972004-10-14 20:54:17 +00001246 * bus number to its real value.
1247 */
Eric Biederman8ca8d762003-04-22 19:02:15 +00001248 bus->subordinate = max;
Myles Watson29cc9ed2009-07-02 18:56:24 +00001249 buses = (buses & 0xff00ffff) | ((unsigned int)(bus->subordinate) << 16);
Eric Biedermane9a271e32003-09-02 03:36:25 +00001250 pci_write_config32(dev, PCI_PRIMARY_BUS, buses);
1251 pci_write_config16(dev, PCI_COMMAND, cr);
Myles Watson032a9652009-05-11 22:24:53 +00001252
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001253 printk(BIOS_SPEW, "%s returns max %d\n", __func__, max);
Eric Biederman8ca8d762003-04-22 19:02:15 +00001254 return max;
1255}
Li-Ta Loe5266692004-03-23 21:28:05 +00001256
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001257/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001258 * Scan a PCI bridge and the buses behind the bridge.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001259 *
1260 * Determine the existence of buses behind the bridge. Set up the bridge
1261 * according to the result of the scan.
1262 *
1263 * This function is the default scan_bus() method for PCI bridge devices.
1264 *
Myles Watson29cc9ed2009-07-02 18:56:24 +00001265 * @param dev Pointer to the bridge device.
1266 * @param max The highest bus number assigned up to now.
1267 * @return The maximum bus number found, after scanning all subordinate buses.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001268 */
1269unsigned int pci_scan_bridge(struct device *dev, unsigned int max)
1270{
1271 return do_pci_scan_bridge(dev, max, pci_scan_bus);
1272}
1273
Myles Watson29cc9ed2009-07-02 18:56:24 +00001274/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001275 * Scan a PCI domain.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001276 *
1277 * This function is the default scan_bus() method for PCI domains.
1278 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001279 * @param dev Pointer to the domain.
1280 * @param max The highest bus number assigned up to now.
1281 * @return The maximum bus number found, after scanning all subordinate busses.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001282 */
1283unsigned int pci_domain_scan_bus(device_t dev, unsigned int max)
1284{
Myles Watson894a3472010-06-09 22:41:35 +00001285 max = pci_scan_bus(dev->link_list, PCI_DEVFN(0, 0), 0xff, max);
Myles Watson29cc9ed2009-07-02 18:56:24 +00001286 return max;
1287}
1288
Mike Loptien0f5cf5e2014-05-12 21:46:31 -06001289/**
1290 * Take an INT_PIN number (0, 1 - 4) and convert
1291 * it to a string ("NO PIN", "PIN A" - "PIN D")
1292 *
1293 * @param pin PCI Interrupt Pin number (0, 1 - 4)
1294 * @return A string corresponding to the pin number or "Invalid"
1295 */
1296const char *pin_to_str(int pin)
1297{
1298 const char *str[5] = {
1299 "NO PIN",
1300 "PIN A",
1301 "PIN B",
1302 "PIN C",
1303 "PIN D",
1304 };
1305
1306 if (pin >= 0 && pin <= 4)
1307 return str[pin];
1308 else
1309 return "Invalid PIN, not 0 - 4";
1310}
1311
1312/**
1313 * Get the PCI INT_PIN swizzle for a device defined as:
1314 * pin_parent = (pin_child + devn_child) % 4 + 1
1315 * where PIN A = 1 ... PIN_D = 4
1316 *
1317 * Given a PCI device structure 'dev', find the interrupt pin
1318 * that will be triggered on its parent bridge device when
1319 * generating an interrupt. For example: Device 1:3.2 may
1320 * use INT_PIN A but will trigger PIN D on its parent bridge
1321 * device. In this case, this function will return 4 (PIN D).
1322 *
1323 * @param dev A PCI device structure to swizzle interrupt pins for
Martin Roth32bc6b62015-01-04 16:54:35 -07001324 * @param *parent_bridge The PCI device structure for the bridge
Mike Loptien0f5cf5e2014-05-12 21:46:31 -06001325 * device 'dev' is attached to
1326 * @return The interrupt pin number (1 - 4) that 'dev' will
1327 * trigger when generating an interrupt
1328 */
1329static int swizzle_irq_pins(device_t dev, device_t *parent_bridge)
1330{
1331 device_t parent; /* Our current device's parent device */
1332 device_t child; /* The child device of the parent */
1333 uint8_t parent_bus = 0; /* Parent Bus number */
1334 uint16_t parent_devfn = 0; /* Parent Device and Function number */
1335 uint16_t child_devfn = 0; /* Child Device and Function number */
1336 uint8_t swizzled_pin = 0; /* Pin swizzled across a bridge */
1337
1338 /* Start with PIN A = 0 ... D = 3 */
1339 swizzled_pin = pci_read_config8(dev, PCI_INTERRUPT_PIN) - 1;
1340
1341 /* While our current device has parent devices */
1342 child = dev;
1343 for (parent = child->bus->dev; parent; parent = parent->bus->dev) {
1344 parent_bus = parent->bus->secondary;
1345 parent_devfn = parent->path.pci.devfn;
1346 child_devfn = child->path.pci.devfn;
1347
1348 /* Swizzle the INT_PIN for any bridges not on root bus */
1349 swizzled_pin = (PCI_SLOT(child_devfn) + swizzled_pin) % 4;
1350 printk(BIOS_SPEW, "\tWith INT_PIN swizzled to %s\n"
1351 "\tAttached to bridge device %01X:%02Xh.%02Xh\n",
1352 pin_to_str(swizzled_pin + 1), parent_bus,
1353 PCI_SLOT(parent_devfn), PCI_FUNC(parent_devfn));
1354
1355 /* Continue until we find the root bus */
1356 if (parent_bus > 0) {
1357 /*
1358 * We will go on to the next parent so this parent
1359 * becomes the child
1360 */
1361 child = parent;
1362 continue;
1363 } else {
1364 /*
1365 * Found the root bridge device,
1366 * fill in the structure and exit
1367 */
1368 *parent_bridge = parent;
1369 break;
1370 }
1371 }
1372
1373 /* End with PIN A = 1 ... D = 4 */
1374 return swizzled_pin + 1;
1375}
1376
1377/**
1378 * Given a device structure 'dev', find its interrupt pin
1379 * and its parent bridge 'parent_bdg' device structure.
1380 * If it is behind a bridge, it will return the interrupt
1381 * pin number (1 - 4) of the parent bridge that the device
1382 * interrupt pin has been swizzled to, otherwise it will
1383 * return the interrupt pin that is programmed into the
1384 * PCI config space of the target device. If 'dev' is
1385 * behind a bridge, it will fill in 'parent_bdg' with the
1386 * device structure of the bridge it is behind, otherwise
1387 * it will copy 'dev' into 'parent_bdg'.
1388 *
1389 * @param dev A PCI device structure to get interrupt pins for.
1390 * @param *parent_bdg The PCI device structure for the bridge
1391 * device 'dev' is attached to.
1392 * @return The interrupt pin number (1 - 4) that 'dev' will
1393 * trigger when generating an interrupt.
1394 * Errors: -1 is returned if the device is not enabled
1395 * -2 is returned if a parent bridge could not be found.
1396 */
1397int get_pci_irq_pins(device_t dev, device_t *parent_bdg)
1398{
1399 uint8_t bus = 0; /* The bus this device is on */
1400 uint16_t devfn = 0; /* This device's device and function numbers */
1401 uint8_t int_pin = 0; /* Interrupt pin used by the device */
1402 uint8_t target_pin = 0; /* Interrupt pin we want to assign an IRQ to */
1403
1404 /* Make sure this device is enabled */
1405 if (!(dev->enabled && (dev->path.type == DEVICE_PATH_PCI)))
1406 return -1;
1407
1408 bus = dev->bus->secondary;
1409 devfn = dev->path.pci.devfn;
1410
1411 /* Get and validate the interrupt pin used. Only 1-4 are allowed */
1412 int_pin = pci_read_config8(dev, PCI_INTERRUPT_PIN);
1413 if (int_pin < 1 || int_pin > 4)
1414 return -1;
1415
1416 printk(BIOS_SPEW, "PCI IRQ: Found device %01X:%02X.%02X using %s\n",
1417 bus, PCI_SLOT(devfn), PCI_FUNC(devfn), pin_to_str(int_pin));
1418
1419 /* If this device is on a bridge, swizzle its INT_PIN */
1420 if (bus) {
1421 /* Swizzle its INT_PINs */
1422 target_pin = swizzle_irq_pins(dev, parent_bdg);
1423
1424 /* Make sure the swizzle returned valid structures */
1425 if (parent_bdg == NULL) {
1426 printk(BIOS_WARNING,
1427 "Warning: Could not find parent bridge for this device!\n");
1428 return -2;
1429 }
1430 } else { /* Device is not behind a bridge */
1431 target_pin = int_pin; /* Return its own interrupt pin */
1432 *parent_bdg = dev; /* Return its own structure */
1433 }
1434
1435 /* Target pin is the interrupt pin we want to assign an IRQ to */
1436 return target_pin;
1437}
1438
Patrick Georgie1667822012-05-05 15:29:32 +02001439#if CONFIG_PC80_SYSTEM
Myles Watson29cc9ed2009-07-02 18:56:24 +00001440/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001441 * Assign IRQ numbers.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001442 *
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001443 * This function assigns IRQs for all functions contained within the indicated
Uwe Hermanne4870472010-11-04 23:23:47 +00001444 * device address. If the device does not exist or does not require interrupts
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001445 * then this function has no effect.
Myles Watson29cc9ed2009-07-02 18:56:24 +00001446 *
1447 * This function should be called for each PCI slot in your system.
1448 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +00001449 * @param bus Pointer to the bus structure.
1450 * @param slot TODO
1451 * @param pIntAtoD An array of IRQ #s that are assigned to PINTA through PINTD
1452 * of this slot. The particular IRQ #s that are passed in depend on the
1453 * routing inside your southbridge and on your board.
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001454 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +00001455void pci_assign_irqs(unsigned bus, unsigned slot,
Uwe Hermanne4870472010-11-04 23:23:47 +00001456 const unsigned char pIntAtoD[4])
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001457{
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001458 unsigned int funct;
1459 device_t pdev;
Uwe Hermanne4870472010-11-04 23:23:47 +00001460 u8 line, irq;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001461
Uwe Hermanne4870472010-11-04 23:23:47 +00001462 /* Each slot may contain up to eight functions. */
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001463 for (funct = 0; funct < 8; funct++) {
1464 pdev = dev_find_slot(bus, (slot << 3) + funct);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001465
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001466 if (!pdev)
1467 continue;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001468
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001469 line = pci_read_config8(pdev, PCI_INTERRUPT_PIN);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001470
Uwe Hermanne4870472010-11-04 23:23:47 +00001471 /* PCI spec says all values except 1..4 are reserved. */
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001472 if ((line < 1) || (line > 4))
1473 continue;
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001474
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001475 irq = pIntAtoD[line - 1];
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001476
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001477 printk(BIOS_DEBUG, "Assigning IRQ %d to %d:%x.%d\n",
Uwe Hermanne4870472010-11-04 23:23:47 +00001478 irq, bus, slot, funct);
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001479
Stefan Reinauer14e22772010-04-27 06:56:47 +00001480 pci_write_config8(pdev, PCI_INTERRUPT_LINE,
Uwe Hermanne4870472010-11-04 23:23:47 +00001481 pIntAtoD[line - 1]);
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001482
1483#ifdef PARANOID_IRQ_ASSIGNMENTS
Myles Watson17aeeca2009-10-07 18:41:08 +00001484 irq = pci_read_config8(pdev, PCI_INTERRUPT_LINE);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +00001485 printk(BIOS_DEBUG, " Readback = %d\n", irq);
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001486#endif
1487
Patrick Georgie1667822012-05-05 15:29:32 +02001488#if CONFIG_PC80_SYSTEM
Uwe Hermanne4870472010-11-04 23:23:47 +00001489 /* Change to level triggered. */
1490 i8259_configure_irq_trigger(pIntAtoD[line - 1],
1491 IRQ_LEVEL_TRIGGERED);
Stefan Reinauer5fb62162010-12-16 23:52:04 +00001492#endif
Ronald G. Minnich6dd6c6852003-10-02 00:08:42 +00001493 }
1494}
Stefan Reinauer4d933dd2009-07-21 21:36:41 +00001495#endif