Simon Glass | d7db004 | 2019-12-08 17:40:16 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Primary-to-Sideband Bridge |
| 4 | * |
| 5 | * Copyright 2019 Google LLC |
| 6 | */ |
| 7 | |
| 8 | #define LOG_CATEGORY UCLASS_P2SB |
| 9 | |
| 10 | #include <common.h> |
| 11 | #include <dm.h> |
| 12 | #include <dt-structs.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | d7db004 | 2019-12-08 17:40:16 -0700 | [diff] [blame] | 14 | #include <p2sb.h> |
| 15 | #include <spl.h> |
| 16 | #include <asm/pci.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 17 | #include <linux/bitops.h> |
Simon Glass | d7db004 | 2019-12-08 17:40:16 -0700 | [diff] [blame] | 18 | |
Simon Glass | 459a474 | 2020-07-07 21:32:31 -0600 | [diff] [blame] | 19 | #define PCH_P2SB_E0 0xe0 |
| 20 | #define HIDE_BIT BIT(0) |
| 21 | |
Simon Glass | d7db004 | 2019-12-08 17:40:16 -0700 | [diff] [blame] | 22 | struct p2sb_platdata { |
| 23 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
Wolfgang Wallner | 949a26e | 2020-02-18 15:32:10 +0100 | [diff] [blame] | 24 | struct dtd_intel_p2sb dtplat; |
Simon Glass | d7db004 | 2019-12-08 17:40:16 -0700 | [diff] [blame] | 25 | #endif |
| 26 | ulong mmio_base; |
| 27 | pci_dev_t bdf; |
| 28 | }; |
| 29 | |
| 30 | /* PCI config space registers */ |
| 31 | #define HPTC_OFFSET 0x60 |
| 32 | #define HPTC_ADDR_ENABLE_BIT BIT(7) |
| 33 | |
| 34 | /* High Performance Event Timer Configuration */ |
| 35 | #define P2SB_HPTC 0x60 |
| 36 | #define P2SB_HPTC_ADDRESS_ENABLE BIT(7) |
| 37 | |
| 38 | /* |
| 39 | * ADDRESS_SELECT ENCODING_RANGE |
| 40 | * 0 0xfed0 0000 - 0xfed0 03ff |
| 41 | * 1 0xfed0 1000 - 0xfed0 13ff |
| 42 | * 2 0xfed0 2000 - 0xfed0 23ff |
| 43 | * 3 0xfed0 3000 - 0xfed0 33ff |
| 44 | */ |
| 45 | #define P2SB_HPTC_ADDRESS_SELECT_0 (0 << 0) |
| 46 | #define P2SB_HPTC_ADDRESS_SELECT_1 (1 << 0) |
| 47 | #define P2SB_HPTC_ADDRESS_SELECT_2 (2 << 0) |
| 48 | #define P2SB_HPTC_ADDRESS_SELECT_3 (3 << 0) |
| 49 | |
| 50 | /* |
Wolfgang Wallner | 949a26e | 2020-02-18 15:32:10 +0100 | [diff] [blame] | 51 | * p2sb_early_init() - Enable decoding for HPET range |
Simon Glass | d7db004 | 2019-12-08 17:40:16 -0700 | [diff] [blame] | 52 | * |
| 53 | * This is needed by FSP-M which uses the High Precision Event Timer. |
| 54 | * |
| 55 | * @dev: P2SB device |
| 56 | * @return 0 if OK, -ve on error |
| 57 | */ |
Wolfgang Wallner | 949a26e | 2020-02-18 15:32:10 +0100 | [diff] [blame] | 58 | static int p2sb_early_init(struct udevice *dev) |
Simon Glass | d7db004 | 2019-12-08 17:40:16 -0700 | [diff] [blame] | 59 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame^] | 60 | struct p2sb_platdata *plat = dev_get_plat(dev); |
Simon Glass | d7db004 | 2019-12-08 17:40:16 -0700 | [diff] [blame] | 61 | pci_dev_t pdev = plat->bdf; |
| 62 | |
| 63 | /* |
| 64 | * Enable decoding for HPET memory address range. |
| 65 | * HPTC_OFFSET(0x60) bit 7, when set the P2SB will decode |
| 66 | * the High Performance Timer memory address range |
| 67 | * selected by bits 1:0 |
| 68 | */ |
| 69 | pci_x86_write_config(pdev, HPTC_OFFSET, HPTC_ADDR_ENABLE_BIT, |
| 70 | PCI_SIZE_8); |
| 71 | |
| 72 | /* Enable PCR Base address in PCH */ |
| 73 | pci_x86_write_config(pdev, PCI_BASE_ADDRESS_0, plat->mmio_base, |
| 74 | PCI_SIZE_32); |
| 75 | pci_x86_write_config(pdev, PCI_BASE_ADDRESS_1, 0, PCI_SIZE_32); |
| 76 | |
| 77 | /* Enable P2SB MSE */ |
| 78 | pci_x86_write_config(pdev, PCI_COMMAND, PCI_COMMAND_MASTER | |
| 79 | PCI_COMMAND_MEMORY, PCI_SIZE_8); |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
Wolfgang Wallner | 949a26e | 2020-02-18 15:32:10 +0100 | [diff] [blame] | 84 | static int p2sb_spl_init(struct udevice *dev) |
Simon Glass | d7db004 | 2019-12-08 17:40:16 -0700 | [diff] [blame] | 85 | { |
| 86 | /* Enable decoding for HPET. Needed for FSP global pointer storage */ |
| 87 | dm_pci_write_config(dev, P2SB_HPTC, P2SB_HPTC_ADDRESS_SELECT_0 | |
| 88 | P2SB_HPTC_ADDRESS_ENABLE, PCI_SIZE_8); |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
Wolfgang Wallner | 949a26e | 2020-02-18 15:32:10 +0100 | [diff] [blame] | 93 | int p2sb_ofdata_to_platdata(struct udevice *dev) |
Simon Glass | d7db004 | 2019-12-08 17:40:16 -0700 | [diff] [blame] | 94 | { |
| 95 | struct p2sb_uc_priv *upriv = dev_get_uclass_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame^] | 96 | struct p2sb_platdata *plat = dev_get_plat(dev); |
Simon Glass | d7db004 | 2019-12-08 17:40:16 -0700 | [diff] [blame] | 97 | |
| 98 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
| 99 | int ret; |
Simon Glass | 9976b01 | 2020-04-08 16:57:28 -0600 | [diff] [blame] | 100 | u32 base[2]; |
Simon Glass | d7db004 | 2019-12-08 17:40:16 -0700 | [diff] [blame] | 101 | |
Simon Glass | 9976b01 | 2020-04-08 16:57:28 -0600 | [diff] [blame] | 102 | ret = dev_read_u32_array(dev, "early-regs", base, ARRAY_SIZE(base)); |
| 103 | if (ret) |
| 104 | return log_msg_ret("Missing/short early-regs", ret); |
| 105 | plat->mmio_base = base[0]; |
| 106 | /* TPL sets up the initial BAR */ |
Simon Glass | d7db004 | 2019-12-08 17:40:16 -0700 | [diff] [blame] | 107 | if (spl_phase() == PHASE_TPL) { |
Simon Glass | d7db004 | 2019-12-08 17:40:16 -0700 | [diff] [blame] | 108 | plat->bdf = pci_get_devfn(dev); |
| 109 | if (plat->bdf < 0) |
| 110 | return log_msg_ret("Cannot get p2sb PCI address", |
| 111 | plat->bdf); |
Simon Glass | d7db004 | 2019-12-08 17:40:16 -0700 | [diff] [blame] | 112 | } |
Simon Glass | 9976b01 | 2020-04-08 16:57:28 -0600 | [diff] [blame] | 113 | upriv->mmio_base = plat->mmio_base; |
Simon Glass | d7db004 | 2019-12-08 17:40:16 -0700 | [diff] [blame] | 114 | #else |
| 115 | plat->mmio_base = plat->dtplat.early_regs[0]; |
| 116 | plat->bdf = pci_ofplat_get_devfn(plat->dtplat.reg[0]); |
Simon Glass | d7db004 | 2019-12-08 17:40:16 -0700 | [diff] [blame] | 117 | upriv->mmio_base = plat->mmio_base; |
Simon Glass | 9976b01 | 2020-04-08 16:57:28 -0600 | [diff] [blame] | 118 | #endif |
Simon Glass | d7db004 | 2019-12-08 17:40:16 -0700 | [diff] [blame] | 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
Wolfgang Wallner | 949a26e | 2020-02-18 15:32:10 +0100 | [diff] [blame] | 123 | static int p2sb_probe(struct udevice *dev) |
Simon Glass | d7db004 | 2019-12-08 17:40:16 -0700 | [diff] [blame] | 124 | { |
Simon Glass | 9976b01 | 2020-04-08 16:57:28 -0600 | [diff] [blame] | 125 | if (spl_phase() == PHASE_TPL) |
Wolfgang Wallner | 949a26e | 2020-02-18 15:32:10 +0100 | [diff] [blame] | 126 | return p2sb_early_init(dev); |
Simon Glass | 9976b01 | 2020-04-08 16:57:28 -0600 | [diff] [blame] | 127 | else if (spl_phase() == PHASE_SPL) |
| 128 | return p2sb_spl_init(dev); |
Simon Glass | d7db004 | 2019-12-08 17:40:16 -0700 | [diff] [blame] | 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
Simon Glass | 459a474 | 2020-07-07 21:32:31 -0600 | [diff] [blame] | 133 | static void p2sb_set_hide_bit(struct udevice *dev, bool hide) |
| 134 | { |
| 135 | dm_pci_clrset_config8(dev, PCH_P2SB_E0 + 1, HIDE_BIT, |
| 136 | hide ? HIDE_BIT : 0); |
| 137 | } |
| 138 | |
| 139 | static int intel_p2sb_set_hide(struct udevice *dev, bool hide) |
| 140 | { |
| 141 | u16 vendor; |
| 142 | |
| 143 | if (!CONFIG_IS_ENABLED(PCI)) |
| 144 | return -EPERM; |
| 145 | p2sb_set_hide_bit(dev, hide); |
| 146 | |
| 147 | dm_pci_read_config16(dev, PCI_VENDOR_ID, &vendor); |
| 148 | if (hide && vendor != 0xffff) |
| 149 | return log_msg_ret("hide", -EEXIST); |
| 150 | else if (!hide && vendor != PCI_VENDOR_ID_INTEL) |
| 151 | return log_msg_ret("unhide", -ENOMEDIUM); |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
Simon Glass | 8719dcc | 2020-07-07 21:32:32 -0600 | [diff] [blame] | 156 | static int p2sb_remove(struct udevice *dev) |
| 157 | { |
| 158 | int ret; |
| 159 | |
| 160 | ret = intel_p2sb_set_hide(dev, true); |
| 161 | if (ret) |
| 162 | return log_msg_ret("hide", ret); |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
Simon Glass | d7db004 | 2019-12-08 17:40:16 -0700 | [diff] [blame] | 167 | static int p2sb_child_post_bind(struct udevice *dev) |
| 168 | { |
| 169 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 170 | struct p2sb_child_platdata *pplat = dev_get_parent_plat(dev); |
Simon Glass | d7db004 | 2019-12-08 17:40:16 -0700 | [diff] [blame] | 171 | int ret; |
| 172 | u32 pid; |
| 173 | |
| 174 | ret = dev_read_u32(dev, "intel,p2sb-port-id", &pid); |
| 175 | if (ret) |
| 176 | return ret; |
| 177 | pplat->pid = pid; |
| 178 | #endif |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
Simon Glass | 459a474 | 2020-07-07 21:32:31 -0600 | [diff] [blame] | 183 | struct p2sb_ops p2sb_ops = { |
| 184 | .set_hide = intel_p2sb_set_hide, |
| 185 | }; |
| 186 | |
Wolfgang Wallner | 949a26e | 2020-02-18 15:32:10 +0100 | [diff] [blame] | 187 | static const struct udevice_id p2sb_ids[] = { |
| 188 | { .compatible = "intel,p2sb" }, |
Simon Glass | d7db004 | 2019-12-08 17:40:16 -0700 | [diff] [blame] | 189 | { } |
| 190 | }; |
| 191 | |
Simon Glass | a055da8 | 2020-10-05 05:27:01 -0600 | [diff] [blame] | 192 | U_BOOT_DRIVER(intel_p2sb) = { |
Wolfgang Wallner | 949a26e | 2020-02-18 15:32:10 +0100 | [diff] [blame] | 193 | .name = "intel_p2sb", |
Simon Glass | d7db004 | 2019-12-08 17:40:16 -0700 | [diff] [blame] | 194 | .id = UCLASS_P2SB, |
Wolfgang Wallner | 949a26e | 2020-02-18 15:32:10 +0100 | [diff] [blame] | 195 | .of_match = p2sb_ids, |
| 196 | .probe = p2sb_probe, |
Simon Glass | 8719dcc | 2020-07-07 21:32:32 -0600 | [diff] [blame] | 197 | .remove = p2sb_remove, |
Simon Glass | 459a474 | 2020-07-07 21:32:31 -0600 | [diff] [blame] | 198 | .ops = &p2sb_ops, |
Wolfgang Wallner | 949a26e | 2020-02-18 15:32:10 +0100 | [diff] [blame] | 199 | .ofdata_to_platdata = p2sb_ofdata_to_platdata, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 200 | .plat_auto = sizeof(struct p2sb_platdata), |
| 201 | .per_child_plat_auto = |
Simon Glass | d7db004 | 2019-12-08 17:40:16 -0700 | [diff] [blame] | 202 | sizeof(struct p2sb_child_platdata), |
| 203 | .child_post_bind = p2sb_child_post_bind, |
Simon Glass | 8719dcc | 2020-07-07 21:32:32 -0600 | [diff] [blame] | 204 | .flags = DM_FLAG_OS_PREPARE, |
Simon Glass | d7db004 | 2019-12-08 17:40:16 -0700 | [diff] [blame] | 205 | }; |