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)
74 static void syborg_serial_update(SyborgSerialState
*s
)
78 if ((s
->int_enable
& SERIAL_INT_FIFO
) && s
->read_count
)
80 if (s
->int_enable
& SERIAL_INT_DMA_TX
)
82 if ((s
->int_enable
& SERIAL_INT_DMA_RX
) && s
->dma_rx_size
== 0)
85 qemu_set_irq(s
->irq
, level
);
88 static uint32_t fifo_pop(SyborgSerialState
*s
)
90 const uint32_t c
= s
->read_fifo
[s
->read_pos
];
93 if (s
->read_pos
== s
->fifo_size
)
96 DPRINTF("FIFO pop %x (%d)\n", c
, s
->read_count
);
100 static void fifo_push(SyborgSerialState
*s
, uint32_t new_value
)
104 DPRINTF("FIFO push %x (%d)\n", new_value
, s
->read_count
);
105 slot
= s
->read_pos
+ s
->read_count
;
106 if (slot
>= s
->fifo_size
)
107 slot
-= s
->fifo_size
;
108 s
->read_fifo
[slot
] = new_value
;
112 static void do_dma_tx(SyborgSerialState
*s
, uint32_t count
)
119 if (s
->chr
!= NULL
) {
120 /* optimize later. Now, 1 byte per iteration */
122 cpu_physical_memory_read(s
->dma_tx_ptr
, &ch
, 1);
123 qemu_chr_fe_write(s
->chr
, &ch
, 1);
127 s
->dma_tx_ptr
+= count
;
129 /* QEMU char backends do not have a nonblocking mode, so we transmit all
130 the data immediately and the interrupt status will be unchanged. */
133 /* Initiate RX DMA, and transfer data from the FIFO. */
134 static void dma_rx_start(SyborgSerialState
*s
, uint32_t len
)
139 dest
= s
->dma_rx_ptr
;
140 if (s
->read_count
< len
) {
141 s
->dma_rx_size
= len
- s
->read_count
;
149 cpu_physical_memory_write(dest
, &ch
, 1);
152 s
->dma_rx_ptr
= dest
;
153 syborg_serial_update(s
);
156 static uint64_t syborg_serial_read(void *opaque
, target_phys_addr_t offset
,
159 SyborgSerialState
*s
= (SyborgSerialState
*)opaque
;
163 DPRINTF("read 0x%x\n", (int)offset
);
164 switch(offset
>> 2) {
166 return SYBORG_ID_SERIAL
;
168 if (s
->read_count
> 0)
172 syborg_serial_update(s
);
174 case SERIAL_FIFO_COUNT
:
175 return s
->read_count
;
176 case SERIAL_INT_ENABLE
:
177 return s
->int_enable
;
178 case SERIAL_DMA_TX_ADDR
:
179 return s
->dma_tx_ptr
;
180 case SERIAL_DMA_TX_COUNT
:
182 case SERIAL_DMA_RX_ADDR
:
183 return s
->dma_rx_ptr
;
184 case SERIAL_DMA_RX_COUNT
:
185 return s
->dma_rx_size
;
186 case SERIAL_FIFO_SIZE
:
190 cpu_abort(cpu_single_env
, "syborg_serial_read: Bad offset %x\n",
196 static void syborg_serial_write(void *opaque
, target_phys_addr_t offset
,
197 uint64_t value
, unsigned size
)
199 SyborgSerialState
*s
= (SyborgSerialState
*)opaque
;
203 DPRINTF("Write 0x%x=0x%x\n", (int)offset
, value
);
204 switch (offset
>> 2) {
208 qemu_chr_fe_write(s
->chr
, &ch
, 1);
210 case SERIAL_INT_ENABLE
:
211 s
->int_enable
= value
;
212 syborg_serial_update(s
);
214 case SERIAL_DMA_TX_ADDR
:
215 s
->dma_tx_ptr
= value
;
217 case SERIAL_DMA_TX_COUNT
:
220 case SERIAL_DMA_RX_ADDR
:
221 /* For safety, writes to this register cancel any pending DMA. */
223 s
->dma_rx_ptr
= value
;
225 case SERIAL_DMA_RX_COUNT
:
226 dma_rx_start(s
, value
);
229 cpu_abort(cpu_single_env
, "syborg_serial_write: Bad offset %x\n",
235 static int syborg_serial_can_receive(void *opaque
)
237 SyborgSerialState
*s
= (SyborgSerialState
*)opaque
;
240 return s
->dma_rx_size
;
241 return s
->fifo_size
- s
->read_count
;
244 static void syborg_serial_receive(void *opaque
, const uint8_t *buf
, int size
)
246 SyborgSerialState
*s
= (SyborgSerialState
*)opaque
;
248 if (s
->dma_rx_size
) {
249 /* Place it in the DMA buffer. */
250 cpu_physical_memory_write(s
->dma_rx_ptr
, buf
, size
);
251 s
->dma_rx_size
-= size
;
252 s
->dma_rx_ptr
+= size
;
258 syborg_serial_update(s
);
261 static void syborg_serial_event(void *opaque
, int event
)
263 /* TODO: Report BREAK events? */
266 static const MemoryRegionOps syborg_serial_ops
= {
267 .read
= syborg_serial_read
,
268 .write
= syborg_serial_write
,
269 .endianness
= DEVICE_NATIVE_ENDIAN
,
272 static const VMStateDescription vmstate_syborg_serial
= {
273 .name
= "syborg_serial",
275 .minimum_version_id
= 1,
276 .minimum_version_id_old
= 1,
277 .fields
= (VMStateField
[]) {
278 VMSTATE_UINT32_EQUAL(fifo_size
, SyborgSerialState
),
279 VMSTATE_UINT32(int_enable
, SyborgSerialState
),
280 VMSTATE_INT32(read_pos
, SyborgSerialState
),
281 VMSTATE_INT32(read_count
, SyborgSerialState
),
282 VMSTATE_UINT32(dma_tx_ptr
, SyborgSerialState
),
283 VMSTATE_UINT32(dma_rx_ptr
, SyborgSerialState
),
284 VMSTATE_UINT32(dma_rx_size
, SyborgSerialState
),
285 VMSTATE_VARRAY_UINT32(read_fifo
, SyborgSerialState
, fifo_size
, 1,
286 vmstate_info_uint32
, uint32
),
287 VMSTATE_END_OF_LIST()
291 static int syborg_serial_init(SysBusDevice
*dev
)
293 SyborgSerialState
*s
= FROM_SYSBUS(SyborgSerialState
, dev
);
295 sysbus_init_irq(dev
, &s
->irq
);
296 memory_region_init_io(&s
->iomem
, &syborg_serial_ops
, s
,
298 sysbus_init_mmio(dev
, &s
->iomem
);
299 s
->chr
= qdev_init_chardev(&dev
->qdev
);
301 qemu_chr_add_handlers(s
->chr
, syborg_serial_can_receive
,
302 syborg_serial_receive
, syborg_serial_event
, s
);
304 if (s
->fifo_size
<= 0) {
305 fprintf(stderr
, "syborg_serial: fifo too small\n");
308 s
->read_fifo
= g_malloc0(s
->fifo_size
* sizeof(s
->read_fifo
[0]));
313 static SysBusDeviceInfo syborg_serial_info
= {
314 .init
= syborg_serial_init
,
315 .qdev
.name
= "syborg,serial",
316 .qdev
.size
= sizeof(SyborgSerialState
),
317 .qdev
.vmsd
= &vmstate_syborg_serial
,
318 .qdev
.props
= (Property
[]) {
319 DEFINE_PROP_UINT32("fifo-size", SyborgSerialState
, fifo_size
, 16),
320 DEFINE_PROP_END_OF_LIST(),
324 static void syborg_serial_register_devices(void)
326 sysbus_register_withprop(&syborg_serial_info
);
329 device_init(syborg_serial_register_devices
)