blob: dfa8c50be3eacfab6db16f3f643c61b9fe36ede6 [file] [log] [blame]
Angel Pons210a0082020-04-02 23:48:24 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauer7e568552013-03-13 17:03:04 -07002
Edward O'Callaghanb57fef92014-06-17 20:13:08 +10003#include <arch/io.h>
Stefan Reinauer7e568552013-03-13 17:03:04 -07004#include <console/console.h>
Edward O'Callaghanb57fef92014-06-17 20:13:08 +10005#include <delay.h>
Stefan Reinauer7e568552013-03-13 17:03:04 -07006#include <device/device.h>
7#include <device/pnp.h>
Edward O'Callaghanb57fef92014-06-17 20:13:08 +10008#include <pc80/keyboard.h>
Edward O'Callaghanb57fef92014-06-17 20:13:08 +10009
Stefan Reinauer7e568552013-03-13 17:03:04 -070010#include "ec.h"
11#include "chip.h"
12
13/* helper functions from drivers/pc80/keyboard.c */
14static int input_buffer_empty(u16 status_reg)
15{
16 u32 timeout;
Elyes HAOUAS2b010b82016-08-25 20:57:08 +020017 for (timeout = KBC_TIMEOUT_IN_MS; timeout && (inb(status_reg) & KBD_IBF);
Stefan Reinauer7e568552013-03-13 17:03:04 -070018 timeout--) {
Idwer Volleringd26da9c2013-12-22 21:38:18 +000019 udelay(1000);
Stefan Reinauer7e568552013-03-13 17:03:04 -070020 }
21
22 if (!timeout) {
23 printk(BIOS_WARNING, "EC-IT8518 Unexpected input buffer full\n");
24 printk(BIOS_WARNING, " Status (0x%x): 0x%x\n", status_reg, inb(status_reg));
25 }
26 return !!timeout;
27}
28
Stefan Reinauer7e568552013-03-13 17:03:04 -070029static int output_buffer_full(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)
Stefan Reinauer7e568552013-03-13 17:03:04 -070033 & KBD_OBF) == 0); 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_INFO, "EC-IT8518 output buffer result timeout\n");
39 printk(BIOS_INFO, " Status (0x%x): 0x%x\n", status_reg, inb(status_reg));
40 }
41 return !!timeout;
42}
43
Stefan Reinauer7e568552013-03-13 17:03:04 -070044/* The IT8518 60/64 EC registers are the same command/status IB/OB KBC pair.
45 * Check status from 64 port before each command.
46 *
47 * Ex. Get panel ID command C43/D77
48 * Check IBF empty. Then Write 0x43(CMD) to 0x64 Port
49 * Check IBF empty. Then Write 0x77(DATA) to 0x60 Port
50 * Check OBF set. Then Get Data(0x03:panel ID) from 0x60
51 * Different commands return may or may not respond and may have multiple
52 * bytes. Keep it simple for nor
53 */
54
55u8 ec_kbc_read_ob(void)
56{
57 if (!output_buffer_full(KBD_STATUS)) return 0;
58 return inb(KBD_DATA);
59}
60
61void ec_kbc_write_cmd(u8 cmd)
62{
63 if (!input_buffer_empty(KBD_STATUS)) return;
64 outb(cmd, KBD_COMMAND);
65}
66
67void ec_kbc_write_ib(u8 data)
68{
69 if (!input_buffer_empty(KBD_STATUS)) return;
70 outb(data, KBD_DATA);
71}
72
Stefan Reinauer7e568552013-03-13 17:03:04 -070073/*
74 * These functions are for accessing the IT8518 device RAM space via 0x66/0x68
75 */
76
77u8 ec_read_ob(void)
78{
79 if (!output_buffer_full(EC_SC)) return 0;
80 return inb(EC_DATA);
81}
82
83void ec_write_cmd(u8 cmd)
84{
85 if (!input_buffer_empty(EC_SC)) return;
86 outb(cmd, EC_SC);
87}
88
89void ec_write_ib(u8 data)
90{
91 if (!input_buffer_empty(EC_SC)) return;
92 outb(data, EC_DATA);
93}
94
95u8 ec_read(u16 addr)
96{
97 ec_write_cmd(RD_EC);
98 ec_write_ib(addr);
99 return ec_read_ob();
100}
101
102void ec_write(u16 addr, u8 data)
103{
104 ec_write_cmd(WR_EC);
105 ec_write_ib(addr);
106 ec_write_ib(data);
107}
108
Stefan Reinauer7e568552013-03-13 17:03:04 -0700109u8 ec_it8518_get_event(void)
110{
111 u8 cmd = 0;
112 u8 status = inb(EC_SC);
Elyes HAOUASb9331092016-09-05 19:55:34 +0200113 if (status & SCI_EVT) {
Stefan Reinauer7e568552013-03-13 17:03:04 -0700114 ec_write_cmd(QR_EC);
115 cmd = ec_read_ob();
Elyes HAOUASb9331092016-09-05 19:55:34 +0200116 } else if (status & SMI_EVT) {
Stefan Reinauer7e568552013-03-13 17:03:04 -0700117 ec_kbc_write_cmd(EC_KBD_SMI_EVENT);
118 cmd = ec_kbc_read_ob();
119 }
120 return cmd;
121}
122
123void ec_it8518_enable_wake_events(void)
124{
125 /*
126 * Set the bit in ECRAM that will enable the Lid switch as a wake source
127 */
128 u8 reg8 = ec_read(EC_WAKE_SRC_ENABLE);
129 ec_write(EC_WAKE_SRC_ENABLE, reg8 | EC_LID_WAKE_ENABLE);
130}
131
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100132static void it8518_init(struct device *dev)
Stefan Reinauer7e568552013-03-13 17:03:04 -0700133{
Stefan Reinauer7e568552013-03-13 17:03:04 -0700134 if (!dev->enabled)
135 return;
136
137 printk(BIOS_DEBUG, "Quanta IT8518: Initializing keyboard.\n");
Timothy Pearson448e3862015-11-24 14:12:01 -0600138 pc_keyboard_init(NO_AUX_DEVICE);
Stefan Reinauer7e568552013-03-13 17:03:04 -0700139}
140
Stefan Reinauer7e568552013-03-13 17:03:04 -0700141static struct device_operations ops = {
142 .init = it8518_init,
Nico Huber2f8ba692020-04-05 14:05:24 +0200143 .read_resources = noop_read_resources,
Elyes HAOUAS4d319c32020-04-07 13:51:13 +0200144 .set_resources = noop_set_resources,
Stefan Reinauer7e568552013-03-13 17:03:04 -0700145};
146
147static struct pnp_info pnp_dev_info[] = {
Felix Held1d9199c2018-07-07 00:27:44 +0200148 { NULL, 0, 0, 0, }
Stefan Reinauer7e568552013-03-13 17:03:04 -0700149};
150
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100151static void enable_dev(struct device *dev)
Stefan Reinauer7e568552013-03-13 17:03:04 -0700152{
Felix Held1d9199c2018-07-07 00:27:44 +0200153 pnp_enable_devices(dev, &ops, ARRAY_SIZE(pnp_dev_info), pnp_dev_info);
Stefan Reinauer7e568552013-03-13 17:03:04 -0700154}
155
156struct chip_operations ec_quanta_it8518_ops = {
Nicholas Sudsgaardbfb11be2024-01-30 09:53:46 +0900157 .name = "QUANTA IT8518 EC",
Stefan Reinauer7e568552013-03-13 17:03:04 -0700158 .enable_dev = enable_dev
159};