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 101d9d4b07SMark Cave-Ayland /* 111d9d4b07SMark Cave-Ayland * QEMU interface: 121d9d4b07SMark Cave-Ayland * + sysbus MMIO region 0: MemoryRegion defining the PL050 registers 131d9d4b07SMark Cave-Ayland * + Named GPIO input "ps2-input-irq": set to 1 if the downstream PS2 device 141d9d4b07SMark Cave-Ayland * has asserted its irq 151d9d4b07SMark Cave-Ayland * + sysbus IRQ 0: PL050 output irq 161d9d4b07SMark Cave-Ayland */ 171d9d4b07SMark Cave-Ayland 188ef94f0bSPeter Maydell #include "qemu/osdep.h" 1949ab747fSPaolo Bonzini #include "hw/sysbus.h" 20d6454270SMarkus Armbruster #include "migration/vmstate.h" 2149ab747fSPaolo Bonzini #include "hw/input/ps2.h" 2264552b6bSMarkus Armbruster #include "hw/irq.h" 2303dd024fSPaolo Bonzini #include "qemu/log.h" 240b8fa32fSMarkus Armbruster #include "qemu/module.h" 25db1015e9SEduardo Habkost #include "qom/object.h" 2649ab747fSPaolo Bonzini 273e5dd364SAndreas Färber #define TYPE_PL050 "pl050" 288063396bSEduardo Habkost OBJECT_DECLARE_SIMPLE_TYPE(PL050State, PL050) 293e5dd364SAndreas Färber 30db1015e9SEduardo Habkost struct PL050State { 313e5dd364SAndreas Färber SysBusDevice parent_obj; 323e5dd364SAndreas Färber 3349ab747fSPaolo Bonzini MemoryRegion iomem; 3449ab747fSPaolo Bonzini void *dev; 3549ab747fSPaolo Bonzini uint32_t cr; 3649ab747fSPaolo Bonzini uint32_t clk; 3749ab747fSPaolo Bonzini uint32_t last; 3849ab747fSPaolo Bonzini int pending; 3949ab747fSPaolo Bonzini qemu_irq irq; 403e5dd364SAndreas Färber bool is_mouse; 41db1015e9SEduardo Habkost }; 4249ab747fSPaolo Bonzini 4349ab747fSPaolo Bonzini static const VMStateDescription vmstate_pl050 = { 4449ab747fSPaolo Bonzini .name = "pl050", 4549ab747fSPaolo Bonzini .version_id = 2, 4649ab747fSPaolo Bonzini .minimum_version_id = 2, 4749ab747fSPaolo Bonzini .fields = (VMStateField[]) { 48e607f25aSAndreas Färber VMSTATE_UINT32(cr, PL050State), 49e607f25aSAndreas Färber VMSTATE_UINT32(clk, PL050State), 50e607f25aSAndreas Färber VMSTATE_UINT32(last, PL050State), 51e607f25aSAndreas Färber VMSTATE_INT32(pending, PL050State), 5249ab747fSPaolo Bonzini VMSTATE_END_OF_LIST() 5349ab747fSPaolo Bonzini } 5449ab747fSPaolo Bonzini }; 5549ab747fSPaolo Bonzini 5649ab747fSPaolo Bonzini #define PL050_TXEMPTY (1 << 6) 5749ab747fSPaolo Bonzini #define PL050_TXBUSY (1 << 5) 5849ab747fSPaolo Bonzini #define PL050_RXFULL (1 << 4) 5949ab747fSPaolo Bonzini #define PL050_RXBUSY (1 << 3) 6049ab747fSPaolo Bonzini #define PL050_RXPARITY (1 << 2) 6149ab747fSPaolo Bonzini #define PL050_KMIC (1 << 1) 6249ab747fSPaolo Bonzini #define PL050_KMID (1 << 0) 6349ab747fSPaolo Bonzini 64600f7110SMark Cave-Ayland static const unsigned char pl050_id[] = { 65600f7110SMark Cave-Ayland 0x50, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 66600f7110SMark Cave-Ayland }; 6749ab747fSPaolo Bonzini 68eca9e870SMark Cave-Ayland static void pl050_update_irq(PL050State *s) 69eca9e870SMark Cave-Ayland { 70eca9e870SMark Cave-Ayland int level = (s->pending && (s->cr & 0x10) != 0) 71eca9e870SMark Cave-Ayland || (s->cr & 0x08) != 0; 72eca9e870SMark Cave-Ayland 73eca9e870SMark Cave-Ayland qemu_set_irq(s->irq, level); 74eca9e870SMark Cave-Ayland } 75eca9e870SMark Cave-Ayland 76423bcb23SMark Cave-Ayland static void pl050_set_irq(void *opaque, int n, int level) 7749ab747fSPaolo Bonzini { 78e607f25aSAndreas Färber PL050State *s = (PL050State *)opaque; 7949ab747fSPaolo Bonzini 8049ab747fSPaolo Bonzini s->pending = level; 81eca9e870SMark Cave-Ayland pl050_update_irq(s); 8249ab747fSPaolo Bonzini } 8349ab747fSPaolo Bonzini 8449ab747fSPaolo Bonzini static uint64_t pl050_read(void *opaque, hwaddr offset, 8549ab747fSPaolo Bonzini unsigned size) 8649ab747fSPaolo Bonzini { 87e607f25aSAndreas Färber PL050State *s = (PL050State *)opaque; 88600f7110SMark Cave-Ayland 89600f7110SMark Cave-Ayland if (offset >= 0xfe0 && offset < 0x1000) { 9049ab747fSPaolo Bonzini return pl050_id[(offset - 0xfe0) >> 2]; 91600f7110SMark Cave-Ayland } 9249ab747fSPaolo Bonzini 9349ab747fSPaolo Bonzini switch (offset >> 2) { 9449ab747fSPaolo Bonzini case 0: /* KMICR */ 9549ab747fSPaolo Bonzini return s->cr; 9649ab747fSPaolo Bonzini case 1: /* KMISTAT */ 9749ab747fSPaolo Bonzini { 9849ab747fSPaolo Bonzini uint8_t val; 9949ab747fSPaolo Bonzini uint32_t stat; 10049ab747fSPaolo Bonzini 10149ab747fSPaolo Bonzini val = s->last; 10249ab747fSPaolo Bonzini val = val ^ (val >> 4); 10349ab747fSPaolo Bonzini val = val ^ (val >> 2); 10449ab747fSPaolo Bonzini val = (val ^ (val >> 1)) & 1; 10549ab747fSPaolo Bonzini 10649ab747fSPaolo Bonzini stat = PL050_TXEMPTY; 107600f7110SMark Cave-Ayland if (val) { 10849ab747fSPaolo Bonzini stat |= PL050_RXPARITY; 109600f7110SMark Cave-Ayland } 110600f7110SMark Cave-Ayland if (s->pending) { 11149ab747fSPaolo Bonzini stat |= PL050_RXFULL; 112600f7110SMark Cave-Ayland } 11349ab747fSPaolo Bonzini 11449ab747fSPaolo Bonzini return stat; 11549ab747fSPaolo Bonzini } 11649ab747fSPaolo Bonzini case 2: /* KMIDATA */ 117600f7110SMark Cave-Ayland if (s->pending) { 11849ab747fSPaolo Bonzini s->last = ps2_read_data(s->dev); 119600f7110SMark Cave-Ayland } 12049ab747fSPaolo Bonzini return s->last; 12149ab747fSPaolo Bonzini case 3: /* KMICLKDIV */ 12249ab747fSPaolo Bonzini return s->clk; 12349ab747fSPaolo Bonzini case 4: /* KMIIR */ 12449ab747fSPaolo Bonzini return s->pending | 2; 12549ab747fSPaolo Bonzini default: 12649ab747fSPaolo Bonzini qemu_log_mask(LOG_GUEST_ERROR, 12749ab747fSPaolo Bonzini "pl050_read: Bad offset %x\n", (int)offset); 12849ab747fSPaolo Bonzini return 0; 12949ab747fSPaolo Bonzini } 13049ab747fSPaolo Bonzini } 13149ab747fSPaolo Bonzini 13249ab747fSPaolo Bonzini static void pl050_write(void *opaque, hwaddr offset, 13349ab747fSPaolo Bonzini uint64_t value, unsigned size) 13449ab747fSPaolo Bonzini { 135e607f25aSAndreas Färber PL050State *s = (PL050State *)opaque; 136600f7110SMark Cave-Ayland 13749ab747fSPaolo Bonzini switch (offset >> 2) { 13849ab747fSPaolo Bonzini case 0: /* KMICR */ 13949ab747fSPaolo Bonzini s->cr = value; 140eca9e870SMark Cave-Ayland pl050_update_irq(s); 14149ab747fSPaolo Bonzini /* ??? Need to implement the enable/disable bit. */ 14249ab747fSPaolo Bonzini break; 14349ab747fSPaolo Bonzini case 2: /* KMIDATA */ 14449ab747fSPaolo Bonzini /* ??? This should toggle the TX interrupt line. */ 14549ab747fSPaolo Bonzini /* ??? This means kbd/mouse can block each other. */ 14649ab747fSPaolo Bonzini if (s->is_mouse) { 14749ab747fSPaolo Bonzini ps2_write_mouse(s->dev, value); 14849ab747fSPaolo Bonzini } else { 14949ab747fSPaolo Bonzini ps2_write_keyboard(s->dev, value); 15049ab747fSPaolo Bonzini } 15149ab747fSPaolo Bonzini break; 15249ab747fSPaolo Bonzini case 3: /* KMICLKDIV */ 15349ab747fSPaolo Bonzini s->clk = value; 15449ab747fSPaolo Bonzini return; 15549ab747fSPaolo Bonzini default: 15649ab747fSPaolo Bonzini qemu_log_mask(LOG_GUEST_ERROR, 15749ab747fSPaolo Bonzini "pl050_write: Bad offset %x\n", (int)offset); 15849ab747fSPaolo Bonzini } 15949ab747fSPaolo Bonzini } 16049ab747fSPaolo Bonzini static const MemoryRegionOps pl050_ops = { 16149ab747fSPaolo Bonzini .read = pl050_read, 16249ab747fSPaolo Bonzini .write = pl050_write, 16349ab747fSPaolo Bonzini .endianness = DEVICE_NATIVE_ENDIAN, 16449ab747fSPaolo Bonzini }; 16549ab747fSPaolo Bonzini 166988e501aSMao Zhongyi static void pl050_realize(DeviceState *dev, Error **errp) 16749ab747fSPaolo Bonzini { 1683e5dd364SAndreas Färber PL050State *s = PL050(dev); 169988e501aSMao Zhongyi SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 17049ab747fSPaolo Bonzini 1711437c94bSPaolo Bonzini memory_region_init_io(&s->iomem, OBJECT(s), &pl050_ops, s, "pl050", 0x1000); 172988e501aSMao Zhongyi sysbus_init_mmio(sbd, &s->iomem); 173988e501aSMao Zhongyi sysbus_init_irq(sbd, &s->irq); 1743e5dd364SAndreas Färber if (s->is_mouse) { 175*7227de94SMark Cave-Ayland s->dev = ps2_mouse_init(); 1763e5dd364SAndreas Färber } else { 177*7227de94SMark Cave-Ayland s->dev = ps2_kbd_init(); 1783e5dd364SAndreas Färber } 179423bcb23SMark Cave-Ayland qdev_connect_gpio_out(DEVICE(s->dev), PS2_DEVICE_IRQ, 180423bcb23SMark Cave-Ayland qdev_get_gpio_in_named(dev, "ps2-input-irq", 0)); 18149ab747fSPaolo Bonzini } 18249ab747fSPaolo Bonzini 1833e5dd364SAndreas Färber static void pl050_keyboard_init(Object *obj) 18449ab747fSPaolo Bonzini { 1853e5dd364SAndreas Färber PL050State *s = PL050(obj); 1863e5dd364SAndreas Färber 1873e5dd364SAndreas Färber s->is_mouse = false; 18849ab747fSPaolo Bonzini } 18949ab747fSPaolo Bonzini 1903e5dd364SAndreas Färber static void pl050_mouse_init(Object *obj) 19149ab747fSPaolo Bonzini { 1923e5dd364SAndreas Färber PL050State *s = PL050(obj); 19349ab747fSPaolo Bonzini 1943e5dd364SAndreas Färber s->is_mouse = true; 19549ab747fSPaolo Bonzini } 19649ab747fSPaolo Bonzini 19749ab747fSPaolo Bonzini static const TypeInfo pl050_kbd_info = { 19849ab747fSPaolo Bonzini .name = "pl050_keyboard", 1993e5dd364SAndreas Färber .parent = TYPE_PL050, 2003e5dd364SAndreas Färber .instance_init = pl050_keyboard_init, 20149ab747fSPaolo Bonzini }; 20249ab747fSPaolo Bonzini 20349ab747fSPaolo Bonzini static const TypeInfo pl050_mouse_info = { 20449ab747fSPaolo Bonzini .name = "pl050_mouse", 2053e5dd364SAndreas Färber .parent = TYPE_PL050, 2063e5dd364SAndreas Färber .instance_init = pl050_mouse_init, 2073e5dd364SAndreas Färber }; 2083e5dd364SAndreas Färber 209423bcb23SMark Cave-Ayland static void pl050_init(Object *obj) 210423bcb23SMark Cave-Ayland { 211423bcb23SMark Cave-Ayland qdev_init_gpio_in_named(DEVICE(obj), pl050_set_irq, "ps2-input-irq", 1); 212423bcb23SMark Cave-Ayland } 213423bcb23SMark Cave-Ayland 2143e5dd364SAndreas Färber static void pl050_class_init(ObjectClass *oc, void *data) 2153e5dd364SAndreas Färber { 2163e5dd364SAndreas Färber DeviceClass *dc = DEVICE_CLASS(oc); 2173e5dd364SAndreas Färber 218988e501aSMao Zhongyi dc->realize = pl050_realize; 2193e5dd364SAndreas Färber dc->vmsd = &vmstate_pl050; 2203e5dd364SAndreas Färber } 2213e5dd364SAndreas Färber 2223e5dd364SAndreas Färber static const TypeInfo pl050_type_info = { 2233e5dd364SAndreas Färber .name = TYPE_PL050, 22449ab747fSPaolo Bonzini .parent = TYPE_SYS_BUS_DEVICE, 225423bcb23SMark Cave-Ayland .instance_init = pl050_init, 226e607f25aSAndreas Färber .instance_size = sizeof(PL050State), 2273e5dd364SAndreas Färber .abstract = true, 2283e5dd364SAndreas Färber .class_init = pl050_class_init, 22949ab747fSPaolo Bonzini }; 23049ab747fSPaolo Bonzini 23149ab747fSPaolo Bonzini static void pl050_register_types(void) 23249ab747fSPaolo Bonzini { 2333e5dd364SAndreas Färber type_register_static(&pl050_type_info); 23449ab747fSPaolo Bonzini type_register_static(&pl050_kbd_info); 23549ab747fSPaolo Bonzini type_register_static(&pl050_mouse_info); 23649ab747fSPaolo Bonzini } 23749ab747fSPaolo Bonzini 23849ab747fSPaolo Bonzini type_init(pl050_register_types) 239