ravb_ptp.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* PTP 1588 clock using the Renesas Ethernet AVB
  3. *
  4. * Copyright (C) 2013-2015 Renesas Electronics Corporation
  5. * Copyright (C) 2015 Renesas Solutions Corp.
  6. * Copyright (C) 2015-2016 Cogent Embedded, Inc. <source@cogentembedded.com>
  7. */
  8. #include "ravb.h"
  9. static int ravb_ptp_tcr_request(struct ravb_private *priv, u32 request)
  10. {
  11. struct net_device *ndev = priv->ndev;
  12. int error;
  13. error = ravb_wait(ndev, GCCR, GCCR_TCR, GCCR_TCR_NOREQ);
  14. if (error)
  15. return error;
  16. ravb_modify(ndev, GCCR, request, request);
  17. return ravb_wait(ndev, GCCR, GCCR_TCR, GCCR_TCR_NOREQ);
  18. }
  19. /* Caller must hold the lock */
  20. static int ravb_ptp_time_read(struct ravb_private *priv, struct timespec64 *ts)
  21. {
  22. struct net_device *ndev = priv->ndev;
  23. int error;
  24. error = ravb_ptp_tcr_request(priv, GCCR_TCR_CAPTURE);
  25. if (error)
  26. return error;
  27. ts->tv_nsec = ravb_read(ndev, GCT0);
  28. ts->tv_sec = ravb_read(ndev, GCT1) |
  29. ((s64)ravb_read(ndev, GCT2) << 32);
  30. return 0;
  31. }
  32. /* Caller must hold the lock */
  33. static int ravb_ptp_time_write(struct ravb_private *priv,
  34. const struct timespec64 *ts)
  35. {
  36. struct net_device *ndev = priv->ndev;
  37. int error;
  38. u32 gccr;
  39. error = ravb_ptp_tcr_request(priv, GCCR_TCR_RESET);
  40. if (error)
  41. return error;
  42. gccr = ravb_read(ndev, GCCR);
  43. if (gccr & GCCR_LTO)
  44. return -EBUSY;
  45. ravb_write(ndev, ts->tv_nsec, GTO0);
  46. ravb_write(ndev, ts->tv_sec, GTO1);
  47. ravb_write(ndev, (ts->tv_sec >> 32) & 0xffff, GTO2);
  48. ravb_write(ndev, gccr | GCCR_LTO, GCCR);
  49. return 0;
  50. }
  51. /* Caller must hold the lock */
  52. static int ravb_ptp_update_compare(struct ravb_private *priv, u32 ns)
  53. {
  54. struct net_device *ndev = priv->ndev;
  55. /* When the comparison value (GPTC.PTCV) is in range of
  56. * [x-1 to x+1] (x is the configured increment value in
  57. * GTI.TIV), it may happen that a comparison match is
  58. * not detected when the timer wraps around.
  59. */
  60. u32 gti_ns_plus_1 = (priv->ptp.current_addend >> 20) + 1;
  61. u32 gccr;
  62. if (ns < gti_ns_plus_1)
  63. ns = gti_ns_plus_1;
  64. else if (ns > 0 - gti_ns_plus_1)
  65. ns = 0 - gti_ns_plus_1;
  66. gccr = ravb_read(ndev, GCCR);
  67. if (gccr & GCCR_LPTC)
  68. return -EBUSY;
  69. ravb_write(ndev, ns, GPTC);
  70. ravb_write(ndev, gccr | GCCR_LPTC, GCCR);
  71. return 0;
  72. }
  73. /* PTP clock operations */
  74. static int ravb_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
  75. {
  76. struct ravb_private *priv = container_of(ptp, struct ravb_private,
  77. ptp.info);
  78. struct net_device *ndev = priv->ndev;
  79. unsigned long flags;
  80. u32 diff, addend;
  81. bool neg_adj = false;
  82. u32 gccr;
  83. if (ppb < 0) {
  84. neg_adj = true;
  85. ppb = -ppb;
  86. }
  87. addend = priv->ptp.default_addend;
  88. diff = div_u64((u64)addend * ppb, NSEC_PER_SEC);
  89. addend = neg_adj ? addend - diff : addend + diff;
  90. spin_lock_irqsave(&priv->lock, flags);
  91. priv->ptp.current_addend = addend;
  92. gccr = ravb_read(ndev, GCCR);
  93. if (gccr & GCCR_LTI) {
  94. spin_unlock_irqrestore(&priv->lock, flags);
  95. return -EBUSY;
  96. }
  97. ravb_write(ndev, addend & GTI_TIV, GTI);
  98. ravb_write(ndev, gccr | GCCR_LTI, GCCR);
  99. spin_unlock_irqrestore(&priv->lock, flags);
  100. return 0;
  101. }
  102. static int ravb_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
  103. {
  104. struct ravb_private *priv = container_of(ptp, struct ravb_private,
  105. ptp.info);
  106. struct timespec64 ts;
  107. unsigned long flags;
  108. int error;
  109. spin_lock_irqsave(&priv->lock, flags);
  110. error = ravb_ptp_time_read(priv, &ts);
  111. if (!error) {
  112. u64 now = ktime_to_ns(timespec64_to_ktime(ts));
  113. ts = ns_to_timespec64(now + delta);
  114. error = ravb_ptp_time_write(priv, &ts);
  115. }
  116. spin_unlock_irqrestore(&priv->lock, flags);
  117. return error;
  118. }
  119. static int ravb_ptp_gettime64(struct ptp_clock_info *ptp, struct timespec64 *ts)
  120. {
  121. struct ravb_private *priv = container_of(ptp, struct ravb_private,
  122. ptp.info);
  123. unsigned long flags;
  124. int error;
  125. spin_lock_irqsave(&priv->lock, flags);
  126. error = ravb_ptp_time_read(priv, ts);
  127. spin_unlock_irqrestore(&priv->lock, flags);
  128. return error;
  129. }
  130. static int ravb_ptp_settime64(struct ptp_clock_info *ptp,
  131. const struct timespec64 *ts)
  132. {
  133. struct ravb_private *priv = container_of(ptp, struct ravb_private,
  134. ptp.info);
  135. unsigned long flags;
  136. int error;
  137. spin_lock_irqsave(&priv->lock, flags);
  138. error = ravb_ptp_time_write(priv, ts);
  139. spin_unlock_irqrestore(&priv->lock, flags);
  140. return error;
  141. }
  142. static int ravb_ptp_extts(struct ptp_clock_info *ptp,
  143. struct ptp_extts_request *req, int on)
  144. {
  145. struct ravb_private *priv = container_of(ptp, struct ravb_private,
  146. ptp.info);
  147. struct net_device *ndev = priv->ndev;
  148. unsigned long flags;
  149. if (req->index)
  150. return -EINVAL;
  151. if (priv->ptp.extts[req->index] == on)
  152. return 0;
  153. priv->ptp.extts[req->index] = on;
  154. spin_lock_irqsave(&priv->lock, flags);
  155. if (priv->chip_id == RCAR_GEN2)
  156. ravb_modify(ndev, GIC, GIC_PTCE, on ? GIC_PTCE : 0);
  157. else if (on)
  158. ravb_write(ndev, GIE_PTCS, GIE);
  159. else
  160. ravb_write(ndev, GID_PTCD, GID);
  161. mmiowb();
  162. spin_unlock_irqrestore(&priv->lock, flags);
  163. return 0;
  164. }
  165. static int ravb_ptp_perout(struct ptp_clock_info *ptp,
  166. struct ptp_perout_request *req, int on)
  167. {
  168. struct ravb_private *priv = container_of(ptp, struct ravb_private,
  169. ptp.info);
  170. struct net_device *ndev = priv->ndev;
  171. struct ravb_ptp_perout *perout;
  172. unsigned long flags;
  173. int error = 0;
  174. if (req->index)
  175. return -EINVAL;
  176. if (on) {
  177. u64 start_ns;
  178. u64 period_ns;
  179. start_ns = req->start.sec * NSEC_PER_SEC + req->start.nsec;
  180. period_ns = req->period.sec * NSEC_PER_SEC + req->period.nsec;
  181. if (start_ns > U32_MAX) {
  182. netdev_warn(ndev,
  183. "ptp: start value (nsec) is over limit. Maximum size of start is only 32 bits\n");
  184. return -ERANGE;
  185. }
  186. if (period_ns > U32_MAX) {
  187. netdev_warn(ndev,
  188. "ptp: period value (nsec) is over limit. Maximum size of period is only 32 bits\n");
  189. return -ERANGE;
  190. }
  191. spin_lock_irqsave(&priv->lock, flags);
  192. perout = &priv->ptp.perout[req->index];
  193. perout->target = (u32)start_ns;
  194. perout->period = (u32)period_ns;
  195. error = ravb_ptp_update_compare(priv, (u32)start_ns);
  196. if (!error) {
  197. /* Unmask interrupt */
  198. if (priv->chip_id == RCAR_GEN2)
  199. ravb_modify(ndev, GIC, GIC_PTME, GIC_PTME);
  200. else
  201. ravb_write(ndev, GIE_PTMS0, GIE);
  202. }
  203. } else {
  204. spin_lock_irqsave(&priv->lock, flags);
  205. perout = &priv->ptp.perout[req->index];
  206. perout->period = 0;
  207. /* Mask interrupt */
  208. if (priv->chip_id == RCAR_GEN2)
  209. ravb_modify(ndev, GIC, GIC_PTME, 0);
  210. else
  211. ravb_write(ndev, GID_PTMD0, GID);
  212. }
  213. mmiowb();
  214. spin_unlock_irqrestore(&priv->lock, flags);
  215. return error;
  216. }
  217. static int ravb_ptp_enable(struct ptp_clock_info *ptp,
  218. struct ptp_clock_request *req, int on)
  219. {
  220. switch (req->type) {
  221. case PTP_CLK_REQ_EXTTS:
  222. return ravb_ptp_extts(ptp, &req->extts, on);
  223. case PTP_CLK_REQ_PEROUT:
  224. return ravb_ptp_perout(ptp, &req->perout, on);
  225. default:
  226. return -EOPNOTSUPP;
  227. }
  228. }
  229. static const struct ptp_clock_info ravb_ptp_info = {
  230. .owner = THIS_MODULE,
  231. .name = "ravb clock",
  232. .max_adj = 50000000,
  233. .n_ext_ts = N_EXT_TS,
  234. .n_per_out = N_PER_OUT,
  235. .adjfreq = ravb_ptp_adjfreq,
  236. .adjtime = ravb_ptp_adjtime,
  237. .gettime64 = ravb_ptp_gettime64,
  238. .settime64 = ravb_ptp_settime64,
  239. .enable = ravb_ptp_enable,
  240. };
  241. /* Caller must hold the lock */
  242. void ravb_ptp_interrupt(struct net_device *ndev)
  243. {
  244. struct ravb_private *priv = netdev_priv(ndev);
  245. u32 gis = ravb_read(ndev, GIS);
  246. gis &= ravb_read(ndev, GIC);
  247. if (gis & GIS_PTCF) {
  248. struct ptp_clock_event event;
  249. event.type = PTP_CLOCK_EXTTS;
  250. event.index = 0;
  251. event.timestamp = ravb_read(ndev, GCPT);
  252. ptp_clock_event(priv->ptp.clock, &event);
  253. }
  254. if (gis & GIS_PTMF) {
  255. struct ravb_ptp_perout *perout = priv->ptp.perout;
  256. if (perout->period) {
  257. perout->target += perout->period;
  258. ravb_ptp_update_compare(priv, perout->target);
  259. }
  260. }
  261. ravb_write(ndev, ~(gis | GIS_RESERVED), GIS);
  262. }
  263. void ravb_ptp_init(struct net_device *ndev, struct platform_device *pdev)
  264. {
  265. struct ravb_private *priv = netdev_priv(ndev);
  266. unsigned long flags;
  267. priv->ptp.info = ravb_ptp_info;
  268. priv->ptp.default_addend = ravb_read(ndev, GTI);
  269. priv->ptp.current_addend = priv->ptp.default_addend;
  270. spin_lock_irqsave(&priv->lock, flags);
  271. ravb_wait(ndev, GCCR, GCCR_TCR, GCCR_TCR_NOREQ);
  272. ravb_modify(ndev, GCCR, GCCR_TCSS, GCCR_TCSS_ADJGPTP);
  273. mmiowb();
  274. spin_unlock_irqrestore(&priv->lock, flags);
  275. priv->ptp.clock = ptp_clock_register(&priv->ptp.info, &pdev->dev);
  276. }
  277. void ravb_ptp_stop(struct net_device *ndev)
  278. {
  279. struct ravb_private *priv = netdev_priv(ndev);
  280. ravb_write(ndev, 0, GIC);
  281. ravb_write(ndev, 0, GIS);
  282. ptp_clock_unregister(priv->ptp.clock);
  283. }