i8042.c 8.0 KB

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