i8042.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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. static int kbd_controller_present(void)
  303. {
  304. return in8(I8042_STATUS_REG) != 0xff;
  305. }
  306. /*******************************************************************************
  307. *
  308. * i8042_kbd_init - reset keyboard and init state flags
  309. */
  310. int i8042_kbd_init(void)
  311. {
  312. int keymap, try;
  313. char *penv;
  314. if (!kbd_controller_present())
  315. return -1;
  316. #ifdef CONFIG_USE_CPCIDVI
  317. penv = getenv("console");
  318. if (penv != NULL) {
  319. if (strncmp(penv, "serial", 7) == 0)
  320. return -1;
  321. }
  322. #endif
  323. /* Init keyboard device (default US layout) */
  324. keymap = KBD_US;
  325. penv = getenv("keymap");
  326. if (penv != NULL) {
  327. if (strncmp(penv, "de", 3) == 0)
  328. keymap = KBD_GER;
  329. }
  330. for (try = 0; try < KBD_RESET_TRIES; try++) {
  331. if (kbd_reset() == 0) {
  332. kbd_mapping = keymap;
  333. kbd_flags = NORMAL;
  334. kbd_state = 0;
  335. kbd_led_set();
  336. return 0;
  337. }
  338. }
  339. return -1;
  340. }
  341. /*******************************************************************************
  342. *
  343. * i8042_tstc - test if keyboard input is available
  344. * option: cursor blinking if called in a loop
  345. */
  346. int i8042_tstc(void)
  347. {
  348. unsigned char scan_code = 0;
  349. #ifdef CONFIG_CONSOLE_CURSOR
  350. if (--blinkCount == 0) {
  351. cursor_state ^= 1;
  352. console_cursor(cursor_state);
  353. blinkCount = CONFIG_SYS_CONSOLE_BLINK_COUNT;
  354. udelay(10);
  355. }
  356. #endif
  357. if ((in8(I8042_STATUS_REG) & 0x01) == 0) {
  358. return 0;
  359. } else {
  360. scan_code = in8(I8042_DATA_REG);
  361. if (scan_code == 0xfa)
  362. return 0;
  363. kbd_conv_char(scan_code);
  364. if (kbd_input != -1)
  365. return 1;
  366. }
  367. return 0;
  368. }
  369. /*******************************************************************************
  370. *
  371. * i8042_getc - wait till keyboard input is available
  372. * option: turn on/off cursor while waiting
  373. */
  374. int i8042_getc(void)
  375. {
  376. int ret_chr;
  377. unsigned char scan_code;
  378. while (kbd_input == -1) {
  379. while ((in8(I8042_STATUS_REG) & 0x01) == 0) {
  380. #ifdef CONFIG_CONSOLE_CURSOR
  381. if (--blinkCount == 0) {
  382. cursor_state ^= 1;
  383. console_cursor(cursor_state);
  384. blinkCount = CONFIG_SYS_CONSOLE_BLINK_COUNT;
  385. }
  386. udelay(10);
  387. #endif
  388. }
  389. scan_code = in8(I8042_DATA_REG);
  390. if (scan_code != 0xfa)
  391. kbd_conv_char (scan_code);
  392. }
  393. ret_chr = kbd_input;
  394. kbd_input = -1;
  395. return ret_chr;
  396. }
  397. /******************************************************************************/
  398. static void kbd_conv_char(unsigned char scan_code)
  399. {
  400. if (scan_code == 0xe0) {
  401. kbd_flags |= EXT;
  402. return;
  403. }
  404. /* if high bit of scan_code, set break flag */
  405. if (scan_code & 0x80)
  406. kbd_flags |= BRK;
  407. else
  408. kbd_flags &= ~BRK;
  409. if ((scan_code == 0xe1) || (kbd_flags & E1)) {
  410. if (scan_code == 0xe1) {
  411. kbd_flags ^= BRK; /* reset the break flag */
  412. kbd_flags ^= E1; /* bitwise EXOR with E1 flag */
  413. }
  414. return;
  415. }
  416. scan_code &= 0x7f;
  417. if (kbd_flags & EXT) {
  418. int i;
  419. kbd_flags ^= EXT;
  420. for (i = 0; ext_key_map[i]; i++) {
  421. if (ext_key_map[i] == scan_code) {
  422. scan_code = 0x80 + i;
  423. break;
  424. }
  425. }
  426. /* not found ? */
  427. if (!ext_key_map[i])
  428. return;
  429. }
  430. switch (kbd_fct_map[scan_code]) {
  431. case AS:
  432. kbd_normal(scan_code);
  433. break;
  434. case SH:
  435. kbd_shift(scan_code);
  436. break;
  437. case CN:
  438. kbd_ctrl(scan_code);
  439. break;
  440. case NM:
  441. kbd_num(scan_code);
  442. break;
  443. case CP:
  444. kbd_caps(scan_code);
  445. break;
  446. case ST:
  447. kbd_scroll(scan_code);
  448. break;
  449. case AK:
  450. kbd_alt(scan_code);
  451. break;
  452. }
  453. return;
  454. }
  455. /******************************************************************************/
  456. static void kbd_normal(unsigned char scan_code)
  457. {
  458. unsigned char chr;
  459. if ((kbd_flags & BRK) == NORMAL) {
  460. chr = kbd_key_map[kbd_mapping][kbd_state][scan_code];
  461. if ((chr == 0xff) || (chr == 0x00))
  462. return;
  463. /* if caps lock convert upper to lower */
  464. if (((kbd_flags & CAPS) == CAPS) &&
  465. (chr >= 'a' && chr <= 'z')) {
  466. chr -= 'a' - 'A';
  467. }
  468. kbd_input = chr;
  469. }
  470. }
  471. /******************************************************************************/
  472. static void kbd_shift(unsigned char scan_code)
  473. {
  474. if ((kbd_flags & BRK) == BRK) {
  475. kbd_state = AS;
  476. kbd_flags &= (~SHIFT);
  477. } else {
  478. kbd_state = SH;
  479. kbd_flags |= SHIFT;
  480. }
  481. }
  482. /******************************************************************************/
  483. static void kbd_ctrl(unsigned char scan_code)
  484. {
  485. if ((kbd_flags & BRK) == BRK) {
  486. kbd_state = AS;
  487. kbd_flags &= (~CTRL);
  488. } else {
  489. kbd_state = CN;
  490. kbd_flags |= CTRL;
  491. }
  492. }
  493. /******************************************************************************/
  494. static void kbd_caps(unsigned char scan_code)
  495. {
  496. if ((kbd_flags & BRK) == NORMAL) {
  497. kbd_flags ^= CAPS;
  498. kbd_led_set(); /* update keyboard LED */
  499. }
  500. }
  501. /******************************************************************************/
  502. static void kbd_num(unsigned char scan_code)
  503. {
  504. if ((kbd_flags & BRK) == NORMAL) {
  505. kbd_flags ^= NUM;
  506. kbd_state = (kbd_flags & NUM) ? AS : NM;
  507. kbd_led_set(); /* update keyboard LED */
  508. }
  509. }
  510. /******************************************************************************/
  511. static void kbd_scroll(unsigned char scan_code)
  512. {
  513. if ((kbd_flags & BRK) == NORMAL) {
  514. kbd_flags ^= STP;
  515. kbd_led_set(); /* update keyboard LED */
  516. if (kbd_flags & STP)
  517. kbd_input = 0x13;
  518. else
  519. kbd_input = 0x11;
  520. }
  521. }
  522. /******************************************************************************/
  523. static void kbd_alt(unsigned char scan_code)
  524. {
  525. if ((kbd_flags & BRK) == BRK) {
  526. kbd_state = AS;
  527. kbd_flags &= (~ALT);
  528. } else {
  529. kbd_state = AK;
  530. kbd_flags &= ALT;
  531. }
  532. }
  533. /******************************************************************************/
  534. static void kbd_led_set(void)
  535. {
  536. kbd_input_empty();
  537. out8(I8042_DATA_REG, 0xed); /* SET LED command */
  538. kbd_input_empty();
  539. out8(I8042_DATA_REG, (kbd_flags & 0x7)); /* LED bits only */
  540. }
  541. /******************************************************************************/
  542. static int kbd_input_empty(void)
  543. {
  544. int kbdTimeout = KBD_TIMEOUT * 1000;
  545. while ((in8(I8042_STATUS_REG) & I8042_STATUS_IN_DATA) && kbdTimeout--)
  546. udelay(1);
  547. return kbdTimeout != -1;
  548. }
  549. /******************************************************************************/
  550. static int kbd_reset(void)
  551. {
  552. if (kbd_input_empty() == 0)
  553. return -1;
  554. out8(I8042_DATA_REG, 0xff);
  555. if (kbd_input_empty() == 0)
  556. return -1;
  557. #ifdef CONFIG_USE_CPCIDVI
  558. out8(I8042_COMMAND_REG, 0x60);
  559. #else
  560. out8(I8042_DATA_REG, 0x60);
  561. #endif
  562. if (kbd_input_empty() == 0)
  563. return -1;
  564. out8(I8042_DATA_REG, 0x45);
  565. if (kbd_input_empty() == 0)
  566. return -1;
  567. out8(I8042_COMMAND_REG, 0xae);
  568. if (kbd_input_empty() == 0)
  569. return -1;
  570. return 0;
  571. }