xref: /openbmc/qemu/hw/input/pl050.c (revision 0b8fa32f551e863bb548a11394239239270dd3dc)
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"
1249ab747fSPaolo Bonzini #include "hw/input/ps2.h"
1303dd024fSPaolo Bonzini #include "qemu/log.h"
14*0b8fa32fSMarkus Armbruster #include "qemu/module.h"
1549ab747fSPaolo Bonzini 
163e5dd364SAndreas Färber #define TYPE_PL050 "pl050"
173e5dd364SAndreas Färber #define PL050(obj) OBJECT_CHECK(PL050State, (obj), TYPE_PL050)
183e5dd364SAndreas Färber 
19e607f25aSAndreas Färber typedef struct PL050State {
203e5dd364SAndreas Färber     SysBusDevice parent_obj;
213e5dd364SAndreas Färber 
2249ab747fSPaolo Bonzini     MemoryRegion iomem;
2349ab747fSPaolo Bonzini     void *dev;
2449ab747fSPaolo Bonzini     uint32_t cr;
2549ab747fSPaolo Bonzini     uint32_t clk;
2649ab747fSPaolo Bonzini     uint32_t last;
2749ab747fSPaolo Bonzini     int pending;
2849ab747fSPaolo Bonzini     qemu_irq irq;
293e5dd364SAndreas Färber     bool is_mouse;
30e607f25aSAndreas Färber } PL050State;
3149ab747fSPaolo Bonzini 
3249ab747fSPaolo Bonzini static const VMStateDescription vmstate_pl050 = {
3349ab747fSPaolo Bonzini     .name = "pl050",
3449ab747fSPaolo Bonzini     .version_id = 2,
3549ab747fSPaolo Bonzini     .minimum_version_id = 2,
3649ab747fSPaolo Bonzini     .fields = (VMStateField[]) {
37e607f25aSAndreas Färber         VMSTATE_UINT32(cr, PL050State),
38e607f25aSAndreas Färber         VMSTATE_UINT32(clk, PL050State),
39e607f25aSAndreas Färber         VMSTATE_UINT32(last, PL050State),
40e607f25aSAndreas Färber         VMSTATE_INT32(pending, PL050State),
4149ab747fSPaolo Bonzini         VMSTATE_END_OF_LIST()
4249ab747fSPaolo Bonzini     }
4349ab747fSPaolo Bonzini };
4449ab747fSPaolo Bonzini 
4549ab747fSPaolo Bonzini #define PL050_TXEMPTY         (1 << 6)
4649ab747fSPaolo Bonzini #define PL050_TXBUSY          (1 << 5)
4749ab747fSPaolo Bonzini #define PL050_RXFULL          (1 << 4)
4849ab747fSPaolo Bonzini #define PL050_RXBUSY          (1 << 3)
4949ab747fSPaolo Bonzini #define PL050_RXPARITY        (1 << 2)
5049ab747fSPaolo Bonzini #define PL050_KMIC            (1 << 1)
5149ab747fSPaolo Bonzini #define PL050_KMID            (1 << 0)
5249ab747fSPaolo Bonzini 
5349ab747fSPaolo Bonzini static const unsigned char pl050_id[] =
5449ab747fSPaolo Bonzini { 0x50, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
5549ab747fSPaolo Bonzini 
5649ab747fSPaolo Bonzini static void pl050_update(void *opaque, int level)
5749ab747fSPaolo Bonzini {
58e607f25aSAndreas Färber     PL050State *s = (PL050State *)opaque;
5949ab747fSPaolo Bonzini     int raise;
6049ab747fSPaolo Bonzini 
6149ab747fSPaolo Bonzini     s->pending = level;
6249ab747fSPaolo Bonzini     raise = (s->pending && (s->cr & 0x10) != 0)
6349ab747fSPaolo Bonzini             || (s->cr & 0x08) != 0;
6449ab747fSPaolo Bonzini     qemu_set_irq(s->irq, raise);
6549ab747fSPaolo Bonzini }
6649ab747fSPaolo Bonzini 
6749ab747fSPaolo Bonzini static uint64_t pl050_read(void *opaque, hwaddr offset,
6849ab747fSPaolo Bonzini                            unsigned size)
6949ab747fSPaolo Bonzini {
70e607f25aSAndreas Färber     PL050State *s = (PL050State *)opaque;
7149ab747fSPaolo Bonzini     if (offset >= 0xfe0 && offset < 0x1000)
7249ab747fSPaolo Bonzini         return pl050_id[(offset - 0xfe0) >> 2];
7349ab747fSPaolo Bonzini 
7449ab747fSPaolo Bonzini     switch (offset >> 2) {
7549ab747fSPaolo Bonzini     case 0: /* KMICR */
7649ab747fSPaolo Bonzini         return s->cr;
7749ab747fSPaolo Bonzini     case 1: /* KMISTAT */
7849ab747fSPaolo Bonzini         {
7949ab747fSPaolo Bonzini             uint8_t val;
8049ab747fSPaolo Bonzini             uint32_t stat;
8149ab747fSPaolo Bonzini 
8249ab747fSPaolo Bonzini             val = s->last;
8349ab747fSPaolo Bonzini             val = val ^ (val >> 4);
8449ab747fSPaolo Bonzini             val = val ^ (val >> 2);
8549ab747fSPaolo Bonzini             val = (val ^ (val >> 1)) & 1;
8649ab747fSPaolo Bonzini 
8749ab747fSPaolo Bonzini             stat = PL050_TXEMPTY;
8849ab747fSPaolo Bonzini             if (val)
8949ab747fSPaolo Bonzini                 stat |= PL050_RXPARITY;
9049ab747fSPaolo Bonzini             if (s->pending)
9149ab747fSPaolo Bonzini                 stat |= PL050_RXFULL;
9249ab747fSPaolo Bonzini 
9349ab747fSPaolo Bonzini             return stat;
9449ab747fSPaolo Bonzini         }
9549ab747fSPaolo Bonzini     case 2: /* KMIDATA */
9649ab747fSPaolo Bonzini         if (s->pending)
9749ab747fSPaolo Bonzini             s->last = ps2_read_data(s->dev);
9849ab747fSPaolo Bonzini         return s->last;
9949ab747fSPaolo Bonzini     case 3: /* KMICLKDIV */
10049ab747fSPaolo Bonzini         return s->clk;
10149ab747fSPaolo Bonzini     case 4: /* KMIIR */
10249ab747fSPaolo Bonzini         return s->pending | 2;
10349ab747fSPaolo Bonzini     default:
10449ab747fSPaolo Bonzini         qemu_log_mask(LOG_GUEST_ERROR,
10549ab747fSPaolo Bonzini                       "pl050_read: Bad offset %x\n", (int)offset);
10649ab747fSPaolo Bonzini         return 0;
10749ab747fSPaolo Bonzini     }
10849ab747fSPaolo Bonzini }
10949ab747fSPaolo Bonzini 
11049ab747fSPaolo Bonzini static void pl050_write(void *opaque, hwaddr offset,
11149ab747fSPaolo Bonzini                         uint64_t value, unsigned size)
11249ab747fSPaolo Bonzini {
113e607f25aSAndreas Färber     PL050State *s = (PL050State *)opaque;
11449ab747fSPaolo Bonzini     switch (offset >> 2) {
11549ab747fSPaolo Bonzini     case 0: /* KMICR */
11649ab747fSPaolo Bonzini         s->cr = value;
11749ab747fSPaolo Bonzini         pl050_update(s, s->pending);
11849ab747fSPaolo Bonzini         /* ??? Need to implement the enable/disable bit.  */
11949ab747fSPaolo Bonzini         break;
12049ab747fSPaolo Bonzini     case 2: /* KMIDATA */
12149ab747fSPaolo Bonzini         /* ??? This should toggle the TX interrupt line.  */
12249ab747fSPaolo Bonzini         /* ??? This means kbd/mouse can block each other.  */
12349ab747fSPaolo Bonzini         if (s->is_mouse) {
12449ab747fSPaolo Bonzini             ps2_write_mouse(s->dev, value);
12549ab747fSPaolo Bonzini         } else {
12649ab747fSPaolo Bonzini             ps2_write_keyboard(s->dev, value);
12749ab747fSPaolo Bonzini         }
12849ab747fSPaolo Bonzini         break;
12949ab747fSPaolo Bonzini     case 3: /* KMICLKDIV */
13049ab747fSPaolo Bonzini         s->clk = value;
13149ab747fSPaolo Bonzini         return;
13249ab747fSPaolo Bonzini     default:
13349ab747fSPaolo Bonzini         qemu_log_mask(LOG_GUEST_ERROR,
13449ab747fSPaolo Bonzini                       "pl050_write: Bad offset %x\n", (int)offset);
13549ab747fSPaolo Bonzini     }
13649ab747fSPaolo Bonzini }
13749ab747fSPaolo Bonzini static const MemoryRegionOps pl050_ops = {
13849ab747fSPaolo Bonzini     .read = pl050_read,
13949ab747fSPaolo Bonzini     .write = pl050_write,
14049ab747fSPaolo Bonzini     .endianness = DEVICE_NATIVE_ENDIAN,
14149ab747fSPaolo Bonzini };
14249ab747fSPaolo Bonzini 
143988e501aSMao Zhongyi static void pl050_realize(DeviceState *dev, Error **errp)
14449ab747fSPaolo Bonzini {
1453e5dd364SAndreas Färber     PL050State *s = PL050(dev);
146988e501aSMao Zhongyi     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
14749ab747fSPaolo Bonzini 
1481437c94bSPaolo Bonzini     memory_region_init_io(&s->iomem, OBJECT(s), &pl050_ops, s, "pl050", 0x1000);
149988e501aSMao Zhongyi     sysbus_init_mmio(sbd, &s->iomem);
150988e501aSMao Zhongyi     sysbus_init_irq(sbd, &s->irq);
1513e5dd364SAndreas Färber     if (s->is_mouse) {
15249ab747fSPaolo Bonzini         s->dev = ps2_mouse_init(pl050_update, s);
1533e5dd364SAndreas Färber     } else {
15449ab747fSPaolo Bonzini         s->dev = ps2_kbd_init(pl050_update, s);
1553e5dd364SAndreas Färber     }
15649ab747fSPaolo Bonzini }
15749ab747fSPaolo Bonzini 
1583e5dd364SAndreas Färber static void pl050_keyboard_init(Object *obj)
15949ab747fSPaolo Bonzini {
1603e5dd364SAndreas Färber     PL050State *s = PL050(obj);
1613e5dd364SAndreas Färber 
1623e5dd364SAndreas Färber     s->is_mouse = false;
16349ab747fSPaolo Bonzini }
16449ab747fSPaolo Bonzini 
1653e5dd364SAndreas Färber static void pl050_mouse_init(Object *obj)
16649ab747fSPaolo Bonzini {
1673e5dd364SAndreas Färber     PL050State *s = PL050(obj);
16849ab747fSPaolo Bonzini 
1693e5dd364SAndreas Färber     s->is_mouse = true;
17049ab747fSPaolo Bonzini }
17149ab747fSPaolo Bonzini 
17249ab747fSPaolo Bonzini static const TypeInfo pl050_kbd_info = {
17349ab747fSPaolo Bonzini     .name          = "pl050_keyboard",
1743e5dd364SAndreas Färber     .parent        = TYPE_PL050,
1753e5dd364SAndreas Färber     .instance_init = pl050_keyboard_init,
17649ab747fSPaolo Bonzini };
17749ab747fSPaolo Bonzini 
17849ab747fSPaolo Bonzini static const TypeInfo pl050_mouse_info = {
17949ab747fSPaolo Bonzini     .name          = "pl050_mouse",
1803e5dd364SAndreas Färber     .parent        = TYPE_PL050,
1813e5dd364SAndreas Färber     .instance_init = pl050_mouse_init,
1823e5dd364SAndreas Färber };
1833e5dd364SAndreas Färber 
1843e5dd364SAndreas Färber static void pl050_class_init(ObjectClass *oc, void *data)
1853e5dd364SAndreas Färber {
1863e5dd364SAndreas Färber     DeviceClass *dc = DEVICE_CLASS(oc);
1873e5dd364SAndreas Färber 
188988e501aSMao Zhongyi     dc->realize = pl050_realize;
1893e5dd364SAndreas Färber     dc->vmsd = &vmstate_pl050;
1903e5dd364SAndreas Färber }
1913e5dd364SAndreas Färber 
1923e5dd364SAndreas Färber static const TypeInfo pl050_type_info = {
1933e5dd364SAndreas Färber     .name          = TYPE_PL050,
19449ab747fSPaolo Bonzini     .parent        = TYPE_SYS_BUS_DEVICE,
195e607f25aSAndreas Färber     .instance_size = sizeof(PL050State),
1963e5dd364SAndreas Färber     .abstract      = true,
1973e5dd364SAndreas Färber     .class_init    = pl050_class_init,
19849ab747fSPaolo Bonzini };
19949ab747fSPaolo Bonzini 
20049ab747fSPaolo Bonzini static void pl050_register_types(void)
20149ab747fSPaolo Bonzini {
2023e5dd364SAndreas Färber     type_register_static(&pl050_type_info);
20349ab747fSPaolo Bonzini     type_register_static(&pl050_kbd_info);
20449ab747fSPaolo Bonzini     type_register_static(&pl050_mouse_info);
20549ab747fSPaolo Bonzini }
20649ab747fSPaolo Bonzini 
20749ab747fSPaolo Bonzini type_init(pl050_register_types)
208