2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2002-11-03 00:24:07 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2002 ELTEC Elektronik AG
|
|
|
|
* Frank Gottschling <fgottschling@eltec.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* i8042.c - Intel 8042 keyboard driver routines */
|
|
|
|
|
|
|
|
#include <common.h>
|
2015-11-11 17:05:45 +00:00
|
|
|
#include <dm.h>
|
2019-08-01 15:46:52 +00:00
|
|
|
#include <env.h>
|
2015-11-11 17:05:45 +00:00
|
|
|
#include <errno.h>
|
2002-11-03 00:24:07 +00:00
|
|
|
#include <i8042.h>
|
2015-11-11 17:05:41 +00:00
|
|
|
#include <input.h>
|
2015-11-11 17:05:45 +00:00
|
|
|
#include <keyboard.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2020-10-31 03:38:53 +00:00
|
|
|
#include <asm/global_data.h>
|
2015-11-11 17:05:41 +00:00
|
|
|
#include <asm/io.h>
|
2020-05-10 17:40:11 +00:00
|
|
|
#include <linux/delay.h>
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2015-11-11 17:05:46 +00:00
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2002-11-03 00:24:07 +00:00
|
|
|
/* defines */
|
2015-08-24 08:00:05 +00:00
|
|
|
#define in8(p) inb(p)
|
|
|
|
#define out8(p, v) outb(v, p)
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2015-11-11 17:05:46 +00:00
|
|
|
enum {
|
|
|
|
QUIRK_DUP_POR = 1 << 0,
|
|
|
|
};
|
|
|
|
|
2002-11-03 00:24:07 +00:00
|
|
|
/* locals */
|
2015-11-11 17:05:45 +00:00
|
|
|
struct i8042_kbd_priv {
|
|
|
|
bool extended; /* true if an extended keycode is expected next */
|
2015-11-11 17:05:46 +00:00
|
|
|
int quirks; /* quirks that we support */
|
2015-11-11 17:05:45 +00:00
|
|
|
};
|
2011-11-14 19:24:14 +00:00
|
|
|
|
|
|
|
static unsigned char ext_key_map[] = {
|
|
|
|
0x1c, /* keypad enter */
|
|
|
|
0x1d, /* right control */
|
|
|
|
0x35, /* keypad slash */
|
|
|
|
0x37, /* print screen */
|
|
|
|
0x38, /* right alt */
|
|
|
|
0x46, /* break */
|
|
|
|
0x47, /* editpad home */
|
|
|
|
0x48, /* editpad up */
|
|
|
|
0x49, /* editpad pgup */
|
|
|
|
0x4b, /* editpad left */
|
|
|
|
0x4d, /* editpad right */
|
|
|
|
0x4f, /* editpad end */
|
|
|
|
0x50, /* editpad dn */
|
|
|
|
0x51, /* editpad pgdn */
|
|
|
|
0x52, /* editpad ins */
|
|
|
|
0x53, /* editpad del */
|
|
|
|
0x00 /* map end */
|
|
|
|
};
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2015-08-24 08:00:04 +00:00
|
|
|
static int kbd_input_empty(void)
|
|
|
|
{
|
2015-08-24 08:00:05 +00:00
|
|
|
int kbd_timeout = KBD_TIMEOUT * 1000;
|
2015-08-24 08:00:04 +00:00
|
|
|
|
2015-08-24 08:00:05 +00:00
|
|
|
while ((in8(I8042_STS_REG) & STATUS_IBF) && kbd_timeout--)
|
2015-08-24 08:00:04 +00:00
|
|
|
udelay(1);
|
|
|
|
|
2015-08-24 08:00:05 +00:00
|
|
|
return kbd_timeout != -1;
|
2015-08-24 08:00:04 +00:00
|
|
|
}
|
|
|
|
|
2015-08-24 08:00:05 +00:00
|
|
|
static int kbd_output_full(void)
|
2015-08-24 08:00:04 +00:00
|
|
|
{
|
2015-08-24 08:00:05 +00:00
|
|
|
int kbd_timeout = KBD_TIMEOUT * 1000;
|
2015-08-24 08:00:04 +00:00
|
|
|
|
2015-08-24 08:00:05 +00:00
|
|
|
while (((in8(I8042_STS_REG) & STATUS_OBF) == 0) && kbd_timeout--)
|
2015-08-24 08:00:04 +00:00
|
|
|
udelay(1);
|
|
|
|
|
2015-08-24 08:00:05 +00:00
|
|
|
return kbd_timeout != -1;
|
2015-08-24 08:00:04 +00:00
|
|
|
}
|
|
|
|
|
2015-11-11 17:05:45 +00:00
|
|
|
/**
|
|
|
|
* check_leds() - Check the keyboard LEDs and update them it needed
|
|
|
|
*
|
|
|
|
* @ret: Value to return
|
2022-01-19 17:05:50 +00:00
|
|
|
* Return: value of @ret
|
2015-11-11 17:05:45 +00:00
|
|
|
*/
|
|
|
|
static int i8042_kbd_update_leds(struct udevice *dev, int leds)
|
2015-08-24 08:00:04 +00:00
|
|
|
{
|
|
|
|
kbd_input_empty();
|
2015-08-24 08:00:05 +00:00
|
|
|
out8(I8042_DATA_REG, CMD_SET_KBD_LED);
|
2015-08-24 08:00:04 +00:00
|
|
|
kbd_input_empty();
|
2015-11-11 17:05:45 +00:00
|
|
|
out8(I8042_DATA_REG, leds & 0x7);
|
|
|
|
|
|
|
|
return 0;
|
2015-08-24 08:00:04 +00:00
|
|
|
}
|
|
|
|
|
2015-10-19 03:17:19 +00:00
|
|
|
static int kbd_write(int reg, int value)
|
2015-08-24 08:00:04 +00:00
|
|
|
{
|
2015-10-19 03:17:19 +00:00
|
|
|
if (!kbd_input_empty())
|
2015-08-24 08:00:06 +00:00
|
|
|
return -1;
|
2015-10-19 03:17:19 +00:00
|
|
|
out8(reg, value);
|
2015-08-24 08:00:04 +00:00
|
|
|
|
2015-10-19 03:17:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int kbd_read(int reg)
|
|
|
|
{
|
|
|
|
if (!kbd_output_full())
|
2015-08-24 08:00:06 +00:00
|
|
|
return -1;
|
2015-10-19 03:17:19 +00:00
|
|
|
|
|
|
|
return in8(reg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int kbd_cmd_read(int cmd)
|
|
|
|
{
|
|
|
|
if (kbd_write(I8042_CMD_REG, cmd))
|
2015-08-24 08:00:04 +00:00
|
|
|
return -1;
|
2015-10-19 03:17:19 +00:00
|
|
|
|
|
|
|
return kbd_read(I8042_DATA_REG);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int kbd_cmd_write(int cmd, int data)
|
|
|
|
{
|
|
|
|
if (kbd_write(I8042_CMD_REG, cmd))
|
2015-08-24 08:00:04 +00:00
|
|
|
return -1;
|
2015-10-19 03:17:19 +00:00
|
|
|
|
|
|
|
return kbd_write(I8042_DATA_REG, data);
|
|
|
|
}
|
|
|
|
|
2015-11-11 17:05:46 +00:00
|
|
|
static int kbd_reset(int quirk)
|
2015-10-19 03:17:19 +00:00
|
|
|
{
|
|
|
|
int config;
|
|
|
|
|
|
|
|
/* controller self test */
|
|
|
|
if (kbd_cmd_read(CMD_SELF_TEST) != KBC_TEST_OK)
|
2015-10-19 03:17:20 +00:00
|
|
|
goto err;
|
2015-10-19 03:17:19 +00:00
|
|
|
|
|
|
|
/* keyboard reset */
|
|
|
|
if (kbd_write(I8042_DATA_REG, CMD_RESET_KBD) ||
|
|
|
|
kbd_read(I8042_DATA_REG) != KBD_ACK ||
|
|
|
|
kbd_read(I8042_DATA_REG) != KBD_POR)
|
2015-10-19 03:17:20 +00:00
|
|
|
goto err;
|
2015-08-24 08:00:04 +00:00
|
|
|
|
2016-03-12 05:06:50 +00:00
|
|
|
if (kbd_write(I8042_DATA_REG, CMD_DRAIN_OUTPUT) ||
|
|
|
|
kbd_read(I8042_DATA_REG) != KBD_ACK)
|
|
|
|
goto err;
|
|
|
|
|
2015-08-24 08:00:06 +00:00
|
|
|
/* set AT translation and disable irq */
|
2015-10-19 03:17:19 +00:00
|
|
|
config = kbd_cmd_read(CMD_RD_CONFIG);
|
|
|
|
if (config == -1)
|
2015-10-19 03:17:20 +00:00
|
|
|
goto err;
|
2015-10-19 03:17:19 +00:00
|
|
|
|
2015-11-11 17:05:46 +00:00
|
|
|
/* Sometimes get a second byte */
|
|
|
|
else if ((quirk & QUIRK_DUP_POR) && config == KBD_POR)
|
|
|
|
config = kbd_cmd_read(CMD_RD_CONFIG);
|
|
|
|
|
2021-08-19 18:58:00 +00:00
|
|
|
config |= CFG_AT_TRANS;
|
|
|
|
config &= ~(CFG_KIRQ_EN | CFG_MIRQ_EN);
|
2015-10-19 03:17:19 +00:00
|
|
|
if (kbd_cmd_write(CMD_WR_CONFIG, config))
|
2015-10-19 03:17:20 +00:00
|
|
|
goto err;
|
2015-08-24 08:00:04 +00:00
|
|
|
|
2015-08-24 08:00:06 +00:00
|
|
|
/* enable keyboard */
|
2015-10-19 03:17:19 +00:00
|
|
|
if (kbd_write(I8042_CMD_REG, CMD_KBD_EN) ||
|
|
|
|
!kbd_input_empty())
|
2015-10-19 03:17:20 +00:00
|
|
|
goto err;
|
2015-08-24 08:00:04 +00:00
|
|
|
|
|
|
|
return 0;
|
2015-10-19 03:17:20 +00:00
|
|
|
err:
|
|
|
|
debug("%s: Keyboard failure\n", __func__);
|
|
|
|
return -1;
|
2015-08-24 08:00:04 +00:00
|
|
|
}
|
|
|
|
|
2011-11-14 20:18:12 +00:00
|
|
|
static int kbd_controller_present(void)
|
|
|
|
{
|
2015-08-24 08:00:05 +00:00
|
|
|
return in8(I8042_STS_REG) != 0xff;
|
2011-11-14 20:18:12 +00:00
|
|
|
}
|
|
|
|
|
2018-11-24 04:29:38 +00:00
|
|
|
/** Flush all buffer from keyboard controller to host*/
|
|
|
|
static void i8042_flush(void)
|
2012-10-11 15:15:51 +00:00
|
|
|
{
|
|
|
|
int timeout;
|
|
|
|
|
|
|
|
/*
|
2015-08-24 08:00:05 +00:00
|
|
|
* The delay is to give the keyboard controller some time
|
|
|
|
* to fill the next byte.
|
2012-10-11 15:15:51 +00:00
|
|
|
*/
|
|
|
|
while (1) {
|
2015-08-24 08:00:05 +00:00
|
|
|
timeout = 100; /* wait for no longer than 100us */
|
|
|
|
while (timeout > 0 && !(in8(I8042_STS_REG) & STATUS_OBF)) {
|
2012-10-11 15:15:51 +00:00
|
|
|
udelay(1);
|
|
|
|
timeout--;
|
|
|
|
}
|
|
|
|
|
2015-08-24 08:00:05 +00:00
|
|
|
/* Try to pull next byte if not timeout */
|
|
|
|
if (in8(I8042_STS_REG) & STATUS_OBF)
|
2012-10-11 15:15:51 +00:00
|
|
|
in8(I8042_DATA_REG);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-24 04:29:38 +00:00
|
|
|
/**
|
|
|
|
* Disables the keyboard so that key strokes no longer generate scancodes to
|
|
|
|
* the host.
|
|
|
|
*
|
2022-01-19 17:05:50 +00:00
|
|
|
* Return: 0 if ok, -1 if keyboard input was found while disabling
|
2018-11-24 04:29:38 +00:00
|
|
|
*/
|
|
|
|
static int i8042_disable(void)
|
2012-10-11 15:15:51 +00:00
|
|
|
{
|
|
|
|
if (kbd_input_empty() == 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Disable keyboard */
|
2015-08-24 08:00:05 +00:00
|
|
|
out8(I8042_CMD_REG, CMD_KBD_DIS);
|
2012-10-11 15:15:51 +00:00
|
|
|
|
|
|
|
if (kbd_input_empty() == 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-11-11 17:05:41 +00:00
|
|
|
static int i8042_kbd_check(struct input_config *input)
|
|
|
|
{
|
2015-11-11 17:05:45 +00:00
|
|
|
struct i8042_kbd_priv *priv = dev_get_priv(input->dev);
|
|
|
|
|
2015-11-11 17:05:41 +00:00
|
|
|
if ((in8(I8042_STS_REG) & STATUS_OBF) == 0) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
bool release = false;
|
|
|
|
int scan_code;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
scan_code = in8(I8042_DATA_REG);
|
|
|
|
if (scan_code == 0xfa) {
|
|
|
|
return 0;
|
|
|
|
} else if (scan_code == 0xe0) {
|
2015-11-11 17:05:45 +00:00
|
|
|
priv->extended = true;
|
2015-11-11 17:05:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (scan_code & 0x80) {
|
|
|
|
scan_code &= 0x7f;
|
|
|
|
release = true;
|
|
|
|
}
|
2015-11-11 17:05:45 +00:00
|
|
|
if (priv->extended) {
|
|
|
|
priv->extended = false;
|
2015-11-11 17:05:41 +00:00
|
|
|
for (i = 0; ext_key_map[i]; i++) {
|
|
|
|
if (ext_key_map[i] == scan_code) {
|
|
|
|
scan_code = 0x60 + i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* not found ? */
|
|
|
|
if (!ext_key_map[i])
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-11-11 17:05:45 +00:00
|
|
|
input_add_keycode(input, scan_code, release);
|
2015-11-11 17:05:41 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-24 08:00:05 +00:00
|
|
|
/* i8042_kbd_init - reset keyboard and init state flags */
|
2015-11-11 17:05:45 +00:00
|
|
|
static int i8042_start(struct udevice *dev)
|
2002-11-03 00:24:07 +00:00
|
|
|
{
|
2015-11-11 17:05:45 +00:00
|
|
|
struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
|
2015-11-11 17:05:46 +00:00
|
|
|
struct i8042_kbd_priv *priv = dev_get_priv(dev);
|
2015-11-11 17:05:45 +00:00
|
|
|
struct input_config *input = &uc_priv->input;
|
2011-11-14 19:24:14 +00:00
|
|
|
int keymap, try;
|
|
|
|
char *penv;
|
2015-11-11 17:05:41 +00:00
|
|
|
int ret;
|
2002-11-03 00:24:07 +00:00
|
|
|
|
2018-11-24 04:29:38 +00:00
|
|
|
if (!kbd_controller_present()) {
|
2015-08-24 08:00:05 +00:00
|
|
|
debug("i8042 keyboard controller is not present\n");
|
2015-11-11 17:05:45 +00:00
|
|
|
return -ENOENT;
|
2015-08-24 08:00:05 +00:00
|
|
|
}
|
2011-11-14 20:18:12 +00:00
|
|
|
|
2011-11-14 19:24:14 +00:00
|
|
|
/* Init keyboard device (default US layout) */
|
|
|
|
keymap = KBD_US;
|
2017-08-03 18:22:12 +00:00
|
|
|
penv = env_get("keymap");
|
2011-11-14 19:24:14 +00:00
|
|
|
if (penv != NULL) {
|
|
|
|
if (strncmp(penv, "de", 3) == 0)
|
|
|
|
keymap = KBD_GER;
|
|
|
|
}
|
|
|
|
|
2015-11-11 17:05:46 +00:00
|
|
|
for (try = 0; kbd_reset(priv->quirks) != 0; try++) {
|
2015-10-19 03:17:21 +00:00
|
|
|
if (try >= KBD_RESET_TRIES)
|
|
|
|
return -1;
|
2011-11-14 19:24:14 +00:00
|
|
|
}
|
2015-08-24 08:00:05 +00:00
|
|
|
|
2015-11-11 17:05:45 +00:00
|
|
|
ret = input_add_tables(input, keymap == KBD_GER);
|
2015-11-11 17:05:41 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2015-11-11 17:05:45 +00:00
|
|
|
i8042_kbd_update_leds(dev, NORMAL);
|
|
|
|
debug("%s: started\n", __func__);
|
2015-10-19 03:17:21 +00:00
|
|
|
|
|
|
|
return 0;
|
2002-11-03 00:24:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-24 04:29:38 +00:00
|
|
|
static int i8042_kbd_remove(struct udevice *dev)
|
|
|
|
{
|
|
|
|
if (i8042_disable())
|
|
|
|
log_debug("i8042_disable() failed. fine, continue.\n");
|
|
|
|
i8042_flush();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-11-11 17:05:41 +00:00
|
|
|
/**
|
2015-11-11 17:05:45 +00:00
|
|
|
* Set up the i8042 keyboard. This is called by the stdio device handler
|
2015-08-24 08:00:05 +00:00
|
|
|
*
|
2015-11-11 17:05:45 +00:00
|
|
|
* We want to do this init when the keyboard is actually used rather than
|
|
|
|
* at start-up, since keyboard input may not currently be selected.
|
|
|
|
*
|
|
|
|
* Once the keyboard starts there will be a period during which we must
|
|
|
|
* wait for the keyboard to init. We do this only when a key is first
|
|
|
|
* read - see kbd_wait_for_fifo_init().
|
|
|
|
*
|
2022-01-19 17:05:50 +00:00
|
|
|
* Return: 0 if ok, -ve on error
|
2002-11-03 00:24:07 +00:00
|
|
|
*/
|
2015-11-11 17:05:45 +00:00
|
|
|
static int i8042_kbd_probe(struct udevice *dev)
|
2002-11-03 00:24:07 +00:00
|
|
|
{
|
2015-11-11 17:05:45 +00:00
|
|
|
struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
|
2015-11-11 17:05:46 +00:00
|
|
|
struct i8042_kbd_priv *priv = dev_get_priv(dev);
|
2015-11-11 17:05:45 +00:00
|
|
|
struct stdio_dev *sdev = &uc_priv->sdev;
|
|
|
|
struct input_config *input = &uc_priv->input;
|
|
|
|
int ret;
|
2011-11-14 19:24:14 +00:00
|
|
|
|
2017-01-17 23:52:55 +00:00
|
|
|
if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
|
2015-11-11 17:05:46 +00:00
|
|
|
"intel,duplicate-por"))
|
|
|
|
priv->quirks |= QUIRK_DUP_POR;
|
|
|
|
|
2015-11-11 17:05:45 +00:00
|
|
|
/* Register the device. i8042_start() will be called soon */
|
|
|
|
input->dev = dev;
|
|
|
|
input->read_keys = i8042_kbd_check;
|
|
|
|
input_allow_repeats(input, true);
|
|
|
|
strcpy(sdev->name, "i8042-kbd");
|
|
|
|
ret = input_stdio_register(sdev);
|
|
|
|
if (ret) {
|
|
|
|
debug("%s: input_stdio_register() failed\n", __func__);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
debug("%s: ready\n", __func__);
|
2015-08-24 08:00:05 +00:00
|
|
|
|
2015-11-11 17:05:45 +00:00
|
|
|
return 0;
|
2002-11-03 00:24:07 +00:00
|
|
|
}
|
|
|
|
|
2015-11-11 17:05:45 +00:00
|
|
|
static const struct keyboard_ops i8042_kbd_ops = {
|
|
|
|
.start = i8042_start,
|
|
|
|
.update_leds = i8042_kbd_update_leds,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id i8042_kbd_ids[] = {
|
|
|
|
{ .compatible = "intel,i8042-keyboard" },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(i8042_kbd) = {
|
|
|
|
.name = "i8042_kbd",
|
|
|
|
.id = UCLASS_KEYBOARD,
|
|
|
|
.of_match = i8042_kbd_ids,
|
|
|
|
.probe = i8042_kbd_probe,
|
2018-11-24 04:29:38 +00:00
|
|
|
.remove = i8042_kbd_remove,
|
2015-11-11 17:05:45 +00:00
|
|
|
.ops = &i8042_kbd_ops,
|
2020-12-03 23:55:17 +00:00
|
|
|
.priv_auto = sizeof(struct i8042_kbd_priv),
|
2015-11-11 17:05:45 +00:00
|
|
|
};
|