digic-uart.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. #include "hw/qdev-properties-system.h"
  37. enum {
  38. ST_RX_RDY = (1 << 0),
  39. ST_TX_RDY = (1 << 1),
  40. };
  41. static uint64_t digic_uart_read(void *opaque, hwaddr addr,
  42. unsigned size)
  43. {
  44. DigicUartState *s = opaque;
  45. uint64_t ret = 0;
  46. addr >>= 2;
  47. switch (addr) {
  48. case R_RX:
  49. s->reg_st &= ~(ST_RX_RDY);
  50. ret = s->reg_rx;
  51. break;
  52. case R_ST:
  53. ret = s->reg_st;
  54. break;
  55. default:
  56. qemu_log_mask(LOG_UNIMP,
  57. "digic-uart: read access to unknown register 0x"
  58. HWADDR_FMT_plx "\n", addr << 2);
  59. }
  60. return ret;
  61. }
  62. static void digic_uart_write(void *opaque, hwaddr addr, uint64_t value,
  63. unsigned size)
  64. {
  65. DigicUartState *s = opaque;
  66. unsigned char ch = value;
  67. addr >>= 2;
  68. switch (addr) {
  69. case R_TX:
  70. /* XXX this blocks entire thread. Rewrite to use
  71. * qemu_chr_fe_write and background I/O callbacks */
  72. qemu_chr_fe_write_all(&s->chr, &ch, 1);
  73. break;
  74. case R_ST:
  75. /*
  76. * Ignore write to R_ST.
  77. *
  78. * The point is that this register is actively used
  79. * during receiving and transmitting symbols,
  80. * but we don't know the function of most of bits.
  81. *
  82. * Ignoring writes to R_ST is only a simplification
  83. * of the model. It has no perceptible side effects
  84. * for existing guests.
  85. */
  86. break;
  87. default:
  88. qemu_log_mask(LOG_UNIMP,
  89. "digic-uart: write access to unknown register 0x"
  90. HWADDR_FMT_plx "\n", addr << 2);
  91. }
  92. }
  93. static const MemoryRegionOps uart_mmio_ops = {
  94. .read = digic_uart_read,
  95. .write = digic_uart_write,
  96. .valid = {
  97. .min_access_size = 4,
  98. .max_access_size = 4,
  99. },
  100. .endianness = DEVICE_NATIVE_ENDIAN,
  101. };
  102. static int uart_can_rx(void *opaque)
  103. {
  104. DigicUartState *s = opaque;
  105. return !(s->reg_st & ST_RX_RDY);
  106. }
  107. static void uart_rx(void *opaque, const uint8_t *buf, int size)
  108. {
  109. DigicUartState *s = opaque;
  110. assert(uart_can_rx(opaque));
  111. s->reg_st |= ST_RX_RDY;
  112. s->reg_rx = *buf;
  113. }
  114. static void uart_event(void *opaque, QEMUChrEvent event)
  115. {
  116. }
  117. static void digic_uart_reset(DeviceState *d)
  118. {
  119. DigicUartState *s = DIGIC_UART(d);
  120. s->reg_rx = 0;
  121. s->reg_st = ST_TX_RDY;
  122. }
  123. static void digic_uart_realize(DeviceState *dev, Error **errp)
  124. {
  125. DigicUartState *s = DIGIC_UART(dev);
  126. qemu_chr_fe_set_handlers(&s->chr, uart_can_rx, uart_rx,
  127. uart_event, NULL, s, NULL, true);
  128. }
  129. static void digic_uart_init(Object *obj)
  130. {
  131. DigicUartState *s = DIGIC_UART(obj);
  132. memory_region_init_io(&s->regs_region, OBJECT(s), &uart_mmio_ops, s,
  133. TYPE_DIGIC_UART, 0x18);
  134. sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->regs_region);
  135. }
  136. static const VMStateDescription vmstate_digic_uart = {
  137. .name = "digic-uart",
  138. .version_id = 1,
  139. .minimum_version_id = 1,
  140. .fields = (const VMStateField[]) {
  141. VMSTATE_UINT32(reg_rx, DigicUartState),
  142. VMSTATE_UINT32(reg_st, DigicUartState),
  143. VMSTATE_END_OF_LIST()
  144. }
  145. };
  146. static Property digic_uart_properties[] = {
  147. DEFINE_PROP_CHR("chardev", DigicUartState, chr),
  148. DEFINE_PROP_END_OF_LIST(),
  149. };
  150. static void digic_uart_class_init(ObjectClass *klass, void *data)
  151. {
  152. DeviceClass *dc = DEVICE_CLASS(klass);
  153. dc->realize = digic_uart_realize;
  154. device_class_set_legacy_reset(dc, digic_uart_reset);
  155. dc->vmsd = &vmstate_digic_uart;
  156. device_class_set_props(dc, digic_uart_properties);
  157. }
  158. static const TypeInfo digic_uart_info = {
  159. .name = TYPE_DIGIC_UART,
  160. .parent = TYPE_SYS_BUS_DEVICE,
  161. .instance_size = sizeof(DigicUartState),
  162. .instance_init = digic_uart_init,
  163. .class_init = digic_uart_class_init,
  164. };
  165. static void digic_uart_register_types(void)
  166. {
  167. type_register_static(&digic_uart_info);
  168. }
  169. type_init(digic_uart_register_types)