blob: 2cacb4a9ac6227fe176eff926ae94290c6f735df [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 */
Aaron Durbine4d7abc2017-04-16 22:05:36 -050027DEVTREE_CONST struct device * DEVTREE_CONST all_devices = &dev_root;
Stefan Reinauer57879c92012-07-31 16:47:25 -070028
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 */
Aaron Durbine4d7abc2017-04-16 22:05:36 -050036DEVTREE_CONST struct device *dev_find_slot(unsigned int bus,
Stefan Reinauer57879c92012-07-31 16:47:25 -070037 unsigned int devfn)
38{
Aaron Durbine4d7abc2017-04-16 22:05:36 -050039 DEVTREE_CONST struct device *dev, *result;
Stefan Reinauer57879c92012-07-31 16:47:25 -070040
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 */
Aaron Durbine4d7abc2017-04-16 22:05:36 -050059DEVTREE_CONST struct device *dev_find_next_pci_device(
60 DEVTREE_CONST struct device *previous_dev)
Martin Roth16d953a2014-05-12 17:38:59 -060061{
Aaron Durbine4d7abc2017-04-16 22:05:36 -050062 DEVTREE_CONST struct device *dev, *result;
Martin Roth16d953a2014-05-12 17:38:59 -060063
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 */
Aaron Durbine4d7abc2017-04-16 22:05:36 -050084DEVTREE_CONST struct device *dev_find_slot_on_smbus(unsigned int bus,
Stefan Reinauer57879c92012-07-31 16:47:25 -070085 unsigned int addr)
86{
Aaron Durbine4d7abc2017-04-16 22:05:36 -050087 DEVTREE_CONST struct device *dev, *result;
Stefan Reinauer57879c92012-07-31 16:47:25 -070088
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 */
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500108DEVTREE_CONST struct device *dev_find_slot_pnp(u16 port, u16 device)
Kyösti Mälkki96643452015-01-28 22:03:46 +0200109{
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500110 DEVTREE_CONST struct device *dev;
Kyösti Mälkki96643452015-01-28 22:03:46 +0200111
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}