Merge tag 'u-boot-atmel-fixes-2020.07-a' of https://gitlab.denx.de/u-boot/custodians...
[oweals/u-boot.git] / arch / x86 / cpu / apollolake / fsp_s.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2019 Google LLC
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6
7 #include <common.h>
8 #include <binman.h>
9 #include <bootstage.h>
10 #include <dm.h>
11 #include <init.h>
12 #include <irq.h>
13 #include <log.h>
14 #include <malloc.h>
15 #include <acpi/acpi_s3.h>
16 #include <asm/intel_pinctrl.h>
17 #include <asm/io.h>
18 #include <asm/intel_regs.h>
19 #include <asm/msr.h>
20 #include <asm/msr-index.h>
21 #include <asm/pci.h>
22 #include <asm/arch/cpu.h>
23 #include <asm/arch/systemagent.h>
24 #include <asm/arch/fsp/fsp_configs.h>
25 #include <asm/arch/fsp/fsp_s_upd.h>
26 #include <linux/bitops.h>
27 #include <asm/arch/fsp_bindings.h>
28
29 #define PCH_P2SB_E0             0xe0
30 #define HIDE_BIT                BIT(0)
31
32 int fsps_update_config(struct udevice *dev, ulong rom_offset,
33                        struct fsps_upd *upd)
34 {
35         struct fsp_s_config *cfg = &upd->config;
36         ofnode node;
37
38         if (IS_ENABLED(CONFIG_HAVE_VBT)) {
39                 struct binman_entry vbt;
40                 void *vbt_buf;
41                 int ret;
42
43                 ret = binman_entry_find("intel-vbt", &vbt);
44                 if (ret)
45                         return log_msg_ret("Cannot find VBT", ret);
46                 vbt.image_pos += rom_offset;
47                 vbt_buf = malloc(vbt.size);
48                 if (!vbt_buf)
49                         return log_msg_ret("Alloc VBT", -ENOMEM);
50
51                 /*
52                  * Load VBT before devicetree-specific config. This only
53                  * supports memory-mapped SPI at present.
54                  */
55                 bootstage_start(BOOTSTAGE_ID_ACCUM_MMAP_SPI, "mmap_spi");
56                 memcpy(vbt_buf, (void *)vbt.image_pos, vbt.size);
57                 bootstage_accum(BOOTSTAGE_ID_ACCUM_MMAP_SPI);
58                 if (*(u32 *)vbt_buf != VBT_SIGNATURE)
59                         return log_msg_ret("VBT signature", -EINVAL);
60
61                 cfg->graphics_config_ptr = (ulong)vbt_buf;
62         }
63
64         node = dev_read_subnode(dev, "fsp-s");
65         if (!ofnode_valid(node))
66                 return log_msg_ret("fsp-s settings", -ENOENT);
67
68         return fsp_s_update_config_from_dtb(node, cfg);
69 }
70
71 static void p2sb_set_hide_bit(pci_dev_t dev, int hide)
72 {
73         pci_x86_clrset_config(dev, PCH_P2SB_E0 + 1, HIDE_BIT,
74                               hide ? HIDE_BIT : 0, PCI_SIZE_8);
75 }
76
77 /* Configure package power limits */
78 static int set_power_limits(struct udevice *dev)
79 {
80         msr_t rapl_msr_reg, limit;
81         u32 power_unit;
82         u32 tdp, min_power, max_power;
83         u32 pl2_val;
84         u32 override_tdp[2];
85         int ret;
86
87         /* Get units */
88         rapl_msr_reg = msr_read(MSR_PKG_POWER_SKU_UNIT);
89         power_unit = 1 << (rapl_msr_reg.lo & 0xf);
90
91         /* Get power defaults for this SKU */
92         rapl_msr_reg = msr_read(MSR_PKG_POWER_SKU);
93         tdp = rapl_msr_reg.lo & PKG_POWER_LIMIT_MASK;
94         pl2_val = rapl_msr_reg.hi & PKG_POWER_LIMIT_MASK;
95         min_power = (rapl_msr_reg.lo >> 16) & PKG_POWER_LIMIT_MASK;
96         max_power = rapl_msr_reg.hi & PKG_POWER_LIMIT_MASK;
97
98         if (min_power > 0 && tdp < min_power)
99                 tdp = min_power;
100
101         if (max_power > 0 && tdp > max_power)
102                 tdp = max_power;
103
104         ret = dev_read_u32_array(dev, "tdp-pl-override-mw", override_tdp,
105                                  ARRAY_SIZE(override_tdp));
106         if (ret)
107                 return log_msg_ret("tdp-pl-override-mw", ret);
108
109         /* Set PL1 override value */
110         if (override_tdp[0])
111                 tdp = override_tdp[0] * power_unit / 1000;
112
113         /* Set PL2 override value */
114         if (override_tdp[1])
115                 pl2_val = override_tdp[1] * power_unit / 1000;
116
117         /* Set long term power limit to TDP */
118         limit.lo = tdp & PKG_POWER_LIMIT_MASK;
119         /* Set PL1 Pkg Power clamp bit */
120         limit.lo |= PKG_POWER_LIMIT_CLAMP;
121
122         limit.lo |= PKG_POWER_LIMIT_EN;
123         limit.lo |= (MB_POWER_LIMIT1_TIME_DEFAULT &
124                 PKG_POWER_LIMIT_TIME_MASK) << PKG_POWER_LIMIT_TIME_SHIFT;
125
126         /* Set short term power limit PL2 */
127         limit.hi = pl2_val & PKG_POWER_LIMIT_MASK;
128         limit.hi |= PKG_POWER_LIMIT_EN;
129
130         /* Program package power limits in RAPL MSR */
131         msr_write(MSR_PKG_POWER_LIMIT, limit);
132         log_info("RAPL PL1 %d.%dW\n", tdp / power_unit,
133                  100 * (tdp % power_unit) / power_unit);
134         log_info("RAPL PL2 %d.%dW\n", pl2_val / power_unit,
135                  100 * (pl2_val % power_unit) / power_unit);
136
137         /*
138          * Sett RAPL MMIO register for Power limits. RAPL driver is using MSR
139          * instead of MMIO, so disable LIMIT_EN bit for MMIO
140          */
141         writel(limit.lo & ~PKG_POWER_LIMIT_EN, MCHBAR_REG(MCHBAR_RAPL_PPL));
142         writel(limit.hi & ~PKG_POWER_LIMIT_EN, MCHBAR_REG(MCHBAR_RAPL_PPL + 4));
143
144         return 0;
145 }
146
147 int p2sb_unhide(void)
148 {
149         pci_dev_t dev = PCI_BDF(0, 0xd, 0);
150         ulong val;
151
152         p2sb_set_hide_bit(dev, 0);
153
154         pci_x86_read_config(dev, PCI_VENDOR_ID, &val, PCI_SIZE_16);
155
156         if (val != PCI_VENDOR_ID_INTEL)
157                 return log_msg_ret("p2sb unhide", -EIO);
158
159         return 0;
160 }
161
162 /* Overwrites the SCI IRQ if another IRQ number is given by device tree */
163 static void set_sci_irq(void)
164 {
165         /* Skip this for now */
166 }
167
168 int arch_fsps_preinit(void)
169 {
170         struct udevice *itss;
171         int ret;
172
173         ret = irq_first_device_type(X86_IRQT_ITSS, &itss);
174         if (ret)
175                 return log_msg_ret("no itss", ret);
176         /*
177          * Snapshot the current GPIO IRQ polarities. FSP is setting a default
178          * policy that doesn't honour boards' requirements
179          */
180         irq_snapshot_polarities(itss);
181
182         /*
183          * Clear the GPI interrupt status and enable registers. These
184          * registers do not get reset to default state when booting from S5.
185          */
186         ret = pinctrl_gpi_clear_int_cfg();
187         if (ret)
188                 return log_msg_ret("gpi_clear", ret);
189
190         return 0;
191 }
192
193 int arch_fsp_init_r(void)
194 {
195 #ifdef CONFIG_HAVE_ACPI_RESUME
196         bool s3wake = gd->arch.prev_sleep_state == ACPI_S3;
197 #else
198         bool s3wake = false;
199 #endif
200         struct udevice *dev, *itss;
201         int ret;
202
203         if (!ll_boot_init())
204                 return 0;
205         /*
206          * This must be called before any devices are probed. Put any probing
207          * into arch_fsps_preinit() above.
208          *
209          * We don't use CONFIG_APL_BOOT_FROM_FAST_SPI_FLASH here since it will
210          * force PCI to be probed.
211          */
212         ret = fsp_silicon_init(s3wake, false);
213         if (ret)
214                 return ret;
215
216         ret = irq_first_device_type(X86_IRQT_ITSS, &itss);
217         if (ret)
218                 return log_msg_ret("no itss", ret);
219         /* Restore GPIO IRQ polarities back to previous settings */
220         irq_restore_polarities(itss);
221
222         /* soc_init() */
223         ret = p2sb_unhide();
224         if (ret)
225                 return log_msg_ret("unhide p2sb", ret);
226
227         /* Set RAPL MSR for Package power limits*/
228         ret = uclass_first_device_err(UCLASS_NORTHBRIDGE, &dev);
229         if (ret)
230                 return log_msg_ret("Cannot get northbridge", ret);
231         set_power_limits(dev);
232
233         /*
234          * FSP-S routes SCI to IRQ 9. With the help of this function you can
235          * select another IRQ for SCI.
236          */
237         set_sci_irq();
238
239         return 0;
240 }