tests/acpi: add microvm test
[qemu.git] / hw / input / milkymist-softusb.c
1 /*
2 * QEMU model of the Milkymist SoftUSB block.
3 *
4 * Copyright (c) 2010 Michael Walle <michael@walle.cc>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 *
19 *
20 * Specification available at:
21 * not available yet
22 */
23
24 #include "qemu/osdep.h"
25 #include "qapi/error.h"
26 #include "hw/sysbus.h"
27 #include "migration/vmstate.h"
28 #include "trace.h"
29 #include "ui/console.h"
30 #include "hw/input/hid.h"
31 #include "hw/irq.h"
32 #include "hw/qdev-properties.h"
33 #include "qemu/error-report.h"
34 #include "qemu/module.h"
35 #include "qom/object.h"
36
37 enum {
38 R_CTRL = 0,
39 R_MAX
40 };
41
42 enum {
43 CTRL_RESET = (1<<0),
44 };
45
46 #define COMLOC_DEBUG_PRODUCE 0x1000
47 #define COMLOC_DEBUG_BASE 0x1001
48 #define COMLOC_MEVT_PRODUCE 0x1101
49 #define COMLOC_MEVT_BASE 0x1102
50 #define COMLOC_KEVT_PRODUCE 0x1142
51 #define COMLOC_KEVT_BASE 0x1143
52
53 #define TYPE_MILKYMIST_SOFTUSB "milkymist-softusb"
54 typedef struct MilkymistSoftUsbState MilkymistSoftUsbState;
55 DECLARE_INSTANCE_CHECKER(MilkymistSoftUsbState, MILKYMIST_SOFTUSB,
56 TYPE_MILKYMIST_SOFTUSB)
57
58 struct MilkymistSoftUsbState {
59 SysBusDevice parent_obj;
60
61 HIDState hid_kbd;
62 HIDState hid_mouse;
63
64 MemoryRegion regs_region;
65 MemoryRegion pmem;
66 MemoryRegion dmem;
67 qemu_irq irq;
68
69 void *pmem_ptr;
70 void *dmem_ptr;
71
72 /* device properties */
73 uint32_t pmem_size;
74 uint32_t dmem_size;
75
76 /* device registers */
77 uint32_t regs[R_MAX];
78
79 /* mouse state */
80 uint8_t mouse_hid_buffer[4];
81
82 /* keyboard state */
83 uint8_t kbd_hid_buffer[8];
84 };
85
86 static uint64_t softusb_read(void *opaque, hwaddr addr,
87 unsigned size)
88 {
89 MilkymistSoftUsbState *s = opaque;
90 uint32_t r = 0;
91
92 addr >>= 2;
93 switch (addr) {
94 case R_CTRL:
95 r = s->regs[addr];
96 break;
97
98 default:
99 error_report("milkymist_softusb: read access to unknown register 0x"
100 TARGET_FMT_plx, addr << 2);
101 break;
102 }
103
104 trace_milkymist_softusb_memory_read(addr << 2, r);
105
106 return r;
107 }
108
109 static void
110 softusb_write(void *opaque, hwaddr addr, uint64_t value,
111 unsigned size)
112 {
113 MilkymistSoftUsbState *s = opaque;
114
115 trace_milkymist_softusb_memory_write(addr, value);
116
117 addr >>= 2;
118 switch (addr) {
119 case R_CTRL:
120 s->regs[addr] = value;
121 break;
122
123 default:
124 error_report("milkymist_softusb: write access to unknown register 0x"
125 TARGET_FMT_plx, addr << 2);
126 break;
127 }
128 }
129
130 static const MemoryRegionOps softusb_mmio_ops = {
131 .read = softusb_read,
132 .write = softusb_write,
133 .endianness = DEVICE_NATIVE_ENDIAN,
134 .valid = {
135 .min_access_size = 4,
136 .max_access_size = 4,
137 },
138 };
139
140 static inline void softusb_read_dmem(MilkymistSoftUsbState *s,
141 uint32_t offset, uint8_t *buf, uint32_t len)
142 {
143 if (offset + len >= s->dmem_size) {
144 error_report("milkymist_softusb: read dmem out of bounds "
145 "at offset 0x%x, len %d", offset, len);
146 memset(buf, 0, len);
147 return;
148 }
149
150 memcpy(buf, s->dmem_ptr + offset, len);
151 }
152
153 static inline void softusb_write_dmem(MilkymistSoftUsbState *s,
154 uint32_t offset, uint8_t *buf, uint32_t len)
155 {
156 if (offset + len >= s->dmem_size) {
157 error_report("milkymist_softusb: write dmem out of bounds "
158 "at offset 0x%x, len %d", offset, len);
159 return;
160 }
161
162 memcpy(s->dmem_ptr + offset, buf, len);
163 }
164
165 static void softusb_mouse_changed(MilkymistSoftUsbState *s)
166 {
167 uint8_t m;
168
169 softusb_read_dmem(s, COMLOC_MEVT_PRODUCE, &m, 1);
170 trace_milkymist_softusb_mevt(m);
171 softusb_write_dmem(s, COMLOC_MEVT_BASE + 4 * m, s->mouse_hid_buffer, 4);
172 m = (m + 1) & 0xf;
173 softusb_write_dmem(s, COMLOC_MEVT_PRODUCE, &m, 1);
174
175 trace_milkymist_softusb_pulse_irq();
176 qemu_irq_pulse(s->irq);
177 }
178
179 static void softusb_kbd_changed(MilkymistSoftUsbState *s)
180 {
181 uint8_t m;
182
183 softusb_read_dmem(s, COMLOC_KEVT_PRODUCE, &m, 1);
184 trace_milkymist_softusb_kevt(m);
185 softusb_write_dmem(s, COMLOC_KEVT_BASE + 8 * m, s->kbd_hid_buffer, 8);
186 m = (m + 1) & 0x7;
187 softusb_write_dmem(s, COMLOC_KEVT_PRODUCE, &m, 1);
188
189 trace_milkymist_softusb_pulse_irq();
190 qemu_irq_pulse(s->irq);
191 }
192
193 static void softusb_kbd_hid_datain(HIDState *hs)
194 {
195 MilkymistSoftUsbState *s = container_of(hs, MilkymistSoftUsbState, hid_kbd);
196 int len;
197
198 /* if device is in reset, do nothing */
199 if (s->regs[R_CTRL] & CTRL_RESET) {
200 return;
201 }
202
203 while (hid_has_events(hs)) {
204 len = hid_keyboard_poll(hs, s->kbd_hid_buffer,
205 sizeof(s->kbd_hid_buffer));
206
207 if (len == 8) {
208 softusb_kbd_changed(s);
209 }
210 }
211 }
212
213 static void softusb_mouse_hid_datain(HIDState *hs)
214 {
215 MilkymistSoftUsbState *s =
216 container_of(hs, MilkymistSoftUsbState, hid_mouse);
217 int len;
218
219 /* if device is in reset, do nothing */
220 if (s->regs[R_CTRL] & CTRL_RESET) {
221 return;
222 }
223
224 while (hid_has_events(hs)) {
225 len = hid_pointer_poll(hs, s->mouse_hid_buffer,
226 sizeof(s->mouse_hid_buffer));
227
228 if (len == 4) {
229 softusb_mouse_changed(s);
230 }
231 }
232 }
233
234 static void milkymist_softusb_reset(DeviceState *d)
235 {
236 MilkymistSoftUsbState *s = MILKYMIST_SOFTUSB(d);
237 int i;
238
239 for (i = 0; i < R_MAX; i++) {
240 s->regs[i] = 0;
241 }
242 memset(s->kbd_hid_buffer, 0, sizeof(s->kbd_hid_buffer));
243 memset(s->mouse_hid_buffer, 0, sizeof(s->mouse_hid_buffer));
244
245 hid_reset(&s->hid_kbd);
246 hid_reset(&s->hid_mouse);
247
248 /* defaults */
249 s->regs[R_CTRL] = CTRL_RESET;
250 }
251
252 static void milkymist_softusb_realize(DeviceState *dev, Error **errp)
253 {
254 MilkymistSoftUsbState *s = MILKYMIST_SOFTUSB(dev);
255 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
256
257 sysbus_init_irq(sbd, &s->irq);
258
259 memory_region_init_io(&s->regs_region, OBJECT(s), &softusb_mmio_ops, s,
260 "milkymist-softusb", R_MAX * 4);
261 sysbus_init_mmio(sbd, &s->regs_region);
262
263 /* register pmem and dmem */
264 memory_region_init_ram_nomigrate(&s->pmem, OBJECT(s), "milkymist-softusb.pmem",
265 s->pmem_size, &error_fatal);
266 vmstate_register_ram_global(&s->pmem);
267 s->pmem_ptr = memory_region_get_ram_ptr(&s->pmem);
268 sysbus_init_mmio(sbd, &s->pmem);
269 memory_region_init_ram_nomigrate(&s->dmem, OBJECT(s), "milkymist-softusb.dmem",
270 s->dmem_size, &error_fatal);
271 vmstate_register_ram_global(&s->dmem);
272 s->dmem_ptr = memory_region_get_ram_ptr(&s->dmem);
273 sysbus_init_mmio(sbd, &s->dmem);
274
275 hid_init(&s->hid_kbd, HID_KEYBOARD, softusb_kbd_hid_datain);
276 hid_init(&s->hid_mouse, HID_MOUSE, softusb_mouse_hid_datain);
277 }
278
279 static const VMStateDescription vmstate_milkymist_softusb = {
280 .name = "milkymist-softusb",
281 .version_id = 1,
282 .minimum_version_id = 1,
283 .fields = (VMStateField[]) {
284 VMSTATE_UINT32_ARRAY(regs, MilkymistSoftUsbState, R_MAX),
285 VMSTATE_HID_KEYBOARD_DEVICE(hid_kbd, MilkymistSoftUsbState),
286 VMSTATE_HID_POINTER_DEVICE(hid_mouse, MilkymistSoftUsbState),
287 VMSTATE_BUFFER(kbd_hid_buffer, MilkymistSoftUsbState),
288 VMSTATE_BUFFER(mouse_hid_buffer, MilkymistSoftUsbState),
289 VMSTATE_END_OF_LIST()
290 }
291 };
292
293 static Property milkymist_softusb_properties[] = {
294 DEFINE_PROP_UINT32("pmem_size", MilkymistSoftUsbState, pmem_size, 0x00001000),
295 DEFINE_PROP_UINT32("dmem_size", MilkymistSoftUsbState, dmem_size, 0x00002000),
296 DEFINE_PROP_END_OF_LIST(),
297 };
298
299 static void milkymist_softusb_class_init(ObjectClass *klass, void *data)
300 {
301 DeviceClass *dc = DEVICE_CLASS(klass);
302
303 dc->realize = milkymist_softusb_realize;
304 dc->reset = milkymist_softusb_reset;
305 dc->vmsd = &vmstate_milkymist_softusb;
306 device_class_set_props(dc, milkymist_softusb_properties);
307 }
308
309 static const TypeInfo milkymist_softusb_info = {
310 .name = TYPE_MILKYMIST_SOFTUSB,
311 .parent = TYPE_SYS_BUS_DEVICE,
312 .instance_size = sizeof(MilkymistSoftUsbState),
313 .class_init = milkymist_softusb_class_init,
314 };
315
316 static void milkymist_softusb_register_types(void)
317 {
318 type_register_static(&milkymist_softusb_info);
319 }
320
321 type_init(milkymist_softusb_register_types)