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" 12*d6454270SMarkus 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" 1749ab747fSPaolo Bonzini 183e5dd364SAndreas Färber #define TYPE_PL050 "pl050" 193e5dd364SAndreas Färber #define PL050(obj) OBJECT_CHECK(PL050State, (obj), TYPE_PL050) 203e5dd364SAndreas Färber 21e607f25aSAndreas Färber typedef struct PL050State { 223e5dd364SAndreas Färber SysBusDevice parent_obj; 233e5dd364SAndreas Färber 2449ab747fSPaolo Bonzini MemoryRegion iomem; 2549ab747fSPaolo Bonzini void *dev; 2649ab747fSPaolo Bonzini uint32_t cr; 2749ab747fSPaolo Bonzini uint32_t clk; 2849ab747fSPaolo Bonzini uint32_t last; 2949ab747fSPaolo Bonzini int pending; 3049ab747fSPaolo Bonzini qemu_irq irq; 313e5dd364SAndreas Färber bool is_mouse; 32e607f25aSAndreas Färber } PL050State; 3349ab747fSPaolo Bonzini 3449ab747fSPaolo Bonzini static const VMStateDescription vmstate_pl050 = { 3549ab747fSPaolo Bonzini .name = "pl050", 3649ab747fSPaolo Bonzini .version_id = 2, 3749ab747fSPaolo Bonzini .minimum_version_id = 2, 3849ab747fSPaolo Bonzini .fields = (VMStateField[]) { 39e607f25aSAndreas Färber VMSTATE_UINT32(cr, PL050State), 40e607f25aSAndreas Färber VMSTATE_UINT32(clk, PL050State), 41e607f25aSAndreas Färber VMSTATE_UINT32(last, PL050State), 42e607f25aSAndreas Färber VMSTATE_INT32(pending, PL050State), 4349ab747fSPaolo Bonzini VMSTATE_END_OF_LIST() 4449ab747fSPaolo Bonzini } 4549ab747fSPaolo Bonzini }; 4649ab747fSPaolo Bonzini 4749ab747fSPaolo Bonzini #define PL050_TXEMPTY (1 << 6) 4849ab747fSPaolo Bonzini #define PL050_TXBUSY (1 << 5) 4949ab747fSPaolo Bonzini #define PL050_RXFULL (1 << 4) 5049ab747fSPaolo Bonzini #define PL050_RXBUSY (1 << 3) 5149ab747fSPaolo Bonzini #define PL050_RXPARITY (1 << 2) 5249ab747fSPaolo Bonzini #define PL050_KMIC (1 << 1) 5349ab747fSPaolo Bonzini #define PL050_KMID (1 << 0) 5449ab747fSPaolo Bonzini 5549ab747fSPaolo Bonzini static const unsigned char pl050_id[] = 5649ab747fSPaolo Bonzini { 0x50, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 }; 5749ab747fSPaolo Bonzini 5849ab747fSPaolo Bonzini static void pl050_update(void *opaque, int level) 5949ab747fSPaolo Bonzini { 60e607f25aSAndreas Färber PL050State *s = (PL050State *)opaque; 6149ab747fSPaolo Bonzini int raise; 6249ab747fSPaolo Bonzini 6349ab747fSPaolo Bonzini s->pending = level; 6449ab747fSPaolo Bonzini raise = (s->pending && (s->cr & 0x10) != 0) 6549ab747fSPaolo Bonzini || (s->cr & 0x08) != 0; 6649ab747fSPaolo Bonzini qemu_set_irq(s->irq, raise); 6749ab747fSPaolo Bonzini } 6849ab747fSPaolo Bonzini 6949ab747fSPaolo Bonzini static uint64_t pl050_read(void *opaque, hwaddr offset, 7049ab747fSPaolo Bonzini unsigned size) 7149ab747fSPaolo Bonzini { 72e607f25aSAndreas Färber PL050State *s = (PL050State *)opaque; 7349ab747fSPaolo Bonzini if (offset >= 0xfe0 && offset < 0x1000) 7449ab747fSPaolo Bonzini return pl050_id[(offset - 0xfe0) >> 2]; 7549ab747fSPaolo Bonzini 7649ab747fSPaolo Bonzini switch (offset >> 2) { 7749ab747fSPaolo Bonzini case 0: /* KMICR */ 7849ab747fSPaolo Bonzini return s->cr; 7949ab747fSPaolo Bonzini case 1: /* KMISTAT */ 8049ab747fSPaolo Bonzini { 8149ab747fSPaolo Bonzini uint8_t val; 8249ab747fSPaolo Bonzini uint32_t stat; 8349ab747fSPaolo Bonzini 8449ab747fSPaolo Bonzini val = s->last; 8549ab747fSPaolo Bonzini val = val ^ (val >> 4); 8649ab747fSPaolo Bonzini val = val ^ (val >> 2); 8749ab747fSPaolo Bonzini val = (val ^ (val >> 1)) & 1; 8849ab747fSPaolo Bonzini 8949ab747fSPaolo Bonzini stat = PL050_TXEMPTY; 9049ab747fSPaolo Bonzini if (val) 9149ab747fSPaolo Bonzini stat |= PL050_RXPARITY; 9249ab747fSPaolo Bonzini if (s->pending) 9349ab747fSPaolo Bonzini stat |= PL050_RXFULL; 9449ab747fSPaolo Bonzini 9549ab747fSPaolo Bonzini return stat; 9649ab747fSPaolo Bonzini } 9749ab747fSPaolo Bonzini case 2: /* KMIDATA */ 9849ab747fSPaolo Bonzini if (s->pending) 9949ab747fSPaolo Bonzini s->last = ps2_read_data(s->dev); 10049ab747fSPaolo Bonzini return s->last; 10149ab747fSPaolo Bonzini case 3: /* KMICLKDIV */ 10249ab747fSPaolo Bonzini return s->clk; 10349ab747fSPaolo Bonzini case 4: /* KMIIR */ 10449ab747fSPaolo Bonzini return s->pending | 2; 10549ab747fSPaolo Bonzini default: 10649ab747fSPaolo Bonzini qemu_log_mask(LOG_GUEST_ERROR, 10749ab747fSPaolo Bonzini "pl050_read: Bad offset %x\n", (int)offset); 10849ab747fSPaolo Bonzini return 0; 10949ab747fSPaolo Bonzini } 11049ab747fSPaolo Bonzini } 11149ab747fSPaolo Bonzini 11249ab747fSPaolo Bonzini static void pl050_write(void *opaque, hwaddr offset, 11349ab747fSPaolo Bonzini uint64_t value, unsigned size) 11449ab747fSPaolo Bonzini { 115e607f25aSAndreas Färber PL050State *s = (PL050State *)opaque; 11649ab747fSPaolo Bonzini switch (offset >> 2) { 11749ab747fSPaolo Bonzini case 0: /* KMICR */ 11849ab747fSPaolo Bonzini s->cr = value; 11949ab747fSPaolo Bonzini pl050_update(s, s->pending); 12049ab747fSPaolo Bonzini /* ??? Need to implement the enable/disable bit. */ 12149ab747fSPaolo Bonzini break; 12249ab747fSPaolo Bonzini case 2: /* KMIDATA */ 12349ab747fSPaolo Bonzini /* ??? This should toggle the TX interrupt line. */ 12449ab747fSPaolo Bonzini /* ??? This means kbd/mouse can block each other. */ 12549ab747fSPaolo Bonzini if (s->is_mouse) { 12649ab747fSPaolo Bonzini ps2_write_mouse(s->dev, value); 12749ab747fSPaolo Bonzini } else { 12849ab747fSPaolo Bonzini ps2_write_keyboard(s->dev, value); 12949ab747fSPaolo Bonzini } 13049ab747fSPaolo Bonzini break; 13149ab747fSPaolo Bonzini case 3: /* KMICLKDIV */ 13249ab747fSPaolo Bonzini s->clk = value; 13349ab747fSPaolo Bonzini return; 13449ab747fSPaolo Bonzini default: 13549ab747fSPaolo Bonzini qemu_log_mask(LOG_GUEST_ERROR, 13649ab747fSPaolo Bonzini "pl050_write: Bad offset %x\n", (int)offset); 13749ab747fSPaolo Bonzini } 13849ab747fSPaolo Bonzini } 13949ab747fSPaolo Bonzini static const MemoryRegionOps pl050_ops = { 14049ab747fSPaolo Bonzini .read = pl050_read, 14149ab747fSPaolo Bonzini .write = pl050_write, 14249ab747fSPaolo Bonzini .endianness = DEVICE_NATIVE_ENDIAN, 14349ab747fSPaolo Bonzini }; 14449ab747fSPaolo Bonzini 145988e501aSMao Zhongyi static void pl050_realize(DeviceState *dev, Error **errp) 14649ab747fSPaolo Bonzini { 1473e5dd364SAndreas Färber PL050State *s = PL050(dev); 148988e501aSMao Zhongyi SysBusDevice *sbd = SYS_BUS_DEVICE(dev); 14949ab747fSPaolo Bonzini 1501437c94bSPaolo Bonzini memory_region_init_io(&s->iomem, OBJECT(s), &pl050_ops, s, "pl050", 0x1000); 151988e501aSMao Zhongyi sysbus_init_mmio(sbd, &s->iomem); 152988e501aSMao Zhongyi sysbus_init_irq(sbd, &s->irq); 1533e5dd364SAndreas Färber if (s->is_mouse) { 15449ab747fSPaolo Bonzini s->dev = ps2_mouse_init(pl050_update, s); 1553e5dd364SAndreas Färber } else { 15649ab747fSPaolo Bonzini s->dev = ps2_kbd_init(pl050_update, s); 1573e5dd364SAndreas Färber } 15849ab747fSPaolo Bonzini } 15949ab747fSPaolo Bonzini 1603e5dd364SAndreas Färber static void pl050_keyboard_init(Object *obj) 16149ab747fSPaolo Bonzini { 1623e5dd364SAndreas Färber PL050State *s = PL050(obj); 1633e5dd364SAndreas Färber 1643e5dd364SAndreas Färber s->is_mouse = false; 16549ab747fSPaolo Bonzini } 16649ab747fSPaolo Bonzini 1673e5dd364SAndreas Färber static void pl050_mouse_init(Object *obj) 16849ab747fSPaolo Bonzini { 1693e5dd364SAndreas Färber PL050State *s = PL050(obj); 17049ab747fSPaolo Bonzini 1713e5dd364SAndreas Färber s->is_mouse = true; 17249ab747fSPaolo Bonzini } 17349ab747fSPaolo Bonzini 17449ab747fSPaolo Bonzini static const TypeInfo pl050_kbd_info = { 17549ab747fSPaolo Bonzini .name = "pl050_keyboard", 1763e5dd364SAndreas Färber .parent = TYPE_PL050, 1773e5dd364SAndreas Färber .instance_init = pl050_keyboard_init, 17849ab747fSPaolo Bonzini }; 17949ab747fSPaolo Bonzini 18049ab747fSPaolo Bonzini static const TypeInfo pl050_mouse_info = { 18149ab747fSPaolo Bonzini .name = "pl050_mouse", 1823e5dd364SAndreas Färber .parent = TYPE_PL050, 1833e5dd364SAndreas Färber .instance_init = pl050_mouse_init, 1843e5dd364SAndreas Färber }; 1853e5dd364SAndreas Färber 1863e5dd364SAndreas Färber static void pl050_class_init(ObjectClass *oc, void *data) 1873e5dd364SAndreas Färber { 1883e5dd364SAndreas Färber DeviceClass *dc = DEVICE_CLASS(oc); 1893e5dd364SAndreas Färber 190988e501aSMao Zhongyi dc->realize = pl050_realize; 1913e5dd364SAndreas Färber dc->vmsd = &vmstate_pl050; 1923e5dd364SAndreas Färber } 1933e5dd364SAndreas Färber 1943e5dd364SAndreas Färber static const TypeInfo pl050_type_info = { 1953e5dd364SAndreas Färber .name = TYPE_PL050, 19649ab747fSPaolo Bonzini .parent = TYPE_SYS_BUS_DEVICE, 197e607f25aSAndreas Färber .instance_size = sizeof(PL050State), 1983e5dd364SAndreas Färber .abstract = true, 1993e5dd364SAndreas Färber .class_init = pl050_class_init, 20049ab747fSPaolo Bonzini }; 20149ab747fSPaolo Bonzini 20249ab747fSPaolo Bonzini static void pl050_register_types(void) 20349ab747fSPaolo Bonzini { 2043e5dd364SAndreas Färber type_register_static(&pl050_type_info); 20549ab747fSPaolo Bonzini type_register_static(&pl050_kbd_info); 20649ab747fSPaolo Bonzini type_register_static(&pl050_mouse_info); 20749ab747fSPaolo Bonzini } 20849ab747fSPaolo Bonzini 20949ab747fSPaolo Bonzini type_init(pl050_register_types) 210