Statistics
| Branch: | Revision:

root / hw / input / milkymist-softusb.c @ 3bd88451

History | View | Annotate | Download (8.9 kB)

1 87a381ec Michael Walle
/*
2 87a381ec Michael Walle
 *  QEMU model of the Milkymist SoftUSB block.
3 87a381ec Michael Walle
 *
4 87a381ec Michael Walle
 *  Copyright (c) 2010 Michael Walle <michael@walle.cc>
5 87a381ec Michael Walle
 *
6 87a381ec Michael Walle
 * This library is free software; you can redistribute it and/or
7 87a381ec Michael Walle
 * modify it under the terms of the GNU Lesser General Public
8 87a381ec Michael Walle
 * License as published by the Free Software Foundation; either
9 87a381ec Michael Walle
 * version 2 of the License, or (at your option) any later version.
10 87a381ec Michael Walle
 *
11 87a381ec Michael Walle
 * This library is distributed in the hope that it will be useful,
12 87a381ec Michael Walle
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 87a381ec Michael Walle
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 87a381ec Michael Walle
 * Lesser General Public License for more details.
15 87a381ec Michael Walle
 *
16 87a381ec Michael Walle
 * You should have received a copy of the GNU Lesser General Public
17 87a381ec Michael Walle
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 87a381ec Michael Walle
 *
19 87a381ec Michael Walle
 *
20 87a381ec Michael Walle
 * Specification available at:
21 87a381ec Michael Walle
 *   not available yet
22 87a381ec Michael Walle
 */
23 87a381ec Michael Walle
24 83c9f4ca Paolo Bonzini
#include "hw/hw.h"
25 83c9f4ca Paolo Bonzini
#include "hw/sysbus.h"
26 87a381ec Michael Walle
#include "trace.h"
27 28ecbaee Paolo Bonzini
#include "ui/console.h"
28 0d09e41a Paolo Bonzini
#include "hw/input/hid.h"
29 1de7afc9 Paolo Bonzini
#include "qemu/error-report.h"
30 87a381ec Michael Walle
31 87a381ec Michael Walle
enum {
32 87a381ec Michael Walle
    R_CTRL = 0,
33 87a381ec Michael Walle
    R_MAX
34 87a381ec Michael Walle
};
35 87a381ec Michael Walle
36 87a381ec Michael Walle
enum {
37 87a381ec Michael Walle
    CTRL_RESET = (1<<0),
38 87a381ec Michael Walle
};
39 87a381ec Michael Walle
40 87a381ec Michael Walle
#define COMLOC_DEBUG_PRODUCE 0x1000
41 87a381ec Michael Walle
#define COMLOC_DEBUG_BASE    0x1001
42 87a381ec Michael Walle
#define COMLOC_MEVT_PRODUCE  0x1101
43 87a381ec Michael Walle
#define COMLOC_MEVT_BASE     0x1102
44 87a381ec Michael Walle
#define COMLOC_KEVT_PRODUCE  0x1142
45 87a381ec Michael Walle
#define COMLOC_KEVT_BASE     0x1143
46 87a381ec Michael Walle
47 87a381ec Michael Walle
struct MilkymistSoftUsbState {
48 87a381ec Michael Walle
    SysBusDevice busdev;
49 4c15ba9c Michael Walle
    HIDState hid_kbd;
50 4c15ba9c Michael Walle
    HIDState hid_mouse;
51 87a381ec Michael Walle
52 5105ed3b Avi Kivity
    MemoryRegion regs_region;
53 5105ed3b Avi Kivity
    MemoryRegion pmem;
54 5105ed3b Avi Kivity
    MemoryRegion dmem;
55 87a381ec Michael Walle
    qemu_irq irq;
56 87a381ec Michael Walle
57 c34e1205 Peter Maydell
    void *pmem_ptr;
58 c34e1205 Peter Maydell
    void *dmem_ptr;
59 c34e1205 Peter Maydell
60 87a381ec Michael Walle
    /* device properties */
61 87a381ec Michael Walle
    uint32_t pmem_size;
62 87a381ec Michael Walle
    uint32_t dmem_size;
63 87a381ec Michael Walle
64 87a381ec Michael Walle
    /* device registers */
65 87a381ec Michael Walle
    uint32_t regs[R_MAX];
66 87a381ec Michael Walle
67 87a381ec Michael Walle
    /* mouse state */
68 4c15ba9c Michael Walle
    uint8_t mouse_hid_buffer[4];
69 87a381ec Michael Walle
70 87a381ec Michael Walle
    /* keyboard state */
71 4c15ba9c Michael Walle
    uint8_t kbd_hid_buffer[8];
72 87a381ec Michael Walle
};
73 87a381ec Michael Walle
typedef struct MilkymistSoftUsbState MilkymistSoftUsbState;
74 87a381ec Michael Walle
75 a8170e5e Avi Kivity
static uint64_t softusb_read(void *opaque, hwaddr addr,
76 5105ed3b Avi Kivity
                             unsigned size)
