blob: 084d2ac385c96597d5f9dce3274b0d97ae821404 [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/**
Nico Huberf6a43442018-05-15 14:13:32 +020054 * Given a Device Path Type, find the device structure.
55 *
56 * @param prev_match The previously matched device instance.
57 * @param path_type The Device Path Type.
58 * @return Pointer to the device structure (if found), 0 otherwise.
59 */
60DEVTREE_CONST struct device *dev_find_path(
61 DEVTREE_CONST struct device *prev_match,
62 enum device_path_type path_type)
63{
64 DEVTREE_CONST struct device *dev, *result = NULL;
65
66 if (prev_match == NULL)
67 prev_match = all_devices;
68 else
69 prev_match = prev_match->next;
70
71 for (dev = prev_match; dev; dev = dev->next) {
72 if (dev->path.type == path_type) {
73 result = dev;
74 break;
75 }
76 }
77 return result;
78}
79
80/**
Martin Roth16d953a2014-05-12 17:38:59 -060081 * Given a device pointer, find the next PCI device.
82 *
83 * @param previous_dev A pointer to a PCI device structure.
84 * @return Pointer to the next device structure (if found), 0 otherwise.
85 */
Aaron Durbine4d7abc2017-04-16 22:05:36 -050086DEVTREE_CONST struct device *dev_find_next_pci_device(
87 DEVTREE_CONST struct device *previous_dev)
Martin Roth16d953a2014-05-12 17:38:59 -060088{
Nico Huberf6a43442018-05-15 14:13:32 +020089 return dev_find_path(previous_dev, DEVICE_PATH_PCI);
Martin Roth16d953a2014-05-12 17:38:59 -060090}
91
92/**
Stefan Reinauer57879c92012-07-31 16:47:25 -070093 * Given an SMBus bus and a device number, find the device structure.
94 *
95 * @param bus The bus number.
96 * @param addr A device number.
97 * @return Pointer to the device structure (if found), 0 otherwise.
98 */
Aaron Durbine4d7abc2017-04-16 22:05:36 -050099DEVTREE_CONST struct device *dev_find_slot_on_smbus(unsigned int bus,
Stefan Reinauer57879c92012-07-31 16:47:25 -0700100 unsigned int addr)
101{
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500102 DEVTREE_CONST struct device *dev, *result;
Stefan Reinauer57879c92012-07-31 16:47:25 -0700103
104 result = 0;
105 for (dev = all_devices; dev; dev = dev->next) {
106 if ((dev->path.type == DEVICE_PATH_I2C) &&
107 (dev->bus->secondary == bus) &&
108 (dev->path.i2c.device == addr)) {
109 result = dev;
110 break;
111 }
112 }
113 return result;
114}
Kyösti Mälkki96643452015-01-28 22:03:46 +0200115
116/**
117 * Given a PnP port and a device number, find the device structure.
118 *
119 * @param port The I/O port.
120 * @param device Logical device number.
121 * @return Pointer to the device structure (if found), 0 otherwise.
122 */
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500123DEVTREE_CONST struct device *dev_find_slot_pnp(u16 port, u16 device)
Kyösti Mälkki96643452015-01-28 22:03:46 +0200124{
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500125 DEVTREE_CONST struct device *dev;
Kyösti Mälkki96643452015-01-28 22:03:46 +0200126
127 for (dev = all_devices; dev; dev = dev->next) {
128 if ((dev->path.type == DEVICE_PATH_PNP) &&
129 (dev->path.pnp.port == port) &&
130 (dev->path.pnp.device == device)) {
131 return dev;
132 }
133 }
134 return 0;
135}