Simon Glass | 0b36ecd | 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 | 30580fc | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 7 | * and src/cpu/intel/model_206ax/bootblock.c |
Simon Glass | 0b36ecd | 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 | e0e7b36 | 2015-03-05 12:25:33 -0700 | [diff] [blame] | 15 | #include <dm.h> |
Simon Glass | dcfac35 | 2014-11-12 22:42:15 -0700 | [diff] [blame] | 16 | #include <errno.h> |
| 17 | #include <fdtdec.h> |
Simon Glass | 0b36ecd | 2014-11-12 22:42:07 -0700 | [diff] [blame] | 18 | #include <asm/cpu.h> |
Simon Glass | f226c41 | 2014-11-12 22:42:19 -0700 | [diff] [blame] | 19 | #include <asm/io.h> |
Simon Glass | d22f5c9 | 2014-11-12 22:42:27 -0700 | [diff] [blame] | 20 | #include <asm/lapic.h> |
Simon Glass | f226c41 | 2014-11-12 22:42:19 -0700 | [diff] [blame] | 21 | #include <asm/msr.h> |
| 22 | #include <asm/mtrr.h> |
Simon Glass | 3274ae0 | 2014-11-12 22:42:13 -0700 | [diff] [blame] | 23 | #include <asm/pci.h> |
Simon Glass | 98f139b | 2014-11-12 22:42:10 -0700 | [diff] [blame] | 24 | #include <asm/post.h> |
Simon Glass | 0b36ecd | 2014-11-12 22:42:07 -0700 | [diff] [blame] | 25 | #include <asm/processor.h> |
Simon Glass | f226c41 | 2014-11-12 22:42:19 -0700 | [diff] [blame] | 26 | #include <asm/arch/model_206ax.h> |
Simon Glass | f79d538 | 2014-11-12 22:42:21 -0700 | [diff] [blame] | 27 | #include <asm/arch/microcode.h> |
Simon Glass | dcfac35 | 2014-11-12 22:42:15 -0700 | [diff] [blame] | 28 | #include <asm/arch/pch.h> |
Simon Glass | 30580fc | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 29 | #include <asm/arch/sandybridge.h> |
Simon Glass | 0b36ecd | 2014-11-12 22:42:07 -0700 | [diff] [blame] | 30 | |
| 31 | DECLARE_GLOBAL_DATA_PTR; |
| 32 | |
Simon Glass | f226c41 | 2014-11-12 22:42:19 -0700 | [diff] [blame] | 33 | static void enable_port80_on_lpc(struct pci_controller *hose, pci_dev_t dev) |
| 34 | { |
| 35 | /* Enable port 80 POST on LPC */ |
| 36 | pci_hose_write_config_dword(hose, dev, PCH_RCBA_BASE, DEFAULT_RCBA | 1); |
| 37 | clrbits_le32(RCB_REG(GCS), 4); |
| 38 | } |
| 39 | |
| 40 | /* |
| 41 | * Enable Prefetching and Caching. |
| 42 | */ |
| 43 | static void enable_spi_prefetch(struct pci_controller *hose, pci_dev_t dev) |
| 44 | { |
| 45 | u8 reg8; |
| 46 | |
| 47 | pci_hose_read_config_byte(hose, dev, 0xdc, ®8); |
| 48 | reg8 &= ~(3 << 2); |
| 49 | reg8 |= (2 << 2); /* Prefetching and Caching Enabled */ |
| 50 | pci_hose_write_config_byte(hose, dev, 0xdc, reg8); |
| 51 | } |
| 52 | |
Simon Glass | f226c41 | 2014-11-12 22:42:19 -0700 | [diff] [blame] | 53 | static int set_flex_ratio_to_tdp_nominal(void) |
| 54 | { |
| 55 | msr_t flex_ratio, msr; |
| 56 | u8 nominal_ratio; |
| 57 | |
| 58 | /* Minimum CPU revision for configurable TDP support */ |
| 59 | if (cpuid_eax(1) < IVB_CONFIG_TDP_MIN_CPUID) |
| 60 | return -EINVAL; |
| 61 | |
| 62 | /* Check for Flex Ratio support */ |
| 63 | flex_ratio = msr_read(MSR_FLEX_RATIO); |
| 64 | if (!(flex_ratio.lo & FLEX_RATIO_EN)) |
| 65 | return -EINVAL; |
| 66 | |
| 67 | /* Check for >0 configurable TDPs */ |
| 68 | msr = msr_read(MSR_PLATFORM_INFO); |
| 69 | if (((msr.hi >> 1) & 3) == 0) |
| 70 | return -EINVAL; |
| 71 | |
| 72 | /* Use nominal TDP ratio for flex ratio */ |
| 73 | msr = msr_read(MSR_CONFIG_TDP_NOMINAL); |
| 74 | nominal_ratio = msr.lo & 0xff; |
| 75 | |
| 76 | /* See if flex ratio is already set to nominal TDP ratio */ |
| 77 | if (((flex_ratio.lo >> 8) & 0xff) == nominal_ratio) |
| 78 | return 0; |
| 79 | |
| 80 | /* Set flex ratio to nominal TDP ratio */ |
| 81 | flex_ratio.lo &= ~0xff00; |
| 82 | flex_ratio.lo |= nominal_ratio << 8; |
| 83 | flex_ratio.lo |= FLEX_RATIO_LOCK; |
| 84 | msr_write(MSR_FLEX_RATIO, flex_ratio); |
| 85 | |
| 86 | /* Set flex ratio in soft reset data register bits 11:6 */ |
| 87 | clrsetbits_le32(RCB_REG(SOFT_RESET_DATA), 0x3f << 6, |
| 88 | (nominal_ratio & 0x3f) << 6); |
| 89 | |
| 90 | /* Set soft reset control to use register value */ |
| 91 | setbits_le32(RCB_REG(SOFT_RESET_CTRL), 1); |
| 92 | |
| 93 | /* Issue warm reset, will be "CPU only" due to soft reset data */ |
| 94 | outb(0x0, PORT_RESET); |
Simon Glass | 1375e9a | 2015-04-28 20:11:30 -0600 | [diff] [blame^] | 95 | outb(SYS_RST | RST_CPU, PORT_RESET); |
Simon Glass | f226c41 | 2014-11-12 22:42:19 -0700 | [diff] [blame] | 96 | cpu_hlt(); |
| 97 | |
| 98 | /* Not reached */ |
| 99 | return -EINVAL; |
| 100 | } |
| 101 | |
| 102 | static void set_spi_speed(void) |
| 103 | { |
| 104 | u32 fdod; |
| 105 | |
| 106 | /* Observe SPI Descriptor Component Section 0 */ |
| 107 | writel(0x1000, RCB_REG(SPI_DESC_COMP0)); |
| 108 | |
| 109 | /* Extract the1 Write/Erase SPI Frequency from descriptor */ |
| 110 | fdod = readl(RCB_REG(SPI_FREQ_WR_ERA)); |
| 111 | fdod >>= 24; |
| 112 | fdod &= 7; |
| 113 | |
| 114 | /* Set Software Sequence frequency to match */ |
| 115 | clrsetbits_8(RCB_REG(SPI_FREQ_SWSEQ), 7, fdod); |
| 116 | } |
| 117 | |
Simon Glass | 0b36ecd | 2014-11-12 22:42:07 -0700 | [diff] [blame] | 118 | int arch_cpu_init(void) |
| 119 | { |
Simon Glass | 7567f46 | 2015-03-05 12:25:17 -0700 | [diff] [blame] | 120 | post_code(POST_CPU_INIT); |
| 121 | timer_set_base(rdtsc()); |
| 122 | |
| 123 | return x86_cpu_init_f(); |
| 124 | } |
| 125 | |
| 126 | int arch_cpu_init_dm(void) |
| 127 | { |
Simon Glass | dcfac35 | 2014-11-12 22:42:15 -0700 | [diff] [blame] | 128 | const void *blob = gd->fdt_blob; |
Simon Glass | 3274ae0 | 2014-11-12 22:42:13 -0700 | [diff] [blame] | 129 | struct pci_controller *hose; |
Simon Glass | e0e7b36 | 2015-03-05 12:25:33 -0700 | [diff] [blame] | 130 | struct udevice *bus; |
Simon Glass | dcfac35 | 2014-11-12 22:42:15 -0700 | [diff] [blame] | 131 | int node; |
Simon Glass | 0b36ecd | 2014-11-12 22:42:07 -0700 | [diff] [blame] | 132 | int ret; |
| 133 | |
Simon Glass | e0e7b36 | 2015-03-05 12:25:33 -0700 | [diff] [blame] | 134 | post_code(0x70); |
| 135 | ret = uclass_get_device(UCLASS_PCI, 0, &bus); |
| 136 | post_code(0x71); |
Simon Glass | 0b36ecd | 2014-11-12 22:42:07 -0700 | [diff] [blame] | 137 | if (ret) |
| 138 | return ret; |
Simon Glass | e0e7b36 | 2015-03-05 12:25:33 -0700 | [diff] [blame] | 139 | post_code(0x72); |
| 140 | hose = dev_get_uclass_priv(bus); |
Simon Glass | 0b36ecd | 2014-11-12 22:42:07 -0700 | [diff] [blame] | 141 | |
Simon Glass | e0e7b36 | 2015-03-05 12:25:33 -0700 | [diff] [blame] | 142 | /* TODO(sjg@chromium.org): Get rid of gd->hose */ |
| 143 | gd->hose = hose; |
Simon Glass | 3274ae0 | 2014-11-12 22:42:13 -0700 | [diff] [blame] | 144 | |
Simon Glass | 06e694f | 2015-03-26 09:29:29 -0600 | [diff] [blame] | 145 | node = fdtdec_next_compatible(blob, 0, COMPAT_INTEL_PCH); |
Simon Glass | dcfac35 | 2014-11-12 22:42:15 -0700 | [diff] [blame] | 146 | if (node < 0) |
| 147 | return -ENOENT; |
| 148 | ret = lpc_early_init(gd->fdt_blob, node, PCH_LPC_DEV); |
| 149 | if (ret) |
| 150 | return ret; |
| 151 | |
Simon Glass | f226c41 | 2014-11-12 22:42:19 -0700 | [diff] [blame] | 152 | enable_spi_prefetch(hose, PCH_LPC_DEV); |
| 153 | |
| 154 | /* This is already done in start.S, but let's do it in C */ |
| 155 | enable_port80_on_lpc(hose, PCH_LPC_DEV); |
| 156 | |
Simon Glass | f226c41 | 2014-11-12 22:42:19 -0700 | [diff] [blame] | 157 | set_spi_speed(); |
| 158 | |
| 159 | /* |
| 160 | * We should do as little as possible before the serial console is |
| 161 | * up. Perhaps this should move to later. Our next lot of init |
| 162 | * happens in print_cpuinfo() when we have a console |
| 163 | */ |
| 164 | ret = set_flex_ratio_to_tdp_nominal(); |
| 165 | if (ret) |
| 166 | return ret; |
| 167 | |
Simon Glass | 0b36ecd | 2014-11-12 22:42:07 -0700 | [diff] [blame] | 168 | return 0; |
| 169 | } |
| 170 | |
Simon Glass | 30580fc | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 171 | static int enable_smbus(void) |
| 172 | { |
| 173 | pci_dev_t dev; |
| 174 | uint16_t value; |
| 175 | |
| 176 | /* Set the SMBus device statically. */ |
| 177 | dev = PCI_BDF(0x0, 0x1f, 0x3); |
| 178 | |
| 179 | /* Check to make sure we've got the right device. */ |
Simon Glass | 240d06d | 2015-03-05 12:25:15 -0700 | [diff] [blame] | 180 | value = x86_pci_read_config16(dev, 0x0); |
Simon Glass | 30580fc | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 181 | if (value != 0x8086) { |
| 182 | printf("SMBus controller not found\n"); |
| 183 | return -ENOSYS; |
| 184 | } |
| 185 | |
| 186 | /* Set SMBus I/O base. */ |
Simon Glass | 240d06d | 2015-03-05 12:25:15 -0700 | [diff] [blame] | 187 | x86_pci_write_config32(dev, SMB_BASE, |
| 188 | SMBUS_IO_BASE | PCI_BASE_ADDRESS_SPACE_IO); |
Simon Glass | 30580fc | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 189 | |
| 190 | /* Set SMBus enable. */ |
Simon Glass | 240d06d | 2015-03-05 12:25:15 -0700 | [diff] [blame] | 191 | x86_pci_write_config8(dev, HOSTC, HST_EN); |
Simon Glass | 30580fc | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 192 | |
| 193 | /* Set SMBus I/O space enable. */ |
Simon Glass | 240d06d | 2015-03-05 12:25:15 -0700 | [diff] [blame] | 194 | x86_pci_write_config16(dev, PCI_COMMAND, PCI_COMMAND_IO); |
Simon Glass | 30580fc | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 195 | |
| 196 | /* Disable interrupt generation. */ |
| 197 | outb(0, SMBUS_IO_BASE + SMBHSTCTL); |
| 198 | |
| 199 | /* Clear any lingering errors, so transactions can run. */ |
| 200 | outb(inb(SMBUS_IO_BASE + SMBHSTSTAT), SMBUS_IO_BASE + SMBHSTSTAT); |
| 201 | debug("SMBus controller enabled\n"); |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | #define PCH_EHCI0_TEMP_BAR0 0xe8000000 |
| 207 | #define PCH_EHCI1_TEMP_BAR0 0xe8000400 |
| 208 | #define PCH_XHCI_TEMP_BAR0 0xe8001000 |
| 209 | |
| 210 | /* |
| 211 | * Setup USB controller MMIO BAR to prevent the reference code from |
| 212 | * resetting the controller. |
| 213 | * |
| 214 | * The BAR will be re-assigned during device enumeration so these are only |
| 215 | * temporary. |
| 216 | * |
| 217 | * This is used to speed up the resume path. |
| 218 | */ |
| 219 | static void enable_usb_bar(void) |
| 220 | { |
| 221 | pci_dev_t usb0 = PCH_EHCI1_DEV; |
| 222 | pci_dev_t usb1 = PCH_EHCI2_DEV; |
| 223 | pci_dev_t usb3 = PCH_XHCI_DEV; |
| 224 | u32 cmd; |
| 225 | |
| 226 | /* USB Controller 1 */ |
Simon Glass | 240d06d | 2015-03-05 12:25:15 -0700 | [diff] [blame] | 227 | x86_pci_write_config32(usb0, PCI_BASE_ADDRESS_0, |
| 228 | PCH_EHCI0_TEMP_BAR0); |
| 229 | cmd = x86_pci_read_config32(usb0, PCI_COMMAND); |
Simon Glass | 30580fc | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 230 | cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY; |
Simon Glass | 240d06d | 2015-03-05 12:25:15 -0700 | [diff] [blame] | 231 | x86_pci_write_config32(usb0, PCI_COMMAND, cmd); |
Simon Glass | 30580fc | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 232 | |
| 233 | /* USB Controller 1 */ |
Simon Glass | 240d06d | 2015-03-05 12:25:15 -0700 | [diff] [blame] | 234 | x86_pci_write_config32(usb1, PCI_BASE_ADDRESS_0, |
| 235 | PCH_EHCI1_TEMP_BAR0); |
| 236 | cmd = x86_pci_read_config32(usb1, PCI_COMMAND); |
Simon Glass | 30580fc | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 237 | cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY; |
Simon Glass | 240d06d | 2015-03-05 12:25:15 -0700 | [diff] [blame] | 238 | x86_pci_write_config32(usb1, PCI_COMMAND, cmd); |
Simon Glass | 30580fc | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 239 | |
| 240 | /* USB3 Controller */ |
Simon Glass | 240d06d | 2015-03-05 12:25:15 -0700 | [diff] [blame] | 241 | x86_pci_write_config32(usb3, PCI_BASE_ADDRESS_0, |
| 242 | PCH_XHCI_TEMP_BAR0); |
| 243 | cmd = x86_pci_read_config32(usb3, PCI_COMMAND); |
Simon Glass | 30580fc | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 244 | cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY; |
Simon Glass | 240d06d | 2015-03-05 12:25:15 -0700 | [diff] [blame] | 245 | x86_pci_write_config32(usb3, PCI_COMMAND, cmd); |
Simon Glass | 30580fc | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Simon Glass | 367077a | 2014-11-12 22:42:20 -0700 | [diff] [blame] | 248 | static int report_bist_failure(void) |
| 249 | { |
| 250 | if (gd->arch.bist != 0) { |
Bin Meng | 642d248 | 2014-12-12 21:05:30 +0800 | [diff] [blame] | 251 | post_code(POST_BIST_FAILURE); |
Simon Glass | 367077a | 2014-11-12 22:42:20 -0700 | [diff] [blame] | 252 | printf("BIST failed: %08x\n", gd->arch.bist); |
| 253 | return -EFAULT; |
| 254 | } |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
Simon Glass | 0b36ecd | 2014-11-12 22:42:07 -0700 | [diff] [blame] | 259 | int print_cpuinfo(void) |
| 260 | { |
Simon Glass | 30580fc | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 261 | enum pei_boot_mode_t boot_mode = PEI_BOOT_NONE; |
Simon Glass | 0b36ecd | 2014-11-12 22:42:07 -0700 | [diff] [blame] | 262 | char processor_name[CPU_MAX_NAME_LEN]; |
| 263 | const char *name; |
Simon Glass | 30580fc | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 264 | uint32_t pm1_cnt; |
| 265 | uint16_t pm1_sts; |
Simon Glass | 367077a | 2014-11-12 22:42:20 -0700 | [diff] [blame] | 266 | int ret; |
| 267 | |
| 268 | /* Halt if there was a built in self test failure */ |
| 269 | ret = report_bist_failure(); |
| 270 | if (ret) |
| 271 | return ret; |
Simon Glass | 0b36ecd | 2014-11-12 22:42:07 -0700 | [diff] [blame] | 272 | |
Simon Glass | d22f5c9 | 2014-11-12 22:42:27 -0700 | [diff] [blame] | 273 | enable_lapic(); |
| 274 | |
Simon Glass | f79d538 | 2014-11-12 22:42:21 -0700 | [diff] [blame] | 275 | ret = microcode_update_intel(); |
Simon Glass | 9281eb5 | 2015-01-01 16:18:14 -0700 | [diff] [blame] | 276 | if (ret) |
Simon Glass | f79d538 | 2014-11-12 22:42:21 -0700 | [diff] [blame] | 277 | return ret; |
| 278 | |
Simon Glass | 30580fc | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 279 | /* Enable upper 128bytes of CMOS */ |
| 280 | writel(1 << 2, RCB_REG(RC)); |
| 281 | |
| 282 | /* TODO: cmos_post_init() */ |
| 283 | if (readl(MCHBAR_REG(SSKPD)) == 0xCAFE) { |
| 284 | debug("soft reset detected\n"); |
| 285 | boot_mode = PEI_BOOT_SOFT_RESET; |
| 286 | |
| 287 | /* System is not happy after keyboard reset... */ |
| 288 | debug("Issuing CF9 warm reset\n"); |
Simon Glass | 1375e9a | 2015-04-28 20:11:30 -0600 | [diff] [blame^] | 289 | reset_cpu(0); |
Simon Glass | 30580fc | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /* Early chipset init required before RAM init can work */ |
| 293 | sandybridge_early_init(SANDYBRIDGE_MOBILE); |
| 294 | |
| 295 | /* Check PM1_STS[15] to see if we are waking from Sx */ |
| 296 | pm1_sts = inw(DEFAULT_PMBASE + PM1_STS); |
| 297 | |
| 298 | /* Read PM1_CNT[12:10] to determine which Sx state */ |
| 299 | pm1_cnt = inl(DEFAULT_PMBASE + PM1_CNT); |
| 300 | |
| 301 | if ((pm1_sts & WAK_STS) && ((pm1_cnt >> 10) & 7) == 5) { |
| 302 | #if CONFIG_HAVE_ACPI_RESUME |
| 303 | debug("Resume from S3 detected.\n"); |
| 304 | boot_mode = PEI_BOOT_RESUME; |
| 305 | /* Clear SLP_TYPE. This will break stage2 but |
| 306 | * we care for that when we get there. |
| 307 | */ |
| 308 | outl(pm1_cnt & ~(7 << 10), DEFAULT_PMBASE + PM1_CNT); |
| 309 | #else |
| 310 | debug("Resume from S3 detected, but disabled.\n"); |
| 311 | #endif |
| 312 | } else { |
| 313 | /* |
| 314 | * TODO: An indication of life might be possible here (e.g. |
| 315 | * keyboard light) |
| 316 | */ |
| 317 | } |
| 318 | post_code(POST_EARLY_INIT); |
| 319 | |
| 320 | /* Enable SPD ROMs and DDR-III DRAM */ |
| 321 | ret = enable_smbus(); |
| 322 | if (ret) |
| 323 | return ret; |
| 324 | |
| 325 | /* Prepare USB controller early in S3 resume */ |
| 326 | if (boot_mode == PEI_BOOT_RESUME) |
| 327 | enable_usb_bar(); |
| 328 | |
| 329 | gd->arch.pei_boot_mode = boot_mode; |
| 330 | |
| 331 | /* TODO: Move this to the board or driver */ |
Simon Glass | 240d06d | 2015-03-05 12:25:15 -0700 | [diff] [blame] | 332 | x86_pci_write_config32(PCH_LPC_DEV, GPIO_BASE, DEFAULT_GPIOBASE | 1); |
| 333 | x86_pci_write_config32(PCH_LPC_DEV, GPIO_CNTL, 0x10); |
Simon Glass | 30580fc | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 334 | |
Simon Glass | 0b36ecd | 2014-11-12 22:42:07 -0700 | [diff] [blame] | 335 | /* Print processor name */ |
| 336 | name = cpu_get_name(processor_name); |
| 337 | printf("CPU: %s\n", name); |
| 338 | |
Simon Glass | 30580fc | 2014-11-12 22:42:23 -0700 | [diff] [blame] | 339 | post_code(POST_CPU_INFO); |
| 340 | |
Simon Glass | 0b36ecd | 2014-11-12 22:42:07 -0700 | [diff] [blame] | 341 | return 0; |
| 342 | } |