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