Debian Codegraph @

src/ec/starlabs/merlin/ite.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <console/console.h>
4 #include <device/device.h>
5 #include <device/pnp.h>
6 #include <ec/acpi/ec.h>
7 #include <option.h>
8 #include <pc80/mc146818rtc.h>
9 #include <halt.h>
10
11 #include "ecdefs.h"
12 #include "option_table.h"
13 #include "ec.h"
14
15 #define ITE_IT5570 0x5570
16 #define ITE_IT8987 0x8987
17
18 uint16_t ec_get_version(void)
19 {
20 return (ec_read(ECRAM_MAJOR_VERSION) << 8) | ec_read(ECRAM_MINOR_VERSION);
21 }
22
23 static uint8_t get_cmos_value(uint32_t bit, uint32_t length)
24 {
25 uint32_t byte, byte_bit;
26 uint8_t uchar;
27
28 byte = bit / 8; // find the byte where the data starts
29 byte_bit = bit % 8; // find the bit in the byte where the data starts
30
31 uchar = cmos_read(byte); // load the byte
32 uchar >>= byte_bit; // shift the bits to byte align
33 // clear unspecified bits
34 return uchar & ((1U << length) - 1);
35 }
36
37 static uint8_t get_ec_value_from_option(const char *name,
38 uint32_t fallback,
39 const uint8_t *lut,
40 size_t lut_size,
41 uint32_t cmos_start_bit,
42 uint32_t cmos_length)
43 {
44 unsigned int index;
45
46 if (cmos_start_bit != UINT_MAX)
47 index = get_cmos_value(cmos_start_bit, cmos_length);
48 else
49 index = get_uint_option(name, fallback);
50
51 if (index >= lut_size)
52 index = fallback;
53 return lut[index];
54 }
55
56 static uint16_t ec_get_chip_id(unsigned int port)
57 {
58 return (pnp_read_index(port, ITE_CHIPID1) << 8) |
59 pnp_read_index(port, ITE_CHIPID2);
60 }
61
62 static void merlin_init(struct device *dev)
63 {
64 if (!dev->enabled)
65 return;
66
67 /*
68 * The address/data IO port pair for the ite EC are configurable
69 * through the EC domain and are fixed by the EC's firmware blob. If
70 * the value(s) passed through the "dev" structure don't match the
71 * expected values then output severe warnings.
72 */
73 if (dev->path.pnp.port != ITE_FIXED_ADDR) {
74 printk(BIOS_ERR, "ITE: Incorrect ports defined in devicetree.cb.\n");
75 printk(BIOS_ERR, "ITE: Serious operational issues will arise.\n");
76 return;
77 }
78
79 const uint16_t chip_id = ec_get_chip_id(dev->path.pnp.port);
80
81 if (chip_id != ITE_IT5570 && chip_id != ITE_IT8987) {
82 printk(BIOS_ERR, "ITE: Unsupported chip ID 0x%04x.\n", chip_id);
83 return;
84 }
85
86 /*
87 * Restore settings from CMOS into EC RAM:
88 *
89 * kbl_timeout
90 * fn_ctrl_swap
91 * max_charge
92 * fan_mode
93 * fn_lock_state
94 * trackpad_state
95 * kbl_brightness
96 * kbl_state
97 * charging_speed
98 * lid_switch
99 * power_led
100 */
101
102 /*
103 * Keyboard Backlight Timeout
104 *
105 * Setting: kbl_timeout
106 *
107 * Values: 30 Seconds, 1 Minute, 3 Minutes, 5 Minutes, Never
108 * Default: 30 Seconds
109 *
110 */
111 const uint8_t kbl_timeout[] = {
112 SEC_30,
113 MIN_1,
114 MIN_3,
115 MIN_5,
116 NEVER
117 };
118
119 ec_write(ECRAM_KBL_TIMEOUT,
120 get_ec_value_from_option("kbl_timeout",
121 0,
122 kbl_timeout,
123 ARRAY_SIZE(kbl_timeout),
124 UINT_MAX,
125 UINT_MAX));
126
127 /*
128 * Fn Ctrl Reverse
129 *
130 * Setting: fn_ctrl_swap
131 *
132 * Values: Enabled, Disabled
133 * Default: Disabled
134 *
135 */
136 const uint8_t fn_ctrl_swap[] = {
137 FN_CTRL,
138 CTRL_FN
139 };
140
141 ec_write(ECRAM_FN_CTRL_REVERSE,
142 get_ec_value_from_option("fn_ctrl_swap",
143 0,
144 fn_ctrl_swap,
145 ARRAY_SIZE(fn_ctrl_swap),
146 UINT_MAX,
147 UINT_MAX));
148
149 /*
150 * Maximum Charge Level
151 *
152 * Setting: max_charge
153 *
154 * Values: 60%, 80%, 100%
155 * Default: 100%
156 *
157 */
158 const uint8_t max_charge[] = {
159 CHARGE_100,
160 CHARGE_80,
161 CHARGE_60
162 };
163
164 if (CONFIG(EC_STARLABS_MAX_CHARGE))
165 ec_write(ECRAM_MAX_CHARGE,
166 get_ec_value_from_option("max_charge",
167 0,
168 max_charge,
169 ARRAY_SIZE(max_charge),
170 UINT_MAX,
171 UINT_MAX));
172
173 /*
174 * Fan Mode
175 *
176 * Setting: fan_mode
177 *
178 * Values: Disabled, Quiet, Normal, Aggressive
179 * Default: Normal
180 *
181 */
182 const uint8_t fan_mode[] = {
183 FAN_NORMAL,
184 FAN_AGGRESSIVE,
185 FAN_QUIET,
186 FAN_DISABLED
187 };
188
189 if (CONFIG(EC_STARLABS_FAN))
190 ec_write(ECRAM_FAN_MODE,
191 get_ec_value_from_option("fan_mode",
192 0,
193 fan_mode,
194 ARRAY_SIZE(fan_mode),
195 UINT_MAX,
196 UINT_MAX));
197
198 /*
199 * Function Lock
200 *
201 * Setting: fn_lock_state
202 *
203 * Values: Locked, Unlocked
204 * Default: Locked
205 *
206 */
207 #ifdef CMOS_VLEN_fn_lock_state
208 const uint8_t fn_lock_state[] = {
209 UNLOCKED,
210 LOCKED
211 };
212
213 ec_write(ECRAM_FN_LOCK_STATE,
214 get_ec_value_from_option("fn_lock_state",
215 1,
216 fn_lock_state,
217 ARRAY_SIZE(fn_lock_state),
218 CMOS_VSTART_fn_lock_state,
219 CMOS_VLEN_fn_lock_state));
220 #endif
221
222 /*
223 * Trackpad State
224 *
225 * Setting: trackpad_state
226 *
227 * Values: Enabled, Disabled
228 * Default: Enabled
229 *
230 */
231 #ifdef CMOS_VSTART_trackpad_state
232 const uint8_t trackpad_state[] = {
233 TRACKPAD_ENABLED,
234 TRACKPAD_DISABLED
235 };
236
237 ec_write(ECRAM_TRACKPAD_STATE,
238 get_ec_value_from_option("trackpad_state",
239 0,
240 trackpad_state,
241 ARRAY_SIZE(trackpad_state),
242 CMOS_VSTART_trackpad_state,
243 CMOS_VLEN_trackpad_state));
244 #endif
245
246 /*
247 * Keyboard Backlight Brightness
248 *
249 * Setting: kbl_brightness
250 *
251 * Values: Off, Low, High / Off, On
252 * Default: Low
253 *
254 */
255 #ifdef CMOS_VSTART_kbl_brightness
256 const uint8_t kbl_brightness[] = {
257 KBL_ON,
258 KBL_OFF,
259 KBL_LOW,
260 KBL_HIGH
261 };
262
263 ec_write(ECRAM_KBL_BRIGHTNESS,
264 get_ec_value_from_option("kbl_brightness",
265 CONFIG(EC_STARLABS_KBL_LEVELS) ? 2 : 0,
266 kbl_brightness,
267 ARRAY_SIZE(kbl_brightness),
268 CMOS_VSTART_kbl_brightness,
269 CMOS_VLEN_kbl_brightness));
270
271 #endif
272
273
274 /*
275 * Keyboard Backlight State
276 *
277 * Setting: kbl_state
278 *
279 * Values: Off, On
280 * Default: On
281 *
282 * Note: Always enable, as the brightness level of `off` disables it.
283 *
284 */
285
286 ec_write(ECRAM_KBL_STATE, KBL_ENABLED);
287
288 /*
289 * Charging Speed
290 *
291 * Setting: charging_speed
292 *
293 * Values: 1.0C, 0.5C, 0.2C
294 * Default: 0.5C
295 *
296 */
297 const uint8_t charging_speed[] = {
298 SPEED_1_0C,
299 SPEED_0_5C,
300 SPEED_0_2C
301 };
302
303 if (CONFIG(EC_STARLABS_CHARGING_SPEED))
304 ec_write(ECRAM_CHARGING_SPEED,
305 get_ec_value_from_option("charging_speed",
306 SPEED_0_5C,
307 charging_speed,
308 ARRAY_SIZE(charging_speed),
309 UINT_MAX,
310 UINT_MAX));
311
312 /*
313 * Lid Switch
314 *
315 * Setting: lid_switch
316 *
317 * Values: 0, 1
318 * Default: 0
319 *
320 */
321 const uint8_t lid_switch[] = {
322 SWITCH_ENABLED,
323 SWITCH_DISABLED
324 };
325
326 if (CONFIG(EC_STARLABS_LID_SWITCH))
327 ec_write(ECRAM_LID_SWITCH,
328 get_ec_value_from_option("lid_switch",
329 0,
330 lid_switch,
331 ARRAY_SIZE(lid_switch),
332 UINT_MAX,
333 UINT_MAX));
334
335 /*
336 * Power LED Brightness
337 *
338 * Setting: power_led
339 *
340 * Values: 0, 1, 2
341 * Default: 0
342 *
343 */
344 const uint8_t power_led[] = {
345 LED_NORMAL,
346 LED_REDUCED,
347 LED_OFF
348 };
349
350 if (CONFIG(EC_STARLABS_POWER_LED))
351 ec_write(ECRAM_POWER_LED,
352 get_ec_value_from_option("power_led",
353 0,
354 power_led,
355 ARRAY_SIZE(power_led),
356 UINT_MAX,
357 UINT_MAX));
358 }
359
360 static struct device_operations ops = {
361 .init = merlin_init,
362 .read_resources = noop_read_resources,
363 .set_resources = noop_set_resources,
364 };
365
366 static struct pnp_info pnp_dev_info[] = {
367 /* Serial Port 1 (UART1) */
368 { NULL, ITE_SP1, PNP_IO0 | PNP_IRQ0, 0x0ff8, },
369 /* Serial Port 2 (UART2) */
370 { NULL, ITE_SP2, PNP_IO0 | PNP_IRQ0, 0x0ff8, },
371 /* System Wake-Up Control (SWUC) */
372 { NULL, ITE_SWUC, PNP_IO0 | PNP_IRQ0, 0xfff0, },
373 /* KBC / Mouse Interface */
374 { NULL, ITE_SWUC, PNP_IRQ0, },
375 /* KBC / Keyboard Interface */
376 { NULL, ITE_KBCK, PNP_IO0 | PNP_IO1 | PNP_IRQ0, 0x07ff, 0x07ff, },
377 /* Consumer IR (CIR) */
378 { NULL, ITE_IR, PNP_IO0 | PNP_IRQ0, 0xfff8, },
379 /* Shared Memory / Flash Interface (SMFI) */
380 { NULL, ITE_SMFI, PNP_IO0 | PNP_IRQ0, 0xfff0, },
381 /* RTC-like Timer (RCTC) */
382 { NULL, ITE_RTCT, PNP_IO0 | PNP_IO1 | PNP_IO2 | PNP_IO3 | PNP_IRQ0,
383 0xfffe, 0xfffe, 0xfffe, 0xfffe, },
384 /* Power Management I/F Channel 1 (PMC1) */
385 { NULL, ITE_PMC1, PNP_IO0 | PNP_IO1 | PNP_IRQ0, 0x07ff, 0x07ff, },
386 /* Power Management I/F Channel 2 (PMC2) */
387 { NULL, ITE_PMC2, PNP_IO0 | PNP_IO1 | PNP_IO2 | PNP_IRQ0, 0x07fc,
388 0x07fc, 0xfff0, },
389 /* Serial Peripheral Interface (SSPI) */
390 { NULL, ITE_SSPI, PNP_IO0 | PNP_IRQ0, 0xfff8, },
391 /* Platform Environment Control Interface (PECI) */
392 { NULL, ITE_PECI, PNP_IRQ0, 0xfff8, },
393 /* Power Management I/F Channel 3 (PMC3) */
394 { NULL, ITE_PMC3, PNP_IO0 | PNP_IO1 | PNP_IRQ0, 0x07ff, 0x07ff, },
395 /* Power Management I/F Channel 4 (PMC4) */
396 { NULL, ITE_PMC4, PNP_IO0 | PNP_IO1 | PNP_IRQ0, 0x07ff, 0x07ff, },
397 /* Power Management I/F Channel 5 (PMC5) */
398 { NULL, ITE_PMC5, PNP_IO0 | PNP_IO1 | PNP_IRQ0, 0x07ff, 0x07ff, },
399 };
400
401 static void enable_dev(struct device *dev)
402 {
403 pnp_enable_devices(dev, &ops, ARRAY_SIZE(pnp_dev_info), pnp_dev_info);
404 }
405
406 struct chip_operations ec_starlabs_merlin_ops = {
407 .name = "ITE EC",
408 .enable_dev = enable_dev
409 };