ec/lenovo/h8: don't load configuration when booting from s3
[coreboot.git] / src / ec / lenovo / h8 / h8.c
blob4e8172e171bd8cd71e3cbb4403b39790ec667487
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 <kconfig.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <smbios.h>
26 #include <pc80/mc146818rtc.h>
27 #include <pc80/keyboard.h>
29 #include "h8.h"
30 #include "chip.h"
32 static void h8_bluetooth_enable(int on)
34 if (on)
35 ec_set_bit(0x3a, 4);
36 else
37 ec_clr_bit(0x3a, 4);
40 void h8_trackpoint_enable(int on)
42 ec_write(H8_TRACKPOINT_CTRL,
43 on ? H8_TRACKPOINT_ON : H8_TRACKPOINT_OFF);
47 /* Controls radio-off pin in WLAN MiniPCIe slot. */
48 void h8_wlan_enable(int on)
50 if (on)
51 ec_set_bit(0x3a, 5);
52 else
53 ec_clr_bit(0x3a, 5);
56 /* Controls radio-off pin in WWAN MiniPCIe slot. */
57 static void h8_wwan_enable(int on)
59 if (on)
60 ec_set_bit(0x3a, 6);
61 else
62 ec_clr_bit(0x3a, 6);
65 /* Controls radio-off pin in UWB MiniPCIe slot. */
66 static void h8_uwb_enable(int on)
68 if (on)
69 ec_set_bit(0x31, 2);
70 else
71 ec_clr_bit(0x31, 2);
74 static void h8_fn_ctrl_swap(int on)
76 if (on)
77 ec_set_bit(0xce, 4);
78 else
79 ec_clr_bit(0xce, 4);
82 enum battery {
83 SECONDARY_BATTERY = 0,
84 PRIMARY_BATTERY = 1,
87 /* h8 charge priority. Defines if primary or secondary
88 * battery is charged first.
89 * Because NVRAM is complete the otherway around as this register,
90 * it's inverted by if
92 static void h8_charge_priority(enum battery battery)
94 if (battery == PRIMARY_BATTERY)
95 ec_clr_bit(0x0, 4);
96 else
97 ec_set_bit(0x0, 4);
100 static void h8_sticky_fn(int on)
102 if (on)
103 ec_set_bit(0x0, 3);
104 else
105 ec_clr_bit(0x0, 3);
108 static void h8_log_ec_version(void)
110 char ecfw[17];
111 u8 len;
112 u16 fwvh, fwvl;
114 len = h8_build_id_and_function_spec_version(ecfw, sizeof ecfw - 1);
115 ecfw[len] = 0;
117 fwvh = ec_read(0xe9);
118 fwvl = ec_read(0xe8);
120 printk(BIOS_INFO, "EC Firmware ID %s, Version %d.%d%d%c\n", ecfw,
121 fwvh >> 4, fwvh & 0x0f, fwvl >> 4, 0x41 + (fwvl & 0xf));
124 void h8_set_audio_mute(int mute)
126 if (mute)
127 ec_set_bit(0x3a, 0);
128 else
129 ec_clr_bit(0x3a, 0);
132 void h8_enable_event(int event)
134 if (event < 0 || event > 127)
135 return;
137 ec_set_bit(0x10 + (event >> 3), event & 7);
140 void h8_disable_event(int event)
142 if (event < 0 || event > 127)
143 return;
145 ec_clr_bit(0x10 + (event >> 3), event & 7);
149 void h8_usb_power_enable(int onoff)
151 if (onoff)
152 ec_set_bit(0x3b, 4);
153 else
154 ec_clr_bit(0x3b, 4);
157 int h8_ultrabay_device_present(void)
159 return ec_read(H8_STATUS1) & 0x5 ? 0 : 1;
162 u8 h8_build_id_and_function_spec_version(char *buf, u8 buf_len)
164 u8 i, c;
165 char str[16 + 1]; /* 16 ASCII chars + \0 */
167 /* Build ID */
168 for (i = 0; i < 8; i++) {
169 c = ec_read(0xf0 + i);
170 if (c < 0x20 || c > 0x7f) {
171 i = snprintf(str, sizeof (str), "*INVALID");
172 break;
174 str[i] = c;
177 /* EC firmware function specification version */
178 i += snprintf(str + i, sizeof (str) - i, "-%u.%u", ec_read(0xef), ec_read(0xeb));
180 i = MIN(buf_len, i);
181 memcpy(buf, str, i);
183 return i;
186 static void h8_smbios_strings(struct device *dev, struct smbios_type11 *t)
188 char tpec[] = "IBM ThinkPad Embedded Controller -[ ]-";
190 h8_build_id_and_function_spec_version(tpec + 35, 17);
192 t->count = smbios_add_string(t->eos, tpec);
195 static void h8_init(device_t dev)
197 pc_keyboard_init(NO_AUX_DEVICE);
200 struct device_operations h8_dev_ops = {
201 .get_smbios_strings = h8_smbios_strings,
202 .init = h8_init,
205 /* configure default values or load value from nvram */
206 static void h8_configure(struct ec_lenovo_h8_config *conf)
208 u8 val;
209 u8 beepmask0, beepmask1;
211 if (get_option(&val, "wlan") != CB_SUCCESS)
212 val = 1;
213 h8_wlan_enable(val);
215 if (get_option(&val, "bluetooth") != CB_SUCCESS)
216 val = 1;
217 h8_bluetooth_enable(val);
219 if (get_option(&val, "wwan") != CB_SUCCESS)
220 val = 1;
221 h8_wwan_enable(val);
223 if (conf->has_uwb) {
224 if (get_option(&val, "uwb") != CB_SUCCESS)
225 val = 1;
227 h8_uwb_enable(val);
230 if (get_option(&val, "fn_ctrl_swap") != CB_SUCCESS)
231 val = 0;
232 h8_fn_ctrl_swap(val);
234 if (get_option(&val, "sticky_fn") != CB_SUCCESS)
235 val = 0;
236 h8_sticky_fn(val);
238 if (get_option(&val, "first_battery") != CB_SUCCESS)
239 val = PRIMARY_BATTERY;
240 h8_charge_priority(val);
242 /* configure sound */
243 if (get_option(&val, "volume") == CB_SUCCESS)
244 ec_write(H8_VOLUME_CONTROL, val);
246 beepmask0 = conf->beepmask0;
247 beepmask1 = conf->beepmask1;
249 if (conf->has_power_management_beeps
250 && get_option(&val, "power_management_beeps") == CB_SUCCESS
251 && val == 0) {
252 beepmask0 = 0x00;
253 beepmask1 = 0x00;
256 if (conf->has_power_management_beeps) {
257 if (get_option(&val, "low_battery_beep") != CB_SUCCESS)
258 val = 1;
259 if (val)
260 beepmask0 |= 2;
261 else
262 beepmask0 &= ~2;
264 ec_write(H8_SOUND_ENABLE0, beepmask0);
265 ec_write(H8_SOUND_ENABLE1, beepmask1);
266 h8_set_audio_mute(0);
269 static void h8_enable(struct device *dev)
271 struct ec_lenovo_h8_config *conf = dev->chip_info;
272 u8 val;
273 u8 config1;
275 dev->ops = &h8_dev_ops;
277 h8_log_ec_version();
279 ec_write(H8_CONFIG0, conf->config0);
280 config1 = conf->config1;
282 if (conf->has_keyboard_backlight) {
283 if (get_option(&val, "backlight") != CB_SUCCESS)
284 val = 0; /* Both backlights. */
285 config1 = (config1 & 0xf3) | ((val & 0x3) << 2);
287 ec_write(H8_CONFIG1, config1);
288 ec_write(H8_CONFIG2, conf->config2);
289 ec_write(H8_CONFIG3, conf->config3);
291 ec_write(H8_SOUND_REPEAT, 0x00);
292 if (!acpi_is_wakeup_s3())
293 h8_configure(conf);
295 /* silence sounds in queue */
296 ec_write(H8_SOUND_REG, 0x00);
298 ec_write(0x10, conf->event0_enable);
299 ec_write(0x11, conf->event1_enable);
300 ec_write(0x12, conf->event2_enable);
301 ec_write(0x13, conf->event3_enable);
302 ec_write(0x14, conf->event4_enable);
303 ec_write(0x15, conf->event5_enable);
304 ec_write(0x16, conf->event6_enable);
305 ec_write(0x17, conf->event7_enable);
306 ec_write(0x18, conf->event8_enable);
307 ec_write(0x19, conf->event9_enable);
308 ec_write(0x1a, conf->eventa_enable);
309 ec_write(0x1b, conf->eventb_enable);
310 ec_write(0x1c, conf->eventc_enable);
311 ec_write(0x1d, conf->eventd_enable);
312 ec_write(0x1e, conf->evente_enable);
313 ec_write(0x1f, conf->eventf_enable);
315 ec_write(H8_FAN_CONTROL, H8_FAN_CONTROL_AUTO);
317 h8_trackpoint_enable(1);
318 h8_usb_power_enable(1);
320 #if !IS_ENABLED(CONFIG_H8_DOCK_EARLY_INIT)
321 h8_mainboard_init_dock();
322 #endif
325 struct chip_operations ec_lenovo_h8_ops = {
326 CHIP_NAME("Lenovo H8 EC")
327 .enable_dev = h8_enable,