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