Angel Pons | 89ab250 | 2020-04-03 01:22:28 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Sven Schnelle | e2ca71e | 2011-02-14 20:02:47 +0000 | [diff] [blame] | 2 | |
| 3 | #include <arch/io.h> |
Kyösti Mälkki | f1b58b7 | 2019-03-01 13:43:02 +0200 | [diff] [blame] | 4 | #include <device/pci_ops.h> |
Sven Schnelle | e2ca71e | 2011-02-14 20:02:47 +0000 | [diff] [blame] | 5 | #include <console/console.h> |
| 6 | #include <cpu/x86/smm.h> |
Kyösti Mälkki | 661ad46 | 2020-12-29 06:26:21 +0200 | [diff] [blame] | 7 | #include <soc/nvs.h> |
Arthur Heymans | 31312b2 | 2018-04-10 12:56:19 +0200 | [diff] [blame] | 8 | #include <southbridge/intel/common/pmutil.h> |
Sven Schnelle | fea6bd1 | 2011-04-01 07:28:56 +0000 | [diff] [blame] | 9 | #include <ec/acpi/ec.h> |
Kyösti Mälkki | cbf9571 | 2020-01-05 08:05:45 +0200 | [diff] [blame] | 10 | #include <option.h> |
Sven Schnelle | 4297a9a | 2011-06-12 14:35:11 +0200 | [diff] [blame] | 11 | #include <ec/lenovo/h8/h8.h> |
Sven Schnelle | 483ec41 | 2012-01-10 14:44:12 +0100 | [diff] [blame] | 12 | #include <delay.h> |
Sven Schnelle | fea6bd1 | 2011-04-01 07:28:56 +0000 | [diff] [blame] | 13 | #include "dock.h" |
| 14 | #include "smi.h" |
Sven Schnelle | e2ca71e | 2011-02-14 20:02:47 +0000 | [diff] [blame] | 15 | |
Kyösti Mälkki | 189f3ba | 2014-12-29 11:32:27 +0200 | [diff] [blame] | 16 | #define GPE_EC_SCI 12 |
| 17 | |
Sven Schnelle | 4297a9a | 2011-06-12 14:35:11 +0200 | [diff] [blame] | 18 | static void mainboard_smi_save_cmos(void) |
| 19 | { |
Sven Schnelle | edcf9f4 | 2011-06-28 08:05:26 +0200 | [diff] [blame] | 20 | u8 tmp70, tmp72; |
Sven Schnelle | 4297a9a | 2011-06-12 14:35:11 +0200 | [diff] [blame] | 21 | |
| 22 | tmp70 = inb(0x70); |
| 23 | tmp72 = inb(0x72); |
Sven Schnelle | 4297a9a | 2011-06-12 14:35:11 +0200 | [diff] [blame] | 24 | |
Angel Pons | e76f15f | 2021-04-19 15:20:28 +0200 | [diff] [blame] | 25 | 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 Schnelle | 4297a9a | 2011-06-12 14:35:11 +0200 | [diff] [blame] | 27 | |
| 28 | outb(tmp70, 0x70); |
| 29 | outb(tmp72, 0x72); |
Sven Schnelle | fea6bd1 | 2011-04-01 07:28:56 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Sven Schnelle | e2ca71e | 2011-02-14 20:02:47 +0000 | [diff] [blame] | 32 | int mainboard_io_trap_handler(int smif) |
| 33 | { |
| 34 | switch (smif) { |
Sven Schnelle | fea6bd1 | 2011-04-01 07:28:56 +0000 | [diff] [blame] | 35 | case SMI_DOCK_CONNECT: |
Sven Schnelle | 8d0b86c | 2011-07-11 18:36:16 +0200 | [diff] [blame] | 36 | ec_clr_bit(0x03, 2); |
Peter Lemenkov | 386d341 | 2019-11-20 14:50:31 +0100 | [diff] [blame] | 37 | mdelay(250); |
Sven Schnelle | 483ec41 | 2012-01-10 14:44:12 +0100 | [diff] [blame] | 38 | if (!dock_connect()) { |
Sven Schnelle | 8d0b86c | 2011-07-11 18:36:16 +0200 | [diff] [blame] | 39 | ec_set_bit(0x03, 2); |
Sven Schnelle | fea6bd1 | 2011-04-01 07:28:56 +0000 | [diff] [blame] | 40 | /* set dock LED to indicate status */ |
Sven Schnelle | 8d0b86c | 2011-07-11 18:36:16 +0200 | [diff] [blame] | 41 | ec_write(0x0c, 0x09); |
Sven Schnelle | fea6bd1 | 2011-04-01 07:28:56 +0000 | [diff] [blame] | 42 | ec_write(0x0c, 0x88); |
| 43 | } else { |
| 44 | /* blink dock LED to indicate failure */ |
Sven Schnelle | 8d0b86c | 2011-07-11 18:36:16 +0200 | [diff] [blame] | 45 | ec_write(0x0c, 0x08); |
| 46 | ec_write(0x0c, 0xc9); |
Sven Schnelle | fea6bd1 | 2011-04-01 07:28:56 +0000 | [diff] [blame] | 47 | } |
Sven Schnelle | e2ca71e | 2011-02-14 20:02:47 +0000 | [diff] [blame] | 48 | break; |
Sven Schnelle | fea6bd1 | 2011-04-01 07:28:56 +0000 | [diff] [blame] | 49 | |
| 50 | case SMI_DOCK_DISCONNECT: |
Sven Schnelle | 8d0b86c | 2011-07-11 18:36:16 +0200 | [diff] [blame] | 51 | ec_clr_bit(0x03, 2); |
Sven Schnelle | fea6bd1 | 2011-04-01 07:28:56 +0000 | [diff] [blame] | 52 | dock_disconnect(); |
Sven Schnelle | fea6bd1 | 2011-04-01 07:28:56 +0000 | [diff] [blame] | 53 | break; |
| 54 | |
Sven Schnelle | 4297a9a | 2011-06-12 14:35:11 +0200 | [diff] [blame] | 55 | case SMI_SAVE_CMOS: |
| 56 | mainboard_smi_save_cmos(); |
| 57 | break; |
Sven Schnelle | e2ca71e | 2011-02-14 20:02:47 +0000 | [diff] [blame] | 58 | default: |
Sven Schnelle | 0f9cd43 | 2011-06-12 16:55:56 +0200 | [diff] [blame] | 59 | return 0; |
Sven Schnelle | e2ca71e | 2011-02-14 20:02:47 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Sven Schnelle | 0f9cd43 | 2011-06-12 16:55:56 +0200 | [diff] [blame] | 62 | /* On success, the IO Trap Handler returns 1 |
| 63 | * On failure, the IO Trap Handler returns a value != 1 */ |
| 64 | return 1; |
Sven Schnelle | e2ca71e | 2011-02-14 20:02:47 +0000 | [diff] [blame] | 65 | } |
Sven Schnelle | 8b39e07 | 2011-06-12 16:49:13 +0200 | [diff] [blame] | 66 | |
| 67 | static 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 | |
| 75 | static 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 | |
| 83 | static 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 Menzel | 5664293 | 2020-07-19 09:42:53 +0200 | [diff] [blame] | 92 | printk(BIOS_DEBUG, "EC event %#02x\n", event); |
Sven Schnelle | 8b39e07 | 2011-06-12 16:49:13 +0200 | [diff] [blame] | 93 | |
Elyes HAOUAS | 0ce41f1 | 2018-11-13 10:03:31 +0100 | [diff] [blame] | 94 | switch (event) { |
Sven Schnelle | 8b39e07 | 2011-06-12 16:49:13 +0200 | [diff] [blame] | 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; |
Sven Schnelle | 8d0b86c | 2011-07-11 18:36:16 +0200 | [diff] [blame] | 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; |
Sven Schnelle | 8b39e07 | 2011-06-12 16:49:13 +0200 | [diff] [blame] | 118 | default: |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | |
Duncan Laurie | 0edc224 | 2013-04-29 15:04:30 -0700 | [diff] [blame] | 123 | void mainboard_smi_gpi(u32 gpi) |
Sven Schnelle | 8b39e07 | 2011-06-12 16:49:13 +0200 | [diff] [blame] | 124 | { |
Kyösti Mälkki | 189f3ba | 2014-12-29 11:32:27 +0200 | [diff] [blame] | 125 | if (gpi & (1 << GPE_EC_SCI)) |
Sven Schnelle | 8b39e07 | 2011-06-12 16:49:13 +0200 | [diff] [blame] | 126 | mainboard_smi_handle_ec_sci(); |
| 127 | } |
| 128 | |
Stefan Reinauer | bf34e94 | 2012-04-27 00:44:04 +0200 | [diff] [blame] | 129 | int mainboard_smi_apmc(u8 data) |
Sven Schnelle | 8b39e07 | 2011-06-12 16:49:13 +0200 | [diff] [blame] | 130 | { |
Elyes HAOUAS | 0ce41f1 | 2018-11-13 10:03:31 +0100 | [diff] [blame] | 131 | switch (data) { |
Sven Schnelle | 8b39e07 | 2011-06-12 16:49:13 +0200 | [diff] [blame] | 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 */ |
Kyösti Mälkki | b85a87b | 2014-12-29 11:32:27 +0200 | [diff] [blame] | 136 | gpi_route_interrupt(GPE_EC_SCI, GPI_IS_SCI); |
Sven Schnelle | 8b39e07 | 2011-06-12 16:49:13 +0200 | [diff] [blame] | 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 */ |
Kyösti Mälkki | b85a87b | 2014-12-29 11:32:27 +0200 | [diff] [blame] | 145 | gpi_route_interrupt(GPE_EC_SCI, GPI_IS_SMI); |
Sven Schnelle | 8b39e07 | 2011-06-12 16:49:13 +0200 | [diff] [blame] | 146 | /* discard all events, and enable attention */ |
| 147 | ec_write(0x80, 0x01); |
| 148 | break; |
| 149 | default: |
| 150 | break; |
| 151 | } |
| 152 | return 0; |
| 153 | } |