ps2mult.c 8.8 KB

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