blob: 39edc458863726030459477d76717840042555ca [file] [log] [blame]
Hannah Williams01bc8972016-02-04 20:13:34 -08001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2013 Google Inc.
5 * Copyright (C) 2015-2016 Intel Corp.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
Alexandru Gagniuca6339802016-04-05 12:40:24 -070018#define __SIMPLE_DEVICE__
19
Hannah Williams01bc8972016-02-04 20:13:34 -080020#include <arch/io.h>
21#include <console/console.h>
Shaunak Saha60b46182016-08-02 17:25:13 -070022#include <cbmem.h>
Hannah Williams01bc8972016-02-04 20:13:34 -080023#include <rules.h>
24#include <device/pci_def.h>
Aaron Durbinc2b77792016-07-14 00:26:50 -050025#include <halt.h>
Hannah Williams01bc8972016-02-04 20:13:34 -080026#include <soc/iomap.h>
Alexandru Gagniuca6339802016-04-05 12:40:24 -070027#include <soc/pci_devs.h>
Hannah Williams01bc8972016-02-04 20:13:34 -080028#include <soc/pm.h>
29#include <device/device.h>
30#include <device/pci.h>
Furquan Shaikh2a12e2e2016-07-25 11:48:03 -070031#include <vboot/vboot_common.h>
Hannah Williams01bc8972016-02-04 20:13:34 -080032
Alexandru Gagniuca6339802016-04-05 12:40:24 -070033static uintptr_t read_pmc_mmio_bar(void)
34{
35 uint32_t bar = pci_read_config32(PMC_DEV, PCI_BASE_ADDRESS_0);
36 return bar & ~PCI_BASE_ADDRESS_MEM_ATTR_MASK;
37}
Hannah Williams01bc8972016-02-04 20:13:34 -080038
Shaunak Saha9a0c9ac2016-06-27 23:00:15 -070039uintptr_t get_pmc_mmio_bar(void)
40{
41 return read_pmc_mmio_bar();
42}
43
Hannah Williams01bc8972016-02-04 20:13:34 -080044static void print_num_status_bits(int num_bits, uint32_t status,
45 const char * const bit_names[])
46{
47 int i;
48
49 if (!status)
50 return;
51
52 for (i = num_bits - 1; i >= 0; i--) {
53 if (status & (1 << i)) {
54 if (bit_names[i])
55 printk(BIOS_DEBUG, "%s ", bit_names[i]);
56 else
57 printk(BIOS_DEBUG, "BIT%d ", i);
58 }
59 }
60}
61
62static uint32_t print_smi_status(uint32_t smi_sts)
63{
64 static const char * const smi_sts_bits[] = {
Aaron Durbin7929dd02016-06-10 18:01:45 -050065 [BIOS_SMI_STS] = "BIOS",
66 [LEGACY_USB_SMI_STS] = "LEGACY USB",
67 [SLP_SMI_STS] = "SLP_SMI",
68 [APM_SMI_STS] = "APM",
69 [SWSMI_TMR_SMI_STS] = "SWSMI_TMR",
Aaron Durbina554b712016-06-10 18:04:21 -050070 [FAKE_PM1_SMI_STS] = "PM1",
Aaron Durbin7929dd02016-06-10 18:01:45 -050071 [GPIO_SMI_STS]= "GPIO_SMI",
72 [GPIO_UNLOCK_SMI_STS]= "GPIO_UNLOCK_SSMI",
73 [MC_SMI_STS] = "MCSMI",
74 [TCO_SMI_STS] = "TCO",
75 [PERIODIC_SMI_STS] = "PERIODIC",
76 [SERIRQ_SMI_STS] = "SERIRQ",
77 [SMBUS_SMI_STS] = "SMBUS_SMI",
78 [XHCI_SMI_STS] = "XHCI",
79 [HSMBUS_SMI_STS] = "HOST_SMBUS",
80 [SCS_SMI_STS] = "SCS",
81 [PCIE_SMI_STS] = "PCI_EXP_SMI",
82 [SCC2_SMI_STS] = "SCC2",
83 [SPI_SSMI_STS] = "SPI_SSMI",
84 [SPI_SMI_STS] = "SPI",
85 [PMC_OCP_SMI_STS] = "OCP_CSE",
Hannah Williams01bc8972016-02-04 20:13:34 -080086 };
87
88 if (!smi_sts)
89 return 0;
90
91 printk(BIOS_DEBUG, "SMI_STS: ");
92 print_num_status_bits(ARRAY_SIZE(smi_sts_bits), smi_sts, smi_sts_bits);
93 printk(BIOS_DEBUG, "\n");
94
95 return smi_sts;
96}
97
98static uint32_t reset_smi_status(void)
99{
100 uint32_t smi_sts = inl(ACPI_PMIO_BASE + SMI_STS);
101 outl(smi_sts, ACPI_PMIO_BASE + SMI_STS);
102 return smi_sts;
103}
104
105uint32_t clear_smi_status(void)
106{
Aaron Durbina554b712016-06-10 18:04:21 -0500107 uint32_t sts = reset_smi_status();
108
109 /*
110 * Check for power button status if nothing else is indicating an SMI
111 * and SMIs aren't turned into SCIs. Apparently, there is no PM1 status
112 * bit in the SMI status register. That makes things difficult for
113 * determining if the power button caused an SMI.
114 */
115 if (sts == 0 && !(inl(ACPI_PMIO_BASE + PM1_CNT) & SCI_EN)) {
116 uint16_t pm1_sts = inw(ACPI_PMIO_BASE + PM1_STS);
117
118 /* Fake PM1 status bit if power button pressed. */
119 if (pm1_sts & PWRBTN_STS)
120 sts |= (1 << FAKE_PM1_SMI_STS);
121 }
122
123 return print_smi_status(sts);
Hannah Williams01bc8972016-02-04 20:13:34 -0800124}
125
126uint32_t get_smi_en(void)
127{
128 return inl(ACPI_PMIO_BASE + SMI_EN);
129}
130
131void enable_smi(uint32_t mask)
132{
133 uint32_t smi_en = inl(ACPI_PMIO_BASE + SMI_EN);
134 smi_en |= mask;
135 outl(smi_en, ACPI_PMIO_BASE + SMI_EN);
136}
137
138void disable_smi(uint32_t mask)
139{
140 uint32_t smi_en = inl(ACPI_PMIO_BASE + SMI_EN);
141 smi_en &= ~mask;
142 outl(smi_en, ACPI_PMIO_BASE + SMI_EN);
143}
144
145void enable_pm1_control(uint32_t mask)
146{
147 uint32_t pm1_cnt = inl(ACPI_PMIO_BASE + PM1_CNT);
148 pm1_cnt |= mask;
149 outl(pm1_cnt, ACPI_PMIO_BASE + PM1_CNT);
150}
151
152void disable_pm1_control(uint32_t mask)
153{
154 uint32_t pm1_cnt = inl(ACPI_PMIO_BASE + PM1_CNT);
155 pm1_cnt &= ~mask;
156 outl(pm1_cnt, ACPI_PMIO_BASE + PM1_CNT);
157}
158
159static uint16_t reset_pm1_status(void)
160{
161 uint16_t pm1_sts = inw(ACPI_PMIO_BASE + PM1_STS);
162 outw(pm1_sts, ACPI_PMIO_BASE + PM1_STS);
163 return pm1_sts;
164}
165
166static uint16_t print_pm1_status(uint16_t pm1_sts)
167{
168 static const char * const pm1_sts_bits[] = {
169 [0] = "TMROF",
170 [5] = "GBL",
171 [8] = "PWRBTN",
172 [10] = "RTC",
173 [11] = "PRBTNOR",
174 [13] = "USB",
175 [14] = "PCIEXPWAK",
176 [15] = "WAK",
177 };
178
179 if (!pm1_sts)
180 return 0;
181
182 printk(BIOS_SPEW, "PM1_STS: ");
183 print_num_status_bits(ARRAY_SIZE(pm1_sts_bits), pm1_sts, pm1_sts_bits);
184 printk(BIOS_SPEW, "\n");
185
186 return pm1_sts;
187}
188
189uint16_t clear_pm1_status(void)
190{
191 return print_pm1_status(reset_pm1_status());
192}
193
194void enable_pm1(uint16_t events)
195{
196 outw(events, ACPI_PMIO_BASE + PM1_EN);
197}
198
199static uint32_t print_tco_status(uint32_t tco_sts)
200{
201 static const char * const tco_sts_bits[] = {
202 [3] = "TIMEOUT",
203 [17] = "SECOND_TO",
204 };
205
206 if (!tco_sts)
207 return 0;
208
209 printk(BIOS_DEBUG, "TCO_STS: ");
210 print_num_status_bits(ARRAY_SIZE(tco_sts_bits), tco_sts, tco_sts_bits);
211 printk(BIOS_DEBUG, "\n");
212
213 return tco_sts;
214}
215
216static uint32_t reset_tco_status(void)
217{
218 uint32_t tco_sts = inl(ACPI_PMIO_BASE + TCO_STS);
219 uint32_t tco_en = inl(ACPI_PMIO_BASE + TCO1_CNT);
220
221 outl(tco_sts, ACPI_PMIO_BASE + TCO_STS);
222 return tco_sts & tco_en;
223}
224
225uint32_t clear_tco_status(void)
226{
227 return print_tco_status(reset_tco_status());
228}
229
230void enable_gpe(uint32_t mask)
231{
232 uint32_t gpe0a_en = inl(ACPI_PMIO_BASE + GPE0_EN(0));
233 gpe0a_en |= mask;
234 outl(gpe0a_en, ACPI_PMIO_BASE + GPE0_EN(0));
235}
236
237void disable_gpe(uint32_t mask)
238{
239 uint32_t gpe0a_en = inl(ACPI_PMIO_BASE + GPE0_EN(0));
240 gpe0a_en &= ~mask;
241 outl(gpe0a_en, ACPI_PMIO_BASE + GPE0_EN(0));
242}
243
244void disable_all_gpe(void)
245{
246 disable_gpe(~0);
247}
248
Shaunak Sahad6bb5492016-08-22 21:55:23 -0700249/* Clear the gpio gpe0 status bits in ACPI registers */
250void clear_gpi_gpe_sts(void)
251{
252 int i;
253
254 for (i = 1; i < GPE0_REG_MAX; i++) {
255 uint32_t gpe_sts = inl(ACPI_PMIO_BASE + GPE0_STS(i));
256 outl(gpe_sts, ACPI_PMIO_BASE + GPE0_STS(i));
257 }
258}
Hannah Williams01bc8972016-02-04 20:13:34 -0800259
260static uint32_t reset_gpe_status(void)
261{
262 uint32_t gpe_sts = inl(ACPI_PMIO_BASE + GPE0_STS(0));
263 outl(gpe_sts, ACPI_PMIO_BASE + GPE0_STS(0));
264 return gpe_sts;
265}
266
267static uint32_t print_gpe_sts(uint32_t gpe_sts)
268{
269 static const char * const gpe_sts_bits[] = {
270 [0] = "PCIE_SCI",
271 [2] = "SWGPE",
272 [3] = "PCIE_WAKE0",
273 [4] = "PUNIT",
274 [6] = "PCIE_WAKE1",
275 [7] = "PCIE_WAKE2",
276 [8] = "PCIE_WAKE3",
277 [9] = "PCI_EXP",
278 [10] = "BATLOW",
279 [11] = "CSE_PME",
280 [12] = "XDCI_PME",
281 [13] = "XHCI_PME",
282 [14] = "AVS_PME",
283 [15] = "GPIO_TIER1_SCI",
284 [16] = "SMB_WAK",
285 [17] = "SATA_PME",
286 };
287
288 if (!gpe_sts)
289 return gpe_sts;
290
291 printk(BIOS_DEBUG, "GPE0a_STS: ");
292 print_num_status_bits(ARRAY_SIZE(gpe_sts_bits), gpe_sts, gpe_sts_bits);
293 printk(BIOS_DEBUG, "\n");
294
295 return gpe_sts;
296}
297
298uint32_t clear_gpe_status(void)
299{
300 return print_gpe_sts(reset_gpe_status());
301}
302
303void clear_pmc_status(void)
304{
305 uint32_t prsts;
306 uint32_t gen_pmcon1;
Alexandru Gagniuca6339802016-04-05 12:40:24 -0700307 uintptr_t pmc_bar0 = read_pmc_mmio_bar();
Hannah Williams01bc8972016-02-04 20:13:34 -0800308
Alexandru Gagniuca6339802016-04-05 12:40:24 -0700309 prsts = read32((void *)(pmc_bar0 + PRSTS));
310 gen_pmcon1 = read32((void *)(pmc_bar0 + GEN_PMCON1));
Hannah Williams01bc8972016-02-04 20:13:34 -0800311
312 /* Clear the status bits. The RPS field is cleared on a 0 write. */
Alexandru Gagniuca6339802016-04-05 12:40:24 -0700313 write32((void *)(pmc_bar0 + GEN_PMCON1), gen_pmcon1 & ~RPS);
314 write32((void *)(pmc_bar0 + PRSTS), prsts);
Hannah Williams01bc8972016-02-04 20:13:34 -0800315}
316
317
318/* Return 0, 3, or 5 to indicate the previous sleep state. */
319int chipset_prev_sleep_state(struct chipset_power_state *ps)
320{
321 /* Default to S0. */
Aaron Durbined35b7c2016-07-13 23:17:38 -0500322 int prev_sleep_state = ACPI_S0;
Hannah Williams01bc8972016-02-04 20:13:34 -0800323
324 if (ps->pm1_sts & WAK_STS) {
Aaron Durbined35b7c2016-07-13 23:17:38 -0500325 switch (acpi_sleep_from_pm1(ps->pm1_cnt)) {
326 case ACPI_S3:
327 if (IS_ENABLED(CONFIG_HAVE_ACPI_RESUME))
328 prev_sleep_state = ACPI_S3;
Hannah Williams01bc8972016-02-04 20:13:34 -0800329 break;
Aaron Durbined35b7c2016-07-13 23:17:38 -0500330 case ACPI_S5:
331 prev_sleep_state = ACPI_S5;
Hannah Williams01bc8972016-02-04 20:13:34 -0800332 break;
333 }
Hannah Williams5992afa2016-06-23 09:50:28 -0700334
335 /* Clear SLP_TYP. */
336 outl(ps->pm1_cnt & ~(SLP_TYP), ACPI_PMIO_BASE + PM1_CNT);
Hannah Williams01bc8972016-02-04 20:13:34 -0800337 }
338 return prev_sleep_state;
339}
340
Shaunak Saha60b46182016-08-02 17:25:13 -0700341/*
342 * This function re-writes the gpe0 register values in power state
343 * cbmem variable. After system wakes from sleep state internal PMC logic
344 * writes default values in GPE_CFG register which gives a wrong offset to
345 * calculate the wake reason. So we need to set it again to the routing
346 * table as per the devicetree.
347 */
348void fixup_power_state(void)
349{
350 int i;
351 struct chipset_power_state *ps;
352
353 ps = cbmem_find(CBMEM_ID_POWER_STATE);
354 if (ps == NULL)
355 return;
356
357 for (i = 0; i < GPE0_REG_MAX; i++) {
358 ps->gpe0_sts[i] = inl(ACPI_PMIO_BASE + GPE0_STS(i));
359 ps->gpe0_en[i] = inl(ACPI_PMIO_BASE + GPE0_EN(i));
360 printk(BIOS_DEBUG, "gpe0_sts[%d]: %08x gpe0_en[%d]: %08x\n",
361 i, ps->gpe0_sts[i], i, ps->gpe0_en[i]);
362 }
363}
364
Hannah Williams01bc8972016-02-04 20:13:34 -0800365/* returns prev_sleep_state */
366int fill_power_state(struct chipset_power_state *ps)
367{
368 int i;
Alexandru Gagniuca6339802016-04-05 12:40:24 -0700369 uintptr_t pmc_bar0 = read_pmc_mmio_bar();
370
Hannah Williams01bc8972016-02-04 20:13:34 -0800371 ps->pm1_sts = inw(ACPI_PMIO_BASE + PM1_STS);
372 ps->pm1_en = inw(ACPI_PMIO_BASE + PM1_EN);
373 ps->pm1_cnt = inl(ACPI_PMIO_BASE + PM1_CNT);
374 ps->tco_sts = inl(ACPI_PMIO_BASE + TCO_STS);
Alexandru Gagniuca6339802016-04-05 12:40:24 -0700375 ps->prsts = read32((void *)(pmc_bar0 + PRSTS));
376 ps->gen_pmcon1 =read32((void *)(pmc_bar0 + GEN_PMCON1));
377 ps->gen_pmcon2 = read32((void *)(pmc_bar0 + GEN_PMCON2));
378 ps->gen_pmcon3 = read32((void *)(pmc_bar0 + GEN_PMCON3));
Hannah Williams01bc8972016-02-04 20:13:34 -0800379
380 ps->prev_sleep_state = chipset_prev_sleep_state(ps);
381
382 printk(BIOS_DEBUG, "pm1_sts: %04x pm1_en: %04x pm1_cnt: %08x\n",
383 ps->pm1_sts, ps->pm1_en, ps->pm1_cnt);
384 printk(BIOS_DEBUG, "prsts: %08x tco_sts: %08x\n",
385 ps->prsts, ps->tco_sts);
386 printk(BIOS_DEBUG,
387 "gen_pmcon1: %08x gen_pmcon2: %08x gen_pmcon3: %08x\n",
388 ps->gen_pmcon1, ps->gen_pmcon2, ps->gen_pmcon3);
389 printk(BIOS_DEBUG, "smi_en: %08x smi_sts: %08x\n",
390 inl(ACPI_PMIO_BASE + SMI_EN), inl(ACPI_PMIO_BASE + SMI_STS));
391 for (i=0; i < GPE0_REG_MAX; i++) {
392 ps->gpe0_sts[i] = inl(ACPI_PMIO_BASE + GPE0_STS(i));
393 ps->gpe0_en[i] = inl(ACPI_PMIO_BASE + GPE0_EN(i));
394 printk(BIOS_DEBUG, "gpe0_sts[%d]: %08x gpe0_en[%d]: %08x\n",
395 i, ps->gpe0_sts[i], i, ps->gpe0_en[i]);
396 }
397 printk(BIOS_DEBUG, "prev_sleep_state %d\n", ps->prev_sleep_state);
398 return ps->prev_sleep_state;
399}
Aaron Durbinbef75e72016-05-26 11:00:44 -0500400
401int vboot_platform_is_resuming(void)
402{
Aaron Durbinbef75e72016-05-26 11:00:44 -0500403 if (!(inw(ACPI_PMIO_BASE + PM1_STS) & WAK_STS))
404 return 0;
405
Aaron Durbined35b7c2016-07-13 23:17:38 -0500406 return acpi_sleep_from_pm1(inl(ACPI_PMIO_BASE + PM1_CNT)) == ACPI_S3;
Aaron Durbinbef75e72016-05-26 11:00:44 -0500407}
Andrey Petrov0f593c22016-06-17 15:30:13 -0700408
409/*
410 * If possible, lock 0xcf9. Once the register is locked, it can't be changed.
411 * This lock is reset on cold boot, hard reset, soft reset and Sx.
412 */
413void global_reset_lock(void)
414{
415 uintptr_t etr = read_pmc_mmio_bar() + ETR;
416 uint32_t reg;
417
418 reg = read32((void *)etr);
419 if (reg & CF9_LOCK)
420 return;
421 reg |= CF9_LOCK;
422 write32((void *)etr, reg);
423}
424
425/*
426 * Enable or disable global reset. If global reset is enabled, hard reset and
427 * soft reset will trigger global reset, where both host and TXE are reset.
428 * This is cleared on cold boot, hard reset, soft reset and Sx.
429 */
430void global_reset_enable(bool enable)
431{
432 uintptr_t etr = read_pmc_mmio_bar() + ETR;
433 uint32_t reg;
434
435 reg = read32((void *)etr);
436 reg = enable ? reg | CF9_GLB_RST : reg & ~CF9_GLB_RST;
437 write32((void *)etr, reg);
438}
Furquan Shaikh4c1cb422016-06-23 14:00:05 -0700439
440/*
441 * The PM1 control is set to S5 when vboot requests a reboot because the power
442 * state code above may not have collected its data yet. Therefore, set it to
443 * S5 when vboot requests a reboot. That's necessary if vboot fails in the
444 * resume path and requests a reboot. This prevents a reboot loop where the
445 * error is continually hit on the failing vboot resume path.
446 */
447void vboot_platform_prepare_reboot(void)
448{
449 const uint16_t port = ACPI_PMIO_BASE + PM1_CNT;
450 outl((inl(port) & ~(SLP_TYP)) | (SLP_TYP_S5 << SLP_TYP_SHIFT), port);
451}
Aaron Durbinc2b77792016-07-14 00:26:50 -0500452
453void poweroff(void)
454{
455 enable_pm1_control(SLP_EN | (SLP_TYP_S5 << SLP_TYP_SHIFT));
Furquan Shaikh3828e552016-08-18 21:31:50 -0700456
457 /*
458 * Setting SLP_TYP_S5 in PM1 triggers SLP_SMI, which is handled by SMM
459 * to transition to S5 state. If halt is called in SMM, then it prevents
460 * the SMI handler from being triggered and system never enters S5.
461 */
462 if (!ENV_SMM)
463 halt();
Aaron Durbinc2b77792016-07-14 00:26:50 -0500464}