i8042.c 22 KB

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