ec/lenovo/h8: Add BDC detection support
[coreboot.git] / src / ec / lenovo / h8 / h8.c
blob6f14953cedc2d07f67cacdbbea1ee4c30497b152
1 /*
2 * This file is part of the coreboot project.
4 * Copyright (C) 2011 Sven Schnelle <svens@stackframe.org>
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.
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.
16 #include <arch/acpi.h>
17 #include <arch/io.h>
18 #include <console/console.h>
19 #include <device/device.h>
20 #include <device/pnp.h>
21 #include <ec/acpi/ec.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <smbios.h>
25 #include <pc80/mc146818rtc.h>
26 #include <pc80/keyboard.h>
28 #include "h8.h"
29 #include "chip.h"
31 void h8_trackpoint_enable(int on)
33 ec_write(H8_TRACKPOINT_CTRL,
34 on ? H8_TRACKPOINT_ON : H8_TRACKPOINT_OFF);
38 /* Controls radio-off pin in WLAN MiniPCIe slot. */
39 void h8_wlan_enable(int on)
41 if (on)
42 ec_set_bit(0x3a, 5);
43 else
44 ec_clr_bit(0x3a, 5);
47 /* Controls radio-off pin in WWAN MiniPCIe slot. */
48 static void h8_wwan_enable(int on)
50 if (on)
51 ec_set_bit(0x3a, 6);
52 else
53 ec_clr_bit(0x3a, 6);
56 /* Controls radio-off pin in UWB MiniPCIe slot. */
57 static void h8_uwb_enable(int on)
59 if (on)
60 ec_set_bit(0x31, 2);
61 else
62 ec_clr_bit(0x31, 2);
65 static void h8_fn_ctrl_swap(int on)
67 if (on)
68 ec_set_bit(0xce, 4);
69 else
70 ec_clr_bit(0xce, 4);
73 enum battery {
74 SECONDARY_BATTERY = 0,
75 PRIMARY_BATTERY = 1,
78 /* h8 charge priority. Defines if primary or secondary
79 * battery is charged first.
80 * Because NVRAM is complete the otherway around as this register,
81 * it's inverted by if
83 static void h8_charge_priority(enum battery battery)
85 if (battery == PRIMARY_BATTERY)
86 ec_clr_bit(0x0, 4);
87 else
88 ec_set_bit(0x0, 4);
91 static void h8_sticky_fn(int on)
93 if (on)
94 ec_set_bit(0x0, 3);
95 else
96 ec_clr_bit(0x0, 3);
99 static void h8_log_ec_version(void)
101 char ecfw[17];
102 u8 len;
103 u16 fwvh, fwvl;
105 len = h8_build_id_and_function_spec_version(ecfw, sizeof ecfw - 1);
106 ecfw[len] = 0;
108 fwvh = ec_read(0xe9);
109 fwvl = ec_read(0xe8);
111 printk(BIOS_INFO, "EC Firmware ID %s, Version %d.%d%d%c\n", ecfw,
112 fwvh >> 4, fwvh & 0x0f, fwvl >> 4, 0x41 + (fwvl & 0xf));
115 void h8_set_audio_mute(int mute)
117 if (mute)
118 ec_set_bit(0x3a, 0);
119 else
120 ec_clr_bit(0x3a, 0);
123 void h8_enable_event(int event)
125 if (event < 0 || event > 127)
126 return;
128 ec_set_bit(0x10 + (event >> 3), event & 7);
131 void h8_disable_event(int event)
133 if (event < 0 || event > 127)
134 return;
136 ec_clr_bit(0x10 + (event >> 3), event & 7);
140 void h8_usb_power_enable(int onoff)
142 if (onoff)
143 ec_set_bit(0x3b, 4);
144 else
145 ec_clr_bit(0x3b, 4);
148 int h8_ultrabay_device_present(void)
150 return ec_read(H8_STATUS1) & 0x5 ? 0 : 1;
153 u8 h8_build_id_and_function_spec_version(char *buf, u8 buf_len)
155 u8 i, c;
156 char str[16 + 1]; /* 16 ASCII chars + \0 */
158 /* Build ID */
159 for (i = 0; i < 8; i++) {
160 c = ec_read(0xf0 + i);
161 if (c < 0x20 || c > 0x7f) {
162 i = snprintf(str, sizeof (str), "*INVALID");
163 break;
165 str[i] = c;
168 /* EC firmware function specification version */
169 i += snprintf(str + i, sizeof (str) - i, "-%u.%u", ec_read(0xef), ec_read(0xeb));
171 i = MIN(buf_len, i);
172 memcpy(buf, str, i);
174 return i;
177 #if IS_ENABLED(CONFIG_GENERATE_SMBIOS_TABLES)
178 static void h8_smbios_strings(struct device *dev, struct smbios_type11 *t)
180 char tpec[] = "IBM ThinkPad Embedded Controller -[ ]-";
182 h8_build_id_and_function_spec_version(tpec + 35, 17);
184 t->count = smbios_add_string(t->eos, tpec);
186 #endif
188 static void h8_init(device_t dev)
190 pc_keyboard_init(NO_AUX_DEVICE);
193 struct device_operations h8_dev_ops = {
194 #if IS_ENABLED(CONFIG_GENERATE_SMBIOS_TABLES)
195 .get_smbios_strings = h8_smbios_strings,
196 #endif
197 .init = h8_init,
200 static void h8_enable(struct device *dev)
202 struct ec_lenovo_h8_config *conf = dev->chip_info;
203 u8 val;
204 u8 beepmask0, beepmask1, reg8;
206 dev->ops = &h8_dev_ops;
208 h8_log_ec_version();
210 /* Always enable I/O range 0x1600-0x160f and thermal management */
211 reg8 = conf->config0;
212 reg8 |= H8_CONFIG0_SMM_H8_ENABLE;
213 reg8 |= H8_CONFIG0_TC_ENABLE;
214 ec_write(H8_CONFIG0, reg8);
216 reg8 = conf->config1;
217 if (conf->has_keyboard_backlight) {
218 if (get_option(&val, "backlight") != CB_SUCCESS)
219 val = 0; /* Both backlights. */
220 reg8 = (reg8 & 0xf3) | ((val & 0x3) << 2);
222 ec_write(H8_CONFIG1, reg8);
223 ec_write(H8_CONFIG2, conf->config2);
224 ec_write(H8_CONFIG3, conf->config3);
226 beepmask0 = conf->beepmask0;
227 beepmask1 = conf->beepmask1;
229 if (conf->has_power_management_beeps
230 && get_option(&val, "power_management_beeps") == CB_SUCCESS
231 && val == 0) {
232 beepmask0 = 0x00;
233 beepmask1 = 0x00;
236 if (conf->has_power_management_beeps) {
237 if (get_option(&val, "low_battery_beep") != CB_SUCCESS)
238 val = 1;
239 if (val)
240 beepmask0 |= 2;
241 else
242 beepmask0 &= ~2;
245 ec_write(H8_SOUND_ENABLE0, beepmask0);
246 ec_write(H8_SOUND_ENABLE1, beepmask1);
248 /* silence sounds in queue */
249 ec_write(H8_SOUND_REPEAT, 0x00);
250 ec_write(H8_SOUND_REG, 0x00);
252 ec_write(0x10, conf->event0_enable);
253 ec_write(0x11, conf->event1_enable);
254 ec_write(0x12, conf->event2_enable);
255 ec_write(0x13, conf->event3_enable);
256 ec_write(0x14, conf->event4_enable);
257 ec_write(0x15, conf->event5_enable);
258 ec_write(0x16, conf->event6_enable);
259 ec_write(0x17, conf->event7_enable);
260 ec_write(0x18, conf->event8_enable);
261 ec_write(0x19, conf->event9_enable);
262 ec_write(0x1a, conf->eventa_enable);
263 ec_write(0x1b, conf->eventb_enable);
264 ec_write(0x1c, conf->eventc_enable);
265 ec_write(0x1d, conf->eventd_enable);
266 ec_write(0x1e, conf->evente_enable);
267 ec_write(0x1f, conf->eventf_enable);
269 ec_write(H8_FAN_CONTROL, H8_FAN_CONTROL_AUTO);
270 ec_write(H8_USB_ALWAYS_ON, ec_read(H8_USB_ALWAYS_ON) &
271 ~H8_USB_ALWAYS_ON_ENABLE);
273 if (get_option(&val, "wlan") != CB_SUCCESS)
274 val = 1;
275 h8_wlan_enable(val);
277 h8_trackpoint_enable(1);
278 h8_usb_power_enable(1);
280 if (get_option(&val, "volume") == CB_SUCCESS && !acpi_is_wakeup_s3())
281 ec_write(H8_VOLUME_CONTROL, val);
283 val = h8_has_bdc(dev) && h8_bluetooth_nv_enable();
284 h8_bluetooth_enable(val);
286 if (get_option(&val, "wwan") != CB_SUCCESS)
287 val = 1;
289 h8_wwan_enable(val);
291 if (conf->has_uwb) {
292 if (get_option(&val, "uwb") != CB_SUCCESS)
293 val = 1;
295 h8_uwb_enable(val);
298 if (get_option(&val, "fn_ctrl_swap") != CB_SUCCESS)
299 val = 0;
300 h8_fn_ctrl_swap(val);
302 if (get_option(&val, "sticky_fn") != CB_SUCCESS)
303 val = 0;
304 h8_sticky_fn(val);
306 if (get_option(&val, "first_battery") != CB_SUCCESS)
307 val = PRIMARY_BATTERY;
308 h8_charge_priority(val);
310 h8_set_audio_mute(0);
312 #if !IS_ENABLED(CONFIG_H8_DOCK_EARLY_INIT)
313 h8_mainboard_init_dock();
314 #endif
317 struct chip_operations ec_lenovo_h8_ops = {
318 CHIP_NAME("Lenovo H8 EC")
319 .enable_dev = h8_enable,