digic-uart.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * QEMU model of the Canon DIGIC UART block.
  3. *
  4. * Copyright (C) 2013 Antony Pavlov <antonynpavlov@gmail.com>
  5. *
  6. * This model is based on reverse engineering efforts
  7. * made by CHDK (http://chdk.wikia.com) and
  8. * Magic Lantern (http://www.magiclantern.fm) projects
  9. * contributors.
  10. *
  11. * See "Serial terminal" docs here:
  12. * http://magiclantern.wikia.com/wiki/Register_Map#Misc_Registers
  13. *
  14. * The QEMU model of the Milkymist UART block by Michael Walle
  15. * is used as a template.
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. */
  28. #include "qemu/osdep.h"
  29. #include "hw/sysbus.h"
  30. #include "migration/vmstate.h"
  31. #include "chardev/char-fe.h"
  32. #include "qemu/log.h"
  33. #include "qemu/module.h"
  34. #include "hw/char/digic-uart.h"
  35. #include "hw/qdev-properties.h"
  36. enum {
  37. ST_RX_RDY = (1 << 0),
  38. ST_TX_RDY = (1 << 1),
  39. };
  40. static uint64_t digic_uart_read(void *opaque, hwaddr addr,
  41. unsigned size)
  42. {
  43. DigicUartState *s = opaque;
  44. uint64_t ret = 0;
  45. addr >>= 2;
  46. switch (addr) {
  47. case R_RX:
  48. s->reg_st &= ~(ST_RX_RDY);
  49. ret = s->reg_rx;
  50. break;
  51. case R_ST:
  52. ret = s->reg_st;
  53. break;
  54. default:
  55. qemu_log_mask(LOG_UNIMP,
  56. "digic-uart: read access to unknown register 0x"
  57. TARGET_FMT_plx "\n", addr << 2);
  58. }
  59. return ret;
  60. }
  61. static void digic_uart_write(void *opaque, hwaddr addr, uint64_t value,
  62. unsigned size)
  63. {
  64. DigicUartState *s = opaque;
  65. unsigned char ch = value;
  66. addr >>= 2;
  67. switch (addr) {
  68. case R_TX:
  69. /* XXX this blocks entire thread. Rewrite to use
  70. * qemu_chr_fe_write and background I/O callbacks */
  71. qemu_chr_fe_write_all(&s->chr, &ch, 1);
  72. break;
  73. case R_ST:
  74. /*
  75. * Ignore write to R_ST.
  76. *
  77. * The point is that this register is actively used
  78. * during receiving and transmitting symbols,
  79. * but we don't know the function of most of bits.
  80. *
  81. * Ignoring writes to R_ST is only a simplification
  82. * of the model. It has no perceptible side effects
  83. * for existing guests.
  84. */
  85. break;
  86. default:
  87. qemu_log_mask(LOG_UNIMP,
  88. "digic-uart: write access to unknown register 0x"
  89. TARGET_FMT_plx "\n", addr << 2);
  90. }
  91. }
  92. static const MemoryRegionOps uart_mmio_ops = {
  93. .read = digic_uart_read,
  94. .write = digic_uart_write,
  95. .valid = {
  96. .min_access_size = 4,
  97. .max_access_size = 4,
  98. },
  99. .endianness = DEVICE_NATIVE_ENDIAN,
  100. };
  101. static int uart_can_rx(void *opaque)
  102. {
  103. DigicUartState *s = opaque;
  104. return !(s->reg_st & ST_RX_RDY);
  105. }
  106. static void uart_rx(void *opaque, const uint8_t *buf, int size)
  107. {
  108. DigicUartState *s = opaque;
  109. assert(uart_can_rx(opaque));
  110. s->reg_st |= ST_RX_RDY;
  111. s->reg_rx = *buf;
  112. }
  113. static void uart_event(void *opaque, QEMUChrEvent event)
  114. {
  115. }
  116. static void digic_uart_reset(DeviceState *d)
  117. {
  118. DigicUartState *s = DIGIC_UART(d);
  119. s->reg_rx = 0;
  120. s->reg_st = ST_TX_RDY;
  121. }
  122. static void digic_uart_realize(DeviceState *dev, Error **errp)
  123. {
  124. DigicUartState *s = DIGIC_UART(dev);
  125. qemu_chr_fe_set_handlers(&s->chr, uart_can_rx, uart_rx,
  126. uart_event, NULL, s, NULL, true);
  127. }
  128. static void digic_uart_init(Object *obj)
  129. {
  130. DigicUartState *s = DIGIC_UART(obj);
  131. memory_region_init_io(&s->regs_region, OBJECT(s), &uart_mmio_ops, s,
  132. TYPE_DIGIC_UART, 0x18);
  133. sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->regs_region);
  134. }
  135. static const VMStateDescription vmstate_digic_uart = {
  136. .name = "digic-uart",
  137. .version_id = 1,
  138. .minimum_version_id = 1,
  139. .fields = (VMStateField[]) {
  140. VMSTATE_UINT32(reg_rx, DigicUartState),
  141. VMSTATE_UINT32(reg_st, DigicUartState),
  142. VMSTATE_END_OF_LIST()
  143. }
  144. };
  145. static Property digic_uart_properties[] = {
  146. DEFINE_PROP_CHR("chardev", DigicUartState, chr),
  147. DEFINE_PROP_END_OF_LIST(),
  148. };
  149. static void digic_uart_class_init(ObjectClass *klass, void *data)
  150. {
  151. DeviceClass *dc = DEVICE_CLASS(klass);
  152. dc->realize = digic_uart_realize;
  153. dc->reset = digic_uart_reset;
  154. dc->vmsd = &vmstate_digic_uart;
  155. device_class_set_props(dc, digic_uart_properties);
  156. }
  157. static const TypeInfo digic_uart_info = {
  158. .name = TYPE_DIGIC_UART,
  159. .parent = TYPE_SYS_BUS_DEVICE,
  160. .instance_size = sizeof(DigicUartState),
  161. .instance_init = digic_uart_init,
  162. .class_init = digic_uart_class_init,
  163. };
  164. static void digic_uart_register_types(void)
  165. {
  166. type_register_static(&digic_uart_info);
  167. }
  168. type_init(digic_uart_register_types)