kbd.c 16.9 KB
Newer Older
wdenk's avatar
wdenk committed
1
2
3
4
/*
 * (C) Copyright 2001
 * Denis Peter, MPL AG Switzerland, d.peter@mpl.ch
 *
5
 * SPDX-License-Identifier:	GPL-2.0+
wdenk's avatar
wdenk committed
6
7
8
9
10
11
 *
 * Source partly derived from:
 * linux/drivers/char/pc_keyb.c
 */
#include <common.h>
#include <asm/processor.h>
12
#include <stdio_dev.h>
wdenk's avatar
wdenk committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "isa.h"
#include "kbd.h"


unsigned char kbd_read_status(void);
unsigned char kbd_read_input(void);
void kbd_send_data(unsigned char data);
void disable_8259A_irq(unsigned int irq);
void enable_8259A_irq(unsigned int irq);

/* used only by send_data - set by keyboard_interrupt */


#undef KBG_DEBUG

#ifdef KBG_DEBUG
#define	PRINTF(fmt,args...)	printf (fmt ,##args)
#else
#define PRINTF(fmt,args...)
#endif

#define KBD_STAT_KOBF		0x01
#define KBD_STAT_IBF		0x02
#define KBD_STAT_SYS		0x04
Wolfgang Denk's avatar
Wolfgang Denk committed
37
#define KBD_STAT_CD		0x08
wdenk's avatar
wdenk committed
38
39
#define KBD_STAT_LOCK		0x10
#define KBD_STAT_MOBF		0x20
Wolfgang Denk's avatar
Wolfgang Denk committed
40
41
#define KBD_STAT_TI_OUT		0x40
#define KBD_STAT_PARERR		0x80
wdenk's avatar
wdenk committed
42

Wolfgang Denk's avatar
Wolfgang Denk committed
43
44
45
#define KBD_INIT_TIMEOUT	1000	/* Timeout in ms for initializing the keyboard */
#define KBC_TIMEOUT		250	/* Timeout in ms for sending to keyboard controller */
#define KBD_TIMEOUT		2000	/* Timeout in ms for keyboard command acknowledge */
wdenk's avatar
wdenk committed
46
47
48
49
/*
 *	Keyboard Controller Commands
 */

Wolfgang Denk's avatar
Wolfgang Denk committed
50
51
52
#define KBD_CCMD_READ_MODE	0x20	/* Read mode bits */
#define KBD_CCMD_WRITE_MODE	0x60	/* Write mode bits */
#define KBD_CCMD_GET_VERSION	0xA1	/* Get controller version */
wdenk's avatar
wdenk committed
53
#define KBD_CCMD_MOUSE_DISABLE	0xA7	/* Disable mouse interface */
Wolfgang Denk's avatar
Wolfgang Denk committed
54
55
56
57
58
59
#define KBD_CCMD_MOUSE_ENABLE	0xA8	/* Enable mouse interface */
#define KBD_CCMD_TEST_MOUSE	0xA9	/* Mouse interface test */
#define KBD_CCMD_SELF_TEST	0xAA	/* Controller self test */
#define KBD_CCMD_KBD_TEST	0xAB	/* Keyboard interface test */
#define KBD_CCMD_KBD_DISABLE	0xAD	/* Keyboard interface disable */
#define KBD_CCMD_KBD_ENABLE	0xAE	/* Keyboard interface enable */
wdenk's avatar
wdenk committed
60
61
#define KBD_CCMD_WRITE_AUX_OBUF	0xD3    /* Write to output buffer as if
					   initiated by the auxiliary device */
Wolfgang Denk's avatar
Wolfgang Denk committed
62
#define KBD_CCMD_WRITE_MOUSE	0xD4	/* Write the following byte to the mouse */
wdenk's avatar
wdenk committed
63
64
65
66
67

/*
 *	Keyboard Commands
 */

