Skip to content

Commit 0aa639d

Browse files
Jimmy Assarssonmarckleinebudde
authored andcommitted
can: kvaser_usb: leaf: Add hardware timestamp support to usbcan devices
Add hardware timestamp support for all usbcan based devices (M16C). The usbcan firmware is slightly different compared to the other Kvaser USB interfaces: - The timestamp is provided by a 32-bit counter, with 10us resolution. Hence, the hardware timestamp will wrap after less than 12 hours. - Each Rx CAN or Tx ACK command only contains the 16-bits LSB of the timestamp counter. - The 16-bits MSB are sent in an asynchronous event (command), if any change occurred in the MSB since the last event. Signed-off-by: Jimmy Assarsson <[email protected]> Reviewed-by: Vincent Mailhol <[email protected]> Link: https://lore.kernel.org/all/[email protected] Signed-off-by: Marc Kleine-Budde <[email protected]>
1 parent c644c96 commit 0aa639d

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ static const struct kvaser_usb_driver_info kvaser_usb_driver_info_hydra = {
100100

101101
static const struct kvaser_usb_driver_info kvaser_usb_driver_info_usbcan = {
102102
.quirks = KVASER_USB_QUIRK_HAS_TXRX_ERRORS |
103-
KVASER_USB_QUIRK_HAS_SILENT_MODE,
103+
KVASER_USB_QUIRK_HAS_SILENT_MODE |
104+
KVASER_USB_QUIRK_HAS_HARDWARE_TIMESTAMP,
104105
.family = KVASER_USBCAN,
105106
.ops = &kvaser_usb_leaf_dev_ops,
106107
};

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121

122122
/* USBCanII timestamp */
123123
#define KVASER_USB_USBCAN_CLK_OVERFLOW_MASK GENMASK(31, 16)
124+
#define KVASER_USB_USBCAN_TIMESTAMP_FACTOR 10
124125

125126
struct kvaser_cmd_simple {
126127
u8 tid;
@@ -536,6 +537,15 @@ static const struct kvaser_usb_dev_cfg kvaser_usb_leaf_imx_dev_cfg_32mhz = {
536537
.bittiming_const = &kvaser_usb_flexc_bittiming_const,
537538
};
538539

540+
static inline ktime_t kvaser_usb_usbcan_timestamp_to_ktime(const struct kvaser_usb *dev,
541+
__le16 timestamp)
542+
{
543+
u64 ticks = le16_to_cpu(timestamp) |
544+
dev->card_data.usbcan_timestamp_msb;
545+
546+
return kvaser_usb_ticks_to_ktime(dev->cfg, ticks * KVASER_USB_USBCAN_TIMESTAMP_FACTOR);
547+
}
548+
539549
static int kvaser_usb_leaf_verify_size(const struct kvaser_usb *dev,
540550
const struct kvaser_cmd *cmd)
541551
{
@@ -978,6 +988,7 @@ static void kvaser_usb_leaf_tx_acknowledge(const struct kvaser_usb *dev,
978988
hwtstamp = kvaser_usb_timestamp48_to_ktime(dev->cfg, cmd->u.leaf.tx_ack.time);
979989
break;
980990
case KVASER_USBCAN:
991+
hwtstamp = kvaser_usb_usbcan_timestamp_to_ktime(dev, cmd->u.usbcan.tx_ack.time);
981992
break;
982993
}
983994

@@ -1398,6 +1409,7 @@ static void kvaser_usb_leaf_rx_can_msg(const struct kvaser_usb *dev,
13981409
break;
13991410
case KVASER_USBCAN:
14001411
rx_data = cmd->u.usbcan.rx_can.data;
1412+
hwtstamp = kvaser_usb_usbcan_timestamp_to_ktime(dev, cmd->u.usbcan.rx_can.time);
14011413
break;
14021414
}
14031415

0 commit comments

Comments
 (0)