2 * QEMU HP Lasi PS/2 interface emulation
4 * Copyright (c) 2019 Sven Schnelle
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu/osdep.h"
26 #include "hw/qdev-properties.h"
27 #include "hw/sysbus.h"
28 #include "hw/input/ps2.h"
29 #include "hw/input/lasips2.h"
30 #include "exec/hwaddr.h"
32 #include "exec/address-spaces.h"
33 #include "migration/vmstate.h"
35 #include "qapi/error.h"
38 static const VMStateDescription vmstate_lasips2
= {
41 .minimum_version_id
= 0,
42 .fields
= (VMStateField
[]) {
43 VMSTATE_UINT8(kbd_port
.parent_obj
.control
, LASIPS2State
),
44 VMSTATE_UINT8(kbd_port
.parent_obj
.id
, LASIPS2State
),
45 VMSTATE_UINT8(mouse_port
.parent_obj
.control
, LASIPS2State
),
46 VMSTATE_UINT8(mouse_port
.parent_obj
.id
, LASIPS2State
),
61 } lasips2_write_reg_t
;
64 LASIPS2_CONTROL_ENABLE
= 0x01,
65 LASIPS2_CONTROL_LOOPBACK
= 0x02,
66 LASIPS2_CONTROL_DIAG
= 0x20,
67 LASIPS2_CONTROL_DATDIR
= 0x40,
68 LASIPS2_CONTROL_CLKDIR
= 0x80,
69 } lasips2_control_reg_t
;
72 LASIPS2_STATUS_RBNE
= 0x01,
73 LASIPS2_STATUS_TBNE
= 0x02,
74 LASIPS2_STATUS_TERR
= 0x04,
75 LASIPS2_STATUS_PERR
= 0x08,
76 LASIPS2_STATUS_CMPINTR
= 0x10,
77 LASIPS2_STATUS_DATSHD
= 0x40,
78 LASIPS2_STATUS_CLKSHD
= 0x80,
79 } lasips2_status_reg_t
;
81 static const char *lasips2_read_reg_name(uint64_t addr
)
88 return " PS2_RCVDATA";
91 return " PS2_CONTROL";
101 static const char *lasips2_write_reg_name(uint64_t addr
)
103 switch (addr
& 0x0c) {
107 case REG_PS2_XMTDATA
:
108 return " PS2_XMTDATA";
110 case REG_PS2_CONTROL
:
111 return " PS2_CONTROL";
118 static void lasips2_update_irq(LASIPS2State
*s
)
120 int level
= s
->int_status
? 1 : 0;
122 trace_lasips2_intr(level
);
123 qemu_set_irq(s
->irq
, level
);
126 static void lasips2_set_irq(void *opaque
, int n
, int level
)
128 LASIPS2State
*s
= LASIPS2(opaque
);
131 s
->int_status
|= BIT(n
);
133 s
->int_status
&= ~BIT(n
);
136 lasips2_update_irq(s
);
139 static void lasips2_reg_write(void *opaque
, hwaddr addr
, uint64_t val
,
142 LASIPS2Port
*lp
= LASIPS2_PORT(opaque
);
144 trace_lasips2_reg_write(size
, lp
->id
, addr
,
145 lasips2_write_reg_name(addr
), val
);
147 switch (addr
& 0xc) {
148 case REG_PS2_CONTROL
:
152 case REG_PS2_XMTDATA
:
153 if (lp
->control
& LASIPS2_CONTROL_LOOPBACK
) {
155 lp
->loopback_rbne
= true;
156 qemu_set_irq(lp
->irq
, 1);
161 ps2_write_mouse(PS2_MOUSE_DEVICE(lp
->ps2dev
), val
);
163 ps2_write_keyboard(PS2_KBD_DEVICE(lp
->ps2dev
), val
);
171 qemu_log_mask(LOG_UNIMP
, "%s: unknown register 0x%02" HWADDR_PRIx
"\n",
177 static uint64_t lasips2_reg_read(void *opaque
, hwaddr addr
, unsigned size
)
179 LASIPS2Port
*lp
= LASIPS2_PORT(opaque
);
182 switch (addr
& 0xc) {
187 case REG_PS2_RCVDATA
:
188 if (lp
->control
& LASIPS2_CONTROL_LOOPBACK
) {
189 lp
->loopback_rbne
= false;
190 qemu_set_irq(lp
->irq
, 0);
195 ret
= ps2_read_data(lp
->ps2dev
);
198 case REG_PS2_CONTROL
:
203 ret
= LASIPS2_STATUS_DATSHD
| LASIPS2_STATUS_CLKSHD
;
205 if (lp
->control
& LASIPS2_CONTROL_DIAG
) {
206 if (!(lp
->control
& LASIPS2_CONTROL_DATDIR
)) {
207 ret
&= ~LASIPS2_STATUS_DATSHD
;
210 if (!(lp
->control
& LASIPS2_CONTROL_CLKDIR
)) {
211 ret
&= ~LASIPS2_STATUS_CLKSHD
;
215 if (lp
->control
& LASIPS2_CONTROL_LOOPBACK
) {
216 if (lp
->loopback_rbne
) {
217 ret
|= LASIPS2_STATUS_RBNE
;
220 if (!ps2_queue_empty(lp
->ps2dev
)) {
221 ret
|= LASIPS2_STATUS_RBNE
;
225 if (lp
->lasips2
->int_status
) {
226 ret
|= LASIPS2_STATUS_CMPINTR
;
231 qemu_log_mask(LOG_UNIMP
, "%s: unknown register 0x%02" HWADDR_PRIx
"\n",
236 trace_lasips2_reg_read(size
, lp
->id
, addr
,
237 lasips2_read_reg_name(addr
), ret
);
241 static const MemoryRegionOps lasips2_reg_ops
= {
242 .read
= lasips2_reg_read
,
243 .write
= lasips2_reg_write
,
245 .min_access_size
= 1,
246 .max_access_size
= 4,
248 .endianness
= DEVICE_BIG_ENDIAN
,
251 static void lasips2_realize(DeviceState
*dev
, Error
**errp
)
253 LASIPS2State
*s
= LASIPS2(dev
);
256 lp
= LASIPS2_PORT(&s
->kbd_port
);
257 if (!(qdev_realize(DEVICE(lp
), NULL
, errp
))) {
261 qdev_connect_gpio_out(DEVICE(lp
), 0,
262 qdev_get_gpio_in_named(dev
, "lasips2-port-input-irq",
265 lp
= LASIPS2_PORT(&s
->mouse_port
);
266 if (!(qdev_realize(DEVICE(lp
), NULL
, errp
))) {
270 qdev_connect_gpio_out(DEVICE(lp
), 0,
271 qdev_get_gpio_in_named(dev
, "lasips2-port-input-irq",
275 static void lasips2_init(Object
*obj
)
277 LASIPS2State
*s
= LASIPS2(obj
);
280 object_initialize_child(obj
, "lasips2-kbd-port", &s
->kbd_port
,
281 TYPE_LASIPS2_KBD_PORT
);
282 object_initialize_child(obj
, "lasips2-mouse-port", &s
->mouse_port
,
283 TYPE_LASIPS2_MOUSE_PORT
);
285 lp
= LASIPS2_PORT(&s
->kbd_port
);
286 sysbus_init_mmio(SYS_BUS_DEVICE(obj
), &lp
->reg
);
287 lp
= LASIPS2_PORT(&s
->mouse_port
);
288 sysbus_init_mmio(SYS_BUS_DEVICE(obj
), &lp
->reg
);
290 sysbus_init_irq(SYS_BUS_DEVICE(obj
), &s
->irq
);
292 qdev_init_gpio_in_named(DEVICE(obj
), lasips2_set_irq
,
293 "lasips2-port-input-irq", 2);
296 static void lasips2_class_init(ObjectClass
*klass
, void *data
)
298 DeviceClass
*dc
= DEVICE_CLASS(klass
);
300 dc
->realize
= lasips2_realize
;
301 dc
->vmsd
= &vmstate_lasips2
;
302 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
305 static const TypeInfo lasips2_info
= {
306 .name
= TYPE_LASIPS2
,
307 .parent
= TYPE_SYS_BUS_DEVICE
,
308 .instance_init
= lasips2_init
,
309 .instance_size
= sizeof(LASIPS2State
),
310 .class_init
= lasips2_class_init
,
313 static void lasips2_port_set_irq(void *opaque
, int n
, int level
)
315 LASIPS2Port
*s
= LASIPS2_PORT(opaque
);
317 qemu_set_irq(s
->irq
, level
);
320 static void lasips2_port_realize(DeviceState
*dev
, Error
**errp
)
322 LASIPS2Port
*s
= LASIPS2_PORT(dev
);
324 qdev_connect_gpio_out(DEVICE(s
->ps2dev
), PS2_DEVICE_IRQ
,
325 qdev_get_gpio_in_named(dev
, "ps2-input-irq", 0));
328 static void lasips2_port_init(Object
*obj
)
330 LASIPS2Port
*s
= LASIPS2_PORT(obj
);
332 qdev_init_gpio_out(DEVICE(obj
), &s
->irq
, 1);
333 qdev_init_gpio_in_named(DEVICE(obj
), lasips2_port_set_irq
,
337 static void lasips2_port_class_init(ObjectClass
*klass
, void *data
)
339 DeviceClass
*dc
= DEVICE_CLASS(klass
);
341 dc
->realize
= lasips2_port_realize
;
344 static const TypeInfo lasips2_port_info
= {
345 .name
= TYPE_LASIPS2_PORT
,
346 .parent
= TYPE_DEVICE
,
347 .instance_init
= lasips2_port_init
,
348 .instance_size
= sizeof(LASIPS2Port
),
349 .class_init
= lasips2_port_class_init
,
350 .class_size
= sizeof(LASIPS2PortDeviceClass
),
354 static void lasips2_kbd_port_realize(DeviceState
*dev
, Error
**errp
)
356 LASIPS2KbdPort
*s
= LASIPS2_KBD_PORT(dev
);
357 LASIPS2Port
*lp
= LASIPS2_PORT(dev
);
358 LASIPS2PortDeviceClass
*lpdc
= LASIPS2_PORT_GET_CLASS(lp
);
360 if (!sysbus_realize(SYS_BUS_DEVICE(&s
->kbd
), errp
)) {
364 lp
->ps2dev
= PS2_DEVICE(&s
->kbd
);
365 lpdc
->parent_realize(dev
, errp
);
368 static void lasips2_kbd_port_init(Object
*obj
)
370 LASIPS2KbdPort
*s
= LASIPS2_KBD_PORT(obj
);
371 LASIPS2Port
*lp
= LASIPS2_PORT(obj
);
373 memory_region_init_io(&lp
->reg
, obj
, &lasips2_reg_ops
, lp
, "lasips2-kbd",
376 object_initialize_child(obj
, "kbd", &s
->kbd
, TYPE_PS2_KBD_DEVICE
);
379 lp
->lasips2
= container_of(s
, LASIPS2State
, kbd_port
);
382 static void lasips2_kbd_port_class_init(ObjectClass
*klass
, void *data
)
384 DeviceClass
*dc
= DEVICE_CLASS(klass
);
385 LASIPS2PortDeviceClass
*lpdc
= LASIPS2_PORT_CLASS(klass
);
387 device_class_set_parent_realize(dc
, lasips2_kbd_port_realize
,
388 &lpdc
->parent_realize
);
391 static const TypeInfo lasips2_kbd_port_info
= {
392 .name
= TYPE_LASIPS2_KBD_PORT
,
393 .parent
= TYPE_LASIPS2_PORT
,
394 .instance_size
= sizeof(LASIPS2KbdPort
),
395 .instance_init
= lasips2_kbd_port_init
,
396 .class_init
= lasips2_kbd_port_class_init
,
399 static void lasips2_mouse_port_realize(DeviceState
*dev
, Error
**errp
)
401 LASIPS2MousePort
*s
= LASIPS2_MOUSE_PORT(dev
);
402 LASIPS2Port
*lp
= LASIPS2_PORT(dev
);
403 LASIPS2PortDeviceClass
*lpdc
= LASIPS2_PORT_GET_CLASS(lp
);
405 if (!sysbus_realize(SYS_BUS_DEVICE(&s
->mouse
), errp
)) {
409 lp
->ps2dev
= PS2_DEVICE(&s
->mouse
);
410 lpdc
->parent_realize(dev
, errp
);
413 static void lasips2_mouse_port_init(Object
*obj
)
415 LASIPS2MousePort
*s
= LASIPS2_MOUSE_PORT(obj
);
416 LASIPS2Port
*lp
= LASIPS2_PORT(obj
);
418 memory_region_init_io(&lp
->reg
, obj
, &lasips2_reg_ops
, lp
, "lasips2-mouse",
421 object_initialize_child(obj
, "mouse", &s
->mouse
, TYPE_PS2_MOUSE_DEVICE
);
424 lp
->lasips2
= container_of(s
, LASIPS2State
, mouse_port
);
427 static void lasips2_mouse_port_class_init(ObjectClass
*klass
, void *data
)
429 DeviceClass
*dc
= DEVICE_CLASS(klass
);
430 LASIPS2PortDeviceClass
*lpdc
= LASIPS2_PORT_CLASS(klass
);
432 device_class_set_parent_realize(dc
, lasips2_mouse_port_realize
,
433 &lpdc
->parent_realize
);
436 static const TypeInfo lasips2_mouse_port_info
= {
437 .name
= TYPE_LASIPS2_MOUSE_PORT
,
438 .parent
= TYPE_LASIPS2_PORT
,
439 .instance_size
= sizeof(LASIPS2MousePort
),
440 .instance_init
= lasips2_mouse_port_init
,
441 .class_init
= lasips2_mouse_port_class_init
,
444 static void lasips2_register_types(void)
446 type_register_static(&lasips2_info
);
447 type_register_static(&lasips2_port_info
);
448 type_register_static(&lasips2_kbd_port_info
);
449 type_register_static(&lasips2_mouse_port_info
);
452 type_init(lasips2_register_types
)