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