at v5.13 123 lines 4.0 kB view raw
1/* SPDX-License-Identifier: GPL-2.0-only */ 2/* Copyright (c) 2020 Pengutronix, Marc Kleine-Budde <kernel@pengutronix.de> 3 * Copyright (c) 2021 Vincent Mailhol <mailhol.vincent@wanadoo.fr> 4 */ 5 6#ifndef _CAN_BITTIMING_H 7#define _CAN_BITTIMING_H 8 9#include <linux/netdevice.h> 10#include <linux/can/netlink.h> 11 12#define CAN_SYNC_SEG 1 13 14 15/* Kilobits and Megabits per second */ 16#define CAN_KBPS 1000UL 17#define CAN_MBPS 1000000UL 18 19/* Megahertz */ 20#define CAN_MHZ 1000000UL 21 22/* 23 * struct can_tdc - CAN FD Transmission Delay Compensation parameters 24 * 25 * At high bit rates, the propagation delay from the TX pin to the RX 26 * pin of the transceiver causes measurement errors: the sample point 27 * on the RX pin might occur on the previous bit. 28 * 29 * To solve this issue, ISO 11898-1 introduces in section 11.3.3 30 * "Transmitter delay compensation" a SSP (Secondary Sample Point) 31 * equal to the distance, in time quanta, from the start of the bit 32 * time on the TX pin to the actual measurement on the RX pin. 33 * 34 * This structure contains the parameters to calculate that SSP. 35 * 36 * @tdcv: Transmitter Delay Compensation Value. Distance, in time 37 * quanta, from when the bit is sent on the TX pin to when it is 38 * received on the RX pin of the transmitter. Possible options: 39 * 40 * O: automatic mode. The controller dynamically measure @tdcv 41 * for each transmitted CAN FD frame. 42 * 43 * Other values: manual mode. Use the fixed provided value. 44 * 45 * @tdco: Transmitter Delay Compensation Offset. Offset value, in time 46 * quanta, defining the distance between the start of the bit 47 * reception on the RX pin of the transceiver and the SSP 48 * position such as SSP = @tdcv + @tdco. 49 * 50 * If @tdco is zero, then TDC is disabled and both @tdcv and 51 * @tdcf should be ignored. 52 * 53 * @tdcf: Transmitter Delay Compensation Filter window. Defines the 54 * minimum value for the SSP position in time quanta. If SSP is 55 * less than @tdcf, then no delay compensations occur and the 56 * normal sampling point is used instead. The feature is enabled 57 * if and only if @tdcv is set to zero (automatic mode) and @tdcf 58 * is configured to a value greater than @tdco. 59 */ 60struct can_tdc { 61 u32 tdcv; 62 u32 tdco; 63 u32 tdcf; 64}; 65 66/* 67 * struct can_tdc_const - CAN hardware-dependent constant for 68 * Transmission Delay Compensation 69 * 70 * @tdcv_max: Transmitter Delay Compensation Value maximum value. 71 * Should be set to zero if the controller does not support 72 * manual mode for tdcv. 73 * @tdco_max: Transmitter Delay Compensation Offset maximum value. 74 * Should not be zero. If the controller does not support TDC, 75 * then the pointer to this structure should be NULL. 76 * @tdcf_max: Transmitter Delay Compensation Filter window maximum 77 * value. Should be set to zero if the controller does not 78 * support this feature. 79 */ 80struct can_tdc_const { 81 u32 tdcv_max; 82 u32 tdco_max; 83 u32 tdcf_max; 84}; 85 86#ifdef CONFIG_CAN_CALC_BITTIMING 87int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt, 88 const struct can_bittiming_const *btc); 89 90void can_calc_tdco(struct net_device *dev); 91#else /* !CONFIG_CAN_CALC_BITTIMING */ 92static inline int 93can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt, 94 const struct can_bittiming_const *btc) 95{ 96 netdev_err(dev, "bit-timing calculation not available\n"); 97 return -EINVAL; 98} 99 100static inline void can_calc_tdco(struct net_device *dev) 101{ 102} 103#endif /* CONFIG_CAN_CALC_BITTIMING */ 104 105int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt, 106 const struct can_bittiming_const *btc, 107 const u32 *bitrate_const, 108 const unsigned int bitrate_const_cnt); 109 110/* 111 * can_bit_time() - Duration of one bit 112 * 113 * Please refer to ISO 11898-1:2015, section 11.3.1.1 "Bit time" for 114 * additional information. 115 * 116 * Return: the number of time quanta in one bit. 117 */ 118static inline unsigned int can_bit_time(const struct can_bittiming *bt) 119{ 120 return CAN_SYNC_SEG + bt->prop_seg + bt->phase_seg1 + bt->phase_seg2; 121} 122 123#endif /* !_CAN_BITTIMING_H */