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:
24 #include "qemu/osdep.h"
25 #include "qapi/error.h"
26 #include "hw/sysbus.h"
27 #include "migration/vmstate.h"
29 #include "ui/console.h"
30 #include "hw/input/hid.h"
32 #include "hw/qdev-properties.h"
33 #include "qemu/error-report.h"
34 #include "qemu/module.h"
45 #define COMLOC_DEBUG_PRODUCE 0x1000
46 #define COMLOC_DEBUG_BASE 0x1001
47 #define COMLOC_MEVT_PRODUCE 0x1101
48 #define COMLOC_MEVT_BASE 0x1102
49 #define COMLOC_KEVT_PRODUCE 0x1142
50 #define COMLOC_KEVT_BASE 0x1143
52 #define TYPE_MILKYMIST_SOFTUSB "milkymist-softusb"
53 #define MILKYMIST_SOFTUSB(obj) \
54 OBJECT_CHECK(MilkymistSoftUsbState, (obj), TYPE_MILKYMIST_SOFTUSB)
56 struct MilkymistSoftUsbState
{
57 SysBusDevice parent_obj
;
62 MemoryRegion regs_region
;
70 /* device properties */
74 /* device registers */
78 uint8_t mouse_hid_buffer
[4];
81 uint8_t kbd_hid_buffer
[8];
83 typedef struct MilkymistSoftUsbState MilkymistSoftUsbState
;
85 static uint64_t softusb_read(void *opaque
, hwaddr addr
,
88 MilkymistSoftUsbState
*s
= opaque
;
98 error_report("milkymist_softusb: read access to unknown register 0x"
99 TARGET_FMT_plx
, addr
<< 2);
103 trace_milkymist_softusb_memory_read(addr
<< 2, r
);
109 softusb_write(void *opaque
, hwaddr addr
, uint64_t value
,
112 MilkymistSoftUsbState
*s
= opaque
;
114 trace_milkymist_softusb_memory_write(addr
, value
);
119 s
->regs
[addr
] = value
;
123 error_report("milkymist_softusb: write access to unknown register 0x"
124 TARGET_FMT_plx
, addr
<< 2);
129 static const MemoryRegionOps softusb_mmio_ops
= {
130 .read
= softusb_read
,
131 .write
= softusb_write
,
132 .endianness
= DEVICE_NATIVE_ENDIAN
,
134 .min_access_size
= 4,
135 .max_access_size
= 4,
139 static inline void softusb_read_dmem(MilkymistSoftUsbState
*s
,
140 uint32_t offset
, uint8_t *buf
, uint32_t len
)
142 if (offset
+ len
>= s
->dmem_size
) {
143 error_report("milkymist_softusb: read dmem out of bounds "
144 "at offset 0x%x, len %d", offset
, len
);
149 memcpy(buf
, s
->dmem_ptr
+ offset
, len
);
152 static inline void softusb_write_dmem(MilkymistSoftUsbState
*s
,
153 uint32_t offset
, uint8_t *buf
, uint32_t len
)
155 if (offset
+ len
>= s
->dmem_size
) {
156 error_report("milkymist_softusb: write dmem out of bounds "
157 "at offset 0x%x, len %d", offset
, len
);
161 memcpy(s
->dmem_ptr
+ offset
, buf
, len
);
164 static void softusb_mouse_changed(MilkymistSoftUsbState
*s
)
168 softusb_read_dmem(s
, COMLOC_MEVT_PRODUCE
, &m
, 1);
169 trace_milkymist_softusb_mevt(m
);
170 softusb_write_dmem(s
, COMLOC_MEVT_BASE
+ 4 * m
, s
->mouse_hid_buffer
, 4);
172 softusb_write_dmem(s
, COMLOC_MEVT_PRODUCE
, &m
, 1);
174 trace_milkymist_softusb_pulse_irq();
175 qemu_irq_pulse(s
->irq
);
178 static void softusb_kbd_changed(MilkymistSoftUsbState
*s
)
182 softusb_read_dmem(s
, COMLOC_KEVT_PRODUCE
, &m
, 1);
183 trace_milkymist_softusb_kevt(m
);
184 softusb_write_dmem(s
, COMLOC_KEVT_BASE
+ 8 * m
, s
->kbd_hid_buffer
, 8);
186 softusb_write_dmem(s
, COMLOC_KEVT_PRODUCE
, &m
, 1);
188 trace_milkymist_softusb_pulse_irq();
189 qemu_irq_pulse(s
->irq
);
192 static void softusb_kbd_hid_datain(HIDState
*hs
)
194 MilkymistSoftUsbState
*s
= container_of(hs
, MilkymistSoftUsbState
, hid_kbd
);
197 /* if device is in reset, do nothing */
198 if (s
->regs
[R_CTRL
] & CTRL_RESET
) {
202 while (hid_has_events(hs
)) {
203 len
= hid_keyboard_poll(hs
, s
->kbd_hid_buffer
,
204 sizeof(s
->kbd_hid_buffer
));
207 softusb_kbd_changed(s
);
212 static void softusb_mouse_hid_datain(HIDState
*hs
)
214 MilkymistSoftUsbState
*s
=
215 container_of(hs
, MilkymistSoftUsbState
, hid_mouse
);
218 /* if device is in reset, do nothing */
219 if (s
->regs
[R_CTRL
] & CTRL_RESET
) {
223 while (hid_has_events(hs
)) {
224 len
= hid_pointer_poll(hs
, s
->mouse_hid_buffer
,
225 sizeof(s
->mouse_hid_buffer
));
228 softusb_mouse_changed(s
);
233 static void milkymist_softusb_reset(DeviceState
*d
)
235 MilkymistSoftUsbState
*s
= MILKYMIST_SOFTUSB(d
);
238 for (i
= 0; i
< R_MAX
; i
++) {
241 memset(s
->kbd_hid_buffer
, 0, sizeof(s
->kbd_hid_buffer
));
242 memset(s
->mouse_hid_buffer
, 0, sizeof(s
->mouse_hid_buffer
));
244 hid_reset(&s
->hid_kbd
);
245 hid_reset(&s
->hid_mouse
);
248 s
->regs
[R_CTRL
] = CTRL_RESET
;
251 static void milkymist_softusb_realize(DeviceState
*dev
, Error
**errp
)
253 MilkymistSoftUsbState
*s
= MILKYMIST_SOFTUSB(dev
);
254 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
256 sysbus_init_irq(sbd
, &s
->irq
);
258 memory_region_init_io(&s
->regs_region
, OBJECT(s
), &softusb_mmio_ops
, s
,
259 "milkymist-softusb", R_MAX
* 4);
260 sysbus_init_mmio(sbd
, &s
->regs_region
);
262 /* register pmem and dmem */
263 memory_region_init_ram_nomigrate(&s
->pmem
, OBJECT(s
), "milkymist-softusb.pmem",
264 s
->pmem_size
, &error_fatal
);
265 vmstate_register_ram_global(&s
->pmem
);
266 s
->pmem_ptr
= memory_region_get_ram_ptr(&s
->pmem
);
267 sysbus_init_mmio(sbd
, &s
->pmem
);
268 memory_region_init_ram_nomigrate(&s
->dmem
, OBJECT(s
), "milkymist-softusb.dmem",
269 s
->dmem_size
, &error_fatal
);
270 vmstate_register_ram_global(&s
->dmem
);
271 s
->dmem_ptr
= memory_region_get_ram_ptr(&s
->dmem
);
272 sysbus_init_mmio(sbd
, &s
->dmem
);
274 hid_init(&s
->hid_kbd
, HID_KEYBOARD
, softusb_kbd_hid_datain
);
275 hid_init(&s
->hid_mouse
, HID_MOUSE
, softusb_mouse_hid_datain
);
278 static const VMStateDescription vmstate_milkymist_softusb
= {
279 .name
= "milkymist-softusb",
281 .minimum_version_id
= 1,
282 .fields
= (VMStateField
[]) {
283 VMSTATE_UINT32_ARRAY(regs
, MilkymistSoftUsbState
, R_MAX
),
284 VMSTATE_HID_KEYBOARD_DEVICE(hid_kbd
, MilkymistSoftUsbState
),
285 VMSTATE_HID_POINTER_DEVICE(hid_mouse
, MilkymistSoftUsbState
),
286 VMSTATE_BUFFER(kbd_hid_buffer
, MilkymistSoftUsbState
),
287 VMSTATE_BUFFER(mouse_hid_buffer
, MilkymistSoftUsbState
),
288 VMSTATE_END_OF_LIST()
292 static Property milkymist_softusb_properties
[] = {
293 DEFINE_PROP_UINT32("pmem_size", MilkymistSoftUsbState
, pmem_size
, 0x00001000),
294 DEFINE_PROP_UINT32("dmem_size", MilkymistSoftUsbState
, dmem_size
, 0x00002000),
295 DEFINE_PROP_END_OF_LIST(),
298 static void milkymist_softusb_class_init(ObjectClass
*klass
, void *data
)
300 DeviceClass
*dc
= DEVICE_CLASS(klass
);
302 dc
->realize
= milkymist_softusb_realize
;
303 dc
->reset
= milkymist_softusb_reset
;
304 dc
->vmsd
= &vmstate_milkymist_softusb
;
305 device_class_set_props(dc
, milkymist_softusb_properties
);
308 static const TypeInfo milkymist_softusb_info
= {
309 .name
= TYPE_MILKYMIST_SOFTUSB
,
310 .parent
= TYPE_SYS_BUS_DEVICE
,
311 .instance_size
= sizeof(MilkymistSoftUsbState
),
312 .class_init
= milkymist_softusb_class_init
,
315 static void milkymist_softusb_register_types(void)
317 type_register_static(&milkymist_softusb_info
);
320 type_init(milkymist_softusb_register_types
)