Rework disabling of ps2 port irqs.
[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" // 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     dprintf(1, "i8042 timeout on wait read\n");
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     dprintf(1, "i8042 timeout on wait write\n");
51     return -1;
52 }
53
54 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     dprintf(1, "i8042 timeout on flush\n");
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 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
132 /****************************************************************
133  * Device commands.
134  ****************************************************************/
135
136 #define PS2_RET_ACK             0xfa
137 #define PS2_RET_NAK             0xfe
138
139 static void
140 process_ps2byte(u8 status, u8 data)
141 {
142     if (!MODE16) {
143         // Don't pull in all of keyboard/mouse code into 32bit code -
144         // just discard the data.
145         dprintf(1, "Discarding ps2 data %x (status=%x)\n", data, status);
146         return;
147     }
148     if (status & I8042_STR_AUXDATA)
149         process_mouse(data);
150     else
151         process_key(data);
152 }
153
154 static void
155 process_ps2bytes(void)
156 {
157     for (;;) {
158         u8 status = inb(PORT_PS2_STATUS);
159         if (!(status & I8042_STR_OBF))
160             return;
161         u8 data = inb(PORT_PS2_DATA);
162         process_ps2byte(status, data);
163     }
164 }
165
166 static int
167 ps2_recvbyte(int aux, int needack, int timeout)
168 {
169     u64 end = calc_future_tsc(timeout);
170     for (;;) {
171         u8 status = inb(PORT_PS2_STATUS);
172         if (status & I8042_STR_OBF) {
173             u8 data = inb(PORT_PS2_DATA);
174             dprintf(7, "ps2 read %x\n", data);
175
176             if (!!(status & I8042_STR_AUXDATA) == aux) {
177                 if (!needack)
178                     return data;
179                 if (data == PS2_RET_ACK)
180                     return data;
181                 if (data == PS2_RET_NAK) {
182                     dprintf(1, "Got ps2 nak (status=%x)\n", status);
183                     return data;
184                 }
185             }
186
187             // Data not part of this command.
188             process_ps2byte(status, data);
189         }
190
191         if (check_time(end)) {
192             dprintf(1, "ps2_recvbyte timeout\n");
193             return -1;
194         }
195         yield();
196     }
197 }
198
199 static int
200 ps2_sendbyte(int aux, u8 command, int timeout)
201 {
202     dprintf(7, "ps2_sendbyte aux=%d cmd=%x\n", aux, command);
203     int ret;
204     if (aux)
205         ret = i8042_aux_write(command);
206     else
207         ret = i8042_kbd_write(command);
208     if (ret)
209         return ret;
210
211     // Read ack.
212     ret = ps2_recvbyte(aux, 1, timeout);
213     if (ret < 0)
214         return ret;
215     if (ret != PS2_RET_ACK)
216         return -1;
217
218     return 0;
219 }
220
221 static int
222 ps2_command(int aux, int command, u8 *param)
223 {
224     int ret;
225     int receive = (command >> 8) & 0xf;
226     int send = (command >> 12) & 0xf;
227
228     // Disable processing of interrupts.
229     u8 kbdflag = GET_BDA(kbd_flag3);
230     SET_BDA(kbd_flag3, kbdflag | KF3_CMD_PENDING);
231
232     if (command == ATKBD_CMD_RESET_BAT) {
233         // Reset is special wrt timeouts.
234
235         // Send command.
236         ret = ps2_sendbyte(aux, command, 1000);
237         if (ret)
238             goto fail;
239
240         // Receive parameters.
241         ret = ps2_recvbyte(aux, 0, 4000);
242         if (ret < 0)
243             goto fail;
244         param[0] = ret;
245         ret = ps2_recvbyte(aux, 0, 100);
246         if (ret < 0)
247             // Some devices only respond with one byte on reset.
248             ret = 0;
249         param[1] = ret;
250     } else {
251         // Send command.
252         ret = ps2_sendbyte(aux, command, 200);
253         if (ret)
254             goto fail;
255
256         // Send parameters (if any).
257         int i;
258         for (i = 0; i < send; i++) {
259             ret = ps2_sendbyte(aux, param[i], 200);
260             if (ret)
261                 goto fail;
262         }
263
264         // Receive parameters (if any).
265         for (i = 0; i < receive; i++) {
266             ret = ps2_recvbyte(aux, 0, 500);
267             if (ret < 0)
268                 goto fail;
269             param[i] = ret;
270         }
271     }
272
273     ret = 0;
274
275 fail:
276     // Restore processing of interrupts.
277     if (!(kbdflag & KF3_CMD_PENDING))
278         process_ps2bytes();
279     SET_BDA(kbd_flag3, kbdflag);
280
281     return ret;
282 }
283
284 int
285 kbd_command(int command, u8 *param)
286 {
287     dprintf(7, "kbd_command cmd=%x\n", command);
288     int ret = ps2_command(0, command, param);
289     if (ret)
290         dprintf(2, "keyboard command %x failed\n", command);
291     return ret;
292 }
293
294 int
295 aux_command(int command, u8 *param)
296 {
297     dprintf(7, "aux_command cmd=%x\n", command);
298     int ret = ps2_command(1, command, param);
299     if (ret)
300         dprintf(2, "mouse command %x failed\n", command);
301     return ret;
302 }
303
304
305 /****************************************************************
306  * IRQ handlers
307  ****************************************************************/
308
309 static void
310 process_ps2irq(void)
311 {
312     if (GET_BDA(kbd_flag3) & KF3_CMD_PENDING)
313         // PS/2 command in progress - it will handle this event.
314         return;
315     process_ps2bytes();
316 }
317
318 // INT74h : PS/2 mouse hardware interrupt
319 void VISIBLE16
320 handle_74(void)
321 {
322     if (! CONFIG_PS2PORT)
323         return;
324
325     debug_isr(DEBUG_ISR_74);
326     process_ps2irq();
327     eoi_pic2();
328 }
329
330 // INT09h : Keyboard Hardware Service Entry Point
331 void VISIBLE16
332 handle_09(void)
333 {
334     if (! CONFIG_PS2PORT)
335         return;
336
337     debug_isr(DEBUG_ISR_09);
338     process_ps2irq();
339     eoi_pic1();
340 }
341
342
343 /****************************************************************
344  * Setup
345  ****************************************************************/
346
347 static void
348 keyboard_init(void *data)
349 {
350     /* flush incoming keys */
351     int ret = i8042_flush();
352     if (ret)
353         return;
354
355     // Controller self-test.
356     u8 param[2];
357     ret = i8042_command(I8042_CMD_CTL_TEST, param);
358     if (ret)
359         return;
360     if (param[0] != 0x55) {
361         dprintf(1, "i8042 self test failed (got %x not 0x55)\n", param[0]);
362         return;
363     }
364
365     // Controller keyboard test.
366     ret = i8042_command(I8042_CMD_KBD_TEST, param);
367     if (ret)
368         return;
369     if (param[0] != 0x00) {
370         dprintf(1, "i8042 keyboard test failed (got %x not 0x00)\n", param[0]);
371         return;
372     }
373
374     // Enable keyboard and mouse ports.
375     ret = i8042_command(I8042_CMD_KBD_ENABLE, NULL);
376     if (ret)
377         return;
378     ret = i8042_command(I8042_CMD_AUX_ENABLE, NULL);
379     if (ret)
380         return;
381
382
383     /* ------------------- keyboard side ------------------------*/
384     /* reset keyboard and self test  (keyboard side) */
385     ret = kbd_command(ATKBD_CMD_RESET_BAT, param);
386     if (ret)
387         return;
388     if (param[0] != 0xaa) {
389         dprintf(1, "keyboard self test failed (got %x not 0xaa)\n", param[0]);
390         return;
391     }
392
393     /* Disable keyboard */
394     ret = kbd_command(ATKBD_CMD_RESET_DIS, NULL);
395     if (ret)
396         return;
397
398     // Set scancode command (mode 2)
399     param[0] = 0x02;
400     ret = kbd_command(ATKBD_CMD_SSCANSET, param);
401     if (ret)
402         return;
403
404     // Mode: scan code convert, enable IRQ 1, enable IRQ 12
405     param[0] = I8042_CTR_XLATE | I8042_CTR_KBDINT | I8042_CTR_AUXINT;
406     ret = i8042_command(I8042_CMD_CTL_WCTR, param);
407     if (ret)
408         return;
409     CLEARBITS_BDA(kbd_flag3, KF3_CMD_PENDING);
410
411     /* Enable keyboard */
412     ret = kbd_command(ATKBD_CMD_ENABLE, NULL);
413     if (ret)
414         return;
415
416     dprintf(1, "keyboard initialized\n");
417 }
418
419 void
420 ps2port_setup(void)
421 {
422     if (! CONFIG_PS2PORT)
423         return;
424     dprintf(3, "init ps2port\n");
425
426     // Setup irqs, but disable them until init complete.
427     SETBITS_BDA(kbd_flag3, KF3_CMD_PENDING);
428     enable_hwirq(1, entry_09);
429     enable_hwirq(12, entry_74);
430
431     run_thread(keyboard_init, NULL);
432 }