Some USB debug updates, mostly comments fixing, license header updates
[coreboot.git] / src / pc80 / keyboard.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2008 Advanced Micro Devices, Inc.
5  * Copyright (C) ???? Ollie Lo <ollielo@hotmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; version 2 of the License.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21
22 #include <console/console.h>
23 #include <pc80/keyboard.h>
24 #include <device/device.h>
25 #include <arch/io.h>
26
27 static int kbc_input_buffer_empty(void)
28 {
29         u32 timeout;
30         for(timeout = 1000000; timeout && (inb(0x64) & 0x02); timeout--) {
31                 inb(0x80);
32         }
33
34         if (!timeout) {
35                 printk_err("Unexpected Keyboard controller input buffer full\n");
36         }
37         return !!timeout;
38 }
39
40
41 static int kbc_output_buffer_full(void)
42 {
43         u32 timeout;
44         for(timeout = 1000000; timeout && ((inb(0x64) & 0x01) == 0); timeout--) {
45                 inb(0x80);
46         }
47
48         if (!timeout) {
49                 printk_err("Keyboard controller output buffer result timeout\n");
50         }
51         return !!timeout;
52 }
53
54
55 static int kbc_cleanup_buffers(void)
56 {
57         u32 timeout;
58         for(timeout = 1000000; timeout && (inb(0x64) & 0x03); timeout--) {
59                 inb(0x60);
60         }
61
62         if (!timeout) {
63                 printk_err("Couldn't cleanup the keyboard controller buffers\n");
64                 printk_err("0x64: 0x%x, 0x60: 0x%x\n", inb(0x64), inb(0x60));
65         }
66         return !!timeout;
67 }
68
69
70 static u8 send_keyboard(u8 command)
71 {
72         u8 regval = 0;
73         u8 resend = 10;
74
75         do {
76                 if (!kbc_input_buffer_empty()) return 0;
77                 outb(command, 0x60);
78                 if (!kbc_output_buffer_full()) return 0;
79                 regval = inb(0x60);
80                 --resend;
81         } while (regval == 0xFE && resend > 0);
82
83         return regval;
84 }
85
86
87 static void pc_keyboard_init(struct pc_keyboard *keyboard)
88 {
89         u8 regval;
90         u8 resend;
91         printk_debug("Keyboard init...\n");
92
93         /* clean up any junk that might have been in the kbc */
94         if (!kbc_cleanup_buffers()) return;
95
96         /* reset/self test 8042 - send cmd 0xAA,  */
97         if (!kbc_input_buffer_empty()) return;
98         outb(0xAA, 0x64);
99         if (!kbc_output_buffer_full()) return;
100
101         /* read self-test result, 0x55 is returned in the output buffer (0x60) */
102         if ((regval = inb(0x60) != 0x55)) {
103                 printk_err("Keyboard Controller selftest failed: 0x%x\n", regval);
104                 return;
105         }
106
107         /* Enable keyboard interface - No IRQ*/
108         resend = 10;
109         regval = 0;
110         do {
111                 if (!kbc_input_buffer_empty()) return;
112                 outb(0x60, 0x64);
113                 if (!kbc_input_buffer_empty()) return;
114                 outb(0x20, 0x60);       /* send cmd: enable keyboard */
115                 if ((inb(0x64) & 0x01)) {
116                         regval = inb(0x60);
117                 }
118                 --resend;
119         } while (regval == 0xFE && resend > 0);
120
121         /* clean up any junk that might have been in the keyboard */
122         if (!kbc_cleanup_buffers()) return;
123
124         /* reset keyboard and self test (keyboard side) */
125         regval = send_keyboard(0xFF);
126         if (regval != 0xFA) {
127                 printk_err("Keyboard selftest failed ACK: 0x%x\n", regval);
128                 return;
129         }
130         if (!kbc_output_buffer_full()) return;
131         regval = inb(0x60);
132         if (regval != 0xAA) {
133                 printk_err("Keyboard selftest failed: 0x%x\n", regval);
134                 return;
135         }
136
137         /*
138          * The following set scancode stuff is what normal BIOS do. It could be
139          * argued that coreboot shouldn't set the scan code.....
140          */
141
142         /* disable the keyboard */
143         regval = send_keyboard(0xF5);
144         if (regval != 0xFA) {
145                 printk_err("Keyboard disable failed ACK: 0x%x\n", regval);
146                 return;
147         }
148
149         /* Set scancode command */
150         regval = send_keyboard(0xF0);
151         if (regval != 0xFA) {
152                 printk_err("Keyboard set scancode cmd failed ACK: 0x%x\n", regval);
153                 return;
154         }
155         /* Set scancode mode 2 */
156         regval = send_keyboard(0x02);
157         if (regval != 0xFA) {
158                 printk_err("Keyboard set scancode mode failed ACK: 0x%x\n", regval);
159                 return;
160         }
161
162         /* enable the keyboard */
163         regval = send_keyboard(0xF4);
164         if (regval != 0xFA) {
165                 printk_err("Keyboard enable failed ACK: 0x%x\n", regval);
166                 return;
167         }
168
169         /* All is well - enable keyboard interface */
170         resend = 10;
171         regval = 0;
172         do {
173                 if (!kbc_input_buffer_empty()) return;
174                 outb(0x60, 0x64);
175                 if (!kbc_input_buffer_empty()) return;
176                 outb(0x61, 0x60);       /* send cmd: enable keyboard and IRQ 1 */
177                 if ((inb(0x64) & 0x01)) {
178                         regval = inb(0x60);
179                 }
180                 --resend;
181         } while (regval == 0xFE && resend > 0);
182 }
183
184
185 void init_pc_keyboard(unsigned port0, unsigned port1, struct pc_keyboard *kbd)
186 {
187         if ((port0 == 0x60) && (port1 == 0x64)) {
188                 pc_keyboard_init(kbd);
189         }
190 }
191
192 /*
193  * Support PS/2 mode -  oddball SIOs(KBC) need this setup
194  * Not well documented. Google - 0xcb keyboard controller
195  * This is called before pc_keyboard_init().
196  */
197 void set_kbc_ps2_mode(void)
198 {
199         /* clean up any junk that might have been in the kbc */
200         if (!kbc_cleanup_buffers()) return;
201
202         /* reset/self test 8042 before we can do anything */
203         if (!kbc_input_buffer_empty()) return;
204         outb(0xAA, 0x64);
205         if (!kbc_output_buffer_full()) return;
206
207         /* read self-test result, 0x55 is returned in the output buffer (0x60) */
208         if ((inb(0x60) != 0x55)) {
209                 printk_err("Keyboard Controller selftest failed\n");
210                 return;
211         }
212
213         /* Support PS/2 mode */
214         if (!kbc_input_buffer_empty()) return;
215         outb(0xcb, 0x64);
216         if (!kbc_input_buffer_empty()) return;
217         outb(0x01, 0x60);
218         kbc_cleanup_buffers();
219 }