lpc.c 14 KB

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