2 * This file is part of the coreboot project.
4 * Copyright (C) 2008 Advanced Micro Devices, Inc.
5 * Copyright (C) ???? Ollie Lo <ollielo@hotmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
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.
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, MA 02110-1301 USA
22 #include <console/console.h>
23 #include <pc80/keyboard.h>
24 #include <device/device.h>
27 static int kbc_input_buffer_empty(void)
30 for(timeout = 1000000; timeout && (inb(0x64) & 0x02); timeout--) {
35 printk_err("Unexpected Keyboard controller input buffer full\n");
41 static int kbc_output_buffer_full(void)
44 for(timeout = 1000000; timeout && ((inb(0x64) & 0x01) == 0); timeout--) {
49 printk_err("Keyboard controller output buffer result timeout\n");
55 static int kbc_cleanup_buffers(void)
58 for(timeout = 1000000; timeout && (inb(0x64) & 0x03); timeout--) {
63 printk_err("Couldn't cleanup the keyboard controller buffers\n");
64 printk_err("0x64: 0x%x, 0x60: 0x%x\n", inb(0x64), inb(0x60));
70 static u8 send_keyboard(u8 command)
76 if (!kbc_input_buffer_empty()) return 0;
78 if (!kbc_output_buffer_full()) return 0;
81 } while (regval == 0xFE && resend > 0);
87 static void pc_keyboard_init(struct pc_keyboard *keyboard)
91 printk_debug("Keyboard init...\n");
93 /* clean up any junk that might have been in the kbc */
94 if (!kbc_cleanup_buffers()) return;
96 /* reset/self test 8042 - send cmd 0xAA, */
97 if (!kbc_input_buffer_empty()) return;
99 if (!kbc_output_buffer_full()) return;
101 /* read self-test result, 0x55 is returned in the output buffer (0x60) */
102 if ((regval = inb(0x60) != 0x55)) {
103 printk_err("Keyboard Controller selftest failed: 0x%x\n", regval);
107 /* Enable keyboard interface - No IRQ*/
111 if (!kbc_input_buffer_empty()) return;
113 if (!kbc_input_buffer_empty()) return;
114 outb(0x20, 0x60); /* send cmd: enable keyboard */
115 if ((inb(0x64) & 0x01)) {
119 } while (regval == 0xFE && resend > 0);
121 /* clean up any junk that might have been in the keyboard */
122 if (!kbc_cleanup_buffers()) return;
124 /* reset keyboard and self test (keyboard side) */
125 regval = send_keyboard(0xFF);
126 if (regval != 0xFA) {
127 printk_err("Keyboard selftest failed ACK: 0x%x\n", regval);
130 if (!kbc_output_buffer_full()) return;
132 if (regval != 0xAA) {
133 printk_err("Keyboard selftest failed: 0x%x\n", regval);
138 * The following set scancode stuff is what normal BIOS do. It could be
139 * argued that coreboot shouldn't set the scan code.....
142 /* disable the keyboard */
143 regval = send_keyboard(0xF5);
144 if (regval != 0xFA) {
145 printk_err("Keyboard disable failed ACK: 0x%x\n", regval);
149 /* Set scancode command */
150 regval = send_keyboard(0xF0);
151 if (regval != 0xFA) {
152 printk_err("Keyboard set scancode cmd failed ACK: 0x%x\n", regval);
155 /* Set scancode mode 2 */
156 regval = send_keyboard(0x02);
157 if (regval != 0xFA) {
158 printk_err("Keyboard set scancode mode failed ACK: 0x%x\n", regval);
162 /* enable the keyboard */
163 regval = send_keyboard(0xF4);
164 if (regval != 0xFA) {
165 printk_err("Keyboard enable failed ACK: 0x%x\n", regval);
169 /* All is well - enable keyboard interface */
173 if (!kbc_input_buffer_empty()) return;
175 if (!kbc_input_buffer_empty()) return;
176 outb(0x61, 0x60); /* send cmd: enable keyboard and IRQ 1 */
177 if ((inb(0x64) & 0x01)) {
181 } while (regval == 0xFE && resend > 0);
185 void init_pc_keyboard(unsigned port0, unsigned port1, struct pc_keyboard *kbd)
187 if ((port0 == 0x60) && (port1 == 0x64)) {
188 pc_keyboard_init(kbd);
193 * Support PS/2 mode - oddball SIOs(KBC) need this setup
194 * Not well documented. Google - 0xcb keyboard controller
195 * This is called before pc_keyboard_init().
197 void set_kbc_ps2_mode(void)
199 /* clean up any junk that might have been in the kbc */
200 if (!kbc_cleanup_buffers()) return;
202 /* reset/self test 8042 before we can do anything */
203 if (!kbc_input_buffer_empty()) return;
205 if (!kbc_output_buffer_full()) return;
207 /* read self-test result, 0x55 is returned in the output buffer (0x60) */
208 if ((inb(0x60) != 0x55)) {
209 printk_err("Keyboard Controller selftest failed\n");
213 /* Support PS/2 mode */
214 if (!kbc_input_buffer_empty()) return;
216 if (!kbc_input_buffer_empty()) return;
218 kbc_cleanup_buffers();