Skip to content

Commit fb12797

Browse files
Jimmy Assarssonmarckleinebudde
authored andcommitted
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: 080f40a ("can: kvaser_usb: Add support for Kvaser CAN/USB devices") Link: https://lore.kernel.org/all/[email protected] Cc: [email protected] Signed-off-by: Jimmy Assarsson <[email protected]> Signed-off-by: Marc Kleine-Budde <[email protected]>
1 parent 36aea60 commit fb12797

File tree

1 file changed

+73
-28
lines changed

1 file changed

+73
-28
lines changed

drivers/net/can/usb/kvaser_usb/kvaser_usb_leaf.c

Lines changed: 73 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@
2828

2929
#include "kvaser_usb.h"
3030

31-
/* Forward declaration */
32-
static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg;
33-
34-
#define CAN_USB_CLOCK 8000000
3531
#define MAX_USBCAN_NET_DEVICES 2
3632

3733
/* Command header size */
@@ -80,6 +76,12 @@ static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg;
8076

8177
#define CMD_LEAF_LOG_MESSAGE 106
8278

79+
/* Leaf frequency options */
80+
#define KVASER_USB_LEAF_SWOPTION_FREQ_MASK 0x60
81+
#define KVASER_USB_LEAF_SWOPTION_FREQ_16_MHZ_CLK 0
82+
#define KVASER_USB_LEAF_SWOPTION_FREQ_32_MHZ_CLK BIT(5)
83+
#define KVASER_USB_LEAF_SWOPTION_FREQ_24_MHZ_CLK BIT(6)
84+
8385
/* error factors */
8486
#define M16C_EF_ACKE BIT(0)
8587
#define M16C_EF_CRCE BIT(1)
@@ -340,6 +342,50 @@ struct kvaser_usb_err_summary {
340342
};
341343
};
342344

