m25p80: Add support for n25q512ax3
[qemu.git] / hw / char / xilinx_uartlite.c
blobae4ccd00c70e5bd194ff2e55907d3ec23b0a2fee
1 /*
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
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include "qemu/log.h"
27 #include "hw/irq.h"
28 #include "hw/qdev-properties.h"
29 #include "hw/sysbus.h"
30 #include "qemu/module.h"
31 #include "chardev/char-fe.h"
33 #define DUART(x)
35 #define R_RX 0
36 #define R_TX 1
37 #define R_STATUS 2
38 #define R_CTRL 3
39 #define R_MAX 4
41 #define STATUS_RXVALID 0x01
42 #define STATUS_RXFULL 0x02
43 #define STATUS_TXEMPTY 0x04
44 #define STATUS_TXFULL 0x08
45 #define STATUS_IE 0x10
46 #define STATUS_OVERRUN 0x20
47 #define STATUS_FRAME 0x40
48 #define STATUS_PARITY 0x80
50 #define CONTROL_RST_TX 0x01
51 #define CONTROL_RST_RX 0x02
52 #define CONTROL_IE 0x10
54 #define TYPE_XILINX_UARTLITE "xlnx.xps-uartlite"
55 #define XILINX_UARTLITE(obj) \
56 OBJECT_CHECK(XilinxUARTLite, (obj), TYPE_XILINX_UARTLITE)
58 typedef struct XilinxUARTLite {
59 SysBusDevice parent_obj;
61 MemoryRegion mmio;
62 CharBackend chr;
63 qemu_irq irq;
65 uint8_t rx_fifo[8];
66 unsigned int rx_fifo_pos;
67 unsigned int rx_fifo_len;
69 uint32_t regs[R_MAX];
70 } XilinxUARTLite;
72 static void uart_update_irq(XilinxUARTLite *s)
74 unsigned int irq;
76 if (s->rx_fifo_len)
77 s->regs[R_STATUS] |= STATUS_IE;
79 irq = (s->regs[R_STATUS] & STATUS_IE) && (s->regs[R_CTRL] & CONTROL_IE);
80 qemu_set_irq(s->irq, irq);
83 static void uart_update_status(XilinxUARTLite *s)
85 uint32_t r;
87 r = s->regs[R_STATUS];
88 r &= ~7;
89 r |= 1 << 2; /* Tx fifo is always empty. We are fast :) */
90 r |= (s->rx_fifo_len == sizeof (s->rx_fifo)) << 1;
91 r |= (!!s->rx_fifo_len);
92 s->regs[R_STATUS] = r;
95 static void xilinx_uartlite_reset(DeviceState *dev)
97 uart_update_status(XILINX_UARTLITE(dev));
100 static uint64_t
101 uart_read(void *opaque, hwaddr addr, unsigned int size)
103 XilinxUARTLite *s = opaque;
104 uint32_t r = 0;
105 addr >>= 2;
106 switch (addr)
108 case R_RX:
109 r = s->rx_fifo[(s->rx_fifo_pos - s->rx_fifo_len) & 7];
110 if (s->rx_fifo_len)
111 s->rx_fifo_len--;
112 uart_update_status(s);
113 uart_update_irq(s);
114 qemu_chr_fe_accept_input(&s->chr);
115 break;
117 default:
118 if (addr < ARRAY_SIZE(s->regs))
119 r = s->regs[addr];
120 DUART(qemu_log("%s addr=%x v=%x\n", __func__, addr, r));
121 break;
123 return r;
126 static void
127 uart_write(void *opaque, hwaddr addr,
128 uint64_t val64, unsigned int size)
130 XilinxUARTLite *s = opaque;
131 uint32_t value = val64;
132 unsigned char ch = value;
134 addr >>= 2;
135 switch (addr)
137 case R_STATUS:
138 qemu_log_mask(LOG_GUEST_ERROR, "%s: write to UART STATUS\n",
139 __func__);
140 break;
142 case R_CTRL:
143 if (value & CONTROL_RST_RX) {
144 s->rx_fifo_pos = 0;
145 s->rx_fifo_len = 0;
147 s->regs[addr] = value;
148 break;
150 case R_TX:
151 /* XXX this blocks entire thread. Rewrite to use
152 * qemu_chr_fe_write and background I/O callbacks */
153 qemu_chr_fe_write_all(&s->chr, &ch, 1);
154 s->regs[addr] = value;
156 /* hax. */
157 s->regs[R_STATUS] |= STATUS_IE;
158 break;
160 default:
161 DUART(printf("%s addr=%x v=%x\n", __func__, addr, value));
162 if (addr < ARRAY_SIZE(s->regs))
163 s->regs[addr] = value;
164 break;
166 uart_update_status(s);
167 uart_update_irq(s);
170 static const MemoryRegionOps uart_ops = {
171 .read = uart_read,
172 .write = uart_write,
173 .endianness = DEVICE_NATIVE_ENDIAN,
174 .valid = {
175 .min_access_size = 1,
176 .max_access_size = 4
180 static Property xilinx_uartlite_properties[] = {
181 DEFINE_PROP_CHR("chardev", XilinxUARTLite, chr),
182 DEFINE_PROP_END_OF_LIST(),
185 static void uart_rx(void *opaque, const uint8_t *buf, int size)
187 XilinxUARTLite *s = opaque;
189 /* Got a byte. */
190 if (s->rx_fifo_len >= 8) {
191 printf("WARNING: UART dropped char.\n");
192 return;
194 s->rx_fifo[s->rx_fifo_pos] = *buf;
195 s->rx_fifo_pos++;
196 s->rx_fifo_pos &= 0x7;
197 s->rx_fifo_len++;
199 uart_update_status(s);
200 uart_update_irq(s);
203 static int uart_can_rx(void *opaque)
205 XilinxUARTLite *s = opaque;
207 return s->rx_fifo_len < sizeof(s->rx_fifo);
210 static void uart_event(void *opaque, QEMUChrEvent event)
215 static void xilinx_uartlite_realize(DeviceState *dev, Error **errp)
217 XilinxUARTLite *s = XILINX_UARTLITE(dev);
219 qemu_chr_fe_set_handlers(&s->chr, uart_can_rx, uart_rx,
220 uart_event, NULL, s, NULL, true);
223 static void xilinx_uartlite_init(Object *obj)
225 XilinxUARTLite *s = XILINX_UARTLITE(obj);
227 sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->irq);
229 memory_region_init_io(&s->mmio, obj, &uart_ops, s,
230 "xlnx.xps-uartlite", R_MAX * 4);
231 sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
234 static void xilinx_uartlite_class_init(ObjectClass *klass, void *data)
236 DeviceClass *dc = DEVICE_CLASS(klass);
238 dc->reset = xilinx_uartlite_reset;
239 dc->realize = xilinx_uartlite_realize;
240 device_class_set_props(dc, xilinx_uartlite_properties);
243 static const TypeInfo xilinx_uartlite_info = {
244 .name = TYPE_XILINX_UARTLITE,
245 .parent = TYPE_SYS_BUS_DEVICE,
246 .instance_size = sizeof(XilinxUARTLite),
247 .instance_init = xilinx_uartlite_init,
248 .class_init = xilinx_uartlite_class_init,
251 static void xilinx_uart_register_types(void)
253 type_register_static(&xilinx_uartlite_info);
256 type_init(xilinx_uart_register_types)