xref: /openbmc/qemu/include/hw/char/cmsdk-apb-uart.h (revision 0b1183e3)
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 
12 #ifndef CMSDK_APB_UART_H
13 #define CMSDK_APB_UART_H
14 
15 #include "hw/sysbus.h"
16 #include "chardev/char-fe.h"
17 
18 #define TYPE_CMSDK_APB_UART "cmsdk-apb-uart"
19 #define CMSDK_APB_UART(obj) OBJECT_CHECK(CMSDKAPBUART, (obj), \
20                                          TYPE_CMSDK_APB_UART)
21 
22 typedef struct {
23     /*< private >*/
24     SysBusDevice parent_obj;
25 
26     /*< public >*/
27     MemoryRegion iomem;
28     CharBackend chr;
29     qemu_irq txint;
30     qemu_irq rxint;
31     qemu_irq txovrint;
32     qemu_irq rxovrint;
33     qemu_irq uartint;
34     guint watch_tag;
35     uint32_t pclk_frq;
36 
37     uint32_t state;
38     uint32_t ctrl;
39     uint32_t intstatus;
40     uint32_t bauddiv;
41     /* This UART has no FIFO, only a 1-character buffer for each of Tx and Rx */
42     uint8_t txbuf;
43     uint8_t rxbuf;
44 } CMSDKAPBUART;
45 
46 /**
47  * cmsdk_apb_uart_create - convenience function to create TYPE_CMSDK_APB_UART
48  * @addr: location in system memory to map registers
49  * @chr: Chardev backend to connect UART to, or NULL if no backend
50  * @pclk_frq: frequency in Hz of the PCLK clock (used for calculating baud rate)
51  */
52 static inline DeviceState *cmsdk_apb_uart_create(hwaddr addr,
53                                                  qemu_irq txint,
54                                                  qemu_irq rxint,
55                                                  qemu_irq txovrint,
56                                                  qemu_irq rxovrint,
57                                                  qemu_irq uartint,
58                                                  Chardev *chr,
59                                                  uint32_t pclk_frq)
60 {
61     DeviceState *dev;
62     SysBusDevice *s;
63 
64     dev = qdev_create(NULL, TYPE_CMSDK_APB_UART);
65     s = SYS_BUS_DEVICE(dev);
66     qdev_prop_set_chr(dev, "chardev", chr);
67     qdev_prop_set_uint32(dev, "pclk-frq", pclk_frq);
68     qdev_init_nofail(dev);
69     sysbus_mmio_map(s, 0, addr);
70     sysbus_connect_irq(s, 0, txint);
71     sysbus_connect_irq(s, 1, rxint);
72     sysbus_connect_irq(s, 2, txovrint);
73     sysbus_connect_irq(s, 3, rxovrint);
74     sysbus_connect_irq(s, 4, uartint);
75     return dev;
76 }
77 
78 #endif
79