stmmac_ptp.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. The full GNU General Public License is included in this distribution in
  12. the file called "COPYING".
  13. Author: Rayagond Kokatanur <rayagond@vayavyalabs.com>
  14. *******************************************************************************/
  15. #include "stmmac.h"
  16. #include "stmmac_ptp.h"
  17. /**
  18. * stmmac_adjust_freq
  19. *
  20. * @ptp: pointer to ptp_clock_info structure
  21. * @ppb: desired period change in parts ber billion
  22. *
  23. * Description: this function will adjust the frequency of hardware clock.
  24. */
  25. static int stmmac_adjust_freq(struct ptp_clock_info *ptp, s32 ppb)
  26. {
  27. struct stmmac_priv *priv =
  28. container_of(ptp, struct stmmac_priv, ptp_clock_ops);
  29. unsigned long flags;
  30. u32 diff, addend;
  31. int neg_adj = 0;
  32. u64 adj;
  33. if (ppb < 0) {
  34. neg_adj = 1;
  35. ppb = -ppb;
  36. }
  37. addend = priv->default_addend;
  38. adj = addend;
  39. adj *= ppb;
  40. diff = div_u64(adj, 1000000000ULL);
  41. addend = neg_adj ? (addend - diff) : (addend + diff);
  42. spin_lock_irqsave(&priv->ptp_lock, flags);
  43. stmmac_config_addend(priv, priv->ptpaddr, addend);
  44. spin_unlock_irqrestore(&priv->ptp_lock, flags);
  45. return 0;
  46. }
  47. /**
  48. * stmmac_adjust_time
  49. *
  50. * @ptp: pointer to ptp_clock_info structure
  51. * @delta: desired change in nanoseconds
  52. *
  53. * Description: this function will shift/adjust the hardware clock time.
  54. */
  55. static int stmmac_adjust_time(struct ptp_clock_info *ptp, s64 delta)
  56. {
  57. struct stmmac_priv *priv =
  58. container_of(ptp, struct stmmac_priv, ptp_clock_ops);
  59. unsigned long flags;
  60. u32 sec, nsec;
  61. u32 quotient, reminder;
  62. int neg_adj = 0;
  63. bool xmac;
  64. xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
  65. if (delta < 0) {
  66. neg_adj = 1;
  67. delta = -delta;
  68. }
  69. quotient = div_u64_rem(delta, 1000000000ULL, &reminder);
  70. sec = quotient;
  71. nsec = reminder;
  72. spin_lock_irqsave(&priv->ptp_lock, flags);
  73. stmmac_adjust_systime(priv, priv->ptpaddr, sec, nsec, neg_adj, xmac);
  74. spin_unlock_irqrestore(&priv->ptp_lock, flags);
  75. return 0;
  76. }
  77. /**
  78. * stmmac_get_time
  79. *
  80. * @ptp: pointer to ptp_clock_info structure
  81. * @ts: pointer to hold time/result
  82. *
  83. * Description: this function will read the current time from the
  84. * hardware clock and store it in @ts.
  85. */
  86. static int stmmac_get_time(struct ptp_clock_info *ptp, struct timespec64 *ts)
  87. {
  88. struct stmmac_priv *priv =
  89. container_of(ptp, struct stmmac_priv, ptp_clock_ops);
  90. unsigned long flags;
  91. u64 ns = 0;
  92. spin_lock_irqsave(&priv->ptp_lock, flags);
  93. stmmac_get_systime(priv, priv->ptpaddr, &ns);
  94. spin_unlock_irqrestore(&priv->ptp_lock, flags);
  95. *ts = ns_to_timespec64(ns);
  96. return 0;
  97. }
  98. /**
  99. * stmmac_set_time
  100. *
  101. * @ptp: pointer to ptp_clock_info structure
  102. * @ts: time value to set
  103. *
  104. * Description: this function will set the current time on the
  105. * hardware clock.
  106. */
  107. static int stmmac_set_time(struct ptp_clock_info *ptp,
  108. const struct timespec64 *ts)
  109. {
  110. struct stmmac_priv *priv =
  111. container_of(ptp, struct stmmac_priv, ptp_clock_ops);
  112. unsigned long flags;
  113. spin_lock_irqsave(&priv->ptp_lock, flags);
  114. stmmac_init_systime(priv, priv->ptpaddr, ts->tv_sec, ts->tv_nsec);
  115. spin_unlock_irqrestore(&priv->ptp_lock, flags);
  116. return 0;
  117. }
  118. static int stmmac_enable(struct ptp_clock_info *ptp,
  119. struct ptp_clock_request *rq, int on)
  120. {
  121. struct stmmac_priv *priv =
  122. container_of(ptp, struct stmmac_priv, ptp_clock_ops);
  123. struct stmmac_pps_cfg *cfg;
  124. int ret = -EOPNOTSUPP;
  125. unsigned long flags;
  126. switch (rq->type) {
  127. case PTP_CLK_REQ_PEROUT:
  128. cfg = &priv->pps[rq->perout.index];
  129. cfg->start.tv_sec = rq->perout.start.sec;
  130. cfg->start.tv_nsec = rq->perout.start.nsec;
  131. cfg->period.tv_sec = rq->perout.period.sec;
  132. cfg->period.tv_nsec = rq->perout.period.nsec;
  133. spin_lock_irqsave(&priv->ptp_lock, flags);
  134. ret = stmmac_flex_pps_config(priv, priv->ioaddr,
  135. rq->perout.index, cfg, on,
  136. priv->sub_second_inc,
  137. priv->systime_flags);
  138. spin_unlock_irqrestore(&priv->ptp_lock, flags);
  139. break;
  140. default:
  141. break;
  142. }
  143. return ret;
  144. }
  145. /* structure describing a PTP hardware clock */
  146. static struct ptp_clock_info stmmac_ptp_clock_ops = {
  147. .owner = THIS_MODULE,
  148. .name = "stmmac ptp",
  149. .max_adj = 62500000,
  150. .n_alarm = 0,
  151. .n_ext_ts = 0,
  152. .n_per_out = 0, /* will be overwritten in stmmac_ptp_register */
  153. .n_pins = 0,
  154. .pps = 0,
  155. .adjfreq = stmmac_adjust_freq,
  156. .adjtime = stmmac_adjust_time,
  157. .gettime64 = stmmac_get_time,
  158. .settime64 = stmmac_set_time,
  159. .enable = stmmac_enable,
  160. };
  161. /**
  162. * stmmac_ptp_register
  163. * @priv: driver private structure
  164. * Description: this function will register the ptp clock driver
  165. * to kernel. It also does some house keeping work.
  166. */
  167. void stmmac_ptp_register(struct stmmac_priv *priv)
  168. {
  169. int i;
  170. for (i = 0; i < priv->dma_cap.pps_out_num; i++) {
  171. if (i >= STMMAC_PPS_MAX)
  172. break;
  173. priv->pps[i].available = true;
  174. }
  175. stmmac_ptp_clock_ops.n_per_out = priv->dma_cap.pps_out_num;
  176. spin_lock_init(&priv->ptp_lock);
  177. priv->ptp_clock_ops = stmmac_ptp_clock_ops;
  178. priv->ptp_clock = ptp_clock_register(&priv->ptp_clock_ops,
  179. priv->device);
  180. if (IS_ERR(priv->ptp_clock)) {
  181. netdev_err(priv->dev, "ptp_clock_register failed\n");
  182. priv->ptp_clock = NULL;
  183. } else if (priv->ptp_clock)
  184. netdev_info(priv->dev, "registered PTP clock\n");
  185. }
  186. /**
  187. * stmmac_ptp_unregister
  188. * @priv: driver private structure
  189. * Description: this function will remove/unregister the ptp clock driver
  190. * from the kernel.
  191. */
  192. void stmmac_ptp_unregister(struct stmmac_priv *priv)
  193. {
  194. if (priv->ptp_clock) {
  195. ptp_clock_unregister(priv->ptp_clock);
  196. priv->ptp_clock = NULL;
  197. pr_debug("Removed PTP HW clock successfully on %s\n",
  198. priv->dev->name);
  199. }
  200. }