Bin Meng | 68a070b | 2017-08-15 22:41:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017, Bin Meng <bmeng.cn@gmail.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0+ |
| 5 | * |
| 6 | * Derived from arch/x86/cpu/baytrail/cpu.c |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <cpu.h> |
| 11 | #include <dm.h> |
| 12 | #include <asm/cpu.h> |
| 13 | #include <asm/cpu_x86.h> |
| 14 | #include <asm/io.h> |
| 15 | #include <asm/lapic.h> |
| 16 | #include <asm/msr.h> |
| 17 | #include <asm/turbo.h> |
| 18 | |
| 19 | static const unsigned int braswell_bus_freq_table[] = { |
| 20 | 83333333, |
| 21 | 100000000, |
| 22 | 133333333, |
| 23 | 116666666, |
| 24 | 80000000, |
| 25 | 93333333, |
| 26 | 90000000, |
| 27 | 88900000, |
| 28 | 87500000 |
| 29 | }; |
| 30 | |
| 31 | static unsigned int braswell_bus_freq(void) |
| 32 | { |
| 33 | msr_t clk_info = msr_read(MSR_BSEL_CR_OVERCLOCK_CONTROL); |
| 34 | |
| 35 | if ((clk_info.lo & 0xf) < (ARRAY_SIZE(braswell_bus_freq_table))) |
| 36 | return braswell_bus_freq_table[clk_info.lo & 0xf]; |
| 37 | |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | static unsigned long braswell_tsc_freq(void) |
| 42 | { |
| 43 | msr_t platform_info; |
| 44 | ulong bclk = braswell_bus_freq(); |
| 45 | |
| 46 | if (!bclk) |
| 47 | return 0; |
| 48 | |
| 49 | platform_info = msr_read(MSR_PLATFORM_INFO); |
| 50 | |
| 51 | return bclk * ((platform_info.lo >> 8) & 0xff); |
| 52 | } |
| 53 | |
| 54 | static int braswell_get_info(struct udevice *dev, struct cpu_info *info) |
| 55 | { |
| 56 | info->cpu_freq = braswell_tsc_freq(); |
| 57 | info->features = (1 << CPU_FEAT_L1_CACHE) | (1 << CPU_FEAT_MMU); |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static int braswell_get_count(struct udevice *dev) |
| 63 | { |
| 64 | int ecx = 0; |
| 65 | |
| 66 | /* |
| 67 | * Use the algorithm described in Intel 64 and IA-32 Architectures |
| 68 | * Software Developer's Manual Volume 3 (3A, 3B & 3C): System |
| 69 | * Programming Guide, Jan-2015. Section 8.9.2: Hierarchical Mapping |
| 70 | * of CPUID Extended Topology Leaf. |
| 71 | */ |
| 72 | while (1) { |
| 73 | struct cpuid_result leaf_b; |
| 74 | |
| 75 | leaf_b = cpuid_ext(0xb, ecx); |
| 76 | |
| 77 | /* |
| 78 | * Braswell doesn't have hyperthreading so just determine the |
| 79 | * number of cores by from level type (ecx[15:8] == * 2) |
| 80 | */ |
| 81 | if ((leaf_b.ecx & 0xff00) == 0x0200) |
| 82 | return leaf_b.ebx & 0xffff; |
| 83 | |
| 84 | ecx++; |
| 85 | } |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static void braswell_set_max_freq(void) |
| 91 | { |
| 92 | msr_t perf_ctl; |
| 93 | msr_t msr; |
| 94 | |
| 95 | /* Enable speed step */ |
| 96 | msr = msr_read(MSR_IA32_MISC_ENABLES); |
| 97 | msr.lo |= (1 << 16); |
| 98 | msr_write(MSR_IA32_MISC_ENABLES, msr); |
| 99 | |
| 100 | /* Enable Burst Mode */ |
| 101 | msr = msr_read(MSR_IA32_MISC_ENABLES); |
| 102 | msr.hi = 0; |
| 103 | msr_write(MSR_IA32_MISC_ENABLES, msr); |
| 104 | |
| 105 | /* |
| 106 | * Set guaranteed ratio [21:16] from IACORE_TURBO_RATIOS to |
| 107 | * bits [15:8] of the PERF_CTL |
| 108 | */ |
| 109 | msr = msr_read(MSR_IACORE_TURBO_RATIOS); |
| 110 | perf_ctl.lo = (msr.lo & 0x3f0000) >> 8; |
| 111 | |
| 112 | /* |
| 113 | * Set guaranteed vid [22:16] from IACORE_TURBO_VIDS to |
| 114 | * bits [7:0] of the PERF_CTL |
| 115 | */ |
| 116 | msr = msr_read(MSR_IACORE_TURBO_VIDS); |
| 117 | perf_ctl.lo |= (msr.lo & 0x7f0000) >> 16; |
| 118 | |
| 119 | perf_ctl.hi = 0; |
| 120 | msr_write(MSR_IA32_PERF_CTL, perf_ctl); |
| 121 | } |
| 122 | |
| 123 | static int braswell_probe(struct udevice *dev) |
| 124 | { |
| 125 | debug("Init Braswell core\n"); |
| 126 | |
| 127 | /* |
| 128 | * On Braswell the turbo disable bit is actually scoped at the |
| 129 | * building-block level, not package. For non-BSP cores that are |
| 130 | * within a building block, enable turbo. The cores within the BSP's |
| 131 | * building block will just see it already enabled and move on. |
| 132 | */ |
| 133 | if (lapicid()) |
| 134 | turbo_enable(); |
| 135 | |
| 136 | /* Dynamic L2 shrink enable and threshold, clear SINGLE_PCTL bit 11 */ |
| 137 | msr_clrsetbits_64(MSR_PMG_CST_CONFIG_CONTROL, 0x3f080f, 0xe0008), |
| 138 | msr_clrsetbits_64(MSR_POWER_MISC, |
| 139 | ENABLE_ULFM_AUTOCM_MASK | ENABLE_INDP_AUTOCM_MASK, 0); |
| 140 | |
| 141 | /* Disable C1E */ |
| 142 | msr_clrsetbits_64(MSR_POWER_CTL, 2, 0); |
| 143 | msr_setbits_64(MSR_POWER_MISC, 0x44); |
| 144 | |
| 145 | /* Set this core to max frequency ratio */ |
| 146 | braswell_set_max_freq(); |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | static const struct udevice_id braswell_ids[] = { |
| 152 | { .compatible = "intel,braswell-cpu" }, |
| 153 | { } |
| 154 | }; |
| 155 | |
| 156 | static const struct cpu_ops braswell_ops = { |
| 157 | .get_desc = cpu_x86_get_desc, |
| 158 | .get_info = braswell_get_info, |
| 159 | .get_count = braswell_get_count, |
| 160 | .get_vendor = cpu_x86_get_vendor, |
| 161 | }; |
| 162 | |
| 163 | U_BOOT_DRIVER(cpu_x86_braswell_drv) = { |
| 164 | .name = "cpu_x86_braswell", |
| 165 | .id = UCLASS_CPU, |
| 166 | .of_match = braswell_ids, |
| 167 | .bind = cpu_x86_bind, |
| 168 | .probe = braswell_probe, |
| 169 | .ops = &braswell_ops, |
| 170 | }; |