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