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