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
27 #include "qemu/osdep.h"
29 #include "hw/sh4/sh.h"
30 #include "chardev/char-fe.h"
31 #include "exec/address-spaces.h"
32 #include "qapi/error.h"
34 //#define DEBUG_SERIAL
36 #define SH_SERIAL_FLAG_TEND (1 << 0)
37 #define SH_SERIAL_FLAG_TDE (1 << 1)
38 #define SH_SERIAL_FLAG_RDF (1 << 2)
39 #define SH_SERIAL_FLAG_BRK (1 << 3)
40 #define SH_SERIAL_FLAG_DR (1 << 4)
42 #define SH_RX_FIFO_LENGTH (16)
46 MemoryRegion iomem_p4
;
47 MemoryRegion iomem_a7
;
51 uint8_t dr
; /* ftdr / tdr */
52 uint8_t sr
; /* fsr / ssr */
56 uint8_t rx_fifo
[SH_RX_FIFO_LENGTH
]; /* frdr / rdr */
75 static void sh_serial_clear_fifo(sh_serial_state
* s
)
77 memset(s
->rx_fifo
, 0, SH_RX_FIFO_LENGTH
);
83 static void sh_serial_write(void *opaque
, hwaddr offs
,
84 uint64_t val
, unsigned size
)
86 sh_serial_state
*s
= opaque
;
90 printf("sh_serial: write offs=0x%02x val=0x%02x\n",
95 s
->smr
= val
& ((s
->feat
& SH_SERIAL_FEAT_SCIF
) ? 0x7b : 0xff);
101 /* TODO : For SH7751, SCIF mask should be 0xfb. */
102 s
->scr
= val
& ((s
->feat
& SH_SERIAL_FEAT_SCIF
) ? 0xfa : 0xff);
103 if (!(val
& (1 << 5)))
104 s
->flags
|= SH_SERIAL_FLAG_TEND
;
105 if ((s
->feat
& SH_SERIAL_FEAT_SCIF
) && s
->txi
) {
106 qemu_set_irq(s
->txi
, val
& (1 << 7));
108 if (!(val
& (1 << 6))) {
109 qemu_set_irq(s
->rxi
, 0);
112 case 0x0c: /* FTDR / TDR */
113 if (qemu_chr_fe_backend_connected(&s
->chr
)) {
115 /* XXX this blocks entire thread. Rewrite to use
116 * qemu_chr_fe_write and background I/O callbacks */
117 qemu_chr_fe_write_all(&s
->chr
, &ch
, 1);
120 s
->flags
&= ~SH_SERIAL_FLAG_TDE
;
123 case 0x14: /* FRDR / RDR */
128 if (s
->feat
& SH_SERIAL_FEAT_SCIF
) {
131 if (!(val
& (1 << 6)))
132 s
->flags
&= ~SH_SERIAL_FLAG_TEND
;
133 if (!(val
& (1 << 5)))
134 s
->flags
&= ~SH_SERIAL_FLAG_TDE
;
135 if (!(val
& (1 << 4)))
136 s
->flags
&= ~SH_SERIAL_FLAG_BRK
;
137 if (!(val
& (1 << 1)))
138 s
->flags
&= ~SH_SERIAL_FLAG_RDF
;
139 if (!(val
& (1 << 0)))
140 s
->flags
&= ~SH_SERIAL_FLAG_DR
;
142 if (!(val
& (1 << 1)) || !(val
& (1 << 0))) {
144 qemu_set_irq(s
->rxi
, 0);
150 switch ((val
>> 6) & 3) {
164 if (val
& (1 << 1)) {
165 sh_serial_clear_fifo(s
);
170 case 0x20: /* SPTR */
171 s
->sptr
= val
& 0xf3;
188 s
->sptr
= val
& 0x8f;
193 fprintf(stderr
, "sh_serial: unsupported write to 0x%02"
194 HWADDR_PRIx
"\n", offs
);
198 static uint64_t sh_serial_read(void *opaque
, hwaddr offs
,
201 sh_serial_state
*s
= opaque
;
220 if (s
->feat
& SH_SERIAL_FEAT_SCIF
) {
230 if (s
->flags
& SH_SERIAL_FLAG_TEND
)
232 if (s
->flags
& SH_SERIAL_FLAG_TDE
)
234 if (s
->flags
& SH_SERIAL_FLAG_BRK
)
236 if (s
->flags
& SH_SERIAL_FLAG_RDF
)
238 if (s
->flags
& SH_SERIAL_FLAG_DR
)
241 if (s
->scr
& (1 << 5))
242 s
->flags
|= SH_SERIAL_FLAG_TDE
| SH_SERIAL_FLAG_TEND
;
247 ret
= s
->rx_fifo
[s
->rx_tail
++];
249 if (s
->rx_tail
== SH_RX_FIFO_LENGTH
)
251 if (s
->rx_cnt
< s
->rtrg
)
252 s
->flags
&= ~SH_SERIAL_FLAG_RDF
;
288 printf("sh_serial: read offs=0x%02x val=0x%x\n",
292 if (ret
& ~((1 << 16) - 1)) {
293 fprintf(stderr
, "sh_serial: unsupported read from 0x%02"
294 HWADDR_PRIx
"\n", offs
);
301 static int sh_serial_can_receive(sh_serial_state
*s
)
303 return s
->scr
& (1 << 4);
306 static void sh_serial_receive_break(sh_serial_state
*s
)
308 if (s
->feat
& SH_SERIAL_FEAT_SCIF
)
312 static int sh_serial_can_receive1(void *opaque
)
314 sh_serial_state
*s
= opaque
;
315 return sh_serial_can_receive(s
);
318 static void sh_serial_receive1(void *opaque
, const uint8_t *buf
, int size
)
320 sh_serial_state
*s
= opaque
;
322 if (s
->feat
& SH_SERIAL_FEAT_SCIF
) {
324 for (i
= 0; i
< size
; i
++) {
325 if (s
->rx_cnt
< SH_RX_FIFO_LENGTH
) {
326 s
->rx_fifo
[s
->rx_head
++] = buf
[i
];
327 if (s
->rx_head
== SH_RX_FIFO_LENGTH
) {
331 if (s
->rx_cnt
>= s
->rtrg
) {
332 s
->flags
|= SH_SERIAL_FLAG_RDF
;
333 if (s
->scr
& (1 << 6) && s
->rxi
) {
334 qemu_set_irq(s
->rxi
, 1);
340 s
->rx_fifo
[0] = buf
[0];
344 static void sh_serial_event(void *opaque
, int event
)
346 sh_serial_state
*s
= opaque
;
347 if (event
== CHR_EVENT_BREAK
)
348 sh_serial_receive_break(s
);
351 static const MemoryRegionOps sh_serial_ops
= {
352 .read
= sh_serial_read
,
353 .write
= sh_serial_write
,
354 .endianness
= DEVICE_NATIVE_ENDIAN
,
357 void sh_serial_init(MemoryRegion
*sysmem
,
358 hwaddr base
, int feat
,
359 uint32_t freq
, Chardev
*chr
,
368 s
= g_malloc0(sizeof(sh_serial_state
));
371 s
->flags
= SH_SERIAL_FLAG_TEND
| SH_SERIAL_FLAG_TDE
;
376 s
->scr
= 1 << 5; /* pretend that TX is enabled so early printk works */
379 if (feat
& SH_SERIAL_FEAT_SCIF
) {
386 sh_serial_clear_fifo(s
);
388 memory_region_init_io(&s
->iomem
, NULL
, &sh_serial_ops
, s
,
389 "serial", 0x100000000ULL
);
391 memory_region_init_alias(&s
->iomem_p4
, NULL
, "serial-p4", &s
->iomem
,
393 memory_region_add_subregion(sysmem
, P4ADDR(base
), &s
->iomem_p4
);
395 memory_region_init_alias(&s
->iomem_a7
, NULL
, "serial-a7", &s
->iomem
,
397 memory_region_add_subregion(sysmem
, A7ADDR(base
), &s
->iomem_a7
);
400 qemu_chr_fe_init(&s
->chr
, chr
, &error_abort
);
401 qemu_chr_fe_set_handlers(&s
->chr
, sh_serial_can_receive1
,
403 sh_serial_event
, NULL
, s
, NULL
, true);