Linux Audio

Check our new training course

Loading...
v6.8
  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 */
  6
  7#include <linux/can/dev.h>
  8
  9void can_sjw_set_default(struct can_bittiming *bt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 10{
 11	if (bt->sjw)
 12		return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 13
 14	/* If user space provides no sjw, use sane default of phase_seg2 / 2 */
 15	bt->sjw = max(1U, min(bt->phase_seg1, bt->phase_seg2 / 2));
 
 
 16}
 17
 18int can_sjw_check(const struct net_device *dev, const struct can_bittiming *bt,
 19		  const struct can_bittiming_const *btc, struct netlink_ext_ack *extack)
 20{
 21	if (bt->sjw > btc->sjw_max) {
 22		NL_SET_ERR_MSG_FMT(extack, "sjw: %u greater than max sjw: %u",
 23				   bt->sjw, btc->sjw_max);
 24		return -EINVAL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 25	}
 26
 27	if (bt->sjw > bt->phase_seg1) {
 28		NL_SET_ERR_MSG_FMT(extack,
 29				   "sjw: %u greater than phase-seg1: %u",
 30				   bt->sjw, bt->phase_seg1);
 31		return -EINVAL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 32	}
 33
 34	if (bt->sjw > bt->phase_seg2) {
 35		NL_SET_ERR_MSG_FMT(extack,
 36				   "sjw: %u greater than phase-seg2: %u",
 37				   bt->sjw, bt->phase_seg2);
 38		return -EINVAL;
 
 
 
 
 
 
 
 
 39	}
 40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 41	return 0;
 42}
 43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 44/* Checks the validity of the specified bit-timing parameters prop_seg,
 45 * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate
 46 * prescaler value brp. You can find more information in the header
 47 * file linux/can/netlink.h.
 48 */
 49static int can_fixup_bittiming(const struct net_device *dev, struct can_bittiming *bt,
 50			       const struct can_bittiming_const *btc,
 51			       struct netlink_ext_ack *extack)
 52{
 53	const unsigned int tseg1 = bt->prop_seg + bt->phase_seg1;
 54	const struct can_priv *priv = netdev_priv(dev);
 55	u64 brp64;
 56	int err;
 57
 58	if (tseg1 < btc->tseg1_min) {
 59		NL_SET_ERR_MSG_FMT(extack, "prop-seg + phase-seg1: %u less than tseg1-min: %u",
 60				   tseg1, btc->tseg1_min);
 61		return -EINVAL;
 62	}
 63	if (tseg1 > btc->tseg1_max) {
 64		NL_SET_ERR_MSG_FMT(extack, "prop-seg + phase-seg1: %u greater than tseg1-max: %u",
 65				   tseg1, btc->tseg1_max);
 66		return -EINVAL;
 67	}
 68	if (bt->phase_seg2 < btc->tseg2_min) {
 69		NL_SET_ERR_MSG_FMT(extack, "phase-seg2: %u less than tseg2-min: %u",
 70				   bt->phase_seg2, btc->tseg2_min);
 71		return -EINVAL;
 72	}
 73	if (bt->phase_seg2 > btc->tseg2_max) {
 74		NL_SET_ERR_MSG_FMT(extack, "phase-seg2: %u greater than tseg2-max: %u",
 75				   bt->phase_seg2, btc->tseg2_max);
 76		return -EINVAL;
 77	}
 78
 79	can_sjw_set_default(bt);
 80
 81	err = can_sjw_check(dev, bt, btc, extack);
 82	if (err)
 83		return err;
 
 
 
 
 84
 85	brp64 = (u64)priv->clock.freq * (u64)bt->tq;
 86	if (btc->brp_inc > 1)
 87		do_div(brp64, btc->brp_inc);
 88	brp64 += 500000000UL - 1;
 89	do_div(brp64, 1000000000UL); /* the practicable BRP */
 90	if (btc->brp_inc > 1)
 91		brp64 *= btc->brp_inc;
 92	bt->brp = (u32)brp64;
 93
 94	if (bt->brp < btc->brp_min) {
 95		NL_SET_ERR_MSG_FMT(extack, "resulting brp: %u less than brp-min: %u",
 96				   bt->brp, btc->brp_min);
 97		return -EINVAL;
 98	}
 99	if (bt->brp > btc->brp_max) {
100		NL_SET_ERR_MSG_FMT(extack, "resulting brp: %u greater than brp-max: %u",
101				   bt->brp, btc->brp_max);
102		return -EINVAL;
103	}
104
105	bt->bitrate = priv->clock.freq / (bt->brp * can_bit_time(bt));
106	bt->sample_point = ((CAN_SYNC_SEG + tseg1) * 1000) / can_bit_time(bt);
107	bt->tq = DIV_U64_ROUND_CLOSEST(mul_u32_u32(bt->brp, NSEC_PER_SEC),
108				       priv->clock.freq);
109
110	return 0;
111}
112
113/* Checks the validity of predefined bitrate settings */
114static int
115can_validate_bitrate(const struct net_device *dev, const struct can_bittiming *bt,
116		     const u32 *bitrate_const,
117		     const unsigned int bitrate_const_cnt,
118		     struct netlink_ext_ack *extack)
119{
 
120	unsigned int i;
121
122	for (i = 0; i < bitrate_const_cnt; i++) {
123		if (bt->bitrate == bitrate_const[i])
124			return 0;
125	}
126
127	NL_SET_ERR_MSG_FMT(extack, "bitrate %u bps not supported",
128			   bt->brp);
129
130	return -EINVAL;
131}
132
133int can_get_bittiming(const struct net_device *dev, struct can_bittiming *bt,
134		      const struct can_bittiming_const *btc,
135		      const u32 *bitrate_const,
136		      const unsigned int bitrate_const_cnt,
137		      struct netlink_ext_ack *extack)
138{
 
 
139	/* Depending on the given can_bittiming parameter structure the CAN
140	 * timing parameters are calculated based on the provided bitrate OR
141	 * alternatively the CAN timing parameters (tq, prop_seg, etc.) are
142	 * provided directly which are then checked and fixed up.
143	 */
144	if (!bt->tq && bt->bitrate && btc)
145		return can_calc_bittiming(dev, bt, btc, extack);
146	if (bt->tq && !bt->bitrate && btc)
147		return can_fixup_bittiming(dev, bt, btc, extack);
148	if (!bt->tq && bt->bitrate && bitrate_const)
149		return can_validate_bitrate(dev, bt, bitrate_const,
150					    bitrate_const_cnt, extack);
 
 
151
152	return -EINVAL;
153}
v5.14.15
  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 */
  6
  7#include <linux/can/dev.h>
  8
  9#ifdef CONFIG_CAN_CALC_BITTIMING
 10#define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
 11
 12/* Bit-timing calculation derived from:
 13 *
 14 * Code based on LinCAN sources and H8S2638 project
 15 * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
 16 * Copyright 2005      Stanislav Marek
 17 * email: pisa@cmp.felk.cvut.cz
 18 *
 19 * Calculates proper bit-timing parameters for a specified bit-rate
 20 * and sample-point, which can then be used to set the bit-timing
 21 * registers of the CAN controller. You can find more information
 22 * in the header file linux/can/netlink.h.
 23 */
 24static int
 25can_update_sample_point(const struct can_bittiming_const *btc,
 26			unsigned int sample_point_nominal, unsigned int tseg,
 27			unsigned int *tseg1_ptr, unsigned int *tseg2_ptr,
 28			unsigned int *sample_point_error_ptr)
 29{
 30	unsigned int sample_point_error, best_sample_point_error = UINT_MAX;
 31	unsigned int sample_point, best_sample_point = 0;
 32	unsigned int tseg1, tseg2;
 33	int i;
 34
 35	for (i = 0; i <= 1; i++) {
 36		tseg2 = tseg + CAN_SYNC_SEG -
 37			(sample_point_nominal * (tseg + CAN_SYNC_SEG)) /
 38			1000 - i;
 39		tseg2 = clamp(tseg2, btc->tseg2_min, btc->tseg2_max);
 40		tseg1 = tseg - tseg2;
 41		if (tseg1 > btc->tseg1_max) {
 42			tseg1 = btc->tseg1_max;
 43			tseg2 = tseg - tseg1;
 44		}
 45
 46		sample_point = 1000 * (tseg + CAN_SYNC_SEG - tseg2) /
 47			(tseg + CAN_SYNC_SEG);
 48		sample_point_error = abs(sample_point_nominal - sample_point);
 49
 50		if (sample_point <= sample_point_nominal &&
 51		    sample_point_error < best_sample_point_error) {
 52			best_sample_point = sample_point;
 53			best_sample_point_error = sample_point_error;
 54			*tseg1_ptr = tseg1;
 55			*tseg2_ptr = tseg2;
 56		}
 57	}
 58
 59	if (sample_point_error_ptr)
 60		*sample_point_error_ptr = best_sample_point_error;
 61
 62	return best_sample_point;
 63}
 64
 65int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
 66		       const struct can_bittiming_const *btc)
 67{
 68	struct can_priv *priv = netdev_priv(dev);
 69	unsigned int bitrate;			/* current bitrate */
 70	unsigned int bitrate_error;		/* difference between current and nominal value */
 71	unsigned int best_bitrate_error = UINT_MAX;
 72	unsigned int sample_point_error;	/* difference between current and nominal value */
 73	unsigned int best_sample_point_error = UINT_MAX;
 74	unsigned int sample_point_nominal;	/* nominal sample point */
 75	unsigned int best_tseg = 0;		/* current best value for tseg */
 76	unsigned int best_brp = 0;		/* current best value for brp */
 77	unsigned int brp, tsegall, tseg, tseg1 = 0, tseg2 = 0;
 78	u64 v64;
 79
 80	/* Use CiA recommended sample points */
 81	if (bt->sample_point) {
 82		sample_point_nominal = bt->sample_point;
 83	} else {
 84		if (bt->bitrate > 800 * CAN_KBPS)
 85			sample_point_nominal = 750;
 86		else if (bt->bitrate > 500 * CAN_KBPS)
 87			sample_point_nominal = 800;
 88		else
 89			sample_point_nominal = 875;
 90	}
 91
 92	/* tseg even = round down, odd = round up */
 93	for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
 94	     tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
 95		tsegall = CAN_SYNC_SEG + tseg / 2;
 96
 97		/* Compute all possible tseg choices (tseg=tseg1+tseg2) */
 98		brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
 99
100		/* choose brp step which is possible in system */
101		brp = (brp / btc->brp_inc) * btc->brp_inc;
102		if (brp < btc->brp_min || brp > btc->brp_max)
103			continue;
104
105		bitrate = priv->clock.freq / (brp * tsegall);
106		bitrate_error = abs(bt->bitrate - bitrate);
107
108		/* tseg brp biterror */
109		if (bitrate_error > best_bitrate_error)
110			continue;
111
112		/* reset sample point error if we have a better bitrate */
113		if (bitrate_error < best_bitrate_error)
114			best_sample_point_error = UINT_MAX;
115
116		can_update_sample_point(btc, sample_point_nominal, tseg / 2,
117					&tseg1, &tseg2, &sample_point_error);
118		if (sample_point_error > best_sample_point_error)
119			continue;
120
121		best_sample_point_error = sample_point_error;
122		best_bitrate_error = bitrate_error;
123		best_tseg = tseg / 2;
124		best_brp = brp;
125
126		if (bitrate_error == 0 && sample_point_error == 0)
127			break;
128	}
129
130	if (best_bitrate_error) {
131		/* Error in one-tenth of a percent */
132		v64 = (u64)best_bitrate_error * 1000;
133		do_div(v64, bt->bitrate);
134		bitrate_error = (u32)v64;
135		if (bitrate_error > CAN_CALC_MAX_ERROR) {
136			netdev_err(dev,
137				   "bitrate error %d.%d%% too high\n",
138				   bitrate_error / 10, bitrate_error % 10);
139			return -EDOM;
140		}
141		netdev_warn(dev, "bitrate error %d.%d%%\n",
142			    bitrate_error / 10, bitrate_error % 10);
143	}
144
145	/* real sample point */
146	bt->sample_point = can_update_sample_point(btc, sample_point_nominal,
147						   best_tseg, &tseg1, &tseg2,
148						   NULL);
149
150	v64 = (u64)best_brp * 1000 * 1000 * 1000;
151	do_div(v64, priv->clock.freq);
152	bt->tq = (u32)v64;
153	bt->prop_seg = tseg1 / 2;
154	bt->phase_seg1 = tseg1 - bt->prop_seg;
155	bt->phase_seg2 = tseg2;
156
157	/* check for sjw user settings */
158	if (!bt->sjw || !btc->sjw_max) {
159		bt->sjw = 1;
160	} else {
161		/* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */
162		if (bt->sjw > btc->sjw_max)
163			bt->sjw = btc->sjw_max;
164		/* bt->sjw must not be higher than tseg2 */
165		if (tseg2 < bt->sjw)
166			bt->sjw = tseg2;
167	}
168
169	bt->brp = best_brp;
170
171	/* real bitrate */
172	bt->bitrate = priv->clock.freq /
173		(bt->brp * (CAN_SYNC_SEG + tseg1 + tseg2));
174
175	return 0;
176}
177
178void can_calc_tdco(struct net_device *dev)
179{
180	struct can_priv *priv = netdev_priv(dev);
181	const struct can_bittiming *dbt = &priv->data_bittiming;
182	struct can_tdc *tdc = &priv->tdc;
183	const struct can_tdc_const *tdc_const = priv->tdc_const;
184
185	if (!tdc_const)
186		return;
187
188	/* As specified in ISO 11898-1 section 11.3.3 "Transmitter
189	 * delay compensation" (TDC) is only applicable if data BRP is
190	 * one or two.
191	 */
192	if (dbt->brp == 1 || dbt->brp == 2) {
193		/* Reuse "normal" sample point and convert it to time quanta */
194		u32 sample_point_in_tq = can_bit_time(dbt) * dbt->sample_point / 1000;
195
196		tdc->tdco = min(sample_point_in_tq, tdc_const->tdco_max);
197	} else {
198		tdc->tdco = 0;
199	}
200}
201#endif /* CONFIG_CAN_CALC_BITTIMING */
202
203/* Checks the validity of the specified bit-timing parameters prop_seg,
204 * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate
205 * prescaler value brp. You can find more information in the header
206 * file linux/can/netlink.h.
207 */
208static int can_fixup_bittiming(struct net_device *dev, struct can_bittiming *bt,
209			       const struct can_bittiming_const *btc)
 
