1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2002 ELTEC Elektronik AG
7 /* i8042.c - Intel 8042 keyboard driver routines */
17 #include <asm/global_data.h>
19 #include <linux/delay.h>
21 DECLARE_GLOBAL_DATA_PTR;
25 #define out8(p, v) outb(v, p)
28 QUIRK_DUP_POR = 1 << 0,
32 struct i8042_kbd_priv {
33 bool extended; /* true if an extended keycode is expected next */
34 int quirks; /* quirks that we support */
37 static unsigned char ext_key_map[] = {
38 0x1c, /* keypad enter */
39 0x1d, /* right control */
40 0x35, /* keypad slash */
41 0x37, /* print screen */
44 0x47, /* editpad home */
45 0x48, /* editpad up */
46 0x49, /* editpad pgup */
47 0x4b, /* editpad left */
48 0x4d, /* editpad right */
49 0x4f, /* editpad end */
50 0x50, /* editpad dn */
51 0x51, /* editpad pgdn */
52 0x52, /* editpad ins */
53 0x53, /* editpad del */
57 static int kbd_input_empty(void)
59 int kbd_timeout = KBD_TIMEOUT * 1000;
61 while ((in8(I8042_STS_REG) & STATUS_IBF) && kbd_timeout--)
64 return kbd_timeout != -1;
67 static int kbd_output_full(void)
69 int kbd_timeout = KBD_TIMEOUT * 1000;
71 while (((in8(I8042_STS_REG) & STATUS_OBF) == 0) && kbd_timeout--)
74 return kbd_timeout != -1;
78 * check_leds() - Check the keyboard LEDs and update them it needed
80 * @ret: Value to return
81 * @return value of @ret
83 static int i8042_kbd_update_leds(struct udevice *dev, int leds)
86 out8(I8042_DATA_REG, CMD_SET_KBD_LED);
88 out8(I8042_DATA_REG, leds & 0x7);
93 static int kbd_write(int reg, int value)
95 if (!kbd_input_empty())
102 static int kbd_read(int reg)
104 if (!kbd_output_full())
110 static int kbd_cmd_read(int cmd)
112 if (kbd_write(I8042_CMD_REG, cmd))
115 return kbd_read(I8042_DATA_REG);
118 static int kbd_cmd_write(int cmd, int data)
120 if (kbd_write(I8042_CMD_REG, cmd))
123 return kbd_write(I8042_DATA_REG, data);
126 static int kbd_reset(int quirk)
130 /* controller self test */
131 if (kbd_cmd_read(CMD_SELF_TEST) != KBC_TEST_OK)
135 if (kbd_write(I8042_DATA_REG, CMD_RESET_KBD) ||
136 kbd_read(I8042_DATA_REG) != KBD_ACK ||
137 kbd_read(I8042_DATA_REG) != KBD_POR)
140 if (kbd_write(I8042_DATA_REG, CMD_DRAIN_OUTPUT) ||
141 kbd_read(I8042_DATA_REG) != KBD_ACK)
144 /* set AT translation and disable irq */
145 config = kbd_cmd_read(CMD_RD_CONFIG);
149 /* Sometimes get a second byte */
150 else if ((quirk & QUIRK_DUP_POR) && config == KBD_POR)
151 config = kbd_cmd_read(CMD_RD_CONFIG);
153 config |= CONFIG_AT_TRANS;
154 config &= ~(CONFIG_KIRQ_EN | CONFIG_MIRQ_EN);
155 if (kbd_cmd_write(CMD_WR_CONFIG, config))
158 /* enable keyboard */
159 if (kbd_write(I8042_CMD_REG, CMD_KBD_EN) ||
165 debug("%s: Keyboard failure\n", __func__);
169 static int kbd_controller_present(void)
171 return in8(I8042_STS_REG) != 0xff;
174 /** Flush all buffer from keyboard controller to host*/
175 static void i8042_flush(void)
180 * The delay is to give the keyboard controller some time
181 * to fill the next byte.
184 timeout = 100; /* wait for no longer than 100us */
185 while (timeout > 0 && !(in8(I8042_STS_REG) & STATUS_OBF)) {
190 /* Try to pull next byte if not timeout */
191 if (in8(I8042_STS_REG) & STATUS_OBF)
199 * Disables the keyboard so that key strokes no longer generate scancodes to
202 * @return 0 if ok, -1 if keyboard input was found while disabling
204 static int i8042_disable(void)
206 if (kbd_input_empty() == 0)
209 /* Disable keyboard */
210 out8(I8042_CMD_REG, CMD_KBD_DIS);
212 if (kbd_input_empty() == 0)
218 static int i8042_kbd_check(struct input_config *input)
220 struct i8042_kbd_priv *priv = dev_get_priv(input->dev);
222 if ((in8(I8042_STS_REG) & STATUS_OBF) == 0) {
225 bool release = false;
229 scan_code = in8(I8042_DATA_REG);
230 if (scan_code == 0xfa) {
232 } else if (scan_code == 0xe0) {
233 priv->extended = true;
236 if (scan_code & 0x80) {
240 if (priv->extended) {
241 priv->extended = false;
242 for (i = 0; ext_key_map[i]; i++) {
243 if (ext_key_map[i] == scan_code) {
244 scan_code = 0x60 + i;
253 input_add_keycode(input, scan_code, release);
258 /* i8042_kbd_init - reset keyboard and init state flags */
259 static int i8042_start(struct udevice *dev)
261 struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
262 struct i8042_kbd_priv *priv = dev_get_priv(dev);
263 struct input_config *input = &uc_priv->input;
268 if (!kbd_controller_present()) {
269 debug("i8042 keyboard controller is not present\n");
273 /* Init keyboard device (default US layout) */
275 penv = env_get("keymap");
277 if (strncmp(penv, "de", 3) == 0)
281 for (try = 0; kbd_reset(priv->quirks) != 0; try++) {
282 if (try >= KBD_RESET_TRIES)
286 ret = input_add_tables(input, keymap == KBD_GER);
290 i8042_kbd_update_leds(dev, NORMAL);
291 debug("%s: started\n", __func__);
296 static int i8042_kbd_remove(struct udevice *dev)
299 log_debug("i8042_disable() failed. fine, continue.\n");
306 * Set up the i8042 keyboard. This is called by the stdio device handler
308 * We want to do this init when the keyboard is actually used rather than
309 * at start-up, since keyboard input may not currently be selected.
311 * Once the keyboard starts there will be a period during which we must
312 * wait for the keyboard to init. We do this only when a key is first
313 * read - see kbd_wait_for_fifo_init().
315 * @return 0 if ok, -ve on error
317 static int i8042_kbd_probe(struct udevice *dev)
319 struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
320 struct i8042_kbd_priv *priv = dev_get_priv(dev);
321 struct stdio_dev *sdev = &uc_priv->sdev;
322 struct input_config *input = &uc_priv->input;
325 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
326 "intel,duplicate-por"))
327 priv->quirks |= QUIRK_DUP_POR;
329 /* Register the device. i8042_start() will be called soon */
331 input->read_keys = i8042_kbd_check;
332 input_allow_repeats(input, true);
333 strcpy(sdev->name, "i8042-kbd");
334 ret = input_stdio_register(sdev);
336 debug("%s: input_stdio_register() failed\n", __func__);
339 debug("%s: ready\n", __func__);
344 static const struct keyboard_ops i8042_kbd_ops = {
345 .start = i8042_start,
346 .update_leds = i8042_kbd_update_leds,
349 static const struct udevice_id i8042_kbd_ids[] = {
350 { .compatible = "intel,i8042-keyboard" },
354 U_BOOT_DRIVER(i8042_kbd) = {
356 .id = UCLASS_KEYBOARD,
357 .of_match = i8042_kbd_ids,
358 .probe = i8042_kbd_probe,
359 .remove = i8042_kbd_remove,
360 .ops = &i8042_kbd_ops,
361 .priv_auto = sizeof(struct i8042_kbd_priv),