i8042.c 7.44 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
wdenk's avatar
wdenk committed
2
3
4
5
6
7
8
9
/*
 * (C) Copyright 2002 ELTEC Elektronik AG
 * Frank Gottschling <fgottschling@eltec.de>
 */

/* i8042.c - Intel 8042 keyboard driver routines */

#include <common.h>
10
#include <dm.h>
Simon Glass's avatar
Simon Glass committed
11
#include <env.h>
12
#include <errno.h>
wdenk's avatar
wdenk committed
13
#include <i8042.h>
14
#include <input.h>
15
#include <keyboard.h>
16
#include <log.h>
17
#include <asm/global_data.h>
18
#include <asm/io.h>
19
#include <linux/delay.h>
wdenk's avatar
wdenk committed
20

21
22
DECLARE_GLOBAL_DATA_PTR;

wdenk's avatar
wdenk committed
23
/* defines */
24
25
#define in8(p)		inb(p)
#define out8(p, v)	outb(v, p)
wdenk's avatar
wdenk committed
26

27
28
29
30
enum {
	QUIRK_DUP_POR	= 1 << 0,
};

wdenk's avatar
wdenk committed
31
/* locals */
32
33
struct i8042_kbd_priv {
	bool extended;	/* true if an extended keycode is expected next */
34
	int quirks;	/* quirks that we support */
35
};
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

static unsigned char ext_key_map[] = {
	0x1c, /* keypad enter */
	0x1d, /* right control */
	0x35, /* keypad slash */
	0x37, /* print screen */
	0x38, /* right alt */
	0x46, /* break */
	0x47, /* editpad home */
	0x48, /* editpad up */
	0x49, /* editpad pgup */
	0x4b, /* editpad left */
	0x4d, /* editpad right */
	0x4f, /* editpad end */
	0x50, /* editpad dn */
	0x51, /* editpad pgdn */
	0x52, /* editpad ins */
	0x53, /* editpad del */
	0x00  /* map end */
	};
wdenk's avatar
wdenk committed
56

57
58
static int kbd_input_empty(void)
{
59
	int kbd_timeout = KBD_TIMEOUT * 1000;
60

61
	while ((in8(I8042_STS_REG) & STATUS_IBF) && kbd_timeout--)
62
63
		udelay(1);

64
	return kbd_timeout != -1;
65
66
}

67
static int kbd_output_full(void)
68
{
69
	int kbd_timeout = KBD_TIMEOUT * 1000;
70

71
	while (((in8(I8042_STS_REG) & STATUS_OBF) == 0) && kbd_timeout--)
72
73
		udelay(1);

74
	return kbd_timeout != -1;
75
76
}

77
78
79
80
81
82
83
/**
 * check_leds() - Check the keyboard LEDs and update them it needed
 *
 * @ret:	Value to return
 * @return value of @ret
 */
static int i8042_kbd_update_leds(struct udevice *dev, int leds)
84
85
{
	kbd_input_empty();
86
	out8(I8042_DATA_REG, CMD_SET_KBD_LED);
87
	kbd_input_empty();
88
89
90
	out8(I8042_DATA_REG, leds & 0x7);

	return 0;
91
92
}

93
static int kbd_write(int reg, int value)
94
{
95
	if (!kbd_input_empty())
96
		return -1;
97
	out8(reg, value);
98

99
100
101
102
103
104
	return 0;
}

static int kbd_read(int reg)
{
	if (!kbd_output_full())
105
		return -1;
106
107
108
109
110
111
112

	return in8(reg);
}

static int kbd_cmd_read(int cmd)
{
	if (kbd_write(I8042_CMD_REG, cmd))
113
		return -1;
114
115
116
117
118
119
120

	return kbd_read(I8042_DATA_REG);
}

static int kbd_cmd_write(int cmd, int data)
{
	if (kbd_write(I8042_CMD_REG, cmd))
121
		return -1;
122
123
124
125

	return kbd_write(I8042_DATA_REG, data);
}

