Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | ede9709 | 2015-04-29 22:26:02 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Google, Inc |
| 4 | * |
Simon Glass | ede9709 | 2015-04-29 22:26:02 -0600 | [diff] [blame] | 5 | * Based on code from coreboot |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <cpu.h> |
| 10 | #include <dm.h> |
Simon Glass | 7fe32b3 | 2022-03-04 08:43:05 -0700 | [diff] [blame] | 11 | #include <event.h> |
Simon Glass | 35a3f87 | 2019-12-28 10:44:56 -0700 | [diff] [blame] | 12 | #include <init.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Stefan Roese | d7b935b | 2016-07-19 07:41:25 +0200 | [diff] [blame] | 14 | #include <pci.h> |
Simon Glass | ede9709 | 2015-04-29 22:26:02 -0600 | [diff] [blame] | 15 | #include <asm/cpu.h> |
Bin Meng | be3f06b | 2015-06-12 14:52:20 +0800 | [diff] [blame] | 16 | #include <asm/cpu_x86.h> |
Stefan Roese | d7b935b | 2016-07-19 07:41:25 +0200 | [diff] [blame] | 17 | #include <asm/io.h> |
Simon Glass | ede9709 | 2015-04-29 22:26:02 -0600 | [diff] [blame] | 18 | #include <asm/lapic.h> |
Simon Glass | ede9709 | 2015-04-29 22:26:02 -0600 | [diff] [blame] | 19 | #include <asm/msr.h> |
| 20 | #include <asm/turbo.h> |
| 21 | |
Stefan Roese | d7b935b | 2016-07-19 07:41:25 +0200 | [diff] [blame] | 22 | #define BYT_PRV_CLK 0x800 |
| 23 | #define BYT_PRV_CLK_EN (1 << 0) |
| 24 | #define BYT_PRV_CLK_M_VAL_SHIFT 1 |
| 25 | #define BYT_PRV_CLK_N_VAL_SHIFT 16 |
| 26 | #define BYT_PRV_CLK_UPDATE (1 << 31) |
| 27 | |
| 28 | static void hsuart_clock_set(void *base) |
| 29 | { |
| 30 | u32 m, n, reg; |
| 31 | |
| 32 | /* |
| 33 | * Configure the BayTrail UART clock for the internal HS UARTs |
| 34 | * (PCI devices) to 58982400 Hz |
| 35 | */ |
| 36 | m = 0x2400; |
| 37 | n = 0x3d09; |
| 38 | reg = (m << BYT_PRV_CLK_M_VAL_SHIFT) | (n << BYT_PRV_CLK_N_VAL_SHIFT); |
| 39 | writel(reg, base + BYT_PRV_CLK); |
| 40 | reg |= BYT_PRV_CLK_EN | BYT_PRV_CLK_UPDATE; |
| 41 | writel(reg, base + BYT_PRV_CLK); |
| 42 | } |
| 43 | |
| 44 | /* |
| 45 | * Configure the internal clock of both SIO HS-UARTs, if they are enabled |
| 46 | * via FSP |
| 47 | */ |
Simon Glass | 7fe32b3 | 2022-03-04 08:43:05 -0700 | [diff] [blame] | 48 | static int baytrail_uart_init(void *ctx, struct event *event) |
Stefan Roese | d7b935b | 2016-07-19 07:41:25 +0200 | [diff] [blame] | 49 | { |
| 50 | struct udevice *dev; |
| 51 | void *base; |
| 52 | int ret; |
| 53 | int i; |
| 54 | |
| 55 | /* Loop over the 2 HS-UARTs */ |
| 56 | for (i = 0; i < 2; i++) { |
| 57 | ret = dm_pci_bus_find_bdf(PCI_BDF(0, 0x1e, 3 + i), &dev); |
| 58 | if (!ret) { |
Andrew Scull | 2635e3b | 2022-04-21 16:11:13 +0000 | [diff] [blame] | 59 | base = dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0, 0, 0, PCI_REGION_TYPE, |
Stefan Roese | d7b935b | 2016-07-19 07:41:25 +0200 | [diff] [blame] | 60 | PCI_REGION_MEM); |
| 61 | hsuart_clock_set(base); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return 0; |
| 66 | } |
Simon Glass | 7fe32b3 | 2022-03-04 08:43:05 -0700 | [diff] [blame] | 67 | EVENT_SPY(EVT_DM_POST_INIT, baytrail_uart_init); |
Stefan Roese | d7b935b | 2016-07-19 07:41:25 +0200 | [diff] [blame] | 68 | |
Simon Glass | ede9709 | 2015-04-29 22:26:02 -0600 | [diff] [blame] | 69 | static void set_max_freq(void) |
| 70 | { |
| 71 | msr_t perf_ctl; |
| 72 | msr_t msr; |
| 73 | |
| 74 | /* Enable speed step */ |
Simon Glass | f6d00da | 2019-09-25 08:56:39 -0600 | [diff] [blame] | 75 | msr = msr_read(MSR_IA32_MISC_ENABLE); |
| 76 | msr.lo |= MISC_ENABLE_ENHANCED_SPEEDSTEP; |
| 77 | msr_write(MSR_IA32_MISC_ENABLE, msr); |
Simon Glass | ede9709 | 2015-04-29 22:26:02 -0600 | [diff] [blame] | 78 | |
| 79 | /* |
| 80 | * Set guaranteed ratio [21:16] from IACORE_RATIOS to bits [15:8] of |
| 81 | * the PERF_CTL |
| 82 | */ |
| 83 | msr = msr_read(MSR_IACORE_RATIOS); |
| 84 | perf_ctl.lo = (msr.lo & 0x3f0000) >> 8; |
| 85 | |
| 86 | /* |
Bin Meng | 341dda3 | 2018-05-24 03:05:59 -0700 | [diff] [blame] | 87 | * Set guaranteed vid [22:16] from IACORE_VIDS to bits [7:0] of |
Simon Glass | ede9709 | 2015-04-29 22:26:02 -0600 | [diff] [blame] | 88 | * the PERF_CTL |
| 89 | */ |
| 90 | msr = msr_read(MSR_IACORE_VIDS); |
| 91 | perf_ctl.lo |= (msr.lo & 0x7f0000) >> 16; |
| 92 | perf_ctl.hi = 0; |
| 93 | |
| 94 | msr_write(MSR_IA32_PERF_CTL, perf_ctl); |
| 95 | } |
| 96 | |
| 97 | static int cpu_x86_baytrail_probe(struct udevice *dev) |
| 98 | { |
Simon Glass | b430258 | 2015-08-04 12:34:02 -0600 | [diff] [blame] | 99 | if (!ll_boot_init()) |
| 100 | return 0; |
Simon Glass | ede9709 | 2015-04-29 22:26:02 -0600 | [diff] [blame] | 101 | debug("Init BayTrail core\n"); |
| 102 | |
| 103 | /* |
| 104 | * On BayTrail the turbo disable bit is actually scoped at the |
| 105 | * building-block level, not package. For non-BSP cores that are |
| 106 | * within a building block, enable turbo. The cores within the BSP's |
| 107 | * building block will just see it already enabled and move on. |
| 108 | */ |
| 109 | if (lapicid()) |
| 110 | turbo_enable(); |
| 111 | |
| 112 | /* Dynamic L2 shrink enable and threshold */ |
| 113 | msr_clrsetbits_64(MSR_PMG_CST_CONFIG_CONTROL, 0x3f000f, 0xe0008), |
| 114 | |
| 115 | /* Disable C1E */ |
| 116 | msr_clrsetbits_64(MSR_POWER_CTL, 2, 0); |
| 117 | msr_setbits_64(MSR_POWER_MISC, 0x44); |
| 118 | |
| 119 | /* Set this core to max frequency ratio */ |
| 120 | set_max_freq(); |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static unsigned bus_freq(void) |
| 126 | { |
| 127 | msr_t clk_info = msr_read(MSR_BSEL_CR_OVERCLOCK_CONTROL); |
| 128 | switch (clk_info.lo & 0x3) { |
| 129 | case 0: |
| 130 | return 83333333; |
| 131 | case 1: |
| 132 | return 100000000; |
| 133 | case 2: |
| 134 | return 133333333; |
| 135 | case 3: |
| 136 | return 116666666; |
| 137 | default: |
| 138 | return 0; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | static unsigned long tsc_freq(void) |
| 143 | { |
| 144 | msr_t platform_info; |
| 145 | ulong bclk = bus_freq(); |
| 146 | |
| 147 | if (!bclk) |
| 148 | return 0; |
| 149 | |
| 150 | platform_info = msr_read(MSR_PLATFORM_INFO); |
| 151 | |
| 152 | return bclk * ((platform_info.lo >> 8) & 0xff); |
| 153 | } |
| 154 | |
Simon Glass | 961420f | 2020-01-26 22:06:27 -0700 | [diff] [blame] | 155 | static int baytrail_get_info(const struct udevice *dev, struct cpu_info *info) |
Simon Glass | ede9709 | 2015-04-29 22:26:02 -0600 | [diff] [blame] | 156 | { |
| 157 | info->cpu_freq = tsc_freq(); |
| 158 | info->features = 1 << CPU_FEAT_L1_CACHE | 1 << CPU_FEAT_MMU; |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
Simon Glass | 961420f | 2020-01-26 22:06:27 -0700 | [diff] [blame] | 163 | static int baytrail_get_count(const struct udevice *dev) |
Bin Meng | 6e6f4ce | 2015-06-17 11:15:36 +0800 | [diff] [blame] | 164 | { |
| 165 | int ecx = 0; |
| 166 | |
| 167 | /* |
| 168 | * Use the algorithm described in Intel 64 and IA-32 Architectures |
| 169 | * Software Developer's Manual Volume 3 (3A, 3B & 3C): System |
| 170 | * Programming Guide, Jan-2015. Section 8.9.2: Hierarchical Mapping |
| 171 | * of CPUID Extended Topology Leaf. |
| 172 | */ |
| 173 | while (1) { |
| 174 | struct cpuid_result leaf_b; |
| 175 | |
| 176 | leaf_b = cpuid_ext(0xb, ecx); |
| 177 | |
| 178 | /* |
| 179 | * Bay Trail doesn't have hyperthreading so just determine the |
| 180 | * number of cores by from level type (ecx[15:8] == * 2) |
| 181 | */ |
| 182 | if ((leaf_b.ecx & 0xff00) == 0x0200) |
| 183 | return leaf_b.ebx & 0xffff; |
| 184 | |
| 185 | ecx++; |
| 186 | } |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
Simon Glass | ede9709 | 2015-04-29 22:26:02 -0600 | [diff] [blame] | 191 | static const struct cpu_ops cpu_x86_baytrail_ops = { |
Bin Meng | be3f06b | 2015-06-12 14:52:20 +0800 | [diff] [blame] | 192 | .get_desc = cpu_x86_get_desc, |
Simon Glass | ede9709 | 2015-04-29 22:26:02 -0600 | [diff] [blame] | 193 | .get_info = baytrail_get_info, |
Bin Meng | 6e6f4ce | 2015-06-17 11:15:36 +0800 | [diff] [blame] | 194 | .get_count = baytrail_get_count, |
Alexander Graf | 94eaa79 | 2016-08-19 01:23:27 +0200 | [diff] [blame] | 195 | .get_vendor = cpu_x86_get_vendor, |
Simon Glass | ede9709 | 2015-04-29 22:26:02 -0600 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | static const struct udevice_id cpu_x86_baytrail_ids[] = { |
| 199 | { .compatible = "intel,baytrail-cpu" }, |
| 200 | { } |
| 201 | }; |
| 202 | |
| 203 | U_BOOT_DRIVER(cpu_x86_baytrail_drv) = { |
| 204 | .name = "cpu_x86_baytrail", |
| 205 | .id = UCLASS_CPU, |
| 206 | .of_match = cpu_x86_baytrail_ids, |
Bin Meng | be3f06b | 2015-06-12 14:52:20 +0800 | [diff] [blame] | 207 | .bind = cpu_x86_bind, |
Simon Glass | ede9709 | 2015-04-29 22:26:02 -0600 | [diff] [blame] | 208 | .probe = cpu_x86_baytrail_probe, |
| 209 | .ops = &cpu_x86_baytrail_ops, |
Bin Meng | c337e1a | 2018-10-14 01:07:19 -0700 | [diff] [blame] | 210 | .flags = DM_FLAG_PRE_RELOC, |
Simon Glass | ede9709 | 2015-04-29 22:26:02 -0600 | [diff] [blame] | 211 | }; |