can: bittiming: add calculation for CAN FD 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
82 void can_calc_tdco(struct net_device *dev);
83 #else /* !CONFIG_CAN_CALC_BITTIMING */
84 static inline int
85 can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
86                    const struct can_bittiming_const *btc)
87 {
88         netdev_err(dev, "bit-timing calculation not available\n");
89         return -EINVAL;
90 }
91
92 static inline void can_calc_tdco(struct net_device *dev)
93 {
94 }
95 #endif /* CONFIG_CAN_CALC_BITTIMING */
96
97 int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt,
98                       const struct can_bittiming_const *btc,
99                       const u32 *bitrate_const,
100                       const unsigned int bitrate_const_cnt);
101
102 /*
103  * can_bit_time() - Duration of one bit
104  *
105  * Please refer to ISO 11898-1:2015, section 11.3.1.1 "Bit time" for
106  * additional information.
107  *
108  * Return: the number of time quanta in one bit.
109  */
110 static inline unsigned int can_bit_time(const struct can_bittiming *bt)
111 {
112         return CAN_SYNC_SEG + bt->prop_seg + bt->phase_seg1 + bt->phase_seg2;
113 }
114
115 #endif /* !_CAN_BITTIMING_H */