2 * This file is part of the coreboot project.
4 * Copyright (C) 2009 coresystems GmbH
5 * Copyright (C) 2008 Advanced Micro Devices, Inc.
6 * Copyright (C) 2003 Ollie Lo <ollielo@hotmail.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <console/console.h>
24 #include <pc80/keyboard.h>
25 #include <device/device.h>
29 /* Wait 200ms for keyboard controller answers */
30 #define KBC_TIMEOUT_IN_MS 200
32 static int kbc_input_buffer_empty(void)
35 for(timeout = KBC_TIMEOUT_IN_MS; timeout && (inb(0x64) & 0x02); timeout--) {
40 printk_warning("Unexpected Keyboard controller input buffer full\n");
46 static int kbc_output_buffer_full(void)
49 for(timeout = KBC_TIMEOUT_IN_MS; timeout && ((inb(0x64) & 0x01) == 0); timeout--) {
54 printk_warning("Keyboard controller output buffer result timeout\n");
60 static int kbc_cleanup_buffers(void)
63 for(timeout = KBC_TIMEOUT_IN_MS; timeout && (inb(0x64) & 0x03); timeout--) {
69 printk_err("Couldn't cleanup the keyboard controller buffers\n");
70 printk_err("0x64: 0x%x, 0x60: 0x%x\n", inb(0x64), inb(0x60));
76 static u8 send_keyboard(u8 command)
82 if (!kbc_input_buffer_empty()) return 0;
84 if (!kbc_output_buffer_full()) {
85 printk_err("Could not send keyboard command %02x\n",
91 } while (regval == 0xFE && resend > 0);
97 static void pc_keyboard_init(struct pc_keyboard *keyboard)
100 printk_debug("Keyboard init...\n");
102 /* clean up any junk that might have been in the kbc */
103 if (!kbc_cleanup_buffers()) return;
105 /* reset/self test 8042 - send cmd 0xAA */
106 if (!kbc_input_buffer_empty()) return;
108 if (!kbc_output_buffer_full()) {
109 printk_err("Could not reset keyboard controller.\n");
113 /* read self-test result, 0x55 is returned in the output buffer (0x60) */
114 if ((regval = inb(0x60) != 0x55)) {
115 printk_err("Keyboard Controller self-test failed: 0x%x\n", regval);
119 /* Enable keyboard interface - No IRQ */
120 if (!kbc_input_buffer_empty()) return;
122 if (!kbc_input_buffer_empty()) return;
123 outb(0x20, 0x60); /* send cmd: enable keyboard */
124 if (!kbc_input_buffer_empty()) {
125 printk_info("Timeout while enabling keyboard\n");
129 /* clean up any junk that might have been in the keyboard */
130 if (!kbc_cleanup_buffers()) return;
132 /* reset keyboard and self test (keyboard side) */
133 regval = send_keyboard(0xFF);
134 if (regval != 0xFA) {
135 printk_err("Keyboard selftest failed ACK: 0x%x\n", regval);
138 if (!kbc_output_buffer_full()) {
139 printk_err("Timeout waiting for keyboard after reset.\n");
144 if (regval != 0xAA) {
145 printk_err("Keyboard selftest failed: 0x%x\n", regval);
150 * The following set scancode stuff is what normal BIOS do. It could be
151 * argued that coreboot shouldn't set the scan code.....
154 /* disable the keyboard */
155 regval = send_keyboard(0xF5);
156 if (regval != 0xFA) {
157 printk_err("Keyboard disable failed ACK: 0x%x\n", regval);
161 /* Set scancode command */
162 regval = send_keyboard(0xF0);
163 if (regval != 0xFA) {
164 printk_err("Keyboard set scancode cmd failed ACK: 0x%x\n", regval);
167 /* Set scancode mode 2 */
168 regval = send_keyboard(0x02);
169 if (regval != 0xFA) {
170 printk_err("Keyboard set scancode mode failed ACK: 0x%x\n", regval);
174 /* enable the keyboard */
175 regval = send_keyboard(0xF4);
176 if (regval != 0xFA) {
177 printk_err("Keyboard enable failed ACK: 0x%x\n", regval);
181 /* All is well - enable keyboard interface */
182 if (!kbc_input_buffer_empty()) return;
184 if (!kbc_input_buffer_empty()) return;
185 outb(0x61, 0x60); /* send cmd: enable keyboard and IRQ 1 */
186 if (!kbc_input_buffer_empty()) {
187 printk_err("Timeout during final keyboard enable\n");
193 void init_pc_keyboard(unsigned port0, unsigned port1, struct pc_keyboard *kbd)
195 if ((port0 == 0x60) && (port1 == 0x64)) {
196 pc_keyboard_init(kbd);
201 * Support PS/2 mode - oddball SIOs(KBC) need this setup
202 * Not well documented. Google - 0xcb keyboard controller
203 * This is called before pc_keyboard_init().
205 void set_kbc_ps2_mode(void)
207 /* clean up any junk that might have been in the kbc */
208 if (!kbc_cleanup_buffers()) return;
210 /* reset/self test 8042 before we can do anything */
211 if (!kbc_input_buffer_empty()) return;
213 if (!kbc_output_buffer_full()) return;
215 /* read self-test result, 0x55 is returned in the output buffer (0x60) */
216 if ((inb(0x60) != 0x55)) {
217 printk_err("Keyboard Controller selftest failed\n");
221 /* Support PS/2 mode */
222 if (!kbc_input_buffer_empty()) return;
224 if (!kbc_input_buffer_empty()) return;
226 kbc_cleanup_buffers();