xhci: remove leftover debug printf
[qemu/ar7.git] / hw / milkymist-softusb.c
blob90a0ae57696dca218ac5165278c975c316267e5e
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/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 struct MilkymistSoftUsbState {
48 SysBusDevice busdev;
49 HIDState hid_kbd;
50 HIDState hid_mouse;
52 MemoryRegion regs_region;
53 MemoryRegion pmem;
54 MemoryRegion dmem;
55 qemu_irq irq;
57 void *pmem_ptr;
58 void *dmem_ptr;
60 /* device properties */
61 uint32_t pmem_size;
62 uint32_t dmem_size;
64 /* device registers */
65 uint32_t regs[R_MAX];
67 /* mouse state */
68 uint8_t mouse_hid_buffer[4];
70 /* keyboard state */
71 uint8_t kbd_hid_buffer[8];
73 typedef struct MilkymistSoftUsbState MilkymistSoftUsbState;
75 static uint64_t softusb_read(void *opaque, hwaddr addr,
76 unsigned size)
78 MilkymistSoftUsbState *s = opaque;
79 uint32_t r = 0;
81 addr >>= 2;
82 switch (addr) {
83 case R_CTRL:
84 r = s->regs[addr];
85 break;
87 default:
88 error_report("milkymist_softusb: read access to unknown register 0x"
89 TARGET_FMT_plx, addr << 2);
90 break;
93 trace_milkymist_softusb_memory_read(addr << 2, r);
95 return r;
98 static void
99 softusb_write(void *opaque, hwaddr addr, uint64_t value,
100 unsigned size)
102 MilkymistSoftUsbState *s = opaque;
104 trace_milkymist_softusb_memory_write(addr, value);
106 addr >>= 2;
107 switch (addr) {
108 case R_CTRL:
109 s->regs[addr] = value;
110 break;
112 default:
113 error_report("milkymist_softusb: write access to unknown register 0x"
114 TARGET_FMT_plx, addr << 2);
115 break;
119 static const MemoryRegionOps softusb_mmio_ops = {
120 .read = softusb_read,
121 .write = softusb_write,
122 .endianness = DEVICE_NATIVE_ENDIAN,
123 .valid = {
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);
135 memset(buf, 0, len);
136 return;
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);
148 return;
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);
160 memset(buf, 0, len);
161 return;
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);
173 return;
176 memcpy(s->pmem_ptr + offset, buf, len);
179 static void softusb_mouse_changed(MilkymistSoftUsbState *s)
181 uint8_t m;
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);
186 m = (m + 1) & 0xf;
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)
195 uint8_t m;
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);
200 m = (m + 1) & 0x7;
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);
210 int len;
212 /* if device is in reset, do nothing */
213 if (s->regs[R_CTRL] & CTRL_RESET) {
214 return;
217 len = hid_keyboard_poll(hs, s->kbd_hid_buffer, sizeof(s->kbd_hid_buffer));
219 if (len == 8) {
220 softusb_kbd_changed(s);
224 static void softusb_mouse_hid_datain(HIDState *hs)
226 MilkymistSoftUsbState *s =
227 container_of(hs, MilkymistSoftUsbState, hid_mouse);
228 int len;
230 /* if device is in reset, do nothing */
231 if (s->regs[R_CTRL] & CTRL_RESET) {
232 return;
235 len = hid_pointer_poll(hs, s->mouse_hid_buffer,
236 sizeof(s->mouse_hid_buffer));
238 if (len == 4) {
239 softusb_mouse_changed(s);
243 static void milkymist_softusb_reset(DeviceState *d)
245 MilkymistSoftUsbState *s =
246 container_of(d, MilkymistSoftUsbState, busdev.qdev);
247 int i;
249 for (i = 0; i < R_MAX; i++) {
250 s->regs[i] = 0;
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);
258 /* defaults */
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, &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, "milkymist-softusb.pmem",
274 s->pmem_size);
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, "milkymist-softusb.dmem",
279 s->dmem_size);
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);
287 return 0;
290 static const VMStateDescription vmstate_milkymist_softusb = {
291 .name = "milkymist-softusb",
292 .version_id = 1,
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)