root / hw / syborg_keyboard.c @ af5374aa
History | View | Annotate | Download (6.6 kB)
1 | 4af39611 | Paul Brook | /*
|
---|---|---|---|
2 | 4af39611 | Paul Brook | * Syborg keyboard controller.
|
3 | 4af39611 | Paul Brook | *
|
4 | 4af39611 | Paul Brook | * Copyright (c) 2008 CodeSourcery
|
5 | 4af39611 | Paul Brook | *
|
6 | 4af39611 | Paul Brook | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 4af39611 | Paul Brook | * of this software and associated documentation files (the "Software"), to deal
|
8 | 4af39611 | Paul Brook | * in the Software without restriction, including without limitation the rights
|
9 | 4af39611 | Paul Brook | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 4af39611 | Paul Brook | * copies of the Software, and to permit persons to whom the Software is
|
11 | 4af39611 | Paul Brook | * furnished to do so, subject to the following conditions:
|
12 | 4af39611 | Paul Brook | *
|
13 | 4af39611 | Paul Brook | * The above copyright notice and this permission notice shall be included in
|
14 | 4af39611 | Paul Brook | * all copies or substantial portions of the Software.
|
15 | 4af39611 | Paul Brook | *
|
16 | 4af39611 | Paul Brook | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 4af39611 | Paul Brook | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 4af39611 | Paul Brook | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 4af39611 | Paul Brook | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 4af39611 | Paul Brook | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 4af39611 | Paul Brook | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 4af39611 | Paul Brook | * THE SOFTWARE.
|
23 | 4af39611 | Paul Brook | */
|
24 | 4af39611 | Paul Brook | |
25 | 4af39611 | Paul Brook | #include "sysbus.h" |
26 | 4af39611 | Paul Brook | #include "console.h" |
27 | 4af39611 | Paul Brook | #include "syborg.h" |
28 | 4af39611 | Paul Brook | |
29 | 4af39611 | Paul Brook | //#define DEBUG_SYBORG_KEYBOARD
|
30 | 4af39611 | Paul Brook | |
31 | 4af39611 | Paul Brook | #ifdef DEBUG_SYBORG_KEYBOARD
|
32 | 4af39611 | Paul Brook | #define DPRINTF(fmt, ...) \
|
33 | 4af39611 | Paul Brook | do { printf("syborg_keyboard: " fmt , ##args); } while (0) |
34 | 4af39611 | Paul Brook | #define BADF(fmt, ...) \
|
35 | 4af39611 | Paul Brook | do { fprintf(stderr, "syborg_keyboard: error: " fmt , ## __VA_ARGS__); \ |
36 | 4af39611 | Paul Brook | exit(1);} while (0) |
37 | 4af39611 | Paul Brook | #else
|
38 | 4af39611 | Paul Brook | #define DPRINTF(fmt, ...) do {} while(0) |
39 | 4af39611 | Paul Brook | #define BADF(fmt, ...) \
|
40 | 4af39611 | Paul Brook | do { fprintf(stderr, "syborg_keyboard: error: " fmt , ## __VA_ARGS__); \ |
41 | 4af39611 | Paul Brook | } while (0) |
42 | 4af39611 | Paul Brook | #endif
|
43 | 4af39611 | Paul Brook | |
44 | 4af39611 | Paul Brook | enum {
|
45 | 4af39611 | Paul Brook | KBD_ID = 0,
|
46 | 4af39611 | Paul Brook | KBD_DATA = 1,
|
47 | 4af39611 | Paul Brook | KBD_FIFO_COUNT = 2,
|
48 | 4af39611 | Paul Brook | KBD_INT_ENABLE = 3,
|
49 | 4af39611 | Paul Brook | KBD_FIFO_SIZE = 4
|
50 | 4af39611 | Paul Brook | }; |
51 | 4af39611 | Paul Brook | |
52 | 4af39611 | Paul Brook | typedef struct { |
53 | 4af39611 | Paul Brook | SysBusDevice busdev; |
54 | 0c067bbb | Juan Quintela | uint32_t int_enabled; |
55 | 4af39611 | Paul Brook | int extension_bit;
|
56 | ee6847d1 | Gerd Hoffmann | uint32_t fifo_size; |
57 | 4af39611 | Paul Brook | uint32_t *key_fifo; |
58 | 0c067bbb | Juan Quintela | uint32_t read_pos, read_count; |
59 | 4af39611 | Paul Brook | qemu_irq irq; |
60 | 4af39611 | Paul Brook | } SyborgKeyboardState; |
61 | 4af39611 | Paul Brook | |
62 | 4af39611 | Paul Brook | static void syborg_keyboard_update(SyborgKeyboardState *s) |
63 | 4af39611 | Paul Brook | { |
64 | 4af39611 | Paul Brook | int level = s->read_count && s->int_enabled;
|
65 | 4af39611 | Paul Brook | DPRINTF("Update IRQ %d\n", level);
|
66 | 4af39611 | Paul Brook | qemu_set_irq(s->irq, level); |
67 | 4af39611 | Paul Brook | } |
68 | 4af39611 | Paul Brook | |
69 | c227f099 | Anthony Liguori | static uint32_t syborg_keyboard_read(void *opaque, target_phys_addr_t offset) |
70 | 4af39611 | Paul Brook | { |
71 | 4af39611 | Paul Brook | SyborgKeyboardState *s = (SyborgKeyboardState *)opaque; |
72 | 4af39611 | Paul Brook | int c;
|
73 | 4af39611 | Paul Brook | |
74 | 4af39611 | Paul Brook | DPRINTF("reg read %d\n", (int)offset); |
75 | 4af39611 | Paul Brook | offset &= 0xfff;
|
76 | 4af39611 | Paul Brook | switch (offset >> 2) { |
77 | 4af39611 | Paul Brook | case KBD_ID:
|
78 | 4af39611 | Paul Brook | return SYBORG_ID_KEYBOARD;
|
79 | 4af39611 | Paul Brook | case KBD_FIFO_COUNT:
|
80 | 4af39611 | Paul Brook | return s->read_count;
|
81 | 4af39611 | Paul Brook | case KBD_DATA:
|
82 | 4af39611 | Paul Brook | if (s->read_count == 0) { |
83 | 4af39611 | Paul Brook | c = -1;
|
84 | 4af39611 | Paul Brook | DPRINTF("FIFO underflow\n");
|
85 | 4af39611 | Paul Brook | } else {
|
86 | 4af39611 | Paul Brook | c = s->key_fifo[s->read_pos]; |
87 | 4af39611 | Paul Brook | DPRINTF("FIFO read 0x%x\n", c);
|
88 | 4af39611 | Paul Brook | s->read_count--; |
89 | 4af39611 | Paul Brook | s->read_pos++; |
90 | 4af39611 | Paul Brook | if (s->read_pos == s->fifo_size)
|
91 | 4af39611 | Paul Brook | s->read_pos = 0;
|
92 | 4af39611 | Paul Brook | } |
93 | 4af39611 | Paul Brook | syborg_keyboard_update(s); |
94 | 4af39611 | Paul Brook | return c;
|
95 | 4af39611 | Paul Brook | case KBD_INT_ENABLE:
|
96 | 4af39611 | Paul Brook | return s->int_enabled;
|
97 | 4af39611 | Paul Brook | case KBD_FIFO_SIZE:
|
98 | 4af39611 | Paul Brook | return s->fifo_size;
|
99 | 4af39611 | Paul Brook | default:
|
100 | 4af39611 | Paul Brook | cpu_abort(cpu_single_env, "syborg_keyboard_read: Bad offset %x\n",
|
101 | 4af39611 | Paul Brook | (int)offset);
|
102 | 4af39611 | Paul Brook | return 0; |
103 | 4af39611 | Paul Brook | } |
104 | 4af39611 | Paul Brook | } |
105 | 4af39611 | Paul Brook | |
106 | c227f099 | Anthony Liguori | static void syborg_keyboard_write(void *opaque, target_phys_addr_t offset, |
107 | 4af39611 | Paul Brook | uint32_t value) |
108 | 4af39611 | Paul Brook | { |
109 | 4af39611 | Paul Brook | SyborgKeyboardState *s = (SyborgKeyboardState *)opaque; |
110 | 4af39611 | Paul Brook | |
111 | 4af39611 | Paul Brook | DPRINTF("reg write %d\n", (int)offset); |
112 | 4af39611 | Paul Brook | offset &= 0xfff;
|
113 | 4af39611 | Paul Brook | switch (offset >> 2) { |
114 | 4af39611 | Paul Brook | case KBD_INT_ENABLE:
|
115 | 4af39611 | Paul Brook | s->int_enabled = value; |
116 | 4af39611 | Paul Brook | syborg_keyboard_update(s); |
117 | 4af39611 | Paul Brook | break;
|
118 | 4af39611 | Paul Brook | default:
|
119 | 4af39611 | Paul Brook | cpu_abort(cpu_single_env, "syborg_keyboard_write: Bad offset %x\n",
|
120 | 4af39611 | Paul Brook | (int)offset);
|
121 | 4af39611 | Paul Brook | } |
122 | 4af39611 | Paul Brook | } |
123 | 4af39611 | Paul Brook | |
124 | d60efc6b | Blue Swirl | static CPUReadMemoryFunc * const syborg_keyboard_readfn[] = { |
125 | 4af39611 | Paul Brook | syborg_keyboard_read, |
126 | 4af39611 | Paul Brook | syborg_keyboard_read, |
127 | 4af39611 | Paul Brook | syborg_keyboard_read |
128 | 4af39611 | Paul Brook | }; |
129 | 4af39611 | Paul Brook | |
130 | d60efc6b | Blue Swirl | static CPUWriteMemoryFunc * const syborg_keyboard_writefn[] = { |
131 | 4af39611 | Paul Brook | syborg_keyboard_write, |
132 | 4af39611 | Paul Brook | syborg_keyboard_write, |
133 | 4af39611 | Paul Brook | syborg_keyboard_write |
134 | 4af39611 | Paul Brook | }; |
135 | 4af39611 | Paul Brook | |
136 | 4af39611 | Paul Brook | static void syborg_keyboard_event(void *opaque, int keycode) |
137 | 4af39611 | Paul Brook | { |
138 | 4af39611 | Paul Brook | SyborgKeyboardState *s = (SyborgKeyboardState *)opaque; |
139 | 4af39611 | Paul Brook | int slot;
|
140 | 4af39611 | Paul Brook | uint32_t val; |
141 | 4af39611 | Paul Brook | |
142 | 4af39611 | Paul Brook | /* Strip off 0xe0 prefixes and reconstruct the full scancode. */
|
143 | 4af39611 | Paul Brook | if (keycode == 0xe0 && !s->extension_bit) { |
144 | 4af39611 | Paul Brook | DPRINTF("Extension bit\n");
|
145 | 4af39611 | Paul Brook | s->extension_bit = 0x80;
|
146 | 4af39611 | Paul Brook | return;
|
147 | 4af39611 | Paul Brook | } |
148 | 4af39611 | Paul Brook | val = (keycode & 0x7f) | s->extension_bit;
|
149 | 4af39611 | Paul Brook | if (keycode & 0x80) |
150 | 4af39611 | Paul Brook | val |= 0x80000000u;
|
151 | 4af39611 | Paul Brook | s->extension_bit = 0;
|
152 | 4af39611 | Paul Brook | |
153 | 4af39611 | Paul Brook | DPRINTF("FIFO push 0x%x\n", val);
|
154 | 4af39611 | Paul Brook | slot = s->read_pos + s->read_count; |
155 | 4af39611 | Paul Brook | if (slot >= s->fifo_size)
|
156 | 4af39611 | Paul Brook | slot -= s->fifo_size; |
157 | 4af39611 | Paul Brook | |
158 | 4af39611 | Paul Brook | if (s->read_count < s->fifo_size) {
|
159 | 4af39611 | Paul Brook | s->read_count++; |
160 | 4af39611 | Paul Brook | s->key_fifo[slot] = val; |
161 | 4af39611 | Paul Brook | } else {
|
162 | 4af39611 | Paul Brook | fprintf(stderr, "syborg_keyboard error! FIFO overflow\n");
|
163 | 4af39611 | Paul Brook | } |
164 | 4af39611 | Paul Brook | |
165 | 4af39611 | Paul Brook | syborg_keyboard_update(s); |
166 | 4af39611 | Paul Brook | } |
167 | 4af39611 | Paul Brook | |
168 | 0c067bbb | Juan Quintela | static const VMStateDescription vmstate_syborg_keyboard = { |
169 | 0c067bbb | Juan Quintela | .name = "syborg_keyboard",
|
170 | 0c067bbb | Juan Quintela | .version_id = 1,
|
171 | 0c067bbb | Juan Quintela | .minimum_version_id = 1,
|
172 | 0c067bbb | Juan Quintela | .minimum_version_id_old = 1,
|
173 | 0c067bbb | Juan Quintela | .fields = (VMStateField[]) { |
174 | 0c067bbb | Juan Quintela | VMSTATE_UINT32_EQUAL(fifo_size, SyborgKeyboardState), |
175 | 0c067bbb | Juan Quintela | VMSTATE_UINT32(int_enabled, SyborgKeyboardState), |
176 | 0c067bbb | Juan Quintela | VMSTATE_UINT32(read_pos, SyborgKeyboardState), |
177 | 0c067bbb | Juan Quintela | VMSTATE_UINT32(read_count, SyborgKeyboardState), |
178 | 0c067bbb | Juan Quintela | VMSTATE_VARRAY_UINT32(key_fifo, SyborgKeyboardState, fifo_size, 1,
|
179 | 0c067bbb | Juan Quintela | vmstate_info_uint32, uint32), |
180 | 0c067bbb | Juan Quintela | VMSTATE_END_OF_LIST() |
181 | 4af39611 | Paul Brook | } |
182 | 0c067bbb | Juan Quintela | }; |
183 | 4af39611 | Paul Brook | |
184 | 81a322d4 | Gerd Hoffmann | static int syborg_keyboard_init(SysBusDevice *dev) |
185 | 4af39611 | Paul Brook | { |
186 | 4af39611 | Paul Brook | SyborgKeyboardState *s = FROM_SYSBUS(SyborgKeyboardState, dev); |
187 | 4af39611 | Paul Brook | int iomemtype;
|
188 | 4af39611 | Paul Brook | |
189 | 4af39611 | Paul Brook | sysbus_init_irq(dev, &s->irq); |
190 | 1eed09cb | Avi Kivity | iomemtype = cpu_register_io_memory(syborg_keyboard_readfn, |
191 | 2507c12a | Alexander Graf | syborg_keyboard_writefn, s, |
192 | 2507c12a | Alexander Graf | DEVICE_NATIVE_ENDIAN); |
193 | 4af39611 | Paul Brook | sysbus_init_mmio(dev, 0x1000, iomemtype);
|
194 | 4af39611 | Paul Brook | if (s->fifo_size <= 0) { |
195 | 4af39611 | Paul Brook | fprintf(stderr, "syborg_keyboard: fifo too small\n");
|
196 | 4af39611 | Paul Brook | s->fifo_size = 16;
|
197 | 4af39611 | Paul Brook | } |
198 | 4af39611 | Paul Brook | s->key_fifo = qemu_mallocz(s->fifo_size * sizeof(s->key_fifo[0])); |
199 | 4af39611 | Paul Brook | |
200 | 4af39611 | Paul Brook | qemu_add_kbd_event_handler(syborg_keyboard_event, s); |
201 | 4af39611 | Paul Brook | |
202 | 0c067bbb | Juan Quintela | vmstate_register(&dev->qdev, -1, &vmstate_syborg_keyboard, s);
|
203 | 81a322d4 | Gerd Hoffmann | return 0; |
204 | 4af39611 | Paul Brook | } |
205 | 4af39611 | Paul Brook | |
206 | ee6847d1 | Gerd Hoffmann | static SysBusDeviceInfo syborg_keyboard_info = {
|
207 | ee6847d1 | Gerd Hoffmann | .init = syborg_keyboard_init, |
208 | ee6847d1 | Gerd Hoffmann | .qdev.name = "syborg,keyboard",
|
209 | ee6847d1 | Gerd Hoffmann | .qdev.size = sizeof(SyborgKeyboardState),
|
210 | ee6847d1 | Gerd Hoffmann | .qdev.props = (Property[]) { |
211 | 51dd5927 | Gerd Hoffmann | DEFINE_PROP_UINT32("fifo-size", SyborgKeyboardState, fifo_size, 16), |
212 | 51dd5927 | Gerd Hoffmann | DEFINE_PROP_END_OF_LIST(), |
213 | ee6847d1 | Gerd Hoffmann | } |
214 | ee6847d1 | Gerd Hoffmann | }; |
215 | ee6847d1 | Gerd Hoffmann | |
216 | 4af39611 | Paul Brook | static void syborg_keyboard_register_devices(void) |
217 | 4af39611 | Paul Brook | { |
218 | ee6847d1 | Gerd Hoffmann | sysbus_register_withprop(&syborg_keyboard_info); |
219 | 4af39611 | Paul Brook | } |
220 | 4af39611 | Paul Brook | |
221 | 4af39611 | Paul Brook | device_init(syborg_keyboard_register_devices) |