blob: 5ce5c6c648dea02d67856db69e1c358b04deeb1f [file] [log] [blame]
Stefan Reinauer7e568552013-03-13 17:03:04 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2013 Google Inc.
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
Edward O'Callaghanb57fef92014-06-17 20:13:08 +100022#include <arch/io.h>
Stefan Reinauer7e568552013-03-13 17:03:04 -070023#include <console/console.h>
Edward O'Callaghanb57fef92014-06-17 20:13:08 +100024#include <delay.h>
Stefan Reinauer7e568552013-03-13 17:03:04 -070025#include <device/device.h>
26#include <device/pnp.h>
Edward O'Callaghanb57fef92014-06-17 20:13:08 +100027#include <pc80/keyboard.h>
Stefan Reinauer7e568552013-03-13 17:03:04 -070028#include <stdlib.h>
Edward O'Callaghanb57fef92014-06-17 20:13:08 +100029
Stefan Reinauer7e568552013-03-13 17:03:04 -070030#include "ec.h"
31#include "chip.h"
32
33/* helper functions from drivers/pc80/keyboard.c */
34static int input_buffer_empty(u16 status_reg)
35{
36 u32 timeout;
37 for(timeout = KBC_TIMEOUT_IN_MS; timeout && (inb(status_reg) & KBD_IBF);
38 timeout--) {
Idwer Volleringd26da9c2013-12-22 21:38:18 +000039 udelay(1000);
Stefan Reinauer7e568552013-03-13 17:03:04 -070040 }
41
42 if (!timeout) {
43 printk(BIOS_WARNING, "EC-IT8518 Unexpected input buffer full\n");
44 printk(BIOS_WARNING, " Status (0x%x): 0x%x\n", status_reg, inb(status_reg));
45 }
46 return !!timeout;
47}
48
49
50static int output_buffer_full(u16 status_reg)
51{
52 u32 timeout;
53 for(timeout = KBC_TIMEOUT_IN_MS; timeout && ((inb(status_reg)
54 & KBD_OBF) == 0); timeout--) {
Idwer Volleringd26da9c2013-12-22 21:38:18 +000055 udelay(1000);
Stefan Reinauer7e568552013-03-13 17:03:04 -070056 }
57
58 if (!timeout) {
59 printk(BIOS_INFO, "EC-IT8518 output buffer result timeout\n");
60 printk(BIOS_INFO, " Status (0x%x): 0x%x\n", status_reg, inb(status_reg));
61 }
62 return !!timeout;
63}
64
65
66
67/* The IT8518 60/64 EC registers are the same command/status IB/OB KBC pair.
68 * Check status from 64 port before each command.
69 *
70 * Ex. Get panel ID command C43/D77
71 * Check IBF empty. Then Write 0x43(CMD) to 0x64 Port
72 * Check IBF empty. Then Write 0x77(DATA) to 0x60 Port
73 * Check OBF set. Then Get Data(0x03:panel ID) from 0x60
74 * Different commands return may or may not respond and may have multiple
75 * bytes. Keep it simple for nor
76 */
77
78u8 ec_kbc_read_ob(void)
79{
80 if (!output_buffer_full(KBD_STATUS)) return 0;
81 return inb(KBD_DATA);
82}
83
84void ec_kbc_write_cmd(u8 cmd)
85{
86 if (!input_buffer_empty(KBD_STATUS)) return;
87 outb(cmd, KBD_COMMAND);
88}
89
90void ec_kbc_write_ib(u8 data)
91{
92 if (!input_buffer_empty(KBD_STATUS)) return;
93 outb(data, KBD_DATA);
94}
95
96
97/*
98 * These functions are for accessing the IT8518 device RAM space via 0x66/0x68
99 */
100
101u8 ec_read_ob(void)
102{
103 if (!output_buffer_full(EC_SC)) return 0;
104 return inb(EC_DATA);
105}
106
107void ec_write_cmd(u8 cmd)
108{
109 if (!input_buffer_empty(EC_SC)) return;
110 outb(cmd, EC_SC);
111}
112
113void ec_write_ib(u8 data)
114{
115 if (!input_buffer_empty(EC_SC)) return;
116 outb(data, EC_DATA);
117}
118
119u8 ec_read(u16 addr)
120{
121 ec_write_cmd(RD_EC);
122 ec_write_ib(addr);
123 return ec_read_ob();
124}
125
126void ec_write(u16 addr, u8 data)
127{
128 ec_write_cmd(WR_EC);
129 ec_write_ib(addr);
130 ec_write_ib(data);
131}
132
133#ifndef __PRE_RAM__
134
135u8 ec_it8518_get_event(void)
136{
137 u8 cmd = 0;
138 u8 status = inb(EC_SC);
139 if (status & SCI_EVT) {
140 ec_write_cmd(QR_EC);
141 cmd = ec_read_ob();
142 } else if ( status & SMI_EVT) {
143 ec_kbc_write_cmd(EC_KBD_SMI_EVENT);
144 cmd = ec_kbc_read_ob();
145 }
146 return cmd;
147}
148
149void ec_it8518_enable_wake_events(void)
150{
151 /*
152 * Set the bit in ECRAM that will enable the Lid switch as a wake source
153 */
154 u8 reg8 = ec_read(EC_WAKE_SRC_ENABLE);
155 ec_write(EC_WAKE_SRC_ENABLE, reg8 | EC_LID_WAKE_ENABLE);
156}
157
158#ifndef __SMM__
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100159static void it8518_init(struct device *dev)
Stefan Reinauer7e568552013-03-13 17:03:04 -0700160{
Stefan Reinauer7e568552013-03-13 17:03:04 -0700161 if (!dev->enabled)
162 return;
163
164 printk(BIOS_DEBUG, "Quanta IT8518: Initializing keyboard.\n");
Edward O'Callaghandef00be2014-04-30 05:01:52 +1000165 pc_keyboard_init();
Stefan Reinauer7e568552013-03-13 17:03:04 -0700166}
167
Stefan Reinauer7e568552013-03-13 17:03:04 -0700168static struct device_operations ops = {
169 .init = it8518_init,
Edward O'Callaghan5f19eb62014-11-29 00:03:03 +1100170 .read_resources = DEVICE_NOOP,
171 .enable_resources = DEVICE_NOOP,
Stefan Reinauer7e568552013-03-13 17:03:04 -0700172};
173
174static struct pnp_info pnp_dev_info[] = {
175 { &ops, 0, 0, { 0, 0 }, }
176};
177
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100178static void enable_dev(struct device *dev)
Stefan Reinauer7e568552013-03-13 17:03:04 -0700179{
180 pnp_enable_devices(dev, &pnp_ops, ARRAY_SIZE(pnp_dev_info),
181 pnp_dev_info);
182}
183
184struct chip_operations ec_quanta_it8518_ops = {
185 CHIP_NAME("QUANTA IT8518 EC")
186 .enable_dev = enable_dev
187};
188#endif /* ! __SMM__ */
189#endif /* ! __PRE_RAM__ */