]>
Commit | Line | Data |
---|---|---|
c824cacd AZ |
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. | |
6b620ca3 PB |
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. | |
c824cacd AZ |
11 | */ |
12 | ||
0d1c9782 | 13 | #include "qemu/osdep.h" |
64552b6b | 14 | #include "hw/irq.h" |
8fd06719 | 15 | #include "hw/ssi/ssi.h" |
d6454270 | 16 | #include "migration/vmstate.h" |
0b8fa32f | 17 | #include "qemu/module.h" |
c824cacd | 18 | |
a984a69e | 19 | typedef struct { |
7c77b654 PC |
20 | SSISlave parent_obj; |
21 | ||
c824cacd AZ |
22 | qemu_irq interrupt; |
23 | uint8_t tb1, rb2, rb3; | |
24 | int cycle; | |
25 | ||
54d970d1 | 26 | uint8_t input[8]; |
c824cacd | 27 | int inputs, com; |
a984a69e | 28 | } MAX111xState; |
c824cacd | 29 | |
5ef4a1c3 PC |
30 | #define TYPE_MAX_111X "max111x" |
31 | ||
7c77b654 PC |
32 | #define MAX_111X(obj) \ |
33 | OBJECT_CHECK(MAX111xState, (obj), TYPE_MAX_111X) | |
34 | ||
5ef4a1c3 PC |
35 | #define TYPE_MAX_1110 "max1110" |
36 | #define TYPE_MAX_1111 "max1111" | |
37 | ||
c824cacd AZ |
38 | /* Control-byte bitfields */ |
39 | #define CB_PD0 (1 << 0) | |
40 | #define CB_PD1 (1 << 1) | |
41 | #define CB_SGL (1 << 2) | |
42 | #define CB_UNI (1 << 3) | |
43 | #define CB_SEL0 (1 << 4) | |
44 | #define CB_SEL1 (1 << 5) | |
45 | #define CB_SEL2 (1 << 6) | |
46 | #define CB_START (1 << 7) | |
47 | ||
48 | #define CHANNEL_NUM(v, b0, b1, b2) \ | |
7d37435b PB |
49 | ((((v) >> (2 + (b0))) & 4) | \ |
50 | (((v) >> (3 + (b1))) & 2) | \ | |
51 | (((v) >> (4 + (b2))) & 1)) | |
c824cacd | 52 | |
a984a69e | 53 | static uint32_t max111x_read(MAX111xState *s) |
c824cacd | 54 | { |
c824cacd AZ |
55 | if (!s->tb1) |
56 | return 0; | |
57 | ||
58 | switch (s->cycle ++) { | |
59 | case 1: | |
60 | return s->rb2; | |
61 | case 2: | |
62 | return s->rb3; | |
63 | } | |
64 | ||
65 | return 0; | |
66 | } | |
67 | ||
68 | /* Interpret a control-byte */ | |
a984a69e | 69 | static void max111x_write(MAX111xState *s, uint32_t value) |
c824cacd | 70 | { |
c824cacd AZ |
71 | int measure, chan; |
72 | ||
73 | /* Ignore the value if START bit is zero */ | |
74 | if (!(value & CB_START)) | |
75 | return; | |
76 | ||
77 | s->cycle = 0; | |
78 | ||
79 | if (!(value & CB_PD1)) { | |
80 | s->tb1 = 0; | |
81 | return; | |
82 | } | |
83 | ||
84 | s->tb1 = value; | |
85 | ||
86 | if (s->inputs == 8) | |
87 | chan = CHANNEL_NUM(value, 1, 0, 2); | |
88 | else | |
89 | chan = CHANNEL_NUM(value & ~CB_SEL0, 0, 1, 2); | |
90 | ||
91 | if (value & CB_SGL) | |
92 | measure = s->input[chan] - s->com; | |
93 | else | |
94 | measure = s->input[chan] - s->input[chan ^ 1]; | |
95 | ||
96 | if (!(value & CB_UNI)) | |
97 | measure ^= 0x80; | |
98 | ||
99 | s->rb2 = (measure >> 2) & 0x3f; | |
100 | s->rb3 = (measure << 6) & 0xc0; | |
101 | ||
a984a69e PB |
102 | /* FIXME: When should the IRQ be lowered? */ |
103 | qemu_irq_raise(s->interrupt); | |
104 | } | |
105 | ||
106 | static uint32_t max111x_transfer(SSISlave *dev, uint32_t value) | |
107 | { | |
7c77b654 | 108 | MAX111xState *s = MAX_111X(dev); |
a984a69e PB |
109 | max111x_write(s, value); |
110 | return max111x_read(s); | |
c824cacd AZ |
111 | } |
112 | ||
38cb3aa9 JQ |
113 | static const VMStateDescription vmstate_max111x = { |
114 | .name = "max111x", | |
66530953 PC |
115 | .version_id = 1, |
116 | .minimum_version_id = 1, | |
8f1e884b | 117 | .fields = (VMStateField[]) { |
7c77b654 | 118 | VMSTATE_SSI_SLAVE(parent_obj, MAX111xState), |
38cb3aa9 JQ |
119 | VMSTATE_UINT8(tb1, MAX111xState), |
120 | VMSTATE_UINT8(rb2, MAX111xState), | |
121 | VMSTATE_UINT8(rb3, MAX111xState), | |
d2164ad3 | 122 | VMSTATE_INT32_EQUAL(inputs, MAX111xState, NULL), |
38cb3aa9 JQ |
123 | VMSTATE_INT32(com, MAX111xState), |
124 | VMSTATE_ARRAY_INT32_UNSAFE(input, MAX111xState, inputs, | |
125 | vmstate_info_uint8, uint8_t), | |
126 | VMSTATE_END_OF_LIST() | |
127 | } | |
128 | }; | |
aa941b94 | 129 | |
1a7d9ee6 | 130 | static int max111x_init(SSISlave *d, int inputs) |
c824cacd | 131 | { |
1a7d9ee6 | 132 | DeviceState *dev = DEVICE(d); |
7c77b654 | 133 | MAX111xState *s = MAX_111X(dev); |
c824cacd | 134 | |
1a7d9ee6 | 135 | qdev_init_gpio_out(dev, &s->interrupt, 1); |
c824cacd | 136 | |
a984a69e | 137 | s->inputs = inputs; |
c824cacd AZ |
138 | /* TODO: add a user interface for setting these */ |
139 | s->input[0] = 0xf0; | |
140 | s->input[1] = 0xe0; | |
141 | s->input[2] = 0xd0; | |
142 | s->input[3] = 0xc0; | |
143 | s->input[4] = 0xb0; | |
144 | s->input[5] = 0xa0; | |
145 | s->input[6] = 0x90; | |
146 | s->input[7] = 0x80; | |
147 | s->com = 0; | |
aa941b94 | 148 | |
1df2c9a2 PX |
149 | vmstate_register(VMSTATE_IF(dev), VMSTATE_INSTANCE_ID_ANY, |
150 | &vmstate_max111x, s); | |
81a322d4 | 151 | return 0; |
c824cacd AZ |
152 | } |
153 | ||
7673bb4c | 154 | static void max1110_realize(SSISlave *dev, Error **errp) |
c824cacd | 155 | { |
7673bb4c | 156 | max111x_init(dev, 8); |
c824cacd AZ |
157 | } |
158 | ||
7673bb4c | 159 | static void max1111_realize(SSISlave *dev, Error **errp) |
c824cacd | 160 | { |
7673bb4c | 161 | max111x_init(dev, 4); |
c824cacd AZ |
162 | } |
163 | ||
a984a69e | 164 | void max111x_set_input(DeviceState *dev, int line, uint8_t value) |
c824cacd | 165 | { |
7c77b654 | 166 | MAX111xState *s = MAX_111X(dev); |
a984a69e | 167 | assert(line >= 0 && line < s->inputs); |
c824cacd AZ |
168 | s->input[line] = value; |
169 | } | |
a984a69e | 170 | |
5ef4a1c3 | 171 | static void max111x_class_init(ObjectClass *klass, void *data) |
cd6c4cf2 AL |
172 | { |
173 | SSISlaveClass *k = SSI_SLAVE_CLASS(klass); | |
174 | ||
cd6c4cf2 AL |
175 | k->transfer = max111x_transfer; |
176 | } | |
177 | ||
5ef4a1c3 PC |
178 | static const TypeInfo max111x_info = { |
179 | .name = TYPE_MAX_111X, | |
39bffca2 AL |
180 | .parent = TYPE_SSI_SLAVE, |
181 | .instance_size = sizeof(MAX111xState), | |
5ef4a1c3 PC |
182 | .class_init = max111x_class_init, |
183 | .abstract = true, | |
184 | }; | |
185 | ||
186 | static void max1110_class_init(ObjectClass *klass, void *data) | |
187 | { | |
188 | SSISlaveClass *k = SSI_SLAVE_CLASS(klass); | |
189 | ||
7673bb4c | 190 | k->realize = max1110_realize; |
5ef4a1c3 PC |
191 | } |
192 | ||
193 | static const TypeInfo max1110_info = { | |
194 | .name = TYPE_MAX_1110, | |
195 | .parent = TYPE_MAX_111X, | |
39bffca2 | 196 | .class_init = max1110_class_init, |
a984a69e PB |
197 | }; |
198 | ||
cd6c4cf2 AL |
199 | static void max1111_class_init(ObjectClass *klass, void *data) |
200 | { | |
201 | SSISlaveClass *k = SSI_SLAVE_CLASS(klass); | |
202 | ||
7673bb4c | 203 | k->realize = max1111_realize; |
cd6c4cf2 AL |
204 | } |
205 | ||
8c43a6f0 | 206 | static const TypeInfo max1111_info = { |
5ef4a1c3 PC |
207 | .name = TYPE_MAX_1111, |
208 | .parent = TYPE_MAX_111X, | |
39bffca2 | 209 | .class_init = max1111_class_init, |
a984a69e PB |
210 | }; |
211 | ||
83f7d43a | 212 | static void max111x_register_types(void) |
a984a69e | 213 | { |
5ef4a1c3 | 214 | type_register_static(&max111x_info); |
39bffca2 AL |
215 | type_register_static(&max1110_info); |
216 | type_register_static(&max1111_info); | |
a984a69e PB |
217 | } |
218 | ||
83f7d43a | 219 | type_init(max111x_register_types) |