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