Handle tsc rollover.
[seabios.git] / src / ps2port.c
1 // Support for handling the PS/2 mouse/keyboard ports.
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 // Several ideas taken from code Copyright (c) 1999-2004 Vojtech Pavlik
5 //
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
7
8 #include "ioport.h" // inb
9 #include "util.h" // dprintf
10 #include "biosvar.h" // GET_EBDA
11 #include "ps2port.h" // kbd_command
12
13
14 /****************************************************************
15  * Low level i8042 commands.
16  ****************************************************************/
17
18 // Timeout value.
19 #define I8042_CTL_TIMEOUT       10000
20
21 #define I8042_BUFFER_SIZE       16
22
23 static int
24 i8042_wait_read(void)
25 {
26     dprintf(7, "i8042_wait_read\n");
27     int i;
28     for (i=0; i<I8042_CTL_TIMEOUT; i++) {
29         u8 status = inb(PORT_PS2_STATUS);
30         if (status & I8042_STR_OBF)
31             return 0;
32         udelay(50);
33     }
34     dprintf(1, "i8042 timeout on wait read\n");
35     return -1;
36 }
37
38 static int
39 i8042_wait_write(void)
40 {
41     dprintf(7, "i8042_wait_write\n");
42     int i;
43     for (i=0; i<I8042_CTL_TIMEOUT; i++) {
44         u8 status = inb(PORT_PS2_STATUS);
45         if (! (status & I8042_STR_IBF))
46             return 0;
47         udelay(50);
48     }
49     dprintf(1, "i8042 timeout on wait write\n");
50     return -1;
51 }
52
53 int
54 i8042_flush(void)
55 {
56     dprintf(7, "i8042_flush\n");
57     unsigned long flags = irq_save();
58
59     int i;
60     for (i=0; i<I8042_BUFFER_SIZE; i++) {
61         u8 status = inb(PORT_PS2_STATUS);
62         if (! (status & I8042_STR_OBF)) {
63             irq_restore(flags);
64             return 0;
65         }
66         udelay(50);
67         u8 data = inb(PORT_PS2_DATA);
68         dprintf(7, "i8042 flushed %x (status=%x)\n", data, status);
69     }
70
71     irq_restore(flags);
72     dprintf(1, "i8042 timeout on flush\n");
73     return -1;
74 }
75
76 static int
77 __i8042_command(int command, u8 *param)
78 {
79     int receive = (command >> 8) & 0xf;
80     int send = (command >> 12) & 0xf;
81
82     // Send the command.
83     int ret = i8042_wait_write();
84     if (ret)
85         return ret;
86     outb(command, PORT_PS2_STATUS);
87
88     // Send parameters (if any).
89     int i;
90     for (i = 0; i < send; i++) {
91         ret = i8042_wait_write();
92         if (ret)
93             return ret;
94         outb(param[i], PORT_PS2_DATA);
95     }
96
97     // Receive parameters (if any).
98     for (i = 0; i < receive; i++) {
99         ret = i8042_wait_read();
100         if (ret)
101             return ret;
102         param[i] = inb(PORT_PS2_DATA);
103         dprintf(7, "i8042 param=%x\n", param[i]);
104     }
105
106     return 0;
107 }
108
109 int
110 i8042_command(int command, u8 *param)
111 {
112     dprintf(7, "i8042_command cmd=%x\n", command);
113     unsigned long flags = irq_save();
114     int ret = __i8042_command(command, param);
115     irq_restore(flags);
116     if (ret)
117         dprintf(2, "i8042 command %x failed\n", command);
118     return ret;
119 }
120
121 static int
122 i8042_kbd_write(u8 c)
123 {
124     dprintf(7, "i8042_kbd_write c=%d\n", c);
125     unsigned long flags = irq_save();
126
127     int ret = i8042_wait_write();
128     if (! ret)
129         outb(c, PORT_PS2_DATA);
130
131     irq_restore(flags);
132
133     return ret;
134 }
135
136 static int
137 i8042_aux_write(u8 c)
138 {
139     return i8042_command(I8042_CMD_AUX_SEND, &c);
140 }
141
142
143 /****************************************************************
144  * Device commands.
145  ****************************************************************/
146
147 #define PS2_RET_ACK             0xfa
148 #define PS2_RET_NAK             0xfe
149
150 static int
151 ps2_recvbyte(int aux, int needack, int timeout)
152 {
153     u64 end = calc_future_tsc(timeout);
154     for (;;) {
155         if (check_time(end)) {
156             dprintf(1, "ps2_recvbyte timeout\n");
157             return -1;
158         }
159
160         u8 status = inb(PORT_PS2_STATUS);
161         if (! (status & I8042_STR_OBF))
162             continue;
163         u8 data = inb(PORT_PS2_DATA);
164         dprintf(7, "ps2 read %x\n", data);
165
166         if (!!(status & I8042_STR_AUXDATA) == aux) {
167             if (!needack)
168                 return data;
169             if (data == PS2_RET_ACK)
170                 return data;
171             if (data == PS2_RET_NAK) {
172                 dprintf(1, "Got ps2 nak (status=%x); continuing\n", status);
173                 return data;
174             }
175         }
176
177         // This data not for us - XXX - just discard it for now.
178         dprintf(1, "Discarding ps2 data %x (status=%x)\n", data, status);
179     }
180 }
181
182 static int
183 ps2_sendbyte(int aux, u8 command, int timeout)
184 {
185     dprintf(7, "ps2_sendbyte aux=%d cmd=%x\n", aux, command);
186     int ret;
187     if (aux)
188         ret = i8042_aux_write(command);
189     else
190         ret = i8042_kbd_write(command);
191     if (ret)
192         return ret;
193
194     // Read ack.
195     ret = ps2_recvbyte(aux, 1, timeout);
196     if (ret < 0)
197         return ret;
198
199     return 0;
200 }
201
202 static int
203 ps2_command(int aux, int command, u8 *param)
204 {
205     int ret2;
206     int receive = (command >> 8) & 0xf;
207     int send = (command >> 12) & 0xf;
208
209     // Disable interrupts and keyboard/mouse.
210     u8 ps2ctr = GET_EBDA(ps2ctr);
211     u8 newctr = ps2ctr;
212     if (aux)
213         newctr |= I8042_CTR_KBDDIS;
214     else
215         newctr |= I8042_CTR_AUXDIS;
216     newctr &= ~(I8042_CTR_KBDINT|I8042_CTR_AUXINT);
217     dprintf(6, "i8042 ctr old=%x new=%x\n", ps2ctr, newctr);
218     int ret = i8042_command(I8042_CMD_CTL_WCTR, &newctr);
219     if (ret)
220         return ret;
221
222     if (command == ATKBD_CMD_RESET_BAT) {
223         // Reset is special wrt timeouts.
224
225         // Send command.
226         ret = ps2_sendbyte(aux, command, 1000);
227         if (ret)
228             goto fail;
229
230         // Receive parameters.
231         ret = ps2_recvbyte(aux, 0, 4000);
232         if (ret < 0)
233             goto fail;
234         param[0] = ret;
235         ret = ps2_recvbyte(aux, 0, 100);
236         if (ret < 0)
237             // Some devices only respond with one byte on reset.
238             ret = 0;
239         param[1] = ret;
240     } else {
241         // Send command.
242         ret = ps2_sendbyte(aux, command, 200);
243         if (ret)
244             goto fail;
245
246         // Send parameters (if any).
247         int i;
248         for (i = 0; i < send; i++) {
249             ret = ps2_sendbyte(aux, param[i], 200);
250             if (ret)
251                 goto fail;
252         }
253
254         // Receive parameters (if any).
255         for (i = 0; i < receive; i++) {
256             ret = ps2_recvbyte(aux, 0, 500);
257             if (ret < 0)
258                 goto fail;
259             param[i] = ret;
260         }
261     }
262
263     ret = 0;
264
265 fail:
266     // Restore interrupts and keyboard/mouse.
267     ret2 = i8042_command(I8042_CMD_CTL_WCTR, &ps2ctr);
268     if (ret2)
269         return ret2;
270
271     return ret;
272 }
273
274 int
275 kbd_command(int command, u8 *param)
276 {
277     dprintf(7, "kbd_command cmd=%x\n", command);
278     int ret = ps2_command(0, command, param);
279     if (ret)
280         dprintf(2, "keyboard command %x failed\n", command);
281     return ret;
282 }
283
284 int
285 aux_command(int command, u8 *param)
286 {
287     dprintf(7, "aux_command cmd=%x\n", command);
288     int ret = ps2_command(1, command, param);
289     if (ret)
290         dprintf(2, "mouse command %x failed\n", command);
291     return ret;
292 }