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"
14 #define TYPE_PL050 "pl050"
15 #define PL050(obj) OBJECT_CHECK(PL050State, (obj), TYPE_PL050)
17 typedef struct PL050State
{
18 SysBusDevice parent_obj
;
30 static const VMStateDescription vmstate_pl050
= {
33 .minimum_version_id
= 2,
34 .fields
= (VMStateField
[]) {
35 VMSTATE_UINT32(cr
, PL050State
),
36 VMSTATE_UINT32(clk
, PL050State
),
37 VMSTATE_UINT32(last
, PL050State
),
38 VMSTATE_INT32(pending
, PL050State
),
43 #define PL050_TXEMPTY (1 << 6)
44 #define PL050_TXBUSY (1 << 5)
45 #define PL050_RXFULL (1 << 4)
46 #define PL050_RXBUSY (1 << 3)
47 #define PL050_RXPARITY (1 << 2)
48 #define PL050_KMIC (1 << 1)
49 #define PL050_KMID (1 << 0)
51 static const unsigned char pl050_id
[] =
52 { 0x50, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
54 static void pl050_update(void *opaque
, int level
)
56 PL050State
*s
= (PL050State
*)opaque
;
60 raise
= (s
->pending
&& (s
->cr
& 0x10) != 0)
61 || (s
->cr
& 0x08) != 0;
62 qemu_set_irq(s
->irq
, raise
);
65 static uint64_t pl050_read(void *opaque
, hwaddr offset
,
68 PL050State
*s
= (PL050State
*)opaque
;
69 if (offset
>= 0xfe0 && offset
< 0x1000)
70 return pl050_id
[(offset
- 0xfe0) >> 2];
72 switch (offset
>> 2) {
81 val
= val
^ (val
>> 4);
82 val
= val
^ (val
>> 2);
83 val
= (val
^ (val
>> 1)) & 1;
87 stat
|= PL050_RXPARITY
;
95 s
->last
= ps2_read_data(s
->dev
);
97 case 3: /* KMICLKDIV */
100 return s
->pending
| 2;
102 qemu_log_mask(LOG_GUEST_ERROR
,
103 "pl050_read: Bad offset %x\n", (int)offset
);
108 static void pl050_write(void *opaque
, hwaddr offset
,
109 uint64_t value
, unsigned size
)
111 PL050State
*s
= (PL050State
*)opaque
;
112 switch (offset
>> 2) {
115 pl050_update(s
, s
->pending
);
116 /* ??? Need to implement the enable/disable bit. */
118 case 2: /* KMIDATA */
119 /* ??? This should toggle the TX interrupt line. */
120 /* ??? This means kbd/mouse can block each other. */
122 ps2_write_mouse(s
->dev
, value
);
124 ps2_write_keyboard(s
->dev
, value
);
127 case 3: /* KMICLKDIV */
131 qemu_log_mask(LOG_GUEST_ERROR
,
132 "pl050_write: Bad offset %x\n", (int)offset
);
135 static const MemoryRegionOps pl050_ops
= {
137 .write
= pl050_write
,
138 .endianness
= DEVICE_NATIVE_ENDIAN
,
141 static int pl050_initfn(SysBusDevice
*dev
)
143 PL050State
*s
= PL050(dev
);
145 memory_region_init_io(&s
->iomem
, OBJECT(s
), &pl050_ops
, s
, "pl050", 0x1000);
146 sysbus_init_mmio(dev
, &s
->iomem
);
147 sysbus_init_irq(dev
, &s
->irq
);
149 s
->dev
= ps2_mouse_init(pl050_update
, s
);
151 s
->dev
= ps2_kbd_init(pl050_update
, s
);
156 static void pl050_keyboard_init(Object
*obj
)
158 PL050State
*s
= PL050(obj
);
163 static void pl050_mouse_init(Object
*obj
)
165 PL050State
*s
= PL050(obj
);
170 static const TypeInfo pl050_kbd_info
= {
171 .name
= "pl050_keyboard",
172 .parent
= TYPE_PL050
,
173 .instance_init
= pl050_keyboard_init
,
176 static const TypeInfo pl050_mouse_info
= {
177 .name
= "pl050_mouse",
178 .parent
= TYPE_PL050
,
179 .instance_init
= pl050_mouse_init
,
182 static void pl050_class_init(ObjectClass
*oc
, void *data
)
184 DeviceClass
*dc
= DEVICE_CLASS(oc
);
185 SysBusDeviceClass
*sdc
= SYS_BUS_DEVICE_CLASS(oc
);
187 sdc
->init
= pl050_initfn
;
188 dc
->vmsd
= &vmstate_pl050
;
191 static const TypeInfo pl050_type_info
= {
193 .parent
= TYPE_SYS_BUS_DEVICE
,
194 .instance_size
= sizeof(PL050State
),
196 .class_init
= pl050_class_init
,
199 static void pl050_register_types(void)
201 type_register_static(&pl050_type_info
);
202 type_register_static(&pl050_kbd_info
);
203 type_register_static(&pl050_mouse_info
);
206 type_init(pl050_register_types
)