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