max111x.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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/misc/max111x.h"
  14. #include "hw/irq.h"
  15. #include "migration/vmstate.h"
  16. #include "qemu/module.h"
  17. #include "hw/qdev-properties.h"
  18. /* Control-byte bitfields */
  19. #define CB_PD0 (1 << 0)
  20. #define CB_PD1 (1 << 1)
  21. #define CB_SGL (1 << 2)
  22. #define CB_UNI (1 << 3)
  23. #define CB_SEL0 (1 << 4)
  24. #define CB_SEL1 (1 << 5)
  25. #define CB_SEL2 (1 << 6)
  26. #define CB_START (1 << 7)
  27. #define CHANNEL_NUM(v, b0, b1, b2) \
  28. ((((v) >> (2 + (b0))) & 4) | \
  29. (((v) >> (3 + (b1))) & 2) | \
  30. (((v) >> (4 + (b2))) & 1))
  31. static uint32_t max111x_read(MAX111xState *s)
  32. {
  33. if (!s->tb1)
  34. return 0;
  35. switch (s->cycle ++) {
  36. case 1:
  37. return s->rb2;
  38. case 2:
  39. return s->rb3;
  40. }
  41. return 0;
  42. }
  43. /* Interpret a control-byte */
  44. static void max111x_write(MAX111xState *s, uint32_t value)
  45. {
  46. int measure, chan;
  47. /* Ignore the value if START bit is zero */
  48. if (!(value & CB_START))
  49. return;
  50. s->cycle = 0;
  51. if (!(value & CB_PD1)) {
  52. s->tb1 = 0;
  53. return;
  54. }
  55. s->tb1 = value;
  56. if (s->inputs == 8)
  57. chan = CHANNEL_NUM(value, 1, 0, 2);
  58. else
  59. chan = CHANNEL_NUM(value & ~CB_SEL0, 0, 1, 2);
  60. if (value & CB_SGL)
  61. measure = s->input[chan] - s->com;
  62. else
  63. measure = s->input[chan] - s->input[chan ^ 1];
  64. if (!(value & CB_UNI))
  65. measure ^= 0x80;
  66. s->rb2 = (measure >> 2) & 0x3f;
  67. s->rb3 = (measure << 6) & 0xc0;
  68. /* FIXME: When should the IRQ be lowered? */
  69. qemu_irq_raise(s->interrupt);
  70. }
  71. static uint32_t max111x_transfer(SSISlave *dev, uint32_t value)
  72. {
  73. MAX111xState *s = MAX_111X(dev);
  74. max111x_write(s, value);
  75. return max111x_read(s);
  76. }
  77. static const VMStateDescription vmstate_max111x = {
  78. .name = "max111x",
  79. .version_id = 1,
  80. .minimum_version_id = 1,
  81. .fields = (VMStateField[]) {
  82. VMSTATE_SSI_SLAVE(parent_obj, MAX111xState),
  83. VMSTATE_UINT8(tb1, MAX111xState),
  84. VMSTATE_UINT8(rb2, MAX111xState),
  85. VMSTATE_UINT8(rb3, MAX111xState),
  86. VMSTATE_INT32_EQUAL(inputs, MAX111xState, NULL),
  87. VMSTATE_INT32(com, MAX111xState),
  88. VMSTATE_ARRAY_INT32_UNSAFE(input, MAX111xState, inputs,
  89. vmstate_info_uint8, uint8_t),
  90. VMSTATE_END_OF_LIST()
  91. }
  92. };
  93. static void max111x_input_set(void *opaque, int line, int value)
  94. {
  95. MAX111xState *s = MAX_111X(opaque);
  96. assert(line >= 0 && line < s->inputs);
  97. s->input[line] = value;
  98. }
  99. static int max111x_init(SSISlave *d, int inputs)
  100. {
  101. DeviceState *dev = DEVICE(d);
  102. MAX111xState *s = MAX_111X(dev);
  103. qdev_init_gpio_out(dev, &s->interrupt, 1);
  104. qdev_init_gpio_in(dev, max111x_input_set, inputs);
  105. s->inputs = inputs;
  106. return 0;
  107. }
  108. static void max1110_realize(SSISlave *dev, Error **errp)
  109. {
  110. max111x_init(dev, 8);
  111. }
  112. static void max1111_realize(SSISlave *dev, Error **errp)
  113. {
  114. max111x_init(dev, 4);
  115. }
  116. static void max111x_reset(DeviceState *dev)
  117. {
  118. MAX111xState *s = MAX_111X(dev);
  119. int i;
  120. for (i = 0; i < s->inputs; i++) {
  121. s->input[i] = s->reset_input[i];
  122. }
  123. s->com = 0;
  124. s->tb1 = 0;
  125. s->rb2 = 0;
  126. s->rb3 = 0;
  127. s->cycle = 0;
  128. }
  129. static Property max1110_properties[] = {
  130. /* Reset values for ADC inputs */
  131. DEFINE_PROP_UINT8("input0", MAX111xState, reset_input[0], 0xf0),
  132. DEFINE_PROP_UINT8("input1", MAX111xState, reset_input[1], 0xe0),
  133. DEFINE_PROP_UINT8("input2", MAX111xState, reset_input[2], 0xd0),
  134. DEFINE_PROP_UINT8("input3", MAX111xState, reset_input[3], 0xc0),
  135. DEFINE_PROP_END_OF_LIST(),
  136. };
  137. static Property max1111_properties[] = {
  138. /* Reset values for ADC inputs */
  139. DEFINE_PROP_UINT8("input0", MAX111xState, reset_input[0], 0xf0),
  140. DEFINE_PROP_UINT8("input1", MAX111xState, reset_input[1], 0xe0),
  141. DEFINE_PROP_UINT8("input2", MAX111xState, reset_input[2], 0xd0),
  142. DEFINE_PROP_UINT8("input3", MAX111xState, reset_input[3], 0xc0),
  143. DEFINE_PROP_UINT8("input4", MAX111xState, reset_input[4], 0xb0),
  144. DEFINE_PROP_UINT8("input5", MAX111xState, reset_input[5], 0xa0),
  145. DEFINE_PROP_UINT8("input6", MAX111xState, reset_input[6], 0x90),
  146. DEFINE_PROP_UINT8("input7", MAX111xState, reset_input[7], 0x80),
  147. DEFINE_PROP_END_OF_LIST(),
  148. };
  149. static void max111x_class_init(ObjectClass *klass, void *data)
  150. {
  151. SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
  152. DeviceClass *dc = DEVICE_CLASS(klass);
  153. k->transfer = max111x_transfer;
  154. dc->reset = max111x_reset;
  155. dc->vmsd = &vmstate_max111x;
  156. }
  157. static const TypeInfo max111x_info = {
  158. .name = TYPE_MAX_111X,
  159. .parent = TYPE_SSI_SLAVE,
  160. .instance_size = sizeof(MAX111xState),
  161. .class_init = max111x_class_init,
  162. .abstract = true,
  163. };
  164. static void max1110_class_init(ObjectClass *klass, void *data)
  165. {
  166. SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
  167. DeviceClass *dc = DEVICE_CLASS(klass);
  168. k->realize = max1110_realize;
  169. device_class_set_props(dc, max1110_properties);
  170. }
  171. static const TypeInfo max1110_info = {
  172. .name = TYPE_MAX_1110,
  173. .parent = TYPE_MAX_111X,
  174. .class_init = max1110_class_init,
  175. };
  176. static void max1111_class_init(ObjectClass *klass, void *data)
  177. {
  178. SSISlaveClass *k = SSI_SLAVE_CLASS(klass);
  179. DeviceClass *dc = DEVICE_CLASS(klass);
  180. k->realize = max1111_realize;
  181. device_class_set_props(dc, max1111_properties);
  182. }
  183. static const TypeInfo max1111_info = {
  184. .name = TYPE_MAX_1111,
  185. .parent = TYPE_MAX_111X,
  186. .class_init = max1111_class_init,
  187. };
  188. static void max111x_register_types(void)
  189. {
  190. type_register_static(&max111x_info);
  191. type_register_static(&max1110_info);
  192. type_register_static(&max1111_info);
  193. }
  194. type_init(max111x_register_types)