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 and IRQ 1 */
116 if ((inb(0x64) & 0x01)) {
120 } while (regval == 0xFE && resend > 0);
122 /* clean up any junk that might have been in the keyboard */
123 if (!kbc_cleanup_buffers()) return;
125 /* reset keyboard and self test (keyboard side) */
126 regval = send_keyboard(0xFF);
127 if (regval != 0xFA) {
128 printk_err("Keyboard selftest failed ACK: 0x%x\n", regval);
131 if (!kbc_output_buffer_full()) return;
133 if (regval != 0xAA) {
134 printk_err("Keyboard selftest failed: 0x%x\n", regval);
139 * The following set scancode stuff is what normal BIOS do. It could be
140 * argued that coreboot shouldn't set the scan code.....
143 /* disable the keyboard */
144 regval = send_keyboard(0xF5);
145 if (regval != 0xFA) {
146 printk_err("Keyboard disable failed ACK: 0x%x\n", regval);
150 /* Set scancode command */
151 regval = send_keyboard(0xF0);
152 if (regval != 0xFA) {
153 printk_err("Keyboard set scancode cmd failed ACK: 0x%x\n", regval);
156 /* Set scancode mode 2 */
157 regval = send_keyboard(0x02);
158 if (regval != 0xFA) {
159 printk_err("Keyboard set scancode mode failed ACK: 0x%x\n", regval);
163 /* enable the keyboard */
164 regval = send_keyboard(0xF4);
165 if (regval != 0xFA) {
166 printk_err("Keyboard enable failed ACK: 0x%x\n", regval);
170 /* All is well - enable keyboard interface */
174 if (!kbc_input_buffer_empty()) return;
176 if (!kbc_input_buffer_empty()) return;
177 outb(0x61, 0x60); /* send cmd: enable keyboard and IRQ 1 */
178 if ((inb(0x64) & 0x01)) {
182 } while (regval == 0xFE && resend > 0);
186 void init_pc_keyboard(unsigned port0, unsigned port1, struct pc_keyboard *kbd)
188 if ((port0 == 0x60) && (port1 == 0x64)) {
189 pc_keyboard_init(kbd);
194 * Support PS/2 mode - oddball SIOs(KBC) need this setup
195 * Not well documented. Google - 0xcb keyboard controller
196 * This is called before pc_keyboard_init().
198 void set_kbc_ps2_mode()
200 /* clean up any junk that might have been in the kbc */
201 if (!kbc_cleanup_buffers()) return;
203 /* reset/self test 8042 before we can do anything */
204 if (!kbc_input_buffer_empty()) return;
206 if (!kbc_output_buffer_full()) return;
208 /* read self-test result, 0x55 is returned in the output buffer (0x60) */
209 if ((inb(0x60) != 0x55)) {
210 printk_err("Keyboard Controller selftest failed\n");
214 /* Support PS/2 mode */
215 if (!kbc_input_buffer_empty()) return;
217 if (!kbc_input_buffer_empty()) return;
219 kbc_cleanup_buffers();