]> git.ipfire.org Git - thirdparty/u-boot.git/blame - drivers/input/i8042.c
SPDX: Convert all of our single license tags to Linux Kernel style
[thirdparty/u-boot.git] / drivers / input / i8042.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
c609719b
WD
2/*
3 * (C) Copyright 2002 ELTEC Elektronik AG
4 * Frank Gottschling <fgottschling@eltec.de>
c609719b
WD
5 */
6
7/* i8042.c - Intel 8042 keyboard driver routines */
8
c609719b 9#include <common.h>
dcbf8257
SG
10#include <dm.h>
11#include <errno.h>
c609719b 12#include <i8042.h>
2ec739db 13#include <input.h>
dcbf8257 14#include <keyboard.h>
2ec739db 15#include <asm/io.h>
c609719b 16
011d89d6
SG
17DECLARE_GLOBAL_DATA_PTR;
18
c609719b 19/* defines */
835dd000
BM
20#define in8(p) inb(p)
21#define out8(p, v) outb(v, p)
c609719b 22
011d89d6
SG
23enum {
24 QUIRK_DUP_POR = 1 << 0,
25};
26
c609719b 27/* locals */
dcbf8257
SG
28struct i8042_kbd_priv {
29 bool extended; /* true if an extended keycode is expected next */
011d89d6 30 int quirks; /* quirks that we support */
dcbf8257 31};
dd4a5b22
GB
32
33static 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 };
c609719b 52
3928d66a
BM
53static int kbd_input_empty(void)
54{
835dd000 55 int kbd_timeout = KBD_TIMEOUT * 1000;
3928d66a 56
835dd000 57 while ((in8(I8042_STS_REG) & STATUS_IBF) && kbd_timeout--)
3928d66a
BM
58 udelay(1);
59
835dd000 60 return kbd_timeout != -1;
3928d66a
BM
61}
62
835dd000 63static int kbd_output_full(void)
3928d66a 64{
835dd000 65 int kbd_timeout = KBD_TIMEOUT * 1000;
3928d66a 66
835dd000 67 while (((in8(I8042_STS_REG) & STATUS_OBF) == 0) && kbd_timeout--)
3928d66a
BM
68 udelay(1);
69
835dd000 70 return kbd_timeout != -1;
3928d66a
BM
71}
72
dcbf8257
SG
73/**
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)
3928d66a
BM
80{
81 kbd_input_empty();
835dd000 82 out8(I8042_DATA_REG, CMD_SET_KBD_LED);
3928d66a 83 kbd_input_empty();
dcbf8257
SG
84 out8(I8042_DATA_REG, leds & 0x7);
85
86 return 0;
3928d66a
BM
87}
88
31d38ee6 89static int kbd_write(int reg, int value)
3928d66a 90{
31d38ee6 91 if (!kbd_input_empty())
7d96166b 92 return -1;
31d38ee6 93 out8(reg, value);
3928d66a 94
31d38ee6
SG
95 return 0;
96}
97
98static int kbd_read(int reg)
99{
100 if (!kbd_output_full())
7d96166b 101 return -1;
31d38ee6
SG
102
103 return in8(reg);
104}
105
106static int kbd_cmd_read(int cmd)
107{
108 if (kbd_write(I8042_CMD_REG, cmd))
3928d66a 109 return -1;
31d38ee6
SG
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))
3928d66a 117 return -1;
31d38ee6
SG
118
119 return kbd_write(I8042_DATA_REG, data);
120}
121
011d89d6 122static int kbd_reset(int quirk)
31d38ee6
SG
123{
124 int config;
125
126 /* controller self test */
127 if (kbd_cmd_read(CMD_SELF_TEST) != KBC_TEST_OK)
4f087bac 128 goto err;
31d38ee6
SG
129
130 /* keyboard reset */
131 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)
4f087bac 134 goto err;
3928d66a 135
8226a3e9
SG
136 if (kbd_write(I8042_DATA_REG, CMD_DRAIN_OUTPUT) ||
137 kbd_read(I8042_DATA_REG) != KBD_ACK)
138 goto err;
139
7d96166b 140 /* set AT translation and disable irq */
31d38ee6
SG
141 config = kbd_cmd_read(CMD_RD_CONFIG);
142 if (config == -1)
4f087bac 143 goto err;
31d38ee6 144
011d89d6
SG
145 /* Sometimes get a second byte */
146 else if ((quirk & QUIRK_DUP_POR) && config == KBD_POR)
147 config = kbd_cmd_read(CMD_RD_CONFIG);
148
7d96166b
BM
149 config |= CONFIG_AT_TRANS;
150 config &= ~(CONFIG_KIRQ_EN | CONFIG_MIRQ_EN);
31d38ee6 151 if (kbd_cmd_write(CMD_WR_CONFIG, config))
4f087bac 152 goto err;
3928d66a 153
7d96166b 154 /* enable keyboard */
31d38ee6
SG
155 if (kbd_write(I8042_CMD_REG, CMD_KBD_EN) ||
156 !kbd_input_empty())
4f087bac 157 goto err;
3928d66a
BM
158
159 return 0;
4f087bac
SG
160err:
161 debug("%s: Keyboard failure\n", __func__);
162 return -1;
3928d66a
BM
163}
164
22e0f5a9
GB
165static int kbd_controller_present(void)
166{
835dd000 167 return in8(I8042_STS_REG) != 0xff;
22e0f5a9
GB
168}
169
48edb304
GB
170/*
171 * Implement a weak default function for boards that optionally
172 * need to skip the i8042 initialization.
dcbf8257
SG
173 *
174 * TODO(sjg@chromium.org): Use device tree for this?
48edb304
GB
175 */
176int __weak board_i8042_skip(void)
177{
178 /* As default, don't skip */
179 return 0;
180}
181
45fe668f
LYCL
182void i8042_flush(void)
183{
184 int timeout;
185
186 /*
835dd000
BM
187 * The delay is to give the keyboard controller some time
188 * to fill the next byte.
45fe668f
LYCL
189 */
190 while (1) {
835dd000
BM
191 timeout = 100; /* wait for no longer than 100us */
192 while (timeout > 0 && !(in8(I8042_STS_REG) & STATUS_OBF)) {
45fe668f
LYCL
193 udelay(1);
194 timeout--;
195 }
196
835dd000
BM
197 /* Try to pull next byte if not timeout */
198 if (in8(I8042_STS_REG) & STATUS_OBF)
45fe668f
LYCL
199 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 */
835dd000 211 out8(I8042_CMD_REG, CMD_KBD_DIS);
45fe668f
LYCL
212
213 if (kbd_input_empty() == 0)
214 return -1;
215
216 return 0;
217}
218
2ec739db
SG
219static int i8042_kbd_check(struct input_config *input)
220{
dcbf8257
SG
221 struct i8042_kbd_priv *priv = dev_get_priv(input->dev);
222
2ec739db
SG
223 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) {
dcbf8257 234 priv->extended = true;
2ec739db
SG
235 return 0;
236 }
237 if (scan_code & 0x80) {
238 scan_code &= 0x7f;
239 release = true;
240 }
dcbf8257
SG
241 if (priv->extended) {
242 priv->extended = false;
2ec739db
SG
243 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
dcbf8257 254 input_add_keycode(input, scan_code, release);
2ec739db
SG
255 return 1;
256 }
257}
258
835dd000 259/* i8042_kbd_init - reset keyboard and init state flags */
dcbf8257 260static int i8042_start(struct udevice *dev)
c609719b 261{
dcbf8257 262 struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
011d89d6 263 struct i8042_kbd_priv *priv = dev_get_priv(dev);
dcbf8257 264 struct input_config *input = &uc_priv->input;
dd4a5b22
GB
265 int keymap, try;
266 char *penv;
2ec739db 267 int ret;
c609719b 268
835dd000
BM
269 if (!kbd_controller_present() || board_i8042_skip()) {
270 debug("i8042 keyboard controller is not present\n");
dcbf8257 271 return -ENOENT;
835dd000 272 }
22e0f5a9 273
dd4a5b22
GB
274 /* Init keyboard device (default US layout) */
275 keymap = KBD_US;
00caae6d 276 penv = env_get("keymap");
dd4a5b22
GB
277 if (penv != NULL) {
278 if (strncmp(penv, "de", 3) == 0)
279 keymap = KBD_GER;
280 }
281
011d89d6 282 for (try = 0; kbd_reset(priv->quirks) != 0; try++) {
c5d257f9
SG
283 if (try >= KBD_RESET_TRIES)
284 return -1;
dd4a5b22 285 }
835dd000 286
dcbf8257 287 ret = input_add_tables(input, keymap == KBD_GER);
2ec739db
SG
288 if (ret)
289 return ret;
2ec739db 290
dcbf8257
SG
291 i8042_kbd_update_leds(dev, NORMAL);
292 debug("%s: started\n", __func__);
c5d257f9
SG
293
294 return 0;
c609719b
WD
295}
296
2ec739db 297/**
dcbf8257 298 * Set up the i8042 keyboard. This is called by the stdio device handler
835dd000 299 *
dcbf8257
SG
300 * 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
c609719b 308 */
dcbf8257 309static int i8042_kbd_probe(struct udevice *dev)
c609719b 310{
dcbf8257 311 struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
011d89d6 312 struct i8042_kbd_priv *priv = dev_get_priv(dev);
dcbf8257
SG
313 struct stdio_dev *sdev = &uc_priv->sdev;
314 struct input_config *input = &uc_priv->input;
315 int ret;
dd4a5b22 316
e160f7d4 317 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
011d89d6
SG
318 "intel,duplicate-por"))
319 priv->quirks |= QUIRK_DUP_POR;
320
dcbf8257
SG
321 /* 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__);
835dd000 332
dcbf8257 333 return 0;
c609719b
WD
334}
335
dcbf8257
SG
336static const struct keyboard_ops i8042_kbd_ops = {
337 .start = i8042_start,
338 .update_leds = i8042_kbd_update_leds,
339};
340
341static 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};