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_clear_fifo(sh_serial_state
* s
)
71 memset(s
->rx_fifo
, 0, SH_RX_FIFO_LENGTH
);
77 static void sh_serial_ioport_write(void *opaque
, uint32_t offs
, uint32_t val
)
79 sh_serial_state
*s
= opaque
;
83 printf("sh_serial: write offs=0x%02x val=0x%02x\n",
88 s
->smr
= val
& ((s
->feat
& SH_SERIAL_FEAT_SCIF
) ? 0x7b : 0xff);
94 /* TODO : For SH7751, SCIF mask should be 0xfb. */
95 s
->scr
= val
& ((s
->feat
& SH_SERIAL_FEAT_SCIF
) ? 0xfa : 0xff);
96 if (!(val
& (1 << 5)))
97 s
->flags
|= SH_SERIAL_FLAG_TEND
;
98 if ((s
->feat
& SH_SERIAL_FEAT_SCIF
) && s
->txi
) {
99 qemu_set_irq(s
->txi
, val
& (1 << 7));
101 if (!(val
& (1 << 6))) {
102 qemu_set_irq(s
->rxi
, 0);
105 case 0x0c: /* FTDR / TDR */
108 qemu_chr_write(s
->chr
, &ch
, 1);
111 s
->flags
&= ~SH_SERIAL_FLAG_TDE
;
114 case 0x14: /* FRDR / RDR */
119 if (s
->feat
& SH_SERIAL_FEAT_SCIF
) {
122 if (!(val
& (1 << 6)))
123 s
->flags
&= ~SH_SERIAL_FLAG_TEND
;
124 if (!(val
& (1 << 5)))
125 s
->flags
&= ~SH_SERIAL_FLAG_TDE
;
126 if (!(val
& (1 << 4)))
127 s
->flags
&= ~SH_SERIAL_FLAG_BRK
;
128 if (!(val
& (1 << 1)))
129 s
->flags
&= ~SH_SERIAL_FLAG_RDF
;
130 if (!(val
& (1 << 0)))
131 s
->flags
&= ~SH_SERIAL_FLAG_DR
;
133 if (!(val
& (1 << 1)) || !(val
& (1 << 0))) {
135 qemu_set_irq(s
->rxi
, 0);
141 switch ((val
>> 6) & 3) {
155 if (val
& (1 << 1)) {
156 sh_serial_clear_fifo(s
);
161 case 0x20: /* SPTR */
162 s
->sptr
= val
& 0xf3;
179 s
->sptr
= val
& 0x8f;
184 fprintf(stderr
, "sh_serial: unsupported write to 0x%02x\n", offs
);
188 static uint32_t sh_serial_ioport_read(void *opaque
, uint32_t offs
)
190 sh_serial_state
*s
= opaque
;
209 if (s
->feat
& SH_SERIAL_FEAT_SCIF
) {
219 if (s
->flags
& SH_SERIAL_FLAG_TEND
)
221 if (s
->flags
& SH_SERIAL_FLAG_TDE
)
223 if (s
->flags
& SH_SERIAL_FLAG_BRK
)
225 if (s
->flags
& SH_SERIAL_FLAG_RDF
)
227 if (s
->flags
& SH_SERIAL_FLAG_DR
)
230 if (s
->scr
& (1 << 5))
231 s
->flags
|= SH_SERIAL_FLAG_TDE
| SH_SERIAL_FLAG_TEND
;
236 ret
= s
->rx_fifo
[s
->rx_tail
++];
238 if (s
->rx_tail
== SH_RX_FIFO_LENGTH
)
240 if (s
->rx_cnt
< s
->rtrg
)
241 s
->flags
&= ~SH_SERIAL_FLAG_RDF
;
279 printf("sh_serial: read offs=0x%02x val=0x%x\n",
283 if (ret
& ~((1 << 16) - 1)) {
284 fprintf(stderr
, "sh_serial: unsupported read from 0x%02x\n", offs
);
291 static int sh_serial_can_receive(sh_serial_state
*s
)
293 return s
->scr
& (1 << 4);
296 static void sh_serial_receive_byte(sh_serial_state
*s
, int ch
)
298 if (s
->feat
& SH_SERIAL_FEAT_SCIF
) {
299 if (s
->rx_cnt
< SH_RX_FIFO_LENGTH
) {
300 s
->rx_fifo
[s
->rx_head
++] = ch
;
301 if (s
->rx_head
== SH_RX_FIFO_LENGTH
)
304 if (s
->rx_cnt
>= s
->rtrg
) {
305 s
->flags
|= SH_SERIAL_FLAG_RDF
;
306 if (s
->scr
& (1 << 6) && s
->rxi
) {
307 qemu_set_irq(s
->rxi
, 1);
316 static void sh_serial_receive_break(sh_serial_state
*s
)
318 if (s
->feat
& SH_SERIAL_FEAT_SCIF
)
322 static int sh_serial_can_receive1(void *opaque
)
324 sh_serial_state
*s
= opaque
;
325 return sh_serial_can_receive(s
);
328 static void sh_serial_receive1(void *opaque
, const uint8_t *buf
, int size
)
330 sh_serial_state
*s
= opaque
;
331 sh_serial_receive_byte(s
, buf
[0]);
334 static void sh_serial_event(void *opaque
, int event
)
336 sh_serial_state
*s
= opaque
;
337 if (event
== CHR_EVENT_BREAK
)
338 sh_serial_receive_break(s
);
341 static uint32_t sh_serial_read (void *opaque
, target_phys_addr_t addr
)
343 sh_serial_state
*s
= opaque
;
344 return sh_serial_ioport_read(s
, addr
);
347 static void sh_serial_write (void *opaque
,
348 target_phys_addr_t addr
, uint32_t value
)
350 sh_serial_state
*s
= opaque
;
351 sh_serial_ioport_write(s
, addr
, value
);
354 static CPUReadMemoryFunc
*sh_serial_readfn
[] = {
360 static CPUWriteMemoryFunc
*sh_serial_writefn
[] = {
366 void sh_serial_init (target_phys_addr_t base
, int feat
,
367 uint32_t freq
, CharDriverState
*chr
,
377 s
= qemu_mallocz(sizeof(sh_serial_state
));
380 s
->flags
= SH_SERIAL_FLAG_TEND
| SH_SERIAL_FLAG_TDE
;
385 s
->scr
= 1 << 5; /* pretend that TX is enabled so early printk works */
388 if (feat
& SH_SERIAL_FEAT_SCIF
) {
395 sh_serial_clear_fifo(s
);
397 s_io_memory
= cpu_register_io_memory(sh_serial_readfn
,
398 sh_serial_writefn
, s
);
399 cpu_register_physical_memory(P4ADDR(base
), 0x28, s_io_memory
);
400 cpu_register_physical_memory(A7ADDR(base
), 0x28, s_io_memory
);
405 qemu_chr_add_handlers(chr
, sh_serial_can_receive1
, sh_serial_receive1
,