i8042.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2002 ELTEC Elektronik AG
  4. * Frank Gottschling <fgottschling@eltec.de>
  5. */
  6. /* i8042.c - Intel 8042 keyboard driver routines */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <env.h>
  10. #include <errno.h>
  11. #include <i8042.h>
  12. #include <input.h>
  13. #include <keyboard.h>
  14. #include <log.h>
  15. #include <asm/io.h>
  16. #include <linux/delay.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. /* defines */
  19. #define in8(p) inb(p)
  20. #define out8(p, v) outb(v, p)
  21. enum {
  22. QUIRK_DUP_POR = 1 << 0,
  23. };
  24. /* locals */
  25. struct i8042_kbd_priv {
  26. bool extended; /* true if an extended keycode is expected next */
  27. int quirks; /* quirks that we support */
  28. };
  29. static unsigned char ext_key_map[] = {
  30. 0x1c, /* keypad enter */
  31. 0x1d, /* right control */
  32. 0x35, /* keypad slash */
  33. 0x37, /* print screen */
  34. 0x38, /* right alt */
  35. 0x46, /* break */
  36. 0x47, /* editpad home */
  37. 0x48, /* editpad up */
  38. 0x49, /* editpad pgup */
  39. 0x4b, /* editpad left */
  40. 0x4d, /* editpad right */
  41. 0x4f, /* editpad end */
  42. 0x50, /* editpad dn */
  43. 0x51, /* editpad pgdn */
  44. 0x52, /* editpad ins */
  45. 0x53, /* editpad del */
  46. 0x00 /* map end */
  47. };
  48. static int kbd_input_empty(void)
  49. {
  50. int kbd_timeout = KBD_TIMEOUT * 1000;
  51. while ((in8(I8042_STS_REG) & STATUS_IBF) && kbd_timeout--)
  52. udelay(1);
  53. return kbd_timeout != -1;
  54. }
  55. static int kbd_output_full(void)
  56. {
  57. int kbd_timeout = KBD_TIMEOUT * 1000;
  58. while (((in8(I8042_STS_REG) & STATUS_OBF) == 0) && kbd_timeout--)
  59. udelay(1);
  60. return kbd_timeout != -1;
  61. }
  62. /**
  63. * check_leds() - Check the keyboard LEDs and update them it needed
  64. *
  65. * @ret: Value to return
  66. * @return value of @ret
  67. */
  68. static int i8042_kbd_update_leds(struct udevice *dev, int leds)
  69. {
  70. kbd_input_empty();
  71. out8(I8042_DATA_REG, CMD_SET_KBD_LED);
  72. kbd_input_empty();
  73. out8(I8042_DATA_REG, leds & 0x7);
  74. return 0;
  75. }
  76. static int kbd_write(int reg, int value)
  77. {
  78. if (!kbd_input_empty())
  79. return -1;
  80. out8(reg, value);
  81. return 0;
  82. }
  83. static int kbd_read(int reg)
  84. {
  85. if (!kbd_output_full())
  86. return -1;
  87. return in8(reg);
  88. }
  89. static int kbd_cmd_read(int cmd)
  90. {
  91. if (kbd_write(I8042_CMD_REG, cmd))
  92. return -1;
  93. return kbd_read(I8042_DATA_REG);
  94. }
  95. static int kbd_cmd_write(int cmd, int data)
  96. {
  97. if (kbd_write(I8042_CMD_REG, cmd))
  98. return -1;
  99. return kbd_write(I8042_DATA_REG, data);
  100. }
  101. static int kbd_reset(int quirk)
  102. {
  103. int config;
  104. /* controller self test */
  105. if (kbd_cmd_read(CMD_SELF_TEST) != KBC_TEST_OK)
  106. goto err;
  107. /* keyboard reset */
  108. if (kbd_write(I8042_DATA_REG, CMD_RESET_KBD) ||
  109. kbd_read(I8042_DATA_REG) != KBD_ACK ||
  110. kbd_read(I8042_DATA_REG) != KBD_POR)
  111. goto err;
  112. if (kbd_write(I8042_DATA_REG, CMD_DRAIN_OUTPUT) ||
  113. kbd_read(I8042_DATA_REG) != KBD_ACK)
  114. goto err;
  115. /* set AT translation and disable irq */
  116. config = kbd_cmd_read(CMD_RD_CONFIG);
  117. if (config == -1)
  118. goto err;
  119. /* Sometimes get a second byte */
  120. else if ((quirk & QUIRK_DUP_POR) && config == KBD_POR)
  121. config = kbd_cmd_read(CMD_RD_CONFIG);
  122. config |= CONFIG_AT_TRANS;
  123. config &= ~(CONFIG_KIRQ_EN | CONFIG_MIRQ_EN);
  124. if (kbd_cmd_write(CMD_WR_CONFIG, config))
  125. goto err;
  126. /* enable keyboard */
  127. if (kbd_write(I8042_CMD_REG, CMD_KBD_EN) ||
  128. !kbd_input_empty())
  129. goto err;
  130. return 0;
  131. err:
  132. debug("%s: Keyboard failure\n", __func__);
  133. return -1;
  134. }
  135. static int kbd_controller_present(void)
  136. {
  137. return in8(I8042_STS_REG) != 0xff;
  138. }
  139. /** Flush all buffer from keyboard controller to host*/
  140. static void i8042_flush(void)
  141. {
  142. int timeout;
  143. /*
  144. * The delay is to give the keyboard controller some time
  145. * to fill the next byte.
  146. */
  147. while (1) {
  148. timeout = 100; /* wait for no longer than 100us */
  149. while (timeout > 0 && !(in8(I8042_STS_REG) & STATUS_OBF)) {
  150. udelay(1);
  151. timeout--;
  152. }
  153. /* Try to pull next byte if not timeout */
  154. if (in8(I8042_STS_REG) & STATUS_OBF)
  155. in8(I8042_DATA_REG);
  156. else
  157. break;
  158. }
  159. }
  160. /**
  161. * Disables the keyboard so that key strokes no longer generate scancodes to
  162. * the host.
  163. *
  164. * @return 0 if ok, -1 if keyboard input was found while disabling
  165. */
  166. static int i8042_disable(void)
  167. {
  168. if (kbd_input_empty() == 0)
  169. return -1;
  170. /* Disable keyboard */
  171. out8(I8042_CMD_REG, CMD_KBD_DIS);
  172. if (kbd_input_empty() == 0)
  173. return -1;
  174. return 0;
  175. }
  176. static int i8042_kbd_check(struct input_config *input)
  177. {
  178. struct i8042_kbd_priv *priv = dev_get_priv(input->dev);
  179. if ((in8(I8042_STS_REG) & STATUS_OBF) == 0) {
  180. return 0;
  181. } else {
  182. bool release = false;
  183. int scan_code;
  184. int i;
  185. scan_code = in8(I8042_DATA_REG);
  186. if (scan_code == 0xfa) {
  187. return 0;
  188. } else if (scan_code == 0xe0) {
  189. priv->extended = true;
  190. return 0;
  191. }
  192. if (scan_code & 0x80) {
  193. scan_code &= 0x7f;
  194. release = true;
  195. }
  196. if (priv->extended) {
  197. priv->extended = false;
  198. for (i = 0; ext_key_map[i]; i++) {
  199. if (ext_key_map[i] == scan_code) {
  200. scan_code = 0x60 + i;
  201. break;
  202. }
  203. }
  204. /* not found ? */
  205. if (!ext_key_map[i])
  206. return 0;
  207. }
  208. input_add_keycode(input, scan_code, release);
  209. return 1;
  210. }
  211. }
  212. /* i8042_kbd_init - reset keyboard and init state flags */
  213. static int i8042_start(struct udevice *dev)
  214. {
  215. struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
  216. struct i8042_kbd_priv *priv = dev_get_priv(dev);
  217. struct input_config *input = &uc_priv->input;
  218. int keymap, try;
  219. char *penv;
  220. int ret;
  221. if (!kbd_controller_present()) {
  222. debug("i8042 keyboard controller is not present\n");
  223. return -ENOENT;
  224. }
  225. /* Init keyboard device (default US layout) */
  226. keymap = KBD_US;
  227. penv = env_get("keymap");
  228. if (penv != NULL) {
  229. if (strncmp(penv, "de", 3) == 0)
  230. keymap = KBD_GER;
  231. }
  232. for (try = 0; kbd_reset(priv->quirks) != 0; try++) {
  233. if (try >= KBD_RESET_TRIES)
  234. return -1;
  235. }
  236. ret = input_add_tables(input, keymap == KBD_GER);
  237. if (ret)
  238. return ret;
  239. i8042_kbd_update_leds(dev, NORMAL);
  240. debug("%s: started\n", __func__);
  241. return 0;
  242. }
  243. static int i8042_kbd_remove(struct udevice *dev)
  244. {
  245. if (i8042_disable())
  246. log_debug("i8042_disable() failed. fine, continue.\n");
  247. i8042_flush();
  248. return 0;
  249. }
  250. /**
  251. * Set up the i8042 keyboard. This is called by the stdio device handler
  252. *
  253. * We want to do this init when the keyboard is actually used rather than
  254. * at start-up, since keyboard input may not currently be selected.
  255. *
  256. * Once the keyboard starts there will be a period during which we must
  257. * wait for the keyboard to init. We do this only when a key is first
  258. * read - see kbd_wait_for_fifo_init().
  259. *
  260. * @return 0 if ok, -ve on error
  261. */
  262. static int i8042_kbd_probe(struct udevice *dev)
  263. {
  264. struct keyboard_priv *uc_priv = dev_get_uclass_priv(dev);
  265. struct i8042_kbd_priv *priv = dev_get_priv(dev);
  266. struct stdio_dev *sdev = &uc_priv->sdev;
  267. struct input_config *input = &uc_priv->input;
  268. int ret;
  269. if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
  270. "intel,duplicate-por"))
  271. priv->quirks |= QUIRK_DUP_POR;
  272. /* Register the device. i8042_start() will be called soon */
  273. input->dev = dev;
  274. input->read_keys = i8042_kbd_check;
  275. input_allow_repeats(input, true);
  276. strcpy(sdev->name, "i8042-kbd");
  277. ret = input_stdio_register(sdev);
  278. if (ret) {
  279. debug("%s: input_stdio_register() failed\n", __func__);
  280. return ret;
  281. }
  282. debug("%s: ready\n", __func__);
  283. return 0;
  284. }
  285. static const struct keyboard_ops i8042_kbd_ops = {
  286. .start = i8042_start,
  287. .update_leds = i8042_kbd_update_leds,
  288. };
  289. static const struct udevice_id i8042_kbd_ids[] = {
  290. { .compatible = "intel,i8042-keyboard" },
  291. { }
  292. };
  293. U_BOOT_DRIVER(i8042_kbd) = {
  294. .name = "i8042_kbd",
  295. .id = UCLASS_KEYBOARD,
  296. .of_match = i8042_kbd_ids,
  297. .probe = i8042_kbd_probe,
  298. .remove = i8042_kbd_remove,
  299. .ops = &i8042_kbd_ops,
  300. .priv_auto_alloc_size = sizeof(struct i8042_kbd_priv),
  301. };