blob: 9d4170f53901b5d0a2818f2c593facbc07c02c0a [file] [log] [blame]
Duncan Laurieb0bf2802018-10-15 02:18:03 +00001/*
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>
Duncan Laurie32346f02019-03-20 12:51:07 -070017#include <ec/acpi/ec.h>
Duncan Laurieb0bf2802018-10-15 02:18:03 +000018#include <stdint.h>
19#include <string.h>
20
21#include "ec.h"
22#include "commands.h"
23
24int wilco_ec_get_info(enum get_ec_info_cmd type, char *info)
25{
26 struct ec_response_get_ec_info rsp;
27
28 if (!info)
29 return -1;
30 if (wilco_ec_sendrecv(KB_EC_INFO, type, &rsp, sizeof(rsp)) < 0)
31 return -1;
32
33 /* Copy returned string */
34 strncpy(info, rsp.data, sizeof(rsp.data));
35 return 0;
36}
37
38void wilco_ec_print_all_info(void)
39{
40 char info[EC_INFO_MAX_SIZE];
41
42 if (!wilco_ec_get_info(GET_EC_LABEL, info))
43 printk(BIOS_INFO, "EC Label : %s\n", info);
44
45 if (!wilco_ec_get_info(GET_EC_SVN_REV, info))
46 printk(BIOS_INFO, "EC Revision : %s\n", info);
47
48 if (!wilco_ec_get_info(GET_EC_MODEL_NO, info))
49 printk(BIOS_INFO, "EC Model Num : %s\n", info);
50
51 if (!wilco_ec_get_info(GET_EC_BUILD_DATE, info))
52 printk(BIOS_INFO, "EC Build Date : %s\n", info);
53}
Duncan Laurie2f954922018-10-15 02:28:54 +000054
55static int wilco_ec_get_power_smi(struct ec_pm_event_state *pm)
56{
57 struct ec_response_power_smi {
58 uint8_t pm_event_1;
59 uint8_t pm_state_1;
60 uint8_t hotkey;
61 uint8_t pm_state_2;
62 uint8_t pm_state_3;
63 uint8_t pm_state_4;
64 uint8_t pm_state_5;
65 uint8_t pm_event_2;
66 uint8_t pm_state_6;
67 } __packed rsp;
68
69 if (!pm)
70 return -1;
71 if (wilco_ec_sendrecv_noargs(KB_POWER_SMI, &rsp, sizeof(rsp)) < 0)
72 return -1;
73
74 pm->event[0] = rsp.pm_event_1;
75 pm->event[1] = rsp.pm_event_2;
76 pm->state[0] = rsp.pm_state_1;
77 pm->state[1] = rsp.pm_state_2;
78 pm->state[2] = rsp.pm_state_3;
79 pm->state[3] = rsp.pm_state_4;
80 pm->state[4] = rsp.pm_state_5;
81 pm->state[5] = rsp.pm_state_6;
82 pm->hotkey = rsp.hotkey;
83
84 return 0;
85}
86
87static int wilco_ec_get_power_status(struct ec_pm_event_state *pm)
88{
89 struct ec_response_power_status {
90 uint8_t pm_state_1;
91 uint8_t pm_state_2;
92 uint8_t pm_state_3;
93 uint8_t pm_state_4;
94 uint8_t pm_state_5;
95 uint8_t ac_type_lsb;
96 uint8_t pm_state_6;
97 uint8_t pm_event_2;
98 uint8_t ac_type_msb;
99 } __packed rsp;
100
101 if (!pm)
102 return -1;
103 if (wilco_ec_sendrecv_noargs(KB_POWER_STATUS, &rsp, sizeof(rsp)) < 0)
104 return -1;
105
106 pm->hotkey = 0;
107 pm->event[0] = 0;
108 pm->event[1] = rsp.pm_event_2;
109 pm->state[0] = rsp.pm_state_1;
110 pm->state[1] = rsp.pm_state_2;
111 pm->state[2] = rsp.pm_state_3;
112 pm->state[3] = rsp.pm_state_4;
113 pm->state[4] = rsp.pm_state_5;
114 pm->state[5] = rsp.pm_state_6;
115 pm->ac_type = rsp.ac_type_msb << 8 | rsp.ac_type_lsb;
116
117 return 0;
118}
119
120int wilco_ec_get_pm(struct ec_pm_event_state *pm, bool clear)
121{
122 if (clear)
123 return wilco_ec_get_power_smi(pm);
124 else
125 return wilco_ec_get_power_status(pm);
126}
127
128int wilco_ec_get_lid_state(void)
129{
130 struct ec_pm_event_state pm;
131
132 if (wilco_ec_get_power_status(&pm) < 0)
133 return -1;
134
135 return !!(pm.state[0] & EC_PM1_LID_OPEN);
136}
137
Duncan Laurieb34de932019-04-10 18:43:56 -0700138int wilco_ec_get_board_id(uint8_t *id)
139{
140 return wilco_ec_mailbox(WILCO_EC_MSG_RAW, KB_BOARD_ID,
141 NULL, 0, id, sizeof(*id));
142}
143
Duncan Laurie2f954922018-10-15 02:28:54 +0000144void wilco_ec_slp_en(void)
145{
146 /* EC does not respond to this command */
147 if (wilco_ec_mailbox(WILCO_EC_MSG_NO_RESPONSE,
148 KB_SLP_EN, NULL, 0, NULL, 0) < 0)
149 printk(BIOS_ERR, "%s: command failed\n", __func__);
150}
151
152void wilco_ec_power_off(enum ec_power_off_reason reason)
153{
154 /* EC does not respond to this command */
155 if (wilco_ec_mailbox(WILCO_EC_MSG_NO_RESPONSE,
156 KB_POWER_OFF, &reason, 1, NULL, 0) < 0)
157 printk(BIOS_ERR, "%s: command failed\n", __func__);
158}
Duncan Laurie57f22f62018-11-17 12:17:04 -0700159
160int wilco_ec_radio_control(enum ec_radio radio, uint8_t state)
161{
162 uint8_t radio_control[3] = { 0, radio, state };
163
164 return wilco_ec_mailbox(WILCO_EC_MSG_DEFAULT, KB_RADIO_CONTROL,
165 radio_control, ARRAY_SIZE(radio_control),
166 NULL, 0);
167}
Lijian Zhaoa31872c2019-01-10 19:31:15 -0800168
169int wilco_ec_change_wake(uint8_t source, enum ec_wake_change change)
170{
171 uint8_t wake_source[3] = { change, source };
172
173 return wilco_ec_mailbox(WILCO_EC_MSG_DEFAULT, KB_ACPI_WAKEUP_CHANGE,
174 wake_source, ARRAY_SIZE(wake_source),
175 NULL, 0);
176}
Duncan Laurie32346f02019-03-20 12:51:07 -0700177
178int wilco_ec_signed_fw(void)
179{
180 ec_set_ports(CONFIG_EC_BASE_ACPI_COMMAND,
181 CONFIG_EC_BASE_ACPI_DATA);
182 return !!ec_read(EC_RAM_SIGNED_FW);
183}
Keith Short8ef67322019-05-10 11:49:24 -0600184
Keith Shortf41afde2019-05-10 11:52:55 -0600185struct err_code_entry {
186 uint8_t post_code;
187 enum ec_err_code ec_err;
188};
189
190/*
191 * Any post codes not listed in the post_code_err_map[] use default.
192 */
193static const enum ec_err_code default_ec_err = DLED_ROM;
194static const struct err_code_entry post_code_err_map[] = {
195 { .post_code = POST_RAM_FAILURE, .ec_err = DLED_MEMORY, },
196 { .post_code = POST_VIDEO_FAILURE, .ec_err = DLED_PANEL, },
197};
198
199/* Records the most recent post code during boot */
200static uint8_t wilco_ec_saved_post_code;
201
202void wilco_ec_save_post_code(uint8_t post_code)
Keith Short8ef67322019-05-10 11:49:24 -0600203{
Keith Shortf41afde2019-05-10 11:52:55 -0600204 wilco_ec_saved_post_code = post_code;
205}
206
207/* Send error code to the EC based on last saved post code */
208void die_notify(void)
209{
210 size_t i;
211 enum ec_err_code err_code = default_ec_err;
212
213 for (i = 0; i < ARRAY_SIZE(post_code_err_map); i++) {
214 if (post_code_err_map[i].post_code ==
215 wilco_ec_saved_post_code) {
216 err_code = post_code_err_map[i].ec_err;
217 break;
218 }
219 }
220
221 printk(BIOS_EMERG, "Fatal error: post_code 0x%02x, EC err 0x%02x\n",
222 wilco_ec_saved_post_code, err_code);
223
224 wilco_ec_mailbox(WILCO_EC_MSG_DEFAULT, KB_ERR_CODE,
225 &err_code, 1, NULL, 0);
Keith Short8ef67322019-05-10 11:49:24 -0600226}