Wolfgang Denk's avatar
Wolfgang Denk committed
68
69
70
71
72
#define KBD_CMD_SET_LEDS	0xED	/* Set keyboard leds */
#define KBD_CMD_SET_RATE	0xF3	/* Set typematic rate */
#define KBD_CMD_ENABLE		0xF4	/* Enable scanning */
#define KBD_CMD_DISABLE		0xF5	/* Disable scanning */
#define KBD_CMD_RESET		0xFF	/* Reset */
wdenk's avatar
wdenk committed
73
74
75
76
77

/*
 *	Keyboard Replies
 */

Wolfgang Denk's avatar
Wolfgang Denk committed
78
79
80
#define KBD_REPLY_POR		0xAA	/* Power on reset */
#define KBD_REPLY_ACK		0xFA	/* Command ACK */
#define KBD_REPLY_RESEND	0xFE	/* Command NACK, send the cmd again */
wdenk's avatar
wdenk committed
81
82
83
84
85

/*
 *	Status Register Bits
 */

Wolfgang Denk's avatar
Wolfgang Denk committed
86
87
88
89
90
91
92
93
#define KBD_STAT_OBF		0x01	/* Keyboard output buffer full */
#define KBD_STAT_IBF		0x02	/* Keyboard input buffer full */
#define KBD_STAT_SELFTEST	0x04	/* Self test successful */
#define KBD_STAT_CMD		0x08	/* Last write was a command write (0=data) */
#define KBD_STAT_UNLOCKED	0x10	/* Zero if keyboard locked */
#define KBD_STAT_MOUSE_OBF	0x20	/* Mouse output buffer full */
#define KBD_STAT_GTO		0x40	/* General receive/xmit timeout */
#define KBD_STAT_PERR		0x80	/* Parity error */
wdenk's avatar
wdenk committed
94
95
96
97
98
99
100

#define AUX_STAT_OBF (KBD_STAT_OBF | KBD_STAT_MOUSE_OBF)

/*
 *	Controller Mode Register Bits
 */

Wolfgang Denk's avatar
Wolfgang Denk committed
101
102
103
104
105
#define KBD_MODE_KBD_INT	0x01	/* Keyboard data generate IRQ1 */
#define KBD_MODE_MOUSE_INT	0x02	/* Mouse data generate IRQ12 */
#define KBD_MODE_SYS		0x04	/* The system flag (?) */
#define KBD_MODE_NO_KEYLOCK	0x08	/* The keylock doesn't affect the keyboard if set */
#define KBD_MODE_DISABLE_KBD	0x10	/* Disable keyboard interface */
wdenk's avatar
wdenk committed
106
#define KBD_MODE_DISABLE_MOUSE	0x20	/* Disable mouse interface */
Wolfgang Denk's avatar
Wolfgang Denk committed
107
108
#define KBD_MODE_KCC		0x40	/* Scan code conversion to PC format */
#define KBD_MODE_RFU		0x80
wdenk's avatar
wdenk committed
109
110


Wolfgang Denk's avatar
Wolfgang Denk committed
111
#define KDB_DATA_PORT		0x60
wdenk's avatar
wdenk committed
112
113
#define KDB_COMMAND_PORT	0x64

Wolfgang Denk's avatar
Wolfgang Denk committed
114
115
116
#define	LED_SCR			0x01	/* scroll lock led */
#define	LED_CAP			0x04	/* caps lock led */
#define	LED_NUM			0x02	/* num lock led */
wdenk's avatar
wdenk committed
117

Wolfgang Denk's avatar
Wolfgang Denk committed
118
#define	KBD_BUFFER_LEN		0x20	/* size of the keyboardbuffer */
wdenk's avatar
wdenk committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180


static volatile char kbd_buffer[KBD_BUFFER_LEN];
static volatile int in_pointer = 0;
static volatile int out_pointer = 0;


static unsigned char num_lock = 0;
static unsigned char caps_lock = 0;
static unsigned char scroll_lock = 0;
static unsigned char shift = 0;
static unsigned char ctrl = 0;
static unsigned char alt = 0;
static unsigned char e0 = 0;
static unsigned char leds = 0;

