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)
101 printk_debug("Keyboard init...\n");
103 /* clean up any junk that might have been in the kbc */
104 if (!kbc_cleanup_buffers()) return;
106 /* reset/self test 8042 - send cmd 0xAA */
107 if (!kbc_input_buffer_empty()) return;
109 if (!kbc_output_buffer_full()) {
110 printk_err("Could not reset keyboard controller.\n");
114 /* read self-test result, 0x55 is returned in the output buffer (0x60) */
115 if ((regval = inb(0x60) != 0x55)) {
116 printk_err("Keyboard Controller self-test failed: 0x%x\n", regval);
120 /* Enable keyboard interface - No IRQ */
124 if (!kbc_input_buffer_empty()) return;
126 if (!kbc_input_buffer_empty()) return;
127 outb(0x20, 0x60); /* send cmd: enable keyboard */
128 if (kbc_output_buffer_full()) {
131 printk_info("Timeout while enabling keyboard. (No keyboard present?)\n");
132 regval = inb(0x60); /* Better than 0 ? */
135 } while (regval == 0xFE && resend > 0);
137 /* clean up any junk that might have been in the keyboard */
138 if (!kbc_cleanup_buffers()) return;
140 /* reset keyboard and self test (keyboard side) */
141 regval = send_keyboard(0xFF);
142 if (regval != 0xFA) {
143 printk_err("Keyboard selftest failed ACK: 0x%x\n", regval);
146 if (!kbc_output_buffer_full()) {
147 printk_err("Timeout waiting for keyboard after reset.\n");
152 if (regval != 0xAA) {
153 printk_err("Keyboard selftest failed: 0x%x\n", regval);
158 * The following set scancode stuff is what normal BIOS do. It could be
159 * argued that coreboot shouldn't set the scan code.....
162 /* disable the keyboard */
163 regval = send_keyboard(0xF5);
164 if (regval != 0xFA) {
165 printk_err("Keyboard disable failed ACK: 0x%x\n", regval);
169 /* Set scancode command */
170 regval = send_keyboard(0xF0);
171 if (regval != 0xFA) {
172 printk_err("Keyboard set scancode cmd failed ACK: 0x%x\n", regval);
175 /* Set scancode mode 2 */
176 regval = send_keyboard(0x02);
177 if (regval != 0xFA) {
178 printk_err("Keyboard set scancode mode failed ACK: 0x%x\n", regval);
182 /* enable the keyboard */
183 regval = send_keyboard(0xF4);
184 if (regval != 0xFA) {
185 printk_err("Keyboard enable failed ACK: 0x%x\n", regval);
189 /* All is well - enable keyboard interface */
193 if (!kbc_input_buffer_empty()) return;
195 if (!kbc_input_buffer_empty()) return;
196 outb(0x61, 0x60); /* send cmd: enable keyboard and IRQ 1 */
197 if (kbc_output_buffer_full()) {
201 } while (regval == 0xFE && resend > 0);
205 void init_pc_keyboard(unsigned port0, unsigned port1, struct pc_keyboard *kbd)
207 if ((port0 == 0x60) && (port1 == 0x64)) {
208 pc_keyboard_init(kbd);
213 * Support PS/2 mode - oddball SIOs(KBC) need this setup
214 * Not well documented. Google - 0xcb keyboard controller
215 * This is called before pc_keyboard_init().
217 void set_kbc_ps2_mode(void)
219 /* clean up any junk that might have been in the kbc */
220 if (!kbc_cleanup_buffers()) return;
222 /* reset/self test 8042 before we can do anything */
223 if (!kbc_input_buffer_empty()) return;
225 if (!kbc_output_buffer_full()) return;
227 /* read self-test result, 0x55 is returned in the output buffer (0x60) */
228 if ((inb(0x60) != 0x55)) {
229 printk_err("Keyboard Controller selftest failed\n");
233 /* Support PS/2 mode */
234 if (!kbc_input_buffer_empty()) return;
236 if (!kbc_input_buffer_empty()) return;
238 kbc_cleanup_buffers();