2 * QEMU model of Xilinx uartlite.
4 * Copyright (c) 2009 Edgar E. Iglesias.
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
26 #include "qemu-char.h"
36 #define STATUS_RXVALID 0x01
37 #define STATUS_RXFULL 0x02
38 #define STATUS_TXEMPTY 0x04
39 #define STATUS_TXFULL 0x08
40 #define STATUS_IE 0x10
41 #define STATUS_OVERRUN 0x20
42 #define STATUS_FRAME 0x40
43 #define STATUS_PARITY 0x80
45 #define CONTROL_RST_TX 0x01
46 #define CONTROL_RST_RX 0x02
47 #define CONTROL_IE 0x10
57 unsigned int rx_fifo_pos
;
58 unsigned int rx_fifo_len
;
63 static void uart_update_irq(struct xlx_uartlite
*s
)
68 s
->regs
[R_STATUS
] |= STATUS_IE
;
70 irq
= (s
->regs
[R_STATUS
] & STATUS_IE
) && (s
->regs
[R_CTRL
] & CONTROL_IE
);
71 qemu_set_irq(s
->irq
, irq
);
74 static void uart_update_status(struct xlx_uartlite
*s
)
78 r
= s
->regs
[R_STATUS
];
80 r
|= 1 << 2; /* Tx fifo is always empty. We are fast :) */
81 r
|= (s
->rx_fifo_len
== sizeof (s
->rx_fifo
)) << 1;
82 r
|= (!!s
->rx_fifo_len
);
83 s
->regs
[R_STATUS
] = r
;
87 uart_read(void *opaque
, target_phys_addr_t addr
, unsigned int size
)
89 struct xlx_uartlite
*s
= opaque
;
95 r
= s
->rx_fifo
[(s
->rx_fifo_pos
- s
->rx_fifo_len
) & 7];
98 uart_update_status(s
);
103 if (addr
< ARRAY_SIZE(s
->regs
))
105 DUART(qemu_log("%s addr=%x v=%x\n", __func__
, addr
, r
));
112 uart_write(void *opaque
, target_phys_addr_t addr
,
113 uint64_t val64
, unsigned int size
)
115 struct xlx_uartlite
*s
= opaque
;
116 uint32_t value
= val64
;
117 unsigned char ch
= value
;
123 hw_error("write to UART STATUS?\n");
127 if (value
& CONTROL_RST_RX
) {
131 s
->regs
[addr
] = value
;
136 qemu_chr_fe_write(s
->chr
, &ch
, 1);
138 s
->regs
[addr
] = value
;
141 s
->regs
[R_STATUS
] |= STATUS_IE
;
145 DUART(printf("%s addr=%x v=%x\n", __func__
, addr
, value
));
146 if (addr
< ARRAY_SIZE(s
->regs
))
147 s
->regs
[addr
] = value
;
150 uart_update_status(s
);
154 static const MemoryRegionOps uart_ops
= {
157 .endianness
= DEVICE_NATIVE_ENDIAN
,
159 .min_access_size
= 1,
164 static void uart_rx(void *opaque
, const uint8_t *buf
, int size
)
166 struct xlx_uartlite
*s
= opaque
;
169 if (s
->rx_fifo_len
>= 8) {
170 printf("WARNING: UART dropped char.\n");
173 s
->rx_fifo
[s
->rx_fifo_pos
] = *buf
;
175 s
->rx_fifo_pos
&= 0x7;
178 uart_update_status(s
);
182 static int uart_can_rx(void *opaque
)
184 struct xlx_uartlite
*s
= opaque
;
187 r
= s
->rx_fifo_len
< sizeof(s
->rx_fifo
);
189 printf("cannot receive!\n");
193 static void uart_event(void *opaque
, int event
)
198 static int xilinx_uartlite_init(SysBusDevice
*dev
)
200 struct xlx_uartlite
*s
= FROM_SYSBUS(typeof (*s
), dev
);
202 sysbus_init_irq(dev
, &s
->irq
);
204 uart_update_status(s
);
205 memory_region_init_io(&s
->mmio
, &uart_ops
, s
, "xlnx.xps-uartlite",
207 sysbus_init_mmio(dev
, &s
->mmio
);
209 s
->chr
= qemu_char_get_next_serial();
211 qemu_chr_add_handlers(s
->chr
, uart_can_rx
, uart_rx
, uart_event
, s
);
215 static void xilinx_uartlite_class_init(ObjectClass
*klass
, void *data
)
217 SysBusDeviceClass
*sdc
= SYS_BUS_DEVICE_CLASS(klass
);
219 sdc
->init
= xilinx_uartlite_init
;
222 static TypeInfo xilinx_uartlite_info
= {
223 .name
= "xlnx.xps-uartlite",
224 .parent
= TYPE_SYS_BUS_DEVICE
,
225 .instance_size
= sizeof (struct xlx_uartlite
),
226 .class_init
= xilinx_uartlite_class_init
,
229 static void xilinx_uart_register_types(void)
231 type_register_static(&xilinx_uartlite_info
);
234 type_init(xilinx_uart_register_types
)