blob: 4e8dc68e08056b92ec90d72b650048d3c5c4251f [file] [log] [blame]
Angel Ponsc3f58f62020-04-05 15:46:41 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Aaron Durbin452d31a2013-09-24 16:47:49 -05002
Kyösti Mälkki3139c8d2020-06-28 16:33:33 +03003#include <acpi/acpi_gnvs.h>
Kyösti Mälkki27872372021-01-21 16:05:26 +02004#include <acpi/acpi_pm.h>
Kyösti Mälkkid4b58252021-01-21 16:16:58 +02005#include <bootstate.h>
Aaron Durbin452d31a2013-09-24 16:47:49 -05006#include <console/console.h>
Elyes Haouasdef74aa2022-10-31 13:44:40 +01007#include <cpu/cpu.h>
Aaron Durbin452d31a2013-09-24 16:47:49 -05008#include <cpu/intel/microcode.h>
Aaron Durbin1ce0b302013-10-10 12:47:47 -05009#include <cpu/x86/cr.h>
Aaron Durbin452d31a2013-09-24 16:47:49 -050010#include <cpu/x86/msr.h>
11#include <device/device.h>
12#include <device/pci_def.h>
13#include <device/pci_ops.h>
Kyösti Mälkki4bd91872021-03-16 19:02:26 +020014#include <soc/device_nvs.h>
Julius Werner18ea2d32014-10-07 16:42:17 -070015#include <soc/gpio.h>
Elyes Haouasdef74aa2022-10-31 13:44:40 +010016#include <soc/iosf.h>
Julius Werner18ea2d32014-10-07 16:42:17 -070017#include <soc/lpc.h>
18#include <soc/msr.h>
19#include <soc/nvs.h>
20#include <soc/pattrs.h>
21#include <soc/pci_devs.h>
Angel Ponsb5320b22020-07-07 18:27:30 +020022#include <soc/pm.h>
Julius Werner18ea2d32014-10-07 16:42:17 -070023#include <soc/ramstage.h>
Aaron Durbin452d31a2013-09-24 16:47:49 -050024
Aaron Durbin452d31a2013-09-24 16:47:49 -050025#define SHOW_PATTRS 1
26
Angel Ponsbaebe2a2020-07-07 18:21:27 +020027struct pattrs __global_pattrs;
28
Aaron Durbin452d31a2013-09-24 16:47:49 -050029static void detect_num_cpus(struct pattrs *attrs)
30{
31 int ecx = 0;
32
33 while (1) {
34 struct cpuid_result leaf_b;
35
36 leaf_b = cpuid_ext(0xb, ecx);
37
38 /* Bay Trail doesn't have hyperthreading so just determine the
39 * number of cores by from level type (ecx[15:8] == * 2). */
40 if ((leaf_b.ecx & 0xff00) == 0x0200) {
41 attrs->num_cpus = leaf_b.ebx & 0xffff;
42 break;
43 }
44 ecx++;
45 }
46}
47
48static inline void fill_in_msr(msr_t *msr, int idx)
49{
50 *msr = rdmsr(idx);
51 if (SHOW_PATTRS) {
52 printk(BIOS_DEBUG, "msr(%x) = %08x%08x\n",
53 idx, msr->hi, msr->lo);
54 }
55}
56
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080057static const char *stepping_str[] = {
Ben Gardner2d3d1b72015-11-19 16:12:21 -060058 "A0", "A1", "B0", "B1", "B2", "B3", "C0", "D0",
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080059};
Aaron Durbin452d31a2013-09-24 16:47:49 -050060
61static void fill_in_pattrs(void)
62{
Elyes HAOUAS17a3ceb2018-05-22 10:42:28 +020063 struct device *dev;
Duncan Laurie6aa9f1f2013-11-07 12:47:35 -080064 msr_t msr;
Aaron Durbin452d31a2013-09-24 16:47:49 -050065 struct pattrs *attrs = (struct pattrs *)pattrs_get();
66
67 attrs->cpuid = cpuid_eax(1);
Kyösti Mälkkic70eed12018-05-22 02:18:00 +030068 dev = pcidev_on_root(LPC_DEV, LPC_FUNC);
Aaron Durbin452d31a2013-09-24 16:47:49 -050069 attrs->revid = pci_read_config8(dev, REVID);
70 /* The revision to stepping IDs have two values per metal stepping. */
Ben Gardner2d3d1b72015-11-19 16:12:21 -060071 if (attrs->revid >= RID_D_STEPPING_START) {
72 attrs->stepping = (attrs->revid - RID_D_STEPPING_START) / 2;
73 attrs->stepping += STEP_D0;
74 } else if (attrs->revid >= RID_C_STEPPING_START) {
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -080075 attrs->stepping = (attrs->revid - RID_C_STEPPING_START) / 2;
76 attrs->stepping += STEP_C0;
77 } else if (attrs->revid >= RID_B_STEPPING_START) {
Aaron Durbin452d31a2013-09-24 16:47:49 -050078 attrs->stepping = (attrs->revid - RID_B_STEPPING_START) / 2;
79 attrs->stepping += STEP_B0;
80 } else {
81 attrs->stepping = (attrs->revid - RID_A_STEPPING_START) / 2;
82 attrs->stepping += STEP_A0;
83 }
84
85 attrs->microcode_patch = intel_microcode_find();
86 attrs->address_bits = cpuid_eax(0x80000008) & 0xff;
87 detect_num_cpus(attrs);
88
89 if (SHOW_PATTRS) {
90 printk(BIOS_DEBUG, "BYT: cpuid %08x cpus %d rid %02x step %s\n",
91 attrs->cpuid, attrs->num_cpus, attrs->revid,
92 (attrs->stepping >= ARRAY_SIZE(stepping_str)) ? "??" :
93 stepping_str[attrs->stepping]);
94 }
95
Elyes HAOUAS419bfbc2018-10-01 08:47:51 +020096 fill_in_msr(&attrs->platform_id, IA32_PLATFORM_ID);
Aaron Durbin452d31a2013-09-24 16:47:49 -050097 fill_in_msr(&attrs->platform_info, MSR_PLATFORM_INFO);
Duncan Laurie6aa9f1f2013-11-07 12:47:35 -080098
99 /* Set IA core speed ratio and voltages */
100 msr = rdmsr(MSR_IACORE_RATIOS);
101 attrs->iacore_ratios[IACORE_MIN] = msr.lo & 0x7f;
102 attrs->iacore_ratios[IACORE_LFM] = (msr.lo >> 8) & 0x7f;
103 attrs->iacore_ratios[IACORE_MAX] = (msr.lo >> 16) & 0x7f;
104 msr = rdmsr(MSR_IACORE_TURBO_RATIOS);
105 attrs->iacore_ratios[IACORE_TURBO] = (msr.lo & 0xff); /* 1 core max */
106
107 msr = rdmsr(MSR_IACORE_VIDS);
108 attrs->iacore_vids[IACORE_MIN] = msr.lo & 0x7f;
109 attrs->iacore_vids[IACORE_LFM] = (msr.lo >> 8) & 0x7f;
110 attrs->iacore_vids[IACORE_MAX] = (msr.lo >> 16) & 0x7f;
111 msr = rdmsr(MSR_IACORE_TURBO_VIDS);
112 attrs->iacore_vids[IACORE_TURBO] = (msr.lo & 0xff); /* 1 core max */
113
114 /* Set bus clock speed */
115 attrs->bclk_khz = bus_freq_khz();
Aaron Durbin452d31a2013-09-24 16:47:49 -0500116}
117
Kyösti Mälkki4bd91872021-03-16 19:02:26 +0200118size_t size_of_dnvs(void)
119{
120 return sizeof(struct device_nvs);
121}
122
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800123/* Save bit index for first enabled event in PM1_STS for \_SB._SWS */
Kyösti Mälkkid4b58252021-01-21 16:16:58 +0200124static void pm_fill_gnvs(struct global_nvs *gnvs, const struct chipset_power_state *ps)
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800125{
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800126 uint16_t pm1;
Kyösti Mälkki4de1a312021-01-15 05:58:42 +0200127 int index;
128
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800129 pm1 = ps->pm1_sts & ps->pm1_en;
130
131 /* Scan for first set bit in PM1 */
Kyösti Mälkki4de1a312021-01-15 05:58:42 +0200132 for (index = 0; index < 16; index++) {
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800133 if (pm1 & 1)
134 break;
135 pm1 >>= 1;
136 }
137
Kyösti Mälkki4de1a312021-01-15 05:58:42 +0200138 if (index < 16)
139 gnvs->pm1i = index;
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800140}
141
Kyösti Mälkkid4b58252021-01-21 16:16:58 +0200142static void acpi_save_wake_source(void *unused)
143{
144 const struct chipset_power_state *ps;
Kyösti Mälkki4de1a312021-01-15 05:58:42 +0200145 struct global_nvs *gnvs;
Kyösti Mälkkid4b58252021-01-21 16:16:58 +0200146
Kyösti Mälkki4de1a312021-01-15 05:58:42 +0200147 if (acpi_reset_gnvs_for_wake(&gnvs) < 0)
148 return;
Fabio Aiutofdcf6982022-09-11 12:25:13 +0200149 if (acpi_fetch_pm_state(&ps, PS_CLAIMER_WAKE) < 0)
Kyösti Mälkkid4b58252021-01-21 16:16:58 +0200150 return;
151
152 pm_fill_gnvs(gnvs, ps);
Kyösti Mälkki4de1a312021-01-15 05:58:42 +0200153
154 printk(BIOS_DEBUG, "ACPI System Wake Source is PM1 Index %d\n",
155 gnvs->pm1i);
Kyösti Mälkkid4b58252021-01-21 16:16:58 +0200156}
157
Kyösti Mälkki4de1a312021-01-15 05:58:42 +0200158BOOT_STATE_INIT_ENTRY(BS_PRE_DEVICE, BS_ON_ENTRY, acpi_save_wake_source, NULL);
Kyösti Mälkkid4b58252021-01-21 16:16:58 +0200159
Kane Chenba9b7bf2015-01-17 08:19:54 +0800160static void baytrail_enable_2x_refresh_rate(void)
161{
162 u32 reg;
163 reg = iosf_dunit_read(0x8);
164 reg = reg & ~0x7000;
165 reg = reg | 0x2000;
166 iosf_dunit_write(0x8, reg);
167}
168
Kein Yuan35110232014-02-22 12:26:55 -0800169void baytrail_init_pre_device(struct soc_intel_baytrail_config *config)
Aaron Durbin452d31a2013-09-24 16:47:49 -0500170{
Kein Yuan35110232014-02-22 12:26:55 -0800171 struct soc_gpio_config *gpio_config;
Shawn Nematbakhshebe3b3c2013-09-26 16:44:14 -0700172
Aaron Durbin452d31a2013-09-24 16:47:49 -0500173 fill_in_pattrs();
Shawn Nematbakhshebe3b3c2013-09-26 16:44:14 -0700174
Kane Chenba9b7bf2015-01-17 08:19:54 +0800175 if (!config->disable_ddr_2x_refresh_rate)
176 baytrail_enable_2x_refresh_rate();
177
Aaron Durbin1ce0b302013-10-10 12:47:47 -0500178 /* Allow for SSE instructions to be executed. */
179 write_cr4(read_cr4() | CR4_OSFXSR | CR4_OSXMMEXCPT);
180
Aaron Durbinae5d83e2013-10-24 10:21:43 -0500181 /* Run reference code. */
182 baytrail_run_reference_code();
183
Shawn Nematbakhshebe3b3c2013-09-26 16:44:14 -0700184 /* Get GPIO initial states from mainboard */
Kein Yuan35110232014-02-22 12:26:55 -0800185 gpio_config = mainboard_get_gpios();
186 setup_soc_gpios(gpio_config, config->enable_xdp_tap);
Aaron Durbin6e77bee2013-10-30 15:25:42 -0500187
Aaron Durbinc626b742013-11-12 16:40:33 -0600188 baytrail_init_scc();
Aaron Durbin452d31a2013-09-24 16:47:49 -0500189}