Statistics
| Branch: | Revision:

root / hw / milkymist-softusb.c @ cff0cfbe

History | View | Annotate | Download (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 87a381ec Michael Walle
#include "hw.h"
25 87a381ec Michael Walle
#include "sysbus.h"
26 87a381ec Michael Walle
#include "trace.h"
27 87a381ec Michael Walle
#include "console.h"
28 4c15ba9c Michael Walle
#include "hid.h"
29 87a381ec Michael Walle
#include "qemu-error.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 87a381ec Michael Walle
    /* device properties */
58 87a381ec Michael Walle
    uint32_t pmem_base;
59 87a381ec Michael Walle
    uint32_t pmem_size;
60 87a381ec Michael Walle
    uint32_t dmem_base;
61 87a381ec Michael Walle
    uint32_t dmem_size;
62 87a381ec Michael Walle
63 87a381ec Michael Walle
    /* device registers */
64 87a381ec Michael Walle
    uint32_t regs[R_MAX];
65 87a381ec Michael Walle
66 87a381ec Michael Walle
    /* mouse state */
67 4c15ba9c Michael Walle
    uint8_t mouse_hid_buffer[4];
68 87a381ec Michael Walle
69 87a381ec Michael Walle
    /* keyboard state */
70 4c15ba9c Michael Walle
    uint8_t kbd_hid_buffer[8];
71 87a381ec Michael Walle
};
72 87a381ec Michael Walle
typedef struct MilkymistSoftUsbState MilkymistSoftUsbState;
73 87a381ec Michael Walle
74 5105ed3b Avi Kivity
static uint64_t softusb_read(void *opaque, target_phys_addr_t addr,
75 5105ed3b Avi Kivity
                             unsigned size)
76 87a381ec Michael Walle
{
77 87a381ec Michael Walle
    MilkymistSoftUsbState *s = opaque;
78 87a381ec Michael Walle
    uint32_t r = 0;
79 87a381ec Michael Walle
80 87a381ec Michael Walle
    addr >>= 2;
81 87a381ec Michael Walle
    switch (addr) {
82 87a381ec Michael Walle
    case R_CTRL:
83 87a381ec Michael Walle
        r = s->regs[addr];
84 87a381ec Michael Walle
        break;
85 87a381ec Michael Walle
86 87a381ec Michael Walle
    default:
87 87a381ec Michael Walle
        error_report("milkymist_softusb: read access to unknown register 0x"
88 87a381ec Michael Walle
                TARGET_FMT_plx, addr << 2);
89 87a381ec Michael Walle
        break;
90 87a381ec Michael Walle
    }
91 87a381ec Michael Walle
92 87a381ec Michael Walle
    trace_milkymist_softusb_memory_read(addr << 2, r);
93 87a381ec Michael Walle
94 87a381ec Michael Walle
    return r;
95 87a381ec Michael Walle
}
96 87a381ec Michael Walle
97 87a381ec Michael Walle
static void
98 5105ed3b Avi Kivity
softusb_write(void *opaque, target_phys_addr_t addr, uint64_t value,
99 5105ed3b Avi Kivity
              unsigned size)
100 87a381ec Michael Walle
{
101 87a381ec Michael Walle
    MilkymistSoftUsbState *s = opaque;
102 87a381ec Michael Walle
103 87a381ec Michael Walle
    trace_milkymist_softusb_memory_write(addr, value);
104 87a381ec Michael Walle
105 87a381ec Michael Walle
    addr >>= 2;
106 87a381ec Michael Walle
    switch (addr) {
107 87a381ec Michael Walle
    case R_CTRL:
108 87a381ec Michael Walle
        s->regs[addr] = value;
109 87a381ec Michael Walle
        break;
110 87a381ec Michael Walle
111 87a381ec Michael Walle
    default:
112 87a381ec Michael Walle
        error_report("milkymist_softusb: write access to unknown register 0x"
113 87a381ec Michael Walle
                TARGET_FMT_plx, addr << 2);
114 87a381ec Michael Walle
        break;
115 87a381ec Michael Walle
    }
116 87a381ec Michael Walle
}
117 87a381ec Michael Walle
118 5105ed3b Avi Kivity
static const MemoryRegionOps softusb_mmio_ops = {
119 5105ed3b Avi Kivity
    .read = softusb_read,
120 5105ed3b Avi Kivity
    .write = softusb_write,
121 5105ed3b Avi Kivity
    .endianness = DEVICE_NATIVE_ENDIAN,
122 5105ed3b Avi Kivity
    .valid = {
123 5105ed3b Avi Kivity
        .min_access_size = 4,
124 5105ed3b Avi Kivity
        .max_access_size = 4,
125 5105ed3b Avi Kivity
    },
126 87a381ec Michael Walle
};
127 87a381ec Michael Walle
128 87a381ec Michael Walle
static inline void softusb_read_dmem(MilkymistSoftUsbState *s,
129 87a381ec Michael Walle
        uint32_t offset, uint8_t *buf, uint32_t len)