345+
static const struct can_bittiming_const kvaser_usb_leaf_bittiming_const = {
346+
.name = "kvaser_usb",
347+
.tseg1_min = KVASER_USB_TSEG1_MIN,
348+
.tseg1_max = KVASER_USB_TSEG1_MAX,
349+
.tseg2_min = KVASER_USB_TSEG2_MIN,
350+
.tseg2_max = KVASER_USB_TSEG2_MAX,
351+
.sjw_max = KVASER_USB_SJW_MAX,
352+
.brp_min = KVASER_USB_BRP_MIN,
353+
.brp_max = KVASER_USB_BRP_MAX,
354+
.brp_inc = KVASER_USB_BRP_INC,
355+
};
356+
357+
static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg_8mhz = {
358+
.clock = {
359+
.freq = 8000000,
360+
},
361+
.timestamp_freq = 1,
362+
.bittiming_const = &kvaser_usb_leaf_bittiming_const,
363+
};
364+
365+
static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg_16mhz = {
366+
.clock = {
367+
.freq = 16000000,
368+
},
369+
.timestamp_freq = 1,
370+
.bittiming_const = &kvaser_usb_leaf_bittiming_const,
371+
};
372+
373+
static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg_24mhz = {
374+
.clock = {
375+
.freq = 24000000,
376+
},
377+
.timestamp_freq = 1,
378+
.bittiming_const = &kvaser_usb_leaf_bittiming_const,
379+
};
380+
381+
static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg_32mhz = {
382+
.clock = {
383+
.freq = 32000000,
384+
},
385+
.timestamp_freq = 1,
386+
.bittiming_const = &kvaser_usb_leaf_bittiming_const,
387+
};
388+
343389
static void *
344390
kvaser_usb_leaf_frame_to_cmd(const struct kvaser_usb_net_priv *priv,
345391
const struct sk_buff *skb, int *frame_len,
@@ -471,6 +517,27 @@ static int kvaser_usb_leaf_send_simple_cmd(const struct kvaser_usb *dev,
471517
return rc;
472518
}
473519

520+
static void kvaser_usb_leaf_get_software_info_leaf(struct kvaser_usb *dev,
521+
const struct leaf_cmd_softinfo *softinfo)
522+
{
523+
u32 sw_options = le32_to_cpu(softinfo->sw_options);
524+
525+
dev->fw_version = le32_to_cpu(softinfo->fw_version);
526+
dev->max_tx_urbs = le16_to_cpu(softinfo->max_outstanding_tx);
527+
528+
switch (sw_options & KVASER_USB_LEAF_SWOPTION_FREQ_MASK) {
529+
case KVASER_USB_LEAF_SWOPTION_FREQ_16_MHZ_CLK:
530+
dev->cfg = &kvaser_usb_leaf_dev_cfg_16mhz;
531+
break;
532+
case KVASER_USB_LEAF_SWOPTION_FREQ_24_MHZ_CLK:
533+
dev->cfg = &kvaser_usb_leaf_dev_cfg_24mhz;
534+
break;
535+
case KVASER_USB_LEAF_SWOPTION_FREQ_32_MHZ_CLK:
536+
dev->cfg = &kvaser_usb_leaf_dev_cfg_32mhz;
537+
break;
538+
}
539+
}
540+
474541
static int kvaser_usb_leaf_get_software_info_inner(struct kvaser_usb *dev)
475542
{
476543
struct kvaser_cmd cmd;
@@ -486,14 +553,13 @@ static int kvaser_usb_leaf_get_software_info_inner(struct kvaser_usb *dev)
486553

487554
switch (dev->card_data.leaf.family) {
488555
case KVASER_LEAF:
489-
dev->fw_version = le32_to_cpu(cmd.u.leaf.softinfo.fw_version);
490-
dev->max_tx_urbs =
491-
le16_to_cpu(cmd.u.leaf.softinfo.max_outstanding_tx);
556+
kvaser_usb_leaf_get_software_info_leaf(dev, &cmd.u.leaf.softinfo);
492557
break;
493558
case KVASER_USBCAN:
494559
dev->fw_version = le32_to_cpu(cmd.u.usbcan.softinfo.fw_version);
495560
dev->max_tx_urbs =
496561
le16_to_cpu(cmd.u.usbcan.softinfo.max_outstanding_tx);
562+
dev->cfg = &kvaser_usb_leaf_dev_cfg_8mhz;
497563
break;
498564
}
499565

@@ -1225,24 +1291,11 @@ static int kvaser_usb_leaf_init_card(struct kvaser_usb *dev)
12251291
{
12261292
struct kvaser_usb_dev_card_data *card_data = &dev->card_data;
12271293

1228-
dev->cfg = &kvaser_usb_leaf_dev_cfg;
12291294
card_data->ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
12301295

12311296
return 0;
12321297
}
12331298

1234-
static const struct can_bittiming_const kvaser_usb_leaf_bittiming_const = {
1235-
.name = "kvaser_usb",
1236-
.tseg1_min = KVASER_USB_TSEG1_MIN,
1237-
.tseg1_max = KVASER_USB_TSEG1_MAX,
1238-
.tseg2_min = KVASER_USB_TSEG2_MIN,
1239-
.tseg2_max = KVASER_USB_TSEG2_MAX,
1240-
.sjw_max = KVASER_USB_SJW_MAX,
1241-
.brp_min = KVASER_USB_BRP_MIN,
1242-
.brp_max = KVASER_USB_BRP_MAX,
1243-
.brp_inc = KVASER_USB_BRP_INC,
1244-
};
1245-
12461299
static int kvaser_usb_leaf_set_bittiming(struct net_device *netdev)
12471300
{
12481301
struct kvaser_usb_net_priv *priv = netdev_priv(netdev);
@@ -1348,11 +1401,3 @@ const struct kvaser_usb_dev_ops kvaser_usb_leaf_dev_ops = {
13481401
.dev_read_bulk_callback = kvaser_usb_leaf_read_bulk_callback,
13491402
.dev_frame_to_cmd = kvaser_usb_leaf_frame_to_cmd,
13501403
};
1351-
1352-
static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_dev_cfg = {
1353-
.clock = {
1354-
.freq = CAN_USB_CLOCK,
1355-
},
1356-
.timestamp_freq = 1,
1357-
.bittiming_const = &kvaser_usb_leaf_bittiming_const,
1358-
};

0 commit comments

Comments
 (0)