This patch changes rtc_get() to return an int. Without this change
[u-boot-openmoko/mini2440.git] / drivers / input / pc_keyb.c
blob81d3e989341e97b44260fd102bbc55e5268a0b31
1 /***********************************************************************
3 * (C) Copyright 2004
4 * DENX Software Engineering
5 * Wolfgang Denk, wd@denx.de
6 * All rights reserved.
8 * PS/2 keyboard driver
10 * Originally from linux source (drivers/char/pc_keyb.c)
12 ***********************************************************************/
14 #include <common.h>
16 #ifdef CONFIG_PS2KBD
18 #include <keyboard.h>
19 #include <pc_keyb.h>
21 #undef KBG_DEBUG
23 #ifdef KBG_DEBUG
24 #define PRINTF(fmt,args...) printf (fmt ,##args)
25 #else
26 #define PRINTF(fmt,args...)
27 #endif
31 * This reads the keyboard status port, and does the
32 * appropriate action.
35 static unsigned char handle_kbd_event(void)
37 unsigned char status = kbd_read_status();
38 unsigned int work = 10000;
40 while ((--work > 0) && (status & KBD_STAT_OBF)) {
41 unsigned char scancode;
43 scancode = kbd_read_input();
45 /* Error bytes must be ignored to make the
46 Synaptics touchpads compaq use work */
47 /* Ignore error bytes */
48 if (!(status & (KBD_STAT_GTO | KBD_STAT_PERR))) {
49 if (status & KBD_STAT_MOUSE_OBF)
50 ; /* not supported: handle_mouse_event(scancode); */
51 else
52 handle_scancode(scancode);
54 status = kbd_read_status();
56 if (!work)
57 PRINTF("pc_keyb: controller jammed (0x%02X).\n", status);
58 return status;
62 static int kbd_read_data(void)
64 int val;
65 unsigned char status;
67 val=-1;
68 status = kbd_read_status();
69 if (status & KBD_STAT_OBF) {
70 val = kbd_read_input();
71 if (status & (KBD_STAT_GTO | KBD_STAT_PERR))
72 val = -2;
74 return val;
77 static int kbd_wait_for_input(void)
79 unsigned long timeout;
80 int val;
82 timeout = KBD_TIMEOUT;
83 val=kbd_read_data();
84 while(val < 0) {
85 if(timeout--==0)
86 return -1;
87 udelay(1000);
88 val=kbd_read_data();
90 return val;
94 static int kb_wait(void)
96 unsigned long timeout = KBC_TIMEOUT * 10;
98 do {
99 unsigned char status = handle_kbd_event();
100 if (!(status & KBD_STAT_IBF))
101 return 0; /* ok */
102 udelay(1000);
103 timeout--;
104 } while (timeout);
105 return 1;
108 static void kbd_write_command_w(int data)
110 if(kb_wait())
111 PRINTF("timeout in kbd_write_command_w\n");
112 kbd_write_command(data);
115 static void kbd_write_output_w(int data)
117 if(kb_wait())
118 PRINTF("timeout in kbd_write_output_w\n");
119 kbd_write_output(data);
122 static void kbd_send_data(unsigned char data)
124 kbd_write_output_w(data);
125 kbd_wait_for_input();
129 static char * kbd_initialize(void)
131 int status;
134 * Test the keyboard interface.
135 * This seems to be the only way to get it going.
136 * If the test is successful a x55 is placed in the input buffer.
138 kbd_write_command_w(KBD_CCMD_SELF_TEST);
139 if (kbd_wait_for_input() != 0x55)
140 return "Kbd: failed self test";
142 * Perform a keyboard interface test. This causes the controller
143 * to test the keyboard clock and data lines. The results of the
144 * test are placed in the input buffer.
146 kbd_write_command_w(KBD_CCMD_KBD_TEST);
147 if (kbd_wait_for_input() != 0x00)
148 return "Kbd: interface failed self test";
150 * Enable the keyboard by allowing the keyboard clock to run.
152 kbd_write_command_w(KBD_CCMD_KBD_ENABLE);
155 * Reset keyboard. If the read times out
156 * then the assumption is that no keyboard is
157 * plugged into the machine.
158 * This defaults the keyboard to scan-code set 2.
160 * Set up to try again if the keyboard asks for RESEND.
162 do {
163 kbd_write_output_w(KBD_CMD_RESET);
164 status = kbd_wait_for_input();
165 if (status == KBD_REPLY_ACK)
166 break;
167 if (status != KBD_REPLY_RESEND) {
168 PRINTF("status: %X\n",status);
169 return "Kbd: reset failed, no ACK";
171 } while (1);
172 if (kbd_wait_for_input() != KBD_REPLY_POR)
173 return "Kbd: reset failed, no POR";
176 * Set keyboard controller mode. During this, the keyboard should be
177 * in the disabled state.
179 * Set up to try again if the keyboard asks for RESEND.
181 do {
182 kbd_write_output_w(KBD_CMD_DISABLE);
183 status = kbd_wait_for_input();
184 if (status == KBD_REPLY_ACK)
185 break;
186 if (status != KBD_REPLY_RESEND)
187 return "Kbd: disable keyboard: no ACK";
188 } while (1);
190 kbd_write_command_w(KBD_CCMD_WRITE_MODE);
191 kbd_write_output_w(KBD_MODE_KBD_INT
192 | KBD_MODE_SYS
193 | KBD_MODE_DISABLE_MOUSE
194 | KBD_MODE_KCC);
196 /* AMCC powerpc portables need this to use scan-code set 1 -- Cort */
197 kbd_write_command_w(KBD_CCMD_READ_MODE);
198 if (!(kbd_wait_for_input() & KBD_MODE_KCC)) {
200 * If the controller does not support conversion,
201 * Set the keyboard to scan-code set 1.
203 kbd_write_output_w(0xF0);
204 kbd_wait_for_input();
205 kbd_write_output_w(0x01);
206 kbd_wait_for_input();
208 kbd_write_output_w(KBD_CMD_ENABLE);
209 if (kbd_wait_for_input() != KBD_REPLY_ACK)
210 return "Kbd: enable keyboard: no ACK";
213 * Finally, set the typematic rate to maximum.
215 kbd_write_output_w(KBD_CMD_SET_RATE);
216 if (kbd_wait_for_input() != KBD_REPLY_ACK)
217 return "Kbd: Set rate: no ACK";
218 kbd_write_output_w(0x00);
219 if (kbd_wait_for_input() != KBD_REPLY_ACK)
220 return "Kbd: Set rate: no ACK";
221 return NULL;
224 static void kbd_interrupt(void *dev_id)
226 handle_kbd_event();
229 /******************************************************************
230 * Init
231 ******************************************************************/
233 int kbd_init_hw(void)
235 char* result;
237 kbd_request_region();
239 result=kbd_initialize();
240 if (result==NULL) {
241 PRINTF("AT Keyboard initialized\n");
242 kbd_request_irq(kbd_interrupt);
243 return (1);
244 } else {
245 printf("%s\n",result);
246 return (-1);
250 void pckbd_leds(unsigned char leds)
252 kbd_send_data(KBD_CMD_SET_LEDS);
253 kbd_send_data(leds);
256 #endif /* CONFIG_PS2KBD */