grlib_apbuart.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 "char/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. #define FIFO_LENGTH 1024
  62. typedef struct UART {
  63. SysBusDevice busdev;
  64. MemoryRegion iomem;
  65. qemu_irq irq;
  66. CharDriverState *chr;
  67. /* registers */
  68. uint32_t receive;
  69. uint32_t status;
  70. uint32_t control;
  71. /* FIFO */
  72. char buffer[FIFO_LENGTH];
  73. int len;
  74. int current;
  75. } UART;
  76. static int uart_data_to_read(UART *uart)
  77. {
  78. return uart->current < uart->len;
  79. }
  80. static char uart_pop(UART *uart)
  81. {
  82. char ret;
  83. if (uart->len == 0) {
  84. uart->status &= ~UART_DATA_READY;
  85. return 0;
  86. }
  87. ret = uart->buffer[uart->current++];
  88. if (uart->current >= uart->len) {
  89. /* Flush */
  90. uart->len = 0;
  91. uart->current = 0;
  92. }
  93. if (!uart_data_to_read(uart)) {
  94. uart->status &= ~UART_DATA_READY;
  95. }
  96. return ret;
  97. }
  98. static void uart_add_to_fifo(UART *uart,
  99. const uint8_t *buffer,
  100. int length)
  101. {
  102. if (uart->len + length > FIFO_LENGTH) {
  103. abort();
  104. }
  105. memcpy(uart->buffer + uart->len, buffer, length);
  106. uart->len += length;
  107. }
  108. static int grlib_apbuart_can_receive(void *opaque)
  109. {
  110. UART *uart = opaque;
  111. return FIFO_LENGTH - uart->len;
  112. }
  113. static void grlib_apbuart_receive(void *opaque, const uint8_t *buf, int size)
  114. {
  115. UART *uart = opaque;
  116. uart_add_to_fifo(uart, buf, size);
  117. uart->status |= UART_DATA_READY;
  118. if (uart->control & UART_RECEIVE_INTERRUPT) {
  119. qemu_irq_pulse(uart->irq);
  120. }
  121. }
  122. static void grlib_apbuart_event(void *opaque, int event)
  123. {
  124. trace_grlib_apbuart_event(event);
  125. }
  126. static uint64_t grlib_apbuart_read(void *opaque, hwaddr addr,
  127. unsigned size)
  128. {
  129. UART *uart = opaque;
  130. addr &= 0xff;
  131. /* Unit registers */
  132. switch (addr) {
  133. case DATA_OFFSET:
  134. case DATA_OFFSET + 3: /* when only one byte read */
  135. return uart_pop(uart);
  136. case STATUS_OFFSET:
  137. /* Read Only */
  138. return uart->status;
  139. case CONTROL_OFFSET:
  140. return uart->control;
  141. case SCALER_OFFSET:
  142. /* Not supported */
  143. return 0;
  144. default:
  145. trace_grlib_apbuart_readl_unknown(addr);
  146. return 0;
  147. }
  148. }
  149. static void grlib_apbuart_write(void *opaque, hwaddr addr,
  150. uint64_t value, unsigned size)
  151. {
  152. UART *uart = opaque;
  153. unsigned char c = 0;
  154. addr &= 0xff;
  155. /* Unit registers */
  156. switch (addr) {
  157. case DATA_OFFSET:
  158. case DATA_OFFSET + 3: /* When only one byte write */
  159. c = value & 0xFF;
  160. qemu_chr_fe_write(uart->chr, &c, 1);
  161. return;
  162. case STATUS_OFFSET:
  163. /* Read Only */
  164. return;
  165. case CONTROL_OFFSET:
  166. uart->control = value;
  167. return;
  168. case SCALER_OFFSET:
  169. /* Not supported */
  170. return;
  171. default:
  172. break;
  173. }
  174. trace_grlib_apbuart_writel_unknown(addr, value);
  175. }
  176. static const MemoryRegionOps grlib_apbuart_ops = {
  177. .write = grlib_apbuart_write,
  178. .read = grlib_apbuart_read,
  179. .endianness = DEVICE_NATIVE_ENDIAN,
  180. };
  181. static int grlib_apbuart_init(SysBusDevice *dev)
  182. {
  183. UART *uart = FROM_SYSBUS(typeof(*uart), dev);
  184. qemu_chr_add_handlers(uart->chr,
  185. grlib_apbuart_can_receive,
  186. grlib_apbuart_receive,
  187. grlib_apbuart_event,
  188. uart);
  189. sysbus_init_irq(dev, &uart->irq);
  190. memory_region_init_io(&uart->iomem, &grlib_apbuart_ops, uart,
  191. "uart", UART_REG_SIZE);
  192. sysbus_init_mmio(dev, &uart->iomem);
  193. return 0;
  194. }
  195. static Property grlib_gptimer_properties[] = {
  196. DEFINE_PROP_CHR("chrdev", UART, chr),
  197. DEFINE_PROP_END_OF_LIST(),
  198. };
  199. static void grlib_gptimer_class_init(ObjectClass *klass, void *data)
  200. {
  201. DeviceClass *dc = DEVICE_CLASS(klass);
  202. SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
  203. k->init = grlib_apbuart_init;
  204. dc->props = grlib_gptimer_properties;
  205. }
  206. static const TypeInfo grlib_gptimer_info = {
  207. .name = "grlib,apbuart",
  208. .parent = TYPE_SYS_BUS_DEVICE,
  209. .instance_size = sizeof(UART),
  210. .class_init = grlib_gptimer_class_init,
  211. };
  212. static void grlib_gptimer_register_types(void)
  213. {
  214. type_register_static(&grlib_gptimer_info);
  215. }
  216. type_init(grlib_gptimer_register_types)