hw/ppc: Drop useless CONFIG_KVM ifdefery
[qemu/ar7.git] / hw / i2c / core.c
blob20f36f1d5505b3dcc428412eee1d25d002d62911
1 /*
2 * QEMU I2C bus interface.
4 * Copyright (c) 2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the LGPL.
8 */
10 #include "qemu/osdep.h"
11 #include "hw/i2c/i2c.h"
12 #include "qemu/module.h"
13 #include "trace.h"
15 #define I2C_BROADCAST 0x00
17 static Property i2c_props[] = {
18 DEFINE_PROP_UINT8("address", struct I2CSlave, address, 0),
19 DEFINE_PROP_END_OF_LIST(),
22 static const TypeInfo i2c_bus_info = {
23 .name = TYPE_I2C_BUS,
24 .parent = TYPE_BUS,
25 .instance_size = sizeof(I2CBus),
28 static int i2c_bus_pre_save(void *opaque)
30 I2CBus *bus = opaque;
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;
41 return 0;
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()
55 /* Create a new I2C bus. */
56 I2CBus *i2c_init_bus(DeviceState *parent, const char *name)
58 I2CBus *bus;
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;
66 void i2c_set_slave_address(I2CSlave *dev, uint8_t address)
68 dev->address = address;
71 /* Return nonzero if bus is busy. */
72 int i2c_bus_busy(I2CBus *bus)
74 return !QLIST_EMPTY(&bus->current_devs);
77 /* TODO: Make this handle multiple masters. */
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.
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.
91 int i2c_start_transfer(I2CBus *bus, uint8_t address, int recv)
93 BusChild *kid;
94 I2CSlaveClass *sc;
95 I2CNode *node;
96 bool bus_scanned = false;
98 if (address == I2C_BROADCAST) {
100 * This is a broadcast, the current_devs will be all the devices of the
101 * bus.
103 bus->broadcast = true;
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.
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.
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;
127 bus_scanned = true;
130 if (QLIST_EMPTY(&bus->current_devs)) {
131 return 1;
134 QLIST_FOREACH(node, &bus->current_devs, next) {
135 I2CSlave *s = node->elt;
136 int rv;
138 sc = I2C_SLAVE_GET_CLASS(s);
139 /* If the bus is already busy, assume this is a repeated
140 start condition. */
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);
150 return rv;
154 return 0;
157 void i2c_end_transfer(I2CBus *bus)
159 I2CSlaveClass *sc;
160 I2CNode *node, *next;
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);
169 QLIST_REMOVE(node, next);
170 g_free(node);
172 bus->broadcast = false;
175 int i2c_send_recv(I2CBus *bus, uint8_t *data, bool send)
177 I2CSlaveClass *sc;
178 I2CSlave *s;
179 I2CNode *node;
180 int ret = 0;
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;
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);
204 *data = ret;
205 return 0;
209 int i2c_send(I2CBus *bus, uint8_t data)
211 return i2c_send_recv(bus, &data, true);
214 uint8_t i2c_recv(I2CBus *bus)
216 uint8_t data = 0xff;
218 i2c_send_recv(bus, &data, false);
219 return data;
222 void i2c_nack(I2CBus *bus)
224 I2CSlaveClass *sc;
225 I2CNode *node;
227 if (QLIST_EMPTY(&bus->current_devs)) {
228 return;
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);
240 static int i2c_slave_post_load(void *opaque, int version_id)
242 I2CSlave *dev = opaque;
243 I2CBus *bus;
244 I2CNode *node;
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);
253 return 0;
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()
267 DeviceState *i2c_create_slave(I2CBus *bus, const char *name, uint8_t addr)
269 DeviceState *dev;
271 dev = qdev_create(&bus->qbus, name);
272 qdev_prop_set_uint8(dev, "address", addr);
273 qdev_init_nofail(dev);
274 return dev;
277 static void i2c_slave_class_init(ObjectClass *klass, void *data)
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;
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,
294 static void i2c_slave_register_types(void)
296 type_register_static(&i2c_bus_info);
297 type_register_static(&i2c_slave_type_info);
300 type_init(i2c_slave_register_types)