pch.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2016 Google, Inc
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <pch.h>
  8. #include <asm/cpu.h>
  9. #include <asm/gpio.h>
  10. #include <asm/i8259.h>
  11. #include <asm/intel_regs.h>
  12. #include <asm/io.h>
  13. #include <asm/ioapic.h>
  14. #include <asm/lpc_common.h>
  15. #include <asm/pch_common.h>
  16. #include <asm/arch/cpu.h>
  17. #include <asm/arch/gpio.h>
  18. #include <asm/arch/iomap.h>
  19. #include <asm/arch/pch.h>
  20. #include <asm/arch/pm.h>
  21. #include <asm/arch/rcb.h>
  22. #include <asm/arch/spi.h>
  23. #define BIOS_CTRL 0xdc
  24. bool cpu_is_ult(void)
  25. {
  26. u32 fm = cpu_get_family_model();
  27. return fm == BROADWELL_FAMILY_ULT || fm == HASWELL_FAMILY_ULT;
  28. }
  29. static int broadwell_pch_early_init(struct udevice *dev)
  30. {
  31. struct gpio_desc desc;
  32. struct udevice *bus;
  33. pci_dev_t bdf;
  34. int ret;
  35. dm_pci_write_config32(dev, PCH_RCBA, RCB_BASE_ADDRESS | 1);
  36. dm_pci_write_config32(dev, PMBASE, ACPI_BASE_ADDRESS | 1);
  37. dm_pci_write_config8(dev, ACPI_CNTL, ACPI_EN);
  38. dm_pci_write_config32(dev, GPIO_BASE, GPIO_BASE_ADDRESS | 1);
  39. dm_pci_write_config8(dev, GPIO_CNTL, GPIO_EN);
  40. /* Enable IOAPIC */
  41. writew(0x1000, RCB_REG(OIC));
  42. /* Read back for posted write */
  43. readw(RCB_REG(OIC));
  44. /* Set HPET address and enable it */
  45. clrsetbits_le32(RCB_REG(HPTC), 3, 1 << 7);
  46. /* Read back for posted write */
  47. readl(RCB_REG(HPTC));
  48. /* Enable HPET to start counter */
  49. setbits_le32(HPET_BASE_ADDRESS + 0x10, 1 << 0);
  50. setbits_le32(RCB_REG(GCS), 1 << 5);
  51. /*
  52. * Enable PP3300_AUTOBAHN_EN after initial GPIO setup
  53. * to prevent possible brownout. This will cause the GPIOs to be set
  54. * up if it has not been done already.
  55. */
  56. ret = gpio_request_by_name(dev, "power-enable-gpio", 0, &desc,
  57. GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
  58. if (ret)
  59. return ret;
  60. /* 8.14 Additional PCI Express Programming Steps, step #1 */
  61. bdf = PCI_BDF(0, 0x1c, 0);
  62. bus = pci_get_controller(dev);
  63. pci_bus_clrset_config32(bus, bdf, 0xf4, 0x60, 0);
  64. pci_bus_clrset_config32(bus, bdf, 0xf4, 0x80, 0x80);
  65. pci_bus_clrset_config32(bus, bdf, 0xe2, 0x30, 0x30);
  66. return 0;
  67. }
  68. static void pch_misc_init(struct udevice *dev)
  69. {
  70. /* Setup SLP signal assertion, SLP_S4=4s, SLP_S3=50ms */
  71. dm_pci_clrset_config8(dev, GEN_PMCON_3, 3 << 4 | 1 << 10,
  72. 1 << 3 | 1 << 11 | 1 << 12);
  73. /* Prepare sleep mode */
  74. clrsetio_32(ACPI_BASE_ADDRESS + PM1_CNT, SLP_TYP, SCI_EN);
  75. /* Setup NMI on errors, disable SERR */
  76. clrsetio_8(0x61, 0xf0, 1 << 2);
  77. /* Disable NMI sources */
  78. setio_8(0x70, 1 << 7);
  79. /* Indicate DRAM init done for MRC */
  80. dm_pci_clrset_config8(dev, GEN_PMCON_2, 0, 1 << 7);
  81. /* Clear status bits to prevent unexpected wake */
  82. setbits_le32(RCB_REG(0x3310), 0x0000002f);
  83. clrsetbits_le32(RCB_REG(0x3f02), 0x0000000f, 0);
  84. /* Enable PCIe Relaxed Order */
  85. setbits_le32(RCB_REG(0x2314), 1 << 31 | 1 << 7);
  86. setbits_le32(RCB_REG(0x1114), 1 << 15 | 1 << 14);
  87. /* Setup SERIRQ, enable continuous mode */
  88. dm_pci_clrset_config8(dev, SERIRQ_CNTL, 0, 1 << 7 | 1 << 6);
  89. };
  90. static void pch_enable_ioapic(void)
  91. {
  92. u32 reg32;
  93. /* Make sure this is a unique ID within system */
  94. io_apic_set_id(0x04);
  95. /* affirm full set of redirection table entries ("write once") */
  96. reg32 = io_apic_read(0x01);
  97. /* PCH-LP has 39 redirection entries */
  98. reg32 &= ~0x00ff0000;
  99. reg32 |= 0x00270000;
  100. io_apic_write(0x01, reg32);
  101. /*
  102. * Select Boot Configuration register (0x03) and
  103. * use Processor System Bus (0x01) to deliver interrupts.
  104. */
  105. io_apic_write(0x03, 0x01);
  106. }
  107. /* Enable all requested GPE */
  108. void enable_all_gpe(u32 set1, u32 set2, u32 set3, u32 set4)
  109. {
  110. outl(set1, ACPI_BASE_ADDRESS + GPE0_EN(GPE_31_0));
  111. outl(set2, ACPI_BASE_ADDRESS + GPE0_EN(GPE_63_32));
  112. outl(set3, ACPI_BASE_ADDRESS + GPE0_EN(GPE_94_64));
  113. outl(set4, ACPI_BASE_ADDRESS + GPE0_EN(GPE_STD));
  114. }
  115. /*
  116. * Enable GPIO SMI events - it would be good to put this in the GPIO driver
  117. * but it would need a new driver operation.
  118. */
  119. int enable_alt_smi(struct udevice *pch, u32 mask)
  120. {
  121. struct pch_lp_gpio_regs *regs;
  122. u32 gpiobase;
  123. int ret;
  124. ret = pch_get_gpio_base(pch, &gpiobase);
  125. if (ret) {
  126. debug("%s: invalid GPIOBASE address (%08x)\n", __func__,
  127. gpiobase);
  128. return -EINVAL;
  129. }
  130. regs = (struct pch_lp_gpio_regs *)gpiobase;
  131. setio_32(regs->alt_gpi_smi_en, mask);
  132. return 0;
  133. }
  134. static int pch_power_options(struct udevice *dev)
  135. {
  136. int pwr_on_after_power_fail = MAINBOARD_POWER_OFF;
  137. const char *state;
  138. u32 enable[4];
  139. u16 reg16;
  140. int ret;
  141. dm_pci_read_config16(dev, GEN_PMCON_3, &reg16);
  142. reg16 &= 0xfffe;
  143. switch (pwr_on_after_power_fail) {
  144. case MAINBOARD_POWER_OFF:
  145. reg16 |= 1;
  146. state = "off";
  147. break;
  148. case MAINBOARD_POWER_ON:
  149. reg16 &= ~1;
  150. state = "on";
  151. break;
  152. case MAINBOARD_POWER_KEEP:
  153. reg16 &= ~1;
  154. state = "state keep";
  155. break;
  156. default:
  157. state = "undefined";
  158. }
  159. dm_pci_write_config16(dev, GEN_PMCON_3, reg16);
  160. debug("Set power %s after power failure.\n", state);
  161. /* GPE setup based on device tree configuration */
  162. ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev),
  163. "intel,gpe0-en", enable, ARRAY_SIZE(enable));
  164. if (ret)
  165. return -EINVAL;
  166. enable_all_gpe(enable[0], enable[1], enable[2], enable[3]);
  167. /* SMI setup based on device tree configuration */
  168. enable_alt_smi(dev, fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
  169. "intel,alt-gp-smi-enable", 0));
  170. return 0;
  171. }
  172. /* Magic register settings for power management */
  173. static void pch_pm_init_magic(struct udevice *dev)
  174. {
  175. dm_pci_write_config8(dev, 0xa9, 0x46);
  176. clrbits_le32(RCB_REG(0x232c), 1),
  177. setbits_le32(RCB_REG(0x1100), 0x0000c13f);
  178. clrsetbits_le32(RCB_REG(0x2320), 0x60, 0x10);
  179. writel(0x00012fff, RCB_REG(0x3314));
  180. clrsetbits_le32(RCB_REG(0x3318), 0x000f0330, 0x0dcf0400);
  181. writel(0x04000000, RCB_REG(0x3324));
  182. writel(0x00041400, RCB_REG(0x3368));
  183. writel(0x3f8ddbff, RCB_REG(0x3388));
  184. writel(0x00007001, RCB_REG(0x33ac));
  185. writel(0x00181900, RCB_REG(0x33b0));
  186. writel(0x00060A00, RCB_REG(0x33c0));
  187. writel(0x06200840, RCB_REG(0x33d0));
  188. writel(0x01010101, RCB_REG(0x3a28));
  189. writel(0x040c0404, RCB_REG(0x3a2c));
  190. writel(0x9000000a, RCB_REG(0x3a9c));
  191. writel(0x03808033, RCB_REG(0x2b1c));
  192. writel(0x80000009, RCB_REG(0x2b34));
  193. writel(0x022ddfff, RCB_REG(0x3348));
  194. writel(0x00000001, RCB_REG(0x334c));
  195. writel(0x0001c000, RCB_REG(0x3358));
  196. writel(0x3f8ddbff, RCB_REG(0x3380));
  197. writel(0x0001c7e1, RCB_REG(0x3384));
  198. writel(0x0001c7e1, RCB_REG(0x338c));
  199. writel(0x0001c000, RCB_REG(0x3398));
  200. writel(0x00181900, RCB_REG(0x33a8));
  201. writel(0x00080000, RCB_REG(0x33dc));
  202. writel(0x00000001, RCB_REG(0x33e0));
  203. writel(0x0000040c, RCB_REG(0x3a20));
  204. writel(0x01010101, RCB_REG(0x3a24));
  205. writel(0x01010101, RCB_REG(0x3a30));
  206. dm_pci_clrset_config32(dev, 0xac, 0x00200000, 0);
  207. setbits_le32(RCB_REG(0x0410), 0x00000003);
  208. setbits_le32(RCB_REG(0x2618), 0x08000000);
  209. setbits_le32(RCB_REG(0x2300), 0x00000002);
  210. setbits_le32(RCB_REG(0x2600), 0x00000008);
  211. writel(0x00007001, RCB_REG(0x33b4));
  212. writel(0x022ddfff, RCB_REG(0x3350));
  213. writel(0x00000001, RCB_REG(0x3354));
  214. /* Power Optimizer */
  215. setbits_le32(RCB_REG(0x33d4), 0x08000000);
  216. /*
  217. * This stops the LCD from turning on:
  218. * setbits_le32(RCB_REG(0x33c8), 0x08000080);
  219. */
  220. writel(0x0000883c, RCB_REG(0x2b10));
  221. writel(0x1e0a4616, RCB_REG(0x2b14));
  222. writel(0x40000005, RCB_REG(0x2b24));
  223. writel(0x0005db01, RCB_REG(0x2b20));
  224. writel(0x05145005, RCB_REG(0x3a80));
  225. writel(0x00001005, RCB_REG(0x3a84));
  226. setbits_le32(RCB_REG(0x33d4), 0x2fff2fb1);
  227. setbits_le32(RCB_REG(0x33c8), 0x00008000);
  228. };
  229. static int pch_type(struct udevice *dev)
  230. {
  231. u16 type;
  232. dm_pci_read_config16(dev, PCI_DEVICE_ID, &type);
  233. return type;
  234. }
  235. /* Return 1 if PCH type is WildcatPoint */
  236. static int pch_is_wpt(struct udevice *dev)
  237. {
  238. return ((pch_type(dev) & 0xfff0) == 0x9cc0) ? 1 : 0;
  239. }
  240. /* Return 1 if PCH type is WildcatPoint ULX */
  241. static int pch_is_wpt_ulx(struct udevice *dev)
  242. {
  243. u16 lpcid = pch_type(dev);
  244. switch (lpcid) {
  245. case PCH_WPT_BDW_Y_SAMPLE:
  246. case PCH_WPT_BDW_Y_PREMIUM:
  247. case PCH_WPT_BDW_Y_BASE:
  248. return 1;
  249. }
  250. return 0;
  251. }
  252. static u32 pch_read_soft_strap(int id)
  253. {
  254. clrbits_le32(SPI_REG(SPIBAR_FDOC), 0x00007ffc);
  255. setbits_le32(SPI_REG(SPIBAR_FDOC), 0x00004000 | id * 4);
  256. return readl(SPI_REG(SPIBAR_FDOD));
  257. }
  258. static void pch_enable_mphy(struct udevice *dev)
  259. {
  260. u32 data_and = 0xffffffff;
  261. u32 data_or = (1 << 14) | (1 << 13) | (1 << 12);
  262. data_or |= (1 << 0);
  263. if (pch_is_wpt(dev)) {
  264. data_and &= ~((1 << 7) | (1 << 6) | (1 << 3));
  265. data_or |= (1 << 5) | (1 << 4);
  266. if (pch_is_wpt_ulx(dev)) {
  267. /* Check if SATA and USB3 MPHY are enabled */
  268. u32 strap19 = pch_read_soft_strap(19);
  269. strap19 &= ((1 << 31) | (1 << 30));
  270. strap19 >>= 30;
  271. if (strap19 == 3) {
  272. data_or |= (1 << 3);
  273. debug("Enable ULX MPHY PG control in single domain\n");
  274. } else if (strap19 == 0) {
  275. debug("Enable ULX MPHY PG control in split domains\n");
  276. } else {
  277. debug("Invalid PCH Soft Strap 19 configuration\n");
  278. }
  279. } else {
  280. data_or |= (1 << 3);
  281. }
  282. }
  283. pch_iobp_update(0xCF000000, data_and, data_or);
  284. }
  285. static void pch_init_deep_sx(bool deep_sx_enable_ac, bool deep_sx_enable_dc)
  286. {
  287. if (deep_sx_enable_ac) {
  288. setbits_le32(RCB_REG(DEEP_S3_POL), DEEP_S3_EN_AC);
  289. setbits_le32(RCB_REG(DEEP_S5_POL), DEEP_S5_EN_AC);
  290. }
  291. if (deep_sx_enable_dc) {
  292. setbits_le32(RCB_REG(DEEP_S3_POL), DEEP_S3_EN_DC);
  293. setbits_le32(RCB_REG(DEEP_S5_POL), DEEP_S5_EN_DC);
  294. }
  295. if (deep_sx_enable_ac || deep_sx_enable_dc) {
  296. setbits_le32(RCB_REG(DEEP_SX_CONFIG),
  297. DEEP_SX_WAKE_PIN_EN | DEEP_SX_GP27_PIN_EN);
  298. }
  299. }
  300. /* Power Management init */
  301. static void pch_pm_init(struct udevice *dev)
  302. {
  303. debug("PCH PM init\n");
  304. pch_init_deep_sx(false, false);
  305. pch_enable_mphy(dev);
  306. pch_pm_init_magic(dev);
  307. if (pch_is_wpt(dev)) {
  308. setbits_le32(RCB_REG(0x33e0), 1 << 4 | 1 << 1);
  309. setbits_le32(RCB_REG(0x2b1c), 1 << 22 | 1 << 14 | 1 << 13);
  310. writel(0x16bf0002, RCB_REG(0x33e4));
  311. setbits_le32(RCB_REG(0x33e4), 0x1);
  312. }
  313. pch_iobp_update(0xCA000000, ~0UL, 0x00000009);
  314. /* Set RCBA 0x2b1c[29]=1 if DSP disabled */
  315. if (readl(RCB_REG(FD)) & PCH_DISABLE_ADSPD)
  316. setbits_le32(RCB_REG(0x2b1c), 1 << 29);
  317. }
  318. static void pch_cg_init(struct udevice *dev)
  319. {
  320. struct udevice *bus = pci_get_controller(dev);
  321. u32 reg32;
  322. u16 reg16;
  323. ulong val;
  324. /* DMI */
  325. setbits_le32(RCB_REG(0x2234), 0xf);
  326. dm_pci_read_config16(dev, GEN_PMCON_1, &reg16);
  327. reg16 &= ~(1 << 10); /* Disable BIOS_PCI_EXP_EN for native PME */
  328. if (pch_is_wpt(dev))
  329. reg16 &= ~(1 << 11);
  330. else
  331. reg16 |= 1 << 11;
  332. reg16 |= 1 << 5 | 1 << 6 | 1 << 7 | 1 << 12;
  333. reg16 |= 1 << 2; /* PCI CLKRUN# Enable */
  334. dm_pci_write_config16(dev, GEN_PMCON_1, reg16);
  335. /*
  336. * RCBA + 0x2614[27:25,14:13,10,8] = 101,11,1,1
  337. * RCBA + 0x2614[23:16] = 0x20
  338. * RCBA + 0x2614[30:28] = 0x0
  339. * RCBA + 0x2614[26] = 1 (IF 0:2.0@0x08 >= 0x0b)
  340. */
  341. clrsetbits_le32(RCB_REG(0x2614), 0x64ff0000, 0x0a206500);
  342. /* Check for 0:2.0@0x08 >= 0x0b */
  343. pci_bus_read_config(bus, PCI_BDF(0, 0x2, 0), 0x8, &val, PCI_SIZE_8);
  344. if (pch_is_wpt(dev) || val >= 0x0b)
  345. setbits_le32(RCB_REG(0x2614), 1 << 26);
  346. setbits_le32(RCB_REG(0x900), 0x0000031f);
  347. reg32 = readl(RCB_REG(CG));
  348. if (readl(RCB_REG(0x3454)) & (1 << 4))
  349. reg32 &= ~(1 << 29); /* LPC Dynamic */
  350. else
  351. reg32 |= (1 << 29); /* LPC Dynamic */
  352. reg32 |= 1 << 31; /* LP LPC */
  353. reg32 |= 1 << 30; /* LP BLA */
  354. if (readl(RCB_REG(0x3454)) & (1 << 4))
  355. reg32 &= ~(1 << 29);
  356. else
  357. reg32 |= 1 << 29;
  358. reg32 |= 1 << 28; /* GPIO Dynamic */
  359. reg32 |= 1 << 27; /* HPET Dynamic */
  360. reg32 |= 1 << 26; /* Generic Platform Event Clock */
  361. if (readl(RCB_REG(BUC)) & PCH_DISABLE_GBE)
  362. reg32 |= 1 << 23; /* GbE Static */
  363. if (readl(RCB_REG(FD)) & PCH_DISABLE_HD_AUDIO)
  364. reg32 |= 1 << 21; /* HDA Static */
  365. reg32 |= 1 << 22; /* HDA Dynamic */
  366. writel(reg32, RCB_REG(CG));
  367. /* PCH-LP LPC */
  368. if (pch_is_wpt(dev))
  369. clrsetbits_le32(RCB_REG(0x3434), 0x1f, 0x17);
  370. else
  371. setbits_le32(RCB_REG(0x3434), 0x7);
  372. /* SPI */
  373. setbits_le32(RCB_REG(0x38c0), 0x3c07);
  374. pch_iobp_update(0xCE00C000, ~1UL, 0x00000000);
  375. }
  376. static void systemagent_init(void)
  377. {
  378. /* Enable Power Aware Interrupt Routing */
  379. clrsetbits_8(MCHBAR_REG(MCH_PAIR), 0x7, 0x4); /* Fixed Priority */
  380. /*
  381. * Set bits 0+1 of BIOS_RESET_CPL to indicate to the CPU
  382. * that BIOS has initialized memory and power management
  383. */
  384. setbits_8(MCHBAR_REG(BIOS_RESET_CPL), 3);
  385. debug("Set BIOS_RESET_CPL\n");
  386. /* Configure turbo power limits 1ms after reset complete bit */
  387. mdelay(1);
  388. cpu_set_power_limits(28);
  389. }
  390. static int broadwell_pch_init(struct udevice *dev)
  391. {
  392. int ret;
  393. /* Enable upper 128 bytes of CMOS */
  394. setbits_le32(RCB_REG(RC), 1 << 2);
  395. /*
  396. * TODO: TCO timer halt - this hangs
  397. * setio_16(ACPI_BASE_ADDRESS + TCO1_CNT, TCO_TMR_HLT);
  398. */
  399. /* Disable unused device (always) */
  400. setbits_le32(RCB_REG(FD), PCH_DISABLE_ALWAYS);
  401. pch_misc_init(dev);
  402. /* Interrupt configuration */
  403. pch_enable_ioapic();
  404. /* Initialize power management */
  405. ret = pch_power_options(dev);
  406. if (ret)
  407. return ret;
  408. pch_pm_init(dev);
  409. pch_cg_init(dev);
  410. systemagent_init();
  411. return 0;
  412. }
  413. static int broadwell_pch_probe(struct udevice *dev)
  414. {
  415. if (!(gd->flags & GD_FLG_RELOC))
  416. return broadwell_pch_early_init(dev);
  417. else
  418. return broadwell_pch_init(dev);
  419. }
  420. static int broadwell_pch_get_spi_base(struct udevice *dev, ulong *sbasep)
  421. {
  422. u32 rcba;
  423. dm_pci_read_config32(dev, PCH_RCBA, &rcba);
  424. /* Bits 31-14 are the base address, 13-1 are reserved, 0 is enable */
  425. rcba = rcba & 0xffffc000;
  426. *sbasep = rcba + 0x3800;
  427. return 0;
  428. }
  429. static int broadwell_set_spi_protect(struct udevice *dev, bool protect)
  430. {
  431. return lpc_set_spi_protect(dev, BIOS_CTRL, protect);
  432. }
  433. static int broadwell_get_gpio_base(struct udevice *dev, u32 *gbasep)
  434. {
  435. dm_pci_read_config32(dev, GPIO_BASE, gbasep);
  436. *gbasep &= PCI_BASE_ADDRESS_IO_MASK;
  437. return 0;
  438. }
  439. static const struct pch_ops broadwell_pch_ops = {
  440. .get_spi_base = broadwell_pch_get_spi_base,
  441. .set_spi_protect = broadwell_set_spi_protect,
  442. .get_gpio_base = broadwell_get_gpio_base,
  443. };
  444. static const struct udevice_id broadwell_pch_ids[] = {
  445. { .compatible = "intel,broadwell-pch" },
  446. { }
  447. };
  448. U_BOOT_DRIVER(broadwell_pch) = {
  449. .name = "broadwell_pch",
  450. .id = UCLASS_PCH,
  451. .of_match = broadwell_pch_ids,
  452. .probe = broadwell_pch_probe,
  453. .ops = &broadwell_pch_ops,
  454. };