blob: 20ca060d82be4beffb61ac92f426633fec878da8 [file] [log] [blame]
Angel Pons89ab2502020-04-03 01:22:28 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Sven Schnellee2ca71e2011-02-14 20:02:47 +00002
3#include <arch/io.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02004#include <device/pci_ops.h>
Sven Schnellee2ca71e2011-02-14 20:02:47 +00005#include <console/console.h>
6#include <cpu/x86/smm.h>
Kyösti Mälkki661ad462020-12-29 06:26:21 +02007#include <soc/nvs.h>
Arthur Heymans31312b22018-04-10 12:56:19 +02008#include <southbridge/intel/common/pmutil.h>
Sven Schnellefea6bd12011-04-01 07:28:56 +00009#include <ec/acpi/ec.h>
Kyösti Mälkkicbf95712020-01-05 08:05:45 +020010#include <option.h>
Sven Schnelle4297a9a2011-06-12 14:35:11 +020011#include <ec/lenovo/h8/h8.h>
Sven Schnelle483ec412012-01-10 14:44:12 +010012#include <delay.h>
Sven Schnellefea6bd12011-04-01 07:28:56 +000013#include "dock.h"
14#include "smi.h"
Sven Schnellee2ca71e2011-02-14 20:02:47 +000015
Kyösti Mälkki189f3ba2014-12-29 11:32:27 +020016#define GPE_EC_SCI 12
17
Sven Schnelle4297a9a2011-06-12 14:35:11 +020018static void mainboard_smi_save_cmos(void)
19{
Sven Schnelleedcf9f42011-06-28 08:05:26 +020020 u8 tmp70, tmp72;
Sven Schnelle4297a9a2011-06-12 14:35:11 +020021
22 tmp70 = inb(0x70);
23 tmp72 = inb(0x72);
Sven Schnelle4297a9a2011-06-12 14:35:11 +020024
Angel Ponse76f15f2021-04-19 15:20:28 +020025 set_int_option("tft_brightness", pci_read_config8(PCI_DEV(0, 2, 1), 0xf4));
26 set_int_option("volume", ec_read(H8_VOLUME_CONTROL));
Sven Schnelle4297a9a2011-06-12 14:35:11 +020027
28 outb(tmp70, 0x70);
29 outb(tmp72, 0x72);
Sven Schnellefea6bd12011-04-01 07:28:56 +000030}
31
Sven Schnellee2ca71e2011-02-14 20:02:47 +000032int mainboard_io_trap_handler(int smif)
33{
34 switch (smif) {
Sven Schnellefea6bd12011-04-01 07:28:56 +000035 case SMI_DOCK_CONNECT:
Sven Schnelle8d0b86c2011-07-11 18:36:16 +020036 ec_clr_bit(0x03, 2);
Peter Lemenkov386d3412019-11-20 14:50:31 +010037 mdelay(250);
Sven Schnelle483ec412012-01-10 14:44:12 +010038 if (!dock_connect()) {
Sven Schnelle8d0b86c2011-07-11 18:36:16 +020039 ec_set_bit(0x03, 2);
Sven Schnellefea6bd12011-04-01 07:28:56 +000040 /* set dock LED to indicate status */
Sven Schnelle8d0b86c2011-07-11 18:36:16 +020041 ec_write(0x0c, 0x09);
Sven Schnellefea6bd12011-04-01 07:28:56 +000042 ec_write(0x0c, 0x88);
43 } else {
44 /* blink dock LED to indicate failure */
Sven Schnelle8d0b86c2011-07-11 18:36:16 +020045 ec_write(0x0c, 0x08);
46 ec_write(0x0c, 0xc9);
Sven Schnellefea6bd12011-04-01 07:28:56 +000047 }
Sven Schnellee2ca71e2011-02-14 20:02:47 +000048 break;
Sven Schnellefea6bd12011-04-01 07:28:56 +000049
50 case SMI_DOCK_DISCONNECT:
Sven Schnelle8d0b86c2011-07-11 18:36:16 +020051 ec_clr_bit(0x03, 2);
Sven Schnellefea6bd12011-04-01 07:28:56 +000052 dock_disconnect();
Sven Schnellefea6bd12011-04-01 07:28:56 +000053 break;
54
Sven Schnelle4297a9a2011-06-12 14:35:11 +020055 case SMI_SAVE_CMOS:
56 mainboard_smi_save_cmos();
57 break;
Sven Schnellee2ca71e2011-02-14 20:02:47 +000058 default:
Sven Schnelle0f9cd432011-06-12 16:55:56 +020059 return 0;
Sven Schnellee2ca71e2011-02-14 20:02:47 +000060 }
61
Sven Schnelle0f9cd432011-06-12 16:55:56 +020062 /* On success, the IO Trap Handler returns 1
63 * On failure, the IO Trap Handler returns a value != 1 */
64 return 1;
Sven Schnellee2ca71e2011-02-14 20:02:47 +000065}
Sven Schnelle8b39e072011-06-12 16:49:13 +020066
67static void mainboard_smi_brightness_up(void)
68{
69 u8 value;
70
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);
73}
74
75static void mainboard_smi_brightness_down(void)
76{
77 u8 value;
78
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);
81}
82
83static void mainboard_smi_handle_ec_sci(void)
84{
85 u8 status = inb(EC_SC);
86 u8 event;
87
88 if (!(status & EC_SCI_EVT))
89 return;
90
91 event = ec_query();
Paul Menzel56642932020-07-19 09:42:53 +020092 printk(BIOS_DEBUG, "EC event %#02x\n", event);
Sven Schnelle8b39e072011-06-12 16:49:13 +020093
Elyes HAOUAS0ce41f12018-11-13 10:03:31 +010094 switch (event) {
Sven Schnelle8b39e072011-06-12 16:49:13 +020095 /* 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;
Sven Schnelle8d0b86c2011-07-11 18:36:16 +0200105 /* 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;
Sven Schnelle8b39e072011-06-12 16:49:13 +0200118 default:
119 break;
120 }
121}
122
Duncan Laurie0edc2242013-04-29 15:04:30 -0700123void mainboard_smi_gpi(u32 gpi)
Sven Schnelle8b39e072011-06-12 16:49:13 +0200124{
Kyösti Mälkki189f3ba2014-12-29 11:32:27 +0200125 if (gpi & (1 << GPE_EC_SCI))
Sven Schnelle8b39e072011-06-12 16:49:13 +0200126 mainboard_smi_handle_ec_sci();
127}
128
Stefan Reinauerbf34e942012-04-27 00:44:04 +0200129int mainboard_smi_apmc(u8 data)
Sven Schnelle8b39e072011-06-12 16:49:13 +0200130{
Elyes HAOUAS0ce41f12018-11-13 10:03:31 +0100131 switch (data) {
Sven Schnelle8b39e072011-06-12 16:49:13 +0200132 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 */
Kyösti Mälkkib85a87b2014-12-29 11:32:27 +0200136 gpi_route_interrupt(GPE_EC_SCI, GPI_IS_SCI);
Sven Schnelle8b39e072011-06-12 16:49:13 +0200137 /* 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 */
Kyösti Mälkkib85a87b2014-12-29 11:32:27 +0200145 gpi_route_interrupt(GPE_EC_SCI, GPI_IS_SMI);
Sven Schnelle8b39e072011-06-12 16:49:13 +0200146 /* discard all events, and enable attention */
147 ec_write(0x80, 0x01);
148 break;
149 default:
150 break;
151 }
152 return 0;
153}