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 13 typedef struct I2CNode I2CNode; 14 15 struct I2CNode { 16 I2CSlave *elt; 17 QLIST_ENTRY(I2CNode) next; 18 }; 19 20 #define I2C_BROADCAST 0x00 21 22 struct I2CBus 23 { 24 BusState qbus; 25 QLIST_HEAD(, I2CNode) current_devs; 26 uint8_t saved_address; 27 bool broadcast; 28 }; 29 30 static Property i2c_props[] = { 31 DEFINE_PROP_UINT8("address", struct I2CSlave, address, 0), 32 DEFINE_PROP_END_OF_LIST(), 33 }; 34 35 #define TYPE_I2C_BUS "i2c-bus" 36 #define I2C_BUS(obj) OBJECT_CHECK(I2CBus, (obj), TYPE_I2C_BUS) 37 38 static const TypeInfo i2c_bus_info = { 39 .name = TYPE_I2C_BUS, 40 .parent = TYPE_BUS, 41 .instance_size = sizeof(I2CBus), 42 }; 43 44 static void i2c_bus_pre_save(void *opaque) 45 { 46 I2CBus *bus = opaque; 47 48 bus->saved_address = -1; 49 if (!QLIST_EMPTY(&bus->current_devs)) { 50 if (!bus->broadcast) { 51 bus->saved_address = QLIST_FIRST(&bus->current_devs)->elt->address; 52 } else { 53 bus->saved_address = I2C_BROADCAST; 54 } 55 } 56 } 57 58 static const VMStateDescription vmstate_i2c_bus = { 59 .name = "i2c_bus", 60 .version_id = 1, 61 .minimum_version_id = 1, 62 .pre_save = i2c_bus_pre_save, 63 .fields = (VMStateField[]) { 64 VMSTATE_UINT8(saved_address, I2CBus), 65 VMSTATE_END_OF_LIST() 66 } 67 }; 68 69 /* Create a new I2C bus. */ 70 I2CBus *i2c_init_bus(DeviceState *parent, const char *name) 71 { 72 I2CBus *bus; 73 74 bus = I2C_BUS(qbus_create(TYPE_I2C_BUS, parent, name)); 75 QLIST_INIT(&bus->current_devs); 76 vmstate_register(NULL, -1, &vmstate_i2c_bus, bus); 77 return bus; 78 } 79 80 void i2c_set_slave_address(I2CSlave *dev, uint8_t address) 81 { 82 dev->address = address; 83 } 84 85 /* Return nonzero if bus is busy. */ 86 int i2c_bus_busy(I2CBus *bus) 87 { 88 return !QLIST_EMPTY(&bus->current_devs); 89 } 90 91 /* Returns non-zero if the address is not valid. */ 92 /* TODO: Make this handle multiple masters. */ 93 int i2c_start_transfer(I2CBus *bus, uint8_t address, int recv) 94 { 95 BusChild *kid; 96 I2CSlaveClass *sc; 97 I2CNode *node; 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 QTAILQ_FOREACH(kid, &bus->qbus.children, sibling) { 108 DeviceState *qdev = kid->child; 109 I2CSlave *candidate = I2C_SLAVE(qdev); 110 if ((candidate->address == address) || (bus->broadcast)) { 111 node = g_malloc(sizeof(struct I2CNode)); 112 node->elt = candidate; 113 QLIST_INSERT_HEAD(&bus->current_devs, node, next); 114 if (!bus->broadcast) { 115 break; 116 } 117 } 118 } 119 120 if (QLIST_EMPTY(&bus->current_devs)) { 121 return 1; 122 } 123 124 QLIST_FOREACH(node, &bus->current_devs, next) { 125 sc = I2C_SLAVE_GET_CLASS(node->elt); 126 /* If the bus is already busy, assume this is a repeated 127 start condition. */ 128 if (sc->event) { 129 sc->event(node->elt, recv ? I2C_START_RECV : I2C_START_SEND); 130 } 131 } 132 return 0; 133 } 134 135 void i2c_end_transfer(I2CBus *bus) 136 { 137 I2CSlaveClass *sc; 138 I2CNode *node, *next; 139 140 if (QLIST_EMPTY(&bus->current_devs)) { 141 return; 142 } 143 144 QLIST_FOREACH_SAFE(node, &bus->current_devs, next, next) { 145 sc = I2C_SLAVE_GET_CLASS(node->elt); 146 if (sc->event) { 147 sc->event(node->elt, I2C_FINISH); 148 } 149 QLIST_REMOVE(node, next); 150 g_free(node); 151 } 152 bus->broadcast = false; 153 } 154 155 int i2c_send_recv(I2CBus *bus, uint8_t *data, bool send) 156 { 157 I2CSlaveClass *sc; 158 I2CNode *node; 159 int ret = 0; 160 161 if (send) { 162 QLIST_FOREACH(node, &bus->current_devs, next) { 163 sc = I2C_SLAVE_GET_CLASS(node->elt); 164 if (sc->send) { 165 ret = ret || sc->send(node->elt, *data); 166 } else { 167 ret = -1; 168 } 169 } 170 return ret ? -1 : 0; 171 } else { 172 if ((QLIST_EMPTY(&bus->current_devs)) || (bus->broadcast)) { 173 return -1; 174 } 175 176 sc = I2C_SLAVE_GET_CLASS(QLIST_FIRST(&bus->current_devs)->elt); 177 if (sc->recv) { 178 ret = sc->recv(QLIST_FIRST(&bus->current_devs)->elt); 179 if (ret < 0) { 180 return ret; 181 } else { 182 *data = ret; 183 return 0; 184 } 185 } 186 return -1; 187 } 188 } 189 190 int i2c_send(I2CBus *bus, uint8_t data) 191 { 192 return i2c_send_recv(bus, &data, true); 193 } 194 195 int i2c_recv(I2CBus *bus) 196 { 197 uint8_t data; 198 int ret = i2c_send_recv(bus, &data, false); 199 200 return ret < 0 ? ret : data; 201 } 202 203 void i2c_nack(I2CBus *bus) 204 { 205 I2CSlaveClass *sc; 206 I2CNode *node; 207 208 if (QLIST_EMPTY(&bus->current_devs)) { 209 return; 210 } 211 212 QLIST_FOREACH(node, &bus->current_devs, next) { 213 sc = I2C_SLAVE_GET_CLASS(node->elt); 214 if (sc->event) { 215 sc->event(node->elt, I2C_NACK); 216 } 217 } 218 } 219 220 static int i2c_slave_post_load(void *opaque, int version_id) 221 { 222 I2CSlave *dev = opaque; 223 I2CBus *bus; 224 I2CNode *node; 225 226 bus = I2C_BUS(qdev_get_parent_bus(DEVICE(dev))); 227 if ((bus->saved_address == dev->address) || 228 (bus->saved_address == I2C_BROADCAST)) { 229 node = g_malloc(sizeof(struct I2CNode)); 230 node->elt = dev; 231 QLIST_INSERT_HEAD(&bus->current_devs, node, next); 232 } 233 return 0; 234 } 235 236 const VMStateDescription vmstate_i2c_slave = { 237 .name = "I2CSlave", 238 .version_id = 1, 239 .minimum_version_id = 1, 240 .post_load = i2c_slave_post_load, 241 .fields = (VMStateField[]) { 242 VMSTATE_UINT8(address, I2CSlave), 243 VMSTATE_END_OF_LIST() 244 } 245 }; 246 247 static int i2c_slave_qdev_init(DeviceState *dev) 248 { 249 I2CSlave *s = I2C_SLAVE(dev); 250 I2CSlaveClass *sc = I2C_SLAVE_GET_CLASS(s); 251 252 return sc->init(s); 253 } 254 255 DeviceState *i2c_create_slave(I2CBus *bus, const char *name, uint8_t addr) 256 { 257 DeviceState *dev; 258 259 dev = qdev_create(&bus->qbus, name); 260 qdev_prop_set_uint8(dev, "address", addr); 261 qdev_init_nofail(dev); 262 return dev; 263 } 264 265 static void i2c_slave_class_init(ObjectClass *klass, void *data) 266 { 267 DeviceClass *k = DEVICE_CLASS(klass); 268 k->init = i2c_slave_qdev_init; 269 set_bit(DEVICE_CATEGORY_MISC, k->categories); 270 k->bus_type = TYPE_I2C_BUS; 271 k->props = i2c_props; 272 } 273 274 static const TypeInfo i2c_slave_type_info = { 275 .name = TYPE_I2C_SLAVE, 276 .parent = TYPE_DEVICE, 277 .instance_size = sizeof(I2CSlave), 278 .abstract = true, 279 .class_size = sizeof(I2CSlaveClass), 280 .class_init = i2c_slave_class_init, 281 }; 282 283 static void i2c_slave_register_types(void) 284 { 285 type_register_static(&i2c_bus_info); 286 type_register_static(&i2c_slave_type_info); 287 } 288 289 type_init(i2c_slave_register_types) 290