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