blob: 58094c925ceccfae14e969c2567cc496feca51de [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 Denk67777b92006-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>
29
Stefan Roeseb7a97c72006-01-18 20:05:34 +010030#ifdef CONFIG_USE_CPCIDVI
31extern u8 gt_cpcidvi_in8(u32 offset);
32extern void gt_cpcidvi_out8(u32 offset, u8 data);
33
Wolfgang Denk67777b92006-03-06 13:03:37 +010034#define in8(a) gt_cpcidvi_in8(a)
Stefan Roeseb7a97c72006-01-18 20:05:34 +010035#define out8(a, b) gt_cpcidvi_out8(a,b)
36#endif
37
wdenkc6097192002-11-03 00:24:07 +000038#include <i8042.h>
39
40/* defines */
41
42#ifdef CONFIG_CONSOLE_CURSOR
43extern void console_cursor (int state);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020044static int blinkCount = CONFIG_SYS_CONSOLE_BLINK_COUNT;
wdenkc6097192002-11-03 00:24:07 +000045static int cursor_state = 0;
46#endif
47
48/* locals */
49
Wolfgang Denk67777b92006-03-06 13:03:37 +010050static int kbd_input = -1; /* no input yet */
51static int kbd_mapping = KBD_US; /* default US keyboard */
52static int kbd_flags = NORMAL; /* after reset */
53static int kbd_state = 0; /* unshift code */
wdenkc6097192002-11-03 00:24:07 +000054
55static void kbd_conv_char (unsigned char scan_code);
56static void kbd_led_set (void);
57static void kbd_normal (unsigned char scan_code);
58static void kbd_shift (unsigned char scan_code);
59static void kbd_ctrl (unsigned char scan_code);
60static void kbd_num (unsigned char scan_code);
61static void kbd_caps (unsigned char scan_code);
62static void kbd_scroll (unsigned char scan_code);
63static void kbd_alt (unsigned char scan_code);
64static int kbd_input_empty (void);
65static int kbd_reset (void);
66
67static unsigned char kbd_fct_map [144] =
68 { /* kbd_fct_map table for scan code */
Wolfgang Denk67777b92006-03-06 13:03:37 +010069 0, AS, AS, AS, AS, AS, AS, AS, /* scan 0- 7 */
70 AS, AS, AS, AS, AS, AS, AS, AS, /* scan 8- F */
71 AS, AS, AS, AS, AS, AS, AS, AS, /* scan 10-17 */
72 AS, AS, AS, AS, AS, CN, AS, AS, /* scan 18-1F */
73 AS, AS, AS, AS, AS, AS, AS, AS, /* scan 20-27 */
74 AS, AS, SH, AS, AS, AS, AS, AS, /* scan 28-2F */
75 AS, AS, AS, AS, AS, AS, SH, AS, /* scan 30-37 */
76 AS, AS, CP, 0, 0, 0, 0, 0, /* scan 38-3F */
77 0, 0, 0, 0, 0, NM, ST, ES, /* scan 40-47 */
78 ES, ES, ES, ES, ES, ES, ES, ES, /* scan 48-4F */
79 ES, ES, ES, ES, 0, 0, AS, 0, /* scan 50-57 */
80 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
81 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
82 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
83 AS, 0, 0, AS, 0, 0, AS, 0, /* scan 70-77 */
84 0, AS, 0, 0, 0, AS, 0, 0, /* scan 78-7F */
85 AS, CN, AS, AS, AK, ST, EX, EX, /* enhanced */
86 AS, EX, EX, AS, EX, AS, EX, EX /* enhanced */
wdenkc6097192002-11-03 00:24:07 +000087 };
88
89static unsigned char kbd_key_map [2][5][144] =
90 {
91 { /* US keyboard */
92 { /* unshift code */
Wolfgang Denk67777b92006-03-06 13:03:37 +010093 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 */
wdenkc6097192002-11-03 00:24:07 +0000111 },
112 { /* shift code */
Wolfgang Denk67777b92006-03-06 13:03:37 +0100113 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 */
wdenkc6097192002-11-03 00:24:07 +0000131 },
132 { /* control code */
Wolfgang Denk67777b92006-03-06 13:03:37 +0100133 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 */
wdenkc6097192002-11-03 00:24:07 +0000151 },
152 { /* non numeric code */
Wolfgang Denk67777b92006-03-06 13:03:37 +0100153 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 */
wdenkc6097192002-11-03 00:24:07 +0000171 },
172 { /* right alt mode - not used in US keyboard */
Wolfgang Denk67777b92006-03-06 13:03:37 +0100173 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 */
wdenkc6097192002-11-03 00:24:07 +0000191 }
192 },
193 { /* german keyboard */
194 { /* unshift code */
Wolfgang Denk67777b92006-03-06 13:03:37 +0100195 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 */
wdenkc6097192002-11-03 00:24:07 +0000213 },
214 { /* shift code */
Wolfgang Denk67777b92006-03-06 13:03:37 +0100215 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 */
wdenkc6097192002-11-03 00:24:07 +0000233 },
234 { /* control code */
Wolfgang Denk67777b92006-03-06 13:03:37 +0100235 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 */
wdenkc6097192002-11-03 00:24:07 +0000253 },
254 { /* non numeric code */
Wolfgang Denk67777b92006-03-06 13:03:37 +0100255 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 */
wdenkc6097192002-11-03 00:24:07 +0000273 },
274 { /* Right alt mode - is used in German keyboard */
Wolfgang Denk67777b92006-03-06 13:03:37 +0100275 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 */
wdenkc6097192002-11-03 00:24:07 +0000293 }
294 }
295 };
296
297static unsigned char ext_key_map [] =
298 {
299 0x1c, /* keypad enter */
300 0x1d, /* right control */
301 0x35, /* keypad slash */
302 0x37, /* print screen */
303 0x38, /* right alt */
304 0x46, /* break */
305 0x47, /* editpad home */
306 0x48, /* editpad up */
307 0x49, /* editpad pgup */
308 0x4b, /* editpad left */
309 0x4d, /* editpad right */
310 0x4f, /* editpad end */
311 0x50, /* editpad dn */
312 0x51, /* editpad pgdn */
313 0x52, /* editpad ins */
314 0x53, /* editpad del */
315 0x00 /* map end */
316 };
317
318/*******************************************************************************
319 *
320 * i8042_kbd_init - reset keyboard and init state flags
321 */
322int i8042_kbd_init (void)
323{
324 int keymap, try;
325 char *penv;
326
Stefan Roeseb7a97c72006-01-18 20:05:34 +0100327#ifdef CONFIG_USE_CPCIDVI
328 if ((penv = getenv ("console")) != NULL) {
Wolfgang Denk67777b92006-03-06 13:03:37 +0100329 if (strncmp (penv, "serial", 7) == 0) {
330 return -1;
Stefan Roeseb7a97c72006-01-18 20:05:34 +0100331 }
332 }
333#endif
wdenkc6097192002-11-03 00:24:07 +0000334 /* Init keyboard device (default US layout) */
335 keymap = KBD_US;
336 if ((penv = getenv ("keymap")) != NULL)
337 {
wdenk57b2d802003-06-27 21:31:46 +0000338 if (strncmp (penv, "de", 3) == 0)
339 keymap = KBD_GER;
wdenkc6097192002-11-03 00:24:07 +0000340 }
341
342 for (try = 0; try < KBD_RESET_TRIES; try++)
343 {
wdenk57b2d802003-06-27 21:31:46 +0000344 if (kbd_reset() == 0)
345 {
Wolfgang Denk67777b92006-03-06 13:03:37 +0100346 kbd_mapping = keymap;
347 kbd_flags = NORMAL;
348 kbd_state = 0;
wdenk57b2d802003-06-27 21:31:46 +0000349 kbd_led_set();
350 return 0;
351 }
wdenkc6097192002-11-03 00:24:07 +0000352 }
353 return -1;
354}
355
356
357/*******************************************************************************
358 *
359 * i8042_tstc - test if keyboard input is available
Wolfgang Denk67777b92006-03-06 13:03:37 +0100360 * option: cursor blinking if called in a loop
wdenkc6097192002-11-03 00:24:07 +0000361 */
362int i8042_tstc (void)
363{
364 unsigned char scan_code = 0;
365
366#ifdef CONFIG_CONSOLE_CURSOR
367 if (--blinkCount == 0)
368 {
wdenk57b2d802003-06-27 21:31:46 +0000369 cursor_state ^= 1;
370 console_cursor (cursor_state);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200371 blinkCount = CONFIG_SYS_CONSOLE_BLINK_COUNT;
wdenk57b2d802003-06-27 21:31:46 +0000372 udelay (10);
wdenkc6097192002-11-03 00:24:07 +0000373 }
374#endif
375
376 if ((in8 (I8042_STATUS_REG) & 0x01) == 0)
wdenk57b2d802003-06-27 21:31:46 +0000377 return 0;
wdenkc6097192002-11-03 00:24:07 +0000378 else
379 {
wdenk57b2d802003-06-27 21:31:46 +0000380 scan_code = in8 (I8042_DATA_REG);
381 if (scan_code == 0xfa)
382 return 0;
wdenkc6097192002-11-03 00:24:07 +0000383
wdenk57b2d802003-06-27 21:31:46 +0000384 kbd_conv_char(scan_code);
wdenkc6097192002-11-03 00:24:07 +0000385
wdenk57b2d802003-06-27 21:31:46 +0000386 if (kbd_input != -1)
387 return 1;
wdenkc6097192002-11-03 00:24:07 +0000388 }
389 return 0;
390}
391
392
393/*******************************************************************************
394 *
395 * i8042_getc - wait till keyboard input is available
Wolfgang Denk67777b92006-03-06 13:03:37 +0100396 * option: turn on/off cursor while waiting
wdenkc6097192002-11-03 00:24:07 +0000397 */
398int i8042_getc (void)
399{
400 int ret_chr;
401 unsigned char scan_code;
402
403 while (kbd_input == -1)
404 {
wdenk57b2d802003-06-27 21:31:46 +0000405 while ((in8 (I8042_STATUS_REG) & 0x01) == 0)
406 {
wdenkc6097192002-11-03 00:24:07 +0000407#ifdef CONFIG_CONSOLE_CURSOR
wdenk57b2d802003-06-27 21:31:46 +0000408 if (--blinkCount==0)
409 {
410 cursor_state ^= 1;
411 console_cursor (cursor_state);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200412 blinkCount = CONFIG_SYS_CONSOLE_BLINK_COUNT;
wdenk57b2d802003-06-27 21:31:46 +0000413 }
414 udelay (10);
wdenkc6097192002-11-03 00:24:07 +0000415#endif
wdenk57b2d802003-06-27 21:31:46 +0000416 }
wdenkc6097192002-11-03 00:24:07 +0000417
wdenk57b2d802003-06-27 21:31:46 +0000418 scan_code = in8 (I8042_DATA_REG);
wdenkc6097192002-11-03 00:24:07 +0000419
wdenk57b2d802003-06-27 21:31:46 +0000420 if (scan_code != 0xfa)
421 kbd_conv_char (scan_code);
wdenkc6097192002-11-03 00:24:07 +0000422 }
423 ret_chr = kbd_input;
424 kbd_input = -1;
425 return ret_chr;
426}
427
428
429/******************************************************************************/
430
431static void kbd_conv_char (unsigned char scan_code)
432{
433 if (scan_code == 0xe0)
434 {
wdenk57b2d802003-06-27 21:31:46 +0000435 kbd_flags |= EXT;
436 return;
wdenkc6097192002-11-03 00:24:07 +0000437 }
438
439 /* if high bit of scan_code, set break flag */
440 if (scan_code & 0x80)
wdenk57b2d802003-06-27 21:31:46 +0000441 kbd_flags |= BRK;
wdenkc6097192002-11-03 00:24:07 +0000442 else
wdenk57b2d802003-06-27 21:31:46 +0000443 kbd_flags &= ~BRK;
wdenkc6097192002-11-03 00:24:07 +0000444
445 if ((scan_code == 0xe1) || (kbd_flags & E1))
446 {
wdenk57b2d802003-06-27 21:31:46 +0000447 if (scan_code == 0xe1)
448 {
Wolfgang Denk67777b92006-03-06 13:03:37 +0100449 kbd_flags ^= BRK; /* reset the break flag */
450 kbd_flags ^= E1; /* bitwise EXOR with E1 flag */
wdenk57b2d802003-06-27 21:31:46 +0000451 }
452 return;
wdenkc6097192002-11-03 00:24:07 +0000453 }
454
455 scan_code &= 0x7f;
456
457 if (kbd_flags & EXT)
458 {
wdenk57b2d802003-06-27 21:31:46 +0000459 int i;
wdenkc6097192002-11-03 00:24:07 +0000460
wdenk57b2d802003-06-27 21:31:46 +0000461 kbd_flags ^= EXT;
462 for (i=0; ext_key_map[i]; i++)
463 {
464 if (ext_key_map[i] == scan_code)
465 {
466 scan_code = 0x80 + i;
467 break;
468 }
469 }
470 /* not found ? */
471 if (!ext_key_map[i])
472 return;
wdenkc6097192002-11-03 00:24:07 +0000473 }
474
475 switch (kbd_fct_map [scan_code])
476 {
477 case AS: kbd_normal (scan_code);
wdenk57b2d802003-06-27 21:31:46 +0000478 break;
wdenkc6097192002-11-03 00:24:07 +0000479 case SH: kbd_shift (scan_code);
wdenk57b2d802003-06-27 21:31:46 +0000480 break;
wdenkc6097192002-11-03 00:24:07 +0000481 case CN: kbd_ctrl (scan_code);
wdenk57b2d802003-06-27 21:31:46 +0000482 break;
wdenkc6097192002-11-03 00:24:07 +0000483 case NM: kbd_num (scan_code);
wdenk57b2d802003-06-27 21:31:46 +0000484 break;
wdenkc6097192002-11-03 00:24:07 +0000485 case CP: kbd_caps (scan_code);
wdenk57b2d802003-06-27 21:31:46 +0000486 break;
wdenkc6097192002-11-03 00:24:07 +0000487 case ST: kbd_scroll (scan_code);
wdenk57b2d802003-06-27 21:31:46 +0000488 break;
wdenkc6097192002-11-03 00:24:07 +0000489 case AK: kbd_alt (scan_code);
wdenk57b2d802003-06-27 21:31:46 +0000490 break;
wdenkc6097192002-11-03 00:24:07 +0000491 }
492 return;
493}
494
495
496/******************************************************************************/
497
498static void kbd_normal (unsigned char scan_code)
499{
500 unsigned char chr;
501
502 if ((kbd_flags & BRK) == NORMAL)
503 {
504 chr = kbd_key_map [kbd_mapping][kbd_state][scan_code];
505 if ((chr == 0xff) || (chr == 0x00))
wdenk57b2d802003-06-27 21:31:46 +0000506 {
507 return;
508 }
wdenkc6097192002-11-03 00:24:07 +0000509
wdenk57b2d802003-06-27 21:31:46 +0000510 /* if caps lock convert upper to lower */
511 if (((kbd_flags & CAPS) == CAPS) && (chr >= 'a' && chr <= 'z'))
wdenkc6097192002-11-03 00:24:07 +0000512 {
wdenk57b2d802003-06-27 21:31:46 +0000513 chr -= 'a' - 'A';
wdenkc6097192002-11-03 00:24:07 +0000514 }
515 kbd_input = chr;
516 }
517}
518
519
520/******************************************************************************/
521
522static void kbd_shift (unsigned char scan_code)
523{
524 if ((kbd_flags & BRK) == BRK)
525 {
wdenk57b2d802003-06-27 21:31:46 +0000526 kbd_state = AS;
527 kbd_flags &= (~SHIFT);
wdenkc6097192002-11-03 00:24:07 +0000528 }
529 else
530 {
531 kbd_state = SH;
532 kbd_flags |= SHIFT;
533 }
534}
535
536
537/******************************************************************************/
538
539static void kbd_ctrl (unsigned char scan_code)
540{
541 if ((kbd_flags & BRK) == BRK)
542 {
543 kbd_state = AS;
544 kbd_flags &= (~CTRL);
545 }
546 else
547 {
548 kbd_state = CN;
549 kbd_flags |= CTRL;
550 }
551}
552
553
554/******************************************************************************/
555
556static void kbd_caps (unsigned char scan_code)
557{
558 if ((kbd_flags & BRK) == NORMAL)
559 {
560 kbd_flags ^= CAPS;
Wolfgang Denk67777b92006-03-06 13:03:37 +0100561 kbd_led_set (); /* update keyboard LED */
wdenkc6097192002-11-03 00:24:07 +0000562 }
563}
564
565
566/******************************************************************************/
567
568static void kbd_num (unsigned char scan_code)
569{
570 if ((kbd_flags & BRK) == NORMAL)
571 {
572 kbd_flags ^= NUM;
573 kbd_state = (kbd_flags & NUM) ? AS : NM;
Wolfgang Denk67777b92006-03-06 13:03:37 +0100574 kbd_led_set (); /* update keyboard LED */
wdenkc6097192002-11-03 00:24:07 +0000575 }
576}
577
578
579/******************************************************************************/
580
581static void kbd_scroll (unsigned char scan_code)
582{
583 if ((kbd_flags & BRK) == NORMAL)
584 {
wdenk57b2d802003-06-27 21:31:46 +0000585 kbd_flags ^= STP;
Wolfgang Denk67777b92006-03-06 13:03:37 +0100586 kbd_led_set (); /* update keyboard LED */
wdenk57b2d802003-06-27 21:31:46 +0000587 if (kbd_flags & STP)
588 kbd_input = 0x13;
589 else
590 kbd_input = 0x11;
wdenkc6097192002-11-03 00:24:07 +0000591 }
592}
593
594/******************************************************************************/
595
596static void kbd_alt (unsigned char scan_code)
597{
598 if ((kbd_flags & BRK) == BRK)
599 {
wdenk57b2d802003-06-27 21:31:46 +0000600 kbd_state = AS;
601 kbd_flags &= (~ALT);
wdenkc6097192002-11-03 00:24:07 +0000602 }
603 else
604 {
wdenk57b2d802003-06-27 21:31:46 +0000605 kbd_state = AK;
606 kbd_flags &= ALT;
wdenkc6097192002-11-03 00:24:07 +0000607 }
608}
609
610
611/******************************************************************************/
612
613static void kbd_led_set (void)
614{
615 kbd_input_empty();
Wolfgang Denk67777b92006-03-06 13:03:37 +0100616 out8 (I8042_DATA_REG, 0xed); /* SET LED command */
wdenkc6097192002-11-03 00:24:07 +0000617 kbd_input_empty();
Wolfgang Denk67777b92006-03-06 13:03:37 +0100618 out8 (I8042_DATA_REG, (kbd_flags & 0x7)); /* LED bits only */
wdenkc6097192002-11-03 00:24:07 +0000619}
620
621
622/******************************************************************************/
623
624static int kbd_input_empty (void)
625{
626 int kbdTimeout = KBD_TIMEOUT;
627
628 /* wait for input buf empty */
629 while ((in8 (I8042_STATUS_REG) & 0x02) && kbdTimeout--)
wdenk57b2d802003-06-27 21:31:46 +0000630 udelay(1000);
wdenkc6097192002-11-03 00:24:07 +0000631
632 return kbdTimeout;
633}
634
635/******************************************************************************/
636
637static int kbd_reset (void)
638{
639 if (kbd_input_empty() == 0)
wdenk57b2d802003-06-27 21:31:46 +0000640 return -1;
wdenkc6097192002-11-03 00:24:07 +0000641
642 out8 (I8042_DATA_REG, 0xff);
643
644 udelay(250000);
645
646 if (kbd_input_empty() == 0)
wdenk57b2d802003-06-27 21:31:46 +0000647 return -1;
wdenkc6097192002-11-03 00:24:07 +0000648
Stefan Roeseb7a97c72006-01-18 20:05:34 +0100649#ifdef CONFIG_USE_CPCIDVI
650 out8 (I8042_COMMAND_REG, 0x60);
651#else
wdenkc6097192002-11-03 00:24:07 +0000652 out8 (I8042_DATA_REG, 0x60);
Stefan Roeseb7a97c72006-01-18 20:05:34 +0100653#endif
wdenkc6097192002-11-03 00:24:07 +0000654
655 if (kbd_input_empty() == 0)
wdenk57b2d802003-06-27 21:31:46 +0000656 return -1;
wdenkc6097192002-11-03 00:24:07 +0000657
658 out8 (I8042_DATA_REG, 0x45);
659
660
661 if (kbd_input_empty() == 0)
wdenk57b2d802003-06-27 21:31:46 +0000662 return -1;
wdenkc6097192002-11-03 00:24:07 +0000663
664 out8 (I8042_COMMAND_REG, 0xae);
665
666 if (kbd_input_empty() == 0)
wdenk57b2d802003-06-27 21:31:46 +0000667 return -1;
wdenkc6097192002-11-03 00:24:07 +0000668
669 return 0;
670}