payloads/edk2: Disable the CPU Timer Lib unless supported
[coreboot.git] / src / mainboard / lenovo / x60 / smihandler.c
blobe1a97f57eef348ab8f5722435561544e4375dc8e
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <arch/io.h>
4 #include <device/pci_ops.h>
5 #include <console/console.h>
6 #include <cpu/x86/smm.h>
7 #include <soc/nvs.h>
8 #include <southbridge/intel/common/pmutil.h>
9 #include <ec/acpi/ec.h>
10 #include <option.h>
11 #include <ec/lenovo/h8/h8.h>
12 #include <delay.h>
13 #include "dock.h"
14 #include "smi.h"
16 #define GPE_EC_SCI 12
18 static void mainboard_smi_save_cmos(void)
20 u8 tmp70, tmp72;
22 tmp70 = inb(0x70);
23 tmp72 = inb(0x72);
25 set_uint_option("tft_brightness", pci_read_config8(PCI_DEV(0, 2, 1), 0xf4));
26 set_uint_option("volume", ec_read(H8_VOLUME_CONTROL));
28 outb(tmp70, 0x70);
29 outb(tmp72, 0x72);
32 int mainboard_io_trap_handler(int smif)
34 switch (smif) {
35 case SMI_DOCK_CONNECT:
36 ec_clr_bit(0x03, 2);
37 mdelay(250);
38 if (!dock_connect()) {
39 ec_set_bit(0x03, 2);
40 /* set dock LED to indicate status */
41 ec_write(0x0c, 0x09);
42 ec_write(0x0c, 0x88);
43 } else {
44 /* blink dock LED to indicate failure */
45 ec_write(0x0c, 0x08);
46 ec_write(0x0c, 0xc9);
48 break;
50 case SMI_DOCK_DISCONNECT:
51 ec_clr_bit(0x03, 2);
52 dock_disconnect();
53 break;
55 case SMI_SAVE_CMOS:
56 mainboard_smi_save_cmos();
57 break;
58 default:
59 return 0;
62 /* On success, the IO Trap Handler returns 1
63 * On failure, the IO Trap Handler returns a value != 1 */
64 return 1;
67 static void mainboard_smi_brightness_up(void)
69 u8 value;
71 if ((value = pci_read_config8(PCI_DEV(0, 2, 1), 0xf4)) < 0xf0)
72 pci_write_config8(PCI_DEV(0, 2, 1), 0xf4, (value + 0x10) | 0xf);
75 static void mainboard_smi_brightness_down(void)
77 u8 value;
79 if ((value = pci_read_config8(PCI_DEV(0, 2, 1), 0xf4)) > 0x10)
80 pci_write_config8(PCI_DEV(0, 2, 1), 0xf4, (value - 0x10) & 0xf0);
83 static void mainboard_smi_handle_ec_sci(void)
85 u8 status = inb(EC_SC);
86 u8 event;
88 if (!(status & EC_SCI_EVT))
89 return;
91 event = ec_query();
92 printk(BIOS_DEBUG, "EC event %#02x\n", event);
94 switch (event) {
95 /* brightness up */
96 case 0x14:
97 mainboard_smi_brightness_up();
98 mainboard_smi_save_cmos();
99 break;
100 /* brightness down */
101 case 0x15:
102 mainboard_smi_brightness_down();
103 mainboard_smi_save_cmos();
104 break;
105 /* Fn-F9 key */
106 case 0x18:
107 /* Power loss */
108 case 0x27:
109 /* Undock Key */
110 case 0x50:
111 mainboard_io_trap_handler(SMI_DOCK_DISCONNECT);
112 break;
113 /* Dock Event */
114 case 0x37:
115 case 0x58:
116 mainboard_io_trap_handler(SMI_DOCK_CONNECT);
117 break;
118 default:
119 break;
123 void mainboard_smi_gpi(u32 gpi)
125 if (gpi & (1 << GPE_EC_SCI))
126 mainboard_smi_handle_ec_sci();
129 int mainboard_smi_apmc(u8 data)
131 switch (data) {
132 case APM_CNT_ACPI_ENABLE:
133 /* use 0x1600/0x1604 to prevent races with userspace */
134 ec_set_ports(0x1604, 0x1600);
135 /* route H8SCI to SCI */
136 gpi_route_interrupt(GPE_EC_SCI, GPI_IS_SCI);
137 /* discard all events, and enable attention */
138 ec_write(0x80, 0x01);
139 break;
140 case APM_CNT_ACPI_DISABLE:
141 /* we have to use port 0x62/0x66, as 0x1600/0x1604 doesn't
142 provide a EC query function */
143 ec_set_ports(0x66, 0x62);
144 /* route H8SCI# to SMI */
145 gpi_route_interrupt(GPE_EC_SCI, GPI_IS_SMI);
146 /* discard all events, and enable attention */
147 ec_write(0x80, 0x01);
148 break;
149 default:
150 break;
152 return 0;