-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathpower_sequence.c
667 lines (532 loc) · 15.5 KB
/
power_sequence.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/* Copyright 2013 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/* X86 chipset power control module for Chrome EC */
#include "battery.h"
#include "board.h"
#include "charge_state.h"
#include "chipset.h"
#include "common.h"
#include "console.h"
#include "extpower.h"
#include "i2c.h"
#include "lb_common.h"
#include "lpc.h"
#include "gpio.h"
#include "hooks.h"
#include "host_command_customization.h"
#include "lid_switch.h"
#include "power.h"
#include "power_button.h"
#include "registers.h"
#include "system.h"
#include "task.h"
#include "timer.h"
#include "util.h"
#include "wireless.h"
#include "driver/temp_sensor/f75303.h"
#include "diagnostics.h"
#include "cypress5525.h"
/* Console output macros */
#define CPUTS(outstr) cputs(CC_CHIPSET, outstr)
#define CPRINTS(format, args...) cprints(CC_CHIPSET, format, ## args)
static int forcing_shutdown; /* Forced shutdown in progress? */
static int custom_forcing_shutdown;
/*
* define wake source for keep PCH power
* BIT0 for RTCwake
* BIT1 for USBwake
*/
#define RTCWAKE BIT(0)
#define USBWAKE BIT(1)
static bool want_boot_ap_at_g3;
static int ap_boot_delay = 9; /* set 9 second for global reset wait time */
static int me_change;
static int power_s5_up;
static int s5_exit_tries;
static int stress_test_enable;
static void chipset_force_g3(void);
static int bb_retimer_init_pending = true;
void pending_retimer_init(int pending) {
bb_retimer_init_pending = pending;
}
void chipset_force_shutdown(enum chipset_shutdown_reason reason)
{
CPRINTS("%s(%d)", __func__, reason);
/*
* Force off. Sending a reset command to the PMIC will power off
* the EC, so simulate a long power button press instead. This
* condition will reset once the state machine transitions to G3.
* Consider reducing the latency here by changing the power off
* hold time on the PMIC.
*/
if (!chipset_in_state(CHIPSET_STATE_ANY_OFF)) {
report_ap_reset(reason);
forcing_shutdown = 1;
custom_forcing_shutdown = 1;
chipset_force_g3();
/* we need to clear the wake source to avoid the wrong power state */
*host_get_customer_memmap(0x02) = 0x00;
system_set_bbram(SYSTEM_BBRAM_IDX_VPRO_STATUS, 0);
}
}
void chipset_handle_espi_reset_assert(void)
{
/*
* If eSPI_Reset# pin is asserted without SLP_SUS# being asserted, then
* it means that there is an unexpected power loss (global reset
* event). In this case, check if shutdown was being forced by pressing
* power button. If yes, release power button.
*/
if ((power_get_signals() & IN_PCH_SLP_SUS_DEASSERTED) &&
forcing_shutdown) {
power_button_pch_release();
forcing_shutdown = 0;
}
}
/*
* check EMI region 1 0x02 to control pch power
* if bit set up will keep pch power for RTCwake
* when the unit is vpro type also will keep pch
*
* @return false to disable pch power, true will keep pch
*/
int keep_pch_power(void)
{
int version = board_get_version();
int wake_source = *host_get_customer_memmap(0x02);
uint8_t vpro_change;
system_get_bbram(SYSTEM_BBRAM_IDX_VPRO_STATUS, &vpro_change);
if (custom_forcing_shutdown && power_get_state() == POWER_S5G3) {
custom_forcing_shutdown = 0;
return false;
}
else if (version & BIT(0) && extpower_is_present() && vpro_change)
return true;
#ifdef CONFIG_EMI_REGION1
else if (wake_source & RTCWAKE)
return true;
/* when BIT1 setup, need check AC is exist*/
else if (wake_source & USBWAKE && extpower_is_present())
return true;
#endif
else
return false;
}
#ifdef CONFIG_EMI_REGION1
static void clear_rtcwake(void)
{
*host_get_customer_memmap(0x02) &= ~BIT(0);
}
#endif
void s5_power_up_control(int control)
{
CPRINTS("%s s5 power up!", control ? "setup" : "clear");
power_s5_up = control;
}
static void chipset_force_g3(void)
{
gpio_set_level(GPIO_SUSP_L, 0);
gpio_set_level(GPIO_EC_VCCST_PG, 0);
gpio_set_level(GPIO_VR_ON, 0);
gpio_set_level(GPIO_PCH_PWROK, 0);
gpio_set_level(GPIO_SYS_PWROK, 0);
gpio_set_level(GPIO_SYSON, 0);
/* keep pch power for wake source or vpro type */
if (!keep_pch_power() || me_change) {
gpio_set_level(GPIO_PCH_RSMRST_L, 0);
if (bb_retimer_init_pending == false) {
/* retimer power needs to be tied to PCH_PWR_EN */
cypd_set_retimer_power(POWER_G3);
gpio_set_level(GPIO_PCH_PWR_EN, 0);
}
gpio_set_level(GPIO_PCH_DPWROK, 0);
gpio_set_level(GPIO_PCH_PWRBTN_L, 0);
gpio_set_level(GPIO_AC_PRESENT_OUT, 0);
/*gpio_set_level(GPIO_VS_ON, 0); Todo Fix VS_ON no boot*/
}
f75303_set_enabled(0);
}
void chipset_reset(enum chipset_reset_reason reason)
{
/* TODO */
}
void chipset_throttle_cpu(int throttle)
{
if (chipset_in_state(CHIPSET_STATE_ON))
gpio_set_level(GPIO_EC_PROCHOT_L, !throttle);
}
int board_chipset_power_on(void)
{
/*gpio_set_level(GPIO_VS_ON, 1); Todo fix vson noboot*/
msleep(5);
if (power_wait_signals(IN_PGOOD_PWR_3V5V)) {
CPRINTS("PH Timeout PWR_3V5V_PG");
set_hw_diagnostic(DIAGNOSTICS_HW_PGOOD_3V5V, 1);
chipset_force_g3();
return false;
}
gpio_set_level(GPIO_PCH_PWR_EN, 1);
msleep(10);
/* Need to configure the retimer recovery path before RSMRST is released but after PCH_PWR_EN is up */
cypd_set_retimer_power(POWER_G3S5);
gpio_set_level(GPIO_PCH_PWRBTN_L, 1);
msleep(30);
gpio_set_level(GPIO_PCH_DPWROK, 1);
msleep(5);
if (power_wait_signals(IN_PGOOD_VCCIN_AUX_VR)) {
CPRINTS("PH Timeout VCCIN_AUX_VR_PG");
set_hw_diagnostic(DIAGNOSTICS_VCCIN_AUX_VR, 1);
chipset_force_g3();
return false;
}
me_gpio_change(me_change & ME_UNLOCK ? GPIO_OUT_HIGH : GPIO_OUT_LOW);
/* Add 10ms delay between SUSP_VR and RSMRST */
msleep(20);
/* Deassert RSMRST# */
gpio_set_level(GPIO_PCH_RSMRST_L, 1);
gpio_set_level(GPIO_AC_PRESENT_OUT, 1);
if (want_boot_ap_at_g3) {
CPRINTS("press power button for G3 Boot!");
/* assert the power button to power on system */
msleep(30);
gpio_set_level(GPIO_PCH_PWRBTN_L, 0);
msleep(30);
gpio_set_level(GPIO_PCH_PWRBTN_L, 1);
want_boot_ap_at_g3 = 0;
}
msleep(50);
return true;
}
enum power_state power_chipset_init(void)
{
chipset_force_g3();
return POWER_G3;
}
#ifdef CONFIG_POWER_S0IX
/*
* Backup copies of SCI mask to preserve across S0ix suspend/resume
* cycle. If the host uses S0ix, BIOS is not involved during suspend and resume
* operations and hence SCI masks are programmed only once during boot-up.
*
* These backup variables are set whenever host expresses its interest to
* enter S0ix and then lpc_host_event_mask for SCI are cleared. When
* host resumes from S0ix, masks from backup variables are copied over to
* lpc_host_event_mask for SCI.
*/
static host_event_t backup_sci_mask;
/*
* Clear host event masks for SCI when host is entering S0ix. This is
* done to prevent any SCI interrupts when the host is in suspend. Since
* BIOS is not involved in the suspend path, EC needs to take care of clearing
* these masks.
*/
static void lpc_s0ix_suspend_clear_masks(void)
{
backup_sci_mask = lpc_get_host_event_mask(LPC_HOST_EVENT_SCI);
lpc_set_host_event_mask(LPC_HOST_EVENT_SCI, SCI_HOST_WAKE_EVENT_MASK);
}
/*
* Restore host event masks for SCI when host exits S0ix. This is done
* because BIOS is not involved in the resume path and so EC needs to restore
* the masks from backup variables.
*/
static void lpc_s0ix_resume_restore_masks(void)
{
/*
* No need to restore SCI masks if both backup_sci_mask are zero.
* This indicates that there was a failure to enter S0ix
* and hence SCI masks were never backed up.
*/
if (!backup_sci_mask)
return;
lpc_set_host_event_mask(LPC_HOST_EVENT_SCI, backup_sci_mask);
backup_sci_mask = 0;
}
#ifdef CONFIG_EMI_REGION1
static void power_state_clear(int state)
{
*host_get_customer_memmap(EC_EMEMAP_ER1_POWER_STATE) &= ~state;
}
#endif
static int enter_ms_flag;
static int resume_ms_flag;
static int check_s0ix_statsus(void)
{
int power_status;
int clear_flag;
#ifdef CONFIG_EMI_REGION1
/* check power state S0ix flags */
if (chipset_in_state(CHIPSET_STATE_ON) || chipset_in_state(CHIPSET_STATE_STANDBY)) {
power_status = *host_get_customer_memmap(EC_EMEMAP_ER1_POWER_STATE);
/**
* Sometimes PCH will set the enter and resume flag continuously
* so clear the EMI when we read the flag.
*/
if (power_status & EC_PS_ENTER_S0ix)
enter_ms_flag++;
if (power_status & EC_PS_RESUME_S0ix)
resume_ms_flag++;
clear_flag = power_status & (EC_PS_ENTER_S0ix | EC_PS_RESUME_S0ix);
power_state_clear(clear_flag);
if (enter_ms_flag || resume_ms_flag)
return 1;
}
#endif
return 0;
}
void s0ix_status_handle(void)
{
int s0ix_state_change;
s0ix_state_change = check_s0ix_statsus();
if (s0ix_state_change && chipset_in_state(CHIPSET_STATE_ON))
task_wake(TASK_ID_CHIPSET);
else if (s0ix_state_change && chipset_in_state(CHIPSET_STATE_STANDBY))
task_wake(TASK_ID_CHIPSET);
}
DECLARE_HOOK(HOOK_TICK, s0ix_status_handle, HOOK_PRIO_DEFAULT);
#endif /* CONFIG_POWER_S0IX */
enum power_state power_handle_state(enum power_state state)
{
//struct batt_params batt;
switch (state) {
case POWER_G3:
break;
#ifdef CONFIG_POWER_S0IX
case POWER_S0ix:
CPRINTS("PH S0ix");
if ((power_get_signals() & IN_PCH_SLP_S3_DEASSERTED) == 0) {
/*
* If power signal lose, we need to resume to S0 and
* clear the resume ms flag
*/
if (resume_ms_flag > 0)
resume_ms_flag--;
return POWER_S0;
}
if (check_s0ix_statsus())
return POWER_S0ixS0;
break;
case POWER_S0ixS0:
CPRINTS("PH S0ixS0");
lpc_s0ix_resume_restore_masks();
hook_notify(HOOK_CHIPSET_RESUME);
if (resume_ms_flag > 0)
resume_ms_flag--;
CPRINTS("PH S0ixS0->S0");
return POWER_S0;
break;
case POWER_S0S0ix:
CPRINTS("PH S0->S0ix");
lpc_s0ix_suspend_clear_masks();
hook_notify(HOOK_CHIPSET_SUSPEND);
if (enter_ms_flag > 0)
enter_ms_flag--;
CPRINTS("PH S0S0ix->S0ix");
return POWER_S0ix;
break;
#endif
case POWER_S5:
CPRINTS("PH S5");
if (custom_forcing_shutdown)
/* force shutdown process shouldn't keep PCH power */
return POWER_S5G3;
if (power_s5_up || stress_test_enable) {
/* Wait S5 signal when power up from S5 */
while ((power_get_signals() & IN_PCH_SLP_S4_DEASSERTED) == 0) {
if (task_wait_event(SECOND) == TASK_EVENT_TIMER) {
if (++s5_exit_tries > ap_boot_delay) {
CPRINTS("timeout waiting for S5");
power_button_enable_led(0);
s5_exit_tries = 0;
stress_test_enable = 0;
ap_boot_delay = 9;
set_hw_diagnostic(DIAGNOSTICS_SLP_S4, 1);
return POWER_S5G3;
}
/* power up again */
return POWER_G3S5;
}
}
s5_exit_tries = 0;
return POWER_S5S3; /* Power up to next state */
}
s5_exit_tries = 0;
if ((power_get_signals() & IN_PCH_SLP_S4_DEASSERTED) == IN_PCH_SLP_S4_DEASSERTED) {
return POWER_S5S3;
}
break;
case POWER_S3:
CPRINTS("PH S3");
if (power_get_signals() & IN_PCH_SLP_S3_DEASSERTED) {
/* Power up to next state */
return POWER_S3S0;
} else if ((power_get_signals() & IN_PCH_SLP_S4_DEASSERTED) == 0) {
/* Power down to next state */
return POWER_S3S5;
}
break;
case POWER_S0:
CPRINTS("PH S0");
if ((power_get_signals() & IN_PCH_SLP_S3_DEASSERTED) == 0) {
/* Power down to next state */
gpio_set_level(GPIO_EC_VCCST_PG, 0);
gpio_set_level(GPIO_VR_ON, 0);
return POWER_S0S3;
}
#ifdef CONFIG_POWER_S0IX
if (check_s0ix_statsus())
return POWER_S0S0ix;
#endif
break;
case POWER_G3S5:
/* wait S5 signal, so return POWER S5 state */
if (s5_exit_tries != 0)
return POWER_S5;
s5_power_up_control(1);
if (board_chipset_power_on()) {
cancel_board_power_off();
CPRINTS("PH G3S5->S5");
return POWER_S5;
} else {
return POWER_G3;
}
break;
case POWER_S5S3:
CPRINTS("PH S5S3");
gpio_set_level(GPIO_SYSON, 1);
/* Call hooks now that rails are up */
hook_notify(HOOK_CHIPSET_STARTUP);
CPRINTS("PH S5S3->S3");
return POWER_S3;
break;
case POWER_S3S0:
CPRINTS("PH S3S0");
gpio_set_level(GPIO_SUSP_L, 1);
msleep(10);
f75303_set_enabled(1);
gpio_set_level(GPIO_EC_VCCST_PG, 1);
msleep(30);
gpio_set_level(GPIO_VR_ON, 1);
/* Call hooks now that rails are up */
hook_notify(HOOK_CHIPSET_RESUME);
if (power_wait_signals(IN_PGOOD_PWR_VR)) {
set_hw_diagnostic(DIAGNOSTICS_HW_PGOOD_VR, true);
gpio_set_level(GPIO_SUSP_L, 0);
gpio_set_level(GPIO_EC_VCCST_PG, 0);
gpio_set_level(GPIO_VR_ON, 0);
f75303_set_enabled(0);
return POWER_S3;
}
gpio_set_level(GPIO_PCH_PWROK, 1);
msleep(10);
gpio_set_level(GPIO_SYS_PWROK, 1);
#ifdef CONFIG_EMI_REGION1
clear_rtcwake();
#endif
power_button_enable_led(0);
me_gpio_change(GPIO_FLAG_NONE);
cypd_set_power_active(POWER_S0);
CPRINTS("PH S3S0->S0");
return POWER_S0;
break;
case POWER_S0S3:
CPRINTS("PH S0S3");
gpio_set_level(GPIO_SUSP_L, 0);
gpio_set_level(GPIO_PCH_PWROK, 0);
gpio_set_level(GPIO_SYS_PWROK, 0);
hook_notify(HOOK_CHIPSET_SUSPEND);
me_gpio_change(GPIO_OUT_LOW);
f75303_set_enabled(0);
return POWER_S3;
break;
case POWER_S3S5:
CPRINTS("PH S3S5");
gpio_set_level(GPIO_SYSON, 0);
hook_notify(HOOK_CHIPSET_SHUTDOWN);
cypd_set_power_active(POWER_S5);
/* Call hooks after we remove power rails */
hook_notify(HOOK_CHIPSET_SHUTDOWN_COMPLETE);
power_s5_up = 0;
return POWER_S5;
break;
case POWER_S5G3:
CPRINTS("PH S5G3");
/* if we need to keep pch power, return to G3S5 state */
#ifdef CONFIG_EMI_REGION1
if (keep_pch_power())
return POWER_S5;
#endif
chipset_force_g3();
/* clear suspend flag when system shutdown */
power_state_clear(EC_PS_ENTER_S0ix |
EC_PS_RESUME_S0ix | EC_PS_RESUME_S3 | EC_PS_ENTER_S3);
if (!extpower_is_present()) {
board_power_off();
}
return POWER_G3;
break;
}
return state;
}
void boot_ap_on_g3(void)
{
CPRINTS("Need to boot ap on g3");
want_boot_ap_at_g3 = 1;
}
static enum ec_status set_ap_reboot_delay(struct host_cmd_handler_args *args)
{
const struct ec_response_ap_reboot_delay *p = args->params;
stress_test_enable = 1;
/* don't let AP send zero it will stuck power sequence at S5 */
if (p->delay < 181 && p->delay)
ap_boot_delay = p->delay;
else
return EC_ERROR_INVAL;
return EC_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_SET_AP_REBOOT_DELAY, set_ap_reboot_delay,
EC_VER_MASK(0));
void me_gpio_change(uint32_t flags)
{
enum gpio_signal gpio = GPIO_ME_EN;
int version = board_get_version();
gpio = version > 8 ? GPIO_ME_EN_PVT : GPIO_ME_EN;
gpio_set_flags(gpio, flags);
}
void update_me_change(int change)
{
me_change = change;
}
static enum ec_status me_control(struct host_cmd_handler_args *args)
{
const struct ec_params_me_control *p = args->params;
s5_power_up_control(0); /* power down pch to process ME change */
/* CPU change ME mode based on ME_EN while RSMRST rising.
* So, when we received ME control command, we need to change ME_EN when power on.
* ME_EN low = lock.
*/
if (p->me_mode & ME_UNLOCK)
update_me_change(ME_UNLOCK);
else
update_me_change(ME_LOCK);
CPRINTS("Receive ME %s\n", (p->me_mode & ME_UNLOCK) == ME_UNLOCK ? "unlock" : "lock");
return EC_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_ME_CONTROL, me_control,
EC_VER_MASK(0));
static enum ec_status vpro_control(struct host_cmd_handler_args *args)
{
const struct ec_params_vpro_control *p = args->params;
uint8_t vpro_status;
if (p->vpro_mode & VPRO_ON)
vpro_status = VPRO_ON;
else
vpro_status = VPRO_OFF;
system_set_bbram(SYSTEM_BBRAM_IDX_VPRO_STATUS, vpro_status);
CPRINTS("Receive Vpro %s\n", (p->vpro_mode & VPRO_ON) == VPRO_ON ? "on" : "off");
return EC_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_VPRO_CONTROL, vpro_control,
EC_VER_MASK(0));