#define DEVNAME "kbd"

/* Simple translation table for the keys */

static unsigned char kbd_plain_xlate[] = {
	0xff,0x1b, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=','\b','\t',	/* 0x00 - 0x0f */
	 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']','\r',0xff, 'a', 's',	/* 0x10 - 0x1f */
	 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';','\'', '`',0xff,'\\', 'z', 'x', 'c', 'v',	/* 0x20 - 0x2f */
	 'b', 'n', 'm', ',', '.', '/',0xff,0xff,0xff, ' ',0xff,0xff,0xff,0xff,0xff,0xff,	/* 0x30 - 0x3f */
	0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1',	/* 0x40 - 0x4f */
	 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,  /* 0x50 - 0x5F */
	'\r',0xff,0xff
	};

static unsigned char kbd_shift_xlate[] = {
	0xff,0x1b, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+','\b','\t',	/* 0x00 - 0x0f */
	 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}','\r',0xff, 'A', 'S',	/* 0x10 - 0x1f */
	 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~',0xff, '|', 'Z', 'X', 'C', 'V',	/* 0x20 - 0x2f */
	 'B', 'N', 'M', '<', '>', '?',0xff,0xff,0xff, ' ',0xff,0xff,0xff,0xff,0xff,0xff,	/* 0x30 - 0x3f */
	0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1',	/* 0x40 - 0x4f */
	 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,  /* 0x50 - 0x5F */
	'\r',0xff,0xff
	};

static unsigned char kbd_ctrl_xlate[] = {
	0xff,0x1b, '1',0x00, '3', '4', '5',0x1E, '7', '8', '9', '0',0x1F, '=','\b','\t',	/* 0x00 - 0x0f */
	0x11,0x17,0x05,0x12,0x14,0x18,0x15,0x09,0x0f,0x10,0x1b,0x1d,'\n',0xff,0x01,0x13,	/* 0x10 - 0x1f */
	0x04,0x06,0x08,0x09,0x0a,0x0b,0x0c, ';','\'', '~',0x00,0x1c,0x1a,0x18,0x03,0x16,	/* 0x20 - 0x2f */
	0x02,0x0e,0x0d, '<', '>', '?',0xff,0xff,0xff,0x00,0xff,0xff,0xff,0xff,0xff,0xff,	/* 0x30 - 0x3f */
	0xff,0xff,0xff,0xff,0xff,0xff,0xff, '7', '8', '9', '-', '4', '5', '6', '+', '1',	/* 0x40 - 0x4f */
	 '2', '3', '0', '.',0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,  /* 0x50 - 0x5F */
	'\r',0xff,0xff
	};

/******************************************************************
 * Init
 ******************************************************************/
int isa_kbd_init(void)
{
	char* result;
	result=kbd_initialize();
	if(result==NULL) {
		PRINTF("AT Keyboard initialized\n");
		irq_install_handler(25, (interrupt_handler_t *)handle_isa_int, NULL);
		isa_irq_install_handler(KBD_INTERRUPT, (interrupt_handler_t *)kbd_interrupt, NULL);
		return (1);
Wolfgang Denk's avatar
Wolfgang Denk committed
181
	} else {
wdenk's avatar
wdenk committed
182
183
184
185
186
		printf("%s\n",result);
		return (-1);
	}
}

187
#ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
wdenk's avatar
wdenk committed
188
189
190
191
192
193
194
195
196
197
198
extern int overwrite_console (void);
#else
int overwrite_console (void)
{
	return (0);
}
#endif

