i8042.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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. #include <linux/compiler.h>
  27. #ifdef CONFIG_USE_CPCIDVI
  28. extern u8 gt_cpcidvi_in8(u32 offset);
  29. extern void gt_cpcidvi_out8(u32 offset, u8 data);
  30. #define in8(a) gt_cpcidvi_in8(a)
  31. #define out8(a, b) gt_cpcidvi_out8(a, b)
  32. #endif
  33. #include <i8042.h>
  34. /* defines */
  35. #ifdef CONFIG_CONSOLE_CURSOR
  36. extern void console_cursor(int state);
  37. static int blinkCount = CONFIG_SYS_CONSOLE_BLINK_COUNT;
  38. static int cursor_state;
  39. #endif
  40. /* locals */
  41. static int kbd_input = -1; /* no input yet */
  42. static int kbd_mapping = KBD_US; /* default US keyboard */
  43. static int kbd_flags = NORMAL; /* after reset */
  44. static int kbd_state; /* unshift code */
  45. static void kbd_conv_char(unsigned char scan_code);
  46. static void kbd_led_set(void);
  47. static void kbd_normal(unsigned char scan_code);
  48. static void kbd_shift(unsigned char scan_code);
  49. static void kbd_ctrl(unsigned char scan_code);
  50. static void kbd_num(unsigned char scan_code);
  51. static void kbd_caps(unsigned char scan_code);
  52. static void kbd_scroll(unsigned char scan_code);
  53. static void kbd_alt(unsigned char scan_code);
  54. static int kbd_input_empty(void);
  55. static int kbd_reset(void);
  56. static unsigned char kbd_fct_map[144] = {
  57. /* kbd_fct_map table for scan code */
  58. 0, AS, AS, AS, AS, AS, AS, AS, /* scan 0- 7 */
  59. AS, AS, AS, AS, AS, AS, AS, AS, /* scan 8- F */
  60. AS, AS, AS, AS, AS, AS, AS, AS, /* scan 10-17 */
  61. AS, AS, AS, AS, AS, CN, AS, AS, /* scan 18-1F */
  62. AS, AS, AS, AS, AS, AS, AS, AS, /* scan 20-27 */
  63. AS, AS, SH, AS, AS, AS, AS, AS, /* scan 28-2F */
  64. AS, AS, AS, AS, AS, AS, SH, AS, /* scan 30-37 */
  65. AS, AS, CP, 0, 0, 0, 0, 0, /* scan 38-3F */
  66. 0, 0, 0, 0, 0, NM, ST, ES, /* scan 40-47 */
  67. ES, ES, ES, ES, ES, ES, ES, ES, /* scan 48-4F */
  68. ES, ES, ES, ES, 0, 0, AS, 0, /* scan 50-57 */
  69. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 58-5F */
  70. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 60-67 */
  71. 0, 0, 0, 0, 0, 0, 0, 0, /* scan 68-6F */
  72. AS, 0, 0, AS, 0, 0, AS, 0, /* scan 70-77 */
  73. 0, AS, 0, 0, 0, AS, 0, 0, /* scan 78-7F */
  74. AS, CN, AS, AS, AK, ST, EX, EX, /* enhanced */
  75. AS, EX, EX, AS, EX, AS, EX, EX /* enhanced */
  76. };
  77. static unsigned char kbd_key_map[2][5][144] = {
  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. 0x1c, /* keypad enter */
  285. 0x1d, /* right control */
  286. 0x35, /* keypad slash */
  287. 0x37, /* print screen */
  288. 0x38, /* right alt */
  289. 0x46, /* break */
  290. 0x47, /* editpad home */
  291. 0x48, /* editpad up */
  292. 0x49, /* editpad pgup */
  293. 0x4b, /* editpad left */
  294. 0x4d, /* editpad right */
  295. 0x4f, /* editpad end */
  296. 0x50, /* editpad dn */
  297. 0x51, /* editpad pgdn */
  298. 0x52, /* editpad ins */
  299. 0x53, /* editpad del */
  300. 0x00 /* map end */
  301. };
  302. /******************************************************************************/
  303. static int kbd_controller_present(void)
  304. {
  305. return in8(I8042_STATUS_REG) != 0xff;
  306. }
  307. /*
  308. * Implement a weak default function for boards that optionally
  309. * need to skip the i8042 initialization.
  310. */
  311. int __weak board_i8042_skip(void)
  312. {
  313. /* As default, don't skip */
  314. return 0;
  315. }
  316. void i8042_flush(void)
  317. {
  318. int timeout;
  319. /*
  320. * The delay is to give the keyboard controller some time to fill the
  321. * next byte.
  322. */
  323. while (1) {
  324. timeout = 100; /* wait for no longer than 100us */
  325. while (timeout > 0 && !(in8(I8042_STATUS_REG) & 0x01)) {
  326. udelay(1);
  327. timeout--;
  328. }
  329. /* Try to pull next byte if not timeout. */
  330. if (in8(I8042_STATUS_REG) & 0x01)
  331. in8(I8042_DATA_REG);
  332. else
  333. break;
  334. }
  335. }
  336. int i8042_disable(void)
  337. {
  338. if (kbd_input_empty() == 0)
  339. return -1;
  340. /* Disable keyboard */
  341. out8(I8042_COMMAND_REG, 0xad);
  342. if (kbd_input_empty() == 0)
  343. return -1;
  344. return 0;
  345. }
  346. /*******************************************************************************
  347. *
  348. * i8042_kbd_init - reset keyboard and init state flags
  349. */
  350. int i8042_kbd_init(void)
  351. {
  352. int keymap, try;
  353. char *penv;
  354. if (!kbd_controller_present() || board_i8042_skip())
  355. return -1;
  356. #ifdef CONFIG_USE_CPCIDVI
  357. penv = getenv("console");
  358. if (penv != NULL) {
  359. if (strncmp(penv, "serial", 7) == 0)
  360. return -1;
  361. }
  362. #endif
  363. /* Init keyboard device (default US layout) */
  364. keymap = KBD_US;
  365. penv = getenv("keymap");
  366. if (penv != NULL) {
  367. if (strncmp(penv, "de", 3) == 0)
  368. keymap = KBD_GER;
  369. }
  370. for (try = 0; try < KBD_RESET_TRIES; try++) {
  371. if (kbd_reset() == 0) {
  372. kbd_mapping = keymap;
  373. kbd_flags = NORMAL;
  374. kbd_state = 0;
  375. kbd_led_set();
  376. return 0;
  377. }
  378. }
  379. return -1;
  380. }
  381. /*******************************************************************************
  382. *
  383. * i8042_tstc - test if keyboard input is available
  384. * option: cursor blinking if called in a loop
  385. */
  386. int i8042_tstc(void)
  387. {
  388. unsigned char scan_code = 0;
  389. #ifdef CONFIG_CONSOLE_CURSOR
  390. if (--blinkCount == 0) {
  391. cursor_state ^= 1;
  392. console_cursor(cursor_state);
  393. blinkCount = CONFIG_SYS_CONSOLE_BLINK_COUNT;
  394. udelay(10);
  395. }
  396. #endif
  397. if ((in8(I8042_STATUS_REG) & 0x01) == 0) {
  398. return 0;
  399. } else {
  400. scan_code = in8(I8042_DATA_REG);
  401. if (scan_code == 0xfa)
  402. return 0;
  403. kbd_conv_char(scan_code);
  404. if (kbd_input != -1)
  405. return 1;
  406. }
  407. return 0;
  408. }
  409. /*******************************************************************************
  410. *
  411. * i8042_getc - wait till keyboard input is available
  412. * option: turn on/off cursor while waiting
  413. */
  414. int i8042_getc(void)
  415. {
  416. int ret_chr;
  417. unsigned char scan_code;
  418. while (kbd_input == -1) {
  419. while ((in8(I8042_STATUS_REG) & 0x01) == 0) {
  420. #ifdef CONFIG_CONSOLE_CURSOR
  421. if (--blinkCount == 0) {
  422. cursor_state ^= 1;
  423. console_cursor(cursor_state);
  424. blinkCount = CONFIG_SYS_CONSOLE_BLINK_COUNT;
  425. }
  426. udelay(10);
  427. #endif
  428. }
  429. scan_code = in8(I8042_DATA_REG);
  430. if (scan_code != 0xfa)
  431. kbd_conv_char (scan_code);
  432. }
  433. ret_chr = kbd_input;
  434. kbd_input = -1;
  435. return ret_chr;
  436. }
  437. /******************************************************************************/
  438. static void kbd_conv_char(unsigned char scan_code)
  439. {
  440. if (scan_code == 0xe0) {
  441. kbd_flags |= EXT;
  442. return;
  443. }
  444. /* if high bit of scan_code, set break flag */
  445. if (scan_code & 0x80)
  446. kbd_flags |= BRK;
  447. else
  448. kbd_flags &= ~BRK;
  449. if ((scan_code == 0xe1) || (kbd_flags & E1)) {
  450. if (scan_code == 0xe1) {
  451. kbd_flags ^= BRK; /* reset the break flag */
  452. kbd_flags ^= E1; /* bitwise EXOR with E1 flag */
  453. }
  454. return;
  455. }
  456. scan_code &= 0x7f;
  457. if (kbd_flags & EXT) {
  458. int i;
  459. kbd_flags ^= EXT;
  460. for (i = 0; ext_key_map[i]; i++) {
  461. if (ext_key_map[i] == scan_code) {
  462. scan_code = 0x80 + i;
  463. break;
  464. }
  465. }
  466. /* not found ? */
  467. if (!ext_key_map[i])
  468. return;
  469. }
  470. switch (kbd_fct_map[scan_code]) {
  471. case AS:
  472. kbd_normal(scan_code);
  473. break;
  474. case SH:
  475. kbd_shift(scan_code);
  476. break;
  477. case CN:
  478. kbd_ctrl(scan_code);
  479. break;
  480. case NM:
  481. kbd_num(scan_code);
  482. break;
  483. case CP:
  484. kbd_caps(scan_code);
  485. break;
  486. case ST:
  487. kbd_scroll(scan_code);
  488. break;
  489. case AK:
  490. kbd_alt(scan_code);
  491. break;
  492. }
  493. return;
  494. }
  495. /******************************************************************************/
  496. static void kbd_normal(unsigned char scan_code)
  497. {
  498. unsigned char chr;
  499. if ((kbd_flags & BRK) == NORMAL) {
  500. chr = kbd_key_map[kbd_mapping][kbd_state][scan_code];
  501. if ((chr == 0xff) || (chr == 0x00))
  502. return;
  503. /* if caps lock convert upper to lower */
  504. if (((kbd_flags & CAPS) == CAPS) &&
  505. (chr >= 'a' && chr <= 'z')) {
  506. chr -= 'a' - 'A';
  507. }
  508. kbd_input = chr;
  509. }
  510. }
  511. /******************************************************************************/
  512. static void kbd_shift(unsigned char scan_code)
  513. {
  514. if ((kbd_flags & BRK) == BRK) {
  515. kbd_state = AS;
  516. kbd_flags &= (~SHIFT);
  517. } else {
  518. kbd_state = SH;
  519. kbd_flags |= SHIFT;
  520. }
  521. }
  522. /******************************************************************************/
  523. static void kbd_ctrl(unsigned char scan_code)
  524. {
  525. if ((kbd_flags & BRK) == BRK) {
  526. kbd_state = AS;
  527. kbd_flags &= (~CTRL);
  528. } else {
  529. kbd_state = CN;
  530. kbd_flags |= CTRL;
  531. }
  532. }
  533. /******************************************************************************/
  534. static void kbd_caps(unsigned char scan_code)
  535. {
  536. if ((kbd_flags & BRK) == NORMAL) {
  537. kbd_flags ^= CAPS;
  538. kbd_led_set(); /* update keyboard LED */
  539. }
  540. }
  541. /******************************************************************************/
  542. static void kbd_num(unsigned char scan_code)
  543. {
  544. if ((kbd_flags & BRK) == NORMAL) {
  545. kbd_flags ^= NUM;
  546. kbd_state = (kbd_flags & NUM) ? AS : NM;
  547. kbd_led_set(); /* update keyboard LED */
  548. }
  549. }
  550. /******************************************************************************/
  551. static void kbd_scroll(unsigned char scan_code)
  552. {
  553. if ((kbd_flags & BRK) == NORMAL) {
  554. kbd_flags ^= STP;
  555. kbd_led_set(); /* update keyboard LED */
  556. if (kbd_flags & STP)
  557. kbd_input = 0x13;
  558. else
  559. kbd_input = 0x11;
  560. }
  561. }
  562. /******************************************************************************/
  563. static void kbd_alt(unsigned char scan_code)
  564. {
  565. if ((kbd_flags & BRK) == BRK) {
  566. kbd_state = AS;
  567. kbd_flags &= (~ALT);
  568. } else {
  569. kbd_state = AK;
  570. kbd_flags &= ALT;
  571. }
  572. }
  573. /******************************************************************************/
  574. static void kbd_led_set(void)
  575. {
  576. kbd_input_empty();
  577. out8(I8042_DATA_REG, 0xed); /* SET LED command */
  578. kbd_input_empty();
  579. out8(I8042_DATA_REG, (kbd_flags & 0x7)); /* LED bits only */
  580. }
  581. /******************************************************************************/
  582. static int kbd_input_empty(void)
  583. {
  584. int kbdTimeout = KBD_TIMEOUT * 1000;
  585. while ((in8(I8042_STATUS_REG) & I8042_STATUS_IN_DATA) && kbdTimeout--)
  586. udelay(1);
  587. return kbdTimeout != -1;
  588. }
  589. /******************************************************************************/
  590. static int wait_until_kbd_output_full(void)
  591. {
  592. int kbdTimeout = KBD_TIMEOUT * 1000;
  593. while (((in8(I8042_STATUS_REG) & 0x01) == 0) && kbdTimeout--)
  594. udelay(1);
  595. return kbdTimeout != -1;
  596. }
  597. /******************************************************************************/
  598. static int kbd_reset(void)
  599. {
  600. /* KB Reset */
  601. if (kbd_input_empty() == 0)
  602. return -1;
  603. out8(I8042_DATA_REG, 0xff);
  604. if (wait_until_kbd_output_full() == 0)
  605. return -1;
  606. if (in8(I8042_DATA_REG) != 0xfa) /* ACK */
  607. return -1;
  608. if (wait_until_kbd_output_full() == 0)
  609. return -1;
  610. if (in8(I8042_DATA_REG) != 0xaa) /* Test Pass*/
  611. return -1;
  612. if (kbd_input_empty() == 0)
  613. return -1;
  614. /* Set KBC mode */
  615. out8(I8042_COMMAND_REG, 0x60);
  616. if (kbd_input_empty() == 0)
  617. return -1;
  618. out8(I8042_DATA_REG, 0x45);
  619. if (kbd_input_empty() == 0)
  620. return -1;
  621. /* Enable Keyboard */
  622. out8(I8042_COMMAND_REG, 0xae);
  623. if (kbd_input_empty() == 0)
  624. return -1;
  625. return 0;
  626. }