2adcf4b242c5a37e94b298566cc3512dd8cac95a
[platform/kernel/u-boot.git] / arch / x86 / cpu / broadwell / cpu.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2016 Google, Inc
4  *
5  * Based on code from coreboot src/soc/intel/broadwell/cpu.c
6  */
7
8 #include <common.h>
9 #include <dm.h>
10 #include <cpu.h>
11 #include <event.h>
12 #include <init.h>
13 #include <log.h>
14 #include <asm/cpu.h>
15 #include <asm/cpu_x86.h>
16 #include <asm/cpu_common.h>
17 #include <asm/global_data.h>
18 #include <asm/intel_regs.h>
19 #include <asm/lpc_common.h>
20 #include <asm/msr.h>
21 #include <asm/pci.h>
22 #include <asm/post.h>
23 #include <asm/turbo.h>
24 #include <asm/arch/cpu.h>
25 #include <asm/arch/pch.h>
26 #include <asm/arch/rcb.h>
27
28 static int broadwell_init_cpu(void *ctx, struct event *event)
29 {
30         struct udevice *dev;
31         int ret;
32
33         /* Start up the LPC so we have serial */
34         ret = uclass_first_device(UCLASS_LPC, &dev);
35         if (ret)
36                 return ret;
37         if (!dev)
38                 return -ENODEV;
39         ret = cpu_set_flex_ratio_to_tdp_nominal();
40         if (ret)
41                 return ret;
42
43         return 0;
44 }
45 EVENT_SPY(EVT_DM_POST_INIT, broadwell_init_cpu);
46
47 void set_max_freq(void)
48 {
49         msr_t msr, perf_ctl;
50
51         if (cpu_config_tdp_levels()) {
52                 /* Set to nominal TDP ratio */
53                 msr = msr_read(MSR_CONFIG_TDP_NOMINAL);
54                 perf_ctl.lo = (msr.lo & 0xff) << 8;
55         } else {
56                 /* Platform Info bits 15:8 give max ratio */
57                 msr = msr_read(MSR_PLATFORM_INFO);
58                 perf_ctl.lo = msr.lo & 0xff00;
59         }
60
61         perf_ctl.hi = 0;
62         msr_write(MSR_IA32_PERF_CTL, perf_ctl);
63
64         debug("CPU: frequency set to %d MHz\n",
65               ((perf_ctl.lo >> 8) & 0xff) * INTEL_BCLK_MHZ);
66 }
67
68 int arch_cpu_init(void)
69 {
70         post_code(POST_CPU_INIT);
71
72 #ifdef CONFIG_TPL
73         /* Do a mini-init if TPL has already done the full init */
74         return x86_cpu_reinit_f();
75 #else
76         return x86_cpu_init_f();
77 #endif
78 }
79
80 int checkcpu(void)
81 {
82         int ret;
83
84         set_max_freq();
85
86         ret = cpu_common_init();
87         if (ret)
88                 return ret;
89         gd->arch.pei_boot_mode = PEI_BOOT_NONE;
90
91         return 0;
92 }
93
94 int print_cpuinfo(void)
95 {
96         char processor_name[CPU_MAX_NAME_LEN];
97         const char *name;
98
99         /* Print processor name */
100         name = cpu_get_name(processor_name);
101         printf("CPU:   %s\n", name);
102
103         return 0;
104 }
105
106 void board_debug_uart_init(void)
107 {
108         /* com1 / com2 decode range */
109         pci_x86_write_config(PCH_DEV_LPC, LPC_IO_DEC, 1 << 4, PCI_SIZE_16);
110
111         pci_x86_write_config(PCH_DEV_LPC, LPC_EN, COMA_LPC_EN, PCI_SIZE_16);
112 }