3 * Denis Peter, MPL AG Switzerland, d.peter@mpl.ch
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 * Source partly derived from:
25 * linux/drivers/char/pc_keyb.c
30 #include <asm/processor.h>
36 unsigned char kbd_read_status(void);
37 unsigned char kbd_read_input(void);
38 void kbd_send_data(unsigned char data
);
39 void disable_8259A_irq(unsigned int irq
);
40 void enable_8259A_irq(unsigned int irq
);
42 /* used only by send_data - set by keyboard_interrupt */
48 #define PRINTF(fmt,args...) printf (fmt ,##args)
50 #define PRINTF(fmt,args...)
53 #define KBD_STAT_KOBF 0x01
54 #define KBD_STAT_IBF 0x02
55 #define KBD_STAT_SYS 0x04
56 #define KBD_STAT_CD 0x08
57 #define KBD_STAT_LOCK 0x10
58 #define KBD_STAT_MOBF 0x20
59 #define KBD_STAT_TI_OUT 0x40
60 #define KBD_STAT_PARERR 0x80
62 #define KBD_INIT_TIMEOUT 1000 /* Timeout in ms for initializing the keyboard */
63 #define KBC_TIMEOUT 250 /* Timeout in ms for sending to keyboard controller */
64 #define KBD_TIMEOUT 2000 /* Timeout in ms for keyboard command acknowledge */
66 * Keyboard Controller Commands
69 #define KBD_CCMD_READ_MODE 0x20 /* Read mode bits */
70 #define KBD_CCMD_WRITE_MODE 0x60 /* Write mode bits */
71 #define KBD_CCMD_GET_VERSION 0xA1 /* Get controller version */
72 #define KBD_CCMD_MOUSE_DISABLE 0xA7 /* Disable mouse interface */
73 #define KBD_CCMD_MOUSE_ENABLE 0xA8 /* Enable mouse interface */
74 #define KBD_CCMD_TEST_MOUSE 0xA9 /* Mouse interface test */
75 #define KBD_CCMD_SELF_TEST 0xAA /* Controller self test */
76 #define KBD_CCMD_KBD_TEST 0xAB /* Keyboard interface test */
77 #define KBD_CCMD_KBD_DISABLE 0xAD /* Keyboard interface disable */
78 #define KBD_CCMD_KBD_ENABLE 0xAE /* Keyboard interface enable */
79 #define KBD_CCMD_WRITE_AUX_OBUF 0xD3 /* Write to output buffer as if
80 initiated by the auxiliary device */
81 #define KBD_CCMD_WRITE_MOUSE 0xD4 /* Write the following byte to the mouse */
87 #define KBD_CMD_SET_LEDS 0xED /* Set keyboard leds */
88 #define KBD_CMD_SET_RATE 0xF3 /* Set typematic rate */
89 #define KBD_CMD_ENABLE 0xF4 /* Enable scanning */
90 #define KBD_CMD_DISABLE 0xF5 /* Disable scanning */
91 #define KBD_CMD_RESET 0xFF /* Reset */
97 #define KBD_REPLY_POR 0xAA /* Power on reset */
98 #define KBD_REPLY_ACK 0xFA /* Command ACK */
99 #define KBD_REPLY_RESEND 0xFE /* Command NACK, send the cmd again */
102 * Status Register Bits
105 #define KBD_STAT_OBF 0x01 /* Keyboard output buffer full */
106 #define KBD_STAT_IBF 0x02 /* Keyboard input buffer full */
107 #define KBD_STAT_SELFTEST 0x04 /* Self test successful */
108 #define KBD_STAT_CMD 0x08 /* Last write was a command write (0=data) */
109 #define KBD_STAT_UNLOCKED 0x10 /* Zero if keyboard locked */
110 #define KBD_STAT_MOUSE_OBF 0x20 /* Mouse output buffer full */
111 #define KBD_STAT_GTO 0x40 /* General receive/xmit timeout */
112 #define KBD_STAT_PERR 0x80 /* Parity error */
114 #define AUX_STAT_OBF (KBD_STAT_OBF | KBD_STAT_MOUSE_OBF)
117 * Controller Mode Register Bits
120 #define KBD_MODE_KBD_INT 0x01 /* Keyboard data generate IRQ1 */
121 #define KBD_MODE_MOUSE_INT 0x02 /* Mouse data generate IRQ12 */
122 #define KBD_MODE_SYS 0x04 /* The system flag (?) */
123 #define KBD_MODE_NO_KEYLOCK 0x08 /* The keylock doesn't affect the keyboard if set */
124 #define KBD_MODE_DISABLE_KBD 0x10 /* Disable keyboard interface */
125 #define KBD_MODE_DISABLE_MOUSE 0x20 /* Disable mouse interface */
126 #define KBD_MODE_KCC 0x40 /* Scan code conversion to PC format */
127 #define KBD_MODE_RFU 0x80
130 #define KDB_DATA_PORT 0x60
131 #define KDB_COMMAND_PORT 0x64
133 #define LED_SCR 0x01 /* scroll lock led */
134 #define LED_CAP 0x04 /* caps lock led */
135 #define LED_NUM 0x02 /* num lock led */
137 #define KBD_BUFFER_LEN 0x20 /* size of the keyboardbuffer */
140 static volatile char kbd_buffer
[KBD_BUFFER_LEN
];
141 static volatile int in_pointer
= 0;
142 static volatile int out_pointer
= 0;
145 static unsigned char num_lock
= 0;
146 static unsigned char caps_lock
= 0;
147 static unsigned char scroll_lock
= 0;
148 static unsigned char shift
= 0;
149 static unsigned char ctrl
= 0;
150 static unsigned char alt
= 0;
151 static unsigned char e0
= 0;
152 static unsigned char leds
= 0;
154 #define DEVNAME "kbd"
156 /* Simple translation table for the keys */
158 static unsigned char kbd_plain_xlate
[] = {
159 0xff,0x1b, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=','\b','\t', /* 0x00 - 0x0f */
160 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']','\r',0xff, 'a', 's', /* 0x10 - 0x1f */
161 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';','\'', '`',0xff,'\\', 'z', 'x', 'c', 'v', /* 0x20 - 0x2f */
162 'b', 'n', 'm', ',', '.', '/',0xff,0xff,0xff, ' ',0xff,0xff,0xff,0xff,0xff,0xff, /* 0x30 - 0x3f */
163 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
164 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x50 - 0x5F */
168 static unsigned char kbd_shift_xlate
[] = {
169 0xff,0x1b, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+','\b','\t', /* 0x00 - 0x0f */
170 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}','\r',0xff, 'A', 'S', /* 0x10 - 0x1f */
171 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~',0xff, '|', 'Z', 'X', 'C', 'V', /* 0x20 - 0x2f */
172 'B', 'N', 'M', '<', '>', '?',0xff,0xff,0xff, ' ',0xff,0xff,0xff,0xff,0xff,0xff, /* 0x30 - 0x3f */
173 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
174 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x50 - 0x5F */
178 static unsigned char kbd_ctrl_xlate
[] = {
179 0xff,0x1b, '1',0x00, '3', '4', '5',0x1E, '7', '8', '9', '0',0x1F, '=','\b','\t', /* 0x00 - 0x0f */
180 0x11,0x17,0x05,0x12,0x14,0x18,0x15,0x09,0x0f,0x10,0x1b,0x1d,'\n',0xff,0x01,0x13, /* 0x10 - 0x1f */
181 0x04,0x06,0x08,0x09,0x0a,0x0b,0x0c, ';','\'', '~',0x00,0x1c,0x1a,0x18,0x03,0x16, /* 0x20 - 0x2f */
182 0x02,0x0e,0x0d, '<', '>', '?',0xff,0xff,0xff,0x00,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x30 - 0x3f */
183 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
184 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x50 - 0x5F */
188 /******************************************************************
190 ******************************************************************/
191 int isa_kbd_init(void)
194 result
=kbd_initialize();
196 PRINTF("AT Keyboard initialized\n");
197 irq_install_handler(25, (interrupt_handler_t
*)handle_isa_int
, NULL
);
198 isa_irq_install_handler(KBD_INTERRUPT
, (interrupt_handler_t
*)kbd_interrupt
, NULL
);
202 printf("%s\n",result
);
207 #ifdef CFG_CONSOLE_OVERWRITE_ROUTINE
208 extern int overwrite_console (void);
210 int overwrite_console (void)
216 int drv_isa_kbd_init (void)
220 char *stdinname
= getenv ("stdin");
222 if(isa_kbd_init()==-1)
224 memset (&kbddev
, 0, sizeof(kbddev
));
225 strcpy(kbddev
.name
, DEVNAME
);
226 kbddev
.flags
= DEV_FLAGS_INPUT
| DEV_FLAGS_SYSTEM
;
229 kbddev
.getc
= kbd_getc
;
230 kbddev
.tstc
= kbd_testc
;
232 error
= device_register (&kbddev
);
234 /* check if this is the standard input device */
235 if(strcmp(stdinname
,DEVNAME
)==0) {
236 /* reassign the console */
237 if(overwrite_console()) {
240 error
=console_assign(stdin
,DEVNAME
);
251 /******************************************************************
253 ******************************************************************/
254 /* puts character in the queue and sets up the in and out pointer */
255 void kbd_put_queue(char data
)
257 if((in_pointer
+1)==KBD_BUFFER_LEN
) {
259 return; /* buffer full */
264 if((in_pointer
+1)==out_pointer
)
265 return; /* buffer full */
268 kbd_buffer
[in_pointer
]=data
;
272 /* test if a character is in the queue */
275 if(in_pointer
==out_pointer
)
276 return(0); /* no data */
280 /* gets the character from the queue */
284 while(in_pointer
==out_pointer
);
285 if((out_pointer
+1)==KBD_BUFFER_LEN
)
289 c
=kbd_buffer
[out_pointer
];
297 void kbd_set_leds(void)
300 leds
&=~LED_CAP
; /* switch caps_lock off */
302 leds
|=LED_CAP
; /* switch on LED */
304 leds
&=~LED_NUM
; /* switch LED off */
306 leds
|=LED_NUM
; /* switch on LED */
308 leds
&=~LED_SCR
; /* switch LED off */
310 leds
|=LED_SCR
; /* switch on LED */
311 kbd_send_data(KBD_CMD_SET_LEDS
);
316 void handle_keyboard_event(unsigned char scancode
)
318 unsigned char keycode
;
320 /* Convert scancode to keycode */
321 PRINTF("scancode %x\n",scancode
);
323 e0
=1; /* special charakters */
327 e0
=0; /* delete flag */
328 if(!( ((scancode
&0x7F)==0x38)|| /* the right ctrl key */
329 ((scancode
&0x7F)==0x1D)|| /* the right alt key */
330 ((scancode
&0x7F)==0x35)|| /* the right '/' key */
331 ((scancode
&0x7F)==0x1C) )) /* the right enter key */
332 /* we swallow unknown e0 codes */
335 /* special cntrl keys */
339 case 0x36: /* shift pressed */
341 return; /* do nothing else */
343 case 0xB6: /* shift released */
345 return; /* do nothing else */
346 case 0x38: /* alt pressed */
348 return; /* do nothing else */
349 case 0xB8: /* alt released */
351 return; /* do nothing else */
352 case 0x1d: /* ctrl pressed */
354 return; /* do nothing else */
355 case 0x9d: /* ctrl released */
357 return; /* do nothing else */
358 case 0x46: /* scrollock pressed */
359 scroll_lock
=~scroll_lock
;
361 return; /* do nothing else */
362 case 0x3A: /* capslock pressed */
363 caps_lock
=~caps_lock
;
366 case 0x45: /* numlock pressed */
370 case 0xC6: /* scroll lock released */
371 case 0xC5: /* num lock released */
372 case 0xBA: /* caps lock released */
373 return; /* just swallow */
375 if((scancode
&0x80)==0x80) /* key released */
377 /* now, decide which table we need */
378 if(scancode
> (sizeof(kbd_plain_xlate
)/sizeof(kbd_plain_xlate
[0]))) { /* scancode not in list */
379 PRINTF("unkown scancode %X\n",scancode
);
380 return; /* swallow it */
382 /* setup plain code first */
383 keycode
=kbd_plain_xlate
[scancode
];
384 if(caps_lock
==1) { /* caps_lock is pressed, overwrite plain code */
385 if(scancode
> (sizeof(kbd_shift_xlate
)/sizeof(kbd_shift_xlate
[0]))) { /* scancode not in list */
386 PRINTF("unkown caps-locked scancode %X\n",scancode
);
387 return; /* swallow it */
389 keycode
=kbd_shift_xlate
[scancode
];
390 if(keycode
<'A') { /* we only want the alphas capital */
391 keycode
=kbd_plain_xlate
[scancode
];
394 if(shift
==1) { /* shift overwrites caps_lock */
395 if(scancode
> (sizeof(kbd_shift_xlate
)/sizeof(kbd_shift_xlate
[0]))) { /* scancode not in list */
396 PRINTF("unkown shifted scancode %X\n",scancode
);
397 return; /* swallow it */
399 keycode
=kbd_shift_xlate
[scancode
];
401 if(ctrl
==1) { /* ctrl overwrites caps_lock and shift */
402 if(scancode
> (sizeof(kbd_ctrl_xlate
)/sizeof(kbd_ctrl_xlate
[0]))) { /* scancode not in list */
403 PRINTF("unkown ctrl scancode %X\n",scancode
);
404 return; /* swallow it */
406 keycode
=kbd_ctrl_xlate
[scancode
];
408 /* check if valid keycode */
410 PRINTF("unkown scancode %X\n",scancode
);
411 return; /* swallow unknown codes */
414 kbd_put_queue(keycode
);
415 PRINTF("%x\n",keycode
);
419 * This reads the keyboard status port, and does the
420 * appropriate action.
423 unsigned char handle_kbd_event(void)
425 unsigned char status
= kbd_read_status();
426 unsigned int work
= 10000;
428 while ((--work
> 0) && (status
& KBD_STAT_OBF
)) {
429 unsigned char scancode
;
431 scancode
= kbd_read_input();
433 /* Error bytes must be ignored to make the
434 Synaptics touchpads compaq use work */
435 /* Ignore error bytes */
436 if (!(status
& (KBD_STAT_GTO
| KBD_STAT_PERR
)))
438 if (status
& KBD_STAT_MOUSE_OBF
)
439 ; /* not supported: handle_mouse_event(scancode); */
441 handle_keyboard_event(scancode
);
443 status
= kbd_read_status();
446 PRINTF("pc_keyb: controller jammed (0x%02X).\n", status
);
451 /******************************************************************************
452 * Lowlevel Part of keyboard section
454 unsigned char kbd_read_status(void)
456 return(in8(CFG_ISA_IO_BASE_ADDRESS
+ KDB_COMMAND_PORT
));
459 unsigned char kbd_read_input(void)
461 return(in8(CFG_ISA_IO_BASE_ADDRESS
+ KDB_DATA_PORT
));
464 void kbd_write_command(unsigned char cmd
)
466 out8(CFG_ISA_IO_BASE_ADDRESS
+ KDB_COMMAND_PORT
,cmd
);
469 void kbd_write_output(unsigned char data
)
471 out8(CFG_ISA_IO_BASE_ADDRESS
+ KDB_DATA_PORT
, data
);
474 int kbd_read_data(void)
477 unsigned char status
;
480 status
= kbd_read_status();
481 if (status
& KBD_STAT_OBF
) {
482 val
= kbd_read_input();
483 if (status
& (KBD_STAT_GTO
| KBD_STAT_PERR
))
489 int kbd_wait_for_input(void)
491 unsigned long timeout
;
494 timeout
= KBD_TIMEOUT
;
509 unsigned long timeout
= KBC_TIMEOUT
* 10;
512 unsigned char status
= handle_kbd_event();
513 if (!(status
& KBD_STAT_IBF
))
521 void kbd_write_command_w(int data
)
524 PRINTF("timeout in kbd_write_command_w\n");
525 kbd_write_command(data
);
528 void kbd_write_output_w(int data
)
531 PRINTF("timeout in kbd_write_output_w\n");
532 kbd_write_output(data
);
535 void kbd_send_data(unsigned char data
)
537 unsigned char status
;
538 disable_8259A_irq(1); /* disable interrupt */
539 kbd_write_output_w(data
);
540 status
= kbd_wait_for_input();
541 if (status
== KBD_REPLY_ACK
)
542 enable_8259A_irq(1); /* enable interrupt */
546 char * kbd_initialize(void)
550 in_pointer
= 0; /* delete in Buffer */
553 * Test the keyboard interface.
554 * This seems to be the only way to get it going.
555 * If the test is successful a x55 is placed in the input buffer.
557 kbd_write_command_w(KBD_CCMD_SELF_TEST
);
558 if (kbd_wait_for_input() != 0x55)
559 return "Kbd: failed self test";
561 * Perform a keyboard interface test. This causes the controller
562 * to test the keyboard clock and data lines. The results of the
563 * test are placed in the input buffer.
565 kbd_write_command_w(KBD_CCMD_KBD_TEST
);
566 if (kbd_wait_for_input() != 0x00)
567 return "Kbd: interface failed self test";
569 * Enable the keyboard by allowing the keyboard clock to run.
571 kbd_write_command_w(KBD_CCMD_KBD_ENABLE
);
572 status
= kbd_wait_for_input();
574 * Reset keyboard. If the read times out
575 * then the assumption is that no keyboard is
576 * plugged into the machine.
577 * This defaults the keyboard to scan-code set 2.
579 * Set up to try again if the keyboard asks for RESEND.
582 kbd_write_output_w(KBD_CMD_RESET
);
583 status
= kbd_wait_for_input();
584 if (status
== KBD_REPLY_ACK
)
586 if (status
!= KBD_REPLY_RESEND
)
588 PRINTF("status: %X\n",status
);
589 return "Kbd: reset failed, no ACK";
592 if (kbd_wait_for_input() != KBD_REPLY_POR
)
593 return "Kbd: reset failed, no POR";
596 * Set keyboard controller mode. During this, the keyboard should be
597 * in the disabled state.
599 * Set up to try again if the keyboard asks for RESEND.
602 kbd_write_output_w(KBD_CMD_DISABLE
);
603 status
= kbd_wait_for_input();
604 if (status
== KBD_REPLY_ACK
)
606 if (status
!= KBD_REPLY_RESEND
)
607 return "Kbd: disable keyboard: no ACK";
610 kbd_write_command_w(KBD_CCMD_WRITE_MODE
);
611 kbd_write_output_w(KBD_MODE_KBD_INT
613 | KBD_MODE_DISABLE_MOUSE
616 /* AMCC powerpc portables need this to use scan-code set 1 -- Cort */
617 kbd_write_command_w(KBD_CCMD_READ_MODE
);
618 if (!(kbd_wait_for_input() & KBD_MODE_KCC
)) {
620 * If the controller does not support conversion,
621 * Set the keyboard to scan-code set 1.
623 kbd_write_output_w(0xF0);
624 kbd_wait_for_input();
625 kbd_write_output_w(0x01);
626 kbd_wait_for_input();
628 kbd_write_output_w(KBD_CMD_ENABLE
);
629 if (kbd_wait_for_input() != KBD_REPLY_ACK
)
630 return "Kbd: enable keyboard: no ACK";
633 * Finally, set the typematic rate to maximum.
635 kbd_write_output_w(KBD_CMD_SET_RATE
);
636 if (kbd_wait_for_input() != KBD_REPLY_ACK
)
637 return "Kbd: Set rate: no ACK";
638 kbd_write_output_w(0x00);
639 if (kbd_wait_for_input() != KBD_REPLY_ACK
)
640 return "Kbd: Set rate: no ACK";
644 void kbd_interrupt(void)