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
26 #include "qemu-char.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__); \
38 #define DPRINTF(fmt, ...) do {} while(0)
39 #define BADF(fmt, ...) \
40 do { fprintf(stderr, "syborg_serial: error: " fmt , ## __VA_ARGS__);} while (0)
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 */
55 #define SERIAL_INT_FIFO (1u << 0)
56 #define SERIAL_INT_DMA_TX (1u << 1)
57 #define SERIAL_INT_DMA_RX (1u << 2)
73 static void syborg_serial_update_handlers(SyborgSerialState
*s
);
75 static void syborg_serial_update(SyborgSerialState
*s
)
79 if ((s
->int_enable
& SERIAL_INT_FIFO
) && s
->read_count
)
81 if (s
->int_enable
& SERIAL_INT_DMA_TX
)
83 if ((s
->int_enable
& SERIAL_INT_DMA_RX
) && s
->dma_rx_size
== 0)
86 qemu_set_irq(s
->irq
, level
);
89 static uint32_t fifo_pop(SyborgSerialState
*s
)
91 const uint32_t c
= s
->read_fifo
[s
->read_pos
];
94 if (s
->read_pos
== s
->fifo_size
)
97 DPRINTF("FIFO pop %x (%d)\n", c
, s
->read_count
);
101 static void fifo_push(SyborgSerialState
*s
, uint32_t new_value
)
105 DPRINTF("FIFO push %x (%d)\n", new_value
, s
->read_count
);
106 slot
= s
->read_pos
+ s
->read_count
;
107 if (slot
>= s
->fifo_size
)
108 slot
-= s
->fifo_size
;
109 s
->read_fifo
[slot
] = new_value
;
113 static void do_dma_tx(SyborgSerialState
*s
, uint32_t count
)
120 if (s
->chr
!= NULL
) {
121 /* optimize later. Now, 1 byte per iteration */
123 cpu_physical_memory_read(s
->dma_tx_ptr
, &ch
, 1);
124 qemu_chr_fe_write(s
->chr
, &ch
, 1);
128 s
->dma_tx_ptr
+= count
;
130 /* QEMU char backends do not have a nonblocking mode, so we transmit all
131 the data immediately and the interrupt status will be unchanged. */
134 /* Initiate RX DMA, and transfer data from the FIFO. */
135 static void dma_rx_start(SyborgSerialState
*s
, uint32_t len
)
140 dest
= s
->dma_rx_ptr
;
141 if (s
->read_count
< len
) {
142 s
->dma_rx_size
= len
- s
->read_count
;
147 syborg_serial_update_handlers(s
);
151 cpu_physical_memory_write(dest
, &ch
, 1);
154 s
->dma_rx_ptr
= dest
;
155 syborg_serial_update(s
);
158 static uint32_t syborg_serial_read(void *opaque
, target_phys_addr_t offset
)
160 SyborgSerialState
*s
= (SyborgSerialState
*)opaque
;
164 DPRINTF("read 0x%x\n", (int)offset
);
165 switch(offset
>> 2) {
167 return SYBORG_ID_SERIAL
;
169 if (s
->read_count
> 0)
173 syborg_serial_update(s
);
175 case SERIAL_FIFO_COUNT
:
176 return s
->read_count
;
177 case SERIAL_INT_ENABLE
:
178 return s
->int_enable
;
179 case SERIAL_DMA_TX_ADDR
:
180 return s
->dma_tx_ptr
;
181 case SERIAL_DMA_TX_COUNT
:
183 case SERIAL_DMA_RX_ADDR
:
184 return s
->dma_rx_ptr
;
185 case SERIAL_DMA_RX_COUNT
:
186 return s
->dma_rx_size
;
187 case SERIAL_FIFO_SIZE
:
191 cpu_abort(cpu_single_env
, "syborg_serial_read: Bad offset %x\n",
197 static void syborg_serial_write(void *opaque
, target_phys_addr_t offset
,
200 SyborgSerialState
*s
= (SyborgSerialState
*)opaque
;
204 DPRINTF("Write 0x%x=0x%x\n", (int)offset
, value
);
205 switch (offset
>> 2) {
209 qemu_chr_fe_write(s
->chr
, &ch
, 1);
211 case SERIAL_INT_ENABLE
:
212 s
->int_enable
= value
;
213 syborg_serial_update(s
);
215 case SERIAL_DMA_TX_ADDR
:
216 s
->dma_tx_ptr
= value
;
218 case SERIAL_DMA_TX_COUNT
:
221 case SERIAL_DMA_RX_ADDR
:
222 /* For safety, writes to this register cancel any pending DMA. */
224 s
->dma_rx_ptr
= value
;
225 syborg_serial_update_handlers(s
);
227 case SERIAL_DMA_RX_COUNT
:
228 dma_rx_start(s
, value
);
231 cpu_abort(cpu_single_env
, "syborg_serial_write: Bad offset %x\n",
237 static int syborg_serial_can_receive(SyborgSerialState
*s
)
240 return s
->dma_rx_size
;
241 return s
->fifo_size
- s
->read_count
;
244 static void syborg_serial_receive(SyborgSerialState
*s
, const uint8_t *buf
, int size
)
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
;
256 syborg_serial_update_handlers(s
);
257 syborg_serial_update(s
);
260 static void syborg_serial_receive_handler(void *opaque
)
262 SyborgSerialState
*s
= opaque
;
266 size
= syborg_serial_can_receive(s
);
267 size
= MIN(size
, sizeof(buf
));
268 size
= qemu_chr_fe_read(s
->chr
, buf
, size
);
270 syborg_serial_receive(s
, buf
, size
);
273 static CPUReadMemoryFunc
* const syborg_serial_readfn
[] = {
279 static CPUWriteMemoryFunc
* const syborg_serial_writefn
[] = {
285 static const VMStateDescription vmstate_syborg_serial
= {
286 .name
= "syborg_serial",
288 .minimum_version_id
= 1,
289 .minimum_version_id_old
= 1,
290 .fields
= (VMStateField
[]) {
291 VMSTATE_UINT32_EQUAL(fifo_size
, SyborgSerialState
),
292 VMSTATE_UINT32(int_enable
, SyborgSerialState
),
293 VMSTATE_INT32(read_pos
, SyborgSerialState
),
294 VMSTATE_INT32(read_count
, SyborgSerialState
),
295 VMSTATE_UINT32(dma_tx_ptr
, SyborgSerialState
),
296 VMSTATE_UINT32(dma_rx_ptr
, SyborgSerialState
),
297 VMSTATE_UINT32(dma_rx_size
, SyborgSerialState
),
298 VMSTATE_VARRAY_UINT32(read_fifo
, SyborgSerialState
, fifo_size
, 1,
299 vmstate_info_uint32
, uint32
),
300 VMSTATE_END_OF_LIST()
304 static void syborg_serial_update_handlers(SyborgSerialState
*s
)
306 if (syborg_serial_can_receive(s
)) {
307 qemu_chr_fe_set_handlers(s
->chr
, syborg_serial_receive_handler
,
310 qemu_chr_fe_set_handlers(s
->chr
, NULL
, NULL
, NULL
, s
);
314 static int syborg_serial_init(SysBusDevice
*dev
)
316 SyborgSerialState
*s
= FROM_SYSBUS(SyborgSerialState
, dev
);
319 sysbus_init_irq(dev
, &s
->irq
);
320 iomemtype
= cpu_register_io_memory(syborg_serial_readfn
,
321 syborg_serial_writefn
, s
,
322 DEVICE_NATIVE_ENDIAN
);
323 sysbus_init_mmio(dev
, 0x1000, iomemtype
);
324 s
->chr
= qdev_init_chardev(&dev
->qdev
);
326 qemu_chr_fe_open(s
->chr
);
327 syborg_serial_update_handlers(s
);
329 if (s
->fifo_size
<= 0) {
330 fprintf(stderr
, "syborg_serial: fifo too small\n");
333 s
->read_fifo
= qemu_mallocz(s
->fifo_size
* sizeof(s
->read_fifo
[0]));
338 static SysBusDeviceInfo syborg_serial_info
= {
339 .init
= syborg_serial_init
,
340 .qdev
.name
= "syborg,serial",
341 .qdev
.size
= sizeof(SyborgSerialState
),
342 .qdev
.vmsd
= &vmstate_syborg_serial
,
343 .qdev
.props
= (Property
[]) {
344 DEFINE_PROP_UINT32("fifo-size", SyborgSerialState
, fifo_size
, 16),
345 DEFINE_PROP_END_OF_LIST(),
349 static void syborg_serial_register_devices(void)
351 sysbus_register_withprop(&syborg_serial_info
);
354 device_init(syborg_serial_register_devices
)