blob: 706d89f4fd3151951a0c5bcaf756b6dcf6895358 [file] [log] [blame]
balrogc824cac2007-04-30 02:14:00 +00001/*
2 * Maxim MAX1110/1111 ADC chip emulation.
3 *
4 * Copyright (c) 2006 Openedhand Ltd.
5 * Written by Andrzej Zaborowski <balrog@zabor.org>
6 *
7 * This code is licensed under the GNU GPLv2.
Paolo Bonzini6b620ca2012-01-13 17:44:23 +01008 *
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.
balrogc824cac2007-04-30 02:14:00 +000011 */
12
Paul Brooka984a692009-05-14 22:35:09 +010013#include "ssi.h"
balrogc824cac2007-04-30 02:14:00 +000014
Paul Brooka984a692009-05-14 22:35:09 +010015typedef struct {
16 SSISlave ssidev;
balrogc824cac2007-04-30 02:14:00 +000017 qemu_irq interrupt;
18 uint8_t tb1, rb2, rb3;
19 int cycle;
20
Juan Quintela54d970d2010-12-03 01:03:10 +010021 uint8_t input[8];
balrogc824cac2007-04-30 02:14:00 +000022 int inputs, com;
Paul Brooka984a692009-05-14 22:35:09 +010023} MAX111xState;
balrogc824cac2007-04-30 02:14:00 +000024
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
Paul Brooka984a692009-05-14 22:35:09 +010040static uint32_t max111x_read(MAX111xState *s)
balrogc824cac2007-04-30 02:14:00 +000041{
balrogc824cac2007-04-30 02:14:00 +000042 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 */
Paul Brooka984a692009-05-14 22:35:09 +010056static void max111x_write(MAX111xState *s, uint32_t value)
balrogc824cac2007-04-30 02:14:00 +000057{
balrogc824cac2007-04-30 02:14:00 +000058 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
Paul Brooka984a692009-05-14 22:35:09 +010089 /* FIXME: When should the IRQ be lowered? */
90 qemu_irq_raise(s->interrupt);
91}
92
93static 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);
balrogc824cac2007-04-30 02:14:00 +000098}
99
Juan Quintela38cb3aa2010-12-03 01:03:59 +0100100static const VMStateDescription vmstate_max111x = {
101 .name = "max111x",
102 .version_id = 0,
103 .minimum_version_id = 0,
104 .minimum_version_id_old = 0,
105 .fields = (VMStateField[]) {
106 VMSTATE_UINT8(tb1, MAX111xState),
107 VMSTATE_UINT8(rb2, MAX111xState),
108 VMSTATE_UINT8(rb3, MAX111xState),
109 VMSTATE_INT32_EQUAL(inputs, MAX111xState),
110 VMSTATE_INT32(com, MAX111xState),
111 VMSTATE_ARRAY_INT32_UNSAFE(input, MAX111xState, inputs,
112 vmstate_info_uint8, uint8_t),
113 VMSTATE_END_OF_LIST()
114 }
115};
balrogaa941b92007-05-24 18:50:09 +0000116
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200117static int max111x_init(SSISlave *dev, int inputs)
balrogc824cac2007-04-30 02:14:00 +0000118{
Paul Brooka984a692009-05-14 22:35:09 +0100119 MAX111xState *s = FROM_SSI_SLAVE(MAX111xState, dev);
balrogc824cac2007-04-30 02:14:00 +0000120
Paul Brooka984a692009-05-14 22:35:09 +0100121 qdev_init_gpio_out(&dev->qdev, &s->interrupt, 1);
balrogc824cac2007-04-30 02:14:00 +0000122
Paul Brooka984a692009-05-14 22:35:09 +0100123 s->inputs = inputs;
balrogc824cac2007-04-30 02:14:00 +0000124 /* TODO: add a user interface for setting these */
125 s->input[0] = 0xf0;
126 s->input[1] = 0xe0;
127 s->input[2] = 0xd0;
128 s->input[3] = 0xc0;
129 s->input[4] = 0xb0;
130 s->input[5] = 0xa0;
131 s->input[6] = 0x90;
132 s->input[7] = 0x80;
133 s->com = 0;
balrogaa941b92007-05-24 18:50:09 +0000134
Juan Quintela38cb3aa2010-12-03 01:03:59 +0100135 vmstate_register(&dev->qdev, -1, &vmstate_max111x, s);
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200136 return 0;
balrogc824cac2007-04-30 02:14:00 +0000137}
138
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200139static int max1110_init(SSISlave *dev)
balrogc824cac2007-04-30 02:14:00 +0000140{
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200141 return max111x_init(dev, 8);
balrogc824cac2007-04-30 02:14:00 +0000142}
143
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200144static int max1111_init(SSISlave *dev)
balrogc824cac2007-04-30 02:14:00 +0000145{
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200146 return max111x_init(dev, 4);
balrogc824cac2007-04-30 02:14:00 +0000147}
148
Paul Brooka984a692009-05-14 22:35:09 +0100149void max111x_set_input(DeviceState *dev, int line, uint8_t value)
balrogc824cac2007-04-30 02:14:00 +0000150{
Paul Brooka984a692009-05-14 22:35:09 +0100151 MAX111xState *s = FROM_SSI_SLAVE(MAX111xState, SSI_SLAVE_FROM_QDEV(dev));
152 assert(line >= 0 && line < s->inputs);
balrogc824cac2007-04-30 02:14:00 +0000153 s->input[line] = value;
154}
Paul Brooka984a692009-05-14 22:35:09 +0100155
Anthony Liguoricd6c4cf2011-12-16 13:36:39 -0600156static void max1110_class_init(ObjectClass *klass, void *data)
157{
158 SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
159
160 k->init = max1110_init;
161 k->transfer = max111x_transfer;
162}
163
Anthony Liguori39bffca2011-12-07 21:34:16 -0600164static TypeInfo max1110_info = {
165 .name = "max1110",
166 .parent = TYPE_SSI_SLAVE,
167 .instance_size = sizeof(MAX111xState),
168 .class_init = max1110_class_init,
Paul Brooka984a692009-05-14 22:35:09 +0100169};
170
Anthony Liguoricd6c4cf2011-12-16 13:36:39 -0600171static void max1111_class_init(ObjectClass *klass, void *data)
172{
173 SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
174
175 k->init = max1111_init;
176 k->transfer = max111x_transfer;
177}
178
Anthony Liguori39bffca2011-12-07 21:34:16 -0600179static TypeInfo max1111_info = {
180 .name = "max1111",
181 .parent = TYPE_SSI_SLAVE,
182 .instance_size = sizeof(MAX111xState),
183 .class_init = max1111_class_init,
Paul Brooka984a692009-05-14 22:35:09 +0100184};
185
Andreas Färber83f7d432012-02-09 15:20:55 +0100186static void max111x_register_types(void)
Paul Brooka984a692009-05-14 22:35:09 +0100187{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600188 type_register_static(&max1110_info);
189 type_register_static(&max1111_info);
Paul Brooka984a692009-05-14 22:35:09 +0100190}
191
Andreas Färber83f7d432012-02-09 15:20:55 +0100192type_init(max111x_register_types)