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 #define CAN_CTRLMODE_TDC_MASK \ 23 (CAN_CTRLMODE_TDC_AUTO | CAN_CTRLMODE_TDC_MANUAL) 24 25 /* 26 * struct can_tdc - CAN FD Transmission Delay Compensation parameters 27 * 28 * At high bit rates, the propagation delay from the TX pin to the RX 29 * pin of the transceiver causes measurement errors: the sample point 30 * on the RX pin might occur on the previous bit. 31 * 32 * To solve this issue, ISO 11898-1 introduces in section 11.3.3 33 * "Transmitter delay compensation" a SSP (Secondary Sample Point) 34 * equal to the distance from the start of the bit time on the TX pin 35 * to the actual measurement on the RX pin. 36 * 37 * This structure contains the parameters to calculate that SSP. 38 * 39 * -+----------- one bit ----------+-- TX pin 40 * |<--- Sample Point --->| 41 * 42 * --+----------- one bit ----------+-- RX pin 43 * |<-------- TDCV -------->| 44 * |<------- TDCO ------->| 45 * |<----------- Secondary Sample Point ---------->| 46 * 47 * To increase precision, contrary to the other bittiming parameters 48 * which are measured in time quanta, the TDC parameters are measured 49 * in clock periods (also referred as "minimum time quantum" in ISO 50 * 11898-1). 51 * 52 * @tdcv: Transmitter Delay Compensation Value. The time needed for 53 * the signal to propagate, i.e. the distance, in clock periods, 54 * from the start of the bit on the TX pin to when it is received 55 * on the RX pin. @tdcv depends on the controller modes: 56 * 57 * CAN_CTRLMODE_TDC_AUTO is set: The transceiver dynamically 58 * measures @tdcv for each transmitted CAN FD frame and the 59 * value provided here should be ignored. 60 * 61 * CAN_CTRLMODE_TDC_MANUAL is set: use the fixed provided @tdcv 62 * value. 63 * 64 * N.B. CAN_CTRLMODE_TDC_AUTO and CAN_CTRLMODE_TDC_MANUAL are 65 * mutually exclusive. Only one can be set at a time. If both 66 * CAN_TDC_CTRLMODE_AUTO and CAN_TDC_CTRLMODE_MANUAL are unset, 67 * TDC is disabled and all the values of this structure should be 68 * ignored. 69 * 70 * @tdco: Transmitter Delay Compensation Offset. Offset value, in 71 * clock periods, defining the distance between the start of the 72 * bit reception on the RX pin of the transceiver and the SSP 73 * position such that SSP = @tdcv + @tdco. 74 * 75 * @tdcf: Transmitter Delay Compensation Filter window. Defines the 76 * minimum value for the SSP position in clock periods. If the 77 * SSP position is less than @tdcf, then no delay compensations 78 * occur and the normal sampling point is used instead. The 79 * feature is enabled if and only if @tdcv is set to zero 80 * (automatic mode) and @tdcf is configured to a value greater 81 * than @tdco. 82 */ 83 struct can_tdc { 84 u32 tdcv; 85 u32 tdco; 86 u32 tdcf; 87 }; 88 89 /* 90 * struct can_tdc_const - CAN hardware-dependent constant for 91 * Transmission Delay Compensation 92 * 93 * @tdcv_min: Transmitter Delay Compensation Value minimum value. If 94 * the controller does not support manual mode for tdcv 95 * (c.f. flag CAN_CTRLMODE_TDC_MANUAL) then this value is 96 * ignored. 97 * @tdcv_max: Transmitter Delay Compensation Value maximum value. If 98 * the controller does not support manual mode for tdcv 99 * (c.f. flag CAN_CTRLMODE_TDC_MANUAL) then this value is 100 * ignored. 101 * 102 * @tdco_min: Transmitter Delay Compensation Offset minimum value. 103 * @tdco_max: Transmitter Delay Compensation Offset maximum value. 104 * Should not be zero. If the controller does not support TDC, 105 * then the pointer to this structure should be NULL. 106 * 107 * @tdcf_min: Transmitter Delay Compensation Filter window minimum 108 * value. If @tdcf_max is zero, this value is ignored. 109 * @tdcf_max: Transmitter Delay Compensation Filter window maximum 110 * value. Should be set to zero if the controller does not 111 * support this feature. 112 */ 113 struct can_tdc_const { 114 u32 tdcv_min; 115 u32 tdcv_max; 116 u32 tdco_min; 117 u32 tdco_max; 118 u32 tdcf_min; 119 u32 tdcf_max; 120 }; 121 122 #ifdef CONFIG_CAN_CALC_BITTIMING 123 int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt, 124 const struct can_bittiming_const *btc); 125 126 void can_calc_tdco(struct can_tdc *tdc, const struct can_tdc_const *tdc_const, 127 const struct can_bittiming *dbt, 128 u32 *ctrlmode, u32 ctrlmode_supported); 129 #else /* !CONFIG_CAN_CALC_BITTIMING */ 130 static inline int 131 can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt, 132 const struct can_bittiming_const *btc) 133 { 134 netdev_err(dev, "bit-timing calculation not available\n"); 135 return -EINVAL; 136 } 137 138 static inline void 139 can_calc_tdco(struct can_tdc *tdc, const struct can_tdc_const *tdc_const, 140 const struct can_bittiming *dbt, 141 u32 *ctrlmode, u32 ctrlmode_supported) 142 { 143 } 144 #endif /* CONFIG_CAN_CALC_BITTIMING */ 145 146 int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt, 147 const struct can_bittiming_const *btc, 148 const u32 *bitrate_const, 149 const unsigned int bitrate_const_cnt); 150 151 /* 152 * can_bit_time() - Duration of one bit 153 * 154 * Please refer to ISO 11898-1:2015, section 11.3.1.1 "Bit time" for 155 * additional information. 156 * 157 * Return: the number of time quanta in one bit. 158 */ 159 static inline unsigned int can_bit_time(const struct can_bittiming *bt) 160 { 161 return CAN_SYNC_SEG + bt->prop_seg + bt->phase_seg1 + bt->phase_seg2; 162 } 163 164 #endif /* !_CAN_BITTIMING_H */ 165