1 | /*
|
---|
2 | * Copyright (C) 2001-2004 Jakub Jermar
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup genarch
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * @brief Zilog 8530 serial port / keyboard driver.
|
---|
35 | *
|
---|
36 | * Note that this file is derived from the i8042.c.
|
---|
37 | * The i8042 driver could be persuaded to control
|
---|
38 | * the z8530 at least in the polling mode.
|
---|
39 | * As a result, this file may contain inaccurate
|
---|
40 | * and z8530-irrelevant constants, code and comments.
|
---|
41 | * Still it miraculously works.
|
---|
42 | */
|
---|
43 |
|
---|
44 | #include <genarch/kbd/z8530.h>
|
---|
45 | #include <genarch/kbd/key.h>
|
---|
46 | #include <genarch/kbd/scanc.h>
|
---|
47 | #include <genarch/kbd/scanc_sun.h>
|
---|
48 | #include <arch/drivers/z8530.h>
|
---|
49 | #include <arch/interrupt.h>
|
---|
50 | #include <cpu.h>
|
---|
51 | #include <arch/asm.h>
|
---|
52 | #include <arch.h>
|
---|
53 | #include <typedefs.h>
|
---|
54 | #include <console/chardev.h>
|
---|
55 | #include <console/console.h>
|
---|
56 | #include <interrupt.h>
|
---|
57 |
|
---|
58 | /* Keyboard commands. */
|
---|
59 | #define KBD_ENABLE 0xf4
|
---|
60 | #define KBD_DISABLE 0xf5
|
---|
61 | #define KBD_ACK 0xfa
|
---|
62 |
|
---|
63 | /*
|
---|
64 | * 60 Write 8042 Command Byte: next data byte written to port 60h is
|
---|
65 | * placed in 8042 command register. Format:
|
---|
66 | *
|
---|
67 | * |7|6|5|4|3|2|1|0|8042 Command Byte
|
---|
68 | * | | | | | | | `---- 1=enable output register full interrupt
|
---|
69 | * | | | | | | `----- should be 0
|
---|
70 | * | | | | | `------ 1=set status register system, 0=clear
|
---|
71 | * | | | | `------- 1=override keyboard inhibit, 0=allow inhibit
|
---|
72 | * | | | `-------- disable keyboard I/O by driving clock line low
|
---|
73 | * | | `--------- disable auxiliary device, drives clock line low
|
---|
74 | * | `---------- IBM scancode translation 0=AT, 1=PC/XT
|
---|
75 | * `----------- reserved, should be 0
|
---|
76 | */
|
---|
77 |
|
---|
78 | #define z8530_SET_COMMAND 0x60
|
---|
79 | #define z8530_COMMAND 0x69
|
---|
80 |
|
---|
81 | #define z8530_BUFFER_FULL_MASK 0x01
|
---|
82 | #define z8530_WAIT_MASK 0x02
|
---|
83 | #define z8530_MOUSE_DATA 0x20
|
---|
84 |
|
---|
85 | /*
|
---|
86 | * These codes read from z8530 data register are silently ignored.
|
---|
87 | */
|
---|
88 | #define IGNORE_CODE 0x7f /* all keys up */
|
---|
89 |
|
---|
90 | static void z8530_suspend(chardev_t *);
|
---|
91 | static void z8530_resume(chardev_t *);
|
---|
92 |
|
---|
93 | chardev_t kbrd;
|
---|
94 | static chardev_operations_t ops = {
|
---|
95 | .suspend = z8530_suspend,
|
---|
96 | .resume = z8530_resume,
|
---|
97 | .read = key_read
|
---|
98 | };
|
---|
99 |
|
---|
100 | static void z8530_interrupt(int n, istate_t *istate);
|
---|
101 | static void z8530_wait(void);
|
---|
102 |
|
---|
103 | static iroutine oldvector;
|
---|
104 | /** Initialize keyboard and service interrupts using kernel routine */
|
---|
105 | void z8530_grab(void)
|
---|
106 | {
|
---|
107 | oldvector = exc_register(VECTOR_KBD, "z8530_interrupt", (iroutine) z8530_interrupt);
|
---|
108 | z8530_wait();
|
---|
109 | z8530_command_write(z8530_SET_COMMAND);
|
---|
110 | z8530_wait();
|
---|
111 | z8530_data_write(z8530_COMMAND);
|
---|
112 | z8530_wait();
|
---|
113 | }
|
---|
114 | /** Resume the former interrupt vector */
|
---|
115 | void z8530_release(void)
|
---|
116 | {
|
---|
117 | if (oldvector)
|
---|
118 | exc_register(VECTOR_KBD, "user_interrupt", oldvector);
|
---|
119 | }
|
---|
120 |
|
---|
121 | /** Initialize z8530. */
|
---|
122 | void z8530_init(void)
|
---|
123 | {
|
---|
124 | int i;
|
---|
125 |
|
---|
126 | z8530_grab();
|
---|
127 | /* Prevent user from accidentaly releasing calling z8530_resume
|
---|
128 | * and disabling keyboard
|
---|
129 | */
|
---|
130 | oldvector = NULL;
|
---|
131 |
|
---|
132 | trap_virtual_enable_irqs(1<<IRQ_KBD);
|
---|
133 | chardev_initialize("z8530_kbd", &kbrd, &ops);
|
---|
134 | stdin = &kbrd;
|
---|
135 |
|
---|
136 | /*
|
---|
137 | * Clear input buffer.
|
---|
138 | * Number of iterations is limited to prevent infinite looping.
|
---|
139 | */
|
---|
140 | for (i = 0; (z8530_status_read() & z8530_BUFFER_FULL_MASK) && i < 100; i++) {
|
---|
141 | z8530_data_read();
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | /** Process z8530 interrupt.
|
---|
146 | *
|
---|
147 | * @param n Interrupt vector.
|
---|
148 | * @param istate Interrupted state.
|
---|
149 | */
|
---|
150 | void z8530_interrupt(int n, istate_t *istate)
|
---|
151 | {
|
---|
152 | uint8_t x;
|
---|
153 | uint8_t status;
|
---|
154 |
|
---|
155 | while (((status=z8530_status_read()) & z8530_BUFFER_FULL_MASK)) {
|
---|
156 | x = z8530_data_read();
|
---|
157 |
|
---|
158 | if ((status & z8530_MOUSE_DATA))
|
---|
159 | continue;
|
---|
160 |
|
---|
161 | if (x & KEY_RELEASE)
|
---|
162 | key_released(x ^ KEY_RELEASE);
|
---|
163 | else
|
---|
164 | key_pressed(x);
|
---|
165 | }
|
---|
166 | trap_virtual_eoi();
|
---|
167 | }
|
---|
168 |
|
---|
169 | /** Wait until the controller reads its data. */
|
---|
170 | void z8530_wait(void) {
|
---|
171 | while (z8530_status_read() & z8530_WAIT_MASK) {
|
---|
172 | /* wait */
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | /* Called from getc(). */
|
---|
177 | void z8530_resume(chardev_t *d)
|
---|
178 | {
|
---|
179 | }
|
---|
180 |
|
---|
181 | /* Called from getc(). */
|
---|
182 | void z8530_suspend(chardev_t *d)
|
---|
183 | {
|
---|
184 | }
|
---|
185 |
|
---|
186 | char key_read(chardev_t *d)
|
---|
187 | {
|
---|
188 | char ch;
|
---|
189 |
|
---|
190 | while(!(ch = active_read_buff_read())) {
|
---|
191 | uint8_t x;
|
---|
192 | while (!(z8530_status_read() & z8530_BUFFER_FULL_MASK))
|
---|
193 | ;
|
---|
194 | x = z8530_data_read();
|
---|
195 | if (x != IGNORE_CODE) {
|
---|
196 | if (x & KEY_RELEASE)
|
---|
197 | key_released(x ^ KEY_RELEASE);
|
---|
198 | else
|
---|
199 | active_read_key_pressed(x);
|
---|
200 | }
|
---|
201 | }
|
---|
202 | return ch;
|
---|
203 | }
|
---|
204 |
|
---|
205 | /** Poll for key press and release events.
|
---|
206 | *
|
---|
207 | * This function can be used to implement keyboard polling.
|
---|
208 | */
|
---|
209 | void z8530_poll(void)
|
---|
210 | {
|
---|
211 | uint8_t x;
|
---|
212 |
|
---|
213 | while (((x = z8530_status_read() & z8530_BUFFER_FULL_MASK))) {
|
---|
214 | x = z8530_data_read();
|
---|
215 | if (x != IGNORE_CODE) {
|
---|
216 | if (x & KEY_RELEASE)
|
---|
217 | key_released(x ^ KEY_RELEASE);
|
---|
218 | else
|
---|
219 | key_pressed(x);
|
---|
220 | }
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | /** @}
|
---|
225 | */
|
---|