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.
25 static const VMStateDescription vmstate_pl050
= {
28 .minimum_version_id
= 1,
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
),
34 VMSTATE_INT32(is_mouse
, pl050_state
),
39 #define PL050_TXEMPTY (1 << 6)
40 #define PL050_TXBUSY (1 << 5)
41 #define PL050_RXFULL (1 << 4)
42 #define PL050_RXBUSY (1 << 3)
43 #define PL050_RXPARITY (1 << 2)
44 #define PL050_KMIC (1 << 1)
45 #define PL050_KMID (1 << 0)
47 static const unsigned char pl050_id
[] =
48 { 0x50, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
50 static void pl050_update(void *opaque
, int level
)
52 pl050_state
*s
= (pl050_state
*)opaque
;
56 raise
= (s
->pending
&& (s
->cr
& 0x10) != 0)
57 || (s
->cr
& 0x08) != 0;
58 qemu_set_irq(s
->irq
, raise
);
61 static uint64_t pl050_read(void *opaque
, hwaddr offset
,
64 pl050_state
*s
= (pl050_state
*)opaque
;
65 if (offset
>= 0xfe0 && offset
< 0x1000)
66 return pl050_id
[(offset
- 0xfe0) >> 2];
68 switch (offset
>> 2) {
77 val
= val
^ (val
>> 4);
78 val
= val
^ (val
>> 2);
79 val
= (val
^ (val
>> 1)) & 1;
83 stat
|= PL050_RXPARITY
;
91 s
->last
= ps2_read_data(s
->dev
);
93 case 3: /* KMICLKDIV */
96 return s
->pending
| 2;
98 qemu_log_mask(LOG_GUEST_ERROR
,
99 "pl050_read: Bad offset %x\n", (int)offset
);
104 static void pl050_write(void *opaque
, hwaddr offset
,
105 uint64_t value
, unsigned size
)
107 pl050_state
*s
= (pl050_state
*)opaque
;
108 switch (offset
>> 2) {
111 pl050_update(s
, s
->pending
);
112 /* ??? Need to implement the enable/disable bit. */
114 case 2: /* KMIDATA */
115 /* ??? This should toggle the TX interrupt line. */
116 /* ??? This means kbd/mouse can block each other. */
118 ps2_write_mouse(s
->dev
, value
);
120 ps2_write_keyboard(s
->dev
, value
);
123 case 3: /* KMICLKDIV */
127 qemu_log_mask(LOG_GUEST_ERROR
,
128 "pl050_write: Bad offset %x\n", (int)offset
);
131 static const MemoryRegionOps pl050_ops
= {
133 .write
= pl050_write
,
134 .endianness
= DEVICE_NATIVE_ENDIAN
,
137 static int pl050_init(SysBusDevice
*dev
, int is_mouse
)
139 pl050_state
*s
= FROM_SYSBUS(pl050_state
, dev
);
141 memory_region_init_io(&s
->iomem
, &pl050_ops
, s
, "pl050", 0x1000);
142 sysbus_init_mmio(dev
, &s
->iomem
);
143 sysbus_init_irq(dev
, &s
->irq
);
144 s
->is_mouse
= is_mouse
;
146 s
->dev
= ps2_mouse_init(pl050_update
, s
);
148 s
->dev
= ps2_kbd_init(pl050_update
, s
);
152 static int pl050_init_keyboard(SysBusDevice
*dev
)
154 return pl050_init(dev
, 0);
157 static int pl050_init_mouse(SysBusDevice
*dev
)
159 return pl050_init(dev
, 1);
162 static void pl050_kbd_class_init(ObjectClass
*klass
, void *data
)
164 DeviceClass
*dc
= DEVICE_CLASS(klass
);
165 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
167 k
->init
= pl050_init_keyboard
;
168 dc
->vmsd
= &vmstate_pl050
;
171 static TypeInfo pl050_kbd_info
= {
172 .name
= "pl050_keyboard",
173 .parent
= TYPE_SYS_BUS_DEVICE
,
174 .instance_size
= sizeof(pl050_state
),
175 .class_init
= pl050_kbd_class_init
,
178 static void pl050_mouse_class_init(ObjectClass
*klass
, void *data
)
180 DeviceClass
*dc
= DEVICE_CLASS(klass
);
181 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
183 k
->init
= pl050_init_mouse
;
184 dc
->vmsd
= &vmstate_pl050
;
187 static TypeInfo pl050_mouse_info
= {
188 .name
= "pl050_mouse",
189 .parent
= TYPE_SYS_BUS_DEVICE
,
190 .instance_size
= sizeof(pl050_state
),
191 .class_init
= pl050_mouse_class_init
,
194 static void pl050_register_types(void)
196 type_register_static(&pl050_kbd_info
);
197 type_register_static(&pl050_mouse_info
);
200 type_init(pl050_register_types
)