2 * Arm PrimeCell PL050 Keyboard / Mouse Interface
4 * Copyright (c) 2006-2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GPL.
10 #include "qemu/osdep.h"
11 #include "hw/sysbus.h"
12 #include "migration/vmstate.h"
13 #include "hw/input/ps2.h"
16 #include "qemu/module.h"
17 #include "qom/object.h"
19 #define TYPE_PL050 "pl050"
20 typedef struct PL050State PL050State;
21 #define PL050(obj) OBJECT_CHECK(PL050State, (obj), TYPE_PL050)
24 SysBusDevice parent_obj;
36 static const VMStateDescription vmstate_pl050 = {
39 .minimum_version_id = 2,
40 .fields = (VMStateField[]) {
41 VMSTATE_UINT32(cr, PL050State),
42 VMSTATE_UINT32(clk, PL050State),
43 VMSTATE_UINT32(last, PL050State),
44 VMSTATE_INT32(pending, PL050State),
49 #define PL050_TXEMPTY (1 << 6)
50 #define PL050_TXBUSY (1 << 5)
51 #define PL050_RXFULL (1 << 4)
52 #define PL050_RXBUSY (1 << 3)
53 #define PL050_RXPARITY (1 << 2)
54 #define PL050_KMIC (1 << 1)
55 #define PL050_KMID (1 << 0)
57 static const unsigned char pl050_id[] =
58 { 0x50, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
60 static void pl050_update(void *opaque, int level)
62 PL050State *s = (PL050State *)opaque;
66 raise = (s->pending && (s->cr & 0x10) != 0)
67 || (s->cr & 0x08) != 0;
68 qemu_set_irq(s->irq, raise);
71 static uint64_t pl050_read(void *opaque, hwaddr offset,
74 PL050State *s = (PL050State *)opaque;
75 if (offset >= 0xfe0 && offset < 0x1000)
76 return pl050_id[(offset - 0xfe0) >> 2];
78 switch (offset >> 2) {
87 val = val ^ (val >> 4);
88 val = val ^ (val >> 2);
89 val = (val ^ (val >> 1)) & 1;
93 stat |= PL050_RXPARITY;
101 s->last = ps2_read_data(s->dev);
103 case 3: /* KMICLKDIV */
106 return s->pending | 2;
108 qemu_log_mask(LOG_GUEST_ERROR,
109 "pl050_read: Bad offset %x\n", (int)offset);
114 static void pl050_write(void *opaque, hwaddr offset,
115 uint64_t value, unsigned size)
117 PL050State *s = (PL050State *)opaque;
118 switch (offset >> 2) {
121 pl050_update(s, s->pending);
122 /* ??? Need to implement the enable/disable bit. */
124 case 2: /* KMIDATA */
125 /* ??? This should toggle the TX interrupt line. */
126 /* ??? This means kbd/mouse can block each other. */
128 ps2_write_mouse(s->dev, value);
130 ps2_write_keyboard(s->dev, value);
133 case 3: /* KMICLKDIV */
137 qemu_log_mask(LOG_GUEST_ERROR,
138 "pl050_write: Bad offset %x\n", (int)offset);
141 static const MemoryRegionOps pl050_ops = {
143 .write = pl050_write,
144 .endianness = DEVICE_NATIVE_ENDIAN,
147 static void pl050_realize(DeviceState *dev, Error **errp)
149 PL050State *s = PL050(dev);
150 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
152 memory_region_init_io(&s->iomem, OBJECT(s), &pl050_ops, s, "pl050", 0x1000);
153 sysbus_init_mmio(sbd, &s->iomem);
154 sysbus_init_irq(sbd, &s->irq);
156 s->dev = ps2_mouse_init(pl050_update, s);
158 s->dev = ps2_kbd_init(pl050_update, s);
162 static void pl050_keyboard_init(Object *obj)
164 PL050State *s = PL050(obj);
169 static void pl050_mouse_init(Object *obj)
171 PL050State *s = PL050(obj);
176 static const TypeInfo pl050_kbd_info = {
177 .name = "pl050_keyboard",
178 .parent = TYPE_PL050,
179 .instance_init = pl050_keyboard_init,
182 static const TypeInfo pl050_mouse_info = {
183 .name = "pl050_mouse",
184 .parent = TYPE_PL050,
185 .instance_init = pl050_mouse_init,
188 static void pl050_class_init(ObjectClass *oc, void *data)
190 DeviceClass *dc = DEVICE_CLASS(oc);
192 dc->realize = pl050_realize;
193 dc->vmsd = &vmstate_pl050;
196 static const TypeInfo pl050_type_info = {
198 .parent = TYPE_SYS_BUS_DEVICE,
199 .instance_size = sizeof(PL050State),
201 .class_init = pl050_class_init,
204 static void pl050_register_types(void)
206 type_register_static(&pl050_type_info);
207 type_register_static(&pl050_kbd_info);
208 type_register_static(&pl050_mouse_info);
211 type_init(pl050_register_types)