Rewrite keyboard driver to actually wait time in ms as specified in the specs,
[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         u8 resend;
101         printk_debug("Keyboard init...\n");
102
103         /* clean up any junk that might have been in the kbc */
104         if (!kbc_cleanup_buffers()) return;
105
106         /* reset/self test 8042 - send cmd 0xAA */
107         if (!kbc_input_buffer_empty()) return;
108         outb(0xAA, 0x64);
109         if (!kbc_output_buffer_full()) {
110                 printk_err("Could not reset keyboard controller.\n");
111                 return;
112         }
113
114         /* read self-test result, 0x55 is returned in the output buffer (0x60) */
115         if ((regval = inb(0x60) != 0x55)) {
116                 printk_err("Keyboard Controller self-test failed: 0x%x\n", regval);
117                 return;
118         }
119
120         /* Enable keyboard interface - No IRQ */
121         resend = 10;
122         regval = 0;
123         do {
124                 if (!kbc_input_buffer_empty()) return;
125                 outb(0x60, 0x64);
126                 if (!kbc_input_buffer_empty()) return;
127                 outb(0x20, 0x60);       /* send cmd: enable keyboard */
128                 if (kbc_output_buffer_full()) {
129                         regval = inb(0x60);
130                 } else {
131                         printk_info("Timeout while enabling keyboard. (No keyboard present?)\n");
132                         regval = inb(0x60); /* Better than 0 ? */
133                 }
134                 --resend;
135         } while (regval == 0xFE && resend > 0);
136
137         /* clean up any junk that might have been in the keyboard */
138         if (!kbc_cleanup_buffers()) return;
139
140         /* reset keyboard and self test (keyboard side) */
141         regval = send_keyboard(0xFF);
142         if (regval != 0xFA) {
143                 printk_err("Keyboard selftest failed ACK: 0x%x\n", regval);
144                 return;
145         }
146         if (!kbc_output_buffer_full()) {
147                 printk_err("Timeout waiting for keyboard after reset.\n");
148                 return;
149         }
150                 
151         regval = inb(0x60);
152         if (regval != 0xAA) {
153                 printk_err("Keyboard selftest failed: 0x%x\n", regval);
154                 return;
155         }
156
157         /*
158          * The following set scancode stuff is what normal BIOS do. It could be
159          * argued that coreboot shouldn't set the scan code.....
160          */
161
162         /* disable the keyboard */
163         regval = send_keyboard(0xF5);
164         if (regval != 0xFA) {
165                 printk_err("Keyboard disable failed ACK: 0x%x\n", regval);
166                 return;
167         }
168
169         /* Set scancode command */
170         regval = send_keyboard(0xF0);
171         if (regval != 0xFA) {
172                 printk_err("Keyboard set scancode cmd failed ACK: 0x%x\n", regval);
173                 return;
174         }
175         /* Set scancode mode 2 */
176         regval = send_keyboard(0x02);
177         if (regval != 0xFA) {
178                 printk_err("Keyboard set scancode mode failed ACK: 0x%x\n", regval);
179                 return;
180         }
181
182         /* enable the keyboard */
183         regval = send_keyboard(0xF4);
184         if (regval != 0xFA) {
185                 printk_err("Keyboard enable failed ACK: 0x%x\n", regval);
186                 return;
187         }
188
189         /* All is well - enable keyboard interface */
190         resend = 10;
191         regval = 0;
192         do {
193                 if (!kbc_input_buffer_empty()) return;
194                 outb(0x60, 0x64);
195                 if (!kbc_input_buffer_empty()) return;
196                 outb(0x61, 0x60);       /* send cmd: enable keyboard and IRQ 1 */
197                 if (kbc_output_buffer_full()) {
198                         regval = inb(0x60);
199                 }
200                 --resend;
201         } while (regval == 0xFE && resend > 0);
202 }
203
204
205 void init_pc_keyboard(unsigned port0, unsigned port1, struct pc_keyboard *kbd)
206 {
207         if ((port0 == 0x60) && (port1 == 0x64)) {
208                 pc_keyboard_init(kbd);
209         }
210 }
211
212 /*
213  * Support PS/2 mode -  oddball SIOs(KBC) need this setup
214  * Not well documented. Google - 0xcb keyboard controller
215  * This is called before pc_keyboard_init().
216  */
217 void set_kbc_ps2_mode(void)
218 {
219         /* clean up any junk that might have been in the kbc */
220         if (!kbc_cleanup_buffers()) return;
221
222         /* reset/self test 8042 before we can do anything */
223         if (!kbc_input_buffer_empty()) return;
224         outb(0xAA, 0x64);
225         if (!kbc_output_buffer_full()) return;
226
227         /* read self-test result, 0x55 is returned in the output buffer (0x60) */
228         if ((inb(0x60) != 0x55)) {
229                 printk_err("Keyboard Controller selftest failed\n");
230                 return;
231         }
232
233         /* Support PS/2 mode */
234         if (!kbc_input_buffer_empty()) return;
235         outb(0xcb, 0x64);
236         if (!kbc_input_buffer_empty()) return;
237         outb(0x01, 0x60);
238         kbc_cleanup_buffers();
239 }