xref: /openbmc/qemu/include/hw/char/ibex_uart.h (revision e2abbeac)
1 /*
2  * QEMU lowRISC Ibex UART device
3  *
4  * Copyright (c) 2020 Western Digital
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #ifndef HW_IBEX_UART_H
26 #define HW_IBEX_UART_H
27 
28 #include "hw/sysbus.h"
29 #include "hw/registerfields.h"
30 #include "chardev/char-fe.h"
31 #include "qemu/timer.h"
32 #include "qom/object.h"
33 
34 REG32(INTR_STATE, 0x00)
35     FIELD(INTR_STATE, TX_WATERMARK, 0, 1)
36     FIELD(INTR_STATE, RX_WATERMARK, 1, 1)
37     FIELD(INTR_STATE, TX_EMPTY, 2, 1)
38     FIELD(INTR_STATE, RX_OVERFLOW, 3, 1)
39 REG32(INTR_ENABLE, 0x04)
40 REG32(INTR_TEST, 0x08)
41 REG32(CTRL, 0x0C)
42     FIELD(CTRL, TX_ENABLE, 0, 1)
43     FIELD(CTRL, RX_ENABLE, 1, 1)
44     FIELD(CTRL, NF, 2, 1)
45     FIELD(CTRL, SLPBK, 4, 1)
46     FIELD(CTRL, LLPBK, 5, 1)
47     FIELD(CTRL, PARITY_EN, 6, 1)
48     FIELD(CTRL, PARITY_ODD, 7, 1)
49     FIELD(CTRL, RXBLVL, 8, 2)
50     FIELD(CTRL, NCO, 16, 16)
51 REG32(STATUS, 0x10)
52     FIELD(STATUS, TXFULL, 0, 1)
53     FIELD(STATUS, RXFULL, 1, 1)
54     FIELD(STATUS, TXEMPTY, 2, 1)
55     FIELD(STATUS, RXIDLE, 4, 1)
56     FIELD(STATUS, RXEMPTY, 5, 1)
57 REG32(RDATA, 0x14)
58 REG32(WDATA, 0x18)
59 REG32(FIFO_CTRL, 0x1c)
60     FIELD(FIFO_CTRL, RXRST, 0, 1)
61     FIELD(FIFO_CTRL, TXRST, 1, 1)
62     FIELD(FIFO_CTRL, RXILVL, 2, 3)
63     FIELD(FIFO_CTRL, TXILVL, 5, 2)
64 REG32(FIFO_STATUS, 0x20)
65     FIELD(FIFO_STATUS, TXLVL, 0, 5)
66     FIELD(FIFO_STATUS, RXLVL, 16, 5)
67 REG32(OVRD, 0x24)
68 REG32(VAL, 0x28)
69 REG32(TIMEOUT_CTRL, 0x2c)
70 
71 #define IBEX_UART_TX_FIFO_SIZE 16
72 #define IBEX_UART_CLOCK 50000000 /* 50MHz clock */
73 
74 #define TYPE_IBEX_UART "ibex-uart"
75 OBJECT_DECLARE_SIMPLE_TYPE(IbexUartState, IBEX_UART)
76 
77 struct IbexUartState {
78     /* <private> */
79     SysBusDevice parent_obj;
80 
81     /* <public> */
82     MemoryRegion mmio;
83 
84     uint8_t tx_fifo[IBEX_UART_TX_FIFO_SIZE];
85     uint32_t tx_level;
86 
87     uint32_t rx_level;
88 
89     QEMUTimer *fifo_trigger_handle;
90     uint64_t char_tx_time;
91 
92     uint32_t uart_intr_state;
93     uint32_t uart_intr_enable;
94     uint32_t uart_ctrl;
95     uint32_t uart_status;
96     uint32_t uart_rdata;
97     uint32_t uart_fifo_ctrl;
98     uint32_t uart_fifo_status;
99     uint32_t uart_ovrd;
100     uint32_t uart_val;
101     uint32_t uart_timeout_ctrl;
102 
103     Clock *f_clk;
104 
105     CharBackend chr;
106     qemu_irq tx_watermark;
107     qemu_irq rx_watermark;
108     qemu_irq tx_empty;
109     qemu_irq rx_overflow;
110 };
111 #endif /* HW_IBEX_UART_H */
112