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>
30 #define KBD_COMMAND 0x64
31 #define KBD_STATUS 0x64
32 #define KBD_IBF (1 << 1) // 1: input buffer full (data ready for ec)
33 #define KBD_OBF (1 << 0) // 1: output buffer full (data ready for host)
35 // Keyboard Controller Commands
36 #define KBC_CMD_READ_COMMAND 0x20 // Read command byte
37 #define KBC_CMD_WRITE_COMMAND 0x60 // Write command byte
38 #define KBC_CMD_SELF_TEST 0xAA // Controller self-test
40 /* The Keyboard controller command byte
42 * ----+-------------------------------------------------------
43 * 7 | reserved, must be zero
44 * 6 | XT Translation, (1 = on, 0 = off)
45 * 5 | Disable Mouse Port (1 = disable, 0 = enable)
46 * 4 | Disable Keyboard Port (1 = disable, 0 = enable)
47 * 3 | reserved, must be zero
48 * 2 | System Flag (1 = self-test passed. DO NOT SET TO ZERO)
49 * 1 | Mouse Port Interrupts (1 = enable, 0 = disable)
50 * 0 | Keyboard Port Interrupts (1 = enable, 0 = disable)
53 // Keyboard Controller Replies
54 #define KBC_REPLY_SELFTEST_OK 0x55 // controller self-test succeeded
59 #define KBD_REPLY_POR 0xAA // Power on reset
60 #define KBD_REPLY_ACK 0xFA // Command ACK
61 #define KBD_REPLY_RESEND 0xFE // Command NACK, send command again
63 /* Wait 200ms for keyboard controller answers */
64 #define KBC_TIMEOUT_IN_MS 200
66 static int kbc_input_buffer_empty(void)
69 for(timeout = KBC_TIMEOUT_IN_MS; timeout && (inb(KBD_STATUS) & KBD_IBF); timeout--) {
74 printk_warning("Unexpected Keyboard controller input buffer full\n");
80 static int kbc_output_buffer_full(void)
83 for(timeout = KBC_TIMEOUT_IN_MS; timeout && ((inb(KBD_STATUS) & KBD_OBF) == 0); timeout--) {
88 printk_warning("Keyboard controller output buffer result timeout\n");
94 static int kbc_cleanup_buffers(void)
97 for(timeout = KBC_TIMEOUT_IN_MS; timeout && (inb(KBD_STATUS) & (KBD_OBF | KBD_IBF)); timeout--) {
103 printk_err("Couldn't cleanup the keyboard controller buffers\n");
104 printk_err("Status (0x%x): 0x%x, Buffer (0x%x): 0x%x\n",
105 KBD_STATUS, inb(KBD_STATUS), KBD_DATA, inb(KBD_DATA));
111 static int kbc_self_test(void)
115 /* Clean up any junk that might have been in the KBC.
116 * Both input and output buffers must be empty.
118 if (!kbc_cleanup_buffers())
121 /* reset/self test 8042 - send cmd 0xAA */
122 outb(KBC_CMD_SELF_TEST, KBD_COMMAND);
124 if (!kbc_output_buffer_full()) {
125 /* There probably is no keyboard controller. */
126 printk_err("Could not reset keyboard controller.\n");
130 /* read self-test result, 0x55 is returned in the output buffer */
131 self_test = inb(KBD_DATA);
133 if (self_test != 0x55) {
134 printk_err("Keyboard Controller self-test failed: 0x%x\n",
142 static u8 send_keyboard(u8 command)
148 if (!kbc_input_buffer_empty()) return 0;
149 outb(command, KBD_DATA);
150 if (!kbc_output_buffer_full()) {
151 printk_err("Could not send keyboard command %02x\n",
155 regval = inb(KBD_DATA);
157 } while (regval == KBD_REPLY_RESEND && resend > 0);
162 void pc_keyboard_init(struct pc_keyboard *keyboard)
165 printk_debug("Keyboard init...\n");
167 /* Run a keyboard controller self-test */
168 if (!kbc_self_test())
171 /* Enable keyboard interface - No IRQ */
172 if (!kbc_input_buffer_empty()) return;
173 outb(0x60, KBD_COMMAND);
174 if (!kbc_input_buffer_empty()) return;
175 outb(0x20, KBD_DATA); /* send cmd: enable keyboard */
176 if (!kbc_input_buffer_empty()) {
177 printk_info("Timeout while enabling keyboard\n");
181 /* clean up any junk that might have been in the keyboard */
182 if (!kbc_cleanup_buffers()) return;
184 /* reset keyboard and self test (keyboard side) */
185 regval = send_keyboard(0xFF);
186 if (regval == KBD_REPLY_RESEND) {
187 /* keeps sending RESENDs, probably no keyboard. */
188 printk_info("No PS/2 keyboard detected.\n");
192 if (regval != KBD_REPLY_ACK) {
193 printk_err("Keyboard selftest failed ACK: 0x%x\n", regval);
197 if (!kbc_output_buffer_full()) {
198 printk_err("Timeout waiting for keyboard after reset.\n");
202 regval = inb(KBD_DATA);
203 if (regval != 0xAA) {
204 printk_err("Keyboard selftest failed: 0x%x\n", regval);
209 * The following set scancode stuff is what normal BIOS do. It could be
210 * argued that coreboot shouldn't set the scan code.....
213 /* disable the keyboard */
214 regval = send_keyboard(0xF5);
215 if (regval != KBD_REPLY_ACK) {
216 printk_err("Keyboard disable failed ACK: 0x%x\n", regval);
220 /* Set scancode command */
221 regval = send_keyboard(0xF0);
222 if (regval != KBD_REPLY_ACK) {
223 printk_err("Keyboard set scancode cmd failed ACK: 0x%x\n", regval);
226 /* Set scancode mode 2 */
227 regval = send_keyboard(0x02);
228 if (regval != KBD_REPLY_ACK) {
229 printk_err("Keyboard set scancode mode failed ACK: 0x%x\n", regval);
233 /* enable the keyboard */
234 regval = send_keyboard(0xF4);
235 if (regval != KBD_REPLY_ACK) {
236 printk_err("Keyboard enable failed ACK: 0x%x\n", regval);
240 /* All is well - enable keyboard interface */
241 if (!kbc_input_buffer_empty()) return;
242 outb(0x60, KBD_COMMAND);
243 if (!kbc_input_buffer_empty()) return;
244 outb(0x61, KBD_DATA); /* send cmd: enable keyboard and IRQ 1 */
245 if (kbc_output_buffer_full()) {
246 printk_err("Timeout during final keyboard enable\n");
252 * Support PS/2 mode - oddball SIOs(KBC) need this setup
253 * Not well documented. Google - 0xcb keyboard controller
254 * This is called before pc_keyboard_init().
256 void set_kbc_ps2_mode(void)
258 /* Run a keyboard controller self-test */
259 if (!kbc_self_test())
262 /* Support PS/2 mode */
263 if (!kbc_input_buffer_empty()) return;
264 outb(0xcb, KBD_COMMAND);
266 if (!kbc_input_buffer_empty()) return;
267 outb(0x01, KBD_DATA);
269 kbc_cleanup_buffers();