ptp_clockmatrix.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * PTP hardware clock driver for the IDT ClockMatrix(TM) family of timing and
  4. * synchronization devices.
  5. *
  6. * Copyright (C) 2019 Integrated Device Technology, Inc., a Renesas Company.
  7. */
  8. #ifndef PTP_IDTCLOCKMATRIX_H
  9. #define PTP_IDTCLOCKMATRIX_H
  10. #include <linux/ktime.h>
  11. #include <linux/mfd/idt8a340_reg.h>
  12. #include <linux/ptp_clock.h>
  13. #include <linux/regmap.h>
  14. #define FW_FILENAME "idtcm.bin"
  15. #define MAX_TOD (4)
  16. #define MAX_PLL (8)
  17. #define MAX_REF_CLK (16)
  18. #define MAX_ABS_WRITE_PHASE_PICOSECONDS (107374182350LL)
  19. #define TOD_MASK_ADDR (0xFFA5)
  20. #define DEFAULT_TOD_MASK (0x04)
  21. #define SET_U16_LSB(orig, val8) (orig = (0xff00 & (orig)) | (val8))
  22. #define SET_U16_MSB(orig, val8) (orig = (0x00ff & (orig)) | (val8 << 8))
  23. #define TOD0_PTP_PLL_ADDR (0xFFA8)
  24. #define TOD1_PTP_PLL_ADDR (0xFFA9)
  25. #define TOD2_PTP_PLL_ADDR (0xFFAA)
  26. #define TOD3_PTP_PLL_ADDR (0xFFAB)
  27. #define TOD0_OUT_ALIGN_MASK_ADDR (0xFFB0)
  28. #define TOD1_OUT_ALIGN_MASK_ADDR (0xFFB2)
  29. #define TOD2_OUT_ALIGN_MASK_ADDR (0xFFB4)
  30. #define TOD3_OUT_ALIGN_MASK_ADDR (0xFFB6)
  31. #define DEFAULT_OUTPUT_MASK_PLL0 (0x003)
  32. #define DEFAULT_OUTPUT_MASK_PLL1 (0x00c)
  33. #define DEFAULT_OUTPUT_MASK_PLL2 (0x030)
  34. #define DEFAULT_OUTPUT_MASK_PLL3 (0x0c0)
  35. #define DEFAULT_TOD0_PTP_PLL (0)
  36. #define DEFAULT_TOD1_PTP_PLL (1)
  37. #define DEFAULT_TOD2_PTP_PLL (2)
  38. #define DEFAULT_TOD3_PTP_PLL (3)
  39. #define PHASE_PULL_IN_THRESHOLD_NS_DEPRECATED (150000)
  40. #define PHASE_PULL_IN_THRESHOLD_NS (15000)
  41. #define TOD_WRITE_OVERHEAD_COUNT_MAX (2)
  42. #define TOD_BYTE_COUNT (11)
  43. #define LOCK_TIMEOUT_MS (2000)
  44. #define LOCK_POLL_INTERVAL_MS (10)
  45. #define IDTCM_MAX_WRITE_COUNT (512)
  46. #define PHASE_PULL_IN_MAX_PPB (144000)
  47. #define PHASE_PULL_IN_MIN_THRESHOLD_NS (2)
  48. /*
  49. * Return register address based on passed in firmware version
  50. */
  51. #define IDTCM_FW_REG(FW, VER, REG) (((FW) < (VER)) ? (REG) : (REG##_##VER))
  52. enum fw_version {
  53. V_DEFAULT = 0,
  54. V487 = 1,
  55. V520 = 2,
  56. };
  57. /* PTP PLL Mode */
  58. enum ptp_pll_mode {
  59. PTP_PLL_MODE_MIN = 0,
  60. PTP_PLL_MODE_WRITE_FREQUENCY = PTP_PLL_MODE_MIN,
  61. PTP_PLL_MODE_WRITE_PHASE,
  62. PTP_PLL_MODE_UNSUPPORTED,
  63. PTP_PLL_MODE_MAX = PTP_PLL_MODE_UNSUPPORTED,
  64. };
  65. struct idtcm;
  66. struct idtcm_channel {
  67. struct ptp_clock_info caps;
  68. struct ptp_clock *ptp_clock;
  69. struct idtcm *idtcm;
  70. u16 dpll_phase;
  71. u16 dpll_freq;
  72. u16 dpll_n;
  73. u16 dpll_ctrl_n;
  74. u16 dpll_phase_pull_in;
  75. u16 tod_read_primary;
  76. u16 tod_read_secondary;
  77. u16 tod_write;
  78. u16 tod_n;
  79. u16 hw_dpll_n;
  80. u8 sync_src;
  81. enum ptp_pll_mode mode;
  82. int (*configure_write_frequency)(struct idtcm_channel *channel);
  83. int (*configure_write_phase)(struct idtcm_channel *channel);
  84. int (*do_phase_pull_in)(struct idtcm_channel *channel,
  85. s32 offset_ns, u32 max_ffo_ppb);
  86. s32 current_freq_scaled_ppm;
  87. bool phase_pull_in;
  88. u32 dco_delay;
  89. /* last input trigger for extts */
  90. u8 refn;
  91. u8 pll;
  92. u8 tod;
  93. u16 output_mask;
  94. };
  95. struct idtcm {
  96. struct idtcm_channel channel[MAX_TOD];
  97. struct device *dev;
  98. u8 tod_mask;
  99. char version[16];
  100. enum fw_version fw_ver;
  101. /* Polls for external time stamps */
  102. u8 extts_mask;
  103. bool extts_single_shot;
  104. struct delayed_work extts_work;
  105. /* Remember the ptp channel to report extts */
  106. struct idtcm_channel *event_channel[MAX_TOD];
  107. /* Mutex to protect operations from being interrupted */
  108. struct mutex *lock;
  109. struct device *mfd;
  110. struct regmap *regmap;
  111. /* Overhead calculation for adjtime */
  112. u8 calculate_overhead_flag;
  113. s64 tod_write_overhead_ns;
  114. ktime_t start_time;
  115. };
  116. struct idtcm_fwrc {
  117. u8 hiaddr;
  118. u8 loaddr;
  119. u8 value;
  120. u8 reserved;
  121. } __packed;
  122. #endif /* PTP_IDTCLOCKMATRIX_H */