lm32_juart.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "hw.h"
  20. #include "sysbus.h"
  21. #include "trace.h"
  22. #include "qemu-char.h"
  23. #include "lm32_juart.h"
  24. enum {
  25. LM32_JUART_MIN_SAVE_VERSION = 0,
  26. LM32_JUART_CURRENT_SAVE_VERSION = 0,
  27. LM32_JUART_MAX_SAVE_VERSION = 0,
  28. };
  29. enum {
  30. JTX_FULL = (1<<8),
  31. };
  32. enum {
  33. JRX_FULL = (1<<8),
  34. };
  35. struct LM32JuartState {
  36. SysBusDevice busdev;
  37. CharDriverState *chr;
  38. uint32_t jtx;
  39. uint32_t jrx;
  40. };
  41. typedef struct LM32JuartState LM32JuartState;
  42. uint32_t lm32_juart_get_jtx(DeviceState *d)
  43. {
  44. LM32JuartState *s = container_of(d, LM32JuartState, busdev.qdev);
  45. trace_lm32_juart_get_jtx(s->jtx);
  46. return s->jtx;
  47. }
  48. uint32_t lm32_juart_get_jrx(DeviceState *d)
  49. {
  50. LM32JuartState *s = container_of(d, LM32JuartState, busdev.qdev);
  51. trace_lm32_juart_get_jrx(s->jrx);
  52. return s->jrx;
  53. }
  54. void lm32_juart_set_jtx(DeviceState *d, uint32_t jtx)
  55. {
  56. LM32JuartState *s = container_of(d, LM32JuartState, busdev.qdev);
  57. unsigned char ch = jtx & 0xff;
  58. trace_lm32_juart_set_jtx(s->jtx);
  59. s->jtx = jtx;
  60. if (s->chr) {
  61. qemu_chr_fe_write(s->chr, &ch, 1);
  62. }
  63. }
  64. void lm32_juart_set_jrx(DeviceState *d, uint32_t jtx)
  65. {
  66. LM32JuartState *s = container_of(d, LM32JuartState, busdev.qdev);
  67. trace_lm32_juart_set_jrx(s->jrx);
  68. s->jrx &= ~JRX_FULL;
  69. }
  70. static void juart_rx(void *opaque, const uint8_t *buf, int size)
  71. {
  72. LM32JuartState *s = opaque;
  73. s->jrx = *buf | JRX_FULL;
  74. }
  75. static int juart_can_rx(void *opaque)
  76. {
  77. LM32JuartState *s = opaque;
  78. return !(s->jrx & JRX_FULL);
  79. }
  80. static void juart_event(void *opaque, int event)
  81. {
  82. }
  83. static void juart_reset(DeviceState *d)
  84. {
  85. LM32JuartState *s = container_of(d, LM32JuartState, busdev.qdev);
  86. s->jtx = 0;
  87. s->jrx = 0;
  88. }
  89. static int lm32_juart_init(SysBusDevice *dev)
  90. {
  91. LM32JuartState *s = FROM_SYSBUS(typeof(*s), dev);
  92. s->chr = qdev_init_chardev(&dev->qdev);
  93. if (s->chr) {
  94. qemu_chr_add_handlers(s->chr, juart_can_rx, juart_rx, juart_event, s);
  95. }
  96. return 0;
  97. }
  98. static const VMStateDescription vmstate_lm32_juart = {
  99. .name = "lm32-juart",
  100. .version_id = 1,
  101. .minimum_version_id = 1,
  102. .minimum_version_id_old = 1,
  103. .fields = (VMStateField[]) {
  104. VMSTATE_UINT32(jtx, LM32JuartState),
  105. VMSTATE_UINT32(jrx, LM32JuartState),
  106. VMSTATE_END_OF_LIST()
  107. }
  108. };
  109. static SysBusDeviceInfo lm32_juart_info = {
  110. .init = lm32_juart_init,
  111. .qdev.name = "lm32-juart",
  112. .qdev.size = sizeof(LM32JuartState),
  113. .qdev.vmsd = &vmstate_lm32_juart,
  114. .qdev.reset = juart_reset,
  115. };
  116. static void lm32_juart_register(void)
  117. {
  118. sysbus_register_withprop(&lm32_juart_info);
  119. }
  120. device_init(lm32_juart_register)