Revert "Rework disabling of ps2 port irqs."
[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 #include "pic.h" // eoi_pic1
13
14
15 /****************************************************************
16  * Low level i8042 commands.
17  ****************************************************************/
18
19 // Timeout value.
20 #define I8042_CTL_TIMEOUT       10000
21
22 #define I8042_BUFFER_SIZE       16
23
24 static int
25 i8042_wait_read(void)
26 {
27     dprintf(7, "i8042_wait_read\n");
28     int i;
29     for (i=0; i<I8042_CTL_TIMEOUT; i++) {
30         u8 status = inb(PORT_PS2_STATUS);
31         if (status & I8042_STR_OBF)
32             return 0;
33         udelay(50);
34     }
35     warn_timeout();
36     return -1;
37 }
38
39 static int
40 i8042_wait_write(void)
41 {
42     dprintf(7, "i8042_wait_write\n");
43     int i;
44     for (i=0; i<I8042_CTL_TIMEOUT; i++) {
45         u8 status = inb(PORT_PS2_STATUS);
46         if (! (status & I8042_STR_IBF))
47             return 0;
48         udelay(50);
49     }
50     warn_timeout();
51     return -1;
52 }
53
54 int
55 i8042_flush(void)
56 {
57     dprintf(7, "i8042_flush\n");
58     int i;
59     for (i=0; i<I8042_BUFFER_SIZE; i++) {
60         u8 status = inb(PORT_PS2_STATUS);
61         if (! (status & I8042_STR_OBF))
62             return 0;
63         udelay(50);
64         u8 data = inb(PORT_PS2_DATA);
65         dprintf(7, "i8042 flushed %x (status=%x)\n", data, status);
66     }
67
68     warn_timeout();
69     return -1;
70 }
71
72 static int
73 __i8042_command(int command, u8 *param)
74 {
75     int receive = (command >> 8) & 0xf;
76     int send = (command >> 12) & 0xf;
77
78     // Send the command.
79     int ret = i8042_wait_write();
80     if (ret)
81         return ret;
82     outb(command, PORT_PS2_STATUS);
83
84     // Send parameters (if any).
85     int i;
86     for (i = 0; i < send; i++) {
87         ret = i8042_wait_write();
88         if (ret)
89             return ret;
90         outb(param[i], PORT_PS2_DATA);
91     }
92
93     // Receive parameters (if any).
94     for (i = 0; i < receive; i++) {
95         ret = i8042_wait_read();
96         if (ret)
97             return ret;
98         param[i] = inb(PORT_PS2_DATA);
99         dprintf(7, "i8042 param=%x\n", param[i]);
100     }
101
102     return 0;
103 }
104
105 int
106 i8042_command(int command, u8 *param)
107 {
108     dprintf(7, "i8042_command cmd=%x\n", command);
109     int ret = __i8042_command(command, param);
110     if (ret)
111         dprintf(2, "i8042 command %x failed\n", command);
112     return ret;
113 }
114
115 static int
116 i8042_kbd_write(u8 c)
117 {
118     dprintf(7, "i8042_kbd_write c=%d\n", c);
119     int ret = i8042_wait_write();
120     if (! ret)
121         outb(c, PORT_PS2_DATA);
122     return ret;
123 }
124
125 static int
126 i8042_aux_write(u8 c)
127 {
128     return i8042_command(I8042_CMD_AUX_SEND, &c);
129 }
130
131
132 /****************************************************************
133  * Device commands.
134  ****************************************************************/
135
136 #define PS2_RET_ACK             0xfa
137 #define PS2_RET_NAK             0xfe
138
139 static void
140 process_ps2byte(u8 status, u8 data)
141 {
142     if (!MODE16) {
143         // Don't pull in all of keyboard/mouse code into 32bit code -
144         // just discard the data.
145         dprintf(1, "Discarding ps2 data %x (status=%x)\n", data, status);
146         return;
147     }
148     if (status & I8042_STR_AUXDATA)
149         process_mouse(data);
150     else
151         process_key(data);
152 }
153
154 static int
155 ps2_recvbyte(int aux, int needack, int timeout)
156 {
157     u64 end = calc_future_tsc(timeout);
158     for (;;) {
159         u8 status = inb(PORT_PS2_STATUS);
160         if (status & I8042_STR_OBF) {
161             u8 data = inb(PORT_PS2_DATA);
162             dprintf(7, "ps2 read %x\n", data);
163
164             if (!!(status & I8042_STR_AUXDATA) == aux) {
165                 if (!needack)
166                     return data;
167                 if (data == PS2_RET_ACK)
168                     return data;
169                 if (data == PS2_RET_NAK) {
170                     dprintf(1, "Got ps2 nak (status=%x)\n", status);
171                     return data;
172                 }
173             }
174
175             // Data not part of this command.
176             process_ps2byte(status, data);
177         }
178
179         if (check_time(end)) {
180             // Don't warn on second byte of a reset
181             if (timeout > 100)
182                 warn_timeout();
183             return -1;
184         }
185         yield();
186     }
187 }
188
189 static int
190 ps2_sendbyte(int aux, u8 command, int timeout)
191 {
192     dprintf(7, "ps2_sendbyte aux=%d cmd=%x\n", aux, command);
193     int ret;
194     if (aux)
195         ret = i8042_aux_write(command);
196     else
197         ret = i8042_kbd_write(command);
198     if (ret)
199         return ret;
200
201     // Read ack.
202     ret = ps2_recvbyte(aux, 1, timeout);
203     if (ret < 0)
204         return ret;
205     if (ret != PS2_RET_ACK)
206         return -1;
207
208     return 0;
209 }
210
211 static int
212 ps2_command(int aux, int command, u8 *param)
213 {
214     int ret2;
215     int receive = (command >> 8) & 0xf;
216     int send = (command >> 12) & 0xf;
217
218     // Disable interrupts and keyboard/mouse.
219     u8 ps2ctr = GET_EBDA(ps2ctr);
220     u8 newctr = ps2ctr;
221     if (aux)
222         newctr |= I8042_CTR_KBDDIS;
223     else
224         newctr |= I8042_CTR_AUXDIS;
225     newctr &= ~(I8042_CTR_KBDINT|I8042_CTR_AUXINT);
226     dprintf(6, "i8042 ctr old=%x new=%x\n", ps2ctr, newctr);
227     int ret = i8042_command(I8042_CMD_CTL_WCTR, &newctr);
228     if (ret)
229         return ret;
230
231     if (command == ATKBD_CMD_RESET_BAT) {
232         // Reset is special wrt timeouts.
233
234         // Send command.
235         ret = ps2_sendbyte(aux, command, 1000);
236         if (ret)
237             goto fail;
238
239         // Receive parameters.
240         ret = ps2_recvbyte(aux, 0, 4000);
241         if (ret < 0)
242             goto fail;
243         param[0] = ret;
244         ret = ps2_recvbyte(aux, 0, 100);
245         if (ret < 0)
246             // Some devices only respond with one byte on reset.
247             ret = 0;
248         param[1] = ret;
249     } else {
250         // Send command.
251         ret = ps2_sendbyte(aux, command, 200);
252         if (ret)
253             goto fail;
254
255         // Send parameters (if any).
256         int i;
257         for (i = 0; i < send; i++) {
258             ret = ps2_sendbyte(aux, param[i], 200);
259             if (ret)
260                 goto fail;
261         }
262
263         // Receive parameters (if any).
264         for (i = 0; i < receive; i++) {
265             ret = ps2_recvbyte(aux, 0, 500);
266             if (ret < 0)
267                 goto fail;
268             param[i] = ret;
269         }
270     }
271
272     ret = 0;
273
274 fail:
275     // Restore interrupts and keyboard/mouse.
276     ret2 = i8042_command(I8042_CMD_CTL_WCTR, &ps2ctr);
277     if (ret2)
278         return ret2;
279
280     return ret;
281 }
282
283 int
284 kbd_command(int command, u8 *param)
285 {
286     dprintf(7, "kbd_command cmd=%x\n", command);
287     int ret = ps2_command(0, command, param);
288     if (ret)
289         dprintf(2, "keyboard command %x failed\n", command);
290     return ret;
291 }
292
293 int
294 aux_command(int command, u8 *param)
295 {
296     dprintf(7, "aux_command cmd=%x\n", command);
297     int ret = ps2_command(1, command, param);
298     if (ret)
299         dprintf(2, "mouse command %x failed\n", command);
300     return ret;
301 }
302
303
304 /****************************************************************
305  * IRQ handlers
306  ****************************************************************/
307
308 static void
309 process_ps2irq(void)
310 {
311     u8 status = inb(PORT_PS2_STATUS);
312     if (!(status & I8042_STR_OBF)) {
313         dprintf(1, "ps2 irq but no data.\n");
314         return;
315     }
316     u8 data = inb(PORT_PS2_DATA);
317
318     process_ps2byte(status, data);
319 }
320
321 // INT74h : PS/2 mouse hardware interrupt
322 void VISIBLE16
323 handle_74(void)
324 {
325     if (! CONFIG_PS2PORT)
326         return;
327
328     debug_isr(DEBUG_ISR_74);
329     process_ps2irq();
330     eoi_pic2();
331 }
332
333 // INT09h : Keyboard Hardware Service Entry Point
334 void VISIBLE16
335 handle_09(void)
336 {
337     if (! CONFIG_PS2PORT)
338         return;
339
340     debug_isr(DEBUG_ISR_09);
341     process_ps2irq();
342     eoi_pic1();
343 }
344
345
346 /****************************************************************
347  * Setup
348  ****************************************************************/
349
350 static void
351 keyboard_init(void *data)
352 {
353     /* flush incoming keys */
354     int ret = i8042_flush();
355     if (ret)
356         return;
357
358     // Controller self-test.
359     u8 param[2];
360     ret = i8042_command(I8042_CMD_CTL_TEST, param);
361     if (ret)
362         return;
363     if (param[0] != 0x55) {
364         dprintf(1, "i8042 self test failed (got %x not 0x55)\n", param[0]);
365         return;
366     }
367
368     // Controller keyboard test.
369     ret = i8042_command(I8042_CMD_KBD_TEST, param);
370     if (ret)
371         return;
372     if (param[0] != 0x00) {
373         dprintf(1, "i8042 keyboard test failed (got %x not 0x00)\n", param[0]);
374         return;
375     }
376
377     // Enable keyboard and mouse ports.
378     ret = i8042_command(I8042_CMD_KBD_ENABLE, NULL);
379     if (ret)
380         return;
381     ret = i8042_command(I8042_CMD_AUX_ENABLE, NULL);
382     if (ret)
383         return;
384
385
386     /* ------------------- keyboard side ------------------------*/
387     /* reset keyboard and self test  (keyboard side) */
388     ret = kbd_command(ATKBD_CMD_RESET_BAT, param);
389     if (ret)
390         return;
391     if (param[0] != 0xaa) {
392         dprintf(1, "keyboard self test failed (got %x not 0xaa)\n", param[0]);
393         return;
394     }
395
396     /* Disable keyboard */
397     ret = kbd_command(ATKBD_CMD_RESET_DIS, NULL);
398     if (ret)
399         return;
400
401     // Set scancode command (mode 2)
402     param[0] = 0x02;
403     ret = kbd_command(ATKBD_CMD_SSCANSET, param);
404     if (ret)
405         return;
406
407     // Keyboard Mode: scan code convert, disable mouse, enable IRQ 1
408     SET_EBDA(ps2ctr, I8042_CTR_AUXDIS | I8042_CTR_XLATE | I8042_CTR_KBDINT);
409
410     /* Enable keyboard */
411     ret = kbd_command(ATKBD_CMD_ENABLE, NULL);
412     if (ret)
413         return;
414
415     dprintf(1, "PS2 keyboard initialized\n");
416 }
417
418 void
419 ps2port_setup(void)
420 {
421     ASSERT32FLAT();
422     if (! CONFIG_PS2PORT)
423         return;
424     dprintf(3, "init ps2port\n");
425
426     enable_hwirq(1, entry_09);
427     enable_hwirq(12, entry_74);
428
429     run_thread(keyboard_init, NULL);
430 }