]> rtime.felk.cvut.cz Git - can-utils.git/blob - can-calc-bit-timing.c
candump: Enable HW timestamping before using it
[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  * Copyright (C) 2016 Marc Kleine-Budde <mkl@pengutronix.de>
5  *
6  * Derived from:
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
12  *
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
16  * any later version.
17  */
18
19 #include <errno.h>
20 #include <getopt.h>
21 #include <limits.h>
22 #include <stdbool.h>
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include <linux/types.h>
29 #include <linux/can/netlink.h>
30
31 /* imported from kernel */
32
33 /**
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.
38  *
39  * Return: an absolute value of x.
40  */
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; }), \
49                         ((void)0)))))))
50
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)
55
56 /*
57  * min()/max()/clamp() macros that also do
58  * strict type-checking.. See the
59  * "unnecessary" pointer comparison.
60  */
61 #define min(x, y) ({                            \
62         typeof(x) _min1 = (x);                  \
63         typeof(y) _min2 = (y);                  \
64         (void) (&_min1 == &_min2);              \
65         _min1 < _min2 ? _min1 : _min2; })
66
67 #define max(x, y) ({                            \
68         typeof(x) _max1 = (x);                  \
69         typeof(y) _max2 = (y);                  \
70         (void) (&_max1 == &_max2);              \
71         _max1 > _max2 ? _max1 : _max2; })
72
73 /**
74  * clamp - return a value clamped to a given range with strict typechecking
75  * @val: current value
76  * @lo: lowest allowable value
77  * @hi: highest allowable value
78  *
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.
81  */
82 #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
83
84 # define do_div(n,base) ({                                      \
85         uint32_t __base = (base);                               \
86         uint32_t __rem;                                         \
87         __rem = ((uint64_t)(n)) % __base;                       \
88         (n) = ((uint64_t)(n)) / __base;                         \
89         __rem;                                                  \
90  })
91
92 /* */
93
94 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
95
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)
99
100 /* define in-kernel-types */
101 typedef __u64 u64;
102 typedef __u32 u32;
103
104 struct calc_ref_clk {
105         __u32 clk;      /* CAN system clock frequency in Hz */
106         char *name;
107 };
108
109 struct calc_bittiming_const {
110         struct can_bittiming_const bittiming_const;
111
112         const struct calc_ref_clk ref_clk[16];
113         void (*printf_btr)(struct can_bittiming *bt, bool hdr);
114 };
115
116 /*
117  * minimal structs, just enough to be source level compatible
118  */
119 struct can_priv {
120         struct can_clock clock;
121 };
122
123 struct net_device {
124         struct can_priv priv;
125 };
126
127 static inline void *netdev_priv(const struct net_device *dev)
128 {
129         return (void *)&dev->priv;
130 }
131
132 static void print_usage(char *cmd)
133 {
134         printf("Usage: %s [options] [<CAN-contoller-name>]\n"
135                "\tOptions:\n"
136                "\t-q           : don't print header line\n"
137                "\t-l           : list all support CAN controller names\n"
138                "\t-b <bitrate> : bit-rate in bits/sec\n"
139                "\t-s <samp_pt> : sample-point in one-tenth of a percent\n"
140                "\t               or 0 for CIA recommended sample points\n"
141                "\t-c <clock>   : real CAN system clock in Hz\n",
142                cmd);
143
144         exit(EXIT_FAILURE);
145 }
146
147 static void printf_btr_sja1000(struct can_bittiming *bt, bool hdr)
148 {
149         uint8_t btr0, btr1;
150
151         if (hdr) {
152                 printf("BTR0 BTR1");
153         } else {
154                 btr0 = ((bt->brp - 1) & 0x3f) | (((bt->sjw - 1) & 0x3) << 6);
155                 btr1 = ((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) |
156                         (((bt->phase_seg2 - 1) & 0x7) << 4);
157                 printf("0x%02x 0x%02x", btr0, btr1);
158         }
159 }
160
161 static void printf_btr_at91(struct can_bittiming *bt, bool hdr)
162 {
163         if (hdr) {
164                 printf("%10s", "CAN_BR");
165         } else {
166                 uint32_t br = ((bt->phase_seg2 - 1) |
167                                ((bt->phase_seg1 - 1) << 4) |
168                                ((bt->prop_seg - 1) << 8) |
169                                ((bt->sjw - 1) << 12) |
170                                ((bt->brp - 1) << 16));
171                 printf("0x%08x", br);
172         }
173 }
174
175 static void printf_btr_flexcan(struct can_bittiming *bt, bool hdr)
176 {
177         if (hdr) {
178                 printf("%10s", "CAN_CTRL");
179         } else {
180                 uint32_t ctrl = (((bt->brp        - 1) << 24) |
181                                  ((bt->sjw        - 1) << 22) |
182                                  ((bt->phase_seg1 - 1) << 19) |
183                                  ((bt->phase_seg2 - 1) << 16) |
184                                  ((bt->prop_seg   - 1) <<  0));
185
186                 printf("0x%08x", ctrl);
187         }
188 }
189
190 static void printf_btr_mcp251x(struct can_bittiming *bt, bool hdr)
191 {
192         uint8_t cnf1, cnf2, cnf3;
193
194         if (hdr) {
195                 printf("CNF1 CNF2 CNF3");
196         } else {
197                 cnf1 = ((bt->sjw - 1) << 6) | (bt->brp - 1);
198                 cnf2 = 0x80 | ((bt->phase_seg1 - 1) << 3) | (bt->prop_seg - 1);
199                 cnf3 = bt->phase_seg2 - 1;
200                 printf("0x%02x 0x%02x 0x%02x", cnf1, cnf2, cnf3);
201         }
202 }
203
204 static void printf_btr_ti_hecc(struct can_bittiming *bt, bool hdr)
205 {
206         if (hdr) {
207                 printf("%10s", "CANBTC");
208         } else {
209                 uint32_t can_btc;
210
211                 can_btc = (bt->phase_seg2 - 1) & 0x7;
212                 can_btc |= ((bt->phase_seg1 + bt->prop_seg - 1)
213                             & 0xF) << 3;
214                 can_btc |= ((bt->sjw - 1) & 0x3) << 8;
215                 can_btc |= ((bt->brp - 1) & 0xFF) << 16;
216
217                 printf("0x%08x", can_btc);
218         }
219 }
220
221 #define RCAR_CAN_BCR_TSEG1(x)   (((x) & 0x0f) << 20)
222 #define RCAR_CAN_BCR_BPR(x)     (((x) & 0x3ff) << 8)
223 #define RCAR_CAN_BCR_SJW(x)     (((x) & 0x3) << 4)
224 #define RCAR_CAN_BCR_TSEG2(x)   ((x) & 0x07)
225
226 static void printf_btr_rcar_can(struct can_bittiming *bt, bool hdr)
227 {
228         if (hdr) {
229                 printf("%10s", "CiBCR");
230         } else {
231                 uint32_t bcr;
232
233                 bcr = RCAR_CAN_BCR_TSEG1(bt->phase_seg1 + bt->prop_seg - 1) |
234                         RCAR_CAN_BCR_BPR(bt->brp - 1) |
235                         RCAR_CAN_BCR_SJW(bt->sjw - 1) |
236                         RCAR_CAN_BCR_TSEG2(bt->phase_seg2 - 1);
237
238                 printf("0x%08x", bcr << 8);
239         }
240 }
241
242 static struct calc_bittiming_const can_calc_consts[] = {
243         {
244                 .bittiming_const = {
245                         .name = "sja1000",
246                         .tseg1_min = 1,
247                         .tseg1_max = 16,
248                         .tseg2_min = 1,
249                         .tseg2_max = 8,
250                         .sjw_max = 4,
251                         .brp_min = 1,
252                         .brp_max = 64,
253                         .brp_inc = 1,
254                 },
255                 .ref_clk = {
256                         { .clk = 8000000, },
257                 },
258                 .printf_btr = printf_btr_sja1000,
259         }, {
260                 .bittiming_const = {
261                         .name = "mscan",
262                         .tseg1_min = 4,
263                         .tseg1_max = 16,
264                         .tseg2_min = 2,
265                         .tseg2_max = 8,
266                         .sjw_max = 4,
267                         .brp_min = 1,
268                         .brp_max = 64,
269                         .brp_inc = 1,
270                 },
271                 .ref_clk = {
272                         { .clk = 32000000, },
273                         { .clk = 33000000, },
274                         { .clk = 33300000, },
275                         { .clk = 33333333, },
276                         { .clk = 66660000, .name = "mpc5121", },
277                         { .clk = 66666666, .name = "mpc5121" },
278                 },
279                 .printf_btr = printf_btr_sja1000,
280         }, {
281                 .bittiming_const = {
282                         .name = "at91",
283                         .tseg1_min = 4,
284                         .tseg1_max = 16,
285                         .tseg2_min = 2,
286                         .tseg2_max = 8,
287                         .sjw_max = 4,
288                         .brp_min = 2,
289                         .brp_max = 128,
290                         .brp_inc = 1,
291                 },
292                 .ref_clk = {
293                         { .clk = 99532800, .name = "ronetix PM9263", },
294                         { .clk = 100000000, },
295                 },
296                 .printf_btr = printf_btr_at91,
297         }, {
298                 .bittiming_const = {
299                         .name = "flexcan",
300                         .tseg1_min = 4,
301                         .tseg1_max = 16,
302                         .tseg2_min = 2,
303                         .tseg2_max = 8,
304                         .sjw_max = 4,
305                         .brp_min = 1,
306                         .brp_max = 256,
307                         .brp_inc = 1,
308                 },
309                 .ref_clk = {
310                         { .clk = 24000000, .name = "mx28" },
311                         { .clk = 30000000, .name = "mx6" },
312                         { .clk = 49875000, },
313                         { .clk = 66000000, },
314                         { .clk = 66500000, },
315                         { .clk = 66666666, },
316                         { .clk = 83368421, .name = "vybrid" },
317                 },
318                 .printf_btr = printf_btr_flexcan,
319         }, {
320                 .bittiming_const = {
321                         .name = "mcp251x",
322                         .tseg1_min = 3,
323                         .tseg1_max = 16,
324                         .tseg2_min = 2,
325                         .tseg2_max = 8,
326                         .sjw_max = 4,
327                         .brp_min = 1,
328                         .brp_max = 64,
329                         .brp_inc = 1,
330                 },
331                 .ref_clk = {
332                         { .clk =  8000000, },
333                         { .clk = 16000000, },
334                 },
335                 .printf_btr = printf_btr_mcp251x,
336         }, {
337                 .bittiming_const = {
338                         .name = "ti_hecc",
339                         .tseg1_min = 1,
340                         .tseg1_max = 16,
341                         .tseg2_min = 1,
342                         .tseg2_max = 8,
343                         .sjw_max = 4,
344                         .brp_min = 1,
345                         .brp_max = 256,
346                         .brp_inc = 1,
347                 },
348                 .ref_clk = {
349                         { .clk = 13000000, },
350                 },
351                 .printf_btr = printf_btr_ti_hecc,
352         }, {
353                 .bittiming_const = {
354                         .name = "rcar_can",
355                         .tseg1_min = 4,
356                         .tseg1_max = 16,
357                         .tseg2_min = 2,
358                         .tseg2_max = 8,
359                         .sjw_max = 4,
360                         .brp_min = 1,
361                         .brp_max = 1024,
362                         .brp_inc = 1,
363                 },
364                 .ref_clk = {
365                         { .clk = 65000000, },
366                 },
367                 .printf_btr = printf_btr_rcar_can,
368         },
369 };
370
371 static long common_bitrates[] = {
372         1000000,
373         800000,
374         500000,
375         250000,
376         125000,
377         100000,
378         50000,
379         20000,
380         10000,
381 };
382
383 #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */
384 #define CAN_CALC_SYNC_SEG 1
385
386 /*
387  * Bit-timing calculation derived from:
388  *
389  * Code based on LinCAN sources and H8S2638 project
390  * Copyright 2004-2006 Pavel Pisa - DCE FELK CVUT cz
391  * Copyright 2005      Stanislav Marek
392  * email: pisa@cmp.felk.cvut.cz
393  *
394  * Calculates proper bit-timing parameters for a specified bit-rate
395  * and sample-point, which can then be used to set the bit-timing
396  * registers of the CAN controller. You can find more information
397  * in the header file linux/can/netlink.h.
398  */
399 static int can_update_spt(const struct can_bittiming_const *btc,
400                           unsigned int spt_nominal, unsigned int tseg,
401                           unsigned int *tseg1_ptr, unsigned int *tseg2_ptr,
402                           unsigned int *spt_error_ptr)
403 {
404         unsigned int spt_error, best_spt_error = UINT_MAX;
405         unsigned int spt, best_spt = 0;
406         unsigned int tseg1, tseg2;
407         int i;
408
409         for (i = 0; i <= 1; i++) {
410                 tseg2 = tseg + CAN_CALC_SYNC_SEG - (spt_nominal * (tseg + CAN_CALC_SYNC_SEG)) / 1000 - i;
411                 tseg2 = clamp(tseg2, btc->tseg2_min, btc->tseg2_max);
412                 tseg1 = tseg - tseg2;
413                 if (tseg1 > btc->tseg1_max) {
414                         tseg1 = btc->tseg1_max;
415                         tseg2 = tseg - tseg1;
416                 }
417
418                 spt = 1000 * (tseg + CAN_CALC_SYNC_SEG - tseg2) / (tseg + CAN_CALC_SYNC_SEG);
419                 spt_error = abs(spt_nominal - spt);
420
421                 if ((spt <= spt_nominal) && (spt_error < best_spt_error)) {
422                         best_spt = spt;
423                         best_spt_error = spt_error;
424                         *tseg1_ptr = tseg1;
425                         *tseg2_ptr = tseg2;
426                 }
427         }
428
429         if (spt_error_ptr)
430                 *spt_error_ptr = best_spt_error;
431
432         return best_spt;
433 }
434
435 static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt,
436                               const struct can_bittiming_const *btc)
437 {
438         struct can_priv *priv = netdev_priv(dev);
439         unsigned int rate;              /* current bitrate */
440         unsigned int rate_error;        /* difference between current and nominal value */
441         unsigned int best_rate_error = UINT_MAX;
442         unsigned int spt_error;         /* difference between current and nominal value */
443         unsigned int best_spt_error = UINT_MAX;
444         unsigned int spt_nominal;       /* nominal sample point */
445         unsigned int best_tseg = 0;     /* current best value for tseg */
446         unsigned int best_brp = 0;      /* current best value for brp */
447         unsigned int brp, tsegall, tseg, tseg1, tseg2;
448         u64 v64;
449
450         /* Use CiA recommended sample points */
451         if (bt->sample_point) {
452                 spt_nominal = bt->sample_point;
453         } else {
454                 if (bt->bitrate > 800000)
455                         spt_nominal = 750;
456                 else if (bt->bitrate > 500000)
457                         spt_nominal = 800;
458                 else
459                         spt_nominal = 875;
460         }
461
462         /* tseg even = round down, odd = round up */
463         for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1;
464              tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) {
465                 tsegall = CAN_CALC_SYNC_SEG + tseg / 2;
466
467                 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */
468                 brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2;
469
470                 /* choose brp step which is possible in system */
471                 brp = (brp / btc->brp_inc) * btc->brp_inc;
472                 if ((brp < btc->brp_min) || (brp > btc->brp_max))
473                         continue;
474
475                 rate = priv->clock.freq / (brp * tsegall);
476                 rate_error = abs(bt->bitrate - rate);
477
478                 /* tseg brp biterror */
479                 if (rate_error > best_rate_error)
480                         continue;
481
482                 /* reset sample point error if we have a better bitrate */
483                 if (rate_error < best_rate_error)
484                         best_spt_error = UINT_MAX;
485
486                 can_update_spt(btc, spt_nominal, tseg / 2, &tseg1, &tseg2, &spt_error);
487                 if (spt_error > best_spt_error)
488                         continue;
489
490                 best_spt_error = spt_error;
491                 best_rate_error = rate_error;
492                 best_tseg = tseg / 2;
493                 best_brp = brp;
494
495                 if (rate_error == 0 && spt_error == 0)
496                         break;
497         }
498
499         if (best_rate_error) {
500                 /* Error in one-tenth of a percent */
501                 rate_error = (best_rate_error * 1000) / bt->bitrate;
502                 if (rate_error > CAN_CALC_MAX_ERROR) {
503                         netdev_err(dev,
504                                    "bitrate error %ld.%ld%% too high\n",
505                                    rate_error / 10, rate_error % 10);
506                         return -EDOM;
507                 }
508                 netdev_warn(dev, "bitrate error %ld.%ld%%\n",
509                             rate_error / 10, rate_error % 10);
510         }
511
512         /* real sample point */
513         bt->sample_point = can_update_spt(btc, spt_nominal, best_tseg,
514                                           &tseg1, &tseg2, NULL);
515
516         v64 = (u64)best_brp * 1000 * 1000 * 1000;
517         do_div(v64, priv->clock.freq);
518         bt->tq = (u32)v64;
519         bt->prop_seg = tseg1 / 2;
520         bt->phase_seg1 = tseg1 - bt->prop_seg;
521         bt->phase_seg2 = tseg2;
522
523         /* check for sjw user settings */
524         if (!bt->sjw || !btc->sjw_max) {
525                 bt->sjw = 1;
526         } else {
527                 /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */
528                 if (bt->sjw > btc->sjw_max)
529                         bt->sjw = btc->sjw_max;
530                 /* bt->sjw must not be higher than tseg2 */
531                 if (tseg2 < bt->sjw)
532                         bt->sjw = tseg2;
533         }
534
535         bt->brp = best_brp;
536
537         /* real bit-rate */
538         bt->bitrate = priv->clock.freq / (bt->brp * (CAN_CALC_SYNC_SEG + tseg1 + tseg2));
539
540         return 0;
541 }
542
543 static __u32 get_cia_sample_point(__u32 bitrate)
544 {
545         __u32 sampl_pt;
546
547         if (bitrate > 800000)
548                 sampl_pt = 750;
549         else if (bitrate > 500000)
550                 sampl_pt = 800;
551         else
552                 sampl_pt = 875;
553
554         return sampl_pt;
555 }
556
557 static void print_bit_timing(const struct calc_bittiming_const *btc,
558                              const struct calc_ref_clk *ref_clk,
559                              unsigned int bitrate_nominal,
560                              unsigned int spt_nominal,
561                              bool quiet)
562 {
563         struct net_device dev = {
564                 .priv.clock.freq = ref_clk->clk,
565         };
566         struct can_bittiming bt = {
567                 .bitrate = bitrate_nominal,
568                 .sample_point = spt_nominal,
569         };
570         long rate_error, spt_error;
571
572         if (!quiet) {
573                 printf("Bit timing parameters for %s%s%s%s with %.6f MHz ref clock\n"
574                        "nominal                                 real Bitrt   nom  real SampP\n"
575                        "Bitrate TQ[ns] PrS PhS1 PhS2 SJW BRP Bitrate Error SampP SampP Error ",
576                        btc->bittiming_const.name,
577                        ref_clk->name ? " (" : "",
578                        ref_clk->name ? ref_clk->name : "",
579                        ref_clk->name ? ")" : "",
580                        ref_clk->clk / 1000000.0);
581
582                 btc->printf_btr(&bt, true);
583                 printf("\n");
584         }
585
586         if (can_calc_bittiming(&dev, &bt, &btc->bittiming_const)) {
587                 printf("%7d ***bitrate not possible***\n", bitrate_nominal);
588                 return;
589         }
590
591         /* get nominal sample point */
592         if (!spt_nominal)
593                 spt_nominal = get_cia_sample_point(bitrate_nominal);
594
595         rate_error = abs(bitrate_nominal - bt.bitrate);
596         spt_error = abs(spt_nominal - bt.sample_point);
597
598         printf("%7d "
599                "%6d %3d %4d %4d "
600                "%3d %3d "
601                "%7d %4.1f%% "
602                "%4.1f%% %4.1f%% %4.1f%% ",
603                bitrate_nominal,
604                bt.tq, bt.prop_seg, bt.phase_seg1, bt.phase_seg2,
605                bt.sjw, bt.brp,
606
607                bt.bitrate,
608                100.0 * rate_error / bitrate_nominal,
609
610                spt_nominal / 10.0,
611                bt.sample_point / 10.0,
612                100.0 * spt_error / spt_nominal);
613
614         btc->printf_btr(&bt, false);
615         printf("\n");
616 }
617
618 static void do_list(void)
619 {
620         unsigned int i;
621
622         for (i = 0; i < ARRAY_SIZE(can_calc_consts); i++)
623                 printf("%s\n", can_calc_consts[i].bittiming_const.name);
624 }
625
626 int main(int argc, char *argv[])
627 {
628         __u32 bitrate_nominal = 0;
629         struct calc_ref_clk opt_ref_clk = {
630                 .name = "cmd-line",
631         };
632         const struct calc_ref_clk *ref_clk;
633         unsigned int spt_nominal = 0;
634         bool quiet = false, list = false, found = false;
635         char *name = NULL;
636         unsigned int i, j, k;
637         int opt;
638
639         const struct calc_bittiming_const *btc;
640
641         while ((opt = getopt(argc, argv, "b:c:lqs:")) != -1) {
642                 switch (opt) {
643                 case 'b':
644                         bitrate_nominal = atoi(optarg);
645                         break;
646
647                 case 'c':
648                         opt_ref_clk.clk = strtoul(optarg, NULL, 10);
649                         break;
650
651                 case 'l':
652                         list = true;
653                         break;
654
655                 case 'q':
656                         quiet = true;
657                         break;
658
659                 case 's':
660                         spt_nominal = strtoul(optarg, NULL, 10);
661                         break;
662
663                 default:
664                         print_usage(argv[0]);
665                         break;
666                 }
667         }
668
669         if (argc > optind + 1)
670                 print_usage(argv[0]);
671
672         if (argc == optind + 1)
673                 name = argv[optind];
674
675         if (list) {
676                 do_list();
677                 exit(EXIT_SUCCESS);
678         }
679
680         if (spt_nominal && (spt_nominal >= 1000 || spt_nominal < 100))
681                 print_usage(argv[0]);
682
683         for (i = 0; i < ARRAY_SIZE(can_calc_consts); i++) {
684                 if (name && strcmp(can_calc_consts[i].bittiming_const.name, name))
685                         continue;
686
687                 found = true;
688                 btc = &can_calc_consts[i];
689
690                 for (j = 0; j < ARRAY_SIZE(btc->ref_clk); j++) {
691                         if (opt_ref_clk.clk)
692                                 ref_clk = &opt_ref_clk;
693                         else
694                                 ref_clk = &btc->ref_clk[j];
695
696                         if (!ref_clk->clk)
697                                 break;
698
699                         if (bitrate_nominal) {
700                                 print_bit_timing(btc, ref_clk, bitrate_nominal,
701                                                  spt_nominal, quiet);
702                         } else {
703                                 for (k = 0; k < ARRAY_SIZE(common_bitrates); k++)
704                                         print_bit_timing(btc, ref_clk,
705                                                          common_bitrates[k],
706                                                          spt_nominal, k);
707                         }
708                         printf("\n");
709
710                         if (opt_ref_clk.clk)
711                                 break;
712                 }
713         }
714
715         if (!found) {
716                 printf("error: unknown CAN controller '%s', try one of these:\n\n", name);
717                 do_list();
718                 exit(EXIT_FAILURE);
719         }
720
721         exit(EXIT_SUCCESS);
722 }