stmmac_ptp.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*******************************************************************************
  2. PTP 1588 clock using the STMMAC.
  3. Copyright (C) 2013 Vayavya Labs Pvt Ltd
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms and conditions of the GNU General Public License,
  6. version 2, as published by the Free Software Foundation.
  7. This program is distributed in the hope it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  10. more details.
  11. You should have received a copy of the GNU General Public License along with
  12. this program; if not, write to the Free Software Foundation, Inc.,
  13. 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  14. The full GNU General Public License is included in this distribution in
  15. the file called "COPYING".
  16. Author: Rayagond Kokatanur <rayagond@vayavyalabs.com>
  17. *******************************************************************************/
  18. #include "stmmac.h"
  19. #include "stmmac_ptp.h"
  20. /**
  21. * stmmac_adjust_freq
  22. *
  23. * @ptp: pointer to ptp_clock_info structure
  24. * @ppb: desired period change in parts ber billion
  25. *
  26. * Description: this function will adjust the frequency of hardware clock.
  27. */
  28. static int stmmac_adjust_freq(struct ptp_clock_info *ptp, s32 ppb)
  29. {
  30. struct stmmac_priv *priv =
  31. container_of(ptp, struct stmmac_priv, ptp_clock_ops);
  32. unsigned long flags;
  33. u32 diff, addend;
  34. int neg_adj = 0;
  35. u64 adj;
  36. if (ppb < 0) {
  37. neg_adj = 1;
  38. ppb = -ppb;
  39. }
  40. addend = priv->default_addend;
  41. adj = addend;
  42. adj *= ppb;
  43. diff = div_u64(adj, 1000000000ULL);
  44. addend = neg_adj ? (addend - diff) : (addend + diff);
  45. spin_lock_irqsave(&priv->ptp_lock, flags);
  46. priv->hw->ptp->config_addend(priv->ptpaddr, addend);
  47. spin_unlock_irqrestore(&priv->ptp_lock, flags);
  48. return 0;
  49. }
  50. /**
  51. * stmmac_adjust_time
  52. *
  53. * @ptp: pointer to ptp_clock_info structure
  54. * @delta: desired change in nanoseconds
  55. *
  56. * Description: this function will shift/adjust the hardware clock time.
  57. */
  58. static int stmmac_adjust_time(struct ptp_clock_info *ptp, s64 delta)
  59. {
  60. struct stmmac_priv *priv =
  61. container_of(ptp, struct stmmac_priv, ptp_clock_ops);
  62. unsigned long flags;
  63. u32 sec, nsec;
  64. u32 quotient, reminder;
  65. int neg_adj = 0;
  66. if (delta < 0) {
  67. neg_adj = 1;
  68. delta = -delta;
  69. }
  70. quotient = div_u64_rem(delta, 1000000000ULL, &reminder);
  71. sec = quotient;
  72. nsec = reminder;
  73. spin_lock_irqsave(&priv->ptp_lock, flags);
  74. priv->hw->ptp->adjust_systime(priv->ptpaddr, sec, nsec, neg_adj,
  75. priv->plat->has_gmac4);
  76. spin_unlock_irqrestore(&priv->ptp_lock, flags);
  77. return 0;
  78. }
  79. /**
  80. * stmmac_get_time
  81. *
  82. * @ptp: pointer to ptp_clock_info structure
  83. * @ts: pointer to hold time/result
  84. *
  85. * Description: this function will read the current time from the
  86. * hardware clock and store it in @ts.
  87. */
  88. static int stmmac_get_time(struct ptp_clock_info *ptp, struct timespec64 *ts)
  89. {
  90. struct stmmac_priv *priv =
  91. container_of(ptp, struct stmmac_priv, ptp_clock_ops);
  92. unsigned long flags;
  93. u64 ns;
  94. spin_lock_irqsave(&priv->ptp_lock, flags);
  95. ns = priv->hw->ptp->get_systime(priv->ptpaddr);
  96. spin_unlock_irqrestore(&priv->ptp_lock, flags);
  97. *ts = ns_to_timespec64(ns);
  98. return 0;
  99. }
  100. /**
  101. * stmmac_set_time
  102. *
  103. * @ptp: pointer to ptp_clock_info structure
  104. * @ts: time value to set
  105. *
  106. * Description: this function will set the current time on the
  107. * hardware clock.
  108. */
  109. static int stmmac_set_time(struct ptp_clock_info *ptp,
  110. const struct timespec64 *ts)
  111. {
  112. struct stmmac_priv *priv =
  113. container_of(ptp, struct stmmac_priv, ptp_clock_ops);
  114. unsigned long flags;
  115. spin_lock_irqsave(&priv->ptp_lock, flags);
  116. priv->hw->ptp->init_systime(priv->ptpaddr, ts->tv_sec, ts->tv_nsec);
  117. spin_unlock_irqrestore(&priv->ptp_lock, flags);
  118. return 0;
  119. }
  120. static int stmmac_enable(struct ptp_clock_info *ptp,
  121. struct ptp_clock_request *rq, int on)
  122. {
  123. return -EOPNOTSUPP;
  124. }
  125. /* structure describing a PTP hardware clock */
  126. static struct ptp_clock_info stmmac_ptp_clock_ops = {
  127. .owner = THIS_MODULE,
  128. .name = "stmmac_ptp_clock",
  129. .max_adj = 62500000,
  130. .n_alarm = 0,
  131. .n_ext_ts = 0,
  132. .n_per_out = 0,
  133. .n_pins = 0,
  134. .pps = 0,
  135. .adjfreq = stmmac_adjust_freq,
  136. .adjtime = stmmac_adjust_time,
  137. .gettime64 = stmmac_get_time,
  138. .settime64 = stmmac_set_time,
  139. .enable = stmmac_enable,
  140. };
  141. /**
  142. * stmmac_ptp_register
  143. * @priv: driver private structure
  144. * Description: this function will register the ptp clock driver
  145. * to kernel. It also does some house keeping work.
  146. */
  147. void stmmac_ptp_register(struct stmmac_priv *priv)
  148. {
  149. spin_lock_init(&priv->ptp_lock);
  150. priv->ptp_clock_ops = stmmac_ptp_clock_ops;
  151. priv->ptp_clock = ptp_clock_register(&priv->ptp_clock_ops,
  152. priv->device);
  153. if (IS_ERR(priv->ptp_clock)) {
  154. netdev_err(priv->dev, "ptp_clock_register failed\n");
  155. priv->ptp_clock = NULL;
  156. } else if (priv->ptp_clock)
  157. netdev_info(priv->dev, "registered PTP clock\n");
  158. }
  159. /**
  160. * stmmac_ptp_unregister
  161. * @priv: driver private structure
  162. * Description: this function will remove/unregister the ptp clock driver
  163. * from the kernel.
  164. */
  165. void stmmac_ptp_unregister(struct stmmac_priv *priv)
  166. {
  167. if (priv->ptp_clock) {
  168. ptp_clock_unregister(priv->ptp_clock);
  169. priv->ptp_clock = NULL;
  170. pr_debug("Removed PTP HW clock successfully on %s\n",
  171. priv->dev->name);
  172. }
  173. }