Angel Pons | 89ab250 | 2020-04-03 01:22:28 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Sven Schnelle | d8129f9 | 2011-04-20 09:12:17 +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> |
Patrick Rudolph | 9f8f115 | 2020-05-06 11:58:45 +0200 | [diff] [blame] | 5 | #include <device/pci_def.h> |
Sven Schnelle | d8129f9 | 2011-04-20 09:12:17 +0000 | [diff] [blame] | 6 | #include <console/console.h> |
| 7 | #include <cpu/x86/smm.h> |
Kyösti Mälkki | 661ad46 | 2020-12-29 06:26:21 +0200 | [diff] [blame] | 8 | #include <soc/nvs.h> |
Arthur Heymans | 31312b2 | 2018-04-10 12:56:19 +0200 | [diff] [blame] | 9 | #include <southbridge/intel/common/pmutil.h> |
Sven Schnelle | d8129f9 | 2011-04-20 09:12:17 +0000 | [diff] [blame] | 10 | #include <ec/acpi/ec.h> |
| 11 | #include "dock.h" |
| 12 | #include "smi.h" |
| 13 | |
Kyösti Mälkki | 189f3ba | 2014-12-29 11:32:27 +0200 | [diff] [blame] | 14 | #define GPE_EC_SCI 12 |
| 15 | |
Sven Schnelle | 61cd5bf | 2011-06-23 19:12:25 +0200 | [diff] [blame] | 16 | #define LVTMA_BL_MOD_LEVEL 0x7af9 /* ATI Radeon backlight level */ |
Sven Schnelle | d8129f9 | 2011-04-20 09:12:17 +0000 | [diff] [blame] | 17 | |
Sven Schnelle | 61cd5bf | 2011-06-23 19:12:25 +0200 | [diff] [blame] | 18 | static void mainboard_smi_brightness_down(void) |
| 19 | { |
Patrick Rudolph | 9f8f115 | 2020-05-06 11:58:45 +0200 | [diff] [blame] | 20 | uint32_t reg32 = pci_read_config32(PCI_DEV(1, 0, 0), PCI_BASE_ADDRESS_2) & ~0xf; |
| 21 | u8 *bar = (void *)(uintptr_t)reg32; |
| 22 | |
| 23 | /* Validate pointer before using it */ |
| 24 | if (!bar || smm_points_to_smram(bar, LVTMA_BL_MOD_LEVEL + sizeof(uint8_t))) |
| 25 | return; |
| 26 | |
| 27 | printk(BIOS_DEBUG, "bar: %p, level %02X\n", bar, *(bar+LVTMA_BL_MOD_LEVEL)); |
| 28 | *(bar+LVTMA_BL_MOD_LEVEL) &= 0xf0; |
| 29 | if (*(bar+LVTMA_BL_MOD_LEVEL) > 0x10) |
| 30 | *(bar+LVTMA_BL_MOD_LEVEL) -= 0x10; |
Sven Schnelle | 61cd5bf | 2011-06-23 19:12:25 +0200 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | static void mainboard_smi_brightness_up(void) |
| 34 | { |
Patrick Rudolph | 9f8f115 | 2020-05-06 11:58:45 +0200 | [diff] [blame] | 35 | uint32_t reg32 = pci_read_config32(PCI_DEV(1, 0, 0), PCI_BASE_ADDRESS_2) & ~0xf; |
| 36 | u8 *bar = (void *)(uintptr_t)reg32; |
| 37 | |
| 38 | /* Validate pointer before using it */ |
| 39 | if (!bar || smm_points_to_smram(bar, LVTMA_BL_MOD_LEVEL + sizeof(uint8_t))) |
| 40 | return; |
| 41 | |
| 42 | printk(BIOS_DEBUG, "bar: %p, level %02X\n", bar, *(bar+LVTMA_BL_MOD_LEVEL)); |
| 43 | *(bar+LVTMA_BL_MOD_LEVEL) |= 0x0f; |
| 44 | if (*(bar+LVTMA_BL_MOD_LEVEL) < 0xf0) |
| 45 | *(bar+LVTMA_BL_MOD_LEVEL) += 0x10; |
Sven Schnelle | d8129f9 | 2011-04-20 09:12:17 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Kyösti Mälkki | 791f7a4 | 2022-12-02 17:59:35 +0200 | [diff] [blame] | 48 | static void mainboard_smi_dock_connect(void) |
| 49 | { |
| 50 | /* If there's an legacy I/O module present, we're not |
| 51 | * allowed to connect the Docking LPC Bus, as both Super I/O |
| 52 | * chips are using 0x2e as base address. |
| 53 | */ |
| 54 | if (legacy_io_present()) |
| 55 | return; |
| 56 | |
| 57 | if (!dock_connect()) { |
| 58 | /* set dock LED to indicate status */ |
| 59 | ec_write(0x0c, 0x08); |
| 60 | ec_write(0x0c, 0x89); |
| 61 | } else { |
| 62 | /* blink dock LED to indicate failure */ |
| 63 | ec_write(0x0c, 0xc8); |
| 64 | ec_write(0x0c, 0x09); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | static void mainboard_smi_dock_disconnect(void) |
| 69 | { |
| 70 | dock_disconnect(); |
| 71 | ec_write(0x0c, 0x09); |
| 72 | ec_write(0x0c, 0x08); |
| 73 | } |
| 74 | |
Sven Schnelle | d8129f9 | 2011-04-20 09:12:17 +0000 | [diff] [blame] | 75 | int mainboard_io_trap_handler(int smif) |
| 76 | { |
Sven Schnelle | d8129f9 | 2011-04-20 09:12:17 +0000 | [diff] [blame] | 77 | switch (smif) { |
| 78 | case SMI_DOCK_CONNECT: |
Kyösti Mälkki | 791f7a4 | 2022-12-02 17:59:35 +0200 | [diff] [blame] | 79 | mainboard_smi_dock_connect(); |
Sven Schnelle | d8129f9 | 2011-04-20 09:12:17 +0000 | [diff] [blame] | 80 | break; |
| 81 | |
| 82 | case SMI_DOCK_DISCONNECT: |
Kyösti Mälkki | 791f7a4 | 2022-12-02 17:59:35 +0200 | [diff] [blame] | 83 | mainboard_smi_dock_disconnect(); |
Sven Schnelle | d8129f9 | 2011-04-20 09:12:17 +0000 | [diff] [blame] | 84 | break; |
| 85 | |
Sven Schnelle | 61cd5bf | 2011-06-23 19:12:25 +0200 | [diff] [blame] | 86 | case SMI_BRIGHTNESS_UP: |
| 87 | mainboard_smi_brightness_up(); |
| 88 | break; |
| 89 | |
| 90 | case SMI_BRIGHTNESS_DOWN: |
| 91 | mainboard_smi_brightness_down(); |
| 92 | break; |
| 93 | |
Sven Schnelle | d8129f9 | 2011-04-20 09:12:17 +0000 | [diff] [blame] | 94 | default: |
Sven Schnelle | 0f9cd43 | 2011-06-12 16:55:56 +0200 | [diff] [blame] | 95 | return 0; |
Sven Schnelle | d8129f9 | 2011-04-20 09:12:17 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Sven Schnelle | 0f9cd43 | 2011-06-12 16:55:56 +0200 | [diff] [blame] | 98 | /* On success, the IO Trap Handler returns 1 |
| 99 | * On failure, the IO Trap Handler returns a value != 1 */ |
| 100 | return 1; |
Sven Schnelle | d8129f9 | 2011-04-20 09:12:17 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Sven Schnelle | 61cd5bf | 2011-06-23 19:12:25 +0200 | [diff] [blame] | 103 | static void mainboard_smi_handle_ec_sci(void) |
| 104 | { |
| 105 | u8 status = inb(EC_SC); |
| 106 | u8 event; |
| 107 | |
| 108 | if (!(status & EC_SCI_EVT)) |
| 109 | return; |
| 110 | |
| 111 | event = ec_query(); |
Paul Menzel | 5664293 | 2020-07-19 09:42:53 +0200 | [diff] [blame] | 112 | printk(BIOS_DEBUG, "EC event %#02x\n", event); |
Sven Schnelle | 61cd5bf | 2011-06-23 19:12:25 +0200 | [diff] [blame] | 113 | |
Elyes HAOUAS | 0ce41f1 | 2018-11-13 10:03:31 +0100 | [diff] [blame] | 114 | switch (event) { |
Felix Singer | df98b81 | 2023-12-08 10:52:34 +0100 | [diff] [blame^] | 115 | /* brightness up */ |
| 116 | case 0x14: |
| 117 | mainboard_smi_brightness_up(); |
| 118 | break; |
| 119 | /* brightness down */ |
| 120 | case 0x15: |
| 121 | mainboard_smi_brightness_down(); |
| 122 | break; |
| 123 | /* Fn-F9 Key */ |
| 124 | case 0x18: |
| 125 | /* power loss */ |
| 126 | case 0x27: |
| 127 | /* undock event */ |
| 128 | case 0x50: |
| 129 | mainboard_smi_dock_disconnect(); |
| 130 | break; |
| 131 | /* dock event */ |
| 132 | case 0x37: |
| 133 | mainboard_smi_dock_connect(); |
| 134 | break; |
| 135 | default: |
| 136 | break; |
Sven Schnelle | 61cd5bf | 2011-06-23 19:12:25 +0200 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
Duncan Laurie | 0edc224 | 2013-04-29 15:04:30 -0700 | [diff] [blame] | 140 | void mainboard_smi_gpi(u32 gpi) |
Sven Schnelle | 61cd5bf | 2011-06-23 19:12:25 +0200 | [diff] [blame] | 141 | { |
Kyösti Mälkki | 189f3ba | 2014-12-29 11:32:27 +0200 | [diff] [blame] | 142 | if (gpi & (1 << GPE_EC_SCI)) |
Sven Schnelle | 61cd5bf | 2011-06-23 19:12:25 +0200 | [diff] [blame] | 143 | mainboard_smi_handle_ec_sci(); |
| 144 | } |
| 145 | |
Stefan Reinauer | bf34e94 | 2012-04-27 00:44:04 +0200 | [diff] [blame] | 146 | int mainboard_smi_apmc(u8 data) |
Sven Schnelle | 61cd5bf | 2011-06-23 19:12:25 +0200 | [diff] [blame] | 147 | { |
Elyes HAOUAS | 0ce41f1 | 2018-11-13 10:03:31 +0100 | [diff] [blame] | 148 | switch (data) { |
Felix Singer | df98b81 | 2023-12-08 10:52:34 +0100 | [diff] [blame^] | 149 | case APM_CNT_ACPI_ENABLE: |
| 150 | /* use 0x1600/0x1604 to prevent races with userspace */ |
| 151 | ec_set_ports(0x1604, 0x1600); |
| 152 | /* route H8SCI to SCI */ |
| 153 | gpi_route_interrupt(GPE_EC_SCI, GPI_IS_SCI); |
| 154 | break; |
| 155 | case APM_CNT_ACPI_DISABLE: |
| 156 | /* we have to use port 0x62/0x66, as 0x1600/0x1604 doesn't |
| 157 | provide a EC query function */ |
| 158 | ec_set_ports(0x66, 0x62); |
| 159 | /* route H8SCI# to SMI */ |
| 160 | gpi_route_interrupt(GPE_EC_SCI, GPI_IS_SMI); |
| 161 | break; |
| 162 | default: |
| 163 | break; |
Sven Schnelle | 61cd5bf | 2011-06-23 19:12:25 +0200 | [diff] [blame] | 164 | } |
| 165 | return 0; |
| 166 | } |