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 /* seems not to be defined in errno.h */
31 #define ENOTSUPP 524 /* Operation is not supported */
35 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
37 #define do_div(a,b) a = (a) / (b)
41 (__x < 0) ? -__x : __x; \
45 * clamp - return a value clamped to a given range with strict typechecking
47 * @min: minimum allowable value
48 * @max: maximum allowable value
50 * This macro does strict typechecking of min/max to make sure they are of the
51 * same type as val. See the unnecessary pointer comparisons.
53 #define clamp(val, min, max) ({ \
54 typeof(val) __val = (val); \
55 typeof(min) __min = (min); \
56 typeof(max) __max = (max); \
57 (void) (&__val == &__min); \
58 (void) (&__val == &__max); \
59 __val = __val < __min ? __min: __val; \
60 __val > __max ? __max: __val; })
62 /* we don't want to see these prints */
63 #define dev_err(dev, format, arg...) do { } while (0)
64 #define dev_warn(dev, format, arg...) do { } while (0)
66 /* define in-kernel-types */
70 struct calc_bittiming_const {
71 struct can_bittiming_const bittiming_const;
73 __u32 ref_clk; /* CAN system clock frequency in Hz */
74 void (*printf_btr)(struct can_bittiming *bt, bool hdr);
78 * minimal structs, just enough to be source level compatible
81 const struct can_bittiming_const *bittiming_const;
82 struct can_clock clock;
89 static inline void *netdev_priv(const struct net_device *dev)
91 return (void *)&dev->priv;
94 static void print_usage(char *cmd)
96 printf("Usage: %s [options] [<CAN-contoller-name>]\n"
98 "\t-q : don't print header line\n"
99 "\t-l : list all support CAN controller names\n"
100 "\t-b <bitrate> : bit-rate in bits/sec\n"
101 "\t-s <samp_pt> : sample-point in one-tenth of a percent\n"
102 "\t or 0 for CIA recommended sample points\n"
103 "\t-c <clock> : real CAN system clock in Hz\n",
109 static void printf_btr_sja1000(struct can_bittiming *bt, bool hdr)
116 btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
117 btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
118 (((bt->phase_seg2 - 1) & 0x7) << 4);
119 printf("0x%02x 0x%02x", btr0, btr1);
123 static void printf_btr_at91(struct can_bittiming *bt, bool hdr)
126 printf("%10s", "CAN_BR");
128 uint32_t br = ((bt->phase_seg2 - 1) |
129 ((bt->phase_seg1 - 1) << 4) |
130 ((bt->prop_seg - 1) << 8) |
131 ((bt->sjw - 1) << 12) |
132 ((bt->brp - 1) << 16));
133 printf("0x%08x", br);
137 static void printf_btr_flexcan(struct can_bittiming *bt, bool hdr)
140 printf("%10s", "CAN_CTRL");
142 uint32_t ctrl = (((bt->brp - 1) << 24) |
143 ((bt->sjw - 1) << 22) |
144 ((bt->phase_seg1 - 1) << 19) |
145 ((bt->phase_seg2 - 1) << 16) |
146 ((bt->prop_seg - 1) << 0));
148 printf("0x%08x", ctrl);
152 static void printf_btr_mcp251x(struct can_bittiming *bt, bool hdr)
154 uint8_t cnf1, cnf2, cnf3;
157 printf("CNF1 CNF2 CNF3");
159 cnf1 = ((bt->sjw - 1) << 6) | (bt->brp - 1);
160 cnf2 = 0x80 | ((bt->phase_seg1 - 1) << 3) | (bt->prop_seg - 1);
161 cnf3 = bt->phase_seg2 - 1;
162 printf("0x%02x 0x%02x 0x%02x", cnf1, cnf2, cnf3);
166 static void printf_btr_ti_hecc(struct can_bittiming *bt, bool hdr)
169 printf("%10s", "CANBTC");
173 can_btc = (bt->phase_seg2 - 1) & 0x7;
174 can_btc |= ((bt->phase_seg1 + bt->prop_seg - 1)
176 can_btc |= ((bt->sjw - 1) & 0x3) << 8;
177 can_btc |= ((bt->brp - 1) & 0xFF) << 16;
179 printf("0x%08x", can_btc);
183 #define RCAR_CAN_BCR_TSEG1(x) (((x) & 0x0f) << 20)
184 #define RCAR_CAN_BCR_BPR(x) (((x) & 0x3ff) << 8)
185 #define RCAR_CAN_BCR_SJW(x) (((x) & 0x3) << 4)
186 #define RCAR_CAN_BCR_TSEG2(x) ((x) & 0x07)
188 static void printf_btr_rcar_can(struct can_bittiming *bt, bool hdr)
191 printf("%10s", "CiBCR");
195 bcr = RCAR_CAN_BCR_TSEG1(bt->phase_seg1 + bt->prop_seg - 1) |
196 RCAR_CAN_BCR_BPR(bt->brp - 1) |
197 RCAR_CAN_BCR_SJW(bt->sjw - 1) |
198 RCAR_CAN_BCR_TSEG2(bt->phase_seg2 - 1);
200 printf("0x%08x", bcr << 8);
204 static struct calc_bittiming_const can_calc_consts[] = {
218 .printf_btr = printf_btr_sja1000,
232 .printf_btr = printf_btr_sja1000,
246 .printf_btr = printf_btr_sja1000,
260 .printf_btr = printf_btr_sja1000,
274 .printf_btr = printf_btr_sja1000,
287 .ref_clk = 66660000, /* mpc5121 */
288 .printf_btr = printf_btr_sja1000,
301 .ref_clk = 66666666, /* mpc5121 */
302 .printf_btr = printf_btr_sja1000,
315 .ref_clk = 100000000,
316 .printf_btr = printf_btr_at91,
329 /* real world clock as found on the ronetix PM9263 */
331 .printf_btr = printf_btr_at91,
344 .ref_clk = 24000000, /* mx28 */
345 .printf_btr = printf_btr_flexcan,
358 .ref_clk = 30000000, /* mx6 */
359 .printf_btr = printf_btr_flexcan,
373 .printf_btr = printf_btr_flexcan,
387 .printf_btr = printf_btr_flexcan,
401 .printf_btr = printf_btr_flexcan,
415 .printf_btr = printf_btr_flexcan,
429 .printf_btr = printf_btr_flexcan, /* vybrid */
443 .printf_btr = printf_btr_mcp251x,
457 .printf_btr = printf_btr_mcp251x,
471 .printf_btr = printf_btr_ti_hecc,
485 .printf_btr = printf_btr_rcar_can,
489 static long common_bitrates[] = {
501 #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
503 static int can_update_spt(const struct can_bittiming_const *btc,
504 int sampl_pt, int tseg, int *tseg1, int *tseg2)
506 *tseg2 = tseg + 1 - (sampl_pt * (tseg + 1)) / 1000;
507 if (*tseg2 < btc->tseg2_min)
508 *tseg2 = btc->tseg2_min;
509 if (*tseg2 > btc->tseg2_max)
510 *tseg2 = btc->tseg2_max;
511 *tseg1 = tseg - *tseg2;
512 if (*tseg1 > btc->tseg1_max) {
513 *tseg1 = btc->tseg1_max;
514 *tseg2 = tseg - *tseg1;
516 return 1000 * (tseg + 1 - *tseg2) / (tseg + 1);
519 static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt)
521 struct can_priv *priv = netdev_priv(dev);
522 const struct can_bittiming_const *btc = priv->bittiming_const;
524 long best_error = 1000000000, error = 0;
525 int best_tseg = 0, best_brp = 0, brp = 0;
526 int tsegall, tseg = 0, tseg1 = 0, tseg2 = 0;
527 int spt_error = 1000, spt = 0, sampl_pt;
530 if (!priv->bittiming_const)
533 /* Use CIA recommended sample points */
534 if (bt->sample_point) {
535 sampl_pt = bt->sample_point;
537 if (bt->bitrate > 800000)
539 else if (bt->bitrate > 500000)
545 /* tseg even = round down, odd = round up */
546 for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
547 tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
548 tsegall = 1 + tseg / 2;
549 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
550 brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
551 /* chose brp step which is possible in system */
552 brp = (brp / btc->brp_inc) * btc->brp_inc;
553 if ((brp < btc->brp_min) || (brp > btc->brp_max))
555 rate = priv->clock.freq / (brp * tsegall);
556 error = bt->bitrate - rate;
557 /* tseg brp biterror */
560 if (error > best_error)
564 spt = can_update_spt(btc, sampl_pt, tseg / 2,
566 error = sampl_pt - spt;
569 if (error > spt_error)
573 best_tseg = tseg / 2;
580 /* Error in one-tenth of a percent */
581 error = (best_error * 1000) / bt->bitrate;
582 if (error > CAN_CALC_MAX_ERROR) {
583 dev_err(dev->dev.parent,
584 "bitrate error %ld.%ld%% too high\n",
585 error / 10, error % 10);
588 dev_warn(dev->dev.parent, "bitrate error %ld.%ld%%\n",
589 error / 10, error % 10);
593 /* real sample point */
594 bt->sample_point = can_update_spt(btc, sampl_pt, best_tseg,
597 v64 = (u64)best_brp * 1000000000UL;
598 do_div(v64, priv->clock.freq);
600 bt->prop_seg = tseg1 / 2;
601 bt->phase_seg1 = tseg1 - bt->prop_seg;
602 bt->phase_seg2 = tseg2;
607 bt->bitrate = priv->clock.freq / (bt->brp * (tseg1 + tseg2 + 1));
612 static __u32 get_cia_sample_point(__u32 bitrate)
616 if (bitrate > 800000)
618 else if (bitrate > 500000)
626 static void print_bit_timing(const struct calc_bittiming_const *btc,
627 __u32 bitrate, __u32 sample_point, __u32 ref_clk,
630 struct net_device dev = {
631 .priv.bittiming_const = &btc->bittiming_const,
632 .priv.clock.freq = ref_clk,
634 struct can_bittiming bt = {
636 .sample_point = sample_point,
638 long rate_error, spt_error;
641 printf("Bit timing parameters for %s with %.6f MHz ref clock\n"
642 "nominal real Bitrt nom real SampP\n"
643 "Bitrate TQ[ns] PrS PhS1 PhS2 SJW BRP Bitrate Error SampP SampP Error ",
644 btc->bittiming_const.name,
645 ref_clk / 1000000.0);
647 btc->printf_btr(&bt, true);
651 if (can_calc_bittiming(&dev, &bt)) {
652 printf("%7d ***bitrate not possible***\n", bitrate);
656 /* get nominal sample point */
658 sample_point = get_cia_sample_point(bitrate);
660 rate_error = abs((__s32)(bitrate - bt.bitrate));
661 spt_error = abs((__s32)(sample_point - bt.sample_point));
667 "%4.1f%% %4.1f%% %4.1f%% ",
669 bt.tq, bt.prop_seg, bt.phase_seg1, bt.phase_seg2,
673 100.0 * rate_error / bitrate,
676 bt.sample_point / 10.0,
677 100.0 * spt_error / sample_point);
679 btc->printf_btr(&bt, false);
683 static void do_list(void)
687 for (i = 0; i < ARRAY_SIZE(can_calc_consts); i++)
688 printf("%s\n", can_calc_consts[i].bittiming_const.name);
691 int main(int argc, char *argv[])
694 __u32 opt_ref_clk = 0, ref_clk;
696 bool quiet = false, list = false, found = false;
701 const struct calc_bittiming_const *btc = NULL;
703 while ((opt = getopt(argc, argv, "b:c:lps:")) != -1) {
706 bitrate = atoi(optarg);
710 opt_ref_clk = atoi(optarg);
722 sampl_pt = atoi(optarg);
726 print_usage(argv[0]);
731 if (argc > optind + 1)
732 print_usage(argv[0]);
734 if (argc == optind + 1)
742 if (sampl_pt && (sampl_pt >= 1000 || sampl_pt < 100))
743 print_usage(argv[0]);
745 for (i = 0; i < ARRAY_SIZE(can_calc_consts); i++) {
746 if (name && strcmp(can_calc_consts[i].bittiming_const.name, name))
750 btc = &can_calc_consts[i];
753 ref_clk = opt_ref_clk;
755 ref_clk = btc->ref_clk;
758 print_bit_timing(btc, bitrate, sampl_pt, ref_clk, quiet);
760 for (j = 0; j < ARRAY_SIZE(common_bitrates); j++)
761 print_bit_timing(btc, common_bitrates[j],
762 sampl_pt, ref_clk, j);
768 printf("error: unknown CAN controller '%s', try one of these:\n\n", name);