scsi: change "removable" field to host many features
[qemu-kvm.git] / hw / grlib_apbuart.c
blob73fc9894db336ea15f8e00d195adcb3e61671274
1 /*
2 * QEMU GRLIB APB UART Emulator
4 * Copyright (c) 2010-2011 AdaCore
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include "sysbus.h"
26 #include "qemu-char.h"
28 #include "trace.h"
30 #define UART_REG_SIZE 20 /* Size of memory mapped registers */
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)
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)
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 */
68 #define FIFO_LENGTH 1024
70 typedef struct UART {
71 SysBusDevice busdev;
72 MemoryRegion iomem;
73 qemu_irq irq;
75 CharDriverState *chr;
77 /* registers */
78 uint32_t receive;
79 uint32_t status;
80 uint32_t control;
82 /* FIFO */
83 char buffer[FIFO_LENGTH];
84 int len;
85 int current;
86 } UART;
88 static int uart_data_to_read(UART *uart)
90 return uart->current < uart->len;
93 static char uart_pop(UART *uart)
95 char ret;
97 if (uart->len == 0) {
98 uart->status &= ~UART_DATA_READY;
99 return 0;
102 ret = uart->buffer[uart->current++];
104 if (uart->current >= uart->len) {
105 /* Flush */
106 uart->len = 0;
107 uart->current = 0;
110 if (!uart_data_to_read(uart)) {
111 uart->status &= ~UART_DATA_READY;
114 return ret;
117 static void uart_add_to_fifo(UART *uart,
118 const uint8_t *buffer,
119 int length)
121 if (uart->len + length > FIFO_LENGTH) {
122 abort();
124 memcpy(uart->buffer + uart->len, buffer, length);
125 uart->len += length;
128 static int grlib_apbuart_can_receive(void *opaque)
130 UART *uart = opaque;
132 return FIFO_LENGTH - uart->len;
135 static void grlib_apbuart_receive(void *opaque, const uint8_t *buf, int size)
137 UART *uart = opaque;
139 uart_add_to_fifo(uart, buf, size);
141 uart->status |= UART_DATA_READY;
143 if (uart->control & UART_RECEIVE_INTERRUPT) {
144 qemu_irq_pulse(uart->irq);
148 static void grlib_apbuart_event(void *opaque, int event)
150 trace_grlib_apbuart_event(event);
154 static uint64_t grlib_apbuart_read(void *opaque, target_phys_addr_t addr,
155 unsigned size)
157 UART *uart = opaque;
159 addr &= 0xff;
161 /* Unit registers */
162 switch (addr) {
163 case DATA_OFFSET:
164 case DATA_OFFSET + 3: /* when only one byte read */
165 return uart_pop(uart);
167 case STATUS_OFFSET:
168 /* Read Only */
169 return uart->status;
171 case CONTROL_OFFSET:
172 return uart->control;
174 case SCALER_OFFSET:
175 /* Not supported */
176 return 0;
178 default:
179 trace_grlib_apbuart_readl_unknown(addr);
180 return 0;
184 static void grlib_apbuart_write(void *opaque, target_phys_addr_t addr,
185 uint64_t value, unsigned size)
187 UART *uart = opaque;
188 unsigned char c = 0;
190 addr &= 0xff;
192 /* Unit registers */
193 switch (addr) {
194 case DATA_OFFSET:
195 case DATA_OFFSET + 3: /* When only one byte write */
196 c = value & 0xFF;
197 qemu_chr_fe_write(uart->chr, &c, 1);
198 return;
200 case STATUS_OFFSET:
201 /* Read Only */
202 return;
204 case CONTROL_OFFSET:
205 uart->control = value;
206 return;
208 case SCALER_OFFSET:
209 /* Not supported */
210 return;
212 default:
213 break;
216 trace_grlib_apbuart_writel_unknown(addr, value);
219 static const MemoryRegionOps grlib_apbuart_ops = {
220 .write = grlib_apbuart_write,
221 .read = grlib_apbuart_read,
222 .endianness = DEVICE_NATIVE_ENDIAN,
225 static int grlib_apbuart_init(SysBusDevice *dev)
227 UART *uart = FROM_SYSBUS(typeof(*uart), dev);
229 qemu_chr_add_handlers(uart->chr,
230 grlib_apbuart_can_receive,
231 grlib_apbuart_receive,
232 grlib_apbuart_event,
233 uart);
235 sysbus_init_irq(dev, &uart->irq);
237 memory_region_init_io(&uart->iomem, &grlib_apbuart_ops, uart,
238 "uart", UART_REG_SIZE);
240 sysbus_init_mmio(dev, &uart->iomem);
242 return 0;
245 static Property grlib_gptimer_properties[] = {
246 DEFINE_PROP_CHR("chrdev", UART, chr),
247 DEFINE_PROP_END_OF_LIST(),
250 static void grlib_gptimer_class_init(ObjectClass *klass, void *data)
252 DeviceClass *dc = DEVICE_CLASS(klass);
253 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
255 k->init = grlib_apbuart_init;
256 dc->props = grlib_gptimer_properties;
259 static TypeInfo grlib_gptimer_info = {
260 .name = "grlib,apbuart",
261 .parent = TYPE_SYS_BUS_DEVICE,
262 .instance_size = sizeof(UART),
263 .class_init = grlib_gptimer_class_init,
266 static void grlib_gptimer_register_types(void)
268 type_register_static(&grlib_gptimer_info);
271 type_init(grlib_gptimer_register_types)