Angel Pons | c74dae9 | 2020-04-02 23:48:16 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* This file is part of the coreboot project. */ |
Kyösti Mälkki | 2161c1d | 2014-02-11 19:56:57 +0200 | [diff] [blame] | 3 | |
Kyösti Mälkki | 2161c1d | 2014-02-11 19:56:57 +0200 | [diff] [blame] | 4 | #include <device/pci.h> |
| 5 | #include <device/pci_def.h> |
Kyösti Mälkki | 5b14116 | 2019-01-23 16:02:28 +0200 | [diff] [blame] | 6 | #include <device/pci_ops.h> |
| 7 | #include <device/pci_type.h> |
Kyösti Mälkki | 4c686f2 | 2014-02-14 12:45:09 +0200 | [diff] [blame] | 8 | #include <delay.h> |
| 9 | |
Kyösti Mälkki | a0b366d | 2019-09-26 22:33:51 +0300 | [diff] [blame] | 10 | void pci_s_assert_secondary_reset(pci_devfn_t p2p_bridge) |
Kyösti Mälkki | 4c686f2 | 2014-02-14 12:45:09 +0200 | [diff] [blame] | 11 | { |
| 12 | u16 reg16; |
Kyösti Mälkki | a0b366d | 2019-09-26 22:33:51 +0300 | [diff] [blame] | 13 | reg16 = pci_s_read_config16(p2p_bridge, PCI_BRIDGE_CONTROL); |
| 14 | reg16 |= PCI_BRIDGE_CTL_BUS_RESET; |
| 15 | pci_s_write_config16(p2p_bridge, PCI_BRIDGE_CONTROL, reg16); |
Kyösti Mälkki | 4c686f2 | 2014-02-14 12:45:09 +0200 | [diff] [blame] | 16 | } |
| 17 | |
Kyösti Mälkki | a0b366d | 2019-09-26 22:33:51 +0300 | [diff] [blame] | 18 | void pci_s_deassert_secondary_reset(pci_devfn_t p2p_bridge) |
| 19 | { |
| 20 | u16 reg16; |
| 21 | reg16 = pci_s_read_config16(p2p_bridge, PCI_BRIDGE_CONTROL); |
| 22 | reg16 &= ~PCI_BRIDGE_CTL_BUS_RESET; |
| 23 | pci_s_write_config16(p2p_bridge, PCI_BRIDGE_CONTROL, reg16); |
| 24 | } |
| 25 | |
| 26 | void pci_s_bridge_set_secondary(pci_devfn_t p2p_bridge, u8 secondary) |
Kyösti Mälkki | 4c686f2 | 2014-02-14 12:45:09 +0200 | [diff] [blame] | 27 | { |
| 28 | /* Disable config transaction forwarding. */ |
Kyösti Mälkki | a0b366d | 2019-09-26 22:33:51 +0300 | [diff] [blame] | 29 | pci_s_write_config8(p2p_bridge, PCI_SECONDARY_BUS, 0x00); |
| 30 | pci_s_write_config8(p2p_bridge, PCI_SUBORDINATE_BUS, 0x00); |
Kyösti Mälkki | 4c686f2 | 2014-02-14 12:45:09 +0200 | [diff] [blame] | 31 | /* Enable config transaction forwarding. */ |
Kyösti Mälkki | a0b366d | 2019-09-26 22:33:51 +0300 | [diff] [blame] | 32 | pci_s_write_config8(p2p_bridge, PCI_SECONDARY_BUS, secondary); |
| 33 | pci_s_write_config8(p2p_bridge, PCI_SUBORDINATE_BUS, secondary); |
Kyösti Mälkki | 4c686f2 | 2014-02-14 12:45:09 +0200 | [diff] [blame] | 34 | } |
| 35 | |
Kyösti Mälkki | a0b366d | 2019-09-26 22:33:51 +0300 | [diff] [blame] | 36 | static void pci_s_bridge_set_mmio(pci_devfn_t p2p_bridge, u32 base, u32 size) |
Kyösti Mälkki | 4c686f2 | 2014-02-14 12:45:09 +0200 | [diff] [blame] | 37 | { |
| 38 | u16 reg16; |
| 39 | |
| 40 | /* Disable MMIO window behind the bridge. */ |
Kyösti Mälkki | a0b366d | 2019-09-26 22:33:51 +0300 | [diff] [blame] | 41 | reg16 = pci_s_read_config16(p2p_bridge, PCI_COMMAND); |
Kyösti Mälkki | 4c686f2 | 2014-02-14 12:45:09 +0200 | [diff] [blame] | 42 | reg16 &= ~PCI_COMMAND_MEMORY; |
Kyösti Mälkki | a0b366d | 2019-09-26 22:33:51 +0300 | [diff] [blame] | 43 | pci_s_write_config16(p2p_bridge, PCI_COMMAND, reg16); |
| 44 | pci_s_write_config32(p2p_bridge, PCI_MEMORY_BASE, 0x10); |
Kyösti Mälkki | 4c686f2 | 2014-02-14 12:45:09 +0200 | [diff] [blame] | 45 | |
| 46 | if (!size) |
| 47 | return; |
| 48 | |
| 49 | /* Enable MMIO window behind the bridge. */ |
Kyösti Mälkki | a0b366d | 2019-09-26 22:33:51 +0300 | [diff] [blame] | 50 | pci_s_write_config32(p2p_bridge, PCI_MEMORY_BASE, |
Kyösti Mälkki | 4c686f2 | 2014-02-14 12:45:09 +0200 | [diff] [blame] | 51 | ((base + size - 1) & 0xfff00000) | ((base >> 16) & 0xfff0)); |
| 52 | |
Kyösti Mälkki | a0b366d | 2019-09-26 22:33:51 +0300 | [diff] [blame] | 53 | reg16 = pci_s_read_config16(p2p_bridge, PCI_COMMAND); |
Kyösti Mälkki | 4c686f2 | 2014-02-14 12:45:09 +0200 | [diff] [blame] | 54 | reg16 |= PCI_COMMAND_MEMORY; |
Kyösti Mälkki | a0b366d | 2019-09-26 22:33:51 +0300 | [diff] [blame] | 55 | pci_s_write_config16(p2p_bridge, PCI_COMMAND, reg16); |
Kyösti Mälkki | 4c686f2 | 2014-02-14 12:45:09 +0200 | [diff] [blame] | 56 | } |
| 57 | |
Kyösti Mälkki | a0b366d | 2019-09-26 22:33:51 +0300 | [diff] [blame] | 58 | static void pci_s_early_mmio_window(pci_devfn_t p2p_bridge, u32 mmio_base, u32 mmio_size) |
Kyösti Mälkki | 4c686f2 | 2014-02-14 12:45:09 +0200 | [diff] [blame] | 59 | { |
| 60 | int timeout, ret = -1; |
| 61 | |
Kyösti Mälkki | 4c686f2 | 2014-02-14 12:45:09 +0200 | [diff] [blame] | 62 | /* Secondary bus number is mostly irrelevant as we disable |
| 63 | * configuration transactions right after the probe. |
| 64 | */ |
| 65 | u8 secondary = 15; |
| 66 | u8 dev = 0; |
Kyösti Mälkki | 4c686f2 | 2014-02-14 12:45:09 +0200 | [diff] [blame] | 67 | |
| 68 | /* Enable configuration and MMIO over bridge. */ |
Kyösti Mälkki | a0b366d | 2019-09-26 22:33:51 +0300 | [diff] [blame] | 69 | pci_s_assert_secondary_reset(p2p_bridge); |
| 70 | pci_s_deassert_secondary_reset(p2p_bridge); |
| 71 | pci_s_bridge_set_secondary(p2p_bridge, secondary); |
| 72 | pci_s_bridge_set_mmio(p2p_bridge, mmio_base, mmio_size); |
Kyösti Mälkki | 4c686f2 | 2014-02-14 12:45:09 +0200 | [diff] [blame] | 73 | |
| 74 | for (timeout = 20000; timeout; timeout--) { |
Kyösti Mälkki | a0b366d | 2019-09-26 22:33:51 +0300 | [diff] [blame] | 75 | pci_devfn_t dbg_dev = PCI_DEV(secondary, dev, 0); |
| 76 | u32 id = pci_s_read_config32(dbg_dev, PCI_VENDOR_ID); |
Kyösti Mälkki | 4c686f2 | 2014-02-14 12:45:09 +0200 | [diff] [blame] | 77 | if (id != 0 && id != 0xffffffff && id != 0xffff0001) |
| 78 | break; |
| 79 | udelay(10); |
| 80 | } |
| 81 | |
| 82 | if (timeout != 0) |
| 83 | ret = pci_early_device_probe(secondary, dev, mmio_base); |
| 84 | |
| 85 | /* Disable MMIO window if we found no suitable device. */ |
| 86 | if (ret) |
Kyösti Mälkki | a0b366d | 2019-09-26 22:33:51 +0300 | [diff] [blame] | 87 | pci_s_bridge_set_mmio(p2p_bridge, 0, 0); |
Kyösti Mälkki | 4c686f2 | 2014-02-14 12:45:09 +0200 | [diff] [blame] | 88 | |
| 89 | /* Resource allocator will reconfigure bridges and secondary bus |
| 90 | * number may change. Thus early device cannot reliably use config |
| 91 | * transactions from here on, so we may as well disable them. |
| 92 | */ |
Kyösti Mälkki | a0b366d | 2019-09-26 22:33:51 +0300 | [diff] [blame] | 93 | pci_s_bridge_set_secondary(p2p_bridge, 0); |
Kyösti Mälkki | 4c686f2 | 2014-02-14 12:45:09 +0200 | [diff] [blame] | 94 | } |
Kyösti Mälkki | 3521e26 | 2018-12-26 19:33:28 +0200 | [diff] [blame] | 95 | |
| 96 | void pci_early_bridge_init(void) |
| 97 | { |
| 98 | /* No PCI-to-PCI bridges are enabled yet, so the one we try to |
| 99 | * configure must have its primary on bus 0. |
| 100 | */ |
| 101 | pci_devfn_t p2p_bridge = PCI_DEV(0, CONFIG_EARLY_PCI_BRIDGE_DEVICE, |
| 102 | CONFIG_EARLY_PCI_BRIDGE_FUNCTION); |
| 103 | |
Kyösti Mälkki | a0b366d | 2019-09-26 22:33:51 +0300 | [diff] [blame] | 104 | pci_s_early_mmio_window(p2p_bridge, CONFIG_EARLY_PCI_MMIO_BASE, 0x4000); |
Kyösti Mälkki | 3521e26 | 2018-12-26 19:33:28 +0200 | [diff] [blame] | 105 | } |
Kyösti Mälkki | 8a41f4b | 2019-02-08 18:14:34 +0200 | [diff] [blame] | 106 | |
| 107 | /* FIXME: A lot of issues using the following, please avoid. |
| 108 | * Assumes 256 PCI busses, scans them all even when PCI bridges are still |
| 109 | * disabled. Probes all functions even if 0 is not present. |
| 110 | */ |
| 111 | pci_devfn_t pci_locate_device(unsigned int pci_id, pci_devfn_t dev) |
| 112 | { |
| 113 | for (; dev <= PCI_DEV(255, 31, 7); dev += PCI_DEV(0, 0, 1)) { |
| 114 | unsigned int id; |
Kyösti Mälkki | a0b366d | 2019-09-26 22:33:51 +0300 | [diff] [blame] | 115 | id = pci_s_read_config32(dev, 0); |
Kyösti Mälkki | 8a41f4b | 2019-02-08 18:14:34 +0200 | [diff] [blame] | 116 | if (id == pci_id) |
| 117 | return dev; |
| 118 | } |
| 119 | return PCI_DEV_INVALID; |
| 120 | } |
| 121 | |
| 122 | pci_devfn_t pci_locate_device_on_bus(unsigned int pci_id, unsigned int bus) |
| 123 | { |
| 124 | pci_devfn_t dev, last; |
| 125 | |
| 126 | dev = PCI_DEV(bus, 0, 0); |
| 127 | last = PCI_DEV(bus, 31, 7); |
| 128 | |
| 129 | for (; dev <= last; dev += PCI_DEV(0, 0, 1)) { |
| 130 | unsigned int id; |
Kyösti Mälkki | a0b366d | 2019-09-26 22:33:51 +0300 | [diff] [blame] | 131 | id = pci_s_read_config32(dev, 0); |
Kyösti Mälkki | 8a41f4b | 2019-02-08 18:14:34 +0200 | [diff] [blame] | 132 | if (id == pci_id) |
| 133 | return dev; |
| 134 | } |
| 135 | return PCI_DEV_INVALID; |
| 136 | } |