2
0

core.c 7.7 KB

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