can: add new CAN FD bittiming parameters: Transmitter Delay Compensation (TDC)
[linux-2.6-microblaze.git] / include / linux / can / bittiming.h
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  * struct can_tdc - CAN FD Transmission Delay Compensation parameters
16  *
17  * At high bit rates, the propagation delay from the TX pin to the RX
18  * pin of the transceiver causes measurement errors: the sample point
19  * on the RX pin might occur on the previous bit.
20  *
21  * To solve this issue, ISO 11898-1 introduces in section 11.3.3
22  * "Transmitter delay compensation" a SSP (Secondary Sample Point)
23  * equal to the distance, in time quanta, from the start of the bit
24  * time on the TX pin to the actual measurement on the RX pin.
25  *
26  * This structure contains the parameters to calculate that SSP.
27  *
28  * @tdcv: Transmitter Delay Compensation Value. Distance, in time
29  *      quanta, from when the bit is sent on the TX pin to when it is
30  *      received on the RX pin of the transmitter. Possible options:
31  *
32  *        O: automatic mode. The controller dynamically measure @tdcv
33  *        for each transmitted CAN FD frame.
34  *
35  *        Other values: manual mode. Use the fixed provided value.
36  *
37  * @tdco: Transmitter Delay Compensation Offset. Offset value, in time
38  *      quanta, defining the distance between the start of the bit
39  *      reception on the RX pin of the transceiver and the SSP
40  *      position such as SSP = @tdcv + @tdco.
41  *
42  *      If @tdco is zero, then TDC is disabled and both @tdcv and
43  *      @tdcf should be ignored.
44  *
45  * @tdcf: Transmitter Delay Compensation Filter window. Defines the
46  *      minimum value for the SSP position in time quanta. If SSP is
47  *      less than @tdcf, then no delay compensations occur and the
48  *      normal sampling point is used instead. The feature is enabled
49  *      if and only if @tdcv is set to zero (automatic mode) and @tdcf
50  *      is configured to a value greater than @tdco.
51  */
52 struct can_tdc {
53         u32 tdcv;
54         u32 tdco;
55         u32 tdcf;
56 };
57
58 /*
59  * struct can_tdc_const - CAN hardware-dependent constant for
60  *      Transmission Delay Compensation
61  *
62  * @tdcv_max: Transmitter Delay Compensation Value maximum value.
63  *      Should be set to zero if the controller does not support
64  *      manual mode for tdcv.
65  * @tdco_max: Transmitter Delay Compensation Offset maximum value.
66  *      Should not be zero. If the controller does not support TDC,
67  *      then the pointer to this structure should be NULL.
68  * @tdcf_max: Transmitter Delay Compensation Filter window maximum
69  *      value. Should be set to zero if the controller does not
70  *      support this feature.
71  */
72 struct can_tdc_const {
73         u32 tdcv_max;
74         u32 tdco_max;
75         u32 tdcf_max;
76 };
77
78 #ifdef CONFIG_CAN_CALC_BITTIMING
79 int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
80                        const struct can_bittiming_const *btc);
81 #else /* !CONFIG_CAN_CALC_BITTIMING */
82 static inline int
83 can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
84                    const struct can_bittiming_const *btc)
85 {
86         netdev_err(dev, "bit-timing calculation not available\n");
87         return -EINVAL;
88 }
89 #endif /* CONFIG_CAN_CALC_BITTIMING */
90
91 int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt,
92                       const struct can_bittiming_const *btc,
93                       const u32 *bitrate_const,
94                       const unsigned int bitrate_const_cnt);
95
96 /*
97  * can_bit_time() - Duration of one bit
98  *
99  * Please refer to ISO 11898-1:2015, section 11.3.1.1 "Bit time" for
100  * additional information.
101  *
102  * Return: the number of time quanta in one bit.
103  */
104 static inline unsigned int can_bit_time(const struct can_bittiming *bt)
105 {
106         return CAN_SYNC_SEG + bt->prop_seg + bt->phase_seg1 + bt->phase_seg2;
107 }
108
109 #endif /* !_CAN_BITTIMING_H */