blob: 7756a1a4a8e857905817489148a9b17da982c209 [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
Simon Glass987214d2015-04-29 22:26:02 -06008#include <cpu.h>
9#include <dm.h>
Simon Glassfc557362022-03-04 08:43:05 -070010#include <event.h>
Simon Glassda25eff2019-12-28 10:44:56 -070011#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Stefan Roesefb2cac82016-07-19 07:41:25 +020013#include <pci.h>
Simon Glass987214d2015-04-29 22:26:02 -060014#include <asm/cpu.h>
Bin Meng12d41052015-06-12 14:52:20 +080015#include <asm/cpu_x86.h>
Stefan Roesefb2cac82016-07-19 07:41:25 +020016#include <asm/io.h>
Simon Glass987214d2015-04-29 22:26:02 -060017#include <asm/lapic.h>
Simon Glass987214d2015-04-29 22:26:02 -060018#include <asm/msr.h>
19#include <asm/turbo.h>
20
Stefan Roesefb2cac82016-07-19 07:41:25 +020021#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
27static 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 */
Simon Glassb8357c12023-08-21 21:16:56 -060047static int baytrail_uart_init(void)
Stefan Roesefb2cac82016-07-19 07:41:25 +020048{
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) {
Andrew Scull6520c822022-04-21 16:11:13 +000058 base = dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0, 0, 0, PCI_REGION_TYPE,
Stefan Roesefb2cac82016-07-19 07:41:25 +020059 PCI_REGION_MEM);
60 hsuart_clock_set(base);
61 }
62 }
63
64 return 0;
65}
Simon Glassb8357c12023-08-21 21:16:56 -060066EVENT_SPY_SIMPLE(EVT_DM_POST_INIT_F, baytrail_uart_init);
Stefan Roesefb2cac82016-07-19 07:41:25 +020067
Simon Glass987214d2015-04-29 22:26:02 -060068static void set_max_freq(void)
69{
70 msr_t perf_ctl;
71 msr_t msr;
72
73 /* Enable speed step */
Simon Glass05e85b92019-09-25 08:56:39 -060074 msr = msr_read(MSR_IA32_MISC_ENABLE);
75 msr.lo |= MISC_ENABLE_ENHANCED_SPEEDSTEP;
76 msr_write(MSR_IA32_MISC_ENABLE, msr);
Simon Glass987214d2015-04-29 22:26:02 -060077
78 /*
79 * Set guaranteed ratio [21:16] from IACORE_RATIOS to bits [15:8] of
80 * the PERF_CTL
81 */
82 msr = msr_read(MSR_IACORE_RATIOS);
83 perf_ctl.lo = (msr.lo & 0x3f0000) >> 8;
84
85 /*
Bin Menga45c39d2018-05-24 03:05:59 -070086 * Set guaranteed vid [22:16] from IACORE_VIDS to bits [7:0] of
Simon Glass987214d2015-04-29 22:26:02 -060087 * the PERF_CTL
88 */
89 msr = msr_read(MSR_IACORE_VIDS);
90 perf_ctl.lo |= (msr.lo & 0x7f0000) >> 16;
91 perf_ctl.hi = 0;
92
93 msr_write(MSR_IA32_PERF_CTL, perf_ctl);
94}
95
96static int cpu_x86_baytrail_probe(struct udevice *dev)
97{
Simon Glassa815df72015-08-04 12:34:02 -060098 if (!ll_boot_init())
99 return 0;
Simon Glass987214d2015-04-29 22:26:02 -0600100 debug("Init BayTrail core\n");
101
102 /*
103 * On BayTrail the turbo disable bit is actually scoped at the
104 * building-block level, not package. For non-BSP cores that are
105 * within a building block, enable turbo. The cores within the BSP's
106 * building block will just see it already enabled and move on.
107 */
108 if (lapicid())
109 turbo_enable();
110
111 /* Dynamic L2 shrink enable and threshold */
112 msr_clrsetbits_64(MSR_PMG_CST_CONFIG_CONTROL, 0x3f000f, 0xe0008),
113
114 /* Disable C1E */
115 msr_clrsetbits_64(MSR_POWER_CTL, 2, 0);
116 msr_setbits_64(MSR_POWER_MISC, 0x44);
117
118 /* Set this core to max frequency ratio */
119 set_max_freq();
120
121 return 0;
122}
123
124static unsigned bus_freq(void)
125{
126 msr_t clk_info = msr_read(MSR_BSEL_CR_OVERCLOCK_CONTROL);
127 switch (clk_info.lo & 0x3) {
128 case 0:
129 return 83333333;
130 case 1:
131 return 100000000;
132 case 2:
133 return 133333333;
134 case 3:
135 return 116666666;
136 default:
137 return 0;
138 }
139}
140
141static unsigned long tsc_freq(void)
142{
143 msr_t platform_info;
144 ulong bclk = bus_freq();
145
146 if (!bclk)
147 return 0;
148
149 platform_info = msr_read(MSR_PLATFORM_INFO);
150
151 return bclk * ((platform_info.lo >> 8) & 0xff);
152}
153
Simon Glass791fa452020-01-26 22:06:27 -0700154static int baytrail_get_info(const struct udevice *dev, struct cpu_info *info)
Simon Glass987214d2015-04-29 22:26:02 -0600155{
156 info->cpu_freq = tsc_freq();
157 info->features = 1 << CPU_FEAT_L1_CACHE | 1 << CPU_FEAT_MMU;
158
159 return 0;
160}
161
Simon Glass791fa452020-01-26 22:06:27 -0700162static int baytrail_get_count(const struct udevice *dev)
Bin Mengf967f9a2015-06-17 11:15:36 +0800163{
164 int ecx = 0;
165
166 /*
167 * Use the algorithm described in Intel 64 and IA-32 Architectures
168 * Software Developer's Manual Volume 3 (3A, 3B & 3C): System
169 * Programming Guide, Jan-2015. Section 8.9.2: Hierarchical Mapping
170 * of CPUID Extended Topology Leaf.
171 */
172 while (1) {
173 struct cpuid_result leaf_b;
174
175 leaf_b = cpuid_ext(0xb, ecx);
176
177 /*
178 * Bay Trail doesn't have hyperthreading so just determine the
179 * number of cores by from level type (ecx[15:8] == * 2)
180 */
181 if ((leaf_b.ecx & 0xff00) == 0x0200)
182 return leaf_b.ebx & 0xffff;
183
184 ecx++;
185 }
186
187 return 0;
188}
189
Simon Glass987214d2015-04-29 22:26:02 -0600190static const struct cpu_ops cpu_x86_baytrail_ops = {
Bin Meng12d41052015-06-12 14:52:20 +0800191 .get_desc = cpu_x86_get_desc,
Simon Glass987214d2015-04-29 22:26:02 -0600192 .get_info = baytrail_get_info,
Bin Mengf967f9a2015-06-17 11:15:36 +0800193 .get_count = baytrail_get_count,
Alexander Graf67f66bd2016-08-19 01:23:27 +0200194 .get_vendor = cpu_x86_get_vendor,
Simon Glass987214d2015-04-29 22:26:02 -0600195};
196
197static const struct udevice_id cpu_x86_baytrail_ids[] = {
198 { .compatible = "intel,baytrail-cpu" },
199 { }
200};
201
202U_BOOT_DRIVER(cpu_x86_baytrail_drv) = {
203 .name = "cpu_x86_baytrail",
204 .id = UCLASS_CPU,
205 .of_match = cpu_x86_baytrail_ids,
Bin Meng12d41052015-06-12 14:52:20 +0800206 .bind = cpu_x86_bind,
Simon Glass987214d2015-04-29 22:26:02 -0600207 .probe = cpu_x86_baytrail_probe,
208 .ops = &cpu_x86_baytrail_ops,
Bin Meng09921ac2018-10-14 01:07:19 -0700209 .flags = DM_FLAG_PRE_RELOC,
Simon Glass987214d2015-04-29 22:26:02 -0600210};