Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

can: kvaser_usb: get CAN clock frequency from device

The CAN clock frequency is used when calculating the CAN bittiming
parameters. When wrong clock frequency is used, the device may end up
with wrong bittiming parameters, depending on user requested bittiming
parameters.

To avoid this, get the CAN clock frequency from the device. Various
existing Kvaser Leaf products use different CAN clocks.

Fixes: 080f40a6fa28 ("can: kvaser_usb: Add support for Kvaser CAN/USB devices")
Link: https://lore.kernel.org/all/20211208152122.250852-2-extja@kvaser.com
Cc: stable@vger.kernel.org
Signed-off-by: Jimmy Assarsson <extja@kvaser.com>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>

authored by

Jimmy Assarsson and committed by
Marc Kleine-Budde
fb12797a 36aea60f

+73 -28
+73 -28
drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c
··· 28 28 29 29 #include "kvaser_usb.h" 30 30 31 - /* Forward declaration */ 32 - static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg; 33 - 34 - #define CAN_USB_CLOCK 8000000 35 31 #define MAX_USBCAN_NET_DEVICES 2 36 32 37 33 /* Command header size */ ··· 75 79 #define CMD_FLUSH_QUEUE_REPLY 68 76 80 77 81 #define CMD_LEAF_LOG_MESSAGE 106 82 + 83 + /* Leaf frequency options */ 84 + #define KVASER_USB_LEAF_SWOPTION_FREQ_MASK 0x60 85 + #define KVASER_USB_LEAF_SWOPTION_FREQ_16_MHZ_CLK 0 86 + #define KVASER_USB_LEAF_SWOPTION_FREQ_32_MHZ_CLK BIT(5) 87 + #define KVASER_USB_LEAF_SWOPTION_FREQ_24_MHZ_CLK BIT(6) 78 88 79 89 /* error factors */ 80 90 #define M16C_EF_ACKE BIT(0) ··· 342 340 }; 343 341 }; 344 342 343 + static const struct can_bittiming_const kvaser_usb_leaf_bittiming_const = { 344 + .name = "kvaser_usb", 345 + .tseg1_min = KVASER_USB_TSEG1_MIN, 346 + .tseg1_max = KVASER_USB_TSEG1_MAX, 347 + .tseg2_min = KVASER_USB_TSEG2_MIN, 348 + .tseg2_max = KVASER_USB_TSEG2_MAX, 349 + .sjw_max = KVASER_USB_SJW_MAX, 350 + .brp_min = KVASER_USB_BRP_MIN, 351 + .brp_max = KVASER_USB_BRP_MAX, 352 + .brp_inc = KVASER_USB_BRP_INC, 353 + }; 354 + 355 + static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg_8mhz = { 356 + .clock = { 357 + .freq = 8000000, 358 + }, 359 + .timestamp_freq = 1, 360 + .bittiming_const = &kvaser_usb_leaf_bittiming_const, 361 + }; 362 + 363 + static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg_16mhz = { 364 + .clock = { 365 + .freq = 16000000, 366 + }, 367 + .timestamp_freq = 1, 368 + .bittiming_const = &kvaser_usb_leaf_bittiming_const, 369 + }; 370 + 371 + static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg_24mhz = { 372 + .clock = { 373 + .freq = 24000000, 374 + }, 375 + .timestamp_freq = 1, 376 + .bittiming_const = &kvaser_usb_leaf_bittiming_const, 377 + }; 378 + 379 + static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg_32mhz = { 380 + .clock = { 381 + .freq = 32000000, 382 + }, 383 + .timestamp_freq = 1, 384 + .bittiming_const = &kvaser_usb_leaf_bittiming_const, 385 + }; 386 + 345 387 static void * 346 388 kvaser_usb_leaf_frame_to_cmd(const struct kvaser_usb_net_priv *priv, 347 389 const struct sk_buff *skb, int *frame_len, ··· 517 471 return rc; 518 472 } 519 473 474 + static void kvaser_usb_leaf_get_software_info_leaf(struct kvaser_usb *dev, 475 + const struct leaf_cmd_softinfo *softinfo) 476 + { 477 + u32 sw_options = le32_to_cpu(softinfo->sw_options); 478 + 479 + dev->fw_version = le32_to_cpu(softinfo->fw_version); 480 + dev->max_tx_urbs = le16_to_cpu(softinfo->max_outstanding_tx); 481 + 482 + switch (sw_options & KVASER_USB_LEAF_SWOPTION_FREQ_MASK) { 483 + case KVASER_USB_LEAF_SWOPTION_FREQ_16_MHZ_CLK: 484 + dev->cfg = &kvaser_usb_leaf_dev_cfg_16mhz; 485 + break; 486 + case KVASER_USB_LEAF_SWOPTION_FREQ_24_MHZ_CLK: 487 + dev->cfg = &kvaser_usb_leaf_dev_cfg_24mhz; 488 + break; 489 + case KVASER_USB_LEAF_SWOPTION_FREQ_32_MHZ_CLK: 490 + dev->cfg = &kvaser_usb_leaf_dev_cfg_32mhz; 491 + break; 492 + } 493 + } 494 + 520 495 static int kvaser_usb_leaf_get_software_info_inner(struct kvaser_usb *dev) 521 496 { 522 497 struct kvaser_cmd cmd; ··· 553 486 554 487 switch (dev->card_data.leaf.family) { 555 488 case KVASER_LEAF: 556 - dev->fw_version = le32_to_cpu(cmd.u.leaf.softinfo.fw_version); 557 - dev->max_tx_urbs = 558 - le16_to_cpu(cmd.u.leaf.softinfo.max_outstanding_tx); 489 + kvaser_usb_leaf_get_software_info_leaf(dev, &cmd.u.leaf.softinfo); 559 490 break; 560 491 case KVASER_USBCAN: 561 492 dev->fw_version = le32_to_cpu(cmd.u.usbcan.softinfo.fw_version); 562 493 dev->max_tx_urbs = 563 494 le16_to_cpu(cmd.u.usbcan.softinfo.max_outstanding_tx); 495 + dev->cfg = &kvaser_usb_leaf_dev_cfg_8mhz; 564 496 break; 565 497 } 566 498 ··· 1291 1225 { 1292 1226 struct kvaser_usb_dev_card_data *card_data = &dev->card_data; 1293 1227 1294 - dev->cfg = &kvaser_usb_leaf_dev_cfg; 1295 1228 card_data->ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES; 1296 1229 1297 1230 return 0; 1298 1231 } 1299 - 1300 - static const struct can_bittiming_const kvaser_usb_leaf_bittiming_const = { 1301 - .name = "kvaser_usb", 1302 - .tseg1_min = KVASER_USB_TSEG1_MIN, 1303 - .tseg1_max = KVASER_USB_TSEG1_MAX, 1304 - .tseg2_min = KVASER_USB_TSEG2_MIN, 1305 - .tseg2_max = KVASER_USB_TSEG2_MAX, 1306 - .sjw_max = KVASER_USB_SJW_MAX, 1307 - .brp_min = KVASER_USB_BRP_MIN, 1308 - .brp_max = KVASER_USB_BRP_MAX, 1309 - .brp_inc = KVASER_USB_BRP_INC, 1310 - }; 1311 1232 1312 1233 static int kvaser_usb_leaf_set_bittiming(struct net_device *netdev) 1313 1234 { ··· 1400 1347 .dev_flush_queue = kvaser_usb_leaf_flush_queue, 1401 1348 .dev_read_bulk_callback = kvaser_usb_leaf_read_bulk_callback, 1402 1349 .dev_frame_to_cmd = kvaser_usb_leaf_frame_to_cmd, 1403 - }; 1404 - 1405 - static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg = { 1406 - .clock = { 1407 - .freq = CAN_USB_CLOCK, 1408 - }, 1409 - .timestamp_freq = 1, 1410 - .bittiming_const = &kvaser_usb_leaf_bittiming_const, 1411 1350 };