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