blob: 24722a033174dbc039bf88d0fbc1f34ff8d43242 [file] [log] [blame]
Zaolina823f9b2014-05-06 21:31:45 +02001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2008-2009 coresystems GmbH
5 * Copyright (C) 2014 Vladimir Serbinenko
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; version 2 of
10 * the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Zaolina823f9b2014-05-06 21:31:45 +020016 */
17
18#include <arch/io.h>
19#include <console/console.h>
20#include <cpu/x86/smm.h>
21#include <ec/acpi/ec.h>
22#include <pc80/mc146818rtc.h>
23#include <ec/lenovo/h8/h8.h>
24#include <delay.h>
25#include <southbridge/intel/bd82x6x/nvs.h>
26#include <southbridge/intel/bd82x6x/pch.h>
27#include <southbridge/intel/bd82x6x/me.h>
28#include <northbridge/intel/sandybridge/sandybridge.h>
29#include <cpu/intel/model_206ax/model_206ax.h>
30
Nicolas Reineckeb0922f02015-02-01 02:53:35 +010031#define GPE_EC_SCI 1
32#define GPE_EC_WAKE 13
33
Zaolina823f9b2014-05-06 21:31:45 +020034static void mainboard_smm_init(void)
35{
36 printk(BIOS_DEBUG, "initializing SMI\n");
37 /* Enable 0x1600/0x1600 register pair */
38 ec_set_bit(0x00, 0x05);
39}
40
41int mainboard_io_trap_handler(int smif)
42{
43 static int smm_initialized;
44
45 if (!smm_initialized) {
46 mainboard_smm_init();
47 smm_initialized = 1;
48 }
49
50 /* On success, the IO Trap Handler returns 1
51 * On failure, the IO Trap Handler returns a value != 1 */
52 return 0;
53}
54
55static void mainboard_smi_brightness_up(void)
56{
57 u8 value;
58
59 if ((value = pci_read_config8(PCI_DEV(0, 2, 1), 0xf4)) < 0xf0)
60 pci_write_config8(PCI_DEV(0, 2, 1), 0xf4, (value + 0x10) | 0xf);
61}
62
63static void mainboard_smi_brightness_down(void)
64{
65 u8 value;
66
67 if ((value = pci_read_config8(PCI_DEV(0, 2, 1), 0xf4)) > 0x10)
68 pci_write_config8(PCI_DEV(0, 2, 1), 0xf4,
69 (value - 0x10) & 0xf0);
70}
71
72static void mainboard_smi_handle_ec_sci(void)
73{
74 u8 status = inb(EC_SC);
75 u8 event;
76
77 if (!(status & EC_SCI_EVT))
78 return;
79
80 event = ec_query();
81 printk(BIOS_DEBUG, "EC event %02x\n", event);
82
83 switch (event) {
84 case 0x14:
85 /* brightness up */
86 mainboard_smi_brightness_up();
87 break;
88 case 0x15:
89 /* brightness down */
90 mainboard_smi_brightness_down();
91 break;
92 default:
93 break;
94 }
95}
96
97void mainboard_smi_gpi(u32 gpi_sts)
98{
Nicolas Reineckeb0922f02015-02-01 02:53:35 +010099 if (gpi_sts & (1 << GPE_EC_SCI))
Zaolina823f9b2014-05-06 21:31:45 +0200100 mainboard_smi_handle_ec_sci();
101}
102
Zaolina823f9b2014-05-06 21:31:45 +0200103int mainboard_smi_apmc(u8 data)
104{
Zaolina823f9b2014-05-06 21:31:45 +0200105 switch (data) {
106 case APM_CNT_ACPI_ENABLE:
107 /* use 0x1600/0x1604 to prevent races with userspace */
108 ec_set_ports(0x1604, 0x1600);
Nicolas Reineckeb0922f02015-02-01 02:53:35 +0100109 /* route EC_SCI to SCI */
Kyösti Mälkkib85a87b2014-12-29 11:32:27 +0200110 gpi_route_interrupt(GPE_EC_SCI, GPI_IS_SCI);
Zaolina823f9b2014-05-06 21:31:45 +0200111 /* discard all events, and enable attention */
112 ec_write(0x80, 0x01);
113 break;
114 case APM_CNT_ACPI_DISABLE:
115 /* we have to use port 0x62/0x66, as 0x1600/0x1604 doesn't
116 provide a EC query function */
117 ec_set_ports(0x66, 0x62);
Kyösti Mälkkib85a87b2014-12-29 11:32:27 +0200118 /* route EC_SCI to SMI */
119 gpi_route_interrupt(GPE_EC_SCI, GPI_IS_SMI);
Zaolina823f9b2014-05-06 21:31:45 +0200120 /* discard all events, and enable attention */
121 ec_write(0x80, 0x01);
122 break;
Zaolina823f9b2014-05-06 21:31:45 +0200123 default:
124 break;
125 }
126 return 0;
127}
Nicolas Reinecke9ebb3f52014-10-17 15:06:29 +0200128
129void mainboard_smi_sleep(u8 slp_typ)
130{
131 if (slp_typ == 3) {
132 u8 ec_wake = ec_read(0x32);
133 /* If EC wake events are enabled, enable wake on EC WAKE GPE. */
134 if (ec_wake & 0x14) {
Nicolas Reinecke9ebb3f52014-10-17 15:06:29 +0200135 /* Redirect EC WAKE GPE to SCI. */
Kyösti Mälkkib85a87b2014-12-29 11:32:27 +0200136 gpi_route_interrupt(GPE_EC_WAKE, GPI_IS_SCI);
Nicolas Reinecke9ebb3f52014-10-17 15:06:29 +0200137 }
138 }
139}