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 */
56 target_phys_addr_t base;
64 struct intc_source *eri;
65 struct intc_source *rxi;
66 struct intc_source *txi;
67 struct intc_source *tei;
68 struct intc_source *bri;
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_ioport_write(void *opaque, uint32_t offs, uint32_t val)
81 sh_serial_state *s = opaque;
85 printf("sh_serial: write base=0x%08lx offs=0x%02x val=0x%02x\n",
86 (unsigned long) s->base, offs, val);
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 if (!(val & (1 << 5)))
99 s->flags |= SH_SERIAL_FLAG_TEND;
100 if ((s->feat & SH_SERIAL_FEAT_SCIF) && s->txi) {
101 if ((val & (1 << 7)) && !(s->txi->asserted))
102 sh_intc_toggle_source(s->txi, 0, 1);
103 else if (!(val & (1 << 7)) && s->txi->asserted)
104 sh_intc_toggle_source(s->txi, 0, -1);
106 if (!(val & (1 << 6)) && s->rxi->asserted) {
107 sh_intc_toggle_source(s->rxi, 0, -1);
110 case 0x0c: /* FTDR / TDR */
113 qemu_chr_write(s->chr, &ch, 1);
116 s->flags &= ~SH_SERIAL_FLAG_TDE;
119 case 0x14: /* FRDR / RDR */
124 if (s->feat & SH_SERIAL_FEAT_SCIF) {
127 if (!(val & (1 << 6)))
128 s->flags &= ~SH_SERIAL_FLAG_TEND;
129 if (!(val & (1 << 5)))
130 s->flags &= ~SH_SERIAL_FLAG_TDE;
131 if (!(val & (1 << 4)))
132 s->flags &= ~SH_SERIAL_FLAG_BRK;
133 if (!(val & (1 << 1)))
134 s->flags &= ~SH_SERIAL_FLAG_RDF;
135 if (!(val & (1 << 0)))
136 s->flags &= ~SH_SERIAL_FLAG_DR;
138 if (!(val & (1 << 1)) || !(val & (1 << 0))) {
139 if (s->rxi && s->rxi->asserted) {
140 sh_intc_toggle_source(s->rxi, 0, -1);
146 switch ((val >> 6) & 3) {
160 if (val & (1 << 1)) {
161 sh_serial_clear_fifo(s);
166 case 0x20: /* SPTR */
167 s->sptr = val & 0xf3;
189 fprintf(stderr, "sh_serial: unsupported write to 0x%02x\n", offs);
193 static uint32_t sh_serial_ioport_read(void *opaque, uint32_t offs)
195 sh_serial_state *s = opaque;
214 if (s->feat & SH_SERIAL_FEAT_SCIF) {
224 if (s->flags & SH_SERIAL_FLAG_TEND)
226 if (s->flags & SH_SERIAL_FLAG_TDE)
228 if (s->flags & SH_SERIAL_FLAG_BRK)
230 if (s->flags & SH_SERIAL_FLAG_RDF)
232 if (s->flags & SH_SERIAL_FLAG_DR)
235 if (s->scr & (1 << 5))
236 s->flags |= SH_SERIAL_FLAG_TDE | SH_SERIAL_FLAG_TEND;
241 ret = s->rx_fifo[s->rx_tail++];
243 if (s->rx_tail == SH_RX_FIFO_LENGTH)
245 if (s->rx_cnt < s->rtrg)
246 s->flags &= ~SH_SERIAL_FLAG_RDF;
284 printf("sh_serial: read base=0x%08lx offs=0x%02x val=0x%x\n",
285 (unsigned long) s->base, offs, ret);
288 if (ret & ~((1 << 16) - 1)) {
289 fprintf(stderr, "sh_serial: unsupported read from 0x%02x\n", offs);
296 static int sh_serial_can_receive(sh_serial_state *s)
298 return s->scr & (1 << 4);
301 static void sh_serial_receive_byte(sh_serial_state *s, int ch)
303 if (s->feat & SH_SERIAL_FEAT_SCIF) {
304 if (s->rx_cnt < SH_RX_FIFO_LENGTH) {
305 s->rx_fifo[s->rx_head++] = ch;
306 if (s->rx_head == SH_RX_FIFO_LENGTH)
309 if (s->rx_cnt >= s->rtrg) {
310 s->flags |= SH_SERIAL_FLAG_RDF;
311 if (s->scr & (1 << 6) && s->rxi) {
312 sh_intc_toggle_source(s->rxi, 0, 1);
321 static void sh_serial_receive_break(sh_serial_state *s)
323 if (s->feat & SH_SERIAL_FEAT_SCIF)
327 static int sh_serial_can_receive1(void *opaque)
329 sh_serial_state *s = opaque;
330 return sh_serial_can_receive(s);
333 static void sh_serial_receive1(void *opaque, const uint8_t *buf, int size)
335 sh_serial_state *s = opaque;
336 sh_serial_receive_byte(s, buf[0]);
339 static void sh_serial_event(void *opaque, int event)
341 sh_serial_state *s = opaque;
342 if (event == CHR_EVENT_BREAK)
343 sh_serial_receive_break(s);
346 static uint32_t sh_serial_read (void *opaque, target_phys_addr_t addr)
348 sh_serial_state *s = opaque;
349 return sh_serial_ioport_read(s, addr - s->base);
352 static void sh_serial_write (void *opaque,
353 target_phys_addr_t addr, uint32_t value)
355 sh_serial_state *s = opaque;
356 sh_serial_ioport_write(s, addr - s->base, value);
359 static CPUReadMemoryFunc *sh_serial_readfn[] = {
365 static CPUWriteMemoryFunc *sh_serial_writefn[] = {
371 void sh_serial_init (target_phys_addr_t base, int feat,
372 uint32_t freq, CharDriverState *chr,
373 struct intc_source *eri_source,
374 struct intc_source *rxi_source,
375 struct intc_source *txi_source,
376 struct intc_source *tei_source,
377 struct intc_source *bri_source)
382 s = qemu_mallocz(sizeof(sh_serial_state));
388 s->flags = SH_SERIAL_FLAG_TEND | SH_SERIAL_FLAG_TDE;
393 s->scr = 1 << 5; /* pretend that TX is enabled so early printk works */
396 if (feat & SH_SERIAL_FEAT_SCIF) {
403 sh_serial_clear_fifo(s);
405 s_io_memory = cpu_register_io_memory(0, sh_serial_readfn,
406 sh_serial_writefn, s);
407 cpu_register_physical_memory(base, 0x28, s_io_memory);
412 qemu_chr_add_handlers(chr, sh_serial_can_receive1, sh_serial_receive1,