Skip to contentSkip to conversation

can: dev: Add check for the minimum value of a bit time

RFC PATCH New Joakim Zhang 1 participant 2 messages Oct 30, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 can: dev: Add check for the minimum value of a bit time

As ISO 11898-1 said: The total number of time quanta in a nominal bit time shall be programmable at least from 8 to 25 for implementation that are not FD enabled. For implementations that are FD enabled, the total number of time quanta in a data bit time shall be programmable at least from 5 to 25 and in a nominal bit time at least from 8 to 80.

So the minimum value for nominal bit time is 8 and for data bit time is 5, had better ensure the minimum value of a bit time.

---
 drivers/net/can/dev.c | 37 +++++++++++++++++++++++++++++--------
 1 file changed, 29 insertions(+), 8 deletions(-)
drivers/net/can/dev.c +29 -8
index ac86be52b461..69e693c79579 100644
@@ -115,7 +115,8 @@ can_update_sample_point(const struct can_bittiming_const *btc,
 	return best_sample_point;
 }
 
-static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
+static int can_calc_bittiming(struct net_device *dev, bool is_data_bt,
+			      struct can_bittiming *bt,
 			      const struct can_bittiming_const *btc)
 {
 	struct can_priv *priv = netdev_priv(dev);
@@ -147,6 +148,14 @@ static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
 	     tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
 		tsegall = CAN_CALC_SYNC_SEG + tseg / 2;
 
+		/* the total number of time quanta in a data bit time
+		 * (SYNC_SEG + TSEG1 + TSEG2) shall be programmable at least 5.
+		 * the total number of time quanta in a nominal bit time
+		 * (SYNC_SEG + TSEG1 + TSEG2) shall be programmable at least 8.
+		 */
+		if ((is_data_bt && tsegall < 5) || (!is_data_bt && tsegall < 8))
+			continue;
+
 		/* Compute all possible tseg choices (tseg=tseg1+tseg2) */
 		brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
 
@@ -228,7 +237,8 @@ static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
 	return 0;
 }
 #else /* !CONFIG_CAN_CALC_BITTIMING */
-static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
+static int can_calc_bittiming(struct net_device *dev, bool is_data_bt,
+			      struct can_bittiming *bt,
 			      const struct can_bittiming_const *btc)
 {
 	netdev_err(dev, "bit-timing calculation not available\n");
@@ -241,7 +251,8 @@ static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
  * prescaler value brp. You can find more information in the header
  * file linux/can/netlink.h.
  */
-static int can_fixup_bittiming(struct net_device *dev, struct can_bittiming *bt,
+static int can_fixup_bittiming(struct net_device *dev, bool is_data_bt,
+			       struct can_bittiming *bt,
 			       const struct can_bittiming_const *btc)
 {
 	struct can_priv *priv = netdev_priv(dev);
@@ -269,6 +280,15 @@ static int can_fixup_bittiming(struct net_device *dev, struct can_bittiming *bt,
 		return -EINVAL;
 
 	alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1;
+
+	/* the total number of time quanta in a data bit time
+	 * (SYNC_SEG + TSEG1 + TSEG2) shall be programmable at least 5.
+	 * the total number of time quanta in a nominal bit time
+	 * (SYNC_SEG + TSEG1 + TSEG2) shall be programmable at least 8.
+	 */
+	if ((is_data_bt && alltseg < 5) || (!is_data_bt && alltseg < 8))
+		return -EINVAL;
+
 	bt->bitrate = priv->clock.freq / (bt->brp * alltseg);
 	bt->sample_point = ((tseg1 + 1) * 1000) / alltseg;
 
@@ -295,7 +315,8 @@ can_validate_bitrate(struct net_device *dev, struct can_bittiming *bt,
 	return 0;
 }
 
-static int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt,
+static int can_get_bittiming(struct net_device *dev, bool is_data_bt,
+			     struct can_bittiming *bt,
 			     const struct can_bittiming_const *btc,
 			     const u32 *bitrate_const,
 			     const unsigned int bitrate_const_cnt)
@@ -308,9 +329,9 @@ static int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt,
 	 * provided directly which are then checked and fixed up.
 	 */
 	if (!bt->tq && bt->bitrate && btc)
-		err = can_calc_bittiming(dev, bt, btc);
+		err = can_calc_bittiming(dev, is_data_bt, bt, btc);
 	else if (bt->tq && !bt->bitrate && btc)
-		err = can_fixup_bittiming(dev, bt, btc);
+		err = can_fixup_bittiming(dev, is_data_bt, bt, btc);
 	else if (!bt->tq && bt->bitrate && bitrate_const)
 		err = can_validate_bitrate(dev, bt, bitrate_const,
 					   bitrate_const_cnt);
@@ -944,7 +965,7 @@ static int can_changelink(struct net_device *dev, struct nlattr *tb[],
 			return -EOPNOTSUPP;
 
 		memcpy(&bt, nla_data(data[IFLA_CAN_BITTIMING]), sizeof(bt));
-		err = can_get_bittiming(dev, &bt,
+		err = can_get_bittiming(dev, false, &bt,
 					priv->bittiming_const,
 					priv->bitrate_const,
 					priv->bitrate_const_cnt);
@@ -1035,7 +1056,7 @@ static int can_changelink(struct net_device *dev, struct nlattr *tb[],
 
 		memcpy(&dbt, nla_data(data[IFLA_CAN_DATA_BITTIMING]),
 		       sizeof(dbt));
-		err = can_get_bittiming(dev, &dbt,
+		err = can_get_bittiming(dev, true, &dbt,
 					priv->data_bittiming_const,
 					priv->data_bitrate_const,
 					priv->data_bitrate_const_cnt);
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/1 can: flexcan: Add check for transceiver maximum bitrate limitation

CAN FD can transmit up to 8Mbps, but some transceivers only can support 5Mbps, so add check in driver.

---
 drivers/net/can/flexcan.c | 1 +
 1 file changed, 1 insertion(+)
drivers/net/can/flexcan.c +1 -0
index c5d53edb17ad..ee2d57b5a382 100644
@@ -1626,6 +1626,7 @@ static int flexcan_probe(struct platform_device *pdev)
 		goto failed_register;
 	}
 
+	of_can_transceiver(dev);
 	devm_can_led_init(dev);
 
 	if (priv->devtype_data->quirks & FLEXCAN_QUIRK_SETUP_STOP_MODE) {
Signature · 1 line
2.17.1