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)
213 u8 Ps2ctr VARLOW = I8042_CTR_KBDDIS | I8042_CTR_AUXDIS;
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.
235 SET_LOW(Ps2ctr, newctr);
237 newctr &= ~I8042_CTR_AUXDIS;
239 newctr &= ~I8042_CTR_KBDDIS;
240 ret = i8042_command(I8042_CMD_CTL_WCTR, &newctr);
244 if ((u8)command == (u8)ATKBD_CMD_RESET_BAT) {
245 // Reset is special wrt timeouts.
248 ret = ps2_sendbyte(aux, command, 1000);
252 // Receive parameters.
253 ret = ps2_recvbyte(aux, 0, 4000);
258 ret = ps2_recvbyte(aux, 0, 500);
263 } else if (command == ATKBD_CMD_GETID) {
264 // Getid is special wrt bytes received.
267 ret = ps2_sendbyte(aux, command, 200);
271 // Receive parameters.
272 ret = ps2_recvbyte(aux, 0, 500);
276 if (ret == 0xab || ret == 0xac || ret == 0x2b || ret == 0x5d
277 || ret == 0x60 || ret == 0x47) {
278 // These ids (keyboards) return two bytes.
279 ret = ps2_recvbyte(aux, 0, 500);
288 ret = ps2_sendbyte(aux, command, 200);
292 // Send parameters (if any).
294 for (i = 0; i < send; i++) {
295 ret = ps2_sendbyte(aux, param[i], 200);
300 // Receive parameters (if any).
301 for (i = 0; i < receive; i++) {
302 ret = ps2_recvbyte(aux, 0, 500);
312 // Restore interrupts and keyboard/mouse.
313 SET_LOW(Ps2ctr, ps2ctr);
314 ret2 = i8042_command(I8042_CMD_CTL_WCTR, &ps2ctr);
322 ps2_command(int aux, int command, u8 *param)
324 dprintf(7, "ps2_command aux=%d cmd=%x\n", aux, command);
325 int ret = __ps2_command(aux, command, param);
327 dprintf(2, "ps2 command %x failed (aux=%d)\n", command, aux);
332 ps2_kbd_command(int command, u8 *param)
334 if (! CONFIG_PS2PORT)
336 return ps2_command(0, command, param);
340 ps2_mouse_command(int command, u8 *param)
342 if (! CONFIG_PS2PORT)
345 // Update ps2ctr for mouse enable/disable.
346 if (command == PSMOUSE_CMD_ENABLE || command == PSMOUSE_CMD_DISABLE) {
347 u8 ps2ctr = GET_LOW(Ps2ctr);
348 if (command == PSMOUSE_CMD_ENABLE)
349 ps2ctr = ((ps2ctr | (CONFIG_HARDWARE_IRQ ? I8042_CTR_AUXINT : 0))
350 & ~I8042_CTR_AUXDIS);
352 ps2ctr = (ps2ctr | I8042_CTR_AUXDIS) & ~I8042_CTR_AUXINT;
353 SET_LOW(Ps2ctr, ps2ctr);
356 return ps2_command(1, command, param);
360 /****************************************************************
362 ****************************************************************/
364 // INT74h : PS/2 mouse hardware interrupt
368 if (! CONFIG_PS2PORT)
371 debug_isr(DEBUG_ISR_74);
373 u8 v = inb(PORT_PS2_STATUS);
374 if ((v & (I8042_STR_OBF|I8042_STR_AUXDATA))
375 != (I8042_STR_OBF|I8042_STR_AUXDATA)) {
376 dprintf(1, "ps2 mouse irq but no mouse data.\n");
379 v = inb(PORT_PS2_DATA);
381 if (!(GET_LOW(Ps2ctr) & I8042_CTR_AUXINT))
382 // Interrupts not enabled.
391 // INT09h : Keyboard Hardware Service Entry Point
395 if (! CONFIG_PS2PORT)
398 debug_isr(DEBUG_ISR_09);
400 // read key from keyboard controller
401 u8 v = inb(PORT_PS2_STATUS);
402 if (v & I8042_STR_AUXDATA) {
403 dprintf(1, "ps2 keyboard irq but found mouse data?!\n");
406 v = inb(PORT_PS2_DATA);
408 if (!(GET_LOW(Ps2ctr) & I8042_CTR_KBDINT))
409 // Interrupts not enabled.
414 // Some old programs expect ISR to turn keyboard back on.
415 i8042_command(I8042_CMD_KBD_ENABLE, NULL);
421 // Check for ps2 activity on machines without hardware irqs
423 ps2_check_event(void)
425 if (! CONFIG_PS2PORT || CONFIG_HARDWARE_IRQ)
427 u8 ps2ctr = GET_LOW(Ps2ctr);
428 if ((ps2ctr & (I8042_CTR_KBDDIS|I8042_CTR_AUXDIS))
429 == (I8042_CTR_KBDDIS|I8042_CTR_AUXDIS))
432 u8 status = inb(PORT_PS2_STATUS);
433 if (!(status & I8042_STR_OBF))
435 u8 data = inb(PORT_PS2_DATA);
436 if (status & I8042_STR_AUXDATA) {
437 if (!(ps2ctr & I8042_CTR_AUXDIS))
440 if (!(ps2ctr & I8042_CTR_KBDDIS))
447 /****************************************************************
449 ****************************************************************/
452 ps2_keyboard_setup(void *data)
454 /* flush incoming keys */
455 int ret = i8042_flush();
459 // Controller self-test.
461 ret = i8042_command(I8042_CMD_CTL_TEST, param);
464 if (param[0] != 0x55) {
465 dprintf(1, "i8042 self test failed (got %x not 0x55)\n", param[0]);
469 // Controller keyboard test.
470 ret = i8042_command(I8042_CMD_KBD_TEST, param);
473 if (param[0] != 0x00) {
474 dprintf(1, "i8042 keyboard test failed (got %x not 0x00)\n", param[0]);
479 /* ------------------- keyboard side ------------------------*/
480 /* reset keyboard and self test (keyboard side) */
481 int spinupdelay = romfile_loadint("etc/ps2-keyboard-spinup", 0);
482 u32 end = timer_calc(spinupdelay);
484 ret = ps2_kbd_command(ATKBD_CMD_RESET_BAT, param);
487 if (timer_check(end)) {
494 if (param[0] != 0xaa) {
495 dprintf(1, "keyboard self test failed (got %x not 0xaa)\n", param[0]);
499 /* Disable keyboard */
500 ret = ps2_kbd_command(ATKBD_CMD_RESET_DIS, NULL);
504 // Set scancode command (mode 2)
506 ret = ps2_kbd_command(ATKBD_CMD_SSCANSET, param);
510 // Keyboard Mode: disable mouse, scan code convert, enable kbd IRQ
511 Ps2ctr = (I8042_CTR_AUXDIS | I8042_CTR_XLATE
512 | (CONFIG_HARDWARE_IRQ ? I8042_CTR_KBDINT : 0));
514 /* Enable keyboard */
515 ret = ps2_kbd_command(ATKBD_CMD_ENABLE, NULL);
519 dprintf(1, "PS2 keyboard initialized\n");
526 if (! CONFIG_PS2PORT)
528 dprintf(3, "init ps2port\n");
530 enable_hwirq(1, FUNC16(entry_09));
531 enable_hwirq(12, FUNC16(entry_74));
533 run_thread(ps2_keyboard_setup, NULL);