Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 1 | /* |
| 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 Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 19 | */ |
| 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 Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 27 | DEVTREE_CONST struct device * DEVTREE_CONST all_devices = &dev_root; |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 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 | */ |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 36 | DEVTREE_CONST struct device *dev_find_slot(unsigned int bus, |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 37 | unsigned int devfn) |
| 38 | { |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 39 | DEVTREE_CONST struct device *dev, *result; |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 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 Roth | 16d953a | 2014-05-12 17:38:59 -0600 | [diff] [blame] | 54 | * 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 Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 59 | DEVTREE_CONST struct device *dev_find_next_pci_device( |
| 60 | DEVTREE_CONST struct device *previous_dev) |
Martin Roth | 16d953a | 2014-05-12 17:38:59 -0600 | [diff] [blame] | 61 | { |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 62 | DEVTREE_CONST struct device *dev, *result; |
Martin Roth | 16d953a | 2014-05-12 17:38:59 -0600 | [diff] [blame] | 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 Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 78 | * 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 Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 84 | DEVTREE_CONST struct device *dev_find_slot_on_smbus(unsigned int bus, |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 85 | unsigned int addr) |
| 86 | { |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 87 | DEVTREE_CONST struct device *dev, *result; |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 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älkki | 9664345 | 2015-01-28 22:03:46 +0200 | [diff] [blame] | 100 | |
| 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 Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 108 | DEVTREE_CONST struct device *dev_find_slot_pnp(u16 port, u16 device) |
Kyösti Mälkki | 9664345 | 2015-01-28 22:03:46 +0200 | [diff] [blame] | 109 | { |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 110 | DEVTREE_CONST struct device *dev; |
Kyösti Mälkki | 9664345 | 2015-01-28 22:03:46 +0200 | [diff] [blame] | 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 | } |