blob: d73ae438bbb07f4de7c6f888d0c269c16fb5dca1 [file] [log] [blame]
Simon Glassd7db0042019-12-08 17:40:16 -07001// 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 Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glassd7db0042019-12-08 17:40:16 -070014#include <p2sb.h>
15#include <spl.h>
Simon Glass83819cb2020-12-19 10:39:58 -070016#include <asm/p2sb.h>
Simon Glassd7db0042019-12-08 17:40:16 -070017#include <asm/pci.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060018#include <linux/bitops.h>
Simon Glassd7db0042019-12-08 17:40:16 -070019
Simon Glass459a4742020-07-07 21:32:31 -060020#define PCH_P2SB_E0 0xe0
21#define HIDE_BIT BIT(0)
22
Simon Glassd7db0042019-12-08 17:40:16 -070023/* PCI config space registers */
24#define HPTC_OFFSET 0x60
25#define HPTC_ADDR_ENABLE_BIT BIT(7)
26
27/* High Performance Event Timer Configuration */
28#define P2SB_HPTC 0x60
29#define P2SB_HPTC_ADDRESS_ENABLE BIT(7)
30
31/*
32 * ADDRESS_SELECT ENCODING_RANGE
33 * 0 0xfed0 0000 - 0xfed0 03ff
34 * 1 0xfed0 1000 - 0xfed0 13ff
35 * 2 0xfed0 2000 - 0xfed0 23ff
36 * 3 0xfed0 3000 - 0xfed0 33ff
37 */
38#define P2SB_HPTC_ADDRESS_SELECT_0 (0 << 0)
39#define P2SB_HPTC_ADDRESS_SELECT_1 (1 << 0)
40#define P2SB_HPTC_ADDRESS_SELECT_2 (2 << 0)
41#define P2SB_HPTC_ADDRESS_SELECT_3 (3 << 0)
42
43/*
Wolfgang Wallner949a26e2020-02-18 15:32:10 +010044 * p2sb_early_init() - Enable decoding for HPET range
Simon Glassd7db0042019-12-08 17:40:16 -070045 *
46 * This is needed by FSP-M which uses the High Precision Event Timer.
47 *
48 * @dev: P2SB device
49 * @return 0 if OK, -ve on error
50 */
Wolfgang Wallner949a26e2020-02-18 15:32:10 +010051static int p2sb_early_init(struct udevice *dev)
Simon Glassd7db0042019-12-08 17:40:16 -070052{
Simon Glassb75b15b2020-12-03 16:55:23 -070053 struct p2sb_plat *plat = dev_get_plat(dev);
Simon Glassd7db0042019-12-08 17:40:16 -070054 pci_dev_t pdev = plat->bdf;
55
56 /*
57 * Enable decoding for HPET memory address range.
58 * HPTC_OFFSET(0x60) bit 7, when set the P2SB will decode
59 * the High Performance Timer memory address range
60 * selected by bits 1:0
61 */
62 pci_x86_write_config(pdev, HPTC_OFFSET, HPTC_ADDR_ENABLE_BIT,
63 PCI_SIZE_8);
64
65 /* Enable PCR Base address in PCH */
66 pci_x86_write_config(pdev, PCI_BASE_ADDRESS_0, plat->mmio_base,
67 PCI_SIZE_32);
68 pci_x86_write_config(pdev, PCI_BASE_ADDRESS_1, 0, PCI_SIZE_32);
69
70 /* Enable P2SB MSE */
71 pci_x86_write_config(pdev, PCI_COMMAND, PCI_COMMAND_MASTER |
72 PCI_COMMAND_MEMORY, PCI_SIZE_8);
73
74 return 0;
75}
76
Wolfgang Wallner949a26e2020-02-18 15:32:10 +010077static int p2sb_spl_init(struct udevice *dev)
Simon Glassd7db0042019-12-08 17:40:16 -070078{
79 /* Enable decoding for HPET. Needed for FSP global pointer storage */
80 dm_pci_write_config(dev, P2SB_HPTC, P2SB_HPTC_ADDRESS_SELECT_0 |
81 P2SB_HPTC_ADDRESS_ENABLE, PCI_SIZE_8);
82
83 return 0;
84}
85
Simon Glassaad29ae2020-12-03 16:55:21 -070086int p2sb_of_to_plat(struct udevice *dev)
Simon Glassd7db0042019-12-08 17:40:16 -070087{
88 struct p2sb_uc_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassb75b15b2020-12-03 16:55:23 -070089 struct p2sb_plat *plat = dev_get_plat(dev);
Simon Glassd7db0042019-12-08 17:40:16 -070090
91#if !CONFIG_IS_ENABLED(OF_PLATDATA)
92 int ret;
Simon Glass9976b012020-04-08 16:57:28 -060093 u32 base[2];
Simon Glassd7db0042019-12-08 17:40:16 -070094
Simon Glass9976b012020-04-08 16:57:28 -060095 ret = dev_read_u32_array(dev, "early-regs", base, ARRAY_SIZE(base));
96 if (ret)
97 return log_msg_ret("Missing/short early-regs", ret);
98 plat->mmio_base = base[0];
99 /* TPL sets up the initial BAR */
Simon Glassd7db0042019-12-08 17:40:16 -0700100 if (spl_phase() == PHASE_TPL) {
Simon Glassd7db0042019-12-08 17:40:16 -0700101 plat->bdf = pci_get_devfn(dev);
102 if (plat->bdf < 0)
103 return log_msg_ret("Cannot get p2sb PCI address",
104 plat->bdf);
Simon Glassd7db0042019-12-08 17:40:16 -0700105 }
Simon Glass9976b012020-04-08 16:57:28 -0600106 upriv->mmio_base = plat->mmio_base;
Simon Glassd7db0042019-12-08 17:40:16 -0700107#else
108 plat->mmio_base = plat->dtplat.early_regs[0];
109 plat->bdf = pci_ofplat_get_devfn(plat->dtplat.reg[0]);
Simon Glassd7db0042019-12-08 17:40:16 -0700110 upriv->mmio_base = plat->mmio_base;
Simon Glass9976b012020-04-08 16:57:28 -0600111#endif
Simon Glassd7db0042019-12-08 17:40:16 -0700112
113 return 0;
114}
115
Wolfgang Wallner949a26e2020-02-18 15:32:10 +0100116static int p2sb_probe(struct udevice *dev)
Simon Glassd7db0042019-12-08 17:40:16 -0700117{
Simon Glass9976b012020-04-08 16:57:28 -0600118 if (spl_phase() == PHASE_TPL)
Wolfgang Wallner949a26e2020-02-18 15:32:10 +0100119 return p2sb_early_init(dev);
Simon Glass9976b012020-04-08 16:57:28 -0600120 else if (spl_phase() == PHASE_SPL)
121 return p2sb_spl_init(dev);
Simon Glassd7db0042019-12-08 17:40:16 -0700122
123 return 0;
124}
125
Simon Glass459a4742020-07-07 21:32:31 -0600126static void p2sb_set_hide_bit(struct udevice *dev, bool hide)
127{
128 dm_pci_clrset_config8(dev, PCH_P2SB_E0 + 1, HIDE_BIT,
129 hide ? HIDE_BIT : 0);
130}
131
132static int intel_p2sb_set_hide(struct udevice *dev, bool hide)
133{
134 u16 vendor;
135
136 if (!CONFIG_IS_ENABLED(PCI))
137 return -EPERM;
138 p2sb_set_hide_bit(dev, hide);
139
140 dm_pci_read_config16(dev, PCI_VENDOR_ID, &vendor);
141 if (hide && vendor != 0xffff)
142 return log_msg_ret("hide", -EEXIST);
143 else if (!hide && vendor != PCI_VENDOR_ID_INTEL)
144 return log_msg_ret("unhide", -ENOMEDIUM);
145
146 return 0;
147}
148
Simon Glass8719dcc2020-07-07 21:32:32 -0600149static int p2sb_remove(struct udevice *dev)
150{
151 int ret;
152
153 ret = intel_p2sb_set_hide(dev, true);
154 if (ret)
155 return log_msg_ret("hide", ret);
156
157 return 0;
158}
159
Simon Glassd7db0042019-12-08 17:40:16 -0700160static int p2sb_child_post_bind(struct udevice *dev)
161{
162#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassb75b15b2020-12-03 16:55:23 -0700163 struct p2sb_child_plat *pplat = dev_get_parent_plat(dev);
Simon Glassd7db0042019-12-08 17:40:16 -0700164 int ret;
165 u32 pid;
166
167 ret = dev_read_u32(dev, "intel,p2sb-port-id", &pid);
168 if (ret)
169 return ret;
170 pplat->pid = pid;
171#endif
172
173 return 0;
174}
175
Simon Glass772136b2020-12-23 08:11:26 -0700176static const struct p2sb_ops p2sb_ops = {
Simon Glass459a4742020-07-07 21:32:31 -0600177 .set_hide = intel_p2sb_set_hide,
178};
179
Simon Glassec8ae8a2020-12-23 08:11:30 -0700180#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Wolfgang Wallner949a26e2020-02-18 15:32:10 +0100181static const struct udevice_id p2sb_ids[] = {
182 { .compatible = "intel,p2sb" },
Simon Glassd7db0042019-12-08 17:40:16 -0700183 { }
184};
Simon Glassec8ae8a2020-12-23 08:11:30 -0700185#endif
Simon Glassd7db0042019-12-08 17:40:16 -0700186
Simon Glassa055da82020-10-05 05:27:01 -0600187U_BOOT_DRIVER(intel_p2sb) = {
Wolfgang Wallner949a26e2020-02-18 15:32:10 +0100188 .name = "intel_p2sb",
Simon Glassd7db0042019-12-08 17:40:16 -0700189 .id = UCLASS_P2SB,
Simon Glassec8ae8a2020-12-23 08:11:30 -0700190 .of_match = of_match_ptr(p2sb_ids),
Wolfgang Wallner949a26e2020-02-18 15:32:10 +0100191 .probe = p2sb_probe,
Simon Glass8719dcc2020-07-07 21:32:32 -0600192 .remove = p2sb_remove,
Simon Glass459a4742020-07-07 21:32:31 -0600193 .ops = &p2sb_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700194 .of_to_plat = p2sb_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700195 .plat_auto = sizeof(struct p2sb_plat),
196 .per_child_plat_auto = sizeof(struct p2sb_child_plat),
Simon Glassd7db0042019-12-08 17:40:16 -0700197 .child_post_bind = p2sb_child_post_bind,
Simon Glass8719dcc2020-07-07 21:32:32 -0600198 .flags = DM_FLAG_OS_PREPARE,
Simon Glassd7db0042019-12-08 17:40:16 -0700199};