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" 20*8063396bSEduardo 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 5649ab747fSPaolo Bonzini static const unsigned char pl050_id[] = 5749ab747fSPaolo Bonzini { 0x50, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 }; 5849ab747fSPaolo Bonzini 5949ab747fSPaolo Bonzini static void pl050_update(void *opaque, int level) 6049ab747fSPaolo Bonzini { 61e607f25aSAndreas Färber PL050State *s = (PL050State *)opaque; 6249ab747fSPaolo Bonzini int raise; 6349ab747fSPaolo Bonzini 6449ab747fSPaolo Bonzini s->pending = level; 6549ab747fSPaolo Bonzini raise = (s->pending && (s->cr & 0x10) != 0) 6649ab747fSPaolo Bonzini || (s->cr & 0x08) != 0; 6749ab747fSPaolo Bonzini qemu_set_irq(s->irq, raise); 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; 7449ab747fSPaolo Bonzini if (offset >= 0xfe0 && offset < 0x1000) 7549ab747fSPaolo Bonzini return pl050_id[(offset - 0xfe0) >> 2]; 7649ab747fSPaolo Bonzini 7749ab747fSPaolo Bonzini switch (offset >> 2) { 7849ab747fSPaolo Bonzini case 0: /* KMICR */ 7949ab747fSPaolo Bonzini return s->cr; 8049ab747fSPaolo Bonzini case 1: /* KMISTAT */ 8149ab747fSPaolo Bonzini { 8249ab747fSPaolo Bonzini uint8_t val; 8349ab747fSPaolo Bonzini uint32_t stat; 8449ab747fSPaolo Bonzini 8549ab747fSPaolo Bonzini val = s->last; 8649ab747fSPaolo Bonzini val = val ^ (val >> 4); 8749ab747fSPaolo Bonzini val = val ^ (val >> 2); 8849ab747fSPaolo Bonzini val = (val ^ (val >> 1)) & 1; 8949ab747fSPaolo Bonzini 9049ab747fSPaolo Bonzini stat = PL050_TXEMPTY; 9149ab747fSPaolo Bonzini if (val) 9249ab747fSPaolo Bonzini stat |= PL050_RXPARITY; 9349ab747fSPaolo Bonzini if (s->pending) 9449ab747fSPaolo Bonzini stat |= PL050_RXFULL; 9549ab747fSPaolo Bonzini 9649ab747fSPaolo Bonzini return stat; 9749ab747fSPaolo Bonzini } 9849ab747fSPaolo Bonzini case 2: /* KMIDATA */ 9949ab747fSPaolo Bonzini if (s->pending) 10049ab747fSPaolo Bonzini s->last = ps2_read_data(s->dev); 10149ab747fSPaolo Bonzini return s->last; 10249ab747fSPaolo Bonzini case 3: /* KMICLKDIV */ 10349ab747fSPaolo Bonzini return s->clk; 10449ab747fSPaolo Bonzini case 4: /* KMIIR */ 10549ab747fSPaolo Bonzini return s->pending | 2; 10649ab747fSPaolo Bonzini default: 10749ab747fSPaolo Bonzini qemu_log_mask(LOG_GUEST_ERROR, 10849ab747fSPaolo Bonzini "pl050_read: Bad offset %x\n", (int)offset); 10949ab747fSPaolo Bonzini return 0; 11049ab747fSPaolo Bonzini } 11149ab747fSPaolo Bonzini } 11249ab747fSPaolo Bonzini 11349ab747fSPaolo Bonzini static void pl050_write(void *opaque, hwaddr offset, 11449ab747fSPaolo Bonzini uint64_t value, unsigned size) 11549ab747fSPaolo Bonzini { 116e607f25aSAndreas Färber PL050State *s = (PL050State *)opaque; 11749ab747fSPaolo Bonzini switch (offset >> 2) { 11849ab747fSPaolo Bonzini case 0: /* KMICR */ 11949ab747fSPaolo Bonzini s->cr = value; 12049ab747fSPaolo Bonzini pl050_update(s, s->pending); 12149ab747fSPaolo Bonzini /* ??? Need to implement the enable/disable bit. */ 12249ab747fSPaolo Bonzini break; 12349ab747fSPaolo Bonzini case 2: /* KMIDATA */ 12449ab747fSPaolo Bonzini /* ??? This should toggle the TX interrupt line. */ 12549ab747fSPaolo Bonzini /* ??? This means kbd/mouse can block each other. */ 12649ab747fSPaolo Bonzini if (s->is_mouse) { 12749ab747fSPaolo Bonzini ps2_write_mouse(s->dev, value); 12849ab747fSPaolo Bonzini } else { 12949ab747fSPaolo Bonzini ps2_write_keyboard(s->dev, value); 13049ab747fSPaolo Bonzini } 13149ab747fSPaolo Bonzini break; 13249ab747fSPaolo Bonzini case 3: /* KMICLKDIV */ 13349ab747fSPaolo Bonzini s->clk = value; 13449ab747fSPaolo Bonzini return; 13549ab747fSPaolo Bonzini default: 13649ab747fSPaolo Bonzini qemu_log_mask(LOG_GUEST_ERROR, 13749ab747fSPaolo Bonzini "pl050_write: Bad offset %x\n", (int)offset); 13849ab747fSPaolo Bonzini } 13949ab747fSPaolo Bonzini } 14049ab747fSPaolo Bonzini static const MemoryRegionOps pl050_ops = { 14149ab747fSPaolo Bonzini .read = pl050_read, 14249ab747fSPaolo Bonzini .write = pl050_write, 14349ab747fSPaolo Bonzini .endianness = DEVICE_NATIVE_ENDIAN, 14449ab747fSPaolo Bonzini }; 14549ab747fSPaolo Bonzini 146988e501aSMao Zhongyi static void pl050_realize(DeviceState *dev, Error **errp) 14749ab747fSPaolo Bonzini { 1483e5dd364SAndreas Färber PL050State *s = PL050(dev); 149988e501aSMao Zhongyi SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 15049ab747fSPaolo Bonzini 1511437c94bSPaolo Bonzini memory_region_init_io(&s->iomem, OBJECT(s), &pl050_ops, s, "pl050", 0x1000); 152988e501aSMao Zhongyi sysbus_init_mmio(sbd, &s->iomem); 153988e501aSMao Zhongyi sysbus_init_irq(sbd, &s->irq); 1543e5dd364SAndreas Färber if (s->is_mouse) { 15549ab747fSPaolo Bonzini s->dev = ps2_mouse_init(pl050_update, s); 1563e5dd364SAndreas Färber } else { 15749ab747fSPaolo Bonzini s->dev = ps2_kbd_init(pl050_update, s); 1583e5dd364SAndreas Färber } 15949ab747fSPaolo Bonzini } 16049ab747fSPaolo Bonzini 1613e5dd364SAndreas Färber static void pl050_keyboard_init(Object *obj) 16249ab747fSPaolo Bonzini { 1633e5dd364SAndreas Färber PL050State *s = PL050(obj); 1643e5dd364SAndreas Färber 1653e5dd364SAndreas Färber s->is_mouse = false; 16649ab747fSPaolo Bonzini } 16749ab747fSPaolo Bonzini 1683e5dd364SAndreas Färber static void pl050_mouse_init(Object *obj) 16949ab747fSPaolo Bonzini { 1703e5dd364SAndreas Färber PL050State *s = PL050(obj); 17149ab747fSPaolo Bonzini 1723e5dd364SAndreas Färber s->is_mouse = true; 17349ab747fSPaolo Bonzini } 17449ab747fSPaolo Bonzini 17549ab747fSPaolo Bonzini static const TypeInfo pl050_kbd_info = { 17649ab747fSPaolo Bonzini .name = "pl050_keyboard", 1773e5dd364SAndreas Färber .parent = TYPE_PL050, 1783e5dd364SAndreas Färber .instance_init = pl050_keyboard_init, 17949ab747fSPaolo Bonzini }; 18049ab747fSPaolo Bonzini 18149ab747fSPaolo Bonzini static const TypeInfo pl050_mouse_info = { 18249ab747fSPaolo Bonzini .name = "pl050_mouse", 1833e5dd364SAndreas Färber .parent = TYPE_PL050, 1843e5dd364SAndreas Färber .instance_init = pl050_mouse_init, 1853e5dd364SAndreas Färber }; 1863e5dd364SAndreas Färber 1873e5dd364SAndreas Färber static void pl050_class_init(ObjectClass *oc, void *data) 1883e5dd364SAndreas Färber { 1893e5dd364SAndreas Färber DeviceClass *dc = DEVICE_CLASS(oc); 1903e5dd364SAndreas Färber 191988e501aSMao Zhongyi dc->realize = pl050_realize; 1923e5dd364SAndreas Färber dc->vmsd = &vmstate_pl050; 1933e5dd364SAndreas Färber } 1943e5dd364SAndreas Färber 1953e5dd364SAndreas Färber static const TypeInfo pl050_type_info = { 1963e5dd364SAndreas Färber .name = TYPE_PL050, 19749ab747fSPaolo Bonzini .parent = TYPE_SYS_BUS_DEVICE, 198e607f25aSAndreas Färber .instance_size = sizeof(PL050State), 1993e5dd364SAndreas Färber .abstract = true, 2003e5dd364SAndreas Färber .class_init = pl050_class_init, 20149ab747fSPaolo Bonzini }; 20249ab747fSPaolo Bonzini 20349ab747fSPaolo Bonzini static void pl050_register_types(void) 20449ab747fSPaolo Bonzini { 2053e5dd364SAndreas Färber type_register_static(&pl050_type_info); 20649ab747fSPaolo Bonzini type_register_static(&pl050_kbd_info); 20749ab747fSPaolo Bonzini type_register_static(&pl050_mouse_info); 20849ab747fSPaolo Bonzini } 20949ab747fSPaolo Bonzini 21049ab747fSPaolo Bonzini type_init(pl050_register_types) 211