i8042.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /*
  2. * (C) Copyright 2002 ELTEC Elektronik AG
  3. * Frank Gottschling <fgottschling@eltec.de>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /* i8042.c - Intel 8042 keyboard driver routines */
  8. /* includes */
  9. #include <common.h>
  10. #include <linux/compiler.h>
  11. #ifdef CONFIG_USE_CPCIDVI
  12. extern u8 gt_cpcidvi_in8(u32 offset);
  13. extern void gt_cpcidvi_out8(u32 offset, u8 data);
  14. #define in8(a) gt_cpcidvi_in8(a)
  15. #define out8(a, b) gt_cpcidvi_out8(a, b)
  16. #endif
  17. #include <i8042.h>
  18. /* defines */
  19. #ifdef CONFIG_CONSOLE_CURSOR
  20. extern void console_cursor(int state);
  21. static int blinkCount = CONFIG_SYS_CONSOLE_BLINK_COUNT;
  22. static int cursor_state;
  23. #endif
  24. /* locals */
  25. static int kbd_input = -1; /* no input yet */
  26. static int kbd_mapping = KBD_US; /* default US keyboard */
  27. static int kbd_flags = NORMAL; /* after reset */
  28. static int kbd_state; /* unshift code */
  29. static void kbd_conv_char(unsigned char scan_code);
  30. static void kbd_led_set(void);
  31. static void kbd_normal(unsigned char scan_code);
  32. static void kbd_shift(unsigned char scan_code);
  33. static void kbd_ctrl(unsigned char scan_code);
  34. static void kbd_num(unsigned char scan_code);
  35. static void kbd_caps(unsigned char scan_code);
  36. static void kbd_scroll(unsigned char scan_code);
  37. static void kbd_alt(unsigned char scan_code);
  38. static int kbd_input_empty(void);
  39. static int kbd_reset(void);
  40. static unsigned char kbd_fct_map[144] = {
  41. /* kbd_fct_map table for scan code */
  42. 0, AS, AS, AS, AS, AS, AS, AS, /* scan 0- 7 */
  43. AS, AS, AS, AS, AS, AS, AS, AS, /* scan 8- F */
  44. AS, AS, AS, AS, AS, AS, AS, AS, /* scan 10-17 */
  45. AS, AS, AS, AS, AS, CN, AS, AS, /* scan 18-1F */
  46. AS, AS, AS, AS, AS, AS, AS, AS, /* scan 20-27 */
  47. AS, AS, SH, AS, AS, AS, AS, AS, /* scan 28-2F */
  48. AS, AS, AS, AS, AS, AS, SH, AS, /* scan 30-37 */
  49. AS, AS, CP, 0, 0, 0, 0, 0, /* scan 38-3F */
  50. 0, 0, 0, 0, 0, NM, ST, ES, /* scan 40-47 */
  51. ES, ES, ES, ES, ES, ES, ES, ES, /* scan 48-4F */
  52. ES, ES, ES, ES, 0, 0, AS, 0, /* scan 50-57 */
  53. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
  54. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
  55. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
  56. AS, 0, 0, AS, 0, 0, AS, 0, /* scan 70-77 */
  57. 0, AS, 0, 0, 0, AS, 0, 0, /* scan 78-7F */
  58. AS, CN, AS, AS, AK, ST, EX, EX, /* enhanced */
  59. AS, EX, EX, AS, EX, AS, EX, EX /* enhanced */
  60. };
  61. static unsigned char kbd_key_map[2][5][144] = {
  62. { /* US keyboard */
  63. { /* unshift code */
  64. 0, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 0- 7 */
  65. '7', '8', '9', '0', '-', '=', 0x08, '\t', /* scan 8- F */
  66. 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', /* scan 10-17 */
  67. 'o', 'p', '[', ']', '\r', CN, 'a', 's', /* scan 18-1F */
  68. 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* scan 20-27 */
  69. '\'', '`', SH, '\\', 'z', 'x', 'c', 'v', /* scan 28-2F */
  70. 'b', 'n', 'm', ',', '.', '/', SH, '*', /* scan 30-37 */
  71. ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
  72. 0, 0, 0, 0, 0, NM, ST, '7', /* scan 40-47 */
  73. '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
  74. '2', '3', '0', '.', 0, 0, 0, 0, /* scan 50-57 */
  75. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
  76. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
  77. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
  78. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
  79. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
  80. '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
  81. 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
  82. },
  83. { /* shift code */
  84. 0, 0x1b, '!', '@', '#', '$', '%', '^', /* scan 0- 7 */
  85. '&', '*', '(', ')', '_', '+', 0x08, '\t', /* scan 8- F */
  86. 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', /* scan 10-17 */
  87. 'O', 'P', '{', '}', '\r', CN, 'A', 'S', /* scan 18-1F */
  88. 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', /* scan 20-27 */
  89. '"', '~', SH, '|', 'Z', 'X', 'C', 'V', /* scan 28-2F */
  90. 'B', 'N', 'M', '<', '>', '?', SH, '*', /* scan 30-37 */
  91. ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
  92. 0, 0, 0, 0, 0, NM, ST, '7', /* scan 40-47 */
  93. '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
  94. '2', '3', '0', '.', 0, 0, 0, 0, /* scan 50-57 */
  95. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
  96. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
  97. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
  98. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
  99. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
  100. '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
  101. 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
  102. },
  103. { /* control code */
  104. 0xff, 0x1b, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, /* scan 0- 7 */
  105. 0x1e, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, '\t', /* scan 8- F */
  106. 0x11, 0x17, 0x05, 0x12, 0x14, 0x19, 0x15, 0x09, /* scan 10-17 */
  107. 0x0f, 0x10, 0x1b, 0x1d, '\r', CN, 0x01, 0x13, /* scan 18-1F */
  108. 0x04, 0x06, 0x07, 0x08, 0x0a, 0x0b, 0x0c, 0xff, /* scan 20-27 */
  109. 0xff, 0x1c, SH, 0xff, 0x1a, 0x18, 0x03, 0x16, /* scan 28-2F */
  110. 0x02, 0x0e, 0x0d, 0xff, 0xff, 0xff, SH, 0xff, /* scan 30-37 */
  111. 0xff, 0xff, CP, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
  112. 0xff, 0xff, 0xff, 0xff, 0xff, NM, ST, 0xff, /* scan 40-47 */
  113. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48-4F */
  114. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 50-57 */
  115. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58-5F */
  116. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60-67 */
  117. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68-6F */
  118. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70-77 */
  119. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78-7F */
  120. '\r', CN, '/', '*', ' ', ST, 0xff, 0xff, /* extended */
  121. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* extended */
  122. },
  123. { /* non numeric code */
  124. 0, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 0- 7 */
  125. '7', '8', '9', '0', '-', '=', 0x08, '\t', /* scan 8- F */
  126. 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', /* scan 10-17 */
  127. 'o', 'p', '[', ']', '\r', CN, 'a', 's', /* scan 18-1F */
  128. 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* scan 20-27 */
  129. '\'', '`', SH, '\\', 'z', 'x', 'c', 'v', /* scan 28-2F */
  130. 'b', 'n', 'm', ',', '.', '/', SH, '*', /* scan 30-37 */
  131. ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
  132. 0, 0, 0, 0, 0, NM, ST, 'w', /* scan 40-47 */
  133. 'x', 'y', 'l', 't', 'u', 'v', 'm', 'q', /* scan 48-4F */
  134. 'r', 's', 'p', 'n', 0, 0, 0, 0, /* scan 50-57 */
  135. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
  136. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
  137. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
  138. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
  139. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
  140. '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
  141. 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
  142. },
  143. { /* right alt mode - not used in US keyboard */
  144. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 0 - 7 */
  145. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 8 - F */
  146. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 10 -17 */
  147. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 18 -1F */
  148. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 20 -27 */
  149. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 28 -2F */
  150. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 30 -37 */
  151. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38 -3F */
  152. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 40 -47 */
  153. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48 -4F */
  154. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 50 -57 */
  155. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58 -5F */
  156. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60 -67 */
  157. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68 -6F */
  158. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70 -77 */
  159. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78 -7F */
  160. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* extended */
  161. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* extended */
  162. }
  163. },
  164. { /* german keyboard */
  165. { /* unshift code */
  166. 0, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 0- 7 */
  167. '7', '8', '9', '0', 0xe1, '\'', 0x08, '\t', /* scan 8- F */
  168. 'q', 'w', 'e', 'r', 't', 'z', 'u', 'i', /* scan 10-17 */
  169. 'o', 'p', 0x81, '+', '\r', CN, 'a', 's', /* scan 18-1F */
  170. 'd', 'f', 'g', 'h', 'j', 'k', 'l', 0x94, /* scan 20-27 */
  171. 0x84, '^', SH, '#', 'y', 'x', 'c', 'v', /* scan 28-2F */
  172. 'b', 'n', 'm', ',', '.', '-', SH, '*', /* scan 30-37 */
  173. ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
  174. 0, 0, 0, 0, 0, NM, ST, '7', /* scan 40-47 */
  175. '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
  176. '2', '3', '0', ',', 0, 0, '<', 0, /* scan 50-57 */
  177. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
  178. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
  179. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
  180. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
  181. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
  182. '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
  183. 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
  184. },
  185. { /* shift code */
  186. 0, 0x1b, '!', '"', 0x15, '$', '%', '&', /* scan 0- 7 */
  187. '/', '(', ')', '=', '?', '`', 0x08, '\t', /* scan 8- F */
  188. 'Q', 'W', 'E', 'R', 'T', 'Z', 'U', 'I', /* scan 10-17 */
  189. 'O', 'P', 0x9a, '*', '\r', CN, 'A', 'S', /* scan 18-1F */
  190. 'D', 'F', 'G', 'H', 'J', 'K', 'L', 0x99, /* scan 20-27 */
  191. 0x8e, 0xf8, SH, '\'', 'Y', 'X', 'C', 'V', /* scan 28-2F */
  192. 'B', 'N', 'M', ';', ':', '_', SH, '*', /* scan 30-37 */
  193. ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
  194. 0, 0, 0, 0, 0, NM, ST, '7', /* scan 40-47 */
  195. '8', '9', '-', '4', '5', '6', '+', '1', /* scan 48-4F */
  196. '2', '3', '0', ',', 0, 0, '>', 0, /* scan 50-57 */
  197. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
  198. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
  199. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
  200. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
  201. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
  202. '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
  203. 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
  204. },
  205. { /* control code */
  206. 0xff, 0x1b, 0xff, 0x00, 0xff, 0xff, 0xff, 0xff, /* scan 0- 7 */
  207. 0x1e, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, '\t', /* scan 8- F */
  208. 0x11, 0x17, 0x05, 0x12, 0x14, 0x19, 0x15, 0x09, /* scan 10-17 */
  209. 0x0f, 0x10, 0x1b, 0x1d, '\r', CN, 0x01, 0x13, /* scan 18-1F */
  210. 0x04, 0x06, 0x07, 0x08, 0x0a, 0x0b, 0x0c, 0xff, /* scan 20-27 */
  211. 0xff, 0x1c, SH, 0xff, 0x1a, 0x18, 0x03, 0x16, /* scan 28-2F */
  212. 0x02, 0x0e, 0x0d, 0xff, 0xff, 0xff, SH, 0xff, /* scan 30-37 */
  213. 0xff, 0xff, CP, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38-3F */
  214. 0xff, 0xff, 0xff, 0xff, 0xff, NM, ST, 0xff, /* scan 40-47 */
  215. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48-4F */
  216. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 50-57 */
  217. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58-5F */
  218. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60-67 */
  219. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68-6F */
  220. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70-77 */
  221. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78-7F */
  222. '\r', CN, '/', '*', ' ', ST, 0xff, 0xff, /* extended */
  223. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* extended */
  224. },
  225. { /* non numeric code */
  226. 0, 0x1b, '1', '2', '3', '4', '5', '6', /* scan 0- 7 */
  227. '7', '8', '9', '0', 0xe1, '\'', 0x08, '\t', /* scan 8- F */
  228. 'q', 'w', 'e', 'r', 't', 'z', 'u', 'i', /* scan 10-17 */
  229. 'o', 'p', 0x81, '+', '\r', CN, 'a', 's', /* scan 18-1F */
  230. 'd', 'f', 'g', 'h', 'j', 'k', 'l', 0x94, /* scan 20-27 */
  231. 0x84, '^', SH, 0, 'y', 'x', 'c', 'v', /* scan 28-2F */
  232. 'b', 'n', 'm', ',', '.', '-', SH, '*', /* scan 30-37 */
  233. ' ', ' ', CP, 0, 0, 0, 0, 0, /* scan 38-3F */
  234. 0, 0, 0, 0, 0, NM, ST, 'w', /* scan 40-47 */
  235. 'x', 'y', 'l', 't', 'u', 'v', 'm', 'q', /* scan 48-4F */
  236. 'r', 's', 'p', 'n', 0, 0, '<', 0, /* scan 50-57 */
  237. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
  238. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
  239. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
  240. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 70-77 */
  241. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 78-7F */
  242. '\r', CN, '/', '*', ' ', ST, 'F', 'A', /* extended */
  243. 0, 'D', 'C', 0, 'B', 0, '@', 'P' /* extended */
  244. },
  245. { /* Right alt mode - is used in German keyboard */
  246. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 0 - 7 */
  247. '{', '[', ']', '}', '\\', 0xff, 0xff, 0xff, /* scan 8 - F */
  248. '@', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 10 -17 */
  249. 0xff, 0xff, 0xff, '~', 0xff, 0xff, 0xff, 0xff, /* scan 18 -1F */
  250. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 20 -27 */
  251. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 28 -2F */
  252. 0xff, 0xff, 0xe6, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 30 -37 */
  253. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 38 -3F */
  254. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 40 -47 */
  255. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 48 -4F */
  256. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '|', 0xff, /* scan 50 -57 */
  257. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 58 -5F */
  258. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 60 -67 */
  259. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 68 -6F */
  260. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 70 -77 */
  261. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* scan 78 -7F */
  262. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* extended */
  263. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff /* extended */
  264. }
  265. }
  266. };
  267. static unsigned char ext_key_map[] = {
  268. 0x1c, /* keypad enter */
  269. 0x1d, /* right control */
  270. 0x35, /* keypad slash */
  271. 0x37, /* print screen */
  272. 0x38, /* right alt */
  273. 0x46, /* break */
  274. 0x47, /* editpad home */
  275. 0x48, /* editpad up */
  276. 0x49, /* editpad pgup */
  277. 0x4b, /* editpad left */
  278. 0x4d, /* editpad right */
  279. 0x4f, /* editpad end */
  280. 0x50, /* editpad dn */
  281. 0x51, /* editpad pgdn */
  282. 0x52, /* editpad ins */
  283. 0x53, /* editpad del */
  284. 0x00 /* map end */
  285. };
  286. /******************************************************************************/
  287. static int kbd_controller_present(void)
  288. {
  289. return in8(I8042_STATUS_REG) != 0xff;
  290. }
  291. /*
  292. * Implement a weak default function for boards that optionally
  293. * need to skip the i8042 initialization.
  294. */
  295. int __weak board_i8042_skip(void)
  296. {
  297. /* As default, don't skip */
  298. return 0;
  299. }
  300. void i8042_flush(void)
  301. {
  302. int timeout;
  303. /*
  304. * The delay is to give the keyboard controller some time to fill the
  305. * next byte.
  306. */
  307. while (1) {
  308. timeout = 100; /* wait for no longer than 100us */
  309. while (timeout > 0 && !(in8(I8042_STATUS_REG) & 0x01)) {
  310. udelay(1);
  311. timeout--;
  312. }
  313. /* Try to pull next byte if not timeout. */
  314. if (in8(I8042_STATUS_REG) & 0x01)
  315. in8(I8042_DATA_REG);
  316. else
  317. break;
  318. }
  319. }
  320. int i8042_disable(void)
  321. {
  322. if (kbd_input_empty() == 0)
  323. return -1;
  324. /* Disable keyboard */
  325. out8(I8042_COMMAND_REG, 0xad);
  326. if (kbd_input_empty() == 0)
  327. return -1;
  328. return 0;
  329. }
  330. /*******************************************************************************
  331. *
  332. * i8042_kbd_init - reset keyboard and init state flags
  333. */
  334. int i8042_kbd_init(void)
  335. {
  336. int keymap, try;
  337. char *penv;
  338. if (!kbd_controller_present() || board_i8042_skip())
  339. return -1;
  340. #ifdef CONFIG_USE_CPCIDVI
  341. penv = getenv("console");
  342. if (penv != NULL) {
  343. if (strncmp(penv, "serial", 7) == 0)
  344. return -1;
  345. }
  346. #endif
  347. /* Init keyboard device (default US layout) */
  348. keymap = KBD_US;
  349. penv = getenv("keymap");
  350. if (penv != NULL) {
  351. if (strncmp(penv, "de", 3) == 0)
  352. keymap = KBD_GER;
  353. }
  354. for (try = 0; try < KBD_RESET_TRIES; try++) {
  355. if (kbd_reset() == 0) {
  356. kbd_mapping = keymap;
  357. kbd_flags = NORMAL;
  358. kbd_state = 0;
  359. kbd_led_set();
  360. return 0;
  361. }
  362. }
  363. return -1;
  364. }
  365. /*******************************************************************************
  366. *
  367. * i8042_tstc - test if keyboard input is available
  368. * option: cursor blinking if called in a loop
  369. */
  370. int i8042_tstc(struct stdio_dev *dev)
  371. {
  372. unsigned char scan_code = 0;
  373. #ifdef CONFIG_CONSOLE_CURSOR
  374. if (--blinkCount == 0) {
  375. cursor_state ^= 1;
  376. console_cursor(cursor_state);
  377. blinkCount = CONFIG_SYS_CONSOLE_BLINK_COUNT;
  378. udelay(10);
  379. }
  380. #endif
  381. if ((in8(I8042_STATUS_REG) & 0x01) == 0) {
  382. return 0;
  383. } else {
  384. scan_code = in8(I8042_DATA_REG);
  385. if (scan_code == 0xfa)
  386. return 0;
  387. kbd_conv_char(scan_code);
  388. if (kbd_input != -1)
  389. return 1;
  390. }
  391. return 0;
  392. }
  393. /*******************************************************************************
  394. *
  395. * i8042_getc - wait till keyboard input is available
  396. * option: turn on/off cursor while waiting
  397. */
  398. int i8042_getc(struct stdio_dev *dev)
  399. {
  400. int ret_chr;
  401. unsigned char scan_code;
  402. while (kbd_input == -1) {
  403. while ((in8(I8042_STATUS_REG) & 0x01) == 0) {
  404. #ifdef CONFIG_CONSOLE_CURSOR
  405. if (--blinkCount == 0) {
  406. cursor_state ^= 1;
  407. console_cursor(cursor_state);
  408. blinkCount = CONFIG_SYS_CONSOLE_BLINK_COUNT;
  409. }
  410. udelay(10);
  411. #endif
  412. }
  413. scan_code = in8(I8042_DATA_REG);
  414. if (scan_code != 0xfa)
  415. kbd_conv_char (scan_code);
  416. }
  417. ret_chr = kbd_input;
  418. kbd_input = -1;
  419. return ret_chr;
  420. }
  421. /******************************************************************************/
  422. static void kbd_conv_char(unsigned char scan_code)
  423. {
  424. if (scan_code == 0xe0) {
  425. kbd_flags |= EXT;
  426. return;
  427. }
  428. /* if high bit of scan_code, set break flag */
  429. if (scan_code & 0x80)
  430. kbd_flags |= BRK;
  431. else
  432. kbd_flags &= ~BRK;
  433. if ((scan_code == 0xe1) || (kbd_flags & E1)) {
  434. if (scan_code == 0xe1) {
  435. kbd_flags ^= BRK; /* reset the break flag */
  436. kbd_flags ^= E1; /* bitwise EXOR with E1 flag */
  437. }
  438. return;
  439. }
  440. scan_code &= 0x7f;
  441. if (kbd_flags & EXT) {
  442. int i;
  443. kbd_flags ^= EXT;
  444. for (i = 0; ext_key_map[i]; i++) {
  445. if (ext_key_map[i] == scan_code) {
  446. scan_code = 0x80 + i;
  447. break;
  448. }
  449. }
  450. /* not found ? */
  451. if (!ext_key_map[i])
  452. return;
  453. }
  454. switch (kbd_fct_map[scan_code]) {
  455. case AS:
  456. kbd_normal(scan_code);
  457. break;
  458. case SH:
  459. kbd_shift(scan_code);
  460. break;
  461. case CN:
  462. kbd_ctrl(scan_code);
  463. break;
  464. case NM:
  465. kbd_num(scan_code);
  466. break;
  467. case CP:
  468. kbd_caps(scan_code);
  469. break;
  470. case ST:
  471. kbd_scroll(scan_code);
  472. break;
  473. case AK:
  474. kbd_alt(scan_code);
  475. break;
  476. }
  477. return;
  478. }
  479. /******************************************************************************/
  480. static void kbd_normal(unsigned char scan_code)
  481. {
  482. unsigned char chr;
  483. if ((kbd_flags & BRK) == NORMAL) {
  484. chr = kbd_key_map[kbd_mapping][kbd_state][scan_code];
  485. if ((chr == 0xff) || (chr == 0x00))
  486. return;
  487. /* if caps lock convert upper to lower */
  488. if (((kbd_flags & CAPS) == CAPS) &&
  489. (chr >= 'a' && chr <= 'z')) {
  490. chr -= 'a' - 'A';
  491. }
  492. kbd_input = chr;
  493. }
  494. }
  495. /******************************************************************************/
  496. static void kbd_shift(unsigned char scan_code)
  497. {
  498. if ((kbd_flags & BRK) == BRK) {
  499. kbd_state = AS;
  500. kbd_flags &= (~SHIFT);
  501. } else {
  502. kbd_state = SH;
  503. kbd_flags |= SHIFT;
  504. }
  505. }
  506. /******************************************************************************/
  507. static void kbd_ctrl(unsigned char scan_code)
  508. {
  509. if ((kbd_flags & BRK) == BRK) {
  510. kbd_state = AS;
  511. kbd_flags &= (~CTRL);
  512. } else {
  513. kbd_state = CN;
  514. kbd_flags |= CTRL;
  515. }
  516. }
  517. /******************************************************************************/
  518. static void kbd_caps(unsigned char scan_code)
  519. {
  520. if ((kbd_flags & BRK) == NORMAL) {
  521. kbd_flags ^= CAPS;
  522. kbd_led_set(); /* update keyboard LED */
  523. }
  524. }
  525. /******************************************************************************/
  526. static void kbd_num(unsigned char scan_code)
  527. {
  528. if ((kbd_flags & BRK) == NORMAL) {
  529. kbd_flags ^= NUM;
  530. kbd_state = (kbd_flags & NUM) ? AS : NM;
  531. kbd_led_set(); /* update keyboard LED */
  532. }
  533. }
  534. /******************************************************************************/
  535. static void kbd_scroll(unsigned char scan_code)
  536. {
  537. if ((kbd_flags & BRK) == NORMAL) {
  538. kbd_flags ^= STP;
  539. kbd_led_set(); /* update keyboard LED */
  540. if (kbd_flags & STP)
  541. kbd_input = 0x13;
  542. else
  543. kbd_input = 0x11;
  544. }
  545. }
  546. /******************************************************************************/
  547. static void kbd_alt(unsigned char scan_code)
  548. {
  549. if ((kbd_flags & BRK) == BRK) {
  550. kbd_state = AS;
  551. kbd_flags &= (~ALT);
  552. } else {
  553. kbd_state = AK;
  554. kbd_flags &= ALT;
  555. }
  556. }
  557. /******************************************************************************/
  558. static void kbd_led_set(void)
  559. {
  560. kbd_input_empty();
  561. out8(I8042_DATA_REG, 0xed); /* SET LED command */
  562. kbd_input_empty();
  563. out8(I8042_DATA_REG, (kbd_flags & 0x7)); /* LED bits only */
  564. }
  565. /******************************************************************************/
  566. static int kbd_input_empty(void)
  567. {
  568. int kbdTimeout = KBD_TIMEOUT * 1000;
  569. while ((in8(I8042_STATUS_REG) & I8042_STATUS_IN_DATA) && kbdTimeout--)
  570. udelay(1);
  571. return kbdTimeout != -1;
  572. }
  573. /******************************************************************************/
  574. static int wait_until_kbd_output_full(void)
  575. {
  576. int kbdTimeout = KBD_TIMEOUT * 1000;
  577. while (((in8(I8042_STATUS_REG) & 0x01) == 0) && kbdTimeout--)
  578. udelay(1);
  579. return kbdTimeout != -1;
  580. }
  581. /******************************************************************************/
  582. static int kbd_reset(void)
  583. {
  584. /* KB Reset */
  585. if (kbd_input_empty() == 0)
  586. return -1;
  587. out8(I8042_DATA_REG, 0xff);
  588. if (wait_until_kbd_output_full() == 0)
  589. return -1;
  590. if (in8(I8042_DATA_REG) != 0xfa) /* ACK */
  591. return -1;
  592. if (wait_until_kbd_output_full() == 0)
  593. return -1;
  594. if (in8(I8042_DATA_REG) != 0xaa) /* Test Pass*/
  595. return -1;
  596. if (kbd_input_empty() == 0)
  597. return -1;
  598. /* Set KBC mode */
  599. out8(I8042_COMMAND_REG, 0x60);
  600. if (kbd_input_empty() == 0)
  601. return -1;
  602. out8(I8042_DATA_REG, 0x45);
  603. if (kbd_input_empty() == 0)
  604. return -1;
  605. /* Enable Keyboard */
  606. out8(I8042_COMMAND_REG, 0xae);
  607. if (kbd_input_empty() == 0)
  608. return -1;
  609. return 0;
  610. }