blob: 5678f8e3cfb1b98e41e881b81692d33a6bc7d50e [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>
11#include <errno.h>
wdenkc6097192002-11-03 00:24:07 +000012#include <i8042.h>
Simon Glass2ec739d2015-11-11 10:05:41 -070013#include <input.h>
Simon Glassdcbf8252015-11-11 10:05:45 -070014#include <keyboard.h>
Simon Glass2ec739d2015-11-11 10:05:41 -070015#include <asm/io.h>
wdenkc6097192002-11-03 00:24:07 +000016
Simon Glass011d89d2015-11-11 10:05:46 -070017DECLARE_GLOBAL_DATA_PTR;
18
wdenkc6097192002-11-03 00:24:07 +000019/* defines */
Bin Meng835dd002015-08-24 01:00:05 -070020#define in8(p) inb(p)
21#define out8(p, v) outb(v, p)
wdenkc6097192002-11-03 00:24:07 +000022
Simon Glass011d89d2015-11-11 10:05:46 -070023enum {
24 QUIRK_DUP_POR = 1 << 0,
25};
26
wdenkc6097192002-11-03 00:24:07 +000027/* locals */
Simon Glassdcbf8252015-11-11 10:05:45 -070028struct i8042_kbd_priv {
29 bool extended; /* true if an extended keycode is expected next */
Simon Glass011d89d2015-11-11 10:05:46 -070030 int quirks; /* quirks that we support */
Simon Glassdcbf8252015-11-11 10:05:45 -070031};
wdenkc6097192002-11-03 00:24:07 +000032
Gabe Blackdd4a5b22011-11-14 19:24:14 +000033static unsigned char ext_key_map[] = {
34 0x1c, /* keypad enter */
35 0x1d, /* right control */
36 0x35, /* keypad slash */
37 0x37, /* print screen */
38 0x38, /* right alt */
39 0x46, /* break */
40 0x47, /* editpad home */
41 0x48, /* editpad up */
42 0x49, /* editpad pgup */
43 0x4b, /* editpad left */
44 0x4d, /* editpad right */
45 0x4f, /* editpad end */
46 0x50, /* editpad dn */
47 0x51, /* editpad pgdn */
48 0x52, /* editpad ins */
49 0x53, /* editpad del */
50 0x00 /* map end */
51 };
wdenkc6097192002-11-03 00:24:07 +000052
Bin Meng3928d662015-08-24 01:00:04 -070053static int kbd_input_empty(void)
54{
Bin Meng835dd002015-08-24 01:00:05 -070055 int kbd_timeout = KBD_TIMEOUT * 1000;
Bin Meng3928d662015-08-24 01:00:04 -070056
Bin Meng835dd002015-08-24 01:00:05 -070057 while ((in8(I8042_STS_REG) & STATUS_IBF) && kbd_timeout--)
Bin Meng3928d662015-08-24 01:00:04 -070058 udelay(1);
59
Bin Meng835dd002015-08-24 01:00:05 -070060 return kbd_timeout != -1;
Bin Meng3928d662015-08-24 01:00:04 -070061}
62
Bin Meng835dd002015-08-24 01:00:05 -070063static int kbd_output_full(void)
Bin Meng3928d662015-08-24 01:00:04 -070064{
Bin Meng835dd002015-08-24 01:00:05 -070065 int kbd_timeout = KBD_TIMEOUT * 1000;
Bin Meng3928d662015-08-24 01:00:04 -070066
Bin Meng835dd002015-08-24 01:00:05 -070067 while (((in8(I8042_STS_REG) & STATUS_OBF) == 0) && kbd_timeout--)
Bin Meng3928d662015-08-24 01:00:04 -070068 udelay(1);
69
Bin Meng835dd002015-08-24 01:00:05 -070070 return kbd_timeout != -1;
Bin Meng3928d662015-08-24 01:00:04 -070071}
72
Simon Glassdcbf8252015-11-11 10:05:45 -070073/**
74 * check_leds() - Check the keyboard LEDs and update them it needed
75 *
76 * @ret: Value to return
77 * @return value of @ret
78 */
79static int i8042_kbd_update_leds(struct udevice *dev, int leds)
Bin Meng3928d662015-08-24 01:00:04 -070080{
81 kbd_input_empty();
Bin Meng835dd002015-08-24 01:00:05 -070082 out8(I8042_DATA_REG, CMD_SET_KBD_LED);
Bin Meng3928d662015-08-24 01:00:04 -070083 kbd_input_empty();
Simon Glassdcbf8252015-11-11 10:05:45 -070084 out8(I8042_DATA_REG, leds & 0x7);
85
86 return 0;
Bin Meng3928d662015-08-24 01:00:04 -070087}
88
Simon Glass31d38ee2015-10-18 21:17:19 -060089static int kbd_write(int reg, int value)
90{
91 if (!kbd_input_empty())
92 return -1;
93 out8(reg, value);
94
95 return 0;
96}
97
98static int kbd_read(int reg)
99{
100 if (!kbd_output_full())
101 return -1;
102
103 return in8(reg);
104}
105
106static int kbd_cmd_read(int cmd)
107{
108 if (kbd_write(I8042_CMD_REG, cmd))
109 return -1;
110
111 return kbd_read(I8042_DATA_REG);
112}
113
114static int kbd_cmd_write(int cmd, int data)
115{
116 if (kbd_write(I8042_CMD_REG, cmd))
117 return -1;
118
119 return kbd_write(I8042_DATA_REG, data);
120}
121
Simon Glass011d89d2015-11-11 10:05:46 -0700122static int kbd_reset(int quirk)
Bin Meng3928d662015-08-24 01:00:04 -0700123{
Simon Glass31d38ee2015-10-18 21:17:19 -0600124 int config;
Bin Meng7d961662015-08-24 01:00:06 -0700125
126 /* controller self test */
Simon Glass31d38ee2015-10-18 21:17:19 -0600127 if (kbd_cmd_read(CMD_SELF_TEST) != KBC_TEST_OK)
Simon Glass4f087ba2015-10-18 21:17:20 -0600128 goto err;
Bin Meng3928d662015-08-24 01:00:04 -0700129
Bin Meng7d961662015-08-24 01:00:06 -0700130 /* keyboard reset */
Simon Glass31d38ee2015-10-18 21:17:19 -0600131 if (kbd_write(I8042_DATA_REG, CMD_RESET_KBD) ||
132 kbd_read(I8042_DATA_REG) != KBD_ACK ||
133 kbd_read(I8042_DATA_REG) != KBD_POR)
Simon Glass4f087ba2015-10-18 21:17:20 -0600134 goto err;
Bin Meng3928d662015-08-24 01:00:04 -0700135
Simon Glass8226a3e2016-03-11 22:06:50 -0700136 if (kbd_write(I8042_DATA_REG, CMD_DRAIN_OUTPUT) ||
137 kbd_read(I8042_DATA_REG) != KBD_ACK)
138 goto err;
139
Bin Meng7d961662015-08-24 01:00:06 -0700140 /* set AT translation and disable irq */
Simon Glass31d38ee2015-10-18 21:17:19 -0600141 config = kbd_cmd_read(CMD_RD_CONFIG);
142 if (config == -1)
Simon Glass4f087ba2015-10-18 21:17:20 -0600143 goto err;
Simon Glass31d38ee2015-10-18 21:17:19 -0600144
Simon Glass011d89d2015-11-11 10:05:46 -0700145 /* Sometimes get a second byte */
146 else if ((quirk & QUIRK_DUP_POR) && config == KBD_POR)
147 config = kbd_cmd_read(CMD_RD_CONFIG);
148
Bin Meng7d961662015-08-24 01:00:06 -0700149 config |= CONFIG_AT_TRANS;
150 config &= ~(CONFIG_KIRQ_EN | CONFIG_MIRQ_EN);
Simon Glass31d38ee2015-10-18 21:17:19 -0600151 if (kbd_cmd_write(CMD_WR_CONFIG, config))
Simon Glass4f087ba2015-10-18 21:17:20 -0600152 goto err;
Bin Meng3928d662015-08-24 01:00:04 -0700153
Bin Meng7d961662015-08-24 01:00:06 -0700154 /* enable keyboard */
Simon Glass31d38ee2015-10-18 21:17:19 -0600155 if (kbd_write(I8042_CMD_REG, CMD_KBD_EN) ||
156 !kbd_input_empty())
Simon Glass4f087ba2015-10-18 21:17:20 -0600157 goto err;
Bin Meng3928d662015-08-24 01:00:04 -0700158
Bin Meng3928d662015-08-24 01:00:04 -0700159 return 0;
Simon Glass4f087ba2015-10-18 21:17:20 -0600160err:
161 debug("%s: Keyboard failure\n", __func__);
162 return -1;
Bin Meng3928d662015-08-24 01:00:04 -0700163}
164
Gabe Black22e0f5a2011-11-14 20:18:12 +0000165static int kbd_controller_present(void)
166{
Bin Meng835dd002015-08-24 01:00:05 -0700167 return in8(I8042_STS_REG) != 0xff;
Gabe Black22e0f5a2011-11-14 20:18:12 +0000168}
169
Gabe Black48edb302012-10-12 14:02:02 +0000170/*
171 * Implement a weak default function for boards that optionally
172 * need to skip the i8042 initialization.
Simon Glassdcbf8252015-11-11 10:05:45 -0700173 *
174 * TODO(sjg@chromium.org): Use device tree for this?
Gabe Black48edb302012-10-12 14:02:02 +0000175 */
176int __weak board_i8042_skip(void)
177{
178 /* As default, don't skip */
179 return 0;
180}
181
Louis Yung-Chieh Lo45fe6682012-10-11 15:15:51 +0000182void i8042_flush(void)
183{
184 int timeout;
185
186 /*
Bin Meng835dd002015-08-24 01:00:05 -0700187 * The delay is to give the keyboard controller some time
188 * to fill the next byte.
Louis Yung-Chieh Lo45fe6682012-10-11 15:15:51 +0000189 */
190 while (1) {
Bin Meng835dd002015-08-24 01:00:05 -0700191 timeout = 100; /* wait for no longer than 100us */
192 while (timeout > 0 && !(in8(I8042_STS_REG) & STATUS_OBF)) {
Louis Yung-Chieh Lo45fe6682012-10-11 15:15:51 +0000193 udelay(1);
194 timeout--;
195 }
196
Bin Meng835dd002015-08-24 01:00:05 -0700197 /* Try to pull next byte if not timeout */
198 if (in8(I8042_STS_REG) & STATUS_OBF)
Louis Yung-Chieh Lo45fe6682012-10-11 15:15:51 +0000199 in8(I8042_DATA_REG);
200 else
201 break;
202 }
203}
204
205int i8042_disable(void)
206{
207 if (kbd_input_empty() == 0)
208 return -1;
209
210 /* Disable keyboard */
Bin Meng835dd002015-08-24 01:00:05 -0700211 out8(I8042_CMD_REG, CMD_KBD_DIS);
Louis Yung-Chieh Lo45fe6682012-10-11 15:15:51 +0000212
213 if (kbd_input_empty() == 0)
214 return -1;
215
216 return 0;
217}
218
Simon Glass2ec739d2015-11-11 10:05:41 -0700219static int i8042_kbd_check(struct input_config *input)
220{
Simon Glassdcbf8252015-11-11 10:05:45 -0700221 struct i8042_kbd_priv *priv = dev_get_priv(input->dev);
222
Simon Glass2ec739d2015-11-11 10:05:41 -0700223 if ((in8(I8042_STS_REG) & STATUS_OBF) == 0) {
224 return 0;
225 } else {
226 bool release = false;
227 int scan_code;
228 int i;
229
230 scan_code = in8(I8042_DATA_REG);
231 if (scan_code == 0xfa) {
232 return 0;
233 } else if (scan_code == 0xe0) {
Simon Glassdcbf8252015-11-11 10:05:45 -0700234 priv->extended = true;
Simon Glass2ec739d2015-11-11 10:05:41 -0700235 return 0;
236 }
237 if (scan_code & 0x80) {
238 scan_code &= 0x7f;
239 release = true;
240 }
Simon Glassdcbf8252015-11-11 10:05:45 -0700241 if (priv->extended) {
242 priv->extended = false;
Simon Glass2ec739d2015-11-11 10:05:41 -0700243 for (i = 0; ext_key_map[i]; i++) {
244 if (ext_key_map[i] == scan_code) {
245 scan_code = 0x60 + i;
246 break;
247 }
248 }
249 /* not found ? */
250 if (!ext_key_map[i])
251 return 0;
252 }
253
Simon Glassdcbf8252015-11-11 10:05:45 -0700254 input_add_keycode(input, scan_code, release);
Simon Glass2ec739d2015-11-11 10:05:41 -0700255 return 1;
256 }
257}
258
Bin Meng835dd002015-08-24 01:00:05 -0700259/* i8042_kbd_init - reset keyboard and init state flags */
Simon Glassdcbf8252015-11-11 10:05:45 -0700260static int i8042_start(struct udevice *dev)
wdenkc6097192002-11-03 00:24:07 +0000261{
Simon Glassdcbf8252015-11-11 10:05:45 -0700262 struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass011d89d2015-11-11 10:05:46 -0700263 struct i8042_kbd_priv *priv = dev_get_priv(dev);
Simon Glassdcbf8252015-11-11 10:05:45 -0700264 struct input_config *input = &uc_priv->input;
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000265 int keymap, try;
266 char *penv;
Simon Glass2ec739d2015-11-11 10:05:41 -0700267 int ret;
wdenkc6097192002-11-03 00:24:07 +0000268
Bin Meng835dd002015-08-24 01:00:05 -0700269 if (!kbd_controller_present() || board_i8042_skip()) {
270 debug("i8042 keyboard controller is not present\n");
Simon Glassdcbf8252015-11-11 10:05:45 -0700271 return -ENOENT;
Bin Meng835dd002015-08-24 01:00:05 -0700272 }
Gabe Black22e0f5a2011-11-14 20:18:12 +0000273
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000274 /* Init keyboard device (default US layout) */
275 keymap = KBD_US;
Simon Glass00caae62017-08-03 12:22:12 -0600276 penv = env_get("keymap");
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000277 if (penv != NULL) {
278 if (strncmp(penv, "de", 3) == 0)
279 keymap = KBD_GER;
280 }
wdenkc6097192002-11-03 00:24:07 +0000281
Simon Glass011d89d2015-11-11 10:05:46 -0700282 for (try = 0; kbd_reset(priv->quirks) != 0; try++) {
Simon Glassc5d257f2015-10-18 21:17:21 -0600283 if (try >= KBD_RESET_TRIES)
284 return -1;
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000285 }
Bin Meng835dd002015-08-24 01:00:05 -0700286
Simon Glassdcbf8252015-11-11 10:05:45 -0700287 ret = input_add_tables(input, keymap == KBD_GER);
Simon Glass2ec739d2015-11-11 10:05:41 -0700288 if (ret)
289 return ret;
Simon Glass2ec739d2015-11-11 10:05:41 -0700290
Simon Glassdcbf8252015-11-11 10:05:45 -0700291 i8042_kbd_update_leds(dev, NORMAL);
292 debug("%s: started\n", __func__);
Simon Glassc5d257f2015-10-18 21:17:21 -0600293
294 return 0;
wdenkc6097192002-11-03 00:24:07 +0000295}
296
Simon Glass2ec739d2015-11-11 10:05:41 -0700297/**
Simon Glassdcbf8252015-11-11 10:05:45 -0700298 * Set up the i8042 keyboard. This is called by the stdio device handler
Bin Meng835dd002015-08-24 01:00:05 -0700299 *
Simon Glassdcbf8252015-11-11 10:05:45 -0700300 * We want to do this init when the keyboard is actually used rather than
301 * at start-up, since keyboard input may not currently be selected.
302 *
303 * Once the keyboard starts there will be a period during which we must
304 * wait for the keyboard to init. We do this only when a key is first
305 * read - see kbd_wait_for_fifo_init().
306 *
307 * @return 0 if ok, -ve on error
wdenkc6097192002-11-03 00:24:07 +0000308 */
Simon Glassdcbf8252015-11-11 10:05:45 -0700309static int i8042_kbd_probe(struct udevice *dev)
Simon Glass2ec739d2015-11-11 10:05:41 -0700310{
Simon Glassdcbf8252015-11-11 10:05:45 -0700311 struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass011d89d2015-11-11 10:05:46 -0700312 struct i8042_kbd_priv *priv = dev_get_priv(dev);
Simon Glassdcbf8252015-11-11 10:05:45 -0700313 struct stdio_dev *sdev = &uc_priv->sdev;
314 struct input_config *input = &uc_priv->input;
315 int ret;
Simon Glass2ec739d2015-11-11 10:05:41 -0700316
Simon Glasse160f7d2017-01-17 16:52:55 -0700317 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
Simon Glass011d89d2015-11-11 10:05:46 -0700318 "intel,duplicate-por"))
319 priv->quirks |= QUIRK_DUP_POR;
320
Simon Glassdcbf8252015-11-11 10:05:45 -0700321 /* Register the device. i8042_start() will be called soon */
322 input->dev = dev;
323 input->read_keys = i8042_kbd_check;
324 input_allow_repeats(input, true);
325 strcpy(sdev->name, "i8042-kbd");
326 ret = input_stdio_register(sdev);
327 if (ret) {
328 debug("%s: input_stdio_register() failed\n", __func__);
329 return ret;
330 }
331 debug("%s: ready\n", __func__);
Simon Glass2ec739d2015-11-11 10:05:41 -0700332
Simon Glassdcbf8252015-11-11 10:05:45 -0700333 return 0;
Simon Glass2ec739d2015-11-11 10:05:41 -0700334}
335
Simon Glassdcbf8252015-11-11 10:05:45 -0700336static const struct keyboard_ops i8042_kbd_ops = {
337 .start = i8042_start,
338 .update_leds = i8042_kbd_update_leds,
339};
wdenkc6097192002-11-03 00:24:07 +0000340
Simon Glassdcbf8252015-11-11 10:05:45 -0700341static const struct udevice_id i8042_kbd_ids[] = {
342 { .compatible = "intel,i8042-keyboard" },
343 { }
344};
345
346U_BOOT_DRIVER(i8042_kbd) = {
347 .name = "i8042_kbd",
348 .id = UCLASS_KEYBOARD,
349 .of_match = i8042_kbd_ids,
350 .probe = i8042_kbd_probe,
351 .ops = &i8042_kbd_ops,
352 .priv_auto_alloc_size = sizeof(struct i8042_kbd_priv),
353};