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.
12 * + sysbus MMIO region 0: MemoryRegion defining the PL050 registers
13 * + Named GPIO input "ps2-input-irq": set to 1 if the downstream PS2 device
14 * has asserted its irq
15 * + sysbus IRQ 0: PL050 output irq
18 #include "qemu/osdep.h"
19 #include "hw/sysbus.h"
20 #include "migration/vmstate.h"
21 #include "hw/input/ps2.h"
24 #include "qemu/module.h"
25 #include "qom/object.h"
27 #define TYPE_PL050 "pl050"
28 OBJECT_DECLARE_SIMPLE_TYPE(PL050State
, PL050
)
31 SysBusDevice parent_obj
;
43 static const VMStateDescription vmstate_pl050
= {
46 .minimum_version_id
= 2,
47 .fields
= (VMStateField
[]) {
48 VMSTATE_UINT32(cr
, PL050State
),
49 VMSTATE_UINT32(clk
, PL050State
),
50 VMSTATE_UINT32(last
, PL050State
),
51 VMSTATE_INT32(pending
, PL050State
),
56 #define PL050_TXEMPTY (1 << 6)
57 #define PL050_TXBUSY (1 << 5)
58 #define PL050_RXFULL (1 << 4)
59 #define PL050_RXBUSY (1 << 3)
60 #define PL050_RXPARITY (1 << 2)
61 #define PL050_KMIC (1 << 1)
62 #define PL050_KMID (1 << 0)
64 static const unsigned char pl050_id
[] = {
65 0x50, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1
68 static void pl050_update_irq(PL050State
*s
)
70 int level
= (s
->pending
&& (s
->cr
& 0x10) != 0)
71 || (s
->cr
& 0x08) != 0;
73 qemu_set_irq(s
->irq
, level
);
76 static void pl050_set_irq(void *opaque
, int n
, int level
)
78 PL050State
*s
= (PL050State
*)opaque
;
84 static uint64_t pl050_read(void *opaque
, hwaddr offset
,
87 PL050State
*s
= (PL050State
*)opaque
;
89 if (offset
>= 0xfe0 && offset
< 0x1000) {
90 return pl050_id
[(offset
- 0xfe0) >> 2];
93 switch (offset
>> 2) {
102 val
= val
^ (val
>> 4);
103 val
= val
^ (val
>> 2);
104 val
= (val
^ (val
>> 1)) & 1;
106 stat
= PL050_TXEMPTY
;
108 stat
|= PL050_RXPARITY
;
111 stat
|= PL050_RXFULL
;
116 case 2: /* KMIDATA */
118 s
->last
= ps2_read_data(s
->dev
);
121 case 3: /* KMICLKDIV */
124 return s
->pending
| 2;
126 qemu_log_mask(LOG_GUEST_ERROR
,
127 "pl050_read: Bad offset %x\n", (int)offset
);
132 static void pl050_write(void *opaque
, hwaddr offset
,
133 uint64_t value
, unsigned size
)
135 PL050State
*s
= (PL050State
*)opaque
;
137 switch (offset
>> 2) {
141 /* ??? Need to implement the enable/disable bit. */
143 case 2: /* KMIDATA */
144 /* ??? This should toggle the TX interrupt line. */
145 /* ??? This means kbd/mouse can block each other. */
147 ps2_write_mouse(s
->dev
, value
);
149 ps2_write_keyboard(s
->dev
, value
);
152 case 3: /* KMICLKDIV */
156 qemu_log_mask(LOG_GUEST_ERROR
,
157 "pl050_write: Bad offset %x\n", (int)offset
);
160 static const MemoryRegionOps pl050_ops
= {
162 .write
= pl050_write
,
163 .endianness
= DEVICE_NATIVE_ENDIAN
,
166 static void pl050_realize(DeviceState
*dev
, Error
**errp
)
168 PL050State
*s
= PL050(dev
);
169 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
171 memory_region_init_io(&s
->iomem
, OBJECT(s
), &pl050_ops
, s
, "pl050", 0x1000);
172 sysbus_init_mmio(sbd
, &s
->iomem
);
173 sysbus_init_irq(sbd
, &s
->irq
);
175 s
->dev
= ps2_mouse_init(NULL
, NULL
);
177 s
->dev
= ps2_kbd_init(NULL
, NULL
);
179 qdev_connect_gpio_out(DEVICE(s
->dev
), PS2_DEVICE_IRQ
,
180 qdev_get_gpio_in_named(dev
, "ps2-input-irq", 0));
183 static void pl050_keyboard_init(Object
*obj
)
185 PL050State
*s
= PL050(obj
);
190 static void pl050_mouse_init(Object
*obj
)
192 PL050State
*s
= PL050(obj
);
197 static const TypeInfo pl050_kbd_info
= {
198 .name
= "pl050_keyboard",
199 .parent
= TYPE_PL050
,
200 .instance_init
= pl050_keyboard_init
,
203 static const TypeInfo pl050_mouse_info
= {
204 .name
= "pl050_mouse",
205 .parent
= TYPE_PL050
,
206 .instance_init
= pl050_mouse_init
,
209 static void pl050_init(Object
*obj
)
211 qdev_init_gpio_in_named(DEVICE(obj
), pl050_set_irq
, "ps2-input-irq", 1);
214 static void pl050_class_init(ObjectClass
*oc
, void *data
)
216 DeviceClass
*dc
= DEVICE_CLASS(oc
);
218 dc
->realize
= pl050_realize
;
219 dc
->vmsd
= &vmstate_pl050
;
222 static const TypeInfo pl050_type_info
= {
224 .parent
= TYPE_SYS_BUS_DEVICE
,
225 .instance_init
= pl050_init
,
226 .instance_size
= sizeof(PL050State
),
228 .class_init
= pl050_class_init
,
231 static void pl050_register_types(void)
233 type_register_static(&pl050_type_info
);
234 type_register_static(&pl050_kbd_info
);
235 type_register_static(&pl050_mouse_info
);
238 type_init(pl050_register_types
)