2
0

xilinx_uartlite.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 "qemu-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, target_phys_addr_t 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. break;
  87. default:
  88. if (addr < ARRAY_SIZE(s->regs))
  89. r = s->regs[addr];
  90. DUART(qemu_log("%s addr=%x v=%x\n", __func__, addr, r));
  91. break;
  92. }
  93. return r;
  94. }
  95. static void
  96. uart_write(void *opaque, target_phys_addr_t addr,
  97. uint64_t val64, unsigned int size)
  98. {
  99. struct xlx_uartlite *s = opaque;
  100. uint32_t value = val64;
  101. unsigned char ch = value;
  102. addr >>= 2;
  103. switch (addr)
  104. {
  105. case R_STATUS:
  106. hw_error("write to UART STATUS?\n");
  107. break;
  108. case R_CTRL:
  109. if (value & CONTROL_RST_RX) {
  110. s->rx_fifo_pos = 0;
  111. s->rx_fifo_len = 0;
  112. }
  113. s->regs[addr] = value;
  114. break;
  115. case R_TX:
  116. if (s->chr)
  117. qemu_chr_fe_write(s->chr, &ch, 1);
  118. s->regs[addr] = value;
  119. /* hax. */
  120. s->regs[R_STATUS] |= STATUS_IE;
  121. break;
  122. default:
  123. DUART(printf("%s addr=%x v=%x\n", __func__, addr, value));
  124. if (addr < ARRAY_SIZE(s->regs))
  125. s->regs[addr] = value;
  126. break;
  127. }
  128. uart_update_status(s);
  129. uart_update_irq(s);
  130. }
  131. static const MemoryRegionOps uart_ops = {
  132. .read = uart_read,
  133. .write = uart_write,
  134. .endianness = DEVICE_NATIVE_ENDIAN,
  135. .valid = {
  136. .min_access_size = 1,
  137. .max_access_size = 4
  138. }
  139. };
  140. static void uart_rx(void *opaque, const uint8_t *buf, int size)
  141. {
  142. struct xlx_uartlite *s = opaque;
  143. /* Got a byte. */
  144. if (s->rx_fifo_len >= 8) {
  145. printf("WARNING: UART dropped char.\n");
  146. return;
  147. }
  148. s->rx_fifo[s->rx_fifo_pos] = *buf;
  149. s->rx_fifo_pos++;
  150. s->rx_fifo_pos &= 0x7;
  151. s->rx_fifo_len++;
  152. uart_update_status(s);
  153. uart_update_irq(s);
  154. }
  155. static int uart_can_rx(void *opaque)
  156. {
  157. struct xlx_uartlite *s = opaque;
  158. int r;
  159. r = s->rx_fifo_len < sizeof(s->rx_fifo);
  160. if (!r)
  161. printf("cannot receive!\n");
  162. return r;
  163. }
  164. static void uart_event(void *opaque, int event)
  165. {
  166. }
  167. static int xilinx_uartlite_init(SysBusDevice *dev)
  168. {
  169. struct xlx_uartlite *s = FROM_SYSBUS(typeof (*s), dev);
  170. sysbus_init_irq(dev, &s->irq);
  171. uart_update_status(s);
  172. memory_region_init_io(&s->mmio, &uart_ops, s, "xilinx-uartlite", R_MAX * 4);
  173. sysbus_init_mmio_region(dev, &s->mmio);
  174. s->chr = qdev_init_chardev(&dev->qdev);
  175. if (s->chr)
  176. qemu_chr_add_handlers(s->chr, uart_can_rx, uart_rx, uart_event, s);
  177. return 0;
  178. }
  179. static void xilinx_uart_register(void)
  180. {
  181. sysbus_register_dev("xilinx,uartlite", sizeof (struct xlx_uartlite),
  182. xilinx_uartlite_init);
  183. }
  184. device_init(xilinx_uart_register)