1 /***********************************************************************
4 * DENX Software Engineering
5 * Wolfgang Denk, wd@denx.de
8 * PS/2 multiplexer driver
10 * Originally from linux source (drivers/char/ps2mult.c)
12 * Uses simple serial driver (ps2ser.c) to access the multiplexer
13 * Used by PS/2 keyboard driver (pc_keyb.c)
15 ***********************************************************************/
22 #include <asm/atomic.h>
25 /* #define DEBUG_MULT */
26 /* #define DEBUG_KEYB */
28 #define KBD_STAT_DEFAULT (KBD_STAT_SELFTEST | KBD_STAT_UNLOCKED)
30 #define PRINTF(format, args...) printf("ps2mult.c: " format, ## args)
33 #define PRINTF_MULT(format, args...) printf("PS2MULT: " format, ## args)
35 #define PRINTF_MULT(format, args...)
39 #define PRINTF_KEYB(format, args...) printf("KEYB: " format, ## args)
41 #define PRINTF_KEYB(format, args...)
45 static ulong start_time;
46 static int init_done = 0;
48 static int received_escape = 0;
49 static int received_bsync = 0;
50 static int received_selector = 0;
52 static int kbd_command_active = 0;
53 static int mouse_command_active = 0;
54 static int ctl_command_active = 0;
56 static u_char command_byte = 0;
58 static void (*keyb_handler)(void *dev_id);
60 static u_char ps2mult_buf [PS2BUF_SIZE];
61 static atomic_t ps2mult_buf_cnt;
62 static int ps2mult_buf_in_idx;
63 static int ps2mult_buf_out_idx;
65 static u_char ps2mult_buf_status [PS2BUF_SIZE];
67 #ifndef CONFIG_BOARD_EARLY_INIT_R
68 #error #define CONFIG_BOARD_EARLY_INIT_R and call ps2mult_early_init() in board_early_init_r()
70 void ps2mult_early_init (void)
72 start_time = get_timer(0);
75 static void ps2mult_send_byte(u_char byte, u_char sel)
79 if (sel == PS2MULT_KB_SELECTOR) {
80 PRINTF_MULT("0x%02x send KEYBOARD\n", byte);
81 kbd_command_active = 1;
83 PRINTF_MULT("0x%02x send MOUSE\n", byte);
84 mouse_command_active = 1;
90 case PS2MULT_KB_SELECTOR:
91 case PS2MULT_MS_SELECTOR:
92 case PS2MULT_SESSION_START:
93 case PS2MULT_SESSION_END:
94 ps2ser_putc(PS2MULT_ESCAPE);
103 static void ps2mult_receive_byte(u_char byte, u_char sel)
105 u_char status = KBD_STAT_DEFAULT;
107 #if 1 /* Ignore mouse in U-Boot */
108 if (sel == PS2MULT_MS_SELECTOR) return;
111 if (sel == PS2MULT_KB_SELECTOR) {
112 if (kbd_command_active) {
113 if (!received_bsync) {
114 PRINTF_MULT("0x%02x lost KEYBOARD !!!\n", byte);
117 kbd_command_active = 0;
121 PRINTF_MULT("0x%02x receive KEYBOARD\n", byte);
122 status |= KBD_STAT_IBF | KBD_STAT_OBF;
124 if (mouse_command_active) {
125 if (!received_bsync) {
126 PRINTF_MULT("0x%02x lost MOUSE !!!\n", byte);
129 mouse_command_active = 0;
133 PRINTF_MULT("0x%02x receive MOUSE\n", byte);
134 status |= KBD_STAT_IBF | KBD_STAT_OBF | KBD_STAT_MOUSE_OBF;
137 if (atomic_read(&ps2mult_buf_cnt) < PS2BUF_SIZE) {
138 ps2mult_buf_status[ps2mult_buf_in_idx] = status;
139 ps2mult_buf[ps2mult_buf_in_idx++] = byte;
140 ps2mult_buf_in_idx &= (PS2BUF_SIZE - 1);
141 atomic_inc(&ps2mult_buf_cnt);
143 PRINTF("buffer overflow\n");
146 if (received_bsync) {
147 PRINTF("unexpected BSYNC\n");
152 void ps2mult_callback (int in_cnt)
156 static int keyb_handler_active = 0;
162 for (i = 0; i < in_cnt; i ++) {
163 byte = ps2ser_getc();
165 if (received_escape) {
166 ps2mult_receive_byte(byte, received_selector);
168 } else switch (byte) {
170 PRINTF_MULT("ESCAPE receive\n");
175 PRINTF_MULT("BSYNC receive\n");
179 case PS2MULT_KB_SELECTOR:
180 case PS2MULT_MS_SELECTOR:
181 PRINTF_MULT("%s receive\n",
182 byte == PS2MULT_KB_SELECTOR ? "KB_SEL" : "MS_SEL");
183 received_selector = byte;
186 case PS2MULT_SESSION_START:
187 case PS2MULT_SESSION_END:
188 PRINTF_MULT("%s receive\n",
189 byte == PS2MULT_SESSION_START ?
190 "SESSION_START" : "SESSION_END");
194 ps2mult_receive_byte(byte, received_selector);
198 if (keyb_handler && !keyb_handler_active &&
199 atomic_read(&ps2mult_buf_cnt)) {
200 keyb_handler_active = 1;
202 keyb_handler_active = 0;
206 u_char ps2mult_read_status(void)
210 if (atomic_read(&ps2mult_buf_cnt) == 0) {
214 if (atomic_read(&ps2mult_buf_cnt)) {
215 byte = ps2mult_buf_status[ps2mult_buf_out_idx];
217 byte = KBD_STAT_DEFAULT;
219 PRINTF_KEYB("read_status()=0x%02x\n", byte);
223 u_char ps2mult_read_input(void)
227 if (atomic_read(&ps2mult_buf_cnt) == 0) {
231 if (atomic_read(&ps2mult_buf_cnt)) {
232 byte = ps2mult_buf[ps2mult_buf_out_idx++];
233 ps2mult_buf_out_idx &= (PS2BUF_SIZE - 1);
234 atomic_dec(&ps2mult_buf_cnt);
236 PRINTF_KEYB("read_input()=0x%02x\n", byte);
240 void ps2mult_write_output(u_char val)
244 PRINTF_KEYB("write_output(0x%02x)\n", val);
246 for (i = 0; i < KBD_TIMEOUT; i++) {
247 if (!kbd_command_active && !mouse_command_active) {
254 if (kbd_command_active) {
255 PRINTF("keyboard command not acknoledged\n");
256 kbd_command_active = 0;
259 if (mouse_command_active) {
260 PRINTF("mouse command not acknoledged\n");
261 mouse_command_active = 0;
264 if (ctl_command_active) {
265 switch (ctl_command_active) {
266 case KBD_CCMD_WRITE_MODE:
267 /* Scan code conversion not supported */
268 command_byte = val & ~KBD_MODE_KCC;
271 case KBD_CCMD_WRITE_AUX_OBUF:
272 ps2mult_receive_byte(val, PS2MULT_MS_SELECTOR);
275 case KBD_CCMD_WRITE_MOUSE:
276 ps2mult_send_byte(val, PS2MULT_MS_SELECTOR);
280 PRINTF("invalid controller command\n");
284 ctl_command_active = 0;
288 ps2mult_send_byte(val, PS2MULT_KB_SELECTOR);
291 void ps2mult_write_command(u_char val)
293 ctl_command_active = 0;
295 PRINTF_KEYB("write_command(0x%02x)\n", val);
298 case KBD_CCMD_READ_MODE:
299 ps2mult_receive_byte(command_byte, PS2MULT_KB_SELECTOR);
302 case KBD_CCMD_WRITE_MODE:
303 ctl_command_active = val;
306 case KBD_CCMD_MOUSE_DISABLE:
309 case KBD_CCMD_MOUSE_ENABLE:
312 case KBD_CCMD_SELF_TEST:
313 ps2mult_receive_byte(0x55, PS2MULT_KB_SELECTOR);
316 case KBD_CCMD_KBD_TEST:
317 ps2mult_receive_byte(0x00, PS2MULT_KB_SELECTOR);
320 case KBD_CCMD_KBD_DISABLE:
323 case KBD_CCMD_KBD_ENABLE:
326 case KBD_CCMD_WRITE_AUX_OBUF:
327 ctl_command_active = val;
330 case KBD_CCMD_WRITE_MOUSE:
331 ctl_command_active = val;
335 PRINTF("invalid controller command\n");
340 static int ps2mult_getc_w (void)
345 for (i = 0; i < KBD_TIMEOUT; i++) {
346 if (ps2ser_check()) {
354 case PS2MULT_KB_SELECTOR:
355 case PS2MULT_MS_SELECTOR:
356 received_selector = res;
365 int ps2mult_init (void)
371 while (get_timer(start_time) < CONFIG_PS2MULT_DELAY);
375 ps2ser_putc(PS2MULT_SESSION_START);
377 ps2ser_putc(PS2MULT_KB_SELECTOR);
378 ps2ser_putc(KBD_CMD_RESET);
381 byte = ps2mult_getc_w();
382 } while (byte >= 0 && byte != KBD_REPLY_ACK);
384 if (byte == KBD_REPLY_ACK) {
385 byte = ps2mult_getc_w();
394 byte = ps2mult_getc_w();
398 #if 1 /* detect mouse */
399 ps2ser_putc(PS2MULT_MS_SELECTOR);
400 ps2ser_putc(AUX_RESET);
403 byte = ps2mult_getc_w();
404 } while (byte >= 0 && byte != AUX_ACK);
406 if (byte == AUX_ACK) {
407 byte = ps2mult_getc_w();
409 byte = ps2mult_getc_w();
419 byte = ps2mult_getc_w();
424 if (mouse_found || kbd_found) {
425 if (!received_selector) {
427 received_selector = PS2MULT_MS_SELECTOR;
429 received_selector = PS2MULT_KB_SELECTOR;
435 puts("No device found");
440 #if 0 /* for testing */
444 0x1f, 0x12, 0x14, 0x12, 0x31, 0x2f, 0x39, /* setenv */
445 0x1f, 0x14, 0x20, 0x17, 0x31, 0x39, /* stdin */
446 0x1f, 0x12, 0x13, 0x17, 0x1e, 0x26, 0x1c, /* serial */
449 for (i = 0; i < sizeof (key); i++) {
450 ps2mult_receive_byte (key[i], PS2MULT_KB_SELECTOR);
451 ps2mult_receive_byte (key[i] | 0x80, PS2MULT_KB_SELECTOR);
456 return init_done ? 0 : -1;
459 int ps2mult_request_irq(void (*handler)(void *))
461 keyb_handler = handler;
466 #endif /* CONFIG_PS2MULT */