hw: m25p80: add tests for write protect (WP# and SRWD bit)
[qemu.git] / hw / input / lasips2.c
blob9223cb0af407b00d733080627108941cbb1725a2
1 /*
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
22 * THE SOFTWARE.
24 #include "qemu/osdep.h"
25 #include "qemu/log.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"
31 #include "trace.h"
32 #include "exec/address-spaces.h"
33 #include "migration/vmstate.h"
34 #include "hw/irq.h"
35 #include "qapi/error.h"
38 static const VMStateDescription vmstate_lasips2 = {
39 .name = "lasips2",
40 .version_id = 0,
41 .minimum_version_id = 0,
42 .fields = (VMStateField[]) {
43 VMSTATE_UINT8(kbd.control, LASIPS2State),
44 VMSTATE_UINT8(kbd.id, LASIPS2State),
45 VMSTATE_BOOL(kbd.irq, LASIPS2State),
46 VMSTATE_UINT8(mouse.control, LASIPS2State),
47 VMSTATE_UINT8(mouse.id, LASIPS2State),
48 VMSTATE_BOOL(mouse.irq, LASIPS2State),
49 VMSTATE_END_OF_LIST()
53 typedef enum {
54 REG_PS2_ID = 0,
55 REG_PS2_RCVDATA = 4,
56 REG_PS2_CONTROL = 8,
57 REG_PS2_STATUS = 12,
58 } lasips2_read_reg_t;
60 typedef enum {
61 REG_PS2_RESET = 0,
62 REG_PS2_XMTDATA = 4,
63 } lasips2_write_reg_t;
65 typedef enum {
66 LASIPS2_CONTROL_ENABLE = 0x01,
67 LASIPS2_CONTROL_LOOPBACK = 0x02,
68 LASIPS2_CONTROL_DIAG = 0x20,
69 LASIPS2_CONTROL_DATDIR = 0x40,
70 LASIPS2_CONTROL_CLKDIR = 0x80,
71 } lasips2_control_reg_t;
73 typedef enum {
74 LASIPS2_STATUS_RBNE = 0x01,
75 LASIPS2_STATUS_TBNE = 0x02,
76 LASIPS2_STATUS_TERR = 0x04,
77 LASIPS2_STATUS_PERR = 0x08,
78 LASIPS2_STATUS_CMPINTR = 0x10,
79 LASIPS2_STATUS_DATSHD = 0x40,
80 LASIPS2_STATUS_CLKSHD = 0x80,
81 } lasips2_status_reg_t;
83 static const char *lasips2_read_reg_name(uint64_t addr)
85 switch (addr & 0xc) {
86 case REG_PS2_ID:
87 return " PS2_ID";
89 case REG_PS2_RCVDATA:
90 return " PS2_RCVDATA";
92 case REG_PS2_CONTROL:
93 return " PS2_CONTROL";
95 case REG_PS2_STATUS:
96 return " PS2_STATUS";
98 default:
99 return "";
103 static const char *lasips2_write_reg_name(uint64_t addr)
105 switch (addr & 0x0c) {
106 case REG_PS2_RESET:
107 return " PS2_RESET";
109 case REG_PS2_XMTDATA:
110 return " PS2_XMTDATA";
112 case REG_PS2_CONTROL:
113 return " PS2_CONTROL";
115 default:
116 return "";
120 static void lasips2_update_irq(LASIPS2State *s)
122 trace_lasips2_intr(s->kbd.irq | s->mouse.irq);
123 qemu_set_irq(s->irq, s->kbd.irq | s->mouse.irq);
126 static void lasips2_reg_write(void *opaque, hwaddr addr, uint64_t val,
127 unsigned size)
129 LASIPS2Port *port = opaque;
131 trace_lasips2_reg_write(size, port->id, addr,
132 lasips2_write_reg_name(addr), val);
134 switch (addr & 0xc) {
135 case REG_PS2_CONTROL:
136 port->control = val;
137 break;
139 case REG_PS2_XMTDATA:
140 if (port->control & LASIPS2_CONTROL_LOOPBACK) {
141 port->buf = val;
142 port->irq = true;
143 port->loopback_rbne = true;
144 lasips2_update_irq(port->parent);
145 break;
148 if (port->id) {
149 ps2_write_mouse(port->dev, val);
150 } else {
151 ps2_write_keyboard(port->dev, val);
153 break;
155 case REG_PS2_RESET:
156 break;
158 default:
159 qemu_log_mask(LOG_UNIMP, "%s: unknown register 0x%02" HWADDR_PRIx "\n",
160 __func__, addr);
161 break;
165 static uint64_t lasips2_reg_read(void *opaque, hwaddr addr, unsigned size)
167 LASIPS2Port *port = opaque;
168 uint64_t ret = 0;
170 switch (addr & 0xc) {
171 case REG_PS2_ID:
172 ret = port->id;
173 break;
175 case REG_PS2_RCVDATA:
176 if (port->control & LASIPS2_CONTROL_LOOPBACK) {
177 port->irq = false;
178 port->loopback_rbne = false;
179 lasips2_update_irq(port->parent);
180 ret = port->buf;
181 break;
184 ret = ps2_read_data(port->dev);
185 break;
187 case REG_PS2_CONTROL:
188 ret = port->control;
189 break;
191 case REG_PS2_STATUS:
192 ret = LASIPS2_STATUS_DATSHD | LASIPS2_STATUS_CLKSHD;
194 if (port->control & LASIPS2_CONTROL_DIAG) {
195 if (!(port->control & LASIPS2_CONTROL_DATDIR)) {
196 ret &= ~LASIPS2_STATUS_DATSHD;
199 if (!(port->control & LASIPS2_CONTROL_CLKDIR)) {
200 ret &= ~LASIPS2_STATUS_CLKSHD;
204 if (port->control & LASIPS2_CONTROL_LOOPBACK) {
205 if (port->loopback_rbne) {
206 ret |= LASIPS2_STATUS_RBNE;
208 } else {
209 if (!ps2_queue_empty(port->dev)) {
210 ret |= LASIPS2_STATUS_RBNE;
214 if (port->parent->kbd.irq || port->parent->mouse.irq) {
215 ret |= LASIPS2_STATUS_CMPINTR;
217 break;
219 default:
220 qemu_log_mask(LOG_UNIMP, "%s: unknown register 0x%02" HWADDR_PRIx "\n",
221 __func__, addr);
222 break;
225 trace_lasips2_reg_read(size, port->id, addr,
226 lasips2_read_reg_name(addr), ret);
227 return ret;
230 static const MemoryRegionOps lasips2_reg_ops = {
231 .read = lasips2_reg_read,
232 .write = lasips2_reg_write,
233 .impl = {
234 .min_access_size = 1,
235 .max_access_size = 4,
237 .endianness = DEVICE_NATIVE_ENDIAN,
240 static void lasips2_set_kbd_irq(void *opaque, int n, int level)
242 LASIPS2State *s = LASIPS2(opaque);
243 LASIPS2Port *port = &s->kbd;
245 port->irq = level;
246 lasips2_update_irq(port->parent);
249 static void lasips2_set_mouse_irq(void *opaque, int n, int level)
251 LASIPS2State *s = LASIPS2(opaque);
252 LASIPS2Port *port = &s->mouse;
254 port->irq = level;
255 lasips2_update_irq(port->parent);
258 LASIPS2State *lasips2_initfn(hwaddr base, qemu_irq irq)
260 DeviceState *dev;
262 dev = qdev_new(TYPE_LASIPS2);
263 qdev_prop_set_uint64(dev, "base", base);
264 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
266 sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, irq);
268 return LASIPS2(dev);
271 static void lasips2_realize(DeviceState *dev, Error **errp)
273 LASIPS2State *s = LASIPS2(dev);
275 vmstate_register(NULL, s->base, &vmstate_lasips2, s);
277 s->kbd.dev = ps2_kbd_init();
278 qdev_connect_gpio_out(DEVICE(s->kbd.dev), PS2_DEVICE_IRQ,
279 qdev_get_gpio_in_named(dev, "ps2-kbd-input-irq",
280 0));
281 s->mouse.dev = ps2_mouse_init();
282 qdev_connect_gpio_out(DEVICE(s->mouse.dev), PS2_DEVICE_IRQ,
283 qdev_get_gpio_in_named(dev, "ps2-mouse-input-irq",
284 0));
287 static void lasips2_init(Object *obj)
289 LASIPS2State *s = LASIPS2(obj);
291 s->kbd.id = 0;
292 s->mouse.id = 1;
293 s->kbd.parent = s;
294 s->mouse.parent = s;
296 memory_region_init_io(&s->kbd.reg, obj, &lasips2_reg_ops, &s->kbd,
297 "lasips2-kbd", 0x100);
298 memory_region_init_io(&s->mouse.reg, obj, &lasips2_reg_ops, &s->mouse,
299 "lasips2-mouse", 0x100);
301 sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->kbd.reg);
302 sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mouse.reg);
304 sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->irq);
306 qdev_init_gpio_in_named(DEVICE(obj), lasips2_set_kbd_irq,
307 "ps2-kbd-input-irq", 1);
308 qdev_init_gpio_in_named(DEVICE(obj), lasips2_set_mouse_irq,
309 "ps2-mouse-input-irq", 1);
312 static Property lasips2_properties[] = {
313 DEFINE_PROP_UINT64("base", LASIPS2State, base, UINT64_MAX),
314 DEFINE_PROP_END_OF_LIST(),
317 static void lasips2_class_init(ObjectClass *klass, void *data)
319 DeviceClass *dc = DEVICE_CLASS(klass);
321 dc->realize = lasips2_realize;
322 device_class_set_props(dc, lasips2_properties);
323 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
326 static const TypeInfo lasips2_info = {
327 .name = TYPE_LASIPS2,
328 .parent = TYPE_SYS_BUS_DEVICE,
329 .instance_init = lasips2_init,
330 .instance_size = sizeof(LASIPS2State),
331 .class_init = lasips2_class_init,
334 static void lasips2_register_types(void)
336 type_register_static(&lasips2_info);
339 type_init(lasips2_register_types)