cpu.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015 Google, Inc
  4. *
  5. * Based on code from coreboot
  6. */
  7. #include <common.h>
  8. #include <cpu.h>
  9. #include <dm.h>
  10. #include <pci.h>
  11. #include <asm/cpu.h>
  12. #include <asm/cpu_x86.h>
  13. #include <asm/io.h>
  14. #include <asm/lapic.h>
  15. #include <asm/msr.h>
  16. #include <asm/turbo.h>
  17. #define BYT_PRV_CLK 0x800
  18. #define BYT_PRV_CLK_EN (1 << 0)
  19. #define BYT_PRV_CLK_M_VAL_SHIFT 1
  20. #define BYT_PRV_CLK_N_VAL_SHIFT 16
  21. #define BYT_PRV_CLK_UPDATE (1 << 31)
  22. static void hsuart_clock_set(void *base)
  23. {
  24. u32 m, n, reg;
  25. /*
  26. * Configure the BayTrail UART clock for the internal HS UARTs
  27. * (PCI devices) to 58982400 Hz
  28. */
  29. m = 0x2400;
  30. n = 0x3d09;
  31. reg = (m << BYT_PRV_CLK_M_VAL_SHIFT) | (n << BYT_PRV_CLK_N_VAL_SHIFT);
  32. writel(reg, base + BYT_PRV_CLK);
  33. reg |= BYT_PRV_CLK_EN | BYT_PRV_CLK_UPDATE;
  34. writel(reg, base + BYT_PRV_CLK);
  35. }
  36. /*
  37. * Configure the internal clock of both SIO HS-UARTs, if they are enabled
  38. * via FSP
  39. */
  40. int arch_cpu_init_dm(void)
  41. {
  42. struct udevice *dev;
  43. void *base;
  44. int ret;
  45. int i;
  46. /* Loop over the 2 HS-UARTs */
  47. for (i = 0; i < 2; i++) {
  48. ret = dm_pci_bus_find_bdf(PCI_BDF(0, 0x1e, 3 + i), &dev);
  49. if (!ret) {
  50. base = dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0,
  51. PCI_REGION_MEM);
  52. hsuart_clock_set(base);
  53. }
  54. }
  55. return 0;
  56. }
  57. static void set_max_freq(void)
  58. {
  59. msr_t perf_ctl;
  60. msr_t msr;
  61. /* Enable speed step */
  62. msr = msr_read(MSR_IA32_MISC_ENABLES);
  63. msr.lo |= (1 << 16);
  64. msr_write(MSR_IA32_MISC_ENABLES, msr);
  65. /*
  66. * Set guaranteed ratio [21:16] from IACORE_RATIOS to bits [15:8] of
  67. * the PERF_CTL
  68. */
  69. msr = msr_read(MSR_IACORE_RATIOS);
  70. perf_ctl.lo = (msr.lo & 0x3f0000) >> 8;
  71. /*
  72. * Set guaranteed vid [22:16] from IACORE_VIDS to bits [7:0] of
  73. * the PERF_CTL
  74. */
  75. msr = msr_read(MSR_IACORE_VIDS);
  76. perf_ctl.lo |= (msr.lo & 0x7f0000) >> 16;
  77. perf_ctl.hi = 0;
  78. msr_write(MSR_IA32_PERF_CTL, perf_ctl);
  79. }
  80. static int cpu_x86_baytrail_probe(struct udevice *dev)
  81. {
  82. if (!ll_boot_init())
  83. return 0;
  84. debug("Init BayTrail core\n");
  85. /*
  86. * On BayTrail the turbo disable bit is actually scoped at the
  87. * building-block level, not package. For non-BSP cores that are
  88. * within a building block, enable turbo. The cores within the BSP's
  89. * building block will just see it already enabled and move on.
  90. */
  91. if (lapicid())
  92. turbo_enable();
  93. /* Dynamic L2 shrink enable and threshold */
  94. msr_clrsetbits_64(MSR_PMG_CST_CONFIG_CONTROL, 0x3f000f, 0xe0008),
  95. /* Disable C1E */
  96. msr_clrsetbits_64(MSR_POWER_CTL, 2, 0);
  97. msr_setbits_64(MSR_POWER_MISC, 0x44);
  98. /* Set this core to max frequency ratio */
  99. set_max_freq();
  100. return 0;
  101. }
  102. static unsigned bus_freq(void)
  103. {
  104. msr_t clk_info = msr_read(MSR_BSEL_CR_OVERCLOCK_CONTROL);
  105. switch (clk_info.lo & 0x3) {
  106. case 0:
  107. return 83333333;
  108. case 1:
  109. return 100000000;
  110. case 2:
  111. return 133333333;
  112. case 3:
  113. return 116666666;
  114. default:
  115. return 0;
  116. }
  117. }
  118. static unsigned long tsc_freq(void)
  119. {
  120. msr_t platform_info;
  121. ulong bclk = bus_freq();
  122. if (!bclk)
  123. return 0;
  124. platform_info = msr_read(MSR_PLATFORM_INFO);
  125. return bclk * ((platform_info.lo >> 8) & 0xff);
  126. }
  127. static int baytrail_get_info(struct udevice *dev, struct cpu_info *info)
  128. {
  129. info->cpu_freq = tsc_freq();
  130. info->features = 1 << CPU_FEAT_L1_CACHE | 1 << CPU_FEAT_MMU;
  131. return 0;
  132. }
  133. static int baytrail_get_count(struct udevice *dev)
  134. {
  135. int ecx = 0;
  136. /*
  137. * Use the algorithm described in Intel 64 and IA-32 Architectures
  138. * Software Developer's Manual Volume 3 (3A, 3B & 3C): System
  139. * Programming Guide, Jan-2015. Section 8.9.2: Hierarchical Mapping
  140. * of CPUID Extended Topology Leaf.
  141. */
  142. while (1) {
  143. struct cpuid_result leaf_b;
  144. leaf_b = cpuid_ext(0xb, ecx);
  145. /*
  146. * Bay Trail doesn't have hyperthreading so just determine the
  147. * number of cores by from level type (ecx[15:8] == * 2)
  148. */
  149. if ((leaf_b.ecx & 0xff00) == 0x0200)
  150. return leaf_b.ebx & 0xffff;
  151. ecx++;
  152. }
  153. return 0;
  154. }
  155. static const struct cpu_ops cpu_x86_baytrail_ops = {
  156. .get_desc = cpu_x86_get_desc,
  157. .get_info = baytrail_get_info,
  158. .get_count = baytrail_get_count,
  159. .get_vendor = cpu_x86_get_vendor,
  160. };
  161. static const struct udevice_id cpu_x86_baytrail_ids[] = {
  162. { .compatible = "intel,baytrail-cpu" },
  163. { }
  164. };
  165. U_BOOT_DRIVER(cpu_x86_baytrail_drv) = {
  166. .name = "cpu_x86_baytrail",
  167. .id = UCLASS_CPU,
  168. .of_match = cpu_x86_baytrail_ids,
  169. .bind = cpu_x86_bind,
  170. .probe = cpu_x86_baytrail_probe,
  171. .ops = &cpu_x86_baytrail_ops,
  172. };