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 "hw/input/ps2.h"
15 #define TYPE_PL050 "pl050"
16 #define PL050(obj) OBJECT_CHECK(PL050State, (obj), TYPE_PL050)
18 typedef struct PL050State {
19 SysBusDevice parent_obj;
31 static const VMStateDescription vmstate_pl050 = {
34 .minimum_version_id = 2,
35 .fields = (VMStateField[]) {
36 VMSTATE_UINT32(cr, PL050State),
37 VMSTATE_UINT32(clk, PL050State),
38 VMSTATE_UINT32(last, PL050State),
39 VMSTATE_INT32(pending, PL050State),
44 #define PL050_TXEMPTY (1 << 6)
45 #define PL050_TXBUSY (1 << 5)
46 #define PL050_RXFULL (1 << 4)
47 #define PL050_RXBUSY (1 << 3)
48 #define PL050_RXPARITY (1 << 2)
49 #define PL050_KMIC (1 << 1)
50 #define PL050_KMID (1 << 0)
52 static const unsigned char pl050_id[] =
53 { 0x50, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
55 static void pl050_update(void *opaque, int level)
57 PL050State *s = (PL050State *)opaque;
61 raise = (s->pending && (s->cr & 0x10) != 0)
62 || (s->cr & 0x08) != 0;
63 qemu_set_irq(s->irq, raise);
66 static uint64_t pl050_read(void *opaque, hwaddr offset,
69 PL050State *s = (PL050State *)opaque;
70 if (offset >= 0xfe0 && offset < 0x1000)
71 return pl050_id[(offset - 0xfe0) >> 2];
73 switch (offset >> 2) {
82 val = val ^ (val >> 4);
83 val = val ^ (val >> 2);
84 val = (val ^ (val >> 1)) & 1;
88 stat |= PL050_RXPARITY;
96 s->last = ps2_read_data(s->dev);
98 case 3: /* KMICLKDIV */
101 return s->pending | 2;
103 qemu_log_mask(LOG_GUEST_ERROR,
104 "pl050_read: Bad offset %x\n", (int)offset);
109 static void pl050_write(void *opaque, hwaddr offset,
110 uint64_t value, unsigned size)
112 PL050State *s = (PL050State *)opaque;
113 switch (offset >> 2) {
116 pl050_update(s, s->pending);
117 /* ??? Need to implement the enable/disable bit. */
119 case 2: /* KMIDATA */
120 /* ??? This should toggle the TX interrupt line. */
121 /* ??? This means kbd/mouse can block each other. */
123 ps2_write_mouse(s->dev, value);
125 ps2_write_keyboard(s->dev, value);
128 case 3: /* KMICLKDIV */
132 qemu_log_mask(LOG_GUEST_ERROR,
133 "pl050_write: Bad offset %x\n", (int)offset);
136 static const MemoryRegionOps pl050_ops = {
138 .write = pl050_write,
139 .endianness = DEVICE_NATIVE_ENDIAN,
142 static int pl050_initfn(SysBusDevice *dev)
144 PL050State *s = PL050(dev);
146 memory_region_init_io(&s->iomem, OBJECT(s), &pl050_ops, s, "pl050", 0x1000);
147 sysbus_init_mmio(dev, &s->iomem);
148 sysbus_init_irq(dev, &s->irq);
150 s->dev = ps2_mouse_init(pl050_update, s);
152 s->dev = ps2_kbd_init(pl050_update, s);
157 static void pl050_keyboard_init(Object *obj)
159 PL050State *s = PL050(obj);
164 static void pl050_mouse_init(Object *obj)
166 PL050State *s = PL050(obj);
171 static const TypeInfo pl050_kbd_info = {
172 .name = "pl050_keyboard",
173 .parent = TYPE_PL050,
174 .instance_init = pl050_keyboard_init,
177 static const TypeInfo pl050_mouse_info = {
178 .name = "pl050_mouse",
179 .parent = TYPE_PL050,
180 .instance_init = pl050_mouse_init,
183 static void pl050_class_init(ObjectClass *oc, void *data)
185 DeviceClass *dc = DEVICE_CLASS(oc);
186 SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(oc);
188 sdc->init = pl050_initfn;
189 dc->vmsd = &vmstate_pl050;
192 static const TypeInfo pl050_type_info = {
194 .parent = TYPE_SYS_BUS_DEVICE,
195 .instance_size = sizeof(PL050State),
197 .class_init = pl050_class_init,
200 static void pl050_register_types(void)
202 type_register_static(&pl050_type_info);
203 type_register_static(&pl050_kbd_info);
204 type_register_static(&pl050_mouse_info);
207 type_init(pl050_register_types)