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" 22bce0e9c1SMark Cave-Ayland #include "hw/input/pl050.h" 2364552b6bSMarkus Armbruster #include "hw/irq.h" 2403dd024fSPaolo Bonzini #include "qemu/log.h" 250b8fa32fSMarkus Armbruster #include "qemu/module.h" 26db1015e9SEduardo Habkost #include "qom/object.h" 2749ab747fSPaolo Bonzini 2849ab747fSPaolo Bonzini 2949ab747fSPaolo Bonzini static const VMStateDescription vmstate_pl050 = { 3049ab747fSPaolo Bonzini .name = "pl050", 3149ab747fSPaolo Bonzini .version_id = 2, 3249ab747fSPaolo Bonzini .minimum_version_id = 2, 3349ab747fSPaolo Bonzini .fields = (VMStateField[]) { 34e607f25aSAndreas Färber VMSTATE_UINT32(cr, PL050State), 35e607f25aSAndreas Färber VMSTATE_UINT32(clk, PL050State), 36e607f25aSAndreas Färber VMSTATE_UINT32(last, PL050State), 37e607f25aSAndreas Färber VMSTATE_INT32(pending, PL050State), 3849ab747fSPaolo Bonzini VMSTATE_END_OF_LIST() 3949ab747fSPaolo Bonzini } 4049ab747fSPaolo Bonzini }; 4149ab747fSPaolo Bonzini 4249ab747fSPaolo Bonzini #define PL050_TXEMPTY (1 << 6) 4349ab747fSPaolo Bonzini #define PL050_TXBUSY (1 << 5) 4449ab747fSPaolo Bonzini #define PL050_RXFULL (1 << 4) 4549ab747fSPaolo Bonzini #define PL050_RXBUSY (1 << 3) 4649ab747fSPaolo Bonzini #define PL050_RXPARITY (1 << 2) 4749ab747fSPaolo Bonzini #define PL050_KMIC (1 << 1) 4849ab747fSPaolo Bonzini #define PL050_KMID (1 << 0) 4949ab747fSPaolo Bonzini 50600f7110SMark Cave-Ayland static const unsigned char pl050_id[] = { 51600f7110SMark Cave-Ayland 0x50, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 52600f7110SMark Cave-Ayland }; 5349ab747fSPaolo Bonzini 54eca9e870SMark Cave-Ayland static void pl050_update_irq(PL050State *s) 55eca9e870SMark Cave-Ayland { 56eca9e870SMark Cave-Ayland int level = (s->pending && (s->cr & 0x10) != 0) 57eca9e870SMark Cave-Ayland || (s->cr & 0x08) != 0; 58eca9e870SMark Cave-Ayland 59eca9e870SMark Cave-Ayland qemu_set_irq(s->irq, level); 60eca9e870SMark Cave-Ayland } 61eca9e870SMark Cave-Ayland 62423bcb23SMark Cave-Ayland static void pl050_set_irq(void *opaque, int n, int level) 6349ab747fSPaolo Bonzini { 64e607f25aSAndreas Färber PL050State *s = (PL050State *)opaque; 6549ab747fSPaolo Bonzini 6649ab747fSPaolo Bonzini s->pending = level; 67eca9e870SMark Cave-Ayland pl050_update_irq(s); 6849ab747fSPaolo Bonzini } 6949ab747fSPaolo Bonzini 7049ab747fSPaolo Bonzini static uint64_t pl050_read(void *opaque, hwaddr offset, 7149ab747fSPaolo Bonzini unsigned size) 7249ab747fSPaolo Bonzini { 73e607f25aSAndreas Färber PL050State *s = (PL050State *)opaque; 74600f7110SMark Cave-Ayland 75600f7110SMark Cave-Ayland if (offset >= 0xfe0 && offset < 0x1000) { 7649ab747fSPaolo Bonzini return pl050_id[(offset - 0xfe0) >> 2]; 77600f7110SMark Cave-Ayland } 7849ab747fSPaolo Bonzini 7949ab747fSPaolo Bonzini switch (offset >> 2) { 8049ab747fSPaolo Bonzini case 0: /* KMICR */ 8149ab747fSPaolo Bonzini return s->cr; 8249ab747fSPaolo Bonzini case 1: /* KMISTAT */ 8349ab747fSPaolo Bonzini { 8449ab747fSPaolo Bonzini uint8_t val; 8549ab747fSPaolo Bonzini uint32_t stat; 8649ab747fSPaolo Bonzini 8749ab747fSPaolo Bonzini val = s->last; 8849ab747fSPaolo Bonzini val = val ^ (val >> 4); 8949ab747fSPaolo Bonzini val = val ^ (val >> 2); 9049ab747fSPaolo Bonzini val = (val ^ (val >> 1)) & 1; 9149ab747fSPaolo Bonzini 9249ab747fSPaolo Bonzini stat = PL050_TXEMPTY; 93600f7110SMark Cave-Ayland if (val) { 9449ab747fSPaolo Bonzini stat |= PL050_RXPARITY; 95600f7110SMark Cave-Ayland } 96600f7110SMark Cave-Ayland if (s->pending) { 9749ab747fSPaolo Bonzini stat |= PL050_RXFULL; 98600f7110SMark Cave-Ayland } 9949ab747fSPaolo Bonzini 10049ab747fSPaolo Bonzini return stat; 10149ab747fSPaolo Bonzini } 10249ab747fSPaolo Bonzini case 2: /* KMIDATA */ 103600f7110SMark Cave-Ayland if (s->pending) { 10449ab747fSPaolo Bonzini s->last = ps2_read_data(s->dev); 105600f7110SMark Cave-Ayland } 10649ab747fSPaolo Bonzini return s->last; 10749ab747fSPaolo Bonzini case 3: /* KMICLKDIV */ 10849ab747fSPaolo Bonzini return s->clk; 10949ab747fSPaolo Bonzini case 4: /* KMIIR */ 11049ab747fSPaolo Bonzini return s->pending | 2; 11149ab747fSPaolo Bonzini default: 11249ab747fSPaolo Bonzini qemu_log_mask(LOG_GUEST_ERROR, 11349ab747fSPaolo Bonzini "pl050_read: Bad offset %x\n", (int)offset); 11449ab747fSPaolo Bonzini return 0; 11549ab747fSPaolo Bonzini } 11649ab747fSPaolo Bonzini } 11749ab747fSPaolo Bonzini 11849ab747fSPaolo Bonzini static void pl050_write(void *opaque, hwaddr offset, 11949ab747fSPaolo Bonzini uint64_t value, unsigned size) 12049ab747fSPaolo Bonzini { 121e607f25aSAndreas Färber PL050State *s = (PL050State *)opaque; 122600f7110SMark Cave-Ayland 12349ab747fSPaolo Bonzini switch (offset >> 2) { 12449ab747fSPaolo Bonzini case 0: /* KMICR */ 12549ab747fSPaolo Bonzini s->cr = value; 126eca9e870SMark Cave-Ayland pl050_update_irq(s); 12749ab747fSPaolo Bonzini /* ??? Need to implement the enable/disable bit. */ 12849ab747fSPaolo Bonzini break; 12949ab747fSPaolo Bonzini case 2: /* KMIDATA */ 13049ab747fSPaolo Bonzini /* ??? This should toggle the TX interrupt line. */ 13149ab747fSPaolo Bonzini /* ??? This means kbd/mouse can block each other. */ 13249ab747fSPaolo Bonzini if (s->is_mouse) { 13349ab747fSPaolo Bonzini ps2_write_mouse(s->dev, value); 13449ab747fSPaolo Bonzini } else { 13549ab747fSPaolo Bonzini ps2_write_keyboard(s->dev, value); 13649ab747fSPaolo Bonzini } 13749ab747fSPaolo Bonzini break; 13849ab747fSPaolo Bonzini case 3: /* KMICLKDIV */ 13949ab747fSPaolo Bonzini s->clk = value; 14049ab747fSPaolo Bonzini return; 14149ab747fSPaolo Bonzini default: 14249ab747fSPaolo Bonzini qemu_log_mask(LOG_GUEST_ERROR, 14349ab747fSPaolo Bonzini "pl050_write: Bad offset %x\n", (int)offset); 14449ab747fSPaolo Bonzini } 14549ab747fSPaolo Bonzini } 14649ab747fSPaolo Bonzini static const MemoryRegionOps pl050_ops = { 14749ab747fSPaolo Bonzini .read = pl050_read, 14849ab747fSPaolo Bonzini .write = pl050_write, 14949ab747fSPaolo Bonzini .endianness = DEVICE_NATIVE_ENDIAN, 15049ab747fSPaolo Bonzini }; 15149ab747fSPaolo Bonzini 152988e501aSMao Zhongyi static void pl050_realize(DeviceState *dev, Error **errp) 15349ab747fSPaolo Bonzini { 1543e5dd364SAndreas Färber PL050State *s = PL050(dev); 155988e501aSMao Zhongyi SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 15649ab747fSPaolo Bonzini 1571437c94bSPaolo Bonzini memory_region_init_io(&s->iomem, OBJECT(s), &pl050_ops, s, "pl050", 0x1000); 158988e501aSMao Zhongyi sysbus_init_mmio(sbd, &s->iomem); 159988e501aSMao Zhongyi sysbus_init_irq(sbd, &s->irq); 1603e5dd364SAndreas Färber if (s->is_mouse) { 1617227de94SMark Cave-Ayland s->dev = ps2_mouse_init(); 1623e5dd364SAndreas Färber } else { 1637227de94SMark Cave-Ayland s->dev = ps2_kbd_init(); 1643e5dd364SAndreas Färber } 165423bcb23SMark Cave-Ayland qdev_connect_gpio_out(DEVICE(s->dev), PS2_DEVICE_IRQ, 166423bcb23SMark Cave-Ayland qdev_get_gpio_in_named(dev, "ps2-input-irq", 0)); 16749ab747fSPaolo Bonzini } 16849ab747fSPaolo Bonzini 169*b6c575d8SMark Cave-Ayland static void pl050_kbd_init(Object *obj) 17049ab747fSPaolo Bonzini { 1713e5dd364SAndreas Färber PL050State *s = PL050(obj); 1723e5dd364SAndreas Färber 1733e5dd364SAndreas Färber s->is_mouse = false; 17449ab747fSPaolo Bonzini } 17549ab747fSPaolo Bonzini 1763e5dd364SAndreas Färber static void pl050_mouse_init(Object *obj) 17749ab747fSPaolo Bonzini { 1783e5dd364SAndreas Färber PL050State *s = PL050(obj); 17949ab747fSPaolo Bonzini 1803e5dd364SAndreas Färber s->is_mouse = true; 18149ab747fSPaolo Bonzini } 18249ab747fSPaolo Bonzini 18349ab747fSPaolo Bonzini static const TypeInfo pl050_kbd_info = { 18449ab747fSPaolo Bonzini .name = "pl050_keyboard", 1853e5dd364SAndreas Färber .parent = TYPE_PL050, 186*b6c575d8SMark Cave-Ayland .instance_init = pl050_kbd_init, 18749ab747fSPaolo Bonzini }; 18849ab747fSPaolo Bonzini 18949ab747fSPaolo Bonzini static const TypeInfo pl050_mouse_info = { 19049ab747fSPaolo Bonzini .name = "pl050_mouse", 1913e5dd364SAndreas Färber .parent = TYPE_PL050, 1923e5dd364SAndreas Färber .instance_init = pl050_mouse_init, 1933e5dd364SAndreas Färber }; 1943e5dd364SAndreas Färber 195423bcb23SMark Cave-Ayland static void pl050_init(Object *obj) 196423bcb23SMark Cave-Ayland { 197423bcb23SMark Cave-Ayland qdev_init_gpio_in_named(DEVICE(obj), pl050_set_irq, "ps2-input-irq", 1); 198423bcb23SMark Cave-Ayland } 199423bcb23SMark Cave-Ayland 2003e5dd364SAndreas Färber static void pl050_class_init(ObjectClass *oc, void *data) 2013e5dd364SAndreas Färber { 2023e5dd364SAndreas Färber DeviceClass *dc = DEVICE_CLASS(oc); 2033e5dd364SAndreas Färber 204988e501aSMao Zhongyi dc->realize = pl050_realize; 2053e5dd364SAndreas Färber dc->vmsd = &vmstate_pl050; 2063e5dd364SAndreas Färber } 2073e5dd364SAndreas Färber 2083e5dd364SAndreas Färber static const TypeInfo pl050_type_info = { 2093e5dd364SAndreas Färber .name = TYPE_PL050, 21049ab747fSPaolo Bonzini .parent = TYPE_SYS_BUS_DEVICE, 211423bcb23SMark Cave-Ayland .instance_init = pl050_init, 212e607f25aSAndreas Färber .instance_size = sizeof(PL050State), 2133e5dd364SAndreas Färber .abstract = true, 2143e5dd364SAndreas Färber .class_init = pl050_class_init, 21549ab747fSPaolo Bonzini }; 21649ab747fSPaolo Bonzini 21749ab747fSPaolo Bonzini static void pl050_register_types(void) 21849ab747fSPaolo Bonzini { 2193e5dd364SAndreas Färber type_register_static(&pl050_type_info); 22049ab747fSPaolo Bonzini type_register_static(&pl050_kbd_info); 22149ab747fSPaolo Bonzini type_register_static(&pl050_mouse_info); 22249ab747fSPaolo Bonzini } 22349ab747fSPaolo Bonzini 22449ab747fSPaolo Bonzini type_init(pl050_register_types) 225