xref: /qemu/hw/char/xilinx_uartlite.c (revision 300b1fc68c47478f36705f23a93dce77ac3e429a)
1ee118d95SEdgar E. Iglesias /*
2ee118d95SEdgar E. Iglesias  * QEMU model of Xilinx uartlite.
3ee118d95SEdgar E. Iglesias  *
4ee118d95SEdgar E. Iglesias  * Copyright (c) 2009 Edgar E. Iglesias.
5ee118d95SEdgar E. Iglesias  *
6ee118d95SEdgar E. Iglesias  * Permission is hereby granted, free of charge, to any person obtaining a copy
7ee118d95SEdgar E. Iglesias  * of this software and associated documentation files (the "Software"), to deal
8ee118d95SEdgar E. Iglesias  * in the Software without restriction, including without limitation the rights
9ee118d95SEdgar E. Iglesias  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10ee118d95SEdgar E. Iglesias  * copies of the Software, and to permit persons to whom the Software is
11ee118d95SEdgar E. Iglesias  * furnished to do so, subject to the following conditions:
12ee118d95SEdgar E. Iglesias  *
13ee118d95SEdgar E. Iglesias  * The above copyright notice and this permission notice shall be included in
14ee118d95SEdgar E. Iglesias  * all copies or substantial portions of the Software.
15ee118d95SEdgar E. Iglesias  *
16ee118d95SEdgar E. Iglesias  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17ee118d95SEdgar E. Iglesias  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18ee118d95SEdgar E. Iglesias  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19ee118d95SEdgar E. Iglesias  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20ee118d95SEdgar E. Iglesias  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21ee118d95SEdgar E. Iglesias  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22ee118d95SEdgar E. Iglesias  * THE SOFTWARE.
23ee118d95SEdgar E. Iglesias  */
24ee118d95SEdgar E. Iglesias 
2583c9f4caSPaolo Bonzini #include "hw/sysbus.h"
26dccfcd0eSPaolo Bonzini #include "sysemu/char.h"
27ee118d95SEdgar E. Iglesias 
28ee118d95SEdgar E. Iglesias #define DUART(x)
29ee118d95SEdgar E. Iglesias 
30ee118d95SEdgar E. Iglesias #define R_RX            0
31ee118d95SEdgar E. Iglesias #define R_TX            1
32ee118d95SEdgar E. Iglesias #define R_STATUS        2
33ee118d95SEdgar E. Iglesias #define R_CTRL          3
34ee118d95SEdgar E. Iglesias #define R_MAX           4
35ee118d95SEdgar E. Iglesias 
36ee118d95SEdgar E. Iglesias #define STATUS_RXVALID    0x01
37ee118d95SEdgar E. Iglesias #define STATUS_RXFULL     0x02
38ee118d95SEdgar E. Iglesias #define STATUS_TXEMPTY    0x04
39ee118d95SEdgar E. Iglesias #define STATUS_TXFULL     0x08
40ee118d95SEdgar E. Iglesias #define STATUS_IE         0x10
41ee118d95SEdgar E. Iglesias #define STATUS_OVERRUN    0x20
42ee118d95SEdgar E. Iglesias #define STATUS_FRAME      0x40
43ee118d95SEdgar E. Iglesias #define STATUS_PARITY     0x80
44ee118d95SEdgar E. Iglesias 
45ee118d95SEdgar E. Iglesias #define CONTROL_RST_TX    0x01
46ee118d95SEdgar E. Iglesias #define CONTROL_RST_RX    0x02
47ee118d95SEdgar E. Iglesias #define CONTROL_IE        0x10
48ee118d95SEdgar E. Iglesias 
49ee118d95SEdgar E. Iglesias struct xlx_uartlite
50ee118d95SEdgar E. Iglesias {
51ee118d95SEdgar E. Iglesias     SysBusDevice busdev;
52010f3f5fSEdgar E. Iglesias     MemoryRegion mmio;
53ee118d95SEdgar E. Iglesias     CharDriverState *chr;
54ee118d95SEdgar E. Iglesias     qemu_irq irq;
55ee118d95SEdgar E. Iglesias 
56ee118d95SEdgar E. Iglesias     uint8_t rx_fifo[8];
57ee118d95SEdgar E. Iglesias     unsigned int rx_fifo_pos;
58ee118d95SEdgar E. Iglesias     unsigned int rx_fifo_len;
59ee118d95SEdgar E. Iglesias 
60ee118d95SEdgar E. Iglesias     uint32_t regs[R_MAX];
61ee118d95SEdgar E. Iglesias };
62ee118d95SEdgar E. Iglesias 
63ee118d95SEdgar E. Iglesias static void uart_update_irq(struct xlx_uartlite *s)
64ee118d95SEdgar E. Iglesias {
65ee118d95SEdgar E. Iglesias     unsigned int irq;
66ee118d95SEdgar E. Iglesias 
67ee118d95SEdgar E. Iglesias     if (s->rx_fifo_len)
68ee118d95SEdgar E. Iglesias         s->regs[R_STATUS] |= STATUS_IE;
69ee118d95SEdgar E. Iglesias 
70ee118d95SEdgar E. Iglesias     irq = (s->regs[R_STATUS] & STATUS_IE) && (s->regs[R_CTRL] & CONTROL_IE);
71ee118d95SEdgar E. Iglesias     qemu_set_irq(s->irq, irq);
72ee118d95SEdgar E. Iglesias }
73ee118d95SEdgar E. Iglesias 
74ee118d95SEdgar E. Iglesias static void uart_update_status(struct xlx_uartlite *s)
75ee118d95SEdgar E. Iglesias {
76ee118d95SEdgar E. Iglesias     uint32_t r;
77ee118d95SEdgar E. Iglesias 
78ee118d95SEdgar E. Iglesias     r = s->regs[R_STATUS];
79ee118d95SEdgar E. Iglesias     r &= ~7;
80ee118d95SEdgar E. Iglesias     r |= 1 << 2; /* Tx fifo is always empty. We are fast :) */
81ee118d95SEdgar E. Iglesias     r |= (s->rx_fifo_len == sizeof (s->rx_fifo)) << 1;
82ee118d95SEdgar E. Iglesias     r |= (!!s->rx_fifo_len);
83ee118d95SEdgar E. Iglesias     s->regs[R_STATUS] = r;
84ee118d95SEdgar E. Iglesias }
85ee118d95SEdgar E. Iglesias 
86010f3f5fSEdgar E. Iglesias static uint64_t
87a8170e5eSAvi Kivity uart_read(void *opaque, hwaddr addr, unsigned int size)
88ee118d95SEdgar E. Iglesias {
89ee118d95SEdgar E. Iglesias     struct xlx_uartlite *s = opaque;
90ee118d95SEdgar E. Iglesias     uint32_t r = 0;
91ee118d95SEdgar E. Iglesias     addr >>= 2;
92ee118d95SEdgar E. Iglesias     switch (addr)
93ee118d95SEdgar E. Iglesias     {
94ee118d95SEdgar E. Iglesias         case R_RX:
95ee118d95SEdgar E. Iglesias             r = s->rx_fifo[(s->rx_fifo_pos - s->rx_fifo_len) & 7];
96ee118d95SEdgar E. Iglesias             if (s->rx_fifo_len)
97ee118d95SEdgar E. Iglesias                 s->rx_fifo_len--;
98ee118d95SEdgar E. Iglesias             uart_update_status(s);
99ee118d95SEdgar E. Iglesias             uart_update_irq(s);
10080625b97SPeter Crosthwaite             qemu_chr_accept_input(s->chr);
101ee118d95SEdgar E. Iglesias             break;
102ee118d95SEdgar E. Iglesias 
103ee118d95SEdgar E. Iglesias         default:
104ee118d95SEdgar E. Iglesias             if (addr < ARRAY_SIZE(s->regs))
105ee118d95SEdgar E. Iglesias                 r = s->regs[addr];
106ee118d95SEdgar E. Iglesias             DUART(qemu_log("%s addr=%x v=%x\n", __func__, addr, r));
107ee118d95SEdgar E. Iglesias             break;
108ee118d95SEdgar E. Iglesias     }
109ee118d95SEdgar E. Iglesias     return r;
110ee118d95SEdgar E. Iglesias }
111ee118d95SEdgar E. Iglesias 
112ee118d95SEdgar E. Iglesias static void
113a8170e5eSAvi Kivity uart_write(void *opaque, hwaddr addr,
114010f3f5fSEdgar E. Iglesias            uint64_t val64, unsigned int size)
115ee118d95SEdgar E. Iglesias {
116ee118d95SEdgar E. Iglesias     struct xlx_uartlite *s = opaque;
117010f3f5fSEdgar E. Iglesias     uint32_t value = val64;
118ee118d95SEdgar E. Iglesias     unsigned char ch = value;
119ee118d95SEdgar E. Iglesias 
120ee118d95SEdgar E. Iglesias     addr >>= 2;
121ee118d95SEdgar E. Iglesias     switch (addr)
122ee118d95SEdgar E. Iglesias     {
123ee118d95SEdgar E. Iglesias         case R_STATUS:
124ee118d95SEdgar E. Iglesias             hw_error("write to UART STATUS?\n");
125ee118d95SEdgar E. Iglesias             break;
126ee118d95SEdgar E. Iglesias 
127ee118d95SEdgar E. Iglesias         case R_CTRL:
128ee118d95SEdgar E. Iglesias             if (value & CONTROL_RST_RX) {
129ee118d95SEdgar E. Iglesias                 s->rx_fifo_pos = 0;
130ee118d95SEdgar E. Iglesias                 s->rx_fifo_len = 0;
131ee118d95SEdgar E. Iglesias             }
132ee118d95SEdgar E. Iglesias             s->regs[addr] = value;
133ee118d95SEdgar E. Iglesias             break;
134ee118d95SEdgar E. Iglesias 
135ee118d95SEdgar E. Iglesias         case R_TX:
136ee118d95SEdgar E. Iglesias             if (s->chr)
1372cc6e0a1SAnthony Liguori                 qemu_chr_fe_write(s->chr, &ch, 1);
138ee118d95SEdgar E. Iglesias 
139ee118d95SEdgar E. Iglesias             s->regs[addr] = value;
140ee118d95SEdgar E. Iglesias 
141ee118d95SEdgar E. Iglesias             /* hax.  */
142ee118d95SEdgar E. Iglesias             s->regs[R_STATUS] |= STATUS_IE;
143ee118d95SEdgar E. Iglesias             break;
144ee118d95SEdgar E. Iglesias 
145ee118d95SEdgar E. Iglesias         default:
146ee118d95SEdgar E. Iglesias             DUART(printf("%s addr=%x v=%x\n", __func__, addr, value));
147ee118d95SEdgar E. Iglesias             if (addr < ARRAY_SIZE(s->regs))
148ee118d95SEdgar E. Iglesias                 s->regs[addr] = value;
149ee118d95SEdgar E. Iglesias             break;
150ee118d95SEdgar E. Iglesias     }
151ee118d95SEdgar E. Iglesias     uart_update_status(s);
152ee118d95SEdgar E. Iglesias     uart_update_irq(s);
153ee118d95SEdgar E. Iglesias }
154ee118d95SEdgar E. Iglesias 
155010f3f5fSEdgar E. Iglesias static const MemoryRegionOps uart_ops = {
156010f3f5fSEdgar E. Iglesias     .read = uart_read,
157010f3f5fSEdgar E. Iglesias     .write = uart_write,
158010f3f5fSEdgar E. Iglesias     .endianness = DEVICE_NATIVE_ENDIAN,
159010f3f5fSEdgar E. Iglesias     .valid = {
160010f3f5fSEdgar E. Iglesias         .min_access_size = 1,
161010f3f5fSEdgar E. Iglesias         .max_access_size = 4
162010f3f5fSEdgar E. Iglesias     }
163ee118d95SEdgar E. Iglesias };
164ee118d95SEdgar E. Iglesias 
165ee118d95SEdgar E. Iglesias static void uart_rx(void *opaque, const uint8_t *buf, int size)
166ee118d95SEdgar E. Iglesias {
167ee118d95SEdgar E. Iglesias     struct xlx_uartlite *s = opaque;
168ee118d95SEdgar E. Iglesias 
169ee118d95SEdgar E. Iglesias     /* Got a byte.  */
170ee118d95SEdgar E. Iglesias     if (s->rx_fifo_len >= 8) {
171ee118d95SEdgar E. Iglesias         printf("WARNING: UART dropped char.\n");
172ee118d95SEdgar E. Iglesias         return;
173ee118d95SEdgar E. Iglesias     }
174ee118d95SEdgar E. Iglesias     s->rx_fifo[s->rx_fifo_pos] = *buf;
175ee118d95SEdgar E. Iglesias     s->rx_fifo_pos++;
176ee118d95SEdgar E. Iglesias     s->rx_fifo_pos &= 0x7;
177ee118d95SEdgar E. Iglesias     s->rx_fifo_len++;
178ee118d95SEdgar E. Iglesias 
179ee118d95SEdgar E. Iglesias     uart_update_status(s);
180ee118d95SEdgar E. Iglesias     uart_update_irq(s);
181ee118d95SEdgar E. Iglesias }
182ee118d95SEdgar E. Iglesias 
183ee118d95SEdgar E. Iglesias static int uart_can_rx(void *opaque)
184ee118d95SEdgar E. Iglesias {
185ee118d95SEdgar E. Iglesias     struct xlx_uartlite *s = opaque;
186ee118d95SEdgar E. Iglesias 
187859cc10dSPeter Crosthwaite     return s->rx_fifo_len < sizeof(s->rx_fifo);
188ee118d95SEdgar E. Iglesias }
189ee118d95SEdgar E. Iglesias 
190ee118d95SEdgar E. Iglesias static void uart_event(void *opaque, int event)
191ee118d95SEdgar E. Iglesias {
192ee118d95SEdgar E. Iglesias 
193ee118d95SEdgar E. Iglesias }
194ee118d95SEdgar E. Iglesias 
19581a322d4SGerd Hoffmann static int xilinx_uartlite_init(SysBusDevice *dev)
196ee118d95SEdgar E. Iglesias {
197ee118d95SEdgar E. Iglesias     struct xlx_uartlite *s = FROM_SYSBUS(typeof (*s), dev);
198ee118d95SEdgar E. Iglesias 
199ee118d95SEdgar E. Iglesias     sysbus_init_irq(dev, &s->irq);
200ee118d95SEdgar E. Iglesias 
201ee118d95SEdgar E. Iglesias     uart_update_status(s);
202*300b1fc6SPaolo Bonzini     memory_region_init_io(&s->mmio, OBJECT(s), &uart_ops, s,
203*300b1fc6SPaolo Bonzini                           "xlnx.xps-uartlite", R_MAX * 4);
204750ecd44SAvi Kivity     sysbus_init_mmio(dev, &s->mmio);
205ee118d95SEdgar E. Iglesias 
2060beb4942SAnthony Liguori     s->chr = qemu_char_get_next_serial();
207ee118d95SEdgar E. Iglesias     if (s->chr)
208ee118d95SEdgar E. Iglesias         qemu_chr_add_handlers(s->chr, uart_can_rx, uart_rx, uart_event, s);
20981a322d4SGerd Hoffmann     return 0;
210ee118d95SEdgar E. Iglesias }
211ee118d95SEdgar E. Iglesias 
212999e12bbSAnthony Liguori static void xilinx_uartlite_class_init(ObjectClass *klass, void *data)
213999e12bbSAnthony Liguori {
214999e12bbSAnthony Liguori     SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(klass);
215999e12bbSAnthony Liguori 
216999e12bbSAnthony Liguori     sdc->init = xilinx_uartlite_init;
217999e12bbSAnthony Liguori }
218999e12bbSAnthony Liguori 
2198c43a6f0SAndreas Färber static const TypeInfo xilinx_uartlite_info = {
22023d6055eSPeter A. G. Crosthwaite     .name          = "xlnx.xps-uartlite",
22139bffca2SAnthony Liguori     .parent        = TYPE_SYS_BUS_DEVICE,
22239bffca2SAnthony Liguori     .instance_size = sizeof (struct xlx_uartlite),
223999e12bbSAnthony Liguori     .class_init    = xilinx_uartlite_class_init,
224999e12bbSAnthony Liguori };
225999e12bbSAnthony Liguori 
22683f7d43aSAndreas Färber static void xilinx_uart_register_types(void)
227ee118d95SEdgar E. Iglesias {
22839bffca2SAnthony Liguori     type_register_static(&xilinx_uartlite_info);
229ee118d95SEdgar E. Iglesias }
230ee118d95SEdgar E. Iglesias 
23183f7d43aSAndreas Färber type_init(xilinx_uart_register_types)
232