blob: e6070ca015296b29d618e31fb123669786f2d5e8 [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
Simon Glassd8062e92023-05-04 16:54:56 -06009#define LOG_CATEGORY UCLASS_KEYBOARD
10
wdenkc6097192002-11-03 00:24:07 +000011#include <common.h>
Simon Glassdcbf8252015-11-11 10:05:45 -070012#include <dm.h>
Simon Glass7b51b572019-08-01 09:46:52 -060013#include <env.h>
Simon Glassdcbf8252015-11-11 10:05:45 -070014#include <errno.h>
wdenkc6097192002-11-03 00:24:07 +000015#include <i8042.h>
Simon Glass2ec739d2015-11-11 10:05:41 -070016#include <input.h>
Simon Glassdcbf8252015-11-11 10:05:45 -070017#include <keyboard.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060018#include <log.h>
Simon Glass401d1c42020-10-30 21:38:53 -060019#include <asm/global_data.h>
Simon Glass2ec739d2015-11-11 10:05:41 -070020#include <asm/io.h>
Simon Glassc05ed002020-05-10 11:40:11 -060021#include <linux/delay.h>
wdenkc6097192002-11-03 00:24:07 +000022
Simon Glass011d89d2015-11-11 10:05:46 -070023DECLARE_GLOBAL_DATA_PTR;
24
wdenkc6097192002-11-03 00:24:07 +000025/* defines */
Bin Meng835dd002015-08-24 01:00:05 -070026#define in8(p) inb(p)
27#define out8(p, v) outb(v, p)
wdenkc6097192002-11-03 00:24:07 +000028
Simon Glass011d89d2015-11-11 10:05:46 -070029enum {
30 QUIRK_DUP_POR = 1 << 0,
31};
32
wdenkc6097192002-11-03 00:24:07 +000033/* locals */
Simon Glassdcbf8252015-11-11 10:05:45 -070034struct i8042_kbd_priv {
35 bool extended; /* true if an extended keycode is expected next */
Simon Glass011d89d2015-11-11 10:05:46 -070036 int quirks; /* quirks that we support */
Simon Glassdcbf8252015-11-11 10:05:45 -070037};
wdenkc6097192002-11-03 00:24:07 +000038
Gabe Blackdd4a5b22011-11-14 19:24:14 +000039static unsigned char ext_key_map[] = {
40 0x1c, /* keypad enter */
41 0x1d, /* right control */
42 0x35, /* keypad slash */
43 0x37, /* print screen */
44 0x38, /* right alt */
45 0x46, /* break */
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 */
56 0x00 /* map end */
57 };
wdenkc6097192002-11-03 00:24:07 +000058
Simon Glassd8062e92023-05-04 16:54:56 -060059/**
60 * kbd_input_empty() - Wait until the keyboard is ready for a command
61 *
62 * Checks the IBF flag (input buffer full), waiting for it to indicate that
63 * any previous command has been processed.
64 *
65 * Return: true if ready, false if it timed out
66 */
Bin Meng3928d662015-08-24 01:00:04 -070067static int kbd_input_empty(void)
68{
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_IBF) && 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 Glassd8062e92023-05-04 16:54:56 -060077/**
78 * kbd_output_full() - Wait until the keyboard has data available
79 *
80 * Checks the OBF flag (output buffer full), waiting for it to indicate that
81 * a response to a previous command is available
82 */
Bin Meng835dd002015-08-24 01:00:05 -070083static int kbd_output_full(void)
Bin Meng3928d662015-08-24 01:00:04 -070084{
Bin Meng835dd002015-08-24 01:00:05 -070085 int kbd_timeout = KBD_TIMEOUT * 1000;
Bin Meng3928d662015-08-24 01:00:04 -070086
Bin Meng835dd002015-08-24 01:00:05 -070087 while (((in8(I8042_STS_REG) & STATUS_OBF) == 0) && kbd_timeout--)
Bin Meng3928d662015-08-24 01:00:04 -070088 udelay(1);
89
Bin Meng835dd002015-08-24 01:00:05 -070090 return kbd_timeout != -1;
Bin Meng3928d662015-08-24 01:00:04 -070091}
92
Simon Glassdcbf8252015-11-11 10:05:45 -070093/**
94 * check_leds() - Check the keyboard LEDs and update them it needed
95 *
96 * @ret: Value to return
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010097 * Return: value of @ret
Simon Glassdcbf8252015-11-11 10:05:45 -070098 */
99static int i8042_kbd_update_leds(struct udevice *dev, int leds)
Bin Meng3928d662015-08-24 01:00:04 -0700100{
101 kbd_input_empty();
Bin Meng835dd002015-08-24 01:00:05 -0700102 out8(I8042_DATA_REG, CMD_SET_KBD_LED);
Bin Meng3928d662015-08-24 01:00:04 -0700103 kbd_input_empty();
Simon Glassdcbf8252015-11-11 10:05:45 -0700104 out8(I8042_DATA_REG, leds & 0x7);
105
106 return 0;
Bin Meng3928d662015-08-24 01:00:04 -0700107}
108
Simon Glass31d38ee2015-10-18 21:17:19 -0600109static int kbd_write(int reg, int value)
110{
111 if (!kbd_input_empty())
112 return -1;
113 out8(reg, value);
114
115 return 0;
116}
117
118static int kbd_read(int reg)
119{
120 if (!kbd_output_full())
121 return -1;
122
123 return in8(reg);
124}
125
126static int kbd_cmd_read(int cmd)
127{
128 if (kbd_write(I8042_CMD_REG, cmd))
129 return -1;
130
131 return kbd_read(I8042_DATA_REG);
132}
133
134static int kbd_cmd_write(int cmd, int data)
135{
136 if (kbd_write(I8042_CMD_REG, cmd))
137 return -1;
138
139 return kbd_write(I8042_DATA_REG, data);
140}
141
Simon Glass011d89d2015-11-11 10:05:46 -0700142static int kbd_reset(int quirk)
Bin Meng3928d662015-08-24 01:00:04 -0700143{
Simon Glass31d38ee2015-10-18 21:17:19 -0600144 int config;
Bin Meng7d961662015-08-24 01:00:06 -0700145
Simon Glassd8062e92023-05-04 16:54:56 -0600146 if (!kbd_input_empty())
147 goto err;
148
Bin Meng7d961662015-08-24 01:00:06 -0700149 /* controller self test */
Simon Glass31d38ee2015-10-18 21:17:19 -0600150 if (kbd_cmd_read(CMD_SELF_TEST) != KBC_TEST_OK)
Simon Glass4f087ba2015-10-18 21:17:20 -0600151 goto err;
Bin Meng3928d662015-08-24 01:00:04 -0700152
Bin Meng7d961662015-08-24 01:00:06 -0700153 /* keyboard reset */
Simon Glass31d38ee2015-10-18 21:17:19 -0600154 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)
Simon Glass4f087ba2015-10-18 21:17:20 -0600157 goto err;
Bin Meng3928d662015-08-24 01:00:04 -0700158
Simon Glass8226a3e2016-03-11 22:06:50 -0700159 if (kbd_write(I8042_DATA_REG, CMD_DRAIN_OUTPUT) ||
160 kbd_read(I8042_DATA_REG) != KBD_ACK)
161 goto err;
162
Bin Meng7d961662015-08-24 01:00:06 -0700163 /* set AT translation and disable irq */
Simon Glass31d38ee2015-10-18 21:17:19 -0600164 config = kbd_cmd_read(CMD_RD_CONFIG);
165 if (config == -1)
Simon Glass4f087ba2015-10-18 21:17:20 -0600166 goto err;
Simon Glass31d38ee2015-10-18 21:17:19 -0600167
Simon Glass011d89d2015-11-11 10:05:46 -0700168 /* Sometimes get a second byte */
169 else if ((quirk & QUIRK_DUP_POR) && config == KBD_POR)
170 config = kbd_cmd_read(CMD_RD_CONFIG);
171
Tom Rinib21f9652021-08-19 14:58:00 -0400172 config |= CFG_AT_TRANS;
173 config &= ~(CFG_KIRQ_EN | CFG_MIRQ_EN);
Simon Glass31d38ee2015-10-18 21:17:19 -0600174 if (kbd_cmd_write(CMD_WR_CONFIG, config))
Simon Glass4f087ba2015-10-18 21:17:20 -0600175 goto err;
Bin Meng3928d662015-08-24 01:00:04 -0700176
Bin Meng7d961662015-08-24 01:00:06 -0700177 /* enable keyboard */
Simon Glass31d38ee2015-10-18 21:17:19 -0600178 if (kbd_write(I8042_CMD_REG, CMD_KBD_EN) ||
179 !kbd_input_empty())
Simon Glass4f087ba2015-10-18 21:17:20 -0600180 goto err;
Bin Meng3928d662015-08-24 01:00:04 -0700181
Bin Meng3928d662015-08-24 01:00:04 -0700182 return 0;
Simon Glass4f087ba2015-10-18 21:17:20 -0600183err:
184 debug("%s: Keyboard failure\n", __func__);
185 return -1;
Bin Meng3928d662015-08-24 01:00:04 -0700186}
187
Gabe Black22e0f5a2011-11-14 20:18:12 +0000188static int kbd_controller_present(void)
189{
Bin Meng835dd002015-08-24 01:00:05 -0700190 return in8(I8042_STS_REG) != 0xff;
Gabe Black22e0f5a2011-11-14 20:18:12 +0000191}
192
Simon Glass165be502018-11-23 21:29:38 -0700193/** Flush all buffer from keyboard controller to host*/
194static void i8042_flush(void)
Louis Yung-Chieh Lo45fe6682012-10-11 15:15:51 +0000195{
196 int timeout;
197
198 /*
Bin Meng835dd002015-08-24 01:00:05 -0700199 * The delay is to give the keyboard controller some time
200 * to fill the next byte.
Louis Yung-Chieh Lo45fe6682012-10-11 15:15:51 +0000201 */
202 while (1) {
Bin Meng835dd002015-08-24 01:00:05 -0700203 timeout = 100; /* wait for no longer than 100us */
204 while (timeout > 0 && !(in8(I8042_STS_REG) & STATUS_OBF)) {
Louis Yung-Chieh Lo45fe6682012-10-11 15:15:51 +0000205 udelay(1);
206 timeout--;
207 }
208
Bin Meng835dd002015-08-24 01:00:05 -0700209 /* Try to pull next byte if not timeout */
210 if (in8(I8042_STS_REG) & STATUS_OBF)
Louis Yung-Chieh Lo45fe6682012-10-11 15:15:51 +0000211 in8(I8042_DATA_REG);
212 else
213 break;
214 }
215}
216
Simon Glass165be502018-11-23 21:29:38 -0700217/**
218 * Disables the keyboard so that key strokes no longer generate scancodes to
219 * the host.
220 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100221 * Return: 0 if ok, -1 if keyboard input was found while disabling
Simon Glass165be502018-11-23 21:29:38 -0700222 */
223static int i8042_disable(void)
Louis Yung-Chieh Lo45fe6682012-10-11 15:15:51 +0000224{
225 if (kbd_input_empty() == 0)
226 return -1;
227
228 /* Disable keyboard */
Bin Meng835dd002015-08-24 01:00:05 -0700229 out8(I8042_CMD_REG, CMD_KBD_DIS);
Louis Yung-Chieh Lo45fe6682012-10-11 15:15:51 +0000230
231 if (kbd_input_empty() == 0)
232 return -1;
233
234 return 0;
235}
236
Simon Glass2ec739d2015-11-11 10:05:41 -0700237static int i8042_kbd_check(struct input_config *input)
238{
Simon Glassdcbf8252015-11-11 10:05:45 -0700239 struct i8042_kbd_priv *priv = dev_get_priv(input->dev);
240
Simon Glass2ec739d2015-11-11 10:05:41 -0700241 if ((in8(I8042_STS_REG) & STATUS_OBF) == 0) {
242 return 0;
243 } else {
244 bool release = false;
245 int scan_code;
246 int i;
247
248 scan_code = in8(I8042_DATA_REG);
249 if (scan_code == 0xfa) {
250 return 0;
251 } else if (scan_code == 0xe0) {
Simon Glassdcbf8252015-11-11 10:05:45 -0700252 priv->extended = true;
Simon Glass2ec739d2015-11-11 10:05:41 -0700253 return 0;
254 }
255 if (scan_code & 0x80) {
256 scan_code &= 0x7f;
257 release = true;
258 }
Simon Glassdcbf8252015-11-11 10:05:45 -0700259 if (priv->extended) {
260 priv->extended = false;
Simon Glass2ec739d2015-11-11 10:05:41 -0700261 for (i = 0; ext_key_map[i]; i++) {
262 if (ext_key_map[i] == scan_code) {
263 scan_code = 0x60 + i;
264 break;
265 }
266 }
267 /* not found ? */
268 if (!ext_key_map[i])
269 return 0;
270 }
271
Simon Glassdcbf8252015-11-11 10:05:45 -0700272 input_add_keycode(input, scan_code, release);
Simon Glass2ec739d2015-11-11 10:05:41 -0700273 return 1;
274 }
275}
276
Bin Meng835dd002015-08-24 01:00:05 -0700277/* i8042_kbd_init - reset keyboard and init state flags */
Simon Glassdcbf8252015-11-11 10:05:45 -0700278static int i8042_start(struct udevice *dev)
wdenkc6097192002-11-03 00:24:07 +0000279{
Simon Glassdcbf8252015-11-11 10:05:45 -0700280 struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass011d89d2015-11-11 10:05:46 -0700281 struct i8042_kbd_priv *priv = dev_get_priv(dev);
Simon Glassdcbf8252015-11-11 10:05:45 -0700282 struct input_config *input = &uc_priv->input;
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000283 int keymap, try;
284 char *penv;
Simon Glass2ec739d2015-11-11 10:05:41 -0700285 int ret;
wdenkc6097192002-11-03 00:24:07 +0000286
Simon Glass165be502018-11-23 21:29:38 -0700287 if (!kbd_controller_present()) {
Bin Meng835dd002015-08-24 01:00:05 -0700288 debug("i8042 keyboard controller is not present\n");
Simon Glassdcbf8252015-11-11 10:05:45 -0700289 return -ENOENT;
Bin Meng835dd002015-08-24 01:00:05 -0700290 }
Gabe Black22e0f5a2011-11-14 20:18:12 +0000291
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000292 /* Init keyboard device (default US layout) */
293 keymap = KBD_US;
Simon Glass00caae62017-08-03 12:22:12 -0600294 penv = env_get("keymap");
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000295 if (penv != NULL) {
296 if (strncmp(penv, "de", 3) == 0)
297 keymap = KBD_GER;
298 }
wdenkc6097192002-11-03 00:24:07 +0000299
Simon Glass011d89d2015-11-11 10:05:46 -0700300 for (try = 0; kbd_reset(priv->quirks) != 0; try++) {
Simon Glassc5d257f2015-10-18 21:17:21 -0600301 if (try >= KBD_RESET_TRIES)
302 return -1;
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000303 }
Bin Meng835dd002015-08-24 01:00:05 -0700304
Simon Glassdcbf8252015-11-11 10:05:45 -0700305 ret = input_add_tables(input, keymap == KBD_GER);
Simon Glass2ec739d2015-11-11 10:05:41 -0700306 if (ret)
307 return ret;
Simon Glass2ec739d2015-11-11 10:05:41 -0700308
Simon Glassdcbf8252015-11-11 10:05:45 -0700309 i8042_kbd_update_leds(dev, NORMAL);
310 debug("%s: started\n", __func__);
Simon Glassc5d257f2015-10-18 21:17:21 -0600311
312 return 0;
wdenkc6097192002-11-03 00:24:07 +0000313}
314
Simon Glass165be502018-11-23 21:29:38 -0700315static int i8042_kbd_remove(struct udevice *dev)
316{
317 if (i8042_disable())
318 log_debug("i8042_disable() failed. fine, continue.\n");
319 i8042_flush();
320
321 return 0;
322}
323
Simon Glass2ec739d2015-11-11 10:05:41 -0700324/**
Simon Glassdcbf8252015-11-11 10:05:45 -0700325 * Set up the i8042 keyboard. This is called by the stdio device handler
Bin Meng835dd002015-08-24 01:00:05 -0700326 *
Simon Glassdcbf8252015-11-11 10:05:45 -0700327 * 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.
329 *
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().
333 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100334 * Return: 0 if ok, -ve on error
wdenkc6097192002-11-03 00:24:07 +0000335 */
Simon Glassdcbf8252015-11-11 10:05:45 -0700336static int i8042_kbd_probe(struct udevice *dev)
Simon Glass2ec739d2015-11-11 10:05:41 -0700337{
Simon Glassdcbf8252015-11-11 10:05:45 -0700338 struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass011d89d2015-11-11 10:05:46 -0700339 struct i8042_kbd_priv *priv = dev_get_priv(dev);
Simon Glassdcbf8252015-11-11 10:05:45 -0700340 struct stdio_dev *sdev = &uc_priv->sdev;
341 struct input_config *input = &uc_priv->input;
342 int ret;
Simon Glass2ec739d2015-11-11 10:05:41 -0700343
Simon Glasse160f7d2017-01-17 16:52:55 -0700344 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
Simon Glass011d89d2015-11-11 10:05:46 -0700345 "intel,duplicate-por"))
346 priv->quirks |= QUIRK_DUP_POR;
347
Simon Glassdcbf8252015-11-11 10:05:45 -0700348 /* Register the device. i8042_start() will be called soon */
349 input->dev = dev;
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);
354 if (ret) {
355 debug("%s: input_stdio_register() failed\n", __func__);
356 return ret;
357 }
358 debug("%s: ready\n", __func__);
Simon Glass2ec739d2015-11-11 10:05:41 -0700359
Simon Glassdcbf8252015-11-11 10:05:45 -0700360 return 0;
Simon Glass2ec739d2015-11-11 10:05:41 -0700361}
362
Simon Glassdcbf8252015-11-11 10:05:45 -0700363static const struct keyboard_ops i8042_kbd_ops = {
364 .start = i8042_start,
365 .update_leds = i8042_kbd_update_leds,
366};
wdenkc6097192002-11-03 00:24:07 +0000367
Simon Glassdcbf8252015-11-11 10:05:45 -0700368static const struct udevice_id i8042_kbd_ids[] = {
369 { .compatible = "intel,i8042-keyboard" },
370 { }
371};
372
373U_BOOT_DRIVER(i8042_kbd) = {
374 .name = "i8042_kbd",
375 .id = UCLASS_KEYBOARD,
376 .of_match = i8042_kbd_ids,
377 .probe = i8042_kbd_probe,
Simon Glass165be502018-11-23 21:29:38 -0700378 .remove = i8042_kbd_remove,
Simon Glassdcbf8252015-11-11 10:05:45 -0700379 .ops = &i8042_kbd_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700380 .priv_auto = sizeof(struct i8042_kbd_priv),
Simon Glassdcbf8252015-11-11 10:05:45 -0700381};