]> rtime.felk.cvut.cz Git - can-utils.git/blob - can-calc-bit-timing.c
834093818e1aa47e72f53ca806572c16afb8ddfe
[can-utils.git] / can-calc-bit-timing.c
1 /* can-calc-bit-timing.c: Calculate CAN bit timing parameters
2  *
3  * Copyright (C) 2008 Wolfgang Grandegger <wg@grandegger.com>
4  *
5  * Derived from:
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
11  *
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
15  * any later version.
16  */
17
18 #include <errno.h>
19 #include <getopt.h>
20 #include <stdbool.h>
21 #include <stdint.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <linux/types.h>
27
28 /* seems not to be defined in errno.h */
29 #ifndef ENOTSUPP
30 #define ENOTSUPP        524     /* Operation is not supported */
31 #endif
32
33 /* useful defines */
34 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
35
36 #define do_div(a,b) a = (a) / (b)
37
38 #define abs(x) ({                               \
39                 long __x = (x);                 \
40                 (__x < 0) ? -__x : __x;         \
41         })
42
43 /**
44  * clamp - return a value clamped to a given range with strict typechecking
45  * @val: current value
46  * @min: minimum allowable value
47  * @max: maximum allowable value
48  *
49  * This macro does strict typechecking of min/max to make sure they are of the
50  * same type as val.  See the unnecessary pointer comparisons.
51  */
52 #define clamp(val, min, max) ({                 \
53         typeof(val) __val = (val);              \
54         typeof(min) __min = (min);              \
55         typeof(max) __max = (max);              \
56         (void) (&__val == &__min);              \
57         (void) (&__val == &__max);              \
58         __val = __val < __min ? __min: __val;   \
59         __val > __max ? __max: __val; })
60
61 /* we don't want to see these prints */
62 #define dev_err(dev, format, arg...)    do { } while (0)
63 #define dev_warn(dev, format, arg...)   do { } while (0)
64
65 /* define in-kernel-types */
66 typedef __u64 u64;
67 typedef __u32 u32;
68
69
70 /*
71  * CAN bit-timing parameters
72  *
73  * For futher information, please read chapter "8 BIT TIMING
74  * REQUIREMENTS" of the "Bosch CAN Specification version 2.0"
75  * at http://www.semiconductors.bosch.de/pdf/can2spec.pdf.
76  */
77 struct can_bittiming {
78         __u32 bitrate;          /* Bit-rate in bits/second */
79         __u32 sample_point;     /* Sample point in one-tenth of a percent */
80         __u32 tq;               /* Time quanta (TQ) in nanoseconds */
81         __u32 prop_seg;         /* Propagation segment in TQs */
82         __u32 phase_seg1;       /* Phase buffer segment 1 in TQs */
83         __u32 phase_seg2;       /* Phase buffer segment 2 in TQs */
84         __u32 sjw;              /* Synchronisation jump width in TQs */
85         __u32 brp;              /* Bit-rate prescaler */
86 };
87
88 /*
89  * CAN harware-dependent bit-timing constant
90  *
91  * Used for calculating and checking bit-timing parameters
92  */
93 struct can_bittiming_const {
94         char name[16];          /* Name of the CAN controller hardware */
95         __u32 tseg1_min;        /* Time segement 1 = prop_seg + phase_seg1 */
96         __u32 tseg1_max;
97         __u32 tseg2_min;        /* Time segement 2 = phase_seg2 */
98         __u32 tseg2_max;
99         __u32 sjw_max;          /* Synchronisation jump width */
100         __u32 brp_min;          /* Bit-rate prescaler */
101         __u32 brp_max;
102         __u32 brp_inc;
103
104         /* added for can-calc-bit-timing utility */
105         __u32 ref_clk;          /* CAN system clock frequency in Hz */
106         void (*printf_btr)(struct can_bittiming *bt, bool hdr);
107 };
108
109 /*
110  * CAN clock parameters
111  */
112 struct can_clock {
113         __u32 freq;             /* CAN system clock frequency in Hz */
114 };
115
116
117 /*
118  * minimal structs, just enough to be source level compatible
119  */
120 struct can_priv {
121         const struct can_bittiming_const *bittiming_const;
122         struct can_clock clock;
123 };
124
125 struct net_device {
126         struct can_priv priv;
127 };
128
129 static inline void *netdev_priv(const struct net_device *dev)
130 {
131         return (void *)&dev->priv;
132 }
133
134 static void print_usage(char *cmd)
135 {
136         printf("Usage: %s [options] [<CAN-contoller-name>]\n"
137                "\tOptions:\n"
138                "\t-q           : don't print header line\n"
139                "\t-l           : list all support CAN controller names\n"
140                "\t-b <bitrate> : bit-rate in bits/sec\n"
141                "\t-s <samp_pt> : sample-point in one-tenth of a percent\n"
142                "\t               or 0 for CIA recommended sample points\n"
143                "\t-c <clock>   : real CAN system clock in Hz\n",
144                cmd);
145
146         exit(EXIT_FAILURE);
147 }
148
149 static void printf_btr_sja1000(struct can_bittiming *bt, bool hdr)
150 {
151         uint8_t btr0, btr1;
152
153         if (hdr) {
154                 printf("BTR0 BTR1");
155         } else {
156                 btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
157                 btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
158                         (((bt->phase_seg2 - 1) & 0x7) << 4);
159                 printf("0x%02x 0x%02x", btr0, btr1);
160         }
161 }
162
163 static void printf_btr_at91(struct can_bittiming *bt, bool hdr)
164 {
165         if (hdr) {
166                 printf("%10s", "CAN_BR");
167         } else {
168                 uint32_t br = ((bt->phase_seg2 - 1) |
169                                ((bt->phase_seg1 - 1) << 4) |
170                                ((bt->prop_seg - 1) << 8) |
171                                ((bt->sjw - 1) << 12) |
172                                ((bt->brp - 1) << 16));
173                 printf("0x%08x", br);
174         }
175 }
176
177 static void printf_btr_flexcan(struct can_bittiming *bt, bool hdr)
178 {
179         if (hdr) {
180                 printf("%10s", "CAN_CTRL");
181         } else {
182                 uint32_t ctrl = (((bt->brp        - 1) << 24) |
183                                  ((bt->sjw        - 1) << 22) |
184                                  ((bt->phase_seg1 - 1) << 19) |
185                                  ((bt->phase_seg2 - 1) << 16) |
186                                  ((bt->prop_seg   - 1) <<  0));
187
188                 printf("0x%08x", ctrl);
189         }
190 }
191
192 static void printf_btr_mcp251x(struct can_bittiming *bt, bool hdr)
193 {
194         uint8_t cnf1, cnf2, cnf3;
195
196         if (hdr) {
197                 printf("CNF1 CNF2 CNF3");
198         } else {
199                 cnf1 = ((bt->sjw - 1) << 6) | (bt->brp - 1);
200                 cnf2 = 0x80 | ((bt->phase_seg1 - 1) << 3) | (bt->prop_seg - 1);
201                 cnf3 = bt->phase_seg2 - 1;
202                 printf("0x%02x 0x%02x 0x%02x", cnf1, cnf2, cnf3);
203         }
204 }
205
206 static void printf_btr_ti_hecc(struct can_bittiming *bt, bool hdr)
207 {
208         if (hdr) {
209                 printf("%10s", "CANBTC");
210         } else {
211                 uint32_t can_btc;
212
213                 can_btc = (bt->phase_seg2 - 1) & 0x7;
214                 can_btc |= ((bt->phase_seg1 + bt->prop_seg - 1)
215                             & 0xF) << 3;
216                 can_btc |= ((bt->sjw - 1) & 0x3) << 8;
217                 can_btc |= ((bt->brp - 1) & 0xFF) << 16;
218
219                 printf("0x%08x", can_btc);
220         }
221 }
222
223 #define RCAR_CAN_BCR_TSEG1(x)   (((x) & 0x0f) << 20)
224 #define RCAR_CAN_BCR_BPR(x)     (((x) & 0x3ff) << 8)
225 #define RCAR_CAN_BCR_SJW(x)     (((x) & 0x3) << 4)
226 #define RCAR_CAN_BCR_TSEG2(x)   ((x) & 0x07)
227
228 static void printf_btr_rcar_can(struct can_bittiming *bt, bool hdr)
229 {
230         if (hdr) {
231                 printf("%10s", "CiBCR");
232         } else {
233                 uint32_t bcr;
234
235                 bcr = RCAR_CAN_BCR_TSEG1(bt->phase_seg1 + bt->prop_seg - 1) |
236                         RCAR_CAN_BCR_BPR(bt->brp - 1) |
237                         RCAR_CAN_BCR_SJW(bt->sjw - 1) |
238                         RCAR_CAN_BCR_TSEG2(bt->phase_seg2 - 1);
239
240                 printf("0x%08x", bcr << 8);
241         }
242 }
243
244 static struct can_bittiming_const can_calc_consts[] = {
245         {
246                 .name = "sja1000",
247                 .tseg1_min = 1,
248                 .tseg1_max = 16,
249                 .tseg2_min = 1,
250                 .tseg2_max = 8,
251                 .sjw_max = 4,
252                 .brp_min = 1,
253                 .brp_max = 64,
254                 .brp_inc = 1,
255
256                 .ref_clk = 8000000,
257                 .printf_btr = printf_btr_sja1000,
258         },
259         {
260                 .name = "mscan",
261                 .tseg1_min = 4,
262                 .tseg1_max = 16,
263                 .tseg2_min = 2,
264                 .tseg2_max = 8,
265                 .sjw_max = 4,
266                 .brp_min = 1,
267                 .brp_max = 64,
268                 .brp_inc = 1,
269
270                 .ref_clk = 32000000,
271                 .printf_btr = printf_btr_sja1000,
272         },
273         {
274                 .name = "mscan",
275                 .tseg1_min = 4,
276                 .tseg1_max = 16,
277                 .tseg2_min = 2,
278                 .tseg2_max = 8,
279                 .sjw_max = 4,
280                 .brp_min = 1,
281                 .brp_max = 64,
282                 .brp_inc = 1,
283
284                 .ref_clk = 33000000,
285                 .printf_btr = printf_btr_sja1000,
286         },
287         {
288                 .name = "mscan",
289                 .tseg1_min = 4,
290                 .tseg1_max = 16,
291                 .tseg2_min = 2,
292                 .tseg2_max = 8,
293                 .sjw_max = 4,
294                 .brp_min = 1,
295                 .brp_max = 64,
296                 .brp_inc = 1,
297
298                 .ref_clk = 33300000,
299                 .printf_btr = printf_btr_sja1000,
300         },
301         {
302                 .name = "mscan",
303                 .tseg1_min = 4,
304                 .tseg1_max = 16,
305                 .tseg2_min = 2,
306                 .tseg2_max = 8,
307                 .sjw_max = 4,
308                 .brp_min = 1,
309                 .brp_max = 64,
310                 .brp_inc = 1,
311
312                 .ref_clk = 33333333,
313                 .printf_btr = printf_btr_sja1000,
314         },
315         {
316                 .name = "mscan",
317                 .tseg1_min = 4,
318                 .tseg1_max = 16,
319                 .tseg2_min = 2,
320                 .tseg2_max = 8,
321                 .sjw_max = 4,
322                 .brp_min = 1,
323                 .brp_max = 64,
324                 .brp_inc = 1,
325
326                 .ref_clk = 66660000,    /* mpc5121 */
327                 .printf_btr = printf_btr_sja1000,
328         },
329         {
330                 .name = "mscan",
331                 .tseg1_min = 4,
332                 .tseg1_max = 16,
333                 .tseg2_min = 2,
334                 .tseg2_max = 8,
335                 .sjw_max = 4,
336                 .brp_min = 1,
337                 .brp_max = 64,
338                 .brp_inc = 1,
339
340                 .ref_clk = 66666666,    /* mpc5121 */
341                 .printf_btr = printf_btr_sja1000,
342         },
343         {
344                 .name = "at91",
345                 .tseg1_min = 4,
346                 .tseg1_max = 16,
347                 .tseg2_min = 2,
348                 .tseg2_max = 8,
349                 .sjw_max = 4,
350                 .brp_min = 2,
351                 .brp_max = 128,
352                 .brp_inc = 1,
353
354                 .ref_clk = 100000000,
355                 .printf_btr = printf_btr_at91,
356         },
357         {
358                 .name = "at91",
359                 .tseg1_min = 4,
360                 .tseg1_max = 16,
361                 .tseg2_min = 2,
362                 .tseg2_max = 8,
363                 .sjw_max = 4,
364                 .brp_min = 2,
365                 .brp_max = 128,
366                 .brp_inc = 1,
367
368                 /* real world clock as found on the ronetix PM9263 */
369                 .ref_clk = 99532800,
370                 .printf_btr = printf_btr_at91,
371         },
372         {
373                 .name = "flexcan",
374                 .tseg1_min = 4,
375                 .tseg1_max = 16,
376                 .tseg2_min = 2,
377                 .tseg2_max = 8,
378                 .sjw_max = 4,
379                 .brp_min = 1,
380                 .brp_max = 256,
381                 .brp_inc = 1,
382
383                 .ref_clk = 24000000,    /* mx28 */
384                 .printf_btr = printf_btr_flexcan,
385         },
386         {
387                 .name = "flexcan",
388                 .tseg1_min = 4,
389                 .tseg1_max = 16,
390                 .tseg2_min = 2,
391                 .tseg2_max = 8,
392                 .sjw_max = 4,
393                 .brp_min = 1,
394                 .brp_max = 256,
395                 .brp_inc = 1,
396
397                 .ref_clk = 30000000,    /* mx6 */
398                 .printf_btr = printf_btr_flexcan,
399         },
400         {
401                 .name = "flexcan",
402                 .tseg1_min = 4,
403                 .tseg1_max = 16,
404                 .tseg2_min = 2,
405                 .tseg2_max = 8,
406                 .sjw_max = 4,
407                 .brp_min = 1,
408                 .brp_max = 256,
409                 .brp_inc = 1,
410
411                 .ref_clk = 49875000,
412                 .printf_btr = printf_btr_flexcan,
413         },
414         {
415                 .name = "flexcan",
416                 .tseg1_min = 4,
417                 .tseg1_max = 16,
418                 .tseg2_min = 2,
419                 .tseg2_max = 8,
420                 .sjw_max = 4,
421                 .brp_min = 1,
422                 .brp_max = 256,
423                 .brp_inc = 1,
424
425                 .ref_clk = 66000000,
426                 .printf_btr = printf_btr_flexcan,
427         },
428         {
429                 .name = "flexcan",
430                 .tseg1_min = 4,
431                 .tseg1_max = 16,
432                 .tseg2_min = 2,
433                 .tseg2_max = 8,
434                 .sjw_max = 4,
435                 .brp_min = 1,
436                 .brp_max = 256,
437                 .brp_inc = 1,
438
439                 .ref_clk = 66500000,
440                 .printf_btr = printf_btr_flexcan,
441         },
442         {
443                 .name = "flexcan",
444                 .tseg1_min = 4,
445                 .tseg1_max = 16,
446                 .tseg2_min = 2,
447                 .tseg2_max = 8,
448                 .sjw_max = 4,
449                 .brp_min = 1,
450                 .brp_max = 256,
451                 .brp_inc = 1,
452
453                 .ref_clk = 66666666,
454                 .printf_btr = printf_btr_flexcan,
455         },
456         {
457                 .name = "flexcan",
458                 .tseg1_min = 4,
459                 .tseg1_max = 16,
460                 .tseg2_min = 2,
461                 .tseg2_max = 8,
462                 .sjw_max = 4,
463                 .brp_min = 1,
464                 .brp_max = 256,
465                 .brp_inc = 1,
466
467                 .ref_clk = 83368421,
468                 .printf_btr = printf_btr_flexcan, /* vybrid */
469         },
470         {
471                 .name = "mcp251x",
472                 .tseg1_min = 3,
473                 .tseg1_max = 16,
474                 .tseg2_min = 2,
475                 .tseg2_max = 8,
476                 .sjw_max = 4,
477                 .brp_min = 1,
478                 .brp_max = 64,
479                 .brp_inc = 1,
480
481                 .ref_clk = 8000000,
482                 .printf_btr = printf_btr_mcp251x,
483         },
484         {
485                 .name = "mcp251x",
486                 .tseg1_min = 3,
487                 .tseg1_max = 16,
488                 .tseg2_min = 2,
489                 .tseg2_max = 8,
490                 .sjw_max = 4,
491                 .brp_min = 1,
492                 .brp_max = 64,
493                 .brp_inc = 1,
494
495                 .ref_clk = 16000000,
496                 .printf_btr = printf_btr_mcp251x,
497         },
498         {
499                 .name = "ti_hecc",
500                 .tseg1_min = 1,
501                 .tseg1_max = 16,
502                 .tseg2_min = 1,
503                 .tseg2_max = 8,
504                 .sjw_max = 4,
505                 .brp_min = 1,
506                 .brp_max = 256,
507                 .brp_inc = 1,
508
509                 .ref_clk = 13000000,
510                 .printf_btr = printf_btr_ti_hecc,
511         },
512         {
513                 .name = "rcar_can",
514                 .tseg1_min = 4,
515                 .tseg1_max = 16,
516                 .tseg2_min = 2,
517                 .tseg2_max = 8,
518                 .sjw_max = 4,
519                 .brp_min = 1,
520                 .brp_max = 1024,
521                 .brp_inc = 1,
522
523                 .ref_clk = 65000000,
524                 .printf_btr = printf_btr_rcar_can,
525         },
526 };
527
528 static long common_bitrates[] = {
529         1000000,
530         800000,
531         500000,
532         250000,
533         125000,
534         100000,
535         50000,
536         20000,
537         10000,
538 };
539
540 #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
541
542 static int can_update_spt(const struct can_bittiming_const *btc,
543                           int sampl_pt, int tseg, int *tseg1, int *tseg2)
544 {
545         *tseg2 = tseg + 1 - (sampl_pt * (tseg + 1)) / 1000;
546         if (*tseg2 < btc->tseg2_min)
547                 *tseg2 = btc->tseg2_min;
548         if (*tseg2 > btc->tseg2_max)
549                 *tseg2 = btc->tseg2_max;
550         *tseg1 = tseg - *tseg2;
551         if (*tseg1 > btc->tseg1_max) {
552                 *tseg1 = btc->tseg1_max;
553                 *tseg2 = tseg - *tseg1;
554         }
555         return 1000 * (tseg + 1 - *tseg2) / (tseg + 1);
556 }
557
558 static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt)
559 {
560         struct can_priv *priv = netdev_priv(dev);
561         const struct can_bittiming_const *btc = priv->bittiming_const;
562         long rate = 0;
563         long best_error = 1000000000, error = 0;
564         int best_tseg = 0, best_brp = 0, brp = 0;
565         int tsegall, tseg = 0, tseg1 = 0, tseg2 = 0;
566         int spt_error = 1000, spt = 0, sampl_pt;
567         u64 v64;
568
569         if (!priv->bittiming_const)
570                 return -ENOTSUPP;
571
572         /* Use CIA recommended sample points */
573         if (bt->sample_point) {
574                 sampl_pt = bt->sample_point;
575         } else {
576                 if (bt->bitrate > 800000)
577                         sampl_pt = 750;
578                 else if (bt->bitrate > 500000)
579                         sampl_pt = 800;
580                 else
581                         sampl_pt = 875;
582         }
583
584         /* tseg even = round down, odd = round up */
585         for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
586              tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
587                 tsegall = 1 + tseg / 2;
588                 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
589                 brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
590                 /* chose brp step which is possible in system */
591                 brp = (brp / btc->brp_inc) * btc->brp_inc;
592                 if ((brp < btc->brp_min) || (brp > btc->brp_max))
593                         continue;
594                 rate = priv->clock.freq / (brp * tsegall);
595                 error = bt->bitrate - rate;
596                 /* tseg brp biterror */
597                 if (error < 0)
598                         error = -error;
599                 if (error > best_error)
600                         continue;
601                 best_error = error;
602                 if (error == 0) {
603                         spt = can_update_spt(btc, sampl_pt, tseg / 2,
604                                              &tseg1, &tseg2);
605                         error = sampl_pt - spt;
606                         if (error < 0)
607                                 error = -error;
608                         if (error > spt_error)
609                                 continue;
610                         spt_error = error;
611                 }
612                 best_tseg = tseg / 2;
613                 best_brp = brp;
614                 if (error == 0)
615                         break;
616         }
617
618         if (best_error) {
619                 /* Error in one-tenth of a percent */
620                 error = (best_error * 1000) / bt->bitrate;
621                 if (error > CAN_CALC_MAX_ERROR) {
622                         dev_err(dev->dev.parent,
623                                 "bitrate error %ld.%ld%% too high\n",
624                                 error / 10, error % 10);
625                         return -EDOM;
626                 } else {
627                         dev_warn(dev->dev.parent, "bitrate error %ld.%ld%%\n",
628                                  error / 10, error % 10);
629                 }
630         }
631
632         /* real sample point */
633         bt->sample_point = can_update_spt(btc, sampl_pt, best_tseg,
634                                           &tseg1, &tseg2);
635
636         v64 = (u64)best_brp * 1000000000UL;
637         do_div(v64, priv->clock.freq);
638         bt->tq = (u32)v64;
639         bt->prop_seg = tseg1 / 2;
640         bt->phase_seg1 = tseg1 - bt->prop_seg;
641         bt->phase_seg2 = tseg2;
642         bt->sjw = 1;
643         bt->brp = best_brp;
644
645         /* real bit-rate */
646         bt->bitrate = priv->clock.freq / (bt->brp * (tseg1 + tseg2 + 1));
647
648         return 0;
649 }
650
651 static __u32 get_cia_sample_point(__u32 bitrate)
652 {
653         __u32 sampl_pt;
654
655         if (bitrate > 800000)
656                 sampl_pt = 750;
657         else if (bitrate > 500000)
658                 sampl_pt = 800;
659         else
660                 sampl_pt = 875;
661
662         return sampl_pt;
663 }
664
665 static void print_bit_timing(const struct can_bittiming_const *btc,
666                              __u32 bitrate, __u32 sample_point, __u32 ref_clk,
667                              bool quiet)
668 {
669         struct net_device dev = {
670                 .priv.bittiming_const = btc,
671                 .priv.clock.freq = ref_clk,
672         };
673         struct can_bittiming bt = {
674                 .bitrate = bitrate,
675                 .sample_point = sample_point,
676         };
677         long rate_error, spt_error;
678
679         if (!quiet) {
680                 printf("Bit timing parameters for %s with %.6f MHz ref clock\n"
681                        "nominal                                 real Bitrt   nom  real SampP\n"
682                        "Bitrate TQ[ns] PrS PhS1 PhS2 SJW BRP Bitrate Error SampP SampP Error ",
683                        btc->name,
684                        ref_clk / 1000000.0);
685
686                 btc->printf_btr(&bt, true);
687                 printf("\n");
688         }
689
690         if (can_calc_bittiming(&dev, &bt)) {
691                 printf("%7d ***bitrate not possible***\n", bitrate);
692                 return;
693         }
694
695         /* get nominal sample point */
696         if (!sample_point)
697                 sample_point = get_cia_sample_point(bitrate);
698
699         rate_error = abs((__s32)(bitrate - bt.bitrate));
700         spt_error = abs((__s32)(sample_point - bt.sample_point));
701
702         printf("%7d "
703                "%6d %3d %4d %4d "
704                "%3d %3d "
705                "%7d %4.1f%% "
706                "%4.1f%% %4.1f%% %4.1f%% ",
707                bitrate,
708                bt.tq, bt.prop_seg, bt.phase_seg1, bt.phase_seg2,
709                bt.sjw, bt.brp,
710
711                bt.bitrate,
712                100.0 * rate_error / bitrate,
713
714                sample_point / 10.0,
715                bt.sample_point / 10.0,
716                100.0 * spt_error / sample_point);
717
718         btc->printf_btr(&bt, false);
719         printf("\n");
720 }
721
722 static void do_list(void)
723 {
724         unsigned int i;
725
726         for (i = 0; i < ARRAY_SIZE(can_calc_consts); i++)
727                 printf("%s\n", can_calc_consts[i].name);
728 }
729
730 int main(int argc, char *argv[])
731 {
732         __u32 bitrate = 0;
733         __u32 opt_ref_clk = 0, ref_clk;
734         int sampl_pt = 0;
735         bool quiet = false, list = false, found = false;
736         char *name = NULL;
737         unsigned int i, j;
738         int opt;
739
740         const struct can_bittiming_const *btc = NULL;
741
742         while ((opt = getopt(argc, argv, "b:c:lps:")) != -1) {
743                 switch (opt) {
744                 case 'b':
745                         bitrate = atoi(optarg);
746                         break;
747
748                 case 'c':
749                         opt_ref_clk = atoi(optarg);
750                         break;
751
752                 case 'l':
753                         list = true;
754                         break;
755
756                 case 'q':
757                         quiet = true;
758                         break;
759
760                 case 's':
761                         sampl_pt = atoi(optarg);
762                         break;
763
764                 default:
765                         print_usage(argv[0]);
766                         break;
767                 }
768         }
769
770         if (argc > optind + 1)
771                 print_usage(argv[0]);
772
773         if (argc == optind + 1)
774                 name = argv[optind];
775
776         if (list) {
777                 do_list();
778                 exit(EXIT_SUCCESS);
779         }
780
781         if (sampl_pt && (sampl_pt >= 1000 || sampl_pt < 100))
782                 print_usage(argv[0]);
783
784         for (i = 0; i < ARRAY_SIZE(can_calc_consts); i++) {
785                 if (name && strcmp(can_calc_consts[i].name, name))
786                         continue;
787
788                 found = true;
789                 btc = &can_calc_consts[i];
790
791                 if (opt_ref_clk)
792                         ref_clk = opt_ref_clk;
793                 else
794                         ref_clk = btc->ref_clk;
795
796                 if (bitrate) {
797                         print_bit_timing(btc, bitrate, sampl_pt, ref_clk, quiet);
798                 } else {
799                         for (j = 0; j < ARRAY_SIZE(common_bitrates); j++)
800                                 print_bit_timing(btc, common_bitrates[j],
801                                                  sampl_pt, ref_clk, j);
802                 }
803                 printf("\n");
804         }
805
806         if (!found) {
807                 printf("error: unknown CAN controller '%s', try one of these:\n\n", name);
808                 do_list();
809                 exit(EXIT_FAILURE);
810         }
811
812         exit(EXIT_SUCCESS);
813 }