Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (8.9 kB)

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

    
24
#include "hw/hw.h"
25
#include "hw/sysbus.h"
26
#include "trace.h"
27
#include "ui/console.h"
28
#include "hw/input/hid.h"
29
#include "qemu/error-report.h"
30

    
31
enum {
32
    R_CTRL = 0,
33
    R_MAX
34
};
35

    
36
enum {
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

    
47
struct MilkymistSoftUsbState {
48
    SysBusDevice busdev;
49
    HIDState hid_kbd;
50
    HIDState hid_mouse;
51

    
52
    MemoryRegion regs_region;
53
    MemoryRegion pmem;
54
    MemoryRegion dmem;
55
    qemu_irq irq;
56

    
57
    void *pmem_ptr;
58
    void *dmem_ptr;
59

    
60
    /* device properties */
61
    uint32_t pmem_size;
62
    uint32_t dmem_size;
63

    
64
    /* device registers */
65
    uint32_t regs[R_MAX];
66

    
67
    /* mouse state */
68
    uint8_t mouse_hid_buffer[4];
69

    
70
    /* keyboard state */
71
    uint8_t kbd_hid_buffer[8];
72
};
73
typedef struct MilkymistSoftUsbState MilkymistSoftUsbState;
74

    
75
static uint64_t softusb_read(void *opaque, hwaddr addr,
76
                             unsigned size)
77
{
78
    MilkymistSoftUsbState *s = opaque;
79
    uint32_t r = 0;
80

    
81
    addr >>= 2;
82
    switch (addr) {
83
    case R_CTRL:
84
        r = s->regs[addr];
85
        break;
86

    
87
    default:
88
        error_report("milkymist_softusb: read access to unknown register 0x"
89
                TARGET_FMT_plx, addr << 2);
90
        break;
91
    }
92

    
93
    trace_milkymist_softusb_memory_read(addr << 2, r);
94

    
95
    return r;
96
}
97

    
98
static void
99
softusb_write(void *opaque, hwaddr addr, uint64_t value,
100
              unsigned size)
101
{
102
    MilkymistSoftUsbState *s = opaque;
103

    
104
    trace_milkymist_softusb_memory_write(addr, value);
105

    
106
    addr >>= 2;
107
    switch (addr) {
108
    case R_CTRL:
109
        s->regs[addr] = value;
110
        break;
111

    
112
    default:
113
        error_report("milkymist_softusb: write access to unknown register 0x"
114
                TARGET_FMT_plx, addr << 2);
115
        break;
116
    }
117
}
118

    
119
static const MemoryRegionOps softusb_mmio_ops = {
120
    .read = softusb_read,
121
    .write = softusb_write,
122
    .endianness = DEVICE_NATIVE_ENDIAN,
123
    .valid = {
124
        .min_access_size = 4,
125
        .max_access_size = 4,
126
    },
127
};
128

    
129
static inline void softusb_read_dmem(MilkymistSoftUsbState *s,
130
        uint32_t offset, uint8_t *buf, uint32_t len)
131
{
132
    if (offset + len >= s->dmem_size) {
133
        error_report("milkymist_softusb: read dmem out of bounds "
134
                "at offset 0x%x, len %d", offset, len);
135
        memset(buf, 0, len);
136
        return;
137
    }
138

    
139
    memcpy(buf, s->dmem_ptr + offset, len);
140
}
141

    
142
static inline void softusb_write_dmem(MilkymistSoftUsbState *s,
143
        uint32_t offset, uint8_t *buf, uint32_t len)
144
{
145
    if (offset + len >= s->dmem_size) {
146
        error_report("milkymist_softusb: write dmem out of bounds "
147
                "at offset 0x%x, len %d", offset, len);
148
        return;
149
    }
150

    
151
    memcpy(s->dmem_ptr + offset, buf, len);
152
}
153

    
154
static inline void softusb_read_pmem(MilkymistSoftUsbState *s,
155
        uint32_t offset, uint8_t *buf, uint32_t len)
156
{
157
    if (offset + len >= s->pmem_size) {
158
        error_report("milkymist_softusb: read pmem out of bounds "
159
                "at offset 0x%x, len %d", offset, len);
160
        memset(buf, 0, len);
161
        return;
162
    }
163

    
164
    memcpy(buf, s->pmem_ptr + offset, len);
165
}
166

    
167
static inline void softusb_write_pmem(MilkymistSoftUsbState *s,
168
        uint32_t offset, uint8_t *buf, uint32_t len)
169
{
170
    if (offset + len >= s->pmem_size) {
171
        error_report("milkymist_softusb: write pmem out of bounds "
172
                "at offset 0x%x, len %d", offset, len);
173
        return;
174
    }
175

    
176
    memcpy(s->pmem_ptr + offset, buf, len);
177
}
178

    
179
static void softusb_mouse_changed(MilkymistSoftUsbState *s)
180
{
181
    uint8_t m;
182

    
183
    softusb_read_dmem(s, COMLOC_MEVT_PRODUCE, &m, 1);
184
    trace_milkymist_softusb_mevt(m);
185
    softusb_write_dmem(s, COMLOC_MEVT_BASE + 4 * m, s->mouse_hid_buffer, 4);
186
    m = (m + 1) & 0xf;
187
    softusb_write_dmem(s, COMLOC_MEVT_PRODUCE, &m, 1);
188

    
189
    trace_milkymist_softusb_pulse_irq();
190
    qemu_irq_pulse(s->irq);
191
}
192

    
193
static void softusb_kbd_changed(MilkymistSoftUsbState *s)
194
{
195
    uint8_t m;
196

    
197
    softusb_read_dmem(s, COMLOC_KEVT_PRODUCE, &m, 1);
198
    trace_milkymist_softusb_kevt(m);
199
    softusb_write_dmem(s, COMLOC_KEVT_BASE + 8 * m, s->kbd_hid_buffer, 8);
200
    m = (m + 1) & 0x7;
201
    softusb_write_dmem(s, COMLOC_KEVT_PRODUCE, &m, 1);
202

    
203
    trace_milkymist_softusb_pulse_irq();
204
    qemu_irq_pulse(s->irq);
205
}
206

    
207
static void softusb_kbd_hid_datain(HIDState *hs)
208
{
209
    MilkymistSoftUsbState *s = container_of(hs, MilkymistSoftUsbState, hid_kbd);
210
    int len;
211

    
212
    /* if device is in reset, do nothing */
213
    if (s->regs[R_CTRL] & CTRL_RESET) {
214
        return;
215
    }
216

    
217
    len = hid_keyboard_poll(hs, s->kbd_hid_buffer, sizeof(s->kbd_hid_buffer));
218

    
219
    if (len == 8) {
220
        softusb_kbd_changed(s);
221
    }
222
}
223

    
224
static void softusb_mouse_hid_datain(HIDState *hs)
225
{
226
    MilkymistSoftUsbState *s =
227
            container_of(hs, MilkymistSoftUsbState, hid_mouse);
228
    int len;
229

    
230
    /* if device is in reset, do nothing */
231
    if (s->regs[R_CTRL] & CTRL_RESET) {
232
        return;
233
    }
234

    
235
    len = hid_pointer_poll(hs, s->mouse_hid_buffer,
236
            sizeof(s->mouse_hid_buffer));
237

    
238
    if (len == 4) {
239
        softusb_mouse_changed(s);
240
    }
241
}
242

    
243
static void milkymist_softusb_reset(DeviceState *d)
244
{
245
    MilkymistSoftUsbState *s =
246
            container_of(d, MilkymistSoftUsbState, busdev.qdev);
247
    int i;
248

    
249
    for (i = 0; i < R_MAX; i++) {
250
        s->regs[i] = 0;
251
    }
252
    memset(s->kbd_hid_buffer, 0, sizeof(s->kbd_hid_buffer));
253
    memset(s->mouse_hid_buffer, 0, sizeof(s->mouse_hid_buffer));
254

    
255
    hid_reset(&s->hid_kbd);
256
    hid_reset(&s->hid_mouse);
257

    
258
    /* defaults */
259
    s->regs[R_CTRL] = CTRL_RESET;
260
}
261

    
262
static int milkymist_softusb_init(SysBusDevice *dev)
263
{
264
    MilkymistSoftUsbState *s = FROM_SYSBUS(typeof(*s), dev);
265

    
266
    sysbus_init_irq(dev, &s->irq);
267

    
268
    memory_region_init_io(&s->regs_region, &softusb_mmio_ops, s,
269
                          "milkymist-softusb", R_MAX * 4);
270
    sysbus_init_mmio(dev, &s->regs_region);
271

    
272
    /* register pmem and dmem */
273
    memory_region_init_ram(&s->pmem, "milkymist-softusb.pmem",
274
                           s->pmem_size);
275
    vmstate_register_ram_global(&s->pmem);
276
    s->pmem_ptr = memory_region_get_ram_ptr(&s->pmem);
277
    sysbus_init_mmio(dev, &s->pmem);
278
    memory_region_init_ram(&s->dmem, "milkymist-softusb.dmem",
279
                           s->dmem_size);
280
    vmstate_register_ram_global(&s->dmem);
281
    s->dmem_ptr = memory_region_get_ram_ptr(&s->dmem);
282
    sysbus_init_mmio(dev, &s->dmem);
283

    
284
    hid_init(&s->hid_kbd, HID_KEYBOARD, softusb_kbd_hid_datain);
285
    hid_init(&s->hid_mouse, HID_MOUSE, softusb_mouse_hid_datain);
286

    
287
    return 0;
288
}
289

    
290
static const VMStateDescription vmstate_milkymist_softusb = {
291
    .name = "milkymist-softusb",
292
    .version_id = 1,
293
    .minimum_version_id = 1,
294
    .minimum_version_id_old = 1,
295
    .fields      = (VMStateField[]) {
296
        VMSTATE_UINT32_ARRAY(regs, MilkymistSoftUsbState, R_MAX),
297
        VMSTATE_HID_KEYBOARD_DEVICE(hid_kbd, MilkymistSoftUsbState),
298
        VMSTATE_HID_POINTER_DEVICE(hid_mouse, MilkymistSoftUsbState),
299
        VMSTATE_BUFFER(kbd_hid_buffer, MilkymistSoftUsbState),
300
        VMSTATE_BUFFER(mouse_hid_buffer, MilkymistSoftUsbState),
301
        VMSTATE_END_OF_LIST()
302
    }
303
};
304

    
305
static Property milkymist_softusb_properties[] = {
306
    DEFINE_PROP_UINT32("pmem_size", MilkymistSoftUsbState, pmem_size, 0x00001000),
307
    DEFINE_PROP_UINT32("dmem_size", MilkymistSoftUsbState, dmem_size, 0x00002000),
308
    DEFINE_PROP_END_OF_LIST(),
309
};
310

    
311
static void milkymist_softusb_class_init(ObjectClass *klass, void *data)
312
{
313
    DeviceClass *dc = DEVICE_CLASS(klass);
314
    SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
315

    
316
    k->init = milkymist_softusb_init;
317
    dc->reset = milkymist_softusb_reset;
318
    dc->vmsd = &vmstate_milkymist_softusb;
319
    dc->props = milkymist_softusb_properties;
320
}
321

    
322
static const TypeInfo milkymist_softusb_info = {
323
    .name          = "milkymist-softusb",
324
    .parent        = TYPE_SYS_BUS_DEVICE,
325
    .instance_size = sizeof(MilkymistSoftUsbState),
326
    .class_init    = milkymist_softusb_class_init,
327
};
328

    
329
static void milkymist_softusb_register_types(void)
330
{
331
    type_register_static(&milkymist_softusb_info);
332
}
333

    
334
type_init(milkymist_softusb_register_types)