xref: /qemu/hw/i2c/core.c (revision d6454270575da1f16a8923c7cb240e46ef243f72)
10ff596d0Spbrook /*
20ff596d0Spbrook  * QEMU I2C bus interface.
30ff596d0Spbrook  *
40ff596d0Spbrook  * Copyright (c) 2007 CodeSourcery.
50ff596d0Spbrook  * Written by Paul Brook
60ff596d0Spbrook  *
78e31bf38SMatthew Fernandez  * This code is licensed under the LGPL.
80ff596d0Spbrook  */
90ff596d0Spbrook 
100430891cSPeter Maydell #include "qemu/osdep.h"
110d09e41aSPaolo Bonzini #include "hw/i2c/i2c.h"
12*d6454270SMarkus Armbruster #include "migration/vmstate.h"
130b8fa32fSMarkus Armbruster #include "qemu/module.h"
1408bb9b34SPhilippe Mathieu-Daudé #include "trace.h"
150ff596d0Spbrook 
1671ae65e5SIgor Mammedov #define I2C_BROADCAST 0x00
1771ae65e5SIgor Mammedov 
183cb75a7cSPaolo Bonzini static Property i2c_props[] = {
193cb75a7cSPaolo Bonzini     DEFINE_PROP_UINT8("address", struct I2CSlave, address, 0),
203cb75a7cSPaolo Bonzini     DEFINE_PROP_END_OF_LIST(),
213cb75a7cSPaolo Bonzini };
223cb75a7cSPaolo Bonzini 
230d936928SAnthony Liguori static const TypeInfo i2c_bus_info = {
240d936928SAnthony Liguori     .name = TYPE_I2C_BUS,
250d936928SAnthony Liguori     .parent = TYPE_BUS,
26a5c82852SAndreas Färber     .instance_size = sizeof(I2CBus),
2710c4c98aSGerd Hoffmann };
2810c4c98aSGerd Hoffmann 
2944b1ff31SDr. David Alan Gilbert static int i2c_bus_pre_save(void *opaque)
30c701b35bSpbrook {
31a5c82852SAndreas Färber     I2CBus *bus = opaque;
32c701b35bSpbrook 
332293c27fSKONRAD Frederic     bus->saved_address = -1;
342293c27fSKONRAD Frederic     if (!QLIST_EMPTY(&bus->current_devs)) {
352293c27fSKONRAD Frederic         if (!bus->broadcast) {
362293c27fSKONRAD Frederic             bus->saved_address = QLIST_FIRST(&bus->current_devs)->elt->address;
3771ae65e5SIgor Mammedov         } else {
3871ae65e5SIgor Mammedov             bus->saved_address = I2C_BROADCAST;
39c701b35bSpbrook         }
402293c27fSKONRAD Frederic     }
4144b1ff31SDr. David Alan Gilbert 
4244b1ff31SDr. David Alan Gilbert     return 0;
43c701b35bSpbrook }
44c701b35bSpbrook 
458d0eb050SJuan Quintela static const VMStateDescription vmstate_i2c_bus = {
468d0eb050SJuan Quintela     .name = "i2c_bus",
478d0eb050SJuan Quintela     .version_id = 1,
488d0eb050SJuan Quintela     .minimum_version_id = 1,
498d0eb050SJuan Quintela     .pre_save = i2c_bus_pre_save,
508d0eb050SJuan Quintela     .fields = (VMStateField[]) {
51a5c82852SAndreas Färber         VMSTATE_UINT8(saved_address, I2CBus),
528d0eb050SJuan Quintela         VMSTATE_END_OF_LIST()
538d0eb050SJuan Quintela     }
548d0eb050SJuan Quintela };
558d0eb050SJuan Quintela 
560ff596d0Spbrook /* Create a new I2C bus.  */
57a5c82852SAndreas Färber I2CBus *i2c_init_bus(DeviceState *parent, const char *name)
580ff596d0Spbrook {
59a5c82852SAndreas Färber     I2CBus *bus;
600ff596d0Spbrook 
61fef7fbc9SAndreas Färber     bus = I2C_BUS(qbus_create(TYPE_I2C_BUS, parent, name));
622293c27fSKONRAD Frederic     QLIST_INIT(&bus->current_devs);
630be71e32SAlex Williamson     vmstate_register(NULL, -1, &vmstate_i2c_bus, bus);
640ff596d0Spbrook     return bus;
650ff596d0Spbrook }
660ff596d0Spbrook 
679e07bdf8SAnthony Liguori void i2c_set_slave_address(I2CSlave *dev, uint8_t address)
680ff596d0Spbrook {
690ff596d0Spbrook     dev->address = address;
700ff596d0Spbrook }
710ff596d0Spbrook 
720ff596d0Spbrook /* Return nonzero if bus is busy.  */
73a5c82852SAndreas Färber int i2c_bus_busy(I2CBus *bus)
740ff596d0Spbrook {
752293c27fSKONRAD Frederic     return !QLIST_EMPTY(&bus->current_devs);
760ff596d0Spbrook }
770ff596d0Spbrook 
780ff596d0Spbrook /* TODO: Make this handle multiple masters.  */
79d307c28cSCorey Minyard /*
80d307c28cSCorey Minyard  * Start or continue an i2c transaction.  When this is called for the
81d307c28cSCorey Minyard  * first time or after an i2c_end_transfer(), if it returns an error
82d307c28cSCorey Minyard  * the bus transaction is terminated (or really never started).  If
83d307c28cSCorey Minyard  * this is called after another i2c_start_transfer() without an
84d307c28cSCorey Minyard  * intervening i2c_end_transfer(), and it returns an error, the
85d307c28cSCorey Minyard  * transaction will not be terminated.  The caller must do it.
86d307c28cSCorey Minyard  *
87d307c28cSCorey Minyard  * This corresponds with the way real hardware works.  The SMBus
88d307c28cSCorey Minyard  * protocol uses a start transfer to switch from write to read mode
89d307c28cSCorey Minyard  * without releasing the bus.  If that fails, the bus is still
90d307c28cSCorey Minyard  * in a transaction.
91d307c28cSCorey Minyard  */
92a5c82852SAndreas Färber int i2c_start_transfer(I2CBus *bus, uint8_t address, int recv)
930ff596d0Spbrook {
940866aca1SAnthony Liguori     BusChild *kid;
95b5ea9327SAnthony Liguori     I2CSlaveClass *sc;
962293c27fSKONRAD Frederic     I2CNode *node;
97d307c28cSCorey Minyard     bool bus_scanned = false;
982293c27fSKONRAD Frederic 
9971ae65e5SIgor Mammedov     if (address == I2C_BROADCAST) {
1002293c27fSKONRAD Frederic         /*
1012293c27fSKONRAD Frederic          * This is a broadcast, the current_devs will be all the devices of the
1022293c27fSKONRAD Frederic          * bus.
1032293c27fSKONRAD Frederic          */
1042293c27fSKONRAD Frederic         bus->broadcast = true;
1052293c27fSKONRAD Frederic     }
1060ff596d0Spbrook 
1070fa758c3SCorey Minyard     /*
1080fa758c3SCorey Minyard      * If there are already devices in the list, that means we are in
1090fa758c3SCorey Minyard      * the middle of a transaction and we shouldn't rescan the bus.
1100fa758c3SCorey Minyard      *
1110fa758c3SCorey Minyard      * This happens with any SMBus transaction, even on a pure I2C
1120fa758c3SCorey Minyard      * device.  The interface does a transaction start without
1130fa758c3SCorey Minyard      * terminating the previous transaction.
1140fa758c3SCorey Minyard      */
1150fa758c3SCorey Minyard     if (QLIST_EMPTY(&bus->current_devs)) {
1160866aca1SAnthony Liguori         QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) {
1170866aca1SAnthony Liguori             DeviceState *qdev = kid->child;
1188aae84a1SAndreas Färber             I2CSlave *candidate = I2C_SLAVE(qdev);
1192293c27fSKONRAD Frederic             if ((candidate->address == address) || (bus->broadcast)) {
1202293c27fSKONRAD Frederic                 node = g_malloc(sizeof(struct I2CNode));
1212293c27fSKONRAD Frederic                 node->elt = candidate;
1222293c27fSKONRAD Frederic                 QLIST_INSERT_HEAD(&bus->current_devs, node, next);
1232293c27fSKONRAD Frederic                 if (!bus->broadcast) {
1240ff596d0Spbrook                     break;
1250ff596d0Spbrook                 }
126b3a21988SJuha Riihimäki             }
1272293c27fSKONRAD Frederic         }
128d307c28cSCorey Minyard         bus_scanned = true;
1290fa758c3SCorey Minyard     }
1300ff596d0Spbrook 
1312293c27fSKONRAD Frederic     if (QLIST_EMPTY(&bus->current_devs)) {
1320ff596d0Spbrook         return 1;
133b5ea9327SAnthony Liguori     }
1340ff596d0Spbrook 
1352293c27fSKONRAD Frederic     QLIST_FOREACH(node, &bus->current_devs, next) {
13608bb9b34SPhilippe Mathieu-Daudé         I2CSlave *s = node->elt;
137d307c28cSCorey Minyard         int rv;
138d307c28cSCorey Minyard 
13908bb9b34SPhilippe Mathieu-Daudé         sc = I2C_SLAVE_GET_CLASS(s);
1400ff596d0Spbrook         /* If the bus is already busy, assume this is a repeated
1410ff596d0Spbrook            start condition.  */
142d307c28cSCorey Minyard 
143b5ea9327SAnthony Liguori         if (sc->event) {
14408bb9b34SPhilippe Mathieu-Daudé             trace_i2c_event("start", s->address);
14508bb9b34SPhilippe Mathieu-Daudé             rv = sc->event(s, recv ? I2C_START_RECV : I2C_START_SEND);
146d307c28cSCorey Minyard             if (rv && !bus->broadcast) {
147d307c28cSCorey Minyard                 if (bus_scanned) {
148d307c28cSCorey Minyard                     /* First call, terminate the transfer. */
149d307c28cSCorey Minyard                     i2c_end_transfer(bus);
150d307c28cSCorey Minyard                 }
151d307c28cSCorey Minyard                 return rv;
152d307c28cSCorey Minyard             }
1532293c27fSKONRAD Frederic         }
154b5ea9327SAnthony Liguori     }
1550ff596d0Spbrook     return 0;
1560ff596d0Spbrook }
1570ff596d0Spbrook 
158a5c82852SAndreas Färber void i2c_end_transfer(I2CBus *bus)
1590ff596d0Spbrook {
160b5ea9327SAnthony Liguori     I2CSlaveClass *sc;
1612293c27fSKONRAD Frederic     I2CNode *node, *next;
1620ff596d0Spbrook 
1632293c27fSKONRAD Frederic     QLIST_FOREACH_SAFE(node, &bus->current_devs, next, next) {
16408bb9b34SPhilippe Mathieu-Daudé         I2CSlave *s = node->elt;
16508bb9b34SPhilippe Mathieu-Daudé         sc = I2C_SLAVE_GET_CLASS(s);
166b5ea9327SAnthony Liguori         if (sc->event) {
16708bb9b34SPhilippe Mathieu-Daudé             trace_i2c_event("finish", s->address);
16808bb9b34SPhilippe Mathieu-Daudé             sc->event(s, I2C_FINISH);
169b5ea9327SAnthony Liguori         }
1702293c27fSKONRAD Frederic         QLIST_REMOVE(node, next);
1712293c27fSKONRAD Frederic         g_free(node);
1722293c27fSKONRAD Frederic     }
1732293c27fSKONRAD Frederic     bus->broadcast = false;
1740ff596d0Spbrook }
1750ff596d0Spbrook 
176056fca7bSPeter Crosthwaite int i2c_send_recv(I2CBus *bus, uint8_t *data, bool send)
1770ff596d0Spbrook {
178b5ea9327SAnthony Liguori     I2CSlaveClass *sc;
17908bb9b34SPhilippe Mathieu-Daudé     I2CSlave *s;
1802293c27fSKONRAD Frederic     I2CNode *node;
1812293c27fSKONRAD Frederic     int ret = 0;
1820ff596d0Spbrook 
183056fca7bSPeter Crosthwaite     if (send) {
1842293c27fSKONRAD Frederic         QLIST_FOREACH(node, &bus->current_devs, next) {
18508bb9b34SPhilippe Mathieu-Daudé             s = node->elt;
18608bb9b34SPhilippe Mathieu-Daudé             sc = I2C_SLAVE_GET_CLASS(s);
187b5ea9327SAnthony Liguori             if (sc->send) {
18808bb9b34SPhilippe Mathieu-Daudé                 trace_i2c_send(s->address, *data);
18908bb9b34SPhilippe Mathieu-Daudé                 ret = ret || sc->send(s, *data);
1902293c27fSKONRAD Frederic             } else {
1912293c27fSKONRAD Frederic                 ret = -1;
192b5ea9327SAnthony Liguori             }
1932293c27fSKONRAD Frederic         }
1942293c27fSKONRAD Frederic         return ret ? -1 : 0;
195056fca7bSPeter Crosthwaite     } else {
1962ac4c5f4SCorey Minyard         ret = 0xff;
1972ac4c5f4SCorey Minyard         if (!QLIST_EMPTY(&bus->current_devs) && !bus->broadcast) {
1982293c27fSKONRAD Frederic             sc = I2C_SLAVE_GET_CLASS(QLIST_FIRST(&bus->current_devs)->elt);
199b5ea9327SAnthony Liguori             if (sc->recv) {
20008bb9b34SPhilippe Mathieu-Daudé                 s = QLIST_FIRST(&bus->current_devs)->elt;
20108bb9b34SPhilippe Mathieu-Daudé                 ret = sc->recv(s);
20208bb9b34SPhilippe Mathieu-Daudé                 trace_i2c_recv(s->address, ret);
2032ac4c5f4SCorey Minyard             }
2042ac4c5f4SCorey Minyard         }
205056fca7bSPeter Crosthwaite         *data = ret;
206056fca7bSPeter Crosthwaite         return 0;
207056fca7bSPeter Crosthwaite     }
208b5ea9327SAnthony Liguori }
209056fca7bSPeter Crosthwaite 
210056fca7bSPeter Crosthwaite int i2c_send(I2CBus *bus, uint8_t data)
211056fca7bSPeter Crosthwaite {
212056fca7bSPeter Crosthwaite     return i2c_send_recv(bus, &data, true);
213056fca7bSPeter Crosthwaite }
214056fca7bSPeter Crosthwaite 
2152ac4c5f4SCorey Minyard uint8_t i2c_recv(I2CBus *bus)
216056fca7bSPeter Crosthwaite {
2172ac4c5f4SCorey Minyard     uint8_t data = 0xff;
218056fca7bSPeter Crosthwaite 
2192ac4c5f4SCorey Minyard     i2c_send_recv(bus, &data, false);
2202ac4c5f4SCorey Minyard     return data;
221056fca7bSPeter Crosthwaite }
2220ff596d0Spbrook 
223a5c82852SAndreas Färber void i2c_nack(I2CBus *bus)
2240ff596d0Spbrook {
225b5ea9327SAnthony Liguori     I2CSlaveClass *sc;
2262293c27fSKONRAD Frederic     I2CNode *node;
2270ff596d0Spbrook 
2282293c27fSKONRAD Frederic     if (QLIST_EMPTY(&bus->current_devs)) {
2290ff596d0Spbrook         return;
230b5ea9327SAnthony Liguori     }
2310ff596d0Spbrook 
2322293c27fSKONRAD Frederic     QLIST_FOREACH(node, &bus->current_devs, next) {
2332293c27fSKONRAD Frederic         sc = I2C_SLAVE_GET_CLASS(node->elt);
234b5ea9327SAnthony Liguori         if (sc->event) {
23508bb9b34SPhilippe Mathieu-Daudé             trace_i2c_event("nack", node->elt->address);
2362293c27fSKONRAD Frederic             sc->event(node->elt, I2C_NACK);
2372293c27fSKONRAD Frederic         }
238b5ea9327SAnthony Liguori     }
2390ff596d0Spbrook }
2400ff596d0Spbrook 
241bcbe8068SJuan Quintela static int i2c_slave_post_load(void *opaque, int version_id)
242bcbe8068SJuan Quintela {
2439e07bdf8SAnthony Liguori     I2CSlave *dev = opaque;
244a5c82852SAndreas Färber     I2CBus *bus;
2452293c27fSKONRAD Frederic     I2CNode *node;
2462293c27fSKONRAD Frederic 
247fef7fbc9SAndreas Färber     bus = I2C_BUS(qdev_get_parent_bus(DEVICE(dev)));
24871ae65e5SIgor Mammedov     if ((bus->saved_address == dev->address) ||
24971ae65e5SIgor Mammedov         (bus->saved_address == I2C_BROADCAST)) {
2502293c27fSKONRAD Frederic         node = g_malloc(sizeof(struct I2CNode));
2512293c27fSKONRAD Frederic         node->elt = dev;
2522293c27fSKONRAD Frederic         QLIST_INSERT_HEAD(&bus->current_devs, node, next);
253bcbe8068SJuan Quintela     }
254bcbe8068SJuan Quintela     return 0;
255bcbe8068SJuan Quintela }
256bcbe8068SJuan Quintela 
2571894839fSJuan Quintela const VMStateDescription vmstate_i2c_slave = {
2589e07bdf8SAnthony Liguori     .name = "I2CSlave",
259bcbe8068SJuan Quintela     .version_id = 1,
260bcbe8068SJuan Quintela     .minimum_version_id = 1,
261bcbe8068SJuan Quintela     .post_load = i2c_slave_post_load,
262bcbe8068SJuan Quintela     .fields = (VMStateField[]) {
2639e07bdf8SAnthony Liguori         VMSTATE_UINT8(address, I2CSlave),
264bcbe8068SJuan Quintela         VMSTATE_END_OF_LIST()
265bcbe8068SJuan Quintela     }
266bcbe8068SJuan Quintela };
267bcbe8068SJuan Quintela 
268a5c82852SAndreas Färber DeviceState *i2c_create_slave(I2CBus *bus, const char *name, uint8_t addr)
269fe8de492SPaul Brook {
270fe8de492SPaul Brook     DeviceState *dev;
271fe8de492SPaul Brook 
27202e2da45SPaul Brook     dev = qdev_create(&bus->qbus, name);
2735b7f5327SJuan Quintela     qdev_prop_set_uint8(dev, "address", addr);
274e23a1b33SMarkus Armbruster     qdev_init_nofail(dev);
275fe8de492SPaul Brook     return dev;
276aa941b94Sbalrog }
277b5ea9327SAnthony Liguori 
27839bffca2SAnthony Liguori static void i2c_slave_class_init(ObjectClass *klass, void *data)
27939bffca2SAnthony Liguori {
28039bffca2SAnthony Liguori     DeviceClass *k = DEVICE_CLASS(klass);
281125ee0edSMarcel Apfelbaum     set_bit(DEVICE_CATEGORY_MISC, k->categories);
2820d936928SAnthony Liguori     k->bus_type = TYPE_I2C_BUS;
283bce54474SPaolo Bonzini     k->props = i2c_props;
28439bffca2SAnthony Liguori }
28539bffca2SAnthony Liguori 
2868c43a6f0SAndreas Färber static const TypeInfo i2c_slave_type_info = {
287b5ea9327SAnthony Liguori     .name = TYPE_I2C_SLAVE,
288b5ea9327SAnthony Liguori     .parent = TYPE_DEVICE,
289b5ea9327SAnthony Liguori     .instance_size = sizeof(I2CSlave),
290b5ea9327SAnthony Liguori     .abstract = true,
291b5ea9327SAnthony Liguori     .class_size = sizeof(I2CSlaveClass),
29239bffca2SAnthony Liguori     .class_init = i2c_slave_class_init,
293b5ea9327SAnthony Liguori };
294b5ea9327SAnthony Liguori 
29583f7d43aSAndreas Färber static void i2c_slave_register_types(void)
296b5ea9327SAnthony Liguori {
2970d936928SAnthony Liguori     type_register_static(&i2c_bus_info);
298b5ea9327SAnthony Liguori     type_register_static(&i2c_slave_type_info);
299b5ea9327SAnthony Liguori }
300b5ea9327SAnthony Liguori 
30183f7d43aSAndreas Färber type_init(i2c_slave_register_types)
302