adb: fix read reg 3 byte ordering
[qemu.git] / hw / input / milkymist-softusb.c
1 /*
2 * QEMU model of the Milkymist SoftUSB block.
3 *
4 * Copyright (c) 2010 Michael Walle <michael@walle.cc>
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 *
20 * Specification available at:
21 * not available yet
22 */
23
24 #include "qemu/osdep.h"
25 #include "qapi/error.h"
26 #include "hw/hw.h"
27 #include "hw/sysbus.h"
28 #include "trace.h"
29 #include "ui/console.h"
30 #include "hw/input/hid.h"
31 #include "qemu/error-report.h"
32
33 enum {
34 R_CTRL = 0,
35 R_MAX
36 };
37
38 enum {
39 CTRL_RESET = (1<<0),
40 };
41
42 #define COMLOC_DEBUG_PRODUCE 0x1000
43 #define COMLOC_DEBUG_BASE 0x1001
44 #define COMLOC_MEVT_PRODUCE 0x1101
45 #define COMLOC_MEVT_BASE 0x1102
46 #define COMLOC_KEVT_PRODUCE 0x1142
47 #define COMLOC_KEVT_BASE 0x1143
48
49 #define TYPE_MILKYMIST_SOFTUSB "milkymist-softusb"
50 #define MILKYMIST_SOFTUSB(obj) \
51 OBJECT_CHECK(MilkymistSoftUsbState, (obj), TYPE_MILKYMIST_SOFTUSB)
52
53 struct MilkymistSoftUsbState {
54 SysBusDevice parent_obj;
55
56 HIDState hid_kbd;
57 HIDState hid_mouse;
58
59 MemoryRegion regs_region;
60 MemoryRegion pmem;
61 MemoryRegion dmem;
62 qemu_irq irq;
63
64 void *pmem_ptr;
65 void *dmem_ptr;
66
67 /* device properties */
68 uint32_t pmem_size;
69 uint32_t dmem_size;
70
71 /* device registers */
72 uint32_t regs[R_MAX];
73
74 /* mouse state */
75 uint8_t mouse_hid_buffer[4];
76
77 /* keyboard state */
78 uint8_t kbd_hid_buffer[8];
79 };
80 typedef struct MilkymistSoftUsbState MilkymistSoftUsbState;
81
82 static uint64_t softusb_read(void *opaque, hwaddr addr,
83 unsigned size)
84 {
85 MilkymistSoftUsbState *s = opaque;
86 uint32_t r = 0;
87
88 addr >>= 2;
89 switch (addr) {
90 case R_CTRL:
91 r = s->regs[addr];
92 break;
93
94 default:
95 error_report("milkymist_softusb: read access to unknown register 0x"
96 TARGET_FMT_plx, addr << 2);
97 break;
98 }
99
100 trace_milkymist_softusb_memory_read(addr << 2, r);
101
102 return r;
103 }
104
105 static void
106 softusb_write(void *opaque, hwaddr addr, uint64_t value,
107 unsigned size)
108 {
109 MilkymistSoftUsbState *s = opaque;
110
111 trace_milkymist_softusb_memory_write(addr, value);
112
113 addr >>= 2;
114 switch (addr) {
115 case R_CTRL:
116 s->regs[addr] = value;
117 break;
118
119 default:
120 error_report("milkymist_softusb: write access to unknown register 0x"
121 TARGET_FMT_plx, addr << 2);
122 break;
123 }
124 }
125
126 static const MemoryRegionOps softusb_mmio_ops = {
127 .read = softusb_read,
128 .write = softusb_write,
129 .endianness = DEVICE_NATIVE_ENDIAN,
130 .valid = {
131 .min_access_size = 4,
132 .max_access_size = 4,
133 },
134 };
135
136 static inline void softusb_read_dmem(MilkymistSoftUsbState *s,
137 uint32_t offset, uint8_t *buf, uint32_t len)
138 {
139 if (offset + len >= s->dmem_size) {
140 error_report("milkymist_softusb: read dmem out of bounds "
141 "at offset 0x%x, len %d", offset, len);
142 memset(buf, 0, len);
143 return;
144 }
145
146 memcpy(buf, s->dmem_ptr + offset, len);
147 }
148
149 static inline void softusb_write_dmem(MilkymistSoftUsbState *s,
150 uint32_t offset, uint8_t *buf, uint32_t len)
151 {
152 if (offset + len >= s->dmem_size) {
153 error_report("milkymist_softusb: write dmem out of bounds "
154 "at offset 0x%x, len %d", offset, len);
155 return;
156 }
157
158 memcpy(s->dmem_ptr + offset, buf, len);
159 }
160
161 static void softusb_mouse_changed(MilkymistSoftUsbState *s)
162 {
163 uint8_t m;
164
165 softusb_read_dmem(s, COMLOC_MEVT_PRODUCE, &m, 1);
166 trace_milkymist_softusb_mevt(m);
167 softusb_write_dmem(s, COMLOC_MEVT_BASE + 4 * m, s->mouse_hid_buffer, 4);
168 m = (m + 1) & 0xf;
169 softusb_write_dmem(s, COMLOC_MEVT_PRODUCE, &m, 1);
170
171 trace_milkymist_softusb_pulse_irq();
172 qemu_irq_pulse(s->irq);
173 }
174
175 static void softusb_kbd_changed(MilkymistSoftUsbState *s)
176 {
177 uint8_t m;
178
179 softusb_read_dmem(s, COMLOC_KEVT_PRODUCE, &m, 1);
180 trace_milkymist_softusb_kevt(m);
181 softusb_write_dmem(s, COMLOC_KEVT_BASE + 8 * m, s->kbd_hid_buffer, 8);
182 m = (m + 1) & 0x7;
183 softusb_write_dmem(s, COMLOC_KEVT_PRODUCE, &m, 1);
184
185 trace_milkymist_softusb_pulse_irq();
186 qemu_irq_pulse(s->irq);
187 }
188
189 static void softusb_kbd_hid_datain(HIDState *hs)
190 {
191 MilkymistSoftUsbState *s = container_of(hs, MilkymistSoftUsbState, hid_kbd);
192 int len;
193
194 /* if device is in reset, do nothing */
195 if (s->regs[R_CTRL] & CTRL_RESET) {
196 return;
197 }
198
199 while (hid_has_events(hs)) {
200 len = hid_keyboard_poll(hs, s->kbd_hid_buffer,
201 sizeof(s->kbd_hid_buffer));
202
203 if (len == 8) {
204 softusb_kbd_changed(s);
205 }
206 }
207 }
208
209 static void softusb_mouse_hid_datain(HIDState *hs)
210 {
211 MilkymistSoftUsbState *s =
212 container_of(hs, MilkymistSoftUsbState, hid_mouse);
213 int len;
214
215 /* if device is in reset, do nothing */
216 if (s->regs[R_CTRL] & CTRL_RESET) {
217 return;
218 }
219
220 while (hid_has_events(hs)) {
221 len = hid_pointer_poll(hs, s->mouse_hid_buffer,
222 sizeof(s->mouse_hid_buffer));
223
224 if (len == 4) {
225 softusb_mouse_changed(s);
226 }
227 }
228 }
229
230 static void milkymist_softusb_reset(DeviceState *d)
231 {
232 MilkymistSoftUsbState *s = MILKYMIST_SOFTUSB(d);
233 int i;
234
235 for (i = 0; i < R_MAX; i++) {
236 s->regs[i] = 0;
237 }
238 memset(s->kbd_hid_buffer, 0, sizeof(s->kbd_hid_buffer));
239 memset(s->mouse_hid_buffer, 0, sizeof(s->mouse_hid_buffer));
240
241 hid_reset(&s->hid_kbd);
242 hid_reset(&s->hid_mouse);
243
244 /* defaults */
245 s->regs[R_CTRL] = CTRL_RESET;
246 }
247
248 static int milkymist_softusb_init(SysBusDevice *dev)
249 {
250 MilkymistSoftUsbState *s = MILKYMIST_SOFTUSB(dev);
251
252 sysbus_init_irq(dev, &s->irq);
253
254 memory_region_init_io(&s->regs_region, OBJECT(s), &softusb_mmio_ops, s,
255 "milkymist-softusb", R_MAX * 4);
256 sysbus_init_mmio(dev, &s->regs_region);
257
258 /* register pmem and dmem */
259 memory_region_init_ram_nomigrate(&s->pmem, OBJECT(s), "milkymist-softusb.pmem",
260 s->pmem_size, &error_fatal);
261 vmstate_register_ram_global(&s->pmem);
262 s->pmem_ptr = memory_region_get_ram_ptr(&s->pmem);
263 sysbus_init_mmio(dev, &s->pmem);
264 memory_region_init_ram_nomigrate(&s->dmem, OBJECT(s), "milkymist-softusb.dmem",
265 s->dmem_size, &error_fatal);
266 vmstate_register_ram_global(&s->dmem);
267 s->dmem_ptr = memory_region_get_ram_ptr(&s->dmem);
268 sysbus_init_mmio(dev, &s->dmem);
269
270 hid_init(&s->hid_kbd, HID_KEYBOARD, softusb_kbd_hid_datain);
271 hid_init(&s->hid_mouse, HID_MOUSE, softusb_mouse_hid_datain);
272
273 return 0;
274 }
275
276 static const VMStateDescription vmstate_milkymist_softusb = {
277 .name = "milkymist-softusb",
278 .version_id = 1,
279 .minimum_version_id = 1,
280 .fields = (VMStateField[]) {
281 VMSTATE_UINT32_ARRAY(regs, MilkymistSoftUsbState, R_MAX),
282 VMSTATE_HID_KEYBOARD_DEVICE(hid_kbd, MilkymistSoftUsbState),
283 VMSTATE_HID_POINTER_DEVICE(hid_mouse, MilkymistSoftUsbState),
284 VMSTATE_BUFFER(kbd_hid_buffer, MilkymistSoftUsbState),
285 VMSTATE_BUFFER(mouse_hid_buffer, MilkymistSoftUsbState),
286 VMSTATE_END_OF_LIST()
287 }
288 };
289
290 static Property milkymist_softusb_properties[] = {
291 DEFINE_PROP_UINT32("pmem_size", MilkymistSoftUsbState, pmem_size, 0x00001000),
292 DEFINE_PROP_UINT32("dmem_size", MilkymistSoftUsbState, dmem_size, 0x00002000),
293 DEFINE_PROP_END_OF_LIST(),
294 };
295
296 static void milkymist_softusb_class_init(ObjectClass *klass, void *data)
297 {
298 DeviceClass *dc = DEVICE_CLASS(klass);
299 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
300
301 k->init = milkymist_softusb_init;
302 dc->reset = milkymist_softusb_reset;
303 dc->vmsd = &vmstate_milkymist_softusb;
304 dc->props = milkymist_softusb_properties;
305 }
306
307 static const TypeInfo milkymist_softusb_info = {
308 .name = TYPE_MILKYMIST_SOFTUSB,
309 .parent = TYPE_SYS_BUS_DEVICE,
310 .instance_size = sizeof(MilkymistSoftUsbState),
311 .class_init = milkymist_softusb_class_init,
312 };
313
314 static void milkymist_softusb_register_types(void)
315 {
316 type_register_static(&milkymist_softusb_info);
317 }
318
319 type_init(milkymist_softusb_register_types)