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