summaryrefslogtreecommitdiff
path: root/common/gaia_power.c
blob: 6f392adf7843fb813a76ce200530bef54e4a06e7 (plain)
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
/* Copyright (c) 2012 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.
 */

/*
 * GAIA SoC power sequencing module for Chrome EC
 *
 * This implements the following features:
 *
 * - Cold reset powers off the AP
 *
 *  When powered off:
 *  - Press pwron turns on the AP
 *  - Hold pwron turns on the AP, and then 16s later turns it off and leaves
 *  it off until pwron is released and pressed again
 *
 *  When powered on:
 *  - The PMIC PWRON signal is released one second after the power button is
 *  released (we expect that U-Boot as asserted XPSHOLD by then)
 *  - Holding pwron for 8s powers off the AP
 *  - Pressing and releasing pwron within that 8s is ignored
 *  - If XPSHOLD is dropped by the AP, then we power the AP off
 */

#include "board.h"
#include "chipset.h"  /* This module implements chipset functions too */
#include "console.h"
#include "gpio.h"
#include "keyboard_scan.h"
#include "power_led.h"
#include "task.h"
#include "timer.h"
#include "util.h"

/* Console output macros */
#define CPUTS(outstr) cputs(CC_CHIPSET, outstr)
#define CPRINTF(format, args...) cprintf(CC_CHIPSET, format, ## args)

/* Time necessary for the 5v regulator output to stabilize */
#define DELAY_5V_SETUP        1000  /* 1ms */

/* Delay between 1.35v and 3.3v rails startup */
#define DELAY_RAIL_STAGGERING 100  /* 100us */

/* Long power key press to force shutdown */
#define DELAY_FORCE_SHUTDOWN  8000000 /* 8s */

/*
 * If the power key is pressed to turn on, then held for this long, we
 * power off.
 *
 * The idea here is that behavior for 8s for AP shutdown is unchanged
 * but power-on is modified to allow enough time U-Boot to be updated
 * via USB (which takes about 10sec).
 *
 * So after power button is pressed:

 * Normal case: User releases power button and gaia_power_task() goes
 *    into the inner loop, waiting for next event to occur (power button
 *    press or XPSHOLD == 0).
 *
 * U-Boot updating: User presses and holds power button. EC does not
 *    check XPSHOLD, and waits up to 16sec for an event. If no event occurs
 *    within 16sec, EC powers off AP.
 */
#define DELAY_SHUTDOWN_ON_POWER_HOLD	(16 * 1000000)

/* Delay after power button release before we release GPIO_PMIC_PWRON_L */
#define DELAY_RELEASE_PWRON	1000000 /* 1s */

/* debounce time to prevent accidental power-on after keyboard power off */
#define KB_PWR_ON_DEBOUNCE    250    /* 250us */

/* PMIC fails to set the LDO2 output */
#define PMIC_TIMEOUT          100000  /* 100ms */

/* Default timeout for input transition */
#define FAIL_TIMEOUT          500000 /* 500ms */


/* Application processor power state */
static int ap_on;
static int ap_suspended;

/* simulated event state */
static int force_signal = -1;
static int force_value;

/* 1 if the power button was pressed last time we checked */
static char power_button_was_pressed;

/* time where we will power off, if power button still held down */
static timestamp_t power_off_deadline;

/* 1 if we have released GPIO_PMIC_PWRON_L */
static int pwron_released;

/* time where we will release GPIO_PMIC_PWRON_L */
static timestamp_t pwron_deadline;

/* force AP power on (used for recovery keypress) */
static int auto_power_on;

/*
 * Wait for GPIO "signal" to reach level "value".
 * Returns EC_ERROR_TIMEOUT if timeout before reaching the desired state.
 *
 * @param signal	Signal to watch
 * @param value		Value to watch for
 * @param timeout	Timeout in microseconds from now, or -1 to wait forever
 * @return 0 if signal did change to required value, EC_ERROR_TIMEOUT if we
 * timed out first.
 */
static int wait_in_signal(enum gpio_signal signal, int value, int timeout)
{
	timestamp_t deadline;
	timestamp_t now = get_time();

	deadline.val = now.val + timeout;

	while (((force_signal != signal) || (force_value != value)) &&
			gpio_get_level(signal) != value) {
		now = get_time();
		if (timeout < 0) {
			task_wait_event(-1);
		} else if (timestamp_expired(deadline, &now) ||
				(task_wait_event(deadline.val - now.val) ==
					TASK_EVENT_TIMER)) {
			CPRINTF("Timeout waiting for GPIO %d/%s\n", signal,
				    gpio_get_name(signal));
			return EC_ERROR_TIMEOUT;
		}
	}

	return EC_SUCCESS;
}

