lm32_uart.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * QEMU model of the LatticeMico32 UART block.
  3. *
  4. * Copyright (c) 2010 Michael Walle <michael@walle.cc>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. *
  19. *
  20. * Specification available at:
  21. * http://www.latticesemi.com/documents/mico32uart.pdf
  22. */
  23. #include "hw.h"
  24. #include "sysbus.h"
  25. #include "trace.h"
  26. #include "qemu-char.h"
  27. #include "qemu-error.h"
  28. enum {
  29. R_RXTX = 0,
  30. R_IER,
  31. R_IIR,
  32. R_LCR,
  33. R_MCR,
  34. R_LSR,
  35. R_MSR,
  36. R_DIV,
  37. R_MAX
  38. };
  39. enum {
  40. IER_RBRI = (1<<0),
  41. IER_THRI = (1<<1),
  42. IER_RLSI = (1<<2),
  43. IER_MSI = (1<<3),
  44. };
  45. enum {
  46. IIR_STAT = (1<<0),
  47. IIR_ID0 = (1<<1),
  48. IIR_ID1 = (1<<2),
  49. };
  50. enum {
  51. LCR_WLS0 = (1<<0),
  52. LCR_WLS1 = (1<<1),
  53. LCR_STB = (1<<2),
  54. LCR_PEN = (1<<3),
  55. LCR_EPS = (1<<4),
  56. LCR_SP = (1<<5),
  57. LCR_SB = (1<<6),
  58. };
  59. enum {
  60. MCR_DTR = (1<<0),
  61. MCR_RTS = (1<<1),
  62. };
  63. enum {
  64. LSR_DR = (1<<0),
  65. LSR_OE = (1<<1),
  66. LSR_PE = (1<<2),
  67. LSR_FE = (1<<3),
  68. LSR_BI = (1<<4),
  69. LSR_THRE = (1<<5),
  70. LSR_TEMT = (1<<6),
  71. };
  72. enum {
  73. MSR_DCTS = (1<<0),
  74. MSR_DDSR = (1<<1),
  75. MSR_TERI = (1<<2),
  76. MSR_DDCD = (1<<3),
  77. MSR_CTS = (1<<4),
  78. MSR_DSR = (1<<5),
  79. MSR_RI = (1<<6),
  80. MSR_DCD = (1<<7),
  81. };
  82. struct LM32UartState {
  83. SysBusDevice busdev;
  84. MemoryRegion iomem;
  85. CharDriverState *chr;
  86. qemu_irq irq;
  87. uint32_t regs[R_MAX];
  88. };
  89. typedef struct LM32UartState LM32UartState;
  90. static void uart_update_irq(LM32UartState *s)
  91. {
  92. unsigned int irq;
  93. if ((s->regs[R_LSR] & (LSR_OE | LSR_PE | LSR_FE | LSR_BI))
  94. && (s->regs[R_IER] & IER_RLSI)) {
  95. irq = 1;
  96. s->regs[R_IIR] = IIR_ID1 | IIR_ID0;
  97. } else if ((s->regs[R_LSR] & LSR_DR) && (s->regs[R_IER] & IER_RBRI)) {
  98. irq = 1;
  99. s->regs[R_IIR] = IIR_ID1;
  100. } else if ((s->regs[R_LSR] & LSR_THRE) && (s->regs[R_IER] & IER_THRI)) {
  101. irq = 1;
  102. s->regs[R_IIR] = IIR_ID0;
  103. } else if ((s->regs[R_MSR] & 0x0f) && (s->regs[R_IER] & IER_MSI)) {
  104. irq = 1;
  105. s->regs[R_IIR] = 0;
  106. } else {
  107. irq = 0;
  108. s->regs[R_IIR] = IIR_STAT;
  109. }
  110. trace_lm32_uart_irq_state(irq);
  111. qemu_set_irq(s->irq, irq);
  112. }
  113. static uint64_t uart_read(void *opaque, target_phys_addr_t addr,
  114. unsigned size)
  115. {
  116. LM32UartState *s = opaque;
  117. uint32_t r = 0;
  118. addr >>= 2;
  119. switch (addr) {
  120. case R_RXTX:
  121. r = s->regs[R_RXTX];
  122. s->regs[R_LSR] &= ~LSR_DR;
  123. uart_update_irq(s);
  124. break;
  125. case R_IIR:
  126. case R_LSR:
  127. case R_MSR:
  128. r = s->regs[addr];
  129. break;
  130. case R_IER:
  131. case R_LCR:
  132. case R_MCR:
  133. case R_DIV:
  134. error_report("lm32_uart: read access to write only register 0x"
  135. TARGET_FMT_plx, addr << 2);
  136. break;
  137. default:
  138. error_report("lm32_uart: read access to unknown register 0x"
  139. TARGET_FMT_plx, addr << 2);
  140. break;
  141. }
  142. trace_lm32_uart_memory_read(addr << 2, r);
  143. return r;
  144. }
  145. static void uart_write(void *opaque, target_phys_addr_t addr,
  146. uint64_t value, unsigned size)
  147. {
  148. LM32UartState *s = opaque;
  149. unsigned char ch = value;
  150. trace_lm32_uart_memory_write(addr, value);
  151. addr >>= 2;
  152. switch (addr) {
  153. case R_RXTX:
  154. if (s->chr) {
  155. qemu_chr_fe_write(s->chr, &ch, 1);
  156. }
  157. break;
  158. case R_IER:
  159. case R_LCR:
  160. case R_MCR:
  161. case R_DIV:
  162. s->regs[addr] = value;
  163. break;
  164. case R_IIR:
  165. case R_LSR:
  166. case R_MSR:
  167. error_report("lm32_uart: write access to read only register 0x"
  168. TARGET_FMT_plx, addr << 2);
  169. break;
  170. default:
  171. error_report("lm32_uart: write access to unknown register 0x"
  172. TARGET_FMT_plx, addr << 2);
  173. break;
  174. }
  175. uart_update_irq(s);
  176. }
  177. static const MemoryRegionOps uart_ops = {
  178. .read = uart_read,
  179. .write = uart_write,
  180. .endianness = DEVICE_NATIVE_ENDIAN,
  181. .valid = {
  182. .min_access_size = 4,
  183. .max_access_size = 4,
  184. },
  185. };
  186. static void uart_rx(void *opaque, const uint8_t *buf, int size)
  187. {
  188. LM32UartState *s = opaque;
  189. if (s->regs[R_LSR] & LSR_DR) {
  190. s->regs[R_LSR] |= LSR_OE;
  191. }
  192. s->regs[R_LSR] |= LSR_DR;
  193. s->regs[R_RXTX] = *buf;
  194. uart_update_irq(s);
  195. }
  196. static int uart_can_rx(void *opaque)
  197. {
  198. LM32UartState *s = opaque;
  199. return !(s->regs[R_LSR] & LSR_DR);
  200. }
  201. static void uart_event(void *opaque, int event)
  202. {
  203. }
  204. static void uart_reset(DeviceState *d)
  205. {
  206. LM32UartState *s = container_of(d, LM32UartState, busdev.qdev);
  207. int i;
  208. for (i = 0; i < R_MAX; i++) {
  209. s->regs[i] = 0;
  210. }
  211. /* defaults */
  212. s->regs[R_LSR] = LSR_THRE | LSR_TEMT;
  213. }
  214. static int lm32_uart_init(SysBusDevice *dev)
  215. {
  216. LM32UartState *s = FROM_SYSBUS(typeof(*s), dev);
  217. sysbus_init_irq(dev, &s->irq);
  218. memory_region_init_io(&s->iomem, &uart_ops, s, "uart", R_MAX * 4);
  219. sysbus_init_mmio(dev, &s->iomem);
  220. s->chr = qemu_char_get_next_serial();
  221. if (s->chr) {
  222. qemu_chr_add_handlers(s->chr, uart_can_rx, uart_rx, uart_event, s);
  223. }
  224. return 0;
  225. }
  226. static const VMStateDescription vmstate_lm32_uart = {
  227. .name = "lm32-uart",
  228. .version_id = 1,
  229. .minimum_version_id = 1,
  230. .minimum_version_id_old = 1,
  231. .fields = (VMStateField[]) {
  232. VMSTATE_UINT32_ARRAY(regs, LM32UartState, R_MAX),
  233. VMSTATE_END_OF_LIST()
  234. }
  235. };
  236. static void lm32_uart_class_init(ObjectClass *klass, void *data)
  237. {
  238. DeviceClass *dc = DEVICE_CLASS(klass);
  239. SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
  240. k->init = lm32_uart_init;
  241. dc->reset = uart_reset;
  242. dc->vmsd = &vmstate_lm32_uart;
  243. }
  244. static TypeInfo lm32_uart_info = {
  245. .name = "lm32-uart",
  246. .parent = TYPE_SYS_BUS_DEVICE,
  247. .instance_size = sizeof(LM32UartState),
  248. .class_init = lm32_uart_class_init,
  249. };
  250. static void lm32_uart_register_types(void)
  251. {
  252. type_register_static(&lm32_uart_info);
  253. }
  254. type_init(lm32_uart_register_types)