tsnep_ptp.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2021 Gerhard Engleder <[email protected]> */
  3. #include "tsnep.h"
  4. void tsnep_get_system_time(struct tsnep_adapter *adapter, u64 *time)
  5. {
  6. u32 high_before;
  7. u32 low;
  8. u32 high;
  9. /* read high dword twice to detect overrun */
  10. high = ioread32(adapter->addr + ECM_SYSTEM_TIME_HIGH);
  11. do {
  12. low = ioread32(adapter->addr + ECM_SYSTEM_TIME_LOW);
  13. high_before = high;
  14. high = ioread32(adapter->addr + ECM_SYSTEM_TIME_HIGH);
  15. } while (high != high_before);
  16. *time = (((u64)high) << 32) | ((u64)low);
  17. }
  18. int tsnep_ptp_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  19. {
  20. struct tsnep_adapter *adapter = netdev_priv(netdev);
  21. struct hwtstamp_config config;
  22. if (!ifr)
  23. return -EINVAL;
  24. if (cmd == SIOCSHWTSTAMP) {
  25. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  26. return -EFAULT;
  27. switch (config.tx_type) {
  28. case HWTSTAMP_TX_OFF:
  29. case HWTSTAMP_TX_ON:
  30. break;
  31. default:
  32. return -ERANGE;
  33. }
  34. switch (config.rx_filter) {
  35. case HWTSTAMP_FILTER_NONE:
  36. break;
  37. case HWTSTAMP_FILTER_ALL:
  38. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  39. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  40. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  41. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  42. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  43. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  44. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  45. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  46. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  47. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  48. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  49. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  50. case HWTSTAMP_FILTER_NTP_ALL:
  51. config.rx_filter = HWTSTAMP_FILTER_ALL;
  52. break;
  53. default:
  54. return -ERANGE;
  55. }
  56. memcpy(&adapter->hwtstamp_config, &config,
  57. sizeof(adapter->hwtstamp_config));
  58. }
  59. if (copy_to_user(ifr->ifr_data, &adapter->hwtstamp_config,
  60. sizeof(adapter->hwtstamp_config)))
  61. return -EFAULT;
  62. return 0;
  63. }
  64. static int tsnep_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
  65. {
  66. struct tsnep_adapter *adapter = container_of(ptp, struct tsnep_adapter,
  67. ptp_clock_info);
  68. bool negative = false;
  69. u64 rate_offset;
  70. if (scaled_ppm < 0) {
  71. scaled_ppm = -scaled_ppm;
  72. negative = true;
  73. }
  74. /* convert from 16 bit to 32 bit binary fractional, divide by 1000000 to
  75. * eliminate ppm, multiply with 8 to compensate 8ns clock cycle time,
  76. * simplify calculation because 15625 * 8 = 1000000 / 8
  77. */
  78. rate_offset = scaled_ppm;
  79. rate_offset <<= 16 - 3;
  80. rate_offset = div_u64(rate_offset, 15625);
  81. rate_offset &= ECM_CLOCK_RATE_OFFSET_MASK;
  82. if (negative)
  83. rate_offset |= ECM_CLOCK_RATE_OFFSET_SIGN;
  84. iowrite32(rate_offset & 0xFFFFFFFF, adapter->addr + ECM_CLOCK_RATE);
  85. return 0;
  86. }
  87. static int tsnep_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  88. {
  89. struct tsnep_adapter *adapter = container_of(ptp, struct tsnep_adapter,
  90. ptp_clock_info);
  91. u64 system_time;
  92. unsigned long flags;
  93. spin_lock_irqsave(&adapter->ptp_lock, flags);
  94. tsnep_get_system_time(adapter, &system_time);
  95. system_time += delta;
  96. /* high dword is buffered in hardware and synchronously written to
  97. * system time when low dword is written
  98. */
  99. iowrite32(system_time >> 32, adapter->addr + ECM_SYSTEM_TIME_HIGH);
  100. iowrite32(system_time & 0xFFFFFFFF,
  101. adapter->addr + ECM_SYSTEM_TIME_LOW);
  102. spin_unlock_irqrestore(&adapter->ptp_lock, flags);
  103. return 0;
  104. }
  105. static int tsnep_ptp_gettimex64(struct ptp_clock_info *ptp,
  106. struct timespec64 *ts,
  107. struct ptp_system_timestamp *sts)
  108. {
  109. struct tsnep_adapter *adapter = container_of(ptp, struct tsnep_adapter,
  110. ptp_clock_info);
  111. u32 high_before;
  112. u32 low;
  113. u32 high;
  114. u64 system_time;
  115. /* read high dword twice to detect overrun */
  116. high = ioread32(adapter->addr + ECM_SYSTEM_TIME_HIGH);
  117. do {
  118. ptp_read_system_prets(sts);
  119. low = ioread32(adapter->addr + ECM_SYSTEM_TIME_LOW);
  120. ptp_read_system_postts(sts);
  121. high_before = high;
  122. high = ioread32(adapter->addr + ECM_SYSTEM_TIME_HIGH);
  123. } while (high != high_before);
  124. system_time = (((u64)high) << 32) | ((u64)low);
  125. *ts = ns_to_timespec64(system_time);
  126. return 0;
  127. }
  128. static int tsnep_ptp_settime64(struct ptp_clock_info *ptp,
  129. const struct timespec64 *ts)
  130. {
  131. struct tsnep_adapter *adapter = container_of(ptp, struct tsnep_adapter,
  132. ptp_clock_info);
  133. u64 system_time = timespec64_to_ns(ts);
  134. unsigned long flags;
  135. spin_lock_irqsave(&adapter->ptp_lock, flags);
  136. /* high dword is buffered in hardware and synchronously written to
  137. * system time when low dword is written
  138. */
  139. iowrite32(system_time >> 32, adapter->addr + ECM_SYSTEM_TIME_HIGH);
  140. iowrite32(system_time & 0xFFFFFFFF,
  141. adapter->addr + ECM_SYSTEM_TIME_LOW);
  142. spin_unlock_irqrestore(&adapter->ptp_lock, flags);
  143. return 0;
  144. }
  145. static int tsnep_ptp_getcyclesx64(struct ptp_clock_info *ptp,
  146. struct timespec64 *ts,
  147. struct ptp_system_timestamp *sts)
  148. {
  149. struct tsnep_adapter *adapter = container_of(ptp, struct tsnep_adapter,
  150. ptp_clock_info);
  151. u32 high_before;
  152. u32 low;
  153. u32 high;
  154. u64 counter;
  155. /* read high dword twice to detect overrun */
  156. high = ioread32(adapter->addr + ECM_COUNTER_HIGH);
  157. do {
  158. ptp_read_system_prets(sts);
  159. low = ioread32(adapter->addr + ECM_COUNTER_LOW);
  160. ptp_read_system_postts(sts);
  161. high_before = high;
  162. high = ioread32(adapter->addr + ECM_COUNTER_HIGH);
  163. } while (high != high_before);
  164. counter = (((u64)high) << 32) | ((u64)low);
  165. *ts = ns_to_timespec64(counter);
  166. return 0;
  167. }
  168. int tsnep_ptp_init(struct tsnep_adapter *adapter)
  169. {
  170. int retval = 0;
  171. adapter->hwtstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
  172. adapter->hwtstamp_config.tx_type = HWTSTAMP_TX_OFF;
  173. snprintf(adapter->ptp_clock_info.name, 16, "%s", TSNEP);
  174. adapter->ptp_clock_info.owner = THIS_MODULE;
  175. /* at most 2^-1ns adjustment every clock cycle for 8ns clock cycle time,
  176. * stay slightly below because only bits below 2^-1ns are supported
  177. */
  178. adapter->ptp_clock_info.max_adj = (500000000 / 8 - 1);
  179. adapter->ptp_clock_info.adjfine = tsnep_ptp_adjfine;
  180. adapter->ptp_clock_info.adjtime = tsnep_ptp_adjtime;
  181. adapter->ptp_clock_info.gettimex64 = tsnep_ptp_gettimex64;
  182. adapter->ptp_clock_info.settime64 = tsnep_ptp_settime64;
  183. adapter->ptp_clock_info.getcyclesx64 = tsnep_ptp_getcyclesx64;
  184. spin_lock_init(&adapter->ptp_lock);
  185. adapter->ptp_clock = ptp_clock_register(&adapter->ptp_clock_info,
  186. &adapter->pdev->dev);
  187. if (IS_ERR(adapter->ptp_clock)) {
  188. netdev_err(adapter->netdev, "ptp_clock_register failed\n");
  189. retval = PTR_ERR(adapter->ptp_clock);
  190. adapter->ptp_clock = NULL;
  191. } else if (adapter->ptp_clock) {
  192. netdev_info(adapter->netdev, "PHC added\n");
  193. }
  194. return retval;
  195. }
  196. void tsnep_ptp_cleanup(struct tsnep_adapter *adapter)
  197. {
  198. if (adapter->ptp_clock) {
  199. ptp_clock_unregister(adapter->ptp_clock);
  200. netdev_info(adapter->netdev, "PHC removed\n");
  201. }
  202. }