blob: 26958aaa3eaca4e9a3dced2bd3d6fc81735a08bb [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * (C) Copyright 2002 ELTEC Elektronik AG
3 * Frank Gottschling <fgottschling@eltec.de>
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Wolfgang Denkbfc81252006-03-06 13:03:37 +010015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wdenkc6097192002-11-03 00:24:07 +000016 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24/* i8042.c - Intel 8042 keyboard driver routines */
25
26/* includes */
27
28#include <common.h>
Gabe Black48edb302012-10-12 14:02:02 +000029#include <linux/compiler.h>
wdenkc6097192002-11-03 00:24:07 +000030
Stefan Roesea7b9fb92006-01-18 20:05:34 +010031#ifdef CONFIG_USE_CPCIDVI
Gabe Blackdd4a5b22011-11-14 19:24:14 +000032extern u8 gt_cpcidvi_in8(u32 offset);
Stefan Roesea7b9fb92006-01-18 20:05:34 +010033extern void gt_cpcidvi_out8(u32 offset, u8 data);
34
Wolfgang Denkbfc81252006-03-06 13:03:37 +010035#define in8(a) gt_cpcidvi_in8(a)
Gabe Blackdd4a5b22011-11-14 19:24:14 +000036#define out8(a, b) gt_cpcidvi_out8(a, b)
Stefan Roesea7b9fb92006-01-18 20:05:34 +010037#endif
38
wdenkc6097192002-11-03 00:24:07 +000039#include <i8042.h>
40
41/* defines */
42
43#ifdef CONFIG_CONSOLE_CURSOR
Gabe Blackdd4a5b22011-11-14 19:24:14 +000044extern void console_cursor(int state);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020045static int blinkCount = CONFIG_SYS_CONSOLE_BLINK_COUNT;
Gabe Blackdd4a5b22011-11-14 19:24:14 +000046static int cursor_state;
wdenkc6097192002-11-03 00:24:07 +000047#endif
48
49/* locals */
50
Wolfgang Denkbfc81252006-03-06 13:03:37 +010051static int kbd_input = -1; /* no input yet */
52static int kbd_mapping = KBD_US; /* default US keyboard */
53static int kbd_flags = NORMAL; /* after reset */
Gabe Blackdd4a5b22011-11-14 19:24:14 +000054static int kbd_state; /* unshift code */
wdenkc6097192002-11-03 00:24:07 +000055
Gabe Blackdd4a5b22011-11-14 19:24:14 +000056static void kbd_conv_char(unsigned char scan_code);
57static void kbd_led_set(void);
58static void kbd_normal(unsigned char scan_code);
59static void kbd_shift(unsigned char scan_code);
60static void kbd_ctrl(unsigned char scan_code);
61static void kbd_num(unsigned char scan_code);
62static void kbd_caps(unsigned char scan_code);
63static void kbd_scroll(unsigned char scan_code);
64static void kbd_alt(unsigned char scan_code);
65static int kbd_input_empty(void);
66static int kbd_reset(void);
wdenkc6097192002-11-03 00:24:07 +000067
Gabe Blackdd4a5b22011-11-14 19:24:14 +000068static unsigned char kbd_fct_map[144] = {
69 /* kbd_fct_map table for scan code */
70 0, AS, AS, AS, AS, AS, AS, AS, /* scan 0- 7 */
71 AS, AS, AS, AS, AS, AS, AS, AS, /* scan 8- F */
72 AS, AS, AS, AS, AS, AS, AS, AS, /* scan 10-17 */
73 AS, AS, AS, AS, AS, CN, AS, AS, /* scan 18-1F */
74 AS, AS, AS, AS, AS, AS, AS, AS, /* scan 20-27 */
75 AS, AS, SH, AS, AS, AS, AS, AS, /* scan 28-2F */
76 AS, AS, AS, AS, AS, AS, SH, AS, /* scan 30-37 */
77 AS, AS, CP, 0, 0, 0, 0, 0, /* scan 38-3F */
78 0, 0, 0, 0, 0, NM, ST, ES, /* scan 40-47 */
79 ES, ES, ES, ES, ES, ES, ES, ES, /* scan 48-4F */
80 ES, ES, ES, ES, 0, 0, AS, 0, /* scan 50-57 */
81 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
82 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
83 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
84 AS, 0, 0, AS, 0, 0, AS, 0, /* scan 70-77 */
85 0, AS, 0, 0, 0, AS, 0, 0, /* scan 78-7F */
86 AS, CN, AS, AS, AK, ST, EX, EX, /* enhanced */
87 AS, EX, EX, AS, EX, AS, EX, EX /* enhanced */
88 };
wdenkc6097192002-11-03 00:24:07 +000089
Gabe Blackdd4a5b22011-11-14 19:24:14 +000090static unsigned char kbd_key_map[2][5][144] = {
91 { /* US keyboard */
92 { /* unshift code */
93 0, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 0- 7 */
94 '7', '8', '9', '0', '-', '=', 0x08, '\t', /* scan 8- F */
95 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', /* scan 10-17 */
96 'o', 'p', '[', ']', '\r', CN, 'a', 's', /* scan 18-1F */
97 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* scan 20-27 */
98 '\'', '`', SH, '\\', 'z', 'x', 'c', 'v', /* scan 28-2F */
99 'b', 'n', 'm', ',', '.', '/', SH, '*', /* scan 30-37 */
100 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
101 0, 0, 0, 0, 0, NM, ST, '7', /* scan 40-47 */
102 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
103 '2', '3', '0', '.', 0, 0, 0, 0, /* scan 50-57 */
104 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
105 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
106 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
107 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
108 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
109 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
110 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
111 },
112 { /* shift code */
113 0, 0x1b, '!', '@', '#', '$', '%', '^', /* scan 0- 7 */
114 '&', '*', '(', ')', '_', '+', 0x08, '\t', /* scan 8- F */
115 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', /* scan 10-17 */
116 'O', 'P', '{', '}', '\r', CN, 'A', 'S', /* scan 18-1F */
117 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', /* scan 20-27 */
118 '"', '~', SH, '|', 'Z', 'X', 'C', 'V', /* scan 28-2F */
119 'B', 'N', 'M', '<', '>', '?', SH, '*', /* scan 30-37 */
120 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
121 0, 0, 0, 0, 0, NM, ST, '7', /* scan 40-47 */
122 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
123 '2', '3', '0', '.', 0, 0, 0, 0, /* scan 50-57 */
124 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
125 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
126 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
127 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
128 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
129 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
130 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
131 },
132 { /* control code */
133 0xff, 0x1b, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, /* scan 0- 7 */
134 0x1e, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, '\t', /* scan 8- F */
135 0x11, 0x17, 0x05, 0x12, 0x14, 0x19, 0x15, 0x09, /* scan 10-17 */
136 0x0f, 0x10, 0x1b, 0x1d, '\r', CN, 0x01, 0x13, /* scan 18-1F */
137 0x04, 0x06, 0x07, 0x08, 0x0a, 0x0b, 0x0c, 0xff, /* scan 20-27 */
138 0xff, 0x1c, SH, 0xff, 0x1a, 0x18, 0x03, 0x16, /* scan 28-2F */
139 0x02, 0x0e, 0x0d, 0xff, 0xff, 0xff, SH, 0xff, /* scan 30-37 */
140 0xff, 0xff, CP, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
141 0xff, 0xff, 0xff, 0xff, 0xff, NM, ST, 0xff, /* scan 40-47 */
142 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48-4F */
143 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 50-57 */
144 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58-5F */
145 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60-67 */
146 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68-6F */
147 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70-77 */
148 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78-7F */
149 '\r', CN, '/', '*', ' ', ST, 0xff, 0xff, /* extended */
150 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* extended */
151 },
152 { /* non numeric code */
153 0, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 0- 7 */
154 '7', '8', '9', '0', '-', '=', 0x08, '\t', /* scan 8- F */
155 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', /* scan 10-17 */
156 'o', 'p', '[', ']', '\r', CN, 'a', 's', /* scan 18-1F */
157 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* scan 20-27 */
158 '\'', '`', SH, '\\', 'z', 'x', 'c', 'v', /* scan 28-2F */
159 'b', 'n', 'm', ',', '.', '/', SH, '*', /* scan 30-37 */
160 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
161 0, 0, 0, 0, 0, NM, ST, 'w', /* scan 40-47 */
162 'x', 'y', 'l', 't', 'u', 'v', 'm', 'q', /* scan 48-4F */
163 'r', 's', 'p', 'n', 0, 0, 0, 0, /* scan 50-57 */
164 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
165 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
166 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
167 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
168 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
169 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
170 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
171 },
172 { /* right alt mode - not used in US keyboard */
173 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 0 - 7 */
174 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 8 - F */
175 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 10 -17 */
176 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 18 -1F */
177 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 20 -27 */
178 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 28 -2F */
179 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 30 -37 */
180 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38 -3F */
181 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 40 -47 */
182 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48 -4F */
183 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 50 -57 */
184 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58 -5F */
185 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60 -67 */
186 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68 -6F */
187 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70 -77 */
188 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78 -7F */
189 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* extended */
190 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* extended */
191 }
192 },
193 { /* german keyboard */
194 { /* unshift code */
195 0, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 0- 7 */
196 '7', '8', '9', '0', 0xe1, '\'', 0x08, '\t', /* scan 8- F */
197 'q', 'w', 'e', 'r', 't', 'z', 'u', 'i', /* scan 10-17 */
198 'o', 'p', 0x81, '+', '\r', CN, 'a', 's', /* scan 18-1F */
199 'd', 'f', 'g', 'h', 'j', 'k', 'l', 0x94, /* scan 20-27 */
200 0x84, '^', SH, '#', 'y', 'x', 'c', 'v', /* scan 28-2F */
201 'b', 'n', 'm', ',', '.', '-', SH, '*', /* scan 30-37 */
202 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
203 0, 0, 0, 0, 0, NM, ST, '7', /* scan 40-47 */
204 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
205 '2', '3', '0', ',', 0, 0, '<', 0, /* scan 50-57 */
206 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
207 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
208 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
209 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
210 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
211 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
212 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
213 },
214 { /* shift code */
215 0, 0x1b, '!', '"', 0x15, '$', '%', '&', /* scan 0- 7 */
216 '/', '(', ')', '=', '?', '`', 0x08, '\t', /* scan 8- F */
217 'Q', 'W', 'E', 'R', 'T', 'Z', 'U', 'I', /* scan 10-17 */
218 'O', 'P', 0x9a, '*', '\r', CN, 'A', 'S', /* scan 18-1F */
219 'D', 'F', 'G', 'H', 'J', 'K', 'L', 0x99, /* scan 20-27 */
220 0x8e, 0xf8, SH, '\'', 'Y', 'X', 'C', 'V', /* scan 28-2F */
221 'B', 'N', 'M', ';', ':', '_', SH, '*', /* scan 30-37 */
222 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
223 0, 0, 0, 0, 0, NM, ST, '7', /* scan 40-47 */
224 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
225 '2', '3', '0', ',', 0, 0, '>', 0, /* scan 50-57 */
226 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
227 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
228 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
229 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
230 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
231 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
232 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
233 },
234 { /* control code */
235 0xff, 0x1b, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, /* scan 0- 7 */
236 0x1e, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, '\t', /* scan 8- F */
237 0x11, 0x17, 0x05, 0x12, 0x14, 0x19, 0x15, 0x09, /* scan 10-17 */
238 0x0f, 0x10, 0x1b, 0x1d, '\r', CN, 0x01, 0x13, /* scan 18-1F */
239 0x04, 0x06, 0x07, 0x08, 0x0a, 0x0b, 0x0c, 0xff, /* scan 20-27 */
240 0xff, 0x1c, SH, 0xff, 0x1a, 0x18, 0x03, 0x16, /* scan 28-2F */
241 0x02, 0x0e, 0x0d, 0xff, 0xff, 0xff, SH, 0xff, /* scan 30-37 */
242 0xff, 0xff, CP, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
243 0xff, 0xff, 0xff, 0xff, 0xff, NM, ST, 0xff, /* scan 40-47 */
244 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48-4F */
245 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 50-57 */
246 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58-5F */
247 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60-67 */
248 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68-6F */
249 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70-77 */
250 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78-7F */
251 '\r', CN, '/', '*', ' ', ST, 0xff, 0xff, /* extended */
252 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* extended */
253 },
254 { /* non numeric code */
255 0, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 0- 7 */
256 '7', '8', '9', '0', 0xe1, '\'', 0x08, '\t', /* scan 8- F */
257 'q', 'w', 'e', 'r', 't', 'z', 'u', 'i', /* scan 10-17 */
258 'o', 'p', 0x81, '+', '\r', CN, 'a', 's', /* scan 18-1F */
259 'd', 'f', 'g', 'h', 'j', 'k', 'l', 0x94, /* scan 20-27 */
260 0x84, '^', SH, 0, 'y', 'x', 'c', 'v', /* scan 28-2F */
261 'b', 'n', 'm', ',', '.', '-', SH, '*', /* scan 30-37 */
262 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
263 0, 0, 0, 0, 0, NM, ST, 'w', /* scan 40-47 */
264 'x', 'y', 'l', 't', 'u', 'v', 'm', 'q', /* scan 48-4F */
265 'r', 's', 'p', 'n', 0, 0, '<', 0, /* scan 50-57 */
266 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
267 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
268 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
269 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
270 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
271 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
272 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
273 },
274 { /* Right alt mode - is used in German keyboard */
275 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 0 - 7 */
276 '{', '[', ']', '}', '\\', 0xff, 0xff, 0xff, /* scan 8 - F */
277 '@', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 10 -17 */
278 0xff, 0xff, 0xff, '~', 0xff, 0xff, 0xff, 0xff, /* scan 18 -1F */
279 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 20 -27 */
280 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 28 -2F */
281 0xff, 0xff, 0xe6, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 30 -37 */
282 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38 -3F */
283 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 40 -47 */
284 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48 -4F */
285 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '|', 0xff, /* scan 50 -57 */
286 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58 -5F */
287 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60 -67 */
288 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68 -6F */
289 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70 -77 */
290 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78 -7F */
291 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* extended */
292 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* extended */
293 }
294 }
295 };
wdenkc6097192002-11-03 00:24:07 +0000296
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000297static unsigned char ext_key_map[] = {
298 0x1c, /* keypad enter */
299 0x1d, /* right control */
300 0x35, /* keypad slash */
301 0x37, /* print screen */
302 0x38, /* right alt */
303 0x46, /* break */
304 0x47, /* editpad home */
305 0x48, /* editpad up */
306 0x49, /* editpad pgup */
307 0x4b, /* editpad left */
308 0x4d, /* editpad right */
309 0x4f, /* editpad end */
310 0x50, /* editpad dn */
311 0x51, /* editpad pgdn */
312 0x52, /* editpad ins */
313 0x53, /* editpad del */
314 0x00 /* map end */
315 };
wdenkc6097192002-11-03 00:24:07 +0000316
Gabe Black22e0f5a2011-11-14 20:18:12 +0000317/******************************************************************************/
318
319static int kbd_controller_present(void)
320{
321 return in8(I8042_STATUS_REG) != 0xff;
322}
323
Gabe Black48edb302012-10-12 14:02:02 +0000324/*
325 * Implement a weak default function for boards that optionally
326 * need to skip the i8042 initialization.
327 */
328int __weak board_i8042_skip(void)
329{
330 /* As default, don't skip */
331 return 0;
332}
333
Louis Yung-Chieh Lo45fe6682012-10-11 15:15:51 +0000334void i8042_flush(void)
335{
336 int timeout;
337
338 /*
339 * The delay is to give the keyboard controller some time to fill the
340 * next byte.
341 */
342 while (1) {
343 timeout = 100; /* wait for no longer than 100us */
344 while (timeout > 0 && !(in8(I8042_STATUS_REG) & 0x01)) {
345 udelay(1);
346 timeout--;
347 }
348
349 /* Try to pull next byte if not timeout. */
350 if (in8(I8042_STATUS_REG) & 0x01)
351 in8(I8042_DATA_REG);
352 else
353 break;
354 }
355}
356
357int i8042_disable(void)
358{
359 if (kbd_input_empty() == 0)
360 return -1;
361
362 /* Disable keyboard */
363 out8(I8042_COMMAND_REG, 0xad);
364
365 if (kbd_input_empty() == 0)
366 return -1;
367
368 return 0;
369}
370
371
wdenkc6097192002-11-03 00:24:07 +0000372/*******************************************************************************
373 *
374 * i8042_kbd_init - reset keyboard and init state flags
375 */
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000376int i8042_kbd_init(void)
wdenkc6097192002-11-03 00:24:07 +0000377{
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000378 int keymap, try;
379 char *penv;
wdenkc6097192002-11-03 00:24:07 +0000380
Gabe Black48edb302012-10-12 14:02:02 +0000381 if (!kbd_controller_present() || board_i8042_skip())
Gabe Black22e0f5a2011-11-14 20:18:12 +0000382 return -1;
383
Stefan Roesea7b9fb92006-01-18 20:05:34 +0100384#ifdef CONFIG_USE_CPCIDVI
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000385 penv = getenv("console");
386 if (penv != NULL) {
387 if (strncmp(penv, "serial", 7) == 0)
388 return -1;
389 }
Stefan Roesea7b9fb92006-01-18 20:05:34 +0100390#endif
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000391 /* Init keyboard device (default US layout) */
392 keymap = KBD_US;
393 penv = getenv("keymap");
394 if (penv != NULL) {
395 if (strncmp(penv, "de", 3) == 0)
396 keymap = KBD_GER;
397 }
wdenkc6097192002-11-03 00:24:07 +0000398
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000399 for (try = 0; try < KBD_RESET_TRIES; try++) {
400 if (kbd_reset() == 0) {
401 kbd_mapping = keymap;
402 kbd_flags = NORMAL;
403 kbd_state = 0;
404 kbd_led_set();
405 return 0;
406 }
407 }
408 return -1;
wdenkc6097192002-11-03 00:24:07 +0000409}
410
411
412/*******************************************************************************
413 *
414 * i8042_tstc - test if keyboard input is available
Wolfgang Denkbfc81252006-03-06 13:03:37 +0100415 * option: cursor blinking if called in a loop
wdenkc6097192002-11-03 00:24:07 +0000416 */
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000417int i8042_tstc(void)
wdenkc6097192002-11-03 00:24:07 +0000418{
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000419 unsigned char scan_code = 0;
wdenkc6097192002-11-03 00:24:07 +0000420
421#ifdef CONFIG_CONSOLE_CURSOR
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000422 if (--blinkCount == 0) {
423 cursor_state ^= 1;
424 console_cursor(cursor_state);
425 blinkCount = CONFIG_SYS_CONSOLE_BLINK_COUNT;
426 udelay(10);
427 }
wdenkc6097192002-11-03 00:24:07 +0000428#endif
429
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000430 if ((in8(I8042_STATUS_REG) & 0x01) == 0) {
431 return 0;
432 } else {
433 scan_code = in8(I8042_DATA_REG);
434 if (scan_code == 0xfa)
435 return 0;
436
437 kbd_conv_char(scan_code);
438
439 if (kbd_input != -1)
440 return 1;
441 }
wdenk8bde7f72003-06-27 21:31:46 +0000442 return 0;
wdenkc6097192002-11-03 00:24:07 +0000443}
444
445
446/*******************************************************************************
447 *
448 * i8042_getc - wait till keyboard input is available
Wolfgang Denkbfc81252006-03-06 13:03:37 +0100449 * option: turn on/off cursor while waiting
wdenkc6097192002-11-03 00:24:07 +0000450 */
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000451int i8042_getc(void)
wdenkc6097192002-11-03 00:24:07 +0000452{
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000453 int ret_chr;
454 unsigned char scan_code;
wdenkc6097192002-11-03 00:24:07 +0000455
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000456 while (kbd_input == -1) {
457 while ((in8(I8042_STATUS_REG) & 0x01) == 0) {
wdenkc6097192002-11-03 00:24:07 +0000458#ifdef CONFIG_CONSOLE_CURSOR
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000459 if (--blinkCount == 0) {
460 cursor_state ^= 1;
461 console_cursor(cursor_state);
462 blinkCount = CONFIG_SYS_CONSOLE_BLINK_COUNT;
463 }
464 udelay(10);
wdenkc6097192002-11-03 00:24:07 +0000465#endif
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000466 }
467 scan_code = in8(I8042_DATA_REG);
468 if (scan_code != 0xfa)
469 kbd_conv_char (scan_code);
470 }
471 ret_chr = kbd_input;
472 kbd_input = -1;
473 return ret_chr;
474}
475
476
477/******************************************************************************/
478
479static void kbd_conv_char(unsigned char scan_code)
480{
481 if (scan_code == 0xe0) {
482 kbd_flags |= EXT;
483 return;
wdenk8bde7f72003-06-27 21:31:46 +0000484 }
wdenkc6097192002-11-03 00:24:07 +0000485
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000486 /* if high bit of scan_code, set break flag */
487 if (scan_code & 0x80)
488 kbd_flags |= BRK;
wdenk8bde7f72003-06-27 21:31:46 +0000489 else
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000490 kbd_flags &= ~BRK;
wdenkc6097192002-11-03 00:24:07 +0000491
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000492 if ((scan_code == 0xe1) || (kbd_flags & E1)) {
493 if (scan_code == 0xe1) {
494 kbd_flags ^= BRK; /* reset the break flag */
495 kbd_flags ^= E1; /* bitwise EXOR with E1 flag */
496 }
497 return;
498 }
wdenkc6097192002-11-03 00:24:07 +0000499
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000500 scan_code &= 0x7f;
501
502 if (kbd_flags & EXT) {
503 int i;
504
505 kbd_flags ^= EXT;
506 for (i = 0; ext_key_map[i]; i++) {
507 if (ext_key_map[i] == scan_code) {
508 scan_code = 0x80 + i;
509 break;
510 }
511 }
512 /* not found ? */
513 if (!ext_key_map[i])
514 return;
515 }
516
517 switch (kbd_fct_map[scan_code]) {
518 case AS:
519 kbd_normal(scan_code);
520 break;
521 case SH:
522 kbd_shift(scan_code);
523 break;
524 case CN:
525 kbd_ctrl(scan_code);
526 break;
527 case NM:
528 kbd_num(scan_code);
529 break;
530 case CP:
531 kbd_caps(scan_code);
532 break;
533 case ST:
534 kbd_scroll(scan_code);
535 break;
536 case AK:
537 kbd_alt(scan_code);
538 break;
539 }
540 return;
wdenkc6097192002-11-03 00:24:07 +0000541}
542
543
544/******************************************************************************/
545
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000546static void kbd_normal(unsigned char scan_code)
wdenkc6097192002-11-03 00:24:07 +0000547{
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000548 unsigned char chr;
549
550 if ((kbd_flags & BRK) == NORMAL) {
551 chr = kbd_key_map[kbd_mapping][kbd_state][scan_code];
552 if ((chr == 0xff) || (chr == 0x00))
553 return;
554
555 /* if caps lock convert upper to lower */
556 if (((kbd_flags & CAPS) == CAPS) &&
557 (chr >= 'a' && chr <= 'z')) {
558 chr -= 'a' - 'A';
559 }
560 kbd_input = chr;
561 }
wdenkc6097192002-11-03 00:24:07 +0000562}
563
564
565/******************************************************************************/
566
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000567static void kbd_shift(unsigned char scan_code)
wdenkc6097192002-11-03 00:24:07 +0000568{
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000569 if ((kbd_flags & BRK) == BRK) {
570 kbd_state = AS;
571 kbd_flags &= (~SHIFT);
572 } else {
573 kbd_state = SH;
574 kbd_flags |= SHIFT;
575 }
576}
wdenkc6097192002-11-03 00:24:07 +0000577
wdenkc6097192002-11-03 00:24:07 +0000578
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000579/******************************************************************************/
580
581static void kbd_ctrl(unsigned char scan_code)
582{
583 if ((kbd_flags & BRK) == BRK) {
584 kbd_state = AS;
585 kbd_flags &= (~CTRL);
586 } else {
587 kbd_state = CN;
588 kbd_flags |= CTRL;
589 }
590}
591
592
593/******************************************************************************/
594
595static void kbd_caps(unsigned char scan_code)
596{
597 if ((kbd_flags & BRK) == NORMAL) {
598 kbd_flags ^= CAPS;
599 kbd_led_set(); /* update keyboard LED */
600 }
601}
602
603
604/******************************************************************************/
605
606static void kbd_num(unsigned char scan_code)
607{
608 if ((kbd_flags & BRK) == NORMAL) {
609 kbd_flags ^= NUM;
610 kbd_state = (kbd_flags & NUM) ? AS : NM;
611 kbd_led_set(); /* update keyboard LED */
612 }
613}
614
615
616/******************************************************************************/
617
618static void kbd_scroll(unsigned char scan_code)
619{
620 if ((kbd_flags & BRK) == NORMAL) {
621 kbd_flags ^= STP;
622 kbd_led_set(); /* update keyboard LED */
623 if (kbd_flags & STP)
624 kbd_input = 0x13;
625 else
626 kbd_input = 0x11;
627 }
wdenkc6097192002-11-03 00:24:07 +0000628}
629
630/******************************************************************************/
631
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000632static void kbd_alt(unsigned char scan_code)
wdenkc6097192002-11-03 00:24:07 +0000633{
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000634 if ((kbd_flags & BRK) == BRK) {
635 kbd_state = AS;
636 kbd_flags &= (~ALT);
637 } else {
638 kbd_state = AK;
639 kbd_flags &= ALT;
640 }
641}
wdenkc6097192002-11-03 00:24:07 +0000642
wdenkc6097192002-11-03 00:24:07 +0000643
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000644/******************************************************************************/
wdenkc6097192002-11-03 00:24:07 +0000645
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000646static void kbd_led_set(void)
647{
648 kbd_input_empty();
649 out8(I8042_DATA_REG, 0xed); /* SET LED command */
650 kbd_input_empty();
651 out8(I8042_DATA_REG, (kbd_flags & 0x7)); /* LED bits only */
652}
653
654
655/******************************************************************************/
656
657static int kbd_input_empty(void)
658{
Gabe Blackef94f7f2012-10-12 14:02:01 +0000659 int kbdTimeout = KBD_TIMEOUT * 1000;
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000660
Gabe Blackef94f7f2012-10-12 14:02:01 +0000661 while ((in8(I8042_STATUS_REG) & I8042_STATUS_IN_DATA) && kbdTimeout--)
662 udelay(1);
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000663
Gabe Black22e0f5a2011-11-14 20:18:12 +0000664 return kbdTimeout != -1;
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000665}
666
667/******************************************************************************/
668
Marc Jones59a1b722012-10-11 15:15:52 +0000669static int wait_until_kbd_output_full(void)
670{
671 int kbdTimeout = KBD_TIMEOUT * 1000;
672
673 while (((in8(I8042_STATUS_REG) & 0x01) == 0) && kbdTimeout--)
674 udelay(1);
675
676 return kbdTimeout != -1;
677}
678
679/******************************************************************************/
680
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000681static int kbd_reset(void)
682{
Marc Jones59a1b722012-10-11 15:15:52 +0000683 /* KB Reset */
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000684 if (kbd_input_empty() == 0)
685 return -1;
686
687 out8(I8042_DATA_REG, 0xff);
688
Marc Jones59a1b722012-10-11 15:15:52 +0000689 if (wait_until_kbd_output_full() == 0)
690 return -1;
691
692 if (in8(I8042_DATA_REG) != 0xfa) /* ACK */
693 return -1;
694
695 if (wait_until_kbd_output_full() == 0)
696 return -1;
697
698 if (in8(I8042_DATA_REG) != 0xaa) /* Test Pass*/
699 return -1;
700
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000701 if (kbd_input_empty() == 0)
702 return -1;
wdenkc6097192002-11-03 00:24:07 +0000703
Marc Jones59a1b722012-10-11 15:15:52 +0000704 /* Set KBC mode */
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000705 out8(I8042_COMMAND_REG, 0x60);
wdenkc6097192002-11-03 00:24:07 +0000706
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000707 if (kbd_input_empty() == 0)
708 return -1;
wdenkc6097192002-11-03 00:24:07 +0000709
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000710 out8(I8042_DATA_REG, 0x45);
wdenkc6097192002-11-03 00:24:07 +0000711
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000712 if (kbd_input_empty() == 0)
713 return -1;
wdenkc6097192002-11-03 00:24:07 +0000714
Marc Jones59a1b722012-10-11 15:15:52 +0000715 /* Enable Keyboard */
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000716 out8(I8042_COMMAND_REG, 0xae);
wdenkc6097192002-11-03 00:24:07 +0000717
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000718 if (kbd_input_empty() == 0)
719 return -1;
wdenkc6097192002-11-03 00:24:07 +0000720
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000721 return 0;
wdenkc6097192002-11-03 00:24:07 +0000722}