blob: dcd9c4af0599e6dadc21e056a5fa70209de9abd3 [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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
19 * MA 02110-1301 USA
20 */
21
22#ifndef __PRE_RAM__
23
Stefan Reinauer24d1d4b2013-03-21 11:51:41 -070024#include <arch/io.h>
Edward O'Callaghanb57fef92014-06-17 20:13:08 +100025#include <console/console.h>
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -080026#include <device/device.h>
27#include <device/pnp.h>
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -080028#include <delay.h>
29#include <elog.h>
Edward O'Callaghanb57fef92014-06-17 20:13:08 +100030#include <stdlib.h>
Edward O'Callaghane1fe6882014-04-30 20:41:41 +100031#include <pc80/keyboard.h>
Edward O'Callaghanb57fef92014-06-17 20:13:08 +100032
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -080033#include "ec.h"
34#include "chip.h"
35
36/* kbc helper functions from drivers/pc80/keyboard.c */
37static int ec_input_buffer_empty(u8 status_port)
38{
39 u32 timeout;
40 for(timeout = KBC_TIMEOUT_IN_MS; timeout && (inb(status_port) & KBD_IBF);
41 timeout--) {
42 mdelay(1);
43 }
44
45 if (!timeout) {
46 printk(BIOS_WARNING, "Unexpected EC/KBD input buffer full\n");
47 }
48 return !!timeout;
49}
50
51
52static int ec_output_buffer_full(u8 status_port)
53{
54 u32 timeout;
55 for(timeout = KBC_TIMEOUT_IN_MS; timeout && ((inb(status_port)
56 & KBD_OBF) == 0); timeout--) {
57 mdelay(1);
58 }
59
60 if (!timeout) {
61 printk(BIOS_INFO, "EC/KBD output buffer result timeout\n");
62 }
63 return !!timeout;
64}
65
66
67
68/* The ENE 60/64 EC registers are the same command/status IB/OB KBC pair.
69 * Check status from 64 port before each command.
70 *
71 * Ex. Get panel ID command C43/D77
72 * Check IBF empty. Then Write 0x43(CMD) to 0x64 Port
73 * Check IBF empty. Then Write 0x77(DATA) to 0x60 Port
74 * Check OBF set. Then Get Data(0x03:panel ID) from 0x60
75 * Different commands return may or may not respond and may have multiple
76 * bytes. Keep it simple for now
77 */
78
79u8 ec_kbc_read_ob(void)
80{
81 if (!ec_output_buffer_full(KBD_STATUS)) return 0;
82 return inb(KBD_DATA);
83}
84
85void ec_kbc_write_cmd(u8 cmd)
86{
87 if (!ec_input_buffer_empty(KBD_STATUS)) return;
88 outb(cmd, KBD_COMMAND);
89}
90
91void ec_kbc_write_ib(u8 data)
92{
93 if (!ec_input_buffer_empty(KBD_STATUS)) return;
94 outb(data, KBD_DATA);
95}
96
97/* EC Host Control Protocol routines */
98u8 ec_read_ob(void)
99{
100 if (!ec_output_buffer_full(EC_SC)) return 0;
101 return inb(EC_DATA);
102}
103
104void ec_write_cmd(u8 cmd)
105{
106 if (!ec_input_buffer_empty(EC_SC)) return;
107 outb(cmd, EC_COMMAND);
108}
109
110void ec_write_ib(u8 data)
111{
112 if (!ec_input_buffer_empty(EC_SC)) return;
113 outb(data, EC_DATA);
114}
115
116/*
117 * These functions are for accessing the ENE932 device RAM space
118 */
119u8 ec_mem_read(u8 addr)
120{
121 ec_write_cmd(EC_CMD_READ_RAM);
122 ec_write_ib(addr);
123 return ec_read_ob();
124}
125
126void ec_mem_write(u8 addr, u8 data)
127{
128 ec_write_cmd(EC_CMD_WRITE_RAM);
129 ec_write_ib(addr);
130 ec_write_ib(data);
131 return;
132}
133
134#ifndef __SMM__
135static void ene_kb3940q_log_events(void)
136{
137#if CONFIG_ELOG
138 u8 reason = ec_mem_read(EC_SHUTDOWN_REASON);
139 if (reason)
140 elog_add_event_byte(ELOG_TYPE_EC_SHUTDOWN, reason);
141#endif
142}
143
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100144static void ene_kb3940q_init(struct device *dev)
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -0800145{
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -0800146 if (!dev->enabled)
147 return;
148
149 printk(BIOS_DEBUG, "Quanta EnE KB3940Q: Initializing keyboard.\n");
Edward O'Callaghandef00be2014-04-30 05:01:52 +1000150 pc_keyboard_init();
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -0800151
152 ene_kb3940q_log_events();
153}
154
155
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100156static void ene_kb3940q_read_resources(struct device *dev)
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -0800157{
158 /* This function avoids an error on serial console. */
159}
160
161
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100162static void ene_kb3940q_enable_resources(struct device *dev)
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -0800163{
164 /* This function avoids an error on serial console. */
165}
166
167static struct device_operations ops = {
168 .init = ene_kb3940q_init,
169 .read_resources = ene_kb3940q_read_resources,
170 .enable_resources = ene_kb3940q_enable_resources
171};
172
173static struct pnp_info pnp_dev_info[] = {
174 { &ops, 0, 0, { 0, 0 }, }
175};
176
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100177static void enable_dev(struct device *dev)
Stefan Reinauerd7bd4eb2013-02-11 11:11:36 -0800178{
179 pnp_enable_devices(dev, &pnp_ops, ARRAY_SIZE(pnp_dev_info),
180 pnp_dev_info);
181}
182
183struct chip_operations ec_quanta_ene_kb3940q_ops = {
184 CHIP_NAME("QUANTA EnE KB3940Q EC")
185 .enable_dev = enable_dev
186};
187#endif /* ! __SMM__ */
188#endif /* ! __PRE_RAM__ */