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/qdev-properties-system.h"
30 #include "hw/char/renesas_sci.h"
31 #include "migration/vmstate.h"
33 /* SCI register map */
37 FIELD(SMR
, STOP
, 3, 1)
45 FIELD(SCR
, TEIE
, 2, 1)
46 FIELD(SCR
, MPIE
, 3, 1)
53 FIELD(SSR
, MPBT
, 0, 1)
55 FIELD(SSR
, TEND
, 2, 1)
59 FIELD(SSR
, ORER
, 5, 1)
60 FIELD(SSR
, RDRF
, 6, 1)
61 FIELD(SSR
, TDRE
, 7, 1)
64 FIELD(SCMR
, SMIF
, 0, 1)
65 FIELD(SCMR
, SINV
, 2, 1)
66 FIELD(SCMR
, SDIR
, 3, 1)
67 FIELD(SCMR
, BCP2
, 7, 1)
69 FIELD(SEMR
, ACS0
, 0, 1)
70 FIELD(SEMR
, ABCS
, 4, 1)
72 static int can_receive(void *opaque
)
74 RSCIState
*sci
= RSCI(opaque
);
75 if (sci
->rx_next
> qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
)) {
78 return FIELD_EX8(sci
->scr
, SCR
, RE
);
82 static void receive(void *opaque
, const uint8_t *buf
, int size
)
84 RSCIState
*sci
= RSCI(opaque
);
85 sci
->rx_next
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + sci
->trtime
;
86 if (FIELD_EX8(sci
->ssr
, SSR
, RDRF
) || size
> 1) {
87 sci
->ssr
= FIELD_DP8(sci
->ssr
, SSR
, ORER
, 1);
88 if (FIELD_EX8(sci
->scr
, SCR
, RIE
)) {
89 qemu_set_irq(sci
->irq
[ERI
], 1);
93 sci
->ssr
= FIELD_DP8(sci
->ssr
, SSR
, RDRF
, 1);
94 if (FIELD_EX8(sci
->scr
, SCR
, RIE
)) {
95 qemu_irq_pulse(sci
->irq
[RXI
]);
100 static void send_byte(RSCIState
*sci
)
102 if (qemu_chr_fe_backend_connected(&sci
->chr
)) {
103 qemu_chr_fe_write_all(&sci
->chr
, &sci
->tdr
, 1);
105 timer_mod(&sci
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + sci
->trtime
);
106 sci
->ssr
= FIELD_DP8(sci
->ssr
, SSR
, TEND
, 0);
107 sci
->ssr
= FIELD_DP8(sci
->ssr
, SSR
, TDRE
, 1);
108 qemu_set_irq(sci
->irq
[TEI
], 0);
109 if (FIELD_EX8(sci
->scr
, SCR
, TIE
)) {
110 qemu_irq_pulse(sci
->irq
[TXI
]);
114 static void txend(void *opaque
)
116 RSCIState
*sci
= RSCI(opaque
);
117 if (!FIELD_EX8(sci
->ssr
, SSR
, TDRE
)) {
120 sci
->ssr
= FIELD_DP8(sci
->ssr
, SSR
, TEND
, 1);
121 if (FIELD_EX8(sci
->scr
, SCR
, TEIE
)) {
122 qemu_set_irq(sci
->irq
[TEI
], 1);
127 static void update_trtime(RSCIState
*sci
)
130 sci
->trtime
= 8 - FIELD_EX8(sci
->smr
, SMR
, CHR
);
131 sci
->trtime
+= FIELD_EX8(sci
->smr
, SMR
, PE
);
132 sci
->trtime
+= FIELD_EX8(sci
->smr
, SMR
, STOP
) + 1;
133 /* x bit transmit time (32 * divrate * brr) / base freq */
134 sci
->trtime
*= 32 * sci
->brr
;
135 sci
->trtime
*= 1 << (2 * FIELD_EX8(sci
->smr
, SMR
, CKS
));
136 sci
->trtime
*= NANOSECONDS_PER_SECOND
;
137 sci
->trtime
/= sci
->input_freq
;
140 static bool sci_is_tr_enabled(RSCIState
*sci
)
142 return FIELD_EX8(sci
->scr
, SCR
, TE
) || FIELD_EX8(sci
->scr
, SCR
, RE
);
145 static void sci_write(void *opaque
, hwaddr offset
, uint64_t val
, unsigned size
)
147 RSCIState
*sci
= RSCI(opaque
);
151 if (!sci_is_tr_enabled(sci
)) {
157 if (!sci_is_tr_enabled(sci
)) {
164 if (FIELD_EX8(sci
->scr
, SCR
, TE
)) {
165 sci
->ssr
= FIELD_DP8(sci
->ssr
, SSR
, TDRE
, 1);
166 sci
->ssr
= FIELD_DP8(sci
->ssr
, SSR
, TEND
, 1);
167 if (FIELD_EX8(sci
->scr
, SCR
, TIE
)) {
168 qemu_irq_pulse(sci
->irq
[TXI
]);
171 if (!FIELD_EX8(sci
->scr
, SCR
, TEIE
)) {
172 qemu_set_irq(sci
->irq
[TEI
], 0);
174 if (!FIELD_EX8(sci
->scr
, SCR
, RIE
)) {
175 qemu_set_irq(sci
->irq
[ERI
], 0);
180 if (FIELD_EX8(sci
->ssr
, SSR
, TEND
)) {
183 sci
->ssr
= FIELD_DP8(sci
->ssr
, SSR
, TDRE
, 0);
187 sci
->ssr
= FIELD_DP8(sci
->ssr
, SSR
, MPBT
,
188 FIELD_EX8(val
, SSR
, MPBT
));
189 sci
->ssr
= FIELD_DP8(sci
->ssr
, SSR
, ERR
,
190 FIELD_EX8(val
, SSR
, ERR
) & 0x07);
191 if (FIELD_EX8(sci
->read_ssr
, SSR
, ERR
) &&
192 FIELD_EX8(sci
->ssr
, SSR
, ERR
) == 0) {
193 qemu_set_irq(sci
->irq
[ERI
], 0);
197 qemu_log_mask(LOG_GUEST_ERROR
, "reneas_sci: RDR is read only.\n");
200 sci
->scmr
= val
; break;
201 case A_SEMR
: /* SEMR */
202 sci
->semr
= val
; break;
204 qemu_log_mask(LOG_UNIMP
, "renesas_sci: Register 0x%" HWADDR_PRIX
" "
210 static uint64_t sci_read(void *opaque
, hwaddr offset
, unsigned size
)
212 RSCIState
*sci
= RSCI(opaque
);
224 sci
->read_ssr
= sci
->ssr
;
227 sci
->ssr
= FIELD_DP8(sci
->ssr
, SSR
, RDRF
, 0);
234 qemu_log_mask(LOG_UNIMP
, "renesas_sci: Register 0x%" HWADDR_PRIX
235 " not implemented.\n", offset
);
240 static const MemoryRegionOps sci_ops
= {
243 .endianness
= DEVICE_NATIVE_ENDIAN
,
244 .impl
.max_access_size
= 1,
245 .valid
.max_access_size
= 1,
248 static void rsci_reset(DeviceState
*dev
)
250 RSCIState
*sci
= RSCI(dev
);
251 sci
->smr
= sci
->scr
= 0x00;
258 sci
->rx_next
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
261 static void sci_event(void *opaque
, QEMUChrEvent event
)
263 RSCIState
*sci
= RSCI(opaque
);
264 if (event
== CHR_EVENT_BREAK
) {
265 sci
->ssr
= FIELD_DP8(sci
->ssr
, SSR
, FER
, 1);
266 if (FIELD_EX8(sci
->scr
, SCR
, RIE
)) {
267 qemu_set_irq(sci
->irq
[ERI
], 1);
272 static void rsci_realize(DeviceState
*dev
, Error
**errp
)
274 RSCIState
*sci
= RSCI(dev
);
276 if (sci
->input_freq
== 0) {
277 qemu_log_mask(LOG_GUEST_ERROR
,
278 "renesas_sci: input-freq property must be set.");
281 qemu_chr_fe_set_handlers(&sci
->chr
, can_receive
, receive
,
282 sci_event
, NULL
, sci
, NULL
, true);
285 static void rsci_init(Object
*obj
)
287 SysBusDevice
*d
= SYS_BUS_DEVICE(obj
);
288 RSCIState
*sci
= RSCI(obj
);
291 memory_region_init_io(&sci
->memory
, OBJECT(sci
), &sci_ops
,
292 sci
, "renesas-sci", 0x8);
293 sysbus_init_mmio(d
, &sci
->memory
);
295 for (i
= 0; i
< SCI_NR_IRQ
; i
++) {
296 sysbus_init_irq(d
, &sci
->irq
[i
]);
298 timer_init_ns(&sci
->timer
, QEMU_CLOCK_VIRTUAL
, txend
, sci
);
301 static const VMStateDescription vmstate_rsci
= {
302 .name
= "renesas-sci",
304 .minimum_version_id
= 1,
305 .fields
= (VMStateField
[]) {
306 VMSTATE_INT64(trtime
, RSCIState
),
307 VMSTATE_INT64(rx_next
, RSCIState
),
308 VMSTATE_UINT8(smr
, RSCIState
),
309 VMSTATE_UINT8(brr
, RSCIState
),
310 VMSTATE_UINT8(scr
, RSCIState
),
311 VMSTATE_UINT8(tdr
, RSCIState
),
312 VMSTATE_UINT8(ssr
, RSCIState
),
313 VMSTATE_UINT8(rdr
, RSCIState
),
314 VMSTATE_UINT8(scmr
, RSCIState
),
315 VMSTATE_UINT8(semr
, RSCIState
),
316 VMSTATE_UINT8(read_ssr
, RSCIState
),
317 VMSTATE_TIMER(timer
, RSCIState
),
318 VMSTATE_END_OF_LIST()
322 static Property rsci_properties
[] = {
323 DEFINE_PROP_UINT64("input-freq", RSCIState
, input_freq
, 0),
324 DEFINE_PROP_CHR("chardev", RSCIState
, chr
),
325 DEFINE_PROP_END_OF_LIST(),
328 static void rsci_class_init(ObjectClass
*klass
, void *data
)
330 DeviceClass
*dc
= DEVICE_CLASS(klass
);
332 dc
->realize
= rsci_realize
;
333 dc
->vmsd
= &vmstate_rsci
;
334 dc
->reset
= rsci_reset
;
335 device_class_set_props(dc
, rsci_properties
);
338 static const TypeInfo rsci_info
= {
339 .name
= TYPE_RENESAS_SCI
,
340 .parent
= TYPE_SYS_BUS_DEVICE
,
341 .instance_size
= sizeof(RSCIState
),
342 .instance_init
= rsci_init
,
343 .class_init
= rsci_class_init
,
346 static void rsci_register_types(void)
348 type_register_static(&rsci_info
);
351 type_init(rsci_register_types
)