xref: /openbmc/qemu/hw/i2c/core.c (revision dc5bd18f)
1 /*
2  * QEMU I2C bus interface.
3  *
4  * Copyright (c) 2007 CodeSourcery.
5  * Written by Paul Brook
6  *
7  * This code is licensed under the LGPL.
8  */
9 
10 #include "qemu/osdep.h"
11 #include "hw/i2c/i2c.h"
12 
13 #define I2C_BROADCAST 0x00
14 
15 static Property i2c_props[] = {
16     DEFINE_PROP_UINT8("address", struct I2CSlave, address, 0),
17     DEFINE_PROP_END_OF_LIST(),
18 };
19 
20 static const TypeInfo i2c_bus_info = {
21     .name = TYPE_I2C_BUS,
22     .parent = TYPE_BUS,
23     .instance_size = sizeof(I2CBus),
24 };
25 
26 static int i2c_bus_pre_save(void *opaque)
27 {
28     I2CBus *bus = opaque;
29 
30     bus->saved_address = -1;
31     if (!QLIST_EMPTY(&bus->current_devs)) {
32         if (!bus->broadcast) {
33             bus->saved_address = QLIST_FIRST(&bus->current_devs)->elt->address;
34         } else {
35             bus->saved_address = I2C_BROADCAST;
36         }
37     }
38 
39     return 0;
40 }
41 
42 static const VMStateDescription vmstate_i2c_bus = {
43     .name = "i2c_bus",
44     .version_id = 1,
45     .minimum_version_id = 1,
46     .pre_save = i2c_bus_pre_save,
47     .fields = (VMStateField[]) {
48         VMSTATE_UINT8(saved_address, I2CBus),
49         VMSTATE_END_OF_LIST()
50     }
51 };
52 
53 /* Create a new I2C bus.  */
54 I2CBus *i2c_init_bus(DeviceState *parent, const char *name)
55 {
56     I2CBus *bus;
57 
58     bus = I2C_BUS(qbus_create(TYPE_I2C_BUS, parent, name));
59     QLIST_INIT(&bus->current_devs);
60     vmstate_register(NULL, -1, &vmstate_i2c_bus, bus);
61     return bus;
62 }
63 
64 void i2c_set_slave_address(I2CSlave *dev, uint8_t address)
65 {
66     dev->address = address;
67 }
68 
69 /* Return nonzero if bus is busy.  */
70 int i2c_bus_busy(I2CBus *bus)
71 {
72     return !QLIST_EMPTY(&bus->current_devs);
73 }
74 
75 /* TODO: Make this handle multiple masters.  */
76 /*
77  * Start or continue an i2c transaction.  When this is called for the
78  * first time or after an i2c_end_transfer(), if it returns an error
79  * the bus transaction is terminated (or really never started).  If
80  * this is called after another i2c_start_transfer() without an
81  * intervening i2c_end_transfer(), and it returns an error, the
82  * transaction will not be terminated.  The caller must do it.
83  *
84  * This corresponds with the way real hardware works.  The SMBus
85  * protocol uses a start transfer to switch from write to read mode
86  * without releasing the bus.  If that fails, the bus is still
87  * in a transaction.
88  */
89 int i2c_start_transfer(I2CBus *bus, uint8_t address, int recv)
90 {
91     BusChild *kid;
92     I2CSlaveClass *sc;
93     I2CNode *node;
94     bool bus_scanned = false;
95 
96     if (address == I2C_BROADCAST) {
97         /*
98          * This is a broadcast, the current_devs will be all the devices of the
99          * bus.
100          */
101         bus->broadcast = true;
102     }
103 
104     /*
105      * If there are already devices in the list, that means we are in
106      * the middle of a transaction and we shouldn't rescan the bus.
107      *
108      * This happens with any SMBus transaction, even on a pure I2C
109      * device.  The interface does a transaction start without
110      * terminating the previous transaction.
111      */
112     if (QLIST_EMPTY(&bus->current_devs)) {
113         QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) {
114             DeviceState *qdev = kid->child;
115             I2CSlave *candidate = I2C_SLAVE(qdev);
116             if ((candidate->address == address) || (bus->broadcast)) {
117                 node = g_malloc(sizeof(struct I2CNode));
118                 node->elt = candidate;
119                 QLIST_INSERT_HEAD(&bus->current_devs, node, next);
120                 if (!bus->broadcast) {
121                     break;
122                 }
123             }
124         }
125         bus_scanned = true;
126     }
127 
128     if (QLIST_EMPTY(&bus->current_devs)) {
129         return 1;
130     }
131 
132     QLIST_FOREACH(node, &bus->current_devs, next) {
133         int rv;
134 
135         sc = I2C_SLAVE_GET_CLASS(node->elt);
136         /* If the bus is already busy, assume this is a repeated
137            start condition.  */
138 
139         if (sc->event) {
140             rv = sc->event(node->elt, recv ? I2C_START_RECV : I2C_START_SEND);
141             if (rv && !bus->broadcast) {
142                 if (bus_scanned) {
143                     /* First call, terminate the transfer. */
144                     i2c_end_transfer(bus);
145                 }
146                 return rv;
147             }
148         }
149     }
150     return 0;
151 }
152 
153 void i2c_end_transfer(I2CBus *bus)
154 {
155     I2CSlaveClass *sc;
156     I2CNode *node, *next;
157 
158     QLIST_FOREACH_SAFE(node, &bus->current_devs, next, next) {
159         sc = I2C_SLAVE_GET_CLASS(node->elt);
160         if (sc->event) {
161             sc->event(node->elt, I2C_FINISH);
162         }
163         QLIST_REMOVE(node, next);
164         g_free(node);
165     }
166     bus->broadcast = false;
167 }
168 
169 int i2c_send_recv(I2CBus *bus, uint8_t *data, bool send)
170 {
171     I2CSlaveClass *sc;
172     I2CNode *node;
173     int ret = 0;
174 
175     if (send) {
176         QLIST_FOREACH(node, &bus->current_devs, next) {
177             sc = I2C_SLAVE_GET_CLASS(node->elt);
178             if (sc->send) {
179                 ret = ret || sc->send(node->elt, *data);
180             } else {
181                 ret = -1;
182             }
183         }
184         return ret ? -1 : 0;
185     } else {
186         if ((QLIST_EMPTY(&bus->current_devs)) || (bus->broadcast)) {
187             return -1;
188         }
189 
190         sc = I2C_SLAVE_GET_CLASS(QLIST_FIRST(&bus->current_devs)->elt);
191         if (sc->recv) {
192             ret = sc->recv(QLIST_FIRST(&bus->current_devs)->elt);
193             if (ret < 0) {
194                 return ret;
195             } else {
196                 *data = ret;
197                 return 0;
198             }
199         }
200         return -1;
201     }
202 }
203 
204 int i2c_send(I2CBus *bus, uint8_t data)
205 {
206     return i2c_send_recv(bus, &data, true);
207 }
208 
209 int i2c_recv(I2CBus *bus)
210 {
211     uint8_t data;
212     int ret = i2c_send_recv(bus, &data, false);
213 
214     return ret < 0 ? ret : data;
215 }
216 
217 void i2c_nack(I2CBus *bus)
218 {
219     I2CSlaveClass *sc;
220     I2CNode *node;
221 
222     if (QLIST_EMPTY(&bus->current_devs)) {
223         return;
224     }
225 
226     QLIST_FOREACH(node, &bus->current_devs, next) {
227         sc = I2C_SLAVE_GET_CLASS(node->elt);
228         if (sc->event) {
229             sc->event(node->elt, I2C_NACK);
230         }
231     }
232 }
233 
234 static int i2c_slave_post_load(void *opaque, int version_id)
235 {
236     I2CSlave *dev = opaque;
237     I2CBus *bus;
238     I2CNode *node;
239 
240     bus = I2C_BUS(qdev_get_parent_bus(DEVICE(dev)));
241     if ((bus->saved_address == dev->address) ||
242         (bus->saved_address == I2C_BROADCAST)) {
243         node = g_malloc(sizeof(struct I2CNode));
244         node->elt = dev;
245         QLIST_INSERT_HEAD(&bus->current_devs, node, next);
246     }
247     return 0;
248 }
249 
250 const VMStateDescription vmstate_i2c_slave = {
251     .name = "I2CSlave",
252     .version_id = 1,
253     .minimum_version_id = 1,
254     .post_load = i2c_slave_post_load,
255     .fields = (VMStateField[]) {
256         VMSTATE_UINT8(address, I2CSlave),
257         VMSTATE_END_OF_LIST()
258     }
259 };
260 
261 static int i2c_slave_qdev_init(DeviceState *dev)
262 {
263     I2CSlave *s = I2C_SLAVE(dev);
264     I2CSlaveClass *sc = I2C_SLAVE_GET_CLASS(s);
265 
266     if (sc->init) {
267         return sc->init(s);
268     }
269 
270     return 0;
271 }
272 
273 DeviceState *i2c_create_slave(I2CBus *bus, const char *name, uint8_t addr)
274 {
275     DeviceState *dev;
276 
277     dev = qdev_create(&bus->qbus, name);
278     qdev_prop_set_uint8(dev, "address", addr);
279     qdev_init_nofail(dev);
280     return dev;
281 }
282 
283 static void i2c_slave_class_init(ObjectClass *klass, void *data)
284 {
285     DeviceClass *k = DEVICE_CLASS(klass);
286     k->init = i2c_slave_qdev_init;
287     set_bit(DEVICE_CATEGORY_MISC, k->categories);
288     k->bus_type = TYPE_I2C_BUS;
289     k->props = i2c_props;
290 }
291 
292 static const TypeInfo i2c_slave_type_info = {
293     .name = TYPE_I2C_SLAVE,
294     .parent = TYPE_DEVICE,
295     .instance_size = sizeof(I2CSlave),
296     .abstract = true,
297     .class_size = sizeof(I2CSlaveClass),
298     .class_init = i2c_slave_class_init,
299 };
300 
301 static void i2c_slave_register_types(void)
302 {
303     type_register_static(&i2c_bus_info);
304     type_register_static(&i2c_slave_type_info);
305 }
306 
307 type_init(i2c_slave_register_types)
308