wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001 |
| 3 | * Denis Peter, MPL AG Switzerland, d.peter@mpl.ch |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 6 | * |
| 7 | * Source partly derived from: |
| 8 | * linux/drivers/char/pc_keyb.c |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 9 | */ |
| 10 | #include <common.h> |
Simon Glass | a73bda4 | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 11 | #include <console.h> |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 12 | #include <asm/processor.h> |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 13 | #include <stdio_dev.h> |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 14 | #include "isa.h" |
| 15 | #include "kbd.h" |
| 16 | |
| 17 | |
| 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); |
| 23 | |
| 24 | /* used only by send_data - set by keyboard_interrupt */ |
| 25 | |
| 26 | |
| 27 | #undef KBG_DEBUG |
| 28 | |
| 29 | #ifdef KBG_DEBUG |
| 30 | #define PRINTF(fmt,args...) printf (fmt ,##args) |
| 31 | #else |
| 32 | #define PRINTF(fmt,args...) |
| 33 | #endif |
| 34 | |
| 35 | #define KBD_STAT_KOBF 0x01 |
| 36 | #define KBD_STAT_IBF 0x02 |
| 37 | #define KBD_STAT_SYS 0x04 |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 38 | #define KBD_STAT_CD 0x08 |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 39 | #define KBD_STAT_LOCK 0x10 |
| 40 | #define KBD_STAT_MOBF 0x20 |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 41 | #define KBD_STAT_TI_OUT 0x40 |
| 42 | #define KBD_STAT_PARERR 0x80 |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 43 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 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 */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 47 | /* |
| 48 | * Keyboard Controller Commands |
| 49 | */ |
| 50 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 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 */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 54 | #define KBD_CCMD_MOUSE_DISABLE 0xA7 /* Disable mouse interface */ |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 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 */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 61 | #define KBD_CCMD_WRITE_AUX_OBUF 0xD3 /* Write to output buffer as if |
| 62 | initiated by the auxiliary device */ |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 63 | #define KBD_CCMD_WRITE_MOUSE 0xD4 /* Write the following byte to the mouse */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 64 | |
| 65 | /* |
| 66 | * Keyboard Commands |
| 67 | */ |
| 68 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 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 */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 74 | |
| 75 | /* |
| 76 | * Keyboard Replies |
| 77 | */ |
| 78 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 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 */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 82 | |
| 83 | /* |
| 84 | * Status Register Bits |
| 85 | */ |
| 86 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 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 */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 95 | |
| 96 | #define AUX_STAT_OBF (KBD_STAT_OBF | KBD_STAT_MOUSE_OBF) |
| 97 | |
| 98 | /* |
| 99 | * Controller Mode Register Bits |
| 100 | */ |
| 101 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 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 */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 107 | #define KBD_MODE_DISABLE_MOUSE 0x20 /* Disable mouse interface */ |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 108 | #define KBD_MODE_KCC 0x40 /* Scan code conversion to PC format */ |
| 109 | #define KBD_MODE_RFU 0x80 |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 110 | |
| 111 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 112 | #define KDB_DATA_PORT 0x60 |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 113 | #define KDB_COMMAND_PORT 0x64 |
| 114 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 115 | #define LED_SCR 0x01 /* scroll lock led */ |
| 116 | #define LED_CAP 0x04 /* caps lock led */ |
| 117 | #define LED_NUM 0x02 /* num lock led */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 118 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 119 | #define KBD_BUFFER_LEN 0x20 /* size of the keyboardbuffer */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 120 | |
| 121 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 122 | static volatile char kbd_buffer[KBD_BUFFER_LEN]; |
| 123 | static volatile int in_pointer = 0; |
| 124 | static volatile int out_pointer = 0; |
| 125 | |
| 126 | |
| 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; |
| 135 | |
| 136 | #define DEVNAME "kbd" |
| 137 | |
| 138 | /* Simple translation table for the keys */ |
| 139 | |
| 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 */ |
| 147 | '\r',0xff,0xff |
| 148 | }; |
| 149 | |
| 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 */ |
| 157 | '\r',0xff,0xff |
| 158 | }; |
| 159 | |
| 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 */ |
| 167 | '\r',0xff,0xff |
| 168 | }; |
| 169 | |
| 170 | /****************************************************************** |
| 171 | * Init |
| 172 | ******************************************************************/ |
| 173 | int isa_kbd_init(void) |
| 174 | { |
| 175 | char* result; |
| 176 | result=kbd_initialize(); |
| 177 | if(result==NULL) { |
| 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); |
| 181 | return (1); |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 182 | } else { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 183 | printf("%s\n",result); |
| 184 | return (-1); |
| 185 | } |
| 186 | } |
| 187 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 188 | #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 189 | extern int overwrite_console (void); |
| 190 | #else |
| 191 | int overwrite_console (void) |
| 192 | { |
| 193 | return (0); |
| 194 | } |
| 195 | #endif |
| 196 | |
| 197 | int drv_isa_kbd_init (void) |
| 198 | { |
| 199 | int error; |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 200 | struct stdio_dev kbddev ; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 201 | char *stdinname = getenv ("stdin"); |
| 202 | |
| 203 | if(isa_kbd_init()==-1) |
| 204 | return -1; |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 205 | memset (&kbddev, 0, sizeof(kbddev)); |
| 206 | strcpy(kbddev.name, DEVNAME); |
Bin Meng | 6abe4b6 | 2015-11-03 23:23:37 -0800 | [diff] [blame] | 207 | kbddev.flags = DEV_FLAGS_INPUT; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 208 | kbddev.getc = kbd_getc ; |
| 209 | kbddev.tstc = kbd_testc ; |
| 210 | |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 211 | error = stdio_register (&kbddev); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 212 | if(error==0) { |
| 213 | /* check if this is the standard input device */ |
| 214 | if(strcmp(stdinname,DEVNAME)==0) { |
| 215 | /* reassign the console */ |
| 216 | if(overwrite_console()) { |
| 217 | return 1; |
| 218 | } |
| 219 | error=console_assign(stdin,DEVNAME); |
| 220 | if(error==0) |
| 221 | return 1; |
| 222 | else |
| 223 | return error; |
| 224 | } |
| 225 | return 1; |
| 226 | } |
| 227 | return error; |
| 228 | } |
| 229 | |
| 230 | /****************************************************************** |
| 231 | * Queue handling |
| 232 | ******************************************************************/ |
| 233 | /* puts character in the queue and sets up the in and out pointer */ |
| 234 | void kbd_put_queue(char data) |
| 235 | { |
| 236 | if((in_pointer+1)==KBD_BUFFER_LEN) { |
| 237 | if(out_pointer==0) { |
| 238 | return; /* buffer full */ |
| 239 | } else{ |
| 240 | in_pointer=0; |
| 241 | } |
| 242 | } else { |
| 243 | if((in_pointer+1)==out_pointer) |
| 244 | return; /* buffer full */ |
| 245 | in_pointer++; |
| 246 | } |
| 247 | kbd_buffer[in_pointer]=data; |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | /* test if a character is in the queue */ |
Simon Glass | 0d1e1f7 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 252 | int kbd_testc(struct stdio_dev *dev) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 253 | { |
| 254 | if(in_pointer==out_pointer) |
| 255 | return(0); /* no data */ |
| 256 | else |
| 257 | return(1); |
| 258 | } |
| 259 | /* gets the character from the queue */ |
Simon Glass | 0d1e1f7 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 260 | int kbd_getc(struct stdio_dev *dev) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 261 | { |
| 262 | char c; |
| 263 | while(in_pointer==out_pointer); |
| 264 | if((out_pointer+1)==KBD_BUFFER_LEN) |
| 265 | out_pointer=0; |
| 266 | else |
| 267 | out_pointer++; |
| 268 | c=kbd_buffer[out_pointer]; |
| 269 | return (int)c; |
| 270 | |
| 271 | } |
| 272 | |
| 273 | |
| 274 | /* set LEDs */ |
| 275 | |
| 276 | void kbd_set_leds(void) |
| 277 | { |
| 278 | if(caps_lock==0) |
| 279 | leds&=~LED_CAP; /* switch caps_lock off */ |
| 280 | else |
| 281 | leds|=LED_CAP; /* switch on LED */ |
| 282 | if(num_lock==0) |
| 283 | leds&=~LED_NUM; /* switch LED off */ |
| 284 | else |
| 285 | leds|=LED_NUM; /* switch on LED */ |
| 286 | if(scroll_lock==0) |
| 287 | leds&=~LED_SCR; /* switch LED off */ |
| 288 | else |
| 289 | leds|=LED_SCR; /* switch on LED */ |
| 290 | kbd_send_data(KBD_CMD_SET_LEDS); |
| 291 | kbd_send_data(leds); |
| 292 | } |
| 293 | |
| 294 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 295 | void handle_keyboard_event (unsigned char scancode) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 296 | { |
| 297 | unsigned char keycode; |
| 298 | |
| 299 | /* Convert scancode to keycode */ |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 300 | PRINTF ("scancode %x\n", scancode); |
| 301 | if (scancode == 0xe0) { |
| 302 | e0 = 1; /* special charakters */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 303 | return; |
| 304 | } |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 305 | if (e0 == 1) { |
| 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 */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 312 | /* we swallow unknown e0 codes */ |
| 313 | return; |
| 314 | } |
| 315 | /* special cntrl keys */ |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 316 | switch (scancode) { |
| 317 | case 0x2A: |
| 318 | case 0x36: /* shift pressed */ |
| 319 | shift = 1; |
| 320 | return; /* do nothing else */ |
| 321 | case 0xAA: |
| 322 | case 0xB6: /* shift released */ |
| 323 | shift = 0; |
| 324 | return; /* do nothing else */ |
| 325 | case 0x38: /* alt pressed */ |
| 326 | alt = 1; |
| 327 | return; /* do nothing else */ |
| 328 | case 0xB8: /* alt released */ |
| 329 | alt = 0; |
| 330 | return; /* do nothing else */ |
| 331 | case 0x1d: /* ctrl pressed */ |
| 332 | ctrl = 1; |
| 333 | return; /* do nothing else */ |
| 334 | case 0x9d: /* ctrl released */ |
| 335 | ctrl = 0; |
| 336 | return; /* do nothing else */ |
| 337 | case 0x46: /* scrollock pressed */ |
| 338 | scroll_lock = ~scroll_lock; |
| 339 | kbd_set_leds (); |
| 340 | return; /* do nothing else */ |
| 341 | case 0x3A: /* capslock pressed */ |
| 342 | caps_lock = ~caps_lock; |
| 343 | kbd_set_leds (); |
| 344 | return; |
| 345 | case 0x45: /* numlock pressed */ |
| 346 | num_lock = ~num_lock; |
| 347 | kbd_set_leds (); |
| 348 | return; |
| 349 | case 0xC6: /* scroll lock released */ |
| 350 | case 0xC5: /* num lock released */ |
| 351 | case 0xBA: /* caps lock released */ |
| 352 | return; /* just swallow */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 353 | } |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 354 | if ((scancode & 0x80) == 0x80) /* key released */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 355 | return; |
| 356 | /* now, decide which table we need */ |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 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 */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 360 | } |
| 361 | /* setup plain code first */ |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 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 */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 367 | } |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 368 | keycode = kbd_shift_xlate[scancode]; |
| 369 | if (keycode < 'A') { /* we only want the alphas capital */ |
| 370 | keycode = kbd_plain_xlate[scancode]; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 371 | } |
| 372 | } |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 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 */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 377 | } |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 378 | keycode = kbd_shift_xlate[scancode]; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 379 | } |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 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 */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 384 | } |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 385 | keycode = kbd_ctrl_xlate[scancode]; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 386 | } |
| 387 | /* check if valid keycode */ |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 388 | if (keycode == 0xff) { |
| 389 | PRINTF ("unkown scancode %X\n", scancode); |
| 390 | return; /* swallow unknown codes */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 393 | kbd_put_queue (keycode); |
| 394 | PRINTF ("%x\n", keycode); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /* |
| 398 | * This reads the keyboard status port, and does the |
| 399 | * appropriate action. |
| 400 | * |
| 401 | */ |
| 402 | unsigned char handle_kbd_event(void) |
| 403 | { |
| 404 | unsigned char status = kbd_read_status(); |
| 405 | unsigned int work = 10000; |
| 406 | |
| 407 | while ((--work > 0) && (status & KBD_STAT_OBF)) { |
| 408 | unsigned char scancode; |
| 409 | |
| 410 | scancode = kbd_read_input(); |
| 411 | |
| 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))) |
| 416 | { |
| 417 | if (status & KBD_STAT_MOUSE_OBF) |
| 418 | ; /* not supported: handle_mouse_event(scancode); */ |
| 419 | else |
| 420 | handle_keyboard_event(scancode); |
| 421 | } |
| 422 | status = kbd_read_status(); |
| 423 | } |
| 424 | if (!work) |
| 425 | PRINTF("pc_keyb: controller jammed (0x%02X).\n", status); |
| 426 | return status; |
| 427 | } |
| 428 | |
| 429 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 430 | /****************************************************************************** |
| 431 | * Lowlevel Part of keyboard section |
| 432 | */ |
| 433 | unsigned char kbd_read_status(void) |
| 434 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 435 | return(in8(CONFIG_SYS_ISA_IO_BASE_ADDRESS + KDB_COMMAND_PORT)); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | unsigned char kbd_read_input(void) |
| 439 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 440 | return(in8(CONFIG_SYS_ISA_IO_BASE_ADDRESS + KDB_DATA_PORT)); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | void kbd_write_command(unsigned char cmd) |
| 444 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 445 | out8(CONFIG_SYS_ISA_IO_BASE_ADDRESS + KDB_COMMAND_PORT,cmd); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | void kbd_write_output(unsigned char data) |
| 449 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 450 | out8(CONFIG_SYS_ISA_IO_BASE_ADDRESS + KDB_DATA_PORT, data); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | int kbd_read_data(void) |
| 454 | { |
| 455 | int val; |
| 456 | unsigned char status; |
| 457 | |
Wolfgang Denk | dc770c7 | 2008-07-14 15:19:07 +0200 | [diff] [blame] | 458 | val = -1; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 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)) |
| 463 | val = -2; |
| 464 | } |
| 465 | return val; |
| 466 | } |
| 467 | |
| 468 | int kbd_wait_for_input(void) |
| 469 | { |
| 470 | unsigned long timeout; |
| 471 | int val; |
| 472 | |
| 473 | timeout = KBD_TIMEOUT; |
| 474 | val=kbd_read_data(); |
| 475 | while(val < 0) |
| 476 | { |
| 477 | if(timeout--==0) |
| 478 | return -1; |
| 479 | udelay(1000); |
| 480 | val=kbd_read_data(); |
| 481 | } |
| 482 | return val; |
| 483 | } |
| 484 | |
| 485 | |
| 486 | int kb_wait(void) |
| 487 | { |
| 488 | unsigned long timeout = KBC_TIMEOUT * 10; |
| 489 | |
| 490 | do { |
| 491 | unsigned char status = handle_kbd_event(); |
| 492 | if (!(status & KBD_STAT_IBF)) |
| 493 | return 0; /* ok */ |
| 494 | udelay(1000); |
| 495 | timeout--; |
| 496 | } while (timeout); |
| 497 | return 1; |
| 498 | } |
| 499 | |
| 500 | void kbd_write_command_w(int data) |
| 501 | { |
| 502 | if(kb_wait()) |
| 503 | PRINTF("timeout in kbd_write_command_w\n"); |
| 504 | kbd_write_command(data); |
| 505 | } |
| 506 | |
| 507 | void kbd_write_output_w(int data) |
| 508 | { |
| 509 | if(kb_wait()) |
| 510 | PRINTF("timeout in kbd_write_output_w\n"); |
| 511 | kbd_write_output(data); |
| 512 | } |
| 513 | |
| 514 | void kbd_send_data(unsigned char data) |
| 515 | { |
| 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 */ |
| 522 | } |
| 523 | |
| 524 | |
| 525 | char * kbd_initialize(void) |
| 526 | { |
| 527 | int status; |
| 528 | |
| 529 | in_pointer = 0; /* delete in Buffer */ |
| 530 | out_pointer = 0; |
| 531 | /* |
| 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. |
| 535 | */ |
| 536 | kbd_write_command_w(KBD_CCMD_SELF_TEST); |
| 537 | if (kbd_wait_for_input() != 0x55) |
| 538 | return "Kbd: failed self test"; |
| 539 | /* |
| 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. |
| 543 | */ |
| 544 | kbd_write_command_w(KBD_CCMD_KBD_TEST); |
| 545 | if (kbd_wait_for_input() != 0x00) |
| 546 | return "Kbd: interface failed self test"; |
| 547 | /* |
| 548 | * Enable the keyboard by allowing the keyboard clock to run. |
| 549 | */ |
| 550 | kbd_write_command_w(KBD_CCMD_KBD_ENABLE); |
| 551 | status = kbd_wait_for_input(); |
| 552 | /* |
| 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. |
| 557 | * |
| 558 | * Set up to try again if the keyboard asks for RESEND. |
| 559 | */ |
| 560 | do { |
| 561 | kbd_write_output_w(KBD_CMD_RESET); |
| 562 | status = kbd_wait_for_input(); |
| 563 | if (status == KBD_REPLY_ACK) |
| 564 | break; |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 565 | if (status != KBD_REPLY_RESEND) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 566 | PRINTF("status: %X\n",status); |
| 567 | return "Kbd: reset failed, no ACK"; |
| 568 | } |
| 569 | } while (1); |
| 570 | if (kbd_wait_for_input() != KBD_REPLY_POR) |
| 571 | return "Kbd: reset failed, no POR"; |
| 572 | |
| 573 | /* |
| 574 | * Set keyboard controller mode. During this, the keyboard should be |
| 575 | * in the disabled state. |
| 576 | * |
| 577 | * Set up to try again if the keyboard asks for RESEND. |
| 578 | */ |
| 579 | do { |
| 580 | kbd_write_output_w(KBD_CMD_DISABLE); |
| 581 | status = kbd_wait_for_input(); |
| 582 | if (status == KBD_REPLY_ACK) |
| 583 | break; |
| 584 | if (status != KBD_REPLY_RESEND) |
| 585 | return "Kbd: disable keyboard: no ACK"; |
| 586 | } while (1); |
| 587 | |
| 588 | kbd_write_command_w(KBD_CCMD_WRITE_MODE); |
| 589 | kbd_write_output_w(KBD_MODE_KBD_INT |
| 590 | | KBD_MODE_SYS |
| 591 | | KBD_MODE_DISABLE_MOUSE |
| 592 | | KBD_MODE_KCC); |
| 593 | |
Wolfgang Denk | 0ee7077 | 2005-09-23 11:05:55 +0200 | [diff] [blame] | 594 | /* AMCC powerpc portables need this to use scan-code set 1 -- Cort */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 595 | kbd_write_command_w(KBD_CCMD_READ_MODE); |
| 596 | if (!(kbd_wait_for_input() & KBD_MODE_KCC)) { |
| 597 | /* |
| 598 | * If the controller does not support conversion, |
| 599 | * Set the keyboard to scan-code set 1. |
| 600 | */ |
| 601 | kbd_write_output_w(0xF0); |
| 602 | kbd_wait_for_input(); |
| 603 | kbd_write_output_w(0x01); |
| 604 | kbd_wait_for_input(); |
| 605 | } |
| 606 | kbd_write_output_w(KBD_CMD_ENABLE); |
| 607 | if (kbd_wait_for_input() != KBD_REPLY_ACK) |
| 608 | return "Kbd: enable keyboard: no ACK"; |
| 609 | |
| 610 | /* |
| 611 | * Finally, set the typematic rate to maximum. |
| 612 | */ |
| 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"; |
| 619 | return NULL; |
| 620 | } |
| 621 | |
| 622 | void kbd_interrupt(void) |
| 623 | { |
| 624 | handle_kbd_event(); |
| 625 | } |