configure: Print a banner comment at the top of config.log
[qemu/ar7.git] / hw / syborg_keyboard.c
blob942a7dc800db1c74e7a4d39a2afe9ebc9644afe8
1 /*
2 * Syborg keyboard controller.
4 * Copyright (c) 2008 CodeSourcery
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "sysbus.h"
26 #include "console.h"
27 #include "syborg.h"
29 //#define DEBUG_SYBORG_KEYBOARD
31 #ifdef DEBUG_SYBORG_KEYBOARD
32 #define DPRINTF(fmt, ...) \
33 do { printf("syborg_keyboard: " fmt , ##args); } while (0)
34 #define BADF(fmt, ...) \
35 do { fprintf(stderr, "syborg_keyboard: error: " fmt , ## __VA_ARGS__); \
36 exit(1);} while (0)
37 #else
38 #define DPRINTF(fmt, ...) do {} while(0)
39 #define BADF(fmt, ...) \
40 do { fprintf(stderr, "syborg_keyboard: error: " fmt , ## __VA_ARGS__); \
41 } while (0)
42 #endif
44 enum {
45 KBD_ID = 0,
46 KBD_DATA = 1,
47 KBD_FIFO_COUNT = 2,
48 KBD_INT_ENABLE = 3,
49 KBD_FIFO_SIZE = 4
52 typedef struct {
53 SysBusDevice busdev;
54 MemoryRegion iomem;
55 uint32_t int_enabled;
56 int extension_bit;
57 uint32_t fifo_size;
58 uint32_t *key_fifo;
59 uint32_t read_pos, read_count;
60 qemu_irq irq;
61 } SyborgKeyboardState;
63 static void syborg_keyboard_update(SyborgKeyboardState *s)
65 int level = s->read_count && s->int_enabled;
66 DPRINTF("Update IRQ %d\n", level);
67 qemu_set_irq(s->irq, level);
70 static uint64_t syborg_keyboard_read(void *opaque, target_phys_addr_t offset,
71 unsigned size)
73 SyborgKeyboardState *s = (SyborgKeyboardState *)opaque;
74 int c;
76 DPRINTF("reg read %d\n", (int)offset);
77 offset &= 0xfff;
78 switch (offset >> 2) {
79 case KBD_ID:
80 return SYBORG_ID_KEYBOARD;
81 case KBD_FIFO_COUNT:
82 return s->read_count;
83 case KBD_DATA:
84 if (s->read_count == 0) {
85 c = -1;
86 DPRINTF("FIFO underflow\n");
87 } else {
88 c = s->key_fifo[s->read_pos];
89 DPRINTF("FIFO read 0x%x\n", c);
90 s->read_count--;
91 s->read_pos++;
92 if (s->read_pos == s->fifo_size)
93 s->read_pos = 0;
95 syborg_keyboard_update(s);
96 return c;
97 case KBD_INT_ENABLE:
98 return s->int_enabled;
99 case KBD_FIFO_SIZE:
100 return s->fifo_size;
101 default:
102 cpu_abort(cpu_single_env, "syborg_keyboard_read: Bad offset %x\n",
103 (int)offset);
104 return 0;
108 static void syborg_keyboard_write(void *opaque, target_phys_addr_t offset,
109 uint64_t value, unsigned size)
111 SyborgKeyboardState *s = (SyborgKeyboardState *)opaque;
113 DPRINTF("reg write %d\n", (int)offset);
114 offset &= 0xfff;
115 switch (offset >> 2) {
116 case KBD_INT_ENABLE:
117 s->int_enabled = value;
118 syborg_keyboard_update(s);
119 break;
120 default:
121 cpu_abort(cpu_single_env, "syborg_keyboard_write: Bad offset %x\n",
122 (int)offset);
126 static const MemoryRegionOps syborg_keyboard_ops = {
127 .read = syborg_keyboard_read,
128 .write = syborg_keyboard_write,
129 .endianness = DEVICE_NATIVE_ENDIAN,
132 static void syborg_keyboard_event(void *opaque, int keycode)
134 SyborgKeyboardState *s = (SyborgKeyboardState *)opaque;
135 int slot;
136 uint32_t val;
138 /* Strip off 0xe0 prefixes and reconstruct the full scancode. */
139 if (keycode == 0xe0 && !s->extension_bit) {
140 DPRINTF("Extension bit\n");
141 s->extension_bit = 0x80;
142 return;
144 val = (keycode & 0x7f) | s->extension_bit;
145 if (keycode & 0x80)
146 val |= 0x80000000u;
147 s->extension_bit = 0;
149 DPRINTF("FIFO push 0x%x\n", val);
150 slot = s->read_pos + s->read_count;
151 if (slot >= s->fifo_size)
152 slot -= s->fifo_size;
154 if (s->read_count < s->fifo_size) {
155 s->read_count++;
156 s->key_fifo[slot] = val;
157 } else {
158 fprintf(stderr, "syborg_keyboard error! FIFO overflow\n");
161 syborg_keyboard_update(s);
164 static const VMStateDescription vmstate_syborg_keyboard = {
165 .name = "syborg_keyboard",
166 .version_id = 1,
167 .minimum_version_id = 1,
168 .minimum_version_id_old = 1,
169 .fields = (VMStateField[]) {
170 VMSTATE_UINT32_EQUAL(fifo_size, SyborgKeyboardState),
171 VMSTATE_UINT32(int_enabled, SyborgKeyboardState),
172 VMSTATE_UINT32(read_pos, SyborgKeyboardState),
173 VMSTATE_UINT32(read_count, SyborgKeyboardState),
174 VMSTATE_VARRAY_UINT32(key_fifo, SyborgKeyboardState, fifo_size, 1,
175 vmstate_info_uint32, uint32),
176 VMSTATE_END_OF_LIST()
180 static int syborg_keyboard_init(SysBusDevice *dev)
182 SyborgKeyboardState *s = FROM_SYSBUS(SyborgKeyboardState, dev);
184 sysbus_init_irq(dev, &s->irq);
185 memory_region_init_io(&s->iomem, &syborg_keyboard_ops, s,
186 "keyboard", 0x1000);
187 sysbus_init_mmio(dev, &s->iomem);
188 if (s->fifo_size <= 0) {
189 fprintf(stderr, "syborg_keyboard: fifo too small\n");
190 s->fifo_size = 16;
192 s->key_fifo = g_malloc0(s->fifo_size * sizeof(s->key_fifo[0]));
194 qemu_add_kbd_event_handler(syborg_keyboard_event, s);
196 vmstate_register(&dev->qdev, -1, &vmstate_syborg_keyboard, s);
197 return 0;
200 static SysBusDeviceInfo syborg_keyboard_info = {
201 .init = syborg_keyboard_init,
202 .qdev.name = "syborg,keyboard",
203 .qdev.size = sizeof(SyborgKeyboardState),
204 .qdev.props = (Property[]) {
205 DEFINE_PROP_UINT32("fifo-size", SyborgKeyboardState, fifo_size, 16),
206 DEFINE_PROP_END_OF_LIST(),
210 static void syborg_keyboard_register_devices(void)
212 sysbus_register_withprop(&syborg_keyboard_info);
215 device_init(syborg_keyboard_register_devices)