1 // SPDX-License-Identifier: GPL-2.0
3 * Primary-to-Sideband Bridge
5 * Copyright 2019 Google LLC
8 #define LOG_CATEGORY UCLASS_P2SB
12 #include <dt-structs.h>
17 #include <linux/bitops.h>
19 #define PCH_P2SB_E0 0xe0
20 #define HIDE_BIT BIT(0)
23 #if CONFIG_IS_ENABLED(OF_PLATDATA)
24 struct dtd_intel_p2sb dtplat;
30 /* PCI config space registers */
31 #define HPTC_OFFSET 0x60
32 #define HPTC_ADDR_ENABLE_BIT BIT(7)
34 /* High Performance Event Timer Configuration */
35 #define P2SB_HPTC 0x60
36 #define P2SB_HPTC_ADDRESS_ENABLE BIT(7)
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
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)
51 * p2sb_early_init() - Enable decoding for HPET range
53 * This is needed by FSP-M which uses the High Precision Event Timer.
56 * @return 0 if OK, -ve on error
58 static int p2sb_early_init(struct udevice *dev)
60 struct p2sb_plat *plat = dev_get_plat(dev);
61 pci_dev_t pdev = plat->bdf;
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
69 pci_x86_write_config(pdev, HPTC_OFFSET, HPTC_ADDR_ENABLE_BIT,
72 /* Enable PCR Base address in PCH */
73 pci_x86_write_config(pdev, PCI_BASE_ADDRESS_0, plat->mmio_base,
75 pci_x86_write_config(pdev, PCI_BASE_ADDRESS_1, 0, PCI_SIZE_32);
78 pci_x86_write_config(pdev, PCI_COMMAND, PCI_COMMAND_MASTER |
79 PCI_COMMAND_MEMORY, PCI_SIZE_8);
84 static int p2sb_spl_init(struct udevice *dev)
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);
93 int p2sb_of_to_plat(struct udevice *dev)
95 struct p2sb_uc_priv *upriv = dev_get_uclass_priv(dev);
96 struct p2sb_plat *plat = dev_get_plat(dev);
98 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
102 ret = dev_read_u32_array(dev, "early-regs", base, ARRAY_SIZE(base));
104 return log_msg_ret("Missing/short early-regs", ret);
105 plat->mmio_base = base[0];
106 /* TPL sets up the initial BAR */
107 if (spl_phase() == PHASE_TPL) {
108 plat->bdf = pci_get_devfn(dev);
110 return log_msg_ret("Cannot get p2sb PCI address",
113 upriv->mmio_base = plat->mmio_base;
115 plat->mmio_base = plat->dtplat.early_regs[0];
116 plat->bdf = pci_ofplat_get_devfn(plat->dtplat.reg[0]);
117 upriv->mmio_base = plat->mmio_base;
123 static int p2sb_probe(struct udevice *dev)
125 if (spl_phase() == PHASE_TPL)
126 return p2sb_early_init(dev);
127 else if (spl_phase() == PHASE_SPL)
128 return p2sb_spl_init(dev);
133 static void p2sb_set_hide_bit(struct udevice *dev, bool hide)
135 dm_pci_clrset_config8(dev, PCH_P2SB_E0 + 1, HIDE_BIT,
136 hide ? HIDE_BIT : 0);
139 static int intel_p2sb_set_hide(struct udevice *dev, bool hide)
143 if (!CONFIG_IS_ENABLED(PCI))
145 p2sb_set_hide_bit(dev, hide);
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);
156 static int p2sb_remove(struct udevice *dev)
160 ret = intel_p2sb_set_hide(dev, true);
162 return log_msg_ret("hide", ret);
167 static int p2sb_child_post_bind(struct udevice *dev)
169 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
170 struct p2sb_child_plat *pplat = dev_get_parent_plat(dev);
174 ret = dev_read_u32(dev, "intel,p2sb-port-id", &pid);
183 struct p2sb_ops p2sb_ops = {
184 .set_hide = intel_p2sb_set_hide,
187 static const struct udevice_id p2sb_ids[] = {
188 { .compatible = "intel,p2sb" },
192 U_BOOT_DRIVER(intel_p2sb) = {
193 .name = "intel_p2sb",
195 .of_match = p2sb_ids,
197 .remove = p2sb_remove,
199 .of_to_plat = p2sb_of_to_plat,
200 .plat_auto = sizeof(struct p2sb_plat),
201 .per_child_plat_auto = sizeof(struct p2sb_child_plat),
202 .child_post_bind = p2sb_child_post_bind,
203 .flags = DM_FLAG_OS_PREPARE,