Merge remote-tracking branch 'remotes/bonzini/tags/for-upstream' into staging
[qemu/ar7.git] / hw / input / pl050.c
blobc1b08d5a407812d205e38a8942b00674bdcbf852
1 /*
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.
8 */
10 #include "hw/sysbus.h"
11 #include "hw/input/ps2.h"
13 #define TYPE_PL050 "pl050"
14 #define PL050(obj) OBJECT_CHECK(PL050State, (obj), TYPE_PL050)
16 typedef struct PL050State {
17 SysBusDevice parent_obj;
19 MemoryRegion iomem;
20 void *dev;
21 uint32_t cr;
22 uint32_t clk;
23 uint32_t last;
24 int pending;
25 qemu_irq irq;
26 bool is_mouse;
27 } PL050State;
29 static const VMStateDescription vmstate_pl050 = {
30 .name = "pl050",
31 .version_id = 2,
32 .minimum_version_id = 2,
33 .fields = (VMStateField[]) {
34 VMSTATE_UINT32(cr, PL050State),
35 VMSTATE_UINT32(clk, PL050State),
36 VMSTATE_UINT32(last, PL050State),
37 VMSTATE_INT32(pending, PL050State),
38 VMSTATE_END_OF_LIST()
42 #define PL050_TXEMPTY (1 << 6)
43 #define PL050_TXBUSY (1 << 5)
44 #define PL050_RXFULL (1 << 4)
45 #define PL050_RXBUSY (1 << 3)
46 #define PL050_RXPARITY (1 << 2)
47 #define PL050_KMIC (1 << 1)
48 #define PL050_KMID (1 << 0)
50 static const unsigned char pl050_id[] =
51 { 0x50, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
53 static void pl050_update(void *opaque, int level)
55 PL050State *s = (PL050State *)opaque;
56 int raise;
58 s->pending = level;
59 raise = (s->pending && (s->cr & 0x10) != 0)
60 || (s->cr & 0x08) != 0;
61 qemu_set_irq(s->irq, raise);
64 static uint64_t pl050_read(void *opaque, hwaddr offset,
65 unsigned size)
67 PL050State *s = (PL050State *)opaque;
68 if (offset >= 0xfe0 && offset < 0x1000)
69 return pl050_id[(offset - 0xfe0) >> 2];
71 switch (offset >> 2) {
72 case 0: /* KMICR */
73 return s->cr;
74 case 1: /* KMISTAT */
76 uint8_t val;
77 uint32_t stat;
79 val = s->last;
80 val = val ^ (val >> 4);
81 val = val ^ (val >> 2);
82 val = (val ^ (val >> 1)) & 1;
84 stat = PL050_TXEMPTY;
85 if (val)
86 stat |= PL050_RXPARITY;
87 if (s->pending)
88 stat |= PL050_RXFULL;
90 return stat;
92 case 2: /* KMIDATA */
93 if (s->pending)
94 s->last = ps2_read_data(s->dev);
95 return s->last;
96 case 3: /* KMICLKDIV */
97 return s->clk;
98 case 4: /* KMIIR */
99 return s->pending | 2;
100 default:
101 qemu_log_mask(LOG_GUEST_ERROR,
102 "pl050_read: Bad offset %x\n", (int)offset);
103 return 0;
107 static void pl050_write(void *opaque, hwaddr offset,
108 uint64_t value, unsigned size)
110 PL050State *s = (PL050State *)opaque;
111 switch (offset >> 2) {
112 case 0: /* KMICR */
113 s->cr = value;
114 pl050_update(s, s->pending);
115 /* ??? Need to implement the enable/disable bit. */
116 break;
117 case 2: /* KMIDATA */
118 /* ??? This should toggle the TX interrupt line. */
119 /* ??? This means kbd/mouse can block each other. */
120 if (s->is_mouse) {
121 ps2_write_mouse(s->dev, value);
122 } else {
123 ps2_write_keyboard(s->dev, value);
125 break;
126 case 3: /* KMICLKDIV */
127 s->clk = value;
128 return;
129 default:
130 qemu_log_mask(LOG_GUEST_ERROR,
131 "pl050_write: Bad offset %x\n", (int)offset);
134 static const MemoryRegionOps pl050_ops = {
135 .read = pl050_read,
136 .write = pl050_write,
137 .endianness = DEVICE_NATIVE_ENDIAN,
140 static int pl050_initfn(SysBusDevice *dev)
142 PL050State *s = PL050(dev);
144 memory_region_init_io(&s->iomem, OBJECT(s), &pl050_ops, s, "pl050", 0x1000);
145 sysbus_init_mmio(dev, &s->iomem);
146 sysbus_init_irq(dev, &s->irq);
147 if (s->is_mouse) {
148 s->dev = ps2_mouse_init(pl050_update, s);
149 } else {
150 s->dev = ps2_kbd_init(pl050_update, s);
152 return 0;
155 static void pl050_keyboard_init(Object *obj)
157 PL050State *s = PL050(obj);
159 s->is_mouse = false;
162 static void pl050_mouse_init(Object *obj)
164 PL050State *s = PL050(obj);
166 s->is_mouse = true;
169 static const TypeInfo pl050_kbd_info = {
170 .name = "pl050_keyboard",
171 .parent = TYPE_PL050,
172 .instance_init = pl050_keyboard_init,
175 static const TypeInfo pl050_mouse_info = {
176 .name = "pl050_mouse",
177 .parent = TYPE_PL050,
178 .instance_init = pl050_mouse_init,
181 static void pl050_class_init(ObjectClass *oc, void *data)
183 DeviceClass *dc = DEVICE_CLASS(oc);
184 SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(oc);
186 sdc->init = pl050_initfn;
187 dc->vmsd = &vmstate_pl050;
190 static const TypeInfo pl050_type_info = {
191 .name = TYPE_PL050,
192 .parent = TYPE_SYS_BUS_DEVICE,
193 .instance_size = sizeof(PL050State),
194 .abstract = true,
195 .class_init = pl050_class_init,
198 static void pl050_register_types(void)
200 type_register_static(&pl050_type_info);
201 type_register_static(&pl050_kbd_info);
202 type_register_static(&pl050_mouse_info);
205 type_init(pl050_register_types)