blob: 3e61a51cabdad48c067fb59649c3f045ca52beb3 [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.
Marc Jonesecd176f2008-09-26 19:15:38 +000016 */
17
Eric Biederman5cd81732004-03-11 15:01:31 +000018#include <console/console.h>
19#include <pc80/keyboard.h>
20#include <device/device.h>
Ronald G. Minnichef5f2182003-09-26 15:24:54 +000021#include <arch/io.h>
Stefan Reinauer094198c2009-07-21 21:24:22 +000022#include <delay.h>
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +010023#include <types.h>
Martin Roth08637d32013-07-29 16:39:00 -060024#include <arch/acpi.h>
Stefan Reinauer094198c2009-07-21 21:24:22 +000025
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +000026#define KBD_DATA 0x60
27#define KBD_COMMAND 0x64
28#define KBD_STATUS 0x64
Andrew Wue33d6ca2013-10-23 20:47:20 +080029#define KBD_IBF (1 << 1) // 1: input buffer full (data ready for ec)
30#define KBD_OBF (1 << 0) // 1: output buffer full (data ready for host)
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +000031
32// Keyboard Controller Commands
Andrew Wue33d6ca2013-10-23 20:47:20 +080033#define KBC_CMD_READ_COMMAND 0x20 // Read command byte
34#define KBC_CMD_WRITE_COMMAND 0x60 // Write command byte
35#define KBC_CMD_SELF_TEST 0xAA // Controller self-test
36#define KBC_CMD_KBD_TEST 0xAB // Keyboard Interface test
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +000037
38/* The Keyboard controller command byte
39 * BIT | Description
40 * ----+-------------------------------------------------------
41 * 7 | reserved, must be zero
42 * 6 | XT Translation, (1 = on, 0 = off)
43 * 5 | Disable Mouse Port (1 = disable, 0 = enable)
44 * 4 | Disable Keyboard Port (1 = disable, 0 = enable)
45 * 3 | reserved, must be zero
46 * 2 | System Flag (1 = self-test passed. DO NOT SET TO ZERO)
47 * 1 | Mouse Port Interrupts (1 = enable, 0 = disable)
48 * 0 | Keyboard Port Interrupts (1 = enable, 0 = disable)
49 */
50
51// Keyboard Controller Replies
Andrew Wue33d6ca2013-10-23 20:47:20 +080052#define KBC_REPLY_SELFTEST_OK 0x55 // controller self-test succeeded
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +000053
54//
55// Keyboard Replies
56//
Andrew Wue33d6ca2013-10-23 20:47:20 +080057#define KBD_REPLY_POR 0xAA // Power on reset
58#define KBD_REPLY_ACK 0xFA // Command ACK
59#define KBD_REPLY_RESEND 0xFE // Command NACK, send command again
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +000060
Stefan Reinauerd1149d72010-03-24 21:24:17 +000061/* Wait 400ms for keyboard controller answers */
62#define KBC_TIMEOUT_IN_MS 400
Ronald G. Minnich430111b2003-09-26 16:12:23 +000063
Marc Jonesecd176f2008-09-26 19:15:38 +000064static int kbc_input_buffer_empty(void)
Eric Biederman029517c2004-11-11 11:56:00 +000065{
Marc Jonesecd176f2008-09-26 19:15:38 +000066 u32 timeout;
Andrew Wue33d6ca2013-10-23 20:47:20 +080067 for (timeout = KBC_TIMEOUT_IN_MS;
68 timeout && (inb(KBD_STATUS) & KBD_IBF); timeout--)
Stefan Reinauer094198c2009-07-21 21:24:22 +000069 mdelay(1);
Marc Jonesecd176f2008-09-26 19:15:38 +000070
Andrew Wue33d6ca2013-10-23 20:47:20 +080071 if (!timeout)
72 printk(BIOS_WARNING,
73 "Unexpected Keyboard controller input buffer full\n");
Eric Biederman029517c2004-11-11 11:56:00 +000074 return !!timeout;
75}
76
Marc Jonesecd176f2008-09-26 19:15:38 +000077static int kbc_output_buffer_full(void)
Eric Biederman029517c2004-11-11 11:56:00 +000078{
Marc Jonesecd176f2008-09-26 19:15:38 +000079 u32 timeout;
Andrew Wue33d6ca2013-10-23 20:47:20 +080080 for (timeout = KBC_TIMEOUT_IN_MS;
81 timeout && ((inb(KBD_STATUS) & KBD_OBF) == 0); timeout--)
Stefan Reinauer094198c2009-07-21 21:24:22 +000082 mdelay(1);
Marc Jonesecd176f2008-09-26 19:15:38 +000083
Andrew Wue33d6ca2013-10-23 20:47:20 +080084 if (!timeout)
85 printk(BIOS_INFO,
86 "Keyboard controller output buffer result timeout\n");
Eric Biederman029517c2004-11-11 11:56:00 +000087 return !!timeout;
88}
89
Marc Jonesecd176f2008-09-26 19:15:38 +000090static int kbc_cleanup_buffers(void)
91{
92 u32 timeout;
Andrew Wue33d6ca2013-10-23 20:47:20 +080093 for (timeout = KBC_TIMEOUT_IN_MS;
94 timeout && (inb(KBD_STATUS) & (KBD_OBF | KBD_IBF)); timeout--) {
Stefan Reinauer094198c2009-07-21 21:24:22 +000095 mdelay(1);
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +000096 inb(KBD_DATA);
Marc Jonesecd176f2008-09-26 19:15:38 +000097 }
98
99 if (!timeout) {
Andrew Wue33d6ca2013-10-23 20:47:20 +0800100 printk(BIOS_ERR,
101 "Couldn't cleanup the keyboard controller buffers\n");
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000102 printk(BIOS_ERR, "Status (0x%x): 0x%x, Buffer (0x%x): 0x%x\n",
Andrew Wue33d6ca2013-10-23 20:47:20 +0800103 KBD_STATUS, inb(KBD_STATUS), KBD_DATA, inb(KBD_DATA));
Marc Jonesecd176f2008-09-26 19:15:38 +0000104 }
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000105
Marc Jonesecd176f2008-09-26 19:15:38 +0000106 return !!timeout;
107}
108
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100109static enum cb_err kbc_self_test(void)
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000110{
111 u8 self_test;
112
113 /* Clean up any junk that might have been in the KBC.
114 * Both input and output buffers must be empty.
115 */
116 if (!kbc_cleanup_buffers())
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100117 return CB_KBD_CONTROLLER_FAILURE;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000118
119 /* reset/self test 8042 - send cmd 0xAA */
120 outb(KBC_CMD_SELF_TEST, KBD_COMMAND);
121
122 if (!kbc_output_buffer_full()) {
123 /* There probably is no keyboard controller. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000124 printk(BIOS_ERR, "Could not reset keyboard controller.\n");
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100125 return CB_KBD_CONTROLLER_FAILURE;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000126 }
127
128 /* read self-test result, 0x55 is returned in the output buffer */
129 self_test = inb(KBD_DATA);
130
131 if (self_test != 0x55) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000132 printk(BIOS_ERR, "Keyboard Controller self-test failed: 0x%x\n",
Andrew Wue33d6ca2013-10-23 20:47:20 +0800133 self_test);
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100134 return CB_KBD_CONTROLLER_FAILURE;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000135 }
136
Mathias Krause1c80cf02011-12-14 08:40:48 +0100137 /* ensure the buffers are empty */
138 kbc_cleanup_buffers();
139
140 /* keyboard interface test */
141 outb(KBC_CMD_KBD_TEST, KBD_COMMAND);
142
143 if (!kbc_output_buffer_full()) {
144 printk(BIOS_ERR, "Keyboard Interface test timed out.\n");
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100145 return CB_KBD_CONTROLLER_FAILURE;
Mathias Krause1c80cf02011-12-14 08:40:48 +0100146 }
147
148 /* read test result, 0x00 should be returned in case of no failures */
149 self_test = inb(KBD_DATA);
150
151 if (self_test != 0x00) {
152 printk(BIOS_ERR, "Keyboard Interface test failed: 0x%x\n",
Andrew Wue33d6ca2013-10-23 20:47:20 +0800153 self_test);
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100154 return CB_KBD_INTERFACE_FAILURE;
Mathias Krause1c80cf02011-12-14 08:40:48 +0100155 }
156
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100157 return CB_SUCCESS;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000158}
Marc Jonesecd176f2008-09-26 19:15:38 +0000159
160static u8 send_keyboard(u8 command)
161{
162 u8 regval = 0;
163 u8 resend = 10;
164
165 do {
Andrew Wue33d6ca2013-10-23 20:47:20 +0800166 if (!kbc_input_buffer_empty())
167 return 0;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000168 outb(command, KBD_DATA);
Mathias Krause1c80cf02011-12-14 08:40:48 +0100169 /* the reset command takes much longer then normal commands and
170 * even worse, some keyboards do send the ACK _after_ doing the
171 * reset */
172 if (command == 0xFF) {
173 u8 retries;
Andrew Wue33d6ca2013-10-23 20:47:20 +0800174 for (retries = 9; retries && !kbc_output_buffer_full();
175 retries--) ;
Mathias Krause1c80cf02011-12-14 08:40:48 +0100176 }
Stefan Reinauer094198c2009-07-21 21:24:22 +0000177 if (!kbc_output_buffer_full()) {
Andrew Wue33d6ca2013-10-23 20:47:20 +0800178 printk(BIOS_ERR,
179 "Could not send keyboard command %02x\n",
180 command);
Stefan Reinauer094198c2009-07-21 21:24:22 +0000181 return 0;
182 }
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000183 regval = inb(KBD_DATA);
Marc Jonesecd176f2008-09-26 19:15:38 +0000184 --resend;
Stefan Reinauer9c3e8b52010-02-28 19:12:37 +0000185 } while (regval == KBD_REPLY_RESEND && resend > 0);
Marc Jonesecd176f2008-09-26 19:15:38 +0000186
187 return regval;
188}
189
Edward O'Callaghandef00be2014-04-30 05:01:52 +1000190void pc_keyboard_init(void)
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000191{
Mathias Krause1c80cf02011-12-14 08:40:48 +0100192 u8 retries;
Marc Jonesecd176f2008-09-26 19:15:38 +0000193 u8 regval;
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100194 enum cb_err err;
195
Kevin O'Connor9c35c842010-09-09 08:34:02 +0000196 if (!CONFIG_DRIVERS_PS2_KEYBOARD)
197 return;
Martin Roth08637d32013-07-29 16:39:00 -0600198
Kyösti Mälkki6202aea2014-06-20 05:41:21 +0300199 if (acpi_is_wakeup_s3())
Martin Roth08637d32013-07-29 16:39:00 -0600200 return;
Martin Roth08637d32013-07-29 16:39:00 -0600201
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000202 printk(BIOS_DEBUG, "Keyboard init...\n");
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000203
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000204 /* Run a keyboard controller self-test */
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100205 err = kbc_self_test();
206 /* Ignore iterface failure as it's non-fatal. */
207 if (err != CB_SUCCESS && err != CB_KBD_INTERFACE_FAILURE)
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000208 return;
Stefan Reinauer094198c2009-07-21 21:24:22 +0000209
210 /* Enable keyboard interface - No IRQ */
Andrew Wue33d6ca2013-10-23 20:47:20 +0800211 if (!kbc_input_buffer_empty())
212 return;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000213 outb(0x60, KBD_COMMAND);
Andrew Wue33d6ca2013-10-23 20:47:20 +0800214 if (!kbc_input_buffer_empty())
215 return;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000216 outb(0x20, KBD_DATA); /* send cmd: enable keyboard */
Marc Jonese59f2e12009-09-29 19:12:23 +0000217 if (!kbc_input_buffer_empty()) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000218 printk(BIOS_INFO, "Timeout while enabling keyboard\n");
Marc Jonese59f2e12009-09-29 19:12:23 +0000219 return;
220 }
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000221
Marc Jonesecd176f2008-09-26 19:15:38 +0000222 /* clean up any junk that might have been in the keyboard */
Andrew Wue33d6ca2013-10-23 20:47:20 +0800223 if (!kbc_cleanup_buffers())
224 return;
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000225
Marc Jonesecd176f2008-09-26 19:15:38 +0000226 /* reset keyboard and self test (keyboard side) */
227 regval = send_keyboard(0xFF);
Stefan Reinauer9c3e8b52010-02-28 19:12:37 +0000228 if (regval == KBD_REPLY_RESEND) {
229 /* keeps sending RESENDs, probably no keyboard. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000230 printk(BIOS_INFO, "No PS/2 keyboard detected.\n");
Stefan Reinauer9c3e8b52010-02-28 19:12:37 +0000231 return;
232 }
233
234 if (regval != KBD_REPLY_ACK) {
Mathias Krause1c80cf02011-12-14 08:40:48 +0100235 printk(BIOS_ERR, "Keyboard reset failed ACK: 0x%x\n", regval);
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000236 return;
Marc Jonesecd176f2008-09-26 19:15:38 +0000237 }
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000238
Mathias Krause1c80cf02011-12-14 08:40:48 +0100239 /* the reset command takes some time, so wait a little longer */
Andrew Wue33d6ca2013-10-23 20:47:20 +0800240 for (retries = 9; retries && !kbc_output_buffer_full(); retries--) ;
Mathias Krause1c80cf02011-12-14 08:40:48 +0100241
Stefan Reinauer094198c2009-07-21 21:24:22 +0000242 if (!kbc_output_buffer_full()) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000243 printk(BIOS_ERR, "Timeout waiting for keyboard after reset.\n");
Stefan Reinauer094198c2009-07-21 21:24:22 +0000244 return;
245 }
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000246
247 regval = inb(KBD_DATA);
Marc Jonesecd176f2008-09-26 19:15:38 +0000248 if (regval != 0xAA) {
Andrew Wue33d6ca2013-10-23 20:47:20 +0800249 printk(BIOS_ERR, "Keyboard reset selftest failed: 0x%x\n",
250 regval);
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000251 return;
Marc Jonesecd176f2008-09-26 19:15:38 +0000252 }
253
254 /*
255 * The following set scancode stuff is what normal BIOS do. It could be
256 * argued that coreboot shouldn't set the scan code.....
257 */
258
259 /* disable the keyboard */
260 regval = send_keyboard(0xF5);
Stefan Reinauer9c3e8b52010-02-28 19:12:37 +0000261 if (regval != KBD_REPLY_ACK) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000262 printk(BIOS_ERR, "Keyboard disable failed ACK: 0x%x\n", regval);
Marc Jonesecd176f2008-09-26 19:15:38 +0000263 return;
264 }
265
266 /* Set scancode command */
267 regval = send_keyboard(0xF0);
Stefan Reinauer9c3e8b52010-02-28 19:12:37 +0000268 if (regval != KBD_REPLY_ACK) {
Andrew Wue33d6ca2013-10-23 20:47:20 +0800269 printk(BIOS_ERR, "Keyboard set scancode cmd failed ACK: 0x%x\n",
270 regval);
Marc Jonesecd176f2008-09-26 19:15:38 +0000271 return;
272 }
273 /* Set scancode mode 2 */
274 regval = send_keyboard(0x02);
Stefan Reinauer9c3e8b52010-02-28 19:12:37 +0000275 if (regval != KBD_REPLY_ACK) {
Andrew Wue33d6ca2013-10-23 20:47:20 +0800276 printk(BIOS_ERR,
277 "Keyboard set scancode mode failed ACK: 0x%x\n", regval);
Marc Jonesecd176f2008-09-26 19:15:38 +0000278 return;
279 }
280
Marc Jonesecd176f2008-09-26 19:15:38 +0000281 /* All is well - enable keyboard interface */
Andrew Wue33d6ca2013-10-23 20:47:20 +0800282 if (!kbc_input_buffer_empty())
283 return;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000284 outb(0x60, KBD_COMMAND);
Andrew Wue33d6ca2013-10-23 20:47:20 +0800285 if (!kbc_input_buffer_empty())
286 return;
Mathias Krause1c80cf02011-12-14 08:40:48 +0100287 outb(0x65, KBD_DATA); /* send cmd: enable keyboard and IRQ 1 */
Scott Duplichan9c934d12010-09-09 20:37:00 +0000288 if (!kbc_input_buffer_empty()) {
Mathias Krause1c80cf02011-12-14 08:40:48 +0100289 printk(BIOS_ERR, "Timeout during keyboard enable\n");
290 return;
291 }
292
293 /* enable the keyboard */
294 regval = send_keyboard(0xF4);
295 if (regval != KBD_REPLY_ACK) {
296 printk(BIOS_ERR, "Keyboard enable failed ACK: 0x%x\n", regval);
Marc Jonese59f2e12009-09-29 19:12:23 +0000297 return;
298 }
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000299}
300
Marc Jonesecd176f2008-09-26 19:15:38 +0000301/*
302 * Support PS/2 mode - oddball SIOs(KBC) need this setup
303 * Not well documented. Google - 0xcb keyboard controller
304 * This is called before pc_keyboard_init().
305 */
Stefan Reinauerbbe29ee2009-05-23 22:02:31 +0000306void set_kbc_ps2_mode(void)
Marc Jonesecd176f2008-09-26 19:15:38 +0000307{
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100308 enum cb_err err;
309
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000310 /* Run a keyboard controller self-test */
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100311 err = kbc_self_test();
312 /* Ignore iterface failure as it's non-fatal. */
313 if (err != CB_SUCCESS && err != CB_KBD_INTERFACE_FAILURE)
Marc Jonesecd176f2008-09-26 19:15:38 +0000314 return;
Marc Jonesecd176f2008-09-26 19:15:38 +0000315
316 /* Support PS/2 mode */
Andrew Wue33d6ca2013-10-23 20:47:20 +0800317 if (!kbc_input_buffer_empty())
318 return;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000319 outb(0xcb, KBD_COMMAND);
320
Andrew Wue33d6ca2013-10-23 20:47:20 +0800321 if (!kbc_input_buffer_empty())
322 return;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000323 outb(0x01, KBD_DATA);
324
Marc Jonesecd176f2008-09-26 19:15:38 +0000325 kbc_cleanup_buffers();
326}