blob: 87220cad356c3d1ddb93a85e89a0a123c579da0c [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * (C) Copyright 2002 ELTEC Elektronik AG
3 * Frank Gottschling <fgottschling@eltec.de>
4 *
Wolfgang Denkbd8ec7e2013-10-07 13:07:26 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00006 */
7
8/* i8042.c - Intel 8042 keyboard driver routines */
9
10/* includes */
11
12#include <common.h>
wdenkc6097192002-11-03 00:24:07 +000013#include <i8042.h>
14
15/* defines */
16
17#ifdef CONFIG_CONSOLE_CURSOR
Gabe Black76a5d322011-11-14 19:24:14 +000018extern void console_cursor(int state);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020019static int blinkCount = CONFIG_SYS_CONSOLE_BLINK_COUNT;
Gabe Black76a5d322011-11-14 19:24:14 +000020static int cursor_state;
wdenkc6097192002-11-03 00:24:07 +000021#endif
22
23/* locals */
24
Wolfgang Denk67777b92006-03-06 13:03:37 +010025static int kbd_input = -1; /* no input yet */
26static int kbd_mapping = KBD_US; /* default US keyboard */
27static int kbd_flags = NORMAL; /* after reset */
Gabe Black76a5d322011-11-14 19:24:14 +000028static int kbd_state; /* unshift code */
wdenkc6097192002-11-03 00:24:07 +000029
Gabe Black76a5d322011-11-14 19:24:14 +000030static void kbd_conv_char(unsigned char scan_code);
31static void kbd_led_set(void);
32static void kbd_normal(unsigned char scan_code);
33static void kbd_shift(unsigned char scan_code);
34static void kbd_ctrl(unsigned char scan_code);
35static void kbd_num(unsigned char scan_code);
36static void kbd_caps(unsigned char scan_code);
37static void kbd_scroll(unsigned char scan_code);
38static void kbd_alt(unsigned char scan_code);
39static int kbd_input_empty(void);
40static int kbd_reset(void);
wdenkc6097192002-11-03 00:24:07 +000041
Gabe Black76a5d322011-11-14 19:24:14 +000042static unsigned char kbd_fct_map[144] = {
43 /* kbd_fct_map table for scan code */
44 0, AS, AS, AS, AS, AS, AS, AS, /* scan 0- 7 */
45 AS, AS, AS, AS, AS, AS, AS, AS, /* scan 8- F */
46 AS, AS, AS, AS, AS, AS, AS, AS, /* scan 10-17 */
47 AS, AS, AS, AS, AS, CN, AS, AS, /* scan 18-1F */
48 AS, AS, AS, AS, AS, AS, AS, AS, /* scan 20-27 */
49 AS, AS, SH, AS, AS, AS, AS, AS, /* scan 28-2F */
50 AS, AS, AS, AS, AS, AS, SH, AS, /* scan 30-37 */
51 AS, AS, CP, 0, 0, 0, 0, 0, /* scan 38-3F */
52 0, 0, 0, 0, 0, NM, ST, ES, /* scan 40-47 */
53 ES, ES, ES, ES, ES, ES, ES, ES, /* scan 48-4F */
54 ES, ES, ES, ES, 0, 0, AS, 0, /* scan 50-57 */
55 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
56 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
57 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
58 AS, 0, 0, AS, 0, 0, AS, 0, /* scan 70-77 */
59 0, AS, 0, 0, 0, AS, 0, 0, /* scan 78-7F */
60 AS, CN, AS, AS, AK, ST, EX, EX, /* enhanced */
61 AS, EX, EX, AS, EX, AS, EX, EX /* enhanced */
62 };
wdenkc6097192002-11-03 00:24:07 +000063
Gabe Black76a5d322011-11-14 19:24:14 +000064static unsigned char kbd_key_map[2][5][144] = {
65 { /* US keyboard */
66 { /* unshift code */
67 0, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 0- 7 */
68 '7', '8', '9', '0', '-', '=', 0x08, '\t', /* scan 8- F */
69 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', /* scan 10-17 */
70 'o', 'p', '[', ']', '\r', CN, 'a', 's', /* scan 18-1F */
71 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* scan 20-27 */
72 '\'', '`', SH, '\\', 'z', 'x', 'c', 'v', /* scan 28-2F */
73 'b', 'n', 'm', ',', '.', '/', SH, '*', /* scan 30-37 */
74 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
75 0, 0, 0, 0, 0, NM, ST, '7', /* scan 40-47 */
76 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
77 '2', '3', '0', '.', 0, 0, 0, 0, /* scan 50-57 */
78 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
79 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
80 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
81 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
82 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
83 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
84 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
85 },
86 { /* shift code */
87 0, 0x1b, '!', '@', '#', '$', '%', '^', /* scan 0- 7 */
88 '&', '*', '(', ')', '_', '+', 0x08, '\t', /* scan 8- F */
89 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', /* scan 10-17 */
90 'O', 'P', '{', '}', '\r', CN, 'A', 'S', /* scan 18-1F */
91 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', /* scan 20-27 */
92 '"', '~', SH, '|', 'Z', 'X', 'C', 'V', /* scan 28-2F */
93 'B', 'N', 'M', '<', '>', '?', SH, '*', /* scan 30-37 */
94 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
95 0, 0, 0, 0, 0, NM, ST, '7', /* scan 40-47 */
96 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
97 '2', '3', '0', '.', 0, 0, 0, 0, /* scan 50-57 */
98 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
99 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
100 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
101 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
102 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
103 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
104 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
105 },
106 { /* control code */
107 0xff, 0x1b, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, /* scan 0- 7 */
108 0x1e, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, '\t', /* scan 8- F */
109 0x11, 0x17, 0x05, 0x12, 0x14, 0x19, 0x15, 0x09, /* scan 10-17 */
110 0x0f, 0x10, 0x1b, 0x1d, '\r', CN, 0x01, 0x13, /* scan 18-1F */
111 0x04, 0x06, 0x07, 0x08, 0x0a, 0x0b, 0x0c, 0xff, /* scan 20-27 */
112 0xff, 0x1c, SH, 0xff, 0x1a, 0x18, 0x03, 0x16, /* scan 28-2F */
113 0x02, 0x0e, 0x0d, 0xff, 0xff, 0xff, SH, 0xff, /* scan 30-37 */
114 0xff, 0xff, CP, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
115 0xff, 0xff, 0xff, 0xff, 0xff, NM, ST, 0xff, /* scan 40-47 */
116 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48-4F */
117 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 50-57 */
118 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58-5F */
119 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60-67 */
120 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68-6F */
121 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70-77 */
122 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78-7F */
123 '\r', CN, '/', '*', ' ', ST, 0xff, 0xff, /* extended */
124 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* extended */
125 },
126 { /* non numeric code */
127 0, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 0- 7 */
128 '7', '8', '9', '0', '-', '=', 0x08, '\t', /* scan 8- F */
129 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', /* scan 10-17 */
130 'o', 'p', '[', ']', '\r', CN, 'a', 's', /* scan 18-1F */
131 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* scan 20-27 */
132 '\'', '`', SH, '\\', 'z', 'x', 'c', 'v', /* scan 28-2F */
133 'b', 'n', 'm', ',', '.', '/', SH, '*', /* scan 30-37 */
134 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
135 0, 0, 0, 0, 0, NM, ST, 'w', /* scan 40-47 */
136 'x', 'y', 'l', 't', 'u', 'v', 'm', 'q', /* scan 48-4F */
137 'r', 's', 'p', 'n', 0, 0, 0, 0, /* scan 50-57 */
138 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
139 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
140 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
141 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
142 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
143 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
144 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
145 },
146 { /* right alt mode - not used in US keyboard */
147 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 0 - 7 */
148 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 8 - F */
149 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 10 -17 */
150 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 18 -1F */
151 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 20 -27 */
152 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 28 -2F */
153 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 30 -37 */
154 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38 -3F */
155 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 40 -47 */
156 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48 -4F */
157 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 50 -57 */
158 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58 -5F */
159 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60 -67 */
160 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68 -6F */
161 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70 -77 */
162 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78 -7F */
163 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* extended */
164 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* extended */
165 }
166 },
167 { /* german keyboard */
168 { /* unshift code */
169 0, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 0- 7 */
170 '7', '8', '9', '0', 0xe1, '\'', 0x08, '\t', /* scan 8- F */
171 'q', 'w', 'e', 'r', 't', 'z', 'u', 'i', /* scan 10-17 */
172 'o', 'p', 0x81, '+', '\r', CN, 'a', 's', /* scan 18-1F */
173 'd', 'f', 'g', 'h', 'j', 'k', 'l', 0x94, /* scan 20-27 */
174 0x84, '^', SH, '#', 'y', 'x', 'c', 'v', /* scan 28-2F */
175 'b', 'n', 'm', ',', '.', '-', SH, '*', /* scan 30-37 */
176 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
177 0, 0, 0, 0, 0, NM, ST, '7', /* scan 40-47 */
178 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
179 '2', '3', '0', ',', 0, 0, '<', 0, /* scan 50-57 */
180 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
181 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
182 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
183 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
184 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
185 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
186 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
187 },
188 { /* shift code */
189 0, 0x1b, '!', '"', 0x15, '$', '%', '&', /* scan 0- 7 */
190 '/', '(', ')', '=', '?', '`', 0x08, '\t', /* scan 8- F */
191 'Q', 'W', 'E', 'R', 'T', 'Z', 'U', 'I', /* scan 10-17 */
192 'O', 'P', 0x9a, '*', '\r', CN, 'A', 'S', /* scan 18-1F */
193 'D', 'F', 'G', 'H', 'J', 'K', 'L', 0x99, /* scan 20-27 */
194 0x8e, 0xf8, SH, '\'', 'Y', 'X', 'C', 'V', /* scan 28-2F */
195 'B', 'N', 'M', ';', ':', '_', SH, '*', /* scan 30-37 */
196 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
197 0, 0, 0, 0, 0, NM, ST, '7', /* scan 40-47 */
198 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
199 '2', '3', '0', ',', 0, 0, '>', 0, /* scan 50-57 */
200 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
201 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
202 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
203 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
204 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
205 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
206 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
207 },
208 { /* control code */
209 0xff, 0x1b, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, /* scan 0- 7 */
210 0x1e, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, '\t', /* scan 8- F */
211 0x11, 0x17, 0x05, 0x12, 0x14, 0x19, 0x15, 0x09, /* scan 10-17 */
212 0x0f, 0x10, 0x1b, 0x1d, '\r', CN, 0x01, 0x13, /* scan 18-1F */
213 0x04, 0x06, 0x07, 0x08, 0x0a, 0x0b, 0x0c, 0xff, /* scan 20-27 */
214 0xff, 0x1c, SH, 0xff, 0x1a, 0x18, 0x03, 0x16, /* scan 28-2F */
215 0x02, 0x0e, 0x0d, 0xff, 0xff, 0xff, SH, 0xff, /* scan 30-37 */
216 0xff, 0xff, CP, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
217 0xff, 0xff, 0xff, 0xff, 0xff, NM, ST, 0xff, /* scan 40-47 */
218 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48-4F */
219 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 50-57 */
220 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58-5F */
221 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60-67 */
222 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68-6F */
223 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70-77 */
224 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78-7F */
225 '\r', CN, '/', '*', ' ', ST, 0xff, 0xff, /* extended */
226 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* extended */
227 },
228 { /* non numeric code */
229 0, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 0- 7 */
230 '7', '8', '9', '0', 0xe1, '\'', 0x08, '\t', /* scan 8- F */
231 'q', 'w', 'e', 'r', 't', 'z', 'u', 'i', /* scan 10-17 */
232 'o', 'p', 0x81, '+', '\r', CN, 'a', 's', /* scan 18-1F */
233 'd', 'f', 'g', 'h', 'j', 'k', 'l', 0x94, /* scan 20-27 */
234 0x84, '^', SH, 0, 'y', 'x', 'c', 'v', /* scan 28-2F */
235 'b', 'n', 'm', ',', '.', '-', SH, '*', /* scan 30-37 */
236 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
237 0, 0, 0, 0, 0, NM, ST, 'w', /* scan 40-47 */
238 'x', 'y', 'l', 't', 'u', 'v', 'm', 'q', /* scan 48-4F */
239 'r', 's', 'p', 'n', 0, 0, '<', 0, /* scan 50-57 */
240 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
241 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
242 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
243 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
244 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
245 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
246 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
247 },
248 { /* Right alt mode - is used in German keyboard */
249 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 0 - 7 */
250 '{', '[', ']', '}', '\\', 0xff, 0xff, 0xff, /* scan 8 - F */
251 '@', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 10 -17 */
252 0xff, 0xff, 0xff, '~', 0xff, 0xff, 0xff, 0xff, /* scan 18 -1F */
253 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 20 -27 */
254 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 28 -2F */
255 0xff, 0xff, 0xe6, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 30 -37 */
256 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38 -3F */
257 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 40 -47 */
258 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48 -4F */
259 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '|', 0xff, /* scan 50 -57 */
260 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58 -5F */
261 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60 -67 */
262 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68 -6F */
263 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70 -77 */
264 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78 -7F */
265 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* extended */
266 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* extended */
267 }
268 }
269 };
wdenkc6097192002-11-03 00:24:07 +0000270
Gabe Black76a5d322011-11-14 19:24:14 +0000271static unsigned char ext_key_map[] = {
272 0x1c, /* keypad enter */
273 0x1d, /* right control */
274 0x35, /* keypad slash */
275 0x37, /* print screen */
276 0x38, /* right alt */
277 0x46, /* break */
278 0x47, /* editpad home */
279 0x48, /* editpad up */
280 0x49, /* editpad pgup */
281 0x4b, /* editpad left */
282 0x4d, /* editpad right */
283 0x4f, /* editpad end */
284 0x50, /* editpad dn */
285 0x51, /* editpad pgdn */
286 0x52, /* editpad ins */
287 0x53, /* editpad del */
288 0x00 /* map end */
289 };
wdenkc6097192002-11-03 00:24:07 +0000290
Gabe Black15bbe8d2011-11-14 20:18:12 +0000291/******************************************************************************/
292
293static int kbd_controller_present(void)
294{
295 return in8(I8042_STATUS_REG) != 0xff;
296}
297
Gabe Black13702302012-10-12 14:02:02 +0000298/*
299 * Implement a weak default function for boards that optionally
300 * need to skip the i8042 initialization.
301 */
302int __weak board_i8042_skip(void)
303{
304 /* As default, don't skip */
305 return 0;
306}
307
Louis Yung-Chieh Lo46d579c2012-10-11 15:15:51 +0000308void i8042_flush(void)
309{
310 int timeout;
311
312 /*
313 * The delay is to give the keyboard controller some time to fill the
314 * next byte.
315 */
316 while (1) {
317 timeout = 100; /* wait for no longer than 100us */
318 while (timeout > 0 && !(in8(I8042_STATUS_REG) & 0x01)) {
319 udelay(1);
320 timeout--;
321 }
322
323 /* Try to pull next byte if not timeout. */
324 if (in8(I8042_STATUS_REG) & 0x01)
325 in8(I8042_DATA_REG);
326 else
327 break;
328 }
329}
330
331int i8042_disable(void)
332{
333 if (kbd_input_empty() == 0)
334 return -1;
335
336 /* Disable keyboard */
337 out8(I8042_COMMAND_REG, 0xad);
338
339 if (kbd_input_empty() == 0)
340 return -1;
341
342 return 0;
343}
344
345
wdenkc6097192002-11-03 00:24:07 +0000346/*******************************************************************************
347 *
348 * i8042_kbd_init - reset keyboard and init state flags
349 */
Gabe Black76a5d322011-11-14 19:24:14 +0000350int i8042_kbd_init(void)
wdenkc6097192002-11-03 00:24:07 +0000351{
Gabe Black76a5d322011-11-14 19:24:14 +0000352 int keymap, try;
353 char *penv;
wdenkc6097192002-11-03 00:24:07 +0000354
Gabe Black13702302012-10-12 14:02:02 +0000355 if (!kbd_controller_present() || board_i8042_skip())
Gabe Black15bbe8d2011-11-14 20:18:12 +0000356 return -1;
357
Gabe Black76a5d322011-11-14 19:24:14 +0000358 /* Init keyboard device (default US layout) */
359 keymap = KBD_US;
360 penv = getenv("keymap");
361 if (penv != NULL) {
362 if (strncmp(penv, "de", 3) == 0)
363 keymap = KBD_GER;
364 }
wdenkc6097192002-11-03 00:24:07 +0000365
Gabe Black76a5d322011-11-14 19:24:14 +0000366 for (try = 0; try < KBD_RESET_TRIES; try++) {
367 if (kbd_reset() == 0) {
368 kbd_mapping = keymap;
369 kbd_flags = NORMAL;
370 kbd_state = 0;
371 kbd_led_set();
372 return 0;
373 }
374 }
375 return -1;
wdenkc6097192002-11-03 00:24:07 +0000376}
377
378
379/*******************************************************************************
380 *
381 * i8042_tstc - test if keyboard input is available
Wolfgang Denk67777b92006-03-06 13:03:37 +0100382 * option: cursor blinking if called in a loop
wdenkc6097192002-11-03 00:24:07 +0000383 */
Simon Glass0d1e1f72014-07-23 06:54:59 -0600384int i8042_tstc(struct stdio_dev *dev)
wdenkc6097192002-11-03 00:24:07 +0000385{
Gabe Black76a5d322011-11-14 19:24:14 +0000386 unsigned char scan_code = 0;
wdenkc6097192002-11-03 00:24:07 +0000387
388#ifdef CONFIG_CONSOLE_CURSOR
Gabe Black76a5d322011-11-14 19:24:14 +0000389 if (--blinkCount == 0) {
390 cursor_state ^= 1;
391 console_cursor(cursor_state);
392 blinkCount = CONFIG_SYS_CONSOLE_BLINK_COUNT;
393 udelay(10);
394 }
wdenkc6097192002-11-03 00:24:07 +0000395#endif
396
Gabe Black76a5d322011-11-14 19:24:14 +0000397 if ((in8(I8042_STATUS_REG) & 0x01) == 0) {
398 return 0;
399 } else {
400 scan_code = in8(I8042_DATA_REG);
401 if (scan_code == 0xfa)
402 return 0;
wdenkc6097192002-11-03 00:24:07 +0000403
Gabe Black76a5d322011-11-14 19:24:14 +0000404 kbd_conv_char(scan_code);
wdenkc6097192002-11-03 00:24:07 +0000405
Gabe Black76a5d322011-11-14 19:24:14 +0000406 if (kbd_input != -1)
407 return 1;
408 }
409 return 0;
wdenkc6097192002-11-03 00:24:07 +0000410}
411
412
413/*******************************************************************************
414 *
415 * i8042_getc - wait till keyboard input is available
Wolfgang Denk67777b92006-03-06 13:03:37 +0100416 * option: turn on/off cursor while waiting
wdenkc6097192002-11-03 00:24:07 +0000417 */
Simon Glass0d1e1f72014-07-23 06:54:59 -0600418int i8042_getc(struct stdio_dev *dev)
wdenkc6097192002-11-03 00:24:07 +0000419{
Gabe Black76a5d322011-11-14 19:24:14 +0000420 int ret_chr;
421 unsigned char scan_code;
wdenkc6097192002-11-03 00:24:07 +0000422
Gabe Black76a5d322011-11-14 19:24:14 +0000423 while (kbd_input == -1) {
424 while ((in8(I8042_STATUS_REG) & 0x01) == 0) {
wdenkc6097192002-11-03 00:24:07 +0000425#ifdef CONFIG_CONSOLE_CURSOR
Gabe Black76a5d322011-11-14 19:24:14 +0000426 if (--blinkCount == 0) {
427 cursor_state ^= 1;
428 console_cursor(cursor_state);
429 blinkCount = CONFIG_SYS_CONSOLE_BLINK_COUNT;
430 }
431 udelay(10);
wdenkc6097192002-11-03 00:24:07 +0000432#endif
Gabe Black76a5d322011-11-14 19:24:14 +0000433 }
434 scan_code = in8(I8042_DATA_REG);
435 if (scan_code != 0xfa)
436 kbd_conv_char (scan_code);
wdenk57b2d802003-06-27 21:31:46 +0000437 }
Gabe Black76a5d322011-11-14 19:24:14 +0000438 ret_chr = kbd_input;
439 kbd_input = -1;
440 return ret_chr;
wdenkc6097192002-11-03 00:24:07 +0000441}
442
443
444/******************************************************************************/
445
Gabe Black76a5d322011-11-14 19:24:14 +0000446static void kbd_conv_char(unsigned char scan_code)
wdenkc6097192002-11-03 00:24:07 +0000447{
Gabe Black76a5d322011-11-14 19:24:14 +0000448 if (scan_code == 0xe0) {
449 kbd_flags |= EXT;
450 return;
451 }
wdenkc6097192002-11-03 00:24:07 +0000452
Gabe Black76a5d322011-11-14 19:24:14 +0000453 /* if high bit of scan_code, set break flag */
454 if (scan_code & 0x80)
455 kbd_flags |= BRK;
456 else
457 kbd_flags &= ~BRK;
wdenkc6097192002-11-03 00:24:07 +0000458
Gabe Black76a5d322011-11-14 19:24:14 +0000459 if ((scan_code == 0xe1) || (kbd_flags & E1)) {
460 if (scan_code == 0xe1) {
461 kbd_flags ^= BRK; /* reset the break flag */
462 kbd_flags ^= E1; /* bitwise EXOR with E1 flag */
463 }
464 return;
wdenk57b2d802003-06-27 21:31:46 +0000465 }
wdenkc6097192002-11-03 00:24:07 +0000466
Gabe Black76a5d322011-11-14 19:24:14 +0000467 scan_code &= 0x7f;
wdenkc6097192002-11-03 00:24:07 +0000468
Gabe Black76a5d322011-11-14 19:24:14 +0000469 if (kbd_flags & EXT) {
470 int i;
wdenkc6097192002-11-03 00:24:07 +0000471
Gabe Black76a5d322011-11-14 19:24:14 +0000472 kbd_flags ^= EXT;
473 for (i = 0; ext_key_map[i]; i++) {
474 if (ext_key_map[i] == scan_code) {
475 scan_code = 0x80 + i;
476 break;
477 }
478 }
479 /* not found ? */
480 if (!ext_key_map[i])
481 return;
wdenk57b2d802003-06-27 21:31:46 +0000482 }
wdenkc6097192002-11-03 00:24:07 +0000483
Gabe Black76a5d322011-11-14 19:24:14 +0000484 switch (kbd_fct_map[scan_code]) {
485 case AS:
486 kbd_normal(scan_code);
487 break;
488 case SH:
489 kbd_shift(scan_code);
490 break;
491 case CN:
492 kbd_ctrl(scan_code);
493 break;
494 case NM:
495 kbd_num(scan_code);
496 break;
497 case CP:
498 kbd_caps(scan_code);
499 break;
500 case ST:
501 kbd_scroll(scan_code);
502 break;
503 case AK:
504 kbd_alt(scan_code);
505 break;
506 }
507 return;
wdenkc6097192002-11-03 00:24:07 +0000508}
509
510
511/******************************************************************************/
512
Gabe Black76a5d322011-11-14 19:24:14 +0000513static void kbd_normal(unsigned char scan_code)
wdenkc6097192002-11-03 00:24:07 +0000514{
Gabe Black76a5d322011-11-14 19:24:14 +0000515 unsigned char chr;
wdenkc6097192002-11-03 00:24:07 +0000516
Gabe Black76a5d322011-11-14 19:24:14 +0000517 if ((kbd_flags & BRK) == NORMAL) {
518 chr = kbd_key_map[kbd_mapping][kbd_state][scan_code];
519 if ((chr == 0xff) || (chr == 0x00))
520 return;
wdenkc6097192002-11-03 00:24:07 +0000521
Gabe Black76a5d322011-11-14 19:24:14 +0000522 /* if caps lock convert upper to lower */
523 if (((kbd_flags & CAPS) == CAPS) &&
524 (chr >= 'a' && chr <= 'z')) {
525 chr -= 'a' - 'A';
526 }
527 kbd_input = chr;
528 }
wdenkc6097192002-11-03 00:24:07 +0000529}
530
531
532/******************************************************************************/
533
Gabe Black76a5d322011-11-14 19:24:14 +0000534static void kbd_shift(unsigned char scan_code)
wdenkc6097192002-11-03 00:24:07 +0000535{
Gabe Black76a5d322011-11-14 19:24:14 +0000536 if ((kbd_flags & BRK) == BRK) {
537 kbd_state = AS;
538 kbd_flags &= (~SHIFT);
539 } else {
540 kbd_state = SH;
541 kbd_flags |= SHIFT;
542 }
wdenkc6097192002-11-03 00:24:07 +0000543}
544
545
546/******************************************************************************/
547
Gabe Black76a5d322011-11-14 19:24:14 +0000548static void kbd_ctrl(unsigned char scan_code)
wdenkc6097192002-11-03 00:24:07 +0000549{
Gabe Black76a5d322011-11-14 19:24:14 +0000550 if ((kbd_flags & BRK) == BRK) {
551 kbd_state = AS;
552 kbd_flags &= (~CTRL);
553 } else {
554 kbd_state = CN;
555 kbd_flags |= CTRL;
556 }
wdenkc6097192002-11-03 00:24:07 +0000557}
558
559
560/******************************************************************************/
561
Gabe Black76a5d322011-11-14 19:24:14 +0000562static void kbd_caps(unsigned char scan_code)
wdenkc6097192002-11-03 00:24:07 +0000563{
Gabe Black76a5d322011-11-14 19:24:14 +0000564 if ((kbd_flags & BRK) == NORMAL) {
565 kbd_flags ^= CAPS;
566 kbd_led_set(); /* update keyboard LED */
567 }
wdenkc6097192002-11-03 00:24:07 +0000568}
569
570
571/******************************************************************************/
572
Gabe Black76a5d322011-11-14 19:24:14 +0000573static void kbd_num(unsigned char scan_code)
wdenkc6097192002-11-03 00:24:07 +0000574{
Gabe Black76a5d322011-11-14 19:24:14 +0000575 if ((kbd_flags & BRK) == NORMAL) {
576 kbd_flags ^= NUM;
577 kbd_state = (kbd_flags & NUM) ? AS : NM;
578 kbd_led_set(); /* update keyboard LED */
579 }
wdenkc6097192002-11-03 00:24:07 +0000580}
581
582
583/******************************************************************************/
584
Gabe Black76a5d322011-11-14 19:24:14 +0000585static void kbd_scroll(unsigned char scan_code)
wdenkc6097192002-11-03 00:24:07 +0000586{
Gabe Black76a5d322011-11-14 19:24:14 +0000587 if ((kbd_flags & BRK) == NORMAL) {
588 kbd_flags ^= STP;
589 kbd_led_set(); /* update keyboard LED */
590 if (kbd_flags & STP)
591 kbd_input = 0x13;
592 else
593 kbd_input = 0x11;
594 }
wdenkc6097192002-11-03 00:24:07 +0000595}
596
597/******************************************************************************/
598
Gabe Black76a5d322011-11-14 19:24:14 +0000599static void kbd_alt(unsigned char scan_code)
wdenkc6097192002-11-03 00:24:07 +0000600{
Gabe Black76a5d322011-11-14 19:24:14 +0000601 if ((kbd_flags & BRK) == BRK) {
602 kbd_state = AS;
603 kbd_flags &= (~ALT);
604 } else {
605 kbd_state = AK;
606 kbd_flags &= ALT;
607 }
wdenkc6097192002-11-03 00:24:07 +0000608}
609
610
611/******************************************************************************/
612
Gabe Black76a5d322011-11-14 19:24:14 +0000613static void kbd_led_set(void)
wdenkc6097192002-11-03 00:24:07 +0000614{
Gabe Black76a5d322011-11-14 19:24:14 +0000615 kbd_input_empty();
616 out8(I8042_DATA_REG, 0xed); /* SET LED command */
617 kbd_input_empty();
618 out8(I8042_DATA_REG, (kbd_flags & 0x7)); /* LED bits only */
wdenkc6097192002-11-03 00:24:07 +0000619}
620
621
622/******************************************************************************/
623
Gabe Black76a5d322011-11-14 19:24:14 +0000624static int kbd_input_empty(void)
wdenkc6097192002-11-03 00:24:07 +0000625{
Gabe Blackc4459cb2012-10-12 14:02:01 +0000626 int kbdTimeout = KBD_TIMEOUT * 1000;
wdenkc6097192002-11-03 00:24:07 +0000627
Gabe Blackc4459cb2012-10-12 14:02:01 +0000628 while ((in8(I8042_STATUS_REG) & I8042_STATUS_IN_DATA) && kbdTimeout--)
629 udelay(1);
wdenkc6097192002-11-03 00:24:07 +0000630
Gabe Black15bbe8d2011-11-14 20:18:12 +0000631 return kbdTimeout != -1;
wdenkc6097192002-11-03 00:24:07 +0000632}
633
634/******************************************************************************/
635
Marc Jones84f8db62012-10-11 15:15:52 +0000636static int wait_until_kbd_output_full(void)
637{
638 int kbdTimeout = KBD_TIMEOUT * 1000;
639
640 while (((in8(I8042_STATUS_REG) & 0x01) == 0) && kbdTimeout--)
641 udelay(1);
642
643 return kbdTimeout != -1;
644}
645
646/******************************************************************************/
647
Gabe Black76a5d322011-11-14 19:24:14 +0000648static int kbd_reset(void)
wdenkc6097192002-11-03 00:24:07 +0000649{
Marc Jones84f8db62012-10-11 15:15:52 +0000650 /* KB Reset */
Gabe Black76a5d322011-11-14 19:24:14 +0000651 if (kbd_input_empty() == 0)
652 return -1;
wdenkc6097192002-11-03 00:24:07 +0000653
Gabe Black76a5d322011-11-14 19:24:14 +0000654 out8(I8042_DATA_REG, 0xff);
wdenkc6097192002-11-03 00:24:07 +0000655
Marc Jones84f8db62012-10-11 15:15:52 +0000656 if (wait_until_kbd_output_full() == 0)
657 return -1;
658
659 if (in8(I8042_DATA_REG) != 0xfa) /* ACK */
660 return -1;
661
662 if (wait_until_kbd_output_full() == 0)
663 return -1;
664
665 if (in8(I8042_DATA_REG) != 0xaa) /* Test Pass*/
666 return -1;
667
Gabe Black76a5d322011-11-14 19:24:14 +0000668 if (kbd_input_empty() == 0)
669 return -1;
wdenkc6097192002-11-03 00:24:07 +0000670
Marc Jones84f8db62012-10-11 15:15:52 +0000671 /* Set KBC mode */
Gabe Black76a5d322011-11-14 19:24:14 +0000672 out8(I8042_COMMAND_REG, 0x60);
wdenkc6097192002-11-03 00:24:07 +0000673
Gabe Black76a5d322011-11-14 19:24:14 +0000674 if (kbd_input_empty() == 0)
675 return -1;
wdenkc6097192002-11-03 00:24:07 +0000676
Gabe Black76a5d322011-11-14 19:24:14 +0000677 out8(I8042_DATA_REG, 0x45);
wdenkc6097192002-11-03 00:24:07 +0000678
Gabe Black76a5d322011-11-14 19:24:14 +0000679 if (kbd_input_empty() == 0)
680 return -1;
wdenkc6097192002-11-03 00:24:07 +0000681
Marc Jones84f8db62012-10-11 15:15:52 +0000682 /* Enable Keyboard */
Gabe Black76a5d322011-11-14 19:24:14 +0000683 out8(I8042_COMMAND_REG, 0xae);
Simon Glass2d1e0292015-03-26 09:29:41 -0600684 if (kbd_input_empty() == 0)
685 return -1;
686
687 out8(I8042_COMMAND_REG, 0x60);
688 if (kbd_input_empty() == 0)
689 return -1;
wdenkc6097192002-11-03 00:24:07 +0000690
Simon Glass2d1e0292015-03-26 09:29:41 -0600691 out8(I8042_DATA_REG, 0xf4);
Gabe Black76a5d322011-11-14 19:24:14 +0000692 if (kbd_input_empty() == 0)
693 return -1;
wdenkc6097192002-11-03 00:24:07 +0000694
Gabe Black76a5d322011-11-14 19:24:14 +0000695 return 0;
wdenkc6097192002-11-03 00:24:07 +0000696}