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 /* XXX this blocks entire thread. Rewrite to use
115 * qemu_chr_fe_write and background I/O callbacks */
116 qemu_chr_fe_write_all(s
->chr
, &ch
, 1);
119 s
->flags
&= ~SH_SERIAL_FLAG_TDE
;
122 case 0x14: /* FRDR / RDR */
127 if (s
->feat
& SH_SERIAL_FEAT_SCIF
) {
130 if (!(val
& (1 << 6)))
131 s
->flags
&= ~SH_SERIAL_FLAG_TEND
;
132 if (!(val
& (1 << 5)))
133 s
->flags
&= ~SH_SERIAL_FLAG_TDE
;
134 if (!(val
& (1 << 4)))
135 s
->flags
&= ~SH_SERIAL_FLAG_BRK
;
136 if (!(val
& (1 << 1)))
137 s
->flags
&= ~SH_SERIAL_FLAG_RDF
;
138 if (!(val
& (1 << 0)))
139 s
->flags
&= ~SH_SERIAL_FLAG_DR
;
141 if (!(val
& (1 << 1)) || !(val
& (1 << 0))) {
143 qemu_set_irq(s
->rxi
, 0);
149 switch ((val
>> 6) & 3) {
163 if (val
& (1 << 1)) {
164 sh_serial_clear_fifo(s
);
169 case 0x20: /* SPTR */
170 s
->sptr
= val
& 0xf3;
187 s
->sptr
= val
& 0x8f;
192 fprintf(stderr
, "sh_serial: unsupported write to 0x%02"
193 HWADDR_PRIx
"\n", offs
);
197 static uint64_t sh_serial_read(void *opaque
, hwaddr offs
,
200 sh_serial_state
*s
= opaque
;
219 if (s
->feat
& SH_SERIAL_FEAT_SCIF
) {
229 if (s
->flags
& SH_SERIAL_FLAG_TEND
)
231 if (s
->flags
& SH_SERIAL_FLAG_TDE
)
233 if (s
->flags
& SH_SERIAL_FLAG_BRK
)
235 if (s
->flags
& SH_SERIAL_FLAG_RDF
)
237 if (s
->flags
& SH_SERIAL_FLAG_DR
)
240 if (s
->scr
& (1 << 5))
241 s
->flags
|= SH_SERIAL_FLAG_TDE
| SH_SERIAL_FLAG_TEND
;
246 ret
= s
->rx_fifo
[s
->rx_tail
++];
248 if (s
->rx_tail
== SH_RX_FIFO_LENGTH
)
250 if (s
->rx_cnt
< s
->rtrg
)
251 s
->flags
&= ~SH_SERIAL_FLAG_RDF
;
287 printf("sh_serial: read offs=0x%02x val=0x%x\n",
291 if (ret
& ~((1 << 16) - 1)) {
292 fprintf(stderr
, "sh_serial: unsupported read from 0x%02"
293 HWADDR_PRIx
"\n", offs
);
300 static int sh_serial_can_receive(sh_serial_state
*s
)
302 return s
->scr
& (1 << 4);
305 static void sh_serial_receive_break(sh_serial_state
*s
)
307 if (s
->feat
& SH_SERIAL_FEAT_SCIF
)
311 static int sh_serial_can_receive1(void *opaque
)
313 sh_serial_state
*s
= opaque
;
314 return sh_serial_can_receive(s
);
317 static void sh_serial_receive1(void *opaque
, const uint8_t *buf
, int size
)
319 sh_serial_state
*s
= opaque
;
321 if (s
->feat
& SH_SERIAL_FEAT_SCIF
) {
323 for (i
= 0; i
< size
; i
++) {
324 if (s
->rx_cnt
< SH_RX_FIFO_LENGTH
) {
325 s
->rx_fifo
[s
->rx_head
++] = buf
[i
];
326 if (s
->rx_head
== SH_RX_FIFO_LENGTH
) {
330 if (s
->rx_cnt
>= s
->rtrg
) {
331 s
->flags
|= SH_SERIAL_FLAG_RDF
;
332 if (s
->scr
& (1 << 6) && s
->rxi
) {
333 qemu_set_irq(s
->rxi
, 1);
339 s
->rx_fifo
[0] = buf
[0];
343 static void sh_serial_event(void *opaque
, int event
)
345 sh_serial_state
*s
= opaque
;
346 if (event
== CHR_EVENT_BREAK
)
347 sh_serial_receive_break(s
);
350 static const MemoryRegionOps sh_serial_ops
= {
351 .read
= sh_serial_read
,
352 .write
= sh_serial_write
,
353 .endianness
= DEVICE_NATIVE_ENDIAN
,
356 void sh_serial_init(MemoryRegion
*sysmem
,
357 hwaddr base
, int feat
,
358 uint32_t freq
, CharDriverState
*chr
,
367 s
= g_malloc0(sizeof(sh_serial_state
));
370 s
->flags
= SH_SERIAL_FLAG_TEND
| SH_SERIAL_FLAG_TDE
;
375 s
->scr
= 1 << 5; /* pretend that TX is enabled so early printk works */
378 if (feat
& SH_SERIAL_FEAT_SCIF
) {
385 sh_serial_clear_fifo(s
);
387 memory_region_init_io(&s
->iomem
, NULL
, &sh_serial_ops
, s
,
388 "serial", 0x100000000ULL
);
390 memory_region_init_alias(&s
->iomem_p4
, NULL
, "serial-p4", &s
->iomem
,
392 memory_region_add_subregion(sysmem
, P4ADDR(base
), &s
->iomem_p4
);
394 memory_region_init_alias(&s
->iomem_a7
, NULL
, "serial-a7", &s
->iomem
,
396 memory_region_add_subregion(sysmem
, A7ADDR(base
), &s
->iomem_a7
);
401 qemu_chr_fe_claim_no_fail(chr
);
402 qemu_chr_add_handlers(chr
, sh_serial_can_receive1
, sh_serial_receive1
,