blob: 3563dc98838fd7234f6090a6e85835714d76bec4 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00002/*
3 * (C) Copyright 2002 ELTEC Elektronik AG
4 * Frank Gottschling <fgottschling@eltec.de>
wdenkc6097192002-11-03 00:24:07 +00005 */
6
7/* i8042.c - Intel 8042 keyboard driver routines */
8
wdenkc6097192002-11-03 00:24:07 +00009#include <common.h>
Simon Glassdcbf8252015-11-11 10:05:45 -070010#include <dm.h>
Simon Glass7b51b572019-08-01 09:46:52 -060011#include <env.h>
Simon Glassdcbf8252015-11-11 10:05:45 -070012#include <errno.h>
wdenkc6097192002-11-03 00:24:07 +000013#include <i8042.h>
Simon Glass2ec739d2015-11-11 10:05:41 -070014#include <input.h>
Simon Glassdcbf8252015-11-11 10:05:45 -070015#include <keyboard.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060016#include <log.h>
Simon Glass401d1c42020-10-30 21:38:53 -060017#include <asm/global_data.h>
Simon Glass2ec739d2015-11-11 10:05:41 -070018#include <asm/io.h>
Simon Glassc05ed002020-05-10 11:40:11 -060019#include <linux/delay.h>
wdenkc6097192002-11-03 00:24:07 +000020
Simon Glass011d89d2015-11-11 10:05:46 -070021DECLARE_GLOBAL_DATA_PTR;
22
wdenkc6097192002-11-03 00:24:07 +000023/* defines */
Bin Meng835dd002015-08-24 01:00:05 -070024#define in8(p) inb(p)
25#define out8(p, v) outb(v, p)
wdenkc6097192002-11-03 00:24:07 +000026
Simon Glass011d89d2015-11-11 10:05:46 -070027enum {
28 QUIRK_DUP_POR = 1 << 0,
29};
30
wdenkc6097192002-11-03 00:24:07 +000031/* locals */
Simon Glassdcbf8252015-11-11 10:05:45 -070032struct i8042_kbd_priv {
33 bool extended; /* true if an extended keycode is expected next */
Simon Glass011d89d2015-11-11 10:05:46 -070034 int quirks; /* quirks that we support */
Simon Glassdcbf8252015-11-11 10:05:45 -070035};
wdenkc6097192002-11-03 00:24:07 +000036
Gabe Blackdd4a5b22011-11-14 19:24:14 +000037static unsigned char ext_key_map[] = {
38 0x1c, /* keypad enter */
39 0x1d, /* right control */
40 0x35, /* keypad slash */
41 0x37, /* print screen */
42 0x38, /* right alt */
43 0x46, /* break */
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 */
54 0x00 /* map end */
55 };
wdenkc6097192002-11-03 00:24:07 +000056
Bin Meng3928d662015-08-24 01:00:04 -070057static int kbd_input_empty(void)
58{
Bin Meng835dd002015-08-24 01:00:05 -070059 int kbd_timeout = KBD_TIMEOUT * 1000;
Bin Meng3928d662015-08-24 01:00:04 -070060
Bin Meng835dd002015-08-24 01:00:05 -070061 while ((in8(I8042_STS_REG) & STATUS_IBF) && kbd_timeout--)
Bin Meng3928d662015-08-24 01:00:04 -070062 udelay(1);
63
Bin Meng835dd002015-08-24 01:00:05 -070064 return kbd_timeout != -1;
Bin Meng3928d662015-08-24 01:00:04 -070065}
66
Bin Meng835dd002015-08-24 01:00:05 -070067static int kbd_output_full(void)
Bin Meng3928d662015-08-24 01:00:04 -070068{
Bin Meng835dd002015-08-24 01:00:05 -070069 int kbd_timeout = KBD_TIMEOUT * 1000;
Bin Meng3928d662015-08-24 01:00:04 -070070
Bin Meng835dd002015-08-24 01:00:05 -070071 while (((in8(I8042_STS_REG) & STATUS_OBF) == 0) && kbd_timeout--)
Bin Meng3928d662015-08-24 01:00:04 -070072 udelay(1);
73
Bin Meng835dd002015-08-24 01:00:05 -070074 return kbd_timeout != -1;
Bin Meng3928d662015-08-24 01:00:04 -070075}
76
Simon Glassdcbf8252015-11-11 10:05:45 -070077/**
78 * check_leds() - Check the keyboard LEDs and update them it needed
79 *
80 * @ret: Value to return
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010081 * Return: value of @ret
Simon Glassdcbf8252015-11-11 10:05:45 -070082 */
83static int i8042_kbd_update_leds(struct udevice *dev, int leds)
Bin Meng3928d662015-08-24 01:00:04 -070084{
85 kbd_input_empty();
Bin Meng835dd002015-08-24 01:00:05 -070086 out8(I8042_DATA_REG, CMD_SET_KBD_LED);
Bin Meng3928d662015-08-24 01:00:04 -070087 kbd_input_empty();
Simon Glassdcbf8252015-11-11 10:05:45 -070088 out8(I8042_DATA_REG, leds & 0x7);
89
90 return 0;
Bin Meng3928d662015-08-24 01:00:04 -070091}
92
Simon Glass31d38ee2015-10-18 21:17:19 -060093static int kbd_write(int reg, int value)
94{
95 if (!kbd_input_empty())
96 return -1;
97 out8(reg, value);
98
99 return 0;
100}
101
102static int kbd_read(int reg)
103{
104 if (!kbd_output_full())
105 return -1;
106
107 return in8(reg);
108}
109
110static int kbd_cmd_read(int cmd)
111{
112 if (kbd_write(I8042_CMD_REG, cmd))
113 return -1;
114
115 return kbd_read(I8042_DATA_REG);
116}
117
118static int kbd_cmd_write(int cmd, int data)
119{
120 if (kbd_write(I8042_CMD_REG, cmd))
121 return -1;
122
123 return kbd_write(I8042_DATA_REG, data);
124}
125
Simon Glass011d89d2015-11-11 10:05:46 -0700126static int kbd_reset(int quirk)
Bin Meng3928d662015-08-24 01:00:04 -0700127{
Simon Glass31d38ee2015-10-18 21:17:19 -0600128 int config;
Bin Meng7d961662015-08-24 01:00:06 -0700129
130 /* controller self test */
Simon Glass31d38ee2015-10-18 21:17:19 -0600131 if (kbd_cmd_read(CMD_SELF_TEST) != KBC_TEST_OK)
Simon Glass4f087ba2015-10-18 21:17:20 -0600132 goto err;
Bin Meng3928d662015-08-24 01:00:04 -0700133
Bin Meng7d961662015-08-24 01:00:06 -0700134 /* keyboard reset */
Simon Glass31d38ee2015-10-18 21:17:19 -0600135 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)
Simon Glass4f087ba2015-10-18 21:17:20 -0600138 goto err;
Bin Meng3928d662015-08-24 01:00:04 -0700139
Simon Glass8226a3e2016-03-11 22:06:50 -0700140 if (kbd_write(I8042_DATA_REG, CMD_DRAIN_OUTPUT) ||
141 kbd_read(I8042_DATA_REG) != KBD_ACK)
142 goto err;
143
Bin Meng7d961662015-08-24 01:00:06 -0700144 /* set AT translation and disable irq */
Simon Glass31d38ee2015-10-18 21:17:19 -0600145 config = kbd_cmd_read(CMD_RD_CONFIG);
146 if (config == -1)
Simon Glass4f087ba2015-10-18 21:17:20 -0600147 goto err;
Simon Glass31d38ee2015-10-18 21:17:19 -0600148
Simon Glass011d89d2015-11-11 10:05:46 -0700149 /* Sometimes get a second byte */
150 else if ((quirk & QUIRK_DUP_POR) && config == KBD_POR)
151 config = kbd_cmd_read(CMD_RD_CONFIG);
152
Tom Rinib21f9652021-08-19 14:58:00 -0400153 config |= CFG_AT_TRANS;
154 config &= ~(CFG_KIRQ_EN | CFG_MIRQ_EN);
Simon Glass31d38ee2015-10-18 21:17:19 -0600155 if (kbd_cmd_write(CMD_WR_CONFIG, config))
Simon Glass4f087ba2015-10-18 21:17:20 -0600156 goto err;
Bin Meng3928d662015-08-24 01:00:04 -0700157
Bin Meng7d961662015-08-24 01:00:06 -0700158 /* enable keyboard */
Simon Glass31d38ee2015-10-18 21:17:19 -0600159 if (kbd_write(I8042_CMD_REG, CMD_KBD_EN) ||
160 !kbd_input_empty())
Simon Glass4f087ba2015-10-18 21:17:20 -0600161 goto err;
Bin Meng3928d662015-08-24 01:00:04 -0700162
Bin Meng3928d662015-08-24 01:00:04 -0700163 return 0;
Simon Glass4f087ba2015-10-18 21:17:20 -0600164err:
165 debug("%s: Keyboard failure\n", __func__);
166 return -1;
Bin Meng3928d662015-08-24 01:00:04 -0700167}
168
Gabe Black22e0f5a2011-11-14 20:18:12 +0000169static int kbd_controller_present(void)
170{
Bin Meng835dd002015-08-24 01:00:05 -0700171 return in8(I8042_STS_REG) != 0xff;
Gabe Black22e0f5a2011-11-14 20:18:12 +0000172}
173
Simon Glass165be502018-11-23 21:29:38 -0700174/** Flush all buffer from keyboard controller to host*/
175static void i8042_flush(void)
Louis Yung-Chieh Lo45fe6682012-10-11 15:15:51 +0000176{
177 int timeout;
178
179 /*
Bin Meng835dd002015-08-24 01:00:05 -0700180 * The delay is to give the keyboard controller some time
181 * to fill the next byte.
Louis Yung-Chieh Lo45fe6682012-10-11 15:15:51 +0000182 */
183 while (1) {
Bin Meng835dd002015-08-24 01:00:05 -0700184 timeout = 100; /* wait for no longer than 100us */
185 while (timeout > 0 && !(in8(I8042_STS_REG) & STATUS_OBF)) {
Louis Yung-Chieh Lo45fe6682012-10-11 15:15:51 +0000186 udelay(1);
187 timeout--;
188 }
189
Bin Meng835dd002015-08-24 01:00:05 -0700190 /* Try to pull next byte if not timeout */
191 if (in8(I8042_STS_REG) & STATUS_OBF)
Louis Yung-Chieh Lo45fe6682012-10-11 15:15:51 +0000192 in8(I8042_DATA_REG);
193 else
194 break;
195 }
196}
197
Simon Glass165be502018-11-23 21:29:38 -0700198/**
199 * Disables the keyboard so that key strokes no longer generate scancodes to
200 * the host.
201 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100202 * Return: 0 if ok, -1 if keyboard input was found while disabling
Simon Glass165be502018-11-23 21:29:38 -0700203 */
204static int i8042_disable(void)
Louis Yung-Chieh Lo45fe6682012-10-11 15:15:51 +0000205{
206 if (kbd_input_empty() == 0)
207 return -1;
208
209 /* Disable keyboard */
Bin Meng835dd002015-08-24 01:00:05 -0700210 out8(I8042_CMD_REG, CMD_KBD_DIS);
Louis Yung-Chieh Lo45fe6682012-10-11 15:15:51 +0000211
212 if (kbd_input_empty() == 0)
213 return -1;
214
215 return 0;
216}
217
Simon Glass2ec739d2015-11-11 10:05:41 -0700218static int i8042_kbd_check(struct input_config *input)
219{
Simon Glassdcbf8252015-11-11 10:05:45 -0700220 struct i8042_kbd_priv *priv = dev_get_priv(input->dev);
221
Simon Glass2ec739d2015-11-11 10:05:41 -0700222 if ((in8(I8042_STS_REG) & STATUS_OBF) == 0) {
223 return 0;
224 } else {
225 bool release = false;
226 int scan_code;
227 int i;
228
229 scan_code = in8(I8042_DATA_REG);
230 if (scan_code == 0xfa) {
231 return 0;
232 } else if (scan_code == 0xe0) {
Simon Glassdcbf8252015-11-11 10:05:45 -0700233 priv->extended = true;
Simon Glass2ec739d2015-11-11 10:05:41 -0700234 return 0;
235 }
236 if (scan_code & 0x80) {
237 scan_code &= 0x7f;
238 release = true;
239 }
Simon Glassdcbf8252015-11-11 10:05:45 -0700240 if (priv->extended) {
241 priv->extended = false;
Simon Glass2ec739d2015-11-11 10:05:41 -0700242 for (i = 0; ext_key_map[i]; i++) {
243 if (ext_key_map[i] == scan_code) {
244 scan_code = 0x60 + i;
245 break;
246 }
247 }
248 /* not found ? */
249 if (!ext_key_map[i])
250 return 0;
251 }
252
Simon Glassdcbf8252015-11-11 10:05:45 -0700253 input_add_keycode(input, scan_code, release);
Simon Glass2ec739d2015-11-11 10:05:41 -0700254 return 1;
255 }
256}
257
Bin Meng835dd002015-08-24 01:00:05 -0700258/* i8042_kbd_init - reset keyboard and init state flags */
Simon Glassdcbf8252015-11-11 10:05:45 -0700259static int i8042_start(struct udevice *dev)
wdenkc6097192002-11-03 00:24:07 +0000260{
Simon Glassdcbf8252015-11-11 10:05:45 -0700261 struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass011d89d2015-11-11 10:05:46 -0700262 struct i8042_kbd_priv *priv = dev_get_priv(dev);
Simon Glassdcbf8252015-11-11 10:05:45 -0700263 struct input_config *input = &uc_priv->input;
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000264 int keymap, try;
265 char *penv;
Simon Glass2ec739d2015-11-11 10:05:41 -0700266 int ret;
wdenkc6097192002-11-03 00:24:07 +0000267
Simon Glass165be502018-11-23 21:29:38 -0700268 if (!kbd_controller_present()) {
Bin Meng835dd002015-08-24 01:00:05 -0700269 debug("i8042 keyboard controller is not present\n");
Simon Glassdcbf8252015-11-11 10:05:45 -0700270 return -ENOENT;
Bin Meng835dd002015-08-24 01:00:05 -0700271 }
Gabe Black22e0f5a2011-11-14 20:18:12 +0000272
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000273 /* Init keyboard device (default US layout) */
274 keymap = KBD_US;
Simon Glass00caae62017-08-03 12:22:12 -0600275 penv = env_get("keymap");
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000276 if (penv != NULL) {
277 if (strncmp(penv, "de", 3) == 0)
278 keymap = KBD_GER;
279 }
wdenkc6097192002-11-03 00:24:07 +0000280
Simon Glass011d89d2015-11-11 10:05:46 -0700281 for (try = 0; kbd_reset(priv->quirks) != 0; try++) {
Simon Glassc5d257f2015-10-18 21:17:21 -0600282 if (try >= KBD_RESET_TRIES)
283 return -1;
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000284 }
Bin Meng835dd002015-08-24 01:00:05 -0700285
Simon Glassdcbf8252015-11-11 10:05:45 -0700286 ret = input_add_tables(input, keymap == KBD_GER);
Simon Glass2ec739d2015-11-11 10:05:41 -0700287 if (ret)
288 return ret;
Simon Glass2ec739d2015-11-11 10:05:41 -0700289
Simon Glassdcbf8252015-11-11 10:05:45 -0700290 i8042_kbd_update_leds(dev, NORMAL);
291 debug("%s: started\n", __func__);
Simon Glassc5d257f2015-10-18 21:17:21 -0600292
293 return 0;
wdenkc6097192002-11-03 00:24:07 +0000294}
295
Simon Glass165be502018-11-23 21:29:38 -0700296static int i8042_kbd_remove(struct udevice *dev)
297{
298 if (i8042_disable())
299 log_debug("i8042_disable() failed. fine, continue.\n");
300 i8042_flush();
301
302 return 0;
303}
304
Simon Glass2ec739d2015-11-11 10:05:41 -0700305/**
Simon Glassdcbf8252015-11-11 10:05:45 -0700306 * Set up the i8042 keyboard. This is called by the stdio device handler
Bin Meng835dd002015-08-24 01:00:05 -0700307 *
Simon Glassdcbf8252015-11-11 10:05:45 -0700308 * 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.
310 *
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().
314 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100315 * Return: 0 if ok, -ve on error
wdenkc6097192002-11-03 00:24:07 +0000316 */
Simon Glassdcbf8252015-11-11 10:05:45 -0700317static int i8042_kbd_probe(struct udevice *dev)
Simon Glass2ec739d2015-11-11 10:05:41 -0700318{
Simon Glassdcbf8252015-11-11 10:05:45 -0700319 struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass011d89d2015-11-11 10:05:46 -0700320 struct i8042_kbd_priv *priv = dev_get_priv(dev);
Simon Glassdcbf8252015-11-11 10:05:45 -0700321 struct stdio_dev *sdev = &uc_priv->sdev;
322 struct input_config *input = &uc_priv->input;
323 int ret;
Simon Glass2ec739d2015-11-11 10:05:41 -0700324
Simon Glasse160f7d2017-01-17 16:52:55 -0700325 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
Simon Glass011d89d2015-11-11 10:05:46 -0700326 "intel,duplicate-por"))
327 priv->quirks |= QUIRK_DUP_POR;
328
Simon Glassdcbf8252015-11-11 10:05:45 -0700329 /* Register the device. i8042_start() will be called soon */
330 input->dev = dev;
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);
335 if (ret) {
336 debug("%s: input_stdio_register() failed\n", __func__);
337 return ret;
338 }
339 debug("%s: ready\n", __func__);
Simon Glass2ec739d2015-11-11 10:05:41 -0700340
Simon Glassdcbf8252015-11-11 10:05:45 -0700341 return 0;
Simon Glass2ec739d2015-11-11 10:05:41 -0700342}
343
Simon Glassdcbf8252015-11-11 10:05:45 -0700344static const struct keyboard_ops i8042_kbd_ops = {
345 .start = i8042_start,
346 .update_leds = i8042_kbd_update_leds,
347};
wdenkc6097192002-11-03 00:24:07 +0000348
Simon Glassdcbf8252015-11-11 10:05:45 -0700349static const struct udevice_id i8042_kbd_ids[] = {
350 { .compatible = "intel,i8042-keyboard" },
351 { }
352};
353
354U_BOOT_DRIVER(i8042_kbd) = {
355 .name = "i8042_kbd",
356 .id = UCLASS_KEYBOARD,
357 .of_match = i8042_kbd_ids,
358 .probe = i8042_kbd_probe,
Simon Glass165be502018-11-23 21:29:38 -0700359 .remove = i8042_kbd_remove,
Simon Glassdcbf8252015-11-11 10:05:45 -0700360 .ops = &i8042_kbd_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700361 .priv_auto = sizeof(struct i8042_kbd_priv),
Simon Glassdcbf8252015-11-11 10:05:45 -0700362};