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