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 struct MilkymistSoftUsbState
{
52 MemoryRegion regs_region
;
60 /* device properties */
64 /* device registers */
68 uint8_t mouse_hid_buffer
[4];
71 uint8_t kbd_hid_buffer
[8];
73 typedef struct MilkymistSoftUsbState MilkymistSoftUsbState
;
75 static uint64_t softusb_read(void *opaque
, hwaddr addr
,
78 MilkymistSoftUsbState
*s
= opaque
;
88 error_report("milkymist_softusb: read access to unknown register 0x"
89 TARGET_FMT_plx
, addr
<< 2);
93 trace_milkymist_softusb_memory_read(addr
<< 2, r
);
99 softusb_write(void *opaque
, hwaddr addr
, uint64_t value
,
102 MilkymistSoftUsbState
*s
= opaque
;
104 trace_milkymist_softusb_memory_write(addr
, value
);
109 s
->regs
[addr
] = value
;
113 error_report("milkymist_softusb: write access to unknown register 0x"
114 TARGET_FMT_plx
, addr
<< 2);
119 static const MemoryRegionOps softusb_mmio_ops
= {
120 .read
= softusb_read
,
121 .write
= softusb_write
,
122 .endianness
= DEVICE_NATIVE_ENDIAN
,
124 .min_access_size
= 4,
125 .max_access_size
= 4,
129 static inline void softusb_read_dmem(MilkymistSoftUsbState
*s
,
130 uint32_t offset
, uint8_t *buf
, uint32_t len
)
132 if (offset
+ len
>= s
->dmem_size
) {
133 error_report("milkymist_softusb: read dmem out of bounds "
134 "at offset 0x%x, len %d", offset
, len
);
139 memcpy(buf
, s
->dmem_ptr
+ offset
, len
);
142 static inline void softusb_write_dmem(MilkymistSoftUsbState
*s
,
143 uint32_t offset
, uint8_t *buf
, uint32_t len
)
145 if (offset
+ len
>= s
->dmem_size
) {
146 error_report("milkymist_softusb: write dmem out of bounds "
147 "at offset 0x%x, len %d", offset
, len
);
151 memcpy(s
->dmem_ptr
+ offset
, buf
, len
);
154 static inline void softusb_read_pmem(MilkymistSoftUsbState
*s
,
155 uint32_t offset
, uint8_t *buf
, uint32_t len
)
157 if (offset
+ len
>= s
->pmem_size
) {
158 error_report("milkymist_softusb: read pmem out of bounds "
159 "at offset 0x%x, len %d", offset
, len
);
164 memcpy(buf
, s
->pmem_ptr
+ offset
, len
);
167 static inline void softusb_write_pmem(MilkymistSoftUsbState
*s
,
168 uint32_t offset
, uint8_t *buf
, uint32_t len
)
170 if (offset
+ len
>= s
->pmem_size
) {
171 error_report("milkymist_softusb: write pmem out of bounds "
172 "at offset 0x%x, len %d", offset
, len
);
176 memcpy(s
->pmem_ptr
+ offset
, buf
, len
);
179 static void softusb_mouse_changed(MilkymistSoftUsbState
*s
)
183 softusb_read_dmem(s
, COMLOC_MEVT_PRODUCE
, &m
, 1);
184 trace_milkymist_softusb_mevt(m
);
185 softusb_write_dmem(s
, COMLOC_MEVT_BASE
+ 4 * m
, s
->mouse_hid_buffer
, 4);
187 softusb_write_dmem(s
, COMLOC_MEVT_PRODUCE
, &m
, 1);
189 trace_milkymist_softusb_pulse_irq();
190 qemu_irq_pulse(s
->irq
);
193 static void softusb_kbd_changed(MilkymistSoftUsbState
*s
)
197 softusb_read_dmem(s
, COMLOC_KEVT_PRODUCE
, &m
, 1);
198 trace_milkymist_softusb_kevt(m
);
199 softusb_write_dmem(s
, COMLOC_KEVT_BASE
+ 8 * m
, s
->kbd_hid_buffer
, 8);
201 softusb_write_dmem(s
, COMLOC_KEVT_PRODUCE
, &m
, 1);
203 trace_milkymist_softusb_pulse_irq();
204 qemu_irq_pulse(s
->irq
);
207 static void softusb_kbd_hid_datain(HIDState
*hs
)
209 MilkymistSoftUsbState
*s
= container_of(hs
, MilkymistSoftUsbState
, hid_kbd
);
212 /* if device is in reset, do nothing */
213 if (s
->regs
[R_CTRL
] & CTRL_RESET
) {
217 len
= hid_keyboard_poll(hs
, s
->kbd_hid_buffer
, sizeof(s
->kbd_hid_buffer
));
220 softusb_kbd_changed(s
);
224 static void softusb_mouse_hid_datain(HIDState
*hs
)
226 MilkymistSoftUsbState
*s
=
227 container_of(hs
, MilkymistSoftUsbState
, hid_mouse
);
230 /* if device is in reset, do nothing */
231 if (s
->regs
[R_CTRL
] & CTRL_RESET
) {
235 len
= hid_pointer_poll(hs
, s
->mouse_hid_buffer
,
236 sizeof(s
->mouse_hid_buffer
));
239 softusb_mouse_changed(s
);
243 static void milkymist_softusb_reset(DeviceState
*d
)
245 MilkymistSoftUsbState
*s
=
246 container_of(d
, MilkymistSoftUsbState
, busdev
.qdev
);
249 for (i
= 0; i
< R_MAX
; i
++) {
252 memset(s
->kbd_hid_buffer
, 0, sizeof(s
->kbd_hid_buffer
));
253 memset(s
->mouse_hid_buffer
, 0, sizeof(s
->mouse_hid_buffer
));
255 hid_reset(&s
->hid_kbd
);
256 hid_reset(&s
->hid_mouse
);
259 s
->regs
[R_CTRL
] = CTRL_RESET
;
262 static int milkymist_softusb_init(SysBusDevice
*dev
)
264 MilkymistSoftUsbState
*s
= FROM_SYSBUS(typeof(*s
), dev
);
266 sysbus_init_irq(dev
, &s
->irq
);
268 memory_region_init_io(&s
->regs_region
, OBJECT(s
), &softusb_mmio_ops
, s
,
269 "milkymist-softusb", R_MAX
* 4);
270 sysbus_init_mmio(dev
, &s
->regs_region
);
272 /* register pmem and dmem */
273 memory_region_init_ram(&s
->pmem
, OBJECT(s
), "milkymist-softusb.pmem",
275 vmstate_register_ram_global(&s
->pmem
);
276 s
->pmem_ptr
= memory_region_get_ram_ptr(&s
->pmem
);
277 sysbus_init_mmio(dev
, &s
->pmem
);
278 memory_region_init_ram(&s
->dmem
, OBJECT(s
), "milkymist-softusb.dmem",
280 vmstate_register_ram_global(&s
->dmem
);
281 s
->dmem_ptr
= memory_region_get_ram_ptr(&s
->dmem
);
282 sysbus_init_mmio(dev
, &s
->dmem
);
284 hid_init(&s
->hid_kbd
, HID_KEYBOARD
, softusb_kbd_hid_datain
);
285 hid_init(&s
->hid_mouse
, HID_MOUSE
, softusb_mouse_hid_datain
);
290 static const VMStateDescription vmstate_milkymist_softusb
= {
291 .name
= "milkymist-softusb",
293 .minimum_version_id
= 1,
294 .minimum_version_id_old
= 1,
295 .fields
= (VMStateField
[]) {
296 VMSTATE_UINT32_ARRAY(regs
, MilkymistSoftUsbState
, R_MAX
),
297 VMSTATE_HID_KEYBOARD_DEVICE(hid_kbd
, MilkymistSoftUsbState
),
298 VMSTATE_HID_POINTER_DEVICE(hid_mouse
, MilkymistSoftUsbState
),
299 VMSTATE_BUFFER(kbd_hid_buffer
, MilkymistSoftUsbState
),
300 VMSTATE_BUFFER(mouse_hid_buffer
, MilkymistSoftUsbState
),
301 VMSTATE_END_OF_LIST()
305 static Property milkymist_softusb_properties
[] = {
306 DEFINE_PROP_UINT32("pmem_size", MilkymistSoftUsbState
, pmem_size
, 0x00001000),
307 DEFINE_PROP_UINT32("dmem_size", MilkymistSoftUsbState
, dmem_size
, 0x00002000),
308 DEFINE_PROP_END_OF_LIST(),
311 static void milkymist_softusb_class_init(ObjectClass
*klass
, void *data
)
313 DeviceClass
*dc
= DEVICE_CLASS(klass
);
314 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
316 k
->init
= milkymist_softusb_init
;
317 dc
->reset
= milkymist_softusb_reset
;
318 dc
->vmsd
= &vmstate_milkymist_softusb
;
319 dc
->props
= milkymist_softusb_properties
;
322 static const TypeInfo milkymist_softusb_info
= {
323 .name
= "milkymist-softusb",
324 .parent
= TYPE_SYS_BUS_DEVICE
,
325 .instance_size
= sizeof(MilkymistSoftUsbState
),
326 .class_init
= milkymist_softusb_class_init
,
329 static void milkymist_softusb_register_types(void)
331 type_register_static(&milkymist_softusb_info
);
334 type_init(milkymist_softusb_register_types
)