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
29 #include "qemu-char.h"
32 //#define DEBUG_SERIAL
34 #define SH_SERIAL_FLAG_TEND (1 << 0)
35 #define SH_SERIAL_FLAG_TDE (1 << 1)
36 #define SH_SERIAL_FLAG_RDF (1 << 2)
37 #define SH_SERIAL_FLAG_BRK (1 << 3)
38 #define SH_SERIAL_FLAG_DR (1 << 4)
40 #define SH_RX_FIFO_LENGTH (16)
46 uint8_t dr
; /* ftdr / tdr */
47 uint8_t sr
; /* fsr / ssr */
51 uint8_t rx_fifo
[SH_RX_FIFO_LENGTH
]; /* frdr / rdr */
70 static void sh_serial_clear_fifo(sh_serial_state
* s
)
72 memset(s
->rx_fifo
, 0, SH_RX_FIFO_LENGTH
);
78 static void sh_serial_ioport_write(void *opaque
, uint32_t offs
, uint32_t val
)
80 sh_serial_state
*s
= opaque
;
84 printf("sh_serial: write offs=0x%02x val=0x%02x\n",
89 s
->smr
= val
& ((s
->feat
& SH_SERIAL_FEAT_SCIF
) ? 0x7b : 0xff);
95 /* TODO : For SH7751, SCIF mask should be 0xfb. */
96 s
->scr
= val
& ((s
->feat
& SH_SERIAL_FEAT_SCIF
) ? 0xfa : 0xff);
97 if (!(val
& (1 << 5)))
98 s
->flags
|= SH_SERIAL_FLAG_TEND
;
99 if ((s
->feat
& SH_SERIAL_FEAT_SCIF
) && s
->txi
) {
100 qemu_set_irq(s
->txi
, val
& (1 << 7));
102 if (!(val
& (1 << 6))) {
103 qemu_set_irq(s
->rxi
, 0);
106 case 0x0c: /* FTDR / TDR */
109 qemu_chr_write(s
->chr
, &ch
, 1);
112 s
->flags
&= ~SH_SERIAL_FLAG_TDE
;
115 case 0x14: /* FRDR / RDR */
120 if (s
->feat
& SH_SERIAL_FEAT_SCIF
) {
123 if (!(val
& (1 << 6)))
124 s
->flags
&= ~SH_SERIAL_FLAG_TEND
;
125 if (!(val
& (1 << 5)))
126 s
->flags
&= ~SH_SERIAL_FLAG_TDE
;
127 if (!(val
& (1 << 4)))
128 s
->flags
&= ~SH_SERIAL_FLAG_BRK
;
129 if (!(val
& (1 << 1)))
130 s
->flags
&= ~SH_SERIAL_FLAG_RDF
;
131 if (!(val
& (1 << 0)))
132 s
->flags
&= ~SH_SERIAL_FLAG_DR
;
134 if (!(val
& (1 << 1)) || !(val
& (1 << 0))) {
136 qemu_set_irq(s
->rxi
, 0);
142 switch ((val
>> 6) & 3) {
156 if (val
& (1 << 1)) {
157 sh_serial_clear_fifo(s
);
162 case 0x20: /* SPTR */
163 s
->sptr
= val
& 0xf3;
180 s
->sptr
= val
& 0x8f;
185 fprintf(stderr
, "sh_serial: unsupported write to 0x%02x\n", offs
);
189 static uint32_t sh_serial_ioport_read(void *opaque
, uint32_t offs
)
191 sh_serial_state
*s
= opaque
;
210 if (s
->feat
& SH_SERIAL_FEAT_SCIF
) {
220 if (s
->flags
& SH_SERIAL_FLAG_TEND
)
222 if (s
->flags
& SH_SERIAL_FLAG_TDE
)
224 if (s
->flags
& SH_SERIAL_FLAG_BRK
)
226 if (s
->flags
& SH_SERIAL_FLAG_RDF
)
228 if (s
->flags
& SH_SERIAL_FLAG_DR
)
231 if (s
->scr
& (1 << 5))
232 s
->flags
|= SH_SERIAL_FLAG_TDE
| SH_SERIAL_FLAG_TEND
;
237 ret
= s
->rx_fifo
[s
->rx_tail
++];
239 if (s
->rx_tail
== SH_RX_FIFO_LENGTH
)
241 if (s
->rx_cnt
< s
->rtrg
)
242 s
->flags
&= ~SH_SERIAL_FLAG_RDF
;
280 printf("sh_serial: read offs=0x%02x val=0x%x\n",
284 if (ret
& ~((1 << 16) - 1)) {
285 fprintf(stderr
, "sh_serial: unsupported read from 0x%02x\n", offs
);
292 static int sh_serial_can_receive(sh_serial_state
*s
)
294 return s
->scr
& (1 << 4);
297 static void sh_serial_receive_byte(sh_serial_state
*s
, int ch
)
299 if (s
->feat
& SH_SERIAL_FEAT_SCIF
) {
300 if (s
->rx_cnt
< SH_RX_FIFO_LENGTH
) {
301 s
->rx_fifo
[s
->rx_head
++] = ch
;
302 if (s
->rx_head
== SH_RX_FIFO_LENGTH
)
305 if (s
->rx_cnt
>= s
->rtrg
) {
306 s
->flags
|= SH_SERIAL_FLAG_RDF
;
307 if (s
->scr
& (1 << 6) && s
->rxi
) {
308 qemu_set_irq(s
->rxi
, 1);
317 static void sh_serial_receive_break(sh_serial_state
*s
)
319 if (s
->feat
& SH_SERIAL_FEAT_SCIF
)
323 static int sh_serial_can_receive1(void *opaque
)
325 sh_serial_state
*s
= opaque
;
326 return sh_serial_can_receive(s
);
329 static void sh_serial_receive1(void *opaque
, const uint8_t *buf
, int size
)
331 sh_serial_state
*s
= opaque
;
332 sh_serial_receive_byte(s
, buf
[0]);
335 static void sh_serial_event(void *opaque
, int event
)
337 sh_serial_state
*s
= opaque
;
338 if (event
== CHR_EVENT_BREAK
)
339 sh_serial_receive_break(s
);
342 static uint32_t sh_serial_read (void *opaque
, target_phys_addr_t addr
)
344 sh_serial_state
*s
= opaque
;
345 return sh_serial_ioport_read(s
, addr
);
348 static void sh_serial_write (void *opaque
,
349 target_phys_addr_t addr
, uint32_t value
)
351 sh_serial_state
*s
= opaque
;
352 sh_serial_ioport_write(s
, addr
, value
);
355 static CPUReadMemoryFunc
*sh_serial_readfn
[] = {
361 static CPUWriteMemoryFunc
*sh_serial_writefn
[] = {
367 void sh_serial_init (target_phys_addr_t base
, int feat
,
368 uint32_t freq
, CharDriverState
*chr
,
378 s
= qemu_mallocz(sizeof(sh_serial_state
));
381 s
->flags
= SH_SERIAL_FLAG_TEND
| SH_SERIAL_FLAG_TDE
;
386 s
->scr
= 1 << 5; /* pretend that TX is enabled so early printk works */
389 if (feat
& SH_SERIAL_FEAT_SCIF
) {
396 sh_serial_clear_fifo(s
);
398 s_io_memory
= cpu_register_io_memory(0, sh_serial_readfn
,
399 sh_serial_writefn
, s
);
400 cpu_register_physical_memory(P4ADDR(base
), 0x28, s_io_memory
);
401 cpu_register_physical_memory(A7ADDR(base
), 0x28, s_io_memory
);
406 qemu_chr_add_handlers(chr
, sh_serial_can_receive1
, sh_serial_receive1
,