core.c 7.7 KB

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