2 * QEMU SCI/SCIF serial port emulation
4 * Copyright (c) 2007 Magnus Damm
6 * Based on serial.c - QEMU 16450 UART emulation
7 * Copyright (c) 2003-2004 Fabrice Bellard
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 #include "qemu/osdep.h"
29 #include "hw/sysbus.h"
31 #include "hw/qdev-core.h"
32 #include "hw/qdev-properties.h"
33 #include "hw/qdev-properties-system.h"
34 #include "hw/sh4/sh.h"
35 #include "chardev/char-fe.h"
36 #include "qapi/error.h"
37 #include "qemu/timer.h"
41 #define SH_SERIAL_FLAG_TEND (1 << 0)
42 #define SH_SERIAL_FLAG_TDE (1 << 1)
43 #define SH_SERIAL_FLAG_RDF (1 << 2)
44 #define SH_SERIAL_FLAG_BRK (1 << 3)
45 #define SH_SERIAL_FLAG_DR (1 << 4)
47 #define SH_RX_FIFO_LENGTH (16)
49 OBJECT_DECLARE_SIMPLE_TYPE(SHSerialState
, SH_SERIAL
)
51 struct SHSerialState
{
56 uint8_t dr
; /* ftdr / tdr */
57 uint8_t sr
; /* fsr / ssr */
61 uint8_t rx_fifo
[SH_RX_FIFO_LENGTH
]; /* frdr / rdr */
71 QEMUTimer fifo_timeout_timer
;
72 uint64_t etu
; /* Elementary Time Unit (ns) */
81 typedef struct {} SHSerialStateClass
;
83 OBJECT_DEFINE_TYPE(SHSerialState
, sh_serial
, SH_SERIAL
, SYS_BUS_DEVICE
)
85 static void sh_serial_clear_fifo(SHSerialState
*s
)
87 memset(s
->rx_fifo
, 0, SH_RX_FIFO_LENGTH
);
93 static void sh_serial_write(void *opaque
, hwaddr offs
,
94 uint64_t val
, unsigned size
)
96 SHSerialState
*s
= opaque
;
97 DeviceState
*d
= DEVICE(s
);
100 trace_sh_serial_write(d
->id
, size
, offs
, val
);
103 s
->smr
= val
& ((s
->feat
& SH_SERIAL_FEAT_SCIF
) ? 0x7b : 0xff);
109 /* TODO : For SH7751, SCIF mask should be 0xfb. */
110 s
->scr
= val
& ((s
->feat
& SH_SERIAL_FEAT_SCIF
) ? 0xfa : 0xff);
111 if (!(val
& (1 << 5))) {
112 s
->flags
|= SH_SERIAL_FLAG_TEND
;
114 if ((s
->feat
& SH_SERIAL_FEAT_SCIF
) && s
->txi
) {
115 qemu_set_irq(s
->txi
, val
& (1 << 7));
117 if (!(val
& (1 << 6))) {
118 qemu_set_irq(s
->rxi
, 0);
121 case 0x0c: /* FTDR / TDR */
122 if (qemu_chr_fe_backend_connected(&s
->chr
)) {
125 * XXX this blocks entire thread. Rewrite to use
126 * qemu_chr_fe_write and background I/O callbacks
128 qemu_chr_fe_write_all(&s
->chr
, &ch
, 1);
131 s
->flags
&= ~SH_SERIAL_FLAG_TDE
;
134 case 0x14: /* FRDR / RDR */
139 if (s
->feat
& SH_SERIAL_FEAT_SCIF
) {
142 if (!(val
& (1 << 6))) {
143 s
->flags
&= ~SH_SERIAL_FLAG_TEND
;
145 if (!(val
& (1 << 5))) {
146 s
->flags
&= ~SH_SERIAL_FLAG_TDE
;
148 if (!(val
& (1 << 4))) {
149 s
->flags
&= ~SH_SERIAL_FLAG_BRK
;
151 if (!(val
& (1 << 1))) {
152 s
->flags
&= ~SH_SERIAL_FLAG_RDF
;
154 if (!(val
& (1 << 0))) {
155 s
->flags
&= ~SH_SERIAL_FLAG_DR
;
158 if (!(val
& (1 << 1)) || !(val
& (1 << 0))) {
160 qemu_set_irq(s
->rxi
, 0);
166 switch ((val
>> 6) & 3) {
180 if (val
& (1 << 1)) {
181 sh_serial_clear_fifo(s
);
186 case 0x20: /* SPTR */
187 s
->sptr
= val
& 0xf3;
203 s
->sptr
= val
& 0x8f;
207 qemu_log_mask(LOG_GUEST_ERROR
,
208 "%s: unsupported write to 0x%02" HWADDR_PRIx
"\n",
212 static uint64_t sh_serial_read(void *opaque
, hwaddr offs
,
215 SHSerialState
*s
= opaque
;
216 DeviceState
*d
= DEVICE(s
);
217 uint32_t ret
= UINT32_MAX
;
235 if (s
->feat
& SH_SERIAL_FEAT_SCIF
) {
245 if (s
->flags
& SH_SERIAL_FLAG_TEND
) {
248 if (s
->flags
& SH_SERIAL_FLAG_TDE
) {
251 if (s
->flags
& SH_SERIAL_FLAG_BRK
) {
254 if (s
->flags
& SH_SERIAL_FLAG_RDF
) {
257 if (s
->flags
& SH_SERIAL_FLAG_DR
) {
261 if (s
->scr
& (1 << 5)) {
262 s
->flags
|= SH_SERIAL_FLAG_TDE
| SH_SERIAL_FLAG_TEND
;
268 ret
= s
->rx_fifo
[s
->rx_tail
++];
270 if (s
->rx_tail
== SH_RX_FIFO_LENGTH
) {
273 if (s
->rx_cnt
< s
->rtrg
) {
274 s
->flags
&= ~SH_SERIAL_FLAG_RDF
;
309 trace_sh_serial_read(d
->id
, size
, offs
, ret
);
311 if (ret
> UINT16_MAX
) {
312 qemu_log_mask(LOG_GUEST_ERROR
,
313 "%s: unsupported read from 0x%02" HWADDR_PRIx
"\n",
321 static int sh_serial_can_receive(SHSerialState
*s
)
323 return s
->scr
& (1 << 4);
326 static void sh_serial_receive_break(SHSerialState
*s
)
328 if (s
->feat
& SH_SERIAL_FEAT_SCIF
) {
333 static int sh_serial_can_receive1(void *opaque
)
335 SHSerialState
*s
= opaque
;
336 return sh_serial_can_receive(s
);
339 static void sh_serial_timeout_int(void *opaque
)
341 SHSerialState
*s
= opaque
;
343 s
->flags
|= SH_SERIAL_FLAG_RDF
;
344 if (s
->scr
& (1 << 6) && s
->rxi
) {
345 qemu_set_irq(s
->rxi
, 1);
349 static void sh_serial_receive1(void *opaque
, const uint8_t *buf
, int size
)
351 SHSerialState
*s
= opaque
;
353 if (s
->feat
& SH_SERIAL_FEAT_SCIF
) {
355 for (i
= 0; i
< size
; i
++) {
356 if (s
->rx_cnt
< SH_RX_FIFO_LENGTH
) {
357 s
->rx_fifo
[s
->rx_head
++] = buf
[i
];
358 if (s
->rx_head
== SH_RX_FIFO_LENGTH
) {
362 if (s
->rx_cnt
>= s
->rtrg
) {
363 s
->flags
|= SH_SERIAL_FLAG_RDF
;
364 if (s
->scr
& (1 << 6) && s
->rxi
) {
365 timer_del(&s
->fifo_timeout_timer
);
366 qemu_set_irq(s
->rxi
, 1);
369 timer_mod(&s
->fifo_timeout_timer
,
370 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + 15 * s
->etu
);
375 s
->rx_fifo
[0] = buf
[0];
379 static void sh_serial_event(void *opaque
, QEMUChrEvent event
)
381 SHSerialState
*s
= opaque
;
382 if (event
== CHR_EVENT_BREAK
) {
383 sh_serial_receive_break(s
);
387 static const MemoryRegionOps sh_serial_ops
= {
388 .read
= sh_serial_read
,
389 .write
= sh_serial_write
,
390 .endianness
= DEVICE_NATIVE_ENDIAN
,
393 static void sh_serial_reset(DeviceState
*dev
)
395 SHSerialState
*s
= SH_SERIAL(dev
);
397 s
->flags
= SH_SERIAL_FLAG_TEND
| SH_SERIAL_FLAG_TDE
;
402 s
->scr
= 1 << 5; /* pretend that TX is enabled so early printk works */
405 if (s
->feat
& SH_SERIAL_FEAT_SCIF
) {
411 sh_serial_clear_fifo(s
);
414 static void sh_serial_realize(DeviceState
*d
, Error
**errp
)
416 SHSerialState
*s
= SH_SERIAL(d
);
417 MemoryRegion
*iomem
= g_malloc(sizeof(*iomem
));
420 memory_region_init_io(iomem
, OBJECT(d
), &sh_serial_ops
, s
, d
->id
, 0x28);
421 sysbus_init_mmio(SYS_BUS_DEVICE(d
), iomem
);
422 qdev_init_gpio_out_named(d
, &s
->eri
, "eri", 1);
423 qdev_init_gpio_out_named(d
, &s
->rxi
, "rxi", 1);
424 qdev_init_gpio_out_named(d
, &s
->txi
, "txi", 1);
425 qdev_init_gpio_out_named(d
, &s
->tei
, "tei", 1);
426 qdev_init_gpio_out_named(d
, &s
->bri
, "bri", 1);
428 if (qemu_chr_fe_backend_connected(&s
->chr
)) {
429 qemu_chr_fe_set_handlers(&s
->chr
, sh_serial_can_receive1
,
431 sh_serial_event
, NULL
, s
, NULL
, true);
434 timer_init_ns(&s
->fifo_timeout_timer
, QEMU_CLOCK_VIRTUAL
,
435 sh_serial_timeout_int
, s
);
436 s
->etu
= NANOSECONDS_PER_SECOND
/ 9600;
439 static void sh_serial_finalize(Object
*obj
)
441 SHSerialState
*s
= SH_SERIAL(obj
);
443 timer_del(&s
->fifo_timeout_timer
);
446 static void sh_serial_init(Object
*obj
)
450 static Property sh_serial_properties
[] = {
451 DEFINE_PROP_CHR("chardev", SHSerialState
, chr
),
452 DEFINE_PROP_UINT8("features", SHSerialState
, feat
, 0),
453 DEFINE_PROP_END_OF_LIST()
456 static void sh_serial_class_init(ObjectClass
*oc
, void *data
)
458 DeviceClass
*dc
= DEVICE_CLASS(oc
);
460 device_class_set_props(dc
, sh_serial_properties
);
461 dc
->realize
= sh_serial_realize
;
462 dc
->reset
= sh_serial_reset
;
463 /* Reason: part of SuperH CPU/SoC, needs to be wired up */
464 dc
->user_creatable
= false;