lm32_juart.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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/hw.h"
  21. #include "hw/sysbus.h"
  22. #include "trace.h"
  23. #include "chardev/char-fe.h"
  24. #include "hw/char/lm32_juart.h"
  25. enum {
  26. LM32_JUART_MIN_SAVE_VERSION = 0,
  27. LM32_JUART_CURRENT_SAVE_VERSION = 0,
  28. LM32_JUART_MAX_SAVE_VERSION = 0,
  29. };
  30. enum {
  31. JTX_FULL = (1<<8),
  32. };
  33. enum {
  34. JRX_FULL = (1<<8),
  35. };
  36. #define LM32_JUART(obj) OBJECT_CHECK(LM32JuartState, (obj), TYPE_LM32_JUART)
  37. struct LM32JuartState {
  38. SysBusDevice parent_obj;
  39. CharBackend chr;
  40. uint32_t jtx;
  41. uint32_t jrx;
  42. };
  43. typedef struct LM32JuartState LM32JuartState;
  44. uint32_t lm32_juart_get_jtx(DeviceState *d)
  45. {
  46. LM32JuartState *s = LM32_JUART(d);
  47. trace_lm32_juart_get_jtx(s->jtx);
  48. return s->jtx;
  49. }
  50. uint32_t lm32_juart_get_jrx(DeviceState *d)
  51. {
  52. LM32JuartState *s = LM32_JUART(d);
  53. trace_lm32_juart_get_jrx(s->jrx);
  54. return s->jrx;
  55. }
  56. void lm32_juart_set_jtx(DeviceState *d, uint32_t jtx)
  57. {
  58. LM32JuartState *s = LM32_JUART(d);
  59. unsigned char ch = jtx & 0xff;
  60. trace_lm32_juart_set_jtx(s->jtx);
  61. s->jtx = jtx;
  62. /* XXX this blocks entire thread. Rewrite to use
  63. * qemu_chr_fe_write and background I/O callbacks */
  64. qemu_chr_fe_write_all(&s->chr, &ch, 1);
  65. }
  66. void lm32_juart_set_jrx(DeviceState *d, uint32_t jtx)
  67. {
  68. LM32JuartState *s = LM32_JUART(d);
  69. trace_lm32_juart_set_jrx(s->jrx);
  70. s->jrx &= ~JRX_FULL;
  71. }
  72. static void juart_rx(void *opaque, const uint8_t *buf, int size)
  73. {
  74. LM32JuartState *s = opaque;
  75. s->jrx = *buf | JRX_FULL;
  76. }
  77. static int juart_can_rx(void *opaque)
  78. {
  79. LM32JuartState *s = opaque;
  80. return !(s->jrx & JRX_FULL);
  81. }
  82. static void juart_event(void *opaque, int event)
  83. {
  84. }
  85. static void juart_reset(DeviceState *d)
  86. {
  87. LM32JuartState *s = LM32_JUART(d);
  88. s->jtx = 0;
  89. s->jrx = 0;
  90. }
  91. static void lm32_juart_realize(DeviceState *dev, Error **errp)
  92. {
  93. LM32JuartState *s = LM32_JUART(dev);
  94. qemu_chr_fe_set_handlers(&s->chr, juart_can_rx, juart_rx,
  95. juart_event, s, NULL, true);
  96. }
  97. static const VMStateDescription vmstate_lm32_juart = {
  98. .name = "lm32-juart",
  99. .version_id = 1,
  100. .minimum_version_id = 1,
  101. .fields = (VMStateField[]) {
  102. VMSTATE_UINT32(jtx, LM32JuartState),
  103. VMSTATE_UINT32(jrx, LM32JuartState),
  104. VMSTATE_END_OF_LIST()
  105. }
  106. };
  107. static Property lm32_juart_properties[] = {
  108. DEFINE_PROP_CHR("chardev", LM32JuartState, chr),
  109. DEFINE_PROP_END_OF_LIST(),
  110. };
  111. static void lm32_juart_class_init(ObjectClass *klass, void *data)
  112. {
  113. DeviceClass *dc = DEVICE_CLASS(klass);
  114. dc->reset = juart_reset;
  115. dc->vmsd = &vmstate_lm32_juart;
  116. dc->props = lm32_juart_properties;
  117. dc->realize = lm32_juart_realize;
  118. }
  119. static const TypeInfo lm32_juart_info = {
  120. .name = TYPE_LM32_JUART,
  121. .parent = TYPE_SYS_BUS_DEVICE,
  122. .instance_size = sizeof(LM32JuartState),
  123. .class_init = lm32_juart_class_init,
  124. };
  125. static void lm32_juart_register_types(void)
  126. {
  127. type_register_static(&lm32_juart_info);
  128. }
  129. type_init(lm32_juart_register_types)