cmsdk-apb-uart.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * ARM CMSDK APB UART emulation
  3. *
  4. * Copyright (c) 2017 Linaro Limited
  5. * Written by Peter Maydell
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 or
  9. * (at your option) any later version.
  10. */
  11. /* This is a model of the "APB UART" which is part of the Cortex-M
  12. * System Design Kit (CMSDK) and documented in the Cortex-M System
  13. * Design Kit Technical Reference Manual (ARM DDI0479C):
  14. * https://developer.arm.com/products/system-design/system-design-kits/cortex-m-system-design-kit
  15. */
  16. #include "qemu/osdep.h"
  17. #include "qemu/log.h"
  18. #include "qemu/module.h"
  19. #include "qapi/error.h"
  20. #include "trace.h"
  21. #include "hw/sysbus.h"
  22. #include "migration/vmstate.h"
  23. #include "hw/registerfields.h"
  24. #include "chardev/char-fe.h"
  25. #include "chardev/char-serial.h"
  26. #include "hw/char/cmsdk-apb-uart.h"
  27. #include "hw/irq.h"
  28. REG32(DATA, 0)
  29. REG32(STATE, 4)
  30. FIELD(STATE, TXFULL, 0, 1)
  31. FIELD(STATE, RXFULL, 1, 1)
  32. FIELD(STATE, TXOVERRUN, 2, 1)
  33. FIELD(STATE, RXOVERRUN, 3, 1)
  34. REG32(CTRL, 8)
  35. FIELD(CTRL, TX_EN, 0, 1)
  36. FIELD(CTRL, RX_EN, 1, 1)
  37. FIELD(CTRL, TX_INTEN, 2, 1)
  38. FIELD(CTRL, RX_INTEN, 3, 1)
  39. FIELD(CTRL, TXO_INTEN, 4, 1)
  40. FIELD(CTRL, RXO_INTEN, 5, 1)
  41. FIELD(CTRL, HSTEST, 6, 1)
  42. REG32(INTSTATUS, 0xc)
  43. FIELD(INTSTATUS, TX, 0, 1)
  44. FIELD(INTSTATUS, RX, 1, 1)
  45. FIELD(INTSTATUS, TXO, 2, 1)
  46. FIELD(INTSTATUS, RXO, 3, 1)
  47. REG32(BAUDDIV, 0x10)
  48. REG32(PID4, 0xFD0)
  49. REG32(PID5, 0xFD4)
  50. REG32(PID6, 0xFD8)
  51. REG32(PID7, 0xFDC)
  52. REG32(PID0, 0xFE0)
  53. REG32(PID1, 0xFE4)
  54. REG32(PID2, 0xFE8)
  55. REG32(PID3, 0xFEC)
  56. REG32(CID0, 0xFF0)
  57. REG32(CID1, 0xFF4)
  58. REG32(CID2, 0xFF8)
  59. REG32(CID3, 0xFFC)
  60. /* PID/CID values */
  61. static const int uart_id[] = {
  62. 0x04, 0x00, 0x00, 0x00, /* PID4..PID7 */
  63. 0x21, 0xb8, 0x1b, 0x00, /* PID0..PID3 */
  64. 0x0d, 0xf0, 0x05, 0xb1, /* CID0..CID3 */
  65. };
  66. static bool uart_baudrate_ok(CMSDKAPBUART *s)
  67. {
  68. /* The minimum permitted bauddiv setting is 16, so we just ignore
  69. * settings below that (usually this means the device has just
  70. * been reset and not yet programmed).
  71. */
  72. return s->bauddiv >= 16 && s->bauddiv <= s->pclk_frq;
  73. }
  74. static void uart_update_parameters(CMSDKAPBUART *s)
  75. {
  76. QEMUSerialSetParams ssp;
  77. /* This UART is always 8N1 but the baud rate is programmable. */
  78. if (!uart_baudrate_ok(s)) {
  79. return;
  80. }
  81. ssp.data_bits = 8;
  82. ssp.parity = 'N';
  83. ssp.stop_bits = 1;
  84. ssp.speed = s->pclk_frq / s->bauddiv;
  85. qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_SERIAL_SET_PARAMS, &ssp);
  86. trace_cmsdk_apb_uart_set_params(ssp.speed);
  87. }
  88. static void cmsdk_apb_uart_update(CMSDKAPBUART *s)
  89. {
  90. /* update outbound irqs, including handling the way the rxo and txo
  91. * interrupt status bits are just logical AND of the overrun bit in
  92. * STATE and the overrun interrupt enable bit in CTRL.
  93. */
  94. uint32_t omask = (R_INTSTATUS_RXO_MASK | R_INTSTATUS_TXO_MASK);
  95. s->intstatus &= ~omask;
  96. s->intstatus |= (s->state & (s->ctrl >> 2) & omask);
  97. qemu_set_irq(s->txint, !!(s->intstatus & R_INTSTATUS_TX_MASK));
  98. qemu_set_irq(s->rxint, !!(s->intstatus & R_INTSTATUS_RX_MASK));
  99. qemu_set_irq(s->txovrint, !!(s->intstatus & R_INTSTATUS_TXO_MASK));
  100. qemu_set_irq(s->rxovrint, !!(s->intstatus & R_INTSTATUS_RXO_MASK));
  101. qemu_set_irq(s->uartint, !!(s->intstatus));
  102. }
  103. static int uart_can_receive(void *opaque)
  104. {
  105. CMSDKAPBUART *s = CMSDK_APB_UART(opaque);
  106. /* We can take a char if RX is enabled and the buffer is empty */
  107. if (s->ctrl & R_CTRL_RX_EN_MASK && !(s->state & R_STATE_RXFULL_MASK)) {
  108. return 1;
  109. }
  110. return 0;
  111. }
  112. static void uart_receive(void *opaque, const uint8_t *buf, int size)
  113. {
  114. CMSDKAPBUART *s = CMSDK_APB_UART(opaque);
  115. trace_cmsdk_apb_uart_receive(*buf);
  116. /* In fact uart_can_receive() ensures that we can't be
  117. * called unless RX is enabled and the buffer is empty,
  118. * but we include this logic as documentation of what the
  119. * hardware does if a character arrives in these circumstances.
  120. */
  121. if (!(s->ctrl & R_CTRL_RX_EN_MASK)) {
  122. /* Just drop the character on the floor */
  123. return;
  124. }
  125. if (s->state & R_STATE_RXFULL_MASK) {
  126. s->state |= R_STATE_RXOVERRUN_MASK;
  127. }
  128. s->rxbuf = *buf;
  129. s->state |= R_STATE_RXFULL_MASK;
  130. if (s->ctrl & R_CTRL_RX_INTEN_MASK) {
  131. s->intstatus |= R_INTSTATUS_RX_MASK;
  132. }
  133. cmsdk_apb_uart_update(s);
  134. }
  135. static uint64_t uart_read(void *opaque, hwaddr offset, unsigned size)
  136. {
  137. CMSDKAPBUART *s = CMSDK_APB_UART(opaque);
  138. uint64_t r;
  139. switch (offset) {
  140. case A_DATA:
  141. r = s->rxbuf;
  142. s->state &= ~R_STATE_RXFULL_MASK;
  143. cmsdk_apb_uart_update(s);
  144. qemu_chr_fe_accept_input(&s->chr);
  145. break;
  146. case A_STATE:
  147. r = s->state;
  148. break;
  149. case A_CTRL:
  150. r = s->ctrl;
  151. break;
  152. case A_INTSTATUS:
  153. r = s->intstatus;
  154. break;
  155. case A_BAUDDIV:
  156. r = s->bauddiv;
  157. break;
  158. case A_PID4 ... A_CID3:
  159. r = uart_id[(offset - A_PID4) / 4];
  160. break;
  161. default:
  162. qemu_log_mask(LOG_GUEST_ERROR,
  163. "CMSDK APB UART read: bad offset %x\n", (int) offset);
  164. r = 0;
  165. break;
  166. }
  167. trace_cmsdk_apb_uart_read(offset, r, size);
  168. return r;
  169. }
  170. /* Try to send tx data, and arrange to be called back later if
  171. * we can't (ie the char backend is busy/blocking).
  172. */
  173. static gboolean uart_transmit(GIOChannel *chan, GIOCondition cond, void *opaque)
  174. {
  175. CMSDKAPBUART *s = CMSDK_APB_UART(opaque);
  176. int ret;
  177. s->watch_tag = 0;
  178. if (!(s->ctrl & R_CTRL_TX_EN_MASK) || !(s->state & R_STATE_TXFULL_MASK)) {
  179. return FALSE;
  180. }
  181. ret = qemu_chr_fe_write(&s->chr, &s->txbuf, 1);
  182. if (ret <= 0) {
  183. s->watch_tag = qemu_chr_fe_add_watch(&s->chr, G_IO_OUT | G_IO_HUP,
  184. uart_transmit, s);
  185. if (!s->watch_tag) {
  186. /* Most common reason to be here is "no chardev backend":
  187. * just insta-drain the buffer, so the serial output
  188. * goes into a void, rather than blocking the guest.
  189. */
  190. goto buffer_drained;
  191. }
  192. /* Transmit pending */
  193. trace_cmsdk_apb_uart_tx_pending();
  194. return FALSE;
  195. }
  196. buffer_drained:
  197. /* Character successfully sent */
  198. trace_cmsdk_apb_uart_tx(s->txbuf);
  199. s->state &= ~R_STATE_TXFULL_MASK;
  200. /* Going from TXFULL set to clear triggers the tx interrupt */
  201. if (s->ctrl & R_CTRL_TX_INTEN_MASK) {
  202. s->intstatus |= R_INTSTATUS_TX_MASK;
  203. }
  204. cmsdk_apb_uart_update(s);
  205. return FALSE;
  206. }
  207. static void uart_cancel_transmit(CMSDKAPBUART *s)
  208. {
  209. if (s->watch_tag) {
  210. g_source_remove(s->watch_tag);
  211. s->watch_tag = 0;
  212. }
  213. }
  214. static void uart_write(void *opaque, hwaddr offset, uint64_t value,
  215. unsigned size)
  216. {
  217. CMSDKAPBUART *s = CMSDK_APB_UART(opaque);
  218. trace_cmsdk_apb_uart_write(offset, value, size);
  219. switch (offset) {
  220. case A_DATA:
  221. s->txbuf = value;
  222. if (s->state & R_STATE_TXFULL_MASK) {
  223. /* Buffer already full -- note the overrun and let the
  224. * existing pending transmit callback handle the new char.
  225. */
  226. s->state |= R_STATE_TXOVERRUN_MASK;
  227. cmsdk_apb_uart_update(s);
  228. } else {
  229. s->state |= R_STATE_TXFULL_MASK;
  230. uart_transmit(NULL, G_IO_OUT, s);
  231. }
  232. break;
  233. case A_STATE:
  234. /* Bits 0 and 1 are read only; bits 2 and 3 are W1C */
  235. s->state &= ~(value &
  236. (R_STATE_TXOVERRUN_MASK | R_STATE_RXOVERRUN_MASK));
  237. cmsdk_apb_uart_update(s);
  238. break;
  239. case A_CTRL:
  240. s->ctrl = value & 0x7f;
  241. if ((s->ctrl & R_CTRL_TX_EN_MASK) && !uart_baudrate_ok(s)) {
  242. qemu_log_mask(LOG_GUEST_ERROR,
  243. "CMSDK APB UART: Tx enabled with invalid baudrate\n");
  244. }
  245. cmsdk_apb_uart_update(s);
  246. break;
  247. case A_INTSTATUS:
  248. /* All bits are W1C. Clearing the overrun interrupt bits really
  249. * clears the overrun status bits in the STATE register (which
  250. * is then reflected into the intstatus value by the update function).
  251. */
  252. s->state &= ~(value & (R_INTSTATUS_TXO_MASK | R_INTSTATUS_RXO_MASK));
  253. s->intstatus &= ~value;
  254. cmsdk_apb_uart_update(s);
  255. break;
  256. case A_BAUDDIV:
  257. s->bauddiv = value & 0xFFFFF;
  258. uart_update_parameters(s);
  259. break;
  260. case A_PID4 ... A_CID3:
  261. qemu_log_mask(LOG_GUEST_ERROR,
  262. "CMSDK APB UART write: write to RO offset 0x%x\n",
  263. (int)offset);
  264. break;
  265. default:
  266. qemu_log_mask(LOG_GUEST_ERROR,
  267. "CMSDK APB UART write: bad offset 0x%x\n", (int) offset);
  268. break;
  269. }
  270. }
  271. static const MemoryRegionOps uart_ops = {
  272. .read = uart_read,
  273. .write = uart_write,
  274. .endianness = DEVICE_LITTLE_ENDIAN,
  275. };
  276. static void cmsdk_apb_uart_reset(DeviceState *dev)
  277. {
  278. CMSDKAPBUART *s = CMSDK_APB_UART(dev);
  279. trace_cmsdk_apb_uart_reset();
  280. uart_cancel_transmit(s);
  281. s->state = 0;
  282. s->ctrl = 0;
  283. s->intstatus = 0;
  284. s->bauddiv = 0;
  285. s->txbuf = 0;
  286. s->rxbuf = 0;
  287. }
  288. static void cmsdk_apb_uart_init(Object *obj)
  289. {
  290. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  291. CMSDKAPBUART *s = CMSDK_APB_UART(obj);
  292. memory_region_init_io(&s->iomem, obj, &uart_ops, s, "uart", 0x1000);
  293. sysbus_init_mmio(sbd, &s->iomem);
  294. sysbus_init_irq(sbd, &s->txint);
  295. sysbus_init_irq(sbd, &s->rxint);
  296. sysbus_init_irq(sbd, &s->txovrint);
  297. sysbus_init_irq(sbd, &s->rxovrint);
  298. sysbus_init_irq(sbd, &s->uartint);
  299. }
  300. static void cmsdk_apb_uart_realize(DeviceState *dev, Error **errp)
  301. {
  302. CMSDKAPBUART *s = CMSDK_APB_UART(dev);
  303. if (s->pclk_frq == 0) {
  304. error_setg(errp, "CMSDK APB UART: pclk-frq property must be set");
  305. return;
  306. }
  307. /* This UART has no flow control, so we do not need to register
  308. * an event handler to deal with CHR_EVENT_BREAK.
  309. */
  310. qemu_chr_fe_set_handlers(&s->chr, uart_can_receive, uart_receive,
  311. NULL, NULL, s, NULL, true);
  312. }
  313. static int cmsdk_apb_uart_post_load(void *opaque, int version_id)
  314. {
  315. CMSDKAPBUART *s = CMSDK_APB_UART(opaque);
  316. /* If we have a pending character, arrange to resend it. */
  317. if (s->state & R_STATE_TXFULL_MASK) {
  318. s->watch_tag = qemu_chr_fe_add_watch(&s->chr, G_IO_OUT | G_IO_HUP,
  319. uart_transmit, s);
  320. }
  321. uart_update_parameters(s);
  322. return 0;
  323. }
  324. static const VMStateDescription cmsdk_apb_uart_vmstate = {
  325. .name = "cmsdk-apb-uart",
  326. .version_id = 1,
  327. .minimum_version_id = 1,
  328. .post_load = cmsdk_apb_uart_post_load,
  329. .fields = (VMStateField[]) {
  330. VMSTATE_UINT32(state, CMSDKAPBUART),
  331. VMSTATE_UINT32(ctrl, CMSDKAPBUART),
  332. VMSTATE_UINT32(intstatus, CMSDKAPBUART),
  333. VMSTATE_UINT32(bauddiv, CMSDKAPBUART),
  334. VMSTATE_UINT8(txbuf, CMSDKAPBUART),
  335. VMSTATE_UINT8(rxbuf, CMSDKAPBUART),
  336. VMSTATE_END_OF_LIST()
  337. }
  338. };
  339. static Property cmsdk_apb_uart_properties[] = {
  340. DEFINE_PROP_CHR("chardev", CMSDKAPBUART, chr),
  341. DEFINE_PROP_UINT32("pclk-frq", CMSDKAPBUART, pclk_frq, 0),
  342. DEFINE_PROP_END_OF_LIST(),
  343. };
  344. static void cmsdk_apb_uart_class_init(ObjectClass *klass, void *data)
  345. {
  346. DeviceClass *dc = DEVICE_CLASS(klass);
  347. dc->realize = cmsdk_apb_uart_realize;
  348. dc->vmsd = &cmsdk_apb_uart_vmstate;
  349. dc->reset = cmsdk_apb_uart_reset;
  350. device_class_set_props(dc, cmsdk_apb_uart_properties);
  351. }
  352. static const TypeInfo cmsdk_apb_uart_info = {
  353. .name = TYPE_CMSDK_APB_UART,
  354. .parent = TYPE_SYS_BUS_DEVICE,
  355. .instance_size = sizeof(CMSDKAPBUART),
  356. .instance_init = cmsdk_apb_uart_init,
  357. .class_init = cmsdk_apb_uart_class_init,
  358. };
  359. static void cmsdk_apb_uart_register_types(void)
  360. {
  361. type_register_static(&cmsdk_apb_uart_info);
  362. }
  363. type_init(cmsdk_apb_uart_register_types);