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"
28 #include "hw/input/ps2.h"
29 #include "hw/input/lasips2.h"
30 #include "hw/sysbus.h"
31 #include "exec/hwaddr.h"
32 #include "sysemu/sysemu.h"
34 #include "exec/address-spaces.h"
35 #include "migration/vmstate.h"
40 typedef struct LASIPS2Port
{
41 struct LASIPS2State
*parent
;
51 typedef struct LASIPS2State
{
57 static const VMStateDescription vmstate_lasips2
= {
60 .minimum_version_id
= 0,
61 .fields
= (VMStateField
[]) {
62 VMSTATE_UINT8(kbd
.control
, LASIPS2State
),
63 VMSTATE_UINT8(kbd
.id
, LASIPS2State
),
64 VMSTATE_BOOL(kbd
.irq
, LASIPS2State
),
65 VMSTATE_UINT8(mouse
.control
, LASIPS2State
),
66 VMSTATE_UINT8(mouse
.id
, LASIPS2State
),
67 VMSTATE_BOOL(mouse
.irq
, LASIPS2State
),
82 } lasips2_write_reg_t
;
85 LASIPS2_CONTROL_ENABLE
= 0x01,
86 LASIPS2_CONTROL_LOOPBACK
= 0x02,
87 LASIPS2_CONTROL_DIAG
= 0x20,
88 LASIPS2_CONTROL_DATDIR
= 0x40,
89 LASIPS2_CONTROL_CLKDIR
= 0x80,
90 } lasips2_control_reg_t
;
93 LASIPS2_STATUS_RBNE
= 0x01,
94 LASIPS2_STATUS_TBNE
= 0x02,
95 LASIPS2_STATUS_TERR
= 0x04,
96 LASIPS2_STATUS_PERR
= 0x08,
97 LASIPS2_STATUS_CMPINTR
= 0x10,
98 LASIPS2_STATUS_DATSHD
= 0x40,
99 LASIPS2_STATUS_CLKSHD
= 0x80,
100 } lasips2_status_reg_t
;
102 static const char *artist_read_reg_name(uint64_t addr
)
104 switch (addr
& 0xc) {
108 case REG_PS2_RCVDATA
:
109 return " PS2_RCVDATA";
111 case REG_PS2_CONTROL
:
112 return " PS2_CONTROL";
115 return " PS2_STATUS";
122 static const char *artist_write_reg_name(uint64_t addr
)
124 switch (addr
& 0x0c) {
128 case REG_PS2_XMTDATA
:
129 return " PS2_XMTDATA";
131 case REG_PS2_CONTROL
:
132 return " PS2_CONTROL";
139 static void lasips2_update_irq(LASIPS2State
*s
)
141 trace_lasips2_intr(s
->kbd
.irq
| s
->mouse
.irq
);
142 qemu_set_irq(s
->irq
, s
->kbd
.irq
| s
->mouse
.irq
);
145 static void lasips2_reg_write(void *opaque
, hwaddr addr
, uint64_t val
,
148 LASIPS2Port
*port
= opaque
;
150 trace_lasips2_reg_write(size
, port
->id
, addr
,
151 artist_write_reg_name(addr
), val
);
153 switch (addr
& 0xc) {
154 case REG_PS2_CONTROL
:
158 case REG_PS2_XMTDATA
:
159 if (port
->control
& LASIPS2_CONTROL_LOOPBACK
) {
162 port
->loopback_rbne
= true;
163 lasips2_update_irq(port
->parent
);
168 ps2_write_mouse(port
->dev
, val
);
170 ps2_write_keyboard(port
->dev
, val
);
178 qemu_log_mask(LOG_UNIMP
, "%s: unknown register 0x%02" HWADDR_PRIx
"\n",
184 static uint64_t lasips2_reg_read(void *opaque
, hwaddr addr
, unsigned size
)
186 LASIPS2Port
*port
= opaque
;
189 switch (addr
& 0xc) {
194 case REG_PS2_RCVDATA
:
195 if (port
->control
& LASIPS2_CONTROL_LOOPBACK
) {
197 port
->loopback_rbne
= false;
198 lasips2_update_irq(port
->parent
);
203 ret
= ps2_read_data(port
->dev
);
206 case REG_PS2_CONTROL
:
212 ret
= LASIPS2_STATUS_DATSHD
| LASIPS2_STATUS_CLKSHD
;
214 if (port
->control
& LASIPS2_CONTROL_DIAG
) {
215 if (!(port
->control
& LASIPS2_CONTROL_DATDIR
)) {
216 ret
&= ~LASIPS2_STATUS_DATSHD
;
219 if (!(port
->control
& LASIPS2_CONTROL_CLKDIR
)) {
220 ret
&= ~LASIPS2_STATUS_CLKSHD
;
224 if (port
->control
& LASIPS2_CONTROL_LOOPBACK
) {
225 if (port
->loopback_rbne
) {
226 ret
|= LASIPS2_STATUS_RBNE
;
229 if (!ps2_queue_empty(port
->dev
)) {
230 ret
|= LASIPS2_STATUS_RBNE
;
234 if (port
->parent
->kbd
.irq
|| port
->parent
->mouse
.irq
) {
235 ret
|= LASIPS2_STATUS_CMPINTR
;
240 qemu_log_mask(LOG_UNIMP
, "%s: unknown register 0x%02" HWADDR_PRIx
"\n",
244 trace_lasips2_reg_read(size
, port
->id
, addr
,
245 artist_read_reg_name(addr
), ret
);
250 static const MemoryRegionOps lasips2_reg_ops
= {
251 .read
= lasips2_reg_read
,
252 .write
= lasips2_reg_write
,
254 .min_access_size
= 1,
255 .max_access_size
= 4,
257 .endianness
= DEVICE_NATIVE_ENDIAN
,
260 static void ps2dev_update_irq(void *opaque
, int level
)
262 LASIPS2Port
*port
= opaque
;
264 lasips2_update_irq(port
->parent
);
267 void lasips2_init(MemoryRegion
*address_space
,
268 hwaddr base
, qemu_irq irq
)
272 s
= g_malloc0(sizeof(LASIPS2State
));
279 vmstate_register(NULL
, base
, &vmstate_lasips2
, s
);
281 s
->kbd
.dev
= ps2_kbd_init(ps2dev_update_irq
, &s
->kbd
);
282 s
->mouse
.dev
= ps2_mouse_init(ps2dev_update_irq
, &s
->mouse
);
284 memory_region_init_io(&s
->kbd
.reg
, NULL
, &lasips2_reg_ops
, &s
->kbd
,
285 "lasips2-kbd", 0x100);
286 memory_region_add_subregion(address_space
, base
, &s
->kbd
.reg
);
288 memory_region_init_io(&s
->mouse
.reg
, NULL
, &lasips2_reg_ops
, &s
->mouse
,
289 "lasips2-mouse", 0x100);
290 memory_region_add_subregion(address_space
, base
+ 0x100, &s
->mouse
.reg
);