soc: Remove copyright notices
[coreboot.git] / src / soc / intel / common / block / timer / timer.c
blob73690c364850dcf20ac931a603285e59e3da2385
1 /*
2 * This file is part of the coreboot project.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <arch/cpu.h>
16 #include <cpu/x86/msr.h>
17 #include <cpu/x86/tsc.h>
18 #include <intelblocks/msr.h>
20 /* Goldmont Microserver */
21 #define CPU_MODEL_INTEL_ATOM_DENVERTON 0x5F
23 static int get_processor_model(void)
25 struct cpuinfo_x86 c;
27 get_fms(&c, cpuid_eax(1));
29 return c.x86_model;
32 static unsigned long get_hardcoded_crystal_freq(void)
34 unsigned long core_crystal_nominal_freq_khz = 0;
37 * Denverton SoCs don't report crystal clock, and also don't support
38 * CPUID.0x16, so hardcode the 25MHz crystal clock.
40 switch (get_processor_model()) {
41 case CPU_MODEL_INTEL_ATOM_DENVERTON:
42 core_crystal_nominal_freq_khz = 25000;
43 break;
46 return core_crystal_nominal_freq_khz;
50 * Nominal TSC frequency = "core crystal clock frequency" *
51 * CPUID_15h.EBX/CPUID_15h.EAX
53 * Time Stamp Counter
54 * CPUID Initial EAX value = 0x15
55 * EAX Bit 31-0 : An unsigned integer which is the denominator of the
56 * TSC/"core crystal clock" ratio
57 * EBX Bit 31-0 : An unsigned integer which is the numerator of the
58 * TSC/"core crystal clock" ratio
59 * ECX Bit 31-0 : An unsigned integer which is the nominal frequency of the
60 * core crystal clock in Hz.
61 * EDX Bit 31-0 : Reserved = 0
64 static unsigned long calculate_tsc_freq_from_core_crystal(void)
66 unsigned long core_crystal_nominal_freq_khz;
67 struct cpuid_result cpuidr_15h;
69 if (cpuid_get_max_func() < 0x15)
70 return 0;
72 /* CPUID 15H TSC/Crystal ratio, plus optionally Crystal Hz */
73 cpuidr_15h = cpuid(0x15);
75 if (!cpuidr_15h.ebx || !cpuidr_15h.eax)
76 return 0;
78 core_crystal_nominal_freq_khz = cpuidr_15h.ecx / 1000;
80 if (!core_crystal_nominal_freq_khz)
81 core_crystal_nominal_freq_khz = get_hardcoded_crystal_freq();
83 return (core_crystal_nominal_freq_khz * cpuidr_15h.ebx /
84 cpuidr_15h.eax) / 1000;
88 * Processor Frequency Information
89 * CPUID Initial EAX value = 0x16
90 * EAX Bit 31-0 : An unsigned integer which has the processor base frequency
91 * information
92 * EBX Bit 31-0 : An unsigned integer which has maximum frequency information
93 * ECX Bit 31-0 : An unsigned integer which has bus frequency information
94 * EDX Bit 31-0 : Reserved = 0
96 * Refer to Intel SDM Jan 2019 Vol 3B Section 18.7.3
98 static unsigned long get_freq_from_cpuid16h(void)
100 if (cpuid_get_max_func() < 0x16)
101 return 0;
103 return cpuid_eax(0x16);
106 unsigned long tsc_freq_mhz(void)
108 unsigned long tsc_freq;
110 tsc_freq = calculate_tsc_freq_from_core_crystal();
112 if (tsc_freq)
113 return tsc_freq;
116 * Some Intel SoCs like Skylake, Kabylake and Cometlake don't report
117 * the crystal clock, in that case return bus frequency using CPUID.16h
119 return get_freq_from_cpuid16h();