1 /***********************************************************************
4 * DENX Software Engineering
5 * Wolfgang Denk, wd@denx.de
9 * Originally from linux source (drivers/char/pc_keyb.c)
11 ***********************************************************************/
21 #define PRINTF(fmt,args...) printf (fmt ,##args)
23 #define PRINTF(fmt,args...)
28 * This reads the keyboard status port, and does the
32 static unsigned char handle_kbd_event(void)
34 unsigned char status = kbd_read_status();
35 unsigned int work = 10000;
37 while ((--work > 0) && (status & KBD_STAT_OBF)) {
38 unsigned char scancode;
40 scancode = kbd_read_input();
42 /* Error bytes must be ignored to make the
43 Synaptics touchpads compaq use work */
44 /* Ignore error bytes */
45 if (!(status & (KBD_STAT_GTO | KBD_STAT_PERR))) {
46 if (status & KBD_STAT_MOUSE_OBF)
47 ; /* not supported: handle_mouse_event(scancode); */
49 handle_scancode(scancode);
51 status = kbd_read_status();
54 PRINTF("pc_keyb: controller jammed (0x%02X).\n", status);
59 static int kbd_read_data(void)
65 status = kbd_read_status();
66 if (status & KBD_STAT_OBF) {
67 val = kbd_read_input();
68 if (status & (KBD_STAT_GTO | KBD_STAT_PERR))
74 static int kbd_wait_for_input(void)
76 unsigned long timeout;
79 timeout = KBD_TIMEOUT;
91 static int kb_wait(void)
93 unsigned long timeout = KBC_TIMEOUT * 10;
96 unsigned char status = handle_kbd_event();
97 if (!(status & KBD_STAT_IBF))
105 static void kbd_write_command_w(int data)
108 PRINTF("timeout in kbd_write_command_w\n");
109 kbd_write_command(data);
112 static void kbd_write_output_w(int data)
115 PRINTF("timeout in kbd_write_output_w\n");
116 kbd_write_output(data);
119 static void kbd_send_data(unsigned char data)
121 kbd_write_output_w(data);
122 kbd_wait_for_input();
126 static char * kbd_initialize(void)
131 * Test the keyboard interface.
132 * This seems to be the only way to get it going.
133 * If the test is successful a x55 is placed in the input buffer.
135 kbd_write_command_w(KBD_CCMD_SELF_TEST);
136 if (kbd_wait_for_input() != 0x55)
137 return "Kbd: failed self test";
139 * Perform a keyboard interface test. This causes the controller
140 * to test the keyboard clock and data lines. The results of the
141 * test are placed in the input buffer.
143 kbd_write_command_w(KBD_CCMD_KBD_TEST);
144 if (kbd_wait_for_input() != 0x00)
145 return "Kbd: interface failed self test";
147 * Enable the keyboard by allowing the keyboard clock to run.
149 kbd_write_command_w(KBD_CCMD_KBD_ENABLE);
152 * Reset keyboard. If the read times out
153 * then the assumption is that no keyboard is
154 * plugged into the machine.
155 * This defaults the keyboard to scan-code set 2.
157 * Set up to try again if the keyboard asks for RESEND.
160 kbd_write_output_w(KBD_CMD_RESET);
161 status = kbd_wait_for_input();
162 if (status == KBD_REPLY_ACK)
164 if (status != KBD_REPLY_RESEND) {
165 PRINTF("status: %X\n",status);
166 return "Kbd: reset failed, no ACK";
169 if (kbd_wait_for_input() != KBD_REPLY_POR)
170 return "Kbd: reset failed, no POR";
173 * Set keyboard controller mode. During this, the keyboard should be
174 * in the disabled state.
176 * Set up to try again if the keyboard asks for RESEND.
179 kbd_write_output_w(KBD_CMD_DISABLE);
180 status = kbd_wait_for_input();
181 if (status == KBD_REPLY_ACK)
183 if (status != KBD_REPLY_RESEND)
184 return "Kbd: disable keyboard: no ACK";
187 kbd_write_command_w(KBD_CCMD_WRITE_MODE);
188 kbd_write_output_w(KBD_MODE_KBD_INT
190 | KBD_MODE_DISABLE_MOUSE
193 /* AMCC powerpc portables need this to use scan-code set 1 -- Cort */
194 kbd_write_command_w(KBD_CCMD_READ_MODE);
195 if (!(kbd_wait_for_input() & KBD_MODE_KCC)) {
197 * If the controller does not support conversion,
198 * Set the keyboard to scan-code set 1.
200 kbd_write_output_w(0xF0);
201 kbd_wait_for_input();
202 kbd_write_output_w(0x01);
203 kbd_wait_for_input();
205 kbd_write_output_w(KBD_CMD_ENABLE);
206 if (kbd_wait_for_input() != KBD_REPLY_ACK)
207 return "Kbd: enable keyboard: no ACK";
210 * Finally, set the typematic rate to maximum.
212 kbd_write_output_w(KBD_CMD_SET_RATE);
213 if (kbd_wait_for_input() != KBD_REPLY_ACK)
214 return "Kbd: Set rate: no ACK";
215 kbd_write_output_w(0x00);
216 if (kbd_wait_for_input() != KBD_REPLY_ACK)
217 return "Kbd: Set rate: no ACK";
221 static void kbd_interrupt(void *dev_id)
226 /******************************************************************
228 ******************************************************************/
230 int kbd_init_hw(void)
234 kbd_request_region();
236 result=kbd_initialize();
238 PRINTF("AT Keyboard initialized\n");
239 kbd_request_irq(kbd_interrupt);
242 printf("%s\n",result);
247 void pckbd_leds(unsigned char leds)
249 kbd_send_data(KBD_CMD_SET_LEDS);