77 87a381ec Michael Walle
{
78 87a381ec Michael Walle
    MilkymistSoftUsbState *s = opaque;
79 87a381ec Michael Walle
    uint32_t r = 0;
80 87a381ec Michael Walle
81 87a381ec Michael Walle
    addr >>= 2;
82 87a381ec Michael Walle
    switch (addr) {
83 87a381ec Michael Walle
    case R_CTRL:
84 87a381ec Michael Walle
        r = s->regs[addr];
85 87a381ec Michael Walle
        break;
86 87a381ec Michael Walle
87 87a381ec Michael Walle
    default:
88 87a381ec Michael Walle
        error_report("milkymist_softusb: read access to unknown register 0x"
89 87a381ec Michael Walle
                TARGET_FMT_plx, addr << 2);
90 87a381ec Michael Walle
        break;
91 87a381ec Michael Walle
    }
92 87a381ec Michael Walle
93 87a381ec Michael Walle
    trace_milkymist_softusb_memory_read(addr << 2, r);
94 87a381ec Michael Walle
95 87a381ec Michael Walle
    return r;
96 87a381ec Michael Walle
}
97 87a381ec Michael Walle
98 87a381ec Michael Walle
static void
99 a8170e5e Avi Kivity
softusb_write(void *opaque, hwaddr addr, uint64_t value,
100 5105ed3b Avi Kivity
              unsigned size)
101 87a381ec Michael Walle
{
102 87a381ec Michael Walle
    MilkymistSoftUsbState *s = opaque;
103 87a381ec Michael Walle
104 87a381ec Michael Walle
    trace_milkymist_softusb_memory_write(addr, value);
105 87a381ec Michael Walle
106 87a381ec Michael Walle
    addr >>= 2;
107 87a381ec Michael Walle
    switch (addr) {
108 87a381ec Michael Walle
    case R_CTRL:
109 87a381ec Michael Walle
        s->regs[addr] = value;
110 87a381ec Michael Walle
        break;
111 87a381ec Michael Walle
112 87a381ec Michael Walle
    default:
113 87a381ec Michael Walle
        error_report("milkymist_softusb: write access to unknown register 0x"
114 87a381ec Michael Walle
                TARGET_FMT_plx, addr << 2);
115 87a381ec Michael Walle
        break;
116 87a381ec Michael Walle
    }
117 87a381ec Michael Walle
}
118 87a381ec Michael Walle
119 5105ed3b Avi Kivity
static const MemoryRegionOps softusb_mmio_ops = {
120 5105ed3b Avi Kivity
    .read = softusb_read,
121 5105ed3b Avi Kivity
    .write = softusb_write,
122 5105ed3b Avi Kivity
    .endianness = DEVICE_NATIVE_ENDIAN,
123 5105ed3b Avi Kivity
    .valid = {
124 5105ed3b Avi Kivity
        .min_access_size = 4,
125 5105ed3b Avi Kivity
        .max_access_size = 4,
126 5105ed3b Avi Kivity
    },
127 87a381ec Michael Walle
};
128 87a381ec Michael Walle
129 87a381ec Michael Walle
static inline void softusb_read_dmem(MilkymistSoftUsbState *s,
130 87a381ec Michael Walle
        uint32_t offset, uint8_t *buf, uint32_t len)
