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