blob: 11a1cb8e7dc14d41d3656b7e21a87d91fdc2cbb2 [file] [log] [blame]
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -08001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2012 The Chromium OS Authors. All rights reserved.
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 Reinauerd7bd4eb2013-02-11 11:11:36 -080015 */
16
17#ifndef __PRE_RAM__
18
Stefan Reinauer24d1d4b2013-03-21 11:51:41 -070019#include <arch/io.h>
Edward O'Callaghanb57fef92014-06-17 20:13:08 +100020#include <console/console.h>
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -080021#include <device/device.h>
22#include <device/pnp.h>
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -080023#include <delay.h>
24#include <elog.h>
Edward O'Callaghanb57fef92014-06-17 20:13:08 +100025#include <stdlib.h>
Edward O'Callaghane1fe6882014-04-30 20:41:41 +100026#include <pc80/keyboard.h>
Edward O'Callaghanb57fef92014-06-17 20:13:08 +100027
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -080028#include "ec.h"
29#include "chip.h"
30
31/* kbc helper functions from drivers/pc80/keyboard.c */
32static int ec_input_buffer_empty(u8 status_port)
33{
34 u32 timeout;
Elyes HAOUAS2b010b82016-08-25 20:57:08 +020035 for (timeout = KBC_TIMEOUT_IN_MS; timeout && (inb(status_port) & KBD_IBF);
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -080036 timeout--) {
37 mdelay(1);
38 }
39
40 if (!timeout) {
41 printk(BIOS_WARNING, "Unexpected EC/KBD input buffer full\n");
42 }
43 return !!timeout;
44}
45
46
47static int ec_output_buffer_full(u8 status_port)
48{
49 u32 timeout;
Elyes HAOUAS2b010b82016-08-25 20:57:08 +020050 for (timeout = KBC_TIMEOUT_IN_MS; timeout && ((inb(status_port)
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -080051 & KBD_OBF) == 0); timeout--) {
52 mdelay(1);
53 }
54
55 if (!timeout) {
56 printk(BIOS_INFO, "EC/KBD output buffer result timeout\n");
57 }
58 return !!timeout;
59}
60
61
62
63/* The ENE 60/64 EC registers are the same command/status IB/OB KBC pair.
64 * Check status from 64 port before each command.
65 *
66 * Ex. Get panel ID command C43/D77
67 * Check IBF empty. Then Write 0x43(CMD) to 0x64 Port
68 * Check IBF empty. Then Write 0x77(DATA) to 0x60 Port
69 * Check OBF set. Then Get Data(0x03:panel ID) from 0x60
70 * Different commands return may or may not respond and may have multiple
71 * bytes. Keep it simple for now
72 */
73
74u8 ec_kbc_read_ob(void)
75{
76 if (!ec_output_buffer_full(KBD_STATUS)) return 0;
77 return inb(KBD_DATA);
78}
79
80void ec_kbc_write_cmd(u8 cmd)
81{
82 if (!ec_input_buffer_empty(KBD_STATUS)) return;
83 outb(cmd, KBD_COMMAND);
84}
85
86void ec_kbc_write_ib(u8 data)
87{
88 if (!ec_input_buffer_empty(KBD_STATUS)) return;
89 outb(data, KBD_DATA);
90}
91
92/* EC Host Control Protocol routines */
93u8 ec_read_ob(void)
94{
95 if (!ec_output_buffer_full(EC_SC)) return 0;
96 return inb(EC_DATA);
97}
98
99void ec_write_cmd(u8 cmd)
100{
101 if (!ec_input_buffer_empty(EC_SC)) return;
102 outb(cmd, EC_COMMAND);
103}
104
105void ec_write_ib(u8 data)
106{
107 if (!ec_input_buffer_empty(EC_SC)) return;
108 outb(data, EC_DATA);
109}
110
111/*
112 * These functions are for accessing the ENE932 device RAM space
113 */
114u8 ec_mem_read(u8 addr)
115{
116 ec_write_cmd(EC_CMD_READ_RAM);
117 ec_write_ib(addr);
118 return ec_read_ob();
119}
120
121void ec_mem_write(u8 addr, u8 data)
122{
123 ec_write_cmd(EC_CMD_WRITE_RAM);
124 ec_write_ib(addr);
125 ec_write_ib(data);
126 return;
127}
128
129#ifndef __SMM__
130static void ene_kb3940q_log_events(void)
131{
Martin Rothf5c35182017-06-24 14:09:38 -0600132#if IS_ENABLED(CONFIG_ELOG)
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -0800133 u8 reason = ec_mem_read(EC_SHUTDOWN_REASON);
134 if (reason)
135 elog_add_event_byte(ELOG_TYPE_EC_SHUTDOWN, reason);
136#endif
137}
138
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100139static void ene_kb3940q_init(struct device *dev)
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -0800140{
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -0800141 if (!dev->enabled)
142 return;
143
144 printk(BIOS_DEBUG, "Quanta EnE KB3940Q: Initializing keyboard.\n");
Timothy Pearson448e3862015-11-24 14:12:01 -0600145 pc_keyboard_init(NO_AUX_DEVICE);
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -0800146
147 ene_kb3940q_log_events();
148}
149
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -0800150static struct device_operations ops = {
151 .init = ene_kb3940q_init,
Edward O'Callaghan5f19eb62014-11-29 00:03:03 +1100152 .read_resources = DEVICE_NOOP,
153 .enable_resources = DEVICE_NOOP,
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -0800154};
155
156static struct pnp_info pnp_dev_info[] = {
Felix Held1d9199c2018-07-07 00:27:44 +0200157 { NULL, 0, 0, 0, }
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -0800158};
159
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100160static void enable_dev(struct device *dev)
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -0800161{
Felix Held1d9199c2018-07-07 00:27:44 +0200162 pnp_enable_devices(dev, &ops, ARRAY_SIZE(pnp_dev_info), pnp_dev_info);
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -0800163}
164
165struct chip_operations ec_quanta_ene_kb3940q_ops = {
166 CHIP_NAME("QUANTA EnE KB3940Q EC")
167 .enable_dev = enable_dev
168};
169#endif /* ! __SMM__ */
170#endif /* ! __PRE_RAM__ */