blob: ee64bd13e6047b8d22290b3ab81ff441c3a5ef62 [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
Kyösti Mälkki2df1c122018-05-21 01:52:10 +030021#include <console/console.h>
Stefan Reinauer57879c92012-07-31 16:47:25 -070022#include <device/device.h>
23#include <device/path.h>
24#include <device/pci.h>
25#include <device/resource.h>
26
27/** Linked list of ALL devices */
Aaron Durbine4d7abc2017-04-16 22:05:36 -050028DEVTREE_CONST struct device * DEVTREE_CONST all_devices = &dev_root;
Stefan Reinauer57879c92012-07-31 16:47:25 -070029
30/**
31 * Given a PCI bus and a devfn number, find the device structure.
32 *
33 * @param bus The bus number.
34 * @param devfn A device/function number.
35 * @return Pointer to the device structure (if found), 0 otherwise.
36 */
Aaron Durbine4d7abc2017-04-16 22:05:36 -050037DEVTREE_CONST struct device *dev_find_slot(unsigned int bus,
Stefan Reinauer57879c92012-07-31 16:47:25 -070038 unsigned int devfn)
39{
Aaron Durbine4d7abc2017-04-16 22:05:36 -050040 DEVTREE_CONST struct device *dev, *result;
Stefan Reinauer57879c92012-07-31 16:47:25 -070041
42 result = 0;
43 for (dev = all_devices; dev; dev = dev->next) {
44 if ((dev->path.type == DEVICE_PATH_PCI) &&
45 (dev->bus->secondary == bus) &&
46 (dev->path.pci.devfn == devfn)) {
47 result = dev;
48 break;
49 }
50 }
51 return result;
52}
53
54/**
Nico Huberf6a43442018-05-15 14:13:32 +020055 * Given a Device Path Type, find the device structure.
56 *
57 * @param prev_match The previously matched device instance.
58 * @param path_type The Device Path Type.
59 * @return Pointer to the device structure (if found), 0 otherwise.
60 */
61DEVTREE_CONST struct device *dev_find_path(
62 DEVTREE_CONST struct device *prev_match,
63 enum device_path_type path_type)
64{
65 DEVTREE_CONST struct device *dev, *result = NULL;
66
67 if (prev_match == NULL)
68 prev_match = all_devices;
69 else
70 prev_match = prev_match->next;
71
72 for (dev = prev_match; dev; dev = dev->next) {
73 if (dev->path.type == path_type) {
74 result = dev;
75 break;
76 }
77 }
78 return result;
79}
80
81/**
Martin Roth16d953a2014-05-12 17:38:59 -060082 * Given a device pointer, find the next PCI device.
83 *
84 * @param previous_dev A pointer to a PCI device structure.
85 * @return Pointer to the next device structure (if found), 0 otherwise.
86 */
Aaron Durbine4d7abc2017-04-16 22:05:36 -050087DEVTREE_CONST struct device *dev_find_next_pci_device(
88 DEVTREE_CONST struct device *previous_dev)
Martin Roth16d953a2014-05-12 17:38:59 -060089{
Nico Huberf6a43442018-05-15 14:13:32 +020090 return dev_find_path(previous_dev, DEVICE_PATH_PCI);
Martin Roth16d953a2014-05-12 17:38:59 -060091}
92
Kyösti Mälkki2df1c122018-05-21 01:52:10 +030093static int path_eq(const struct device_path *path1,
94 const struct device_path *path2)
95{
96 int equal = 0;
97
98 if (path1->type != path2->type)
99 return 0;
100
101 switch (path1->type) {
102 case DEVICE_PATH_NONE:
103 break;
104 case DEVICE_PATH_ROOT:
105 equal = 1;
106 break;
107 case DEVICE_PATH_PCI:
108 equal = (path1->pci.devfn == path2->pci.devfn);
109 break;
110 case DEVICE_PATH_PNP:
111 equal = (path1->pnp.port == path2->pnp.port) &&
112 (path1->pnp.device == path2->pnp.device);
113 break;
114 case DEVICE_PATH_I2C:
115 equal = (path1->i2c.device == path2->i2c.device) &&
116 (path1->i2c.mode_10bit == path2->i2c.mode_10bit);
117 break;
118 case DEVICE_PATH_APIC:
119 equal = (path1->apic.apic_id == path2->apic.apic_id);
120 break;
121 case DEVICE_PATH_DOMAIN:
122 equal = (path1->domain.domain == path2->domain.domain);
123 break;
124 case DEVICE_PATH_CPU_CLUSTER:
125 equal = (path1->cpu_cluster.cluster
126 == path2->cpu_cluster.cluster);
127 break;
128 case DEVICE_PATH_CPU:
129 equal = (path1->cpu.id == path2->cpu.id);
130 break;
131 case DEVICE_PATH_CPU_BUS:
132 equal = (path1->cpu_bus.id == path2->cpu_bus.id);
133 break;
134 case DEVICE_PATH_GENERIC:
135 equal = (path1->generic.id == path2->generic.id) &&
136 (path1->generic.subid == path2->generic.subid);
137 break;
138 case DEVICE_PATH_SPI:
139 equal = (path1->spi.cs == path2->spi.cs);
140 break;
141 case DEVICE_PATH_USB:
142 equal = (path1->usb.port_type == path2->usb.port_type) &&
143 (path1->usb.port_id == path2->usb.port_id);
144 break;
145 case DEVICE_PATH_MMIO:
146 equal = (path1->mmio.addr == path2->mmio.addr);
147 break;
148 default:
149 printk(BIOS_ERR, "Unknown device type: %d\n", path1->type);
150 break;
151 }
152
153 return equal;
154}
155
156/**
157 * See if a device structure exists for path.
158 *
159 * @param parent The bus to find the device on.
160 * @param path The relative path from the bus to the appropriate device.
161 * @return Pointer to a device structure for the device on bus at path
162 * or 0/NULL if no device is found.
163 */
164DEVTREE_CONST struct device *find_dev_path(
165 const struct bus *parent, const struct device_path *path)
166{
167 DEVTREE_CONST struct device *child;
168 for (child = parent->children; child; child = child->sibling) {
169 if (path_eq(path, &child->path))
170 break;
171 }
172 return child;
173}
174
Martin Roth16d953a2014-05-12 17:38:59 -0600175/**
Stefan Reinauer57879c92012-07-31 16:47:25 -0700176 * Given an SMBus bus and a device number, find the device structure.
177 *
178 * @param bus The bus number.
179 * @param addr A device number.
180 * @return Pointer to the device structure (if found), 0 otherwise.
181 */
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500182DEVTREE_CONST struct device *dev_find_slot_on_smbus(unsigned int bus,
Stefan Reinauer57879c92012-07-31 16:47:25 -0700183 unsigned int addr)
184{
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500185 DEVTREE_CONST struct device *dev, *result;
Stefan Reinauer57879c92012-07-31 16:47:25 -0700186
187 result = 0;
188 for (dev = all_devices; dev; dev = dev->next) {
189 if ((dev->path.type == DEVICE_PATH_I2C) &&
190 (dev->bus->secondary == bus) &&
191 (dev->path.i2c.device == addr)) {
192 result = dev;
193 break;
194 }
195 }
196 return result;
197}
Kyösti Mälkki96643452015-01-28 22:03:46 +0200198
199/**
200 * Given a PnP port and a device number, find the device structure.
201 *
202 * @param port The I/O port.
203 * @param device Logical device number.
204 * @return Pointer to the device structure (if found), 0 otherwise.
205 */
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500206DEVTREE_CONST struct device *dev_find_slot_pnp(u16 port, u16 device)
Kyösti Mälkki96643452015-01-28 22:03:46 +0200207{
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500208 DEVTREE_CONST struct device *dev;
Kyösti Mälkki96643452015-01-28 22:03:46 +0200209
210 for (dev = all_devices; dev; dev = dev->next) {
211 if ((dev->path.type == DEVICE_PATH_PNP) &&
212 (dev->path.pnp.port == port) &&
213 (dev->path.pnp.device == device)) {
214 return dev;
215 }
216 }
217 return 0;
218}