xref: /openbmc/qemu/hw/input/pl050.c (revision 423bcb234b358a46c868f6baa0483c84870825c5)
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 "qemu/osdep.h"
11 #include "hw/sysbus.h"
12 #include "migration/vmstate.h"
13 #include "hw/input/ps2.h"
14 #include "hw/irq.h"
15 #include "qemu/log.h"
16 #include "qemu/module.h"
17 #include "qom/object.h"
18 
19 #define TYPE_PL050 "pl050"
20 OBJECT_DECLARE_SIMPLE_TYPE(PL050State, PL050)
21 
22 struct PL050State {
23     SysBusDevice parent_obj;
24 
25     MemoryRegion iomem;
26     void *dev;
27     uint32_t cr;
28     uint32_t clk;
29     uint32_t last;
30     int pending;
31     qemu_irq irq;
32     bool is_mouse;
33 };
34 
35 static const VMStateDescription vmstate_pl050 = {
36     .name = "pl050",
37     .version_id = 2,
38     .minimum_version_id = 2,
39     .fields = (VMStateField[]) {
40         VMSTATE_UINT32(cr, PL050State),
41         VMSTATE_UINT32(clk, PL050State),
42         VMSTATE_UINT32(last, PL050State),
43         VMSTATE_INT32(pending, PL050State),
44         VMSTATE_END_OF_LIST()
45     }
46 };
47 
48 #define PL050_TXEMPTY         (1 << 6)
49 #define PL050_TXBUSY          (1 << 5)
50 #define PL050_RXFULL          (1 << 4)
51 #define PL050_RXBUSY          (1 << 3)
52 #define PL050_RXPARITY        (1 << 2)
53 #define PL050_KMIC            (1 << 1)
54 #define PL050_KMID            (1 << 0)
55 
56 static const unsigned char pl050_id[] = {
57     0x50, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1
58 };
59 
60 static void pl050_update_irq(PL050State *s)
61 {
62     int level = (s->pending && (s->cr & 0x10) != 0)
63                  || (s->cr & 0x08) != 0;
64 
65     qemu_set_irq(s->irq, level);
66 }
67 
68 static void pl050_set_irq(void *opaque, int n, int level)
69 {
70     PL050State *s = (PL050State *)opaque;
71 
72     s->pending = level;
73     pl050_update_irq(s);
74 }
75 
76 static uint64_t pl050_read(void *opaque, hwaddr offset,
77                            unsigned size)
78 {
79     PL050State *s = (PL050State *)opaque;
80 
81     if (offset >= 0xfe0 && offset < 0x1000) {
82         return pl050_id[(offset - 0xfe0) >> 2];
83     }
84 
85     switch (offset >> 2) {
86     case 0: /* KMICR */
87         return s->cr;
88     case 1: /* KMISTAT */
89         {
90             uint8_t val;
91             uint32_t stat;
92 
93             val = s->last;
94             val = val ^ (val >> 4);
95             val = val ^ (val >> 2);
96             val = (val ^ (val >> 1)) & 1;
97 
98             stat = PL050_TXEMPTY;
99             if (val) {
100                 stat |= PL050_RXPARITY;
101             }
102             if (s->pending) {
103                 stat |= PL050_RXFULL;
104             }
105 
106             return stat;
107         }
108     case 2: /* KMIDATA */
109         if (s->pending) {
110             s->last = ps2_read_data(s->dev);
111         }
112         return s->last;
113     case 3: /* KMICLKDIV */
114         return s->clk;
115     case 4: /* KMIIR */
116         return s->pending | 2;
117     default:
118         qemu_log_mask(LOG_GUEST_ERROR,
119                       "pl050_read: Bad offset %x\n", (int)offset);
120         return 0;
121     }
122 }
123 
124 static void pl050_write(void *opaque, hwaddr offset,
125                         uint64_t value, unsigned size)
126 {
127     PL050State *s = (PL050State *)opaque;
128 
129     switch (offset >> 2) {
130     case 0: /* KMICR */
131         s->cr = value;
132         pl050_update_irq(s);
133         /* ??? Need to implement the enable/disable bit.  */
134         break;
135     case 2: /* KMIDATA */
136         /* ??? This should toggle the TX interrupt line.  */
137         /* ??? This means kbd/mouse can block each other.  */
138         if (s->is_mouse) {
139             ps2_write_mouse(s->dev, value);
140         } else {
141             ps2_write_keyboard(s->dev, value);
142         }
143         break;
144     case 3: /* KMICLKDIV */
145         s->clk = value;
146         return;
147     default:
148         qemu_log_mask(LOG_GUEST_ERROR,
149                       "pl050_write: Bad offset %x\n", (int)offset);
150     }
151 }
152 static const MemoryRegionOps pl050_ops = {
153     .read = pl050_read,
154     .write = pl050_write,
155     .endianness = DEVICE_NATIVE_ENDIAN,
156 };
157 
158 static void pl050_realize(DeviceState *dev, Error **errp)
159 {
160     PL050State *s = PL050(dev);
161     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
162 
163     memory_region_init_io(&s->iomem, OBJECT(s), &pl050_ops, s, "pl050", 0x1000);
164     sysbus_init_mmio(sbd, &s->iomem);
165     sysbus_init_irq(sbd, &s->irq);
166     if (s->is_mouse) {
167         s->dev = ps2_mouse_init(NULL, NULL);
168     } else {
169         s->dev = ps2_kbd_init(NULL, NULL);
170     }
171     qdev_connect_gpio_out(DEVICE(s->dev), PS2_DEVICE_IRQ,
172                           qdev_get_gpio_in_named(dev, "ps2-input-irq", 0));
173 }
174 
175 static void pl050_keyboard_init(Object *obj)
176 {
177     PL050State *s = PL050(obj);
178 
179     s->is_mouse = false;
180 }
181 
182 static void pl050_mouse_init(Object *obj)
183 {
184     PL050State *s = PL050(obj);
185 
186     s->is_mouse = true;
187 }
188 
189 static const TypeInfo pl050_kbd_info = {
190     .name          = "pl050_keyboard",
191     .parent        = TYPE_PL050,
192     .instance_init = pl050_keyboard_init,
193 };
194 
195 static const TypeInfo pl050_mouse_info = {
196     .name          = "pl050_mouse",
197     .parent        = TYPE_PL050,
198     .instance_init = pl050_mouse_init,
199 };
200 
201 static void pl050_init(Object *obj)
202 {
203     qdev_init_gpio_in_named(DEVICE(obj), pl050_set_irq, "ps2-input-irq", 1);
204 }
205 
206 static void pl050_class_init(ObjectClass *oc, void *data)
207 {
208     DeviceClass *dc = DEVICE_CLASS(oc);
209 
210     dc->realize = pl050_realize;
211     dc->vmsd = &vmstate_pl050;
212 }
213 
214 static const TypeInfo pl050_type_info = {
215     .name          = TYPE_PL050,
216     .parent        = TYPE_SYS_BUS_DEVICE,
217     .instance_init = pl050_init,
218     .instance_size = sizeof(PL050State),
219     .abstract      = true,
220     .class_init    = pl050_class_init,
221 };
222 
223 static void pl050_register_types(void)
224 {
225     type_register_static(&pl050_type_info);
226     type_register_static(&pl050_kbd_info);
227     type_register_static(&pl050_mouse_info);
228 }
229 
230 type_init(pl050_register_types)
231