126
static int kbd_reset(int quirk)
127
128
129
130
131
{
	int config;

	/* controller self test */
	if (kbd_cmd_read(CMD_SELF_TEST) != KBC_TEST_OK)
132
		goto err;
133
134
135
136
137

	/* keyboard reset */
	if (kbd_write(I8042_DATA_REG, CMD_RESET_KBD) ||
	    kbd_read(I8042_DATA_REG) != KBD_ACK ||
	    kbd_read(I8042_DATA_REG) != KBD_POR)
138
		goto err;
139

140
141
142
143
	if (kbd_write(I8042_DATA_REG, CMD_DRAIN_OUTPUT) ||
	    kbd_read(I8042_DATA_REG) != KBD_ACK)
		goto err;

144
	/* set AT translation and disable irq */
145
146
	config = kbd_cmd_read(CMD_RD_CONFIG);
	if (config == -1)
147
		goto err;
148

149
150
151
152
	/* Sometimes get a second byte */
	else if ((quirk & QUIRK_DUP_POR) && config == KBD_POR)
		config = kbd_cmd_read(CMD_RD_CONFIG);

153
154
	config |= CFG_AT_TRANS;
	config &= ~(CFG_KIRQ_EN | CFG_MIRQ_EN);
155
	if (kbd_cmd_write(CMD_WR_CONFIG, config))
156
		goto err;
157

158
	/* enable keyboard */
159
160
	if (kbd_write(I8042_CMD_REG, CMD_KBD_EN) ||
	    !kbd_input_empty())
161
		goto err;
162
163

	return 0;
164
165
166
err:
	debug("%s: Keyboard failure\n", __func__);
	return -1;
167
168
}

169
170
static int kbd_controller_present(void)
{
171
	return in8(I8042_STS_REG) != 0xff;
172
173
}

174
175
/** Flush all buffer from keyboard controller to host*/
static void i8042_flush(void)
176
177
178
179
{
	int timeout;

	/*
180
181
	 * The delay is to give the keyboard controller some time
	 * to fill the next byte.
182
183
	 */
	while (1) {
184
185
		timeout = 100;	/* wait for no longer than 100us */
		while (timeout > 0 && !(in8(I8042_STS_REG) & STATUS_OBF)) {
186
187
188
189
			udelay(1);
			timeout--;
		}

190
191
		/* Try to pull next byte if not timeout */
		if (in8(I8042_STS_REG) & STATUS_OBF)
192
193
194
195
196
197
			in8(I8042_DATA_REG);
		else
			break;
	}
}

198
199
200
201
202
203
204
/**
 * Disables the keyboard so that key strokes no longer generate scancodes to
 * the host.
 *
 * @return 0 if ok, -1 if keyboard input was found while disabling
 */
static int i8042_disable(void)
205
206
207
208
209
{
	if (kbd_input_empty() == 0)
		return -1;

	/* Disable keyboard */
210
	out8(I8042_CMD_REG, CMD_KBD_DIS);
211
212
213
214
215
216
217

	if (kbd_input_empty() == 0)
		return -1;

	return 0;
}

218
219
static int i8042_kbd_check(struct input_config *input)
{
220
221
	struct i8042_kbd_priv *priv = dev_get_priv(input->dev);

222
223
224
225
226
227
228
229
230
231
232
	if ((in8(I8042_STS_REG) & STATUS_OBF) == 0) {
		return 0;
	} else {
		bool release = false;
		int scan_code;
		int i;

		scan_code = in8(I8042_DATA_REG);
		if (scan_code == 0xfa) {
			return 0;
		} else if (scan_code == 0xe0) {
233
			priv->extended = true;
234
235
236
237
238
239
			return 0;
		}
		if (scan_code & 0x80) {
			scan_code &= 0x7f;
			release = true;
		}
240
241
		if (priv->extended) {
			priv->extended = false;
242
243
244
245
246
247
248
249
250
251
252
			for (i = 0; ext_key_map[i]; i++) {
				if (ext_key_map[i] == scan_code) {
					scan_code = 0x60 + i;
					break;
				}
			}
			/* not found ? */
			if (!ext_key_map[i])
				return 0;
		}

253
		input_add_keycode(input, scan_code, release);
254
255
256
257
		return 1;
	}
}

