]> Git Repo - qemu.git/blob - hw/misc/max111x.c
intel_iommu: Fix unexpected unmaps during global unmap
[qemu.git] / hw / misc / max111x.c
1 /*
2  * Maxim MAX1110/1111 ADC chip emulation.
3  *
4  * Copyright (c) 2006 Openedhand Ltd.
5  * Written by Andrzej Zaborowski <[email protected]>
6  *
7  * This code is licensed under the GNU GPLv2.
8  *
9  * Contributions after 2012-01-13 are licensed under the terms of the
10  * GNU GPL, version 2 or (at your option) any later version.
11  */
12
13 #include "qemu/osdep.h"
14 #include "hw/ssi/ssi.h"
15 #include "qemu/module.h"
16
17 typedef struct {
18     SSISlave parent_obj;
19
20     qemu_irq interrupt;
21     uint8_t tb1, rb2, rb3;
22     int cycle;
23
24     uint8_t input[8];
25     int inputs, com;
26 } MAX111xState;
27
28 #define TYPE_MAX_111X "max111x"
29
30 #define MAX_111X(obj) \
31     OBJECT_CHECK(MAX111xState, (obj), TYPE_MAX_111X)
32
33 #define TYPE_MAX_1110 "max1110"
34 #define TYPE_MAX_1111 "max1111"
35
36 /* Control-byte bitfields */
37 #define CB_PD0          (1 << 0)
38 #define CB_PD1          (1 << 1)
39 #define CB_SGL          (1 << 2)
40 #define CB_UNI          (1 << 3)
41 #define CB_SEL0         (1 << 4)
42 #define CB_SEL1         (1 << 5)
43 #define CB_SEL2         (1 << 6)
44 #define CB_START        (1 << 7)
45
46 #define CHANNEL_NUM(v, b0, b1, b2)      \
47                         ((((v) >> (2 + (b0))) & 4) |    \
48                          (((v) >> (3 + (b1))) & 2) |    \
49                          (((v) >> (4 + (b2))) & 1))
50
51 static uint32_t max111x_read(MAX111xState *s)
52 {
53     if (!s->tb1)
54         return 0;
55
56     switch (s->cycle ++) {
57     case 1:
58         return s->rb2;
59     case 2:
60         return s->rb3;
61     }
62
63     return 0;
64 }
65
66 /* Interpret a control-byte */
67 static void max111x_write(MAX111xState *s, uint32_t value)
68 {
69     int measure, chan;
70
71     /* Ignore the value if START bit is zero */
72     if (!(value & CB_START))
73         return;
74
75     s->cycle = 0;
76
77     if (!(value & CB_PD1)) {
78         s->tb1 = 0;
79         return;
80     }
81
82     s->tb1 = value;
83
84     if (s->inputs == 8)
85         chan = CHANNEL_NUM(value, 1, 0, 2);
86     else
87         chan = CHANNEL_NUM(value & ~CB_SEL0, 0, 1, 2);
88
89     if (value & CB_SGL)
90         measure = s->input[chan] - s->com;
91     else
92         measure = s->input[chan] - s->input[chan ^ 1];
93
94     if (!(value & CB_UNI))
95         measure ^= 0x80;
96
97     s->rb2 = (measure >> 2) & 0x3f;
98     s->rb3 = (measure << 6) & 0xc0;
99
100     /* FIXME: When should the IRQ be lowered?  */
101     qemu_irq_raise(s->interrupt);
102 }
103
104 static uint32_t max111x_transfer(SSISlave *dev, uint32_t value)
105 {
106     MAX111xState *s = MAX_111X(dev);
107     max111x_write(s, value);
108     return max111x_read(s);
109 }
110
111 static const VMStateDescription vmstate_max111x = {
112     .name = "max111x",
113     .version_id = 1,
114     .minimum_version_id = 1,
115     .fields = (VMStateField[]) {
116         VMSTATE_SSI_SLAVE(parent_obj, MAX111xState),
117         VMSTATE_UINT8(tb1, MAX111xState),
118         VMSTATE_UINT8(rb2, MAX111xState),
119         VMSTATE_UINT8(rb3, MAX111xState),
120         VMSTATE_INT32_EQUAL(inputs, MAX111xState, NULL),
121         VMSTATE_INT32(com, MAX111xState),
122         VMSTATE_ARRAY_INT32_UNSAFE(input, MAX111xState, inputs,
123                                    vmstate_info_uint8, uint8_t),
124         VMSTATE_END_OF_LIST()
125     }
126 };
127
128 static int max111x_init(SSISlave *d, int inputs)
129 {
130     DeviceState *dev = DEVICE(d);
131     MAX111xState *s = MAX_111X(dev);
132
133     qdev_init_gpio_out(dev, &s->interrupt, 1);
134
135     s->inputs = inputs;
136     /* TODO: add a user interface for setting these */
137     s->input[0] = 0xf0;
138     s->input[1] = 0xe0;
139     s->input[2] = 0xd0;
140     s->input[3] = 0xc0;
141     s->input[4] = 0xb0;
142     s->input[5] = 0xa0;
143     s->input[6] = 0x90;
144     s->input[7] = 0x80;
145     s->com = 0;
146
147     vmstate_register(dev, -1, &vmstate_max111x, s);
148     return 0;
149 }
150
151 static void max1110_realize(SSISlave *dev, Error **errp)
152 {
153     max111x_init(dev, 8);
154 }
155
156 static void max1111_realize(SSISlave *dev, Error **errp)
157 {
158     max111x_init(dev, 4);
159 }
160
161 void max111x_set_input(DeviceState *dev, int line, uint8_t value)
162 {
163     MAX111xState *s = MAX_111X(dev);
164     assert(line >= 0 && line < s->inputs);
165     s->input[line] = value;
166 }
167
168 static void max111x_class_init(ObjectClass *klass, void *data)
169 {
170     SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
171
172     k->transfer = max111x_transfer;
173 }
174
175 static const TypeInfo max111x_info = {
176     .name          = TYPE_MAX_111X,
177     .parent        = TYPE_SSI_SLAVE,
178     .instance_size = sizeof(MAX111xState),
179     .class_init    = max111x_class_init,
180     .abstract      = true,
181 };
182
183 static void max1110_class_init(ObjectClass *klass, void *data)
184 {
185     SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
186
187     k->realize = max1110_realize;
188 }
189
190 static const TypeInfo max1110_info = {
191     .name          = TYPE_MAX_1110,
192     .parent        = TYPE_MAX_111X,
193     .class_init    = max1110_class_init,
194 };
195
196 static void max1111_class_init(ObjectClass *klass, void *data)
197 {
198     SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
199
200     k->realize = max1111_realize;
201 }
202
203 static const TypeInfo max1111_info = {
204     .name          = TYPE_MAX_1111,
205     .parent        = TYPE_MAX_111X,
206     .class_init    = max1111_class_init,
207 };
208
209 static void max111x_register_types(void)
210 {
211     type_register_static(&max111x_info);
212     type_register_static(&max1110_info);
213     type_register_static(&max1111_info);
214 }
215
216 type_init(max111x_register_types)
This page took 0.035996 seconds and 4 git commands to generate.