blob: 8acbbc768ef26417c734d9ca9b52d60333591474 [file] [log] [blame]
Sean Rhodes296994b2021-10-14 20:58:15 +01001/* SPDX-License-Identifier: GPL-2.0-only */
2
3#include <console/console.h>
Sean Rhodes296994b2021-10-14 20:58:15 +01004#include <device/device.h>
5#include <device/pnp.h>
6#include <ec/acpi/ec.h>
7#include <option.h>
8#include <pc80/keyboard.h>
Sean Rhodes0579c602023-02-16 15:00:14 +00009#include <halt.h>
Sean Rhodes296994b2021-10-14 20:58:15 +010010
11#include "ec.h"
12#include "ecdefs.h"
13
Sean Rhodes6082ee52022-03-09 08:07:30 +000014uint16_t ec_get_version(void)
Sean Rhodes296994b2021-10-14 20:58:15 +010015{
16 return (ec_read(ECRAM_MAJOR_VERSION) << 8) | ec_read(ECRAM_MINOR_VERSION);
17}
18
19static uint8_t get_ec_value_from_option(const char *name,
20 unsigned int fallback,
21 const uint8_t *lut,
22 size_t lut_size)
23{
24 unsigned int index = get_uint_option(name, fallback);
25 if (index >= lut_size)
26 index = fallback;
27 return lut[index];
28}
29
Sean Rhodes0579c602023-02-16 15:00:14 +000030void ec_mirror_flag(void)
31{
32 /*
33 * For the mirror flag to work, the status of the EC pin must be known
34 * at all times, which means external power. This can be either a DC
35 * charger, or PD with CCG6. PD with an ANX7447 requires configuration
36 * from the EC, so the update will interrupt this.
37 *
38 * This means we can unconditionally apply the mirror flag to devices
39 * that have CCG6, present on devices with TBT, but have a manual
40 * flag for devices without it.
41 */
Sean Rhodes7d00b7c2023-07-07 14:27:28 +010042 uint16_t ec_version = ec_get_version();
43
Sean Rhodesded5a602023-09-20 14:51:57 +010044 /* Full mirror support was added in EC 1.18 (0x0112) */
45 if (ec_version < 0x0112)
46 return;
47
Sean Rhodes0579c602023-02-16 15:00:14 +000048 if (CONFIG(EC_STARLABS_MIRROR_SUPPORT) &&
Sean Rhodese4fd5612023-07-18 11:49:19 +010049 (CONFIG(DRIVERS_INTEL_USB4_RETIMER) || get_uint_option("mirror_flag", 0)) &&
Sean Rhodes7d00b7c2023-07-07 14:27:28 +010050 (ec_version != CONFIG_EC_STARLABS_MIRROR_VERSION)) {
Sean Rhodesded5a602023-09-20 14:51:57 +010051
Sean Rhodes7d00b7c2023-07-07 14:27:28 +010052 printk(BIOS_ERR, "ITE: EC version 0x%x doesn't match coreboot version 0x%x.\n",
53 ec_version, CONFIG_EC_STARLABS_MIRROR_VERSION);
Sean Rhodesded5a602023-09-20 14:51:57 +010054
55 ec_write(ECRAM_MIRROR_FLAG, MIRROR_ENABLED);
Sean Rhodes0579c602023-02-16 15:00:14 +000056 }
57}
58
Sean Rhodes6082ee52022-03-09 08:07:30 +000059static uint16_t ec_get_chip_id(unsigned int port)
Sean Rhodes296994b2021-10-14 20:58:15 +010060{
61 return (pnp_read_index(port, ITE_CHIPID1) << 8) |
62 pnp_read_index(port, ITE_CHIPID2);
63}
64
65static void merlin_init(struct device *dev)
66{
67 if (!dev->enabled)
68 return;
69
70 /*
71 * The address/data IO port pair for the ite EC are configurable
72 * through the EC domain and are fixed by the EC's firmware blob. If
73 * the value(s) passed through the "dev" structure don't match the
74 * expected values then output severe warnings.
75 */
76 if (dev->path.pnp.port != ITE_FIXED_ADDR) {
77 printk(BIOS_ERR, "ITE: Incorrect ports defined in devicetree.cb.\n");
78 printk(BIOS_ERR, "ITE: Serious operational issues will arise.\n");
79 return;
80 }
81
Sean Rhodes6082ee52022-03-09 08:07:30 +000082 const uint16_t chip_id = ec_get_chip_id(dev->path.pnp.port);
Sean Rhodes296994b2021-10-14 20:58:15 +010083
84 if (chip_id != ITE_CHIPID_VAL) {
85 printk(BIOS_ERR, "ITE: Expected chip ID 0x%04x, but got 0x%04x instead.\n",
86 ITE_CHIPID_VAL, chip_id);
87 return;
88 }
89
Sean Rhodes0579c602023-02-16 15:00:14 +000090 ec_mirror_flag();
91
Sean Rhodes296994b2021-10-14 20:58:15 +010092 /*
93 * Restore settings from CMOS into EC RAM:
94 *
95 * kbl_timeout
96 * fn_ctrl_swap
97 * max_charge
98 * fan_mode
99 * fn_lock_state
100 * trackpad_state
101 * kbl_brightness
102 * kbl_state
103 */
104
105 /*
106 * Keyboard Backlight Timeout
107 *
108 * Setting: kbl_timeout
109 *
110 * Values: 30 Seconds, 1 Minute, 3 Minutes, 5 Minutes, Never
111 * Default: 30 Seconds
112 *
113 */
114 const uint8_t kbl_timeout[] = {
115 SEC_30,
116 MIN_1,
117 MIN_3,
118 MIN_5,
119 NEVER
120 };
121
122 ec_write(ECRAM_KBL_TIMEOUT,
123 get_ec_value_from_option("kbl_timeout",
124 0,
125 kbl_timeout,
126 ARRAY_SIZE(kbl_timeout)));
127
128 /*
129 * Fn Ctrl Reverse
130 *
131 * Setting: fn_ctrl_swap
132 *
133 * Values: Enabled, Disabled
134 * Default: Disabled
135 *
136 */
137 const uint8_t fn_ctrl_swap[] = {
138 FN_CTRL,
139 CTRL_FN
140 };
141
142 ec_write(ECRAM_FN_CTRL_REVERSE,
143 get_ec_value_from_option("fn_ctrl_swap",
Sean Rhodesc45cfad2023-04-25 22:51:57 +0100144 0,
Sean Rhodes296994b2021-10-14 20:58:15 +0100145 fn_ctrl_swap,
146 ARRAY_SIZE(fn_ctrl_swap)));
147
148 /*
149 * Maximum Charge Level
150 *
151 * Setting: max_charge
152 *
153 * Values: 60%, 80%, 100%
154 * Default: 100%
155 *
156 */
157 const uint8_t max_charge[] = {
158 CHARGE_100,
159 CHARGE_80,
160 CHARGE_60
161 };
162
Sean Rhodes4d1bf7b2022-02-17 13:55:34 +0000163 if (CONFIG(EC_STARLABS_MAX_CHARGE))
164 ec_write(ECRAM_MAX_CHARGE,
165 get_ec_value_from_option("max_charge",
166 0,
167 max_charge,
168 ARRAY_SIZE(max_charge)));
Sean Rhodes296994b2021-10-14 20:58:15 +0100169
170 /*
171 * Fan Mode
172 *
173 * Setting: fan_mode
174 *
175 * Values: Quiet, Normal, Aggressive
176 * Default: Normal
177 *
178 */
179 const uint8_t fan_mode[] = {
180 FAN_NORMAL,
181 FAN_AGGRESSIVE,
182 FAN_QUIET
183 };
184
185 if (CONFIG(EC_STARLABS_FAN))
186 ec_write(ECRAM_FAN_MODE,
187 get_ec_value_from_option("fan_mode",
188 0,
189 fan_mode,
190 ARRAY_SIZE(fan_mode)));
191
192 /*
193 * Function Lock
194 *
195 * Setting: fn_lock_state
196 *
197 * Values: Locked, Unlocked
198 * Default: Locked
199 *
200 */
201 const uint8_t fn_lock_state[] = {
202 UNLOCKED,
203 LOCKED
204 };
205
206 ec_write(ECRAM_FN_LOCK_STATE,
207 get_ec_value_from_option("fn_lock_state",
208 1,
209 fn_lock_state,
210 ARRAY_SIZE(fn_lock_state)));
211
212 /*
213 * Trackpad State
214 *
215 * Setting: trackpad_state
216 *
217 * Values: Enabled, Disabled
218 * Default: Enabled
219 *
220 */
221 const uint8_t trackpad_state[] = {
222 TRACKPAD_ENABLED,
223 TRACKPAD_DISABLED
224 };
225
226 ec_write(ECRAM_TRACKPAD_STATE,
227 get_ec_value_from_option("trackpad_state",
228 0,
229 trackpad_state,
230 ARRAY_SIZE(trackpad_state)));
231
232 /*
233 * Keyboard Backlight Brightness
234 *
235 * Setting: kbl_brightness
236 *
237 * Values: Off, Low, High / Off, On
238 * Default: Low
239 *
240 */
241 const uint8_t kbl_brightness[] = {
242 KBL_ON,
243 KBL_OFF,
244 KBL_LOW,
245 KBL_HIGH
246 };
247
248 if (CONFIG(EC_STARLABS_KBL_LEVELS))
249 ec_write(ECRAM_KBL_BRIGHTNESS,
250 get_ec_value_from_option("kbl_brightness",
251 2,
252 kbl_brightness,
253 ARRAY_SIZE(kbl_brightness)));
254 else
255 ec_write(ECRAM_KBL_BRIGHTNESS,
256 get_ec_value_from_option("kbl_brightness",
257 0,
258 kbl_brightness,
259 ARRAY_SIZE(kbl_brightness)));
260
261 /*
262 * Keyboard Backlight State
263 *
264 * Setting: kbl_state
265 *
266 * Values: Off, On
267 * Default: On
268 *
Sean Rhodescbedae12023-08-16 09:25:42 +0100269 * Note: Always enable, as the brightness level of `off` disables it.
270 *
Sean Rhodes296994b2021-10-14 20:58:15 +0100271 */
Sean Rhodes296994b2021-10-14 20:58:15 +0100272
Sean Rhodescbedae12023-08-16 09:25:42 +0100273 ec_write(ECRAM_KBL_STATE, KBL_ENABLED);
Sean Rhodes296994b2021-10-14 20:58:15 +0100274}
275
276static struct device_operations ops = {
277 .init = merlin_init,
278 .read_resources = noop_read_resources,
279 .set_resources = noop_set_resources,
280};
281
282static struct pnp_info pnp_dev_info[] = {
283 /* Serial Port 1 (UART1) */
284 { NULL, ITE_SP1, PNP_IO0 | PNP_IRQ0, 0x0ff8, },
285 /* Serial Port 2 (UART2) */
286 { NULL, ITE_SP2, PNP_IO0 | PNP_IRQ0, 0x0ff8, },
287 /* System Wake-Up Control (SWUC) */
288 { NULL, ITE_SWUC, PNP_IO0 | PNP_IRQ0, 0xfff0, },
289 /* KBC / Mouse Interface */
290 { NULL, ITE_SWUC, PNP_IRQ0, },
291 /* KBC / Keyboard Interface */
292 { NULL, ITE_KBCK, PNP_IO0 | PNP_IO1 | PNP_IRQ0, 0x07ff, 0x07ff, },
293 /* Consumer IR (CIR) */
294 { NULL, ITE_IR, PNP_IO0 | PNP_IRQ0, 0xfff8, },
295 /* Shared Memory / Flash Interface (SMFI) */
296 { NULL, ITE_SMFI, PNP_IO0 | PNP_IRQ0, 0xfff0, },
297 /* RTC-like Timer (RCTC) */
298 { NULL, ITE_RTCT, PNP_IO0 | PNP_IO1 | PNP_IO2 | PNP_IO3 | PNP_IRQ0,
299 0xfffe, 0xfffe, 0xfffe, 0xfffe, },
300 /* Power Management I/F Channel 1 (PMC1) */
301 { NULL, ITE_PMC1, PNP_IO0 | PNP_IO1 | PNP_IRQ0, 0x07ff, 0x07ff, },
302 /* Power Management I/F Channel 2 (PMC2) */
303 { NULL, ITE_PMC2, PNP_IO0 | PNP_IO1 | PNP_IO2 | PNP_IRQ0, 0x07fc,
304 0x07fc, 0xfff0, },
305 /* Serial Peripheral Interface (SSPI) */
306 { NULL, ITE_SSPI, PNP_IO0 | PNP_IRQ0, 0xfff8, },
307 /* Platform Environment Control Interface (PECI) */
308 { NULL, ITE_PECI, PNP_IRQ0, 0xfff8, },
309 /* Power Management I/F Channel 3 (PMC3) */
310 { NULL, ITE_PMC3, PNP_IO0 | PNP_IO1 | PNP_IRQ0, 0x07ff, 0x07ff, },
311 /* Power Management I/F Channel 4 (PMC4) */
312 { NULL, ITE_PMC4, PNP_IO0 | PNP_IO1 | PNP_IRQ0, 0x07ff, 0x07ff, },
313 /* Power Management I/F Channel 5 (PMC5) */
314 { NULL, ITE_PMC5, PNP_IO0 | PNP_IO1 | PNP_IRQ0, 0x07ff, 0x07ff, },
315};
316
317static void enable_dev(struct device *dev)
318{
319 pnp_enable_devices(dev, &ops, ARRAY_SIZE(pnp_dev_info), pnp_dev_info);
320}
321
322struct chip_operations ec_starlabs_merlin_ops = {
Nicholas Sudsgaardbfb11be2024-01-30 09:53:46 +0900323 .name = "ITE EC",
Sean Rhodes296994b2021-10-14 20:58:15 +0100324 .enable_dev = enable_dev
325};