Add qemu 2.4.0
[kvmfornfv.git] / qemu / hw / gpio / max7310.c
1 /*
2  * MAX7310 8-port GPIO expansion chip.
3  *
4  * Copyright (c) 2006 Openedhand Ltd.
5  * Written by Andrzej Zaborowski <balrog@zabor.org>
6  *
7  * This file is licensed under GNU GPL.
8  */
9
10 #include "hw/i2c/i2c.h"
11
12 #define TYPE_MAX7310 "max7310"
13 #define MAX7310(obj) OBJECT_CHECK(MAX7310State, (obj), TYPE_MAX7310)
14
15 typedef struct MAX7310State {
16     I2CSlave parent_obj;
17
18     int i2c_command_byte;
19     int len;
20
21     uint8_t level;
22     uint8_t direction;
23     uint8_t polarity;
24     uint8_t status;
25     uint8_t command;
26     qemu_irq handler[8];
27     qemu_irq *gpio_in;
28 } MAX7310State;
29
30 static void max7310_reset(DeviceState *dev)
31 {
32     MAX7310State *s = MAX7310(dev);
33
34     s->level &= s->direction;
35     s->direction = 0xff;
36     s->polarity = 0xf0;
37     s->status = 0x01;
38     s->command = 0x00;
39 }
40
41 static int max7310_rx(I2CSlave *i2c)
42 {
43     MAX7310State *s = MAX7310(i2c);
44
45     switch (s->command) {
46     case 0x00:  /* Input port */
47         return s->level ^ s->polarity;
48         break;
49
50     case 0x01:  /* Output port */
51         return s->level & ~s->direction;
52         break;
53
54     case 0x02:  /* Polarity inversion */
55         return s->polarity;
56
57     case 0x03:  /* Configuration */
58         return s->direction;
59
60     case 0x04:  /* Timeout */
61         return s->status;
62         break;
63
64     case 0xff:  /* Reserved */
65         return 0xff;
66
67     default:
68 #ifdef VERBOSE
69         printf("%s: unknown register %02x\n", __FUNCTION__, s->command);
70 #endif
71         break;
72     }
73     return 0xff;
74 }
75
76 static int max7310_tx(I2CSlave *i2c, uint8_t data)
77 {
78     MAX7310State *s = MAX7310(i2c);
79     uint8_t diff;
80     int line;
81
82     if (s->len ++ > 1) {
83 #ifdef VERBOSE
84         printf("%s: message too long (%i bytes)\n", __FUNCTION__, s->len);
85 #endif
86         return 1;
87     }
88
89     if (s->i2c_command_byte) {
90         s->command = data;
91         s->i2c_command_byte = 0;
92         return 0;
93     }
94
95     switch (s->command) {
96     case 0x01:  /* Output port */
97         for (diff = (data ^ s->level) & ~s->direction; diff;
98                         diff &= ~(1 << line)) {
99             line = ctz32(diff);
100             if (s->handler[line])
101                 qemu_set_irq(s->handler[line], (data >> line) & 1);
102         }
103         s->level = (s->level & s->direction) | (data & ~s->direction);
104         break;
105
106     case 0x02:  /* Polarity inversion */
107         s->polarity = data;
108         break;
109
110     case 0x03:  /* Configuration */
111         s->level &= ~(s->direction ^ data);
112         s->direction = data;
113         break;
114
115     case 0x04:  /* Timeout */
116         s->status = data;
117         break;
118
119     case 0x00:  /* Input port - ignore writes */
120         break;
121     default:
122 #ifdef VERBOSE
123         printf("%s: unknown register %02x\n", __FUNCTION__, s->command);
124 #endif
125         return 1;
126     }
127
128     return 0;
129 }
130
131 static void max7310_event(I2CSlave *i2c, enum i2c_event event)
132 {
133     MAX7310State *s = MAX7310(i2c);
134     s->len = 0;
135
136     switch (event) {
137     case I2C_START_SEND:
138         s->i2c_command_byte = 1;
139         break;
140     case I2C_FINISH:
141 #ifdef VERBOSE
142         if (s->len == 1)
143             printf("%s: message too short (%i bytes)\n", __FUNCTION__, s->len);
144 #endif
145         break;
146     default:
147         break;
148     }
149 }
150
151 static const VMStateDescription vmstate_max7310 = {
152     .name = "max7310",
153     .version_id = 0,
154     .minimum_version_id = 0,
155     .fields = (VMStateField[]) {
156         VMSTATE_INT32(i2c_command_byte, MAX7310State),
157         VMSTATE_INT32(len, MAX7310State),
158         VMSTATE_UINT8(level, MAX7310State),
159         VMSTATE_UINT8(direction, MAX7310State),
160         VMSTATE_UINT8(polarity, MAX7310State),
161         VMSTATE_UINT8(status, MAX7310State),
162         VMSTATE_UINT8(command, MAX7310State),
163         VMSTATE_I2C_SLAVE(parent_obj, MAX7310State),
164         VMSTATE_END_OF_LIST()
165     }
166 };
167
168 static void max7310_gpio_set(void *opaque, int line, int level)
169 {
170     MAX7310State *s = (MAX7310State *) opaque;
171     if (line >= ARRAY_SIZE(s->handler) || line  < 0)
172         hw_error("bad GPIO line");
173
174     if (level)
175         s->level |= s->direction & (1 << line);
176     else
177         s->level &= ~(s->direction & (1 << line));
178 }
179
180 /* MAX7310 is SMBus-compatible (can be used with only SMBus protocols),
181  * but also accepts sequences that are not SMBus so return an I2C device.  */
182 static int max7310_init(I2CSlave *i2c)
183 {
184     MAX7310State *s = MAX7310(i2c);
185
186     qdev_init_gpio_in(&i2c->qdev, max7310_gpio_set, 8);
187     qdev_init_gpio_out(&i2c->qdev, s->handler, 8);
188
189     return 0;
190 }
191
192 static void max7310_class_init(ObjectClass *klass, void *data)
193 {
194     DeviceClass *dc = DEVICE_CLASS(klass);
195     I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
196
197     k->init = max7310_init;
198     k->event = max7310_event;
199     k->recv = max7310_rx;
200     k->send = max7310_tx;
201     dc->reset = max7310_reset;
202     dc->vmsd = &vmstate_max7310;
203 }
204
205 static const TypeInfo max7310_info = {
206     .name          = TYPE_MAX7310,
207     .parent        = TYPE_I2C_SLAVE,
208     .instance_size = sizeof(MAX7310State),
209     .class_init    = max7310_class_init,
210 };
211
212 static void max7310_register_types(void)
213 {
214     type_register_static(&max7310_info);
215 }
216
217 type_init(max7310_register_types)