target-xtensa: add target to the configure script
[qemu.git] / hw / syborg_serial.c
blobc83f82c36e70306024f360dfc063edb9e456d242
1 /*
2 * Syborg serial port
4 * Copyright (c) 2008 CodeSourcery
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"
27 #include "syborg.h"
29 //#define DEBUG_SYBORG_SERIAL
31 #ifdef DEBUG_SYBORG_SERIAL
32 #define DPRINTF(fmt, ...) \
33 do { printf("syborg_serial: " fmt , ##args); } while (0)
34 #define BADF(fmt, ...) \
35 do { fprintf(stderr, "syborg_serial: error: " fmt , ## __VA_ARGS__); \
36 exit(1);} while (0)
37 #else
38 #define DPRINTF(fmt, ...) do {} while(0)
39 #define BADF(fmt, ...) \
40 do { fprintf(stderr, "syborg_serial: error: " fmt , ## __VA_ARGS__);} while (0)
41 #endif
43 enum {
44 SERIAL_ID = 0,
45 SERIAL_DATA = 1,
46 SERIAL_FIFO_COUNT = 2,
47 SERIAL_INT_ENABLE = 3,
48 SERIAL_DMA_TX_ADDR = 4,
49 SERIAL_DMA_TX_COUNT = 5, /* triggers dma */
50 SERIAL_DMA_RX_ADDR = 6,
51 SERIAL_DMA_RX_COUNT = 7, /* triggers dma */
52 SERIAL_FIFO_SIZE = 8
55 #define SERIAL_INT_FIFO (1u << 0)
56 #define SERIAL_INT_DMA_TX (1u << 1)
57 #define SERIAL_INT_DMA_RX (1u << 2)
59 typedef struct {
60 SysBusDevice busdev;
61 uint32_t int_enable;
62 uint32_t fifo_size;
63 uint32_t *read_fifo;
64 int read_pos;
65 int read_count;
66 CharDriverState *chr;
67 qemu_irq irq;
68 uint32_t dma_tx_ptr;
69 uint32_t dma_rx_ptr;
70 uint32_t dma_rx_size;
71 } SyborgSerialState;
73 static void syborg_serial_update(SyborgSerialState *s)
75 int level;
76 level = 0;
77 if ((s->int_enable & SERIAL_INT_FIFO) && s->read_count)
78 level = 1;
79 if (s->int_enable & SERIAL_INT_DMA_TX)
80 level = 1;
81 if ((s->int_enable & SERIAL_INT_DMA_RX) && s->dma_rx_size == 0)
82 level = 1;
84 qemu_set_irq(s->irq, level);
87 static uint32_t fifo_pop(SyborgSerialState *s)
89 const uint32_t c = s->read_fifo[s->read_pos];
90 s->read_count--;
91 s->read_pos++;
92 if (s->read_pos == s->fifo_size)
93 s->read_pos = 0;
95 DPRINTF("FIFO pop %x (%d)\n", c, s->read_count);
96 return c;
99 static void fifo_push(SyborgSerialState *s, uint32_t new_value)
101 int slot;
103 DPRINTF("FIFO push %x (%d)\n", new_value, s->read_count);
104 slot = s->read_pos + s->read_count;
105 if (slot >= s->fifo_size)
106 slot -= s->fifo_size;
107 s->read_fifo[slot] = new_value;
108 s->read_count++;
111 static void do_dma_tx(SyborgSerialState *s, uint32_t count)
113 unsigned char ch;
115 if (count == 0)
116 return;
118 if (s->chr != NULL) {
119 /* optimize later. Now, 1 byte per iteration */
120 while (count--) {
121 cpu_physical_memory_read(s->dma_tx_ptr, &ch, 1);
122 qemu_chr_fe_write(s->chr, &ch, 1);
123 s->dma_tx_ptr++;
125 } else {
126 s->dma_tx_ptr += count;
128 /* QEMU char backends do not have a nonblocking mode, so we transmit all
129 the data immediately and the interrupt status will be unchanged. */
132 /* Initiate RX DMA, and transfer data from the FIFO. */
133 static void dma_rx_start(SyborgSerialState *s, uint32_t len)
135 uint32_t dest;
136 unsigned char ch;
138 dest = s->dma_rx_ptr;
139 if (s->read_count < len) {
140 s->dma_rx_size = len - s->read_count;
141 len = s->read_count;
142 } else {
143 s->dma_rx_size = 0;
146 while (len--) {
147 ch = fifo_pop(s);
148 cpu_physical_memory_write(dest, &ch, 1);
149 dest++;
151 s->dma_rx_ptr = dest;
152 syborg_serial_update(s);
155 static uint32_t syborg_serial_read(void *opaque, target_phys_addr_t offset)
157 SyborgSerialState *s = (SyborgSerialState *)opaque;
158 uint32_t c;
160 offset &= 0xfff;
161 DPRINTF("read 0x%x\n", (int)offset);
162 switch(offset >> 2) {
163 case SERIAL_ID:
164 return SYBORG_ID_SERIAL;
165 case SERIAL_DATA:
166 if (s->read_count > 0)
167 c = fifo_pop(s);
168 else
169 c = -1;
170 syborg_serial_update(s);
171 return c;
172 case SERIAL_FIFO_COUNT:
173 return s->read_count;
174 case SERIAL_INT_ENABLE:
175 return s->int_enable;
176 case SERIAL_DMA_TX_ADDR:
177 return s->dma_tx_ptr;
178 case SERIAL_DMA_TX_COUNT:
179 return 0;
180 case SERIAL_DMA_RX_ADDR:
181 return s->dma_rx_ptr;
182 case SERIAL_DMA_RX_COUNT:
183 return s->dma_rx_size;
184 case SERIAL_FIFO_SIZE:
185 return s->fifo_size;
187 default:
188 cpu_abort(cpu_single_env, "syborg_serial_read: Bad offset %x\n",
189 (int)offset);
190 return 0;
194 static void syborg_serial_write(void *opaque, target_phys_addr_t offset,
195 uint32_t value)
197 SyborgSerialState *s = (SyborgSerialState *)opaque;
198 unsigned char ch;
200 offset &= 0xfff;
201 DPRINTF("Write 0x%x=0x%x\n", (int)offset, value);
202 switch (offset >> 2) {
203 case SERIAL_DATA:
204 ch = value;
205 if (s->chr)
206 qemu_chr_fe_write(s->chr, &ch, 1);
207 break;
208 case SERIAL_INT_ENABLE:
209 s->int_enable = value;
210 syborg_serial_update(s);
211 break;
212 case SERIAL_DMA_TX_ADDR:
213 s->dma_tx_ptr = value;
214 break;
215 case SERIAL_DMA_TX_COUNT:
216 do_dma_tx(s, value);
217 break;
218 case SERIAL_DMA_RX_ADDR:
219 /* For safety, writes to this register cancel any pending DMA. */
220 s->dma_rx_size = 0;
221 s->dma_rx_ptr = value;
222 break;
223 case SERIAL_DMA_RX_COUNT:
224 dma_rx_start(s, value);
225 break;
226 default:
227 cpu_abort(cpu_single_env, "syborg_serial_write: Bad offset %x\n",
228 (int)offset);
229 break;
233 static int syborg_serial_can_receive(void *opaque)
235 SyborgSerialState *s = (SyborgSerialState *)opaque;
237 if (s->dma_rx_size)
238 return s->dma_rx_size;
239 return s->fifo_size - s->read_count;
242 static void syborg_serial_receive(void *opaque, const uint8_t *buf, int size)
244 SyborgSerialState *s = (SyborgSerialState *)opaque;
246 if (s->dma_rx_size) {
247 /* Place it in the DMA buffer. */
248 cpu_physical_memory_write(s->dma_rx_ptr, buf, size);
249 s->dma_rx_size -= size;
250 s->dma_rx_ptr += size;
251 } else {
252 while (size--)
253 fifo_push(s, *buf);
256 syborg_serial_update(s);
259 static void syborg_serial_event(void *opaque, int event)
261 /* TODO: Report BREAK events? */
264 static CPUReadMemoryFunc * const syborg_serial_readfn[] = {
265 syborg_serial_read,
266 syborg_serial_read,
267 syborg_serial_read
270 static CPUWriteMemoryFunc * const syborg_serial_writefn[] = {
271 syborg_serial_write,
272 syborg_serial_write,
273 syborg_serial_write
276 static const VMStateDescription vmstate_syborg_serial = {
277 .name = "syborg_serial",
278 .version_id = 1,
279 .minimum_version_id = 1,
280 .minimum_version_id_old = 1,
281 .fields = (VMStateField[]) {
282 VMSTATE_UINT32_EQUAL(fifo_size, SyborgSerialState),
283 VMSTATE_UINT32(int_enable, SyborgSerialState),
284 VMSTATE_INT32(read_pos, SyborgSerialState),
285 VMSTATE_INT32(read_count, SyborgSerialState),
286 VMSTATE_UINT32(dma_tx_ptr, SyborgSerialState),
287 VMSTATE_UINT32(dma_rx_ptr, SyborgSerialState),
288 VMSTATE_UINT32(dma_rx_size, SyborgSerialState),
289 VMSTATE_VARRAY_UINT32(read_fifo, SyborgSerialState, fifo_size, 1,
290 vmstate_info_uint32, uint32),
291 VMSTATE_END_OF_LIST()
295 static int syborg_serial_init(SysBusDevice *dev)
297 SyborgSerialState *s = FROM_SYSBUS(SyborgSerialState, dev);
298 int iomemtype;
300 sysbus_init_irq(dev, &s->irq);
301 iomemtype = cpu_register_io_memory(syborg_serial_readfn,
302 syborg_serial_writefn, s,
303 DEVICE_NATIVE_ENDIAN);
304 sysbus_init_mmio(dev, 0x1000, iomemtype);
305 s->chr = qdev_init_chardev(&dev->qdev);
306 if (s->chr) {
307 qemu_chr_add_handlers(s->chr, syborg_serial_can_receive,
308 syborg_serial_receive, syborg_serial_event, s);
310 if (s->fifo_size <= 0) {
311 fprintf(stderr, "syborg_serial: fifo too small\n");
312 s->fifo_size = 16;
314 s->read_fifo = g_malloc0(s->fifo_size * sizeof(s->read_fifo[0]));
316 return 0;
319 static SysBusDeviceInfo syborg_serial_info = {
320 .init = syborg_serial_init,
321 .qdev.name = "syborg,serial",
322 .qdev.size = sizeof(SyborgSerialState),
323 .qdev.vmsd = &vmstate_syborg_serial,
324 .qdev.props = (Property[]) {
325 DEFINE_PROP_UINT32("fifo-size", SyborgSerialState, fifo_size, 16),
326 DEFINE_PROP_END_OF_LIST(),
330 static void syborg_serial_register_devices(void)
332 sysbus_register_withprop(&syborg_serial_info);
335 device_init(syborg_serial_register_devices)