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 int init_done = 0;
47 static int received_escape = 0;
48 static int received_bsync = 0;
49 static int received_selector = 0;
51 static int kbd_command_active = 0;
52 static int mouse_command_active = 0;
53 static int ctl_command_active = 0;
55 static u_char command_byte = 0;
57 static void (*keyb_handler)(void *dev_id);
59 static u_char ps2mult_buf [PS2BUF_SIZE];
60 static atomic_t ps2mult_buf_cnt;
61 static int ps2mult_buf_in_idx;
62 static int ps2mult_buf_out_idx;
64 static u_char ps2mult_buf_status [PS2BUF_SIZE];
67 static void ps2mult_send_byte(u_char byte, u_char sel)
71 if (sel == PS2MULT_KB_SELECTOR) {
72 PRINTF_MULT("0x%02x send KEYBOARD\n", byte);
73 kbd_command_active = 1;
75 PRINTF_MULT("0x%02x send MOUSE\n", byte);
76 mouse_command_active = 1;
82 case PS2MULT_KB_SELECTOR:
83 case PS2MULT_MS_SELECTOR:
84 case PS2MULT_SESSION_START:
85 case PS2MULT_SESSION_END:
86 ps2ser_putc(PS2MULT_ESCAPE);
95 static void ps2mult_receive_byte(u_char byte, u_char sel)
97 u_char status = KBD_STAT_DEFAULT;
99 #if 1 /* Ignore mouse in U-Boot */
100 if (sel == PS2MULT_MS_SELECTOR) return;
103 if (sel == PS2MULT_KB_SELECTOR) {
104 if (kbd_command_active) {
105 if (!received_bsync) {
106 PRINTF_MULT("0x%02x lost KEYBOARD !!!\n", byte);
109 kbd_command_active = 0;
113 PRINTF_MULT("0x%02x receive KEYBOARD\n", byte);
114 status |= KBD_STAT_IBF | KBD_STAT_OBF;
116 if (mouse_command_active) {
117 if (!received_bsync) {
118 PRINTF_MULT("0x%02x lost MOUSE !!!\n", byte);
121 mouse_command_active = 0;
125 PRINTF_MULT("0x%02x receive MOUSE\n", byte);
126 status |= KBD_STAT_IBF | KBD_STAT_OBF | KBD_STAT_MOUSE_OBF;
129 if (atomic_read(&ps2mult_buf_cnt) < PS2BUF_SIZE) {
130 ps2mult_buf_status[ps2mult_buf_in_idx] = status;
131 ps2mult_buf[ps2mult_buf_in_idx++] = byte;
132 ps2mult_buf_in_idx &= (PS2BUF_SIZE - 1);
133 atomic_inc(&ps2mult_buf_cnt);
135 PRINTF("buffer overflow\n");
138 if (received_bsync) {
139 PRINTF("unexpected BSYNC\n");
144 void ps2mult_callback (int in_cnt)
148 static int keyb_handler_active = 0;
154 for (i = 0; i < in_cnt; i ++) {
155 byte = ps2ser_getc();
157 if (received_escape) {
158 ps2mult_receive_byte(byte, received_selector);
160 } else switch (byte) {
162 PRINTF_MULT("ESCAPE receive\n");
167 PRINTF_MULT("BSYNC receive\n");
171 case PS2MULT_KB_SELECTOR:
172 case PS2MULT_MS_SELECTOR:
173 PRINTF_MULT("%s receive\n",
174 byte == PS2MULT_KB_SELECTOR ? "KB_SEL" : "MS_SEL");
175 received_selector = byte;
178 case PS2MULT_SESSION_START:
179 case PS2MULT_SESSION_END:
180 PRINTF_MULT("%s receive\n",
181 byte == PS2MULT_SESSION_START ?
182 "SESSION_START" : "SESSION_END");
186 ps2mult_receive_byte(byte, received_selector);
190 if (keyb_handler && !keyb_handler_active &&
191 atomic_read(&ps2mult_buf_cnt)) {
192 keyb_handler_active = 1;
194 keyb_handler_active = 0;
198 u_char ps2mult_read_status(void)
202 if (atomic_read(&ps2mult_buf_cnt) == 0) {
206 if (atomic_read(&ps2mult_buf_cnt)) {
207 byte = ps2mult_buf_status[ps2mult_buf_out_idx];
209 byte = KBD_STAT_DEFAULT;
211 PRINTF_KEYB("read_status()=0x%02x\n", byte);
215 u_char ps2mult_read_input(void)
219 if (atomic_read(&ps2mult_buf_cnt) == 0) {
223 if (atomic_read(&ps2mult_buf_cnt)) {
224 byte = ps2mult_buf[ps2mult_buf_out_idx++];
225 ps2mult_buf_out_idx &= (PS2BUF_SIZE - 1);
226 atomic_dec(&ps2mult_buf_cnt);
228 PRINTF_KEYB("read_input()=0x%02x\n", byte);
232 void ps2mult_write_output(u_char val)
236 PRINTF_KEYB("write_output(0x%02x)\n", val);
238 for (i = 0; i < KBD_TIMEOUT; i++) {
239 if (!kbd_command_active && !mouse_command_active) {
246 if (kbd_command_active) {
247 PRINTF("keyboard command not acknoledged\n");
248 kbd_command_active = 0;
251 if (mouse_command_active) {
252 PRINTF("mouse command not acknoledged\n");
253 mouse_command_active = 0;
256 if (ctl_command_active) {
257 switch (ctl_command_active) {
258 case KBD_CCMD_WRITE_MODE:
259 /* Scan code conversion not supported */
260 command_byte = val & ~KBD_MODE_KCC;
263 case KBD_CCMD_WRITE_AUX_OBUF:
264 ps2mult_receive_byte(val, PS2MULT_MS_SELECTOR);
267 case KBD_CCMD_WRITE_MOUSE:
268 ps2mult_send_byte(val, PS2MULT_MS_SELECTOR);
272 PRINTF("invalid controller command\n");
276 ctl_command_active = 0;
280 ps2mult_send_byte(val, PS2MULT_KB_SELECTOR);
283 void ps2mult_write_command(u_char val)
285 ctl_command_active = 0;
287 PRINTF_KEYB("write_command(0x%02x)\n", val);
290 case KBD_CCMD_READ_MODE:
291 ps2mult_receive_byte(command_byte, PS2MULT_KB_SELECTOR);
294 case KBD_CCMD_WRITE_MODE:
295 ctl_command_active = val;
298 case KBD_CCMD_MOUSE_DISABLE:
301 case KBD_CCMD_MOUSE_ENABLE:
304 case KBD_CCMD_SELF_TEST:
305 ps2mult_receive_byte(0x55, PS2MULT_KB_SELECTOR);
308 case KBD_CCMD_KBD_TEST:
309 ps2mult_receive_byte(0x00, PS2MULT_KB_SELECTOR);
312 case KBD_CCMD_KBD_DISABLE:
315 case KBD_CCMD_KBD_ENABLE:
318 case KBD_CCMD_WRITE_AUX_OBUF:
319 ctl_command_active = val;
322 case KBD_CCMD_WRITE_MOUSE:
323 ctl_command_active = val;
327 PRINTF("invalid controller command\n");
332 static int ps2mult_getc_w (void)
337 for (i = 0; i < KBD_TIMEOUT; i++) {
338 if (ps2ser_check()) {
346 case PS2MULT_KB_SELECTOR:
347 case PS2MULT_MS_SELECTOR:
348 received_selector = res;
357 int ps2mult_init (void)
365 ps2ser_putc(PS2MULT_SESSION_START);
367 ps2ser_putc(PS2MULT_KB_SELECTOR);
368 ps2ser_putc(KBD_CMD_RESET);
371 byte = ps2mult_getc_w();
372 } while (byte >= 0 && byte != KBD_REPLY_ACK);
374 if (byte == KBD_REPLY_ACK) {
375 byte = ps2mult_getc_w();
384 byte = ps2mult_getc_w();
388 #if 1 /* detect mouse */
389 ps2ser_putc(PS2MULT_MS_SELECTOR);
390 ps2ser_putc(AUX_RESET);
393 byte = ps2mult_getc_w();
394 } while (byte >= 0 && byte != AUX_ACK);
396 if (byte == AUX_ACK) {
397 byte = ps2mult_getc_w();
399 byte = ps2mult_getc_w();
409 byte = ps2mult_getc_w();
414 if (mouse_found || kbd_found) {
415 if (!received_selector) {
417 received_selector = PS2MULT_MS_SELECTOR;
419 received_selector = PS2MULT_KB_SELECTOR;
425 puts("No device found");
430 #if 0 /* for testing */
434 0x1f, 0x12, 0x14, 0x12, 0x31, 0x2f, 0x39, /* setenv */
435 0x1f, 0x14, 0x20, 0x17, 0x31, 0x39, /* stdin */
436 0x1f, 0x12, 0x13, 0x17, 0x1e, 0x26, 0x1c, /* serial */
439 for (i = 0; i < sizeof (key); i++) {
440 ps2mult_receive_byte (key[i], PS2MULT_KB_SELECTOR);
441 ps2mult_receive_byte (key[i] | 0x80, PS2MULT_KB_SELECTOR);
446 return init_done ? 0 : -1;
449 int ps2mult_request_irq(void (*handler)(void *))
451 keyb_handler = handler;
456 #endif /* CONFIG_PS2MULT */