Am 2. Juli 2026 12:37:59 UTC schrieb "Philippe Mathieu-Daudé"
<[email protected]>:
>Hi Matyáš,
>
>On 29/6/26 14:00, Peter Maydell wrote:
>> From: Matyáš Bobek <[email protected]>
>>
>> Added the FlexCAN2 emulator implementation core, with
>> CAN_FLEXCAN Kconfig flag and MAINTAINERS entry.
>>
>> FlexCAN2 version can be found in i.MX6 SoCs and others.
>>
>> More information about the implementation can be found in [1].
>>
>> Some macro and struct defintions were borrowed from the Linux kernel.
>> The original authors agreed with relicensing them to GPL-2.0-or-later on
>> the qemu-devel mailing list.
>>
>> [1]
>> http://dspace.cvut.cz/bitstream/handle/10467/122654/F3-BP-2025-Bobek-Matyas-BP_Bobek_FlexCAN_final_4.pdf
>>
>> Signed-off-by: Matyáš Bobek <[email protected]>
>> Signed-off-by: Pavel Pisa <[email protected]>
>> Tested-by: Pavel Pisa <[email protected]>
>> Reviewed-by: Bernhard Beschow <[email protected]>
>> Reviewed-by: Pavel Pisa <[email protected]>
>> Message-id:
>> 03dc62ff8013bb946aab8f64e51638b810629529.1782140438.git.matyas.bo...@gmail.com
>> Signed-off-by: Peter Maydell <[email protected]>
>> ---
>> MAINTAINERS | 8 +
>> hw/net/Kconfig | 5 +
>> hw/net/can/flexcan.c | 1396 +++++++++++++++++++++++++++++++++++++
>> hw/net/can/flexcan_regs.h | 197 ++++++
>> hw/net/can/meson.build | 1 +
>> hw/net/can/trace-events | 18 +
>> include/hw/net/flexcan.h | 145 ++++
>> 7 files changed, 1770 insertions(+)
>
>This commit is huge, not easy to follow / review.
>
>> create mode 100644 hw/net/can/flexcan.c
>> create mode 100644 hw/net/can/flexcan_regs.h
>> create mode 100644 include/hw/net/flexcan.h
>
>
>> diff --git a/hw/net/can/flexcan.c b/hw/net/can/flexcan.c
>> new file mode 100644
>> index 0000000000..1ea459d9f6
>> --- /dev/null
>> +++ b/hw/net/can/flexcan.c
>> @@ -0,0 +1,1396 @@
>> +/*
>> + * QEMU model of the NXP FLEXCAN device.
>> + *
>> + * This implementation is based on the following reference manual:
>> + * i.MX 6Dual/6Quad Applications Processor Reference Manual
>> + * Document Number: IMX6DQRM, Rev. 6, 05/2020
>> + *
>> + * Copyright (c) 2025 Matyas Bobek <[email protected]>
>> + *
>> + * Based on CTU CAN FD emulation implemented by Jan Charvat.
>> + *
>> + * SPDX-License-Identifier: GPL-2.0-or-later
>> + */
>> +
>> +#include "qemu/osdep.h"
>> +#include "qemu/log.h"
>> +#include "hw/core/sysbus.h"
>> +#include "qapi/error.h"
>> +#include "hw/core/irq.h"
>> +#include "migration/vmstate.h"
>> +#include "net/can_emu.h"
>> +#include "hw/core/qdev-properties.h"
>> +#include "trace.h"
>> +
>> +#include "hw/net/flexcan.h"
>> +#include "flexcan_regs.h"
>> +#include "qemu/timer.h"
>
>
>> +/**
>> + * flexcan_get_bitrate() - Calculate CAN bitrate (in Hz)
>> + * @s: FlexCAN device pointer
>> + *
>> + * The bitrate is determined by FlexCAN configuration in CTRL1 register,
>> + * and CCM co
>> + */
>> +static uint32_t flexcan_get_bitrate(FlexcanState *s)
>> +{
>> + uint32_t conf_presdiv = (s->regs.ctrl & FLEXCAN_CTRL_PRESDIV_MASK) >>
>> 24;
>> + uint32_t conf_pseg1 = (s->regs.ctrl & FLEXCAN_CTRL_PSEG1_MASK) >> 19;
>> + uint32_t conf_pseg2 = (s->regs.ctrl & FLEXCAN_CTRL_PSEG2_MASK) >> 16;
>> + uint32_t conf_propseg = s->regs.ctrl & FLEXCAN_CTRL_PROPSEG_MASK;
>> +
>> + /* N of time quanta for segments */
>> + uint32_t tseg1 = 2 + conf_pseg1 + conf_propseg;
>> + uint32_t tseg2 = 1 + conf_pseg2;
>> + uint32_t total_qpb = 1 + tseg1 + tseg2;
>> +
>> + uint32_t pe_freq, s_freq, bitrate;
>> +
>> + assert(s->ccm);
>
>Instead of that deep buried late unexplained assertion crashing
>when the guest is running, ...
>
>> +
>> + /* s_freq: CAN clock from CCM divided by the prescaler */
>> + pe_freq = imx_ccm_get_clock_frequency(s->ccm, CLK_CAN);
>> + s_freq = pe_freq / (1 + conf_presdiv);
>> + bitrate = s_freq / total_qpb;
>> +
>> + trace_flexcan_get_bitrate(DEVICE(s)->canonical_path, pe_freq,
>> + 1 + conf_presdiv, s_freq, tseg1, tseg2,
>> total_qpb,
>> + bitrate);
>> + return bitrate;
>> +}
>> +static void flexcan_init(Object *obj)
>> +{
>> + FlexcanState *s = CAN_FLEXCAN(obj);
>> +
>> + memory_region_init_io(
>> + &s->iomem, obj, &flexcan_ops, s, TYPE_CAN_FLEXCAN,
>> + offsetof(FlexcanRegs, _reserved6)
>> + );
>> +}
>> +
>> +static void flexcan_realize(DeviceState *dev, Error **errp)
>> +{
>> + FlexcanState *s = CAN_FLEXCAN(dev);
>> +
>> + if (s->canbus) {
>> + if (flexcan_connect_to_bus(s, s->canbus) < 0) {
>> + error_setg(errp, "%s: flexcan_connect_to_bus failed",
>> + dev->canonical_path);
>> + return;
>> + }
>> + }
>
>... expose it as a link in [*] and check it is set here, returning
>an error to the upper layer before starting emulation:
>
> if (!s->dma_mr) {
> error_setg(errp, TYPE_CAN_FLEXCAN " 'clock-control-module' "
> "link not set");
> return;
> }
>
>> +
>> + sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->iomem);
>> + sysbus_init_irq(SYS_BUS_DEVICE(SYS_BUS_DEVICE(dev)), &s->irq);
>> +}
>> +
>> +static const VMStateDescription vmstate_can = {
>> + .name = TYPE_CAN_FLEXCAN,
>> + .version_id = 1,
>> + .minimum_version_id = 1,
>> + .fields = (const VMStateField[]) {
>> + VMSTATE_INT64(timer_start, FlexcanState),
>> + VMSTATE_UINT32_ARRAY(regs_raw, FlexcanState, sizeof(FlexcanRegs) /
>> 4),
>> + VMSTATE_INT32(locked_mbidx, FlexcanState),
>> + VMSTATE_INT32(smb_target_mbidx, FlexcanState),
>> + VMSTATE_END_OF_LIST(),
>> + },
>> +};
>> +
>> +static const Property flexcan_properties[] = {
>> + DEFINE_PROP_LINK("canbus", FlexcanState, canbus, TYPE_CAN_BUS,
>> + CanBusState *),
>
>[*] here:
>
> DEFINE_PROP_LINK("clock-control-module", FlexcanState, ccm,
> TYPE_IMX_CCM, IMXCCMState *),
>
Yeah, this is even consistent with the "canbus" property. I'll take care of it
in my imx8mp-flexcan series, so I can address your comment there, too.
Best regards,
Bernhard
>> +};
>> +
>> +static void flexcan_class_init(ObjectClass *klass, const void *data)
>> +{
>> + DeviceClass *dc = DEVICE_CLASS(klass);
>> + ResettableClass *rc = RESETTABLE_CLASS(klass);
>> +
>> + rc->phases.enter = flexcan_reset_enter;
>> + rc->phases.hold = flexcan_reset_hold;
>> + dc->realize = flexcan_realize;
>> + device_class_set_props(dc, flexcan_properties);
>> + dc->vmsd = &vmstate_can;
>> + dc->desc = "i.MX FLEXCAN Controller";
>> +}
>> +
>> +static const TypeInfo flexcan_info = {
>> + .name = TYPE_CAN_FLEXCAN,
>> + .parent = TYPE_SYS_BUS_DEVICE,
>> + .instance_size = sizeof(FlexcanState),
>> + .class_init = flexcan_class_init,
>> + .instance_init = flexcan_init,
>> +};
>