Lee Leahy | 5cb9dda | 2015-05-01 10:34:54 -0700 | [diff] [blame] | 1 | /* |
| 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 Leahy | 5cb9dda | 2015-05-01 10:34:54 -0700 | [diff] [blame] | 15 | */ |
| 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 Williams | 9657f3b | 2016-01-22 23:04:05 -0800 | [diff] [blame] | 26 | #include <gpio.h> |
Lee Leahy | 5cb9dda | 2015-05-01 10:34:54 -0700 | [diff] [blame] | 27 | #include <string.h> |
| 28 | #include <vendorcode/google/chromeos/chromeos.h> |
| 29 | |
Hannah Williams | 9657f3b | 2016-01-22 23:04:05 -0800 | [diff] [blame] | 30 | #define WP_GPIO GP_E_22 |
Lee Leahy | 5cb9dda | 2015-05-01 10:34:54 -0700 | [diff] [blame] | 31 | |
| 32 | #if ENV_RAMSTAGE |
| 33 | #include <boot/coreboot_tables.h> |
| 34 | |
Lee Leahy | 5cb9dda | 2015-05-01 10:34:54 -0700 | [diff] [blame] | 35 | #define ACTIVE_LOW 0 |
| 36 | #define ACTIVE_HIGH 1 |
| 37 | |
| 38 | void fill_lb_gpios(struct lb_gpios *gpios) |
| 39 | { |
Julius Werner | c445b4f | 2016-03-31 17:27:05 -0700 | [diff] [blame] | 40 | struct lb_gpio chromeos_gpios[] = { |
| 41 | {-1, ACTIVE_HIGH, get_write_protect_state(), "write protect"}, |
Furquan Shaikh | 0325dc6 | 2016-07-25 13:02:36 -0700 | [diff] [blame^] | 42 | {-1, ACTIVE_HIGH, vboot_recovery_mode_enabled(), "recovery"}, |
Julius Werner | c445b4f | 2016-03-31 17:27:05 -0700 | [diff] [blame] | 43 | {-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 Leahy | 5cb9dda | 2015-05-01 10:34:54 -0700 | [diff] [blame] | 49 | } |
| 50 | #endif /* ENV_RAMSTAGE */ |
| 51 | |
| 52 | int 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 | |
| 67 | int get_developer_mode_switch(void) |
| 68 | { |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | int 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 | |
| 94 | int 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 | |
| 106 | int get_write_protect_state(void) |
| 107 | { |
| 108 | /* |
| 109 | * The vboot loader queries this function in romstage. The GPIOs have |
Hannah Williams | 9657f3b | 2016-01-22 23:04:05 -0800 | [diff] [blame] | 110 | * 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 Leahy | 5cb9dda | 2015-05-01 10:34:54 -0700 | [diff] [blame] | 113 | */ |
| 114 | #if ENV_ROMSTAGE |
Hannah Williams | 9657f3b | 2016-01-22 23:04:05 -0800 | [diff] [blame] | 115 | gpio_input_pullup(WP_GPIO); |
Lee Leahy | 5cb9dda | 2015-05-01 10:34:54 -0700 | [diff] [blame] | 116 | #endif |
| 117 | |
| 118 | /* WP is enabled when the pin is reading high. */ |
Hannah Williams | 9657f3b | 2016-01-22 23:04:05 -0800 | [diff] [blame] | 119 | return !!gpio_get(WP_GPIO); |
Lee Leahy | 5cb9dda | 2015-05-01 10:34:54 -0700 | [diff] [blame] | 120 | } |