130 87a381ec Michael Walle
{
131 87a381ec Michael Walle
    if (offset + len >= s->dmem_size) {
132 87a381ec Michael Walle
        error_report("milkymist_softusb: read dmem out of bounds "
133 6daf194d Markus Armbruster
                "at offset 0x%x, len %d", offset, len);
134 87a381ec Michael Walle
        return;
135 87a381ec Michael Walle
    }
136 87a381ec Michael Walle
137 87a381ec Michael Walle
    cpu_physical_memory_read(s->dmem_base + offset, buf, len);
138 87a381ec Michael Walle
}
139 87a381ec Michael Walle
140 87a381ec Michael Walle
static inline void softusb_write_dmem(MilkymistSoftUsbState *s,
141 87a381ec Michael Walle
        uint32_t offset, uint8_t *buf, uint32_t len)
142 87a381ec Michael Walle
{
143 87a381ec Michael Walle
    if (offset + len >= s->dmem_size) {
144 87a381ec Michael Walle
        error_report("milkymist_softusb: write dmem out of bounds "
145 6daf194d Markus Armbruster
                "at offset 0x%x, len %d", offset, len);
146 87a381ec Michael Walle
        return;
147 87a381ec Michael Walle
    }
148 87a381ec Michael Walle
149 87a381ec Michael Walle
    cpu_physical_memory_write(s->dmem_base + offset, buf, len);
150 87a381ec Michael Walle
}
151 87a381ec Michael Walle
152 87a381ec Michael Walle
static inline void softusb_read_pmem(MilkymistSoftUsbState *s,
153 87a381ec Michael Walle
        uint32_t offset, uint8_t *buf, uint32_t len)
154 87a381ec Michael Walle
{
155 87a381ec Michael Walle
    if (offset + len >= s->pmem_size) {
156 87a381ec Michael Walle
        error_report("milkymist_softusb: read pmem out of bounds "
157 6daf194d Markus Armbruster
                "at offset 0x%x, len %d", offset, len);
158 87a381ec Michael Walle
        return;
159 87a381ec Michael Walle
    }
160 87a381ec Michael Walle
161 87a381ec Michael Walle
    cpu_physical_memory_read(s->pmem_base + offset, buf, len);
162 87a381ec Michael Walle
}
163 87a381ec Michael Walle
164 87a381ec Michael Walle
static inline void softusb_write_pmem(MilkymistSoftUsbState *s,
165 87a381ec Michael Walle
        uint32_t offset, uint8_t *buf, uint32_t len)
