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 "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
18 /****************************************************************
19 * Low level i8042 commands.
20 ****************************************************************/
23 #define I8042_CTL_TIMEOUT 10000
25 #define I8042_BUFFER_SIZE 16
30 dprintf(7, "i8042_wait_read\n");
32 for (i=0; i<I8042_CTL_TIMEOUT; i++) {
33 u8 status = inb(PORT_PS2_STATUS);
34 if (status & I8042_STR_OBF)
43 i8042_wait_write(void)
45 dprintf(7, "i8042_wait_write\n");
47 for (i=0; i<I8042_CTL_TIMEOUT; i++) {
48 u8 status = inb(PORT_PS2_STATUS);
49 if (! (status & I8042_STR_IBF))
60 dprintf(7, "i8042_flush\n");
62 for (i=0; i<I8042_BUFFER_SIZE; i++) {
63 u8 status = inb(PORT_PS2_STATUS);
64 if (! (status & I8042_STR_OBF))
67 u8 data = inb(PORT_PS2_DATA);
68 dprintf(7, "i8042 flushed %x (status=%x)\n", data, status);
76 __i8042_command(int command, u8 *param)
78 int receive = (command >> 8) & 0xf;
79 int send = (command >> 12) & 0xf;
82 int ret = i8042_wait_write();
85 outb(command, PORT_PS2_STATUS);
87 // Send parameters (if any).
89 for (i = 0; i < send; i++) {
90 ret = i8042_wait_write();
93 outb(param[i], PORT_PS2_DATA);
96 // Receive parameters (if any).
97 for (i = 0; i < receive; i++) {
98 ret = i8042_wait_read();
101 param[i] = inb(PORT_PS2_DATA);
102 dprintf(7, "i8042 param=%x\n", param[i]);
109 i8042_command(int command, u8 *param)
111 dprintf(7, "i8042_command cmd=%x\n", command);
112 int ret = __i8042_command(command, param);
114 dprintf(2, "i8042 command %x failed\n", command);
119 i8042_kbd_write(u8 c)
121 dprintf(7, "i8042_kbd_write c=%d\n", c);
122 int ret = i8042_wait_write();
124 outb(c, PORT_PS2_DATA);
129 i8042_aux_write(u8 c)
131 return i8042_command(I8042_CMD_AUX_SEND, &c);
137 if (! CONFIG_PS2PORT)
140 for (i=0; i<10; i++) {
143 outb(0xfe, PORT_PS2_STATUS); /* pulse reset low */
149 /****************************************************************
151 ****************************************************************/
153 #define PS2_RET_ACK 0xfa
154 #define PS2_RET_NAK 0xfe
157 ps2_recvbyte(int aux, int needack, int timeout)
159 u32 end = timer_calc(timeout);
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);
166 if (!!(status & I8042_STR_AUXDATA) == aux) {
169 if (data == PS2_RET_ACK)
171 if (data == PS2_RET_NAK) {
172 dprintf(1, "Got ps2 nak (status=%x)\n", status);
177 // This data not part of command - just discard it.
178 dprintf(1, "Discarding ps2 data %02x (status=%02x)\n", data, status);
181 if (timer_check(end)) {
182 // Don't warn on second byte of a reset
192 ps2_sendbyte(int aux, u8 command, int timeout)
194 dprintf(7, "ps2_sendbyte aux=%d cmd=%x\n", aux, command);
197 ret = i8042_aux_write(command);
199 ret = i8042_kbd_write(command);
204 ret = ps2_recvbyte(aux, 1, timeout);
207 if (ret != PS2_RET_ACK)
216 __ps2_command(int aux, int command, u8 *param)
219 int receive = (command >> 8) & 0xf;
220 int send = (command >> 12) & 0xf;
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);
231 // Flush any interrupts already pending.
234 // Enable port command is being sent to.
236 newctr &= ~I8042_CTR_AUXDIS;
238 newctr &= ~I8042_CTR_KBDDIS;
239 ret = i8042_command(I8042_CMD_CTL_WCTR, &newctr);
243 if (command == ATKBD_CMD_RESET_BAT) {
244 // Reset is special wrt timeouts and bytes received.
247 ret = ps2_sendbyte(aux, command, 1000);
251 // Receive parameters.
252 ret = ps2_recvbyte(aux, 0, 4000);
256 ret = ps2_recvbyte(aux, 0, 100);
258 // Some devices only respond with one byte on reset.
261 } else if (command == ATKBD_CMD_GETID) {
262 // Getid is special wrt bytes received.
265 ret = ps2_sendbyte(aux, command, 200);
269 // Receive parameters.
270 ret = ps2_recvbyte(aux, 0, 500);
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);
286 ret = ps2_sendbyte(aux, command, 200);
290 // Send parameters (if any).
292 for (i = 0; i < send; i++) {
293 ret = ps2_sendbyte(aux, param[i], 200);
298 // Receive parameters (if any).
299 for (i = 0; i < receive; i++) {
300 ret = ps2_recvbyte(aux, 0, 500);
310 // Restore interrupts and keyboard/mouse.
311 ret2 = i8042_command(I8042_CMD_CTL_WCTR, &ps2ctr);
319 ps2_command(int aux, int command, u8 *param)
321 dprintf(7, "ps2_command aux=%d cmd=%x\n", aux, command);
322 int ret = __ps2_command(aux, command, param);
324 dprintf(2, "ps2 command %x failed (aux=%d)\n", command, aux);
329 ps2_kbd_command(int command, u8 *param)
331 if (! CONFIG_PS2PORT)
333 return ps2_command(0, command, param);
337 ps2_mouse_command(int command, u8 *param)
339 if (! CONFIG_PS2PORT)
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;
348 ps2ctr = (ps2ctr | I8042_CTR_AUXDIS) & ~I8042_CTR_AUXINT;
349 SET_LOW(Ps2ctr, ps2ctr);
352 return ps2_command(1, command, param);
356 /****************************************************************
358 ****************************************************************/
360 // INT74h : PS/2 mouse hardware interrupt
364 if (! CONFIG_PS2PORT)
367 debug_isr(DEBUG_ISR_74);
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");
375 v = inb(PORT_PS2_DATA);
377 if (!(GET_LOW(Ps2ctr) & I8042_CTR_AUXINT))
378 // Interrupts not enabled.
387 // INT09h : Keyboard Hardware Service Entry Point
391 if (! CONFIG_PS2PORT)
394 debug_isr(DEBUG_ISR_09);
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");
402 v = inb(PORT_PS2_DATA);
404 if (!(GET_LOW(Ps2ctr) & I8042_CTR_KBDINT))
405 // Interrupts not enabled.
410 // Some old programs expect ISR to turn keyboard back on.
411 i8042_command(I8042_CMD_KBD_ENABLE, NULL);
418 /****************************************************************
420 ****************************************************************/
423 ps2_keyboard_setup(void *data)
425 /* flush incoming keys */
426 int ret = i8042_flush();
430 // Controller self-test.
432 ret = i8042_command(I8042_CMD_CTL_TEST, param);
435 if (param[0] != 0x55) {
436 dprintf(1, "i8042 self test failed (got %x not 0x55)\n", param[0]);
440 // Controller keyboard test.
441 ret = i8042_command(I8042_CMD_KBD_TEST, param);
444 if (param[0] != 0x00) {
445 dprintf(1, "i8042 keyboard test failed (got %x not 0x00)\n", param[0]);
449 // Disable keyboard and mouse events.
450 SET_LOW(Ps2ctr, I8042_CTR_KBDDIS | I8042_CTR_AUXDIS);
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);
458 ret = ps2_kbd_command(ATKBD_CMD_RESET_BAT, param);
461 if (timer_check(end)) {
468 if (param[0] != 0xaa) {
469 dprintf(1, "keyboard self test failed (got %x not 0xaa)\n", param[0]);
473 /* Disable keyboard */
474 ret = ps2_kbd_command(ATKBD_CMD_RESET_DIS, NULL);
478 // Set scancode command (mode 2)
480 ret = ps2_kbd_command(ATKBD_CMD_SSCANSET, param);
484 // Keyboard Mode: disable mouse, scan code convert, enable kbd IRQ
485 SET_LOW(Ps2ctr, I8042_CTR_AUXDIS | I8042_CTR_XLATE | I8042_CTR_KBDINT);
487 /* Enable keyboard */
488 ret = ps2_kbd_command(ATKBD_CMD_ENABLE, NULL);
492 dprintf(1, "PS2 keyboard initialized\n");
499 if (! CONFIG_PS2PORT)
501 dprintf(3, "init ps2port\n");
503 enable_hwirq(1, FUNC16(entry_09));
504 enable_hwirq(12, FUNC16(entry_74));
506 run_thread(ps2_keyboard_setup, NULL);