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 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 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. |
| 12 | * |
| 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. |
| 17 | * |
| 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, |
| 21 | * MA 02111-1307 USA |
| 22 | * |
| 23 | * |
| 24 | * Source partly derived from: |
| 25 | * linux/drivers/char/pc_keyb.c |
| 26 | * |
| 27 | * |
| 28 | */ |
| 29 | #include <common.h> |
| 30 | #include <asm/processor.h> |
| 31 | #include <devices.h> |
| 32 | #include "isa.h" |
| 33 | #include "kbd.h" |
| 34 | |
| 35 | |
| 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); |
| 41 | |
| 42 | /* used only by send_data - set by keyboard_interrupt */ |
| 43 | |
| 44 | |
| 45 | #undef KBG_DEBUG |
| 46 | |
| 47 | #ifdef KBG_DEBUG |
| 48 | #define PRINTF(fmt,args...) printf (fmt ,##args) |
| 49 | #else |
| 50 | #define PRINTF(fmt,args...) |
| 51 | #endif |
| 52 | |
| 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 |
| 61 | |
| 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 */ |
| 65 | /* |
| 66 | * Keyboard Controller Commands |
| 67 | */ |
| 68 | |
| 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 */ |
| 82 | |
| 83 | /* |
| 84 | * Keyboard Commands |
| 85 | */ |
| 86 | |
| 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 */ |
| 92 | |
| 93 | /* |
| 94 | * Keyboard Replies |
| 95 | */ |
| 96 | |
| 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 */ |
| 100 | |
| 101 | /* |
| 102 | * Status Register Bits |
| 103 | */ |
| 104 | |
| 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 */ |
| 113 | |
| 114 | #define AUX_STAT_OBF (KBD_STAT_OBF | KBD_STAT_MOUSE_OBF) |
| 115 | |
| 116 | /* |
| 117 | * Controller Mode Register Bits |
| 118 | */ |
| 119 | |
| 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 |
| 128 | |
| 129 | |
| 130 | #define KDB_DATA_PORT 0x60 |
| 131 | #define KDB_COMMAND_PORT 0x64 |
| 132 | |
| 133 | #define LED_SCR 0x01 /* scroll lock led */ |
| 134 | #define LED_CAP 0x04 /* caps lock led */ |
| 135 | #define LED_NUM 0x02 /* num lock led */ |
| 136 | |
| 137 | #define KBD_BUFFER_LEN 0x20 /* size of the keyboardbuffer */ |
| 138 | |
| 139 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 140 | static volatile char kbd_buffer[KBD_BUFFER_LEN]; |
| 141 | static volatile int in_pointer = 0; |
| 142 | static volatile int out_pointer = 0; |
| 143 | |
| 144 | |
| 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; |
| 153 | |
| 154 | #define DEVNAME "kbd" |
| 155 | |
| 156 | /* Simple translation table for the keys */ |
| 157 | |
| 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 */ |
| 165 | '\r',0xff,0xff |
| 166 | }; |
| 167 | |
| 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 */ |
| 175 | '\r',0xff,0xff |
| 176 | }; |
| 177 | |
| 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 */ |
| 185 | '\r',0xff,0xff |
| 186 | }; |
| 187 | |
| 188 | /****************************************************************** |
| 189 | * Init |
| 190 | ******************************************************************/ |
| 191 | int isa_kbd_init(void) |
| 192 | { |
| 193 | char* result; |
| 194 | result=kbd_initialize(); |
| 195 | if(result==NULL) { |
| 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); |
| 199 | return (1); |
| 200 | } |
| 201 | else { |
| 202 | printf("%s\n",result); |
| 203 | return (-1); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | #ifdef CFG_CONSOLE_OVERWRITE_ROUTINE |
| 208 | extern int overwrite_console (void); |
| 209 | #else |
| 210 | int overwrite_console (void) |
| 211 | { |
| 212 | return (0); |
| 213 | } |
| 214 | #endif |
| 215 | |
| 216 | int drv_isa_kbd_init (void) |
| 217 | { |
| 218 | int error; |
| 219 | device_t kbddev ; |
| 220 | char *stdinname = getenv ("stdin"); |
| 221 | |
| 222 | if(isa_kbd_init()==-1) |
| 223 | return -1; |
| 224 | memset (&kbddev, 0, sizeof(kbddev)); |
| 225 | strcpy(kbddev.name, DEVNAME); |
| 226 | kbddev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM; |
| 227 | kbddev.putc = NULL ; |
| 228 | kbddev.puts = NULL ; |
| 229 | kbddev.getc = kbd_getc ; |
| 230 | kbddev.tstc = kbd_testc ; |
| 231 | |
| 232 | error = device_register (&kbddev); |
| 233 | if(error==0) { |
| 234 | /* check if this is the standard input device */ |
| 235 | if(strcmp(stdinname,DEVNAME)==0) { |
| 236 | /* reassign the console */ |
| 237 | if(overwrite_console()) { |
| 238 | return 1; |
| 239 | } |
| 240 | error=console_assign(stdin,DEVNAME); |
| 241 | if(error==0) |
| 242 | return 1; |
| 243 | else |
| 244 | return error; |
| 245 | } |
| 246 | return 1; |
| 247 | } |
| 248 | return error; |
| 249 | } |
| 250 | |
| 251 | /****************************************************************** |
| 252 | * Queue handling |
| 253 | ******************************************************************/ |
| 254 | /* puts character in the queue and sets up the in and out pointer */ |
| 255 | void kbd_put_queue(char data) |
| 256 | { |
| 257 | if((in_pointer+1)==KBD_BUFFER_LEN) { |
| 258 | if(out_pointer==0) { |
| 259 | return; /* buffer full */ |
| 260 | } else{ |
| 261 | in_pointer=0; |
| 262 | } |
| 263 | } else { |
| 264 | if((in_pointer+1)==out_pointer) |
| 265 | return; /* buffer full */ |
| 266 | in_pointer++; |
| 267 | } |
| 268 | kbd_buffer[in_pointer]=data; |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | /* test if a character is in the queue */ |
| 273 | int kbd_testc(void) |
| 274 | { |
| 275 | if(in_pointer==out_pointer) |
| 276 | return(0); /* no data */ |
| 277 | else |
| 278 | return(1); |
| 279 | } |
| 280 | /* gets the character from the queue */ |
| 281 | int kbd_getc(void) |
| 282 | { |
| 283 | char c; |
| 284 | while(in_pointer==out_pointer); |
| 285 | if((out_pointer+1)==KBD_BUFFER_LEN) |
| 286 | out_pointer=0; |
| 287 | else |
| 288 | out_pointer++; |
| 289 | c=kbd_buffer[out_pointer]; |
| 290 | return (int)c; |
| 291 | |
| 292 | } |
| 293 | |
| 294 | |
| 295 | /* set LEDs */ |
| 296 | |
| 297 | void kbd_set_leds(void) |
| 298 | { |
| 299 | if(caps_lock==0) |
| 300 | leds&=~LED_CAP; /* switch caps_lock off */ |
| 301 | else |
| 302 | leds|=LED_CAP; /* switch on LED */ |
| 303 | if(num_lock==0) |
| 304 | leds&=~LED_NUM; /* switch LED off */ |
| 305 | else |
| 306 | leds|=LED_NUM; /* switch on LED */ |
| 307 | if(scroll_lock==0) |
| 308 | leds&=~LED_SCR; /* switch LED off */ |
| 309 | else |
| 310 | leds|=LED_SCR; /* switch on LED */ |
| 311 | kbd_send_data(KBD_CMD_SET_LEDS); |
| 312 | kbd_send_data(leds); |
| 313 | } |
| 314 | |
| 315 | |
| 316 | void handle_keyboard_event(unsigned char scancode) |
| 317 | { |
| 318 | unsigned char keycode; |
| 319 | |
| 320 | /* Convert scancode to keycode */ |
| 321 | PRINTF("scancode %x\n",scancode); |
| 322 | if(scancode==0xe0) { |
| 323 | e0=1; /* special charakters */ |
| 324 | return; |
| 325 | } |
| 326 | if(e0==1) { |
| 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 */ |
| 333 | return; |
| 334 | } |
| 335 | /* special cntrl keys */ |
| 336 | switch(scancode) |
| 337 | { |
| 338 | case 0x2A: |
| 339 | case 0x36: /* shift pressed */ |
| 340 | shift=1; |
| 341 | return; /* do nothing else */ |
| 342 | case 0xAA: |
| 343 | case 0xB6: /* shift released */ |
| 344 | shift=0; |
| 345 | return; /* do nothing else */ |
| 346 | case 0x38: /* alt pressed */ |
| 347 | alt=1; |
| 348 | return; /* do nothing else */ |
| 349 | case 0xB8: /* alt released */ |
| 350 | alt=0; |
| 351 | return; /* do nothing else */ |
| 352 | case 0x1d: /* ctrl pressed */ |
| 353 | ctrl=1; |
| 354 | return; /* do nothing else */ |
| 355 | case 0x9d: /* ctrl released */ |
| 356 | ctrl=0; |
| 357 | return; /* do nothing else */ |
| 358 | case 0x46: /* scrollock pressed */ |
| 359 | scroll_lock=~scroll_lock; |
| 360 | kbd_set_leds(); |
| 361 | return; /* do nothing else */ |
| 362 | case 0x3A: /* capslock pressed */ |
| 363 | caps_lock=~caps_lock; |
| 364 | kbd_set_leds(); |
| 365 | return; |
| 366 | case 0x45: /* numlock pressed */ |
| 367 | num_lock=~num_lock; |
| 368 | kbd_set_leds(); |
| 369 | return; |
| 370 | case 0xC6: /* scroll lock released */ |
| 371 | case 0xC5: /* num lock released */ |
| 372 | case 0xBA: /* caps lock released */ |
| 373 | return; /* just swallow */ |
| 374 | } |
| 375 | if((scancode&0x80)==0x80) /* key released */ |
| 376 | return; |
| 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 */ |
| 381 | } |
| 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 */ |
| 388 | } |
| 389 | keycode=kbd_shift_xlate[scancode]; |
| 390 | if(keycode<'A') { /* we only want the alphas capital */ |
| 391 | keycode=kbd_plain_xlate[scancode]; |
| 392 | } |
| 393 | } |
| 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 */ |
| 398 | } |
| 399 | keycode=kbd_shift_xlate[scancode]; |
| 400 | } |
| 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 */ |
| 405 | } |
| 406 | keycode=kbd_ctrl_xlate[scancode]; |
| 407 | } |
| 408 | /* check if valid keycode */ |
| 409 | if(keycode==0xff) { |
| 410 | PRINTF("unkown scancode %X\n",scancode); |
| 411 | return; /* swallow unknown codes */ |
| 412 | } |
| 413 | |
| 414 | kbd_put_queue(keycode); |
| 415 | PRINTF("%x\n",keycode); |
| 416 | } |
| 417 | |
| 418 | /* |
| 419 | * This reads the keyboard status port, and does the |
| 420 | * appropriate action. |
| 421 | * |
| 422 | */ |
| 423 | unsigned char handle_kbd_event(void) |
| 424 | { |
| 425 | unsigned char status = kbd_read_status(); |
| 426 | unsigned int work = 10000; |
| 427 | |
| 428 | while ((--work > 0) && (status & KBD_STAT_OBF)) { |
| 429 | unsigned char scancode; |
| 430 | |
| 431 | scancode = kbd_read_input(); |
| 432 | |
| 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))) |
| 437 | { |
| 438 | if (status & KBD_STAT_MOUSE_OBF) |
| 439 | ; /* not supported: handle_mouse_event(scancode); */ |
| 440 | else |
| 441 | handle_keyboard_event(scancode); |
| 442 | } |
| 443 | status = kbd_read_status(); |
| 444 | } |
| 445 | if (!work) |
| 446 | PRINTF("pc_keyb: controller jammed (0x%02X).\n", status); |
| 447 | return status; |
| 448 | } |
| 449 | |
| 450 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 451 | /****************************************************************************** |
| 452 | * Lowlevel Part of keyboard section |
| 453 | */ |
| 454 | unsigned char kbd_read_status(void) |
| 455 | { |
| 456 | return(in8(CFG_ISA_IO_BASE_ADDRESS + KDB_COMMAND_PORT)); |
| 457 | } |
| 458 | |
| 459 | unsigned char kbd_read_input(void) |
| 460 | { |
| 461 | return(in8(CFG_ISA_IO_BASE_ADDRESS + KDB_DATA_PORT)); |
| 462 | } |
| 463 | |
| 464 | void kbd_write_command(unsigned char cmd) |
| 465 | { |
| 466 | out8(CFG_ISA_IO_BASE_ADDRESS + KDB_COMMAND_PORT,cmd); |
| 467 | } |
| 468 | |
| 469 | void kbd_write_output(unsigned char data) |
| 470 | { |
| 471 | out8(CFG_ISA_IO_BASE_ADDRESS + KDB_DATA_PORT, data); |
| 472 | } |
| 473 | |
| 474 | int kbd_read_data(void) |
| 475 | { |
| 476 | int val; |
| 477 | unsigned char status; |
| 478 | |
| 479 | val=-1; |
| 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)) |
| 484 | val = -2; |
| 485 | } |
| 486 | return val; |
| 487 | } |
| 488 | |
| 489 | int kbd_wait_for_input(void) |
| 490 | { |
| 491 | unsigned long timeout; |
| 492 | int val; |
| 493 | |
| 494 | timeout = KBD_TIMEOUT; |
| 495 | val=kbd_read_data(); |
| 496 | while(val < 0) |
| 497 | { |
| 498 | if(timeout--==0) |
| 499 | return -1; |
| 500 | udelay(1000); |
| 501 | val=kbd_read_data(); |
| 502 | } |
| 503 | return val; |
| 504 | } |
| 505 | |
| 506 | |
| 507 | int kb_wait(void) |
| 508 | { |
| 509 | unsigned long timeout = KBC_TIMEOUT * 10; |
| 510 | |
| 511 | do { |
| 512 | unsigned char status = handle_kbd_event(); |
| 513 | if (!(status & KBD_STAT_IBF)) |
| 514 | return 0; /* ok */ |
| 515 | udelay(1000); |
| 516 | timeout--; |
| 517 | } while (timeout); |
| 518 | return 1; |
| 519 | } |
| 520 | |
| 521 | void kbd_write_command_w(int data) |
| 522 | { |
| 523 | if(kb_wait()) |
| 524 | PRINTF("timeout in kbd_write_command_w\n"); |
| 525 | kbd_write_command(data); |
| 526 | } |
| 527 | |
| 528 | void kbd_write_output_w(int data) |
| 529 | { |
| 530 | if(kb_wait()) |
| 531 | PRINTF("timeout in kbd_write_output_w\n"); |
| 532 | kbd_write_output(data); |
| 533 | } |
| 534 | |
| 535 | void kbd_send_data(unsigned char data) |
| 536 | { |
| 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 */ |
| 543 | } |
| 544 | |
| 545 | |
| 546 | char * kbd_initialize(void) |
| 547 | { |
| 548 | int status; |
| 549 | |
| 550 | in_pointer = 0; /* delete in Buffer */ |
| 551 | out_pointer = 0; |
| 552 | /* |
| 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. |
| 556 | */ |
| 557 | kbd_write_command_w(KBD_CCMD_SELF_TEST); |
| 558 | if (kbd_wait_for_input() != 0x55) |
| 559 | return "Kbd: failed self test"; |
| 560 | /* |
| 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. |
| 564 | */ |
| 565 | kbd_write_command_w(KBD_CCMD_KBD_TEST); |
| 566 | if (kbd_wait_for_input() != 0x00) |
| 567 | return "Kbd: interface failed self test"; |
| 568 | /* |
| 569 | * Enable the keyboard by allowing the keyboard clock to run. |
| 570 | */ |
| 571 | kbd_write_command_w(KBD_CCMD_KBD_ENABLE); |
| 572 | status = kbd_wait_for_input(); |
| 573 | /* |
| 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. |
| 578 | * |
| 579 | * Set up to try again if the keyboard asks for RESEND. |
| 580 | */ |
| 581 | do { |
| 582 | kbd_write_output_w(KBD_CMD_RESET); |
| 583 | status = kbd_wait_for_input(); |
| 584 | if (status == KBD_REPLY_ACK) |
| 585 | break; |
| 586 | if (status != KBD_REPLY_RESEND) |
| 587 | { |
| 588 | PRINTF("status: %X\n",status); |
| 589 | return "Kbd: reset failed, no ACK"; |
| 590 | } |
| 591 | } while (1); |
| 592 | if (kbd_wait_for_input() != KBD_REPLY_POR) |
| 593 | return "Kbd: reset failed, no POR"; |
| 594 | |
| 595 | /* |
| 596 | * Set keyboard controller mode. During this, the keyboard should be |
| 597 | * in the disabled state. |
| 598 | * |
| 599 | * Set up to try again if the keyboard asks for RESEND. |
| 600 | */ |
| 601 | do { |
| 602 | kbd_write_output_w(KBD_CMD_DISABLE); |
| 603 | status = kbd_wait_for_input(); |
| 604 | if (status == KBD_REPLY_ACK) |
| 605 | break; |
| 606 | if (status != KBD_REPLY_RESEND) |
| 607 | return "Kbd: disable keyboard: no ACK"; |
| 608 | } while (1); |
| 609 | |
| 610 | kbd_write_command_w(KBD_CCMD_WRITE_MODE); |
| 611 | kbd_write_output_w(KBD_MODE_KBD_INT |
| 612 | | KBD_MODE_SYS |
| 613 | | KBD_MODE_DISABLE_MOUSE |
| 614 | | KBD_MODE_KCC); |
| 615 | |
Wolfgang Denk | 0c8721a | 2005-09-23 11:05:55 +0200 | [diff] [blame] | 616 | /* AMCC powerpc portables need this to use scan-code set 1 -- Cort */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 617 | kbd_write_command_w(KBD_CCMD_READ_MODE); |
| 618 | if (!(kbd_wait_for_input() & KBD_MODE_KCC)) { |
| 619 | /* |
| 620 | * If the controller does not support conversion, |
| 621 | * Set the keyboard to scan-code set 1. |
| 622 | */ |
| 623 | kbd_write_output_w(0xF0); |
| 624 | kbd_wait_for_input(); |
| 625 | kbd_write_output_w(0x01); |
| 626 | kbd_wait_for_input(); |
| 627 | } |
| 628 | kbd_write_output_w(KBD_CMD_ENABLE); |
| 629 | if (kbd_wait_for_input() != KBD_REPLY_ACK) |
| 630 | return "Kbd: enable keyboard: no ACK"; |
| 631 | |
| 632 | /* |
| 633 | * Finally, set the typematic rate to maximum. |
| 634 | */ |
| 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"; |
| 641 | return NULL; |
| 642 | } |
| 643 | |
| 644 | void kbd_interrupt(void) |
| 645 | { |
| 646 | handle_kbd_event(); |
| 647 | } |