core.c 8.1 KB

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