Merge tag 'u-boot-atmel-fixes-2020.07-a' of https://gitlab.denx.de/u-boot/custodians...
[oweals/u-boot.git] / arch / x86 / cpu / ivybridge / cpu.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2014 Google, Inc
4  * (C) Copyright 2008
5  * Graeme Russ, graeme.russ@gmail.com.
6  *
7  * Some portions from coreboot src/mainboard/google/link/romstage.c
8  * and src/cpu/intel/model_206ax/bootblock.c
9  * Copyright (C) 2007-2010 coresystems GmbH
10  * Copyright (C) 2011 Google Inc.
11  */
12
13 #include <common.h>
14 #include <cpu_func.h>
15 #include <dm.h>
16 #include <errno.h>
17 #include <fdtdec.h>
18 #include <init.h>
19 #include <log.h>
20 #include <pch.h>
21 #include <asm/cpu.h>
22 #include <asm/cpu_common.h>
23 #include <asm/intel_regs.h>
24 #include <asm/io.h>
25 #include <asm/lapic.h>
26 #include <asm/lpc_common.h>
27 #include <asm/microcode.h>
28 #include <asm/msr.h>
29 #include <asm/mtrr.h>
30 #include <asm/pci.h>
31 #include <asm/post.h>
32 #include <asm/processor.h>
33 #include <asm/arch/model_206ax.h>
34 #include <asm/arch/pch.h>
35 #include <asm/arch/sandybridge.h>
36
37 DECLARE_GLOBAL_DATA_PTR;
38
39 static int set_flex_ratio_to_tdp_nominal(void)
40 {
41         /* Minimum CPU revision for configurable TDP support */
42         if (cpuid_eax(1) < IVB_CONFIG_TDP_MIN_CPUID)
43                 return -EINVAL;
44
45         return cpu_set_flex_ratio_to_tdp_nominal();
46 }
47
48 int arch_cpu_init(void)
49 {
50         post_code(POST_CPU_INIT);
51
52         return x86_cpu_init_f();
53 }
54
55 int arch_cpu_init_dm(void)
56 {
57         struct pci_controller *hose;
58         struct udevice *bus, *dev;
59         int ret;
60
61         post_code(0x70);
62         ret = uclass_get_device(UCLASS_PCI, 0, &bus);
63         post_code(0x71);
64         if (ret)
65                 return ret;
66         post_code(0x72);
67         hose = dev_get_uclass_priv(bus);
68
69         /* TODO(sjg@chromium.org): Get rid of gd->hose */
70         gd->hose = hose;
71
72         ret = uclass_first_device_err(UCLASS_LPC, &dev);
73         if (ret)
74                 return ret;
75
76         /*
77          * We should do as little as possible before the serial console is
78          * up. Perhaps this should move to later. Our next lot of init
79          * happens in checkcpu() when we have a console
80          */
81         ret = set_flex_ratio_to_tdp_nominal();
82         if (ret)
83                 return ret;
84
85         return 0;
86 }
87
88 #define PCH_EHCI0_TEMP_BAR0 0xe8000000
89 #define PCH_EHCI1_TEMP_BAR0 0xe8000400
90 #define PCH_XHCI_TEMP_BAR0  0xe8001000
91
92 /*
93  * Setup USB controller MMIO BAR to prevent the reference code from
94  * resetting the controller.
95  *
96  * The BAR will be re-assigned during device enumeration so these are only
97  * temporary.
98  *
99  * This is used to speed up the resume path.
100  */
101 static void enable_usb_bar(struct udevice *bus)
102 {
103         pci_dev_t usb0 = PCH_EHCI1_DEV;
104         pci_dev_t usb1 = PCH_EHCI2_DEV;
105         pci_dev_t usb3 = PCH_XHCI_DEV;
106         ulong cmd;
107
108         /* USB Controller 1 */
109         pci_bus_write_config(bus, usb0, PCI_BASE_ADDRESS_0,
110                              PCH_EHCI0_TEMP_BAR0, PCI_SIZE_32);
111         pci_bus_read_config(bus, usb0, PCI_COMMAND, &cmd, PCI_SIZE_32);
112         cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
113         pci_bus_write_config(bus, usb0, PCI_COMMAND, cmd, PCI_SIZE_32);
114
115         /* USB Controller 2 */
116         pci_bus_write_config(bus, usb1, PCI_BASE_ADDRESS_0,
117                              PCH_EHCI1_TEMP_BAR0, PCI_SIZE_32);
118         pci_bus_read_config(bus, usb1, PCI_COMMAND, &cmd, PCI_SIZE_32);
119         cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
120         pci_bus_write_config(bus, usb1, PCI_COMMAND, cmd, PCI_SIZE_32);
121
122         /* USB3 Controller 1 */
123         pci_bus_write_config(bus, usb3, PCI_BASE_ADDRESS_0,
124                              PCH_XHCI_TEMP_BAR0, PCI_SIZE_32);
125         pci_bus_read_config(bus, usb3, PCI_COMMAND, &cmd, PCI_SIZE_32);
126         cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
127         pci_bus_write_config(bus, usb3, PCI_COMMAND, cmd, PCI_SIZE_32);
128 }
129
130 int checkcpu(void)
131 {
132         enum pei_boot_mode_t boot_mode = PEI_BOOT_NONE;
133         struct udevice *dev, *lpc;
134         uint32_t pm1_cnt;
135         uint16_t pm1_sts;
136         int ret;
137
138         /* TODO: cmos_post_init() */
139         if (readl(MCHBAR_REG(SSKPD)) == 0xCAFE) {
140                 debug("soft reset detected\n");
141                 boot_mode = PEI_BOOT_SOFT_RESET;
142
143                 /* System is not happy after keyboard reset... */
144                 debug("Issuing CF9 warm reset\n");
145                 reset_cpu(0);
146         }
147
148         ret = cpu_common_init();
149         if (ret) {
150                 debug("%s: cpu_common_init() failed\n", __func__);
151                 return ret;
152         }
153
154         /* Check PM1_STS[15] to see if we are waking from Sx */
155         pm1_sts = inw(DEFAULT_PMBASE + PM1_STS);
156
157         /* Read PM1_CNT[12:10] to determine which Sx state */
158         pm1_cnt = inl(DEFAULT_PMBASE + PM1_CNT);
159
160         if ((pm1_sts & WAK_STS) && ((pm1_cnt >> 10) & 7) == 5) {
161                 debug("Resume from S3 detected, but disabled.\n");
162         } else {
163                 /*
164                  * TODO: An indication of life might be possible here (e.g.
165                  * keyboard light)
166                  */
167         }
168         post_code(POST_EARLY_INIT);
169
170         /* Enable SPD ROMs and DDR-III DRAM */
171         ret = uclass_first_device_err(UCLASS_I2C, &dev);
172         if (ret) {
173                 debug("%s: Failed to get I2C (ret=%d)\n", __func__, ret);
174                 return ret;
175         }
176
177         /* Prepare USB controller early in S3 resume */
178         if (boot_mode == PEI_BOOT_RESUME) {
179                 uclass_first_device(UCLASS_LPC, &lpc);
180                 enable_usb_bar(pci_get_controller(lpc->parent));
181         }
182
183         gd->arch.pei_boot_mode = boot_mode;
184
185         return 0;
186 }
187
188 int print_cpuinfo(void)
189 {
190         char processor_name[CPU_MAX_NAME_LEN];
191         const char *name;
192
193         /* Print processor name */
194         name = cpu_get_name(processor_name);
195         printf("CPU:   %s\n", name);
196
197         post_code(POST_CPU_INFO);
198
199         return 0;
200 }
201
202 void board_debug_uart_init(void)
203 {
204         /* This enables the debug UART */
205         pci_x86_write_config(PCH_LPC_DEV, LPC_EN, COMA_LPC_EN, PCI_SIZE_16);
206 }