blob: 661d7fd86c44a90f95ee9aba7cd0295852fb2a0e [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * (C) Copyright 2002 ELTEC Elektronik AG
3 * Frank Gottschling <fgottschling@eltec.de>
4 *
Wolfgang Denk3765b3e2013-10-07 13:07:26 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00006 */
7
8/* i8042.c - Intel 8042 keyboard driver routines */
9
wdenkc6097192002-11-03 00:24:07 +000010#include <common.h>
Simon Glassdcbf8252015-11-11 10:05:45 -070011#include <dm.h>
12#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 Glass2ec739d2015-11-11 10:05:41 -070016#include <asm/io.h>
wdenkc6097192002-11-03 00:24:07 +000017
Simon Glass011d89d2015-11-11 10:05:46 -070018DECLARE_GLOBAL_DATA_PTR;
19
wdenkc6097192002-11-03 00:24:07 +000020/* defines */
Bin Meng835dd002015-08-24 01:00:05 -070021#define in8(p) inb(p)
22#define out8(p, v) outb(v, p)
wdenkc6097192002-11-03 00:24:07 +000023
Simon Glass011d89d2015-11-11 10:05:46 -070024enum {
25 QUIRK_DUP_POR = 1 << 0,
26};
27
wdenkc6097192002-11-03 00:24:07 +000028/* locals */
Simon Glassdcbf8252015-11-11 10:05:45 -070029struct i8042_kbd_priv {
30 bool extended; /* true if an extended keycode is expected next */
Simon Glass011d89d2015-11-11 10:05:46 -070031 int quirks; /* quirks that we support */
Simon Glassdcbf8252015-11-11 10:05:45 -070032};
wdenkc6097192002-11-03 00:24:07 +000033
Gabe Blackdd4a5b22011-11-14 19:24:14 +000034static unsigned char ext_key_map[] = {
35 0x1c, /* keypad enter */
36 0x1d, /* right control */
37 0x35, /* keypad slash */
38 0x37, /* print screen */
39 0x38, /* right alt */
40 0x46, /* break */
41 0x47, /* editpad home */
42 0x48, /* editpad up */
43 0x49, /* editpad pgup */
44 0x4b, /* editpad left */
45 0x4d, /* editpad right */
46 0x4f, /* editpad end */
47 0x50, /* editpad dn */
48 0x51, /* editpad pgdn */
49 0x52, /* editpad ins */
50 0x53, /* editpad del */
51 0x00 /* map end */
52 };
wdenkc6097192002-11-03 00:24:07 +000053
Bin Meng3928d662015-08-24 01:00:04 -070054static int kbd_input_empty(void)
55{
Bin Meng835dd002015-08-24 01:00:05 -070056 int kbd_timeout = KBD_TIMEOUT * 1000;
Bin Meng3928d662015-08-24 01:00:04 -070057
Bin Meng835dd002015-08-24 01:00:05 -070058 while ((in8(I8042_STS_REG) & STATUS_IBF) && kbd_timeout--)
Bin Meng3928d662015-08-24 01:00:04 -070059 udelay(1);
60
Bin Meng835dd002015-08-24 01:00:05 -070061 return kbd_timeout != -1;
Bin Meng3928d662015-08-24 01:00:04 -070062}
63
Bin Meng835dd002015-08-24 01:00:05 -070064static int kbd_output_full(void)
Bin Meng3928d662015-08-24 01:00:04 -070065{
Bin Meng835dd002015-08-24 01:00:05 -070066 int kbd_timeout = KBD_TIMEOUT * 1000;
Bin Meng3928d662015-08-24 01:00:04 -070067
Bin Meng835dd002015-08-24 01:00:05 -070068 while (((in8(I8042_STS_REG) & STATUS_OBF) == 0) && kbd_timeout--)
Bin Meng3928d662015-08-24 01:00:04 -070069 udelay(1);
70
Bin Meng835dd002015-08-24 01:00:05 -070071 return kbd_timeout != -1;
Bin Meng3928d662015-08-24 01:00:04 -070072}
73
Simon Glassdcbf8252015-11-11 10:05:45 -070074/**
75 * check_leds() - Check the keyboard LEDs and update them it needed
76 *
77 * @ret: Value to return
78 * @return value of @ret
79 */
80static int i8042_kbd_update_leds(struct udevice *dev, int leds)
Bin Meng3928d662015-08-24 01:00:04 -070081{
82 kbd_input_empty();
Bin Meng835dd002015-08-24 01:00:05 -070083 out8(I8042_DATA_REG, CMD_SET_KBD_LED);
Bin Meng3928d662015-08-24 01:00:04 -070084 kbd_input_empty();
Simon Glassdcbf8252015-11-11 10:05:45 -070085 out8(I8042_DATA_REG, leds & 0x7);
86
87 return 0;
Bin Meng3928d662015-08-24 01:00:04 -070088}
89
Simon Glass31d38ee2015-10-18 21:17:19 -060090static int kbd_write(int reg, int value)
91{
92 if (!kbd_input_empty())
93 return -1;
94 out8(reg, value);
95
96 return 0;
97}
98
99static int kbd_read(int reg)
100{
101 if (!kbd_output_full())
102 return -1;
103
104 return in8(reg);
105}
106
107static int kbd_cmd_read(int cmd)
108{
109 if (kbd_write(I8042_CMD_REG, cmd))
110 return -1;
111
112 return kbd_read(I8042_DATA_REG);
113}
114
115static int kbd_cmd_write(int cmd, int data)
116{
117 if (kbd_write(I8042_CMD_REG, cmd))
118 return -1;
119
120 return kbd_write(I8042_DATA_REG, data);
121}
122
Simon Glass011d89d2015-11-11 10:05:46 -0700123static int kbd_reset(int quirk)
Bin Meng3928d662015-08-24 01:00:04 -0700124{
Simon Glass31d38ee2015-10-18 21:17:19 -0600125 int config;
Bin Meng7d961662015-08-24 01:00:06 -0700126
127 /* controller self test */
Simon Glass31d38ee2015-10-18 21:17:19 -0600128 if (kbd_cmd_read(CMD_SELF_TEST) != KBC_TEST_OK)
Simon Glass4f087ba2015-10-18 21:17:20 -0600129 goto err;
Bin Meng3928d662015-08-24 01:00:04 -0700130
Bin Meng7d961662015-08-24 01:00:06 -0700131 /* keyboard reset */
Simon Glass31d38ee2015-10-18 21:17:19 -0600132 if (kbd_write(I8042_DATA_REG, CMD_RESET_KBD) ||
133 kbd_read(I8042_DATA_REG) != KBD_ACK ||
134 kbd_read(I8042_DATA_REG) != KBD_POR)
Simon Glass4f087ba2015-10-18 21:17:20 -0600135 goto err;
Bin Meng3928d662015-08-24 01:00:04 -0700136
Bin Meng7d961662015-08-24 01:00:06 -0700137 /* set AT translation and disable irq */
Simon Glass31d38ee2015-10-18 21:17:19 -0600138 config = kbd_cmd_read(CMD_RD_CONFIG);
139 if (config == -1)
Simon Glass4f087ba2015-10-18 21:17:20 -0600140 goto err;
Simon Glass31d38ee2015-10-18 21:17:19 -0600141
Simon Glass011d89d2015-11-11 10:05:46 -0700142 /* Sometimes get a second byte */
143 else if ((quirk & QUIRK_DUP_POR) && config == KBD_POR)
144 config = kbd_cmd_read(CMD_RD_CONFIG);
145
Bin Meng7d961662015-08-24 01:00:06 -0700146 config |= CONFIG_AT_TRANS;
147 config &= ~(CONFIG_KIRQ_EN | CONFIG_MIRQ_EN);
Simon Glass31d38ee2015-10-18 21:17:19 -0600148 if (kbd_cmd_write(CMD_WR_CONFIG, config))
Simon Glass4f087ba2015-10-18 21:17:20 -0600149 goto err;
Bin Meng3928d662015-08-24 01:00:04 -0700150
Bin Meng7d961662015-08-24 01:00:06 -0700151 /* enable keyboard */
Simon Glass31d38ee2015-10-18 21:17:19 -0600152 if (kbd_write(I8042_CMD_REG, CMD_KBD_EN) ||
153 !kbd_input_empty())
Simon Glass4f087ba2015-10-18 21:17:20 -0600154 goto err;
Bin Meng3928d662015-08-24 01:00:04 -0700155
Bin Meng3928d662015-08-24 01:00:04 -0700156 return 0;
Simon Glass4f087ba2015-10-18 21:17:20 -0600157err:
158 debug("%s: Keyboard failure\n", __func__);
159 return -1;
Bin Meng3928d662015-08-24 01:00:04 -0700160}
161
Gabe Black22e0f5a2011-11-14 20:18:12 +0000162static int kbd_controller_present(void)
163{
Bin Meng835dd002015-08-24 01:00:05 -0700164 return in8(I8042_STS_REG) != 0xff;
Gabe Black22e0f5a2011-11-14 20:18:12 +0000165}
166
Gabe Black48edb302012-10-12 14:02:02 +0000167/*
168 * Implement a weak default function for boards that optionally
169 * need to skip the i8042 initialization.
Simon Glassdcbf8252015-11-11 10:05:45 -0700170 *
171 * TODO(sjg@chromium.org): Use device tree for this?
Gabe Black48edb302012-10-12 14:02:02 +0000172 */
173int __weak board_i8042_skip(void)
174{
175 /* As default, don't skip */
176 return 0;
177}
178
Louis Yung-Chieh Lo45fe6682012-10-11 15:15:51 +0000179void i8042_flush(void)
180{
181 int timeout;
182
183 /*
Bin Meng835dd002015-08-24 01:00:05 -0700184 * The delay is to give the keyboard controller some time
185 * to fill the next byte.
Louis Yung-Chieh Lo45fe6682012-10-11 15:15:51 +0000186 */
187 while (1) {
Bin Meng835dd002015-08-24 01:00:05 -0700188 timeout = 100; /* wait for no longer than 100us */
189 while (timeout > 0 && !(in8(I8042_STS_REG) & STATUS_OBF)) {
Louis Yung-Chieh Lo45fe6682012-10-11 15:15:51 +0000190 udelay(1);
191 timeout--;
192 }
193
Bin Meng835dd002015-08-24 01:00:05 -0700194 /* Try to pull next byte if not timeout */
195 if (in8(I8042_STS_REG) & STATUS_OBF)
Louis Yung-Chieh Lo45fe6682012-10-11 15:15:51 +0000196 in8(I8042_DATA_REG);
197 else
198 break;
199 }
200}
201
202int i8042_disable(void)
203{
204 if (kbd_input_empty() == 0)
205 return -1;
206
207 /* Disable keyboard */
Bin Meng835dd002015-08-24 01:00:05 -0700208 out8(I8042_CMD_REG, CMD_KBD_DIS);
Louis Yung-Chieh Lo45fe6682012-10-11 15:15:51 +0000209
210 if (kbd_input_empty() == 0)
211 return -1;
212
213 return 0;
214}
215
Simon Glass2ec739d2015-11-11 10:05:41 -0700216static int i8042_kbd_check(struct input_config *input)
217{
Simon Glassdcbf8252015-11-11 10:05:45 -0700218 struct i8042_kbd_priv *priv = dev_get_priv(input->dev);
219
Simon Glass2ec739d2015-11-11 10:05:41 -0700220 if ((in8(I8042_STS_REG) & STATUS_OBF) == 0) {
221 return 0;
222 } else {
223 bool release = false;
224 int scan_code;
225 int i;
226
227 scan_code = in8(I8042_DATA_REG);
228 if (scan_code == 0xfa) {
229 return 0;
230 } else if (scan_code == 0xe0) {
Simon Glassdcbf8252015-11-11 10:05:45 -0700231 priv->extended = true;
Simon Glass2ec739d2015-11-11 10:05:41 -0700232 return 0;
233 }
234 if (scan_code & 0x80) {
235 scan_code &= 0x7f;
236 release = true;
237 }
Simon Glassdcbf8252015-11-11 10:05:45 -0700238 if (priv->extended) {
239 priv->extended = false;
Simon Glass2ec739d2015-11-11 10:05:41 -0700240 for (i = 0; ext_key_map[i]; i++) {
241 if (ext_key_map[i] == scan_code) {
242 scan_code = 0x60 + i;
243 break;
244 }
245 }
246 /* not found ? */
247 if (!ext_key_map[i])
248 return 0;
249 }
250
Simon Glassdcbf8252015-11-11 10:05:45 -0700251 input_add_keycode(input, scan_code, release);
Simon Glass2ec739d2015-11-11 10:05:41 -0700252 return 1;
253 }
254}
255
Bin Meng835dd002015-08-24 01:00:05 -0700256/* i8042_kbd_init - reset keyboard and init state flags */
Simon Glassdcbf8252015-11-11 10:05:45 -0700257static int i8042_start(struct udevice *dev)
wdenkc6097192002-11-03 00:24:07 +0000258{
Simon Glassdcbf8252015-11-11 10:05:45 -0700259 struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass011d89d2015-11-11 10:05:46 -0700260 struct i8042_kbd_priv *priv = dev_get_priv(dev);
Simon Glassdcbf8252015-11-11 10:05:45 -0700261 struct input_config *input = &uc_priv->input;
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000262 int keymap, try;
263 char *penv;
Simon Glass2ec739d2015-11-11 10:05:41 -0700264 int ret;
wdenkc6097192002-11-03 00:24:07 +0000265
Bin Meng835dd002015-08-24 01:00:05 -0700266 if (!kbd_controller_present() || board_i8042_skip()) {
267 debug("i8042 keyboard controller is not present\n");
Simon Glassdcbf8252015-11-11 10:05:45 -0700268 return -ENOENT;
Bin Meng835dd002015-08-24 01:00:05 -0700269 }
Gabe Black22e0f5a2011-11-14 20:18:12 +0000270
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000271 /* Init keyboard device (default US layout) */
272 keymap = KBD_US;
273 penv = getenv("keymap");
274 if (penv != NULL) {
275 if (strncmp(penv, "de", 3) == 0)
276 keymap = KBD_GER;
277 }
wdenkc6097192002-11-03 00:24:07 +0000278
Simon Glass011d89d2015-11-11 10:05:46 -0700279 for (try = 0; kbd_reset(priv->quirks) != 0; try++) {
Simon Glassc5d257f2015-10-18 21:17:21 -0600280 if (try >= KBD_RESET_TRIES)
281 return -1;
Gabe Blackdd4a5b22011-11-14 19:24:14 +0000282 }
Bin Meng835dd002015-08-24 01:00:05 -0700283
Simon Glassdcbf8252015-11-11 10:05:45 -0700284 ret = input_add_tables(input, keymap == KBD_GER);
Simon Glass2ec739d2015-11-11 10:05:41 -0700285 if (ret)
286 return ret;
Simon Glass2ec739d2015-11-11 10:05:41 -0700287
Simon Glassdcbf8252015-11-11 10:05:45 -0700288 i8042_kbd_update_leds(dev, NORMAL);
289 debug("%s: started\n", __func__);
Simon Glassc5d257f2015-10-18 21:17:21 -0600290
291 return 0;
wdenkc6097192002-11-03 00:24:07 +0000292}
293
Simon Glass2ec739d2015-11-11 10:05:41 -0700294/**
Simon Glassdcbf8252015-11-11 10:05:45 -0700295 * Set up the i8042 keyboard. This is called by the stdio device handler
Bin Meng835dd002015-08-24 01:00:05 -0700296 *
Simon Glassdcbf8252015-11-11 10:05:45 -0700297 * We want to do this init when the keyboard is actually used rather than
298 * at start-up, since keyboard input may not currently be selected.
299 *
300 * Once the keyboard starts there will be a period during which we must
301 * wait for the keyboard to init. We do this only when a key is first
302 * read - see kbd_wait_for_fifo_init().
303 *
304 * @return 0 if ok, -ve on error
wdenkc6097192002-11-03 00:24:07 +0000305 */
Simon Glassdcbf8252015-11-11 10:05:45 -0700306static int i8042_kbd_probe(struct udevice *dev)
Simon Glass2ec739d2015-11-11 10:05:41 -0700307{
Simon Glassdcbf8252015-11-11 10:05:45 -0700308 struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass011d89d2015-11-11 10:05:46 -0700309 struct i8042_kbd_priv *priv = dev_get_priv(dev);
Simon Glassdcbf8252015-11-11 10:05:45 -0700310 struct stdio_dev *sdev = &uc_priv->sdev;
311 struct input_config *input = &uc_priv->input;
312 int ret;
Simon Glass2ec739d2015-11-11 10:05:41 -0700313
Simon Glass011d89d2015-11-11 10:05:46 -0700314 if (fdtdec_get_bool(gd->fdt_blob, dev->of_offset,
315 "intel,duplicate-por"))
316 priv->quirks |= QUIRK_DUP_POR;
317
Simon Glassdcbf8252015-11-11 10:05:45 -0700318 /* Register the device. i8042_start() will be called soon */
319 input->dev = dev;
320 input->read_keys = i8042_kbd_check;
321 input_allow_repeats(input, true);
322 strcpy(sdev->name, "i8042-kbd");
323 ret = input_stdio_register(sdev);
324 if (ret) {
325 debug("%s: input_stdio_register() failed\n", __func__);
326 return ret;
327 }
328 debug("%s: ready\n", __func__);
Simon Glass2ec739d2015-11-11 10:05:41 -0700329
Simon Glassdcbf8252015-11-11 10:05:45 -0700330 return 0;
Simon Glass2ec739d2015-11-11 10:05:41 -0700331}
332
Simon Glassdcbf8252015-11-11 10:05:45 -0700333static const struct keyboard_ops i8042_kbd_ops = {
334 .start = i8042_start,
335 .update_leds = i8042_kbd_update_leds,
336};
wdenkc6097192002-11-03 00:24:07 +0000337
Simon Glassdcbf8252015-11-11 10:05:45 -0700338static const struct udevice_id i8042_kbd_ids[] = {
339 { .compatible = "intel,i8042-keyboard" },
340 { }
341};
342
343U_BOOT_DRIVER(i8042_kbd) = {
344 .name = "i8042_kbd",
345 .id = UCLASS_KEYBOARD,
346 .of_match = i8042_kbd_ids,
347 .probe = i8042_kbd_probe,
348 .ops = &i8042_kbd_ops,
349 .priv_auto_alloc_size = sizeof(struct i8042_kbd_priv),
350};