Statistics
| Branch: | Revision:

root / hw / milkymist-softusb.c @ fe55ff6e

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