]>
Commit | Line | Data |
---|---|---|
8b1e1320 FC |
1 | /* |
2 | * QEMU GRLIB APB UART Emulator | |
3 | * | |
4 | * Copyright (c) 2010-2011 AdaCore | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | ||
25 | #include "sysbus.h" | |
26 | #include "qemu-char.h" | |
27 | ||
28 | #include "trace.h" | |
29 | ||
30 | #define UART_REG_SIZE 20 /* Size of memory mapped registers */ | |
31 | ||
32 | /* UART status register fields */ | |
33 | #define UART_DATA_READY (1 << 0) | |
34 | #define UART_TRANSMIT_SHIFT_EMPTY (1 << 1) | |
35 | #define UART_TRANSMIT_FIFO_EMPTY (1 << 2) | |
36 | #define UART_BREAK_RECEIVED (1 << 3) | |
37 | #define UART_OVERRUN (1 << 4) | |
38 | #define UART_PARITY_ERROR (1 << 5) | |
39 | #define UART_FRAMING_ERROR (1 << 6) | |
40 | #define UART_TRANSMIT_FIFO_HALF (1 << 7) | |
41 | #define UART_RECEIVE_FIFO_HALF (1 << 8) | |
42 | #define UART_TRANSMIT_FIFO_FULL (1 << 9) | |
43 | #define UART_RECEIVE_FIFO_FULL (1 << 10) | |
44 | ||
45 | /* UART control register fields */ | |
46 | #define UART_RECEIVE_ENABLE (1 << 0) | |
47 | #define UART_TRANSMIT_ENABLE (1 << 1) | |
48 | #define UART_RECEIVE_INTERRUPT (1 << 2) | |
49 | #define UART_TRANSMIT_INTERRUPT (1 << 3) | |
50 | #define UART_PARITY_SELECT (1 << 4) | |
51 | #define UART_PARITY_ENABLE (1 << 5) | |
52 | #define UART_FLOW_CONTROL (1 << 6) | |
53 | #define UART_LOOPBACK (1 << 7) | |
54 | #define UART_EXTERNAL_CLOCK (1 << 8) | |
55 | #define UART_RECEIVE_FIFO_INTERRUPT (1 << 9) | |
56 | #define UART_TRANSMIT_FIFO_INTERRUPT (1 << 10) | |
57 | #define UART_FIFO_DEBUG_MODE (1 << 11) | |
58 | #define UART_OUTPUT_ENABLE (1 << 12) | |
59 | #define UART_FIFO_AVAILABLE (1 << 31) | |
60 | ||
61 | /* Memory mapped register offsets */ | |
62 | #define DATA_OFFSET 0x00 | |
63 | #define STATUS_OFFSET 0x04 | |
64 | #define CONTROL_OFFSET 0x08 | |
65 | #define SCALER_OFFSET 0x0C /* not supported */ | |
66 | #define FIFO_DEBUG_OFFSET 0x10 /* not supported */ | |
67 | ||
68 | typedef struct UART { | |
69 | SysBusDevice busdev; | |
6281f7d1 | 70 | MemoryRegion iomem; |
8b1e1320 FC |
71 | qemu_irq irq; |
72 | ||
73 | CharDriverState *chr; | |
74 | ||
75 | /* registers */ | |
76 | uint32_t receive; | |
77 | uint32_t status; | |
78 | uint32_t control; | |
79 | } UART; | |
80 | ||
81 | static int grlib_apbuart_can_receive(void *opaque) | |
82 | { | |
83 | UART *uart = opaque; | |
84 | ||
85 | return !!(uart->status & UART_DATA_READY); | |
86 | } | |
87 | ||
88 | static void grlib_apbuart_receive(void *opaque, const uint8_t *buf, int size) | |
89 | { | |
90 | UART *uart = opaque; | |
91 | ||
92 | uart->receive = *buf; | |
93 | uart->status |= UART_DATA_READY; | |
94 | ||
95 | if (uart->control & UART_RECEIVE_INTERRUPT) { | |
96 | qemu_irq_pulse(uart->irq); | |
97 | } | |
98 | } | |
99 | ||
100 | static void grlib_apbuart_event(void *opaque, int event) | |
101 | { | |
102 | trace_grlib_apbuart_event(event); | |
103 | } | |
104 | ||
105 | static void | |
6281f7d1 AK |
106 | grlib_apbuart_write(void *opaque, target_phys_addr_t addr, |
107 | uint64_t value, unsigned size) | |
8b1e1320 FC |
108 | { |
109 | UART *uart = opaque; | |
110 | unsigned char c = 0; | |
111 | ||
112 | addr &= 0xff; | |
113 | ||
114 | /* Unit registers */ | |
115 | switch (addr) { | |
116 | case DATA_OFFSET: | |
117 | c = value & 0xFF; | |
2cc6e0a1 | 118 | qemu_chr_fe_write(uart->chr, &c, 1); |
8b1e1320 FC |
119 | return; |
120 | ||
121 | case STATUS_OFFSET: | |
122 | /* Read Only */ | |
123 | return; | |
124 | ||
125 | case CONTROL_OFFSET: | |
126 | /* Not supported */ | |
127 | return; | |
128 | ||
129 | case SCALER_OFFSET: | |
130 | /* Not supported */ | |
131 | return; | |
132 | ||
133 | default: | |
134 | break; | |
135 | } | |
136 | ||
b4548fcc | 137 | trace_grlib_apbuart_writel_unknown(addr, value); |
8b1e1320 FC |
138 | } |
139 | ||
6281f7d1 AK |
140 | static bool grlib_apbuart_accepts(void *opaque, target_phys_addr_t addr, |
141 | unsigned size, bool is_write) | |
142 | { | |
143 | return is_write && size == 4; | |
144 | } | |
8b1e1320 | 145 | |
6281f7d1 AK |
146 | static const MemoryRegionOps grlib_apbuart_ops = { |
147 | .write = grlib_apbuart_write, | |
148 | .valid.accepts = grlib_apbuart_accepts, | |
149 | .endianness = DEVICE_NATIVE_ENDIAN, | |
8b1e1320 FC |
150 | }; |
151 | ||
152 | static int grlib_apbuart_init(SysBusDevice *dev) | |
153 | { | |
154 | UART *uart = FROM_SYSBUS(typeof(*uart), dev); | |
8b1e1320 FC |
155 | |
156 | qemu_chr_add_handlers(uart->chr, | |
157 | grlib_apbuart_can_receive, | |
158 | grlib_apbuart_receive, | |
159 | grlib_apbuart_event, | |
160 | uart); | |
161 | ||
162 | sysbus_init_irq(dev, &uart->irq); | |
163 | ||
6281f7d1 AK |
164 | memory_region_init_io(&uart->iomem, &grlib_apbuart_ops, uart, |
165 | "uart", UART_REG_SIZE); | |
8b1e1320 | 166 | |
750ecd44 | 167 | sysbus_init_mmio(dev, &uart->iomem); |
8b1e1320 FC |
168 | |
169 | return 0; | |
170 | } | |
171 | ||
172 | static SysBusDeviceInfo grlib_gptimer_info = { | |
173 | .init = grlib_apbuart_init, | |
174 | .qdev.name = "grlib,apbuart", | |
175 | .qdev.size = sizeof(UART), | |
176 | .qdev.props = (Property[]) { | |
177 | DEFINE_PROP_CHR("chrdev", UART, chr), | |
178 | DEFINE_PROP_END_OF_LIST() | |
179 | } | |
180 | }; | |
181 | ||
182 | static void grlib_gptimer_register(void) | |
183 | { | |
184 | sysbus_register_withprop(&grlib_gptimer_info); | |
185 | } | |
186 | ||
187 | device_init(grlib_gptimer_register) |