2 * Renesas Serial Communication Interface
4 * Datasheet: RX62N Group, RX621 Group User's Manual: Hardware
5 * (Rev.1.40 R01UH0033EJ0140)
7 * Copyright (c) 2019 Yoshinori Sato
9 * SPDX-License-Identifier: GPL-2.0-or-later
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms and conditions of the GNU General Public License,
13 * version 2 or later, as published by the Free Software Foundation.
15 * This program is distributed in the hope it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20 * You should have received a copy of the GNU General Public License along with
21 * this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "qemu/osdep.h"
27 #include "hw/registerfields.h"
28 #include "hw/qdev-properties.h"
29 #include "hw/char/renesas_sci.h"
30 #include "migration/vmstate.h"
32 /* SCI register map */
36 FIELD(SMR
, STOP
, 3, 1)
44 FIELD(SCR
, TEIE
, 2, 1)
45 FIELD(SCR
, MPIE
, 3, 1)
52 FIELD(SSR
, MPBT
, 0, 1)
54 FIELD(SSR
, TEND
, 2, 1)
58 FIELD(SSR
, ORER
, 5, 1)
59 FIELD(SSR
, RDRF
, 6, 1)
60 FIELD(SSR
, TDRE
, 7, 1)
63 FIELD(SCMR
, SMIF
, 0, 1)
64 FIELD(SCMR
, SINV
, 2, 1)
65 FIELD(SCMR
, SDIR
, 3, 1)
66 FIELD(SCMR
, BCP2
, 7, 1)
68 FIELD(SEMR
, ACS0
, 0, 1)
69 FIELD(SEMR
, ABCS
, 4, 1)
71 static int can_receive(void *opaque
)
73 RSCIState
*sci
= RSCI(opaque
);
74 if (sci
->rx_next
> qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
)) {
77 return FIELD_EX8(sci
->scr
, SCR
, RE
);
81 static void receive(void *opaque
, const uint8_t *buf
, int size
)
83 RSCIState
*sci
= RSCI(opaque
);
84 sci
->rx_next
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + sci
->trtime
;
85 if (FIELD_EX8(sci
->ssr
, SSR
, RDRF
) || size
> 1) {
86 sci
->ssr
= FIELD_DP8(sci
->ssr
, SSR
, ORER
, 1);
87 if (FIELD_EX8(sci
->scr
, SCR
, RIE
)) {
88 qemu_set_irq(sci
->irq
[ERI
], 1);
92 sci
->ssr
= FIELD_DP8(sci
->ssr
, SSR
, RDRF
, 1);
93 if (FIELD_EX8(sci
->scr
, SCR
, RIE
)) {
94 qemu_irq_pulse(sci
->irq
[RXI
]);
99 static void send_byte(RSCIState
*sci
)
101 if (qemu_chr_fe_backend_connected(&sci
->chr
)) {
102 qemu_chr_fe_write_all(&sci
->chr
, &sci
->tdr
, 1);
104 timer_mod(&sci
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + sci
->trtime
);
105 sci
->ssr
= FIELD_DP8(sci
->ssr
, SSR
, TEND
, 0);
106 sci
->ssr
= FIELD_DP8(sci
->ssr
, SSR
, TDRE
, 1);
107 qemu_set_irq(sci
->irq
[TEI
], 0);
108 if (FIELD_EX8(sci
->scr
, SCR
, TIE
)) {
109 qemu_irq_pulse(sci
->irq
[TXI
]);
113 static void txend(void *opaque
)
115 RSCIState
*sci
= RSCI(opaque
);
116 if (!FIELD_EX8(sci
->ssr
, SSR
, TDRE
)) {
119 sci
->ssr
= FIELD_DP8(sci
->ssr
, SSR
, TEND
, 1);
120 if (FIELD_EX8(sci
->scr
, SCR
, TEIE
)) {
121 qemu_set_irq(sci
->irq
[TEI
], 1);
126 static void update_trtime(RSCIState
*sci
)
129 sci
->trtime
= 8 - FIELD_EX8(sci
->smr
, SMR
, CHR
);
130 sci
->trtime
+= FIELD_EX8(sci
->smr
, SMR
, PE
);
131 sci
->trtime
+= FIELD_EX8(sci
->smr
, SMR
, STOP
) + 1;
132 /* x bit transmit time (32 * divrate * brr) / base freq */
133 sci
->trtime
*= 32 * sci
->brr
;
134 sci
->trtime
*= 1 << (2 * FIELD_EX8(sci
->smr
, SMR
, CKS
));
135 sci
->trtime
*= NANOSECONDS_PER_SECOND
;
136 sci
->trtime
/= sci
->input_freq
;
139 static bool sci_is_tr_enabled(RSCIState
*sci
)
141 return FIELD_EX8(sci
->scr
, SCR
, TE
) || FIELD_EX8(sci
->scr
, SCR
, RE
);
144 static void sci_write(void *opaque
, hwaddr offset
, uint64_t val
, unsigned size
)
146 RSCIState
*sci
= RSCI(opaque
);
150 if (!sci_is_tr_enabled(sci
)) {
156 if (!sci_is_tr_enabled(sci
)) {
163 if (FIELD_EX8(sci
->scr
, SCR
, TE
)) {
164 sci
->ssr
= FIELD_DP8(sci
->ssr
, SSR
, TDRE
, 1);
165 sci
->ssr
= FIELD_DP8(sci
->ssr
, SSR
, TEND
, 1);
166 if (FIELD_EX8(sci
->scr
, SCR
, TIE
)) {
167 qemu_irq_pulse(sci
->irq
[TXI
]);
170 if (!FIELD_EX8(sci
->scr
, SCR
, TEIE
)) {
171 qemu_set_irq(sci
->irq
[TEI
], 0);
173 if (!FIELD_EX8(sci
->scr
, SCR
, RIE
)) {
174 qemu_set_irq(sci
->irq
[ERI
], 0);
179 if (FIELD_EX8(sci
->ssr
, SSR
, TEND
)) {
182 sci
->ssr
= FIELD_DP8(sci
->ssr
, SSR
, TDRE
, 0);
186 sci
->ssr
= FIELD_DP8(sci
->ssr
, SSR
, MPBT
,
187 FIELD_EX8(val
, SSR
, MPBT
));
188 sci
->ssr
= FIELD_DP8(sci
->ssr
, SSR
, ERR
,
189 FIELD_EX8(val
, SSR
, ERR
) & 0x07);
190 if (FIELD_EX8(sci
->read_ssr
, SSR
, ERR
) &&
191 FIELD_EX8(sci
->ssr
, SSR
, ERR
) == 0) {
192 qemu_set_irq(sci
->irq
[ERI
], 0);
196 qemu_log_mask(LOG_GUEST_ERROR
, "reneas_sci: RDR is read only.\n");
199 sci
->scmr
= val
; break;
200 case A_SEMR
: /* SEMR */
201 sci
->semr
= val
; break;
203 qemu_log_mask(LOG_UNIMP
, "renesas_sci: Register 0x%" HWADDR_PRIX
" "
209 static uint64_t sci_read(void *opaque
, hwaddr offset
, unsigned size
)
211 RSCIState
*sci
= RSCI(opaque
);
223 sci
->read_ssr
= sci
->ssr
;
226 sci
->ssr
= FIELD_DP8(sci
->ssr
, SSR
, RDRF
, 0);
233 qemu_log_mask(LOG_UNIMP
, "renesas_sci: Register 0x%" HWADDR_PRIX
234 " not implemented.\n", offset
);
239 static const MemoryRegionOps sci_ops
= {
242 .endianness
= DEVICE_NATIVE_ENDIAN
,
243 .impl
.max_access_size
= 1,
244 .valid
.max_access_size
= 1,
247 static void rsci_reset(DeviceState
*dev
)
249 RSCIState
*sci
= RSCI(dev
);
250 sci
->smr
= sci
->scr
= 0x00;
257 sci
->rx_next
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
260 static void sci_event(void *opaque
, QEMUChrEvent event
)
262 RSCIState
*sci
= RSCI(opaque
);
263 if (event
== CHR_EVENT_BREAK
) {
264 sci
->ssr
= FIELD_DP8(sci
->ssr
, SSR
, FER
, 1);
265 if (FIELD_EX8(sci
->scr
, SCR
, RIE
)) {
266 qemu_set_irq(sci
->irq
[ERI
], 1);
271 static void rsci_realize(DeviceState
*dev
, Error
**errp
)
273 RSCIState
*sci
= RSCI(dev
);
275 if (sci
->input_freq
== 0) {
276 qemu_log_mask(LOG_GUEST_ERROR
,
277 "renesas_sci: input-freq property must be set.");
280 qemu_chr_fe_set_handlers(&sci
->chr
, can_receive
, receive
,
281 sci_event
, NULL
, sci
, NULL
, true);
284 static void rsci_init(Object
*obj
)
286 SysBusDevice
*d
= SYS_BUS_DEVICE(obj
);
287 RSCIState
*sci
= RSCI(obj
);
290 memory_region_init_io(&sci
->memory
, OBJECT(sci
), &sci_ops
,
291 sci
, "renesas-sci", 0x8);
292 sysbus_init_mmio(d
, &sci
->memory
);
294 for (i
= 0; i
< SCI_NR_IRQ
; i
++) {
295 sysbus_init_irq(d
, &sci
->irq
[i
]);
297 timer_init_ns(&sci
->timer
, QEMU_CLOCK_VIRTUAL
, txend
, sci
);
300 static const VMStateDescription vmstate_rsci
= {
301 .name
= "renesas-sci",
303 .minimum_version_id
= 1,
304 .fields
= (VMStateField
[]) {
305 VMSTATE_INT64(trtime
, RSCIState
),
306 VMSTATE_INT64(rx_next
, RSCIState
),
307 VMSTATE_UINT8(smr
, RSCIState
),
308 VMSTATE_UINT8(brr
, RSCIState
),
309 VMSTATE_UINT8(scr
, RSCIState
),
310 VMSTATE_UINT8(tdr
, RSCIState
),
311 VMSTATE_UINT8(ssr
, RSCIState
),
312 VMSTATE_UINT8(rdr
, RSCIState
),
313 VMSTATE_UINT8(scmr
, RSCIState
),
314 VMSTATE_UINT8(semr
, RSCIState
),
315 VMSTATE_UINT8(read_ssr
, RSCIState
),
316 VMSTATE_TIMER(timer
, RSCIState
),
317 VMSTATE_END_OF_LIST()
321 static Property rsci_properties
[] = {
322 DEFINE_PROP_UINT64("input-freq", RSCIState
, input_freq
, 0),
323 DEFINE_PROP_CHR("chardev", RSCIState
, chr
),
324 DEFINE_PROP_END_OF_LIST(),
327 static void rsci_class_init(ObjectClass
*klass
, void *data
)
329 DeviceClass
*dc
= DEVICE_CLASS(klass
);
331 dc
->realize
= rsci_realize
;
332 dc
->vmsd
= &vmstate_rsci
;
333 dc
->reset
= rsci_reset
;
334 device_class_set_props(dc
, rsci_properties
);
337 static const TypeInfo rsci_info
= {
338 .name
= TYPE_RENESAS_SCI
,
339 .parent
= TYPE_SYS_BUS_DEVICE
,
340 .instance_size
= sizeof(RSCIState
),
341 .instance_init
= rsci_init
,
342 .class_init
= rsci_class_init
,
345 static void rsci_register_types(void)
347 type_register_static(&rsci_info
);
350 type_init(rsci_register_types
)