pch.c 14 KB

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