1 /*
2 * QEMU model of the UART on the SiFive E300 and U500 series SOCs.
3 *
4 * Copyright (c) 2016 Stefan O'Rear
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2 or later, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "qemu/osdep.h"
20 #include "qapi/error.h"
21 #include "qemu/log.h"
22 #include "migration/vmstate.h"
23 #include "chardev/char.h"
24 #include "chardev/char-fe.h"
25 #include "hw/irq.h"
26 #include "hw/char/sifive_uart.h"
27 #include "hw/qdev-properties-system.h"
28
29 #define TX_INTERRUPT_TRIGGER_DELAY_NS 100
30
31 /*
32 * Not yet implemented:
33 *
34 * Transmit FIFO using "qemu/fifo8.h"
35 */
36
37 /* Returns the state of the IP (interrupt pending) register */
sifive_uart_ip(SiFiveUARTState * s)38 static uint64_t sifive_uart_ip(SiFiveUARTState *s)
39 {
40 uint64_t ret = 0;
41
42 uint64_t txcnt = SIFIVE_UART_GET_TXCNT(s->txctrl);
43 uint64_t rxcnt = SIFIVE_UART_GET_RXCNT(s->rxctrl);
44
45 if (txcnt != 0) {
46 ret |= SIFIVE_UART_IP_TXWM;
47 }
48 if (s->rx_fifo_len > rxcnt) {
49 ret |= SIFIVE_UART_IP_RXWM;
50 }
51
52 return ret;
53 }
54
sifive_uart_update_irq(SiFiveUARTState * s)55 static void sifive_uart_update_irq(SiFiveUARTState *s)
56 {
57 int cond = 0;
58 if ((s->ie & SIFIVE_UART_IE_TXWM) ||
59 ((s->ie & SIFIVE_UART_IE_RXWM) && s->rx_fifo_len)) {
60 cond = 1;
61 }
62 if (cond) {
63 qemu_irq_raise(s->irq);
64 } else {
65 qemu_irq_lower(s->irq);
66 }
67 }
68
sifive_uart_xmit(void * do_not_use,GIOCondition cond,void * opaque)69 static gboolean sifive_uart_xmit(void *do_not_use, GIOCondition cond,
70 void *opaque)
71 {
72 SiFiveUARTState *s = opaque;
73 int ret;
74 const uint8_t *characters;
75 uint32_t numptr = 0;
76
77 /* instant drain the fifo when there's no back-end */
78 if (!qemu_chr_fe_backend_connected(&s->chr)) {
79 fifo8_reset(&s->tx_fifo);
80 return G_SOURCE_REMOVE;
81 }
82
83 if (fifo8_is_empty(&s->tx_fifo)) {
84 return G_SOURCE_REMOVE;
85 }
86
87 /* Don't pop the FIFO in case the write fails */
88 characters = fifo8_peek_bufptr(&s->tx_fifo,
89 fifo8_num_used(&s->tx_fifo), &numptr);
90 ret = qemu_chr_fe_write(&s->chr, characters, numptr);
91
92 if (ret >= 0) {
93 /* We wrote the data, actually pop the fifo */
94 fifo8_pop_bufptr(&s->tx_fifo, ret, NULL);
95 }
96
97 if (!fifo8_is_empty(&s->tx_fifo)) {
98 guint r = qemu_chr_fe_add_watch(&s->chr, G_IO_OUT | G_IO_HUP,
99 sifive_uart_xmit, s);
100 if (!r) {
101 fifo8_reset(&s->tx_fifo);
102 return G_SOURCE_REMOVE;
103 }
104 }
105
106 /* Clear the TX Full bit */
107 if (!fifo8_is_full(&s->tx_fifo)) {
108 s->txfifo &= ~SIFIVE_UART_TXFIFO_FULL;
109 }
110
111 sifive_uart_update_irq(s);
112 return G_SOURCE_REMOVE;
113 }
114
sifive_uart_write_tx_fifo(SiFiveUARTState * s,const uint8_t * buf,int size)115 static void sifive_uart_write_tx_fifo(SiFiveUARTState *s, const uint8_t *buf,
116 int size)
117 {
118 uint64_t current_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
119
120 if (size > fifo8_num_free(&s->tx_fifo)) {
121 size = fifo8_num_free(&s->tx_fifo);
122 qemu_log_mask(LOG_GUEST_ERROR, "sifive_uart: TX FIFO overflow");
123 }
124
125 fifo8_push_all(&s->tx_fifo, buf, size);
126
127 if (fifo8_is_full(&s->tx_fifo)) {
128 s->txfifo |= SIFIVE_UART_TXFIFO_FULL;
129 }
130
131 timer_mod(s->fifo_trigger_handle, current_time +
132 TX_INTERRUPT_TRIGGER_DELAY_NS);
133 }
134
135 static uint64_t
sifive_uart_read(void * opaque,hwaddr addr,unsigned int size)136 sifive_uart_read(void *opaque, hwaddr addr, unsigned int size)
137 {
138 SiFiveUARTState *s = opaque;
139 unsigned char r;
140 switch (addr) {
141 case SIFIVE_UART_RXFIFO:
142 if (s->rx_fifo_len) {
143 r = s->rx_fifo[0];
144 memmove(s->rx_fifo, s->rx_fifo + 1, s->rx_fifo_len - 1);
145 s->rx_fifo_len--;
146 qemu_chr_fe_accept_input(&s->chr);
147 sifive_uart_update_irq(s);
148 return r;
149 }
150 return 0x80000000;
151
152 case SIFIVE_UART_TXFIFO:
153 return s->txfifo;
154 case SIFIVE_UART_IE:
155 return s->ie;
156 case SIFIVE_UART_IP:
157 return sifive_uart_ip(s);
158 case SIFIVE_UART_TXCTRL:
159 return s->txctrl;
160 case SIFIVE_UART_RXCTRL:
161 return s->rxctrl;
162 case SIFIVE_UART_DIV:
163 return s->div;
164 }
165
166 qemu_log_mask(LOG_GUEST_ERROR, "%s: bad read: addr=0x%x\n",
167 __func__, (int)addr);
168 return 0;
169 }
170
171 static void
sifive_uart_write(void * opaque,hwaddr addr,uint64_t val64,unsigned int size)172 sifive_uart_write(void *opaque, hwaddr addr,
173 uint64_t val64, unsigned int size)
174 {
175 SiFiveUARTState *s = opaque;
176 uint32_t value = val64;
177 uint8_t ch = value;
178
179 switch (addr) {
180 case SIFIVE_UART_TXFIFO:
181 sifive_uart_write_tx_fifo(s, &ch, 1);
182 return;
183 case SIFIVE_UART_IE:
184 s->ie = val64;
185 sifive_uart_update_irq(s);
186 return;
187 case SIFIVE_UART_TXCTRL:
188 s->txctrl = val64;
189 return;
190 case SIFIVE_UART_RXCTRL:
191 s->rxctrl = val64;
192 return;
193 case SIFIVE_UART_DIV:
194 s->div = val64;
195 return;
196 }
197 qemu_log_mask(LOG_GUEST_ERROR, "%s: bad write: addr=0x%x v=0x%x\n",
198 __func__, (int)addr, (int)value);
199 }
200
fifo_trigger_update(void * opaque)201 static void fifo_trigger_update(void *opaque)
202 {
203 SiFiveUARTState *s = opaque;
204
205 sifive_uart_xmit(NULL, G_IO_OUT, s);
206 }
207
208 static const MemoryRegionOps sifive_uart_ops = {
209 .read = sifive_uart_read,
210 .write = sifive_uart_write,
211 .endianness = DEVICE_NATIVE_ENDIAN,
212 .valid = {
213 .min_access_size = 4,
214 .max_access_size = 4
215 }
216 };
217
sifive_uart_rx(void * opaque,const uint8_t * buf,int size)218 static void sifive_uart_rx(void *opaque, const uint8_t *buf, int size)
219 {
220 SiFiveUARTState *s = opaque;
221
222 /* Got a byte. */
223 if (s->rx_fifo_len >= sizeof(s->rx_fifo)) {
224 printf("WARNING: UART dropped char.\n");
225 return;
226 }
227 s->rx_fifo[s->rx_fifo_len++] = *buf;
228
229 sifive_uart_update_irq(s);
230 }
231
sifive_uart_can_rx(void * opaque)232 static int sifive_uart_can_rx(void *opaque)
233 {
234 SiFiveUARTState *s = opaque;
235
236 return s->rx_fifo_len < sizeof(s->rx_fifo);
237 }
238
sifive_uart_event(void * opaque,QEMUChrEvent event)239 static void sifive_uart_event(void *opaque, QEMUChrEvent event)
240 {
241 }
242
sifive_uart_be_change(void * opaque)243 static int sifive_uart_be_change(void *opaque)
244 {
245 SiFiveUARTState *s = opaque;
246
247 qemu_chr_fe_set_handlers(&s->chr, sifive_uart_can_rx, sifive_uart_rx,
248 sifive_uart_event, sifive_uart_be_change, s,
249 NULL, true);
250
251 return 0;
252 }
253
sifive_uart_reset_enter(Object * obj,ResetType type)254 static void sifive_uart_reset_enter(Object *obj, ResetType type)
255 {
256 SiFiveUARTState *s = SIFIVE_UART(obj);
257
258 s->txfifo = 0;
259 s->ie = 0;
260 s->ip = 0;
261 s->txctrl = 0;
262 s->rxctrl = 0;
263 s->div = 0;
264
265 s->rx_fifo_len = 0;
266
267 memset(s->rx_fifo, 0, SIFIVE_UART_RX_FIFO_SIZE);
268 fifo8_reset(&s->tx_fifo);
269 }
270
271 static const Property sifive_uart_properties[] = {
272 DEFINE_PROP_CHR("chardev", SiFiveUARTState, chr),
273 };
274
sifive_uart_init(Object * obj)275 static void sifive_uart_init(Object *obj)
276 {
277 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
278 SiFiveUARTState *s = SIFIVE_UART(obj);
279
280 memory_region_init_io(&s->mmio, OBJECT(s), &sifive_uart_ops, s,
281 TYPE_SIFIVE_UART, SIFIVE_UART_MAX);
282 sysbus_init_mmio(sbd, &s->mmio);
283 sysbus_init_irq(sbd, &s->irq);
284 }
285
sifive_uart_realize(DeviceState * dev,Error ** errp)286 static void sifive_uart_realize(DeviceState *dev, Error **errp)
287 {
288 SiFiveUARTState *s = SIFIVE_UART(dev);
289
290 fifo8_create(&s->tx_fifo, SIFIVE_UART_TX_FIFO_SIZE);
291
292 s->fifo_trigger_handle = timer_new_ns(QEMU_CLOCK_VIRTUAL,
293 fifo_trigger_update, s);
294
295 if (qemu_chr_fe_backend_connected(&s->chr)) {
296 qemu_chr_fe_set_handlers(&s->chr, sifive_uart_can_rx, sifive_uart_rx,
297 sifive_uart_event, sifive_uart_be_change, s,
298 NULL, true);
299 }
300
301 }
302
sifive_uart_unrealize(DeviceState * dev)303 static void sifive_uart_unrealize(DeviceState *dev)
304 {
305 SiFiveUARTState *s = SIFIVE_UART(dev);
306
307 fifo8_destroy(&s->tx_fifo);
308 }
309
sifive_uart_reset_hold(Object * obj,ResetType type)310 static void sifive_uart_reset_hold(Object *obj, ResetType type)
311 {
312 SiFiveUARTState *s = SIFIVE_UART(obj);
313 qemu_irq_lower(s->irq);
314 }
315
316 static const VMStateDescription vmstate_sifive_uart = {
317 .name = TYPE_SIFIVE_UART,
318 .version_id = 2,
319 .minimum_version_id = 2,
320 .fields = (const VMStateField[]) {
321 VMSTATE_UINT8_ARRAY(rx_fifo, SiFiveUARTState,
322 SIFIVE_UART_RX_FIFO_SIZE),
323 VMSTATE_UINT8(rx_fifo_len, SiFiveUARTState),
324 VMSTATE_UINT32(ie, SiFiveUARTState),
325 VMSTATE_UINT32(ip, SiFiveUARTState),
326 VMSTATE_UINT32(txctrl, SiFiveUARTState),
327 VMSTATE_UINT32(rxctrl, SiFiveUARTState),
328 VMSTATE_UINT32(div, SiFiveUARTState),
329 VMSTATE_UINT32(txfifo, SiFiveUARTState),
330 VMSTATE_FIFO8(tx_fifo, SiFiveUARTState),
331 VMSTATE_TIMER_PTR(fifo_trigger_handle, SiFiveUARTState),
332 VMSTATE_END_OF_LIST()
333 },
334 };
335
336
sifive_uart_class_init(ObjectClass * oc,const void * data)337 static void sifive_uart_class_init(ObjectClass *oc, const void *data)
338 {
339 DeviceClass *dc = DEVICE_CLASS(oc);
340 ResettableClass *rc = RESETTABLE_CLASS(oc);
341
342 dc->realize = sifive_uart_realize;
343 dc->unrealize = sifive_uart_unrealize;
344 dc->vmsd = &vmstate_sifive_uart;
345 rc->phases.enter = sifive_uart_reset_enter;
346 rc->phases.hold = sifive_uart_reset_hold;
347 device_class_set_props(dc, sifive_uart_properties);
348 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
349 }
350
351 static const TypeInfo sifive_uart_info = {
352 .name = TYPE_SIFIVE_UART,
353 .parent = TYPE_SYS_BUS_DEVICE,
354 .instance_size = sizeof(SiFiveUARTState),
355 .instance_init = sifive_uart_init,
356 .class_init = sifive_uart_class_init,
357 };
358
sifive_uart_register_types(void)359 static void sifive_uart_register_types(void)
360 {
361 type_register_static(&sifive_uart_info);
362 }
363
type_init(sifive_uart_register_types)364 type_init(sifive_uart_register_types)
365
366 /*
367 * Create UART device.
368 */
369 SiFiveUARTState *sifive_uart_create(MemoryRegion *address_space, hwaddr base,
370 Chardev *chr, qemu_irq irq)
371 {
372 DeviceState *dev;
373 SysBusDevice *s;
374
375 dev = qdev_new("riscv.sifive.uart");
376 s = SYS_BUS_DEVICE(dev);
377 qdev_prop_set_chr(dev, "chardev", chr);
378 sysbus_realize_and_unref(s, &error_fatal);
379 memory_region_add_subregion(address_space, base,
380 sysbus_mmio_get_region(s, 0));
381 sysbus_connect_irq(s, 0, irq);
382
383 return SIFIVE_UART(dev);
384 }
385