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