1 /* SPDX-License-Identifier: GPL-2.0-only */ 1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* Copyright (c) 2020 Pengutronix, Marc Kleine 2 /* Copyright (c) 2020 Pengutronix, Marc Kleine-Budde <kernel@pengutronix.de> 3 * Copyright (c) 2021 Vincent Mailhol <mailhol 3 * Copyright (c) 2021 Vincent Mailhol <mailhol.vincent@wanadoo.fr> 4 */ 4 */ 5 5 6 #ifndef _CAN_BITTIMING_H 6 #ifndef _CAN_BITTIMING_H 7 #define _CAN_BITTIMING_H 7 #define _CAN_BITTIMING_H 8 8 9 #include <linux/netdevice.h> 9 #include <linux/netdevice.h> 10 #include <linux/can/netlink.h> 10 #include <linux/can/netlink.h> 11 11 12 #define CAN_SYNC_SEG 1 12 #define CAN_SYNC_SEG 1 13 13 14 #define CAN_BITRATE_UNSET 0 !! 14 15 #define CAN_BITRATE_UNKNOWN (-1U) !! 15 /* Kilobits and Megabits per second */ >> 16 #define CAN_KBPS 1000UL >> 17 #define CAN_MBPS 1000000UL >> 18 >> 19 /* Megahertz */ >> 20 #define CAN_MHZ 1000000UL 16 21 17 #define CAN_CTRLMODE_TDC_MASK 22 #define CAN_CTRLMODE_TDC_MASK \ 18 (CAN_CTRLMODE_TDC_AUTO | CAN_CTRLMODE_ 23 (CAN_CTRLMODE_TDC_AUTO | CAN_CTRLMODE_TDC_MANUAL) 19 24 20 /* 25 /* 21 * struct can_tdc - CAN FD Transmission Delay 26 * struct can_tdc - CAN FD Transmission Delay Compensation parameters 22 * 27 * 23 * At high bit rates, the propagation delay fr 28 * At high bit rates, the propagation delay from the TX pin to the RX 24 * pin of the transceiver causes measurement e 29 * pin of the transceiver causes measurement errors: the sample point 25 * on the RX pin might occur on the previous b 30 * on the RX pin might occur on the previous bit. 26 * 31 * 27 * To solve this issue, ISO 11898-1 introduces 32 * To solve this issue, ISO 11898-1 introduces in section 11.3.3 28 * "Transmitter delay compensation" a SSP (Sec 33 * "Transmitter delay compensation" a SSP (Secondary Sample Point) 29 * equal to the distance from the start of the 34 * equal to the distance from the start of the bit time on the TX pin 30 * to the actual measurement on the RX pin. 35 * to the actual measurement on the RX pin. 31 * 36 * 32 * This structure contains the parameters to c 37 * This structure contains the parameters to calculate that SSP. 33 * 38 * 34 * -+----------- one bit ----------+-- TX pin 39 * -+----------- one bit ----------+-- TX pin 35 * |<--- Sample Point --->| 40 * |<--- Sample Point --->| 36 * 41 * 37 * --+----------- one 42 * --+----------- one bit ----------+-- RX pin 38 * |<-------- TDCV -------->| 43 * |<-------- TDCV -------->| 39 * |<------- TDCO -- 44 * |<------- TDCO ------->| 40 * |<----------- Secondary Sample Point ----- 45 * |<----------- Secondary Sample Point ---------->| 41 * 46 * 42 * To increase precision, contrary to the othe 47 * To increase precision, contrary to the other bittiming parameters 43 * which are measured in time quanta, the TDC 48 * which are measured in time quanta, the TDC parameters are measured 44 * in clock periods (also referred as "minimum 49 * in clock periods (also referred as "minimum time quantum" in ISO 45 * 11898-1). 50 * 11898-1). 46 * 51 * 47 * @tdcv: Transmitter Delay Compensation Value 52 * @tdcv: Transmitter Delay Compensation Value. The time needed for 48 * the signal to propagate, i.e. the dist 53 * the signal to propagate, i.e. the distance, in clock periods, 49 * from the start of the bit on the TX pi 54 * from the start of the bit on the TX pin to when it is received 50 * on the RX pin. @tdcv depends on the co 55 * on the RX pin. @tdcv depends on the controller modes: 51 * 56 * 52 * CAN_CTRLMODE_TDC_AUTO is set: The tr 57 * CAN_CTRLMODE_TDC_AUTO is set: The transceiver dynamically 53 * measures @tdcv for each transmitted 58 * measures @tdcv for each transmitted CAN FD frame and the 54 * value provided here should be ignore 59 * value provided here should be ignored. 55 * 60 * 56 * CAN_CTRLMODE_TDC_MANUAL is set: use 61 * CAN_CTRLMODE_TDC_MANUAL is set: use the fixed provided @tdcv 57 * value. 62 * value. 58 * 63 * 59 * N.B. CAN_CTRLMODE_TDC_AUTO and CAN_CTR 64 * N.B. CAN_CTRLMODE_TDC_AUTO and CAN_CTRLMODE_TDC_MANUAL are 60 * mutually exclusive. Only one can be se 65 * mutually exclusive. Only one can be set at a time. If both 61 * CAN_TDC_CTRLMODE_AUTO and CAN_TDC_CTRL 66 * CAN_TDC_CTRLMODE_AUTO and CAN_TDC_CTRLMODE_MANUAL are unset, 62 * TDC is disabled and all the values of 67 * TDC is disabled and all the values of this structure should be 63 * ignored. 68 * ignored. 64 * 69 * 65 * @tdco: Transmitter Delay Compensation Offse 70 * @tdco: Transmitter Delay Compensation Offset. Offset value, in 66 * clock periods, defining the distance b 71 * clock periods, defining the distance between the start of the 67 * bit reception on the RX pin of the tra 72 * bit reception on the RX pin of the transceiver and the SSP 68 * position such that SSP = @tdcv + @tdco 73 * position such that SSP = @tdcv + @tdco. 69 * 74 * 70 * @tdcf: Transmitter Delay Compensation Filte 75 * @tdcf: Transmitter Delay Compensation Filter window. Defines the 71 * minimum value for the SSP position in 76 * minimum value for the SSP position in clock periods. If the 72 * SSP position is less than @tdcf, then 77 * SSP position is less than @tdcf, then no delay compensations 73 * occur and the normal sampling point is 78 * occur and the normal sampling point is used instead. The 74 * feature is enabled if and only if @tdc 79 * feature is enabled if and only if @tdcv is set to zero 75 * (automatic mode) and @tdcf is configur 80 * (automatic mode) and @tdcf is configured to a value greater 76 * than @tdco. 81 * than @tdco. 77 */ 82 */ 78 struct can_tdc { 83 struct can_tdc { 79 u32 tdcv; 84 u32 tdcv; 80 u32 tdco; 85 u32 tdco; 81 u32 tdcf; 86 u32 tdcf; 82 }; 87 }; 83 88 84 /* 89 /* 85 * struct can_tdc_const - CAN hardware-depende 90 * struct can_tdc_const - CAN hardware-dependent constant for 86 * Transmission Delay Compensation 91 * Transmission Delay Compensation 87 * 92 * 88 * @tdcv_min: Transmitter Delay Compensation V 93 * @tdcv_min: Transmitter Delay Compensation Value minimum value. If 89 * the controller does not support manual 94 * the controller does not support manual mode for tdcv 90 * (c.f. flag CAN_CTRLMODE_TDC_MANUAL) th 95 * (c.f. flag CAN_CTRLMODE_TDC_MANUAL) then this value is 91 * ignored. 96 * ignored. 92 * @tdcv_max: Transmitter Delay Compensation V 97 * @tdcv_max: Transmitter Delay Compensation Value maximum value. If 93 * the controller does not support manual 98 * the controller does not support manual mode for tdcv 94 * (c.f. flag CAN_CTRLMODE_TDC_MANUAL) th 99 * (c.f. flag CAN_CTRLMODE_TDC_MANUAL) then this value is 95 * ignored. 100 * ignored. 96 * 101 * 97 * @tdco_min: Transmitter Delay Compensation O 102 * @tdco_min: Transmitter Delay Compensation Offset minimum value. 98 * @tdco_max: Transmitter Delay Compensation O 103 * @tdco_max: Transmitter Delay Compensation Offset maximum value. 99 * Should not be zero. If the controller 104 * Should not be zero. If the controller does not support TDC, 100 * then the pointer to this structure sho 105 * then the pointer to this structure should be NULL. 101 * 106 * 102 * @tdcf_min: Transmitter Delay Compensation F 107 * @tdcf_min: Transmitter Delay Compensation Filter window minimum 103 * value. If @tdcf_max is zero, this valu 108 * value. If @tdcf_max is zero, this value is ignored. 104 * @tdcf_max: Transmitter Delay Compensation F 109 * @tdcf_max: Transmitter Delay Compensation Filter window maximum 105 * value. Should be set to zero if the co 110 * value. Should be set to zero if the controller does not 106 * support this feature. 111 * support this feature. 107 */ 112 */ 108 struct can_tdc_const { 113 struct can_tdc_const { 109 u32 tdcv_min; 114 u32 tdcv_min; 110 u32 tdcv_max; 115 u32 tdcv_max; 111 u32 tdco_min; 116 u32 tdco_min; 112 u32 tdco_max; 117 u32 tdco_max; 113 u32 tdcf_min; 118 u32 tdcf_min; 114 u32 tdcf_max; 119 u32 tdcf_max; 115 }; 120 }; 116 121 117 #ifdef CONFIG_CAN_CALC_BITTIMING 122 #ifdef CONFIG_CAN_CALC_BITTIMING 118 int can_calc_bittiming(const struct net_device !! 123 int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt, 119 const struct can_bittim !! 124 const struct can_bittiming_const *btc); 120 125 121 void can_calc_tdco(struct can_tdc *tdc, const 126 void can_calc_tdco(struct can_tdc *tdc, const struct can_tdc_const *tdc_const, 122 const struct can_bittiming 127 const struct can_bittiming *dbt, 123 u32 *ctrlmode, u32 ctrlmode 128 u32 *ctrlmode, u32 ctrlmode_supported); 124 #else /* !CONFIG_CAN_CALC_BITTIMING */ 129 #else /* !CONFIG_CAN_CALC_BITTIMING */ 125 static inline int 130 static inline int 126 can_calc_bittiming(const struct net_device *de !! 131 can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt, 127 const struct can_bittiming_ !! 132 const struct can_bittiming_const *btc) 128 { 133 { 129 netdev_err(dev, "bit-timing calculatio 134 netdev_err(dev, "bit-timing calculation not available\n"); 130 return -EINVAL; 135 return -EINVAL; 131 } 136 } 132 137 133 static inline void 138 static inline void 134 can_calc_tdco(struct can_tdc *tdc, const struc 139 can_calc_tdco(struct can_tdc *tdc, const struct can_tdc_const *tdc_const, 135 const struct can_bittiming *dbt, 140 const struct can_bittiming *dbt, 136 u32 *ctrlmode, u32 ctrlmode_supp 141 u32 *ctrlmode, u32 ctrlmode_supported) 137 { 142 { 138 } 143 } 139 #endif /* CONFIG_CAN_CALC_BITTIMING */ 144 #endif /* CONFIG_CAN_CALC_BITTIMING */ 140 145 141 void can_sjw_set_default(struct can_bittiming !! 146 int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt, 142 << 143 int can_sjw_check(const struct net_device *dev << 144 const struct can_bittiming_c << 145 << 146 int can_get_bittiming(const struct net_device << 147 const struct can_bittimi 147 const struct can_bittiming_const *btc, 148 const u32 *bitrate_const 148 const u32 *bitrate_const, 149 const unsigned int bitra !! 149 const unsigned int bitrate_const_cnt); 150 struct netlink_ext_ack * << 151 150 152 /* 151 /* 153 * can_bit_time() - Duration of one bit 152 * can_bit_time() - Duration of one bit 154 * 153 * 155 * Please refer to ISO 11898-1:2015, section 1 154 * Please refer to ISO 11898-1:2015, section 11.3.1.1 "Bit time" for 156 * additional information. 155 * additional information. 157 * 156 * 158 * Return: the number of time quanta in one bi 157 * Return: the number of time quanta in one bit. 159 */ 158 */ 160 static inline unsigned int can_bit_time(const 159 static inline unsigned int can_bit_time(const struct can_bittiming *bt) 161 { 160 { 162 return CAN_SYNC_SEG + bt->prop_seg + b 161 return CAN_SYNC_SEG + bt->prop_seg + bt->phase_seg1 + bt->phase_seg2; 163 } 162 } 164 163 165 #endif /* !_CAN_BITTIMING_H */ 164 #endif /* !_CAN_BITTIMING_H */ 166 165
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.