lm32_uart.c 7.0 KB

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