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/input/ps2.h"
28 #include "hw/input/lasips2.h"
29 #include "exec/hwaddr.h"
31 #include "exec/address-spaces.h"
32 #include "migration/vmstate.h"
37 typedef struct LASIPS2Port
{
38 struct LASIPS2State
*parent
;
48 typedef struct LASIPS2State
{
54 static const VMStateDescription vmstate_lasips2
= {
57 .minimum_version_id
= 0,
58 .fields
= (VMStateField
[]) {
59 VMSTATE_UINT8(kbd
.control
, LASIPS2State
),
60 VMSTATE_UINT8(kbd
.id
, LASIPS2State
),
61 VMSTATE_BOOL(kbd
.irq
, LASIPS2State
),
62 VMSTATE_UINT8(mouse
.control
, LASIPS2State
),
63 VMSTATE_UINT8(mouse
.id
, LASIPS2State
),
64 VMSTATE_BOOL(mouse
.irq
, LASIPS2State
),
79 } lasips2_write_reg_t
;
82 LASIPS2_CONTROL_ENABLE
= 0x01,
83 LASIPS2_CONTROL_LOOPBACK
= 0x02,
84 LASIPS2_CONTROL_DIAG
= 0x20,
85 LASIPS2_CONTROL_DATDIR
= 0x40,
86 LASIPS2_CONTROL_CLKDIR
= 0x80,
87 } lasips2_control_reg_t
;
90 LASIPS2_STATUS_RBNE
= 0x01,
91 LASIPS2_STATUS_TBNE
= 0x02,
92 LASIPS2_STATUS_TERR
= 0x04,
93 LASIPS2_STATUS_PERR
= 0x08,
94 LASIPS2_STATUS_CMPINTR
= 0x10,
95 LASIPS2_STATUS_DATSHD
= 0x40,
96 LASIPS2_STATUS_CLKSHD
= 0x80,
97 } lasips2_status_reg_t
;
99 static const char *lasips2_read_reg_name(uint64_t addr
)
101 switch (addr
& 0xc) {
105 case REG_PS2_RCVDATA
:
106 return " PS2_RCVDATA";
108 case REG_PS2_CONTROL
:
109 return " PS2_CONTROL";
112 return " PS2_STATUS";
119 static const char *lasips2_write_reg_name(uint64_t addr
)
121 switch (addr
& 0x0c) {
125 case REG_PS2_XMTDATA
:
126 return " PS2_XMTDATA";
128 case REG_PS2_CONTROL
:
129 return " PS2_CONTROL";
136 static void lasips2_update_irq(LASIPS2State
*s
)
138 trace_lasips2_intr(s
->kbd
.irq
| s
->mouse
.irq
);
139 qemu_set_irq(s
->irq
, s
->kbd
.irq
| s
->mouse
.irq
);
142 static void lasips2_reg_write(void *opaque
, hwaddr addr
, uint64_t val
,
145 LASIPS2Port
*port
= opaque
;
147 trace_lasips2_reg_write(size
, port
->id
, addr
,
148 lasips2_write_reg_name(addr
), val
);
150 switch (addr
& 0xc) {
151 case REG_PS2_CONTROL
:
155 case REG_PS2_XMTDATA
:
156 if (port
->control
& LASIPS2_CONTROL_LOOPBACK
) {
159 port
->loopback_rbne
= true;
160 lasips2_update_irq(port
->parent
);
165 ps2_write_mouse(port
->dev
, val
);
167 ps2_write_keyboard(port
->dev
, val
);
175 qemu_log_mask(LOG_UNIMP
, "%s: unknown register 0x%02" HWADDR_PRIx
"\n",
181 static uint64_t lasips2_reg_read(void *opaque
, hwaddr addr
, unsigned size
)
183 LASIPS2Port
*port
= opaque
;
186 switch (addr
& 0xc) {
191 case REG_PS2_RCVDATA
:
192 if (port
->control
& LASIPS2_CONTROL_LOOPBACK
) {
194 port
->loopback_rbne
= false;
195 lasips2_update_irq(port
->parent
);
200 ret
= ps2_read_data(port
->dev
);
203 case REG_PS2_CONTROL
:
209 ret
= LASIPS2_STATUS_DATSHD
| LASIPS2_STATUS_CLKSHD
;
211 if (port
->control
& LASIPS2_CONTROL_DIAG
) {
212 if (!(port
->control
& LASIPS2_CONTROL_DATDIR
)) {
213 ret
&= ~LASIPS2_STATUS_DATSHD
;
216 if (!(port
->control
& LASIPS2_CONTROL_CLKDIR
)) {
217 ret
&= ~LASIPS2_STATUS_CLKSHD
;
221 if (port
->control
& LASIPS2_CONTROL_LOOPBACK
) {
222 if (port
->loopback_rbne
) {
223 ret
|= LASIPS2_STATUS_RBNE
;
226 if (!ps2_queue_empty(port
->dev
)) {
227 ret
|= LASIPS2_STATUS_RBNE
;
231 if (port
->parent
->kbd
.irq
|| port
->parent
->mouse
.irq
) {
232 ret
|= LASIPS2_STATUS_CMPINTR
;
237 qemu_log_mask(LOG_UNIMP
, "%s: unknown register 0x%02" HWADDR_PRIx
"\n",
241 trace_lasips2_reg_read(size
, port
->id
, addr
,
242 lasips2_read_reg_name(addr
), ret
);
247 static const MemoryRegionOps lasips2_reg_ops
= {
248 .read
= lasips2_reg_read
,
249 .write
= lasips2_reg_write
,
251 .min_access_size
= 1,
252 .max_access_size
= 4,
254 .endianness
= DEVICE_NATIVE_ENDIAN
,
257 static void ps2dev_update_irq(void *opaque
, int level
)
259 LASIPS2Port
*port
= opaque
;
261 lasips2_update_irq(port
->parent
);
264 void lasips2_init(MemoryRegion
*address_space
,
265 hwaddr base
, qemu_irq irq
)
269 s
= g_malloc0(sizeof(LASIPS2State
));
276 vmstate_register(NULL
, base
, &vmstate_lasips2
, s
);
278 s
->kbd
.dev
= ps2_kbd_init(ps2dev_update_irq
, &s
->kbd
);
279 s
->mouse
.dev
= ps2_mouse_init(ps2dev_update_irq
, &s
->mouse
);
281 memory_region_init_io(&s
->kbd
.reg
, NULL
, &lasips2_reg_ops
, &s
->kbd
,
282 "lasips2-kbd", 0x100);
283 memory_region_add_subregion(address_space
, base
, &s
->kbd
.reg
);
285 memory_region_init_io(&s
->mouse
.reg
, NULL
, &lasips2_reg_ops
, &s
->mouse
,
286 "lasips2-mouse", 0x100);
287 memory_region_add_subregion(address_space
, base
+ 0x100, &s
->mouse
.reg
);