bittiming.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (C) 2005 Marc Kleine-Budde, Pengutronix
  3. * Copyright (C) 2006 Andrey Volkov, Varma Electronics
  4. * Copyright (C) 2008-2009 Wolfgang Grandegger <[email protected]>
  5. */
  6. #include <linux/can/dev.h>
  7. /* Checks the validity of the specified bit-timing parameters prop_seg,
  8. * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate
  9. * prescaler value brp. You can find more information in the header
  10. * file linux/can/netlink.h.
  11. */
  12. static int can_fixup_bittiming(const struct net_device *dev, struct can_bittiming *bt,
  13. const struct can_bittiming_const *btc)
  14. {
  15. const struct can_priv *priv = netdev_priv(dev);
  16. unsigned int tseg1, alltseg;
  17. u64 brp64;
  18. tseg1 = bt->prop_seg + bt->phase_seg1;
  19. if (!bt->sjw)
  20. bt->sjw = 1;
  21. if (bt->sjw > btc->sjw_max ||
  22. tseg1 < btc->tseg1_min || tseg1 > btc->tseg1_max ||
  23. bt->phase_seg2 < btc->tseg2_min || bt->phase_seg2 > btc->tseg2_max)
  24. return -ERANGE;
  25. brp64 = (u64)priv->clock.freq * (u64)bt->tq;
  26. if (btc->brp_inc > 1)
  27. do_div(brp64, btc->brp_inc);
  28. brp64 += 500000000UL - 1;
  29. do_div(brp64, 1000000000UL); /* the practicable BRP */
  30. if (btc->brp_inc > 1)
  31. brp64 *= btc->brp_inc;
  32. bt->brp = (u32)brp64;
  33. if (bt->brp < btc->brp_min || bt->brp > btc->brp_max)
  34. return -EINVAL;
  35. alltseg = bt->prop_seg + bt->phase_seg1 + bt->phase_seg2 + 1;
  36. bt->bitrate = priv->clock.freq / (bt->brp * alltseg);
  37. bt->sample_point = ((tseg1 + 1) * 1000) / alltseg;
  38. return 0;
  39. }
  40. /* Checks the validity of predefined bitrate settings */
  41. static int
  42. can_validate_bitrate(const struct net_device *dev, const struct can_bittiming *bt,
  43. const u32 *bitrate_const,
  44. const unsigned int bitrate_const_cnt)
  45. {
  46. unsigned int i;
  47. for (i = 0; i < bitrate_const_cnt; i++) {
  48. if (bt->bitrate == bitrate_const[i])
  49. return 0;
  50. }
  51. return -EINVAL;
  52. }
  53. int can_get_bittiming(const struct net_device *dev, struct can_bittiming *bt,
  54. const struct can_bittiming_const *btc,
  55. const u32 *bitrate_const,
  56. const unsigned int bitrate_const_cnt)
  57. {
  58. int err;
  59. /* Depending on the given can_bittiming parameter structure the CAN
  60. * timing parameters are calculated based on the provided bitrate OR
  61. * alternatively the CAN timing parameters (tq, prop_seg, etc.) are
  62. * provided directly which are then checked and fixed up.
  63. */
  64. if (!bt->tq && bt->bitrate && btc)
  65. err = can_calc_bittiming(dev, bt, btc);
  66. else if (bt->tq && !bt->bitrate && btc)
  67. err = can_fixup_bittiming(dev, bt, btc);
  68. else if (!bt->tq && bt->bitrate && bitrate_const)
  69. err = can_validate_bitrate(dev, bt, bitrate_const,
  70. bitrate_const_cnt);
  71. else
  72. err = -EINVAL;
  73. return err;
  74. }