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 "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
;
29 static const VMStateDescription vmstate_pl050
= {
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
),
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
;
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
,
67 PL050State
*s
= (PL050State
*)opaque
;
68 if (offset
>= 0xfe0 && offset
< 0x1000)
69 return pl050_id
[(offset
- 0xfe0) >> 2];
71 switch (offset
>> 2) {
80 val
= val
^ (val
>> 4);
81 val
= val
^ (val
>> 2);
82 val
= (val
^ (val
>> 1)) & 1;
86 stat
|= PL050_RXPARITY
;
94 s
->last
= ps2_read_data(s
->dev
);
96 case 3: /* KMICLKDIV */
99 return s
->pending
| 2;
101 qemu_log_mask(LOG_GUEST_ERROR
,
102 "pl050_read: Bad offset %x\n", (int)offset
);
107 static void pl050_write(void *opaque
, hwaddr offset
,
108 uint64_t value
, unsigned size
)
110 PL050State
*s
= (PL050State
*)opaque
;
111 switch (offset
>> 2) {
114 pl050_update(s
, s
->pending
);
115 /* ??? Need to implement the enable/disable bit. */
117 case 2: /* KMIDATA */
118 /* ??? This should toggle the TX interrupt line. */
119 /* ??? This means kbd/mouse can block each other. */
121 ps2_write_mouse(s
->dev
, value
);
123 ps2_write_keyboard(s
->dev
, value
);
126 case 3: /* KMICLKDIV */
130 qemu_log_mask(LOG_GUEST_ERROR
,
131 "pl050_write: Bad offset %x\n", (int)offset
);
134 static const MemoryRegionOps pl050_ops
= {
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
);
148 s
->dev
= ps2_mouse_init(pl050_update
, s
);
150 s
->dev
= ps2_kbd_init(pl050_update
, s
);
155 static void pl050_keyboard_init(Object
*obj
)
157 PL050State
*s
= PL050(obj
);
162 static void pl050_mouse_init(Object
*obj
)
164 PL050State
*s
= PL050(obj
);
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
= {
192 .parent
= TYPE_SYS_BUS_DEVICE
,
193 .instance_size
= sizeof(PL050State
),
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
)