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:
25 #include "hw/sysbus.h"
27 #include "ui/console.h"
28 #include "hw/input/hid.h"
29 #include "qemu/error-report.h"
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
;
57 MemoryRegion regs_region
;
65 /* device properties */
69 /* device registers */
73 uint8_t mouse_hid_buffer
[4];
76 uint8_t kbd_hid_buffer
[8];
78 typedef struct MilkymistSoftUsbState MilkymistSoftUsbState
;
80 static uint64_t softusb_read(void *opaque
, hwaddr addr
,
83 MilkymistSoftUsbState
*s
= opaque
;
93 error_report("milkymist_softusb: read access to unknown register 0x"
94 TARGET_FMT_plx
, addr
<< 2);
98 trace_milkymist_softusb_memory_read(addr
<< 2, r
);
104 softusb_write(void *opaque
, hwaddr addr
, uint64_t value
,
107 MilkymistSoftUsbState
*s
= opaque
;
109 trace_milkymist_softusb_memory_write(addr
, value
);
114 s
->regs
[addr
] = value
;
118 error_report("milkymist_softusb: write access to unknown register 0x"
119 TARGET_FMT_plx
, addr
<< 2);
124 static const MemoryRegionOps softusb_mmio_ops
= {
125 .read
= softusb_read
,
126 .write
= softusb_write
,
127 .endianness
= DEVICE_NATIVE_ENDIAN
,
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
);
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
);
156 memcpy(s
->dmem_ptr
+ offset
, buf
, len
);
159 static void softusb_mouse_changed(MilkymistSoftUsbState
*s
)
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);
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
)
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);
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
);
192 /* if device is in reset, do nothing */
193 if (s
->regs
[R_CTRL
] & CTRL_RESET
) {
197 while (hid_has_events(hs
)) {
198 len
= hid_keyboard_poll(hs
, s
->kbd_hid_buffer
,
199 sizeof(s
->kbd_hid_buffer
));
202 softusb_kbd_changed(s
);
207 static void softusb_mouse_hid_datain(HIDState
*hs
)
209 MilkymistSoftUsbState
*s
=
210 container_of(hs
, MilkymistSoftUsbState
, hid_mouse
);
213 /* if device is in reset, do nothing */
214 if (s
->regs
[R_CTRL
] & CTRL_RESET
) {
218 while (hid_has_events(hs
)) {
219 len
= hid_pointer_poll(hs
, s
->mouse_hid_buffer
,
220 sizeof(s
->mouse_hid_buffer
));
223 softusb_mouse_changed(s
);
228 static void milkymist_softusb_reset(DeviceState
*d
)
230 MilkymistSoftUsbState
*s
= MILKYMIST_SOFTUSB(d
);
233 for (i
= 0; i
< R_MAX
; i
++) {
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
);
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_fatal
);
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_fatal
);
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
);
274 static const VMStateDescription vmstate_milkymist_softusb
= {
275 .name
= "milkymist-softusb",
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
)