blob: 4fb6a485542fdd4ec9dbac0faaa195ba5b1147a2 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass987214d2015-04-29 22:26:02 -06002/*
3 * Copyright (C) 2015 Google, Inc
4 *
Simon Glass987214d2015-04-29 22:26:02 -06005 * Based on code from coreboot
6 */
7
8#include <common.h>
9#include <cpu.h>
10#include <dm.h>
Simon Glassfc557362022-03-04 08:43:05 -070011#include <event.h>
Simon Glassda25eff2019-12-28 10:44:56 -070012#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Stefan Roesefb2cac82016-07-19 07:41:25 +020014#include <pci.h>
Simon Glass987214d2015-04-29 22:26:02 -060015#include <asm/cpu.h>
Bin Meng12d41052015-06-12 14:52:20 +080016#include <asm/cpu_x86.h>
Stefan Roesefb2cac82016-07-19 07:41:25 +020017#include <asm/io.h>
Simon Glass987214d2015-04-29 22:26:02 -060018#include <asm/lapic.h>
Simon Glass987214d2015-04-29 22:26:02 -060019#include <asm/msr.h>
20#include <asm/turbo.h>
21
Stefan Roesefb2cac82016-07-19 07:41:25 +020022#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
28static 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 Glassfc557362022-03-04 08:43:05 -070048static int baytrail_uart_init(void *ctx, struct event *event)
Stefan Roesefb2cac82016-07-19 07:41:25 +020049{
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 Scull6520c822022-04-21 16:11:13 +000059 base = dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0, 0, 0, PCI_REGION_TYPE,
Stefan Roesefb2cac82016-07-19 07:41:25 +020060 PCI_REGION_MEM);
61 hsuart_clock_set(base);
62 }
63 }
64
65 return 0;
66}
Simon Glassfc557362022-03-04 08:43:05 -070067EVENT_SPY(EVT_DM_POST_INIT, baytrail_uart_init);
Stefan Roesefb2cac82016-07-19 07:41:25 +020068
Simon Glass987214d2015-04-29 22:26:02 -060069static void set_max_freq(void)
70{
71 msr_t perf_ctl;
72 msr_t msr;
73
74 /* Enable speed step */
Simon Glass05e85b92019-09-25 08:56:39 -060075 msr = msr_read(MSR_IA32_MISC_ENABLE);
76 msr.lo |= MISC_ENABLE_ENHANCED_SPEEDSTEP;
77 msr_write(MSR_IA32_MISC_ENABLE, msr);
Simon Glass987214d2015-04-29 22:26:02 -060078
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 Menga45c39d2018-05-24 03:05:59 -070087 * Set guaranteed vid [22:16] from IACORE_VIDS to bits [7:0] of
Simon Glass987214d2015-04-29 22:26:02 -060088 * 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
97static int cpu_x86_baytrail_probe(struct udevice *dev)
98{
Simon Glassa815df72015-08-04 12:34:02 -060099 if (!ll_boot_init())
100 return 0;
Simon Glass987214d2015-04-29 22:26:02 -0600101 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
125static 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
142static 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 Glass791fa452020-01-26 22:06:27 -0700155static int baytrail_get_info(const struct udevice *dev, struct cpu_info *info)
Simon Glass987214d2015-04-29 22:26:02 -0600156{
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 Glass791fa452020-01-26 22:06:27 -0700163static int baytrail_get_count(const struct udevice *dev)
Bin Mengf967f9a2015-06-17 11:15:36 +0800164{
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 Glass987214d2015-04-29 22:26:02 -0600191static const struct cpu_ops cpu_x86_baytrail_ops = {
Bin Meng12d41052015-06-12 14:52:20 +0800192 .get_desc = cpu_x86_get_desc,
Simon Glass987214d2015-04-29 22:26:02 -0600193 .get_info = baytrail_get_info,
Bin Mengf967f9a2015-06-17 11:15:36 +0800194 .get_count = baytrail_get_count,
Alexander Graf67f66bd2016-08-19 01:23:27 +0200195 .get_vendor = cpu_x86_get_vendor,
Simon Glass987214d2015-04-29 22:26:02 -0600196};
197
198static const struct udevice_id cpu_x86_baytrail_ids[] = {
199 { .compatible = "intel,baytrail-cpu" },
200 { }
201};
202
203U_BOOT_DRIVER(cpu_x86_baytrail_drv) = {
204 .name = "cpu_x86_baytrail",
205 .id = UCLASS_CPU,
206 .of_match = cpu_x86_baytrail_ids,
Bin Meng12d41052015-06-12 14:52:20 +0800207 .bind = cpu_x86_bind,
Simon Glass987214d2015-04-29 22:26:02 -0600208 .probe = cpu_x86_baytrail_probe,
209 .ops = &cpu_x86_baytrail_ops,
Bin Meng09921ac2018-10-14 01:07:19 -0700210 .flags = DM_FLAG_PRE_RELOC,
Simon Glass987214d2015-04-29 22:26:02 -0600211};