Duncan Laurie | b0bf280 | 2018-10-15 02:18:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright 2018 Google LLC |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | #include <console/console.h> |
| 17 | #include <stdint.h> |
| 18 | #include <string.h> |
| 19 | |
| 20 | #include "ec.h" |
| 21 | #include "commands.h" |
| 22 | |
| 23 | int wilco_ec_get_info(enum get_ec_info_cmd type, char *info) |
| 24 | { |
| 25 | struct ec_response_get_ec_info rsp; |
| 26 | |
| 27 | if (!info) |
| 28 | return -1; |
| 29 | if (wilco_ec_sendrecv(KB_EC_INFO, type, &rsp, sizeof(rsp)) < 0) |
| 30 | return -1; |
| 31 | |
| 32 | /* Copy returned string */ |
| 33 | strncpy(info, rsp.data, sizeof(rsp.data)); |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | void wilco_ec_print_all_info(void) |
| 38 | { |
| 39 | char info[EC_INFO_MAX_SIZE]; |
| 40 | |
| 41 | if (!wilco_ec_get_info(GET_EC_LABEL, info)) |
| 42 | printk(BIOS_INFO, "EC Label : %s\n", info); |
| 43 | |
| 44 | if (!wilco_ec_get_info(GET_EC_SVN_REV, info)) |
| 45 | printk(BIOS_INFO, "EC Revision : %s\n", info); |
| 46 | |
| 47 | if (!wilco_ec_get_info(GET_EC_MODEL_NO, info)) |
| 48 | printk(BIOS_INFO, "EC Model Num : %s\n", info); |
| 49 | |
| 50 | if (!wilco_ec_get_info(GET_EC_BUILD_DATE, info)) |
| 51 | printk(BIOS_INFO, "EC Build Date : %s\n", info); |
| 52 | } |
Duncan Laurie | 2f95492 | 2018-10-15 02:28:54 +0000 | [diff] [blame^] | 53 | |
| 54 | static int wilco_ec_get_power_smi(struct ec_pm_event_state *pm) |
| 55 | { |
| 56 | struct ec_response_power_smi { |
| 57 | uint8_t pm_event_1; |
| 58 | uint8_t pm_state_1; |
| 59 | uint8_t hotkey; |
| 60 | uint8_t pm_state_2; |
| 61 | uint8_t pm_state_3; |
| 62 | uint8_t pm_state_4; |
| 63 | uint8_t pm_state_5; |
| 64 | uint8_t pm_event_2; |
| 65 | uint8_t pm_state_6; |
| 66 | } __packed rsp; |
| 67 | |
| 68 | if (!pm) |
| 69 | return -1; |
| 70 | if (wilco_ec_sendrecv_noargs(KB_POWER_SMI, &rsp, sizeof(rsp)) < 0) |
| 71 | return -1; |
| 72 | |
| 73 | pm->event[0] = rsp.pm_event_1; |
| 74 | pm->event[1] = rsp.pm_event_2; |
| 75 | pm->state[0] = rsp.pm_state_1; |
| 76 | pm->state[1] = rsp.pm_state_2; |
| 77 | pm->state[2] = rsp.pm_state_3; |
| 78 | pm->state[3] = rsp.pm_state_4; |
| 79 | pm->state[4] = rsp.pm_state_5; |
| 80 | pm->state[5] = rsp.pm_state_6; |
| 81 | pm->hotkey = rsp.hotkey; |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | static int wilco_ec_get_power_status(struct ec_pm_event_state *pm) |
| 87 | { |
| 88 | struct ec_response_power_status { |
| 89 | uint8_t pm_state_1; |
| 90 | uint8_t pm_state_2; |
| 91 | uint8_t pm_state_3; |
| 92 | uint8_t pm_state_4; |
| 93 | uint8_t pm_state_5; |
| 94 | uint8_t ac_type_lsb; |
| 95 | uint8_t pm_state_6; |
| 96 | uint8_t pm_event_2; |
| 97 | uint8_t ac_type_msb; |
| 98 | } __packed rsp; |
| 99 | |
| 100 | if (!pm) |
| 101 | return -1; |
| 102 | if (wilco_ec_sendrecv_noargs(KB_POWER_STATUS, &rsp, sizeof(rsp)) < 0) |
| 103 | return -1; |
| 104 | |
| 105 | pm->hotkey = 0; |
| 106 | pm->event[0] = 0; |
| 107 | pm->event[1] = rsp.pm_event_2; |
| 108 | pm->state[0] = rsp.pm_state_1; |
| 109 | pm->state[1] = rsp.pm_state_2; |
| 110 | pm->state[2] = rsp.pm_state_3; |
| 111 | pm->state[3] = rsp.pm_state_4; |
| 112 | pm->state[4] = rsp.pm_state_5; |
| 113 | pm->state[5] = rsp.pm_state_6; |
| 114 | pm->ac_type = rsp.ac_type_msb << 8 | rsp.ac_type_lsb; |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | int wilco_ec_get_pm(struct ec_pm_event_state *pm, bool clear) |
| 120 | { |
| 121 | if (clear) |
| 122 | return wilco_ec_get_power_smi(pm); |
| 123 | else |
| 124 | return wilco_ec_get_power_status(pm); |
| 125 | } |
| 126 | |
| 127 | int wilco_ec_get_lid_state(void) |
| 128 | { |
| 129 | struct ec_pm_event_state pm; |
| 130 | |
| 131 | if (wilco_ec_get_power_status(&pm) < 0) |
| 132 | return -1; |
| 133 | |
| 134 | return !!(pm.state[0] & EC_PM1_LID_OPEN); |
| 135 | } |
| 136 | |
| 137 | void wilco_ec_slp_en(void) |
| 138 | { |
| 139 | /* EC does not respond to this command */ |
| 140 | if (wilco_ec_mailbox(WILCO_EC_MSG_NO_RESPONSE, |
| 141 | KB_SLP_EN, NULL, 0, NULL, 0) < 0) |
| 142 | printk(BIOS_ERR, "%s: command failed\n", __func__); |
| 143 | } |
| 144 | |
| 145 | void wilco_ec_power_off(enum ec_power_off_reason reason) |
| 146 | { |
| 147 | /* EC does not respond to this command */ |
| 148 | if (wilco_ec_mailbox(WILCO_EC_MSG_NO_RESPONSE, |
| 149 | KB_POWER_OFF, &reason, 1, NULL, 0) < 0) |
| 150 | printk(BIOS_ERR, "%s: command failed\n", __func__); |
| 151 | } |