blob: 0e072f0dc0aa845e896b88b7208aebef981fcace [file] [log] [blame]
Lee Leahy5cb9dda2015-05-01 10:34:54 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2013 Google Inc.
5 * Copyright (C) 2015 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.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Lee Leahy5cb9dda2015-05-01 10:34:54 -070015 */
16
17#include <arch/io.h>
18#include <device/device.h>
19#include <device/pci.h>
20
21#if IS_ENABLED(CONFIG_EC_GOOGLE_CHROMEEC)
22#include "ec.h"
23#include <ec/google/chromeec/ec.h>
24#endif
25#include <rules.h>
Hannah Williams9657f3b2016-01-22 23:04:05 -080026#include <gpio.h>
Lee Leahy5cb9dda2015-05-01 10:34:54 -070027#include <string.h>
28#include <vendorcode/google/chromeos/chromeos.h>
29
Hannah Williams9657f3b2016-01-22 23:04:05 -080030#define WP_GPIO GP_E_22
Lee Leahy5cb9dda2015-05-01 10:34:54 -070031
32#if ENV_RAMSTAGE
33#include <boot/coreboot_tables.h>
34
Lee Leahy5cb9dda2015-05-01 10:34:54 -070035#define ACTIVE_LOW 0
36#define ACTIVE_HIGH 1
37
38void fill_lb_gpios(struct lb_gpios *gpios)
39{
Julius Wernerc445b4f2016-03-31 17:27:05 -070040 struct lb_gpio chromeos_gpios[] = {
41 {-1, ACTIVE_HIGH, get_write_protect_state(), "write protect"},
Furquan Shaikh0325dc62016-07-25 13:02:36 -070042 {-1, ACTIVE_HIGH, vboot_recovery_mode_enabled(), "recovery"},
Julius Wernerc445b4f2016-03-31 17:27:05 -070043 {-1, ACTIVE_HIGH, get_developer_mode_switch(), "developer"},
44 {-1, ACTIVE_HIGH, get_lid_switch(), "lid"},
45 {-1, ACTIVE_HIGH, 0, "power"},
46 {-1, ACTIVE_HIGH, gfx_get_init_done(), "oprom"},
47 };
48 lb_add_gpios(gpios, chromeos_gpios, ARRAY_SIZE(chromeos_gpios));
Lee Leahy5cb9dda2015-05-01 10:34:54 -070049}
50#endif /* ENV_RAMSTAGE */
51
52int get_lid_switch(void)
53{
54#if IS_ENABLED(CONFIG_EC_GOOGLE_CHROMEEC)
55 u8 ec_switches;
56
57 mec_io_bytes(0, EC_LPC_ADDR_MEMMAP + EC_MEMMAP_SWITCHES, 1,
58 &ec_switches, NULL);
59
60 return !!(ec_switches & EC_SWITCH_LID_OPEN);
61#else
62 /* Default to force open. */
63 return 1;
64#endif
65}
66
67int get_developer_mode_switch(void)
68{
69 return 0;
70}
71
72int get_recovery_mode_switch(void)
73{
74#if IS_ENABLED(CONFIG_EC_GOOGLE_CHROMEEC)
75 u8 ec_switches;
76 u32 ec_events;
77 mec_io_bytes(0, EC_LPC_ADDR_MEMMAP + EC_MEMMAP_SWITCHES, 1,
78 &ec_switches, NULL);
79
80 /* If a switch is set, we don't need to look at events. */
81 if (ec_switches & (EC_SWITCH_DEDICATED_RECOVERY))
82 return 1;
83
84 /* Else check if the EC has posted the keyboard recovery event. */
85 ec_events = google_chromeec_get_events_b();
86
87 return !!(ec_events &
88 EC_HOST_EVENT_MASK(EC_HOST_EVENT_KEYBOARD_RECOVERY));
89#else
90 return 0;
91#endif
92}
93
94int clear_recovery_mode_switch(void)
95{
96#if IS_ENABLED(CONFIG_EC_GOOGLE_CHROMEEC)
97 const uint32_t kb_rec_mask =
98 EC_HOST_EVENT_MASK(EC_HOST_EVENT_KEYBOARD_RECOVERY);
99 /* Unconditionally clear the EC recovery request. */
100 return google_chromeec_clear_events_b(kb_rec_mask);
101#else
102 return 0;
103#endif
104}
105
106int get_write_protect_state(void)
107{
108 /*
109 * The vboot loader queries this function in romstage. The GPIOs have
Hannah Williams9657f3b2016-01-22 23:04:05 -0800110 * not been set up yet as that configuration is done in ramstage.
111 * Configuring this GPIO as input so that there isn't any ambiguity
112 * in the reading.
Lee Leahy5cb9dda2015-05-01 10:34:54 -0700113 */
114#if ENV_ROMSTAGE
Hannah Williams9657f3b2016-01-22 23:04:05 -0800115 gpio_input_pullup(WP_GPIO);
Lee Leahy5cb9dda2015-05-01 10:34:54 -0700116#endif
117
118 /* WP is enabled when the pin is reading high. */
Hannah Williams9657f3b2016-01-22 23:04:05 -0800119 return !!gpio_get(WP_GPIO);
Lee Leahy5cb9dda2015-05-01 10:34:54 -0700120}