258
/* i8042_kbd_init - reset keyboard and init state flags */
259
static int i8042_start(struct udevice *dev)
wdenk's avatar
wdenk committed
260
{
261
	struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
262
	struct i8042_kbd_priv *priv = dev_get_priv(dev);
263
	struct input_config *input = &uc_priv->input;
264
265
	int keymap, try;
	char *penv;
266
	int ret;
wdenk's avatar
wdenk committed
267

268
	if (!kbd_controller_present()) {
269
		debug("i8042 keyboard controller is not present\n");
270
		return -ENOENT;
271
	}
272

273
274
	/* Init keyboard device (default US layout) */
	keymap = KBD_US;
275
	penv = env_get("keymap");
276
277
278
279
280
	if (penv != NULL) {
		if (strncmp(penv, "de", 3) == 0)
			keymap = KBD_GER;
	}

281
	for (try = 0; kbd_reset(priv->quirks) != 0; try++) {
282
283
		if (try >= KBD_RESET_TRIES)
			return -1;
284
	}
285

286
	ret = input_add_tables(input, keymap == KBD_GER);
287
288
289
	if (ret)
		return ret;

290
291
	i8042_kbd_update_leds(dev, NORMAL);
	debug("%s: started\n", __func__);
292
293

	return 0;
wdenk's avatar
wdenk committed
294
295
}

296
297
298
299
300
301
302
303
304
static int i8042_kbd_remove(struct udevice *dev)
{
	if (i8042_disable())
		log_debug("i8042_disable() failed. fine, continue.\n");
	i8042_flush();

	return 0;
}

305
/**
306
 * Set up the i8042 keyboard. This is called by the stdio device handler
307
 *
308
309
310
311
312
313
314
315
 * We want to do this init when the keyboard is actually used rather than
 * at start-up, since keyboard input may not currently be selected.
 *
 * Once the keyboard starts there will be a period during which we must
 * wait for the keyboard to init. We do this only when a key is first
 * read - see kbd_wait_for_fifo_init().
 *
 * @return 0 if ok, -ve on error
wdenk's avatar
wdenk committed
316
 */
317
static int i8042_kbd_probe(struct udevice *dev)
wdenk's avatar
wdenk committed
318
{
319
	struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
320
	struct i8042_kbd_priv *priv = dev_get_priv(dev);
321
322
323
	struct stdio_dev *sdev = &uc_priv->sdev;
	struct input_config *input = &uc_priv->input;
	int ret;
324

325
	if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
326
327
328
			    "intel,duplicate-por"))
		priv->quirks |= QUIRK_DUP_POR;

329
330
331
332
333
334
335
336
337
338
339
	/* Register the device. i8042_start() will be called soon */
	input->dev = dev;
	input->read_keys = i8042_kbd_check;
	input_allow_repeats(input, true);
	strcpy(sdev->name, "i8042-kbd");
	ret = input_stdio_register(sdev);
	if (ret) {
		debug("%s: input_stdio_register() failed\n", __func__);
		return ret;
	}
	debug("%s: ready\n", __func__);
340

341
	return 0;
wdenk's avatar
wdenk committed
342
343
}

344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
static const struct keyboard_ops i8042_kbd_ops = {
	.start	= i8042_start,
	.update_leds	= i8042_kbd_update_leds,
};

static const struct udevice_id i8042_kbd_ids[] = {
	{ .compatible = "intel,i8042-keyboard" },
	{ }
};

U_BOOT_DRIVER(i8042_kbd) = {
	.name	= "i8042_kbd",
	.id	= UCLASS_KEYBOARD,
	.of_match = i8042_kbd_ids,
	.probe = i8042_kbd_probe,
359
	.remove = i8042_kbd_remove,
360
	.ops	= &i8042_kbd_ops,
361
	.priv_auto	= sizeof(struct i8042_kbd_priv),
362
};