1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <console/console.h>
4 #include <device/mmio.h>
5 #include <ec/acpi/ec.h>
6 #include <swab.h>
7 #include <timer.h>
8 #include <types.h>
9
10 #include "chip.h"
11 #include "commands.h"
12 #include "ec.h"
13
14 #define ec_cmd send_ec_command
15 #define ec_dat send_ec_data
16
ec_fcmd(uint8_t fcmd)17 static void ec_fcmd(uint8_t fcmd)
18 {
19 write8p(ECRAM + FCMD, fcmd);
20 ec_cmd(ECCMD_NOP);
21
22 /* EC sets FCMD = 0x00 on completion (FCMD = 0xfa on some commands) */
23 int time = wait_us(50000, read8p(ECRAM + FCMD) == 0x00 || read8p(ECRAM + FCMD) == 0xfa);
24 if (time)
25 printk(BIOS_DEBUG, "EC: FCMD 0x%02x completed after %d us\n", fcmd, time);
26 else
27 printk(BIOS_ERR, "EC: FCMD 0x%02x timed out\n", fcmd);
28 }
29
ec_recv_str(char * buf,size_t size)30 static void ec_recv_str(char *buf, size_t size)
31 {
32 while (size--) {
33 *buf = recv_ec_data();
34 if (*buf == '$') { /* end mark */
35 *buf = '\0';
36 return;
37 }
38 buf++;
39 }
40
41 /* Truncate and discard the rest */
42 *--buf = '\0';
43 do {} while (recv_ec_data() != '$');
44 printk(BIOS_ERR, "EC: Received string longer than buffer. Data truncated.\n");
45 }
46
ec_read_model(void)47 char *ec_read_model(void)
48 {
49 static char model[10];
50
51 ec_cmd(ECCMD_READ_MODEL);
52 ec_recv_str(model, sizeof(model));
53
54 return model;
55 }
56
ec_read_fw_version(void)57 char *ec_read_fw_version(void)
58 {
59 static char version[10] = "1.";
60
61 ec_cmd(ECCMD_READ_FW_VER);
62 ec_recv_str(version + 2, sizeof(version) - 2);
63
64 return version;
65 }
66
ec_set_acpi_mode(bool state)67 void ec_set_acpi_mode(bool state)
68 {
69 ec_cmd(state ? ECCMD_ENABLE_ACPI_MODE : ECCMD_DISABLE_ACPI_MODE);
70 if (state)
71 ec_cmd(ECCMD_ENABLE_HOTKEYS);
72 }
73
ec_set_enter_g3_in_s4s5(bool state)74 void ec_set_enter_g3_in_s4s5(bool state)
75 {
76 clrsetbits8p(ECRAM + 0x1e6, 1 << G3FG, state << G3FG);
77 }
78
ec_set_aprd(void)79 void ec_set_aprd(void)
80 {
81 setbits8p(ECRAM + 0x1eb, 1 << APRD);
82 }
83
84 /* To be called by a graphics driver, when detecting a dGPU */
ec_set_dgpu_present(bool state)85 void ec_set_dgpu_present(bool state)
86 {
87 clrsetbits8p(ECRAM + 0x1eb, 1 << DGPT, state << DGPT);
88 }
89
ec_set_fn_win_swap(bool state)90 void ec_set_fn_win_swap(bool state)
91 {
92 clrsetbits8p(ECRAM + ECKS, 1 << SWFN, state << SWFN);
93 }
94
ec_set_ac_fan_always_on(bool state)95 void ec_set_ac_fan_always_on(bool state)
96 {
97 clrsetbits8p(ECRAM + 0x1e6, 1 << FOAC, state << FOAC);
98 }
99
ec_set_kbled_timeout(uint16_t timeout)100 void ec_set_kbled_timeout(uint16_t timeout)
101 {
102 printk(BIOS_DEBUG, "EC: set keyboard backlight timeout to %us\n", timeout);
103
104 write8p(ECRAM + FDAT, timeout ? 0xff : 0x00);
105 write16p(ECRAM + FBUF, swab16(timeout));
106 ec_fcmd(FCMD_SET_KBLED_TIMEOUT);
107 }
108
ec_set_flexicharger(bool state,uint8_t start,uint8_t stop)109 void ec_set_flexicharger(bool state, uint8_t start, uint8_t stop)
110 {
111 printk(BIOS_DEBUG, "EC: set flexicharger: enabled=%d, start=%u%%, stop=%u%%\n",
112 state, start, stop);
113
114 if (!state) {
115 start = 0xff;
116 stop = 0xff;
117
118 } else if (start > 100 || stop > 100) {
119 printk(BIOS_ERR, "EC: invalid flexicharger settings: start/stop > 100%%\n");
120 return;
121
122 } else if (start >= stop) {
123 printk(BIOS_ERR, "EC: invalid flexicharger settings: start >= stop\n");
124 return;
125 }
126
127 write8p(ECRAM + FBF1, state << 1);
128 write8p(ECRAM + FBUF, start);
129 write8p(ECRAM + FDAT, stop);
130 ec_fcmd(FCMD_FLEXICHARGER);
131 }
132
ec_set_camera_boot_state(enum camera_state state)133 void ec_set_camera_boot_state(enum camera_state state)
134 {
135 if (state > CAMERA_STATE_KEEP) {
136 printk(BIOS_ERR,
137 "EC: invalid camera boot state %u. Keeping previous state.\n", state);
138 state = CAMERA_STATE_KEEP;
139 }
140
141 if (state == CAMERA_STATE_KEEP) {
142 /*
143 * The EC maintains the camera's state in RAM. However, it doesn't sync the GPIO
144 * on a concurrent boot. Thus, read the previous state from the EC and set the
145 * state and the GPIO by sending the state command even in the keep-case.
146 */
147 ec_cmd(ECCMD_GET_DEVICES_STATE);
148 state = recv_ec_data() & 1;
149 }
150
151 printk(BIOS_DEBUG, "EC: set camera: enabled=%u\n", state);
152
153 ec_dat(DEVICE_CAMERA | DEVICE_STATE(state));
154 ec_cmd(ECCMD_SET_INV_DEVICE_STATE);
155 }
156
ec_set_tp_toggle_mode(uint8_t mode)157 void ec_set_tp_toggle_mode(uint8_t mode)
158 {
159 switch (mode) {
160 case 0: /* CtrlAltF9 */
161 setbits8p(ECRAM + RINF, TP_TOGGLE_CTRLALTF9);
162 break;
163 case 1: /* KeycodeF7F8*/
164 clrbits8p(ECRAM + RINF, TP_TOGGLE_CTRLALTF9);
165 break;
166 }
167 }
168