blob: e389329710dd972b75c62d3fc71d857b2e6edae8 [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
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>
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
35#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)
37
38// Keyboard Controller Commands
39#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
Mathias Krause1c80cf02011-12-14 08:40:48 +010042#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
58#define KBC_REPLY_SELFTEST_OK 0x55 // controller self-test succeeded
59
60//
61// Keyboard Replies
62//
63#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
66
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;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +000073 for(timeout = KBC_TIMEOUT_IN_MS; timeout && (inb(KBD_STATUS) & KBD_IBF); timeout--) {
Stefan Reinauer094198c2009-07-21 21:24:22 +000074 mdelay(1);
Marc Jonesecd176f2008-09-26 19:15:38 +000075 }
76
77 if (!timeout) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000078 printk(BIOS_WARNING, "Unexpected Keyboard controller input buffer full\n");
Eric Biederman029517c2004-11-11 11:56:00 +000079 }
80 return !!timeout;
81}
82
Marc Jonesecd176f2008-09-26 19:15:38 +000083
84static int kbc_output_buffer_full(void)
Eric Biederman029517c2004-11-11 11:56:00 +000085{
Marc Jonesecd176f2008-09-26 19:15:38 +000086 u32 timeout;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +000087 for(timeout = KBC_TIMEOUT_IN_MS; 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 }
90
91 if (!timeout) {
Stefan Reinauerd1149d72010-03-24 21:24:17 +000092 printk(BIOS_INFO, "Keyboard controller output buffer result timeout\n");
Eric Biederman029517c2004-11-11 11:56:00 +000093 }
94 return !!timeout;
95}
96
Marc Jonesecd176f2008-09-26 19:15:38 +000097
98static int kbc_cleanup_buffers(void)
99{
100 u32 timeout;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000101 for(timeout = KBC_TIMEOUT_IN_MS; timeout && (inb(KBD_STATUS) & (KBD_OBF | KBD_IBF)); timeout--) {
Stefan Reinauer094198c2009-07-21 21:24:22 +0000102 mdelay(1);
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000103 inb(KBD_DATA);
Marc Jonesecd176f2008-09-26 19:15:38 +0000104 }
105
106 if (!timeout) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000107 printk(BIOS_ERR, "Couldn't cleanup the keyboard controller buffers\n");
108 printk(BIOS_ERR, "Status (0x%x): 0x%x, Buffer (0x%x): 0x%x\n",
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000109 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
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000115static int kbc_self_test(void)
116{
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())
123 return 0;
124
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");
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000131 return 0;
132 }
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",
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000139 self_test);
140 return 0;
141 }
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");
151 return 0;
152 }
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",
159 self_test);
160 return 0;
161 }
162
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000163 return 1;
164}
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 {
172 if (!kbc_input_buffer_empty()) return 0;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000173 outb(command, KBD_DATA);
Mathias Krause1c80cf02011-12-14 08:40:48 +0100174 /* the reset command takes much longer then normal commands and
175 * even worse, some keyboards do send the ACK _after_ doing the
176 * reset */
177 if (command == 0xFF) {
178 u8 retries;
179 for (retries = 9; retries && !kbc_output_buffer_full(); retries--)
180 ;
181 }
Stefan Reinauer094198c2009-07-21 21:24:22 +0000182 if (!kbc_output_buffer_full()) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000183 printk(BIOS_ERR, "Could not send keyboard command %02x\n",
Stefan Reinauer094198c2009-07-21 21:24:22 +0000184 command);
185 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
Stefan Reinauer740b5872010-02-23 20:31:37 +0000194void pc_keyboard_init(struct pc_keyboard *keyboard)
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;
Kevin O'Connor9c35c842010-09-09 08:34:02 +0000198 if (!CONFIG_DRIVERS_PS2_KEYBOARD)
199 return;
Martin Roth08637d32013-07-29 16:39:00 -0600200
201#if CONFIG_HAVE_ACPI_RESUME
202 if (acpi_slp_type == 3)
203 return;
204#endif
205
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 */
209 if (!kbc_self_test())
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000210 return;
Stefan Reinauer094198c2009-07-21 21:24:22 +0000211
212 /* Enable keyboard interface - No IRQ */
Marc Jonese59f2e12009-09-29 19:12:23 +0000213 if (!kbc_input_buffer_empty()) return;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000214 outb(0x60, KBD_COMMAND);
Marc Jonese59f2e12009-09-29 19:12:23 +0000215 if (!kbc_input_buffer_empty()) 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 */
223 if (!kbc_cleanup_buffers()) return;
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000224
Marc Jonesecd176f2008-09-26 19:15:38 +0000225 /* reset keyboard and self test (keyboard side) */
226 regval = send_keyboard(0xFF);
Stefan Reinauer9c3e8b52010-02-28 19:12:37 +0000227 if (regval == KBD_REPLY_RESEND) {
228 /* keeps sending RESENDs, probably no keyboard. */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000229 printk(BIOS_INFO, "No PS/2 keyboard detected.\n");
Stefan Reinauer9c3e8b52010-02-28 19:12:37 +0000230 return;
231 }
232
233 if (regval != KBD_REPLY_ACK) {
Mathias Krause1c80cf02011-12-14 08:40:48 +0100234 printk(BIOS_ERR, "Keyboard reset failed ACK: 0x%x\n", regval);
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000235 return;
Marc Jonesecd176f2008-09-26 19:15:38 +0000236 }
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000237
Mathias Krause1c80cf02011-12-14 08:40:48 +0100238 /* the reset command takes some time, so wait a little longer */
239 for (retries = 9; retries && !kbc_output_buffer_full(); retries--)
240 ;
241
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) {
Mathias Krause1c80cf02011-12-14 08:40:48 +0100249 printk(BIOS_ERR, "Keyboard reset selftest failed: 0x%x\n", regval);
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000250 return;
Marc Jonesecd176f2008-09-26 19:15:38 +0000251 }
252
253 /*
254 * The following set scancode stuff is what normal BIOS do. It could be
255 * argued that coreboot shouldn't set the scan code.....
256 */
257
258 /* disable the keyboard */
259 regval = send_keyboard(0xF5);
Stefan Reinauer9c3e8b52010-02-28 19:12:37 +0000260 if (regval != KBD_REPLY_ACK) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000261 printk(BIOS_ERR, "Keyboard disable failed ACK: 0x%x\n", regval);
Marc Jonesecd176f2008-09-26 19:15:38 +0000262 return;
263 }
264
265 /* Set scancode command */
266 regval = send_keyboard(0xF0);
Stefan Reinauer9c3e8b52010-02-28 19:12:37 +0000267 if (regval != KBD_REPLY_ACK) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000268 printk(BIOS_ERR, "Keyboard set scancode cmd failed ACK: 0x%x\n", regval);
Marc Jonesecd176f2008-09-26 19:15:38 +0000269 return;
270 }
271 /* Set scancode mode 2 */
272 regval = send_keyboard(0x02);
Stefan Reinauer9c3e8b52010-02-28 19:12:37 +0000273 if (regval != KBD_REPLY_ACK) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000274 printk(BIOS_ERR, "Keyboard set scancode mode failed ACK: 0x%x\n", regval);
Marc Jonesecd176f2008-09-26 19:15:38 +0000275 return;
276 }
277
Marc Jonesecd176f2008-09-26 19:15:38 +0000278 /* All is well - enable keyboard interface */
Marc Jonese59f2e12009-09-29 19:12:23 +0000279 if (!kbc_input_buffer_empty()) return;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000280 outb(0x60, KBD_COMMAND);
Marc Jonese59f2e12009-09-29 19:12:23 +0000281 if (!kbc_input_buffer_empty()) return;
Mathias Krause1c80cf02011-12-14 08:40:48 +0100282 outb(0x65, KBD_DATA); /* send cmd: enable keyboard and IRQ 1 */
Scott Duplichan9c934d12010-09-09 20:37:00 +0000283 if (!kbc_input_buffer_empty()) {
Mathias Krause1c80cf02011-12-14 08:40:48 +0100284 printk(BIOS_ERR, "Timeout during keyboard enable\n");
285 return;
286 }
287
288 /* enable the keyboard */
289 regval = send_keyboard(0xF4);
290 if (regval != KBD_REPLY_ACK) {
291 printk(BIOS_ERR, "Keyboard enable failed ACK: 0x%x\n", regval);
Marc Jonese59f2e12009-09-29 19:12:23 +0000292 return;
293 }
Ronald G. Minnichef5f2182003-09-26 15:24:54 +0000294}
295
Marc Jonesecd176f2008-09-26 19:15:38 +0000296/*
297 * Support PS/2 mode - oddball SIOs(KBC) need this setup
298 * Not well documented. Google - 0xcb keyboard controller
299 * This is called before pc_keyboard_init().
300 */
Stefan Reinauerbbe29ee2009-05-23 22:02:31 +0000301void set_kbc_ps2_mode(void)
Marc Jonesecd176f2008-09-26 19:15:38 +0000302{
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000303 /* Run a keyboard controller self-test */
304 if (!kbc_self_test())
Marc Jonesecd176f2008-09-26 19:15:38 +0000305 return;
Marc Jonesecd176f2008-09-26 19:15:38 +0000306
307 /* Support PS/2 mode */
308 if (!kbc_input_buffer_empty()) return;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000309 outb(0xcb, KBD_COMMAND);
310
Marc Jonesecd176f2008-09-26 19:15:38 +0000311 if (!kbc_input_buffer_empty()) return;
Stefan Reinauerdd3e82a2009-10-24 18:15:07 +0000312 outb(0x01, KBD_DATA);
313
Marc Jonesecd176f2008-09-26 19:15:38 +0000314 kbc_cleanup_buffers();
315}