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
25 #include <linux/types.h>
27 /* seems not to be defined in errno.h */
29 #define ENOTSUPP 524 /* Operation is not supported */
33 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
35 #define do_div(a,b) a = (a) / (b)
39 (__x < 0) ? -__x : __x; \
43 * clamp - return a value clamped to a given range with strict typechecking
45 * @min: minimum allowable value
46 * @max: maximum allowable value
48 * This macro does strict typechecking of min/max to make sure they are of the
49 * same type as val. See the unnecessary pointer comparisons.
51 #define clamp(val, min, max) ({ \
52 typeof(val) __val = (val); \
53 typeof(min) __min = (min); \
54 typeof(max) __max = (max); \
55 (void) (&__val == &__min); \
56 (void) (&__val == &__max); \
57 __val = __val < __min ? __min: __val; \
58 __val > __max ? __max: __val; })
60 /* we don't want to see these prints */
61 #define dev_err(dev, format, arg...) do { } while (0)
62 #define dev_warn(dev, format, arg...) do { } while (0)
64 /* define in-kernel-types */
70 * CAN bit-timing parameters
72 * For futher information, please read chapter "8 BIT TIMING
73 * REQUIREMENTS" of the "Bosch CAN Specification version 2.0"
74 * at http://www.semiconductors.bosch.de/pdf/can2spec.pdf.
76 struct can_bittiming {
77 __u32 bitrate; /* Bit-rate in bits/second */
78 __u32 sample_point; /* Sample point in one-tenth of a percent */
79 __u32 tq; /* Time quanta (TQ) in nanoseconds */
80 __u32 prop_seg; /* Propagation segment in TQs */
81 __u32 phase_seg1; /* Phase buffer segment 1 in TQs */
82 __u32 phase_seg2; /* Phase buffer segment 2 in TQs */
83 __u32 sjw; /* Synchronisation jump width in TQs */
84 __u32 brp; /* Bit-rate prescaler */
88 * CAN harware-dependent bit-timing constant
90 * Used for calculating and checking bit-timing parameters
92 struct can_bittiming_const {
93 char name[16]; /* Name of the CAN controller hardware */
94 __u32 tseg1_min; /* Time segement 1 = prop_seg + phase_seg1 */
96 __u32 tseg2_min; /* Time segement 2 = phase_seg2 */
98 __u32 sjw_max; /* Synchronisation jump width */
99 __u32 brp_min; /* Bit-rate prescaler */
103 /* added for can-calc-bit-timing utility */
104 __u32 ref_clk; /* CAN system clock frequency in Hz */
105 void (*printf_btr)(struct can_bittiming *bt, int hdr);
109 * CAN clock parameters
112 __u32 freq; /* CAN system clock frequency in Hz */
117 * minimal structs, just enough to be source level compatible
120 const struct can_bittiming_const *bittiming_const;
121 struct can_clock clock;
125 struct can_priv priv;
128 static inline void *netdev_priv(const struct net_device *dev)
130 return (void *)&dev->priv;
133 static void print_usage(char* cmd)
135 printf("Usage: %s [options] [<CAN-contoller-name>]\n"
137 "\t-q : don't print header line\n"
138 "\t-l : list all support CAN controller names\n"
139 "\t-b <bitrate> : bit-rate in bits/sec\n"
140 "\t-s <samp_pt> : sample-point in one-tenth of a percent\n"
141 "\t or 0 for CIA recommended sample points\n"
142 "\t-c <clock> : real CAN system clock in Hz\n",
148 static void printf_btr_sja1000(struct can_bittiming *bt, int hdr)
155 btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
156 btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
157 (((bt->phase_seg2 - 1) & 0x7) << 4);
158 printf("0x%02x 0x%02x", btr0, btr1);
162 static void printf_btr_at91(struct can_bittiming *bt, int hdr)
165 printf("%10s", "CAN_BR");
167 uint32_t br = ((bt->phase_seg2 - 1) |
168 ((bt->phase_seg1 - 1) << 4) |
169 ((bt->prop_seg - 1) << 8) |
170 ((bt->sjw - 1) << 12) |
171 ((bt->brp - 1) << 16));
172 printf("0x%08x", br);
176 static void printf_btr_flexcan(struct can_bittiming *bt, int hdr)
179 printf("%10s", "CAN_CTRL");
181 uint32_t ctrl = (((bt->brp - 1) << 24) |
182 ((bt->sjw - 1) << 22) |
183 ((bt->phase_seg1 - 1) << 19) |
184 ((bt->phase_seg2 - 1) << 16) |
185 ((bt->prop_seg - 1) << 0));
187 printf("0x%08x", ctrl);
191 static void printf_btr_mcp251x(struct can_bittiming *bt, int hdr)
193 uint8_t cnf1, cnf2, cnf3;
196 printf("CNF1 CNF2 CNF3");
198 cnf1 = ((bt->sjw - 1) << 6) | (bt->brp - 1);
199 cnf2 = 0x80 | ((bt->phase_seg1 - 1) << 3) | (bt->prop_seg - 1);
200 cnf3 = bt->phase_seg2 - 1;
201 printf("0x%02x 0x%02x 0x%02x", cnf1, cnf2, cnf3);
205 static void printf_btr_ti_hecc(struct can_bittiming *bt, int hdr)
208 printf("%10s", "CANBTC");
212 can_btc = (bt->phase_seg2 - 1) & 0x7;
213 can_btc |= ((bt->phase_seg1 + bt->prop_seg - 1)
215 can_btc |= ((bt->sjw - 1) & 0x3) << 8;
216 can_btc |= ((bt->brp - 1) & 0xFF) << 16;
218 printf("0x%08x", can_btc);
222 static struct can_bittiming_const can_calc_consts[] = {
235 .printf_btr = printf_btr_sja1000,
249 .printf_btr = printf_btr_sja1000,
263 .printf_btr = printf_btr_sja1000,
277 .printf_btr = printf_btr_sja1000,
291 .printf_btr = printf_btr_sja1000,
304 .ref_clk = 66660000, /* mpc5121 */
305 .printf_btr = printf_btr_sja1000,
318 .ref_clk = 66666666, /* mpc5121 */
319 .printf_btr = printf_btr_sja1000,
332 .ref_clk = 100000000,
333 .printf_btr = printf_btr_at91,
346 /* real world clock as found on the ronetix PM9263 */
348 .printf_btr = printf_btr_at91,
361 .ref_clk = 24000000, /* mx28 */
362 .printf_btr = printf_btr_flexcan,
376 .printf_btr = printf_btr_flexcan,
390 .printf_btr = printf_btr_flexcan,
404 .printf_btr = printf_btr_flexcan,
418 .printf_btr = printf_btr_flexcan,
432 .printf_btr = printf_btr_flexcan, /* vybrid */
446 .printf_btr = printf_btr_mcp251x,
460 .printf_btr = printf_btr_mcp251x,
474 .printf_btr = printf_btr_ti_hecc,
478 static long common_bitrates[] = {
490 #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
492 static int can_update_spt(const struct can_bittiming_const *btc,
493 int sampl_pt, int tseg, int *tseg1, int *tseg2)
495 *tseg2 = tseg + 1 - (sampl_pt * (tseg + 1)) / 1000;
496 if (*tseg2 < btc->tseg2_min)
497 *tseg2 = btc->tseg2_min;
498 if (*tseg2 > btc->tseg2_max)
499 *tseg2 = btc->tseg2_max;
500 *tseg1 = tseg - *tseg2;
501 if (*tseg1 > btc->tseg1_max) {
502 *tseg1 = btc->tseg1_max;
503 *tseg2 = tseg - *tseg1;
505 return 1000 * (tseg + 1 - *tseg2) / (tseg + 1);
508 static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt)
510 struct can_priv *priv = netdev_priv(dev);
511 const struct can_bittiming_const *btc = priv->bittiming_const;
513 long best_error = 1000000000, error = 0;
514 int best_tseg = 0, best_brp = 0, brp = 0;
515 int tsegall, tseg = 0, tseg1 = 0, tseg2 = 0;
516 int spt_error = 1000, spt = 0, sampl_pt;
519 if (!priv->bittiming_const)
522 /* Use CIA recommended sample points */
523 if (bt->sample_point) {
524 sampl_pt = bt->sample_point;
526 if (bt->bitrate > 800000)
528 else if (bt->bitrate > 500000)
534 /* tseg even = round down, odd = round up */
535 for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
536 tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
537 tsegall = 1 + tseg / 2;
538 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
539 brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
540 /* chose brp step which is possible in system */
541 brp = (brp / btc->brp_inc) * btc->brp_inc;
542 if ((brp < btc->brp_min) || (brp > btc->brp_max))
544 rate = priv->clock.freq / (brp * tsegall);
545 error = bt->bitrate - rate;
546 /* tseg brp biterror */
549 if (error > best_error)
553 spt = can_update_spt(btc, sampl_pt, tseg / 2,
555 error = sampl_pt - spt;
558 if (error > spt_error)
562 best_tseg = tseg / 2;
569 /* Error in one-tenth of a percent */
570 error = (best_error * 1000) / bt->bitrate;
571 if (error > CAN_CALC_MAX_ERROR) {
572 dev_err(dev->dev.parent,
573 "bitrate error %ld.%ld%% too high\n",
574 error / 10, error % 10);
577 dev_warn(dev->dev.parent, "bitrate error %ld.%ld%%\n",
578 error / 10, error % 10);
582 /* real sample point */
583 bt->sample_point = can_update_spt(btc, sampl_pt, best_tseg,
586 v64 = (u64)best_brp * 1000000000UL;
587 do_div(v64, priv->clock.freq);
589 bt->prop_seg = tseg1 / 2;
590 bt->phase_seg1 = tseg1 - bt->prop_seg;
591 bt->phase_seg2 = tseg2;
596 bt->bitrate = priv->clock.freq / (bt->brp * (tseg1 + tseg2 + 1));
601 static __u32 get_cia_sample_point(__u32 bitrate)
605 if (bitrate > 800000)
607 else if (bitrate > 500000)
615 static void print_bit_timing(const struct can_bittiming_const *btc,
616 __u32 bitrate, __u32 sample_point, __u32 ref_clk,
619 struct net_device dev = {
620 .priv.bittiming_const = btc,
621 .priv.clock.freq = ref_clk,
623 struct can_bittiming bt = {
625 .sample_point = sample_point,
627 long rate_error, spt_error;
630 printf("Bit timing parameters for %s with %.6f MHz ref clock\n"
631 "nominal real Bitrt nom real SampP\n"
632 "Bitrate TQ[ns] PrS PhS1 PhS2 SJW BRP Bitrate Error SampP SampP Error ",
634 ref_clk / 1000000.0);
636 btc->printf_btr(&bt, 1);
640 if (can_calc_bittiming(&dev, &bt)) {
641 printf("%7d ***bitrate not possible***\n", bitrate);
645 /* get nominal sample point */
647 sample_point = get_cia_sample_point(bitrate);
649 rate_error = abs((__s32)(bitrate - bt.bitrate));
650 spt_error = abs((__s32)(sample_point - bt.sample_point));
656 "%4.1f%% %4.1f%% %4.1f%% ",
658 bt.tq, bt.prop_seg, bt.phase_seg1, bt.phase_seg2,
662 100.0 * rate_error / bitrate,
665 bt.sample_point / 10.0,
666 100.0 * spt_error / sample_point);
668 btc->printf_btr(&bt, 0);
672 static void do_list(void)
676 for (i = 0; i < ARRAY_SIZE(can_calc_consts); i++)
677 printf("%s\n", can_calc_consts[i].name);
680 int main(int argc, char *argv[])
683 __u32 opt_ref_clk = 0, ref_clk;
691 const struct can_bittiming_const *btc = NULL;
693 while ((opt = getopt(argc, argv, "b:c:lps:")) != -1) {
696 bitrate = atoi(optarg);
700 opt_ref_clk = atoi(optarg);
712 sampl_pt = atoi(optarg);
716 print_usage(argv[0]);
721 if (argc > optind + 1)
722 print_usage(argv[0]);
724 if (argc == optind + 1)
732 if (sampl_pt && (sampl_pt >= 1000 || sampl_pt < 100))
733 print_usage(argv[0]);
735 for (i = 0; i < ARRAY_SIZE(can_calc_consts); i++) {
736 if (name && strcmp(can_calc_consts[i].name, name))
740 btc = &can_calc_consts[i];
743 ref_clk = opt_ref_clk;
745 ref_clk = btc->ref_clk;
748 print_bit_timing(btc, bitrate, sampl_pt, ref_clk, quiet);
750 for (j = 0; j < ARRAY_SIZE(common_bitrates); j++)
751 print_bit_timing(btc, common_bitrates[j],
752 sampl_pt, ref_clk, j);
758 printf("error: unknown CAN controller '%s', try one of these:\n\n", name);