blob: bd82ecc5fe26f882667746659cae3853d6245f45 [file] [log] [blame]
Angel Pons210a0082020-04-02 23:48:24 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Stefan Reinauer7e568552013-03-13 17:03:04 -07003
Edward O'Callaghanb57fef92014-06-17 20:13:08 +10004#include <arch/io.h>
Stefan Reinauer7e568552013-03-13 17:03:04 -07005#include <console/console.h>
Edward O'Callaghanb57fef92014-06-17 20:13:08 +10006#include <delay.h>
Stefan Reinauer7e568552013-03-13 17:03:04 -07007#include <device/device.h>
8#include <device/pnp.h>
Edward O'Callaghanb57fef92014-06-17 20:13:08 +10009#include <pc80/keyboard.h>
Edward O'Callaghanb57fef92014-06-17 20:13:08 +100010
Stefan Reinauer7e568552013-03-13 17:03:04 -070011#include "ec.h"
12#include "chip.h"
13
14/* helper functions from drivers/pc80/keyboard.c */
15static int input_buffer_empty(u16 status_reg)
16{
17 u32 timeout;
Elyes HAOUAS2b010b82016-08-25 20:57:08 +020018 for (timeout = KBC_TIMEOUT_IN_MS; timeout && (inb(status_reg) & KBD_IBF);
Stefan Reinauer7e568552013-03-13 17:03:04 -070019 timeout--) {
Idwer Volleringd26da9c2013-12-22 21:38:18 +000020 udelay(1000);
Stefan Reinauer7e568552013-03-13 17:03:04 -070021 }
22
23 if (!timeout) {
24 printk(BIOS_WARNING, "EC-IT8518 Unexpected input buffer full\n");
25 printk(BIOS_WARNING, " Status (0x%x): 0x%x\n", status_reg, inb(status_reg));
26 }
27 return !!timeout;
28}
29
30
31static int output_buffer_full(u16 status_reg)
32{
33 u32 timeout;
Elyes HAOUAS2b010b82016-08-25 20:57:08 +020034 for (timeout = KBC_TIMEOUT_IN_MS; timeout && ((inb(status_reg)
Stefan Reinauer7e568552013-03-13 17:03:04 -070035 & KBD_OBF) == 0); timeout--) {
Idwer Volleringd26da9c2013-12-22 21:38:18 +000036 udelay(1000);
Stefan Reinauer7e568552013-03-13 17:03:04 -070037 }
38
39 if (!timeout) {
40 printk(BIOS_INFO, "EC-IT8518 output buffer result timeout\n");
41 printk(BIOS_INFO, " Status (0x%x): 0x%x\n", status_reg, inb(status_reg));
42 }
43 return !!timeout;
44}
45
46
47
48/* The IT8518 60/64 EC registers are the same command/status IB/OB KBC pair.
49 * Check status from 64 port before each command.
50 *
51 * Ex. Get panel ID command C43/D77
52 * Check IBF empty. Then Write 0x43(CMD) to 0x64 Port
53 * Check IBF empty. Then Write 0x77(DATA) to 0x60 Port
54 * Check OBF set. Then Get Data(0x03:panel ID) from 0x60
55 * Different commands return may or may not respond and may have multiple
56 * bytes. Keep it simple for nor
57 */
58
59u8 ec_kbc_read_ob(void)
60{
61 if (!output_buffer_full(KBD_STATUS)) return 0;
62 return inb(KBD_DATA);
63}
64
65void ec_kbc_write_cmd(u8 cmd)
66{
67 if (!input_buffer_empty(KBD_STATUS)) return;
68 outb(cmd, KBD_COMMAND);
69}
70
71void ec_kbc_write_ib(u8 data)
72{
73 if (!input_buffer_empty(KBD_STATUS)) return;
74 outb(data, KBD_DATA);
75}
76
77
78/*
79 * These functions are for accessing the IT8518 device RAM space via 0x66/0x68
80 */
81
82u8 ec_read_ob(void)
83{
84 if (!output_buffer_full(EC_SC)) return 0;
85 return inb(EC_DATA);
86}
87
88void ec_write_cmd(u8 cmd)
89{
90 if (!input_buffer_empty(EC_SC)) return;
91 outb(cmd, EC_SC);
92}
93
94void ec_write_ib(u8 data)
95{
96 if (!input_buffer_empty(EC_SC)) return;
97 outb(data, EC_DATA);
98}
99
100u8 ec_read(u16 addr)
101{
102 ec_write_cmd(RD_EC);
103 ec_write_ib(addr);
104 return ec_read_ob();
105}
106
107void ec_write(u16 addr, u8 data)
108{
109 ec_write_cmd(WR_EC);
110 ec_write_ib(addr);
111 ec_write_ib(data);
112}
113
Stefan Reinauer7e568552013-03-13 17:03:04 -0700114
115u8 ec_it8518_get_event(void)
116{
117 u8 cmd = 0;
118 u8 status = inb(EC_SC);
Elyes HAOUASb9331092016-09-05 19:55:34 +0200119 if (status & SCI_EVT) {
Stefan Reinauer7e568552013-03-13 17:03:04 -0700120 ec_write_cmd(QR_EC);
121 cmd = ec_read_ob();
Elyes HAOUASb9331092016-09-05 19:55:34 +0200122 } else if (status & SMI_EVT) {
Stefan Reinauer7e568552013-03-13 17:03:04 -0700123 ec_kbc_write_cmd(EC_KBD_SMI_EVENT);
124 cmd = ec_kbc_read_ob();
125 }
126 return cmd;
127}
128
129void ec_it8518_enable_wake_events(void)
130{
131 /*
132 * Set the bit in ECRAM that will enable the Lid switch as a wake source
133 */
134 u8 reg8 = ec_read(EC_WAKE_SRC_ENABLE);
135 ec_write(EC_WAKE_SRC_ENABLE, reg8 | EC_LID_WAKE_ENABLE);
136}
137
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100138static void it8518_init(struct device *dev)
Stefan Reinauer7e568552013-03-13 17:03:04 -0700139{
Stefan Reinauer7e568552013-03-13 17:03:04 -0700140 if (!dev->enabled)
141 return;
142
143 printk(BIOS_DEBUG, "Quanta IT8518: Initializing keyboard.\n");
Timothy Pearson448e3862015-11-24 14:12:01 -0600144 pc_keyboard_init(NO_AUX_DEVICE);
Stefan Reinauer7e568552013-03-13 17:03:04 -0700145}
146
Stefan Reinauer7e568552013-03-13 17:03:04 -0700147static struct device_operations ops = {
148 .init = it8518_init,
Nico Huber2f8ba692020-04-05 14:05:24 +0200149 .read_resources = noop_read_resources,
Elyes HAOUAS4d319c32020-04-07 13:51:13 +0200150 .set_resources = noop_set_resources,
Stefan Reinauer7e568552013-03-13 17:03:04 -0700151};
152
153static struct pnp_info pnp_dev_info[] = {
Felix Held1d9199c2018-07-07 00:27:44 +0200154 { NULL, 0, 0, 0, }
Stefan Reinauer7e568552013-03-13 17:03:04 -0700155};
156
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100157static void enable_dev(struct device *dev)
Stefan Reinauer7e568552013-03-13 17:03:04 -0700158{
Felix Held1d9199c2018-07-07 00:27:44 +0200159 pnp_enable_devices(dev, &ops, ARRAY_SIZE(pnp_dev_info), pnp_dev_info);
Stefan Reinauer7e568552013-03-13 17:03:04 -0700160}
161
162struct chip_operations ec_quanta_it8518_ops = {
163 CHIP_NAME("QUANTA IT8518 EC")
164 .enable_dev = enable_dev
165};