1
2
3
4
5
6
7
8
9
10#include <common.h>
11#include <console.h>
12#include <asm/processor.h>
13#include <stdio_dev.h>
14#include "isa.h"
15#include "kbd.h"
16
17
18unsigned char kbd_read_status(void);
19unsigned char kbd_read_input(void);
20void kbd_send_data(unsigned char data);
21void disable_8259A_irq(unsigned int irq);
22void enable_8259A_irq(unsigned int irq);
23
24
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
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
43
44#define KBD_INIT_TIMEOUT 1000
45#define KBC_TIMEOUT 250
46#define KBD_TIMEOUT 2000
47
48
49
50
51#define KBD_CCMD_READ_MODE 0x20
52#define KBD_CCMD_WRITE_MODE 0x60
53#define KBD_CCMD_GET_VERSION 0xA1
54#define KBD_CCMD_MOUSE_DISABLE 0xA7
55#define KBD_CCMD_MOUSE_ENABLE 0xA8
56#define KBD_CCMD_TEST_MOUSE 0xA9
57#define KBD_CCMD_SELF_TEST 0xAA
58#define KBD_CCMD_KBD_TEST 0xAB
59#define KBD_CCMD_KBD_DISABLE 0xAD
60#define KBD_CCMD_KBD_ENABLE 0xAE
61#define KBD_CCMD_WRITE_AUX_OBUF 0xD3
62
63#define KBD_CCMD_WRITE_MOUSE 0xD4
64
65
66
67
68
69#define KBD_CMD_SET_LEDS 0xED
70#define KBD_CMD_SET_RATE 0xF3
71#define KBD_CMD_ENABLE 0xF4
72#define KBD_CMD_DISABLE 0xF5
73#define KBD_CMD_RESET 0xFF
74
75
76
77
78
79#define KBD_REPLY_POR 0xAA
80#define KBD_REPLY_ACK 0xFA
81#define KBD_REPLY_RESEND 0xFE
82
83
84
85
86
87#define KBD_STAT_OBF 0x01
88#define KBD_STAT_IBF 0x02
89#define KBD_STAT_SELFTEST 0x04
90#define KBD_STAT_CMD 0x08
91#define KBD_STAT_UNLOCKED 0x10
92#define KBD_STAT_MOUSE_OBF 0x20
93#define KBD_STAT_GTO 0x40
94#define KBD_STAT_PERR 0x80
95
96#define AUX_STAT_OBF (KBD_STAT_OBF | KBD_STAT_MOUSE_OBF)
97
98
99
100
101
102#define KBD_MODE_KBD_INT 0x01
103#define KBD_MODE_MOUSE_INT 0x02
104#define KBD_MODE_SYS 0x04
105#define KBD_MODE_NO_KEYLOCK 0x08
106#define KBD_MODE_DISABLE_KBD 0x10
107#define KBD_MODE_DISABLE_MOUSE 0x20
108#define KBD_MODE_KCC 0x40
109#define KBD_MODE_RFU 0x80
110
111
112#define KDB_DATA_PORT 0x60
113#define KDB_COMMAND_PORT 0x64
114
115#define LED_SCR 0x01
116#define LED_CAP 0x04
117#define LED_NUM 0x02
118
119#define KBD_BUFFER_LEN 0x20
120
121
122static volatile char kbd_buffer[KBD_BUFFER_LEN];
123static volatile int in_pointer = 0;
124static volatile int out_pointer = 0;
125
126
127static unsigned char num_lock = 0;
128static unsigned char caps_lock = 0;
129static unsigned char scroll_lock = 0;
130static unsigned char shift = 0;
131static unsigned char ctrl = 0;
132static unsigned char alt = 0;
133static unsigned char e0 = 0;
134static unsigned char leds = 0;
135
136#define DEVNAME "kbd"
137
138
139
140static unsigned char kbd_plain_xlate[] = {
141 0xff,0x1b, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=','\b','\t',
142 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']','\r',0xff, 'a', 's',
143 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';','\'', '`',0xff,'\\', 'z', 'x', 'c', 'v',
144 'b', 'n', 'm', ',', '.', '/',0xff,0xff,0xff, ' ',0xff,0xff,0xff,0xff,0xff,0xff,
145 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1',
146 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
147 '\r',0xff,0xff
148 };
149
150static unsigned char kbd_shift_xlate[] = {
151 0xff,0x1b, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+','\b','\t',
152 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}','\r',0xff, 'A', 'S',
153 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~',0xff, '|', 'Z', 'X', 'C', 'V',
154 'B', 'N', 'M', '<', '>', '?',0xff,0xff,0xff, ' ',0xff,0xff,0xff,0xff,0xff,0xff,
155 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1',
156 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
157 '\r',0xff,0xff
158 };
159
160static unsigned char kbd_ctrl_xlate[] = {
161 0xff,0x1b, '1',0x00, '3', '4', '5',0x1E, '7', '8', '9', '0',0x1F, '=','\b','\t',
162 0x11,0x17,0x05,0x12,0x14,0x18,0x15,0x09,0x0f,0x10,0x1b,0x1d,'\n',0xff,0x01,0x13,
163 0x04,0x06,0x08,0x09,0x0a,0x0b,0x0c, ';','\'', '~',0x00,0x1c,0x1a,0x18,0x03,0x16,
164 0x02,0x0e,0x0d, '<', '>', '?',0xff,0xff,0xff,0x00,0xff,0xff,0xff,0xff,0xff,0xff,
165 0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1',
166 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
167 '\r',0xff,0xff
168 };
169
170
171
172
173int 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);
182 } else {
183 printf("%s\n",result);
184 return (-1);
185 }
186}
187
188#ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
189extern int overwrite_console (void);
190#else
191int overwrite_console (void)
192{
193 return (0);
194}
195#endif
196
197int drv_isa_kbd_init (void)
198{
199 int error;
200 struct stdio_dev kbddev ;
201 char *stdinname = getenv ("stdin");
202
203 if(isa_kbd_init()==-1)
204 return -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 ;
210
211 error = stdio_register (&kbddev);
212 if(error==0) {
213
214 if(strcmp(stdinname,DEVNAME)==0) {
215
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
232
233
234void kbd_put_queue(char data)
235{
236 if((in_pointer+1)==KBD_BUFFER_LEN) {
237 if(out_pointer==0) {
238 return;
239 } else{
240 in_pointer=0;
241 }
242 } else {
243 if((in_pointer+1)==out_pointer)
244 return;
245 in_pointer++;
246 }
247 kbd_buffer[in_pointer]=data;
248 return;
249}
250
251
252int kbd_testc(struct stdio_dev *dev)
253{
254 if(in_pointer==out_pointer)
255 return(0);
256 else
257 return(1);
258}
259
260int kbd_getc(struct stdio_dev *dev)
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
275
276void kbd_set_leds(void)
277{
278 if(caps_lock==0)
279 leds&=~LED_CAP;
280 else
281 leds|=LED_CAP;
282 if(num_lock==0)
283 leds&=~LED_NUM;
284 else
285 leds|=LED_NUM;
286 if(scroll_lock==0)
287 leds&=~LED_SCR;
288 else
289 leds|=LED_SCR;
290 kbd_send_data(KBD_CMD_SET_LEDS);
291 kbd_send_data(leds);
292}
293
294
295void handle_keyboard_event (unsigned char scancode)
296{
297 unsigned char keycode;
298
299
300 PRINTF ("scancode %x\n", scancode);
301 if (scancode == 0xe0) {
302 e0 = 1;
303 return;
304 }
305 if (e0 == 1) {
306 e0 = 0;
307 if (!(((scancode & 0x7F) == 0x38) ||
308 ((scancode & 0x7F) == 0x1D) ||
309 ((scancode & 0x7F) == 0x35) ||
310 ((scancode & 0x7F) == 0x1C)))
311
312
313 return;
314 }
315
316 switch (scancode) {
317 case 0x2A:
318 case 0x36:
319 shift = 1;
320 return;
321 case 0xAA:
322 case 0xB6:
323 shift = 0;
324 return;
325 case 0x38:
326 alt = 1;
327 return;
328 case 0xB8:
329 alt = 0;
330 return;
331 case 0x1d:
332 ctrl = 1;
333 return;
334 case 0x9d:
335 ctrl = 0;
336 return;
337 case 0x46:
338 scroll_lock = ~scroll_lock;
339 kbd_set_leds ();
340 return;
341 case 0x3A:
342 caps_lock = ~caps_lock;
343 kbd_set_leds ();
344 return;
345 case 0x45:
346 num_lock = ~num_lock;
347 kbd_set_leds ();
348 return;
349 case 0xC6:
350 case 0xC5:
351 case 0xBA:
352 return;
353 }
354 if ((scancode & 0x80) == 0x80)
355 return;
356
357 if (scancode > (sizeof (kbd_plain_xlate) / sizeof (kbd_plain_xlate[0]))) {
358 PRINTF ("unkown scancode %X\n", scancode);
359 return;
360 }
361
362 keycode = kbd_plain_xlate[scancode];
363 if (caps_lock == 1) {
364 if (scancode > (sizeof (kbd_shift_xlate) / sizeof (kbd_shift_xlate[0]))) {
365 PRINTF ("unkown caps-locked scancode %X\n", scancode);
366 return;
367 }
368 keycode = kbd_shift_xlate[scancode];
369 if (keycode < 'A') {
370 keycode = kbd_plain_xlate[scancode];
371 }
372 }
373 if (shift == 1) {
374 if (scancode > (sizeof (kbd_shift_xlate) / sizeof (kbd_shift_xlate[0]))) {
375 PRINTF ("unkown shifted scancode %X\n", scancode);
376 return;
377 }
378 keycode = kbd_shift_xlate[scancode];
379 }
380 if (ctrl == 1) {
381 if (scancode > (sizeof (kbd_ctrl_xlate) / sizeof (kbd_ctrl_xlate[0]))) {
382 PRINTF ("unkown ctrl scancode %X\n", scancode);
383 return;
384 }
385 keycode = kbd_ctrl_xlate[scancode];
386 }
387
388 if (keycode == 0xff) {
389 PRINTF ("unkown scancode %X\n", scancode);
390 return;
391 }
392
393 kbd_put_queue (keycode);
394 PRINTF ("%x\n", keycode);
395}
396
397
398
399
400
401
402unsigned 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
413
414
415 if (!(status & (KBD_STAT_GTO | KBD_STAT_PERR)))
416 {
417 if (status & KBD_STAT_MOUSE_OBF)
418 ;
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
430
431
432
433unsigned char kbd_read_status(void)
434{
435 return(in8(CONFIG_SYS_ISA_IO_BASE_ADDRESS + KDB_COMMAND_PORT));
436}
437
438unsigned char kbd_read_input(void)
439{
440 return(in8(CONFIG_SYS_ISA_IO_BASE_ADDRESS + KDB_DATA_PORT));
441}
442
443void kbd_write_command(unsigned char cmd)
444{
445 out8(CONFIG_SYS_ISA_IO_BASE_ADDRESS + KDB_COMMAND_PORT,cmd);
446}
447
448void kbd_write_output(unsigned char data)
449{
450 out8(CONFIG_SYS_ISA_IO_BASE_ADDRESS + KDB_DATA_PORT, data);
451}
452
453int kbd_read_data(void)
454{
455 int val;
456 unsigned char status;
457
458 val = -1;
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
468int 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
486int 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;
494 udelay(1000);
495 timeout--;
496 } while (timeout);
497 return 1;
498}
499
500void 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
507void 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
514void kbd_send_data(unsigned char data)
515{
516 unsigned char status;
517 disable_8259A_irq(1);
518 kbd_write_output_w(data);
519 status = kbd_wait_for_input();
520 if (status == KBD_REPLY_ACK)
521 enable_8259A_irq(1);
522}
523
524
525char * kbd_initialize(void)
526{
527 int status;
528
529 in_pointer = 0;
530 out_pointer = 0;
531
532
533
534
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
541
542
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
549
550 kbd_write_command_w(KBD_CCMD_KBD_ENABLE);
551 status = kbd_wait_for_input();
552
553
554
555
556
557
558
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;
565 if (status != KBD_REPLY_RESEND) {
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
575
576
577
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
594
595 kbd_write_command_w(KBD_CCMD_READ_MODE);
596 if (!(kbd_wait_for_input() & KBD_MODE_KCC)) {
597
598
599
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
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
622void kbd_interrupt(void)
623{
624 handle_kbd_event();
625}
626