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"
25 static const VMStateDescription vmstate_pl050
= {
28 .minimum_version_id
= 2,
29 .fields
= (VMStateField
[]) {
30 VMSTATE_UINT32(cr
, pl050_state
),
31 VMSTATE_UINT32(clk
, pl050_state
),
32 VMSTATE_UINT32(last
, pl050_state
),
33 VMSTATE_INT32(pending
, pl050_state
),
38 #define PL050_TXEMPTY (1 << 6)
39 #define PL050_TXBUSY (1 << 5)
40 #define PL050_RXFULL (1 << 4)
41 #define PL050_RXBUSY (1 << 3)
42 #define PL050_RXPARITY (1 << 2)
43 #define PL050_KMIC (1 << 1)
44 #define PL050_KMID (1 << 0)
46 static const unsigned char pl050_id
[] =
47 { 0x50, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
49 static void pl050_update(void *opaque
, int level
)
51 pl050_state
*s
= (pl050_state
*)opaque
;
55 raise
= (s
->pending
&& (s
->cr
& 0x10) != 0)
56 || (s
->cr
& 0x08) != 0;
57 qemu_set_irq(s
->irq
, raise
);
60 static uint64_t pl050_read(void *opaque
, hwaddr offset
,
63 pl050_state
*s
= (pl050_state
*)opaque
;
64 if (offset
>= 0xfe0 && offset
< 0x1000)
65 return pl050_id
[(offset
- 0xfe0) >> 2];
67 switch (offset
>> 2) {
76 val
= val
^ (val
>> 4);
77 val
= val
^ (val
>> 2);
78 val
= (val
^ (val
>> 1)) & 1;
82 stat
|= PL050_RXPARITY
;
90 s
->last
= ps2_read_data(s
->dev
);
92 case 3: /* KMICLKDIV */
95 return s
->pending
| 2;
97 qemu_log_mask(LOG_GUEST_ERROR
,
98 "pl050_read: Bad offset %x\n", (int)offset
);
103 static void pl050_write(void *opaque
, hwaddr offset
,
104 uint64_t value
, unsigned size
)
106 pl050_state
*s
= (pl050_state
*)opaque
;
107 switch (offset
>> 2) {
110 pl050_update(s
, s
->pending
);
111 /* ??? Need to implement the enable/disable bit. */
113 case 2: /* KMIDATA */
114 /* ??? This should toggle the TX interrupt line. */
115 /* ??? This means kbd/mouse can block each other. */
117 ps2_write_mouse(s
->dev
, value
);
119 ps2_write_keyboard(s
->dev
, value
);
122 case 3: /* KMICLKDIV */
126 qemu_log_mask(LOG_GUEST_ERROR
,
127 "pl050_write: Bad offset %x\n", (int)offset
);
130 static const MemoryRegionOps pl050_ops
= {
132 .write
= pl050_write
,
133 .endianness
= DEVICE_NATIVE_ENDIAN
,
136 static int pl050_init(SysBusDevice
*dev
, int is_mouse
)
138 pl050_state
*s
= FROM_SYSBUS(pl050_state
, dev
);
140 memory_region_init_io(&s
->iomem
, &pl050_ops
, s
, "pl050", 0x1000);
141 sysbus_init_mmio(dev
, &s
->iomem
);
142 sysbus_init_irq(dev
, &s
->irq
);
143 s
->is_mouse
= is_mouse
;
145 s
->dev
= ps2_mouse_init(pl050_update
, s
);
147 s
->dev
= ps2_kbd_init(pl050_update
, s
);
151 static int pl050_init_keyboard(SysBusDevice
*dev
)
153 return pl050_init(dev
, 0);
156 static int pl050_init_mouse(SysBusDevice
*dev
)
158 return pl050_init(dev
, 1);
161 static void pl050_kbd_class_init(ObjectClass
*klass
, void *data
)
163 DeviceClass
*dc
= DEVICE_CLASS(klass
);
164 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
166 k
->init
= pl050_init_keyboard
;
167 dc
->vmsd
= &vmstate_pl050
;
170 static const TypeInfo pl050_kbd_info
= {
171 .name
= "pl050_keyboard",
172 .parent
= TYPE_SYS_BUS_DEVICE
,
173 .instance_size
= sizeof(pl050_state
),
174 .class_init
= pl050_kbd_class_init
,
177 static void pl050_mouse_class_init(ObjectClass
*klass
, void *data
)
179 DeviceClass
*dc
= DEVICE_CLASS(klass
);
180 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
182 k
->init
= pl050_init_mouse
;
183 dc
->vmsd
= &vmstate_pl050
;
186 static const TypeInfo pl050_mouse_info
= {
187 .name
= "pl050_mouse",
188 .parent
= TYPE_SYS_BUS_DEVICE
,
189 .instance_size
= sizeof(pl050_state
),
190 .class_init
= pl050_mouse_class_init
,
193 static void pl050_register_types(void)
195 type_register_static(&pl050_kbd_info
);
196 type_register_static(&pl050_mouse_info
);
199 type_init(pl050_register_types
)