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"
31 //#define DEBUG_SERIAL
33 #define SH_SERIAL_FLAG_TEND (1 << 0)
34 #define SH_SERIAL_FLAG_TDE (1 << 1)
35 #define SH_SERIAL_FLAG_RDF (1 << 2)
36 #define SH_SERIAL_FLAG_BRK (1 << 3)
37 #define SH_SERIAL_FLAG_DR (1 << 4)
39 #define SH_RX_FIFO_LENGTH (16)
45 uint8_t dr
; /* ftdr / tdr */
46 uint8_t sr
; /* fsr / ssr */
50 uint8_t rx_fifo
[SH_RX_FIFO_LENGTH
]; /* frdr / rdr */
69 static void sh_serial_update_handlers(sh_serial_state
*s
);
71 static void sh_serial_clear_fifo(sh_serial_state
* s
)
73 memset(s
->rx_fifo
, 0, SH_RX_FIFO_LENGTH
);
79 static void sh_serial_write(void *opaque
, uint32_t offs
, uint32_t val
)
81 sh_serial_state
*s
= opaque
;
85 printf("sh_serial: write offs=0x%02x val=0x%02x\n",
90 s
->smr
= val
& ((s
->feat
& SH_SERIAL_FEAT_SCIF
) ? 0x7b : 0xff);
96 /* TODO : For SH7751, SCIF mask should be 0xfb. */
97 s
->scr
= val
& ((s
->feat
& SH_SERIAL_FEAT_SCIF
) ? 0xfa : 0xff);
98 sh_serial_update_handlers(s
);
99 if (!(val
& (1 << 5)))
100 s
->flags
|= SH_SERIAL_FLAG_TEND
;
101 if ((s
->feat
& SH_SERIAL_FEAT_SCIF
) && s
->txi
) {
102 qemu_set_irq(s
->txi
, val
& (1 << 7));
104 if (!(val
& (1 << 6))) {
105 qemu_set_irq(s
->rxi
, 0);
108 case 0x0c: /* FTDR / TDR */
111 qemu_chr_fe_write(s
->chr
, &ch
, 1);
114 s
->flags
&= ~SH_SERIAL_FLAG_TDE
;
117 case 0x14: /* FRDR / RDR */
122 if (s
->feat
& SH_SERIAL_FEAT_SCIF
) {
125 if (!(val
& (1 << 6)))
126 s
->flags
&= ~SH_SERIAL_FLAG_TEND
;
127 if (!(val
& (1 << 5)))
128 s
->flags
&= ~SH_SERIAL_FLAG_TDE
;
129 if (!(val
& (1 << 4)))
130 s
->flags
&= ~SH_SERIAL_FLAG_BRK
;
131 if (!(val
& (1 << 1)))
132 s
->flags
&= ~SH_SERIAL_FLAG_RDF
;
133 if (!(val
& (1 << 0)))
134 s
->flags
&= ~SH_SERIAL_FLAG_DR
;
136 if (!(val
& (1 << 1)) || !(val
& (1 << 0))) {
138 qemu_set_irq(s
->rxi
, 0);
144 switch ((val
>> 6) & 3) {
158 if (val
& (1 << 1)) {
159 sh_serial_clear_fifo(s
);
164 case 0x20: /* SPTR */
165 s
->sptr
= val
& 0xf3;
182 s
->sptr
= val
& 0x8f;
187 fprintf(stderr
, "sh_serial: unsupported write to 0x%02x\n", offs
);
191 static uint32_t sh_serial_read(void *opaque
, uint32_t offs
)
193 sh_serial_state
*s
= opaque
;
212 if (s
->feat
& SH_SERIAL_FEAT_SCIF
) {
222 if (s
->flags
& SH_SERIAL_FLAG_TEND
)
224 if (s
->flags
& SH_SERIAL_FLAG_TDE
)
226 if (s
->flags
& SH_SERIAL_FLAG_BRK
)
228 if (s
->flags
& SH_SERIAL_FLAG_RDF
)
230 if (s
->flags
& SH_SERIAL_FLAG_DR
)
233 if (s
->scr
& (1 << 5))
234 s
->flags
|= SH_SERIAL_FLAG_TDE
| SH_SERIAL_FLAG_TEND
;
239 ret
= s
->rx_fifo
[s
->rx_tail
++];
241 if (s
->rx_tail
== SH_RX_FIFO_LENGTH
)
243 if (s
->rx_cnt
< s
->rtrg
)
244 s
->flags
&= ~SH_SERIAL_FLAG_RDF
;
282 printf("sh_serial: read offs=0x%02x val=0x%x\n",
286 if (ret
& ~((1 << 16) - 1)) {
287 fprintf(stderr
, "sh_serial: unsupported read from 0x%02x\n", offs
);
294 static int sh_serial_can_receive(sh_serial_state
*s
)
296 return s
->scr
& (1 << 4);
299 static void sh_serial_receive_break(sh_serial_state
*s
)
301 if (s
->feat
& SH_SERIAL_FEAT_SCIF
)
305 static void sh_serial_receive1(sh_serial_state
*s
, const uint8_t *buf
, int size
)
307 if (s
->feat
& SH_SERIAL_FEAT_SCIF
) {
309 for (i
= 0; i
< size
; i
++) {
310 if (s
->rx_cnt
< SH_RX_FIFO_LENGTH
) {
311 s
->rx_fifo
[s
->rx_head
++] = buf
[i
];
312 if (s
->rx_head
== SH_RX_FIFO_LENGTH
) {
316 if (s
->rx_cnt
>= s
->rtrg
) {
317 s
->flags
|= SH_SERIAL_FLAG_RDF
;
318 if (s
->scr
& (1 << 6) && s
->rxi
) {
319 qemu_set_irq(s
->rxi
, 1);
325 s
->rx_fifo
[0] = buf
[0];
329 static void sh_serial_receive_handler(void *opaque
)
331 sh_serial_state
*s
= opaque
;
335 size
= sh_serial_can_receive(s
);
336 size
= MIN(size
, sizeof(buf
));
337 size
= qemu_chr_fe_read(s
->chr
, buf
, size
);
339 sh_serial_receive1(s
, buf
, size
);
342 static int sh_serial_event(void *opaque
, int event
, void *data
)
344 sh_serial_state
*s
= opaque
;
345 if (event
== CHR_EVENT_BREAK
)
346 sh_serial_receive_break(s
);
350 static CPUReadMemoryFunc
* const sh_serial_readfn
[] = {
356 static CPUWriteMemoryFunc
* const sh_serial_writefn
[] = {
362 static void sh_serial_update_handlers(sh_serial_state
*s
)
364 if (sh_serial_can_receive(s
) > 0) {
365 qemu_chr_fe_set_handlers(s
->chr
, sh_serial_receive_handler
,
366 NULL
, sh_serial_event
, s
);
368 qemu_chr_fe_set_handlers(s
->chr
, NULL
, NULL
, sh_serial_event
, s
);
372 void sh_serial_init (target_phys_addr_t base
, int feat
,
373 uint32_t freq
, CharDriverState
*chr
,
383 s
= qemu_mallocz(sizeof(sh_serial_state
));
386 s
->flags
= SH_SERIAL_FLAG_TEND
| SH_SERIAL_FLAG_TDE
;
391 s
->scr
= 1 << 5; /* pretend that TX is enabled so early printk works */
394 if (feat
& SH_SERIAL_FEAT_SCIF
) {
401 sh_serial_clear_fifo(s
);
403 s_io_memory
= cpu_register_io_memory(sh_serial_readfn
,
404 sh_serial_writefn
, s
,
405 DEVICE_NATIVE_ENDIAN
);
406 cpu_register_physical_memory(P4ADDR(base
), 0x28, s_io_memory
);
407 cpu_register_physical_memory(A7ADDR(base
), 0x28, s_io_memory
);
412 qemu_chr_fe_open(chr
);
413 sh_serial_update_handlers(s
);