int drv_isa_kbd_init (void)
{
	int error;
199
	struct stdio_dev kbddev ;
wdenk's avatar
wdenk committed
200
201
202
203
	char *stdinname  = getenv ("stdin");

	if(isa_kbd_init()==-1)
		return -1;
Wolfgang Denk's avatar
Wolfgang Denk committed
204
205
	memset (&kbddev, 0, sizeof(kbddev));
	strcpy(kbddev.name, DEVNAME);
Bin Meng's avatar
Bin Meng committed
206
	kbddev.flags =  DEV_FLAGS_INPUT;
wdenk's avatar
wdenk committed
207
208
209
	kbddev.getc = kbd_getc ;
	kbddev.tstc = kbd_testc ;

210
	error = stdio_register (&kbddev);
wdenk's avatar
wdenk committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
	if(error==0) {
		/* check if this is the standard input device */
		if(strcmp(stdinname,DEVNAME)==0) {
			/* reassign the console */
			if(overwrite_console()) {
				return 1;
			}
			error=console_assign(stdin,DEVNAME);
			if(error==0)
				return 1;
			else
				return error;
		}
		return 1;
	}
	return error;
}

/******************************************************************
 * Queue handling
 ******************************************************************/
/* puts character in the queue and sets up the in and out pointer */
void kbd_put_queue(char data)
{
	if((in_pointer+1)==KBD_BUFFER_LEN) {
		if(out_pointer==0) {
			return; /* buffer full */
		} else{
			in_pointer=0;
		}
	} else {
		if((in_pointer+1)==out_pointer)
			return; /* buffer full */
		in_pointer++;
	}
	kbd_buffer[in_pointer]=data;
	return;
}

/* test if a character is in the queue */
251
int kbd_testc(struct stdio_dev *dev)
wdenk's avatar
wdenk committed
252
253
254
255
256
257
258
{
	if(in_pointer==out_pointer)
		return(0); /* no data */
	else
		return(1);
}
/* gets the character from the queue */
259
int kbd_getc(struct stdio_dev *dev)
wdenk's avatar
wdenk committed
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
{
	char c;
	while(in_pointer==out_pointer);
	if((out_pointer+1)==KBD_BUFFER_LEN)
		out_pointer=0;
	else
		out_pointer++;
	c=kbd_buffer[out_pointer];
	return (int)c;

}


/* set LEDs */

void kbd_set_leds(void)
{
	if(caps_lock==0)
		leds&=~LED_CAP; /* switch caps_lock off */
	else
		leds|=LED_CAP; /* switch on LED */
	if(num_lock==0)
		leds&=~LED_NUM; /* switch LED off */
	else
		leds|=LED_NUM;  /* switch on LED */
	if(scroll_lock==0)
		leds&=~LED_SCR; /* switch LED off */
	else
		leds|=LED_SCR; /* switch on LED */
	kbd_send_data(KBD_CMD_SET_LEDS);
	kbd_send_data(leds);
}


