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>
6 #ifndef _CAN_BITTIMING_H
7 #define _CAN_BITTIMING_H
9 #include <linux/netdevice.h>
10 #include <linux/can/netlink.h>
12 #define CAN_SYNC_SEG 1
14 #define CAN_BITRATE_UNSET 0
15 #define CAN_BITRATE_UNKNOWN (-1U)
17 #define CAN_CTRLMODE_TDC_MASK \
18 (CAN_CTRLMODE_TDC_AUTO | CAN_CTRLMODE_TDC_MANUAL)
21 * struct can_tdc - CAN FD Transmission Delay Compensation parameters
23 * At high bit rates, the propagation delay from the TX pin to the RX
24 * pin of the transceiver causes measurement errors: the sample point
25 * on the RX pin might occur on the previous bit.
27 * To solve this issue, ISO 11898-1 introduces in section 11.3.3
28 * "Transmitter delay compensation" a SSP (Secondary Sample Point)
29 * equal to the distance from the start of the bit time on the TX pin
30 * to the actual measurement on the RX pin.
32 * This structure contains the parameters to calculate that SSP.
34 * -+----------- one bit ----------+-- TX pin
35 * |<--- Sample Point --->|
37 * --+----------- one bit ----------+-- RX pin
38 * |<-------- TDCV -------->|
39 * |<------- TDCO ------->|
40 * |<----------- Secondary Sample Point ---------->|
42 * To increase precision, contrary to the other bittiming parameters
43 * which are measured in time quanta, the TDC parameters are measured
44 * in clock periods (also referred as "minimum time quantum" in ISO
47 * @tdcv: Transmitter Delay Compensation Value. The time needed for
48 * the signal to propagate, i.e. the distance, in clock periods,
49 * from the start of the bit on the TX pin to when it is received
50 * on the RX pin. @tdcv depends on the controller modes:
52 * CAN_CTRLMODE_TDC_AUTO is set: The transceiver dynamically
53 * measures @tdcv for each transmitted CAN FD frame and the
54 * value provided here should be ignored.
56 * CAN_CTRLMODE_TDC_MANUAL is set: use the fixed provided @tdcv
59 * N.B. CAN_CTRLMODE_TDC_AUTO and CAN_CTRLMODE_TDC_MANUAL are
60 * mutually exclusive. Only one can be set at a time. If both
61 * CAN_TDC_CTRLMODE_AUTO and CAN_TDC_CTRLMODE_MANUAL are unset,
62 * TDC is disabled and all the values of this structure should be
65 * @tdco: Transmitter Delay Compensation Offset. Offset value, in
66 * clock periods, defining the distance between the start of the
67 * bit reception on the RX pin of the transceiver and the SSP
68 * position such that SSP = @tdcv + @tdco.
70 * @tdcf: Transmitter Delay Compensation Filter window. Defines the
71 * minimum value for the SSP position in clock periods. If the
72 * SSP position is less than @tdcf, then no delay compensations
73 * occur and the normal sampling point is used instead. The
74 * feature is enabled if and only if @tdcv is set to zero
75 * (automatic mode) and @tdcf is configured to a value greater
85 * struct can_tdc_const - CAN hardware-dependent constant for
86 * Transmission Delay Compensation
88 * @tdcv_min: Transmitter Delay Compensation Value minimum value. If
89 * the controller does not support manual mode for tdcv
90 * (c.f. flag CAN_CTRLMODE_TDC_MANUAL) then this value is
92 * @tdcv_max: Transmitter Delay Compensation Value maximum value. If
93 * the controller does not support manual mode for tdcv
94 * (c.f. flag CAN_CTRLMODE_TDC_MANUAL) then this value is
97 * @tdco_min: Transmitter Delay Compensation Offset minimum value.
98 * @tdco_max: Transmitter Delay Compensation Offset maximum value.
99 * Should not be zero. If the controller does not support TDC,
100 * then the pointer to this structure should be NULL.
102 * @tdcf_min: Transmitter Delay Compensation Filter window minimum
103 * value. If @tdcf_max is zero, this value is ignored.
104 * @tdcf_max: Transmitter Delay Compensation Filter window maximum
105 * value. Should be set to zero if the controller does not
106 * support this feature.
108 struct can_tdc_const
{
117 #ifdef CONFIG_CAN_CALC_BITTIMING
118 int can_calc_bittiming(const struct net_device
*dev
, struct can_bittiming
*bt
,
119 const struct can_bittiming_const
*btc
, struct netlink_ext_ack
*extack
);
121 void can_calc_tdco(struct can_tdc
*tdc
, const struct can_tdc_const
*tdc_const
,
122 const struct can_bittiming
*dbt
,
123 u32
*ctrlmode
, u32 ctrlmode_supported
);
124 #else /* !CONFIG_CAN_CALC_BITTIMING */
126 can_calc_bittiming(const struct net_device
*dev
, struct can_bittiming
*bt
,
127 const struct can_bittiming_const
*btc
, struct netlink_ext_ack
*extack
)
129 netdev_err(dev
, "bit-timing calculation not available\n");
134 can_calc_tdco(struct can_tdc
*tdc
, const struct can_tdc_const
*tdc_const
,
135 const struct can_bittiming
*dbt
,
136 u32
*ctrlmode
, u32 ctrlmode_supported
)
139 #endif /* CONFIG_CAN_CALC_BITTIMING */
141 void can_sjw_set_default(struct can_bittiming
*bt
);
143 int can_sjw_check(const struct net_device
*dev
, const struct can_bittiming
*bt
,
144 const struct can_bittiming_const
*btc
, struct netlink_ext_ack
*extack
);
146 int can_get_bittiming(const 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 struct netlink_ext_ack
*extack
);
153 * can_bit_time() - Duration of one bit
155 * Please refer to ISO 11898-1:2015, section 11.3.1.1 "Bit time" for
156 * additional information.
158 * Return: the number of time quanta in one bit.
160 static inline unsigned int can_bit_time(const struct can_bittiming
*bt
)
162 return CAN_SYNC_SEG
+ bt
->prop_seg
+ bt
->phase_seg1
+ bt
->phase_seg2
;
165 #endif /* !_CAN_BITTIMING_H */