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
28 #include "qemu/osdep.h"
30 #include "hw/sh4/sh.h"
31 #include "chardev/char-fe.h"
32 #include "qapi/error.h"
33 #include "qemu/timer.h"
35 //#define DEBUG_SERIAL
37 #define SH_SERIAL_FLAG_TEND (1 << 0)
38 #define SH_SERIAL_FLAG_TDE (1 << 1)
39 #define SH_SERIAL_FLAG_RDF (1 << 2)
40 #define SH_SERIAL_FLAG_BRK (1 << 3)
41 #define SH_SERIAL_FLAG_DR (1 << 4)
43 #define SH_RX_FIFO_LENGTH (16)
47 MemoryRegion iomem_p4;
48 MemoryRegion iomem_a7;
52 uint8_t dr; /* ftdr / tdr */
53 uint8_t sr; /* fsr / ssr */
57 uint8_t rx_fifo[SH_RX_FIFO_LENGTH]; /* frdr / rdr */
68 QEMUTimer *fifo_timeout_timer;
69 uint64_t etu; /* Elementary Time Unit (ns) */
78 static void sh_serial_clear_fifo(sh_serial_state * s)
80 memset(s->rx_fifo, 0, SH_RX_FIFO_LENGTH);
86 static void sh_serial_write(void *opaque, hwaddr offs,
87 uint64_t val, unsigned size)
89 sh_serial_state *s = opaque;
93 printf("sh_serial: write offs=0x%02x val=0x%02x\n",
98 s->smr = val & ((s->feat & SH_SERIAL_FEAT_SCIF) ? 0x7b : 0xff);
104 /* TODO : For SH7751, SCIF mask should be 0xfb. */
105 s->scr = val & ((s->feat & SH_SERIAL_FEAT_SCIF) ? 0xfa : 0xff);
106 if (!(val & (1 << 5)))
107 s->flags |= SH_SERIAL_FLAG_TEND;
108 if ((s->feat & SH_SERIAL_FEAT_SCIF) && s->txi) {
109 qemu_set_irq(s->txi, val & (1 << 7));
111 if (!(val & (1 << 6))) {
112 qemu_set_irq(s->rxi, 0);
115 case 0x0c: /* FTDR / TDR */
116 if (qemu_chr_fe_backend_connected(&s->chr)) {
118 /* XXX this blocks entire thread. Rewrite to use
119 * qemu_chr_fe_write and background I/O callbacks */
120 qemu_chr_fe_write_all(&s->chr, &ch, 1);
123 s->flags &= ~SH_SERIAL_FLAG_TDE;
126 case 0x14: /* FRDR / RDR */
131 if (s->feat & SH_SERIAL_FEAT_SCIF) {
134 if (!(val & (1 << 6)))
135 s->flags &= ~SH_SERIAL_FLAG_TEND;
136 if (!(val & (1 << 5)))
137 s->flags &= ~SH_SERIAL_FLAG_TDE;
138 if (!(val & (1 << 4)))
139 s->flags &= ~SH_SERIAL_FLAG_BRK;
140 if (!(val & (1 << 1)))
141 s->flags &= ~SH_SERIAL_FLAG_RDF;
142 if (!(val & (1 << 0)))
143 s->flags &= ~SH_SERIAL_FLAG_DR;
145 if (!(val & (1 << 1)) || !(val & (1 << 0))) {
147 qemu_set_irq(s->rxi, 0);
153 switch ((val >> 6) & 3) {
167 if (val & (1 << 1)) {
168 sh_serial_clear_fifo(s);
173 case 0x20: /* SPTR */
174 s->sptr = val & 0xf3;
191 s->sptr = val & 0x8f;
196 fprintf(stderr, "sh_serial: unsupported write to 0x%02"
197 HWADDR_PRIx "\n", offs);
201 static uint64_t sh_serial_read(void *opaque, hwaddr offs,
204 sh_serial_state *s = opaque;
223 if (s->feat & SH_SERIAL_FEAT_SCIF) {
233 if (s->flags & SH_SERIAL_FLAG_TEND)
235 if (s->flags & SH_SERIAL_FLAG_TDE)
237 if (s->flags & SH_SERIAL_FLAG_BRK)
239 if (s->flags & SH_SERIAL_FLAG_RDF)
241 if (s->flags & SH_SERIAL_FLAG_DR)
244 if (s->scr & (1 << 5))
245 s->flags |= SH_SERIAL_FLAG_TDE | SH_SERIAL_FLAG_TEND;
250 ret = s->rx_fifo[s->rx_tail++];
252 if (s->rx_tail == SH_RX_FIFO_LENGTH)
254 if (s->rx_cnt < s->rtrg)
255 s->flags &= ~SH_SERIAL_FLAG_RDF;
291 printf("sh_serial: read offs=0x%02x val=0x%x\n",
295 if (ret & ~((1 << 16) - 1)) {
296 fprintf(stderr, "sh_serial: unsupported read from 0x%02"
297 HWADDR_PRIx "\n", offs);
304 static int sh_serial_can_receive(sh_serial_state *s)
306 return s->scr & (1 << 4);
309 static void sh_serial_receive_break(sh_serial_state *s)
311 if (s->feat & SH_SERIAL_FEAT_SCIF)
315 static int sh_serial_can_receive1(void *opaque)
317 sh_serial_state *s = opaque;
318 return sh_serial_can_receive(s);
321 static void sh_serial_timeout_int(void *opaque)
323 sh_serial_state *s = opaque;
325 s->flags |= SH_SERIAL_FLAG_RDF;
326 if (s->scr & (1 << 6) && s->rxi) {
327 qemu_set_irq(s->rxi, 1);
331 static void sh_serial_receive1(void *opaque, const uint8_t *buf, int size)
333 sh_serial_state *s = opaque;
335 if (s->feat & SH_SERIAL_FEAT_SCIF) {
337 for (i = 0; i < size; i++) {
338 if (s->rx_cnt < SH_RX_FIFO_LENGTH) {
339 s->rx_fifo[s->rx_head++] = buf[i];
340 if (s->rx_head == SH_RX_FIFO_LENGTH) {
344 if (s->rx_cnt >= s->rtrg) {
345 s->flags |= SH_SERIAL_FLAG_RDF;
346 if (s->scr & (1 << 6) && s->rxi) {
347 timer_del(s->fifo_timeout_timer);
348 qemu_set_irq(s->rxi, 1);
351 timer_mod(s->fifo_timeout_timer,
352 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 15 * s->etu);
357 s->rx_fifo[0] = buf[0];
361 static void sh_serial_event(void *opaque, int event)
363 sh_serial_state *s = opaque;
364 if (event == CHR_EVENT_BREAK)
365 sh_serial_receive_break(s);
368 static const MemoryRegionOps sh_serial_ops = {
369 .read = sh_serial_read,
370 .write = sh_serial_write,
371 .endianness = DEVICE_NATIVE_ENDIAN,
374 void sh_serial_init(MemoryRegion *sysmem,
375 hwaddr base, int feat,
376 uint32_t freq, Chardev *chr,
385 s = g_malloc0(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 memory_region_init_io(&s->iomem, NULL, &sh_serial_ops, s,
406 "serial", 0x100000000ULL);
408 memory_region_init_alias(&s->iomem_p4, NULL, "serial-p4", &s->iomem,
410 memory_region_add_subregion(sysmem, P4ADDR(base), &s->iomem_p4);
412 memory_region_init_alias(&s->iomem_a7, NULL, "serial-a7", &s->iomem,
414 memory_region_add_subregion(sysmem, A7ADDR(base), &s->iomem_a7);
417 qemu_chr_fe_init(&s->chr, chr, &error_abort);
418 qemu_chr_fe_set_handlers(&s->chr, sh_serial_can_receive1,
420 sh_serial_event, NULL, s, NULL, true);
423 s->fifo_timeout_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
424 sh_serial_timeout_int, s);
425 s->etu = NANOSECONDS_PER_SECOND / 9600;