hw/riscv: Use the CPU to determine if 32-bit
[qemu/ar7.git] / hw / input / milkymist-softusb.c
blobd885c708d7cbaa5ca77a587e331d4c77077749f0
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.1 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 "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"
37 enum {
38 R_CTRL = 0,
39 R_MAX
42 enum {
43 CTRL_RESET = (1<<0),
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
53 #define TYPE_MILKYMIST_SOFTUSB "milkymist-softusb"
54 OBJECT_DECLARE_SIMPLE_TYPE(MilkymistSoftUsbState, MILKYMIST_SOFTUSB)
56 struct MilkymistSoftUsbState {
57 SysBusDevice parent_obj;
59 HIDState hid_kbd;
60 HIDState hid_mouse;
62 MemoryRegion regs_region;
63 MemoryRegion pmem;
64 MemoryRegion dmem;
65 qemu_irq irq;
67 void *pmem_ptr;
68 void *dmem_ptr;
70 /* device properties */
71 uint32_t pmem_size;
72 uint32_t dmem_size;
74 /* device registers */
75 uint32_t regs[R_MAX];
77 /* mouse state */
78 uint8_t mouse_hid_buffer[4];
80 /* keyboard state */
81 uint8_t kbd_hid_buffer[8];
84 static uint64_t softusb_read(void *opaque, hwaddr addr,
85 unsigned size)
87 MilkymistSoftUsbState *s = opaque;
88 uint32_t r = 0;
90 addr >>= 2;
91 switch (addr) {
92 case R_CTRL:
93 r = s->regs[addr];
94 break;
96 default:
97 error_report("milkymist_softusb: read access to unknown register 0x"
98 TARGET_FMT_plx, addr << 2);
99 break;
102 trace_milkymist_softusb_memory_read(addr << 2, r);
104 return r;
107 static void
108 softusb_write(void *opaque, hwaddr addr, uint64_t value,
109 unsigned size)
111 MilkymistSoftUsbState *s = opaque;
113 trace_milkymist_softusb_memory_write(addr, value);
115 addr >>= 2;
116 switch (addr) {
117 case R_CTRL:
118 s->regs[addr] = value;
119 break;
121 default:
122 error_report("milkymist_softusb: write access to unknown register 0x"
123 TARGET_FMT_plx, addr << 2);
124 break;
128 static const MemoryRegionOps softusb_mmio_ops = {
129 .read = softusb_read,
130 .write = softusb_write,
131 .endianness = DEVICE_NATIVE_ENDIAN,
132 .valid = {
133 .min_access_size = 4,
134 .max_access_size = 4,
138 static inline void softusb_read_dmem(MilkymistSoftUsbState *s,
139 uint32_t offset, uint8_t *buf, uint32_t len)
141 if (offset + len >= s->dmem_size) {
142 error_report("milkymist_softusb: read dmem out of bounds "
143 "at offset 0x%x, len %d", offset, len);
144 memset(buf, 0, len);
145 return;
148 memcpy(buf, s->dmem_ptr + offset, len);
151 static inline void softusb_write_dmem(MilkymistSoftUsbState *s,
152 uint32_t offset, uint8_t *buf, uint32_t len)
154 if (offset + len >= s->dmem_size) {
155 error_report("milkymist_softusb: write dmem out of bounds "
156 "at offset 0x%x, len %d", offset, len);
157 return;
160 memcpy(s->dmem_ptr + offset, buf, len);
163 static void softusb_mouse_changed(MilkymistSoftUsbState *s)
165 uint8_t m;
167 softusb_read_dmem(s, COMLOC_MEVT_PRODUCE, &m, 1);
168 trace_milkymist_softusb_mevt(m);
169 softusb_write_dmem(s, COMLOC_MEVT_BASE + 4 * m, s->mouse_hid_buffer, 4);
170 m = (m + 1) & 0xf;
171 softusb_write_dmem(s, COMLOC_MEVT_PRODUCE, &m, 1);
173 trace_milkymist_softusb_pulse_irq();
174 qemu_irq_pulse(s->irq);
177 static void softusb_kbd_changed(MilkymistSoftUsbState *s)
179 uint8_t m;
181 softusb_read_dmem(s, COMLOC_KEVT_PRODUCE, &m, 1);
182 trace_milkymist_softusb_kevt(m);
183 softusb_write_dmem(s, COMLOC_KEVT_BASE + 8 * m, s->kbd_hid_buffer, 8);
184 m = (m + 1) & 0x7;
185 softusb_write_dmem(s, COMLOC_KEVT_PRODUCE, &m, 1);
187 trace_milkymist_softusb_pulse_irq();
188 qemu_irq_pulse(s->irq);
191 static void softusb_kbd_hid_datain(HIDState *hs)
193 MilkymistSoftUsbState *s = container_of(hs, MilkymistSoftUsbState, hid_kbd);
194 int len;
196 /* if device is in reset, do nothing */
197 if (s->regs[R_CTRL] & CTRL_RESET) {
198 return;
201 while (hid_has_events(hs)) {
202 len = hid_keyboard_poll(hs, s->kbd_hid_buffer,
203 sizeof(s->kbd_hid_buffer));
205 if (len == 8) {
206 softusb_kbd_changed(s);
211 static void softusb_mouse_hid_datain(HIDState *hs)
213 MilkymistSoftUsbState *s =
214 container_of(hs, MilkymistSoftUsbState, hid_mouse);
215 int len;
217 /* if device is in reset, do nothing */
218 if (s->regs[R_CTRL] & CTRL_RESET) {
219 return;
222 while (hid_has_events(hs)) {
223 len = hid_pointer_poll(hs, s->mouse_hid_buffer,
224 sizeof(s->mouse_hid_buffer));
226 if (len == 4) {
227 softusb_mouse_changed(s);
232 static void milkymist_softusb_reset(DeviceState *d)
234 MilkymistSoftUsbState *s = MILKYMIST_SOFTUSB(d);
235 int i;
237 for (i = 0; i < R_MAX; i++) {
238 s->regs[i] = 0;
240 memset(s->kbd_hid_buffer, 0, sizeof(s->kbd_hid_buffer));
241 memset(s->mouse_hid_buffer, 0, sizeof(s->mouse_hid_buffer));
243 hid_reset(&s->hid_kbd);
244 hid_reset(&s->hid_mouse);
246 /* defaults */
247 s->regs[R_CTRL] = CTRL_RESET;
250 static void milkymist_softusb_realize(DeviceState *dev, Error **errp)
252 MilkymistSoftUsbState *s = MILKYMIST_SOFTUSB(dev);
253 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
255 sysbus_init_irq(sbd, &s->irq);
257 memory_region_init_io(&s->regs_region, OBJECT(s), &softusb_mmio_ops, s,
258 "milkymist-softusb", R_MAX * 4);
259 sysbus_init_mmio(sbd, &s->regs_region);
261 /* register pmem and dmem */
262 memory_region_init_ram_nomigrate(&s->pmem, OBJECT(s), "milkymist-softusb.pmem",
263 s->pmem_size, &error_fatal);
264 vmstate_register_ram_global(&s->pmem);
265 s->pmem_ptr = memory_region_get_ram_ptr(&s->pmem);
266 sysbus_init_mmio(sbd, &s->pmem);
267 memory_region_init_ram_nomigrate(&s->dmem, OBJECT(s), "milkymist-softusb.dmem",
268 s->dmem_size, &error_fatal);
269 vmstate_register_ram_global(&s->dmem);
270 s->dmem_ptr = memory_region_get_ram_ptr(&s->dmem);
271 sysbus_init_mmio(sbd, &s->dmem);
273 hid_init(&s->hid_kbd, HID_KEYBOARD, softusb_kbd_hid_datain);
274 hid_init(&s->hid_mouse, HID_MOUSE, softusb_mouse_hid_datain);
277 static const VMStateDescription vmstate_milkymist_softusb = {
278 .name = "milkymist-softusb",
279 .version_id = 1,
280 .minimum_version_id = 1,
281 .fields = (VMStateField[]) {
282 VMSTATE_UINT32_ARRAY(regs, MilkymistSoftUsbState, R_MAX),
283 VMSTATE_HID_KEYBOARD_DEVICE(hid_kbd, MilkymistSoftUsbState),
284 VMSTATE_HID_POINTER_DEVICE(hid_mouse, MilkymistSoftUsbState),
285 VMSTATE_BUFFER(kbd_hid_buffer, MilkymistSoftUsbState),
286 VMSTATE_BUFFER(mouse_hid_buffer, MilkymistSoftUsbState),
287 VMSTATE_END_OF_LIST()
291 static Property milkymist_softusb_properties[] = {
292 DEFINE_PROP_UINT32("pmem_size", MilkymistSoftUsbState, pmem_size, 0x00001000),
293 DEFINE_PROP_UINT32("dmem_size", MilkymistSoftUsbState, dmem_size, 0x00002000),
294 DEFINE_PROP_END_OF_LIST(),
297 static void milkymist_softusb_class_init(ObjectClass *klass, void *data)
299 DeviceClass *dc = DEVICE_CLASS(klass);
301 dc->realize = milkymist_softusb_realize;
302 dc->reset = milkymist_softusb_reset;
303 dc->vmsd = &vmstate_milkymist_softusb;
304 device_class_set_props(dc, milkymist_softusb_properties);
307 static const TypeInfo milkymist_softusb_info = {
308 .name = TYPE_MILKYMIST_SOFTUSB,
309 .parent = TYPE_SYS_BUS_DEVICE,
310 .instance_size = sizeof(MilkymistSoftUsbState),
311 .class_init = milkymist_softusb_class_init,
314 static void milkymist_softusb_register_types(void)
316 type_register_static(&milkymist_softusb_info);
319 type_init(milkymist_softusb_register_types)