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:
29 #include "qemu-error.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
{
54 /* device properties */
60 /* device registers */
64 uint8_t mouse_hid_buffer
[4];
67 uint8_t kbd_hid_buffer
[8];
69 typedef struct MilkymistSoftUsbState MilkymistSoftUsbState
;
71 static uint32_t softusb_read(void *opaque
, target_phys_addr_t addr
)
73 MilkymistSoftUsbState
*s
= opaque
;
83 error_report("milkymist_softusb: read access to unknown register 0x"
84 TARGET_FMT_plx
, addr
<< 2);
88 trace_milkymist_softusb_memory_read(addr
<< 2, r
);
94 softusb_write(void *opaque
, target_phys_addr_t addr
, uint32_t value
)
96 MilkymistSoftUsbState
*s
= opaque
;
98 trace_milkymist_softusb_memory_write(addr
, value
);
103 s
->regs
[addr
] = value
;
107 error_report("milkymist_softusb: write access to unknown register 0x"
108 TARGET_FMT_plx
, addr
<< 2);
113 static CPUReadMemoryFunc
* const softusb_read_fn
[] = {
119 static CPUWriteMemoryFunc
* const softusb_write_fn
[] = {
125 static inline void softusb_read_dmem(MilkymistSoftUsbState
*s
,
126 uint32_t offset
, uint8_t *buf
, uint32_t len
)
128 if (offset
+ len
>= s
->dmem_size
) {
129 error_report("milkymist_softusb: read dmem out of bounds "
130 "at offset 0x%x, len %d", offset
, len
);
134 cpu_physical_memory_read(s
->dmem_base
+ offset
, buf
, len
);
137 static inline void softusb_write_dmem(MilkymistSoftUsbState
*s
,
138 uint32_t offset
, uint8_t *buf
, uint32_t len
)
140 if (offset
+ len
>= s
->dmem_size
) {
141 error_report("milkymist_softusb: write dmem out of bounds "
142 "at offset 0x%x, len %d", offset
, len
);
146 cpu_physical_memory_write(s
->dmem_base
+ offset
, buf
, len
);
149 static inline void softusb_read_pmem(MilkymistSoftUsbState
*s
,
150 uint32_t offset
, uint8_t *buf
, uint32_t len
)
152 if (offset
+ len
>= s
->pmem_size
) {
153 error_report("milkymist_softusb: read pmem out of bounds "
154 "at offset 0x%x, len %d", offset
, len
);
158 cpu_physical_memory_read(s
->pmem_base
+ offset
, buf
, len
);
161 static inline void softusb_write_pmem(MilkymistSoftUsbState
*s
,
162 uint32_t offset
, uint8_t *buf
, uint32_t len
)
164 if (offset
+ len
>= s
->pmem_size
) {
165 error_report("milkymist_softusb: write pmem out of bounds "
166 "at offset 0x%x, len %d", offset
, len
);
170 cpu_physical_memory_write(s
->pmem_base
+ offset
, buf
, len
);
173 static void softusb_mouse_changed(MilkymistSoftUsbState
*s
)
177 softusb_read_dmem(s
, COMLOC_MEVT_PRODUCE
, &m
, 1);
178 trace_milkymist_softusb_mevt(m
);
179 softusb_write_dmem(s
, COMLOC_MEVT_BASE
+ 4 * m
, s
->mouse_hid_buffer
, 4);
181 softusb_write_dmem(s
, COMLOC_MEVT_PRODUCE
, &m
, 1);
183 trace_milkymist_softusb_pulse_irq();
184 qemu_irq_pulse(s
->irq
);
187 static void softusb_kbd_changed(MilkymistSoftUsbState
*s
)
191 softusb_read_dmem(s
, COMLOC_KEVT_PRODUCE
, &m
, 1);
192 trace_milkymist_softusb_kevt(m
);
193 softusb_write_dmem(s
, COMLOC_KEVT_BASE
+ 8 * m
, s
->kbd_hid_buffer
, 8);
195 softusb_write_dmem(s
, COMLOC_KEVT_PRODUCE
, &m
, 1);
197 trace_milkymist_softusb_pulse_irq();
198 qemu_irq_pulse(s
->irq
);
201 static void softusb_kbd_hid_datain(HIDState
*hs
)
203 MilkymistSoftUsbState
*s
= container_of(hs
, MilkymistSoftUsbState
, hid_kbd
);
206 /* if device is in reset, do nothing */
207 if (s
->regs
[R_CTRL
] & CTRL_RESET
) {
211 len
= hid_keyboard_poll(hs
, s
->kbd_hid_buffer
, sizeof(s
->kbd_hid_buffer
));
214 softusb_kbd_changed(s
);
218 static void softusb_mouse_hid_datain(HIDState
*hs
)
220 MilkymistSoftUsbState
*s
=
221 container_of(hs
, MilkymistSoftUsbState
, hid_mouse
);
224 /* if device is in reset, do nothing */
225 if (s
->regs
[R_CTRL
] & CTRL_RESET
) {
229 len
= hid_pointer_poll(hs
, s
->mouse_hid_buffer
,
230 sizeof(s
->mouse_hid_buffer
));
233 softusb_mouse_changed(s
);
237 static void milkymist_softusb_reset(DeviceState
*d
)
239 MilkymistSoftUsbState
*s
=
240 container_of(d
, MilkymistSoftUsbState
, busdev
.qdev
);
243 for (i
= 0; i
< R_MAX
; i
++) {
246 memset(s
->kbd_hid_buffer
, 0, sizeof(s
->kbd_hid_buffer
));
247 memset(s
->mouse_hid_buffer
, 0, sizeof(s
->mouse_hid_buffer
));
249 hid_reset(&s
->hid_kbd
);
250 hid_reset(&s
->hid_mouse
);
253 s
->regs
[R_CTRL
] = CTRL_RESET
;
256 static int milkymist_softusb_init(SysBusDevice
*dev
)
258 MilkymistSoftUsbState
*s
= FROM_SYSBUS(typeof(*s
), dev
);
263 sysbus_init_irq(dev
, &s
->irq
);
265 softusb_regs
= cpu_register_io_memory(softusb_read_fn
, softusb_write_fn
, s
,
266 DEVICE_NATIVE_ENDIAN
);
267 sysbus_init_mmio(dev
, R_MAX
* 4, softusb_regs
);
269 /* register pmem and dmem */
270 pmem_ram
= qemu_ram_alloc(NULL
, "milkymist_softusb.pmem", s
->pmem_size
);
271 cpu_register_physical_memory(s
->pmem_base
, s
->pmem_size
,
272 pmem_ram
| IO_MEM_RAM
);
273 dmem_ram
= qemu_ram_alloc(NULL
, "milkymist_softusb.dmem", s
->dmem_size
);
274 cpu_register_physical_memory(s
->dmem_base
, s
->dmem_size
,
275 dmem_ram
| IO_MEM_RAM
);
277 hid_init(&s
->hid_kbd
, HID_KEYBOARD
, softusb_kbd_hid_datain
);
278 hid_init(&s
->hid_mouse
, HID_MOUSE
, softusb_mouse_hid_datain
);
283 static const VMStateDescription vmstate_milkymist_softusb
= {
284 .name
= "milkymist-softusb",
286 .minimum_version_id
= 1,
287 .minimum_version_id_old
= 1,
288 .fields
= (VMStateField
[]) {
289 VMSTATE_UINT32_ARRAY(regs
, MilkymistSoftUsbState
, R_MAX
),
290 VMSTATE_HID_KEYBOARD_DEVICE(hid_kbd
, MilkymistSoftUsbState
),
291 VMSTATE_HID_POINTER_DEVICE(hid_mouse
, MilkymistSoftUsbState
),
292 VMSTATE_BUFFER(kbd_hid_buffer
, MilkymistSoftUsbState
),
293 VMSTATE_BUFFER(mouse_hid_buffer
, MilkymistSoftUsbState
),
294 VMSTATE_END_OF_LIST()
298 static SysBusDeviceInfo milkymist_softusb_info
= {
299 .init
= milkymist_softusb_init
,
300 .qdev
.name
= "milkymist-softusb",
301 .qdev
.size
= sizeof(MilkymistSoftUsbState
),
302 .qdev
.vmsd
= &vmstate_milkymist_softusb
,
303 .qdev
.reset
= milkymist_softusb_reset
,
304 .qdev
.props
= (Property
[]) {
306 "pmem_base", MilkymistSoftUsbState
, pmem_base
, 0xa0000000
309 "pmem_size", MilkymistSoftUsbState
, pmem_size
, 0x00001000
312 "dmem_base", MilkymistSoftUsbState
, dmem_base
, 0xa0020000
315 "dmem_size", MilkymistSoftUsbState
, dmem_size
, 0x00002000
317 DEFINE_PROP_END_OF_LIST(),
321 static void milkymist_softusb_register(void)
323 sysbus_register_withprop(&milkymist_softusb_info
);
326 device_init(milkymist_softusb_register
)