netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Marc Kleine-Budde <mkl@pengutronix.de>
To: netdev@vger.kernel.org
Cc: davem@davemloft.net, kuba@kernel.org, linux-can@vger.kernel.org,
	kernel@pengutronix.de, Marc Kleine-Budde <mkl@pengutronix.de>,
	Mark Bath <mark@baggywrinkle.co.uk>
Subject: [PATCH net-next 44/47] can: bittiming: can_sjw_set_default(): use Phase Seg2 / 2 as default for SJW
Date: Mon,  6 Feb 2023 14:16:17 +0100	[thread overview]
Message-ID: <20230206131620.2758724-45-mkl@pengutronix.de> (raw)
In-Reply-To: <20230206131620.2758724-1-mkl@pengutronix.de>

"The (Re-)Synchronization Jump Width (SJW) defines how far a
 resynchronization may move the Sample Point inside the limits defined
 by the Phase Buffer Segments to compensate for edge phase errors." [1]

In other words, this means that the SJW parameter controls the
tolerance of the CAN controller to frequency errors compared to other
CAN controllers.

If the user space does not provide an SJW parameter, the kernel
chooses a default value of 1. This has proven to be a good default
value for classic CAN controllers, but no longer for modern CAN-FD
controllers.

In the past there were CAN controllers like the sja1000 with a rather
limited range of bit timing parameters. For the standard bit rates
this results in the following bit timing parameters:

| Bit timing parameters for sja1000 with 8.000000 MHz ref clock
|                     _----+--------------=> tseg1: 1 …   16
|                    /    /     _---------=> tseg2: 1 …    8
|                   |    |     /    _-----=> sjw:   1 …    4
|                   |    |    |    /    _-=> brp:   1 …   64 (inc: 1)
|                   |    |    |   |    /
|  nominal          |    |    |   |   |     real  Bitrt    nom   real   SampP
|  Bitrate TQ[ns] PrS PhS1 PhS2 SJW BRP  Bitrate  Error  SampP  SampP   Error  BTR0 BTR1
|  1000000    125   2    3    2   1   1  1000000   0.0%  75.0%  75.0%   0.0%   0x00 0x14
|   800000    125   3    4    2   1   1   800000   0.0%  80.0%  80.0%   0.0%   0x00 0x16
|   666666    125   4    4    3   1   1   666666   0.0%  80.0%  75.0%   6.2%   0x00 0x27
|   500000    125   6    7    2   1   1   500000   0.0%  87.5%  87.5%   0.0%   0x00 0x1c
|   250000    250   6    7    2   1   2   250000   0.0%  87.5%  87.5%   0.0%   0x01 0x1c
|   125000    500   6    7    2   1   4   125000   0.0%  87.5%  87.5%   0.0%   0x03 0x1c
|   100000    625   6    7    2   1   5   100000   0.0%  87.5%  87.5%   0.0%   0x04 0x1c
|    83333    750   6    7    2   1   6    83333   0.0%  87.5%  87.5%   0.0%   0x05 0x1c
|    50000   1250   6    7    2   1  10    50000   0.0%  87.5%  87.5%   0.0%   0x09 0x1c
|    33333   1875   6    7    2   1  15    33333   0.0%  87.5%  87.5%   0.0%   0x0e 0x1c
|    20000   3125   6    7    2   1  25    20000   0.0%  87.5%  87.5%   0.0%   0x18 0x1c
|    10000   6250   6    7    2   1  50    10000   0.0%  87.5%  87.5%   0.0%   0x31 0x1c

The attentive reader will notice that the SJW is 1 in most cases,
while the Seg2 phase is 2. Both values are given in TQ units, which in
turn is a duration in nanoseconds.

For example the 500 kbit/s configuration:

|  nominal                                  real  Bitrt    nom   real   SampP
|  Bitrate TQ[ns] PrS PhS1 PhS2 SJW BRP  Bitrate  Error  SampP  SampP   Error  BTR0 BTR1
|   500000    125   6    7    2   1   1   500000   0.0%  87.5%  87.5%   0.0%   0x00 0x1c

