blob: b1ada867e400a281d8a07ff706ad293455f1726e [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 Denk3765b3e2013-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>
Bin Meng835dd002015-08-24 01:00:05 -070013#include <asm/io.h>
wdenkc6097192002-11-03 00:24:07 +000014#include <i8042.h>
15
16/* defines */
Bin Meng835dd002015-08-24 01:00:05 -070017#define in8(p) inb(p)
18#define out8(p, v) outb(v, p)
wdenkc6097192002-11-03 00:24:07 +000019
wdenkc6097192002-11-03 00:24:07 +000020/* locals */
21
Bin Meng835dd002015-08-24 01:00:05 -070022static int kbd_input = -1; /* no input yet */
23static int kbd_mapping = KBD_US; /* default US keyboard */
24static int kbd_flags = NORMAL; /* after reset */
25static int kbd_state; /* unshift code */
wdenkc6097192002-11-03 00:24:07 +000026
Gabe Blackdd4a5b22011-11-14 19:24:14 +000027static unsigned char kbd_fct_map[144] = {
28 /* kbd_fct_map table for scan code */
Bin Meng835dd002015-08-24 01:00:05 -070029 0, AS, AS, AS, AS, AS, AS, AS, /* scan 00-07 */
30 AS, AS, AS, AS, AS, AS, AS, AS, /* scan 08-0F */
Gabe Blackdd4a5b22011-11-14 19:24:14 +000031 AS, AS, AS, AS, AS, AS, AS, AS, /* scan 10-17 */
32 AS, AS, AS, AS, AS, CN, AS, AS, /* scan 18-1F */
33 AS, AS, AS, AS, AS, AS, AS, AS, /* scan 20-27 */
34 AS, AS, SH, AS, AS, AS, AS, AS, /* scan 28-2F */
35 AS, AS, AS, AS, AS, AS, SH, AS, /* scan 30-37 */
36 AS, AS, CP, 0, 0, 0, 0, 0, /* scan 38-3F */
37 0, 0, 0, 0, 0, NM, ST, ES, /* scan 40-47 */
38 ES, ES, ES, ES, ES, ES, ES, ES, /* scan 48-4F */
39 ES, ES, ES, ES, 0, 0, AS, 0, /* scan 50-57 */
40 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
41 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
42 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
43 AS, 0, 0, AS, 0, 0, AS, 0, /* scan 70-77 */
44 0, AS, 0, 0, 0, AS, 0, 0, /* scan 78-7F */
45 AS, CN, AS, AS, AK, ST, EX, EX, /* enhanced */
46 AS, EX, EX, AS, EX, AS, EX, EX /* enhanced */
47 };
wdenkc6097192002-11-03 00:24:07 +000048
Gabe Blackdd4a5b22011-11-14 19:24:14 +000049static unsigned char kbd_key_map[2][5][144] = {
50 { /* US keyboard */
51 { /* unshift code */
Bin Meng835dd002015-08-24 01:00:05 -070052 0, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 00-07 */
53 '7', '8', '9', '0', '-', '=', 0x08, '\t', /* scan 08-0F */
Gabe Blackdd4a5b22011-11-14 19:24:14 +000054 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', /* scan 10-17 */
55 'o', 'p', '[', ']', '\r', CN, 'a', 's', /* scan 18-1F */
56 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* scan 20-27 */
57 '\'', '`', SH, '\\', 'z', 'x', 'c', 'v', /* scan 28-2F */
58 'b', 'n', 'm', ',', '.', '/', SH, '*', /* scan 30-37 */
59 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
60 0, 0, 0, 0, 0, NM, ST, '7', /* scan 40-47 */
61 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
62 '2', '3', '0', '.', 0, 0, 0, 0, /* scan 50-57 */
63 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
64 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
65 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
66 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
67 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
68 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
69 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
70 },
71 { /* shift code */
Bin Meng835dd002015-08-24 01:00:05 -070072 0, 0x1b, '!', '@', '#', '$', '%', '^', /* scan 00-07 */
73 '&', '*', '(', ')', '_', '+', 0x08, '\t', /* scan 08-0F */
Gabe Blackdd4a5b22011-11-14 19:24:14 +000074 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', /* scan 10-17 */
75 'O', 'P', '{', '}', '\r', CN, 'A', 'S', /* scan 18-1F */
76 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', /* scan 20-27 */
77 '"', '~', SH, '|', 'Z', 'X', 'C', 'V', /* scan 28-2F */
78 'B', 'N', 'M', '<', '>', '?', SH, '*', /* scan 30-37 */
79 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
80 0, 0, 0, 0, 0, NM, ST, '7', /* scan 40-47 */
81 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
82 '2', '3', '0', '.', 0, 0, 0, 0, /* scan 50-57 */
83 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
84 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
85 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
86 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
87 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
88 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
89 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
90 },
91 { /* control code */
Bin Meng835dd002015-08-24 01:00:05 -070092 0xff, 0x1b, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, /* scan 00-07 */
93 0x1e, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, '\t', /* scan 08-0F */
Gabe Blackdd4a5b22011-11-14 19:24:14 +000094 0x11, 0x17, 0x05, 0x12, 0x14, 0x19, 0x15, 0x09, /* scan 10-17 */
95 0x0f, 0x10, 0x1b, 0x1d, '\r', CN, 0x01, 0x13, /* scan 18-1F */
96 0x04, 0x06, 0x07, 0x08, 0x0a, 0x0b, 0x0c, 0xff, /* scan 20-27 */
97 0xff, 0x1c, SH, 0xff, 0x1a, 0x18, 0x03, 0x16, /* scan 28-2F */
98 0x02, 0x0e, 0x0d, 0xff, 0xff, 0xff, SH, 0xff, /* scan 30-37 */
99 0xff, 0xff, CP, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
100 0xff, 0xff, 0xff, 0xff, 0xff, NM, ST, 0xff, /* scan 40-47 */
101 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48-4F */
102 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 50-57 */
103 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58-5F */
104 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60-67 */
105 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68-6F */
106 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70-77 */
107 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78-7F */
108 '\r', CN, '/', '*', ' ', ST, 0xff, 0xff, /* extended */
109 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* extended */
110 },
111 { /* non numeric code */
Bin Meng835dd002015-08-24 01:00:05 -0700112 0, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 00-07 */
113 '7', '8', '9', '0', '-', '=', 0x08, '\t', /* scan 08-0F */
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000114 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', /* scan 10-17 */
115 'o', 'p', '[', ']', '\r', CN, 'a', 's', /* scan 18-1F */
116 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* scan 20-27 */
117 '\'', '`', SH, '\\', 'z', 'x', 'c', 'v', /* scan 28-2F */
118 'b', 'n', 'm', ',', '.', '/', SH, '*', /* scan 30-37 */
119 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
120 0, 0, 0, 0, 0, NM, ST, 'w', /* scan 40-47 */
121 'x', 'y', 'l', 't', 'u', 'v', 'm', 'q', /* scan 48-4F */
122 'r', 's', 'p', 'n', 0, 0, 0, 0, /* scan 50-57 */
123 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
124 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
125 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
126 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
127 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
128 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
129 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
130 },
131 { /* right alt mode - not used in US keyboard */
Bin Meng835dd002015-08-24 01:00:05 -0700132 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 00-07 */
133 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 08-0F */
134 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 10-17 */
135 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 18-1F */
136 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 20-27 */
137 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 28-2F */
138 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 30-37 */
139 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
140 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 40-47 */
141 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48-4F */
142 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 50-57 */
143 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58-5F */
144 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60-67 */
145 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68-6F */
146 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70-77 */
147 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78-7F */
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000148 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* extended */
149 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* extended */
150 }
151 },
Bin Meng835dd002015-08-24 01:00:05 -0700152 { /* German keyboard */
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000153 { /* unshift code */
Bin Meng835dd002015-08-24 01:00:05 -0700154 0, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 00-07 */
155 '7', '8', '9', '0', 0xe1, '\'', 0x08, '\t', /* scan 08-0F */
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000156 'q', 'w', 'e', 'r', 't', 'z', 'u', 'i', /* scan 10-17 */
157 'o', 'p', 0x81, '+', '\r', CN, 'a', 's', /* scan 18-1F */
158 'd', 'f', 'g', 'h', 'j', 'k', 'l', 0x94, /* scan 20-27 */
159 0x84, '^', SH, '#', 'y', 'x', 'c', 'v', /* scan 28-2F */
160 'b', 'n', 'm', ',', '.', '-', SH, '*', /* scan 30-37 */
161 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
162 0, 0, 0, 0, 0, NM, ST, '7', /* scan 40-47 */
163 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
164 '2', '3', '0', ',', 0, 0, '<', 0, /* scan 50-57 */
165 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
166 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
167 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
168 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
169 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
170 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
171 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
172 },
173 { /* shift code */
Bin Meng835dd002015-08-24 01:00:05 -0700174 0, 0x1b, '!', '"', 0x15, '$', '%', '&', /* scan 00-07 */
175 '/', '(', ')', '=', '?', '`', 0x08, '\t', /* scan 08-0F */
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000176 'Q', 'W', 'E', 'R', 'T', 'Z', 'U', 'I', /* scan 10-17 */
177 'O', 'P', 0x9a, '*', '\r', CN, 'A', 'S', /* scan 18-1F */
178 'D', 'F', 'G', 'H', 'J', 'K', 'L', 0x99, /* scan 20-27 */
179 0x8e, 0xf8, SH, '\'', 'Y', 'X', 'C', 'V', /* scan 28-2F */
180 'B', 'N', 'M', ';', ':', '_', SH, '*', /* scan 30-37 */
181 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
182 0, 0, 0, 0, 0, NM, ST, '7', /* scan 40-47 */
183 '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
184 '2', '3', '0', ',', 0, 0, '>', 0, /* scan 50-57 */
185 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
186 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
187 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
188 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
189 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
190 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
191 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
192 },
193 { /* control code */
Bin Meng835dd002015-08-24 01:00:05 -0700194 0xff, 0x1b, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, /* scan 00-07 */
195 0x1e, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, '\t', /* scan 08-0F */
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000196 0x11, 0x17, 0x05, 0x12, 0x14, 0x19, 0x15, 0x09, /* scan 10-17 */
197 0x0f, 0x10, 0x1b, 0x1d, '\r', CN, 0x01, 0x13, /* scan 18-1F */
198 0x04, 0x06, 0x07, 0x08, 0x0a, 0x0b, 0x0c, 0xff, /* scan 20-27 */
199 0xff, 0x1c, SH, 0xff, 0x1a, 0x18, 0x03, 0x16, /* scan 28-2F */
200 0x02, 0x0e, 0x0d, 0xff, 0xff, 0xff, SH, 0xff, /* scan 30-37 */
201 0xff, 0xff, CP, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
202 0xff, 0xff, 0xff, 0xff, 0xff, NM, ST, 0xff, /* scan 40-47 */
203 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48-4F */
204 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 50-57 */
205 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58-5F */
206 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60-67 */
207 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68-6F */
208 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70-77 */
209 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78-7F */
210 '\r', CN, '/', '*', ' ', ST, 0xff, 0xff, /* extended */
211 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* extended */
212 },
213 { /* non numeric code */
Bin Meng835dd002015-08-24 01:00:05 -0700214 0, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 00-07 */
215 '7', '8', '9', '0', 0xe1, '\'', 0x08, '\t', /* scan 08-0F */
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000216 'q', 'w', 'e', 'r', 't', 'z', 'u', 'i', /* scan 10-17 */
217 'o', 'p', 0x81, '+', '\r', CN, 'a', 's', /* scan 18-1F */
218 'd', 'f', 'g', 'h', 'j', 'k', 'l', 0x94, /* scan 20-27 */
219 0x84, '^', SH, 0, 'y', 'x', 'c', 'v', /* scan 28-2F */
220 'b', 'n', 'm', ',', '.', '-', SH, '*', /* scan 30-37 */
221 ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
222 0, 0, 0, 0, 0, NM, ST, 'w', /* scan 40-47 */
223 'x', 'y', 'l', 't', 'u', 'v', 'm', 'q', /* scan 48-4F */
224 'r', 's', 'p', 'n', 0, 0, '<', 0, /* scan 50-57 */
225 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
226 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
227 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
228 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
229 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
230 '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
231 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
232 },
Bin Meng835dd002015-08-24 01:00:05 -0700233 { /* right alt mode - is used in German keyboard */
234 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 00-07 */
235 '{', '[', ']', '}', '\\', 0xff, 0xff, 0xff, /* scan 08-0F */
236 '@', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 10-17 */
237 0xff, 0xff, 0xff, '~', 0xff, 0xff, 0xff, 0xff, /* scan 18-1F */
238 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 20-27 */
239 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 28-2F */
240 0xff, 0xff, 0xe6, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 30-37 */
241 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
242 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 40-47 */
243 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48-4F */
244 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '|', 0xff, /* scan 50-57 */
245 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58-5F */
246 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60-67 */
247 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68-6F */
248 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70-77 */
249 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78-7F */
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000250 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* extended */
251 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* extended */
252 }
253 }
254 };
wdenkc6097192002-11-03 00:24:07 +0000255
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000256static unsigned char ext_key_map[] = {
257 0x1c, /* keypad enter */
258 0x1d, /* right control */
259 0x35, /* keypad slash */
260 0x37, /* print screen */
261 0x38, /* right alt */
262 0x46, /* break */
263 0x47, /* editpad home */
264 0x48, /* editpad up */
265 0x49, /* editpad pgup */
266 0x4b, /* editpad left */
267 0x4d, /* editpad right */
268 0x4f, /* editpad end */
269 0x50, /* editpad dn */
270 0x51, /* editpad pgdn */
271 0x52, /* editpad ins */
272 0x53, /* editpad del */
273 0x00 /* map end */
274 };
wdenkc6097192002-11-03 00:24:07 +0000275
Bin Meng3928d662015-08-24 01:00:04 -0700276static int kbd_input_empty(void)
277{
Bin Meng835dd002015-08-24 01:00:05 -0700278 int kbd_timeout = KBD_TIMEOUT * 1000;
Bin Meng3928d662015-08-24 01:00:04 -0700279
Bin Meng835dd002015-08-24 01:00:05 -0700280 while ((in8(I8042_STS_REG) & STATUS_IBF) && kbd_timeout--)
Bin Meng3928d662015-08-24 01:00:04 -0700281 udelay(1);
282
Bin Meng835dd002015-08-24 01:00:05 -0700283 return kbd_timeout != -1;
Bin Meng3928d662015-08-24 01:00:04 -0700284}
285
Bin Meng835dd002015-08-24 01:00:05 -0700286static int kbd_output_full(void)
Bin Meng3928d662015-08-24 01:00:04 -0700287{
Bin Meng835dd002015-08-24 01:00:05 -0700288 int kbd_timeout = KBD_TIMEOUT * 1000;
Bin Meng3928d662015-08-24 01:00:04 -0700289
Bin Meng835dd002015-08-24 01:00:05 -0700290 while (((in8(I8042_STS_REG) & STATUS_OBF) == 0) && kbd_timeout--)
Bin Meng3928d662015-08-24 01:00:04 -0700291 udelay(1);
292
Bin Meng835dd002015-08-24 01:00:05 -0700293 return kbd_timeout != -1;
Bin Meng3928d662015-08-24 01:00:04 -0700294}
295
296static void kbd_led_set(void)
297{
298 kbd_input_empty();
Bin Meng835dd002015-08-24 01:00:05 -0700299 out8(I8042_DATA_REG, CMD_SET_KBD_LED);
Bin Meng3928d662015-08-24 01:00:04 -0700300 kbd_input_empty();
Bin Meng835dd002015-08-24 01:00:05 -0700301 out8(I8042_DATA_REG, (kbd_flags & 0x7));
Bin Meng3928d662015-08-24 01:00:04 -0700302}
303
304static void kbd_normal(unsigned char scan_code)
305{
306 unsigned char chr;
307
308 if ((kbd_flags & BRK) == NORMAL) {
309 chr = kbd_key_map[kbd_mapping][kbd_state][scan_code];
310 if ((chr == 0xff) || (chr == 0x00))
311 return;
312
313 /* if caps lock convert upper to lower */
314 if (((kbd_flags & CAPS) == CAPS) &&
Bin Meng835dd002015-08-24 01:00:05 -0700315 (chr >= 'a' && chr <= 'z')) {
Bin Meng3928d662015-08-24 01:00:04 -0700316 chr -= 'a' - 'A';
317 }
318 kbd_input = chr;
319 }
320}
321
322static void kbd_shift(unsigned char scan_code)
323{
324 if ((kbd_flags & BRK) == BRK) {
325 kbd_state = AS;
326 kbd_flags &= (~SHIFT);
327 } else {
328 kbd_state = SH;
329 kbd_flags |= SHIFT;
330 }
331}
332
333static void kbd_ctrl(unsigned char scan_code)
334{
335 if ((kbd_flags & BRK) == BRK) {
336 kbd_state = AS;
337 kbd_flags &= (~CTRL);
338 } else {
339 kbd_state = CN;
340 kbd_flags |= CTRL;
341 }
342}
343
344static void kbd_num(unsigned char scan_code)
345{
346 if ((kbd_flags & BRK) == NORMAL) {
347 kbd_flags ^= NUM;
348 kbd_state = (kbd_flags & NUM) ? AS : NM;
Bin Meng835dd002015-08-24 01:00:05 -0700349 kbd_led_set();
Bin Meng3928d662015-08-24 01:00:04 -0700350 }
351}
352
353static void kbd_alt(unsigned char scan_code)
354{
355 if ((kbd_flags & BRK) == BRK) {
356 kbd_state = AS;
357 kbd_flags &= (~ALT);
358 } else {
359 kbd_state = AK;
360 kbd_flags &= ALT;
361 }
362}
363
364static void kbd_caps(unsigned char scan_code)
365{
366 if ((kbd_flags & BRK) == NORMAL) {
367 kbd_flags ^= CAPS;
Bin Meng835dd002015-08-24 01:00:05 -0700368 kbd_led_set();
Bin Meng3928d662015-08-24 01:00:04 -0700369 }
370}
371
372static void kbd_scroll(unsigned char scan_code)
373{
374 if ((kbd_flags & BRK) == NORMAL) {
375 kbd_flags ^= STP;
Bin Meng835dd002015-08-24 01:00:05 -0700376 kbd_led_set();
Bin Meng3928d662015-08-24 01:00:04 -0700377 if (kbd_flags & STP)
378 kbd_input = 0x13;
379 else
380 kbd_input = 0x11;
381 }
382}
383
384static void kbd_conv_char(unsigned char scan_code)
385{
386 if (scan_code == 0xe0) {
387 kbd_flags |= EXT;
388 return;
389 }
390
391 /* if high bit of scan_code, set break flag */
392 if (scan_code & 0x80)
393 kbd_flags |= BRK;
394 else
395 kbd_flags &= ~BRK;
396
397 if ((scan_code == 0xe1) || (kbd_flags & E1)) {
398 if (scan_code == 0xe1) {
Bin Meng835dd002015-08-24 01:00:05 -0700399 kbd_flags ^= BRK; /* reset the break flag */
400 kbd_flags ^= E1; /* bitwise EXOR with E1 flag */
Bin Meng3928d662015-08-24 01:00:04 -0700401 }
402 return;
403 }
404
405 scan_code &= 0x7f;
406
407 if (kbd_flags & EXT) {
408 int i;
409
410 kbd_flags ^= EXT;
411 for (i = 0; ext_key_map[i]; i++) {
412 if (ext_key_map[i] == scan_code) {
413 scan_code = 0x80 + i;
414 break;
415 }
416 }
417 /* not found ? */
418 if (!ext_key_map[i])
419 return;
420 }
421
422 switch (kbd_fct_map[scan_code]) {
423 case AS:
424 kbd_normal(scan_code);
425 break;
426 case SH:
427 kbd_shift(scan_code);
428 break;
429 case CN:
430 kbd_ctrl(scan_code);
431 break;
432 case NM:
433 kbd_num(scan_code);
434 break;
435 case AK:
436 kbd_alt(scan_code);
437 break;
438 case CP:
439 kbd_caps(scan_code);
440 break;
441 case ST:
442 kbd_scroll(scan_code);
443 break;
444 }
Bin Meng835dd002015-08-24 01:00:05 -0700445
Bin Meng3928d662015-08-24 01:00:04 -0700446 return;
447}
448
Simon Glass31d38ee2015-10-18 21:17:19 -0600449static int kbd_write(int reg, int value)
450{
451 if (!kbd_input_empty())
452 return -1;
453 out8(reg, value);
454
455 return 0;
456}
457
458static int kbd_read(int reg)
459{
460 if (!kbd_output_full())
461 return -1;
462
463 return in8(reg);
464}
465
466static int kbd_cmd_read(int cmd)
467{
468 if (kbd_write(I8042_CMD_REG, cmd))
469 return -1;
470
471 return kbd_read(I8042_DATA_REG);
472}
473
474static int kbd_cmd_write(int cmd, int data)
475{
476 if (kbd_write(I8042_CMD_REG, cmd))
477 return -1;
478
479 return kbd_write(I8042_DATA_REG, data);
480}
481
Bin Meng3928d662015-08-24 01:00:04 -0700482static int kbd_reset(void)
483{
Simon Glass31d38ee2015-10-18 21:17:19 -0600484 int config;
Bin Meng7d961662015-08-24 01:00:06 -0700485
486 /* controller self test */
Simon Glass31d38ee2015-10-18 21:17:19 -0600487 if (kbd_cmd_read(CMD_SELF_TEST) != KBC_TEST_OK)
Simon Glass4f087ba2015-10-18 21:17:20 -0600488 goto err;
Bin Meng3928d662015-08-24 01:00:04 -0700489
Bin Meng7d961662015-08-24 01:00:06 -0700490 /* keyboard reset */
Simon Glass31d38ee2015-10-18 21:17:19 -0600491 if (kbd_write(I8042_DATA_REG, CMD_RESET_KBD) ||
492 kbd_read(I8042_DATA_REG) != KBD_ACK ||
493 kbd_read(I8042_DATA_REG) != KBD_POR)
Simon Glass4f087ba2015-10-18 21:17:20 -0600494 goto err;
Bin Meng3928d662015-08-24 01:00:04 -0700495
Bin Meng7d961662015-08-24 01:00:06 -0700496 /* set AT translation and disable irq */
Simon Glass31d38ee2015-10-18 21:17:19 -0600497 config = kbd_cmd_read(CMD_RD_CONFIG);
498 if (config == -1)
Simon Glass4f087ba2015-10-18 21:17:20 -0600499 goto err;
Simon Glass31d38ee2015-10-18 21:17:19 -0600500
Bin Meng7d961662015-08-24 01:00:06 -0700501 config |= CONFIG_AT_TRANS;
502 config &= ~(CONFIG_KIRQ_EN | CONFIG_MIRQ_EN);
Simon Glass31d38ee2015-10-18 21:17:19 -0600503 if (kbd_cmd_write(CMD_WR_CONFIG, config))
Simon Glass4f087ba2015-10-18 21:17:20 -0600504 goto err;
Bin Meng3928d662015-08-24 01:00:04 -0700505
Bin Meng7d961662015-08-24 01:00:06 -0700506 /* enable keyboard */
Simon Glass31d38ee2015-10-18 21:17:19 -0600507 if (kbd_write(I8042_CMD_REG, CMD_KBD_EN) ||
508 !kbd_input_empty())
Simon Glass4f087ba2015-10-18 21:17:20 -0600509 goto err;
Bin Meng3928d662015-08-24 01:00:04 -0700510
Bin Meng3928d662015-08-24 01:00:04 -0700511 return 0;
Simon Glass4f087ba2015-10-18 21:17:20 -0600512err:
513 debug("%s: Keyboard failure\n", __func__);
514 return -1;
Bin Meng3928d662015-08-24 01:00:04 -0700515}
516
Gabe Black22e0f5a2011-11-14 20:18:12 +0000517static int kbd_controller_present(void)
518{
Bin Meng835dd002015-08-24 01:00:05 -0700519 return in8(I8042_STS_REG) != 0xff;
Gabe Black22e0f5a2011-11-14 20:18:12 +0000520}
521
Gabe Black48edb302012-10-12 14:02:02 +0000522/*
523 * Implement a weak default function for boards that optionally
524 * need to skip the i8042 initialization.
525 */
526int __weak board_i8042_skip(void)
527{
528 /* As default, don't skip */
529 return 0;
530}
531
Louis Yung-Chieh Lo45fe6682012-10-11 15:15:51 +0000532void i8042_flush(void)
533{
534 int timeout;
535
536 /*
Bin Meng835dd002015-08-24 01:00:05 -0700537 * The delay is to give the keyboard controller some time
538 * to fill the next byte.
Louis Yung-Chieh Lo45fe6682012-10-11 15:15:51 +0000539 */
540 while (1) {
Bin Meng835dd002015-08-24 01:00:05 -0700541 timeout = 100; /* wait for no longer than 100us */
542 while (timeout > 0 && !(in8(I8042_STS_REG) & STATUS_OBF)) {
Louis Yung-Chieh Lo45fe6682012-10-11 15:15:51 +0000543 udelay(1);
544 timeout--;
545 }
546
Bin Meng835dd002015-08-24 01:00:05 -0700547 /* Try to pull next byte if not timeout */
548 if (in8(I8042_STS_REG) & STATUS_OBF)
Louis Yung-Chieh Lo45fe6682012-10-11 15:15:51 +0000549 in8(I8042_DATA_REG);
550 else
551 break;
552 }
553}
554
555int i8042_disable(void)
556{
557 if (kbd_input_empty() == 0)
558 return -1;
559
560 /* Disable keyboard */
Bin Meng835dd002015-08-24 01:00:05 -0700561 out8(I8042_CMD_REG, CMD_KBD_DIS);
Louis Yung-Chieh Lo45fe6682012-10-11 15:15:51 +0000562
563 if (kbd_input_empty() == 0)
564 return -1;
565
566 return 0;
567}
568
Bin Meng835dd002015-08-24 01:00:05 -0700569/* i8042_kbd_init - reset keyboard and init state flags */
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000570int i8042_kbd_init(void)
wdenkc6097192002-11-03 00:24:07 +0000571{
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000572 int keymap, try;
573 char *penv;
wdenkc6097192002-11-03 00:24:07 +0000574
Bin Meng835dd002015-08-24 01:00:05 -0700575 if (!kbd_controller_present() || board_i8042_skip()) {
576 debug("i8042 keyboard controller is not present\n");
Gabe Black22e0f5a2011-11-14 20:18:12 +0000577 return -1;
Bin Meng835dd002015-08-24 01:00:05 -0700578 }
Gabe Black22e0f5a2011-11-14 20:18:12 +0000579
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000580 /* Init keyboard device (default US layout) */
581 keymap = KBD_US;
582 penv = getenv("keymap");
583 if (penv != NULL) {
584 if (strncmp(penv, "de", 3) == 0)
585 keymap = KBD_GER;
586 }
wdenkc6097192002-11-03 00:24:07 +0000587
Simon Glassc5d257f2015-10-18 21:17:21 -0600588 for (try = 0; kbd_reset() != 0; try++) {
589 if (try >= KBD_RESET_TRIES)
590 return -1;
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000591 }
Bin Meng835dd002015-08-24 01:00:05 -0700592
Simon Glassc5d257f2015-10-18 21:17:21 -0600593 kbd_mapping = keymap;
594 kbd_flags = NORMAL;
595 kbd_state = 0;
596 kbd_led_set();
597
598 return 0;
wdenkc6097192002-11-03 00:24:07 +0000599}
600
Bin Meng835dd002015-08-24 01:00:05 -0700601/*
wdenkc6097192002-11-03 00:24:07 +0000602 * i8042_tstc - test if keyboard input is available
Bin Meng835dd002015-08-24 01:00:05 -0700603 *
604 * option: cursor blinking if called in a loop
wdenkc6097192002-11-03 00:24:07 +0000605 */
Simon Glass709ea542014-07-23 06:54:59 -0600606int i8042_tstc(struct stdio_dev *dev)
wdenkc6097192002-11-03 00:24:07 +0000607{
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000608 unsigned char scan_code = 0;
wdenkc6097192002-11-03 00:24:07 +0000609
Bin Meng835dd002015-08-24 01:00:05 -0700610 if ((in8(I8042_STS_REG) & STATUS_OBF) == 0) {
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000611 return 0;
612 } else {
613 scan_code = in8(I8042_DATA_REG);
614 if (scan_code == 0xfa)
615 return 0;
616
617 kbd_conv_char(scan_code);
618
619 if (kbd_input != -1)
620 return 1;
621 }
Bin Meng835dd002015-08-24 01:00:05 -0700622
wdenk8bde7f72003-06-27 21:31:46 +0000623 return 0;
wdenkc6097192002-11-03 00:24:07 +0000624}
625
Bin Meng835dd002015-08-24 01:00:05 -0700626/*
wdenkc6097192002-11-03 00:24:07 +0000627 * i8042_getc - wait till keyboard input is available
Bin Meng835dd002015-08-24 01:00:05 -0700628 *
629 * option: turn on/off cursor while waiting
wdenkc6097192002-11-03 00:24:07 +0000630 */
Simon Glass709ea542014-07-23 06:54:59 -0600631int i8042_getc(struct stdio_dev *dev)
wdenkc6097192002-11-03 00:24:07 +0000632{
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000633 int ret_chr;
634 unsigned char scan_code;
wdenkc6097192002-11-03 00:24:07 +0000635
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000636 while (kbd_input == -1) {
Bin Meng835dd002015-08-24 01:00:05 -0700637 while ((in8(I8042_STS_REG) & STATUS_OBF) == 0) {
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000638 }
639 scan_code = in8(I8042_DATA_REG);
640 if (scan_code != 0xfa)
Bin Meng835dd002015-08-24 01:00:05 -0700641 kbd_conv_char(scan_code);
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000642 }
643 ret_chr = kbd_input;
644 kbd_input = -1;
Bin Meng835dd002015-08-24 01:00:05 -0700645
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000646 return ret_chr;
647}