blob: 78b43e44e96382e3128c7658b2cfa62cd31c2088 [file] [log] [blame]
Shaunak Saha9dffbdd2017-03-08 19:27:17 -08001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2017 Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <arch/io.h>
17#include <cbmem.h>
18#include <console/console.h>
19#include <halt.h>
20#include <intelblocks/pmclib.h>
21#include <intelblocks/gpio.h>
22#include <soc/pm.h>
23#include <timer.h>
24#include <vboot/vboot_common.h>
25
26static void print_num_status_bits(int num_bits, uint32_t status,
27 const char *const bit_names[])
28{
29 int i;
30
31 if (!status)
32 return;
33
34 for (i = num_bits - 1; i >= 0; i--) {
35 if (status & (1 << i)) {
36 if (bit_names[i])
37 printk(BIOS_DEBUG, "%s ", bit_names[i]);
38 else
39 printk(BIOS_DEBUG, "BIT%d ", i);
40 }
41 }
42}
43
44__attribute__ ((weak)) uint32_t soc_get_smi_status(uint32_t generic_sts)
45{
46 return generic_sts;
47}
48
49static uint32_t pmc_reset_smi_status(void)
50{
51 uint32_t smi_sts = inl(ACPI_BASE_ADDRESS + SMI_STS);
52 outl(smi_sts, ACPI_BASE_ADDRESS + SMI_STS);
53
54 return soc_get_smi_status(smi_sts);
55}
56
57static uint32_t print_smi_status(uint32_t smi_sts)
58{
59 size_t array_size;
60 const char *const *smi_arr;
61
62 if (!smi_sts)
63 return 0;
64
65 printk(BIOS_DEBUG, "SMI_STS: ");
66
67 smi_arr = soc_smi_sts_array(&array_size);
68
69 print_num_status_bits(array_size, smi_sts, smi_arr);
70 printk(BIOS_DEBUG, "\n");
71
72 return smi_sts;
73}
74
Shaunak Saha25cc76f2017-09-28 15:13:05 -070075/*
76 * Update supplied events in PM1_EN register. This does not disable any already
77 * set events.
78 */
79void pmc_update_pm1_enable(u16 events)
80{
81 u16 pm1_en = pmc_read_pm1_enable();
82 pm1_en |= events;
83 pmc_enable_pm1(pm1_en);
84}
85
86/* Read events set in PM1_EN register. */
87uint16_t pmc_read_pm1_enable(void)
88{
89 return inw(ACPI_BASE_ADDRESS + PM1_EN);
90}
91
Shaunak Saha9dffbdd2017-03-08 19:27:17 -080092uint32_t pmc_clear_smi_status(void)
93{
94 uint32_t sts = pmc_reset_smi_status();
95
96 return print_smi_status(sts);
97}
98
99uint32_t pmc_get_smi_en(void)
100{
101 return inl(ACPI_BASE_ADDRESS + SMI_EN);
102}
103
104void pmc_enable_smi(uint32_t mask)
105{
106 uint32_t smi_en = inl(ACPI_BASE_ADDRESS + SMI_EN);
107 smi_en |= mask;
108 outl(smi_en, ACPI_BASE_ADDRESS + SMI_EN);
109}
110
111void pmc_disable_smi(uint32_t mask)
112{
113 uint32_t smi_en = inl(ACPI_BASE_ADDRESS + SMI_EN);
114 smi_en &= ~mask;
115 outl(smi_en, ACPI_BASE_ADDRESS + SMI_EN);
116}
117
118/* PM1 */
119void pmc_enable_pm1(uint16_t events)
120{
121 outw(events, ACPI_BASE_ADDRESS + PM1_EN);
122}
123
124void pmc_enable_pm1_control(uint32_t mask)
125{
126 uint32_t pm1_cnt = inl(ACPI_BASE_ADDRESS + PM1_CNT);
127 pm1_cnt |= mask;
128 outl(pm1_cnt, ACPI_BASE_ADDRESS + PM1_CNT);
129}
130
131void pmc_disable_pm1_control(uint32_t mask)
132{
133 uint32_t pm1_cnt = inl(ACPI_BASE_ADDRESS + PM1_CNT);
134 pm1_cnt &= ~mask;
135 outl(pm1_cnt, ACPI_BASE_ADDRESS + PM1_CNT);
136}
137
138static uint16_t reset_pm1_status(void)
139{
140 uint16_t pm1_sts = inw(ACPI_BASE_ADDRESS + PM1_STS);
141 outw(pm1_sts, ACPI_BASE_ADDRESS + PM1_STS);
142 return pm1_sts;
143}
144
145static uint16_t print_pm1_status(uint16_t pm1_sts)
146{
147 static const char *const pm1_sts_bits[] = {
148 [0] = "TMROF",
149 [5] = "GBL",
150 [8] = "PWRBTN",
151 [10] = "RTC",
152 [11] = "PRBTNOR",
153 [13] = "USB",
154 [14] = "PCIEXPWAK",
155 [15] = "WAK",
156 };
157
158 if (!pm1_sts)
159 return 0;
160
161 printk(BIOS_SPEW, "PM1_STS: ");
162 print_num_status_bits(ARRAY_SIZE(pm1_sts_bits), pm1_sts, pm1_sts_bits);
163 printk(BIOS_SPEW, "\n");
164
165 return pm1_sts;
166}
167
168uint16_t pmc_clear_pm1_status(void)
169{
170 return print_pm1_status(reset_pm1_status());
171}
172
173/* TCO */
174
175static uint32_t print_tco_status(uint32_t tco_sts)
176{
177 size_t array_size;
178 const char *const *tco_arr;
179
180 if (!tco_sts)
181 return 0;
182
183 printk(BIOS_DEBUG, "TCO_STS: ");
184
185 tco_arr = soc_tco_sts_array(&array_size);
186
187 print_num_status_bits(array_size, tco_sts, tco_arr);
188 printk(BIOS_DEBUG, "\n");
189
190 return tco_sts;
191}
192
193uint32_t pmc_clear_tco_status(void)
194{
195 return print_tco_status(soc_reset_tco_status());
196}
197
198/* GPE */
199void pmc_enable_gpe(uint32_t mask)
200{
201 uint32_t gpe0a_en = inl(ACPI_BASE_ADDRESS + GPE0_EN(GPE_STD));
202 gpe0a_en |= mask;
203 outl(gpe0a_en, ACPI_BASE_ADDRESS + GPE0_EN(GPE_STD));
204}
205
206void pmc_disable_gpe(uint32_t mask)
207{
208 uint32_t gpe0a_en = inl(ACPI_BASE_ADDRESS + GPE0_EN(GPE_STD));
209 gpe0a_en &= ~mask;
210 outl(gpe0a_en, ACPI_BASE_ADDRESS + GPE0_EN(GPE_STD));
211}
212
213void pmc_disable_all_gpe(void)
214{
215 pmc_disable_gpe(~0);
216}
217
218/* Clear the gpio gpe0 status bits in ACPI registers */
219void pmc_clear_gpi_gpe_sts(void)
220{
221 int i;
222
223 for (i = 0; i < GPE0_REG_MAX; i++) {
224 /* This is reserved GPE block and specific to chipset */
225 if (i == GPE_STD)
226 continue;
227 uint32_t gpe_sts = inl(ACPI_BASE_ADDRESS + GPE0_STS(i));
228 outl(gpe_sts, ACPI_BASE_ADDRESS + GPE0_STS(i));
229 }
230}
231
232static uint32_t reset_gpe_status(void)
233{
234 uint32_t gpe_sts = inl(ACPI_BASE_ADDRESS + GPE0_STS(GPE_STD));
235 outl(gpe_sts, ACPI_BASE_ADDRESS + GPE0_STS(GPE_STD));
236 return gpe_sts;
237}
238
239static uint32_t print_gpe_sts(uint32_t gpe_sts)
240{
241 size_t array_size;
242 const char *const *sts_arr;
243
244 if (!gpe_sts)
245 return gpe_sts;
246
247 printk(BIOS_DEBUG, "GPE0a_STS: ");
248
249 sts_arr = soc_gpe_sts_array(&array_size);
250 print_num_status_bits(array_size, gpe_sts, sts_arr);
251 printk(BIOS_DEBUG, "\n");
252
253 return gpe_sts;
254}
255
256uint32_t pmc_clear_gpe_status(void)
257{
258 return print_gpe_sts(reset_gpe_status());
259}
260
261__attribute__ ((weak))
262void soc_clear_pm_registers(uintptr_t pmc_bar)
263{
264}
265
266void pmc_clear_status(void)
267{
268 uint32_t prsts;
269 uintptr_t pmc_bar;
270
271 /* Read PMC base address from soc */
272 pmc_bar = soc_read_pmc_base();
273
274 prsts = read32((void *)(pmc_bar + PRSTS));
275 write32((void *)(pmc_bar + PRSTS), prsts);
276
277 soc_clear_pm_registers(pmc_bar);
278}
279
280__attribute__ ((weak))
281int soc_prev_sleep_state(const struct chipset_power_state *ps,
282 int prev_sleep_state)
283{
284 return prev_sleep_state;
285}
286
287/*
288 * Returns prev_sleep_state and also prints all power management registers.
289 * Calls soc_prev_sleep_state which may be impelmented by SOC.
290 */
291static int pmc_prev_sleep_state(const struct chipset_power_state *ps)
292{
293 /* Default to S0. */
294 int prev_sleep_state = ACPI_S0;
295
296 if (ps->pm1_sts & WAK_STS) {
297 switch (acpi_sleep_from_pm1(ps->pm1_cnt)) {
298 case ACPI_S3:
299 if (IS_ENABLED(CONFIG_HAVE_ACPI_RESUME))
300 prev_sleep_state = ACPI_S3;
301 break;
302 case ACPI_S5:
303 prev_sleep_state = ACPI_S5;
304 break;
305 }
306
307 /* Clear SLP_TYP. */
308 outl(ps->pm1_cnt & ~(SLP_TYP), ACPI_BASE_ADDRESS + PM1_CNT);
309 }
310 return soc_prev_sleep_state(ps, prev_sleep_state);
311}
312
313/*
314 * This function re-writes the gpe0 register values in power state
315 * cbmem variable. After system wakes from sleep state internal PMC logic
316 * writes default values in GPE_CFG register which gives a wrong offset to
317 * calculate the wake reason. So we need to set it again to the routing
318 * table as per the devicetree.
319 */
320void pmc_fixup_power_state(void)
321{
322 int i;
323 struct chipset_power_state *ps;
324
325 ps = cbmem_find(CBMEM_ID_POWER_STATE);
326 if (ps == NULL)
327 return;
328
329 for (i = 0; i < GPE0_REG_MAX; i++) {
330 ps->gpe0_sts[i] = inl(ACPI_BASE_ADDRESS + GPE0_STS(i));
331 ps->gpe0_en[i] = inl(ACPI_BASE_ADDRESS + GPE0_EN(i));
332 printk(BIOS_DEBUG, "gpe0_sts[%d]: %08x gpe0_en[%d]: %08x\n",
333 i, ps->gpe0_sts[i], i, ps->gpe0_en[i]);
334 }
335}
336
337/* Reads and prints ACPI specific PM registers */
338int pmc_fill_power_state(struct chipset_power_state *ps)
339{
340 int i;
341
342 ps->pm1_sts = inw(ACPI_BASE_ADDRESS + PM1_STS);
343 ps->pm1_en = inw(ACPI_BASE_ADDRESS + PM1_EN);
344 ps->pm1_cnt = inl(ACPI_BASE_ADDRESS + PM1_CNT);
345
346 printk(BIOS_DEBUG, "pm1_sts: %04x pm1_en: %04x pm1_cnt: %08x\n",
347 ps->pm1_sts, ps->pm1_en, ps->pm1_cnt);
348
349 for (i = 0; i < GPE0_REG_MAX; i++) {
350 ps->gpe0_sts[i] = inl(ACPI_BASE_ADDRESS + GPE0_STS(i));
351 ps->gpe0_en[i] = inl(ACPI_BASE_ADDRESS + GPE0_EN(i));
352 printk(BIOS_DEBUG, "gpe0_sts[%d]: %08x gpe0_en[%d]: %08x\n",
353 i, ps->gpe0_sts[i], i, ps->gpe0_en[i]);
354 }
355
356 soc_fill_power_state(ps);
357
358 ps->prev_sleep_state = pmc_prev_sleep_state(ps);
359 printk(BIOS_DEBUG, "prev_sleep_state %d\n", ps->prev_sleep_state);
360
361 return ps->prev_sleep_state;
362}
363
364/*
365 * If possible, lock 0xcf9. Once the register is locked, it can't be changed.
366 * This lock is reset on cold boot, hard reset, soft reset and Sx.
367 */
368void pmc_global_reset_lock(void)
369{
370 /* Read PMC base address from soc */
371 uintptr_t etr = soc_read_pmc_base() + ETR;
372 uint32_t reg;
373
374 reg = read32((void *)etr);
375 if (reg & CF9_LOCK)
376 return;
377 reg |= CF9_LOCK;
378 write32((void *)etr, reg);
379}
380
381/*
382 * Enable or disable global reset. If global reset is enabled, hard reset and
383 * soft reset will trigger global reset, where both host and TXE are reset.
384 * This is cleared on cold boot, hard reset, soft reset and Sx.
385 */
386void pmc_global_reset_enable(bool enable)
387{
388 /* Read PMC base address from soc */
389 uintptr_t etr = soc_read_pmc_base() + ETR;
390 uint32_t reg;
391
392 reg = read32((void *)etr);
393 reg = enable ? reg | CF9_GLB_RST : reg & ~CF9_GLB_RST;
394 write32((void *)etr, reg);
395}
396
397int vboot_platform_is_resuming(void)
398{
399 if (!(inw(ACPI_BASE_ADDRESS + PM1_STS) & WAK_STS))
400 return 0;
401
402 return acpi_sleep_from_pm1(inl(ACPI_BASE_ADDRESS + PM1_CNT)) == ACPI_S3;
403}
404
405/* Read and clear GPE status (defined in arch/acpi.h) */
406int acpi_get_gpe(int gpe)
407{
408 int bank;
409 uint32_t mask, sts;
410 struct stopwatch sw;
411 int rc = 0;
412
413 if (gpe < 0 || gpe > GPE_MAX)
414 return -1;
415
416 bank = gpe / 32;
417 mask = 1 << (gpe % 32);
418
419 /* Wait up to 1ms for GPE status to clear */
420 stopwatch_init_msecs_expire(&sw, 1);
421 do {
422 if (stopwatch_expired(&sw))
423 return rc;
424
425 sts = inl(ACPI_BASE_ADDRESS + GPE0_STS(bank));
426 if (sts & mask) {
427 outl(mask, ACPI_BASE_ADDRESS + GPE0_STS(bank));
428 rc = 1;
429 }
430 } while (sts & mask);
431
432 return rc;
433}
434
435/*
436 * The PM1 control is set to S5 when vboot requests a reboot because the power
437 * state code above may not have collected its data yet. Therefore, set it to
438 * S5 when vboot requests a reboot. That's necessary if vboot fails in the
439 * resume path and requests a reboot. This prevents a reboot loop where the
440 * error is continually hit on the failing vboot resume path.
441 */
442void vboot_platform_prepare_reboot(void)
443{
444 const uint16_t port = ACPI_BASE_ADDRESS + PM1_CNT;
445 outl((inl(port) & ~(SLP_TYP)) | (SLP_TYP_S5 << SLP_TYP_SHIFT), port);
446}
447
448void poweroff(void)
449{
450 pmc_enable_pm1_control(SLP_EN | (SLP_TYP_S5 << SLP_TYP_SHIFT));
451
452 /*
453 * Setting SLP_TYP_S5 in PM1 triggers SLP_SMI, which is handled by SMM
454 * to transition to S5 state. If halt is called in SMM, then it prevents
455 * the SMI handler from being triggered and system never enters S5.
456 */
457 if (!ENV_SMM)
458 halt();
459}
460
461void pmc_gpe_init(void)
462{
463 uint32_t gpio_cfg = 0;
464 uint32_t gpio_cfg_reg;
465 uint8_t dw0, dw1, dw2;
466
467 /* Read PMC base address from soc. This is implemented in soc */
468 uintptr_t pmc_bar = soc_read_pmc_base();
469
470 /*
471 * Get the dwX values for pmc gpe settings.
472 */
473 soc_get_gpe_configs(&dw0, &dw1, &dw2);
474
475 const uint32_t gpio_cfg_mask =
476 (GPE0_DWX_MASK << GPE0_DW_SHIFT(0)) |
477 (GPE0_DWX_MASK << GPE0_DW_SHIFT(1)) |
478 (GPE0_DWX_MASK << GPE0_DW_SHIFT(2));
479
480 /* Making sure that bad values don't bleed into the other fields */
481 dw0 &= GPE0_DWX_MASK;
482 dw1 &= GPE0_DWX_MASK;
483 dw2 &= GPE0_DWX_MASK;
484
485 /*
486 * Route the GPIOs to the GPE0 block. Determine that all values
487 * are different, and if they aren't use the reset values.
488 */
489 if (dw0 == dw1 || dw1 == dw2) {
490 printk(BIOS_INFO, "PMC: Using default GPE route.\n");
491 gpio_cfg = read32((void *)pmc_bar + GPIO_GPE_CFG);
492
493 dw0 = (gpio_cfg >> GPE0_DW_SHIFT(0)) & GPE0_DWX_MASK;
494 dw1 = (gpio_cfg >> GPE0_DW_SHIFT(1)) & GPE0_DWX_MASK;
495 dw2 = (gpio_cfg >> GPE0_DW_SHIFT(2)) & GPE0_DWX_MASK;
496 } else {
497 gpio_cfg |= (uint32_t) dw0 << GPE0_DW_SHIFT(0);
498 gpio_cfg |= (uint32_t) dw1 << GPE0_DW_SHIFT(1);
499 gpio_cfg |= (uint32_t) dw2 << GPE0_DW_SHIFT(2);
500 }
501
502 gpio_cfg_reg = read32((void *)pmc_bar + GPIO_GPE_CFG) & ~gpio_cfg_mask;
503 gpio_cfg_reg |= gpio_cfg & gpio_cfg_mask;
504
505 write32((void *)pmc_bar + GPIO_GPE_CFG, gpio_cfg_reg);
506
507 /* Set the routes in the GPIO communities as well. */
508 gpio_route_gpe(dw0, dw1, dw2);
509}