2007-09-16 23:08:06 +02:00
|
|
|
/*
|
2006-07-17 20:45:34 +02:00
|
|
|
* Arm PrimeCell PL050 Keyboard / Mouse Interface
|
2006-04-09 03:32:52 +02:00
|
|
|
*
|
2007-04-30 04:39:55 +02:00
|
|
|
* Copyright (c) 2006-2007 CodeSourcery.
|
2006-04-09 03:32:52 +02:00
|
|
|
* Written by Paul Brook
|
|
|
|
*
|
2011-06-26 04:21:35 +02:00
|
|
|
* This code is licensed under the GPL.
|
2006-04-09 03:32:52 +02:00
|
|
|
*/
|
|
|
|
|
2022-06-24 15:40:52 +02:00
|
|
|
/*
|
|
|
|
* QEMU interface:
|
|
|
|
* + sysbus MMIO region 0: MemoryRegion defining the PL050 registers
|
|
|
|
* + Named GPIO input "ps2-input-irq": set to 1 if the downstream PS2 device
|
|
|
|
* has asserted its irq
|
|
|
|
* + sysbus IRQ 0: PL050 output irq
|
|
|
|
*/
|
|
|
|
|
2016-01-26 19:17:05 +01:00
|
|
|
#include "qemu/osdep.h"
|
2013-02-04 15:40:22 +01:00
|
|
|
#include "hw/sysbus.h"
|
2019-08-12 07:23:45 +02:00
|
|
|
#include "migration/vmstate.h"
|
2013-02-05 17:06:20 +01:00
|
|
|
#include "hw/input/ps2.h"
|
2022-07-12 23:52:12 +02:00
|
|
|
#include "hw/input/pl050.h"
|
2019-08-12 07:23:42 +02:00
|
|
|
#include "hw/irq.h"
|
2015-12-15 13:16:16 +01:00
|
|
|
#include "qemu/log.h"
|
2019-05-23 16:35:07 +02:00
|
|
|
#include "qemu/module.h"
|
2020-09-03 22:43:22 +02:00
|
|
|
#include "qom/object.h"
|
2006-04-09 03:32:52 +02:00
|
|
|
|
|
|
|
|
2010-12-23 18:19:54 +01:00
|
|
|
static const VMStateDescription vmstate_pl050 = {
|
|
|
|
.name = "pl050",
|
2013-04-05 17:17:58 +02:00
|
|
|
.version_id = 2,
|
|
|
|
.minimum_version_id = 2,
|
2010-12-23 18:19:54 +01:00
|
|
|
.fields = (VMStateField[]) {
|
2013-07-26 18:40:25 +02:00
|
|
|
VMSTATE_UINT32(cr, PL050State),
|
|
|
|
VMSTATE_UINT32(clk, PL050State),
|
|
|
|
VMSTATE_UINT32(last, PL050State),
|
|
|
|
VMSTATE_INT32(pending, PL050State),
|
2010-12-23 18:19:54 +01:00
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-04-30 04:39:55 +02:00
|
|
|
#define PL050_TXEMPTY (1 << 6)
|
|
|
|
#define PL050_TXBUSY (1 << 5)
|
|
|
|
#define PL050_RXFULL (1 << 4)
|
|
|
|
#define PL050_RXBUSY (1 << 3)
|
|
|
|
#define PL050_RXPARITY (1 << 2)
|
|
|
|
#define PL050_KMIC (1 << 1)
|
|
|
|
#define PL050_KMID (1 << 0)
|
|
|
|
|
2022-06-24 15:40:29 +02:00
|
|
|
static const unsigned char pl050_id[] = {
|
|
|
|
0x50, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1
|
|
|
|
};
|
2006-04-09 03:32:52 +02:00
|
|
|
|
2022-06-24 15:40:30 +02:00
|
|
|
static void pl050_update_irq(PL050State *s)
|
|
|
|
{
|
|
|
|
int level = (s->pending && (s->cr & 0x10) != 0)
|
|
|
|
|| (s->cr & 0x08) != 0;
|
|
|
|
|
|
|
|
qemu_set_irq(s->irq, level);
|
|
|
|
}
|
|
|
|
|
2022-06-24 15:40:51 +02:00
|
|
|
static void pl050_set_irq(void *opaque, int n, int level)
|
2006-04-09 03:32:52 +02:00
|
|
|
{
|
2013-07-26 18:40:25 +02:00
|
|
|
PL050State *s = (PL050State *)opaque;
|
2006-04-09 03:32:52 +02:00
|
|
|
|
|
|
|
s->pending = level;
|
2022-06-24 15:40:30 +02:00
|
|
|
pl050_update_irq(s);
|
2006-04-09 03:32:52 +02:00
|
|
|
}
|
|
|
|
|
2012-10-23 12:30:10 +02:00
|
|
|
static uint64_t pl050_read(void *opaque, hwaddr offset,
|
2011-10-10 17:18:44 +02:00
|
|
|
unsigned size)
|
2006-04-09 03:32:52 +02:00
|
|
|
{
|
2013-07-26 18:40:25 +02:00
|
|
|
PL050State *s = (PL050State *)opaque;
|
2022-06-24 15:40:29 +02:00
|
|
|
|
|
|
|
if (offset >= 0xfe0 && offset < 0x1000) {
|
2006-04-09 03:32:52 +02:00
|
|
|
return pl050_id[(offset - 0xfe0) >> 2];
|
2022-06-24 15:40:29 +02:00
|
|
|
}
|
2006-04-09 03:32:52 +02:00
|
|
|
|
|
|
|
switch (offset >> 2) {
|
|
|
|
case 0: /* KMICR */
|
|
|
|
return s->cr;
|
|
|
|
case 1: /* KMISTAT */
|
2007-04-30 04:39:55 +02:00
|
|
|
{
|
|
|
|
uint8_t val;
|
|
|
|
uint32_t stat;
|
|
|
|
|
|
|
|
val = s->last;
|
|
|
|
val = val ^ (val >> 4);
|
|
|
|
val = val ^ (val >> 2);
|
|
|
|
val = (val ^ (val >> 1)) & 1;
|
|
|
|
|
|
|
|
stat = PL050_TXEMPTY;
|
2022-06-24 15:40:29 +02:00
|
|
|
if (val) {
|
2007-04-30 04:39:55 +02:00
|
|
|
stat |= PL050_RXPARITY;
|
2022-06-24 15:40:29 +02:00
|
|
|
}
|
|
|
|
if (s->pending) {
|
2007-04-30 04:39:55 +02:00
|
|
|
stat |= PL050_RXFULL;
|
2022-06-24 15:40:29 +02:00
|
|
|
}
|
2007-04-30 04:39:55 +02:00
|
|
|
|
|
|
|
return stat;
|
2006-04-09 03:32:52 +02:00
|
|
|
}
|
|
|
|
case 2: /* KMIDATA */
|
2022-06-24 15:40:29 +02:00
|
|
|
if (s->pending) {
|
2022-07-12 23:52:14 +02:00
|
|
|
s->last = ps2_read_data(s->ps2dev);
|
2022-06-24 15:40:29 +02:00
|
|
|
}
|
2006-04-09 03:32:52 +02:00
|
|
|
return s->last;
|
|
|
|
case 3: /* KMICLKDIV */
|
|
|
|
return s->clk;
|
|
|
|
case 4: /* KMIIR */
|
|
|
|
return s->pending | 2;
|
|
|
|
default:
|
2012-10-30 08:45:08 +01:00
|
|
|
qemu_log_mask(LOG_GUEST_ERROR,
|
|
|
|
"pl050_read: Bad offset %x\n", (int)offset);
|
2006-04-09 03:32:52 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-23 12:30:10 +02:00
|
|
|
static void pl050_write(void *opaque, hwaddr offset,
|
2011-10-10 17:18:44 +02:00
|
|
|
uint64_t value, unsigned size)
|
2006-04-09 03:32:52 +02:00
|
|
|
{
|
2013-07-26 18:40:25 +02:00
|
|
|
PL050State *s = (PL050State *)opaque;
|
2022-06-24 15:40:29 +02:00
|
|
|
|
2006-04-09 03:32:52 +02:00
|
|
|
switch (offset >> 2) {
|
|
|
|
case 0: /* KMICR */
|
|
|
|
s->cr = value;
|
2022-06-24 15:40:30 +02:00
|
|
|
pl050_update_irq(s);
|
2006-04-09 03:32:52 +02:00
|
|
|
/* ??? Need to implement the enable/disable bit. */
|
|
|
|
break;
|
|
|
|
case 2: /* KMIDATA */
|
|
|
|
/* ??? This should toggle the TX interrupt line. */
|
|
|
|
/* ??? This means kbd/mouse can block each other. */
|
|
|
|
if (s->is_mouse) {
|
2022-07-12 23:52:14 +02:00
|
|
|
ps2_write_mouse(PS2_MOUSE_DEVICE(s->ps2dev), value);
|
2006-04-09 03:32:52 +02:00
|
|
|
} else {
|
2022-07-12 23:52:14 +02:00
|
|
|
ps2_write_keyboard(PS2_KBD_DEVICE(s->ps2dev), value);
|
2006-04-09 03:32:52 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3: /* KMICLKDIV */
|
|
|
|
s->clk = value;
|
|
|
|
return;
|
|
|
|
default:
|
2012-10-30 08:45:08 +01:00
|
|
|
qemu_log_mask(LOG_GUEST_ERROR,
|
|
|
|
"pl050_write: Bad offset %x\n", (int)offset);
|
2006-04-09 03:32:52 +02:00
|
|
|
}
|
|
|
|
}
|
2011-10-10 17:18:44 +02:00
|
|
|
static const MemoryRegionOps pl050_ops = {
|
|
|
|
.read = pl050_read,
|
|
|
|
.write = pl050_write,
|
|
|
|
.endianness = DEVICE_NATIVE_ENDIAN,
|
2006-04-09 03:32:52 +02:00
|
|
|
};
|
|
|
|
|
2018-12-13 14:47:59 +01:00
|
|
|
static void pl050_realize(DeviceState *dev, Error **errp)
|
2006-04-09 03:32:52 +02:00
|
|
|
{
|
2013-07-26 18:49:24 +02:00
|
|
|
PL050State *s = PL050(dev);
|
2006-04-09 03:32:52 +02:00
|
|
|
|
2022-07-12 23:52:14 +02:00
|
|
|
qdev_connect_gpio_out(DEVICE(s->ps2dev), PS2_DEVICE_IRQ,
|
2022-06-24 15:40:51 +02:00
|
|
|
qdev_get_gpio_in_named(dev, "ps2-input-irq", 0));
|
2006-04-09 03:32:52 +02:00
|
|
|
}
|
2009-05-14 23:35:07 +02:00
|
|
|
|
2022-07-12 23:52:19 +02:00
|
|
|
static void pl050_kbd_realize(DeviceState *dev, Error **errp)
|
|
|
|
{
|
|
|
|
PL050DeviceClass *pdc = PL050_GET_CLASS(dev);
|
2022-07-12 23:52:21 +02:00
|
|
|
PL050KbdState *s = PL050_KBD_DEVICE(dev);
|
2022-07-12 23:52:19 +02:00
|
|
|
PL050State *ps = PL050(dev);
|
|
|
|
|
2022-07-12 23:52:21 +02:00
|
|
|
if (!sysbus_realize(SYS_BUS_DEVICE(&s->kbd), errp)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ps->ps2dev = PS2_DEVICE(&s->kbd);
|
2022-07-12 23:52:19 +02:00
|
|
|
pdc->parent_realize(dev, errp);
|
|
|
|
}
|
|
|
|
|
2022-07-12 23:52:13 +02:00
|
|
|
static void pl050_kbd_init(Object *obj)
|
2009-05-14 23:35:07 +02:00
|
|
|
{
|
2022-07-12 23:52:21 +02:00
|
|
|
PL050KbdState *s = PL050_KBD_DEVICE(obj);
|
|
|
|
PL050State *ps = PL050(obj);
|
2009-05-14 23:35:07 +02:00
|
|
|
|
2022-07-12 23:52:21 +02:00
|
|
|
ps->is_mouse = false;
|
|
|
|
object_initialize_child(obj, "kbd", &s->kbd, TYPE_PS2_KBD_DEVICE);
|
2009-05-14 23:35:07 +02:00
|
|
|
}
|
|
|
|
|
2022-07-12 23:52:20 +02:00
|
|
|
static void pl050_mouse_realize(DeviceState *dev, Error **errp)
|
|
|
|
{
|
|
|
|
PL050DeviceClass *pdc = PL050_GET_CLASS(dev);
|
2022-07-12 23:52:22 +02:00
|
|
|
PL050MouseState *s = PL050_MOUSE_DEVICE(dev);
|
2022-07-12 23:52:20 +02:00
|
|
|
PL050State *ps = PL050(dev);
|
|
|
|
|
2022-07-12 23:52:22 +02:00
|
|
|
if (!sysbus_realize(SYS_BUS_DEVICE(&s->mouse), errp)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ps->ps2dev = PS2_DEVICE(&s->mouse);
|
2022-07-12 23:52:20 +02:00
|
|
|
pdc->parent_realize(dev, errp);
|
|
|
|
}
|
|
|
|
|
2013-07-26 18:49:24 +02:00
|
|
|
static void pl050_mouse_init(Object *obj)
|
2012-01-24 20:12:29 +01:00
|
|
|
{
|
2022-07-12 23:52:22 +02:00
|
|
|
PL050MouseState *s = PL050_MOUSE_DEVICE(obj);
|
|
|
|
PL050State *ps = PL050(obj);
|
2012-01-24 20:12:29 +01:00
|
|
|
|
2022-07-12 23:52:22 +02:00
|
|
|
ps->is_mouse = true;
|
|
|
|
object_initialize_child(obj, "mouse", &s->mouse, TYPE_PS2_MOUSE_DEVICE);
|
2012-01-24 20:12:29 +01:00
|
|
|
}
|
|
|
|
|
2022-07-12 23:52:19 +02:00
|
|
|
static void pl050_kbd_class_init(ObjectClass *oc, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(oc);
|
|
|
|
PL050DeviceClass *pdc = PL050_CLASS(oc);
|
|
|
|
|
|
|
|
device_class_set_parent_realize(dc, pl050_kbd_realize,
|
|
|
|
&pdc->parent_realize);
|
|
|
|
}
|
|
|
|
|
2013-01-10 16:19:07 +01:00
|
|
|
static const TypeInfo pl050_kbd_info = {
|
2022-07-12 23:52:15 +02:00
|
|
|
.name = TYPE_PL050_KBD_DEVICE,
|
2013-07-26 18:49:24 +02:00
|
|
|
.parent = TYPE_PL050,
|
2022-07-12 23:52:13 +02:00
|
|
|
.instance_init = pl050_kbd_init,
|
2022-07-12 23:52:15 +02:00
|
|
|
.instance_size = sizeof(PL050KbdState),
|
2022-07-12 23:52:19 +02:00
|
|
|
.class_init = pl050_kbd_class_init,
|
2010-12-23 18:19:54 +01:00
|
|
|
};
|
|
|
|
|
2022-07-12 23:52:20 +02:00
|
|
|
static void pl050_mouse_class_init(ObjectClass *oc, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(oc);
|
|
|
|
PL050DeviceClass *pdc = PL050_CLASS(oc);
|
|
|
|
|
|
|
|
device_class_set_parent_realize(dc, pl050_mouse_realize,
|
|
|
|
&pdc->parent_realize);
|
|
|
|
}
|
|
|
|
|
2013-07-26 18:49:24 +02:00
|
|
|
static const TypeInfo pl050_mouse_info = {
|
2022-07-12 23:52:16 +02:00
|
|
|
.name = TYPE_PL050_MOUSE_DEVICE,
|
2013-07-26 18:49:24 +02:00
|
|
|
.parent = TYPE_PL050,
|
|
|
|
.instance_init = pl050_mouse_init,
|
2022-07-12 23:52:16 +02:00
|
|
|
.instance_size = sizeof(PL050MouseState),
|
2022-07-12 23:52:20 +02:00
|
|
|
.class_init = pl050_mouse_class_init,
|
2013-07-26 18:49:24 +02:00
|
|
|
};
|
|
|
|
|
2022-06-24 15:40:51 +02:00
|
|
|
static void pl050_init(Object *obj)
|
|
|
|
{
|
2022-07-12 23:52:17 +02:00
|
|
|
PL050State *s = PL050(obj);
|
|
|
|
SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
|
|
|
|
|
|
|
|
memory_region_init_io(&s->iomem, obj, &pl050_ops, s, "pl050", 0x1000);
|
|
|
|
sysbus_init_mmio(sbd, &s->iomem);
|
|
|
|
sysbus_init_irq(sbd, &s->irq);
|
|
|
|
|
2022-06-24 15:40:51 +02:00
|
|
|
qdev_init_gpio_in_named(DEVICE(obj), pl050_set_irq, "ps2-input-irq", 1);
|
|
|
|
}
|
|
|
|
|
2013-07-26 18:49:24 +02:00
|
|
|
static void pl050_class_init(ObjectClass *oc, void *data)
|
2012-01-24 20:12:29 +01:00
|
|
|
{
|
2013-07-26 18:49:24 +02:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(oc);
|
2012-01-24 20:12:29 +01:00
|
|
|
|
2018-12-13 14:47:59 +01:00
|
|
|
dc->realize = pl050_realize;
|
2011-12-08 04:34:16 +01:00
|
|
|
dc->vmsd = &vmstate_pl050;
|
2012-01-24 20:12:29 +01:00
|
|
|
}
|
|
|
|
|
2013-07-26 18:49:24 +02:00
|
|
|
static const TypeInfo pl050_type_info = {
|
|
|
|
.name = TYPE_PL050,
|
2011-12-08 04:34:16 +01:00
|
|
|
.parent = TYPE_SYS_BUS_DEVICE,
|
2022-06-24 15:40:51 +02:00
|
|
|
.instance_init = pl050_init,
|
2013-07-26 18:40:25 +02:00
|
|
|
.instance_size = sizeof(PL050State),
|
2022-07-12 23:52:18 +02:00
|
|
|
.class_init = pl050_class_init,
|
|
|
|
.class_size = sizeof(PL050DeviceClass),
|
2013-07-26 18:49:24 +02:00
|
|
|
.abstract = true,
|
|
|
|
.class_init = pl050_class_init,
|
2010-12-23 18:19:54 +01:00
|
|
|
};
|
|
|
|
|
2012-02-09 15:20:55 +01:00
|
|
|
static void pl050_register_types(void)
|
2009-05-14 23:35:07 +02:00
|
|
|
{
|
2013-07-26 18:49:24 +02:00
|
|
|
type_register_static(&pl050_type_info);
|
2011-12-08 04:34:16 +01:00
|
|
|
type_register_static(&pl050_kbd_info);
|
|
|
|
type_register_static(&pl050_mouse_info);
|
2009-05-14 23:35:07 +02:00
|
|
|
}
|
|
|
|
|
2012-02-09 15:20:55 +01:00
|
|
|
type_init(pl050_register_types)
|