blob: 3f4d1b99bab60877379778a658afc64961b5bf5a [file] [log] [blame]
Stefan Reinauerd8a5fd22012-12-11 15:51:47 -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
24#include <console/console.h>
25#include <device/device.h>
26#include <device/pnp.h>
27#include <stdlib.h>
28#include <arch/io.h>
29#include <delay.h>
30#include "ec.h"
31#include "chip.h"
32
33/* kbc helper functions from drivers/pc80/keyboard.c. TODO: share functions. */
34static int kbc_input_buffer_empty(void)
35{
36 u32 timeout;
37 for(timeout = KBC_TIMEOUT_IN_MS; timeout && (inb(KBD_STATUS) & KBD_IBF);
38 timeout--) {
39 mdelay(1);
40 }
41
42 if (!timeout) {
43 printk(BIOS_WARNING,
44 "Unexpected Keyboard controller input buffer full\n");
45 }
46 return !!timeout;
47}
48
49
50static int kbc_output_buffer_full(void)
51{
52 u32 timeout;
53 for(timeout = KBC_TIMEOUT_IN_MS; timeout && ((inb(KBD_STATUS)
54 & KBD_OBF) == 0); timeout--) {
55 mdelay(1);
56 }
57
58 if (!timeout) {
59 printk(BIOS_INFO, "Keyboard controller output buffer result timeout\n");
60 }
61 return !!timeout;
62}
63
64int kbc_cleanup_buffers(void)
65{
66 u32 timeout;
67 for(timeout = KBC_TIMEOUT_IN_MS; timeout && (inb(KBD_STATUS)
68 & (KBD_OBF | KBD_IBF)); timeout--) {
69 mdelay(1);
70 inb(KBD_DATA);
71 }
72
73 if (!timeout) {
74 printk(BIOS_ERR, "Couldn't cleanup the keyboard controller buffers\n");
75 printk(BIOS_ERR, "Status (0x%x): 0x%x, Buffer (0x%x): 0x%x\n",
76 KBD_STATUS, inb(KBD_STATUS), KBD_DATA, inb(KBD_DATA));
77 }
78
79 return !!timeout;
80}
81
82
Martin Roth8940d3e2013-07-09 21:52:41 -060083/* The ENE 60/64 EC registers are the same command/status IB/OB KBC pair.
Stefan Reinauerd8a5fd22012-12-11 15:51:47 -080084 * Check status from 64 port before each command.
85 *
86 * Ex. Get panel ID command C43/D77
87 * Check IBF empty. Then Write 0x43(CMD) to 0x64 Port
88 * Check IBF empty. Then Write 0x77(DATA) to 0x60 Port
89 * Check OBF set. Then Get Data(0x03:panel ID) from 0x60
Martin Roth8940d3e2013-07-09 21:52:41 -060090 * Different commands return may or may not respond and may have multiple
Stefan Reinauerd8a5fd22012-12-11 15:51:47 -080091 * bytes. Keep it simple for nor
92 */
93
94u8 ec_kbc_read_ob(void)
95{
96 if (!kbc_output_buffer_full()) return 0;
97 return inb(KBD_DATA);
98}
99
100void ec_kbc_write_cmd(u8 cmd)
101{
102 if (!kbc_input_buffer_empty()) return;
103 outb(cmd, KBD_COMMAND);
104}
105
106void ec_kbc_write_ib(u8 data)
107{
108 if (!kbc_input_buffer_empty()) return;
109 outb(data, KBD_DATA);
110}
111
112
113/*
114 * These functions are for accessing the ENE932 device space, but are not
115 * currently used.
116 */
117/*
118static u8 ec_io_read(u16 addr)
119{
120 outb(addr >> 8, EC_IO_HIGH);
121 outb(addr & 0xff, EC_IO_LOW);
122 return inb(EC_IO_DATA);
123}
124*/
125/*static void ec_write(u16 addr, u8 data)
126{
127 outb(addr >> 8, EC_IO_HIGH);
128 outb(addr & 0xff, EC_IO_LOW;
129 outb(data, EC_IO_DATA);
130}
131*/
132
133#ifndef __SMM__
134static void ene932_init(device_t dev)
135{
136 struct ec_compal_ene932_config *conf = dev->chip_info;
137
138
139 if (!dev->enabled)
140 return;
141
142 printk(BIOS_DEBUG, "Compal ENE932: Initializing keyboard.\n");
143 pc_keyboard_init(&conf->keyboard);
144
145}
146
147
148static void ene932_read_resources(device_t dev)
149{
150 /* This function avoids an error on serial console. */
151}
152
153
154static void ene932_enable_resources(device_t dev)
155{
156 /* This function avoids an error on serial console. */
157}
158
159static struct device_operations ops = {
160 .init = ene932_init,
161 .read_resources = ene932_read_resources,
162 .enable_resources = ene932_enable_resources
163};
164
165static struct pnp_info pnp_dev_info[] = {
166 { &ops, 0, 0, { 0, 0 }, }
167};
168
169static void enable_dev(device_t dev)
170{
171 pnp_enable_devices(dev, &pnp_ops, ARRAY_SIZE(pnp_dev_info),
172 pnp_dev_info);
173}
174
175struct chip_operations ec_compal_ene932_ops = {
176 CHIP_NAME("COMPAL ENE932 EC")
177 .enable_dev = enable_dev
178};
179#endif /* ! __SMM__ */
180#endif /* ! __PRE_RAM__ */