blob: 9a5dc46207c4664d8e90b82bac59ca672b2f4b9c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00002/*
3 * (C) Copyright 2002 ELTEC Elektronik AG
4 * Frank Gottschling <fgottschling@eltec.de>
wdenkc6097192002-11-03 00:24:07 +00005 */
6
7/* i8042.c - Intel 8042 keyboard driver routines */
8
wdenkc6097192002-11-03 00:24:07 +00009#include <common.h>
Simon Glassfa0a42a2015-11-11 10:05:45 -070010#include <dm.h>
11#include <errno.h>
wdenkc6097192002-11-03 00:24:07 +000012#include <i8042.h>
Simon Glasse1a77bb2015-11-11 10:05:41 -070013#include <input.h>
Simon Glassfa0a42a2015-11-11 10:05:45 -070014#include <keyboard.h>
Simon Glasse1a77bb2015-11-11 10:05:41 -070015#include <asm/io.h>
wdenkc6097192002-11-03 00:24:07 +000016
Simon Glass708c8312015-11-11 10:05:46 -070017DECLARE_GLOBAL_DATA_PTR;
18
wdenkc6097192002-11-03 00:24:07 +000019/* defines */
Bin Mengbc823082015-08-24 01:00:05 -070020#define in8(p) inb(p)
21#define out8(p, v) outb(v, p)
wdenkc6097192002-11-03 00:24:07 +000022
Simon Glass708c8312015-11-11 10:05:46 -070023enum {
24 QUIRK_DUP_POR = 1 << 0,
25};
26
wdenkc6097192002-11-03 00:24:07 +000027/* locals */
Simon Glassfa0a42a2015-11-11 10:05:45 -070028struct i8042_kbd_priv {
29 bool extended; /* true if an extended keycode is expected next */
Simon Glass708c8312015-11-11 10:05:46 -070030 int quirks; /* quirks that we support */
Simon Glassfa0a42a2015-11-11 10:05:45 -070031};
wdenkc6097192002-11-03 00:24:07 +000032
Gabe Black76a5d322011-11-14 19:24:14 +000033static 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 };
wdenkc6097192002-11-03 00:24:07 +000052
Bin Mengc89f2512015-08-24 01:00:04 -070053static int kbd_input_empty(void)
54{
Bin Mengbc823082015-08-24 01:00:05 -070055 int kbd_timeout = KBD_TIMEOUT * 1000;
Bin Mengc89f2512015-08-24 01:00:04 -070056
Bin Mengbc823082015-08-24 01:00:05 -070057 while ((in8(I8042_STS_REG) & STATUS_IBF) && kbd_timeout--)
Bin Mengc89f2512015-08-24 01:00:04 -070058 udelay(1);
59
Bin Mengbc823082015-08-24 01:00:05 -070060 return kbd_timeout != -1;
Bin Mengc89f2512015-08-24 01:00:04 -070061}
62
Bin Mengbc823082015-08-24 01:00:05 -070063static int kbd_output_full(void)
Bin Mengc89f2512015-08-24 01:00:04 -070064{
Bin Mengbc823082015-08-24 01:00:05 -070065 int kbd_timeout = KBD_TIMEOUT * 1000;
Bin Mengc89f2512015-08-24 01:00:04 -070066
Bin Mengbc823082015-08-24 01:00:05 -070067 while (((in8(I8042_STS_REG) & STATUS_OBF) == 0) && kbd_timeout--)
Bin Mengc89f2512015-08-24 01:00:04 -070068 udelay(1);
69
Bin Mengbc823082015-08-24 01:00:05 -070070 return kbd_timeout != -1;
Bin Mengc89f2512015-08-24 01:00:04 -070071}
72
Simon Glassfa0a42a2015-11-11 10:05:45 -070073/**
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)
Bin Mengc89f2512015-08-24 01:00:04 -070080{
81 kbd_input_empty();
Bin Mengbc823082015-08-24 01:00:05 -070082 out8(I8042_DATA_REG, CMD_SET_KBD_LED);
Bin Mengc89f2512015-08-24 01:00:04 -070083 kbd_input_empty();
Simon Glassfa0a42a2015-11-11 10:05:45 -070084 out8(I8042_DATA_REG, leds & 0x7);
85
86 return 0;
Bin Mengc89f2512015-08-24 01:00:04 -070087}
88
Simon Glass4b48eaf2015-10-18 21:17:19 -060089static int kbd_write(int reg, int value)
Bin Mengc89f2512015-08-24 01:00:04 -070090{
Simon Glass4b48eaf2015-10-18 21:17:19 -060091 if (!kbd_input_empty())
Bin Mengef4d7852015-08-24 01:00:06 -070092 return -1;
Simon Glass4b48eaf2015-10-18 21:17:19 -060093 out8(reg, value);
Bin Mengc89f2512015-08-24 01:00:04 -070094
Simon Glass4b48eaf2015-10-18 21:17:19 -060095 return 0;
96}
97
98static int kbd_read(int reg)
99{
100 if (!kbd_output_full())
Bin Mengef4d7852015-08-24 01:00:06 -0700101 return -1;
Simon Glass4b48eaf2015-10-18 21:17:19 -0600102
103 return in8(reg);
104}
105
106static int kbd_cmd_read(int cmd)
107{
108 if (kbd_write(I8042_CMD_REG, cmd))
Bin Mengc89f2512015-08-24 01:00:04 -0700109 return -1;
Simon Glass4b48eaf2015-10-18 21:17:19 -0600110
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))
Bin Mengc89f2512015-08-24 01:00:04 -0700117 return -1;
Simon Glass4b48eaf2015-10-18 21:17:19 -0600118
119 return kbd_write(I8042_DATA_REG, data);
120}
121
Simon Glass708c8312015-11-11 10:05:46 -0700122static int kbd_reset(int quirk)
Simon Glass4b48eaf2015-10-18 21:17:19 -0600123{
124 int config;
125
126 /* controller self test */
127 if (kbd_cmd_read(CMD_SELF_TEST) != KBC_TEST_OK)
Simon Glass4f58bbc2015-10-18 21:17:20 -0600128 goto err;
Simon Glass4b48eaf2015-10-18 21:17:19 -0600129
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)
Simon Glass4f58bbc2015-10-18 21:17:20 -0600134 goto err;
Bin Mengc89f2512015-08-24 01:00:04 -0700135
Simon Glass0a7fb2c2016-03-11 22:06:50 -0700136 if (kbd_write(I8042_DATA_REG, CMD_DRAIN_OUTPUT) ||
137 kbd_read(I8042_DATA_REG) != KBD_ACK)
138 goto err;
139
Bin Mengef4d7852015-08-24 01:00:06 -0700140 /* set AT translation and disable irq */
Simon Glass4b48eaf2015-10-18 21:17:19 -0600141 config = kbd_cmd_read(CMD_RD_CONFIG);
142 if (config == -1)
Simon Glass4f58bbc2015-10-18 21:17:20 -0600143 goto err;
Simon Glass4b48eaf2015-10-18 21:17:19 -0600144
Simon Glass708c8312015-11-11 10:05:46 -0700145 /* Sometimes get a second byte */
146 else if ((quirk & QUIRK_DUP_POR) && config == KBD_POR)
147 config = kbd_cmd_read(CMD_RD_CONFIG);
148
Bin Mengef4d7852015-08-24 01:00:06 -0700149 config |= CONFIG_AT_TRANS;
150 config &= ~(CONFIG_KIRQ_EN | CONFIG_MIRQ_EN);
Simon Glass4b48eaf2015-10-18 21:17:19 -0600151 if (kbd_cmd_write(CMD_WR_CONFIG, config))
Simon Glass4f58bbc2015-10-18 21:17:20 -0600152 goto err;
Bin Mengc89f2512015-08-24 01:00:04 -0700153
Bin Mengef4d7852015-08-24 01:00:06 -0700154 /* enable keyboard */
Simon Glass4b48eaf2015-10-18 21:17:19 -0600155 if (kbd_write(I8042_CMD_REG, CMD_KBD_EN) ||
156 !kbd_input_empty())
Simon Glass4f58bbc2015-10-18 21:17:20 -0600157 goto err;
Bin Mengc89f2512015-08-24 01:00:04 -0700158
159 return 0;
Simon Glass4f58bbc2015-10-18 21:17:20 -0600160err:
161 debug("%s: Keyboard failure\n", __func__);
162 return -1;
Bin Mengc89f2512015-08-24 01:00:04 -0700163}
164
Gabe Black15bbe8d2011-11-14 20:18:12 +0000165static int kbd_controller_present(void)
166{
Bin Mengbc823082015-08-24 01:00:05 -0700167 return in8(I8042_STS_REG) != 0xff;
Gabe Black15bbe8d2011-11-14 20:18:12 +0000168}
169
Simon Glass5a6e8fd2018-11-23 21:29:38 -0700170/** Flush all buffer from keyboard controller to host*/
171static void i8042_flush(void)
Louis Yung-Chieh Lo46d579c2012-10-11 15:15:51 +0000172{
173 int timeout;
174
175 /*
Bin Mengbc823082015-08-24 01:00:05 -0700176 * The delay is to give the keyboard controller some time
177 * to fill the next byte.
Louis Yung-Chieh Lo46d579c2012-10-11 15:15:51 +0000178 */
179 while (1) {
Bin Mengbc823082015-08-24 01:00:05 -0700180 timeout = 100; /* wait for no longer than 100us */
181 while (timeout > 0 && !(in8(I8042_STS_REG) & STATUS_OBF)) {
Louis Yung-Chieh Lo46d579c2012-10-11 15:15:51 +0000182 udelay(1);
183 timeout--;
184 }
185
Bin Mengbc823082015-08-24 01:00:05 -0700186 /* Try to pull next byte if not timeout */
187 if (in8(I8042_STS_REG) & STATUS_OBF)
Louis Yung-Chieh Lo46d579c2012-10-11 15:15:51 +0000188 in8(I8042_DATA_REG);
189 else
190 break;
191 }
192}
193
Simon Glass5a6e8fd2018-11-23 21:29:38 -0700194/**
195 * Disables the keyboard so that key strokes no longer generate scancodes to
196 * the host.
197 *
198 * @return 0 if ok, -1 if keyboard input was found while disabling
199 */
200static int i8042_disable(void)
Louis Yung-Chieh Lo46d579c2012-10-11 15:15:51 +0000201{
202 if (kbd_input_empty() == 0)
203 return -1;
204
205 /* Disable keyboard */
Bin Mengbc823082015-08-24 01:00:05 -0700206 out8(I8042_CMD_REG, CMD_KBD_DIS);
Louis Yung-Chieh Lo46d579c2012-10-11 15:15:51 +0000207
208 if (kbd_input_empty() == 0)
209 return -1;
210
211 return 0;
212}
213
Simon Glasse1a77bb2015-11-11 10:05:41 -0700214static int i8042_kbd_check(struct input_config *input)
215{
Simon Glassfa0a42a2015-11-11 10:05:45 -0700216 struct i8042_kbd_priv *priv = dev_get_priv(input->dev);
217
Simon Glasse1a77bb2015-11-11 10:05:41 -0700218 if ((in8(I8042_STS_REG) & STATUS_OBF) == 0) {
219 return 0;
220 } else {
221 bool release = false;
222 int scan_code;
223 int i;
224
225 scan_code = in8(I8042_DATA_REG);
226 if (scan_code == 0xfa) {
227 return 0;
228 } else if (scan_code == 0xe0) {
Simon Glassfa0a42a2015-11-11 10:05:45 -0700229 priv->extended = true;
Simon Glasse1a77bb2015-11-11 10:05:41 -0700230 return 0;
231 }
232 if (scan_code & 0x80) {
233 scan_code &= 0x7f;
234 release = true;
235 }
Simon Glassfa0a42a2015-11-11 10:05:45 -0700236 if (priv->extended) {
237 priv->extended = false;
Simon Glasse1a77bb2015-11-11 10:05:41 -0700238 for (i = 0; ext_key_map[i]; i++) {
239 if (ext_key_map[i] == scan_code) {
240 scan_code = 0x60 + i;
241 break;
242 }
243 }
244 /* not found ? */
245 if (!ext_key_map[i])
246 return 0;
247 }
248
Simon Glassfa0a42a2015-11-11 10:05:45 -0700249 input_add_keycode(input, scan_code, release);
Simon Glasse1a77bb2015-11-11 10:05:41 -0700250 return 1;
251 }
252}
253
Bin Mengbc823082015-08-24 01:00:05 -0700254/* i8042_kbd_init - reset keyboard and init state flags */
Simon Glassfa0a42a2015-11-11 10:05:45 -0700255static int i8042_start(struct udevice *dev)
wdenkc6097192002-11-03 00:24:07 +0000256{
Simon Glassfa0a42a2015-11-11 10:05:45 -0700257 struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass708c8312015-11-11 10:05:46 -0700258 struct i8042_kbd_priv *priv = dev_get_priv(dev);
Simon Glassfa0a42a2015-11-11 10:05:45 -0700259 struct input_config *input = &uc_priv->input;
Gabe Black76a5d322011-11-14 19:24:14 +0000260 int keymap, try;
261 char *penv;
Simon Glasse1a77bb2015-11-11 10:05:41 -0700262 int ret;
wdenkc6097192002-11-03 00:24:07 +0000263
Simon Glass5a6e8fd2018-11-23 21:29:38 -0700264 if (!kbd_controller_present()) {
Bin Mengbc823082015-08-24 01:00:05 -0700265 debug("i8042 keyboard controller is not present\n");
Simon Glassfa0a42a2015-11-11 10:05:45 -0700266 return -ENOENT;
Bin Mengbc823082015-08-24 01:00:05 -0700267 }
Gabe Black15bbe8d2011-11-14 20:18:12 +0000268
Gabe Black76a5d322011-11-14 19:24:14 +0000269 /* Init keyboard device (default US layout) */
270 keymap = KBD_US;
Simon Glass64b723f2017-08-03 12:22:12 -0600271 penv = env_get("keymap");
Gabe Black76a5d322011-11-14 19:24:14 +0000272 if (penv != NULL) {
273 if (strncmp(penv, "de", 3) == 0)
274 keymap = KBD_GER;
275 }
wdenkc6097192002-11-03 00:24:07 +0000276
Simon Glass708c8312015-11-11 10:05:46 -0700277 for (try = 0; kbd_reset(priv->quirks) != 0; try++) {
Simon Glass2915ad62015-10-18 21:17:21 -0600278 if (try >= KBD_RESET_TRIES)
279 return -1;
Gabe Black76a5d322011-11-14 19:24:14 +0000280 }
Bin Mengbc823082015-08-24 01:00:05 -0700281
Simon Glassfa0a42a2015-11-11 10:05:45 -0700282 ret = input_add_tables(input, keymap == KBD_GER);
Simon Glasse1a77bb2015-11-11 10:05:41 -0700283 if (ret)
284 return ret;
Simon Glasse1a77bb2015-11-11 10:05:41 -0700285
Simon Glassfa0a42a2015-11-11 10:05:45 -0700286 i8042_kbd_update_leds(dev, NORMAL);
287 debug("%s: started\n", __func__);
Simon Glass2915ad62015-10-18 21:17:21 -0600288
289 return 0;
wdenkc6097192002-11-03 00:24:07 +0000290}
291
Simon Glass5a6e8fd2018-11-23 21:29:38 -0700292static int i8042_kbd_remove(struct udevice *dev)
293{
294 if (i8042_disable())
295 log_debug("i8042_disable() failed. fine, continue.\n");
296 i8042_flush();
297
298 return 0;
299}
300
Simon Glasse1a77bb2015-11-11 10:05:41 -0700301/**
Simon Glassfa0a42a2015-11-11 10:05:45 -0700302 * Set up the i8042 keyboard. This is called by the stdio device handler
Bin Mengbc823082015-08-24 01:00:05 -0700303 *
Simon Glassfa0a42a2015-11-11 10:05:45 -0700304 * We want to do this init when the keyboard is actually used rather than
305 * at start-up, since keyboard input may not currently be selected.
306 *
307 * Once the keyboard starts there will be a period during which we must
308 * wait for the keyboard to init. We do this only when a key is first
309 * read - see kbd_wait_for_fifo_init().
310 *
311 * @return 0 if ok, -ve on error
wdenkc6097192002-11-03 00:24:07 +0000312 */
Simon Glassfa0a42a2015-11-11 10:05:45 -0700313static int i8042_kbd_probe(struct udevice *dev)
wdenkc6097192002-11-03 00:24:07 +0000314{
Simon Glassfa0a42a2015-11-11 10:05:45 -0700315 struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass708c8312015-11-11 10:05:46 -0700316 struct i8042_kbd_priv *priv = dev_get_priv(dev);
Simon Glassfa0a42a2015-11-11 10:05:45 -0700317 struct stdio_dev *sdev = &uc_priv->sdev;
318 struct input_config *input = &uc_priv->input;
319 int ret;
wdenkc6097192002-11-03 00:24:07 +0000320
Simon Glassdd79d6e2017-01-17 16:52:55 -0700321 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
Simon Glass708c8312015-11-11 10:05:46 -0700322 "intel,duplicate-por"))
323 priv->quirks |= QUIRK_DUP_POR;
324
Simon Glassfa0a42a2015-11-11 10:05:45 -0700325 /* Register the device. i8042_start() will be called soon */
326 input->dev = dev;
327 input->read_keys = i8042_kbd_check;
328 input_allow_repeats(input, true);
329 strcpy(sdev->name, "i8042-kbd");
330 ret = input_stdio_register(sdev);
331 if (ret) {
332 debug("%s: input_stdio_register() failed\n", __func__);
333 return ret;
334 }
335 debug("%s: ready\n", __func__);
wdenkc6097192002-11-03 00:24:07 +0000336
Simon Glassfa0a42a2015-11-11 10:05:45 -0700337 return 0;
Simon Glasse1a77bb2015-11-11 10:05:41 -0700338}
Bin Mengbc823082015-08-24 01:00:05 -0700339
Simon Glassfa0a42a2015-11-11 10:05:45 -0700340static const struct keyboard_ops i8042_kbd_ops = {
341 .start = i8042_start,
342 .update_leds = i8042_kbd_update_leds,
343};
wdenkc6097192002-11-03 00:24:07 +0000344
Simon Glassfa0a42a2015-11-11 10:05:45 -0700345static const struct udevice_id i8042_kbd_ids[] = {
346 { .compatible = "intel,i8042-keyboard" },
347 { }
348};
349
350U_BOOT_DRIVER(i8042_kbd) = {
351 .name = "i8042_kbd",
352 .id = UCLASS_KEYBOARD,
353 .of_match = i8042_kbd_ids,
354 .probe = i8042_kbd_probe,
Simon Glass5a6e8fd2018-11-23 21:29:38 -0700355 .remove = i8042_kbd_remove,
Simon Glassfa0a42a2015-11-11 10:05:45 -0700356 .ops = &i8042_kbd_ops,
357 .priv_auto_alloc_size = sizeof(struct i8042_kbd_priv),
358};