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
|
|
|
*/
|
|
|
|
|
2016-01-26 19:17:05 +01:00
|
|
|
#include "qemu/osdep.h"
|
2013-02-04 15:40:22 +01:00
|
|
|
#include "hw/sysbus.h"
|
2013-02-05 17:06:20 +01:00
|
|
|
#include "hw/input/ps2.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"
|
2006-04-09 03:32:52 +02:00
|
|
|
|
2013-07-26 18:49:24 +02:00
|
|
|
#define TYPE_PL050 "pl050"
|
|
|
|
#define PL050(obj) OBJECT_CHECK(PL050State, (obj), TYPE_PL050)
|
|
|
|
|
2013-07-26 18:40:25 +02:00
|
|
|
typedef struct PL050State {
|
2013-07-26 18:49:24 +02:00
|
|
|
SysBusDevice parent_obj;
|
|
|
|
|
2011-10-10 17:18:44 +02:00
|
|
|
MemoryRegion iomem;
|
2006-04-09 03:32:52 +02:00
|
|
|
void *dev;
|
|
|
|
uint32_t cr;
|
|
|
|
uint32_t clk;
|
|
|
|
uint32_t last;
|
|
|
|
int pending;
|
2007-04-07 20:14:41 +02:00
|
|
|
qemu_irq irq;
|
2013-07-26 18:49:24 +02:00
|
|
|
bool is_mouse;
|
2013-07-26 18:40:25 +02:00
|
|
|
} PL050State;
|
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)
|
|
|
|
|
2006-04-09 03:32:52 +02:00
|
|
|
static const unsigned char pl050_id[] =
|
|
|
|
{ 0x50, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
|
|
|
|
|
|
|
|
static void pl050_update(void *opaque, int level)
|
|
|
|
{
|
2013-07-26 18:40:25 +02:00
|
|
|
PL050State *s = (PL050State *)opaque;
|
2006-04-09 03:32:52 +02:00
|
|
|
int raise;
|
|
|
|
|
|
|
|
s->pending = level;
|
|
|
|
raise = (s->pending && (s->cr & 0x10) != 0)
|
|
|
|
|| (s->cr & 0x08) != 0;
|
2007-04-07 20:14:41 +02:00
|
|
|
qemu_set_irq(s->irq, raise);
|
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;
|
2006-04-09 03:32:52 +02:00
|
|
|
if (offset >= 0xfe0 && offset < 0x1000)
|
|
|
|
return pl050_id[(offset - 0xfe0) >> 2];
|
|
|
|
|
|
|
|
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;
|
|
|
|
if (val)
|
|
|
|
stat |= PL050_RXPARITY;
|
|
|
|
if (s->pending)
|
|
|
|
stat |= PL050_RXFULL;
|
|
|
|
|
|
|
|
return stat;
|
2006-04-09 03:32:52 +02:00
|
|
|
}
|
|
|
|
case 2: /* KMIDATA */
|
|
|
|
if (s->pending)
|
|
|
|
s->last = ps2_read_data(s->dev);
|
|
|
|
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;
|
2006-04-09 03:32:52 +02:00
|
|
|
switch (offset >> 2) {
|
|
|
|
case 0: /* KMICR */
|
|
|
|
s->cr = value;
|
|
|
|
pl050_update(s, s->pending);
|
|
|
|
/* ??? 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) {
|
|
|
|
ps2_write_mouse(s->dev, value);
|
|
|
|
} else {
|
|
|
|
ps2_write_keyboard(s->dev, value);
|
|
|
|
}
|
|
|
|
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);
|
2018-12-13 14:47:59 +01:00
|
|
|
SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
|
2006-04-09 03:32:52 +02:00
|
|
|
|
2013-06-07 03:25:08 +02:00
|
|
|
memory_region_init_io(&s->iomem, OBJECT(s), &pl050_ops, s, "pl050", 0x1000);
|
2018-12-13 14:47:59 +01:00
|
|
|
sysbus_init_mmio(sbd, &s->iomem);
|
|
|
|
sysbus_init_irq(sbd, &s->irq);
|
2013-07-26 18:49:24 +02:00
|
|
|
if (s->is_mouse) {
|
2006-04-09 03:32:52 +02:00
|
|
|
s->dev = ps2_mouse_init(pl050_update, s);
|
2013-07-26 18:49:24 +02:00
|
|
|
} else {
|
2006-04-09 03:32:52 +02:00
|
|
|
s->dev = ps2_kbd_init(pl050_update, s);
|
2013-07-26 18:49:24 +02:00
|
|
|
}
|
2006-04-09 03:32:52 +02:00
|
|
|
}
|
2009-05-14 23:35:07 +02:00
|
|
|
|
2013-07-26 18:49:24 +02:00
|
|
|
static void pl050_keyboard_init(Object *obj)
|
2009-05-14 23:35:07 +02:00
|
|
|
{
|
2013-07-26 18:49:24 +02:00
|
|
|
PL050State *s = PL050(obj);
|
2009-05-14 23:35:07 +02:00
|
|
|
|
2013-07-26 18:49:24 +02:00
|
|
|
s->is_mouse = false;
|
2009-05-14 23:35:07 +02:00
|
|
|
}
|
|
|
|
|
2013-07-26 18:49:24 +02:00
|
|
|
static void pl050_mouse_init(Object *obj)
|
2012-01-24 20:12:29 +01:00
|
|
|
{
|
2013-07-26 18:49:24 +02:00
|
|
|
PL050State *s = PL050(obj);
|
2012-01-24 20:12:29 +01:00
|
|
|
|
2013-07-26 18:49:24 +02:00
|
|
|
s->is_mouse = true;
|
2012-01-24 20:12:29 +01:00
|
|
|
}
|
|
|
|
|
2013-01-10 16:19:07 +01:00
|
|
|
static const TypeInfo pl050_kbd_info = {
|
2011-12-08 04:34:16 +01:00
|
|
|
.name = "pl050_keyboard",
|
2013-07-26 18:49:24 +02:00
|
|
|
.parent = TYPE_PL050,
|
|
|
|
.instance_init = pl050_keyboard_init,
|
2010-12-23 18:19:54 +01:00
|
|
|
};
|
|
|
|
|
2013-07-26 18:49:24 +02:00
|
|
|
static const TypeInfo pl050_mouse_info = {
|
|
|
|
.name = "pl050_mouse",
|
|
|
|
.parent = TYPE_PL050,
|
|
|
|
.instance_init = pl050_mouse_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
2013-07-26 18:40:25 +02:00
|
|
|
.instance_size = sizeof(PL050State),
|
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)
|