]>
Commit | Line | Data |
---|---|---|
c609719b WD |
1 | /* |
2 | * (C) Copyright 2002 ELTEC Elektronik AG | |
3 | * Frank Gottschling <fgottschling@eltec.de> | |
4 | * | |
3765b3e7 | 5 | * SPDX-License-Identifier: GPL-2.0+ |
c609719b WD |
6 | */ |
7 | ||
8 | /* i8042.c - Intel 8042 keyboard driver routines */ | |
9 | ||
c609719b | 10 | #include <common.h> |
dcbf8257 SG |
11 | #include <dm.h> |
12 | #include <errno.h> | |
c609719b | 13 | #include <i8042.h> |
2ec739db | 14 | #include <input.h> |
dcbf8257 | 15 | #include <keyboard.h> |
2ec739db | 16 | #include <asm/io.h> |
c609719b | 17 | |
011d89d6 SG |
18 | DECLARE_GLOBAL_DATA_PTR; |
19 | ||
c609719b | 20 | /* defines */ |
835dd000 BM |
21 | #define in8(p) inb(p) |
22 | #define out8(p, v) outb(v, p) | |
c609719b | 23 | |
011d89d6 SG |
24 | enum { |
25 | QUIRK_DUP_POR = 1 << 0, | |
26 | }; | |
27 | ||
c609719b | 28 | /* locals */ |
dcbf8257 SG |
29 | struct i8042_kbd_priv { |
30 | bool extended; /* true if an extended keycode is expected next */ | |
011d89d6 | 31 | int quirks; /* quirks that we support */ |
dcbf8257 | 32 | }; |
dd4a5b22 GB |
33 | |
34 | static unsigned char ext_key_map[] = { | |
35 | 0x1c, /* keypad enter */ | |
36 | 0x1d, /* right control */ | |
37 | 0x35, /* keypad slash */ | |
38 | 0x37, /* print screen */ | |
39 | 0x38, /* right alt */ | |
40 | 0x46, /* break */ | |
41 | 0x47, /* editpad home */ | |
42 | 0x48, /* editpad up */ | |
43 | 0x49, /* editpad pgup */ | |
44 | 0x4b, /* editpad left */ | |
45 | 0x4d, /* editpad right */ | |
46 | 0x4f, /* editpad end */ | |
47 | 0x50, /* editpad dn */ | |
48 | 0x51, /* editpad pgdn */ | |
49 | 0x52, /* editpad ins */ | |
50 | 0x53, /* editpad del */ | |
51 | 0x00 /* map end */ | |
52 | }; | |
c609719b | 53 | |
3928d66a BM |
54 | static int kbd_input_empty(void) |
55 | { | |
835dd000 | 56 | int kbd_timeout = KBD_TIMEOUT * 1000; |
3928d66a | 57 | |
835dd000 | 58 | while ((in8(I8042_STS_REG) & STATUS_IBF) && kbd_timeout--) |
3928d66a BM |
59 | udelay(1); |
60 | ||
835dd000 | 61 | return kbd_timeout != -1; |
3928d66a BM |
62 | } |
63 | ||
835dd000 | 64 | static int kbd_output_full(void) |
3928d66a | 65 | { |
835dd000 | 66 | int kbd_timeout = KBD_TIMEOUT * 1000; |
3928d66a | 67 | |
835dd000 | 68 | while (((in8(I8042_STS_REG) & STATUS_OBF) == 0) && kbd_timeout--) |
3928d66a BM |
69 | udelay(1); |
70 | ||
835dd000 | 71 | return kbd_timeout != -1; |
3928d66a BM |
72 | } |
73 | ||
dcbf8257 SG |
74 | /** |
75 | * check_leds() - Check the keyboard LEDs and update them it needed | |
76 | * | |
77 | * @ret: Value to return | |
78 | * @return value of @ret | |
79 | */ | |
80 | static int i8042_kbd_update_leds(struct udevice *dev, int leds) | |
3928d66a BM |
81 | { |
82 | kbd_input_empty(); | |
835dd000 | 83 | out8(I8042_DATA_REG, CMD_SET_KBD_LED); |
3928d66a | 84 | kbd_input_empty(); |
dcbf8257 SG |
85 | out8(I8042_DATA_REG, leds & 0x7); |
86 | ||
87 | return 0; | |
3928d66a BM |
88 | } |
89 | ||
31d38ee6 | 90 | static int kbd_write(int reg, int value) |
3928d66a | 91 | { |
31d38ee6 | 92 | if (!kbd_input_empty()) |
7d96166b | 93 | return -1; |
31d38ee6 | 94 | out8(reg, value); |
3928d66a | 95 | |
31d38ee6 SG |
96 | return 0; |
97 | } | |
98 | ||
99 | static int kbd_read(int reg) | |
100 | { | |
101 | if (!kbd_output_full()) | |
7d96166b | 102 | return -1; |
31d38ee6 SG |
103 | |
104 | return in8(reg); | |
105 | } | |
106 | ||
107 | static int kbd_cmd_read(int cmd) | |
108 | { | |
109 | if (kbd_write(I8042_CMD_REG, cmd)) | |
3928d66a | 110 | return -1; |
31d38ee6 SG |
111 | |
112 | return kbd_read(I8042_DATA_REG); | |
113 | } | |
114 | ||
115 | static int kbd_cmd_write(int cmd, int data) | |
116 | { | |
117 | if (kbd_write(I8042_CMD_REG, cmd)) | |
3928d66a | 118 | return -1; |
31d38ee6 SG |
119 | |
120 | return kbd_write(I8042_DATA_REG, data); | |
121 | } | |
122 | ||
011d89d6 | 123 | static int kbd_reset(int quirk) |
31d38ee6 SG |
124 | { |
125 | int config; | |
126 | ||
127 | /* controller self test */ | |
128 | if (kbd_cmd_read(CMD_SELF_TEST) != KBC_TEST_OK) | |
4f087bac | 129 | goto err; |
31d38ee6 SG |
130 | |
131 | /* keyboard reset */ | |
132 | if (kbd_write(I8042_DATA_REG, CMD_RESET_KBD) || | |
133 | kbd_read(I8042_DATA_REG) != KBD_ACK || | |
134 | kbd_read(I8042_DATA_REG) != KBD_POR) | |
4f087bac | 135 | goto err; |
3928d66a | 136 | |
7d96166b | 137 | /* set AT translation and disable irq */ |
31d38ee6 SG |
138 | config = kbd_cmd_read(CMD_RD_CONFIG); |
139 | if (config == -1) | |
4f087bac | 140 | goto err; |
31d38ee6 | 141 | |
011d89d6 SG |
142 | /* Sometimes get a second byte */ |
143 | else if ((quirk & QUIRK_DUP_POR) && config == KBD_POR) | |
144 | config = kbd_cmd_read(CMD_RD_CONFIG); | |
145 | ||
7d96166b BM |
146 | config |= CONFIG_AT_TRANS; |
147 | config &= ~(CONFIG_KIRQ_EN | CONFIG_MIRQ_EN); | |
31d38ee6 | 148 | if (kbd_cmd_write(CMD_WR_CONFIG, config)) |
4f087bac | 149 | goto err; |
3928d66a | 150 | |
7d96166b | 151 | /* enable keyboard */ |
31d38ee6 SG |
152 | if (kbd_write(I8042_CMD_REG, CMD_KBD_EN) || |
153 | !kbd_input_empty()) | |
4f087bac | 154 | goto err; |
3928d66a BM |
155 | |
156 | return 0; | |
4f087bac SG |
157 | err: |
158 | debug("%s: Keyboard failure\n", __func__); | |
159 | return -1; | |
3928d66a BM |
160 | } |
161 | ||
22e0f5a9 GB |
162 | static int kbd_controller_present(void) |
163 | { | |
835dd000 | 164 | return in8(I8042_STS_REG) != 0xff; |
22e0f5a9 GB |
165 | } |
166 | ||
48edb304 GB |
167 | /* |
168 | * Implement a weak default function for boards that optionally | |
169 | * need to skip the i8042 initialization. | |
dcbf8257 SG |
170 | * |
171 | * TODO(sjg@chromium.org): Use device tree for this? | |
48edb304 GB |
172 | */ |
173 | int __weak board_i8042_skip(void) | |
174 | { | |
175 | /* As default, don't skip */ | |
176 | return 0; | |
177 | } | |
178 | ||
45fe668f LYCL |
179 | void i8042_flush(void) |
180 | { | |
181 | int timeout; | |
182 | ||
183 | /* | |
835dd000 BM |
184 | * The delay is to give the keyboard controller some time |
185 | * to fill the next byte. | |
45fe668f LYCL |
186 | */ |
187 | while (1) { | |
835dd000 BM |
188 | timeout = 100; /* wait for no longer than 100us */ |
189 | while (timeout > 0 && !(in8(I8042_STS_REG) & STATUS_OBF)) { | |
45fe668f LYCL |
190 | udelay(1); |
191 | timeout--; | |
192 | } | |
193 | ||
835dd000 BM |
194 | /* Try to pull next byte if not timeout */ |
195 | if (in8(I8042_STS_REG) & STATUS_OBF) | |
45fe668f LYCL |
196 | in8(I8042_DATA_REG); |
197 | else | |
198 | break; | |
199 | } | |
200 | } | |
201 | ||
202 | int i8042_disable(void) | |
203 | { | |
204 | if (kbd_input_empty() == 0) | |
205 | return -1; | |
206 | ||
207 | /* Disable keyboard */ | |
835dd000 | 208 | out8(I8042_CMD_REG, CMD_KBD_DIS); |
45fe668f LYCL |
209 | |
210 | if (kbd_input_empty() == 0) | |
211 | return -1; | |
212 | ||
213 | return 0; | |
214 | } | |
215 | ||
2ec739db SG |
216 | static int i8042_kbd_check(struct input_config *input) |
217 | { | |
dcbf8257 SG |
218 | struct i8042_kbd_priv *priv = dev_get_priv(input->dev); |
219 | ||
2ec739db SG |
220 | if ((in8(I8042_STS_REG) & STATUS_OBF) == 0) { |
221 | return 0; | |
222 | } else { | |
223 | bool release = false; | |
224 | int scan_code; | |
225 | int i; | |
226 | ||
227 | scan_code = in8(I8042_DATA_REG); | |
228 | if (scan_code == 0xfa) { | |
229 | return 0; | |
230 | } else if (scan_code == 0xe0) { | |
dcbf8257 | 231 | priv->extended = true; |
2ec739db SG |
232 | return 0; |
233 | } | |
234 | if (scan_code & 0x80) { | |
235 | scan_code &= 0x7f; | |
236 | release = true; | |
237 | } | |
dcbf8257 SG |
238 | if (priv->extended) { |
239 | priv->extended = false; | |
2ec739db SG |
240 | for (i = 0; ext_key_map[i]; i++) { |
241 | if (ext_key_map[i] == scan_code) { | |
242 | scan_code = 0x60 + i; | |
243 | break; | |
244 | } | |
245 | } | |
246 | /* not found ? */ | |
247 | if (!ext_key_map[i]) | |
248 | return 0; | |
249 | } | |
250 | ||
dcbf8257 | 251 | input_add_keycode(input, scan_code, release); |
2ec739db SG |
252 | return 1; |
253 | } | |
254 | } | |
255 | ||
835dd000 | 256 | /* i8042_kbd_init - reset keyboard and init state flags */ |
dcbf8257 | 257 | static int i8042_start(struct udevice *dev) |
c609719b | 258 | { |
dcbf8257 | 259 | struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev); |
011d89d6 | 260 | struct i8042_kbd_priv *priv = dev_get_priv(dev); |
dcbf8257 | 261 | struct input_config *input = &uc_priv->input; |
dd4a5b22 GB |
262 | int keymap, try; |
263 | char *penv; | |
2ec739db | 264 | int ret; |
c609719b | 265 | |
835dd000 BM |
266 | if (!kbd_controller_present() || board_i8042_skip()) { |
267 | debug("i8042 keyboard controller is not present\n"); | |
dcbf8257 | 268 | return -ENOENT; |
835dd000 | 269 | } |
22e0f5a9 | 270 | |
dd4a5b22 GB |
271 | /* Init keyboard device (default US layout) */ |
272 | keymap = KBD_US; | |
273 | penv = getenv("keymap"); | |
274 | if (penv != NULL) { | |
275 | if (strncmp(penv, "de", 3) == 0) | |
276 | keymap = KBD_GER; | |
277 | } | |
278 | ||
011d89d6 | 279 | for (try = 0; kbd_reset(priv->quirks) != 0; try++) { |
c5d257f9 SG |
280 | if (try >= KBD_RESET_TRIES) |
281 | return -1; | |
dd4a5b22 | 282 | } |
835dd000 | 283 | |
dcbf8257 | 284 | ret = input_add_tables(input, keymap == KBD_GER); |
2ec739db SG |
285 | if (ret) |
286 | return ret; | |
2ec739db | 287 | |
dcbf8257 SG |
288 | i8042_kbd_update_leds(dev, NORMAL); |
289 | debug("%s: started\n", __func__); | |
c5d257f9 SG |
290 | |
291 | return 0; | |
c609719b WD |
292 | } |
293 | ||
2ec739db | 294 | /** |
dcbf8257 | 295 | * Set up the i8042 keyboard. This is called by the stdio device handler |
835dd000 | 296 | * |
dcbf8257 SG |
297 | * We want to do this init when the keyboard is actually used rather than |
298 | * at start-up, since keyboard input may not currently be selected. | |
299 | * | |
300 | * Once the keyboard starts there will be a period during which we must | |
301 | * wait for the keyboard to init. We do this only when a key is first | |
302 | * read - see kbd_wait_for_fifo_init(). | |
303 | * | |
304 | * @return 0 if ok, -ve on error | |
c609719b | 305 | */ |
dcbf8257 | 306 | static int i8042_kbd_probe(struct udevice *dev) |
c609719b | 307 | { |
dcbf8257 | 308 | struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev); |
011d89d6 | 309 | struct i8042_kbd_priv *priv = dev_get_priv(dev); |
dcbf8257 SG |
310 | struct stdio_dev *sdev = &uc_priv->sdev; |
311 | struct input_config *input = &uc_priv->input; | |
312 | int ret; | |
dd4a5b22 | 313 | |
011d89d6 SG |
314 | if (fdtdec_get_bool(gd->fdt_blob, dev->of_offset, |
315 | "intel,duplicate-por")) | |
316 | priv->quirks |= QUIRK_DUP_POR; | |
317 | ||
dcbf8257 SG |
318 | /* Register the device. i8042_start() will be called soon */ |
319 | input->dev = dev; | |
320 | input->read_keys = i8042_kbd_check; | |
321 | input_allow_repeats(input, true); | |
322 | strcpy(sdev->name, "i8042-kbd"); | |
323 | ret = input_stdio_register(sdev); | |
324 | if (ret) { | |
325 | debug("%s: input_stdio_register() failed\n", __func__); | |
326 | return ret; | |
327 | } | |
328 | debug("%s: ready\n", __func__); | |
835dd000 | 329 | |
dcbf8257 | 330 | return 0; |
c609719b WD |
331 | } |
332 | ||
dcbf8257 SG |
333 | static const struct keyboard_ops i8042_kbd_ops = { |
334 | .start = i8042_start, | |
335 | .update_leds = i8042_kbd_update_leds, | |
336 | }; | |
337 | ||
338 | static const struct udevice_id i8042_kbd_ids[] = { | |
339 | { .compatible = "intel,i8042-keyboard" }, | |
340 | { } | |
341 | }; | |
342 | ||
343 | U_BOOT_DRIVER(i8042_kbd) = { | |
344 | .name = "i8042_kbd", | |
345 | .id = UCLASS_KEYBOARD, | |
346 | .of_match = i8042_kbd_ids, | |
347 | .probe = i8042_kbd_probe, | |
348 | .ops = &i8042_kbd_ops, | |
349 | .priv_auto_alloc_size = sizeof(struct i8042_kbd_priv), | |
350 | }; |