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> |
| 11 | #include <asm/processor.h> |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 12 | #include <stdio_dev.h> |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 13 | #include "isa.h" |
| 14 | #include "kbd.h" |
| 15 | |
| 16 | |
| 17 | unsigned char kbd_read_status(void); |
| 18 | unsigned char kbd_read_input(void); |
| 19 | void kbd_send_data(unsigned char data); |
| 20 | void disable_8259A_irq(unsigned int irq); |
| 21 | void enable_8259A_irq(unsigned int irq); |
| 22 | |
| 23 | /* used only by send_data - set by keyboard_interrupt */ |
| 24 | |
| 25 | |
| 26 | #undef KBG_DEBUG |
| 27 | |
| 28 | #ifdef KBG_DEBUG |
| 29 | #define PRINTF(fmt,args...) printf (fmt ,##args) |
| 30 | #else |
| 31 | #define PRINTF(fmt,args...) |
| 32 | #endif |
| 33 | |
| 34 | #define KBD_STAT_KOBF 0x01 |
| 35 | #define KBD_STAT_IBF 0x02 |
| 36 | #define KBD_STAT_SYS 0x04 |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 37 | #define KBD_STAT_CD 0x08 |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 38 | #define KBD_STAT_LOCK 0x10 |
| 39 | #define KBD_STAT_MOBF 0x20 |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 40 | #define KBD_STAT_TI_OUT 0x40 |
| 41 | #define KBD_STAT_PARERR 0x80 |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 42 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 43 | #define KBD_INIT_TIMEOUT 1000 /* Timeout in ms for initializing the keyboard */ |
| 44 | #define KBC_TIMEOUT 250 /* Timeout in ms for sending to keyboard controller */ |
| 45 | #define KBD_TIMEOUT 2000 /* Timeout in ms for keyboard command acknowledge */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 46 | /* |
| 47 | * Keyboard Controller Commands |
| 48 | */ |
| 49 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 50 | #define KBD_CCMD_READ_MODE 0x20 /* Read mode bits */ |
| 51 | #define KBD_CCMD_WRITE_MODE 0x60 /* Write mode bits */ |
| 52 | #define KBD_CCMD_GET_VERSION 0xA1 /* Get controller version */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 53 | #define KBD_CCMD_MOUSE_DISABLE 0xA7 /* Disable mouse interface */ |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 54 | #define KBD_CCMD_MOUSE_ENABLE 0xA8 /* Enable mouse interface */ |
| 55 | #define KBD_CCMD_TEST_MOUSE 0xA9 /* Mouse interface test */ |
| 56 | #define KBD_CCMD_SELF_TEST 0xAA /* Controller self test */ |
| 57 | #define KBD_CCMD_KBD_TEST 0xAB /* Keyboard interface test */ |
| 58 | #define KBD_CCMD_KBD_DISABLE 0xAD /* Keyboard interface disable */ |
| 59 | #define KBD_CCMD_KBD_ENABLE 0xAE /* Keyboard interface enable */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 60 | #define KBD_CCMD_WRITE_AUX_OBUF 0xD3 /* Write to output buffer as if |
| 61 | initiated by the auxiliary device */ |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 62 | #define KBD_CCMD_WRITE_MOUSE 0xD4 /* Write the following byte to the mouse */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 63 | |
| 64 | /* |
| 65 | * Keyboard Commands |
| 66 | */ |
| 67 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 68 | #define KBD_CMD_SET_LEDS 0xED /* Set keyboard leds */ |
| 69 | #define KBD_CMD_SET_RATE 0xF3 /* Set typematic rate */ |
| 70 | #define KBD_CMD_ENABLE 0xF4 /* Enable scanning */ |
| 71 | #define KBD_CMD_DISABLE 0xF5 /* Disable scanning */ |
| 72 | #define KBD_CMD_RESET 0xFF /* Reset */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 73 | |
| 74 | /* |
| 75 | * Keyboard Replies |
| 76 | */ |
| 77 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 78 | #define KBD_REPLY_POR 0xAA /* Power on reset */ |
| 79 | #define KBD_REPLY_ACK 0xFA /* Command ACK */ |
| 80 | #define KBD_REPLY_RESEND 0xFE /* Command NACK, send the cmd again */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 81 | |
| 82 | /* |
| 83 | * Status Register Bits |
| 84 | */ |
| 85 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 86 | #define KBD_STAT_OBF 0x01 /* Keyboard output buffer full */ |
| 87 | #define KBD_STAT_IBF 0x02 /* Keyboard input buffer full */ |
| 88 | #define KBD_STAT_SELFTEST 0x04 /* Self test successful */ |
| 89 | #define KBD_STAT_CMD 0x08 /* Last write was a command write (0=data) */ |
| 90 | #define KBD_STAT_UNLOCKED 0x10 /* Zero if keyboard locked */ |
| 91 | #define KBD_STAT_MOUSE_OBF 0x20 /* Mouse output buffer full */ |
| 92 | #define KBD_STAT_GTO 0x40 /* General receive/xmit timeout */ |
| 93 | #define KBD_STAT_PERR 0x80 /* Parity error */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 94 | |
| 95 | #define AUX_STAT_OBF (KBD_STAT_OBF | KBD_STAT_MOUSE_OBF) |
| 96 | |
| 97 | /* |
| 98 | * Controller Mode Register Bits |
| 99 | */ |
| 100 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 101 | #define KBD_MODE_KBD_INT 0x01 /* Keyboard data generate IRQ1 */ |
| 102 | #define KBD_MODE_MOUSE_INT 0x02 /* Mouse data generate IRQ12 */ |
| 103 | #define KBD_MODE_SYS 0x04 /* The system flag (?) */ |
| 104 | #define KBD_MODE_NO_KEYLOCK 0x08 /* The keylock doesn't affect the keyboard if set */ |
| 105 | #define KBD_MODE_DISABLE_KBD 0x10 /* Disable keyboard interface */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 106 | #define KBD_MODE_DISABLE_MOUSE 0x20 /* Disable mouse interface */ |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 107 | #define KBD_MODE_KCC 0x40 /* Scan code conversion to PC format */ |
| 108 | #define KBD_MODE_RFU 0x80 |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 109 | |
| 110 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 111 | #define KDB_DATA_PORT 0x60 |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 112 | #define KDB_COMMAND_PORT 0x64 |
| 113 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 114 | #define LED_SCR 0x01 /* scroll lock led */ |
| 115 | #define LED_CAP 0x04 /* caps lock led */ |
| 116 | #define LED_NUM 0x02 /* num lock led */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 117 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 118 | #define KBD_BUFFER_LEN 0x20 /* size of the keyboardbuffer */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 119 | |
| 120 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 121 | static volatile char kbd_buffer[KBD_BUFFER_LEN]; |
| 122 | static volatile int in_pointer = 0; |
| 123 | static volatile int out_pointer = 0; |
| 124 | |
| 125 | |
| 126 | static unsigned char num_lock = 0; |
| 127 | static unsigned char caps_lock = 0; |
| 128 | static unsigned char scroll_lock = 0; |
| 129 | static unsigned char shift = 0; |
| 130 | static unsigned char ctrl = 0; |
| 131 | static unsigned char alt = 0; |
| 132 | static unsigned char e0 = 0; |
| 133 | static unsigned char leds = 0; |
| 134 | |
| 135 | #define DEVNAME "kbd" |
| 136 | |
| 137 | /* Simple translation table for the keys */ |
| 138 | |
| 139 | static unsigned char kbd_plain_xlate[] = { |
| 140 | 0xff,0x1b, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=','\b','\t', /* 0x00 - 0x0f */ |
| 141 | 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']','\r',0xff, 'a', 's', /* 0x10 - 0x1f */ |
| 142 | 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';','\'', '`',0xff,'\\', 'z', 'x', 'c', 'v', /* 0x20 - 0x2f */ |
| 143 | 'b', 'n', 'm', ',', '.', '/',0xff,0xff,0xff, ' ',0xff,0xff,0xff,0xff,0xff,0xff, /* 0x30 - 0x3f */ |
| 144 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */ |
| 145 | '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x50 - 0x5F */ |
| 146 | '\r',0xff,0xff |
| 147 | }; |
| 148 | |
| 149 | static unsigned char kbd_shift_xlate[] = { |
| 150 | 0xff,0x1b, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+','\b','\t', /* 0x00 - 0x0f */ |
| 151 | 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}','\r',0xff, 'A', 'S', /* 0x10 - 0x1f */ |
| 152 | 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~',0xff, '|', 'Z', 'X', 'C', 'V', /* 0x20 - 0x2f */ |
| 153 | 'B', 'N', 'M', '<', '>', '?',0xff,0xff,0xff, ' ',0xff,0xff,0xff,0xff,0xff,0xff, /* 0x30 - 0x3f */ |
| 154 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */ |
| 155 | '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x50 - 0x5F */ |
| 156 | '\r',0xff,0xff |
| 157 | }; |
| 158 | |
| 159 | static unsigned char kbd_ctrl_xlate[] = { |
| 160 | 0xff,0x1b, '1',0x00, '3', '4', '5',0x1E, '7', '8', '9', '0',0x1F, '=','\b','\t', /* 0x00 - 0x0f */ |
| 161 | 0x11,0x17,0x05,0x12,0x14,0x18,0x15,0x09,0x0f,0x10,0x1b,0x1d,'\n',0xff,0x01,0x13, /* 0x10 - 0x1f */ |
| 162 | 0x04,0x06,0x08,0x09,0x0a,0x0b,0x0c, ';','\'', '~',0x00,0x1c,0x1a,0x18,0x03,0x16, /* 0x20 - 0x2f */ |
| 163 | 0x02,0x0e,0x0d, '<', '>', '?',0xff,0xff,0xff,0x00,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x30 - 0x3f */ |
| 164 | 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1', /* 0x40 - 0x4f */ |
| 165 | '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 0x50 - 0x5F */ |
| 166 | '\r',0xff,0xff |
| 167 | }; |
| 168 | |
| 169 | /****************************************************************** |
| 170 | * Init |
| 171 | ******************************************************************/ |
| 172 | int isa_kbd_init(void) |
| 173 | { |
| 174 | char* result; |
| 175 | result=kbd_initialize(); |
| 176 | if(result==NULL) { |
| 177 | PRINTF("AT Keyboard initialized\n"); |
| 178 | irq_install_handler(25, (interrupt_handler_t *)handle_isa_int, NULL); |
| 179 | isa_irq_install_handler(KBD_INTERRUPT, (interrupt_handler_t *)kbd_interrupt, NULL); |
| 180 | return (1); |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 181 | } else { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 182 | printf("%s\n",result); |
| 183 | return (-1); |
| 184 | } |
| 185 | } |
| 186 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 187 | #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 188 | extern int overwrite_console (void); |
| 189 | #else |
| 190 | int overwrite_console (void) |
| 191 | { |
| 192 | return (0); |
| 193 | } |
| 194 | #endif |
| 195 | |
| 196 | int drv_isa_kbd_init (void) |
| 197 | { |
| 198 | int error; |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 199 | struct stdio_dev kbddev ; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 200 | char *stdinname = getenv ("stdin"); |
| 201 | |
| 202 | if(isa_kbd_init()==-1) |
| 203 | return -1; |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 204 | memset (&kbddev, 0, sizeof(kbddev)); |
| 205 | strcpy(kbddev.name, DEVNAME); |
| 206 | kbddev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 207 | kbddev.getc = kbd_getc ; |
| 208 | kbddev.tstc = kbd_testc ; |
| 209 | |
Jean-Christophe PLAGNIOL-VILLARD | 2a7a031 | 2009-05-16 12:14:54 +0200 | [diff] [blame] | 210 | error = stdio_register (&kbddev); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 211 | if(error==0) { |
| 212 | /* check if this is the standard input device */ |
| 213 | if(strcmp(stdinname,DEVNAME)==0) { |
| 214 | /* reassign the console */ |
| 215 | if(overwrite_console()) { |
| 216 | return 1; |
| 217 | } |
| 218 | error=console_assign(stdin,DEVNAME); |
| 219 | if(error==0) |
| 220 | return 1; |
| 221 | else |
| 222 | return error; |
| 223 | } |
| 224 | return 1; |
| 225 | } |
| 226 | return error; |
| 227 | } |
| 228 | |
| 229 | /****************************************************************** |
| 230 | * Queue handling |
| 231 | ******************************************************************/ |
| 232 | /* puts character in the queue and sets up the in and out pointer */ |
| 233 | void kbd_put_queue(char data) |
| 234 | { |
| 235 | if((in_pointer+1)==KBD_BUFFER_LEN) { |
| 236 | if(out_pointer==0) { |
| 237 | return; /* buffer full */ |
| 238 | } else{ |
| 239 | in_pointer=0; |
| 240 | } |
| 241 | } else { |
| 242 | if((in_pointer+1)==out_pointer) |
| 243 | return; /* buffer full */ |
| 244 | in_pointer++; |
| 245 | } |
| 246 | kbd_buffer[in_pointer]=data; |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | /* test if a character is in the queue */ |
Simon Glass | 0d1e1f7 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 251 | int kbd_testc(struct stdio_dev *dev) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 252 | { |
| 253 | if(in_pointer==out_pointer) |
| 254 | return(0); /* no data */ |
| 255 | else |
| 256 | return(1); |
| 257 | } |
| 258 | /* gets the character from the queue */ |
Simon Glass | 0d1e1f7 | 2014-07-23 06:54:59 -0600 | [diff] [blame] | 259 | int kbd_getc(struct stdio_dev *dev) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 260 | { |
| 261 | char c; |
| 262 | while(in_pointer==out_pointer); |
| 263 | if((out_pointer+1)==KBD_BUFFER_LEN) |
| 264 | out_pointer=0; |
| 265 | else |
| 266 | out_pointer++; |
| 267 | c=kbd_buffer[out_pointer]; |
| 268 | return (int)c; |
| 269 | |
| 270 | } |
| 271 | |
| 272 | |
| 273 | /* set LEDs */ |
| 274 | |
| 275 | void kbd_set_leds(void) |
| 276 | { |
| 277 | if(caps_lock==0) |
| 278 | leds&=~LED_CAP; /* switch caps_lock off */ |
| 279 | else |
| 280 | leds|=LED_CAP; /* switch on LED */ |
| 281 | if(num_lock==0) |
| 282 | leds&=~LED_NUM; /* switch LED off */ |
| 283 | else |
| 284 | leds|=LED_NUM; /* switch on LED */ |
| 285 | if(scroll_lock==0) |
| 286 | leds&=~LED_SCR; /* switch LED off */ |
| 287 | else |
| 288 | leds|=LED_SCR; /* switch on LED */ |
| 289 | kbd_send_data(KBD_CMD_SET_LEDS); |
| 290 | kbd_send_data(leds); |
| 291 | } |
| 292 | |
| 293 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 294 | void handle_keyboard_event (unsigned char scancode) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 295 | { |
| 296 | unsigned char keycode; |
| 297 | |
| 298 | /* Convert scancode to keycode */ |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 299 | PRINTF ("scancode %x\n", scancode); |
| 300 | if (scancode == 0xe0) { |
| 301 | e0 = 1; /* special charakters */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 302 | return; |
| 303 | } |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 304 | if (e0 == 1) { |
| 305 | e0 = 0; /* delete flag */ |
| 306 | if (!(((scancode & 0x7F) == 0x38) || /* the right ctrl key */ |
| 307 | ((scancode & 0x7F) == 0x1D) || /* the right alt key */ |
| 308 | ((scancode & 0x7F) == 0x35) || /* the right '/' key */ |
| 309 | ((scancode & 0x7F) == 0x1C))) |
| 310 | /* the right enter key */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 311 | /* we swallow unknown e0 codes */ |
| 312 | return; |
| 313 | } |
| 314 | /* special cntrl keys */ |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 315 | switch (scancode) { |
| 316 | case 0x2A: |
| 317 | case 0x36: /* shift pressed */ |
| 318 | shift = 1; |
| 319 | return; /* do nothing else */ |
| 320 | case 0xAA: |
| 321 | case 0xB6: /* shift released */ |
| 322 | shift = 0; |
| 323 | return; /* do nothing else */ |
| 324 | case 0x38: /* alt pressed */ |
| 325 | alt = 1; |
| 326 | return; /* do nothing else */ |
| 327 | case 0xB8: /* alt released */ |
| 328 | alt = 0; |
| 329 | return; /* do nothing else */ |
| 330 | case 0x1d: /* ctrl pressed */ |
| 331 | ctrl = 1; |
| 332 | return; /* do nothing else */ |
| 333 | case 0x9d: /* ctrl released */ |
| 334 | ctrl = 0; |
| 335 | return; /* do nothing else */ |
| 336 | case 0x46: /* scrollock pressed */ |
| 337 | scroll_lock = ~scroll_lock; |
| 338 | kbd_set_leds (); |
| 339 | return; /* do nothing else */ |
| 340 | case 0x3A: /* capslock pressed */ |
| 341 | caps_lock = ~caps_lock; |
| 342 | kbd_set_leds (); |
| 343 | return; |
| 344 | case 0x45: /* numlock pressed */ |
| 345 | num_lock = ~num_lock; |
| 346 | kbd_set_leds (); |
| 347 | return; |
| 348 | case 0xC6: /* scroll lock released */ |
| 349 | case 0xC5: /* num lock released */ |
| 350 | case 0xBA: /* caps lock released */ |
| 351 | return; /* just swallow */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 352 | } |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 353 | if ((scancode & 0x80) == 0x80) /* key released */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 354 | return; |
| 355 | /* now, decide which table we need */ |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 356 | if (scancode > (sizeof (kbd_plain_xlate) / sizeof (kbd_plain_xlate[0]))) { /* scancode not in list */ |
| 357 | PRINTF ("unkown scancode %X\n", scancode); |
| 358 | return; /* swallow it */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 359 | } |
| 360 | /* setup plain code first */ |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 361 | keycode = kbd_plain_xlate[scancode]; |
| 362 | if (caps_lock == 1) { /* caps_lock is pressed, overwrite plain code */ |
| 363 | if (scancode > (sizeof (kbd_shift_xlate) / sizeof (kbd_shift_xlate[0]))) { /* scancode not in list */ |
| 364 | PRINTF ("unkown caps-locked scancode %X\n", scancode); |
| 365 | return; /* swallow it */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 366 | } |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 367 | keycode = kbd_shift_xlate[scancode]; |
| 368 | if (keycode < 'A') { /* we only want the alphas capital */ |
| 369 | keycode = kbd_plain_xlate[scancode]; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 370 | } |
| 371 | } |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 372 | if (shift == 1) { /* shift overwrites caps_lock */ |
| 373 | if (scancode > (sizeof (kbd_shift_xlate) / sizeof (kbd_shift_xlate[0]))) { /* scancode not in list */ |
| 374 | PRINTF ("unkown shifted scancode %X\n", scancode); |
| 375 | return; /* swallow it */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 376 | } |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 377 | keycode = kbd_shift_xlate[scancode]; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 378 | } |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 379 | if (ctrl == 1) { /* ctrl overwrites caps_lock and shift */ |
| 380 | if (scancode > (sizeof (kbd_ctrl_xlate) / sizeof (kbd_ctrl_xlate[0]))) { /* scancode not in list */ |
| 381 | PRINTF ("unkown ctrl scancode %X\n", scancode); |
| 382 | return; /* swallow it */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 383 | } |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 384 | keycode = kbd_ctrl_xlate[scancode]; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 385 | } |
| 386 | /* check if valid keycode */ |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 387 | if (keycode == 0xff) { |
| 388 | PRINTF ("unkown scancode %X\n", scancode); |
| 389 | return; /* swallow unknown codes */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 392 | kbd_put_queue (keycode); |
| 393 | PRINTF ("%x\n", keycode); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | /* |
| 397 | * This reads the keyboard status port, and does the |
| 398 | * appropriate action. |
| 399 | * |
| 400 | */ |
| 401 | unsigned char handle_kbd_event(void) |
| 402 | { |
| 403 | unsigned char status = kbd_read_status(); |
| 404 | unsigned int work = 10000; |
| 405 | |
| 406 | while ((--work > 0) && (status & KBD_STAT_OBF)) { |
| 407 | unsigned char scancode; |
| 408 | |
| 409 | scancode = kbd_read_input(); |
| 410 | |
| 411 | /* Error bytes must be ignored to make the |
| 412 | Synaptics touchpads compaq use work */ |
| 413 | /* Ignore error bytes */ |
| 414 | if (!(status & (KBD_STAT_GTO | KBD_STAT_PERR))) |
| 415 | { |
| 416 | if (status & KBD_STAT_MOUSE_OBF) |
| 417 | ; /* not supported: handle_mouse_event(scancode); */ |
| 418 | else |
| 419 | handle_keyboard_event(scancode); |
| 420 | } |
| 421 | status = kbd_read_status(); |
| 422 | } |
| 423 | if (!work) |
| 424 | PRINTF("pc_keyb: controller jammed (0x%02X).\n", status); |
| 425 | return status; |
| 426 | } |
| 427 | |
| 428 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 429 | /****************************************************************************** |
| 430 | * Lowlevel Part of keyboard section |
| 431 | */ |
| 432 | unsigned char kbd_read_status(void) |
| 433 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 434 | return(in8(CONFIG_SYS_ISA_IO_BASE_ADDRESS + KDB_COMMAND_PORT)); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | unsigned char kbd_read_input(void) |
| 438 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 439 | return(in8(CONFIG_SYS_ISA_IO_BASE_ADDRESS + KDB_DATA_PORT)); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | void kbd_write_command(unsigned char cmd) |
| 443 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 444 | out8(CONFIG_SYS_ISA_IO_BASE_ADDRESS + KDB_COMMAND_PORT,cmd); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | void kbd_write_output(unsigned char data) |
| 448 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 449 | out8(CONFIG_SYS_ISA_IO_BASE_ADDRESS + KDB_DATA_PORT, data); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | int kbd_read_data(void) |
| 453 | { |
| 454 | int val; |
| 455 | unsigned char status; |
| 456 | |
Wolfgang Denk | dc770c7 | 2008-07-14 15:19:07 +0200 | [diff] [blame] | 457 | val = -1; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 458 | status = kbd_read_status(); |
| 459 | if (status & KBD_STAT_OBF) { |
| 460 | val = kbd_read_input(); |
| 461 | if (status & (KBD_STAT_GTO | KBD_STAT_PERR)) |
| 462 | val = -2; |
| 463 | } |
| 464 | return val; |
| 465 | } |
| 466 | |
| 467 | int kbd_wait_for_input(void) |
| 468 | { |
| 469 | unsigned long timeout; |
| 470 | int val; |
| 471 | |
| 472 | timeout = KBD_TIMEOUT; |
| 473 | val=kbd_read_data(); |
| 474 | while(val < 0) |
| 475 | { |
| 476 | if(timeout--==0) |
| 477 | return -1; |
| 478 | udelay(1000); |
| 479 | val=kbd_read_data(); |
| 480 | } |
| 481 | return val; |
| 482 | } |
| 483 | |
| 484 | |
| 485 | int kb_wait(void) |
| 486 | { |
| 487 | unsigned long timeout = KBC_TIMEOUT * 10; |
| 488 | |
| 489 | do { |
| 490 | unsigned char status = handle_kbd_event(); |
| 491 | if (!(status & KBD_STAT_IBF)) |
| 492 | return 0; /* ok */ |
| 493 | udelay(1000); |
| 494 | timeout--; |
| 495 | } while (timeout); |
| 496 | return 1; |
| 497 | } |
| 498 | |
| 499 | void kbd_write_command_w(int data) |
| 500 | { |
| 501 | if(kb_wait()) |
| 502 | PRINTF("timeout in kbd_write_command_w\n"); |
| 503 | kbd_write_command(data); |
| 504 | } |
| 505 | |
| 506 | void kbd_write_output_w(int data) |
| 507 | { |
| 508 | if(kb_wait()) |
| 509 | PRINTF("timeout in kbd_write_output_w\n"); |
| 510 | kbd_write_output(data); |
| 511 | } |
| 512 | |
| 513 | void kbd_send_data(unsigned char data) |
| 514 | { |
| 515 | unsigned char status; |
| 516 | disable_8259A_irq(1); /* disable interrupt */ |
| 517 | kbd_write_output_w(data); |
| 518 | status = kbd_wait_for_input(); |
| 519 | if (status == KBD_REPLY_ACK) |
| 520 | enable_8259A_irq(1); /* enable interrupt */ |
| 521 | } |
| 522 | |
| 523 | |
| 524 | char * kbd_initialize(void) |
| 525 | { |
| 526 | int status; |
| 527 | |
| 528 | in_pointer = 0; /* delete in Buffer */ |
| 529 | out_pointer = 0; |
| 530 | /* |
| 531 | * Test the keyboard interface. |
| 532 | * This seems to be the only way to get it going. |
| 533 | * If the test is successful a x55 is placed in the input buffer. |
| 534 | */ |
| 535 | kbd_write_command_w(KBD_CCMD_SELF_TEST); |
| 536 | if (kbd_wait_for_input() != 0x55) |
| 537 | return "Kbd: failed self test"; |
| 538 | /* |
| 539 | * Perform a keyboard interface test. This causes the controller |
| 540 | * to test the keyboard clock and data lines. The results of the |
| 541 | * test are placed in the input buffer. |
| 542 | */ |
| 543 | kbd_write_command_w(KBD_CCMD_KBD_TEST); |
| 544 | if (kbd_wait_for_input() != 0x00) |
| 545 | return "Kbd: interface failed self test"; |
| 546 | /* |
| 547 | * Enable the keyboard by allowing the keyboard clock to run. |
| 548 | */ |
| 549 | kbd_write_command_w(KBD_CCMD_KBD_ENABLE); |
| 550 | status = kbd_wait_for_input(); |
| 551 | /* |
| 552 | * Reset keyboard. If the read times out |
| 553 | * then the assumption is that no keyboard is |
| 554 | * plugged into the machine. |
| 555 | * This defaults the keyboard to scan-code set 2. |
| 556 | * |
| 557 | * Set up to try again if the keyboard asks for RESEND. |
| 558 | */ |
| 559 | do { |
| 560 | kbd_write_output_w(KBD_CMD_RESET); |
| 561 | status = kbd_wait_for_input(); |
| 562 | if (status == KBD_REPLY_ACK) |
| 563 | break; |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 564 | if (status != KBD_REPLY_RESEND) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 565 | PRINTF("status: %X\n",status); |
| 566 | return "Kbd: reset failed, no ACK"; |
| 567 | } |
| 568 | } while (1); |
| 569 | if (kbd_wait_for_input() != KBD_REPLY_POR) |
| 570 | return "Kbd: reset failed, no POR"; |
| 571 | |
| 572 | /* |
| 573 | * Set keyboard controller mode. During this, the keyboard should be |
| 574 | * in the disabled state. |
| 575 | * |
| 576 | * Set up to try again if the keyboard asks for RESEND. |
| 577 | */ |
| 578 | do { |
| 579 | kbd_write_output_w(KBD_CMD_DISABLE); |
| 580 | status = kbd_wait_for_input(); |
| 581 | if (status == KBD_REPLY_ACK) |
| 582 | break; |
| 583 | if (status != KBD_REPLY_RESEND) |
| 584 | return "Kbd: disable keyboard: no ACK"; |
| 585 | } while (1); |
| 586 | |
| 587 | kbd_write_command_w(KBD_CCMD_WRITE_MODE); |
| 588 | kbd_write_output_w(KBD_MODE_KBD_INT |
| 589 | | KBD_MODE_SYS |
| 590 | | KBD_MODE_DISABLE_MOUSE |
| 591 | | KBD_MODE_KCC); |
| 592 | |
Wolfgang Denk | 0ee7077 | 2005-09-23 11:05:55 +0200 | [diff] [blame] | 593 | /* AMCC powerpc portables need this to use scan-code set 1 -- Cort */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 594 | kbd_write_command_w(KBD_CCMD_READ_MODE); |
| 595 | if (!(kbd_wait_for_input() & KBD_MODE_KCC)) { |
| 596 | /* |
| 597 | * If the controller does not support conversion, |
| 598 | * Set the keyboard to scan-code set 1. |
| 599 | */ |
| 600 | kbd_write_output_w(0xF0); |
| 601 | kbd_wait_for_input(); |
| 602 | kbd_write_output_w(0x01); |
| 603 | kbd_wait_for_input(); |
| 604 | } |
| 605 | kbd_write_output_w(KBD_CMD_ENABLE); |
| 606 | if (kbd_wait_for_input() != KBD_REPLY_ACK) |
| 607 | return "Kbd: enable keyboard: no ACK"; |
| 608 | |
| 609 | /* |
| 610 | * Finally, set the typematic rate to maximum. |
| 611 | */ |
| 612 | kbd_write_output_w(KBD_CMD_SET_RATE); |
| 613 | if (kbd_wait_for_input() != KBD_REPLY_ACK) |
| 614 | return "Kbd: Set rate: no ACK"; |
| 615 | kbd_write_output_w(0x00); |
| 616 | if (kbd_wait_for_input() != KBD_REPLY_ACK) |
| 617 | return "Kbd: Set rate: no ACK"; |
| 618 | return NULL; |
| 619 | } |
| 620 | |
| 621 | void kbd_interrupt(void) |
| 622 | { |
| 623 | handle_kbd_event(); |
| 624 | } |