3 * John W. Linville, linville@tuxdriver.com
5 * Modified from code for support of MIP405 and PIP405 boards. Previous
9 * Denis Peter, MPL AG Switzerland, d.peter@mpl.ch
11 * See file CREDITS for list of people who contributed to this
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 * Source partly derived from:
31 * linux/drivers/char/pc_keyb.c
36 #include <asm/processor.h>
41 unsigned char kbd_read_status(void);
42 unsigned char kbd_read_input(void);
43 void kbd_send_data(unsigned char data);
44 void i8259_mask_irq(unsigned int irq);
45 void i8259_unmask_irq(unsigned int irq);
47 /* used only by send_data - set by keyboard_interrupt */
54 #define PRINTF(fmt,args...) printf (fmt ,##args)
56 #define PRINTF(fmt,args...)
59 #define KBD_STAT_KOBF 0x01
60 #define KBD_STAT_IBF 0x02
61 #define KBD_STAT_SYS 0x04
62 #define KBD_STAT_CD 0x08
63 #define KBD_STAT_LOCK 0x10
64 #define KBD_STAT_MOBF 0x20
65 #define KBD_STAT_TI_OUT 0x40
66 #define KBD_STAT_PARERR 0x80
68 #define KBD_INIT_TIMEOUT 2000 /* Timeout in ms for initializing the keyboard */
69 #define KBC_TIMEOUT 250 /* Timeout in ms for sending to keyboard controller */
70 #define KBD_TIMEOUT 2000 /* Timeout in ms for keyboard command acknowledge */
72 * Keyboard Controller Commands
75 #define KBD_CCMD_READ_MODE 0x20 /* Read mode bits */
76 #define KBD_CCMD_WRITE_MODE 0x60 /* Write mode bits */
77 #define KBD_CCMD_GET_VERSION 0xA1 /* Get controller version */
78 #define KBD_CCMD_MOUSE_DISABLE 0xA7 /* Disable mouse interface */
79 #define KBD_CCMD_MOUSE_ENABLE 0xA8 /* Enable mouse interface */
80 #define KBD_CCMD_TEST_MOUSE 0xA9 /* Mouse interface test */
81 #define KBD_CCMD_SELF_TEST 0xAA /* Controller self test */
82 #define KBD_CCMD_KBD_TEST 0xAB /* Keyboard interface test */
83 #define KBD_CCMD_KBD_DISABLE 0xAD /* Keyboard interface disable */
84 #define KBD_CCMD_KBD_ENABLE 0xAE /* Keyboard interface enable */
85 #define KBD_CCMD_WRITE_AUX_OBUF 0xD3 /* Write to output buffer as if
86 initiated by the auxiliary device */
87 #define KBD_CCMD_WRITE_MOUSE 0xD4 /* Write the following byte to the mouse */
93 #define KBD_CMD_SET_LEDS 0xED /* Set keyboard leds */
94 #define KBD_CMD_SET_RATE 0xF3 /* Set typematic rate */
95 #define KBD_CMD_ENABLE 0xF4 /* Enable scanning */
96 #define KBD_CMD_DISABLE 0xF5 /* Disable scanning */
97 #define KBD_CMD_RESET 0xFF /* Reset */
103 #define KBD_REPLY_POR 0xAA /* Power on reset */
104 #define KBD_REPLY_ACK 0xFA /* Command ACK */
105 #define KBD_REPLY_RESEND 0xFE /* Command NACK, send the cmd again */
108 * Status Register Bits
111 #define KBD_STAT_OBF 0x01 /* Keyboard output buffer full */
112 #define KBD_STAT_IBF 0x02 /* Keyboard input buffer full */
113 #define KBD_STAT_SELFTEST 0x04 /* Self test successful */
114 #define KBD_STAT_CMD 0x08 /* Last write was a command write (0=data) */
115 #define KBD_STAT_UNLOCKED 0x10 /* Zero if keyboard locked */
116 #define KBD_STAT_MOUSE_OBF 0x20 /* Mouse output buffer full */
117 #define KBD_STAT_GTO 0x40 /* General receive/xmit timeout */
118 #define KBD_STAT_PERR 0x80 /* Parity error */
120 #define AUX_STAT_OBF (KBD_STAT_OBF | KBD_STAT_MOUSE_OBF)
123 * Controller Mode Register Bits
126 #define KBD_MODE_KBD_INT 0x01 /* Keyboard data generate IRQ1 */
127 #define KBD_MODE_MOUSE_INT 0x02 /* Mouse data generate IRQ12 */
128 #define KBD_MODE_SYS 0x04 /* The system flag (?) */
129 #define KBD_MODE_NO_KEYLOCK 0x08 /* The keylock doesn't affect the keyboard if set */
130 #define KBD_MODE_DISABLE_KBD 0x10 /* Disable keyboard interface */
131 #define KBD_MODE_DISABLE_MOUSE 0x20 /* Disable mouse interface */
132 #define KBD_MODE_KCC 0x40 /* Scan code conversion to PC format */
133 #define KBD_MODE_RFU 0x80
136 #define KDB_DATA_PORT 0x60
137 #define KDB_COMMAND_PORT 0x64
139 #define LED_SCR 0x01 /* scroll lock led */
140 #define LED_CAP 0x04 /* caps lock led */
141 #define LED_NUM 0x02 /* num lock led */
143 #define KBD_BUFFER_LEN 0x20 /* size of the keyboardbuffer */
148 static volatile char kbd_buffer[KBD_BUFFER_LEN];
149 static volatile int in_pointer = 0;
150 static volatile int out_pointer = 0;
153 static unsigned char num_lock = 0;
154 static unsigned char caps_lock = 0;
155 static unsigned char scroll_lock = 0;
156 static unsigned char shift = 0;
157 static unsigned char ctrl = 0;
158 static unsigned char alt = 0;
159 static unsigned char e0 = 0;
160 static unsigned char leds = 0;
162 #define DEVNAME "ps2kbd"
164 /* Simple translation table for the keys */
166 static unsigned char kbd_plain_xlate[] = {
167 0xff,0x1b, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=','\b','\t', /* 0x00 - 0x0f */
168 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']','\r',0xff, 'a', 's', /* 0x10 - 0x1f */
169 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';','\'', '`',0xff,'\\', 'z', 'x', 'c', 'v', /* 0x20 - 0x2f */
170 'b', 'n', 'm', ',', '.', '/',0xff,0xff,0xff, ' ',0xff,0xff,0xff,0xff,0xff,0xff, /* 0x30 - 0x3f */
171 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
172 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x50 - 0x5F */
176 static unsigned char kbd_shift_xlate[] = {
177 0xff,0x1b, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+','\b','\t', /* 0x00 - 0x0f */
178 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}','\r',0xff, 'A', 'S', /* 0x10 - 0x1f */
179 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~',0xff, '|', 'Z', 'X', 'C', 'V', /* 0x20 - 0x2f */
180 'B', 'N', 'M', '<', '>', '?',0xff,0xff,0xff, ' ',0xff,0xff,0xff,0xff,0xff,0xff, /* 0x30 - 0x3f */
181 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
182 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x50 - 0x5F */
186 static unsigned char kbd_ctrl_xlate[] = {
187 0xff,0x1b, '1',0x00, '3', '4', '5',0x1E, '7', '8', '9', '0',0x1F, '=','\b','\t', /* 0x00 - 0x0f */
188 0x11,0x17,0x05,0x12,0x14,0x18,0x15,0x09,0x0f,0x10,0x1b,0x1d,'\n',0xff,0x01,0x13, /* 0x10 - 0x1f */
189 0x04,0x06,0x08,0x09,0x0a,0x0b,0x0c, ';','\'', '~',0x00,0x1c,0x1a,0x18,0x03,0x16, /* 0x20 - 0x2f */
190 0x02,0x0e,0x0d, '<', '>', '?',0xff,0xff,0xff,0x00,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x30 - 0x3f */
191 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
192 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x50 - 0x5F */
196 /******************************************************************
198 ******************************************************************/
200 int isa_kbd_init(void)
203 result=kbd_initialize();
206 result = kbd_initialize();
209 printf("AT Keyboard initialized\n");
210 irq_install_handler(KBD_INTERRUPT, (interrupt_handler_t *)kbd_interrupt, NULL);
214 printf("%s\n",result);
219 #ifdef CFG_CONSOLE_OVERWRITE_ROUTINE
220 extern int overwrite_console (void);
222 int overwrite_console (void)
228 int drv_isa_kbd_init (void)
232 char *stdinname = getenv ("stdin");
234 if(isa_kbd_init()==-1)
236 memset (&kbddev, 0, sizeof(kbddev));
237 strcpy(kbddev.name, DEVNAME);
238 kbddev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
241 kbddev.getc = kbd_getc ;
242 kbddev.tstc = kbd_testc ;
244 error = device_register (&kbddev);
246 /* check if this is the standard input device */
247 if(strcmp(stdinname,DEVNAME)==0) {
248 /* reassign the console */
249 if(overwrite_console()) {
252 error=console_assign(stdin,DEVNAME);
263 /******************************************************************
265 ******************************************************************/
266 /* puts character in the queue and sets up the in and out pointer */
267 void kbd_put_queue(char data)
269 if((in_pointer+1)==KBD_BUFFER_LEN) {
271 return; /* buffer full */
276 if((in_pointer+1)==out_pointer)
277 return; /* buffer full */
280 kbd_buffer[in_pointer]=data;
284 /* test if a character is in the queue */
287 if(in_pointer==out_pointer)
288 return(0); /* no data */
292 /* gets the character from the queue */
297 while(in_pointer==out_pointer);
298 if((out_pointer+1)==KBD_BUFFER_LEN)
302 c=kbd_buffer[out_pointer];
310 void kbd_set_leds(void)
313 leds&=~LED_CAP; /* switch caps_lock off */
315 leds|=LED_CAP; /* switch on LED */
317 leds&=~LED_NUM; /* switch LED off */
319 leds|=LED_NUM; /* switch on LED */
321 leds&=~LED_SCR; /* switch LED off */
323 leds|=LED_SCR; /* switch on LED */
324 kbd_send_data(KBD_CMD_SET_LEDS);
329 void handle_keyboard_event(unsigned char scancode)
331 unsigned char keycode;
333 /* Convert scancode to keycode */
334 PRINTF("scancode %x\n",scancode);
336 e0=1; /* special charakters */
340 e0=0; /* delete flag */
341 if(!( ((scancode&0x7F)==0x38)|| /* the right ctrl key */
342 ((scancode&0x7F)==0x1D)|| /* the right alt key */
343 ((scancode&0x7F)==0x35)|| /* the right '/' key */
344 ((scancode&0x7F)==0x1C)|| /* the right enter key */
345 ((scancode)==0x48)|| /* arrow up */
346 ((scancode)==0x50)|| /* arrow down */
347 ((scancode)==0x4b)|| /* arrow left */
348 ((scancode)==0x4d))) /* arrow right */
349 /* we swallow unknown e0 codes */
352 /* special cntrl keys */
375 case 0x58: /* F12 key */
378 extern int console_changed;
379 setenv("stdin", DEVNAME);
380 setenv("stdout", "vga");
385 case 0x36: /* shift pressed */
387 return; /* do nothing else */
389 case 0xB6: /* shift released */
391 return; /* do nothing else */
392 case 0x38: /* alt pressed */
394 return; /* do nothing else */
395 case 0xB8: /* alt released */
397 return; /* do nothing else */
398 case 0x1d: /* ctrl pressed */
400 return; /* do nothing else */
401 case 0x9d: /* ctrl released */
403 return; /* do nothing else */
404 case 0x46: /* scrollock pressed */
405 scroll_lock=~scroll_lock;
407 return; /* do nothing else */
408 case 0x3A: /* capslock pressed */
409 caps_lock=~caps_lock;
412 case 0x45: /* numlock pressed */
416 case 0xC6: /* scroll lock released */
417 case 0xC5: /* num lock released */
418 case 0xBA: /* caps lock released */
419 return; /* just swallow */
421 if((scancode&0x80)==0x80) /* key released */
423 /* now, decide which table we need */
424 if(scancode > (sizeof(kbd_plain_xlate)/sizeof(kbd_plain_xlate[0]))) { /* scancode not in list */
425 PRINTF("unkown scancode %X\n",scancode);
426 return; /* swallow it */
428 /* setup plain code first */
429 keycode=kbd_plain_xlate[scancode];
430 if(caps_lock==1) { /* caps_lock is pressed, overwrite plain code */
431 if(scancode > (sizeof(kbd_shift_xlate)/sizeof(kbd_shift_xlate[0]))) { /* scancode not in list */
432 PRINTF("unkown caps-locked scancode %X\n",scancode);
433 return; /* swallow it */
435 keycode=kbd_shift_xlate[scancode];
436 if(keycode<'A') { /* we only want the alphas capital */
437 keycode=kbd_plain_xlate[scancode];
440 if(shift==1) { /* shift overwrites caps_lock */
441 if(scancode > (sizeof(kbd_shift_xlate)/sizeof(kbd_shift_xlate[0]))) { /* scancode not in list */
442 PRINTF("unkown shifted scancode %X\n",scancode);
443 return; /* swallow it */
445 keycode=kbd_shift_xlate[scancode];
447 if(ctrl==1) { /* ctrl overwrites caps_lock and shift */
448 if(scancode > (sizeof(kbd_ctrl_xlate)/sizeof(kbd_ctrl_xlate[0]))) { /* scancode not in list */
449 PRINTF("unkown ctrl scancode %X\n",scancode);
450 return; /* swallow it */
452 keycode=kbd_ctrl_xlate[scancode];
454 /* check if valid keycode */
456 PRINTF("unkown scancode %X\n",scancode);
457 return; /* swallow unknown codes */
460 kbd_put_queue(keycode);
461 PRINTF("%x\n",keycode);
465 * This reads the keyboard status port, and does the
466 * appropriate action.
469 unsigned char handle_kbd_event(void)
471 unsigned char status = kbd_read_status();
472 unsigned int work = 10000;
474 while ((--work > 0) && (status & KBD_STAT_OBF)) {
475 unsigned char scancode;
477 scancode = kbd_read_input();
479 /* Error bytes must be ignored to make the
480 Synaptics touchpads compaq use work */
481 /* Ignore error bytes */
482 if (!(status & (KBD_STAT_GTO | KBD_STAT_PERR)))
484 if (status & KBD_STAT_MOUSE_OBF)
485 ; /* not supported: handle_mouse_event(scancode); */
487 handle_keyboard_event(scancode);
489 status = kbd_read_status();
492 PRINTF("pc_keyb: controller jammed (0x%02X).\n", status);
498 /******************************************************************************
499 * Lowlevel Part of keyboard section
501 unsigned char kbd_read_status(void)
503 return(in8(CFG_ISA_IO_BASE_ADDRESS + KDB_COMMAND_PORT));
506 unsigned char kbd_read_input(void)
508 return(in8(CFG_ISA_IO_BASE_ADDRESS + KDB_DATA_PORT));
511 void kbd_write_command(unsigned char cmd)
513 out8(CFG_ISA_IO_BASE_ADDRESS + KDB_COMMAND_PORT,cmd);
516 void kbd_write_output(unsigned char data)
518 out8(CFG_ISA_IO_BASE_ADDRESS + KDB_DATA_PORT, data);
521 int kbd_read_data(void)
524 unsigned char status;
527 status = kbd_read_status();
528 if (status & KBD_STAT_OBF) {
529 val = kbd_read_input();
530 if (status & (KBD_STAT_GTO | KBD_STAT_PERR))
536 int kbd_wait_for_input(void)
538 unsigned long timeout;
541 timeout = KBD_TIMEOUT;
556 unsigned long timeout = KBC_TIMEOUT * 10;
559 unsigned char status = handle_kbd_event();
560 if (!(status & KBD_STAT_IBF))
568 void kbd_write_command_w(int data)
571 PRINTF("timeout in kbd_write_command_w\n");
572 kbd_write_command(data);
575 void kbd_write_output_w(int data)
578 PRINTF("timeout in kbd_write_output_w\n");
579 kbd_write_output(data);
582 void kbd_send_data(unsigned char data)
584 unsigned char status;
585 i8259_mask_irq(KBD_INTERRUPT); /* disable interrupt */
586 kbd_write_output_w(data);
587 status = kbd_wait_for_input();
588 if (status == KBD_REPLY_ACK)
589 i8259_unmask_irq(KBD_INTERRUPT); /* enable interrupt */
593 char * kbd_initialize(void)
597 in_pointer = 0; /* delete in Buffer */
600 * Test the keyboard interface.
601 * This seems to be the only way to get it going.
602 * If the test is successful a x55 is placed in the input buffer.
604 kbd_write_command_w(KBD_CCMD_SELF_TEST);
605 if (kbd_wait_for_input() != 0x55)
606 return "Kbd: failed self test";
608 * Perform a keyboard interface test. This causes the controller
609 * to test the keyboard clock and data lines. The results of the
610 * test are placed in the input buffer.
612 kbd_write_command_w(KBD_CCMD_KBD_TEST);
613 if (kbd_wait_for_input() != 0x00)
614 return "Kbd: interface failed self test";
616 * Enable the keyboard by allowing the keyboard clock to run.
618 kbd_write_command_w(KBD_CCMD_KBD_ENABLE);
619 status = kbd_wait_for_input();
621 * Reset keyboard. If the read times out
622 * then the assumption is that no keyboard is
623 * plugged into the machine.
624 * This defaults the keyboard to scan-code set 2.
626 * Set up to try again if the keyboard asks for RESEND.
629 kbd_write_output_w(KBD_CMD_RESET);
630 status = kbd_wait_for_input();
631 if (status == KBD_REPLY_ACK)
633 if (status != KBD_REPLY_RESEND)
635 PRINTF("status: %X\n",status);
636 return "Kbd: reset failed, no ACK";
639 if (kbd_wait_for_input() != KBD_REPLY_POR)
640 return "Kbd: reset failed, no POR";
643 * Set keyboard controller mode. During this, the keyboard should be
644 * in the disabled state.
646 * Set up to try again if the keyboard asks for RESEND.
649 kbd_write_output_w(KBD_CMD_DISABLE);
650 status = kbd_wait_for_input();
651 if (status == KBD_REPLY_ACK)
653 if (status != KBD_REPLY_RESEND)
654 return "Kbd: disable keyboard: no ACK";
657 kbd_write_command_w(KBD_CCMD_WRITE_MODE);
658 kbd_write_output_w(KBD_MODE_KBD_INT
660 | KBD_MODE_DISABLE_MOUSE
663 /* ibm powerpc portables need this to use scan-code set 1 -- Cort */
664 kbd_write_command_w(KBD_CCMD_READ_MODE);
665 if (!(kbd_wait_for_input() & KBD_MODE_KCC)) {
667 * If the controller does not support conversion,
668 * Set the keyboard to scan-code set 1.
670 kbd_write_output_w(0xF0);
671 kbd_wait_for_input();
672 kbd_write_output_w(0x01);
673 kbd_wait_for_input();
675 kbd_write_output_w(KBD_CMD_ENABLE);
676 if (kbd_wait_for_input() != KBD_REPLY_ACK)
677 return "Kbd: enable keyboard: no ACK";
680 * Finally, set the typematic rate to maximum.
682 kbd_write_output_w(KBD_CMD_SET_RATE);
683 if (kbd_wait_for_input() != KBD_REPLY_ACK)
684 return "Kbd: Set rate: no ACK";
685 kbd_write_output_w(0x00);
686 if (kbd_wait_for_input() != KBD_REPLY_ACK)
687 return "Kbd: Set rate: no ACK";
691 void kbd_interrupt(void)