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