blob: 36b169489eac2d22574d791a986c15974ffeccbc [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +00001/*
2 * (C) Copyright 2001
3 * Denis Peter, MPL AG Switzerland, d.peter@mpl.ch
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkaffae2b2002-08-17 09:36:01 +00006 *
7 * Source partly derived from:
8 * linux/drivers/char/pc_keyb.c
wdenkaffae2b2002-08-17 09:36:01 +00009 */
10#include <common.h>
Simon Glassa73bda42015-11-08 23:47:45 -070011#include <console.h>
wdenkaffae2b2002-08-17 09:36:01 +000012#include <asm/processor.h>
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020013#include <stdio_dev.h>
wdenkaffae2b2002-08-17 09:36:01 +000014#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/* 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 Denka1be4762008-05-20 16:00:29 +020038#define KBD_STAT_CD 0x08
wdenkaffae2b2002-08-17 09:36:01 +000039#define KBD_STAT_LOCK 0x10
40#define KBD_STAT_MOBF 0x20
Wolfgang Denka1be4762008-05-20 16:00:29 +020041#define KBD_STAT_TI_OUT 0x40
42#define KBD_STAT_PARERR 0x80
wdenkaffae2b2002-08-17 09:36:01 +000043
Wolfgang Denka1be4762008-05-20 16:00:29 +020044#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 */
wdenkaffae2b2002-08-17 09:36:01 +000047/*
48 * Keyboard Controller Commands
49 */
50
Wolfgang Denka1be4762008-05-20 16:00:29 +020051#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 */
wdenkaffae2b2002-08-17 09:36:01 +000054#define KBD_CCMD_MOUSE_DISABLE 0xA7 /* Disable mouse interface */
Wolfgang Denka1be4762008-05-20 16:00:29 +020055#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 */
wdenkaffae2b2002-08-17 09:36:01 +000061#define KBD_CCMD_WRITE_AUX_OBUF 0xD3 /* Write to output buffer as if
62 initiated by the auxiliary device */
Wolfgang Denka1be4762008-05-20 16:00:29 +020063#define KBD_CCMD_WRITE_MOUSE 0xD4 /* Write the following byte to the mouse */
wdenkaffae2b2002-08-17 09:36:01 +000064
65/*
66 * Keyboard Commands
67 */
68
Wolfgang Denka1be4762008-05-20 16:00:29 +020069#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 */
wdenkaffae2b2002-08-17 09:36:01 +000074
75/*
76 * Keyboard Replies
77 */
78
Wolfgang Denka1be4762008-05-20 16:00:29 +020079#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 */
wdenkaffae2b2002-08-17 09:36:01 +000082
83/*
84 * Status Register Bits
85 */
86
Wolfgang Denka1be4762008-05-20 16:00:29 +020087#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 */
wdenkaffae2b2002-08-17 09:36:01 +000095
96#define AUX_STAT_OBF (KBD_STAT_OBF | KBD_STAT_MOUSE_OBF)
97
98/*
99 * Controller Mode Register Bits
100 */
101
Wolfgang Denka1be4762008-05-20 16:00:29 +0200102#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 */
wdenkaffae2b2002-08-17 09:36:01 +0000107#define KBD_MODE_DISABLE_MOUSE 0x20 /* Disable mouse interface */
Wolfgang Denka1be4762008-05-20 16:00:29 +0200108#define KBD_MODE_KCC 0x40 /* Scan code conversion to PC format */
109#define KBD_MODE_RFU 0x80
wdenkaffae2b2002-08-17 09:36:01 +0000110
111
Wolfgang Denka1be4762008-05-20 16:00:29 +0200112#define KDB_DATA_PORT 0x60
wdenkaffae2b2002-08-17 09:36:01 +0000113#define KDB_COMMAND_PORT 0x64
114
Wolfgang Denka1be4762008-05-20 16:00:29 +0200115#define LED_SCR 0x01 /* scroll lock led */
116#define LED_CAP 0x04 /* caps lock led */
117#define LED_NUM 0x02 /* num lock led */
wdenkaffae2b2002-08-17 09:36:01 +0000118
Wolfgang Denka1be4762008-05-20 16:00:29 +0200119#define KBD_BUFFER_LEN 0x20 /* size of the keyboardbuffer */
wdenkaffae2b2002-08-17 09:36:01 +0000120
121
wdenkaffae2b2002-08-17 09:36:01 +0000122static 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/* Simple translation table for the keys */
139
140static 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
150static 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
160static 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 ******************************************************************/
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);
Wolfgang Denka1be4762008-05-20 16:00:29 +0200182 } else {
wdenkaffae2b2002-08-17 09:36:01 +0000183 printf("%s\n",result);
184 return (-1);
185 }
186}
187
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200188#ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
wdenkaffae2b2002-08-17 09:36:01 +0000189extern 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;
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200200 struct stdio_dev kbddev ;
wdenkaffae2b2002-08-17 09:36:01 +0000201 char *stdinname = getenv ("stdin");
202
203 if(isa_kbd_init()==-1)
204 return -1;
Wolfgang Denka1be4762008-05-20 16:00:29 +0200205 memset (&kbddev, 0, sizeof(kbddev));
206 strcpy(kbddev.name, DEVNAME);
Bin Meng6abe4b62015-11-03 23:23:37 -0800207 kbddev.flags = DEV_FLAGS_INPUT;
wdenkaffae2b2002-08-17 09:36:01 +0000208 kbddev.getc = kbd_getc ;
209 kbddev.tstc = kbd_testc ;
210
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200211 error = stdio_register (&kbddev);
wdenkaffae2b2002-08-17 09:36:01 +0000212 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 */
234void 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 Glass0d1e1f72014-07-23 06:54:59 -0600252int kbd_testc(struct stdio_dev *dev)
wdenkaffae2b2002-08-17 09:36:01 +0000253{
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 Glass0d1e1f72014-07-23 06:54:59 -0600260int kbd_getc(struct stdio_dev *dev)
wdenkaffae2b2002-08-17 09:36:01 +0000261{
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
276void 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 Denka1be4762008-05-20 16:00:29 +0200295void handle_keyboard_event (unsigned char scancode)
wdenkaffae2b2002-08-17 09:36:01 +0000296{
297 unsigned char keycode;
298
299 /* Convert scancode to keycode */
Wolfgang Denka1be4762008-05-20 16:00:29 +0200300 PRINTF ("scancode %x\n", scancode);
301 if (scancode == 0xe0) {
302 e0 = 1; /* special charakters */
wdenkaffae2b2002-08-17 09:36:01 +0000303 return;
304 }
Wolfgang Denka1be4762008-05-20 16:00:29 +0200305 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 */
wdenkaffae2b2002-08-17 09:36:01 +0000312 /* we swallow unknown e0 codes */
313 return;
314 }
315 /* special cntrl keys */
Wolfgang Denka1be4762008-05-20 16:00:29 +0200316 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 */
wdenkaffae2b2002-08-17 09:36:01 +0000353 }
Wolfgang Denka1be4762008-05-20 16:00:29 +0200354 if ((scancode & 0x80) == 0x80) /* key released */
wdenkaffae2b2002-08-17 09:36:01 +0000355 return;
356 /* now, decide which table we need */
Wolfgang Denka1be4762008-05-20 16:00:29 +0200357 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 */
wdenkaffae2b2002-08-17 09:36:01 +0000360 }
361 /* setup plain code first */
Wolfgang Denka1be4762008-05-20 16:00:29 +0200362 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 */
wdenkaffae2b2002-08-17 09:36:01 +0000367 }
Wolfgang Denka1be4762008-05-20 16:00:29 +0200368 keycode = kbd_shift_xlate[scancode];
369 if (keycode < 'A') { /* we only want the alphas capital */
370 keycode = kbd_plain_xlate[scancode];
wdenkaffae2b2002-08-17 09:36:01 +0000371 }
372 }
Wolfgang Denka1be4762008-05-20 16:00:29 +0200373 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 */
wdenkaffae2b2002-08-17 09:36:01 +0000377 }
Wolfgang Denka1be4762008-05-20 16:00:29 +0200378 keycode = kbd_shift_xlate[scancode];
wdenkaffae2b2002-08-17 09:36:01 +0000379 }
Wolfgang Denka1be4762008-05-20 16:00:29 +0200380 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 */
wdenkaffae2b2002-08-17 09:36:01 +0000384 }
Wolfgang Denka1be4762008-05-20 16:00:29 +0200385 keycode = kbd_ctrl_xlate[scancode];
wdenkaffae2b2002-08-17 09:36:01 +0000386 }
387 /* check if valid keycode */
Wolfgang Denka1be4762008-05-20 16:00:29 +0200388 if (keycode == 0xff) {
389 PRINTF ("unkown scancode %X\n", scancode);
390 return; /* swallow unknown codes */
wdenkaffae2b2002-08-17 09:36:01 +0000391 }
392
Wolfgang Denka1be4762008-05-20 16:00:29 +0200393 kbd_put_queue (keycode);
394 PRINTF ("%x\n", keycode);
wdenkaffae2b2002-08-17 09:36:01 +0000395}
396
397/*
398 * This reads the keyboard status port, and does the
399 * appropriate action.
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 /* 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
wdenkaffae2b2002-08-17 09:36:01 +0000430/******************************************************************************
431 * Lowlevel Part of keyboard section
432 */
433unsigned char kbd_read_status(void)
434{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200435 return(in8(CONFIG_SYS_ISA_IO_BASE_ADDRESS + KDB_COMMAND_PORT));
wdenkaffae2b2002-08-17 09:36:01 +0000436}
437
438unsigned char kbd_read_input(void)
439{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200440 return(in8(CONFIG_SYS_ISA_IO_BASE_ADDRESS + KDB_DATA_PORT));
wdenkaffae2b2002-08-17 09:36:01 +0000441}
442
443void kbd_write_command(unsigned char cmd)
444{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200445 out8(CONFIG_SYS_ISA_IO_BASE_ADDRESS + KDB_COMMAND_PORT,cmd);
wdenkaffae2b2002-08-17 09:36:01 +0000446}
447
448void kbd_write_output(unsigned char data)
449{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200450 out8(CONFIG_SYS_ISA_IO_BASE_ADDRESS + KDB_DATA_PORT, data);
wdenkaffae2b2002-08-17 09:36:01 +0000451}
452
453int kbd_read_data(void)
454{
455 int val;
456 unsigned char status;
457
Wolfgang Denkdc770c72008-07-14 15:19:07 +0200458 val = -1;
wdenkaffae2b2002-08-17 09:36:01 +0000459 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; /* ok */
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); /* 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
525char * 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 Denka1be4762008-05-20 16:00:29 +0200565 if (status != KBD_REPLY_RESEND) {
wdenkaffae2b2002-08-17 09:36:01 +0000566 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 Denk0ee70772005-09-23 11:05:55 +0200594 /* AMCC powerpc portables need this to use scan-code set 1 -- Cort */
wdenkaffae2b2002-08-17 09:36:01 +0000595 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
622void kbd_interrupt(void)
623{
624 handle_kbd_event();
625}