blob: 06eae463e152fb306709fc2ab653854aed4bb4e3 [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#include <arch/acpi.h>
Stefan Reinauer094198c2009-07-21 21:24:22 +000029
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +000030#define KBD_DATA 0x60
31#define KBD_COMMAND 0x64
32#define KBD_STATUS 0x64
Andrew Wue33d6ca2013-10-23 20:47:20 +080033#define KBD_IBF (1 << 1) // 1: input buffer full (data ready for ec)
34#define KBD_OBF (1 << 0) // 1: output buffer full (data ready for host)
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +000035
36// Keyboard Controller Commands
Andrew Wue33d6ca2013-10-23 20:47:20 +080037#define KBC_CMD_READ_COMMAND 0x20 // Read command byte
38#define KBC_CMD_WRITE_COMMAND 0x60 // Write command byte
39#define KBC_CMD_SELF_TEST 0xAA // Controller self-test
40#define KBC_CMD_KBD_TEST 0xAB // Keyboard Interface test
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +000041
42/* The Keyboard controller command byte
43 * BIT | Description
44 * ----+-------------------------------------------------------
45 * 7 | reserved, must be zero
46 * 6 | XT Translation, (1 = on, 0 = off)
47 * 5 | Disable Mouse Port (1 = disable, 0 = enable)
48 * 4 | Disable Keyboard Port (1 = disable, 0 = enable)
49 * 3 | reserved, must be zero
50 * 2 | System Flag (1 = self-test passed. DO NOT SET TO ZERO)
51 * 1 | Mouse Port Interrupts (1 = enable, 0 = disable)
52 * 0 | Keyboard Port Interrupts (1 = enable, 0 = disable)
53 */
54
55// Keyboard Controller Replies
Andrew Wue33d6ca2013-10-23 20:47:20 +080056#define KBC_REPLY_SELFTEST_OK 0x55 // controller self-test succeeded
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +000057
58//
59// Keyboard Replies
60//
Andrew Wue33d6ca2013-10-23 20:47:20 +080061#define KBD_REPLY_POR 0xAA // Power on reset
62#define KBD_REPLY_ACK 0xFA // Command ACK
63#define KBD_REPLY_RESEND 0xFE // Command NACK, send command again
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +000064
Stefan Reinauerd1149d72010-03-24 21:24:17 +000065/* Wait 400ms for keyboard controller answers */
66#define KBC_TIMEOUT_IN_MS 400
Ronald G. Minnich430111b2003-09-26 16:12:23 +000067
Marc Jonesecd176f2008-09-26 19:15:38 +000068static int kbc_input_buffer_empty(void)
Eric Biederman029517c2004-11-11 11:56:00 +000069{
Marc Jonesecd176f2008-09-26 19:15:38 +000070 u32 timeout;
Andrew Wue33d6ca2013-10-23 20:47:20 +080071 for (timeout = KBC_TIMEOUT_IN_MS;
72 timeout && (inb(KBD_STATUS) & KBD_IBF); timeout--)
Stefan Reinauer094198c2009-07-21 21:24:22 +000073 mdelay(1);
Marc Jonesecd176f2008-09-26 19:15:38 +000074
Andrew Wue33d6ca2013-10-23 20:47:20 +080075 if (!timeout)
76 printk(BIOS_WARNING,
77 "Unexpected Keyboard controller input buffer full\n");
Eric Biederman029517c2004-11-11 11:56:00 +000078 return !!timeout;
79}
80
Marc Jonesecd176f2008-09-26 19:15:38 +000081static int kbc_output_buffer_full(void)
Eric Biederman029517c2004-11-11 11:56:00 +000082{
Marc Jonesecd176f2008-09-26 19:15:38 +000083 u32 timeout;
Andrew Wue33d6ca2013-10-23 20:47:20 +080084 for (timeout = KBC_TIMEOUT_IN_MS;
85 timeout && ((inb(KBD_STATUS) & KBD_OBF) == 0); timeout--)
Stefan Reinauer094198c2009-07-21 21:24:22 +000086 mdelay(1);
Marc Jonesecd176f2008-09-26 19:15:38 +000087
Andrew Wue33d6ca2013-10-23 20:47:20 +080088 if (!timeout)
89 printk(BIOS_INFO,
90 "Keyboard controller output buffer result timeout\n");
Eric Biederman029517c2004-11-11 11:56:00 +000091 return !!timeout;
92}
93
Marc Jonesecd176f2008-09-26 19:15:38 +000094static int kbc_cleanup_buffers(void)
95{
96 u32 timeout;
Andrew Wue33d6ca2013-10-23 20:47:20 +080097 for (timeout = KBC_TIMEOUT_IN_MS;
98 timeout && (inb(KBD_STATUS) & (KBD_OBF | KBD_IBF)); timeout--) {
Stefan Reinauer094198c2009-07-21 21:24:22 +000099 mdelay(1);
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000100 inb(KBD_DATA);
Marc Jonesecd176f2008-09-26 19:15:38 +0000101 }
102
103 if (!timeout) {
Andrew Wue33d6ca2013-10-23 20:47:20 +0800104 printk(BIOS_ERR,
105 "Couldn't cleanup the keyboard controller buffers\n");
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000106 printk(BIOS_ERR, "Status (0x%x): 0x%x, Buffer (0x%x): 0x%x\n",
Andrew Wue33d6ca2013-10-23 20:47:20 +0800107 KBD_STATUS, inb(KBD_STATUS), KBD_DATA, inb(KBD_DATA));
Marc Jonesecd176f2008-09-26 19:15:38 +0000108 }
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000109
Marc Jonesecd176f2008-09-26 19:15:38 +0000110 return !!timeout;
111}
112
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100113static enum cb_err kbc_self_test(void)
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000114{
115 u8 self_test;
116
117 /* Clean up any junk that might have been in the KBC.
118 * Both input and output buffers must be empty.
119 */
120 if (!kbc_cleanup_buffers())
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100121 return CB_KBD_CONTROLLER_FAILURE;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000122
123 /* reset/self test 8042 - send cmd 0xAA */
124 outb(KBC_CMD_SELF_TEST, KBD_COMMAND);
125
126 if (!kbc_output_buffer_full()) {
127 /* There probably is no keyboard controller. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000128 printk(BIOS_ERR, "Could not reset keyboard controller.\n");
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100129 return CB_KBD_CONTROLLER_FAILURE;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000130 }
131
132 /* read self-test result, 0x55 is returned in the output buffer */
133 self_test = inb(KBD_DATA);
134
135 if (self_test != 0x55) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000136 printk(BIOS_ERR, "Keyboard Controller self-test failed: 0x%x\n",
Andrew Wue33d6ca2013-10-23 20:47:20 +0800137 self_test);
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100138 return CB_KBD_CONTROLLER_FAILURE;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000139 }
140
Mathias Krause1c80cf02011-12-14 08:40:48 +0100141 /* ensure the buffers are empty */
142 kbc_cleanup_buffers();
143
144 /* keyboard interface test */
145 outb(KBC_CMD_KBD_TEST, KBD_COMMAND);
146
147 if (!kbc_output_buffer_full()) {
148 printk(BIOS_ERR, "Keyboard Interface test timed out.\n");
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100149 return CB_KBD_CONTROLLER_FAILURE;
Mathias Krause1c80cf02011-12-14 08:40:48 +0100150 }
151
152 /* read test result, 0x00 should be returned in case of no failures */
153 self_test = inb(KBD_DATA);
154
155 if (self_test != 0x00) {
156 printk(BIOS_ERR, "Keyboard Interface test failed: 0x%x\n",
Andrew Wue33d6ca2013-10-23 20:47:20 +0800157 self_test);
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100158 return CB_KBD_INTERFACE_FAILURE;
Mathias Krause1c80cf02011-12-14 08:40:48 +0100159 }
160
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100161 return CB_SUCCESS;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000162}
Marc Jonesecd176f2008-09-26 19:15:38 +0000163
164static u8 send_keyboard(u8 command)
165{
166 u8 regval = 0;
167 u8 resend = 10;
168
169 do {
Andrew Wue33d6ca2013-10-23 20:47:20 +0800170 if (!kbc_input_buffer_empty())
171 return 0;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000172 outb(command, KBD_DATA);
Mathias Krause1c80cf02011-12-14 08:40:48 +0100173 /* the reset command takes much longer then normal commands and
174 * even worse, some keyboards do send the ACK _after_ doing the
175 * reset */
176 if (command == 0xFF) {
177 u8 retries;
Andrew Wue33d6ca2013-10-23 20:47:20 +0800178 for (retries = 9; retries && !kbc_output_buffer_full();
179 retries--) ;
Mathias Krause1c80cf02011-12-14 08:40:48 +0100180 }
Stefan Reinauer094198c2009-07-21 21:24:22 +0000181 if (!kbc_output_buffer_full()) {
Andrew Wue33d6ca2013-10-23 20:47:20 +0800182 printk(BIOS_ERR,
183 "Could not send keyboard command %02x\n",
184 command);
Stefan Reinauer094198c2009-07-21 21:24:22 +0000185 return 0;
186 }
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000187 regval = inb(KBD_DATA);
Marc Jonesecd176f2008-09-26 19:15:38 +0000188 --resend;
Stefan Reinauer9c3e8b52010-02-28 19:12:37 +0000189 } while (regval == KBD_REPLY_RESEND && resend > 0);
Marc Jonesecd176f2008-09-26 19:15:38 +0000190
191 return regval;
192}
193
Edward O'Callaghandef00be2014-04-30 05:01:52 +1000194void pc_keyboard_init(void)
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000195{
Mathias Krause1c80cf02011-12-14 08:40:48 +0100196 u8 retries;
Marc Jonesecd176f2008-09-26 19:15:38 +0000197 u8 regval;
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100198 enum cb_err err;
199
Kevin O'Connor9c35c842010-09-09 08:34:02 +0000200 if (!CONFIG_DRIVERS_PS2_KEYBOARD)
201 return;
Martin Roth08637d32013-07-29 16:39:00 -0600202
Kyösti Mälkki6202aea2014-06-20 05:41:21 +0300203 if (acpi_is_wakeup_s3())
Martin Roth08637d32013-07-29 16:39:00 -0600204 return;
Martin Roth08637d32013-07-29 16:39:00 -0600205
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000206 printk(BIOS_DEBUG, "Keyboard init...\n");
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000207
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000208 /* Run a keyboard controller self-test */
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100209 err = kbc_self_test();
210 /* Ignore iterface failure as it's non-fatal. */
211 if (err != CB_SUCCESS && err != CB_KBD_INTERFACE_FAILURE)
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000212 return;
Stefan Reinauer094198c2009-07-21 21:24:22 +0000213
214 /* Enable keyboard interface - No IRQ */
Andrew Wue33d6ca2013-10-23 20:47:20 +0800215 if (!kbc_input_buffer_empty())
216 return;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000217 outb(0x60, KBD_COMMAND);
Andrew Wue33d6ca2013-10-23 20:47:20 +0800218 if (!kbc_input_buffer_empty())
219 return;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000220 outb(0x20, KBD_DATA); /* send cmd: enable keyboard */
Marc Jonese59f2e12009-09-29 19:12:23 +0000221 if (!kbc_input_buffer_empty()) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000222 printk(BIOS_INFO, "Timeout while enabling keyboard\n");
Marc Jonese59f2e12009-09-29 19:12:23 +0000223 return;
224 }
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000225
Marc Jonesecd176f2008-09-26 19:15:38 +0000226 /* clean up any junk that might have been in the keyboard */
Andrew Wue33d6ca2013-10-23 20:47:20 +0800227 if (!kbc_cleanup_buffers())
228 return;
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000229
Marc Jonesecd176f2008-09-26 19:15:38 +0000230 /* reset keyboard and self test (keyboard side) */
231 regval = send_keyboard(0xFF);
Stefan Reinauer9c3e8b52010-02-28 19:12:37 +0000232 if (regval == KBD_REPLY_RESEND) {
233 /* keeps sending RESENDs, probably no keyboard. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000234 printk(BIOS_INFO, "No PS/2 keyboard detected.\n");
Stefan Reinauer9c3e8b52010-02-28 19:12:37 +0000235 return;
236 }
237
238 if (regval != KBD_REPLY_ACK) {
Mathias Krause1c80cf02011-12-14 08:40:48 +0100239 printk(BIOS_ERR, "Keyboard reset failed ACK: 0x%x\n", regval);
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000240 return;
Marc Jonesecd176f2008-09-26 19:15:38 +0000241 }
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000242
Mathias Krause1c80cf02011-12-14 08:40:48 +0100243 /* the reset command takes some time, so wait a little longer */
Andrew Wue33d6ca2013-10-23 20:47:20 +0800244 for (retries = 9; retries && !kbc_output_buffer_full(); retries--) ;
Mathias Krause1c80cf02011-12-14 08:40:48 +0100245
Stefan Reinauer094198c2009-07-21 21:24:22 +0000246 if (!kbc_output_buffer_full()) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000247 printk(BIOS_ERR, "Timeout waiting for keyboard after reset.\n");
Stefan Reinauer094198c2009-07-21 21:24:22 +0000248 return;
249 }
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000250
251 regval = inb(KBD_DATA);
Marc Jonesecd176f2008-09-26 19:15:38 +0000252 if (regval != 0xAA) {
Andrew Wue33d6ca2013-10-23 20:47:20 +0800253 printk(BIOS_ERR, "Keyboard reset selftest failed: 0x%x\n",
254 regval);
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000255 return;
Marc Jonesecd176f2008-09-26 19:15:38 +0000256 }
257
258 /*
259 * The following set scancode stuff is what normal BIOS do. It could be
260 * argued that coreboot shouldn't set the scan code.....
261 */
262
263 /* disable the keyboard */
264 regval = send_keyboard(0xF5);
Stefan Reinauer9c3e8b52010-02-28 19:12:37 +0000265 if (regval != KBD_REPLY_ACK) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000266 printk(BIOS_ERR, "Keyboard disable failed ACK: 0x%x\n", regval);
Marc Jonesecd176f2008-09-26 19:15:38 +0000267 return;
268 }
269
270 /* Set scancode command */
271 regval = send_keyboard(0xF0);
Stefan Reinauer9c3e8b52010-02-28 19:12:37 +0000272 if (regval != KBD_REPLY_ACK) {
Andrew Wue33d6ca2013-10-23 20:47:20 +0800273 printk(BIOS_ERR, "Keyboard set scancode cmd failed ACK: 0x%x\n",
274 regval);
Marc Jonesecd176f2008-09-26 19:15:38 +0000275 return;
276 }
277 /* Set scancode mode 2 */
278 regval = send_keyboard(0x02);
Stefan Reinauer9c3e8b52010-02-28 19:12:37 +0000279 if (regval != KBD_REPLY_ACK) {
Andrew Wue33d6ca2013-10-23 20:47:20 +0800280 printk(BIOS_ERR,
281 "Keyboard set scancode mode failed ACK: 0x%x\n", regval);
Marc Jonesecd176f2008-09-26 19:15:38 +0000282 return;
283 }
284
Marc Jonesecd176f2008-09-26 19:15:38 +0000285 /* All is well - enable keyboard interface */
Andrew Wue33d6ca2013-10-23 20:47:20 +0800286 if (!kbc_input_buffer_empty())
287 return;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000288 outb(0x60, KBD_COMMAND);
Andrew Wue33d6ca2013-10-23 20:47:20 +0800289 if (!kbc_input_buffer_empty())
290 return;
Mathias Krause1c80cf02011-12-14 08:40:48 +0100291 outb(0x65, KBD_DATA); /* send cmd: enable keyboard and IRQ 1 */
Scott Duplichan9c934d12010-09-09 20:37:00 +0000292 if (!kbc_input_buffer_empty()) {
Mathias Krause1c80cf02011-12-14 08:40:48 +0100293 printk(BIOS_ERR, "Timeout during keyboard enable\n");
294 return;
295 }
296
297 /* enable the keyboard */
298 regval = send_keyboard(0xF4);
299 if (regval != KBD_REPLY_ACK) {
300 printk(BIOS_ERR, "Keyboard enable failed ACK: 0x%x\n", regval);
Marc Jonese59f2e12009-09-29 19:12:23 +0000301 return;
302 }
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000303}
304
Marc Jonesecd176f2008-09-26 19:15:38 +0000305/*
306 * Support PS/2 mode - oddball SIOs(KBC) need this setup
307 * Not well documented. Google - 0xcb keyboard controller
308 * This is called before pc_keyboard_init().
309 */
Stefan Reinauerbbe29ee2009-05-23 22:02:31 +0000310void set_kbc_ps2_mode(void)
Marc Jonesecd176f2008-09-26 19:15:38 +0000311{
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100312 enum cb_err err;
313
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000314 /* Run a keyboard controller self-test */
Vladimir Serbinenko63e35f22014-01-26 03:55:01 +0100315 err = kbc_self_test();
316 /* Ignore iterface failure as it's non-fatal. */
317 if (err != CB_SUCCESS && err != CB_KBD_INTERFACE_FAILURE)
Marc Jonesecd176f2008-09-26 19:15:38 +0000318 return;
Marc Jonesecd176f2008-09-26 19:15:38 +0000319
320 /* Support PS/2 mode */
Andrew Wue33d6ca2013-10-23 20:47:20 +0800321 if (!kbc_input_buffer_empty())
322 return;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000323 outb(0xcb, KBD_COMMAND);
324
Andrew Wue33d6ca2013-10-23 20:47:20 +0800325 if (!kbc_input_buffer_empty())
326 return;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000327 outb(0x01, KBD_DATA);
328
Marc Jonesecd176f2008-09-26 19:15:38 +0000329 kbc_cleanup_buffers();
330}