131 87a381ec Michael Walle
{
132 87a381ec Michael Walle
    if (offset + len >= s->dmem_size) {
133 87a381ec Michael Walle
        error_report("milkymist_softusb: read dmem out of bounds "
134 6daf194d Markus Armbruster
                "at offset 0x%x, len %d", offset, len);
135 c31bc98e Peter Maydell
        memset(buf, 0, len);
136 87a381ec Michael Walle
        return;
137 87a381ec Michael Walle
    }
138 87a381ec Michael Walle
139 c34e1205 Peter Maydell
    memcpy(buf, s->dmem_ptr + offset, len);
140 87a381ec Michael Walle
}
141 87a381ec Michael Walle
142 87a381ec Michael Walle
static inline void softusb_write_dmem(MilkymistSoftUsbState *s,
143 87a381ec Michael Walle
        uint32_t offset, uint8_t *buf, uint32_t len)
144 87a381ec Michael Walle
{
145 87a381ec Michael Walle
    if (offset + len >= s->dmem_size) {
146 87a381ec Michael Walle
        error_report("milkymist_softusb: write dmem out of bounds "
147 6daf194d Markus Armbruster
                "at offset 0x%x, len %d", offset, len);
148 87a381ec Michael Walle
        return;
149 87a381ec Michael Walle
    }
150 87a381ec Michael Walle
151 c34e1205 Peter Maydell
    memcpy(s->dmem_ptr + offset, buf, len);
152 87a381ec Michael Walle
}
153 87a381ec Michael Walle
154 87a381ec Michael Walle
static inline void softusb_read_pmem(MilkymistSoftUsbState *s,
155 87a381ec Michael Walle
        uint32_t offset, uint8_t *buf, uint32_t len)
156 87a381ec Michael Walle
{
157 87a381ec Michael Walle
    if (offset + len >= s->pmem_size) {
158 87a381ec Michael Walle
        error_report("milkymist_softusb: read pmem out of bounds "
159 6daf194d Markus Armbruster
                "at offset 0x%x, len %d", offset, len);
160 c31bc98e Peter Maydell
        memset(buf, 0, len);
161 87a381ec Michael Walle
        return;
162 87a381ec Michael Walle
    }
163 87a381ec Michael Walle
164 c34e1205 Peter Maydell
    memcpy(buf, s->pmem_ptr + offset, len);
165 87a381ec Michael Walle
}
166 87a381ec Michael Walle
167 87a381ec Michael Walle
static inline void softusb_write_pmem(MilkymistSoftUsbState *s,
168 87a381ec Michael Walle
        uint32_t offset, uint8_t *buf, uint32_t len)
