454de986ad6ebcb9864ba401a61b7d7c3b19e21f
[coreboot.git] / src / pc80 / keyboard.c
1 #include <console/console.h>
2 #include <pc80/keyboard.h>
3 #include <device/device.h>
4 #include <arch/io.h>
5
6 static int kbd_empty_input_buffer(void)
7 {
8         unsigned long timeout;
9         for(timeout = 1000000; timeout && (inb(0x64) & 0x02); timeout--) {
10                 post_code(0);
11         }
12         return !!timeout;
13 }
14
15 static int kbd_empty_output_buffer(void)
16 {
17         unsigned long timeout;
18         for(timeout = 1000000; timeout && ((inb(0x64) & 0x01) == 0); timeout--) {
19                 post_code(0);
20         }
21         return !!timeout;
22 }
23
24 /* much better keyboard init courtesy ollie@sis.com.tw 
25    TODO: Typematic Setting, the keyboard is too slow for me */
26 static void pc_keyboard_init(struct pc_keyboard *keyboard)
27 {
28         unsigned char regval;
29
30         printk_debug("Keyboard init...\n");
31         /* send cmd = 0xAA, self test 8042 */
32         outb(0xaa, 0x64);
33
34         /* empty input buffer or any other command/data will be lost */
35         if (!kbd_empty_input_buffer()) {
36                 printk_err("Keyboard input buffer would not empty\n");
37                 return;
38         }
39
40         /* empty output buffer or any other command/data will be lost */
41         if (!kbd_empty_output_buffer()) {
42                 printk_err("Keyboard output buffer would not empty\n");
43                 return;
44         }
45
46         /* read self-test result, 0x55 should be returned form 0x60 */
47         if ((regval = inb(0x60) != 0x55)) {
48                 printk_err("Keyboard selftest failed\n");
49                 return;
50         }
51
52         /* enable keyboard interface */
53         outb(0x60, 0x64);
54         kbd_empty_input_buffer();
55
56         /* send cmd: enable IRQ 1 */
57         outb(0x61, 0x60);
58         kbd_empty_input_buffer();
59
60         /* reset kerboard and self test  (keyboard side) */
61         outb(0xff, 0x60);
62
63         /* empty inut bufferm or any other command/data will be lost */
64         kbd_empty_input_buffer();
65
66         /* empty output buffer or any other command/data will be lost */
67         kbd_empty_output_buffer();
68
69         if ((regval = inb(0x60) != 0xfa))
70                 return;
71
72         kbd_empty_output_buffer();
73         if ((regval = inb(0x60) != 0xaa))
74                 return;
75 }
76
77 void init_pc_keyboard(unsigned port0, unsigned port1, struct pc_keyboard *kbd)
78 {
79         if ((port0 == 0x60) && (port1 == 0x64)) {
80                 pc_keyboard_init(kbd);
81         }
82 }