blob: e293f7cb6ade4a5d46412e6e1dfbd984166b190e [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.
Stefan Reinauer7e568552013-03-13 17:03:04 -070015 */
16
Edward O'Callaghanb57fef92014-06-17 20:13:08 +100017#include <arch/io.h>
Stefan Reinauer7e568552013-03-13 17:03:04 -070018#include <console/console.h>
Edward O'Callaghanb57fef92014-06-17 20:13:08 +100019#include <delay.h>
Stefan Reinauer7e568552013-03-13 17:03:04 -070020#include <device/device.h>
21#include <device/pnp.h>
Edward O'Callaghanb57fef92014-06-17 20:13:08 +100022#include <pc80/keyboard.h>
Stefan Reinauer7e568552013-03-13 17:03:04 -070023#include <stdlib.h>
Edward O'Callaghanb57fef92014-06-17 20:13:08 +100024
Stefan Reinauer7e568552013-03-13 17:03:04 -070025#include "ec.h"
26#include "chip.h"
27
28/* helper functions from drivers/pc80/keyboard.c */
29static int input_buffer_empty(u16 status_reg)
30{
31 u32 timeout;
Elyes HAOUAS2b010b82016-08-25 20:57:08 +020032 for (timeout = KBC_TIMEOUT_IN_MS; timeout && (inb(status_reg) & KBD_IBF);
Stefan Reinauer7e568552013-03-13 17:03:04 -070033 timeout--) {
Idwer Volleringd26da9c2013-12-22 21:38:18 +000034 udelay(1000);
Stefan Reinauer7e568552013-03-13 17:03:04 -070035 }
36
37 if (!timeout) {
38 printk(BIOS_WARNING, "EC-IT8518 Unexpected input buffer full\n");
39 printk(BIOS_WARNING, " Status (0x%x): 0x%x\n", status_reg, inb(status_reg));
40 }
41 return !!timeout;
42}
43
44
45static int output_buffer_full(u16 status_reg)
46{
47 u32 timeout;
Elyes HAOUAS2b010b82016-08-25 20:57:08 +020048 for (timeout = KBC_TIMEOUT_IN_MS; timeout && ((inb(status_reg)
Stefan Reinauer7e568552013-03-13 17:03:04 -070049 & KBD_OBF) == 0); timeout--) {
Idwer Volleringd26da9c2013-12-22 21:38:18 +000050 udelay(1000);
Stefan Reinauer7e568552013-03-13 17:03:04 -070051 }
52
53 if (!timeout) {
54 printk(BIOS_INFO, "EC-IT8518 output buffer result timeout\n");
55 printk(BIOS_INFO, " Status (0x%x): 0x%x\n", status_reg, inb(status_reg));
56 }
57 return !!timeout;
58}
59
60
61
62/* The IT8518 60/64 EC registers are the same command/status IB/OB KBC pair.
63 * Check status from 64 port before each command.
64 *
65 * Ex. Get panel ID command C43/D77
66 * Check IBF empty. Then Write 0x43(CMD) to 0x64 Port
67 * Check IBF empty. Then Write 0x77(DATA) to 0x60 Port
68 * Check OBF set. Then Get Data(0x03:panel ID) from 0x60
69 * Different commands return may or may not respond and may have multiple
70 * bytes. Keep it simple for nor
71 */
72
73u8 ec_kbc_read_ob(void)
74{
75 if (!output_buffer_full(KBD_STATUS)) return 0;
76 return inb(KBD_DATA);
77}
78
79void ec_kbc_write_cmd(u8 cmd)
80{
81 if (!input_buffer_empty(KBD_STATUS)) return;
82 outb(cmd, KBD_COMMAND);
83}
84
85void ec_kbc_write_ib(u8 data)
86{
87 if (!input_buffer_empty(KBD_STATUS)) return;
88 outb(data, KBD_DATA);
89}
90
91
92/*
93 * These functions are for accessing the IT8518 device RAM space via 0x66/0x68
94 */
95
96u8 ec_read_ob(void)
97{
98 if (!output_buffer_full(EC_SC)) return 0;
99 return inb(EC_DATA);
100}
101
102void ec_write_cmd(u8 cmd)
103{
104 if (!input_buffer_empty(EC_SC)) return;
105 outb(cmd, EC_SC);
106}
107
108void ec_write_ib(u8 data)
109{
110 if (!input_buffer_empty(EC_SC)) return;
111 outb(data, EC_DATA);
112}
113
114u8 ec_read(u16 addr)
115{
116 ec_write_cmd(RD_EC);
117 ec_write_ib(addr);
118 return ec_read_ob();
119}
120
121void ec_write(u16 addr, u8 data)
122{
123 ec_write_cmd(WR_EC);
124 ec_write_ib(addr);
125 ec_write_ib(data);
126}
127
128#ifndef __PRE_RAM__
129
130u8 ec_it8518_get_event(void)
131{
132 u8 cmd = 0;
133 u8 status = inb(EC_SC);
Elyes HAOUASb9331092016-09-05 19:55:34 +0200134 if (status & SCI_EVT) {
Stefan Reinauer7e568552013-03-13 17:03:04 -0700135 ec_write_cmd(QR_EC);
136 cmd = ec_read_ob();
Elyes HAOUASb9331092016-09-05 19:55:34 +0200137 } else if (status & SMI_EVT) {
Stefan Reinauer7e568552013-03-13 17:03:04 -0700138 ec_kbc_write_cmd(EC_KBD_SMI_EVENT);
139 cmd = ec_kbc_read_ob();
140 }
141 return cmd;
142}
143
144void ec_it8518_enable_wake_events(void)
145{
146 /*
147 * Set the bit in ECRAM that will enable the Lid switch as a wake source
148 */
149 u8 reg8 = ec_read(EC_WAKE_SRC_ENABLE);
150 ec_write(EC_WAKE_SRC_ENABLE, reg8 | EC_LID_WAKE_ENABLE);
151}
152
153#ifndef __SMM__
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100154static void it8518_init(struct device *dev)
Stefan Reinauer7e568552013-03-13 17:03:04 -0700155{
Stefan Reinauer7e568552013-03-13 17:03:04 -0700156 if (!dev->enabled)
157 return;
158
159 printk(BIOS_DEBUG, "Quanta IT8518: Initializing keyboard.\n");
Timothy Pearson448e3862015-11-24 14:12:01 -0600160 pc_keyboard_init(NO_AUX_DEVICE);
Stefan Reinauer7e568552013-03-13 17:03:04 -0700161}
162
Stefan Reinauer7e568552013-03-13 17:03:04 -0700163static struct device_operations ops = {
164 .init = it8518_init,
Edward O'Callaghan5f19eb62014-11-29 00:03:03 +1100165 .read_resources = DEVICE_NOOP,
166 .enable_resources = DEVICE_NOOP,
Stefan Reinauer7e568552013-03-13 17:03:04 -0700167};
168
169static struct pnp_info pnp_dev_info[] = {
Felix Held1d9199c2018-07-07 00:27:44 +0200170 { NULL, 0, 0, 0, }
Stefan Reinauer7e568552013-03-13 17:03:04 -0700171};
172
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100173static void enable_dev(struct device *dev)
Stefan Reinauer7e568552013-03-13 17:03:04 -0700174{
Felix Held1d9199c2018-07-07 00:27:44 +0200175 pnp_enable_devices(dev, &ops, ARRAY_SIZE(pnp_dev_info), pnp_dev_info);
Stefan Reinauer7e568552013-03-13 17:03:04 -0700176}
177
178struct chip_operations ec_quanta_it8518_ops = {
179 CHIP_NAME("QUANTA IT8518 EC")
180 .enable_dev = enable_dev
181};
182#endif /* ! __SMM__ */
183#endif /* ! __PRE_RAM__ */