1 | /*
|
---|
2 | * Copyright (C) 2006 Martin Decky
|
---|
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 ppc32
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <arch/drivers/cuda.h>
|
---|
36 | #include <arch/asm.h>
|
---|
37 | #include <console/console.h>
|
---|
38 | #include <console/chardev.h>
|
---|
39 | #include <arch/drivers/pic.h>
|
---|
40 | #include <sysinfo/sysinfo.h>
|
---|
41 | #include <interrupt.h>
|
---|
42 | #include <stdarg.h>
|
---|
43 |
|
---|
44 | #define SPECIAL '?'
|
---|
45 |
|
---|
46 | #define PACKET_ADB 0x00
|
---|
47 | #define PACKET_CUDA 0x01
|
---|
48 |
|
---|
49 | #define CUDA_POWERDOWN 0x0a
|
---|
50 |
|
---|
51 | #define RS 0x200
|
---|
52 | #define B (0 * RS)
|
---|
53 | #define A (1 * RS)
|
---|
54 | #define SR (10 * RS)
|
---|
55 | #define ACR (11 * RS)
|
---|
56 |
|
---|
57 | #define SR_OUT 0x10
|
---|
58 | #define TACK 0x10
|
---|
59 | #define TIP 0x20
|
---|
60 |
|
---|
61 |
|
---|
62 | static volatile uint8_t *cuda = NULL;
|
---|
63 | static iroutine vector;
|
---|
64 |
|
---|
65 |
|
---|
66 | static char lchars[0x80] = {
|
---|
67 | 'a',
|
---|
68 | 's',
|
---|
69 | 'd',
|
---|
70 | 'f',
|
---|
71 | 'h',
|
---|
72 | 'g',
|
---|
73 | 'z',
|
---|
74 | 'x',
|
---|
75 | 'c',
|
---|
76 | 'v',
|
---|
77 | SPECIAL,
|
---|
78 | 'b',
|
---|
79 | 'q',
|
---|
80 | 'w',
|
---|
81 | 'e',
|
---|
82 | 'r',
|
---|
83 | 'y',
|
---|
84 | 't',
|
---|
85 | '1',
|
---|
86 | '2',
|
---|
87 | '3',
|
---|
88 | '4',
|
---|
89 | '6',
|
---|
90 | '5',
|
---|
91 | '=',
|
---|
92 | '9',
|
---|
93 | '7',
|
---|
94 | '-',
|
---|
95 | '8',
|
---|
96 | '0',
|
---|
97 | ']',
|
---|
98 | 'o',
|
---|
99 | 'u',
|
---|
100 | '[',
|
---|
101 | 'i',
|
---|
102 | 'p',
|
---|
103 | '\n', /* Enter */
|
---|
104 | 'l',
|
---|
105 | 'j',
|
---|
106 | '\'',
|
---|
107 | 'k',
|
---|
108 | ';',
|
---|
109 | '\\',
|
---|
110 | ',',
|
---|
111 | '/',
|
---|
112 | 'n',
|
---|
113 | 'm',
|
---|
114 | '.',
|
---|
115 | '\t', /* Tab */
|
---|
116 | ' ',
|
---|
117 | '`',
|
---|
118 | '\b', /* Backspace */
|
---|
119 | SPECIAL,
|
---|
120 | SPECIAL, /* Escape */
|
---|
121 | SPECIAL, /* Ctrl */
|
---|
122 | SPECIAL, /* Alt */
|
---|
123 | SPECIAL, /* Shift */
|
---|
124 | SPECIAL, /* Caps-Lock */
|
---|
125 | SPECIAL, /* RAlt */
|
---|
126 | SPECIAL, /* Left */
|
---|
127 | SPECIAL, /* Right */
|
---|
128 | SPECIAL, /* Down */
|
---|
129 | SPECIAL, /* Up */
|
---|
130 | SPECIAL,
|
---|
131 | SPECIAL,
|
---|
132 | '.', /* Keypad . */
|
---|
133 | SPECIAL,
|
---|
134 | '*', /* Keypad * */
|
---|
135 | SPECIAL,
|
---|
136 | '+', /* Keypad + */
|
---|
137 | SPECIAL,
|
---|
138 | SPECIAL, /* NumLock */
|
---|
139 | SPECIAL,
|
---|
140 | SPECIAL,
|
---|
141 | SPECIAL,
|
---|
142 | '/', /* Keypad / */
|
---|
143 | '\n', /* Keypad Enter */
|
---|
144 | SPECIAL,
|
---|
145 | '-', /* Keypad - */
|
---|
146 | SPECIAL,
|
---|
147 | SPECIAL,
|
---|
148 | SPECIAL,
|
---|
149 | '0', /* Keypad 0 */
|
---|
150 | '1', /* Keypad 1 */
|
---|
151 | '2', /* Keypad 2 */
|
---|
152 | '3', /* Keypad 3 */
|
---|
153 | '4', /* Keypad 4 */
|
---|
154 | '5', /* Keypad 5 */
|
---|
155 | '6', /* Keypad 6 */
|
---|
156 | '7', /* Keypad 7 */
|
---|
157 | SPECIAL,
|
---|
158 | '8', /* Keypad 8 */
|
---|
159 | '9', /* Keypad 9 */
|
---|
160 | SPECIAL,
|
---|
161 | SPECIAL,
|
---|
162 | SPECIAL,
|
---|
163 | SPECIAL, /* F5 */
|
---|
164 | SPECIAL, /* F6 */
|
---|
165 | SPECIAL, /* F7 */
|
---|
166 | SPECIAL, /* F3 */
|
---|
167 | SPECIAL, /* F8 */
|
---|
168 | SPECIAL, /* F9 */
|
---|
169 | SPECIAL,
|
---|
170 | SPECIAL, /* F11 */
|
---|
171 | SPECIAL,
|
---|
172 | SPECIAL, /* F13 */
|
---|
173 | SPECIAL,
|
---|
174 | SPECIAL, /* ScrollLock */
|
---|
175 | SPECIAL,
|
---|
176 | SPECIAL, /* F10 */
|
---|
177 | SPECIAL,
|
---|
178 | SPECIAL, /* F12 */
|
---|
179 | SPECIAL,
|
---|
180 | SPECIAL, /* Pause */
|
---|
181 | SPECIAL, /* Insert */
|
---|
182 | SPECIAL, /* Home */
|
---|
183 | SPECIAL, /* PageUp */
|
---|
184 | SPECIAL, /* Delete */
|
---|
185 | SPECIAL, /* F4 */
|
---|
186 | SPECIAL, /* End */
|
---|
187 | SPECIAL, /* F2 */
|
---|
188 | SPECIAL, /* PageDown */
|
---|
189 | SPECIAL /* F1 */
|
---|
190 | };
|
---|
191 |
|
---|
192 |
|
---|
193 | void send_packet(const uint8_t kind, index_t count, ...);
|
---|
194 |
|
---|
195 |
|
---|
196 | static void receive_packet(uint8_t *kind, index_t count, uint8_t data[])
|
---|
197 | {
|
---|
198 | cuda[B] = cuda[B] & ~TIP;
|
---|
199 | *kind = cuda[SR];
|
---|
200 |
|
---|
201 | index_t i;
|
---|
202 | for (i = 0; i < count; i++)
|
---|
203 | data[i] = cuda[SR];
|
---|
204 |
|
---|
205 | cuda[B] = cuda[B] | TIP;
|
---|
206 | }
|
---|
207 |
|
---|
208 |
|
---|
209 | /* Called from getc(). */
|
---|
210 | static void cuda_resume(chardev_t *d)
|
---|
211 | {
|
---|
212 | }
|
---|
213 |
|
---|
214 |
|
---|
215 | /* Called from getc(). */
|
---|
216 | static void cuda_suspend(chardev_t *d)
|
---|
217 | {
|
---|
218 | }
|
---|
219 |
|
---|
220 |
|
---|
221 | static char key_read(chardev_t *d)
|
---|
222 | {
|
---|
223 | char ch;
|
---|
224 |
|
---|
225 | ch = 0;
|
---|
226 | return ch;
|
---|
227 | }
|
---|
228 |
|
---|
229 |
|
---|
230 | static chardev_t kbrd;
|
---|
231 | static chardev_operations_t ops = {
|
---|
232 | .suspend = cuda_suspend,
|
---|
233 | .resume = cuda_resume,
|
---|
234 | .read = key_read
|
---|
235 | };
|
---|
236 |
|
---|
237 |
|
---|
238 | int cuda_get_scancode(void)
|
---|
239 | {
|
---|
240 | uint8_t kind;
|
---|
241 | uint8_t data[4];
|
---|
242 |
|
---|
243 | receive_packet(&kind, 4, data);
|
---|
244 |
|
---|
245 | if ((kind == PACKET_ADB) && (data[0] == 0x40) && (data[1] == 0x2c))
|
---|
246 | return data[2];
|
---|
247 |
|
---|
248 | return -1;
|
---|
249 | }
|
---|
250 |
|
---|
251 | static void cuda_irq(int n, istate_t *istate)
|
---|
252 | {
|
---|
253 | int scan_code = cuda_get_scancode();
|
---|
254 |
|
---|
255 | if (scan_code != -1) {
|
---|
256 | uint8_t scancode = (uint8_t) scan_code;
|
---|
257 | if ((scancode & 0x80) != 0x80)
|
---|
258 | chardev_push_character(&kbrd, lchars[scancode & 0x7f]);
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 |
|
---|
263 | /** Initialize keyboard and service interrupts using kernel routine */
|
---|
264 | void cuda_grab(void)
|
---|
265 | {
|
---|
266 | vector = int_register(CUDA_IRQ, "cuda", cuda_irq);
|
---|
267 | }
|
---|
268 |
|
---|
269 |
|
---|
270 | /** Resume the former interrupt vector */
|
---|
271 | void cuda_release(void)
|
---|
272 | {
|
---|
273 | if (vector)
|
---|
274 | int_register(CUDA_IRQ, "user_interrupt", vector);
|
---|
275 | }
|
---|
276 |
|
---|
277 |
|
---|
278 | void cuda_init(uintptr_t base, size_t size)
|
---|
279 | {
|
---|
280 | cuda = (uint8_t *) hw_map(base, size);
|
---|
281 |
|
---|
282 | int_register(CUDA_IRQ, "cuda", cuda_irq);
|
---|
283 | pic_enable_interrupt(CUDA_IRQ);
|
---|
284 |
|
---|
285 | chardev_initialize("cuda_kbd", &kbrd, &ops);
|
---|
286 | stdin = &kbrd;
|
---|
287 |
|
---|
288 | sysinfo_set_item_val("cuda", NULL, true);
|
---|
289 | sysinfo_set_item_val("cuda.irq", NULL, CUDA_IRQ);
|
---|
290 | }
|
---|
291 |
|
---|
292 |
|
---|
293 | void send_packet(const uint8_t kind, index_t count, ...)
|
---|
294 | {
|
---|
295 | index_t i;
|
---|
296 | va_list va;
|
---|
297 |
|
---|
298 | cuda[B] = cuda[B] | TIP;
|
---|
299 | cuda[ACR] = cuda[ACR] | SR_OUT;
|
---|
300 | cuda[SR] = kind;
|
---|
301 | cuda[B] = cuda[B] & ~TIP;
|
---|
302 |
|
---|
303 | va_start(va, count);
|
---|
304 |
|
---|
305 | for (i = 0; i < count; i++) {
|
---|
306 | cuda[ACR] = cuda[ACR] | SR_OUT;
|
---|
307 | cuda[SR] = va_arg(va, int);
|
---|
308 | cuda[B] = cuda[B] | TACK;
|
---|
309 | }
|
---|
310 |
|
---|
311 | va_end(va);
|
---|
312 |
|
---|
313 | cuda[B] = cuda[B] | TIP;
|
---|
314 | }
|
---|
315 |
|
---|
316 |
|
---|
317 | void cpu_halt(void) {
|
---|
318 | #ifdef CONFIG_POWEROFF
|
---|
319 | send_packet(PACKET_CUDA, 1, CUDA_POWERDOWN);
|
---|
320 | #endif
|
---|
321 | asm volatile (
|
---|
322 | "b 0\n"
|
---|
323 | );
|
---|
324 | }
|
---|
325 |
|
---|
326 | /** @}
|
---|
327 | */
|
---|