ps2mult.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /***********************************************************************
  2. *
  3. * (C) Copyright 2004
  4. * DENX Software Engineering
  5. * Wolfgang Denk, wd@denx.de
  6. *
  7. * PS/2 multiplexer driver
  8. *
  9. * Originally from linux source (drivers/char/ps2mult.c)
  10. *
  11. * Uses simple serial driver (ps2ser.c) to access the multiplexer
  12. * Used by PS/2 keyboard driver (pc_keyb.c)
  13. *
  14. ***********************************************************************/
  15. #include <common.h>
  16. #include <pc_keyb.h>
  17. #include <asm/atomic.h>
  18. #include <ps2mult.h>
  19. /* #define DEBUG_MULT */
  20. /* #define DEBUG_KEYB */
  21. #define KBD_STAT_DEFAULT (KBD_STAT_SELFTEST | KBD_STAT_UNLOCKED)
  22. #define PRINTF(format, args...) printf("ps2mult.c: " format, ## args)
  23. #ifdef DEBUG_MULT
  24. #define PRINTF_MULT(format, args...) printf("PS2MULT: " format, ## args)
  25. #else
  26. #define PRINTF_MULT(format, args...)
  27. #endif
  28. #ifdef DEBUG_KEYB
  29. #define PRINTF_KEYB(format, args...) printf("KEYB: " format, ## args)
  30. #else
  31. #define PRINTF_KEYB(format, args...)
  32. #endif
  33. static ulong start_time;
  34. static int init_done = 0;
  35. static int received_escape = 0;
  36. static int received_bsync = 0;
  37. static int received_selector = 0;
  38. static int kbd_command_active = 0;
  39. static int mouse_command_active = 0;
  40. static int ctl_command_active = 0;
  41. static u_char command_byte = 0;
  42. static void (*keyb_handler)(void *dev_id);
  43. static u_char ps2mult_buf [PS2BUF_SIZE];
  44. static atomic_t ps2mult_buf_cnt;
  45. static int ps2mult_buf_in_idx;
  46. static int ps2mult_buf_out_idx;
  47. static u_char ps2mult_buf_status [PS2BUF_SIZE];
  48. #ifndef CONFIG_BOARD_EARLY_INIT_R
  49. #error #define CONFIG_BOARD_EARLY_INIT_R and call ps2mult_early_init() in board_early_init_r()
  50. #endif
  51. void ps2mult_early_init (void)
  52. {
  53. start_time = get_timer(0);
  54. }
  55. static void ps2mult_send_byte(u_char byte, u_char sel)
  56. {
  57. ps2ser_putc(sel);
  58. if (sel == PS2MULT_KB_SELECTOR) {
  59. PRINTF_MULT("0x%02x send KEYBOARD\n", byte);
  60. kbd_command_active = 1;
  61. } else {
  62. PRINTF_MULT("0x%02x send MOUSE\n", byte);
  63. mouse_command_active = 1;
  64. }
  65. switch (byte) {
  66. case PS2MULT_ESCAPE:
  67. case PS2MULT_BSYNC:
  68. case PS2MULT_KB_SELECTOR:
  69. case PS2MULT_MS_SELECTOR:
  70. case PS2MULT_SESSION_START:
  71. case PS2MULT_SESSION_END:
  72. ps2ser_putc(PS2MULT_ESCAPE);
  73. break;
  74. default:
  75. break;
  76. }
  77. ps2ser_putc(byte);
  78. }
  79. static void ps2mult_receive_byte(u_char byte, u_char sel)
  80. {
  81. u_char status = KBD_STAT_DEFAULT;
  82. #if 1 /* Ignore mouse in U-Boot */
  83. if (sel == PS2MULT_MS_SELECTOR) return;
  84. #endif
  85. if (sel == PS2MULT_KB_SELECTOR) {
  86. if (kbd_command_active) {
  87. if (!received_bsync) {
  88. PRINTF_MULT("0x%02x lost KEYBOARD !!!\n", byte);
  89. return;
  90. } else {
  91. kbd_command_active = 0;
  92. received_bsync = 0;
  93. }
  94. }
  95. PRINTF_MULT("0x%02x receive KEYBOARD\n", byte);
  96. status |= KBD_STAT_IBF | KBD_STAT_OBF;
  97. } else {
  98. if (mouse_command_active) {
  99. if (!received_bsync) {
  100. PRINTF_MULT("0x%02x lost MOUSE !!!\n", byte);
  101. return;
  102. } else {
  103. mouse_command_active = 0;
  104. received_bsync = 0;
  105. }
  106. }
  107. PRINTF_MULT("0x%02x receive MOUSE\n", byte);
  108. status |= KBD_STAT_IBF | KBD_STAT_OBF | KBD_STAT_MOUSE_OBF;
  109. }
  110. if (atomic_read(&ps2mult_buf_cnt) < PS2BUF_SIZE) {
  111. ps2mult_buf_status[ps2mult_buf_in_idx] = status;
  112. ps2mult_buf[ps2mult_buf_in_idx++] = byte;
  113. ps2mult_buf_in_idx &= (PS2BUF_SIZE - 1);
  114. atomic_inc(&ps2mult_buf_cnt);
  115. } else {
  116. PRINTF("buffer overflow\n");
  117. }
  118. if (received_bsync) {
  119. PRINTF("unexpected BSYNC\n");
  120. received_bsync = 0;
  121. }
  122. }
  123. void ps2mult_callback (int in_cnt)
  124. {
  125. int i;
  126. u_char byte;
  127. static int keyb_handler_active = 0;
  128. if (!init_done) {
  129. return;
  130. }
  131. for (i = 0; i < in_cnt; i ++) {
  132. byte = ps2ser_getc();
  133. if (received_escape) {
  134. ps2mult_receive_byte(byte, received_selector);
  135. received_escape = 0;
  136. } else switch (byte) {
  137. case PS2MULT_ESCAPE:
  138. PRINTF_MULT("ESCAPE receive\n");
  139. received_escape = 1;
  140. break;
  141. case PS2MULT_BSYNC:
  142. PRINTF_MULT("BSYNC receive\n");
  143. received_bsync = 1;
  144. break;
  145. case PS2MULT_KB_SELECTOR:
  146. case PS2MULT_MS_SELECTOR:
  147. PRINTF_MULT("%s receive\n",
  148. byte == PS2MULT_KB_SELECTOR ? "KB_SEL" : "MS_SEL");
  149. received_selector = byte;
  150. break;
  151. case PS2MULT_SESSION_START:
  152. case PS2MULT_SESSION_END:
  153. PRINTF_MULT("%s receive\n",
  154. byte == PS2MULT_SESSION_START ?
  155. "SESSION_START" : "SESSION_END");
  156. break;
  157. default:
  158. ps2mult_receive_byte(byte, received_selector);
  159. }
  160. }
  161. if (keyb_handler && !keyb_handler_active &&
  162. atomic_read(&ps2mult_buf_cnt)) {
  163. keyb_handler_active = 1;
  164. keyb_handler(NULL);
  165. keyb_handler_active = 0;
  166. }
  167. }
  168. u_char ps2mult_read_status(void)
  169. {
  170. u_char byte;
  171. if (atomic_read(&ps2mult_buf_cnt) == 0) {
  172. ps2ser_check();
  173. }
  174. if (atomic_read(&ps2mult_buf_cnt)) {
  175. byte = ps2mult_buf_status[ps2mult_buf_out_idx];
  176. } else {
  177. byte = KBD_STAT_DEFAULT;
  178. }
  179. PRINTF_KEYB("read_status()=0x%02x\n", byte);
  180. return byte;
  181. }
  182. u_char ps2mult_read_input(void)
  183. {
  184. u_char byte = 0;
  185. if (atomic_read(&ps2mult_buf_cnt) == 0) {
  186. ps2ser_check();
  187. }
  188. if (atomic_read(&ps2mult_buf_cnt)) {
  189. byte = ps2mult_buf[ps2mult_buf_out_idx++];
  190. ps2mult_buf_out_idx &= (PS2BUF_SIZE - 1);
  191. atomic_dec(&ps2mult_buf_cnt);
  192. }
  193. PRINTF_KEYB("read_input()=0x%02x\n", byte);
  194. return byte;
  195. }
  196. void ps2mult_write_output(u_char val)
  197. {
  198. int i;
  199. PRINTF_KEYB("write_output(0x%02x)\n", val);
  200. for (i = 0; i < KBD_TIMEOUT; i++) {
  201. if (!kbd_command_active && !mouse_command_active) {
  202. break;
  203. }
  204. udelay(1000);
  205. ps2ser_check();
  206. }
  207. if (kbd_command_active) {
  208. PRINTF("keyboard command not acknoledged\n");
  209. kbd_command_active = 0;
  210. }
  211. if (mouse_command_active) {
  212. PRINTF("mouse command not acknoledged\n");
  213. mouse_command_active = 0;
  214. }
  215. if (ctl_command_active) {
  216. switch (ctl_command_active) {
  217. case KBD_CCMD_WRITE_MODE:
  218. /* Scan code conversion not supported */
  219. command_byte = val & ~KBD_MODE_KCC;
  220. break;
  221. case KBD_CCMD_WRITE_AUX_OBUF:
  222. ps2mult_receive_byte(val, PS2MULT_MS_SELECTOR);
  223. break;
  224. case KBD_CCMD_WRITE_MOUSE:
  225. ps2mult_send_byte(val, PS2MULT_MS_SELECTOR);
  226. break;
  227. default:
  228. PRINTF("invalid controller command\n");
  229. break;
  230. }
  231. ctl_command_active = 0;
  232. return;
  233. }
  234. ps2mult_send_byte(val, PS2MULT_KB_SELECTOR);
  235. }
  236. void ps2mult_write_command(u_char val)
  237. {
  238. ctl_command_active = 0;
  239. PRINTF_KEYB("write_command(0x%02x)\n", val);
  240. switch (val) {
  241. case KBD_CCMD_READ_MODE:
  242. ps2mult_receive_byte(command_byte, PS2MULT_KB_SELECTOR);
  243. break;
  244. case KBD_CCMD_WRITE_MODE:
  245. ctl_command_active = val;
  246. break;
  247. case KBD_CCMD_MOUSE_DISABLE:
  248. break;
  249. case KBD_CCMD_MOUSE_ENABLE:
  250. break;
  251. case KBD_CCMD_SELF_TEST:
  252. ps2mult_receive_byte(0x55, PS2MULT_KB_SELECTOR);
  253. break;
  254. case KBD_CCMD_KBD_TEST:
  255. ps2mult_receive_byte(0x00, PS2MULT_KB_SELECTOR);
  256. break;
  257. case KBD_CCMD_KBD_DISABLE:
  258. break;
  259. case KBD_CCMD_KBD_ENABLE:
  260. break;
  261. case KBD_CCMD_WRITE_AUX_OBUF:
  262. ctl_command_active = val;
  263. break;
  264. case KBD_CCMD_WRITE_MOUSE:
  265. ctl_command_active = val;
  266. break;
  267. default:
  268. PRINTF("invalid controller command\n");
  269. break;
  270. }
  271. }
  272. static int ps2mult_getc_w (void)
  273. {
  274. int res = -1;
  275. int i;
  276. for (i = 0; i < KBD_TIMEOUT; i++) {
  277. if (ps2ser_check()) {
  278. res = ps2ser_getc();
  279. break;
  280. }
  281. udelay(1000);
  282. }
  283. switch (res) {
  284. case PS2MULT_KB_SELECTOR:
  285. case PS2MULT_MS_SELECTOR:
  286. received_selector = res;
  287. break;
  288. default:
  289. break;
  290. }
  291. return res;
  292. }
  293. int ps2mult_init (void)
  294. {
  295. int byte;
  296. int kbd_found = 0;
  297. int mouse_found = 0;
  298. while (get_timer(start_time) < CONFIG_PS2MULT_DELAY);
  299. ps2ser_init();
  300. ps2ser_putc(PS2MULT_SESSION_START);
  301. ps2ser_putc(PS2MULT_KB_SELECTOR);
  302. ps2ser_putc(KBD_CMD_RESET);
  303. do {
  304. byte = ps2mult_getc_w();
  305. } while (byte >= 0 && byte != KBD_REPLY_ACK);
  306. if (byte == KBD_REPLY_ACK) {
  307. byte = ps2mult_getc_w();
  308. if (byte == 0xaa) {
  309. kbd_found = 1;
  310. puts("keyboard");
  311. }
  312. }
  313. if (!kbd_found) {
  314. while (byte >= 0) {
  315. byte = ps2mult_getc_w();
  316. }
  317. }
  318. #if 1 /* detect mouse */
  319. ps2ser_putc(PS2MULT_MS_SELECTOR);
  320. ps2ser_putc(AUX_RESET);
  321. do {
  322. byte = ps2mult_getc_w();
  323. } while (byte >= 0 && byte != AUX_ACK);
  324. if (byte == AUX_ACK) {
  325. byte = ps2mult_getc_w();
  326. if (byte == 0xaa) {
  327. byte = ps2mult_getc_w();
  328. if (byte == 0x00) {
  329. mouse_found = 1;
  330. puts(", mouse");
  331. }
  332. }
  333. }
  334. if (!mouse_found) {
  335. while (byte >= 0) {
  336. byte = ps2mult_getc_w();
  337. }
  338. }
  339. #endif
  340. if (mouse_found || kbd_found) {
  341. if (!received_selector) {
  342. if (mouse_found) {
  343. received_selector = PS2MULT_MS_SELECTOR;
  344. } else {
  345. received_selector = PS2MULT_KB_SELECTOR;
  346. }
  347. }
  348. init_done = 1;
  349. } else {
  350. puts("No device found");
  351. }
  352. puts("\n");
  353. #if 0 /* for testing */
  354. {
  355. int i;
  356. u_char key[] = {
  357. 0x1f, 0x12, 0x14, 0x12, 0x31, 0x2f, 0x39, /* setenv */
  358. 0x1f, 0x14, 0x20, 0x17, 0x31, 0x39, /* stdin */
  359. 0x1f, 0x12, 0x13, 0x17, 0x1e, 0x26, 0x1c, /* serial */
  360. };
  361. for (i = 0; i < sizeof (key); i++) {
  362. ps2mult_receive_byte (key[i], PS2MULT_KB_SELECTOR);
  363. ps2mult_receive_byte (key[i] | 0x80, PS2MULT_KB_SELECTOR);
  364. }
  365. }
  366. #endif
  367. return init_done ? 0 : -1;
  368. }
  369. int ps2mult_request_irq(void (*handler)(void *))
  370. {
  371. keyb_handler = handler;
  372. return 0;
  373. }