blob: e37b980572398744f66760138a2616e666a53ade [file] [log] [blame]
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -08001/*
2 * This file is part of the coreboot project.
3 *
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -08004 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; version 2 of
8 * the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -080014 */
15
Stefan Reinauer24d1d4b2013-03-21 11:51:41 -070016#include <arch/io.h>
Edward O'Callaghanb57fef92014-06-17 20:13:08 +100017#include <console/console.h>
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -080018#include <device/device.h>
19#include <device/pnp.h>
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -080020#include <delay.h>
21#include <elog.h>
Edward O'Callaghane1fe6882014-04-30 20:41:41 +100022#include <pc80/keyboard.h>
Edward O'Callaghanb57fef92014-06-17 20:13:08 +100023
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -080024#include "ec.h"
25#include "chip.h"
26
27/* kbc helper functions from drivers/pc80/keyboard.c */
28static int ec_input_buffer_empty(u8 status_port)
29{
30 u32 timeout;
Elyes HAOUAS2b010b82016-08-25 20:57:08 +020031 for (timeout = KBC_TIMEOUT_IN_MS; timeout && (inb(status_port) & KBD_IBF);
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -080032 timeout--) {
33 mdelay(1);
34 }
35
36 if (!timeout) {
37 printk(BIOS_WARNING, "Unexpected EC/KBD input buffer full\n");
38 }
39 return !!timeout;
40}
41
42
43static int ec_output_buffer_full(u8 status_port)
44{
45 u32 timeout;
Elyes HAOUAS2b010b82016-08-25 20:57:08 +020046 for (timeout = KBC_TIMEOUT_IN_MS; timeout && ((inb(status_port)
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -080047 & KBD_OBF) == 0); timeout--) {
48 mdelay(1);
49 }
50
51 if (!timeout) {
52 printk(BIOS_INFO, "EC/KBD output buffer result timeout\n");
53 }
54 return !!timeout;
55}
56
57
58
59/* The ENE 60/64 EC registers are the same command/status IB/OB KBC pair.
60 * Check status from 64 port before each command.
61 *
62 * Ex. Get panel ID command C43/D77
63 * Check IBF empty. Then Write 0x43(CMD) to 0x64 Port
64 * Check IBF empty. Then Write 0x77(DATA) to 0x60 Port
65 * Check OBF set. Then Get Data(0x03:panel ID) from 0x60
66 * Different commands return may or may not respond and may have multiple
67 * bytes. Keep it simple for now
68 */
69
70u8 ec_kbc_read_ob(void)
71{
72 if (!ec_output_buffer_full(KBD_STATUS)) return 0;
73 return inb(KBD_DATA);
74}
75
76void ec_kbc_write_cmd(u8 cmd)
77{
78 if (!ec_input_buffer_empty(KBD_STATUS)) return;
79 outb(cmd, KBD_COMMAND);
80}
81
82void ec_kbc_write_ib(u8 data)
83{
84 if (!ec_input_buffer_empty(KBD_STATUS)) return;
85 outb(data, KBD_DATA);
86}
87
88/* EC Host Control Protocol routines */
89u8 ec_read_ob(void)
90{
91 if (!ec_output_buffer_full(EC_SC)) return 0;
92 return inb(EC_DATA);
93}
94
95void ec_write_cmd(u8 cmd)
96{
97 if (!ec_input_buffer_empty(EC_SC)) return;
98 outb(cmd, EC_COMMAND);
99}
100
101void ec_write_ib(u8 data)
102{
103 if (!ec_input_buffer_empty(EC_SC)) return;
104 outb(data, EC_DATA);
105}
106
107/*
108 * These functions are for accessing the ENE932 device RAM space
109 */
110u8 ec_mem_read(u8 addr)
111{
112 ec_write_cmd(EC_CMD_READ_RAM);
113 ec_write_ib(addr);
114 return ec_read_ob();
115}
116
117void ec_mem_write(u8 addr, u8 data)
118{
119 ec_write_cmd(EC_CMD_WRITE_RAM);
120 ec_write_ib(addr);
121 ec_write_ib(data);
122 return;
123}
124
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -0800125static void ene_kb3940q_log_events(void)
126{
Kyösti Mälkkibe5317f2019-11-06 12:07:21 +0200127 if (CONFIG(ELOG)) {
128 u8 reason = ec_mem_read(EC_SHUTDOWN_REASON);
129 if (reason)
130 elog_add_event_byte(ELOG_TYPE_EC_SHUTDOWN, reason);
131 }
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -0800132}
133
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100134static void ene_kb3940q_init(struct device *dev)
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -0800135{
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -0800136 if (!dev->enabled)
137 return;
138
139 printk(BIOS_DEBUG, "Quanta EnE KB3940Q: Initializing keyboard.\n");
Timothy Pearson448e3862015-11-24 14:12:01 -0600140 pc_keyboard_init(NO_AUX_DEVICE);
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -0800141
142 ene_kb3940q_log_events();
143}
144
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -0800145static struct device_operations ops = {
146 .init = ene_kb3940q_init,
Edward O'Callaghan5f19eb62014-11-29 00:03:03 +1100147 .read_resources = DEVICE_NOOP,
148 .enable_resources = DEVICE_NOOP,
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -0800149};
150
151static struct pnp_info pnp_dev_info[] = {
Felix Held1d9199c2018-07-07 00:27:44 +0200152 { NULL, 0, 0, 0, }
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -0800153};
154
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100155static void enable_dev(struct device *dev)
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -0800156{
Felix Held1d9199c2018-07-07 00:27:44 +0200157 pnp_enable_devices(dev, &ops, ARRAY_SIZE(pnp_dev_info), pnp_dev_info);
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -0800158}
159
160struct chip_operations ec_quanta_ene_kb3940q_ops = {
161 CHIP_NAME("QUANTA EnE KB3940Q EC")
162 .enable_dev = enable_dev
163};