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