wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 ELTEC Elektronik AG |
| 3 | * Frank Gottschling <fgottschling@eltec.de> |
| 4 | * |
Wolfgang Denk | 3765b3e | 2013-10-07 13:07:26 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* i8042.c - Intel 8042 keyboard driver routines */ |
| 9 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 10 | #include <common.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 11 | #include <i8042.h> |
Simon Glass | 2ec739d | 2015-11-11 10:05:41 -0700 | [diff] [blame^] | 12 | #include <input.h> |
| 13 | #include <asm/io.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 14 | |
| 15 | /* defines */ |
Bin Meng | 835dd00 | 2015-08-24 01:00:05 -0700 | [diff] [blame] | 16 | #define in8(p) inb(p) |
| 17 | #define out8(p, v) outb(v, p) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 18 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 19 | /* locals */ |
Simon Glass | 2ec739d | 2015-11-11 10:05:41 -0700 | [diff] [blame^] | 20 | static struct input_config config; |
| 21 | static bool extended; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 22 | |
Gabe Black | dd4a5b2 | 2011-11-14 19:24:14 +0000 | [diff] [blame] | 23 | static unsigned char ext_key_map[] = { |
| 24 | 0x1c, /* keypad enter */ |
| 25 | 0x1d, /* right control */ |
| 26 | 0x35, /* keypad slash */ |
| 27 | 0x37, /* print screen */ |
| 28 | 0x38, /* right alt */ |
| 29 | 0x46, /* break */ |
| 30 | 0x47, /* editpad home */ |
| 31 | 0x48, /* editpad up */ |
| 32 | 0x49, /* editpad pgup */ |
| 33 | 0x4b, /* editpad left */ |
| 34 | 0x4d, /* editpad right */ |
| 35 | 0x4f, /* editpad end */ |
| 36 | 0x50, /* editpad dn */ |
| 37 | 0x51, /* editpad pgdn */ |
| 38 | 0x52, /* editpad ins */ |
| 39 | 0x53, /* editpad del */ |
| 40 | 0x00 /* map end */ |
| 41 | }; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 42 | |
Bin Meng | 3928d66 | 2015-08-24 01:00:04 -0700 | [diff] [blame] | 43 | static int kbd_input_empty(void) |
| 44 | { |
Bin Meng | 835dd00 | 2015-08-24 01:00:05 -0700 | [diff] [blame] | 45 | int kbd_timeout = KBD_TIMEOUT * 1000; |
Bin Meng | 3928d66 | 2015-08-24 01:00:04 -0700 | [diff] [blame] | 46 | |
Bin Meng | 835dd00 | 2015-08-24 01:00:05 -0700 | [diff] [blame] | 47 | while ((in8(I8042_STS_REG) & STATUS_IBF) && kbd_timeout--) |
Bin Meng | 3928d66 | 2015-08-24 01:00:04 -0700 | [diff] [blame] | 48 | udelay(1); |
| 49 | |
Bin Meng | 835dd00 | 2015-08-24 01:00:05 -0700 | [diff] [blame] | 50 | return kbd_timeout != -1; |
Bin Meng | 3928d66 | 2015-08-24 01:00:04 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Bin Meng | 835dd00 | 2015-08-24 01:00:05 -0700 | [diff] [blame] | 53 | static int kbd_output_full(void) |
Bin Meng | 3928d66 | 2015-08-24 01:00:04 -0700 | [diff] [blame] | 54 | { |
Bin Meng | 835dd00 | 2015-08-24 01:00:05 -0700 | [diff] [blame] | 55 | int kbd_timeout = KBD_TIMEOUT * 1000; |
Bin Meng | 3928d66 | 2015-08-24 01:00:04 -0700 | [diff] [blame] | 56 | |
Bin Meng | 835dd00 | 2015-08-24 01:00:05 -0700 | [diff] [blame] | 57 | while (((in8(I8042_STS_REG) & STATUS_OBF) == 0) && kbd_timeout--) |
Bin Meng | 3928d66 | 2015-08-24 01:00:04 -0700 | [diff] [blame] | 58 | udelay(1); |
| 59 | |
Bin Meng | 835dd00 | 2015-08-24 01:00:05 -0700 | [diff] [blame] | 60 | return kbd_timeout != -1; |
Bin Meng | 3928d66 | 2015-08-24 01:00:04 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Simon Glass | 2ec739d | 2015-11-11 10:05:41 -0700 | [diff] [blame^] | 63 | static void kbd_led_set(int flags) |
Bin Meng | 3928d66 | 2015-08-24 01:00:04 -0700 | [diff] [blame] | 64 | { |
| 65 | kbd_input_empty(); |
Bin Meng | 835dd00 | 2015-08-24 01:00:05 -0700 | [diff] [blame] | 66 | out8(I8042_DATA_REG, CMD_SET_KBD_LED); |
Bin Meng | 3928d66 | 2015-08-24 01:00:04 -0700 | [diff] [blame] | 67 | kbd_input_empty(); |
Simon Glass | 2ec739d | 2015-11-11 10:05:41 -0700 | [diff] [blame^] | 68 | out8(I8042_DATA_REG, flags & 0x7); |
Bin Meng | 3928d66 | 2015-08-24 01:00:04 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Simon Glass | 31d38ee | 2015-10-18 21:17:19 -0600 | [diff] [blame] | 71 | static int kbd_write(int reg, int value) |
| 72 | { |
| 73 | if (!kbd_input_empty()) |
| 74 | return -1; |
| 75 | out8(reg, value); |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static int kbd_read(int reg) |
| 81 | { |
| 82 | if (!kbd_output_full()) |
| 83 | return -1; |
| 84 | |
| 85 | return in8(reg); |
| 86 | } |
| 87 | |
| 88 | static int kbd_cmd_read(int cmd) |
| 89 | { |
| 90 | if (kbd_write(I8042_CMD_REG, cmd)) |
| 91 | return -1; |
| 92 | |
| 93 | return kbd_read(I8042_DATA_REG); |
| 94 | } |
| 95 | |
| 96 | static int kbd_cmd_write(int cmd, int data) |
| 97 | { |
| 98 | if (kbd_write(I8042_CMD_REG, cmd)) |
| 99 | return -1; |
| 100 | |
| 101 | return kbd_write(I8042_DATA_REG, data); |
| 102 | } |
| 103 | |
Bin Meng | 3928d66 | 2015-08-24 01:00:04 -0700 | [diff] [blame] | 104 | static int kbd_reset(void) |
| 105 | { |
Simon Glass | 31d38ee | 2015-10-18 21:17:19 -0600 | [diff] [blame] | 106 | int config; |
Bin Meng | 7d96166 | 2015-08-24 01:00:06 -0700 | [diff] [blame] | 107 | |
| 108 | /* controller self test */ |
Simon Glass | 31d38ee | 2015-10-18 21:17:19 -0600 | [diff] [blame] | 109 | if (kbd_cmd_read(CMD_SELF_TEST) != KBC_TEST_OK) |
Simon Glass | 4f087ba | 2015-10-18 21:17:20 -0600 | [diff] [blame] | 110 | goto err; |
Bin Meng | 3928d66 | 2015-08-24 01:00:04 -0700 | [diff] [blame] | 111 | |
Bin Meng | 7d96166 | 2015-08-24 01:00:06 -0700 | [diff] [blame] | 112 | /* keyboard reset */ |
Simon Glass | 31d38ee | 2015-10-18 21:17:19 -0600 | [diff] [blame] | 113 | if (kbd_write(I8042_DATA_REG, CMD_RESET_KBD) || |
| 114 | kbd_read(I8042_DATA_REG) != KBD_ACK || |
| 115 | kbd_read(I8042_DATA_REG) != KBD_POR) |
Simon Glass | 4f087ba | 2015-10-18 21:17:20 -0600 | [diff] [blame] | 116 | goto err; |
Bin Meng | 3928d66 | 2015-08-24 01:00:04 -0700 | [diff] [blame] | 117 | |
Bin Meng | 7d96166 | 2015-08-24 01:00:06 -0700 | [diff] [blame] | 118 | /* set AT translation and disable irq */ |
Simon Glass | 31d38ee | 2015-10-18 21:17:19 -0600 | [diff] [blame] | 119 | config = kbd_cmd_read(CMD_RD_CONFIG); |
| 120 | if (config == -1) |
Simon Glass | 4f087ba | 2015-10-18 21:17:20 -0600 | [diff] [blame] | 121 | goto err; |
Simon Glass | 31d38ee | 2015-10-18 21:17:19 -0600 | [diff] [blame] | 122 | |
Bin Meng | 7d96166 | 2015-08-24 01:00:06 -0700 | [diff] [blame] | 123 | config |= CONFIG_AT_TRANS; |
| 124 | config &= ~(CONFIG_KIRQ_EN | CONFIG_MIRQ_EN); |
Simon Glass | 31d38ee | 2015-10-18 21:17:19 -0600 | [diff] [blame] | 125 | if (kbd_cmd_write(CMD_WR_CONFIG, config)) |
Simon Glass | 4f087ba | 2015-10-18 21:17:20 -0600 | [diff] [blame] | 126 | goto err; |
Bin Meng | 3928d66 | 2015-08-24 01:00:04 -0700 | [diff] [blame] | 127 | |
Bin Meng | 7d96166 | 2015-08-24 01:00:06 -0700 | [diff] [blame] | 128 | /* enable keyboard */ |
Simon Glass | 31d38ee | 2015-10-18 21:17:19 -0600 | [diff] [blame] | 129 | if (kbd_write(I8042_CMD_REG, CMD_KBD_EN) || |
| 130 | !kbd_input_empty()) |
Simon Glass | 4f087ba | 2015-10-18 21:17:20 -0600 | [diff] [blame] | 131 | goto err; |
Bin Meng | 3928d66 | 2015-08-24 01:00:04 -0700 | [diff] [blame] | 132 | |
Bin Meng | 3928d66 | 2015-08-24 01:00:04 -0700 | [diff] [blame] | 133 | return 0; |
Simon Glass | 4f087ba | 2015-10-18 21:17:20 -0600 | [diff] [blame] | 134 | err: |
| 135 | debug("%s: Keyboard failure\n", __func__); |
| 136 | return -1; |
Bin Meng | 3928d66 | 2015-08-24 01:00:04 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Gabe Black | 22e0f5a | 2011-11-14 20:18:12 +0000 | [diff] [blame] | 139 | static int kbd_controller_present(void) |
| 140 | { |
Bin Meng | 835dd00 | 2015-08-24 01:00:05 -0700 | [diff] [blame] | 141 | return in8(I8042_STS_REG) != 0xff; |
Gabe Black | 22e0f5a | 2011-11-14 20:18:12 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Gabe Black | 48edb30 | 2012-10-12 14:02:02 +0000 | [diff] [blame] | 144 | /* |
| 145 | * Implement a weak default function for boards that optionally |
| 146 | * need to skip the i8042 initialization. |
| 147 | */ |
| 148 | int __weak board_i8042_skip(void) |
| 149 | { |
| 150 | /* As default, don't skip */ |
| 151 | return 0; |
| 152 | } |
| 153 | |
Louis Yung-Chieh Lo | 45fe668 | 2012-10-11 15:15:51 +0000 | [diff] [blame] | 154 | void i8042_flush(void) |
| 155 | { |
| 156 | int timeout; |
| 157 | |
| 158 | /* |
Bin Meng | 835dd00 | 2015-08-24 01:00:05 -0700 | [diff] [blame] | 159 | * The delay is to give the keyboard controller some time |
| 160 | * to fill the next byte. |
Louis Yung-Chieh Lo | 45fe668 | 2012-10-11 15:15:51 +0000 | [diff] [blame] | 161 | */ |
| 162 | while (1) { |
Bin Meng | 835dd00 | 2015-08-24 01:00:05 -0700 | [diff] [blame] | 163 | timeout = 100; /* wait for no longer than 100us */ |
| 164 | while (timeout > 0 && !(in8(I8042_STS_REG) & STATUS_OBF)) { |
Louis Yung-Chieh Lo | 45fe668 | 2012-10-11 15:15:51 +0000 | [diff] [blame] | 165 | udelay(1); |
| 166 | timeout--; |
| 167 | } |
| 168 | |
Bin Meng | 835dd00 | 2015-08-24 01:00:05 -0700 | [diff] [blame] | 169 | /* Try to pull next byte if not timeout */ |
| 170 | if (in8(I8042_STS_REG) & STATUS_OBF) |
Louis Yung-Chieh Lo | 45fe668 | 2012-10-11 15:15:51 +0000 | [diff] [blame] | 171 | in8(I8042_DATA_REG); |
| 172 | else |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | int i8042_disable(void) |
| 178 | { |
| 179 | if (kbd_input_empty() == 0) |
| 180 | return -1; |
| 181 | |
| 182 | /* Disable keyboard */ |
Bin Meng | 835dd00 | 2015-08-24 01:00:05 -0700 | [diff] [blame] | 183 | out8(I8042_CMD_REG, CMD_KBD_DIS); |
Louis Yung-Chieh Lo | 45fe668 | 2012-10-11 15:15:51 +0000 | [diff] [blame] | 184 | |
| 185 | if (kbd_input_empty() == 0) |
| 186 | return -1; |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
Simon Glass | 2ec739d | 2015-11-11 10:05:41 -0700 | [diff] [blame^] | 191 | static int i8042_kbd_check(struct input_config *input) |
| 192 | { |
| 193 | if ((in8(I8042_STS_REG) & STATUS_OBF) == 0) { |
| 194 | return 0; |
| 195 | } else { |
| 196 | bool release = false; |
| 197 | int scan_code; |
| 198 | int i; |
| 199 | |
| 200 | scan_code = in8(I8042_DATA_REG); |
| 201 | if (scan_code == 0xfa) { |
| 202 | return 0; |
| 203 | } else if (scan_code == 0xe0) { |
| 204 | extended = true; |
| 205 | return 0; |
| 206 | } |
| 207 | if (scan_code & 0x80) { |
| 208 | scan_code &= 0x7f; |
| 209 | release = true; |
| 210 | } |
| 211 | if (extended) { |
| 212 | extended = false; |
| 213 | for (i = 0; ext_key_map[i]; i++) { |
| 214 | if (ext_key_map[i] == scan_code) { |
| 215 | scan_code = 0x60 + i; |
| 216 | break; |
| 217 | } |
| 218 | } |
| 219 | /* not found ? */ |
| 220 | if (!ext_key_map[i]) |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | input_add_keycode(&config, scan_code, release); |
| 225 | return 1; |
| 226 | } |
| 227 | } |
| 228 | |
Bin Meng | 835dd00 | 2015-08-24 01:00:05 -0700 | [diff] [blame] | 229 | /* i8042_kbd_init - reset keyboard and init state flags */ |
Gabe Black | dd4a5b2 | 2011-11-14 19:24:14 +0000 | [diff] [blame] | 230 | int i8042_kbd_init(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 231 | { |
Gabe Black | dd4a5b2 | 2011-11-14 19:24:14 +0000 | [diff] [blame] | 232 | int keymap, try; |
| 233 | char *penv; |
Simon Glass | 2ec739d | 2015-11-11 10:05:41 -0700 | [diff] [blame^] | 234 | int ret; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 235 | |
Bin Meng | 835dd00 | 2015-08-24 01:00:05 -0700 | [diff] [blame] | 236 | if (!kbd_controller_present() || board_i8042_skip()) { |
| 237 | debug("i8042 keyboard controller is not present\n"); |
Gabe Black | 22e0f5a | 2011-11-14 20:18:12 +0000 | [diff] [blame] | 238 | return -1; |
Bin Meng | 835dd00 | 2015-08-24 01:00:05 -0700 | [diff] [blame] | 239 | } |
Gabe Black | 22e0f5a | 2011-11-14 20:18:12 +0000 | [diff] [blame] | 240 | |
Gabe Black | dd4a5b2 | 2011-11-14 19:24:14 +0000 | [diff] [blame] | 241 | /* Init keyboard device (default US layout) */ |
| 242 | keymap = KBD_US; |
| 243 | penv = getenv("keymap"); |
| 244 | if (penv != NULL) { |
| 245 | if (strncmp(penv, "de", 3) == 0) |
| 246 | keymap = KBD_GER; |
| 247 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 248 | |
Simon Glass | c5d257f | 2015-10-18 21:17:21 -0600 | [diff] [blame] | 249 | for (try = 0; kbd_reset() != 0; try++) { |
| 250 | if (try >= KBD_RESET_TRIES) |
| 251 | return -1; |
Gabe Black | dd4a5b2 | 2011-11-14 19:24:14 +0000 | [diff] [blame] | 252 | } |
Bin Meng | 835dd00 | 2015-08-24 01:00:05 -0700 | [diff] [blame] | 253 | |
Simon Glass | 2ec739d | 2015-11-11 10:05:41 -0700 | [diff] [blame^] | 254 | ret = input_init(&config, keymap == KBD_GER); |
| 255 | if (ret) |
| 256 | return ret; |
| 257 | config.read_keys = i8042_kbd_check; |
| 258 | input_allow_repeats(&config, true); |
| 259 | |
| 260 | kbd_led_set(NORMAL); |
Simon Glass | c5d257f | 2015-10-18 21:17:21 -0600 | [diff] [blame] | 261 | |
| 262 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Simon Glass | 2ec739d | 2015-11-11 10:05:41 -0700 | [diff] [blame^] | 265 | /** |
| 266 | * check_leds() - Check the keyboard LEDs and update them it needed |
Bin Meng | 835dd00 | 2015-08-24 01:00:05 -0700 | [diff] [blame] | 267 | * |
Simon Glass | 2ec739d | 2015-11-11 10:05:41 -0700 | [diff] [blame^] | 268 | * @ret: Value to return |
| 269 | * @return value of @ret |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 270 | */ |
Simon Glass | 2ec739d | 2015-11-11 10:05:41 -0700 | [diff] [blame^] | 271 | static int check_leds(int ret) |
| 272 | { |
| 273 | int leds; |
| 274 | |
| 275 | leds = input_leds_changed(&config); |
| 276 | if (leds >= 0) |
| 277 | kbd_led_set(leds); |
| 278 | |
| 279 | return ret; |
| 280 | } |
| 281 | |
| 282 | /* i8042_tstc - test if keyboard input is available */ |
Simon Glass | 709ea54 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 283 | int i8042_tstc(struct stdio_dev *dev) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 284 | { |
Simon Glass | 2ec739d | 2015-11-11 10:05:41 -0700 | [diff] [blame^] | 285 | return check_leds(input_tstc(&config)); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Simon Glass | 2ec739d | 2015-11-11 10:05:41 -0700 | [diff] [blame^] | 288 | /* i8042_getc - wait till keyboard input is available */ |
Simon Glass | 709ea54 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 289 | int i8042_getc(struct stdio_dev *dev) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 290 | { |
Simon Glass | 2ec739d | 2015-11-11 10:05:41 -0700 | [diff] [blame^] | 291 | return check_leds(input_getc(&config)); |
Gabe Black | dd4a5b2 | 2011-11-14 19:24:14 +0000 | [diff] [blame] | 292 | } |