blob: 828e99b75ed87144cc89b637e1ac81b699415122 [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.
Stefan Reinauer57879c92012-07-31 16:47:25 -070019 */
20
21#include <device/device.h>
22#include <device/path.h>
23#include <device/pci.h>
24#include <device/resource.h>
25
26/** Linked list of ALL devices */
27ROMSTAGE_CONST struct device * ROMSTAGE_CONST all_devices = &dev_root;
28
29/**
30 * Given a PCI bus and a devfn number, find the device structure.
31 *
32 * @param bus The bus number.
33 * @param devfn A device/function number.
34 * @return Pointer to the device structure (if found), 0 otherwise.
35 */
36ROMSTAGE_CONST struct device *dev_find_slot(unsigned int bus,
37 unsigned int devfn)
38{
39 ROMSTAGE_CONST struct device *dev, *result;
40
41 result = 0;
42 for (dev = all_devices; dev; dev = dev->next) {
43 if ((dev->path.type == DEVICE_PATH_PCI) &&
44 (dev->bus->secondary == bus) &&
45 (dev->path.pci.devfn == devfn)) {
46 result = dev;
47 break;
48 }
49 }
50 return result;
51}
52
53/**
Martin Roth16d953a2014-05-12 17:38:59 -060054 * Given a device pointer, find the next PCI device.
55 *
56 * @param previous_dev A pointer to a PCI device structure.
57 * @return Pointer to the next device structure (if found), 0 otherwise.
58 */
59ROMSTAGE_CONST struct device *dev_find_next_pci_device(
60 ROMSTAGE_CONST struct device *previous_dev)
61{
62 ROMSTAGE_CONST struct device *dev, *result;
63
64 if (previous_dev == NULL)
65 previous_dev = all_devices;
66
67 result = 0;
68 for (dev = previous_dev->next; dev; dev = dev->next) {
69 if (dev->path.type == DEVICE_PATH_PCI) {
70 result = dev;
71 break;
72 }
73 }
74 return result;
75}
76
77/**
Stefan Reinauer57879c92012-07-31 16:47:25 -070078 * Given an SMBus bus and a device number, find the device structure.
79 *
80 * @param bus The bus number.
81 * @param addr A device number.
82 * @return Pointer to the device structure (if found), 0 otherwise.
83 */
84ROMSTAGE_CONST struct device *dev_find_slot_on_smbus(unsigned int bus,
85 unsigned int addr)
86{
87 ROMSTAGE_CONST struct device *dev, *result;
88
89 result = 0;
90 for (dev = all_devices; dev; dev = dev->next) {
91 if ((dev->path.type == DEVICE_PATH_I2C) &&
92 (dev->bus->secondary == bus) &&
93 (dev->path.i2c.device == addr)) {
94 result = dev;
95 break;
96 }
97 }
98 return result;
99}
Kyösti Mälkki96643452015-01-28 22:03:46 +0200100
101/**
102 * Given a PnP port and a device number, find the device structure.
103 *
104 * @param port The I/O port.
105 * @param device Logical device number.
106 * @return Pointer to the device structure (if found), 0 otherwise.
107 */
108ROMSTAGE_CONST struct device *dev_find_slot_pnp(u16 port, u16 device)
109{
110 ROMSTAGE_CONST struct device *dev;
111
112 for (dev = all_devices; dev; dev = dev->next) {
113 if ((dev->path.type == DEVICE_PATH_PNP) &&
114 (dev->path.pnp.port == port) &&
115 (dev->path.pnp.device == device)) {
116 return dev;
117 }
118 }
119 return 0;
120}