blockjob: remove clock argument from block_job_sleep_ns
[qemu/ar7.git] / hw / input / milkymist-softusb.c
blobef8f47cd8340ff12629d9312b817946cebc9540e
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 "qemu/osdep.h"
25 #include "qapi/error.h"
26 #include "hw/hw.h"
27 #include "hw/sysbus.h"
28 #include "trace.h"
29 #include "ui/console.h"
30 #include "hw/input/hid.h"
31 #include "qemu/error-report.h"
33 enum {
34 R_CTRL = 0,
35 R_MAX
38 enum {
39 CTRL_RESET = (1<<0),
42 #define COMLOC_DEBUG_PRODUCE 0x1000
43 #define COMLOC_DEBUG_BASE 0x1001
44 #define COMLOC_MEVT_PRODUCE 0x1101
45 #define COMLOC_MEVT_BASE 0x1102
46 #define COMLOC_KEVT_PRODUCE 0x1142
47 #define COMLOC_KEVT_BASE 0x1143
49 #define TYPE_MILKYMIST_SOFTUSB "milkymist-softusb"
50 #define MILKYMIST_SOFTUSB(obj) \
51 OBJECT_CHECK(MilkymistSoftUsbState, (obj), TYPE_MILKYMIST_SOFTUSB)
53 struct MilkymistSoftUsbState {
54 SysBusDevice parent_obj;
56 HIDState hid_kbd;
57 HIDState hid_mouse;
59 MemoryRegion regs_region;
60 MemoryRegion pmem;
61 MemoryRegion dmem;
62 qemu_irq irq;
64 void *pmem_ptr;
65 void *dmem_ptr;
67 /* device properties */
68 uint32_t pmem_size;
69 uint32_t dmem_size;
71 /* device registers */
72 uint32_t regs[R_MAX];
74 /* mouse state */
75 uint8_t mouse_hid_buffer[4];
77 /* keyboard state */
78 uint8_t kbd_hid_buffer[8];
80 typedef struct MilkymistSoftUsbState MilkymistSoftUsbState;
82 static uint64_t softusb_read(void *opaque, hwaddr addr,
83 unsigned size)
85 MilkymistSoftUsbState *s = opaque;
86 uint32_t r = 0;
88 addr >>= 2;
89 switch (addr) {
90 case R_CTRL:
91 r = s->regs[addr];
92 break;
94 default:
95 error_report("milkymist_softusb: read access to unknown register 0x"
96 TARGET_FMT_plx, addr << 2);
97 break;
100 trace_milkymist_softusb_memory_read(addr << 2, r);
102 return r;
105 static void
106 softusb_write(void *opaque, hwaddr addr, uint64_t value,
107 unsigned size)
109 MilkymistSoftUsbState *s = opaque;
111 trace_milkymist_softusb_memory_write(addr, value);
113 addr >>= 2;
114 switch (addr) {
115 case R_CTRL:
116 s->regs[addr] = value;
117 break;
119 default:
120 error_report("milkymist_softusb: write access to unknown register 0x"
121 TARGET_FMT_plx, addr << 2);
122 break;
126 static const MemoryRegionOps softusb_mmio_ops = {
127 .read = softusb_read,
128 .write = softusb_write,
129 .endianness = DEVICE_NATIVE_ENDIAN,
130 .valid = {
131 .min_access_size = 4,
132 .max_access_size = 4,
136 static inline void softusb_read_dmem(MilkymistSoftUsbState *s,
137 uint32_t offset, uint8_t *buf, uint32_t len)
139 if (offset + len >= s->dmem_size) {
140 error_report("milkymist_softusb: read dmem out of bounds "
141 "at offset 0x%x, len %d", offset, len);
142 memset(buf, 0, len);
143 return;
146 memcpy(buf, s->dmem_ptr + offset, len);
149 static inline void softusb_write_dmem(MilkymistSoftUsbState *s,
150 uint32_t offset, uint8_t *buf, uint32_t len)
152 if (offset + len >= s->dmem_size) {
153 error_report("milkymist_softusb: write dmem out of bounds "
154 "at offset 0x%x, len %d", offset, len);
155 return;
158 memcpy(s->dmem_ptr + offset, buf, len);
161 static void softusb_mouse_changed(MilkymistSoftUsbState *s)
163 uint8_t m;
165 softusb_read_dmem(s, COMLOC_MEVT_PRODUCE, &m, 1);
166 trace_milkymist_softusb_mevt(m);
167 softusb_write_dmem(s, COMLOC_MEVT_BASE + 4 * m, s->mouse_hid_buffer, 4);
168 m = (m + 1) & 0xf;
169 softusb_write_dmem(s, COMLOC_MEVT_PRODUCE, &m, 1);
171 trace_milkymist_softusb_pulse_irq();
172 qemu_irq_pulse(s->irq);
175 static void softusb_kbd_changed(MilkymistSoftUsbState *s)
177 uint8_t m;
179 softusb_read_dmem(s, COMLOC_KEVT_PRODUCE, &m, 1);
180 trace_milkymist_softusb_kevt(m);
181 softusb_write_dmem(s, COMLOC_KEVT_BASE + 8 * m, s->kbd_hid_buffer, 8);
182 m = (m + 1) & 0x7;
183 softusb_write_dmem(s, COMLOC_KEVT_PRODUCE, &m, 1);
185 trace_milkymist_softusb_pulse_irq();
186 qemu_irq_pulse(s->irq);
189 static void softusb_kbd_hid_datain(HIDState *hs)
191 MilkymistSoftUsbState *s = container_of(hs, MilkymistSoftUsbState, hid_kbd);
192 int len;
194 /* if device is in reset, do nothing */
195 if (s->regs[R_CTRL] & CTRL_RESET) {
196 return;
199 while (hid_has_events(hs)) {
200 len = hid_keyboard_poll(hs, s->kbd_hid_buffer,
201 sizeof(s->kbd_hid_buffer));
203 if (len == 8) {
204 softusb_kbd_changed(s);
209 static void softusb_mouse_hid_datain(HIDState *hs)
211 MilkymistSoftUsbState *s =
212 container_of(hs, MilkymistSoftUsbState, hid_mouse);
213 int len;
215 /* if device is in reset, do nothing */
216 if (s->regs[R_CTRL] & CTRL_RESET) {
217 return;
220 while (hid_has_events(hs)) {
221 len = hid_pointer_poll(hs, s->mouse_hid_buffer,
222 sizeof(s->mouse_hid_buffer));
224 if (len == 4) {
225 softusb_mouse_changed(s);
230 static void milkymist_softusb_reset(DeviceState *d)
232 MilkymistSoftUsbState *s = MILKYMIST_SOFTUSB(d);
233 int i;
235 for (i = 0; i < R_MAX; i++) {
236 s->regs[i] = 0;
238 memset(s->kbd_hid_buffer, 0, sizeof(s->kbd_hid_buffer));
239 memset(s->mouse_hid_buffer, 0, sizeof(s->mouse_hid_buffer));
241 hid_reset(&s->hid_kbd);
242 hid_reset(&s->hid_mouse);
244 /* defaults */
245 s->regs[R_CTRL] = CTRL_RESET;
248 static int milkymist_softusb_init(SysBusDevice *dev)
250 MilkymistSoftUsbState *s = MILKYMIST_SOFTUSB(dev);
252 sysbus_init_irq(dev, &s->irq);
254 memory_region_init_io(&s->regs_region, OBJECT(s), &softusb_mmio_ops, s,
255 "milkymist-softusb", R_MAX * 4);
256 sysbus_init_mmio(dev, &s->regs_region);
258 /* register pmem and dmem */
259 memory_region_init_ram_nomigrate(&s->pmem, OBJECT(s), "milkymist-softusb.pmem",
260 s->pmem_size, &error_fatal);
261 vmstate_register_ram_global(&s->pmem);
262 s->pmem_ptr = memory_region_get_ram_ptr(&s->pmem);
263 sysbus_init_mmio(dev, &s->pmem);
264 memory_region_init_ram_nomigrate(&s->dmem, OBJECT(s), "milkymist-softusb.dmem",
265 s->dmem_size, &error_fatal);
266 vmstate_register_ram_global(&s->dmem);
267 s->dmem_ptr = memory_region_get_ram_ptr(&s->dmem);
268 sysbus_init_mmio(dev, &s->dmem);
270 hid_init(&s->hid_kbd, HID_KEYBOARD, softusb_kbd_hid_datain);
271 hid_init(&s->hid_mouse, HID_MOUSE, softusb_mouse_hid_datain);
273 return 0;
276 static const VMStateDescription vmstate_milkymist_softusb = {
277 .name = "milkymist-softusb",
278 .version_id = 1,
279 .minimum_version_id = 1,
280 .fields = (VMStateField[]) {
281 VMSTATE_UINT32_ARRAY(regs, MilkymistSoftUsbState, R_MAX),
282 VMSTATE_HID_KEYBOARD_DEVICE(hid_kbd, MilkymistSoftUsbState),
283 VMSTATE_HID_POINTER_DEVICE(hid_mouse, MilkymistSoftUsbState),
284 VMSTATE_BUFFER(kbd_hid_buffer, MilkymistSoftUsbState),
285 VMSTATE_BUFFER(mouse_hid_buffer, MilkymistSoftUsbState),
286 VMSTATE_END_OF_LIST()
290 static Property milkymist_softusb_properties[] = {
291 DEFINE_PROP_UINT32("pmem_size", MilkymistSoftUsbState, pmem_size, 0x00001000),
292 DEFINE_PROP_UINT32("dmem_size", MilkymistSoftUsbState, dmem_size, 0x00002000),
293 DEFINE_PROP_END_OF_LIST(),
296 static void milkymist_softusb_class_init(ObjectClass *klass, void *data)
298 DeviceClass *dc = DEVICE_CLASS(klass);
299 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
301 k->init = milkymist_softusb_init;
302 dc->reset = milkymist_softusb_reset;
303 dc->vmsd = &vmstate_milkymist_softusb;
304 dc->props = 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)