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"
18 #define TYPE_PL050 "pl050"
19 #define PL050(obj) OBJECT_CHECK(PL050State, (obj), TYPE_PL050)
21 typedef struct PL050State
{
22 SysBusDevice parent_obj
;
34 static const VMStateDescription vmstate_pl050
= {
37 .minimum_version_id
= 2,
38 .fields
= (VMStateField
[]) {
39 VMSTATE_UINT32(cr
, PL050State
),
40 VMSTATE_UINT32(clk
, PL050State
),
41 VMSTATE_UINT32(last
, PL050State
),
42 VMSTATE_INT32(pending
, PL050State
),
47 #define PL050_TXEMPTY (1 << 6)
48 #define PL050_TXBUSY (1 << 5)
49 #define PL050_RXFULL (1 << 4)
50 #define PL050_RXBUSY (1 << 3)
51 #define PL050_RXPARITY (1 << 2)
52 #define PL050_KMIC (1 << 1)
53 #define PL050_KMID (1 << 0)
55 static const unsigned char pl050_id
[] =
56 { 0x50, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
58 static void pl050_update(void *opaque
, int level
)
60 PL050State
*s
= (PL050State
*)opaque
;
64 raise
= (s
->pending
&& (s
->cr
& 0x10) != 0)
65 || (s
->cr
& 0x08) != 0;
66 qemu_set_irq(s
->irq
, raise
);
69 static uint64_t pl050_read(void *opaque
, hwaddr offset
,
72 PL050State
*s
= (PL050State
*)opaque
;
73 if (offset
>= 0xfe0 && offset
< 0x1000)
74 return pl050_id
[(offset
- 0xfe0) >> 2];
76 switch (offset
>> 2) {
85 val
= val
^ (val
>> 4);
86 val
= val
^ (val
>> 2);
87 val
= (val
^ (val
>> 1)) & 1;
91 stat
|= PL050_RXPARITY
;
99 s
->last
= ps2_read_data(s
->dev
);
101 case 3: /* KMICLKDIV */
104 return s
->pending
| 2;
106 qemu_log_mask(LOG_GUEST_ERROR
,
107 "pl050_read: Bad offset %x\n", (int)offset
);
112 static void pl050_write(void *opaque
, hwaddr offset
,
113 uint64_t value
, unsigned size
)
115 PL050State
*s
= (PL050State
*)opaque
;
116 switch (offset
>> 2) {
119 pl050_update(s
, s
->pending
);
120 /* ??? Need to implement the enable/disable bit. */
122 case 2: /* KMIDATA */
123 /* ??? This should toggle the TX interrupt line. */
124 /* ??? This means kbd/mouse can block each other. */
126 ps2_write_mouse(s
->dev
, value
);
128 ps2_write_keyboard(s
->dev
, value
);
131 case 3: /* KMICLKDIV */
135 qemu_log_mask(LOG_GUEST_ERROR
,
136 "pl050_write: Bad offset %x\n", (int)offset
);
139 static const MemoryRegionOps pl050_ops
= {
141 .write
= pl050_write
,
142 .endianness
= DEVICE_NATIVE_ENDIAN
,
145 static void pl050_realize(DeviceState
*dev
, Error
**errp
)
147 PL050State
*s
= PL050(dev
);
148 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
150 memory_region_init_io(&s
->iomem
, OBJECT(s
), &pl050_ops
, s
, "pl050", 0x1000);
151 sysbus_init_mmio(sbd
, &s
->iomem
);
152 sysbus_init_irq(sbd
, &s
->irq
);
154 s
->dev
= ps2_mouse_init(pl050_update
, s
);
156 s
->dev
= ps2_kbd_init(pl050_update
, s
);
160 static void pl050_keyboard_init(Object
*obj
)
162 PL050State
*s
= PL050(obj
);
167 static void pl050_mouse_init(Object
*obj
)
169 PL050State
*s
= PL050(obj
);
174 static const TypeInfo pl050_kbd_info
= {
175 .name
= "pl050_keyboard",
176 .parent
= TYPE_PL050
,
177 .instance_init
= pl050_keyboard_init
,
180 static const TypeInfo pl050_mouse_info
= {
181 .name
= "pl050_mouse",
182 .parent
= TYPE_PL050
,
183 .instance_init
= pl050_mouse_init
,
186 static void pl050_class_init(ObjectClass
*oc
, void *data
)
188 DeviceClass
*dc
= DEVICE_CLASS(oc
);
190 dc
->realize
= pl050_realize
;
191 dc
->vmsd
= &vmstate_pl050
;
194 static const TypeInfo pl050_type_info
= {
196 .parent
= TYPE_SYS_BUS_DEVICE
,
197 .instance_size
= sizeof(PL050State
),
199 .class_init
= pl050_class_init
,
202 static void pl050_register_types(void)
204 type_register_static(&pl050_type_info
);
205 type_register_static(&pl050_kbd_info
);
206 type_register_static(&pl050_mouse_info
);
209 type_init(pl050_register_types
)