ps2mult.c 8.9 KB

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