blob: 03d48403857a862c2e6224a15a71eb4472119607 [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 Denkbd8ec7e2013-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 Glassfa0a42a2015-11-11 10:05:45 -070011#include <dm.h>
12#include <errno.h>
wdenkc6097192002-11-03 00:24:07 +000013#include <i8042.h>
Simon Glasse1a77bb2015-11-11 10:05:41 -070014#include <input.h>
Simon Glassfa0a42a2015-11-11 10:05:45 -070015#include <keyboard.h>
Simon Glasse1a77bb2015-11-11 10:05:41 -070016#include <asm/io.h>
wdenkc6097192002-11-03 00:24:07 +000017
Simon Glass708c8312015-11-11 10:05:46 -070018DECLARE_GLOBAL_DATA_PTR;
19
wdenkc6097192002-11-03 00:24:07 +000020/* defines */
Bin Mengbc823082015-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 Glass708c8312015-11-11 10:05:46 -070024enum {
25 QUIRK_DUP_POR = 1 << 0,
26};
27
wdenkc6097192002-11-03 00:24:07 +000028/* locals */
Simon Glassfa0a42a2015-11-11 10:05:45 -070029struct i8042_kbd_priv {
30 bool extended; /* true if an extended keycode is expected next */
Simon Glass708c8312015-11-11 10:05:46 -070031 int quirks; /* quirks that we support */
Simon Glassfa0a42a2015-11-11 10:05:45 -070032};
wdenkc6097192002-11-03 00:24:07 +000033
Gabe Black76a5d322011-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 Mengc89f2512015-08-24 01:00:04 -070054static int kbd_input_empty(void)
55{
Bin Mengbc823082015-08-24 01:00:05 -070056 int kbd_timeout = KBD_TIMEOUT * 1000;
Bin Mengc89f2512015-08-24 01:00:04 -070057
Bin Mengbc823082015-08-24 01:00:05 -070058 while ((in8(I8042_STS_REG) & STATUS_IBF) && kbd_timeout--)
Bin Mengc89f2512015-08-24 01:00:04 -070059 udelay(1);
60
Bin Mengbc823082015-08-24 01:00:05 -070061 return kbd_timeout != -1;
Bin Mengc89f2512015-08-24 01:00:04 -070062}
63
Bin Mengbc823082015-08-24 01:00:05 -070064static int kbd_output_full(void)
Bin Mengc89f2512015-08-24 01:00:04 -070065{
Bin Mengbc823082015-08-24 01:00:05 -070066 int kbd_timeout = KBD_TIMEOUT * 1000;
Bin Mengc89f2512015-08-24 01:00:04 -070067
Bin Mengbc823082015-08-24 01:00:05 -070068 while (((in8(I8042_STS_REG) & STATUS_OBF) == 0) && kbd_timeout--)
Bin Mengc89f2512015-08-24 01:00:04 -070069 udelay(1);
70
Bin Mengbc823082015-08-24 01:00:05 -070071 return kbd_timeout != -1;
Bin Mengc89f2512015-08-24 01:00:04 -070072}
73
Simon Glassfa0a42a2015-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 Mengc89f2512015-08-24 01:00:04 -070081{
82 kbd_input_empty();
Bin Mengbc823082015-08-24 01:00:05 -070083 out8(I8042_DATA_REG, CMD_SET_KBD_LED);
Bin Mengc89f2512015-08-24 01:00:04 -070084 kbd_input_empty();
Simon Glassfa0a42a2015-11-11 10:05:45 -070085 out8(I8042_DATA_REG, leds & 0x7);
86
87 return 0;
Bin Mengc89f2512015-08-24 01:00:04 -070088}
89
Simon Glass4b48eaf2015-10-18 21:17:19 -060090static int kbd_write(int reg, int value)
Bin Mengc89f2512015-08-24 01:00:04 -070091{
Simon Glass4b48eaf2015-10-18 21:17:19 -060092 if (!kbd_input_empty())
Bin Mengef4d7852015-08-24 01:00:06 -070093 return -1;
Simon Glass4b48eaf2015-10-18 21:17:19 -060094 out8(reg, value);
Bin Mengc89f2512015-08-24 01:00:04 -070095
Simon Glass4b48eaf2015-10-18 21:17:19 -060096 return 0;
97}
98
99static int kbd_read(int reg)
100{
101 if (!kbd_output_full())
Bin Mengef4d7852015-08-24 01:00:06 -0700102 return -1;
Simon Glass4b48eaf2015-10-18 21:17:19 -0600103
104 return in8(reg);
105}
106
107static int kbd_cmd_read(int cmd)
108{
109 if (kbd_write(I8042_CMD_REG, cmd))
Bin Mengc89f2512015-08-24 01:00:04 -0700110 return -1;
Simon Glass4b48eaf2015-10-18 21:17:19 -0600111
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))
Bin Mengc89f2512015-08-24 01:00:04 -0700118 return -1;
Simon Glass4b48eaf2015-10-18 21:17:19 -0600119
120 return kbd_write(I8042_DATA_REG, data);
121}
122
Simon Glass708c8312015-11-11 10:05:46 -0700123static int kbd_reset(int quirk)
Simon Glass4b48eaf2015-10-18 21:17:19 -0600124{
125 int config;
126
127 /* controller self test */
128 if (kbd_cmd_read(CMD_SELF_TEST) != KBC_TEST_OK)
Simon Glass4f58bbc2015-10-18 21:17:20 -0600129 goto err;
Simon Glass4b48eaf2015-10-18 21:17:19 -0600130
131 /* keyboard reset */
132 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 Glass4f58bbc2015-10-18 21:17:20 -0600135 goto err;
Bin Mengc89f2512015-08-24 01:00:04 -0700136
Simon Glass0a7fb2c2016-03-11 22:06:50 -0700137 if (kbd_write(I8042_DATA_REG, CMD_DRAIN_OUTPUT) ||
138 kbd_read(I8042_DATA_REG) != KBD_ACK)
139 goto err;
140
Bin Mengef4d7852015-08-24 01:00:06 -0700141 /* set AT translation and disable irq */
Simon Glass4b48eaf2015-10-18 21:17:19 -0600142 config = kbd_cmd_read(CMD_RD_CONFIG);
143 if (config == -1)
Simon Glass4f58bbc2015-10-18 21:17:20 -0600144 goto err;
Simon Glass4b48eaf2015-10-18 21:17:19 -0600145
Simon Glass708c8312015-11-11 10:05:46 -0700146 /* Sometimes get a second byte */
147 else if ((quirk & QUIRK_DUP_POR) && config == KBD_POR)
148 config = kbd_cmd_read(CMD_RD_CONFIG);
149
Bin Mengef4d7852015-08-24 01:00:06 -0700150 config |= CONFIG_AT_TRANS;
151 config &= ~(CONFIG_KIRQ_EN | CONFIG_MIRQ_EN);
Simon Glass4b48eaf2015-10-18 21:17:19 -0600152 if (kbd_cmd_write(CMD_WR_CONFIG, config))
Simon Glass4f58bbc2015-10-18 21:17:20 -0600153 goto err;
Bin Mengc89f2512015-08-24 01:00:04 -0700154
Bin Mengef4d7852015-08-24 01:00:06 -0700155 /* enable keyboard */
Simon Glass4b48eaf2015-10-18 21:17:19 -0600156 if (kbd_write(I8042_CMD_REG, CMD_KBD_EN) ||
157 !kbd_input_empty())
Simon Glass4f58bbc2015-10-18 21:17:20 -0600158 goto err;
Bin Mengc89f2512015-08-24 01:00:04 -0700159
160 return 0;
Simon Glass4f58bbc2015-10-18 21:17:20 -0600161err:
162 debug("%s: Keyboard failure\n", __func__);
163 return -1;
Bin Mengc89f2512015-08-24 01:00:04 -0700164}
165
Gabe Black15bbe8d2011-11-14 20:18:12 +0000166static int kbd_controller_present(void)
167{
Bin Mengbc823082015-08-24 01:00:05 -0700168 return in8(I8042_STS_REG) != 0xff;
Gabe Black15bbe8d2011-11-14 20:18:12 +0000169}
170
Gabe Black13702302012-10-12 14:02:02 +0000171/*
172 * Implement a weak default function for boards that optionally
173 * need to skip the i8042 initialization.
Simon Glassfa0a42a2015-11-11 10:05:45 -0700174 *
175 * TODO(sjg@chromium.org): Use device tree for this?
Gabe Black13702302012-10-12 14:02:02 +0000176 */
177int __weak board_i8042_skip(void)
178{
179 /* As default, don't skip */
180 return 0;
181}
182
Louis Yung-Chieh Lo46d579c2012-10-11 15:15:51 +0000183void i8042_flush(void)
184{
185 int timeout;
186
187 /*
Bin Mengbc823082015-08-24 01:00:05 -0700188 * The delay is to give the keyboard controller some time
189 * to fill the next byte.
Louis Yung-Chieh Lo46d579c2012-10-11 15:15:51 +0000190 */
191 while (1) {
Bin Mengbc823082015-08-24 01:00:05 -0700192 timeout = 100; /* wait for no longer than 100us */
193 while (timeout > 0 && !(in8(I8042_STS_REG) & STATUS_OBF)) {
Louis Yung-Chieh Lo46d579c2012-10-11 15:15:51 +0000194 udelay(1);
195 timeout--;
196 }
197
Bin Mengbc823082015-08-24 01:00:05 -0700198 /* Try to pull next byte if not timeout */
199 if (in8(I8042_STS_REG) & STATUS_OBF)
Louis Yung-Chieh Lo46d579c2012-10-11 15:15:51 +0000200 in8(I8042_DATA_REG);
201 else
202 break;
203 }
204}
205
206int i8042_disable(void)
207{
208 if (kbd_input_empty() == 0)
209 return -1;
210
211 /* Disable keyboard */
Bin Mengbc823082015-08-24 01:00:05 -0700212 out8(I8042_CMD_REG, CMD_KBD_DIS);
Louis Yung-Chieh Lo46d579c2012-10-11 15:15:51 +0000213
214 if (kbd_input_empty() == 0)
215 return -1;
216
217 return 0;
218}
219
Simon Glasse1a77bb2015-11-11 10:05:41 -0700220static int i8042_kbd_check(struct input_config *input)
221{
Simon Glassfa0a42a2015-11-11 10:05:45 -0700222 struct i8042_kbd_priv *priv = dev_get_priv(input->dev);
223
Simon Glasse1a77bb2015-11-11 10:05:41 -0700224 if ((in8(I8042_STS_REG) & STATUS_OBF) == 0) {
225 return 0;
226 } else {
227 bool release = false;
228 int scan_code;
229 int i;
230
231 scan_code = in8(I8042_DATA_REG);
232 if (scan_code == 0xfa) {
233 return 0;
234 } else if (scan_code == 0xe0) {
Simon Glassfa0a42a2015-11-11 10:05:45 -0700235 priv->extended = true;
Simon Glasse1a77bb2015-11-11 10:05:41 -0700236 return 0;
237 }
238 if (scan_code & 0x80) {
239 scan_code &= 0x7f;
240 release = true;
241 }
Simon Glassfa0a42a2015-11-11 10:05:45 -0700242 if (priv->extended) {
243 priv->extended = false;
Simon Glasse1a77bb2015-11-11 10:05:41 -0700244 for (i = 0; ext_key_map[i]; i++) {
245 if (ext_key_map[i] == scan_code) {
246 scan_code = 0x60 + i;
247 break;
248 }
249 }
250 /* not found ? */
251 if (!ext_key_map[i])
252 return 0;
253 }
254
Simon Glassfa0a42a2015-11-11 10:05:45 -0700255 input_add_keycode(input, scan_code, release);
Simon Glasse1a77bb2015-11-11 10:05:41 -0700256 return 1;
257 }
258}
259
Bin Mengbc823082015-08-24 01:00:05 -0700260/* i8042_kbd_init - reset keyboard and init state flags */
Simon Glassfa0a42a2015-11-11 10:05:45 -0700261static int i8042_start(struct udevice *dev)
wdenkc6097192002-11-03 00:24:07 +0000262{
Simon Glassfa0a42a2015-11-11 10:05:45 -0700263 struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass708c8312015-11-11 10:05:46 -0700264 struct i8042_kbd_priv *priv = dev_get_priv(dev);
Simon Glassfa0a42a2015-11-11 10:05:45 -0700265 struct input_config *input = &uc_priv->input;
Gabe Black76a5d322011-11-14 19:24:14 +0000266 int keymap, try;
267 char *penv;
Simon Glasse1a77bb2015-11-11 10:05:41 -0700268 int ret;
wdenkc6097192002-11-03 00:24:07 +0000269
Bin Mengbc823082015-08-24 01:00:05 -0700270 if (!kbd_controller_present() || board_i8042_skip()) {
271 debug("i8042 keyboard controller is not present\n");
Simon Glassfa0a42a2015-11-11 10:05:45 -0700272 return -ENOENT;
Bin Mengbc823082015-08-24 01:00:05 -0700273 }
Gabe Black15bbe8d2011-11-14 20:18:12 +0000274
Gabe Black76a5d322011-11-14 19:24:14 +0000275 /* Init keyboard device (default US layout) */
276 keymap = KBD_US;
277 penv = getenv("keymap");
278 if (penv != NULL) {
279 if (strncmp(penv, "de", 3) == 0)
280 keymap = KBD_GER;
281 }
wdenkc6097192002-11-03 00:24:07 +0000282
Simon Glass708c8312015-11-11 10:05:46 -0700283 for (try = 0; kbd_reset(priv->quirks) != 0; try++) {
Simon Glass2915ad62015-10-18 21:17:21 -0600284 if (try >= KBD_RESET_TRIES)
285 return -1;
Gabe Black76a5d322011-11-14 19:24:14 +0000286 }
Bin Mengbc823082015-08-24 01:00:05 -0700287
Simon Glassfa0a42a2015-11-11 10:05:45 -0700288 ret = input_add_tables(input, keymap == KBD_GER);
Simon Glasse1a77bb2015-11-11 10:05:41 -0700289 if (ret)
290 return ret;
Simon Glasse1a77bb2015-11-11 10:05:41 -0700291
Simon Glassfa0a42a2015-11-11 10:05:45 -0700292 i8042_kbd_update_leds(dev, NORMAL);
293 debug("%s: started\n", __func__);
Simon Glass2915ad62015-10-18 21:17:21 -0600294
295 return 0;
wdenkc6097192002-11-03 00:24:07 +0000296}
297
Simon Glasse1a77bb2015-11-11 10:05:41 -0700298/**
Simon Glassfa0a42a2015-11-11 10:05:45 -0700299 * Set up the i8042 keyboard. This is called by the stdio device handler
Bin Mengbc823082015-08-24 01:00:05 -0700300 *
Simon Glassfa0a42a2015-11-11 10:05:45 -0700301 * We want to do this init when the keyboard is actually used rather than
302 * at start-up, since keyboard input may not currently be selected.
303 *
304 * Once the keyboard starts there will be a period during which we must
305 * wait for the keyboard to init. We do this only when a key is first
306 * read - see kbd_wait_for_fifo_init().
307 *
308 * @return 0 if ok, -ve on error
wdenkc6097192002-11-03 00:24:07 +0000309 */
Simon Glassfa0a42a2015-11-11 10:05:45 -0700310static int i8042_kbd_probe(struct udevice *dev)
wdenkc6097192002-11-03 00:24:07 +0000311{
Simon Glassfa0a42a2015-11-11 10:05:45 -0700312 struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass708c8312015-11-11 10:05:46 -0700313 struct i8042_kbd_priv *priv = dev_get_priv(dev);
Simon Glassfa0a42a2015-11-11 10:05:45 -0700314 struct stdio_dev *sdev = &uc_priv->sdev;
315 struct input_config *input = &uc_priv->input;
316 int ret;
wdenkc6097192002-11-03 00:24:07 +0000317
Simon Glass708c8312015-11-11 10:05:46 -0700318 if (fdtdec_get_bool(gd->fdt_blob, dev->of_offset,
319 "intel,duplicate-por"))
320 priv->quirks |= QUIRK_DUP_POR;
321
Simon Glassfa0a42a2015-11-11 10:05:45 -0700322 /* Register the device. i8042_start() will be called soon */
323 input->dev = dev;
324 input->read_keys = i8042_kbd_check;
325 input_allow_repeats(input, true);
326 strcpy(sdev->name, "i8042-kbd");
327 ret = input_stdio_register(sdev);
328 if (ret) {
329 debug("%s: input_stdio_register() failed\n", __func__);
330 return ret;
331 }
332 debug("%s: ready\n", __func__);
wdenkc6097192002-11-03 00:24:07 +0000333
Simon Glassfa0a42a2015-11-11 10:05:45 -0700334 return 0;
Simon Glasse1a77bb2015-11-11 10:05:41 -0700335}
Bin Mengbc823082015-08-24 01:00:05 -0700336
Simon Glassfa0a42a2015-11-11 10:05:45 -0700337static const struct keyboard_ops i8042_kbd_ops = {
338 .start = i8042_start,
339 .update_leds = i8042_kbd_update_leds,
340};
wdenkc6097192002-11-03 00:24:07 +0000341
Simon Glassfa0a42a2015-11-11 10:05:45 -0700342static const struct udevice_id i8042_kbd_ids[] = {
343 { .compatible = "intel,i8042-keyboard" },
344 { }
345};
346
347U_BOOT_DRIVER(i8042_kbd) = {
348 .name = "i8042_kbd",
349 .id = UCLASS_KEYBOARD,
350 .of_match = i8042_kbd_ids,
351 .probe = i8042_kbd_probe,
352 .ops = &i8042_kbd_ops,
353 .priv_auto_alloc_size = sizeof(struct i8042_kbd_priv),
354};