eepro100: convert to memory API
[qemu/mdroth.git] / hw / milkymist-softusb.c
blob75c85aeb6f2dd068e5a738a844bea8d9c7a07ad0
1 /*
2 * QEMU model of the Milkymist SoftUSB block.
4 * Copyright (c) 2010 Michael Walle <michael@walle.cc>
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.
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.
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/>.
20 * Specification available at:
21 * not available yet
24 #include "hw.h"
25 #include "sysbus.h"
26 #include "trace.h"
27 #include "console.h"
28 #include "usb.h"
29 #include "qemu-error.h"
31 enum {
32 R_CTRL = 0,
33 R_MAX
36 enum {
37 CTRL_RESET = (1<<0),
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
47 struct MilkymistSoftUsbState {
48 SysBusDevice busdev;
49 USBBus usbbus;
50 USBPort usbport[2];
51 USBDevice *usbdev;
53 qemu_irq irq;
55 /* device properties */
56 uint32_t pmem_base;
57 uint32_t pmem_size;
58 uint32_t dmem_base;
59 uint32_t dmem_size;
61 /* device registers */
62 uint32_t regs[R_MAX];
64 /* mouse state */
65 int mouse_dx;
66 int mouse_dy;
67 int mouse_dz;
68 uint8_t mouse_buttons_state;
70 /* keyboard state */
71 uint8_t kbd_usb_buffer[8];
73 typedef struct MilkymistSoftUsbState MilkymistSoftUsbState;
75 static uint32_t softusb_read(void *opaque, target_phys_addr_t addr)
77 MilkymistSoftUsbState *s = opaque;
78 uint32_t r = 0;
80 addr >>= 2;
81 switch (addr) {
82 case R_CTRL:
83 r = s->regs[addr];
84 break;
86 default:
87 error_report("milkymist_softusb: read access to unknown register 0x"
88 TARGET_FMT_plx, addr << 2);
89 break;
92 trace_milkymist_softusb_memory_read(addr << 2, r);
94 return r;
97 static void
98 softusb_write(void *opaque, target_phys_addr_t addr, uint32_t value)
100 MilkymistSoftUsbState *s = opaque;
102 trace_milkymist_softusb_memory_write(addr, value);
104 addr >>= 2;
105 switch (addr) {
106 case R_CTRL:
107 s->regs[addr] = value;
108 break;
110 default:
111 error_report("milkymist_softusb: write access to unknown register 0x"
112 TARGET_FMT_plx, addr << 2);
113 break;
117 static CPUReadMemoryFunc * const softusb_read_fn[] = {
118 NULL,
119 NULL,
120 &softusb_read,
123 static CPUWriteMemoryFunc * const softusb_write_fn[] = {
124 NULL,
125 NULL,
126 &softusb_write,
129 static inline void softusb_read_dmem(MilkymistSoftUsbState *s,
130 uint32_t offset, uint8_t *buf, uint32_t len)
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 return;
138 cpu_physical_memory_read(s->dmem_base + offset, buf, len);
141 static inline void softusb_write_dmem(MilkymistSoftUsbState *s,
142 uint32_t offset, uint8_t *buf, uint32_t len)
144 if (offset + len >= s->dmem_size) {
145 error_report("milkymist_softusb: write dmem out of bounds "
146 "at offset 0x%x, len %d", offset, len);
147 return;
150 cpu_physical_memory_write(s->dmem_base + offset, buf, len);
153 static inline void softusb_read_pmem(MilkymistSoftUsbState *s,
154 uint32_t offset, uint8_t *buf, uint32_t len)
156 if (offset + len >= s->pmem_size) {
157 error_report("milkymist_softusb: read pmem out of bounds "
158 "at offset 0x%x, len %d", offset, len);
159 return;
162 cpu_physical_memory_read(s->pmem_base + offset, buf, len);
165 static inline void softusb_write_pmem(MilkymistSoftUsbState *s,
166 uint32_t offset, uint8_t *buf, uint32_t len)
168 if (offset + len >= s->pmem_size) {
169 error_report("milkymist_softusb: write pmem out of bounds "
170 "at offset 0x%x, len %d", offset, len);
171 return;
174 cpu_physical_memory_write(s->pmem_base + offset, buf, len);
177 static void softusb_mouse_changed(MilkymistSoftUsbState *s)
179 uint8_t m;
180 uint8_t buf[4];
182 buf[0] = s->mouse_buttons_state;
183 buf[1] = s->mouse_dx;
184 buf[2] = s->mouse_dy;
185 buf[3] = s->mouse_dz;
187 softusb_read_dmem(s, COMLOC_MEVT_PRODUCE, &m, 1);
188 trace_milkymist_softusb_mevt(m);
189 softusb_write_dmem(s, COMLOC_MEVT_BASE + 4 * m, buf, 4);
190 m = (m + 1) & 0xf;
191 softusb_write_dmem(s, COMLOC_MEVT_PRODUCE, &m, 1);
193 trace_milkymist_softusb_pulse_irq();
194 qemu_irq_pulse(s->irq);
197 static void softusb_kbd_changed(MilkymistSoftUsbState *s)
199 uint8_t m;
201 softusb_read_dmem(s, COMLOC_KEVT_PRODUCE, &m, 1);
202 trace_milkymist_softusb_kevt(m);
203 softusb_write_dmem(s, COMLOC_KEVT_BASE + 8 * m, s->kbd_usb_buffer, 8);
204 m = (m + 1) & 0x7;
205 softusb_write_dmem(s, COMLOC_KEVT_PRODUCE, &m, 1);
207 trace_milkymist_softusb_pulse_irq();
208 qemu_irq_pulse(s->irq);
211 static void softusb_mouse_event(void *opaque,
212 int dx, int dy, int dz, int buttons_state)
214 MilkymistSoftUsbState *s = opaque;
216 /* if device is in reset, do nothing */
217 if (s->regs[R_CTRL] & CTRL_RESET) {
218 return;
221 trace_milkymist_softusb_mouse_event(dx, dy, dz, buttons_state);
223 s->mouse_dx = dx;
224 s->mouse_dy = dy;
225 s->mouse_dz = dz;
226 s->mouse_buttons_state = buttons_state;
228 softusb_mouse_changed(s);
231 static void softusb_usbdev_datain(void *opaque)
233 MilkymistSoftUsbState *s = opaque;
235 USBPacket p;
237 usb_packet_init(&p);
238 usb_packet_setup(&p, USB_TOKEN_IN, 0, 1);
239 usb_packet_addbuf(&p, s->kbd_usb_buffer, sizeof(s->kbd_usb_buffer));
240 s->usbdev->info->handle_data(s->usbdev, &p);
241 usb_packet_cleanup(&p);
243 softusb_kbd_changed(s);
246 static void softusb_attach(USBPort *port)
250 static void softusb_detach(USBPort *port)
254 static void softusb_child_detach(USBPort *port, USBDevice *child)
258 static USBPortOps softusb_ops = {
259 .attach = softusb_attach,
260 .detach = softusb_detach,
261 .child_detach = softusb_child_detach,
264 static USBBusOps softusb_bus_ops = {
267 static void milkymist_softusb_reset(DeviceState *d)
269 MilkymistSoftUsbState *s =
270 container_of(d, MilkymistSoftUsbState, busdev.qdev);
271 int i;
273 for (i = 0; i < R_MAX; i++) {
274 s->regs[i] = 0;
276 s->mouse_dx = 0;
277 s->mouse_dy = 0;
278 s->mouse_dz = 0;
279 s->mouse_buttons_state = 0;
280 memset(s->kbd_usb_buffer, 0, sizeof(s->kbd_usb_buffer));
282 /* defaults */
283 s->regs[R_CTRL] = CTRL_RESET;
286 static int milkymist_softusb_init(SysBusDevice *dev)
288 MilkymistSoftUsbState *s = FROM_SYSBUS(typeof(*s), dev);
289 int softusb_regs;
290 ram_addr_t pmem_ram;
291 ram_addr_t dmem_ram;
293 sysbus_init_irq(dev, &s->irq);
295 softusb_regs = cpu_register_io_memory(softusb_read_fn, softusb_write_fn, s,
296 DEVICE_NATIVE_ENDIAN);
297 sysbus_init_mmio(dev, R_MAX * 4, softusb_regs);
299 /* register pmem and dmem */
300 pmem_ram = qemu_ram_alloc(NULL, "milkymist_softusb.pmem", s->pmem_size);
301 cpu_register_physical_memory(s->pmem_base, s->pmem_size,
302 pmem_ram | IO_MEM_RAM);
303 dmem_ram = qemu_ram_alloc(NULL, "milkymist_softusb.dmem", s->dmem_size);
304 cpu_register_physical_memory(s->dmem_base, s->dmem_size,
305 dmem_ram | IO_MEM_RAM);
307 qemu_add_mouse_event_handler(softusb_mouse_event, s, 0, "Milkymist Mouse");
309 /* create our usb bus */
310 usb_bus_new(&s->usbbus, &softusb_bus_ops, NULL);
312 /* our two ports */
313 /* FIXME: claim to support full speed devices. qemu mouse and keyboard
314 * report themselves as full speed devices. */
315 usb_register_port(&s->usbbus, &s->usbport[0], NULL, 0, &softusb_ops,
316 USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL);
317 usb_register_port(&s->usbbus, &s->usbport[1], NULL, 1, &softusb_ops,
318 USB_SPEED_MASK_LOW | USB_SPEED_MASK_FULL);
320 /* and finally create an usb keyboard */
321 s->usbdev = usb_create_simple(&s->usbbus, "usb-kbd");
322 usb_hid_datain_cb(s->usbdev, s, softusb_usbdev_datain);
323 s->usbdev->info->handle_reset(s->usbdev);
325 return 0;
328 static const VMStateDescription vmstate_milkymist_softusb = {
329 .name = "milkymist-softusb",
330 .version_id = 1,
331 .minimum_version_id = 1,
332 .minimum_version_id_old = 1,
333 .fields = (VMStateField[]) {
334 VMSTATE_UINT32_ARRAY(regs, MilkymistSoftUsbState, R_MAX),
335 VMSTATE_INT32(mouse_dx, MilkymistSoftUsbState),
336 VMSTATE_INT32(mouse_dy, MilkymistSoftUsbState),
337 VMSTATE_INT32(mouse_dz, MilkymistSoftUsbState),
338 VMSTATE_UINT8(mouse_buttons_state, MilkymistSoftUsbState),
339 VMSTATE_BUFFER(kbd_usb_buffer, MilkymistSoftUsbState),
340 VMSTATE_END_OF_LIST()
344 static SysBusDeviceInfo milkymist_softusb_info = {
345 .init = milkymist_softusb_init,
346 .qdev.name = "milkymist-softusb",
347 .qdev.size = sizeof(MilkymistSoftUsbState),
348 .qdev.vmsd = &vmstate_milkymist_softusb,
349 .qdev.reset = milkymist_softusb_reset,
350 .qdev.props = (Property[]) {
351 DEFINE_PROP_UINT32(
352 "pmem_base", MilkymistSoftUsbState, pmem_base, 0xa0000000
354 DEFINE_PROP_UINT32(
355 "pmem_size", MilkymistSoftUsbState, pmem_size, 0x00001000
357 DEFINE_PROP_UINT32(
358 "dmem_base", MilkymistSoftUsbState, dmem_base, 0xa0020000
360 DEFINE_PROP_UINT32(
361 "dmem_size", MilkymistSoftUsbState, dmem_size, 0x00002000
363 DEFINE_PROP_END_OF_LIST(),
367 static void milkymist_softusb_register(void)
369 sysbus_register_withprop(&milkymist_softusb_info);
372 device_init(milkymist_softusb_register)