grlib_apbuart.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * QEMU GRLIB APB UART Emulator
  3. *
  4. * SPDX-License-Identifier: MIT
  5. *
  6. * Copyright (c) 2010-2024 AdaCore
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "qemu/osdep.h"
  27. #include "hw/irq.h"
  28. #include "hw/qdev-properties.h"
  29. #include "hw/qdev-properties-system.h"
  30. #include "hw/char/grlib_uart.h"
  31. #include "hw/sysbus.h"
  32. #include "qemu/module.h"
  33. #include "chardev/char-fe.h"
  34. #include "trace.h"
  35. #include "qom/object.h"
  36. #define UART_REG_SIZE 20 /* Size of memory mapped registers */
  37. /* UART status register fields */
  38. #define UART_DATA_READY (1 << 0)
  39. #define UART_TRANSMIT_SHIFT_EMPTY (1 << 1)
  40. #define UART_TRANSMIT_FIFO_EMPTY (1 << 2)
  41. #define UART_BREAK_RECEIVED (1 << 3)
  42. #define UART_OVERRUN (1 << 4)
  43. #define UART_PARITY_ERROR (1 << 5)
  44. #define UART_FRAMING_ERROR (1 << 6)
  45. #define UART_TRANSMIT_FIFO_HALF (1 << 7)
  46. #define UART_RECEIVE_FIFO_HALF (1 << 8)
  47. #define UART_TRANSMIT_FIFO_FULL (1 << 9)
  48. #define UART_RECEIVE_FIFO_FULL (1 << 10)
  49. /* UART control register fields */
  50. #define UART_RECEIVE_ENABLE (1 << 0)
  51. #define UART_TRANSMIT_ENABLE (1 << 1)
  52. #define UART_RECEIVE_INTERRUPT (1 << 2)
  53. #define UART_TRANSMIT_INTERRUPT (1 << 3)
  54. #define UART_PARITY_SELECT (1 << 4)
  55. #define UART_PARITY_ENABLE (1 << 5)
  56. #define UART_FLOW_CONTROL (1 << 6)
  57. #define UART_LOOPBACK (1 << 7)
  58. #define UART_EXTERNAL_CLOCK (1 << 8)
  59. #define UART_RECEIVE_FIFO_INTERRUPT (1 << 9)
  60. #define UART_TRANSMIT_FIFO_INTERRUPT (1 << 10)
  61. #define UART_FIFO_DEBUG_MODE (1 << 11)
  62. #define UART_OUTPUT_ENABLE (1 << 12)
  63. #define UART_FIFO_AVAILABLE (1 << 31)
  64. /* Memory mapped register offsets */
  65. #define DATA_OFFSET 0x00
  66. #define STATUS_OFFSET 0x04
  67. #define CONTROL_OFFSET 0x08
  68. #define SCALER_OFFSET 0x0C /* not supported */
  69. #define FIFO_DEBUG_OFFSET 0x10 /* not supported */
  70. #define FIFO_LENGTH 1024
  71. OBJECT_DECLARE_SIMPLE_TYPE(UART, GRLIB_APB_UART)
  72. struct UART {
  73. SysBusDevice parent_obj;
  74. MemoryRegion iomem;
  75. qemu_irq irq;
  76. CharBackend chr;
  77. /* registers */
  78. uint32_t status;
  79. uint32_t control;
  80. /* FIFO */
  81. char buffer[FIFO_LENGTH];
  82. int len;
  83. int current;
  84. };
  85. static int uart_data_to_read(UART *uart)
  86. {
  87. return uart->current < uart->len;
  88. }
  89. static char uart_pop(UART *uart)
  90. {
  91. char ret;
  92. if (uart->len == 0) {
  93. uart->status &= ~UART_DATA_READY;
  94. return 0;
  95. }
  96. ret = uart->buffer[uart->current++];
  97. if (uart->current >= uart->len) {
  98. /* Flush */
  99. uart->len = 0;
  100. uart->current = 0;
  101. }
  102. if (!uart_data_to_read(uart)) {
  103. uart->status &= ~UART_DATA_READY;
  104. }
  105. return ret;
  106. }
  107. static void uart_add_to_fifo(UART *uart,
  108. const uint8_t *buffer,
  109. int length)
  110. {
  111. if (uart->len + length > FIFO_LENGTH) {
  112. abort();
  113. }
  114. memcpy(uart->buffer + uart->len, buffer, length);
  115. uart->len += length;
  116. }
  117. static int grlib_apbuart_can_receive(void *opaque)
  118. {
  119. UART *uart = opaque;
  120. return FIFO_LENGTH - uart->len;
  121. }
  122. static void grlib_apbuart_receive(void *opaque, const uint8_t *buf, int size)
  123. {
  124. UART *uart = opaque;
  125. if (uart->control & UART_RECEIVE_ENABLE) {
  126. uart_add_to_fifo(uart, buf, size);
  127. uart->status |= UART_DATA_READY;
  128. if (uart->control & UART_RECEIVE_INTERRUPT) {
  129. qemu_irq_pulse(uart->irq);
  130. }
  131. }
  132. }
  133. static void grlib_apbuart_event(void *opaque, QEMUChrEvent event)
  134. {
  135. trace_grlib_apbuart_event(event);
  136. }
  137. static uint64_t grlib_apbuart_read(void *opaque, hwaddr addr,
  138. unsigned size)
  139. {
  140. UART *uart = opaque;
  141. addr &= 0xff;
  142. /* Unit registers */
  143. switch (addr) {
  144. case DATA_OFFSET:
  145. case DATA_OFFSET + 3: /* when only one byte read */
  146. return uart_pop(uart);
  147. case STATUS_OFFSET:
  148. /* Read Only */
  149. return uart->status;
  150. case CONTROL_OFFSET:
  151. return uart->control;
  152. case SCALER_OFFSET:
  153. /* Not supported */
  154. return 0;
  155. default:
  156. trace_grlib_apbuart_readl_unknown(addr);
  157. return 0;
  158. }
  159. }
  160. static void grlib_apbuart_write(void *opaque, hwaddr addr,
  161. uint64_t value, unsigned size)
  162. {
  163. UART *uart = opaque;
  164. unsigned char c = 0;
  165. addr &= 0xff;
  166. /* Unit registers */
  167. switch (addr) {
  168. case DATA_OFFSET:
  169. case DATA_OFFSET + 3: /* When only one byte write */
  170. /* Transmit when character device available and transmitter enabled */
  171. if (qemu_chr_fe_backend_connected(&uart->chr) &&
  172. (uart->control & UART_TRANSMIT_ENABLE)) {
  173. c = value & 0xFF;
  174. /* XXX this blocks entire thread. Rewrite to use
  175. * qemu_chr_fe_write and background I/O callbacks */
  176. qemu_chr_fe_write_all(&uart->chr, &c, 1);
  177. /* Generate interrupt */
  178. if (uart->control & UART_TRANSMIT_INTERRUPT) {
  179. qemu_irq_pulse(uart->irq);
  180. }
  181. }
  182. return;
  183. case STATUS_OFFSET:
  184. /* Read Only */
  185. return;
  186. case CONTROL_OFFSET:
  187. uart->control = value;
  188. return;
  189. case SCALER_OFFSET:
  190. /* Not supported */
  191. return;
  192. default:
  193. break;
  194. }
  195. trace_grlib_apbuart_writel_unknown(addr, value);
  196. }
  197. static const MemoryRegionOps grlib_apbuart_ops = {
  198. .write = grlib_apbuart_write,
  199. .read = grlib_apbuart_read,
  200. .endianness = DEVICE_NATIVE_ENDIAN,
  201. };
  202. static void grlib_apbuart_realize(DeviceState *dev, Error **errp)
  203. {
  204. UART *uart = GRLIB_APB_UART(dev);
  205. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  206. qemu_chr_fe_set_handlers(&uart->chr,
  207. grlib_apbuart_can_receive,
  208. grlib_apbuart_receive,
  209. grlib_apbuart_event,
  210. NULL, uart, NULL, true);
  211. sysbus_init_irq(sbd, &uart->irq);
  212. memory_region_init_io(&uart->iomem, OBJECT(uart), &grlib_apbuart_ops, uart,
  213. "uart", UART_REG_SIZE);
  214. sysbus_init_mmio(sbd, &uart->iomem);
  215. }
  216. static void grlib_apbuart_reset(DeviceState *d)
  217. {
  218. UART *uart = GRLIB_APB_UART(d);
  219. /* Transmitter FIFO and shift registers are always empty in QEMU */
  220. uart->status = UART_TRANSMIT_FIFO_EMPTY | UART_TRANSMIT_SHIFT_EMPTY;
  221. /* Everything is off */
  222. uart->control = 0;
  223. /* Flush receive FIFO */
  224. uart->len = 0;
  225. uart->current = 0;
  226. }
  227. static const Property grlib_apbuart_properties[] = {
  228. DEFINE_PROP_CHR("chrdev", UART, chr),
  229. };
  230. static void grlib_apbuart_class_init(ObjectClass *klass, void *data)
  231. {
  232. DeviceClass *dc = DEVICE_CLASS(klass);
  233. dc->realize = grlib_apbuart_realize;
  234. device_class_set_legacy_reset(dc, grlib_apbuart_reset);
  235. device_class_set_props(dc, grlib_apbuart_properties);
  236. }
  237. static const TypeInfo grlib_apbuart_info = {
  238. .name = TYPE_GRLIB_APB_UART,
  239. .parent = TYPE_SYS_BUS_DEVICE,
  240. .instance_size = sizeof(UART),
  241. .class_init = grlib_apbuart_class_init,
  242. };
  243. static void grlib_apbuart_register_types(void)
  244. {
  245. type_register_static(&grlib_apbuart_info);
  246. }
  247. type_init(grlib_apbuart_register_types)