can: mcp251xfd: fix TDC setting for low data bit rates
Subsystems (2)
PATCH v3 1/1 can: mcp251xfd: fix TDC setting for low data bit rates
The TDC is currently hardcoded enabled. This means that even for lower CAN-FD data bitrates (with a DBRP (data bitrate prescaler) > 2) a TDC is configured. This leads to a bus-off condition.
ISO 11898-1 section 11.3.3 says "Transmitter delay compensation" (TDC) is only applicable if DBRP is 1 or 2.
To fix the problem, switch the driver to use the TDC calculation provided by the CAN driver framework (which respects ISO 11898-1 section 11.3.3). This has the positive side effect that userspace can control TDC as needed.
Demonstration of the feature in action:
$ ip link set can0 up type can bitrate 125000 dbitrate 500000 fd on
$ ip -details link show can0
3: can0: <NOARP,UP,LOWER_UP,ECHO> mtu 72 qdisc pfifo_fast state UP mode DEFAULT group default qlen 10
link/can promiscuity 0 allmulti 0 minmtu 0 maxmtu 0
can <FD> state ERROR-ACTIVE (berr-counter tx 0 rx 0) restart-ms 0
bitrate 125000 sample-point 0.875
tq 50 prop-seg 69 phase-seg1 70 phase-seg2 20 sjw 10 brp 2
mcp251xfd: tseg1 2..256 tseg2 1..128 sjw 1..128 brp 1..256 brp_inc 1
dbitrate 500000 dsample-point 0.875
dtq 125 dprop-seg 6 dphase-seg1 7 dphase-seg2 2 dsjw 1 dbrp 5
mcp251xfd: dtseg1 1..32 dtseg2 1..16 dsjw 1..16 dbrp 1..256 dbrp_inc 1
tdco 0..63
clock 40000000 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535 tso_max_size 65536 tso_max_segs 65535 gro_max_size 65536 parentbus spi parentdev spi0.0
$ ip link set can0 up type can bitrate 1000000 dbitrate 4000000 fd on
$ ip -details link show can0
3: can0: <NOARP,UP,LOWER_UP,ECHO> mtu 72 qdisc pfifo_fast state UP mode DEFAULT group default qlen 10
link/can promiscuity 0 allmulti 0 minmtu 0 maxmtu 0
can <FD,TDC-AUTO> state ERROR-ACTIVE (berr-counter tx 0 rx 0) restart-ms 0
bitrate 1000000 sample-point 0.750
tq 25 prop-seg 14 phase-seg1 15 phase-seg2 10 sjw 5 brp 1
mcp251xfd: tseg1 2..256 tseg2 1..128 sjw 1..128 brp 1..256 brp_inc 1
dbitrate 4000000 dsample-point 0.700
dtq 25 dprop-seg 3 dphase-seg1 3 dphase-seg2 3 dsjw 1 dbrp 1
tdco 7
mcp251xfd: dtseg1 1..32 dtseg2 1..16 dsjw 1..16 dbrp 1..256 dbrp_inc 1
tdco 0..63
clock 40000000 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535 tso_max_size 65536 tso_max_segs 65535 gro_max_size 65536 parentbus spi parentdev spi0.0There has been some confusion about the MCP2518FD using a relative or absolute TDCO due to the datasheet specifying a range of [-64,63]. I have a custom board with a 40 MHz clock and an estimated loop delay of 100 to 216 ns. During testing at a data bit rate of 4 Mbit/s I found that using can_get_relative_tdco() resulted in bus-off errors. The final TDCO value was 1 which corresponds to a 10% SSP in an absolute configuration. This behavior is expected if the TDCO value is really absolute and not relative. Using priv->can.tdc.tdco instead results in a final TDCO of 8, setting the SSP at exactly 80%. This configuration works.
--- .../net/can/spi/mcp251xfd/mcp251xfd-core.c | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-)
drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c +18 -7
@@ -75,6 +75,15 @@ static const struct can_bittiming_const mcp251xfd_data_bittiming_const = { .brp_inc = 1, }; +static const struct can_tdc_const mcp251xfd_tdc_const = { + .tdcv_min =0 , + .tdcv_max =63 , + .tdco_min =0 , + .tdco_max =63 , + .tdcf_min =0 , + .tdcf_max =0 , +}; +static const char *__mcp251xfd_get_model_str (enum mcp251xfd_model model) {switch (model) {
@@ -511,7 +520,6 @@ static int mcp251xfd_set_bittiming(const struct mcp251xfd_priv *priv)const struct can_bittiming *bt = &priv->can.bittiming;const struct can_bittiming *dbt = &priv->can.data_bittiming;u32 val =0 ; -s8 tdco;int err;/* CAN Control Register
@@ -575,11 +583,13 @@ static int mcp251xfd_set_bittiming(const struct mcp251xfd_priv *priv)return err;/* Transmitter Delay Compensation */ - tdco =clamp_t (int, dbt->brp * (dbt->prop_seg + dbt->phase_seg1), --64 ,63 ); - val =FIELD_PREP (MCP251XFD_REG_TDC_TDCMOD_MASK , -MCP251XFD_REG_TDC_TDCMOD_AUTO ) | -FIELD_PREP (MCP251XFD_REG_TDC_TDCO_MASK , tdco); +if (can_tdc_is_enabled (&priv->can)) + val =FIELD_PREP (MCP251XFD_REG_TDC_TDCMOD_MASK , +MCP251XFD_REG_TDC_TDCMOD_AUTO ); +else + val =FIELD_PREP (MCP251XFD_REG_TDC_TDCMOD_MASK , +MCP251XFD_REG_TDC_TDCMOD_DISABLED ) | +FIELD_PREP (MCP251XFD_REG_TDC_TDCO_MASK , priv->can.tdc.tdco);return regmap_write (priv->map_reg,MCP251XFD_REG_TDC , val); }
@@ -2083,10 +2093,11 @@ static int mcp251xfd_probe(struct spi_device *spi) priv->can.do_get_berr_counter = mcp251xfd_get_berr_counter; priv->can.bittiming_const = &mcp251xfd_bittiming_const; priv->can.data_bittiming_const = &mcp251xfd_data_bittiming_const; + priv->can.tdc_const = &mcp251xfd_tdc_const; priv->can.ctrlmode_supported =CAN_CTRLMODE_LOOPBACK |CAN_CTRLMODE_LISTENONLY |CAN_CTRLMODE_BERR_REPORTING |CAN_CTRLMODE_FD |CAN_CTRLMODE_FD_NON_ISO | -CAN_CTRLMODE_CC_LEN8_DLC ; +CAN_CTRLMODE_CC_LEN8_DLC |CAN_CTRLMODE_TDC_AUTO ;set_bit (MCP251XFD_FLAGS_DOWN , priv->flags); priv->ndev = ndev; priv->spi = spi;
Signature · 1 line
2.39.5 (Apple Git-154)
Hi Kelsey,
The tdco logic seems inverted. While at it, I also added a suggestion to add TDCV support.
Quoted 62 lines · Kelsey Maes
The TDC is currently hardcoded enabled. This means that even for lower CAN-FD data bitrates (with a DBRP (data bitrate prescaler) > 2) a TDC is configured. This leads to a bus-off condition.
ISO 11898-1 section 11.3.3 says "Transmitter delay compensation" (TDC) is only applicable if DBRP is 1 or 2.
To fix the problem, switch the driver to use the TDC calculation provided by the CAN driver framework (which respects ISO 11898-1 section 11.3.3). This has the positive side effect that userspace can control TDC as needed.
Demonstration of the feature in action:
$ ip link set can0 up type can bitrate 125000 dbitrate 500000 fd on $ ip -details link show can0 3: can0: <NOARP,UP,LOWER_UP,ECHO> mtu 72 qdisc pfifo_fast state UP mode DEFAULT group default qlen 10 link/can promiscuity 0 allmulti 0 minmtu 0 maxmtu 0 can <FD> state ERROR-ACTIVE (berr-counter tx 0 rx 0) restart-ms 0 bitrate 125000 sample-point 0.875 tq 50 prop-seg 69 phase-seg1 70 phase-seg2 20 sjw 10 brp 2 mcp251xfd: tseg1 2..256 tseg2 1..128 sjw 1..128 brp 1..256 brp_inc 1 dbitrate 500000 dsample-point 0.875 dtq 125 dprop-seg 6 dphase-seg1 7 dphase-seg2 2 dsjw 1 dbrp 5 mcp251xfd: dtseg1 1..32 dtseg2 1..16 dsjw 1..16 dbrp 1..256 dbrp_inc 1 tdco 0..63 clock 40000000 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535 tso_max_size 65536 tso_max_segs 65535 gro_max_size 65536 parentbus spi parentdev spi0.0 $ ip link set can0 up type can bitrate 1000000 dbitrate 4000000 fd on $ ip -details link show can0 3: can0: <NOARP,UP,LOWER_UP,ECHO> mtu 72 qdisc pfifo_fast state UP mode DEFAULT group default qlen 10 link/can promiscuity 0 allmulti 0 minmtu 0 maxmtu 0 can <FD,TDC-AUTO> state ERROR-ACTIVE (berr-counter tx 0 rx 0) restart-ms 0 bitrate 1000000 sample-point 0.750 tq 25 prop-seg 14 phase-seg1 15 phase-seg2 10 sjw 5 brp 1 mcp251xfd: tseg1 2..256 tseg2 1..128 sjw 1..128 brp 1..256 brp_inc 1 dbitrate 4000000 dsample-point 0.700 dtq 25 dprop-seg 3 dphase-seg1 3 dphase-seg2 3 dsjw 1 dbrp 1 tdco 7 mcp251xfd: dtseg1 1..32 dtseg2 1..16 dsjw 1..16 dbrp 1..256 dbrp_inc 1 tdco 0..63 clock 40000000 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535 tso_max_size 65536 tso_max_segs 65535 gro_max_size 65536 parentbus spi parentdev spi0.0There has been some confusion about the MCP2518FD using a relative or absolute TDCO due to the datasheet specifying a range of [-64,63]. I have a custom board with a 40 MHz clock and an estimated loop delay of 100 to 216 ns. During testing at a data bit rate of 4 Mbit/s I found that using
can_get_relative_tdco()resulted in bus-off errors. The final TDCO value was 1 which corresponds to a 10% SSP in an absolute configuration. This behavior is expected if the TDCO value is really absolute and not relative. Usingpriv->can.tdc.tdcoinstead results in a final TDCO of 8, setting the SSP at exactly 80%. This configuration works.Fixes 55e5b97f003e ("can: mcp25xxfd: add driver for Microchip MCP25xxFD SPI CAN")Reported-by Kelsey Maes <[email protected]>Signed-off-by Kelsey Maes <[email protected]>> ---.../net/can/spi/mcp251xfd/mcp251xfd-core.c | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-)drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c +0
-0
drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c static const struct can_bittiming_const mcp251xfd_data_bittiming_const = { · line 80
Quoted 6 lines
drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c +3
-0@@ -75,6 +75,15 @@ static const struct can_bittiming_const mcp251xfd_data_bittiming_const = { .brp_inc = 1, }; +static const struct can_tdc_const mcp251xfd_tdc_const = { + .tdcv_min =0 , + .tdcv_max =63 ,
I missed this in my previous review, sorry.
Does the mcp251xfd support the manual TDC? Unless you add the CAN_CTRLMODE_TDC_MANUAL flag, the TDCV value is simply ignored, c.f.
https://elixir.bootlin.com/linux/v6.14/source/include/linux/can/bittiming.h#L92
All the macro are already there:
https://elixir.bootlin.com/linux/v6.14/source/drivers/net/can/spi/mcp251xfd/mcp251xfd.h#L76
So that shouldn't be hard. Can you test this in mcp251xfd_set_bittiming()?
u32 tdcmod, val;
/* ... */
/* Transmitter Delay Compensation */ if (priv->can.ctrlmode & CAN_CTRLMODE_TDC_AUTO) tdcmod = MCP251XFD_REG_TDC_TDCMOD_AUTO; else if (priv->can.ctrlmode & CAN_CTRLMODE_TDC_MANUAL) tdcmod = MCP251XFD_REG_TDC_TDCMOD_MANUAL; else tdcmod = MCP251XFD_REG_TDC_TDCMOD_DISABLED; val = FIELD_PREP(MCP251XFD_REG_TDC_TDCMOD_MASK, tdcmod) | FIELD_PREP(MCP251XFD_REG_TDC_TDCV_MASK, priv->can.tdc.tdcv) | FIELD_PREP(MCP251XFD_REG_TDC_TDCO_MASK, priv->can.tdc.tdco); return regmap_write(priv->map_reg, MCP251XFD_REG_TDC, val);
(and yes, in my previous message, I said that it is better to move the tdco's FIELD_PREP inside the if, but this only applies for a simple if/else, if we have three branches, the above looks better).
Quoted 23 lines
drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c +6
-3+ .tdco_min = 0, + .tdco_max = 63, + .tdcf_min = 0, + .tdcf_max = 0, +}; +static const char *__mcp251xfd_get_model_str (enum mcp251xfd_model model) {switch (model) {@@ -511,7 +520,6 @@ static int mcp251xfd_set_bittiming(const struct mcp251xfd_priv *priv)const struct can_bittiming *bt = &priv->can.bittiming;const struct can_bittiming *dbt = &priv->can.data_bittiming;u32 val =0 ; -s8 tdco;int err;/* CAN Control Register @@ -575,11 +583,13 @@ static int mcp251xfd_set_bittiming(const struct mcp251xfd_priv *priv)return err;/* Transmitter Delay Compensation */ - tdco =clamp_t (int, dbt->brp * (dbt->prop_seg + dbt->phase_seg1), --64 ,63 );
drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c static int mcp251xfd_set_bittiming(const struct mcp251xfd_priv *priv) · line 592
Quoted 10 lines
drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c +7
-3- val =FIELD_PREP (MCP251XFD_REG_TDC_TDCMOD_MASK , -MCP251XFD_REG_TDC_TDCMOD_AUTO ) | -FIELD_PREP (MCP251XFD_REG_TDC_TDCO_MASK , tdco); +if (can_tdc_is_enabled (&priv->can)) + val =FIELD_PREP (MCP251XFD_REG_TDC_TDCMOD_MASK , +MCP251XFD_REG_TDC_TDCMOD_AUTO ); +else + val =FIELD_PREP (MCP251XFD_REG_TDC_TDCMOD_MASK , +MCP251XFD_REG_TDC_TDCMOD_DISABLED ) | +FIELD_PREP (MCP251XFD_REG_TDC_TDCO_MASK , priv->can.tdc.tdco);
Shouldn't this have been the opposite?
if (can_tdc_is_enabled(&priv->can)) val = FIELD_PREP(MCP251XFD_REG_TDC_TDCMOD_MASK, MCP251XFD_REG_TDC_TDCMOD_AUTO) | FIELD_PREP(MCP251XFD_REG_TDC_TDCO_MASK, priv->can.tdc.tdco); else val = FIELD_PREP(MCP251XFD_REG_TDC_TDCMOD_MASK, MCP251XFD_REG_TDC_TDCMOD_DISABLED);
Did you reconfirm that the device works well with high bitrates (e.g. 5 Mbps) with no bus off conditions after the change?
Quoted 5 lines
drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c +0
-0return regmap_write (priv->map_reg,MCP251XFD_REG_TDC , val); }@@ -2083,10 +2093,11 @@ static int mcp251xfd_probe(struct spi_device *spi) priv->can.do_get_berr_counter = mcp251xfd_get_berr_counter; priv->can.bittiming_const = &mcp251xfd_bittiming_const;
drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c static int mcp251xfd_probe(struct spi_device *spi) · line 2103
Quoted 10 lines
drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c +2
-1priv->can.data_bittiming_const = &mcp251xfd_data_bittiming_const; + priv->can.tdc_const = &mcp251xfd_tdc_const; priv->can.ctrlmode_supported =CAN_CTRLMODE_LOOPBACK |CAN_CTRLMODE_LISTENONLY |CAN_CTRLMODE_BERR_REPORTING |CAN_CTRLMODE_FD |CAN_CTRLMODE_FD_NON_ISO | -CAN_CTRLMODE_CC_LEN8_DLC ; +CAN_CTRLMODE_CC_LEN8_DLC |CAN_CTRLMODE_TDC_AUTO ;set_bit (MCP251XFD_FLAGS_DOWN , priv->flags); priv->ndev = ndev; priv->spi = spi;
Yours sincerely,
Vincent Mailhol
Quoted 100 lines · Vincent Mailhol
Quoted 62 lines · Kelsey Maes
The TDC is currently hardcoded enabled. This means that even for lower CAN-FD data bitrates (with a DBRP (data bitrate prescaler) > 2) a TDC is configured. This leads to a bus-off condition.
ISO 11898-1 section 11.3.3 says "Transmitter delay compensation" (TDC) is only applicable if DBRP is 1 or 2.
To fix the problem, switch the driver to use the TDC calculation provided by the CAN driver framework (which respects ISO 11898-1 section 11.3.3). This has the positive side effect that userspace can control TDC as needed.
Demonstration of the feature in action:
$ ip link set can0 up type can bitrate 125000 dbitrate 500000 fd on $ ip -details link show can0 3: can0: <NOARP,UP,LOWER_UP,ECHO> mtu 72 qdisc pfifo_fast state UP mode DEFAULT group default qlen 10 link/can promiscuity 0 allmulti 0 minmtu 0 maxmtu 0 can <FD> state ERROR-ACTIVE (berr-counter tx 0 rx 0) restart-ms 0 bitrate 125000 sample-point 0.875 tq 50 prop-seg 69 phase-seg1 70 phase-seg2 20 sjw 10 brp 2 mcp251xfd: tseg1 2..256 tseg2 1..128 sjw 1..128 brp 1..256 brp_inc 1 dbitrate 500000 dsample-point 0.875 dtq 125 dprop-seg 6 dphase-seg1 7 dphase-seg2 2 dsjw 1 dbrp 5 mcp251xfd: dtseg1 1..32 dtseg2 1..16 dsjw 1..16 dbrp 1..256 dbrp_inc 1 tdco 0..63 clock 40000000 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535 tso_max_size 65536 tso_max_segs 65535 gro_max_size 65536 parentbus spi parentdev spi0.0 $ ip link set can0 up type can bitrate 1000000 dbitrate 4000000 fd on $ ip -details link show can0 3: can0: <NOARP,UP,LOWER_UP,ECHO> mtu 72 qdisc pfifo_fast state UP mode DEFAULT group default qlen 10 link/can promiscuity 0 allmulti 0 minmtu 0 maxmtu 0 can <FD,TDC-AUTO> state ERROR-ACTIVE (berr-counter tx 0 rx 0) restart-ms 0 bitrate 1000000 sample-point 0.750 tq 25 prop-seg 14 phase-seg1 15 phase-seg2 10 sjw 5 brp 1 mcp251xfd: tseg1 2..256 tseg2 1..128 sjw 1..128 brp 1..256 brp_inc 1 dbitrate 4000000 dsample-point 0.700 dtq 25 dprop-seg 3 dphase-seg1 3 dphase-seg2 3 dsjw 1 dbrp 1 tdco 7 mcp251xfd: dtseg1 1..32 dtseg2 1..16 dsjw 1..16 dbrp 1..256 dbrp_inc 1 tdco 0..63 clock 40000000 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535 tso_max_size 65536 tso_max_segs 65535 gro_max_size 65536 parentbus spi parentdev spi0.0There has been some confusion about the MCP2518FD using a relative or absolute TDCO due to the datasheet specifying a range of [-64,63]. I have a custom board with a 40 MHz clock and an estimated loop delay of 100 to 216 ns. During testing at a data bit rate of 4 Mbit/s I found that using
can_get_relative_tdco()resulted in bus-off errors. The final TDCO value was 1 which corresponds to a 10% SSP in an absolute configuration. This behavior is expected if the TDCO value is really absolute and not relative. Usingpriv->can.tdc.tdcoinstead results in a final TDCO of 8, setting the SSP at exactly 80%. This configuration works.Fixes 55e5b97f003e ("can: mcp25xxfd: add driver for Microchip MCP25xxFD SPI CAN")Reported-by Kelsey Maes <[email protected]>Signed-off-by Kelsey Maes <[email protected]>> ---
.../net/can/spi/mcp251xfd/mcp251xfd-core.c| 25 +++++++++++++------1 file changed, 18 insertions(+), 7 deletions(-)drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c +0
-0drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c static const struct can_bittiming_const mcp251xfd_data_bittiming_const = { · line 80
Quoted 6 lines
drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c +3
-0@@ -75,6 +75,15 @@ static const struct can_bittiming_const mcp251xfd_data_bittiming_const = { .brp_inc = 1, }; +static const struct can_tdc_const mcp251xfd_tdc_const = { + .tdcv_min =0 , + .tdcv_max =63 ,I missed this in my previous review, sorry.
Does the mcp251xfd support the manual TDC? Unless you add the
CAN_CTRLMODE_TDC_MANUALflag, the TDCV value is simply ignored, c.f.https://elixir.bootlin.com/linux/v6.14/source/include/linux/can/bittiming.h#L92
All the macro are already there:
https://elixir.bootlin.com/linux/v6.14/source/drivers/net/can/spi/mcp251xfd/mcp251xfd.h#L76
So that shouldn't be hard. Can you test this in
mcp251xfd_set_bittiming()?u32 tdcmod, val;
/* ... */
/* Transmitter Delay Compensation */
if (priv->can.ctrlmode&CAN_CTRLMODE_TDC_AUTO)
tdcmod =MCP251XFD_REG_TDC_TDCMOD_AUTO;
else if (priv->can.ctrlmode&CAN_CTRLMODE_TDC_MANUAL)
tdcmod =MCP251XFD_REG_TDC_TDCMOD_MANUAL;
else
tdcmod =MCP251XFD_REG_TDC_TDCMOD_DISABLED;val =
FIELD_PREP(MCP251XFD_REG_TDC_TDCMOD_MASK, tdcmod)|
FIELD_PREP(MCP251XFD_REG_TDC_TDCV_MASK, priv->can.tdc.tdcv)|FIELD_PREP(MCP251XFD_REG_TDC_TDCO_MASK, priv->can.tdc.tdco);
Quoted 5 lines
return
regmap_write(priv->map_reg, MCP251XFD_REG_TDC, val);(and yes, in my previous message, I said that it is better to move the tdco's
FIELD_PREPinside the if, but this only applies for a simple if/else, if we have three branches, the above looks better).
Manual TDCV is supported by the hardware but this patch does not enable it. I can take a quick look at this but it is out of scope for our needs.
Quoted 44 lines
Quoted 23 lines
drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c +6
-3+ .tdco_min = 0, + .tdco_max = 63, + .tdcf_min = 0, + .tdcf_max = 0, +}; +static const char *__mcp251xfd_get_model_str (enum mcp251xfd_model model) {switch (model) {@@ -511,7 +520,6 @@ static int mcp251xfd_set_bittiming(const struct mcp251xfd_priv *priv)const struct can_bittiming *bt = &priv->can.bittiming;const struct can_bittiming *dbt = &priv->can.data_bittiming;u32 val =0 ; -s8 tdco;int err;/* CAN Control Register @@ -575,11 +583,13 @@ static int mcp251xfd_set_bittiming(const struct mcp251xfd_priv *priv)return err;/* Transmitter Delay Compensation */ - tdco =clamp_t (int, dbt->brp * (dbt->prop_seg + dbt->phase_seg1), --64 ,63 );drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c static int mcp251xfd_set_bittiming(const struct mcp251xfd_priv *priv) · line 592
Quoted 10 lines
drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c +7
-3- val =FIELD_PREP (MCP251XFD_REG_TDC_TDCMOD_MASK , -MCP251XFD_REG_TDC_TDCMOD_AUTO ) | -FIELD_PREP (MCP251XFD_REG_TDC_TDCO_MASK , tdco); +if (can_tdc_is_enabled (&priv->can)) + val =FIELD_PREP (MCP251XFD_REG_TDC_TDCMOD_MASK , +MCP251XFD_REG_TDC_TDCMOD_AUTO ); +else + val =FIELD_PREP (MCP251XFD_REG_TDC_TDCMOD_MASK , +MCP251XFD_REG_TDC_TDCMOD_DISABLED ) | +FIELD_PREP (MCP251XFD_REG_TDC_TDCO_MASK , priv->can.tdc.tdco);Shouldn't this have been the opposite?
if (
can_tdc_is_enabled(&priv->can))
val = FIELD_PREP(MCP251XFD_REG_TDC_TDCMOD_MASK,
MCP251XFD_REG_TDC_TDCMOD_AUTO) |
FIELD_PREP(MCP251XFD_REG_TDC_TDCO_MASK, priv->can.tdc.tdco);
else
val = FIELD_PREP(MCP251XFD_REG_TDC_TDCMOD_MASK,
MCP251XFD_REG_TDC_TDCMOD_DISABLED);
Quoted 2 lines
Did you reconfirm that the device works well with high bitrates (e.g. 5 Mbps) with no bus off conditions after the change?
Heh, that’s embarrassing. You are correct that the logic is inverted and it passed my tests up to 4 Mbit/s. My guess here is that the loop delay in ideal conditions is low enough to work without TDC. I will see if I can add a temporary higher speed for testing.
Quoted 7 lines
Quoted 7 lines
drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c +1
-0return regmap_write (priv->map_reg,MCP251XFD_REG_TDC , val); }@@ -2083,10 +2093,11 @@ static int mcp251xfd_probe(struct spi_device *spi) priv->can.do_get_berr_counter = mcp251xfd_get_berr_counter; priv->can.bittiming_const = &mcp251xfd_bittiming_const; priv->can.data_bittiming_const = &mcp251xfd_data_bittiming_const; + priv->can.tdc_const = &mcp251xfd_tdc_const;
Quoted 8 lines
Quoted 8 lines
priv->can.ctrlmode_supported=CAN_CTRLMODE_LOOPBACK|CAN_CTRLMODE_LISTENONLY|CAN_CTRLMODE_BERR_REPORTING|CAN_CTRLMODE_FD|CAN_CTRLMODE_FD_NON_ISO|
-CAN_CTRLMODE_CC_LEN8_DLC;
+CAN_CTRLMODE_CC_LEN8_DLC|CAN_CTRLMODE_TDC_AUTO;set_bit(MCP251XFD_FLAGS_DOWN, priv->flags);priv->ndev= ndev;priv->spi= spi;
--
Kelsey Maes
VP Process Inc.
Quoted 101 lines · Kelsey Maes
Quoted 100 lines · Vincent Mailhol
Quoted 62 lines · Kelsey Maes
The TDC is currently hardcoded enabled. This means that even for lower CAN-FD data bitrates (with a DBRP (data bitrate prescaler) > 2) a TDC is configured. This leads to a bus-off condition.
ISO 11898-1 section 11.3.3 says "Transmitter delay compensation" (TDC) is only applicable if DBRP is 1 or 2.
To fix the problem, switch the driver to use the TDC calculation provided by the CAN driver framework (which respects ISO 11898-1 section 11.3.3). This has the positive side effect that userspace can control TDC as needed.
Demonstration of the feature in action:
$ ip link set can0 up type can bitrate 125000 dbitrate 500000 fd on $ ip -details link show can0 3: can0: <NOARP,UP,LOWER_UP,ECHO> mtu 72 qdisc pfifo_fast state UP mode DEFAULT group default qlen 10 link/can promiscuity 0 allmulti 0 minmtu 0 maxmtu 0 can <FD> state ERROR-ACTIVE (berr-counter tx 0 rx 0) restart-ms 0 bitrate 125000 sample-point 0.875 tq 50 prop-seg 69 phase-seg1 70 phase-seg2 20 sjw 10 brp 2 mcp251xfd: tseg1 2..256 tseg2 1..128 sjw 1..128 brp 1..256 brp_inc 1 dbitrate 500000 dsample-point 0.875 dtq 125 dprop-seg 6 dphase-seg1 7 dphase-seg2 2 dsjw 1 dbrp 5 mcp251xfd: dtseg1 1..32 dtseg2 1..16 dsjw 1..16 dbrp 1..256 dbrp_inc 1 tdco 0..63 clock 40000000 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535 tso_max_size 65536 tso_max_segs 65535 gro_max_size 65536 parentbus spi parentdev spi0.0 $ ip link set can0 up type can bitrate 1000000 dbitrate 4000000 fd on $ ip -details link show can0 3: can0: <NOARP,UP,LOWER_UP,ECHO> mtu 72 qdisc pfifo_fast state UP mode DEFAULT group default qlen 10 link/can promiscuity 0 allmulti 0 minmtu 0 maxmtu 0 can <FD,TDC-AUTO> state ERROR-ACTIVE (berr-counter tx 0 rx 0) restart-ms 0 bitrate 1000000 sample-point 0.750 tq 25 prop-seg 14 phase-seg1 15 phase-seg2 10 sjw 5 brp 1 mcp251xfd: tseg1 2..256 tseg2 1..128 sjw 1..128 brp 1..256 brp_inc 1 dbitrate 4000000 dsample-point 0.700 dtq 25 dprop-seg 3 dphase-seg1 3 dphase-seg2 3 dsjw 1 dbrp 1 tdco 7 mcp251xfd: dtseg1 1..32 dtseg2 1..16 dsjw 1..16 dbrp 1..256 dbrp_inc 1 tdco 0..63 clock 40000000 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535 tso_max_size 65536 tso_max_segs 65535 gro_max_size 65536 parentbus spi parentdev spi0.0There has been some confusion about the MCP2518FD using a relative or absolute TDCO due to the datasheet specifying a range of [-64,63]. I have a custom board with a 40 MHz clock and an estimated loop delay of 100 to 216 ns. During testing at a data bit rate of 4 Mbit/s I found that using
can_get_relative_tdco()resulted in bus-off errors. The final TDCO value was 1 which corresponds to a 10% SSP in an absolute configuration. This behavior is expected if the TDCO value is really absolute and not relative. Usingpriv->can.tdc.tdcoinstead results in a final TDCO of 8, setting the SSP at exactly 80%. This configuration works.Fixes 55e5b97f003e ("can: mcp25xxfd: add driver for Microchip MCP25xxFD SPI CAN")Reported-by Kelsey Maes <[email protected]>Signed-off-by Kelsey Maes <[email protected]>> ---
.../net/can/spi/mcp251xfd/mcp251xfd-core.c| 25 +++++++++++++------1 file changed, 18 insertions(+), 7 deletions(-)drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c +0
-0drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c static const struct can_bittiming_const mcp251xfd_data_bittiming_const = { · line 80
Quoted 6 lines
drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c +3
-0@@ -75,6 +75,15 @@ static const struct can_bittiming_const mcp251xfd_data_bittiming_const = { .brp_inc = 1, }; +static const struct can_tdc_const mcp251xfd_tdc_const = { + .tdcv_min =0 , + .tdcv_max =63 ,I missed this in my previous review, sorry.
Does the mcp251xfd support the manual TDC? Unless you add the
CAN_CTRLMODE_TDC_MANUALflag, the TDCV value is simply ignored, c.f.https://elixir.bootlin.com/linux/v6.14/source/include/linux/can/bittiming.h#L92
All the macro are already there:
https://elixir.bootlin.com/linux/v6.14/source/drivers/net/can/spi/mcp251xfd/mcp251xfd.h#L76
So that shouldn't be hard. Can you test this in
mcp251xfd_set_bittiming()?u32 tdcmod, val;
/* ... */
/* Transmitter Delay Compensation */
if (priv->can.ctrlmode&CAN_CTRLMODE_TDC_AUTO)
tdcmod =MCP251XFD_REG_TDC_TDCMOD_AUTO;
else if (priv->can.ctrlmode&CAN_CTRLMODE_TDC_MANUAL)
tdcmod =MCP251XFD_REG_TDC_TDCMOD_MANUAL;
else
tdcmod =MCP251XFD_REG_TDC_TDCMOD_DISABLED;val =
FIELD_PREP(MCP251XFD_REG_TDC_TDCMOD_MASK, tdcmod)|
FIELD_PREP(MCP251XFD_REG_TDC_TDCV_MASK, priv->can.tdc.tdcv)|FIELD_PREP(MCP251XFD_REG_TDC_TDCO_MASK, priv->can.tdc.tdco);
Quoted 8 lines
Quoted 5 lines
return
regmap_write(priv->map_reg, MCP251XFD_REG_TDC, val);(and yes, in my previous message, I said that it is better to move the tdco's
FIELD_PREPinside the if, but this only applies for a simple if/else, if we have three branches, the above looks better).Manual TDCV is supported by the hardware but this patch does not enable it. I can take a quick look at this but it is out of scope for our needs.
I figured out that this was probably not your use case. This is why I wrote the suggestion. The fact is that you need a v4 anyway and now that the code is here, that shouldn't be much effort for you.
You can use this command to test:
$ ip link set can0 type can bitrate 1000000 dbitrate 5000000 fd on \ tdcv 5 tdco 8 tdc-mode manual
(the tdcv 5 is a completely random value, it didn't do the math. The point is just whether or not the value reaches the device).
If there is a mistake in my suggestion, I am fine if you stay to your needs and only support the tdco. I am already really glad that you came up with the fix and do not want to create you more work!
Quoted 44 lines
Quoted 44 lines
Quoted 23 lines
drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c +6
-3+ .tdco_min = 0, + .tdco_max = 63, + .tdcf_min = 0, + .tdcf_max = 0, +}; +static const char *__mcp251xfd_get_model_str (enum mcp251xfd_model model) {switch (model) {@@ -511,7 +520,6 @@ static int mcp251xfd_set_bittiming(const struct mcp251xfd_priv *priv)const struct can_bittiming *bt = &priv->can.bittiming;const struct can_bittiming *dbt = &priv->can.data_bittiming;u32 val =0 ; -s8 tdco;int err;/* CAN Control Register @@ -575,11 +583,13 @@ static int mcp251xfd_set_bittiming(const struct mcp251xfd_priv *priv)return err;/* Transmitter Delay Compensation */ - tdco =clamp_t (int, dbt->brp * (dbt->prop_seg + dbt->phase_seg1), --64 ,63 );drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c static int mcp251xfd_set_bittiming(const struct mcp251xfd_priv *priv) · line 592
Quoted 10 lines
drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c +7
-3- val =FIELD_PREP (MCP251XFD_REG_TDC_TDCMOD_MASK , -MCP251XFD_REG_TDC_TDCMOD_AUTO ) | -FIELD_PREP (MCP251XFD_REG_TDC_TDCO_MASK , tdco); +if (can_tdc_is_enabled (&priv->can)) + val =FIELD_PREP (MCP251XFD_REG_TDC_TDCMOD_MASK , +MCP251XFD_REG_TDC_TDCMOD_AUTO ); +else + val =FIELD_PREP (MCP251XFD_REG_TDC_TDCMOD_MASK , +MCP251XFD_REG_TDC_TDCMOD_DISABLED ) | +FIELD_PREP (MCP251XFD_REG_TDC_TDCO_MASK , priv->can.tdc.tdco);Shouldn't this have been the opposite?
if (
can_tdc_is_enabled(&priv->can))
val = FIELD_PREP(MCP251XFD_REG_TDC_TDCMOD_MASK,
MCP251XFD_REG_TDC_TDCMOD_AUTO) |
FIELD_PREP(MCP251XFD_REG_TDC_TDCO_MASK, priv->can.tdc.tdco);
else
val = FIELD_PREP(MCP251XFD_REG_TDC_TDCMOD_MASK,
MCP251XFD_REG_TDC_TDCMOD_DISABLED);
Quoted 7 lines
Quoted 2 lines
Did you reconfirm that the device works well with high bitrates (e.g. 5 Mbps) with no bus off conditions after the change?
Heh, that’s embarrassing. You are correct that the logic is inverted and it passed my tests up to 4 Mbit/s. My guess here is that the loop delay in ideal conditions is low enough to work without TDC. I will see if I can add a temporary higher speed for testing.
I see. Actually, even with a TDCO of zero, the transceiver will still calculate a correct TDCV (because you are in automatic mode). This could explain why you do not get the bus off condition. If you implement the manual mode as described above, then you can force both the tdco and the tdcv to be zero:
$ ip link set can0 type can bitrate 1000000 dbitrate 5000000 fd on \ tdcv 0 tdco 0 tdc-mode manual
That would increase the probability to witness the bus off condition.
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c +1
-0return regmap_write (priv->map_reg,MCP251XFD_REG_TDC , val); }@@ -2083,10 +2093,11 @@ static int mcp251xfd_probe(struct spi_device *spi) priv->can.do_get_berr_counter = mcp251xfd_get_berr_counter; priv->can.bittiming_const = &mcp251xfd_bittiming_const; priv->can.data_bittiming_const = &mcp251xfd_data_bittiming_const; + priv->can.tdc_const = &mcp251xfd_tdc_const;
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
priv->can.ctrlmode_supported=CAN_CTRLMODE_LOOPBACK|CAN_CTRLMODE_LISTENONLY|CAN_CTRLMODE_BERR_REPORTING|CAN_CTRLMODE_FD|CAN_CTRLMODE_FD_NON_ISO|
-CAN_CTRLMODE_CC_LEN8_DLC;
+CAN_CTRLMODE_CC_LEN8_DLC|CAN_CTRLMODE_TDC_AUTO;set_bit(MCP251XFD_FLAGS_DOWN, priv->flags);priv->ndev= ndev;priv->spi= spi;
Yours sincerely,
Vincent Mailhol