stmmac_ptp.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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->ioaddr, addend);
  47. spin_unlock_irqrestore(&priv->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->ioaddr, sec, nsec, neg_adj);
  75. spin_unlock_irqrestore(&priv->lock, flags);
  76. return 0;
  77. }
  78. /**
  79. * stmmac_get_time
  80. *
  81. * @ptp: pointer to ptp_clock_info structure
  82. * @ts: pointer to hold time/result
  83. *
  84. * Description: this function will read the current time from the
  85. * hardware clock and store it in @ts.
  86. */
  87. static int stmmac_get_time(struct ptp_clock_info *ptp, struct timespec *ts)
  88. {
  89. struct stmmac_priv *priv =
  90. container_of(ptp, struct stmmac_priv, ptp_clock_ops);
  91. unsigned long flags;
  92. u64 ns;
  93. u32 reminder;
  94. spin_lock_irqsave(&priv->ptp_lock, flags);
  95. ns = priv->hw->ptp->get_systime(priv->ioaddr);
  96. spin_unlock_irqrestore(&priv->ptp_lock, flags);
  97. ts->tv_sec = div_u64_rem(ns, 1000000000ULL, &reminder);
  98. ts->tv_nsec = reminder;
  99. return 0;
  100. }
  101. /**
  102. * stmmac_set_time
  103. *
  104. * @ptp: pointer to ptp_clock_info structure
  105. * @ts: time value to set
  106. *
  107. * Description: this function will set the current time on the
  108. * hardware clock.
  109. */
  110. static int stmmac_set_time(struct ptp_clock_info *ptp,
  111. const struct timespec *ts)
  112. {
  113. struct stmmac_priv *priv =
  114. container_of(ptp, struct stmmac_priv, ptp_clock_ops);
  115. unsigned long flags;
  116. spin_lock_irqsave(&priv->ptp_lock, flags);
  117. priv->hw->ptp->init_systime(priv->ioaddr, ts->tv_sec, ts->tv_nsec);
  118. spin_unlock_irqrestore(&priv->ptp_lock, flags);
  119. return 0;
  120. }
  121. static int stmmac_enable(struct ptp_clock_info *ptp,
  122. struct ptp_clock_request *rq, int on)
  123. {
  124. return -EOPNOTSUPP;
  125. }
  126. /* structure describing a PTP hardware clock */
  127. static struct ptp_clock_info stmmac_ptp_clock_ops = {
  128. .owner = THIS_MODULE,
  129. .name = "stmmac_ptp_clock",
  130. .max_adj = 62500000,
  131. .n_alarm = 0,
  132. .n_ext_ts = 0,
  133. .n_per_out = 0,
  134. .pps = 0,
  135. .adjfreq = stmmac_adjust_freq,
  136. .adjtime = stmmac_adjust_time,
  137. .gettime = stmmac_get_time,
  138. .settime = 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. int 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. priv->ptp_clock = NULL;
  155. pr_err("ptp_clock_register() failed on %s\n", priv->dev->name);
  156. } else
  157. pr_debug("Added PTP HW clock successfully on %s\n",
  158. priv->dev->name);
  159. return 0;
  160. }
  161. /**
  162. * stmmac_ptp_unregister
  163. * @priv: driver private structure
  164. * Description: this function will remove/unregister the ptp clock driver
  165. * from the kernel.
  166. */
  167. void stmmac_ptp_unregister(struct stmmac_priv *priv)
  168. {
  169. if (priv->ptp_clock) {
  170. ptp_clock_unregister(priv->ptp_clock);
  171. pr_debug("Removed PTP HW clock successfully on %s\n",
  172. priv->dev->name);
  173. }
  174. }