Wolfgang Denk's avatar
Wolfgang Denk committed
294
void handle_keyboard_event (unsigned char scancode)
wdenk's avatar
wdenk committed
295
296
297
298
{
	unsigned char keycode;

	/*  Convert scancode to keycode */
Wolfgang Denk's avatar
Wolfgang Denk committed
299
300
301
	PRINTF ("scancode %x\n", scancode);
	if (scancode == 0xe0) {
		e0 = 1;		/* special charakters */
wdenk's avatar
wdenk committed
302
303
		return;
	}
Wolfgang Denk's avatar
Wolfgang Denk committed
304
305
306
307
308
309
310
	if (e0 == 1) {
		e0 = 0;		/* delete flag */
		if (!(((scancode & 0x7F) == 0x38) ||	/* the right ctrl key */
		      ((scancode & 0x7F) == 0x1D) ||	/* the right alt key */
		      ((scancode & 0x7F) == 0x35) ||	/* the right '/' key */
		      ((scancode & 0x7F) == 0x1C)))
			/* the right enter key */
wdenk's avatar
wdenk committed
311
312
313
314
			/* we swallow unknown e0 codes */
			return;
	}
	/* special cntrl keys */
Wolfgang Denk's avatar
Wolfgang Denk committed
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
	switch (scancode) {
	case 0x2A:
	case 0x36:		/* shift pressed */
		shift = 1;
		return;		/* do nothing else */
	case 0xAA:
	case 0xB6:		/* shift released */
		shift = 0;
		return;		/* do nothing else */
	case 0x38:		/* alt pressed */
		alt = 1;
		return;		/* do nothing else */
	case 0xB8:		/* alt released */
		alt = 0;
		return;		/* do nothing else */
	case 0x1d:		/* ctrl pressed */
		ctrl = 1;
		return;		/* do nothing else */
	case 0x9d:		/* ctrl released */
		ctrl = 0;
		return;		/* do nothing else */
	case 0x46:		/* scrollock pressed */
		scroll_lock = ~scroll_lock;
		kbd_set_leds ();
		return;		/* do nothing else */
	case 0x3A:		/* capslock pressed */
		caps_lock = ~caps_lock;
		kbd_set_leds ();
		return;
	case 0x45:		/* numlock pressed */
		num_lock = ~num_lock;
		kbd_set_leds ();
		return;
	case 0xC6:		/* scroll lock released */
	case 0xC5:		/* num lock released */
	case 0xBA:		/* caps lock released */
		return;		/* just swallow */
wdenk's avatar
wdenk committed
352
	}
Wolfgang Denk's avatar
Wolfgang Denk committed
353
	if ((scancode & 0x80) == 0x80)	/* key released */
wdenk's avatar
wdenk committed
354
355
		return;
	/* now, decide which table we need */
Wolfgang Denk's avatar
Wolfgang Denk committed
356
357
358
	if (scancode > (sizeof (kbd_plain_xlate) / sizeof (kbd_plain_xlate[0]))) {	/* scancode not in list */
		PRINTF ("unkown scancode %X\n", scancode);
		return;		/* swallow it */
wdenk's avatar
wdenk committed
359
360
	}
	/* setup plain code first */
Wolfgang Denk's avatar
Wolfgang Denk committed
361
362
363
364
365
	keycode = kbd_plain_xlate[scancode];
	if (caps_lock == 1) {	/* caps_lock is pressed, overwrite plain code */
		if (scancode > (sizeof (kbd_shift_xlate) / sizeof (kbd_shift_xlate[0]))) {	/* scancode not in list */
			PRINTF ("unkown caps-locked scancode %X\n", scancode);
			return;	/* swallow it */
wdenk's avatar
wdenk committed
366
		}
Wolfgang Denk's avatar
Wolfgang Denk committed
367
368
369
		keycode = kbd_shift_xlate[scancode];
		if (keycode < 'A') {	/* we only want the alphas capital */
			keycode = kbd_plain_xlate[scancode];
wdenk's avatar
wdenk committed
370
371
		}
	}
Wolfgang Denk's avatar
Wolfgang Denk committed
372
373
374
375
	if (shift == 1) {	/* shift overwrites caps_lock */
		if (scancode > (sizeof (kbd_shift_xlate) / sizeof (kbd_shift_xlate[0]))) {	/* scancode not in list */
			PRINTF ("unkown shifted scancode %X\n", scancode);
			return;	/* swallow it */
wdenk's avatar
wdenk committed
376
		}
Wolfgang Denk's avatar
Wolfgang Denk committed
377
		keycode = kbd_shift_xlate[scancode];
wdenk's avatar
wdenk committed
378
	}
Wolfgang Denk's avatar
Wolfgang Denk committed
379
380
381
382
	if (ctrl == 1) {	/* ctrl overwrites caps_lock and shift */
		if (scancode > (sizeof (kbd_ctrl_xlate) / sizeof (kbd_ctrl_xlate[0]))) {	/* scancode not in list */
			PRINTF ("unkown ctrl scancode %X\n", scancode);
			return;	/* swallow it */
wdenk's avatar
wdenk committed
383
		}
Wolfgang Denk's avatar
Wolfgang Denk committed
384
		keycode = kbd_ctrl_xlate[scancode];
wdenk's avatar
wdenk committed
385
386
	}
	/* check if valid keycode */
Wolfgang Denk's avatar
Wolfgang Denk committed
387
388
389
	if (keycode == 0xff) {
		PRINTF ("unkown scancode %X\n", scancode);
		return;		/* swallow unknown codes */
wdenk's avatar
wdenk committed
390
391
	}

Wolfgang Denk's avatar
Wolfgang Denk committed
392
393
	kbd_put_queue (keycode);
	PRINTF ("%x\n", keycode);
wdenk's avatar
wdenk committed
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
}

