Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / seabios / src / hw / 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 "biosvar.h" // GET_LOW
9 #include "output.h" // dprintf
10 #include "pic.h" // pic_eoi1
11 #include "ps2port.h" // ps2_kbd_command
12 #include "romfile.h" // romfile_loadint
13 #include "stacks.h" // yield
14 #include "util.h" // udelay
15 #include "x86.h" // inb
16
17
18 /****************************************************************
19  * Low level i8042 commands.
20  ****************************************************************/
21
22 // Timeout value.
23 #define I8042_CTL_TIMEOUT       10000
24
25 #define I8042_BUFFER_SIZE       16
26
27 static int
28 i8042_wait_read(void)
29 {
30     dprintf(7, "i8042_wait_read\n");
31     int i;
32     for (i=0; i<I8042_CTL_TIMEOUT; i++) {
33         u8 status = inb(PORT_PS2_STATUS);
34         if (status & I8042_STR_OBF)
35             return 0;
36         udelay(50);
37     }
38     warn_timeout();
39     return -1;
40 }
41
42 static int
43 i8042_wait_write(void)
44 {
45     dprintf(7, "i8042_wait_write\n");
46     int i;
47     for (i=0; i<I8042_CTL_TIMEOUT; i++) {
48         u8 status = inb(PORT_PS2_STATUS);
49         if (! (status & I8042_STR_IBF))
50             return 0;
51         udelay(50);
52     }
53     warn_timeout();
54     return -1;
55 }
56
57 static int
58 i8042_flush(void)
59 {
60     dprintf(7, "i8042_flush\n");
61     int i;
62     for (i=0; i<I8042_BUFFER_SIZE; i++) {
63         u8 status = inb(PORT_PS2_STATUS);
64         if (! (status & I8042_STR_OBF))
65             return 0;
66         udelay(50);
67         u8 data = inb(PORT_PS2_DATA);
68         dprintf(7, "i8042 flushed %x (status=%x)\n", data, status);
69     }
70
71     warn_timeout();
72     return -1;
73 }
74
75 static int
76 __i8042_command(int command, u8 *param)
77 {
78     int receive = (command >> 8) & 0xf;
79     int send = (command >> 12) & 0xf;
80
81     // Send the command.
82     int ret = i8042_wait_write();
83     if (ret)
84         return ret;
85     outb(command, PORT_PS2_STATUS);
86
87     // Send parameters (if any).
88     int i;
89     for (i = 0; i < send; i++) {
90         ret = i8042_wait_write();
91         if (ret)
92             return ret;
93         outb(param[i], PORT_PS2_DATA);
94     }
95
96     // Receive parameters (if any).
97     for (i = 0; i < receive; i++) {
98         ret = i8042_wait_read();
99         if (ret)
100             return ret;
101         param[i] = inb(PORT_PS2_DATA);
102         dprintf(7, "i8042 param=%x\n", param[i]);
103     }
104
105     return 0;
106 }
107
108 static int
109 i8042_command(int command, u8 *param)
110 {
111     dprintf(7, "i8042_command cmd=%x\n", command);
112     int ret = __i8042_command(command, param);
113     if (ret)
114         dprintf(2, "i8042 command %x failed\n", command);
115     return ret;
116 }
117
118 static int
119 i8042_kbd_write(u8 c)
120 {
121     dprintf(7, "i8042_kbd_write c=%d\n", c);
122     int ret = i8042_wait_write();
123     if (! ret)
124         outb(c, PORT_PS2_DATA);
125     return ret;
126 }
127
128 static int
129 i8042_aux_write(u8 c)
130 {
131     return i8042_command(I8042_CMD_AUX_SEND, &c);
132 }
133
134 void
135 i8042_reboot(void)
136 {
137     if (! CONFIG_PS2PORT)
138        return;
139     int i;
140     for (i=0; i<10; i++) {
141         i8042_wait_write();
142         udelay(50);
143         outb(0xfe, PORT_PS2_STATUS); /* pulse reset low */
144         udelay(50);
145     }
146 }
147
148
149 /****************************************************************
150  * Device commands.
151  ****************************************************************/
152
153 #define PS2_RET_ACK             0xfa
154 #define PS2_RET_NAK             0xfe
155
156 static int
157 ps2_recvbyte(int aux, int needack, int timeout)
158 {
159     u32 end = timer_calc(timeout);
160     for (;;) {
161         u8 status = inb(PORT_PS2_STATUS);
162         if (status & I8042_STR_OBF) {
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)\n", status);
173                     return data;
174                 }
175             }
176
177             // This data not part of command - just discard it.
178             dprintf(1, "Discarding ps2 data %02x (status=%02x)\n", data, status);
179         }
180
181         if (timer_check(end)) {
182             // Don't warn on second byte of a reset
183             if (timeout > 100)
184                 warn_timeout();
185             return -1;
186         }
187         yield();
188     }
189 }
190
191 static int
192 ps2_sendbyte(int aux, u8 command, int timeout)
193 {
194     dprintf(7, "ps2_sendbyte aux=%d cmd=%x\n", aux, command);
195     int ret;
196     if (aux)
197         ret = i8042_aux_write(command);
198     else
199         ret = i8042_kbd_write(command);
200     if (ret)
201         return ret;
202
203     // Read ack.
204     ret = ps2_recvbyte(aux, 1, timeout);
205     if (ret < 0)
206         return ret;
207     if (ret != PS2_RET_ACK)
208         return -1;
209
210     return 0;
211 }
212
213 u8 Ps2ctr VARLOW;
214
215 static int
216 __ps2_command(int aux, int command, u8 *param)
217 {
218     int ret2;
219     int receive = (command >> 8) & 0xf;
220     int send = (command >> 12) & 0xf;
221
222     // Disable interrupts and keyboard/mouse.
223     u8 ps2ctr = GET_LOW(Ps2ctr);
224     u8 newctr = ((ps2ctr | I8042_CTR_AUXDIS | I8042_CTR_KBDDIS)
225                  & ~(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     // Flush any interrupts already pending.
232     yield();
233
234     // Enable port command is being sent to.
235     if (aux)
236         newctr &= ~I8042_CTR_AUXDIS;
237     else
238         newctr &= ~I8042_CTR_KBDDIS;
239     ret = i8042_command(I8042_CMD_CTL_WCTR, &newctr);
240     if (ret)
241         goto fail;
242
243     if (command == ATKBD_CMD_RESET_BAT) {
244         // Reset is special wrt timeouts and bytes received.
245
246         // Send command.
247         ret = ps2_sendbyte(aux, command, 1000);
248         if (ret)
249             goto fail;
250
251         // Receive parameters.
252         ret = ps2_recvbyte(aux, 0, 4000);
253         if (ret < 0)
254             goto fail;
255         param[0] = ret;
256         ret = ps2_recvbyte(aux, 0, 100);
257         if (ret < 0)
258             // Some devices only respond with one byte on reset.
259             ret = 0;
260         param[1] = ret;
261     } else if (command == ATKBD_CMD_GETID) {
262         // Getid is special wrt bytes received.
263
264         // Send command.
265         ret = ps2_sendbyte(aux, command, 200);
266         if (ret)
267             goto fail;
268
269         // Receive parameters.
270         ret = ps2_recvbyte(aux, 0, 500);
271         if (ret < 0)
272             goto fail;
273         param[0] = ret;
274         if (ret == 0xab || ret == 0xac || ret == 0x2b || ret == 0x5d
275             || ret == 0x60 || ret == 0x47) {
276             // These ids (keyboards) return two bytes.
277             ret = ps2_recvbyte(aux, 0, 500);
278             if (ret < 0)
279                 goto fail;
280             param[1] = ret;
281         } else {
282             param[1] = 0;
283         }
284     } else {
285         // Send command.
286         ret = ps2_sendbyte(aux, command, 200);
287         if (ret)
288             goto fail;
289
290         // Send parameters (if any).
291         int i;
292         for (i = 0; i < send; i++) {
293             ret = ps2_sendbyte(aux, param[i], 200);
294             if (ret)
295                 goto fail;
296         }
297
298         // Receive parameters (if any).
299         for (i = 0; i < receive; i++) {
300             ret = ps2_recvbyte(aux, 0, 500);
301             if (ret < 0)
302                 goto fail;
303             param[i] = ret;
304         }
305     }
306
307     ret = 0;
308
309 fail:
310     // Restore interrupts and keyboard/mouse.
311     ret2 = i8042_command(I8042_CMD_CTL_WCTR, &ps2ctr);
312     if (ret2)
313         return ret2;
314
315     return ret;
316 }
317
318 static int
319 ps2_command(int aux, int command, u8 *param)
320 {
321     dprintf(7, "ps2_command aux=%d cmd=%x\n", aux, command);
322     int ret = __ps2_command(aux, command, param);
323     if (ret)
324         dprintf(2, "ps2 command %x failed (aux=%d)\n", command, aux);
325     return ret;
326 }
327
328 int
329 ps2_kbd_command(int command, u8 *param)
330 {
331     if (! CONFIG_PS2PORT)
332         return -1;
333     return ps2_command(0, command, param);
334 }
335
336 int
337 ps2_mouse_command(int command, u8 *param)
338 {
339     if (! CONFIG_PS2PORT)
340         return -1;
341
342     // Update ps2ctr for mouse enable/disable.
343     if (command == PSMOUSE_CMD_ENABLE || command == PSMOUSE_CMD_DISABLE) {
344         u8 ps2ctr = GET_LOW(Ps2ctr);
345         if (command == PSMOUSE_CMD_ENABLE)
346             ps2ctr = (ps2ctr | I8042_CTR_AUXINT) & ~I8042_CTR_AUXDIS;
347         else
348             ps2ctr = (ps2ctr | I8042_CTR_AUXDIS) & ~I8042_CTR_AUXINT;
349         SET_LOW(Ps2ctr, ps2ctr);
350     }
351
352     return ps2_command(1, command, param);
353 }
354
355
356 /****************************************************************
357  * IRQ handlers
358  ****************************************************************/
359
360 // INT74h : PS/2 mouse hardware interrupt
361 void VISIBLE16
362 handle_74(void)
363 {
364     if (! CONFIG_PS2PORT)
365         return;
366
367     debug_isr(DEBUG_ISR_74);
368
369     u8 v = inb(PORT_PS2_STATUS);
370     if ((v & (I8042_STR_OBF|I8042_STR_AUXDATA))
371         != (I8042_STR_OBF|I8042_STR_AUXDATA)) {
372         dprintf(1, "ps2 mouse irq but no mouse data.\n");
373         goto done;
374     }
375     v = inb(PORT_PS2_DATA);
376
377     if (!(GET_LOW(Ps2ctr) & I8042_CTR_AUXINT))
378         // Interrupts not enabled.
379         goto done;
380
381     process_mouse(v);
382
383 done:
384     pic_eoi2();
385 }
386
387 // INT09h : Keyboard Hardware Service Entry Point
388 void VISIBLE16
389 handle_09(void)
390 {
391     if (! CONFIG_PS2PORT)
392         return;
393
394     debug_isr(DEBUG_ISR_09);
395
396     // read key from keyboard controller
397     u8 v = inb(PORT_PS2_STATUS);
398     if (v & I8042_STR_AUXDATA) {
399         dprintf(1, "ps2 keyboard irq but found mouse data?!\n");
400         goto done;
401     }
402     v = inb(PORT_PS2_DATA);
403
404     if (!(GET_LOW(Ps2ctr) & I8042_CTR_KBDINT))
405         // Interrupts not enabled.
406         goto done;
407
408     process_key(v);
409
410     // Some old programs expect ISR to turn keyboard back on.
411     i8042_command(I8042_CMD_KBD_ENABLE, NULL);
412
413 done:
414     pic_eoi1();
415 }
416
417
418 /****************************************************************
419  * Setup
420  ****************************************************************/
421
422 static void
423 ps2_keyboard_setup(void *data)
424 {
425     /* flush incoming keys */
426     int ret = i8042_flush();
427     if (ret)
428         return;
429
430     // Controller self-test.
431     u8 param[2];
432     ret = i8042_command(I8042_CMD_CTL_TEST, param);
433     if (ret)
434         return;
435     if (param[0] != 0x55) {
436         dprintf(1, "i8042 self test failed (got %x not 0x55)\n", param[0]);
437         return;
438     }
439
440     // Controller keyboard test.
441     ret = i8042_command(I8042_CMD_KBD_TEST, param);
442     if (ret)
443         return;
444     if (param[0] != 0x00) {
445         dprintf(1, "i8042 keyboard test failed (got %x not 0x00)\n", param[0]);
446         return;
447     }
448
449     // Disable keyboard and mouse events.
450     SET_LOW(Ps2ctr, I8042_CTR_KBDDIS | I8042_CTR_AUXDIS);
451
452
453     /* ------------------- keyboard side ------------------------*/
454     /* reset keyboard and self test  (keyboard side) */
455     int spinupdelay = romfile_loadint("etc/ps2-keyboard-spinup", 0);
456     u32 end = timer_calc(spinupdelay);
457     for (;;) {
458         ret = ps2_kbd_command(ATKBD_CMD_RESET_BAT, param);
459         if (!ret)
460             break;
461         if (timer_check(end)) {
462             if (spinupdelay)
463                 warn_timeout();
464             return;
465         }
466         yield();
467     }
468     if (param[0] != 0xaa) {
469         dprintf(1, "keyboard self test failed (got %x not 0xaa)\n", param[0]);
470         return;
471     }
472
473     /* Disable keyboard */
474     ret = ps2_kbd_command(ATKBD_CMD_RESET_DIS, NULL);
475     if (ret)
476         return;
477
478     // Set scancode command (mode 2)
479     param[0] = 0x02;
480     ret = ps2_kbd_command(ATKBD_CMD_SSCANSET, param);
481     if (ret)
482         return;
483
484     // Keyboard Mode: disable mouse, scan code convert, enable kbd IRQ
485     SET_LOW(Ps2ctr, I8042_CTR_AUXDIS | I8042_CTR_XLATE | I8042_CTR_KBDINT);
486
487     /* Enable keyboard */
488     ret = ps2_kbd_command(ATKBD_CMD_ENABLE, NULL);
489     if (ret)
490         return;
491
492     dprintf(1, "PS2 keyboard initialized\n");
493 }
494
495 void
496 ps2port_setup(void)
497 {
498     ASSERT32FLAT();
499     if (! CONFIG_PS2PORT)
500         return;
501     dprintf(3, "init ps2port\n");
502
503     enable_hwirq(1, FUNC16(entry_09));
504     enable_hwirq(12, FUNC16(entry_74));
505
506     run_thread(ps2_keyboard_setup, NULL);
507 }