2007-09-17 01:08:06 +04:00
|
|
|
/*
|
2006-07-17 22:45:34 +04:00
|
|
|
* Arm PrimeCell PL050 Keyboard / Mouse Interface
|
2006-04-09 05:32:52 +04:00
|
|
|
*
|
2007-04-30 06:39:55 +04:00
|
|
|
* Copyright (c) 2006-2007 CodeSourcery.
|
2006-04-09 05:32:52 +04:00
|
|
|
* Written by Paul Brook
|
|
|
|
*
|
2011-06-26 06:21:35 +04:00
|
|
|
* This code is licensed under the GPL.
|
2006-04-09 05:32:52 +04:00
|
|
|
*/
|
|
|
|
|
2016-01-26 21:17:05 +03:00
|
|
|
#include "qemu/osdep.h"
|
2013-02-04 18:40:22 +04:00
|
|
|
#include "hw/sysbus.h"
|
2013-02-05 20:06:20 +04:00
|
|
|
#include "hw/input/ps2.h"
|
2015-12-15 15:16:16 +03:00
|
|
|
#include "qemu/log.h"
|
2006-04-09 05:32:52 +04:00
|
|
|
|
2013-07-26 20:49:24 +04:00
|
|
|
#define TYPE_PL050 "pl050"
|
|
|
|
#define PL050(obj) OBJECT_CHECK(PL050State, (obj), TYPE_PL050)
|
|
|
|
|
2013-07-26 20:40:25 +04:00
|
|
|
typedef struct PL050State {
|
2013-07-26 20:49:24 +04:00
|
|
|
SysBusDevice parent_obj;
|
|
|
|
|
2011-10-10 19:18:44 +04:00
|
|
|
MemoryRegion iomem;
|
2006-04-09 05:32:52 +04:00
|
|
|
void *dev;
|
|
|
|
uint32_t cr;
|
|
|
|
uint32_t clk;
|
|
|
|
uint32_t last;
|
|
|
|
int pending;
|
2007-04-07 22:14:41 +04:00
|
|
|
qemu_irq irq;
|
2013-07-26 20:49:24 +04:00
|
|
|
bool is_mouse;
|
2013-07-26 20:40:25 +04:00
|
|
|
} PL050State;
|
2006-04-09 05:32:52 +04:00
|
|
|
|
2010-12-23 20:19:54 +03:00
|
|
|
static const VMStateDescription vmstate_pl050 = {
|
|
|
|
.name = "pl050",
|
2013-04-05 19:17:58 +04:00
|
|
|
.version_id = 2,
|
|
|
|
.minimum_version_id = 2,
|
2010-12-23 20:19:54 +03:00
|
|
|
.fields = (VMStateField[]) {
|
2013-07-26 20:40:25 +04:00
|
|
|
VMSTATE_UINT32(cr, PL050State),
|
|
|
|
VMSTATE_UINT32(clk, PL050State),
|
|
|
|
VMSTATE_UINT32(last, PL050State),
|
|
|
|
VMSTATE_INT32(pending, PL050State),
|
2010-12-23 20:19:54 +03:00
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-04-30 06:39:55 +04: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 05:32:52 +04: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 20:40:25 +04:00
|
|
|
PL050State *s = (PL050State *)opaque;
|
2006-04-09 05:32:52 +04:00
|
|
|
int raise;
|
|
|
|
|
|
|
|
s->pending = level;
|
|
|
|
raise = (s->pending && (s->cr & 0x10) != 0)
|
|
|
|
|| (s->cr & 0x08) != 0;
|
2007-04-07 22:14:41 +04:00
|
|
|
qemu_set_irq(s->irq, raise);
|
2006-04-09 05:32:52 +04:00
|
|
|
}
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static uint64_t pl050_read(void *opaque, hwaddr offset,
|
2011-10-10 19:18:44 +04:00
|
|
|
unsigned size)
|
2006-04-09 05:32:52 +04:00
|
|
|
{
|
2013-07-26 20:40:25 +04:00
|
|
|
PL050State *s = (PL050State *)opaque;
|
2006-04-09 05:32:52 +04: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 06:39:55 +04: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 05:32:52 +04: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 11:45:08 +04:00
|
|
|
qemu_log_mask(LOG_GUEST_ERROR,
|
|
|
|
"pl050_read: Bad offset %x\n", (int)offset);
|
2006-04-09 05:32:52 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static void pl050_write(void *opaque, hwaddr offset,
|
2011-10-10 19:18:44 +04:00
|
|
|
uint64_t value, unsigned size)
|
2006-04-09 05:32:52 +04:00
|
|
|
{
|
2013-07-26 20:40:25 +04:00
|
|
|
PL050State *s = (PL050State *)opaque;
|
2006-04-09 05:32:52 +04: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 11:45:08 +04:00
|
|
|
qemu_log_mask(LOG_GUEST_ERROR,
|
|
|
|
"pl050_write: Bad offset %x\n", (int)offset);
|
2006-04-09 05:32:52 +04:00
|
|
|
}
|
|
|
|
}
|
2011-10-10 19:18:44 +04:00
|
|
|
static const MemoryRegionOps pl050_ops = {
|
|
|
|
.read = pl050_read,
|
|
|
|
.write = pl050_write,
|
|
|
|
.endianness = DEVICE_NATIVE_ENDIAN,
|
2006-04-09 05:32:52 +04:00
|
|
|
};
|
|
|
|
|
2018-12-13 16:47:59 +03:00
|
|
|
static void pl050_realize(DeviceState *dev, Error **errp)
|
2006-04-09 05:32:52 +04:00
|
|
|
{
|
2013-07-26 20:49:24 +04:00
|
|
|
PL050State *s = PL050(dev);
|
2018-12-13 16:47:59 +03:00
|
|
|
SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
|
2006-04-09 05:32:52 +04:00
|
|
|
|
2013-06-07 05:25:08 +04:00
|
|
|
memory_region_init_io(&s->iomem, OBJECT(s), &pl050_ops, s, "pl050", 0x1000);
|
2018-12-13 16:47:59 +03:00
|
|
|
sysbus_init_mmio(sbd, &s->iomem);
|
|
|
|
sysbus_init_irq(sbd, &s->irq);
|
2013-07-26 20:49:24 +04:00
|
|
|
if (s->is_mouse) {
|
2006-04-09 05:32:52 +04:00
|
|
|
s->dev = ps2_mouse_init(pl050_update, s);
|
2013-07-26 20:49:24 +04:00
|
|
|
} else {
|
2006-04-09 05:32:52 +04:00
|
|
|
s->dev = ps2_kbd_init(pl050_update, s);
|
2013-07-26 20:49:24 +04:00
|
|
|
}
|
2006-04-09 05:32:52 +04:00
|
|
|
}
|
2009-05-15 01:35:07 +04:00
|
|
|
|
2013-07-26 20:49:24 +04:00
|
|
|
static void pl050_keyboard_init(Object *obj)
|
2009-05-15 01:35:07 +04:00
|
|
|
{
|
2013-07-26 20:49:24 +04:00
|
|
|
PL050State *s = PL050(obj);
|
2009-05-15 01:35:07 +04:00
|
|
|
|
2013-07-26 20:49:24 +04:00
|
|
|
s->is_mouse = false;
|
2009-05-15 01:35:07 +04:00
|
|
|
}
|
|
|
|
|
2013-07-26 20:49:24 +04:00
|
|
|
static void pl050_mouse_init(Object *obj)
|
2012-01-24 23:12:29 +04:00
|
|
|
{
|
2013-07-26 20:49:24 +04:00
|
|
|
PL050State *s = PL050(obj);
|
2012-01-24 23:12:29 +04:00
|
|
|
|
2013-07-26 20:49:24 +04:00
|
|
|
s->is_mouse = true;
|
2012-01-24 23:12:29 +04:00
|
|
|
}
|
|
|
|
|
2013-01-10 19:19:07 +04:00
|
|
|
static const TypeInfo pl050_kbd_info = {
|
2011-12-08 07:34:16 +04:00
|
|
|
.name = "pl050_keyboard",
|
2013-07-26 20:49:24 +04:00
|
|
|
.parent = TYPE_PL050,
|
|
|
|
.instance_init = pl050_keyboard_init,
|
2010-12-23 20:19:54 +03:00
|
|
|
};
|
|
|
|
|
2013-07-26 20:49:24 +04: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 23:12:29 +04:00
|
|
|
{
|
2013-07-26 20:49:24 +04:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(oc);
|
2012-01-24 23:12:29 +04:00
|
|
|
|
2018-12-13 16:47:59 +03:00
|
|
|
dc->realize = pl050_realize;
|
2011-12-08 07:34:16 +04:00
|
|
|
dc->vmsd = &vmstate_pl050;
|
2012-01-24 23:12:29 +04:00
|
|
|
}
|
|
|
|
|
2013-07-26 20:49:24 +04:00
|
|
|
static const TypeInfo pl050_type_info = {
|
|
|
|
.name = TYPE_PL050,
|
2011-12-08 07:34:16 +04:00
|
|
|
.parent = TYPE_SYS_BUS_DEVICE,
|
2013-07-26 20:40:25 +04:00
|
|
|
.instance_size = sizeof(PL050State),
|
2013-07-26 20:49:24 +04:00
|
|
|
.abstract = true,
|
|
|
|
.class_init = pl050_class_init,
|
2010-12-23 20:19:54 +03:00
|
|
|
};
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
static void pl050_register_types(void)
|
2009-05-15 01:35:07 +04:00
|
|
|
{
|
2013-07-26 20:49:24 +04:00
|
|
|
type_register_static(&pl050_type_info);
|
2011-12-08 07:34:16 +04:00
|
|
|
type_register_static(&pl050_kbd_info);
|
|
|
|
type_register_static(&pl050_mouse_info);
|
2009-05-15 01:35:07 +04:00
|
|
|
}
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
type_init(pl050_register_types)
|