2 * (C) Copyright 2002 ELTEC Elektronik AG
3 * Frank Gottschling <fgottschling@eltec.de>
5 * SPDX-License-Identifier: GPL-2.0+
8 /* i8042.c - Intel 8042 keyboard driver routines */
17 #define out8(p, v) outb(v, p)
20 static struct input_config config;
23 static unsigned char ext_key_map[] = {
24 0x1c, /* keypad enter */
25 0x1d, /* right control */
26 0x35, /* keypad slash */
27 0x37, /* print screen */
30 0x47, /* editpad home */
31 0x48, /* editpad up */
32 0x49, /* editpad pgup */
33 0x4b, /* editpad left */
34 0x4d, /* editpad right */
35 0x4f, /* editpad end */
36 0x50, /* editpad dn */
37 0x51, /* editpad pgdn */
38 0x52, /* editpad ins */
39 0x53, /* editpad del */
43 static int kbd_input_empty(void)
45 int kbd_timeout = KBD_TIMEOUT * 1000;
47 while ((in8(I8042_STS_REG) & STATUS_IBF) && kbd_timeout--)
50 return kbd_timeout != -1;
53 static int kbd_output_full(void)
55 int kbd_timeout = KBD_TIMEOUT * 1000;
57 while (((in8(I8042_STS_REG) & STATUS_OBF) == 0) && kbd_timeout--)
60 return kbd_timeout != -1;
63 static void kbd_led_set(int flags)
66 out8(I8042_DATA_REG, CMD_SET_KBD_LED);
68 out8(I8042_DATA_REG, flags & 0x7);
71 static int kbd_write(int reg, int value)
73 if (!kbd_input_empty())
80 static int kbd_read(int reg)
82 if (!kbd_output_full())
88 static int kbd_cmd_read(int cmd)
90 if (kbd_write(I8042_CMD_REG, cmd))
93 return kbd_read(I8042_DATA_REG);
96 static int kbd_cmd_write(int cmd, int data)
98 if (kbd_write(I8042_CMD_REG, cmd))
101 return kbd_write(I8042_DATA_REG, data);
104 static int kbd_reset(void)
108 /* controller self test */
109 if (kbd_cmd_read(CMD_SELF_TEST) != KBC_TEST_OK)
113 if (kbd_write(I8042_DATA_REG, CMD_RESET_KBD) ||
114 kbd_read(I8042_DATA_REG) != KBD_ACK ||
115 kbd_read(I8042_DATA_REG) != KBD_POR)
118 /* set AT translation and disable irq */
119 config = kbd_cmd_read(CMD_RD_CONFIG);
123 config |= CONFIG_AT_TRANS;
124 config &= ~(CONFIG_KIRQ_EN | CONFIG_MIRQ_EN);
125 if (kbd_cmd_write(CMD_WR_CONFIG, config))
128 /* enable keyboard */
129 if (kbd_write(I8042_CMD_REG, CMD_KBD_EN) ||
135 debug("%s: Keyboard failure\n", __func__);
139 static int kbd_controller_present(void)
141 return in8(I8042_STS_REG) != 0xff;
145 * Implement a weak default function for boards that optionally
146 * need to skip the i8042 initialization.
148 int __weak board_i8042_skip(void)
150 /* As default, don't skip */
154 void i8042_flush(void)
159 * The delay is to give the keyboard controller some time
160 * to fill the next byte.
163 timeout = 100; /* wait for no longer than 100us */
164 while (timeout > 0 && !(in8(I8042_STS_REG) & STATUS_OBF)) {
169 /* Try to pull next byte if not timeout */
170 if (in8(I8042_STS_REG) & STATUS_OBF)
177 int i8042_disable(void)
179 if (kbd_input_empty() == 0)
182 /* Disable keyboard */
183 out8(I8042_CMD_REG, CMD_KBD_DIS);
185 if (kbd_input_empty() == 0)
191 static int i8042_kbd_check(struct input_config *input)
193 if ((in8(I8042_STS_REG) & STATUS_OBF) == 0) {
196 bool release = false;
200 scan_code = in8(I8042_DATA_REG);
201 if (scan_code == 0xfa) {
203 } else if (scan_code == 0xe0) {
207 if (scan_code & 0x80) {
213 for (i = 0; ext_key_map[i]; i++) {
214 if (ext_key_map[i] == scan_code) {
215 scan_code = 0x60 + i;
224 input_add_keycode(&config, scan_code, release);
229 /* i8042_kbd_init - reset keyboard and init state flags */
230 int i8042_kbd_init(void)
236 if (!kbd_controller_present() || board_i8042_skip()) {
237 debug("i8042 keyboard controller is not present\n");
241 /* Init keyboard device (default US layout) */
243 penv = getenv("keymap");
245 if (strncmp(penv, "de", 3) == 0)
249 for (try = 0; kbd_reset() != 0; try++) {
250 if (try >= KBD_RESET_TRIES)
254 ret = input_init(&config, keymap == KBD_GER);
257 config.read_keys = i8042_kbd_check;
258 input_allow_repeats(&config, true);
266 * check_leds() - Check the keyboard LEDs and update them it needed
268 * @ret: Value to return
269 * @return value of @ret
271 static int check_leds(int ret)
275 leds = input_leds_changed(&config);
282 /* i8042_tstc - test if keyboard input is available */
283 int i8042_tstc(struct stdio_dev *dev)
285 return check_leds(input_tstc(&config));
288 /* i8042_getc - wait till keyboard input is available */
289 int i8042_getc(struct stdio_dev *dev)
291 return check_leds(input_getc(&config));