blob: 8552860b6fdf218a82072bc4afa03a0a02ef1d18 [file] [log] [blame]
Marc Jonesecd176f2008-09-26 19:15:38 +00001/*
2 * This file is part of the coreboot project.
3 *
Stefan Reinauer094198c2009-07-21 21:24:22 +00004 * Copyright (C) 2009 coresystems GmbH
Marc Jonesecd176f2008-09-26 19:15:38 +00005 * Copyright (C) 2008 Advanced Micro Devices, Inc.
Stefan Reinauer094198c2009-07-21 21:24:22 +00006 * Copyright (C) 2003 Ollie Lo <ollielo@hotmail.com>
Marc Jonesecd176f2008-09-26 19:15:38 +00007 *
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.
11 *
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.
16 *
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
20 */
21
22
Eric Biederman5cd81732004-03-11 15:01:31 +000023#include <console/console.h>
24#include <pc80/keyboard.h>
25#include <device/device.h>
Ronald G. Minnichef5f2182003-09-26 15:24:54 +000026#include <arch/io.h>
Stefan Reinauer094198c2009-07-21 21:24:22 +000027#include <delay.h>
28
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +000029#define KBD_DATA 0x60
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)
34
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
39
40/* The Keyboard controller command byte
41 * BIT | Description
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)
51 */
52
53// Keyboard Controller Replies
54#define KBC_REPLY_SELFTEST_OK 0x55 // controller self-test succeeded
55
56//
57// Keyboard Replies
58//
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
62
Stefan Reinauerd1149d72010-03-24 21:24:17 +000063/* Wait 400ms for keyboard controller answers */
64#define KBC_TIMEOUT_IN_MS 400
Ronald G. Minnich430111b2003-09-26 16:12:23 +000065
Marc Jonesecd176f2008-09-26 19:15:38 +000066static int kbc_input_buffer_empty(void)
Eric Biederman029517c2004-11-11 11:56:00 +000067{
Marc Jonesecd176f2008-09-26 19:15:38 +000068 u32 timeout;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +000069 for(timeout = KBC_TIMEOUT_IN_MS; timeout && (inb(KBD_STATUS) & KBD_IBF); timeout--) {
Stefan Reinauer094198c2009-07-21 21:24:22 +000070 mdelay(1);
Marc Jonesecd176f2008-09-26 19:15:38 +000071 }
72
73 if (!timeout) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000074 printk(BIOS_WARNING, "Unexpected Keyboard controller input buffer full\n");
Eric Biederman029517c2004-11-11 11:56:00 +000075 }
76 return !!timeout;
77}
78
Marc Jonesecd176f2008-09-26 19:15:38 +000079
80static int kbc_output_buffer_full(void)
Eric Biederman029517c2004-11-11 11:56:00 +000081{
Marc Jonesecd176f2008-09-26 19:15:38 +000082 u32 timeout;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +000083 for(timeout = KBC_TIMEOUT_IN_MS; timeout && ((inb(KBD_STATUS) & KBD_OBF) == 0); timeout--) {
Stefan Reinauer094198c2009-07-21 21:24:22 +000084 mdelay(1);
Marc Jonesecd176f2008-09-26 19:15:38 +000085 }
86
87 if (!timeout) {
Stefan Reinauerd1149d72010-03-24 21:24:17 +000088 printk(BIOS_INFO, "Keyboard controller output buffer result timeout\n");
Eric Biederman029517c2004-11-11 11:56:00 +000089 }
90 return !!timeout;
91}
92
Marc Jonesecd176f2008-09-26 19:15:38 +000093
94static int kbc_cleanup_buffers(void)
95{
96 u32 timeout;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +000097 for(timeout = KBC_TIMEOUT_IN_MS; timeout && (inb(KBD_STATUS) & (KBD_OBF | KBD_IBF)); timeout--) {
Stefan Reinauer094198c2009-07-21 21:24:22 +000098 mdelay(1);
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +000099 inb(KBD_DATA);
Marc Jonesecd176f2008-09-26 19:15:38 +0000100 }
101
102 if (!timeout) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000103 printk(BIOS_ERR, "Couldn't cleanup the keyboard controller buffers\n");
104 printk(BIOS_ERR, "Status (0x%x): 0x%x, Buffer (0x%x): 0x%x\n",
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000105 KBD_STATUS, inb(KBD_STATUS), KBD_DATA, inb(KBD_DATA));
Marc Jonesecd176f2008-09-26 19:15:38 +0000106 }
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000107
Marc Jonesecd176f2008-09-26 19:15:38 +0000108 return !!timeout;
109}
110
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000111static int kbc_self_test(void)
112{
113 u8 self_test;
114
115 /* Clean up any junk that might have been in the KBC.
116 * Both input and output buffers must be empty.
117 */
118 if (!kbc_cleanup_buffers())
119 return 0;
120
121 /* reset/self test 8042 - send cmd 0xAA */
122 outb(KBC_CMD_SELF_TEST, KBD_COMMAND);
123
124 if (!kbc_output_buffer_full()) {
125 /* There probably is no keyboard controller. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000126 printk(BIOS_ERR, "Could not reset keyboard controller.\n");
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000127 return 0;
128 }
129
130 /* read self-test result, 0x55 is returned in the output buffer */
131 self_test = inb(KBD_DATA);
132
133 if (self_test != 0x55) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000134 printk(BIOS_ERR, "Keyboard Controller self-test failed: 0x%x\n",
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000135 self_test);
136 return 0;
137 }
138
139 return 1;
140}
Marc Jonesecd176f2008-09-26 19:15:38 +0000141
142static u8 send_keyboard(u8 command)
143{
144 u8 regval = 0;
145 u8 resend = 10;
146
147 do {
148 if (!kbc_input_buffer_empty()) return 0;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000149 outb(command, KBD_DATA);
Stefan Reinauer094198c2009-07-21 21:24:22 +0000150 if (!kbc_output_buffer_full()) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000151 printk(BIOS_ERR, "Could not send keyboard command %02x\n",
Stefan Reinauer094198c2009-07-21 21:24:22 +0000152 command);
153 return 0;
154 }
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000155 regval = inb(KBD_DATA);
Marc Jonesecd176f2008-09-26 19:15:38 +0000156 --resend;
Stefan Reinauer9c3e8b52010-02-28 19:12:37 +0000157 } while (regval == KBD_REPLY_RESEND && resend > 0);
Marc Jonesecd176f2008-09-26 19:15:38 +0000158
159 return regval;
160}
161
Stefan Reinauer740b5872010-02-23 20:31:37 +0000162void pc_keyboard_init(struct pc_keyboard *keyboard)
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000163{
Marc Jonesecd176f2008-09-26 19:15:38 +0000164 u8 regval;
Kevin O'Connor9c35c842010-09-09 08:34:02 +0000165 if (!CONFIG_DRIVERS_PS2_KEYBOARD)
166 return;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000167 printk(BIOS_DEBUG, "Keyboard init...\n");
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000168
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000169 /* Run a keyboard controller self-test */
170 if (!kbc_self_test())
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000171 return;
Stefan Reinauer094198c2009-07-21 21:24:22 +0000172
173 /* Enable keyboard interface - No IRQ */
Marc Jonese59f2e12009-09-29 19:12:23 +0000174 if (!kbc_input_buffer_empty()) return;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000175 outb(0x60, KBD_COMMAND);
Marc Jonese59f2e12009-09-29 19:12:23 +0000176 if (!kbc_input_buffer_empty()) return;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000177 outb(0x20, KBD_DATA); /* send cmd: enable keyboard */
Marc Jonese59f2e12009-09-29 19:12:23 +0000178 if (!kbc_input_buffer_empty()) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000179 printk(BIOS_INFO, "Timeout while enabling keyboard\n");
Marc Jonese59f2e12009-09-29 19:12:23 +0000180 return;
181 }
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000182
Marc Jonesecd176f2008-09-26 19:15:38 +0000183 /* clean up any junk that might have been in the keyboard */
184 if (!kbc_cleanup_buffers()) return;
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000185
Marc Jonesecd176f2008-09-26 19:15:38 +0000186 /* reset keyboard and self test (keyboard side) */
187 regval = send_keyboard(0xFF);
Stefan Reinauer9c3e8b52010-02-28 19:12:37 +0000188 if (regval == KBD_REPLY_RESEND) {
189 /* keeps sending RESENDs, probably no keyboard. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000190 printk(BIOS_INFO, "No PS/2 keyboard detected.\n");
Stefan Reinauer9c3e8b52010-02-28 19:12:37 +0000191 return;
192 }
193
194 if (regval != KBD_REPLY_ACK) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000195 printk(BIOS_ERR, "Keyboard selftest failed ACK: 0x%x\n", regval);
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000196 return;
Marc Jonesecd176f2008-09-26 19:15:38 +0000197 }
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000198
Stefan Reinauer094198c2009-07-21 21:24:22 +0000199 if (!kbc_output_buffer_full()) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000200 printk(BIOS_ERR, "Timeout waiting for keyboard after reset.\n");
Stefan Reinauer094198c2009-07-21 21:24:22 +0000201 return;
202 }
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000203
204 regval = inb(KBD_DATA);
Marc Jonesecd176f2008-09-26 19:15:38 +0000205 if (regval != 0xAA) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000206 printk(BIOS_ERR, "Keyboard selftest failed: 0x%x\n", regval);
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000207 return;
Marc Jonesecd176f2008-09-26 19:15:38 +0000208 }
209
210 /*
211 * The following set scancode stuff is what normal BIOS do. It could be
212 * argued that coreboot shouldn't set the scan code.....
213 */
214
215 /* disable the keyboard */
216 regval = send_keyboard(0xF5);
Stefan Reinauer9c3e8b52010-02-28 19:12:37 +0000217 if (regval != KBD_REPLY_ACK) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000218 printk(BIOS_ERR, "Keyboard disable failed ACK: 0x%x\n", regval);
Marc Jonesecd176f2008-09-26 19:15:38 +0000219 return;
220 }
221
222 /* Set scancode command */
223 regval = send_keyboard(0xF0);
Stefan Reinauer9c3e8b52010-02-28 19:12:37 +0000224 if (regval != KBD_REPLY_ACK) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000225 printk(BIOS_ERR, "Keyboard set scancode cmd failed ACK: 0x%x\n", regval);
Marc Jonesecd176f2008-09-26 19:15:38 +0000226 return;
227 }
228 /* Set scancode mode 2 */
229 regval = send_keyboard(0x02);
Stefan Reinauer9c3e8b52010-02-28 19:12:37 +0000230 if (regval != KBD_REPLY_ACK) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000231 printk(BIOS_ERR, "Keyboard set scancode mode failed ACK: 0x%x\n", regval);
Marc Jonesecd176f2008-09-26 19:15:38 +0000232 return;
233 }
234
235 /* enable the keyboard */
236 regval = send_keyboard(0xF4);
Stefan Reinauer9c3e8b52010-02-28 19:12:37 +0000237 if (regval != KBD_REPLY_ACK) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000238 printk(BIOS_ERR, "Keyboard enable failed ACK: 0x%x\n", regval);
Marc Jonesecd176f2008-09-26 19:15:38 +0000239 return;
240 }
241
242 /* All is well - enable keyboard interface */
Marc Jonese59f2e12009-09-29 19:12:23 +0000243 if (!kbc_input_buffer_empty()) return;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000244 outb(0x60, KBD_COMMAND);
Marc Jonese59f2e12009-09-29 19:12:23 +0000245 if (!kbc_input_buffer_empty()) return;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000246 outb(0x61, KBD_DATA); /* send cmd: enable keyboard and IRQ 1 */
Scott Duplichan9c934d12010-09-09 20:37:00 +0000247 if (!kbc_input_buffer_empty()) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000248 printk(BIOS_ERR, "Timeout during final keyboard enable\n");
Marc Jonese59f2e12009-09-29 19:12:23 +0000249 return;
250 }
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000251}
252
Marc Jonesecd176f2008-09-26 19:15:38 +0000253/*
254 * Support PS/2 mode - oddball SIOs(KBC) need this setup
255 * Not well documented. Google - 0xcb keyboard controller
256 * This is called before pc_keyboard_init().
257 */
Stefan Reinauerbbe29ee2009-05-23 22:02:31 +0000258void set_kbc_ps2_mode(void)
Marc Jonesecd176f2008-09-26 19:15:38 +0000259{
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000260 /* Run a keyboard controller self-test */
261 if (!kbc_self_test())
Marc Jonesecd176f2008-09-26 19:15:38 +0000262 return;
Marc Jonesecd176f2008-09-26 19:15:38 +0000263
264 /* Support PS/2 mode */
265 if (!kbc_input_buffer_empty()) return;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000266 outb(0xcb, KBD_COMMAND);
267
Marc Jonesecd176f2008-09-26 19:15:38 +0000268 if (!kbc_input_buffer_empty()) return;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000269 outb(0x01, KBD_DATA);
270
Marc Jonesecd176f2008-09-26 19:15:38 +0000271 kbc_cleanup_buffers();
272}