1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
3 * Copyright (C) 2006 Andrey Volkov, Varma Electronics
4 * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>
5 * Copyright (C) 2021-2025 Vincent Mailhol <mailhol@kernel.org>
6 */
7
8 #include <linux/units.h>
9 #include <linux/can/dev.h>
10
11 #define CAN_CALC_MAX_ERROR 500 /* max error 5% */
12
13 /* CiA recommended sample points for Non Return to Zero encoding. */
can_calc_sample_point_nrz(const struct can_bittiming * bt)14 static int can_calc_sample_point_nrz(const struct can_bittiming *bt)
15 {
16 if (bt->bitrate > 800 * KILO /* BPS */)
17 return 750;
18
19 if (bt->bitrate > 500 * KILO /* BPS */)
20 return 800;
21
22 return 875;
23 }
24
25 /* Sample points for Pulse-Width Modulation encoding. */
can_calc_sample_point_pwm(const struct can_bittiming * bt)26 static int can_calc_sample_point_pwm(const struct can_bittiming *bt)
27 {
28 if (bt->bitrate > 15 * MEGA /* BPS */)
29 return 625;
30
31 if (bt->bitrate > 9 * MEGA /* BPS */)
32 return 600;
33
34 if (bt->bitrate > 4 * MEGA /* BPS */)
35 return 560;
36
37 return 520;
38 }
39
40 /* Bit-timing calculation derived from:
41 *
42 * Code based on LinCAN sources and H8S2638 project
43 * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
44 * Copyright 2005 Stanislav Marek
45 * email: pisa@cmp.felk.cvut.cz
46 *
47 * Calculates proper bit-timing parameters for a specified bit-rate
48 * and sample-point, which can then be used to set the bit-timing
49 * registers of the CAN controller. You can find more information
50 * in the header file linux/can/netlink.h.
51 */
52 static int
can_update_sample_point(const struct can_bittiming_const * btc,const unsigned int sample_point_reference,const unsigned int tseg,unsigned int * tseg1_ptr,unsigned int * tseg2_ptr,unsigned int * sample_point_error_ptr)53 can_update_sample_point(const struct can_bittiming_const *btc,
54 const unsigned int sample_point_reference, const unsigned int tseg,
55 unsigned int *tseg1_ptr, unsigned int *tseg2_ptr,
56 unsigned int *sample_point_error_ptr)
57 {
58 unsigned int sample_point_error, best_sample_point_error = UINT_MAX;
59 unsigned int sample_point, best_sample_point = 0;
60 unsigned int tseg1, tseg2;
61 int i;
62
63 for (i = 0; i <= 1; i++) {
64 tseg2 = tseg + CAN_SYNC_SEG -
65 (sample_point_reference * (tseg + CAN_SYNC_SEG)) /
66 1000 - i;
67 tseg2 = clamp(tseg2, btc->tseg2_min, btc->tseg2_max);
68 tseg1 = tseg - tseg2;
69 if (tseg1 > btc->tseg1_max) {
70 tseg1 = btc->tseg1_max;
71 tseg2 = tseg - tseg1;
72 }
73
74 sample_point = 1000 * (tseg + CAN_SYNC_SEG - tseg2) /
75 (tseg + CAN_SYNC_SEG);
76 sample_point_error = abs(sample_point_reference - sample_point);
77
78 if (sample_point <= sample_point_reference &&
79 sample_point_error < best_sample_point_error) {
80 best_sample_point = sample_point;
81 best_sample_point_error = sample_point_error;
82 *tseg1_ptr = tseg1;
83 *tseg2_ptr = tseg2;
84 }
85 }
86
87 if (sample_point_error_ptr)
88 *sample_point_error_ptr = best_sample_point_error;
89
90 return best_sample_point;
91 }
92
can_calc_bittiming(const struct net_device * dev,struct can_bittiming * bt,const struct can_bittiming_const * btc,struct netlink_ext_ack * extack)93 int can_calc_bittiming(const struct net_device *dev, struct can_bittiming *bt,
94 const struct can_bittiming_const *btc, struct netlink_ext_ack *extack)
95 {
96 struct can_priv *priv = netdev_priv(dev);
97 unsigned int bitrate; /* current bitrate */
98 unsigned int bitrate_error; /* diff between calculated and reference value */
99 unsigned int best_bitrate_error = UINT_MAX;
100 unsigned int sample_point_error; /* diff between calculated and reference value */
101 unsigned int best_sample_point_error = UINT_MAX;
102 unsigned int sample_point_reference; /* reference sample point */
103 unsigned int best_tseg = 0; /* current best value for tseg */
104 unsigned int best_brp = 0; /* current best value for brp */
105 unsigned int brp, tsegall, tseg, tseg1 = 0, tseg2 = 0;
106 u64 v64;
107 int err;
108
109 if (bt->sample_point)
110 sample_point_reference = bt->sample_point;
111 else if (btc == priv->xl.data_bittiming_const &&
112 (priv->ctrlmode & CAN_CTRLMODE_XL_TMS))
113 sample_point_reference = can_calc_sample_point_pwm(bt);
114 else
115 sample_point_reference = can_calc_sample_point_nrz(bt);
116
117 /* tseg even = round down, odd = round up */
118 for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
119 tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
120 tsegall = CAN_SYNC_SEG + tseg / 2;
121
122 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
123 brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
124
125 /* choose brp step which is possible in system */
126 brp = (brp / btc->brp_inc) * btc->brp_inc;
127 if (brp < btc->brp_min || brp > btc->brp_max)
128 continue;
129
130 bitrate = priv->clock.freq / (brp * tsegall);
131 bitrate_error = abs(bt->bitrate - bitrate);
132
133 /* tseg brp biterror */
134 if (bitrate_error > best_bitrate_error)
135 continue;
136
137 /* reset sample point error if we have a better bitrate */
138 if (bitrate_error < best_bitrate_error)
139 best_sample_point_error = UINT_MAX;
140
141 can_update_sample_point(btc, sample_point_reference, tseg / 2,
142 &tseg1, &tseg2, &sample_point_error);
143 if (sample_point_error >= best_sample_point_error)
144 continue;
145
146 best_sample_point_error = sample_point_error;
147 best_bitrate_error = bitrate_error;
148 best_tseg = tseg / 2;
149 best_brp = brp;
150
151 if (bitrate_error == 0 && sample_point_error == 0)
152 break;
153 }
154
155 if (best_bitrate_error) {
156 /* Error in one-hundredth of a percent */
157 v64 = (u64)best_bitrate_error * 10000;
158 do_div(v64, bt->bitrate);
159 bitrate_error = (u32)v64;
160 /* print at least 0.01% if the error is smaller */
161 bitrate_error = max(bitrate_error, 1U);
162 if (bitrate_error > CAN_CALC_MAX_ERROR) {
163 NL_SET_ERR_MSG_FMT(extack,
164 "bitrate error: %u.%02u%% too high",
165 bitrate_error / 100,
166 bitrate_error % 100);
167 return -EINVAL;
168 }
169 NL_SET_ERR_MSG_FMT(extack,
170 "bitrate error: %u.%02u%%",
171 bitrate_error / 100, bitrate_error % 100);
172 }
173
174 /* real sample point */
175 bt->sample_point = can_update_sample_point(btc, sample_point_reference,
176 best_tseg, &tseg1, &tseg2,
177 NULL);
178
179 v64 = (u64)best_brp * 1000 * 1000 * 1000;
180 do_div(v64, priv->clock.freq);
181 bt->tq = (u32)v64;
182 bt->prop_seg = tseg1 / 2;
183 bt->phase_seg1 = tseg1 - bt->prop_seg;
184 bt->phase_seg2 = tseg2;
185
186 can_sjw_set_default(bt);
187
188 err = can_sjw_check(dev, bt, btc, extack);
189 if (err)
190 return err;
191
192 bt->brp = best_brp;
193
194 /* real bitrate */
195 bt->bitrate = priv->clock.freq /
196 (bt->brp * can_bit_time(bt));
197
198 return 0;
199 }
200
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)201 void can_calc_tdco(struct can_tdc *tdc, const struct can_tdc_const *tdc_const,
202 const struct can_bittiming *dbt,
203 u32 tdc_mask, u32 *ctrlmode, u32 ctrlmode_supported)
204
205 {
206 u32 tdc_auto = tdc_mask & CAN_CTRLMODE_TDC_AUTO_MASK;
207
208 if (!tdc_const || !(ctrlmode_supported & tdc_auto))
209 return;
210
211 *ctrlmode &= ~tdc_mask;
212
213 /* As specified in ISO 11898-1 section 11.3.3 "Transmitter
214 * delay compensation" (TDC) is only applicable if data BRP is
215 * one or two.
216 */
217 if (dbt->brp == 1 || dbt->brp == 2) {
218 /* Sample point in clock periods */
219 u32 sample_point_in_tc = (CAN_SYNC_SEG + dbt->prop_seg +
220 dbt->phase_seg1) * dbt->brp;
221
222 if (sample_point_in_tc < tdc_const->tdco_min)
223 return;
224 tdc->tdco = min(sample_point_in_tc, tdc_const->tdco_max);
225 *ctrlmode |= tdc_auto;
226 }
227 }
228
can_calc_pwm(struct net_device * dev,struct netlink_ext_ack * extack)229 int can_calc_pwm(struct net_device *dev, struct netlink_ext_ack *extack)
230 {
231 struct can_priv *priv = netdev_priv(dev);
232 const struct can_pwm_const *pwm_const = priv->xl.pwm_const;
233 struct can_pwm *pwm = &priv->xl.pwm;
234 u32 xl_tqmin = can_bit_time_tqmin(&priv->xl.data_bittiming);
235 u32 xl_ns = can_tqmin_to_ns(xl_tqmin, priv->clock.freq);
236 u32 nom_tqmin = can_bit_time_tqmin(&priv->bittiming);
237 int pwm_per_bit_max = xl_tqmin / (pwm_const->pwms_min + pwm_const->pwml_min);
238 int pwm_per_bit;
239 u32 pwm_tqmin;
240
241 /* For 5 MB/s databitrate or greater, xl_ns < CAN_PWM_NS_MAX
242 * giving us a pwm_per_bit of 1 and the loop immediately breaks
243 */
244 for (pwm_per_bit = DIV_ROUND_UP(xl_ns, CAN_PWM_NS_MAX);
245 pwm_per_bit <= pwm_per_bit_max; pwm_per_bit++)
246 if (xl_tqmin % pwm_per_bit == 0)
247 break;
248
249 if (pwm_per_bit > pwm_per_bit_max) {
250 NL_SET_ERR_MSG_FMT(extack,
251 "Can not divide the XL data phase's bit time: %u tqmin into multiple PWM symbols",
252 xl_tqmin);
253 return -EINVAL;
254 }
255
256 pwm_tqmin = xl_tqmin / pwm_per_bit;
257 pwm->pwms = DIV_ROUND_UP_POW2(pwm_tqmin, 4);
258 pwm->pwml = pwm_tqmin - pwm->pwms;
259 pwm->pwmo = nom_tqmin % pwm_tqmin;
260
261 return 0;
262 }
263