X60: use EC events 0x50/0x58 instead of GPIO GPE for Docking/Undocking
[coreboot.git] / src / mainboard / lenovo / x60 / mainboard_smi.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2008-2009 coresystems GmbH
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; version 2 of
9  * the License.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
19  * MA 02110-1301 USA
20  */
21
22 #include <arch/io.h>
23 #include <arch/romcc_io.h>
24 #include <console/console.h>
25 #include <cpu/x86/smm.h>
26 #include "southbridge/intel/i82801gx/nvs.h"
27 #include "southbridge/intel/i82801gx/i82801gx.h"
28 #include <ec/acpi/ec.h>
29 #include <pc80/mc146818rtc.h>
30 #include <ec/lenovo/h8/h8.h>
31 #include "dock.h"
32 #include "smi.h"
33
34 /* The southbridge SMI handler checks whether gnvs has a
35  * valid pointer before calling the trap handler
36  */
37 extern global_nvs_t *gnvs;
38
39 static void mainboard_smm_init(void)
40 {
41         printk(BIOS_DEBUG, "initializing SMI\n");
42         /* Enable 0x1600/0x1600 register pair */
43         ec_set_bit(0x00, 0x05);
44 }
45
46 static void mainboard_smi_save_cmos(void)
47 {
48         u8 val;
49         u8 tmp70, tmp72;
50
51         tmp70 = inb(0x70);
52         tmp72 = inb(0x72);
53
54         val = pci_read_config8(PCI_DEV(0, 2, 1), 0xf4);
55         set_option("tft_brightness", &val);
56         val = ec_read(H8_VOLUME_CONTROL);
57         set_option("volume", &val);
58
59         outb(tmp70, 0x70);
60         outb(tmp72, 0x72);
61 }
62
63 int mainboard_io_trap_handler(int smif)
64 {
65         static int smm_initialized;
66
67         if (!smm_initialized) {
68                 mainboard_smm_init();
69                 smm_initialized = 1;
70         }
71
72         switch (smif) {
73         case SMI_DOCK_CONNECT:
74                 ec_clr_bit(0x03, 2);
75                 dlpc_init();
76                 if (!dlpc_init() && !dock_connect()) {
77                         ec_set_bit(0x03, 2);
78                         /* set dock LED to indicate status */
79                         ec_write(0x0c, 0x09);
80                         ec_write(0x0c, 0x88);
81                 } else {
82                         /* blink dock LED to indicate failure */
83                         ec_write(0x0c, 0x08);
84                         ec_write(0x0c, 0xc9);
85                 }
86                 break;
87
88         case SMI_DOCK_DISCONNECT:
89                 ec_clr_bit(0x03, 2);
90                 dock_disconnect();
91                 break;
92
93         case SMI_SAVE_CMOS:
94                 mainboard_smi_save_cmos();
95                 break;
96         default:
97                 return 0;
98         }
99
100         /* On success, the IO Trap Handler returns 1
101          * On failure, the IO Trap Handler returns a value != 1 */
102         return 1;
103 }
104
105 static void mainboard_smi_brightness_up(void)
106 {
107         u8 value;
108
109         if ((value = pci_read_config8(PCI_DEV(0, 2, 1), 0xf4)) < 0xf0)
110                 pci_write_config8(PCI_DEV(0, 2, 1), 0xf4, (value + 0x10) | 0xf);
111 }
112
113 static void mainboard_smi_brightness_down(void)
114 {
115         u8 value;
116
117         if ((value = pci_read_config8(PCI_DEV(0, 2, 1), 0xf4)) > 0x10)
118                 pci_write_config8(PCI_DEV(0, 2, 1), 0xf4, (value - 0x10) & 0xf0);
119 }
120
121 static void mainboard_smi_handle_ec_sci(void)
122 {
123         u8 status = inb(EC_SC);
124         u8 event;
125
126         if (!(status & EC_SCI_EVT))
127                 return;
128
129         event = ec_query();
130         printk(BIOS_DEBUG, "EC event %02x\n", event);
131
132         switch(event) {
133                 /* brightness up */
134                 case 0x14:
135                         mainboard_smi_brightness_up();
136                         mainboard_smi_save_cmos();
137                         break;
138                 /* brightness down */
139                 case 0x15:
140                         mainboard_smi_brightness_down();
141                         mainboard_smi_save_cmos();
142                         break;
143                         /* Fn-F9 key */
144                 case 0x18:
145                         /* Power loss */
146                 case 0x27:
147                         /* Undock Key */
148                 case 0x50:
149                         mainboard_io_trap_handler(SMI_DOCK_DISCONNECT);
150                         break;
151                         /* Dock Event */
152                 case 0x37:
153                 case 0x58:
154                         mainboard_io_trap_handler(SMI_DOCK_CONNECT);
155                         break;
156                 default:
157                         break;
158         }
159 }
160
161 void mainboard_smi_gpi(u16 gpi)
162 {
163         if (gpi & (1 << 12))
164                 mainboard_smi_handle_ec_sci();
165 }
166
167 int mainboard_apm_cnt(u8 data)
168 {
169         u16 pmbase = pci_read_config16(PCI_DEV(0, 0x1f, 0), 0x40) & 0xfffc;
170         u8 tmp;
171
172         printk(BIOS_DEBUG, "%s: pmbase %04X, data %02X\n", __func__, pmbase, data);
173
174         if (!pmbase)
175                 return 0;
176
177         switch(data) {
178                 case APM_CNT_ACPI_ENABLE:
179                         /* use 0x1600/0x1604 to prevent races with userspace */
180                         ec_set_ports(0x1604, 0x1600);
181                         /* route H8SCI to SCI */
182                         outw(inw(ALT_GP_SMI_EN) & ~0x1000, pmbase + ALT_GP_SMI_EN);
183                         tmp = pci_read_config8(PCI_DEV(0, 0x1f, 0), 0xbb);
184                         tmp &= ~0x03;
185                         tmp |= 0x02;
186                         pci_write_config8(PCI_DEV(0, 0x1f, 0), 0xbb, tmp);
187                         /* discard all events, and enable attention */
188                         ec_write(0x80, 0x01);
189                         break;
190                 case APM_CNT_ACPI_DISABLE:
191                         /* we have to use port 0x62/0x66, as 0x1600/0x1604 doesn't
192                            provide a EC query function */
193                         ec_set_ports(0x66, 0x62);
194                         /* route H8SCI# to SMI */
195                         outw(inw(pmbase + ALT_GP_SMI_EN) | 0x1000, pmbase + ALT_GP_SMI_EN);
196                         tmp = pci_read_config8(PCI_DEV(0, 0x1f, 0), 0xbb);
197                         tmp &= ~0x03;
198                         tmp |= 0x01;
199                         pci_write_config8(PCI_DEV(0, 0x1f, 0), 0xbb, tmp);
200                         /* discard all events, and enable attention */
201                         ec_write(0x80, 0x01);
202                         break;
203                 default:
204                         break;
205         }
206         return 0;
207 }