X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=src%2Fps2port.c;h=1f042999047ed818e7a5b458c0c03716b9b4cc0d;hb=e216ce8fb5470a375756574d65723cbf764a2919;hp=3f2fee7796a79840911caa520d0f2e56fe324328;hpb=b1b7c2a1c3b940b787fdb5da67fc37003e9d7b0e;p=seabios.git diff --git a/src/ps2port.c b/src/ps2port.c index 3f2fee7..1f04299 100644 --- a/src/ps2port.c +++ b/src/ps2port.c @@ -7,8 +7,10 @@ #include "ioport.h" // inb #include "util.h" // dprintf +#include "paravirt.h" // romfile_loadint #include "biosvar.h" // GET_EBDA -#include "ps2port.h" // kbd_command +#include "ps2port.h" // ps2_kbd_command +#include "pic.h" // eoi_pic1 /**************************************************************** @@ -31,7 +33,7 @@ i8042_wait_read(void) return 0; udelay(50); } - dprintf(1, "i8042 timeout on wait read\n"); + warn_timeout(); return -1; } @@ -46,30 +48,25 @@ i8042_wait_write(void) return 0; udelay(50); } - dprintf(1, "i8042 timeout on wait write\n"); + warn_timeout(); return -1; } -int +static int i8042_flush(void) { dprintf(7, "i8042_flush\n"); - unsigned long flags = irq_save(); - int i; for (i=0; i= end) { - dprintf(1, "ps2_recvbyte timeout\n"); - return -1; - } - u8 status = inb(PORT_PS2_STATUS); - if (! (status & I8042_STR_OBF)) - continue; - u8 data = inb(PORT_PS2_DATA); - dprintf(7, "ps2 read %x\n", data); - - if ((!!(status & I8042_STR_AUXDATA) != aux) - || (needack && data != PS2_RET_ACK)) { - // This data not for us - XXX - just discard it for now. - dprintf(1, "Discarding ps2 data %x\n", data); - continue; + if (status & I8042_STR_OBF) { + u8 data = inb(PORT_PS2_DATA); + dprintf(7, "ps2 read %x\n", data); + + if (!!(status & I8042_STR_AUXDATA) == aux) { + if (!needack) + return data; + if (data == PS2_RET_ACK) + return data; + if (data == PS2_RET_NAK) { + dprintf(1, "Got ps2 nak (status=%x)\n", status); + return data; + } + } + + // This data not part of command - just discard it. + dprintf(1, "Discarding ps2 data %02x (status=%02x)\n", data, status); } - return data; + if (check_tsc(end)) { + // Don't warn on second byte of a reset + if (timeout > 100) + warn_timeout(); + return -1; + } + yield(); } } static int -ps2_sendbyte(int aux, u8 command) +ps2_sendbyte(int aux, u8 command, int timeout) { dprintf(7, "ps2_sendbyte aux=%d cmd=%x\n", aux, command); int ret; @@ -187,15 +199,17 @@ ps2_sendbyte(int aux, u8 command) return ret; // Read ack. - ret = ps2_recvbyte(aux, 1, 200); + ret = ps2_recvbyte(aux, 1, timeout); if (ret < 0) return ret; + if (ret != PS2_RET_ACK) + return -1; return 0; } static int -ps2_command(int aux, int command, u8 *param) +__ps2_command(int aux, int command, u8 *param) { int ret2; int receive = (command >> 8) & 0xf; @@ -203,42 +217,91 @@ ps2_command(int aux, int command, u8 *param) // Disable interrupts and keyboard/mouse. u8 ps2ctr = GET_EBDA(ps2ctr); - u8 newctr = ps2ctr; - if (aux) - newctr |= I8042_CTR_KBDDIS; - else - newctr |= I8042_CTR_AUXDIS; - newctr &= ~(I8042_CTR_KBDINT|I8042_CTR_AUXINT); + u8 newctr = ((ps2ctr | I8042_CTR_AUXDIS | I8042_CTR_KBDDIS) + & ~(I8042_CTR_KBDINT|I8042_CTR_AUXINT)); dprintf(6, "i8042 ctr old=%x new=%x\n", ps2ctr, newctr); int ret = i8042_command(I8042_CMD_CTL_WCTR, &newctr); if (ret) return ret; - // Send command. - ret = ps2_sendbyte(aux, command); + // Flush any interrupts already pending. + yield(); + + // Enable port command is being sent to. + if (aux) + newctr &= ~I8042_CTR_AUXDIS; + else + newctr &= ~I8042_CTR_KBDDIS; + ret = i8042_command(I8042_CMD_CTL_WCTR, &newctr); if (ret) goto fail; - // Send parameters (if any). - int i; - for (i = 0; i < send; i++) { - ret = ps2_sendbyte(aux, param[i]); + if (command == ATKBD_CMD_RESET_BAT) { + // Reset is special wrt timeouts and bytes received. + + // Send command. + ret = ps2_sendbyte(aux, command, 1000); if (ret) goto fail; - } - // Receive parameters (if any). - for (i = 0; i < receive; i++) { - u8 data = ps2_recvbyte(aux, 0, 200); - if (data < 0) { - // On a receive timeout, return the item number that the - // transfer failed on. - ret = i + 1; + // Receive parameters. + ret = ps2_recvbyte(aux, 0, 4000); + if (ret < 0) + goto fail; + param[0] = ret; + ret = ps2_recvbyte(aux, 0, 100); + if (ret < 0) + // Some devices only respond with one byte on reset. + ret = 0; + param[1] = ret; + } else if (command == ATKBD_CMD_GETID) { + // Getid is special wrt bytes received. + + // Send command. + ret = ps2_sendbyte(aux, command, 200); + if (ret) + goto fail; + + // Receive parameters. + ret = ps2_recvbyte(aux, 0, 500); + if (ret < 0) + goto fail; + param[0] = ret; + if (ret == 0xab || ret == 0xac || ret == 0x2b || ret == 0x5d + || ret == 0x60 || ret == 0x47) { + // These ids (keyboards) return two bytes. + ret = ps2_recvbyte(aux, 0, 500); + if (ret < 0) + goto fail; + param[1] = ret; + } else { + param[1] = 0; + } + } else { + // Send command. + ret = ps2_sendbyte(aux, command, 200); + if (ret) goto fail; + + // Send parameters (if any). + int i; + for (i = 0; i < send; i++) { + ret = ps2_sendbyte(aux, param[i], 200); + if (ret) + goto fail; + } + + // Receive parameters (if any). + for (i = 0; i < receive; i++) { + ret = ps2_recvbyte(aux, 0, 500); + if (ret < 0) + goto fail; + param[i] = ret; } - param[i] = data; } + ret = 0; + fail: // Restore interrupts and keyboard/mouse. ret2 = i8042_command(I8042_CMD_CTL_WCTR, &ps2ctr); @@ -248,22 +311,191 @@ fail: return ret; } -int -kbd_command(int command, u8 *param) +static int +ps2_command(int aux, int command, u8 *param) { - dprintf(7, "kbd_command cmd=%x\n", command); - int ret = ps2_command(0, command, param); + dprintf(7, "ps2_command aux=%d cmd=%x\n", aux, command); + int ret = __ps2_command(aux, command, param); if (ret) - dprintf(2, "keyboard command %x failed (ret=%d)\n", command, ret); + dprintf(2, "ps2 command %x failed (aux=%d)\n", command, aux); return ret; } int -aux_command(int command, u8 *param) +ps2_kbd_command(int command, u8 *param) +{ + if (! CONFIG_PS2PORT) + return -1; + return ps2_command(0, command, param); +} + +int +ps2_mouse_command(int command, u8 *param) +{ + if (! CONFIG_PS2PORT) + return -1; + + // Update ps2ctr for mouse enable/disable. + if (command == PSMOUSE_CMD_ENABLE || command == PSMOUSE_CMD_DISABLE) { + u16 ebda_seg = get_ebda_seg(); + u8 ps2ctr = GET_EBDA2(ebda_seg, ps2ctr); + if (command == PSMOUSE_CMD_ENABLE) + ps2ctr = (ps2ctr | I8042_CTR_AUXINT) & ~I8042_CTR_AUXDIS; + else + ps2ctr = (ps2ctr | I8042_CTR_AUXDIS) & ~I8042_CTR_AUXINT; + SET_EBDA2(ebda_seg, ps2ctr, ps2ctr); + } + + return ps2_command(1, command, param); +} + + +/**************************************************************** + * IRQ handlers + ****************************************************************/ + +// INT74h : PS/2 mouse hardware interrupt +void VISIBLE16 +handle_74(void) { - dprintf(7, "aux_command cmd=%x\n", command); - int ret = ps2_command(1, command, param); + if (! CONFIG_PS2PORT) + return; + + debug_isr(DEBUG_ISR_74); + + u8 v = inb(PORT_PS2_STATUS); + if ((v & (I8042_STR_OBF|I8042_STR_AUXDATA)) + != (I8042_STR_OBF|I8042_STR_AUXDATA)) { + dprintf(1, "ps2 mouse irq but no mouse data.\n"); + goto done; + } + v = inb(PORT_PS2_DATA); + + if (!(GET_EBDA(ps2ctr) & I8042_CTR_AUXINT)) + // Interrupts not enabled. + goto done; + + process_mouse(v); + +done: + eoi_pic2(); +} + +// INT09h : Keyboard Hardware Service Entry Point +void VISIBLE16 +handle_09(void) +{ + if (! CONFIG_PS2PORT) + return; + + debug_isr(DEBUG_ISR_09); + + // read key from keyboard controller + u8 v = inb(PORT_PS2_STATUS); + if (v & I8042_STR_AUXDATA) { + dprintf(1, "ps2 keyboard irq but found mouse data?!\n"); + goto done; + } + v = inb(PORT_PS2_DATA); + + if (!(GET_EBDA(ps2ctr) & I8042_CTR_KBDINT)) + // Interrupts not enabled. + goto done; + + process_key(v); + +done: + eoi_pic1(); +} + + +/**************************************************************** + * Setup + ****************************************************************/ + +static void +keyboard_init(void *data) +{ + /* flush incoming keys */ + int ret = i8042_flush(); if (ret) - dprintf(2, "mouse command %x failed (ret=%d)\n", command, ret); - return ret; + return; + + // Controller self-test. + u8 param[2]; + ret = i8042_command(I8042_CMD_CTL_TEST, param); + if (ret) + return; + if (param[0] != 0x55) { + dprintf(1, "i8042 self test failed (got %x not 0x55)\n", param[0]); + return; + } + + // Controller keyboard test. + ret = i8042_command(I8042_CMD_KBD_TEST, param); + if (ret) + return; + if (param[0] != 0x00) { + dprintf(1, "i8042 keyboard test failed (got %x not 0x00)\n", param[0]); + return; + } + + // Disable keyboard and mouse events. + SET_EBDA(ps2ctr, I8042_CTR_KBDDIS | I8042_CTR_AUXDIS); + + + /* ------------------- keyboard side ------------------------*/ + /* reset keyboard and self test (keyboard side) */ + int spinupdelay = romfile_loadint("etc/ps2-keyboard-spinup", 0); + u64 end = calc_future_tsc(spinupdelay); + for (;;) { + ret = ps2_kbd_command(ATKBD_CMD_RESET_BAT, param); + if (!ret) + break; + if (check_tsc(end)) { + if (spinupdelay) + warn_timeout(); + return; + } + yield(); + } + if (param[0] != 0xaa) { + dprintf(1, "keyboard self test failed (got %x not 0xaa)\n", param[0]); + return; + } + + /* Disable keyboard */ + ret = ps2_kbd_command(ATKBD_CMD_RESET_DIS, NULL); + if (ret) + return; + + // Set scancode command (mode 2) + param[0] = 0x02; + ret = ps2_kbd_command(ATKBD_CMD_SSCANSET, param); + if (ret) + return; + + // Keyboard Mode: disable mouse, scan code convert, enable kbd IRQ + SET_EBDA(ps2ctr, I8042_CTR_AUXDIS | I8042_CTR_XLATE | I8042_CTR_KBDINT); + + /* Enable keyboard */ + ret = ps2_kbd_command(ATKBD_CMD_ENABLE, NULL); + if (ret) + return; + + dprintf(1, "PS2 keyboard initialized\n"); +} + +void +ps2port_setup(void) +{ + ASSERT32FLAT(); + if (! CONFIG_PS2PORT) + return; + dprintf(3, "init ps2port\n"); + + enable_hwirq(1, FUNC16(entry_09)); + enable_hwirq(12, FUNC16(entry_74)); + + run_thread(keyboard_init, NULL); }