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