]> git.proxmox.com Git - qemu.git/blame - hw/pl050.c
report serial devices created with -device in the PIIX4 config space
[qemu.git] / hw / pl050.c
CommitLineData
5fafdf24 1/*
69db0ac7 2 * Arm PrimeCell PL050 Keyboard / Mouse Interface
cdbdb648 3 *
9e61ec31 4 * Copyright (c) 2006-2007 CodeSourcery.
cdbdb648
PB
5 * Written by Paul Brook
6 *
7 * This code is licenced under the GPL.
8 */
9
86394e96 10#include "sysbus.h"
87ecb68b 11#include "ps2.h"
cdbdb648
PB
12
13typedef struct {
86394e96 14 SysBusDevice busdev;
cdbdb648 15 void *dev;
cdbdb648
PB
16 uint32_t cr;
17 uint32_t clk;
18 uint32_t last;
cdbdb648 19 int pending;
d537cf6c 20 qemu_irq irq;
cdbdb648
PB
21 int is_mouse;
22} pl050_state;
23
d6ac172a
PM
24static const VMStateDescription vmstate_pl050 = {
25 .name = "pl050",
26 .version_id = 1,
27 .minimum_version_id = 1,
28 .fields = (VMStateField[]) {
29 VMSTATE_UINT32(cr, pl050_state),
30 VMSTATE_UINT32(clk, pl050_state),
31 VMSTATE_UINT32(last, pl050_state),
32 VMSTATE_INT32(pending, pl050_state),
33 VMSTATE_INT32(is_mouse, pl050_state),
34 VMSTATE_END_OF_LIST()
35 }
36};
37
9e61ec31
PB
38#define PL050_TXEMPTY (1 << 6)
39#define PL050_TXBUSY (1 << 5)
40#define PL050_RXFULL (1 << 4)
41#define PL050_RXBUSY (1 << 3)
42#define PL050_RXPARITY (1 << 2)
43#define PL050_KMIC (1 << 1)
44#define PL050_KMID (1 << 0)
45
cdbdb648
PB
46static const unsigned char pl050_id[] =
47{ 0x50, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
48
49static void pl050_update(void *opaque, int level)
50{
51 pl050_state *s = (pl050_state *)opaque;
52 int raise;
53
54 s->pending = level;
55 raise = (s->pending && (s->cr & 0x10) != 0)
56 || (s->cr & 0x08) != 0;
d537cf6c 57 qemu_set_irq(s->irq, raise);
cdbdb648
PB
58}
59
c227f099 60static uint32_t pl050_read(void *opaque, target_phys_addr_t offset)
cdbdb648
PB
61{
62 pl050_state *s = (pl050_state *)opaque;
cdbdb648
PB
63 if (offset >= 0xfe0 && offset < 0x1000)
64 return pl050_id[(offset - 0xfe0) >> 2];
65
66 switch (offset >> 2) {
67 case 0: /* KMICR */
68 return s->cr;
69 case 1: /* KMISTAT */
9e61ec31
PB
70 {
71 uint8_t val;
72 uint32_t stat;
73
74 val = s->last;
75 val = val ^ (val >> 4);
76 val = val ^ (val >> 2);
77 val = (val ^ (val >> 1)) & 1;
78
79 stat = PL050_TXEMPTY;
80 if (val)
81 stat |= PL050_RXPARITY;
82 if (s->pending)
83 stat |= PL050_RXFULL;
84
85 return stat;
cdbdb648
PB
86 }
87 case 2: /* KMIDATA */
88 if (s->pending)
89 s->last = ps2_read_data(s->dev);
90 return s->last;
91 case 3: /* KMICLKDIV */
92 return s->clk;
93 case 4: /* KMIIR */
94 return s->pending | 2;
95 default:
2ac71179 96 hw_error("pl050_read: Bad offset %x\n", (int)offset);
cdbdb648
PB
97 return 0;
98 }
99}
100
c227f099 101static void pl050_write(void *opaque, target_phys_addr_t offset,
cdbdb648
PB
102 uint32_t value)
103{
104 pl050_state *s = (pl050_state *)opaque;
cdbdb648
PB
105 switch (offset >> 2) {
106 case 0: /* KMICR */
107 s->cr = value;
108 pl050_update(s, s->pending);
109 /* ??? Need to implement the enable/disable bit. */
110 break;
111 case 2: /* KMIDATA */
112 /* ??? This should toggle the TX interrupt line. */
113 /* ??? This means kbd/mouse can block each other. */
114 if (s->is_mouse) {
115 ps2_write_mouse(s->dev, value);
116 } else {
117 ps2_write_keyboard(s->dev, value);
118 }
119 break;
120 case 3: /* KMICLKDIV */
121 s->clk = value;
122 return;
123 default:
2ac71179 124 hw_error("pl050_write: Bad offset %x\n", (int)offset);
cdbdb648
PB
125 }
126}
d60efc6b 127static CPUReadMemoryFunc * const pl050_readfn[] = {
cdbdb648
PB
128 pl050_read,
129 pl050_read,
130 pl050_read
131};
132
d60efc6b 133static CPUWriteMemoryFunc * const pl050_writefn[] = {
cdbdb648
PB
134 pl050_write,
135 pl050_write,
136 pl050_write
137};
138
81a322d4 139static int pl050_init(SysBusDevice *dev, int is_mouse)
cdbdb648 140{
86394e96 141 pl050_state *s = FROM_SYSBUS(pl050_state, dev);
cdbdb648 142 int iomemtype;
cdbdb648 143
1eed09cb 144 iomemtype = cpu_register_io_memory(pl050_readfn,
2507c12a
AG
145 pl050_writefn, s,
146 DEVICE_NATIVE_ENDIAN);
86394e96
PB
147 sysbus_init_mmio(dev, 0x1000, iomemtype);
148 sysbus_init_irq(dev, &s->irq);
cdbdb648 149 s->is_mouse = is_mouse;
86394e96 150 if (s->is_mouse)
cdbdb648
PB
151 s->dev = ps2_mouse_init(pl050_update, s);
152 else
153 s->dev = ps2_kbd_init(pl050_update, s);
81a322d4 154 return 0;
cdbdb648 155}
86394e96 156
81a322d4 157static int pl050_init_keyboard(SysBusDevice *dev)
86394e96 158{
81a322d4 159 return pl050_init(dev, 0);
86394e96
PB
160}
161
81a322d4 162static int pl050_init_mouse(SysBusDevice *dev)
86394e96 163{
81a322d4 164 return pl050_init(dev, 1);
86394e96
PB
165}
166
d6ac172a
PM
167static SysBusDeviceInfo pl050_kbd_info = {
168 .init = pl050_init_keyboard,
169 .qdev.name = "pl050_keyboard",
170 .qdev.size = sizeof(pl050_state),
171 .qdev.vmsd = &vmstate_pl050,
172};
173
174static SysBusDeviceInfo pl050_mouse_info = {
175 .init = pl050_init_mouse,
176 .qdev.name = "pl050_mouse",
177 .qdev.size = sizeof(pl050_state),
178 .qdev.vmsd = &vmstate_pl050,
179};
180
86394e96
PB
181static void pl050_register_devices(void)
182{
d6ac172a
PM
183 sysbus_register_withprop(&pl050_kbd_info);
184 sysbus_register_withprop(&pl050_mouse_info);
86394e96
PB
185}
186
187device_init(pl050_register_devices)