cmd: Fix potential memory leak
[qemu/ar7.git] / hw / pl050.c
blobf7fa2e253cfd47cf55a90f024b0d6dcd0815dd52
1 /*
2 * Arm PrimeCell PL050 Keyboard / Mouse Interface
4 * Copyright (c) 2006-2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GPL.
8 */
10 #include "sysbus.h"
11 #include "ps2.h"
13 typedef struct {
14 SysBusDevice busdev;
15 void *dev;
16 uint32_t cr;
17 uint32_t clk;
18 uint32_t last;
19 int pending;
20 qemu_irq irq;
21 int is_mouse;
22 } pl050_state;
24 static 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()
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)
46 static const unsigned char pl050_id[] =
47 { 0x50, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
49 static void pl050_update(void *opaque, int level)
51 pl050_state *s = (pl050_state *)opaque;
52 int raise;
54 s->pending = level;
55 raise = (s->pending && (s->cr & 0x10) != 0)
56 || (s->cr & 0x08) != 0;
57 qemu_set_irq(s->irq, raise);
60 static uint32_t pl050_read(void *opaque, target_phys_addr_t offset)
62 pl050_state *s = (pl050_state *)opaque;
63 if (offset >= 0xfe0 && offset < 0x1000)
64 return pl050_id[(offset - 0xfe0) >> 2];
66 switch (offset >> 2) {
67 case 0: /* KMICR */
68 return s->cr;
69 case 1: /* KMISTAT */
71 uint8_t val;
72 uint32_t stat;
74 val = s->last;
75 val = val ^ (val >> 4);
76 val = val ^ (val >> 2);
77 val = (val ^ (val >> 1)) & 1;
79 stat = PL050_TXEMPTY;
80 if (val)
81 stat |= PL050_RXPARITY;
82 if (s->pending)
83 stat |= PL050_RXFULL;
85 return stat;
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:
96 hw_error("pl050_read: Bad offset %x\n", (int)offset);
97 return 0;
101 static void pl050_write(void *opaque, target_phys_addr_t offset,
102 uint32_t value)
104 pl050_state *s = (pl050_state *)opaque;
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);
119 break;
120 case 3: /* KMICLKDIV */
121 s->clk = value;
122 return;
123 default:
124 hw_error("pl050_write: Bad offset %x\n", (int)offset);
127 static CPUReadMemoryFunc * const pl050_readfn[] = {
128 pl050_read,
129 pl050_read,
130 pl050_read
133 static CPUWriteMemoryFunc * const pl050_writefn[] = {
134 pl050_write,
135 pl050_write,
136 pl050_write
139 static int pl050_init(SysBusDevice *dev, int is_mouse)
141 pl050_state *s = FROM_SYSBUS(pl050_state, dev);
142 int iomemtype;
144 iomemtype = cpu_register_io_memory(pl050_readfn,
145 pl050_writefn, s,
146 DEVICE_NATIVE_ENDIAN);
147 sysbus_init_mmio(dev, 0x1000, iomemtype);
148 sysbus_init_irq(dev, &s->irq);
149 s->is_mouse = is_mouse;
150 if (s->is_mouse)
151 s->dev = ps2_mouse_init(pl050_update, s);
152 else
153 s->dev = ps2_kbd_init(pl050_update, s);
154 return 0;
157 static int pl050_init_keyboard(SysBusDevice *dev)
159 return pl050_init(dev, 0);
162 static int pl050_init_mouse(SysBusDevice *dev)
164 return pl050_init(dev, 1);
167 static 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,
174 static 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,
181 static void pl050_register_devices(void)
183 sysbus_register_withprop(&pl050_kbd_info);
184 sysbus_register_withprop(&pl050_mouse_info);
187 device_init(pl050_register_devices)