• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <console/console.h>
4 #include <ec/acpi/ec.h>
5 #include <stdint.h>
6 #include <string.h>
7 
8 #include "ec.h"
9 #include "commands.h"
10 
wilco_ec_get_info(enum get_ec_info_cmd type,char * info)11 int wilco_ec_get_info(enum get_ec_info_cmd type, char *info)
12 {
13 	struct ec_response_get_ec_info rsp;
14 
15 	if (!info)
16 		return -1;
17 	if (wilco_ec_sendrecv(KB_EC_INFO, type, &rsp, sizeof(rsp)) < 0)
18 		return -1;
19 
20 	/* Copy returned string */
21 	strncpy(info, rsp.data, sizeof(rsp.data));
22 	return 0;
23 }
24 
wilco_ec_print_all_info(void)25 void wilco_ec_print_all_info(void)
26 {
27 	char info[EC_INFO_MAX_SIZE];
28 
29 	if (!wilco_ec_get_info(GET_EC_LABEL, info))
30 		printk(BIOS_INFO, "EC Label      : %s\n", info);
31 
32 	if (!wilco_ec_get_info(GET_EC_SVN_REV, info))
33 		printk(BIOS_INFO, "EC Revision   : %s\n", info);
34 
35 	if (!wilco_ec_get_info(GET_EC_MODEL_NO, info))
36 		printk(BIOS_INFO, "EC Model Num  : %s\n", info);
37 
38 	if (!wilco_ec_get_info(GET_EC_BUILD_DATE, info))
39 		printk(BIOS_INFO, "EC Build Date : %s\n", info);
40 }
41 
wilco_ec_get_power_smi(struct ec_pm_event_state * pm)42 static int wilco_ec_get_power_smi(struct ec_pm_event_state *pm)
43 {
44 	struct ec_response_power_smi {
45 		uint8_t pm_event_1;
46 		uint8_t pm_state_1;
47 		uint8_t hotkey;
48 		uint8_t pm_state_2;
49 		uint8_t pm_state_3;
50 		uint8_t pm_state_4;
51 		uint8_t pm_state_5;
52 		uint8_t pm_event_2;
53 		uint8_t pm_state_6;
54 	} __packed rsp;
55 
56 	if (!pm)
57 		return -1;
58 	if (wilco_ec_sendrecv_noargs(KB_POWER_SMI, &rsp, sizeof(rsp)) < 0)
59 		return -1;
60 
61 	pm->event[0] = rsp.pm_event_1;
62 	pm->event[1] = rsp.pm_event_2;
63 	pm->state[0] = rsp.pm_state_1;
64 	pm->state[1] = rsp.pm_state_2;
65 	pm->state[2] = rsp.pm_state_3;
66 	pm->state[3] = rsp.pm_state_4;
67 	pm->state[4] = rsp.pm_state_5;
68 	pm->state[5] = rsp.pm_state_6;
69 	pm->hotkey = rsp.hotkey;
70 
71 	return 0;
72 }
73 
wilco_ec_get_power_status(struct ec_pm_event_state * pm)74 static int wilco_ec_get_power_status(struct ec_pm_event_state *pm)
75 {
76 	struct ec_response_power_status {
77 		uint8_t pm_state_1;
78 		uint8_t pm_state_2;
79 		uint8_t pm_state_3;
80 		uint8_t pm_state_4;
81 		uint8_t pm_state_5;
82 		uint8_t ac_type_lsb;
83 		uint8_t pm_state_6;
84 		uint8_t pm_event_2;
85 		uint8_t ac_type_msb;
86 	} __packed rsp;
87 
88 	if (!pm)
89 		return -1;
90 	if (wilco_ec_sendrecv_noargs(KB_POWER_STATUS, &rsp, sizeof(rsp)) < 0)
91 		return -1;
92 
93 	pm->hotkey = 0;
94 	pm->event[0] = 0;
95 	pm->event[1] = rsp.pm_event_2;
96 	pm->state[0] = rsp.pm_state_1;
97 	pm->state[1] = rsp.pm_state_2;
98 	pm->state[2] = rsp.pm_state_3;
99 	pm->state[3] = rsp.pm_state_4;
100 	pm->state[4] = rsp.pm_state_5;
101 	pm->state[5] = rsp.pm_state_6;
102 	pm->ac_type = rsp.ac_type_msb << 8 | rsp.ac_type_lsb;
103 
104 	return 0;
105 }
106 
wilco_ec_get_pm(struct ec_pm_event_state * pm,bool clear)107 int wilco_ec_get_pm(struct ec_pm_event_state *pm, bool clear)
108 {
109 	if (clear)
110 		return wilco_ec_get_power_smi(pm);
111 	else
112 		return wilco_ec_get_power_status(pm);
113 }
114 
wilco_ec_get_lid_state(void)115 int wilco_ec_get_lid_state(void)
116 {
117 	struct ec_pm_event_state pm;
118 
119 	if (wilco_ec_get_power_status(&pm) < 0)
120 		return -1;
121 
122 	return !!(pm.state[0] & EC_PM1_LID_OPEN);
123 }
124 
wilco_ec_get_board_id(uint8_t * id)125 int wilco_ec_get_board_id(uint8_t *id)
126 {
127 	return wilco_ec_mailbox(WILCO_EC_MSG_RAW, KB_BOARD_ID,
128 				NULL, 0, id, sizeof(*id));
129 }
130 
wilco_ec_slp_en(void)131 void wilco_ec_slp_en(void)
132 {
133 	/* EC does not respond to this command */
134 	if (wilco_ec_mailbox(WILCO_EC_MSG_NO_RESPONSE,
135 			     KB_SLP_EN, NULL, 0, NULL, 0) < 0)
136 		printk(BIOS_ERR, "%s: command failed\n", __func__);
137 }
138 
wilco_ec_power_off(enum ec_power_off_reason reason)139 void wilco_ec_power_off(enum ec_power_off_reason reason)
140 {
141 	/* EC does not respond to this command */
142 	if (wilco_ec_mailbox(WILCO_EC_MSG_NO_RESPONSE,
143 			     KB_POWER_OFF, &reason, 1, NULL, 0) < 0)
144 		printk(BIOS_ERR, "%s: command failed\n", __func__);
145 }
146 
wilco_ec_radio_control(enum ec_radio radio,uint8_t state)147 int wilco_ec_radio_control(enum ec_radio radio, uint8_t state)
148 {
149 	uint8_t radio_control[3] = { radio, RADIO_WRITE, state };
150 
151 	return wilco_ec_mailbox(WILCO_EC_MSG_DEFAULT, KB_RADIO_CONTROL,
152 				radio_control, ARRAY_SIZE(radio_control),
153 				NULL, 0);
154 }
155 
wilco_ec_change_wake(uint8_t source,enum ec_wake_change change)156 int wilco_ec_change_wake(uint8_t source, enum ec_wake_change change)
157 {
158 	uint8_t wake_source[3] = { change, source };
159 
160 	return wilco_ec_mailbox(WILCO_EC_MSG_DEFAULT, KB_ACPI_WAKEUP_CHANGE,
161 				wake_source, ARRAY_SIZE(wake_source),
162 				NULL, 0);
163 }
164 
wilco_ec_signed_fw(void)165 int wilco_ec_signed_fw(void)
166 {
167 	ec_set_ports(CONFIG_EC_BASE_ACPI_COMMAND,
168 		     CONFIG_EC_BASE_ACPI_DATA);
169 	return !!ec_read(EC_RAM_SIGNED_FW);
170 }
171 
172 struct err_code_entry {
173 	uint8_t post_code;
174 	enum ec_err_code ec_err;
175 };
176 
177 /*
178  * Any post codes not listed in the post_code_err_map[] use default.
179  */
180 static const enum ec_err_code default_ec_err = DLED_ROM;
181 static const struct err_code_entry post_code_err_map[] = {
182 	{ .post_code = POSTCODE_RAM_FAILURE, .ec_err = DLED_MEMORY, },
183 	{ .post_code = POSTCODE_VIDEO_FAILURE, .ec_err = DLED_PANEL, },
184 };
185 
186 /* Records the most recent post code during boot */
187 static uint8_t wilco_ec_saved_post_code;
188 
wilco_ec_save_post_code(uint8_t post_code)189 void wilco_ec_save_post_code(uint8_t post_code)
190 {
191 	wilco_ec_saved_post_code = post_code;
192 }
193 
194 /* Send error code to the EC based on last saved post code */
die_notify(void)195 void die_notify(void)
196 {
197 	size_t i;
198 	enum ec_err_code err_code = default_ec_err;
199 
200 	for (i = 0; i < ARRAY_SIZE(post_code_err_map); i++) {
201 		if (post_code_err_map[i].post_code ==
202 		    wilco_ec_saved_post_code) {
203 			err_code = post_code_err_map[i].ec_err;
204 			break;
205 		}
206 	}
207 
208 	printk(BIOS_EMERG, "Fatal error: post_code 0x%02x, EC err 0x%02x\n",
209 	       wilco_ec_saved_post_code, err_code);
210 
211 	wilco_ec_mailbox(WILCO_EC_MSG_DEFAULT, KB_ERR_CODE,
212 			 &err_code, 1, NULL, 0);
213 }
214 
215 /*
216  * EC CPU ID data struct
217  * MBOX[2] = 0xFF
218  * MBOX[3] = CPUID_Low
219  * MBOX[4] = CPUID_Mid
220  * MBOX[5] = CPUID_High
221  * MBOX[6] = CPU_Core
222  * MBOX[7] = GPU_Core
223  * MBOX[8] = Reserved
224  */
wilco_ec_set_cpuid(uint32_t cpuid,uint8_t cpu_cores,uint8_t gpu_cores)225 int wilco_ec_set_cpuid(uint32_t cpuid, uint8_t cpu_cores, uint8_t gpu_cores)
226 {
227 	uint8_t cpu_id[7] = {0}, i;
228 
229 	cpu_id[0] = 0xff;
230 	for (i = 1; i < 4; i++) {
231 		cpu_id[i] = cpuid & 0xff;
232 		cpuid = cpuid >> 8;
233 	}
234 	cpu_id[4] = cpu_cores;
235 	cpu_id[5] = gpu_cores;
236 
237 	return wilco_ec_mailbox(WILCO_EC_MSG_DEFAULT, KB_CPU_ID, cpu_id,
238 				ARRAY_SIZE(cpu_id), NULL, 0);
239 }
240