1 // Support for handling the PS/2 mouse/keyboard ports.
3 // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4 // Several ideas taken from code Copyright (c) 1999-2004 Vojtech Pavlik
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
8 #include "ioport.h" // inb
9 #include "util.h" // dprintf
10 #include "paravirt.h" // romfile_loadint
11 #include "biosvar.h" // GET_EBDA
12 #include "ps2port.h" // ps2_kbd_command
13 #include "pic.h" // eoi_pic1
16 /****************************************************************
17 * Low level i8042 commands.
18 ****************************************************************/
21 #define I8042_CTL_TIMEOUT 10000
23 #define I8042_BUFFER_SIZE 16
28 dprintf(7, "i8042_wait_read\n");
30 for (i=0; i<I8042_CTL_TIMEOUT; i++) {
31 u8 status = inb(PORT_PS2_STATUS);
32 if (status & I8042_STR_OBF)
41 i8042_wait_write(void)
43 dprintf(7, "i8042_wait_write\n");
45 for (i=0; i<I8042_CTL_TIMEOUT; i++) {
46 u8 status = inb(PORT_PS2_STATUS);
47 if (! (status & I8042_STR_IBF))
58 dprintf(7, "i8042_flush\n");
60 for (i=0; i<I8042_BUFFER_SIZE; i++) {
61 u8 status = inb(PORT_PS2_STATUS);
62 if (! (status & I8042_STR_OBF))
65 u8 data = inb(PORT_PS2_DATA);
66 dprintf(7, "i8042 flushed %x (status=%x)\n", data, status);
74 __i8042_command(int command, u8 *param)
76 int receive = (command >> 8) & 0xf;
77 int send = (command >> 12) & 0xf;
80 int ret = i8042_wait_write();
83 outb(command, PORT_PS2_STATUS);
85 // Send parameters (if any).
87 for (i = 0; i < send; i++) {
88 ret = i8042_wait_write();
91 outb(param[i], PORT_PS2_DATA);
94 // Receive parameters (if any).
95 for (i = 0; i < receive; i++) {
96 ret = i8042_wait_read();
99 param[i] = inb(PORT_PS2_DATA);
100 dprintf(7, "i8042 param=%x\n", param[i]);
107 i8042_command(int command, u8 *param)
109 dprintf(7, "i8042_command cmd=%x\n", command);
110 int ret = __i8042_command(command, param);
112 dprintf(2, "i8042 command %x failed\n", command);
117 i8042_kbd_write(u8 c)
119 dprintf(7, "i8042_kbd_write c=%d\n", c);
120 int ret = i8042_wait_write();
122 outb(c, PORT_PS2_DATA);
127 i8042_aux_write(u8 c)
129 return i8042_command(I8042_CMD_AUX_SEND, &c);
136 for (i=0; i<10; i++) {
139 outb(0xfe, PORT_PS2_STATUS); /* pulse reset low */
145 /****************************************************************
147 ****************************************************************/
149 #define PS2_RET_ACK 0xfa
150 #define PS2_RET_NAK 0xfe
153 ps2_recvbyte(int aux, int needack, int timeout)
155 u64 end = calc_future_tsc(timeout);
157 u8 status = inb(PORT_PS2_STATUS);
158 if (status & I8042_STR_OBF) {
159 u8 data = inb(PORT_PS2_DATA);
160 dprintf(7, "ps2 read %x\n", data);
162 if (!!(status & I8042_STR_AUXDATA) == aux) {
165 if (data == PS2_RET_ACK)
167 if (data == PS2_RET_NAK) {
168 dprintf(1, "Got ps2 nak (status=%x)\n", status);
173 // This data not part of command - just discard it.
174 dprintf(1, "Discarding ps2 data %02x (status=%02x)\n", data, status);
177 if (check_tsc(end)) {
178 // Don't warn on second byte of a reset
188 ps2_sendbyte(int aux, u8 command, int timeout)
190 dprintf(7, "ps2_sendbyte aux=%d cmd=%x\n", aux, command);
193 ret = i8042_aux_write(command);
195 ret = i8042_kbd_write(command);
200 ret = ps2_recvbyte(aux, 1, timeout);
203 if (ret != PS2_RET_ACK)
210 __ps2_command(int aux, int command, u8 *param)
213 int receive = (command >> 8) & 0xf;
214 int send = (command >> 12) & 0xf;
216 // Disable interrupts and keyboard/mouse.
217 u8 ps2ctr = GET_EBDA(ps2ctr);
218 u8 newctr = ((ps2ctr | I8042_CTR_AUXDIS | I8042_CTR_KBDDIS)
219 & ~(I8042_CTR_KBDINT|I8042_CTR_AUXINT));
220 dprintf(6, "i8042 ctr old=%x new=%x\n", ps2ctr, newctr);
221 int ret = i8042_command(I8042_CMD_CTL_WCTR, &newctr);
225 // Flush any interrupts already pending.
228 // Enable port command is being sent to.
230 newctr &= ~I8042_CTR_AUXDIS;
232 newctr &= ~I8042_CTR_KBDDIS;
233 ret = i8042_command(I8042_CMD_CTL_WCTR, &newctr);
237 if (command == ATKBD_CMD_RESET_BAT) {
238 // Reset is special wrt timeouts and bytes received.
241 ret = ps2_sendbyte(aux, command, 1000);
245 // Receive parameters.
246 ret = ps2_recvbyte(aux, 0, 4000);
250 ret = ps2_recvbyte(aux, 0, 100);
252 // Some devices only respond with one byte on reset.
255 } else if (command == ATKBD_CMD_GETID) {
256 // Getid is special wrt bytes received.
259 ret = ps2_sendbyte(aux, command, 200);
263 // Receive parameters.
264 ret = ps2_recvbyte(aux, 0, 500);
268 if (ret == 0xab || ret == 0xac || ret == 0x2b || ret == 0x5d
269 || ret == 0x60 || ret == 0x47) {
270 // These ids (keyboards) return two bytes.
271 ret = ps2_recvbyte(aux, 0, 500);
280 ret = ps2_sendbyte(aux, command, 200);
284 // Send parameters (if any).
286 for (i = 0; i < send; i++) {
287 ret = ps2_sendbyte(aux, param[i], 200);
292 // Receive parameters (if any).
293 for (i = 0; i < receive; i++) {
294 ret = ps2_recvbyte(aux, 0, 500);
304 // Restore interrupts and keyboard/mouse.
305 ret2 = i8042_command(I8042_CMD_CTL_WCTR, &ps2ctr);
313 ps2_command(int aux, int command, u8 *param)
315 dprintf(7, "ps2_command aux=%d cmd=%x\n", aux, command);
316 int ret = __ps2_command(aux, command, param);
318 dprintf(2, "ps2 command %x failed (aux=%d)\n", command, aux);
323 ps2_kbd_command(int command, u8 *param)
325 return ps2_command(0, command, param);
329 ps2_mouse_command(int command, u8 *param)
331 // Update ps2ctr for mouse enable/disable.
332 if (command == PSMOUSE_CMD_ENABLE || command == PSMOUSE_CMD_DISABLE) {
333 u16 ebda_seg = get_ebda_seg();
334 u8 ps2ctr = GET_EBDA2(ebda_seg, ps2ctr);
335 if (command == PSMOUSE_CMD_ENABLE)
336 ps2ctr = (ps2ctr | I8042_CTR_AUXINT) & ~I8042_CTR_AUXDIS;
338 ps2ctr = (ps2ctr | I8042_CTR_AUXDIS) & ~I8042_CTR_AUXINT;
339 SET_EBDA2(ebda_seg, ps2ctr, ps2ctr);
342 return ps2_command(1, command, param);
346 /****************************************************************
348 ****************************************************************/
350 // INT74h : PS/2 mouse hardware interrupt
354 if (! CONFIG_PS2PORT)
357 debug_isr(DEBUG_ISR_74);
359 u8 v = inb(PORT_PS2_STATUS);
360 if ((v & (I8042_STR_OBF|I8042_STR_AUXDATA))
361 != (I8042_STR_OBF|I8042_STR_AUXDATA)) {
362 dprintf(1, "ps2 mouse irq but no mouse data.\n");
365 v = inb(PORT_PS2_DATA);
367 if (!(GET_EBDA(ps2ctr) & I8042_CTR_AUXINT))
368 // Interrupts not enabled.
377 // INT09h : Keyboard Hardware Service Entry Point
381 if (! CONFIG_PS2PORT)
384 debug_isr(DEBUG_ISR_09);
386 // read key from keyboard controller
387 u8 v = inb(PORT_PS2_STATUS);
388 if (v & I8042_STR_AUXDATA) {
389 dprintf(1, "ps2 keyboard irq but found mouse data?!\n");
392 v = inb(PORT_PS2_DATA);
394 if (!(GET_EBDA(ps2ctr) & I8042_CTR_KBDINT))
395 // Interrupts not enabled.
405 /****************************************************************
407 ****************************************************************/
410 keyboard_init(void *data)
412 /* flush incoming keys */
413 int ret = i8042_flush();
417 // Controller self-test.
419 ret = i8042_command(I8042_CMD_CTL_TEST, param);
422 if (param[0] != 0x55) {
423 dprintf(1, "i8042 self test failed (got %x not 0x55)\n", param[0]);
427 // Controller keyboard test.
428 ret = i8042_command(I8042_CMD_KBD_TEST, param);
431 if (param[0] != 0x00) {
432 dprintf(1, "i8042 keyboard test failed (got %x not 0x00)\n", param[0]);
436 // Disable keyboard and mouse events.
437 SET_EBDA(ps2ctr, I8042_CTR_KBDDIS | I8042_CTR_AUXDIS);
440 /* ------------------- keyboard side ------------------------*/
441 /* reset keyboard and self test (keyboard side) */
442 int spinupdelay = romfile_loadint("etc/ps2-keyboard-spinup", 0);
443 u64 end = calc_future_tsc(spinupdelay);
445 ret = ps2_kbd_command(ATKBD_CMD_RESET_BAT, param);
448 if (check_tsc(end)) {
455 if (param[0] != 0xaa) {
456 dprintf(1, "keyboard self test failed (got %x not 0xaa)\n", param[0]);
460 /* Disable keyboard */
461 ret = ps2_kbd_command(ATKBD_CMD_RESET_DIS, NULL);
465 // Set scancode command (mode 2)
467 ret = ps2_kbd_command(ATKBD_CMD_SSCANSET, param);
471 // Keyboard Mode: disable mouse, scan code convert, enable kbd IRQ
472 SET_EBDA(ps2ctr, I8042_CTR_AUXDIS | I8042_CTR_XLATE | I8042_CTR_KBDINT);
474 /* Enable keyboard */
475 ret = ps2_kbd_command(ATKBD_CMD_ENABLE, NULL);
479 dprintf(1, "PS2 keyboard initialized\n");
486 if (! CONFIG_PS2PORT)
488 dprintf(3, "init ps2port\n");
490 enable_hwirq(1, FUNC16(entry_09));
491 enable_hwirq(12, FUNC16(entry_74));
493 run_thread(keyboard_init, NULL);