2
0

lm32_uart.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 "qemu/osdep.h"
  24. #include "hw/hw.h"
  25. #include "hw/sysbus.h"
  26. #include "trace.h"
  27. #include "chardev/char-fe.h"
  28. #include "qemu/error-report.h"
  29. enum {
  30. R_RXTX = 0,
  31. R_IER,
  32. R_IIR,
  33. R_LCR,
  34. R_MCR,
  35. R_LSR,
  36. R_MSR,
  37. R_DIV,
  38. R_MAX
  39. };
  40. enum {
  41. IER_RBRI = (1<<0),
  42. IER_THRI = (1<<1),
  43. IER_RLSI = (1<<2),
  44. IER_MSI = (1<<3),
  45. };
  46. enum {
  47. IIR_STAT = (1<<0),
  48. IIR_ID0 = (1<<1),
  49. IIR_ID1 = (1<<2),
  50. };
  51. enum {
  52. LCR_WLS0 = (1<<0),
  53. LCR_WLS1 = (1<<1),
  54. LCR_STB = (1<<2),
  55. LCR_PEN = (1<<3),
  56. LCR_EPS = (1<<4),
  57. LCR_SP = (1<<5),
  58. LCR_SB = (1<<6),
  59. };
  60. enum {
  61. MCR_DTR = (1<<0),
  62. MCR_RTS = (1<<1),
  63. };
  64. enum {
  65. LSR_DR = (1<<0),
  66. LSR_OE = (1<<1),
  67. LSR_PE = (1<<2),
  68. LSR_FE = (1<<3),
  69. LSR_BI = (1<<4),
  70. LSR_THRE = (1<<5),
  71. LSR_TEMT = (1<<6),
  72. };
  73. enum {
  74. MSR_DCTS = (1<<0),
  75. MSR_DDSR = (1<<1),
  76. MSR_TERI = (1<<2),
  77. MSR_DDCD = (1<<3),
  78. MSR_CTS = (1<<4),
  79. MSR_DSR = (1<<5),
  80. MSR_RI = (1<<6),
  81. MSR_DCD = (1<<7),
  82. };
  83. #define TYPE_LM32_UART "lm32-uart"
  84. #define LM32_UART(obj) OBJECT_CHECK(LM32UartState, (obj), TYPE_LM32_UART)
  85. struct LM32UartState {
  86. SysBusDevice parent_obj;
  87. MemoryRegion iomem;
  88. CharBackend chr;
  89. qemu_irq irq;
  90. uint32_t regs[R_MAX];
  91. };
  92. typedef struct LM32UartState LM32UartState;
  93. static void uart_update_irq(LM32UartState *s)
  94. {
  95. unsigned int irq;
  96. if ((s->regs[R_LSR] & (LSR_OE | LSR_PE | LSR_FE | LSR_BI))
  97. && (s->regs[R_IER] & IER_RLSI)) {
  98. irq = 1;
  99. s->regs[R_IIR] = IIR_ID1 | IIR_ID0;
  100. } else if ((s->regs[R_LSR] & LSR_DR) && (s->regs[R_IER] & IER_RBRI)) {
  101. irq = 1;
  102. s->regs[R_IIR] = IIR_ID1;
  103. } else if ((s->regs[R_LSR] & LSR_THRE) && (s->regs[R_IER] & IER_THRI)) {
  104. irq = 1;
  105. s->regs[R_IIR] = IIR_ID0;
  106. } else if ((s->regs[R_MSR] & 0x0f) && (s->regs[R_IER] & IER_MSI)) {
  107. irq = 1;
  108. s->regs[R_IIR] = 0;
  109. } else {
  110. irq = 0;
  111. s->regs[R_IIR] = IIR_STAT;
  112. }
  113. trace_lm32_uart_irq_state(irq);
  114. qemu_set_irq(s->irq, irq);
  115. }
  116. static uint64_t uart_read(void *opaque, hwaddr addr,
  117. unsigned size)
  118. {
  119. LM32UartState *s = opaque;
  120. uint32_t r = 0;
  121. addr >>= 2;
  122. switch (addr) {
  123. case R_RXTX:
  124. r = s->regs[R_RXTX];
  125. s->regs[R_LSR] &= ~LSR_DR;
  126. uart_update_irq(s);
  127. qemu_chr_fe_accept_input(&s->chr);
  128. break;
  129. case R_IIR:
  130. case R_LSR:
  131. case R_MSR:
  132. r = s->regs[addr];
  133. break;
  134. case R_IER:
  135. case R_LCR:
  136. case R_MCR:
  137. case R_DIV:
  138. error_report("lm32_uart: read access to write only register 0x"
  139. TARGET_FMT_plx, addr << 2);
  140. break;
  141. default:
  142. error_report("lm32_uart: read access to unknown register 0x"
  143. TARGET_FMT_plx, addr << 2);
  144. break;
  145. }
  146. trace_lm32_uart_memory_read(addr << 2, r);
  147. return r;
  148. }
  149. static void uart_write(void *opaque, hwaddr addr,
  150. uint64_t value, unsigned size)
  151. {
  152. LM32UartState *s = opaque;
  153. unsigned char ch = value;
  154. trace_lm32_uart_memory_write(addr, value);
  155. addr >>= 2;
  156. switch (addr) {
  157. case R_RXTX:
  158. /* XXX this blocks entire thread. Rewrite to use
  159. * qemu_chr_fe_write and background I/O callbacks */
  160. qemu_chr_fe_write_all(&s->chr, &ch, 1);
  161. break;
  162. case R_IER:
  163. case R_LCR:
  164. case R_MCR:
  165. case R_DIV:
  166. s->regs[addr] = value;
  167. break;
  168. case R_IIR:
  169. case R_LSR:
  170. case R_MSR:
  171. error_report("lm32_uart: write access to read only register 0x"
  172. TARGET_FMT_plx, addr << 2);
  173. break;
  174. default:
  175. error_report("lm32_uart: write access to unknown register 0x"
  176. TARGET_FMT_plx, addr << 2);
  177. break;
  178. }
  179. uart_update_irq(s);
  180. }
  181. static const MemoryRegionOps uart_ops = {
  182. .read = uart_read,
  183. .write = uart_write,
  184. .endianness = DEVICE_NATIVE_ENDIAN,
  185. .valid = {
  186. .min_access_size = 4,
  187. .max_access_size = 4,
  188. },
  189. };
  190. static void uart_rx(void *opaque, const uint8_t *buf, int size)
  191. {
  192. LM32UartState *s = opaque;
  193. if (s->regs[R_LSR] & LSR_DR) {
  194. s->regs[R_LSR] |= LSR_OE;
  195. }
  196. s->regs[R_LSR] |= LSR_DR;
  197. s->regs[R_RXTX] = *buf;
  198. uart_update_irq(s);
  199. }
  200. static int uart_can_rx(void *opaque)
  201. {
  202. LM32UartState *s = opaque;
  203. return !(s->regs[R_LSR] & LSR_DR);
  204. }
  205. static void uart_event(void *opaque, int event)
  206. {
  207. }
  208. static void uart_reset(DeviceState *d)
  209. {
  210. LM32UartState *s = LM32_UART(d);
  211. int i;
  212. for (i = 0; i < R_MAX; i++) {
  213. s->regs[i] = 0;
  214. }
  215. /* defaults */
  216. s->regs[R_LSR] = LSR_THRE | LSR_TEMT;
  217. }
  218. static void lm32_uart_init(Object *obj)
  219. {
  220. LM32UartState *s = LM32_UART(obj);
  221. SysBusDevice *dev = SYS_BUS_DEVICE(obj);
  222. sysbus_init_irq(dev, &s->irq);
  223. memory_region_init_io(&s->iomem, obj, &uart_ops, s,
  224. "uart", R_MAX * 4);
  225. sysbus_init_mmio(dev, &s->iomem);
  226. }
  227. static void lm32_uart_realize(DeviceState *dev, Error **errp)
  228. {
  229. LM32UartState *s = LM32_UART(dev);
  230. qemu_chr_fe_set_handlers(&s->chr, uart_can_rx, uart_rx,
  231. uart_event, s, NULL, true);
  232. }
  233. static const VMStateDescription vmstate_lm32_uart = {
  234. .name = "lm32-uart",
  235. .version_id = 1,
  236. .minimum_version_id = 1,
  237. .fields = (VMStateField[]) {
  238. VMSTATE_UINT32_ARRAY(regs, LM32UartState, R_MAX),
  239. VMSTATE_END_OF_LIST()
  240. }
  241. };
  242. static Property lm32_uart_properties[] = {
  243. DEFINE_PROP_CHR("chardev", LM32UartState, chr),
  244. DEFINE_PROP_END_OF_LIST(),
  245. };
  246. static void lm32_uart_class_init(ObjectClass *klass, void *data)
  247. {
  248. DeviceClass *dc = DEVICE_CLASS(klass);
  249. dc->reset = uart_reset;
  250. dc->vmsd = &vmstate_lm32_uart;
  251. dc->props = lm32_uart_properties;
  252. dc->realize = lm32_uart_realize;
  253. }
  254. static const TypeInfo lm32_uart_info = {
  255. .name = TYPE_LM32_UART,
  256. .parent = TYPE_SYS_BUS_DEVICE,
  257. .instance_size = sizeof(LM32UartState),
  258. .instance_init = lm32_uart_init,
  259. .class_init = lm32_uart_class_init,
  260. };
  261. static void lm32_uart_register_types(void)
  262. {
  263. type_register_static(&lm32_uart_info);
  264. }
  265. type_init(lm32_uart_register_types)