blob: 8a02d35ca15af83e3b0b84be2d7461e4bf1dee2a [file] [log] [blame]
Michael Walle87a381e2011-03-07 23:32:37 +01001/*
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
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010024#include "hw/hw.h"
25#include "hw/sysbus.h"
Michael Walle87a381e2011-03-07 23:32:37 +010026#include "trace.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010027#include "ui/console.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010028#include "hw/input/hid.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010029#include "qemu/error-report.h"
Michael Walle87a381e2011-03-07 23:32:37 +010030
31enum {
32 R_CTRL = 0,
33 R_MAX
34};
35
36enum {
37 CTRL_RESET = (1<<0),
38};
39
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
46
Andreas Färber2a052f62013-07-26 18:37:02 +020047#define TYPE_MILKYMIST_SOFTUSB "milkymist-softusb"
48#define MILKYMIST_SOFTUSB(obj) \
49 OBJECT_CHECK(MilkymistSoftUsbState, (obj), TYPE_MILKYMIST_SOFTUSB)
50
Michael Walle87a381e2011-03-07 23:32:37 +010051struct MilkymistSoftUsbState {
Andreas Färber2a052f62013-07-26 18:37:02 +020052 SysBusDevice parent_obj;
53
Michael Walle4c15ba92011-08-09 23:54:55 +020054 HIDState hid_kbd;
55 HIDState hid_mouse;
Michael Walle87a381e2011-03-07 23:32:37 +010056
Avi Kivity5105ed32011-08-08 21:40:35 +030057 MemoryRegion regs_region;
58 MemoryRegion pmem;
59 MemoryRegion dmem;
Michael Walle87a381e2011-03-07 23:32:37 +010060 qemu_irq irq;
61
Peter Maydellc34e1202013-03-15 14:34:22 +000062 void *pmem_ptr;
63 void *dmem_ptr;
64
Michael Walle87a381e2011-03-07 23:32:37 +010065 /* device properties */
Michael Walle87a381e2011-03-07 23:32:37 +010066 uint32_t pmem_size;
Michael Walle87a381e2011-03-07 23:32:37 +010067 uint32_t dmem_size;
68
69 /* device registers */
70 uint32_t regs[R_MAX];
71
72 /* mouse state */
Michael Walle4c15ba92011-08-09 23:54:55 +020073 uint8_t mouse_hid_buffer[4];
Michael Walle87a381e2011-03-07 23:32:37 +010074
75 /* keyboard state */
Michael Walle4c15ba92011-08-09 23:54:55 +020076 uint8_t kbd_hid_buffer[8];
Michael Walle87a381e2011-03-07 23:32:37 +010077};
78typedef struct MilkymistSoftUsbState MilkymistSoftUsbState;
79
Avi Kivitya8170e52012-10-23 12:30:10 +020080static uint64_t softusb_read(void *opaque, hwaddr addr,
Avi Kivity5105ed32011-08-08 21:40:35 +030081 unsigned size)
Michael Walle87a381e2011-03-07 23:32:37 +010082{
83 MilkymistSoftUsbState *s = opaque;
84 uint32_t r = 0;
85
86 addr >>= 2;
87 switch (addr) {
88 case R_CTRL:
89 r = s->regs[addr];
90 break;
91
92 default:
93 error_report("milkymist_softusb: read access to unknown register 0x"
94 TARGET_FMT_plx, addr << 2);
95 break;
96 }
97
98 trace_milkymist_softusb_memory_read(addr << 2, r);
99
100 return r;
101}
102
103static void
Avi Kivitya8170e52012-10-23 12:30:10 +0200104softusb_write(void *opaque, hwaddr addr, uint64_t value,
Avi Kivity5105ed32011-08-08 21:40:35 +0300105 unsigned size)
Michael Walle87a381e2011-03-07 23:32:37 +0100106{
107 MilkymistSoftUsbState *s = opaque;
108
109 trace_milkymist_softusb_memory_write(addr, value);
110
111 addr >>= 2;
112 switch (addr) {
113 case R_CTRL:
114 s->regs[addr] = value;
115 break;
116
117 default:
118 error_report("milkymist_softusb: write access to unknown register 0x"
119 TARGET_FMT_plx, addr << 2);
120 break;
121 }
122}
123
Avi Kivity5105ed32011-08-08 21:40:35 +0300124static const MemoryRegionOps softusb_mmio_ops = {
125 .read = softusb_read,
126 .write = softusb_write,
127 .endianness = DEVICE_NATIVE_ENDIAN,
128 .valid = {
129 .min_access_size = 4,
130 .max_access_size = 4,
131 },
Michael Walle87a381e2011-03-07 23:32:37 +0100132};
133
134static inline void softusb_read_dmem(MilkymistSoftUsbState *s,
135 uint32_t offset, uint8_t *buf, uint32_t len)
136{
137 if (offset + len >= s->dmem_size) {
138 error_report("milkymist_softusb: read dmem out of bounds "
Markus Armbruster6daf1942011-06-22 14:03:54 +0200139 "at offset 0x%x, len %d", offset, len);
Peter Maydellc31bc982013-03-28 18:43:04 +0000140 memset(buf, 0, len);
Michael Walle87a381e2011-03-07 23:32:37 +0100141 return;
142 }
143
Peter Maydellc34e1202013-03-15 14:34:22 +0000144 memcpy(buf, s->dmem_ptr + offset, len);
Michael Walle87a381e2011-03-07 23:32:37 +0100145}
146
147static inline void softusb_write_dmem(MilkymistSoftUsbState *s,
148 uint32_t offset, uint8_t *buf, uint32_t len)
149{
150 if (offset + len >= s->dmem_size) {
151 error_report("milkymist_softusb: write dmem out of bounds "
Markus Armbruster6daf1942011-06-22 14:03:54 +0200152 "at offset 0x%x, len %d", offset, len);
Michael Walle87a381e2011-03-07 23:32:37 +0100153 return;
154 }
155
Peter Maydellc34e1202013-03-15 14:34:22 +0000156 memcpy(s->dmem_ptr + offset, buf, len);
Michael Walle87a381e2011-03-07 23:32:37 +0100157}
158
Michael Walle87a381e2011-03-07 23:32:37 +0100159static void softusb_mouse_changed(MilkymistSoftUsbState *s)
160{
161 uint8_t m;
Michael Walle87a381e2011-03-07 23:32:37 +0100162
163 softusb_read_dmem(s, COMLOC_MEVT_PRODUCE, &m, 1);
164 trace_milkymist_softusb_mevt(m);
Michael Walle4c15ba92011-08-09 23:54:55 +0200165 softusb_write_dmem(s, COMLOC_MEVT_BASE + 4 * m, s->mouse_hid_buffer, 4);
Michael Walle87a381e2011-03-07 23:32:37 +0100166 m = (m + 1) & 0xf;
167 softusb_write_dmem(s, COMLOC_MEVT_PRODUCE, &m, 1);
168
169 trace_milkymist_softusb_pulse_irq();
170 qemu_irq_pulse(s->irq);
171}
172
173static void softusb_kbd_changed(MilkymistSoftUsbState *s)
174{
175 uint8_t m;
176
177 softusb_read_dmem(s, COMLOC_KEVT_PRODUCE, &m, 1);
178 trace_milkymist_softusb_kevt(m);
Michael Walle4c15ba92011-08-09 23:54:55 +0200179 softusb_write_dmem(s, COMLOC_KEVT_BASE + 8 * m, s->kbd_hid_buffer, 8);
Michael Walle87a381e2011-03-07 23:32:37 +0100180 m = (m + 1) & 0x7;
181 softusb_write_dmem(s, COMLOC_KEVT_PRODUCE, &m, 1);
182
183 trace_milkymist_softusb_pulse_irq();
184 qemu_irq_pulse(s->irq);
185}
186
Michael Walle4c15ba92011-08-09 23:54:55 +0200187static void softusb_kbd_hid_datain(HIDState *hs)
Michael Walle87a381e2011-03-07 23:32:37 +0100188{
Michael Walle4c15ba92011-08-09 23:54:55 +0200189 MilkymistSoftUsbState *s = container_of(hs, MilkymistSoftUsbState, hid_kbd);
190 int len;
Michael Walle87a381e2011-03-07 23:32:37 +0100191
192 /* if device is in reset, do nothing */
193 if (s->regs[R_CTRL] & CTRL_RESET) {
194 return;
195 }
196
Michael Walle857ccca2014-10-04 20:00:07 +0200197 while (hid_has_events(hs)) {
198 len = hid_keyboard_poll(hs, s->kbd_hid_buffer,
199 sizeof(s->kbd_hid_buffer));
Michael Walle87a381e2011-03-07 23:32:37 +0100200
Michael Walle857ccca2014-10-04 20:00:07 +0200201 if (len == 8) {
202 softusb_kbd_changed(s);
203 }
Michael Walle4c15ba92011-08-09 23:54:55 +0200204 }
Michael Walle87a381e2011-03-07 23:32:37 +0100205}
206
Michael Walle4c15ba92011-08-09 23:54:55 +0200207static void softusb_mouse_hid_datain(HIDState *hs)
Michael Walle87a381e2011-03-07 23:32:37 +0100208{
Michael Walle4c15ba92011-08-09 23:54:55 +0200209 MilkymistSoftUsbState *s =
210 container_of(hs, MilkymistSoftUsbState, hid_mouse);
211 int len;
Michael Walle87a381e2011-03-07 23:32:37 +0100212
Michael Walle4c15ba92011-08-09 23:54:55 +0200213 /* if device is in reset, do nothing */
214 if (s->regs[R_CTRL] & CTRL_RESET) {
215 return;
216 }
Michael Walle87a381e2011-03-07 23:32:37 +0100217
Michael Walle857ccca2014-10-04 20:00:07 +0200218 while (hid_has_events(hs)) {
219 len = hid_pointer_poll(hs, s->mouse_hid_buffer,
220 sizeof(s->mouse_hid_buffer));
Michael Walle87a381e2011-03-07 23:32:37 +0100221
Michael Walle857ccca2014-10-04 20:00:07 +0200222 if (len == 4) {
223 softusb_mouse_changed(s);
224 }
Michael Walle4c15ba92011-08-09 23:54:55 +0200225 }
Michael Walle87a381e2011-03-07 23:32:37 +0100226}
227
Michael Walle87a381e2011-03-07 23:32:37 +0100228static void milkymist_softusb_reset(DeviceState *d)
229{
Andreas Färber2a052f62013-07-26 18:37:02 +0200230 MilkymistSoftUsbState *s = MILKYMIST_SOFTUSB(d);
Michael Walle87a381e2011-03-07 23:32:37 +0100231 int i;
232
233 for (i = 0; i < R_MAX; i++) {
234 s->regs[i] = 0;
235 }
Michael Walle4c15ba92011-08-09 23:54:55 +0200236 memset(s->kbd_hid_buffer, 0, sizeof(s->kbd_hid_buffer));
237 memset(s->mouse_hid_buffer, 0, sizeof(s->mouse_hid_buffer));
238
239 hid_reset(&s->hid_kbd);
240 hid_reset(&s->hid_mouse);
Michael Walle87a381e2011-03-07 23:32:37 +0100241
242 /* defaults */
243 s->regs[R_CTRL] = CTRL_RESET;
244}
245
246static int milkymist_softusb_init(SysBusDevice *dev)
247{
Andreas Färber2a052f62013-07-26 18:37:02 +0200248 MilkymistSoftUsbState *s = MILKYMIST_SOFTUSB(dev);
Michael Walle87a381e2011-03-07 23:32:37 +0100249
250 sysbus_init_irq(dev, &s->irq);
251
Paolo Bonzini1437c942013-06-06 21:25:08 -0400252 memory_region_init_io(&s->regs_region, OBJECT(s), &softusb_mmio_ops, s,
Avi Kivity5105ed32011-08-08 21:40:35 +0300253 "milkymist-softusb", R_MAX * 4);
Avi Kivity750ecd42011-11-27 11:38:10 +0200254 sysbus_init_mmio(dev, &s->regs_region);
Michael Walle87a381e2011-03-07 23:32:37 +0100255
256 /* register pmem and dmem */
Paolo Bonzini1437c942013-06-06 21:25:08 -0400257 memory_region_init_ram(&s->pmem, OBJECT(s), "milkymist-softusb.pmem",
Markus Armbrusterf8ed85a2015-09-11 16:51:43 +0200258 s->pmem_size, &error_fatal);
Avi Kivityc5705a72011-12-20 15:59:12 +0200259 vmstate_register_ram_global(&s->pmem);
Peter Maydellc34e1202013-03-15 14:34:22 +0000260 s->pmem_ptr = memory_region_get_ram_ptr(&s->pmem);
261 sysbus_init_mmio(dev, &s->pmem);
Paolo Bonzini1437c942013-06-06 21:25:08 -0400262 memory_region_init_ram(&s->dmem, OBJECT(s), "milkymist-softusb.dmem",
Markus Armbrusterf8ed85a2015-09-11 16:51:43 +0200263 s->dmem_size, &error_fatal);
Avi Kivityc5705a72011-12-20 15:59:12 +0200264 vmstate_register_ram_global(&s->dmem);
Peter Maydellc34e1202013-03-15 14:34:22 +0000265 s->dmem_ptr = memory_region_get_ram_ptr(&s->dmem);
266 sysbus_init_mmio(dev, &s->dmem);
Michael Walle87a381e2011-03-07 23:32:37 +0100267
Michael Walle4c15ba92011-08-09 23:54:55 +0200268 hid_init(&s->hid_kbd, HID_KEYBOARD, softusb_kbd_hid_datain);
269 hid_init(&s->hid_mouse, HID_MOUSE, softusb_mouse_hid_datain);
Michael Walle87a381e2011-03-07 23:32:37 +0100270
271 return 0;
272}
273
274static const VMStateDescription vmstate_milkymist_softusb = {
275 .name = "milkymist-softusb",
276 .version_id = 1,
277 .minimum_version_id = 1,
Juan Quintela35d08452014-04-16 16:01:33 +0200278 .fields = (VMStateField[]) {
Michael Walle87a381e2011-03-07 23:32:37 +0100279 VMSTATE_UINT32_ARRAY(regs, MilkymistSoftUsbState, R_MAX),
Michael Walle4c15ba92011-08-09 23:54:55 +0200280 VMSTATE_HID_KEYBOARD_DEVICE(hid_kbd, MilkymistSoftUsbState),
281 VMSTATE_HID_POINTER_DEVICE(hid_mouse, MilkymistSoftUsbState),
282 VMSTATE_BUFFER(kbd_hid_buffer, MilkymistSoftUsbState),
283 VMSTATE_BUFFER(mouse_hid_buffer, MilkymistSoftUsbState),
Michael Walle87a381e2011-03-07 23:32:37 +0100284 VMSTATE_END_OF_LIST()
285 }
286};
287
Anthony Liguori999e12b2012-01-24 13:12:29 -0600288static Property milkymist_softusb_properties[] = {
Anthony Liguori999e12b2012-01-24 13:12:29 -0600289 DEFINE_PROP_UINT32("pmem_size", MilkymistSoftUsbState, pmem_size, 0x00001000),
Anthony Liguori999e12b2012-01-24 13:12:29 -0600290 DEFINE_PROP_UINT32("dmem_size", MilkymistSoftUsbState, dmem_size, 0x00002000),
291 DEFINE_PROP_END_OF_LIST(),
292};
293
294static void milkymist_softusb_class_init(ObjectClass *klass, void *data)
295{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600296 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori999e12b2012-01-24 13:12:29 -0600297 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
298
299 k->init = milkymist_softusb_init;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600300 dc->reset = milkymist_softusb_reset;
301 dc->vmsd = &vmstate_milkymist_softusb;
302 dc->props = milkymist_softusb_properties;
Anthony Liguori999e12b2012-01-24 13:12:29 -0600303}
304
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100305static const TypeInfo milkymist_softusb_info = {
Andreas Färber2a052f62013-07-26 18:37:02 +0200306 .name = TYPE_MILKYMIST_SOFTUSB,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600307 .parent = TYPE_SYS_BUS_DEVICE,
308 .instance_size = sizeof(MilkymistSoftUsbState),
309 .class_init = milkymist_softusb_class_init,
Michael Walle87a381e2011-03-07 23:32:37 +0100310};
311
Andreas Färber83f7d432012-02-09 15:20:55 +0100312static void milkymist_softusb_register_types(void)
Michael Walle87a381e2011-03-07 23:32:37 +0100313{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600314 type_register_static(&milkymist_softusb_info);
Michael Walle87a381e2011-03-07 23:32:37 +0100315}
316
Andreas Färber83f7d432012-02-09 15:20:55 +0100317type_init(milkymist_softusb_register_types)