target-arm: Pass timeridx as argument to various timer functions
[qemu/ar7.git] / hw / input / milkymist-softusb.c
blob7b0f4db88d87eaa1a8abead6b61431f520f83c9d
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 void softusb_mouse_changed(MilkymistSoftUsbState *s)
161 uint8_t m;
163 softusb_read_dmem(s, COMLOC_MEVT_PRODUCE, &m, 1);
164 trace_milkymist_softusb_mevt(m);
165 softusb_write_dmem(s, COMLOC_MEVT_BASE + 4 * m, s->mouse_hid_buffer, 4);
166 m = (m + 1) & 0xf;
167 softusb_write_dmem(s, COMLOC_MEVT_PRODUCE, &m, 1);
169 trace_milkymist_softusb_pulse_irq();
170 qemu_irq_pulse(s->irq);
173 static void softusb_kbd_changed(MilkymistSoftUsbState *s)
175 uint8_t m;
177 softusb_read_dmem(s, COMLOC_KEVT_PRODUCE, &m, 1);
178 trace_milkymist_softusb_kevt(m);
179 softusb_write_dmem(s, COMLOC_KEVT_BASE + 8 * m, s->kbd_hid_buffer, 8);
180 m = (m + 1) & 0x7;
181 softusb_write_dmem(s, COMLOC_KEVT_PRODUCE, &m, 1);
183 trace_milkymist_softusb_pulse_irq();
184 qemu_irq_pulse(s->irq);
187 static void softusb_kbd_hid_datain(HIDState *hs)
189 MilkymistSoftUsbState *s = container_of(hs, MilkymistSoftUsbState, hid_kbd);
190 int len;
192 /* if device is in reset, do nothing */
193 if (s->regs[R_CTRL] & CTRL_RESET) {
194 return;
197 while (hid_has_events(hs)) {
198 len = hid_keyboard_poll(hs, s->kbd_hid_buffer,
199 sizeof(s->kbd_hid_buffer));
201 if (len == 8) {
202 softusb_kbd_changed(s);
207 static void softusb_mouse_hid_datain(HIDState *hs)
209 MilkymistSoftUsbState *s =
210 container_of(hs, MilkymistSoftUsbState, hid_mouse);
211 int len;
213 /* if device is in reset, do nothing */
214 if (s->regs[R_CTRL] & CTRL_RESET) {
215 return;
218 while (hid_has_events(hs)) {
219 len = hid_pointer_poll(hs, s->mouse_hid_buffer,
220 sizeof(s->mouse_hid_buffer));
222 if (len == 4) {
223 softusb_mouse_changed(s);
228 static void milkymist_softusb_reset(DeviceState *d)
230 MilkymistSoftUsbState *s = MILKYMIST_SOFTUSB(d);
231 int i;
233 for (i = 0; i < R_MAX; i++) {
234 s->regs[i] = 0;
236 memset(s->kbd_hid_buffer, 0, sizeof(s->kbd_hid_buffer));
237 memset(s->mouse_hid_buffer, 0, sizeof(s->mouse_hid_buffer));
239 hid_reset(&s->hid_kbd);
240 hid_reset(&s->hid_mouse);
242 /* defaults */
243 s->regs[R_CTRL] = CTRL_RESET;
246 static int milkymist_softusb_init(SysBusDevice *dev)
248 MilkymistSoftUsbState *s = MILKYMIST_SOFTUSB(dev);
250 sysbus_init_irq(dev, &s->irq);
252 memory_region_init_io(&s->regs_region, OBJECT(s), &softusb_mmio_ops, s,
253 "milkymist-softusb", R_MAX * 4);
254 sysbus_init_mmio(dev, &s->regs_region);
256 /* register pmem and dmem */
257 memory_region_init_ram(&s->pmem, OBJECT(s), "milkymist-softusb.pmem",
258 s->pmem_size, &error_abort);
259 vmstate_register_ram_global(&s->pmem);
260 s->pmem_ptr = memory_region_get_ram_ptr(&s->pmem);
261 sysbus_init_mmio(dev, &s->pmem);
262 memory_region_init_ram(&s->dmem, OBJECT(s), "milkymist-softusb.dmem",
263 s->dmem_size, &error_abort);
264 vmstate_register_ram_global(&s->dmem);
265 s->dmem_ptr = memory_region_get_ram_ptr(&s->dmem);
266 sysbus_init_mmio(dev, &s->dmem);
268 hid_init(&s->hid_kbd, HID_KEYBOARD, softusb_kbd_hid_datain);
269 hid_init(&s->hid_mouse, HID_MOUSE, softusb_mouse_hid_datain);
271 return 0;
274 static const VMStateDescription vmstate_milkymist_softusb = {
275 .name = "milkymist-softusb",
276 .version_id = 1,
277 .minimum_version_id = 1,
278 .fields = (VMStateField[]) {
279 VMSTATE_UINT32_ARRAY(regs, MilkymistSoftUsbState, R_MAX),
280 VMSTATE_HID_KEYBOARD_DEVICE(hid_kbd, MilkymistSoftUsbState),
281 VMSTATE_HID_POINTER_DEVICE(hid_mouse, MilkymistSoftUsbState),
282 VMSTATE_BUFFER(kbd_hid_buffer, MilkymistSoftUsbState),
283 VMSTATE_BUFFER(mouse_hid_buffer, MilkymistSoftUsbState),
284 VMSTATE_END_OF_LIST()
288 static Property milkymist_softusb_properties[] = {
289 DEFINE_PROP_UINT32("pmem_size", MilkymistSoftUsbState, pmem_size, 0x00001000),
290 DEFINE_PROP_UINT32("dmem_size", MilkymistSoftUsbState, dmem_size, 0x00002000),
291 DEFINE_PROP_END_OF_LIST(),
294 static void milkymist_softusb_class_init(ObjectClass *klass, void *data)
296 DeviceClass *dc = DEVICE_CLASS(klass);
297 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
299 k->init = milkymist_softusb_init;
300 dc->reset = milkymist_softusb_reset;
301 dc->vmsd = &vmstate_milkymist_softusb;
302 dc->props = milkymist_softusb_properties;
305 static const TypeInfo milkymist_softusb_info = {
306 .name = TYPE_MILKYMIST_SOFTUSB,
307 .parent = TYPE_SYS_BUS_DEVICE,
308 .instance_size = sizeof(MilkymistSoftUsbState),
309 .class_init = milkymist_softusb_class_init,
312 static void milkymist_softusb_register_types(void)
314 type_register_static(&milkymist_softusb_info);
317 type_init(milkymist_softusb_register_types)