ibex_uart.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /*
  2. * QEMU lowRISC Ibex UART device
  3. *
  4. * Copyright (c) 2020 Western Digital
  5. *
  6. * For details check the documentation here:
  7. * https://docs.opentitan.org/hw/ip/uart/doc/
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include "qemu/osdep.h"
  28. #include "hw/char/ibex_uart.h"
  29. #include "hw/irq.h"
  30. #include "hw/qdev-clock.h"
  31. #include "hw/qdev-properties.h"
  32. #include "migration/vmstate.h"
  33. #include "qemu/log.h"
  34. #include "qemu/module.h"
  35. static void ibex_uart_update_irqs(IbexUartState *s)
  36. {
  37. if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_TX_WATERMARK_MASK) {
  38. qemu_set_irq(s->tx_watermark, 1);
  39. } else {
  40. qemu_set_irq(s->tx_watermark, 0);
  41. }
  42. if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_RX_WATERMARK_MASK) {
  43. qemu_set_irq(s->rx_watermark, 1);
  44. } else {
  45. qemu_set_irq(s->rx_watermark, 0);
  46. }
  47. if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_TX_EMPTY_MASK) {
  48. qemu_set_irq(s->tx_empty, 1);
  49. } else {
  50. qemu_set_irq(s->tx_empty, 0);
  51. }
  52. if (s->uart_intr_state & s->uart_intr_enable & R_INTR_STATE_RX_OVERFLOW_MASK) {
  53. qemu_set_irq(s->rx_overflow, 1);
  54. } else {
  55. qemu_set_irq(s->rx_overflow, 0);
  56. }
  57. }
  58. static int ibex_uart_can_receive(void *opaque)
  59. {
  60. IbexUartState *s = opaque;
  61. if (s->uart_ctrl & R_CTRL_RX_ENABLE_MASK) {
  62. return 1;
  63. }
  64. return 0;
  65. }
  66. static void ibex_uart_receive(void *opaque, const uint8_t *buf, int size)
  67. {
  68. IbexUartState *s = opaque;
  69. uint8_t rx_fifo_level = (s->uart_fifo_ctrl & R_FIFO_CTRL_RXILVL_MASK)
  70. >> R_FIFO_CTRL_RXILVL_SHIFT;
  71. s->uart_rdata = *buf;
  72. s->uart_status &= ~R_STATUS_RXIDLE_MASK;
  73. s->uart_status &= ~R_STATUS_RXEMPTY_MASK;
  74. if (size > rx_fifo_level) {
  75. s->uart_intr_state |= R_INTR_STATE_RX_WATERMARK_MASK;
  76. }
  77. ibex_uart_update_irqs(s);
  78. }
  79. static gboolean ibex_uart_xmit(GIOChannel *chan, GIOCondition cond,
  80. void *opaque)
  81. {
  82. IbexUartState *s = opaque;
  83. uint8_t tx_fifo_level = (s->uart_fifo_ctrl & R_FIFO_CTRL_TXILVL_MASK)
  84. >> R_FIFO_CTRL_TXILVL_SHIFT;
  85. int ret;
  86. /* instant drain the fifo when there's no back-end */
  87. if (!qemu_chr_fe_backend_connected(&s->chr)) {
  88. s->tx_level = 0;
  89. return FALSE;
  90. }
  91. if (!s->tx_level) {
  92. s->uart_status &= ~R_STATUS_TXFULL_MASK;
  93. s->uart_status |= R_STATUS_TXEMPTY_MASK;
  94. s->uart_intr_state |= R_INTR_STATE_TX_EMPTY_MASK;
  95. s->uart_intr_state &= ~R_INTR_STATE_TX_WATERMARK_MASK;
  96. ibex_uart_update_irqs(s);
  97. return FALSE;
  98. }
  99. ret = qemu_chr_fe_write(&s->chr, s->tx_fifo, s->tx_level);
  100. if (ret >= 0) {
  101. s->tx_level -= ret;
  102. memmove(s->tx_fifo, s->tx_fifo + ret, s->tx_level);
  103. }
  104. if (s->tx_level) {
  105. guint r = qemu_chr_fe_add_watch(&s->chr, G_IO_OUT | G_IO_HUP,
  106. ibex_uart_xmit, s);
  107. if (!r) {
  108. s->tx_level = 0;
  109. return FALSE;
  110. }
  111. }
  112. /* Clear the TX Full bit */
  113. if (s->tx_level != IBEX_UART_TX_FIFO_SIZE) {
  114. s->uart_status &= ~R_STATUS_TXFULL_MASK;
  115. }
  116. /* Disable the TX_WATERMARK IRQ */
  117. if (s->tx_level < tx_fifo_level) {
  118. s->uart_intr_state &= ~R_INTR_STATE_TX_WATERMARK_MASK;
  119. }
  120. /* Set TX empty */
  121. if (s->tx_level == 0) {
  122. s->uart_status |= R_STATUS_TXEMPTY_MASK;
  123. s->uart_intr_state |= R_INTR_STATE_TX_EMPTY_MASK;
  124. }
  125. ibex_uart_update_irqs(s);
  126. return FALSE;
  127. }
  128. static void uart_write_tx_fifo(IbexUartState *s, const uint8_t *buf,
  129. int size)
  130. {
  131. uint64_t current_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  132. uint8_t tx_fifo_level = (s->uart_fifo_ctrl & R_FIFO_CTRL_TXILVL_MASK)
  133. >> R_FIFO_CTRL_TXILVL_SHIFT;
  134. if (size > IBEX_UART_TX_FIFO_SIZE - s->tx_level) {
  135. size = IBEX_UART_TX_FIFO_SIZE - s->tx_level;
  136. qemu_log_mask(LOG_GUEST_ERROR, "ibex_uart: TX FIFO overflow");
  137. }
  138. memcpy(s->tx_fifo + s->tx_level, buf, size);
  139. s->tx_level += size;
  140. if (s->tx_level > 0) {
  141. s->uart_status &= ~R_STATUS_TXEMPTY_MASK;
  142. }
  143. if (s->tx_level >= tx_fifo_level) {
  144. s->uart_intr_state |= R_INTR_STATE_TX_WATERMARK_MASK;
  145. ibex_uart_update_irqs(s);
  146. }
  147. if (s->tx_level == IBEX_UART_TX_FIFO_SIZE) {
  148. s->uart_status |= R_STATUS_TXFULL_MASK;
  149. }
  150. timer_mod(s->fifo_trigger_handle, current_time +
  151. (s->char_tx_time * 4));
  152. }
  153. static void ibex_uart_reset(DeviceState *dev)
  154. {
  155. IbexUartState *s = IBEX_UART(dev);
  156. s->uart_intr_state = 0x00000000;
  157. s->uart_intr_state = 0x00000000;
  158. s->uart_intr_enable = 0x00000000;
  159. s->uart_ctrl = 0x00000000;
  160. s->uart_status = 0x0000003c;
  161. s->uart_rdata = 0x00000000;
  162. s->uart_fifo_ctrl = 0x00000000;
  163. s->uart_fifo_status = 0x00000000;
  164. s->uart_ovrd = 0x00000000;
  165. s->uart_val = 0x00000000;
  166. s->uart_timeout_ctrl = 0x00000000;
  167. s->tx_level = 0;
  168. s->char_tx_time = (NANOSECONDS_PER_SECOND / 230400) * 10;
  169. ibex_uart_update_irqs(s);
  170. }
  171. static uint64_t ibex_uart_get_baud(IbexUartState *s)
  172. {
  173. uint64_t baud;
  174. baud = ((s->uart_ctrl & R_CTRL_NCO_MASK) >> 16);
  175. baud *= clock_get_hz(s->f_clk);
  176. baud >>= 20;
  177. return baud;
  178. }
  179. static uint64_t ibex_uart_read(void *opaque, hwaddr addr,
  180. unsigned int size)
  181. {
  182. IbexUartState *s = opaque;
  183. uint64_t retvalue = 0;
  184. switch (addr >> 2) {
  185. case R_INTR_STATE:
  186. retvalue = s->uart_intr_state;
  187. break;
  188. case R_INTR_ENABLE:
  189. retvalue = s->uart_intr_enable;
  190. break;
  191. case R_INTR_TEST:
  192. qemu_log_mask(LOG_GUEST_ERROR,
  193. "%s: wdata is write only\n", __func__);
  194. break;
  195. case R_CTRL:
  196. retvalue = s->uart_ctrl;
  197. break;
  198. case R_STATUS:
  199. retvalue = s->uart_status;
  200. break;
  201. case R_RDATA:
  202. retvalue = s->uart_rdata;
  203. if (s->uart_ctrl & R_CTRL_RX_ENABLE_MASK) {
  204. qemu_chr_fe_accept_input(&s->chr);
  205. s->uart_status |= R_STATUS_RXIDLE_MASK;
  206. s->uart_status |= R_STATUS_RXEMPTY_MASK;
  207. }
  208. break;
  209. case R_WDATA:
  210. qemu_log_mask(LOG_GUEST_ERROR,
  211. "%s: wdata is write only\n", __func__);
  212. break;
  213. case R_FIFO_CTRL:
  214. retvalue = s->uart_fifo_ctrl;
  215. break;
  216. case R_FIFO_STATUS:
  217. retvalue = s->uart_fifo_status;
  218. retvalue |= s->tx_level & 0x1F;
  219. qemu_log_mask(LOG_UNIMP,
  220. "%s: RX fifos are not supported\n", __func__);
  221. break;
  222. case R_OVRD:
  223. retvalue = s->uart_ovrd;
  224. qemu_log_mask(LOG_UNIMP,
  225. "%s: ovrd is not supported\n", __func__);
  226. break;
  227. case R_VAL:
  228. retvalue = s->uart_val;
  229. qemu_log_mask(LOG_UNIMP,
  230. "%s: val is not supported\n", __func__);
  231. break;
  232. case R_TIMEOUT_CTRL:
  233. retvalue = s->uart_timeout_ctrl;
  234. qemu_log_mask(LOG_UNIMP,
  235. "%s: timeout_ctrl is not supported\n", __func__);
  236. break;
  237. default:
  238. qemu_log_mask(LOG_GUEST_ERROR,
  239. "%s: Bad offset 0x%"HWADDR_PRIx"\n", __func__, addr);
  240. return 0;
  241. }
  242. return retvalue;
  243. }
  244. static void ibex_uart_write(void *opaque, hwaddr addr,
  245. uint64_t val64, unsigned int size)
  246. {
  247. IbexUartState *s = opaque;
  248. uint32_t value = val64;
  249. switch (addr >> 2) {
  250. case R_INTR_STATE:
  251. /* Write 1 clear */
  252. s->uart_intr_state &= ~value;
  253. ibex_uart_update_irqs(s);
  254. break;
  255. case R_INTR_ENABLE:
  256. s->uart_intr_enable = value;
  257. ibex_uart_update_irqs(s);
  258. break;
  259. case R_INTR_TEST:
  260. s->uart_intr_state |= value;
  261. ibex_uart_update_irqs(s);
  262. break;
  263. case R_CTRL:
  264. s->uart_ctrl = value;
  265. if (value & R_CTRL_NF_MASK) {
  266. qemu_log_mask(LOG_UNIMP,
  267. "%s: UART_CTRL_NF is not supported\n", __func__);
  268. }
  269. if (value & R_CTRL_SLPBK_MASK) {
  270. qemu_log_mask(LOG_UNIMP,
  271. "%s: UART_CTRL_SLPBK is not supported\n", __func__);
  272. }
  273. if (value & R_CTRL_LLPBK_MASK) {
  274. qemu_log_mask(LOG_UNIMP,
  275. "%s: UART_CTRL_LLPBK is not supported\n", __func__);
  276. }
  277. if (value & R_CTRL_PARITY_EN_MASK) {
  278. qemu_log_mask(LOG_UNIMP,
  279. "%s: UART_CTRL_PARITY_EN is not supported\n",
  280. __func__);
  281. }
  282. if (value & R_CTRL_PARITY_ODD_MASK) {
  283. qemu_log_mask(LOG_UNIMP,
  284. "%s: UART_CTRL_PARITY_ODD is not supported\n",
  285. __func__);
  286. }
  287. if (value & R_CTRL_RXBLVL_MASK) {
  288. qemu_log_mask(LOG_UNIMP,
  289. "%s: UART_CTRL_RXBLVL is not supported\n", __func__);
  290. }
  291. if (value & R_CTRL_NCO_MASK) {
  292. uint64_t baud = ibex_uart_get_baud(s);
  293. s->char_tx_time = (NANOSECONDS_PER_SECOND / baud) * 10;
  294. }
  295. break;
  296. case R_STATUS:
  297. qemu_log_mask(LOG_GUEST_ERROR,
  298. "%s: status is read only\n", __func__);
  299. break;
  300. case R_RDATA:
  301. qemu_log_mask(LOG_GUEST_ERROR,
  302. "%s: rdata is read only\n", __func__);
  303. break;
  304. case R_WDATA:
  305. uart_write_tx_fifo(s, (uint8_t *) &value, 1);
  306. break;
  307. case R_FIFO_CTRL:
  308. s->uart_fifo_ctrl = value;
  309. if (value & R_FIFO_CTRL_RXRST_MASK) {
  310. qemu_log_mask(LOG_UNIMP,
  311. "%s: RX fifos are not supported\n", __func__);
  312. }
  313. if (value & R_FIFO_CTRL_TXRST_MASK) {
  314. s->tx_level = 0;
  315. }
  316. break;
  317. case R_FIFO_STATUS:
  318. qemu_log_mask(LOG_GUEST_ERROR,
  319. "%s: fifo_status is read only\n", __func__);
  320. break;
  321. case R_OVRD:
  322. s->uart_ovrd = value;
  323. qemu_log_mask(LOG_UNIMP,
  324. "%s: ovrd is not supported\n", __func__);
  325. break;
  326. case R_VAL:
  327. qemu_log_mask(LOG_GUEST_ERROR,
  328. "%s: val is read only\n", __func__);
  329. break;
  330. case R_TIMEOUT_CTRL:
  331. s->uart_timeout_ctrl = value;
  332. qemu_log_mask(LOG_UNIMP,
  333. "%s: timeout_ctrl is not supported\n", __func__);
  334. break;
  335. default:
  336. qemu_log_mask(LOG_GUEST_ERROR,
  337. "%s: Bad offset 0x%"HWADDR_PRIx"\n", __func__, addr);
  338. }
  339. }
  340. static void ibex_uart_clk_update(void *opaque)
  341. {
  342. IbexUartState *s = opaque;
  343. /* recompute uart's speed on clock change */
  344. uint64_t baud = ibex_uart_get_baud(s);
  345. s->char_tx_time = (NANOSECONDS_PER_SECOND / baud) * 10;
  346. }
  347. static void fifo_trigger_update(void *opaque)
  348. {
  349. IbexUartState *s = opaque;
  350. if (s->uart_ctrl & R_CTRL_TX_ENABLE_MASK) {
  351. ibex_uart_xmit(NULL, G_IO_OUT, s);
  352. }
  353. }
  354. static const MemoryRegionOps ibex_uart_ops = {
  355. .read = ibex_uart_read,
  356. .write = ibex_uart_write,
  357. .endianness = DEVICE_NATIVE_ENDIAN,
  358. .impl.min_access_size = 4,
  359. .impl.max_access_size = 4,
  360. };
  361. static int ibex_uart_post_load(void *opaque, int version_id)
  362. {
  363. IbexUartState *s = opaque;
  364. ibex_uart_update_irqs(s);
  365. return 0;
  366. }
  367. static const VMStateDescription vmstate_ibex_uart = {
  368. .name = TYPE_IBEX_UART,
  369. .version_id = 1,
  370. .minimum_version_id = 1,
  371. .post_load = ibex_uart_post_load,
  372. .fields = (VMStateField[]) {
  373. VMSTATE_UINT8_ARRAY(tx_fifo, IbexUartState,
  374. IBEX_UART_TX_FIFO_SIZE),
  375. VMSTATE_UINT32(tx_level, IbexUartState),
  376. VMSTATE_UINT64(char_tx_time, IbexUartState),
  377. VMSTATE_TIMER_PTR(fifo_trigger_handle, IbexUartState),
  378. VMSTATE_UINT32(uart_intr_state, IbexUartState),
  379. VMSTATE_UINT32(uart_intr_enable, IbexUartState),
  380. VMSTATE_UINT32(uart_ctrl, IbexUartState),
  381. VMSTATE_UINT32(uart_status, IbexUartState),
  382. VMSTATE_UINT32(uart_rdata, IbexUartState),
  383. VMSTATE_UINT32(uart_fifo_ctrl, IbexUartState),
  384. VMSTATE_UINT32(uart_fifo_status, IbexUartState),
  385. VMSTATE_UINT32(uart_ovrd, IbexUartState),
  386. VMSTATE_UINT32(uart_val, IbexUartState),
  387. VMSTATE_UINT32(uart_timeout_ctrl, IbexUartState),
  388. VMSTATE_END_OF_LIST()
  389. }
  390. };
  391. static Property ibex_uart_properties[] = {
  392. DEFINE_PROP_CHR("chardev", IbexUartState, chr),
  393. DEFINE_PROP_END_OF_LIST(),
  394. };
  395. static void ibex_uart_init(Object *obj)
  396. {
  397. IbexUartState *s = IBEX_UART(obj);
  398. s->f_clk = qdev_init_clock_in(DEVICE(obj), "f_clock",
  399. ibex_uart_clk_update, s);
  400. clock_set_hz(s->f_clk, IBEX_UART_CLOCK);
  401. sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->tx_watermark);
  402. sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->rx_watermark);
  403. sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->tx_empty);
  404. sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->rx_overflow);
  405. memory_region_init_io(&s->mmio, obj, &ibex_uart_ops, s,
  406. TYPE_IBEX_UART, 0x400);
  407. sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
  408. }
  409. static void ibex_uart_realize(DeviceState *dev, Error **errp)
  410. {
  411. IbexUartState *s = IBEX_UART(dev);
  412. s->fifo_trigger_handle = timer_new_ns(QEMU_CLOCK_VIRTUAL,
  413. fifo_trigger_update, s);
  414. qemu_chr_fe_set_handlers(&s->chr, ibex_uart_can_receive,
  415. ibex_uart_receive, NULL, NULL,
  416. s, NULL, true);
  417. }
  418. static void ibex_uart_class_init(ObjectClass *klass, void *data)
  419. {
  420. DeviceClass *dc = DEVICE_CLASS(klass);
  421. dc->reset = ibex_uart_reset;
  422. dc->realize = ibex_uart_realize;
  423. dc->vmsd = &vmstate_ibex_uart;
  424. device_class_set_props(dc, ibex_uart_properties);
  425. }
  426. static const TypeInfo ibex_uart_info = {
  427. .name = TYPE_IBEX_UART,
  428. .parent = TYPE_SYS_BUS_DEVICE,
  429. .instance_size = sizeof(IbexUartState),
  430. .instance_init = ibex_uart_init,
  431. .class_init = ibex_uart_class_init,
  432. };
  433. static void ibex_uart_register_types(void)
  434. {
  435. type_register_static(&ibex_uart_info);
  436. }
  437. type_init(ibex_uart_register_types)