1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2002 ELTEC Elektronik AG
4 * Frank Gottschling <fgottschling@eltec.de>
7 /* i8042.c - Intel 8042 keyboard driver routines */
9 #define LOG_CATEGORY UCLASS_KEYBOARD
19 #include <asm/global_data.h>
21 #include <linux/delay.h>
23 DECLARE_GLOBAL_DATA_PTR;
27 #define out8(p, v) outb(v, p)
30 QUIRK_DUP_POR = 1 << 0,
34 struct i8042_kbd_priv {
35 bool extended; /* true if an extended keycode is expected next */
36 int quirks; /* quirks that we support */
39 static unsigned char ext_key_map[] = {
40 0x1c, /* keypad enter */
41 0x1d, /* right control */
42 0x35, /* keypad slash */
43 0x37, /* print screen */
46 0x47, /* editpad home */
47 0x48, /* editpad up */
48 0x49, /* editpad pgup */
49 0x4b, /* editpad left */
50 0x4d, /* editpad right */
51 0x4f, /* editpad end */
52 0x50, /* editpad dn */
53 0x51, /* editpad pgdn */
54 0x52, /* editpad ins */
55 0x53, /* editpad del */
60 * kbd_input_empty() - Wait until the keyboard is ready for a command
62 * Checks the IBF flag (input buffer full), waiting for it to indicate that
63 * any previous command has been processed.
65 * Return: true if ready, false if it timed out
67 static int kbd_input_empty(void)
69 int kbd_timeout = KBD_TIMEOUT * 1000;
71 while ((in8(I8042_STS_REG) & STATUS_IBF) && kbd_timeout--)
74 return kbd_timeout != -1;
78 * kbd_output_full() - Wait until the keyboard has data available
80 * Checks the OBF flag (output buffer full), waiting for it to indicate that
81 * a response to a previous command is available
83 static int kbd_output_full(void)
85 int kbd_timeout = KBD_TIMEOUT * 1000;
87 while (((in8(I8042_STS_REG) & STATUS_OBF) == 0) && kbd_timeout--)
90 return kbd_timeout != -1;
94 * check_leds() - Check the keyboard LEDs and update them it needed
96 * @ret: Value to return
97 * Return: value of @ret
99 static int i8042_kbd_update_leds(struct udevice *dev, int leds)
102 out8(I8042_DATA_REG, CMD_SET_KBD_LED);
104 out8(I8042_DATA_REG, leds & 0x7);
109 static int kbd_write(int reg, int value)
111 if (!kbd_input_empty())
118 static int kbd_read(int reg)
120 if (!kbd_output_full())
126 static int kbd_cmd_read(int cmd)
128 if (kbd_write(I8042_CMD_REG, cmd))
131 return kbd_read(I8042_DATA_REG);
134 static int kbd_cmd_write(int cmd, int data)
136 if (kbd_write(I8042_CMD_REG, cmd))
139 return kbd_write(I8042_DATA_REG, data);
142 static int kbd_reset(int quirk)
146 if (!kbd_input_empty())
149 /* controller self test */
150 if (kbd_cmd_read(CMD_SELF_TEST) != KBC_TEST_OK)
154 if (kbd_write(I8042_DATA_REG, CMD_RESET_KBD) ||
155 kbd_read(I8042_DATA_REG) != KBD_ACK ||
156 kbd_read(I8042_DATA_REG) != KBD_POR)
159 if (kbd_write(I8042_DATA_REG, CMD_DRAIN_OUTPUT) ||
160 kbd_read(I8042_DATA_REG) != KBD_ACK)
163 /* set AT translation and disable irq */
164 config = kbd_cmd_read(CMD_RD_CONFIG);
168 /* Sometimes get a second byte */
169 else if ((quirk & QUIRK_DUP_POR) && config == KBD_POR)
170 config = kbd_cmd_read(CMD_RD_CONFIG);
172 config |= CFG_AT_TRANS;
173 config &= ~(CFG_KIRQ_EN | CFG_MIRQ_EN);
174 if (kbd_cmd_write(CMD_WR_CONFIG, config))
177 /* enable keyboard */
178 if (kbd_write(I8042_CMD_REG, CMD_KBD_EN) ||
184 debug("%s: Keyboard failure\n", __func__);
188 static int kbd_controller_present(void)
190 return in8(I8042_STS_REG) != 0xff;
193 /** Flush all buffer from keyboard controller to host*/
194 static void i8042_flush(void)
199 * The delay is to give the keyboard controller some time
200 * to fill the next byte.
203 timeout = 100; /* wait for no longer than 100us */
204 while (timeout > 0 && !(in8(I8042_STS_REG) & STATUS_OBF)) {
209 /* Try to pull next byte if not timeout */
210 if (in8(I8042_STS_REG) & STATUS_OBF)
218 * Disables the keyboard so that key strokes no longer generate scancodes to
221 * Return: 0 if ok, -1 if keyboard input was found while disabling
223 static int i8042_disable(void)
225 if (kbd_input_empty() == 0)
228 /* Disable keyboard */
229 out8(I8042_CMD_REG, CMD_KBD_DIS);
231 if (kbd_input_empty() == 0)
237 static int i8042_kbd_check(struct input_config *input)
239 struct i8042_kbd_priv *priv = dev_get_priv(input->dev);
241 if ((in8(I8042_STS_REG) & STATUS_OBF) == 0) {
244 bool release = false;
248 scan_code = in8(I8042_DATA_REG);
249 if (scan_code == 0xfa) {
251 } else if (scan_code == 0xe0) {
252 priv->extended = true;
255 if (scan_code & 0x80) {
259 if (priv->extended) {
260 priv->extended = false;
261 for (i = 0; ext_key_map[i]; i++) {
262 if (ext_key_map[i] == scan_code) {
263 scan_code = 0x60 + i;
272 input_add_keycode(input, scan_code, release);
277 /* i8042_kbd_init - reset keyboard and init state flags */
278 static int i8042_start(struct udevice *dev)
280 struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
281 struct i8042_kbd_priv *priv = dev_get_priv(dev);
282 struct input_config *input = &uc_priv->input;
287 if (!kbd_controller_present()) {
288 debug("i8042 keyboard controller is not present\n");
292 /* Init keyboard device (default US layout) */
294 penv = env_get("keymap");
296 if (strncmp(penv, "de", 3) == 0)
300 for (try = 0; kbd_reset(priv->quirks) != 0; try++) {
301 if (try >= KBD_RESET_TRIES)
305 ret = input_add_tables(input, keymap == KBD_GER);
309 i8042_kbd_update_leds(dev, NORMAL);
310 debug("%s: started\n", __func__);
315 static int i8042_kbd_remove(struct udevice *dev)
318 log_debug("i8042_disable() failed. fine, continue.\n");
325 * Set up the i8042 keyboard. This is called by the stdio device handler
327 * We want to do this init when the keyboard is actually used rather than
328 * at start-up, since keyboard input may not currently be selected.
330 * Once the keyboard starts there will be a period during which we must
331 * wait for the keyboard to init. We do this only when a key is first
332 * read - see kbd_wait_for_fifo_init().
334 * Return: 0 if ok, -ve on error
336 static int i8042_kbd_probe(struct udevice *dev)
338 struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
339 struct i8042_kbd_priv *priv = dev_get_priv(dev);
340 struct stdio_dev *sdev = &uc_priv->sdev;
341 struct input_config *input = &uc_priv->input;
344 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
345 "intel,duplicate-por"))
346 priv->quirks |= QUIRK_DUP_POR;
348 /* Register the device. i8042_start() will be called soon */
350 input->read_keys = i8042_kbd_check;
351 input_allow_repeats(input, true);
352 strcpy(sdev->name, "i8042-kbd");
353 ret = input_stdio_register(sdev);
355 debug("%s: input_stdio_register() failed\n", __func__);
358 debug("%s: ready\n", __func__);
363 static const struct keyboard_ops i8042_kbd_ops = {
364 .start = i8042_start,
365 .update_leds = i8042_kbd_update_leds,
368 static const struct udevice_id i8042_kbd_ids[] = {
369 { .compatible = "intel,i8042-keyboard" },
373 U_BOOT_DRIVER(i8042_kbd) = {
375 .id = UCLASS_KEYBOARD,
376 .of_match = i8042_kbd_ids,
377 .probe = i8042_kbd_probe,
378 .remove = i8042_kbd_remove,
379 .ops = &i8042_kbd_ops,
380 .priv_auto = sizeof(struct i8042_kbd_priv),