cpu.c 7.0 KB

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