169 87a381ec Michael Walle
{
170 87a381ec Michael Walle
    if (offset + len >= s->pmem_size) {
171 87a381ec Michael Walle
        error_report("milkymist_softusb: write pmem out of bounds "
172 6daf194d Markus Armbruster
                "at offset 0x%x, len %d", offset, len);
173 87a381ec Michael Walle
        return;
174 87a381ec Michael Walle
    }
175 87a381ec Michael Walle
176 c34e1205 Peter Maydell
    memcpy(s->pmem_ptr + offset, buf, len);
177 87a381ec Michael Walle
}
178 87a381ec Michael Walle
179 87a381ec Michael Walle
static void softusb_mouse_changed(MilkymistSoftUsbState *s)
180 87a381ec Michael Walle
{
181 87a381ec Michael Walle
    uint8_t m;
182 87a381ec Michael Walle
183 87a381ec Michael Walle
    softusb_read_dmem(s, COMLOC_MEVT_PRODUCE, &m, 1);
184 87a381ec Michael Walle
    trace_milkymist_softusb_mevt(m);
185 4c15ba9c Michael Walle
    softusb_write_dmem(s, COMLOC_MEVT_BASE + 4 * m, s->mouse_hid_buffer, 4);
186 87a381ec Michael Walle
    m = (m + 1) & 0xf;
187 87a381ec Michael Walle
    softusb_write_dmem(s, COMLOC_MEVT_PRODUCE, &m, 1);
188 87a381ec Michael Walle
189 87a381ec Michael Walle
    trace_milkymist_softusb_pulse_irq();
190 87a381ec Michael Walle
    qemu_irq_pulse(s->irq);
191 87a381ec Michael Walle
}
192 87a381ec Michael Walle
193 87a381ec Michael Walle
static void softusb_kbd_changed(MilkymistSoftUsbState *s)
194 87a381ec Michael Walle
{
195 87a381ec Michael Walle
    uint8_t m;
196 87a381ec Michael Walle
197 87a381ec Michael Walle
    softusb_read_dmem(s, COMLOC_KEVT_PRODUCE, &m, 1);
198 87a381ec Michael Walle
    trace_milkymist_softusb_kevt(m);
199 4c15ba9c Michael Walle
    softusb_write_dmem(s, COMLOC_KEVT_BASE + 8 * m, s->kbd_hid_buffer, 8);
200 87a381ec Michael Walle
    m = (m + 1) & 0x7;
201 87a381ec Michael Walle
    softusb_write_dmem(s, COMLOC_KEVT_PRODUCE, &m, 1);
202 87a381ec Michael Walle
203 87a381ec Michael Walle
    trace_milkymist_softusb_pulse_irq();
204 87a381ec Michael Walle
    qemu_irq_pulse(s->irq);
205 87a381ec Michael Walle
}
206 87a381ec Michael Walle
207 4c15ba9c Michael Walle
static void softusb_kbd_hid_datain(HIDState *hs)
208 87a381ec Michael Walle
{
209 4c15ba9c Michael Walle
    MilkymistSoftUsbState *s = container_of(hs, MilkymistSoftUsbState, hid_kbd);
210 4c15ba9c Michael Walle
    int len;
211 87a381ec Michael Walle
212 87a381ec Michael Walle
    /* if device is in reset, do nothing */
213 87a381ec Michael Walle
    if (s->regs[R_CTRL] & CTRL_RESET) {
214 87a381ec Michael Walle
        return;
215 87a381ec Michael Walle
    }
216 87a381ec Michael Walle
217 4c15ba9c Michael Walle
    len = hid_keyboard_poll(hs, s->kbd_hid_buffer, sizeof(s->kbd_hid_buffer));
218 87a381ec Michael Walle
219 4c15ba9c Michael Walle
    if (len == 8) {
220 4c15ba9c Michael Walle
        softusb_kbd_changed(s);
221 4c15ba9c Michael Walle
    }
222 87a381ec Michael Walle
}
223 87a381ec Michael Walle
224 4c15ba9c Michael Walle
static void softusb_mouse_hid_datain(HIDState *hs)
225 87a381ec Michael Walle
{
226 4c15ba9c Michael Walle
    MilkymistSoftUsbState *s =
227 4c15ba9c Michael Walle
            container_of(hs, MilkymistSoftUsbState, hid_mouse);
228 4c15ba9c Michael Walle
    int len;
229 87a381ec Michael Walle
230 4c15ba9c Michael Walle
    /* if device is in reset, do nothing */
231 4c15ba9c Michael Walle
    if (s->regs[R_CTRL] & CTRL_RESET) {
232 4c15ba9c Michael Walle
        return;
233 4c15ba9c Michael Walle
    }
234 87a381ec Michael Walle
235 4c15ba9c Michael Walle
    len = hid_pointer_poll(hs, s->mouse_hid_buffer,
236 4c15ba9c Michael Walle
            sizeof(s->mouse_hid_buffer));
237 4706ab6c Hans de Goede
238 4c15ba9c Michael Walle
    if (len == 4) {
239 4c15ba9c Michael Walle
        softusb_mouse_changed(s);
240 4c15ba9c Michael Walle
    }
241 07771f6f Gerd Hoffmann
}
242 07771f6f Gerd Hoffmann
243 87a381ec Michael Walle
static void milkymist_softusb_reset(DeviceState *d)
244 87a381ec Michael Walle
{
245 87a381ec Michael Walle
    MilkymistSoftUsbState *s =
246 87a381ec Michael Walle
            container_of(d, MilkymistSoftUsbState, busdev.qdev);
247 87a381ec Michael Walle
    int i;
248 87a381ec Michael Walle
249 87a381ec Michael Walle
    for (i = 0; i < R_MAX; i++) {
250 87a381ec Michael Walle
        s->regs[i] = 0;
251 87a381ec Michael Walle
    }
252 4c15ba9c Michael Walle
    memset(s->kbd_hid_buffer, 0, sizeof(s->kbd_hid_buffer));
253 4c15ba9c Michael Walle
    memset(s->mouse_hid_buffer, 0, sizeof(s->mouse_hid_buffer));
254 4c15ba9c Michael Walle
255 4c15ba9c Michael Walle
    hid_reset(&s->hid_kbd);
256 4c15ba9c Michael Walle
    hid_reset(&s->hid_mouse);
257 87a381ec Michael Walle
258 87a381ec Michael Walle
    /* defaults */
259 87a381ec Michael Walle
    s->regs[R_CTRL] = CTRL_RESET;
260 87a381ec Michael Walle
}
261 87a381ec Michael Walle
262 87a381ec Michael Walle
static int milkymist_softusb_init(SysBusDevice *dev)
263 87a381ec Michael Walle
{
264 87a381ec Michael Walle
    MilkymistSoftUsbState *s = FROM_SYSBUS(typeof(*s), dev);
265 87a381ec Michael Walle
266 87a381ec Michael Walle
    sysbus_init_irq(dev, &s->irq);
267 87a381ec Michael Walle
268 5105ed3b Avi Kivity
    memory_region_init_io(&s->regs_region, &softusb_mmio_ops, s,
269 5105ed3b Avi Kivity
                          "milkymist-softusb", R_MAX * 4);
270 750ecd44 Avi Kivity
    sysbus_init_mmio(dev, &s->regs_region);
271 87a381ec Michael Walle
272 87a381ec Michael Walle
    /* register pmem and dmem */
273 c5705a77 Avi Kivity
    memory_region_init_ram(&s->pmem, "milkymist-softusb.pmem",
274 5105ed3b Avi Kivity
                           s->pmem_size);
275 c5705a77 Avi Kivity
    vmstate_register_ram_global(&s->pmem);
276 c34e1205 Peter Maydell
    s->pmem_ptr = memory_region_get_ram_ptr(&s->pmem);
277 c34e1205 Peter Maydell
    sysbus_init_mmio(dev, &s->pmem);
278 c5705a77 Avi Kivity
    memory_region_init_ram(&s->dmem, "milkymist-softusb.dmem",
279 5105ed3b Avi Kivity
                           s->dmem_size);
280 c5705a77 Avi Kivity
    vmstate_register_ram_global(&s->dmem);
281 c34e1205 Peter Maydell
    s->dmem_ptr = memory_region_get_ram_ptr(&s->dmem);
282 c34e1205 Peter Maydell
    sysbus_init_mmio(dev, &s->dmem);
283 87a381ec Michael Walle
284 4c15ba9c Michael Walle
    hid_init(&s->hid_kbd, HID_KEYBOARD, softusb_kbd_hid_datain);
285 4c15ba9c Michael Walle
    hid_init(&s->hid_mouse, HID_MOUSE, softusb_mouse_hid_datain);
286 87a381ec Michael Walle
287 87a381ec Michael Walle
    return 0;
288 87a381ec Michael Walle
}
289 87a381ec Michael Walle
290 87a381ec Michael Walle
static const VMStateDescription vmstate_milkymist_softusb = {
291 87a381ec Michael Walle
    .name = "milkymist-softusb",
292 87a381ec Michael Walle
    .version_id = 1,
293 87a381ec Michael Walle
    .minimum_version_id = 1,
294 87a381ec Michael Walle
    .minimum_version_id_old = 1,
295 87a381ec Michael Walle
    .fields      = (VMStateField[]) {
296 87a381ec Michael Walle
        VMSTATE_UINT32_ARRAY(regs, MilkymistSoftUsbState, R_MAX),
297 4c15ba9c Michael Walle
        VMSTATE_HID_KEYBOARD_DEVICE(hid_kbd, MilkymistSoftUsbState),
298 4c15ba9c Michael Walle
        VMSTATE_HID_POINTER_DEVICE(hid_mouse, MilkymistSoftUsbState),
299 4c15ba9c Michael Walle
        VMSTATE_BUFFER(kbd_hid_buffer, MilkymistSoftUsbState),
300 4c15ba9c Michael Walle
        VMSTATE_BUFFER(mouse_hid_buffer, MilkymistSoftUsbState),
301 87a381ec Michael Walle
        VMSTATE_END_OF_LIST()
302 87a381ec Michael Walle
    }
303 87a381ec Michael Walle
};
304 87a381ec Michael Walle
305 999e12bb Anthony Liguori
static Property milkymist_softusb_properties[] = {
306 999e12bb Anthony Liguori
    DEFINE_PROP_UINT32("pmem_size", MilkymistSoftUsbState, pmem_size, 0x00001000),
307 999e12bb Anthony Liguori
    DEFINE_PROP_UINT32("dmem_size", MilkymistSoftUsbState, dmem_size, 0x00002000),
308 999e12bb Anthony Liguori
    DEFINE_PROP_END_OF_LIST(),
309 999e12bb Anthony Liguori
};
310 999e12bb Anthony Liguori
311 999e12bb Anthony Liguori
static void milkymist_softusb_class_init(ObjectClass *klass, void *data)
312 999e12bb Anthony Liguori
{
313 39bffca2 Anthony Liguori
    DeviceClass *dc = DEVICE_CLASS(klass);
314 999e12bb Anthony Liguori
    SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
315 999e12bb Anthony Liguori
316 999e12bb Anthony Liguori
    k->init = milkymist_softusb_init;
317 39bffca2 Anthony Liguori
    dc->reset = milkymist_softusb_reset;
318 39bffca2 Anthony Liguori
    dc->vmsd = &vmstate_milkymist_softusb;
319 39bffca2 Anthony Liguori
    dc->props = milkymist_softusb_properties;
320 999e12bb Anthony Liguori
}
321 999e12bb Anthony Liguori
322 8c43a6f0 Andreas Färber
static const TypeInfo milkymist_softusb_info = {
323 39bffca2 Anthony Liguori
    .name          = "milkymist-softusb",
324 39bffca2 Anthony Liguori
    .parent        = TYPE_SYS_BUS_DEVICE,
325 39bffca2 Anthony Liguori
    .instance_size = sizeof(MilkymistSoftUsbState),
326 39bffca2 Anthony Liguori
    .class_init    = milkymist_softusb_class_init,
327 87a381ec Michael Walle
};
328 87a381ec Michael Walle
329 83f7d43a Andreas Färber
static void milkymist_softusb_register_types(void)
330 87a381ec Michael Walle
{
331 39bffca2 Anthony Liguori
    type_register_static(&milkymist_softusb_info);
332 87a381ec Michael Walle
}
333 87a381ec Michael Walle
334 83f7d43a Andreas Färber
type_init(milkymist_softusb_register_types)