Skip to content

Commit 5e16638

Browse files
optlinkmarckleinebudde
authored andcommitted
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 | tdcv 0..63 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 | tdcv 0..63 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 There 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. The automatic, manual, and off TDC modes were tested at speeds up to, and including, 8 Mbit/s on real hardware and behave as expected. Fixes: 55e5b97 ("can: mcp25xxfd: add driver for Microchip MCP25xxFD SPI CAN") Reported-by: Kelsey Maes <[email protected]> Closes: https://lore.kernel.org/all/[email protected] Reviewed-by: Vincent Mailhol <[email protected]> Signed-off-by: Kelsey Maes <[email protected]> Link: https://patch.msgid.link/[email protected] [mkl: add comment] Signed-off-by: Marc Kleine-Budde <[email protected]>
1 parent dcaeeb8 commit 5e16638

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

drivers/net/can/spi/mcp251xfd/mcp251xfd-core.c

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,24 @@ static const struct can_bittiming_const mcp251xfd_data_bittiming_const = {
7575
.brp_inc = 1,
7676
};
7777

78+
/* The datasheet of the mcp2518fd (DS20006027B) specifies a range of
79+
* [-64,63] for TDCO, indicating a relative TDCO.
80+
*
81+
* Manual tests have shown, that using a relative TDCO configuration
82+
* results in bus off, while an absolute configuration works.
83+
*
84+
* For TDCO use the max value (63) from the data sheet, but 0 as the
85+
* minimum.
86+
*/
87+
static const struct can_tdc_const mcp251xfd_tdc_const = {
88+
.tdcv_min = 0,
89+
.tdcv_max = 63,
90+
.tdco_min = 0,
91+
.tdco_max = 63,
92+
.tdcf_min = 0,
93+
.tdcf_max = 0,
94+
};
95+
7896
static const char *__mcp251xfd_get_model_str(enum mcp251xfd_model model)
7997
{
8098
switch (model) {
@@ -510,8 +528,7 @@ static int mcp251xfd_set_bittiming(const struct mcp251xfd_priv *priv)
510528
{
511529
const struct can_bittiming *bt = &priv->can.bittiming;
512530
const struct can_bittiming *dbt = &priv->can.data_bittiming;
513-
u32 val = 0;
514-
s8 tdco;
531+
u32 tdcmod, val = 0;
515532
int err;
516533

517534
/* CAN Control Register
@@ -575,11 +592,16 @@ static int mcp251xfd_set_bittiming(const struct mcp251xfd_priv *priv)
575592
return err;
576593

577594
/* Transmitter Delay Compensation */
578-
tdco = clamp_t(int, dbt->brp * (dbt->prop_seg + dbt->phase_seg1),
579-
-64, 63);
580-
val = FIELD_PREP(MCP251XFD_REG_TDC_TDCMOD_MASK,
581-
MCP251XFD_REG_TDC_TDCMOD_AUTO) |
582-
FIELD_PREP(MCP251XFD_REG_TDC_TDCO_MASK, tdco);
595+
if (priv->can.ctrlmode & CAN_CTRLMODE_TDC_AUTO)
596+
tdcmod = MCP251XFD_REG_TDC_TDCMOD_AUTO;
597+
else if (priv->can.ctrlmode & CAN_CTRLMODE_TDC_MANUAL)
598+
tdcmod = MCP251XFD_REG_TDC_TDCMOD_MANUAL;
599+
else
600+
tdcmod = MCP251XFD_REG_TDC_TDCMOD_DISABLED;
601+
602+
val = FIELD_PREP(MCP251XFD_REG_TDC_TDCMOD_MASK, tdcmod) |
603+
FIELD_PREP(MCP251XFD_REG_TDC_TDCV_MASK, priv->can.tdc.tdcv) |
604+
FIELD_PREP(MCP251XFD_REG_TDC_TDCO_MASK, priv->can.tdc.tdco);
583605

584606
return regmap_write(priv->map_reg, MCP251XFD_REG_TDC, val);
585607
}
@@ -2083,10 +2105,12 @@ static int mcp251xfd_probe(struct spi_device *spi)
20832105
priv->can.do_get_berr_counter = mcp251xfd_get_berr_counter;
20842106
priv->can.bittiming_const = &mcp251xfd_bittiming_const;
20852107
priv->can.data_bittiming_const = &mcp251xfd_data_bittiming_const;
2108+
priv->can.tdc_const = &mcp251xfd_tdc_const;
20862109
priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
20872110
CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_BERR_REPORTING |
20882111
CAN_CTRLMODE_FD | CAN_CTRLMODE_FD_NON_ISO |
2089-
CAN_CTRLMODE_CC_LEN8_DLC;
2112+
CAN_CTRLMODE_CC_LEN8_DLC | CAN_CTRLMODE_TDC_AUTO |
2113+
CAN_CTRLMODE_TDC_MANUAL;
20902114
set_bit(MCP251XFD_FLAGS_DOWN, priv->flags);
20912115
priv->ndev = ndev;
20922116
priv->spi = spi;

0 commit comments

Comments
 (0)