149ab747fSPaolo Bonzini /* 249ab747fSPaolo Bonzini * Arm PrimeCell PL050 Keyboard / Mouse Interface 349ab747fSPaolo Bonzini * 449ab747fSPaolo Bonzini * Copyright (c) 2006-2007 CodeSourcery. 549ab747fSPaolo Bonzini * Written by Paul Brook 649ab747fSPaolo Bonzini * 749ab747fSPaolo Bonzini * This code is licensed under the GPL. 849ab747fSPaolo Bonzini */ 949ab747fSPaolo Bonzini 108ef94f0bSPeter Maydell #include "qemu/osdep.h" 1149ab747fSPaolo Bonzini #include "hw/sysbus.h" 12d6454270SMarkus Armbruster #include "migration/vmstate.h" 1349ab747fSPaolo Bonzini #include "hw/input/ps2.h" 1464552b6bSMarkus Armbruster #include "hw/irq.h" 1503dd024fSPaolo Bonzini #include "qemu/log.h" 160b8fa32fSMarkus Armbruster #include "qemu/module.h" 17db1015e9SEduardo Habkost #include "qom/object.h" 1849ab747fSPaolo Bonzini 193e5dd364SAndreas Färber #define TYPE_PL050 "pl050" 208063396bSEduardo Habkost OBJECT_DECLARE_SIMPLE_TYPE(PL050State, PL050) 213e5dd364SAndreas Färber 22db1015e9SEduardo Habkost struct PL050State { 233e5dd364SAndreas Färber SysBusDevice parent_obj; 243e5dd364SAndreas Färber 2549ab747fSPaolo Bonzini MemoryRegion iomem; 2649ab747fSPaolo Bonzini void *dev; 2749ab747fSPaolo Bonzini uint32_t cr; 2849ab747fSPaolo Bonzini uint32_t clk; 2949ab747fSPaolo Bonzini uint32_t last; 3049ab747fSPaolo Bonzini int pending; 3149ab747fSPaolo Bonzini qemu_irq irq; 323e5dd364SAndreas Färber bool is_mouse; 33db1015e9SEduardo Habkost }; 3449ab747fSPaolo Bonzini 3549ab747fSPaolo Bonzini static const VMStateDescription vmstate_pl050 = { 3649ab747fSPaolo Bonzini .name = "pl050", 3749ab747fSPaolo Bonzini .version_id = 2, 3849ab747fSPaolo Bonzini .minimum_version_id = 2, 3949ab747fSPaolo Bonzini .fields = (VMStateField[]) { 40e607f25aSAndreas Färber VMSTATE_UINT32(cr, PL050State), 41e607f25aSAndreas Färber VMSTATE_UINT32(clk, PL050State), 42e607f25aSAndreas Färber VMSTATE_UINT32(last, PL050State), 43e607f25aSAndreas Färber VMSTATE_INT32(pending, PL050State), 4449ab747fSPaolo Bonzini VMSTATE_END_OF_LIST() 4549ab747fSPaolo Bonzini } 4649ab747fSPaolo Bonzini }; 4749ab747fSPaolo Bonzini 4849ab747fSPaolo Bonzini #define PL050_TXEMPTY (1 << 6) 4949ab747fSPaolo Bonzini #define PL050_TXBUSY (1 << 5) 5049ab747fSPaolo Bonzini #define PL050_RXFULL (1 << 4) 5149ab747fSPaolo Bonzini #define PL050_RXBUSY (1 << 3) 5249ab747fSPaolo Bonzini #define PL050_RXPARITY (1 << 2) 5349ab747fSPaolo Bonzini #define PL050_KMIC (1 << 1) 5449ab747fSPaolo Bonzini #define PL050_KMID (1 << 0) 5549ab747fSPaolo Bonzini 56600f7110SMark Cave-Ayland static const unsigned char pl050_id[] = { 57600f7110SMark Cave-Ayland 0x50, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 58600f7110SMark Cave-Ayland }; 5949ab747fSPaolo Bonzini 60*eca9e870SMark Cave-Ayland static void pl050_update_irq(PL050State *s) 61*eca9e870SMark Cave-Ayland { 62*eca9e870SMark Cave-Ayland int level = (s->pending && (s->cr & 0x10) != 0) 63*eca9e870SMark Cave-Ayland || (s->cr & 0x08) != 0; 64*eca9e870SMark Cave-Ayland 65*eca9e870SMark Cave-Ayland qemu_set_irq(s->irq, level); 66*eca9e870SMark Cave-Ayland } 67*eca9e870SMark Cave-Ayland 68*eca9e870SMark Cave-Ayland static void pl050_set_irq(void *opaque, int level) 6949ab747fSPaolo Bonzini { 70e607f25aSAndreas Färber PL050State *s = (PL050State *)opaque; 7149ab747fSPaolo Bonzini 7249ab747fSPaolo Bonzini s->pending = level; 73*eca9e870SMark Cave-Ayland pl050_update_irq(s); 7449ab747fSPaolo Bonzini } 7549ab747fSPaolo Bonzini 7649ab747fSPaolo Bonzini static uint64_t pl050_read(void *opaque, hwaddr offset, 7749ab747fSPaolo Bonzini unsigned size) 7849ab747fSPaolo Bonzini { 79e607f25aSAndreas Färber PL050State *s = (PL050State *)opaque; 80600f7110SMark Cave-Ayland 81600f7110SMark Cave-Ayland if (offset >= 0xfe0 && offset < 0x1000) { 8249ab747fSPaolo Bonzini return pl050_id[(offset - 0xfe0) >> 2]; 83600f7110SMark Cave-Ayland } 8449ab747fSPaolo Bonzini 8549ab747fSPaolo Bonzini switch (offset >> 2) { 8649ab747fSPaolo Bonzini case 0: /* KMICR */ 8749ab747fSPaolo Bonzini return s->cr; 8849ab747fSPaolo Bonzini case 1: /* KMISTAT */ 8949ab747fSPaolo Bonzini { 9049ab747fSPaolo Bonzini uint8_t val; 9149ab747fSPaolo Bonzini uint32_t stat; 9249ab747fSPaolo Bonzini 9349ab747fSPaolo Bonzini val = s->last; 9449ab747fSPaolo Bonzini val = val ^ (val >> 4); 9549ab747fSPaolo Bonzini val = val ^ (val >> 2); 9649ab747fSPaolo Bonzini val = (val ^ (val >> 1)) & 1; 9749ab747fSPaolo Bonzini 9849ab747fSPaolo Bonzini stat = PL050_TXEMPTY; 99600f7110SMark Cave-Ayland if (val) { 10049ab747fSPaolo Bonzini stat |= PL050_RXPARITY; 101600f7110SMark Cave-Ayland } 102600f7110SMark Cave-Ayland if (s->pending) { 10349ab747fSPaolo Bonzini stat |= PL050_RXFULL; 104600f7110SMark Cave-Ayland } 10549ab747fSPaolo Bonzini 10649ab747fSPaolo Bonzini return stat; 10749ab747fSPaolo Bonzini } 10849ab747fSPaolo Bonzini case 2: /* KMIDATA */ 109600f7110SMark Cave-Ayland if (s->pending) { 11049ab747fSPaolo Bonzini s->last = ps2_read_data(s->dev); 111600f7110SMark Cave-Ayland } 11249ab747fSPaolo Bonzini return s->last; 11349ab747fSPaolo Bonzini case 3: /* KMICLKDIV */ 11449ab747fSPaolo Bonzini return s->clk; 11549ab747fSPaolo Bonzini case 4: /* KMIIR */ 11649ab747fSPaolo Bonzini return s->pending | 2; 11749ab747fSPaolo Bonzini default: 11849ab747fSPaolo Bonzini qemu_log_mask(LOG_GUEST_ERROR, 11949ab747fSPaolo Bonzini "pl050_read: Bad offset %x\n", (int)offset); 12049ab747fSPaolo Bonzini return 0; 12149ab747fSPaolo Bonzini } 12249ab747fSPaolo Bonzini } 12349ab747fSPaolo Bonzini 12449ab747fSPaolo Bonzini static void pl050_write(void *opaque, hwaddr offset, 12549ab747fSPaolo Bonzini uint64_t value, unsigned size) 12649ab747fSPaolo Bonzini { 127e607f25aSAndreas Färber PL050State *s = (PL050State *)opaque; 128600f7110SMark Cave-Ayland 12949ab747fSPaolo Bonzini switch (offset >> 2) { 13049ab747fSPaolo Bonzini case 0: /* KMICR */ 13149ab747fSPaolo Bonzini s->cr = value; 132*eca9e870SMark Cave-Ayland pl050_update_irq(s); 13349ab747fSPaolo Bonzini /* ??? Need to implement the enable/disable bit. */ 13449ab747fSPaolo Bonzini break; 13549ab747fSPaolo Bonzini case 2: /* KMIDATA */ 13649ab747fSPaolo Bonzini /* ??? This should toggle the TX interrupt line. */ 13749ab747fSPaolo Bonzini /* ??? This means kbd/mouse can block each other. */ 13849ab747fSPaolo Bonzini if (s->is_mouse) { 13949ab747fSPaolo Bonzini ps2_write_mouse(s->dev, value); 14049ab747fSPaolo Bonzini } else { 14149ab747fSPaolo Bonzini ps2_write_keyboard(s->dev, value); 14249ab747fSPaolo Bonzini } 14349ab747fSPaolo Bonzini break; 14449ab747fSPaolo Bonzini case 3: /* KMICLKDIV */ 14549ab747fSPaolo Bonzini s->clk = value; 14649ab747fSPaolo Bonzini return; 14749ab747fSPaolo Bonzini default: 14849ab747fSPaolo Bonzini qemu_log_mask(LOG_GUEST_ERROR, 14949ab747fSPaolo Bonzini "pl050_write: Bad offset %x\n", (int)offset); 15049ab747fSPaolo Bonzini } 15149ab747fSPaolo Bonzini } 15249ab747fSPaolo Bonzini static const MemoryRegionOps pl050_ops = { 15349ab747fSPaolo Bonzini .read = pl050_read, 15449ab747fSPaolo Bonzini .write = pl050_write, 15549ab747fSPaolo Bonzini .endianness = DEVICE_NATIVE_ENDIAN, 15649ab747fSPaolo Bonzini }; 15749ab747fSPaolo Bonzini 158988e501aSMao Zhongyi static void pl050_realize(DeviceState *dev, Error **errp) 15949ab747fSPaolo Bonzini { 1603e5dd364SAndreas Färber PL050State *s = PL050(dev); 161988e501aSMao Zhongyi SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 16249ab747fSPaolo Bonzini 1631437c94bSPaolo Bonzini memory_region_init_io(&s->iomem, OBJECT(s), &pl050_ops, s, "pl050", 0x1000); 164988e501aSMao Zhongyi sysbus_init_mmio(sbd, &s->iomem); 165988e501aSMao Zhongyi sysbus_init_irq(sbd, &s->irq); 1663e5dd364SAndreas Färber if (s->is_mouse) { 167*eca9e870SMark Cave-Ayland s->dev = ps2_mouse_init(pl050_set_irq, s); 1683e5dd364SAndreas Färber } else { 169*eca9e870SMark Cave-Ayland s->dev = ps2_kbd_init(pl050_set_irq, s); 1703e5dd364SAndreas Färber } 17149ab747fSPaolo Bonzini } 17249ab747fSPaolo Bonzini 1733e5dd364SAndreas Färber static void pl050_keyboard_init(Object *obj) 17449ab747fSPaolo Bonzini { 1753e5dd364SAndreas Färber PL050State *s = PL050(obj); 1763e5dd364SAndreas Färber 1773e5dd364SAndreas Färber s->is_mouse = false; 17849ab747fSPaolo Bonzini } 17949ab747fSPaolo Bonzini 1803e5dd364SAndreas Färber static void pl050_mouse_init(Object *obj) 18149ab747fSPaolo Bonzini { 1823e5dd364SAndreas Färber PL050State *s = PL050(obj); 18349ab747fSPaolo Bonzini 1843e5dd364SAndreas Färber s->is_mouse = true; 18549ab747fSPaolo Bonzini } 18649ab747fSPaolo Bonzini 18749ab747fSPaolo Bonzini static const TypeInfo pl050_kbd_info = { 18849ab747fSPaolo Bonzini .name = "pl050_keyboard", 1893e5dd364SAndreas Färber .parent = TYPE_PL050, 1903e5dd364SAndreas Färber .instance_init = pl050_keyboard_init, 19149ab747fSPaolo Bonzini }; 19249ab747fSPaolo Bonzini 19349ab747fSPaolo Bonzini static const TypeInfo pl050_mouse_info = { 19449ab747fSPaolo Bonzini .name = "pl050_mouse", 1953e5dd364SAndreas Färber .parent = TYPE_PL050, 1963e5dd364SAndreas Färber .instance_init = pl050_mouse_init, 1973e5dd364SAndreas Färber }; 1983e5dd364SAndreas Färber 1993e5dd364SAndreas Färber static void pl050_class_init(ObjectClass *oc, void *data) 2003e5dd364SAndreas Färber { 2013e5dd364SAndreas Färber DeviceClass *dc = DEVICE_CLASS(oc); 2023e5dd364SAndreas Färber 203988e501aSMao Zhongyi dc->realize = pl050_realize; 2043e5dd364SAndreas Färber dc->vmsd = &vmstate_pl050; 2053e5dd364SAndreas Färber } 2063e5dd364SAndreas Färber 2073e5dd364SAndreas Färber static const TypeInfo pl050_type_info = { 2083e5dd364SAndreas Färber .name = TYPE_PL050, 20949ab747fSPaolo Bonzini .parent = TYPE_SYS_BUS_DEVICE, 210e607f25aSAndreas Färber .instance_size = sizeof(PL050State), 2113e5dd364SAndreas Färber .abstract = true, 2123e5dd364SAndreas Färber .class_init = pl050_class_init, 21349ab747fSPaolo Bonzini }; 21449ab747fSPaolo Bonzini 21549ab747fSPaolo Bonzini static void pl050_register_types(void) 21649ab747fSPaolo Bonzini { 2173e5dd364SAndreas Färber type_register_static(&pl050_type_info); 21849ab747fSPaolo Bonzini type_register_static(&pl050_kbd_info); 21949ab747fSPaolo Bonzini type_register_static(&pl050_mouse_info); 22049ab747fSPaolo Bonzini } 22149ab747fSPaolo Bonzini 22249ab747fSPaolo Bonzini type_init(pl050_register_types) 223