1 /* can-calc-bit-timing.c: Calculate CAN bit timing parameters
3 * Copyright (C) 2008 Wolfgang Grandegger <wg@grandegger.com>
4 * Copyright (C) 2016 Marc Kleine-Budde <mkl@pengutronix.de>
7 * can_baud.c - CAN baudrate calculation
8 * Code based on LinCAN sources and H8S2638 project
9 * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
10 * Copyright 2005 Stanislav Marek
11 * email:pisa@cmp.felk.cvut.cz
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
28 #include <linux/types.h>
29 #include <linux/can/netlink.h>
31 /* imported from kernel */
34 * abs - return absolute value of an argument
35 * @x: the value. If it is unsigned type, it is converted to signed type first.
36 * char is treated as if it was signed (regardless of whether it really is)
37 * but the macro's return type is preserved as char.
39 * Return: an absolute value of x.
41 #define abs(x) __abs_choose_expr(x, long long, \
42 __abs_choose_expr(x, long, \
43 __abs_choose_expr(x, int, \
44 __abs_choose_expr(x, short, \
45 __abs_choose_expr(x, char, \
46 __builtin_choose_expr( \
47 __builtin_types_compatible_p(typeof(x), char), \
48 (char)({ signed char __x = (x); __x<0?-__x:__x; }), \
51 #define __abs_choose_expr(x, type, other) __builtin_choose_expr( \
52 __builtin_types_compatible_p(typeof(x), signed type) || \
53 __builtin_types_compatible_p(typeof(x), unsigned type), \
54 ({ signed type __x = (x); __x < 0 ? -__x : __x; }), other)
57 * min()/max()/clamp() macros that also do
58 * strict type-checking.. See the
59 * "unnecessary" pointer comparison.
61 #define min(x, y) ({ \
62 typeof(x) _min1 = (x); \
63 typeof(y) _min2 = (y); \
64 (void) (&_min1 == &_min2); \
65 _min1 < _min2 ? _min1 : _min2; })
67 #define max(x, y) ({ \
68 typeof(x) _max1 = (x); \
69 typeof(y) _max2 = (y); \
70 (void) (&_max1 == &_max2); \
71 _max1 > _max2 ? _max1 : _max2; })
74 * clamp - return a value clamped to a given range with strict typechecking
76 * @lo: lowest allowable value
77 * @hi: highest allowable value
79 * This macro does strict typechecking of lo/hi to make sure they are of the
80 * same type as val. See the unnecessary pointer comparisons.
82 #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
84 # define do_div(n,base) ({ \
85 uint32_t __base = (base); \
87 __rem = ((uint64_t)(n)) % __base; \
88 (n) = ((uint64_t)(n)) / __base; \
94 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
96 /* we don't want to see these prints */
97 #define netdev_err(dev, format, arg...) do { } while (0)
98 #define netdev_warn(dev, format, arg...) do { } while (0)
100 /* define in-kernel-types */
104 struct calc_bittiming_const {
105 struct can_bittiming_const bittiming_const;
107 __u32 ref_clk; /* CAN system clock frequency in Hz */
108 void (*printf_btr)(struct can_bittiming *bt, bool hdr);
112 * minimal structs, just enough to be source level compatible
115 struct can_clock clock;
119 struct can_priv priv;
122 static inline void *netdev_priv(const struct net_device *dev)
124 return (void *)&dev->priv;
127 static void print_usage(char *cmd)
129 printf("Usage: %s [options] [<CAN-contoller-name>]\n"
131 "\t-q : don't print header line\n"
132 "\t-l : list all support CAN controller names\n"
133 "\t-b <bitrate> : bit-rate in bits/sec\n"
134 "\t-s <samp_pt> : sample-point in one-tenth of a percent\n"
135 "\t or 0 for CIA recommended sample points\n"
136 "\t-c <clock> : real CAN system clock in Hz\n",
142 static void printf_btr_sja1000(struct can_bittiming *bt, bool hdr)
149 btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
150 btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
151 (((bt->phase_seg2 - 1) & 0x7) << 4);
152 printf("0x%02x 0x%02x", btr0, btr1);
156 static void printf_btr_at91(struct can_bittiming *bt, bool hdr)
159 printf("%10s", "CAN_BR");
161 uint32_t br = ((bt->phase_seg2 - 1) |
162 ((bt->phase_seg1 - 1) << 4) |
163 ((bt->prop_seg - 1) << 8) |
164 ((bt->sjw - 1) << 12) |
165 ((bt->brp - 1) << 16));
166 printf("0x%08x", br);
170 static void printf_btr_flexcan(struct can_bittiming *bt, bool hdr)
173 printf("%10s", "CAN_CTRL");
175 uint32_t ctrl = (((bt->brp - 1) << 24) |
176 ((bt->sjw - 1) << 22) |
177 ((bt->phase_seg1 - 1) << 19) |
178 ((bt->phase_seg2 - 1) << 16) |
179 ((bt->prop_seg - 1) << 0));
181 printf("0x%08x", ctrl);
185 static void printf_btr_mcp251x(struct can_bittiming *bt, bool hdr)
187 uint8_t cnf1, cnf2, cnf3;
190 printf("CNF1 CNF2 CNF3");
192 cnf1 = ((bt->sjw - 1) << 6) | (bt->brp - 1);
193 cnf2 = 0x80 | ((bt->phase_seg1 - 1) << 3) | (bt->prop_seg - 1);
194 cnf3 = bt->phase_seg2 - 1;
195 printf("0x%02x 0x%02x 0x%02x", cnf1, cnf2, cnf3);
199 static void printf_btr_ti_hecc(struct can_bittiming *bt, bool hdr)
202 printf("%10s", "CANBTC");
206 can_btc = (bt->phase_seg2 - 1) & 0x7;
207 can_btc |= ((bt->phase_seg1 + bt->prop_seg - 1)
209 can_btc |= ((bt->sjw - 1) & 0x3) << 8;
210 can_btc |= ((bt->brp - 1) & 0xFF) << 16;
212 printf("0x%08x", can_btc);
216 #define RCAR_CAN_BCR_TSEG1(x) (((x) & 0x0f) << 20)
217 #define RCAR_CAN_BCR_BPR(x) (((x) & 0x3ff) << 8)
218 #define RCAR_CAN_BCR_SJW(x) (((x) & 0x3) << 4)
219 #define RCAR_CAN_BCR_TSEG2(x) ((x) & 0x07)
221 static void printf_btr_rcar_can(struct can_bittiming *bt, bool hdr)
224 printf("%10s", "CiBCR");
228 bcr = RCAR_CAN_BCR_TSEG1(bt->phase_seg1 + bt->prop_seg - 1) |
229 RCAR_CAN_BCR_BPR(bt->brp - 1) |
230 RCAR_CAN_BCR_SJW(bt->sjw - 1) |
231 RCAR_CAN_BCR_TSEG2(bt->phase_seg2 - 1);
233 printf("0x%08x", bcr << 8);
237 static struct calc_bittiming_const can_calc_consts[] = {
251 .printf_btr = printf_btr_sja1000,
265 .printf_btr = printf_btr_sja1000,
279 .printf_btr = printf_btr_sja1000,
293 .printf_btr = printf_btr_sja1000,
307 .printf_btr = printf_btr_sja1000,
320 .ref_clk = 66660000, /* mpc5121 */
321 .printf_btr = printf_btr_sja1000,
334 .ref_clk = 66666666, /* mpc5121 */
335 .printf_btr = printf_btr_sja1000,
348 .ref_clk = 100000000,
349 .printf_btr = printf_btr_at91,
362 /* real world clock as found on the ronetix PM9263 */
364 .printf_btr = printf_btr_at91,
377 .ref_clk = 24000000, /* mx28 */
378 .printf_btr = printf_btr_flexcan,
391 .ref_clk = 30000000, /* mx6 */
392 .printf_btr = printf_btr_flexcan,
406 .printf_btr = printf_btr_flexcan,
420 .printf_btr = printf_btr_flexcan,
434 .printf_btr = printf_btr_flexcan,
448 .printf_btr = printf_btr_flexcan,
462 .printf_btr = printf_btr_flexcan, /* vybrid */
476 .printf_btr = printf_btr_mcp251x,
490 .printf_btr = printf_btr_mcp251x,
504 .printf_btr = printf_btr_ti_hecc,
518 .printf_btr = printf_btr_rcar_can,
522 static long common_bitrates[] = {
534 #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
535 #define CAN_CALC_SYNC_SEG 1
538 * Bit-timing calculation derived from:
540 * Code based on LinCAN sources and H8S2638 project
541 * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
542 * Copyright 2005 Stanislav Marek
543 * email: pisa@cmp.felk.cvut.cz
545 * Calculates proper bit-timing parameters for a specified bit-rate
546 * and sample-point, which can then be used to set the bit-timing
547 * registers of the CAN controller. You can find more information
548 * in the header file linux/can/netlink.h.
550 static int can_update_spt(const struct can_bittiming_const *btc,
551 unsigned int spt_target, unsigned int tseg,
552 unsigned int *tseg1_ptr, unsigned int *tseg2_ptr,
553 unsigned int *spt_error_ptr)
555 unsigned int spt_error, best_spt_error = UINT_MAX;
556 unsigned int spt, best_spt = 0;
557 unsigned int tseg1, tseg2;
560 for (i = 0; i <= 1; i++) {
561 tseg2 = tseg + CAN_CALC_SYNC_SEG - (spt_target * (tseg + CAN_CALC_SYNC_SEG)) / 1000 - i;
562 tseg2 = clamp(tseg2, btc->tseg2_min, btc->tseg2_max);
563 tseg1 = tseg - tseg2;
564 if (tseg1 > btc->tseg1_max) {
565 tseg1 = btc->tseg1_max;
566 tseg2 = tseg - tseg1;
569 spt = 1000 * (tseg + CAN_CALC_SYNC_SEG - tseg2) / (tseg + CAN_CALC_SYNC_SEG);
570 spt_error = abs(spt_target - spt);
572 if ((spt <= spt_target) && (spt_error < best_spt_error)) {
574 best_spt_error = spt_error;
581 *spt_error_ptr = best_spt_error;
586 static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
587 const struct can_bittiming_const *btc)
589 struct can_priv *priv = netdev_priv(dev);
590 unsigned int rate; /* current bitrate */
591 unsigned int rate_error; /* difference between current and target value */
592 unsigned int best_rate_error = UINT_MAX;
593 unsigned int spt_error; /* difference between current and target value */
594 unsigned int best_spt_error = UINT_MAX;
595 unsigned int spt_target; /* target sample point */
596 unsigned int best_tseg = 0; /* current best value for tseg */
597 unsigned int best_brp = 0; /* current best value for brp */
598 unsigned int brp, tsegall, tseg, tseg1, tseg2;
601 /* Use CiA recommended sample points */
602 if (bt->sample_point) {
603 spt_target = bt->sample_point;
605 if (bt->bitrate > 800000)
607 else if (bt->bitrate > 500000)
613 /* tseg even = round down, odd = round up */
614 for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
615 tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
616 tsegall = CAN_CALC_SYNC_SEG + tseg / 2;
618 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
619 brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
621 /* choose brp step which is possible in system */
622 brp = (brp / btc->brp_inc) * btc->brp_inc;
623 if ((brp < btc->brp_min) || (brp > btc->brp_max))
626 rate = priv->clock.freq / (brp * tsegall);
627 rate_error = abs(bt->bitrate - rate);
629 /* tseg brp biterror */
630 if (rate_error > best_rate_error)
633 /* reset sample point error if we have a better bitrate */
634 if (rate_error < best_rate_error)
635 best_spt_error = UINT_MAX;
637 can_update_spt(btc, spt_target, tseg / 2, &tseg1, &tseg2, &spt_error);
638 if (spt_error > best_spt_error)
641 best_spt_error = spt_error;
642 best_rate_error = rate_error;
643 best_tseg = tseg / 2;
646 if (rate_error == 0 && spt_error == 0)
650 if (best_rate_error) {
651 /* Error in one-tenth of a percent */
652 rate_error = (best_rate_error * 1000) / bt->bitrate;
653 if (rate_error > CAN_CALC_MAX_ERROR) {
655 "bitrate error %ld.%ld%% too high\n",
656 rate_error / 10, rate_error % 10);
659 netdev_warn(dev, "bitrate error %ld.%ld%%\n",
660 rate_error / 10, rate_error % 10);
663 /* real sample point */
664 bt->sample_point = can_update_spt(btc, spt_target, best_tseg,
665 &tseg1, &tseg2, NULL);
667 v64 = (u64)best_brp * 1000 * 1000 * 1000;
668 do_div(v64, priv->clock.freq);
670 bt->prop_seg = tseg1 / 2;
671 bt->phase_seg1 = tseg1 - bt->prop_seg;
672 bt->phase_seg2 = tseg2;
674 /* check for sjw user settings */
675 if (!bt->sjw || !btc->sjw_max) {
678 /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */
679 if (bt->sjw > btc->sjw_max)
680 bt->sjw = btc->sjw_max;
681 /* bt->sjw must not be higher than tseg2 */
689 bt->bitrate = priv->clock.freq / (bt->brp * (CAN_CALC_SYNC_SEG + tseg1 + tseg2));
694 static __u32 get_cia_sample_point(__u32 bitrate)
698 if (bitrate > 800000)
700 else if (bitrate > 500000)
708 static void print_bit_timing(const struct calc_bittiming_const *btc,
709 __u32 bitrate, __u32 sample_point, __u32 ref_clk,
712 struct net_device dev = {
713 .priv.clock.freq = ref_clk,
715 struct can_bittiming bt = {
717 .sample_point = sample_point,
719 long rate_error, spt_error;
722 printf("Bit timing parameters for %s with %.6f MHz ref clock\n"
723 "nominal real Bitrt nom real SampP\n"
724 "Bitrate TQ[ns] PrS PhS1 PhS2 SJW BRP Bitrate Error SampP SampP Error ",
725 btc->bittiming_const.name,
726 ref_clk / 1000000.0);
728 btc->printf_btr(&bt, true);
732 if (can_calc_bittiming(&dev, &bt, &btc->bittiming_const)) {
733 printf("%7d ***bitrate not possible***\n", bitrate);
737 /* get nominal sample point */
739 sample_point = get_cia_sample_point(bitrate);
741 rate_error = abs(bitrate - bt.bitrate);
742 spt_error = abs(sample_point - bt.sample_point);
748 "%4.1f%% %4.1f%% %4.1f%% ",
750 bt.tq, bt.prop_seg, bt.phase_seg1, bt.phase_seg2,
754 100.0 * rate_error / bitrate,
757 bt.sample_point / 10.0,
758 100.0 * spt_error / sample_point);
760 btc->printf_btr(&bt, false);
764 static void do_list(void)
768 for (i = 0; i < ARRAY_SIZE(can_calc_consts); i++)
769 printf("%s\n", can_calc_consts[i].bittiming_const.name);
772 int main(int argc, char *argv[])
775 __u32 opt_ref_clk = 0, ref_clk;
776 unsigned int spt_target = 0;
777 bool quiet = false, list = false, found = false;
782 const struct calc_bittiming_const *btc = NULL;
784 while ((opt = getopt(argc, argv, "b:c:lps:")) != -1) {
787 bitrate = atoi(optarg);
791 opt_ref_clk = strtoul(optarg, NULL, 10);
803 spt_target = strtoul(optarg, NULL, 10);
807 print_usage(argv[0]);
812 if (argc > optind + 1)
813 print_usage(argv[0]);
815 if (argc == optind + 1)
823 if (spt_target && (spt_target >= 1000 || spt_target < 100))
824 print_usage(argv[0]);
826 for (i = 0; i < ARRAY_SIZE(can_calc_consts); i++) {
827 if (name && strcmp(can_calc_consts[i].bittiming_const.name, name))
831 btc = &can_calc_consts[i];
834 ref_clk = opt_ref_clk;
836 ref_clk = btc->ref_clk;
839 print_bit_timing(btc, bitrate, spt_target, ref_clk, quiet);
841 for (j = 0; j < ARRAY_SIZE(common_bitrates); j++)
842 print_bit_timing(btc, common_bitrates[j],
843 spt_target, ref_clk, j);
849 printf("error: unknown CAN controller '%s', try one of these:\n\n", name);