210{
211	struct can_priv *priv = netdev_priv(dev);
212	int tseg1, alltseg;
213	u64 brp64;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
214
215	tseg1 = bt->prop_seg + bt->phase_seg1;
216	if (!bt->sjw)
217		bt->sjw = 1;
218	if (bt->sjw > btc->sjw_max ||
219	    tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max ||
220	    bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max)
221		return -ERANGE;
222
223	brp64 = (u64)priv->clock.freq * (u64)bt->tq;
224	if (btc->brp_inc > 1)
225		do_div(brp64, btc->brp_inc);
226	brp64 += 500000000UL - 1;
227	do_div(brp64, 1000000000UL); /* the practicable BRP */
228	if (btc->brp_inc > 1)
229		brp64 *= btc->brp_inc;
230	bt->brp = (u32)brp64;
231
232	if (bt->brp < btc->brp_min || bt->brp > btc->brp_max)
 
 
 
 
 
 
 
233		return -EINVAL;
 
234
235	alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1;
236	bt->bitrate = priv->clock.freq / (bt->brp * alltseg);
237	bt->sample_point = ((tseg1 + 1) * 1000) / alltseg;
 
238
239	return 0;
240}
241
242/* Checks the validity of predefined bitrate settings */
243static int
244can_validate_bitrate(struct net_device *dev, struct can_bittiming *bt,
245		     const u32 *bitrate_const,
246		     const unsigned int bitrate_const_cnt)
 
