i8042.c 23 KB

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