/*
 * This reads the keyboard status port, and does the
 * appropriate action.
 *
 */
unsigned char handle_kbd_event(void)
{
	unsigned char status = kbd_read_status();
	unsigned int work = 10000;

	while ((--work > 0) && (status & KBD_STAT_OBF)) {
		unsigned char scancode;

		scancode = kbd_read_input();

		/* Error bytes must be ignored to make the
		   Synaptics touchpads compaq use work */
		/* Ignore error bytes */
		if (!(status & (KBD_STAT_GTO | KBD_STAT_PERR)))
		{
			if (status & KBD_STAT_MOUSE_OBF)
				; /* not supported: handle_mouse_event(scancode); */
			else
				handle_keyboard_event(scancode);
		}
		status = kbd_read_status();
	}
	if (!work)
		PRINTF("pc_keyb: controller jammed (0x%02X).\n", status);
	return status;
}


/******************************************************************************
 * Lowlevel Part of keyboard section
 */
unsigned char kbd_read_status(void)
{
434
	return(in8(CONFIG_SYS_ISA_IO_BASE_ADDRESS + KDB_COMMAND_PORT));
wdenk's avatar
wdenk committed
435
436
437
438
}

unsigned char kbd_read_input(void)
{
439
	return(in8(CONFIG_SYS_ISA_IO_BASE_ADDRESS + KDB_DATA_PORT));
wdenk's avatar
wdenk committed
440
441
442
443
}

void kbd_write_command(unsigned char cmd)
{
444
	out8(CONFIG_SYS_ISA_IO_BASE_ADDRESS + KDB_COMMAND_PORT,cmd);
wdenk's avatar
wdenk committed
445
446
447
448
}

void kbd_write_output(unsigned char data)
{
449
	out8(CONFIG_SYS_ISA_IO_BASE_ADDRESS + KDB_DATA_PORT, data);
wdenk's avatar
wdenk committed
450
451
452
453
454
455
456
}

int kbd_read_data(void)
{
	int val;
	unsigned char status;

457
	val = -1;
wdenk's avatar
wdenk committed
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
	status = kbd_read_status();
	if (status & KBD_STAT_OBF) {
		val = kbd_read_input();
		if (status & (KBD_STAT_GTO | KBD_STAT_PERR))
			val = -2;
	}
	return val;
}

int kbd_wait_for_input(void)
{
	unsigned long timeout;
	int val;

	timeout = KBD_TIMEOUT;
	val=kbd_read_data();
	while(val < 0)
	{
		if(timeout--==0)
			return -1;
		udelay(1000);
		val=kbd_read_data();
	}
	return val;
}


int kb_wait(void)
{
	unsigned long timeout = KBC_TIMEOUT * 10;

	do {
		unsigned char status = handle_kbd_event();
		if (!(status & KBD_STAT_IBF))
			return 0; /* ok */
		udelay(1000);
		timeout--;
	} while (timeout);
	return 1;
}

void kbd_write_command_w(int data)
{
	if(kb_wait())
		PRINTF("timeout in kbd_write_command_w\n");
	kbd_write_command(data);
}

void kbd_write_output_w(int data)
{
	if(kb_wait())
		PRINTF("timeout in kbd_write_output_w\n");
	kbd_write_output(data);
}

void kbd_send_data(unsigned char data)
{
	unsigned char status;
	disable_8259A_irq(1); /* disable interrupt */
	kbd_write_output_w(data);
	status = kbd_wait_for_input();
	if (status == KBD_REPLY_ACK)
		enable_8259A_irq(1); /* enable interrupt */
}


