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 "sysemu/char.h"
31 #include "exec/address-spaces.h"
33 //#define DEBUG_SERIAL
35 #define SH_SERIAL_FLAG_TEND (1 << 0)
36 #define SH_SERIAL_FLAG_TDE (1 << 1)
37 #define SH_SERIAL_FLAG_RDF (1 << 2)
38 #define SH_SERIAL_FLAG_BRK (1 << 3)
39 #define SH_SERIAL_FLAG_DR (1 << 4)
41 #define SH_RX_FIFO_LENGTH (16)
45 MemoryRegion iomem_p4
;
46 MemoryRegion iomem_a7
;
50 uint8_t dr
; /* ftdr / tdr */
51 uint8_t sr
; /* fsr / ssr */
55 uint8_t rx_fifo
[SH_RX_FIFO_LENGTH
]; /* frdr / rdr */
74 static void sh_serial_clear_fifo(sh_serial_state
* s
)
76 memset(s
->rx_fifo
, 0, SH_RX_FIFO_LENGTH
);
82 static void sh_serial_write(void *opaque
, hwaddr offs
,
83 uint64_t val
, unsigned size
)
85 sh_serial_state
*s
= opaque
;
89 printf("sh_serial: write offs=0x%02x val=0x%02x\n",
94 s
->smr
= val
& ((s
->feat
& SH_SERIAL_FEAT_SCIF
) ? 0x7b : 0xff);
100 /* TODO : For SH7751, SCIF mask should be 0xfb. */
101 s
->scr
= val
& ((s
->feat
& SH_SERIAL_FEAT_SCIF
) ? 0xfa : 0xff);
102 if (!(val
& (1 << 5)))
103 s
->flags
|= SH_SERIAL_FLAG_TEND
;
104 if ((s
->feat
& SH_SERIAL_FEAT_SCIF
) && s
->txi
) {
105 qemu_set_irq(s
->txi
, val
& (1 << 7));
107 if (!(val
& (1 << 6))) {
108 qemu_set_irq(s
->rxi
, 0);
111 case 0x0c: /* FTDR / TDR */
114 qemu_chr_fe_write(s
->chr
, &ch
, 1);
117 s
->flags
&= ~SH_SERIAL_FLAG_TDE
;
120 case 0x14: /* FRDR / RDR */
125 if (s
->feat
& SH_SERIAL_FEAT_SCIF
) {
128 if (!(val
& (1 << 6)))
129 s
->flags
&= ~SH_SERIAL_FLAG_TEND
;
130 if (!(val
& (1 << 5)))
131 s
->flags
&= ~SH_SERIAL_FLAG_TDE
;
132 if (!(val
& (1 << 4)))
133 s
->flags
&= ~SH_SERIAL_FLAG_BRK
;
134 if (!(val
& (1 << 1)))
135 s
->flags
&= ~SH_SERIAL_FLAG_RDF
;
136 if (!(val
& (1 << 0)))
137 s
->flags
&= ~SH_SERIAL_FLAG_DR
;
139 if (!(val
& (1 << 1)) || !(val
& (1 << 0))) {
141 qemu_set_irq(s
->rxi
, 0);
147 switch ((val
>> 6) & 3) {
161 if (val
& (1 << 1)) {
162 sh_serial_clear_fifo(s
);
167 case 0x20: /* SPTR */
168 s
->sptr
= val
& 0xf3;
185 s
->sptr
= val
& 0x8f;
190 fprintf(stderr
, "sh_serial: unsupported write to 0x%02"
191 HWADDR_PRIx
"\n", offs
);
195 static uint64_t sh_serial_read(void *opaque
, hwaddr offs
,
198 sh_serial_state
*s
= opaque
;
217 if (s
->feat
& SH_SERIAL_FEAT_SCIF
) {
227 if (s
->flags
& SH_SERIAL_FLAG_TEND
)
229 if (s
->flags
& SH_SERIAL_FLAG_TDE
)
231 if (s
->flags
& SH_SERIAL_FLAG_BRK
)
233 if (s
->flags
& SH_SERIAL_FLAG_RDF
)
235 if (s
->flags
& SH_SERIAL_FLAG_DR
)
238 if (s
->scr
& (1 << 5))
239 s
->flags
|= SH_SERIAL_FLAG_TDE
| SH_SERIAL_FLAG_TEND
;
244 ret
= s
->rx_fifo
[s
->rx_tail
++];
246 if (s
->rx_tail
== SH_RX_FIFO_LENGTH
)
248 if (s
->rx_cnt
< s
->rtrg
)
249 s
->flags
&= ~SH_SERIAL_FLAG_RDF
;
285 printf("sh_serial: read offs=0x%02x val=0x%x\n",
289 if (ret
& ~((1 << 16) - 1)) {
290 fprintf(stderr
, "sh_serial: unsupported read from 0x%02"
291 HWADDR_PRIx
"\n", offs
);
298 static int sh_serial_can_receive(sh_serial_state
*s
)
300 return s
->scr
& (1 << 4);
303 static void sh_serial_receive_break(sh_serial_state
*s
)
305 if (s
->feat
& SH_SERIAL_FEAT_SCIF
)
309 static int sh_serial_can_receive1(void *opaque
)
311 sh_serial_state
*s
= opaque
;
312 return sh_serial_can_receive(s
);
315 static void sh_serial_receive1(void *opaque
, const uint8_t *buf
, int size
)
317 sh_serial_state
*s
= opaque
;
319 if (s
->feat
& SH_SERIAL_FEAT_SCIF
) {
321 for (i
= 0; i
< size
; i
++) {
322 if (s
->rx_cnt
< SH_RX_FIFO_LENGTH
) {
323 s
->rx_fifo
[s
->rx_head
++] = buf
[i
];
324 if (s
->rx_head
== SH_RX_FIFO_LENGTH
) {
328 if (s
->rx_cnt
>= s
->rtrg
) {
329 s
->flags
|= SH_SERIAL_FLAG_RDF
;
330 if (s
->scr
& (1 << 6) && s
->rxi
) {
331 qemu_set_irq(s
->rxi
, 1);
337 s
->rx_fifo
[0] = buf
[0];
341 static void sh_serial_event(void *opaque
, int event
)
343 sh_serial_state
*s
= opaque
;
344 if (event
== CHR_EVENT_BREAK
)
345 sh_serial_receive_break(s
);
348 static const MemoryRegionOps sh_serial_ops
= {
349 .read
= sh_serial_read
,
350 .write
= sh_serial_write
,
351 .endianness
= DEVICE_NATIVE_ENDIAN
,
354 void sh_serial_init(MemoryRegion
*sysmem
,
355 hwaddr base
, int feat
,
356 uint32_t freq
, CharDriverState
*chr
,
365 s
= g_malloc0(sizeof(sh_serial_state
));
368 s
->flags
= SH_SERIAL_FLAG_TEND
| SH_SERIAL_FLAG_TDE
;
373 s
->scr
= 1 << 5; /* pretend that TX is enabled so early printk works */
376 if (feat
& SH_SERIAL_FEAT_SCIF
) {
383 sh_serial_clear_fifo(s
);
385 memory_region_init_io(&s
->iomem
, NULL
, &sh_serial_ops
, s
,
386 "serial", 0x100000000ULL
);
388 memory_region_init_alias(&s
->iomem_p4
, NULL
, "serial-p4", &s
->iomem
,
390 memory_region_add_subregion(sysmem
, P4ADDR(base
), &s
->iomem_p4
);
392 memory_region_init_alias(&s
->iomem_a7
, NULL
, "serial-a7", &s
->iomem
,
394 memory_region_add_subregion(sysmem
, A7ADDR(base
), &s
->iomem_a7
);
399 qemu_chr_fe_claim_no_fail(chr
);
400 qemu_chr_add_handlers(chr
, sh_serial_can_receive1
, sh_serial_receive1
,