1 /* can-calc-bit-timing.c: Calculate CAN bit timing parameters
3 * Copyright (C) 2008 Wolfgang Grandegger <wg@grandegger.com>
6 * can_baud.c - CAN baudrate calculation
7 * Code based on LinCAN sources and H8S2638 project
8 * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
9 * Copyright 2005 Stanislav Marek
10 * email:pisa@cmp.felk.cvut.cz
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
26 #include <linux/types.h>
27 #include <linux/can/netlink.h>
29 /* imported from kernel */
32 * abs - return absolute value of an argument
33 * @x: the value. If it is unsigned type, it is converted to signed type first.
34 * char is treated as if it was signed (regardless of whether it really is)
35 * but the macro's return type is preserved as char.
37 * Return: an absolute value of x.
39 #define abs(x) __abs_choose_expr(x, long long, \
40 __abs_choose_expr(x, long, \
41 __abs_choose_expr(x, int, \
42 __abs_choose_expr(x, short, \
43 __abs_choose_expr(x, char, \
44 __builtin_choose_expr( \
45 __builtin_types_compatible_p(typeof(x), char), \
46 (char)({ signed char __x = (x); __x<0?-__x:__x; }), \
49 #define __abs_choose_expr(x, type, other) __builtin_choose_expr( \
50 __builtin_types_compatible_p(typeof(x), signed type) || \
51 __builtin_types_compatible_p(typeof(x), unsigned type), \
52 ({ signed type __x = (x); __x < 0 ? -__x : __x; }), other)
55 * min()/max()/clamp() macros that also do
56 * strict type-checking.. See the
57 * "unnecessary" pointer comparison.
59 #define min(x, y) ({ \
60 typeof(x) _min1 = (x); \
61 typeof(y) _min2 = (y); \
62 (void) (&_min1 == &_min2); \
63 _min1 < _min2 ? _min1 : _min2; })
65 #define max(x, y) ({ \
66 typeof(x) _max1 = (x); \
67 typeof(y) _max2 = (y); \
68 (void) (&_max1 == &_max2); \
69 _max1 > _max2 ? _max1 : _max2; })
72 * clamp - return a value clamped to a given range with strict typechecking
74 * @lo: lowest allowable value
75 * @hi: highest allowable value
77 * This macro does strict typechecking of lo/hi to make sure they are of the
78 * same type as val. See the unnecessary pointer comparisons.
80 #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
82 # define do_div(n,base) ({ \
83 uint32_t __base = (base); \
85 __rem = ((uint64_t)(n)) % __base; \
86 (n) = ((uint64_t)(n)) / __base; \
92 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
94 /* we don't want to see these prints */
95 #define dev_err(dev, format, arg...) do { } while (0)
96 #define dev_warn(dev, format, arg...) do { } while (0)
98 /* define in-kernel-types */
102 struct calc_bittiming_const {
103 struct can_bittiming_const bittiming_const;
105 __u32 ref_clk; /* CAN system clock frequency in Hz */
106 void (*printf_btr)(struct can_bittiming *bt, bool hdr);
110 * minimal structs, just enough to be source level compatible
113 const struct can_bittiming_const *bittiming_const;
114 struct can_clock clock;
118 struct can_priv priv;
121 static inline void *netdev_priv(const struct net_device *dev)
123 return (void *)&dev->priv;
126 static void print_usage(char *cmd)
128 printf("Usage: %s [options] [<CAN-contoller-name>]\n"
130 "\t-q : don't print header line\n"
131 "\t-l : list all support CAN controller names\n"
132 "\t-b <bitrate> : bit-rate in bits/sec\n"
133 "\t-s <samp_pt> : sample-point in one-tenth of a percent\n"
134 "\t or 0 for CIA recommended sample points\n"
135 "\t-c <clock> : real CAN system clock in Hz\n",
141 static void printf_btr_sja1000(struct can_bittiming *bt, bool hdr)
148 btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
149 btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
150 (((bt->phase_seg2 - 1) & 0x7) << 4);
151 printf("0x%02x 0x%02x", btr0, btr1);
155 static void printf_btr_at91(struct can_bittiming *bt, bool hdr)
158 printf("%10s", "CAN_BR");
160 uint32_t br = ((bt->phase_seg2 - 1) |
161 ((bt->phase_seg1 - 1) << 4) |
162 ((bt->prop_seg - 1) << 8) |
163 ((bt->sjw - 1) << 12) |
164 ((bt->brp - 1) << 16));
165 printf("0x%08x", br);
169 static void printf_btr_flexcan(struct can_bittiming *bt, bool hdr)
172 printf("%10s", "CAN_CTRL");
174 uint32_t ctrl = (((bt->brp - 1) << 24) |
175 ((bt->sjw - 1) << 22) |
176 ((bt->phase_seg1 - 1) << 19) |
177 ((bt->phase_seg2 - 1) << 16) |
178 ((bt->prop_seg - 1) << 0));
180 printf("0x%08x", ctrl);
184 static void printf_btr_mcp251x(struct can_bittiming *bt, bool hdr)
186 uint8_t cnf1, cnf2, cnf3;
189 printf("CNF1 CNF2 CNF3");
191 cnf1 = ((bt->sjw - 1) << 6) | (bt->brp - 1);
192 cnf2 = 0x80 | ((bt->phase_seg1 - 1) << 3) | (bt->prop_seg - 1);
193 cnf3 = bt->phase_seg2 - 1;
194 printf("0x%02x 0x%02x 0x%02x", cnf1, cnf2, cnf3);
198 static void printf_btr_ti_hecc(struct can_bittiming *bt, bool hdr)
201 printf("%10s", "CANBTC");
205 can_btc = (bt->phase_seg2 - 1) & 0x7;
206 can_btc |= ((bt->phase_seg1 + bt->prop_seg - 1)
208 can_btc |= ((bt->sjw - 1) & 0x3) << 8;
209 can_btc |= ((bt->brp - 1) & 0xFF) << 16;
211 printf("0x%08x", can_btc);
215 #define RCAR_CAN_BCR_TSEG1(x) (((x) & 0x0f) << 20)
216 #define RCAR_CAN_BCR_BPR(x) (((x) & 0x3ff) << 8)
217 #define RCAR_CAN_BCR_SJW(x) (((x) & 0x3) << 4)
218 #define RCAR_CAN_BCR_TSEG2(x) ((x) & 0x07)
220 static void printf_btr_rcar_can(struct can_bittiming *bt, bool hdr)
223 printf("%10s", "CiBCR");
227 bcr = RCAR_CAN_BCR_TSEG1(bt->phase_seg1 + bt->prop_seg - 1) |
228 RCAR_CAN_BCR_BPR(bt->brp - 1) |
229 RCAR_CAN_BCR_SJW(bt->sjw - 1) |
230 RCAR_CAN_BCR_TSEG2(bt->phase_seg2 - 1);
232 printf("0x%08x", bcr << 8);
236 static struct calc_bittiming_const can_calc_consts[] = {
250 .printf_btr = printf_btr_sja1000,
264 .printf_btr = printf_btr_sja1000,
278 .printf_btr = printf_btr_sja1000,
292 .printf_btr = printf_btr_sja1000,
306 .printf_btr = printf_btr_sja1000,
319 .ref_clk = 66660000, /* mpc5121 */
320 .printf_btr = printf_btr_sja1000,
333 .ref_clk = 66666666, /* mpc5121 */
334 .printf_btr = printf_btr_sja1000,
347 .ref_clk = 100000000,
348 .printf_btr = printf_btr_at91,
361 /* real world clock as found on the ronetix PM9263 */
363 .printf_btr = printf_btr_at91,
376 .ref_clk = 24000000, /* mx28 */
377 .printf_btr = printf_btr_flexcan,
390 .ref_clk = 30000000, /* mx6 */
391 .printf_btr = printf_btr_flexcan,
405 .printf_btr = printf_btr_flexcan,
419 .printf_btr = printf_btr_flexcan,
433 .printf_btr = printf_btr_flexcan,
447 .printf_btr = printf_btr_flexcan,
461 .printf_btr = printf_btr_flexcan, /* vybrid */
475 .printf_btr = printf_btr_mcp251x,
489 .printf_btr = printf_btr_mcp251x,
503 .printf_btr = printf_btr_ti_hecc,
517 .printf_btr = printf_btr_rcar_can,
521 static long common_bitrates[] = {
533 #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
535 static int can_update_spt(const struct can_bittiming_const *btc,
536 int sampl_pt, int tseg, int *tseg1, int *tseg2)
538 *tseg2 = tseg + 1 - (sampl_pt * (tseg + 1)) / 1000;
539 if (*tseg2 < btc->tseg2_min)
540 *tseg2 = btc->tseg2_min;
541 if (*tseg2 > btc->tseg2_max)
542 *tseg2 = btc->tseg2_max;
543 *tseg1 = tseg - *tseg2;
544 if (*tseg1 > btc->tseg1_max) {
545 *tseg1 = btc->tseg1_max;
546 *tseg2 = tseg - *tseg1;
548 return 1000 * (tseg + 1 - *tseg2) / (tseg + 1);
551 static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt)
553 struct can_priv *priv = netdev_priv(dev);
554 const struct can_bittiming_const *btc = priv->bittiming_const;
556 long best_error = 1000000000, error = 0;
557 int best_tseg = 0, best_brp = 0, brp = 0;
558 int tsegall, tseg = 0, tseg1 = 0, tseg2 = 0;
559 int spt_error = 1000, spt = 0, sampl_pt;
562 if (!priv->bittiming_const)
565 /* Use CIA recommended sample points */
566 if (bt->sample_point) {
567 sampl_pt = bt->sample_point;
569 if (bt->bitrate > 800000)
571 else if (bt->bitrate > 500000)
577 /* tseg even = round down, odd = round up */
578 for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
579 tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
580 tsegall = 1 + tseg / 2;
581 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
582 brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
583 /* chose brp step which is possible in system */
584 brp = (brp / btc->brp_inc) * btc->brp_inc;
585 if ((brp < btc->brp_min) || (brp > btc->brp_max))
587 rate = priv->clock.freq / (brp * tsegall);
588 error = bt->bitrate - rate;
589 /* tseg brp biterror */
592 if (error > best_error)
596 spt = can_update_spt(btc, sampl_pt, tseg / 2,
598 error = sampl_pt - spt;
601 if (error > spt_error)
605 best_tseg = tseg / 2;
612 /* Error in one-tenth of a percent */
613 error = (best_error * 1000) / bt->bitrate;
614 if (error > CAN_CALC_MAX_ERROR) {
615 dev_err(dev->dev.parent,
616 "bitrate error %ld.%ld%% too high\n",
617 error / 10, error % 10);
620 dev_warn(dev->dev.parent, "bitrate error %ld.%ld%%\n",
621 error / 10, error % 10);
625 /* real sample point */
626 bt->sample_point = can_update_spt(btc, sampl_pt, best_tseg,
629 v64 = (u64)best_brp * 1000000000UL;
630 do_div(v64, priv->clock.freq);
632 bt->prop_seg = tseg1 / 2;
633 bt->phase_seg1 = tseg1 - bt->prop_seg;
634 bt->phase_seg2 = tseg2;
639 bt->bitrate = priv->clock.freq / (bt->brp * (tseg1 + tseg2 + 1));
644 static __u32 get_cia_sample_point(__u32 bitrate)
648 if (bitrate > 800000)
650 else if (bitrate > 500000)
658 static void print_bit_timing(const struct calc_bittiming_const *btc,
659 __u32 bitrate, __u32 sample_point, __u32 ref_clk,
662 struct net_device dev = {
663 .priv.bittiming_const = &btc->bittiming_const,
664 .priv.clock.freq = ref_clk,
666 struct can_bittiming bt = {
668 .sample_point = sample_point,
670 long rate_error, spt_error;
673 printf("Bit timing parameters for %s with %.6f MHz ref clock\n"
674 "nominal real Bitrt nom real SampP\n"
675 "Bitrate TQ[ns] PrS PhS1 PhS2 SJW BRP Bitrate Error SampP SampP Error ",
676 btc->bittiming_const.name,
677 ref_clk / 1000000.0);
679 btc->printf_btr(&bt, true);
683 if (can_calc_bittiming(&dev, &bt)) {
684 printf("%7d ***bitrate not possible***\n", bitrate);
688 /* get nominal sample point */
690 sample_point = get_cia_sample_point(bitrate);
692 rate_error = abs((__s32)(bitrate - bt.bitrate));
693 spt_error = abs((__s32)(sample_point - bt.sample_point));
699 "%4.1f%% %4.1f%% %4.1f%% ",
701 bt.tq, bt.prop_seg, bt.phase_seg1, bt.phase_seg2,
705 100.0 * rate_error / bitrate,
708 bt.sample_point / 10.0,
709 100.0 * spt_error / sample_point);
711 btc->printf_btr(&bt, false);
715 static void do_list(void)
719 for (i = 0; i < ARRAY_SIZE(can_calc_consts); i++)
720 printf("%s\n", can_calc_consts[i].bittiming_const.name);
723 int main(int argc, char *argv[])
726 __u32 opt_ref_clk = 0, ref_clk;
728 bool quiet = false, list = false, found = false;
733 const struct calc_bittiming_const *btc = NULL;
735 while ((opt = getopt(argc, argv, "b:c:lps:")) != -1) {
738 bitrate = atoi(optarg);
742 opt_ref_clk = atoi(optarg);
754 sampl_pt = atoi(optarg);
758 print_usage(argv[0]);
763 if (argc > optind + 1)
764 print_usage(argv[0]);
766 if (argc == optind + 1)
774 if (sampl_pt && (sampl_pt >= 1000 || sampl_pt < 100))
775 print_usage(argv[0]);
777 for (i = 0; i < ARRAY_SIZE(can_calc_consts); i++) {
778 if (name && strcmp(can_calc_consts[i].bittiming_const.name, name))
782 btc = &can_calc_consts[i];
785 ref_clk = opt_ref_clk;
787 ref_clk = btc->ref_clk;
790 print_bit_timing(btc, bitrate, sampl_pt, ref_clk, quiet);
792 for (j = 0; j < ARRAY_SIZE(common_bitrates); j++)
793 print_bit_timing(btc, common_bitrates[j],
794 sampl_pt, ref_clk, j);
800 printf("error: unknown CAN controller '%s', try one of these:\n\n", name);