/*
 * Check for some event triggering the shutdown.
 *
 * It can be either a long power button press or a shutdown triggered from the
 * AP and detected by reading XPSHOLD.
 *
 * @return 1 if a shutdown should happen, 0 if not
 */
static int check_for_power_off_event(void)
{
	timestamp_t now;
	int pressed = 0;

	/* Check for power button press */
	if (gpio_get_level(GPIO_KB_PWR_ON_L) == 0) {
		udelay(KB_PWR_ON_DEBOUNCE);
		if (gpio_get_level(GPIO_KB_PWR_ON_L) == 0)
			pressed = 1;
	}

	now = get_time();
	if (pressed) {
		gpio_set_level(GPIO_PMIC_PWRON_L, 0);

		if (!power_button_was_pressed) {
			power_off_deadline.val = now.val + DELAY_FORCE_SHUTDOWN;
			CPRINTF("Waiting for long power press %u\n",
				power_off_deadline.le.lo);
		} else if (timestamp_expired(power_off_deadline, &now)) {
			power_off_deadline.val = 0;
			CPRINTF("Power off after long press now=%u, %u\n",
				now.le.lo, power_off_deadline.le.lo);
			return 2;
		}
	} else if (power_button_was_pressed) {
		CPUTS("Cancel power off\n");
		gpio_set_level(GPIO_PMIC_PWRON_L, 1);
	}
	power_button_was_pressed = pressed;

	/* XPSHOLD released by AP : shutdown immediately */
	if (pwron_released && gpio_get_level(GPIO_SOC1V8_XPSHOLD) == 0)
		return 3;
	return 0;
}

void gaia_suspend_event(enum gpio_signal signal)
{
	if (!ap_on) /* power on/off : not a real suspend / resume */
		return;

	ap_suspended = !gpio_get_level(GPIO_SUSPEND_L);

	powerled_set_state(ap_suspended ?
			POWERLED_STATE_SUSPEND : POWERLED_STATE_ON);
}

void gaia_power_event(enum gpio_signal signal)
{
	/* Wake up the task */
	task_wake(TASK_ID_GAIAPOWER);
}

int gaia_power_init(void)
{
	/* Enable interrupts for our GPIOs */
	gpio_enable_interrupt(GPIO_KB_PWR_ON_L);
	gpio_enable_interrupt(GPIO_PP1800_LDO2);
	gpio_enable_interrupt(GPIO_SOC1V8_XPSHOLD);

	/* auto power on if the recovery combination was pressed */
	if (keyboard_scan_recovery_pressed())
		auto_power_on = 1;

	return EC_SUCCESS;
}


/*****************************************************************************/
/* Chipset interface */

int chipset_in_state(int state_mask)
{
	/* If AP is off, match any off state for now */
	if ((state_mask & CHIPSET_STATE_ANY_OFF) && !ap_on)
		return 1;

	/* If AP is on, match on state */
	if ((state_mask & CHIPSET_STATE_ON) && ap_on && !ap_suspended)
		return 1;

	/* if AP is suspended, match on state */
	if ((state_mask & CHIPSET_STATE_SUSPEND) && ap_on && ap_suspended)
		return 1;

	/* In any other case, we don't have a match */
	return 0;
}

void chipset_exit_hard_off(void)
{
	/* TODO: implement, if/when we take the AP down to a hard-off state */
}

/*****************************************************************************/

/**
 * Check if there has been a power-on event
 *
 * This waits for the power button to be pressed, then returns whether it
 * is still pressed, after a debounce period
 *
 * @return 1 if there has been a power-on event, 0 if not
 */
static int check_for_power_on_event(void)
{
	/* the system is already ON */
	if (gpio_get_level(GPIO_EN_PP3300))
		return 1;

	/* power on requested at EC startup for recovery */
	if (auto_power_on) {
		auto_power_on = 0;
		return 1;
	}

	/* wait for Power button press */
	wait_in_signal(GPIO_KB_PWR_ON_L, 0, -1);

	udelay(KB_PWR_ON_DEBOUNCE);
	return gpio_get_level(GPIO_KB_PWR_ON_L) == 0;
}

/**
 * Power on the AP
 *
 * @return 0 if ok, -1 on error (PP1800_LDO2 failed to come on)
 */
