Revert "Merge patch series "arm: dts: am62-beagleplay: Fix Beagleplay Ethernet""
[J-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
d8062e95
SG
9#define LOG_CATEGORY UCLASS_KEYBOARD
10
d678a59d 11#include <common.h>
dcbf8257 12#include <dm.h>
7b51b576 13#include <env.h>
dcbf8257 14#include <errno.h>
c609719b 15#include <i8042.h>
2ec739db 16#include <input.h>
dcbf8257 17#include <keyboard.h>
f7ae49fc 18#include <log.h>
401d1c4f 19#include <asm/global_data.h>
2ec739db 20#include <asm/io.h>
c05ed00a 21#include <linux/delay.h>
c609719b 22
011d89d6
SG
23DECLARE_GLOBAL_DATA_PTR;
24
c609719b 25/* defines */
835dd000
BM
26#define in8(p) inb(p)
27#define out8(p, v) outb(v, p)
c609719b 28
011d89d6
SG
29enum {
30 QUIRK_DUP_POR = 1 << 0,
31};
32
c609719b 33/* locals */
dcbf8257
SG
34struct i8042_kbd_priv {
35 bool extended; /* true if an extended keycode is expected next */
011d89d6 36 int quirks; /* quirks that we support */
dcbf8257 37};
dd4a5b22
GB
38
39static 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 };
c609719b 58
d8062e95
SG
59/**
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 */
3928d66a
BM
67static int kbd_input_empty(void)
68{
835dd000 69 int kbd_timeout = KBD_TIMEOUT * 1000;
3928d66a 70
835dd000 71 while ((in8(I8042_STS_REG) & STATUS_IBF) && kbd_timeout--)
3928d66a
BM
72 udelay(1);
73
835dd000 74 return kbd_timeout != -1;
3928d66a
BM
75}
76
d8062e95
SG
77/**
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 */
835dd000 83static int kbd_output_full(void)
3928d66a 84{
835dd000 85 int kbd_timeout = KBD_TIMEOUT * 1000;
3928d66a 86
835dd000 87 while (((in8(I8042_STS_REG) & STATUS_OBF) == 0) && kbd_timeout--)
3928d66a
BM
88 udelay(1);
89
835dd000 90 return kbd_timeout != -1;
3928d66a
BM
91}
92
dcbf8257
SG
93/**
94 * check_leds() - Check the keyboard LEDs and update them it needed
95 *
96 * @ret: Value to return
185f812c 97 * Return: value of @ret
dcbf8257
SG
98 */
99static int i8042_kbd_update_leds(struct udevice *dev, int leds)
3928d66a
BM
100{
101 kbd_input_empty();
835dd000 102 out8(I8042_DATA_REG, CMD_SET_KBD_LED);
3928d66a 103 kbd_input_empty();
dcbf8257
SG
104 out8(I8042_DATA_REG, leds & 0x7);
105
106 return 0;
3928d66a
BM
107}
108
31d38ee6 109static int kbd_write(int reg, int value)
3928d66a 110{
31d38ee6 111 if (!kbd_input_empty())
7d96166b 112 return -1;
31d38ee6 113 out8(reg, value);
3928d66a 114
31d38ee6
SG
115 return 0;
116}
117
118static int kbd_read(int reg)
119{
120 if (!kbd_output_full())
7d96166b 121 return -1;
31d38ee6
SG
122
123 return in8(reg);
124}
125
126static int kbd_cmd_read(int cmd)
127{
128 if (kbd_write(I8042_CMD_REG, cmd))
3928d66a 129 return -1;
31d38ee6
SG
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))
3928d66a 137 return -1;
31d38ee6
SG
138
139 return kbd_write(I8042_DATA_REG, data);
140}
141
011d89d6 142static int kbd_reset(int quirk)
31d38ee6
SG
143{
144 int config;
145
d8062e95
SG
146 if (!kbd_input_empty())
147 goto err;
148
31d38ee6
SG
149 /* controller self test */
150 if (kbd_cmd_read(CMD_SELF_TEST) != KBC_TEST_OK)
4f087bac 151 goto err;
31d38ee6
SG
152
153 /* keyboard reset */
154 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)
4f087bac 157 goto err;
3928d66a 158
8226a3e9
SG
159 if (kbd_write(I8042_DATA_REG, CMD_DRAIN_OUTPUT) ||
160 kbd_read(I8042_DATA_REG) != KBD_ACK)
161 goto err;
162
7d96166b 163 /* set AT translation and disable irq */
31d38ee6
SG
164 config = kbd_cmd_read(CMD_RD_CONFIG);
165 if (config == -1)
4f087bac 166 goto err;
31d38ee6 167
011d89d6
SG
168 /* Sometimes get a second byte */
169 else if ((quirk & QUIRK_DUP_POR) && config == KBD_POR)
170 config = kbd_cmd_read(CMD_RD_CONFIG);
171
b21f965b
TR
172 config |= CFG_AT_TRANS;
173 config &= ~(CFG_KIRQ_EN | CFG_MIRQ_EN);
31d38ee6 174 if (kbd_cmd_write(CMD_WR_CONFIG, config))
4f087bac 175 goto err;
3928d66a 176
7d96166b 177 /* enable keyboard */
31d38ee6
SG
178 if (kbd_write(I8042_CMD_REG, CMD_KBD_EN) ||
179 !kbd_input_empty())
4f087bac 180 goto err;
3928d66a
BM
181
182 return 0;
4f087bac
SG
183err:
184 debug("%s: Keyboard failure\n", __func__);
185 return -1;
3928d66a
BM
186}
187
22e0f5a9
GB
188static int kbd_controller_present(void)
189{
835dd000 190 return in8(I8042_STS_REG) != 0xff;
22e0f5a9
GB
191}
192
165be50f
SG
193/** Flush all buffer from keyboard controller to host*/
194static void i8042_flush(void)
45fe668f
LYCL
195{
196 int timeout;
197
198 /*
835dd000
BM
199 * The delay is to give the keyboard controller some time
200 * to fill the next byte.
45fe668f
LYCL
201 */
202 while (1) {
835dd000
BM
203 timeout = 100; /* wait for no longer than 100us */
204 while (timeout > 0 && !(in8(I8042_STS_REG) & STATUS_OBF)) {
45fe668f
LYCL
205 udelay(1);
206 timeout--;
207 }
208
835dd000
BM
209 /* Try to pull next byte if not timeout */
210 if (in8(I8042_STS_REG) & STATUS_OBF)
45fe668f
LYCL
211 in8(I8042_DATA_REG);
212 else
213 break;
214 }
215}
216
165be50f
SG
217/**
218 * Disables the keyboard so that key strokes no longer generate scancodes to
219 * the host.
220 *
185f812c 221 * Return: 0 if ok, -1 if keyboard input was found while disabling
165be50f
SG
222 */
223static int i8042_disable(void)
45fe668f
LYCL
224{
225 if (kbd_input_empty() == 0)
226 return -1;
227
228 /* Disable keyboard */
835dd000 229 out8(I8042_CMD_REG, CMD_KBD_DIS);
45fe668f
LYCL
230
231 if (kbd_input_empty() == 0)
232 return -1;
233
234 return 0;
235}
236
2ec739db
SG
237static int i8042_kbd_check(struct input_config *input)
238{
dcbf8257
SG
239 struct i8042_kbd_priv *priv = dev_get_priv(input->dev);
240
2ec739db
SG
241 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) {
dcbf8257 252 priv->extended = true;
2ec739db
SG
253 return 0;
254 }
255 if (scan_code & 0x80) {
256 scan_code &= 0x7f;
257 release = true;
258 }
dcbf8257
SG
259 if (priv->extended) {
260 priv->extended = false;
2ec739db
SG
261 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
dcbf8257 272 input_add_keycode(input, scan_code, release);
2ec739db
SG
273 return 1;
274 }
275}
276
835dd000 277/* i8042_kbd_init - reset keyboard and init state flags */
dcbf8257 278static int i8042_start(struct udevice *dev)
c609719b 279{
dcbf8257 280 struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
011d89d6 281 struct i8042_kbd_priv *priv = dev_get_priv(dev);
dcbf8257 282 struct input_config *input = &uc_priv->input;
dd4a5b22
GB
283 int keymap, try;
284 char *penv;
2ec739db 285 int ret;
c609719b 286
165be50f 287 if (!kbd_controller_present()) {
835dd000 288 debug("i8042 keyboard controller is not present\n");
dcbf8257 289 return -ENOENT;
835dd000 290 }
22e0f5a9 291
dd4a5b22
GB
292 /* Init keyboard device (default US layout) */
293 keymap = KBD_US;
00caae6d 294 penv = env_get("keymap");
dd4a5b22
GB
295 if (penv != NULL) {
296 if (strncmp(penv, "de", 3) == 0)
297 keymap = KBD_GER;
298 }
299
011d89d6 300 for (try = 0; kbd_reset(priv->quirks) != 0; try++) {
c5d257f9
SG
301 if (try >= KBD_RESET_TRIES)
302 return -1;
dd4a5b22 303 }
835dd000 304
dcbf8257 305 ret = input_add_tables(input, keymap == KBD_GER);
2ec739db
SG
306 if (ret)
307 return ret;
2ec739db 308
dcbf8257
SG
309 i8042_kbd_update_leds(dev, NORMAL);
310 debug("%s: started\n", __func__);
c5d257f9
SG
311
312 return 0;
c609719b
WD
313}
314
165be50f
SG
315static 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
2ec739db 324/**
dcbf8257 325 * Set up the i8042 keyboard. This is called by the stdio device handler
835dd000 326 *
dcbf8257
SG
327 * 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 *
185f812c 334 * Return: 0 if ok, -ve on error
c609719b 335 */
dcbf8257 336static int i8042_kbd_probe(struct udevice *dev)
c609719b 337{
dcbf8257 338 struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
011d89d6 339 struct i8042_kbd_priv *priv = dev_get_priv(dev);
dcbf8257
SG
340 struct stdio_dev *sdev = &uc_priv->sdev;
341 struct input_config *input = &uc_priv->input;
342 int ret;
dd4a5b22 343
e160f7d4 344 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
011d89d6
SG
345 "intel,duplicate-por"))
346 priv->quirks |= QUIRK_DUP_POR;
347
dcbf8257
SG
348 /* 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__);
835dd000 359
dcbf8257 360 return 0;
c609719b
WD
361}
362
dcbf8257
SG
363static const struct keyboard_ops i8042_kbd_ops = {
364 .start = i8042_start,
365 .update_leds = i8042_kbd_update_leds,
366};
367
368static 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,
165be50f 378 .remove = i8042_kbd_remove,
dcbf8257 379 .ops = &i8042_kbd_ops,
41575d8e 380 .priv_auto = sizeof(struct i8042_kbd_priv),
dcbf8257 381};
This page took 0.572903 seconds and 4 git commands to generate.