the TQ is 125ns, the Phase Seg2 is "2" (== 250ns), the SJW is "1" (==
125 ns).

Looking at a more modern CAN controller like a mcp2518fd, it has wider
bit timing registers.

| Bit timing parameters for mcp251xfd with 40.000000 MHz ref clock
|                     _----+--------------=> tseg1: 2 …  256
|                    /    /     _---------=> tseg2: 1 …  128
|                   |    |     /    _-----=> sjw:   1 …  128
|                   |    |    |    /    _-=> brp:   1 …  256 (inc: 1)
|                   |    |    |   |    /
|  nominal          |    |    |   |   |     real  Bitrt    nom   real   SampP
|  Bitrate TQ[ns] PrS PhS1 PhS2 SJW BRP  Bitrate  Error  SampP  SampP   Error      NBTCFG
|   500000     25  34   35   10   1   1   500000   0.0%  87.5%  87.5%   0.0%   0x00440900

The TQ is 25ns, the Phase Seg 2 is "10" (== 250ns), the SJW is "1" (==
25ns).

Since the kernel chooses a default SJW of 1 regardless of the TQ, this
leads to a much smaller SJW and thus much smaller tolerances to
frequency errors.

To maintain the same oscillator tolerances on controllers with wide
bit timing registers, select a default SJW value of Phase Seg2 / 2
unless Phase Seg 1 is less. This results in the following bit timing
parameters:

| Bit timing parameters for mcp251xfd with 40.000000 MHz ref clock
|                     _----+--------------=> tseg1: 2 …  256
|                    /    /     _---------=> tseg2: 1 …  128
|                   |    |     /    _-----=> sjw:   1 …  128
|                   |    |    |    /    _-=> brp:   1 …  256 (inc: 1)
|                   |    |    |   |    /
|  nominal          |    |    |   |   |     real  Bitrt    nom   real   SampP
|  Bitrate TQ[ns] PrS PhS1 PhS2 SJW BRP  Bitrate  Error  SampP  SampP   Error      NBTCFG
|   500000     25  34   35   10   5   1   500000   0.0%  87.5%  87.5%   0.0%   0x00440904

The TQ is 25ns, the Phase Seg 2 is "10" (== 250ns), the SJW is "5" (==
125ns). Which is the same as on the sja1000 controller.

[1] http://web.archive.org/http://www.oertel-halle.de/files/cia99paper.pdf

Link: https://lore.kernel.org/all/20230202110854.2318594-15-mkl@pengutronix.de
Cc: Mark Bath <mark@baggywrinkle.co.uk>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
 drivers/net/can/dev/bittiming.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/net/can/dev/bittiming.c b/drivers/net/can/dev/bittiming.c
index 68287b79afe8..55714e08ca3a 100644
--- a/drivers/net/can/dev/bittiming.c
+++ b/drivers/net/can/dev/bittiming.c
@@ -11,8 +11,8 @@ void can_sjw_set_default(struct can_bittiming *bt)
 	if (bt->sjw)
 		return;
 
