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