char * kbd_initialize(void)
{
	int status;

	in_pointer = 0; /* delete in Buffer */
	out_pointer = 0;
	/*
	 * Test the keyboard interface.
	 * This seems to be the only way to get it going.
	 * If the test is successful a x55 is placed in the input buffer.
	 */
	kbd_write_command_w(KBD_CCMD_SELF_TEST);
	if (kbd_wait_for_input() != 0x55)
		return "Kbd:   failed self test";
	/*
	 * Perform a keyboard interface test.  This causes the controller
	 * to test the keyboard clock and data lines.  The results of the
	 * test are placed in the input buffer.
	 */
	kbd_write_command_w(KBD_CCMD_KBD_TEST);
	if (kbd_wait_for_input() != 0x00)
		return "Kbd:   interface failed self test";
	/*
	 * Enable the keyboard by allowing the keyboard clock to run.
	 */
	kbd_write_command_w(KBD_CCMD_KBD_ENABLE);
	status = kbd_wait_for_input();
	/*
	 * Reset keyboard. If the read times out
	 * then the assumption is that no keyboard is
	 * plugged into the machine.
	 * This defaults the keyboard to scan-code set 2.
	 *
	 * Set up to try again if the keyboard asks for RESEND.
	 */
	do {
		kbd_write_output_w(KBD_CMD_RESET);
		status = kbd_wait_for_input();
		if (status == KBD_REPLY_ACK)
			break;
Wolfgang Denk's avatar
Wolfgang Denk committed
564
		if (status != KBD_REPLY_RESEND) {
wdenk's avatar
wdenk committed
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
			PRINTF("status: %X\n",status);
			return "Kbd:   reset failed, no ACK";
		}
	} while (1);
	if (kbd_wait_for_input() != KBD_REPLY_POR)
		return "Kbd:   reset failed, no POR";

	/*
	 * Set keyboard controller mode. During this, the keyboard should be
	 * in the disabled state.
	 *
	 * Set up to try again if the keyboard asks for RESEND.
	 */
	do {
		kbd_write_output_w(KBD_CMD_DISABLE);
		status = kbd_wait_for_input();
		if (status == KBD_REPLY_ACK)
			break;
		if (status != KBD_REPLY_RESEND)
			return "Kbd:   disable keyboard: no ACK";
	} while (1);

	kbd_write_command_w(KBD_CCMD_WRITE_MODE);
	kbd_write_output_w(KBD_MODE_KBD_INT
			      | KBD_MODE_SYS
			      | KBD_MODE_DISABLE_MOUSE
			      | KBD_MODE_KCC);

Wolfgang Denk's avatar
Wolfgang Denk committed
593
	/* AMCC powerpc portables need this to use scan-code set 1 -- Cort */
wdenk's avatar
wdenk committed
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
	kbd_write_command_w(KBD_CCMD_READ_MODE);
	if (!(kbd_wait_for_input() & KBD_MODE_KCC)) {
		/*
		 * If the controller does not support conversion,
		 * Set the keyboard to scan-code set 1.
		 */
		kbd_write_output_w(0xF0);
		kbd_wait_for_input();
		kbd_write_output_w(0x01);
		kbd_wait_for_input();
	}
	kbd_write_output_w(KBD_CMD_ENABLE);
	if (kbd_wait_for_input() != KBD_REPLY_ACK)
		return "Kbd:   enable keyboard: no ACK";

	/*
	 * Finally, set the typematic rate to maximum.
	 */
	kbd_write_output_w(KBD_CMD_SET_RATE);
	if (kbd_wait_for_input() != KBD_REPLY_ACK)
		return "Kbd:   Set rate: no ACK";
	kbd_write_output_w(0x00);
	if (kbd_wait_for_input() != KBD_REPLY_ACK)
		return "Kbd:   Set rate: no ACK";
	return NULL;
}

void kbd_interrupt(void)
{
	handle_kbd_event();
}