-	/* If user space provides no sjw, use 1 as default */
-	bt->sjw = 1;
+	/* If user space provides no sjw, use sane default of phase_seg2 / 2 */
+	bt->sjw = max(1U, min(bt->phase_seg1, bt->phase_seg2 / 2));
 }
 
 int can_sjw_check(const struct net_device *dev, const struct can_bittiming *bt,
-- 
2.39.1



  parent reply	other threads:[~2023-02-06 13:19 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-06 13:15 [PATCH net-next 0/47] pull-request: can-next 2023-02-06 Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 01/47] can: gw: give feedback on missing CGW_FLAGS_CAN_IIF_TX_OK flag Marc Kleine-Budde
2023-02-07 15:30   ` patchwork-bot+netdevbpf
2023-02-06 13:15 ` [PATCH net-next 02/47] can: isotp: check CAN address family in isotp_bind() Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 03/47] can: mcp251xfd: regmap: optimizing transfer size for CRC transfers size 1 Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 04/47] dt-bindings: can: renesas,rcar-canfd: R-Car V3U is R-Car Gen4 Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 05/47] dt-bindings: can: renesas,rcar-canfd: Document R-Car V4H support Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 06/47] dt-bindings: can: renesas,rcar-canfd: Add transceiver support Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 07/47] can: rcar_canfd: Fix R-Car V3U CAN mode selection Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 08/47] can: rcar_canfd: Fix R-Car V3U GAFLCFG field accesses Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 09/47] can: rcar_canfd: Abstract out DCFG address differences Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 10/47] can: rcar_canfd: Add support for R-Car Gen4 Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 11/47] can: rcar_canfd: Fix R-Car Gen4 DCFG.DSJW field width Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 12/47] can: rcar_canfd: Fix R-Car Gen4 CFCC.CFTML " Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 13/47] can: rcar_canfd: Sort included header files Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 14/47] can: rcar_canfd: Add helper variable dev Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 15/47] can: ems_pci: Fix code style, copyright and email address Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 16/47] can: ems_pci: Add Asix AX99100 definitions Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 17/47] can: ems_pci: Initialize BAR registers Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 18/47] can: ems_pci: Add read/write register and post irq functions Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 19/47] can: ems_pci: Initialize CAN controller base addresses Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 20/47] can: ems_pci: Add IRQ enable Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 21/47] can: ems_pci: Deassert hardware reset Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 22/47] can: ems_pci: Add myself as module author Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 23/47] can: peak_usb: rename device_id to CAN channel ID Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 24/47] can: peak_usb: add callback to read CAN channel ID of PEAK CAN-FD devices Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 25/47] can: peak_usb: allow flashing of the CAN channel ID Marc Kleine-Budde
2023-02-06 13:15 ` [PATCH net-next 26/47] can: peak_usb: replace unregister_netdev() with unregister_candev() Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 27/47] can: peak_usb: add ethtool interface to user-configurable CAN channel identifier Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 28/47] can: peak_usb: export PCAN CAN channel ID as sysfs device attribute Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 29/47] can: peak_usb: align CAN channel ID format in log with sysfs attribute Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 30/47] can: peak_usb: Reorder include directives alphabetically Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 31/47] can: bittiming(): replace open coded variants of can_bit_time() Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 32/47] can: bittiming: can_fixup_bittiming(): use CAN_SYNC_SEG instead of 1 Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 33/47] can: bittiming: can_fixup_bittiming(): set effective tq Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 34/47] can: bittiming: can_get_bittiming(): use direct return and remove unneeded else Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 35/47] can: dev: register_candev(): ensure that bittiming const are valid Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 36/47] can: dev: register_candev(): bail out if both fixed bit rates and bit timing constants are provided Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 37/47] can: netlink: can_validate(): validate sample point for CAN and CAN-FD Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 38/47] can: netlink: can_changelink(): convert from netdev_err() to NL_SET_ERR_MSG_FMT() Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 39/47] can: bittiming: can_changelink() pass extack down callstack Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 40/47] can: bittiming: factor out can_sjw_set_default() and can_sjw_check() Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 41/47] can: bittiming: can_fixup_bittiming(): report error via netlink and harmonize error value Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 42/47] can: bittiming: can_sjw_check(): " Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 43/47] can: bittiming: can_sjw_check(): check that SJW is not longer than either Phase Buffer Segment Marc Kleine-Budde
2023-02-06 13:16 ` Marc Kleine-Budde [this message]
2023-02-06 13:16 ` [PATCH net-next 45/47] can: bittiming: can_calc_bittiming(): clean up SJW handling Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 46/47] can: bittiming: can_calc_bittiming(): convert from netdev_err() to NL_SET_ERR_MSG_FMT() Marc Kleine-Budde
2023-02-06 13:16 ` [PATCH net-next 47/47] can: bittiming: can_validate_bitrate(): report error via netlink Marc Kleine-Budde

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230206131620.2758724-45-mkl@pengutronix.de \
    --to=mkl@pengutronix.de \
    --cc=davem@davemloft.net \
    --cc=kernel@pengutronix.de \
    --cc=kuba@kernel.org \
    --cc=linux-can@vger.kernel.org \
    --cc=mark@baggywrinkle.co.uk \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).