blob: 5b686170c6d5afc0f47ae56f0d06b2f1936a50b3 [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
Paul Menzela46a7122013-02-23 18:37:27 +010019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Marc Jonesecd176f2008-09-26 19:15:38 +000020 */
21
Eric Biederman5cd81732004-03-11 15:01:31 +000022#include <console/console.h>
23#include <pc80/keyboard.h>
24#include <device/device.h>
Ronald G. Minnichef5f2182003-09-26 15:24:54 +000025#include <arch/io.h>
Stefan Reinauer094198c2009-07-21 21:24:22 +000026#include <delay.h>
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +010027#include <types.h>
Martin Roth08637d32013-07-29 16:39:00 -060028#if CONFIG_HAVE_ACPI_RESUME
29#include <arch/acpi.h>
30#endif
Stefan Reinauer094198c2009-07-21 21:24:22 +000031
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +000032#define KBD_DATA 0x60
33#define KBD_COMMAND 0x64
34#define KBD_STATUS 0x64
Andrew Wue33d6ca2013-10-23 20:47:20 +080035#define KBD_IBF (1 << 1) // 1: input buffer full (data ready for ec)
36#define KBD_OBF (1 << 0) // 1: output buffer full (data ready for host)
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +000037
38// Keyboard Controller Commands
Andrew Wue33d6ca2013-10-23 20:47:20 +080039#define KBC_CMD_READ_COMMAND 0x20 // Read command byte
40#define KBC_CMD_WRITE_COMMAND 0x60 // Write command byte
41#define KBC_CMD_SELF_TEST 0xAA // Controller self-test
42#define KBC_CMD_KBD_TEST 0xAB // Keyboard Interface test
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +000043
44/* The Keyboard controller command byte
45 * BIT | Description
46 * ----+-------------------------------------------------------
47 * 7 | reserved, must be zero
48 * 6 | XT Translation, (1 = on, 0 = off)
49 * 5 | Disable Mouse Port (1 = disable, 0 = enable)
50 * 4 | Disable Keyboard Port (1 = disable, 0 = enable)
51 * 3 | reserved, must be zero
52 * 2 | System Flag (1 = self-test passed. DO NOT SET TO ZERO)
53 * 1 | Mouse Port Interrupts (1 = enable, 0 = disable)
54 * 0 | Keyboard Port Interrupts (1 = enable, 0 = disable)
55 */
56
57// Keyboard Controller Replies
Andrew Wue33d6ca2013-10-23 20:47:20 +080058#define KBC_REPLY_SELFTEST_OK 0x55 // controller self-test succeeded
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +000059
60//
61// Keyboard Replies
62//
Andrew Wue33d6ca2013-10-23 20:47:20 +080063#define KBD_REPLY_POR 0xAA // Power on reset
64#define KBD_REPLY_ACK 0xFA // Command ACK
65#define KBD_REPLY_RESEND 0xFE // Command NACK, send command again
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +000066
Stefan Reinauerd1149d72010-03-24 21:24:17 +000067/* Wait 400ms for keyboard controller answers */
68#define KBC_TIMEOUT_IN_MS 400
Ronald G. Minnich430111b2003-09-26 16:12:23 +000069
Marc Jonesecd176f2008-09-26 19:15:38 +000070static int kbc_input_buffer_empty(void)
Eric Biederman029517c2004-11-11 11:56:00 +000071{
Marc Jonesecd176f2008-09-26 19:15:38 +000072 u32 timeout;
Andrew Wue33d6ca2013-10-23 20:47:20 +080073 for (timeout = KBC_TIMEOUT_IN_MS;
74 timeout && (inb(KBD_STATUS) & KBD_IBF); timeout--)
Stefan Reinauer094198c2009-07-21 21:24:22 +000075 mdelay(1);
Marc Jonesecd176f2008-09-26 19:15:38 +000076
Andrew Wue33d6ca2013-10-23 20:47:20 +080077 if (!timeout)
78 printk(BIOS_WARNING,
79 "Unexpected Keyboard controller input buffer full\n");
Eric Biederman029517c2004-11-11 11:56:00 +000080 return !!timeout;
81}
82
Marc Jonesecd176f2008-09-26 19:15:38 +000083static int kbc_output_buffer_full(void)
Eric Biederman029517c2004-11-11 11:56:00 +000084{
Marc Jonesecd176f2008-09-26 19:15:38 +000085 u32 timeout;
Andrew Wue33d6ca2013-10-23 20:47:20 +080086 for (timeout = KBC_TIMEOUT_IN_MS;
87 timeout && ((inb(KBD_STATUS) & KBD_OBF) == 0); timeout--)
Stefan Reinauer094198c2009-07-21 21:24:22 +000088 mdelay(1);
Marc Jonesecd176f2008-09-26 19:15:38 +000089
Andrew Wue33d6ca2013-10-23 20:47:20 +080090 if (!timeout)
91 printk(BIOS_INFO,
92 "Keyboard controller output buffer result timeout\n");
Eric Biederman029517c2004-11-11 11:56:00 +000093 return !!timeout;
94}
95
Marc Jonesecd176f2008-09-26 19:15:38 +000096static int kbc_cleanup_buffers(void)
97{
98 u32 timeout;
Andrew Wue33d6ca2013-10-23 20:47:20 +080099 for (timeout = KBC_TIMEOUT_IN_MS;
100 timeout && (inb(KBD_STATUS) & (KBD_OBF | KBD_IBF)); timeout--) {
Stefan Reinauer094198c2009-07-21 21:24:22 +0000101 mdelay(1);
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000102 inb(KBD_DATA);
Marc Jonesecd176f2008-09-26 19:15:38 +0000103 }
104
105 if (!timeout) {
Andrew Wue33d6ca2013-10-23 20:47:20 +0800106 printk(BIOS_ERR,
107 "Couldn't cleanup the keyboard controller buffers\n");
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000108 printk(BIOS_ERR, "Status (0x%x): 0x%x, Buffer (0x%x): 0x%x\n",
Andrew Wue33d6ca2013-10-23 20:47:20 +0800109 KBD_STATUS, inb(KBD_STATUS), KBD_DATA, inb(KBD_DATA));
Marc Jonesecd176f2008-09-26 19:15:38 +0000110 }
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000111
Marc Jonesecd176f2008-09-26 19:15:38 +0000112 return !!timeout;
113}
114
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100115static enum cb_err kbc_self_test(void)
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000116{
117 u8 self_test;
118
119 /* Clean up any junk that might have been in the KBC.
120 * Both input and output buffers must be empty.
121 */
122 if (!kbc_cleanup_buffers())
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100123 return CB_KBD_CONTROLLER_FAILURE;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000124
125 /* reset/self test 8042 - send cmd 0xAA */
126 outb(KBC_CMD_SELF_TEST, KBD_COMMAND);
127
128 if (!kbc_output_buffer_full()) {
129 /* There probably is no keyboard controller. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000130 printk(BIOS_ERR, "Could not reset keyboard controller.\n");
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100131 return CB_KBD_CONTROLLER_FAILURE;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000132 }
133
134 /* read self-test result, 0x55 is returned in the output buffer */
135 self_test = inb(KBD_DATA);
136
137 if (self_test != 0x55) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000138 printk(BIOS_ERR, "Keyboard Controller self-test failed: 0x%x\n",
Andrew Wue33d6ca2013-10-23 20:47:20 +0800139 self_test);
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100140 return CB_KBD_CONTROLLER_FAILURE;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000141 }
142
Mathias Krause1c80cf02011-12-14 08:40:48 +0100143 /* ensure the buffers are empty */
144 kbc_cleanup_buffers();
145
146 /* keyboard interface test */
147 outb(KBC_CMD_KBD_TEST, KBD_COMMAND);
148
149 if (!kbc_output_buffer_full()) {
150 printk(BIOS_ERR, "Keyboard Interface test timed out.\n");
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100151 return CB_KBD_CONTROLLER_FAILURE;
Mathias Krause1c80cf02011-12-14 08:40:48 +0100152 }
153
154 /* read test result, 0x00 should be returned in case of no failures */
155 self_test = inb(KBD_DATA);
156
157 if (self_test != 0x00) {
158 printk(BIOS_ERR, "Keyboard Interface test failed: 0x%x\n",
Andrew Wue33d6ca2013-10-23 20:47:20 +0800159 self_test);
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100160 return CB_KBD_INTERFACE_FAILURE;
Mathias Krause1c80cf02011-12-14 08:40:48 +0100161 }
162
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100163 return CB_SUCCESS;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000164}
Marc Jonesecd176f2008-09-26 19:15:38 +0000165
166static u8 send_keyboard(u8 command)
167{
168 u8 regval = 0;
169 u8 resend = 10;
170
171 do {
Andrew Wue33d6ca2013-10-23 20:47:20 +0800172 if (!kbc_input_buffer_empty())
173 return 0;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000174 outb(command, KBD_DATA);
Mathias Krause1c80cf02011-12-14 08:40:48 +0100175 /* the reset command takes much longer then normal commands and
176 * even worse, some keyboards do send the ACK _after_ doing the
177 * reset */
178 if (command == 0xFF) {
179 u8 retries;
Andrew Wue33d6ca2013-10-23 20:47:20 +0800180 for (retries = 9; retries && !kbc_output_buffer_full();
181 retries--) ;
Mathias Krause1c80cf02011-12-14 08:40:48 +0100182 }
Stefan Reinauer094198c2009-07-21 21:24:22 +0000183 if (!kbc_output_buffer_full()) {
Andrew Wue33d6ca2013-10-23 20:47:20 +0800184 printk(BIOS_ERR,
185 "Could not send keyboard command %02x\n",
186 command);
Stefan Reinauer094198c2009-07-21 21:24:22 +0000187 return 0;
188 }
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000189 regval = inb(KBD_DATA);
Marc Jonesecd176f2008-09-26 19:15:38 +0000190 --resend;
Stefan Reinauer9c3e8b52010-02-28 19:12:37 +0000191 } while (regval == KBD_REPLY_RESEND && resend > 0);
Marc Jonesecd176f2008-09-26 19:15:38 +0000192
193 return regval;
194}
195
Stefan Reinauer740b5872010-02-23 20:31:37 +0000196void pc_keyboard_init(struct pc_keyboard *keyboard)
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000197{
Mathias Krause1c80cf02011-12-14 08:40:48 +0100198 u8 retries;
Marc Jonesecd176f2008-09-26 19:15:38 +0000199 u8 regval;
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100200 enum cb_err err;
201
Kevin O'Connor9c35c842010-09-09 08:34:02 +0000202 if (!CONFIG_DRIVERS_PS2_KEYBOARD)
203 return;
Martin Roth08637d32013-07-29 16:39:00 -0600204
205#if CONFIG_HAVE_ACPI_RESUME
206 if (acpi_slp_type == 3)
207 return;
208#endif
209
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000210 printk(BIOS_DEBUG, "Keyboard init...\n");
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000211
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000212 /* Run a keyboard controller self-test */
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100213 err = kbc_self_test();
214 /* Ignore iterface failure as it's non-fatal. */
215 if (err != CB_SUCCESS && err != CB_KBD_INTERFACE_FAILURE)
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000216 return;
Stefan Reinauer094198c2009-07-21 21:24:22 +0000217
218 /* Enable keyboard interface - No IRQ */
Andrew Wue33d6ca2013-10-23 20:47:20 +0800219 if (!kbc_input_buffer_empty())
220 return;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000221 outb(0x60, KBD_COMMAND);
Andrew Wue33d6ca2013-10-23 20:47:20 +0800222 if (!kbc_input_buffer_empty())
223 return;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000224 outb(0x20, KBD_DATA); /* send cmd: enable keyboard */
Marc Jonese59f2e12009-09-29 19:12:23 +0000225 if (!kbc_input_buffer_empty()) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000226 printk(BIOS_INFO, "Timeout while enabling keyboard\n");
Marc Jonese59f2e12009-09-29 19:12:23 +0000227 return;
228 }
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000229
Marc Jonesecd176f2008-09-26 19:15:38 +0000230 /* clean up any junk that might have been in the keyboard */
Andrew Wue33d6ca2013-10-23 20:47:20 +0800231 if (!kbc_cleanup_buffers())
232 return;
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000233
Marc Jonesecd176f2008-09-26 19:15:38 +0000234 /* reset keyboard and self test (keyboard side) */
235 regval = send_keyboard(0xFF);
Stefan Reinauer9c3e8b52010-02-28 19:12:37 +0000236 if (regval == KBD_REPLY_RESEND) {
237 /* keeps sending RESENDs, probably no keyboard. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000238 printk(BIOS_INFO, "No PS/2 keyboard detected.\n");
Stefan Reinauer9c3e8b52010-02-28 19:12:37 +0000239 return;
240 }
241
242 if (regval != KBD_REPLY_ACK) {
Mathias Krause1c80cf02011-12-14 08:40:48 +0100243 printk(BIOS_ERR, "Keyboard reset failed ACK: 0x%x\n", regval);
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000244 return;
Marc Jonesecd176f2008-09-26 19:15:38 +0000245 }
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000246
Mathias Krause1c80cf02011-12-14 08:40:48 +0100247 /* the reset command takes some time, so wait a little longer */
Andrew Wue33d6ca2013-10-23 20:47:20 +0800248 for (retries = 9; retries && !kbc_output_buffer_full(); retries--) ;
Mathias Krause1c80cf02011-12-14 08:40:48 +0100249
Stefan Reinauer094198c2009-07-21 21:24:22 +0000250 if (!kbc_output_buffer_full()) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000251 printk(BIOS_ERR, "Timeout waiting for keyboard after reset.\n");
Stefan Reinauer094198c2009-07-21 21:24:22 +0000252 return;
253 }
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000254
255 regval = inb(KBD_DATA);
Marc Jonesecd176f2008-09-26 19:15:38 +0000256 if (regval != 0xAA) {
Andrew Wue33d6ca2013-10-23 20:47:20 +0800257 printk(BIOS_ERR, "Keyboard reset selftest failed: 0x%x\n",
258 regval);
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000259 return;
Marc Jonesecd176f2008-09-26 19:15:38 +0000260 }
261
262 /*
263 * The following set scancode stuff is what normal BIOS do. It could be
264 * argued that coreboot shouldn't set the scan code.....
265 */
266
267 /* disable the keyboard */
268 regval = send_keyboard(0xF5);
Stefan Reinauer9c3e8b52010-02-28 19:12:37 +0000269 if (regval != KBD_REPLY_ACK) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000270 printk(BIOS_ERR, "Keyboard disable failed ACK: 0x%x\n", regval);
Marc Jonesecd176f2008-09-26 19:15:38 +0000271 return;
272 }
273
274 /* Set scancode command */
275 regval = send_keyboard(0xF0);
Stefan Reinauer9c3e8b52010-02-28 19:12:37 +0000276 if (regval != KBD_REPLY_ACK) {
Andrew Wue33d6ca2013-10-23 20:47:20 +0800277 printk(BIOS_ERR, "Keyboard set scancode cmd failed ACK: 0x%x\n",
278 regval);
Marc Jonesecd176f2008-09-26 19:15:38 +0000279 return;
280 }
281 /* Set scancode mode 2 */
282 regval = send_keyboard(0x02);
Stefan Reinauer9c3e8b52010-02-28 19:12:37 +0000283 if (regval != KBD_REPLY_ACK) {
Andrew Wue33d6ca2013-10-23 20:47:20 +0800284 printk(BIOS_ERR,
285 "Keyboard set scancode mode failed ACK: 0x%x\n", regval);
Marc Jonesecd176f2008-09-26 19:15:38 +0000286 return;
287 }
288
Marc Jonesecd176f2008-09-26 19:15:38 +0000289 /* All is well - enable keyboard interface */
Andrew Wue33d6ca2013-10-23 20:47:20 +0800290 if (!kbc_input_buffer_empty())
291 return;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000292 outb(0x60, KBD_COMMAND);
Andrew Wue33d6ca2013-10-23 20:47:20 +0800293 if (!kbc_input_buffer_empty())
294 return;
Mathias Krause1c80cf02011-12-14 08:40:48 +0100295 outb(0x65, KBD_DATA); /* send cmd: enable keyboard and IRQ 1 */
Scott Duplichan9c934d12010-09-09 20:37:00 +0000296 if (!kbc_input_buffer_empty()) {
Mathias Krause1c80cf02011-12-14 08:40:48 +0100297 printk(BIOS_ERR, "Timeout during keyboard enable\n");
298 return;
299 }
300
301 /* enable the keyboard */
302 regval = send_keyboard(0xF4);
303 if (regval != KBD_REPLY_ACK) {
304 printk(BIOS_ERR, "Keyboard enable failed ACK: 0x%x\n", regval);
Marc Jonese59f2e12009-09-29 19:12:23 +0000305 return;
306 }
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000307}
308
Marc Jonesecd176f2008-09-26 19:15:38 +0000309/*
310 * Support PS/2 mode - oddball SIOs(KBC) need this setup
311 * Not well documented. Google - 0xcb keyboard controller
312 * This is called before pc_keyboard_init().
313 */
Stefan Reinauerbbe29ee2009-05-23 22:02:31 +0000314void set_kbc_ps2_mode(void)
Marc Jonesecd176f2008-09-26 19:15:38 +0000315{
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100316 enum cb_err err;
317
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000318 /* Run a keyboard controller self-test */
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100319 err = kbc_self_test();
320 /* Ignore iterface failure as it's non-fatal. */
321 if (err != CB_SUCCESS && err != CB_KBD_INTERFACE_FAILURE)
Marc Jonesecd176f2008-09-26 19:15:38 +0000322 return;
Marc Jonesecd176f2008-09-26 19:15:38 +0000323
324 /* Support PS/2 mode */
Andrew Wue33d6ca2013-10-23 20:47:20 +0800325 if (!kbc_input_buffer_empty())
326 return;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000327 outb(0xcb, KBD_COMMAND);
328
Andrew Wue33d6ca2013-10-23 20:47:20 +0800329 if (!kbc_input_buffer_empty())
330 return;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000331 outb(0x01, KBD_DATA);
332
Marc Jonesecd176f2008-09-26 19:15:38 +0000333 kbc_cleanup_buffers();
334}