xilinx_uartlite.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * QEMU model of Xilinx uartlite.
  3. *
  4. * Copyright (c) 2009 Edgar E. Iglesias.
  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. #define DUART(x)
  27. #define R_RX 0
  28. #define R_TX 1
  29. #define R_STATUS 2
  30. #define R_CTRL 3
  31. #define R_MAX 4
  32. #define STATUS_RXVALID 0x01
  33. #define STATUS_RXFULL 0x02
  34. #define STATUS_TXEMPTY 0x04
  35. #define STATUS_TXFULL 0x08
  36. #define STATUS_IE 0x10
  37. #define STATUS_OVERRUN 0x20
  38. #define STATUS_FRAME 0x40
  39. #define STATUS_PARITY 0x80
  40. #define CONTROL_RST_TX 0x01
  41. #define CONTROL_RST_RX 0x02
  42. #define CONTROL_IE 0x10
  43. struct xlx_uartlite
  44. {
  45. SysBusDevice busdev;
  46. MemoryRegion mmio;
  47. CharDriverState *chr;
  48. qemu_irq irq;
  49. uint8_t rx_fifo[8];
  50. unsigned int rx_fifo_pos;
  51. unsigned int rx_fifo_len;
  52. uint32_t regs[R_MAX];
  53. };
  54. static void uart_update_irq(struct xlx_uartlite *s)
  55. {
  56. unsigned int irq;
  57. if (s->rx_fifo_len)
  58. s->regs[R_STATUS] |= STATUS_IE;
  59. irq = (s->regs[R_STATUS] & STATUS_IE) && (s->regs[R_CTRL] & CONTROL_IE);
  60. qemu_set_irq(s->irq, irq);
  61. }
  62. static void uart_update_status(struct xlx_uartlite *s)
  63. {
  64. uint32_t r;
  65. r = s->regs[R_STATUS];
  66. r &= ~7;
  67. r |= 1 << 2; /* Tx fifo is always empty. We are fast :) */
  68. r |= (s->rx_fifo_len == sizeof (s->rx_fifo)) << 1;
  69. r |= (!!s->rx_fifo_len);
  70. s->regs[R_STATUS] = r;
  71. }
  72. static uint64_t
  73. uart_read(void *opaque, hwaddr addr, unsigned int size)
  74. {
  75. struct xlx_uartlite *s = opaque;
  76. uint32_t r = 0;
  77. addr >>= 2;
  78. switch (addr)
  79. {
  80. case R_RX:
  81. r = s->rx_fifo[(s->rx_fifo_pos - s->rx_fifo_len) & 7];
  82. if (s->rx_fifo_len)
  83. s->rx_fifo_len--;
  84. uart_update_status(s);
  85. uart_update_irq(s);
  86. qemu_chr_accept_input(s->chr);
  87. break;
  88. default:
  89. if (addr < ARRAY_SIZE(s->regs))
  90. r = s->regs[addr];
  91. DUART(qemu_log("%s addr=%x v=%x\n", __func__, addr, r));
  92. break;
  93. }
  94. return r;
  95. }
  96. static void
  97. uart_write(void *opaque, hwaddr addr,
  98. uint64_t val64, unsigned int size)
  99. {
  100. struct xlx_uartlite *s = opaque;
  101. uint32_t value = val64;
  102. unsigned char ch = value;
  103. addr >>= 2;
  104. switch (addr)
  105. {
  106. case R_STATUS:
  107. hw_error("write to UART STATUS?\n");
  108. break;
  109. case R_CTRL:
  110. if (value & CONTROL_RST_RX) {
  111. s->rx_fifo_pos = 0;
  112. s->rx_fifo_len = 0;
  113. }
  114. s->regs[addr] = value;
  115. break;
  116. case R_TX:
  117. if (s->chr)
  118. qemu_chr_fe_write(s->chr, &ch, 1);
  119. s->regs[addr] = value;
  120. /* hax. */
  121. s->regs[R_STATUS] |= STATUS_IE;
  122. break;
  123. default:
  124. DUART(printf("%s addr=%x v=%x\n", __func__, addr, value));
  125. if (addr < ARRAY_SIZE(s->regs))
  126. s->regs[addr] = value;
  127. break;
  128. }
  129. uart_update_status(s);
  130. uart_update_irq(s);
  131. }
  132. static const MemoryRegionOps uart_ops = {
  133. .read = uart_read,
  134. .write = uart_write,
  135. .endianness = DEVICE_NATIVE_ENDIAN,
  136. .valid = {
  137. .min_access_size = 1,
  138. .max_access_size = 4
  139. }
  140. };
  141. static void uart_rx(void *opaque, const uint8_t *buf, int size)
  142. {
  143. struct xlx_uartlite *s = opaque;
  144. /* Got a byte. */
  145. if (s->rx_fifo_len >= 8) {
  146. printf("WARNING: UART dropped char.\n");
  147. return;
  148. }
  149. s->rx_fifo[s->rx_fifo_pos] = *buf;
  150. s->rx_fifo_pos++;
  151. s->rx_fifo_pos &= 0x7;
  152. s->rx_fifo_len++;
  153. uart_update_status(s);
  154. uart_update_irq(s);
  155. }
  156. static int uart_can_rx(void *opaque)
  157. {
  158. struct xlx_uartlite *s = opaque;
  159. return s->rx_fifo_len < sizeof(s->rx_fifo);
  160. }
  161. static void uart_event(void *opaque, int event)
  162. {
  163. }
  164. static int xilinx_uartlite_init(SysBusDevice *dev)
  165. {
  166. struct xlx_uartlite *s = FROM_SYSBUS(typeof (*s), dev);
  167. sysbus_init_irq(dev, &s->irq);
  168. uart_update_status(s);
  169. memory_region_init_io(&s->mmio, &uart_ops, s, "xlnx.xps-uartlite",
  170. R_MAX * 4);
  171. sysbus_init_mmio(dev, &s->mmio);
  172. s->chr = qemu_char_get_next_serial();
  173. if (s->chr)
  174. qemu_chr_add_handlers(s->chr, uart_can_rx, uart_rx, uart_event, s);
  175. return 0;
  176. }
  177. static void xilinx_uartlite_class_init(ObjectClass *klass, void *data)
  178. {
  179. SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(klass);
  180. sdc->init = xilinx_uartlite_init;
  181. }
  182. static const TypeInfo xilinx_uartlite_info = {
  183. .name = "xlnx.xps-uartlite",
  184. .parent = TYPE_SYS_BUS_DEVICE,
  185. .instance_size = sizeof (struct xlx_uartlite),
  186. .class_init = xilinx_uartlite_class_init,
  187. };
  188. static void xilinx_uart_register_types(void)
  189. {
  190. type_register_static(&xilinx_uartlite_info);
  191. }
  192. type_init(xilinx_uart_register_types)