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