Skip to content

Commit 6f61564

Browse files
Fedor Rossgregkh
authored andcommitted
can: mcp251xfd: __mcp251xfd_chip_set_mode(): increase poll timeout
commit 9efa1a5 upstream. The mcp251xfd controller needs an idle bus to enter 'Normal CAN 2.0 mode' or . The maximum length of a CAN frame is 736 bits (64 data bytes, CAN-FD, EFF mode, worst case bit stuffing and interframe spacing). For low bit rates like 10 kbit/s the arbitrarily chosen MCP251XFD_POLL_TIMEOUT_US of 1 ms is too small. Otherwise during polling for the CAN controller to enter 'Normal CAN 2.0 mode' the timeout limit is exceeded and the configuration fails with: | $ ip link set dev can1 up type can bitrate 10000 | [ 731.911072] mcp251xfd spi2.1 can1: Controller failed to enter mode CAN 2.0 Mode (6) and stays in Configuration Mode (4) (con=0x068b0760, osc=0x00000468). | [ 731.927192] mcp251xfd spi2.1 can1: CRC read error at address 0x0e0c (length=4, data=00 00 00 00, CRC=0x0000) retrying. | [ 731.938101] A link change request failed with some changes committed already. Interface can1 may have been left with an inconsistent configuration, please check. | RTNETLINK answers: Connection timed out Make MCP251XFD_POLL_TIMEOUT_US timeout calculation dynamic. Use maximum of 1ms and bit time of 1 full 64 data bytes CAN-FD frame in EFF mode, worst case bit stuffing and interframe spacing at the current bit rate. For easier backporting define the macro MCP251XFD_FRAME_LEN_MAX_BITS that holds the max frame length in bits, which is 736. This can be replaced by can_frame_bits(true, true, true, true, CANFD_MAX_DLEN) in a cleanup patch later. Fixes: 55e5b97 ("can: mcp25xxfd: add driver for Microchip MCP25xxFD SPI CAN") Signed-off-by: Fedor Ross <[email protected]> Signed-off-by: Marek Vasut <[email protected]> Cc: [email protected] Link: https://lore.kernel.org/all/20230717-mcp251xfd-fix-increase-poll-timeout-v5-1-06600f34c684@pengutronix.de Signed-off-by: Marc Kleine-Budde <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 292f045 commit 6f61564

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ static int
227227
__mcp251xfd_chip_set_mode(const struct mcp251xfd_priv *priv,
228228
const u8 mode_req, bool nowait)
229229
{
230+
const struct can_bittiming *bt = &priv->can.bittiming;
231+
unsigned long timeout_us = MCP251XFD_POLL_TIMEOUT_US;
230232
u32 con = 0, con_reqop, osc = 0;
231233
u8 mode;
232234
int err;
@@ -246,12 +248,16 @@ __mcp251xfd_chip_set_mode(const struct mcp251xfd_priv *priv,
246248
if (mode_req == MCP251XFD_REG_CON_MODE_SLEEP || nowait)
247249
return 0;
248250

251+
if (bt->bitrate)
252+
timeout_us = max_t(unsigned long, timeout_us,
253+
MCP251XFD_FRAME_LEN_MAX_BITS * USEC_PER_SEC /
254+
bt->bitrate);
255+
249256
err = regmap_read_poll_timeout(priv->map_reg, MCP251XFD_REG_CON, con,
250257
!mcp251xfd_reg_invalid(con) &&
251258
FIELD_GET(MCP251XFD_REG_CON_OPMOD_MASK,
252259
con) == mode_req,
253-
MCP251XFD_POLL_SLEEP_US,
254-
MCP251XFD_POLL_TIMEOUT_US);
260+
MCP251XFD_POLL_SLEEP_US, timeout_us);
255261
if (err != -ETIMEDOUT && err != -EBADMSG)
256262
return err;
257263

drivers/net/can/spi/mcp251xfd/mcp251xfd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ static_assert(MCP251XFD_TIMESTAMP_WORK_DELAY_SEC <
387387
#define MCP251XFD_OSC_STAB_TIMEOUT_US (10 * MCP251XFD_OSC_STAB_SLEEP_US)
388388
#define MCP251XFD_POLL_SLEEP_US (10)
389389
#define MCP251XFD_POLL_TIMEOUT_US (USEC_PER_MSEC)
390+
#define MCP251XFD_FRAME_LEN_MAX_BITS (736)
390391

391392
/* Misc */
392393
#define MCP251XFD_NAPI_WEIGHT 32

0 commit comments

Comments
 (0)