grlib_apbuart.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * QEMU GRLIB APB UART Emulator
  3. *
  4. * Copyright (c) 2010-2011 AdaCore
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "sysbus.h"
  25. #include "qemu-char.h"
  26. #include "trace.h"
  27. #define UART_REG_SIZE 20 /* Size of memory mapped registers */
  28. /* UART status register fields */
  29. #define UART_DATA_READY (1 << 0)
  30. #define UART_TRANSMIT_SHIFT_EMPTY (1 << 1)
  31. #define UART_TRANSMIT_FIFO_EMPTY (1 << 2)
  32. #define UART_BREAK_RECEIVED (1 << 3)
  33. #define UART_OVERRUN (1 << 4)
  34. #define UART_PARITY_ERROR (1 << 5)
  35. #define UART_FRAMING_ERROR (1 << 6)
  36. #define UART_TRANSMIT_FIFO_HALF (1 << 7)
  37. #define UART_RECEIVE_FIFO_HALF (1 << 8)
  38. #define UART_TRANSMIT_FIFO_FULL (1 << 9)
  39. #define UART_RECEIVE_FIFO_FULL (1 << 10)
  40. /* UART control register fields */
  41. #define UART_RECEIVE_ENABLE (1 << 0)
  42. #define UART_TRANSMIT_ENABLE (1 << 1)
  43. #define UART_RECEIVE_INTERRUPT (1 << 2)
  44. #define UART_TRANSMIT_INTERRUPT (1 << 3)
  45. #define UART_PARITY_SELECT (1 << 4)
  46. #define UART_PARITY_ENABLE (1 << 5)
  47. #define UART_FLOW_CONTROL (1 << 6)
  48. #define UART_LOOPBACK (1 << 7)
  49. #define UART_EXTERNAL_CLOCK (1 << 8)
  50. #define UART_RECEIVE_FIFO_INTERRUPT (1 << 9)
  51. #define UART_TRANSMIT_FIFO_INTERRUPT (1 << 10)
  52. #define UART_FIFO_DEBUG_MODE (1 << 11)
  53. #define UART_OUTPUT_ENABLE (1 << 12)
  54. #define UART_FIFO_AVAILABLE (1 << 31)
  55. /* Memory mapped register offsets */
  56. #define DATA_OFFSET 0x00
  57. #define STATUS_OFFSET 0x04
  58. #define CONTROL_OFFSET 0x08
  59. #define SCALER_OFFSET 0x0C /* not supported */
  60. #define FIFO_DEBUG_OFFSET 0x10 /* not supported */
  61. typedef struct UART {
  62. SysBusDevice busdev;
  63. qemu_irq irq;
  64. CharDriverState *chr;
  65. /* registers */
  66. uint32_t receive;
  67. uint32_t status;
  68. uint32_t control;
  69. } UART;
  70. static int grlib_apbuart_can_receive(void *opaque)
  71. {
  72. UART *uart = opaque;
  73. return !!(uart->status & UART_DATA_READY);
  74. }
  75. static void grlib_apbuart_receive(void *opaque, const uint8_t *buf, int size)
  76. {
  77. UART *uart = opaque;
  78. uart->receive = *buf;
  79. uart->status |= UART_DATA_READY;
  80. if (uart->control & UART_RECEIVE_INTERRUPT) {
  81. qemu_irq_pulse(uart->irq);
  82. }
  83. }
  84. static void grlib_apbuart_event(void *opaque, int event)
  85. {
  86. trace_grlib_apbuart_event(event);
  87. }
  88. static void
  89. grlib_apbuart_writel(void *opaque, target_phys_addr_t addr, uint32_t value)
  90. {
  91. UART *uart = opaque;
  92. unsigned char c = 0;
  93. addr &= 0xff;
  94. /* Unit registers */
  95. switch (addr) {
  96. case DATA_OFFSET:
  97. c = value & 0xFF;
  98. qemu_chr_fe_write(uart->chr, &c, 1);
  99. return;
  100. case STATUS_OFFSET:
  101. /* Read Only */
  102. return;
  103. case CONTROL_OFFSET:
  104. /* Not supported */
  105. return;
  106. case SCALER_OFFSET:
  107. /* Not supported */
  108. return;
  109. default:
  110. break;
  111. }
  112. trace_grlib_apbuart_writel_unknown(addr, value);
  113. }
  114. static CPUReadMemoryFunc * const grlib_apbuart_read[] = {
  115. NULL, NULL, NULL,
  116. };
  117. static CPUWriteMemoryFunc * const grlib_apbuart_write[] = {
  118. NULL, NULL, grlib_apbuart_writel,
  119. };
  120. static int grlib_apbuart_init(SysBusDevice *dev)
  121. {
  122. UART *uart = FROM_SYSBUS(typeof(*uart), dev);
  123. int uart_regs = 0;
  124. qemu_chr_add_handlers(uart->chr,
  125. grlib_apbuart_can_receive,
  126. grlib_apbuart_receive,
  127. grlib_apbuart_event,
  128. uart);
  129. sysbus_init_irq(dev, &uart->irq);
  130. uart_regs = cpu_register_io_memory(grlib_apbuart_read,
  131. grlib_apbuart_write,
  132. uart, DEVICE_NATIVE_ENDIAN);
  133. if (uart_regs < 0) {
  134. return -1;
  135. }
  136. sysbus_init_mmio(dev, UART_REG_SIZE, uart_regs);
  137. return 0;
  138. }
  139. static SysBusDeviceInfo grlib_gptimer_info = {
  140. .init = grlib_apbuart_init,
  141. .qdev.name = "grlib,apbuart",
  142. .qdev.size = sizeof(UART),
  143. .qdev.props = (Property[]) {
  144. DEFINE_PROP_CHR("chrdev", UART, chr),
  145. DEFINE_PROP_END_OF_LIST()
  146. }
  147. };
  148. static void grlib_gptimer_register(void)
  149. {
  150. sysbus_register_withprop(&grlib_gptimer_info);
  151. }
  152. device_init(grlib_gptimer_register)