166 87a381ec Michael Walle
{
167 87a381ec Michael Walle
    if (offset + len >= s->pmem_size) {
168 87a381ec Michael Walle
        error_report("milkymist_softusb: write pmem out of bounds "
169 6daf194d Markus Armbruster
                "at offset 0x%x, len %d", offset, len);
170 87a381ec Michael Walle
        return;
171 87a381ec Michael Walle
    }
172 87a381ec Michael Walle
173 87a381ec Michael Walle
    cpu_physical_memory_write(s->pmem_base + offset, buf, len);
174 87a381ec Michael Walle
}
175 87a381ec Michael Walle
176 87a381ec Michael Walle
static void softusb_mouse_changed(MilkymistSoftUsbState *s)
177 87a381ec Michael Walle
{
178 87a381ec Michael Walle
    uint8_t m;
179 87a381ec Michael Walle
180 87a381ec Michael Walle
    softusb_read_dmem(s, COMLOC_MEVT_PRODUCE, &m, 1);
181 87a381ec Michael Walle
    trace_milkymist_softusb_mevt(m);
182 4c15ba9c Michael Walle
    softusb_write_dmem(s, COMLOC_MEVT_BASE + 4 * m, s->mouse_hid_buffer, 4);
183 87a381ec Michael Walle
    m = (m + 1) & 0xf;
184 87a381ec Michael Walle
    softusb_write_dmem(s, COMLOC_MEVT_PRODUCE, &m, 1);
185 87a381ec Michael Walle
186 87a381ec Michael Walle
    trace_milkymist_softusb_pulse_irq();
187 87a381ec Michael Walle
    qemu_irq_pulse(s->irq);
188 87a381ec Michael Walle
}
189 87a381ec Michael Walle
190 87a381ec Michael Walle
static void softusb_kbd_changed(MilkymistSoftUsbState *s)
191 87a381ec Michael Walle
{
192 87a381ec Michael Walle
    uint8_t m;
193 87a381ec Michael Walle
194 87a381ec Michael Walle
    softusb_read_dmem(s, COMLOC_KEVT_PRODUCE, &m, 1);
195 87a381ec Michael Walle
    trace_milkymist_softusb_kevt(m);
196 4c15ba9c Michael Walle
    softusb_write_dmem(s, COMLOC_KEVT_BASE + 8 * m, s->kbd_hid_buffer, 8);
197 87a381ec Michael Walle
    m = (m + 1) & 0x7;
198 87a381ec Michael Walle
    softusb_write_dmem(s, COMLOC_KEVT_PRODUCE, &m, 1);
199 87a381ec Michael Walle
200 87a381ec Michael Walle
    trace_milkymist_softusb_pulse_irq();
201 87a381ec Michael Walle
    qemu_irq_pulse(s->irq);
202 87a381ec Michael Walle
}
203 87a381ec Michael Walle
204 4c15ba9c Michael Walle
static void softusb_kbd_hid_datain(HIDState *hs)
205 87a381ec Michael Walle
{
206 4c15ba9c Michael Walle
    MilkymistSoftUsbState *s = container_of(hs, MilkymistSoftUsbState, hid_kbd);
207 4c15ba9c Michael Walle
    int len;
208 87a381ec Michael Walle
209 87a381ec Michael Walle
    /* if device is in reset, do nothing */
210 87a381ec Michael Walle
    if (s->regs[R_CTRL] & CTRL_RESET) {
211 87a381ec Michael Walle
        return;
212 87a381ec Michael Walle
    }
213 87a381ec Michael Walle
214 4c15ba9c Michael Walle
    len = hid_keyboard_poll(hs, s->kbd_hid_buffer, sizeof(s->kbd_hid_buffer));
215 87a381ec Michael Walle
216 4c15ba9c Michael Walle
    if (len == 8) {
217 4c15ba9c Michael Walle
        softusb_kbd_changed(s);
218 4c15ba9c Michael Walle
    }
219 87a381ec Michael Walle
}
220 87a381ec Michael Walle
221 4c15ba9c Michael Walle
static void softusb_mouse_hid_datain(HIDState *hs)
222 87a381ec Michael Walle
{
223 4c15ba9c Michael Walle
    MilkymistSoftUsbState *s =
224 4c15ba9c Michael Walle
            container_of(hs, MilkymistSoftUsbState, hid_mouse);
225 4c15ba9c Michael Walle
    int len;
226 87a381ec Michael Walle
227 4c15ba9c Michael Walle
    /* if device is in reset, do nothing */
228 4c15ba9c Michael Walle
    if (s->regs[R_CTRL] & CTRL_RESET) {
229 4c15ba9c Michael Walle
        return;
230 4c15ba9c Michael Walle
    }
231 87a381ec Michael Walle
232 4c15ba9c Michael Walle
    len = hid_pointer_poll(hs, s->mouse_hid_buffer,
233 4c15ba9c Michael Walle
            sizeof(s->mouse_hid_buffer));
234 4706ab6c Hans de Goede
235 4c15ba9c Michael Walle
    if (len == 4) {
236 4c15ba9c Michael Walle
        softusb_mouse_changed(s);
237 4c15ba9c Michael Walle
    }
238 07771f6f Gerd Hoffmann
}
239 07771f6f Gerd Hoffmann
240 87a381ec Michael Walle
static void milkymist_softusb_reset(DeviceState *d)
241 87a381ec Michael Walle
{
242 87a381ec Michael Walle
    MilkymistSoftUsbState *s =
243 87a381ec Michael Walle
            container_of(d, MilkymistSoftUsbState, busdev.qdev);
244 87a381ec Michael Walle
    int i;
245 87a381ec Michael Walle
246 87a381ec Michael Walle
    for (i = 0; i < R_MAX; i++) {
247 87a381ec Michael Walle
        s->regs[i] = 0;
248 87a381ec Michael Walle
    }
249 4c15ba9c Michael Walle
    memset(s->kbd_hid_buffer, 0, sizeof(s->kbd_hid_buffer));
250 4c15ba9c Michael Walle
    memset(s->mouse_hid_buffer, 0, sizeof(s->mouse_hid_buffer));
251 4c15ba9c Michael Walle
252 4c15ba9c Michael Walle
    hid_reset(&s->hid_kbd);
253 4c15ba9c Michael Walle
    hid_reset(&s->hid_mouse);
254 87a381ec Michael Walle
255 87a381ec Michael Walle
    /* defaults */
256 87a381ec Michael Walle
    s->regs[R_CTRL] = CTRL_RESET;
257 87a381ec Michael Walle
}
258 87a381ec Michael Walle
259 87a381ec Michael Walle
static int milkymist_softusb_init(SysBusDevice *dev)
260 87a381ec Michael Walle
{
261 87a381ec Michael Walle
    MilkymistSoftUsbState *s = FROM_SYSBUS(typeof(*s), dev);
262 87a381ec Michael Walle
263 87a381ec Michael Walle
    sysbus_init_irq(dev, &s->irq);
264 87a381ec Michael Walle
265 5105ed3b Avi Kivity
    memory_region_init_io(&s->regs_region, &softusb_mmio_ops, s,
266 5105ed3b Avi Kivity
                          "milkymist-softusb", R_MAX * 4);
267 750ecd44 Avi Kivity
    sysbus_init_mmio(dev, &s->regs_region);
268 87a381ec Michael Walle
269 87a381ec Michael Walle
    /* register pmem and dmem */
270 c5705a77 Avi Kivity
    memory_region_init_ram(&s->pmem, "milkymist-softusb.pmem",
271 5105ed3b Avi Kivity
                           s->pmem_size);
272 c5705a77 Avi Kivity
    vmstate_register_ram_global(&s->pmem);
273 5105ed3b Avi Kivity
    sysbus_add_memory(dev, s->pmem_base, &s->pmem);
274 c5705a77 Avi Kivity
    memory_region_init_ram(&s->dmem, "milkymist-softusb.dmem",
275 5105ed3b Avi Kivity
                           s->dmem_size);
276 c5705a77 Avi Kivity
    vmstate_register_ram_global(&s->dmem);
277 5105ed3b Avi Kivity
    sysbus_add_memory(dev, s->dmem_base, &s->dmem);
278 87a381ec Michael Walle
279 4c15ba9c Michael Walle
    hid_init(&s->hid_kbd, HID_KEYBOARD, softusb_kbd_hid_datain);
280 4c15ba9c Michael Walle
    hid_init(&s->hid_mouse, HID_MOUSE, softusb_mouse_hid_datain);
281 87a381ec Michael Walle
282 87a381ec Michael Walle
    return 0;
283 87a381ec Michael Walle
}
284 87a381ec Michael Walle
285 87a381ec Michael Walle
static const VMStateDescription vmstate_milkymist_softusb = {
286 87a381ec Michael Walle
    .name = "milkymist-softusb",
287 87a381ec Michael Walle
    .version_id = 1,
288 87a381ec Michael Walle
    .minimum_version_id = 1,
289 87a381ec Michael Walle
    .minimum_version_id_old = 1,
290 87a381ec Michael Walle
    .fields      = (VMStateField[]) {
291 87a381ec Michael Walle
        VMSTATE_UINT32_ARRAY(regs, MilkymistSoftUsbState, R_MAX),
292 4c15ba9c Michael Walle
        VMSTATE_HID_KEYBOARD_DEVICE(hid_kbd, MilkymistSoftUsbState),
293 4c15ba9c Michael Walle
        VMSTATE_HID_POINTER_DEVICE(hid_mouse, MilkymistSoftUsbState),
294 4c15ba9c Michael Walle
        VMSTATE_BUFFER(kbd_hid_buffer, MilkymistSoftUsbState),
295 4c15ba9c Michael Walle
        VMSTATE_BUFFER(mouse_hid_buffer, MilkymistSoftUsbState),
296 87a381ec Michael Walle
        VMSTATE_END_OF_LIST()
297 87a381ec Michael Walle
    }
298 87a381ec Michael Walle
};
299 87a381ec Michael Walle
300 999e12bb Anthony Liguori
static Property milkymist_softusb_properties[] = {
301 999e12bb Anthony Liguori
    DEFINE_PROP_UINT32("pmem_base", MilkymistSoftUsbState, pmem_base, 0xa0000000),
302 999e12bb Anthony Liguori
    DEFINE_PROP_UINT32("pmem_size", MilkymistSoftUsbState, pmem_size, 0x00001000),
303 999e12bb Anthony Liguori
    DEFINE_PROP_UINT32("dmem_base", MilkymistSoftUsbState, dmem_base, 0xa0020000),
304 999e12bb Anthony Liguori
    DEFINE_PROP_UINT32("dmem_size", MilkymistSoftUsbState, dmem_size, 0x00002000),
305 999e12bb Anthony Liguori
    DEFINE_PROP_END_OF_LIST(),
306 999e12bb Anthony Liguori
};
307 999e12bb Anthony Liguori
308 999e12bb Anthony Liguori
static void milkymist_softusb_class_init(ObjectClass *klass, void *data)
309 999e12bb Anthony Liguori
{
310 39bffca2 Anthony Liguori
    DeviceClass *dc = DEVICE_CLASS(klass);
311 999e12bb Anthony Liguori
    SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
312 999e12bb Anthony Liguori
313 999e12bb Anthony Liguori
    k->init = milkymist_softusb_init;
314 39bffca2 Anthony Liguori
    dc->reset = milkymist_softusb_reset;
315 39bffca2 Anthony Liguori
    dc->vmsd = &vmstate_milkymist_softusb;
316 39bffca2 Anthony Liguori
    dc->props = milkymist_softusb_properties;
317 999e12bb Anthony Liguori
}
318 999e12bb Anthony Liguori
319 39bffca2 Anthony Liguori
static TypeInfo milkymist_softusb_info = {
320 39bffca2 Anthony Liguori
    .name          = "milkymist-softusb",
321 39bffca2 Anthony Liguori
    .parent        = TYPE_SYS_BUS_DEVICE,
322 39bffca2 Anthony Liguori
    .instance_size = sizeof(MilkymistSoftUsbState),
323 39bffca2 Anthony Liguori
    .class_init    = milkymist_softusb_class_init,
324 87a381ec Michael Walle
};
325 87a381ec Michael Walle
326 83f7d43a Andreas Färber
static void milkymist_softusb_register_types(void)
327 87a381ec Michael Walle
{
328 39bffca2 Anthony Liguori
    type_register_static(&milkymist_softusb_info);
329 87a381ec Michael Walle
}
330 87a381ec Michael Walle
331 83f7d43a Andreas Färber
type_init(milkymist_softusb_register_types)