3 * Denis Peter, MPL AG Switzerland, d.peter@mpl.ch
5 * SPDX-License-Identifier: GPL-2.0+
7 * Source partly derived from:
8 * linux/drivers/char/pc_keyb.c
12 #include <asm/processor.h>
13 #include <stdio_dev.h>
18 unsigned char kbd_read_status(void);
19 unsigned char kbd_read_input(void);
20 void kbd_send_data(unsigned char data);
21 void disable_8259A_irq(unsigned int irq);
22 void enable_8259A_irq(unsigned int irq);
24 /* used only by send_data - set by keyboard_interrupt */
30 #define PRINTF(fmt,args...) printf (fmt ,##args)
32 #define PRINTF(fmt,args...)
35 #define KBD_STAT_KOBF 0x01
36 #define KBD_STAT_IBF 0x02
37 #define KBD_STAT_SYS 0x04
38 #define KBD_STAT_CD 0x08
39 #define KBD_STAT_LOCK 0x10
40 #define KBD_STAT_MOBF 0x20
41 #define KBD_STAT_TI_OUT 0x40
42 #define KBD_STAT_PARERR 0x80
44 #define KBD_INIT_TIMEOUT 1000 /* Timeout in ms for initializing the keyboard */
45 #define KBC_TIMEOUT 250 /* Timeout in ms for sending to keyboard controller */
46 #define KBD_TIMEOUT 2000 /* Timeout in ms for keyboard command acknowledge */
48 * Keyboard Controller Commands
51 #define KBD_CCMD_READ_MODE 0x20 /* Read mode bits */
52 #define KBD_CCMD_WRITE_MODE 0x60 /* Write mode bits */
53 #define KBD_CCMD_GET_VERSION 0xA1 /* Get controller version */
54 #define KBD_CCMD_MOUSE_DISABLE 0xA7 /* Disable mouse interface */
55 #define KBD_CCMD_MOUSE_ENABLE 0xA8 /* Enable mouse interface */
56 #define KBD_CCMD_TEST_MOUSE 0xA9 /* Mouse interface test */
57 #define KBD_CCMD_SELF_TEST 0xAA /* Controller self test */
58 #define KBD_CCMD_KBD_TEST 0xAB /* Keyboard interface test */
59 #define KBD_CCMD_KBD_DISABLE 0xAD /* Keyboard interface disable */
60 #define KBD_CCMD_KBD_ENABLE 0xAE /* Keyboard interface enable */
61 #define KBD_CCMD_WRITE_AUX_OBUF 0xD3 /* Write to output buffer as if
62 initiated by the auxiliary device */
63 #define KBD_CCMD_WRITE_MOUSE 0xD4 /* Write the following byte to the mouse */
69 #define KBD_CMD_SET_LEDS 0xED /* Set keyboard leds */
70 #define KBD_CMD_SET_RATE 0xF3 /* Set typematic rate */
71 #define KBD_CMD_ENABLE 0xF4 /* Enable scanning */
72 #define KBD_CMD_DISABLE 0xF5 /* Disable scanning */
73 #define KBD_CMD_RESET 0xFF /* Reset */
79 #define KBD_REPLY_POR 0xAA /* Power on reset */
80 #define KBD_REPLY_ACK 0xFA /* Command ACK */
81 #define KBD_REPLY_RESEND 0xFE /* Command NACK, send the cmd again */
84 * Status Register Bits
87 #define KBD_STAT_OBF 0x01 /* Keyboard output buffer full */
88 #define KBD_STAT_IBF 0x02 /* Keyboard input buffer full */
89 #define KBD_STAT_SELFTEST 0x04 /* Self test successful */
90 #define KBD_STAT_CMD 0x08 /* Last write was a command write (0=data) */
91 #define KBD_STAT_UNLOCKED 0x10 /* Zero if keyboard locked */
92 #define KBD_STAT_MOUSE_OBF 0x20 /* Mouse output buffer full */
93 #define KBD_STAT_GTO 0x40 /* General receive/xmit timeout */
94 #define KBD_STAT_PERR 0x80 /* Parity error */
96 #define AUX_STAT_OBF (KBD_STAT_OBF | KBD_STAT_MOUSE_OBF)
99 * Controller Mode Register Bits
102 #define KBD_MODE_KBD_INT 0x01 /* Keyboard data generate IRQ1 */
103 #define KBD_MODE_MOUSE_INT 0x02 /* Mouse data generate IRQ12 */
104 #define KBD_MODE_SYS 0x04 /* The system flag (?) */
105 #define KBD_MODE_NO_KEYLOCK 0x08 /* The keylock doesn't affect the keyboard if set */
106 #define KBD_MODE_DISABLE_KBD 0x10 /* Disable keyboard interface */
107 #define KBD_MODE_DISABLE_MOUSE 0x20 /* Disable mouse interface */
108 #define KBD_MODE_KCC 0x40 /* Scan code conversion to PC format */
109 #define KBD_MODE_RFU 0x80
112 #define KDB_DATA_PORT 0x60
113 #define KDB_COMMAND_PORT 0x64
115 #define LED_SCR 0x01 /* scroll lock led */
116 #define LED_CAP 0x04 /* caps lock led */
117 #define LED_NUM 0x02 /* num lock led */
119 #define KBD_BUFFER_LEN 0x20 /* size of the keyboardbuffer */
122 static volatile char kbd_buffer[KBD_BUFFER_LEN];
123 static volatile int in_pointer = 0;
124 static volatile int out_pointer = 0;
127 static unsigned char num_lock = 0;
128 static unsigned char caps_lock = 0;
129 static unsigned char scroll_lock = 0;
130 static unsigned char shift = 0;
131 static unsigned char ctrl = 0;
132 static unsigned char alt = 0;
133 static unsigned char e0 = 0;
134 static unsigned char leds = 0;
136 #define DEVNAME "kbd"
138 /* Simple translation table for the keys */
140 static unsigned char kbd_plain_xlate[] = {
141 0xff,0x1b, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=','\b','\t', /* 0x00 - 0x0f */
142 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']','\r',0xff, 'a', 's', /* 0x10 - 0x1f */
143 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';','\'', '`',0xff,'\\', 'z', 'x', 'c', 'v', /* 0x20 - 0x2f */
144 'b', 'n', 'm', ',', '.', '/',0xff,0xff,0xff, ' ',0xff,0xff,0xff,0xff,0xff,0xff, /* 0x30 - 0x3f */
145 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
146 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x50 - 0x5F */
150 static unsigned char kbd_shift_xlate[] = {
151 0xff,0x1b, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+','\b','\t', /* 0x00 - 0x0f */
152 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}','\r',0xff, 'A', 'S', /* 0x10 - 0x1f */
153 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~',0xff, '|', 'Z', 'X', 'C', 'V', /* 0x20 - 0x2f */
154 'B', 'N', 'M', '<', '>', '?',0xff,0xff,0xff, ' ',0xff,0xff,0xff,0xff,0xff,0xff, /* 0x30 - 0x3f */
155 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
156 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x50 - 0x5F */
160 static unsigned char kbd_ctrl_xlate[] = {
161 0xff,0x1b, '1',0x00, '3', '4', '5',0x1E, '7', '8', '9', '0',0x1F, '=','\b','\t', /* 0x00 - 0x0f */
162 0x11,0x17,0x05,0x12,0x14,0x18,0x15,0x09,0x0f,0x10,0x1b,0x1d,'\n',0xff,0x01,0x13, /* 0x10 - 0x1f */
163 0x04,0x06,0x08,0x09,0x0a,0x0b,0x0c, ';','\'', '~',0x00,0x1c,0x1a,0x18,0x03,0x16, /* 0x20 - 0x2f */
164 0x02,0x0e,0x0d, '<', '>', '?',0xff,0xff,0xff,0x00,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x30 - 0x3f */
165 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */
166 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x50 - 0x5F */
170 /******************************************************************
172 ******************************************************************/
173 int isa_kbd_init(void)
176 result=kbd_initialize();
178 PRINTF("AT Keyboard initialized\n");
179 irq_install_handler(25, (interrupt_handler_t *)handle_isa_int, NULL);
180 isa_irq_install_handler(KBD_INTERRUPT, (interrupt_handler_t *)kbd_interrupt, NULL);
183 printf("%s\n",result);
188 #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
189 extern int overwrite_console (void);
191 int overwrite_console (void)
197 int drv_isa_kbd_init (void)
200 struct stdio_dev kbddev ;
201 char *stdinname = getenv ("stdin");
203 if(isa_kbd_init()==-1)
205 memset (&kbddev, 0, sizeof(kbddev));
206 strcpy(kbddev.name, DEVNAME);
207 kbddev.flags = DEV_FLAGS_INPUT;
208 kbddev.getc = kbd_getc ;
209 kbddev.tstc = kbd_testc ;
211 error = stdio_register (&kbddev);
213 /* check if this is the standard input device */
214 if(strcmp(stdinname,DEVNAME)==0) {
215 /* reassign the console */
216 if(overwrite_console()) {
219 error=console_assign(stdin,DEVNAME);
230 /******************************************************************
232 ******************************************************************/
233 /* puts character in the queue and sets up the in and out pointer */
234 void kbd_put_queue(char data)
236 if((in_pointer+1)==KBD_BUFFER_LEN) {
238 return; /* buffer full */
243 if((in_pointer+1)==out_pointer)
244 return; /* buffer full */
247 kbd_buffer[in_pointer]=data;
251 /* test if a character is in the queue */
252 int kbd_testc(struct stdio_dev *dev)
254 if(in_pointer==out_pointer)
255 return(0); /* no data */
259 /* gets the character from the queue */
260 int kbd_getc(struct stdio_dev *dev)
263 while(in_pointer==out_pointer);
264 if((out_pointer+1)==KBD_BUFFER_LEN)
268 c=kbd_buffer[out_pointer];
276 void kbd_set_leds(void)
279 leds&=~LED_CAP; /* switch caps_lock off */
281 leds|=LED_CAP; /* switch on LED */
283 leds&=~LED_NUM; /* switch LED off */
285 leds|=LED_NUM; /* switch on LED */
287 leds&=~LED_SCR; /* switch LED off */
289 leds|=LED_SCR; /* switch on LED */
290 kbd_send_data(KBD_CMD_SET_LEDS);
295 void handle_keyboard_event (unsigned char scancode)
297 unsigned char keycode;
299 /* Convert scancode to keycode */
300 PRINTF ("scancode %x\n", scancode);
301 if (scancode == 0xe0) {
302 e0 = 1; /* special charakters */
306 e0 = 0; /* delete flag */
307 if (!(((scancode & 0x7F) == 0x38) || /* the right ctrl key */
308 ((scancode & 0x7F) == 0x1D) || /* the right alt key */
309 ((scancode & 0x7F) == 0x35) || /* the right '/' key */
310 ((scancode & 0x7F) == 0x1C)))
311 /* the right enter key */
312 /* we swallow unknown e0 codes */
315 /* special cntrl keys */
318 case 0x36: /* shift pressed */
320 return; /* do nothing else */
322 case 0xB6: /* shift released */
324 return; /* do nothing else */
325 case 0x38: /* alt pressed */
327 return; /* do nothing else */
328 case 0xB8: /* alt released */
330 return; /* do nothing else */
331 case 0x1d: /* ctrl pressed */
333 return; /* do nothing else */
334 case 0x9d: /* ctrl released */
336 return; /* do nothing else */
337 case 0x46: /* scrollock pressed */
338 scroll_lock = ~scroll_lock;
340 return; /* do nothing else */
341 case 0x3A: /* capslock pressed */
342 caps_lock = ~caps_lock;
345 case 0x45: /* numlock pressed */
346 num_lock = ~num_lock;
349 case 0xC6: /* scroll lock released */
350 case 0xC5: /* num lock released */
351 case 0xBA: /* caps lock released */
352 return; /* just swallow */
354 if ((scancode & 0x80) == 0x80) /* key released */
356 /* now, decide which table we need */
357 if (scancode > (sizeof (kbd_plain_xlate) / sizeof (kbd_plain_xlate[0]))) { /* scancode not in list */
358 PRINTF ("unkown scancode %X\n", scancode);
359 return; /* swallow it */
361 /* setup plain code first */
362 keycode = kbd_plain_xlate[scancode];
363 if (caps_lock == 1) { /* caps_lock is pressed, overwrite plain code */
364 if (scancode > (sizeof (kbd_shift_xlate) / sizeof (kbd_shift_xlate[0]))) { /* scancode not in list */
365 PRINTF ("unkown caps-locked scancode %X\n", scancode);
366 return; /* swallow it */
368 keycode = kbd_shift_xlate[scancode];
369 if (keycode < 'A') { /* we only want the alphas capital */
370 keycode = kbd_plain_xlate[scancode];
373 if (shift == 1) { /* shift overwrites caps_lock */
374 if (scancode > (sizeof (kbd_shift_xlate) / sizeof (kbd_shift_xlate[0]))) { /* scancode not in list */
375 PRINTF ("unkown shifted scancode %X\n", scancode);
376 return; /* swallow it */
378 keycode = kbd_shift_xlate[scancode];
380 if (ctrl == 1) { /* ctrl overwrites caps_lock and shift */
381 if (scancode > (sizeof (kbd_ctrl_xlate) / sizeof (kbd_ctrl_xlate[0]))) { /* scancode not in list */
382 PRINTF ("unkown ctrl scancode %X\n", scancode);
383 return; /* swallow it */
385 keycode = kbd_ctrl_xlate[scancode];
387 /* check if valid keycode */
388 if (keycode == 0xff) {
389 PRINTF ("unkown scancode %X\n", scancode);
390 return; /* swallow unknown codes */
393 kbd_put_queue (keycode);
394 PRINTF ("%x\n", keycode);
398 * This reads the keyboard status port, and does the
399 * appropriate action.
402 unsigned char handle_kbd_event(void)
404 unsigned char status = kbd_read_status();
405 unsigned int work = 10000;
407 while ((--work > 0) && (status & KBD_STAT_OBF)) {
408 unsigned char scancode;
410 scancode = kbd_read_input();
412 /* Error bytes must be ignored to make the
413 Synaptics touchpads compaq use work */
414 /* Ignore error bytes */
415 if (!(status & (KBD_STAT_GTO | KBD_STAT_PERR)))
417 if (status & KBD_STAT_MOUSE_OBF)
418 ; /* not supported: handle_mouse_event(scancode); */
420 handle_keyboard_event(scancode);
422 status = kbd_read_status();
425 PRINTF("pc_keyb: controller jammed (0x%02X).\n", status);
430 /******************************************************************************
431 * Lowlevel Part of keyboard section
433 unsigned char kbd_read_status(void)
435 return(in8(CONFIG_SYS_ISA_IO_BASE_ADDRESS + KDB_COMMAND_PORT));
438 unsigned char kbd_read_input(void)
440 return(in8(CONFIG_SYS_ISA_IO_BASE_ADDRESS + KDB_DATA_PORT));
443 void kbd_write_command(unsigned char cmd)
445 out8(CONFIG_SYS_ISA_IO_BASE_ADDRESS + KDB_COMMAND_PORT,cmd);
448 void kbd_write_output(unsigned char data)
450 out8(CONFIG_SYS_ISA_IO_BASE_ADDRESS + KDB_DATA_PORT, data);
453 int kbd_read_data(void)
456 unsigned char status;
459 status = kbd_read_status();
460 if (status & KBD_STAT_OBF) {
461 val = kbd_read_input();
462 if (status & (KBD_STAT_GTO | KBD_STAT_PERR))
468 int kbd_wait_for_input(void)
470 unsigned long timeout;
473 timeout = KBD_TIMEOUT;
488 unsigned long timeout = KBC_TIMEOUT * 10;
491 unsigned char status = handle_kbd_event();
492 if (!(status & KBD_STAT_IBF))
500 void kbd_write_command_w(int data)
503 PRINTF("timeout in kbd_write_command_w\n");
504 kbd_write_command(data);
507 void kbd_write_output_w(int data)
510 PRINTF("timeout in kbd_write_output_w\n");
511 kbd_write_output(data);
514 void kbd_send_data(unsigned char data)
516 unsigned char status;
517 disable_8259A_irq(1); /* disable interrupt */
518 kbd_write_output_w(data);
519 status = kbd_wait_for_input();
520 if (status == KBD_REPLY_ACK)
521 enable_8259A_irq(1); /* enable interrupt */
525 char * kbd_initialize(void)
529 in_pointer = 0; /* delete in Buffer */
532 * Test the keyboard interface.
533 * This seems to be the only way to get it going.
534 * If the test is successful a x55 is placed in the input buffer.
536 kbd_write_command_w(KBD_CCMD_SELF_TEST);
537 if (kbd_wait_for_input() != 0x55)
538 return "Kbd: failed self test";
540 * Perform a keyboard interface test. This causes the controller
541 * to test the keyboard clock and data lines. The results of the
542 * test are placed in the input buffer.
544 kbd_write_command_w(KBD_CCMD_KBD_TEST);
545 if (kbd_wait_for_input() != 0x00)
546 return "Kbd: interface failed self test";
548 * Enable the keyboard by allowing the keyboard clock to run.
550 kbd_write_command_w(KBD_CCMD_KBD_ENABLE);
551 status = kbd_wait_for_input();
553 * Reset keyboard. If the read times out
554 * then the assumption is that no keyboard is
555 * plugged into the machine.
556 * This defaults the keyboard to scan-code set 2.
558 * Set up to try again if the keyboard asks for RESEND.
561 kbd_write_output_w(KBD_CMD_RESET);
562 status = kbd_wait_for_input();
563 if (status == KBD_REPLY_ACK)
565 if (status != KBD_REPLY_RESEND) {
566 PRINTF("status: %X\n",status);
567 return "Kbd: reset failed, no ACK";
570 if (kbd_wait_for_input() != KBD_REPLY_POR)
571 return "Kbd: reset failed, no POR";
574 * Set keyboard controller mode. During this, the keyboard should be
575 * in the disabled state.
577 * Set up to try again if the keyboard asks for RESEND.
580 kbd_write_output_w(KBD_CMD_DISABLE);
581 status = kbd_wait_for_input();
582 if (status == KBD_REPLY_ACK)
584 if (status != KBD_REPLY_RESEND)
585 return "Kbd: disable keyboard: no ACK";
588 kbd_write_command_w(KBD_CCMD_WRITE_MODE);
589 kbd_write_output_w(KBD_MODE_KBD_INT
591 | KBD_MODE_DISABLE_MOUSE
594 /* AMCC powerpc portables need this to use scan-code set 1 -- Cort */
595 kbd_write_command_w(KBD_CCMD_READ_MODE);
596 if (!(kbd_wait_for_input() & KBD_MODE_KCC)) {
598 * If the controller does not support conversion,
599 * Set the keyboard to scan-code set 1.
601 kbd_write_output_w(0xF0);
602 kbd_wait_for_input();
603 kbd_write_output_w(0x01);
604 kbd_wait_for_input();
606 kbd_write_output_w(KBD_CMD_ENABLE);
607 if (kbd_wait_for_input() != KBD_REPLY_ACK)
608 return "Kbd: enable keyboard: no ACK";
611 * Finally, set the typematic rate to maximum.
613 kbd_write_output_w(KBD_CMD_SET_RATE);
614 if (kbd_wait_for_input() != KBD_REPLY_ACK)
615 return "Kbd: Set rate: no ACK";
616 kbd_write_output_w(0x00);
617 if (kbd_wait_for_input() != KBD_REPLY_ACK)
618 return "Kbd: Set rate: no ACK";
622 void kbd_interrupt(void)