2
0

max111x.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Maxim MAX1110/1111 ADC chip emulation.
  3. *
  4. * Copyright (c) 2006 Openedhand Ltd.
  5. * Written by Andrzej Zaborowski <balrog@zabor.org>
  6. *
  7. * This code is licensed under the GNU GPLv2.
  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/ssi/ssi.h"
  15. #include "migration/vmstate.h"
  16. #include "qemu/module.h"
  17. typedef struct {
  18. SSISlave parent_obj;
  19. qemu_irq interrupt;
  20. uint8_t tb1, rb2, rb3;
  21. int cycle;
  22. uint8_t input[8];
  23. int inputs, com;
  24. } MAX111xState;
  25. #define TYPE_MAX_111X "max111x"
  26. #define MAX_111X(obj) \
  27. OBJECT_CHECK(MAX111xState, (obj), TYPE_MAX_111X)
  28. #define TYPE_MAX_1110 "max1110"
  29. #define TYPE_MAX_1111 "max1111"
  30. /* Control-byte bitfields */
  31. #define CB_PD0 (1 << 0)
  32. #define CB_PD1 (1 << 1)
  33. #define CB_SGL (1 << 2)
  34. #define CB_UNI (1 << 3)
  35. #define CB_SEL0 (1 << 4)
  36. #define CB_SEL1 (1 << 5)
  37. #define CB_SEL2 (1 << 6)
  38. #define CB_START (1 << 7)
  39. #define CHANNEL_NUM(v, b0, b1, b2) \
  40. ((((v) >> (2 + (b0))) & 4) | \
  41. (((v) >> (3 + (b1))) & 2) | \
  42. (((v) >> (4 + (b2))) & 1))
  43. static uint32_t max111x_read(MAX111xState *s)
  44. {
  45. if (!s->tb1)
  46. return 0;
  47. switch (s->cycle ++) {
  48. case 1:
  49. return s->rb2;
  50. case 2:
  51. return s->rb3;
  52. }
  53. return 0;
  54. }
  55. /* Interpret a control-byte */
  56. static void max111x_write(MAX111xState *s, uint32_t value)
  57. {
  58. int measure, chan;
  59. /* Ignore the value if START bit is zero */
  60. if (!(value & CB_START))
  61. return;
  62. s->cycle = 0;
  63. if (!(value & CB_PD1)) {
  64. s->tb1 = 0;
  65. return;
  66. }
  67. s->tb1 = value;
  68. if (s->inputs == 8)
  69. chan = CHANNEL_NUM(value, 1, 0, 2);
  70. else
  71. chan = CHANNEL_NUM(value & ~CB_SEL0, 0, 1, 2);
  72. if (value & CB_SGL)
  73. measure = s->input[chan] - s->com;
  74. else
  75. measure = s->input[chan] - s->input[chan ^ 1];
  76. if (!(value & CB_UNI))
  77. measure ^= 0x80;
  78. s->rb2 = (measure >> 2) & 0x3f;
  79. s->rb3 = (measure << 6) & 0xc0;
  80. /* FIXME: When should the IRQ be lowered? */
  81. qemu_irq_raise(s->interrupt);
  82. }
  83. static uint32_t max111x_transfer(SSISlave *dev, uint32_t value)
  84. {
  85. MAX111xState *s = MAX_111X(dev);
  86. max111x_write(s, value);
  87. return max111x_read(s);
  88. }
  89. static const VMStateDescription vmstate_max111x = {
  90. .name = "max111x",
  91. .version_id = 1,
  92. .minimum_version_id = 1,
  93. .fields = (VMStateField[]) {
  94. VMSTATE_SSI_SLAVE(parent_obj, MAX111xState),
  95. VMSTATE_UINT8(tb1, MAX111xState),
  96. VMSTATE_UINT8(rb2, MAX111xState),
  97. VMSTATE_UINT8(rb3, MAX111xState),
  98. VMSTATE_INT32_EQUAL(inputs, MAX111xState, NULL),
  99. VMSTATE_INT32(com, MAX111xState),
  100. VMSTATE_ARRAY_INT32_UNSAFE(input, MAX111xState, inputs,
  101. vmstate_info_uint8, uint8_t),
  102. VMSTATE_END_OF_LIST()
  103. }
  104. };
  105. static int max111x_init(SSISlave *d, int inputs)
  106. {
  107. DeviceState *dev = DEVICE(d);
  108. MAX111xState *s = MAX_111X(dev);
  109. qdev_init_gpio_out(dev, &s->interrupt, 1);
  110. s->inputs = inputs;
  111. /* TODO: add a user interface for setting these */
  112. s->input[0] = 0xf0;
  113. s->input[1] = 0xe0;
  114. s->input[2] = 0xd0;
  115. s->input[3] = 0xc0;
  116. s->input[4] = 0xb0;
  117. s->input[5] = 0xa0;
  118. s->input[6] = 0x90;
  119. s->input[7] = 0x80;
  120. s->com = 0;
  121. vmstate_register(dev, -1, &vmstate_max111x, s);
  122. return 0;
  123. }
  124. static void max1110_realize(SSISlave *dev, Error **errp)
  125. {
  126. max111x_init(dev, 8);
  127. }
  128. static void max1111_realize(SSISlave *dev, Error **errp)
  129. {
  130. max111x_init(dev, 4);
  131. }
  132. void max111x_set_input(DeviceState *dev, int line, uint8_t value)
  133. {
  134. MAX111xState *s = MAX_111X(dev);
  135. assert(line >= 0 && line < s->inputs);
  136. s->input[line] = value;
  137. }
  138. static void max111x_class_init(ObjectClass *klass, void *data)
  139. {
  140. SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
  141. k->transfer = max111x_transfer;
  142. }
  143. static const TypeInfo max111x_info = {
  144. .name = TYPE_MAX_111X,
  145. .parent = TYPE_SSI_SLAVE,
  146. .instance_size = sizeof(MAX111xState),
  147. .class_init = max111x_class_init,
  148. .abstract = true,
  149. };
  150. static void max1110_class_init(ObjectClass *klass, void *data)
  151. {
  152. SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
  153. k->realize = max1110_realize;
  154. }
  155. static const TypeInfo max1110_info = {
  156. .name = TYPE_MAX_1110,
  157. .parent = TYPE_MAX_111X,
  158. .class_init = max1110_class_init,
  159. };
  160. static void max1111_class_init(ObjectClass *klass, void *data)
  161. {
  162. SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
  163. k->realize = max1111_realize;
  164. }
  165. static const TypeInfo max1111_info = {
  166. .name = TYPE_MAX_1111,
  167. .parent = TYPE_MAX_111X,
  168. .class_init = max1111_class_init,
  169. };
  170. static void max111x_register_types(void)
  171. {
  172. type_register_static(&max111x_info);
  173. type_register_static(&max1110_info);
  174. type_register_static(&max1111_info);
  175. }
  176. type_init(max111x_register_types)