Simon Glass | 8ef0757 | 2014-11-12 22:42:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 Google, Inc |
| 3 | * (C) Copyright 2008 |
| 4 | * Graeme Russ, graeme.russ@gmail.com. |
| 5 | * |
| 6 | * Some portions from coreboot src/mainboard/google/link/romstage.c |
Simon Glass | 8e0df06 | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 7 | * and src/cpu/intel/model_206ax/bootblock.c |
Simon Glass | 8ef0757 | 2014-11-12 22:42:07 -0700 | [diff] [blame] | 8 | * Copyright (C) 2007-2010 coresystems GmbH |
| 9 | * Copyright (C) 2011 Google Inc. |
| 10 | * |
| 11 | * SPDX-License-Identifier: GPL-2.0 |
| 12 | */ |
| 13 | |
| 14 | #include <common.h> |
Simon Glass | aad78d2 | 2015-03-05 12:25:33 -0700 | [diff] [blame] | 15 | #include <dm.h> |
Simon Glass | 2b60515 | 2014-11-12 22:42:15 -0700 | [diff] [blame] | 16 | #include <errno.h> |
| 17 | #include <fdtdec.h> |
Simon Glass | 858361b | 2016-01-17 16:11:13 -0700 | [diff] [blame] | 18 | #include <pch.h> |
Simon Glass | 8ef0757 | 2014-11-12 22:42:07 -0700 | [diff] [blame] | 19 | #include <asm/cpu.h> |
Simon Glass | f5fbbe9 | 2014-11-12 22:42:19 -0700 | [diff] [blame] | 20 | #include <asm/io.h> |
Simon Glass | 3eafce0 | 2014-11-12 22:42:27 -0700 | [diff] [blame] | 21 | #include <asm/lapic.h> |
Simon Glass | f5fbbe9 | 2014-11-12 22:42:19 -0700 | [diff] [blame] | 22 | #include <asm/msr.h> |
| 23 | #include <asm/mtrr.h> |
Simon Glass | 6e5b12b | 2014-11-12 22:42:13 -0700 | [diff] [blame] | 24 | #include <asm/pci.h> |
Simon Glass | 70a09c6 | 2014-11-12 22:42:10 -0700 | [diff] [blame] | 25 | #include <asm/post.h> |
Simon Glass | 8ef0757 | 2014-11-12 22:42:07 -0700 | [diff] [blame] | 26 | #include <asm/processor.h> |
Simon Glass | f5fbbe9 | 2014-11-12 22:42:19 -0700 | [diff] [blame] | 27 | #include <asm/arch/model_206ax.h> |
Simon Glass | 77f9b1f | 2014-11-12 22:42:21 -0700 | [diff] [blame] | 28 | #include <asm/arch/microcode.h> |
Simon Glass | 2b60515 | 2014-11-12 22:42:15 -0700 | [diff] [blame] | 29 | #include <asm/arch/pch.h> |
Simon Glass | 8e0df06 | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 30 | #include <asm/arch/sandybridge.h> |
Simon Glass | 8ef0757 | 2014-11-12 22:42:07 -0700 | [diff] [blame] | 31 | |
| 32 | DECLARE_GLOBAL_DATA_PTR; |
| 33 | |
Simon Glass | f5fbbe9 | 2014-11-12 22:42:19 -0700 | [diff] [blame] | 34 | static int set_flex_ratio_to_tdp_nominal(void) |
| 35 | { |
| 36 | msr_t flex_ratio, msr; |
| 37 | u8 nominal_ratio; |
| 38 | |
| 39 | /* Minimum CPU revision for configurable TDP support */ |
| 40 | if (cpuid_eax(1) < IVB_CONFIG_TDP_MIN_CPUID) |
| 41 | return -EINVAL; |
| 42 | |
| 43 | /* Check for Flex Ratio support */ |
| 44 | flex_ratio = msr_read(MSR_FLEX_RATIO); |
| 45 | if (!(flex_ratio.lo & FLEX_RATIO_EN)) |
| 46 | return -EINVAL; |
| 47 | |
| 48 | /* Check for >0 configurable TDPs */ |
| 49 | msr = msr_read(MSR_PLATFORM_INFO); |
| 50 | if (((msr.hi >> 1) & 3) == 0) |
| 51 | return -EINVAL; |
| 52 | |
| 53 | /* Use nominal TDP ratio for flex ratio */ |
| 54 | msr = msr_read(MSR_CONFIG_TDP_NOMINAL); |
| 55 | nominal_ratio = msr.lo & 0xff; |
| 56 | |
| 57 | /* See if flex ratio is already set to nominal TDP ratio */ |
| 58 | if (((flex_ratio.lo >> 8) & 0xff) == nominal_ratio) |
| 59 | return 0; |
| 60 | |
| 61 | /* Set flex ratio to nominal TDP ratio */ |
| 62 | flex_ratio.lo &= ~0xff00; |
| 63 | flex_ratio.lo |= nominal_ratio << 8; |
| 64 | flex_ratio.lo |= FLEX_RATIO_LOCK; |
| 65 | msr_write(MSR_FLEX_RATIO, flex_ratio); |
| 66 | |
| 67 | /* Set flex ratio in soft reset data register bits 11:6 */ |
| 68 | clrsetbits_le32(RCB_REG(SOFT_RESET_DATA), 0x3f << 6, |
| 69 | (nominal_ratio & 0x3f) << 6); |
| 70 | |
| 71 | /* Set soft reset control to use register value */ |
| 72 | setbits_le32(RCB_REG(SOFT_RESET_CTRL), 1); |
| 73 | |
| 74 | /* Issue warm reset, will be "CPU only" due to soft reset data */ |
| 75 | outb(0x0, PORT_RESET); |
Simon Glass | 5021c81 | 2015-04-28 20:11:30 -0600 | [diff] [blame] | 76 | outb(SYS_RST | RST_CPU, PORT_RESET); |
Simon Glass | f5fbbe9 | 2014-11-12 22:42:19 -0700 | [diff] [blame] | 77 | cpu_hlt(); |
| 78 | |
| 79 | /* Not reached */ |
| 80 | return -EINVAL; |
| 81 | } |
| 82 | |
Simon Glass | 8ef0757 | 2014-11-12 22:42:07 -0700 | [diff] [blame] | 83 | int arch_cpu_init(void) |
| 84 | { |
Simon Glass | 161d2e4 | 2015-03-05 12:25:17 -0700 | [diff] [blame] | 85 | post_code(POST_CPU_INIT); |
Simon Glass | 161d2e4 | 2015-03-05 12:25:17 -0700 | [diff] [blame] | 86 | |
| 87 | return x86_cpu_init_f(); |
| 88 | } |
| 89 | |
| 90 | int arch_cpu_init_dm(void) |
| 91 | { |
Simon Glass | 6e5b12b | 2014-11-12 22:42:13 -0700 | [diff] [blame] | 92 | struct pci_controller *hose; |
Simon Glass | 4acc83d | 2016-01-17 16:11:10 -0700 | [diff] [blame] | 93 | struct udevice *bus, *dev; |
Simon Glass | 8ef0757 | 2014-11-12 22:42:07 -0700 | [diff] [blame] | 94 | int ret; |
| 95 | |
Simon Glass | aad78d2 | 2015-03-05 12:25:33 -0700 | [diff] [blame] | 96 | post_code(0x70); |
| 97 | ret = uclass_get_device(UCLASS_PCI, 0, &bus); |
| 98 | post_code(0x71); |
Simon Glass | 8ef0757 | 2014-11-12 22:42:07 -0700 | [diff] [blame] | 99 | if (ret) |
| 100 | return ret; |
Simon Glass | aad78d2 | 2015-03-05 12:25:33 -0700 | [diff] [blame] | 101 | post_code(0x72); |
| 102 | hose = dev_get_uclass_priv(bus); |
Simon Glass | 8ef0757 | 2014-11-12 22:42:07 -0700 | [diff] [blame] | 103 | |
Simon Glass | aad78d2 | 2015-03-05 12:25:33 -0700 | [diff] [blame] | 104 | /* TODO(sjg@chromium.org): Get rid of gd->hose */ |
| 105 | gd->hose = hose; |
Simon Glass | 6e5b12b | 2014-11-12 22:42:13 -0700 | [diff] [blame] | 106 | |
Simon Glass | 4acc83d | 2016-01-17 16:11:10 -0700 | [diff] [blame] | 107 | ret = uclass_first_device(UCLASS_LPC, &dev); |
| 108 | if (!dev) |
| 109 | return -ENODEV; |
| 110 | |
Simon Glass | f5fbbe9 | 2014-11-12 22:42:19 -0700 | [diff] [blame] | 111 | /* |
| 112 | * We should do as little as possible before the serial console is |
| 113 | * up. Perhaps this should move to later. Our next lot of init |
| 114 | * happens in print_cpuinfo() when we have a console |
| 115 | */ |
| 116 | ret = set_flex_ratio_to_tdp_nominal(); |
| 117 | if (ret) |
| 118 | return ret; |
| 119 | |
Simon Glass | 8ef0757 | 2014-11-12 22:42:07 -0700 | [diff] [blame] | 120 | return 0; |
| 121 | } |
| 122 | |
Simon Glass | 8e0df06 | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 123 | static int enable_smbus(void) |
| 124 | { |
| 125 | pci_dev_t dev; |
| 126 | uint16_t value; |
| 127 | |
| 128 | /* Set the SMBus device statically. */ |
| 129 | dev = PCI_BDF(0x0, 0x1f, 0x3); |
| 130 | |
| 131 | /* Check to make sure we've got the right device. */ |
Simon Glass | 31f57c2 | 2015-03-05 12:25:15 -0700 | [diff] [blame] | 132 | value = x86_pci_read_config16(dev, 0x0); |
Simon Glass | 8e0df06 | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 133 | if (value != 0x8086) { |
| 134 | printf("SMBus controller not found\n"); |
| 135 | return -ENOSYS; |
| 136 | } |
| 137 | |
| 138 | /* Set SMBus I/O base. */ |
Simon Glass | 31f57c2 | 2015-03-05 12:25:15 -0700 | [diff] [blame] | 139 | x86_pci_write_config32(dev, SMB_BASE, |
| 140 | SMBUS_IO_BASE | PCI_BASE_ADDRESS_SPACE_IO); |
Simon Glass | 8e0df06 | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 141 | |
| 142 | /* Set SMBus enable. */ |
Simon Glass | 31f57c2 | 2015-03-05 12:25:15 -0700 | [diff] [blame] | 143 | x86_pci_write_config8(dev, HOSTC, HST_EN); |
Simon Glass | 8e0df06 | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 144 | |
| 145 | /* Set SMBus I/O space enable. */ |
Simon Glass | 31f57c2 | 2015-03-05 12:25:15 -0700 | [diff] [blame] | 146 | x86_pci_write_config16(dev, PCI_COMMAND, PCI_COMMAND_IO); |
Simon Glass | 8e0df06 | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 147 | |
| 148 | /* Disable interrupt generation. */ |
| 149 | outb(0, SMBUS_IO_BASE + SMBHSTCTL); |
| 150 | |
| 151 | /* Clear any lingering errors, so transactions can run. */ |
| 152 | outb(inb(SMBUS_IO_BASE + SMBHSTSTAT), SMBUS_IO_BASE + SMBHSTSTAT); |
| 153 | debug("SMBus controller enabled\n"); |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | #define PCH_EHCI0_TEMP_BAR0 0xe8000000 |
| 159 | #define PCH_EHCI1_TEMP_BAR0 0xe8000400 |
| 160 | #define PCH_XHCI_TEMP_BAR0 0xe8001000 |
| 161 | |
| 162 | /* |
| 163 | * Setup USB controller MMIO BAR to prevent the reference code from |
| 164 | * resetting the controller. |
| 165 | * |
| 166 | * The BAR will be re-assigned during device enumeration so these are only |
| 167 | * temporary. |
| 168 | * |
| 169 | * This is used to speed up the resume path. |
| 170 | */ |
| 171 | static void enable_usb_bar(void) |
| 172 | { |
| 173 | pci_dev_t usb0 = PCH_EHCI1_DEV; |
| 174 | pci_dev_t usb1 = PCH_EHCI2_DEV; |
| 175 | pci_dev_t usb3 = PCH_XHCI_DEV; |
| 176 | u32 cmd; |
| 177 | |
| 178 | /* USB Controller 1 */ |
Simon Glass | 31f57c2 | 2015-03-05 12:25:15 -0700 | [diff] [blame] | 179 | x86_pci_write_config32(usb0, PCI_BASE_ADDRESS_0, |
| 180 | PCH_EHCI0_TEMP_BAR0); |
| 181 | cmd = x86_pci_read_config32(usb0, PCI_COMMAND); |
Simon Glass | 8e0df06 | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 182 | cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY; |
Simon Glass | 31f57c2 | 2015-03-05 12:25:15 -0700 | [diff] [blame] | 183 | x86_pci_write_config32(usb0, PCI_COMMAND, cmd); |
Simon Glass | 8e0df06 | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 184 | |
| 185 | /* USB Controller 1 */ |
Simon Glass | 31f57c2 | 2015-03-05 12:25:15 -0700 | [diff] [blame] | 186 | x86_pci_write_config32(usb1, PCI_BASE_ADDRESS_0, |
| 187 | PCH_EHCI1_TEMP_BAR0); |
| 188 | cmd = x86_pci_read_config32(usb1, PCI_COMMAND); |
Simon Glass | 8e0df06 | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 189 | cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY; |
Simon Glass | 31f57c2 | 2015-03-05 12:25:15 -0700 | [diff] [blame] | 190 | x86_pci_write_config32(usb1, PCI_COMMAND, cmd); |
Simon Glass | 8e0df06 | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 191 | |
| 192 | /* USB3 Controller */ |
Simon Glass | 31f57c2 | 2015-03-05 12:25:15 -0700 | [diff] [blame] | 193 | x86_pci_write_config32(usb3, PCI_BASE_ADDRESS_0, |
| 194 | PCH_XHCI_TEMP_BAR0); |
| 195 | cmd = x86_pci_read_config32(usb3, PCI_COMMAND); |
Simon Glass | 8e0df06 | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 196 | cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY; |
Simon Glass | 31f57c2 | 2015-03-05 12:25:15 -0700 | [diff] [blame] | 197 | x86_pci_write_config32(usb3, PCI_COMMAND, cmd); |
Simon Glass | 8e0df06 | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Simon Glass | 94060ff | 2014-11-12 22:42:20 -0700 | [diff] [blame] | 200 | static int report_bist_failure(void) |
| 201 | { |
| 202 | if (gd->arch.bist != 0) { |
Bin Meng | 95a5a47 | 2014-12-12 21:05:30 +0800 | [diff] [blame] | 203 | post_code(POST_BIST_FAILURE); |
Simon Glass | 94060ff | 2014-11-12 22:42:20 -0700 | [diff] [blame] | 204 | printf("BIST failed: %08x\n", gd->arch.bist); |
| 205 | return -EFAULT; |
| 206 | } |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
Simon Glass | 8ef0757 | 2014-11-12 22:42:07 -0700 | [diff] [blame] | 211 | int print_cpuinfo(void) |
| 212 | { |
Simon Glass | 8e0df06 | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 213 | enum pei_boot_mode_t boot_mode = PEI_BOOT_NONE; |
Simon Glass | 8ef0757 | 2014-11-12 22:42:07 -0700 | [diff] [blame] | 214 | char processor_name[CPU_MAX_NAME_LEN]; |
Simon Glass | 858361b | 2016-01-17 16:11:13 -0700 | [diff] [blame] | 215 | struct udevice *dev; |
Simon Glass | 8ef0757 | 2014-11-12 22:42:07 -0700 | [diff] [blame] | 216 | const char *name; |
Simon Glass | 8e0df06 | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 217 | uint32_t pm1_cnt; |
| 218 | uint16_t pm1_sts; |
Simon Glass | 94060ff | 2014-11-12 22:42:20 -0700 | [diff] [blame] | 219 | int ret; |
| 220 | |
| 221 | /* Halt if there was a built in self test failure */ |
| 222 | ret = report_bist_failure(); |
| 223 | if (ret) |
| 224 | return ret; |
Simon Glass | 8ef0757 | 2014-11-12 22:42:07 -0700 | [diff] [blame] | 225 | |
Simon Glass | 3eafce0 | 2014-11-12 22:42:27 -0700 | [diff] [blame] | 226 | enable_lapic(); |
| 227 | |
Simon Glass | 77f9b1f | 2014-11-12 22:42:21 -0700 | [diff] [blame] | 228 | ret = microcode_update_intel(); |
Simon Glass | c72f74e | 2015-01-01 16:18:14 -0700 | [diff] [blame] | 229 | if (ret) |
Simon Glass | 77f9b1f | 2014-11-12 22:42:21 -0700 | [diff] [blame] | 230 | return ret; |
| 231 | |
Simon Glass | 8e0df06 | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 232 | /* Enable upper 128bytes of CMOS */ |
| 233 | writel(1 << 2, RCB_REG(RC)); |
| 234 | |
| 235 | /* TODO: cmos_post_init() */ |
| 236 | if (readl(MCHBAR_REG(SSKPD)) == 0xCAFE) { |
| 237 | debug("soft reset detected\n"); |
| 238 | boot_mode = PEI_BOOT_SOFT_RESET; |
| 239 | |
| 240 | /* System is not happy after keyboard reset... */ |
| 241 | debug("Issuing CF9 warm reset\n"); |
Simon Glass | 5021c81 | 2015-04-28 20:11:30 -0600 | [diff] [blame] | 242 | reset_cpu(0); |
Simon Glass | 8e0df06 | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /* Early chipset init required before RAM init can work */ |
Simon Glass | 655925a | 2016-01-17 16:11:16 -0700 | [diff] [blame^] | 246 | uclass_first_device(UCLASS_NORTHBRIDGE, &dev); |
| 247 | |
Simon Glass | 858361b | 2016-01-17 16:11:13 -0700 | [diff] [blame] | 248 | ret = uclass_first_device(UCLASS_PCH, &dev); |
| 249 | if (ret) |
| 250 | return ret; |
| 251 | if (!dev) |
| 252 | return -ENODEV; |
| 253 | |
Simon Glass | 8e0df06 | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 254 | sandybridge_early_init(SANDYBRIDGE_MOBILE); |
| 255 | |
| 256 | /* Check PM1_STS[15] to see if we are waking from Sx */ |
| 257 | pm1_sts = inw(DEFAULT_PMBASE + PM1_STS); |
| 258 | |
| 259 | /* Read PM1_CNT[12:10] to determine which Sx state */ |
| 260 | pm1_cnt = inl(DEFAULT_PMBASE + PM1_CNT); |
| 261 | |
| 262 | if ((pm1_sts & WAK_STS) && ((pm1_cnt >> 10) & 7) == 5) { |
Simon Glass | 8e0df06 | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 263 | debug("Resume from S3 detected, but disabled.\n"); |
Simon Glass | 8e0df06 | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 264 | } else { |
| 265 | /* |
| 266 | * TODO: An indication of life might be possible here (e.g. |
| 267 | * keyboard light) |
| 268 | */ |
| 269 | } |
| 270 | post_code(POST_EARLY_INIT); |
| 271 | |
| 272 | /* Enable SPD ROMs and DDR-III DRAM */ |
| 273 | ret = enable_smbus(); |
| 274 | if (ret) |
| 275 | return ret; |
| 276 | |
| 277 | /* Prepare USB controller early in S3 resume */ |
| 278 | if (boot_mode == PEI_BOOT_RESUME) |
| 279 | enable_usb_bar(); |
| 280 | |
| 281 | gd->arch.pei_boot_mode = boot_mode; |
| 282 | |
| 283 | /* TODO: Move this to the board or driver */ |
Simon Glass | 31f57c2 | 2015-03-05 12:25:15 -0700 | [diff] [blame] | 284 | x86_pci_write_config32(PCH_LPC_DEV, GPIO_BASE, DEFAULT_GPIOBASE | 1); |
| 285 | x86_pci_write_config32(PCH_LPC_DEV, GPIO_CNTL, 0x10); |
Simon Glass | 8e0df06 | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 286 | |
Simon Glass | 8ef0757 | 2014-11-12 22:42:07 -0700 | [diff] [blame] | 287 | /* Print processor name */ |
| 288 | name = cpu_get_name(processor_name); |
| 289 | printf("CPU: %s\n", name); |
| 290 | |
Simon Glass | 8e0df06 | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 291 | post_code(POST_CPU_INFO); |
| 292 | |
Simon Glass | 8ef0757 | 2014-11-12 22:42:07 -0700 | [diff] [blame] | 293 | return 0; |
| 294 | } |
Simon Glass | 7b95252 | 2015-10-18 19:51:27 -0600 | [diff] [blame] | 295 | |
| 296 | void board_debug_uart_init(void) |
| 297 | { |
| 298 | /* This enables the debug UART */ |
| 299 | pci_x86_write_config(NULL, PCH_LPC_DEV, LPC_EN, COMA_LPC_EN, |
| 300 | PCI_SIZE_16); |
| 301 | } |