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