configure: Show trace output file conditionally
[qemu.git] / hw / input / milkymist-softusb.c
blobecde33cb9512a5f388cdcd6c8bd16992e733b77d
1 /*
2 * QEMU model of the Milkymist SoftUSB block.
4 * Copyright (c) 2010 Michael Walle <michael@walle.cc>
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.
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.
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/>.
20 * Specification available at:
21 * not available yet
24 #include "hw/hw.h"
25 #include "hw/sysbus.h"
26 #include "trace.h"
27 #include "ui/console.h"
28 #include "hw/input/hid.h"
29 #include "qemu/error-report.h"
31 enum {
32 R_CTRL = 0,
33 R_MAX
36 enum {
37 CTRL_RESET = (1<<0),
40 #define COMLOC_DEBUG_PRODUCE 0x1000
41 #define COMLOC_DEBUG_BASE 0x1001
42 #define COMLOC_MEVT_PRODUCE 0x1101
43 #define COMLOC_MEVT_BASE 0x1102
44 #define COMLOC_KEVT_PRODUCE 0x1142
45 #define COMLOC_KEVT_BASE 0x1143
47 #define TYPE_MILKYMIST_SOFTUSB "milkymist-softusb"
48 #define MILKYMIST_SOFTUSB(obj) \
49 OBJECT_CHECK(MilkymistSoftUsbState, (obj), TYPE_MILKYMIST_SOFTUSB)
51 struct MilkymistSoftUsbState {
52 SysBusDevice parent_obj;
54 HIDState hid_kbd;
55 HIDState hid_mouse;
57 MemoryRegion regs_region;
58 MemoryRegion pmem;
59 MemoryRegion dmem;
60 qemu_irq irq;
62 void *pmem_ptr;
63 void *dmem_ptr;
65 /* device properties */
66 uint32_t pmem_size;
67 uint32_t dmem_size;
69 /* device registers */
70 uint32_t regs[R_MAX];
72 /* mouse state */
73 uint8_t mouse_hid_buffer[4];
75 /* keyboard state */
76 uint8_t kbd_hid_buffer[8];
78 typedef struct MilkymistSoftUsbState MilkymistSoftUsbState;
80 static uint64_t softusb_read(void *opaque, hwaddr addr,
81 unsigned size)
83 MilkymistSoftUsbState *s = opaque;
84 uint32_t r = 0;
86 addr >>= 2;
87 switch (addr) {
88 case R_CTRL:
89 r = s->regs[addr];
90 break;
92 default:
93 error_report("milkymist_softusb: read access to unknown register 0x"
94 TARGET_FMT_plx, addr << 2);
95 break;
98 trace_milkymist_softusb_memory_read(addr << 2, r);
100 return r;
103 static void
104 softusb_write(void *opaque, hwaddr addr, uint64_t value,
105 unsigned size)
107 MilkymistSoftUsbState *s = opaque;
109 trace_milkymist_softusb_memory_write(addr, value);
111 addr >>= 2;
112 switch (addr) {
113 case R_CTRL:
114 s->regs[addr] = value;
115 break;
117 default:
118 error_report("milkymist_softusb: write access to unknown register 0x"
119 TARGET_FMT_plx, addr << 2);
120 break;
124 static const MemoryRegionOps softusb_mmio_ops = {
125 .read = softusb_read,
126 .write = softusb_write,
127 .endianness = DEVICE_NATIVE_ENDIAN,
128 .valid = {
129 .min_access_size = 4,
130 .max_access_size = 4,
134 static inline void softusb_read_dmem(MilkymistSoftUsbState *s,
135 uint32_t offset, uint8_t *buf, uint32_t len)
137 if (offset + len >= s->dmem_size) {
138 error_report("milkymist_softusb: read dmem out of bounds "
139 "at offset 0x%x, len %d", offset, len);
140 memset(buf, 0, len);
141 return;
144 memcpy(buf, s->dmem_ptr + offset, len);
147 static inline void softusb_write_dmem(MilkymistSoftUsbState *s,
148 uint32_t offset, uint8_t *buf, uint32_t len)
150 if (offset + len >= s->dmem_size) {
151 error_report("milkymist_softusb: write dmem out of bounds "
152 "at offset 0x%x, len %d", offset, len);
153 return;
156 memcpy(s->dmem_ptr + offset, buf, len);
159 static inline void softusb_read_pmem(MilkymistSoftUsbState *s,
160 uint32_t offset, uint8_t *buf, uint32_t len)
162 if (offset + len >= s->pmem_size) {
163 error_report("milkymist_softusb: read pmem out of bounds "
164 "at offset 0x%x, len %d", offset, len);
165 memset(buf, 0, len);
166 return;
169 memcpy(buf, s->pmem_ptr + offset, len);
172 static inline void softusb_write_pmem(MilkymistSoftUsbState *s,
173 uint32_t offset, uint8_t *buf, uint32_t len)
175 if (offset + len >= s->pmem_size) {
176 error_report("milkymist_softusb: write pmem out of bounds "
177 "at offset 0x%x, len %d", offset, len);
178 return;
181 memcpy(s->pmem_ptr + offset, buf, len);
184 static void softusb_mouse_changed(MilkymistSoftUsbState *s)
186 uint8_t m;
188 softusb_read_dmem(s, COMLOC_MEVT_PRODUCE, &m, 1);
189 trace_milkymist_softusb_mevt(m);
190 softusb_write_dmem(s, COMLOC_MEVT_BASE + 4 * m, s->mouse_hid_buffer, 4);
191 m = (m + 1) & 0xf;
192 softusb_write_dmem(s, COMLOC_MEVT_PRODUCE, &m, 1);
194 trace_milkymist_softusb_pulse_irq();
195 qemu_irq_pulse(s->irq);
198 static void softusb_kbd_changed(MilkymistSoftUsbState *s)
200 uint8_t m;
202 softusb_read_dmem(s, COMLOC_KEVT_PRODUCE, &m, 1);
203 trace_milkymist_softusb_kevt(m);
204 softusb_write_dmem(s, COMLOC_KEVT_BASE + 8 * m, s->kbd_hid_buffer, 8);
205 m = (m + 1) & 0x7;
206 softusb_write_dmem(s, COMLOC_KEVT_PRODUCE, &m, 1);
208 trace_milkymist_softusb_pulse_irq();
209 qemu_irq_pulse(s->irq);
212 static void softusb_kbd_hid_datain(HIDState *hs)
214 MilkymistSoftUsbState *s = container_of(hs, MilkymistSoftUsbState, hid_kbd);
215 int len;
217 /* if device is in reset, do nothing */
218 if (s->regs[R_CTRL] & CTRL_RESET) {
219 return;
222 len = hid_keyboard_poll(hs, s->kbd_hid_buffer, sizeof(s->kbd_hid_buffer));
224 if (len == 8) {
225 softusb_kbd_changed(s);
229 static void softusb_mouse_hid_datain(HIDState *hs)
231 MilkymistSoftUsbState *s =
232 container_of(hs, MilkymistSoftUsbState, hid_mouse);
233 int len;
235 /* if device is in reset, do nothing */
236 if (s->regs[R_CTRL] & CTRL_RESET) {
237 return;
240 len = hid_pointer_poll(hs, s->mouse_hid_buffer,
241 sizeof(s->mouse_hid_buffer));
243 if (len == 4) {
244 softusb_mouse_changed(s);
248 static void milkymist_softusb_reset(DeviceState *d)
250 MilkymistSoftUsbState *s = MILKYMIST_SOFTUSB(d);
251 int i;
253 for (i = 0; i < R_MAX; i++) {
254 s->regs[i] = 0;
256 memset(s->kbd_hid_buffer, 0, sizeof(s->kbd_hid_buffer));
257 memset(s->mouse_hid_buffer, 0, sizeof(s->mouse_hid_buffer));
259 hid_reset(&s->hid_kbd);
260 hid_reset(&s->hid_mouse);
262 /* defaults */
263 s->regs[R_CTRL] = CTRL_RESET;
266 static int milkymist_softusb_init(SysBusDevice *dev)
268 MilkymistSoftUsbState *s = MILKYMIST_SOFTUSB(dev);
270 sysbus_init_irq(dev, &s->irq);
272 memory_region_init_io(&s->regs_region, OBJECT(s), &softusb_mmio_ops, s,
273 "milkymist-softusb", R_MAX * 4);
274 sysbus_init_mmio(dev, &s->regs_region);
276 /* register pmem and dmem */
277 memory_region_init_ram(&s->pmem, OBJECT(s), "milkymist-softusb.pmem",
278 s->pmem_size);
279 vmstate_register_ram_global(&s->pmem);
280 s->pmem_ptr = memory_region_get_ram_ptr(&s->pmem);
281 sysbus_init_mmio(dev, &s->pmem);
282 memory_region_init_ram(&s->dmem, OBJECT(s), "milkymist-softusb.dmem",
283 s->dmem_size);
284 vmstate_register_ram_global(&s->dmem);
285 s->dmem_ptr = memory_region_get_ram_ptr(&s->dmem);
286 sysbus_init_mmio(dev, &s->dmem);
288 hid_init(&s->hid_kbd, HID_KEYBOARD, softusb_kbd_hid_datain);
289 hid_init(&s->hid_mouse, HID_MOUSE, softusb_mouse_hid_datain);
291 return 0;
294 static const VMStateDescription vmstate_milkymist_softusb = {
295 .name = "milkymist-softusb",
296 .version_id = 1,
297 .minimum_version_id = 1,
298 .minimum_version_id_old = 1,
299 .fields = (VMStateField[]) {
300 VMSTATE_UINT32_ARRAY(regs, MilkymistSoftUsbState, R_MAX),
301 VMSTATE_HID_KEYBOARD_DEVICE(hid_kbd, MilkymistSoftUsbState),
302 VMSTATE_HID_POINTER_DEVICE(hid_mouse, MilkymistSoftUsbState),
303 VMSTATE_BUFFER(kbd_hid_buffer, MilkymistSoftUsbState),
304 VMSTATE_BUFFER(mouse_hid_buffer, MilkymistSoftUsbState),
305 VMSTATE_END_OF_LIST()
309 static Property milkymist_softusb_properties[] = {
310 DEFINE_PROP_UINT32("pmem_size", MilkymistSoftUsbState, pmem_size, 0x00001000),
311 DEFINE_PROP_UINT32("dmem_size", MilkymistSoftUsbState, dmem_size, 0x00002000),
312 DEFINE_PROP_END_OF_LIST(),
315 static void milkymist_softusb_class_init(ObjectClass *klass, void *data)
317 DeviceClass *dc = DEVICE_CLASS(klass);
318 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
320 k->init = milkymist_softusb_init;
321 dc->reset = milkymist_softusb_reset;
322 dc->vmsd = &vmstate_milkymist_softusb;
323 dc->props = milkymist_softusb_properties;
326 static const TypeInfo milkymist_softusb_info = {
327 .name = TYPE_MILKYMIST_SOFTUSB,
328 .parent = TYPE_SYS_BUS_DEVICE,
329 .instance_size = sizeof(MilkymistSoftUsbState),
330 .class_init = milkymist_softusb_class_init,
333 static void milkymist_softusb_register_types(void)
335 type_register_static(&milkymist_softusb_info);
338 type_init(milkymist_softusb_register_types)