xref: /openbmc/qemu/hw/input/pl050.c (revision db1015e92e04835c9eb50c29625fe566d1202dbd)
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"
17*db1015e9SEduardo Habkost #include "qom/object.h"
1849ab747fSPaolo Bonzini 
193e5dd364SAndreas Färber #define TYPE_PL050 "pl050"
20*db1015e9SEduardo Habkost typedef struct PL050State PL050State;
213e5dd364SAndreas Färber #define PL050(obj) OBJECT_CHECK(PL050State, (obj), TYPE_PL050)
223e5dd364SAndreas Färber 
23*db1015e9SEduardo Habkost struct PL050State {
243e5dd364SAndreas Färber     SysBusDevice parent_obj;
253e5dd364SAndreas Färber 
2649ab747fSPaolo Bonzini     MemoryRegion iomem;
2749ab747fSPaolo Bonzini     void *dev;
2849ab747fSPaolo Bonzini     uint32_t cr;
2949ab747fSPaolo Bonzini     uint32_t clk;
3049ab747fSPaolo Bonzini     uint32_t last;
3149ab747fSPaolo Bonzini     int pending;
3249ab747fSPaolo Bonzini     qemu_irq irq;
333e5dd364SAndreas Färber     bool is_mouse;
34*db1015e9SEduardo Habkost };
3549ab747fSPaolo Bonzini 
3649ab747fSPaolo Bonzini static const VMStateDescription vmstate_pl050 = {
3749ab747fSPaolo Bonzini     .name = "pl050",
3849ab747fSPaolo Bonzini     .version_id = 2,
3949ab747fSPaolo Bonzini     .minimum_version_id = 2,
4049ab747fSPaolo Bonzini     .fields = (VMStateField[]) {
41e607f25aSAndreas Färber         VMSTATE_UINT32(cr, PL050State),
42e607f25aSAndreas Färber         VMSTATE_UINT32(clk, PL050State),
43e607f25aSAndreas Färber         VMSTATE_UINT32(last, PL050State),
44e607f25aSAndreas Färber         VMSTATE_INT32(pending, PL050State),
4549ab747fSPaolo Bonzini         VMSTATE_END_OF_LIST()
4649ab747fSPaolo Bonzini     }
4749ab747fSPaolo Bonzini };
4849ab747fSPaolo Bonzini 
4949ab747fSPaolo Bonzini #define PL050_TXEMPTY         (1 << 6)
5049ab747fSPaolo Bonzini #define PL050_TXBUSY          (1 << 5)
5149ab747fSPaolo Bonzini #define PL050_RXFULL          (1 << 4)
5249ab747fSPaolo Bonzini #define PL050_RXBUSY          (1 << 3)
5349ab747fSPaolo Bonzini #define PL050_RXPARITY        (1 << 2)
5449ab747fSPaolo Bonzini #define PL050_KMIC            (1 << 1)
5549ab747fSPaolo Bonzini #define PL050_KMID            (1 << 0)
5649ab747fSPaolo Bonzini 
5749ab747fSPaolo Bonzini static const unsigned char pl050_id[] =
5849ab747fSPaolo Bonzini { 0x50, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
5949ab747fSPaolo Bonzini 
6049ab747fSPaolo Bonzini static void pl050_update(void *opaque, int level)
6149ab747fSPaolo Bonzini {
62e607f25aSAndreas Färber     PL050State *s = (PL050State *)opaque;
6349ab747fSPaolo Bonzini     int raise;
6449ab747fSPaolo Bonzini 
6549ab747fSPaolo Bonzini     s->pending = level;
6649ab747fSPaolo Bonzini     raise = (s->pending && (s->cr & 0x10) != 0)
6749ab747fSPaolo Bonzini             || (s->cr & 0x08) != 0;
6849ab747fSPaolo Bonzini     qemu_set_irq(s->irq, raise);
6949ab747fSPaolo Bonzini }
7049ab747fSPaolo Bonzini 
7149ab747fSPaolo Bonzini static uint64_t pl050_read(void *opaque, hwaddr offset,
7249ab747fSPaolo Bonzini                            unsigned size)
7349ab747fSPaolo Bonzini {
74e607f25aSAndreas Färber     PL050State *s = (PL050State *)opaque;
7549ab747fSPaolo Bonzini     if (offset >= 0xfe0 && offset < 0x1000)
7649ab747fSPaolo Bonzini         return pl050_id[(offset - 0xfe0) >> 2];
7749ab747fSPaolo Bonzini 
7849ab747fSPaolo Bonzini     switch (offset >> 2) {
7949ab747fSPaolo Bonzini     case 0: /* KMICR */
8049ab747fSPaolo Bonzini         return s->cr;
8149ab747fSPaolo Bonzini     case 1: /* KMISTAT */
8249ab747fSPaolo Bonzini         {
8349ab747fSPaolo Bonzini             uint8_t val;
8449ab747fSPaolo Bonzini             uint32_t stat;
8549ab747fSPaolo Bonzini 
8649ab747fSPaolo Bonzini             val = s->last;
8749ab747fSPaolo Bonzini             val = val ^ (val >> 4);
8849ab747fSPaolo Bonzini             val = val ^ (val >> 2);
8949ab747fSPaolo Bonzini             val = (val ^ (val >> 1)) & 1;
9049ab747fSPaolo Bonzini 
9149ab747fSPaolo Bonzini             stat = PL050_TXEMPTY;
9249ab747fSPaolo Bonzini             if (val)
9349ab747fSPaolo Bonzini                 stat |= PL050_RXPARITY;
9449ab747fSPaolo Bonzini             if (s->pending)
9549ab747fSPaolo Bonzini                 stat |= PL050_RXFULL;
9649ab747fSPaolo Bonzini 
9749ab747fSPaolo Bonzini             return stat;
9849ab747fSPaolo Bonzini         }
9949ab747fSPaolo Bonzini     case 2: /* KMIDATA */
10049ab747fSPaolo Bonzini         if (s->pending)
10149ab747fSPaolo Bonzini             s->last = ps2_read_data(s->dev);
10249ab747fSPaolo Bonzini         return s->last;
10349ab747fSPaolo Bonzini     case 3: /* KMICLKDIV */
10449ab747fSPaolo Bonzini         return s->clk;
10549ab747fSPaolo Bonzini     case 4: /* KMIIR */
10649ab747fSPaolo Bonzini         return s->pending | 2;
10749ab747fSPaolo Bonzini     default:
10849ab747fSPaolo Bonzini         qemu_log_mask(LOG_GUEST_ERROR,
10949ab747fSPaolo Bonzini                       "pl050_read: Bad offset %x\n", (int)offset);
11049ab747fSPaolo Bonzini         return 0;
11149ab747fSPaolo Bonzini     }
11249ab747fSPaolo Bonzini }
11349ab747fSPaolo Bonzini 
11449ab747fSPaolo Bonzini static void pl050_write(void *opaque, hwaddr offset,
11549ab747fSPaolo Bonzini                         uint64_t value, unsigned size)
11649ab747fSPaolo Bonzini {
117e607f25aSAndreas Färber     PL050State *s = (PL050State *)opaque;
11849ab747fSPaolo Bonzini     switch (offset >> 2) {
11949ab747fSPaolo Bonzini     case 0: /* KMICR */
12049ab747fSPaolo Bonzini         s->cr = value;
12149ab747fSPaolo Bonzini         pl050_update(s, s->pending);
12249ab747fSPaolo Bonzini         /* ??? Need to implement the enable/disable bit.  */
12349ab747fSPaolo Bonzini         break;
12449ab747fSPaolo Bonzini     case 2: /* KMIDATA */
12549ab747fSPaolo Bonzini         /* ??? This should toggle the TX interrupt line.  */
12649ab747fSPaolo Bonzini         /* ??? This means kbd/mouse can block each other.  */
12749ab747fSPaolo Bonzini         if (s->is_mouse) {
12849ab747fSPaolo Bonzini             ps2_write_mouse(s->dev, value);
12949ab747fSPaolo Bonzini         } else {
13049ab747fSPaolo Bonzini             ps2_write_keyboard(s->dev, value);
13149ab747fSPaolo Bonzini         }
13249ab747fSPaolo Bonzini         break;
13349ab747fSPaolo Bonzini     case 3: /* KMICLKDIV */
13449ab747fSPaolo Bonzini         s->clk = value;
13549ab747fSPaolo Bonzini         return;
13649ab747fSPaolo Bonzini     default:
13749ab747fSPaolo Bonzini         qemu_log_mask(LOG_GUEST_ERROR,
13849ab747fSPaolo Bonzini                       "pl050_write: Bad offset %x\n", (int)offset);
13949ab747fSPaolo Bonzini     }
14049ab747fSPaolo Bonzini }
14149ab747fSPaolo Bonzini static const MemoryRegionOps pl050_ops = {
14249ab747fSPaolo Bonzini     .read = pl050_read,
14349ab747fSPaolo Bonzini     .write = pl050_write,
14449ab747fSPaolo Bonzini     .endianness = DEVICE_NATIVE_ENDIAN,
14549ab747fSPaolo Bonzini };
14649ab747fSPaolo Bonzini 
147988e501aSMao Zhongyi static void pl050_realize(DeviceState *dev, Error **errp)
14849ab747fSPaolo Bonzini {
1493e5dd364SAndreas Färber     PL050State *s = PL050(dev);
150988e501aSMao Zhongyi     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
15149ab747fSPaolo Bonzini 
1521437c94bSPaolo Bonzini     memory_region_init_io(&s->iomem, OBJECT(s), &pl050_ops, s, "pl050", 0x1000);
153988e501aSMao Zhongyi     sysbus_init_mmio(sbd, &s->iomem);
154988e501aSMao Zhongyi     sysbus_init_irq(sbd, &s->irq);
1553e5dd364SAndreas Färber     if (s->is_mouse) {
15649ab747fSPaolo Bonzini         s->dev = ps2_mouse_init(pl050_update, s);
1573e5dd364SAndreas Färber     } else {
15849ab747fSPaolo Bonzini         s->dev = ps2_kbd_init(pl050_update, s);
1593e5dd364SAndreas Färber     }
16049ab747fSPaolo Bonzini }
16149ab747fSPaolo Bonzini 
1623e5dd364SAndreas Färber static void pl050_keyboard_init(Object *obj)
16349ab747fSPaolo Bonzini {
1643e5dd364SAndreas Färber     PL050State *s = PL050(obj);
1653e5dd364SAndreas Färber 
1663e5dd364SAndreas Färber     s->is_mouse = false;
16749ab747fSPaolo Bonzini }
16849ab747fSPaolo Bonzini 
1693e5dd364SAndreas Färber static void pl050_mouse_init(Object *obj)
17049ab747fSPaolo Bonzini {
1713e5dd364SAndreas Färber     PL050State *s = PL050(obj);
17249ab747fSPaolo Bonzini 
1733e5dd364SAndreas Färber     s->is_mouse = true;
17449ab747fSPaolo Bonzini }
17549ab747fSPaolo Bonzini 
17649ab747fSPaolo Bonzini static const TypeInfo pl050_kbd_info = {
17749ab747fSPaolo Bonzini     .name          = "pl050_keyboard",
1783e5dd364SAndreas Färber     .parent        = TYPE_PL050,
1793e5dd364SAndreas Färber     .instance_init = pl050_keyboard_init,
18049ab747fSPaolo Bonzini };
18149ab747fSPaolo Bonzini 
18249ab747fSPaolo Bonzini static const TypeInfo pl050_mouse_info = {
18349ab747fSPaolo Bonzini     .name          = "pl050_mouse",
1843e5dd364SAndreas Färber     .parent        = TYPE_PL050,
1853e5dd364SAndreas Färber     .instance_init = pl050_mouse_init,
1863e5dd364SAndreas Färber };
1873e5dd364SAndreas Färber 
1883e5dd364SAndreas Färber static void pl050_class_init(ObjectClass *oc, void *data)
1893e5dd364SAndreas Färber {
1903e5dd364SAndreas Färber     DeviceClass *dc = DEVICE_CLASS(oc);
1913e5dd364SAndreas Färber 
192988e501aSMao Zhongyi     dc->realize = pl050_realize;
1933e5dd364SAndreas Färber     dc->vmsd = &vmstate_pl050;
1943e5dd364SAndreas Färber }
1953e5dd364SAndreas Färber 
1963e5dd364SAndreas Färber static const TypeInfo pl050_type_info = {
1973e5dd364SAndreas Färber     .name          = TYPE_PL050,
19849ab747fSPaolo Bonzini     .parent        = TYPE_SYS_BUS_DEVICE,
199e607f25aSAndreas Färber     .instance_size = sizeof(PL050State),
2003e5dd364SAndreas Färber     .abstract      = true,
2013e5dd364SAndreas Färber     .class_init    = pl050_class_init,
20249ab747fSPaolo Bonzini };
20349ab747fSPaolo Bonzini 
20449ab747fSPaolo Bonzini static void pl050_register_types(void)
20549ab747fSPaolo Bonzini {
2063e5dd364SAndreas Färber     type_register_static(&pl050_type_info);
20749ab747fSPaolo Bonzini     type_register_static(&pl050_kbd_info);
20849ab747fSPaolo Bonzini     type_register_static(&pl050_mouse_info);
20949ab747fSPaolo Bonzini }
21049ab747fSPaolo Bonzini 
21149ab747fSPaolo Bonzini type_init(pl050_register_types)
212