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 */
20 #define out8(p, v) outb(v, p)
23 struct i8042_kbd_priv {
24 bool extended; /* true if an extended keycode is expected next */
27 static unsigned char ext_key_map[] = {
28 0x1c, /* keypad enter */
29 0x1d, /* right control */
30 0x35, /* keypad slash */
31 0x37, /* print screen */
34 0x47, /* editpad home */
35 0x48, /* editpad up */
36 0x49, /* editpad pgup */
37 0x4b, /* editpad left */
38 0x4d, /* editpad right */
39 0x4f, /* editpad end */
40 0x50, /* editpad dn */
41 0x51, /* editpad pgdn */
42 0x52, /* editpad ins */
43 0x53, /* editpad del */
47 static int kbd_input_empty(void)
49 int kbd_timeout = KBD_TIMEOUT * 1000;
51 while ((in8(I8042_STS_REG) & STATUS_IBF) && kbd_timeout--)
54 return kbd_timeout != -1;
57 static int kbd_output_full(void)
59 int kbd_timeout = KBD_TIMEOUT * 1000;
61 while (((in8(I8042_STS_REG) & STATUS_OBF) == 0) && kbd_timeout--)
64 return kbd_timeout != -1;
68 * check_leds() - Check the keyboard LEDs and update them it needed
70 * @ret: Value to return
71 * @return value of @ret
73 static int i8042_kbd_update_leds(struct udevice *dev, int leds)
76 out8(I8042_DATA_REG, CMD_SET_KBD_LED);
78 out8(I8042_DATA_REG, leds & 0x7);
83 static int kbd_write(int reg, int value)
85 if (!kbd_input_empty())
92 static int kbd_read(int reg)
94 if (!kbd_output_full())
100 static int kbd_cmd_read(int cmd)
102 if (kbd_write(I8042_CMD_REG, cmd))
105 return kbd_read(I8042_DATA_REG);
108 static int kbd_cmd_write(int cmd, int data)
110 if (kbd_write(I8042_CMD_REG, cmd))
113 return kbd_write(I8042_DATA_REG, data);
116 static int kbd_reset(void)
120 /* controller self test */
121 if (kbd_cmd_read(CMD_SELF_TEST) != KBC_TEST_OK)
125 if (kbd_write(I8042_DATA_REG, CMD_RESET_KBD) ||
126 kbd_read(I8042_DATA_REG) != KBD_ACK ||
127 kbd_read(I8042_DATA_REG) != KBD_POR)
130 /* set AT translation and disable irq */
131 config = kbd_cmd_read(CMD_RD_CONFIG);
135 config |= CONFIG_AT_TRANS;
136 config &= ~(CONFIG_KIRQ_EN | CONFIG_MIRQ_EN);
137 if (kbd_cmd_write(CMD_WR_CONFIG, config))
140 /* enable keyboard */
141 if (kbd_write(I8042_CMD_REG, CMD_KBD_EN) ||
147 debug("%s: Keyboard failure\n", __func__);
151 static int kbd_controller_present(void)
153 return in8(I8042_STS_REG) != 0xff;
157 * Implement a weak default function for boards that optionally
158 * need to skip the i8042 initialization.
160 * TODO(sjg@chromium.org): Use device tree for this?
162 int __weak board_i8042_skip(void)
164 /* As default, don't skip */
168 void i8042_flush(void)
173 * The delay is to give the keyboard controller some time
174 * to fill the next byte.
177 timeout = 100; /* wait for no longer than 100us */
178 while (timeout > 0 && !(in8(I8042_STS_REG) & STATUS_OBF)) {
183 /* Try to pull next byte if not timeout */
184 if (in8(I8042_STS_REG) & STATUS_OBF)
191 int i8042_disable(void)
193 if (kbd_input_empty() == 0)
196 /* Disable keyboard */
197 out8(I8042_CMD_REG, CMD_KBD_DIS);
199 if (kbd_input_empty() == 0)
205 static int i8042_kbd_check(struct input_config *input)
207 struct i8042_kbd_priv *priv = dev_get_priv(input->dev);
209 if ((in8(I8042_STS_REG) & STATUS_OBF) == 0) {
212 bool release = false;
216 scan_code = in8(I8042_DATA_REG);
217 if (scan_code == 0xfa) {
219 } else if (scan_code == 0xe0) {
220 priv->extended = true;
223 if (scan_code & 0x80) {
227 if (priv->extended) {
228 priv->extended = false;
229 for (i = 0; ext_key_map[i]; i++) {
230 if (ext_key_map[i] == scan_code) {
231 scan_code = 0x60 + i;
240 input_add_keycode(input, scan_code, release);
245 /* i8042_kbd_init - reset keyboard and init state flags */
246 static int i8042_start(struct udevice *dev)
248 struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
249 struct input_config *input = &uc_priv->input;
254 if (!kbd_controller_present() || board_i8042_skip()) {
255 debug("i8042 keyboard controller is not present\n");
259 /* Init keyboard device (default US layout) */
261 penv = getenv("keymap");
263 if (strncmp(penv, "de", 3) == 0)
267 for (try = 0; kbd_reset() != 0; try++) {
268 if (try >= KBD_RESET_TRIES)
272 ret = input_add_tables(input, keymap == KBD_GER);
276 i8042_kbd_update_leds(dev, NORMAL);
277 debug("%s: started\n", __func__);
283 * Set up the i8042 keyboard. This is called by the stdio device handler
285 * We want to do this init when the keyboard is actually used rather than
286 * at start-up, since keyboard input may not currently be selected.
288 * Once the keyboard starts there will be a period during which we must
289 * wait for the keyboard to init. We do this only when a key is first
290 * read - see kbd_wait_for_fifo_init().
292 * @return 0 if ok, -ve on error
294 static int i8042_kbd_probe(struct udevice *dev)
296 struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
297 struct stdio_dev *sdev = &uc_priv->sdev;
298 struct input_config *input = &uc_priv->input;
301 /* Register the device. i8042_start() will be called soon */
303 input->read_keys = i8042_kbd_check;
304 input_allow_repeats(input, true);
305 strcpy(sdev->name, "i8042-kbd");
306 ret = input_stdio_register(sdev);
308 debug("%s: input_stdio_register() failed\n", __func__);
311 debug("%s: ready\n", __func__);
316 static const struct keyboard_ops i8042_kbd_ops = {
317 .start = i8042_start,
318 .update_leds = i8042_kbd_update_leds,
321 static const struct udevice_id i8042_kbd_ids[] = {
322 { .compatible = "intel,i8042-keyboard" },
326 U_BOOT_DRIVER(i8042_kbd) = {
328 .id = UCLASS_KEYBOARD,
329 .of_match = i8042_kbd_ids,
330 .probe = i8042_kbd_probe,
331 .ops = &i8042_kbd_ops,
332 .priv_auto_alloc_size = sizeof(struct i8042_kbd_priv),