blob: b0a9620232dcc203e166264ea5d2724469a5c0f8 [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 *
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>
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +020031#include <stdio_dev.h>
wdenkaffae2b2002-08-17 09:36:01 +000032#include "isa.h"
33#include "kbd.h"
34
35
36unsigned char kbd_read_status(void);
37unsigned char kbd_read_input(void);
38void kbd_send_data(unsigned char data);
39void disable_8259A_irq(unsigned int irq);
40void 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
Wolfgang Denk53677ef2008-05-20 16:00:29 +020056#define KBD_STAT_CD 0x08
wdenkaffae2b2002-08-17 09:36:01 +000057#define KBD_STAT_LOCK 0x10
58#define KBD_STAT_MOBF 0x20
Wolfgang Denk53677ef2008-05-20 16:00:29 +020059#define KBD_STAT_TI_OUT 0x40
60#define KBD_STAT_PARERR 0x80
wdenkaffae2b2002-08-17 09:36:01 +000061
Wolfgang Denk53677ef2008-05-20 16:00:29 +020062#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 */
wdenkaffae2b2002-08-17 09:36:01 +000065/*
66 * Keyboard Controller Commands
67 */
68
Wolfgang Denk53677ef2008-05-20 16:00:29 +020069#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 */
wdenkaffae2b2002-08-17 09:36:01 +000072#define KBD_CCMD_MOUSE_DISABLE 0xA7 /* Disable mouse interface */
Wolfgang Denk53677ef2008-05-20 16:00:29 +020073#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 */
wdenkaffae2b2002-08-17 09:36:01 +000079#define KBD_CCMD_WRITE_AUX_OBUF 0xD3 /* Write to output buffer as if
80 initiated by the auxiliary device */
Wolfgang Denk53677ef2008-05-20 16:00:29 +020081#define KBD_CCMD_WRITE_MOUSE 0xD4 /* Write the following byte to the mouse */
wdenkaffae2b2002-08-17 09:36:01 +000082
83/*
84 * Keyboard Commands
85 */
86
Wolfgang Denk53677ef2008-05-20 16:00:29 +020087#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 */
wdenkaffae2b2002-08-17 09:36:01 +000092
93/*
94 * Keyboard Replies
95 */
96
Wolfgang Denk53677ef2008-05-20 16:00:29 +020097#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 */
wdenkaffae2b2002-08-17 09:36:01 +0000100
101/*
102 * Status Register Bits
103 */
104
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200105#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 */
wdenkaffae2b2002-08-17 09:36:01 +0000113
114#define AUX_STAT_OBF (KBD_STAT_OBF | KBD_STAT_MOUSE_OBF)
115
116/*
117 * Controller Mode Register Bits
118 */
119
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200120#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 */
wdenkaffae2b2002-08-17 09:36:01 +0000125#define KBD_MODE_DISABLE_MOUSE 0x20 /* Disable mouse interface */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200126#define KBD_MODE_KCC 0x40 /* Scan code conversion to PC format */
127#define KBD_MODE_RFU 0x80
wdenkaffae2b2002-08-17 09:36:01 +0000128
129
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200130#define KDB_DATA_PORT 0x60
wdenkaffae2b2002-08-17 09:36:01 +0000131#define KDB_COMMAND_PORT 0x64
132
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200133#define LED_SCR 0x01 /* scroll lock led */
134#define LED_CAP 0x04 /* caps lock led */
135#define LED_NUM 0x02 /* num lock led */
wdenkaffae2b2002-08-17 09:36:01 +0000136
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200137#define KBD_BUFFER_LEN 0x20 /* size of the keyboardbuffer */
wdenkaffae2b2002-08-17 09:36:01 +0000138
139
wdenkaffae2b2002-08-17 09:36:01 +0000140static volatile char kbd_buffer[KBD_BUFFER_LEN];
141static volatile int in_pointer = 0;
142static volatile int out_pointer = 0;
143
144
145static unsigned char num_lock = 0;
146static unsigned char caps_lock = 0;
147static unsigned char scroll_lock = 0;
148static unsigned char shift = 0;
149static unsigned char ctrl = 0;
150static unsigned char alt = 0;
151static unsigned char e0 = 0;
152static unsigned char leds = 0;
153
154#define DEVNAME "kbd"
155
156/* Simple translation table for the keys */
157
158static 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
168static 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
178static 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 ******************************************************************/
191int 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);
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200200 } else {
wdenkaffae2b2002-08-17 09:36:01 +0000201 printf("%s\n",result);
202 return (-1);
203 }
204}
205
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200206#ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
wdenkaffae2b2002-08-17 09:36:01 +0000207extern int overwrite_console (void);
208#else
209int overwrite_console (void)
210{
211 return (0);
212}
213#endif
214
215int drv_isa_kbd_init (void)
216{
217 int error;
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200218 struct stdio_dev kbddev ;
wdenkaffae2b2002-08-17 09:36:01 +0000219 char *stdinname = getenv ("stdin");
220
221 if(isa_kbd_init()==-1)
222 return -1;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200223 memset (&kbddev, 0, sizeof(kbddev));
224 strcpy(kbddev.name, DEVNAME);
225 kbddev.flags = DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
226 kbddev.putc = NULL ;
wdenkaffae2b2002-08-17 09:36:01 +0000227 kbddev.puts = NULL ;
228 kbddev.getc = kbd_getc ;
229 kbddev.tstc = kbd_testc ;
230
Jean-Christophe PLAGNIOL-VILLARD52cb4d42009-05-16 12:14:54 +0200231 error = stdio_register (&kbddev);
wdenkaffae2b2002-08-17 09:36:01 +0000232 if(error==0) {
233 /* check if this is the standard input device */
234 if(strcmp(stdinname,DEVNAME)==0) {
235 /* reassign the console */
236 if(overwrite_console()) {
237 return 1;
238 }
239 error=console_assign(stdin,DEVNAME);
240 if(error==0)
241 return 1;
242 else
243 return error;
244 }
245 return 1;
246 }
247 return error;
248}
249
250/******************************************************************
251 * Queue handling
252 ******************************************************************/
253/* puts character in the queue and sets up the in and out pointer */
254void kbd_put_queue(char data)
255{
256 if((in_pointer+1)==KBD_BUFFER_LEN) {
257 if(out_pointer==0) {
258 return; /* buffer full */
259 } else{
260 in_pointer=0;
261 }
262 } else {
263 if((in_pointer+1)==out_pointer)
264 return; /* buffer full */
265 in_pointer++;
266 }
267 kbd_buffer[in_pointer]=data;
268 return;
269}
270
271/* test if a character is in the queue */
272int kbd_testc(void)
273{
274 if(in_pointer==out_pointer)
275 return(0); /* no data */
276 else
277 return(1);
278}
279/* gets the character from the queue */
280int kbd_getc(void)
281{
282 char c;
283 while(in_pointer==out_pointer);
284 if((out_pointer+1)==KBD_BUFFER_LEN)
285 out_pointer=0;
286 else
287 out_pointer++;
288 c=kbd_buffer[out_pointer];
289 return (int)c;
290
291}
292
293
294/* set LEDs */
295
296void kbd_set_leds(void)
297{
298 if(caps_lock==0)
299 leds&=~LED_CAP; /* switch caps_lock off */
300 else
301 leds|=LED_CAP; /* switch on LED */
302 if(num_lock==0)
303 leds&=~LED_NUM; /* switch LED off */
304 else
305 leds|=LED_NUM; /* switch on LED */
306 if(scroll_lock==0)
307 leds&=~LED_SCR; /* switch LED off */
308 else
309 leds|=LED_SCR; /* switch on LED */
310 kbd_send_data(KBD_CMD_SET_LEDS);
311 kbd_send_data(leds);
312}
313
314
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200315void handle_keyboard_event (unsigned char scancode)
wdenkaffae2b2002-08-17 09:36:01 +0000316{
317 unsigned char keycode;
318
319 /* Convert scancode to keycode */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200320 PRINTF ("scancode %x\n", scancode);
321 if (scancode == 0xe0) {
322 e0 = 1; /* special charakters */
wdenkaffae2b2002-08-17 09:36:01 +0000323 return;
324 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200325 if (e0 == 1) {
326 e0 = 0; /* delete flag */
327 if (!(((scancode & 0x7F) == 0x38) || /* the right ctrl key */
328 ((scancode & 0x7F) == 0x1D) || /* the right alt key */
329 ((scancode & 0x7F) == 0x35) || /* the right '/' key */
330 ((scancode & 0x7F) == 0x1C)))
331 /* the right enter key */
wdenkaffae2b2002-08-17 09:36:01 +0000332 /* we swallow unknown e0 codes */
333 return;
334 }
335 /* special cntrl keys */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200336 switch (scancode) {
337 case 0x2A:
338 case 0x36: /* shift pressed */
339 shift = 1;
340 return; /* do nothing else */
341 case 0xAA:
342 case 0xB6: /* shift released */
343 shift = 0;
344 return; /* do nothing else */
345 case 0x38: /* alt pressed */
346 alt = 1;
347 return; /* do nothing else */
348 case 0xB8: /* alt released */
349 alt = 0;
350 return; /* do nothing else */
351 case 0x1d: /* ctrl pressed */
352 ctrl = 1;
353 return; /* do nothing else */
354 case 0x9d: /* ctrl released */
355 ctrl = 0;
356 return; /* do nothing else */
357 case 0x46: /* scrollock pressed */
358 scroll_lock = ~scroll_lock;
359 kbd_set_leds ();
360 return; /* do nothing else */
361 case 0x3A: /* capslock pressed */
362 caps_lock = ~caps_lock;
363 kbd_set_leds ();
364 return;
365 case 0x45: /* numlock pressed */
366 num_lock = ~num_lock;
367 kbd_set_leds ();
368 return;
369 case 0xC6: /* scroll lock released */
370 case 0xC5: /* num lock released */
371 case 0xBA: /* caps lock released */
372 return; /* just swallow */
wdenkaffae2b2002-08-17 09:36:01 +0000373 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200374 if ((scancode & 0x80) == 0x80) /* key released */
wdenkaffae2b2002-08-17 09:36:01 +0000375 return;
376 /* now, decide which table we need */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200377 if (scancode > (sizeof (kbd_plain_xlate) / sizeof (kbd_plain_xlate[0]))) { /* scancode not in list */
378 PRINTF ("unkown scancode %X\n", scancode);
379 return; /* swallow it */
wdenkaffae2b2002-08-17 09:36:01 +0000380 }
381 /* setup plain code first */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200382 keycode = kbd_plain_xlate[scancode];
383 if (caps_lock == 1) { /* caps_lock is pressed, overwrite plain code */
384 if (scancode > (sizeof (kbd_shift_xlate) / sizeof (kbd_shift_xlate[0]))) { /* scancode not in list */
385 PRINTF ("unkown caps-locked scancode %X\n", scancode);
386 return; /* swallow it */
wdenkaffae2b2002-08-17 09:36:01 +0000387 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200388 keycode = kbd_shift_xlate[scancode];
389 if (keycode < 'A') { /* we only want the alphas capital */
390 keycode = kbd_plain_xlate[scancode];
wdenkaffae2b2002-08-17 09:36:01 +0000391 }
392 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200393 if (shift == 1) { /* shift overwrites caps_lock */
394 if (scancode > (sizeof (kbd_shift_xlate) / sizeof (kbd_shift_xlate[0]))) { /* scancode not in list */
395 PRINTF ("unkown shifted scancode %X\n", scancode);
396 return; /* swallow it */
wdenkaffae2b2002-08-17 09:36:01 +0000397 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200398 keycode = kbd_shift_xlate[scancode];
wdenkaffae2b2002-08-17 09:36:01 +0000399 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200400 if (ctrl == 1) { /* ctrl overwrites caps_lock and shift */
401 if (scancode > (sizeof (kbd_ctrl_xlate) / sizeof (kbd_ctrl_xlate[0]))) { /* scancode not in list */
402 PRINTF ("unkown ctrl scancode %X\n", scancode);
403 return; /* swallow it */
wdenkaffae2b2002-08-17 09:36:01 +0000404 }
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200405 keycode = kbd_ctrl_xlate[scancode];
wdenkaffae2b2002-08-17 09:36:01 +0000406 }
407 /* check if valid keycode */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200408 if (keycode == 0xff) {
409 PRINTF ("unkown scancode %X\n", scancode);
410 return; /* swallow unknown codes */
wdenkaffae2b2002-08-17 09:36:01 +0000411 }
412
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200413 kbd_put_queue (keycode);
414 PRINTF ("%x\n", keycode);
wdenkaffae2b2002-08-17 09:36:01 +0000415}
416
417/*
418 * This reads the keyboard status port, and does the
419 * appropriate action.
420 *
421 */
422unsigned char handle_kbd_event(void)
423{
424 unsigned char status = kbd_read_status();
425 unsigned int work = 10000;
426
427 while ((--work > 0) && (status & KBD_STAT_OBF)) {
428 unsigned char scancode;
429
430 scancode = kbd_read_input();
431
432 /* Error bytes must be ignored to make the
433 Synaptics touchpads compaq use work */
434 /* Ignore error bytes */
435 if (!(status & (KBD_STAT_GTO | KBD_STAT_PERR)))
436 {
437 if (status & KBD_STAT_MOUSE_OBF)
438 ; /* not supported: handle_mouse_event(scancode); */
439 else
440 handle_keyboard_event(scancode);
441 }
442 status = kbd_read_status();
443 }
444 if (!work)
445 PRINTF("pc_keyb: controller jammed (0x%02X).\n", status);
446 return status;
447}
448
449
wdenkaffae2b2002-08-17 09:36:01 +0000450/******************************************************************************
451 * Lowlevel Part of keyboard section
452 */
453unsigned char kbd_read_status(void)
454{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200455 return(in8(CONFIG_SYS_ISA_IO_BASE_ADDRESS + KDB_COMMAND_PORT));
wdenkaffae2b2002-08-17 09:36:01 +0000456}
457
458unsigned char kbd_read_input(void)
459{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200460 return(in8(CONFIG_SYS_ISA_IO_BASE_ADDRESS + KDB_DATA_PORT));
wdenkaffae2b2002-08-17 09:36:01 +0000461}
462
463void kbd_write_command(unsigned char cmd)
464{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200465 out8(CONFIG_SYS_ISA_IO_BASE_ADDRESS + KDB_COMMAND_PORT,cmd);
wdenkaffae2b2002-08-17 09:36:01 +0000466}
467
468void kbd_write_output(unsigned char data)
469{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200470 out8(CONFIG_SYS_ISA_IO_BASE_ADDRESS + KDB_DATA_PORT, data);
wdenkaffae2b2002-08-17 09:36:01 +0000471}
472
473int kbd_read_data(void)
474{
475 int val;
476 unsigned char status;
477
Wolfgang Denkd0ff51b2008-07-14 15:19:07 +0200478 val = -1;
wdenkaffae2b2002-08-17 09:36:01 +0000479 status = kbd_read_status();
480 if (status & KBD_STAT_OBF) {
481 val = kbd_read_input();
482 if (status & (KBD_STAT_GTO | KBD_STAT_PERR))
483 val = -2;
484 }
485 return val;
486}
487
488int kbd_wait_for_input(void)
489{
490 unsigned long timeout;
491 int val;
492
493 timeout = KBD_TIMEOUT;
494 val=kbd_read_data();
495 while(val < 0)
496 {
497 if(timeout--==0)
498 return -1;
499 udelay(1000);
500 val=kbd_read_data();
501 }
502 return val;
503}
504
505
506int kb_wait(void)
507{
508 unsigned long timeout = KBC_TIMEOUT * 10;
509
510 do {
511 unsigned char status = handle_kbd_event();
512 if (!(status & KBD_STAT_IBF))
513 return 0; /* ok */
514 udelay(1000);
515 timeout--;
516 } while (timeout);
517 return 1;
518}
519
520void kbd_write_command_w(int data)
521{
522 if(kb_wait())
523 PRINTF("timeout in kbd_write_command_w\n");
524 kbd_write_command(data);
525}
526
527void kbd_write_output_w(int data)
528{
529 if(kb_wait())
530 PRINTF("timeout in kbd_write_output_w\n");
531 kbd_write_output(data);
532}
533
534void kbd_send_data(unsigned char data)
535{
536 unsigned char status;
537 disable_8259A_irq(1); /* disable interrupt */
538 kbd_write_output_w(data);
539 status = kbd_wait_for_input();
540 if (status == KBD_REPLY_ACK)
541 enable_8259A_irq(1); /* enable interrupt */
542}
543
544
545char * kbd_initialize(void)
546{
547 int status;
548
549 in_pointer = 0; /* delete in Buffer */
550 out_pointer = 0;
551 /*
552 * Test the keyboard interface.
553 * This seems to be the only way to get it going.
554 * If the test is successful a x55 is placed in the input buffer.
555 */
556 kbd_write_command_w(KBD_CCMD_SELF_TEST);
557 if (kbd_wait_for_input() != 0x55)
558 return "Kbd: failed self test";
559 /*
560 * Perform a keyboard interface test. This causes the controller
561 * to test the keyboard clock and data lines. The results of the
562 * test are placed in the input buffer.
563 */
564 kbd_write_command_w(KBD_CCMD_KBD_TEST);
565 if (kbd_wait_for_input() != 0x00)
566 return "Kbd: interface failed self test";
567 /*
568 * Enable the keyboard by allowing the keyboard clock to run.
569 */
570 kbd_write_command_w(KBD_CCMD_KBD_ENABLE);
571 status = kbd_wait_for_input();
572 /*
573 * Reset keyboard. If the read times out
574 * then the assumption is that no keyboard is
575 * plugged into the machine.
576 * This defaults the keyboard to scan-code set 2.
577 *
578 * Set up to try again if the keyboard asks for RESEND.
579 */
580 do {
581 kbd_write_output_w(KBD_CMD_RESET);
582 status = kbd_wait_for_input();
583 if (status == KBD_REPLY_ACK)
584 break;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200585 if (status != KBD_REPLY_RESEND) {
wdenkaffae2b2002-08-17 09:36:01 +0000586 PRINTF("status: %X\n",status);
587 return "Kbd: reset failed, no ACK";
588 }
589 } while (1);
590 if (kbd_wait_for_input() != KBD_REPLY_POR)
591 return "Kbd: reset failed, no POR";
592
593 /*
594 * Set keyboard controller mode. During this, the keyboard should be
595 * in the disabled state.
596 *
597 * Set up to try again if the keyboard asks for RESEND.
598 */
599 do {
600 kbd_write_output_w(KBD_CMD_DISABLE);
601 status = kbd_wait_for_input();
602 if (status == KBD_REPLY_ACK)
603 break;
604 if (status != KBD_REPLY_RESEND)
605 return "Kbd: disable keyboard: no ACK";
606 } while (1);
607
608 kbd_write_command_w(KBD_CCMD_WRITE_MODE);
609 kbd_write_output_w(KBD_MODE_KBD_INT
610 | KBD_MODE_SYS
611 | KBD_MODE_DISABLE_MOUSE
612 | KBD_MODE_KCC);
613
Wolfgang Denk0c8721a2005-09-23 11:05:55 +0200614 /* AMCC powerpc portables need this to use scan-code set 1 -- Cort */
wdenkaffae2b2002-08-17 09:36:01 +0000615 kbd_write_command_w(KBD_CCMD_READ_MODE);
616 if (!(kbd_wait_for_input() & KBD_MODE_KCC)) {
617 /*
618 * If the controller does not support conversion,
619 * Set the keyboard to scan-code set 1.
620 */
621 kbd_write_output_w(0xF0);
622 kbd_wait_for_input();
623 kbd_write_output_w(0x01);
624 kbd_wait_for_input();
625 }
626 kbd_write_output_w(KBD_CMD_ENABLE);
627 if (kbd_wait_for_input() != KBD_REPLY_ACK)
628 return "Kbd: enable keyboard: no ACK";
629
630 /*
631 * Finally, set the typematic rate to maximum.
632 */
633 kbd_write_output_w(KBD_CMD_SET_RATE);
634 if (kbd_wait_for_input() != KBD_REPLY_ACK)
635 return "Kbd: Set rate: no ACK";
636 kbd_write_output_w(0x00);
637 if (kbd_wait_for_input() != KBD_REPLY_ACK)
638 return "Kbd: Set rate: no ACK";
639 return NULL;
640}
641
642void kbd_interrupt(void)
643{
644 handle_kbd_event();
645}