247{
248	struct can_priv *priv = netdev_priv(dev);
249	unsigned int i;
250
251	for (i = 0; i < bitrate_const_cnt; i++) {
252		if (bt->bitrate == bitrate_const[i])
253			break;
254	}
255
256	if (i >= priv->bitrate_const_cnt)
257		return -EINVAL;
258
259	return 0;
260}
261
262int can_get_bittiming(struct net_device *dev, struct can_bittiming *bt,
263		      const struct can_bittiming_const *btc,
264		      const u32 *bitrate_const,
265		      const unsigned int bitrate_const_cnt)
 
266{
267	int err;
268
269	/* Depending on the given can_bittiming parameter structure the CAN
270	 * timing parameters are calculated based on the provided bitrate OR
271	 * alternatively the CAN timing parameters (tq, prop_seg, etc.) are
272	 * provided directly which are then checked and fixed up.
273	 */
274	if (!bt->tq && bt->bitrate && btc)
275		err = can_calc_bittiming(dev, bt, btc);
276	else if (bt->tq && !bt->bitrate && btc)
277		err = can_fixup_bittiming(dev, bt, btc);
278	else if (!bt->tq && bt->bitrate && bitrate_const)
279		err = can_validate_bitrate(dev, bt, bitrate_const,
280					   bitrate_const_cnt);
281	else
282		err = -EINVAL;
283
284	return err;
285}