static int power_on(void)
{
	/* Enable 5v power rail */
	gpio_set_level(GPIO_EN_PP5000, 1);
	/* wait to have stable power */
	usleep(DELAY_5V_SETUP);

	/* Startup PMIC */
	gpio_set_level(GPIO_PMIC_PWRON_L, 0);
	/* wait for all PMIC regulators to be ready */
	wait_in_signal(GPIO_PP1800_LDO2, 1, PMIC_TIMEOUT);

	/*
	 * If PP1800_LDO2 did not come up (e.g. PMIC_TIMEOUT was reached),
	 * turn off 5v rail and start over.
	 */
	if (gpio_get_level(GPIO_PP1800_LDO2) == 0) {
		gpio_set_level(GPIO_EN_PP5000, 0);
		usleep(DELAY_5V_SETUP);
		CPUTS("Fatal error: PMIC failed to enable\n");
		return -1;
	}

	/* Enable DDR 1.35v power rail */
	gpio_set_level(GPIO_EN_PP1350, 1);
	/* wait to avoid large inrush current */
	usleep(DELAY_RAIL_STAGGERING);
	/* Enable 3.3v power rail */
	gpio_set_level(GPIO_EN_PP3300, 1);
	ap_on = 1;
	powerled_set_state(POWERLED_STATE_ON);
	CPUTS("AP running ...\n");
	return 0;
}

/**
 * Wait for the power button to be released
 *
 * @return 0 if ok, -1 if power button failed to release
 */
static int wait_for_power_button_release(unsigned int timeout_us)
{
	/* wait for Power button release */
	wait_in_signal(GPIO_KB_PWR_ON_L, 1, timeout_us);

	udelay(KB_PWR_ON_DEBOUNCE);
	if (gpio_get_level(GPIO_KB_PWR_ON_L) == 0) {
		CPUTS("Power button was not released in time\n");
		return -1;
	}
	CPUTS("Power button released\n");
	return 0;
}

/**
 * Power off the AP
 */
static void power_off(void)
{
	/* switch off all rails */
	gpio_set_level(GPIO_EN_PP3300, 0);
	gpio_set_level(GPIO_EN_PP1350, 0);
	gpio_set_level(GPIO_PMIC_PWRON_L, 1);
	gpio_set_level(GPIO_EN_PP5000, 0);
	ap_on = 0;
	powerled_set_state(POWERLED_STATE_OFF);
	CPUTS("Shutdown complete.\n");
}

/**
 * Set a timer to release GPIO_PMIC_PWRON_L in the future
 */
static void set_pwron_timer(void)
{
	pwron_deadline = get_time();
	pwron_deadline.val += DELAY_RELEASE_PWRON;
	CPRINTF("Setting pwron timer %d\n", pwron_deadline.val);
	pwron_released = 0;
}

static void check_pwron_release(void)
{
	if (!pwron_released && timestamp_expired(pwron_deadline, NULL)) {
		pwron_deadline.val = 0;
		pwron_released = 1;
		gpio_set_level(GPIO_PMIC_PWRON_L, 1);
		CPRINTF("Releasing pwron\n");
	}
}

/*
 * Calculates the delay in microseconds to the next time we have to check
 * for a power event,
 *
  *@return delay to next check, or -1 if no future check is needed
 */
static int next_pwr_event(void)
{
	uint64_t next;

	if (!pwron_deadline.val && !power_off_deadline.val)
		return -1;

	/* We know that pwron_deadline will be earlier, if it exists */
	next = pwron_deadline.val ? pwron_deadline.val
			: power_off_deadline.val;
	return next - get_time().val;
}


/*****************************************************************************/

void gaia_power_task(void)
{
	int value;

	gaia_power_init();
	ap_on = 0;

	while (1) {
		/* Wait until we need to power on, then power on */
		while (!check_for_power_on_event())
			task_wait_event(-1);

		/*
		 * If we can power on, and the power button is released,
		 * start running!
		 */
		if (!power_on() && !wait_for_power_button_release(
					DELAY_SHUTDOWN_ON_POWER_HOLD)) {
			/* Wait until we need to power off, then power off */
			power_button_was_pressed = 0;
			set_pwron_timer();
			while (value = check_for_power_off_event(), !value) {
				check_pwron_release();
				task_wait_event(next_pwr_event());
			}
			CPRINTF("ending loop %d\n", value);
		}
		power_off();
		wait_for_power_button_release(-1);
	}
}

/*****************************************************************************/
/* Console debug command */

static int command_force_power(int argc, char **argv)
{
	/* simulate power button pressed */
	force_signal = GPIO_KB_PWR_ON_L;
	force_value = 1;
	/* Wake up the task */
	task_wake(TASK_ID_GAIAPOWER);
	/* wait 100 ms */
	usleep(100000);
	/* release power button */
	force_signal = -1;
	force_value = 0;

	return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(forcepower, command_force_power,
			NULL,
			"Force power on",
			NULL);