blob: 35fa0bb5043d0c0321d49c8e674063cad5e41d22 [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>
Gabe Black13702302012-10-12 14:02:02 +000013#include <linux/compiler.h>
wdenkc6097192002-11-03 00:24:07 +000014
Stefan Roeseb7a97c72006-01-18 20:05:34 +010015#ifdef CONFIG_USE_CPCIDVI
Gabe Black76a5d322011-11-14 19:24:14 +000016extern u8 gt_cpcidvi_in8(u32 offset);
Stefan Roeseb7a97c72006-01-18 20:05:34 +010017extern void gt_cpcidvi_out8(u32 offset, u8 data);
18
Wolfgang Denk67777b92006-03-06 13:03:37 +010019#define in8(a) gt_cpcidvi_in8(a)
Gabe Black76a5d322011-11-14 19:24:14 +000020#define out8(a, b) gt_cpcidvi_out8(a, b)
Stefan Roeseb7a97c72006-01-18 20:05:34 +010021#endif
22
wdenkc6097192002-11-03 00:24:07 +000023#include <i8042.h>
24
25/* defines */
26
27#ifdef CONFIG_CONSOLE_CURSOR
Gabe Black76a5d322011-11-14 19:24:14 +000028extern void console_cursor(int state);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020029static int blinkCount = CONFIG_SYS_CONSOLE_BLINK_COUNT;
Gabe Black76a5d322011-11-14 19:24:14 +000030static int cursor_state;
wdenkc6097192002-11-03 00:24:07 +000031#endif
32
33/* locals */
34
Wolfgang Denk67777b92006-03-06 13:03:37 +010035static int kbd_input = -1; /* no input yet */
36static int kbd_mapping = KBD_US; /* default US keyboard */
37static int kbd_flags = NORMAL; /* after reset */
Gabe Black76a5d322011-11-14 19:24:14 +000038static int kbd_state; /* unshift code */
wdenkc6097192002-11-03 00:24:07 +000039
Gabe Black76a5d322011-11-14 19:24:14 +000040static void kbd_conv_char(unsigned char scan_code);
41static void kbd_led_set(void);
42static void kbd_normal(unsigned char scan_code);
43static void kbd_shift(unsigned char scan_code);
44static void kbd_ctrl(unsigned char scan_code);
45static void kbd_num(unsigned char scan_code);
46static void kbd_caps(unsigned char scan_code);
47static void kbd_scroll(unsigned char scan_code);
48static void kbd_alt(unsigned char scan_code);
49static int kbd_input_empty(void);
50static int kbd_reset(void);
wdenkc6097192002-11-03 00:24:07 +000051
Gabe Black76a5d322011-11-14 19:24:14 +000052static unsigned char kbd_fct_map[144] = {
53 /* kbd_fct_map table for scan code */
54 0, AS, AS, AS, AS, AS, AS, AS, /* scan 0- 7 */
55 AS, AS, AS, AS, AS, AS, AS, AS, /* scan 8- F */
56 AS, AS, AS, AS, AS, AS, AS, AS, /* scan 10-17 */
57 AS, AS, AS, AS, AS, CN, AS, AS, /* scan 18-1F */
58 AS, AS, AS, AS, AS, AS, AS, AS, /* scan 20-27 */
59 AS, AS, SH, AS, AS, AS, AS, AS, /* scan 28-2F */
60 AS, AS, AS, AS, AS, AS, SH, AS, /* scan 30-37 */
61 AS, AS, CP, 0, 0, 0, 0, 0, /* scan 38-3F */
62 0, 0, 0, 0, 0, NM, ST, ES, /* scan 40-47 */
63 ES, ES, ES, ES, ES, ES, ES, ES, /* scan 48-4F */
64 ES, ES, ES, ES, 0, 0, AS, 0, /* scan 50-57 */
65 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
66 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
67 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
68 AS, 0, 0, AS, 0, 0, AS, 0, /* scan 70-77 */
69 0, AS, 0, 0, 0, AS, 0, 0, /* scan 78-7F */
70 AS, CN, AS, AS, AK, ST, EX, EX, /* enhanced */
71 AS, EX, EX, AS, EX, AS, EX, EX /* enhanced */
72 };
wdenkc6097192002-11-03 00:24:07 +000073
Gabe Black76a5d322011-11-14 19:24:14 +000074static unsigned char kbd_key_map[2][5][144] = {
75 { /* US keyboard */
76 { /* unshift code */
77 0, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 0- 7 */
78 '7', '8', '9', '0', '-', '=', 0x08, '\t', /* scan 8- F */
79 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', /* scan 10-17 */
80 'o', 'p', '[', ']', '\r', CN, 'a', 's', /* scan 18-1F */
81 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* scan 20-27 */
82 '\'', '`', SH, '\\', 'z', 'x', 'c', 'v', /* scan 28-2F */
83 'b', 'n', 'm', ',', '.', '/', SH, '*', /* scan 30-37 */
84 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
85 0, 0, 0, 0, 0, NM, ST, '7', /* scan 40-47 */
86 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
87 '2', '3', '0', '.', 0, 0, 0, 0, /* scan 50-57 */
88 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
89 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
90 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
91 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
92 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
93 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
94 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
95 },
96 { /* shift code */
97 0, 0x1b, '!', '@', '#', '$', '%', '^', /* scan 0- 7 */
98 '&', '*', '(', ')', '_', '+', 0x08, '\t', /* scan 8- F */
99 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', /* scan 10-17 */
100 'O', 'P', '{', '}', '\r', CN, 'A', 'S', /* scan 18-1F */
101 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', /* scan 20-27 */
102 '"', '~', SH, '|', 'Z', 'X', 'C', 'V', /* scan 28-2F */
103 'B', 'N', 'M', '<', '>', '?', SH, '*', /* scan 30-37 */
104 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
105 0, 0, 0, 0, 0, NM, ST, '7', /* scan 40-47 */
106 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
107 '2', '3', '0', '.', 0, 0, 0, 0, /* scan 50-57 */
108 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
109 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
110 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
111 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
112 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
113 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
114 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
115 },
116 { /* control code */
117 0xff, 0x1b, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, /* scan 0- 7 */
118 0x1e, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, '\t', /* scan 8- F */
119 0x11, 0x17, 0x05, 0x12, 0x14, 0x19, 0x15, 0x09, /* scan 10-17 */
120 0x0f, 0x10, 0x1b, 0x1d, '\r', CN, 0x01, 0x13, /* scan 18-1F */
121 0x04, 0x06, 0x07, 0x08, 0x0a, 0x0b, 0x0c, 0xff, /* scan 20-27 */
122 0xff, 0x1c, SH, 0xff, 0x1a, 0x18, 0x03, 0x16, /* scan 28-2F */
123 0x02, 0x0e, 0x0d, 0xff, 0xff, 0xff, SH, 0xff, /* scan 30-37 */
124 0xff, 0xff, CP, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
125 0xff, 0xff, 0xff, 0xff, 0xff, NM, ST, 0xff, /* scan 40-47 */
126 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48-4F */
127 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 50-57 */
128 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58-5F */
129 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60-67 */
130 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68-6F */
131 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70-77 */
132 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78-7F */
133 '\r', CN, '/', '*', ' ', ST, 0xff, 0xff, /* extended */
134 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* extended */
135 },
136 { /* non numeric code */
137 0, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 0- 7 */
138 '7', '8', '9', '0', '-', '=', 0x08, '\t', /* scan 8- F */
139 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', /* scan 10-17 */
140 'o', 'p', '[', ']', '\r', CN, 'a', 's', /* scan 18-1F */
141 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* scan 20-27 */
142 '\'', '`', SH, '\\', 'z', 'x', 'c', 'v', /* scan 28-2F */
143 'b', 'n', 'm', ',', '.', '/', SH, '*', /* scan 30-37 */
144 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
145 0, 0, 0, 0, 0, NM, ST, 'w', /* scan 40-47 */
146 'x', 'y', 'l', 't', 'u', 'v', 'm', 'q', /* scan 48-4F */
147 'r', 's', 'p', 'n', 0, 0, 0, 0, /* scan 50-57 */
148 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
149 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
150 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
151 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
152 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
153 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
154 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
155 },
156 { /* right alt mode - not used in US keyboard */
157 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 0 - 7 */
158 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 8 - F */
159 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 10 -17 */
160 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 18 -1F */
161 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 20 -27 */
162 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 28 -2F */
163 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 30 -37 */
164 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38 -3F */
165 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 40 -47 */
166 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48 -4F */
167 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 50 -57 */
168 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58 -5F */
169 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60 -67 */
170 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68 -6F */
171 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70 -77 */
172 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78 -7F */
173 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* extended */
174 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* extended */
175 }
176 },
177 { /* german keyboard */
178 { /* unshift code */
179 0, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 0- 7 */
180 '7', '8', '9', '0', 0xe1, '\'', 0x08, '\t', /* scan 8- F */
181 'q', 'w', 'e', 'r', 't', 'z', 'u', 'i', /* scan 10-17 */
182 'o', 'p', 0x81, '+', '\r', CN, 'a', 's', /* scan 18-1F */
183 'd', 'f', 'g', 'h', 'j', 'k', 'l', 0x94, /* scan 20-27 */
184 0x84, '^', SH, '#', 'y', 'x', 'c', 'v', /* scan 28-2F */
185 'b', 'n', 'm', ',', '.', '-', SH, '*', /* scan 30-37 */
186 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
187 0, 0, 0, 0, 0, NM, ST, '7', /* scan 40-47 */
188 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
189 '2', '3', '0', ',', 0, 0, '<', 0, /* scan 50-57 */
190 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
191 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
192 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
193 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
194 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
195 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
196 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
197 },
198 { /* shift code */
199 0, 0x1b, '!', '"', 0x15, '$', '%', '&', /* scan 0- 7 */
200 '/', '(', ')', '=', '?', '`', 0x08, '\t', /* scan 8- F */
201 'Q', 'W', 'E', 'R', 'T', 'Z', 'U', 'I', /* scan 10-17 */
202 'O', 'P', 0x9a, '*', '\r', CN, 'A', 'S', /* scan 18-1F */
203 'D', 'F', 'G', 'H', 'J', 'K', 'L', 0x99, /* scan 20-27 */
204 0x8e, 0xf8, SH, '\'', 'Y', 'X', 'C', 'V', /* scan 28-2F */
205 'B', 'N', 'M', ';', ':', '_', SH, '*', /* scan 30-37 */
206 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
207 0, 0, 0, 0, 0, NM, ST, '7', /* scan 40-47 */
208 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
209 '2', '3', '0', ',', 0, 0, '>', 0, /* scan 50-57 */
210 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
211 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
212 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
213 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
214 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
215 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
216 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
217 },
218 { /* control code */
219 0xff, 0x1b, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, /* scan 0- 7 */
220 0x1e, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, '\t', /* scan 8- F */
221 0x11, 0x17, 0x05, 0x12, 0x14, 0x19, 0x15, 0x09, /* scan 10-17 */
222 0x0f, 0x10, 0x1b, 0x1d, '\r', CN, 0x01, 0x13, /* scan 18-1F */
223 0x04, 0x06, 0x07, 0x08, 0x0a, 0x0b, 0x0c, 0xff, /* scan 20-27 */
224 0xff, 0x1c, SH, 0xff, 0x1a, 0x18, 0x03, 0x16, /* scan 28-2F */
225 0x02, 0x0e, 0x0d, 0xff, 0xff, 0xff, SH, 0xff, /* scan 30-37 */
226 0xff, 0xff, CP, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
227 0xff, 0xff, 0xff, 0xff, 0xff, NM, ST, 0xff, /* scan 40-47 */
228 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48-4F */
229 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 50-57 */
230 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58-5F */
231 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60-67 */
232 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68-6F */
233 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70-77 */
234 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78-7F */
235 '\r', CN, '/', '*', ' ', ST, 0xff, 0xff, /* extended */
236 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* extended */
237 },
238 { /* non numeric code */
239 0, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 0- 7 */
240 '7', '8', '9', '0', 0xe1, '\'', 0x08, '\t', /* scan 8- F */
241 'q', 'w', 'e', 'r', 't', 'z', 'u', 'i', /* scan 10-17 */
242 'o', 'p', 0x81, '+', '\r', CN, 'a', 's', /* scan 18-1F */
243 'd', 'f', 'g', 'h', 'j', 'k', 'l', 0x94, /* scan 20-27 */
244 0x84, '^', SH, 0, 'y', 'x', 'c', 'v', /* scan 28-2F */
245 'b', 'n', 'm', ',', '.', '-', SH, '*', /* scan 30-37 */
246 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
247 0, 0, 0, 0, 0, NM, ST, 'w', /* scan 40-47 */
248 'x', 'y', 'l', 't', 'u', 'v', 'm', 'q', /* scan 48-4F */
249 'r', 's', 'p', 'n', 0, 0, '<', 0, /* scan 50-57 */
250 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
251 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
252 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
253 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
254 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
255 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
256 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
257 },
258 { /* Right alt mode - is used in German keyboard */
259 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 0 - 7 */
260 '{', '[', ']', '}', '\\', 0xff, 0xff, 0xff, /* scan 8 - F */
261 '@', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 10 -17 */
262 0xff, 0xff, 0xff, '~', 0xff, 0xff, 0xff, 0xff, /* scan 18 -1F */
263 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 20 -27 */
264 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 28 -2F */
265 0xff, 0xff, 0xe6, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 30 -37 */
266 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38 -3F */
267 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 40 -47 */
268 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48 -4F */
269 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '|', 0xff, /* scan 50 -57 */
270 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58 -5F */
271 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60 -67 */
272 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68 -6F */
273 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70 -77 */
274 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78 -7F */
275 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* extended */
276 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* extended */
277 }
278 }
279 };
wdenkc6097192002-11-03 00:24:07 +0000280
Gabe Black76a5d322011-11-14 19:24:14 +0000281static unsigned char ext_key_map[] = {
282 0x1c, /* keypad enter */
283 0x1d, /* right control */
284 0x35, /* keypad slash */
285 0x37, /* print screen */
286 0x38, /* right alt */
287 0x46, /* break */
288 0x47, /* editpad home */
289 0x48, /* editpad up */
290 0x49, /* editpad pgup */
291 0x4b, /* editpad left */
292 0x4d, /* editpad right */
293 0x4f, /* editpad end */
294 0x50, /* editpad dn */
295 0x51, /* editpad pgdn */
296 0x52, /* editpad ins */
297 0x53, /* editpad del */
298 0x00 /* map end */
299 };
wdenkc6097192002-11-03 00:24:07 +0000300
Gabe Black15bbe8d2011-11-14 20:18:12 +0000301/******************************************************************************/
302
303static int kbd_controller_present(void)
304{
305 return in8(I8042_STATUS_REG) != 0xff;
306}
307
Gabe Black13702302012-10-12 14:02:02 +0000308/*
309 * Implement a weak default function for boards that optionally
310 * need to skip the i8042 initialization.
311 */
312int __weak board_i8042_skip(void)
313{
314 /* As default, don't skip */
315 return 0;
316}
317
Louis Yung-Chieh Lo46d579c2012-10-11 15:15:51 +0000318void i8042_flush(void)
319{
320 int timeout;
321
322 /*
323 * The delay is to give the keyboard controller some time to fill the
324 * next byte.
325 */
326 while (1) {
327 timeout = 100; /* wait for no longer than 100us */
328 while (timeout > 0 && !(in8(I8042_STATUS_REG) & 0x01)) {
329 udelay(1);
330 timeout--;
331 }
332
333 /* Try to pull next byte if not timeout. */
334 if (in8(I8042_STATUS_REG) & 0x01)
335 in8(I8042_DATA_REG);
336 else
337 break;
338 }
339}
340
341int i8042_disable(void)
342{
343 if (kbd_input_empty() == 0)
344 return -1;
345
346 /* Disable keyboard */
347 out8(I8042_COMMAND_REG, 0xad);
348
349 if (kbd_input_empty() == 0)
350 return -1;
351
352 return 0;
353}
354
355
wdenkc6097192002-11-03 00:24:07 +0000356/*******************************************************************************
357 *
358 * i8042_kbd_init - reset keyboard and init state flags
359 */
Gabe Black76a5d322011-11-14 19:24:14 +0000360int i8042_kbd_init(void)
wdenkc6097192002-11-03 00:24:07 +0000361{
Gabe Black76a5d322011-11-14 19:24:14 +0000362 int keymap, try;
363 char *penv;
wdenkc6097192002-11-03 00:24:07 +0000364
Gabe Black13702302012-10-12 14:02:02 +0000365 if (!kbd_controller_present() || board_i8042_skip())
Gabe Black15bbe8d2011-11-14 20:18:12 +0000366 return -1;
367
Stefan Roeseb7a97c72006-01-18 20:05:34 +0100368#ifdef CONFIG_USE_CPCIDVI
Gabe Black76a5d322011-11-14 19:24:14 +0000369 penv = getenv("console");
370 if (penv != NULL) {
371 if (strncmp(penv, "serial", 7) == 0)
372 return -1;
373 }
Stefan Roeseb7a97c72006-01-18 20:05:34 +0100374#endif
Gabe Black76a5d322011-11-14 19:24:14 +0000375 /* Init keyboard device (default US layout) */
376 keymap = KBD_US;
377 penv = getenv("keymap");
378 if (penv != NULL) {
379 if (strncmp(penv, "de", 3) == 0)
380 keymap = KBD_GER;
381 }
wdenkc6097192002-11-03 00:24:07 +0000382
Gabe Black76a5d322011-11-14 19:24:14 +0000383 for (try = 0; try < KBD_RESET_TRIES; try++) {
384 if (kbd_reset() == 0) {
385 kbd_mapping = keymap;
386 kbd_flags = NORMAL;
387 kbd_state = 0;
388 kbd_led_set();
389 return 0;
390 }
391 }
392 return -1;
wdenkc6097192002-11-03 00:24:07 +0000393}
394
395
396/*******************************************************************************
397 *
398 * i8042_tstc - test if keyboard input is available
Wolfgang Denk67777b92006-03-06 13:03:37 +0100399 * option: cursor blinking if called in a loop
wdenkc6097192002-11-03 00:24:07 +0000400 */
Gabe Black76a5d322011-11-14 19:24:14 +0000401int i8042_tstc(void)
wdenkc6097192002-11-03 00:24:07 +0000402{
Gabe Black76a5d322011-11-14 19:24:14 +0000403 unsigned char scan_code = 0;
wdenkc6097192002-11-03 00:24:07 +0000404
405#ifdef CONFIG_CONSOLE_CURSOR
Gabe Black76a5d322011-11-14 19:24:14 +0000406 if (--blinkCount == 0) {
407 cursor_state ^= 1;
408 console_cursor(cursor_state);
409 blinkCount = CONFIG_SYS_CONSOLE_BLINK_COUNT;
410 udelay(10);
411 }
wdenkc6097192002-11-03 00:24:07 +0000412#endif
413
Gabe Black76a5d322011-11-14 19:24:14 +0000414 if ((in8(I8042_STATUS_REG) & 0x01) == 0) {
415 return 0;
416 } else {
417 scan_code = in8(I8042_DATA_REG);
418 if (scan_code == 0xfa)
419 return 0;
wdenkc6097192002-11-03 00:24:07 +0000420
Gabe Black76a5d322011-11-14 19:24:14 +0000421 kbd_conv_char(scan_code);
wdenkc6097192002-11-03 00:24:07 +0000422
Gabe Black76a5d322011-11-14 19:24:14 +0000423 if (kbd_input != -1)
424 return 1;
425 }
426 return 0;
wdenkc6097192002-11-03 00:24:07 +0000427}
428
429
430/*******************************************************************************
431 *
432 * i8042_getc - wait till keyboard input is available
Wolfgang Denk67777b92006-03-06 13:03:37 +0100433 * option: turn on/off cursor while waiting
wdenkc6097192002-11-03 00:24:07 +0000434 */
Gabe Black76a5d322011-11-14 19:24:14 +0000435int i8042_getc(void)
wdenkc6097192002-11-03 00:24:07 +0000436{
Gabe Black76a5d322011-11-14 19:24:14 +0000437 int ret_chr;
438 unsigned char scan_code;
wdenkc6097192002-11-03 00:24:07 +0000439
Gabe Black76a5d322011-11-14 19:24:14 +0000440 while (kbd_input == -1) {
441 while ((in8(I8042_STATUS_REG) & 0x01) == 0) {
wdenkc6097192002-11-03 00:24:07 +0000442#ifdef CONFIG_CONSOLE_CURSOR
Gabe Black76a5d322011-11-14 19:24:14 +0000443 if (--blinkCount == 0) {
444 cursor_state ^= 1;
445 console_cursor(cursor_state);
446 blinkCount = CONFIG_SYS_CONSOLE_BLINK_COUNT;
447 }
448 udelay(10);
wdenkc6097192002-11-03 00:24:07 +0000449#endif
Gabe Black76a5d322011-11-14 19:24:14 +0000450 }
451 scan_code = in8(I8042_DATA_REG);
452 if (scan_code != 0xfa)
453 kbd_conv_char (scan_code);
wdenk57b2d802003-06-27 21:31:46 +0000454 }
Gabe Black76a5d322011-11-14 19:24:14 +0000455 ret_chr = kbd_input;
456 kbd_input = -1;
457 return ret_chr;
wdenkc6097192002-11-03 00:24:07 +0000458}
459
460
461/******************************************************************************/
462
Gabe Black76a5d322011-11-14 19:24:14 +0000463static void kbd_conv_char(unsigned char scan_code)
wdenkc6097192002-11-03 00:24:07 +0000464{
Gabe Black76a5d322011-11-14 19:24:14 +0000465 if (scan_code == 0xe0) {
466 kbd_flags |= EXT;
467 return;
468 }
wdenkc6097192002-11-03 00:24:07 +0000469
Gabe Black76a5d322011-11-14 19:24:14 +0000470 /* if high bit of scan_code, set break flag */
471 if (scan_code & 0x80)
472 kbd_flags |= BRK;
473 else
474 kbd_flags &= ~BRK;
wdenkc6097192002-11-03 00:24:07 +0000475
Gabe Black76a5d322011-11-14 19:24:14 +0000476 if ((scan_code == 0xe1) || (kbd_flags & E1)) {
477 if (scan_code == 0xe1) {
478 kbd_flags ^= BRK; /* reset the break flag */
479 kbd_flags ^= E1; /* bitwise EXOR with E1 flag */
480 }
481 return;
wdenk57b2d802003-06-27 21:31:46 +0000482 }
wdenkc6097192002-11-03 00:24:07 +0000483
Gabe Black76a5d322011-11-14 19:24:14 +0000484 scan_code &= 0x7f;
wdenkc6097192002-11-03 00:24:07 +0000485
Gabe Black76a5d322011-11-14 19:24:14 +0000486 if (kbd_flags & EXT) {
487 int i;
wdenkc6097192002-11-03 00:24:07 +0000488
Gabe Black76a5d322011-11-14 19:24:14 +0000489 kbd_flags ^= EXT;
490 for (i = 0; ext_key_map[i]; i++) {
491 if (ext_key_map[i] == scan_code) {
492 scan_code = 0x80 + i;
493 break;
494 }
495 }
496 /* not found ? */
497 if (!ext_key_map[i])
498 return;
wdenk57b2d802003-06-27 21:31:46 +0000499 }
wdenkc6097192002-11-03 00:24:07 +0000500
Gabe Black76a5d322011-11-14 19:24:14 +0000501 switch (kbd_fct_map[scan_code]) {
502 case AS:
503 kbd_normal(scan_code);
504 break;
505 case SH:
506 kbd_shift(scan_code);
507 break;
508 case CN:
509 kbd_ctrl(scan_code);
510 break;
511 case NM:
512 kbd_num(scan_code);
513 break;
514 case CP:
515 kbd_caps(scan_code);
516 break;
517 case ST:
518 kbd_scroll(scan_code);
519 break;
520 case AK:
521 kbd_alt(scan_code);
522 break;
523 }
524 return;
wdenkc6097192002-11-03 00:24:07 +0000525}
526
527
528/******************************************************************************/
529
Gabe Black76a5d322011-11-14 19:24:14 +0000530static void kbd_normal(unsigned char scan_code)
wdenkc6097192002-11-03 00:24:07 +0000531{
Gabe Black76a5d322011-11-14 19:24:14 +0000532 unsigned char chr;
wdenkc6097192002-11-03 00:24:07 +0000533
Gabe Black76a5d322011-11-14 19:24:14 +0000534 if ((kbd_flags & BRK) == NORMAL) {
535 chr = kbd_key_map[kbd_mapping][kbd_state][scan_code];
536 if ((chr == 0xff) || (chr == 0x00))
537 return;
wdenkc6097192002-11-03 00:24:07 +0000538
Gabe Black76a5d322011-11-14 19:24:14 +0000539 /* if caps lock convert upper to lower */
540 if (((kbd_flags & CAPS) == CAPS) &&
541 (chr >= 'a' && chr <= 'z')) {
542 chr -= 'a' - 'A';
543 }
544 kbd_input = chr;
545 }
wdenkc6097192002-11-03 00:24:07 +0000546}
547
548
549/******************************************************************************/
550
Gabe Black76a5d322011-11-14 19:24:14 +0000551static void kbd_shift(unsigned char scan_code)
wdenkc6097192002-11-03 00:24:07 +0000552{
Gabe Black76a5d322011-11-14 19:24:14 +0000553 if ((kbd_flags & BRK) == BRK) {
554 kbd_state = AS;
555 kbd_flags &= (~SHIFT);
556 } else {
557 kbd_state = SH;
558 kbd_flags |= SHIFT;
559 }
wdenkc6097192002-11-03 00:24:07 +0000560}
561
562
563/******************************************************************************/
564
Gabe Black76a5d322011-11-14 19:24:14 +0000565static void kbd_ctrl(unsigned char scan_code)
wdenkc6097192002-11-03 00:24:07 +0000566{
Gabe Black76a5d322011-11-14 19:24:14 +0000567 if ((kbd_flags & BRK) == BRK) {
568 kbd_state = AS;
569 kbd_flags &= (~CTRL);
570 } else {
571 kbd_state = CN;
572 kbd_flags |= CTRL;
573 }
wdenkc6097192002-11-03 00:24:07 +0000574}
575
576
577/******************************************************************************/
578
Gabe Black76a5d322011-11-14 19:24:14 +0000579static void kbd_caps(unsigned char scan_code)
wdenkc6097192002-11-03 00:24:07 +0000580{
Gabe Black76a5d322011-11-14 19:24:14 +0000581 if ((kbd_flags & BRK) == NORMAL) {
582 kbd_flags ^= CAPS;
583 kbd_led_set(); /* update keyboard LED */
584 }
wdenkc6097192002-11-03 00:24:07 +0000585}
586
587
588/******************************************************************************/
589
Gabe Black76a5d322011-11-14 19:24:14 +0000590static void kbd_num(unsigned char scan_code)
wdenkc6097192002-11-03 00:24:07 +0000591{
Gabe Black76a5d322011-11-14 19:24:14 +0000592 if ((kbd_flags & BRK) == NORMAL) {
593 kbd_flags ^= NUM;
594 kbd_state = (kbd_flags & NUM) ? AS : NM;
595 kbd_led_set(); /* update keyboard LED */
596 }
wdenkc6097192002-11-03 00:24:07 +0000597}
598
599
600/******************************************************************************/
601
Gabe Black76a5d322011-11-14 19:24:14 +0000602static void kbd_scroll(unsigned char scan_code)
wdenkc6097192002-11-03 00:24:07 +0000603{
Gabe Black76a5d322011-11-14 19:24:14 +0000604 if ((kbd_flags & BRK) == NORMAL) {
605 kbd_flags ^= STP;
606 kbd_led_set(); /* update keyboard LED */
607 if (kbd_flags & STP)
608 kbd_input = 0x13;
609 else
610 kbd_input = 0x11;
611 }
wdenkc6097192002-11-03 00:24:07 +0000612}
613
614/******************************************************************************/
615
Gabe Black76a5d322011-11-14 19:24:14 +0000616static void kbd_alt(unsigned char scan_code)
wdenkc6097192002-11-03 00:24:07 +0000617{
Gabe Black76a5d322011-11-14 19:24:14 +0000618 if ((kbd_flags & BRK) == BRK) {
619 kbd_state = AS;
620 kbd_flags &= (~ALT);
621 } else {
622 kbd_state = AK;
623 kbd_flags &= ALT;
624 }
wdenkc6097192002-11-03 00:24:07 +0000625}
626
627
628/******************************************************************************/
629
Gabe Black76a5d322011-11-14 19:24:14 +0000630static void kbd_led_set(void)
wdenkc6097192002-11-03 00:24:07 +0000631{
Gabe Black76a5d322011-11-14 19:24:14 +0000632 kbd_input_empty();
633 out8(I8042_DATA_REG, 0xed); /* SET LED command */
634 kbd_input_empty();
635 out8(I8042_DATA_REG, (kbd_flags & 0x7)); /* LED bits only */
wdenkc6097192002-11-03 00:24:07 +0000636}
637
638
639/******************************************************************************/
640
Gabe Black76a5d322011-11-14 19:24:14 +0000641static int kbd_input_empty(void)
wdenkc6097192002-11-03 00:24:07 +0000642{
Gabe Blackc4459cb2012-10-12 14:02:01 +0000643 int kbdTimeout = KBD_TIMEOUT * 1000;
wdenkc6097192002-11-03 00:24:07 +0000644
Gabe Blackc4459cb2012-10-12 14:02:01 +0000645 while ((in8(I8042_STATUS_REG) & I8042_STATUS_IN_DATA) && kbdTimeout--)
646 udelay(1);
wdenkc6097192002-11-03 00:24:07 +0000647
Gabe Black15bbe8d2011-11-14 20:18:12 +0000648 return kbdTimeout != -1;
wdenkc6097192002-11-03 00:24:07 +0000649}
650
651/******************************************************************************/
652
Marc Jones84f8db62012-10-11 15:15:52 +0000653static int wait_until_kbd_output_full(void)
654{
655 int kbdTimeout = KBD_TIMEOUT * 1000;
656
657 while (((in8(I8042_STATUS_REG) & 0x01) == 0) && kbdTimeout--)
658 udelay(1);
659
660 return kbdTimeout != -1;
661}
662
663/******************************************************************************/
664
Gabe Black76a5d322011-11-14 19:24:14 +0000665static int kbd_reset(void)
wdenkc6097192002-11-03 00:24:07 +0000666{
Marc Jones84f8db62012-10-11 15:15:52 +0000667 /* KB Reset */
Gabe Black76a5d322011-11-14 19:24:14 +0000668 if (kbd_input_empty() == 0)
669 return -1;
wdenkc6097192002-11-03 00:24:07 +0000670
Gabe Black76a5d322011-11-14 19:24:14 +0000671 out8(I8042_DATA_REG, 0xff);
wdenkc6097192002-11-03 00:24:07 +0000672
Marc Jones84f8db62012-10-11 15:15:52 +0000673 if (wait_until_kbd_output_full() == 0)
674 return -1;
675
676 if (in8(I8042_DATA_REG) != 0xfa) /* ACK */
677 return -1;
678
679 if (wait_until_kbd_output_full() == 0)
680 return -1;
681
682 if (in8(I8042_DATA_REG) != 0xaa) /* Test Pass*/
683 return -1;
684
Gabe Black76a5d322011-11-14 19:24:14 +0000685 if (kbd_input_empty() == 0)
686 return -1;
wdenkc6097192002-11-03 00:24:07 +0000687
Marc Jones84f8db62012-10-11 15:15:52 +0000688 /* Set KBC mode */
Gabe Black76a5d322011-11-14 19:24:14 +0000689 out8(I8042_COMMAND_REG, 0x60);
wdenkc6097192002-11-03 00:24:07 +0000690
Gabe Black76a5d322011-11-14 19:24:14 +0000691 if (kbd_input_empty() == 0)
692 return -1;
wdenkc6097192002-11-03 00:24:07 +0000693
Gabe Black76a5d322011-11-14 19:24:14 +0000694 out8(I8042_DATA_REG, 0x45);
wdenkc6097192002-11-03 00:24:07 +0000695
Gabe Black76a5d322011-11-14 19:24:14 +0000696 if (kbd_input_empty() == 0)
697 return -1;
wdenkc6097192002-11-03 00:24:07 +0000698
Marc Jones84f8db62012-10-11 15:15:52 +0000699 /* Enable Keyboard */
Gabe Black76a5d322011-11-14 19:24:14 +0000700 out8(I8042_COMMAND_REG, 0xae);
wdenkc6097192002-11-03 00:24:07 +0000701
Gabe Black76a5d322011-11-14 19:24:14 +0000702 if (kbd_input_empty() == 0)
703 return -1;
wdenkc6097192002-11-03 00:24:07 +0000704
Gabe Black76a5d322011-11-14 19:24:14 +0000705 return 0;
wdenkc6097192002-11-03 00:24:07 +0000706}