blob: d47a3eb505705d96d8e5ee158515502223ea2e4e [file] [log] [blame]
Angel Ponsc74dae92020-04-02 23:48:16 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Kyösti Mälkkid2cdfff2019-03-05 07:56:38 +02003
4#include <stdint.h>
Kyösti Mälkki3f98d412019-07-29 16:38:14 +03005#include <console/console.h>
Kyösti Mälkki9c0e14e2019-01-23 16:46:35 +02006#include <device/pci.h>
7#include <device/pci_def.h>
8#include <device/pci_ops.h>
9#include <device/pci_type.h>
Kyösti Mälkkid2cdfff2019-03-05 07:56:38 +020010
11u8 *const pci_mmconf = (void *)(uintptr_t)CONFIG_MMCONF_BASE_ADDRESS;
Kyösti Mälkki9c0e14e2019-01-23 16:46:35 +020012
13/**
14 * Given a device, a capability type, and a last position, return the next
15 * matching capability. Always start at the head of the list.
16 *
17 * @param dev Pointer to the device structure.
18 * @param cap PCI_CAP_LIST_ID of the PCI capability we're looking for.
19 * @param last Location of the PCI capability register to start from.
20 * @return The next matching capability.
21 */
22u16 pci_s_find_next_capability(pci_devfn_t dev, u16 cap, u16 last)
23{
24 u16 pos = 0;
25 u16 status;
26 int reps = 48;
27
28 status = pci_s_read_config16(dev, PCI_STATUS);
29 if (!(status & PCI_STATUS_CAP_LIST))
30 return 0;
31
32 u8 hdr_type = pci_s_read_config8(dev, PCI_HEADER_TYPE);
33 switch (hdr_type & 0x7f) {
34 case PCI_HEADER_TYPE_NORMAL:
35 case PCI_HEADER_TYPE_BRIDGE:
36 pos = PCI_CAPABILITY_LIST;
37 break;
38 case PCI_HEADER_TYPE_CARDBUS:
39 pos = PCI_CB_CAPABILITY_LIST;
40 break;
41 default:
42 return 0;
43 }
44
45 pos = pci_s_read_config8(dev, pos);
46 while (reps-- && (pos >= 0x40)) { /* Loop through the linked list. */
47 int this_cap;
48
49 pos &= ~3;
50 this_cap = pci_s_read_config8(dev, pos + PCI_CAP_LIST_ID);
51 if (this_cap == 0xff)
52 break;
53
54 if (!last && (this_cap == cap))
55 return pos;
56
57 if (last == pos)
58 last = 0;
59
60 pos = pci_s_read_config8(dev, pos + PCI_CAP_LIST_NEXT);
61 }
62 return 0;
63}
64
65/**
66 * Given a device, and a capability type, return the next matching
67 * capability. Always start at the head of the list.
68 *
69 * @param dev Pointer to the device structure.
70 * @param cap PCI_CAP_LIST_ID of the PCI capability we're looking for.
71 * @return The next matching capability.
72 */
73u16 pci_s_find_capability(pci_devfn_t dev, u16 cap)
74{
75 return pci_s_find_next_capability(dev, cap, 0);
76}
Kyösti Mälkki3f98d412019-07-29 16:38:14 +030077
78void __noreturn pcidev_die(void)
79{
80 die("PCI: dev is NULL!\n");
81}