xref: /aosp_15_r20/external/coreboot/src/soc/intel/common/block/pmc/pmclib.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <acpi/acpi_pm.h>
4 #include <arch/io.h>
5 #include <assert.h>
6 #include <bootmode.h>
7 #include <device/mmio.h>
8 #include <device/pci.h>
9 #include <cbmem.h>
10 #include <cpu/x86/smm.h>
11 #include <console/console.h>
12 #include <halt.h>
13 #include <intelblocks/pmc_ipc.h>
14 #include <intelblocks/pmclib.h>
15 #include <intelblocks/gpio.h>
16 #include <intelblocks/tco.h>
17 #include <option.h>
18 #include <security/vboot/vboot_common.h>
19 #include <soc/pci_devs.h>
20 #include <soc/pm.h>
21 #include <stdint.h>
22 #include <string.h>
23 #include <timer.h>
24 
25 #define PMC_IPC_BIOS_RST_COMPLETE		0xd0
26 #define PMC_IPC_BIOS_RST_SUBID_PCI_ENUM_DONE	0
27 #define PMC_IPC_BIOS_RST_CMPL_STS_PCI_ENUM	BIT(0)
28 
29 static struct chipset_power_state power_state;
30 
31 /* List of Minimum Assertion durations in microseconds */
32 enum min_assert_dur {
33 	MinAssertDur0s		= 0,
34 	MinAssertDur60us	= 60,
35 	MinAssertDur1ms		= 1000,
36 	MinAssertDur50ms	= 50000,
37 	MinAssertDur98ms	= 98000,
38 	MinAssertDur500ms	= 500000,
39 	MinAssertDur1s		= 1000000,
40 	MinAssertDur2s		= 2000000,
41 	MinAssertDur3s		= 3000000,
42 	MinAssertDur4s		= 4000000,
43 };
44 
45 /* Signal Assertion duration values */
46 struct cfg_assert_dur {
47 	/* Minimum assertion duration of SLP_A signal */
48 	enum min_assert_dur slp_a;
49 
50 	/* Minimum assertion duration of SLP_4 signal */
51 	enum min_assert_dur slp_s4;
52 
53 	/* Minimum assertion duration of SLP_3 signal */
54 	enum min_assert_dur slp_s3;
55 
56 	/* PCH PM Power Cycle duration */
57 	enum min_assert_dur pm_pwr_cyc_dur;
58 };
59 
60 /* Default value of PchPmPwrCycDur */
61 #define PCH_PM_PWR_CYC_DUR	0
62 
pmc_get_power_state(void)63 struct chipset_power_state *pmc_get_power_state(void)
64 {
65 	struct chipset_power_state *ptr = NULL;
66 
67 	if (ENV_HAS_CBMEM)
68 		ptr = acpi_get_pm_state();
69 
70 	/* cbmem is online but ptr is not populated yet */
71 	if (ptr == NULL && !(ENV_RAMSTAGE || ENV_POSTCAR))
72 		return &power_state;
73 
74 	return ptr;
75 }
76 
migrate_power_state(int is_recovery)77 static void migrate_power_state(int is_recovery)
78 {
79 	struct chipset_power_state *ps_cbmem;
80 
81 	ps_cbmem = cbmem_add(CBMEM_ID_POWER_STATE, sizeof(*ps_cbmem));
82 
83 	if (ps_cbmem == NULL) {
84 		printk(BIOS_DEBUG, "Not adding power state to cbmem!\n");
85 		return;
86 	}
87 	memcpy(ps_cbmem, &power_state, sizeof(*ps_cbmem));
88 }
89 CBMEM_CREATION_HOOK(migrate_power_state);
90 
print_num_status_bits(int num_bits,uint32_t status,const char * const bit_names[])91 static void print_num_status_bits(int num_bits, uint32_t status,
92 				  const char *const bit_names[])
93 {
94 	int i;
95 
96 	if (!status)
97 		return;
98 
99 	for (i = num_bits - 1; i >= 0; i--) {
100 		if (status & (1 << i)) {
101 			if (bit_names[i])
102 				printk(BIOS_DEBUG, "%s ", bit_names[i]);
103 			else
104 				printk(BIOS_DEBUG, "BIT%d ", i);
105 		}
106 	}
107 }
108 
soc_get_smi_status(uint32_t generic_sts)109 __weak uint32_t soc_get_smi_status(uint32_t generic_sts)
110 {
111 	return generic_sts;
112 }
113 
acpi_get_sleep_type(void)114 int acpi_get_sleep_type(void)
115 {
116 	struct chipset_power_state *ps;
117 	int prev_sleep_state = ACPI_S0;
118 
119 	ps = pmc_get_power_state();
120 	if (ps)
121 		prev_sleep_state = ps->prev_sleep_state;
122 
123 	return prev_sleep_state;
124 }
125 
pmc_reset_smi_status(void)126 static uint32_t pmc_reset_smi_status(void)
127 {
128 	uint32_t smi_sts = inl(ACPI_BASE_ADDRESS + SMI_STS);
129 	outl(smi_sts, ACPI_BASE_ADDRESS + SMI_STS);
130 
131 	return soc_get_smi_status(smi_sts);
132 }
133 
print_smi_status(uint32_t smi_sts)134 static uint32_t print_smi_status(uint32_t smi_sts)
135 {
136 	size_t array_size;
137 	const char *const *smi_arr;
138 
139 	if (!smi_sts)
140 		return 0;
141 
142 	printk(BIOS_DEBUG, "SMI_STS: ");
143 
144 	smi_arr = soc_smi_sts_array(&array_size);
145 
146 	print_num_status_bits(array_size, smi_sts, smi_arr);
147 	printk(BIOS_DEBUG, "\n");
148 
149 	return smi_sts;
150 }
151 
152 /*
153  * Update supplied events in PM1_EN register. This does not disable any already
154  * set events.
155  */
pmc_update_pm1_enable(u16 events)156 void pmc_update_pm1_enable(u16 events)
157 {
158 	u16 pm1_en = pmc_read_pm1_enable();
159 	pm1_en |= events;
160 	pmc_enable_pm1(pm1_en);
161 }
162 
163 /* Read events set in PM1_EN register. */
pmc_read_pm1_enable(void)164 uint16_t pmc_read_pm1_enable(void)
165 {
166 	return inw(ACPI_BASE_ADDRESS + PM1_EN);
167 }
168 
pmc_clear_smi_status(void)169 uint32_t pmc_clear_smi_status(void)
170 {
171 	uint32_t sts = pmc_reset_smi_status();
172 
173 	return print_smi_status(sts);
174 }
175 
pmc_get_smi_en(void)176 uint32_t pmc_get_smi_en(void)
177 {
178 	return inl(ACPI_BASE_ADDRESS + SMI_EN);
179 }
180 
pmc_enable_smi(uint32_t mask)181 void pmc_enable_smi(uint32_t mask)
182 {
183 	uint32_t smi_en = inl(ACPI_BASE_ADDRESS + SMI_EN);
184 	smi_en |= mask;
185 	outl(smi_en, ACPI_BASE_ADDRESS + SMI_EN);
186 }
187 
pmc_disable_smi(uint32_t mask)188 void pmc_disable_smi(uint32_t mask)
189 {
190 	uint32_t smi_en = inl(ACPI_BASE_ADDRESS + SMI_EN);
191 	smi_en &= ~mask;
192 	outl(smi_en, ACPI_BASE_ADDRESS + SMI_EN);
193 }
194 
195 /* PM1 */
pmc_enable_pm1(uint16_t events)196 void pmc_enable_pm1(uint16_t events)
197 {
198 	outw(events, ACPI_BASE_ADDRESS + PM1_EN);
199 }
200 
pmc_read_pm1_control(void)201 uint32_t pmc_read_pm1_control(void)
202 {
203 	return inl(ACPI_BASE_ADDRESS + PM1_CNT);
204 }
205 
pmc_write_pm1_control(uint32_t pm1_cnt)206 void pmc_write_pm1_control(uint32_t pm1_cnt)
207 {
208 	outl(pm1_cnt, ACPI_BASE_ADDRESS + PM1_CNT);
209 }
210 
pmc_enable_pm1_control(uint32_t mask)211 void pmc_enable_pm1_control(uint32_t mask)
212 {
213 	uint32_t pm1_cnt = pmc_read_pm1_control();
214 	pm1_cnt |= mask;
215 	pmc_write_pm1_control(pm1_cnt);
216 }
217 
pmc_disable_pm1_control(uint32_t mask)218 void pmc_disable_pm1_control(uint32_t mask)
219 {
220 	uint32_t pm1_cnt = pmc_read_pm1_control();
221 	pm1_cnt &= ~mask;
222 	pmc_write_pm1_control(pm1_cnt);
223 }
224 
reset_pm1_status(void)225 static uint16_t reset_pm1_status(void)
226 {
227 	uint16_t pm1_sts = inw(ACPI_BASE_ADDRESS + PM1_STS);
228 	outw(pm1_sts, ACPI_BASE_ADDRESS + PM1_STS);
229 	return pm1_sts;
230 }
231 
print_pm1_status(uint16_t pm1_sts)232 static uint16_t print_pm1_status(uint16_t pm1_sts)
233 {
234 	static const char *const pm1_sts_bits[] = {
235 		[0] = "TMROF",
236 		[5] = "GBL",
237 		[8] = "PWRBTN",
238 		[10] = "RTC",
239 		[11] = "PRBTNOR",
240 		[13] = "USB",
241 		[14] = "PCIEXPWAK",
242 		[15] = "WAK",
243 	};
244 
245 	if (!pm1_sts)
246 		return 0;
247 
248 	printk(BIOS_DEBUG, "PM1_STS: ");
249 	print_num_status_bits(ARRAY_SIZE(pm1_sts_bits), pm1_sts, pm1_sts_bits);
250 	printk(BIOS_DEBUG, "\n");
251 
252 	return pm1_sts;
253 }
254 
pmc_clear_pm1_status(void)255 uint16_t pmc_clear_pm1_status(void)
256 {
257 	return print_pm1_status(reset_pm1_status());
258 }
259 
260 /* TCO */
261 
print_tco_status(uint32_t tco_sts)262 static uint32_t print_tco_status(uint32_t tco_sts)
263 {
264 	size_t array_size;
265 	const char *const *tco_arr;
266 
267 	if (!tco_sts)
268 		return 0;
269 
270 	printk(BIOS_DEBUG, "TCO_STS: ");
271 
272 	tco_arr = soc_tco_sts_array(&array_size);
273 
274 	print_num_status_bits(array_size, tco_sts, tco_arr);
275 	printk(BIOS_DEBUG, "\n");
276 
277 	return tco_sts;
278 }
279 
pmc_clear_tco_status(void)280 uint32_t pmc_clear_tco_status(void)
281 {
282 	return print_tco_status(tco_reset_status());
283 }
284 
285 /* GPE */
pmc_enable_gpe(int gpe,uint32_t mask)286 static void pmc_enable_gpe(int gpe, uint32_t mask)
287 {
288 	uint32_t gpe0_en = inl(ACPI_BASE_ADDRESS + GPE0_EN(gpe));
289 	gpe0_en |= mask;
290 	outl(gpe0_en, ACPI_BASE_ADDRESS + GPE0_EN(gpe));
291 }
292 
pmc_disable_gpe(int gpe,uint32_t mask)293 static void pmc_disable_gpe(int gpe, uint32_t mask)
294 {
295 	uint32_t gpe0_en = inl(ACPI_BASE_ADDRESS + GPE0_EN(gpe));
296 	gpe0_en &= ~mask;
297 	outl(gpe0_en, ACPI_BASE_ADDRESS + GPE0_EN(gpe));
298 }
299 
pmc_enable_std_gpe(uint32_t mask)300 void pmc_enable_std_gpe(uint32_t mask)
301 {
302 	pmc_enable_gpe(GPE_STD, mask);
303 }
304 
pmc_disable_std_gpe(uint32_t mask)305 void pmc_disable_std_gpe(uint32_t mask)
306 {
307 	pmc_disable_gpe(GPE_STD, mask);
308 }
309 
pmc_disable_all_gpe(void)310 void pmc_disable_all_gpe(void)
311 {
312 	int i;
313 	for (i = 0; i < GPE0_REG_MAX; i++)
314 		pmc_disable_gpe(i, ~0);
315 }
316 
317 /* Clear the gpio gpe0 status bits in ACPI registers */
pmc_clear_gpi_gpe_status(void)318 static void pmc_clear_gpi_gpe_status(void)
319 {
320 	int i;
321 
322 	for (i = 0; i < GPE0_REG_MAX; i++) {
323 		/* This is reserved GPE block and specific to chipset */
324 		if (i == GPE_STD)
325 			continue;
326 		uint32_t gpe_sts = inl(ACPI_BASE_ADDRESS + GPE0_STS(i));
327 		outl(gpe_sts, ACPI_BASE_ADDRESS + GPE0_STS(i));
328 	}
329 }
330 
reset_std_gpe_status(void)331 static uint32_t reset_std_gpe_status(void)
332 {
333 	uint32_t gpe_sts = inl(ACPI_BASE_ADDRESS + GPE0_STS(GPE_STD));
334 	outl(gpe_sts, ACPI_BASE_ADDRESS + GPE0_STS(GPE_STD));
335 	return gpe_sts;
336 }
337 
print_std_gpe_sts(uint32_t gpe_sts)338 static uint32_t print_std_gpe_sts(uint32_t gpe_sts)
339 {
340 	size_t array_size;
341 	const char *const *sts_arr;
342 
343 	if (!gpe_sts)
344 		return gpe_sts;
345 
346 	printk(BIOS_DEBUG, "GPE0 STD STS: ");
347 
348 	sts_arr = soc_std_gpe_sts_array(&array_size);
349 	print_num_status_bits(array_size, gpe_sts, sts_arr);
350 	printk(BIOS_DEBUG, "\n");
351 
352 	return gpe_sts;
353 }
354 
pmc_clear_std_gpe_status(void)355 static void pmc_clear_std_gpe_status(void)
356 {
357 	print_std_gpe_sts(reset_std_gpe_status());
358 }
359 
pmc_clear_all_gpe_status(void)360 void pmc_clear_all_gpe_status(void)
361 {
362 	pmc_clear_std_gpe_status();
363 	pmc_clear_gpi_gpe_status();
364 }
365 
366 __weak
soc_clear_pm_registers(uintptr_t pmc_bar)367 void soc_clear_pm_registers(uintptr_t pmc_bar)
368 {
369 }
370 
pmc_or_mmio32(uint32_t offset,uint32_t ormask)371 void pmc_or_mmio32(uint32_t offset, uint32_t ormask)
372 {
373 	uint32_t reg;
374 	uintptr_t pmc_bar;
375 
376 	pmc_bar = soc_read_pmc_base();
377 	reg = read32p(pmc_bar + offset);
378 	reg |= ormask;
379 	write32p(pmc_bar + offset, reg);
380 }
381 
pmc_clear_prsts(void)382 void pmc_clear_prsts(void)
383 {
384 	uint32_t prsts;
385 	uintptr_t pmc_bar;
386 
387 	/* Read PMC base address from soc */
388 	pmc_bar = soc_read_pmc_base();
389 
390 	prsts = read32p(pmc_bar + PRSTS);
391 	write32p(pmc_bar + PRSTS, prsts);
392 
393 	soc_clear_pm_registers(pmc_bar);
394 }
395 
396 __weak
soc_prev_sleep_state(const struct chipset_power_state * ps,int prev_sleep_state)397 int soc_prev_sleep_state(const struct chipset_power_state *ps,
398 			      int prev_sleep_state)
399 {
400 	return prev_sleep_state;
401 }
402 
403 /*
404  * Returns prev_sleep_state and also prints all power management registers.
405  * Calls soc_prev_sleep_state which may be implemented by SOC.
406  */
pmc_prev_sleep_state(const struct chipset_power_state * ps)407 static int pmc_prev_sleep_state(const struct chipset_power_state *ps)
408 {
409 	/* Default to S0. */
410 	int prev_sleep_state = ACPI_S0;
411 
412 	if (ps->pm1_sts & WAK_STS) {
413 		switch (acpi_sleep_from_pm1(ps->pm1_cnt)) {
414 		case ACPI_S3:
415 			if (CONFIG(HAVE_ACPI_RESUME))
416 				prev_sleep_state = ACPI_S3;
417 			break;
418 		case ACPI_S4:
419 			prev_sleep_state = ACPI_S4;
420 			break;
421 		case ACPI_S5:
422 			prev_sleep_state = ACPI_S5;
423 			break;
424 		}
425 
426 		/* Clear SLP_TYP. */
427 		pmc_write_pm1_control(ps->pm1_cnt & ~(SLP_TYP));
428 	}
429 
430 	prev_sleep_state = soc_prev_sleep_state(ps, prev_sleep_state);
431 
432 	/* Clear PMC PMCON_x register power failure status bits. */
433 	pmc_clear_pmcon_pwr_failure_sts();
434 
435 	return prev_sleep_state;
436 }
437 
pmc_fill_pm_reg_info(struct chipset_power_state * ps)438 void pmc_fill_pm_reg_info(struct chipset_power_state *ps)
439 {
440 	int i;
441 
442 	memset(ps, 0, sizeof(*ps));
443 
444 	ps->pm1_sts = inw(ACPI_BASE_ADDRESS + PM1_STS);
445 	ps->pm1_en = inw(ACPI_BASE_ADDRESS + PM1_EN);
446 	ps->pm1_cnt = pmc_read_pm1_control();
447 
448 	printk(BIOS_DEBUG, "pm1_sts: %04x pm1_en: %04x pm1_cnt: %08x\n",
449 	       ps->pm1_sts, ps->pm1_en, ps->pm1_cnt);
450 
451 	for (i = 0; i < GPE0_REG_MAX; i++) {
452 		ps->gpe0_sts[i] = inl(ACPI_BASE_ADDRESS + GPE0_STS(i));
453 		ps->gpe0_en[i] = inl(ACPI_BASE_ADDRESS + GPE0_EN(i));
454 		printk(BIOS_DEBUG, "gpe0_sts[%d]: %08x gpe0_en[%d]: %08x\n",
455 		       i, ps->gpe0_sts[i], i, ps->gpe0_en[i]);
456 	}
457 
458 	soc_fill_power_state(ps);
459 }
460 
461 /* Reads and prints ACPI specific PM registers */
pmc_fill_power_state(struct chipset_power_state * ps)462 int pmc_fill_power_state(struct chipset_power_state *ps)
463 {
464 	/* Define the sleep state string */
465 	static const char * const acpi_sleep_states[] = {
466 		[ACPI_S0] = "S0",
467 		[ACPI_S1] = "S1",
468 		[ACPI_S3] = "S3",
469 		[ACPI_S4] = "S4",
470 		[ACPI_S5] = "S5",
471 	};
472 
473 	pmc_fill_pm_reg_info(ps);
474 
475 	ps->prev_sleep_state = pmc_prev_sleep_state(ps);
476 
477 	if (ps->prev_sleep_state < ARRAY_SIZE(acpi_sleep_states) &&
478 		acpi_sleep_states[ps->prev_sleep_state] != NULL)
479 		printk(BIOS_DEBUG, "prev_sleep_state %d (%s)\n", ps->prev_sleep_state,
480 			acpi_sleep_states[ps->prev_sleep_state]);
481 	else
482 		printk(BIOS_DEBUG, "prev_sleep_state %d (unknown state)\n", ps->prev_sleep_state);
483 
484 	return ps->prev_sleep_state;
485 }
486 
487 #if CONFIG(PMC_GLOBAL_RESET_ENABLE_LOCK)
pmc_global_reset_disable_and_lock(void)488 void pmc_global_reset_disable_and_lock(void)
489 {
490 	uint32_t *etr = soc_pmc_etr_addr();
491 	uint32_t reg;
492 
493 	reg = read32(etr);
494 	reg = (reg & ~CF9_GLB_RST) | CF9_LOCK;
495 	write32(etr, reg);
496 }
497 
pmc_global_reset_enable(bool enable)498 void pmc_global_reset_enable(bool enable)
499 {
500 	uint32_t *etr = soc_pmc_etr_addr();
501 	uint32_t reg;
502 
503 	reg = read32(etr);
504 	reg = enable ? reg | CF9_GLB_RST : reg & ~CF9_GLB_RST;
505 	write32(etr, reg);
506 }
507 #endif // CONFIG_PMC_GLOBAL_RESET_ENABLE_LOCK
508 
platform_is_resuming(void)509 int platform_is_resuming(void)
510 {
511 	/* Read power state from PMC data structure */
512 	if (ENV_RAMSTAGE)
513 		return acpi_get_sleep_type() == ACPI_S3;
514 
515 	/* Read power state from PMC ABASE */
516 	if (!(inw(ACPI_BASE_ADDRESS + PM1_STS) & WAK_STS))
517 		return 0;
518 
519 	return acpi_sleep_from_pm1(pmc_read_pm1_control()) == ACPI_S3;
520 }
521 
522 /* Read and clear GPE status (defined in acpi/acpi.h) */
acpi_get_gpe(int gpe)523 int acpi_get_gpe(int gpe)
524 {
525 	int bank;
526 	uint32_t mask, sts;
527 	struct stopwatch sw;
528 	int rc = 0;
529 
530 	if (gpe < 0 || gpe > GPE_MAX)
531 		return -1;
532 
533 	bank = gpe / 32;
534 	mask = 1 << (gpe % 32);
535 
536 	/* Wait up to 1ms for GPE status to clear */
537 	stopwatch_init_msecs_expire(&sw, 1);
538 	do {
539 		if (stopwatch_expired(&sw))
540 			return rc;
541 
542 		sts = inl(ACPI_BASE_ADDRESS + GPE0_STS(bank));
543 		if (sts & mask) {
544 			outl(mask, ACPI_BASE_ADDRESS + GPE0_STS(bank));
545 			rc = 1;
546 		}
547 	} while (sts & mask);
548 
549 	return rc;
550 }
551 
552 /*
553  * The PM1 control is set to S5 when vboot requests a reboot because the power
554  * state code above may not have collected its data yet. Therefore, set it to
555  * S5 when vboot requests a reboot. That's necessary if vboot fails in the
556  * resume path and requests a reboot. This prevents a reboot loop where the
557  * error is continually hit on the failing vboot resume path.
558  */
vboot_platform_prepare_reboot(void)559 void vboot_platform_prepare_reboot(void)
560 {
561 	uint32_t pm1_cnt;
562 	pm1_cnt = (pmc_read_pm1_control() & ~(SLP_TYP)) |
563 		(SLP_TYP_S5 << SLP_TYP_SHIFT);
564 	pmc_write_pm1_control(pm1_cnt);
565 }
566 
poweroff(void)567 void poweroff(void)
568 {
569 	pmc_enable_pm1_control(SLP_EN | (SLP_TYP_S5 << SLP_TYP_SHIFT));
570 
571 	/*
572 	 * Setting SLP_TYP_S5 in PM1 triggers SLP_SMI, which is handled by SMM
573 	 * to transition to S5 state. If halt is called in SMM, then it prevents
574 	 * the SMI handler from being triggered and system never enters S5.
575 	 */
576 	if (!ENV_SMM)
577 		halt();
578 }
579 
pmc_gpe_init(void)580 void pmc_gpe_init(void)
581 {
582 	uint32_t gpio_cfg = 0;
583 	uint32_t gpio_cfg_reg;
584 	uint8_t dw0 = 0, dw1 = 0, dw2 = 0;
585 
586 	/* Read PMC base address from soc. This is implemented in soc */
587 	uintptr_t pmc_bar = soc_read_pmc_base();
588 
589 	/*
590 	 * Get the dwX values for pmc gpe settings.
591 	 */
592 	soc_get_gpi_gpe_configs(&dw0, &dw1, &dw2);
593 
594 	const uint32_t gpio_cfg_mask =
595 	    (GPE0_DWX_MASK << GPE0_DW_SHIFT(0)) |
596 	    (GPE0_DWX_MASK << GPE0_DW_SHIFT(1)) |
597 	    (GPE0_DWX_MASK << GPE0_DW_SHIFT(2));
598 
599 	/* Making sure that bad values don't bleed into the other fields */
600 	dw0 &= GPE0_DWX_MASK;
601 	dw1 &= GPE0_DWX_MASK;
602 	dw2 &= GPE0_DWX_MASK;
603 
604 	/*
605 	 * Route the GPIOs to the GPE0 block. Determine that all values
606 	 * are different, and if they aren't use the reset values.
607 	 */
608 	if (dw0 == dw1 || dw1 == dw2) {
609 		printk(BIOS_INFO, "PMC: Using default GPE route.\n");
610 		gpio_cfg = read32p(pmc_bar + GPIO_GPE_CFG);
611 
612 		dw0 = (gpio_cfg >> GPE0_DW_SHIFT(0)) & GPE0_DWX_MASK;
613 		dw1 = (gpio_cfg >> GPE0_DW_SHIFT(1)) & GPE0_DWX_MASK;
614 		dw2 = (gpio_cfg >> GPE0_DW_SHIFT(2)) & GPE0_DWX_MASK;
615 	} else {
616 		gpio_cfg |= (uint32_t)dw0 << GPE0_DW_SHIFT(0);
617 		gpio_cfg |= (uint32_t)dw1 << GPE0_DW_SHIFT(1);
618 		gpio_cfg |= (uint32_t)dw2 << GPE0_DW_SHIFT(2);
619 	}
620 
621 	gpio_cfg_reg = read32p(pmc_bar + GPIO_GPE_CFG) & ~gpio_cfg_mask;
622 	gpio_cfg_reg |= gpio_cfg & gpio_cfg_mask;
623 
624 	write32p(pmc_bar + GPIO_GPE_CFG, gpio_cfg_reg);
625 
626 	/* Set the routes in the GPIO communities as well. */
627 	gpio_route_gpe(dw0, dw1, dw2);
628 }
629 
pmc_clear_pmcon_pwr_failure_sts_mmio(void)630 static void pmc_clear_pmcon_pwr_failure_sts_mmio(void)
631 {
632 	uint8_t *addr = pmc_mmio_regs();
633 
634 	/*
635 	 * Clear PMC GEN_PMCON_A register power failure status bits:
636 	 * SUS_PWR_FLR, PWR_FLR bits
637 	 * while retaining MS4V write-1-to-clear bit
638 	 *
639 	 * Note: clearing `GBL_RST_STS` bit earlier than FSP-M/MRC having an adverse effect
640 	 * on the PMC sleep type register which results in calculating wrong
641 	 * `prev_sleep_state` post a global reset, hence, just clearing the power failure
642 	 * status bits rather than clearing the complete PMC PMCON_A register.
643 	 */
644 	clrbits32((addr + GEN_PMCON_A), (MS4V | GBL_RST_STS));
645 }
646 
pmc_clear_pmcon_pwr_failure_sts_pci(void)647 static void pmc_clear_pmcon_pwr_failure_sts_pci(void)
648 {
649 #if defined(__SIMPLE_DEVICE__)
650 	pci_devfn_t dev = PCI_DEV(0, PCI_SLOT(PCH_DEVFN_PMC), PCI_FUNC(PCH_DEVFN_PMC));
651 #else
652 	struct device *dev = pcidev_path_on_root(PCH_DEVFN_PMC);
653 	if (!dev)
654 		return;
655 #endif
656 
657 	pci_or_config32(dev, GEN_PMCON_B, (SUS_PWR_FLR | PWR_FLR));
658 }
659 
660 /*
661  * Clear PMC GEN_PMCON_X register power failure status bits:
662  * SUS_PWR_FLR, PWR_FLR bits (keep the other bits intact)
663  */
pmc_clear_pmcon_pwr_failure_sts(void)664 void pmc_clear_pmcon_pwr_failure_sts(void)
665 {
666 	if (CONFIG(SOC_INTEL_MEM_MAPPED_PM_CONFIGURATION))
667 		pmc_clear_pmcon_pwr_failure_sts_mmio();
668 	else
669 		pmc_clear_pmcon_pwr_failure_sts_pci();
670 }
671 
672 #if ENV_RAMSTAGE
pmc_clear_pmcon_sts_mmio(void)673 static void pmc_clear_pmcon_sts_mmio(void)
674 {
675 	uint8_t *addr = pmc_mmio_regs();
676 
677 	clrbits32((addr + GEN_PMCON_A), MS4V);
678 }
679 
pmc_clear_pmcon_sts_pci(void)680 static void pmc_clear_pmcon_sts_pci(void)
681 {
682 	struct device *dev = pcidev_path_on_root(PCH_DEVFN_PMC);
683 	if (!dev)
684 		return;
685 
686 	pci_and_config32(dev, GEN_PMCON_A, ~MS4V);
687 }
688 
689 /*
690  * Clear PMC GEN_PMCON_A register status bits:
691  * SUS_PWR_FLR, GBL_RST_STS, HOST_RST_STS, PWR_FLR bits
692  * while retaining MS4V write-1-to-clear bit
693  */
pmc_clear_pmcon_sts(void)694 void pmc_clear_pmcon_sts(void)
695 {
696 	/*
697 	 * Accessing PMC GEN_PMCON_A register differs between different Intel chipsets.
698 	 * Typically, there are two possible ways to perform GEN_PMCON_A register programming
699 	 * (like `pmc_clear_pmcon_sts()`) as:
700 	 * 1. Using PCI configuration space when GEN_PMCON_A is a PCI configuration register.
701 	 * 2. Using MMIO access when GEN_PMCON_A is a memory mapped register.
702 	 */
703 	if (CONFIG(SOC_INTEL_MEM_MAPPED_PM_CONFIGURATION))
704 		pmc_clear_pmcon_sts_mmio();
705 	else
706 		pmc_clear_pmcon_sts_pci();
707 }
708 #endif
709 
pmc_set_power_failure_state(const bool target_on)710 void pmc_set_power_failure_state(const bool target_on)
711 {
712 	const unsigned int state = get_uint_option("power_on_after_fail",
713 					 CONFIG_MAINBOARD_POWER_FAILURE_STATE);
714 
715 	/*
716 	 * On the shutdown path (target_on == false), we only need to
717 	 * update the register for MAINBOARD_POWER_STATE_PREVIOUS. For
718 	 * all other cases, we don't write the register to avoid clob-
719 	 * bering the value set on the boot path. This is necessary,
720 	 * for instance, when we can't access the option backend in SMM.
721 	 */
722 
723 	switch (state) {
724 	case MAINBOARD_POWER_STATE_OFF:
725 		if (!target_on)
726 			break;
727 		printk(BIOS_INFO, "Set power off after power failure.\n");
728 		pmc_soc_set_afterg3_en(false);
729 		break;
730 	case MAINBOARD_POWER_STATE_ON:
731 		if (!target_on)
732 			break;
733 		printk(BIOS_INFO, "Set power on after power failure.\n");
734 		pmc_soc_set_afterg3_en(true);
735 		break;
736 	case MAINBOARD_POWER_STATE_PREVIOUS:
737 		printk(BIOS_INFO, "Keep power state after power failure.\n");
738 		pmc_soc_set_afterg3_en(target_on);
739 		break;
740 	default:
741 		printk(BIOS_WARNING, "Unknown power-failure state: %d\n", state);
742 		break;
743 	}
744 }
745 
746 /* This function returns the highest assertion duration of the SLP_Sx assertion widths */
get_high_assert_width(const struct cfg_assert_dur * cfg_assert_dur)747 static enum min_assert_dur get_high_assert_width(const struct cfg_assert_dur *cfg_assert_dur)
748 {
749 	enum min_assert_dur max_assert_dur = cfg_assert_dur->slp_s4;
750 
751 	if (max_assert_dur < cfg_assert_dur->slp_s3)
752 		max_assert_dur = cfg_assert_dur->slp_s3;
753 
754 	if (max_assert_dur < cfg_assert_dur->slp_a)
755 		max_assert_dur = cfg_assert_dur->slp_a;
756 
757 	return max_assert_dur;
758 }
759 
760 /* This function converts assertion durations from register-encoded to microseconds */
get_min_assert_dur(uint8_t slp_s4_min_assert,uint8_t slp_s3_min_assert,uint8_t slp_a_min_assert,uint8_t pm_pwr_cyc_dur,struct cfg_assert_dur * cfg_assert_dur)761 static void get_min_assert_dur(uint8_t slp_s4_min_assert, uint8_t slp_s3_min_assert,
762 		uint8_t slp_a_min_assert, uint8_t pm_pwr_cyc_dur,
763 		struct cfg_assert_dur *cfg_assert_dur)
764 {
765 	/*
766 	 * Ensure slp_x_dur_list[] elements in the devicetree config are in sync with
767 	 * FSP encoded values.
768 	 */
769 
770 	/* slp_s4_assert_dur_list : 1s, 1s(default), 2s, 3s, 4s */
771 	const enum min_assert_dur slp_s4_assert_dur_list[] = {
772 		MinAssertDur1s, MinAssertDur1s, MinAssertDur2s, MinAssertDur3s, MinAssertDur4s
773 	};
774 
775 	/* slp_s3_assert_dur_list: 50ms, 60us, 1ms, 50ms (Default), 2s */
776 	const enum min_assert_dur slp_s3_assert_dur_list[] = {
777 		MinAssertDur50ms, MinAssertDur60us, MinAssertDur1ms, MinAssertDur50ms,
778 										MinAssertDur2s
779 	};
780 
781 	/* slp_a_assert_dur_list: 2s, 0s, 4s, 98ms, 2s(Default) */
782 	const enum min_assert_dur slp_a_assert_dur_list[] = {
783 		MinAssertDur2s, MinAssertDur0s, MinAssertDur4s, MinAssertDur98ms, MinAssertDur2s
784 	};
785 
786 	/* pm_pwr_cyc_dur_list: 4s(Default), 1s, 2s, 3s, 4s */
787 	const enum min_assert_dur pm_pwr_cyc_dur_list[] = {
788 		MinAssertDur4s, MinAssertDur1s, MinAssertDur2s, MinAssertDur3s, MinAssertDur4s
789 	};
790 
791 	/* Get signal assertion width */
792 	if (slp_s4_min_assert < ARRAY_SIZE(slp_s4_assert_dur_list))
793 		cfg_assert_dur->slp_s4 = slp_s4_assert_dur_list[slp_s4_min_assert];
794 
795 	if (slp_s3_min_assert < ARRAY_SIZE(slp_s3_assert_dur_list))
796 		cfg_assert_dur->slp_s3 = slp_s3_assert_dur_list[slp_s3_min_assert];
797 
798 	if (slp_a_min_assert < ARRAY_SIZE(slp_a_assert_dur_list))
799 		cfg_assert_dur->slp_a = slp_a_assert_dur_list[slp_a_min_assert];
800 
801 	if (pm_pwr_cyc_dur < ARRAY_SIZE(pm_pwr_cyc_dur_list))
802 		cfg_assert_dur->pm_pwr_cyc_dur = pm_pwr_cyc_dur_list[pm_pwr_cyc_dur];
803 }
804 
805 /*
806  * This function ensures that the duration programmed in the PchPmPwrCycDur will never be
807  * smaller than the SLP_Sx assertion widths.
808  * If the pm_pwr_cyc_dur is less than any of the SLP_Sx assertion widths then it returns the
809  * default value PCH_PM_PWR_CYC_DUR.
810  */
get_pm_pwr_cyc_dur(uint8_t slp_s4_min_assert,uint8_t slp_s3_min_assert,uint8_t slp_a_min_assert,uint8_t pm_pwr_cyc_dur)811 uint8_t get_pm_pwr_cyc_dur(uint8_t slp_s4_min_assert, uint8_t slp_s3_min_assert,
812 		uint8_t slp_a_min_assert, uint8_t pm_pwr_cyc_dur)
813 {
814 	/* Set default values for the minimum assertion duration */
815 	struct cfg_assert_dur cfg_assert_dur = {
816 		.slp_a		= MinAssertDur2s,
817 		.slp_s4		= MinAssertDur1s,
818 		.slp_s3		= MinAssertDur50ms,
819 		.pm_pwr_cyc_dur	= MinAssertDur4s
820 	};
821 
822 	enum min_assert_dur high_assert_width;
823 
824 	/* Convert assertion durations from register-encoded to microseconds */
825 	get_min_assert_dur(slp_s4_min_assert, slp_s3_min_assert, slp_a_min_assert,
826 		pm_pwr_cyc_dur,	&cfg_assert_dur);
827 
828 	/* Get the highest assertion duration among PCH EDS specified signals for pwr_cyc_dur */
829 	high_assert_width = get_high_assert_width(&cfg_assert_dur);
830 
831 	if (cfg_assert_dur.pm_pwr_cyc_dur >= high_assert_width)
832 		return pm_pwr_cyc_dur;
833 
834 	printk(BIOS_DEBUG,
835 			"Set PmPwrCycDur to 4s as configured PmPwrCycDur (%d) violates PCH EDS "
836 			"spec\n", pm_pwr_cyc_dur);
837 
838 	return PCH_PM_PWR_CYC_DUR;
839 }
840 
pmc_set_acpi_mode(void)841 void pmc_set_acpi_mode(void)
842 {
843 	if (!CONFIG(NO_SMM) && !acpi_is_wakeup_s3()) {
844 		apm_control(APM_CNT_ACPI_DISABLE);
845 	}
846 }
847 
pmc_get_xtal_freq(void)848 enum pch_pmc_xtal pmc_get_xtal_freq(void)
849 {
850 	if (!CONFIG(SOC_INTEL_COMMON_BLOCK_PMC_EPOC))
851 		dead_code();
852 
853 	uint32_t xtal_freq = 0;
854 	const uint32_t epoc = read32p(soc_read_pmc_base() + PCH_PMC_EPOC);
855 
856 	/* XTAL frequency in bits 21, 20, 17 */
857 	xtal_freq |= !!(epoc & (1 << 21)) << 2;
858 	xtal_freq |= !!(epoc & (1 << 20)) << 1;
859 	xtal_freq |= !!(epoc & (1 << 17)) << 0;
860 	switch (xtal_freq) {
861 	case 0:
862 		return XTAL_24_MHZ;
863 	case 1:
864 		return XTAL_19_2_MHZ;
865 	case 2:
866 		return XTAL_38_4_MHZ;
867 	default:
868 		printk(BIOS_ERR, "Unknown EPOC XTAL frequency setting %u\n", xtal_freq);
869 		return XTAL_UNKNOWN_FREQ;
870 	}
871 }
872 
pmc_send_bios_reset_pci_enum_done(void)873 void pmc_send_bios_reset_pci_enum_done(void)
874 {
875 	struct pmc_ipc_buffer req = { 0 };
876 	struct pmc_ipc_buffer rsp;
877 	uint32_t cmd;
878 
879 	req.buf[0] = PMC_IPC_BIOS_RST_CMPL_STS_PCI_ENUM;
880 	cmd = pmc_make_ipc_cmd(PMC_IPC_BIOS_RST_COMPLETE,
881 			 PMC_IPC_BIOS_RST_SUBID_PCI_ENUM_DONE, 0);
882 	if (pmc_send_ipc_cmd(cmd, &req, &rsp) != CB_SUCCESS)
883 		printk(BIOS_ERR, "PMC: Failed sending PCI Enumeration Done Command\n");
884 }
885