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
);
201 printf("%s\n",result
);
206 #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
207 extern int overwrite_console (void);
209 int overwrite_console (void)
215 int drv_isa_kbd_init (void)
219 char *stdinname
= getenv ("stdin");
221 if(isa_kbd_init()==-1)
223 memset (&kbddev
, 0, sizeof(kbddev
));
224 strcpy(kbddev
.name
, DEVNAME
);
225 kbddev
.flags
= DEV_FLAGS_INPUT
| DEV_FLAGS_SYSTEM
;
228 kbddev
.getc
= kbd_getc
;
229 kbddev
.tstc
= kbd_testc
;
231 error
= device_register (&kbddev
);
233 /* check if this is the standard input device */
234 if(strcmp(stdinname
,DEVNAME
)==0) {
235 /* reassign the console */
236 if(overwrite_console()) {
239 error
=console_assign(stdin
,DEVNAME
);
250 /******************************************************************
252 ******************************************************************/
253 /* puts character in the queue and sets up the in and out pointer */
254 void kbd_put_queue(char data
)
256 if((in_pointer
+1)==KBD_BUFFER_LEN
) {
258 return; /* buffer full */
263 if((in_pointer
+1)==out_pointer
)
264 return; /* buffer full */
267 kbd_buffer
[in_pointer
]=data
;
271 /* test if a character is in the queue */
274 if(in_pointer
==out_pointer
)
275 return(0); /* no data */
279 /* gets the character from the queue */
283 while(in_pointer
==out_pointer
);
284 if((out_pointer
+1)==KBD_BUFFER_LEN
)
288 c
=kbd_buffer
[out_pointer
];
296 void kbd_set_leds(void)
299 leds
&=~LED_CAP
; /* switch caps_lock off */
301 leds
|=LED_CAP
; /* switch on LED */
303 leds
&=~LED_NUM
; /* switch LED off */
305 leds
|=LED_NUM
; /* switch on LED */
307 leds
&=~LED_SCR
; /* switch LED off */
309 leds
|=LED_SCR
; /* switch on LED */
310 kbd_send_data(KBD_CMD_SET_LEDS
);
315 void handle_keyboard_event (unsigned char scancode
)
317 unsigned char keycode
;
319 /* Convert scancode to keycode */
320 PRINTF ("scancode %x\n", scancode
);
321 if (scancode
== 0xe0) {
322 e0
= 1; /* special charakters */
326 e0
= 0; /* delete flag */
327 if (!(((scancode
& 0x7F) == 0x38) || /* the right ctrl key */
328 ((scancode
& 0x7F) == 0x1D) || /* the right alt key */
329 ((scancode
& 0x7F) == 0x35) || /* the right '/' key */
330 ((scancode
& 0x7F) == 0x1C)))
331 /* the right enter key */
332 /* we swallow unknown e0 codes */
335 /* special cntrl keys */
338 case 0x36: /* shift pressed */
340 return; /* do nothing else */
342 case 0xB6: /* shift released */
344 return; /* do nothing else */
345 case 0x38: /* alt pressed */
347 return; /* do nothing else */
348 case 0xB8: /* alt released */
350 return; /* do nothing else */
351 case 0x1d: /* ctrl pressed */
353 return; /* do nothing else */
354 case 0x9d: /* ctrl released */
356 return; /* do nothing else */
357 case 0x46: /* scrollock pressed */
358 scroll_lock
= ~scroll_lock
;
360 return; /* do nothing else */
361 case 0x3A: /* capslock pressed */
362 caps_lock
= ~caps_lock
;
365 case 0x45: /* numlock pressed */
366 num_lock
= ~num_lock
;
369 case 0xC6: /* scroll lock released */
370 case 0xC5: /* num lock released */
371 case 0xBA: /* caps lock released */
372 return; /* just swallow */
374 if ((scancode
& 0x80) == 0x80) /* key released */
376 /* now, decide which table we need */
377 if (scancode
> (sizeof (kbd_plain_xlate
) / sizeof (kbd_plain_xlate
[0]))) { /* scancode not in list */
378 PRINTF ("unkown scancode %X\n", scancode
);
379 return; /* swallow it */
381 /* setup plain code first */
382 keycode
= kbd_plain_xlate
[scancode
];
383 if (caps_lock
== 1) { /* caps_lock is pressed, overwrite plain code */
384 if (scancode
> (sizeof (kbd_shift_xlate
) / sizeof (kbd_shift_xlate
[0]))) { /* scancode not in list */
385 PRINTF ("unkown caps-locked scancode %X\n", scancode
);
386 return; /* swallow it */
388 keycode
= kbd_shift_xlate
[scancode
];
389 if (keycode
< 'A') { /* we only want the alphas capital */
390 keycode
= kbd_plain_xlate
[scancode
];
393 if (shift
== 1) { /* shift overwrites caps_lock */
394 if (scancode
> (sizeof (kbd_shift_xlate
) / sizeof (kbd_shift_xlate
[0]))) { /* scancode not in list */
395 PRINTF ("unkown shifted scancode %X\n", scancode
);
396 return; /* swallow it */
398 keycode
= kbd_shift_xlate
[scancode
];
400 if (ctrl
== 1) { /* ctrl overwrites caps_lock and shift */
401 if (scancode
> (sizeof (kbd_ctrl_xlate
) / sizeof (kbd_ctrl_xlate
[0]))) { /* scancode not in list */
402 PRINTF ("unkown ctrl scancode %X\n", scancode
);
403 return; /* swallow it */
405 keycode
= kbd_ctrl_xlate
[scancode
];
407 /* check if valid keycode */
408 if (keycode
== 0xff) {
409 PRINTF ("unkown scancode %X\n", scancode
);
410 return; /* swallow unknown codes */
413 kbd_put_queue (keycode
);
414 PRINTF ("%x\n", keycode
);
418 * This reads the keyboard status port, and does the
419 * appropriate action.
422 unsigned char handle_kbd_event(void)
424 unsigned char status
= kbd_read_status();
425 unsigned int work
= 10000;
427 while ((--work
> 0) && (status
& KBD_STAT_OBF
)) {
428 unsigned char scancode
;
430 scancode
= kbd_read_input();
432 /* Error bytes must be ignored to make the
433 Synaptics touchpads compaq use work */
434 /* Ignore error bytes */
435 if (!(status
& (KBD_STAT_GTO
| KBD_STAT_PERR
)))
437 if (status
& KBD_STAT_MOUSE_OBF
)
438 ; /* not supported: handle_mouse_event(scancode); */
440 handle_keyboard_event(scancode
);
442 status
= kbd_read_status();
445 PRINTF("pc_keyb: controller jammed (0x%02X).\n", status
);
450 /******************************************************************************
451 * Lowlevel Part of keyboard section
453 unsigned char kbd_read_status(void)
455 return(in8(CONFIG_SYS_ISA_IO_BASE_ADDRESS
+ KDB_COMMAND_PORT
));
458 unsigned char kbd_read_input(void)
460 return(in8(CONFIG_SYS_ISA_IO_BASE_ADDRESS
+ KDB_DATA_PORT
));
463 void kbd_write_command(unsigned char cmd
)
465 out8(CONFIG_SYS_ISA_IO_BASE_ADDRESS
+ KDB_COMMAND_PORT
,cmd
);
468 void kbd_write_output(unsigned char data
)
470 out8(CONFIG_SYS_ISA_IO_BASE_ADDRESS
+ KDB_DATA_PORT
, data
);
473 int kbd_read_data(void)
476 unsigned char status
;
479 status
= kbd_read_status();
480 if (status
& KBD_STAT_OBF
) {
481 val
= kbd_read_input();
482 if (status
& (KBD_STAT_GTO
| KBD_STAT_PERR
))
488 int kbd_wait_for_input(void)
490 unsigned long timeout
;
493 timeout
= KBD_TIMEOUT
;
508 unsigned long timeout
= KBC_TIMEOUT
* 10;
511 unsigned char status
= handle_kbd_event();
512 if (!(status
& KBD_STAT_IBF
))
520 void kbd_write_command_w(int data
)
523 PRINTF("timeout in kbd_write_command_w\n");
524 kbd_write_command(data
);
527 void kbd_write_output_w(int data
)
530 PRINTF("timeout in kbd_write_output_w\n");
531 kbd_write_output(data
);
534 void kbd_send_data(unsigned char data
)
536 unsigned char status
;
537 disable_8259A_irq(1); /* disable interrupt */
538 kbd_write_output_w(data
);
539 status
= kbd_wait_for_input();
540 if (status
== KBD_REPLY_ACK
)
541 enable_8259A_irq(1); /* enable interrupt */
545 char * kbd_initialize(void)
549 in_pointer
= 0; /* delete in Buffer */
552 * Test the keyboard interface.
553 * This seems to be the only way to get it going.
554 * If the test is successful a x55 is placed in the input buffer.
556 kbd_write_command_w(KBD_CCMD_SELF_TEST
);
557 if (kbd_wait_for_input() != 0x55)
558 return "Kbd: failed self test";
560 * Perform a keyboard interface test. This causes the controller
561 * to test the keyboard clock and data lines. The results of the
562 * test are placed in the input buffer.
564 kbd_write_command_w(KBD_CCMD_KBD_TEST
);
565 if (kbd_wait_for_input() != 0x00)
566 return "Kbd: interface failed self test";
568 * Enable the keyboard by allowing the keyboard clock to run.
570 kbd_write_command_w(KBD_CCMD_KBD_ENABLE
);
571 status
= kbd_wait_for_input();
573 * Reset keyboard. If the read times out
574 * then the assumption is that no keyboard is
575 * plugged into the machine.
576 * This defaults the keyboard to scan-code set 2.
578 * Set up to try again if the keyboard asks for RESEND.
581 kbd_write_output_w(KBD_CMD_RESET
);
582 status
= kbd_wait_for_input();
583 if (status
== KBD_REPLY_ACK
)
585 if (status
!= KBD_REPLY_RESEND
) {
586 PRINTF("status: %X\n",status
);
587 return "Kbd: reset failed, no ACK";
590 if (kbd_wait_for_input() != KBD_REPLY_POR
)
591 return "Kbd: reset failed, no POR";
594 * Set keyboard controller mode. During this, the keyboard should be
595 * in the disabled state.
597 * Set up to try again if the keyboard asks for RESEND.
600 kbd_write_output_w(KBD_CMD_DISABLE
);
601 status
= kbd_wait_for_input();
602 if (status
== KBD_REPLY_ACK
)
604 if (status
!= KBD_REPLY_RESEND
)
605 return "Kbd: disable keyboard: no ACK";
608 kbd_write_command_w(KBD_CCMD_WRITE_MODE
);
609 kbd_write_output_w(KBD_MODE_KBD_INT
611 | KBD_MODE_DISABLE_MOUSE
614 /* AMCC powerpc portables need this to use scan-code set 1 -- Cort */
615 kbd_write_command_w(KBD_CCMD_READ_MODE
);
616 if (!(kbd_wait_for_input() & KBD_MODE_KCC
)) {
618 * If the controller does not support conversion,
619 * Set the keyboard to scan-code set 1.
621 kbd_write_output_w(0xF0);
622 kbd_wait_for_input();
623 kbd_write_output_w(0x01);
624 kbd_wait_for_input();
626 kbd_write_output_w(KBD_CMD_ENABLE
);
627 if (kbd_wait_for_input() != KBD_REPLY_ACK
)
628 return "Kbd: enable keyboard: no ACK";
631 * Finally, set the typematic rate to maximum.
633 kbd_write_output_w(KBD_CMD_SET_RATE
);
634 if (kbd_wait_for_input() != KBD_REPLY_ACK
)
635 return "Kbd: Set rate: no ACK";
636 kbd_write_output_w(0x00);
637 if (kbd_wait_for_input() != KBD_REPLY_ACK
)
638 return "Kbd: Set rate: no ACK";
642 void kbd_interrupt(void)