i8042.c 7.4 KB

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