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(SyborgSerialState
*s
)
77 if ((s
->int_enable
& SERIAL_INT_FIFO
) && s
->read_count
)
79 if (s
->int_enable
& SERIAL_INT_DMA_TX
)
81 if ((s
->int_enable
& SERIAL_INT_DMA_RX
) && s
->dma_rx_size
== 0)
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
];
92 if (s
->read_pos
== s
->fifo_size
)
95 DPRINTF("FIFO pop %x (%d)\n", c
, s
->read_count
);
99 static void fifo_push(SyborgSerialState
*s
, uint32_t new_value
)
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
;
111 static void do_dma_tx(SyborgSerialState
*s
, uint32_t count
)
118 if (s
->chr
!= NULL
) {
119 /* optimize later. Now, 1 byte per iteration */
121 cpu_physical_memory_read(s
->dma_tx_ptr
, &ch
, 1);
122 qemu_chr_write(s
->chr
, &ch
, 1);
126 s
->dma_tx_ptr
+= count
;
128 /* QEMU char backends do not have a nonblocking mode, so we transmit all
129 the data imediately 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
)
138 dest
= s
->dma_rx_ptr
;
139 if (s
->read_count
< len
) {
140 s
->dma_rx_size
= len
- s
->read_count
;
148 cpu_physical_memory_write(dest
, &ch
, 1);
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
;
161 DPRINTF("read 0x%x\n", (int)offset
);
162 switch(offset
>> 2) {
164 return SYBORG_ID_SERIAL
;
166 if (s
->read_count
> 0)
170 syborg_serial_update(s
);
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
:
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
:
188 cpu_abort(cpu_single_env
, "syborg_serial_read: Bad offset %x\n",
194 static void syborg_serial_write(void *opaque
, target_phys_addr_t offset
,
197 SyborgSerialState
*s
= (SyborgSerialState
*)opaque
;
201 DPRINTF("Write 0x%x=0x%x\n", (int)offset
, value
);
202 switch (offset
>> 2) {
206 qemu_chr_write(s
->chr
, &ch
, 1);
208 case SERIAL_INT_ENABLE
:
209 s
->int_enable
= value
;
210 syborg_serial_update(s
);
212 case SERIAL_DMA_TX_ADDR
:
213 s
->dma_tx_ptr
= value
;
215 case SERIAL_DMA_TX_COUNT
:
218 case SERIAL_DMA_RX_ADDR
:
219 /* For safety, writes to this register cancel any pending DMA. */
221 s
->dma_rx_ptr
= value
;
223 case SERIAL_DMA_RX_COUNT
:
224 dma_rx_start(s
, value
);
227 cpu_abort(cpu_single_env
, "syborg_serial_write: Bad offset %x\n",
233 static int syborg_serial_can_receive(void *opaque
)
235 SyborgSerialState
*s
= (SyborgSerialState
*)opaque
;
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
;
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
[] = {
270 static CPUWriteMemoryFunc
* const syborg_serial_writefn
[] = {
276 static void syborg_serial_save(QEMUFile
*f
, void *opaque
)
278 SyborgSerialState
*s
= opaque
;
281 qemu_put_be32(f
, s
->fifo_size
);
282 qemu_put_be32(f
, s
->int_enable
);
283 qemu_put_be32(f
, s
->read_pos
);
284 qemu_put_be32(f
, s
->read_count
);
285 qemu_put_be32(f
, s
->dma_tx_ptr
);
286 qemu_put_be32(f
, s
->dma_rx_ptr
);
287 qemu_put_be32(f
, s
->dma_rx_size
);
288 for (i
= 0; i
< s
->fifo_size
; i
++) {
289 qemu_put_be32(f
, s
->read_fifo
[i
]);
293 static int syborg_serial_load(QEMUFile
*f
, void *opaque
, int version_id
)
295 SyborgSerialState
*s
= opaque
;
301 i
= qemu_get_be32(f
);
302 if (s
->fifo_size
!= i
)
305 s
->int_enable
= qemu_get_be32(f
);
306 s
->read_pos
= qemu_get_be32(f
);
307 s
->read_count
= qemu_get_be32(f
);
308 s
->dma_tx_ptr
= qemu_get_be32(f
);
309 s
->dma_rx_ptr
= qemu_get_be32(f
);
310 s
->dma_rx_size
= qemu_get_be32(f
);
311 for (i
= 0; i
< s
->fifo_size
; i
++) {
312 s
->read_fifo
[i
] = qemu_get_be32(f
);
318 static int syborg_serial_init(SysBusDevice
*dev
)
320 SyborgSerialState
*s
= FROM_SYSBUS(SyborgSerialState
, dev
);
323 sysbus_init_irq(dev
, &s
->irq
);
324 iomemtype
= cpu_register_io_memory(syborg_serial_readfn
,
325 syborg_serial_writefn
, s
);
326 sysbus_init_mmio(dev
, 0x1000, iomemtype
);
327 s
->chr
= qdev_init_chardev(&dev
->qdev
);
329 qemu_chr_add_handlers(s
->chr
, syborg_serial_can_receive
,
330 syborg_serial_receive
, syborg_serial_event
, s
);
332 if (s
->fifo_size
<= 0) {
333 fprintf(stderr
, "syborg_serial: fifo too small\n");
336 s
->read_fifo
= qemu_mallocz(s
->fifo_size
* sizeof(s
->read_fifo
[0]));
338 register_savevm("syborg_serial", -1, 1,
339 syborg_serial_save
, syborg_serial_load
, s
);
343 static SysBusDeviceInfo syborg_serial_info
= {
344 .init
= syborg_serial_init
,
345 .qdev
.name
= "syborg,serial",
346 .qdev
.size
= sizeof(SyborgSerialState
),
347 .qdev
.props
= (Property
[]) {
348 DEFINE_PROP_UINT32("fifo-size", SyborgSerialState
, fifo_size
, 16),
349 DEFINE_PROP_END_OF_LIST(),
353 static void syborg_serial_register_devices(void)
355 sysbus_register_withprop(&syborg_serial_info
);
358 device_init(syborg_serial_register_devices
)