blob: df0df6a529f94412bbf6a40f43903ce1008f6c44 [file] [log] [blame]
Stefan Reinauer57879c92012-07-31 16:47:25 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2003-2004 Linux Networx
5 * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
6 * Copyright (C) 2003 Greg Watson <jarrah@users.sourceforge.net>
7 * Copyright (C) 2004 Li-Ta Lo <ollie@lanl.gov>
8 * Copyright (C) 2005-2006 Tyan
9 * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan)
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
Paul Menzela46a7122013-02-23 18:37:27 +010022 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Stefan Reinauer57879c92012-07-31 16:47:25 -070023 */
24
25#include <device/device.h>
26#include <device/path.h>
27#include <device/pci.h>
28#include <device/resource.h>
29
30/** Linked list of ALL devices */
31ROMSTAGE_CONST struct device * ROMSTAGE_CONST all_devices = &dev_root;
32
33/**
34 * Given a PCI bus and a devfn number, find the device structure.
35 *
36 * @param bus The bus number.
37 * @param devfn A device/function number.
38 * @return Pointer to the device structure (if found), 0 otherwise.
39 */
40ROMSTAGE_CONST struct device *dev_find_slot(unsigned int bus,
41 unsigned int devfn)
42{
43 ROMSTAGE_CONST struct device *dev, *result;
44
45 result = 0;
46 for (dev = all_devices; dev; dev = dev->next) {
47 if ((dev->path.type == DEVICE_PATH_PCI) &&
48 (dev->bus->secondary == bus) &&
49 (dev->path.pci.devfn == devfn)) {
50 result = dev;
51 break;
52 }
53 }
54 return result;
55}
56
57/**
Martin Roth16d953a2014-05-12 17:38:59 -060058 * Given a device pointer, find the next PCI device.
59 *
60 * @param previous_dev A pointer to a PCI device structure.
61 * @return Pointer to the next device structure (if found), 0 otherwise.
62 */
63ROMSTAGE_CONST struct device *dev_find_next_pci_device(
64 ROMSTAGE_CONST struct device *previous_dev)
65{
66 ROMSTAGE_CONST struct device *dev, *result;
67
68 if (previous_dev == NULL)
69 previous_dev = all_devices;
70
71 result = 0;
72 for (dev = previous_dev->next; dev; dev = dev->next) {
73 if (dev->path.type == DEVICE_PATH_PCI) {
74 result = dev;
75 break;
76 }
77 }
78 return result;
79}
80
81/**
Stefan Reinauer57879c92012-07-31 16:47:25 -070082 * Given an SMBus bus and a device number, find the device structure.
83 *
84 * @param bus The bus number.
85 * @param addr A device number.
86 * @return Pointer to the device structure (if found), 0 otherwise.
87 */
88ROMSTAGE_CONST struct device *dev_find_slot_on_smbus(unsigned int bus,
89 unsigned int addr)
90{
91 ROMSTAGE_CONST struct device *dev, *result;
92
93 result = 0;
94 for (dev = all_devices; dev; dev = dev->next) {
95 if ((dev->path.type == DEVICE_PATH_I2C) &&
96 (dev->bus->secondary == bus) &&
97 (dev->path.i2c.device == addr)) {
98 result = dev;
99 break;
100 }
101 }
102 return result;
103}
Kyösti Mälkki96643452015-01-28 22:03:46 +0200104
105/**
106 * Given a PnP port and a device number, find the device structure.
107 *
108 * @param port The I/O port.
109 * @param device Logical device number.
110 * @return Pointer to the device structure (if found), 0 otherwise.
111 */
112ROMSTAGE_CONST struct device *dev_find_slot_pnp(u16 port, u16 device)
113{
114 ROMSTAGE_CONST struct device *dev;
115
116 for (dev = all_devices; dev; dev = dev->next) {
117 if ((dev->path.type == DEVICE_PATH_PNP) &&
118 (dev->path.pnp.port == port) &&
119 (dev->path.pnp.device == device)) {
120 return dev;
121 }
122 }
123 return 0;
124}