blob: 763ac2600c859d36dbc6fa1b3652b3538dd0f57e [file] [log] [blame]
Vladimir Serbinenko3129f792014-10-15 21:51:47 +02001package main
2
3import "fmt"
4
5func LenovoEC(ctx Context) {
6 ap := Create(ctx, "acpi/platform.asl")
7 defer ap.Close()
8
9 wakeGPE := 13
10
11 sbGPE := GuessECGPE(ctx)
12 var GPE int
13 var GPEUnsure bool
14 if sbGPE < 0 {
15 sbGPE = SouthBridge.EncodeGPE(1)
16 GPE = 1
17 GPEUnsure = true
18 SouthBridge.NeedRouteGPIOManually()
19 } else {
20 GPE = SouthBridge.DecodeGPE(sbGPE)
21 GPEUnsure = false
22 }
23
24 SouthBridge.EnableGPE(wakeGPE)
25 SouthBridge.EnableGPE(GPE)
26
27 GPEDefine := DSDTDefine{
28 Key: "THINKPAD_EC_GPE",
29 }
30
31 GPEDefine.Value = fmt.Sprintf("%d", sbGPE)
32 if GPEUnsure {
33 GPEDefine.Comment = "FIXME: Check this"
34 }
35
36 DSDTDefines = append(DSDTDefines,
37 DSDTDefine{
38 Key: "EC_LENOVO_H8_ME_WORKAROUND",
39 Value: "1",
40 }, GPEDefine)
41
Iru Cai16f213a2020-09-16 16:23:46 +080042 Add_gpl(ap)
Vladimir Serbinenko3129f792014-10-15 21:51:47 +020043 ap.WriteString(
Paul Menzel56258ff2020-01-15 14:28:58 +010044 `Method(_WAK, 1)
Vladimir Serbinenko3129f792014-10-15 21:51:47 +020045{
Peter Lemenkov71a7ca72020-01-22 11:48:55 +010046 /* ME may not be up yet. */
Paul Menzel56258ff2020-01-15 14:28:58 +010047 Store(0, \_TZ.MEB1)
48 Store(0, \_TZ.MEB2)
49 Return(Package() {0, 0})
Vladimir Serbinenko3129f792014-10-15 21:51:47 +020050}
51
52Method(_PTS,1)
53{
54 \_SB.PCI0.LPCB.EC.RADI(0)
55}
56`)
57
58 si := Create(ctx, "acpi/superio.asl")
59 defer si.Close()
60
Iru Cai16f213a2020-09-16 16:23:46 +080061 Add_gpl(si)
Stefan Reinauer86ddd732016-03-11 20:22:28 -080062 si.WriteString("#include <drivers/pc80/pc/ps2_controller.asl>\n")
Vladimir Serbinenko3129f792014-10-15 21:51:47 +020063
Vladimir Serbinenko3129f792014-10-15 21:51:47 +020064 /* FIXME:XX Move this to ec/lenovo. */
65 smi := Create(ctx, "smihandler.c")
66 defer smi.Close()
67
68 AddSMMFile("smihandler.c", "")
69
Arthur Heymans59302852017-05-01 10:33:56 +020070 Add_gpl(smi)
Vladimir Serbinenko3129f792014-10-15 21:51:47 +020071 smi.WriteString(
Arthur Heymans59302852017-05-01 10:33:56 +020072 `#include <arch/io.h>
Vladimir Serbinenko3129f792014-10-15 21:51:47 +020073#include <console/console.h>
74#include <cpu/x86/smm.h>
75#include <ec/acpi/ec.h>
76#include <ec/lenovo/h8/h8.h>
77#include <delay.h>
78#include <` + SouthBridge.GetGPIOHeader() + ">\n\n")
79
80 if GPEUnsure {
81 smi.WriteString("/* FIXME: check this */\n")
82 }
83 fmt.Fprintf(smi, "#define GPE_EC_SCI %d\n", GPE)
84
85 smi.WriteString("/* FIXME: check this */\n")
86 fmt.Fprintf(smi, "#define GPE_EC_WAKE %d\n", wakeGPE)
87
88 smi.WriteString(`
89static void mainboard_smm_init(void)
90{
91 printk(BIOS_DEBUG, "initializing SMI\n");
92 /* Enable 0x1600/0x1600 register pair */
93 ec_set_bit(0x00, 0x05);
94}
95
96int mainboard_io_trap_handler(int smif)
97{
98 static int smm_initialized;
99
100 if (!smm_initialized) {
101 mainboard_smm_init();
102 smm_initialized = 1;
103 }
104
105 return 0;
106}
107
108static void mainboard_smi_handle_ec_sci(void)
109{
110 u8 status = inb(EC_SC);
111 u8 event;
112
113 if (!(status & EC_SCI_EVT))
114 return;
115
116 event = ec_query();
Paul Menzel56642932020-07-19 09:42:53 +0200117 printk(BIOS_DEBUG, "EC event %#02x\n", event);
Vladimir Serbinenko3129f792014-10-15 21:51:47 +0200118}
119
120void mainboard_smi_gpi(u32 gpi_sts)
121{
122 if (gpi_sts & (1 << GPE_EC_SCI))
123 mainboard_smi_handle_ec_sci();
124}
125
126int mainboard_smi_apmc(u8 data)
127{
128 switch (data) {
129 case APM_CNT_ACPI_ENABLE:
130 /* use 0x1600/0x1604 to prevent races with userspace */
131 ec_set_ports(0x1604, 0x1600);
132 /* route EC_SCI to SCI */
133 gpi_route_interrupt(GPE_EC_SCI, GPI_IS_SCI);
134 /* discard all events, and enable attention */
135 ec_write(0x80, 0x01);
136 break;
137 case APM_CNT_ACPI_DISABLE:
138 /* we have to use port 0x62/0x66, as 0x1600/0x1604 doesn't
139 provide a EC query function */
140 ec_set_ports(0x66, 0x62);
141 /* route EC_SCI to SMI */
142 gpi_route_interrupt(GPE_EC_SCI, GPI_IS_SMI);
143 /* discard all events, and enable attention */
144 ec_write(0x80, 0x01);
145 break;
146 default:
147 break;
148 }
149 return 0;
150}
151
152void mainboard_smi_sleep(u8 slp_typ)
153{
154 if (slp_typ == 3) {
155 u8 ec_wake = ec_read(0x32);
Peter Lemenkov71a7ca72020-01-22 11:48:55 +0100156 /* If EC wake events are enabled, enable wake on EC WAKE GPE. */
Vladimir Serbinenko3129f792014-10-15 21:51:47 +0200157 if (ec_wake & 0x14) {
Peter Lemenkov71a7ca72020-01-22 11:48:55 +0100158 /* Redirect EC WAKE GPE to SCI. */
Vladimir Serbinenko3129f792014-10-15 21:51:47 +0200159 gpi_route_interrupt(GPE_EC_WAKE, GPI_IS_SCI);
160 }
161 }
162}
163`)
164
165 ec := Create(ctx, "acpi/ec.asl")
166 defer ec.Close()
167
Iru Cai16f213a2020-09-16 16:23:46 +0800168 Add_gpl(ec)
Vladimir Serbinenko3129f792014-10-15 21:51:47 +0200169 ec.WriteString("#include <ec/lenovo/h8/acpi/ec.asl>\n")
170
171 KconfigBool["EC_LENOVO_PMH7"] = true
172 KconfigBool["EC_LENOVO_H8"] = true
173
174 pmh := DevTreeNode{
175 Chip: "ec/lenovo/pmh7",
176 Registers: map[string]string{
177 "backlight_enable": "0x01",
178 "dock_event_enable": "0x01",
179 },
180 Children: []DevTreeNode{
181 DevTreeNode{
182 Chip: "pnp",
183 Comment: "dummy",
184 Dev: 0xff,
185 Func: 1,
186 },
187 },
188 }
189 PutChip("lpc", pmh)
190
191 ecs := ctx.InfoSource.GetEC()
192 h8 := DevTreeNode{
193 Chip: "ec/lenovo/h8",
194 Children: []DevTreeNode{
195 DevTreeNode{
196 Chip: "pnp",
197 Comment: "dummy",
198 Dev: 0xff,
199 Func: 2,
200 IOs: map[uint16]uint16{
201 0x60: 0x62,
202 0x62: 0x66,
203 0x64: 0x1600,
204 0x66: 0x1604,
205 },
206 },
207 },
208 Comment: "FIXME: has_keyboard_backlight, has_power_management_beeps, has_uwb",
209 Registers: map[string]string{
210 "config0": FormatHex8(ecs[0]),
211 "config1": FormatHex8(ecs[1]),
212 "config2": FormatHex8(ecs[2]),
213 "config3": FormatHex8(ecs[3]),
214 "beepmask0": FormatHex8(ecs[4]),
215 "beepmask1": FormatHex8(ecs[5]),
216 },
217 }
218 for i := 0; i < 0x10; i++ {
219 if ecs[0x10+i] != 0 {
220 h8.Registers[fmt.Sprintf("event%x_enable", i)] = FormatHex8(ecs[0x10+i])
221 }
222 }
223 PutChip("lpc", h8)
224
225 eeprom := DevTreeNode{
226 Chip: "drivers/i2c/at24rf08c",
227 Comment: "eeprom, 8 virtual devices, same chip",
228 Children: []DevTreeNode{
229 DevTreeNode{
230 Chip: "i2c",
231 Dev: 0x54,
232 },
233 DevTreeNode{
234 Chip: "i2c",
235 Dev: 0x55,
236 },
237 DevTreeNode{
238 Chip: "i2c",
239 Dev: 0x56,
240 },
241 DevTreeNode{
242 Chip: "i2c",
243 Dev: 0x57,
244 },
245 DevTreeNode{
246 Chip: "i2c",
247 Dev: 0x5c,
248 },
249 DevTreeNode{
250 Chip: "i2c",
251 Dev: 0x5d,
252 },
253 DevTreeNode{
254 Chip: "i2c",
255 Dev: 0x5e,
256 },
257 DevTreeNode{
258 Chip: "i2c",
259 Dev: 0x5f,
260 },
261 },
262 }
263 PutChip("smbus", eeprom)
264}