2
0

xilinx_uartlite.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 "qemu/osdep.h"
  25. #include "qemu/log.h"
  26. #include "qapi/error.h"
  27. #include "hw/char/xilinx_uartlite.h"
  28. #include "hw/irq.h"
  29. #include "hw/qdev-properties.h"
  30. #include "hw/qdev-properties-system.h"
  31. #include "hw/sysbus.h"
  32. #include "qemu/module.h"
  33. #include "chardev/char-fe.h"
  34. #include "qom/object.h"
  35. #define DUART(x)
  36. #define R_RX 0
  37. #define R_TX 1
  38. #define R_STATUS 2
  39. #define R_CTRL 3
  40. #define R_MAX 4
  41. #define STATUS_RXVALID 0x01
  42. #define STATUS_RXFULL 0x02
  43. #define STATUS_TXEMPTY 0x04
  44. #define STATUS_TXFULL 0x08
  45. #define STATUS_IE 0x10
  46. #define STATUS_OVERRUN 0x20
  47. #define STATUS_FRAME 0x40
  48. #define STATUS_PARITY 0x80
  49. #define CONTROL_RST_TX 0x01
  50. #define CONTROL_RST_RX 0x02
  51. #define CONTROL_IE 0x10
  52. struct XilinxUARTLite {
  53. SysBusDevice parent_obj;
  54. EndianMode model_endianness;
  55. MemoryRegion mmio;
  56. CharBackend chr;
  57. qemu_irq irq;
  58. uint8_t rx_fifo[8];
  59. unsigned int rx_fifo_pos;
  60. unsigned int rx_fifo_len;
  61. uint32_t regs[R_MAX];
  62. };
  63. static void uart_update_irq(XilinxUARTLite *s)
  64. {
  65. unsigned int irq;
  66. if (s->rx_fifo_len)
  67. s->regs[R_STATUS] |= STATUS_IE;
  68. irq = (s->regs[R_STATUS] & STATUS_IE) && (s->regs[R_CTRL] & CONTROL_IE);
  69. qemu_set_irq(s->irq, irq);
  70. }
  71. static void uart_update_status(XilinxUARTLite *s)
  72. {
  73. uint32_t r;
  74. r = s->regs[R_STATUS];
  75. r &= ~7;
  76. r |= 1 << 2; /* Tx fifo is always empty. We are fast :) */
  77. r |= (s->rx_fifo_len == sizeof (s->rx_fifo)) << 1;
  78. r |= (!!s->rx_fifo_len);
  79. s->regs[R_STATUS] = r;
  80. }
  81. static void xilinx_uartlite_reset(DeviceState *dev)
  82. {
  83. uart_update_status(XILINX_UARTLITE(dev));
  84. }
  85. static uint64_t
  86. uart_read(void *opaque, hwaddr addr, unsigned int size)
  87. {
  88. XilinxUARTLite *s = opaque;
  89. uint32_t r = 0;
  90. addr >>= 2;
  91. switch (addr)
  92. {
  93. case R_RX:
  94. r = s->rx_fifo[(s->rx_fifo_pos - s->rx_fifo_len) & 7];
  95. if (s->rx_fifo_len)
  96. s->rx_fifo_len--;
  97. uart_update_status(s);
  98. uart_update_irq(s);
  99. qemu_chr_fe_accept_input(&s->chr);
  100. break;
  101. default:
  102. if (addr < ARRAY_SIZE(s->regs))
  103. r = s->regs[addr];
  104. DUART(qemu_log("%s addr=%x v=%x\n", __func__, addr, r));
  105. break;
  106. }
  107. return r;
  108. }
  109. static void
  110. uart_write(void *opaque, hwaddr addr,
  111. uint64_t val64, unsigned int size)
  112. {
  113. XilinxUARTLite *s = opaque;
  114. uint32_t value = val64;
  115. unsigned char ch = value;
  116. addr >>= 2;
  117. switch (addr)
  118. {
  119. case R_STATUS:
  120. qemu_log_mask(LOG_GUEST_ERROR, "%s: write to UART STATUS\n",
  121. __func__);
  122. break;
  123. case R_CTRL:
  124. if (value & CONTROL_RST_RX) {
  125. s->rx_fifo_pos = 0;
  126. s->rx_fifo_len = 0;
  127. }
  128. s->regs[addr] = value;
  129. break;
  130. case R_TX:
  131. /* XXX this blocks entire thread. Rewrite to use
  132. * qemu_chr_fe_write and background I/O callbacks */
  133. qemu_chr_fe_write_all(&s->chr, &ch, 1);
  134. s->regs[addr] = value;
  135. /* hax. */
  136. s->regs[R_STATUS] |= STATUS_IE;
  137. break;
  138. default:
  139. DUART(printf("%s addr=%x v=%x\n", __func__, addr, value));
  140. if (addr < ARRAY_SIZE(s->regs))
  141. s->regs[addr] = value;
  142. break;
  143. }
  144. uart_update_status(s);
  145. uart_update_irq(s);
  146. }
  147. static const MemoryRegionOps uart_ops[2] = {
  148. [0 ... 1] = {
  149. .read = uart_read,
  150. .write = uart_write,
  151. .valid = {
  152. .min_access_size = 1,
  153. .max_access_size = 4,
  154. },
  155. },
  156. [0].endianness = DEVICE_LITTLE_ENDIAN,
  157. [1].endianness = DEVICE_BIG_ENDIAN,
  158. };
  159. static const Property xilinx_uartlite_properties[] = {
  160. DEFINE_PROP_ENDIAN_NODEFAULT("endianness", XilinxUARTLite, model_endianness),
  161. DEFINE_PROP_CHR("chardev", XilinxUARTLite, chr),
  162. };
  163. static void uart_rx(void *opaque, const uint8_t *buf, int size)
  164. {
  165. XilinxUARTLite *s = opaque;
  166. /* Got a byte. */
  167. if (s->rx_fifo_len >= 8) {
  168. printf("WARNING: UART dropped char.\n");
  169. return;
  170. }
  171. s->rx_fifo[s->rx_fifo_pos] = *buf;
  172. s->rx_fifo_pos++;
  173. s->rx_fifo_pos &= 0x7;
  174. s->rx_fifo_len++;
  175. uart_update_status(s);
  176. uart_update_irq(s);
  177. }
  178. static int uart_can_rx(void *opaque)
  179. {
  180. XilinxUARTLite *s = opaque;
  181. return s->rx_fifo_len < sizeof(s->rx_fifo);
  182. }
  183. static void uart_event(void *opaque, QEMUChrEvent event)
  184. {
  185. }
  186. static void xilinx_uartlite_realize(DeviceState *dev, Error **errp)
  187. {
  188. XilinxUARTLite *s = XILINX_UARTLITE(dev);
  189. if (s->model_endianness == ENDIAN_MODE_UNSPECIFIED) {
  190. error_setg(errp, TYPE_XILINX_UARTLITE " property 'endianness'"
  191. " must be set to 'big' or 'little'");
  192. return;
  193. }
  194. memory_region_init_io(&s->mmio, OBJECT(dev),
  195. &uart_ops[s->model_endianness == ENDIAN_MODE_BIG],
  196. s, "xlnx.xps-uartlite", R_MAX * 4);
  197. qemu_chr_fe_set_handlers(&s->chr, uart_can_rx, uart_rx,
  198. uart_event, NULL, s, NULL, true);
  199. }
  200. static void xilinx_uartlite_init(Object *obj)
  201. {
  202. XilinxUARTLite *s = XILINX_UARTLITE(obj);
  203. sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->irq);
  204. sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
  205. }
  206. static void xilinx_uartlite_class_init(ObjectClass *klass, void *data)
  207. {
  208. DeviceClass *dc = DEVICE_CLASS(klass);
  209. device_class_set_legacy_reset(dc, xilinx_uartlite_reset);
  210. dc->realize = xilinx_uartlite_realize;
  211. device_class_set_props(dc, xilinx_uartlite_properties);
  212. }
  213. static const TypeInfo xilinx_uartlite_info = {
  214. .name = TYPE_XILINX_UARTLITE,
  215. .parent = TYPE_SYS_BUS_DEVICE,
  216. .instance_size = sizeof(XilinxUARTLite),
  217. .instance_init = xilinx_uartlite_init,
  218. .class_init = xilinx_uartlite_class_init,
  219. };
  220. static void xilinx_uart_register_types(void)
  221. {
  222. type_register_static(&xilinx_uartlite_info);
  223. }
  224. type_init(xilinx_uart_register_types)