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