blob: c1b08d5a407812d205e38a8942b00674bdcbf852 [file] [log] [blame]
ths5fafdf22007-09-16 21:08:06 +00001/*
pbrook69db0ac2006-07-17 18:45:34 +00002 * Arm PrimeCell PL050 Keyboard / Mouse Interface
pbrookcdbdb642006-04-09 01:32:52 +00003 *
pbrook9e61ec32007-04-30 02:39:55 +00004 * Copyright (c) 2006-2007 CodeSourcery.
pbrookcdbdb642006-04-09 01:32:52 +00005 * Written by Paul Brook
6 *
Matthew Fernandez8e31bf32011-06-26 12:21:35 +10007 * This code is licensed under the GPL.
pbrookcdbdb642006-04-09 01:32:52 +00008 */
9
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010010#include "hw/sysbus.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010011#include "hw/input/ps2.h"
pbrookcdbdb642006-04-09 01:32:52 +000012
Andreas Färber3e5dd362013-07-26 18:49:24 +020013#define TYPE_PL050 "pl050"
14#define PL050(obj) OBJECT_CHECK(PL050State, (obj), TYPE_PL050)
15
Andreas Färbere607f252013-07-26 18:40:25 +020016typedef struct PL050State {
Andreas Färber3e5dd362013-07-26 18:49:24 +020017 SysBusDevice parent_obj;
18
Avi Kivityb8f7a732011-10-10 17:18:44 +020019 MemoryRegion iomem;
pbrookcdbdb642006-04-09 01:32:52 +000020 void *dev;
pbrookcdbdb642006-04-09 01:32:52 +000021 uint32_t cr;
22 uint32_t clk;
23 uint32_t last;
pbrookcdbdb642006-04-09 01:32:52 +000024 int pending;
pbrookd537cf62007-04-07 18:14:41 +000025 qemu_irq irq;
Andreas Färber3e5dd362013-07-26 18:49:24 +020026 bool is_mouse;
Andreas Färbere607f252013-07-26 18:40:25 +020027} PL050State;
pbrookcdbdb642006-04-09 01:32:52 +000028
Peter Maydelld6ac1722010-12-23 17:19:54 +000029static const VMStateDescription vmstate_pl050 = {
30 .name = "pl050",
Peter Maydelle8945b42013-04-05 16:17:58 +010031 .version_id = 2,
32 .minimum_version_id = 2,
Peter Maydelld6ac1722010-12-23 17:19:54 +000033 .fields = (VMStateField[]) {
Andreas Färbere607f252013-07-26 18:40:25 +020034 VMSTATE_UINT32(cr, PL050State),
35 VMSTATE_UINT32(clk, PL050State),
36 VMSTATE_UINT32(last, PL050State),
37 VMSTATE_INT32(pending, PL050State),
Peter Maydelld6ac1722010-12-23 17:19:54 +000038 VMSTATE_END_OF_LIST()
39 }
40};
41
pbrook9e61ec32007-04-30 02:39:55 +000042#define PL050_TXEMPTY (1 << 6)
43#define PL050_TXBUSY (1 << 5)
44#define PL050_RXFULL (1 << 4)
45#define PL050_RXBUSY (1 << 3)
46#define PL050_RXPARITY (1 << 2)
47#define PL050_KMIC (1 << 1)
48#define PL050_KMID (1 << 0)
49
pbrookcdbdb642006-04-09 01:32:52 +000050static const unsigned char pl050_id[] =
51{ 0x50, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
52
53static void pl050_update(void *opaque, int level)
54{
Andreas Färbere607f252013-07-26 18:40:25 +020055 PL050State *s = (PL050State *)opaque;
pbrookcdbdb642006-04-09 01:32:52 +000056 int raise;
57
58 s->pending = level;
59 raise = (s->pending && (s->cr & 0x10) != 0)
60 || (s->cr & 0x08) != 0;
pbrookd537cf62007-04-07 18:14:41 +000061 qemu_set_irq(s->irq, raise);
pbrookcdbdb642006-04-09 01:32:52 +000062}
63
Avi Kivitya8170e52012-10-23 12:30:10 +020064static uint64_t pl050_read(void *opaque, hwaddr offset,
Avi Kivityb8f7a732011-10-10 17:18:44 +020065 unsigned size)
pbrookcdbdb642006-04-09 01:32:52 +000066{
Andreas Färbere607f252013-07-26 18:40:25 +020067 PL050State *s = (PL050State *)opaque;
pbrookcdbdb642006-04-09 01:32:52 +000068 if (offset >= 0xfe0 && offset < 0x1000)
69 return pl050_id[(offset - 0xfe0) >> 2];
70
71 switch (offset >> 2) {
72 case 0: /* KMICR */
73 return s->cr;
74 case 1: /* KMISTAT */
pbrook9e61ec32007-04-30 02:39:55 +000075 {
76 uint8_t val;
77 uint32_t stat;
78
79 val = s->last;
80 val = val ^ (val >> 4);
81 val = val ^ (val >> 2);
82 val = (val ^ (val >> 1)) & 1;
83
84 stat = PL050_TXEMPTY;
85 if (val)
86 stat |= PL050_RXPARITY;
87 if (s->pending)
88 stat |= PL050_RXFULL;
89
90 return stat;
pbrookcdbdb642006-04-09 01:32:52 +000091 }
92 case 2: /* KMIDATA */
93 if (s->pending)
94 s->last = ps2_read_data(s->dev);
95 return s->last;
96 case 3: /* KMICLKDIV */
97 return s->clk;
98 case 4: /* KMIIR */
99 return s->pending | 2;
100 default:
Peter Maydellfbfecf42012-10-30 07:45:08 +0000101 qemu_log_mask(LOG_GUEST_ERROR,
102 "pl050_read: Bad offset %x\n", (int)offset);
pbrookcdbdb642006-04-09 01:32:52 +0000103 return 0;
104 }
105}
106
Avi Kivitya8170e52012-10-23 12:30:10 +0200107static void pl050_write(void *opaque, hwaddr offset,
Avi Kivityb8f7a732011-10-10 17:18:44 +0200108 uint64_t value, unsigned size)
pbrookcdbdb642006-04-09 01:32:52 +0000109{
Andreas Färbere607f252013-07-26 18:40:25 +0200110 PL050State *s = (PL050State *)opaque;
pbrookcdbdb642006-04-09 01:32:52 +0000111 switch (offset >> 2) {
112 case 0: /* KMICR */
113 s->cr = value;
114 pl050_update(s, s->pending);
115 /* ??? Need to implement the enable/disable bit. */
116 break;
117 case 2: /* KMIDATA */
118 /* ??? This should toggle the TX interrupt line. */
119 /* ??? This means kbd/mouse can block each other. */
120 if (s->is_mouse) {
121 ps2_write_mouse(s->dev, value);
122 } else {
123 ps2_write_keyboard(s->dev, value);
124 }
125 break;
126 case 3: /* KMICLKDIV */
127 s->clk = value;
128 return;
129 default:
Peter Maydellfbfecf42012-10-30 07:45:08 +0000130 qemu_log_mask(LOG_GUEST_ERROR,
131 "pl050_write: Bad offset %x\n", (int)offset);
pbrookcdbdb642006-04-09 01:32:52 +0000132 }
133}
Avi Kivityb8f7a732011-10-10 17:18:44 +0200134static const MemoryRegionOps pl050_ops = {
135 .read = pl050_read,
136 .write = pl050_write,
137 .endianness = DEVICE_NATIVE_ENDIAN,
pbrookcdbdb642006-04-09 01:32:52 +0000138};
139
Andreas Färber3e5dd362013-07-26 18:49:24 +0200140static int pl050_initfn(SysBusDevice *dev)
pbrookcdbdb642006-04-09 01:32:52 +0000141{
Andreas Färber3e5dd362013-07-26 18:49:24 +0200142 PL050State *s = PL050(dev);
pbrookcdbdb642006-04-09 01:32:52 +0000143
Paolo Bonzini1437c942013-06-06 21:25:08 -0400144 memory_region_init_io(&s->iomem, OBJECT(s), &pl050_ops, s, "pl050", 0x1000);
Avi Kivity750ecd42011-11-27 11:38:10 +0200145 sysbus_init_mmio(dev, &s->iomem);
Paul Brook86394e92009-05-14 22:35:07 +0100146 sysbus_init_irq(dev, &s->irq);
Andreas Färber3e5dd362013-07-26 18:49:24 +0200147 if (s->is_mouse) {
pbrookcdbdb642006-04-09 01:32:52 +0000148 s->dev = ps2_mouse_init(pl050_update, s);
Andreas Färber3e5dd362013-07-26 18:49:24 +0200149 } else {
pbrookcdbdb642006-04-09 01:32:52 +0000150 s->dev = ps2_kbd_init(pl050_update, s);
Andreas Färber3e5dd362013-07-26 18:49:24 +0200151 }
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200152 return 0;
pbrookcdbdb642006-04-09 01:32:52 +0000153}
Paul Brook86394e92009-05-14 22:35:07 +0100154
Andreas Färber3e5dd362013-07-26 18:49:24 +0200155static void pl050_keyboard_init(Object *obj)
Paul Brook86394e92009-05-14 22:35:07 +0100156{
Andreas Färber3e5dd362013-07-26 18:49:24 +0200157 PL050State *s = PL050(obj);
158
159 s->is_mouse = false;
Paul Brook86394e92009-05-14 22:35:07 +0100160}
161
Andreas Färber3e5dd362013-07-26 18:49:24 +0200162static void pl050_mouse_init(Object *obj)
Paul Brook86394e92009-05-14 22:35:07 +0100163{
Andreas Färber3e5dd362013-07-26 18:49:24 +0200164 PL050State *s = PL050(obj);
Paul Brook86394e92009-05-14 22:35:07 +0100165
Andreas Färber3e5dd362013-07-26 18:49:24 +0200166 s->is_mouse = true;
Anthony Liguori999e12b2012-01-24 13:12:29 -0600167}
168
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100169static const TypeInfo pl050_kbd_info = {
Anthony Liguori39bffca2011-12-07 21:34:16 -0600170 .name = "pl050_keyboard",
Andreas Färber3e5dd362013-07-26 18:49:24 +0200171 .parent = TYPE_PL050,
172 .instance_init = pl050_keyboard_init,
Peter Maydelld6ac1722010-12-23 17:19:54 +0000173};
174
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100175static const TypeInfo pl050_mouse_info = {
Anthony Liguori39bffca2011-12-07 21:34:16 -0600176 .name = "pl050_mouse",
Andreas Färber3e5dd362013-07-26 18:49:24 +0200177 .parent = TYPE_PL050,
178 .instance_init = pl050_mouse_init,
179};
180
181static void pl050_class_init(ObjectClass *oc, void *data)
182{
183 DeviceClass *dc = DEVICE_CLASS(oc);
184 SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(oc);
185
186 sdc->init = pl050_initfn;
187 dc->vmsd = &vmstate_pl050;
188}
189
190static const TypeInfo pl050_type_info = {
191 .name = TYPE_PL050,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600192 .parent = TYPE_SYS_BUS_DEVICE,
Andreas Färbere607f252013-07-26 18:40:25 +0200193 .instance_size = sizeof(PL050State),
Andreas Färber3e5dd362013-07-26 18:49:24 +0200194 .abstract = true,
195 .class_init = pl050_class_init,
Peter Maydelld6ac1722010-12-23 17:19:54 +0000196};
197
Andreas Färber83f7d432012-02-09 15:20:55 +0100198static void pl050_register_types(void)
Paul Brook86394e92009-05-14 22:35:07 +0100199{
Andreas Färber3e5dd362013-07-26 18:49:24 +0200200 type_register_static(&pl050_type_info);
Anthony Liguori39bffca2011-12-07 21:34:16 -0600201 type_register_static(&pl050_kbd_info);
202 type_register_static(&pl050_mouse_info);
Paul Brook86394e92009-05-14 22:35:07 +0100203}
204
Andreas Färber83f7d432012-02-09 15:20:55 +0100205type_init(pl050_register_types)