xref: /qemu/hw/input/lasips2.c (revision 1702627c3329e6f43b92addc6eb16fc420a4cf18)
12a6505b0SSven Schnelle /*
22a6505b0SSven Schnelle  * QEMU HP Lasi PS/2 interface emulation
32a6505b0SSven Schnelle  *
42a6505b0SSven Schnelle  * Copyright (c) 2019 Sven Schnelle
52a6505b0SSven Schnelle  *
62a6505b0SSven Schnelle  * Permission is hereby granted, free of charge, to any person obtaining a copy
72a6505b0SSven Schnelle  * of this software and associated documentation files (the "Software"), to deal
82a6505b0SSven Schnelle  * in the Software without restriction, including without limitation the rights
92a6505b0SSven Schnelle  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
102a6505b0SSven Schnelle  * copies of the Software, and to permit persons to whom the Software is
112a6505b0SSven Schnelle  * furnished to do so, subject to the following conditions:
122a6505b0SSven Schnelle  *
132a6505b0SSven Schnelle  * The above copyright notice and this permission notice shall be included in
142a6505b0SSven Schnelle  * all copies or substantial portions of the Software.
152a6505b0SSven Schnelle  *
162a6505b0SSven Schnelle  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
172a6505b0SSven Schnelle  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
182a6505b0SSven Schnelle  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
192a6505b0SSven Schnelle  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
202a6505b0SSven Schnelle  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
212a6505b0SSven Schnelle  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
222a6505b0SSven Schnelle  * THE SOFTWARE.
232a6505b0SSven Schnelle  */
242a6505b0SSven Schnelle #include "qemu/osdep.h"
252a6505b0SSven Schnelle #include "qemu/log.h"
262a6505b0SSven Schnelle #include "hw/qdev-properties.h"
27653b388cSMark Cave-Ayland #include "hw/sysbus.h"
282a6505b0SSven Schnelle #include "hw/input/ps2.h"
292a6505b0SSven Schnelle #include "hw/input/lasips2.h"
302a6505b0SSven Schnelle #include "exec/hwaddr.h"
312a6505b0SSven Schnelle #include "trace.h"
322a6505b0SSven Schnelle #include "exec/address-spaces.h"
332a6505b0SSven Schnelle #include "migration/vmstate.h"
342a6505b0SSven Schnelle #include "hw/irq.h"
35653b388cSMark Cave-Ayland #include "qapi/error.h"
362a6505b0SSven Schnelle 
372a6505b0SSven Schnelle 
382a6505b0SSven Schnelle static const VMStateDescription vmstate_lasips2 = {
392a6505b0SSven Schnelle     .name = "lasips2",
402a6505b0SSven Schnelle     .version_id = 0,
412a6505b0SSven Schnelle     .minimum_version_id = 0,
422a6505b0SSven Schnelle     .fields = (VMStateField[]) {
432a6505b0SSven Schnelle         VMSTATE_UINT8(kbd.control, LASIPS2State),
442a6505b0SSven Schnelle         VMSTATE_UINT8(kbd.id, LASIPS2State),
452a6505b0SSven Schnelle         VMSTATE_BOOL(kbd.irq, LASIPS2State),
462a6505b0SSven Schnelle         VMSTATE_UINT8(mouse.control, LASIPS2State),
472a6505b0SSven Schnelle         VMSTATE_UINT8(mouse.id, LASIPS2State),
482a6505b0SSven Schnelle         VMSTATE_BOOL(mouse.irq, LASIPS2State),
492a6505b0SSven Schnelle         VMSTATE_END_OF_LIST()
502a6505b0SSven Schnelle     }
512a6505b0SSven Schnelle };
522a6505b0SSven Schnelle 
532a6505b0SSven Schnelle typedef enum {
542a6505b0SSven Schnelle     REG_PS2_ID = 0,
552a6505b0SSven Schnelle     REG_PS2_RCVDATA = 4,
562a6505b0SSven Schnelle     REG_PS2_CONTROL = 8,
572a6505b0SSven Schnelle     REG_PS2_STATUS = 12,
582a6505b0SSven Schnelle } lasips2_read_reg_t;
592a6505b0SSven Schnelle 
602a6505b0SSven Schnelle typedef enum {
612a6505b0SSven Schnelle     REG_PS2_RESET = 0,
622a6505b0SSven Schnelle     REG_PS2_XMTDATA = 4,
632a6505b0SSven Schnelle } lasips2_write_reg_t;
642a6505b0SSven Schnelle 
652a6505b0SSven Schnelle typedef enum {
662a6505b0SSven Schnelle     LASIPS2_CONTROL_ENABLE = 0x01,
672a6505b0SSven Schnelle     LASIPS2_CONTROL_LOOPBACK = 0x02,
682a6505b0SSven Schnelle     LASIPS2_CONTROL_DIAG = 0x20,
692a6505b0SSven Schnelle     LASIPS2_CONTROL_DATDIR = 0x40,
702a6505b0SSven Schnelle     LASIPS2_CONTROL_CLKDIR = 0x80,
712a6505b0SSven Schnelle } lasips2_control_reg_t;
722a6505b0SSven Schnelle 
732a6505b0SSven Schnelle typedef enum {
742a6505b0SSven Schnelle     LASIPS2_STATUS_RBNE = 0x01,
752a6505b0SSven Schnelle     LASIPS2_STATUS_TBNE = 0x02,
762a6505b0SSven Schnelle     LASIPS2_STATUS_TERR = 0x04,
772a6505b0SSven Schnelle     LASIPS2_STATUS_PERR = 0x08,
782a6505b0SSven Schnelle     LASIPS2_STATUS_CMPINTR = 0x10,
792a6505b0SSven Schnelle     LASIPS2_STATUS_DATSHD = 0x40,
802a6505b0SSven Schnelle     LASIPS2_STATUS_CLKSHD = 0x80,
812a6505b0SSven Schnelle } lasips2_status_reg_t;
822a6505b0SSven Schnelle 
835d2bd735SPhilippe Mathieu-Daudé static const char *lasips2_read_reg_name(uint64_t addr)
842a6505b0SSven Schnelle {
852a6505b0SSven Schnelle     switch (addr & 0xc) {
862a6505b0SSven Schnelle     case REG_PS2_ID:
872a6505b0SSven Schnelle         return " PS2_ID";
882a6505b0SSven Schnelle 
892a6505b0SSven Schnelle     case REG_PS2_RCVDATA:
902a6505b0SSven Schnelle         return " PS2_RCVDATA";
912a6505b0SSven Schnelle 
922a6505b0SSven Schnelle     case REG_PS2_CONTROL:
932a6505b0SSven Schnelle         return " PS2_CONTROL";
942a6505b0SSven Schnelle 
952a6505b0SSven Schnelle     case REG_PS2_STATUS:
962a6505b0SSven Schnelle         return " PS2_STATUS";
972a6505b0SSven Schnelle 
982a6505b0SSven Schnelle     default:
992a6505b0SSven Schnelle         return "";
1002a6505b0SSven Schnelle     }
1012a6505b0SSven Schnelle }
1022a6505b0SSven Schnelle 
1035d2bd735SPhilippe Mathieu-Daudé static const char *lasips2_write_reg_name(uint64_t addr)
1042a6505b0SSven Schnelle {
1052a6505b0SSven Schnelle     switch (addr & 0x0c) {
1062a6505b0SSven Schnelle     case REG_PS2_RESET:
1072a6505b0SSven Schnelle         return " PS2_RESET";
1082a6505b0SSven Schnelle 
1092a6505b0SSven Schnelle     case REG_PS2_XMTDATA:
1102a6505b0SSven Schnelle         return " PS2_XMTDATA";
1112a6505b0SSven Schnelle 
1122a6505b0SSven Schnelle     case REG_PS2_CONTROL:
1132a6505b0SSven Schnelle         return " PS2_CONTROL";
1142a6505b0SSven Schnelle 
1152a6505b0SSven Schnelle     default:
1162a6505b0SSven Schnelle         return "";
1172a6505b0SSven Schnelle     }
1182a6505b0SSven Schnelle }
1192a6505b0SSven Schnelle 
1202a6505b0SSven Schnelle static void lasips2_update_irq(LASIPS2State *s)
1212a6505b0SSven Schnelle {
1222a6505b0SSven Schnelle     trace_lasips2_intr(s->kbd.irq | s->mouse.irq);
1232a6505b0SSven Schnelle     qemu_set_irq(s->irq, s->kbd.irq | s->mouse.irq);
1242a6505b0SSven Schnelle }
1252a6505b0SSven Schnelle 
1262a6505b0SSven Schnelle static void lasips2_reg_write(void *opaque, hwaddr addr, uint64_t val,
1272a6505b0SSven Schnelle                               unsigned size)
1282a6505b0SSven Schnelle {
1292a6505b0SSven Schnelle     LASIPS2Port *port = opaque;
1302a6505b0SSven Schnelle 
1312a6505b0SSven Schnelle     trace_lasips2_reg_write(size, port->id, addr,
1325d2bd735SPhilippe Mathieu-Daudé                             lasips2_write_reg_name(addr), val);
1332a6505b0SSven Schnelle 
1342a6505b0SSven Schnelle     switch (addr & 0xc) {
1352a6505b0SSven Schnelle     case REG_PS2_CONTROL:
1362a6505b0SSven Schnelle         port->control = val;
1372a6505b0SSven Schnelle         break;
1382a6505b0SSven Schnelle 
1392a6505b0SSven Schnelle     case REG_PS2_XMTDATA:
1402a6505b0SSven Schnelle         if (port->control & LASIPS2_CONTROL_LOOPBACK) {
1412a6505b0SSven Schnelle             port->buf = val;
1422a6505b0SSven Schnelle             port->irq = true;
1432a6505b0SSven Schnelle             port->loopback_rbne = true;
1442a6505b0SSven Schnelle             lasips2_update_irq(port->parent);
1452a6505b0SSven Schnelle             break;
1462a6505b0SSven Schnelle         }
1472a6505b0SSven Schnelle 
1482a6505b0SSven Schnelle         if (port->id) {
1492a6505b0SSven Schnelle             ps2_write_mouse(port->dev, val);
1502a6505b0SSven Schnelle         } else {
1512a6505b0SSven Schnelle             ps2_write_keyboard(port->dev, val);
1522a6505b0SSven Schnelle         }
1532a6505b0SSven Schnelle         break;
1542a6505b0SSven Schnelle 
1552a6505b0SSven Schnelle     case REG_PS2_RESET:
1562a6505b0SSven Schnelle         break;
1572a6505b0SSven Schnelle 
1582a6505b0SSven Schnelle     default:
1592a6505b0SSven Schnelle         qemu_log_mask(LOG_UNIMP, "%s: unknown register 0x%02" HWADDR_PRIx "\n",
1602a6505b0SSven Schnelle                       __func__, addr);
1612a6505b0SSven Schnelle         break;
1622a6505b0SSven Schnelle     }
1632a6505b0SSven Schnelle }
1642a6505b0SSven Schnelle 
1652a6505b0SSven Schnelle static uint64_t lasips2_reg_read(void *opaque, hwaddr addr, unsigned size)
1662a6505b0SSven Schnelle {
1672a6505b0SSven Schnelle     LASIPS2Port *port = opaque;
1682a6505b0SSven Schnelle     uint64_t ret = 0;
1692a6505b0SSven Schnelle 
1702a6505b0SSven Schnelle     switch (addr & 0xc) {
1712a6505b0SSven Schnelle     case REG_PS2_ID:
1722a6505b0SSven Schnelle         ret = port->id;
1732a6505b0SSven Schnelle         break;
1742a6505b0SSven Schnelle 
1752a6505b0SSven Schnelle     case REG_PS2_RCVDATA:
1762a6505b0SSven Schnelle         if (port->control & LASIPS2_CONTROL_LOOPBACK) {
1772a6505b0SSven Schnelle             port->irq = false;
1782a6505b0SSven Schnelle             port->loopback_rbne = false;
1792a6505b0SSven Schnelle             lasips2_update_irq(port->parent);
1802a6505b0SSven Schnelle             ret = port->buf;
1812a6505b0SSven Schnelle             break;
1822a6505b0SSven Schnelle         }
1832a6505b0SSven Schnelle 
1842a6505b0SSven Schnelle         ret = ps2_read_data(port->dev);
1852a6505b0SSven Schnelle         break;
1862a6505b0SSven Schnelle 
1872a6505b0SSven Schnelle     case REG_PS2_CONTROL:
1882a6505b0SSven Schnelle         ret = port->control;
1892a6505b0SSven Schnelle         break;
1902a6505b0SSven Schnelle 
1912a6505b0SSven Schnelle     case REG_PS2_STATUS:
1922a6505b0SSven Schnelle         ret = LASIPS2_STATUS_DATSHD | LASIPS2_STATUS_CLKSHD;
1932a6505b0SSven Schnelle 
1942a6505b0SSven Schnelle         if (port->control & LASIPS2_CONTROL_DIAG) {
1952a6505b0SSven Schnelle             if (!(port->control & LASIPS2_CONTROL_DATDIR)) {
1962a6505b0SSven Schnelle                 ret &= ~LASIPS2_STATUS_DATSHD;
1972a6505b0SSven Schnelle             }
1982a6505b0SSven Schnelle 
1992a6505b0SSven Schnelle             if (!(port->control & LASIPS2_CONTROL_CLKDIR)) {
2002a6505b0SSven Schnelle                 ret &= ~LASIPS2_STATUS_CLKSHD;
2012a6505b0SSven Schnelle             }
2022a6505b0SSven Schnelle         }
2032a6505b0SSven Schnelle 
2042a6505b0SSven Schnelle         if (port->control & LASIPS2_CONTROL_LOOPBACK) {
2052a6505b0SSven Schnelle             if (port->loopback_rbne) {
2062a6505b0SSven Schnelle                 ret |= LASIPS2_STATUS_RBNE;
2072a6505b0SSven Schnelle             }
2082a6505b0SSven Schnelle         } else {
2092a6505b0SSven Schnelle             if (!ps2_queue_empty(port->dev)) {
2102a6505b0SSven Schnelle                 ret |= LASIPS2_STATUS_RBNE;
2112a6505b0SSven Schnelle             }
2122a6505b0SSven Schnelle         }
2132a6505b0SSven Schnelle 
2142a6505b0SSven Schnelle         if (port->parent->kbd.irq || port->parent->mouse.irq) {
2152a6505b0SSven Schnelle             ret |= LASIPS2_STATUS_CMPINTR;
2162a6505b0SSven Schnelle         }
2172a6505b0SSven Schnelle         break;
2182a6505b0SSven Schnelle 
2192a6505b0SSven Schnelle     default:
2202a6505b0SSven Schnelle         qemu_log_mask(LOG_UNIMP, "%s: unknown register 0x%02" HWADDR_PRIx "\n",
2212a6505b0SSven Schnelle                       __func__, addr);
2222a6505b0SSven Schnelle         break;
2232a6505b0SSven Schnelle     }
2242a93d3c1SMark Cave-Ayland 
2252a6505b0SSven Schnelle     trace_lasips2_reg_read(size, port->id, addr,
2265d2bd735SPhilippe Mathieu-Daudé                            lasips2_read_reg_name(addr), ret);
2272a6505b0SSven Schnelle     return ret;
2282a6505b0SSven Schnelle }
2292a6505b0SSven Schnelle 
2302a6505b0SSven Schnelle static const MemoryRegionOps lasips2_reg_ops = {
2312a6505b0SSven Schnelle     .read = lasips2_reg_read,
2322a6505b0SSven Schnelle     .write = lasips2_reg_write,
2332a6505b0SSven Schnelle     .impl = {
2342a6505b0SSven Schnelle         .min_access_size = 1,
2352a6505b0SSven Schnelle         .max_access_size = 4,
2362a6505b0SSven Schnelle     },
2372a6505b0SSven Schnelle     .endianness = DEVICE_NATIVE_ENDIAN,
2382a6505b0SSven Schnelle };
2392a6505b0SSven Schnelle 
240f342469fSMark Cave-Ayland static void lasips2_port_set_irq(void *opaque, int level)
2412a6505b0SSven Schnelle {
2422a6505b0SSven Schnelle     LASIPS2Port *port = opaque;
2432a93d3c1SMark Cave-Ayland 
2442a6505b0SSven Schnelle     port->irq = level;
2452a6505b0SSven Schnelle     lasips2_update_irq(port->parent);
2462a6505b0SSven Schnelle }
2472a6505b0SSven Schnelle 
2486479296fSMark Cave-Ayland LASIPS2State *lasips2_initfn(hwaddr base, qemu_irq irq)
2492a6505b0SSven Schnelle {
2502a6505b0SSven Schnelle     LASIPS2State *s;
251653b388cSMark Cave-Ayland     DeviceState *dev;
2522a6505b0SSven Schnelle 
253653b388cSMark Cave-Ayland     dev = qdev_new(TYPE_LASIPS2);
25442119fdbSMark Cave-Ayland     qdev_prop_set_uint64(dev, "base", base);
255653b388cSMark Cave-Ayland     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
256653b388cSMark Cave-Ayland     s = LASIPS2(dev);
2572a6505b0SSven Schnelle 
2582a6505b0SSven Schnelle     s->irq = irq;
259*1702627cSMark Cave-Ayland     return s;
260*1702627cSMark Cave-Ayland }
2612a6505b0SSven Schnelle 
262*1702627cSMark Cave-Ayland static void lasips2_realize(DeviceState *dev, Error **errp)
263*1702627cSMark Cave-Ayland {
264*1702627cSMark Cave-Ayland     LASIPS2State *s = LASIPS2(dev);
265*1702627cSMark Cave-Ayland 
266*1702627cSMark Cave-Ayland     vmstate_register(NULL, s->base, &vmstate_lasips2, s);
2672a6505b0SSven Schnelle 
268f342469fSMark Cave-Ayland     s->kbd.dev = ps2_kbd_init(lasips2_port_set_irq, &s->kbd);
269f342469fSMark Cave-Ayland     s->mouse.dev = ps2_mouse_init(lasips2_port_set_irq, &s->mouse);
2702a6505b0SSven Schnelle }
271653b388cSMark Cave-Ayland 
27263195aa5SMark Cave-Ayland static void lasips2_init(Object *obj)
27363195aa5SMark Cave-Ayland {
27463195aa5SMark Cave-Ayland     LASIPS2State *s = LASIPS2(obj);
27563195aa5SMark Cave-Ayland 
27602bb59a0SMark Cave-Ayland     s->kbd.id = 0;
27702bb59a0SMark Cave-Ayland     s->mouse.id = 1;
27802bb59a0SMark Cave-Ayland     s->kbd.parent = s;
27902bb59a0SMark Cave-Ayland     s->mouse.parent = s;
28002bb59a0SMark Cave-Ayland 
28163195aa5SMark Cave-Ayland     memory_region_init_io(&s->kbd.reg, obj, &lasips2_reg_ops, &s->kbd,
28263195aa5SMark Cave-Ayland                           "lasips2-kbd", 0x100);
28363195aa5SMark Cave-Ayland     memory_region_init_io(&s->mouse.reg, obj, &lasips2_reg_ops, &s->mouse,
28463195aa5SMark Cave-Ayland                           "lasips2-mouse", 0x100);
28563195aa5SMark Cave-Ayland 
28663195aa5SMark Cave-Ayland     sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->kbd.reg);
28763195aa5SMark Cave-Ayland     sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mouse.reg);
28863195aa5SMark Cave-Ayland }
28963195aa5SMark Cave-Ayland 
29042119fdbSMark Cave-Ayland static Property lasips2_properties[] = {
29142119fdbSMark Cave-Ayland     DEFINE_PROP_UINT64("base", LASIPS2State, base, UINT64_MAX),
29242119fdbSMark Cave-Ayland     DEFINE_PROP_END_OF_LIST(),
29342119fdbSMark Cave-Ayland };
29442119fdbSMark Cave-Ayland 
29542119fdbSMark Cave-Ayland static void lasips2_class_init(ObjectClass *klass, void *data)
29642119fdbSMark Cave-Ayland {
29742119fdbSMark Cave-Ayland     DeviceClass *dc = DEVICE_CLASS(klass);
29842119fdbSMark Cave-Ayland 
299*1702627cSMark Cave-Ayland     dc->realize = lasips2_realize;
30042119fdbSMark Cave-Ayland     device_class_set_props(dc, lasips2_properties);
30142119fdbSMark Cave-Ayland     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
30242119fdbSMark Cave-Ayland }
30342119fdbSMark Cave-Ayland 
304653b388cSMark Cave-Ayland static const TypeInfo lasips2_info = {
305653b388cSMark Cave-Ayland     .name          = TYPE_LASIPS2,
306653b388cSMark Cave-Ayland     .parent        = TYPE_SYS_BUS_DEVICE,
30763195aa5SMark Cave-Ayland     .instance_init = lasips2_init,
30842119fdbSMark Cave-Ayland     .instance_size = sizeof(LASIPS2State),
30942119fdbSMark Cave-Ayland     .class_init    = lasips2_class_init,
310653b388cSMark Cave-Ayland };
311653b388cSMark Cave-Ayland 
312653b388cSMark Cave-Ayland static void lasips2_register_types(void)
313653b388cSMark Cave-Ayland {
314653b388cSMark Cave-Ayland     type_register_static(&lasips2_info);
315653b388cSMark Cave-Ayland }
316653b388cSMark Cave-Ayland 
317653b388cSMark Cave-Ayland type_init(lasips2_register_types)
318