bittiming.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 <wg@grandegger.com>
  5. */
  6. #include <linux/can/dev.h>
  7. void can_sjw_set_default(struct can_bittiming *bt)
  8. {
  9. if (bt->sjw)
  10. return;
  11. /* If user space provides no sjw, use sane default of phase_seg2 / 2 */
  12. bt->sjw = max(1U, min(bt->phase_seg1, bt->phase_seg2 / 2));
  13. }
  14. int can_sjw_check(const struct net_device *dev, const struct can_bittiming *bt,
  15. const struct can_bittiming_const *btc, struct netlink_ext_ack *extack)
  16. {
  17. if (bt->sjw > btc->sjw_max) {
  18. NL_SET_ERR_MSG_FMT(extack, "sjw: %u greater than max sjw: %u",
  19. bt->sjw, btc->sjw_max);
  20. return -EINVAL;
  21. }
  22. if (bt->sjw > bt->phase_seg1) {
  23. NL_SET_ERR_MSG_FMT(extack,
  24. "sjw: %u greater than phase-seg1: %u",
  25. bt->sjw, bt->phase_seg1);
  26. return -EINVAL;
  27. }
  28. if (bt->sjw > bt->phase_seg2) {
  29. NL_SET_ERR_MSG_FMT(extack,
  30. "sjw: %u greater than phase-seg2: %u",
  31. bt->sjw, bt->phase_seg2);
  32. return -EINVAL;
  33. }
  34. return 0;
  35. }
  36. /* Checks the validity of the specified bit-timing parameters prop_seg,
  37. * phase_seg1, phase_seg2 and sjw and tries to determine the bitrate
  38. * prescaler value brp. You can find more information in the header
  39. * file linux/can/netlink.h.
  40. */
  41. static int can_fixup_bittiming(const struct net_device *dev, struct can_bittiming *bt,
  42. const struct can_bittiming_const *btc,
  43. struct netlink_ext_ack *extack)
  44. {
  45. const unsigned int tseg1 = bt->prop_seg + bt->phase_seg1;
  46. const struct can_priv *priv = netdev_priv(dev);
  47. u64 brp64;
  48. int err;
  49. if (tseg1 < btc->tseg1_min) {
  50. NL_SET_ERR_MSG_FMT(extack, "prop-seg + phase-seg1: %u less than tseg1-min: %u",
  51. tseg1, btc->tseg1_min);
  52. return -EINVAL;
  53. }
  54. if (tseg1 > btc->tseg1_max) {
  55. NL_SET_ERR_MSG_FMT(extack, "prop-seg + phase-seg1: %u greater than tseg1-max: %u",
  56. tseg1, btc->tseg1_max);
  57. return -EINVAL;
  58. }
  59. if (bt->phase_seg2 < btc->tseg2_min) {
  60. NL_SET_ERR_MSG_FMT(extack, "phase-seg2: %u less than tseg2-min: %u",
  61. bt->phase_seg2, btc->tseg2_min);
  62. return -EINVAL;
  63. }
  64. if (bt->phase_seg2 > btc->tseg2_max) {
  65. NL_SET_ERR_MSG_FMT(extack, "phase-seg2: %u greater than tseg2-max: %u",
  66. bt->phase_seg2, btc->tseg2_max);
  67. return -EINVAL;
  68. }
  69. can_sjw_set_default(bt);
  70. err = can_sjw_check(dev, bt, btc, extack);
  71. if (err)
  72. return err;
  73. brp64 = (u64)priv->clock.freq * (u64)bt->tq;
  74. if (btc->brp_inc > 1)
  75. do_div(brp64, btc->brp_inc);
  76. brp64 += 500000000UL - 1;
  77. do_div(brp64, 1000000000UL); /* the practicable BRP */
  78. if (btc->brp_inc > 1)
  79. brp64 *= btc->brp_inc;
  80. bt->brp = (u32)brp64;
  81. if (bt->brp < btc->brp_min) {
  82. NL_SET_ERR_MSG_FMT(extack, "resulting brp: %u less than brp-min: %u",
  83. bt->brp, btc->brp_min);
  84. return -EINVAL;
  85. }
  86. if (bt->brp > btc->brp_max) {
  87. NL_SET_ERR_MSG_FMT(extack, "resulting brp: %u greater than brp-max: %u",
  88. bt->brp, btc->brp_max);
  89. return -EINVAL;
  90. }
  91. bt->bitrate = priv->clock.freq / (bt->brp * can_bit_time(bt));
  92. bt->sample_point = ((CAN_SYNC_SEG + tseg1) * 1000) / can_bit_time(bt);
  93. bt->tq = DIV_U64_ROUND_CLOSEST(mul_u32_u32(bt->brp, NSEC_PER_SEC),
  94. priv->clock.freq);
  95. return 0;
  96. }
  97. /* Checks the validity of predefined bitrate settings */
  98. static int
  99. can_validate_bitrate(const struct net_device *dev, const struct can_bittiming *bt,
  100. const u32 *bitrate_const,
  101. const unsigned int bitrate_const_cnt,
  102. struct netlink_ext_ack *extack)
  103. {
  104. unsigned int i;
  105. for (i = 0; i < bitrate_const_cnt; i++) {
  106. if (bt->bitrate == bitrate_const[i])
  107. return 0;
  108. }
  109. NL_SET_ERR_MSG_FMT(extack, "bitrate %u bps not supported",
  110. bt->brp);
  111. return -EINVAL;
  112. }
  113. int can_get_bittiming(const struct net_device *dev, struct can_bittiming *bt,
  114. const struct can_bittiming_const *btc,
  115. const u32 *bitrate_const,
  116. const unsigned int bitrate_const_cnt,
  117. struct netlink_ext_ack *extack)
  118. {
  119. /* Depending on the given can_bittiming parameter structure the CAN
  120. * timing parameters are calculated based on the provided bitrate OR
  121. * alternatively the CAN timing parameters (tq, prop_seg, etc.) are
  122. * provided directly which are then checked and fixed up.
  123. */
  124. if (!bt->tq && bt->bitrate && btc)
  125. return can_calc_bittiming(dev, bt, btc, extack);
  126. if (bt->tq && !bt->bitrate && btc)
  127. return can_fixup_bittiming(dev, bt, btc, extack);
  128. if (!bt->tq && bt->bitrate && bitrate_const)
  129. return can_validate_bitrate(dev, bt, bitrate_const,
  130. bitrate_const_cnt, extack);
  131. return -EINVAL;
  132. }