lpc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * From coreboot southbridge/intel/bd82x6x/lpc.c
  4. *
  5. * Copyright (C) 2008-2009 coresystems GmbH
  6. */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <errno.h>
  10. #include <fdtdec.h>
  11. #include <rtc.h>
  12. #include <pci.h>
  13. #include <asm/intel_regs.h>
  14. #include <asm/interrupt.h>
  15. #include <asm/io.h>
  16. #include <asm/ioapic.h>
  17. #include <asm/lpc_common.h>
  18. #include <asm/pci.h>
  19. #include <asm/arch/pch.h>
  20. DECLARE_GLOBAL_DATA_PTR;
  21. #define NMI_OFF 0
  22. #define ENABLE_ACPI_MODE_IN_COREBOOT 0
  23. #define TEST_SMM_FLASH_LOCKDOWN 0
  24. static int pch_enable_apic(struct udevice *pch)
  25. {
  26. u32 reg32;
  27. int i;
  28. /* Enable ACPI I/O and power management. Set SCI IRQ to IRQ9 */
  29. dm_pci_write_config8(pch, ACPI_CNTL, 0x80);
  30. writel(0, IO_APIC_INDEX);
  31. writel(1 << 25, IO_APIC_DATA);
  32. /* affirm full set of redirection table entries ("write once") */
  33. writel(1, IO_APIC_INDEX);
  34. reg32 = readl(IO_APIC_DATA);
  35. writel(1, IO_APIC_INDEX);
  36. writel(reg32, IO_APIC_DATA);
  37. writel(0, IO_APIC_INDEX);
  38. reg32 = readl(IO_APIC_DATA);
  39. debug("PCH APIC ID = %x\n", (reg32 >> 24) & 0x0f);
  40. if (reg32 != (1 << 25)) {
  41. printf("APIC Error - cannot write to registers\n");
  42. return -EPERM;
  43. }
  44. debug("Dumping IOAPIC registers\n");
  45. for (i = 0; i < 3; i++) {
  46. writel(i, IO_APIC_INDEX);
  47. debug(" reg 0x%04x:", i);
  48. reg32 = readl(IO_APIC_DATA);
  49. debug(" 0x%08x\n", reg32);
  50. }
  51. /* Select Boot Configuration register. */
  52. writel(3, IO_APIC_INDEX);
  53. /* Use Processor System Bus to deliver interrupts. */
  54. writel(1, IO_APIC_DATA);
  55. return 0;
  56. }
  57. static void pch_enable_serial_irqs(struct udevice *pch)
  58. {
  59. u32 value;
  60. /* Set packet length and toggle silent mode bit for one frame. */
  61. value = (1 << 7) | (1 << 6) | ((21 - 17) << 2) | (0 << 0);
  62. #ifdef CONFIG_SERIRQ_CONTINUOUS_MODE
  63. dm_pci_write_config8(pch, SERIRQ_CNTL, value);
  64. #else
  65. dm_pci_write_config8(pch, SERIRQ_CNTL, value | (1 << 6));
  66. #endif
  67. }
  68. static int pch_pirq_init(struct udevice *pch)
  69. {
  70. uint8_t route[8], *ptr;
  71. if (fdtdec_get_byte_array(gd->fdt_blob, dev_of_offset(pch),
  72. "intel,pirq-routing", route, sizeof(route)))
  73. return -EINVAL;
  74. ptr = route;
  75. dm_pci_write_config8(pch, PIRQA_ROUT, *ptr++);
  76. dm_pci_write_config8(pch, PIRQB_ROUT, *ptr++);
  77. dm_pci_write_config8(pch, PIRQC_ROUT, *ptr++);
  78. dm_pci_write_config8(pch, PIRQD_ROUT, *ptr++);
  79. dm_pci_write_config8(pch, PIRQE_ROUT, *ptr++);
  80. dm_pci_write_config8(pch, PIRQF_ROUT, *ptr++);
  81. dm_pci_write_config8(pch, PIRQG_ROUT, *ptr++);
  82. dm_pci_write_config8(pch, PIRQH_ROUT, *ptr++);
  83. /*
  84. * TODO(sjg@chromium.org): U-Boot does not set up the interrupts
  85. * here. It's unclear if it is needed
  86. */
  87. return 0;
  88. }
  89. static int pch_gpi_routing(struct udevice *pch)
  90. {
  91. u8 route[16];
  92. u32 reg;
  93. int gpi;
  94. if (fdtdec_get_byte_array(gd->fdt_blob, dev_of_offset(pch),
  95. "intel,gpi-routing", route, sizeof(route)))
  96. return -EINVAL;
  97. for (reg = 0, gpi = 0; gpi < ARRAY_SIZE(route); gpi++)
  98. reg |= route[gpi] << (gpi * 2);
  99. dm_pci_write_config32(pch, 0xb8, reg);
  100. return 0;
  101. }
  102. static int pch_power_options(struct udevice *pch)
  103. {
  104. const void *blob = gd->fdt_blob;
  105. int node = dev_of_offset(pch);
  106. u8 reg8;
  107. u16 reg16, pmbase;
  108. u32 reg32;
  109. const char *state;
  110. int pwr_on;
  111. int nmi_option;
  112. int ret;
  113. /*
  114. * Which state do we want to goto after g3 (power restored)?
  115. * 0 == S0 Full On
  116. * 1 == S5 Soft Off
  117. *
  118. * If the option is not existent (Laptops), use Kconfig setting.
  119. * TODO(sjg@chromium.org): Make this configurable
  120. */
  121. pwr_on = MAINBOARD_POWER_ON;
  122. dm_pci_read_config16(pch, GEN_PMCON_3, &reg16);
  123. reg16 &= 0xfffe;
  124. switch (pwr_on) {
  125. case MAINBOARD_POWER_OFF:
  126. reg16 |= 1;
  127. state = "off";
  128. break;
  129. case MAINBOARD_POWER_ON:
  130. reg16 &= ~1;
  131. state = "on";
  132. break;
  133. case MAINBOARD_POWER_KEEP:
  134. reg16 &= ~1;
  135. state = "state keep";
  136. break;
  137. default:
  138. state = "undefined";
  139. }
  140. reg16 &= ~(3 << 4); /* SLP_S4# Assertion Stretch 4s */
  141. reg16 |= (1 << 3); /* SLP_S4# Assertion Stretch Enable */
  142. reg16 &= ~(1 << 10);
  143. reg16 |= (1 << 11); /* SLP_S3# Min Assertion Width 50ms */
  144. reg16 |= (1 << 12); /* Disable SLP stretch after SUS well */
  145. dm_pci_write_config16(pch, GEN_PMCON_3, reg16);
  146. debug("Set power %s after power failure.\n", state);
  147. /* Set up NMI on errors. */
  148. reg8 = inb(0x61);
  149. reg8 &= 0x0f; /* Higher Nibble must be 0 */
  150. reg8 &= ~(1 << 3); /* IOCHK# NMI Enable */
  151. reg8 |= (1 << 2); /* PCI SERR# Disable for now */
  152. outb(reg8, 0x61);
  153. reg8 = inb(0x70);
  154. /* TODO(sjg@chromium.org): Make this configurable */
  155. nmi_option = NMI_OFF;
  156. if (nmi_option) {
  157. debug("NMI sources enabled.\n");
  158. reg8 &= ~(1 << 7); /* Set NMI. */
  159. } else {
  160. debug("NMI sources disabled.\n");
  161. /* Can't mask NMI from PCI-E and NMI_NOW */
  162. reg8 |= (1 << 7);
  163. }
  164. outb(reg8, 0x70);
  165. /* Enable CPU_SLP# and Intel Speedstep, set SMI# rate down */
  166. dm_pci_read_config16(pch, GEN_PMCON_1, &reg16);
  167. reg16 &= ~(3 << 0); /* SMI# rate 1 minute */
  168. reg16 &= ~(1 << 10); /* Disable BIOS_PCI_EXP_EN for native PME */
  169. #if DEBUG_PERIODIC_SMIS
  170. /* Set DEBUG_PERIODIC_SMIS in pch.h to debug using periodic SMIs */
  171. reg16 |= (3 << 0); /* Periodic SMI every 8s */
  172. #endif
  173. dm_pci_write_config16(pch, GEN_PMCON_1, reg16);
  174. /* Set the board's GPI routing. */
  175. ret = pch_gpi_routing(pch);
  176. if (ret)
  177. return ret;
  178. dm_pci_read_config16(pch, 0x40, &pmbase);
  179. pmbase &= 0xfffe;
  180. writel(fdtdec_get_int(blob, node, "intel,gpe0-enable", 0),
  181. (ulong)pmbase + GPE0_EN);
  182. writew(fdtdec_get_int(blob, node, "intel,alt-gp-smi-enable", 0),
  183. (ulong)pmbase + ALT_GP_SMI_EN);
  184. /* Set up power management block and determine sleep mode */
  185. reg32 = inl(pmbase + 0x04); /* PM1_CNT */
  186. reg32 &= ~(7 << 10); /* SLP_TYP */
  187. reg32 |= (1 << 0); /* SCI_EN */
  188. outl(reg32, pmbase + 0x04);
  189. /* Clear magic status bits to prevent unexpected wake */
  190. setbits_le32(RCB_REG(0x3310), (1 << 4) | (1 << 5) | (1 << 0));
  191. clrbits_le32(RCB_REG(0x3f02), 0xf);
  192. return 0;
  193. }
  194. static void pch_rtc_init(struct udevice *pch)
  195. {
  196. int rtc_failed;
  197. u8 reg8;
  198. dm_pci_read_config8(pch, GEN_PMCON_3, &reg8);
  199. rtc_failed = reg8 & RTC_BATTERY_DEAD;
  200. if (rtc_failed) {
  201. reg8 &= ~RTC_BATTERY_DEAD;
  202. dm_pci_write_config8(pch, GEN_PMCON_3, reg8);
  203. }
  204. debug("rtc_failed = 0x%x\n", rtc_failed);
  205. /* TODO: Handle power failure */
  206. if (rtc_failed)
  207. printf("RTC power failed\n");
  208. }
  209. /* CougarPoint PCH Power Management init */
  210. static void cpt_pm_init(struct udevice *pch)
  211. {
  212. debug("CougarPoint PM init\n");
  213. dm_pci_write_config8(pch, 0xa9, 0x47);
  214. setbits_le32(RCB_REG(0x2238), (1 << 6) | (1 << 0));
  215. setbits_le32(RCB_REG(0x228c), 1 << 0);
  216. setbits_le32(RCB_REG(0x1100), (1 << 13) | (1 << 14));
  217. setbits_le32(RCB_REG(0x0900), 1 << 14);
  218. writel(0xc0388400, RCB_REG(0x2304));
  219. setbits_le32(RCB_REG(0x2314), (1 << 5) | (1 << 18));
  220. setbits_le32(RCB_REG(0x2320), (1 << 15) | (1 << 1));
  221. clrsetbits_le32(RCB_REG(0x3314), ~0x1f, 0xf);
  222. writel(0x050f0000, RCB_REG(0x3318));
  223. writel(0x04000000, RCB_REG(0x3324));
  224. setbits_le32(RCB_REG(0x3340), 0xfffff);
  225. setbits_le32(RCB_REG(0x3344), 1 << 1);
  226. writel(0x0001c000, RCB_REG(0x3360));
  227. writel(0x00061100, RCB_REG(0x3368));
  228. writel(0x7f8fdfff, RCB_REG(0x3378));
  229. writel(0x000003fc, RCB_REG(0x337c));
  230. writel(0x00001000, RCB_REG(0x3388));
  231. writel(0x0001c000, RCB_REG(0x3390));
  232. writel(0x00000800, RCB_REG(0x33a0));
  233. writel(0x00001000, RCB_REG(0x33b0));
  234. writel(0x00093900, RCB_REG(0x33c0));
  235. writel(0x24653002, RCB_REG(0x33cc));
  236. writel(0x062108fe, RCB_REG(0x33d0));
  237. clrsetbits_le32(RCB_REG(0x33d4), 0x0fff0fff, 0x00670060);
  238. writel(0x01010000, RCB_REG(0x3a28));
  239. writel(0x01010404, RCB_REG(0x3a2c));
  240. writel(0x01041041, RCB_REG(0x3a80));
  241. clrsetbits_le32(RCB_REG(0x3a84), 0x0000ffff, 0x00001001);
  242. setbits_le32(RCB_REG(0x3a84), 1 << 24); /* SATA 2/3 disabled */
  243. setbits_le32(RCB_REG(0x3a88), 1 << 0); /* SATA 4/5 disabled */
  244. writel(0x00000001, RCB_REG(0x3a6c));
  245. clrsetbits_le32(RCB_REG(0x2344), ~0x00ffff00, 0xff00000c);
  246. clrsetbits_le32(RCB_REG(0x80c), 0xff << 20, 0x11 << 20);
  247. writel(0, RCB_REG(0x33c8));
  248. setbits_le32(RCB_REG(0x21b0), 0xf);
  249. }
  250. /* PantherPoint PCH Power Management init */
  251. static void ppt_pm_init(struct udevice *pch)
  252. {
  253. debug("PantherPoint PM init\n");
  254. dm_pci_write_config8(pch, 0xa9, 0x47);
  255. setbits_le32(RCB_REG(0x2238), 1 << 0);
  256. setbits_le32(RCB_REG(0x228c), 1 << 0);
  257. setbits_le16(RCB_REG(0x1100), (1 << 13) | (1 << 14));
  258. setbits_le16(RCB_REG(0x0900), 1 << 14);
  259. writel(0xc03b8400, RCB_REG(0x2304));
  260. setbits_le32(RCB_REG(0x2314), (1 << 5) | (1 << 18));
  261. setbits_le32(RCB_REG(0x2320), (1 << 15) | (1 << 1));
  262. clrsetbits_le32(RCB_REG(0x3314), 0x1f, 0xf);
  263. writel(0x054f0000, RCB_REG(0x3318));
  264. writel(0x04000000, RCB_REG(0x3324));
  265. setbits_le32(RCB_REG(0x3340), 0xfffff);
  266. setbits_le32(RCB_REG(0x3344), (1 << 1) | (1 << 0));
  267. writel(0x0001c000, RCB_REG(0x3360));
  268. writel(0x00061100, RCB_REG(0x3368));
  269. writel(0x7f8fdfff, RCB_REG(0x3378));
  270. writel(0x000003fd, RCB_REG(0x337c));
  271. writel(0x00001000, RCB_REG(0x3388));
  272. writel(0x0001c000, RCB_REG(0x3390));
  273. writel(0x00000800, RCB_REG(0x33a0));
  274. writel(0x00001000, RCB_REG(0x33b0));
  275. writel(0x00093900, RCB_REG(0x33c0));
  276. writel(0x24653002, RCB_REG(0x33cc));
  277. writel(0x067388fe, RCB_REG(0x33d0));
  278. clrsetbits_le32(RCB_REG(0x33d4), 0x0fff0fff, 0x00670060);
  279. writel(0x01010000, RCB_REG(0x3a28));
  280. writel(0x01010404, RCB_REG(0x3a2c));
  281. writel(0x01040000, RCB_REG(0x3a80));
  282. clrsetbits_le32(RCB_REG(0x3a84), 0x0000ffff, 0x00001001);
  283. /* SATA 2/3 disabled */
  284. setbits_le32(RCB_REG(0x3a84), 1 << 24);
  285. /* SATA 4/5 disabled */
  286. setbits_le32(RCB_REG(0x3a88), 1 << 0);
  287. writel(0x00000001, RCB_REG(0x3a6c));
  288. clrsetbits_le32(RCB_REG(0x2344), 0xff0000ff, 0xff00000c);
  289. clrsetbits_le32(RCB_REG(0x80c), 0xff << 20, 0x11 << 20);
  290. setbits_le32(RCB_REG(0x33a4), (1 << 0));
  291. writel(0, RCB_REG(0x33c8));
  292. setbits_le32(RCB_REG(0x21b0), 0xf);
  293. }
  294. static void enable_hpet(void)
  295. {
  296. /* Move HPET to default address 0xfed00000 and enable it */
  297. clrsetbits_le32(RCB_REG(HPTC), 3 << 0, 1 << 7);
  298. }
  299. static void enable_clock_gating(struct udevice *pch)
  300. {
  301. u32 reg32;
  302. u16 reg16;
  303. setbits_le32(RCB_REG(0x2234), 0xf);
  304. dm_pci_read_config16(pch, GEN_PMCON_1, &reg16);
  305. reg16 |= (1 << 2) | (1 << 11);
  306. dm_pci_write_config16(pch, GEN_PMCON_1, reg16);
  307. pch_iobp_update(pch, 0xeb007f07, ~0U, 1 << 31);
  308. pch_iobp_update(pch, 0xeb004000, ~0U, 1 << 7);
  309. pch_iobp_update(pch, 0xec007f07, ~0U, 1 << 31);
  310. pch_iobp_update(pch, 0xec004000, ~0U, 1 << 7);
  311. reg32 = readl(RCB_REG(CG));
  312. reg32 |= (1 << 31);
  313. reg32 |= (1 << 29) | (1 << 28);
  314. reg32 |= (1 << 27) | (1 << 26) | (1 << 25) | (1 << 24);
  315. reg32 |= (1 << 16);
  316. reg32 |= (1 << 17);
  317. reg32 |= (1 << 18);
  318. reg32 |= (1 << 22);
  319. reg32 |= (1 << 23);
  320. reg32 &= ~(1 << 20);
  321. reg32 |= (1 << 19);
  322. reg32 |= (1 << 0);
  323. reg32 |= (0xf << 1);
  324. writel(reg32, RCB_REG(CG));
  325. setbits_le32(RCB_REG(0x38c0), 0x7);
  326. setbits_le32(RCB_REG(0x36d4), 0x6680c004);
  327. setbits_le32(RCB_REG(0x3564), 0x3);
  328. }
  329. static void pch_disable_smm_only_flashing(struct udevice *pch)
  330. {
  331. u8 reg8;
  332. debug("Enabling BIOS updates outside of SMM... ");
  333. dm_pci_read_config8(pch, 0xdc, &reg8); /* BIOS_CNTL */
  334. reg8 &= ~(1 << 5);
  335. dm_pci_write_config8(pch, 0xdc, reg8);
  336. }
  337. static void pch_fixups(struct udevice *pch)
  338. {
  339. u8 gen_pmcon_2;
  340. /* Indicate DRAM init done for MRC S3 to know it can resume */
  341. dm_pci_read_config8(pch, GEN_PMCON_2, &gen_pmcon_2);
  342. gen_pmcon_2 |= (1 << 7);
  343. dm_pci_write_config8(pch, GEN_PMCON_2, gen_pmcon_2);
  344. /* Enable DMI ASPM in the PCH */
  345. clrbits_le32(RCB_REG(0x2304), 1 << 10);
  346. setbits_le32(RCB_REG(0x21a4), (1 << 11) | (1 << 10));
  347. setbits_le32(RCB_REG(0x21a8), 0x3);
  348. }
  349. static void set_spi_speed(void)
  350. {
  351. u32 fdod;
  352. /* Observe SPI Descriptor Component Section 0 */
  353. writel(0x1000, RCB_REG(SPI_DESC_COMP0));
  354. /* Extract the1 Write/Erase SPI Frequency from descriptor */
  355. fdod = readl(RCB_REG(SPI_FREQ_WR_ERA));
  356. fdod >>= 24;
  357. fdod &= 7;
  358. /* Set Software Sequence frequency to match */
  359. clrsetbits_8(RCB_REG(SPI_FREQ_SWSEQ), 7, fdod);
  360. }
  361. static int lpc_init_extra(struct udevice *dev)
  362. {
  363. struct udevice *pch = dev->parent;
  364. debug("pch: lpc_init\n");
  365. dm_pci_write_bar32(pch, 0, 0);
  366. dm_pci_write_bar32(pch, 1, 0xff800000);
  367. dm_pci_write_bar32(pch, 2, 0xfec00000);
  368. dm_pci_write_bar32(pch, 3, 0x800);
  369. dm_pci_write_bar32(pch, 4, 0x900);
  370. /* Set the value for PCI command register. */
  371. dm_pci_write_config16(pch, PCI_COMMAND, 0x000f);
  372. /* IO APIC initialization. */
  373. pch_enable_apic(pch);
  374. pch_enable_serial_irqs(pch);
  375. /* Setup the PIRQ. */
  376. pch_pirq_init(pch);
  377. /* Setup power options. */
  378. pch_power_options(pch);
  379. /* Initialize power management */
  380. switch (pch_silicon_type(pch)) {
  381. case PCH_TYPE_CPT: /* CougarPoint */
  382. cpt_pm_init(pch);
  383. break;
  384. case PCH_TYPE_PPT: /* PantherPoint */
  385. ppt_pm_init(pch);
  386. break;
  387. default:
  388. printf("Unknown Chipset: %s\n", pch->name);
  389. return -ENOSYS;
  390. }
  391. /* Initialize the real time clock. */
  392. pch_rtc_init(pch);
  393. /* Initialize the High Precision Event Timers, if present. */
  394. enable_hpet();
  395. /* Initialize Clock Gating */
  396. enable_clock_gating(pch);
  397. pch_disable_smm_only_flashing(pch);
  398. pch_fixups(pch);
  399. return 0;
  400. }
  401. static int bd82x6x_lpc_early_init(struct udevice *dev)
  402. {
  403. set_spi_speed();
  404. /* Setting up Southbridge. In the northbridge code. */
  405. debug("Setting up static southbridge registers\n");
  406. dm_pci_write_config32(dev->parent, PCH_RCBA_BASE,
  407. RCB_BASE_ADDRESS | 1);
  408. dm_pci_write_config32(dev->parent, PMBASE, DEFAULT_PMBASE | 1);
  409. /* Enable ACPI BAR */
  410. dm_pci_write_config8(dev->parent, ACPI_CNTL, 0x80);
  411. debug("Disabling watchdog reboot\n");
  412. setbits_le32(RCB_REG(GCS), 1 >> 5); /* No reset */
  413. outw(1 << 11, DEFAULT_PMBASE | 0x60 | 0x08); /* halt timer */
  414. dm_pci_write_config32(dev->parent, GPIO_BASE, DEFAULT_GPIOBASE | 1);
  415. dm_pci_write_config32(dev->parent, GPIO_CNTL, 0x10);
  416. return 0;
  417. }
  418. static int bd82x6x_lpc_probe(struct udevice *dev)
  419. {
  420. int ret;
  421. if (!(gd->flags & GD_FLG_RELOC)) {
  422. ret = lpc_common_early_init(dev);
  423. if (ret) {
  424. debug("%s: lpc_early_init() failed\n", __func__);
  425. return ret;
  426. }
  427. return bd82x6x_lpc_early_init(dev);
  428. }
  429. return lpc_init_extra(dev);
  430. }
  431. static const struct udevice_id bd82x6x_lpc_ids[] = {
  432. { .compatible = "intel,bd82x6x-lpc" },
  433. { }
  434. };
  435. U_BOOT_DRIVER(bd82x6x_lpc_drv) = {
  436. .name = "lpc",
  437. .id = UCLASS_LPC,
  438. .of_match = bd82x6x_lpc_ids,
  439. .probe = bd82x6x_lpc_probe,
  440. };