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