Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /* Copyright (c) 2020 Pengutronix, Marc Kleine-Budde <kernel@pengutronix.de>
0003  * Copyright (c) 2021 Vincent Mailhol <mailhol.vincent@wanadoo.fr>
0004  */
0005 
0006 #ifndef _CAN_BITTIMING_H
0007 #define _CAN_BITTIMING_H
0008 
0009 #include <linux/netdevice.h>
0010 #include <linux/can/netlink.h>
0011 
0012 #define CAN_SYNC_SEG 1
0013 
0014 #define CAN_BITRATE_UNSET 0
0015 #define CAN_BITRATE_UNKNOWN (-1U)
0016 
0017 #define CAN_CTRLMODE_TDC_MASK                   \
0018     (CAN_CTRLMODE_TDC_AUTO | CAN_CTRLMODE_TDC_MANUAL)
0019 
0020 /*
0021  * struct can_tdc - CAN FD Transmission Delay Compensation parameters
0022  *
0023  * At high bit rates, the propagation delay from the TX pin to the RX
0024  * pin of the transceiver causes measurement errors: the sample point
0025  * on the RX pin might occur on the previous bit.
0026  *
0027  * To solve this issue, ISO 11898-1 introduces in section 11.3.3
0028  * "Transmitter delay compensation" a SSP (Secondary Sample Point)
0029  * equal to the distance from the start of the bit time on the TX pin
0030  * to the actual measurement on the RX pin.
0031  *
0032  * This structure contains the parameters to calculate that SSP.
0033  *
0034  * -+----------- one bit ----------+-- TX pin
0035  *  |<--- Sample Point --->|
0036  *
0037  *                         --+----------- one bit ----------+-- RX pin
0038  *  |<-------- TDCV -------->|
0039  *                           |<------- TDCO ------->|
0040  *  |<----------- Secondary Sample Point ---------->|
0041  *
0042  * To increase precision, contrary to the other bittiming parameters
0043  * which are measured in time quanta, the TDC parameters are measured
0044  * in clock periods (also referred as "minimum time quantum" in ISO
0045  * 11898-1).
0046  *
0047  * @tdcv: Transmitter Delay Compensation Value. The time needed for
0048  *  the signal to propagate, i.e. the distance, in clock periods,
0049  *  from the start of the bit on the TX pin to when it is received
0050  *  on the RX pin. @tdcv depends on the controller modes:
0051  *
0052  *    CAN_CTRLMODE_TDC_AUTO is set: The transceiver dynamically
0053  *    measures @tdcv for each transmitted CAN FD frame and the
0054  *    value provided here should be ignored.
0055  *
0056  *    CAN_CTRLMODE_TDC_MANUAL is set: use the fixed provided @tdcv
0057  *    value.
0058  *
0059  *  N.B. CAN_CTRLMODE_TDC_AUTO and CAN_CTRLMODE_TDC_MANUAL are
0060  *  mutually exclusive. Only one can be set at a time. If both
0061  *  CAN_TDC_CTRLMODE_AUTO and CAN_TDC_CTRLMODE_MANUAL are unset,
0062  *  TDC is disabled and all the values of this structure should be
0063  *  ignored.
0064  *
0065  * @tdco: Transmitter Delay Compensation Offset. Offset value, in
0066  *  clock periods, defining the distance between the start of the
0067  *  bit reception on the RX pin of the transceiver and the SSP
0068  *  position such that SSP = @tdcv + @tdco.
0069  *
0070  * @tdcf: Transmitter Delay Compensation Filter window. Defines the
0071  *  minimum value for the SSP position in clock periods. If the
0072  *  SSP position is less than @tdcf, then no delay compensations
0073  *  occur and the normal sampling point is used instead. The
0074  *  feature is enabled if and only if @tdcv is set to zero
0075  *  (automatic mode) and @tdcf is configured to a value greater
0076  *  than @tdco.
0077  */
0078 struct can_tdc {
0079     u32 tdcv;
0080     u32 tdco;
0081     u32 tdcf;
0082 };
0083 
0084 /*
0085  * struct can_tdc_const - CAN hardware-dependent constant for
0086  *  Transmission Delay Compensation
0087  *
0088  * @tdcv_min: Transmitter Delay Compensation Value minimum value. If
0089  *  the controller does not support manual mode for tdcv
0090  *  (c.f. flag CAN_CTRLMODE_TDC_MANUAL) then this value is
0091  *  ignored.
0092  * @tdcv_max: Transmitter Delay Compensation Value maximum value. If
0093  *  the controller does not support manual mode for tdcv
0094  *  (c.f. flag CAN_CTRLMODE_TDC_MANUAL) then this value is
0095  *  ignored.
0096  *
0097  * @tdco_min: Transmitter Delay Compensation Offset minimum value.
0098  * @tdco_max: Transmitter Delay Compensation Offset maximum value.
0099  *  Should not be zero. If the controller does not support TDC,
0100  *  then the pointer to this structure should be NULL.
0101  *
0102  * @tdcf_min: Transmitter Delay Compensation Filter window minimum
0103  *  value. If @tdcf_max is zero, this value is ignored.
0104  * @tdcf_max: Transmitter Delay Compensation Filter window maximum
0105  *  value. Should be set to zero if the controller does not
0106  *  support this feature.
0107  */
0108 struct can_tdc_const {
0109     u32 tdcv_min;
0110     u32 tdcv_max;
0111     u32 tdco_min;
0112     u32 tdco_max;
0113     u32 tdcf_min;
0114     u32 tdcf_max;
0115 };
0116 
0117 #ifdef CONFIG_CAN_CALC_BITTIMING
0118 int can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt,
0119                const struct can_bittiming_const *btc);
0120 
0121 void can_calc_tdco(struct can_tdc *tdc, const struct can_tdc_const *tdc_const,
0122            const struct can_bittiming *dbt,
0123            u32 *ctrlmode, u32 ctrlmode_supported);
0124 #else /* !CONFIG_CAN_CALC_BITTIMING */
0125 static inline int
0126 can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt,
0127            const struct can_bittiming_const *btc)
0128 {
0129     netdev_err(dev, "bit-timing calculation not available\n");
0130     return -EINVAL;
0131 }
0132 
0133 static inline void
0134 can_calc_tdco(struct can_tdc *tdc, const struct can_tdc_const *tdc_const,
0135           const struct can_bittiming *dbt,
0136           u32 *ctrlmode, u32 ctrlmode_supported)
0137 {
0138 }
0139 #endif /* CONFIG_CAN_CALC_BITTIMING */
0140 
0141 int can_get_bittiming(const struct net_device *dev, struct can_bittiming *bt,
0142               const struct can_bittiming_const *btc,
0143               const u32 *bitrate_const,
0144               const unsigned int bitrate_const_cnt);
0145 
0146 /*
0147  * can_bit_time() - Duration of one bit
0148  *
0149  * Please refer to ISO 11898-1:2015, section 11.3.1.1 "Bit time" for
0150  * additional information.
0151  *
0152  * Return: the number of time quanta in one bit.
0153  */
0154 static inline unsigned int can_bit_time(const struct can_bittiming *bt)
0155 {
0156     return CAN_SYNC_SEG + bt->prop_seg + bt->phase_seg1 + bt->phase_seg2;
0157 }
0158 
0159 #endif /* !_CAN_BITTIMING_H */