1 /* 2 * QEMU HP Lasi PS/2 interface emulation 3 * 4 * Copyright (c) 2019 Sven Schnelle 5 * 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: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 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. 23 */ 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" 36 37 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_port.parent_obj.control, LASIPS2State), 44 VMSTATE_UINT8(kbd_port.parent_obj.id, LASIPS2State), 45 VMSTATE_BOOL(kbd_port.parent_obj.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() 50 } 51 }; 52 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; 59 60 typedef enum { 61 REG_PS2_RESET = 0, 62 REG_PS2_XMTDATA = 4, 63 } lasips2_write_reg_t; 64 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; 72 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; 82 83 static const char *lasips2_read_reg_name(uint64_t addr) 84 { 85 switch (addr & 0xc) { 86 case REG_PS2_ID: 87 return " PS2_ID"; 88 89 case REG_PS2_RCVDATA: 90 return " PS2_RCVDATA"; 91 92 case REG_PS2_CONTROL: 93 return " PS2_CONTROL"; 94 95 case REG_PS2_STATUS: 96 return " PS2_STATUS"; 97 98 default: 99 return ""; 100 } 101 } 102 103 static const char *lasips2_write_reg_name(uint64_t addr) 104 { 105 switch (addr & 0x0c) { 106 case REG_PS2_RESET: 107 return " PS2_RESET"; 108 109 case REG_PS2_XMTDATA: 110 return " PS2_XMTDATA"; 111 112 case REG_PS2_CONTROL: 113 return " PS2_CONTROL"; 114 115 default: 116 return ""; 117 } 118 } 119 120 static void lasips2_update_irq(LASIPS2State *s) 121 { 122 trace_lasips2_intr(s->kbd_port.parent_obj.irq | s->mouse.irq); 123 qemu_set_irq(s->irq, s->kbd_port.parent_obj.irq | s->mouse.irq); 124 } 125 126 static void lasips2_reg_write(void *opaque, hwaddr addr, uint64_t val, 127 unsigned size) 128 { 129 LASIPS2Port *port = opaque; 130 131 trace_lasips2_reg_write(size, port->id, addr, 132 lasips2_write_reg_name(addr), val); 133 134 switch (addr & 0xc) { 135 case REG_PS2_CONTROL: 136 port->control = val; 137 break; 138 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; 146 } 147 148 if (port->id) { 149 ps2_write_mouse(PS2_MOUSE_DEVICE(port->ps2dev), val); 150 } else { 151 ps2_write_keyboard(PS2_KBD_DEVICE(port->ps2dev), val); 152 } 153 break; 154 155 case REG_PS2_RESET: 156 break; 157 158 default: 159 qemu_log_mask(LOG_UNIMP, "%s: unknown register 0x%02" HWADDR_PRIx "\n", 160 __func__, addr); 161 break; 162 } 163 } 164 165 static uint64_t lasips2_reg_read(void *opaque, hwaddr addr, unsigned size) 166 { 167 LASIPS2Port *port = opaque; 168 uint64_t ret = 0; 169 170 switch (addr & 0xc) { 171 case REG_PS2_ID: 172 ret = port->id; 173 break; 174 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; 182 } 183 184 ret = ps2_read_data(port->ps2dev); 185 break; 186 187 case REG_PS2_CONTROL: 188 ret = port->control; 189 break; 190 191 case REG_PS2_STATUS: 192 ret = LASIPS2_STATUS_DATSHD | LASIPS2_STATUS_CLKSHD; 193 194 if (port->control & LASIPS2_CONTROL_DIAG) { 195 if (!(port->control & LASIPS2_CONTROL_DATDIR)) { 196 ret &= ~LASIPS2_STATUS_DATSHD; 197 } 198 199 if (!(port->control & LASIPS2_CONTROL_CLKDIR)) { 200 ret &= ~LASIPS2_STATUS_CLKSHD; 201 } 202 } 203 204 if (port->control & LASIPS2_CONTROL_LOOPBACK) { 205 if (port->loopback_rbne) { 206 ret |= LASIPS2_STATUS_RBNE; 207 } 208 } else { 209 if (!ps2_queue_empty(port->ps2dev)) { 210 ret |= LASIPS2_STATUS_RBNE; 211 } 212 } 213 214 if (port->parent->kbd_port.parent_obj.irq || port->parent->mouse.irq) { 215 ret |= LASIPS2_STATUS_CMPINTR; 216 } 217 break; 218 219 default: 220 qemu_log_mask(LOG_UNIMP, "%s: unknown register 0x%02" HWADDR_PRIx "\n", 221 __func__, addr); 222 break; 223 } 224 225 trace_lasips2_reg_read(size, port->id, addr, 226 lasips2_read_reg_name(addr), ret); 227 return ret; 228 } 229 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, 236 }, 237 .endianness = DEVICE_NATIVE_ENDIAN, 238 }; 239 240 static void lasips2_set_kbd_irq(void *opaque, int n, int level) 241 { 242 LASIPS2State *s = LASIPS2(opaque); 243 LASIPS2Port *port = LASIPS2_PORT(&s->kbd_port); 244 245 port->irq = level; 246 lasips2_update_irq(port->parent); 247 } 248 249 static void lasips2_set_mouse_irq(void *opaque, int n, int level) 250 { 251 LASIPS2State *s = LASIPS2(opaque); 252 LASIPS2Port *port = &s->mouse; 253 254 port->irq = level; 255 lasips2_update_irq(port->parent); 256 } 257 258 static void lasips2_realize(DeviceState *dev, Error **errp) 259 { 260 LASIPS2State *s = LASIPS2(dev); 261 LASIPS2Port *lp; 262 263 lp = LASIPS2_PORT(&s->kbd_port); 264 if (!(qdev_realize(DEVICE(lp), NULL, errp))) { 265 return; 266 } 267 268 lp->ps2dev = ps2_kbd_init(); 269 qdev_connect_gpio_out(DEVICE(lp->ps2dev), PS2_DEVICE_IRQ, 270 qdev_get_gpio_in_named(dev, "ps2-kbd-input-irq", 271 0)); 272 s->mouse.ps2dev = ps2_mouse_init(); 273 qdev_connect_gpio_out(DEVICE(s->mouse.ps2dev), PS2_DEVICE_IRQ, 274 qdev_get_gpio_in_named(dev, "ps2-mouse-input-irq", 275 0)); 276 } 277 278 static void lasips2_init(Object *obj) 279 { 280 LASIPS2State *s = LASIPS2(obj); 281 LASIPS2Port *lp; 282 283 object_initialize_child(obj, "lasips2-kbd-port", &s->kbd_port, 284 TYPE_LASIPS2_KBD_PORT); 285 286 s->mouse.id = 1; 287 s->mouse.parent = s; 288 289 memory_region_init_io(&s->mouse.reg, obj, &lasips2_reg_ops, &s->mouse, 290 "lasips2-mouse", 0x100); 291 292 lp = LASIPS2_PORT(&s->kbd_port); 293 sysbus_init_mmio(SYS_BUS_DEVICE(obj), &lp->reg); 294 sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mouse.reg); 295 296 sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->irq); 297 298 qdev_init_gpio_in_named(DEVICE(obj), lasips2_set_kbd_irq, 299 "ps2-kbd-input-irq", 1); 300 qdev_init_gpio_in_named(DEVICE(obj), lasips2_set_mouse_irq, 301 "ps2-mouse-input-irq", 1); 302 } 303 304 static void lasips2_class_init(ObjectClass *klass, void *data) 305 { 306 DeviceClass *dc = DEVICE_CLASS(klass); 307 308 dc->realize = lasips2_realize; 309 dc->vmsd = &vmstate_lasips2; 310 set_bit(DEVICE_CATEGORY_INPUT, dc->categories); 311 } 312 313 static const TypeInfo lasips2_info = { 314 .name = TYPE_LASIPS2, 315 .parent = TYPE_SYS_BUS_DEVICE, 316 .instance_init = lasips2_init, 317 .instance_size = sizeof(LASIPS2State), 318 .class_init = lasips2_class_init, 319 }; 320 321 static const TypeInfo lasips2_port_info = { 322 .name = TYPE_LASIPS2_PORT, 323 .parent = TYPE_DEVICE, 324 .instance_size = sizeof(LASIPS2Port), 325 .abstract = true, 326 }; 327 328 static void lasips2_kbd_port_init(Object *obj) 329 { 330 LASIPS2KbdPort *s = LASIPS2_KBD_PORT(obj); 331 LASIPS2Port *lp = LASIPS2_PORT(obj); 332 333 memory_region_init_io(&lp->reg, obj, &lasips2_reg_ops, lp, "lasips2-kbd", 334 0x100); 335 lp->id = 0; 336 lp->parent = container_of(s, LASIPS2State, kbd_port); 337 } 338 339 static const TypeInfo lasips2_kbd_port_info = { 340 .name = TYPE_LASIPS2_KBD_PORT, 341 .parent = TYPE_LASIPS2_PORT, 342 .instance_size = sizeof(LASIPS2KbdPort), 343 .instance_init = lasips2_kbd_port_init, 344 }; 345 346 static const TypeInfo lasips2_mouse_port_info = { 347 .name = TYPE_LASIPS2_MOUSE_PORT, 348 .parent = TYPE_LASIPS2_PORT, 349 .instance_size = sizeof(LASIPS2MousePort), 350 }; 351 352 static void lasips2_register_types(void) 353 { 354 type_register_static(&lasips2_info); 355 type_register_static(&lasips2_port_info); 356 type_register_static(&lasips2_kbd_port_info); 357 type_register_static(&lasips2_mouse_port_info); 358 } 359 360 type_init(lasips2_register_types) 361