A keyboard controller fix to stop the code from waiting for a code that never
[coreboot.git] / src / pc80 / keyboard.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2009 coresystems GmbH
5  * Copyright (C) 2008 Advanced Micro Devices, Inc.
6  * Copyright (C) 2003 Ollie Lo <ollielo@hotmail.com>
7  *
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
23 #include <console/console.h>
24 #include <pc80/keyboard.h>
25 #include <device/device.h>
26 #include <arch/io.h>
27 #include <delay.h>
28
29 /* Wait 200ms for keyboard controller answers */
30 #define KBC_TIMEOUT_IN_MS 200
31
32 static int kbc_input_buffer_empty(void)
33 {
34         u32 timeout;
35         for(timeout = KBC_TIMEOUT_IN_MS; timeout && (inb(0x64) & 0x02); timeout--) {
36                 mdelay(1);
37         }
38
39         if (!timeout) {
40                 printk_warning("Unexpected Keyboard controller input buffer full\n");
41         }
42         return !!timeout;
43 }
44
45
46 static int kbc_output_buffer_full(void)
47 {
48         u32 timeout;
49         for(timeout = KBC_TIMEOUT_IN_MS; timeout && ((inb(0x64) & 0x01) == 0); timeout--) {
50                 mdelay(1);
51         }
52
53         if (!timeout) {
54                 printk_warning("Keyboard controller output buffer result timeout\n");
55         }
56         return !!timeout;
57 }
58
59
60 static int kbc_cleanup_buffers(void)
61 {
62         u32 timeout;
63         for(timeout = KBC_TIMEOUT_IN_MS; timeout && (inb(0x64) & 0x03); timeout--) {
64                 mdelay(1);
65                 inb(0x60);
66         }
67
68         if (!timeout) {
69                 printk_err("Couldn't cleanup the keyboard controller buffers\n");
70                 printk_err("0x64: 0x%x, 0x60: 0x%x\n", inb(0x64), inb(0x60));
71         }
72         return !!timeout;
73 }
74
75
76 static u8 send_keyboard(u8 command)
77 {
78         u8 regval = 0;
79         u8 resend = 10;
80
81         do {
82                 if (!kbc_input_buffer_empty()) return 0;
83                 outb(command, 0x60);
84                 if (!kbc_output_buffer_full()) {
85                         printk_err("Could not send keyboard command %02x\n",
86                                         command);
87                         return 0;
88                 }
89                 regval = inb(0x60);
90                 --resend;
91         } while (regval == 0xFE && resend > 0);
92
93         return regval;
94 }
95
96
97 static void pc_keyboard_init(struct pc_keyboard *keyboard)
98 {
99         u8 regval;
100         printk_debug("Keyboard init...\n");
101
102         /* clean up any junk that might have been in the kbc */
103         if (!kbc_cleanup_buffers()) return;
104
105         /* reset/self test 8042 - send cmd 0xAA */
106         if (!kbc_input_buffer_empty()) return;
107         outb(0xAA, 0x64);
108         if (!kbc_output_buffer_full()) {
109                 printk_err("Could not reset keyboard controller.\n");
110                 return;
111         }
112
113         /* read self-test result, 0x55 is returned in the output buffer (0x60) */
114         if ((regval = inb(0x60) != 0x55)) {
115                 printk_err("Keyboard Controller self-test failed: 0x%x\n", regval);
116                 return;
117         }
118
119         /* Enable keyboard interface - No IRQ */
120         if (!kbc_input_buffer_empty()) return;
121         outb(0x60, 0x64);
122         if (!kbc_input_buffer_empty()) return;
123         outb(0x20, 0x60);       /* send cmd: enable keyboard */
124         if (!kbc_input_buffer_empty()) {
125                 printk_info("Timeout while enabling keyboard\n");
126                 return;
127         }
128
129         /* clean up any junk that might have been in the keyboard */
130         if (!kbc_cleanup_buffers()) return;
131
132         /* reset keyboard and self test (keyboard side) */
133         regval = send_keyboard(0xFF);
134         if (regval != 0xFA) {
135                 printk_err("Keyboard selftest failed ACK: 0x%x\n", regval);
136                 return;
137         }
138         if (!kbc_output_buffer_full()) {
139                 printk_err("Timeout waiting for keyboard after reset.\n");
140                 return;
141         }
142                 
143         regval = inb(0x60);
144         if (regval != 0xAA) {
145                 printk_err("Keyboard selftest failed: 0x%x\n", regval);
146                 return;
147         }
148
149         /*
150          * The following set scancode stuff is what normal BIOS do. It could be
151          * argued that coreboot shouldn't set the scan code.....
152          */
153
154         /* disable the keyboard */
155         regval = send_keyboard(0xF5);
156         if (regval != 0xFA) {
157                 printk_err("Keyboard disable failed ACK: 0x%x\n", regval);
158                 return;
159         }
160
161         /* Set scancode command */
162         regval = send_keyboard(0xF0);
163         if (regval != 0xFA) {
164                 printk_err("Keyboard set scancode cmd failed ACK: 0x%x\n", regval);
165                 return;
166         }
167         /* Set scancode mode 2 */
168         regval = send_keyboard(0x02);
169         if (regval != 0xFA) {
170                 printk_err("Keyboard set scancode mode failed ACK: 0x%x\n", regval);
171                 return;
172         }
173
174         /* enable the keyboard */
175         regval = send_keyboard(0xF4);
176         if (regval != 0xFA) {
177                 printk_err("Keyboard enable failed ACK: 0x%x\n", regval);
178                 return;
179         }
180
181         /* All is well - enable keyboard interface */
182         if (!kbc_input_buffer_empty()) return;
183         outb(0x60, 0x64);
184         if (!kbc_input_buffer_empty()) return;
185         outb(0x61, 0x60);       /* send cmd: enable keyboard and IRQ 1 */
186         if (!kbc_input_buffer_empty()) {
187                 printk_err("Timeout during final keyboard enable\n");
188                 return;
189         }
190 }
191
192
193 void init_pc_keyboard(unsigned port0, unsigned port1, struct pc_keyboard *kbd)
194 {
195         if ((port0 == 0x60) && (port1 == 0x64)) {
196                 pc_keyboard_init(kbd);
197         }
198 }
199
200 /*
201  * Support PS/2 mode -  oddball SIOs(KBC) need this setup
202  * Not well documented. Google - 0xcb keyboard controller
203  * This is called before pc_keyboard_init().
204  */
205 void set_kbc_ps2_mode(void)
206 {
207         /* clean up any junk that might have been in the kbc */
208         if (!kbc_cleanup_buffers()) return;
209
210         /* reset/self test 8042 before we can do anything */
211         if (!kbc_input_buffer_empty()) return;
212         outb(0xAA, 0x64);
213         if (!kbc_output_buffer_full()) return;
214
215         /* read self-test result, 0x55 is returned in the output buffer (0x60) */
216         if ((inb(0x60) != 0x55)) {
217                 printk_err("Keyboard Controller selftest failed\n");
218                 return;
219         }
220
221         /* Support PS/2 mode */
222         if (!kbc_input_buffer_empty()) return;
223         outb(0xcb, 0x64);
224         if (!kbc_input_buffer_empty()) return;
225         outb(0x01, 0x60);
226         kbc_cleanup_buffers();
227 }