lm32_juart.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * LatticeMico32 JTAG UART model.
  3. *
  4. * Copyright (c) 2010 Michael Walle <michael@walle.cc>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "hw/sysbus.h"
  21. #include "migration/vmstate.h"
  22. #include "qemu/module.h"
  23. #include "trace.h"
  24. #include "chardev/char-fe.h"
  25. #include "hw/char/lm32_juart.h"
  26. #include "hw/qdev-properties.h"
  27. enum {
  28. LM32_JUART_MIN_SAVE_VERSION = 0,
  29. LM32_JUART_CURRENT_SAVE_VERSION = 0,
  30. LM32_JUART_MAX_SAVE_VERSION = 0,
  31. };
  32. enum {
  33. JTX_FULL = (1<<8),
  34. };
  35. enum {
  36. JRX_FULL = (1<<8),
  37. };
  38. #define LM32_JUART(obj) OBJECT_CHECK(LM32JuartState, (obj), TYPE_LM32_JUART)
  39. struct LM32JuartState {
  40. SysBusDevice parent_obj;
  41. CharBackend chr;
  42. uint32_t jtx;
  43. uint32_t jrx;
  44. };
  45. typedef struct LM32JuartState LM32JuartState;
  46. uint32_t lm32_juart_get_jtx(DeviceState *d)
  47. {
  48. LM32JuartState *s = LM32_JUART(d);
  49. trace_lm32_juart_get_jtx(s->jtx);
  50. return s->jtx;
  51. }
  52. uint32_t lm32_juart_get_jrx(DeviceState *d)
  53. {
  54. LM32JuartState *s = LM32_JUART(d);
  55. trace_lm32_juart_get_jrx(s->jrx);
  56. return s->jrx;
  57. }
  58. void lm32_juart_set_jtx(DeviceState *d, uint32_t jtx)
  59. {
  60. LM32JuartState *s = LM32_JUART(d);
  61. unsigned char ch = jtx & 0xff;
  62. trace_lm32_juart_set_jtx(s->jtx);
  63. s->jtx = jtx;
  64. /* XXX this blocks entire thread. Rewrite to use
  65. * qemu_chr_fe_write and background I/O callbacks */
  66. qemu_chr_fe_write_all(&s->chr, &ch, 1);
  67. }
  68. void lm32_juart_set_jrx(DeviceState *d, uint32_t jtx)
  69. {
  70. LM32JuartState *s = LM32_JUART(d);
  71. trace_lm32_juart_set_jrx(s->jrx);
  72. s->jrx &= ~JRX_FULL;
  73. }
  74. static void juart_rx(void *opaque, const uint8_t *buf, int size)
  75. {
  76. LM32JuartState *s = opaque;
  77. s->jrx = *buf | JRX_FULL;
  78. }
  79. static int juart_can_rx(void *opaque)
  80. {
  81. LM32JuartState *s = opaque;
  82. return !(s->jrx & JRX_FULL);
  83. }
  84. static void juart_event(void *opaque, int event)
  85. {
  86. }
  87. static void juart_reset(DeviceState *d)
  88. {
  89. LM32JuartState *s = LM32_JUART(d);
  90. s->jtx = 0;
  91. s->jrx = 0;
  92. }
  93. static void lm32_juart_realize(DeviceState *dev, Error **errp)
  94. {
  95. LM32JuartState *s = LM32_JUART(dev);
  96. qemu_chr_fe_set_handlers(&s->chr, juart_can_rx, juart_rx,
  97. juart_event, NULL, s, NULL, true);
  98. }
  99. static const VMStateDescription vmstate_lm32_juart = {
  100. .name = "lm32-juart",
  101. .version_id = 1,
  102. .minimum_version_id = 1,
  103. .fields = (VMStateField[]) {
  104. VMSTATE_UINT32(jtx, LM32JuartState),
  105. VMSTATE_UINT32(jrx, LM32JuartState),
  106. VMSTATE_END_OF_LIST()
  107. }
  108. };
  109. static Property lm32_juart_properties[] = {
  110. DEFINE_PROP_CHR("chardev", LM32JuartState, chr),
  111. DEFINE_PROP_END_OF_LIST(),
  112. };
  113. static void lm32_juart_class_init(ObjectClass *klass, void *data)
  114. {
  115. DeviceClass *dc = DEVICE_CLASS(klass);
  116. dc->reset = juart_reset;
  117. dc->vmsd = &vmstate_lm32_juart;
  118. dc->props = lm32_juart_properties;
  119. dc->realize = lm32_juart_realize;
  120. }
  121. static const TypeInfo lm32_juart_info = {
  122. .name = TYPE_LM32_JUART,
  123. .parent = TYPE_SYS_BUS_DEVICE,
  124. .instance_size = sizeof(LM32JuartState),
  125. .class_init = lm32_juart_class_init,
  126. };
  127. static void lm32_juart_register_types(void)
  128. {
  129. type_register_static(&lm32_juart_info);
  130. }
  131. type_init(lm32_juart_register_types)