open_alliance_helpers.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * open_alliance_helpers.c - OPEN Alliance specific PHY diagnostic helpers
  4. *
  5. * This file contains helper functions for implementing advanced diagnostic
  6. * features as specified by the OPEN Alliance for automotive Ethernet PHYs.
  7. * These helpers include functionality for Time Delay Reflection (TDR), dynamic
  8. * channel quality assessment, and other PHY diagnostics.
  9. *
  10. * For more information on the specifications, refer to the OPEN Alliance
  11. * documentation: https://opensig.org/automotive-ethernet-specifications/
  12. * Currently following specifications are partially or fully implemented:
  13. * - Advanced diagnostic features for 1000BASE-T1 automotive Ethernet PHYs.
  14. * TC12 - advanced PHY features.
  15. * https://opensig.org/wp-content/uploads/2024/03/Advanced_PHY_features_for_automotive_Ethernet_v2.0_fin.pdf
  16. */
  17. #include <linux/bitfield.h>
  18. #include <linux/ethtool_netlink.h>
  19. #include "open_alliance_helpers.h"
  20. /**
  21. * oa_1000bt1_get_ethtool_cable_result_code - Convert TDR status to ethtool
  22. * result code
  23. * @reg_value: Value read from the TDR register
  24. *
  25. * This function takes a register value from the HDD.TDR register and converts
  26. * the TDR status to the corresponding ethtool cable test result code.
  27. *
  28. * Return: The appropriate ethtool result code based on the TDR status
  29. */
  30. int oa_1000bt1_get_ethtool_cable_result_code(u16 reg_value)
  31. {
  32. u8 tdr_status = FIELD_GET(OA_1000BT1_HDD_TDR_STATUS_MASK, reg_value);
  33. u8 dist_val = FIELD_GET(OA_1000BT1_HDD_TDR_DISTANCE_MASK, reg_value);
  34. switch (tdr_status) {
  35. case OA_1000BT1_HDD_TDR_STATUS_CABLE_OK:
  36. return ETHTOOL_A_CABLE_RESULT_CODE_OK;
  37. case OA_1000BT1_HDD_TDR_STATUS_OPEN:
  38. return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
  39. case OA_1000BT1_HDD_TDR_STATUS_SHORT:
  40. return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
  41. case OA_1000BT1_HDD_TDR_STATUS_NOISE:
  42. return ETHTOOL_A_CABLE_RESULT_CODE_NOISE;
  43. default:
  44. if (dist_val == OA_1000BT1_HDD_TDR_DISTANCE_RESOLUTION_NOT_POSSIBLE)
  45. return ETHTOOL_A_CABLE_RESULT_CODE_RESOLUTION_NOT_POSSIBLE;
  46. return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
  47. }
  48. }
  49. EXPORT_SYMBOL_GPL(oa_1000bt1_get_ethtool_cable_result_code);
  50. /**
  51. * oa_1000bt1_get_tdr_distance - Get distance to the main fault from TDR
  52. * register value
  53. * @reg_value: Value read from the TDR register
  54. *
  55. * This function takes a register value from the HDD.TDR register and extracts
  56. * the distance to the main fault detected by the TDR feature. The distance is
  57. * measured in centimeters and ranges from 0 to 3100 centimeters. If the
  58. * distance is not available (0x3f), the function returns -ERANGE.
  59. *
  60. * Return: The distance to the main fault in centimeters, or -ERANGE if the
  61. * resolution is not possible.
  62. */
  63. int oa_1000bt1_get_tdr_distance(u16 reg_value)
  64. {
  65. u8 dist_val = FIELD_GET(OA_1000BT1_HDD_TDR_DISTANCE_MASK, reg_value);
  66. if (dist_val == OA_1000BT1_HDD_TDR_DISTANCE_RESOLUTION_NOT_POSSIBLE)
  67. return -ERANGE;
  68. return dist_val * 100;
  69. }
  70. EXPORT_SYMBOL_GPL(oa_1000bt1_get_tdr_distance);