bitbang_i2c.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Bit-Bang i2c emulation extracted from
  3. * Marvell MV88W8618 / Freecom MusicPal emulation.
  4. *
  5. * Copyright (c) 2008 Jan Kiszka
  6. *
  7. * This code is licensed under the GNU GPL v2.
  8. *
  9. * Contributions after 2012-01-13 are licensed under the terms of the
  10. * GNU GPL, version 2 or (at your option) any later version.
  11. */
  12. #include "qemu/osdep.h"
  13. #include "hw/irq.h"
  14. #include "hw/i2c/bitbang_i2c.h"
  15. #include "hw/sysbus.h"
  16. #include "qemu/module.h"
  17. //#define DEBUG_BITBANG_I2C
  18. #ifdef DEBUG_BITBANG_I2C
  19. #define DPRINTF(fmt, ...) \
  20. do { printf("bitbang_i2c: " fmt , ## __VA_ARGS__); } while (0)
  21. #else
  22. #define DPRINTF(fmt, ...) do {} while(0)
  23. #endif
  24. static void bitbang_i2c_enter_stop(bitbang_i2c_interface *i2c)
  25. {
  26. DPRINTF("STOP\n");
  27. if (i2c->current_addr >= 0)
  28. i2c_end_transfer(i2c->bus);
  29. i2c->current_addr = -1;
  30. i2c->state = STOPPED;
  31. }
  32. /* Set device data pin. */
  33. static int bitbang_i2c_ret(bitbang_i2c_interface *i2c, int level)
  34. {
  35. i2c->device_out = level;
  36. //DPRINTF("%d %d %d\n", i2c->last_clock, i2c->last_data, i2c->device_out);
  37. return level & i2c->last_data;
  38. }
  39. /* Leave device data pin unodified. */
  40. static int bitbang_i2c_nop(bitbang_i2c_interface *i2c)
  41. {
  42. return bitbang_i2c_ret(i2c, i2c->device_out);
  43. }
  44. /* Returns data line level. */
  45. int bitbang_i2c_set(bitbang_i2c_interface *i2c, int line, int level)
  46. {
  47. int data;
  48. if (level != 0 && level != 1) {
  49. abort();
  50. }
  51. if (line == BITBANG_I2C_SDA) {
  52. if (level == i2c->last_data) {
  53. return bitbang_i2c_nop(i2c);
  54. }
  55. i2c->last_data = level;
  56. if (i2c->last_clock == 0) {
  57. return bitbang_i2c_nop(i2c);
  58. }
  59. if (level == 0) {
  60. DPRINTF("START\n");
  61. /* START condition. */
  62. i2c->state = SENDING_BIT7;
  63. i2c->current_addr = -1;
  64. } else {
  65. /* STOP condition. */
  66. bitbang_i2c_enter_stop(i2c);
  67. }
  68. return bitbang_i2c_ret(i2c, 1);
  69. }
  70. data = i2c->last_data;
  71. if (i2c->last_clock == level) {
  72. return bitbang_i2c_nop(i2c);
  73. }
  74. i2c->last_clock = level;
  75. if (level == 0) {
  76. /* State is set/read at the start of the clock pulse.
  77. release the data line at the end. */
  78. return bitbang_i2c_ret(i2c, 1);
  79. }
  80. switch (i2c->state) {
  81. case STOPPED:
  82. case SENT_NACK:
  83. return bitbang_i2c_ret(i2c, 1);
  84. case SENDING_BIT7 ... SENDING_BIT0:
  85. i2c->buffer = (i2c->buffer << 1) | data;
  86. /* will end up in WAITING_FOR_ACK */
  87. i2c->state++;
  88. return bitbang_i2c_ret(i2c, 1);
  89. case WAITING_FOR_ACK:
  90. {
  91. int ret;
  92. if (i2c->current_addr < 0) {
  93. i2c->current_addr = i2c->buffer;
  94. DPRINTF("Address 0x%02x\n", i2c->current_addr);
  95. ret = i2c_start_transfer(i2c->bus, i2c->current_addr >> 1,
  96. i2c->current_addr & 1);
  97. } else {
  98. DPRINTF("Sent 0x%02x\n", i2c->buffer);
  99. ret = i2c_send(i2c->bus, i2c->buffer);
  100. }
  101. if (ret) {
  102. /* NACK (either addressing a nonexistent device, or the
  103. * device we were sending to decided to NACK us).
  104. */
  105. DPRINTF("Got NACK\n");
  106. bitbang_i2c_enter_stop(i2c);
  107. return bitbang_i2c_ret(i2c, 1);
  108. }
  109. if (i2c->current_addr & 1) {
  110. i2c->state = RECEIVING_BIT7;
  111. } else {
  112. i2c->state = SENDING_BIT7;
  113. }
  114. return bitbang_i2c_ret(i2c, 0);
  115. }
  116. case RECEIVING_BIT7:
  117. i2c->buffer = i2c_recv(i2c->bus);
  118. DPRINTF("RX byte 0x%02x\n", i2c->buffer);
  119. /* Fall through... */
  120. case RECEIVING_BIT6 ... RECEIVING_BIT0:
  121. data = i2c->buffer >> 7;
  122. /* will end up in SENDING_ACK */
  123. i2c->state++;
  124. i2c->buffer <<= 1;
  125. return bitbang_i2c_ret(i2c, data);
  126. case SENDING_ACK:
  127. i2c->state = RECEIVING_BIT7;
  128. if (data != 0) {
  129. DPRINTF("NACKED\n");
  130. i2c->state = SENT_NACK;
  131. i2c_nack(i2c->bus);
  132. } else {
  133. DPRINTF("ACKED\n");
  134. }
  135. return bitbang_i2c_ret(i2c, 1);
  136. }
  137. abort();
  138. }
  139. void bitbang_i2c_init(bitbang_i2c_interface *s, I2CBus *bus)
  140. {
  141. s->bus = bus;
  142. s->last_data = 1;
  143. s->last_clock = 1;
  144. s->device_out = 1;
  145. }
  146. /* GPIO interface. */
  147. #define TYPE_GPIO_I2C "gpio_i2c"
  148. #define GPIO_I2C(obj) OBJECT_CHECK(GPIOI2CState, (obj), TYPE_GPIO_I2C)
  149. typedef struct GPIOI2CState {
  150. SysBusDevice parent_obj;
  151. MemoryRegion dummy_iomem;
  152. bitbang_i2c_interface bitbang;
  153. int last_level;
  154. qemu_irq out;
  155. } GPIOI2CState;
  156. static void bitbang_i2c_gpio_set(void *opaque, int irq, int level)
  157. {
  158. GPIOI2CState *s = opaque;
  159. level = bitbang_i2c_set(&s->bitbang, irq, level);
  160. if (level != s->last_level) {
  161. s->last_level = level;
  162. qemu_set_irq(s->out, level);
  163. }
  164. }
  165. static void gpio_i2c_init(Object *obj)
  166. {
  167. DeviceState *dev = DEVICE(obj);
  168. GPIOI2CState *s = GPIO_I2C(obj);
  169. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  170. I2CBus *bus;
  171. memory_region_init(&s->dummy_iomem, obj, "gpio_i2c", 0);
  172. sysbus_init_mmio(sbd, &s->dummy_iomem);
  173. bus = i2c_init_bus(dev, "i2c");
  174. bitbang_i2c_init(&s->bitbang, bus);
  175. qdev_init_gpio_in(dev, bitbang_i2c_gpio_set, 2);
  176. qdev_init_gpio_out(dev, &s->out, 1);
  177. }
  178. static void gpio_i2c_class_init(ObjectClass *klass, void *data)
  179. {
  180. DeviceClass *dc = DEVICE_CLASS(klass);
  181. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  182. dc->desc = "Virtual GPIO to I2C bridge";
  183. }
  184. static const TypeInfo gpio_i2c_info = {
  185. .name = TYPE_GPIO_I2C,
  186. .parent = TYPE_SYS_BUS_DEVICE,
  187. .instance_size = sizeof(GPIOI2CState),
  188. .instance_init = gpio_i2c_init,
  189. .class_init = gpio_i2c_class_init,
  190. };
  191. static void bitbang_i2c_register_types(void)
  192. {
  193. type_register_static(&gpio_i2c_info);
  194. }
  195. type_init(bitbang_i2c_register_types)