Debian Codegraph @

src/drivers/pc80/pc/keyboard.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <console/console.h>
4 #include <pc80/keyboard.h>
5 #include <device/device.h>
6 #include <arch/io.h>
7 #include <delay.h>
8 #include <types.h>
9 #include <acpi/acpi.h>
10
11 #define KBD_DATA 0x60
12 #define KBD_COMMAND 0x64
13 #define KBD_STATUS 0x64
14 #define KBD_IBF (1 << 1) // 1: input buffer full (data ready for ec)
15 #define KBD_OBF (1 << 0) // 1: output buffer full (data ready for host)
16
17 // Keyboard Controller Commands
18 #define KBC_CMD_READ_COMMAND 0x20 // Read command byte
19 #define KBC_CMD_WRITE_COMMAND 0x60 // Write command byte
20 #define KBC_CMD_AUX_ENABLE 0xA8 // Auxiliary Interface enable
21 #define KBC_CMD_AUX_TEST 0xA9 // Auxiliary Interface test
22 #define KBC_CMD_SELF_TEST 0xAA // Controller self-test
23 #define KBC_CMD_KBD_TEST 0xAB // Keyboard Interface test
24
25 // Keyboard Controller Replies
26 #define KBC_REPLY_SELFTEST_OK 0x55 // controller self-test succeeded
27
28 //
29 // Keyboard Replies
30 //
31 #define KBD_REPLY_POR 0xAA // Power on reset
32 #define KBD_REPLY_ACK 0xFA // Command ACK
33 #define KBD_REPLY_RESEND 0xFE // Command NACK, send command again
34
35 /* Wait 400ms for keyboard controller answers */
36 #define KBC_TIMEOUT_IN_MS 400
37
38 static int kbc_input_buffer_empty(void)
39 {
40 u32 timeout;
41
42 for (timeout = KBC_TIMEOUT_IN_MS;
43 timeout && (inb(KBD_STATUS) & KBD_IBF); timeout--)
44 mdelay(1);
45
46 if (!timeout)
47 printk(BIOS_WARNING,
48 "Unexpected Keyboard controller input buffer full\n");
49 return !!timeout;
50 }
51
52 static int kbc_output_buffer_full(void)
53 {
54 u32 timeout;
55
56 for (timeout = KBC_TIMEOUT_IN_MS;
57 timeout && ((inb(KBD_STATUS) & KBD_OBF) == 0); timeout--)
58 mdelay(1);
59
60 if (!timeout)
61 printk(BIOS_INFO,
62 "Keyboard controller output buffer result timeout\n");
63 return !!timeout;
64 }
65
66 static int kbc_cleanup_buffers(void)
67 {
68 u32 timeout;
69
70 for (timeout = KBC_TIMEOUT_IN_MS;
71 timeout && (inb(KBD_STATUS) & (KBD_OBF | KBD_IBF)); timeout--) {
72 mdelay(1);
73 inb(KBD_DATA);
74 }
75
76 if (!timeout) {
77 printk(BIOS_ERR,
78 "Couldn't cleanup the keyboard controller buffers\n");
79 printk(BIOS_ERR, "Status (0x%x): 0x%x, Buffer (0x%x): 0x%x\n",
80 KBD_STATUS, inb(KBD_STATUS), KBD_DATA, inb(KBD_DATA));
81 }
82
83 return !!timeout;
84 }
85
86 static enum cb_err kbc_self_test(uint8_t probe_aux, uint8_t *aux_probe_result)
87 {
88 uint8_t self_test;
89 uint8_t byte;
90
91 /* Set initial aux probe output value */
92 if (aux_probe_result)
93 *aux_probe_result = 0;
94
95 /* Clean up any junk that might have been in the KBC.
96 * Both input and output buffers must be empty.
97 */
98 if (!kbc_cleanup_buffers())
99 return CB_KBD_CONTROLLER_FAILURE;
100
101 /* reset/self test 8042 - send cmd 0xAA */
102 outb(KBC_CMD_SELF_TEST, KBD_COMMAND);
103
104 if (!kbc_output_buffer_full()) {
105 /* There probably is no keyboard controller. */
106 printk(BIOS_ERR, "Could not reset keyboard controller.\n");
107 return CB_KBD_CONTROLLER_FAILURE;
108 }
109
110 /* read self-test result, 0x55 is returned in the output buffer */
111 self_test = inb(KBD_DATA);
112
113 if (self_test != 0x55) {
114 printk(BIOS_ERR, "Keyboard Controller self-test failed: 0x%x\n",
115 self_test);
116 return CB_KBD_CONTROLLER_FAILURE;
117 }
118
119 /* ensure the buffers are empty */
120 kbc_cleanup_buffers();
121
122 /* keyboard interface test */
123 outb(KBC_CMD_KBD_TEST, KBD_COMMAND);
124
125 if (!kbc_output_buffer_full()) {
126 printk(BIOS_ERR, "Keyboard Interface test timed out.\n");
127 return CB_KBD_CONTROLLER_FAILURE;
128 }
129
130 /* read test result, 0x00 should be returned in case of no failures */
131 self_test = inb(KBD_DATA);
132
133 if (self_test != 0x00) {
134 printk(BIOS_ERR, "Keyboard Interface test failed: 0x%x\n",
135 self_test);
136 return CB_KBD_INTERFACE_FAILURE;
137 }
138
139 if (probe_aux) {
140 /* aux interface detect */
141 outb(KBC_CMD_AUX_ENABLE, KBD_COMMAND);
142 if (!kbc_input_buffer_empty()) {
143 printk(BIOS_ERR, "Timeout waiting for controller during aux enable.\n");
144 return CB_KBD_CONTROLLER_FAILURE;
145 }
146 outb(KBC_CMD_READ_COMMAND, KBD_COMMAND);
147 if (!kbc_output_buffer_full()) {
148 printk(BIOS_ERR, "Timeout waiting for controller during aux probe.\n");
149 return CB_KBD_CONTROLLER_FAILURE;
150 }
151
152 byte = inb(KBD_DATA);
153 if (!(byte & (0x1 << 5))) {
154 printk(BIOS_DEBUG, "PS/2 auxiliary channel detected...\n");
155
156 /* auxiliary interface test */
157 outb(KBC_CMD_AUX_TEST, KBD_COMMAND);
158
159 if (!kbc_output_buffer_full()) {
160 printk(BIOS_ERR, "Auxiliary channel probe timed out.\n");
161 goto aux_failure;
162 }
163
164 /* read test result, 0x00 should be returned in case of no failures */
165 self_test = inb(KBD_DATA);
166
167 if (self_test != 0x00) {
168 printk(BIOS_ERR, "No device detected on auxiliary channel: 0x%x\n",
169 self_test);
170 goto aux_failure;
171 }
172
173 printk(BIOS_DEBUG, "PS/2 device detected on auxiliary channel\n");
174 if (aux_probe_result)
175 *aux_probe_result = 1;
176 }
177 }
178
179 aux_failure:
180
181 return CB_SUCCESS;
182 }
183
184 static u8 send_keyboard(u8 command)
185 {
186 u8 regval = 0;
187 u8 resend = 10;
188
189 do {
190 if (!kbc_input_buffer_empty())
191 return 0;
192 outb(command, KBD_DATA);
193 /* the reset command takes much longer then normal commands and
194 * even worse, some keyboards do send the ACK _after_ doing the
195 * reset */
196 if (command == 0xFF) {
197 u8 retries;
198
199 for (retries = 9; retries && !kbc_output_buffer_full();
200 retries--)
201 ;
202 }
203 if (!kbc_output_buffer_full()) {
204 printk(BIOS_ERR,
205 "Could not send keyboard command %02x\n",
206 command);
207 return 0;
208 }
209 regval = inb(KBD_DATA);
210 --resend;
211 } while (regval == KBD_REPLY_RESEND && resend > 0);
212
213 return regval;
214 }
215
216 uint8_t pc_keyboard_init(uint8_t probe_aux)
217 {
218 u8 retries;
219 u8 regval;
220 enum cb_err err;
221 uint8_t aux_dev_detected;
222
223 if (!CONFIG(DRIVERS_PS2_KEYBOARD))
224 return 0;
225
226 if (acpi_is_wakeup_s3())
227 return 0;
228
229 printk(BIOS_DEBUG, "Keyboard init...\n");
230
231 /* Run a keyboard controller self-test */
232 err = kbc_self_test(probe_aux, &aux_dev_detected);
233 /* Ignore interface failure as it's non-fatal. */
234 if (err != CB_SUCCESS && err != CB_KBD_INTERFACE_FAILURE)
235 return 0;
236
237 /* Enable keyboard interface - No IRQ */
238 if (!kbc_input_buffer_empty())
239 return 0;
240 outb(0x60, KBD_COMMAND);
241 if (!kbc_input_buffer_empty())
242 return 0;
243 outb(0x20, KBD_DATA); /* send cmd: enable keyboard */
244 if (!kbc_input_buffer_empty()) {
245 printk(BIOS_INFO, "Timeout while enabling keyboard\n");
246 return 0;
247 }
248
249 /* clean up any junk that might have been in the keyboard */
250 if (!kbc_cleanup_buffers())
251 return 0;
252
253 /* reset keyboard and self test (keyboard side) */
254 regval = send_keyboard(0xFF);
255 if (regval == KBD_REPLY_RESEND) {
256 /* keeps sending RESENDs, probably no keyboard. */
257 printk(BIOS_INFO, "No PS/2 keyboard detected.\n");
258 return 0;
259 }
260
261 if (regval != KBD_REPLY_ACK) {
262 printk(BIOS_ERR, "Keyboard reset failed ACK: 0x%x\n", regval);
263 return 0;
264 }
265
266 /* the reset command takes some time, so wait a little longer */
267 for (retries = 9; retries && !kbc_output_buffer_full(); retries--)
268 ;
269
270 if (!kbc_output_buffer_full()) {
271 printk(BIOS_ERR, "Timeout waiting for keyboard after reset.\n");
272 return 0;
273 }
274
275 regval = inb(KBD_DATA);
276 if (regval != 0xAA) {
277 printk(BIOS_ERR, "Keyboard reset selftest failed: 0x%x\n",
278 regval);
279 return 0;
280 }
281
282 /*
283 * The following set scancode stuff is what normal BIOS do. It could be
284 * argued that coreboot shouldn't set the scan code.....
285 */
286
287 /* disable the keyboard */
288 regval = send_keyboard(0xF5);
289 if (regval != KBD_REPLY_ACK) {
290 printk(BIOS_ERR, "Keyboard disable failed ACK: 0x%x\n", regval);
291 return 0;
292 }
293
294 /* Set scancode command */
295 regval = send_keyboard(0xF0);
296 if (regval != KBD_REPLY_ACK) {
297 printk(BIOS_ERR, "Keyboard set scancode cmd failed ACK: 0x%x\n",
298 regval);
299 return 0;
300 }
301 /* Set scancode mode 2 */
302 regval = send_keyboard(0x02);
303 if (regval != KBD_REPLY_ACK) {
304 printk(BIOS_ERR,
305 "Keyboard set scancode mode failed ACK: 0x%x\n", regval);
306 return 0;
307 }
308
309 /* All is well - enable keyboard interface */
310 if (!kbc_input_buffer_empty())
311 return 0;
312 outb(0x60, KBD_COMMAND);
313 if (!kbc_input_buffer_empty())
314 return 0;
315 outb(0x65, KBD_DATA); /* send cmd: enable keyboard and IRQ 1 */
316 if (!kbc_input_buffer_empty()) {
317 printk(BIOS_ERR, "Timeout during keyboard enable\n");
318 return 0;
319 }
320
321 /* enable the keyboard */
322 regval = send_keyboard(0xF4);
323 if (regval != KBD_REPLY_ACK) {
324 printk(BIOS_ERR, "Keyboard enable failed ACK: 0x%x\n", regval);
325 return 0;
326 }
327
328 printk(BIOS_DEBUG, "PS/2 keyboard initialized on primary channel\n");
329
330 return aux_dev_detected;
331 }
332
333 /*
334 * Support PS/2 mode - oddball SIOs(KBC) need this setup
335 * Not well documented. Google - 0xcb keyboard controller
336 * This is called before pc_keyboard_init().
337 */
338 void set_kbc_ps2_mode(void)
339 {
340 enum cb_err err;
341
342 /* Run a keyboard controller self-test */
343 err = kbc_self_test(0, NULL);
344 /* Ignore interface failure as it's non-fatal. */
345 if (err != CB_SUCCESS && err != CB_KBD_INTERFACE_FAILURE)
346 return;
347
348 /* Support PS/2 mode */
349 if (!kbc_input_buffer_empty())
350 return;
351 outb(0xcb, KBD_COMMAND);
352
353 if (!kbc_input_buffer_empty())
354 return;
355 outb(0x01, KBD_DATA);
356
357 kbc_cleanup_buffers();
358 }
359
360 enum cb_err pc_keyboard_set_command_byte_bit(u8 bit, u8 value)
361 {
362 if (!kbc_input_buffer_empty()) {
363 printk(BIOS_ERR, "Timeout waiting to read command byte\n");
364 return CB_KBD_INTERFACE_FAILURE;
365 }
366 outb(KBC_CMD_READ_COMMAND, KBD_COMMAND);
367
368 if (!kbc_output_buffer_full()) {
369 printk(BIOS_ERR, "Timeout waiting to read command byte\n");
370 return CB_KBD_INTERFACE_FAILURE;
371 }
372 u8 byte = inb(KBD_DATA);
373
374 if (!kbc_input_buffer_empty()) {
375 printk(BIOS_ERR, "Timeout waiting to write command byte\n");
376 return CB_KBD_INTERFACE_FAILURE;
377 }
378 outb(KBC_CMD_WRITE_COMMAND, KBD_COMMAND);
379
380 byte = value ? (byte | BIT(bit)) : (byte & ~BIT(bit));
381
382 if (!kbc_input_buffer_empty()) {
383 printk(BIOS_ERR, "Timeout waiting to write command byte\n");
384 return CB_KBD_INTERFACE_FAILURE;
385 }
386 outb(byte, KBD_DATA);
387
388 return CB_SUCCESS;
389 }